@kvsig/core 0.0.1 → 0.1.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.
- package/LICENSE +202 -0
- package/NOTICE +7 -0
- package/README.md +142 -3
- package/dist/index.cjs +1653 -0
- package/dist/index.d.cts +240 -0
- package/dist/index.d.ts +240 -0
- package/dist/index.js +1622 -0
- package/package.json +76 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,1622 @@
|
|
|
1
|
+
import { hmac } from '@noble/hashes/hmac';
|
|
2
|
+
import { sha256 as sha256$1 } from '@noble/hashes/sha2';
|
|
3
|
+
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __typeError = (msg) => {
|
|
6
|
+
throw TypeError(msg);
|
|
7
|
+
};
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
13
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), member.get(obj));
|
|
14
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
15
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
16
|
+
|
|
17
|
+
// src/text.ts
|
|
18
|
+
var REPLACEMENT = "\uFFFD";
|
|
19
|
+
function wellFormed(s) {
|
|
20
|
+
let i = 0;
|
|
21
|
+
const n = s.length;
|
|
22
|
+
for (; i < n; i++) {
|
|
23
|
+
const c = s.charCodeAt(i);
|
|
24
|
+
if (c >= 55296 && c <= 57343) break;
|
|
25
|
+
}
|
|
26
|
+
if (i === n) return s;
|
|
27
|
+
let out = s.slice(0, i);
|
|
28
|
+
for (; i < n; i++) {
|
|
29
|
+
const c = s.charCodeAt(i);
|
|
30
|
+
if (c >= 55296 && c <= 56319) {
|
|
31
|
+
const d = i + 1 < n ? s.charCodeAt(i + 1) : 0;
|
|
32
|
+
if (d >= 56320 && d <= 57343) {
|
|
33
|
+
out += s[i] + s[i + 1];
|
|
34
|
+
i++;
|
|
35
|
+
} else {
|
|
36
|
+
out += REPLACEMENT;
|
|
37
|
+
}
|
|
38
|
+
} else if (c >= 56320 && c <= 57343) {
|
|
39
|
+
out += REPLACEMENT;
|
|
40
|
+
} else {
|
|
41
|
+
out += s[i];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
function utf8(s) {
|
|
47
|
+
const n = s.length;
|
|
48
|
+
let ascii = true;
|
|
49
|
+
for (let i = 0; i < n; i++) {
|
|
50
|
+
if (s.charCodeAt(i) > 127) {
|
|
51
|
+
ascii = false;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (ascii) {
|
|
56
|
+
const out2 = new Uint8Array(n);
|
|
57
|
+
for (let i = 0; i < n; i++) out2[i] = s.charCodeAt(i);
|
|
58
|
+
return out2;
|
|
59
|
+
}
|
|
60
|
+
const out = [];
|
|
61
|
+
for (let i = 0; i < n; i++) {
|
|
62
|
+
let c = s.charCodeAt(i);
|
|
63
|
+
if (c >= 55296 && c <= 56319) {
|
|
64
|
+
const d = i + 1 < n ? s.charCodeAt(i + 1) : 0;
|
|
65
|
+
if (d >= 56320 && d <= 57343) {
|
|
66
|
+
c = 65536 + (c - 55296 << 10) + (d - 56320);
|
|
67
|
+
i++;
|
|
68
|
+
} else {
|
|
69
|
+
c = 65533;
|
|
70
|
+
}
|
|
71
|
+
} else if (c >= 56320 && c <= 57343) {
|
|
72
|
+
c = 65533;
|
|
73
|
+
}
|
|
74
|
+
if (c < 128) out.push(c);
|
|
75
|
+
else if (c < 2048) out.push(192 | c >> 6, 128 | c & 63);
|
|
76
|
+
else if (c < 65536) out.push(224 | c >> 12, 128 | c >> 6 & 63, 128 | c & 63);
|
|
77
|
+
else
|
|
78
|
+
out.push(
|
|
79
|
+
240 | c >> 18,
|
|
80
|
+
128 | c >> 12 & 63,
|
|
81
|
+
128 | c >> 6 & 63,
|
|
82
|
+
128 | c & 63
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return Uint8Array.from(out);
|
|
86
|
+
}
|
|
87
|
+
function decodeUtf8(b) {
|
|
88
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(b);
|
|
89
|
+
}
|
|
90
|
+
function compareCodePoints(a, b) {
|
|
91
|
+
const n = Math.min(a.length, b.length);
|
|
92
|
+
for (let i = 0; i < n; i++) {
|
|
93
|
+
const x = a.charCodeAt(i);
|
|
94
|
+
const y = b.charCodeAt(i);
|
|
95
|
+
if (x === y) continue;
|
|
96
|
+
const xs = x >= 55296 && x <= 57343;
|
|
97
|
+
const ys = y >= 55296 && y <= 57343;
|
|
98
|
+
if (xs !== ys) return xs ? 1 : -1;
|
|
99
|
+
return x - y;
|
|
100
|
+
}
|
|
101
|
+
return a.length - b.length;
|
|
102
|
+
}
|
|
103
|
+
function concatBytes(parts) {
|
|
104
|
+
let len = 0;
|
|
105
|
+
for (const p of parts) len += p.length;
|
|
106
|
+
const out = new Uint8Array(len);
|
|
107
|
+
let off = 0;
|
|
108
|
+
for (const p of parts) {
|
|
109
|
+
out.set(p, off);
|
|
110
|
+
off += p.length;
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
var HEX = "0123456789abcdef";
|
|
115
|
+
function toHex(b) {
|
|
116
|
+
let s = "";
|
|
117
|
+
for (let i = 0; i < b.length; i++) s += HEX[b[i] >> 4] + HEX[b[i] & 15];
|
|
118
|
+
return s;
|
|
119
|
+
}
|
|
120
|
+
var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
121
|
+
var B64_INDEX = (() => {
|
|
122
|
+
const t = new Int16Array(128).fill(-1);
|
|
123
|
+
for (let i = 0; i < 64; i++) t[B64.charCodeAt(i)] = i;
|
|
124
|
+
return t;
|
|
125
|
+
})();
|
|
126
|
+
function fromBase64Strict(input) {
|
|
127
|
+
const s = input.replace(/[ \t\n\r\f\v]/g, "");
|
|
128
|
+
if (s.length % 4 !== 0) return null;
|
|
129
|
+
let pad = 0;
|
|
130
|
+
if (s.endsWith("==")) pad = 2;
|
|
131
|
+
else if (s.endsWith("=")) pad = 1;
|
|
132
|
+
const body = s.length - pad;
|
|
133
|
+
const out = new Uint8Array(s.length / 4 * 3 - pad);
|
|
134
|
+
let o = 0;
|
|
135
|
+
let acc = 0;
|
|
136
|
+
let bits = 0;
|
|
137
|
+
for (let i = 0; i < body; i++) {
|
|
138
|
+
const c = s.charCodeAt(i);
|
|
139
|
+
const v = c < 128 ? B64_INDEX[c] : -1;
|
|
140
|
+
if (v < 0) return null;
|
|
141
|
+
acc = acc << 6 | v;
|
|
142
|
+
bits += 6;
|
|
143
|
+
if (bits >= 8) {
|
|
144
|
+
bits -= 8;
|
|
145
|
+
out[o++] = acc >> bits & 255;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return o === out.length ? out : null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/cbor.ts
|
|
152
|
+
var Writer = class {
|
|
153
|
+
constructor() {
|
|
154
|
+
this.buf = new Uint8Array(256);
|
|
155
|
+
this.len = 0;
|
|
156
|
+
}
|
|
157
|
+
ensure(n) {
|
|
158
|
+
if (this.len + n <= this.buf.length) return;
|
|
159
|
+
let cap = this.buf.length * 2;
|
|
160
|
+
while (cap < this.len + n) cap *= 2;
|
|
161
|
+
const b = new Uint8Array(cap);
|
|
162
|
+
b.set(this.buf.subarray(0, this.len));
|
|
163
|
+
this.buf = b;
|
|
164
|
+
}
|
|
165
|
+
byte(b) {
|
|
166
|
+
this.ensure(1);
|
|
167
|
+
this.buf[this.len++] = b;
|
|
168
|
+
}
|
|
169
|
+
bytes(b) {
|
|
170
|
+
this.ensure(b.length);
|
|
171
|
+
this.buf.set(b, this.len);
|
|
172
|
+
this.len += b.length;
|
|
173
|
+
}
|
|
174
|
+
head(major, n) {
|
|
175
|
+
const m = major << 5;
|
|
176
|
+
if (n < 24) this.byte(m | n);
|
|
177
|
+
else if (n < 256) {
|
|
178
|
+
this.byte(m | 24);
|
|
179
|
+
this.byte(n);
|
|
180
|
+
} else if (n < 65536) {
|
|
181
|
+
this.byte(m | 25);
|
|
182
|
+
this.byte(n >>> 8);
|
|
183
|
+
this.byte(n & 255);
|
|
184
|
+
} else if (n < 4294967296) {
|
|
185
|
+
this.byte(m | 26);
|
|
186
|
+
this.byte(n >>> 24);
|
|
187
|
+
this.byte(n >>> 16 & 255);
|
|
188
|
+
this.byte(n >>> 8 & 255);
|
|
189
|
+
this.byte(n & 255);
|
|
190
|
+
} else {
|
|
191
|
+
this.byte(m | 27);
|
|
192
|
+
const hi = Math.floor(n / 4294967296);
|
|
193
|
+
const lo = n >>> 0;
|
|
194
|
+
for (const v of [hi, lo]) {
|
|
195
|
+
this.byte(v >>> 24);
|
|
196
|
+
this.byte(v >>> 16 & 255);
|
|
197
|
+
this.byte(v >>> 8 & 255);
|
|
198
|
+
this.byte(v & 255);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
result() {
|
|
203
|
+
return this.buf.slice(0, this.len);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
function keyBytes(k) {
|
|
207
|
+
const w = new Writer();
|
|
208
|
+
const b = utf8(k);
|
|
209
|
+
w.head(3, b.length);
|
|
210
|
+
w.bytes(b);
|
|
211
|
+
return w.result();
|
|
212
|
+
}
|
|
213
|
+
function compareBytes(a, b) {
|
|
214
|
+
const n = Math.min(a.length, b.length);
|
|
215
|
+
for (let i = 0; i < n; i++) if (a[i] !== b[i]) return a[i] - b[i];
|
|
216
|
+
return a.length - b.length;
|
|
217
|
+
}
|
|
218
|
+
var KEY_CACHE = /* @__PURE__ */ new Map();
|
|
219
|
+
function cachedKey(k) {
|
|
220
|
+
let b = KEY_CACHE.get(k);
|
|
221
|
+
if (b === void 0) {
|
|
222
|
+
b = keyBytes(k);
|
|
223
|
+
if (KEY_CACHE.size < 1024) KEY_CACHE.set(k, b);
|
|
224
|
+
}
|
|
225
|
+
return b;
|
|
226
|
+
}
|
|
227
|
+
function write(w, v) {
|
|
228
|
+
if (typeof v === "number") {
|
|
229
|
+
if (!Number.isSafeInteger(v)) throw new TypeError("cbor: only safe integers are allowed");
|
|
230
|
+
if (v >= 0) w.head(0, v);
|
|
231
|
+
else w.head(1, -1 - v);
|
|
232
|
+
} else if (typeof v === "string") {
|
|
233
|
+
const b = utf8(v);
|
|
234
|
+
w.head(3, b.length);
|
|
235
|
+
w.bytes(b);
|
|
236
|
+
} else if (typeof v === "boolean") {
|
|
237
|
+
w.byte(v ? 245 : 244);
|
|
238
|
+
} else if (v instanceof Uint8Array) {
|
|
239
|
+
w.head(2, v.length);
|
|
240
|
+
w.bytes(v);
|
|
241
|
+
} else if (Array.isArray(v)) {
|
|
242
|
+
w.head(4, v.length);
|
|
243
|
+
for (const x of v) write(w, x);
|
|
244
|
+
} else if (v !== null && typeof v === "object") {
|
|
245
|
+
const entries = [];
|
|
246
|
+
for (const k of Object.keys(v)) {
|
|
247
|
+
const x = v[k];
|
|
248
|
+
if (x === void 0) continue;
|
|
249
|
+
entries.push([cachedKey(k), x]);
|
|
250
|
+
}
|
|
251
|
+
entries.sort((a, b) => compareBytes(a[0], b[0]));
|
|
252
|
+
w.head(5, entries.length);
|
|
253
|
+
for (let i = 0; i < entries.length; i++) {
|
|
254
|
+
const [kb, x] = entries[i];
|
|
255
|
+
if (i > 0 && compareBytes(entries[i - 1][0], kb) === 0) {
|
|
256
|
+
throw new TypeError("cbor: duplicate map key");
|
|
257
|
+
}
|
|
258
|
+
w.bytes(kb);
|
|
259
|
+
write(w, x);
|
|
260
|
+
}
|
|
261
|
+
} else {
|
|
262
|
+
throw new TypeError(`cbor: value of type ${v === null ? "null" : typeof v} is not allowed`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function encode(v) {
|
|
266
|
+
const w = new Writer();
|
|
267
|
+
write(w, v);
|
|
268
|
+
return w.result();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// src/dialects/anthropic-messages.ts
|
|
272
|
+
var anthropic_messages_exports = {};
|
|
273
|
+
__export(anthropic_messages_exports, {
|
|
274
|
+
canonicalize: () => canonicalize,
|
|
275
|
+
reply: () => reply
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// src/json.ts
|
|
279
|
+
var NoIds = class extends Error {
|
|
280
|
+
constructor(why) {
|
|
281
|
+
super(why);
|
|
282
|
+
this.name = "NoIds";
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
var JsonSyntaxError = class extends Error {
|
|
286
|
+
constructor(why, at) {
|
|
287
|
+
super(`${why} at offset ${at}`);
|
|
288
|
+
this.name = "JsonSyntaxError";
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
var MAX_DEPTH = 128;
|
|
292
|
+
var BODY_MAX_DEPTH = 1e3;
|
|
293
|
+
function isObject(v) {
|
|
294
|
+
return v instanceof Map;
|
|
295
|
+
}
|
|
296
|
+
var Parser = class {
|
|
297
|
+
constructor(s, maxDepth) {
|
|
298
|
+
this.s = s;
|
|
299
|
+
this.maxDepth = maxDepth;
|
|
300
|
+
this.i = 0;
|
|
301
|
+
}
|
|
302
|
+
fail(why) {
|
|
303
|
+
throw new JsonSyntaxError(why, this.i);
|
|
304
|
+
}
|
|
305
|
+
ws() {
|
|
306
|
+
const s = this.s;
|
|
307
|
+
for (; ; ) {
|
|
308
|
+
const c = s.charCodeAt(this.i);
|
|
309
|
+
if (c === 32 || c === 10 || c === 13 || c === 9) this.i++;
|
|
310
|
+
else return;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
value(depth) {
|
|
314
|
+
this.ws();
|
|
315
|
+
const c = this.s[this.i];
|
|
316
|
+
switch (c) {
|
|
317
|
+
case "{":
|
|
318
|
+
return this.object(depth + 1);
|
|
319
|
+
case "[":
|
|
320
|
+
return this.array(depth + 1);
|
|
321
|
+
case '"':
|
|
322
|
+
return this.string();
|
|
323
|
+
case "t":
|
|
324
|
+
return this.literal("true", true);
|
|
325
|
+
case "f":
|
|
326
|
+
return this.literal("false", false);
|
|
327
|
+
case "n":
|
|
328
|
+
return this.literal("null", null);
|
|
329
|
+
default:
|
|
330
|
+
if (c === "-" || c !== void 0 && c >= "0" && c <= "9") return this.number();
|
|
331
|
+
return this.fail("unexpected character");
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
literal(word, v) {
|
|
335
|
+
if (this.s.startsWith(word, this.i)) {
|
|
336
|
+
this.i += word.length;
|
|
337
|
+
return v;
|
|
338
|
+
}
|
|
339
|
+
return this.fail("invalid literal");
|
|
340
|
+
}
|
|
341
|
+
depthCheck(depth) {
|
|
342
|
+
if (depth > this.maxDepth) throw new NoIds("nested too deep");
|
|
343
|
+
}
|
|
344
|
+
object(depth) {
|
|
345
|
+
this.depthCheck(depth);
|
|
346
|
+
this.i++;
|
|
347
|
+
const m = /* @__PURE__ */ new Map();
|
|
348
|
+
this.ws();
|
|
349
|
+
if (this.s[this.i] === "}") {
|
|
350
|
+
this.i++;
|
|
351
|
+
return m;
|
|
352
|
+
}
|
|
353
|
+
for (; ; ) {
|
|
354
|
+
this.ws();
|
|
355
|
+
if (this.s[this.i] !== '"') this.fail("expected a member name");
|
|
356
|
+
const k = this.string();
|
|
357
|
+
this.ws();
|
|
358
|
+
if (this.s[this.i] !== ":") this.fail("expected ':'");
|
|
359
|
+
this.i++;
|
|
360
|
+
const v = this.value(depth);
|
|
361
|
+
m.set(k, v);
|
|
362
|
+
this.ws();
|
|
363
|
+
const c = this.s[this.i];
|
|
364
|
+
if (c === ",") {
|
|
365
|
+
this.i++;
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
if (c === "}") {
|
|
369
|
+
this.i++;
|
|
370
|
+
return m;
|
|
371
|
+
}
|
|
372
|
+
this.fail("expected ',' or '}'");
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
array(depth) {
|
|
376
|
+
this.depthCheck(depth);
|
|
377
|
+
this.i++;
|
|
378
|
+
const a = [];
|
|
379
|
+
this.ws();
|
|
380
|
+
if (this.s[this.i] === "]") {
|
|
381
|
+
this.i++;
|
|
382
|
+
return a;
|
|
383
|
+
}
|
|
384
|
+
for (; ; ) {
|
|
385
|
+
a.push(this.value(depth));
|
|
386
|
+
this.ws();
|
|
387
|
+
const c = this.s[this.i];
|
|
388
|
+
if (c === ",") {
|
|
389
|
+
this.i++;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (c === "]") {
|
|
393
|
+
this.i++;
|
|
394
|
+
return a;
|
|
395
|
+
}
|
|
396
|
+
this.fail("expected ',' or ']'");
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
string() {
|
|
400
|
+
const s = this.s;
|
|
401
|
+
this.i++;
|
|
402
|
+
let out = "";
|
|
403
|
+
let start = this.i;
|
|
404
|
+
for (; ; ) {
|
|
405
|
+
if (this.i >= s.length) this.fail("unterminated string");
|
|
406
|
+
const c = s.charCodeAt(this.i);
|
|
407
|
+
if (c === 34) {
|
|
408
|
+
out += s.slice(start, this.i);
|
|
409
|
+
this.i++;
|
|
410
|
+
return wellFormed(out);
|
|
411
|
+
}
|
|
412
|
+
if (c < 32) this.fail("control character in string");
|
|
413
|
+
if (c !== 92) {
|
|
414
|
+
this.i++;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
out += s.slice(start, this.i);
|
|
418
|
+
const e = s[this.i + 1];
|
|
419
|
+
this.i += 2;
|
|
420
|
+
switch (e) {
|
|
421
|
+
case '"':
|
|
422
|
+
out += '"';
|
|
423
|
+
break;
|
|
424
|
+
case "\\":
|
|
425
|
+
out += "\\";
|
|
426
|
+
break;
|
|
427
|
+
case "/":
|
|
428
|
+
out += "/";
|
|
429
|
+
break;
|
|
430
|
+
case "b":
|
|
431
|
+
out += "\b";
|
|
432
|
+
break;
|
|
433
|
+
case "f":
|
|
434
|
+
out += "\f";
|
|
435
|
+
break;
|
|
436
|
+
case "n":
|
|
437
|
+
out += "\n";
|
|
438
|
+
break;
|
|
439
|
+
case "r":
|
|
440
|
+
out += "\r";
|
|
441
|
+
break;
|
|
442
|
+
case "t":
|
|
443
|
+
out += " ";
|
|
444
|
+
break;
|
|
445
|
+
case "u": {
|
|
446
|
+
const h = s.slice(this.i, this.i + 4);
|
|
447
|
+
if (!/^[0-9a-fA-F]{4}$/.test(h)) this.fail("invalid \\u escape");
|
|
448
|
+
out += String.fromCharCode(parseInt(h, 16));
|
|
449
|
+
this.i += 4;
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
default:
|
|
453
|
+
this.i -= 2;
|
|
454
|
+
this.fail("invalid escape");
|
|
455
|
+
}
|
|
456
|
+
start = this.i;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
number() {
|
|
460
|
+
const s = this.s;
|
|
461
|
+
const start = this.i;
|
|
462
|
+
if (s[this.i] === "-") this.i++;
|
|
463
|
+
if (s[this.i] === "0") this.i++;
|
|
464
|
+
else if (s[this.i] >= "1" && s[this.i] <= "9") {
|
|
465
|
+
while (s[this.i] >= "0" && s[this.i] <= "9") this.i++;
|
|
466
|
+
} else this.fail("invalid number");
|
|
467
|
+
if (s[this.i] === ".") {
|
|
468
|
+
this.i++;
|
|
469
|
+
if (!(s[this.i] >= "0" && s[this.i] <= "9")) this.fail("invalid number");
|
|
470
|
+
while (s[this.i] >= "0" && s[this.i] <= "9") this.i++;
|
|
471
|
+
}
|
|
472
|
+
if (s[this.i] === "e" || s[this.i] === "E") {
|
|
473
|
+
this.i++;
|
|
474
|
+
if (s[this.i] === "+" || s[this.i] === "-") this.i++;
|
|
475
|
+
if (!(s[this.i] >= "0" && s[this.i] <= "9")) this.fail("invalid number");
|
|
476
|
+
while (s[this.i] >= "0" && s[this.i] <= "9") this.i++;
|
|
477
|
+
}
|
|
478
|
+
return Number(s.slice(start, this.i));
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
function parseJson(text, maxDepth = BODY_MAX_DEPTH) {
|
|
482
|
+
let s;
|
|
483
|
+
if (typeof text === "string") s = text;
|
|
484
|
+
else {
|
|
485
|
+
try {
|
|
486
|
+
s = decodeUtf8(text);
|
|
487
|
+
} catch {
|
|
488
|
+
throw new JsonSyntaxError("invalid UTF-8", 0);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
const p = new Parser(s, maxDepth);
|
|
492
|
+
const v = p.value(0);
|
|
493
|
+
p.ws();
|
|
494
|
+
if (p.i !== s.length) p.fail("unexpected text after the value");
|
|
495
|
+
return v;
|
|
496
|
+
}
|
|
497
|
+
function fromJS(v, depth = 0) {
|
|
498
|
+
if (v === null || typeof v === "boolean" || typeof v === "number") return v;
|
|
499
|
+
if (typeof v === "string") return wellFormed(v);
|
|
500
|
+
if (typeof v === "bigint") return Number(v);
|
|
501
|
+
if (depth >= BODY_MAX_DEPTH) throw new NoIds("nested too deep");
|
|
502
|
+
if (v instanceof Map) {
|
|
503
|
+
const m = /* @__PURE__ */ new Map();
|
|
504
|
+
for (const [k, x] of v) {
|
|
505
|
+
if (typeof k !== "string") throw new NoIds("map key is not a string");
|
|
506
|
+
if (x === void 0 || typeof x === "function" || typeof x === "symbol") continue;
|
|
507
|
+
m.set(wellFormed(k), fromJS(x, depth + 1));
|
|
508
|
+
}
|
|
509
|
+
return m;
|
|
510
|
+
}
|
|
511
|
+
if (Array.isArray(v)) {
|
|
512
|
+
return v.map(
|
|
513
|
+
(x) => x === void 0 || typeof x === "function" || typeof x === "symbol" ? null : fromJS(x, depth + 1)
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
if (typeof v === "object") {
|
|
517
|
+
const o = v;
|
|
518
|
+
if (typeof o.toJSON === "function") return fromJS(o.toJSON(), depth);
|
|
519
|
+
const m = /* @__PURE__ */ new Map();
|
|
520
|
+
for (const k of Object.keys(v)) {
|
|
521
|
+
const x = v[k];
|
|
522
|
+
if (x === void 0 || typeof x === "function" || typeof x === "symbol") continue;
|
|
523
|
+
m.set(wellFormed(k), fromJS(x, depth + 1));
|
|
524
|
+
}
|
|
525
|
+
return m;
|
|
526
|
+
}
|
|
527
|
+
throw new NoIds(`value of type ${typeof v} is not JSON`);
|
|
528
|
+
}
|
|
529
|
+
function quote(s) {
|
|
530
|
+
let out = '"';
|
|
531
|
+
let start = 0;
|
|
532
|
+
for (let i = 0; i < s.length; i++) {
|
|
533
|
+
const c = s.charCodeAt(i);
|
|
534
|
+
if (c >= 32 && c !== 34 && c !== 92) continue;
|
|
535
|
+
out += s.slice(start, i);
|
|
536
|
+
start = i + 1;
|
|
537
|
+
switch (c) {
|
|
538
|
+
case 34:
|
|
539
|
+
out += '\\"';
|
|
540
|
+
break;
|
|
541
|
+
case 92:
|
|
542
|
+
out += "\\\\";
|
|
543
|
+
break;
|
|
544
|
+
case 8:
|
|
545
|
+
out += "\\b";
|
|
546
|
+
break;
|
|
547
|
+
case 12:
|
|
548
|
+
out += "\\f";
|
|
549
|
+
break;
|
|
550
|
+
case 10:
|
|
551
|
+
out += "\\n";
|
|
552
|
+
break;
|
|
553
|
+
case 13:
|
|
554
|
+
out += "\\r";
|
|
555
|
+
break;
|
|
556
|
+
case 9:
|
|
557
|
+
out += "\\t";
|
|
558
|
+
break;
|
|
559
|
+
default:
|
|
560
|
+
out += "\\u00" + (c < 16 ? "0" : "") + c.toString(16);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return out + s.slice(start) + '"';
|
|
564
|
+
}
|
|
565
|
+
function num(n) {
|
|
566
|
+
if (!Number.isFinite(n)) return "null";
|
|
567
|
+
if (Object.is(n, -0)) return "0";
|
|
568
|
+
return String(n);
|
|
569
|
+
}
|
|
570
|
+
function dumpInto(out, v, sort, depth) {
|
|
571
|
+
if (v === null) out.push("null");
|
|
572
|
+
else if (v === true) out.push("true");
|
|
573
|
+
else if (v === false) out.push("false");
|
|
574
|
+
else if (typeof v === "number") out.push(num(v));
|
|
575
|
+
else if (typeof v === "string") out.push(quote(wellFormed(v)));
|
|
576
|
+
else {
|
|
577
|
+
if (depth + 1 > MAX_DEPTH) throw new NoIds("nested too deep");
|
|
578
|
+
if (Array.isArray(v)) {
|
|
579
|
+
out.push("[");
|
|
580
|
+
for (let i = 0; i < v.length; i++) {
|
|
581
|
+
if (i > 0) out.push(",");
|
|
582
|
+
dumpInto(out, v[i], sort, depth + 1);
|
|
583
|
+
}
|
|
584
|
+
out.push("]");
|
|
585
|
+
} else {
|
|
586
|
+
let entries = [...v.entries()];
|
|
587
|
+
if (sort) entries = entries.sort((a, b) => compareCodePoints(a[0], b[0]));
|
|
588
|
+
out.push("{");
|
|
589
|
+
for (let i = 0; i < entries.length; i++) {
|
|
590
|
+
if (i > 0) out.push(",");
|
|
591
|
+
out.push(quote(entries[i][0]), ":");
|
|
592
|
+
dumpInto(out, entries[i][1], sort, depth + 1);
|
|
593
|
+
}
|
|
594
|
+
out.push("}");
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function canonicalJson(v, sort = false) {
|
|
599
|
+
const out = [];
|
|
600
|
+
dumpInto(out, v, sort, 0);
|
|
601
|
+
return out.join("");
|
|
602
|
+
}
|
|
603
|
+
function sha256(data) {
|
|
604
|
+
return sha256$1(data);
|
|
605
|
+
}
|
|
606
|
+
function hmacSha256(key, ...parts) {
|
|
607
|
+
const m = hmac.create(sha256$1, key);
|
|
608
|
+
for (const p of parts) m.update(p);
|
|
609
|
+
return m.digest();
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// src/model.ts
|
|
613
|
+
var HEAD_ROLES = /* @__PURE__ */ new Set(["system", "developer"]);
|
|
614
|
+
var DEFAULT_MAX_BYTES = 2 * 1024 * 1024;
|
|
615
|
+
function asCbor(v) {
|
|
616
|
+
return v;
|
|
617
|
+
}
|
|
618
|
+
function headPayload(c) {
|
|
619
|
+
return asCbor({ head: c.head, tools: c.tools });
|
|
620
|
+
}
|
|
621
|
+
function finish(c, maxBytes) {
|
|
622
|
+
let total = encode(headPayload(c)).length;
|
|
623
|
+
if (total > maxBytes) return null;
|
|
624
|
+
for (const m of c.messages) {
|
|
625
|
+
total += encode(asCbor(m)).length;
|
|
626
|
+
if (total > maxBytes) return null;
|
|
627
|
+
}
|
|
628
|
+
return c;
|
|
629
|
+
}
|
|
630
|
+
function textPart(v) {
|
|
631
|
+
return v === "" ? null : { t: "text", v };
|
|
632
|
+
}
|
|
633
|
+
function opaque(v, type) {
|
|
634
|
+
return { t: "opaque", type, sha256: sha256(utf8(canonicalJson(withoutCacheControl(v)))) };
|
|
635
|
+
}
|
|
636
|
+
function mediaBytes(data, mime) {
|
|
637
|
+
return { t: "media", sha256: sha256(data), mime, src: "bytes" };
|
|
638
|
+
}
|
|
639
|
+
function mediaRef(ref, src, mime = "") {
|
|
640
|
+
return { t: "media", sha256: sha256(utf8(ref)), mime, src };
|
|
641
|
+
}
|
|
642
|
+
function str(v) {
|
|
643
|
+
return typeof v === "string" ? v : void 0;
|
|
644
|
+
}
|
|
645
|
+
function get(o, k) {
|
|
646
|
+
return isObject(o) ? o.get(k) : void 0;
|
|
647
|
+
}
|
|
648
|
+
function withoutCacheControl(o) {
|
|
649
|
+
if (!isObject(o) || !o.has("cache_control")) return o;
|
|
650
|
+
const m = new Map(o);
|
|
651
|
+
m.delete("cache_control");
|
|
652
|
+
return m;
|
|
653
|
+
}
|
|
654
|
+
function percentDecode(s) {
|
|
655
|
+
const raw = utf8(s);
|
|
656
|
+
const out = [];
|
|
657
|
+
const hex = (c) => c >= 48 && c <= 57 ? c - 48 : c >= 65 && c <= 70 ? c - 55 : c >= 97 && c <= 102 ? c - 87 : -1;
|
|
658
|
+
for (let i = 0; i < raw.length; i++) {
|
|
659
|
+
const c = raw[i];
|
|
660
|
+
if (c === 37 && i + 2 < raw.length) {
|
|
661
|
+
const a = hex(raw[i + 1]);
|
|
662
|
+
const b = hex(raw[i + 2]);
|
|
663
|
+
if (a >= 0 && b >= 0) {
|
|
664
|
+
out.push(a * 16 + b);
|
|
665
|
+
i += 2;
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
out.push(c);
|
|
670
|
+
}
|
|
671
|
+
return Uint8Array.from(out);
|
|
672
|
+
}
|
|
673
|
+
function parseDataUrl(url) {
|
|
674
|
+
if (!url.startsWith("data:")) return null;
|
|
675
|
+
const comma = url.indexOf(",");
|
|
676
|
+
if (comma < 0) return null;
|
|
677
|
+
const header = url.slice(5, comma).split(";");
|
|
678
|
+
const mime = header[0].trim().toLowerCase();
|
|
679
|
+
const isB64 = header.slice(1).some((p) => p.trim().toLowerCase() === "base64");
|
|
680
|
+
const payload = url.slice(comma + 1);
|
|
681
|
+
if (isB64) {
|
|
682
|
+
const data = fromBase64Strict(payload);
|
|
683
|
+
return data === null ? null : { data, mime };
|
|
684
|
+
}
|
|
685
|
+
return { data: percentDecode(payload), mime };
|
|
686
|
+
}
|
|
687
|
+
function mediaFromUrl(url) {
|
|
688
|
+
const d = parseDataUrl(url);
|
|
689
|
+
return d === null ? mediaRef(url, "url") : mediaBytes(d.data, d.mime);
|
|
690
|
+
}
|
|
691
|
+
function toolArgs(raw) {
|
|
692
|
+
if (raw === void 0 || raw === null) return { args: "null" };
|
|
693
|
+
if (typeof raw !== "string") return { args: canonicalJson(raw) };
|
|
694
|
+
let v;
|
|
695
|
+
try {
|
|
696
|
+
v = parseJson(raw, MAX_DEPTH);
|
|
697
|
+
} catch (e) {
|
|
698
|
+
if (e instanceof NoIds) throw e;
|
|
699
|
+
return { args_raw: raw };
|
|
700
|
+
}
|
|
701
|
+
return { args: canonicalJson(v) };
|
|
702
|
+
}
|
|
703
|
+
var RefTracker = class {
|
|
704
|
+
constructor() {
|
|
705
|
+
this.turns = [];
|
|
706
|
+
}
|
|
707
|
+
/** Record the call ids of one assistant message, by index. */
|
|
708
|
+
assistant(ids2) {
|
|
709
|
+
const m = /* @__PURE__ */ new Map();
|
|
710
|
+
ids2.forEach((id, i) => {
|
|
711
|
+
if (id !== void 0) m.set(id, i);
|
|
712
|
+
});
|
|
713
|
+
this.turns.push(m);
|
|
714
|
+
}
|
|
715
|
+
resolve(id) {
|
|
716
|
+
if (typeof id !== "string") return -1;
|
|
717
|
+
for (let i = this.turns.length - 1; i >= 0; i--) {
|
|
718
|
+
const r = this.turns[i].get(id);
|
|
719
|
+
if (r !== void 0) return r;
|
|
720
|
+
}
|
|
721
|
+
return -1;
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
function splitHead(msgs) {
|
|
725
|
+
let i = 0;
|
|
726
|
+
while (i < msgs.length && HEAD_ROLES.has(msgs[i].role)) i++;
|
|
727
|
+
return { head: msgs.slice(0, i), body: msgs.slice(i) };
|
|
728
|
+
}
|
|
729
|
+
function message(role, parts, name) {
|
|
730
|
+
return typeof name === "string" && name !== "" ? { name, role, parts } : { role, parts };
|
|
731
|
+
}
|
|
732
|
+
function functionTool(name, desc, params) {
|
|
733
|
+
return {
|
|
734
|
+
name: str(name) ?? "",
|
|
735
|
+
desc: str(desc) ?? "",
|
|
736
|
+
schema: canonicalJson(params === void 0 ? null : params)
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
function otherTool(t) {
|
|
740
|
+
return { type: str(get(t, "type")) ?? "", def: canonicalJson(t) };
|
|
741
|
+
}
|
|
742
|
+
function truthy(v) {
|
|
743
|
+
if (v === void 0 || v === null || v === false || v === 0 || v === "") return false;
|
|
744
|
+
if (Array.isArray(v)) return v.length > 0;
|
|
745
|
+
if (v instanceof Map) return v.size > 0;
|
|
746
|
+
return true;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// src/dialects/anthropic-messages.ts
|
|
750
|
+
function blockOpaque(b) {
|
|
751
|
+
return opaque(withoutCacheControl(b), str(get(b, "type")) ?? "");
|
|
752
|
+
}
|
|
753
|
+
function media(b) {
|
|
754
|
+
const t = str(get(b, "type"));
|
|
755
|
+
if (t === "document" && (get(b, "title") !== void 0 || get(b, "context") !== void 0)) {
|
|
756
|
+
return blockOpaque(b);
|
|
757
|
+
}
|
|
758
|
+
const src = get(b, "source");
|
|
759
|
+
const st = str(get(src, "type"));
|
|
760
|
+
const mime = str(get(src, "media_type")) ?? "";
|
|
761
|
+
if (st === "base64") {
|
|
762
|
+
const data = str(get(src, "data"));
|
|
763
|
+
const bytes = data === void 0 ? null : fromBase64Strict(data);
|
|
764
|
+
if (bytes !== null) return mediaBytes(bytes, mime);
|
|
765
|
+
} else if (st === "text") {
|
|
766
|
+
const data = str(get(src, "data"));
|
|
767
|
+
if (data !== void 0) return mediaBytes(utf8(data), mime);
|
|
768
|
+
} else if (st === "url") {
|
|
769
|
+
const url = str(get(src, "url"));
|
|
770
|
+
if (url !== void 0) return mediaRef(url, "url");
|
|
771
|
+
} else if (st === "file") {
|
|
772
|
+
const id = str(get(src, "file_id"));
|
|
773
|
+
if (id !== void 0) return mediaRef(id, "file_id");
|
|
774
|
+
}
|
|
775
|
+
return blockOpaque(b);
|
|
776
|
+
}
|
|
777
|
+
function block(b, assistant) {
|
|
778
|
+
if (typeof b === "string" && !assistant) return textPart(b);
|
|
779
|
+
if (!isObject(b)) return opaque(b, "block");
|
|
780
|
+
const t = str(b.get("type"));
|
|
781
|
+
if (t === "text") return textPart(str(b.get("text")) ?? "");
|
|
782
|
+
if (t === "image" || t === "document") return media(b);
|
|
783
|
+
if (t === "thinking" && assistant) {
|
|
784
|
+
const v = str(b.get("thinking")) ?? "";
|
|
785
|
+
return v === "" ? null : { t: "reasoning", v };
|
|
786
|
+
}
|
|
787
|
+
return blockOpaque(b);
|
|
788
|
+
}
|
|
789
|
+
function resultParts(c) {
|
|
790
|
+
if (c === void 0 || c === null) return [];
|
|
791
|
+
if (typeof c === "string") {
|
|
792
|
+
const p = textPart(c);
|
|
793
|
+
return p ? [p] : [];
|
|
794
|
+
}
|
|
795
|
+
if (Array.isArray(c)) {
|
|
796
|
+
const out = [];
|
|
797
|
+
for (const b of c) {
|
|
798
|
+
const p = block(b, false);
|
|
799
|
+
if (p) out.push(p);
|
|
800
|
+
}
|
|
801
|
+
return out;
|
|
802
|
+
}
|
|
803
|
+
return [opaque(c, "content")];
|
|
804
|
+
}
|
|
805
|
+
function assistantMessage(content, refs, role = "assistant") {
|
|
806
|
+
const parts = [];
|
|
807
|
+
const ids2 = [];
|
|
808
|
+
if (typeof content === "string") {
|
|
809
|
+
const p = textPart(content);
|
|
810
|
+
if (p) parts.push(p);
|
|
811
|
+
} else if (Array.isArray(content)) {
|
|
812
|
+
for (const b of content) {
|
|
813
|
+
if (isObject(b) && b.get("type") === "tool_use") {
|
|
814
|
+
parts.push({
|
|
815
|
+
t: "tool_call",
|
|
816
|
+
name: str(b.get("name")) ?? "",
|
|
817
|
+
args: canonicalJson(b.has("input") ? b.get("input") : null),
|
|
818
|
+
ref: ids2.length
|
|
819
|
+
});
|
|
820
|
+
ids2.push(str(b.get("id")));
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
const p = block(b, true);
|
|
824
|
+
if (p) parts.push(p);
|
|
825
|
+
}
|
|
826
|
+
} else if (content !== void 0 && content !== null) {
|
|
827
|
+
parts.push(opaque(content, "content"));
|
|
828
|
+
}
|
|
829
|
+
refs.assistant(ids2);
|
|
830
|
+
return { role, parts };
|
|
831
|
+
}
|
|
832
|
+
function turn(m, refs) {
|
|
833
|
+
if (!isObject(m)) return [{ role: "", parts: [opaque(m, "message")] }];
|
|
834
|
+
const role = str(m.get("role")) ?? "";
|
|
835
|
+
const content = m.get("content");
|
|
836
|
+
if (role === "assistant") return [assistantMessage(content, refs)];
|
|
837
|
+
if (!Array.isArray(content)) {
|
|
838
|
+
const parts = [];
|
|
839
|
+
if (typeof content === "string") {
|
|
840
|
+
const p = textPart(content);
|
|
841
|
+
if (p) parts.push(p);
|
|
842
|
+
} else if (content !== void 0 && content !== null) parts.push(opaque(content, "content"));
|
|
843
|
+
return [message(role, parts)];
|
|
844
|
+
}
|
|
845
|
+
const out = [];
|
|
846
|
+
let run = null;
|
|
847
|
+
for (const b of content) {
|
|
848
|
+
if (isObject(b) && b.get("type") === "tool_result") {
|
|
849
|
+
if (run !== null) out.push(message(role, run));
|
|
850
|
+
run = null;
|
|
851
|
+
const r = b.get("is_error") === true ? { t: "tool_result", ref: refs.resolve(b.get("tool_use_id")), error: true, parts: resultParts(b.get("content")) } : { t: "tool_result", ref: refs.resolve(b.get("tool_use_id")), parts: resultParts(b.get("content")) };
|
|
852
|
+
out.push({ role: "tool", parts: [r] });
|
|
853
|
+
continue;
|
|
854
|
+
}
|
|
855
|
+
run ?? (run = []);
|
|
856
|
+
const p = block(b, false);
|
|
857
|
+
if (p) run.push(p);
|
|
858
|
+
}
|
|
859
|
+
if (run !== null || out.length === 0) out.push(message(role, run ?? []));
|
|
860
|
+
return out;
|
|
861
|
+
}
|
|
862
|
+
function anthropicTool(t) {
|
|
863
|
+
const type = get(t, "type");
|
|
864
|
+
if (isObject(t) && (type === void 0 || type === null || type === "custom") && typeof t.get("name") === "string") {
|
|
865
|
+
return functionTool(t.get("name"), t.get("description"), t.get("input_schema"));
|
|
866
|
+
}
|
|
867
|
+
return otherTool(withoutCacheControl(t));
|
|
868
|
+
}
|
|
869
|
+
function systemMessage(s) {
|
|
870
|
+
if (s === void 0 || s === null) return null;
|
|
871
|
+
if (typeof s === "string") return s === "" ? null : { role: "system", parts: [{ t: "text", v: s }] };
|
|
872
|
+
if (Array.isArray(s)) {
|
|
873
|
+
const parts = [];
|
|
874
|
+
for (const b of s) {
|
|
875
|
+
const p = block(b, false);
|
|
876
|
+
if (p) parts.push(p);
|
|
877
|
+
}
|
|
878
|
+
return s.length === 0 ? null : { role: "system", parts };
|
|
879
|
+
}
|
|
880
|
+
return { role: "system", parts: [opaque(s, "content")] };
|
|
881
|
+
}
|
|
882
|
+
function canonicalize(body) {
|
|
883
|
+
if (!isObject(body)) return null;
|
|
884
|
+
const msgs = body.get("messages");
|
|
885
|
+
if (!Array.isArray(msgs)) return null;
|
|
886
|
+
const refs = new RefTracker();
|
|
887
|
+
const all = [];
|
|
888
|
+
const sys = systemMessage(body.get("system"));
|
|
889
|
+
if (sys) all.push(sys);
|
|
890
|
+
for (const m of msgs) all.push(...turn(m, refs));
|
|
891
|
+
const tools = [];
|
|
892
|
+
const ts = body.get("tools");
|
|
893
|
+
if (Array.isArray(ts)) for (const t of ts) tools.push(anthropicTool(t));
|
|
894
|
+
const { head, body: rest } = splitHead(all);
|
|
895
|
+
return { head, tools, messages: rest, stateful: false };
|
|
896
|
+
}
|
|
897
|
+
function reply(resp) {
|
|
898
|
+
if (!isObject(resp)) return null;
|
|
899
|
+
return assistantMessage(resp.get("content"), new RefTracker(), str(resp.get("role")) ?? "assistant");
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// src/dialects/openai-chat.ts
|
|
903
|
+
var openai_chat_exports = {};
|
|
904
|
+
__export(openai_chat_exports, {
|
|
905
|
+
canonicalize: () => canonicalize2,
|
|
906
|
+
contentParts: () => contentParts,
|
|
907
|
+
reply: () => reply2
|
|
908
|
+
});
|
|
909
|
+
function partType(p) {
|
|
910
|
+
return str(get(p, "type")) ?? "";
|
|
911
|
+
}
|
|
912
|
+
function fileData(data) {
|
|
913
|
+
const d = parseDataUrl(data);
|
|
914
|
+
if (d !== null) return mediaBytes(d.data, d.mime);
|
|
915
|
+
const b = fromBase64Strict(data);
|
|
916
|
+
return b === null ? null : mediaBytes(b, "");
|
|
917
|
+
}
|
|
918
|
+
function contentPart(p) {
|
|
919
|
+
if (typeof p === "string") return textPart(p);
|
|
920
|
+
if (!isObject(p)) return opaque(p, "part");
|
|
921
|
+
const t = partType(p);
|
|
922
|
+
if (!p.has("type") && typeof p.get("text") === "string") return textPart(p.get("text"));
|
|
923
|
+
if (t === "text" || t === "input_text" || t === "output_text") {
|
|
924
|
+
return textPart(str(get(p, "text")) ?? "");
|
|
925
|
+
} else if (t === "input_image") {
|
|
926
|
+
const url = str(get(p, "image_url"));
|
|
927
|
+
const id = str(get(p, "file_id"));
|
|
928
|
+
if (url !== void 0) return mediaFromUrl(url);
|
|
929
|
+
if (id !== void 0) return mediaRef(id, "file_id");
|
|
930
|
+
} else if (t === "image_url") {
|
|
931
|
+
const iu = get(p, "image_url");
|
|
932
|
+
return mediaFromUrl((typeof iu === "string" ? iu : str(get(iu, "url"))) ?? "");
|
|
933
|
+
} else if (t === "input_audio") {
|
|
934
|
+
const a = get(p, "input_audio");
|
|
935
|
+
const data = str(get(a, "data"));
|
|
936
|
+
const format = str(get(a, "format"));
|
|
937
|
+
if (data !== void 0) {
|
|
938
|
+
const b = fromBase64Strict(data);
|
|
939
|
+
if (b !== null) return mediaBytes(b, format ? `audio/${format}` : "");
|
|
940
|
+
}
|
|
941
|
+
} else if (t === "file") {
|
|
942
|
+
const f = get(p, "file");
|
|
943
|
+
const data = str(get(f, "file_data"));
|
|
944
|
+
const id = str(get(f, "file_id"));
|
|
945
|
+
if (data !== void 0) {
|
|
946
|
+
const m = fileData(data);
|
|
947
|
+
if (m !== null) return m;
|
|
948
|
+
} else if (id !== void 0) return mediaRef(id, "file_id");
|
|
949
|
+
}
|
|
950
|
+
return opaque(p, t);
|
|
951
|
+
}
|
|
952
|
+
function contentParts(c) {
|
|
953
|
+
const out = [];
|
|
954
|
+
if (c === void 0 || c === null) return out;
|
|
955
|
+
if (typeof c === "string") {
|
|
956
|
+
const p = textPart(c);
|
|
957
|
+
if (p) out.push(p);
|
|
958
|
+
return out;
|
|
959
|
+
}
|
|
960
|
+
if (Array.isArray(c)) {
|
|
961
|
+
for (const x of c) {
|
|
962
|
+
const p = contentPart(x);
|
|
963
|
+
if (p) out.push(p);
|
|
964
|
+
}
|
|
965
|
+
return out;
|
|
966
|
+
}
|
|
967
|
+
out.push(opaque(c, "content"));
|
|
968
|
+
return out;
|
|
969
|
+
}
|
|
970
|
+
function toolCall(tc, index) {
|
|
971
|
+
if (!isObject(tc)) return { part: opaque(tc, "tool_call"), id: void 0 };
|
|
972
|
+
const id = str(tc.get("id"));
|
|
973
|
+
const t = str(tc.get("type")) ?? "function";
|
|
974
|
+
if (t === "function" && isObject(tc.get("function"))) {
|
|
975
|
+
const f = tc.get("function");
|
|
976
|
+
return {
|
|
977
|
+
part: { t: "tool_call", name: str(get(f, "name")) ?? "", ...toolArgs(get(f, "arguments")), ref: index },
|
|
978
|
+
id
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
if (t === "custom" && isObject(tc.get("custom"))) {
|
|
982
|
+
const c = tc.get("custom");
|
|
983
|
+
const input = get(c, "input");
|
|
984
|
+
return {
|
|
985
|
+
part: {
|
|
986
|
+
t: "tool_call",
|
|
987
|
+
name: str(get(c, "name")) ?? "",
|
|
988
|
+
args_raw: typeof input === "string" ? input : "",
|
|
989
|
+
ref: index
|
|
990
|
+
},
|
|
991
|
+
id
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
return { part: opaque(tc, "tool_call"), id };
|
|
995
|
+
}
|
|
996
|
+
function chatMessage(m, refs) {
|
|
997
|
+
if (!isObject(m)) return { role: "", parts: [opaque(m, "message")] };
|
|
998
|
+
const role = str(m.get("role")) ?? "";
|
|
999
|
+
const name = m.get("name");
|
|
1000
|
+
if (role === "tool") {
|
|
1001
|
+
const parts2 = contentParts(m.get("content"));
|
|
1002
|
+
return message(role, [{ t: "tool_result", ref: refs.resolve(m.get("tool_call_id")), parts: parts2 }], name);
|
|
1003
|
+
}
|
|
1004
|
+
if (role === "function") {
|
|
1005
|
+
const parts2 = contentParts(m.get("content"));
|
|
1006
|
+
return message(role, [{ t: "tool_result", ref: -1, parts: parts2 }], name);
|
|
1007
|
+
}
|
|
1008
|
+
const parts = [];
|
|
1009
|
+
if (role === "assistant") {
|
|
1010
|
+
let r = m.get("reasoning_content");
|
|
1011
|
+
if (typeof r !== "string" || r === "") r = m.get("reasoning");
|
|
1012
|
+
if (typeof r === "string" && r !== "") parts.push({ t: "reasoning", v: r });
|
|
1013
|
+
}
|
|
1014
|
+
parts.push(...contentParts(m.get("content")));
|
|
1015
|
+
if (role === "assistant") {
|
|
1016
|
+
const refusal = m.get("refusal");
|
|
1017
|
+
if (refusal !== void 0 && refusal !== null) {
|
|
1018
|
+
parts.push(opaque(/* @__PURE__ */ new Map([["refusal", refusal]]), "refusal"));
|
|
1019
|
+
}
|
|
1020
|
+
const audio = m.get("audio");
|
|
1021
|
+
if (audio !== void 0 && audio !== null) parts.push(opaque(audio, "audio"));
|
|
1022
|
+
const ids2 = [];
|
|
1023
|
+
const calls = m.get("tool_calls");
|
|
1024
|
+
if (Array.isArray(calls)) {
|
|
1025
|
+
calls.forEach((tc, i) => {
|
|
1026
|
+
const { part, id } = toolCall(tc, i);
|
|
1027
|
+
parts.push(part);
|
|
1028
|
+
ids2.push(id);
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
const fc = m.get("function_call");
|
|
1032
|
+
if (isObject(fc)) {
|
|
1033
|
+
parts.push({
|
|
1034
|
+
t: "tool_call",
|
|
1035
|
+
name: str(fc.get("name")) ?? "",
|
|
1036
|
+
...toolArgs(fc.get("arguments")),
|
|
1037
|
+
ref: ids2.length
|
|
1038
|
+
});
|
|
1039
|
+
ids2.push(void 0);
|
|
1040
|
+
} else if (fc !== void 0 && fc !== null) {
|
|
1041
|
+
parts.push(opaque(fc, "function_call"));
|
|
1042
|
+
}
|
|
1043
|
+
refs.assistant(ids2);
|
|
1044
|
+
}
|
|
1045
|
+
return message(role, parts, name);
|
|
1046
|
+
}
|
|
1047
|
+
function chatTool(t) {
|
|
1048
|
+
if (isObject(t) && t.get("type") === "function" && isObject(t.get("function"))) {
|
|
1049
|
+
const f = t.get("function");
|
|
1050
|
+
return functionTool(get(f, "name"), get(f, "description"), get(f, "parameters"));
|
|
1051
|
+
}
|
|
1052
|
+
return otherTool(t);
|
|
1053
|
+
}
|
|
1054
|
+
function canonicalize2(body) {
|
|
1055
|
+
if (!isObject(body)) return null;
|
|
1056
|
+
const msgs = body.get("messages");
|
|
1057
|
+
if (!Array.isArray(msgs)) return null;
|
|
1058
|
+
const refs = new RefTracker();
|
|
1059
|
+
const all = msgs.map((m) => chatMessage(m, refs));
|
|
1060
|
+
const tools = [];
|
|
1061
|
+
const ts = body.get("tools");
|
|
1062
|
+
if (Array.isArray(ts)) for (const t of ts) tools.push(chatTool(t));
|
|
1063
|
+
const fs = body.get("functions");
|
|
1064
|
+
if (!Array.isArray(ts) && Array.isArray(fs)) {
|
|
1065
|
+
for (const f of fs) tools.push(chatTool(/* @__PURE__ */ new Map([["type", "function"], ["function", f]])));
|
|
1066
|
+
}
|
|
1067
|
+
const { head, body: rest } = splitHead(all);
|
|
1068
|
+
return { head, tools, messages: rest, stateful: false };
|
|
1069
|
+
}
|
|
1070
|
+
function reply2(msg) {
|
|
1071
|
+
if (!isObject(msg)) return null;
|
|
1072
|
+
return chatMessage(msg, new RefTracker());
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
// src/dialects/openai-responses.ts
|
|
1076
|
+
var openai_responses_exports = {};
|
|
1077
|
+
__export(openai_responses_exports, {
|
|
1078
|
+
canonicalize: () => canonicalize3,
|
|
1079
|
+
reply: () => reply3
|
|
1080
|
+
});
|
|
1081
|
+
var TEXT_TYPES = /* @__PURE__ */ new Set(["input_text", "output_text", "text"]);
|
|
1082
|
+
var ASSISTANT_SIDE = /* @__PURE__ */ new Set(["reasoning", "function_call", "custom_tool_call"]);
|
|
1083
|
+
var SERVER_ITEMS = /* @__PURE__ */ new Set([
|
|
1084
|
+
"web_search_call",
|
|
1085
|
+
"file_search_call",
|
|
1086
|
+
"computer_call",
|
|
1087
|
+
"code_interpreter_call",
|
|
1088
|
+
"image_generation_call",
|
|
1089
|
+
"local_shell_call",
|
|
1090
|
+
"mcp_call",
|
|
1091
|
+
"mcp_list_tools",
|
|
1092
|
+
"mcp_approval_request"
|
|
1093
|
+
]);
|
|
1094
|
+
var OUTPUTS = /* @__PURE__ */ new Set(["function_call_output", "custom_tool_call_output"]);
|
|
1095
|
+
function fileData2(data) {
|
|
1096
|
+
const d = parseDataUrl(data);
|
|
1097
|
+
if (d !== null) return mediaBytes(d.data, d.mime);
|
|
1098
|
+
const b = fromBase64Strict(data);
|
|
1099
|
+
return b === null ? null : mediaBytes(b, "");
|
|
1100
|
+
}
|
|
1101
|
+
function contentPart2(p) {
|
|
1102
|
+
if (typeof p === "string") return textPart(p);
|
|
1103
|
+
if (!isObject(p)) return opaque(p, "part");
|
|
1104
|
+
const t = str(p.get("type")) ?? "";
|
|
1105
|
+
if (TEXT_TYPES.has(t)) {
|
|
1106
|
+
return textPart(str(p.get("text")) ?? "");
|
|
1107
|
+
} else if (t === "input_image") {
|
|
1108
|
+
const url = str(p.get("image_url"));
|
|
1109
|
+
const id = str(p.get("file_id"));
|
|
1110
|
+
if (url !== void 0) return mediaFromUrl(url);
|
|
1111
|
+
if (id !== void 0) return mediaRef(id, "file_id");
|
|
1112
|
+
} else if (t === "input_file") {
|
|
1113
|
+
const data = str(p.get("file_data"));
|
|
1114
|
+
const id = str(p.get("file_id"));
|
|
1115
|
+
const url = str(p.get("file_url"));
|
|
1116
|
+
if (data !== void 0) {
|
|
1117
|
+
const m = fileData2(data);
|
|
1118
|
+
if (m !== null) return m;
|
|
1119
|
+
} else if (id !== void 0) return mediaRef(id, "file_id");
|
|
1120
|
+
else if (url !== void 0) return mediaFromUrl(url);
|
|
1121
|
+
} else if (t === "refusal") {
|
|
1122
|
+
return opaque(/* @__PURE__ */ new Map([["refusal", p.get("refusal") ?? null]]), "refusal");
|
|
1123
|
+
}
|
|
1124
|
+
return opaque(p, t);
|
|
1125
|
+
}
|
|
1126
|
+
function contentParts2(c) {
|
|
1127
|
+
const out = [];
|
|
1128
|
+
if (c === void 0 || c === null) return out;
|
|
1129
|
+
if (typeof c === "string") {
|
|
1130
|
+
const p = textPart(c);
|
|
1131
|
+
if (p) out.push(p);
|
|
1132
|
+
return out;
|
|
1133
|
+
}
|
|
1134
|
+
if (Array.isArray(c)) {
|
|
1135
|
+
for (const x of c) {
|
|
1136
|
+
const p = contentPart2(x);
|
|
1137
|
+
if (p) out.push(p);
|
|
1138
|
+
}
|
|
1139
|
+
return out;
|
|
1140
|
+
}
|
|
1141
|
+
out.push(opaque(c, "content"));
|
|
1142
|
+
return out;
|
|
1143
|
+
}
|
|
1144
|
+
function itemType(it) {
|
|
1145
|
+
if (!isObject(it)) return void 0;
|
|
1146
|
+
const t = it.get("type");
|
|
1147
|
+
if (t === void 0) return "message";
|
|
1148
|
+
return typeof t === "string" ? t : "";
|
|
1149
|
+
}
|
|
1150
|
+
function isAssistantSide(it) {
|
|
1151
|
+
const t = itemType(it);
|
|
1152
|
+
if (t === void 0) return false;
|
|
1153
|
+
if (t === "message") return get(it, "role") === "assistant";
|
|
1154
|
+
return ASSISTANT_SIDE.has(t) || SERVER_ITEMS.has(t);
|
|
1155
|
+
}
|
|
1156
|
+
function reasoningPart(it) {
|
|
1157
|
+
const texts = (list, type) => Array.isArray(list) ? list.flatMap((p) => {
|
|
1158
|
+
const v = get(p, "text");
|
|
1159
|
+
return get(p, "type") === type && typeof v === "string" && v !== "" ? [v] : [];
|
|
1160
|
+
}) : [];
|
|
1161
|
+
let t = texts(get(it, "content"), "reasoning_text");
|
|
1162
|
+
if (t.length === 0) t = texts(get(it, "summary"), "summary_text");
|
|
1163
|
+
if (t.length > 0) return { t: "reasoning", v: t.join("\n\n") };
|
|
1164
|
+
const enc = get(it, "encrypted_content");
|
|
1165
|
+
if (truthy(enc)) {
|
|
1166
|
+
return opaque(/* @__PURE__ */ new Map([["encrypted_content", enc]]), "reasoning");
|
|
1167
|
+
}
|
|
1168
|
+
return null;
|
|
1169
|
+
}
|
|
1170
|
+
function assistantGroup(items2, refs) {
|
|
1171
|
+
const parts = [];
|
|
1172
|
+
const ids2 = [];
|
|
1173
|
+
let opened = false;
|
|
1174
|
+
for (const it of items2) {
|
|
1175
|
+
const t = itemType(it);
|
|
1176
|
+
if (t === "reasoning") {
|
|
1177
|
+
const p = reasoningPart(it);
|
|
1178
|
+
if (p) parts.push(p);
|
|
1179
|
+
opened || (opened = p !== null);
|
|
1180
|
+
continue;
|
|
1181
|
+
}
|
|
1182
|
+
opened = true;
|
|
1183
|
+
if (t === "message") {
|
|
1184
|
+
parts.push(...contentParts2(get(it, "content")));
|
|
1185
|
+
} else if (t === "function_call" || t === "custom_tool_call") {
|
|
1186
|
+
const name = str(get(it, "name")) ?? "";
|
|
1187
|
+
const ref = ids2.length;
|
|
1188
|
+
if (t === "function_call") {
|
|
1189
|
+
parts.push({ t: "tool_call", name, ...toolArgs(get(it, "arguments")), ref });
|
|
1190
|
+
} else {
|
|
1191
|
+
const input = get(it, "input");
|
|
1192
|
+
parts.push({ t: "tool_call", name, args_raw: typeof input === "string" ? input : "", ref });
|
|
1193
|
+
}
|
|
1194
|
+
ids2.push(str(get(it, "call_id")));
|
|
1195
|
+
} else {
|
|
1196
|
+
parts.push(opaque(it, t));
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
if (!opened) return null;
|
|
1200
|
+
refs.assistant(ids2);
|
|
1201
|
+
return { role: "assistant", parts };
|
|
1202
|
+
}
|
|
1203
|
+
function items(input, refs) {
|
|
1204
|
+
const out = [];
|
|
1205
|
+
let i = 0;
|
|
1206
|
+
while (i < input.length) {
|
|
1207
|
+
const it = input[i];
|
|
1208
|
+
if (isAssistantSide(it)) {
|
|
1209
|
+
let j = i;
|
|
1210
|
+
while (j < input.length && isAssistantSide(input[j])) j++;
|
|
1211
|
+
const m = assistantGroup(input.slice(i, j), refs);
|
|
1212
|
+
if (m) out.push(m);
|
|
1213
|
+
i = j;
|
|
1214
|
+
continue;
|
|
1215
|
+
}
|
|
1216
|
+
i++;
|
|
1217
|
+
const t = itemType(it);
|
|
1218
|
+
if (t === void 0) {
|
|
1219
|
+
out.push({ role: "", parts: [opaque(it, "item")] });
|
|
1220
|
+
} else if (t === "message") {
|
|
1221
|
+
out.push({ role: str(get(it, "role")) ?? "", parts: contentParts2(get(it, "content")) });
|
|
1222
|
+
} else if (OUTPUTS.has(t)) {
|
|
1223
|
+
out.push({
|
|
1224
|
+
role: "tool",
|
|
1225
|
+
parts: [{ t: "tool_result", ref: refs.resolve(get(it, "call_id")), parts: contentParts2(get(it, "output")) }]
|
|
1226
|
+
});
|
|
1227
|
+
} else {
|
|
1228
|
+
out.push({ role: "", parts: [opaque(it, t)] });
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
return out;
|
|
1232
|
+
}
|
|
1233
|
+
function responsesTool(t) {
|
|
1234
|
+
if (isObject(t) && t.get("type") === "function") {
|
|
1235
|
+
return functionTool(t.get("name"), t.get("description"), t.get("parameters"));
|
|
1236
|
+
}
|
|
1237
|
+
return otherTool(t);
|
|
1238
|
+
}
|
|
1239
|
+
function canonicalize3(body) {
|
|
1240
|
+
if (!isObject(body)) return null;
|
|
1241
|
+
const input = body.get("input");
|
|
1242
|
+
let list;
|
|
1243
|
+
if (typeof input === "string") list = [/* @__PURE__ */ new Map([["role", "user"], ["content", input]])];
|
|
1244
|
+
else if (Array.isArray(input)) list = input;
|
|
1245
|
+
else return null;
|
|
1246
|
+
const refs = new RefTracker();
|
|
1247
|
+
const msgs = items(list, refs);
|
|
1248
|
+
const head = [];
|
|
1249
|
+
const ins = body.get("instructions");
|
|
1250
|
+
if (typeof ins === "string") {
|
|
1251
|
+
if (ins !== "") head.push({ role: "system", parts: [{ t: "text", v: ins }] });
|
|
1252
|
+
} else if (ins !== void 0 && ins !== null) {
|
|
1253
|
+
head.push({ role: "system", parts: [opaque(ins, "instructions")] });
|
|
1254
|
+
}
|
|
1255
|
+
const split = splitHead(msgs);
|
|
1256
|
+
head.push(...split.head);
|
|
1257
|
+
const tools = [];
|
|
1258
|
+
const ts = body.get("tools");
|
|
1259
|
+
if (Array.isArray(ts)) for (const t of ts) tools.push(responsesTool(t));
|
|
1260
|
+
const prev = body.get("previous_response_id");
|
|
1261
|
+
const conv = body.get("conversation");
|
|
1262
|
+
const stateful = truthy(prev) || truthy(conv);
|
|
1263
|
+
return { head, tools, messages: split.body, stateful };
|
|
1264
|
+
}
|
|
1265
|
+
function reply3(resp) {
|
|
1266
|
+
if (!isObject(resp)) return null;
|
|
1267
|
+
const o = resp.get("output");
|
|
1268
|
+
const out = Array.isArray(o) ? o : [];
|
|
1269
|
+
return assistantGroup(out.filter(isAssistantSide), new RefTracker()) ?? { role: "assistant", parts: [] };
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
// src/loose.ts
|
|
1273
|
+
var WHITESPACE_V1 = /* @__PURE__ */ new Set([
|
|
1274
|
+
9,
|
|
1275
|
+
10,
|
|
1276
|
+
11,
|
|
1277
|
+
12,
|
|
1278
|
+
13,
|
|
1279
|
+
28,
|
|
1280
|
+
29,
|
|
1281
|
+
30,
|
|
1282
|
+
31,
|
|
1283
|
+
32,
|
|
1284
|
+
133,
|
|
1285
|
+
160,
|
|
1286
|
+
5760,
|
|
1287
|
+
8192,
|
|
1288
|
+
8193,
|
|
1289
|
+
8194,
|
|
1290
|
+
8195,
|
|
1291
|
+
8196,
|
|
1292
|
+
8197,
|
|
1293
|
+
8198,
|
|
1294
|
+
8199,
|
|
1295
|
+
8200,
|
|
1296
|
+
8201,
|
|
1297
|
+
8202,
|
|
1298
|
+
8232,
|
|
1299
|
+
8233,
|
|
1300
|
+
8239,
|
|
1301
|
+
8287,
|
|
1302
|
+
12288
|
|
1303
|
+
]);
|
|
1304
|
+
var BLOCK_TAGS_V1 = [
|
|
1305
|
+
["<system-reminder>", "</system-reminder>"],
|
|
1306
|
+
["<environment_context>", "</environment_context>"]
|
|
1307
|
+
];
|
|
1308
|
+
var LOOSE_V1 = Object.freeze({
|
|
1309
|
+
version: "loose/1",
|
|
1310
|
+
blockTags: BLOCK_TAGS_V1,
|
|
1311
|
+
whitespace: WHITESPACE_V1,
|
|
1312
|
+
roleMap: []
|
|
1313
|
+
});
|
|
1314
|
+
var LOOSE_V2 = Object.freeze({
|
|
1315
|
+
version: "loose/2",
|
|
1316
|
+
blockTags: BLOCK_TAGS_V1,
|
|
1317
|
+
whitespace: WHITESPACE_V1,
|
|
1318
|
+
roleMap: [["developer", "system"]]
|
|
1319
|
+
});
|
|
1320
|
+
function validateProfile(p) {
|
|
1321
|
+
if (!p.version || p.version.includes("\0") || p.version.includes(":")) {
|
|
1322
|
+
throw new TypeError("version must be a non-empty label without NUL or ':'");
|
|
1323
|
+
}
|
|
1324
|
+
for (const [o, c] of p.blockTags) {
|
|
1325
|
+
if (typeof o !== "string" || typeof c !== "string" || !o || !c) {
|
|
1326
|
+
throw new TypeError("blockTags must be (open, close) pairs of non-empty strings");
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
if (p.whitespace.size === 0) throw new TypeError("whitespace must not be empty");
|
|
1330
|
+
for (const c of p.whitespace) {
|
|
1331
|
+
if (!Number.isInteger(c) || c < 0 || c > 1114111 || c >= 55296 && c <= 57343) {
|
|
1332
|
+
throw new TypeError("whitespace must hold Unicode scalar values");
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
const from = /* @__PURE__ */ new Set();
|
|
1336
|
+
for (const [f, t] of p.roleMap) {
|
|
1337
|
+
if (!f || !t) throw new TypeError("roleMap must be (from, to) pairs of non-empty strings");
|
|
1338
|
+
if (from.has(f)) throw new TypeError("roleMap must map each role at most once");
|
|
1339
|
+
from.add(f);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
function profileFingerprint(p) {
|
|
1343
|
+
const table = {
|
|
1344
|
+
version: p.version,
|
|
1345
|
+
block_tags: p.blockTags.map((t) => [t[0], t[1]]),
|
|
1346
|
+
whitespace: [...p.whitespace].sort((a, b) => a - b)
|
|
1347
|
+
};
|
|
1348
|
+
if (p.roleMap.length > 0) table.role_map = p.roleMap.map((t) => [t[0], t[1]]);
|
|
1349
|
+
return sha256(encode(table)).slice(0, 8);
|
|
1350
|
+
}
|
|
1351
|
+
function asciiLower(s) {
|
|
1352
|
+
return s.replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32));
|
|
1353
|
+
}
|
|
1354
|
+
function stripBlocks(v, open, close) {
|
|
1355
|
+
const low = asciiLower(v);
|
|
1356
|
+
const o = asciiLower(open);
|
|
1357
|
+
const c = asciiLower(close);
|
|
1358
|
+
let out = "";
|
|
1359
|
+
let pos = 0;
|
|
1360
|
+
for (; ; ) {
|
|
1361
|
+
const a = low.indexOf(o, pos);
|
|
1362
|
+
if (a < 0) break;
|
|
1363
|
+
const b = low.indexOf(c, a + o.length);
|
|
1364
|
+
if (b < 0) break;
|
|
1365
|
+
out += v.slice(pos, a);
|
|
1366
|
+
pos = b + c.length;
|
|
1367
|
+
}
|
|
1368
|
+
return pos === 0 ? v : out + v.slice(pos);
|
|
1369
|
+
}
|
|
1370
|
+
var DATE_LINE = /^[ \t]*[0-9]{4}-[0-9]{2}-[0-9]{2}(?:[T ][0-9]{2}:[0-9]{2}(?::[0-9]{2}(?:\.[0-9]+)?)?(?:Z|[+-][0-9]{2}:?[0-9]{2})?)?[ \t]*$/;
|
|
1371
|
+
var DATE_HINT = /(?:^|\n)[ \t]*[0-9]{4}-[0-9]{2}-[0-9]{2}/;
|
|
1372
|
+
function collapse(v, ws) {
|
|
1373
|
+
const words = [];
|
|
1374
|
+
let cur = "";
|
|
1375
|
+
for (const ch of v) {
|
|
1376
|
+
if (ws.has(ch.codePointAt(0))) {
|
|
1377
|
+
if (cur) words.push(cur);
|
|
1378
|
+
cur = "";
|
|
1379
|
+
} else cur += ch;
|
|
1380
|
+
}
|
|
1381
|
+
if (cur) words.push(cur);
|
|
1382
|
+
return words.join(" ");
|
|
1383
|
+
}
|
|
1384
|
+
function looseText(v, profile = LOOSE_V1) {
|
|
1385
|
+
if (v.includes("<")) {
|
|
1386
|
+
for (const [o, c] of profile.blockTags) v = stripBlocks(v, o, c);
|
|
1387
|
+
}
|
|
1388
|
+
if (DATE_HINT.test(v)) {
|
|
1389
|
+
v = v.split("\n").map((line) => DATE_LINE.test(line) ? "" : line).join("\n");
|
|
1390
|
+
}
|
|
1391
|
+
return collapse(v, profile.whitespace);
|
|
1392
|
+
}
|
|
1393
|
+
function resort(text) {
|
|
1394
|
+
return canonicalJson(parseJson(text, MAX_DEPTH), true);
|
|
1395
|
+
}
|
|
1396
|
+
function looseParts(parts, p) {
|
|
1397
|
+
const out = [];
|
|
1398
|
+
for (const part of parts) {
|
|
1399
|
+
if (part.t === "reasoning") continue;
|
|
1400
|
+
if (part.t === "text") {
|
|
1401
|
+
const v = looseText(part.v, p);
|
|
1402
|
+
if (v) out.push({ t: "text", v });
|
|
1403
|
+
} else if (part.t === "tool_result") {
|
|
1404
|
+
out.push({ ...part, parts: looseParts(part.parts, p) });
|
|
1405
|
+
} else if (part.t === "tool_call" && part.args !== void 0) {
|
|
1406
|
+
out.push({ ...part, args: resort(part.args) });
|
|
1407
|
+
} else {
|
|
1408
|
+
out.push(part);
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
return out;
|
|
1412
|
+
}
|
|
1413
|
+
function looseMessage(m, profile = LOOSE_V1) {
|
|
1414
|
+
const out = { ...m, parts: looseParts(m.parts, profile) };
|
|
1415
|
+
for (const [from, to] of profile.roleMap) {
|
|
1416
|
+
if (m.role === from) {
|
|
1417
|
+
out.role = to;
|
|
1418
|
+
break;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
return out;
|
|
1422
|
+
}
|
|
1423
|
+
function looseTool(t) {
|
|
1424
|
+
return "schema" in t ? { ...t, schema: resort(t.schema) } : { ...t, def: resort(t.def) };
|
|
1425
|
+
}
|
|
1426
|
+
function loose(canon, profile = LOOSE_V1) {
|
|
1427
|
+
return {
|
|
1428
|
+
head: canon.head.map((m) => looseMessage(m, profile)),
|
|
1429
|
+
tools: canon.tools.map(looseTool),
|
|
1430
|
+
messages: canon.messages.map((m) => looseMessage(m, profile)),
|
|
1431
|
+
stateful: canon.stateful
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
// src/ids.ts
|
|
1436
|
+
var SPEC_LABEL = "kvsig/1";
|
|
1437
|
+
var KEY_BYTES = 32;
|
|
1438
|
+
var _bytes;
|
|
1439
|
+
var _Key = class _Key {
|
|
1440
|
+
constructor(epoch, bytes) {
|
|
1441
|
+
__privateAdd(this, _bytes);
|
|
1442
|
+
if (!Number.isInteger(epoch) || epoch < 0 || epoch > 255) {
|
|
1443
|
+
throw new RangeError("epoch must be an integer from 0 to 255");
|
|
1444
|
+
}
|
|
1445
|
+
if (!(bytes instanceof Uint8Array) || bytes.length !== KEY_BYTES) {
|
|
1446
|
+
throw new RangeError(`a key is ${KEY_BYTES} bytes`);
|
|
1447
|
+
}
|
|
1448
|
+
this.epoch = epoch;
|
|
1449
|
+
__privateSet(this, _bytes, Uint8Array.from(bytes));
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* A new random key from Web Crypto `getRandomValues`. Epoch 0 is reserved for
|
|
1453
|
+
* test keys. On Node 18, where `globalThis.crypto` is not defined by default,
|
|
1454
|
+
* use `new Key(epoch, crypto.randomBytes(32))` from `node:crypto` instead.
|
|
1455
|
+
*/
|
|
1456
|
+
static generate(epoch) {
|
|
1457
|
+
if (epoch === 0) throw new RangeError("epoch 0 is reserved for published test keys");
|
|
1458
|
+
const c = globalThis.crypto;
|
|
1459
|
+
if (typeof c?.getRandomValues !== "function") {
|
|
1460
|
+
throw new Error("Key.generate needs Web Crypto (globalThis.crypto); pass 32 random bytes to new Key() instead");
|
|
1461
|
+
}
|
|
1462
|
+
const b = new Uint8Array(KEY_BYTES);
|
|
1463
|
+
c.getRandomValues(b);
|
|
1464
|
+
return new _Key(epoch, b);
|
|
1465
|
+
}
|
|
1466
|
+
/** The secret bytes (a copy). */
|
|
1467
|
+
bytes() {
|
|
1468
|
+
return Uint8Array.from(__privateGet(this, _bytes));
|
|
1469
|
+
}
|
|
1470
|
+
/** @internal */
|
|
1471
|
+
mac(...parts) {
|
|
1472
|
+
return hmacSha256(__privateGet(this, _bytes), ...parts);
|
|
1473
|
+
}
|
|
1474
|
+
toString() {
|
|
1475
|
+
return `Key(epoch=${this.epoch}, <secret>)`;
|
|
1476
|
+
}
|
|
1477
|
+
toJSON() {
|
|
1478
|
+
return this.toString();
|
|
1479
|
+
}
|
|
1480
|
+
};
|
|
1481
|
+
_bytes = new WeakMap();
|
|
1482
|
+
var Key = _Key;
|
|
1483
|
+
var TEST_KEY = new Key(0, Uint8Array.from({ length: 32 }, (_, i) => i));
|
|
1484
|
+
var KeyRing = class {
|
|
1485
|
+
constructor(current, accepted = []) {
|
|
1486
|
+
const epochs = /* @__PURE__ */ new Set([current.epoch]);
|
|
1487
|
+
for (const k of accepted) {
|
|
1488
|
+
if (epochs.has(k.epoch)) throw new RangeError("every key in a ring needs its own epoch");
|
|
1489
|
+
epochs.add(k.epoch);
|
|
1490
|
+
}
|
|
1491
|
+
this.current = current;
|
|
1492
|
+
this.accepted = [...accepted];
|
|
1493
|
+
}
|
|
1494
|
+
keys() {
|
|
1495
|
+
return [this.current, ...this.accepted];
|
|
1496
|
+
}
|
|
1497
|
+
};
|
|
1498
|
+
function scopeFrom(opts = {}) {
|
|
1499
|
+
const m = {};
|
|
1500
|
+
if (opts.cacheSalt) m.cache_salt = opts.cacheSalt;
|
|
1501
|
+
if (opts.tenant && opts.tenant.length > 0) m.tenant = opts.tenant;
|
|
1502
|
+
if (Object.keys(m).length === 0) return new Uint8Array(0);
|
|
1503
|
+
return sha256(encode(m));
|
|
1504
|
+
}
|
|
1505
|
+
function hasher(key, label, scope) {
|
|
1506
|
+
if (scope.length > 255) throw new RangeError("scope is at most 255 bytes");
|
|
1507
|
+
const prefix = concatBytes([utf8(SPEC_LABEL), Uint8Array.of(0), utf8(label), Uint8Array.of(0, scope.length), scope]);
|
|
1508
|
+
const epoch = Uint8Array.of(key.epoch);
|
|
1509
|
+
return (payload) => concatBytes([epoch, key.mac(prefix, payload).subarray(0, 16)]);
|
|
1510
|
+
}
|
|
1511
|
+
function family(key, scope, prefix, c) {
|
|
1512
|
+
const h = (label) => hasher(key, prefix + label, scope);
|
|
1513
|
+
const head = h("head")(encode(asCbor({ head: c.head, tools: c.tools })));
|
|
1514
|
+
const sys = h("sys")(encode(asCbor({ head: c.head })));
|
|
1515
|
+
const tools = h("tools")(encode(asCbor({ tools: c.tools })));
|
|
1516
|
+
const msgH = h("msg");
|
|
1517
|
+
const msgs = c.messages.map((m) => msgH(encode(asCbor(m))));
|
|
1518
|
+
const nodes = [];
|
|
1519
|
+
if (!c.stateful) {
|
|
1520
|
+
const nodeH = h("node");
|
|
1521
|
+
let prev = head;
|
|
1522
|
+
for (const m of msgs) {
|
|
1523
|
+
prev = nodeH(concatBytes([prev, m]));
|
|
1524
|
+
nodes.push(prev);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
return { head, sys, tools, msgs, nodes, msgH };
|
|
1528
|
+
}
|
|
1529
|
+
function ids(canon, key, opts = {}) {
|
|
1530
|
+
const scope = opts.scope ?? new Uint8Array(0);
|
|
1531
|
+
const profile = opts.profile ?? LOOSE_V1;
|
|
1532
|
+
const exact = family(key, scope, "", canon);
|
|
1533
|
+
const lc = loose(canon, profile);
|
|
1534
|
+
const lp = `${profile.version}:`;
|
|
1535
|
+
const lf = family(key, scope, lp, lc);
|
|
1536
|
+
let conv = null;
|
|
1537
|
+
if (!canon.stateful) {
|
|
1538
|
+
const a = lc.messages.findIndex((m) => m.parts.length > 0);
|
|
1539
|
+
if (a >= 0) {
|
|
1540
|
+
conv = toHex(hasher(key, lp + "conv", scope)(concatBytes([lf.sys, lf.msgs[a]])));
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
return {
|
|
1544
|
+
head: toHex(exact.head),
|
|
1545
|
+
sys: toHex(exact.sys),
|
|
1546
|
+
tools: toHex(exact.tools),
|
|
1547
|
+
msgs: exact.msgs.map(toHex),
|
|
1548
|
+
nodes: exact.nodes.map(toHex),
|
|
1549
|
+
loose_head: toHex(lf.head),
|
|
1550
|
+
loose_sys: toHex(lf.sys),
|
|
1551
|
+
loose_tools: toHex(lf.tools),
|
|
1552
|
+
loose_msgs: lf.msgs.map(toHex),
|
|
1553
|
+
loose_nodes: lf.nodes.map(toHex),
|
|
1554
|
+
conv,
|
|
1555
|
+
epoch: key.epoch,
|
|
1556
|
+
profile: profile.version
|
|
1557
|
+
};
|
|
1558
|
+
}
|
|
1559
|
+
function idsAllEpochs(canon, ring, opts = {}) {
|
|
1560
|
+
return ring.keys().map((k) => ids(canon, k, opts));
|
|
1561
|
+
}
|
|
1562
|
+
function messageId(m, key, opts = {}) {
|
|
1563
|
+
const scope = opts.scope ?? new Uint8Array(0);
|
|
1564
|
+
if (opts.profile === void 0) return toHex(hasher(key, "msg", scope)(encode(asCbor(m))));
|
|
1565
|
+
const lm = looseMessage(m, opts.profile);
|
|
1566
|
+
return toHex(hasher(key, `${opts.profile.version}:msg`, scope)(encode(asCbor(lm))));
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
// src/index.ts
|
|
1570
|
+
var VERSION = "0.1.0";
|
|
1571
|
+
var SUPPORTED = ["openai.chat", "openai.responses", "anthropic.messages"];
|
|
1572
|
+
var ADAPTERS = {
|
|
1573
|
+
"openai.chat": openai_chat_exports,
|
|
1574
|
+
"openai.responses": openai_responses_exports,
|
|
1575
|
+
"anthropic.messages": anthropic_messages_exports
|
|
1576
|
+
};
|
|
1577
|
+
function adapter(dialect) {
|
|
1578
|
+
const a = ADAPTERS[dialect];
|
|
1579
|
+
if (a === void 0) throw new RangeError(`dialect ${JSON.stringify(dialect)} is not implemented in this release`);
|
|
1580
|
+
return a;
|
|
1581
|
+
}
|
|
1582
|
+
function toJson(body) {
|
|
1583
|
+
if (typeof body === "string" || body instanceof Uint8Array) return parseJson(body);
|
|
1584
|
+
return fromJS(body);
|
|
1585
|
+
}
|
|
1586
|
+
function failOpen(fn, opts) {
|
|
1587
|
+
try {
|
|
1588
|
+
return fn();
|
|
1589
|
+
} catch (e) {
|
|
1590
|
+
if (e instanceof NoIds || e instanceof JsonSyntaxError || e instanceof RangeError) return null;
|
|
1591
|
+
if (opts.onError) opts.onError(e);
|
|
1592
|
+
else console.error("kvsig: unexpected error; returning no ids", e);
|
|
1593
|
+
return null;
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
function canonicalize4(body, dialect = "openai.chat", opts = {}) {
|
|
1597
|
+
const a = adapter(dialect);
|
|
1598
|
+
const max = opts.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
1599
|
+
return failOpen(() => {
|
|
1600
|
+
const c = a.canonicalize(toJson(body));
|
|
1601
|
+
return c === null ? null : finish(c, max);
|
|
1602
|
+
}, opts);
|
|
1603
|
+
}
|
|
1604
|
+
function canonicalizeReply(message2, dialect = "openai.chat", opts = {}) {
|
|
1605
|
+
const a = adapter(dialect);
|
|
1606
|
+
const max = opts.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
1607
|
+
return failOpen(() => {
|
|
1608
|
+
const m = a.reply(toJson(message2));
|
|
1609
|
+
return m !== null && encode(asCbor(m)).length <= max ? m : null;
|
|
1610
|
+
}, opts);
|
|
1611
|
+
}
|
|
1612
|
+
function encode2(v) {
|
|
1613
|
+
return encode(v);
|
|
1614
|
+
}
|
|
1615
|
+
function canonicalBytes(c) {
|
|
1616
|
+
return encode(asCbor({ head: c.head, tools: c.tools, messages: c.messages }));
|
|
1617
|
+
}
|
|
1618
|
+
function conversationId(i) {
|
|
1619
|
+
return i.conv;
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
export { DEFAULT_MAX_BYTES, HEAD_ROLES, KEY_BYTES, Key, KeyRing, LOOSE_V1, LOOSE_V2, SPEC_LABEL, SUPPORTED, TEST_KEY, VERSION, WHITESPACE_V1, canonicalBytes, canonicalJson, canonicalize4 as canonicalize, canonicalizeReply, conversationId, encode2 as encode, hmacSha256, ids, idsAllEpochs, loose, looseMessage, looseText, messageId, parseJson, profileFingerprint, scopeFrom, sha256, validateProfile };
|