Repository navigation
[typescript/openai-agents] add includeContext to activityAsTool to pass a copy of the run context - #218
Draft
campoy wants to merge 2 commits into
Draft
[typescript/openai-agents] add includeContext to activityAsTool to pass a copy of the run context#218campoy wants to merge 2 commits into
campoy wants to merge 2 commits into
Conversation
The README says Activity tools receive a copy of the agent context, but the Activity behind activityAsTool got only the model's arguments, and its type refused an Activity with a required second parameter. The new optional includeContext field of the tool definition makes the tool pass runContext.context as the Activity's second argument. With it set, activityAsTool also accepts an Activity type with a required second parameter; without it, that type is still refused. Without the field, the Activity gets the same input as before.
This was referenced Oct 27, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was changed
activityAsToolgets an opt-inincludeContextfield on its tool definition. WithincludeContext: true, the tool passesrunContext.contextas the Activity's second argument:ActivityToolDefinition<TActivity>now accepts an Activity with a required second parameter, but then requiresincludeContext: true. The check is part of the definition type, so it applies both to inline definitions and to definitions annotated asActivityToolDefinition<typeof activities.x>. Without the field, such a type is still refused, so a two-parameter Activity such ascredit(account, amount)keeps its compile-time error. Activities with three or more required parameters are refused. For one-argument Activities, the definition is the same as before, plus the optional field.API change: additive. The field is optional, and every definition and call that compiled before still compiles.
ActivityToolDefinitionis now a type alias rather than an interface: annotations andextendswith a concrete Activity type still work, but declaration merging and a genericinterface ... extends ActivityToolDefinition<T>do not.Old runs and rollback: a tool call schedules the same Activity as before; only its input gains a second argument when the field is set, and Temporal does not compare Activity inputs on replay. No
patchedgate is needed, and the previous release is a safe rollback (see testing).Why?
See #213. The README says "Activity tools receive a copy" of the agent context, but the Activity got only the model's arguments, and its type refused an Activity with a required second parameter. Passing the context to every Activity would break Activities that already use an optional second parameter for something else, so it is opt-in.
Checklist
Closes [Bug] openai-agents (TypeScript): Activity tools get no copy of the run context #213
How was this tested:
activityAsTool includeContext gives the Activity a copy of the run contextinsrc/__tests__/test-openai-agents.ts, with its Workflow inworkflows/openai-agents.ts: a real Worker, a run withcontext: { tenant: 't-1' }, and a fake model that callslookupwith{"key":"k"}. Its Workflow passes an Activity type with a required second parameter. Withtools.tsfrommainthat type fails to compile (TS2344), and the emitted test fails ont.deepEqual(received, [[{ key: 'k' }, { tenant: 't-1' }]])(the Activity got[{ key: 'k' }]). With this change it compiles and passes.@ts-expect-error): a two-parameter Activity withoutincludeContext, both inline and as an annotatedActivityToolDefinition;includeContext: false; a three-parameter Activity; and a one-argument definition passed where a two-parameter Activity is named. These compile: annotated and inline context-enabled definitions (with and without an explicit type argument), one-argument definitions, and an optional second parameter. Removing the@ts-expect-errorlines gives exactly one error on each refused line.make lintpasses (the 8 warningsmainhas, none in files this PR touches),make testpasses (208 tests;mainhas 207),make buildpacks, andcheck_npm_package.py,_shared/smoke.mjsandscripts/ci/check_conventions.pypass. Prettier with the SDK's settings is clean on the changed files.main(c664a34) it exits 1 with and without--include-context; with this change,--include-contextexits 0 (lookup got: [{"key":"k"},{"tenant":"t-1"}]) and the run without it is unchanged.mainand on this branch, with and without the field, each run passing acontext. All 9 histories replay on both builds.main's open run completed on this branch, and this branch's open runs completed onmainwith the tool output they recorded. A history with the tool Activity renamed fails with[TMPRL1100]on both builds.CI covers the other Node versions and platforms.
Any docs updates needed?
The README paragraph and table row, included here.
Related: #211's PR adds
inputPropertyto the same tool definition and changes the sameinvokecode; with both, the Activity would get(args[inputProperty], context). That PR and #212's PR add tests next to the ones here, so whichever merges second needs a small rebase. The PRs don't depend on each other.Open questions:
parameters. The Python plugin'sactivity_as_toolpasses the context first when the Activity declares it. Is the TypeScript order fine?Draft while these questions are open.