fix(codemode): suggest tools when a namespace is called (#63697)

This commit is contained in:
Aiden Cline
2026-11-13 00:46:05 -06:00
committed by GitHub
Unverified
parent d0cf80c764
commit 27930d26c8
2 changed files with 33 additions and 2 deletions
+18 -1
View File
@@ -545,7 +545,24 @@ const resolve = <R>(root: ToolNode<R>, path: ReadonlyArray<string>, index: Reado
)
}
if (node.tool === undefined) {
throw new ToolRuntimeError("UnknownTool", `Tool '${segments.join(".")}' is not callable.`)
const name = segments.join(".")
const under = index.filter((entry) => entry.description.path.startsWith(`${name}.`))
const ranked = rank(under, segments.at(-1) ?? "", index)
const shown = [...ranked, ...under.filter((entry) => !ranked.includes(entry))]
.slice(0, 3)
.map((entry) => toolExpression(entry.description.path))
const more = under.length - shown.length
throw new ToolRuntimeError(
"UnknownTool",
shown.length === 0
? `Tool '${name}' is not callable.`
: `Tool '${name}' is not callable, it is a namespace. Did you mean ${new Intl.ListFormat("en", { type: "disjunction" }).format(shown)}?`,
[
more > 0
? `${more} more under '${name}'. Use search({ namespace: ${JSON.stringify(name)} }) to list them.`
: "Use search to find available tools.",
],
)
}
return node.tool
}
+15 -1
View File
@@ -118,7 +118,21 @@ describe("callable namespaces", () => {
const nested = CodeMode.make({ tools: { "issues.list": echo("List issues", "list") } })
const diagnostic = await failure(nested, `return await tools.issues({})`)
expect(diagnostic.kind).toBe("UnknownTool")
expect(diagnostic.message).toContain("Tool 'issues' is not callable")
expect(diagnostic.message).toBe("Tool 'issues' is not callable, it is a namespace. Did you mean tools.issues.list?")
expect(diagnostic.suggestions).toEqual(["Use search to find available tools."])
})
test("calling a namespace names a few of its tools and counts the rest", async () => {
const nested = CodeMode.make({
tools: Object.fromEntries(
["archive", "create", "get", "issues", "list"].map((name) => [`issues.${name}`, echo(`Issue ${name}`, name)]),
),
})
const diagnostic = await failure(nested, `return await tools.issues({})`)
expect(diagnostic.message).toBe(
"Tool 'issues' is not callable, it is a namespace. Did you mean tools.issues.issues, tools.issues.archive, or tools.issues.create?",
)
expect(diagnostic.suggestions).toEqual([`2 more under 'issues'. Use search({ namespace: "issues" }) to list them.`])
})
})