ai.matey.frontend 0.2.0 → 0.3.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 +40 -0
- package/dist/cjs/adapters/anthropic.js +156 -16
- package/dist/cjs/adapters/anthropic.js.map +1 -1
- package/dist/cjs/adapters/chrome-ai.js +58 -0
- package/dist/cjs/adapters/chrome-ai.js.map +1 -1
- package/dist/cjs/adapters/gemini.js +57 -0
- package/dist/cjs/adapters/gemini.js.map +1 -1
- package/dist/cjs/adapters/generic.js +56 -4
- package/dist/cjs/adapters/generic.js.map +1 -1
- package/dist/cjs/adapters/mistral.js +59 -0
- package/dist/cjs/adapters/mistral.js.map +1 -1
- package/dist/cjs/adapters/ollama.js +59 -0
- package/dist/cjs/adapters/ollama.js.map +1 -1
- package/dist/cjs/adapters/openai.js +199 -32
- package/dist/cjs/adapters/openai.js.map +1 -1
- package/dist/esm/adapters/anthropic.js +156 -16
- package/dist/esm/adapters/anthropic.js.map +1 -1
- package/dist/esm/adapters/chrome-ai.js +58 -0
- package/dist/esm/adapters/chrome-ai.js.map +1 -1
- package/dist/esm/adapters/gemini.js +57 -0
- package/dist/esm/adapters/gemini.js.map +1 -1
- package/dist/esm/adapters/generic.js +56 -4
- package/dist/esm/adapters/generic.js.map +1 -1
- package/dist/esm/adapters/mistral.js +59 -0
- package/dist/esm/adapters/mistral.js.map +1 -1
- package/dist/esm/adapters/ollama.js +59 -0
- package/dist/esm/adapters/ollama.js.map +1 -1
- package/dist/esm/adapters/openai.js +199 -32
- package/dist/esm/adapters/openai.js.map +1 -1
- package/dist/types/adapters/anthropic.d.ts +254 -8
- package/dist/types/adapters/anthropic.d.ts.map +1 -1
- package/dist/types/adapters/chrome-ai.d.ts +95 -0
- package/dist/types/adapters/chrome-ai.d.ts.map +1 -1
- package/dist/types/adapters/gemini.d.ts +170 -0
- package/dist/types/adapters/gemini.d.ts.map +1 -1
- package/dist/types/adapters/generic.d.ts +56 -4
- package/dist/types/adapters/generic.d.ts.map +1 -1
- package/dist/types/adapters/mistral.d.ts +143 -0
- package/dist/types/adapters/mistral.d.ts.map +1 -1
- package/dist/types/adapters/ollama.d.ts +132 -0
- package/dist/types/adapters/ollama.d.ts.map +1 -1
- package/dist/types/adapters/openai.d.ts +252 -8
- package/dist/types/adapters/openai.d.ts.map +1 -1
- package/package.json +2 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# ai.matey.frontend
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 58ebc03: Streaming tool-call support end-to-end. OpenAI and Anthropic backends now emit `tool_use` IR chunks
|
|
8
|
+
for streamed tool-call deltas (previously dropped with a console warning) and assemble complete
|
|
9
|
+
`ToolUseContent` blocks on the final `done` chunk. The Anthropic backend reports the real
|
|
10
|
+
provider stop reason (previously fabricated, e.g. `max_tokens` streams reported `stop`) and
|
|
11
|
+
captures usage from every `message_delta`. The OpenAI backend folds the trailing
|
|
12
|
+
`stream_options.include_usage` chunk into the done chunk. Frontend adapters re-emit tool deltas in
|
|
13
|
+
native formats (OpenAI index-based `tool_calls` deltas; Anthropic `content_block_start`/
|
|
14
|
+
`input_json_delta` events — the leading text block is now opened lazily so tool-only streams do not
|
|
15
|
+
fabricate an empty text block). `StreamAccumulator` assembles streamed tool calls.
|
|
16
|
+
|
|
17
|
+
Note: when tools are streamed, the done chunk's `message.content` is now a structured
|
|
18
|
+
`MessageContent[]` (text + tool_use blocks) rather than a plain string, and finish reasons are now
|
|
19
|
+
truthful (`tool_calls`/`length` where `stop` was previously reported).
|
|
20
|
+
|
|
21
|
+
- c7693ac: Complete non-streaming tool-calling support. The OpenAI backend now sends `tools`/`tool_choice`,
|
|
22
|
+
converts assistant `tool_use` blocks to `tool_calls`, expands `tool_result` blocks into
|
|
23
|
+
`role: 'tool'` messages, and parses `tool_calls` from responses (malformed arguments degrade to
|
|
24
|
+
`{}`). The Anthropic backend now sends `tools`/`tool_choice`. Frontend adapters accept
|
|
25
|
+
`tools`/`tool_choice` in their native formats and round-trip tool calls and tool results through
|
|
26
|
+
the IR. OpenAI streaming requests now request usage accounting via `stream_options.include_usage`.
|
|
27
|
+
|
|
28
|
+
### Patch Changes
|
|
29
|
+
|
|
30
|
+
- e7df1d0: Remove vestigial `ai.matey.backend` runtime dependency from `ai.matey.frontend` (frontend adapters
|
|
31
|
+
never imported it). Document `StreamToolUseChunk` delta semantics and add an optional `index` field
|
|
32
|
+
identifying the tool call's position within the assistant message.
|
|
33
|
+
- Updated dependencies [dae4d01]
|
|
34
|
+
- Updated dependencies [e7df1d0]
|
|
35
|
+
- Updated dependencies [2912b7d]
|
|
36
|
+
- Updated dependencies [78731bb]
|
|
37
|
+
- Updated dependencies [b7e2312]
|
|
38
|
+
- Updated dependencies [58ebc03]
|
|
39
|
+
- ai.matey.types@0.3.0
|
|
40
|
+
- ai.matey.utils@0.3.0
|
|
@@ -35,7 +35,27 @@ class AnthropicFrontendAdapter {
|
|
|
35
35
|
},
|
|
36
36
|
};
|
|
37
37
|
/**
|
|
38
|
-
* Convert Anthropic request to Universal IR.
|
|
38
|
+
* Convert Anthropic Messages API request to Universal IR format.
|
|
39
|
+
*
|
|
40
|
+
* This method transforms an Anthropic-formatted request into the standardized
|
|
41
|
+
* Intermediate Representation (IR) format. It handles Anthropic's unique system
|
|
42
|
+
* message format (separate from messages array) by converting it to IR's
|
|
43
|
+
* in-messages format, and maps Anthropic-specific parameters like top_k.
|
|
44
|
+
*
|
|
45
|
+
* @param request - Anthropic Messages API request
|
|
46
|
+
* @returns Promise resolving to IR chat request
|
|
47
|
+
* @throws {AdapterConversionError} If conversion fails
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const adapter = new AnthropicFrontendAdapter();
|
|
52
|
+
* const irRequest = await adapter.toIR({
|
|
53
|
+
* model: 'claude-3-5-sonnet-20241022',
|
|
54
|
+
* max_tokens: 1024,
|
|
55
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
56
|
+
* system: 'You are a helpful assistant'
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
39
59
|
*/
|
|
40
60
|
toIR(request) {
|
|
41
61
|
try {
|
|
@@ -60,6 +80,12 @@ class AnthropicFrontendAdapter {
|
|
|
60
80
|
topK: request.top_k,
|
|
61
81
|
stopSequences: request.stop_sequences,
|
|
62
82
|
},
|
|
83
|
+
tools: request.tools?.map((tool) => ({
|
|
84
|
+
name: tool.name,
|
|
85
|
+
description: tool.description ?? '',
|
|
86
|
+
parameters: tool.input_schema,
|
|
87
|
+
})),
|
|
88
|
+
toolChoice: this.convertToolChoiceToIR(request.tool_choice),
|
|
63
89
|
stream: request.stream ?? false,
|
|
64
90
|
metadata: {
|
|
65
91
|
requestId: crypto.randomUUID(),
|
|
@@ -86,7 +112,23 @@ class AnthropicFrontendAdapter {
|
|
|
86
112
|
}
|
|
87
113
|
}
|
|
88
114
|
/**
|
|
89
|
-
* Convert Universal IR response to Anthropic format.
|
|
115
|
+
* Convert Universal IR response back to Anthropic Messages API format.
|
|
116
|
+
*
|
|
117
|
+
* This method transforms the standardized IR response into the format
|
|
118
|
+
* expected by Anthropic's Messages API. It handles message conversion,
|
|
119
|
+
* stop reason mapping, and usage statistics formatting specific to
|
|
120
|
+
* Anthropic's response structure.
|
|
121
|
+
*
|
|
122
|
+
* @param response - Universal IR chat response
|
|
123
|
+
* @returns Promise resolving to Anthropic Messages API response
|
|
124
|
+
* @throws {AdapterConversionError} If conversion fails
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const adapter = new AnthropicFrontendAdapter();
|
|
129
|
+
* const anthropicResponse = await adapter.fromIR(irResponse);
|
|
130
|
+
* console.log(anthropicResponse.content[0].text);
|
|
131
|
+
* ```
|
|
90
132
|
*/
|
|
91
133
|
fromIR(response) {
|
|
92
134
|
try {
|
|
@@ -121,12 +163,39 @@ class AnthropicFrontendAdapter {
|
|
|
121
163
|
}
|
|
122
164
|
}
|
|
123
165
|
/**
|
|
124
|
-
* Convert Universal IR stream to Anthropic SSE format.
|
|
166
|
+
* Convert Universal IR stream to Anthropic Server-Sent Events (SSE) format.
|
|
167
|
+
*
|
|
168
|
+
* This async generator method transforms a stream of IR chunks into
|
|
169
|
+
* Anthropic-formatted streaming events. It handles stream mode conversion,
|
|
170
|
+
* tracks message metadata, and emits properly formatted SSE events
|
|
171
|
+
* compatible with Anthropic's streaming API including message_start,
|
|
172
|
+
* content_block_delta, and message_stop events.
|
|
173
|
+
*
|
|
174
|
+
* @param stream - Universal IR chat stream
|
|
175
|
+
* @param options - Optional stream conversion options (stream mode, etc.)
|
|
176
|
+
* @yields Anthropic-formatted streaming events
|
|
177
|
+
* @throws {AdapterConversionError} If stream conversion fails
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const adapter = new AnthropicFrontendAdapter();
|
|
182
|
+
* for await (const event of adapter.fromIRStream(irStream)) {
|
|
183
|
+
* if (event.type === 'content_block_delta') {
|
|
184
|
+
* console.log(event.delta.text);
|
|
185
|
+
* }
|
|
186
|
+
* }
|
|
187
|
+
* ```
|
|
125
188
|
*/
|
|
126
189
|
async *fromIRStream(stream, options) {
|
|
127
190
|
try {
|
|
128
191
|
// Apply stream mode conversion if options provided
|
|
129
192
|
const processedStream = options ? (0, ai_matey_utils_1.convertStreamMode)(stream, options) : stream;
|
|
193
|
+
// Content blocks are opened lazily (a tool-only stream must not emit
|
|
194
|
+
// an empty text block) and closed when a different block starts.
|
|
195
|
+
let nextBlockIndex = 0;
|
|
196
|
+
let openBlockIndex = null;
|
|
197
|
+
let openTextBlockIndex = null;
|
|
198
|
+
const toolBlockIndexById = new Map();
|
|
130
199
|
for await (const chunk of processedStream) {
|
|
131
200
|
switch (chunk.type) {
|
|
132
201
|
case 'start':
|
|
@@ -140,27 +209,62 @@ class AnthropicFrontendAdapter {
|
|
|
140
209
|
model: chunk.metadata.provenance?.backend || 'unknown',
|
|
141
210
|
},
|
|
142
211
|
};
|
|
143
|
-
// Emit content_block_start
|
|
144
|
-
yield {
|
|
145
|
-
type: 'content_block_start',
|
|
146
|
-
index: 0,
|
|
147
|
-
content_block: { type: 'text', text: '' },
|
|
148
|
-
};
|
|
149
212
|
break;
|
|
150
213
|
case 'content':
|
|
151
|
-
//
|
|
214
|
+
// Open a text block on first text delta (or after a tool block)
|
|
215
|
+
if (openTextBlockIndex === null) {
|
|
216
|
+
if (openBlockIndex !== null) {
|
|
217
|
+
yield { type: 'content_block_stop', index: openBlockIndex };
|
|
218
|
+
}
|
|
219
|
+
openTextBlockIndex = nextBlockIndex++;
|
|
220
|
+
openBlockIndex = openTextBlockIndex;
|
|
221
|
+
yield {
|
|
222
|
+
type: 'content_block_start',
|
|
223
|
+
index: openTextBlockIndex,
|
|
224
|
+
content_block: { type: 'text', text: '' },
|
|
225
|
+
};
|
|
226
|
+
}
|
|
152
227
|
yield {
|
|
153
228
|
type: 'content_block_delta',
|
|
154
|
-
index:
|
|
229
|
+
index: openTextBlockIndex,
|
|
155
230
|
delta: { type: 'text_delta', text: chunk.delta },
|
|
156
231
|
};
|
|
157
232
|
break;
|
|
233
|
+
case 'tool_use': {
|
|
234
|
+
let blockIndex = toolBlockIndexById.get(chunk.id);
|
|
235
|
+
// First chunk for this tool call: open its content block
|
|
236
|
+
if (blockIndex === undefined) {
|
|
237
|
+
if (openBlockIndex !== null) {
|
|
238
|
+
yield { type: 'content_block_stop', index: openBlockIndex };
|
|
239
|
+
if (openBlockIndex === openTextBlockIndex) {
|
|
240
|
+
openTextBlockIndex = null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
blockIndex = nextBlockIndex++;
|
|
244
|
+
toolBlockIndexById.set(chunk.id, blockIndex);
|
|
245
|
+
openBlockIndex = blockIndex;
|
|
246
|
+
yield {
|
|
247
|
+
type: 'content_block_start',
|
|
248
|
+
index: blockIndex,
|
|
249
|
+
content_block: { type: 'tool_use', id: chunk.id, name: chunk.name, input: {} },
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
if (chunk.inputDelta) {
|
|
253
|
+
yield {
|
|
254
|
+
type: 'content_block_delta',
|
|
255
|
+
index: blockIndex,
|
|
256
|
+
delta: { type: 'input_json_delta', partial_json: chunk.inputDelta },
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
158
261
|
case 'done': {
|
|
159
|
-
//
|
|
160
|
-
|
|
161
|
-
type: 'content_block_stop',
|
|
162
|
-
|
|
163
|
-
|
|
262
|
+
// Close the open content block (if any)
|
|
263
|
+
if (openBlockIndex !== null) {
|
|
264
|
+
yield { type: 'content_block_stop', index: openBlockIndex };
|
|
265
|
+
openBlockIndex = null;
|
|
266
|
+
openTextBlockIndex = null;
|
|
267
|
+
}
|
|
164
268
|
// Emit message_delta with stop reason
|
|
165
269
|
const stopReason = this.mapFinishReason(chunk.finishReason);
|
|
166
270
|
yield {
|
|
@@ -205,6 +309,24 @@ class AnthropicFrontendAdapter {
|
|
|
205
309
|
// ==========================================================================
|
|
206
310
|
// Private Helper Methods
|
|
207
311
|
// ==========================================================================
|
|
312
|
+
/**
|
|
313
|
+
* Convert Anthropic tool_choice to IR toolChoice.
|
|
314
|
+
*/
|
|
315
|
+
convertToolChoiceToIR(toolChoice) {
|
|
316
|
+
if (toolChoice === undefined) {
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
switch (toolChoice.type) {
|
|
320
|
+
case 'auto':
|
|
321
|
+
return 'auto';
|
|
322
|
+
case 'any':
|
|
323
|
+
return 'required';
|
|
324
|
+
case 'none':
|
|
325
|
+
return 'none';
|
|
326
|
+
case 'tool':
|
|
327
|
+
return { name: toolChoice.name };
|
|
328
|
+
}
|
|
329
|
+
}
|
|
208
330
|
/**
|
|
209
331
|
* Convert Anthropic message to IR message.
|
|
210
332
|
*/
|
|
@@ -259,6 +381,15 @@ class AnthropicFrontendAdapter {
|
|
|
259
381
|
name: block.name,
|
|
260
382
|
input: block.input,
|
|
261
383
|
};
|
|
384
|
+
case 'tool_result':
|
|
385
|
+
return {
|
|
386
|
+
type: 'tool_result',
|
|
387
|
+
toolUseId: block.tool_use_id,
|
|
388
|
+
content: typeof block.content === 'string'
|
|
389
|
+
? block.content
|
|
390
|
+
: block.content.map((text) => ({ type: 'text', text: text.text })),
|
|
391
|
+
isError: block.is_error,
|
|
392
|
+
};
|
|
262
393
|
default:
|
|
263
394
|
// Unknown block type, convert to text
|
|
264
395
|
return {
|
|
@@ -302,6 +433,15 @@ class AnthropicFrontendAdapter {
|
|
|
302
433
|
name: block.name,
|
|
303
434
|
input: block.input,
|
|
304
435
|
};
|
|
436
|
+
case 'tool_result':
|
|
437
|
+
return {
|
|
438
|
+
type: 'tool_result',
|
|
439
|
+
tool_use_id: block.toolUseId,
|
|
440
|
+
content: typeof block.content === 'string'
|
|
441
|
+
? block.content
|
|
442
|
+
: block.content.map((text) => ({ type: 'text', text: text.text })),
|
|
443
|
+
is_error: block.isError,
|
|
444
|
+
};
|
|
305
445
|
default:
|
|
306
446
|
// Fallback to text
|
|
307
447
|
return { type: 'text', text: JSON.stringify(block) };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../../src/adapters/anthropic.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../../src/adapters/anthropic.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAYH,qDAAoE;AACpE,mDAAmD;AAwRnD,+EAA+E;AAC/E,6BAA6B;AAC7B,+EAA+E;AAE/E;;GAEG;AACH,MAAa,wBAAwB;IAK1B,QAAQ,GAAoB;QACnC,IAAI,EAAE,oBAAoB;QAC1B,OAAO,EAAE,OAAO;QAChB,QAAQ,EAAE,W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,KAAK,QAAQ;gBACX,OAAO,YAAY,CAAC;YACtB,KAAK,YAAY;gBACf,OAAO,UAAU,CAAC;YACpB;gBACE,OAAO,IAAI,CAAC;QAChB,CAAC;IACH,CAAC;CACF;AA1eD,4DA0eC"}
|
|
@@ -22,6 +22,27 @@ class ChromeAIFrontendAdapter {
|
|
|
22
22
|
supportsMultipleSystemMessages: false,
|
|
23
23
|
},
|
|
24
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Convert Chrome AI request to Universal IR format.
|
|
27
|
+
*
|
|
28
|
+
* This method transforms a Chrome AI-formatted request into the standardized
|
|
29
|
+
* Intermediate Representation (IR) format. Chrome AI uses a simple prompt-based
|
|
30
|
+
* format, so this converts the single prompt string into a user message within
|
|
31
|
+
* the IR message array structure.
|
|
32
|
+
*
|
|
33
|
+
* @param request - Chrome AI request with prompt and optional parameters
|
|
34
|
+
* @returns Promise resolving to IR chat request
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const adapter = new ChromeAIFrontendAdapter();
|
|
39
|
+
* const irRequest = await adapter.toIR({
|
|
40
|
+
* prompt: 'What is the capital of France?',
|
|
41
|
+
* temperature: 0.7,
|
|
42
|
+
* topK: 40
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
25
46
|
toIR(request) {
|
|
26
47
|
return Promise.resolve({
|
|
27
48
|
messages: [{ role: 'user', content: request.prompt }],
|
|
@@ -36,11 +57,48 @@ class ChromeAIFrontendAdapter {
|
|
|
36
57
|
},
|
|
37
58
|
});
|
|
38
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Convert Universal IR response back to Chrome AI format.
|
|
62
|
+
*
|
|
63
|
+
* This method transforms the standardized IR response into the format
|
|
64
|
+
* expected by Chrome AI's interface. It extracts the text content from
|
|
65
|
+
* the IR message and returns it in Chrome AI's simple `{ text }` format.
|
|
66
|
+
*
|
|
67
|
+
* @param response - Universal IR chat response
|
|
68
|
+
* @returns Promise resolving to Chrome AI response
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* const adapter = new ChromeAIFrontendAdapter();
|
|
73
|
+
* const chromeResponse = await adapter.fromIR(irResponse);
|
|
74
|
+
* console.log(chromeResponse.text);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
39
77
|
fromIR(response) {
|
|
40
78
|
return Promise.resolve({
|
|
41
79
|
text: typeof response.message.content === 'string' ? response.message.content : '',
|
|
42
80
|
});
|
|
43
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Convert Universal IR stream to Chrome AI streaming format.
|
|
84
|
+
*
|
|
85
|
+
* This async generator method transforms a stream of IR chunks into
|
|
86
|
+
* Chrome AI's simple text streaming format. It yields raw text strings
|
|
87
|
+
* (no JSON wrapping, no SSE format) and supports stream mode conversion
|
|
88
|
+
* through options (delta, full, or text-only modes).
|
|
89
|
+
*
|
|
90
|
+
* @param stream - Universal IR chat stream
|
|
91
|
+
* @param options - Optional stream conversion options (stream mode, etc.)
|
|
92
|
+
* @yields Raw text strings
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const adapter = new ChromeAIFrontendAdapter();
|
|
97
|
+
* for await (const textChunk of adapter.fromIRStream(irStream)) {
|
|
98
|
+
* console.log(textChunk); // Raw text like "Hello" or " world"
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
44
102
|
async *fromIRStream(stream, options) {
|
|
45
103
|
// Apply stream mode conversion if options provided
|
|
46
104
|
const processedStream = options ? (0, ai_matey_utils_1.convertStreamMode)(stream, options) : stream;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chrome-ai.js","sourceRoot":"","sources":["../../../src/adapters/chrome-ai.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAKH,mDAAmD;
|
|
1
|
+
{"version":3,"file":"chrome-ai.js","sourceRoot":"","sources":["../../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D;QACnD,MAAM,eAAe,GAAG,OAAO,CAAC,CAAC,CAAC,IAAA,kCAAiB,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QAE9E,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,eAAe,EAAE,CAAC;YAC1C,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;gBAC7B,MAAM,KAAK,CAAC,KAAK,CAAC;YACpB,CAAC;QACH,CAAC;IACH,CAAC;CACF;AA1GD,0DA0GC"}
|
|
@@ -22,6 +22,26 @@ class GeminiFrontendAdapter {
|
|
|
22
22
|
supportsMultipleSystemMessages: false,
|
|
23
23
|
},
|
|
24
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Convert Google Gemini API request to Universal IR format.
|
|
27
|
+
*
|
|
28
|
+
* This method transforms a Gemini-formatted request into the standardized
|
|
29
|
+
* Intermediate Representation (IR) format. It handles Gemini's unique
|
|
30
|
+
* content structure (parts array), converts 'model' role to 'assistant',
|
|
31
|
+
* and adjusts temperature values (Gemini uses 0-1, IR uses 0-2).
|
|
32
|
+
*
|
|
33
|
+
* @param request - Google Gemini API request
|
|
34
|
+
* @returns Promise resolving to IR chat request
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const adapter = new GeminiFrontendAdapter();
|
|
39
|
+
* const irRequest = await adapter.toIR({
|
|
40
|
+
* contents: [{ role: 'user', parts: [{ text: 'Hello!' }] }],
|
|
41
|
+
* generationConfig: { temperature: 0.7 }
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
25
45
|
toIR(request) {
|
|
26
46
|
const messages = request.contents.map((c) => ({
|
|
27
47
|
role: c.role === 'model' ? 'assistant' : 'user',
|
|
@@ -53,6 +73,24 @@ class GeminiFrontendAdapter {
|
|
|
53
73
|
},
|
|
54
74
|
});
|
|
55
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Convert Universal IR response back to Google Gemini API format.
|
|
78
|
+
*
|
|
79
|
+
* This method transforms the standardized IR response into the format
|
|
80
|
+
* expected by Gemini's API. It converts 'assistant' role back to 'model',
|
|
81
|
+
* structures content as parts array, and maps finish reasons to Gemini's
|
|
82
|
+
* specific format (STOP, MAX_TOKENS, OTHER).
|
|
83
|
+
*
|
|
84
|
+
* @param response - Universal IR chat response
|
|
85
|
+
* @returns Promise resolving to Gemini API response
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const adapter = new GeminiFrontendAdapter();
|
|
90
|
+
* const geminiResponse = await adapter.fromIR(irResponse);
|
|
91
|
+
* console.log(geminiResponse.candidates[0].content.parts[0].text);
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
56
94
|
fromIR(response) {
|
|
57
95
|
return Promise.resolve({
|
|
58
96
|
candidates: [
|
|
@@ -81,6 +119,25 @@ class GeminiFrontendAdapter {
|
|
|
81
119
|
: undefined,
|
|
82
120
|
});
|
|
83
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Convert Universal IR stream to Google Gemini Server-Sent Events format.
|
|
124
|
+
*
|
|
125
|
+
* This async generator method transforms a stream of IR chunks into
|
|
126
|
+
* Gemini-formatted SSE responses. It yields Server-Sent Event strings
|
|
127
|
+
* with the "data: " prefix containing JSON candidates with parts array.
|
|
128
|
+
*
|
|
129
|
+
* @param stream - Universal IR chat stream
|
|
130
|
+
* @param _options - Optional stream conversion options (currently unused)
|
|
131
|
+
* @yields Server-Sent Event formatted strings
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const adapter = new GeminiFrontendAdapter();
|
|
136
|
+
* for await (const sseData of adapter.fromIRStream(irStream)) {
|
|
137
|
+
* console.log(sseData); // "data: {...}\n\n"
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
84
141
|
async *fromIRStream(stream, _options) {
|
|
85
142
|
for await (const chunk of stream) {
|
|
86
143
|
if (chunk.type === 'content') {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gemini.js","sourceRoot":"","sources":["../../../src/adapters/gemini.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;
|
|
1
|
+
{"version":3,"file":"gemini.js","sourceRoot":"","sources":["../../../src/adapters/gemini.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAmKH,MAAa,qBAAqB;IACvB,QAAQ,GAAoB;QACnC,IAAI,EAAE,iBAAiB;QACvB,OAAO,EAAE,OAAO;QAChB,QAAQ,EAAE,eAAe;QACzB,YAAY,EAAE;YACZ,SAAS,EAAE,IAAI;YACf,UAAU,EAAE,IAAI;YAChB,KAAK,EAAE,IAAI;YACX,qBAAqB,EAAE,oBAAoB;YAC3C,8BAA8B,EAAE,KAAK;SACtC;KACF,CAAC;IAEF;;;;;;;;;;;;;;;;;;;OAmBG;IACH,IAAI,CAAC,OAAsB;QACzB,MAAM,QAAQ,GAAgB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAgB,EAAE,EAAE,CAAC,CAAC;YACxE,IAAI,EAAE,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,MAAM;YAC/C,OAAO,EAAE,CAAC,CAAC,KAAK;iBACb,GAAG,CAAC,CAAC,CAAwE,EAAE,EAAE,CAChF,MAAM,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAC1B;iBACA,IAAI,CAAC,EAAE,CAAC;SACZ,CAAC,CAAC,CAAC;QACJ,IAAI,OAAO,CAAC,iBAAiB,EAAE,CAAC;YAC9B,QAAQ,CAAC,OAAO,CAAC;gBACf,IAAI,EAAE,QAAQ;gBACd,OAAO,EAAE,OAAO,CAAC,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAmB,EAAE,EAAE,CAAC,CAA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AC,OAAO;iBAChB;aACF;YACD,aAAa,EAAE,QAAQ,CAAC,KAAK;gBAC3B,CAAC,CAAC;oBACE,gBAAgB,EAAE,QAAQ,CAAC,KAAK,CAAC,YAAY;oBAC7C,oBAAoB,EAAE,QAAQ,CAAC,KAAK,CAAC,gBAAgB;oBACrD,eAAe,EAAE,QAAQ,CAAC,KAAK,CAAC,WAAW;iBAC5C;gBACH,CAAC,CAAC,SAAS;SACd,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CAAC,CAAC,YAAY,CACjB,MAAqC,EACrC,QAAkC;QAElC,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YACjC,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;gBAC7B,MAAM,QAAQ;oBACZ,IAAI,CAAC,SAAS,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;oBACjF,MAAM,CAAC;YACX,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAnJD,sDAmJC"}
|
|
@@ -81,9 +81,26 @@ class GenericFrontendAdapter {
|
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
|
-
* Pass through IR request
|
|
84
|
+
* Pass through Universal IR request without conversion.
|
|
85
85
|
*
|
|
86
|
-
*
|
|
86
|
+
* This method accepts an IR request and returns it directly, as this is
|
|
87
|
+
* a passthrough adapter. When provenance tracking is enabled (default),
|
|
88
|
+
* it adds the frontend adapter name to the provenance metadata for
|
|
89
|
+
* tracking purposes, but otherwise performs no transformations.
|
|
90
|
+
*
|
|
91
|
+
* @param request - Universal IR chat request
|
|
92
|
+
* @returns Promise resolving to the same IR request (with optional provenance)
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const adapter = new GenericFrontendAdapter();
|
|
97
|
+
* const irRequest = await adapter.toIR({
|
|
98
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
99
|
+
* parameters: { model: 'gpt-4', temperature: 0.7 },
|
|
100
|
+
* metadata: { requestId: 'req_123', timestamp: Date.now(), provenance: {} }
|
|
101
|
+
* });
|
|
102
|
+
* // Returns the same request with provenance.frontend = 'generic-frontend'
|
|
103
|
+
* ```
|
|
87
104
|
*/
|
|
88
105
|
toIR(request) {
|
|
89
106
|
// Optionally add provenance
|
|
@@ -102,13 +119,48 @@ class GenericFrontendAdapter {
|
|
|
102
119
|
return Promise.resolve(request);
|
|
103
120
|
}
|
|
104
121
|
/**
|
|
105
|
-
* Pass through IR response
|
|
122
|
+
* Pass through Universal IR response without conversion.
|
|
123
|
+
*
|
|
124
|
+
* This method accepts an IR response and returns it directly without
|
|
125
|
+
* any transformation. Since this is a passthrough adapter, the response
|
|
126
|
+
* remains in IR format, allowing applications to work directly with
|
|
127
|
+
* the universal format.
|
|
128
|
+
*
|
|
129
|
+
* @param response - Universal IR chat response
|
|
130
|
+
* @returns Promise resolving to the same IR response
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const adapter = new GenericFrontendAdapter();
|
|
135
|
+
* const irResponse = await adapter.fromIR(irResponse);
|
|
136
|
+
* // Returns the exact same response unchanged
|
|
137
|
+
* console.log(irResponse.message.content);
|
|
138
|
+
* ```
|
|
106
139
|
*/
|
|
107
140
|
fromIR(response) {
|
|
108
141
|
return Promise.resolve(response);
|
|
109
142
|
}
|
|
110
143
|
/**
|
|
111
|
-
* Pass through IR stream chunks
|
|
144
|
+
* Pass through Universal IR stream chunks without conversion.
|
|
145
|
+
*
|
|
146
|
+
* This async generator method accepts an IR stream and yields each chunk
|
|
147
|
+
* directly without any transformation. Unlike other frontend adapters that
|
|
148
|
+
* convert to provider-specific formats (SSE, JSON, etc.), this maintains
|
|
149
|
+
* the raw IR chunk structure for direct consumption.
|
|
150
|
+
*
|
|
151
|
+
* @param stream - Universal IR chat stream
|
|
152
|
+
* @param _options - Optional stream conversion options (currently unused)
|
|
153
|
+
* @yields IR stream chunks unchanged
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const adapter = new GenericFrontendAdapter();
|
|
158
|
+
* for await (const chunk of adapter.fromIRStream(irStream)) {
|
|
159
|
+
* if (chunk.type === 'content') {
|
|
160
|
+
* console.log(chunk.delta); // Access IR structure directly
|
|
161
|
+
* }
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
112
164
|
*/
|
|
113
165
|
async *fromIRStream(stream, _options) {
|
|
114
166
|
for await (const chunk of stream) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generic.js","sourceRoot":"","sources":["../../../src/adapters/generic.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;;;
|
|
1
|
+
{"version":3,"file":"generic.js","sourceRoot":"","sources":["../../../src/adapters/generic.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;;;AAyPH,sDAEC;AA1MD,+EAA+E;AAC/E,2BAA2B;AAC3B,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,sBAAsB;IAKxB,QAAQ,CAAkB;IAClB,MAAM,CAAwB;IAE/C,YAAY,SAAgC,EAAE;QAC5C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,IAAI,CAAC,QAAQ,GAAG;YACd,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,kBAAkB;YACvC,OAAO,EAAE,OAAO;YAChB,QAAQ,EAAE,SAAS;YACnB,YAAY,EAAE;gBACZ,SAAS,EAAE,IAAI;gBACf,UAAU,EAAE,IAAI;gBAChB,KAAK,EAAE,IAAI;gBACX,gBAAgB,EAAE,MAAM,CAAC,gBAAgB;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