@maskera/mcp 0.4.2 → 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 +4 -3
package/dist/cli.js
CHANGED
|
@@ -775,7 +775,7 @@ Pick by where the text comes from: **proxy for text typed straight into the chat
|
|
|
775
775
|
# Same warm process also serves Streamable HTTP MCP:
|
|
776
776
|
# http://127.0.0.1:8787/mcp (supports ?no_raw_restore=1&read_only=1&approval_mode=browser)
|
|
777
777
|
\`\`\`
|
|
778
|
-
- 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/
|
|
778
|
+
- 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\`.
|
|
779
779
|
- Routes Anthropic (\`/v1/messages\`) and OpenAI (\`/v1/chat/completions\`, \`/v1/responses\`) automatically, or set \`--upstream <url>\`.
|
|
780
780
|
- Restores placeholders transparently in JSON, tool arguments and chunk-split SSE streams.
|
|
781
781
|
- Fails closed (\`502\`) if local redaction fails. Inspect counts only at \`http://127.0.0.1:8787/_maskera/status\`.
|
|
@@ -1901,7 +1901,7 @@ function createMaskeraMcpServer(options = {}) {
|
|
|
1901
1901
|
);
|
|
1902
1902
|
}
|
|
1903
1903
|
const response = await requestElicitation({
|
|
1904
|
-
message: `${input.tool} wants to release ${input.placeholderCount} original Swedish PII value(s) into the model context (${input.reason})
|
|
1904
|
+
message: `${input.tool} wants to release ${input.placeholderCount} original Swedish PII value(s) into the model context (${input.reason}).`,
|
|
1905
1905
|
requestedSchema: {
|
|
1906
1906
|
type: "object",
|
|
1907
1907
|
properties: {
|
|
@@ -3702,6 +3702,11 @@ function renderInspectorHtml(info) {
|
|
|
3702
3702
|
white-space: pre-wrap;
|
|
3703
3703
|
word-break: break-word;
|
|
3704
3704
|
}
|
|
3705
|
+
.preview.err-preview {
|
|
3706
|
+
border-color: var(--err);
|
|
3707
|
+
background: var(--err-bg);
|
|
3708
|
+
color: var(--err);
|
|
3709
|
+
}
|
|
3705
3710
|
.tok {
|
|
3706
3711
|
display: inline-block;
|
|
3707
3712
|
padding: 0 4px;
|
|
@@ -3734,7 +3739,7 @@ function renderInspectorHtml(info) {
|
|
|
3734
3739
|
</div>
|
|
3735
3740
|
<div class="actions">
|
|
3736
3741
|
<button id="selftest-btn" type="button" class="primary">K\xF6r sj\xE4lvtest</button>
|
|
3737
|
-
<button id="reveal-btn" type="button">Visa originalv\xE4rden
|
|
3742
|
+
<button id="reveal-btn" type="button">Visa originalv\xE4rden</button>
|
|
3738
3743
|
<button id="clear-btn" type="button">Rensa minne</button>
|
|
3739
3744
|
</div>
|
|
3740
3745
|
</header>
|
|
@@ -3775,6 +3780,16 @@ function renderInspectorHtml(info) {
|
|
|
3775
3780
|
|
|
3776
3781
|
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ({ "&":"&","<":"<",">":">",'"':""","'":"'" }[c]));
|
|
3777
3782
|
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>");
|
|
3783
|
+
const formatLocalTime = (iso) => {
|
|
3784
|
+
if (!iso) return "";
|
|
3785
|
+
const d = new Date(iso);
|
|
3786
|
+
if (Number.isNaN(d.getTime())) return String(iso).slice(11, 19);
|
|
3787
|
+
try {
|
|
3788
|
+
return d.toLocaleTimeString("sv-SE", { hour12: false });
|
|
3789
|
+
} catch {
|
|
3790
|
+
return String(iso).slice(11, 19);
|
|
3791
|
+
}
|
|
3792
|
+
};
|
|
3778
3793
|
|
|
3779
3794
|
function renderReceipt() {
|
|
3780
3795
|
if (!lastReceipt) {
|
|
@@ -3787,7 +3802,7 @@ function renderInspectorHtml(info) {
|
|
|
3787
3802
|
return '<li><span class="receipt-tag' + cls + '">[' + esc(c.status) + ']</span><span><strong>' + esc(c.name) + ':</strong> ' + esc(c.detail) + '</span></li>';
|
|
3788
3803
|
}).join("");
|
|
3789
3804
|
const proofHtml = r.sampleProof && !r.sampleProof.rawPiiLeaked
|
|
3790
|
-
? '<p class="body-label">
|
|
3805
|
+
? '<p class="body-label">Maskerat utdata:</p><pre class="preview">' + highlightTokens(r.sampleProof.redactedPreview) + '</pre>'
|
|
3791
3806
|
: "";
|
|
3792
3807
|
receiptRoot.innerHTML =
|
|
3793
3808
|
'<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>' +
|
|
@@ -3884,7 +3899,7 @@ function renderInspectorHtml(info) {
|
|
|
3884
3899
|
const r = await fetch("/_maskera/vault", { headers: { "x-maskera-reveal": "1" } });
|
|
3885
3900
|
const d = await r.json();
|
|
3886
3901
|
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("");
|
|
3887
|
-
vaultRoot.innerHTML = '<section class="vault-box"><h2
|
|
3902
|
+
vaultRoot.innerHTML = '<section class="vault-box"><h2>\xC5terst\xE4llningsnyckel</h2>' +
|
|
3888
3903
|
(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>") +
|
|
3889
3904
|
"</section>";
|
|
3890
3905
|
} catch {}
|
|
@@ -3893,8 +3908,8 @@ function renderInspectorHtml(info) {
|
|
|
3893
3908
|
function render() {
|
|
3894
3909
|
if (!items.length) {
|
|
3895
3910
|
feedEl.innerHTML =
|
|
3896
|
-
'<div class="empty"><p class="empty-title">
|
|
3897
|
-
'
|
|
3911
|
+
'<div class="empty"><p class="empty-title">Inga anrop \xE4nnu.</p>' +
|
|
3912
|
+
'Skicka ett prov fr\xE5n din klient s\xE5 visas maskeringen h\xE4r:' +
|
|
3898
3913
|
'<div class="sample-row"><span class="sample-text">' + esc(SAMPLE_PROMPT) + '</span><button id="copy-sample-btn" type="button">Kopiera testmening</button></div>' +
|
|
3899
3914
|
'<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>';
|
|
3900
3915
|
const copyBtn = document.getElementById("copy-sample-btn");
|
|
@@ -3914,15 +3929,16 @@ function renderInspectorHtml(info) {
|
|
|
3914
3929
|
}
|
|
3915
3930
|
for (const it of items) seenIds.add(it.id);
|
|
3916
3931
|
feedEl.innerHTML = '<div class="list">' + items.map((it) => {
|
|
3917
|
-
const time = (it.timestamp
|
|
3932
|
+
const time = formatLocalTime(it.timestamp);
|
|
3918
3933
|
const tags = Object.entries(it.counts || {}).map(([k, v]) => '<span class="count-tag">' + esc(k) + " \xD7" + v + "</span>").join("");
|
|
3919
3934
|
const previews = (it.redactedPreview || []).map((p) => '<pre class="preview">' + highlightTokens(p) + "</pre>").join("");
|
|
3935
|
+
const errBlock = it.errorDetail ? '<p class="body-label">Svar fr\xE5n API (fel):</p><pre class="preview err-preview">' + esc(it.errorDetail) + "</pre>" : "";
|
|
3920
3936
|
const statusCls = it.status >= 200 && it.status < 400 ? "" : " err";
|
|
3921
3937
|
const isOpen = openIds.has(it.id);
|
|
3922
3938
|
return '<details class="item" data-id="' + esc(it.id) + '"' + (isOpen ? " open" : "") + '>' +
|
|
3923
3939
|
'<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>' +
|
|
3924
3940
|
'<span class="right"><span>' + (it.streamed ? "SSE" : "JSON") + '</span><span>' + it.durationMs + ' ms</span><span>' + esc(time) + '</span></span></summary>' +
|
|
3925
|
-
'<div class="body"
|
|
3941
|
+
'<div class="body">' + errBlock + '<p class="body-label">Skickat vidare:</p>' + (previews || '<pre class="preview">(ingen text i anropet)</pre>') + '</div></details>';
|
|
3926
3942
|
}).join("") + "</div>";
|
|
3927
3943
|
for (const det of feedEl.querySelectorAll("details.item")) {
|
|
3928
3944
|
det.addEventListener("toggle", () => {
|
|
@@ -3938,7 +3954,7 @@ function renderInspectorHtml(info) {
|
|
|
3938
3954
|
revealBtn.addEventListener("click", () => {
|
|
3939
3955
|
reveal = !reveal;
|
|
3940
3956
|
revealBtn.classList.toggle("active", reveal);
|
|
3941
|
-
revealBtn.textContent = reveal ? "D\xF6lj originalv\xE4rden" : "Visa originalv\xE4rden
|
|
3957
|
+
revealBtn.textContent = reveal ? "D\xF6lj originalv\xE4rden" : "Visa originalv\xE4rden";
|
|
3942
3958
|
void refreshVault();
|
|
3943
3959
|
});
|
|
3944
3960
|
|
|
@@ -4125,7 +4141,7 @@ function restoreJsonLeaves(value, map) {
|
|
|
4125
4141
|
}
|
|
4126
4142
|
return value;
|
|
4127
4143
|
}
|
|
4128
|
-
var MASKERA_VAULT_TOOL_RE = /(?:^|
|
|
4144
|
+
var MASKERA_VAULT_TOOL_RE = /(?:^|[^a-zA-Z0-9]|_)maskera_(?:write_file|restore|clear_session)$/;
|
|
4129
4145
|
var MASKERA_VAULT_CODE_RE = /\bmaskera_(?:write_file|restore)\b/;
|
|
4130
4146
|
function isMaskeraVaultToolName(name) {
|
|
4131
4147
|
return typeof name === "string" && MASKERA_VAULT_TOOL_RE.test(name);
|
|
@@ -4136,10 +4152,80 @@ function shouldSkipToolArgRestore(toolName, argsText) {
|
|
|
4136
4152
|
return false;
|
|
4137
4153
|
}
|
|
4138
4154
|
var SWEDISH_DIACRITIC_RE = /[åäöÅÄÖ]/;
|
|
4139
|
-
var
|
|
4155
|
+
var SWEDISH_STOP_WORDS = /* @__PURE__ */ new Set([
|
|
4156
|
+
"och",
|
|
4157
|
+
"att",
|
|
4158
|
+
"det",
|
|
4159
|
+
"som",
|
|
4160
|
+
"f\xF6r",
|
|
4161
|
+
"med",
|
|
4162
|
+
"har",
|
|
4163
|
+
"inte",
|
|
4164
|
+
"p\xE5",
|
|
4165
|
+
"\xE4r",
|
|
4166
|
+
"av",
|
|
4167
|
+
"till",
|
|
4168
|
+
"ett",
|
|
4169
|
+
"fr\xE5n",
|
|
4170
|
+
"kan",
|
|
4171
|
+
"ska",
|
|
4172
|
+
"eller",
|
|
4173
|
+
"vid",
|
|
4174
|
+
"s\xE5",
|
|
4175
|
+
"men",
|
|
4176
|
+
"den",
|
|
4177
|
+
"jag",
|
|
4178
|
+
"din",
|
|
4179
|
+
"ditt",
|
|
4180
|
+
"mitt",
|
|
4181
|
+
"var",
|
|
4182
|
+
"blev",
|
|
4183
|
+
"enligt",
|
|
4184
|
+
"kund",
|
|
4185
|
+
"personnummer",
|
|
4186
|
+
"svenska",
|
|
4187
|
+
"sekretess",
|
|
4188
|
+
"arbetsgivare",
|
|
4189
|
+
"utbetalning",
|
|
4190
|
+
"sammanfatta",
|
|
4191
|
+
"skriv",
|
|
4192
|
+
"svara",
|
|
4193
|
+
"nedan",
|
|
4194
|
+
"uppgifter",
|
|
4195
|
+
"v\xE4nliga",
|
|
4196
|
+
"h\xE4lsningar",
|
|
4197
|
+
"\xE4rendet",
|
|
4198
|
+
"\xE4rende",
|
|
4199
|
+
"mejl",
|
|
4200
|
+
"brev",
|
|
4201
|
+
"patient",
|
|
4202
|
+
"namn",
|
|
4203
|
+
"telefon",
|
|
4204
|
+
"adress"
|
|
4205
|
+
]);
|
|
4206
|
+
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;
|
|
4140
4207
|
var ENGLISH_BOILERPLATE_WORDS = /* @__PURE__ */ new Set([
|
|
4141
4208
|
"the",
|
|
4142
4209
|
"and",
|
|
4210
|
+
"to",
|
|
4211
|
+
"of",
|
|
4212
|
+
"in",
|
|
4213
|
+
"is",
|
|
4214
|
+
"it",
|
|
4215
|
+
"on",
|
|
4216
|
+
"as",
|
|
4217
|
+
"be",
|
|
4218
|
+
"by",
|
|
4219
|
+
"or",
|
|
4220
|
+
"an",
|
|
4221
|
+
"at",
|
|
4222
|
+
"if",
|
|
4223
|
+
"do",
|
|
4224
|
+
"no",
|
|
4225
|
+
"yes",
|
|
4226
|
+
"so",
|
|
4227
|
+
"but",
|
|
4228
|
+
"not",
|
|
4143
4229
|
"you",
|
|
4144
4230
|
"your",
|
|
4145
4231
|
"with",
|
|
@@ -4147,7 +4233,6 @@ var ENGLISH_BOILERPLATE_WORDS = /* @__PURE__ */ new Set([
|
|
|
4147
4233
|
"this",
|
|
4148
4234
|
"from",
|
|
4149
4235
|
"are",
|
|
4150
|
-
"not",
|
|
4151
4236
|
"when",
|
|
4152
4237
|
"use",
|
|
4153
4238
|
"must",
|
|
@@ -4155,7 +4240,9 @@ var ENGLISH_BOILERPLATE_WORDS = /* @__PURE__ */ new Set([
|
|
|
4155
4240
|
"never",
|
|
4156
4241
|
"always",
|
|
4157
4242
|
"tools",
|
|
4243
|
+
"tool",
|
|
4158
4244
|
"files",
|
|
4245
|
+
"file",
|
|
4159
4246
|
"code",
|
|
4160
4247
|
"return",
|
|
4161
4248
|
"output",
|
|
@@ -4163,24 +4250,296 @@ var ENGLISH_BOILERPLATE_WORDS = /* @__PURE__ */ new Set([
|
|
|
4163
4250
|
"assistant",
|
|
4164
4251
|
"instructions",
|
|
4165
4252
|
"command",
|
|
4166
|
-
"
|
|
4253
|
+
"commands",
|
|
4254
|
+
"user",
|
|
4255
|
+
"for",
|
|
4256
|
+
"into",
|
|
4257
|
+
"only",
|
|
4258
|
+
"before",
|
|
4259
|
+
"after",
|
|
4260
|
+
"run",
|
|
4261
|
+
"check",
|
|
4262
|
+
"edit",
|
|
4263
|
+
"read",
|
|
4264
|
+
"write",
|
|
4265
|
+
"git",
|
|
4266
|
+
"branch",
|
|
4267
|
+
"commit",
|
|
4268
|
+
"push",
|
|
4269
|
+
"test",
|
|
4270
|
+
"tests",
|
|
4271
|
+
"error",
|
|
4272
|
+
"path",
|
|
4273
|
+
"directory",
|
|
4274
|
+
"repository",
|
|
4275
|
+
"session",
|
|
4276
|
+
"agent",
|
|
4277
|
+
"prompt",
|
|
4278
|
+
"message",
|
|
4279
|
+
"system",
|
|
4280
|
+
"default",
|
|
4281
|
+
"without",
|
|
4282
|
+
"can",
|
|
4283
|
+
"will",
|
|
4284
|
+
"each",
|
|
4285
|
+
"all",
|
|
4286
|
+
"any",
|
|
4287
|
+
"every",
|
|
4288
|
+
"about",
|
|
4289
|
+
"more",
|
|
4290
|
+
"also",
|
|
4291
|
+
"make",
|
|
4292
|
+
"keep",
|
|
4293
|
+
"does",
|
|
4294
|
+
"have",
|
|
4295
|
+
"has",
|
|
4296
|
+
"been",
|
|
4297
|
+
"its",
|
|
4298
|
+
"their",
|
|
4299
|
+
"them",
|
|
4300
|
+
"then",
|
|
4301
|
+
"than",
|
|
4302
|
+
"using",
|
|
4303
|
+
"following",
|
|
4304
|
+
"other",
|
|
4305
|
+
"same",
|
|
4306
|
+
"first",
|
|
4307
|
+
"next",
|
|
4308
|
+
"existing",
|
|
4309
|
+
"changes",
|
|
4310
|
+
"where",
|
|
4311
|
+
"which",
|
|
4312
|
+
"what",
|
|
4313
|
+
"how",
|
|
4314
|
+
"why",
|
|
4315
|
+
"because",
|
|
4316
|
+
"unless",
|
|
4317
|
+
"instead",
|
|
4318
|
+
"below",
|
|
4319
|
+
"above",
|
|
4320
|
+
"work",
|
|
4321
|
+
"working",
|
|
4322
|
+
"task",
|
|
4323
|
+
"tasks",
|
|
4324
|
+
"skill",
|
|
4325
|
+
"skills",
|
|
4326
|
+
"rule",
|
|
4327
|
+
"rules",
|
|
4328
|
+
"repo",
|
|
4329
|
+
"project",
|
|
4330
|
+
"build",
|
|
4331
|
+
"lint",
|
|
4332
|
+
"hook",
|
|
4333
|
+
"hooks",
|
|
4334
|
+
"memory",
|
|
4335
|
+
"local",
|
|
4336
|
+
"remote",
|
|
4337
|
+
"status",
|
|
4338
|
+
"current",
|
|
4339
|
+
"recent"
|
|
4167
4340
|
]);
|
|
4168
|
-
var
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
|
|
4341
|
+
var WORD_TOKEN_RE2 = /[a-zåäö]{2,}/gi;
|
|
4342
|
+
var SYSTEM_REMINDER_SPLIT_RE = /(<system-reminder>[\s\S]*?<\/system-reminder>)/g;
|
|
4343
|
+
var FENCED_CODE_SPLIT_RE = /(```[\s\S]*?```)/g;
|
|
4344
|
+
var GIT_STATUS_LINE_RE = /^(?:[MADRCU?!]{1,2}\s+\S|[0-9a-f]{7,40}\s+\S)/;
|
|
4345
|
+
var XML_TAG_ONLY_RE = /^<\/?[a-z][a-z0-9_-]*(?:\s[^>]*)?>$/i;
|
|
4346
|
+
var MARKDOWN_HEADING_RE = /^#{1,6}\s+\S/;
|
|
4347
|
+
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*\{)/;
|
|
4348
|
+
var BROWSER_PROBE_PATHS = /* @__PURE__ */ new Set([
|
|
4349
|
+
"/favicon.ico",
|
|
4350
|
+
"/apple-touch-icon.png",
|
|
4351
|
+
"/apple-touch-icon-precomposed.png",
|
|
4352
|
+
"/robots.txt"
|
|
4353
|
+
]);
|
|
4354
|
+
var PLACEHOLDER_TOKEN_RE = /\[[A-ZÅÄÖ][A-ZÅÄÖ0-9_]*_\d+\]/g;
|
|
4355
|
+
function collectPlaceholdersFromUnknown(value, out) {
|
|
4356
|
+
if (typeof value === "string") {
|
|
4357
|
+
if (!value.includes("[")) return;
|
|
4358
|
+
const matches = value.match(PLACEHOLDER_TOKEN_RE);
|
|
4359
|
+
if (matches) {
|
|
4360
|
+
for (const tok of matches) out.add(tok);
|
|
4361
|
+
}
|
|
4362
|
+
return;
|
|
4363
|
+
}
|
|
4364
|
+
if (Array.isArray(value)) {
|
|
4365
|
+
for (const item of value) collectPlaceholdersFromUnknown(item, out);
|
|
4366
|
+
return;
|
|
4367
|
+
}
|
|
4368
|
+
if (isPlainObject(value)) {
|
|
4369
|
+
for (const v of Object.values(value)) collectPlaceholdersFromUnknown(v, out);
|
|
4370
|
+
}
|
|
4371
|
+
}
|
|
4372
|
+
function collectExternalToolResultPlaceholders(body, out) {
|
|
4373
|
+
if (Array.isArray(body.messages)) {
|
|
4374
|
+
for (const msg of body.messages) {
|
|
4375
|
+
if (!isPlainObject(msg)) continue;
|
|
4376
|
+
if (msg.role === "tool") {
|
|
4377
|
+
collectPlaceholdersFromUnknown(msg.content, out);
|
|
4378
|
+
continue;
|
|
4379
|
+
}
|
|
4380
|
+
if (Array.isArray(msg.content)) {
|
|
4381
|
+
for (const block of msg.content) {
|
|
4382
|
+
if (isPlainObject(block) && block.type === "tool_result") {
|
|
4383
|
+
collectPlaceholdersFromUnknown(block.content, out);
|
|
4384
|
+
}
|
|
4385
|
+
}
|
|
4386
|
+
}
|
|
4387
|
+
}
|
|
4388
|
+
}
|
|
4389
|
+
if (Array.isArray(body.input)) {
|
|
4390
|
+
for (const item of body.input) {
|
|
4391
|
+
if (isPlainObject(item) && item.type === "function_call_output") {
|
|
4392
|
+
collectPlaceholdersFromUnknown(item.output, out);
|
|
4393
|
+
}
|
|
4394
|
+
}
|
|
4395
|
+
}
|
|
4396
|
+
}
|
|
4397
|
+
function isToolCatalogJson(text) {
|
|
4398
|
+
const trimmed = text.trim();
|
|
4399
|
+
if (!trimmed.startsWith("{") || !trimmed.includes('"items"') || !trimmed.includes('"tools.')) {
|
|
4400
|
+
return false;
|
|
4401
|
+
}
|
|
4402
|
+
try {
|
|
4403
|
+
const parsed = JSON.parse(trimmed);
|
|
4404
|
+
if (!isPlainObject(parsed) || !Array.isArray(parsed.items) || parsed.items.length === 0) {
|
|
4405
|
+
return false;
|
|
4406
|
+
}
|
|
4407
|
+
return parsed.items.every(
|
|
4408
|
+
(item) => isPlainObject(item) && typeof item.path === "string" && item.path.startsWith("tools.") && (item.description === void 0 || typeof item.description === "string")
|
|
4409
|
+
);
|
|
4410
|
+
} catch {
|
|
4411
|
+
return false;
|
|
4412
|
+
}
|
|
4413
|
+
}
|
|
4414
|
+
function isEnglishSystemParagraph(paragraph) {
|
|
4415
|
+
const trimmed = paragraph.trim();
|
|
4416
|
+
if (!trimmed) return true;
|
|
4417
|
+
if (XML_TAG_ONLY_RE.test(trimmed)) return true;
|
|
4418
|
+
if (trimmed.startsWith("```") && trimmed.endsWith("```")) return true;
|
|
4419
|
+
if (CLI_SYSTEM_HEADER_RE.test(trimmed)) return true;
|
|
4420
|
+
const nonEmptyLines = trimmed.split("\n").map((l) => l.trim()).filter(Boolean);
|
|
4421
|
+
if (nonEmptyLines.length > 0 && nonEmptyLines.every(
|
|
4422
|
+
(l) => GIT_STATUS_LINE_RE.test(l) || XML_TAG_ONLY_RE.test(l) || CLI_SYSTEM_HEADER_RE.test(l) || l.startsWith("(use ")
|
|
4423
|
+
)) {
|
|
4424
|
+
return true;
|
|
4425
|
+
}
|
|
4426
|
+
let enTotal = 0;
|
|
4427
|
+
const enDistinct = /* @__PURE__ */ new Set();
|
|
4428
|
+
let svTotal = 0;
|
|
4429
|
+
let svDiacriticWords = 0;
|
|
4430
|
+
const lower = trimmed.toLowerCase();
|
|
4431
|
+
WORD_TOKEN_RE2.lastIndex = 0;
|
|
4432
|
+
for (let m = WORD_TOKEN_RE2.exec(lower); m !== null; m = WORD_TOKEN_RE2.exec(lower)) {
|
|
4177
4433
|
const word = m[0];
|
|
4178
|
-
if (word
|
|
4179
|
-
|
|
4180
|
-
|
|
4434
|
+
if (!word) continue;
|
|
4435
|
+
if (SWEDISH_STOP_WORDS.has(word)) {
|
|
4436
|
+
svTotal += 1;
|
|
4437
|
+
}
|
|
4438
|
+
if (SWEDISH_DIACRITIC_RE.test(word)) {
|
|
4439
|
+
svDiacriticWords += 1;
|
|
4440
|
+
}
|
|
4441
|
+
if (ENGLISH_BOILERPLATE_WORDS.has(word)) {
|
|
4442
|
+
enTotal += 1;
|
|
4443
|
+
enDistinct.add(word);
|
|
4181
4444
|
}
|
|
4182
4445
|
}
|
|
4183
|
-
|
|
4446
|
+
if (nonEmptyLines.length === 1 && MARKDOWN_HEADING_RE.test(trimmed) && svTotal === 0 && svDiacriticWords === 0 && trimmed.length < 120) {
|
|
4447
|
+
return true;
|
|
4448
|
+
}
|
|
4449
|
+
if (svTotal === 0 && svDiacriticWords === 0 && CODE_SIGNATURE_RE.test(trimmed)) {
|
|
4450
|
+
return true;
|
|
4451
|
+
}
|
|
4452
|
+
if (trimmed.length < 60 && enDistinct.size < 4) return false;
|
|
4453
|
+
const svSignal = svTotal + svDiacriticWords;
|
|
4454
|
+
if (svSignal === 0) {
|
|
4455
|
+
const minDistinct = trimmed.length >= 240 ? 5 : 3;
|
|
4456
|
+
return enDistinct.size >= minDistinct;
|
|
4457
|
+
}
|
|
4458
|
+
return enTotal >= 12 && enDistinct.size >= 6 && enTotal >= svSignal * 6;
|
|
4459
|
+
}
|
|
4460
|
+
function isLikelyEnglishSystemPrompt(text) {
|
|
4461
|
+
const trimmed = text.trim();
|
|
4462
|
+
if (!trimmed) return false;
|
|
4463
|
+
if (trimmed.includes("\n\n")) {
|
|
4464
|
+
const paragraphs = trimmed.split(/\n{2,}/).map((p) => p.trim()).filter(Boolean);
|
|
4465
|
+
return paragraphs.length > 0 && paragraphs.every((p) => isEnglishSystemParagraph(p));
|
|
4466
|
+
}
|
|
4467
|
+
return isEnglishSystemParagraph(trimmed);
|
|
4468
|
+
}
|
|
4469
|
+
async function redactSystemText(text, redactChunk) {
|
|
4470
|
+
if (isLikelyEnglishSystemPrompt(text)) {
|
|
4471
|
+
return redactChunk(text, true, false);
|
|
4472
|
+
}
|
|
4473
|
+
const redactSegment = async (segment) => {
|
|
4474
|
+
if (!segment.trim()) return segment;
|
|
4475
|
+
if (segment.startsWith("```") && segment.endsWith("```")) {
|
|
4476
|
+
return redactChunk(segment, true, false);
|
|
4477
|
+
}
|
|
4478
|
+
if (!segment.includes("\n\n")) {
|
|
4479
|
+
return redactChunk(segment, isEnglishSystemParagraph(segment), false);
|
|
4480
|
+
}
|
|
4481
|
+
const parts = segment.split(/(\n{2,})/);
|
|
4482
|
+
for (let i = 0; i < parts.length; i += 2) {
|
|
4483
|
+
const para = parts[i] ?? "";
|
|
4484
|
+
if (!para.trim()) continue;
|
|
4485
|
+
parts[i] = await redactChunk(para, isEnglishSystemParagraph(para), false);
|
|
4486
|
+
}
|
|
4487
|
+
return parts.join("");
|
|
4488
|
+
};
|
|
4489
|
+
if (!text.includes("```")) {
|
|
4490
|
+
return redactSegment(text);
|
|
4491
|
+
}
|
|
4492
|
+
const codeParts = text.split(FENCED_CODE_SPLIT_RE);
|
|
4493
|
+
for (let i = 0; i < codeParts.length; i += 1) {
|
|
4494
|
+
const part = codeParts[i] ?? "";
|
|
4495
|
+
if (!part.trim()) continue;
|
|
4496
|
+
codeParts[i] = await redactSegment(part);
|
|
4497
|
+
}
|
|
4498
|
+
return codeParts.join("");
|
|
4499
|
+
}
|
|
4500
|
+
async function redactMessageText(text, redactChunk) {
|
|
4501
|
+
if (!text.includes("<system-reminder>")) {
|
|
4502
|
+
return redactChunk(text, false, true);
|
|
4503
|
+
}
|
|
4504
|
+
const parts = text.split(SYSTEM_REMINDER_SPLIT_RE);
|
|
4505
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
4506
|
+
const segment = parts[i] ?? "";
|
|
4507
|
+
if (!segment.trim()) continue;
|
|
4508
|
+
if (segment.startsWith("<system-reminder>") && segment.endsWith("</system-reminder>")) {
|
|
4509
|
+
parts[i] = await redactSystemText(segment, redactChunk);
|
|
4510
|
+
} else {
|
|
4511
|
+
parts[i] = await redactChunk(segment, false, true);
|
|
4512
|
+
}
|
|
4513
|
+
}
|
|
4514
|
+
return parts.join("");
|
|
4515
|
+
}
|
|
4516
|
+
async function extractSafeUpstreamError(resBuf, status, engine) {
|
|
4517
|
+
if (status < 400 || resBuf.byteLength === 0) return void 0;
|
|
4518
|
+
let summary = "";
|
|
4519
|
+
try {
|
|
4520
|
+
const parsed = JSON.parse(resBuf.toString("utf8"));
|
|
4521
|
+
if (isPlainObject(parsed)) {
|
|
4522
|
+
const errObj = isPlainObject(parsed.error) ? parsed.error : parsed;
|
|
4523
|
+
const parts = [];
|
|
4524
|
+
if (typeof errObj.type === "string" && errObj.type.trim()) parts.push(errObj.type.trim());
|
|
4525
|
+
if (typeof errObj.code === "string" && errObj.code.trim()) parts.push(errObj.code.trim());
|
|
4526
|
+
if (typeof errObj.message === "string" && errObj.message.trim()) {
|
|
4527
|
+
parts.push(errObj.message.trim());
|
|
4528
|
+
}
|
|
4529
|
+
summary = parts.join(": ");
|
|
4530
|
+
}
|
|
4531
|
+
} catch {
|
|
4532
|
+
const raw = resBuf.toString("utf8").trim();
|
|
4533
|
+
if (raw && !raw.startsWith("<")) summary = raw;
|
|
4534
|
+
}
|
|
4535
|
+
if (!summary) return void 0;
|
|
4536
|
+
const trimmed = summary.length > 240 ? `${summary.slice(0, 240)}...` : summary;
|
|
4537
|
+
try {
|
|
4538
|
+
const { result } = await engine.redact(trimmed, { mode: "rules" });
|
|
4539
|
+
return result.text;
|
|
4540
|
+
} catch {
|
|
4541
|
+
return `HTTP ${status}`;
|
|
4542
|
+
}
|
|
4184
4543
|
}
|
|
4185
4544
|
function headerFirst(headers, name) {
|
|
4186
4545
|
const value = headers[name];
|
|
@@ -4251,13 +4610,14 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4251
4610
|
noteBinary("input_audio");
|
|
4252
4611
|
}
|
|
4253
4612
|
if (typeof block.text === "string") {
|
|
4254
|
-
|
|
4613
|
+
const blockKind = kind === "message" && isToolCatalogJson(block.text) ? "tool_description" : kind;
|
|
4614
|
+
block.text = await redactString(block.text, blockKind);
|
|
4255
4615
|
}
|
|
4256
4616
|
if (typeof block.refusal === "string") {
|
|
4257
4617
|
block.refusal = await redactString(block.refusal, kind);
|
|
4258
4618
|
}
|
|
4259
4619
|
if (typeof block.thinking === "string") {
|
|
4260
|
-
const redactedThinking = await redactString(block.thinking,
|
|
4620
|
+
const redactedThinking = await redactString(block.thinking, "reasoning");
|
|
4261
4621
|
if (redactedThinking !== block.thinking) {
|
|
4262
4622
|
block.thinking = redactedThinking;
|
|
4263
4623
|
delete block.signature;
|
|
@@ -4270,12 +4630,14 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4270
4630
|
block.context = await redactString(block.context, kind);
|
|
4271
4631
|
}
|
|
4272
4632
|
if (typeof block.content === "string") {
|
|
4273
|
-
|
|
4633
|
+
const blockKind = blockType === "tool_result" && isToolCatalogJson(block.content) ? "tool_description" : kind;
|
|
4634
|
+
block.content = await redactString(block.content, blockKind);
|
|
4274
4635
|
} else if (Array.isArray(block.content)) {
|
|
4275
4636
|
await redactContentBlocks(block.content, kind);
|
|
4276
4637
|
}
|
|
4277
4638
|
if (typeof block.output === "string") {
|
|
4278
|
-
|
|
4639
|
+
const blockKind = kind === "message" && isToolCatalogJson(block.output) ? "tool_description" : kind;
|
|
4640
|
+
block.output = await redactString(block.output, blockKind);
|
|
4279
4641
|
} else if (Array.isArray(block.output)) {
|
|
4280
4642
|
await redactContentBlocks(block.output, kind);
|
|
4281
4643
|
}
|
|
@@ -4414,15 +4776,16 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4414
4776
|
msg.name = await redactString(msg.name, "tool_description");
|
|
4415
4777
|
}
|
|
4416
4778
|
if (typeof msg.content === "string") {
|
|
4417
|
-
msg.
|
|
4779
|
+
const contentKind = msg.role === "tool" && isToolCatalogJson(msg.content) ? "tool_description" : msgKind;
|
|
4780
|
+
msg.content = await redactString(msg.content, contentKind);
|
|
4418
4781
|
} else if (Array.isArray(msg.content)) {
|
|
4419
4782
|
await redactContentBlocks(msg.content, msgKind);
|
|
4420
4783
|
}
|
|
4421
4784
|
if (typeof msg.reasoning_content === "string") {
|
|
4422
|
-
msg.reasoning_content = await redactString(msg.reasoning_content,
|
|
4785
|
+
msg.reasoning_content = await redactString(msg.reasoning_content, "reasoning");
|
|
4423
4786
|
}
|
|
4424
4787
|
if (typeof msg.reasoning === "string") {
|
|
4425
|
-
msg.reasoning = await redactString(msg.reasoning,
|
|
4788
|
+
msg.reasoning = await redactString(msg.reasoning, "reasoning");
|
|
4426
4789
|
}
|
|
4427
4790
|
if (typeof msg.refusal === "string") {
|
|
4428
4791
|
msg.refusal = await redactString(msg.refusal, msgKind);
|
|
@@ -4463,7 +4826,8 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4463
4826
|
await redactContentBlocks(item.content, itemKind);
|
|
4464
4827
|
}
|
|
4465
4828
|
if (typeof item.output === "string") {
|
|
4466
|
-
item.
|
|
4829
|
+
const outKind = item.type === "function_call_output" && isToolCatalogJson(item.output) ? "tool_description" : itemKind;
|
|
4830
|
+
item.output = await redactString(item.output, outKind);
|
|
4467
4831
|
} else if (Array.isArray(item.output)) {
|
|
4468
4832
|
await redactContentBlocks(item.output, itemKind);
|
|
4469
4833
|
}
|
|
@@ -4473,10 +4837,10 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4473
4837
|
item.input = await redactObjectStrings(item.input, itemKind);
|
|
4474
4838
|
}
|
|
4475
4839
|
if (typeof item.reasoning_content === "string") {
|
|
4476
|
-
item.reasoning_content = await redactString(item.reasoning_content,
|
|
4840
|
+
item.reasoning_content = await redactString(item.reasoning_content, "reasoning");
|
|
4477
4841
|
}
|
|
4478
4842
|
if (typeof item.reasoning === "string") {
|
|
4479
|
-
item.reasoning = await redactString(item.reasoning,
|
|
4843
|
+
item.reasoning = await redactString(item.reasoning, "reasoning");
|
|
4480
4844
|
}
|
|
4481
4845
|
if (typeof item.refusal === "string") {
|
|
4482
4846
|
item.refusal = await redactString(item.refusal, itemKind);
|
|
@@ -4492,9 +4856,18 @@ async function redactLlmRequestBody(body, redactString, options = {}) {
|
|
|
4492
4856
|
}
|
|
4493
4857
|
return clone;
|
|
4494
4858
|
}
|
|
4859
|
+
function filterMapExcluding(map, excluded) {
|
|
4860
|
+
if (!excluded || excluded.size === 0) return map;
|
|
4861
|
+
const out = {};
|
|
4862
|
+
for (const [token, value] of Object.entries(map)) {
|
|
4863
|
+
if (!excluded.has(token)) out[token] = value;
|
|
4864
|
+
}
|
|
4865
|
+
return out;
|
|
4866
|
+
}
|
|
4495
4867
|
function restoreLlmResponseBody(body, map, options = {}) {
|
|
4496
4868
|
if (!isPlainObject(body) || Object.keys(map).length === 0) return body;
|
|
4497
4869
|
const clone = options.mutate ? body : structuredClone(body);
|
|
4870
|
+
const reasoningMap = filterMapExcluding(map, options.reasoningExcludedTokens);
|
|
4498
4871
|
if (Array.isArray(clone.content)) {
|
|
4499
4872
|
for (const block of clone.content) {
|
|
4500
4873
|
if (!isPlainObject(block)) continue;
|
|
@@ -4502,7 +4875,7 @@ function restoreLlmResponseBody(body, map, options = {}) {
|
|
|
4502
4875
|
block.text = restorePlainText(block.text, map);
|
|
4503
4876
|
}
|
|
4504
4877
|
if (typeof block.thinking === "string" && typeof block.signature !== "string") {
|
|
4505
|
-
block.thinking = restorePlainText(block.thinking,
|
|
4878
|
+
block.thinking = restorePlainText(block.thinking, reasoningMap);
|
|
4506
4879
|
}
|
|
4507
4880
|
if (isPlainObject(block.input)) {
|
|
4508
4881
|
const skipRestore = isMaskeraVaultToolName(block.name) || block.name === "execute" && typeof block.input.code === "string" && MASKERA_VAULT_CODE_RE.test(block.input.code);
|
|
@@ -4521,10 +4894,10 @@ function restoreLlmResponseBody(body, map, options = {}) {
|
|
|
4521
4894
|
msg.content = restorePlainText(msg.content, map);
|
|
4522
4895
|
}
|
|
4523
4896
|
if (typeof msg.reasoning_content === "string") {
|
|
4524
|
-
msg.reasoning_content = restorePlainText(msg.reasoning_content,
|
|
4897
|
+
msg.reasoning_content = restorePlainText(msg.reasoning_content, reasoningMap);
|
|
4525
4898
|
}
|
|
4526
4899
|
if (typeof msg.reasoning === "string") {
|
|
4527
|
-
msg.reasoning = restorePlainText(msg.reasoning,
|
|
4900
|
+
msg.reasoning = restorePlainText(msg.reasoning, reasoningMap);
|
|
4528
4901
|
}
|
|
4529
4902
|
if (Array.isArray(msg.tool_calls)) {
|
|
4530
4903
|
for (const call of msg.tool_calls) {
|
|
@@ -4573,14 +4946,15 @@ function restoreLlmResponseBody(body, map, options = {}) {
|
|
|
4573
4946
|
}
|
|
4574
4947
|
return clone;
|
|
4575
4948
|
}
|
|
4576
|
-
function createSseRestorer(map) {
|
|
4949
|
+
function createSseRestorer(map, options = {}) {
|
|
4950
|
+
const reasoningMap = filterMapExcluding(map, options.reasoningExcludedTokens);
|
|
4577
4951
|
const streams = /* @__PURE__ */ new Map();
|
|
4578
4952
|
const toolNames = /* @__PURE__ */ new Map();
|
|
4579
4953
|
const bufferedToolArgs = /* @__PURE__ */ new Map();
|
|
4580
|
-
const getStream = (key, escapeForJsonString = false) => {
|
|
4954
|
+
const getStream = (key, escapeForJsonString = false, useReasoningMap = false) => {
|
|
4581
4955
|
let r = streams.get(key);
|
|
4582
4956
|
if (!r) {
|
|
4583
|
-
r = new StreamingRestorer(map, { escapeForJsonString });
|
|
4957
|
+
r = new StreamingRestorer(useReasoningMap ? reasoningMap : map, { escapeForJsonString });
|
|
4584
4958
|
streams.set(key, r);
|
|
4585
4959
|
}
|
|
4586
4960
|
return r;
|
|
@@ -4778,7 +5152,9 @@ data: ${JSON.stringify({
|
|
|
4778
5152
|
if (parsed.delta.type === "text_delta" && typeof parsed.delta.text === "string") {
|
|
4779
5153
|
parsed.delta.text = getStream(`anth_text:${idx}`).push(parsed.delta.text);
|
|
4780
5154
|
} else if (parsed.delta.type === "thinking_delta" && typeof parsed.delta.thinking === "string") {
|
|
4781
|
-
parsed.delta.thinking = getStream(`anth_thinking:${idx}
|
|
5155
|
+
parsed.delta.thinking = getStream(`anth_thinking:${idx}`, false, true).push(
|
|
5156
|
+
parsed.delta.thinking
|
|
5157
|
+
);
|
|
4782
5158
|
} else if (parsed.delta.type === "input_json_delta" && typeof parsed.delta.partial_json === "string") {
|
|
4783
5159
|
const key = `anth_json:${idx}`;
|
|
4784
5160
|
const name = toolNames.get(key);
|
|
@@ -4842,12 +5218,14 @@ data: ${JSON.stringify({
|
|
|
4842
5218
|
delta.content = getStream(`openai_chat_text:${cIdx}`).push(delta.content);
|
|
4843
5219
|
}
|
|
4844
5220
|
if (typeof delta.reasoning_content === "string") {
|
|
4845
|
-
delta.reasoning_content = getStream(
|
|
4846
|
-
|
|
4847
|
-
|
|
5221
|
+
delta.reasoning_content = getStream(
|
|
5222
|
+
`openai_chat_reasoning:${cIdx}`,
|
|
5223
|
+
false,
|
|
5224
|
+
true
|
|
5225
|
+
).push(delta.reasoning_content);
|
|
4848
5226
|
}
|
|
4849
5227
|
if (typeof delta.reasoning === "string") {
|
|
4850
|
-
delta.reasoning = getStream(`openai_chat_reasoning_plain:${cIdx}
|
|
5228
|
+
delta.reasoning = getStream(`openai_chat_reasoning_plain:${cIdx}`, false, true).push(
|
|
4851
5229
|
delta.reasoning
|
|
4852
5230
|
);
|
|
4853
5231
|
}
|
|
@@ -4876,7 +5254,7 @@ data: ${JSON.stringify({
|
|
|
4876
5254
|
}
|
|
4877
5255
|
}
|
|
4878
5256
|
}
|
|
4879
|
-
if (choice.finish_reason
|
|
5257
|
+
if (typeof choice.finish_reason === "string" && choice.finish_reason.length > 0) {
|
|
4880
5258
|
const textTail = flushStream(`openai_chat_text:${cIdx}`);
|
|
4881
5259
|
if (textTail) {
|
|
4882
5260
|
if (!isPlainObject(choice.delta)) choice.delta = {};
|
|
@@ -5337,6 +5715,8 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5337
5715
|
};
|
|
5338
5716
|
const mcpSessions = /* @__PURE__ */ new Map();
|
|
5339
5717
|
const closedMcpSessions = /* @__PURE__ */ new Set();
|
|
5718
|
+
const messageTokensBySession = /* @__PURE__ */ new Map();
|
|
5719
|
+
const externalToolTokensBySession = /* @__PURE__ */ new Map();
|
|
5340
5720
|
if (auditLogPath) {
|
|
5341
5721
|
await mkdir(dirname(auditLogPath), { recursive: true });
|
|
5342
5722
|
await appendFile(auditLogPath, "", { encoding: "utf8", mode: 384 });
|
|
@@ -5518,6 +5898,8 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5518
5898
|
approvals.clearAll();
|
|
5519
5899
|
engine.clearCache();
|
|
5520
5900
|
inspector.clear();
|
|
5901
|
+
messageTokensBySession.clear();
|
|
5902
|
+
externalToolTokensBySession.clear();
|
|
5521
5903
|
totalRequests = 0;
|
|
5522
5904
|
totalRedactions = 0;
|
|
5523
5905
|
totalCounts = {};
|
|
@@ -5637,6 +6019,11 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5637
6019
|
res.end(JSON.stringify({ error: "Unknown inspector route." }));
|
|
5638
6020
|
return;
|
|
5639
6021
|
}
|
|
6022
|
+
if ((method === "GET" || method === "HEAD") && BROWSER_PROBE_PATHS.has(normalizedPath)) {
|
|
6023
|
+
res.writeHead(204, INSPECTOR_SECURITY_HEADERS);
|
|
6024
|
+
res.end();
|
|
6025
|
+
return;
|
|
6026
|
+
}
|
|
5640
6027
|
if (normalizedPath === "/mcp" || normalizedPath === "/mcp/") {
|
|
5641
6028
|
if (!isLoopbackRemoteAddress(req.socket.remoteAddress) || !isLoopbackHost(req.headers.host, host, boundPort, true)) {
|
|
5642
6029
|
res.writeHead(403, {
|
|
@@ -5866,9 +6253,22 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5866
6253
|
return;
|
|
5867
6254
|
}
|
|
5868
6255
|
sessionId = deriveProxySessionId(req.headers, parsed);
|
|
6256
|
+
let externalTokensForSession = externalToolTokensBySession.get(sessionId);
|
|
6257
|
+
const incomingToolTokens = /* @__PURE__ */ new Set();
|
|
6258
|
+
collectExternalToolResultPlaceholders(parsed, incomingToolTokens);
|
|
6259
|
+
if (incomingToolTokens.size > 0) {
|
|
6260
|
+
if (!externalTokensForSession) {
|
|
6261
|
+
externalTokensForSession = /* @__PURE__ */ new Set();
|
|
6262
|
+
externalToolTokensBySession.set(sessionId, externalTokensForSession);
|
|
6263
|
+
}
|
|
6264
|
+
for (const tok of incomingToolTokens) externalTokensForSession.add(tok);
|
|
6265
|
+
}
|
|
6266
|
+
let messageTokensForSession = messageTokensBySession.get(sessionId);
|
|
5869
6267
|
let redactedObj;
|
|
5870
6268
|
try {
|
|
5871
|
-
const
|
|
6269
|
+
const messagePreviews = [];
|
|
6270
|
+
const fallbackPreviews = [];
|
|
6271
|
+
const redactChunk = async (chunk, forceRules, previewable) => {
|
|
5872
6272
|
if (!chunk.trim()) return chunk;
|
|
5873
6273
|
const { result } = await engine.redact(chunk, {
|
|
5874
6274
|
mode: forceRules ? "rules" : void 0,
|
|
@@ -5878,9 +6278,20 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5878
6278
|
const applied = vault.apply(chunk, result, sessionId, proxyVaultOwner);
|
|
5879
6279
|
stats.redactions += applied.totalRedactions;
|
|
5880
6280
|
mergeCounts(stats.counts, applied.counts);
|
|
5881
|
-
if (
|
|
5882
|
-
|
|
5883
|
-
|
|
6281
|
+
if (previewable && applied.totalRedactions > 0) {
|
|
6282
|
+
if (!messageTokensForSession) {
|
|
6283
|
+
messageTokensForSession = /* @__PURE__ */ new Set();
|
|
6284
|
+
messageTokensBySession.set(sessionId, messageTokensForSession);
|
|
6285
|
+
}
|
|
6286
|
+
for (const tok of Object.keys(applied.map)) {
|
|
6287
|
+
messageTokensForSession.add(tok);
|
|
6288
|
+
}
|
|
6289
|
+
}
|
|
6290
|
+
const snippet = applied.redacted.length > 1200 ? `${applied.redacted.slice(0, 1200)}...` : applied.redacted;
|
|
6291
|
+
if (previewable) {
|
|
6292
|
+
if (messagePreviews.length < 6) messagePreviews.push(snippet);
|
|
6293
|
+
} else if (applied.totalRedactions > 0 && fallbackPreviews.length < 4) {
|
|
6294
|
+
fallbackPreviews.push(snippet);
|
|
5884
6295
|
}
|
|
5885
6296
|
return applied.redacted;
|
|
5886
6297
|
};
|
|
@@ -5889,21 +6300,12 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5889
6300
|
async (text, kind) => {
|
|
5890
6301
|
if (!text.trim()) return text;
|
|
5891
6302
|
if (kind === "tool_description") {
|
|
5892
|
-
return redactChunk(text, true);
|
|
6303
|
+
return redactChunk(text, true, false);
|
|
5893
6304
|
}
|
|
5894
|
-
if (kind === "system") {
|
|
5895
|
-
|
|
5896
|
-
const parts = text.split(/(\n{2,})/);
|
|
5897
|
-
for (let i = 0; i < parts.length; i += 2) {
|
|
5898
|
-
const para = parts[i] ?? "";
|
|
5899
|
-
if (!para.trim()) continue;
|
|
5900
|
-
parts[i] = await redactChunk(para, isLikelyEnglishSystemPrompt(para));
|
|
5901
|
-
}
|
|
5902
|
-
return parts.join("");
|
|
5903
|
-
}
|
|
5904
|
-
return redactChunk(text, isLikelyEnglishSystemPrompt(text));
|
|
6305
|
+
if (kind === "system" || kind === "reasoning") {
|
|
6306
|
+
return redactSystemText(text, redactChunk);
|
|
5905
6307
|
}
|
|
5906
|
-
return
|
|
6308
|
+
return redactMessageText(text, redactChunk);
|
|
5907
6309
|
},
|
|
5908
6310
|
{
|
|
5909
6311
|
mutate: true,
|
|
@@ -5912,6 +6314,7 @@ async function startMaskeraProxy(options = {}) {
|
|
|
5912
6314
|
}
|
|
5913
6315
|
}
|
|
5914
6316
|
);
|
|
6317
|
+
redactedPreview.push(...messagePreviews.length > 0 ? messagePreviews : fallbackPreviews);
|
|
5915
6318
|
} catch (err) {
|
|
5916
6319
|
recordAuditAndInspector({
|
|
5917
6320
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -6023,13 +6426,23 @@ async function startMaskeraProxy(options = {}) {
|
|
|
6023
6426
|
if (requestWarnings) {
|
|
6024
6427
|
outHeaders["x-maskera-warning"] = "uninspectable-binary-attachment";
|
|
6025
6428
|
}
|
|
6026
|
-
const
|
|
6429
|
+
const rawSessionMap = restoreResponses ? vault.getMap(sessionId, proxyVaultOwner) : {};
|
|
6430
|
+
const externalTokens = externalToolTokensBySession.get(sessionId);
|
|
6431
|
+
const messageTokens = messageTokensBySession.get(sessionId);
|
|
6432
|
+
let sessionMap = rawSessionMap;
|
|
6433
|
+
if (externalTokens && externalTokens.size > 0) {
|
|
6434
|
+
sessionMap = {};
|
|
6435
|
+
for (const [token, value] of Object.entries(rawSessionMap)) {
|
|
6436
|
+
if (externalTokens.has(token) && !messageTokens?.has(token)) continue;
|
|
6437
|
+
sessionMap[token] = value;
|
|
6438
|
+
}
|
|
6439
|
+
}
|
|
6027
6440
|
const hasMap = Object.keys(sessionMap).length > 0;
|
|
6028
6441
|
const resContentType = (upstreamRes.headers.get("content-type") ?? "").toLowerCase();
|
|
6029
6442
|
const isSse = resContentType.includes("text/event-stream");
|
|
6030
6443
|
if (isSse && upstreamRes.body) {
|
|
6031
6444
|
res.writeHead(upstreamRes.status, outHeaders);
|
|
6032
|
-
const sse = hasMap ? createSseRestorer(sessionMap) : null;
|
|
6445
|
+
const sse = hasMap ? createSseRestorer(sessionMap, { reasoningExcludedTokens: externalTokens }) : null;
|
|
6033
6446
|
const decoder = sse ? new TextDecoder() : null;
|
|
6034
6447
|
const reader = upstreamRes.body.getReader();
|
|
6035
6448
|
const onStreamAbort = () => {
|
|
@@ -6088,10 +6501,14 @@ async function startMaskeraProxy(options = {}) {
|
|
|
6088
6501
|
} finally {
|
|
6089
6502
|
res.off("close", onClientClose);
|
|
6090
6503
|
}
|
|
6504
|
+
const errorDetail = await extractSafeUpstreamError(resBuf, upstreamRes.status, engine);
|
|
6091
6505
|
if (hasMap && resContentType.includes("application/json")) {
|
|
6092
6506
|
try {
|
|
6093
6507
|
const parsed = JSON.parse(resBuf.toString("utf8"));
|
|
6094
|
-
const restored = restoreLlmResponseBody(parsed, sessionMap, {
|
|
6508
|
+
const restored = restoreLlmResponseBody(parsed, sessionMap, {
|
|
6509
|
+
mutate: true,
|
|
6510
|
+
reasoningExcludedTokens: externalTokens
|
|
6511
|
+
});
|
|
6095
6512
|
const restoredBuf = Buffer.from(JSON.stringify(restored), "utf8");
|
|
6096
6513
|
res.writeHead(upstreamRes.status, outHeaders);
|
|
6097
6514
|
res.end(restoredBuf);
|
|
@@ -6108,6 +6525,7 @@ async function startMaskeraProxy(options = {}) {
|
|
|
6108
6525
|
redactions: stats.redactions,
|
|
6109
6526
|
counts: stats.counts,
|
|
6110
6527
|
redactedPreview,
|
|
6528
|
+
...errorDetail ? { errorDetail } : {},
|
|
6111
6529
|
...requestWarnings ? { warnings: requestWarnings } : {}
|
|
6112
6530
|
});
|
|
6113
6531
|
log(
|
|
@@ -6132,6 +6550,7 @@ async function startMaskeraProxy(options = {}) {
|
|
|
6132
6550
|
redactions: stats.redactions,
|
|
6133
6551
|
counts: stats.counts,
|
|
6134
6552
|
redactedPreview,
|
|
6553
|
+
...errorDetail ? { errorDetail } : {},
|
|
6135
6554
|
...requestWarnings ? { warnings: requestWarnings } : {}
|
|
6136
6555
|
});
|
|
6137
6556
|
log(
|
|
@@ -6889,12 +7308,12 @@ function printHelp() {
|
|
|
6889
7308
|
process.stdout.write(
|
|
6890
7309
|
[
|
|
6891
7310
|
`${SERVER_NAME} ${SERVER_VERSION}`,
|
|
6892
|
-
"Local Swedish PII redaction for
|
|
7311
|
+
"Local Swedish PII redaction for AI agents.",
|
|
6893
7312
|
"",
|
|
6894
7313
|
"Usage:",
|
|
6895
7314
|
" maskera-mcp [mcp] [--root <dir>] [--no-raw-restore] [--read-only] [--approval-mode automatic|native] [--rules-only]",
|
|
6896
7315
|
" maskera-mcp proxy [--port 8787] [--host 127.0.0.1] [--root <dir>] [--open] [--upstream <url>] [--audit-log <file.jsonl>] [--no-raw-restore] [--read-only] [--approval-mode automatic|browser] [--rules-only] [--no-restore] [--block-binary]",
|
|
6897
|
-
" maskera-mcp wrap [flags] -- <cmd> [args...]
|
|
7316
|
+
" maskera-mcp wrap [flags] -- <cmd> [args...]",
|
|
6898
7317
|
" maskera-mcp guard [--redact] [--file <path>] [--proxy-url <url>] [--no-proxy] [--rules-only]",
|
|
6899
7318
|
` maskera-mcp install [client|all] [--transport stdio|http] [--url <http://127.0.0.1:8787/mcp>] [--root <dir>] [--apply] [--project] [--with-hook] [--no-raw-restore] [--read-only] [--config <path>] [--receipt <file.json>] [--no-verify] [--dry-run]`,
|
|
6900
7319
|
" maskera-mcp doctor [--root <dir>] [--proxy-url <url>] [--receipt <file.json>] [--rules-only] [--json]",
|
|
@@ -7301,11 +7720,10 @@ Verify: ${primary.verifyStep}
|
|
|
7301
7720
|
process.stderr.write(
|
|
7302
7721
|
[
|
|
7303
7722
|
`[maskera-proxy] listening on ${running.url} (mode=${args.defaultMode}, restore=${args.restoreResponses})`,
|
|
7304
|
-
`
|
|
7723
|
+
` Inspector: ${running.inspectorUrl}`,
|
|
7305
7724
|
` Streamable HTTP MCP: ${running.mcpUrl}`,
|
|
7306
7725
|
` Claude Code / Anthropic: ANTHROPIC_BASE_URL=${running.url}`,
|
|
7307
7726
|
` Codex / Cursor / OpenAI: OPENAI_BASE_URL=${running.url}/v1`,
|
|
7308
|
-
` Or one-command launch: npx -y @maskera/mcp wrap -- claude`,
|
|
7309
7727
|
...args.auditLogPath ? [` Local audit log (JSONL): ${args.auditLogPath}`] : [],
|
|
7310
7728
|
""
|
|
7311
7729
|
].join("\n")
|