@magpie-community/opencode-agy-tui-auth 0.1.4 → 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 +8 -4
- package/README.md +8 -4
- package/array-items.mjs +209 -0
- package/index.mjs +365 -70
- package/package.json +7 -2
- package/re2-pattern.mjs +606 -0
- package/tool-budget.mjs +157 -0
- package/tool-schema.mjs +672 -0
- package/tools.mjs +204 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-agy-tui-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Magpie/OpenCode model provider that runs Antigravity's models through agy's own interactive mode: each request is typed into the real agy TUI and its answer streamed from agy's own conversation store as the model writes it",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./opencode.mjs",
|
|
@@ -14,6 +14,11 @@
|
|
|
14
14
|
"default-agent.mjs",
|
|
15
15
|
"agy-version.mjs",
|
|
16
16
|
"tool-gate.mjs",
|
|
17
|
+
"tools.mjs",
|
|
18
|
+
"tool-schema.mjs",
|
|
19
|
+
"re2-pattern.mjs",
|
|
20
|
+
"array-items.mjs",
|
|
21
|
+
"tool-budget.mjs",
|
|
17
22
|
"opencode.mjs",
|
|
18
23
|
"opencode2.mjs",
|
|
19
24
|
"README.md",
|
|
@@ -39,7 +44,7 @@
|
|
|
39
44
|
"test": "bun test"
|
|
40
45
|
},
|
|
41
46
|
"engines": {
|
|
42
|
-
"magpie": ">=0.1.
|
|
47
|
+
"magpie": ">=0.1.5217"
|
|
43
48
|
},
|
|
44
49
|
"magpie": {
|
|
45
50
|
"maxConcurrency": 4
|
package/re2-pattern.mjs
ADDED
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
// A copy of packages/agy-cli/re2-pattern.mjs (each package is published alone): a
|
|
2
|
+
// change to one goes to the other.
|
|
3
|
+
// RE2-safe JSON Schema `pattern` / `patternProperties` handling for agy.
|
|
4
|
+
//
|
|
5
|
+
// agy validates every tool parameter schema against the JSON Schema 2020-12
|
|
6
|
+
// metaschema using Go's `regexp` library (RE2). JSON Schema defines `pattern`
|
|
7
|
+
// as an ECMA-262 regular expression, so constructs that are legal in JavaScript
|
|
8
|
+
// — `\uXXXX` / `\u{...}` escapes, lookaheads, backreferences — make agy fail
|
|
9
|
+
// "building toolbox" and abort the whole turn before any model call.
|
|
10
|
+
//
|
|
11
|
+
// We translate the Unicode escapes RE2 can express (`\uXXXX` → `\x{XXXX}`),
|
|
12
|
+
// join UTF-16 surrogate pairs into one code point (including mixed escaped
|
|
13
|
+
// and raw pairs), then drop anything still not compilable as RE2 — or whose
|
|
14
|
+
// RE2 meaning is not the ECMA-262 meaning, such as `\z`, `\S`, `\Q…\E`,
|
|
15
|
+
// `\a`, and `\A` — into a description hint. Common character classes
|
|
16
|
+
// (`\s`, `\d`, `\w`) are kept even though RE2 is ASCII-only for these;
|
|
17
|
+
// dropping `\s` would disable many ordinary tool schemas. The rewrite is
|
|
18
|
+
// idempotent. Tool-call arguments are plain JSON values, not patterns, so
|
|
19
|
+
// the response path needs no reverse transform.
|
|
20
|
+
//
|
|
21
|
+
// See: https://github.com/google-antigravity/antigravity-cli/issues/1327
|
|
22
|
+
|
|
23
|
+
const HINT_PREFIX = "pattern: "
|
|
24
|
+
|
|
25
|
+
export function appendHint(node, hint) {
|
|
26
|
+
if (!hint || !node || typeof node !== "object" || Array.isArray(node)) return
|
|
27
|
+
if (typeof node.description === "string" && node.description.trim()) {
|
|
28
|
+
if (!node.description.includes(hint)) node.description = `${node.description} (${hint})`
|
|
29
|
+
} else {
|
|
30
|
+
node.description = hint
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const HIGH_SURROGATE_START = 0xD800
|
|
35
|
+
const HIGH_SURROGATE_END = 0xDBFF
|
|
36
|
+
const LOW_SURROGATE_START = 0xDC00
|
|
37
|
+
const LOW_SURROGATE_END = 0xDFFF
|
|
38
|
+
const UNICODE_MAX = 0x10FFFF
|
|
39
|
+
|
|
40
|
+
function isHighSurrogate(cp) {
|
|
41
|
+
return cp >= HIGH_SURROGATE_START && cp <= HIGH_SURROGATE_END
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isLowSurrogate(cp) {
|
|
45
|
+
return cp >= LOW_SURROGATE_START && cp <= LOW_SURROGATE_END
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function joinSurrogates(high, low) {
|
|
49
|
+
return 0x10000 + ((high - HIGH_SURROGATE_START) << 10) + (low - LOW_SURROGATE_START)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isHex(char) {
|
|
53
|
+
return /[0-9a-fA-F]/.test(char)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function parseUnicodeEscape(source, idx) {
|
|
57
|
+
if (source[idx] !== "\\") return null
|
|
58
|
+
const rest = source.slice(idx + 1)
|
|
59
|
+
let match = /^u\{([0-9a-fA-F]+)\}/.exec(rest)
|
|
60
|
+
if (match) return { end: idx + 1 + match[0].length, cp: parseInt(match[1], 16), hex: match[1] }
|
|
61
|
+
match = /^u([0-9a-fA-F]{4})/.exec(rest)
|
|
62
|
+
if (match) return { end: idx + 1 + match[0].length, cp: parseInt(match[1], 16), hex: match[1] }
|
|
63
|
+
match = /^U([0-9a-fA-F]{8})/.exec(rest)
|
|
64
|
+
if (match) return { end: idx + 1 + match[0].length, cp: parseInt(match[1], 16), hex: match[1] }
|
|
65
|
+
match = /^x\{([0-9a-fA-F]+)\}/.exec(rest)
|
|
66
|
+
if (match) return { end: idx + 1 + match[0].length, cp: parseInt(match[1], 16), hex: match[1] }
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function emitHex(cp, hex = null) {
|
|
71
|
+
if (hex != null && Number.parseInt(hex, 16) === cp) return `\\x{${hex}}`
|
|
72
|
+
const digits = cp.toString(16).toUpperCase()
|
|
73
|
+
return `\\x{${cp <= 0xFFFF ? digits.padStart(4, "0") : digits}}`
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Escape-aware translation of ECMA-262 Unicode escapes into RE2 `\x{...}`.
|
|
77
|
+
// Adjacent UTF-16 surrogate escapes (and raw surrogate pairs in the string)
|
|
78
|
+
// are joined into one code point so `😀` stays a single character in RE2.
|
|
79
|
+
// `\\u0000` (a literal backslash then the letters u0000) is left alone.
|
|
80
|
+
export function translateUnicodeEscapes(source) {
|
|
81
|
+
if (typeof source !== "string") return source
|
|
82
|
+
let out = ""
|
|
83
|
+
let i = 0
|
|
84
|
+
const length = source.length
|
|
85
|
+
while (i < length) {
|
|
86
|
+
const parsed = parseUnicodeEscape(source, i)
|
|
87
|
+
if (parsed) {
|
|
88
|
+
const next = parseUnicodeEscape(source, parsed.end)
|
|
89
|
+
if (Number.isFinite(parsed.cp) && isHighSurrogate(parsed.cp) && next && Number.isFinite(next.cp) && isLowSurrogate(next.cp)) {
|
|
90
|
+
out += emitHex(joinSurrogates(parsed.cp, next.cp))
|
|
91
|
+
i = next.end
|
|
92
|
+
continue
|
|
93
|
+
}
|
|
94
|
+
// Mixed: escaped high surrogate + raw low surrogate code unit.
|
|
95
|
+
if (Number.isFinite(parsed.cp) && isHighSurrogate(parsed.cp) && parsed.end < length) {
|
|
96
|
+
const rawLow = source.charCodeAt(parsed.end)
|
|
97
|
+
if (isLowSurrogate(rawLow)) {
|
|
98
|
+
out += emitHex(joinSurrogates(parsed.cp, rawLow))
|
|
99
|
+
i = parsed.end + 1
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
out += emitHex(parsed.cp, parsed.hex)
|
|
104
|
+
i = parsed.end
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
if (source[i] === "\\") {
|
|
108
|
+
out += source[i++]
|
|
109
|
+
if (i < length) out += source[i++]
|
|
110
|
+
continue
|
|
111
|
+
}
|
|
112
|
+
const code = source.charCodeAt(i)
|
|
113
|
+
if (isHighSurrogate(code) && i + 1 < length) {
|
|
114
|
+
const next = parseUnicodeEscape(source, i + 1)
|
|
115
|
+
if (next && Number.isFinite(next.cp) && isLowSurrogate(next.cp)) {
|
|
116
|
+
out += emitHex(joinSurrogates(code, next.cp))
|
|
117
|
+
i = next.end
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
const low = source.charCodeAt(i + 1)
|
|
121
|
+
if (isLowSurrogate(low)) {
|
|
122
|
+
out += emitHex(joinSurrogates(code, low))
|
|
123
|
+
i += 2
|
|
124
|
+
continue
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
out += source[i++]
|
|
128
|
+
}
|
|
129
|
+
return out
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Letters accepted after a backslash outside a character class, when the
|
|
133
|
+
// RE2 meaning matches ECMA-262 closely enough to keep. `\z` and `\S`
|
|
134
|
+
// compile in RE2 but differ from JS (yetone called these out). `\a` is
|
|
135
|
+
// BEL in RE2 and a literal `a` in non-unicode JS; `\A` is start-of-text
|
|
136
|
+
// in RE2 and a literal `A` in JS; `\Q…\E` is quoting in RE2 and not
|
|
137
|
+
// an ECMA-262 construct. All of those are dropped into a hint.
|
|
138
|
+
// `\s` / `\d` / `\w` are kept (RE2 is ASCII-only for these).
|
|
139
|
+
const RE2_LETTER_ESCAPES = new Set([
|
|
140
|
+
"f", "n", "r", "t", "v",
|
|
141
|
+
"b", "B", "d", "D", "s", "w", "W",
|
|
142
|
+
])
|
|
143
|
+
|
|
144
|
+
// `\b` / `\B` / `\A` / `\z` / `\Q` / `\E` / `\a` are not kept inside a class.
|
|
145
|
+
const RE2_CLASS_LETTER_ESCAPES = new Set([
|
|
146
|
+
"f", "n", "r", "t", "v",
|
|
147
|
+
"d", "D", "s", "w", "W",
|
|
148
|
+
])
|
|
149
|
+
|
|
150
|
+
const SINGLE_ESCAPE_CP = {
|
|
151
|
+
f: 0x0c,
|
|
152
|
+
n: 0x0a,
|
|
153
|
+
r: 0x0d,
|
|
154
|
+
t: 0x09,
|
|
155
|
+
v: 0x0b,
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const RE2_GROUP_FLAGS = new Set(["i", "m", "s", "U"])
|
|
159
|
+
|
|
160
|
+
// Names Go's `regexp` accepts in `\p{...}` / `\P{...}` (case-insensitive).
|
|
161
|
+
// Includes general categories, scripts, and the aliases Go documents (Any,
|
|
162
|
+
// ASCII, Assigned, Letter, …). `Script=Greek` and other key=value forms are
|
|
163
|
+
// not in this set and are rejected. Generated against Go 1.27's `regexp`.
|
|
164
|
+
const RE2_UNICODE_PROPERTIES = new Set([
|
|
165
|
+
"adlam", "ahom", "anatolian_hieroglyphs", "any", "arabic", "armenian", "ascii", "assigned",
|
|
166
|
+
"avestan", "balinese", "bamum", "bassa_vah", "batak", "bengali", "beria_erfe", "bhaiksuki",
|
|
167
|
+
"bopomofo", "brahmi", "braille", "buginese", "buhid", "c", "canadian_aboriginal", "carian",
|
|
168
|
+
"cased_letter", "caucasian_albanian", "cc", "cf", "chakma", "cham", "cherokee", "chorasmian",
|
|
169
|
+
"close_punctuation", "cn", "cntrl", "co", "common", "connector_punctuation", "control", "coptic",
|
|
170
|
+
"cs", "cuneiform", "currency_symbol", "cypriot", "cypro_minoan", "cyrillic", "dash_punctuation", "decimal_number",
|
|
171
|
+
"deseret", "devanagari", "digit", "dives_akuru", "dogra", "duployan", "egyptian_hieroglyphs", "elbasan",
|
|
172
|
+
"elymaic", "enclosing_mark", "ethiopic", "final_punctuation", "format", "garay", "georgian", "glagolitic",
|
|
173
|
+
"gothic", "grantha", "greek", "gujarati", "gunjala_gondi", "gurmukhi", "gurung_khema", "han",
|
|
174
|
+
"hangul", "hanifi_rohingya", "hanunoo", "hatran", "hebrew", "hiragana", "imperial_aramaic", "inherited",
|
|
175
|
+
"initial_punctuation", "inscriptional_pahlavi", "inscriptional_parthian", "javanese", "kaithi", "kannada", "katakana", "kawi",
|
|
176
|
+
"kayah_li", "kharoshthi", "khitan_small_script", "khmer", "khojki", "khudawadi", "kirat_rai", "l",
|
|
177
|
+
"lao", "latin", "lc", "lepcha", "letter", "letter_number", "limbu", "line_separator",
|
|
178
|
+
"linear_a", "linear_b", "lisu", "ll", "lm", "lo", "lowercase_letter", "lt",
|
|
179
|
+
"lu", "lycian", "lydian", "m", "mahajani", "makasar", "malayalam", "mandaic",
|
|
180
|
+
"manichaean", "marchen", "mark", "masaram_gondi", "math_symbol", "mc", "me", "medefaidrin",
|
|
181
|
+
"meetei_mayek", "mende_kikakui", "meroitic_cursive", "meroitic_hieroglyphs", "miao", "mn", "modi", "modifier_letter",
|
|
182
|
+
"modifier_symbol", "mongolian", "mro", "multani", "myanmar", "n", "nabataean", "nag_mundari",
|
|
183
|
+
"nandinagari", "nd", "new_tai_lue", "newa", "nko", "nl", "no", "nonspacing_mark",
|
|
184
|
+
"number", "nushu", "nyiakeng_puachue_hmong", "ogham", "ol_chiki", "ol_onal", "old_hungarian", "old_italic",
|
|
185
|
+
"old_north_arabian", "old_permic", "old_persian", "old_sogdian", "old_south_arabian", "old_turkic", "old_uyghur", "open_punctuation",
|
|
186
|
+
"oriya", "osage", "osmanya", "other", "other_letter", "other_number", "other_punctuation", "other_symbol",
|
|
187
|
+
"p", "pahawh_hmong", "palmyrene", "paragraph_separator", "pau_cin_hau", "pc", "pd", "pe",
|
|
188
|
+
"pf", "phags_pa", "phoenician", "pi", "po", "private_use", "ps", "psalter_pahlavi",
|
|
189
|
+
"punct", "punctuation", "rejang", "runic", "s", "samaritan", "saurashtra", "sc",
|
|
190
|
+
"separator", "sharada", "shavian", "siddham", "sidetic", "signwriting", "sinhala", "sk",
|
|
191
|
+
"sm", "so", "sogdian", "sora_sompeng", "soyombo", "space_separator", "spacing_mark", "sundanese",
|
|
192
|
+
"sunuwar", "surrogate", "syloti_nagri", "symbol", "syriac", "tagalog", "tagbanwa", "tai_le",
|
|
193
|
+
"tai_tham", "tai_viet", "tai_yo", "takri", "tamil", "tangsa", "tangut", "telugu",
|
|
194
|
+
"thaana", "thai", "tibetan", "tifinagh", "tirhuta", "titlecase_letter", "todhri", "tolong_siki",
|
|
195
|
+
"toto", "tulu_tigalari", "ugaritic", "unassigned", "uppercase_letter", "vai", "vithkuqi", "wancho",
|
|
196
|
+
"warang_citi", "yezidi", "yi", "z", "zanabazar_square", "zl", "zp", "zs",
|
|
197
|
+
])
|
|
198
|
+
|
|
199
|
+
function isKnownUnicodeProperty(name) {
|
|
200
|
+
if (typeof name !== "string" || !name) return false
|
|
201
|
+
const body = name[0] === "^" ? name.slice(1) : name
|
|
202
|
+
return body.length > 0 && RE2_UNICODE_PROPERTIES.has(body.toLowerCase())
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function isCodePointInRange(cp) {
|
|
206
|
+
return Number.isInteger(cp) && cp >= 0 && cp <= UNICODE_MAX
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Conservative RE2 compiler check. Returns true only when the pattern should
|
|
210
|
+
// compile with Go's `regexp.Compile` (the same engine agy uses) *and* keep
|
|
211
|
+
// its JavaScript meaning. Anything we cannot positively accept is rejected,
|
|
212
|
+
// which drops the pattern into a hint rather than risking a toolbox failure.
|
|
213
|
+
export function isRE2Safe(source) {
|
|
214
|
+
if (typeof source !== "string") return false
|
|
215
|
+
const length = source.length
|
|
216
|
+
let i = 0
|
|
217
|
+
let depth = 0
|
|
218
|
+
let canQuantify = false
|
|
219
|
+
|
|
220
|
+
// Repeat-product tracking, matching Go's repeatIsValid(..., 1000):
|
|
221
|
+
// the product of nested `{n}` / `{n,m}` / `{n,}` maxima (min if unbounded)
|
|
222
|
+
// along any one path must not exceed 1000. Concat and alternation take
|
|
223
|
+
// the max of their children, not the product. `*`, `+`, `?`, `{0,}`, and
|
|
224
|
+
// `{1,}` are not OpRepeat product-checked in Go.
|
|
225
|
+
const groupStack = [{ alt: 1, best: 1, last: 1 }]
|
|
226
|
+
const ctx = () => groupStack[groupStack.length - 1]
|
|
227
|
+
const foldLast = () => {
|
|
228
|
+
const c = ctx()
|
|
229
|
+
c.alt = Math.max(c.alt, c.last)
|
|
230
|
+
}
|
|
231
|
+
const noteAtom = (mass = 1) => {
|
|
232
|
+
foldLast()
|
|
233
|
+
ctx().last = mass
|
|
234
|
+
canQuantify = true
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const parseEscape = (inCharClass) => {
|
|
238
|
+
// Called at `\`. Returns null on error, or {set} / {cp}.
|
|
239
|
+
if (i >= length || source[i] !== "\\") return null
|
|
240
|
+
i++
|
|
241
|
+
if (i >= length) return null
|
|
242
|
+
const next = source[i++]
|
|
243
|
+
if (next >= "1" && next <= "9") return null
|
|
244
|
+
if (next === "0") {
|
|
245
|
+
let extra = 0
|
|
246
|
+
let oct = 0
|
|
247
|
+
while (extra < 2 && i < length && source[i] >= "0" && source[i] <= "7") {
|
|
248
|
+
oct = (oct << 3) + (source[i++] - "0")
|
|
249
|
+
extra++
|
|
250
|
+
}
|
|
251
|
+
return { cp: oct }
|
|
252
|
+
}
|
|
253
|
+
if (next === "x") {
|
|
254
|
+
if (i < length && source[i] === "{") {
|
|
255
|
+
i++
|
|
256
|
+
let hex = ""
|
|
257
|
+
while (i < length && isHex(source[i])) hex += source[i++]
|
|
258
|
+
if (!hex || i >= length || source[i] !== "}") return null
|
|
259
|
+
i++
|
|
260
|
+
const cp = Number.parseInt(hex, 16)
|
|
261
|
+
if (!isCodePointInRange(cp)) return null
|
|
262
|
+
return { cp }
|
|
263
|
+
}
|
|
264
|
+
if (i + 1 < length && isHex(source[i]) && isHex(source[i + 1])) {
|
|
265
|
+
const cp = Number.parseInt(source.slice(i, i + 2), 16)
|
|
266
|
+
i += 2
|
|
267
|
+
return { cp }
|
|
268
|
+
}
|
|
269
|
+
return null
|
|
270
|
+
}
|
|
271
|
+
if (next === "p" || next === "P") {
|
|
272
|
+
if (i < length && source[i] === "{") {
|
|
273
|
+
i++
|
|
274
|
+
let name = ""
|
|
275
|
+
while (i < length && source[i] !== "}") {
|
|
276
|
+
const char = source[i]
|
|
277
|
+
if (!/[A-Za-z0-9_^]/.test(char)) return null
|
|
278
|
+
name += char
|
|
279
|
+
i++
|
|
280
|
+
}
|
|
281
|
+
if (!name || i >= length || source[i] !== "}") return null
|
|
282
|
+
i++
|
|
283
|
+
if (!isKnownUnicodeProperty(name)) return null
|
|
284
|
+
return { set: true }
|
|
285
|
+
}
|
|
286
|
+
if (i < length && /[A-Za-z]/.test(source[i])) {
|
|
287
|
+
const name = source[i++]
|
|
288
|
+
if (!isKnownUnicodeProperty(name)) return null
|
|
289
|
+
return { set: true }
|
|
290
|
+
}
|
|
291
|
+
return null
|
|
292
|
+
}
|
|
293
|
+
// `\Q…\E` is RE2 quoting, not ECMA-262. Drop it.
|
|
294
|
+
if (next === "Q" || next === "E") return null
|
|
295
|
+
if (next === "u" || next === "U" || next === "C" || next === "k" || next === "Z" || next === "G") return null
|
|
296
|
+
// `\z` and `\S` compile in RE2 but differ from JS; drop them.
|
|
297
|
+
// `\a` / `\A` also differ from JS (BEL / start-of-text vs literal letters).
|
|
298
|
+
if (next === "z" || next === "S" || next === "a" || next === "A") return null
|
|
299
|
+
if (/[A-Za-z]/.test(next)) {
|
|
300
|
+
const allowed = (inCharClass ? RE2_CLASS_LETTER_ESCAPES : RE2_LETTER_ESCAPES).has(next)
|
|
301
|
+
if (!allowed) return null
|
|
302
|
+
if (next === "d" || next === "D" || next === "s" || next === "w" || next === "W") return { set: true }
|
|
303
|
+
if (next === "b" || next === "B") return { set: true }
|
|
304
|
+
if (Object.hasOwn(SINGLE_ESCAPE_CP, next)) return { cp: SINGLE_ESCAPE_CP[next] }
|
|
305
|
+
return { set: true }
|
|
306
|
+
}
|
|
307
|
+
// Unknown letter-like or non-ASCII escapes (`\é`) are invalid in RE2.
|
|
308
|
+
// Punctuation escapes (`\.`, `\ `, `\-`, …) are accepted as that character.
|
|
309
|
+
if (/^[A-Za-z]$/.test(next) === false && next.charCodeAt(0) >= 0x80) return null
|
|
310
|
+
return { cp: next.charCodeAt(0) }
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const parseClassItem = () => {
|
|
314
|
+
if (i >= length) return null
|
|
315
|
+
if (source[i] === "\\") return parseEscape(true)
|
|
316
|
+
let cp = source.charCodeAt(i++)
|
|
317
|
+
if (isHighSurrogate(cp) && i < length && isLowSurrogate(source.charCodeAt(i))) {
|
|
318
|
+
cp = joinSurrogates(cp, source.charCodeAt(i++))
|
|
319
|
+
}
|
|
320
|
+
return { cp }
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const consumeClass = () => {
|
|
324
|
+
// Called at `[`. Rejects inverted ranges (`[z-a]`) the way Go does
|
|
325
|
+
// (`hi < lo` is ErrInvalidCharRange).
|
|
326
|
+
i++
|
|
327
|
+
if (i < length && source[i] === "^") i++
|
|
328
|
+
let justOpened = true
|
|
329
|
+
while (i < length) {
|
|
330
|
+
if (source[i] === "]" && !justOpened) {
|
|
331
|
+
i++
|
|
332
|
+
return true
|
|
333
|
+
}
|
|
334
|
+
// Go treats `[:` inside a class as a POSIX named class (`[[:alpha:]]`).
|
|
335
|
+
// JS does not, so keepers would either fail to compile in RE2 or
|
|
336
|
+
// match a different set. Reject the pattern and drop it to a hint.
|
|
337
|
+
if (source[i] === "[" && i + 1 < length && source[i + 1] === ":") return false
|
|
338
|
+
const lo = parseClassItem()
|
|
339
|
+
if (!lo) return false
|
|
340
|
+
justOpened = false
|
|
341
|
+
// `[a-]` : the '-' before ']' is literal, not a range.
|
|
342
|
+
if (lo.cp != null && i < length && source[i] === "-" && i + 1 < length && source[i + 1] !== "]") {
|
|
343
|
+
i++
|
|
344
|
+
const hi = parseClassItem()
|
|
345
|
+
if (!hi) return false
|
|
346
|
+
if (hi.cp == null) return false
|
|
347
|
+
if (hi.cp < lo.cp) return false
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return false
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const applyRepeatProduct = (min, maxStr) => {
|
|
354
|
+
const hasMax = maxStr != null && maxStr !== ""
|
|
355
|
+
const max = hasMax ? Number(maxStr) : null
|
|
356
|
+
// Go only product-checks OpRepeat when min >= 2 or max >= 2.
|
|
357
|
+
// `{0,}`, `{1,}`, `{1}` and `{0,1}` are not product-checked.
|
|
358
|
+
const productChecked = min >= 2 || (hasMax && max >= 2)
|
|
359
|
+
if (!productChecked) return true
|
|
360
|
+
const m = hasMax ? max : min
|
|
361
|
+
const last = ctx().last || 1
|
|
362
|
+
if (m > 0 && last * m > 1000) return false
|
|
363
|
+
ctx().last = last * m
|
|
364
|
+
return true
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const consumeRepeat = () => {
|
|
368
|
+
// Called at `{`. A well-formed bound is `{n}`, `{n,}` or `{n,m}`.
|
|
369
|
+
// RE2 rejects a leading or nested well-formed bound, a bound above 1000,
|
|
370
|
+
// min > max, and a nested product of maxima above 1000 (e.g. `(a{1000}){2}`).
|
|
371
|
+
// A `{` that is not a well-formed bound is a literal.
|
|
372
|
+
const start = i
|
|
373
|
+
i++
|
|
374
|
+
if (i >= length || source[i] < "0" || source[i] > "9") {
|
|
375
|
+
i = start + 1
|
|
376
|
+
noteAtom(1)
|
|
377
|
+
return true
|
|
378
|
+
}
|
|
379
|
+
let minStr = ""
|
|
380
|
+
while (i < length && source[i] >= "0" && source[i] <= "9") minStr += source[i++]
|
|
381
|
+
let maxStr = null
|
|
382
|
+
if (i < length && source[i] === ",") {
|
|
383
|
+
i++
|
|
384
|
+
maxStr = ""
|
|
385
|
+
while (i < length && source[i] >= "0" && source[i] <= "9") maxStr += source[i++]
|
|
386
|
+
}
|
|
387
|
+
if (i >= length || source[i] !== "}") {
|
|
388
|
+
i = start + 1
|
|
389
|
+
noteAtom(1)
|
|
390
|
+
return true
|
|
391
|
+
}
|
|
392
|
+
if (!canQuantify) return false
|
|
393
|
+
const min = Number(minStr)
|
|
394
|
+
if (min > 1000) return false
|
|
395
|
+
if (maxStr != null && maxStr !== "") {
|
|
396
|
+
const max = Number(maxStr)
|
|
397
|
+
if (max > 1000 || min > max) return false
|
|
398
|
+
}
|
|
399
|
+
i++
|
|
400
|
+
if (i < length && source[i] === "?") i++
|
|
401
|
+
else if (i < length && source[i] === "+") return false
|
|
402
|
+
if (!applyRepeatProduct(min, maxStr)) return false
|
|
403
|
+
canQuantify = false
|
|
404
|
+
return true
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const consumeNamedGroup = () => {
|
|
408
|
+
// Called after `(?<` or `(?P<`.
|
|
409
|
+
let name = 0
|
|
410
|
+
while (i < length && source[i] !== ">") {
|
|
411
|
+
if (!/[A-Za-z0-9_]/.test(source[i])) return false
|
|
412
|
+
i++
|
|
413
|
+
name++
|
|
414
|
+
}
|
|
415
|
+
if (name === 0 || i >= length || source[i] !== ">") return false
|
|
416
|
+
i++
|
|
417
|
+
depth++
|
|
418
|
+
foldLast()
|
|
419
|
+
groupStack.push({ alt: 1, best: 1, last: 1 })
|
|
420
|
+
canQuantify = false
|
|
421
|
+
return true
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const consumeGroup = () => {
|
|
425
|
+
// Called at `(`.
|
|
426
|
+
i++
|
|
427
|
+
if (i < length && source[i] === "?") {
|
|
428
|
+
i++
|
|
429
|
+
if (i >= length) return false
|
|
430
|
+
const next = source[i]
|
|
431
|
+
if (next === ":") {
|
|
432
|
+
i++
|
|
433
|
+
depth++
|
|
434
|
+
foldLast()
|
|
435
|
+
groupStack.push({ alt: 1, best: 1, last: 1 })
|
|
436
|
+
canQuantify = false
|
|
437
|
+
return true
|
|
438
|
+
}
|
|
439
|
+
if (next === "P") {
|
|
440
|
+
i++
|
|
441
|
+
if (i >= length || source[i] !== "<") return false
|
|
442
|
+
i++
|
|
443
|
+
return consumeNamedGroup()
|
|
444
|
+
}
|
|
445
|
+
if (next === "<") {
|
|
446
|
+
// Go 1.26+ accepts `(?<name>…)` as well as `(?P<name>…)`.
|
|
447
|
+
// Lookbehinds `(?<=` / `(?<!` are still rejected.
|
|
448
|
+
i++
|
|
449
|
+
if (i < length && (source[i] === "=" || source[i] === "!")) return false
|
|
450
|
+
return consumeNamedGroup()
|
|
451
|
+
}
|
|
452
|
+
// Flags: (?imsU) or (?imsU-imsU) or (?imsU:...) — not lookaheads.
|
|
453
|
+
let flags = 0
|
|
454
|
+
while (i < length && RE2_GROUP_FLAGS.has(source[i])) {
|
|
455
|
+
i++
|
|
456
|
+
flags++
|
|
457
|
+
}
|
|
458
|
+
if (i < length && source[i] === "-") {
|
|
459
|
+
i++
|
|
460
|
+
while (i < length && RE2_GROUP_FLAGS.has(source[i])) {
|
|
461
|
+
i++
|
|
462
|
+
flags++
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (i < length && source[i] === ":") {
|
|
466
|
+
if (flags === 0) return false
|
|
467
|
+
i++
|
|
468
|
+
depth++
|
|
469
|
+
foldLast()
|
|
470
|
+
groupStack.push({ alt: 1, best: 1, last: 1 })
|
|
471
|
+
canQuantify = false
|
|
472
|
+
return true
|
|
473
|
+
}
|
|
474
|
+
if (i < length && source[i] === ")") {
|
|
475
|
+
if (flags === 0) return false
|
|
476
|
+
i++
|
|
477
|
+
canQuantify = false
|
|
478
|
+
return true
|
|
479
|
+
}
|
|
480
|
+
// `(?=`, `(?!`, `(?>`, `(?(` …
|
|
481
|
+
return false
|
|
482
|
+
}
|
|
483
|
+
depth++
|
|
484
|
+
foldLast()
|
|
485
|
+
groupStack.push({ alt: 1, best: 1, last: 1 })
|
|
486
|
+
canQuantify = false
|
|
487
|
+
return true
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
while (i < length) {
|
|
491
|
+
const char = source[i]
|
|
492
|
+
if (char === "\\") {
|
|
493
|
+
if (!parseEscape(false)) return false
|
|
494
|
+
noteAtom(1)
|
|
495
|
+
continue
|
|
496
|
+
}
|
|
497
|
+
if (char === "[") {
|
|
498
|
+
if (!consumeClass()) return false
|
|
499
|
+
noteAtom(1)
|
|
500
|
+
continue
|
|
501
|
+
}
|
|
502
|
+
if (char === "]") return false
|
|
503
|
+
if (char === "(") {
|
|
504
|
+
if (!consumeGroup()) return false
|
|
505
|
+
continue
|
|
506
|
+
}
|
|
507
|
+
if (char === ")") {
|
|
508
|
+
if (depth <= 0 || groupStack.length <= 1) return false
|
|
509
|
+
depth--
|
|
510
|
+
const inner = groupStack.pop()
|
|
511
|
+
ctx().last = Math.max(inner.best, inner.alt, inner.last)
|
|
512
|
+
canQuantify = true
|
|
513
|
+
i++
|
|
514
|
+
continue
|
|
515
|
+
}
|
|
516
|
+
if (char === "*" || char === "+" || char === "?") {
|
|
517
|
+
if (!canQuantify) return false
|
|
518
|
+
i++
|
|
519
|
+
if (i < length && source[i] === "?") i++
|
|
520
|
+
else if (i < length && source[i] === "+") return false
|
|
521
|
+
canQuantify = false
|
|
522
|
+
continue
|
|
523
|
+
}
|
|
524
|
+
if (char === "{") {
|
|
525
|
+
if (!consumeRepeat()) return false
|
|
526
|
+
continue
|
|
527
|
+
}
|
|
528
|
+
if (char === "|") {
|
|
529
|
+
const c = ctx()
|
|
530
|
+
c.best = Math.max(c.best, c.alt, c.last)
|
|
531
|
+
c.alt = 1
|
|
532
|
+
c.last = 1
|
|
533
|
+
canQuantify = false
|
|
534
|
+
i++
|
|
535
|
+
continue
|
|
536
|
+
}
|
|
537
|
+
if (char === "^" || char === "$") {
|
|
538
|
+
canQuantify = false
|
|
539
|
+
i++
|
|
540
|
+
continue
|
|
541
|
+
}
|
|
542
|
+
// Ordinary literal, including UTF-16 surrogate pairs as one atom.
|
|
543
|
+
if (isHighSurrogate(source.charCodeAt(i)) && i + 1 < length && isLowSurrogate(source.charCodeAt(i + 1))) {
|
|
544
|
+
i += 2
|
|
545
|
+
} else {
|
|
546
|
+
i++
|
|
547
|
+
}
|
|
548
|
+
noteAtom(1)
|
|
549
|
+
}
|
|
550
|
+
return depth === 0 && groupStack.length === 1
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
export function sanitizePatternString(original) {
|
|
554
|
+
if (typeof original !== "string") {
|
|
555
|
+
return { ok: false, pattern: undefined, original }
|
|
556
|
+
}
|
|
557
|
+
const translated = translateUnicodeEscapes(original)
|
|
558
|
+
if (isRE2Safe(translated)) {
|
|
559
|
+
return { ok: true, pattern: translated, original }
|
|
560
|
+
}
|
|
561
|
+
return { ok: false, pattern: undefined, original }
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Mutates a single schema node: `pattern` and `patternProperties` keys.
|
|
565
|
+
// Recursion into child schemas is the caller's job (sanitizeInputSchema).
|
|
566
|
+
export function sanitizePatternFields(node) {
|
|
567
|
+
if (!node || typeof node !== "object" || Array.isArray(node)) return node
|
|
568
|
+
|
|
569
|
+
if (Object.hasOwn(node, "pattern")) {
|
|
570
|
+
const result = sanitizePatternString(node.pattern)
|
|
571
|
+
if (result.ok) {
|
|
572
|
+
node.pattern = result.pattern
|
|
573
|
+
} else {
|
|
574
|
+
const shown = typeof result.original === "string" ? result.original : JSON.stringify(result.original)
|
|
575
|
+
appendHint(node, `${HINT_PREFIX}${shown}`)
|
|
576
|
+
delete node.pattern
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
if (node.patternProperties && typeof node.patternProperties === "object" && !Array.isArray(node.patternProperties)) {
|
|
581
|
+
const next = {}
|
|
582
|
+
const dropped = []
|
|
583
|
+
for (const [key, value] of Object.entries(node.patternProperties)) {
|
|
584
|
+
const result = sanitizePatternString(key)
|
|
585
|
+
if (result.ok) next[result.pattern] = value
|
|
586
|
+
else dropped.push(key)
|
|
587
|
+
}
|
|
588
|
+
if (dropped.length) {
|
|
589
|
+
appendHint(node, `dropped patternProperties: ${dropped.join(", ")}`)
|
|
590
|
+
}
|
|
591
|
+
if (Object.keys(next).length) {
|
|
592
|
+
node.patternProperties = next
|
|
593
|
+
} else {
|
|
594
|
+
delete node.patternProperties
|
|
595
|
+
// If every key was dropped and additionalProperties is false, the
|
|
596
|
+
// object would now reject every property. Lift that so the schema
|
|
597
|
+
// stays usable; the hint already records what was dropped.
|
|
598
|
+
if (node.additionalProperties === false) {
|
|
599
|
+
delete node.additionalProperties
|
|
600
|
+
appendHint(node, "additionalProperties:false was lifted because every patternProperties key was dropped")
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
return node
|
|
606
|
+
}
|