anonyma 1.0.0 → 1.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/CHANGELOG.md +273 -144
- package/README.md +427 -143
- package/dist/ai/index.cjs +522 -0
- package/dist/ai/index.cjs.map +1 -0
- package/dist/ai/index.d.cts +322 -0
- package/dist/ai/index.d.ts +322 -0
- package/dist/ai/index.js +505 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/audit/index.cjs +359 -0
- package/dist/audit/index.cjs.map +1 -0
- package/dist/audit/index.d.cts +287 -0
- package/dist/audit/index.d.ts +287 -0
- package/dist/audit/index.js +340 -0
- package/dist/audit/index.js.map +1 -0
- package/dist/chunk-26IHMQHL.cjs +662 -0
- package/dist/chunk-26IHMQHL.cjs.map +1 -0
- package/dist/chunk-2F2GWGTB.js +174 -0
- package/dist/chunk-2F2GWGTB.js.map +1 -0
- package/dist/chunk-2NNMJ55L.cjs +45 -0
- package/dist/chunk-2NNMJ55L.cjs.map +1 -0
- package/dist/chunk-2YG7R4KJ.cjs +114 -0
- package/dist/chunk-2YG7R4KJ.cjs.map +1 -0
- package/dist/chunk-2ZZHLCEJ.js +237 -0
- package/dist/chunk-2ZZHLCEJ.js.map +1 -0
- package/dist/chunk-3CY6ATKS.cjs +308 -0
- package/dist/chunk-3CY6ATKS.cjs.map +1 -0
- package/dist/chunk-3D5ZCJ7R.js +268 -0
- package/dist/chunk-3D5ZCJ7R.js.map +1 -0
- package/dist/chunk-6LDAIJBL.js +103 -0
- package/dist/chunk-6LDAIJBL.js.map +1 -0
- package/dist/chunk-7HK2V5Q4.js +111 -0
- package/dist/chunk-7HK2V5Q4.js.map +1 -0
- package/dist/chunk-AAP472NG.js +177 -0
- package/dist/chunk-AAP472NG.js.map +1 -0
- package/dist/chunk-AP4JIUNW.js +42 -0
- package/dist/chunk-AP4JIUNW.js.map +1 -0
- package/dist/chunk-BBJPQPGP.js +457 -0
- package/dist/chunk-BBJPQPGP.js.map +1 -0
- package/dist/chunk-CRXRJZEV.cjs +82 -0
- package/dist/chunk-CRXRJZEV.cjs.map +1 -0
- package/dist/chunk-E3PICZYI.js +20 -0
- package/dist/chunk-E3PICZYI.js.map +1 -0
- package/dist/chunk-F7O2FA3F.cjs +1255 -0
- package/dist/chunk-F7O2FA3F.cjs.map +1 -0
- package/dist/chunk-FB56NAAH.cjs +243 -0
- package/dist/chunk-FB56NAAH.cjs.map +1 -0
- package/dist/chunk-G4AN7FND.js +1063 -0
- package/dist/chunk-G4AN7FND.js.map +1 -0
- package/dist/chunk-GS3FRH4K.js +294 -0
- package/dist/chunk-GS3FRH4K.js.map +1 -0
- package/dist/chunk-GYMG5OI3.cjs +186 -0
- package/dist/chunk-GYMG5OI3.cjs.map +1 -0
- package/dist/chunk-IHEIYZPL.cjs +301 -0
- package/dist/chunk-IHEIYZPL.cjs.map +1 -0
- package/dist/chunk-IJAMLYOT.cjs +1087 -0
- package/dist/chunk-IJAMLYOT.cjs.map +1 -0
- package/dist/chunk-ISL5B4LU.cjs +181 -0
- package/dist/chunk-ISL5B4LU.cjs.map +1 -0
- package/dist/chunk-JKW36IPT.js +179 -0
- package/dist/chunk-JKW36IPT.js.map +1 -0
- package/dist/chunk-KOCQBDI4.cjs +179 -0
- package/dist/chunk-KOCQBDI4.cjs.map +1 -0
- package/dist/chunk-OGNLB2KA.js +79 -0
- package/dist/chunk-OGNLB2KA.js.map +1 -0
- package/dist/chunk-PKHZ7CVI.js +28 -0
- package/dist/chunk-PKHZ7CVI.js.map +1 -0
- package/dist/chunk-QYD4F6T7.cjs +114 -0
- package/dist/chunk-QYD4F6T7.cjs.map +1 -0
- package/dist/chunk-RCW5IPPR.js +1288 -0
- package/dist/chunk-RCW5IPPR.js.map +1 -0
- package/dist/chunk-RMMKYHUN.cjs +35 -0
- package/dist/chunk-RMMKYHUN.cjs.map +1 -0
- package/dist/chunk-S4BJ2ADB.cjs +22 -0
- package/dist/chunk-S4BJ2ADB.cjs.map +1 -0
- package/dist/chunk-S7NH6CHY.cjs +462 -0
- package/dist/chunk-S7NH6CHY.cjs.map +1 -0
- package/dist/chunk-SIHPDZXA.js +176 -0
- package/dist/chunk-SIHPDZXA.js.map +1 -0
- package/dist/chunk-TW7PM3UJ.cjs +30 -0
- package/dist/chunk-TW7PM3UJ.cjs.map +1 -0
- package/dist/chunk-UA3EOFBS.cjs +1322 -0
- package/dist/chunk-UA3EOFBS.cjs.map +1 -0
- package/dist/chunk-VE4CCW7W.js +32 -0
- package/dist/chunk-VE4CCW7W.js.map +1 -0
- package/dist/chunk-W6RBCHYF.js +651 -0
- package/dist/chunk-W6RBCHYF.js.map +1 -0
- package/dist/chunk-WLJN7GIS.cjs +189 -0
- package/dist/chunk-WLJN7GIS.cjs.map +1 -0
- package/dist/chunk-YMLR375Z.js +1244 -0
- package/dist/chunk-YMLR375Z.js.map +1 -0
- package/dist/compliance/index.cjs +88 -0
- package/dist/compliance/index.cjs.map +1 -0
- package/dist/compliance/index.d.cts +277 -0
- package/dist/compliance/index.d.ts +277 -0
- package/dist/compliance/index.js +47 -0
- package/dist/compliance/index.js.map +1 -0
- package/dist/crypto.cjs +12 -136
- package/dist/crypto.cjs.map +1 -1
- package/dist/crypto.d.cts +4 -3
- package/dist/crypto.d.ts +4 -3
- package/dist/crypto.js +3 -137
- package/dist/crypto.js.map +1 -1
- package/dist/detectors/index.cjs +146 -1483
- package/dist/detectors/index.cjs.map +1 -1
- package/dist/detectors/index.d.cts +1 -1
- package/dist/detectors/index.d.ts +1 -1
- package/dist/detectors/index.js +3 -1449
- package/dist/detectors/index.js.map +1 -1
- package/dist/engine/index.cjs +441 -0
- package/dist/engine/index.cjs.map +1 -0
- package/dist/engine/index.d.cts +728 -0
- package/dist/engine/index.d.ts +728 -0
- package/dist/engine/index.js +189 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/hash-2PSYNDMM.cjs +14 -0
- package/dist/hash-2PSYNDMM.cjs.map +1 -0
- package/dist/hash-CELLDNKN.js +5 -0
- package/dist/hash-CELLDNKN.js.map +1 -0
- package/dist/index.cjs +153 -2816
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +93 -11
- package/dist/index.d.ts +93 -11
- package/dist/index.js +13 -2791
- package/dist/index.js.map +1 -1
- package/dist/json-CKDvlRtl.d.ts +147 -0
- package/dist/json-s8ny-RCo.d.cts +147 -0
- package/dist/mcp/index.cjs +751 -0
- package/dist/mcp/index.cjs.map +1 -0
- package/dist/mcp/index.d.cts +226 -0
- package/dist/mcp/index.d.ts +226 -0
- package/dist/mcp/index.js +744 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/middleware/express.cjs +93 -0
- package/dist/middleware/express.cjs.map +1 -0
- package/dist/middleware/express.d.cts +90 -0
- package/dist/middleware/express.d.ts +90 -0
- package/dist/middleware/express.js +91 -0
- package/dist/middleware/express.js.map +1 -0
- package/dist/middleware/hono.cjs +58 -0
- package/dist/middleware/hono.cjs.map +1 -0
- package/dist/middleware/hono.d.cts +75 -0
- package/dist/middleware/hono.d.ts +75 -0
- package/dist/middleware/hono.js +56 -0
- package/dist/middleware/hono.js.map +1 -0
- package/dist/middleware/index.cjs +24 -0
- package/dist/middleware/index.cjs.map +1 -0
- package/dist/middleware/index.d.cts +106 -0
- package/dist/middleware/index.d.ts +106 -0
- package/dist/middleware/index.js +15 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/schemas.cjs +5 -5
- package/dist/schemas.cjs.map +1 -1
- package/dist/schemas.d.cts +20 -10
- package/dist/schemas.d.ts +20 -10
- package/dist/schemas.js +5 -5
- package/dist/schemas.js.map +1 -1
- package/dist/session-BdRftyZ5.d.cts +102 -0
- package/dist/session-Cs2zlNQp.d.ts +102 -0
- package/dist/stream.cjs +13 -2262
- package/dist/stream.cjs.map +1 -1
- package/dist/stream.d.cts +1 -1
- package/dist/stream.d.ts +1 -1
- package/dist/stream.js +6 -2255
- package/dist/stream.js.map +1 -1
- package/dist/types--tPm_AjY.d.ts +199 -0
- package/dist/types-0BJH_wqc.d.cts +199 -0
- package/dist/types-CPUhjvl8.d.cts +629 -0
- package/dist/types-Dtht33aq.d.ts +629 -0
- package/dist/{types-D-9HzVuy.d.cts → types-EJTj_BKY.d.cts} +38 -7
- package/dist/{types-D-9HzVuy.d.ts → types-EJTj_BKY.d.ts} +38 -7
- package/dist/types-Zt_bZvwS.d.cts +182 -0
- package/dist/types-Zt_bZvwS.d.ts +182 -0
- package/dist/validators.cjs +39 -181
- package/dist/validators.cjs.map +1 -1
- package/dist/validators.js +1 -174
- package/dist/validators.js.map +1 -1
- package/dist/vault/index.cjs +688 -0
- package/dist/vault/index.cjs.map +1 -0
- package/dist/vault/index.d.cts +385 -0
- package/dist/vault/index.d.ts +385 -0
- package/dist/vault/index.js +669 -0
- package/dist/vault/index.js.map +1 -0
- package/package.json +128 -11
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkS7NH6CHY_cjs = require('../chunk-S7NH6CHY.cjs');
|
|
4
|
+
var chunkIJAMLYOT_cjs = require('../chunk-IJAMLYOT.cjs');
|
|
5
|
+
require('../chunk-3CY6ATKS.cjs');
|
|
6
|
+
require('../chunk-KOCQBDI4.cjs');
|
|
7
|
+
require('../chunk-S4BJ2ADB.cjs');
|
|
8
|
+
require('../chunk-2YG7R4KJ.cjs');
|
|
9
|
+
require('../chunk-IHEIYZPL.cjs');
|
|
10
|
+
require('../chunk-UA3EOFBS.cjs');
|
|
11
|
+
require('../chunk-GYMG5OI3.cjs');
|
|
12
|
+
var chunk2NNMJ55L_cjs = require('../chunk-2NNMJ55L.cjs');
|
|
13
|
+
var chunkFB56NAAH_cjs = require('../chunk-FB56NAAH.cjs');
|
|
14
|
+
require('../chunk-TW7PM3UJ.cjs');
|
|
15
|
+
var chunkWLJN7GIS_cjs = require('../chunk-WLJN7GIS.cjs');
|
|
16
|
+
|
|
17
|
+
// src/ai/restore.ts
|
|
18
|
+
var MAX_JSON_DEPTH = 512;
|
|
19
|
+
function createRestoreTransformer(provider) {
|
|
20
|
+
const partial = provider.partialTokenPattern === void 0 ? void 0 : new RegExp(
|
|
21
|
+
provider.partialTokenPattern.source,
|
|
22
|
+
provider.partialTokenPattern.flags.replace(/[gy]/g, "")
|
|
23
|
+
);
|
|
24
|
+
const whole = new RegExp(
|
|
25
|
+
provider.tokenPattern.source,
|
|
26
|
+
provider.tokenPattern.flags.replace("y", "").replace("g", "") + "g"
|
|
27
|
+
);
|
|
28
|
+
let carry = "";
|
|
29
|
+
async function push(chunk) {
|
|
30
|
+
if (typeof chunk !== "string") throw new chunkWLJN7GIS_cjs.ValidationError("chunk", "must be a string");
|
|
31
|
+
const buffer = carry + chunk;
|
|
32
|
+
let hold;
|
|
33
|
+
if (partial === void 0) {
|
|
34
|
+
hold = Math.max(0, buffer.length - (provider.maxTokenLength - 1));
|
|
35
|
+
for (const match of buffer.matchAll(whole)) {
|
|
36
|
+
if (match.index >= hold) break;
|
|
37
|
+
const end = match.index + match[0].length;
|
|
38
|
+
if (end > hold) hold = end;
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
const match = partial.exec(buffer);
|
|
42
|
+
hold = match === null || buffer.length - match.index >= provider.maxTokenLength ? buffer.length : match.index;
|
|
43
|
+
}
|
|
44
|
+
carry = buffer.slice(hold);
|
|
45
|
+
return hold === 0 ? "" : (await chunk2NNMJ55L_cjs.restoreTokens(buffer.slice(0, hold), provider)).text;
|
|
46
|
+
}
|
|
47
|
+
async function flush() {
|
|
48
|
+
const rest = carry;
|
|
49
|
+
carry = "";
|
|
50
|
+
return rest === "" ? "" : (await chunk2NNMJ55L_cjs.restoreTokens(rest, provider)).text;
|
|
51
|
+
}
|
|
52
|
+
return Object.freeze({ push, flush });
|
|
53
|
+
}
|
|
54
|
+
function createRestoreStream(provider) {
|
|
55
|
+
if (typeof TransformStream === "undefined") {
|
|
56
|
+
throw new chunkWLJN7GIS_cjs.ValidationError("TransformStream", "is not available in this environment");
|
|
57
|
+
}
|
|
58
|
+
const restorer = createRestoreTransformer(provider);
|
|
59
|
+
return new TransformStream({
|
|
60
|
+
async transform(chunk, controller) {
|
|
61
|
+
const out = await restorer.push(chunk);
|
|
62
|
+
if (out.length > 0) controller.enqueue(out);
|
|
63
|
+
},
|
|
64
|
+
async flush(controller) {
|
|
65
|
+
const out = await restorer.flush();
|
|
66
|
+
if (out.length > 0) controller.enqueue(out);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
async function restoreJsonText(text, provider) {
|
|
71
|
+
if (typeof text !== "string") throw new chunkWLJN7GIS_cjs.ValidationError("text", "must be a string");
|
|
72
|
+
let parsed;
|
|
73
|
+
try {
|
|
74
|
+
parsed = chunkS7NH6CHY_cjs.parseJsonLossless(text);
|
|
75
|
+
} catch {
|
|
76
|
+
return (await chunk2NNMJ55L_cjs.restoreTokens(text, provider)).text;
|
|
77
|
+
}
|
|
78
|
+
const { marker } = parsed;
|
|
79
|
+
let restored = 0;
|
|
80
|
+
const restore = async (value2) => {
|
|
81
|
+
const result = await chunk2NNMJ55L_cjs.restoreTokens(value2, provider);
|
|
82
|
+
restored += result.restored;
|
|
83
|
+
return result.text;
|
|
84
|
+
};
|
|
85
|
+
const walk = async (value2, depth) => {
|
|
86
|
+
if (typeof value2 === "string") {
|
|
87
|
+
return marker !== void 0 && value2.startsWith(marker) ? value2 : restore(value2);
|
|
88
|
+
}
|
|
89
|
+
if (typeof value2 !== "object" || value2 === null) return value2;
|
|
90
|
+
if (depth >= MAX_JSON_DEPTH) throw new chunkWLJN7GIS_cjs.ValidationError("text", "is nested too deeply");
|
|
91
|
+
if (Array.isArray(value2)) {
|
|
92
|
+
const items = [];
|
|
93
|
+
for (const item of value2) items.push(await walk(item, depth + 1));
|
|
94
|
+
return items;
|
|
95
|
+
}
|
|
96
|
+
const out = {};
|
|
97
|
+
for (const [key, member] of Object.entries(value2)) {
|
|
98
|
+
Object.defineProperty(out, await restore(key), {
|
|
99
|
+
value: await walk(member, depth + 1),
|
|
100
|
+
writable: true,
|
|
101
|
+
enumerable: true,
|
|
102
|
+
configurable: true
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
};
|
|
107
|
+
const value = await walk(parsed.value, 0);
|
|
108
|
+
return restored === 0 ? text : chunkS7NH6CHY_cjs.stringifyJsonLossless(value, marker);
|
|
109
|
+
}
|
|
110
|
+
var textDeltaLens = Object.freeze({
|
|
111
|
+
get(chunk) {
|
|
112
|
+
if (chunk["type"] !== "text-delta") return void 0;
|
|
113
|
+
const text = chunk["delta"] ?? chunk["textDelta"];
|
|
114
|
+
return typeof text === "string" ? text : void 0;
|
|
115
|
+
},
|
|
116
|
+
set(chunk, text) {
|
|
117
|
+
return typeof chunk["delta"] === "string" ? { ...chunk, delta: text } : { ...chunk, textDelta: text };
|
|
118
|
+
},
|
|
119
|
+
block: (chunk) => typeof chunk["id"] === "string" ? chunk["id"] : void 0
|
|
120
|
+
});
|
|
121
|
+
var contentLens = Object.freeze({
|
|
122
|
+
get: (chunk) => typeof chunk["content"] === "string" ? chunk["content"] : void 0,
|
|
123
|
+
set: (chunk, text) => {
|
|
124
|
+
const copy = Object.create(Object.getPrototypeOf(chunk));
|
|
125
|
+
return Object.assign(copy, chunk, { content: text });
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
var deltaLens = Object.freeze({
|
|
129
|
+
get: (chunk) => typeof chunk["delta"] === "string" ? chunk["delta"] : void 0,
|
|
130
|
+
set: (chunk, text) => ({
|
|
131
|
+
...chunk,
|
|
132
|
+
delta: text
|
|
133
|
+
})
|
|
134
|
+
});
|
|
135
|
+
async function* restoreChunks(source, lens, provider) {
|
|
136
|
+
const blocks = /* @__PURE__ */ new Map();
|
|
137
|
+
async function* drain() {
|
|
138
|
+
for (const block of blocks.values()) {
|
|
139
|
+
const rest = await block.restorer.flush();
|
|
140
|
+
if (rest !== "") yield lens.set(block.last, rest);
|
|
141
|
+
}
|
|
142
|
+
blocks.clear();
|
|
143
|
+
}
|
|
144
|
+
for await (const chunk of source) {
|
|
145
|
+
const text = lens.get(chunk);
|
|
146
|
+
if (text === void 0) {
|
|
147
|
+
yield* drain();
|
|
148
|
+
yield chunk;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const id = lens.block?.(chunk) ?? "";
|
|
152
|
+
let block = blocks.get(id);
|
|
153
|
+
if (block === void 0) {
|
|
154
|
+
block = { restorer: createRestoreTransformer(provider), last: chunk };
|
|
155
|
+
blocks.set(id, block);
|
|
156
|
+
}
|
|
157
|
+
block.last = chunk;
|
|
158
|
+
yield lens.set(chunk, await block.restorer.push(text));
|
|
159
|
+
}
|
|
160
|
+
yield* drain();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/ai/guard.ts
|
|
164
|
+
var MESSAGE_KEEP = /* @__PURE__ */ new Set([
|
|
165
|
+
"role",
|
|
166
|
+
"name",
|
|
167
|
+
"id",
|
|
168
|
+
"type",
|
|
169
|
+
"model",
|
|
170
|
+
"tool_call_id",
|
|
171
|
+
"toolCallId",
|
|
172
|
+
"finish_reason",
|
|
173
|
+
"stop_reason",
|
|
174
|
+
"cache_control"
|
|
175
|
+
]);
|
|
176
|
+
var PART_KEEP = /* @__PURE__ */ new Set([
|
|
177
|
+
"type",
|
|
178
|
+
"id",
|
|
179
|
+
"name",
|
|
180
|
+
"tool_use_id",
|
|
181
|
+
"tool_call_id",
|
|
182
|
+
"toolCallId",
|
|
183
|
+
"toolName",
|
|
184
|
+
"is_error",
|
|
185
|
+
"cache_control"
|
|
186
|
+
]);
|
|
187
|
+
var TOOL_CALL_KEEP = /* @__PURE__ */ new Set(["id", "type", "index"]);
|
|
188
|
+
var FUNCTION_KEEP = /* @__PURE__ */ new Set(["name"]);
|
|
189
|
+
var OPAQUE_KEYS = /* @__PURE__ */ new Set([
|
|
190
|
+
"signature",
|
|
191
|
+
"thoughtSignature",
|
|
192
|
+
"thought_signature",
|
|
193
|
+
"encrypted_content",
|
|
194
|
+
"encrypted_index",
|
|
195
|
+
"reasoningEncryptedContent",
|
|
196
|
+
"providerOptions",
|
|
197
|
+
"providerMetadata",
|
|
198
|
+
"experimental_providerMetadata"
|
|
199
|
+
]);
|
|
200
|
+
var MEDIA_PAYLOAD_KEYS = /* @__PURE__ */ new Set([
|
|
201
|
+
"data",
|
|
202
|
+
"file_data",
|
|
203
|
+
"url",
|
|
204
|
+
"image_url",
|
|
205
|
+
"image",
|
|
206
|
+
"audio",
|
|
207
|
+
"video",
|
|
208
|
+
"file_id",
|
|
209
|
+
"fileId"
|
|
210
|
+
]);
|
|
211
|
+
var MAX_DEPTH = 512;
|
|
212
|
+
function mediaTypeOf(parent) {
|
|
213
|
+
for (const key of ["media_type", "mediaType", "mimeType", "mime_type"]) {
|
|
214
|
+
const value = parent[key];
|
|
215
|
+
if (typeof value === "string") return value;
|
|
216
|
+
}
|
|
217
|
+
return void 0;
|
|
218
|
+
}
|
|
219
|
+
function isTextPayload(parent) {
|
|
220
|
+
const media = mediaTypeOf(parent);
|
|
221
|
+
return parent["type"] === "text" || media !== void 0 && /^text\//i.test(media);
|
|
222
|
+
}
|
|
223
|
+
function isOpaque(key, parent) {
|
|
224
|
+
if (OPAQUE_KEYS.has(key)) return true;
|
|
225
|
+
const value = parent[key];
|
|
226
|
+
if (typeof value === "string" && /^data:[\w.+-]+\/[\w.+-]+;base64,/i.test(value)) return true;
|
|
227
|
+
if (key !== "data") return false;
|
|
228
|
+
if (parent["type"] === "redacted_thinking" || parent["type"] === "base64") return true;
|
|
229
|
+
return mediaTypeOf(parent) !== void 0 && !isTextPayload(parent);
|
|
230
|
+
}
|
|
231
|
+
function isMediaPayload(key, parent) {
|
|
232
|
+
if (isOpaque(key, parent)) return true;
|
|
233
|
+
if (!MEDIA_PAYLOAD_KEYS.has(key)) return false;
|
|
234
|
+
return !(key === "data" && isTextPayload(parent));
|
|
235
|
+
}
|
|
236
|
+
var MEDIA_PARTS = /* @__PURE__ */ new Set([
|
|
237
|
+
"image",
|
|
238
|
+
"image_url",
|
|
239
|
+
"input_image",
|
|
240
|
+
"file",
|
|
241
|
+
"input_file",
|
|
242
|
+
"document",
|
|
243
|
+
"audio",
|
|
244
|
+
"input_audio",
|
|
245
|
+
"video"
|
|
246
|
+
]);
|
|
247
|
+
function isPlainObject(value) {
|
|
248
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
249
|
+
const proto = Object.getPrototypeOf(value);
|
|
250
|
+
return proto === Object.prototype || proto === null;
|
|
251
|
+
}
|
|
252
|
+
function setOwn(target, key, value) {
|
|
253
|
+
Object.defineProperty(target, key, {
|
|
254
|
+
value,
|
|
255
|
+
writable: true,
|
|
256
|
+
enumerable: true,
|
|
257
|
+
configurable: true
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
function createLlmGuard(options = {}) {
|
|
261
|
+
const { skipRoles = [], keyRules, countTokens } = options;
|
|
262
|
+
const passthrough = new Set(options.passthroughKeys ?? []);
|
|
263
|
+
const exempt = (key, parent) => passthrough.has(key) || isOpaque(key, parent);
|
|
264
|
+
const exemptInMedia = (key, parent) => passthrough.has(key) || isMediaPayload(key, parent);
|
|
265
|
+
const sessionOptions = { lenient: true, ...options.session };
|
|
266
|
+
const skip = new Set(skipRoles);
|
|
267
|
+
const probe = chunkFB56NAAH_cjs.createSessionTokenizer(sessionOptions);
|
|
268
|
+
const compile = (session) => chunkIJAMLYOT_cjs.compilePipeline(
|
|
269
|
+
{ ...options.spec, defaultStrategy: { strategy: "tokenize" } },
|
|
270
|
+
{ tokenization: session }
|
|
271
|
+
);
|
|
272
|
+
compile(probe);
|
|
273
|
+
function begin(snapshot) {
|
|
274
|
+
const session = chunkFB56NAAH_cjs.createSessionTokenizer({
|
|
275
|
+
...sessionOptions,
|
|
276
|
+
...snapshot !== void 0 ? { snapshot } : {}
|
|
277
|
+
});
|
|
278
|
+
const pipeline = compile(session);
|
|
279
|
+
function sanitizeText(text) {
|
|
280
|
+
return pipeline.transform(text).text;
|
|
281
|
+
}
|
|
282
|
+
function sanitizeMessages(messages) {
|
|
283
|
+
if (!Array.isArray(messages)) throw new chunkWLJN7GIS_cjs.ValidationError("messages", "must be an array");
|
|
284
|
+
const fields = [];
|
|
285
|
+
const unscanned = [];
|
|
286
|
+
const flaggedKeys = [];
|
|
287
|
+
let replaced = 0;
|
|
288
|
+
const json = (value, path, skipMember = exempt) => {
|
|
289
|
+
const result = chunkS7NH6CHY_cjs.sanitizeJson(value, {
|
|
290
|
+
pipeline,
|
|
291
|
+
jsonStringKeys: ["arguments"],
|
|
292
|
+
skipMember,
|
|
293
|
+
...keyRules !== void 0 ? { keyRules } : {}
|
|
294
|
+
});
|
|
295
|
+
replaced += result.replaced;
|
|
296
|
+
for (const field of result.fields) fields.push({ ...field, path: `${path}${field.path}` });
|
|
297
|
+
for (const skipped of result.skipped) unscanned.push(`${path}${skipped}`);
|
|
298
|
+
for (const flagged of result.flaggedKeys) flaggedKeys.push(`${path}${flagged}`);
|
|
299
|
+
return result.value;
|
|
300
|
+
};
|
|
301
|
+
const member = (key, siblings, parent, skipMember = exempt) => {
|
|
302
|
+
const holder = {};
|
|
303
|
+
setOwn(holder, key, siblings[key]);
|
|
304
|
+
const judge = (name, object) => skipMember(name, object === holder ? siblings : object);
|
|
305
|
+
const [entry] = Object.entries(json(holder, parent, judge));
|
|
306
|
+
return entry ?? [key, void 0];
|
|
307
|
+
};
|
|
308
|
+
const copy = (source, keep, handle) => {
|
|
309
|
+
const out2 = {};
|
|
310
|
+
for (const [key, value] of Object.entries(source)) {
|
|
311
|
+
const [name, item] = keep.has(key) ? [key, value] : handle(key, value);
|
|
312
|
+
setOwn(out2, name, item);
|
|
313
|
+
}
|
|
314
|
+
return out2;
|
|
315
|
+
};
|
|
316
|
+
const part = (value, path, depth) => {
|
|
317
|
+
if (!isPlainObject(value)) return json(value, path);
|
|
318
|
+
if (depth >= MAX_DEPTH) throw new chunkWLJN7GIS_cjs.ValidationError("messages", "are nested too deeply");
|
|
319
|
+
const media = typeof value["type"] === "string" && MEDIA_PARTS.has(value["type"]);
|
|
320
|
+
return copy(
|
|
321
|
+
value,
|
|
322
|
+
PART_KEEP,
|
|
323
|
+
(key, item) => key === "content" ? [key, content(item, `${path}/content`, depth + 1)] : member(key, value, path, media ? exemptInMedia : exempt)
|
|
324
|
+
);
|
|
325
|
+
};
|
|
326
|
+
const content = (value, path, depth) => Array.isArray(value) ? value.map((item, index) => part(item, `${path}/${String(index)}`, depth)) : json(value, path);
|
|
327
|
+
const call = (value, path) => isPlainObject(value) ? copy(value, FUNCTION_KEEP, (key) => member(key, value, path)) : json(value, path);
|
|
328
|
+
const out = messages.map((message, index) => {
|
|
329
|
+
const path = `/${String(index)}`;
|
|
330
|
+
if (!isPlainObject(message)) {
|
|
331
|
+
if (typeof message === "object" && message !== null && !Array.isArray(message)) {
|
|
332
|
+
throw new chunkWLJN7GIS_cjs.ValidationError(
|
|
333
|
+
`messages[${String(index)}]`,
|
|
334
|
+
"is not a plain object and cannot be sanitized; sanitize its text with sanitizeText() before building it"
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
return json(message, path);
|
|
338
|
+
}
|
|
339
|
+
if (typeof message["role"] === "string" && skip.has(message["role"])) return message;
|
|
340
|
+
return copy(message, MESSAGE_KEEP, (key, value) => {
|
|
341
|
+
if (key === "content") return [key, content(value, `${path}/content`, 0)];
|
|
342
|
+
if (key === "function_call") return [key, call(value, `${path}/function_call`)];
|
|
343
|
+
if (key === "tool_calls" && Array.isArray(value)) {
|
|
344
|
+
const calls = value.map((item, position) => {
|
|
345
|
+
const itemPath = `${path}/tool_calls/${String(position)}`;
|
|
346
|
+
if (!isPlainObject(item)) return json(item, itemPath);
|
|
347
|
+
return copy(
|
|
348
|
+
item,
|
|
349
|
+
TOOL_CALL_KEEP,
|
|
350
|
+
(callKey, callValue) => callKey === "function" ? [callKey, call(callValue, `${itemPath}/function`)] : member(callKey, item, itemPath)
|
|
351
|
+
);
|
|
352
|
+
});
|
|
353
|
+
return [key, calls];
|
|
354
|
+
}
|
|
355
|
+
return member(key, message, path);
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
return {
|
|
359
|
+
messages: out,
|
|
360
|
+
fields,
|
|
361
|
+
replaced,
|
|
362
|
+
unscanned,
|
|
363
|
+
flaggedKeys,
|
|
364
|
+
...countTokens !== void 0 ? {
|
|
365
|
+
tokens: {
|
|
366
|
+
before: countTokens(JSON.stringify(messages)),
|
|
367
|
+
after: countTokens(JSON.stringify(out))
|
|
368
|
+
}
|
|
369
|
+
} : {}
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
async function* restoreIterable(source) {
|
|
373
|
+
const restorer = createRestoreTransformer(session);
|
|
374
|
+
for await (const chunk of source) {
|
|
375
|
+
const out = await restorer.push(chunk);
|
|
376
|
+
if (out !== "") yield out;
|
|
377
|
+
}
|
|
378
|
+
const rest = await restorer.flush();
|
|
379
|
+
if (rest !== "") yield rest;
|
|
380
|
+
}
|
|
381
|
+
return Object.freeze({
|
|
382
|
+
session,
|
|
383
|
+
sanitizeText,
|
|
384
|
+
sanitizeMessages,
|
|
385
|
+
restoreText: (text) => session.restore(text),
|
|
386
|
+
restoreStream: () => createRestoreStream(session),
|
|
387
|
+
restoreIterable,
|
|
388
|
+
snapshot: () => session.snapshot()
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
return Object.freeze({ begin });
|
|
392
|
+
}
|
|
393
|
+
var JSON_TEXT_KEYS = /* @__PURE__ */ new Set(["input", "args", "arguments"]);
|
|
394
|
+
var EXCHANGE = /* @__PURE__ */ Symbol("anonyma.exchange");
|
|
395
|
+
async function restoreDeep(value, exchange, key, depth = 0) {
|
|
396
|
+
if (typeof value === "string") {
|
|
397
|
+
return key !== void 0 && JSON_TEXT_KEYS.has(key) ? restoreJsonText(value, exchange.session) : exchange.restoreText(value).text;
|
|
398
|
+
}
|
|
399
|
+
if (typeof value !== "object" || value === null || depth > 64) return value;
|
|
400
|
+
if (Array.isArray(value)) {
|
|
401
|
+
const items = [];
|
|
402
|
+
for (const item of value) {
|
|
403
|
+
items.push(await restoreDeep(item, exchange, void 0, depth + 1));
|
|
404
|
+
}
|
|
405
|
+
return items;
|
|
406
|
+
}
|
|
407
|
+
const proto = Object.getPrototypeOf(value);
|
|
408
|
+
if (proto !== Object.prototype && proto !== null) return value;
|
|
409
|
+
const out = {};
|
|
410
|
+
for (const [name, member] of Object.entries(value)) {
|
|
411
|
+
setOwn(out, name, await restoreDeep(member, exchange, name, depth + 1));
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
415
|
+
function toLanguageModelMiddleware(guard, options = {}) {
|
|
416
|
+
const { onSanitized } = options;
|
|
417
|
+
const exchangeOf = (params) => {
|
|
418
|
+
const exchange = params[EXCHANGE];
|
|
419
|
+
if (exchange === void 0) {
|
|
420
|
+
throw new chunkWLJN7GIS_cjs.ValidationError(
|
|
421
|
+
"params",
|
|
422
|
+
"did not come from transformParams(), so the tokens in the response cannot be restored"
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
return exchange;
|
|
426
|
+
};
|
|
427
|
+
const restorePart = async (part, exchange) => {
|
|
428
|
+
if (!isPlainObject(part) || part["type"] !== "tool-call") return part;
|
|
429
|
+
return restoreDeep(part, exchange);
|
|
430
|
+
};
|
|
431
|
+
return Object.freeze({
|
|
432
|
+
transformParams({
|
|
433
|
+
params
|
|
434
|
+
}) {
|
|
435
|
+
const exchange = guard.begin();
|
|
436
|
+
const prompt = params["prompt"];
|
|
437
|
+
const transformed = { ...params };
|
|
438
|
+
if (Array.isArray(prompt)) {
|
|
439
|
+
const report = exchange.sanitizeMessages(prompt);
|
|
440
|
+
onSanitized?.(report);
|
|
441
|
+
transformed["prompt"] = report.messages;
|
|
442
|
+
} else if (typeof prompt === "string") {
|
|
443
|
+
transformed["prompt"] = exchange.sanitizeText(prompt);
|
|
444
|
+
}
|
|
445
|
+
Object.defineProperty(transformed, EXCHANGE, { value: exchange, enumerable: true });
|
|
446
|
+
return Promise.resolve(transformed);
|
|
447
|
+
},
|
|
448
|
+
async wrapGenerate({
|
|
449
|
+
doGenerate,
|
|
450
|
+
params
|
|
451
|
+
}) {
|
|
452
|
+
const exchange = exchangeOf(params);
|
|
453
|
+
return restoreDeep(await doGenerate(), exchange);
|
|
454
|
+
},
|
|
455
|
+
async wrapStream({
|
|
456
|
+
doStream,
|
|
457
|
+
params
|
|
458
|
+
}) {
|
|
459
|
+
const exchange = exchangeOf(params);
|
|
460
|
+
const result = await doStream();
|
|
461
|
+
async function* parts() {
|
|
462
|
+
const reader = result.stream.getReader();
|
|
463
|
+
try {
|
|
464
|
+
for (; ; ) {
|
|
465
|
+
const { value, done } = await reader.read();
|
|
466
|
+
if (done) return;
|
|
467
|
+
yield value;
|
|
468
|
+
}
|
|
469
|
+
} finally {
|
|
470
|
+
reader.releaseLock();
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
const lens = {
|
|
474
|
+
get: (part) => isPlainObject(part) ? textDeltaLens.get(part) : void 0,
|
|
475
|
+
set: (part, text) => textDeltaLens.set(part, text),
|
|
476
|
+
block: (part) => isPlainObject(part) ? textDeltaLens.block?.(part) : void 0
|
|
477
|
+
};
|
|
478
|
+
const restored = restoreChunks(parts(), lens, exchange.session);
|
|
479
|
+
const stream = new ReadableStream({
|
|
480
|
+
async pull(controller) {
|
|
481
|
+
for (; ; ) {
|
|
482
|
+
const next = await restored.next();
|
|
483
|
+
if (next.done === true) {
|
|
484
|
+
controller.close();
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
const part = next.value;
|
|
488
|
+
const text = lens.get(part);
|
|
489
|
+
if (text === "") continue;
|
|
490
|
+
controller.enqueue(text === void 0 ? await restorePart(part, exchange) : part);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
},
|
|
494
|
+
async cancel(reason) {
|
|
495
|
+
await restored.return();
|
|
496
|
+
await result.stream.cancel(reason);
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
return { ...result, stream };
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
Object.defineProperty(exports, "sanitizeJson", {
|
|
505
|
+
enumerable: true,
|
|
506
|
+
get: function () { return chunkS7NH6CHY_cjs.sanitizeJson; }
|
|
507
|
+
});
|
|
508
|
+
Object.defineProperty(exports, "sanitizeJsonAsync", {
|
|
509
|
+
enumerable: true,
|
|
510
|
+
get: function () { return chunkS7NH6CHY_cjs.sanitizeJsonAsync; }
|
|
511
|
+
});
|
|
512
|
+
exports.contentLens = contentLens;
|
|
513
|
+
exports.createLlmGuard = createLlmGuard;
|
|
514
|
+
exports.createRestoreStream = createRestoreStream;
|
|
515
|
+
exports.createRestoreTransformer = createRestoreTransformer;
|
|
516
|
+
exports.deltaLens = deltaLens;
|
|
517
|
+
exports.restoreChunks = restoreChunks;
|
|
518
|
+
exports.restoreJsonText = restoreJsonText;
|
|
519
|
+
exports.textDeltaLens = textDeltaLens;
|
|
520
|
+
exports.toLanguageModelMiddleware = toLanguageModelMiddleware;
|
|
521
|
+
//# sourceMappingURL=index.cjs.map
|
|
522
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/ai/restore.ts","../../src/ai/guard.ts"],"names":["ValidationError","restoreTokens","parseJsonLossless","value","stringifyJsonLossless","createSessionTokenizer","compilePipeline","sanitizeJson","out"],"mappings":";;;;;;;;;;;;;;;;;AAcA,IAAM,cAAA,GAAiB,GAAA;AAoBhB,SAAS,yBAAyB,QAAA,EAAuD;AAC9F,EAAA,MAAM,OAAA,GACJ,QAAA,CAAS,mBAAA,KAAwB,MAAA,GAC7B,SACA,IAAI,MAAA;AAAA,IACF,SAAS,mBAAA,CAAoB,MAAA;AAAA,IAC7B,QAAA,CAAS,mBAAA,CAAoB,KAAA,CAAM,OAAA,CAAQ,SAAS,EAAE;AAAA,GACxD;AACN,EAAA,MAAM,QAAQ,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MAAA;AAAA,QAClD,KAAK,CAAC,IAAA,EAAe,SACnB,aAAA,CAAc,GAAA,CAAI,MAAiC,IAAI,CAAA;AAAA,QACzD,KAAA,EAAO,CAAC,IAAA,KACN,aAAA,CAAc,IAAI,CAAA,GAAI,aAAA,CAAc,KAAA,GAAQ,IAAI,CAAA,GAAI;AAAA,OACxD;AAGA,MAAA,MAAM,WAAW,aAAA,CAAc,KAAA,EAAM,EAAG,IAAA,EAAM,SAAS,OAAO,CAAA;AAE9D,MAAA,MAAM,MAAA,GAAS,IAAI,cAAA,CAAwB;AAAA,QACzC,MAAM,KAAK,UAAA,EAA2B;AACpC,UAAA,WAAS;AACP,YAAA,MAAM,IAAA,GAAO,MAAM,QAAA,CAAS,IAAA,EAAK;AACjC,YAAA,IAAI,IAAA,CAAK,SAAS,IAAA,EAAM;AACtB,cAAA,UAAA,CAAW,KAAA,EAAM;AACjB,cAAA;AAAA,YACF;AACA,YAAA,MAAM,OAAO,IAAA,CAAK,KAAA;AAClB,YAAA,MAAM,IAAA,GAAO,IAAA,CAAK,GAAA,CAAI,IAAI,CAAA;AAE1B,YAAA,IAAI,SAAS,EAAA,EAAI;AACjB,YAAA,UAAA,CAAW,OAAA,CAAQ,SAAS,MAAA,GAAY,MAAM,YAAY,IAAA,EAAM,QAAQ,IAAI,IAAI,CAAA;AAChF,YAAA;AAAA,UACF;AAAA,QACF,CAAA;AAAA,QACA,MAAM,OAAO,MAAA,EAAuB;AAClC,UAAA,MAAM,SAAS,MAAA,EAAO;AACtB,UAAA,MAAM,MAAA,CAAO,MAAA,CAAO,MAAA,CAAO,MAAM,CAAA;AAAA,QACnC;AAAA,OACD,CAAA;AACD,MAAA,OAAO,EAAE,GAAG,MAAA,EAAQ,MAAA,EAAO;AAAA,IAC7B;AAAA,GACD,CAAA;AACH","file":"index.cjs","sourcesContent":["/**\n * @module ai/restore\n * @description Restoring tokens in model output that arrives as a stream. A\n * token may be split across chunks (`[EMAIL_00` + `01]`), so the transformer\n * holds back a trailing fragment that could still become a token.\n */\n\nimport { ValidationError } from \"../errors.js\";\nimport type { AsyncChunkTransformer } from \"../engine/types.js\";\nimport { parseJsonLossless, stringifyJsonLossless } from \"../internal/json-numbers.js\";\nimport { restoreTokens } from \"../vault/restore.js\";\nimport type { TokenizationProvider } from \"../vault/types.js\";\n\n/** Deepest nesting of a JSON document in which tokens are restored. */\nconst MAX_JSON_DEPTH = 512;\n\n/**\n * Create a push-style transformer that restores the tokens of `provider` in\n * text arriving in arbitrary chunks.\n *\n * The output is the same as restoring the whole text at once, however the\n * text is split, and accepts the same spellings of a token: a provider with\n * its own `restore()` (a lenient session) is used through it.\n *\n * @param provider - The provider that issued the tokens.\n * @returns An {@link AsyncChunkTransformer}. Await each `push()` before the next.\n *\n * @example\n * ```ts\n * const restorer = createRestoreTransformer(session);\n * for await (const delta of modelStream) ui.append(await restorer.push(delta));\n * ui.append(await restorer.flush());\n * ```\n */\nexport function createRestoreTransformer(provider: TokenizationProvider): AsyncChunkTransformer {\n const partial =\n provider.partialTokenPattern === undefined\n ? undefined\n : new RegExp(\n provider.partialTokenPattern.source,\n provider.partialTokenPattern.flags.replace(/[gy]/g, \"\"),\n );\n const whole = new RegExp(\n provider.tokenPattern.source,\n provider.tokenPattern.flags.replace(\"y\", \"\").replace(\"g\", \"\") + \"g\",\n );\n let carry = \"\";\n\n async function push(chunk: string): Promise<string> {\n if (typeof chunk !== \"string\") throw new ValidationError(\"chunk\", \"must be a string\");\n const buffer = carry + chunk;\n let hold: number;\n if (partial === undefined) {\n hold = Math.max(0, buffer.length - (provider.maxTokenLength - 1));\n // A complete token that crosses the cut goes out whole; only what follows it is held back.\n for (const match of buffer.matchAll(whole)) {\n if (match.index >= hold) break;\n const end = match.index + match[0].length;\n if (end > hold) hold = end;\n }\n } else {\n const match = partial.exec(buffer);\n // A fragment longer than any token cannot be the start of one.\n hold =\n match === null || buffer.length - match.index >= provider.maxTokenLength\n ? buffer.length\n : match.index;\n }\n carry = buffer.slice(hold);\n return hold === 0 ? \"\" : (await restoreTokens(buffer.slice(0, hold), provider)).text;\n }\n\n async function flush(): Promise<string> {\n const rest = carry;\n carry = \"\";\n return rest === \"\" ? \"\" : (await restoreTokens(rest, provider)).text;\n }\n\n return Object.freeze({ push, flush });\n}\n\n/**\n * Create a WHATWG `TransformStream` that restores tokens in a stream of text.\n *\n * @param provider - The provider that issued the tokens.\n * @returns A `TransformStream<string, string>`.\n * @throws {@link ValidationError} When `TransformStream` is unavailable.\n *\n * @example\n * ```ts\n * const restored = modelTextStream.pipeThrough(createRestoreStream(session));\n * ```\n */\nexport function createRestoreStream(\n provider: TokenizationProvider,\n): TransformStream<string, string> {\n if (typeof TransformStream === \"undefined\") {\n throw new ValidationError(\"TransformStream\", \"is not available in this environment\");\n }\n const restorer = createRestoreTransformer(provider);\n return new TransformStream<string, string>({\n async transform(chunk, controller): Promise<void> {\n const out = await restorer.push(chunk);\n if (out.length > 0) controller.enqueue(out);\n },\n async flush(controller): Promise<void> {\n const out = await restorer.flush();\n if (out.length > 0) controller.enqueue(out);\n },\n });\n}\n\n/**\n * Restore the tokens inside a JSON document held as text, such as the\n * arguments of a tool call. Values are restored into the parsed document and\n * the document is serialised again, so a restored value that contains a\n * quote, a backslash or a line break can neither break the document nor add\n * members to it. A text that is not JSON is restored as plain text; a\n * document without tokens is returned as it was written.\n *\n * @param text - The JSON document.\n * @param provider - The provider that issued the tokens.\n * @returns The document with restored values.\n * @throws {@link ValidationError} When the document is nested deeper than 512 levels.\n *\n * @example\n * ```ts\n * await restoreJsonText('{\"to\":\"[EMAIL_0001]\"}', session); // '{\"to\":\"alice@example.com\"}'\n * ```\n */\nexport async function restoreJsonText(\n text: string,\n provider: TokenizationProvider,\n): Promise<string> {\n if (typeof text !== \"string\") throw new ValidationError(\"text\", \"must be a string\");\n let parsed: ReturnType<typeof parseJsonLossless>;\n try {\n parsed = parseJsonLossless(text);\n } catch {\n return (await restoreTokens(text, provider)).text;\n }\n const { marker } = parsed;\n let restored = 0;\n\n const restore = async (value: string): Promise<string> => {\n const result = await restoreTokens(value, provider);\n restored += result.restored;\n return result.text;\n };\n const walk = async (value: unknown, depth: number): Promise<unknown> => {\n if (typeof value === \"string\") {\n return marker !== undefined && value.startsWith(marker) ? value : restore(value);\n }\n if (typeof value !== \"object\" || value === null) return value;\n if (depth >= MAX_JSON_DEPTH) throw new ValidationError(\"text\", \"is nested too deeply\");\n if (Array.isArray(value)) {\n const items: unknown[] = [];\n for (const item of value as unknown[]) items.push(await walk(item, depth + 1));\n return items;\n }\n const out: Record<string, unknown> = {};\n for (const [key, member] of Object.entries(value)) {\n Object.defineProperty(out, await restore(key), {\n value: await walk(member, depth + 1),\n writable: true,\n enumerable: true,\n configurable: true,\n });\n }\n return out;\n };\n\n const value = await walk(parsed.value, 0);\n return restored === 0 ? text : stringifyJsonLossless(value, marker);\n}\n\n/**\n * Reads and writes the text of a structured stream chunk.\n */\nexport interface ChunkLens<C> {\n /** The text of the chunk, or `undefined` when the chunk carries no text. */\n readonly get: (chunk: C) => string | undefined;\n /** A copy of the chunk with its text replaced. */\n readonly set: (chunk: C, text: string) => C;\n /**\n * Identifies the text block a chunk belongs to, when a stream can carry\n * several at once. Chunks of different blocks are restored independently.\n */\n readonly block?: (chunk: C) => string | undefined;\n}\n\n/**\n * Lens for chunks shaped `{ type: \"text-delta\", delta }` or\n * `{ type: \"text-delta\", textDelta }` (the stream parts of the Vercel AI SDK).\n */\nexport const textDeltaLens: ChunkLens<Record<string, unknown>> = Object.freeze({\n get(chunk: Record<string, unknown>): string | undefined {\n if (chunk[\"type\"] !== \"text-delta\") return undefined;\n const text = chunk[\"delta\"] ?? chunk[\"textDelta\"];\n return typeof text === \"string\" ? text : undefined;\n },\n set(chunk: Record<string, unknown>, text: string): Record<string, unknown> {\n return typeof chunk[\"delta\"] === \"string\"\n ? { ...chunk, delta: text }\n : { ...chunk, textDelta: text };\n },\n block: (chunk: Record<string, unknown>): string | undefined =>\n typeof chunk[\"id\"] === \"string\" ? chunk[\"id\"] : undefined,\n});\n\n/**\n * Lens for chunks whose text is a string member named `content` (message\n * chunks of LangChain) .\n */\nexport const contentLens: ChunkLens<Record<string, unknown>> = Object.freeze({\n get: (chunk: Record<string, unknown>): string | undefined =>\n typeof chunk[\"content\"] === \"string\" ? chunk[\"content\"] : undefined,\n set: (chunk: Record<string, unknown>, text: string): Record<string, unknown> => {\n const copy = Object.create(Object.getPrototypeOf(chunk) as object | null) as Record<\n string,\n unknown\n >;\n return Object.assign(copy, chunk, { content: text });\n },\n});\n\n/**\n * Lens for chunks whose text is a string member named `delta` (response\n * chunks of LlamaIndex).\n */\nexport const deltaLens: ChunkLens<Record<string, unknown>> = Object.freeze({\n get: (chunk: Record<string, unknown>): string | undefined =>\n typeof chunk[\"delta\"] === \"string\" ? chunk[\"delta\"] : undefined,\n set: (chunk: Record<string, unknown>, text: string): Record<string, unknown> => ({\n ...chunk,\n delta: text,\n }),\n});\n\n/**\n * Restore tokens in a stream of structured chunks. Text that is held back\n * because it may be the start of a token is delivered with the next text\n * chunk of its block, and in any case before the next chunk that carries no\n * text, so the order of the stream is kept: nothing arrives after the chunk\n * that ends its block. A token that is split around a chunk without text is\n * therefore not restored.\n *\n * @param source - The chunks produced by the model SDK.\n * @param lens - How to read and write the text of a chunk.\n * @param provider - The provider that issued the tokens.\n * @returns The chunks with restored text.\n *\n * @example\n * ```ts\n * for await (const chunk of restoreChunks(await chain.stream(input), contentLens, session)) {\n * process.stdout.write(String(chunk.content));\n * }\n * ```\n */\nexport async function* restoreChunks<C>(\n source: AsyncIterable<C>,\n lens: ChunkLens<C>,\n provider: TokenizationProvider,\n): AsyncGenerator<C, void, undefined> {\n /** Per text block: its restorer and the last chunk of it, used to carry held text. */\n const blocks = new Map<string, { readonly restorer: AsyncChunkTransformer; last: C }>();\n\n async function* drain(): AsyncGenerator<C, void, undefined> {\n for (const block of blocks.values()) {\n const rest = await block.restorer.flush();\n if (rest !== \"\") yield lens.set(block.last, rest);\n }\n blocks.clear();\n }\n\n for await (const chunk of source) {\n const text = lens.get(chunk);\n if (text === undefined) {\n yield* drain();\n yield chunk;\n continue;\n }\n const id = lens.block?.(chunk) ?? \"\";\n let block = blocks.get(id);\n if (block === undefined) {\n block = { restorer: createRestoreTransformer(provider), last: chunk };\n blocks.set(id, block);\n }\n block.last = chunk;\n yield lens.set(chunk, await block.restorer.push(text));\n }\n yield* drain();\n}\n","/**\n * @module ai/guard\n * @description Redaction middleware for LLM calls: personal data is replaced\n * by tokens before a prompt leaves the process, and the tokens are replaced\n * by the original values in what comes back — also when it comes back as a\n * stream. The mapping between tokens and values never leaves the process.\n */\n\nimport { ValidationError } from \"../errors.js\";\nimport type { AuditFieldEntry } from \"../audit/types.js\";\nimport { compilePipeline } from \"../engine/compile.js\";\nimport type { Pipeline, PipelineSpec } from \"../engine/types.js\";\nimport { createSessionTokenizer } from \"../vault/session.js\";\nimport type {\n SessionSnapshot,\n SessionTokenizer,\n SessionTokenizerOptions,\n} from \"../vault/session.js\";\nimport type { RestoreResult } from \"../vault/types.js\";\nimport { sanitizeJson } from \"./json.js\";\nimport type { KeyRule } from \"./json.js\";\nimport {\n createRestoreStream,\n createRestoreTransformer,\n restoreChunks,\n restoreJsonText,\n textDeltaLens,\n} from \"./restore.js\";\n\n/**\n * Members of a chat message that describe structure and must reach the model\n * unchanged. They are only honoured at the top level of a message.\n */\nconst MESSAGE_KEEP: ReadonlySet<string> = new Set([\n \"role\",\n \"name\",\n \"id\",\n \"type\",\n \"model\",\n \"tool_call_id\",\n \"toolCallId\",\n \"finish_reason\",\n \"stop_reason\",\n \"cache_control\",\n]);\n\n/** Structural members of a content part, honoured at the top level of the part only. */\nconst PART_KEEP: ReadonlySet<string> = new Set([\n \"type\",\n \"id\",\n \"name\",\n \"tool_use_id\",\n \"tool_call_id\",\n \"toolCallId\",\n \"toolName\",\n \"is_error\",\n \"cache_control\",\n]);\n\n/** Structural members of an OpenAI-style tool call. */\nconst TOOL_CALL_KEEP: ReadonlySet<string> = new Set([\"id\", \"type\", \"index\"]);\n\n/** Structural members of the `{ name, arguments }` object of a tool call. */\nconst FUNCTION_KEEP: ReadonlySet<string> = new Set([\"name\"]);\n\n/**\n * Members a provider expects back byte for byte: signatures of thinking\n * blocks, encrypted reasoning and search results, provider-specific options.\n * Rewriting one makes the provider reject the next request.\n */\nconst OPAQUE_KEYS: ReadonlySet<string> = new Set([\n \"signature\",\n \"thoughtSignature\",\n \"thought_signature\",\n \"encrypted_content\",\n \"encrypted_index\",\n \"reasoningEncryptedContent\",\n \"providerOptions\",\n \"providerMetadata\",\n \"experimental_providerMetadata\",\n]);\n\n/** Members of a media part that hold or locate the media itself. */\nconst MEDIA_PAYLOAD_KEYS: ReadonlySet<string> = new Set([\n \"data\",\n \"file_data\",\n \"url\",\n \"image_url\",\n \"image\",\n \"audio\",\n \"video\",\n \"file_id\",\n \"fileId\",\n]);\n\n/** Deepest nesting of content parts accepted in a message. */\nconst MAX_DEPTH = 512;\n\n/** The media type an object declares for its payload, if any. */\nfunction mediaTypeOf(parent: Readonly<Record<string, unknown>>): string | undefined {\n for (const key of [\"media_type\", \"mediaType\", \"mimeType\", \"mime_type\"]) {\n const value = parent[key];\n if (typeof value === \"string\") return value;\n }\n return undefined;\n}\n\n/** Whether `parent.data` is text that can and must be inspected. */\nfunction isTextPayload(parent: Readonly<Record<string, unknown>>): boolean {\n const media = mediaTypeOf(parent);\n return parent[\"type\"] === \"text\" || (media !== undefined && /^text\\//i.test(media));\n}\n\n/** Whether a member must reach the model untouched, wherever it occurs. */\nfunction isOpaque(key: string, parent: Readonly<Record<string, unknown>>): boolean {\n if (OPAQUE_KEYS.has(key)) return true;\n const value = parent[key];\n // A data URL is a payload under any name.\n if (typeof value === \"string\" && /^data:[\\w.+-]+\\/[\\w.+-]+;base64,/i.test(value)) return true;\n if (key !== \"data\") return false;\n if (parent[\"type\"] === \"redacted_thinking\" || parent[\"type\"] === \"base64\") return true;\n return mediaTypeOf(parent) !== undefined && !isTextPayload(parent);\n}\n\n/** Whether a member of a media part holds the media rather than text about it. */\nfunction isMediaPayload(key: string, parent: Readonly<Record<string, unknown>>): boolean {\n if (isOpaque(key, parent)) return true;\n if (!MEDIA_PAYLOAD_KEYS.has(key)) return false;\n return !(key === \"data\" && isTextPayload(parent));\n}\n\n/** Content parts that carry media. Their payload is passed on; their text is inspected. */\nconst MEDIA_PARTS: ReadonlySet<string> = new Set([\n \"image\",\n \"image_url\",\n \"input_image\",\n \"file\",\n \"input_file\",\n \"document\",\n \"audio\",\n \"input_audio\",\n \"video\",\n]);\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== \"object\" || value === null || Array.isArray(value)) return false;\n const proto = Object.getPrototypeOf(value) as unknown;\n return proto === Object.prototype || proto === null;\n}\n\nfunction setOwn(target: Record<string, unknown>, key: string, value: unknown): void {\n Object.defineProperty(target, key, {\n value,\n writable: true,\n enumerable: true,\n configurable: true,\n });\n}\n\n/**\n * Options accepted by {@link createLlmGuard}.\n */\nexport interface LlmGuardOptions {\n /**\n * What to detect. The strategy members of the spec are ignored: every\n * detection is replaced by a session token. Defaults to every built-in\n * category.\n */\n readonly spec?: Omit<PipelineSpec, \"defaultStrategy\" | \"rules\">;\n /** Options for the session tokenizer of each exchange. `lenient` defaults to `true`. */\n readonly session?: Omit<SessionTokenizerOptions, \"snapshot\">;\n /** Message roles that are sent unchanged, for example `[\"system\"]`. */\n readonly skipRoles?: readonly string[];\n /** Keys whose values are tokenized whole, such as `password`. */\n readonly keyRules?: readonly KeyRule[];\n /**\n * Further members that must reach the model byte for byte, by key name at\n * any depth. Signatures, encrypted reasoning, provider options and media\n * payloads are passed on without this. Whatever is passed on is listed in\n * {@link SanitizedMessages.unscanned}.\n */\n readonly passthroughKeys?: readonly string[];\n /**\n * Counts the tokens of a text with the tokenizer of the target model. When\n * given, {@link SanitizedMessages.tokens} reports the prompt size before and\n * after sanitization.\n */\n readonly countTokens?: (text: string) => number;\n}\n\n/**\n * Sanitized chat messages and an account of what was replaced.\n */\nexport interface SanitizedMessages<M> {\n /** The messages to send: same length, same structure, tokens in place of personal data. */\n readonly messages: M[];\n /** What was replaced where, without any of the replaced text. */\n readonly fields: readonly AuditFieldEntry[];\n /** Total number of replacements. */\n readonly replaced: number;\n /**\n * JSON Pointers of what was passed on without inspection: media payloads\n * (images, files, audio), members a provider needs byte for byte\n * (signatures, encrypted reasoning), `passthroughKeys`, and values that are\n * not JSON.\n */\n readonly unscanned: readonly string[];\n /** JSON Pointers of members whose key held personal data and was replaced by a token. */\n readonly flaggedKeys: readonly string[];\n /** Prompt size in model tokens before and after, when `countTokens` is configured. */\n readonly tokens?: { readonly before: number; readonly after: number };\n}\n\n/**\n * One guarded exchange with a model: a request and its response, or a whole\n * conversation. Everything sanitized through the same exchange shares one\n * token mapping, so a value keeps its token across turns.\n */\nexport interface LlmExchange {\n /** The session that holds the token mapping. */\n readonly session: SessionTokenizer;\n /** Replace personal data in a text with tokens. */\n readonly sanitizeText: (text: string) => string;\n /**\n * Replace personal data in chat messages with tokens, preserving their\n * structure. Structural members are left alone where the message formats\n * define them — `role`, `name`, identifiers and `type` on a message or a\n * content part, the function name of a tool call — and nowhere else: tool\n * arguments and tool results are sanitized at every depth, object keys\n * included. Of a media part the payload is passed on and listed in\n * `unscanned`; its text (title, context, file name, a text source) is\n * sanitized.\n *\n * @throws ValidationError When a message is not a plain object. A class\n * instance cannot be copied with tokens in place of its content: sanitize\n * the text before the message object is built, with `sanitizeText()`.\n */\n readonly sanitizeMessages: <M>(messages: readonly M[]) => SanitizedMessages<M>;\n /** Replace tokens in model output with the original values. */\n readonly restoreText: (text: string) => RestoreResult;\n /** A transform stream that restores tokens in streamed model output. */\n readonly restoreStream: () => TransformStream<string, string>;\n /** Restore tokens in model output delivered as an async iterable of text. */\n readonly restoreIterable: (\n source: AsyncIterable<string>,\n ) => AsyncGenerator<string, void, undefined>;\n /** A serialisable copy of the token mapping, to continue the conversation later. */\n readonly snapshot: () => SessionSnapshot;\n}\n\n/**\n * The redaction middleware.\n */\nexport interface LlmGuard {\n /**\n * Start an exchange.\n *\n * @param snapshot - The snapshot of an earlier exchange to continue from.\n */\n readonly begin: (snapshot?: SessionSnapshot) => LlmExchange;\n}\n\n/**\n * Create the redaction middleware for LLM calls.\n *\n * @param options - What to detect and how tokens look.\n * @returns The {@link LlmGuard}.\n * @throws {@link ValidationError} When the spec is invalid.\n *\n * @example\n * ```ts\n * import { createLlmGuard } from \"anonyma/ai\";\n *\n * const guard = createLlmGuard({ skipRoles: [\"system\"] });\n *\n * const exchange = guard.begin();\n * const { messages } = exchange.sanitizeMessages([\n * { role: \"system\", content: \"You are a support assistant.\" },\n * { role: \"user\", content: \"Email alice@example.com about invoice 42.\" },\n * ]);\n * // messages[1].content === \"Email [EMAIL_0001] about invoice 42.\"\n *\n * const stream = await llm.streamText(messages); // \"Sure, I wrote to [EMAIL_0001].\"\n * for await (const text of exchange.restoreIterable(stream)) {\n * process.stdout.write(text); // \"Sure, I wrote to alice@example.com.\"\n * }\n * ```\n */\nexport function createLlmGuard(options: LlmGuardOptions = {}): LlmGuard {\n const { skipRoles = [], keyRules, countTokens } = options;\n const passthrough = new Set(options.passthroughKeys ?? []);\n const exempt = (key: string, parent: Readonly<Record<string, unknown>>): boolean =>\n passthrough.has(key) || isOpaque(key, parent);\n const exemptInMedia = (key: string, parent: Readonly<Record<string, unknown>>): boolean =>\n passthrough.has(key) || isMediaPayload(key, parent);\n const sessionOptions = { lenient: true, ...options.session };\n const skip = new Set(skipRoles);\n\n // Fail on a bad spec now rather than on the first request.\n const probe = createSessionTokenizer(sessionOptions);\n const compile = (session: SessionTokenizer): Pipeline =>\n compilePipeline(\n { ...options.spec, defaultStrategy: { strategy: \"tokenize\" } },\n { tokenization: session },\n );\n compile(probe);\n\n function begin(snapshot?: SessionSnapshot): LlmExchange {\n const session = createSessionTokenizer({\n ...sessionOptions,\n ...(snapshot !== undefined ? { snapshot } : {}),\n });\n const pipeline = compile(session);\n\n function sanitizeText(text: string): string {\n return pipeline.transform(text).text;\n }\n\n function sanitizeMessages<M>(messages: readonly M[]): SanitizedMessages<M> {\n if (!Array.isArray(messages)) throw new ValidationError(\"messages\", \"must be an array\");\n const fields: AuditFieldEntry[] = [];\n const unscanned: string[] = [];\n const flaggedKeys: string[] = [];\n let replaced = 0;\n type Exempt = (key: string, parent: Readonly<Record<string, unknown>>) => boolean;\n\n /** Sanitize a value in full: every string at every depth. */\n const json = (value: unknown, path: string, skipMember: Exempt = exempt): unknown => {\n const result = sanitizeJson(value, {\n pipeline,\n jsonStringKeys: [\"arguments\"],\n skipMember,\n ...(keyRules !== undefined ? { keyRules } : {}),\n });\n replaced += result.replaced;\n for (const field of result.fields) fields.push({ ...field, path: `${path}${field.path}` });\n for (const skipped of result.skipped) unscanned.push(`${path}${skipped}`);\n for (const flagged of result.flaggedKeys) flaggedKeys.push(`${path}${flagged}`);\n return result.value;\n };\n\n type Entry = readonly [key: string, value: unknown];\n\n /**\n * Sanitize one member together with its key, so that key rules and\n * embedded JSON documents are recognised, and a key that holds personal\n * data is replaced. `siblings` is the object the member comes from,\n * which decides whether it is a payload.\n */\n const member = (\n key: string,\n siblings: Readonly<Record<string, unknown>>,\n parent: string,\n skipMember: Exempt = exempt,\n ): Entry => {\n const holder: Record<string, unknown> = {};\n setOwn(holder, key, siblings[key]);\n const judge: Exempt = (name, object) =>\n skipMember(name, object === holder ? siblings : object);\n const [entry] = Object.entries(json(holder, parent, judge) as Record<string, unknown>);\n return entry ?? [key, undefined];\n };\n\n /** Copy an object, keeping its structural members and handing the others to `handle`. */\n const copy = (\n source: Record<string, unknown>,\n keep: ReadonlySet<string>,\n handle: (key: string, value: unknown) => Entry,\n ): Record<string, unknown> => {\n const out: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(source)) {\n const [name, item] = keep.has(key) ? [key, value] : handle(key, value);\n setOwn(out, name, item);\n }\n return out;\n };\n\n const part = (value: unknown, path: string, depth: number): unknown => {\n if (!isPlainObject(value)) return json(value, path);\n if (depth >= MAX_DEPTH) throw new ValidationError(\"messages\", \"are nested too deeply\");\n // Of a media part the payload is passed on; what is written about it is not.\n const media = typeof value[\"type\"] === \"string\" && MEDIA_PARTS.has(value[\"type\"]);\n return copy(value, PART_KEEP, (key, item) =>\n key === \"content\"\n ? [key, content(item, `${path}/content`, depth + 1)]\n : member(key, value, path, media ? exemptInMedia : exempt),\n );\n };\n\n const content = (value: unknown, path: string, depth: number): unknown =>\n Array.isArray(value)\n ? (value as unknown[]).map((item, index) => part(item, `${path}/${String(index)}`, depth))\n : json(value, path);\n\n /** `{ name, arguments }`: the function name is structure, the arguments are data. */\n const call = (value: unknown, path: string): unknown =>\n isPlainObject(value)\n ? copy(value, FUNCTION_KEEP, (key) => member(key, value, path))\n : json(value, path);\n\n const out = (messages as readonly M[]).map((message, index) => {\n const path = `/${String(index)}`;\n if (!isPlainObject(message)) {\n if (typeof message === \"object\" && message !== null && !Array.isArray(message)) {\n throw new ValidationError(\n `messages[${String(index)}]`,\n \"is not a plain object and cannot be sanitized; sanitize its text with sanitizeText() before building it\",\n );\n }\n return json(message, path) as M;\n }\n if (typeof message[\"role\"] === \"string\" && skip.has(message[\"role\"])) return message;\n return copy(message, MESSAGE_KEEP, (key, value) => {\n if (key === \"content\") return [key, content(value, `${path}/content`, 0)];\n if (key === \"function_call\") return [key, call(value, `${path}/function_call`)];\n if (key === \"tool_calls\" && Array.isArray(value)) {\n const calls = (value as unknown[]).map((item, position) => {\n const itemPath = `${path}/tool_calls/${String(position)}`;\n if (!isPlainObject(item)) return json(item, itemPath);\n return copy(item, TOOL_CALL_KEEP, (callKey, callValue) =>\n callKey === \"function\"\n ? [callKey, call(callValue, `${itemPath}/function`)]\n : member(callKey, item, itemPath),\n );\n });\n return [key, calls];\n }\n return member(key, message, path);\n }) as M;\n });\n\n return {\n messages: out,\n fields,\n replaced,\n unscanned,\n flaggedKeys,\n ...(countTokens !== undefined\n ? {\n tokens: {\n before: countTokens(JSON.stringify(messages)),\n after: countTokens(JSON.stringify(out)),\n },\n }\n : {}),\n };\n }\n\n async function* restoreIterable(\n source: AsyncIterable<string>,\n ): AsyncGenerator<string, void, undefined> {\n const restorer = createRestoreTransformer(session);\n for await (const chunk of source) {\n const out = await restorer.push(chunk);\n if (out !== \"\") yield out;\n }\n const rest = await restorer.flush();\n if (rest !== \"\") yield rest;\n }\n\n return Object.freeze({\n session,\n sanitizeText,\n sanitizeMessages,\n restoreText: (text: string): RestoreResult => session.restore(text),\n restoreStream: (): TransformStream<string, string> => createRestoreStream(session),\n restoreIterable,\n snapshot: (): SessionSnapshot => session.snapshot(),\n });\n }\n\n return Object.freeze({ begin });\n}\n\n/**\n * The parts of a language-model middleware this adapter implements. The\n * shape matches `LanguageModelMiddleware` of the Vercel AI SDK structurally,\n * so the object can be passed to `wrapLanguageModel()` without this library\n * depending on the SDK.\n */\nexport interface LanguageModelMiddlewareLike {\n /** Sanitizes the prompt before it reaches the model. */\n readonly transformParams: (input: {\n readonly params: Record<string, unknown>;\n }) => Promise<Record<string, unknown>>;\n /** Restores tokens in a complete response. */\n readonly wrapGenerate: (input: {\n readonly doGenerate: () => PromiseLike<unknown>;\n readonly params: Record<string, unknown>;\n }) => Promise<unknown>;\n /** Restores tokens in a streamed response. */\n readonly wrapStream: (input: {\n readonly doStream: () => PromiseLike<{ readonly stream: ReadableStream<unknown> }>;\n readonly params: Record<string, unknown>;\n }) => Promise<{ readonly stream: ReadableStream<unknown> }>;\n}\n\n/** Members of a model result that hold a JSON document as text: the arguments of a tool call. */\nconst JSON_TEXT_KEYS: ReadonlySet<string> = new Set([\"input\", \"args\", \"arguments\"]);\n\n/** Carries the exchange from `transformParams` to the call that uses its result. */\nconst EXCHANGE = Symbol(\"anonyma.exchange\");\n\n/**\n * Restore tokens in every string of a model result. The arguments of a tool\n * call are a JSON document: values are restored into the parsed document, so\n * that no restored value can break it or add an argument.\n */\nasync function restoreDeep(\n value: unknown,\n exchange: LlmExchange,\n key?: string,\n depth = 0,\n): Promise<unknown> {\n if (typeof value === \"string\") {\n return key !== undefined && JSON_TEXT_KEYS.has(key)\n ? restoreJsonText(value, exchange.session)\n : exchange.restoreText(value).text;\n }\n if (typeof value !== \"object\" || value === null || depth > 64) return value;\n if (Array.isArray(value)) {\n const items: unknown[] = [];\n for (const item of value as unknown[]) {\n items.push(await restoreDeep(item, exchange, undefined, depth + 1));\n }\n return items;\n }\n const proto = Object.getPrototypeOf(value) as unknown;\n if (proto !== Object.prototype && proto !== null) return value;\n const out: Record<string, unknown> = {};\n for (const [name, member] of Object.entries(value)) {\n setOwn(out, name, await restoreDeep(member, exchange, name, depth + 1));\n }\n return out;\n}\n\n/**\n * Options accepted by {@link toLanguageModelMiddleware}.\n */\nexport interface LanguageModelMiddlewareOptions {\n /**\n * Called with the account of every sanitized prompt: what was replaced,\n * and above all what was passed on without inspection (`unscanned`). Throw\n * from it to stop a request that must not be sent.\n */\n readonly onSanitized?: (report: SanitizedMessages<unknown>) => void;\n}\n\n/**\n * Adapt a guard to the language-model middleware shape of the Vercel AI SDK.\n *\n * The prompt (`params.prompt`) is sanitized on the way in. On the way out,\n * tokens are restored in the result of `doGenerate()` and in the `text-delta`\n * and `tool-call` parts of `doStream()`; the arguments of a tool call are\n * restored as the JSON document they are. Partial tool input\n * (`tool-input-delta`) is passed on as it is. Each call gets its own exchange.\n *\n * @param guard - The guard to adapt.\n * @param options - A callback that receives the account of each sanitized prompt.\n * @returns An object to pass as `middleware` to `wrapLanguageModel()`.\n *\n * @remarks\n * The adapter is typed structurally and tested against stand-ins for the SDK,\n * not against the SDK itself. Check it against the SDK version you use.\n * `wrapGenerate` and `wrapStream` need the `params` object that\n * `transformParams` returned (a copy made with spread syntax works too) and\n * throw when they get another one: restoring nothing would go unnoticed.\n *\n * @example\n * ```ts\n * import { wrapLanguageModel } from \"ai\";\n * import { createLlmGuard, toLanguageModelMiddleware } from \"anonyma/ai\";\n *\n * const model = wrapLanguageModel({\n * model: baseModel,\n * middleware: toLanguageModelMiddleware(createLlmGuard()),\n * });\n * ```\n */\nexport function toLanguageModelMiddleware(\n guard: LlmGuard,\n options: LanguageModelMiddlewareOptions = {},\n): LanguageModelMiddlewareLike {\n const { onSanitized } = options;\n\n const exchangeOf = (params: Record<string, unknown>): LlmExchange => {\n const exchange = (params as { readonly [EXCHANGE]?: LlmExchange })[EXCHANGE];\n if (exchange === undefined) {\n throw new ValidationError(\n \"params\",\n \"did not come from transformParams(), so the tokens in the response cannot be restored\",\n );\n }\n return exchange;\n };\n\n /** Restore the parts of a stream that are not plain text deltas. */\n const restorePart = async (part: unknown, exchange: LlmExchange): Promise<unknown> => {\n if (!isPlainObject(part) || part[\"type\"] !== \"tool-call\") return part;\n return restoreDeep(part, exchange);\n };\n\n return Object.freeze({\n transformParams({\n params,\n }: {\n readonly params: Record<string, unknown>;\n }): Promise<Record<string, unknown>> {\n const exchange = guard.begin();\n const prompt = params[\"prompt\"];\n const transformed: Record<string, unknown> = { ...params };\n if (Array.isArray(prompt)) {\n const report = exchange.sanitizeMessages(prompt as unknown[]);\n onSanitized?.(report);\n transformed[\"prompt\"] = report.messages;\n } else if (typeof prompt === \"string\") {\n transformed[\"prompt\"] = exchange.sanitizeText(prompt);\n }\n // A symbol-keyed member survives a spread copy and is not serialised.\n Object.defineProperty(transformed, EXCHANGE, { value: exchange, enumerable: true });\n return Promise.resolve(transformed);\n },\n\n async wrapGenerate({\n doGenerate,\n params,\n }: {\n readonly doGenerate: () => PromiseLike<unknown>;\n readonly params: Record<string, unknown>;\n }): Promise<unknown> {\n const exchange = exchangeOf(params);\n return restoreDeep(await doGenerate(), exchange);\n },\n\n async wrapStream({\n doStream,\n params,\n }: {\n readonly doStream: () => PromiseLike<{ readonly stream: ReadableStream<unknown> }>;\n readonly params: Record<string, unknown>;\n }): Promise<{ readonly stream: ReadableStream<unknown> }> {\n const exchange = exchangeOf(params);\n const result = await doStream();\n\n async function* parts(): AsyncGenerator<unknown, void, undefined> {\n const reader = result.stream.getReader();\n try {\n for (;;) {\n const { value, done } = await reader.read();\n if (done) return;\n yield value;\n }\n } finally {\n reader.releaseLock();\n }\n }\n const lens = {\n get: (part: unknown): string | undefined =>\n isPlainObject(part) ? textDeltaLens.get(part) : undefined,\n set: (part: unknown, text: string): unknown =>\n textDeltaLens.set(part as Record<string, unknown>, text),\n block: (part: unknown): string | undefined =>\n isPlainObject(part) ? textDeltaLens.block?.(part) : undefined,\n };\n // Held text is delivered before the next part without text, so nothing\n // arrives after the part that ends its block.\n const restored = restoreChunks(parts(), lens, exchange.session);\n\n const stream = new ReadableStream<unknown>({\n async pull(controller): Promise<void> {\n for (;;) {\n const next = await restored.next();\n if (next.done === true) {\n controller.close();\n return;\n }\n const part = next.value;\n const text = lens.get(part);\n // A delta whose text is all held back for now is not worth a part.\n if (text === \"\") continue;\n controller.enqueue(text === undefined ? await restorePart(part, exchange) : part);\n return;\n }\n },\n async cancel(reason): Promise<void> {\n await restored.return();\n await result.stream.cancel(reason);\n },\n });\n return { ...result, stream };\n },\n });\n}\n"]}
|