@cloakapi/sdk 1.5.0 → 1.6.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.
Files changed (34) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +21 -1
  3. package/dist/browser/index.d.mts +3 -2
  4. package/dist/browser/index.d.ts +3 -2
  5. package/dist/browser/index.js +1 -1
  6. package/dist/browser/index.mjs +8 -7
  7. package/dist/browser/light.d.mts +2 -1
  8. package/dist/browser/light.d.ts +2 -1
  9. package/dist/browser/light.mjs +1 -1
  10. package/dist/browser/names/public.mjs +2 -2
  11. package/dist/browser/ner-pipeline-worker.mjs +3 -3
  12. package/dist/{chunk-G6TZ6722.mjs → chunk-6E6T2YO5.mjs} +5 -915
  13. package/dist/{chunk-6TMC2A6M.mjs → chunk-6JZLCPZ7.mjs} +1 -1
  14. package/dist/{chunk-36BC4GYL.mjs → chunk-7OIXV4CE.mjs} +22 -20
  15. package/dist/{chunk-JV52VGVZ.mjs → chunk-JCTZ572R.mjs} +1 -1
  16. package/dist/chunk-L7RLVZMD.mjs +922 -0
  17. package/dist/device/index.d.mts +1448 -0
  18. package/dist/device/index.d.ts +1448 -0
  19. package/dist/device/index.js +4539 -0
  20. package/dist/device/index.mjs +3706 -0
  21. package/dist/{disclosure-Cx6xS8Tj.d.mts → disclosure-Cslh-RaS.d.mts} +2 -12
  22. package/dist/{disclosure-Cx6xS8Tj.d.ts → disclosure-Cslh-RaS.d.ts} +2 -12
  23. package/dist/index.d.mts +3 -2
  24. package/dist/index.d.ts +3 -2
  25. package/dist/index.js +1 -1
  26. package/dist/index.mjs +14 -12
  27. package/dist/{local-pii-supplement-SUOJZO7X.mjs → local-pii-supplement-5KFFPDDN.mjs} +4 -4
  28. package/dist/node.d.mts +2 -1
  29. package/dist/node.d.ts +2 -1
  30. package/dist/node.mjs +10 -9
  31. package/dist/request-body-slots-BMShK1YO.d.mts +56 -0
  32. package/dist/request-body-slots-BMShK1YO.d.ts +56 -0
  33. package/package.json +9 -2
  34. package/dist/{chunk-PEDVEDAW.mjs → chunk-66SEKCCN.mjs} +8 -8
@@ -1,3 +1,8 @@
1
+ import {
2
+ assertRequestBodyReviewable,
3
+ assertRequestBodySlotReviewable,
4
+ collectRequestBodyTextSlots
5
+ } from "./chunk-L7RLVZMD.mjs";
1
6
  import {
2
7
  findCardSpans,
3
8
  firstSpanIndexEndingAfter,
@@ -1001,910 +1006,6 @@ function lowerCoverage(a, b) {
1001
1006
  return COVERAGE_RANK[a] <= COVERAGE_RANK[b] ? a : b;
1002
1007
  }
1003
1008
 
1004
- // src/request-body-slots.ts
1005
- function deepClone(v, seen) {
1006
- if (v === null || typeof v !== "object") return v;
1007
- const bag = seen ?? /* @__PURE__ */ new WeakSet();
1008
- if (bag.has(v)) {
1009
- throw new RequestBodyGuardError("request body contains a cycle");
1010
- }
1011
- bag.add(v);
1012
- try {
1013
- if (Array.isArray(v)) return v.map((x) => deepClone(x, bag));
1014
- if (v instanceof Map) {
1015
- return new Map([...v.entries()].map(([key, value]) => [key, deepClone(value, bag)]));
1016
- }
1017
- const out = {};
1018
- for (const k of Object.keys(v)) {
1019
- out[k] = deepClone(v[k], bag);
1020
- }
1021
- return out;
1022
- } finally {
1023
- bag.delete(v);
1024
- }
1025
- }
1026
- var RequestBodyGuardError = class extends Error {
1027
- constructor(message) {
1028
- super(message);
1029
- this.code = "unreviewable_request_body";
1030
- this.name = "RequestBodyGuardError";
1031
- }
1032
- };
1033
- function slotIsLeftAsWritten(slot, value) {
1034
- return slot.policy === "reject-if-changed" || /^data:/i.test(value.trim());
1035
- }
1036
- function assertRequestBodySlotReviewable(slot, value = slot.get(), strictEncodedProse = true) {
1037
- if (slot.policy === "json-argument" || slot.policy === "json-number" || slot.policy === "reject-if-changed") return;
1038
- if (typeof value === "string" && /^data:/i.test(value.trim())) return;
1039
- if (!strictEncodedProse && typeof value === "string" && isStandaloneEncodedProse(value, slot.path)) return;
1040
- assertReviewableTextSlot(value, slot.path);
1041
- }
1042
- function scanJsonArgumentLeaves(text) {
1043
- try {
1044
- JSON.parse(text);
1045
- } catch {
1046
- return void 0;
1047
- }
1048
- const leaves = [];
1049
- for (let index = 0; index < text.length; ) {
1050
- const char = text[index];
1051
- if (char === '"') {
1052
- const start = index;
1053
- index += 1;
1054
- while (index < text.length) {
1055
- if (text[index] === "\\") {
1056
- index += 2;
1057
- } else if (text[index] === '"') {
1058
- index += 1;
1059
- break;
1060
- } else {
1061
- index += 1;
1062
- }
1063
- }
1064
- const end = index;
1065
- let after = end;
1066
- while (after < text.length && /\s/.test(text[after])) after += 1;
1067
- if (text[after] === ":") continue;
1068
- const value = JSON.parse(text.slice(start, end));
1069
- if (typeof value === "string") leaves.push({ start, end, original: value, kind: "string", current: value });
1070
- continue;
1071
- }
1072
- if (char === "-" || char >= "0" && char <= "9") {
1073
- const start = index;
1074
- if (text[index] === "-") index += 1;
1075
- if (text[index] === "0") {
1076
- index += 1;
1077
- } else {
1078
- while (index < text.length && text[index] >= "0" && text[index] <= "9") index += 1;
1079
- }
1080
- if (text[index] === ".") {
1081
- index += 1;
1082
- while (index < text.length && text[index] >= "0" && text[index] <= "9") index += 1;
1083
- }
1084
- if (text[index] === "e" || text[index] === "E") {
1085
- index += 1;
1086
- if (text[index] === "+" || text[index] === "-") index += 1;
1087
- while (index < text.length && text[index] >= "0" && text[index] <= "9") index += 1;
1088
- }
1089
- const end = index;
1090
- const original = text.slice(start, end);
1091
- leaves.push({ start, end, original, kind: "number", current: original });
1092
- continue;
1093
- }
1094
- index += 1;
1095
- }
1096
- return leaves;
1097
- }
1098
- function renderJsonArgumentLeaves(text, leaves) {
1099
- let output = "";
1100
- let offset = 0;
1101
- for (const leaf of leaves) {
1102
- output += text.slice(offset, leaf.start);
1103
- output += leaf.current === leaf.original ? text.slice(leaf.start, leaf.end) : JSON.stringify(leaf.current);
1104
- offset = leaf.end;
1105
- }
1106
- return output + text.slice(offset);
1107
- }
1108
- function transformJsonArgumentText(text, transform) {
1109
- const leaves = scanJsonArgumentLeaves(text);
1110
- if (!leaves) return void 0;
1111
- for (const leaf of leaves) leaf.current = transform(leaf.original);
1112
- return renderJsonArgumentLeaves(text, leaves);
1113
- }
1114
- function maskJsonArgumentKeys(text) {
1115
- try {
1116
- JSON.parse(text);
1117
- } catch {
1118
- return text;
1119
- }
1120
- let output;
1121
- let offset = 0;
1122
- for (let index = 0; index < text.length; ) {
1123
- if (text[index] !== '"') {
1124
- index += 1;
1125
- continue;
1126
- }
1127
- const start = index;
1128
- index += 1;
1129
- while (index < text.length) {
1130
- if (text[index] === "\\") index += 2;
1131
- else if (text[index] === '"') {
1132
- index += 1;
1133
- break;
1134
- } else index += 1;
1135
- }
1136
- let after = index;
1137
- while (after < text.length && /\s/.test(text[after])) after += 1;
1138
- if (text[after] !== ":") continue;
1139
- output ?? (output = "");
1140
- output += `${text.slice(offset, start)}"__argument_key__"`;
1141
- offset = index;
1142
- }
1143
- return output === void 0 ? text : output + text.slice(offset);
1144
- }
1145
- function isToolArgumentPlace(trail, key, holder) {
1146
- if (trail[0] !== "messages" || trail[1] !== "[]") return false;
1147
- const rest = trail.slice(2);
1148
- if (key === "arguments") {
1149
- if (rest.length === 3 && rest[0] === "tool_calls" && rest[1] === "[]" && rest[2] === "function") return true;
1150
- return rest.length === 1 && rest[0] === "function_call";
1151
- }
1152
- if (key !== "input") return false;
1153
- const toolUse = holder.type === "tool_use" || holder.type === "server_tool_use" || holder.type === "mcp_tool_use";
1154
- return toolUse && rest.length >= 2 && rest.length % 2 === 0 && rest.every((segment, index) => index % 2 === 0 ? segment === "content" : segment === "[]");
1155
- }
1156
- function maskArgumentKeysForResidualCheck(root) {
1157
- const visit = (value, inArguments, trail) => {
1158
- if (typeof value === "string") return inArguments ? maskJsonArgumentKeys(value) : value;
1159
- if (Array.isArray(value)) return value.map((child) => visit(child, inArguments, [...trail, "[]"]));
1160
- if (!isRecord(value)) return value;
1161
- const output = {};
1162
- let argumentKeyIndex = 0;
1163
- for (const key of Object.keys(value)) {
1164
- const argumentData = inArguments || isToolArgumentPlace(trail, key, value);
1165
- const outputKey = inArguments ? `__argument_key_${argumentKeyIndex++}__` : key;
1166
- output[outputKey] = visit(value[key], argumentData, [...trail, key]);
1167
- }
1168
- return output;
1169
- };
1170
- return visit(root, false, []);
1171
- }
1172
- function classifyKeyReplacement(key, replaced, value, output, personal, state) {
1173
- const detected = personal ? personal(key) : replaced !== key;
1174
- const free = replaced !== key && !Object.prototype.hasOwnProperty.call(value, replaced) && !Object.prototype.hasOwnProperty.call(output, replaced);
1175
- if (detected && free) {
1176
- state.changed = true;
1177
- state.renamed = true;
1178
- return replaced;
1179
- }
1180
- if (detected && replaced !== key) state.collided = true;
1181
- else if (detected) state.unchanged = true;
1182
- return key;
1183
- }
1184
- function replaceObjectKeys(root, replace, personal) {
1185
- const state = { changed: false, renamed: false, collided: false, unchanged: false };
1186
- const visit = (value2, inArguments, trail) => {
1187
- if (Array.isArray(value2)) return value2.map((child) => visit(child, inArguments, [...trail, "[]"]));
1188
- if (!isRecord(value2)) return value2;
1189
- const output = {};
1190
- for (const key of Object.keys(value2)) {
1191
- const argumentData = inArguments || isToolArgumentPlace(trail, key, value2);
1192
- const inMetadata = trail.includes("metadata");
1193
- const outputKey = inArguments || inMetadata ? key : classifyKeyReplacement(key, replace(key), value2, output, personal, state);
1194
- Object.defineProperty(output, outputKey, {
1195
- value: visit(value2[key], argumentData, [...trail, key]),
1196
- enumerable: true,
1197
- configurable: true,
1198
- writable: true
1199
- });
1200
- }
1201
- return output;
1202
- };
1203
- const value = visit(root, false, []);
1204
- return { value: state.changed ? value : root, ...state };
1205
- }
1206
- async function replaceObjectKeysAsync(root, replace, personal) {
1207
- const state = { changed: false, renamed: false, collided: false, unchanged: false };
1208
- const visit = async (value2, inArguments, trail) => {
1209
- if (Array.isArray(value2)) {
1210
- return Promise.all(value2.map((child) => visit(child, inArguments, [...trail, "[]"])));
1211
- }
1212
- if (!isRecord(value2)) return value2;
1213
- const output = {};
1214
- for (const key of Object.keys(value2)) {
1215
- const argumentData = inArguments || isToolArgumentPlace(trail, key, value2);
1216
- const inMetadata = trail.includes("metadata");
1217
- const outputKey = inArguments || inMetadata ? key : classifyKeyReplacement(key, await replace(key), value2, output, personal, state);
1218
- Object.defineProperty(output, outputKey, {
1219
- value: await visit(value2[key], argumentData, [...trail, key]),
1220
- enumerable: true,
1221
- configurable: true,
1222
- writable: true
1223
- });
1224
- }
1225
- return output;
1226
- };
1227
- const value = await visit(root, false, []);
1228
- return { value: state.changed ? value : root, ...state };
1229
- }
1230
- var isRecord = (v) => !!v && typeof v === "object" && !Array.isArray(v);
1231
- var CONTENT_PART_KEYS_KEPT = /* @__PURE__ */ new Set([
1232
- "text",
1233
- "name",
1234
- "function_call",
1235
- "audio",
1236
- "input_audio",
1237
- "thinking",
1238
- "content",
1239
- "input",
1240
- "source",
1241
- "type",
1242
- "id",
1243
- "tool_use_id",
1244
- "tool_call_id",
1245
- "file_id",
1246
- "signature",
1247
- "cache_control",
1248
- "is_error",
1249
- "encrypted_content",
1250
- "encrypted_index",
1251
- "image_url",
1252
- "file"
1253
- ]);
1254
- var KNOWN_CONTENT_BLOCK_TYPES = /* @__PURE__ */ new Set([
1255
- "text",
1256
- "image",
1257
- "document",
1258
- "tool_use",
1259
- "tool_result",
1260
- "server_tool_use",
1261
- "mcp_tool_use",
1262
- "thinking",
1263
- "redacted_thinking",
1264
- "input_text",
1265
- "input_image",
1266
- "input_audio",
1267
- "input_file",
1268
- "output_text",
1269
- "refusal",
1270
- "image_url",
1271
- "file",
1272
- "audio",
1273
- "output_audio",
1274
- "tool_call",
1275
- "function_call"
1276
- ]);
1277
- function collectRequestBodyTextSlots(root) {
1278
- const slots = [];
1279
- const visited = /* @__PURE__ */ new WeakSet();
1280
- const push = (holder, key, path, policy = "tokenise") => {
1281
- const cur = holder[key];
1282
- if (typeof cur !== "string" || cur.length === 0) return;
1283
- slots.push({
1284
- path,
1285
- policy,
1286
- get: () => holder[key],
1287
- set: (v) => {
1288
- holder[key] = v;
1289
- }
1290
- });
1291
- };
1292
- const pushArrayItem = (arr, i, path, policy = "tokenise") => {
1293
- const cur = arr[i];
1294
- if (typeof cur !== "string" || cur.length === 0) return;
1295
- slots.push({
1296
- path,
1297
- policy,
1298
- get: () => arr[i],
1299
- set: (v) => {
1300
- arr[i] = v;
1301
- }
1302
- });
1303
- };
1304
- const pushArrayNumber = (arr, i, path) => {
1305
- const original = arr[i];
1306
- if (typeof original !== "number" || !Number.isFinite(original)) return;
1307
- slots.push({
1308
- path,
1309
- policy: "json-number",
1310
- get: () => String(arr[i]),
1311
- set: (next) => {
1312
- if (next !== String(original)) arr[i] = next;
1313
- }
1314
- });
1315
- };
1316
- const pushNumber = (holder, key, path) => {
1317
- const original = holder[key];
1318
- if (typeof original !== "number" || !Number.isFinite(original)) return;
1319
- slots.push({
1320
- path,
1321
- policy: "json-number",
1322
- get: () => String(holder[key]),
1323
- set: (next) => {
1324
- if (next !== String(original)) holder[key] = next;
1325
- }
1326
- });
1327
- };
1328
- const jsonArgumentLeaves = (v, path, seen = /* @__PURE__ */ new WeakSet()) => {
1329
- if (v && typeof v === "object") {
1330
- if (seen.has(v)) return;
1331
- seen.add(v);
1332
- }
1333
- if (Array.isArray(v)) {
1334
- v.forEach((value, i) => {
1335
- if (typeof value === "string") {
1336
- slots.push({
1337
- path: `${path}[${i}]`,
1338
- policy: "tokenise",
1339
- get: () => v[i],
1340
- set: (next) => {
1341
- v[i] = next;
1342
- }
1343
- });
1344
- } else if (typeof value !== "number") jsonArgumentLeaves(value, `${path}[${i}]`, seen);
1345
- });
1346
- return;
1347
- }
1348
- if (!isRecord(v)) return;
1349
- for (const key of Object.keys(v)) {
1350
- const value = v[key];
1351
- if (typeof value === "string") {
1352
- slots.push({
1353
- path: `${path}.${key}`,
1354
- policy: "tokenise",
1355
- get: () => v[key],
1356
- set: (next) => {
1357
- v[key] = next;
1358
- }
1359
- });
1360
- } else if (typeof value !== "number") jsonArgumentLeaves(value, `${path}.${key}`, seen);
1361
- }
1362
- };
1363
- const jsonArgument = (holder, key, path) => {
1364
- const raw = holder[key];
1365
- if (typeof raw === "string") {
1366
- if (raw.length === 0) return;
1367
- const leaves = scanJsonArgumentLeaves(raw);
1368
- if (!leaves) {
1369
- push(holder, key, path);
1370
- return;
1371
- }
1372
- for (const leaf of leaves) {
1373
- if (leaf.kind === "number") continue;
1374
- slots.push({
1375
- path,
1376
- policy: "json-argument",
1377
- get: () => leaf.current,
1378
- set: (next) => {
1379
- leaf.current = next;
1380
- holder[key] = renderJsonArgumentLeaves(raw, leaves);
1381
- }
1382
- });
1383
- }
1384
- return;
1385
- }
1386
- if (Array.isArray(raw) || isRecord(raw)) jsonArgumentLeaves(raw, path);
1387
- };
1388
- const numericLeaf = (holder, key, path) => {
1389
- const raw = holder[key];
1390
- if (typeof raw !== "number" || !Number.isFinite(raw) || !Number.isInteger(raw)) return;
1391
- const digits = String(Math.trunc(Math.abs(raw))).replace(/\D/g, "");
1392
- if (digits.length >= 6 && digits.length <= 16) pushNumber(holder, key, path);
1393
- };
1394
- const jsonLeaves = (v, path) => {
1395
- if (v && typeof v === "object") {
1396
- if (visited.has(v)) return;
1397
- visited.add(v);
1398
- }
1399
- if (Array.isArray(v)) {
1400
- v.forEach((_, i) => {
1401
- pushArrayItem(v, i, `${path}[${i}]`);
1402
- jsonLeaves(v[i], `${path}[${i}]`);
1403
- });
1404
- return;
1405
- }
1406
- if (v instanceof Map) {
1407
- let entryIndex = 0;
1408
- for (const [key, value] of v.entries()) {
1409
- const entryPath = `${path}[${entryIndex}]`;
1410
- entryIndex += 1;
1411
- if (typeof value === "string" && value.length > 0) {
1412
- slots.push({
1413
- path: entryPath,
1414
- policy: "tokenise",
1415
- get: () => v.get(key),
1416
- set: (next) => {
1417
- v.set(key, next);
1418
- }
1419
- });
1420
- } else {
1421
- jsonLeaves(value, entryPath);
1422
- }
1423
- }
1424
- return;
1425
- }
1426
- if (!isRecord(v)) return;
1427
- for (const k of Object.keys(v)) {
1428
- push(v, k, `${path}.${k}`);
1429
- jsonLeaves(v[k], `${path}.${k}`);
1430
- }
1431
- };
1432
- const schemaLiteral = (v, path) => {
1433
- if (typeof v === "string") {
1434
- const holder = { value: v };
1435
- push(holder, "value", path, "reject-if-changed");
1436
- return;
1437
- }
1438
- if (Array.isArray(v)) {
1439
- v.forEach((x, i) => schemaLiteral(x, `${path}[${i}]`));
1440
- return;
1441
- }
1442
- if (!isRecord(v)) return;
1443
- for (const k of Object.keys(v)) schemaLiteral(v[k], `${path}.${k}`);
1444
- };
1445
- const schemaProse = (v, path) => {
1446
- if (Array.isArray(v)) {
1447
- v.forEach((x, i) => schemaProse(x, `${path}[${i}]`));
1448
- return;
1449
- }
1450
- if (!isRecord(v)) return;
1451
- push(v, "description", `${path}.description`);
1452
- push(v, "title", `${path}.title`);
1453
- if (Array.isArray(v.examples)) {
1454
- const ex = v.examples;
1455
- ex.forEach((_, i) => pushArrayItem(ex, i, `${path}.examples[${i}]`));
1456
- }
1457
- for (const k of Object.keys(v)) {
1458
- if (k === "description" || k === "title" || k === "examples") continue;
1459
- if (k === "enum" || k === "const" || k === "default") {
1460
- schemaLiteral(v[k], `${path}.${k}`);
1461
- } else {
1462
- schemaProse(v[k], `${path}.${k}`);
1463
- }
1464
- }
1465
- };
1466
- const content = (holder, key, path) => {
1467
- const c = holder[key];
1468
- if (typeof c === "string") {
1469
- push(holder, key, path);
1470
- return;
1471
- }
1472
- if (c instanceof Map || isRecord(c)) {
1473
- jsonLeaves(c, path);
1474
- return;
1475
- }
1476
- if (!Array.isArray(c)) return;
1477
- c.forEach((part, i) => {
1478
- if (!isRecord(part)) return;
1479
- if (part.type === "thinking" || part.type === "redacted_thinking") return;
1480
- push(part, "text", `${path}[${i}].text`);
1481
- if (part.type !== "tool_use" && part.type !== "server_tool_use" && part.type !== "mcp_tool_use") {
1482
- push(part, "name", `${path}[${i}].name`);
1483
- }
1484
- if (isRecord(part.function_call)) jsonLeaves(part.function_call, `${path}[${i}].function_call`);
1485
- if (isRecord(part.audio)) jsonLeaves(part.audio, `${path}[${i}].audio`);
1486
- if (isRecord(part.input_audio)) jsonLeaves(part.input_audio, `${path}[${i}].input_audio`);
1487
- if (part.content !== void 0) content(part, "content", `${path}[${i}].content`);
1488
- if (isRecord(part.input)) jsonLeaves(part.input, `${path}[${i}].input`);
1489
- if (isRecord(part.source) && part.source.type === "text") {
1490
- push(part.source, "data", `${path}[${i}].source.data`);
1491
- }
1492
- if (isRecord(part.source) && part.source.type === "content" && part.source.content !== void 0) {
1493
- content(part.source, "content", `${path}[${i}].source.content`);
1494
- }
1495
- if (typeof part.source === "string") push(part, "source", `${path}[${i}].source`);
1496
- for (const key2 of Object.keys(part)) {
1497
- if (CONTENT_PART_KEYS_KEPT.has(key2)) continue;
1498
- const value = part[key2];
1499
- if (typeof value === "string") push(part, key2, `${path}[${i}].${key2}`);
1500
- else if (value !== null && typeof value === "object") jsonLeaves(value, `${path}[${i}].${key2}`);
1501
- }
1502
- });
1503
- };
1504
- const systemBlock = (part, path) => {
1505
- push(part, "text", `${path}.text`);
1506
- if (part.content !== void 0) content(part, "content", `${path}.content`);
1507
- };
1508
- const attachments = (a, path) => {
1509
- if (!Array.isArray(a)) return;
1510
- a.forEach((att, i) => {
1511
- if (isRecord(att)) {
1512
- push(att, "text", `${path}[${i}].text`);
1513
- push(att, "url", `${path}[${i}].url`, "reject-if-changed");
1514
- push(att, "name", `${path}[${i}].name`);
1515
- push(att, "filename", `${path}[${i}].filename`);
1516
- }
1517
- });
1518
- };
1519
- if (!isRecord(root)) return slots;
1520
- push(root, "preprompt", "preprompt");
1521
- push(root, "prompt", "prompt");
1522
- if (typeof root.input === "string") {
1523
- push(root, "input", "input");
1524
- } else if (Array.isArray(root.input)) {
1525
- const stringForm = root.input.every((value) => typeof value === "string");
1526
- const tokenIdForm = root.input.every((value) => Number.isInteger(value) || Array.isArray(value) && value.every((token) => Number.isInteger(token)));
1527
- if (stringForm) {
1528
- root.input.forEach((_, i) => pushArrayItem(root.input, i, `input[${i}]`));
1529
- } else if (!tokenIdForm) {
1530
- throw new RequestBodyGuardError("structured input body cannot be reviewed safely");
1531
- }
1532
- } else if (root.input !== void 0 && typeof root.input !== "number") {
1533
- throw new RequestBodyGuardError("input body cannot be reviewed safely");
1534
- }
1535
- jsonLeaves(root.metadata, "metadata");
1536
- if (typeof root.user === "string") push(root, "user", "user");
1537
- else if (root.user !== void 0) jsonLeaves(root.user, "user");
1538
- if (typeof root.system === "string") push(root, "system", "system");
1539
- else if (root.system instanceof Map) jsonLeaves(root.system, "system");
1540
- else if (Array.isArray(root.system)) {
1541
- root.system.forEach((part, i) => {
1542
- if (part instanceof Map) jsonLeaves(part, `system[${i}]`);
1543
- else if (isRecord(part)) systemBlock(part, `system[${i}]`);
1544
- });
1545
- } else if (isRecord(root.system)) systemBlock(root.system, "system");
1546
- push(root, "clipboard", "clipboard");
1547
- attachments(root.attachments, "attachments");
1548
- if (Array.isArray(root.messages)) {
1549
- root.messages.forEach((m, i) => {
1550
- if (!isRecord(m)) return;
1551
- if ("content" in m) content(m, "content", `messages[${i}].content`);
1552
- attachments(m.attachments, `messages[${i}].attachments`);
1553
- jsonLeaves(m.metadata, `messages[${i}].metadata`);
1554
- if (Array.isArray(m.tool_calls)) {
1555
- m.tool_calls.forEach((call, j) => {
1556
- if (!isRecord(call)) return;
1557
- const fn = call.function;
1558
- if (isRecord(fn)) {
1559
- jsonArgument(fn, "arguments", `messages[${i}].tool_calls[${j}].function.arguments`);
1560
- }
1561
- });
1562
- }
1563
- if (isRecord(m.function_call)) {
1564
- const call = m.function_call;
1565
- jsonArgument(call, "arguments", `messages[${i}].function_call.arguments`);
1566
- for (const [key, value] of Object.entries(call)) {
1567
- if (key !== "name" && key !== "arguments") jsonLeaves(value, `messages[${i}].function_call.${key}`);
1568
- }
1569
- }
1570
- if (isRecord(m.audio)) jsonLeaves(m.audio, `messages[${i}].audio`);
1571
- if (isRecord(m.input_audio)) jsonLeaves(m.input_audio, `messages[${i}].input_audio`);
1572
- });
1573
- }
1574
- if (Array.isArray(root.tools)) {
1575
- root.tools.forEach((t, i) => {
1576
- if (!isRecord(t)) return;
1577
- push(t, "description", `tools[${i}].description`);
1578
- if (t.input_schema !== void 0) schemaProse(t.input_schema, `tools[${i}].input_schema`);
1579
- const fn = t.function;
1580
- if (isRecord(fn)) {
1581
- push(fn, "description", `tools[${i}].function.description`);
1582
- if (fn.parameters !== void 0) schemaProse(fn.parameters, `tools[${i}].function.parameters`);
1583
- }
1584
- });
1585
- }
1586
- return slots;
1587
- }
1588
- function rejectBareTextBlocks(node) {
1589
- if (!isRecord(node)) return;
1590
- if (Array.isArray(node.content)) {
1591
- for (const part of node.content) {
1592
- if (typeof part === "string") {
1593
- throw new RequestBodyGuardError("content array entry is not a reviewable block");
1594
- }
1595
- if (isRecord(part)) rejectBareTextBlocks(part);
1596
- }
1597
- }
1598
- if (Array.isArray(node.system)) {
1599
- for (const part of node.system) {
1600
- if (typeof part === "string") {
1601
- throw new RequestBodyGuardError("system array entry is not a reviewable block");
1602
- }
1603
- if (isRecord(part)) rejectBareTextBlocks(part);
1604
- }
1605
- }
1606
- if (Array.isArray(node.messages)) {
1607
- for (const message of node.messages) rejectBareTextBlocks(message);
1608
- }
1609
- }
1610
- function assertAcyclic(root) {
1611
- const seen = /* @__PURE__ */ new WeakSet();
1612
- const walk = (value) => {
1613
- if (value === null || typeof value !== "object") return;
1614
- if (seen.has(value)) throw new RequestBodyGuardError("request body contains a cycle");
1615
- seen.add(value);
1616
- try {
1617
- if (Array.isArray(value)) {
1618
- for (const item of value) walk(item);
1619
- return;
1620
- }
1621
- if (value instanceof Map) {
1622
- for (const entry of value.values()) walk(entry);
1623
- return;
1624
- }
1625
- const record = value;
1626
- for (const key of Object.keys(record)) walk(record[key]);
1627
- } finally {
1628
- seen.delete(value);
1629
- }
1630
- };
1631
- walk(root);
1632
- }
1633
- function assertRequestBodyReviewable(root) {
1634
- assertAcyclic(root);
1635
- rejectBareTextBlocks(root);
1636
- }
1637
- var PLACEMENT_TOOL_KEY = "A key inside tool-call arguments looks like personal data. Keys there follow the tool's schema, so it was not replaced. It is sent as written.";
1638
- var PLACEMENT_SCHEMA = "A tool schema holds a fixed value or property name that looks like personal data. Replacing it would change the tool's contract, so it was not replaced. It is sent as written.";
1639
- var PLACEMENT_NUMBER = "A number looks like personal data. A number cannot be replaced without changing its type, so it was not replaced. It is sent as written.";
1640
- var PLACEMENT_MEDIA = "This request includes an image, audio clip or file that this device did not read. It is sent as provided.";
1641
- var PLACEMENT_METADATA_KEY_REPLACED = "A personal detail in a key was replaced with a stand-in.";
1642
- var PLACEMENT_METADATA_KEY_LEFT = "A personal detail in a metadata key was left as written. Metadata keys stay as written, so it was not replaced. It is sent as written.";
1643
- var PLACEMENT_METADATA_KEY_LEFT_TAKEN = "A personal detail in a key was left as written because another field already uses that name, so the replacement was not applied.";
1644
- var PLACEMENT_METADATA_KEY_LEFT_UNCHANGED = "A personal detail in a key was left as written because the replacement was the same text.";
1645
- var PLACEMENT_TOOL_NAME = "A tool name or tool_choice looks like personal data. Those identifiers are not replaced. It is sent as written.";
1646
- var PLACEMENT_UNKNOWN_BLOCK = "This request includes a content block of a type this SDK does not know. Its text was checked like other text and it is sent.";
1647
- var PLACEMENT_ENCODED = "A value looks like encoded data. This device did not decode it, so it was not replaced. It is sent as written.";
1648
- function looksLikeLongId(value) {
1649
- if (!Number.isFinite(value) || !Number.isInteger(value)) return false;
1650
- const digits = String(Math.trunc(Math.abs(value))).replace(/\D/g, "");
1651
- return digits.length >= 6 && digits.length <= 16;
1652
- }
1653
- function metadataKeyOutcomeWarnings(outcome) {
1654
- const warnings = [];
1655
- if (outcome.renamed) warnings.push({ reason: "value_not_replaced", message: PLACEMENT_METADATA_KEY_REPLACED });
1656
- if (outcome.collided) warnings.push({ reason: "value_not_replaced", message: PLACEMENT_METADATA_KEY_LEFT_TAKEN });
1657
- if (outcome.unchanged) warnings.push({ reason: "value_not_replaced", message: PLACEMENT_METADATA_KEY_LEFT_UNCHANGED });
1658
- return warnings;
1659
- }
1660
- function collectValuePlacementWarnings(root, personal, options) {
1661
- let toolKey = false;
1662
- let schema = false;
1663
- let number = false;
1664
- let media = false;
1665
- let metadataKey = false;
1666
- let toolName = false;
1667
- let encoded = false;
1668
- const allowedTopLevel = /* @__PURE__ */ new Set(["max_tokens", "temperature", "top_p", "n"]);
1669
- const noteMediaRecord = (value) => {
1670
- const source = value.source;
1671
- if (isRecord(source) && source.type !== "text") {
1672
- if (typeof source.data === "string" && source.data !== "") media = true;
1673
- if (typeof source.url === "string" && source.url !== "") media = true;
1674
- }
1675
- const imageUrl = value.image_url;
1676
- if (isRecord(imageUrl) && typeof imageUrl.url === "string" && imageUrl.url !== "") media = true;
1677
- const file = value.file;
1678
- if (isRecord(file) && typeof file.file_data === "string" && file.file_data !== "") media = true;
1679
- };
1680
- const noteAttachments = (value) => {
1681
- if (!Array.isArray(value)) return;
1682
- for (const item of value) {
1683
- if (!isRecord(item)) continue;
1684
- if (item.data !== void 0 && item.data !== null && item.data !== "") media = true;
1685
- if (typeof item.url === "string" && item.url !== "") media = true;
1686
- }
1687
- };
1688
- const walkArgs = (value) => {
1689
- if (Array.isArray(value)) {
1690
- value.forEach((item) => walkArgs(item));
1691
- return;
1692
- }
1693
- if (!isRecord(value)) return;
1694
- for (const key of Object.keys(value)) {
1695
- if (personal(key)) toolKey = true;
1696
- walkArgs(value[key]);
1697
- }
1698
- };
1699
- const probeLiteral = (value) => {
1700
- if (typeof value === "string") {
1701
- if (personal(value)) schema = true;
1702
- return;
1703
- }
1704
- if (Array.isArray(value)) {
1705
- value.forEach((item) => probeLiteral(item));
1706
- return;
1707
- }
1708
- if (!isRecord(value)) return;
1709
- for (const inner of Object.values(value)) probeLiteral(inner);
1710
- };
1711
- const walkSchema = (value, propertyNames = false) => {
1712
- if (Array.isArray(value)) {
1713
- value.forEach((item) => walkSchema(item, propertyNames));
1714
- return;
1715
- }
1716
- if (!isRecord(value)) return;
1717
- if (propertyNames) {
1718
- for (const key of Object.keys(value)) {
1719
- if (personal(key)) schema = true;
1720
- walkSchema(value[key], false);
1721
- }
1722
- return;
1723
- }
1724
- for (const key of Object.keys(value)) {
1725
- if (key === "enum" || key === "const" || key === "default") probeLiteral(value[key]);
1726
- else if (key === "properties" || key === "patternProperties") walkSchema(value[key], true);
1727
- else if (key === "items" || key === "additionalProperties" || key === "unevaluatedProperties" || key === "not" || key === "contains" || key === "anyOf" || key === "oneOf" || key === "allOf" || key === "if" || key === "then" || key === "else" || key === "$defs" || key === "definitions" || key === "dependentSchemas" || key === "prefixItems") walkSchema(value[key], false);
1728
- }
1729
- };
1730
- const walkNumbers = (value, field, top) => {
1731
- if (typeof value === "number") {
1732
- if (top && field !== void 0 && allowedTopLevel.has(field)) return;
1733
- if (looksLikeLongId(value)) number = true;
1734
- return;
1735
- }
1736
- if (Array.isArray(value)) {
1737
- value.forEach((item) => walkNumbers(item, void 0, false));
1738
- return;
1739
- }
1740
- if (!isRecord(value)) return;
1741
- const childrenAreTop = top && field === void 0;
1742
- for (const key of Object.keys(value)) walkNumbers(value[key], key, childrenAreTop);
1743
- };
1744
- const walkMedia = (value) => {
1745
- if (typeof value === "string") {
1746
- if (/^data:/i.test(value.trim())) media = true;
1747
- return;
1748
- }
1749
- if (Array.isArray(value)) {
1750
- value.forEach((item) => walkMedia(item));
1751
- return;
1752
- }
1753
- if (!isRecord(value)) return;
1754
- noteMediaRecord(value);
1755
- for (const key of Object.keys(value)) walkMedia(value[key]);
1756
- };
1757
- if (isRecord(root)) {
1758
- noteAttachments(root.attachments);
1759
- if (Array.isArray(root.messages)) {
1760
- root.messages.forEach((message) => {
1761
- if (!isRecord(message)) return;
1762
- noteAttachments(message.attachments);
1763
- walkMetadataKeys(message.metadata);
1764
- noteToolChoice(message.tool_choice);
1765
- if (isRecord(message.function_call)) noteToolName(message.function_call.name);
1766
- if (Array.isArray(message.content)) {
1767
- for (const part of message.content) {
1768
- if (!isRecord(part)) continue;
1769
- if (part.type === "tool_use" || part.type === "server_tool_use" || part.type === "mcp_tool_use") noteToolName(part.name);
1770
- }
1771
- }
1772
- if (Array.isArray(message.tool_calls)) {
1773
- for (const call of message.tool_calls) {
1774
- if (!isRecord(call) || !isRecord(call.function)) continue;
1775
- noteToolName(call.function.name);
1776
- const args = call.function.arguments;
1777
- if (typeof args === "string") {
1778
- try {
1779
- const parsed = JSON.parse(args);
1780
- walkArgs(parsed);
1781
- walkNumbers(parsed, void 0, false);
1782
- } catch {
1783
- }
1784
- } else {
1785
- walkArgs(args);
1786
- walkNumbers(args, void 0, false);
1787
- }
1788
- }
1789
- }
1790
- if (isRecord(message.function_call)) {
1791
- const args = message.function_call.arguments;
1792
- if (typeof args === "string") {
1793
- try {
1794
- const parsed = JSON.parse(args);
1795
- walkArgs(parsed);
1796
- walkNumbers(parsed, void 0, false);
1797
- } catch {
1798
- }
1799
- } else {
1800
- walkArgs(args);
1801
- walkNumbers(args, void 0, false);
1802
- }
1803
- }
1804
- const content = message.content;
1805
- if (Array.isArray(content)) {
1806
- for (const part of content) {
1807
- if (isRecord(part) && isRecord(part.input)) walkArgs(part.input);
1808
- }
1809
- }
1810
- });
1811
- }
1812
- if (Array.isArray(root.tools)) {
1813
- for (const tool of root.tools) {
1814
- if (!isRecord(tool)) continue;
1815
- noteToolName(tool.name);
1816
- if (isRecord(tool.function)) noteToolName(tool.function.name);
1817
- if (tool.input_schema !== void 0) walkSchema(tool.input_schema, false);
1818
- if (isRecord(tool.function) && tool.function.parameters !== void 0) walkSchema(tool.function.parameters, false);
1819
- }
1820
- }
1821
- noteToolChoice(root.tool_choice);
1822
- walkMetadataKeys(root.metadata);
1823
- }
1824
- let unknownBlock = false;
1825
- const walkBlockTypes = (value) => {
1826
- if (!Array.isArray(value)) return;
1827
- for (const part of value) {
1828
- if (!isRecord(part)) continue;
1829
- if (typeof part.type === "string" && !KNOWN_CONTENT_BLOCK_TYPES.has(part.type)) unknownBlock = true;
1830
- walkBlockTypes(part.content);
1831
- }
1832
- };
1833
- if (isRecord(root) && Array.isArray(root.messages)) {
1834
- for (const message of root.messages) if (isRecord(message)) walkBlockTypes(message.content);
1835
- }
1836
- if (isRecord(root)) walkBlockTypes(root.system);
1837
- walkNumbers(root, void 0, true);
1838
- walkMedia(root);
1839
- for (const slot of collectRequestBodyTextSlots(root)) {
1840
- if (slot.policy === "reject-if-changed" || slot.policy === "json-number") continue;
1841
- const value = slot.get();
1842
- if (isStandaloneEncodedProse(value, slot.path)) encoded = true;
1843
- }
1844
- const warnings = [];
1845
- if (toolKey) warnings.push({ reason: "value_not_replaced", message: PLACEMENT_TOOL_KEY });
1846
- if (toolName) warnings.push({ reason: "value_not_replaced", message: PLACEMENT_TOOL_NAME });
1847
- if (metadataKey && options?.metadataKeys !== false) {
1848
- warnings.push({ reason: "value_not_replaced", message: PLACEMENT_METADATA_KEY_LEFT });
1849
- }
1850
- if (schema) warnings.push({ reason: "value_not_replaced", message: PLACEMENT_SCHEMA });
1851
- if (number) warnings.push({ reason: "value_not_replaced", message: PLACEMENT_NUMBER });
1852
- if (encoded) warnings.push({ reason: "value_not_replaced", message: PLACEMENT_ENCODED });
1853
- if (media) warnings.push({ reason: "media_not_checked", message: PLACEMENT_MEDIA });
1854
- if (unknownBlock) warnings.push({ reason: "unrecognised_block", message: PLACEMENT_UNKNOWN_BLOCK });
1855
- return warnings;
1856
- function noteToolName(value) {
1857
- if (typeof value === "string" && value !== "" && personal(value)) toolName = true;
1858
- }
1859
- function noteToolChoice(choice) {
1860
- if (typeof choice === "string") {
1861
- noteToolName(choice);
1862
- return;
1863
- }
1864
- if (!isRecord(choice)) return;
1865
- noteToolName(choice.name);
1866
- if (isRecord(choice.function)) noteToolName(choice.function.name);
1867
- }
1868
- function walkMetadataKeys(value) {
1869
- if (Array.isArray(value)) {
1870
- value.forEach((item) => walkMetadataKeys(item));
1871
- return;
1872
- }
1873
- if (!isRecord(value)) return;
1874
- for (const key of Object.keys(value)) {
1875
- if (personal(key)) metadataKey = true;
1876
- walkMetadataKeys(value[key]);
1877
- }
1878
- }
1879
- }
1880
- function isStandaloneEncodedProse(value, path) {
1881
- const text = value.trim();
1882
- if (/^data:/i.test(text)) return false;
1883
- if (!/^[A-Za-z0-9+/_-]{4,}={0,2}$/.test(text)) return false;
1884
- const sensitiveSlot = /(?:^|\.)(?:password|passphrase|secret|credential|token)$/i.test(path);
1885
- if (text.length < 12 && !sensitiveSlot) return false;
1886
- if (!/[0-9+/_=-]/.test(text) && !sensitiveSlot) return false;
1887
- try {
1888
- const decoder = globalThis.atob;
1889
- if (typeof decoder !== "function") return true;
1890
- const normalised = text.replace(/-/g, "+").replace(/_/g, "/");
1891
- const padded = normalised.padEnd(Math.ceil(normalised.length / 4) * 4, "=");
1892
- decoder(padded);
1893
- return true;
1894
- } catch {
1895
- return false;
1896
- }
1897
- }
1898
- function assertReviewableTextSlot(value, path) {
1899
- const text = value.trim();
1900
- if (/^data:[^,]*;base64,/i.test(text)) {
1901
- throw new RequestBodyGuardError("base64 attachment payload requires local file review");
1902
- }
1903
- if (isStandaloneEncodedProse(value, path)) {
1904
- throw new RequestBodyGuardError("base64 content requires local file review");
1905
- }
1906
- }
1907
-
1908
1009
  // src/gateway-base.ts
1909
1010
  function validateGatewayBaseUrl(raw, makeError = (message) => new Error(message)) {
1910
1011
  let url;
@@ -2181,17 +1282,6 @@ export {
2181
1282
  detectPiiWithCoverage,
2182
1283
  tokenizedHeaderValue,
2183
1284
  lowerCoverage,
2184
- deepClone,
2185
- slotIsLeftAsWritten,
2186
- assertRequestBodySlotReviewable,
2187
- transformJsonArgumentText,
2188
- maskArgumentKeysForResidualCheck,
2189
- replaceObjectKeys,
2190
- replaceObjectKeysAsync,
2191
- collectRequestBodyTextSlots,
2192
- assertRequestBodyReviewable,
2193
- metadataKeyOutcomeWarnings,
2194
- collectValuePlacementWarnings,
2195
1285
  validateGatewayBaseUrl,
2196
1286
  engineIdentityHeaders,
2197
1287
  EgressGateError2 as EgressGateError,