diff --git a/packages/codemode/src/tool-runtime.ts b/packages/codemode/src/tool-runtime.ts index 8910a44c6b8..fc6925576e5 100644 --- a/packages/codemode/src/tool-runtime.ts +++ b/packages/codemode/src/tool-runtime.ts @@ -545,7 +545,24 @@ const resolve = (root: ToolNode, path: ReadonlyArray, 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 } diff --git a/packages/codemode/test/tool-paths.test.ts b/packages/codemode/test/tool-paths.test.ts index a739d651510..33529514659 100644 --- a/packages/codemode/test/tool-paths.test.ts +++ b/packages/codemode/test/tool-paths.test.ts @@ -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.`]) }) })