@maskera/mcp 0.4.3 → 0.4.4
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/dist/cli.js +491 -73
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +13 -8
- package/dist/index.js +488 -69
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -895,7 +895,7 @@ Pick by where the text comes from: **proxy for text typed straight into the chat
|
|
|
895
895
|
# Same warm process also serves Streamable HTTP MCP:
|
|
896
896
|
# http://127.0.0.1:8787/mcp (supports ?no_raw_restore=1&read_only=1&approval_mode=browser)
|
|
897
897
|
\`\`\`
|
|
898
|
-
- Claude Desktop: Help -> Troubleshooting -> Enable Developer Mode, then Developer -> Configure Third-Party Inference. Set Inference provider to **Gateway**, Credential kind **Static API key**, Gateway base URL \`http://127.0.0.1:8787\` (no \`/v1\`), Gateway API key = the user's Anthropic API key, Gateway auth scheme **x-api-key**. Apply Changes, then Save & Restart. Full guide: \`https://maskera.dev/docs/
|
|
898
|
+
- Claude Desktop: Help -> Troubleshooting -> Enable Developer Mode, then Developer -> Configure Third-Party Inference. Set Inference provider to **Gateway**, Credential kind **Static API key**, Gateway base URL \`http://127.0.0.1:8787\` (no \`/v1\`), Gateway API key = the user's Anthropic API key, Gateway auth scheme **x-api-key**. Apply Changes, then Save & Restart. Full guide: \`https://maskera.dev/en/docs/clients#claude-desktop\`.
|
|
899
899
|
- Routes Anthropic (\`/v1/messages\`) and OpenAI (\`/v1/chat/completions\`, \`/v1/responses\`) automatically, or set \`--upstream <url>\`.
|
|
900
900
|
- Restores placeholders transparently in JSON, tool arguments and chunk-split SSE streams.
|
|
901
901
|
- Fails closed (\`502\`) if local redaction fails. Inspect counts only at \`http://127.0.0.1:8787/_maskera/status\`.
|
|
@@ -2021,7 +2021,7 @@ function createMaskeraMcpServer(options = {}) {
|
|
|
2021
2021
|
);
|
|
2022
2022
|
}
|
|
2023
2023
|
const response = await requestElicitation({
|
|
2024
|
-
message: `${input.tool} wants to release ${input.placeholderCount} original Swedish PII value(s) into the model context (${input.reason})
|
|
2024
|
+
message: `${input.tool} wants to release ${input.placeholderCount} original Swedish PII value(s) into the model context (${input.reason}).`,
|
|
2025
2025
|
requestedSchema: {
|
|
2026
2026
|
type: "object",
|
|
2027
2027
|
properties: {
|
|
@@ -3318,6 +3318,11 @@ function renderInspectorHtml(info) {
|
|
|
3318
3318
|
white-space: pre-wrap;
|
|
3319
3319
|
word-break: break-word;
|
|
3320
3320
|
}
|
|
3321
|
+
.preview.err-preview {
|
|
3322
|
+
border-color: var(--err);
|
|
3323
|
+
background: var(--err-bg);
|
|
3324
|
+
color: var(--err);
|
|
3325
|
+
}
|
|
3321
3326
|
.tok {
|
|
3322
3327
|
display: inline-block;
|
|
3323
3328
|
padding: 0 4px;
|
|
@@ -3350,7 +3355,7 @@ function renderInspectorHtml(info) {
|
|
|
3350
3355
|
</div>
|
|
3351
3356
|
<div class="actions">
|
|
3352
3357
|
<button id="selftest-btn" type="button" class="primary">K\xF6r sj\xE4lvtest</button>
|
|
3353
|
-
<button id="reveal-btn" type="button">Visa originalv\xE4rden
|
|
3358
|
+
<button id="reveal-btn" type="button">Visa originalv\xE4rden</button>
|
|
3354
3359
|
<button id="clear-btn" type="button">Rensa minne</button>
|
|
3355
3360
|
</div>
|
|
3356
3361
|
</header>
|
|
@@ -3391,6 +3396,16 @@ function renderInspectorHtml(info) {
|
|
|
3391
3396
|
|
|
3392
3397
|
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ "&":"&","<":"<",">":">",'"':""","'":"'" }[c]));
|
|
3393
3398
|
const highlightTokens = (s) => esc(s).replace(/\\[[A-Z\xC5\xC4\xD6][A-Z\xC5\xC4\xD60-9_]*_\\d+\\]/g, (m) => '<span class="tok">' + m + "</span>");
|
|
3399
|
+
const formatLocalTime = (iso) => {
|
|
3400
|
+
if (!iso) return "";
|
|
3401
|
+
const d = new Date(iso);
|
|
3402
|
+
if (Number.isNaN(d.getTime())) return String(iso).slice(11, 19);
|
|
3403
|
+
try {
|
|
3404
|
+
return d.toLocaleTimeString("sv-SE", { hour12: false });
|
|
3405
|
+
} catch {
|
|
3406
|
+
return String(iso).slice(11, 19);
|
|
3407
|
+
}
|
|
3408
|
+
};
|
|
3394
3409
|
|
|
3395
3410
|
function renderReceipt() {
|
|
3396
3411
|
if (!lastReceipt) {
|
|
@@ -3403,7 +3418,7 @@ function renderInspectorHtml(info) {
|
|
|
3403
3418
|
return '<li><span class="receipt-tag' + cls + '">[' + esc(c.status) + ']</span><span><strong>' + esc(c.name) + ':</strong> ' + esc(c.detail) + '</span></li>';
|
|
3404
3419
|
}).join("");
|
|
3405
3420
|
const proofHtml = r.sampleProof && !r.sampleProof.rawPiiLeaked
|
|
3406
|
-
? '<p class="body-label">
|
|
3421
|
+
? '<p class="body-label">Maskerat utdata:</p><pre class="preview">' + highlightTokens(r.sampleProof.redactedPreview) + '</pre>'
|
|
3407
3422
|
: "";
|
|
3408
3423
|
receiptRoot.innerHTML =
|
|
3409
3424
|
'<section class="receipt-box"><div class="receipt-head"><h2>Setup-kvitto (' + (r.ok ? "OK" : "FEL") + ' \xB7 v' + esc(r.version) + ' \xB7 ' + esc(r.sampleProof?.modeUsed || r.mode) + ')</h2>' +
|
|
@@ -3500,7 +3515,7 @@ function renderInspectorHtml(info) {
|
|
|
3500
3515
|
const r = await fetch("/_maskera/vault", { headers: { "x-maskera-reveal": "1" } });
|
|
3501
3516
|
const d = await r.json();
|
|
3502
3517
|
const rows = (d.entries || []).map((e) => "<tr><td>" + esc(e.sessionId) + "</td><td><span class=\\"tok\\">" + esc(e.token) + "</span></td><td>" + esc(e.value) + "</td></tr>").join("");
|
|
3503
|
-
vaultRoot.innerHTML = '<section class="vault-box"><h2
|
|
3518
|
+
vaultRoot.innerHTML = '<section class="vault-box"><h2>\xC5terst\xE4llningsnyckel</h2>' +
|
|
3504
3519
|
(rows ? "<table><thead><tr><th>Session</th><th>Platsh\xE5llare</th><th>Originalv\xE4rde</th></tr></thead><tbody>" + rows + "</tbody></table>" : "<p>Inga lagrade v\xE4rden \xE4n.</p>") +
|
|
3505
3520
|
"</section>";
|
|
3506
3521
|
} catch {}
|
|
@@ -3509,8 +3524,8 @@ function renderInspectorHtml(info) {
|
|
|
3509
3524
|
function render() {
|
|
3510
3525
|
if (!items.length) {
|
|
3511
3526
|
feedEl.innerHTML =
|
|
3512
|
-
'<div class="empty"><p class="empty-title">
|
|
3513
|
-
'
|
|
3527
|
+
'<div class="empty"><p class="empty-title">Inga anrop \xE4nnu.</p>' +
|
|
3528
|
+
'Skicka ett prov fr\xE5n din klient s\xE5 visas maskeringen h\xE4r:' +
|
|
3514
3529
|
'<div class="sample-row"><span class="sample-text">' + esc(SAMPLE_PROMPT) + '</span><button id="copy-sample-btn" type="button">Kopiera testmening</button></div>' +
|
|
3515
3530
|
'<code>npx -y @maskera/mcp wrap -- claude\\nANTHROPIC_BASE_URL=${safeUrl} claude\\nOPENAI_BASE_URL=${safeUrl}/v1 codex\\nclaude mcp add --transport http maskera ${safeMcpUrl}</code></div>';
|
|
3516
3531
|
const copyBtn = document.getElementById("copy-sample-btn");
|
|
@@ -3530,15 +3545,16 @@ function renderInspectorHtml(info) {
|
|
|
3530
3545
|
}
|
|
3531
3546
|
for (const it of items) seenIds.add(it.id);
|
|
3532
3547
|
feedEl.innerHTML = '<div class="list">' + items.map((it) => {
|
|
3533
|
-
const time = (it.timestamp
|
|
3548
|
+
const time = formatLocalTime(it.timestamp);
|
|
3534
3549
|
const tags = Object.entries(it.counts || {}).map(([k, v]) => '<span class="count-tag">' + esc(k) + " \xD7" + v + "</span>").join("");
|
|
3535
3550
|
const previews = (it.redactedPreview || []).map((p) => '<pre class="preview">' + highlightTokens(p) + "</pre>").join("");
|
|
3551
|
+
const errBlock = it.errorDetail ? '<p class="body-label">Svar fr\xE5n API (fel):</p><pre class="preview err-preview">' + esc(it.errorDetail) + "</pre>" : "";
|
|
3536
3552
|
const statusCls = it.status >= 200 && it.status < 400 ? "" : " err";
|
|
3537
3553
|
const isOpen = openIds.has(it.id);
|
|
3538
3554
|
return '<details class="item" data-id="' + esc(it.id) + '"' + (isOpen ? " open" : "") + '>' +
|
|
3539
3555
|
'<summary><span class="left"><span class="badge' + statusCls + '">' + it.status + '</span><span class="path">' + esc(it.method + " " + it.path) + '</span>' + tags + (it.redactions === 0 ? '<span style="color:var(--muted);font-size:12px">0 PII</span>' : "") + '</span>' +
|
|
3540
3556
|
'<span class="right"><span>' + (it.streamed ? "SSE" : "JSON") + '</span><span>' + it.durationMs + ' ms</span><span>' + esc(time) + '</span></span></summary>' +
|
|
3541
|
-
'<div class="body"
|
|
3557
|
+
'<div class="body">' + errBlock + '<p class="body-label">Skickat vidare:</p>' + (previews || '<pre class="preview">(ingen text i anropet)</pre>') + '</div></details>';
|
|
3542
3558
|
}).join("") + "</div>";
|
|
3543
3559
|
for (const det of feedEl.querySelectorAll("details.item")) {
|
|
3544
3560
|
det.addEventListener("toggle", () => {
|
|
@@ -3554,7 +3570,7 @@ function renderInspectorHtml(info) {
|
|
|
3554
3570
|
revealBtn.addEventListener("click", () => {
|
|
3555
3571
|
reveal = !reveal;
|
|
3556
3572
|
revealBtn.classList.toggle("active", reveal);
|
|
3557
|
-
revealBtn.textContent = reveal ? "D\xF6lj originalv\xE4rden" : "Visa originalv\xE4rden
|
|
3573
|
+
revealBtn.textContent = reveal ? "D\xF6lj originalv\xE4rden" : "Visa originalv\xE4rden";
|
|
3558
3574
|
void refreshVault();
|
|
3559
3575
|
});
|
|
3560
3576
|
|
|
@@ -4121,7 +4137,7 @@ function restoreJsonLeaves(value, map) {
|
|
|
4121
4137
|
}
|
|
4122
4138
|
return value;
|
|
4123
4139
|
}
|
|
4124
|
-
var MASKERA_VAULT_TOOL_RE = /(?:^|
|
|
4140
|
+
var MASKERA_VAULT_TOOL_RE = /(?:^|[^a-zA-Z0-9]|_)maskera_(?:write_file|restore|clear_session)$/;
|
|
4125
4141
|
var MASKERA_VAULT_CODE_RE = /\bmaskera_(?:write_file|restore)\b/;
|
|
4126
4142
|
function isMaskeraVaultToolName(name) {
|
|
4127
4143
|
return typeof name === "string" && MASKERA_VAULT_TOOL_RE.test(name);
|
|
@@ -4132,10 +4148,80 @@ function shouldSkipToolArgRestore(toolName, argsText) {
|
|
|
4132
4148
|
return false;
|
|
4133
4149
|
}
|
|
4134
4150
|
var SWEDISH_DIACRITIC_RE = /[åäöÅÄÖ]/;
|
|
4135
|
-
var
|
|
4151
|
+
var SWEDISH_STOP_WORDS = /* @__PURE__ */ new Set([
|
|
4152
|
+
"och",
|
|
4153
|
+
"att",
|
|
4154
|
+
"det",
|
|
4155
|
+
"som",
|
|
4156
|
+
"f\xF6r",
|
|
4157
|
+
"med",
|
|
4158
|
+
"har",
|
|
4159
|
+
"inte",
|
|
4160
|
+
"p\xE5",
|
|
4161
|
+
"\xE4r",
|
|
4162
|
+
"av",
|
|
4163
|
+
"till",
|
|
4164
|
+
"ett",
|
|
4165
|
+
"fr\xE5n",
|
|
4166
|
+
"kan",
|
|
4167
|
+
"ska",
|
|
4168
|
+
"eller",
|
|
4169
|
+
"vid",
|
|
4170
|
+
"s\xE5",
|
|
4171
|
+
"men",
|
|
4172
|
+
"den",
|
|
4173
|
+
"jag",
|
|
4174
|
+
"din",
|
|
4175
|
+
"ditt",
|
|
4176
|
+
"mitt",
|
|
4177
|
+
"var",
|
|
4178
|
+
"blev",
|
|
4179
|
+
"enligt",
|
|
4180
|
+
"kund",
|
|
4181
|
+
"personnummer",
|
|
4182
|
+
"svenska",
|
|
4183
|
+
"sekretess",
|
|
4184
|
+
"arbetsgivare",
|
|
4185
|
+
"utbetalning",
|
|
4186
|
+
"sammanfatta",
|
|
4187
|
+
"skriv",
|
|
4188
|
+
"svara",
|
|
4189
|
+
"nedan",
|
|
4190
|
+
"uppgifter",
|
|
4191
|
+
"v\xE4nliga",
|
|
4192
|
+
"h\xE4lsningar",
|
|
4193
|
+
"\xE4rendet",
|
|
4194
|
+
"\xE4rende",
|
|
4195
|
+
"mejl",
|
|
4196
|
+
"brev",
|
|
4197
|
+
"patient",
|
|
4198
|
+
"namn",
|
|
4199
|
+
"telefon",
|
|
4200
|
+
"adress"
|
|
4201
|
+
]);
|
|
4202
|
+
var CLI_SYSTEM_HEADER_RE = /^(?:x-anthropic-billing-header:|you are claude code\b|you are a claude agent\b|you are codex\b|you are opencode\b|sessionstart:|# gitstatus\b|current branch:|recent commits:|status:)/i;
|
|
4136
4203
|
var ENGLISH_BOILERPLATE_WORDS = /* @__PURE__ */ new Set([
|
|
4137
4204
|
"the",
|
|
4138
4205
|
"and",
|
|
4206
|
+
"to",
|
|
4207
|
+
"of",
|
|
4208
|
+
"in",
|
|
4209
|
+
"is",
|
|
4210
|
+
"it",
|
|
4211
|
+
"on",
|
|
4212
|
+
"as",
|
|
4213
|
+
"be",
|
|
4214
|
+
"by",
|
|
4215
|
+
"or",
|
|
4216
|
+
"an",
|
|
4217
|
+
"at",
|
|
4218
|
+
"if",
|
|
4219
|
+
"do",
|
|
4220
|
+
"no",
|
|
4221
|
+
"yes",
|
|
4222
|
+
"so",
|
|
4223
|
+
"but",
|
|
4224
|
+
"not",
|
|
4139
4225
|
"you",
|
|
4140
4226
|
"your",
|
|
4141
4227
|
"with",
|
|
@@ -4143,7 +4229,6 @@ var ENGLISH_BOILERPLATE_WORDS = /* @__PURE__ */ new Set([
|
|
|
4143
4229
|
"this",
|
|
4144
4230
|
"from",
|
|
4145
4231
|
"are",
|
|
4146
|
-
"not",
|
|
4147
4232
|
"when",
|
|
4148
4233
|
"use",
|
|
4149
4234
|
"must",
|
|
@@ -4151,7 +4236,9 @@ var ENGLISH_BOILERPLATE_WORDS = /* @__PURE__ */ new Set([
|
|
|
4151
4236
|
"never",
|
|
4152
4237
|
"always",
|
|
4153
4238
|
"tools",
|
|
4239
|
+
"tool",
|
|
4154
4240
|
"files",
|
|
4241
|
+
"file",
|
|
4155
4242
|
"code",
|
|
4156
4243
|
"return",
|
|
4157
4244
|
"output",
|
|
@@ -4159,24 +4246,296 @@ var ENGLISH_BOILERPLATE_WORDS = /* @__PURE__ */ new Set([
|
|
|
4159
4246
|
"assistant",
|
|
4160
4247
|
"instructions",
|
|
4161
4248
|
"command",
|
|
4162
|
-
"
|
|
4249
|
+
"commands",
|
|
4250
|
+
"user",
|
|
4251
|
+
"for",
|
|
4252
|
+
"into",
|
|
4253
|
+
"only",
|
|
4254
|
+
"before",
|
|
4255
|
+
"after",
|
|
4256
|
+
"run",
|
|
4257
|
+
"check",
|
|
4258
|
+
"edit",
|
|
4259
|
+
"read",
|
|
4260
|
+
"write",
|
|
4261
|
+
"git",
|
|
4262
|
+
"branch",
|
|
4263
|
+
"commit",
|
|
4264
|
+
"push",
|
|
4265
|
+
"test",
|
|
4266
|
+
"tests",
|
|
4267
|
+
"error",
|
|
4268
|
+
"path",
|
|
4269
|
+
"directory",
|
|
4270
|
+
"repository",
|
|
4271
|
+
"session",
|
|
4272
|
+
"agent",
|
|
4273
|
+
"prompt",
|
|
4274
|
+
"message",
|
|
4275
|
+
"system",
|
|
4276
|
+
"default",
|
|
4277
|
+
"without",
|
|
4278
|
+
"can",
|
|
4279
|
+
"will",
|
|
4280
|
+
"each",
|
|
4281
|
+
"all",
|
|
4282
|
+
"any",
|
|
4283
|
+
"every",
|
|
4284
|
+
"about",
|
|
4285
|
+
"more",
|
|
4286
|
+
"also",
|
|
4287
|
+
"make",
|
|
4288
|
+
"keep",
|
|
4289
|
+
"does",
|
|
4290
|
+
"have",
|
|
4291
|
+
"has",
|
|
4292
|
+
"been",
|
|
4293
|
+
"its",
|
|
4294
|
+
"their",
|
|
4295
|
+
"them",
|
|
4296
|
+
"then",
|
|
4297
|
+
"than",
|
|
4298
|
+
"using",
|
|
4299
|
+
"following",
|
|
4300
|
+
"other",
|
|
4301
|
+
"same",
|
|
4302
|
+
"first",
|
|
4303
|
+
"next",
|
|
4304
|
+
"existing",
|
|
4305
|
+
"changes",
|
|
4306
|
+
"where",
|
|
4307
|
+
"which",
|
|
4308
|
+
"what",
|
|
4309
|
+
"how",
|
|
4310
|
+
"why",
|
|
4311
|
+
"because",
|
|
4312
|
+
"unless",
|
|
4313
|
+
"instead",
|
|
4314
|
+
"below",
|
|
4315
|
+
"above",
|
|
4316
|
+
"work",
|
|
4317
|
+
"working",
|
|
4318
|
+
"task",
|
|
4319
|
+
"tasks",
|
|
4320
|
+
"skill",
|
|
4321
|
+
"skills",
|
|
4322
|
+
"rule",
|
|
4323
|
+
"rules",
|
|
4324
|
+
"repo",
|
|
4325
|
+
"project",
|
|
4326
|
+
"build",
|
|
4327
|
+
"lint",
|
|
4328
|
+
"hook",
|
|
4329
|
+
"hooks",
|
|
4330
|
+
"memory",
|
|
4331
|
+
"local",
|
|
4332
|
+
"remote",
|
|
4333
|
+
"status",
|
|
4334
|
+
"current",
|
|
4335
|
+
"recent"
|
|
4163
4336
|
]);
|
|
4164
|
-
var
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4337
|
+
var WORD_TOKEN_RE2 = /[a-zåäö]{2,}/gi;
|
|
4338
|
+
var SYSTEM_REMINDER_SPLIT_RE = /(<system-reminder>[\s\S]*?<\/system-reminder>)/g;
|
|
4339
|
+
var FENCED_CODE_SPLIT_RE = /(```[\s\S]*?```)/g;
|
|
4340
|
+
var GIT_STATUS_LINE_RE = /^(?:[MADRCU?!]{1,2}\s+\S|[0-9a-f]{7,40}\s+\S)/;
|
|
4341
|
+
var XML_TAG_ONLY_RE = /^<\/?[a-z][a-z0-9_-]*(?:\s[^>]*)?>$/i;
|
|
4342
|
+
var MARKDOWN_HEADING_RE = /^#{1,6}\s+\S/;
|
|
4343
|
+
var CODE_SIGNATURE_RE = /(?:\/\*\*|\?\s*:\s*(?:string|number|boolean|object|unknown|any|Record|Array)\b|\b(?:string|number|boolean)\s*\||\b(?:interface|namespace|type)\s+[A-Z]\w*|\bPromise<|=>\s*\{)/;
|
|
4344
|
+
var BROWSER_PROBE_PATHS = /* @__PURE__ */ new Set([
|
|
4345
|
+
"/favicon.ico",
|
|
4346
|
+
"/apple-touch-icon.png",
|
|
4347
|
+
"/apple-touch-icon-precomposed.png",
|
|
4348
|
+
"/robots.txt"
|
|
4349
|
+
]);
|
|
4350
|
+
var PLACEHOLDER_TOKEN_RE = /\[[A-ZÅÄÖ][A-ZÅÄÖ0-9_]*_\d+\]/g;
|
|
4351
|
+
function collectPlaceholdersFromUnknown(value, out) {
|
|
4352
|
+
if (typeof value === "string") {
|
|
4353
|
+
if (!value.includes("[")) return;
|
|
4354
|
+
const matches = value.match(PLACEHOLDER_TOKEN_RE);
|
|
4355
|
+
if (matches) {
|
|
4356
|
+
for (const tok of matches) out.add(tok);
|
|
4357
|
+
}
|
|
4358
|
+
return;
|
|
4359
|
+
}
|
|
4360
|
+
if (Array.isArray(value)) {
|
|
4361
|
+
for (const item of value) collectPlaceholdersFromUnknown(item, out);
|
|
4362
|
+
return;
|
|
4363
|
+
}
|
|
4364
|
+
if (isPlainObject(value)) {
|
|
4365
|
+
for (const v of Object.values(value)) collectPlaceholdersFromUnknown(v, out);
|
|
4366
|
+
}
|
|
4367
|
+
}
|
|
4368
|
+
function collectExternalToolResultPlaceholders(body, out) {
|
|
4369
|
+
if (Array.isArray(body.messages)) {
|
|
4370
|
+
for (const msg of body.messages) {
|
|
4371
|
+
if (!isPlainObject(msg)) continue;
|
|
4372
|
+
if (msg.role === "tool") {
|
|
4373
|
+
collectPlaceholdersFromUnknown(msg.content, out);
|
|
4374
|
+
continue;
|
|
4375
|
+
}
|
|
4376
|
+
if (Array.isArray(msg.content)) {
|
|
4377
|
+
for (const block of msg.content) {
|
|
4378
|
+
if (isPlainObject(block) && block.type === "tool_result") {
|
|
4379
|
+
collectPlaceholdersFromUnknown(block.content, out);
|
|
4380
|
+
}
|
|
4381
|
+
}
|
|
4382
|
+
}
|
|
4383
|
+
}
|
|
4384
|
+
}
|
|
4385
|
+
if (Array.isArray(body.input)) {
|
|
4386
|
+
for (const item of body.input) {
|
|
4387
|
+
if (isPlainObject(item) && item.type === "function_call_output") {
|
|
4388
|
+
collectPlaceholdersFromUnknown(item.output, out);
|
|
4389
|
+
}
|
|
4390
|
+
}
|
|
4391
|
+
}
|
|
4392
|
+
}
|
|
4393
|
+
function isToolCatalogJson(text) {
|
|
4394
|
+
const trimmed = text.trim();
|
|
4395
|
+
if (!trimmed.startsWith("{") || !trimmed.includes('"items"') || !trimmed.includes('"tools.')) {
|
|
4396
|
+
return false;
|
|
4397
|
+
}
|
|
4398
|
+
try {
|
|
4399
|
+
const parsed = JSON.parse(trimmed);
|
|
4400
|
+
if (!isPlainObject(parsed) || !Array.isArray(parsed.items) || parsed.items.length === 0) {
|
|
4401
|
+
return false;
|
|
4402
|
+
}
|
|
4403
|
+
return parsed.items.every(
|
|
4404
|
+
(item) => isPlainObject(item) && typeof item.path === "string" && item.path.startsWith("tools.") && (item.description === void 0 || typeof item.description === "string")
|
|
4405
|
+
);
|
|
4406
|
+
} catch {
|
|
4407
|
+
return false;
|
|
4408
|
+
}
|
|
4409
|
+
}
|
|
4410
|
+
function isEnglishSystemParagraph(paragraph) {
|
|
4411
|
+
const trimmed = paragraph.trim();
|
|
4412
|
+
if (!trimmed) return true;
|
|
4413
|
+
if (XML_TAG_ONLY_RE.test(trimmed)) return true;
|
|
4414
|
+
if (trimmed.startsWith("```") && trimmed.endsWith("```")) return true;
|
|
4415
|
+
if (CLI_SYSTEM_HEADER_RE.test(trimmed)) return true;
|
|
4416
|
+
const nonEmptyLines = trimmed.split("\n").map((l) => l.trim()).filter(Boolean);
|
|
4417
|
+
if (nonEmptyLines.length > 0 && nonEmptyLines.every(
|
|
4418
|
+
(l) => GIT_STATUS_LINE_RE.test(l) || XML_TAG_ONLY_RE.test(l) || CLI_SYSTEM_HEADER_RE.test(l) || l.startsWith("(use ")
|
|
4419
|
+
)) {
|
|
4420
|
+
return true;
|
|
4421
|
+
}
|
|
4422
|
+
let enTotal = 0;
|
|
4423
|
+
const enDistinct = /* @__PURE__ */ new Set();
|
|
4424
|
+
let svTotal = 0;
|
|
4425
|
+
let svDiacriticWords = 0;
|
|
4426
|
+
const lower = trimmed.toLowerCase();
|
|
4427
|
+
WORD_TOKEN_RE2.lastIndex = 0;
|
|
4428
|
+
for (let m = WORD_TOKEN_RE2.exec(lower); m !== null; m = WORD_TOKEN_RE2.exec(lower)) {
|
|
4173
4429
|
const word = m[0];
|
|
4174
|
-
if (word
|
|
4175
|
-
|
|
4176
|
-
|
|
4430
|
+
if (!word) continue;
|
|
4431
|
+
if (SWEDISH_STOP_WORDS.has(word)) {
|
|
4432
|
+
svTotal += 1;
|
|
4433
|
+
}
|
|
4434
|
+
if (SWEDISH_DIACRITIC_RE.test(word)) {
|
|
4435
|
+
svDiacriticWords += 1;
|
|
4436
|
+
}
|
|
4437
|
+
if (ENGLISH_BOILERPLATE_WORDS.has(word)) {
|
|
4438
|
+
enTotal += 1;
|
|
4439
|
+
enDistinct.add(word);
|
|
4177
4440
|
}
|
|
4178
4441
|
}
|
|
4179
|
-
|
|
4442
|
+
if (nonEmptyLines.length === 1 && MARKDOWN_HEADING_RE.test(trimmed) && svTotal === 0 && svDiacriticWords === 0 && trimmed.length < 120) {
|
|
4443
|
+
return true;
|
|
4444
|
+
}
|
|
4445
|
+
if (svTotal === 0 && svDiacriticWords === 0 && CODE_SIGNATURE_RE.test(trimmed)) {
|
|
4446
|
+
return true;
|
|
4447
|
+
}
|
|
4448
|
+
if (trimmed.length < 60 && enDistinct.size < 4) return false;
|
|
4449
|
+
const svSignal = svTotal + svDiacriticWords;
|
|
4450
|
+
if (svSignal === 0) {
|
|
4451
|
+
const minDistinct = trimmed.length >= 240 ? 5 : 3;
|
|
4452
|
+
return enDistinct.size >= minDistinct;
|
|
4453
|
+
}
|
|
4454
|
+
return enTotal >= 12 && enDistinct.size >= 6 && enTotal >= svSignal * 6;
|
|
4455
|
+
}
|
|
4456
|
+
function isLikelyEnglishSystemPrompt(text) {
|
|
4457
|
+
const trimmed = text.trim();
|
|
4458
|
+
if (!trimmed) return false;
|
|
4459
|
+
if (trimmed.includes("\n\n")) {
|
|
4460
|
+
const paragraphs = trimmed.split(/\n{2,}/).map((p) => p.trim()).filter(Boolean);
|
|
4461
|
+
return paragraphs.length > 0 && paragraphs.every((p) => isEnglishSystemParagraph(p));
|
|
4462
|
+
}
|
|
4463
|
+
return isEnglishSystemParagraph(trimmed);
|
|
4464
|
+
}
|
|
4465
|
+
async function redactSystemText(text, redactChunk) {
|
|
4466
|
+
if (isLikelyEnglishSystemPrompt(text)) {
|
|
4467
|
+
return redactChunk(text, true, false);
|
|
4468
|
+
}
|
|
4469
|
+
const redactSegment = async (segment) => {
|
|
4470
|
+
if (!segment.trim()) return segment;
|
|
4471
|
+
if (segment.startsWith("```") && segment.endsWith("```")) {
|
|
4472
|
+
return redactChunk(segment, true, false);
|
|
4473
|
+
}
|
|
4474
|
+
if (!segment.includes("\n\n")) {
|
|
4475
|
+
return redactChunk(segment, isEnglishSystemParagraph(segment), false);
|
|
4476
|
+
}
|
|
4477
|
+
const parts = segment.split(/(\n{2,})/);
|
|
4478
|
+
for (let i = 0; i < parts.length; i += 2) {
|
|
4479
|
+
const para = parts[i] ?? "";
|
|
4480
|
+
if (!para.trim()) continue;
|
|
4481
|
+
parts[i] = await redactChunk(para, isEnglishSystemParagraph(para), false);
|
|
4482
|
+
}
|
|
4483
|
+
return parts.join("");
|
|
4484
|
+
};
|
|
4485
|
+
if (!text.includes("```")) {
|
|
4486
|
+
return redactSegment(text);
|
|
4487
|
+
}
|
|
4488
|
+
const codeParts = text.split(FENCED_CODE_SPLIT_RE);
|
|
4489
|
+
for (let i = 0; i < codeParts.length; i += 1) {
|
|
4490
|
+
const part = codeParts[i] ?? "";
|
|
4491
|
+
if (!part.trim()) continue;
|
|
4492
|
+
codeParts[i] = await redactSegment(part);
|
|
4493
|
+
}
|
|
4494
|
+
return codeParts.join("");
|
|
4495
|
+
}
|
|
4496
|
+
async function redactMessageText(text, redactChunk) {
|
|
4497
|
+
if (!text.includes("<system-reminder>")) {
|
|
4498
|
+
return redactChunk(text, false, true);
|
|
4499
|
+
}
|
|
4500
|
+
const parts = text.split(SYSTEM_REMINDER_SPLIT_RE);
|
|
4501
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
4502
|
+
const segment = parts[i] ?? "";
|
|
4503
|
+
if (!segment.trim()) continue;
|
|
4504
|
+
if (segment.startsWith("<system-reminder>") && segment.endsWith("</system-reminder>")) {
|
|
4505
|
+
parts[i] = await redactSystemText(segment, redactChunk);
|
|
4506
|
+
} else {
|
|
4507
|
+
parts[i] = await redactChunk(segment, false, true);
|
|
4508
|
+
}
|
|
4509
|
+
}
|
|
4510
|
+
return parts.join("");
|
|
4511
|
+
}
|
|
4512
|
+
async function extractSafeUpstreamError(resBuf, status, engine) {
|
|
4513
|
+
if (status < 400 || resBuf.byteLength === 0) return void 0;
|
|
4514
|
+
let summary = "";
|
|
4515
|
+
try {
|
|
4516
|
+
const parsed = JSON.parse(resBuf.toString("utf8"));
|
|
4517
|
+
if (isPlainObject(parsed)) {
|
|
4518
|
+
const errObj = isPlainObject(parsed.error) ? parsed.error : parsed;
|
|
4519
|
+
const parts = [];
|
|
4520
|
+
if (typeof errObj.type === "string" && errObj.type.trim()) parts.push(errObj.type.trim());
|
|
4521
|
+
if (typeof errObj.code === "string" && errObj.code.trim()) parts.push(errObj.code.trim());
|
|
4522
|
+
if (typeof errObj.message === "string" && errObj.message.trim()) {
|
|
4523
|
+
parts.push(errObj.message.trim());
|
|
4524
|
+
}
|
|
4525
|
+
summary = parts.join(": ");
|
|
4526
|
+
}
|
|
4527
|
+
} catch {
|
|
4528
|
+
const raw = resBuf.toString("utf8").trim();
|
|
4529
|
+
if (raw && !raw.startsWith("<")) summary = raw;
|
|
4530
|
+
}
|
|
4531
|
+
if (!summary) return void 0;
|
|
4532
|
+
const trimmed = summary.length > 240 ? `${summary.slice(0, 240)}...` : summary;
|
|
4533
|
+
try {
|
|
4534
|
+
const { result } = await engine.redact(trimmed, { mode: "rules" });
|
|
4535
|
+
return result.text;
|
|
4536
|
+
} catch {
|
|
4537
|
+
return `HTTP ${status}`;
|
|
4538
|
+
}
|
|
4180
4539
|
}
|
|
4181
4540
|
function headerFirst(headers, name) {
|
|
4182
4541
|
const value = headers[name];
|
|
@@ -4247,13 +4606,14 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4247
4606
|
noteBinary("input_audio");
|
|
4248
4607
|
}
|
|
4249
4608
|
if (typeof block.text === "string") {
|
|
4250
|
-
|
|
4609
|
+
const blockKind = kind === "message" && isToolCatalogJson(block.text) ? "tool_description" : kind;
|
|
4610
|
+
block.text = await redactString(block.text, blockKind);
|
|
4251
4611
|
}
|
|
4252
4612
|
if (typeof block.refusal === "string") {
|
|
4253
4613
|
block.refusal = await redactString(block.refusal, kind);
|
|
4254
4614
|
}
|
|
4255
4615
|
if (typeof block.thinking === "string") {
|
|
4256
|
-
const redactedThinking = await redactString(block.thinking,
|
|
4616
|
+
const redactedThinking = await redactString(block.thinking, "reasoning");
|
|
4257
4617
|
if (redactedThinking !== block.thinking) {
|
|
4258
4618
|
block.thinking = redactedThinking;
|
|
4259
4619
|
delete block.signature;
|
|
@@ -4266,12 +4626,14 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4266
4626
|
block.context = await redactString(block.context, kind);
|
|
4267
4627
|
}
|
|
4268
4628
|
if (typeof block.content === "string") {
|
|
4269
|
-
|
|
4629
|
+
const blockKind = blockType === "tool_result" && isToolCatalogJson(block.content) ? "tool_description" : kind;
|
|
4630
|
+
block.content = await redactString(block.content, blockKind);
|
|
4270
4631
|
} else if (Array.isArray(block.content)) {
|
|
4271
4632
|
await redactContentBlocks(block.content, kind);
|
|
4272
4633
|
}
|
|
4273
4634
|
if (typeof block.output === "string") {
|
|
4274
|
-
|
|
4635
|
+
const blockKind = kind === "message" && isToolCatalogJson(block.output) ? "tool_description" : kind;
|
|
4636
|
+
block.output = await redactString(block.output, blockKind);
|
|
4275
4637
|
} else if (Array.isArray(block.output)) {
|
|
4276
4638
|
await redactContentBlocks(block.output, kind);
|
|
4277
4639
|
}
|
|
@@ -4410,15 +4772,16 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4410
4772
|
msg.name = await redactString(msg.name, "tool_description");
|
|
4411
4773
|
}
|
|
4412
4774
|
if (typeof msg.content === "string") {
|
|
4413
|
-
msg.
|
|
4775
|
+
const contentKind = msg.role === "tool" && isToolCatalogJson(msg.content) ? "tool_description" : msgKind;
|
|
4776
|
+
msg.content = await redactString(msg.content, contentKind);
|
|
4414
4777
|
} else if (Array.isArray(msg.content)) {
|
|
4415
4778
|
await redactContentBlocks(msg.content, msgKind);
|
|
4416
4779
|
}
|
|
4417
4780
|
if (typeof msg.reasoning_content === "string") {
|
|
4418
|
-
msg.reasoning_content = await redactString(msg.reasoning_content,
|
|
4781
|
+
msg.reasoning_content = await redactString(msg.reasoning_content, "reasoning");
|
|
4419
4782
|
}
|
|
4420
4783
|
if (typeof msg.reasoning === "string") {
|
|
4421
|
-
msg.reasoning = await redactString(msg.reasoning,
|
|
4784
|
+
msg.reasoning = await redactString(msg.reasoning, "reasoning");
|
|
4422
4785
|
}
|
|
4423
4786
|
if (typeof msg.refusal === "string") {
|
|
4424
4787
|
msg.refusal = await redactString(msg.refusal, msgKind);
|
|
@@ -4459,7 +4822,8 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4459
4822
|
await redactContentBlocks(item.content, itemKind);
|
|
4460
4823
|
}
|
|
4461
4824
|
if (typeof item.output === "string") {
|
|
4462
|
-
item.
|
|
4825
|
+
const outKind = item.type === "function_call_output" && isToolCatalogJson(item.output) ? "tool_description" : itemKind;
|
|
4826
|
+
item.output = await redactString(item.output, outKind);
|
|
4463
4827
|
} else if (Array.isArray(item.output)) {
|
|
4464
4828
|
await redactContentBlocks(item.output, itemKind);
|
|
4465
4829
|
}
|
|
@@ -4469,10 +4833,10 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4469
4833
|
item.input = await redactObjectStrings(item.input, itemKind);
|
|
4470
4834
|
}
|
|
4471
4835
|
if (typeof item.reasoning_content === "string") {
|
|
4472
|
-
item.reasoning_content = await redactString(item.reasoning_content,
|
|
4836
|
+
item.reasoning_content = await redactString(item.reasoning_content, "reasoning");
|
|
4473
4837
|
}
|
|
4474
4838
|
if (typeof item.reasoning === "string") {
|
|
4475
|
-
item.reasoning = await redactString(item.reasoning,
|
|
4839
|
+
item.reasoning = await redactString(item.reasoning, "reasoning");
|
|
4476
4840
|
}
|
|
4477
4841
|
if (typeof item.refusal === "string") {
|
|
4478
4842
|
item.refusal = await redactString(item.refusal, itemKind);
|
|
@@ -4488,9 +4852,18 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4488
4852
|
}
|
|
4489
4853
|
return clone;
|
|
4490
4854
|
}
|
|
4855
|
+
function filterMapExcluding(map, excluded) {
|
|
4856
|
+
if (!excluded || excluded.size === 0) return map;
|
|
4857
|
+
const out = {};
|
|
4858
|
+
for (const [token, value] of Object.entries(map)) {
|
|
4859
|
+
if (!excluded.has(token)) out[token] = value;
|
|
4860
|
+
}
|
|
4861
|
+
return out;
|
|
4862
|
+
}
|
|
4491
4863
|
function restoreLlmResponseBody(body, map, options = {}) {
|
|
4492
4864
|
if (!isPlainObject(body) || Object.keys(map).length === 0) return body;
|
|
4493
4865
|
const clone = options.mutate ? body : structuredClone(body);
|
|
4866
|
+
const reasoningMap = filterMapExcluding(map, options.reasoningExcludedTokens);
|
|
4494
4867
|
if (Array.isArray(clone.content)) {
|
|
4495
4868
|
for (const block of clone.content) {
|
|
4496
4869
|
if (!isPlainObject(block)) continue;
|
|
@@ -4498,7 +4871,7 @@ function restoreLlmResponseBody(body, map, options = {}) {
|
|
|
4498
4871
|
block.text = restorePlainText(block.text, map);
|
|
4499
4872
|
}
|
|
4500
4873
|
if (typeof block.thinking === "string" && typeof block.signature !== "string") {
|
|
4501
|
-
block.thinking = restorePlainText(block.thinking,
|
|
4874
|
+
block.thinking = restorePlainText(block.thinking, reasoningMap);
|
|
4502
4875
|
}
|
|
4503
4876
|
if (isPlainObject(block.input)) {
|
|
4504
4877
|
const skipRestore = isMaskeraVaultToolName(block.name) || block.name === "execute" && typeof block.input.code === "string" && MASKERA_VAULT_CODE_RE.test(block.input.code);
|
|
@@ -4517,10 +4890,10 @@ function restoreLlmResponseBody(body, map, options = {}) {
|
|
|
4517
4890
|
msg.content = restorePlainText(msg.content, map);
|
|
4518
4891
|
}
|
|
4519
4892
|
if (typeof msg.reasoning_content === "string") {
|
|
4520
|
-
msg.reasoning_content = restorePlainText(msg.reasoning_content,
|
|
4893
|
+
msg.reasoning_content = restorePlainText(msg.reasoning_content, reasoningMap);
|
|
4521
4894
|
}
|
|
4522
4895
|
if (typeof msg.reasoning === "string") {
|
|
4523
|
-
msg.reasoning = restorePlainText(msg.reasoning,
|
|
4896
|
+
msg.reasoning = restorePlainText(msg.reasoning, reasoningMap);
|
|
4524
4897
|
}
|
|
4525
4898
|
if (Array.isArray(msg.tool_calls)) {
|
|
4526
4899
|
for (const call of msg.tool_calls) {
|
|
@@ -4569,14 +4942,15 @@ function restoreLlmResponseBody(body, map, options = {}) {
|
|
|
4569
4942
|
}
|
|
4570
4943
|
return clone;
|
|
4571
4944
|
}
|
|
4572
|
-
function createSseRestorer(map) {
|
|
4945
|
+
function createSseRestorer(map, options = {}) {
|
|
4946
|
+
const reasoningMap = filterMapExcluding(map, options.reasoningExcludedTokens);
|
|
4573
4947
|
const streams = /* @__PURE__ */ new Map();
|
|
4574
4948
|
const toolNames = /* @__PURE__ */ new Map();
|
|
4575
4949
|
const bufferedToolArgs = /* @__PURE__ */ new Map();
|
|
4576
|
-
const getStream = (key, escapeForJsonString = false) => {
|
|
4950
|
+
const getStream = (key, escapeForJsonString = false, useReasoningMap = false) => {
|
|
4577
4951
|
let r = streams.get(key);
|
|
4578
4952
|
if (!r) {
|
|
4579
|
-
r = new StreamingRestorer(map, { escapeForJsonString });
|
|
4953
|
+
r = new StreamingRestorer(useReasoningMap ? reasoningMap : map, { escapeForJsonString });
|
|
4580
4954
|
streams.set(key, r);
|
|
4581
4955
|
}
|
|
4582
4956
|
return r;
|
|
@@ -4774,7 +5148,9 @@ data: ${JSON.stringify({
|
|
|
4774
5148
|
if (parsed.delta.type === "text_delta" && typeof parsed.delta.text === "string") {
|
|
4775
5149
|
parsed.delta.text = getStream(`anth_text:${idx}`).push(parsed.delta.text);
|
|
4776
5150
|
} else if (parsed.delta.type === "thinking_delta" && typeof parsed.delta.thinking === "string") {
|
|
4777
|
-
parsed.delta.thinking = getStream(`anth_thinking:${idx}
|
|
5151
|
+
parsed.delta.thinking = getStream(`anth_thinking:${idx}`, false, true).push(
|
|
5152
|
+
parsed.delta.thinking
|
|
5153
|
+
);
|
|
4778
5154
|
} else if (parsed.delta.type === "input_json_delta" && typeof parsed.delta.partial_json === "string") {
|
|
4779
5155
|
const key = `anth_json:${idx}`;
|
|
4780
5156
|
const name = toolNames.get(key);
|
|
@@ -4838,12 +5214,14 @@ data: ${JSON.stringify({
|
|
|
4838
5214
|
delta.content = getStream(`openai_chat_text:${cIdx}`).push(delta.content);
|
|
4839
5215
|
}
|
|
4840
5216
|
if (typeof delta.reasoning_content === "string") {
|
|
4841
|
-
delta.reasoning_content = getStream(
|
|
4842
|
-
|
|
4843
|
-
|
|
5217
|
+
delta.reasoning_content = getStream(
|
|
5218
|
+
`openai_chat_reasoning:${cIdx}`,
|
|
5219
|
+
false,
|
|
5220
|
+
true
|
|
5221
|
+
).push(delta.reasoning_content);
|
|
4844
5222
|
}
|
|
4845
5223
|
if (typeof delta.reasoning === "string") {
|
|
4846
|
-
delta.reasoning = getStream(`openai_chat_reasoning_plain:${cIdx}
|
|
5224
|
+
delta.reasoning = getStream(`openai_chat_reasoning_plain:${cIdx}`, false, true).push(
|
|
4847
5225
|
delta.reasoning
|
|
4848
5226
|
);
|
|
4849
5227
|
}
|
|
@@ -4872,7 +5250,7 @@ data: ${JSON.stringify({
|
|
|
4872
5250
|
}
|
|
4873
5251
|
}
|
|
4874
5252
|
}
|
|
4875
|
-
if (choice.finish_reason
|
|
5253
|
+
if (typeof choice.finish_reason === "string" && choice.finish_reason.length > 0) {
|
|
4876
5254
|
const textTail = flushStream(`openai_chat_text:${cIdx}`);
|
|
4877
5255
|
if (textTail) {
|
|
4878
5256
|
if (!isPlainObject(choice.delta)) choice.delta = {};
|
|
@@ -5333,6 +5711,8 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5333
5711
|
};
|
|
5334
5712
|
const mcpSessions = /* @__PURE__ */ new Map();
|
|
5335
5713
|
const closedMcpSessions = /* @__PURE__ */ new Set();
|
|
5714
|
+
const messageTokensBySession = /* @__PURE__ */ new Map();
|
|
5715
|
+
const externalToolTokensBySession = /* @__PURE__ */ new Map();
|
|
5336
5716
|
if (auditLogPath) {
|
|
5337
5717
|
await mkdir(dirname(auditLogPath), { recursive: true });
|
|
5338
5718
|
await appendFile(auditLogPath, "", { encoding: "utf8", mode: 384 });
|
|
@@ -5514,6 +5894,8 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5514
5894
|
approvals.clearAll();
|
|
5515
5895
|
engine.clearCache();
|
|
5516
5896
|
inspector.clear();
|
|
5897
|
+
messageTokensBySession.clear();
|
|
5898
|
+
externalToolTokensBySession.clear();
|
|
5517
5899
|
totalRequests = 0;
|
|
5518
5900
|
totalRedactions = 0;
|
|
5519
5901
|
totalCounts = {};
|
|
@@ -5633,6 +6015,11 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5633
6015
|
res.end(JSON.stringify({ error: "Unknown inspector route." }));
|
|
5634
6016
|
return;
|
|
5635
6017
|
}
|
|
6018
|
+
if ((method === "GET" || method === "HEAD") && BROWSER_PROBE_PATHS.has(normalizedPath)) {
|
|
6019
|
+
res.writeHead(204, INSPECTOR_SECURITY_HEADERS);
|
|
6020
|
+
res.end();
|
|
6021
|
+
return;
|
|
6022
|
+
}
|
|
5636
6023
|
if (normalizedPath === "/mcp" || normalizedPath === "/mcp/") {
|
|
5637
6024
|
if (!isLoopbackRemoteAddress(req.socket.remoteAddress) || !isLoopbackHost(req.headers.host, host, boundPort, true)) {
|
|
5638
6025
|
res.writeHead(403, {
|
|
@@ -5862,9 +6249,22 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5862
6249
|
return;
|
|
5863
6250
|
}
|
|
5864
6251
|
sessionId = deriveProxySessionId(req.headers, parsed);
|
|
6252
|
+
let externalTokensForSession = externalToolTokensBySession.get(sessionId);
|
|
6253
|
+
const incomingToolTokens = /* @__PURE__ */ new Set();
|
|
6254
|
+
collectExternalToolResultPlaceholders(parsed, incomingToolTokens);
|
|
6255
|
+
if (incomingToolTokens.size > 0) {
|
|
6256
|
+
if (!externalTokensForSession) {
|
|
6257
|
+
externalTokensForSession = /* @__PURE__ */ new Set();
|
|
6258
|
+
externalToolTokensBySession.set(sessionId, externalTokensForSession);
|
|
6259
|
+
}
|
|
6260
|
+
for (const tok of incomingToolTokens) externalTokensForSession.add(tok);
|
|
6261
|
+
}
|
|
6262
|
+
let messageTokensForSession = messageTokensBySession.get(sessionId);
|
|
5865
6263
|
let redactedObj;
|
|
5866
6264
|
try {
|
|
5867
|
-
const
|
|
6265
|
+
const messagePreviews = [];
|
|
6266
|
+
const fallbackPreviews = [];
|
|
6267
|
+
const redactChunk = async (chunk, forceRules, previewable) => {
|
|
5868
6268
|
if (!chunk.trim()) return chunk;
|
|
5869
6269
|
const { result } = await engine.redact(chunk, {
|
|
5870
6270
|
mode: forceRules ? "rules" : void 0,
|
|
@@ -5874,9 +6274,20 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5874
6274
|
const applied = vault.apply(chunk, result, sessionId, proxyVaultOwner);
|
|
5875
6275
|
stats.redactions += applied.totalRedactions;
|
|
5876
6276
|
mergeCounts(stats.counts, applied.counts);
|
|
5877
|
-
if (
|
|
5878
|
-
|
|
5879
|
-
|
|
6277
|
+
if (previewable && applied.totalRedactions > 0) {
|
|
6278
|
+
if (!messageTokensForSession) {
|
|
6279
|
+
messageTokensForSession = /* @__PURE__ */ new Set();
|
|
6280
|
+
messageTokensBySession.set(sessionId, messageTokensForSession);
|
|
6281
|
+
}
|
|
6282
|
+
for (const tok of Object.keys(applied.map)) {
|
|
6283
|
+
messageTokensForSession.add(tok);
|
|
6284
|
+
}
|
|
6285
|
+
}
|
|
6286
|
+
const snippet = applied.redacted.length > 1200 ? `${applied.redacted.slice(0, 1200)}...` : applied.redacted;
|
|
6287
|
+
if (previewable) {
|
|
6288
|
+
if (messagePreviews.length < 6) messagePreviews.push(snippet);
|
|
6289
|
+
} else if (applied.totalRedactions > 0 && fallbackPreviews.length < 4) {
|
|
6290
|
+
fallbackPreviews.push(snippet);
|
|
5880
6291
|
}
|
|
5881
6292
|
return applied.redacted;
|
|
5882
6293
|
};
|
|
@@ -5885,21 +6296,12 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5885
6296
|
async (text, kind) => {
|
|
5886
6297
|
if (!text.trim()) return text;
|
|
5887
6298
|
if (kind === "tool_description") {
|
|
5888
|
-
return redactChunk(text, true);
|
|
6299
|
+
return redactChunk(text, true, false);
|
|
5889
6300
|
}
|
|
5890
|
-
if (kind === "system") {
|
|
5891
|
-
|
|
5892
|
-
const parts = text.split(/(\n{2,})/);
|
|
5893
|
-
for (let i = 0; i < parts.length; i += 2) {
|
|
5894
|
-
const para = parts[i] ?? "";
|
|
5895
|
-
if (!para.trim()) continue;
|
|
5896
|
-
parts[i] = await redactChunk(para, isLikelyEnglishSystemPrompt(para));
|
|
5897
|
-
}
|
|
5898
|
-
return parts.join("");
|
|
5899
|
-
}
|
|
5900
|
-
return redactChunk(text, isLikelyEnglishSystemPrompt(text));
|
|
6301
|
+
if (kind === "system" || kind === "reasoning") {
|
|
6302
|
+
return redactSystemText(text, redactChunk);
|
|
5901
6303
|
}
|
|
5902
|
-
return
|
|
6304
|
+
return redactMessageText(text, redactChunk);
|
|
5903
6305
|
},
|
|
5904
6306
|
{
|
|
5905
6307
|
mutate: true,
|
|
@@ -5908,6 +6310,7 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5908
6310
|
}
|
|
5909
6311
|
}
|
|
5910
6312
|
);
|
|
6313
|
+
redactedPreview.push(...messagePreviews.length > 0 ? messagePreviews : fallbackPreviews);
|
|
5911
6314
|
} catch (err) {
|
|
5912
6315
|
recordAuditAndInspector({
|
|
5913
6316
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -6019,13 +6422,23 @@ async function startMaskeraProxy(options = {}) {
|
|
|
6019
6422
|
if (requestWarnings) {
|
|
6020
6423
|
outHeaders["x-maskera-warning"] = "uninspectable-binary-attachment";
|
|
6021
6424
|
}
|
|
6022
|
-
const
|
|
6425
|
+
const rawSessionMap = restoreResponses ? vault.getMap(sessionId, proxyVaultOwner) : {};
|
|
6426
|
+
const externalTokens = externalToolTokensBySession.get(sessionId);
|
|
6427
|
+
const messageTokens = messageTokensBySession.get(sessionId);
|
|
6428
|
+
let sessionMap = rawSessionMap;
|
|
6429
|
+
if (externalTokens && externalTokens.size > 0) {
|
|
6430
|
+
sessionMap = {};
|
|
6431
|
+
for (const [token, value] of Object.entries(rawSessionMap)) {
|
|
6432
|
+
if (externalTokens.has(token) && !messageTokens?.has(token)) continue;
|
|
6433
|
+
sessionMap[token] = value;
|
|
6434
|
+
}
|
|
6435
|
+
}
|
|
6023
6436
|
const hasMap = Object.keys(sessionMap).length > 0;
|
|
6024
6437
|
const resContentType = (upstreamRes.headers.get("content-type") ?? "").toLowerCase();
|
|
6025
6438
|
const isSse = resContentType.includes("text/event-stream");
|
|
6026
6439
|
if (isSse && upstreamRes.body) {
|
|
6027
6440
|
res.writeHead(upstreamRes.status, outHeaders);
|
|
6028
|
-
const sse = hasMap ? createSseRestorer(sessionMap) : null;
|
|
6441
|
+
const sse = hasMap ? createSseRestorer(sessionMap, { reasoningExcludedTokens: externalTokens }) : null;
|
|
6029
6442
|
const decoder = sse ? new TextDecoder() : null;
|
|
6030
6443
|
const reader = upstreamRes.body.getReader();
|
|
6031
6444
|
const onStreamAbort = () => {
|
|
@@ -6084,10 +6497,14 @@ async function startMaskeraProxy(options = {}) {
|
|
|
6084
6497
|
} finally {
|
|
6085
6498
|
res.off("close", onClientClose);
|
|
6086
6499
|
}
|
|
6500
|
+
const errorDetail = await extractSafeUpstreamError(resBuf, upstreamRes.status, engine);
|
|
6087
6501
|
if (hasMap && resContentType.includes("application/json")) {
|
|
6088
6502
|
try {
|
|
6089
6503
|
const parsed = JSON.parse(resBuf.toString("utf8"));
|
|
6090
|
-
const restored = restoreLlmResponseBody(parsed, sessionMap, {
|
|
6504
|
+
const restored = restoreLlmResponseBody(parsed, sessionMap, {
|
|
6505
|
+
mutate: true,
|
|
6506
|
+
reasoningExcludedTokens: externalTokens
|
|
6507
|
+
});
|
|
6091
6508
|
const restoredBuf = Buffer.from(JSON.stringify(restored), "utf8");
|
|
6092
6509
|
res.writeHead(upstreamRes.status, outHeaders);
|
|
6093
6510
|
res.end(restoredBuf);
|
|
@@ -6104,6 +6521,7 @@ async function startMaskeraProxy(options = {}) {
|
|
|
6104
6521
|
redactions: stats.redactions,
|
|
6105
6522
|
counts: stats.counts,
|
|
6106
6523
|
redactedPreview,
|
|
6524
|
+
...errorDetail ? { errorDetail } : {},
|
|
6107
6525
|
...requestWarnings ? { warnings: requestWarnings } : {}
|
|
6108
6526
|
});
|
|
6109
6527
|
log(
|
|
@@ -6128,6 +6546,7 @@ async function startMaskeraProxy(options = {}) {
|
|
|
6128
6546
|
redactions: stats.redactions,
|
|
6129
6547
|
counts: stats.counts,
|
|
6130
6548
|
redactedPreview,
|
|
6549
|
+
...errorDetail ? { errorDetail } : {},
|
|
6131
6550
|
...requestWarnings ? { warnings: requestWarnings } : {}
|
|
6132
6551
|
});
|
|
6133
6552
|
log(
|