Since #989 every client tool is listed always-loaded, so all of them go into agy's customization budget: 20,000 tokens counted as ceil(UTF-8 bytes / 3). Past it, agy drops whole tools, the largest first, and with tools: [] the model has no other way to reach them. tungloong's 131-tool catalog (~25,000 tokens) lost bash and 12 Paper tools. Proc.listTools() now hands agy the catalog through fitTools (tool-budget.mjs), aiming at 18,000 tokens to leave agy room for its name prefix and serialization: - within the budget, the catalog goes as it is (the same array); - else $schema, title and examples come out where they are keywords (a property named "title" is the client's and stays); - else every description is capped at the highest level L that fits, found by binary search: a property's at L bytes, a tool's own at 2.5L, a contract property's (an object with no properties, or tool-schema's Variants/Accepts anyOf text) at 11L. A cut drops a trailing Examples block first, ends on a sentence or a whole code point, and adds "…"; - at L = 0 the properties' descriptions go and the room left goes to the tools' own. Names, property names, types, enum, const, required, items, anyOf and bounds are never touched, and this.tools / toolsSignature stay as the client sent them. The trimmed list is kept per catalog. Run (WSL magpie-desktop, temp HOME; mote was at load 178 and mica's disk is full): bun test 21 pass; node --test conversation-key, step-usage, usage-settlement, repeat-batch, compat, tool-schema, catalog, tool-budget, steer-in-session, windows-nc: 198 pass, 0 fail; bun scripts/check.mjs agy-cli ok; npm pack --dry-run ships tool-budget.mjs. With main's index.mjs the new bridge test fails (31,690 tokens reach agy). Not tried with a real agy: the budget and its counting are the reporter's reading of agy 1.5.8, not checked here.
140 lines
6.0 KiB
JavaScript
140 lines
6.0 KiB
JavaScript
// agy holds every MCP tool it loads into one customization budget of 20,000
|
||
// tokens, counted as ceil(UTF-8 bytes / 3). Over it, agy drops whole tools,
|
||
// the largest first, so the best-documented ones (bash, a canvas's write
|
||
// tool) vanish from the model's function list, and with `tools: []` there is
|
||
// no other way to reach them (plugins#1140). fitTools trims the tools' prose
|
||
// instead, so every tool and every bit of its schema stays: names, property
|
||
// names, types, enums, required, items, anyOf and the bounds all go as the
|
||
// client wrote them. Only descriptions are shortened, and only as far as the
|
||
// budget needs.
|
||
|
||
// What the listed tools may take of agy's 20,000: the rest is room for the
|
||
// server prefix agy adds to each name and its own serialization.
|
||
export const TOOL_TOKEN_BUDGET = 18000
|
||
|
||
const enc = new TextEncoder()
|
||
const bytes = (s) => enc.encode(s).length
|
||
export const agyTokens = (s) => Math.ceil(bytes(s) / 3)
|
||
|
||
const tokensOf = (tools) => tools.reduce((n, tool) => n + agyTokens(JSON.stringify(tool)), 0)
|
||
|
||
// Keywords that say nothing about which arguments are valid. They are taken
|
||
// out only where they are keywords: a property named "title" is the
|
||
// client's, and stays.
|
||
const DECOR = new Set(["$schema", "title", "examples"])
|
||
|
||
// A property whose description is its contract: an object with no
|
||
// properties of its own, or one whose description carries the branches
|
||
// tool-schema.mjs folded out of an anyOf/oneOf. Its text is the only place
|
||
// the model learns what goes in it, so it keeps more of it.
|
||
const CONTRACT_WEIGHT = 11
|
||
const TOOL_WEIGHT = 2.5
|
||
|
||
function isContract(node) {
|
||
if (!node || typeof node !== "object") return false
|
||
const d = typeof node.description === "string" ? node.description : ""
|
||
if (/\b(Variants|Accepts anyOf):/.test(d)) return true
|
||
const types = Array.isArray(node.type) ? node.type : [node.type]
|
||
return types.includes("object") && !(node.properties && Object.keys(node.properties).length)
|
||
}
|
||
|
||
// cut shortens text to at most max UTF-8 bytes. It drops a trailing
|
||
// Examples block first, then ends on a sentence boundary where one is near,
|
||
// else on a whole code point, and marks the cut with "…". null when nothing
|
||
// worth keeping fits.
|
||
export function cut(text, max) {
|
||
if (bytes(text) <= max) return text
|
||
const bare = text.replace(/\s*\bExamples?:[\s\S]*$/i, "").trimEnd()
|
||
if (bare && bytes(bare) <= max) return bare
|
||
const room = max - bytes("…")
|
||
if (room < 4) return null
|
||
let out = ""
|
||
let used = 0
|
||
for (const ch of bare || text) {
|
||
const b = bytes(ch)
|
||
if (used + b > room) break
|
||
out += ch
|
||
used += b
|
||
}
|
||
const stop = Math.max(...[". ", "; ", "。", ";"].map((p) => out.lastIndexOf(p)))
|
||
if (stop > out.length / 2) out = out.slice(0, stop + 1)
|
||
return out.trimEnd() + "…"
|
||
}
|
||
|
||
// walk copies one schema node with every description capped: cap(node)
|
||
// gives the byte cap for a node's description, and Infinity leaves it.
|
||
// strip takes the decorative keywords out too.
|
||
function walk(node, cap, strip) {
|
||
if (Array.isArray(node)) return node.map((item) => walk(item, cap, strip))
|
||
if (!node || typeof node !== "object") return node
|
||
const out = {}
|
||
for (const [key, value] of Object.entries(node)) {
|
||
if (strip && DECOR.has(key)) continue
|
||
if (key === "description" && typeof value === "string") {
|
||
const max = cap(node)
|
||
const text = max === Infinity ? value : cut(value, max)
|
||
if (text) out.description = text
|
||
continue
|
||
}
|
||
if ((key === "properties" || key === "$defs" || key === "definitions" || key === "patternProperties") && value && typeof value === "object" && !Array.isArray(value)) {
|
||
// a map of names to schemas: the names are the client's, never keywords
|
||
out[key] = Object.fromEntries(Object.entries(value).map(([name, schema]) => [name, walk(schema, cap, strip)]))
|
||
continue
|
||
}
|
||
if (key === "enum" || key === "const" || key === "default" || key === "required") {
|
||
out[key] = value
|
||
continue
|
||
}
|
||
out[key] = walk(value, cap, strip)
|
||
}
|
||
return out
|
||
}
|
||
|
||
function shaped(tools, { strip, L, top }) {
|
||
const prop = L === undefined ? () => Infinity : (node) => (isContract(node) ? CONTRACT_WEIGHT * L : L)
|
||
return tools.map((tool) => {
|
||
const out = { ...tool }
|
||
if (tool.inputSchema && typeof tool.inputSchema === "object") out.inputSchema = walk(tool.inputSchema, prop, strip)
|
||
if (typeof tool.description === "string") {
|
||
const max = top ?? (L === undefined ? Infinity : TOOL_WEIGHT * L)
|
||
const text = max === Infinity ? tool.description : cut(tool.description, max)
|
||
if (text) out.description = text
|
||
else delete out.description
|
||
}
|
||
return out
|
||
})
|
||
}
|
||
|
||
// highest finds the largest whole n in [lo, hi] for which fits(n) holds,
|
||
// given that it holds at lo; fits is monotone in n.
|
||
function highest(lo, hi, fits) {
|
||
while (lo < hi) {
|
||
const mid = Math.ceil((lo + hi) / 2)
|
||
if (fits(mid)) lo = mid
|
||
else hi = mid - 1
|
||
}
|
||
return lo
|
||
}
|
||
|
||
// fitTools gives the tools as agy should list them: unchanged when they fit
|
||
// the budget, else with the decorative keywords gone, then with every
|
||
// description capped at the highest level that fits (a tool's own
|
||
// description gets 2.5 times a property's, a contract property's 11 times),
|
||
// and when that level is nothing, with the properties' descriptions gone and
|
||
// the tools' own capped alone. A set too large even with no descriptions at
|
||
// all goes as small as it can, and agy drops what is left over.
|
||
export function fitTools(tools, budget = TOOL_TOKEN_BUDGET) {
|
||
if (tokensOf(tools) <= budget) return tools
|
||
const stripped = shaped(tools, { strip: true })
|
||
if (tokensOf(stripped) <= budget) return stripped
|
||
const at = (L) => shaped(tools, { strip: true, L })
|
||
const fits = (L) => tokensOf(at(L)) <= budget
|
||
if (!fits(0)) return at(0)
|
||
const L = highest(0, 4000, fits)
|
||
if (L > 0) return at(L)
|
||
// the properties' descriptions are gone; what room is left goes to the
|
||
// tools' own, which say what each tool is for
|
||
const bare = (top) => shaped(tools, { strip: true, L: 0, top })
|
||
return bare(highest(0, 4000, (top) => tokensOf(bare(top)) <= budget))
|
||
}
|