forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix: fix inbox agent can not save config (lobehub#6186)
- Loading branch information
Showing
11 changed files
with
174 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// @vitest-environment node | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import { SessionModel } from '@/database/server/models/session'; | ||
import { parseAgentConfig } from '@/server/globalConfig/parseDefaultAgent'; | ||
|
||
import { AgentService } from './index'; | ||
|
||
vi.mock('@/config/app', () => ({ | ||
appEnv: { | ||
DEFAULT_AGENT_CONFIG: 'model=gpt-4;temperature=0.7', | ||
}, | ||
})); | ||
|
||
vi.mock('@/server/globalConfig/parseDefaultAgent', () => ({ | ||
parseAgentConfig: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/database/server/models/session', () => ({ | ||
SessionModel: vi.fn(), | ||
})); | ||
|
||
describe('AgentService', () => { | ||
let service: AgentService; | ||
const mockDb = {} as any; | ||
const mockUserId = 'test-user-id'; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
service = new AgentService(mockDb, mockUserId); | ||
}); | ||
|
||
describe('createInbox', () => { | ||
it('should create inbox with default agent config', async () => { | ||
const mockConfig = { model: 'gpt-4', temperature: 0.7 }; | ||
const mockSessionModel = { | ||
createInbox: vi.fn(), | ||
}; | ||
|
||
(SessionModel as any).mockImplementation(() => mockSessionModel); | ||
(parseAgentConfig as any).mockReturnValue(mockConfig); | ||
|
||
await service.createInbox(); | ||
|
||
expect(SessionModel).toHaveBeenCalledWith(mockDb, mockUserId); | ||
expect(parseAgentConfig).toHaveBeenCalledWith('model=gpt-4;temperature=0.7'); | ||
expect(mockSessionModel.createInbox).toHaveBeenCalledWith(mockConfig); | ||
}); | ||
|
||
it('should create inbox with empty config if parseAgentConfig returns undefined', async () => { | ||
const mockSessionModel = { | ||
createInbox: vi.fn(), | ||
}; | ||
|
||
(SessionModel as any).mockImplementation(() => mockSessionModel); | ||
(parseAgentConfig as any).mockReturnValue(undefined); | ||
|
||
await service.createInbox(); | ||
|
||
expect(SessionModel).toHaveBeenCalledWith(mockDb, mockUserId); | ||
expect(parseAgentConfig).toHaveBeenCalledWith('model=gpt-4;temperature=0.7'); | ||
expect(mockSessionModel.createInbox).toHaveBeenCalledWith({}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { appEnv } from '@/config/app'; | ||
import { SessionModel } from '@/database/server/models/session'; | ||
import { LobeChatDatabase } from '@/database/type'; | ||
import { parseAgentConfig } from '@/server/globalConfig/parseDefaultAgent'; | ||
|
||
export class AgentService { | ||
private readonly userId: string; | ||
private readonly db: LobeChatDatabase; | ||
|
||
constructor(db: LobeChatDatabase, userId: string) { | ||
this.userId = userId; | ||
this.db = db; | ||
} | ||
|
||
async createInbox() { | ||
const sessionModel = new SessionModel(this.db, this.userId); | ||
|
||
const defaultAgentConfig = parseAgentConfig(appEnv.DEFAULT_AGENT_CONFIG) || {}; | ||
|
||
await sessionModel.createInbox(defaultAgentConfig); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.