feat(agents): add Toolset support to ToolContext and AgentActivity#1525
Open
toubatbrian wants to merge 5 commits into
Open
feat(agents): add Toolset support to ToolContext and AgentActivity#1525toubatbrian wants to merge 5 commits into
toubatbrian wants to merge 5 commits into
Conversation
🦋 Changeset detectedLatest commit: 3c266ed The changes in this PR will be included in the next version bump. This PR includes changesets to release 31 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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.
Summary
Adds Python-parity
Toolsetsupport: a stateful container that bundles a group of tools behind sharedsetup()/aclose()lifecycle hooks.Toolsetinstances can be passed directly intoAgent({ tools: [...] })alongside individual function tools; their tools are flattened into the agent'sToolContextand the runtime drives setup on activity start, close on activity teardown, and a diff onupdateTools().Closes the placeholder TODOs left in
tool_context.tsfrom the previous list-syntax refactor.Changes
Core (
agents/src/llm/tool_context.ts)Toolsetclass withid,tools,setup(),aclose()(default no-ops, override in subclass).ToolCalledEvent/ToolCompletedEventpayload types (exported for plugin authors).ToolContextEntrywidened toFunctionTool | ProviderDefinedTool | Toolset.updateTools()recurses into Toolsets and flattens their tools into_functionToolsMap/_providerToolswhile tracking the Toolset itself in_toolsets.equals()compares_toolsetsas an identity set (matches Python's{id(ts) for ts in self._tool_sets}semantics — order-insensitive).duplicate function name: <name>, including when the duplicate is contributed by a Toolset (Python parity).Activity (
agents/src/voice/agent_activity.ts)setupToolsets()/closeToolsets()driven by_startSessionand_closeSessionResources.setup()failures propagate to the caller after rolling back any toolsets that already initialized — the agent fails explicitly rather than running with half-set-up resources.updateTools()now diffs added/removed Toolsets and runs lifecycle insetup → swap toolCtx → closeorder so a setup failure leaves the activity pointing at the previous (still-valid)ToolContext.IGNORE_ON_ENTERfilter recurses through Toolsets so nested function tools also honor the flag.Plugin migrations to
ToolContext.flatten()+isFunctionToolfilterToolset-contributed tools are included in
_functionToolsMap, but several plugins were also rebuilding tool lists fromObject.entries(toolCtx.functionTools), which would silently drop theToolsetreferences and confuse subsequentupdateTools()diffs. Migrated:plugins/google/src/utils.tsplugins/mistralai/src/llm.tsplugins/openai/src/realtime/realtime_model.ts— also fixes a real bug in_handleSessionUpdated()where the post-updateretainedToolsrebuild dropped Toolsets, causing them to be diffed-as-removed and prematurelyaclose()d.plugins/openai/src/responses/llm.tsplugins/openai/src/ws/llm.tsplugins/phonic/src/realtime/realtime_model.tsplugins/openai/src/llm.ts(chat) andplugins/baseten/src/llm.tsleft unchanged: they iterateObject.keys(toolCtx.functionTools)which already picks up flattened Toolset tools, mirroring Python's chat plugin.Tests
agents/src/llm/tool_context.test.ts: newToolsetdescribe block (id/tools accessors, default no-op lifecycle, subclass override, flatten-into-ToolContext, duplicate-throw parity, identity-setequals()parity).plugins/test/src/llm.ts: newtoolsetdescribe block runs against every LLM plugin's shared test suite — verifies the model can call a function tool nested inside aToolsetand that direct tools continue to work alongside.agent_activity.test.ts/agent_activity_handoff.test.ts: existing tests pass (verified the toolset wiring doesn't disturb the fake activity scaffolding).Example
examples/src/basic_toolsets.tsshowing alocation_toolsToolset bundlinggetWeather+lookupTimezoneand passed alongside anAgent.Test plan
pnpm test --run agents/src/llm/tool_context.test.ts— 46/46 pass (40 prior + 6 new)pnpm test --run agents/src/voice/agent_activity.test.ts agents/src/voice/agent_activity_handoff.test.ts— 24/24 passpnpm test --run plugins/openai/src/llm.test.ts plugins/google/src/llm.test.ts plugins/mistralai/src/llm.test.ts— 29/30 pass (1 pre-existing skip), including 2 new toolset cases per pluginpnpm build— all 32 packages greenpnpm lint— clean (only pre-existinganywarnings in unrelated files)aclose()runs on session close and thatsetup()rejection rolls back already-set-up toolsets