@magpie-community/opencode-agy-tui-auth 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +9 -5
- package/README.md +9 -5
- package/array-items.mjs +209 -0
- package/index.mjs +411 -54
- package/live.mjs +143 -0
- package/package.json +9 -3
- package/re2-pattern.mjs +606 -0
- package/tool-budget.mjs +157 -0
- package/tool-schema.mjs +672 -0
- package/tools.mjs +204 -0
package/tool-budget.mjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// A copy of packages/agy-cli/tool-budget.mjs (each package is published alone): a
|
|
2
|
+
// change to one goes to the other.
|
|
3
|
+
// agy holds every MCP tool it loads into one customization budget of 20,000
|
|
4
|
+
// tokens, counted as ceil(UTF-8 bytes / 3). Over it, agy drops whole tools,
|
|
5
|
+
// the largest first, so the best-documented ones (bash, a canvas's write
|
|
6
|
+
// tool) vanish from the model's function list, and with `tools: []` there is
|
|
7
|
+
// no other way to reach them (plugins#1140). fitTools trims the tools' prose
|
|
8
|
+
// instead, so every tool and every bit of its schema stays: names, property
|
|
9
|
+
// names, types, enums, required, items, anyOf and the bounds all go as the
|
|
10
|
+
// client wrote them. Only descriptions are shortened, and only as far as the
|
|
11
|
+
// budget needs.
|
|
12
|
+
|
|
13
|
+
// What the listed tools may take of agy's 20,000: the rest is room for the
|
|
14
|
+
// server prefix agy adds to each name and its own serialization.
|
|
15
|
+
export const TOOL_TOKEN_BUDGET = 18000
|
|
16
|
+
|
|
17
|
+
const enc = new TextEncoder()
|
|
18
|
+
const bytes = (s) => enc.encode(s).length
|
|
19
|
+
export const agyTokens = (s) => Math.ceil(bytes(s) / 3)
|
|
20
|
+
|
|
21
|
+
const tokensOf = (tools) => tools.reduce((n, tool) => n + agyTokens(JSON.stringify(tool)), 0)
|
|
22
|
+
|
|
23
|
+
// Keywords that say nothing about which arguments are valid. They are taken
|
|
24
|
+
// out only where they are keywords: a property named "title" is the
|
|
25
|
+
// client's, and stays.
|
|
26
|
+
const DECOR = new Set(["$schema", "title", "examples"])
|
|
27
|
+
|
|
28
|
+
// A property whose description is its contract: an object with no
|
|
29
|
+
// properties of its own, or one whose description carries the branches
|
|
30
|
+
// tool-schema.mjs folded out of an anyOf/oneOf. Its text is the only place
|
|
31
|
+
// the model learns what goes in it, so it keeps more of it.
|
|
32
|
+
const CONTRACT_WEIGHT = 11
|
|
33
|
+
const TOOL_WEIGHT = 2.5
|
|
34
|
+
|
|
35
|
+
function isContract(node) {
|
|
36
|
+
if (!node || typeof node !== "object") return false
|
|
37
|
+
const d = typeof node.description === "string" ? node.description : ""
|
|
38
|
+
if (/\b(Variants|Accepts anyOf):/.test(d)) return true
|
|
39
|
+
const types = Array.isArray(node.type) ? node.type : [node.type]
|
|
40
|
+
return types.includes("object") && !(node.properties && Object.keys(node.properties).length)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// cut shortens text to at most max UTF-8 bytes. It drops a trailing
|
|
44
|
+
// Examples block first, then ends on a sentence boundary where one is near,
|
|
45
|
+
// else on a whole code point, and marks the cut with "…". null when nothing
|
|
46
|
+
// worth keeping fits.
|
|
47
|
+
export function cut(text, max) {
|
|
48
|
+
if (bytes(text) <= max) return text
|
|
49
|
+
const bare = text.replace(/\s*\bExamples?:[\s\S]*$/i, "").trimEnd()
|
|
50
|
+
if (bare && bytes(bare) <= max) return bare
|
|
51
|
+
const room = max - bytes("…")
|
|
52
|
+
if (room < 4) return null
|
|
53
|
+
let out = ""
|
|
54
|
+
let used = 0
|
|
55
|
+
for (const ch of bare || text) {
|
|
56
|
+
const b = bytes(ch)
|
|
57
|
+
if (used + b > room) break
|
|
58
|
+
out += ch
|
|
59
|
+
used += b
|
|
60
|
+
}
|
|
61
|
+
const stop = Math.max(...[". ", "; ", "。", ";"].map((p) => out.lastIndexOf(p)))
|
|
62
|
+
if (stop > out.length / 2) out = out.slice(0, stop + 1)
|
|
63
|
+
return out.trimEnd() + "…"
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// A cut text shorter than this says nothing ("The fil…") and only spends
|
|
67
|
+
// the budget on its key: such a description goes whole or not at all
|
|
68
|
+
// (plugins#1140).
|
|
69
|
+
export const MIN_TEXT = 24
|
|
70
|
+
|
|
71
|
+
// trim is text within max bytes: whole when it fits, else cut, else null
|
|
72
|
+
// when what a cut would leave is under MIN_TEXT
|
|
73
|
+
function trim(text, max) {
|
|
74
|
+
if (bytes(text) <= max) return text
|
|
75
|
+
return max < MIN_TEXT ? null : cut(text, max)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// walk copies one schema node with every description capped: cap(node)
|
|
79
|
+
// gives the byte cap for a node's description, and Infinity leaves it.
|
|
80
|
+
// strip takes the decorative keywords out too.
|
|
81
|
+
function walk(node, cap, strip) {
|
|
82
|
+
if (Array.isArray(node)) return node.map((item) => walk(item, cap, strip))
|
|
83
|
+
if (!node || typeof node !== "object") return node
|
|
84
|
+
const out = {}
|
|
85
|
+
for (const [key, value] of Object.entries(node)) {
|
|
86
|
+
if (strip && DECOR.has(key)) continue
|
|
87
|
+
if (key === "description" && typeof value === "string") {
|
|
88
|
+
const max = cap(node)
|
|
89
|
+
const text = max === Infinity ? value : trim(value, max)
|
|
90
|
+
if (text) out.description = text
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
if ((key === "properties" || key === "$defs" || key === "definitions" || key === "patternProperties") && value && typeof value === "object" && !Array.isArray(value)) {
|
|
94
|
+
// a map of names to schemas: the names are the client's, never keywords
|
|
95
|
+
out[key] = Object.fromEntries(Object.entries(value).map(([name, schema]) => [name, walk(schema, cap, strip)]))
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
if (key === "enum" || key === "const" || key === "default" || key === "required") {
|
|
99
|
+
out[key] = value
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
out[key] = walk(value, cap, strip)
|
|
103
|
+
}
|
|
104
|
+
return out
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function shaped(tools, { strip, L, lean }) {
|
|
108
|
+
// lean: an ordinary property keeps only a description short enough to
|
|
109
|
+
// go whole, and the room goes to the tools' own and the contracts'
|
|
110
|
+
const plain = lean ? MIN_TEXT - 1 : L
|
|
111
|
+
const prop = L === undefined ? () => Infinity : (node) => (isContract(node) ? CONTRACT_WEIGHT * L : plain)
|
|
112
|
+
return tools.map((tool) => {
|
|
113
|
+
const out = { ...tool }
|
|
114
|
+
if (tool.inputSchema && typeof tool.inputSchema === "object") out.inputSchema = walk(tool.inputSchema, prop, strip)
|
|
115
|
+
if (typeof tool.description === "string") {
|
|
116
|
+
const max = L === undefined ? Infinity : TOOL_WEIGHT * L
|
|
117
|
+
const text = max === Infinity ? tool.description : trim(tool.description, max)
|
|
118
|
+
if (text) out.description = text
|
|
119
|
+
else delete out.description
|
|
120
|
+
}
|
|
121
|
+
return out
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// highest finds the largest whole n in [lo, hi] for which fits(n) holds,
|
|
126
|
+
// given that it holds at lo; fits is monotone in n.
|
|
127
|
+
function highest(lo, hi, fits) {
|
|
128
|
+
while (lo < hi) {
|
|
129
|
+
const mid = Math.ceil((lo + hi) / 2)
|
|
130
|
+
if (fits(mid)) lo = mid
|
|
131
|
+
else hi = mid - 1
|
|
132
|
+
}
|
|
133
|
+
return lo
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// fitTools gives the tools as agy should list them: unchanged when they fit
|
|
137
|
+
// the budget, else with the decorative keywords gone, then with every
|
|
138
|
+
// description capped at the highest level that fits (a tool's own
|
|
139
|
+
// description gets 2.5 times a property's, a contract property's 11 times).
|
|
140
|
+
// When that level leaves an ordinary property less than MIN_TEXT, those
|
|
141
|
+
// properties keep only what goes whole, and the highest level that fits
|
|
142
|
+
// then goes to the tools' own descriptions and the contracts', which say
|
|
143
|
+
// what each tool is for and what goes in an open object. A set too large
|
|
144
|
+
// even with no descriptions at all goes as small as it can, and agy drops
|
|
145
|
+
// what is left over.
|
|
146
|
+
export function fitTools(tools, budget = TOOL_TOKEN_BUDGET) {
|
|
147
|
+
if (tokensOf(tools) <= budget) return tools
|
|
148
|
+
const stripped = shaped(tools, { strip: true })
|
|
149
|
+
if (tokensOf(stripped) <= budget) return stripped
|
|
150
|
+
const at = (L) => shaped(tools, { strip: true, L })
|
|
151
|
+
if (tokensOf(at(0)) > budget) return at(0)
|
|
152
|
+
const L = highest(0, 4000, (n) => tokensOf(at(n)) <= budget)
|
|
153
|
+
if (L >= MIN_TEXT) return at(L)
|
|
154
|
+
const lean = (n) => shaped(tools, { strip: true, L: n, lean: true })
|
|
155
|
+
if (tokensOf(lean(0)) > budget) return at(L)
|
|
156
|
+
return lean(highest(0, 4000, (n) => tokensOf(lean(n)) <= budget))
|
|
157
|
+
}
|