From 1fd2fa0246104a43c2d94ae88da640798891ab59 Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Mon, 26 Aug 2024 00:14:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20fix=20cannot=20clone=20ag?= =?UTF-8?q?ent=20when=20imported=20from=20client=20(#3606)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 fix: fix cannot clone agent when imported from client * ✅ test: fix lint --- package.json | 2 +- src/database/server/models/session.ts | 3 +- src/libs/agent-runtime/groq/index.test.ts | 2 +- src/libs/agent-runtime/qwen/index.test.ts | 4 +- .../openaiCompatibleFactory/index.test.ts | 50 +++++++++++-------- 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index c1960c9fc7c5..0deed7ed6f9e 100644 --- a/package.json +++ b/package.json @@ -171,7 +171,7 @@ "nuqs": "^1.17.8", "officeparser": "^4.1.1", "ollama": "^0.5.8", - "openai": "~4.54.0", + "openai": "^4.56.0", "partial-json": "^0.1.7", "pdf-parse": "^1.1.1", "pdfjs-dist": "4.4.168", diff --git a/src/database/server/models/session.ts b/src/database/server/models/session.ts index c4349c07faf2..cca3f73f01ae 100644 --- a/src/database/server/models/session.ts +++ b/src/database/server/models/session.ts @@ -170,7 +170,8 @@ export class SessionModel { if (!result) return; - const { agent, ...session } = result; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { agent, clientId, ...session } = result; const sessionId = this.genId(); // eslint-disable-next-line @typescript-eslint/no-unused-vars diff --git a/src/libs/agent-runtime/groq/index.test.ts b/src/libs/agent-runtime/groq/index.test.ts index 4a660d129bf5..809abadacd57 100644 --- a/src/libs/agent-runtime/groq/index.test.ts +++ b/src/libs/agent-runtime/groq/index.test.ts @@ -85,7 +85,7 @@ describe('LobeGroqAI', () => { choices: [ { index: 0, - message: { role: 'assistant', content: 'hello' }, + message: { role: 'assistant', content: 'hello', refusal: null }, logprobs: null, finish_reason: 'stop', }, diff --git a/src/libs/agent-runtime/qwen/index.test.ts b/src/libs/agent-runtime/qwen/index.test.ts index 4c3757fea6c0..3825040978c1 100644 --- a/src/libs/agent-runtime/qwen/index.test.ts +++ b/src/libs/agent-runtime/qwen/index.test.ts @@ -106,7 +106,7 @@ describe('LobeQwenAI', () => { }); it('should transform non-streaming response to stream correctly', async () => { - const mockResponse: OpenAI.ChatCompletion = { + const mockResponse = { id: 'chatcmpl-fc539f49-51a8-94be-8061', object: 'chat.completion', created: 1719901794, @@ -119,7 +119,7 @@ describe('LobeQwenAI', () => { logprobs: null, }, ], - }; + } as OpenAI.ChatCompletion; vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue( mockResponse as any, ); diff --git a/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.test.ts b/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.test.ts index 2affa79147da..f1b74b030f8e 100644 --- a/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.test.ts +++ b/src/libs/agent-runtime/utils/openaiCompatibleFactory/index.test.ts @@ -342,7 +342,7 @@ describe('LobeOpenAICompatibleFactory', () => { }); it('should transform non-streaming response to stream correctly', async () => { - const mockResponse: OpenAI.ChatCompletion = { + const mockResponse = { id: 'a', object: 'chat.completion', created: 123, @@ -360,7 +360,7 @@ describe('LobeOpenAICompatibleFactory', () => { completion_tokens: 5, total_tokens: 10, }, - }; + } as OpenAI.ChatCompletion; vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue( mockResponse as any, ); @@ -426,27 +426,29 @@ describe('LobeOpenAICompatibleFactory', () => { }, provider: ModelProvider.Mistral, }); - + const instance = new LobeMockProvider({ apiKey: 'test' }); - const mockCreateMethod = vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(new ReadableStream() as any); - + const mockCreateMethod = vi + .spyOn(instance['client'].chat.completions, 'create') + .mockResolvedValue(new ReadableStream() as any); + await instance.chat( { messages: [{ content: 'Hello', role: 'user' }], model: 'open-mistral-7b', temperature: 0, }, - { user: 'testUser' } + { user: 'testUser' }, ); - + expect(mockCreateMethod).toHaveBeenCalledWith( expect.not.objectContaining({ user: 'testUser', }), - expect.anything() + expect.anything(), ); }); - + it('should add user to payload when noUserId is false', async () => { const LobeMockProvider = LobeOpenAICompatibleFactory({ baseURL: 'https://api.mistral.ai/v1', @@ -455,50 +457,54 @@ describe('LobeOpenAICompatibleFactory', () => { }, provider: ModelProvider.Mistral, }); - + const instance = new LobeMockProvider({ apiKey: 'test' }); - const mockCreateMethod = vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(new ReadableStream() as any); - + const mockCreateMethod = vi + .spyOn(instance['client'].chat.completions, 'create') + .mockResolvedValue(new ReadableStream() as any); + await instance.chat( { messages: [{ content: 'Hello', role: 'user' }], model: 'open-mistral-7b', temperature: 0, }, - { user: 'testUser' } + { user: 'testUser' }, ); - + expect(mockCreateMethod).toHaveBeenCalledWith( expect.objectContaining({ user: 'testUser', }), - expect.anything() + expect.anything(), ); }); - + it('should add user to payload when noUserId is not set in chatCompletion', async () => { const LobeMockProvider = LobeOpenAICompatibleFactory({ baseURL: 'https://api.mistral.ai/v1', provider: ModelProvider.Mistral, }); - + const instance = new LobeMockProvider({ apiKey: 'test' }); - const mockCreateMethod = vi.spyOn(instance['client'].chat.completions, 'create').mockResolvedValue(new ReadableStream() as any); - + const mockCreateMethod = vi + .spyOn(instance['client'].chat.completions, 'create') + .mockResolvedValue(new ReadableStream() as any); + await instance.chat( { messages: [{ content: 'Hello', role: 'user' }], model: 'open-mistral-7b', temperature: 0, }, - { user: 'testUser' } + { user: 'testUser' }, ); - + expect(mockCreateMethod).toHaveBeenCalledWith( expect.objectContaining({ user: 'testUser', }), - expect.anything() + expect.anything(), ); }); });