anonyma 1.0.0 → 1.1.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 +273 -144
- package/README.md +427 -143
- package/dist/ai/index.cjs +522 -0
- package/dist/ai/index.cjs.map +1 -0
- package/dist/ai/index.d.cts +322 -0
- package/dist/ai/index.d.ts +322 -0
- package/dist/ai/index.js +505 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/audit/index.cjs +359 -0
- package/dist/audit/index.cjs.map +1 -0
- package/dist/audit/index.d.cts +287 -0
- package/dist/audit/index.d.ts +287 -0
- package/dist/audit/index.js +340 -0
- package/dist/audit/index.js.map +1 -0
- package/dist/chunk-26IHMQHL.cjs +662 -0
- package/dist/chunk-26IHMQHL.cjs.map +1 -0
- package/dist/chunk-2F2GWGTB.js +174 -0
- package/dist/chunk-2F2GWGTB.js.map +1 -0
- package/dist/chunk-2NNMJ55L.cjs +45 -0
- package/dist/chunk-2NNMJ55L.cjs.map +1 -0
- package/dist/chunk-2YG7R4KJ.cjs +114 -0
- package/dist/chunk-2YG7R4KJ.cjs.map +1 -0
- package/dist/chunk-2ZZHLCEJ.js +237 -0
- package/dist/chunk-2ZZHLCEJ.js.map +1 -0
- package/dist/chunk-3CY6ATKS.cjs +308 -0
- package/dist/chunk-3CY6ATKS.cjs.map +1 -0
- package/dist/chunk-3D5ZCJ7R.js +268 -0
- package/dist/chunk-3D5ZCJ7R.js.map +1 -0
- package/dist/chunk-6LDAIJBL.js +103 -0
- package/dist/chunk-6LDAIJBL.js.map +1 -0
- package/dist/chunk-7HK2V5Q4.js +111 -0
- package/dist/chunk-7HK2V5Q4.js.map +1 -0
- package/dist/chunk-AAP472NG.js +177 -0
- package/dist/chunk-AAP472NG.js.map +1 -0
- package/dist/chunk-AP4JIUNW.js +42 -0
- package/dist/chunk-AP4JIUNW.js.map +1 -0
- package/dist/chunk-BBJPQPGP.js +457 -0
- package/dist/chunk-BBJPQPGP.js.map +1 -0
- package/dist/chunk-CRXRJZEV.cjs +82 -0
- package/dist/chunk-CRXRJZEV.cjs.map +1 -0
- package/dist/chunk-E3PICZYI.js +20 -0
- package/dist/chunk-E3PICZYI.js.map +1 -0
- package/dist/chunk-F7O2FA3F.cjs +1255 -0
- package/dist/chunk-F7O2FA3F.cjs.map +1 -0
- package/dist/chunk-FB56NAAH.cjs +243 -0
- package/dist/chunk-FB56NAAH.cjs.map +1 -0
- package/dist/chunk-G4AN7FND.js +1063 -0
- package/dist/chunk-G4AN7FND.js.map +1 -0
- package/dist/chunk-GS3FRH4K.js +294 -0
- package/dist/chunk-GS3FRH4K.js.map +1 -0
- package/dist/chunk-GYMG5OI3.cjs +186 -0
- package/dist/chunk-GYMG5OI3.cjs.map +1 -0
- package/dist/chunk-IHEIYZPL.cjs +301 -0
- package/dist/chunk-IHEIYZPL.cjs.map +1 -0
- package/dist/chunk-IJAMLYOT.cjs +1087 -0
- package/dist/chunk-IJAMLYOT.cjs.map +1 -0
- package/dist/chunk-ISL5B4LU.cjs +181 -0
- package/dist/chunk-ISL5B4LU.cjs.map +1 -0
- package/dist/chunk-JKW36IPT.js +179 -0
- package/dist/chunk-JKW36IPT.js.map +1 -0
- package/dist/chunk-KOCQBDI4.cjs +179 -0
- package/dist/chunk-KOCQBDI4.cjs.map +1 -0
- package/dist/chunk-OGNLB2KA.js +79 -0
- package/dist/chunk-OGNLB2KA.js.map +1 -0
- package/dist/chunk-PKHZ7CVI.js +28 -0
- package/dist/chunk-PKHZ7CVI.js.map +1 -0
- package/dist/chunk-QYD4F6T7.cjs +114 -0
- package/dist/chunk-QYD4F6T7.cjs.map +1 -0
- package/dist/chunk-RCW5IPPR.js +1288 -0
- package/dist/chunk-RCW5IPPR.js.map +1 -0
- package/dist/chunk-RMMKYHUN.cjs +35 -0
- package/dist/chunk-RMMKYHUN.cjs.map +1 -0
- package/dist/chunk-S4BJ2ADB.cjs +22 -0
- package/dist/chunk-S4BJ2ADB.cjs.map +1 -0
- package/dist/chunk-S7NH6CHY.cjs +462 -0
- package/dist/chunk-S7NH6CHY.cjs.map +1 -0
- package/dist/chunk-SIHPDZXA.js +176 -0
- package/dist/chunk-SIHPDZXA.js.map +1 -0
- package/dist/chunk-TW7PM3UJ.cjs +30 -0
- package/dist/chunk-TW7PM3UJ.cjs.map +1 -0
- package/dist/chunk-UA3EOFBS.cjs +1322 -0
- package/dist/chunk-UA3EOFBS.cjs.map +1 -0
- package/dist/chunk-VE4CCW7W.js +32 -0
- package/dist/chunk-VE4CCW7W.js.map +1 -0
- package/dist/chunk-W6RBCHYF.js +651 -0
- package/dist/chunk-W6RBCHYF.js.map +1 -0
- package/dist/chunk-WLJN7GIS.cjs +189 -0
- package/dist/chunk-WLJN7GIS.cjs.map +1 -0
- package/dist/chunk-YMLR375Z.js +1244 -0
- package/dist/chunk-YMLR375Z.js.map +1 -0
- package/dist/compliance/index.cjs +88 -0
- package/dist/compliance/index.cjs.map +1 -0
- package/dist/compliance/index.d.cts +277 -0
- package/dist/compliance/index.d.ts +277 -0
- package/dist/compliance/index.js +47 -0
- package/dist/compliance/index.js.map +1 -0
- package/dist/crypto.cjs +12 -136
- package/dist/crypto.cjs.map +1 -1
- package/dist/crypto.d.cts +4 -3
- package/dist/crypto.d.ts +4 -3
- package/dist/crypto.js +3 -137
- package/dist/crypto.js.map +1 -1
- package/dist/detectors/index.cjs +146 -1483
- package/dist/detectors/index.cjs.map +1 -1
- package/dist/detectors/index.d.cts +1 -1
- package/dist/detectors/index.d.ts +1 -1
- package/dist/detectors/index.js +3 -1449
- package/dist/detectors/index.js.map +1 -1
- package/dist/engine/index.cjs +441 -0
- package/dist/engine/index.cjs.map +1 -0
- package/dist/engine/index.d.cts +728 -0
- package/dist/engine/index.d.ts +728 -0
- package/dist/engine/index.js +189 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/hash-2PSYNDMM.cjs +14 -0
- package/dist/hash-2PSYNDMM.cjs.map +1 -0
- package/dist/hash-CELLDNKN.js +5 -0
- package/dist/hash-CELLDNKN.js.map +1 -0
- package/dist/index.cjs +153 -2816
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +93 -11
- package/dist/index.d.ts +93 -11
- package/dist/index.js +13 -2791
- package/dist/index.js.map +1 -1
- package/dist/json-CKDvlRtl.d.ts +147 -0
- package/dist/json-s8ny-RCo.d.cts +147 -0
- package/dist/mcp/index.cjs +751 -0
- package/dist/mcp/index.cjs.map +1 -0
- package/dist/mcp/index.d.cts +226 -0
- package/dist/mcp/index.d.ts +226 -0
- package/dist/mcp/index.js +744 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/middleware/express.cjs +93 -0
- package/dist/middleware/express.cjs.map +1 -0
- package/dist/middleware/express.d.cts +90 -0
- package/dist/middleware/express.d.ts +90 -0
- package/dist/middleware/express.js +91 -0
- package/dist/middleware/express.js.map +1 -0
- package/dist/middleware/hono.cjs +58 -0
- package/dist/middleware/hono.cjs.map +1 -0
- package/dist/middleware/hono.d.cts +75 -0
- package/dist/middleware/hono.d.ts +75 -0
- package/dist/middleware/hono.js +56 -0
- package/dist/middleware/hono.js.map +1 -0
- package/dist/middleware/index.cjs +24 -0
- package/dist/middleware/index.cjs.map +1 -0
- package/dist/middleware/index.d.cts +106 -0
- package/dist/middleware/index.d.ts +106 -0
- package/dist/middleware/index.js +15 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/schemas.cjs +5 -5
- package/dist/schemas.cjs.map +1 -1
- package/dist/schemas.d.cts +20 -10
- package/dist/schemas.d.ts +20 -10
- package/dist/schemas.js +5 -5
- package/dist/schemas.js.map +1 -1
- package/dist/session-BdRftyZ5.d.cts +102 -0
- package/dist/session-Cs2zlNQp.d.ts +102 -0
- package/dist/stream.cjs +13 -2262
- package/dist/stream.cjs.map +1 -1
- package/dist/stream.d.cts +1 -1
- package/dist/stream.d.ts +1 -1
- package/dist/stream.js +6 -2255
- package/dist/stream.js.map +1 -1
- package/dist/types--tPm_AjY.d.ts +199 -0
- package/dist/types-0BJH_wqc.d.cts +199 -0
- package/dist/types-CPUhjvl8.d.cts +629 -0
- package/dist/types-Dtht33aq.d.ts +629 -0
- package/dist/{types-D-9HzVuy.d.cts → types-EJTj_BKY.d.cts} +38 -7
- package/dist/{types-D-9HzVuy.d.ts → types-EJTj_BKY.d.ts} +38 -7
- package/dist/types-Zt_bZvwS.d.cts +182 -0
- package/dist/types-Zt_bZvwS.d.ts +182 -0
- package/dist/validators.cjs +39 -181
- package/dist/validators.cjs.map +1 -1
- package/dist/validators.js +1 -174
- package/dist/validators.js.map +1 -1
- package/dist/vault/index.cjs +688 -0
- package/dist/vault/index.cjs.map +1 -0
- package/dist/vault/index.d.cts +385 -0
- package/dist/vault/index.d.ts +385 -0
- package/dist/vault/index.js +669 -0
- package/dist/vault/index.js.map +1 -0
- package/package.json +128 -11
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { K as KeyRule } from '../json-s8ny-RCo.cjs';
|
|
2
|
+
export { J as JsonSanitizeOptions, a as JsonSanitizeResult, s as sanitizeJson, b as sanitizeJsonAsync } from '../json-s8ny-RCo.cjs';
|
|
3
|
+
import { T as TokenizationProvider, A as AsyncChunkTransformer, R as RestoreResult, c as PipelineSpec } from '../types-CPUhjvl8.cjs';
|
|
4
|
+
import { A as AuditFieldEntry } from '../types-Zt_bZvwS.cjs';
|
|
5
|
+
import { S as SessionTokenizer, a as SessionSnapshot, b as SessionTokenizerOptions } from '../session-BdRftyZ5.cjs';
|
|
6
|
+
import '../types-EJTj_BKY.cjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @module ai/restore
|
|
10
|
+
* @description Restoring tokens in model output that arrives as a stream. A
|
|
11
|
+
* token may be split across chunks (`[EMAIL_00` + `01]`), so the transformer
|
|
12
|
+
* holds back a trailing fragment that could still become a token.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create a push-style transformer that restores the tokens of `provider` in
|
|
17
|
+
* text arriving in arbitrary chunks.
|
|
18
|
+
*
|
|
19
|
+
* The output is the same as restoring the whole text at once, however the
|
|
20
|
+
* text is split, and accepts the same spellings of a token: a provider with
|
|
21
|
+
* its own `restore()` (a lenient session) is used through it.
|
|
22
|
+
*
|
|
23
|
+
* @param provider - The provider that issued the tokens.
|
|
24
|
+
* @returns An {@link AsyncChunkTransformer}. Await each `push()` before the next.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const restorer = createRestoreTransformer(session);
|
|
29
|
+
* for await (const delta of modelStream) ui.append(await restorer.push(delta));
|
|
30
|
+
* ui.append(await restorer.flush());
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function createRestoreTransformer(provider: TokenizationProvider): AsyncChunkTransformer;
|
|
34
|
+
/**
|
|
35
|
+
* Create a WHATWG `TransformStream` that restores tokens in a stream of text.
|
|
36
|
+
*
|
|
37
|
+
* @param provider - The provider that issued the tokens.
|
|
38
|
+
* @returns A `TransformStream<string, string>`.
|
|
39
|
+
* @throws {@link ValidationError} When `TransformStream` is unavailable.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const restored = modelTextStream.pipeThrough(createRestoreStream(session));
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function createRestoreStream(provider: TokenizationProvider): TransformStream<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* Restore the tokens inside a JSON document held as text, such as the
|
|
49
|
+
* arguments of a tool call. Values are restored into the parsed document and
|
|
50
|
+
* the document is serialised again, so a restored value that contains a
|
|
51
|
+
* quote, a backslash or a line break can neither break the document nor add
|
|
52
|
+
* members to it. A text that is not JSON is restored as plain text; a
|
|
53
|
+
* document without tokens is returned as it was written.
|
|
54
|
+
*
|
|
55
|
+
* @param text - The JSON document.
|
|
56
|
+
* @param provider - The provider that issued the tokens.
|
|
57
|
+
* @returns The document with restored values.
|
|
58
|
+
* @throws {@link ValidationError} When the document is nested deeper than 512 levels.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* await restoreJsonText('{"to":"[EMAIL_0001]"}', session); // '{"to":"alice@example.com"}'
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
declare function restoreJsonText(text: string, provider: TokenizationProvider): Promise<string>;
|
|
66
|
+
/**
|
|
67
|
+
* Reads and writes the text of a structured stream chunk.
|
|
68
|
+
*/
|
|
69
|
+
interface ChunkLens<C> {
|
|
70
|
+
/** The text of the chunk, or `undefined` when the chunk carries no text. */
|
|
71
|
+
readonly get: (chunk: C) => string | undefined;
|
|
72
|
+
/** A copy of the chunk with its text replaced. */
|
|
73
|
+
readonly set: (chunk: C, text: string) => C;
|
|
74
|
+
/**
|
|
75
|
+
* Identifies the text block a chunk belongs to, when a stream can carry
|
|
76
|
+
* several at once. Chunks of different blocks are restored independently.
|
|
77
|
+
*/
|
|
78
|
+
readonly block?: (chunk: C) => string | undefined;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Lens for chunks shaped `{ type: "text-delta", delta }` or
|
|
82
|
+
* `{ type: "text-delta", textDelta }` (the stream parts of the Vercel AI SDK).
|
|
83
|
+
*/
|
|
84
|
+
declare const textDeltaLens: ChunkLens<Record<string, unknown>>;
|
|
85
|
+
/**
|
|
86
|
+
* Lens for chunks whose text is a string member named `content` (message
|
|
87
|
+
* chunks of LangChain) .
|
|
88
|
+
*/
|
|
89
|
+
declare const contentLens: ChunkLens<Record<string, unknown>>;
|
|
90
|
+
/**
|
|
91
|
+
* Lens for chunks whose text is a string member named `delta` (response
|
|
92
|
+
* chunks of LlamaIndex).
|
|
93
|
+
*/
|
|
94
|
+
declare const deltaLens: ChunkLens<Record<string, unknown>>;
|
|
95
|
+
/**
|
|
96
|
+
* Restore tokens in a stream of structured chunks. Text that is held back
|
|
97
|
+
* because it may be the start of a token is delivered with the next text
|
|
98
|
+
* chunk of its block, and in any case before the next chunk that carries no
|
|
99
|
+
* text, so the order of the stream is kept: nothing arrives after the chunk
|
|
100
|
+
* that ends its block. A token that is split around a chunk without text is
|
|
101
|
+
* therefore not restored.
|
|
102
|
+
*
|
|
103
|
+
* @param source - The chunks produced by the model SDK.
|
|
104
|
+
* @param lens - How to read and write the text of a chunk.
|
|
105
|
+
* @param provider - The provider that issued the tokens.
|
|
106
|
+
* @returns The chunks with restored text.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* for await (const chunk of restoreChunks(await chain.stream(input), contentLens, session)) {
|
|
111
|
+
* process.stdout.write(String(chunk.content));
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
declare function restoreChunks<C>(source: AsyncIterable<C>, lens: ChunkLens<C>, provider: TokenizationProvider): AsyncGenerator<C, void, undefined>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @module ai/guard
|
|
119
|
+
* @description Redaction middleware for LLM calls: personal data is replaced
|
|
120
|
+
* by tokens before a prompt leaves the process, and the tokens are replaced
|
|
121
|
+
* by the original values in what comes back — also when it comes back as a
|
|
122
|
+
* stream. The mapping between tokens and values never leaves the process.
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Options accepted by {@link createLlmGuard}.
|
|
127
|
+
*/
|
|
128
|
+
interface LlmGuardOptions {
|
|
129
|
+
/**
|
|
130
|
+
* What to detect. The strategy members of the spec are ignored: every
|
|
131
|
+
* detection is replaced by a session token. Defaults to every built-in
|
|
132
|
+
* category.
|
|
133
|
+
*/
|
|
134
|
+
readonly spec?: Omit<PipelineSpec, "defaultStrategy" | "rules">;
|
|
135
|
+
/** Options for the session tokenizer of each exchange. `lenient` defaults to `true`. */
|
|
136
|
+
readonly session?: Omit<SessionTokenizerOptions, "snapshot">;
|
|
137
|
+
/** Message roles that are sent unchanged, for example `["system"]`. */
|
|
138
|
+
readonly skipRoles?: readonly string[];
|
|
139
|
+
/** Keys whose values are tokenized whole, such as `password`. */
|
|
140
|
+
readonly keyRules?: readonly KeyRule[];
|
|
141
|
+
/**
|
|
142
|
+
* Further members that must reach the model byte for byte, by key name at
|
|
143
|
+
* any depth. Signatures, encrypted reasoning, provider options and media
|
|
144
|
+
* payloads are passed on without this. Whatever is passed on is listed in
|
|
145
|
+
* {@link SanitizedMessages.unscanned}.
|
|
146
|
+
*/
|
|
147
|
+
readonly passthroughKeys?: readonly string[];
|
|
148
|
+
/**
|
|
149
|
+
* Counts the tokens of a text with the tokenizer of the target model. When
|
|
150
|
+
* given, {@link SanitizedMessages.tokens} reports the prompt size before and
|
|
151
|
+
* after sanitization.
|
|
152
|
+
*/
|
|
153
|
+
readonly countTokens?: (text: string) => number;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Sanitized chat messages and an account of what was replaced.
|
|
157
|
+
*/
|
|
158
|
+
interface SanitizedMessages<M> {
|
|
159
|
+
/** The messages to send: same length, same structure, tokens in place of personal data. */
|
|
160
|
+
readonly messages: M[];
|
|
161
|
+
/** What was replaced where, without any of the replaced text. */
|
|
162
|
+
readonly fields: readonly AuditFieldEntry[];
|
|
163
|
+
/** Total number of replacements. */
|
|
164
|
+
readonly replaced: number;
|
|
165
|
+
/**
|
|
166
|
+
* JSON Pointers of what was passed on without inspection: media payloads
|
|
167
|
+
* (images, files, audio), members a provider needs byte for byte
|
|
168
|
+
* (signatures, encrypted reasoning), `passthroughKeys`, and values that are
|
|
169
|
+
* not JSON.
|
|
170
|
+
*/
|
|
171
|
+
readonly unscanned: readonly string[];
|
|
172
|
+
/** JSON Pointers of members whose key held personal data and was replaced by a token. */
|
|
173
|
+
readonly flaggedKeys: readonly string[];
|
|
174
|
+
/** Prompt size in model tokens before and after, when `countTokens` is configured. */
|
|
175
|
+
readonly tokens?: {
|
|
176
|
+
readonly before: number;
|
|
177
|
+
readonly after: number;
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* One guarded exchange with a model: a request and its response, or a whole
|
|
182
|
+
* conversation. Everything sanitized through the same exchange shares one
|
|
183
|
+
* token mapping, so a value keeps its token across turns.
|
|
184
|
+
*/
|
|
185
|
+
interface LlmExchange {
|
|
186
|
+
/** The session that holds the token mapping. */
|
|
187
|
+
readonly session: SessionTokenizer;
|
|
188
|
+
/** Replace personal data in a text with tokens. */
|
|
189
|
+
readonly sanitizeText: (text: string) => string;
|
|
190
|
+
/**
|
|
191
|
+
* Replace personal data in chat messages with tokens, preserving their
|
|
192
|
+
* structure. Structural members are left alone where the message formats
|
|
193
|
+
* define them — `role`, `name`, identifiers and `type` on a message or a
|
|
194
|
+
* content part, the function name of a tool call — and nowhere else: tool
|
|
195
|
+
* arguments and tool results are sanitized at every depth, object keys
|
|
196
|
+
* included. Of a media part the payload is passed on and listed in
|
|
197
|
+
* `unscanned`; its text (title, context, file name, a text source) is
|
|
198
|
+
* sanitized.
|
|
199
|
+
*
|
|
200
|
+
* @throws ValidationError When a message is not a plain object. A class
|
|
201
|
+
* instance cannot be copied with tokens in place of its content: sanitize
|
|
202
|
+
* the text before the message object is built, with `sanitizeText()`.
|
|
203
|
+
*/
|
|
204
|
+
readonly sanitizeMessages: <M>(messages: readonly M[]) => SanitizedMessages<M>;
|
|
205
|
+
/** Replace tokens in model output with the original values. */
|
|
206
|
+
readonly restoreText: (text: string) => RestoreResult;
|
|
207
|
+
/** A transform stream that restores tokens in streamed model output. */
|
|
208
|
+
readonly restoreStream: () => TransformStream<string, string>;
|
|
209
|
+
/** Restore tokens in model output delivered as an async iterable of text. */
|
|
210
|
+
readonly restoreIterable: (source: AsyncIterable<string>) => AsyncGenerator<string, void, undefined>;
|
|
211
|
+
/** A serialisable copy of the token mapping, to continue the conversation later. */
|
|
212
|
+
readonly snapshot: () => SessionSnapshot;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The redaction middleware.
|
|
216
|
+
*/
|
|
217
|
+
interface LlmGuard {
|
|
218
|
+
/**
|
|
219
|
+
* Start an exchange.
|
|
220
|
+
*
|
|
221
|
+
* @param snapshot - The snapshot of an earlier exchange to continue from.
|
|
222
|
+
*/
|
|
223
|
+
readonly begin: (snapshot?: SessionSnapshot) => LlmExchange;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Create the redaction middleware for LLM calls.
|
|
227
|
+
*
|
|
228
|
+
* @param options - What to detect and how tokens look.
|
|
229
|
+
* @returns The {@link LlmGuard}.
|
|
230
|
+
* @throws {@link ValidationError} When the spec is invalid.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* import { createLlmGuard } from "anonyma/ai";
|
|
235
|
+
*
|
|
236
|
+
* const guard = createLlmGuard({ skipRoles: ["system"] });
|
|
237
|
+
*
|
|
238
|
+
* const exchange = guard.begin();
|
|
239
|
+
* const { messages } = exchange.sanitizeMessages([
|
|
240
|
+
* { role: "system", content: "You are a support assistant." },
|
|
241
|
+
* { role: "user", content: "Email alice@example.com about invoice 42." },
|
|
242
|
+
* ]);
|
|
243
|
+
* // messages[1].content === "Email [EMAIL_0001] about invoice 42."
|
|
244
|
+
*
|
|
245
|
+
* const stream = await llm.streamText(messages); // "Sure, I wrote to [EMAIL_0001]."
|
|
246
|
+
* for await (const text of exchange.restoreIterable(stream)) {
|
|
247
|
+
* process.stdout.write(text); // "Sure, I wrote to alice@example.com."
|
|
248
|
+
* }
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
declare function createLlmGuard(options?: LlmGuardOptions): LlmGuard;
|
|
252
|
+
/**
|
|
253
|
+
* The parts of a language-model middleware this adapter implements. The
|
|
254
|
+
* shape matches `LanguageModelMiddleware` of the Vercel AI SDK structurally,
|
|
255
|
+
* so the object can be passed to `wrapLanguageModel()` without this library
|
|
256
|
+
* depending on the SDK.
|
|
257
|
+
*/
|
|
258
|
+
interface LanguageModelMiddlewareLike {
|
|
259
|
+
/** Sanitizes the prompt before it reaches the model. */
|
|
260
|
+
readonly transformParams: (input: {
|
|
261
|
+
readonly params: Record<string, unknown>;
|
|
262
|
+
}) => Promise<Record<string, unknown>>;
|
|
263
|
+
/** Restores tokens in a complete response. */
|
|
264
|
+
readonly wrapGenerate: (input: {
|
|
265
|
+
readonly doGenerate: () => PromiseLike<unknown>;
|
|
266
|
+
readonly params: Record<string, unknown>;
|
|
267
|
+
}) => Promise<unknown>;
|
|
268
|
+
/** Restores tokens in a streamed response. */
|
|
269
|
+
readonly wrapStream: (input: {
|
|
270
|
+
readonly doStream: () => PromiseLike<{
|
|
271
|
+
readonly stream: ReadableStream<unknown>;
|
|
272
|
+
}>;
|
|
273
|
+
readonly params: Record<string, unknown>;
|
|
274
|
+
}) => Promise<{
|
|
275
|
+
readonly stream: ReadableStream<unknown>;
|
|
276
|
+
}>;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Options accepted by {@link toLanguageModelMiddleware}.
|
|
280
|
+
*/
|
|
281
|
+
interface LanguageModelMiddlewareOptions {
|
|
282
|
+
/**
|
|
283
|
+
* Called with the account of every sanitized prompt: what was replaced,
|
|
284
|
+
* and above all what was passed on without inspection (`unscanned`). Throw
|
|
285
|
+
* from it to stop a request that must not be sent.
|
|
286
|
+
*/
|
|
287
|
+
readonly onSanitized?: (report: SanitizedMessages<unknown>) => void;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Adapt a guard to the language-model middleware shape of the Vercel AI SDK.
|
|
291
|
+
*
|
|
292
|
+
* The prompt (`params.prompt`) is sanitized on the way in. On the way out,
|
|
293
|
+
* tokens are restored in the result of `doGenerate()` and in the `text-delta`
|
|
294
|
+
* and `tool-call` parts of `doStream()`; the arguments of a tool call are
|
|
295
|
+
* restored as the JSON document they are. Partial tool input
|
|
296
|
+
* (`tool-input-delta`) is passed on as it is. Each call gets its own exchange.
|
|
297
|
+
*
|
|
298
|
+
* @param guard - The guard to adapt.
|
|
299
|
+
* @param options - A callback that receives the account of each sanitized prompt.
|
|
300
|
+
* @returns An object to pass as `middleware` to `wrapLanguageModel()`.
|
|
301
|
+
*
|
|
302
|
+
* @remarks
|
|
303
|
+
* The adapter is typed structurally and tested against stand-ins for the SDK,
|
|
304
|
+
* not against the SDK itself. Check it against the SDK version you use.
|
|
305
|
+
* `wrapGenerate` and `wrapStream` need the `params` object that
|
|
306
|
+
* `transformParams` returned (a copy made with spread syntax works too) and
|
|
307
|
+
* throw when they get another one: restoring nothing would go unnoticed.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* import { wrapLanguageModel } from "ai";
|
|
312
|
+
* import { createLlmGuard, toLanguageModelMiddleware } from "anonyma/ai";
|
|
313
|
+
*
|
|
314
|
+
* const model = wrapLanguageModel({
|
|
315
|
+
* model: baseModel,
|
|
316
|
+
* middleware: toLanguageModelMiddleware(createLlmGuard()),
|
|
317
|
+
* });
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
declare function toLanguageModelMiddleware(guard: LlmGuard, options?: LanguageModelMiddlewareOptions): LanguageModelMiddlewareLike;
|
|
321
|
+
|
|
322
|
+
export { type ChunkLens, KeyRule, type LanguageModelMiddlewareLike, type LanguageModelMiddlewareOptions, type LlmExchange, type LlmGuard, type LlmGuardOptions, type SanitizedMessages, contentLens, createLlmGuard, createRestoreStream, createRestoreTransformer, deltaLens, restoreChunks, restoreJsonText, textDeltaLens, toLanguageModelMiddleware };
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { K as KeyRule } from '../json-CKDvlRtl.js';
|
|
2
|
+
export { J as JsonSanitizeOptions, a as JsonSanitizeResult, s as sanitizeJson, b as sanitizeJsonAsync } from '../json-CKDvlRtl.js';
|
|
3
|
+
import { T as TokenizationProvider, A as AsyncChunkTransformer, R as RestoreResult, c as PipelineSpec } from '../types-Dtht33aq.js';
|
|
4
|
+
import { A as AuditFieldEntry } from '../types-Zt_bZvwS.js';
|
|
5
|
+
import { S as SessionTokenizer, a as SessionSnapshot, b as SessionTokenizerOptions } from '../session-Cs2zlNQp.js';
|
|
6
|
+
import '../types-EJTj_BKY.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @module ai/restore
|
|
10
|
+
* @description Restoring tokens in model output that arrives as a stream. A
|
|
11
|
+
* token may be split across chunks (`[EMAIL_00` + `01]`), so the transformer
|
|
12
|
+
* holds back a trailing fragment that could still become a token.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create a push-style transformer that restores the tokens of `provider` in
|
|
17
|
+
* text arriving in arbitrary chunks.
|
|
18
|
+
*
|
|
19
|
+
* The output is the same as restoring the whole text at once, however the
|
|
20
|
+
* text is split, and accepts the same spellings of a token: a provider with
|
|
21
|
+
* its own `restore()` (a lenient session) is used through it.
|
|
22
|
+
*
|
|
23
|
+
* @param provider - The provider that issued the tokens.
|
|
24
|
+
* @returns An {@link AsyncChunkTransformer}. Await each `push()` before the next.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const restorer = createRestoreTransformer(session);
|
|
29
|
+
* for await (const delta of modelStream) ui.append(await restorer.push(delta));
|
|
30
|
+
* ui.append(await restorer.flush());
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare function createRestoreTransformer(provider: TokenizationProvider): AsyncChunkTransformer;
|
|
34
|
+
/**
|
|
35
|
+
* Create a WHATWG `TransformStream` that restores tokens in a stream of text.
|
|
36
|
+
*
|
|
37
|
+
* @param provider - The provider that issued the tokens.
|
|
38
|
+
* @returns A `TransformStream<string, string>`.
|
|
39
|
+
* @throws {@link ValidationError} When `TransformStream` is unavailable.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const restored = modelTextStream.pipeThrough(createRestoreStream(session));
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function createRestoreStream(provider: TokenizationProvider): TransformStream<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* Restore the tokens inside a JSON document held as text, such as the
|
|
49
|
+
* arguments of a tool call. Values are restored into the parsed document and
|
|
50
|
+
* the document is serialised again, so a restored value that contains a
|
|
51
|
+
* quote, a backslash or a line break can neither break the document nor add
|
|
52
|
+
* members to it. A text that is not JSON is restored as plain text; a
|
|
53
|
+
* document without tokens is returned as it was written.
|
|
54
|
+
*
|
|
55
|
+
* @param text - The JSON document.
|
|
56
|
+
* @param provider - The provider that issued the tokens.
|
|
57
|
+
* @returns The document with restored values.
|
|
58
|
+
* @throws {@link ValidationError} When the document is nested deeper than 512 levels.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* await restoreJsonText('{"to":"[EMAIL_0001]"}', session); // '{"to":"alice@example.com"}'
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
declare function restoreJsonText(text: string, provider: TokenizationProvider): Promise<string>;
|
|
66
|
+
/**
|
|
67
|
+
* Reads and writes the text of a structured stream chunk.
|
|
68
|
+
*/
|
|
69
|
+
interface ChunkLens<C> {
|
|
70
|
+
/** The text of the chunk, or `undefined` when the chunk carries no text. */
|
|
71
|
+
readonly get: (chunk: C) => string | undefined;
|
|
72
|
+
/** A copy of the chunk with its text replaced. */
|
|
73
|
+
readonly set: (chunk: C, text: string) => C;
|
|
74
|
+
/**
|
|
75
|
+
* Identifies the text block a chunk belongs to, when a stream can carry
|
|
76
|
+
* several at once. Chunks of different blocks are restored independently.
|
|
77
|
+
*/
|
|
78
|
+
readonly block?: (chunk: C) => string | undefined;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Lens for chunks shaped `{ type: "text-delta", delta }` or
|
|
82
|
+
* `{ type: "text-delta", textDelta }` (the stream parts of the Vercel AI SDK).
|
|
83
|
+
*/
|
|
84
|
+
declare const textDeltaLens: ChunkLens<Record<string, unknown>>;
|
|
85
|
+
/**
|
|
86
|
+
* Lens for chunks whose text is a string member named `content` (message
|
|
87
|
+
* chunks of LangChain) .
|
|
88
|
+
*/
|
|
89
|
+
declare const contentLens: ChunkLens<Record<string, unknown>>;
|
|
90
|
+
/**
|
|
91
|
+
* Lens for chunks whose text is a string member named `delta` (response
|
|
92
|
+
* chunks of LlamaIndex).
|
|
93
|
+
*/
|
|
94
|
+
declare const deltaLens: ChunkLens<Record<string, unknown>>;
|
|
95
|
+
/**
|
|
96
|
+
* Restore tokens in a stream of structured chunks. Text that is held back
|
|
97
|
+
* because it may be the start of a token is delivered with the next text
|
|
98
|
+
* chunk of its block, and in any case before the next chunk that carries no
|
|
99
|
+
* text, so the order of the stream is kept: nothing arrives after the chunk
|
|
100
|
+
* that ends its block. A token that is split around a chunk without text is
|
|
101
|
+
* therefore not restored.
|
|
102
|
+
*
|
|
103
|
+
* @param source - The chunks produced by the model SDK.
|
|
104
|
+
* @param lens - How to read and write the text of a chunk.
|
|
105
|
+
* @param provider - The provider that issued the tokens.
|
|
106
|
+
* @returns The chunks with restored text.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* for await (const chunk of restoreChunks(await chain.stream(input), contentLens, session)) {
|
|
111
|
+
* process.stdout.write(String(chunk.content));
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
declare function restoreChunks<C>(source: AsyncIterable<C>, lens: ChunkLens<C>, provider: TokenizationProvider): AsyncGenerator<C, void, undefined>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @module ai/guard
|
|
119
|
+
* @description Redaction middleware for LLM calls: personal data is replaced
|
|
120
|
+
* by tokens before a prompt leaves the process, and the tokens are replaced
|
|
121
|
+
* by the original values in what comes back — also when it comes back as a
|
|
122
|
+
* stream. The mapping between tokens and values never leaves the process.
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Options accepted by {@link createLlmGuard}.
|
|
127
|
+
*/
|
|
128
|
+
interface LlmGuardOptions {
|
|
129
|
+
/**
|
|
130
|
+
* What to detect. The strategy members of the spec are ignored: every
|
|
131
|
+
* detection is replaced by a session token. Defaults to every built-in
|
|
132
|
+
* category.
|
|
133
|
+
*/
|
|
134
|
+
readonly spec?: Omit<PipelineSpec, "defaultStrategy" | "rules">;
|
|
135
|
+
/** Options for the session tokenizer of each exchange. `lenient` defaults to `true`. */
|
|
136
|
+
readonly session?: Omit<SessionTokenizerOptions, "snapshot">;
|
|
137
|
+
/** Message roles that are sent unchanged, for example `["system"]`. */
|
|
138
|
+
readonly skipRoles?: readonly string[];
|
|
139
|
+
/** Keys whose values are tokenized whole, such as `password`. */
|
|
140
|
+
readonly keyRules?: readonly KeyRule[];
|
|
141
|
+
/**
|
|
142
|
+
* Further members that must reach the model byte for byte, by key name at
|
|
143
|
+
* any depth. Signatures, encrypted reasoning, provider options and media
|
|
144
|
+
* payloads are passed on without this. Whatever is passed on is listed in
|
|
145
|
+
* {@link SanitizedMessages.unscanned}.
|
|
146
|
+
*/
|
|
147
|
+
readonly passthroughKeys?: readonly string[];
|
|
148
|
+
/**
|
|
149
|
+
* Counts the tokens of a text with the tokenizer of the target model. When
|
|
150
|
+
* given, {@link SanitizedMessages.tokens} reports the prompt size before and
|
|
151
|
+
* after sanitization.
|
|
152
|
+
*/
|
|
153
|
+
readonly countTokens?: (text: string) => number;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Sanitized chat messages and an account of what was replaced.
|
|
157
|
+
*/
|
|
158
|
+
interface SanitizedMessages<M> {
|
|
159
|
+
/** The messages to send: same length, same structure, tokens in place of personal data. */
|
|
160
|
+
readonly messages: M[];
|
|
161
|
+
/** What was replaced where, without any of the replaced text. */
|
|
162
|
+
readonly fields: readonly AuditFieldEntry[];
|
|
163
|
+
/** Total number of replacements. */
|
|
164
|
+
readonly replaced: number;
|
|
165
|
+
/**
|
|
166
|
+
* JSON Pointers of what was passed on without inspection: media payloads
|
|
167
|
+
* (images, files, audio), members a provider needs byte for byte
|
|
168
|
+
* (signatures, encrypted reasoning), `passthroughKeys`, and values that are
|
|
169
|
+
* not JSON.
|
|
170
|
+
*/
|
|
171
|
+
readonly unscanned: readonly string[];
|
|
172
|
+
/** JSON Pointers of members whose key held personal data and was replaced by a token. */
|
|
173
|
+
readonly flaggedKeys: readonly string[];
|
|
174
|
+
/** Prompt size in model tokens before and after, when `countTokens` is configured. */
|
|
175
|
+
readonly tokens?: {
|
|
176
|
+
readonly before: number;
|
|
177
|
+
readonly after: number;
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* One guarded exchange with a model: a request and its response, or a whole
|
|
182
|
+
* conversation. Everything sanitized through the same exchange shares one
|
|
183
|
+
* token mapping, so a value keeps its token across turns.
|
|
184
|
+
*/
|
|
185
|
+
interface LlmExchange {
|
|
186
|
+
/** The session that holds the token mapping. */
|
|
187
|
+
readonly session: SessionTokenizer;
|
|
188
|
+
/** Replace personal data in a text with tokens. */
|
|
189
|
+
readonly sanitizeText: (text: string) => string;
|
|
190
|
+
/**
|
|
191
|
+
* Replace personal data in chat messages with tokens, preserving their
|
|
192
|
+
* structure. Structural members are left alone where the message formats
|
|
193
|
+
* define them — `role`, `name`, identifiers and `type` on a message or a
|
|
194
|
+
* content part, the function name of a tool call — and nowhere else: tool
|
|
195
|
+
* arguments and tool results are sanitized at every depth, object keys
|
|
196
|
+
* included. Of a media part the payload is passed on and listed in
|
|
197
|
+
* `unscanned`; its text (title, context, file name, a text source) is
|
|
198
|
+
* sanitized.
|
|
199
|
+
*
|
|
200
|
+
* @throws ValidationError When a message is not a plain object. A class
|
|
201
|
+
* instance cannot be copied with tokens in place of its content: sanitize
|
|
202
|
+
* the text before the message object is built, with `sanitizeText()`.
|
|
203
|
+
*/
|
|
204
|
+
readonly sanitizeMessages: <M>(messages: readonly M[]) => SanitizedMessages<M>;
|
|
205
|
+
/** Replace tokens in model output with the original values. */
|
|
206
|
+
readonly restoreText: (text: string) => RestoreResult;
|
|
207
|
+
/** A transform stream that restores tokens in streamed model output. */
|
|
208
|
+
readonly restoreStream: () => TransformStream<string, string>;
|
|
209
|
+
/** Restore tokens in model output delivered as an async iterable of text. */
|
|
210
|
+
readonly restoreIterable: (source: AsyncIterable<string>) => AsyncGenerator<string, void, undefined>;
|
|
211
|
+
/** A serialisable copy of the token mapping, to continue the conversation later. */
|
|
212
|
+
readonly snapshot: () => SessionSnapshot;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* The redaction middleware.
|
|
216
|
+
*/
|
|
217
|
+
interface LlmGuard {
|
|
218
|
+
/**
|
|
219
|
+
* Start an exchange.
|
|
220
|
+
*
|
|
221
|
+
* @param snapshot - The snapshot of an earlier exchange to continue from.
|
|
222
|
+
*/
|
|
223
|
+
readonly begin: (snapshot?: SessionSnapshot) => LlmExchange;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Create the redaction middleware for LLM calls.
|
|
227
|
+
*
|
|
228
|
+
* @param options - What to detect and how tokens look.
|
|
229
|
+
* @returns The {@link LlmGuard}.
|
|
230
|
+
* @throws {@link ValidationError} When the spec is invalid.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* import { createLlmGuard } from "anonyma/ai";
|
|
235
|
+
*
|
|
236
|
+
* const guard = createLlmGuard({ skipRoles: ["system"] });
|
|
237
|
+
*
|
|
238
|
+
* const exchange = guard.begin();
|
|
239
|
+
* const { messages } = exchange.sanitizeMessages([
|
|
240
|
+
* { role: "system", content: "You are a support assistant." },
|
|
241
|
+
* { role: "user", content: "Email alice@example.com about invoice 42." },
|
|
242
|
+
* ]);
|
|
243
|
+
* // messages[1].content === "Email [EMAIL_0001] about invoice 42."
|
|
244
|
+
*
|
|
245
|
+
* const stream = await llm.streamText(messages); // "Sure, I wrote to [EMAIL_0001]."
|
|
246
|
+
* for await (const text of exchange.restoreIterable(stream)) {
|
|
247
|
+
* process.stdout.write(text); // "Sure, I wrote to alice@example.com."
|
|
248
|
+
* }
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
declare function createLlmGuard(options?: LlmGuardOptions): LlmGuard;
|
|
252
|
+
/**
|
|
253
|
+
* The parts of a language-model middleware this adapter implements. The
|
|
254
|
+
* shape matches `LanguageModelMiddleware` of the Vercel AI SDK structurally,
|
|
255
|
+
* so the object can be passed to `wrapLanguageModel()` without this library
|
|
256
|
+
* depending on the SDK.
|
|
257
|
+
*/
|
|
258
|
+
interface LanguageModelMiddlewareLike {
|
|
259
|
+
/** Sanitizes the prompt before it reaches the model. */
|
|
260
|
+
readonly transformParams: (input: {
|
|
261
|
+
readonly params: Record<string, unknown>;
|
|
262
|
+
}) => Promise<Record<string, unknown>>;
|
|
263
|
+
/** Restores tokens in a complete response. */
|
|
264
|
+
readonly wrapGenerate: (input: {
|
|
265
|
+
readonly doGenerate: () => PromiseLike<unknown>;
|
|
266
|
+
readonly params: Record<string, unknown>;
|
|
267
|
+
}) => Promise<unknown>;
|
|
268
|
+
/** Restores tokens in a streamed response. */
|
|
269
|
+
readonly wrapStream: (input: {
|
|
270
|
+
readonly doStream: () => PromiseLike<{
|
|
271
|
+
readonly stream: ReadableStream<unknown>;
|
|
272
|
+
}>;
|
|
273
|
+
readonly params: Record<string, unknown>;
|
|
274
|
+
}) => Promise<{
|
|
275
|
+
readonly stream: ReadableStream<unknown>;
|
|
276
|
+
}>;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Options accepted by {@link toLanguageModelMiddleware}.
|
|
280
|
+
*/
|
|
281
|
+
interface LanguageModelMiddlewareOptions {
|
|
282
|
+
/**
|
|
283
|
+
* Called with the account of every sanitized prompt: what was replaced,
|
|
284
|
+
* and above all what was passed on without inspection (`unscanned`). Throw
|
|
285
|
+
* from it to stop a request that must not be sent.
|
|
286
|
+
*/
|
|
287
|
+
readonly onSanitized?: (report: SanitizedMessages<unknown>) => void;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Adapt a guard to the language-model middleware shape of the Vercel AI SDK.
|
|
291
|
+
*
|
|
292
|
+
* The prompt (`params.prompt`) is sanitized on the way in. On the way out,
|
|
293
|
+
* tokens are restored in the result of `doGenerate()` and in the `text-delta`
|
|
294
|
+
* and `tool-call` parts of `doStream()`; the arguments of a tool call are
|
|
295
|
+
* restored as the JSON document they are. Partial tool input
|
|
296
|
+
* (`tool-input-delta`) is passed on as it is. Each call gets its own exchange.
|
|
297
|
+
*
|
|
298
|
+
* @param guard - The guard to adapt.
|
|
299
|
+
* @param options - A callback that receives the account of each sanitized prompt.
|
|
300
|
+
* @returns An object to pass as `middleware` to `wrapLanguageModel()`.
|
|
301
|
+
*
|
|
302
|
+
* @remarks
|
|
303
|
+
* The adapter is typed structurally and tested against stand-ins for the SDK,
|
|
304
|
+
* not against the SDK itself. Check it against the SDK version you use.
|
|
305
|
+
* `wrapGenerate` and `wrapStream` need the `params` object that
|
|
306
|
+
* `transformParams` returned (a copy made with spread syntax works too) and
|
|
307
|
+
* throw when they get another one: restoring nothing would go unnoticed.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* import { wrapLanguageModel } from "ai";
|
|
312
|
+
* import { createLlmGuard, toLanguageModelMiddleware } from "anonyma/ai";
|
|
313
|
+
*
|
|
314
|
+
* const model = wrapLanguageModel({
|
|
315
|
+
* model: baseModel,
|
|
316
|
+
* middleware: toLanguageModelMiddleware(createLlmGuard()),
|
|
317
|
+
* });
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
declare function toLanguageModelMiddleware(guard: LlmGuard, options?: LanguageModelMiddlewareOptions): LanguageModelMiddlewareLike;
|
|
321
|
+
|
|
322
|
+
export { type ChunkLens, KeyRule, type LanguageModelMiddlewareLike, type LanguageModelMiddlewareOptions, type LlmExchange, type LlmGuard, type LlmGuardOptions, type SanitizedMessages, contentLens, createLlmGuard, createRestoreStream, createRestoreTransformer, deltaLens, restoreChunks, restoreJsonText, textDeltaLens, toLanguageModelMiddleware };
|