@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.
- package/.env.example +4 -0
- package/CHANGELOG.md +51 -0
- package/Dockerfile +5 -1
- package/Dockerfile.database +5 -1
- package/Dockerfile.pglite +3 -1
- package/README.md +4 -2
- package/README.zh-CN.md +4 -2
- package/changelog/v1.json +18 -0
- package/docs/self-hosting/environment-variables/model-provider.mdx +13 -0
- package/docs/self-hosting/environment-variables/model-provider.zh-CN.mdx +14 -0
- package/docs/usage/providers/infiniai.mdx +29 -0
- package/docs/usage/providers/infiniai.zh-CN.mdx +29 -0
- package/locales/ar/models.json +30 -0
- package/locales/ar/providers.json +3 -0
- package/locales/bg-BG/models.json +30 -0
- package/locales/bg-BG/providers.json +3 -0
- package/locales/de-DE/models.json +30 -0
- package/locales/de-DE/providers.json +3 -0
- package/locales/en-US/models.json +30 -0
- package/locales/en-US/providers.json +3 -0
- package/locales/es-ES/models.json +30 -0
- package/locales/es-ES/providers.json +3 -0
- package/locales/fa-IR/models.json +30 -0
- package/locales/fa-IR/providers.json +3 -0
- package/locales/fr-FR/models.json +30 -0
- package/locales/fr-FR/providers.json +3 -0
- package/locales/it-IT/models.json +30 -0
- package/locales/it-IT/providers.json +3 -0
- package/locales/ja-JP/models.json +22 -0
- package/locales/ja-JP/providers.json +3 -0
- package/locales/ko-KR/models.json +30 -0
- package/locales/ko-KR/providers.json +3 -0
- package/locales/nl-NL/models.json +30 -0
- package/locales/nl-NL/providers.json +3 -0
- package/locales/pl-PL/models.json +30 -0
- package/locales/pl-PL/providers.json +3 -0
- package/locales/pt-BR/models.json +30 -0
- package/locales/pt-BR/providers.json +3 -0
- package/locales/ru-RU/models.json +30 -0
- package/locales/ru-RU/providers.json +3 -0
- package/locales/tr-TR/models.json +30 -0
- package/locales/tr-TR/providers.json +3 -0
- package/locales/vi-VN/models.json +30 -0
- package/locales/vi-VN/providers.json +3 -0
- package/locales/zh-CN/models.json +30 -0
- package/locales/zh-CN/providers.json +3 -0
- package/locales/zh-TW/models.json +19 -0
- package/locales/zh-TW/providers.json +3 -0
- package/package.json +3 -3
- package/packages/web-crawler/src/utils/htmlToMarkdown.test.ts +1 -1
- package/src/app/[variants]/(main)/settings/llm/ProviderList/providers.tsx +5 -1
- package/src/config/aiModels/index.ts +6 -0
- package/src/config/aiModels/infiniai.ts +307 -0
- package/src/config/aiModels/search1api.ts +63 -0
- package/src/config/llm.ts +12 -0
- package/src/config/modelProviders/index.ts +8 -0
- package/src/config/modelProviders/infiniai.ts +184 -0
- package/src/config/modelProviders/search1api.ts +58 -0
- package/src/database/models/__tests__/knowledgeBase.test.ts +2 -0
- package/src/libs/agent-runtime/ai21/index.test.ts +8 -250
- package/src/libs/agent-runtime/ai360/index.test.ts +8 -250
- package/src/libs/agent-runtime/anthropic/index.ts +5 -1
- package/src/libs/agent-runtime/deepseek/index.test.ts +119 -335
- package/src/libs/agent-runtime/fireworksai/index.test.ts +8 -247
- package/src/libs/agent-runtime/giteeai/index.test.ts +8 -250
- package/src/libs/agent-runtime/github/index.test.ts +8 -207
- package/src/libs/agent-runtime/infiniai/index.ts +43 -0
- package/src/libs/agent-runtime/internlm/index.test.ts +8 -250
- package/src/libs/agent-runtime/lmstudio/index.test.ts +8 -247
- package/src/libs/agent-runtime/moonshot/index.test.ts +10 -243
- package/src/libs/agent-runtime/novita/index.test.ts +9 -221
- package/src/libs/agent-runtime/ollama/index.test.ts +4 -4
- package/src/libs/agent-runtime/openrouter/index.test.ts +12 -217
- package/src/libs/agent-runtime/ppio/index.test.ts +11 -220
- package/src/libs/agent-runtime/providerTestUtils.ts +6 -6
- package/src/libs/agent-runtime/qwen/index.test.ts +10 -242
- package/src/libs/agent-runtime/runtimeMap.ts +4 -0
- package/src/libs/agent-runtime/search1api/index.ts +64 -0
- package/src/libs/agent-runtime/sensenova/index.test.ts +10 -242
- package/src/libs/agent-runtime/spark/index.test.ts +7 -242
- package/src/libs/agent-runtime/stepfun/index.test.ts +7 -242
- package/src/libs/agent-runtime/taichu/index.test.ts +12 -220
- package/src/libs/agent-runtime/types/type.ts +2 -0
- package/src/libs/agent-runtime/upstage/index.test.ts +7 -250
- package/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.test.ts +2 -2
- package/src/libs/agent-runtime/xai/index.test.ts +8 -250
- package/src/services/chat.ts +1 -4
- package/src/types/user/settings/keyVaults.ts +2 -0
- package/src/utils/fetch/__tests__/parseToolCalls.test.ts +9 -11
- package/src/utils/server/jwt.test.ts +1 -1
- package/vitest.server.config.ts +3 -1
@@ -1,251 +1,19 @@
|
|
1
1
|
// @vitest-environment node
|
2
|
-
import OpenAI from 'openai';
|
3
|
-
import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
4
|
-
|
5
|
-
import { LobeOpenAICompatibleRuntime } from '@/libs/agent-runtime';
|
6
2
|
import { ModelProvider } from '@/libs/agent-runtime';
|
7
|
-
import {
|
3
|
+
import { testProvider } from '@/libs/agent-runtime/providerTestUtils';
|
8
4
|
|
9
|
-
import * as debugStreamModule from '../utils/debugStream';
|
10
5
|
import { LobeSenseNovaAI } from './index';
|
11
6
|
|
12
7
|
const provider = ModelProvider.SenseNova;
|
13
8
|
const defaultBaseURL = 'https://api.sensenova.cn/compatible-mode/v1';
|
14
|
-
const bizErrorType = AgentRuntimeErrorType.ProviderBizError;
|
15
|
-
const invalidErrorType = AgentRuntimeErrorType.InvalidProviderAPIKey;
|
16
|
-
|
17
|
-
// Mock the console.error to avoid polluting test output
|
18
|
-
vi.spyOn(console, 'error').mockImplementation(() => {});
|
19
|
-
|
20
|
-
let instance: LobeOpenAICompatibleRuntime;
|
21
|
-
|
22
|
-
beforeEach(() => {
|
23
|
-
instance = new LobeSenseNovaAI({ apiKey: 'test' });
|
24
|
-
|
25
|
-
// 使用 vi.spyOn 来模拟 chat.completions.create 方法
|
26
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(
|
27
|
-
new ReadableStream() as any,
|
28
|
-
);
|
29
|
-
});
|
30
|
-
|
31
|
-
afterEach(() => {
|
32
|
-
vi.clearAllMocks();
|
33
|
-
});
|
34
|
-
|
35
|
-
describe('LobeSenseNovaAI', () => {
|
36
|
-
describe('init', () => {
|
37
|
-
it('should correctly initialize with an API key', async () => {
|
38
|
-
const instance = new LobeSenseNovaAI({ apiKey: 'test_api_key' });
|
39
|
-
expect(instance).toBeInstanceOf(LobeSenseNovaAI);
|
40
|
-
expect(instance.baseURL).toEqual(defaultBaseURL);
|
41
|
-
});
|
42
|
-
});
|
43
|
-
|
44
|
-
describe('chat', () => {
|
45
|
-
describe('Error', () => {
|
46
|
-
it('should return QwenBizError with an openai error response when OpenAI.APIError is thrown', async () => {
|
47
|
-
// Arrange
|
48
|
-
const apiError = new OpenAI.APIError(
|
49
|
-
400,
|
50
|
-
{
|
51
|
-
status: 400,
|
52
|
-
error: {
|
53
|
-
message: 'Bad Request',
|
54
|
-
},
|
55
|
-
},
|
56
|
-
'Error message',
|
57
|
-
{},
|
58
|
-
);
|
59
|
-
|
60
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
61
|
-
|
62
|
-
// Act
|
63
|
-
try {
|
64
|
-
await instance.chat({
|
65
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
66
|
-
model: 'max-32k',
|
67
|
-
temperature: 0.999,
|
68
|
-
});
|
69
|
-
} catch (e) {
|
70
|
-
expect(e).toEqual({
|
71
|
-
endpoint: defaultBaseURL,
|
72
|
-
error: {
|
73
|
-
error: { message: 'Bad Request' },
|
74
|
-
status: 400,
|
75
|
-
},
|
76
|
-
errorType: bizErrorType,
|
77
|
-
provider,
|
78
|
-
});
|
79
|
-
}
|
80
|
-
});
|
81
|
-
|
82
|
-
it('should throw AgentRuntimeError with InvalidQwenAPIKey if no apiKey is provided', async () => {
|
83
|
-
try {
|
84
|
-
new LobeSenseNovaAI({});
|
85
|
-
} catch (e) {
|
86
|
-
expect(e).toEqual({ errorType: invalidErrorType });
|
87
|
-
}
|
88
|
-
});
|
89
|
-
|
90
|
-
it('should return QwenBizError with the cause when OpenAI.APIError is thrown with cause', async () => {
|
91
|
-
// Arrange
|
92
|
-
const errorInfo = {
|
93
|
-
stack: 'abc',
|
94
|
-
cause: {
|
95
|
-
message: 'api is undefined',
|
96
|
-
},
|
97
|
-
};
|
98
|
-
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
99
|
-
|
100
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
101
|
-
|
102
|
-
// Act
|
103
|
-
try {
|
104
|
-
await instance.chat({
|
105
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
106
|
-
model: 'max-32k',
|
107
|
-
temperature: 0.999,
|
108
|
-
});
|
109
|
-
} catch (e) {
|
110
|
-
expect(e).toEqual({
|
111
|
-
endpoint: defaultBaseURL,
|
112
|
-
error: {
|
113
|
-
cause: { message: 'api is undefined' },
|
114
|
-
stack: 'abc',
|
115
|
-
},
|
116
|
-
errorType: bizErrorType,
|
117
|
-
provider,
|
118
|
-
});
|
119
|
-
}
|
120
|
-
});
|
121
|
-
|
122
|
-
it('should return QwenBizError with an cause response with desensitize Url', async () => {
|
123
|
-
// Arrange
|
124
|
-
const errorInfo = {
|
125
|
-
stack: 'abc',
|
126
|
-
cause: { message: 'api is undefined' },
|
127
|
-
};
|
128
|
-
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
129
|
-
|
130
|
-
instance = new LobeSenseNovaAI({
|
131
|
-
apiKey: 'test',
|
132
|
-
|
133
|
-
baseURL: 'https://api.abc.com/v1',
|
134
|
-
});
|
135
|
-
|
136
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
137
|
-
|
138
|
-
// Act
|
139
|
-
try {
|
140
|
-
await instance.chat({
|
141
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
142
|
-
model: 'max-32k',
|
143
|
-
temperature: 0.999,
|
144
|
-
});
|
145
|
-
} catch (e) {
|
146
|
-
expect(e).toEqual({
|
147
|
-
endpoint: 'https://api.***.com/v1',
|
148
|
-
error: {
|
149
|
-
cause: { message: 'api is undefined' },
|
150
|
-
stack: 'abc',
|
151
|
-
},
|
152
|
-
errorType: bizErrorType,
|
153
|
-
provider,
|
154
|
-
});
|
155
|
-
}
|
156
|
-
});
|
157
|
-
|
158
|
-
it('should throw an InvalidQwenAPIKey error type on 401 status code', async () => {
|
159
|
-
// Mock the API call to simulate a 401 error
|
160
|
-
const error = new Error('InvalidApiKey') as any;
|
161
|
-
error.status = 401;
|
162
|
-
vi.mocked(instance['client'].chat.completions.create).mockRejectedValue(error);
|
163
|
-
|
164
|
-
try {
|
165
|
-
await instance.chat({
|
166
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
167
|
-
model: 'max-32k',
|
168
|
-
temperature: 0.999,
|
169
|
-
});
|
170
|
-
} catch (e) {
|
171
|
-
expect(e).toEqual({
|
172
|
-
endpoint: defaultBaseURL,
|
173
|
-
error: new Error('InvalidApiKey'),
|
174
|
-
errorType: invalidErrorType,
|
175
|
-
provider,
|
176
|
-
});
|
177
|
-
}
|
178
|
-
});
|
179
|
-
|
180
|
-
it('should return AgentRuntimeError for non-OpenAI errors', async () => {
|
181
|
-
// Arrange
|
182
|
-
const genericError = new Error('Generic Error');
|
183
|
-
|
184
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(genericError);
|
185
|
-
|
186
|
-
// Act
|
187
|
-
try {
|
188
|
-
await instance.chat({
|
189
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
190
|
-
model: 'max-32k',
|
191
|
-
temperature: 0.999,
|
192
|
-
});
|
193
|
-
} catch (e) {
|
194
|
-
expect(e).toEqual({
|
195
|
-
endpoint: defaultBaseURL,
|
196
|
-
errorType: 'AgentRuntimeError',
|
197
|
-
provider,
|
198
|
-
error: {
|
199
|
-
name: genericError.name,
|
200
|
-
cause: genericError.cause,
|
201
|
-
message: genericError.message,
|
202
|
-
stack: genericError.stack,
|
203
|
-
},
|
204
|
-
});
|
205
|
-
}
|
206
|
-
});
|
207
|
-
});
|
208
|
-
|
209
|
-
describe('DEBUG', () => {
|
210
|
-
it('should call debugStream and return StreamingTextResponse when DEBUG_SENSENOVA_CHAT_COMPLETION is 1', async () => {
|
211
|
-
// Arrange
|
212
|
-
const mockProdStream = new ReadableStream() as any; // 模拟的 prod 流
|
213
|
-
const mockDebugStream = new ReadableStream({
|
214
|
-
start(controller) {
|
215
|
-
controller.enqueue('Debug stream content');
|
216
|
-
controller.close();
|
217
|
-
},
|
218
|
-
}) as any;
|
219
|
-
mockDebugStream.toReadableStream = () => mockDebugStream; // 添加 toReadableStream 方法
|
220
|
-
|
221
|
-
// 模拟 chat.completions.create 返回值,包括模拟的 tee 方法
|
222
|
-
(instance['client'].chat.completions.create as Mock).mockResolvedValue({
|
223
|
-
tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
|
224
|
-
});
|
225
|
-
|
226
|
-
// 保存原始环境变量值
|
227
|
-
const originalDebugValue = process.env.DEBUG_SENSENOVA_CHAT_COMPLETION;
|
228
|
-
|
229
|
-
// 模拟环境变量
|
230
|
-
process.env.DEBUG_SENSENOVA_CHAT_COMPLETION = '1';
|
231
|
-
vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
|
232
|
-
|
233
|
-
// 执行测试
|
234
|
-
// 运行你的测试函数,确保它会在条件满足时调用 debugStream
|
235
|
-
// 假设的测试函数调用,你可能需要根据实际情况调整
|
236
|
-
await instance.chat({
|
237
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
238
|
-
model: 'max-32k',
|
239
|
-
stream: true,
|
240
|
-
temperature: 0.999,
|
241
|
-
});
|
242
|
-
|
243
|
-
// 验证 debugStream 被调用
|
244
|
-
expect(debugStreamModule.debugStream).toHaveBeenCalled();
|
245
9
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
10
|
+
testProvider({
|
11
|
+
Runtime: LobeSenseNovaAI,
|
12
|
+
provider,
|
13
|
+
defaultBaseURL,
|
14
|
+
chatDebugEnv: 'DEBUG_SENSENOVA_CHAT_COMPLETION',
|
15
|
+
chatModel: 'deepseek-r1',
|
16
|
+
test: {
|
17
|
+
skipAPICall: true,
|
18
|
+
},
|
251
19
|
});
|
@@ -1,251 +1,16 @@
|
|
1
1
|
// @vitest-environment node
|
2
|
-
import OpenAI from 'openai';
|
3
|
-
import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
4
|
-
|
5
|
-
import { LobeOpenAICompatibleRuntime } from '@/libs/agent-runtime';
|
6
2
|
import { ModelProvider } from '@/libs/agent-runtime';
|
7
|
-
import {
|
3
|
+
import { testProvider } from '@/libs/agent-runtime/providerTestUtils';
|
8
4
|
|
9
|
-
import * as debugStreamModule from '../utils/debugStream';
|
10
5
|
import { LobeSparkAI } from './index';
|
11
6
|
|
12
7
|
const provider = ModelProvider.Spark;
|
13
8
|
const defaultBaseURL = 'https://spark-api-open.xf-yun.com/v1';
|
14
|
-
const bizErrorType = AgentRuntimeErrorType.ProviderBizError;
|
15
|
-
const invalidErrorType = AgentRuntimeErrorType.InvalidProviderAPIKey;
|
16
|
-
|
17
|
-
// Mock the console.error to avoid polluting test output
|
18
|
-
vi.spyOn(console, 'error').mockImplementation(() => {});
|
19
|
-
|
20
|
-
let instance: LobeOpenAICompatibleRuntime;
|
21
|
-
|
22
|
-
beforeEach(() => {
|
23
|
-
instance = new LobeSparkAI({ apiKey: 'test' });
|
24
|
-
|
25
|
-
// 使用 vi.spyOn 来模拟 chat.completions.create 方法
|
26
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(
|
27
|
-
new ReadableStream() as any,
|
28
|
-
);
|
29
|
-
});
|
30
|
-
|
31
|
-
afterEach(() => {
|
32
|
-
vi.clearAllMocks();
|
33
|
-
});
|
34
|
-
|
35
|
-
describe('LobeSparkAI', () => {
|
36
|
-
describe('init', () => {
|
37
|
-
it('should correctly initialize with an API key', async () => {
|
38
|
-
const instance = new LobeSparkAI({ apiKey: 'test_api_key' });
|
39
|
-
expect(instance).toBeInstanceOf(LobeSparkAI);
|
40
|
-
expect(instance.baseURL).toEqual(defaultBaseURL);
|
41
|
-
});
|
42
|
-
});
|
43
|
-
|
44
|
-
describe('chat', () => {
|
45
|
-
describe('Error', () => {
|
46
|
-
it('should return QwenBizError with an openai error response when OpenAI.APIError is thrown', async () => {
|
47
|
-
// Arrange
|
48
|
-
const apiError = new OpenAI.APIError(
|
49
|
-
400,
|
50
|
-
{
|
51
|
-
status: 400,
|
52
|
-
error: {
|
53
|
-
message: 'Bad Request',
|
54
|
-
},
|
55
|
-
},
|
56
|
-
'Error message',
|
57
|
-
{},
|
58
|
-
);
|
59
|
-
|
60
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
61
|
-
|
62
|
-
// Act
|
63
|
-
try {
|
64
|
-
await instance.chat({
|
65
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
66
|
-
model: 'max-32k',
|
67
|
-
temperature: 0.999,
|
68
|
-
});
|
69
|
-
} catch (e) {
|
70
|
-
expect(e).toEqual({
|
71
|
-
endpoint: defaultBaseURL,
|
72
|
-
error: {
|
73
|
-
error: { message: 'Bad Request' },
|
74
|
-
status: 400,
|
75
|
-
},
|
76
|
-
errorType: bizErrorType,
|
77
|
-
provider,
|
78
|
-
});
|
79
|
-
}
|
80
|
-
});
|
81
|
-
|
82
|
-
it('should throw AgentRuntimeError with InvalidQwenAPIKey if no apiKey is provided', async () => {
|
83
|
-
try {
|
84
|
-
new LobeSparkAI({});
|
85
|
-
} catch (e) {
|
86
|
-
expect(e).toEqual({ errorType: invalidErrorType });
|
87
|
-
}
|
88
|
-
});
|
89
|
-
|
90
|
-
it('should return QwenBizError with the cause when OpenAI.APIError is thrown with cause', async () => {
|
91
|
-
// Arrange
|
92
|
-
const errorInfo = {
|
93
|
-
stack: 'abc',
|
94
|
-
cause: {
|
95
|
-
message: 'api is undefined',
|
96
|
-
},
|
97
|
-
};
|
98
|
-
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
99
|
-
|
100
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
101
|
-
|
102
|
-
// Act
|
103
|
-
try {
|
104
|
-
await instance.chat({
|
105
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
106
|
-
model: 'max-32k',
|
107
|
-
temperature: 0.999,
|
108
|
-
});
|
109
|
-
} catch (e) {
|
110
|
-
expect(e).toEqual({
|
111
|
-
endpoint: defaultBaseURL,
|
112
|
-
error: {
|
113
|
-
cause: { message: 'api is undefined' },
|
114
|
-
stack: 'abc',
|
115
|
-
},
|
116
|
-
errorType: bizErrorType,
|
117
|
-
provider,
|
118
|
-
});
|
119
|
-
}
|
120
|
-
});
|
121
|
-
|
122
|
-
it('should return QwenBizError with an cause response with desensitize Url', async () => {
|
123
|
-
// Arrange
|
124
|
-
const errorInfo = {
|
125
|
-
stack: 'abc',
|
126
|
-
cause: { message: 'api is undefined' },
|
127
|
-
};
|
128
|
-
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
129
|
-
|
130
|
-
instance = new LobeSparkAI({
|
131
|
-
apiKey: 'test',
|
132
|
-
|
133
|
-
baseURL: 'https://api.abc.com/v1',
|
134
|
-
});
|
135
|
-
|
136
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
137
|
-
|
138
|
-
// Act
|
139
|
-
try {
|
140
|
-
await instance.chat({
|
141
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
142
|
-
model: 'max-32k',
|
143
|
-
temperature: 0.999,
|
144
|
-
});
|
145
|
-
} catch (e) {
|
146
|
-
expect(e).toEqual({
|
147
|
-
endpoint: 'https://api.***.com/v1',
|
148
|
-
error: {
|
149
|
-
cause: { message: 'api is undefined' },
|
150
|
-
stack: 'abc',
|
151
|
-
},
|
152
|
-
errorType: bizErrorType,
|
153
|
-
provider,
|
154
|
-
});
|
155
|
-
}
|
156
|
-
});
|
157
|
-
|
158
|
-
it('should throw an InvalidQwenAPIKey error type on 401 status code', async () => {
|
159
|
-
// Mock the API call to simulate a 401 error
|
160
|
-
const error = new Error('InvalidApiKey') as any;
|
161
|
-
error.status = 401;
|
162
|
-
vi.mocked(instance['client'].chat.completions.create).mockRejectedValue(error);
|
163
|
-
|
164
|
-
try {
|
165
|
-
await instance.chat({
|
166
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
167
|
-
model: 'max-32k',
|
168
|
-
temperature: 0.999,
|
169
|
-
});
|
170
|
-
} catch (e) {
|
171
|
-
expect(e).toEqual({
|
172
|
-
endpoint: defaultBaseURL,
|
173
|
-
error: new Error('InvalidApiKey'),
|
174
|
-
errorType: invalidErrorType,
|
175
|
-
provider,
|
176
|
-
});
|
177
|
-
}
|
178
|
-
});
|
179
|
-
|
180
|
-
it('should return AgentRuntimeError for non-OpenAI errors', async () => {
|
181
|
-
// Arrange
|
182
|
-
const genericError = new Error('Generic Error');
|
183
|
-
|
184
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(genericError);
|
185
|
-
|
186
|
-
// Act
|
187
|
-
try {
|
188
|
-
await instance.chat({
|
189
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
190
|
-
model: 'max-32k',
|
191
|
-
temperature: 0.999,
|
192
|
-
});
|
193
|
-
} catch (e) {
|
194
|
-
expect(e).toEqual({
|
195
|
-
endpoint: defaultBaseURL,
|
196
|
-
errorType: 'AgentRuntimeError',
|
197
|
-
provider,
|
198
|
-
error: {
|
199
|
-
name: genericError.name,
|
200
|
-
cause: genericError.cause,
|
201
|
-
message: genericError.message,
|
202
|
-
stack: genericError.stack,
|
203
|
-
},
|
204
|
-
});
|
205
|
-
}
|
206
|
-
});
|
207
|
-
});
|
208
|
-
|
209
|
-
describe('DEBUG', () => {
|
210
|
-
it('should call debugStream and return StreamingTextResponse when DEBUG_SPARK_CHAT_COMPLETION is 1', async () => {
|
211
|
-
// Arrange
|
212
|
-
const mockProdStream = new ReadableStream() as any; // 模拟的 prod 流
|
213
|
-
const mockDebugStream = new ReadableStream({
|
214
|
-
start(controller) {
|
215
|
-
controller.enqueue('Debug stream content');
|
216
|
-
controller.close();
|
217
|
-
},
|
218
|
-
}) as any;
|
219
|
-
mockDebugStream.toReadableStream = () => mockDebugStream; // 添加 toReadableStream 方法
|
220
|
-
|
221
|
-
// 模拟 chat.completions.create 返回值,包括模拟的 tee 方法
|
222
|
-
(instance['client'].chat.completions.create as Mock).mockResolvedValue({
|
223
|
-
tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
|
224
|
-
});
|
225
|
-
|
226
|
-
// 保存原始环境变量值
|
227
|
-
const originalDebugValue = process.env.DEBUG_SPARK_CHAT_COMPLETION;
|
228
|
-
|
229
|
-
// 模拟环境变量
|
230
|
-
process.env.DEBUG_SPARK_CHAT_COMPLETION = '1';
|
231
|
-
vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
|
232
|
-
|
233
|
-
// 执行测试
|
234
|
-
// 运行你的测试函数,确保它会在条件满足时调用 debugStream
|
235
|
-
// 假设的测试函数调用,你可能需要根据实际情况调整
|
236
|
-
await instance.chat({
|
237
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
238
|
-
model: 'max-32k',
|
239
|
-
stream: true,
|
240
|
-
temperature: 0.999,
|
241
|
-
});
|
242
|
-
|
243
|
-
// 验证 debugStream 被调用
|
244
|
-
expect(debugStreamModule.debugStream).toHaveBeenCalled();
|
245
9
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
10
|
+
testProvider({
|
11
|
+
Runtime: LobeSparkAI,
|
12
|
+
provider,
|
13
|
+
defaultBaseURL,
|
14
|
+
chatDebugEnv: 'DEBUG_SPARK_CHAT_COMPLETION',
|
15
|
+
chatModel: 'spark',
|
251
16
|
});
|