fix(cli): correctly handle tool invocation cancellation (#844)

This commit is contained in:
N. Taylor Mullen
2025-06-08 11:14:45 -07:00
committed by GitHub
parent 9efca40dae
commit 241c404573
6 changed files with 156 additions and 4 deletions

View File

@@ -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);
});
});
});

View File

@@ -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;
}

View File

@@ -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);
});
});
});

View File

@@ -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,