@lobehub/chat 1.96.12 → 1.96.14

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.
@@ -330,4 +330,133 @@ describe('VertexAIStream', () => {
330
330
  ].map((i) => i + '\n'),
331
331
  );
332
332
  });
333
+
334
+ it('should return empty text chunk without candidates', async () => {
335
+ vi.spyOn(uuidModule, 'nanoid').mockReturnValueOnce('1');
336
+
337
+ const data = [
338
+ {
339
+ candidates: [
340
+ {
341
+ content: { parts: [{ text: '234' }], role: 'model' },
342
+ safetyRatings: [
343
+ { category: 'HARM_CATEGORY_HATE_SPEECH', probability: 'NEGLIGIBLE' },
344
+ { category: 'HARM_CATEGORY_DANGEROUS_CONTENT', probability: 'NEGLIGIBLE' },
345
+ { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
346
+ { category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT', probability: 'NEGLIGIBLE' },
347
+ ],
348
+ },
349
+ ],
350
+ usageMetadata: {
351
+ promptTokenCount: 20,
352
+ candidatesTokenCount: 3,
353
+ totalTokenCount: 23,
354
+ promptTokensDetails: [{ modality: 'TEXT', tokenCount: 20 }],
355
+ candidatesTokensDetails: [{ modality: 'TEXT', tokenCount: 3 }],
356
+ },
357
+ modelVersion: 'gemini-2.5-flash-preview-04-17',
358
+ },
359
+ {
360
+ usageMetadata: {
361
+ promptTokenCount: 20,
362
+ candidatesTokenCount: 3,
363
+ totalTokenCount: 23,
364
+ promptTokensDetails: [{ modality: 'TEXT', tokenCount: 20 }],
365
+ candidatesTokensDetails: [{ modality: 'TEXT', tokenCount: 3 }],
366
+ },
367
+ modelVersion: 'gemini-2.5-flash-preview-04-17',
368
+ },
369
+ ];
370
+
371
+ const mockGoogleStream = new ReadableStream({
372
+ start(controller) {
373
+ data.forEach((item) => {
374
+ controller.enqueue(item);
375
+ });
376
+
377
+ controller.close();
378
+ },
379
+ });
380
+
381
+ const protocolStream = VertexAIStream(mockGoogleStream);
382
+
383
+ const decoder = new TextDecoder();
384
+ const chunks = [];
385
+
386
+ // @ts-ignore
387
+ for await (const chunk of protocolStream) {
388
+ chunks.push(decoder.decode(chunk, { stream: true }));
389
+ }
390
+
391
+ expect(chunks).toEqual(
392
+ ['id: chat_1', 'event: text', 'data: "234"\n', 'id: chat_1', 'event: text', `data: ""\n`].map(
393
+ (i) => i + '\n',
394
+ ),
395
+ );
396
+ });
397
+
398
+ it('should return stop chunk with empty content candidates', async () => {
399
+ vi.spyOn(uuidModule, 'nanoid').mockReturnValueOnce('1');
400
+
401
+ const data = [
402
+ {
403
+ candidates: [
404
+ {
405
+ content: { parts: [{ text: '234' }], role: 'model' },
406
+ safetyRatings: [
407
+ { category: 'HARM_CATEGORY_HATE_SPEECH', probability: 'NEGLIGIBLE' },
408
+ { category: 'HARM_CATEGORY_DANGEROUS_CONTENT', probability: 'NEGLIGIBLE' },
409
+ { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
410
+ { category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT', probability: 'NEGLIGIBLE' },
411
+ ],
412
+ },
413
+ ],
414
+ usageMetadata: {
415
+ promptTokenCount: 20,
416
+ candidatesTokenCount: 3,
417
+ totalTokenCount: 23,
418
+ promptTokensDetails: [{ modality: 'TEXT', tokenCount: 20 }],
419
+ candidatesTokensDetails: [{ modality: 'TEXT', tokenCount: 3 }],
420
+ },
421
+ modelVersion: 'gemini-2.5-flash-preview-04-17',
422
+ },
423
+ {
424
+ candidates: [{}],
425
+ usageMetadata: {
426
+ promptTokenCount: 20,
427
+ candidatesTokenCount: 3,
428
+ totalTokenCount: 23,
429
+ promptTokensDetails: [{ modality: 'TEXT', tokenCount: 20 }],
430
+ candidatesTokensDetails: [{ modality: 'TEXT', tokenCount: 3 }],
431
+ },
432
+ modelVersion: 'gemini-2.5-flash-preview-04-17',
433
+ },
434
+ ];
435
+
436
+ const mockGoogleStream = new ReadableStream({
437
+ start(controller) {
438
+ data.forEach((item) => {
439
+ controller.enqueue(item);
440
+ });
441
+
442
+ controller.close();
443
+ },
444
+ });
445
+
446
+ const protocolStream = VertexAIStream(mockGoogleStream);
447
+
448
+ const decoder = new TextDecoder();
449
+ const chunks = [];
450
+
451
+ // @ts-ignore
452
+ for await (const chunk of protocolStream) {
453
+ chunks.push(decoder.decode(chunk, { stream: true }));
454
+ }
455
+
456
+ expect(chunks).toEqual(
457
+ ['id: chat_1', 'event: text', 'data: "234"\n', 'id: chat_1', 'event: stop', `data: ""\n`].map(
458
+ (i) => i + '\n',
459
+ ),
460
+ );
461
+ });
333
462
  });
@@ -1,4 +1,4 @@
1
- import { EnhancedGenerateContentResponse, GenerateContentResponse } from '@google/generative-ai';
1
+ import { GenerateContentResponse } from '@google/genai';
2
2
 
3
3
  import { ModelTokensUsage } from '@/types/message';
4
4
  import { nanoid } from '@/utils/uuid';
@@ -22,8 +22,9 @@ const transformVertexAIStream = (
22
22
  const usage = chunk.usageMetadata;
23
23
  const usageChunks: StreamProtocolChunk[] = [];
24
24
  if (candidate?.finishReason && usage) {
25
- const outputReasoningTokens = (usage as any).thoughtsTokenCount || undefined;
26
- const totalOutputTokens = (usage.candidatesTokenCount ?? 0) + (outputReasoningTokens ?? 0);
25
+ const outputReasoningTokens = usage.thoughtsTokenCount || undefined;
26
+ const outputTextTokens = usage.candidatesTokenCount ?? 0;
27
+ const totalOutputTokens = outputTextTokens + (outputReasoningTokens ?? 0);
27
28
 
28
29
  usageChunks.push(
29
30
  { data: candidate.finishReason, id: context?.id, type: 'stop' },
@@ -37,7 +38,7 @@ const transformVertexAIStream = (
37
38
  (i: any) => i.modality === 'TEXT',
38
39
  )?.tokenCount,
39
40
  outputReasoningTokens,
40
- outputTextTokens: totalOutputTokens - (outputReasoningTokens ?? 0),
41
+ outputTextTokens,
41
42
  totalInputTokens: usage.promptTokenCount,
42
43
  totalOutputTokens,
43
44
  totalTokens: usage.totalTokenCount,
@@ -50,7 +51,7 @@ const transformVertexAIStream = (
50
51
 
51
52
  if (
52
53
  candidate && // 首先检查是否为 reasoning 内容 (thought: true)
53
- Array.isArray(candidate.content.parts) &&
54
+ Array.isArray(candidate.content?.parts) &&
54
55
  candidate.content.parts.length > 0
55
56
  ) {
56
57
  for (const part of candidate.content.parts) {
@@ -60,19 +61,18 @@ const transformVertexAIStream = (
60
61
  }
61
62
  }
62
63
 
63
- const candidates = chunk.candidates;
64
- if (!candidates)
64
+ if (!candidate) {
65
65
  return {
66
66
  data: '',
67
67
  id: context?.id,
68
68
  type: 'text',
69
69
  };
70
+ }
70
71
 
71
- const item = candidates[0];
72
- if (item.content) {
73
- const part = item.content.parts[0];
72
+ if (candidate.content) {
73
+ const part = candidate.content.parts?.[0];
74
74
 
75
- if (part.functionCall) {
75
+ if (part?.functionCall) {
76
76
  const functionCall = part.functionCall;
77
77
 
78
78
  return [
@@ -95,18 +95,18 @@ const transformVertexAIStream = (
95
95
  ];
96
96
  }
97
97
 
98
- if (item.finishReason) {
98
+ if (candidate.finishReason) {
99
99
  if (chunk.usageMetadata) {
100
100
  return [
101
- !!part.text ? { data: part.text, id: context?.id, type: 'text' } : undefined,
101
+ !!part?.text ? { data: part.text, id: context?.id, type: 'text' } : undefined,
102
102
  ...usageChunks,
103
103
  ].filter(Boolean) as StreamProtocolChunk[];
104
104
  }
105
- return { data: item.finishReason, id: context?.id, type: 'stop' };
105
+ return { data: candidate.finishReason, id: context?.id, type: 'stop' };
106
106
  }
107
107
 
108
108
  return {
109
- data: part.text,
109
+ data: part?.text,
110
110
  id: context?.id,
111
111
  type: 'text',
112
112
  };
@@ -120,7 +120,7 @@ const transformVertexAIStream = (
120
120
  };
121
121
 
122
122
  export const VertexAIStream = (
123
- rawStream: ReadableStream<EnhancedGenerateContentResponse>,
123
+ rawStream: ReadableStream<GenerateContentResponse>,
124
124
  { callbacks, inputStartAt }: GoogleAIStreamOptions = {},
125
125
  ) => {
126
126
  const streamStack: StreamContext = { id: 'chat_' + nanoid() };
@@ -1,13 +1,19 @@
1
- import { VertexAI, VertexInit } from '@google-cloud/vertexai';
1
+ import { GoogleGenAI, GoogleGenAIOptions } from '@google/genai';
2
2
 
3
3
  import { AgentRuntimeErrorType } from '../error';
4
4
  import { LobeGoogleAI } from '../google';
5
5
  import { AgentRuntimeError } from '../utils/createError';
6
6
 
7
+ const DEFAULT_VERTEXAI_LOCATION = 'global';
8
+
7
9
  export class LobeVertexAI extends LobeGoogleAI {
8
- static initFromVertexAI(params?: VertexInit) {
10
+ static initFromVertexAI(params?: GoogleGenAIOptions) {
9
11
  try {
10
- const client = new VertexAI({ ...params });
12
+ const client = new GoogleGenAI({
13
+ ...params,
14
+ location: params?.location ?? DEFAULT_VERTEXAI_LOCATION, // @google/genai 不传 location 会报错
15
+ vertexai: true,
16
+ });
11
17
 
12
18
  return new LobeGoogleAI({ apiKey: 'avoid-error', client, isVertexAi: true });
13
19
  } catch (e) {