@layer-ai/core 2.0.17 → 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.
@@ -314,6 +314,84 @@ async function testMultiProviderFallback() {
314
314
  console.log(' ✅ Multi-provider fallback test passed\n');
315
315
  }
316
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
+ }
317
395
  // Run all tests
318
396
  (async () => {
319
397
  try {
@@ -324,6 +402,8 @@ async function testMultiProviderFallback() {
324
402
  await testClaudeStreaming();
325
403
  await testClaudeToolCallsStreaming();
326
404
  await testMultiProviderFallback();
405
+ await testGeminiStreaming();
406
+ await testGeminiToolCallsStreaming();
327
407
  console.log('='.repeat(80));
328
408
  console.log('✅ ALL STREAMING ROUTE TESTS PASSED');
329
409
  console.log('='.repeat(80));
@@ -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,mB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AAM,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-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.17",
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",