@lobehub/chat 1.73.2 → 1.74.1

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.
Files changed (91) hide show
  1. package/.env.example +4 -0
  2. package/CHANGELOG.md +51 -0
  3. package/Dockerfile +5 -1
  4. package/Dockerfile.database +5 -1
  5. package/Dockerfile.pglite +3 -1
  6. package/README.md +4 -2
  7. package/README.zh-CN.md +4 -2
  8. package/changelog/v1.json +18 -0
  9. package/docs/self-hosting/environment-variables/model-provider.mdx +13 -0
  10. package/docs/self-hosting/environment-variables/model-provider.zh-CN.mdx +14 -0
  11. package/docs/usage/providers/infiniai.mdx +29 -0
  12. package/docs/usage/providers/infiniai.zh-CN.mdx +29 -0
  13. package/locales/ar/models.json +30 -0
  14. package/locales/ar/providers.json +3 -0
  15. package/locales/bg-BG/models.json +30 -0
  16. package/locales/bg-BG/providers.json +3 -0
  17. package/locales/de-DE/models.json +30 -0
  18. package/locales/de-DE/providers.json +3 -0
  19. package/locales/en-US/models.json +30 -0
  20. package/locales/en-US/providers.json +3 -0
  21. package/locales/es-ES/models.json +30 -0
  22. package/locales/es-ES/providers.json +3 -0
  23. package/locales/fa-IR/models.json +30 -0
  24. package/locales/fa-IR/providers.json +3 -0
  25. package/locales/fr-FR/models.json +30 -0
  26. package/locales/fr-FR/providers.json +3 -0
  27. package/locales/it-IT/models.json +30 -0
  28. package/locales/it-IT/providers.json +3 -0
  29. package/locales/ja-JP/models.json +22 -0
  30. package/locales/ja-JP/providers.json +3 -0
  31. package/locales/ko-KR/models.json +30 -0
  32. package/locales/ko-KR/providers.json +3 -0
  33. package/locales/nl-NL/models.json +30 -0
  34. package/locales/nl-NL/providers.json +3 -0
  35. package/locales/pl-PL/models.json +30 -0
  36. package/locales/pl-PL/providers.json +3 -0
  37. package/locales/pt-BR/models.json +30 -0
  38. package/locales/pt-BR/providers.json +3 -0
  39. package/locales/ru-RU/models.json +30 -0
  40. package/locales/ru-RU/providers.json +3 -0
  41. package/locales/tr-TR/models.json +30 -0
  42. package/locales/tr-TR/providers.json +3 -0
  43. package/locales/vi-VN/models.json +30 -0
  44. package/locales/vi-VN/providers.json +3 -0
  45. package/locales/zh-CN/models.json +30 -0
  46. package/locales/zh-CN/providers.json +3 -0
  47. package/locales/zh-TW/models.json +19 -0
  48. package/locales/zh-TW/providers.json +3 -0
  49. package/package.json +3 -3
  50. package/packages/web-crawler/src/utils/htmlToMarkdown.test.ts +1 -1
  51. package/src/app/[variants]/(main)/settings/llm/ProviderList/providers.tsx +5 -1
  52. package/src/config/aiModels/index.ts +6 -0
  53. package/src/config/aiModels/infiniai.ts +307 -0
  54. package/src/config/aiModels/search1api.ts +63 -0
  55. package/src/config/llm.ts +12 -0
  56. package/src/config/modelProviders/index.ts +8 -0
  57. package/src/config/modelProviders/infiniai.ts +184 -0
  58. package/src/config/modelProviders/search1api.ts +58 -0
  59. package/src/database/models/__tests__/knowledgeBase.test.ts +2 -0
  60. package/src/libs/agent-runtime/ai21/index.test.ts +8 -250
  61. package/src/libs/agent-runtime/ai360/index.test.ts +8 -250
  62. package/src/libs/agent-runtime/anthropic/index.ts +5 -1
  63. package/src/libs/agent-runtime/deepseek/index.test.ts +119 -335
  64. package/src/libs/agent-runtime/fireworksai/index.test.ts +8 -247
  65. package/src/libs/agent-runtime/giteeai/index.test.ts +8 -250
  66. package/src/libs/agent-runtime/github/index.test.ts +8 -207
  67. package/src/libs/agent-runtime/infiniai/index.ts +43 -0
  68. package/src/libs/agent-runtime/internlm/index.test.ts +8 -250
  69. package/src/libs/agent-runtime/lmstudio/index.test.ts +8 -247
  70. package/src/libs/agent-runtime/moonshot/index.test.ts +10 -243
  71. package/src/libs/agent-runtime/novita/index.test.ts +9 -221
  72. package/src/libs/agent-runtime/ollama/index.test.ts +4 -4
  73. package/src/libs/agent-runtime/openrouter/index.test.ts +12 -217
  74. package/src/libs/agent-runtime/ppio/index.test.ts +11 -220
  75. package/src/libs/agent-runtime/providerTestUtils.ts +6 -6
  76. package/src/libs/agent-runtime/qwen/index.test.ts +10 -242
  77. package/src/libs/agent-runtime/runtimeMap.ts +4 -0
  78. package/src/libs/agent-runtime/search1api/index.ts +64 -0
  79. package/src/libs/agent-runtime/sensenova/index.test.ts +10 -242
  80. package/src/libs/agent-runtime/spark/index.test.ts +7 -242
  81. package/src/libs/agent-runtime/stepfun/index.test.ts +7 -242
  82. package/src/libs/agent-runtime/taichu/index.test.ts +12 -220
  83. package/src/libs/agent-runtime/types/type.ts +2 -0
  84. package/src/libs/agent-runtime/upstage/index.test.ts +7 -250
  85. package/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.test.ts +2 -2
  86. package/src/libs/agent-runtime/xai/index.test.ts +8 -250
  87. package/src/services/chat.ts +1 -4
  88. package/src/types/user/settings/keyVaults.ts +2 -0
  89. package/src/utils/fetch/__tests__/parseToolCalls.test.ts +9 -11
  90. package/src/utils/server/jwt.test.ts +1 -1
  91. package/vitest.server.config.ts +3 -1
@@ -0,0 +1,184 @@
1
+ import { ModelProviderCard } from '@/types/llm';
2
+
3
+ // https://cloud.infini-ai.com/genstudio/model
4
+ // All models are currently free
5
+ const InfiniAI: ModelProviderCard = {
6
+ chatModels: [
7
+ {
8
+ contextWindowTokens: 65_536,
9
+ description:
10
+ 'DeepSeek-R1 是一个专注于推理能力的大语言模型,通过创新的训练流程实现了与 OpenAI-o1 相当的数学、代码和推理任务表现。该模型采用了冷启动数据和大规模强化学习相结合的方式进行训练。',
11
+ displayName: 'DeepSeek R1',
12
+ enabled: true,
13
+ id: 'deepseek-r1',
14
+ pricing: {
15
+ currency: 'CNY',
16
+ input: 0,
17
+ output: 0,
18
+ },
19
+ },
20
+ {
21
+ contextWindowTokens: 65_536,
22
+ description:
23
+ 'DeepSeek-V3 是一个强大的专家混合(MoE)语言模型,总参数量为 671B,每个 Token 激活 37B 参数。该模型采用多头潜在注意力(MLA)和 DeepSeekMoE 架构,实现了高效推理和经济训练。',
24
+ displayName: 'DeepSeek V3',
25
+ enabled: true,
26
+ id: 'deepseek-v3',
27
+ pricing: {
28
+ currency: 'CNY',
29
+ input: 0,
30
+ output: 0,
31
+ },
32
+ },
33
+ {
34
+ contextWindowTokens: 65_536,
35
+ description:
36
+ 'QwQ 是 Qwen 系列的推理模型,相比传统指令调优模型,QwQ 具备思考和推理能力,在下游任务尤其是难题上能取得显著性能提升。QwQ-32B 是一款中等规模的推理模型,其性能可与最先进的推理模型相媲美,例如 DeepSeek-R1 和 o1-mini。',
37
+ displayName: 'QwQ',
38
+ enabled: true,
39
+ id: 'qwq-32b',
40
+ pricing: {
41
+ currency: 'CNY',
42
+ input: 0,
43
+ output: 0,
44
+ },
45
+ },
46
+ {
47
+ contextWindowTokens: 32_768,
48
+ description:
49
+ 'DeepSeek-R1-Distill-Qwen-32B 是基于 DeepSeek-R1 蒸馏而来的模型,在 Qwen2.5-32B 的基础上使用 DeepSeek-R1 生成的样本进行微调。该模型在各种基准测试中表现出色,保持了强大的推理能力。',
50
+ displayName: 'DeepSeek R1 Distill Qwen 32B',
51
+ enabled: true,
52
+ id: 'deepseek-r1-distill-qwen-32b',
53
+ pricing: {
54
+ currency: 'CNY',
55
+ input: 0,
56
+ output: 0,
57
+ },
58
+ },
59
+ {
60
+ contextWindowTokens: 32_768,
61
+ description:
62
+ 'Qwen2.5 是 Qwen 大型语言模型系列的最新成果。Qwen2.5 发布了从 0.5 到 720 亿参数不等的基础语言模型及指令调优语言模型。Qwen2.5 相比 Qwen2 带来了以下改进:\n显著增加知识量,在编程与数学领域的能力得到极大提升。\n在遵循指令、生成长文本、理解结构化数据 (例如,表格) 以及生成结构化输出特别是 JSON 方面有显著提升。对系统提示的多样性更具韧性,增强了聊天机器人中的角色扮演实现和条件设定。\n支持长上下文处理。\n支持超过 29 种语言的多语言功能,包括中文、英语、法语、西班牙语、葡萄牙语、德语、意大利语、俄语、日语、韩语、越南语、泰语、阿拉伯语等。',
63
+ displayName: 'Qwen2.5 72B Instruct',
64
+ enabled: true,
65
+ id: 'qwen2.5-72b-instruct',
66
+ pricing: {
67
+ currency: 'CNY',
68
+ input: 0,
69
+ output: 0,
70
+ },
71
+ },
72
+ {
73
+ contextWindowTokens: 32_768,
74
+ description:
75
+ 'Qwen2.5 是 Qwen 大型语言模型系列的最新成果。Qwen2.5 发布了从 0.5 到 720 亿参数不等的基础语言模型及指令调优语言模型。Qwen2.5 相比 Qwen2 带来了以下改进:\n显著增加知识量,在编程与数学领域的能力得到极大提升。\n在遵循指令、生成长文本、理解结构化数据 (例如,表格) 以及生成结构化输出特别是 JSON 方面有显著提升。对系统提示的多样性更具韧性,增强了聊天机器人中的角色扮演实现和条件设定。\n支持长上下文处理。\n支持超过 29 种语言的多语言功能,包括中文、英语、法语、西班牙语、葡萄牙语、德语、意大利语、俄语、日语、韩语、越南语、泰语、阿拉伯语等。',
76
+ displayName: 'Qwen2.5 32B Instruct',
77
+ enabled: true,
78
+ id: 'qwen2.5-32b-instruct',
79
+ pricing: {
80
+ currency: 'CNY',
81
+ input: 0,
82
+ output: 0,
83
+ },
84
+ },
85
+ {
86
+ contextWindowTokens: 32_768,
87
+ description:
88
+ 'Qwen2.5-Coder 是最新的代码专用 Qwen 大型语言模型系列。Qwen2.5-Coder 在 CodeQwen1.5 的基础上带来了以下改进:\n显著提升代码生成、代码推理和代码修复能力。\n支持真实世界应用,例如代码代理,增强编码能力和数学及一般能力。\n支持长上下文处理。',
89
+ displayName: 'Qwen2.5 Coder 32B Instruct',
90
+ enabled: true,
91
+ id: 'qwen2.5-coder-32b-instruct',
92
+ pricing: {
93
+ currency: 'CNY',
94
+ input: 0,
95
+ output: 0,
96
+ },
97
+ },
98
+ {
99
+ contextWindowTokens: 32_768,
100
+ description:
101
+ 'Qwen2.5 是 Qwen 大型语言模型系列的最新成果。Qwen2.5 发布了从 0.5 到 720 亿参数不等的基础语言模型及指令调优语言模型。Qwen2.5 相比 Qwen2 带来了以下改进:\n显著增加知识量,在编程与数学领域的能力得到极大提升。\n在遵循指令、生成长文本、理解结构化数据 (例如,表格) 以及生成结构化输出特别是 JSON 方面有显著提升。对系统提示的多样性更具韧性,增强了聊天机器人中的角色扮演实现和条件设定。\n支持长上下文处理。\n支持超过 29 种语言的多语言功能,包括中文、英语、法语、西班牙语、葡萄牙语、德语、意大利语、俄语、日语、韩语、越南语、泰语、阿拉伯语等。',
102
+ displayName: 'Qwen2.5 14B Instruct',
103
+ enabled: true,
104
+ id: 'qwen2.5-14b-instruct',
105
+ pricing: {
106
+ currency: 'CNY',
107
+ input: 0,
108
+ output: 0,
109
+ },
110
+ },
111
+ {
112
+ contextWindowTokens: 32_768,
113
+ description:
114
+ 'Qwen2.5 是 Qwen 大型语言模型系列的最新成果。Qwen2.5 发布了从 0.5 到 720 亿参数不等的基础语言模型及指令调优语言模型。Qwen2.5 相比 Qwen2 带来了以下改进:\n显著增加知识量,在编程与数学领域的能力得到极大提升。\n在遵循指令、生成长文本、理解结构化数据 (例如,表格) 以及生成结构化输出特别是 JSON 方面有显著提升。对系统提示的多样性更具韧性,增强了聊天机器人中的角色扮演实现和条件设定。\n支持长上下文处理。\n支持超过 29 种语言的多语言功能,包括中文、英语、法语、西班牙语、葡萄牙语、德语、意大利语、俄语、日语、韩语、越南语、泰语、阿拉伯语等。',
115
+ displayName: 'Qwen2.5 7B Instruct',
116
+ enabled: true,
117
+ id: 'qwen2.5-7b-instruct',
118
+ pricing: {
119
+ currency: 'CNY',
120
+ input: 0,
121
+ output: 0,
122
+ },
123
+ },
124
+ {
125
+ contextWindowTokens: 32_768,
126
+ description:
127
+ 'Qwen2 是 Qwen 团队推出的新一代大型语言模型系列。它基于 Transformer 架构,并采用 SwiGLU 激活函数、注意力 QKV 偏置(attention QKV bias)、群组查询注意力(group query attention)、滑动窗口注意力(mixture of sliding window attention)与全注意力的混合等技术。此外,Qwen 团队还改进了适应多种自然语言和代码的分词器。',
128
+ displayName: 'Qwen 2 72B Instruct',
129
+ enabled: true,
130
+ id: 'qwen2-72b-instruct',
131
+ pricing: {
132
+ currency: 'CNY',
133
+ input: 0,
134
+ output: 0,
135
+ },
136
+ },
137
+ {
138
+ contextWindowTokens: 32_768,
139
+ description:
140
+ 'Qwen2 是 Qwen 团队推出的新一代大型语言模型系列。它基于 Transformer 架构,并采用 SwiGLU 激活函数、注意力 QKV 偏置(attention QKV bias)、群组查询注意力(group query attention)、滑动窗口注意力(mixture of sliding window attention)与全注意力的混合等技术。此外,Qwen 团队还改进了适应多种自然语言和代码的分词器。',
141
+ displayName: 'Qwen 2 7B Instruct',
142
+ enabled: true,
143
+ id: 'qwen2-7b-instruct',
144
+ pricing: {
145
+ currency: 'CNY',
146
+ input: 0,
147
+ output: 0,
148
+ },
149
+ },
150
+ {
151
+ contextWindowTokens: 4096,
152
+ description:
153
+ 'Yi-1.5 是 Yi 的升级版本。 它使用 500B Tokens 的高质量语料库在 Yi 上持续进行预训练,并在 3M 个多样化的微调样本上进行微调。',
154
+ displayName: 'Yi-1.5 34B Chat',
155
+ enabled: true,
156
+ id: 'yi-1.5-34b-chat',
157
+ pricing: {
158
+ currency: 'CNY',
159
+ input: 0,
160
+ output: 0,
161
+ },
162
+ },
163
+ ],
164
+ checkModel: 'qwen2.5-7b-instruct',
165
+ description:
166
+ '为应用开发者提供高性能、易上手、安全可靠的大模型服务,覆盖从大模型开发到大模型服务化部署的全流程。',
167
+ id: 'infiniai',
168
+ modelList: { showModelFetcher: true },
169
+ modelsUrl: 'https://cloud.infini-ai.com/genstudio/model',
170
+ name: 'InfiniAI',
171
+ proxyUrl: {
172
+ placeholder: 'https://cloud.infini-ai.com/maas/v1',
173
+ },
174
+ settings: {
175
+ proxyUrl: {
176
+ placeholder: 'https://cloud.infini-ai.com/maas/v1',
177
+ },
178
+ sdkType: 'openai',
179
+ showModelFetcher: true,
180
+ },
181
+ url: 'https://cloud.infini-ai.com/genstudio',
182
+ };
183
+
184
+ export default InfiniAI;
@@ -0,0 +1,58 @@
1
+ import { ModelProviderCard } from '@/types/llm';
2
+
3
+ const Search1API: ModelProviderCard = {
4
+ chatModels: [
5
+ {
6
+ contextWindowTokens: 65_536,
7
+ description:
8
+ 'DeepSeek R1 70B 标准版,支持实时联网搜索,适合需要最新信息的对话和文本处理任务。',
9
+ displayName: 'DeepSeek R1 70B',
10
+ enabled: true,
11
+ id: 'deepseek-r1-70b-online',
12
+ },
13
+ {
14
+ contextWindowTokens: 65_536,
15
+ description:
16
+ 'DeepSeek R1 满血版,拥有 671B 参数,支持实时联网搜索,具有更强大的理解和生成能力。',
17
+ displayName: 'DeepSeek R1',
18
+ enabled: true,
19
+ id: 'deepseek-r1-online',
20
+ },
21
+ {
22
+ contextWindowTokens: 131_072,
23
+ description:
24
+ 'DeepSeek R1 70B 快速版,支持实时联网搜索,在保持模型性能的同时提供更快的响应速度。',
25
+ displayName: 'DeepSeek R1 70B Fast',
26
+ enabled: true,
27
+ id: 'deepseek-r1-70b-fast-online',
28
+ },
29
+ {
30
+ contextWindowTokens: 163_840,
31
+ description:
32
+ 'DeepSeek R1 满血快速版,支持实时联网搜索,结合了 671B 参数的强大能力和更快的响应速度。',
33
+ displayName: 'DeepSeek R1 Fast',
34
+ enabled: false,
35
+ id: 'deepseek-r1-fast-online',
36
+ },
37
+ ],
38
+ checkModel: 'deepseek-r1-70b-fast-online',
39
+ description:
40
+ 'Search1API 提供可根据需要自行联网的 DeepSeek 系列模型的访问,包括标准版和快速版本,支持多种参数规模的模型选择。',
41
+ id: 'search1api',
42
+ modelList: { showModelFetcher: true },
43
+ name: 'Search1API',
44
+ settings: {
45
+ proxyUrl: {
46
+ placeholder: 'https://api.search1api.com/v1',
47
+ },
48
+ sdkType: 'openai',
49
+ showModelFetcher: true,
50
+ smoothing: {
51
+ speed: 2,
52
+ text: true,
53
+ },
54
+ },
55
+ url: 'https://www.search1api.com',
56
+ };
57
+
58
+ export default Search1API;
@@ -3,6 +3,7 @@ import { and, eq } from 'drizzle-orm/expressions';
3
3
  import { afterEach, beforeEach, describe, expect, it } from 'vitest';
4
4
 
5
5
  import { LobeChatDatabase } from '@/database/type';
6
+ import { sleep } from '@/utils/sleep';
6
7
 
7
8
  import {
8
9
  NewKnowledgeBase,
@@ -93,6 +94,7 @@ describe('KnowledgeBaseModel', () => {
93
94
  describe('query', () => {
94
95
  it('should query knowledge bases for the user', async () => {
95
96
  await knowledgeBaseModel.create({ name: 'Test Group 1' });
97
+ await sleep(50);
96
98
  await knowledgeBaseModel.create({ name: 'Test Group 2' });
97
99
 
98
100
  const userGroups = await knowledgeBaseModel.query();
@@ -1,255 +1,13 @@
1
1
  // @vitest-environment node
2
- import OpenAI from 'openai';
3
- import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { ModelProvider } from '@/libs/agent-runtime';
3
+ import { testProvider } from '@/libs/agent-runtime/providerTestUtils';
4
4
 
5
- import {
6
- ChatStreamCallbacks,
7
- LobeOpenAICompatibleRuntime,
8
- ModelProvider,
9
- } from '@/libs/agent-runtime';
10
-
11
- import * as debugStreamModule from '../utils/debugStream';
12
5
  import { LobeAi21AI } from './index';
13
6
 
14
- const provider = ModelProvider.Ai21;
15
- const defaultBaseURL = 'https://api.ai21.com/studio/v1';
16
-
17
- const bizErrorType = 'ProviderBizError';
18
- const invalidErrorType = 'InvalidProviderAPIKey';
19
-
20
- // Mock the console.error to avoid polluting test output
21
- vi.spyOn(console, 'error').mockImplementation(() => {});
22
-
23
- let instance: LobeOpenAICompatibleRuntime;
24
-
25
- beforeEach(() => {
26
- instance = new LobeAi21AI({ apiKey: 'test' });
27
-
28
- // 使用 vi.spyOn 来模拟 chat.completions.create 方法
29
- vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(
30
- new ReadableStream() as any,
31
- );
32
- });
33
-
34
- afterEach(() => {
35
- vi.clearAllMocks();
36
- });
37
-
38
- describe('LobeAi21AI', () => {
39
- describe('init', () => {
40
- it('should correctly initialize with an API key', async () => {
41
- const instance = new LobeAi21AI({ apiKey: 'test_api_key' });
42
- expect(instance).toBeInstanceOf(LobeAi21AI);
43
- expect(instance.baseURL).toEqual(defaultBaseURL);
44
- });
45
- });
46
-
47
- describe('chat', () => {
48
- describe('Error', () => {
49
- it('should return OpenAIBizError with an openai error response when OpenAI.APIError is thrown', async () => {
50
- // Arrange
51
- const apiError = new OpenAI.APIError(
52
- 400,
53
- {
54
- status: 400,
55
- error: {
56
- message: 'Bad Request',
57
- },
58
- },
59
- 'Error message',
60
- {},
61
- );
62
-
63
- vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
64
-
65
- // Act
66
- try {
67
- await instance.chat({
68
- messages: [{ content: 'Hello', role: 'user' }],
69
- model: 'jamba-1.5-mini',
70
- temperature: 0,
71
- });
72
- } catch (e) {
73
- expect(e).toEqual({
74
- endpoint: defaultBaseURL,
75
- error: {
76
- error: { message: 'Bad Request' },
77
- status: 400,
78
- },
79
- errorType: bizErrorType,
80
- provider,
81
- });
82
- }
83
- });
84
-
85
- it('should throw AgentRuntimeError with NoOpenAIAPIKey if no apiKey is provided', async () => {
86
- try {
87
- new LobeAi21AI({});
88
- } catch (e) {
89
- expect(e).toEqual({ errorType: invalidErrorType });
90
- }
91
- });
92
-
93
- it('should return OpenAIBizError with the cause when OpenAI.APIError is thrown with cause', async () => {
94
- // Arrange
95
- const errorInfo = {
96
- stack: 'abc',
97
- cause: {
98
- message: 'api is undefined',
99
- },
100
- };
101
- const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
102
-
103
- vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
104
-
105
- // Act
106
- try {
107
- await instance.chat({
108
- messages: [{ content: 'Hello', role: 'user' }],
109
- model: 'jamba-1.5-mini',
110
- temperature: 0,
111
- });
112
- } catch (e) {
113
- expect(e).toEqual({
114
- endpoint: defaultBaseURL,
115
- error: {
116
- cause: { message: 'api is undefined' },
117
- stack: 'abc',
118
- },
119
- errorType: bizErrorType,
120
- provider,
121
- });
122
- }
123
- });
124
-
125
- it('should return OpenAIBizError with an cause response with desensitize Url', async () => {
126
- // Arrange
127
- const errorInfo = {
128
- stack: 'abc',
129
- cause: { message: 'api is undefined' },
130
- };
131
- const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
132
-
133
- instance = new LobeAi21AI({
134
- apiKey: 'test',
135
-
136
- baseURL: 'https://api.abc.com/v1',
137
- });
138
-
139
- vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
140
-
141
- // Act
142
- try {
143
- await instance.chat({
144
- messages: [{ content: 'Hello', role: 'user' }],
145
- model: 'jamba-1.5-mini',
146
- temperature: 0,
147
- });
148
- } catch (e) {
149
- expect(e).toEqual({
150
- endpoint: 'https://api.***.com/v1',
151
- error: {
152
- cause: { message: 'api is undefined' },
153
- stack: 'abc',
154
- },
155
- errorType: bizErrorType,
156
- provider,
157
- });
158
- }
159
- });
160
-
161
- it('should throw an InvalidAi21APIKey error type on 401 status code', async () => {
162
- // Mock the API call to simulate a 401 error
163
- const error = new Error('Unauthorized') as any;
164
- error.status = 401;
165
- vi.mocked(instance['client'].chat.completions.create).mockRejectedValue(error);
166
-
167
- try {
168
- await instance.chat({
169
- messages: [{ content: 'Hello', role: 'user' }],
170
- model: 'jamba-1.5-mini',
171
- temperature: 0,
172
- });
173
- } catch (e) {
174
- // Expect the chat method to throw an error with InvalidAi21APIKey
175
- expect(e).toEqual({
176
- endpoint: defaultBaseURL,
177
- error: new Error('Unauthorized'),
178
- errorType: invalidErrorType,
179
- provider,
180
- });
181
- }
182
- });
183
-
184
- it('should return AgentRuntimeError for non-OpenAI errors', async () => {
185
- // Arrange
186
- const genericError = new Error('Generic Error');
187
-
188
- vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(genericError);
189
-
190
- // Act
191
- try {
192
- await instance.chat({
193
- messages: [{ content: 'Hello', role: 'user' }],
194
- model: 'jamba-1.5-mini',
195
- temperature: 0,
196
- });
197
- } catch (e) {
198
- expect(e).toEqual({
199
- endpoint: defaultBaseURL,
200
- errorType: 'AgentRuntimeError',
201
- provider,
202
- error: {
203
- name: genericError.name,
204
- cause: genericError.cause,
205
- message: genericError.message,
206
- stack: genericError.stack,
207
- },
208
- });
209
- }
210
- });
211
- });
212
-
213
- describe('DEBUG', () => {
214
- it('should call debugStream and return StreamingTextResponse when DEBUG_AI21_CHAT_COMPLETION is 1', async () => {
215
- // Arrange
216
- const mockProdStream = new ReadableStream() as any; // 模拟的 prod 流
217
- const mockDebugStream = new ReadableStream({
218
- start(controller) {
219
- controller.enqueue('Debug stream content');
220
- controller.close();
221
- },
222
- }) as any;
223
- mockDebugStream.toReadableStream = () => mockDebugStream; // 添加 toReadableStream 方法
224
-
225
- // 模拟 chat.completions.create 返回值,包括模拟的 tee 方法
226
- (instance['client'].chat.completions.create as Mock).mockResolvedValue({
227
- tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
228
- });
229
-
230
- // 保存原始环境变量值
231
- const originalDebugValue = process.env.DEBUG_AI21_CHAT_COMPLETION;
232
-
233
- // 模拟环境变量
234
- process.env.DEBUG_AI21_CHAT_COMPLETION = '1';
235
- vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
236
-
237
- // 执行测试
238
- // 运行你的测试函数,确保它会在条件满足时调用 debugStream
239
- // 假设的测试函数调用,你可能需要根据实际情况调整
240
- await instance.chat({
241
- messages: [{ content: 'Hello', role: 'user' }],
242
- model: 'jamba-1.5-mini',
243
- stream: true,
244
- temperature: 0,
245
- });
246
-
247
- // 验证 debugStream 被调用
248
- expect(debugStreamModule.debugStream).toHaveBeenCalled();
249
-
250
- // 恢复原始环境变量值
251
- process.env.DEBUG_AI21_CHAT_COMPLETION = originalDebugValue;
252
- });
253
- });
254
- });
7
+ testProvider({
8
+ Runtime: LobeAi21AI,
9
+ provider: ModelProvider.Ai21,
10
+ defaultBaseURL: 'https://api.ai21.com/studio/v1',
11
+ chatDebugEnv: 'DEBUG_AI21_CHAT_COMPLETION',
12
+ chatModel: 'deepseek-r1',
255
13
  });