flare-redact 0.9.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/MIGRATION.md +58 -0
- package/README.md +88 -17
- package/SECURITY.md +6 -0
- package/dist/cli.js +1 -1
- package/dist/engine.d.ts +13 -2
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +17 -4
- package/dist/engine.js.map +1 -1
- package/dist/graph.d.ts +9 -0
- package/dist/graph.d.ts.map +1 -0
- package/dist/graph.js +94 -0
- package/dist/graph.js.map +1 -0
- package/dist/http.d.ts +5 -0
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +54 -1
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +9 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +222 -70
- package/dist/index.js.map +1 -1
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +92 -54
- package/dist/llm.js.map +1 -1
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +3 -29
- package/dist/session.js.map +1 -1
- package/dist/stream.d.ts +5 -1
- package/dist/stream.d.ts.map +1 -1
- package/dist/stream.js +68 -8
- package/dist/stream.js.map +1 -1
- package/dist/tool.d.ts +30 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +37 -0
- package/dist/tool.js.map +1 -0
- package/dist/vault.d.ts +9 -0
- package/dist/vault.d.ts.map +1 -1
- package/dist/vault.js +47 -29
- package/dist/vault.js.map +1 -1
- package/package.json +6 -1
package/dist/llm.js
CHANGED
|
@@ -1,35 +1,8 @@
|
|
|
1
|
-
import { createVault,
|
|
1
|
+
import { createVault, buildStreamRestore } from './vault.js';
|
|
2
2
|
export { createVault } from './vault.js';
|
|
3
3
|
const WRAPPED = Symbol.for('flare-redact.wrapped');
|
|
4
|
-
function redactContent(content, vault) {
|
|
5
|
-
if (typeof content === 'string')
|
|
6
|
-
return vault.redact(content);
|
|
7
|
-
if (Array.isArray(content)) {
|
|
8
|
-
return content.map((part) => part && typeof part === 'object' && typeof part.text === 'string'
|
|
9
|
-
? { ...part, text: vault.redact(part.text) }
|
|
10
|
-
: part);
|
|
11
|
-
}
|
|
12
|
-
return content;
|
|
13
|
-
}
|
|
14
4
|
function makeStreamRestorer(vault) {
|
|
15
|
-
|
|
16
|
-
let buf = '';
|
|
17
|
-
return {
|
|
18
|
-
push(chunk) {
|
|
19
|
-
buf += chunk;
|
|
20
|
-
// Hold back a trailing "[…" that could still be an unfinished placeholder.
|
|
21
|
-
const open = buf.lastIndexOf('[');
|
|
22
|
-
const emitEnd = open !== -1 && buf.indexOf(']', open) === -1 ? open : buf.length;
|
|
23
|
-
const emit = buf.slice(0, emitEnd);
|
|
24
|
-
buf = buf.slice(emitEnd);
|
|
25
|
-
return restore(emit);
|
|
26
|
-
},
|
|
27
|
-
flush() {
|
|
28
|
-
const out = restore(buf);
|
|
29
|
-
buf = '';
|
|
30
|
-
return out;
|
|
31
|
-
},
|
|
32
|
-
};
|
|
5
|
+
return buildStreamRestore(vault.entries());
|
|
33
6
|
}
|
|
34
7
|
/** Redact a prompt and hand back the vault so you can restore the reply yourself. */
|
|
35
8
|
export function redactPrompt(text, opts = {}) {
|
|
@@ -54,9 +27,7 @@ export function wrapOpenAI(client, opts = {}) {
|
|
|
54
27
|
const original = completions.create.bind(completions);
|
|
55
28
|
completions.create = (params, ...rest) => {
|
|
56
29
|
const vault = createVault(opts);
|
|
57
|
-
const messages = Array.isArray(params?.messages)
|
|
58
|
-
? params.messages.map((m) => ({ ...m, content: redactContent(m.content, vault) }))
|
|
59
|
-
: params?.messages;
|
|
30
|
+
const messages = Array.isArray(params?.messages) ? vault.redact(params.messages) : params?.messages;
|
|
60
31
|
const result = original({ ...params, messages }, ...rest);
|
|
61
32
|
if (params?.stream)
|
|
62
33
|
return wrapOpenAIStream(result, vault);
|
|
@@ -67,26 +38,66 @@ export function wrapOpenAI(client, opts = {}) {
|
|
|
67
38
|
}
|
|
68
39
|
async function* wrapOpenAIStream(result, vault) {
|
|
69
40
|
const stream = (await result);
|
|
70
|
-
const
|
|
41
|
+
const restorers = new Map();
|
|
42
|
+
const getRestorer = (key) => {
|
|
43
|
+
let restorer = restorers.get(key);
|
|
44
|
+
if (!restorer) {
|
|
45
|
+
restorer = makeStreamRestorer(vault);
|
|
46
|
+
restorers.set(key, restorer);
|
|
47
|
+
}
|
|
48
|
+
return restorer;
|
|
49
|
+
};
|
|
71
50
|
let last;
|
|
72
51
|
for await (const chunk of stream) {
|
|
73
52
|
last = chunk;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
53
|
+
yield {
|
|
54
|
+
...chunk,
|
|
55
|
+
choices: chunk.choices.map((choice, choicePosition) => {
|
|
56
|
+
const choiceIndex = choice.index ?? choicePosition;
|
|
57
|
+
const delta = choice.delta;
|
|
58
|
+
if (!delta)
|
|
59
|
+
return choice;
|
|
60
|
+
let nextDelta = delta;
|
|
61
|
+
if (typeof delta.content === 'string') {
|
|
62
|
+
nextDelta = {
|
|
63
|
+
...nextDelta,
|
|
64
|
+
content: getRestorer(`choice:${choiceIndex}:content`).push(delta.content),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
68
|
+
nextDelta = {
|
|
69
|
+
...nextDelta,
|
|
70
|
+
tool_calls: delta.tool_calls.map((call, callPosition) => {
|
|
71
|
+
const args = call.function?.arguments;
|
|
72
|
+
if (typeof args !== 'string')
|
|
73
|
+
return call;
|
|
74
|
+
const callIndex = call.index ?? callPosition;
|
|
75
|
+
return {
|
|
76
|
+
...call,
|
|
77
|
+
function: {
|
|
78
|
+
...call.function,
|
|
79
|
+
arguments: getRestorer(`choice:${choiceIndex}:tool:${callIndex}`).push(args),
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return { ...choice, delta: nextDelta };
|
|
86
|
+
}),
|
|
87
|
+
};
|
|
85
88
|
}
|
|
86
|
-
const
|
|
87
|
-
|
|
89
|
+
for (const [key, restorer] of restorers) {
|
|
90
|
+
const tail = restorer.flush();
|
|
91
|
+
if (!tail)
|
|
92
|
+
continue;
|
|
88
93
|
const base = last ? { id: last.id, object: last.object, model: last.model } : {};
|
|
89
|
-
|
|
94
|
+
const tool = /^choice:(\d+):tool:(\d+)$/.exec(key);
|
|
95
|
+
const content = /^choice:(\d+):content$/.exec(key);
|
|
96
|
+
const choiceIndex = Number(tool?.[1] ?? content?.[1] ?? 0);
|
|
97
|
+
const delta = tool
|
|
98
|
+
? { tool_calls: [{ index: Number(tool[2]), function: { arguments: tail } }] }
|
|
99
|
+
: { content: tail };
|
|
100
|
+
yield { ...base, choices: [{ index: choiceIndex, delta, finish_reason: null }] };
|
|
90
101
|
}
|
|
91
102
|
}
|
|
92
103
|
/**
|
|
@@ -109,11 +120,11 @@ export function wrapAnthropic(client, opts = {}) {
|
|
|
109
120
|
const redacted = {
|
|
110
121
|
...params,
|
|
111
122
|
messages: Array.isArray(params?.messages)
|
|
112
|
-
? params.messages
|
|
123
|
+
? vault.redact(params.messages)
|
|
113
124
|
: params?.messages,
|
|
114
125
|
};
|
|
115
126
|
if (params?.system !== undefined)
|
|
116
|
-
redacted.system =
|
|
127
|
+
redacted.system = vault.redact(params.system);
|
|
117
128
|
const result = original(redacted, ...rest);
|
|
118
129
|
if (params?.stream)
|
|
119
130
|
return wrapAnthropicStream(result, vault);
|
|
@@ -124,18 +135,45 @@ export function wrapAnthropic(client, opts = {}) {
|
|
|
124
135
|
}
|
|
125
136
|
async function* wrapAnthropicStream(result, vault) {
|
|
126
137
|
const stream = (await result);
|
|
127
|
-
const
|
|
138
|
+
const restorers = new Map();
|
|
139
|
+
const getRestorer = (key) => {
|
|
140
|
+
let restorer = restorers.get(key);
|
|
141
|
+
if (!restorer) {
|
|
142
|
+
restorer = makeStreamRestorer(vault);
|
|
143
|
+
restorers.set(key, restorer);
|
|
144
|
+
}
|
|
145
|
+
return restorer;
|
|
146
|
+
};
|
|
128
147
|
for await (const event of stream) {
|
|
129
148
|
const text = event?.delta?.text;
|
|
149
|
+
const partialJson = event?.delta?.partial_json;
|
|
150
|
+
const index = event.index ?? 0;
|
|
130
151
|
if (event?.type === 'content_block_delta' && typeof text === 'string') {
|
|
131
|
-
yield { ...event, delta: { ...event.delta, text:
|
|
152
|
+
yield { ...event, delta: { ...event.delta, text: getRestorer(`text:${index}`).push(text) } };
|
|
153
|
+
}
|
|
154
|
+
else if (event?.type === 'content_block_delta' && typeof partialJson === 'string') {
|
|
155
|
+
yield {
|
|
156
|
+
...event,
|
|
157
|
+
delta: {
|
|
158
|
+
...event.delta,
|
|
159
|
+
partial_json: getRestorer(`json:${index}`).push(partialJson),
|
|
160
|
+
},
|
|
161
|
+
};
|
|
132
162
|
}
|
|
133
163
|
else {
|
|
134
164
|
yield event;
|
|
135
165
|
}
|
|
136
166
|
}
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
167
|
+
for (const [key, restorer] of restorers) {
|
|
168
|
+
const tail = restorer.flush();
|
|
169
|
+
if (!tail)
|
|
170
|
+
continue;
|
|
171
|
+
const [kind, rawIndex] = key.split(':');
|
|
172
|
+
const index = Number(rawIndex);
|
|
173
|
+
const delta = kind === 'json'
|
|
174
|
+
? { type: 'input_json_delta', partial_json: tail }
|
|
175
|
+
: { type: 'text_delta', text: tail };
|
|
176
|
+
yield { type: 'content_block_delta', index, delta };
|
|
177
|
+
}
|
|
140
178
|
}
|
|
141
179
|
//# sourceMappingURL=llm.js.map
|
package/dist/llm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.js","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,
|
|
1
|
+
{"version":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AS,EAAE,CAAC;QACxC,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC;QAC9B,IAAI,CAAC,IAAI;YAAE,SAAS;QACpB,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACxC,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC/B,MAAM,KAAK,GAAG,IAAI,KAAK,MAAM;YAC3B,CAAC,CAAC,EAAE,IAAI,EAAE,kBAAkB,EAAE,YAAY,EAAE,IAAI,EAAE;YAClD,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;QACvC,MAAM,EAAE,IAAI,EAAE,qBAAqB,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;IACtD,CAAC;AACH,CAAC"}
|
package/dist/session.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,KAAK,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAE5F,MAAM,WAAW,cAAe,SAAQ,YAAY;CAAG;AAEvD,UAAU,WAAW;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,uEAAuE;IACvE,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,qDAAqD;IACrD,KAAK,IAAI,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,OAAO;IACtB,uDAAuD;IACvD,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IACvB,+DAA+D;IAC/D,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IACxB,iEAAiE;IACjE,cAAc,CAAC,CAAC,SAAS,WAAW,EAAE,QAAQ,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;IAC1D,qFAAqF;IACrF,MAAM,IAAI,cAAc,CAAC;IACzB,yDAAyD;IACzD,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,8DAA8D;IAC9D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0EAA0E;IAC1E,KAAK,IAAI,IAAI,CAAC;CACf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,IAAI,GAAE,cAAmB,GAAG,OAAO,CAoBhE"}
|
package/dist/session.js
CHANGED
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import { createVault,
|
|
2
|
-
function redactContent(content, vault) {
|
|
3
|
-
if (typeof content === 'string')
|
|
4
|
-
return vault.redact(content);
|
|
5
|
-
if (Array.isArray(content)) {
|
|
6
|
-
return content.map((part) => part && typeof part === 'object' && typeof part.text === 'string'
|
|
7
|
-
? { ...part, text: vault.redact(part.text) }
|
|
8
|
-
: part);
|
|
9
|
-
}
|
|
10
|
-
return content;
|
|
11
|
-
}
|
|
1
|
+
import { createVault, buildStreamRestore } from './vault.js';
|
|
12
2
|
/**
|
|
13
3
|
* A conversation-scoped redactor for chat/AI apps. One session keeps one vault,
|
|
14
4
|
* so the same value maps to the same placeholder across every turn — mask the
|
|
@@ -25,25 +15,9 @@ export function createSession(opts = {}) {
|
|
|
25
15
|
return {
|
|
26
16
|
redact: (input) => vault.redact(input),
|
|
27
17
|
restore: (input) => vault.restore(input),
|
|
28
|
-
redactMessages: (messages) =>
|
|
18
|
+
redactMessages: (messages) => vault.redact(messages),
|
|
29
19
|
stream() {
|
|
30
|
-
|
|
31
|
-
let buf = '';
|
|
32
|
-
return {
|
|
33
|
-
push(chunk) {
|
|
34
|
-
buf += chunk;
|
|
35
|
-
const open = buf.lastIndexOf('[');
|
|
36
|
-
const emitEnd = open !== -1 && buf.indexOf(']', open) === -1 ? open : buf.length;
|
|
37
|
-
const emit = buf.slice(0, emitEnd);
|
|
38
|
-
buf = buf.slice(emitEnd);
|
|
39
|
-
return restore(emit);
|
|
40
|
-
},
|
|
41
|
-
flush() {
|
|
42
|
-
const out = restore(buf);
|
|
43
|
-
buf = '';
|
|
44
|
-
return out;
|
|
45
|
-
},
|
|
46
|
-
};
|
|
20
|
+
return buildStreamRestore(vault.entries());
|
|
47
21
|
},
|
|
48
22
|
get vault() {
|
|
49
23
|
return vault;
|
package/dist/session.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAiC,MAAM,YAAY,CAAC;AAkC5F;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa,CAAC,OAAuB,EAAE;IACrD,IAAI,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAE9B,OAAO;QACL,MAAM,EAAE,CAAI,KAAQ,EAAK,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;QAC/C,OAAO,EAAE,CAAI,KAAQ,EAAK,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACjD,cAAc,EAAE,CAAwB,QAAa,EAAO,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC;QACrF,MAAM;YACJ,OAAO,kBAAkB,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QAC7C,CAAC;QACD,IAAI,KAAK;YACP,OAAO,KAAK,CAAC;QACf,CAAC;QACD,IAAI,IAAI;YACN,OAAO,KAAK,CAAC,IAAI,CAAC;QACpB,CAAC;QACD,KAAK;YACH,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;QAC5B,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/stream.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { Transform } from 'node:stream';
|
|
2
2
|
import { type RedactOptions } from './index.js';
|
|
3
|
-
export
|
|
3
|
+
export interface RedactStreamOptions extends RedactOptions {
|
|
4
|
+
/** Maximum buffered multiline record size before the stream fails closed. */
|
|
5
|
+
maxRecordLength?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function redactStream(opts?: RedactStreamOptions): Transform;
|
|
4
8
|
//# sourceMappingURL=stream.d.ts.map
|
package/dist/stream.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAA+B,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAE7E,MAAM,WAAW,mBAAoB,SAAQ,aAAa;IACxD,6EAA6E;IAC7E,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAKD,wBAAgB,YAAY,CAAC,IAAI,GAAE,mBAAwB,GAAG,SAAS,CAyEtE"}
|
package/dist/stream.js
CHANGED
|
@@ -1,19 +1,79 @@
|
|
|
1
1
|
import { Transform } from 'node:stream';
|
|
2
|
-
import { redact } from './index.js';
|
|
2
|
+
import { redact, RedactionLimitError } from './index.js';
|
|
3
|
+
const PEM_BEGIN = /-----BEGIN (?:RSA |EC |DSA |OPENSSH |PGP |ENCRYPTED )?PRIVATE KEY-----/;
|
|
4
|
+
const PEM_END = /-----END (?:RSA |EC |DSA |OPENSSH |PGP |ENCRYPTED )?PRIVATE KEY-----/;
|
|
3
5
|
export function redactStream(opts = {}) {
|
|
4
6
|
let carry = '';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
let multiline = '';
|
|
8
|
+
const maxRecordLength = opts.maxRecordLength ?? opts.limits?.maxInputLength ?? 16 * 1024 * 1024;
|
|
9
|
+
const processLine = (line) => {
|
|
10
|
+
if (multiline) {
|
|
11
|
+
multiline += line;
|
|
12
|
+
if (multiline.length > maxRecordLength) {
|
|
13
|
+
throw new RedactionLimitError(`Multiline stream record exceeds the configured limit of ${maxRecordLength}.`);
|
|
14
|
+
}
|
|
15
|
+
if (!PEM_END.test(line))
|
|
16
|
+
return '';
|
|
17
|
+
const out = redact(multiline, opts);
|
|
18
|
+
multiline = '';
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
if (PEM_BEGIN.test(line) && !PEM_END.test(line)) {
|
|
22
|
+
multiline = line;
|
|
23
|
+
return '';
|
|
24
|
+
}
|
|
25
|
+
return redact(line, opts);
|
|
26
|
+
};
|
|
27
|
+
const processCompleteLines = (text) => {
|
|
28
|
+
const data = carry + text;
|
|
29
|
+
const lastNewline = data.lastIndexOf('\n');
|
|
30
|
+
if (lastNewline === -1) {
|
|
31
|
+
carry = data;
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
carry = data.slice(lastNewline + 1);
|
|
35
|
+
const complete = data.slice(0, lastNewline + 1);
|
|
36
|
+
let out = '';
|
|
37
|
+
let start = 0;
|
|
38
|
+
for (let i = 0; i < complete.length; i++) {
|
|
39
|
+
if (complete.charCodeAt(i) !== 10)
|
|
40
|
+
continue;
|
|
41
|
+
out += processLine(complete.slice(start, i + 1));
|
|
42
|
+
start = i + 1;
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
9
45
|
};
|
|
10
46
|
return new Transform({
|
|
11
47
|
transform(chunk, _enc, cb) {
|
|
12
|
-
|
|
13
|
-
|
|
48
|
+
try {
|
|
49
|
+
cb(null, processCompleteLines(chunk.toString()));
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
cb(error);
|
|
53
|
+
}
|
|
14
54
|
},
|
|
15
55
|
flush(cb) {
|
|
16
|
-
|
|
56
|
+
try {
|
|
57
|
+
const pending = multiline + carry;
|
|
58
|
+
if (!pending) {
|
|
59
|
+
cb(null, '');
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (pending.length > maxRecordLength) {
|
|
63
|
+
throw new RedactionLimitError(`Multiline stream record exceeds the configured limit of ${maxRecordLength}.`);
|
|
64
|
+
}
|
|
65
|
+
const begin = pending.search(PEM_BEGIN);
|
|
66
|
+
const hasCompletePem = begin !== -1 && PEM_END.test(pending.slice(begin));
|
|
67
|
+
if (begin === -1 || hasCompletePem) {
|
|
68
|
+
cb(null, redact(pending, opts));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const prefix = begin > 0 ? redact(pending.slice(0, begin), opts) : '';
|
|
72
|
+
cb(null, `${prefix}[REDACTED PRIVATE KEY]`);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
cb(error);
|
|
76
|
+
}
|
|
17
77
|
},
|
|
18
78
|
});
|
|
19
79
|
}
|
package/dist/stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,MAAM,EAAsB,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,mBA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AC;oBACrC,MAAM,IAAI,mBAAmB,CAAC,2DAA2D,eAAe,GAAG,CAAC,CAAC;gBAC/G,CAAC;gBACD,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;gBACxC,MAAM,cAAc,GAAG,KAAK,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;gBAC1E,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,cAAc,EAAE,CAAC;oBACnC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;oBAChC,OAAO;gBACT,CAAC;gBACD,MAAM,MAAM,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBACtE,EAAE,CAAC,IAAI,EAAE,GAAG,MAAM,wBAAwB,CAAC,CAAC;YAC9C,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,EAAE,CAAC,KAAc,CAAC,CAAC;YACrB,CAAC;QACH,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
|
package/dist/tool.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type RedactOptions } from './index.js';
|
|
2
|
+
import { type Vault, type VaultOptions } from './vault.js';
|
|
3
|
+
export interface ToolBoundary {
|
|
4
|
+
/**
|
|
5
|
+
* Mask an MCP/tool result before it is appended to model context. New
|
|
6
|
+
* placeholders are added to this boundary's conversation-scoped vault.
|
|
7
|
+
*/
|
|
8
|
+
redactForModel<T>(result: T): T;
|
|
9
|
+
/** Restore placeholders in a model-produced tool call before execution. */
|
|
10
|
+
restoreForTool<T>(call: T): T;
|
|
11
|
+
/** Restore placeholders in the final model output before showing the app. */
|
|
12
|
+
restoreForApp<T>(output: T): T;
|
|
13
|
+
/** The conversation-scoped placeholder mapping. */
|
|
14
|
+
readonly vault: Vault;
|
|
15
|
+
readonly size: number;
|
|
16
|
+
reset(): void;
|
|
17
|
+
}
|
|
18
|
+
/** One-way redaction for logging or persisting a tool call safely. */
|
|
19
|
+
export declare function redactToolCall<T>(call: T, opts?: RedactOptions): T;
|
|
20
|
+
/** One-way redaction for logging or persisting a tool/MCP result safely. */
|
|
21
|
+
export declare function redactToolResult<T>(result: T, opts?: RedactOptions): T;
|
|
22
|
+
/** One-way redaction for an arbitrary JSON-RPC/MCP message. */
|
|
23
|
+
export declare function redactMcpMessage<T>(message: T, opts?: RedactOptions): T;
|
|
24
|
+
/**
|
|
25
|
+
* Reversible boundary for agent loops:
|
|
26
|
+
*
|
|
27
|
+
* model tool call -> restoreForTool -> execute -> redactForModel -> model
|
|
28
|
+
*/
|
|
29
|
+
export declare function createToolBoundary(opts?: VaultOptions): ToolBoundary;
|
|
30
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,EAAe,KAAK,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAExE,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,cAAc,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;IAChC,2EAA2E;IAC3E,cAAc,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC;IAC9B,6EAA6E;IAC7E,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;IAC/B,mDAAmD;IACnD,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,KAAK,IAAI,IAAI,CAAC;CACf;AAED,sEAAsE;AACtE,wBAAgB,cAAc,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAE,aAAkB,GAAG,CAAC,CAEtE;AAED,4EAA4E;AAC5E,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,GAAE,aAAkB,GAAG,CAAC,CAE1E;AAED,+DAA+D;AAC/D,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,GAAE,aAAkB,GAAG,CAAC,CAE3E;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,GAAE,YAAiB,GAAG,YAAY,CAgBxE"}
|
package/dist/tool.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { redact } from './index.js';
|
|
2
|
+
import { createVault } from './vault.js';
|
|
3
|
+
/** One-way redaction for logging or persisting a tool call safely. */
|
|
4
|
+
export function redactToolCall(call, opts = {}) {
|
|
5
|
+
return redact(call, opts);
|
|
6
|
+
}
|
|
7
|
+
/** One-way redaction for logging or persisting a tool/MCP result safely. */
|
|
8
|
+
export function redactToolResult(result, opts = {}) {
|
|
9
|
+
return redact(result, opts);
|
|
10
|
+
}
|
|
11
|
+
/** One-way redaction for an arbitrary JSON-RPC/MCP message. */
|
|
12
|
+
export function redactMcpMessage(message, opts = {}) {
|
|
13
|
+
return redact(message, opts);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Reversible boundary for agent loops:
|
|
17
|
+
*
|
|
18
|
+
* model tool call -> restoreForTool -> execute -> redactForModel -> model
|
|
19
|
+
*/
|
|
20
|
+
export function createToolBoundary(opts = {}) {
|
|
21
|
+
let vault = createVault(opts);
|
|
22
|
+
return {
|
|
23
|
+
redactForModel: (result) => vault.redact(result),
|
|
24
|
+
restoreForTool: (call) => vault.restore(call),
|
|
25
|
+
restoreForApp: (output) => vault.restore(output),
|
|
26
|
+
get vault() {
|
|
27
|
+
return vault;
|
|
28
|
+
},
|
|
29
|
+
get size() {
|
|
30
|
+
return vault.size;
|
|
31
|
+
},
|
|
32
|
+
reset() {
|
|
33
|
+
vault = createVault(opts);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=tool.js.map
|
package/dist/tool.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.js","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAsB,MAAM,YAAY,CAAC;AACxD,OAAO,EAAE,WAAW,EAAiC,MAAM,YAAY,CAAC;AAkBxE,sEAAsE;AACtE,MAAM,UAAU,cAAc,CAAI,IAAO,EAAE,OAAsB,EAAE;IACjE,OAAO,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAC5B,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,gBAAgB,CAAI,MAAS,EAAE,OAAsB,EAAE;IACrE,OAAO,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAC9B,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,gBAAgB,CAAI,OAAU,EAAE,OAAsB,EAAE;IACtE,OAAO,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAqB,EAAE;IACxD,IAAI,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAC9B,OAAO;QACL,cAAc,EAAE,CAAI,MAAS,EAAK,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;QACzD,cAAc,EAAE,CAAI,IAAO,EAAK,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACtD,aAAa,EAAE,CAAI,MAAS,EAAK,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QACzD,IAAI,KAAK;YACP,OAAO,KAAK,CAAC;QACf,CAAC;QACD,IAAI,IAAI;YACN,OAAO,KAAK,CAAC,IAAI,CAAC;QACpB,CAAC;QACD,KAAK;YACH,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;QAC5B,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/vault.d.ts
CHANGED
|
@@ -15,6 +15,15 @@ export interface VaultOptions extends RedactOptions {
|
|
|
15
15
|
* placeholders match first so prefix-like custom tokens cannot clobber peers.
|
|
16
16
|
*/
|
|
17
17
|
export declare function buildRestore(entries: Array<[string, string]>): (text: string) => string;
|
|
18
|
+
export interface IncrementalRestorer {
|
|
19
|
+
push(chunk: string): string;
|
|
20
|
+
flush(): string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Restore placeholders across arbitrary stream chunk boundaries. This uses the
|
|
24
|
+
* actual placeholder strings, so custom formats do not need square brackets.
|
|
25
|
+
*/
|
|
26
|
+
export declare function buildStreamRestore(entries: Array<[string, string]>): IncrementalRestorer;
|
|
18
27
|
/**
|
|
19
28
|
* A reversible redactor. `redact()` swaps each secret for a stable opaque
|
|
20
29
|
* placeholder and remembers the mapping; `restore()` puts the originals back.
|
package/dist/vault.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vault.d.ts","sourceRoot":"","sources":["../src/vault.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,aAAa,EACnB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"vault.d.ts","sourceRoot":"","sources":["../src/vault.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,aAAa,EACnB,MAAM,aAAa,CAAC;AAGrB,MAAM,WAAW,KAAK;IACpB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IACvB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IACxB,OAAO,IAAI,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,YAAa,SAAQ,aAAa;IACjD,WAAW,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IAC5D,iFAAiF;IACjF,gBAAgB,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAC;CAC1C;AAaD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAMvF;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,KAAK,IAAI,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,mBAAmB,CAkCxF;AAuBD;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,IAAI,GAAE,YAAiB,GAAG,KAAK,CAgD1D;AAED,wFAAwF;AACxF,wBAAgB,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CASpG"}
|
package/dist/vault.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { resolveDetectors, keyMatcher, allowMatcher, scanString, } from './engine.js';
|
|
2
|
+
import { mapGraph } from './graph.js';
|
|
2
3
|
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
3
4
|
function opaqueToken() {
|
|
4
5
|
const provider = globalThis.crypto;
|
|
@@ -22,18 +23,52 @@ export function buildRestore(entries) {
|
|
|
22
23
|
const re = new RegExp(sorted.map(([ph]) => escapeRe(ph)).join('|'), 'g');
|
|
23
24
|
return (text) => text.replace(re, (m) => lookup.get(m) ?? m);
|
|
24
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Restore placeholders across arbitrary stream chunk boundaries. This uses the
|
|
28
|
+
* actual placeholder strings, so custom formats do not need square brackets.
|
|
29
|
+
*/
|
|
30
|
+
export function buildStreamRestore(entries) {
|
|
31
|
+
const restoreText = buildRestore(entries);
|
|
32
|
+
const placeholders = entries.map(([placeholder]) => placeholder);
|
|
33
|
+
let buffer = '';
|
|
34
|
+
const pendingPrefixLength = () => {
|
|
35
|
+
let keep = 0;
|
|
36
|
+
for (const placeholder of placeholders) {
|
|
37
|
+
const limit = Math.min(buffer.length, placeholder.length - 1);
|
|
38
|
+
for (let length = limit; length > keep; length--) {
|
|
39
|
+
if (buffer.endsWith(placeholder.slice(0, length))) {
|
|
40
|
+
keep = length;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return keep;
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
push(chunk) {
|
|
49
|
+
buffer += chunk;
|
|
50
|
+
const keep = pendingPrefixLength();
|
|
51
|
+
const emitEnd = buffer.length - keep;
|
|
52
|
+
const emit = buffer.slice(0, emitEnd);
|
|
53
|
+
buffer = buffer.slice(emitEnd);
|
|
54
|
+
return restoreText(emit);
|
|
55
|
+
},
|
|
56
|
+
flush() {
|
|
57
|
+
const out = restoreText(buffer);
|
|
58
|
+
buffer = '';
|
|
59
|
+
return out;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
25
63
|
function walkStrings(value, fn) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return out;
|
|
35
|
-
}
|
|
36
|
-
return value;
|
|
64
|
+
return mapGraph(value, fn);
|
|
65
|
+
}
|
|
66
|
+
function redactGraph(value, redactString, matchKey, allow, mint) {
|
|
67
|
+
return mapGraph(value, redactString, (key, raw) => {
|
|
68
|
+
if (matchKey(key) && !allow(raw))
|
|
69
|
+
return mint(raw, 'sensitive_key');
|
|
70
|
+
return redactString(raw);
|
|
71
|
+
});
|
|
37
72
|
}
|
|
38
73
|
/**
|
|
39
74
|
* A reversible redactor. `redact()` swaps each secret for a stable opaque
|
|
@@ -79,25 +114,8 @@ export function createVault(opts = {}) {
|
|
|
79
114
|
}
|
|
80
115
|
return out + text.slice(cursor);
|
|
81
116
|
};
|
|
82
|
-
const redactWalk = (value) => {
|
|
83
|
-
if (typeof value === 'string')
|
|
84
|
-
return redactStr(value);
|
|
85
|
-
if (Array.isArray(value))
|
|
86
|
-
return value.map(redactWalk);
|
|
87
|
-
if (value && typeof value === 'object') {
|
|
88
|
-
const out = {};
|
|
89
|
-
for (const [k, v] of Object.entries(value)) {
|
|
90
|
-
if (typeof v === 'string' && matchKey(k) && !allow(v))
|
|
91
|
-
out[k] = mint(v, 'sensitive_key');
|
|
92
|
-
else
|
|
93
|
-
out[k] = redactWalk(v);
|
|
94
|
-
}
|
|
95
|
-
return out;
|
|
96
|
-
}
|
|
97
|
-
return value;
|
|
98
|
-
};
|
|
99
117
|
return {
|
|
100
|
-
redact: (input) =>
|
|
118
|
+
redact: (input) => redactGraph(input, redactStr, matchKey, allow, mint),
|
|
101
119
|
restore: (input) => walkStrings(input, buildRestore([...phToOrig])),
|
|
102
120
|
entries: () => [...phToOrig],
|
|
103
121
|
get size() {
|