diff options
| author | N. Taylor Mullen <[email protected]> | 2025-06-08 11:14:45 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-08 11:14:45 -0700 |
| commit | 241c404573a8dd8c032dde5478a9bec95dd83a19 (patch) | |
| tree | 7b0f6e812a19dfe1dbe09254781af605c370d12f /packages/core/src | |
| parent | 9efca40dae2e75477af1a20df4e3e65bf8dfe93d (diff) | |
fix(cli): correctly handle tool invocation cancellation (#844)
Diffstat (limited to 'packages/core/src')
| -rw-r--r-- | packages/core/src/core/client.test.ts | 18 | ||||
| -rw-r--r-- | packages/core/src/core/client.ts | 5 | ||||
| -rw-r--r-- | packages/core/src/core/geminiChat.test.ts | 30 | ||||
| -rw-r--r-- | packages/core/src/core/geminiChat.ts | 9 |
4 files changed, 62 insertions, 0 deletions
diff --git a/packages/core/src/core/client.test.ts b/packages/core/src/core/client.test.ts index 180d74bb..cbbbd113 100644 --- a/packages/core/src/core/client.test.ts +++ b/packages/core/src/core/client.test.ts @@ -219,4 +219,22 @@ describe('Gemini Client (client.ts)', () => { ); }); }); + + describe('addHistory', () => { + it('should call chat.addHistory with the provided content', async () => { + const mockChat = { + addHistory: vi.fn(), + }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + client['chat'] = Promise.resolve(mockChat as any); + + const newContent = { + role: 'user', + parts: [{ text: 'New history item' }], + }; + await client.addHistory(newContent); + + expect(mockChat.addHistory).toHaveBeenCalledWith(newContent); + }); + }); }); diff --git a/packages/core/src/core/client.ts b/packages/core/src/core/client.ts index 0f2a1b8a..8b921ab1 100644 --- a/packages/core/src/core/client.ts +++ b/packages/core/src/core/client.ts @@ -58,6 +58,11 @@ export class GeminiClient { this.chat = this.startChat(); } + async addHistory(content: Content) { + const chat = await this.chat; + chat.addHistory(content); + } + getChat(): Promise<GeminiChat> { return this.chat; } diff --git a/packages/core/src/core/geminiChat.test.ts b/packages/core/src/core/geminiChat.test.ts index 6d18ebd9..dbed31b1 100644 --- a/packages/core/src/core/geminiChat.test.ts +++ b/packages/core/src/core/geminiChat.test.ts @@ -352,4 +352,34 @@ describe('GeminiChat', () => { expect(history[1].parts).toEqual([{ text: 'Visible text' }]); }); }); + + describe('addHistory', () => { + it('should add a new content item to the history', () => { + const newContent: Content = { + role: 'user', + parts: [{ text: 'A new message' }], + }; + chat.addHistory(newContent); + const history = chat.getHistory(); + expect(history.length).toBe(1); + expect(history[0]).toEqual(newContent); + }); + + it('should add multiple items correctly', () => { + const content1: Content = { + role: 'user', + parts: [{ text: 'Message 1' }], + }; + const content2: Content = { + role: 'model', + parts: [{ text: 'Message 2' }], + }; + chat.addHistory(content1); + chat.addHistory(content2); + const history = chat.getHistory(); + expect(history.length).toBe(2); + expect(history[0]).toEqual(content1); + expect(history[1]).toEqual(content2); + }); + }); }); diff --git a/packages/core/src/core/geminiChat.ts b/packages/core/src/core/geminiChat.ts index 54f74102..47f3f3a6 100644 --- a/packages/core/src/core/geminiChat.ts +++ b/packages/core/src/core/geminiChat.ts @@ -287,6 +287,15 @@ export class GeminiChat { return structuredClone(history); } + /** + * Adds a new entry to the chat history. + * + * @param content - The content to add to the history. + */ + addHistory(content: Content): void { + this.history.push(content); + } + private async *processStreamResponse( streamResponse: AsyncGenerator<GenerateContentResponse>, inputContent: Content, |
