@chatpanel/pii 0.2.2 → 0.2.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/index.js +1 -0
- package/package.json +4 -2
- package/tool-harness.js +103 -0
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chatpanel/pii",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "The canonical ChatPanel privacy engine — reversible PII redaction + pseudonymization with local entity detection. Pure, dependency-free ESM shared by the ChatPanel extension, gateway, and bridge.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"./pii-redact.js": "./pii-redact.js",
|
|
10
10
|
"./pii-detect.js": "./pii-detect.js",
|
|
11
11
|
"./pipeline.js": "./pipeline.js",
|
|
12
|
-
"./tool-rank.js": "./tool-rank.js"
|
|
12
|
+
"./tool-rank.js": "./tool-rank.js",
|
|
13
|
+
"./tool-harness.js": "./tool-harness.js"
|
|
13
14
|
},
|
|
14
15
|
"files": [
|
|
15
16
|
"index.js",
|
|
@@ -17,6 +18,7 @@
|
|
|
17
18
|
"pii-detect.js",
|
|
18
19
|
"pipeline.js",
|
|
19
20
|
"tool-rank.js",
|
|
21
|
+
"tool-harness.js",
|
|
20
22
|
"LICENSE",
|
|
21
23
|
"README.md"
|
|
22
24
|
],
|
package/tool-harness.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// THE tool harness — one interception layer shared by every orchestrator
|
|
2
|
+
// (ChatPanel API + agent, gateway API + relay) so tool handling can't drift. It
|
|
3
|
+
// owns the boundaries around a model turn:
|
|
4
|
+
//
|
|
5
|
+
// ⓪ selectTools — inject + narrow the tools the model is offered (MCP-auto).
|
|
6
|
+
// ② toTool — what a tool RECEIVES: real values (so on-device / remote
|
|
7
|
+
// lookups work), or the redacted token for remote MCP tools
|
|
8
|
+
// when the user chose "redact remote".
|
|
9
|
+
// ③ toModelResult— what the MODEL sees back: the tool result re-redacted so it
|
|
10
|
+
// stays blinded.
|
|
11
|
+
// ④ toUser — the final reply: reversible tokens restored (pseudonyms stay).
|
|
12
|
+
//
|
|
13
|
+
// PRIVACY IS OPTIONAL. With no `vault` (redaction off), ②③④ are pass-throughs —
|
|
14
|
+
// no latency, no placeholder confusion — but ⓪ selectTools STILL narrows, because
|
|
15
|
+
// not every turn is privacy-sensitive yet every turn benefits from fewer tools.
|
|
16
|
+
//
|
|
17
|
+
// Self-contained on the SYNCED engine files (pii-redact.js, tool-rank.js), so the
|
|
18
|
+
// extension (browser ESM) and the gateway (npm) run the exact same code. The caller
|
|
19
|
+
// passes the already-gated `redactOpts` ({tier, entities, dictionary}) it computed
|
|
20
|
+
// from cfg+isPro — keeping tier/dictionary Pro-gating out of the harness.
|
|
21
|
+
|
|
22
|
+
import { restoreText, restoreWithAliases, redactText } from './pii-redact.js';
|
|
23
|
+
import { narrowSpecs } from './tool-rank.js';
|
|
24
|
+
|
|
25
|
+
// MCP / remote tools are server-prefixed (mcp_server__tool). Local tools
|
|
26
|
+
// (history/meeting/page, or a client's core bash/read) are not — they always get
|
|
27
|
+
// real values and are never narrowed away.
|
|
28
|
+
export const isRemoteToolName = (name) => /^mcp[_-]/i.test(String(name || ''));
|
|
29
|
+
|
|
30
|
+
// Deep restore of a tool-call argument value, undoing reversible tokens AND
|
|
31
|
+
// pseudonyms (tools run locally / on real data; only the model stays blinded).
|
|
32
|
+
export function restoreToolArgs(value, vault) {
|
|
33
|
+
if (!vault) return value;
|
|
34
|
+
if (typeof value === 'string') return restoreWithAliases(value, vault);
|
|
35
|
+
if (Array.isArray(value)) return value.map((v) => restoreToolArgs(v, vault));
|
|
36
|
+
if (value && typeof value === 'object') {
|
|
37
|
+
const out = {};
|
|
38
|
+
for (const k of Object.keys(value)) out[k] = restoreToolArgs(value[k], vault);
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// System-prompt note that tells the model how to behave around placeholders when
|
|
45
|
+
// tools are armed. WITHOUT this, privacy-aware models (Codex, Claude) recognize a
|
|
46
|
+
// [[LOCATION_1]] token as redacted and REFUSE to use it for a lookup ("I can't see
|
|
47
|
+
// your real city") — the opposite of what we want. Weak models call the tool blindly
|
|
48
|
+
// and it works (the harness restores the real value), so the note levels them up.
|
|
49
|
+
export function placeholderToolNote({ toolData = 'real' } = {}) {
|
|
50
|
+
const intro =
|
|
51
|
+
'PRIVACY PLACEHOLDERS: some values in this conversation are tokens like [[PERSON_1]], '
|
|
52
|
+
+ '[[LOCATION_1]], [[ORG_1]] that stand in for the user\'s real private data. ';
|
|
53
|
+
const remote = toolData === 'redactRemote'
|
|
54
|
+
? 'When you call a LOCAL tool the placeholder is automatically replaced with the real '
|
|
55
|
+
+ 'value before the tool runs; REMOTE (MCP) tools deliberately receive the placeholder '
|
|
56
|
+
+ 'to keep private data off third-party servers. '
|
|
57
|
+
: 'When you call ANY tool, these placeholders are AUTOMATICALLY replaced with the real '
|
|
58
|
+
+ 'values before the tool executes — the tool receives the TRUE value and returns correct '
|
|
59
|
+
+ 'results. ';
|
|
60
|
+
const rules =
|
|
61
|
+
'So: CALL THE TOOL using the placeholder exactly as written, as if it were the real value. '
|
|
62
|
+
+ 'Do NOT say you cannot see the real value, do NOT ask the user to re-type it, and do NOT '
|
|
63
|
+
+ 'refuse on privacy grounds — the lookup will work. The real values are restored in your '
|
|
64
|
+
+ 'final answer automatically, so write your answer using the placeholders too.';
|
|
65
|
+
return intro + remote + rules;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function makeToolHarness({ vault = null, toolData = 'real', redactOpts = null, redactResults = true } = {}) {
|
|
69
|
+
const on = !!vault; // privacy enabled for this turn?
|
|
70
|
+
const redactRemote = toolData === 'redactRemote';
|
|
71
|
+
return {
|
|
72
|
+
enabled: on,
|
|
73
|
+
isRemoteTool: isRemoteToolName,
|
|
74
|
+
|
|
75
|
+
// ⓪ Always-on tool selection (privacy-independent). `available` is any spec
|
|
76
|
+
// list; `opts` forwards { cap, keep, name, description } to the shared ranker.
|
|
77
|
+
selectTools(available, query, opts = {}) {
|
|
78
|
+
return narrowSpecs(available, query, opts);
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
// ② What the tool receives.
|
|
82
|
+
toTool(name, args) {
|
|
83
|
+
if (!on) return args; // privacy off → already real
|
|
84
|
+
if (redactRemote && isRemoteToolName(name)) return args; // keep PII off remote MCP
|
|
85
|
+
return restoreToolArgs(args, vault); // real values for the tool
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
// ③ What the model sees back (re-redacted). Handles a string or a { text } shape.
|
|
89
|
+
toModelResult(name, raw) {
|
|
90
|
+
if (!on || !redactResults || !redactOpts) return raw;
|
|
91
|
+
if (typeof raw === 'string') return redactText(raw, vault, redactOpts);
|
|
92
|
+
if (raw && typeof raw === 'object' && typeof raw.text === 'string') {
|
|
93
|
+
return { ...raw, text: redactText(raw.text, vault, redactOpts) };
|
|
94
|
+
}
|
|
95
|
+
return raw;
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
// ④ The final reply the user sees.
|
|
99
|
+
toUser(text) {
|
|
100
|
+
return on ? restoreText(text, vault) : text;
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|