Skip to content

feat(providers): add llm transport adapter seam - #60264

Merged
vincentkoc merged 4 commits into
mainfrom
refactor/llm-transport-adapter-fresh
Apr 3, 2026
Merged

vincentkoc merged 4 commits into
mainfrom
refactor/llm-transport-adapter-fresh

Conversation

@vincentkoc

Copy link
Copy Markdown
Member

Summary

  • add an internal LLM transport adapter seam for OpenAI-family model-provider traffic
  • preserve model-provider request.proxy / request.tls on resolved models instead of dropping them
  • route simple-completion and provider stream registration through transport-aware custom APIs when transport overrides are present

Why

The shared request-policy layer already understands proxy and TLS transport overrides, but the generic LLM model path still dropped them before request execution. This PR adds the runtime seam needed to carry those overrides end to end for OpenAI-family traffic without widening the public config surface further than runtime can support.

Testing

  • pnpm test -- src/agents/openai-transport-stream.test.ts src/agents/simple-completion-transport.test.ts src/agents/provider-request-config.test.ts src/agents/pi-embedded-runner/model.test.ts

Notes

  • This PR intentionally does not re-open the full models.providers.*.request schema/secret surface for proxy/TLS yet.
  • The next follow-up can either widen that public config surface with the required secret-matrix updates, or continue provider-local cleanup on top of this runtime seam.

@vincentkoc vincentkoc self-assigned this Apr 3, 2026
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XL maintainer Maintainer-authored PR labels Apr 3, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review April 3, 2026 11:51
@greptile-apps

greptile-apps Bot commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds an internal LLM transport adapter seam that carries proxy and tls overrides from models.providers.*.request all the way through to OpenAI-family API calls. It introduces a new openai-transport-stream.ts module containing transport-aware StreamFn implementations for openai-responses, openai-completions, and azure-openai-responses, uses a Symbol.for-keyed property to attach transport metadata to model objects without changing the public Model type, and wires both the provider-stream and simple-completion paths to fall back to these transport stream functions when no plugin-registered stream function is present.

Key changes:

  • sanitizeConfiguredModelProviderRequest now passes proxy/tls overrides through instead of throwing, unblocking the config surface for proxy/TLS transport
  • attachModelProviderRequestTransport / getModelProviderRequestTransport use a global symbol to attach transport config to model objects in a type-safe, non-invasive way
  • Three model construction sites in pi-embedded-runner/model.ts now stamp the transport metadata onto the returned model
  • sanitizeSurrogates in openai-transport-stream.ts is a no-op stub — called on all message text before sending to OpenAI but does nothing
  • createAzureOpenAIClient defaults apiVersion to \"v1\" — Azure will reject requests with ?api-version=v1, making the azure transport path non-functional without AZURE_OPENAI_API_VERSION explicitly set

Confidence Score: 3/5

Safe to merge with caveats: the core seam is sound but the Azure transport path will silently fail without AZURE_OPENAI_API_VERSION set, and the sanitizeSurrogates no-op stub warrants a decision before landing.

The Symbol-based transport attachment, provider-stream wiring, and simple-completion path are all clean and well-tested. The two P1 issues in openai-transport-stream.ts prevent full confidence: the 'v1' Azure API version default will break every Azure request unless the operator sets the env var, and the sanitizeSurrogates stub creates false confidence that message text is being sanitized when it is not.

src/agents/openai-transport-stream.ts — the sanitizeSurrogates stub (line 89) and the 'v1' Azure API version fallback (line 1046) both need resolution before the transport implementations are production-ready.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/openai-transport-stream.ts
Line: 89-91

Comment:
**`sanitizeSurrogates` is a no-op stub**

The function is called in several places to sanitize user/tool message content before it is sent to the OpenAI API (e.g. `sanitizeSurrogates(context.systemPrompt)`, `sanitizeSurrogates(block.text)`, `sanitizeSurrogates(textResult)`), yet its body simply returns the input unchanged.

Lone surrogates (`\uD800`–`\uDFFF` without a paired surrogate) are not valid Unicode, and while `JSON.stringify` in recent V8 versions replaces them with the replacement character rather than throwing, the OpenAI SDK serialises the request body and some downstream parsers will reject such strings. More concretely, if a tool returns text that contains a lone surrogate, `processResponsesStream` / `processOpenAICompletionsStream` will forward it verbatim.

If the intent is "sanitisation is not needed yet", the call sites should be removed (or the function should be inlined as identity) so the dead indirection does not create false confidence. If the intent is to actually strip lone surrogates, the implementation is missing.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/openai-transport-stream.ts
Line: 1046

Comment:
**Wrong default Azure API version will cause all requests to fail**

When `AZURE_OPENAI_API_VERSION` is not set the `AzureOpenAI` client is constructed with `apiVersion: "v1"`. Azure OpenAI requires a date-based API version (e.g. `"2024-12-01-preview"` or `"2025-01-01-preview"`). Requests sent with `?api-version=v1` will be rejected by Azure with a 400 / 404 response, meaning the `azure-openai-responses` transport path is effectively broken unless the operator has explicitly set that env variable.

Either require the env variable (and throw a clear error when it is absent), or supply a known-valid default such as `"2024-12-01-preview"`.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/openai-transport-stream.ts
Line: 771-781

Comment:
**Dead `input instanceof Request` branch is unreachable**

On line 770, `request` is assigned as:
```ts
const request = input instanceof Request ? new Request(input, init) : undefined;
```

So when `input instanceof Request` is true, `request` is defined and `request?.url` evaluates to the request's URL, meaning the null-coalescing RHS is never evaluated. The inner `input instanceof Request` arm on line 777 is dead code.

This is harmless but may indicate a copy-paste artefact and could confuse future readers.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/openai-transport-stream.ts
Line: 1127-1128

Comment:
**Missing abort-signal guard before emitting `done` (inconsistency with the responses path)**

`createOpenAIResponsesTransportStreamFn` (line 876) and `createAzureOpenAIResponsesTransportStreamFn` (line 1002) both check `options?.signal?.aborted` after the stream finishes and throw before emitting `done`:

```ts
if (options?.signal?.aborted) {
  throw new Error("Request was aborted");
}
```

`createOpenAICompletionsTransportStreamFn` does not have this guard, so if the stream drains but the signal is already aborted, the function emits `{ type: "done" }` rather than `{ type: "error" }`. In practice the OpenAI SDK usually throws when the signal fires, but the guard makes the intent explicit and keeps the three variants consistent.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "Merge branch 'main' into refactor/llm-tr..." | Re-trigger Greptile

Comment thread src/agents/openai-transport-stream.ts Resolved
Comment thread src/agents/openai-transport-stream.ts Resolved
Comment thread src/agents/openai-transport-stream.ts Resolved
Comment thread src/agents/openai-transport-stream.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 065f2d1394

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/openai-transport-stream.ts Resolved
Comment thread src/agents/openai-transport-stream.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0f2cb6658d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/agents/openai-transport-stream.ts Resolved
Comment thread src/agents/openai-transport-stream.ts Resolved
@vincentkoc
vincentkoc merged commit 4798e12 into main Apr 3, 2026
9 checks passed
@vincentkoc
vincentkoc deleted the refactor/llm-transport-adapter-fresh branch April 3, 2026 13:45

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d4c537a169

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1276 to +1280
if (
!currentBlock ||
currentBlock.type !== "toolCall" ||
(toolCall.id && currentBlock.id !== toolCall.id)
) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Track completion tool-call chunks by tool index

processOpenAICompletionsStream chooses the active tool-call block only by comparing toolCall.id, but streamed tool_calls deltas have a required index while id is optional after the first chunk. When multiple tool calls stream in parallel, a later delta without id can be appended to the wrong in-flight block, which corrupts parsed arguments and can invoke the wrong tool. Buffer/update tool-call state by toolCall.index (or index+id), not only the optional id.

Useful? React with 👍 / 👎.

Comment on lines +567 to +570
} else if (type === "response.function_call_arguments.delta") {
if (currentItem?.type === "function_call" && currentBlock?.type === "toolCall") {
currentBlock.partialJson = `${stringifyJsonLike(currentBlock.partialJson)}${stringifyJsonLike(event.delta)}`;
currentBlock.arguments = parseStreamingJson(stringifyJsonLike(currentBlock.partialJson));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Key Responses function-call deltas by item identifier

The response.function_call_arguments.delta branch appends every delta to currentBlock without validating event.item_id/output_index. In Responses streaming, multiple function calls can be in flight, and deltas are identified by those fields; if events interleave, this code merges argument fragments into the wrong call and emits invalid tool arguments. Maintain per-item accumulators keyed by item_id (or output_index) before parsing.

Useful? React with 👍 / 👎.

Lianjinfeng123 pushed a commit to Lianjinfeng123/openclaw that referenced this pull request Apr 6, 2026
* feat(providers): add llm transport adapter seam

* fix(providers): harden openai transport adapter

* fix(providers): correct transport usage accounting
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
* feat(providers): add llm transport adapter seam

* fix(providers): harden openai transport adapter

* fix(providers): correct transport usage accounting
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
* feat(providers): add llm transport adapter seam

* fix(providers): harden openai transport adapter

* fix(providers): correct transport usage accounting
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
* feat(providers): add llm transport adapter seam

* fix(providers): harden openai transport adapter

* fix(providers): correct transport usage accounting
Nachx639 pushed a commit to Nachx639/clawdbot that referenced this pull request Jun 17, 2026
* feat(providers): add llm transport adapter seam

* fix(providers): harden openai transport adapter

* fix(providers): correct transport usage accounting
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
* feat(providers): add llm transport adapter seam

* fix(providers): harden openai transport adapter

* fix(providers): correct transport usage accounting
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request Sep 29, 2026
* feat(providers): add llm transport adapter seam

* fix(providers): harden openai transport adapter

* fix(providers): correct transport usage accounting
lhfer pushed a commit to lhfer/openclaw that referenced this pull request Oct 11, 2026
* feat(providers): add llm transport adapter seam

* fix(providers): harden openai transport adapter

* fix(providers): correct transport usage accounting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant