@layer-ai/core 2.0.16 → 2.0.18

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.
@@ -195,6 +195,203 @@ async function testStreamingWithTools() {
195
195
  console.log(' ⚠️ Tool calls may not have been invoked (model chose not to use tools)\n');
196
196
  }
197
197
  }
198
+ // Test 5: Claude/Anthropic streaming
199
+ async function testClaudeStreaming() {
200
+ console.log('Test 5: Claude/Anthropic Streaming');
201
+ console.log('-'.repeat(80));
202
+ const request = {
203
+ gateId: 'test-gate',
204
+ model: 'claude-3-7-sonnet-20250219',
205
+ type: 'chat',
206
+ data: {
207
+ messages: [
208
+ { role: 'user', content: 'Say "claude test passed" and nothing else.' }
209
+ ],
210
+ maxTokens: 20,
211
+ stream: true,
212
+ }
213
+ };
214
+ let chunkCount = 0;
215
+ let fullContent = '';
216
+ for await (const chunk of callAdapterStream(request)) {
217
+ chunkCount++;
218
+ if (chunk.content) {
219
+ fullContent += chunk.content;
220
+ }
221
+ }
222
+ console.log(` Chunks received: ${chunkCount}`);
223
+ console.log(` Content: ${fullContent.trim()}`);
224
+ console.log(' ✅ Claude streaming test passed\n');
225
+ }
226
+ // Test 6: Claude with tool calls streaming
227
+ async function testClaudeToolCallsStreaming() {
228
+ console.log('Test 6: Claude Tool Calls Streaming');
229
+ console.log('-'.repeat(80));
230
+ const request = {
231
+ gateId: 'test-gate',
232
+ model: 'claude-3-7-sonnet-20250219',
233
+ type: 'chat',
234
+ data: {
235
+ messages: [
236
+ { role: 'user', content: 'What is the weather in Tokyo?' }
237
+ ],
238
+ tools: [
239
+ {
240
+ type: 'function',
241
+ function: {
242
+ name: 'get_weather',
243
+ description: 'Get weather for a location',
244
+ parameters: {
245
+ type: 'object',
246
+ properties: {
247
+ location: { type: 'string' },
248
+ },
249
+ required: ['location'],
250
+ },
251
+ },
252
+ },
253
+ ],
254
+ stream: true,
255
+ }
256
+ };
257
+ let toolCallsFound = false;
258
+ let finishReason = null;
259
+ for await (const chunk of callAdapterStream(request)) {
260
+ if (chunk.toolCalls && chunk.toolCalls.length > 0) {
261
+ toolCallsFound = true;
262
+ }
263
+ if (chunk.finishReason) {
264
+ finishReason = chunk.finishReason;
265
+ }
266
+ }
267
+ console.log(` Tool calls found: ${toolCallsFound}`);
268
+ console.log(` Finish reason: ${finishReason}`);
269
+ if (toolCallsFound && finishReason === 'tool_call') {
270
+ console.log(' ✅ Claude tool calls streaming test passed\n');
271
+ }
272
+ else {
273
+ console.log(' ⚠️ Tool calls may not have been invoked (model chose not to use tools)\n');
274
+ }
275
+ }
276
+ // Test 7: Multi-provider fallback with streaming (OpenAI -> Claude)
277
+ async function testMultiProviderFallback() {
278
+ console.log('Test 7: Multi-Provider Fallback (OpenAI -> Claude) with Streaming');
279
+ console.log('-'.repeat(80));
280
+ const request = {
281
+ gateId: 'test-gate',
282
+ model: 'invalid-openai-model',
283
+ type: 'chat',
284
+ data: {
285
+ messages: [
286
+ { role: 'user', content: 'Say "multi-provider fallback worked" and nothing else.' }
287
+ ],
288
+ maxTokens: 15,
289
+ stream: true,
290
+ }
291
+ };
292
+ const modelsToTry = [
293
+ 'invalid-openai-model',
294
+ 'claude-3-7-sonnet-20250219',
295
+ ];
296
+ let chunkCount = 0;
297
+ let fullContent = '';
298
+ let succeeded = false;
299
+ try {
300
+ for await (const chunk of executeWithFallbackStream(request, modelsToTry)) {
301
+ chunkCount++;
302
+ if (chunk.content) {
303
+ fullContent += chunk.content;
304
+ }
305
+ }
306
+ succeeded = true;
307
+ }
308
+ catch (error) {
309
+ console.error(' ❌ Multi-provider fallback failed:', error instanceof Error ? error.message : error);
310
+ }
311
+ if (succeeded) {
312
+ console.log(` Chunks received: ${chunkCount}`);
313
+ console.log(` Content: ${fullContent.trim()}`);
314
+ console.log(' ✅ Multi-provider fallback test passed\n');
315
+ }
316
+ }
317
+ // Test 8: Google/Gemini streaming
318
+ async function testGeminiStreaming() {
319
+ console.log('Test 8: Google/Gemini Streaming');
320
+ console.log('-'.repeat(80));
321
+ const request = {
322
+ gateId: 'test-gate',
323
+ model: 'gemini-2.0-flash',
324
+ type: 'chat',
325
+ data: {
326
+ messages: [
327
+ { role: 'user', content: 'Say "gemini test passed" and nothing else.' }
328
+ ],
329
+ maxTokens: 20,
330
+ stream: true,
331
+ }
332
+ };
333
+ let chunkCount = 0;
334
+ let fullContent = '';
335
+ for await (const chunk of callAdapterStream(request)) {
336
+ chunkCount++;
337
+ if (chunk.content) {
338
+ fullContent += chunk.content;
339
+ }
340
+ }
341
+ console.log(` Chunks received: ${chunkCount}`);
342
+ console.log(` Content: ${fullContent.trim()}`);
343
+ console.log(' ✅ Gemini streaming test passed\n');
344
+ }
345
+ // Test 9: Gemini with tool calls streaming
346
+ async function testGeminiToolCallsStreaming() {
347
+ console.log('Test 9: Gemini Tool Calls Streaming');
348
+ console.log('-'.repeat(80));
349
+ const request = {
350
+ gateId: 'test-gate',
351
+ model: 'gemini-2.0-flash',
352
+ type: 'chat',
353
+ data: {
354
+ messages: [
355
+ { role: 'user', content: 'What is the weather in London?' }
356
+ ],
357
+ tools: [
358
+ {
359
+ type: 'function',
360
+ function: {
361
+ name: 'get_weather',
362
+ description: 'Get weather for a location',
363
+ parameters: {
364
+ type: 'object',
365
+ properties: {
366
+ location: { type: 'string' },
367
+ },
368
+ required: ['location'],
369
+ },
370
+ },
371
+ },
372
+ ],
373
+ stream: true,
374
+ }
375
+ };
376
+ let toolCallsFound = false;
377
+ let finishReason = null;
378
+ for await (const chunk of callAdapterStream(request)) {
379
+ if (chunk.toolCalls && chunk.toolCalls.length > 0) {
380
+ toolCallsFound = true;
381
+ }
382
+ if (chunk.finishReason) {
383
+ finishReason = chunk.finishReason;
384
+ }
385
+ }
386
+ console.log(` Tool calls found: ${toolCallsFound}`);
387
+ console.log(` Finish reason: ${finishReason}`);
388
+ if (toolCallsFound) {
389
+ console.log(' ✅ Gemini tool calls streaming test passed\n');
390
+ }
391
+ else {
392
+ console.log(' ⚠️ Tool calls may not have been invoked (model chose not to use tools)\n');
393
+ }
394
+ }
198
395
  // Run all tests
199
396
  (async () => {
200
397
  try {
@@ -202,6 +399,11 @@ async function testStreamingWithTools() {
202
399
  await testFallbackRouting();
203
400
  await testRoundRobinRouting();
204
401
  await testStreamingWithTools();
402
+ await testClaudeStreaming();
403
+ await testClaudeToolCallsStreaming();
404
+ await testMultiProviderFallback();
405
+ await testGeminiStreaming();
406
+ await testGeminiToolCallsStreaming();
205
407
  console.log('='.repeat(80));
206
408
  console.log('✅ ALL STREAMING ROUTE TESTS PASSED');
207
409
  console.log('='.repeat(80));
@@ -8,6 +8,8 @@ export declare class AnthropicAdapter extends BaseProviderAdapter {
8
8
  protected finishReasonMappings: Record<string, FinishReason>;
9
9
  protected mapToolChoice(choice: ToolChoice): string | object | undefined;
10
10
  call(request: LayerRequest, userId?: string): Promise<LayerResponse>;
11
+ callStream(request: LayerRequest, userId?: string): AsyncIterable<LayerResponse>;
11
12
  private handleChat;
13
+ private handleChatStream;
12
14
  }
13
15
  //# sourceMappingURL=anthropic-adapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-adapter.d.ts","sourceRoot":"","sources":["../../../src/services/providers/anthropic-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAmB,MAAM,mBAAmB,CAAC;AACzE,OAAO,EACL,YAAY,EACZ,aAAa,EACb,IAAI,EACJ,YAAY,EACZ,UAAU,EACX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAoB1E,qBAAa,gBAAiB,SAAQ,mBAAmB;IACvD,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAsB;IAElD,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAQ1C;IAEF,SAAS,CAAC,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAI3D;IAEF,SAAS,CAAC,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAM1D;IAEF,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS;IAalE,IAAI,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;YAoB5D,UAAU;CA6JzB"}
1
+ {"version":3,"file":"anthropic-adapter.d.ts","sourceRoot":"","sources":["../../../src/services/providers/anthropic-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAmB,MAAM,mBAAmB,CAAC;AACzE,OAAO,EACL,YAAY,EACZ,aAAa,EACb,IAAI,EACJ,YAAY,EACZ,UAAU,EACX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAkB1E,qBAAa,gBAAiB,SAAQ,mBAAmB;IACvD,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAsB;IAElD,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAQ1C;IAEF,SAAS,CAAC,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAI3D;IAEF,SAAS,CAAC,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAM1D;IAEF,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS;IAalE,IAAI,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAmBnE,UAAU,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC,aAAa,CAAC;YAYzE,UAAU;YA8JT,gBAAgB;CA0KhC"}
@@ -4,11 +4,9 @@ import { PROVIDER } from "../../lib/provider-constants.js";
4
4
  import { resolveApiKey } from '../../lib/key-resolver.js';
5
5
  let anthropic = null;
6
6
  function getAnthropicClient(apiKey) {
7
- // If custom API key provided, create new client
8
7
  if (apiKey) {
9
8
  return new Anthropic({ apiKey });
10
9
  }
11
- // Otherwise use singleton with platform key
12
10
  if (!anthropic) {
13
11
  anthropic = new Anthropic({
14
12
  apiKey: process.env.ANTHROPIC_API_KEY,
@@ -54,7 +52,6 @@ export class AnthropicAdapter extends BaseProviderAdapter {
54
52
  return super.mapToolChoice(choice);
55
53
  }
56
54
  async call(request, userId) {
57
- // Resolve API key (BYOK → Platform key)
58
55
  const resolved = await resolveApiKey(this.provider, userId, process.env.ANTHROPIC_API_KEY);
59
56
  switch (request.type) {
60
57
  case 'chat':
@@ -71,6 +68,16 @@ export class AnthropicAdapter extends BaseProviderAdapter {
71
68
  throw new Error(`Unknown modality: ${request.type}`);
72
69
  }
73
70
  }
71
+ async *callStream(request, userId) {
72
+ const resolved = await resolveApiKey(this.provider, userId, process.env.ANTHROPIC_API_KEY);
73
+ switch (request.type) {
74
+ case 'chat':
75
+ yield* this.handleChatStream(request, resolved.key, resolved.usedPlatformKey);
76
+ break;
77
+ default:
78
+ throw new Error(`Streaming not supported for type: ${request.type}`);
79
+ }
80
+ }
74
81
  async handleChat(request, apiKey, usedPlatformKey) {
75
82
  const startTime = Date.now();
76
83
  const client = getAnthropicClient(apiKey);
@@ -215,4 +222,157 @@ export class AnthropicAdapter extends BaseProviderAdapter {
215
222
  raw: response,
216
223
  };
217
224
  }
225
+ async *handleChatStream(request, apiKey, usedPlatformKey) {
226
+ const startTime = Date.now();
227
+ const client = getAnthropicClient(apiKey);
228
+ const { data: chat, model } = request;
229
+ if (!model) {
230
+ throw new Error('Model is required for chat completions');
231
+ }
232
+ const systemPrompt = chat.systemPrompt || undefined;
233
+ const messages = [];
234
+ for (const msg of chat.messages) {
235
+ if (msg.role === 'system')
236
+ continue;
237
+ const role = this.mapRole(msg.role);
238
+ if (msg.toolCallId) {
239
+ messages.push({
240
+ role: 'user',
241
+ content: [{
242
+ type: 'tool_result',
243
+ tool_use_id: msg.toolCallId,
244
+ content: msg.content || '',
245
+ }],
246
+ });
247
+ }
248
+ else if (msg.images?.length || msg.toolCalls?.length) {
249
+ const content = [];
250
+ if (msg.content) {
251
+ content.push({ type: 'text', text: msg.content });
252
+ }
253
+ if (msg.images) {
254
+ for (const image of msg.images) {
255
+ if (image.url) {
256
+ content.push({
257
+ type: 'image',
258
+ source: {
259
+ type: 'url',
260
+ url: image.url,
261
+ }
262
+ });
263
+ }
264
+ else if (image.base64) {
265
+ content.push({
266
+ type: 'image',
267
+ source: {
268
+ type: 'base64',
269
+ media_type: image.mimeType || 'image/jpeg',
270
+ data: image.base64
271
+ }
272
+ });
273
+ }
274
+ }
275
+ }
276
+ if (msg.toolCalls) {
277
+ for (const toolCall of msg.toolCalls) {
278
+ content.push({
279
+ type: 'tool_use',
280
+ id: toolCall.id,
281
+ name: toolCall.function.name,
282
+ input: JSON.parse(toolCall.function.arguments),
283
+ });
284
+ }
285
+ }
286
+ const messageRole = msg.images?.length ? 'user' : (msg.toolCalls?.length ? 'assistant' : role);
287
+ messages.push({ role: messageRole, content });
288
+ }
289
+ else {
290
+ messages.push({
291
+ role: role,
292
+ content: msg.content || '',
293
+ });
294
+ }
295
+ }
296
+ const anthropicRequest = {
297
+ model: model,
298
+ messages,
299
+ max_tokens: chat.maxTokens || 4096,
300
+ stream: true,
301
+ ...(systemPrompt && { system: systemPrompt }),
302
+ ...(chat.temperature !== undefined && { temperature: chat.temperature }),
303
+ ...(chat.temperature === undefined && chat.topP !== undefined && { top_p: chat.topP }),
304
+ ...(chat.stopSequences && { stop_sequences: chat.stopSequences }),
305
+ ...(chat.tools && {
306
+ tools: chat.tools.map(tool => ({
307
+ name: tool.function.name,
308
+ description: tool.function.description,
309
+ input_schema: tool.function.parameters || { type: 'object', properties: {} },
310
+ })),
311
+ ...(chat.toolChoice && { tool_choice: this.mapToolChoice(chat.toolChoice) }),
312
+ }),
313
+ };
314
+ const stream = client.messages.stream(anthropicRequest);
315
+ let promptTokens = 0;
316
+ let completionTokens = 0;
317
+ let fullContent = '';
318
+ let currentToolCalls = [];
319
+ let stopReason = null;
320
+ for await (const event of stream) {
321
+ if (event.type === 'content_block_start') {
322
+ const block = event.content_block;
323
+ if (block.type === 'tool_use') {
324
+ currentToolCalls.push({
325
+ id: block.id,
326
+ type: 'function',
327
+ function: {
328
+ name: block.name,
329
+ arguments: '',
330
+ },
331
+ });
332
+ }
333
+ }
334
+ else if (event.type === 'content_block_delta') {
335
+ const delta = event.delta;
336
+ if (delta.type === 'text_delta') {
337
+ fullContent += delta.text;
338
+ yield {
339
+ content: delta.text,
340
+ model: model,
341
+ stream: true,
342
+ };
343
+ }
344
+ else if (delta.type === 'input_json_delta') {
345
+ if (currentToolCalls.length > 0) {
346
+ const lastToolCall = currentToolCalls[currentToolCalls.length - 1];
347
+ lastToolCall.function.arguments += delta.partial_json;
348
+ }
349
+ }
350
+ }
351
+ else if (event.type === 'message_start') {
352
+ promptTokens = event.message.usage.input_tokens;
353
+ }
354
+ else if (event.type === 'message_delta') {
355
+ completionTokens = event.usage.output_tokens;
356
+ stopReason = event.delta.stop_reason || stopReason;
357
+ }
358
+ }
359
+ const cost = this.calculateCost(model, promptTokens, completionTokens);
360
+ const latencyMs = Date.now() - startTime;
361
+ yield {
362
+ content: '',
363
+ model: model,
364
+ toolCalls: currentToolCalls.length > 0 ? currentToolCalls : undefined,
365
+ usage: {
366
+ promptTokens,
367
+ completionTokens,
368
+ totalTokens: promptTokens + completionTokens,
369
+ },
370
+ cost,
371
+ latencyMs,
372
+ usedPlatformKey,
373
+ stream: true,
374
+ finishReason: this.mapFinishReason(stopReason || 'end_turn'),
375
+ rawFinishReason: stopReason || undefined,
376
+ };
377
+ }
218
378
  }
@@ -12,7 +12,9 @@ export declare class GoogleAdapter extends BaseProviderAdapter {
12
12
  resolution: string;
13
13
  }>;
14
14
  call(request: LayerRequest, userId?: string): Promise<LayerResponse>;
15
+ callStream(request: LayerRequest, userId?: string): AsyncIterable<LayerResponse>;
15
16
  private handleChat;
17
+ private handleChatStream;
16
18
  private handleImageGeneration;
17
19
  private handleEmbeddings;
18
20
  private handleVideoGeneration;
@@ -1 +1 @@
1
- {"version":3,"file":"google-adapter.d.ts","sourceRoot":"","sources":["../../../src/services/providers/google-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,yBAAyB,EAI1B,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,YAAY,EACZ,aAAa,EACb,IAAI,EACJ,YAAY,EACZ,SAAS,EACV,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAkB1E,qBAAa,aAAc,SAAQ,mBAAmB;IACpD,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAmB;IAE/C,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAQ1C;IAGF,SAAS,CAAC,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAO1D;IAEF,SAAS,CAAC,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAIrE;IAGF,SAAS,CAAC,eAAe,EAAE,MAAM,CAC/B,SAAS,EACT;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,CAC5C,CAKC;IAEI,IAAI,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;YAoB5D,UAAU;YAwLV,qBAAqB;YAqCrB,gBAAgB;YAsChB,qBAAqB;YAsHrB,kBAAkB;IA2ChC,OAAO,CAAC,KAAK;CAGd"}
1
+ {"version":3,"file":"google-adapter.d.ts","sourceRoot":"","sources":["../../../src/services/providers/google-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,yBAAyB,EAI1B,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,YAAY,EACZ,aAAa,EACb,IAAI,EACJ,YAAY,EACZ,SAAS,EACV,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,iCAAiC,CAAC;AAgB1E,qBAAa,aAAc,SAAQ,mBAAmB;IACpD,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAmB;IAE/C,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAQ1C;IAEF,SAAS,CAAC,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAO1D;IAEF,SAAS,CAAC,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAIrE;IAEF,SAAS,CAAC,eAAe,EAAE,MAAM,CAC/B,SAAS,EACT;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,CAC5C,CAKC;IAEI,IAAI,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAmBnE,UAAU,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC,aAAa,CAAC;YAYzE,UAAU;YAwLT,gBAAgB;YAmNjB,qBAAqB;YAqCrB,gBAAgB;YAsChB,qBAAqB;YAsHrB,kBAAkB;IA2ChC,OAAO,CAAC,KAAK;CAGd"}
@@ -5,11 +5,9 @@ import { PROVIDER } from "../../lib/provider-constants.js";
5
5
  import { resolveApiKey } from '../../lib/key-resolver.js';
6
6
  let client = null;
7
7
  function getGoogleClient(apiKey) {
8
- // If custom API key provided, create new client
9
8
  if (apiKey) {
10
9
  return new GoogleGenAI({ apiKey });
11
10
  }
12
- // Otherwise use singleton with platform key
13
11
  if (!client) {
14
12
  client = new GoogleGenAI({ apiKey: process.env.GOOGLE_API_KEY || '' });
15
13
  }
@@ -28,7 +26,6 @@ export class GoogleAdapter extends BaseProviderAdapter {
28
26
  model: 'model',
29
27
  developer: 'system',
30
28
  };
31
- // Map Google finish reasons to Layer finish reasons
32
29
  this.finishReasonMappings = {
33
30
  STOP: 'completed',
34
31
  MAX_TOKENS: 'length_limit',
@@ -42,7 +39,6 @@ export class GoogleAdapter extends BaseProviderAdapter {
42
39
  none: FunctionCallingConfigMode.NONE,
43
40
  required: FunctionCallingConfigMode.ANY,
44
41
  };
45
- // Map Layer VideoSize to Veo aspect ratio and resolution
46
42
  this.videoSizeConfig = {
47
43
  '720x1280': { aspectRatio: '9:16', resolution: '720p' },
48
44
  '1280x720': { aspectRatio: '16:9', resolution: '720p' },
@@ -51,7 +47,6 @@ export class GoogleAdapter extends BaseProviderAdapter {
51
47
  };
52
48
  }
53
49
  async call(request, userId) {
54
- // Resolve API key (BYOK → Platform key)
55
50
  const resolved = await resolveApiKey(this.provider, userId, process.env.GOOGLE_API_KEY);
56
51
  switch (request.type) {
57
52
  case 'chat':
@@ -68,6 +63,16 @@ export class GoogleAdapter extends BaseProviderAdapter {
68
63
  throw new Error(`Unknown modality: ${request.type}`);
69
64
  }
70
65
  }
66
+ async *callStream(request, userId) {
67
+ const resolved = await resolveApiKey(this.provider, userId, process.env.GOOGLE_API_KEY);
68
+ switch (request.type) {
69
+ case 'chat':
70
+ yield* this.handleChatStream(request, resolved.key, resolved.usedPlatformKey);
71
+ break;
72
+ default:
73
+ throw new Error(`Streaming not supported for type: ${request.type}`);
74
+ }
75
+ }
71
76
  async handleChat(request, apiKey, usedPlatformKey) {
72
77
  const startTime = Date.now();
73
78
  const client = getGoogleClient(apiKey);
@@ -224,6 +229,182 @@ export class GoogleAdapter extends BaseProviderAdapter {
224
229
  raw: response,
225
230
  };
226
231
  }
232
+ async *handleChatStream(request, apiKey, usedPlatformKey) {
233
+ const startTime = Date.now();
234
+ const client = getGoogleClient(apiKey);
235
+ const { data: chat, model } = request;
236
+ if (!model) {
237
+ throw new Error('Model is required for chat completion');
238
+ }
239
+ const contents = [];
240
+ let systemInstruction;
241
+ if (chat.systemPrompt) {
242
+ systemInstruction = chat.systemPrompt;
243
+ }
244
+ for (const msg of chat.messages) {
245
+ const role = this.mapRole(msg.role);
246
+ if (role === 'system') {
247
+ systemInstruction = systemInstruction
248
+ ? `${systemInstruction}\n${msg.content}`
249
+ : msg.content;
250
+ continue;
251
+ }
252
+ const parts = [];
253
+ if (msg.content) {
254
+ parts.push({ text: msg.content });
255
+ }
256
+ if (msg.images && msg.images.length > 0) {
257
+ for (const image of msg.images) {
258
+ if (image.base64) {
259
+ parts.push({
260
+ inlineData: {
261
+ mimeType: image.mimeType || 'image/jpeg',
262
+ data: image.base64,
263
+ },
264
+ });
265
+ }
266
+ else if (image.url) {
267
+ parts.push({
268
+ fileData: {
269
+ mimeType: image.mimeType || 'image/jpeg',
270
+ fileUri: image.url,
271
+ },
272
+ });
273
+ }
274
+ }
275
+ }
276
+ if (msg.toolCallId && msg.role === 'tool') {
277
+ if (!msg.name) {
278
+ throw new Error('Tool response messages must include the function name');
279
+ }
280
+ parts.push({
281
+ functionResponse: {
282
+ name: msg.name || msg.toolCallId,
283
+ response: { result: msg.content },
284
+ },
285
+ });
286
+ }
287
+ if (msg.toolCalls && msg.toolCalls.length > 0) {
288
+ for (const toolCall of msg.toolCalls) {
289
+ parts.push({
290
+ functionCall: {
291
+ name: toolCall.function.name,
292
+ args: JSON.parse(toolCall.function.arguments),
293
+ },
294
+ });
295
+ }
296
+ }
297
+ if (parts.length > 0) {
298
+ contents.push({
299
+ role: role === 'model' ? 'model' : 'user',
300
+ parts,
301
+ });
302
+ }
303
+ }
304
+ let googleTools;
305
+ if (chat.tools && chat.tools.length > 0) {
306
+ googleTools = [
307
+ {
308
+ functionDeclarations: chat.tools.map((tool) => ({
309
+ name: tool.function.name,
310
+ description: tool.function.description,
311
+ parametersJsonSchema: tool.function.parameters,
312
+ })),
313
+ },
314
+ ];
315
+ }
316
+ let toolConfig;
317
+ if (chat.toolChoice) {
318
+ const mode = this.mapToolChoice(chat.toolChoice);
319
+ if (typeof mode === 'string') {
320
+ toolConfig = {
321
+ functionCallingConfig: { mode: mode },
322
+ };
323
+ }
324
+ }
325
+ const stream = await client.models.generateContentStream({
326
+ model: model,
327
+ contents,
328
+ config: {
329
+ ...(systemInstruction && { systemInstruction }),
330
+ ...(googleTools && { tools: googleTools }),
331
+ ...(toolConfig && { toolConfig }),
332
+ ...(chat.temperature !== undefined && { temperature: chat.temperature }),
333
+ ...(chat.maxTokens !== undefined && { maxOutputTokens: chat.maxTokens }),
334
+ ...(chat.topP !== undefined && { topP: chat.topP }),
335
+ ...(chat.stopSequences !== undefined && { stopSequences: chat.stopSequences }),
336
+ }
337
+ });
338
+ let promptTokens = 0;
339
+ let completionTokens = 0;
340
+ let totalTokens = 0;
341
+ let fullContent = '';
342
+ let currentToolCalls = [];
343
+ let finishReason = null;
344
+ let modelVersion;
345
+ for await (const chunk of stream) {
346
+ const candidate = chunk.candidates?.[0];
347
+ const content = candidate?.content;
348
+ const textChunk = content?.parts
349
+ ?.filter((part) => 'text' in part)
350
+ .map((part) => part.text)
351
+ .join('');
352
+ if (textChunk) {
353
+ fullContent += textChunk;
354
+ yield {
355
+ content: textChunk,
356
+ model: model,
357
+ stream: true,
358
+ };
359
+ }
360
+ const toolCallParts = content?.parts?.filter((part) => 'functionCall' in part);
361
+ if (toolCallParts && toolCallParts.length > 0) {
362
+ for (const part of toolCallParts) {
363
+ const fc = part.functionCall;
364
+ const existingCall = currentToolCalls.find(tc => tc.function.name === fc.name);
365
+ if (!existingCall) {
366
+ currentToolCalls.push({
367
+ id: `call_${currentToolCalls.length}_${fc.name}`,
368
+ type: 'function',
369
+ function: {
370
+ name: fc.name,
371
+ arguments: JSON.stringify(fc.args),
372
+ },
373
+ });
374
+ }
375
+ }
376
+ }
377
+ if (chunk.usageMetadata) {
378
+ promptTokens = chunk.usageMetadata.promptTokenCount || 0;
379
+ completionTokens = chunk.usageMetadata.candidatesTokenCount || 0;
380
+ totalTokens = chunk.usageMetadata.totalTokenCount || 0;
381
+ }
382
+ if (candidate?.finishReason) {
383
+ finishReason = candidate.finishReason;
384
+ }
385
+ if (chunk.modelVersion) {
386
+ modelVersion = chunk.modelVersion;
387
+ }
388
+ }
389
+ const cost = this.calculateCost(model, promptTokens, completionTokens);
390
+ const latencyMs = Date.now() - startTime;
391
+ yield {
392
+ content: '',
393
+ model: modelVersion || model,
394
+ toolCalls: currentToolCalls.length > 0 ? currentToolCalls : undefined,
395
+ usage: {
396
+ promptTokens,
397
+ completionTokens,
398
+ totalTokens,
399
+ },
400
+ cost,
401
+ latencyMs,
402
+ usedPlatformKey,
403
+ stream: true,
404
+ finishReason: this.mapFinishReason(finishReason || 'STOP'),
405
+ rawFinishReason: finishReason || undefined,
406
+ };
407
+ }
227
408
  async handleImageGeneration(request, apiKey, usedPlatformKey) {
228
409
  const startTime = Date.now();
229
410
  const client = getGoogleClient(apiKey);
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=test-anthropic-streaming.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-anthropic-streaming.d.ts","sourceRoot":"","sources":["../../../../src/services/providers/tests/test-anthropic-streaming.ts"],"names":[],"mappings":""}
@@ -0,0 +1,139 @@
1
+ import { AnthropicAdapter } from '../anthropic-adapter.js';
2
+ const adapter = new AnthropicAdapter();
3
+ async function testChatStreamingBasic() {
4
+ console.log('Testing basic chat streaming...');
5
+ const request = {
6
+ gateId: 'test-gate',
7
+ model: 'claude-3-7-sonnet-20250219',
8
+ type: 'chat',
9
+ data: {
10
+ messages: [
11
+ { role: 'user', content: 'Count from 1 to 5 slowly, one number per line.' }
12
+ ],
13
+ temperature: 0.7,
14
+ maxTokens: 50,
15
+ stream: true,
16
+ }
17
+ };
18
+ if (!adapter.callStream) {
19
+ throw new Error('callStream method not available');
20
+ }
21
+ let chunkCount = 0;
22
+ let fullContent = '';
23
+ let finalUsage = null;
24
+ let finalCost = null;
25
+ console.log('\nStreaming chunks:');
26
+ console.log('---');
27
+ for await (const chunk of adapter.callStream(request)) {
28
+ chunkCount++;
29
+ if (chunk.content) {
30
+ process.stdout.write(chunk.content);
31
+ fullContent += chunk.content;
32
+ }
33
+ if (chunk.usage) {
34
+ finalUsage = chunk.usage;
35
+ }
36
+ if (chunk.cost) {
37
+ finalCost = chunk.cost;
38
+ }
39
+ }
40
+ console.log('\n---\n');
41
+ console.log('Total chunks received:', chunkCount);
42
+ console.log('Full content:', fullContent);
43
+ console.log('Final usage:', finalUsage);
44
+ console.log('Final cost:', finalCost);
45
+ console.log('✅ Basic streaming test passed\n');
46
+ }
47
+ async function testChatStreamingWithToolCalls() {
48
+ console.log('Testing chat streaming with tool calls...');
49
+ const request = {
50
+ gateId: 'test-gate',
51
+ model: 'claude-3-7-sonnet-20250219',
52
+ type: 'chat',
53
+ data: {
54
+ messages: [
55
+ { role: 'user', content: 'What is the weather in San Francisco?' }
56
+ ],
57
+ tools: [
58
+ {
59
+ type: 'function',
60
+ function: {
61
+ name: 'get_weather',
62
+ description: 'Get the current weather for a location',
63
+ parameters: {
64
+ type: 'object',
65
+ properties: {
66
+ location: {
67
+ type: 'string',
68
+ description: 'The city and state, e.g. San Francisco, CA',
69
+ },
70
+ },
71
+ required: ['location'],
72
+ },
73
+ },
74
+ },
75
+ ],
76
+ toolChoice: 'auto',
77
+ stream: true,
78
+ }
79
+ };
80
+ if (!adapter.callStream) {
81
+ throw new Error('callStream method not available');
82
+ }
83
+ let toolCallsReceived = false;
84
+ for await (const chunk of adapter.callStream(request)) {
85
+ if (chunk.toolCalls && chunk.toolCalls.length > 0) {
86
+ console.log('Tool calls received:', JSON.stringify(chunk.toolCalls, null, 2));
87
+ toolCallsReceived = true;
88
+ }
89
+ if (chunk.finishReason === 'tool_call') {
90
+ console.log('Finish reason: tool_call');
91
+ }
92
+ }
93
+ if (!toolCallsReceived) {
94
+ console.warn('⚠️ No tool calls received (model may have chosen not to use tools)');
95
+ }
96
+ else {
97
+ console.log('✅ Tool calls streaming test passed\n');
98
+ }
99
+ }
100
+ async function testChatStreamingError() {
101
+ console.log('Testing streaming with invalid model (error handling)...');
102
+ const request = {
103
+ gateId: 'test-gate',
104
+ model: 'invalid-model-name-that-does-not-exist',
105
+ type: 'chat',
106
+ data: {
107
+ messages: [
108
+ { role: 'user', content: 'Hello' }
109
+ ],
110
+ stream: true,
111
+ }
112
+ };
113
+ if (!adapter.callStream) {
114
+ throw new Error('callStream method not available');
115
+ }
116
+ try {
117
+ for await (const chunk of adapter.callStream(request)) {
118
+ console.log('Received chunk:', chunk);
119
+ }
120
+ console.error('❌ Should have thrown an error for invalid model');
121
+ }
122
+ catch (error) {
123
+ console.log('✅ Correctly threw error:', error instanceof Error ? error.message : error);
124
+ console.log('✅ Error handling test passed\n');
125
+ }
126
+ }
127
+ // Run all tests
128
+ (async () => {
129
+ try {
130
+ await testChatStreamingBasic();
131
+ await testChatStreamingWithToolCalls();
132
+ await testChatStreamingError();
133
+ console.log('✅ All streaming tests completed successfully!');
134
+ }
135
+ catch (error) {
136
+ console.error('❌ Test failed:', error);
137
+ process.exit(1);
138
+ }
139
+ })();
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=test-google-streaming.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-google-streaming.d.ts","sourceRoot":"","sources":["../../../../src/services/providers/tests/test-google-streaming.ts"],"names":[],"mappings":""}
@@ -0,0 +1,139 @@
1
+ import { GoogleAdapter } from '../google-adapter.js';
2
+ const adapter = new GoogleAdapter();
3
+ async function testChatStreamingBasic() {
4
+ console.log('Testing basic chat streaming...');
5
+ const request = {
6
+ gateId: 'test-gate',
7
+ model: 'gemini-2.0-flash',
8
+ type: 'chat',
9
+ data: {
10
+ messages: [
11
+ { role: 'user', content: 'Count from 1 to 5 slowly, one number per line.' }
12
+ ],
13
+ temperature: 0.7,
14
+ maxTokens: 50,
15
+ stream: true,
16
+ }
17
+ };
18
+ if (!adapter.callStream) {
19
+ throw new Error('callStream method not available');
20
+ }
21
+ let chunkCount = 0;
22
+ let fullContent = '';
23
+ let finalUsage = null;
24
+ let finalCost = null;
25
+ console.log('\nStreaming chunks:');
26
+ console.log('---');
27
+ for await (const chunk of adapter.callStream(request)) {
28
+ chunkCount++;
29
+ if (chunk.content) {
30
+ process.stdout.write(chunk.content);
31
+ fullContent += chunk.content;
32
+ }
33
+ if (chunk.usage) {
34
+ finalUsage = chunk.usage;
35
+ }
36
+ if (chunk.cost) {
37
+ finalCost = chunk.cost;
38
+ }
39
+ }
40
+ console.log('\n---\n');
41
+ console.log('Total chunks received:', chunkCount);
42
+ console.log('Full content:', fullContent);
43
+ console.log('Final usage:', finalUsage);
44
+ console.log('Final cost:', finalCost);
45
+ console.log('✅ Basic streaming test passed\n');
46
+ }
47
+ async function testChatStreamingWithToolCalls() {
48
+ console.log('Testing chat streaming with tool calls...');
49
+ const request = {
50
+ gateId: 'test-gate',
51
+ model: 'gemini-2.0-flash',
52
+ type: 'chat',
53
+ data: {
54
+ messages: [
55
+ { role: 'user', content: 'What is the weather in San Francisco?' }
56
+ ],
57
+ tools: [
58
+ {
59
+ type: 'function',
60
+ function: {
61
+ name: 'get_weather',
62
+ description: 'Get the current weather for a location',
63
+ parameters: {
64
+ type: 'object',
65
+ properties: {
66
+ location: {
67
+ type: 'string',
68
+ description: 'The city and state, e.g. San Francisco, CA',
69
+ },
70
+ },
71
+ required: ['location'],
72
+ },
73
+ },
74
+ },
75
+ ],
76
+ toolChoice: 'auto',
77
+ stream: true,
78
+ }
79
+ };
80
+ if (!adapter.callStream) {
81
+ throw new Error('callStream method not available');
82
+ }
83
+ let toolCallsReceived = false;
84
+ for await (const chunk of adapter.callStream(request)) {
85
+ if (chunk.toolCalls && chunk.toolCalls.length > 0) {
86
+ console.log('Tool calls received:', JSON.stringify(chunk.toolCalls, null, 2));
87
+ toolCallsReceived = true;
88
+ }
89
+ if (chunk.finishReason === 'tool_call') {
90
+ console.log('Finish reason: tool_call');
91
+ }
92
+ }
93
+ if (!toolCallsReceived) {
94
+ console.warn('⚠️ No tool calls received (model may have chosen not to use tools)');
95
+ }
96
+ else {
97
+ console.log('✅ Tool calls streaming test passed\n');
98
+ }
99
+ }
100
+ async function testChatStreamingError() {
101
+ console.log('Testing streaming with invalid model (error handling)...');
102
+ const request = {
103
+ gateId: 'test-gate',
104
+ model: 'invalid-google-model-name',
105
+ type: 'chat',
106
+ data: {
107
+ messages: [
108
+ { role: 'user', content: 'Hello' }
109
+ ],
110
+ stream: true,
111
+ }
112
+ };
113
+ if (!adapter.callStream) {
114
+ throw new Error('callStream method not available');
115
+ }
116
+ try {
117
+ for await (const chunk of adapter.callStream(request)) {
118
+ console.log('Received chunk:', chunk);
119
+ }
120
+ console.error('❌ Should have thrown an error for invalid model');
121
+ }
122
+ catch (error) {
123
+ console.log('✅ Correctly threw error:', error instanceof Error ? error.message : error);
124
+ console.log('✅ Error handling test passed\n');
125
+ }
126
+ }
127
+ // Run all tests
128
+ (async () => {
129
+ try {
130
+ await testChatStreamingBasic();
131
+ await testChatStreamingWithToolCalls();
132
+ await testChatStreamingError();
133
+ console.log('✅ All streaming tests completed successfully!');
134
+ }
135
+ catch (error) {
136
+ console.error('❌ Test failed:', error);
137
+ process.exit(1);
138
+ }
139
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@layer-ai/core",
3
- "version": "2.0.16",
3
+ "version": "2.0.18",
4
4
  "description": "Core API routes and services for Layer AI",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",