Skip to content

[typescript/openai-agents] add includeContext to activityAsTool to pass a copy of the run context - #218

Draft
campoy wants to merge 2 commits into
mainfrom
fix/openai-agents-typescript-activity-tools-get-no-run-context
Draft

campoy wants to merge 2 commits into
mainfrom
fix/openai-agents-typescript-activity-tools-get-no-run-context

Conversation

@campoy

@campoy campoy commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

activityAsTool gets an opt-in includeContext field on its tool definition. With includeContext: true, the tool passes runContext.context as the Activity's second argument:

// activities.ts
export async function lookup(input: { key: string }, context: { tenant: string }): Promise<string> {
  // ...
}

// workflow code
const lookupTool = activityAsTool<typeof activities.lookup>({
  name: 'lookup',
  description: 'Look up a key',
  parameters: {
    type: 'object',
    properties: { key: { type: 'string' } },
    required: ['key'],
    additionalProperties: false,
  },
  includeContext: true,
});
  • Without the field, the Activity gets the same single argument as before.
  • ActivityToolDefinition<TActivity> now accepts an Activity with a required second parameter, but then requires includeContext: true. The check is part of the definition type, so it applies both to inline definitions and to definitions annotated as ActivityToolDefinition<typeof activities.x>. Without the field, such a type is still refused, so a two-parameter Activity such as credit(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.
  • README: the Activity-backed tools section shows the field and says that the copy is part of the Activity's input, so it is stored in History, and that changes the Activity makes do not reach the run. The feature table's "Activity tools receive a copy" row names the field.

API change: additive. The field is optional, and every definition and call that compiled before still compiles. ActivityToolDefinition is now a type alias rather than an interface: annotations and extends with a concrete Activity type still work, but declaration merging and a generic interface ... 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 patched gate 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

  1. Closes [Bug] openai-agents (TypeScript): Activity tools get no copy of the run context #213

  2. How was this tested:

    • New test activityAsTool includeContext gives the Activity a copy of the run context in src/__tests__/test-openai-agents.ts, with its Workflow in workflows/openai-agents.ts: a real Worker, a run with context: { tenant: 't-1' }, and a fake model that calls lookup with {"key":"k"}. Its Workflow passes an Activity type with a required second parameter. With tools.ts from main that type fails to compile (TS2344), and the emitted test fails on t.deepEqual(received, [[{ key: 'k' }, { tenant: 't-1' }]]) (the Activity got [{ key: 'k' }]). With this change it compiles and passes.
    • The file's compile-time block checks the types. These are refused (@ts-expect-error): a two-parameter Activity without includeContext, both inline and as an annotated ActivityToolDefinition; 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-error lines gives exactly one error on each refused line.
    • Locally, in Docker (Node v24.22.0, pnpm 10.27.0, Linux arm64): make lint passes (the 8 warnings main has, none in files this PR touches), make test passes (208 tests; main has 207), make build packs, and check_npm_package.py, _shared/smoke.mjs and scripts/ci/check_conventions.py pass. Prettier with the SDK's settings is clean on the changed files.
    • [Bug] openai-agents (TypeScript): Activity tools get no copy of the run context #213's reproduction: on 1.24.0 and main (c664a34) it exits 1 with and without --include-context; with this change, --include-context exits 0 (lookup got: [{"key":"k"},{"tenant":"t-1"}]) and the run without it is unchanged.
    • Replay check with a local dev server (Temporal CLI 1.9.1): finished runs recorded on a Worker with no Workflow cache, and runs held at the model call after the tool, on main and on this branch, with and without the field, each run passing a context. All 9 histories replay on both builds. main's open run completed on this branch, and this branch's open runs completed on main with 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.

  3. Any docs updates needed?
    The README paragraph and table row, included here.

Related: #211's PR adds inputProperty to the same tool definition and changes the same invoke code; 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:

  • Context second, as here, keeps the rule that the Activity's first parameter types parameters. The Python plugin's activity_as_tool passes the context first when the Activity declares it. Is the TypeScript order fine?
  • The copy goes into History as part of the Activity's input. The README now says so; should it also warn against secrets in the context?

Draft while these questions are open.

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.
@campoy campoy changed the title Add includeContext to activityAsTool to pass a copy of the run context [openai-agents-typescript] add includeContext to activityAsTool to pass a copy of the run context Nov 16, 2026
@campoy campoy changed the title [openai-agents-typescript] add includeContext to activityAsTool to pass a copy of the run context [typescript/openai-agents] add includeContext to activityAsTool to pass a copy of the run context Nov 16, 2026

This branch has not been deployed

No deployments
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.

[Bug] openai-agents (TypeScript): Activity tools get no copy of the run context

1 participant