@m6d/cortex-cli 0.14.0 → 0.16.0

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.
@@ -1,47 +0,0 @@
1
- /*
2
- * CommonMark fence tracking shared by the prompt rich-text pipeline, the
3
- * widget's streamed-message segmenter, and Cortex's report stand-ins.
4
- */
5
-
6
- /** A fence delimiter of three or more backticks or tildes, indented at most three spaces, and the rest of its line. */
7
- const FENCE_LINE = /^ {0,3}(`{3,}|~{3,})(.*)$/;
8
-
9
- /**
10
- * Line-by-line fence state, following CommonMark's fence rules: a fence line
11
- * is indented at most three spaces; an opening backtick fence's info string
12
- * may not contain backticks; and a fence closes only on the same marker
13
- * character, with at least the opening run's length, followed by nothing but
14
- * whitespace — so a `~~~` line, a shorter same-marker run, or a ```` ```js ````
15
- * info line inside a backtick fence all stay code content.
16
- */
17
- export function fenceTracker() {
18
- let fence: { marker: string; length: number } | null = null;
19
- return (line: string) => {
20
- const match = FENCE_LINE.exec(line);
21
- const delimiter = match?.[1];
22
- const rest = match?.[2] ?? "";
23
- if (delimiter) {
24
- const marker = delimiter.charAt(0);
25
- if (fence) {
26
- if (
27
- marker === fence.marker &&
28
- delimiter.length >= fence.length &&
29
- rest.trim() === ""
30
- ) {
31
- fence = null;
32
- return "close";
33
- }
34
- return "inside";
35
- }
36
- if (marker === "`" && rest.includes("`")) return "outside";
37
- fence = { marker, length: delimiter.length };
38
- return "open";
39
- }
40
- return fence ? "inside" : "outside";
41
- };
42
- }
43
-
44
- /** An opening fence line's language: the info string's first word, "" when there is none. */
45
- export function fenceLanguage(line: string) {
46
- return FENCE_LINE.exec(line)?.[2]?.trim().split(/\s+/)[0] ?? "";
47
- }
@@ -1,276 +0,0 @@
1
- // cspell:ignore fefe fff
2
- import { RE2JS } from "re2js";
3
-
4
- /*
5
- * A tool input schema's `pattern`: a regular expression JavaScript reads with
6
- * no flags, which Cortex matches on RE2 so a value is checked in time linear
7
- * in its length. A pattern RE2 cannot match as JavaScript reads it is refused
8
- * by Cortex and left out of what the TypeScript SDK exports.
9
- *
10
- * RE2 matches code points where JavaScript matches UTF-16 units, so the
11
- * pattern and every input lift each surrogate unit into a private-use plane,
12
- * where it is one code point: RE2 then reads the units JavaScript reads.
13
- * Nothing collides there, since a real character in that plane is astral,
14
- * arrives as two surrogate units and is lifted in turn.
15
- */
16
-
17
- /**
18
- * What one escape or character in a class stands for, spelled as the inside
19
- * of an RE2 class: a single character carries its UTF-16 unit, and an
20
- * unescaped `-` is marked as one that may make a range.
21
- */
22
- type Atom = { body: string; char?: number; dash?: boolean };
23
-
24
- /** JavaScript's `\s`, its WhiteSpace and LineTerminator characters, as the inside of an RE2 class. */
25
- const SPACE =
26
- "\\x{9}-\\x{d}\\x{20}\\x{a0}\\x{1680}\\x{2000}-\\x{200a}\\x{2028}\\x{2029}\\x{202f}\\x{205f}\\x{3000}\\x{feff}";
27
- /** Every character `SPACE` leaves out: JavaScript's `\S`. */
28
- const NON_SPACE =
29
- "\\x{0}-\\x{8}\\x{e}-\\x{1f}\\x{21}-\\x{9f}\\x{a1}-\\x{167f}\\x{1681}-\\x{1fff}\\x{200b}-\\x{2027}" +
30
- "\\x{202a}-\\x{202e}\\x{2030}-\\x{205e}\\x{2060}-\\x{2fff}\\x{3001}-\\x{fefe}\\x{ff00}-\\x{10ffff}";
31
- /** The set escapes as JavaScript reads them; RE2's own `\d` and `\w` match, its `\s` holds only ASCII. */
32
- const SET_ESCAPES = new Map([
33
- ["d", "\\d"],
34
- ["D", "\\D"],
35
- ["w", "\\w"],
36
- ["W", "\\W"],
37
- ["s", SPACE],
38
- ["S", NON_SPACE],
39
- ]);
40
- /** JavaScript's `.`: every character but a line terminator, where RE2's leaves out only `\n`. */
41
- const DOT = "[^\\x{a}\\x{d}\\x{2028}\\x{2029}]";
42
- const CONTROL_ESCAPES = new Map([
43
- ["f", 0x0c],
44
- ["n", 0x0a],
45
- ["r", 0x0d],
46
- ["t", 0x09],
47
- ["v", 0x0b],
48
- ]);
49
- const QUANTIFIER = /\{\d+(?:,\d*)?\}/y;
50
- const SURROGATE = /[\uD800-\uDFFF]/g;
51
- /** Where surrogate unit `0xd800 + n` is lifted: code point `0xf0000 + n`. */
52
- const LIFTED_SURROGATES = 0xf0000;
53
- const HEX = /[0-9A-Fa-f]+/y;
54
- /** What JavaScript's `[^]` and `[]` hold: every character, and none. */
55
- const ANY = "[\\x{0}-\\x{10ffff}]";
56
- const NONE = "[^\\x{0}-\\x{10ffff}]";
57
-
58
- /** A thrown error: the pattern holds what RE2 cannot match as JavaScript reads it. */
59
- function unsupported(what: string): never {
60
- throw new Error(`a pattern with ${what}`);
61
- }
62
-
63
- /** A UTF-16 unit as the one code point RE2 reads: a surrogate lifted, any other unit itself. */
64
- function lifted(unit: number) {
65
- return unit >= 0xd800 && unit <= 0xdfff ? LIFTED_SURROGATES + unit - 0xd800 : unit;
66
- }
67
-
68
- /** `input` with every surrogate unit lifted, one code point per UTF-16 unit. */
69
- function liftedText(input: string) {
70
- return input.replace(SURROGATE, (unit) => String.fromCodePoint(lifted(unit.charCodeAt(0))));
71
- }
72
-
73
- /** One UTF-16 unit as RE2 reads it literally, inside a class or out. */
74
- function literal(unit: number) {
75
- const char = lifted(unit);
76
- const spelled = String.fromCodePoint(char);
77
- return /^[A-Za-z0-9]$/.test(spelled) ? spelled : `\\x{${char.toString(16)}}`;
78
- }
79
-
80
- /**
81
- * The units from `from` to `to` as the inside of an RE2 class: the part
82
- * among the surrogates, lifted, apart from the parts below and above it.
83
- */
84
- function unitRange(from: number, to: number) {
85
- return (
86
- [
87
- [from, Math.min(to, 0xd7ff)],
88
- [Math.max(from, 0xd800), Math.min(to, 0xdfff)],
89
- [Math.max(from, 0xe000), to],
90
- ] satisfies Array<[number, number]>
91
- )
92
- .filter(([low, high]) => low <= high)
93
- .map(([low, high]) => `${literal(low)}-${literal(high)}`)
94
- .join("");
95
- }
96
-
97
- function character(char: number, dash = false) {
98
- return { body: literal(char), char, dash };
99
- }
100
-
101
- /**
102
- * A class's atoms as the inside of an RE2 class: an unescaped dash between
103
- * two characters makes a range, and beside a set escape it is a character,
104
- * as JavaScript reads them.
105
- */
106
- function classBody(atoms: Atom[]) {
107
- let body = "";
108
- for (let index = 0; index < atoms.length; index += 1) {
109
- const [from, dash, to] = atoms.slice(index, index + 3);
110
- if (!from) break;
111
- if (!dash?.dash || !to) {
112
- body += from.body;
113
- continue;
114
- }
115
- body +=
116
- from.char !== undefined && to.char !== undefined
117
- ? unitRange(from.char, to.char)
118
- : `${from.body}${dash.body}${to.body}`;
119
- index += 2;
120
- }
121
- return body;
122
- }
123
-
124
- /**
125
- * A pattern read left to right as JavaScript reads it with no flags (Annex B
126
- * included), written in RE2's syntax; throws on a lookaround, a
127
- * backreference, a legacy octal escape, a group modifier or a `\c` without
128
- * its letter. It reads UTF-16 units, as JavaScript does. Groups do not
129
- * capture: nothing reads them.
130
- */
131
- class Translation {
132
- private at = 0;
133
-
134
- public constructor(private readonly pattern: string) {}
135
-
136
- public spell() {
137
- let re2 = "";
138
- while (this.at < this.pattern.length) re2 += this.term();
139
- return re2;
140
- }
141
-
142
- private term() {
143
- const char = this.pattern[this.at] ?? "";
144
- if (char === "\\") return this.escapeOutsideClass();
145
- if (char === "[") return this.characterClass();
146
- if (char === "(") return this.group();
147
- if (char === "{") return this.brace();
148
- if (!"|)^$*+?.".includes(char)) return literal(this.unit());
149
- this.at += 1;
150
- return char === "." ? DOT : char;
151
- }
152
-
153
- private escapeOutsideClass() {
154
- this.at += 1;
155
- const assertion = this.pattern[this.at];
156
- if (assertion === "b" || assertion === "B") {
157
- this.at += 1;
158
- return `\\${assertion}`;
159
- }
160
- const atom = this.escape(false);
161
- return "char" in atom ? atom.body : `[${atom.body}]`;
162
- }
163
-
164
- /** The escape after a backslash, which `at` is past; in a class `\b` is a backspace. */
165
- private escape(insideClass: boolean) {
166
- const next = this.pattern[this.at] ?? unsupported("a trailing backslash");
167
- this.at += 1;
168
- const set = SET_ESCAPES.get(next);
169
- if (set) return { body: set };
170
- const control = insideClass && next === "b" ? 0x08 : CONTROL_ESCAPES.get(next);
171
- if (control !== undefined) return character(control);
172
- if (next === "c") return character(this.controlLetter(insideClass));
173
- if (next === "x" || next === "u") {
174
- return character(this.hex(next === "x" ? 2 : 4) ?? next.charCodeAt(0));
175
- }
176
- if (next === "0" && !/^\d$/.test(this.pattern[this.at] ?? "")) return character(0);
177
- if (/^[\dk]$/.test(next)) unsupported("a backreference or an octal escape");
178
- this.at -= 1;
179
- return character(this.unit());
180
- }
181
-
182
- /** The control character a `\c` names: a letter, or in a class also a digit or `_`. */
183
- private controlLetter(insideClass: boolean) {
184
- const letter = this.pattern[this.at] ?? "";
185
- if (!(insideClass ? /^\w$/ : /^[A-Za-z]$/).test(letter)) {
186
- unsupported("a \\c without its letter");
187
- }
188
- this.at += 1;
189
- return letter.charCodeAt(0) % 32;
190
- }
191
-
192
- /** `length` hex digits at `at` as a UTF-16 unit, consumed; undefined when fewer follow. */
193
- private hex(length: number) {
194
- HEX.lastIndex = this.at;
195
- const digits = HEX.exec(this.pattern)?.[0] ?? "";
196
- if (digits.length < length) return undefined;
197
- this.at += length;
198
- return Number.parseInt(digits.slice(0, length), 16);
199
- }
200
-
201
- private unit() {
202
- const unit = this.pattern.charCodeAt(this.at);
203
- if (Number.isNaN(unit)) unsupported("a pattern cut short");
204
- this.at += 1;
205
- return unit;
206
- }
207
-
208
- /**
209
- * A class, `at` on its `[`. In JavaScript a `]` always closes one, so
210
- * `[]` holds nothing and `[^]` everything.
211
- */
212
- private characterClass() {
213
- this.at += 1;
214
- const negated = this.pattern[this.at] === "^";
215
- if (negated) this.at += 1;
216
- const atoms: Atom[] = [];
217
- while (this.pattern[this.at] !== "]") {
218
- if (this.at >= this.pattern.length) unsupported("an unclosed class");
219
- atoms.push(this.classAtom());
220
- }
221
- this.at += 1;
222
- const body = classBody(atoms);
223
- if (!body) return negated ? ANY : NONE;
224
- return `[${negated ? "^" : ""}${body}]`;
225
- }
226
-
227
- private classAtom() {
228
- const char = this.pattern[this.at];
229
- this.at += 1;
230
- if (char === "\\") return this.escape(true);
231
- if (char === "-") return character(0x2d, true);
232
- this.at -= 1;
233
- return character(this.unit());
234
- }
235
-
236
- /** A group's opening as RE2 reads it: every group, named or not, without a capture. */
237
- private group() {
238
- const opening = this.pattern.slice(this.at, this.at + 4);
239
- if (/^\(\?<[^=!]/.test(opening)) {
240
- const name = this.pattern.indexOf(">", this.at);
241
- if (name < 0) unsupported("an unclosed group name");
242
- this.at = name + 1;
243
- } else if (opening.startsWith("(?")) {
244
- if (!opening.startsWith("(?:")) unsupported("a lookaround or a group modifier");
245
- this.at += 3;
246
- } else {
247
- this.at += 1;
248
- }
249
- return "(?:";
250
- }
251
-
252
- /** A quantifier, or a brace JavaScript reads as a character because it opens none. */
253
- private brace() {
254
- QUANTIFIER.lastIndex = this.at;
255
- const quantifier = QUANTIFIER.exec(this.pattern)?.[0];
256
- this.at += quantifier?.length ?? 1;
257
- return quantifier ?? literal(0x7b);
258
- }
259
- }
260
-
261
- /**
262
- * A matcher for a pattern JavaScript compiles, meaning what the pattern
263
- * means to JavaScript with no flags, UTF-16 unit for unit; undefined when
264
- * RE2 cannot match it so, as for a lookaround, a backreference or a
265
- * repetition past 1000. Its `test` runs on RE2, in time linear in the input.
266
- * It never calls `new RegExp`, so it works while a caller has replaced that
267
- * global.
268
- */
269
- export function linearPattern(pattern: string) {
270
- try {
271
- const re2 = RE2JS.compile(new Translation(pattern).spell());
272
- return { test: (input: string) => re2.test(liftedText(input)) };
273
- } catch {
274
- return undefined;
275
- }
276
- }
@@ -1,18 +0,0 @@
1
- {
2
- "tools": [
3
- "describeDataset",
4
- "executeCode",
5
- "fetchImage",
6
- "loadSkill",
7
- "queryDataset",
8
- "readAttachment",
9
- "readReport",
10
- "searchKnowledge",
11
- "searchServices",
12
- "searchThreadHistory",
13
- "searchTools",
14
- "requestLocation",
15
- "submitAnswer"
16
- ],
17
- "variables": ["utcTime", "locale", "timezone"]
18
- }