@lobehub/chat 1.73.2 → 1.74.0
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 +26 -0
- package/Dockerfile +5 -1
- package/Dockerfile.database +5 -1
- package/Dockerfile.pglite +3 -1
- package/README.md +1 -0
- package/README.zh-CN.md +1 -0
- package/changelog/v1.json +9 -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 +62 -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,19 +1,23 @@
|
|
1
1
|
// @vitest-environment node
|
2
|
-
import OpenAI from 'openai';
|
3
2
|
import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
4
3
|
|
5
4
|
import { LobeOpenAICompatibleRuntime } from '@/libs/agent-runtime';
|
6
5
|
import { ModelProvider } from '@/libs/agent-runtime';
|
7
|
-
import {
|
6
|
+
import { testProvider } from '@/libs/agent-runtime/providerTestUtils';
|
8
7
|
|
9
|
-
import * as debugStreamModule from '../utils/debugStream';
|
10
8
|
import models from './fixtures/models.json';
|
11
9
|
import { LobeNovitaAI } from './index';
|
12
10
|
|
13
11
|
const provider = ModelProvider.Novita;
|
14
12
|
const defaultBaseURL = 'https://api.novita.ai/v3/openai';
|
15
|
-
|
16
|
-
|
13
|
+
|
14
|
+
testProvider({
|
15
|
+
Runtime: LobeNovitaAI,
|
16
|
+
provider,
|
17
|
+
defaultBaseURL,
|
18
|
+
chatDebugEnv: 'DEBUG_NOVITA_CHAT_COMPLETION',
|
19
|
+
chatModel: 'gpt-3.5-turbo',
|
20
|
+
});
|
17
21
|
|
18
22
|
// Mock the console.error to avoid polluting test output
|
19
23
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
@@ -35,222 +39,6 @@ afterEach(() => {
|
|
35
39
|
});
|
36
40
|
|
37
41
|
describe('NovitaAI', () => {
|
38
|
-
describe('init', () => {
|
39
|
-
it('should correctly initialize with an API key', async () => {
|
40
|
-
const instance = new LobeNovitaAI({ apiKey: 'test_api_key' });
|
41
|
-
expect(instance).toBeInstanceOf(LobeNovitaAI);
|
42
|
-
expect(instance.baseURL).toEqual(defaultBaseURL);
|
43
|
-
});
|
44
|
-
});
|
45
|
-
|
46
|
-
describe('chat', () => {
|
47
|
-
describe('Error', () => {
|
48
|
-
it('should return Error with an openai error response when OpenAI.APIError is thrown', async () => {
|
49
|
-
// Arrange
|
50
|
-
const apiError = new OpenAI.APIError(
|
51
|
-
400,
|
52
|
-
{
|
53
|
-
status: 400,
|
54
|
-
error: {
|
55
|
-
message: 'Bad Request',
|
56
|
-
},
|
57
|
-
},
|
58
|
-
'Error message',
|
59
|
-
{},
|
60
|
-
);
|
61
|
-
|
62
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
63
|
-
|
64
|
-
// Act
|
65
|
-
try {
|
66
|
-
await instance.chat({
|
67
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
68
|
-
model: 'meta-llama/llama-3-8b-instruct',
|
69
|
-
temperature: 0.999,
|
70
|
-
});
|
71
|
-
} catch (e) {
|
72
|
-
expect(e).toEqual({
|
73
|
-
endpoint: defaultBaseURL,
|
74
|
-
error: {
|
75
|
-
error: { message: 'Bad Request' },
|
76
|
-
status: 400,
|
77
|
-
},
|
78
|
-
errorType: bizErrorType,
|
79
|
-
provider,
|
80
|
-
});
|
81
|
-
}
|
82
|
-
});
|
83
|
-
|
84
|
-
it('should throw AgentRuntimeError if no apiKey is provided', async () => {
|
85
|
-
try {
|
86
|
-
new LobeNovitaAI({});
|
87
|
-
} catch (e) {
|
88
|
-
expect(e).toEqual({ errorType: invalidErrorType });
|
89
|
-
}
|
90
|
-
});
|
91
|
-
|
92
|
-
it('should return Error with the cause when OpenAI.APIError is thrown with cause', async () => {
|
93
|
-
// Arrange
|
94
|
-
const errorInfo = {
|
95
|
-
stack: 'abc',
|
96
|
-
cause: {
|
97
|
-
message: 'api is undefined',
|
98
|
-
},
|
99
|
-
};
|
100
|
-
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
101
|
-
|
102
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
103
|
-
|
104
|
-
// Act
|
105
|
-
try {
|
106
|
-
await instance.chat({
|
107
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
108
|
-
model: 'meta-llama/llama-3-8b-instruct',
|
109
|
-
temperature: 0.999,
|
110
|
-
});
|
111
|
-
} catch (e) {
|
112
|
-
expect(e).toEqual({
|
113
|
-
endpoint: defaultBaseURL,
|
114
|
-
error: {
|
115
|
-
cause: { message: 'api is undefined' },
|
116
|
-
stack: 'abc',
|
117
|
-
},
|
118
|
-
errorType: bizErrorType,
|
119
|
-
provider,
|
120
|
-
});
|
121
|
-
}
|
122
|
-
});
|
123
|
-
|
124
|
-
it('should return Error with an cause response with desensitize Url', async () => {
|
125
|
-
// Arrange
|
126
|
-
const errorInfo = {
|
127
|
-
stack: 'abc',
|
128
|
-
cause: { message: 'api is undefined' },
|
129
|
-
};
|
130
|
-
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
131
|
-
|
132
|
-
instance = new LobeNovitaAI({
|
133
|
-
apiKey: 'test',
|
134
|
-
|
135
|
-
baseURL: 'https://api.abc.com/v1',
|
136
|
-
});
|
137
|
-
|
138
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
139
|
-
|
140
|
-
// Act
|
141
|
-
try {
|
142
|
-
await instance.chat({
|
143
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
144
|
-
model: 'meta-llama/llama-3-8b-instruct',
|
145
|
-
temperature: 0.999,
|
146
|
-
});
|
147
|
-
} catch (e) {
|
148
|
-
expect(e).toEqual({
|
149
|
-
endpoint: 'https://api.***.com/v1',
|
150
|
-
error: {
|
151
|
-
cause: { message: 'api is undefined' },
|
152
|
-
stack: 'abc',
|
153
|
-
},
|
154
|
-
errorType: bizErrorType,
|
155
|
-
provider,
|
156
|
-
});
|
157
|
-
}
|
158
|
-
});
|
159
|
-
|
160
|
-
it('should throw an error type on 401 status code', async () => {
|
161
|
-
// Mock the API call to simulate a 401 error
|
162
|
-
const error = new Error('InvalidApiKey') as any;
|
163
|
-
error.status = 401;
|
164
|
-
vi.mocked(instance['client'].chat.completions.create).mockRejectedValue(error);
|
165
|
-
|
166
|
-
try {
|
167
|
-
await instance.chat({
|
168
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
169
|
-
model: 'meta-llama/llama-3-8b-instruct',
|
170
|
-
temperature: 0.999,
|
171
|
-
});
|
172
|
-
} catch (e) {
|
173
|
-
expect(e).toEqual({
|
174
|
-
endpoint: defaultBaseURL,
|
175
|
-
error: new Error('InvalidApiKey'),
|
176
|
-
errorType: invalidErrorType,
|
177
|
-
provider,
|
178
|
-
});
|
179
|
-
}
|
180
|
-
});
|
181
|
-
|
182
|
-
it('should return AgentRuntimeError for non-OpenAI errors', async () => {
|
183
|
-
// Arrange
|
184
|
-
const genericError = new Error('Generic Error');
|
185
|
-
|
186
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(genericError);
|
187
|
-
|
188
|
-
// Act
|
189
|
-
try {
|
190
|
-
await instance.chat({
|
191
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
192
|
-
model: 'meta-llama/llama-3-8b-instruct',
|
193
|
-
temperature: 0.999,
|
194
|
-
});
|
195
|
-
} catch (e) {
|
196
|
-
expect(e).toEqual({
|
197
|
-
endpoint: defaultBaseURL,
|
198
|
-
errorType: 'AgentRuntimeError',
|
199
|
-
provider,
|
200
|
-
error: {
|
201
|
-
name: genericError.name,
|
202
|
-
cause: genericError.cause,
|
203
|
-
message: genericError.message,
|
204
|
-
stack: genericError.stack,
|
205
|
-
},
|
206
|
-
});
|
207
|
-
}
|
208
|
-
});
|
209
|
-
});
|
210
|
-
|
211
|
-
describe('DEBUG', () => {
|
212
|
-
it('should call debugStream and return StreamingTextResponse when DEBUG_NOVITA_CHAT_COMPLETION is 1', async () => {
|
213
|
-
// Arrange
|
214
|
-
const mockProdStream = new ReadableStream() as any; // 模拟的 prod 流
|
215
|
-
const mockDebugStream = new ReadableStream({
|
216
|
-
start(controller) {
|
217
|
-
controller.enqueue('Debug stream content');
|
218
|
-
controller.close();
|
219
|
-
},
|
220
|
-
}) as any;
|
221
|
-
mockDebugStream.toReadableStream = () => mockDebugStream; // 添加 toReadableStream 方法
|
222
|
-
|
223
|
-
// 模拟 chat.completions.create 返回值,包括模拟的 tee 方法
|
224
|
-
(instance['client'].chat.completions.create as Mock).mockResolvedValue({
|
225
|
-
tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
|
226
|
-
});
|
227
|
-
|
228
|
-
// 保存原始环境变量值
|
229
|
-
const originalDebugValue = process.env.DEBUG_NOVITA_CHAT_COMPLETION;
|
230
|
-
|
231
|
-
// 模拟环境变量
|
232
|
-
process.env.DEBUG_NOVITA_CHAT_COMPLETION = '1';
|
233
|
-
vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
|
234
|
-
|
235
|
-
// 执行测试
|
236
|
-
// 运行你的测试函数,确保它会在条件满足时调用 debugStream
|
237
|
-
// 假设的测试函数调用,你可能需要根据实际情况调整
|
238
|
-
await instance.chat({
|
239
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
240
|
-
model: 'meta-llama/llama-3-8b-instruct',
|
241
|
-
stream: true,
|
242
|
-
temperature: 0.999,
|
243
|
-
});
|
244
|
-
|
245
|
-
// 验证 debugStream 被调用
|
246
|
-
expect(debugStreamModule.debugStream).toHaveBeenCalled();
|
247
|
-
|
248
|
-
// 恢复原始环境变量值
|
249
|
-
process.env.DEBUG_NOVITA_CHAT_COMPLETION = originalDebugValue;
|
250
|
-
});
|
251
|
-
});
|
252
|
-
});
|
253
|
-
|
254
42
|
describe('models', () => {
|
255
43
|
it('should get models', async () => {
|
256
44
|
// mock the models.list method
|
@@ -29,7 +29,7 @@ describe('LobeOllamaAI', () => {
|
|
29
29
|
try {
|
30
30
|
new LobeOllamaAI({ baseURL: 'invalid-url' });
|
31
31
|
} catch (e) {
|
32
|
-
expect(e).
|
32
|
+
expect(e).toMatchObject({
|
33
33
|
error: new TypeError('Invalid URL'),
|
34
34
|
errorType: 'InvalidOllamaArgs',
|
35
35
|
});
|
@@ -153,7 +153,7 @@ describe('LobeOllamaAI', () => {
|
|
153
153
|
functionCall: false,
|
154
154
|
id: 'model-1',
|
155
155
|
reasoning: false,
|
156
|
-
vision: false
|
156
|
+
vision: false,
|
157
157
|
},
|
158
158
|
{
|
159
159
|
contextWindowTokens: undefined,
|
@@ -162,8 +162,8 @@ describe('LobeOllamaAI', () => {
|
|
162
162
|
functionCall: false,
|
163
163
|
id: 'model-2',
|
164
164
|
reasoning: false,
|
165
|
-
vision: false
|
166
|
-
}
|
165
|
+
vision: false,
|
166
|
+
},
|
167
167
|
]);
|
168
168
|
});
|
169
169
|
});
|
@@ -1,18 +1,25 @@
|
|
1
1
|
// @vitest-environment node
|
2
|
-
import OpenAI from 'openai';
|
3
2
|
import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
4
3
|
|
5
|
-
import {
|
4
|
+
import { LobeOpenAICompatibleRuntime } from '@/libs/agent-runtime';
|
5
|
+
import { testProvider } from '@/libs/agent-runtime/providerTestUtils';
|
6
6
|
|
7
|
-
import * as debugStreamModule from '../utils/debugStream';
|
8
7
|
import models from './fixtures/models.json';
|
9
8
|
import { LobeOpenRouterAI } from './index';
|
10
9
|
|
11
10
|
const provider = 'openrouter';
|
12
11
|
const defaultBaseURL = 'https://openrouter.ai/api/v1';
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
testProvider({
|
14
|
+
provider,
|
15
|
+
defaultBaseURL,
|
16
|
+
chatModel: 'mistralai/mistral-7b-instruct:free',
|
17
|
+
Runtime: LobeOpenRouterAI,
|
18
|
+
chatDebugEnv: 'DEBUG_OPENROUTER_CHAT_COMPLETION',
|
19
|
+
test: {
|
20
|
+
skipAPICall: true,
|
21
|
+
},
|
22
|
+
});
|
16
23
|
|
17
24
|
// Mock the console.error to avoid polluting test output
|
18
25
|
vi.spyOn(console, 'error').mockImplementation(() => {});
|
@@ -35,12 +42,6 @@ afterEach(() => {
|
|
35
42
|
|
36
43
|
describe('LobeOpenRouterAI', () => {
|
37
44
|
describe('init', () => {
|
38
|
-
it('should correctly initialize with an API key', async () => {
|
39
|
-
const instance = new LobeOpenRouterAI({ apiKey: 'test_api_key' });
|
40
|
-
expect(instance).toBeInstanceOf(LobeOpenRouterAI);
|
41
|
-
expect(instance.baseURL).toEqual(defaultBaseURL);
|
42
|
-
});
|
43
|
-
|
44
45
|
it('should correctly initialize with a custom base URL', async () => {
|
45
46
|
const instance = new LobeOpenRouterAI({
|
46
47
|
apiKey: 'test_api_key',
|
@@ -133,212 +134,6 @@ describe('LobeOpenRouterAI', () => {
|
|
133
134
|
);
|
134
135
|
expect(result).toBeInstanceOf(Response);
|
135
136
|
});
|
136
|
-
|
137
|
-
describe('Error', () => {
|
138
|
-
it('should return OpenRouterBizError with an openai error response when OpenAI.APIError is thrown', async () => {
|
139
|
-
// Arrange
|
140
|
-
const apiError = new OpenAI.APIError(
|
141
|
-
400,
|
142
|
-
{
|
143
|
-
status: 400,
|
144
|
-
error: {
|
145
|
-
message: 'Bad Request',
|
146
|
-
},
|
147
|
-
},
|
148
|
-
'Error message',
|
149
|
-
{},
|
150
|
-
);
|
151
|
-
|
152
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
153
|
-
|
154
|
-
// Act
|
155
|
-
try {
|
156
|
-
await instance.chat({
|
157
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
158
|
-
model: 'mistralai/mistral-7b-instruct:free',
|
159
|
-
temperature: 0,
|
160
|
-
});
|
161
|
-
} catch (e) {
|
162
|
-
expect(e).toEqual({
|
163
|
-
endpoint: defaultBaseURL,
|
164
|
-
error: {
|
165
|
-
error: { message: 'Bad Request' },
|
166
|
-
status: 400,
|
167
|
-
},
|
168
|
-
errorType: bizErrorType,
|
169
|
-
provider,
|
170
|
-
});
|
171
|
-
}
|
172
|
-
});
|
173
|
-
|
174
|
-
it('should throw AgentRuntimeError with InvalidOpenRouterAPIKey if no apiKey is provided', async () => {
|
175
|
-
try {
|
176
|
-
new LobeOpenRouterAI({});
|
177
|
-
} catch (e) {
|
178
|
-
expect(e).toEqual({ errorType: invalidErrorType });
|
179
|
-
}
|
180
|
-
});
|
181
|
-
|
182
|
-
it('should return OpenRouterBizError with the cause when OpenAI.APIError is thrown with cause', async () => {
|
183
|
-
// Arrange
|
184
|
-
const errorInfo = {
|
185
|
-
stack: 'abc',
|
186
|
-
cause: {
|
187
|
-
message: 'api is undefined',
|
188
|
-
},
|
189
|
-
};
|
190
|
-
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
191
|
-
|
192
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
193
|
-
|
194
|
-
// Act
|
195
|
-
try {
|
196
|
-
await instance.chat({
|
197
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
198
|
-
model: 'mistralai/mistral-7b-instruct:free',
|
199
|
-
temperature: 0,
|
200
|
-
});
|
201
|
-
} catch (e) {
|
202
|
-
expect(e).toEqual({
|
203
|
-
endpoint: defaultBaseURL,
|
204
|
-
error: {
|
205
|
-
cause: { message: 'api is undefined' },
|
206
|
-
stack: 'abc',
|
207
|
-
},
|
208
|
-
errorType: bizErrorType,
|
209
|
-
provider,
|
210
|
-
});
|
211
|
-
}
|
212
|
-
});
|
213
|
-
|
214
|
-
it('should return OpenRouterBizError with an cause response with desensitize Url', async () => {
|
215
|
-
// Arrange
|
216
|
-
const errorInfo = {
|
217
|
-
stack: 'abc',
|
218
|
-
cause: { message: 'api is undefined' },
|
219
|
-
};
|
220
|
-
const apiError = new OpenAI.APIError(400, errorInfo, 'module error', {});
|
221
|
-
|
222
|
-
instance = new LobeOpenRouterAI({
|
223
|
-
apiKey: 'test',
|
224
|
-
|
225
|
-
baseURL: 'https://api.abc.com/v1',
|
226
|
-
});
|
227
|
-
|
228
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(apiError);
|
229
|
-
|
230
|
-
// Act
|
231
|
-
try {
|
232
|
-
await instance.chat({
|
233
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
234
|
-
model: 'mistralai/mistral-7b-instruct:free',
|
235
|
-
temperature: 0,
|
236
|
-
});
|
237
|
-
} catch (e) {
|
238
|
-
expect(e).toEqual({
|
239
|
-
endpoint: 'https://api.***.com/v1',
|
240
|
-
error: {
|
241
|
-
cause: { message: 'api is undefined' },
|
242
|
-
stack: 'abc',
|
243
|
-
},
|
244
|
-
errorType: bizErrorType,
|
245
|
-
provider,
|
246
|
-
});
|
247
|
-
}
|
248
|
-
});
|
249
|
-
|
250
|
-
it('should throw an InvalidOpenRouterAPIKey error type on 401 status code', async () => {
|
251
|
-
// Mock the API call to simulate a 401 error
|
252
|
-
const error = new Error('Unauthorized') as any;
|
253
|
-
error.status = 401;
|
254
|
-
vi.mocked(instance['client'].chat.completions.create).mockRejectedValue(error);
|
255
|
-
|
256
|
-
try {
|
257
|
-
await instance.chat({
|
258
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
259
|
-
model: 'mistralai/mistral-7b-instruct:free',
|
260
|
-
temperature: 0,
|
261
|
-
});
|
262
|
-
} catch (e) {
|
263
|
-
// Expect the chat method to throw an error with InvalidMoonshotAPIKey
|
264
|
-
expect(e).toEqual({
|
265
|
-
endpoint: defaultBaseURL,
|
266
|
-
error: new Error('Unauthorized'),
|
267
|
-
errorType: invalidErrorType,
|
268
|
-
provider,
|
269
|
-
});
|
270
|
-
}
|
271
|
-
});
|
272
|
-
|
273
|
-
it('should return AgentRuntimeError for non-OpenAI errors', async () => {
|
274
|
-
// Arrange
|
275
|
-
const genericError = new Error('Generic Error');
|
276
|
-
|
277
|
-
vi.spyOn(instance['client'].chat.completions, 'create').mockRejectedValue(genericError);
|
278
|
-
|
279
|
-
// Act
|
280
|
-
try {
|
281
|
-
await instance.chat({
|
282
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
283
|
-
model: 'mistralai/mistral-7b-instruct:free',
|
284
|
-
temperature: 0,
|
285
|
-
});
|
286
|
-
} catch (e) {
|
287
|
-
expect(e).toEqual({
|
288
|
-
endpoint: defaultBaseURL,
|
289
|
-
errorType: 'AgentRuntimeError',
|
290
|
-
provider,
|
291
|
-
error: {
|
292
|
-
name: genericError.name,
|
293
|
-
cause: genericError.cause,
|
294
|
-
message: genericError.message,
|
295
|
-
stack: genericError.stack,
|
296
|
-
},
|
297
|
-
});
|
298
|
-
}
|
299
|
-
});
|
300
|
-
});
|
301
|
-
|
302
|
-
describe('DEBUG', () => {
|
303
|
-
it('should call debugStream and return StreamingTextResponse when DEBUG_OPENROUTER_CHAT_COMPLETION is 1', async () => {
|
304
|
-
// Arrange
|
305
|
-
const mockProdStream = new ReadableStream() as any; // 模拟的 prod 流
|
306
|
-
const mockDebugStream = new ReadableStream({
|
307
|
-
start(controller) {
|
308
|
-
controller.enqueue('Debug stream content');
|
309
|
-
controller.close();
|
310
|
-
},
|
311
|
-
}) as any;
|
312
|
-
mockDebugStream.toReadableStream = () => mockDebugStream; // 添加 toReadableStream 方法
|
313
|
-
|
314
|
-
// 模拟 chat.completions.create 返回值,包括模拟的 tee 方法
|
315
|
-
(instance['client'].chat.completions.create as Mock).mockResolvedValue({
|
316
|
-
tee: () => [mockProdStream, { toReadableStream: () => mockDebugStream }],
|
317
|
-
});
|
318
|
-
|
319
|
-
// 保存原始环境变量值
|
320
|
-
const originalDebugValue = process.env.DEBUG_OPENROUTER_CHAT_COMPLETION;
|
321
|
-
|
322
|
-
// 模拟环境变量
|
323
|
-
process.env.DEBUG_OPENROUTER_CHAT_COMPLETION = '1';
|
324
|
-
vi.spyOn(debugStreamModule, 'debugStream').mockImplementation(() => Promise.resolve());
|
325
|
-
|
326
|
-
// 执行测试
|
327
|
-
// 运行你的测试函数,确保它会在条件满足时调用 debugStream
|
328
|
-
// 假设的测试函数调用,你可能需要根据实际情况调整
|
329
|
-
await instance.chat({
|
330
|
-
messages: [{ content: 'Hello', role: 'user' }],
|
331
|
-
model: 'mistralai/mistral-7b-instruct:free',
|
332
|
-
temperature: 0,
|
333
|
-
});
|
334
|
-
|
335
|
-
// 验证 debugStream 被调用
|
336
|
-
expect(debugStreamModule.debugStream).toHaveBeenCalled();
|
337
|
-
|
338
|
-
// 恢复原始环境变量值
|
339
|
-
process.env.DEBUG_OPENROUTER_CHAT_COMPLETION = originalDebugValue;
|
340
|
-
});
|
341
|
-
});
|
342
137
|
});
|
343
138
|
|
344
139
|
describe('models', () => {
|