flare-redact 0.6.0 → 0.6.2

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.
@@ -0,0 +1,44 @@
1
+ import { type Vault, type VaultOptions } from './vault.js';
2
+ export interface SessionOptions extends VaultOptions {
3
+ }
4
+ interface ChatMessage {
5
+ role?: string;
6
+ content: unknown;
7
+ [k: string]: unknown;
8
+ }
9
+ export interface StreamRestorer {
10
+ /** Feed a streamed chunk, get back the safe-to-display text so far. */
11
+ push(chunk: string): string;
12
+ /** Flush any held-back tail once the stream ends. */
13
+ flush(): string;
14
+ }
15
+ export interface Session {
16
+ /** Mask a user message before it reaches the model. */
17
+ redact<T>(input: T): T;
18
+ /** Restore the model's reply before showing it to the user. */
19
+ restore<T>(input: T): T;
20
+ /** Mask a whole `[{ role, content }]` chat array in one call. */
21
+ redactMessages<M extends ChatMessage>(messages: M[]): M[];
22
+ /** A restorer for streamed replies — rebuilds originals even across chunk splits. */
23
+ stream(): StreamRestorer;
24
+ /** The underlying vault (placeholder ↔ original map). */
25
+ readonly vault: Vault;
26
+ /** How many distinct values have been masked this session. */
27
+ readonly size: number;
28
+ /** Start a fresh conversation — a new vault, no carried-over mappings. */
29
+ reset(): void;
30
+ }
31
+ /**
32
+ * A conversation-scoped redactor for chat/AI apps. One session keeps one vault,
33
+ * so the same value maps to the same placeholder across every turn — mask the
34
+ * user's message on the way in, restore the model's answer on the way out. Model
35
+ * agnostic (works with a local model or any API), synchronous, and fast enough
36
+ * that the cost disappears next to inference.
37
+ *
38
+ * const session = createSession({ enable: ['pii'] });
39
+ * const safe = session.redact(userMessage); // → send `safe` to the model
40
+ * const reply = session.restore(modelReply); // → show `reply` to the user
41
+ */
42
+ export declare function createSession(opts?: SessionOptions): Session;
43
+ export {};
44
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,KAAK,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAEtF,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;AAcD;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,IAAI,GAAE,cAAmB,GAAG,OAAO,CAqChE"}
@@ -0,0 +1,59 @@
1
+ import { createVault, buildRestore } from './vault.js';
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
+ }
12
+ /**
13
+ * A conversation-scoped redactor for chat/AI apps. One session keeps one vault,
14
+ * so the same value maps to the same placeholder across every turn — mask the
15
+ * user's message on the way in, restore the model's answer on the way out. Model
16
+ * agnostic (works with a local model or any API), synchronous, and fast enough
17
+ * that the cost disappears next to inference.
18
+ *
19
+ * const session = createSession({ enable: ['pii'] });
20
+ * const safe = session.redact(userMessage); // → send `safe` to the model
21
+ * const reply = session.restore(modelReply); // → show `reply` to the user
22
+ */
23
+ export function createSession(opts = {}) {
24
+ let vault = createVault(opts);
25
+ return {
26
+ redact: (input) => vault.redact(input),
27
+ restore: (input) => vault.restore(input),
28
+ redactMessages: (messages) => messages.map((m) => ({ ...m, content: redactContent(m.content, vault) })),
29
+ stream() {
30
+ const restore = buildRestore(vault.entries());
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
+ };
47
+ },
48
+ get vault() {
49
+ return vault;
50
+ },
51
+ get size() {
52
+ return vault.size;
53
+ },
54
+ reset() {
55
+ vault = createVault(opts);
56
+ },
57
+ };
58
+ }
59
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAiC,MAAM,YAAY,CAAC;AAkCtF,SAAS,aAAa,CAAC,OAAgB,EAAE,KAAY;IACnD,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC9D,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,OAAO,OAA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AAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC;oBACjF,MAAM,IAAI,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;oBACnC,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;oBACzB,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;gBACvB,CAAC;gBACD,KAAK;oBACH,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;oBACzB,GAAG,GAAG,EAAE,CAAC;oBACT,OAAO,GAAG,CAAC;gBACb,CAAC;aACF,CAAC;QACJ,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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flare-redact",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Hide secrets & PII in logs, prompts, and text before they leak — with a reversible layer that keeps them out of your OpenAI/Anthropic calls. Zero dependencies, ReDoS-safe.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -19,6 +19,10 @@
19
19
  "types": "./dist/llm.d.ts",
20
20
  "default": "./dist/llm.js"
21
21
  },
22
+ "./session": {
23
+ "types": "./dist/session.d.ts",
24
+ "default": "./dist/session.js"
25
+ },
22
26
  "./pino": {
23
27
  "types": "./dist/pino.d.ts",
24
28
  "default": "./dist/pino.js"