@guhttps/veu 0.0.0-stage → 0.4.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/README.md +113 -2
- package/dist/addresses.d.ts +7 -0
- package/dist/addresses.js +43 -0
- package/dist/analyzer.d.ts +28 -0
- package/dist/analyzer.js +94 -0
- package/dist/anonymizer.d.ts +43 -0
- package/dist/anonymizer.js +173 -0
- package/dist/data.d.ts +16 -0
- package/dist/data.js +249 -0
- package/dist/entities.d.ts +20 -0
- package/dist/entities.js +34 -0
- package/dist/gateway.d.ts +136 -0
- package/dist/gateway.js +736 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +162 -0
- package/dist/integrations/ai-sdk.d.ts +38 -0
- package/dist/integrations/ai-sdk.js +197 -0
- package/dist/integrations/fetch.d.ts +25 -0
- package/dist/integrations/fetch.js +99 -0
- package/dist/navegador.d.ts +3 -0
- package/dist/navegador.js +4 -0
- package/dist/patterns.d.ts +43 -0
- package/dist/patterns.js +142 -0
- package/dist/persons.d.ts +24 -0
- package/dist/persons.js +231 -0
- package/dist/secrets.d.ts +17 -0
- package/dist/secrets.js +144 -0
- package/dist/sha256.d.ts +3 -0
- package/dist/sha256.js +68 -0
- package/dist/text.d.ts +24 -0
- package/dist/text.js +121 -0
- package/dist/validators.d.ts +30 -0
- package/dist/validators.js +267 -0
- package/dist/vault.d.ts +63 -0
- package/dist/vault.js +200 -0
- package/dist/veu.global.js +31 -0
- package/package.json +68 -4
package/dist/gateway.js
ADDED
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
export const INSTRUCTION = "Alguns dados pessoais deste conteúdo foram trocados por marcadores entre colchetes, como [PESSOA_1] ou " +
|
|
2
|
+
"[CPF_1]. Quando precisar se referir a esses dados, repita o marcador exatamente como está, inclusive os " +
|
|
3
|
+
"colchetes. Não tente adivinhar nem completar os valores reais.";
|
|
4
|
+
// Blocos assinados ou criptografados pelo provedor: alterar o texto invalidaria a assinatura.
|
|
5
|
+
const UNTOUCHED_TYPES = new Set(["thinking", "redacted_thinking"]);
|
|
6
|
+
const UNTOUCHED_KEYS = new Set(["encrypted_content", "signature"]);
|
|
7
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
8
|
+
/** Esconde os dados de todos os textos de uma requisição usando um único cofre. */
|
|
9
|
+
export class Masker {
|
|
10
|
+
anonymizer;
|
|
11
|
+
vault;
|
|
12
|
+
counts = {};
|
|
13
|
+
constructor(anonymizer, vault) {
|
|
14
|
+
this.anonymizer = anonymizer;
|
|
15
|
+
this.vault = vault;
|
|
16
|
+
}
|
|
17
|
+
text(value) {
|
|
18
|
+
if (typeof value !== "string" || !value)
|
|
19
|
+
return value;
|
|
20
|
+
const result = this.anonymizer.anonymize(value, this.vault);
|
|
21
|
+
for (const [label, count] of Object.entries(result.counts()))
|
|
22
|
+
this.counts[label] = (this.counts[label] ?? 0) + count;
|
|
23
|
+
return result.text;
|
|
24
|
+
}
|
|
25
|
+
/** Argumentos de ferramentas chegam como texto JSON: esconde os valores sem quebrar o JSON. */
|
|
26
|
+
jsonString(value) {
|
|
27
|
+
if (typeof value !== "string" || !value)
|
|
28
|
+
return value;
|
|
29
|
+
try {
|
|
30
|
+
return JSON.stringify(this.obj(JSON.parse(value)));
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return this.text(value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
obj(value) {
|
|
37
|
+
if (typeof value === "string")
|
|
38
|
+
return this.text(value);
|
|
39
|
+
if (Array.isArray(value))
|
|
40
|
+
return value.map((item) => this.obj(item));
|
|
41
|
+
if (isObject(value))
|
|
42
|
+
return Object.fromEntries(Object.entries(value).map(([k, item]) => [k, this.obj(item)]));
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function restoreObj(value, vault) {
|
|
47
|
+
if (typeof value === "string")
|
|
48
|
+
return vault.restore(value);
|
|
49
|
+
if (Array.isArray(value))
|
|
50
|
+
return value.map((item) => restoreObj(item, vault));
|
|
51
|
+
if (isObject(value)) {
|
|
52
|
+
if (UNTOUCHED_TYPES.has(value.type))
|
|
53
|
+
return value;
|
|
54
|
+
const restored = {};
|
|
55
|
+
for (const [key, item] of Object.entries(value)) {
|
|
56
|
+
if (UNTOUCHED_KEYS.has(key))
|
|
57
|
+
restored[key] = item;
|
|
58
|
+
else if ((key === "arguments" || key === "partial_json") && typeof item === "string")
|
|
59
|
+
restored[key] = restoreJsonString(item, vault);
|
|
60
|
+
else
|
|
61
|
+
restored[key] = restoreObj(item, vault);
|
|
62
|
+
}
|
|
63
|
+
return restored;
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
export function restoreJsonString(value, vault) {
|
|
68
|
+
try {
|
|
69
|
+
return JSON.stringify(restoreObj(JSON.parse(value), vault));
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return vault.restore(value, true);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Um restaurador por campo de texto do stream (cada escolha, bloco ou argumento de ferramenta). */
|
|
76
|
+
export class StreamState {
|
|
77
|
+
vault;
|
|
78
|
+
meta = {};
|
|
79
|
+
restorers = new Map();
|
|
80
|
+
constructor(vault) {
|
|
81
|
+
this.vault = vault;
|
|
82
|
+
}
|
|
83
|
+
feed(key, text, jsonEscape = false) {
|
|
84
|
+
const id = JSON.stringify(key);
|
|
85
|
+
let entry = this.restorers.get(id);
|
|
86
|
+
if (!entry) {
|
|
87
|
+
entry = { key, restorer: this.vault.restorer(jsonEscape) };
|
|
88
|
+
this.restorers.set(id, entry);
|
|
89
|
+
}
|
|
90
|
+
return entry.restorer.feed(text);
|
|
91
|
+
}
|
|
92
|
+
flush(key) {
|
|
93
|
+
const id = JSON.stringify(key);
|
|
94
|
+
const entry = this.restorers.get(id);
|
|
95
|
+
this.restorers.delete(id);
|
|
96
|
+
return entry ? entry.restorer.flush() : "";
|
|
97
|
+
}
|
|
98
|
+
pendingKeys() {
|
|
99
|
+
return [...this.restorers.values()].filter((entry) => entry.restorer.pending).map((entry) => entry.key);
|
|
100
|
+
}
|
|
101
|
+
restore(value) {
|
|
102
|
+
return restoreObj(value, this.vault);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export class Adapter {
|
|
106
|
+
restoresOutput = true;
|
|
107
|
+
instruct(_payload, _instruction) { }
|
|
108
|
+
isStream(payload, _path) {
|
|
109
|
+
return payload.stream === true;
|
|
110
|
+
}
|
|
111
|
+
errorBody(message) {
|
|
112
|
+
return { error: { message, type: "veu_bloqueado", code: "dados_pessoais_bloqueados" } };
|
|
113
|
+
}
|
|
114
|
+
streamEvent(name, data, state) {
|
|
115
|
+
return [[name, state.restore(data)]];
|
|
116
|
+
}
|
|
117
|
+
streamFinish(_state) {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const trimSlash = (path) => path.replace(/\/+$/, "");
|
|
122
|
+
function maskContent(content, masker) {
|
|
123
|
+
if (typeof content === "string")
|
|
124
|
+
return masker.text(content);
|
|
125
|
+
if (Array.isArray(content)) {
|
|
126
|
+
for (const part of content) {
|
|
127
|
+
if (isObject(part) && ["text", "input_text", "output_text"].includes(part.type))
|
|
128
|
+
part.text = masker.text(part.text);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return content;
|
|
132
|
+
}
|
|
133
|
+
const template = (data, keys) => Object.fromEntries(keys.filter((key) => key in data).map((key) => [key, data[key]]));
|
|
134
|
+
export class OpenAIChatAdapter extends Adapter {
|
|
135
|
+
name = "openai-chat";
|
|
136
|
+
matches(method, path) {
|
|
137
|
+
return method === "POST" && trimSlash(path).endsWith("/chat/completions");
|
|
138
|
+
}
|
|
139
|
+
mask(payload, masker) {
|
|
140
|
+
for (const message of payload.messages ?? []) {
|
|
141
|
+
if (!isObject(message))
|
|
142
|
+
continue;
|
|
143
|
+
message.content = maskContent(message.content, masker);
|
|
144
|
+
for (const call of message.tool_calls ?? []) {
|
|
145
|
+
if (isObject(call?.function))
|
|
146
|
+
call.function.arguments = masker.jsonString(call.function.arguments);
|
|
147
|
+
}
|
|
148
|
+
if (isObject(message.function_call))
|
|
149
|
+
message.function_call.arguments = masker.jsonString(message.function_call.arguments);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
instruct(payload, instruction) {
|
|
153
|
+
payload.messages ??= [];
|
|
154
|
+
const first = payload.messages[0];
|
|
155
|
+
if (isObject(first) && (first.role === "system" || first.role === "developer") && typeof first.content === "string") {
|
|
156
|
+
first.content = `${first.content}\n\n${instruction}`;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
payload.messages.unshift({ role: "system", content: instruction });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
streamEvent(name, data, state) {
|
|
163
|
+
if (data === "[DONE]")
|
|
164
|
+
return [...this.streamFinish(state), [name, data]];
|
|
165
|
+
if (!isObject(data))
|
|
166
|
+
return [[name, data]];
|
|
167
|
+
state.meta.template = template(data, ["id", "object", "created", "model"]);
|
|
168
|
+
for (const choice of data.choices ?? []) {
|
|
169
|
+
const index = choice.index ?? 0;
|
|
170
|
+
const delta = choice.delta;
|
|
171
|
+
if (!isObject(delta))
|
|
172
|
+
continue;
|
|
173
|
+
for (const field of ["content", "reasoning_content", "reasoning", "refusal"]) {
|
|
174
|
+
if (typeof delta[field] === "string")
|
|
175
|
+
delta[field] = state.feed(["c", index, field], delta[field]);
|
|
176
|
+
}
|
|
177
|
+
for (const call of delta.tool_calls ?? []) {
|
|
178
|
+
if (typeof call?.function?.arguments === "string") {
|
|
179
|
+
call.function.arguments = state.feed(["t", index, call.index ?? 0], call.function.arguments, true);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (choice.finish_reason)
|
|
183
|
+
this.flushChoice(index, delta, state);
|
|
184
|
+
}
|
|
185
|
+
return [[name, data]];
|
|
186
|
+
}
|
|
187
|
+
flushChoice(index, delta, state) {
|
|
188
|
+
for (const key of state.pendingKeys()) {
|
|
189
|
+
if (key[1] !== index)
|
|
190
|
+
continue;
|
|
191
|
+
const text = state.flush(key);
|
|
192
|
+
if (!text)
|
|
193
|
+
continue;
|
|
194
|
+
if (key[0] === "c") {
|
|
195
|
+
delta[key[2]] = (delta[key[2]] ?? "") + text;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
delta.tool_calls ??= [];
|
|
199
|
+
let call = delta.tool_calls.find((c) => (c.index ?? 0) === key[2]);
|
|
200
|
+
if (!call) {
|
|
201
|
+
call = { index: key[2], function: { arguments: "" } };
|
|
202
|
+
delta.tool_calls.push(call);
|
|
203
|
+
}
|
|
204
|
+
call.function ??= {};
|
|
205
|
+
call.function.arguments = (call.function.arguments ?? "") + text;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
streamFinish(state) {
|
|
210
|
+
const pending = state.pendingKeys();
|
|
211
|
+
if (!pending.length)
|
|
212
|
+
return [];
|
|
213
|
+
const chunk = { ...(state.meta.template ?? { object: "chat.completion.chunk" }) };
|
|
214
|
+
const choices = new Map();
|
|
215
|
+
for (const key of pending)
|
|
216
|
+
if (!choices.has(key[1]))
|
|
217
|
+
choices.set(key[1], { index: key[1], delta: {}, finish_reason: null });
|
|
218
|
+
for (const [index, choice] of choices)
|
|
219
|
+
this.flushChoice(index, choice.delta, state);
|
|
220
|
+
chunk.choices = [...choices.values()];
|
|
221
|
+
return [[null, chunk]];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
export class OpenAICompletionsAdapter extends Adapter {
|
|
225
|
+
name = "openai-completions";
|
|
226
|
+
matches(method, path) {
|
|
227
|
+
const clean = trimSlash(path);
|
|
228
|
+
return method === "POST" && clean.endsWith("/completions") && !clean.endsWith("/chat/completions");
|
|
229
|
+
}
|
|
230
|
+
mask(payload, masker) {
|
|
231
|
+
if (typeof payload.prompt === "string")
|
|
232
|
+
payload.prompt = masker.text(payload.prompt);
|
|
233
|
+
else if (Array.isArray(payload.prompt) && payload.prompt.every((p) => typeof p === "string")) {
|
|
234
|
+
payload.prompt = payload.prompt.map((p) => masker.text(p));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
streamEvent(name, data, state) {
|
|
238
|
+
if (data === "[DONE]")
|
|
239
|
+
return [...this.streamFinish(state), [name, data]];
|
|
240
|
+
if (!isObject(data))
|
|
241
|
+
return [[name, data]];
|
|
242
|
+
state.meta.template = template(data, ["id", "object", "created", "model"]);
|
|
243
|
+
for (const choice of data.choices ?? []) {
|
|
244
|
+
const index = choice.index ?? 0;
|
|
245
|
+
if (typeof choice.text === "string")
|
|
246
|
+
choice.text = state.feed(["c", index], choice.text);
|
|
247
|
+
if (choice.finish_reason)
|
|
248
|
+
choice.text = (choice.text ?? "") + state.flush(["c", index]);
|
|
249
|
+
}
|
|
250
|
+
return [[name, data]];
|
|
251
|
+
}
|
|
252
|
+
streamFinish(state) {
|
|
253
|
+
const pending = state.pendingKeys();
|
|
254
|
+
if (!pending.length)
|
|
255
|
+
return [];
|
|
256
|
+
const chunk = { ...(state.meta.template ?? { object: "text_completion" }) };
|
|
257
|
+
chunk.choices = pending.map((key) => ({ index: key[1], text: state.flush(key), finish_reason: null }));
|
|
258
|
+
return [[null, chunk]];
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
export class OpenAIEmbeddingsAdapter extends Adapter {
|
|
262
|
+
name = "openai-embeddings";
|
|
263
|
+
restoresOutput = false;
|
|
264
|
+
matches(method, path) {
|
|
265
|
+
return method === "POST" && trimSlash(path).endsWith("/embeddings");
|
|
266
|
+
}
|
|
267
|
+
mask(payload, masker) {
|
|
268
|
+
if (typeof payload.input === "string")
|
|
269
|
+
payload.input = masker.text(payload.input);
|
|
270
|
+
else if (Array.isArray(payload.input) && payload.input.every((p) => typeof p === "string")) {
|
|
271
|
+
payload.input = payload.input.map((p) => masker.text(p));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
export class OpenAIResponsesAdapter extends Adapter {
|
|
276
|
+
name = "openai-responses";
|
|
277
|
+
matches(method, path) {
|
|
278
|
+
return method === "POST" && trimSlash(path).endsWith("/responses");
|
|
279
|
+
}
|
|
280
|
+
mask(payload, masker) {
|
|
281
|
+
if (typeof payload.instructions === "string")
|
|
282
|
+
payload.instructions = masker.text(payload.instructions);
|
|
283
|
+
if (typeof payload.input === "string") {
|
|
284
|
+
payload.input = masker.text(payload.input);
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
for (const item of payload.input ?? []) {
|
|
288
|
+
if (!isObject(item) || item.type === "reasoning")
|
|
289
|
+
continue;
|
|
290
|
+
if ("content" in item)
|
|
291
|
+
item.content = maskContent(item.content, masker);
|
|
292
|
+
if (item.type === "function_call")
|
|
293
|
+
item.arguments = masker.jsonString(item.arguments);
|
|
294
|
+
else if (item.type === "function_call_output" && typeof item.output === "string")
|
|
295
|
+
item.output = masker.text(item.output);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
instruct(payload, instruction) {
|
|
299
|
+
const current = payload.instructions;
|
|
300
|
+
payload.instructions = typeof current === "string" && current ? `${current}\n\n${instruction}` : instruction;
|
|
301
|
+
}
|
|
302
|
+
streamEvent(name, data, state) {
|
|
303
|
+
if (!isObject(data))
|
|
304
|
+
return [[name, data]];
|
|
305
|
+
const kind = data.type;
|
|
306
|
+
if (kind === "response.output_text.delta") {
|
|
307
|
+
data.delta = state.feed(["o", data.output_index ?? 0, data.content_index ?? 0], data.delta ?? "");
|
|
308
|
+
return [[name, data]];
|
|
309
|
+
}
|
|
310
|
+
if (kind === "response.function_call_arguments.delta") {
|
|
311
|
+
data.delta = state.feed(["f", data.output_index ?? 0], data.delta ?? "", true);
|
|
312
|
+
return [[name, data]];
|
|
313
|
+
}
|
|
314
|
+
if (kind === "response.output_text.done" || kind === "response.function_call_arguments.done") {
|
|
315
|
+
const text = kind === "response.output_text.done";
|
|
316
|
+
const key = text ? ["o", data.output_index ?? 0, data.content_index ?? 0] : ["f", data.output_index ?? 0];
|
|
317
|
+
const deltaKind = text ? "response.output_text.delta" : "response.function_call_arguments.delta";
|
|
318
|
+
const events = [];
|
|
319
|
+
const tail = state.flush(key);
|
|
320
|
+
if (tail) {
|
|
321
|
+
const extra = template(data, ["item_id", "output_index", "content_index"]);
|
|
322
|
+
events.push([name ? deltaKind : null, { type: deltaKind, ...extra, delta: tail }]);
|
|
323
|
+
}
|
|
324
|
+
events.push([name, state.restore(data)]);
|
|
325
|
+
return events;
|
|
326
|
+
}
|
|
327
|
+
if (typeof kind === "string" && kind.includes("reasoning"))
|
|
328
|
+
return [[name, data]];
|
|
329
|
+
return [[name, state.restore(data)]];
|
|
330
|
+
}
|
|
331
|
+
streamFinish(state) {
|
|
332
|
+
const events = [];
|
|
333
|
+
for (const key of state.pendingKeys()) {
|
|
334
|
+
const tail = state.flush(key);
|
|
335
|
+
if (!tail)
|
|
336
|
+
continue;
|
|
337
|
+
if (key[0] === "o") {
|
|
338
|
+
events.push([null, { type: "response.output_text.delta", output_index: key[1], content_index: key[2], delta: tail }]);
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
events.push([null, { type: "response.function_call_arguments.delta", output_index: key[1], delta: tail }]);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return events;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
export class AnthropicMessagesAdapter extends Adapter {
|
|
348
|
+
name = "anthropic-messages";
|
|
349
|
+
matches(method, path) {
|
|
350
|
+
const clean = trimSlash(path);
|
|
351
|
+
return method === "POST" && (clean.endsWith("/messages") || clean.endsWith("/messages/count_tokens"));
|
|
352
|
+
}
|
|
353
|
+
maskBlocks(blocks, masker) {
|
|
354
|
+
if (typeof blocks === "string")
|
|
355
|
+
return masker.text(blocks);
|
|
356
|
+
if (!Array.isArray(blocks))
|
|
357
|
+
return blocks;
|
|
358
|
+
for (const block of blocks) {
|
|
359
|
+
if (!isObject(block) || UNTOUCHED_TYPES.has(block.type))
|
|
360
|
+
continue;
|
|
361
|
+
if (block.type === "text")
|
|
362
|
+
block.text = masker.text(block.text);
|
|
363
|
+
else if (block.type === "tool_use" || block.type === "server_tool_use")
|
|
364
|
+
block.input = masker.obj(block.input);
|
|
365
|
+
else if (block.type === "tool_result")
|
|
366
|
+
block.content = this.maskBlocks(block.content, masker);
|
|
367
|
+
else if (block.type === "document" && isObject(block.source)) {
|
|
368
|
+
if (block.source.type === "text")
|
|
369
|
+
block.source.data = masker.text(block.source.data);
|
|
370
|
+
else if (block.source.type === "content")
|
|
371
|
+
block.source.content = this.maskBlocks(block.source.content, masker);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return blocks;
|
|
375
|
+
}
|
|
376
|
+
mask(payload, masker) {
|
|
377
|
+
if ("system" in payload)
|
|
378
|
+
payload.system = this.maskBlocks(payload.system, masker);
|
|
379
|
+
for (const message of payload.messages ?? [])
|
|
380
|
+
if (isObject(message))
|
|
381
|
+
message.content = this.maskBlocks(message.content, masker);
|
|
382
|
+
}
|
|
383
|
+
instruct(payload, instruction) {
|
|
384
|
+
if (Array.isArray(payload.system))
|
|
385
|
+
payload.system.push({ type: "text", text: instruction });
|
|
386
|
+
else if (typeof payload.system === "string" && payload.system)
|
|
387
|
+
payload.system = `${payload.system}\n\n${instruction}`;
|
|
388
|
+
else
|
|
389
|
+
payload.system = instruction;
|
|
390
|
+
}
|
|
391
|
+
errorBody(message) {
|
|
392
|
+
return { type: "error", error: { type: "invalid_request_error", message } };
|
|
393
|
+
}
|
|
394
|
+
streamEvent(name, data, state) {
|
|
395
|
+
if (!isObject(data))
|
|
396
|
+
return [[name, data]];
|
|
397
|
+
const kind = data.type;
|
|
398
|
+
if (kind === "content_block_start")
|
|
399
|
+
return [[name, state.restore(data)]];
|
|
400
|
+
if (kind === "content_block_delta") {
|
|
401
|
+
const index = data.index ?? 0;
|
|
402
|
+
const delta = data.delta ?? {};
|
|
403
|
+
if (delta.type === "text_delta")
|
|
404
|
+
delta.text = state.feed(["b", index, "text_delta"], delta.text ?? "");
|
|
405
|
+
else if (delta.type === "input_json_delta") {
|
|
406
|
+
delta.partial_json = state.feed(["b", index, "input_json_delta"], delta.partial_json ?? "", true);
|
|
407
|
+
}
|
|
408
|
+
return [[name, data]];
|
|
409
|
+
}
|
|
410
|
+
if (kind === "content_block_stop")
|
|
411
|
+
return [...this.flushBlock(data.index ?? 0, state, name !== null), [name, data]];
|
|
412
|
+
if (kind === "message_stop") {
|
|
413
|
+
const events = state.pendingKeys().flatMap((key) => this.flushBlock(key[1], state, name !== null));
|
|
414
|
+
return [...events, [name, data]];
|
|
415
|
+
}
|
|
416
|
+
return [[name, state.restore(data)]];
|
|
417
|
+
}
|
|
418
|
+
flushBlock(index, state, withNames) {
|
|
419
|
+
const events = [];
|
|
420
|
+
for (const key of state.pendingKeys()) {
|
|
421
|
+
if (key[1] !== index)
|
|
422
|
+
continue;
|
|
423
|
+
const tail = state.flush(key);
|
|
424
|
+
if (!tail)
|
|
425
|
+
continue;
|
|
426
|
+
const field = key[2] === "text_delta" ? "text" : "partial_json";
|
|
427
|
+
events.push([withNames ? "content_block_delta" : null, { type: "content_block_delta", index, delta: { type: key[2], [field]: tail } }]);
|
|
428
|
+
}
|
|
429
|
+
return events;
|
|
430
|
+
}
|
|
431
|
+
streamFinish(state) {
|
|
432
|
+
return state.pendingKeys().flatMap((key) => this.flushBlock(key[1], state, true));
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
export class GeminiAdapter extends Adapter {
|
|
436
|
+
name = "gemini";
|
|
437
|
+
methods = [":generateContent", ":streamGenerateContent", ":countTokens", ":embedContent", ":batchEmbedContents"];
|
|
438
|
+
matches(method, path) {
|
|
439
|
+
return method === "POST" && this.methods.some((suffix) => trimSlash(path).endsWith(suffix));
|
|
440
|
+
}
|
|
441
|
+
isStream(_payload, path) {
|
|
442
|
+
return path.includes(":streamGenerateContent");
|
|
443
|
+
}
|
|
444
|
+
maskContent(content, masker) {
|
|
445
|
+
if (!isObject(content))
|
|
446
|
+
return;
|
|
447
|
+
for (const part of content.parts ?? []) {
|
|
448
|
+
if (!isObject(part) || part.thought)
|
|
449
|
+
continue;
|
|
450
|
+
if (typeof part.text === "string")
|
|
451
|
+
part.text = masker.text(part.text);
|
|
452
|
+
for (const key of ["functionCall", "function_call"])
|
|
453
|
+
if (isObject(part[key]))
|
|
454
|
+
part[key].args = masker.obj(part[key].args);
|
|
455
|
+
for (const key of ["functionResponse", "function_response"]) {
|
|
456
|
+
if (isObject(part[key]))
|
|
457
|
+
part[key].response = masker.obj(part[key].response);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
mask(payload, masker) {
|
|
462
|
+
for (const content of payload.contents ?? [])
|
|
463
|
+
this.maskContent(content, masker);
|
|
464
|
+
for (const key of ["systemInstruction", "system_instruction", "content"])
|
|
465
|
+
this.maskContent(payload[key], masker);
|
|
466
|
+
for (const request of payload.requests ?? [])
|
|
467
|
+
if (isObject(request))
|
|
468
|
+
this.maskContent(request.content, masker);
|
|
469
|
+
if (isObject(payload.generateContentRequest))
|
|
470
|
+
this.mask(payload.generateContentRequest, masker);
|
|
471
|
+
}
|
|
472
|
+
instruct(payload, instruction) {
|
|
473
|
+
if (!("contents" in payload))
|
|
474
|
+
return;
|
|
475
|
+
const key = "system_instruction" in payload ? "system_instruction" : "systemInstruction";
|
|
476
|
+
if (!isObject(payload[key]))
|
|
477
|
+
payload[key] = { parts: [] };
|
|
478
|
+
(payload[key].parts ??= []).push({ text: instruction });
|
|
479
|
+
}
|
|
480
|
+
errorBody(message) {
|
|
481
|
+
return { error: { code: 400, message, status: "INVALID_ARGUMENT" } };
|
|
482
|
+
}
|
|
483
|
+
streamEvent(name, data, state) {
|
|
484
|
+
if (!isObject(data))
|
|
485
|
+
return [[name, data]];
|
|
486
|
+
state.meta.template = template(data, ["modelVersion", "responseId"]);
|
|
487
|
+
for (const candidate of data.candidates ?? []) {
|
|
488
|
+
const index = candidate.index ?? 0;
|
|
489
|
+
let lastText = null;
|
|
490
|
+
if (isObject(candidate.content)) {
|
|
491
|
+
for (const part of candidate.content.parts ?? []) {
|
|
492
|
+
if (!isObject(part) || part.thought)
|
|
493
|
+
continue;
|
|
494
|
+
if (typeof part.text === "string") {
|
|
495
|
+
part.text = state.feed(["g", index], part.text);
|
|
496
|
+
lastText = part;
|
|
497
|
+
}
|
|
498
|
+
for (const key of ["functionCall", "function_call"])
|
|
499
|
+
if (isObject(part[key]))
|
|
500
|
+
part[key] = state.restore(part[key]);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (candidate.finishReason) {
|
|
504
|
+
const tail = state.flush(["g", index]);
|
|
505
|
+
if (tail) {
|
|
506
|
+
if (lastText === null) {
|
|
507
|
+
candidate.content ??= { role: "model", parts: [] };
|
|
508
|
+
lastText = { text: "" };
|
|
509
|
+
(candidate.content.parts ??= []).push(lastText);
|
|
510
|
+
}
|
|
511
|
+
lastText.text += tail;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return [[name, data]];
|
|
516
|
+
}
|
|
517
|
+
streamFinish(state) {
|
|
518
|
+
const pending = state.pendingKeys();
|
|
519
|
+
if (!pending.length)
|
|
520
|
+
return [];
|
|
521
|
+
const chunk = structuredClone(state.meta.template ?? {});
|
|
522
|
+
chunk.candidates = pending.map((key) => ({ index: key[1], content: { role: "model", parts: [{ text: state.flush(key) }] } }));
|
|
523
|
+
return [[null, chunk]];
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
export class OllamaAdapter extends Adapter {
|
|
527
|
+
name = "ollama";
|
|
528
|
+
paths = ["/api/chat", "/api/generate", "/api/embed", "/api/embeddings"];
|
|
529
|
+
matches(method, path) {
|
|
530
|
+
return method === "POST" && this.paths.some((suffix) => trimSlash(path).endsWith(suffix));
|
|
531
|
+
}
|
|
532
|
+
isStream(payload, path) {
|
|
533
|
+
const clean = trimSlash(path);
|
|
534
|
+
return (clean.endsWith("/api/chat") || clean.endsWith("/api/generate")) && payload.stream !== false;
|
|
535
|
+
}
|
|
536
|
+
mask(payload, masker) {
|
|
537
|
+
for (const message of payload.messages ?? []) {
|
|
538
|
+
if (!isObject(message))
|
|
539
|
+
continue;
|
|
540
|
+
message.content = masker.text(message.content);
|
|
541
|
+
for (const call of message.tool_calls ?? [])
|
|
542
|
+
if (isObject(call?.function))
|
|
543
|
+
call.function.arguments = masker.obj(call.function.arguments);
|
|
544
|
+
}
|
|
545
|
+
for (const key of ["prompt", "system"])
|
|
546
|
+
if (typeof payload[key] === "string")
|
|
547
|
+
payload[key] = masker.text(payload[key]);
|
|
548
|
+
if (typeof payload.input === "string")
|
|
549
|
+
payload.input = masker.text(payload.input);
|
|
550
|
+
else if (Array.isArray(payload.input) && payload.input.every((p) => typeof p === "string")) {
|
|
551
|
+
payload.input = payload.input.map((p) => masker.text(p));
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
instruct(payload, instruction) {
|
|
555
|
+
if ("messages" in payload) {
|
|
556
|
+
const first = payload.messages[0];
|
|
557
|
+
if (isObject(first) && first.role === "system")
|
|
558
|
+
first.content = `${first.content ?? ""}\n\n${instruction}`;
|
|
559
|
+
else
|
|
560
|
+
payload.messages.unshift({ role: "system", content: instruction });
|
|
561
|
+
}
|
|
562
|
+
else if ("prompt" in payload) {
|
|
563
|
+
payload.system = payload.system ? `${payload.system}\n\n${instruction}` : instruction;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
errorBody(message) {
|
|
567
|
+
return { error: message };
|
|
568
|
+
}
|
|
569
|
+
streamEvent(name, data, state) {
|
|
570
|
+
if (!isObject(data))
|
|
571
|
+
return [[name, data]];
|
|
572
|
+
if (isObject(data.message)) {
|
|
573
|
+
if (typeof data.message.content === "string")
|
|
574
|
+
data.message.content = state.feed(["chat"], data.message.content);
|
|
575
|
+
if (data.message.tool_calls)
|
|
576
|
+
data.message.tool_calls = state.restore(data.message.tool_calls);
|
|
577
|
+
if (data.done)
|
|
578
|
+
data.message.content = (data.message.content ?? "") + state.flush(["chat"]);
|
|
579
|
+
}
|
|
580
|
+
if (typeof data.response === "string") {
|
|
581
|
+
data.response = state.feed(["generate"], data.response);
|
|
582
|
+
if (data.done)
|
|
583
|
+
data.response += state.flush(["generate"]);
|
|
584
|
+
}
|
|
585
|
+
return [[name, data]];
|
|
586
|
+
}
|
|
587
|
+
streamFinish(state) {
|
|
588
|
+
return state.pendingKeys().map((key) => {
|
|
589
|
+
const tail = state.flush(key);
|
|
590
|
+
return key[0] === "chat"
|
|
591
|
+
? [null, { message: { role: "assistant", content: tail }, done: false }]
|
|
592
|
+
: [null, { response: tail, done: false }];
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
export const ADAPTERS = [
|
|
597
|
+
new OpenAIChatAdapter(), new OpenAIResponsesAdapter(), new OpenAIEmbeddingsAdapter(), new OpenAICompletionsAdapter(),
|
|
598
|
+
new AnthropicMessagesAdapter(), new GeminiAdapter(), new OllamaAdapter(),
|
|
599
|
+
];
|
|
600
|
+
export function detectAdapter(method, path) {
|
|
601
|
+
const clean = path.split("?")[0];
|
|
602
|
+
return ADAPTERS.find((adapter) => adapter.matches(method.toUpperCase(), clean)) ?? null;
|
|
603
|
+
}
|
|
604
|
+
export function encodeEvent(name, data, extra = []) {
|
|
605
|
+
const lines = [...extra];
|
|
606
|
+
if (name)
|
|
607
|
+
lines.push(`event: ${name}`);
|
|
608
|
+
const payload = typeof data === "string" ? data : JSON.stringify(data);
|
|
609
|
+
for (const line of payload.split("\n"))
|
|
610
|
+
lines.push(`data: ${line}`);
|
|
611
|
+
return `${lines.join("\n")}\n\n`;
|
|
612
|
+
}
|
|
613
|
+
/** Recebe pedaços de um stream SSE e devolve o texto com os eventos transformados. */
|
|
614
|
+
export class SSETransformer {
|
|
615
|
+
decoder = new TextDecoder();
|
|
616
|
+
buffer = "";
|
|
617
|
+
pendingCR = false;
|
|
618
|
+
process;
|
|
619
|
+
finish;
|
|
620
|
+
constructor(process, finish) {
|
|
621
|
+
this.process = process;
|
|
622
|
+
this.finish = finish;
|
|
623
|
+
}
|
|
624
|
+
feed(chunk) {
|
|
625
|
+
let text = this.decoder.decode(chunk, { stream: true });
|
|
626
|
+
if (this.pendingCR) {
|
|
627
|
+
text = `\r${text}`;
|
|
628
|
+
this.pendingCR = false;
|
|
629
|
+
}
|
|
630
|
+
if (text.endsWith("\r")) {
|
|
631
|
+
text = text.slice(0, -1);
|
|
632
|
+
this.pendingCR = true;
|
|
633
|
+
}
|
|
634
|
+
this.buffer += text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
635
|
+
let output = "";
|
|
636
|
+
for (let index = this.buffer.indexOf("\n\n"); index !== -1; index = this.buffer.indexOf("\n\n")) {
|
|
637
|
+
const block = this.buffer.slice(0, index);
|
|
638
|
+
this.buffer = this.buffer.slice(index + 2);
|
|
639
|
+
output += this.handle(block);
|
|
640
|
+
}
|
|
641
|
+
return output;
|
|
642
|
+
}
|
|
643
|
+
close() {
|
|
644
|
+
this.buffer += this.decoder.decode().replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
645
|
+
let output = this.buffer.trim() ? this.handle(this.buffer.replace(/^\n+|\n+$/g, "")) : "";
|
|
646
|
+
this.buffer = "";
|
|
647
|
+
for (const [name, data] of this.finish())
|
|
648
|
+
output += encodeEvent(name, data);
|
|
649
|
+
return output;
|
|
650
|
+
}
|
|
651
|
+
handle(block) {
|
|
652
|
+
let name = null;
|
|
653
|
+
const dataLines = [];
|
|
654
|
+
const extra = [];
|
|
655
|
+
for (const line of block.split("\n")) {
|
|
656
|
+
if (line.startsWith(":") || !line) {
|
|
657
|
+
extra.push(line);
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
const colon = line.indexOf(":");
|
|
661
|
+
const field = colon === -1 ? line : line.slice(0, colon);
|
|
662
|
+
let value = colon === -1 ? "" : line.slice(colon + 1);
|
|
663
|
+
if (value.startsWith(" "))
|
|
664
|
+
value = value.slice(1);
|
|
665
|
+
if (field === "event")
|
|
666
|
+
name = value;
|
|
667
|
+
else if (field === "data")
|
|
668
|
+
dataLines.push(value);
|
|
669
|
+
else
|
|
670
|
+
extra.push(line);
|
|
671
|
+
}
|
|
672
|
+
if (!dataLines.length)
|
|
673
|
+
return `${block}\n\n`;
|
|
674
|
+
const raw = dataLines.join("\n");
|
|
675
|
+
let data;
|
|
676
|
+
try {
|
|
677
|
+
data = JSON.parse(raw);
|
|
678
|
+
}
|
|
679
|
+
catch {
|
|
680
|
+
data = raw;
|
|
681
|
+
}
|
|
682
|
+
const events = this.process(name, data);
|
|
683
|
+
if (!events.length)
|
|
684
|
+
return "";
|
|
685
|
+
return events.map(([eventName, eventData], i) => encodeEvent(eventName, eventData, i === events.length - 1 ? extra : [])).join("");
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
/** O mesmo para streams de JSON por linha (API nativa do Ollama). */
|
|
689
|
+
export class NDJSONTransformer {
|
|
690
|
+
decoder = new TextDecoder();
|
|
691
|
+
buffer = "";
|
|
692
|
+
process;
|
|
693
|
+
finish;
|
|
694
|
+
constructor(process, finish) {
|
|
695
|
+
this.process = process;
|
|
696
|
+
this.finish = finish;
|
|
697
|
+
}
|
|
698
|
+
feed(chunk) {
|
|
699
|
+
this.buffer += this.decoder.decode(chunk, { stream: true });
|
|
700
|
+
const lines = this.buffer.split("\n");
|
|
701
|
+
this.buffer = lines.pop() ?? "";
|
|
702
|
+
return lines.map((line) => this.handle(line)).join("");
|
|
703
|
+
}
|
|
704
|
+
close() {
|
|
705
|
+
this.buffer += this.decoder.decode();
|
|
706
|
+
let output = this.buffer.trim() ? this.handle(this.buffer) : "";
|
|
707
|
+
this.buffer = "";
|
|
708
|
+
for (const [, data] of this.finish())
|
|
709
|
+
output += NDJSONTransformer.encode(data);
|
|
710
|
+
return output;
|
|
711
|
+
}
|
|
712
|
+
static encode(data) {
|
|
713
|
+
return `${typeof data === "string" ? data : JSON.stringify(data)}\n`;
|
|
714
|
+
}
|
|
715
|
+
handle(raw) {
|
|
716
|
+
const line = raw.replace(/\r$/, "");
|
|
717
|
+
if (!line.trim())
|
|
718
|
+
return `${line}\n`;
|
|
719
|
+
let data;
|
|
720
|
+
try {
|
|
721
|
+
data = JSON.parse(line);
|
|
722
|
+
}
|
|
723
|
+
catch {
|
|
724
|
+
return `${line}\n`;
|
|
725
|
+
}
|
|
726
|
+
return this.process(null, data).map(([, eventData]) => NDJSONTransformer.encode(eventData)).join("");
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
export function isStreaming(contentType) {
|
|
730
|
+
return contentType.includes("text/event-stream") || contentType.includes("ndjson");
|
|
731
|
+
}
|
|
732
|
+
export function streamTransformer(adapter, vault, contentType) {
|
|
733
|
+
const state = new StreamState(vault);
|
|
734
|
+
const Transformer = contentType.includes("ndjson") ? NDJSONTransformer : SSETransformer;
|
|
735
|
+
return new Transformer((name, data) => adapter.streamEvent(name, data, state), () => adapter.streamFinish(state));
|
|
736
|
+
}
|