@lobehub/lobehub 2.0.0-next.36 → 2.0.0-next.37

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/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ## [Version 2.0.0-next.37](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.36...v2.0.0-next.37)
6
+
7
+ <sup>Released on **2025-11-07**</sup>
8
+
9
+ #### 🐛 Bug Fixes
10
+
11
+ - **misc**: Don't include runtimeProvider in JWT for non-image operations.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### What's fixed
19
+
20
+ - **misc**: Don't include runtimeProvider in JWT for non-image operations, closes [#9959](https://github.com/lobehub/lobe-chat/issues/9959) [#9569](https://github.com/lobehub/lobe-chat/issues/9569) ([b8f25de](https://github.com/lobehub/lobe-chat/commit/b8f25de))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ## [Version 2.0.0-next.36](https://github.com/lobehub/lobe-chat/compare/v2.0.0-next.35...v2.0.0-next.36)
6
31
 
7
32
  <sup>Released on **2025-11-07**</sup>
package/changelog/v1.json CHANGED
@@ -1,4 +1,13 @@
1
1
  [
2
+ {
3
+ "children": {
4
+ "fixes": [
5
+ "Don't include runtimeProvider in JWT for non-image operations."
6
+ ]
7
+ },
8
+ "date": "2025-11-07",
9
+ "version": "2.0.0-next.37"
10
+ },
2
11
  {
3
12
  "children": {
4
13
  "features": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/lobehub",
3
- "version": "2.0.0-next.36",
3
+ "version": "2.0.0-next.37",
4
4
  "description": "LobeHub - an open-source,comprehensive AI Agent framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
5
5
  "keywords": [
6
6
  "framework",
@@ -0,0 +1,444 @@
1
+ // @vitest-environment node
2
+ import { eq } from 'drizzle-orm';
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
4
+
5
+ import { apiKeys, users } from '../../schemas';
6
+ import { LobeChatDatabase } from '../../type';
7
+ import { ApiKeyModel } from '../apiKey';
8
+ import { getTestDB } from './_util';
9
+
10
+ const serverDB: LobeChatDatabase = await getTestDB();
11
+
12
+ const userId = 'api-key-model-test-user-id';
13
+ const apiKeyModel = new ApiKeyModel(serverDB, userId);
14
+
15
+ beforeEach(async () => {
16
+ await serverDB.delete(users);
17
+ await serverDB.insert(users).values([{ id: userId }, { id: 'user2' }]);
18
+ });
19
+
20
+ afterEach(async () => {
21
+ await serverDB.delete(users).where(eq(users.id, userId));
22
+ await serverDB.delete(apiKeys).where(eq(apiKeys.userId, userId));
23
+ });
24
+
25
+ describe('ApiKeyModel', () => {
26
+ describe('create', () => {
27
+ it('should create a new API key without encryption', async () => {
28
+ const params = {
29
+ enabled: true,
30
+ name: 'Test API Key',
31
+ };
32
+
33
+ const result = await apiKeyModel.create(params);
34
+
35
+ expect(result.id).toBeDefined();
36
+ expect(result.name).toBe(params.name);
37
+ expect(result.enabled).toBe(params.enabled);
38
+ expect(result.key).toBeDefined();
39
+ expect(result.key).toMatch(/^lb-[\da-z]{16}$/);
40
+ expect(result.userId).toBe(userId);
41
+
42
+ const apiKey = await serverDB.query.apiKeys.findFirst({
43
+ where: eq(apiKeys.id, result.id),
44
+ });
45
+ expect(apiKey).toMatchObject({ ...params, userId });
46
+ });
47
+
48
+ it('should create a new API key with encryption', async () => {
49
+ const mockEncryptor = vi.fn().mockResolvedValue('encrypted-key-value');
50
+ const params = {
51
+ enabled: true,
52
+ name: 'Encrypted API Key',
53
+ };
54
+
55
+ const result = await apiKeyModel.create(params, mockEncryptor);
56
+
57
+ expect(result.id).toBeDefined();
58
+ expect(result.name).toBe(params.name);
59
+ expect(result.key).toBe('encrypted-key-value');
60
+ expect(mockEncryptor).toHaveBeenCalledWith(expect.stringMatching(/^lb-[\da-z]{16}$/));
61
+
62
+ const apiKey = await serverDB.query.apiKeys.findFirst({
63
+ where: eq(apiKeys.id, result.id),
64
+ });
65
+ expect(apiKey?.key).toBe('encrypted-key-value');
66
+ });
67
+
68
+ it('should create API key with expiration date', async () => {
69
+ const expiresAt = new Date('2025-12-31');
70
+ const params = {
71
+ enabled: true,
72
+ expiresAt,
73
+ name: 'Expiring Key',
74
+ };
75
+
76
+ const result = await apiKeyModel.create(params);
77
+
78
+ expect(result.expiresAt).toEqual(expiresAt);
79
+ });
80
+ });
81
+
82
+ describe('delete', () => {
83
+ it('should delete an API key by id', async () => {
84
+ const { id } = await apiKeyModel.create({ name: 'Test Key', enabled: true });
85
+
86
+ await apiKeyModel.delete(id);
87
+
88
+ const apiKey = await serverDB.query.apiKeys.findFirst({
89
+ where: eq(apiKeys.id, id),
90
+ });
91
+ expect(apiKey).toBeUndefined();
92
+ });
93
+
94
+ it('should only delete API keys for the current user', async () => {
95
+ const { id: key1 } = await apiKeyModel.create({ name: 'User 1 Key', enabled: true });
96
+
97
+ const anotherApiKeyModel = new ApiKeyModel(serverDB, 'user2');
98
+ const { id: key2 } = await anotherApiKeyModel.create({ name: 'User 2 Key', enabled: true });
99
+
100
+ await apiKeyModel.delete(key2);
101
+
102
+ const key2Still = await serverDB.query.apiKeys.findFirst({
103
+ where: eq(apiKeys.id, key2),
104
+ });
105
+ expect(key2Still).toBeDefined();
106
+
107
+ await apiKeyModel.delete(key1);
108
+
109
+ const key1Deleted = await serverDB.query.apiKeys.findFirst({
110
+ where: eq(apiKeys.id, key1),
111
+ });
112
+ expect(key1Deleted).toBeUndefined();
113
+ });
114
+ });
115
+
116
+ describe('deleteAll', () => {
117
+ it('should delete all API keys for the user', async () => {
118
+ await apiKeyModel.create({ name: 'Test Key 1', enabled: true });
119
+ await apiKeyModel.create({ name: 'Test Key 2', enabled: true });
120
+
121
+ await apiKeyModel.deleteAll();
122
+
123
+ const userKeys = await serverDB.query.apiKeys.findMany({
124
+ where: eq(apiKeys.userId, userId),
125
+ });
126
+ expect(userKeys).toHaveLength(0);
127
+ });
128
+
129
+ it('should only delete API keys for the user, not others', async () => {
130
+ await apiKeyModel.create({ name: 'Test Key 1', enabled: true });
131
+ await apiKeyModel.create({ name: 'Test Key 2', enabled: true });
132
+
133
+ const anotherApiKeyModel = new ApiKeyModel(serverDB, 'user2');
134
+ await anotherApiKeyModel.create({ name: 'User 2 Key', enabled: true });
135
+
136
+ await apiKeyModel.deleteAll();
137
+
138
+ const userKeys = await serverDB.query.apiKeys.findMany({
139
+ where: eq(apiKeys.userId, userId),
140
+ });
141
+ const total = await serverDB.query.apiKeys.findMany();
142
+ expect(userKeys).toHaveLength(0);
143
+ expect(total).toHaveLength(1);
144
+ });
145
+ });
146
+
147
+ describe('query', () => {
148
+ it('should query API keys for the user without decryption', async () => {
149
+ await apiKeyModel.create({ name: 'Key 1', enabled: true });
150
+ await apiKeyModel.create({ name: 'Key 2', enabled: true });
151
+
152
+ const keys = await apiKeyModel.query();
153
+ expect(keys).toHaveLength(2);
154
+ expect(keys[0].key).toMatch(/^lb-[\da-z]{16}$/);
155
+ });
156
+
157
+ it('should query API keys ordered by updatedAt desc', async () => {
158
+ const key1 = await apiKeyModel.create({ name: 'Key 1', enabled: true });
159
+ // Wait a bit to ensure different timestamps
160
+ await new Promise((resolve) => setTimeout(resolve, 10));
161
+ const key2 = await apiKeyModel.create({ name: 'Key 2', enabled: true });
162
+
163
+ const keys = await apiKeyModel.query();
164
+ expect(keys).toHaveLength(2);
165
+ expect(keys[0].id).toBe(key2.id);
166
+ expect(keys[1].id).toBe(key1.id);
167
+ });
168
+
169
+ it('should query API keys with decryption', async () => {
170
+ const mockEncryptor = vi.fn().mockResolvedValue('encrypted-key');
171
+ const mockDecryptor = vi.fn().mockResolvedValue({ plaintext: 'decrypted-key-value' });
172
+
173
+ await apiKeyModel.create({ name: 'Encrypted Key', enabled: true }, mockEncryptor);
174
+
175
+ const keys = await apiKeyModel.query(mockDecryptor);
176
+
177
+ expect(keys).toHaveLength(1);
178
+ expect(keys[0].key).toBe('decrypted-key-value');
179
+ expect(mockDecryptor).toHaveBeenCalledWith('encrypted-key');
180
+ });
181
+
182
+ it('should only query API keys for the current user', async () => {
183
+ await apiKeyModel.create({ name: 'User 1 Key', enabled: true });
184
+
185
+ const anotherApiKeyModel = new ApiKeyModel(serverDB, 'user2');
186
+ await anotherApiKeyModel.create({ name: 'User 2 Key', enabled: true });
187
+
188
+ const keys = await apiKeyModel.query();
189
+ expect(keys).toHaveLength(1);
190
+ expect(keys[0].name).toBe('User 1 Key');
191
+ });
192
+ });
193
+
194
+ describe('findByKey', () => {
195
+ it('should find API key by key value without encryption', async () => {
196
+ // Use a valid hex format key since validateApiKeyFormat checks for hex pattern
197
+ const validKey = 'lb-abcdef0123456789';
198
+ await serverDB.insert(apiKeys).values({
199
+ enabled: true,
200
+ key: validKey,
201
+ name: 'Test Key',
202
+ userId,
203
+ });
204
+
205
+ const found = await apiKeyModel.findByKey(validKey);
206
+
207
+ expect(found).toBeDefined();
208
+ expect(found?.key).toBe(validKey);
209
+ expect(found?.name).toBe('Test Key');
210
+ });
211
+
212
+ it('should find API key by key value with encryption', async () => {
213
+ const mockEncryptor = vi.fn().mockResolvedValue('encrypted-key-value');
214
+ const created = await apiKeyModel.create({ name: 'Test Key', enabled: true }, mockEncryptor);
215
+
216
+ const testKey = 'lb-0123456789abcdef';
217
+ mockEncryptor.mockResolvedValue('encrypted-key-value');
218
+ const found = await apiKeyModel.findByKey(testKey, mockEncryptor);
219
+
220
+ expect(mockEncryptor).toHaveBeenCalledWith(testKey);
221
+ });
222
+
223
+ it('should return null for invalid key format', async () => {
224
+ const found = await apiKeyModel.findByKey('invalid-key-format');
225
+
226
+ expect(found).toBeNull();
227
+ });
228
+
229
+ it('should return undefined for non-existent key', async () => {
230
+ const found = await apiKeyModel.findByKey('lb-0123456789abcdef');
231
+
232
+ expect(found).toBeUndefined();
233
+ });
234
+ });
235
+
236
+ describe('validateKey', () => {
237
+ it('should validate enabled and non-expired key with valid hex format', async () => {
238
+ const futureDate = new Date();
239
+ futureDate.setFullYear(futureDate.getFullYear() + 1);
240
+
241
+ // Use a valid hex format key
242
+ const validKey = 'lb-0123456789abcdef';
243
+ await serverDB.insert(apiKeys).values({
244
+ enabled: true,
245
+ expiresAt: futureDate,
246
+ key: validKey,
247
+ name: 'Valid Key',
248
+ userId,
249
+ });
250
+
251
+ const isValid = await apiKeyModel.validateKey(validKey);
252
+
253
+ expect(isValid).toBe(true);
254
+ });
255
+
256
+ it('should validate enabled key without expiration with valid hex format', async () => {
257
+ // Use a valid hex format key
258
+ const validKey = 'lb-fedcba9876543210';
259
+ await serverDB.insert(apiKeys).values({
260
+ enabled: true,
261
+ key: validKey,
262
+ name: 'Valid Key',
263
+ userId,
264
+ });
265
+
266
+ const isValid = await apiKeyModel.validateKey(validKey);
267
+
268
+ expect(isValid).toBe(true);
269
+ });
270
+
271
+ it('should reject non-existent key', async () => {
272
+ const isValid = await apiKeyModel.validateKey('lb-0123456789abcdef');
273
+
274
+ expect(isValid).toBe(false);
275
+ });
276
+
277
+ it('should reject disabled key', async () => {
278
+ const validKey = 'lb-1111111111111111';
279
+ await serverDB.insert(apiKeys).values({
280
+ enabled: false,
281
+ key: validKey,
282
+ name: 'Disabled Key',
283
+ userId,
284
+ });
285
+
286
+ const isValid = await apiKeyModel.validateKey(validKey);
287
+
288
+ expect(isValid).toBe(false);
289
+ });
290
+
291
+ it('should reject expired key', async () => {
292
+ const pastDate = new Date();
293
+ pastDate.setFullYear(pastDate.getFullYear() - 1);
294
+
295
+ const validKey = 'lb-2222222222222222';
296
+ await serverDB.insert(apiKeys).values({
297
+ enabled: true,
298
+ expiresAt: pastDate,
299
+ key: validKey,
300
+ name: 'Expired Key',
301
+ userId,
302
+ });
303
+
304
+ const isValid = await apiKeyModel.validateKey(validKey);
305
+
306
+ expect(isValid).toBe(false);
307
+ });
308
+
309
+ it('should reject invalid key format', async () => {
310
+ const isValid = await apiKeyModel.validateKey('invalid-format');
311
+
312
+ expect(isValid).toBe(false);
313
+ });
314
+ });
315
+
316
+ describe('update', () => {
317
+ it('should update API key properties', async () => {
318
+ const { id } = await apiKeyModel.create({ name: 'Test Key', enabled: true });
319
+
320
+ await apiKeyModel.update(id, { name: 'Updated Key', enabled: false });
321
+
322
+ const updated = await serverDB.query.apiKeys.findFirst({
323
+ where: eq(apiKeys.id, id),
324
+ });
325
+ expect(updated).toMatchObject({
326
+ enabled: false,
327
+ id,
328
+ name: 'Updated Key',
329
+ userId,
330
+ });
331
+ });
332
+
333
+ it('should update expiration date', async () => {
334
+ const { id } = await apiKeyModel.create({ name: 'Test Key', enabled: true });
335
+
336
+ const newExpiresAt = new Date('2026-12-31');
337
+ await apiKeyModel.update(id, { expiresAt: newExpiresAt });
338
+
339
+ const updated = await serverDB.query.apiKeys.findFirst({
340
+ where: eq(apiKeys.id, id),
341
+ });
342
+ expect(updated?.expiresAt).toEqual(newExpiresAt);
343
+ });
344
+
345
+ it('should only update API keys for the current user', async () => {
346
+ const { id: key1 } = await apiKeyModel.create({ name: 'User 1 Key', enabled: true });
347
+
348
+ const anotherApiKeyModel = new ApiKeyModel(serverDB, 'user2');
349
+ const { id: key2 } = await anotherApiKeyModel.create({ name: 'User 2 Key', enabled: true });
350
+
351
+ await apiKeyModel.update(key2, { name: 'Attempted Update' });
352
+
353
+ const key2Still = await serverDB.query.apiKeys.findFirst({
354
+ where: eq(apiKeys.id, key2),
355
+ });
356
+ expect(key2Still?.name).toBe('User 2 Key');
357
+ });
358
+ });
359
+
360
+ describe('findById', () => {
361
+ it('should find API key by id', async () => {
362
+ const { id } = await apiKeyModel.create({ name: 'Test Key', enabled: true });
363
+
364
+ const found = await apiKeyModel.findById(id);
365
+
366
+ expect(found).toMatchObject({
367
+ enabled: true,
368
+ id,
369
+ name: 'Test Key',
370
+ userId,
371
+ });
372
+ });
373
+
374
+ it('should return undefined for non-existent id', async () => {
375
+ const found = await apiKeyModel.findById(999_999);
376
+
377
+ expect(found).toBeUndefined();
378
+ });
379
+
380
+ it('should only find API keys for the current user', async () => {
381
+ const anotherApiKeyModel = new ApiKeyModel(serverDB, 'user2');
382
+ const { id: key2 } = await anotherApiKeyModel.create({ name: 'User 2 Key', enabled: true });
383
+
384
+ const found = await apiKeyModel.findById(key2);
385
+
386
+ expect(found).toBeUndefined();
387
+ });
388
+ });
389
+
390
+ describe('updateLastUsed', () => {
391
+ it('should update lastUsedAt timestamp', async () => {
392
+ const { id } = await apiKeyModel.create({ name: 'Test Key', enabled: true });
393
+
394
+ const beforeUpdate = await serverDB.query.apiKeys.findFirst({
395
+ where: eq(apiKeys.id, id),
396
+ });
397
+ expect(beforeUpdate?.lastUsedAt).toBeNull();
398
+
399
+ await apiKeyModel.updateLastUsed(id);
400
+
401
+ const afterUpdate = await serverDB.query.apiKeys.findFirst({
402
+ where: eq(apiKeys.id, id),
403
+ });
404
+ expect(afterUpdate?.lastUsedAt).toBeInstanceOf(Date);
405
+ });
406
+
407
+ it('should only update API keys for the current user', async () => {
408
+ const anotherApiKeyModel = new ApiKeyModel(serverDB, 'user2');
409
+ const { id: key2 } = await anotherApiKeyModel.create({ name: 'User 2 Key', enabled: true });
410
+
411
+ await apiKeyModel.updateLastUsed(key2);
412
+
413
+ const key2Still = await serverDB.query.apiKeys.findFirst({
414
+ where: eq(apiKeys.id, key2),
415
+ });
416
+ expect(key2Still?.lastUsedAt).toBeNull();
417
+ });
418
+
419
+ it('should update existing lastUsedAt to a new timestamp', async () => {
420
+ const { id } = await apiKeyModel.create({ name: 'Test Key', enabled: true });
421
+
422
+ await apiKeyModel.updateLastUsed(id);
423
+
424
+ const firstUpdate = await serverDB.query.apiKeys.findFirst({
425
+ where: eq(apiKeys.id, id),
426
+ });
427
+ const firstTimestamp = firstUpdate?.lastUsedAt;
428
+
429
+ // Wait to ensure different timestamp
430
+ await new Promise((resolve) => setTimeout(resolve, 10));
431
+
432
+ await apiKeyModel.updateLastUsed(id);
433
+
434
+ const secondUpdate = await serverDB.query.apiKeys.findFirst({
435
+ where: eq(apiKeys.id, id),
436
+ });
437
+ const secondTimestamp = secondUpdate?.lastUsedAt;
438
+
439
+ expect(secondTimestamp).toBeDefined();
440
+ expect(firstTimestamp).toBeDefined();
441
+ expect(secondTimestamp!.getTime()).toBeGreaterThan(firstTimestamp!.getTime());
442
+ });
443
+ });
444
+ });
@@ -84,7 +84,7 @@ const customHttpBatchLink = httpBatchLink({
84
84
  // dynamic import to avoid circular dependency
85
85
  const { createHeaderWithAuth } = await import('@/services/_auth');
86
86
 
87
- let provider: ModelProvider = ModelProvider.OpenAI;
87
+ let provider: ModelProvider | undefined;
88
88
  // for image page, we need to get the provider from the store
89
89
  log('Getting provider from store for image page: %s', location.pathname);
90
90
  if (location.pathname === '/image') {
@@ -96,8 +96,9 @@ const customHttpBatchLink = httpBatchLink({
96
96
  log('Getting provider from store for image page: %s', provider);
97
97
  }
98
98
 
99
- // TODO: we need to support provider select for chat page
100
- const headers = await createHeaderWithAuth({ provider });
99
+ // Only include provider in JWT for image operations
100
+ // For other operations (like knowledge base embedding), let server use its own config
101
+ const headers = await createHeaderWithAuth(provider ? { provider } : undefined);
101
102
  log('Headers: %O', headers);
102
103
  return headers;
103
104
  },