Repository navigation
refactor(ai): one canonical tool path, mapped to wire names per request - #65739
rekram1-node wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
The fix works. On the base I reproduced the OpenAI Chat failure: crm.lookup is sent as is, and the model's crm_lookup call fails with No tool named "crm_lookup". With this PR, test/tool-names.test.ts covers that flow and passes. The full packages/ai suite passes (2300 tests), the core aisdk tests pass, and tsc passes in packages/ai and packages/core. Core's request path doesn't change: core already checks tool names against the same character set before building definitions.
Public API (@opencode/ai), not backwards compatible for callers that rely on dotted names or flattened names:
ToolDefinition/ToolDefinition.make,ToolNamespace.makeandtool(...)now throw unless the name matches^[a-zA-Z0-9_-]{1,128}$. Before, dotted names were accepted and very long names were not rejected; callers must group tools withnamespace(...)/ToolNamespaceinstead.- Removed
ProviderShared.flattenRequestandProviderShared.flattenToolRequest. AddedToolNames.lower(from@opencode/ai/route). - Added
namespace(...), theToolGrouptype,ToolCallPart.pathandRoute.namespaces.ai.loopandtoolsaccept groups. Message.tools({ remove })takes paths now, andtoolChoicetakes a path or atool(...).- Tool events on non-Responses protocols now report
{ namespace, name }instead ofcrm_lookup. Protocol.makeno longer flattens, so a protocol'sbody.fromcalled directly gets unflattened requests.
One concern, inline: removing a namespace's last tool by path leaves an empty namespace, and OpenAI Responses still sends it.
| path.startsWith(`${tool.name}.`) ? [path.slice(tool.name.length + 1)] : [], | ||
| ) | ||
| if (inner.length === 0 || tool.type !== "namespace" || tool.tools === undefined) return [tool] | ||
| return [{ ...tool, tools: removePaths(tool.tools, inner) }] |
There was a problem hiding this comment.
When the paths remove every member of a namespace, this keeps the namespace with tools: []. I tried a namespace crm holding only lookup, then Message.tools({ remove: ["crm.lookup"] }). On OpenAI Responses the compiled body sends { type: "namespace", name: "crm", tools: [] }, while flat protocols just drop it. I couldn't check live whether Responses accepts an empty namespace, but it looks like a likely 400. Consider dropping a namespace that is left empty, the same as removing crm. That also makes withdrawnTools and the remove list match what's sent.
There was a problem hiding this comment.
The empty-namespace point is fixed: removing a namespace's last tool now drops the namespace, and the new test covers it on OpenAI Responses. Dropping the 128-character limit matches core, which also checks only the characters in tool names. In packages/ai, bun test and typecheck:tsc pass. The public API impact is the same as in my earlier review, except that tool names no longer have a length limit (letters, digits, _ or - only). No new concerns.
Issue for this PR
No issue. Follows #65292 and #65726.
Type of change
What does this PR do?
A tool in a group (
crm→lookup) used to have three unconnected names. Requests flattened it tocrm_lookupon every protocol except OpenAI Responses, nothing mapped the model'scrm_lookupcall back, and a runtimetool({ name: "crm.lookup" })was sent literally, which OpenAI and Anthropic reject with a 400. On OpenAI Chat a grouped tool never ran:tools: [{ type: "namespace", name: "crm", tools: [lookup] }] // runtime tool({ name: "crm.lookup" }) model calls "crm_lookup" -tool result: No tool named "crm_lookup" is currently available +tool runs; call and result are { namespace: "crm", name: "lookup" }Now public inputs and outputs only use canonical tool paths, and provider names exist only inside one compiled request:
Protocol.makeno longer wrapsbody.fromwithflattenRequest, so the xAI compaction special case is gone and OpenAI trigger compaction (whose internal protocol didn't forwardnamespaces) now sends the same names as its steps.OpenAI Responses rejects a forced
tool_choicefor a tool inside a namespace (checked live:crm.lookup,lookup,namespacefield andallowed_toolsall 400), so a request that forces a grouped tool sends flat names for that one request. The table decodes them back, so callers still seecrm.lookup.Breaking changes (
@opencode/ai) and migrationNo issue. Follows #65292 and #65726.
Type of change
What does this PR do?
A tool in a group (
crm→lookup) used to have three unconnected names. Requests flattened it tocrm_lookupon every protocol except OpenAI Responses, nothing mapped the model'scrm_lookupcall back, and a runtimetool({ name: "crm.lookup" })was sent literally, which OpenAI and Anthropic reject with a 400. On OpenAI Chat a grouped tool never ran:tools: [{ type: "namespace", name: "crm", tools: [lookup] }] // runtime tool({ name: "crm.lookup" }) model calls "crm_lookup" -tool result: No tool named "crm_lookup" is currently available +tool runs; call and result are { namespace: "crm", name: "lookup" }Now public inputs and outputs only use canonical tool paths, and provider names exist only inside one compiled request:
Protocol.makeno longer wrapsbody.fromwithflattenRequest, so the xAI compaction special case is gone and OpenAI trigger compaction (whose internal protocol didn't forwardnamespaces) now sends the same names as its steps.OpenAI Responses rejects a forced
tool_choicefor a tool inside a namespace (checked live:crm.lookup,lookup,namespacefield andallowed_toolsall 400), so a request that forces a grouped tool sends flat names for that one request. The table decodes them back, so callers still seecrm.lookup.Breaking changes (
@opencode/ai) and migrationnamespace(...)(runtime tools, dispatched byai.loop) orToolNamespace(definitions).toolChoicetakes a path ("crm.lookup") or the tool/definition; an object matching two grouped tools fails and asks for the path.Protocol.namespacesis now read by the request compiler rather thanProtocol.make. A protocol'sbody.fromcalled directly no longer flattens; go through the client orToolNames.lowerfirst.OpenCode core
Core flattens MCP/plugin tools itself and never declares groups, so its requests don't change.
aisdk.tscallsToolNames.lowerin place offlattenRequest. I compiled one request with flat tools (a duplicate name, a tool update with a removal, a call to a withdrawn tool, with and without a forced choice) on OpenAI Chat, OpenAI Responses, Anthropic, Bedrock, Gemini and xAI on dev and on this branch: the 12 bodies are byte-identical.One wire difference: on OpenAI Responses, a replayed call to an undeclared dotted name (a Code Mode path like
a.b.c) now goes out asa_b_c, as it already did on every other protocol. Responses accepts both (checked live), so it costs at most one prompt-cache miss for such histories.How did you verify your code works?
packages/ai:bun typecheck;bun test2300 pass, 0 fail. Newtest/tool-names.test.ts(13 tests): round trip on Chat and Responses, nestedcrm.deals.geton both, collision error, Responses ↔ Chat replay, undeclareda.b.con Chat and Bedrock, oldcrm_lookuphistories,toolChoiceby path/object/ambiguous,Message.toolsremoval by path, and/responses/compact(OpenAI, xAI) and trigger compaction using the step names. Run against dev, the 9 new-behavior tests fail; the 4 that guard kept behavior (undeclared names, compaction names) pass there too.packages/core:bun typecheck; aisdk, provider, model-resolver, session-runner*, plugin, tool*, codemode, generate and session tests: 2316 pass, 2 fail. The 2 (ShellTool conditional process substitution) fail the same way on dev.bun run checkat the root passes.ai.loopand acrmgroup holdinglookupand a nesteddeals.get, prompted and forced (toolChoice: "crm.lookup"): OpenAI Chatgpt-4.1-mini, OpenAI Responsesgpt-5.4-mini, Anthropicclaude-haiku-4-5. All 6 runs calledcrm.lookupandcrm.deals.get, ran the tools and finished withstopafter the follow-up request.Review carefully:
packages/ai/src/tool-names.ts(the table and encode/decode) and the path-awarefoldTools/planToolUpdatesinpackages/ai/src/tool-updates.ts.Checklist
Added by the opencode bot (AI-generated).
Public API: @opencode/ai: tool/namespace names restricted to one segment (dotted names now throw), removes ProviderShared.flattenRequest/flattenToolRequest, adds ToolNames.lower, namespace(), ToolCallPart.path; tool events use canonical { namespace, name } (breaking)