Skip to content

feat(gateway): P3-A three-protocol conversion — OpenAI Chat / Responses / Anthropic interop + /v1/responses (v0.4.0) - #83

Merged
argszero merged 2 commits into
mainfrom
feat/p3a-protocol-convert
Aug 18, 2026
Merged

argszero merged 2 commits into
mainfrom
feat/p3a-protocol-convert

Conversation

@argszero

Copy link
Copy Markdown
Owner

Summary

P3-A gateway protocol conversion (host rant 2026-08-18T16:15:42): any listed model can now be called via OpenAI Chat API / OpenAI Responses API / Anthropic API with automatic protocol conversion. Previously the gateway only did same-protocol passthrough — an OpenAI Chat request hitting a plan with only Anthropic endpoints returned 503.

Changes

  • src/protocol.rs (new): pairwise request/response converters between openai_chat / anthropic / responses — system↔top-level field, tool_use/tool_result ↔ tool_calls, usage prompt/completion ↔ input/output, stop/finish-reason mapping, Anthropic max_tokens default 4096. Ported/adapted from openlocalrouter src/router/transform.rs per host instruction.
  • Endpoint selection: determine_forwarding_protocol — same-protocol endpoint first (passthrough, zero conversion overhead) → Anthropic (best compatibility) → OpenAI Chat → Responses; all unavailable → 503.
  • gateway::forward: picks per-key outbound protocol, transforms the request body when crossing protocols, converts the upstream response back to the inbound protocol; usage is parsed from the upstream protocol for accurate billing.
  • New endpoint: POST /v1/responses (OpenAI Responses compatible).
  • Streaming: SSE conversion is deferred to P3-B — streaming requests requiring protocol conversion return 400 with an explicit message; same-protocol streaming stays as passthrough.
  • Version 0.3.4 → 0.4.0; README status + endpoint list updated.

Rant reference

Host rant 2026-08-18T16:15:42 — P3-A 网关协议转换(OpenAI Chat / OpenAI Responses / Anthropic 三协议互转 + /v1/responses 端点)

Tests

  • 72 → 92 tests: +15 protocol unit tests (chat→anthropic system/tool extraction, anthropic→chat tool_use, responses round-trips, dispatch), +5 gateway e2e (chat→anthropic-only plan with upstream request-body capture, anthropic→chat reverse, /v1/responses conversion, responses same-protocol passthrough, streaming+conversion → 400)
  • Fake upstreams pre-bind listeners to avoid parallel-test port collisions
  • cargo test 92/92, cargo fmt --check, cargo clippy --all-targets -- -D warnings all green
  • Live smoke (temp DB/config, anthropic-only mock plan): OpenAI Chat → 200 converted response, /v1/responses → 200, Anthropic passthrough → 200, streaming+conversion → 400; ledger records cost/consume/earn correctly

argszero added 2 commits August 18, 2026 16:41
…es / Anthropic interop + /v1/responses (v0.4.0)

Host rant 2026-08-18T16:15:42: support calling any listed model via
OpenAI Chat / OpenAI Responses / Anthropic APIs with automatic protocol
conversion (previously same-protocol passthrough only; openai request
hitting an anthropic-only plan returned 503).

- src/protocol.rs (new): pairwise request/response converters between
  openai_chat / anthropic / responses (system↔top-level, tool_use/
  tool_result ↔ tool_calls, usage prompt/completion ↔ input/output,
  stop/finish reason mapping, anthropic max_tokens default 4096),
  ported from openlocalrouter transform.rs per host instruction;
  determine_forwarding_protocol: same-protocol first → anthropic →
  openai_chat → responses fallback
- gateway: resolve_outbound picks per-key outbound protocol; request
  body transformed when crossing protocols; upstream response converted
  back to the inbound protocol; usage parsed from the upstream protocol
  for accurate billing; /v1/responses endpoint added; streaming still
  same-protocol passthrough (SSE conversion deferred to P3-B → 400 with
  explicit message)
- resolve_endpoint: responses → {base}/responses; parse_usage handles
  responses (input/output_tokens)
- tests 72→92 (+15 protocol unit tests, +5 gateway e2e: chat→anthropic
  conversion with request-body capture, anthropic→chat reverse, /v1/
  responses conversion, responses same-protocol passthrough, streaming+
  conversion 400); fake upstreams pre-bind listeners (parallel port fix)
- version 0.3.4 → 0.4.0; README status + endpoint list updated

Verified: cargo test 92/92, fmt, clippy -D warnings; live smoke (temp
DB/config, anthropic-only mock plan): openai chat → 200 converted,
/v1/responses → 200, anthropic passthrough → 200, streaming+conversion
→ 400; ledger records cost/consume/earn correctly.
CI check formatting failed on src/protocol.rs:40 (the manual_find clippy
fix was not reformatted). No behavior change.
@argszero
argszero merged commit 85ea6df into main Aug 18, 2026
1 check passed
@argszero
argszero deleted the feat/p3a-protocol-convert branch August 18, 2026 08:47
argszero added a commit that referenced this pull request Aug 18, 2026
…v0.4.1) (#84)

OpenAI-compatible GET /v1/models (alias /models) with optional auth:
- no Bearer -> plain model list (id/object/created/owned_by/display_name/context_window)
- with Bearer -> adds available_keys per model
- models table gains context_window column (ensure_column migration for existing DBs)
- seed_models reads context_window from data/models.example.json

Closes rant 2026-08-18T18:10:18 (GET /v1/models missed in P3-A #83).

Co-authored-by: argszero <argszero@argszerodeMac-mini.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant