Repository navigation
feat(providers): add llm transport adapter seam - #60264
Conversation
Greptile SummaryThis PR adds an internal LLM transport adapter seam that carries Key changes:
Confidence Score: 3/5Safe 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 AIThis 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 |
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
There was a problem hiding this comment.
💡 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".
| if ( | ||
| !currentBlock || | ||
| currentBlock.type !== "toolCall" || | ||
| (toolCall.id && currentBlock.id !== toolCall.id) | ||
| ) { |
There was a problem hiding this comment.
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 👍 / 👎.
| } 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)); |
There was a problem hiding this comment.
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 👍 / 👎.
* feat(providers): add llm transport adapter seam * fix(providers): harden openai transport adapter * fix(providers): correct transport usage accounting
* feat(providers): add llm transport adapter seam * fix(providers): harden openai transport adapter * fix(providers): correct transport usage accounting
* feat(providers): add llm transport adapter seam * fix(providers): harden openai transport adapter * fix(providers): correct transport usage accounting
* feat(providers): add llm transport adapter seam * fix(providers): harden openai transport adapter * fix(providers): correct transport usage accounting
* feat(providers): add llm transport adapter seam * fix(providers): harden openai transport adapter * fix(providers): correct transport usage accounting
* feat(providers): add llm transport adapter seam * fix(providers): harden openai transport adapter * fix(providers): correct transport usage accounting
* feat(providers): add llm transport adapter seam * fix(providers): harden openai transport adapter * fix(providers): correct transport usage accounting
* feat(providers): add llm transport adapter seam * fix(providers): harden openai transport adapter * fix(providers): correct transport usage accounting
Summary
request.proxy/request.tlson resolved models instead of dropping themWhy
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.tsNotes
models.providers.*.requestschema/secret surface for proxy/TLS yet.