Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/types/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,38 @@ function sameToolIdentity(
return left.namespace === right.namespace && left.name === right.name;
}

type ToolIdentity = Pick<OcxTool, "namespace" | "name">;

const toolChoiceCandidateIndexes = new WeakMap<
readonly ToolIdentity[],
ReadonlyMap<string, readonly ToolIdentity[]>
>();

function toolChoiceCandidateIndex(
tools: readonly ToolIdentity[],
): ReadonlyMap<string, readonly ToolIdentity[]> {
const cached = toolChoiceCandidateIndexes.get(tools);
if (cached) return cached;

const index = new Map<string, ToolIdentity[]>();
const identities = new Map<string, Set<string>>();
for (const tool of tools) {
const identity = JSON.stringify([tool.namespace ?? null, tool.name]);
for (const selector of [...toolChoiceAliases(tool), tool.name]) {
const candidates = index.get(selector);
if (!candidates) {
index.set(selector, [tool]);
identities.set(selector, new Set([identity]));
} else if (!identities.get(selector)!.has(identity)) {
candidates.push(tool);
identities.get(selector)!.add(identity);
}
}
}
toolChoiceCandidateIndexes.set(tools, index);
return index;
}

/**
* All tools that could be selected by one client-facing name. Bare logical names are included
* here because they are a compatibility selector for namespaced tools, while wire and dotted
Expand All @@ -53,12 +85,7 @@ export function toolChoiceCandidates(
name: string,
): Pick<OcxTool, "namespace" | "name">[] {
if (!tools) return [];
const candidates: Pick<OcxTool, "namespace" | "name">[] = [];
for (const tool of tools) {
if (tool.name !== name && !toolChoiceAliases(tool).includes(name)) continue;
if (!candidates.some(candidate => sameToolIdentity(candidate, tool))) candidates.push(tool);
}
return candidates;
return [...(toolChoiceCandidateIndex(tools).get(name) ?? [])];
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/tool-choice-performance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect, test } from "bun:test";
import type { OcxTool } from "../src/types";
import { toolChoiceToolPredicate } from "../src/types";

test("allowed_tools resolves an ambiguous bare name with one catalog pass", () => {
const size = 256;
const backingTools: OcxTool[] = Array.from({ length: size }, (_, index) => ({
namespace: `namespace_${index}`,
name: "shared_name",
description: "",
parameters: {},
}));
let catalogReads = 0;
const tools = new Proxy(backingTools, {
get(target, property, receiver) {
if (typeof property === "string" && /^\d+$/.test(property)) catalogReads += 1;
return Reflect.get(target, property, receiver);
},
});

const allowed = toolChoiceToolPredicate(
{ allowedTools: ["shared_name"], mode: "auto" },
tools,
);

expect(tools.filter(allowed)).toEqual([]);
expect(catalogReads).toBeLessThanOrEqual(size * 2);
});
Loading