Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Give under-budget arrays items on the session tool-list share path

Ooi Pin Jundcb1fdc11049
diff --git a/server/tool-list-budget.test.ts b/server/tool-list-budget.test.ts
index edb9d7f344..8ab0b41f49 100644
--- a/server/tool-list-budget.test.ts
+++ b/server/tool-list-budget.test.ts
@@ -153,6 +153,24 @@ describe("fitting a list into its share", () => {
} });
});
+ it("adds items to an under-budget array before advertising it", () => {
+ const tool = {
+ name: "sample",
+ inputSchema: { type: "object", required: ["tags"], properties: {
+ tags: { type: "array" },
+ scores: { type: "array", items: { type: "integer", minimum: 0 } },
+ } },
+ };
+ const fitted = fitToolListToShare([tool], 500);
+ const schema = fitted.tools[0]!.inputSchema;
+ expect(fitted.fits).toBe(true);
+ expect(fitted.level.topLevelOnly).toBeFalsy();
+ expect(schema.properties.tags).toHaveProperty("items", { type: "string" });
+ expect(schema.properties.scores).toEqual(tool.inputSchema.properties.scores);
+ expect(schema.required).toEqual(["tags"]);
+ expect(tool.inputSchema.properties.tags).not.toHaveProperty("items");
+ });
+
it("rebuilds an oversized array with its item type, and leaves a plain string array as it was", () => {
const tool: ListedTool = {
name: "sample", description: "A sample tool.",
diff --git a/server/tool-list-budget.ts b/server/tool-list-budget.ts
index d162c462b9..3406b416cb 100644
--- a/server/tool-list-budget.ts
+++ b/server/tool-list-budget.ts
@@ -95,6 +95,21 @@ function acceptsArray(type: unknown): boolean {
return type === "array" || (Array.isArray(type) && type.includes("array"));
}
+function withArrayItems(node: unknown, root: Json): unknown {
+ if (!isObject(node)) return node;
+ const result = Object.fromEntries(Object.entries(node).map(([key, value]) => {
+ if (SCHEMA_MAPS.has(key) && isObject(value)) {
+ return [key, Object.fromEntries(Object.entries(value).map(([name, child]) => [name, withArrayItems(child, root)]))];
+ }
+ if (SCHEMA_LISTS.has(key) && Array.isArray(value)) {
+ return [key, value.map((child) => withArrayItems(child, root))];
+ }
+ return [key, SCHEMA_SLOTS.has(key) ? withArrayItems(value, root) : value];
+ }));
+ if (acceptsArray(node.type) && !("items" in node)) result.items = itemsOf(node, root);
+ return result;
+}
+
/** A top-level field that holds an object, or a nested value, as just its type and choices. Gemini requires `items` on
* every array: a list of objects keeps `{type:"object"}`, and any other array keeps its item type (a string when that
* type cannot be named). A field that is not nested stays as it was, except an array that would otherwise go out with
@@ -138,7 +153,11 @@ export function withDescriptionLevel<T extends ListedTool>(tools: readonly T[],
/** Each tool's schema as a model is shown it. */
export function withCompactSchemas<T extends ListedTool>(tools: readonly T[]): T[] {
- return tools.map((tool) => (isObject(tool.inputSchema) ? { ...tool, inputSchema: compactAdvertisedSchema(tool.inputSchema) } : tool));
+ return tools.map((tool) => {
+ if (!isObject(tool.inputSchema)) return tool;
+ const schema = compactAdvertisedSchema(tool.inputSchema);
+ return { ...tool, inputSchema: withArrayItems(schema, schema) };
+ });
}
function longest(tools: readonly ListedTool[], of: (tool: ListedTool) => string[]): number {