feat: pass context to runTools callbacks#1973
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c0494b1fdc
ℹ️ 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".
jbeckwith-oai
left a comment
There was a problem hiding this comment.
I verified the linked #597 use case against the built declarations and both runner modes. The base revision rejects a three-argument callback/context-bearing call, while this head correctly infers context.eventId; the focused runner suite (21 tests), tsc --noEmit, and an additional streaming harness all pass. The same context object is preserved and toolContext is absent from every API request.
There is one blocking soundness issue: concrete context-bearing tools can still be invoked without a context, which compiles and then supplies undefined to a callback typed as receiving the concrete context. Details and a minimal reproduction are inline. Existing two-argument callbacks remain source/runtime compatible, CI's breaking-change check and SDK tests are green, and I found no other behavioral regressions.
| /** | ||
| * Context to pass to each tool callback during this run. | ||
| */ | ||
| toolContext?: ToolContext; |
There was a problem hiding this comment.
[P1] Require context when callbacks require it
Because this property stays optional for a concrete ToolContext, the following compiles under strict (including against the built package):
const tool: RunnableToolFunction<string, { eventId: string }> = {
type: 'function',
function: { parameters: {}, description: 'x',
function: (_args, _runner, context) => context.eventId },
};
ChatCompletionRunner.runTools(openai, { model, messages, tools: [tool] });At runtime _runTools casts the omitted value to ToolContext and invokes the callback with undefined; the example throws while reading eventId. The resource helper also accepts this typed-tool form without toolContext, and the streaming params have the same issue. Please expose a context-bearing form/overload where toolContext is required, while retaining a separate optional/no-context form for existing callbacks.
Summary
toolContextoption to both streaming and non-streamingrunToolsparameterstoolContextbefore creating Chat Completions requests so it remains local to the runnerMotivation
runToolsusers currently need closures or custom wrapper types to share per-run application state across tools defined in separate modules. This adds a first-class context channel while preserving existing callback signatures and keeping the context scoped to one runner invocation.Resolves #597
Validation
pnpm run formatpnpm exec tsc --noEmitpnpm exec jest tests/lib/ChatCompletionRunFunctions.test.ts --runInBandgit diff --check