feat(gateway): wire safe-point runtime cancellation for agent.cancel
This commit is contained in:
@@ -73,6 +73,43 @@ describe('NativeAgent', () => {
|
||||
expect(mockSession.addMessage).toHaveBeenNthCalledWith(1, { role: 'user', content: 'Hi' });
|
||||
expect(mockSession.addMessage).toHaveBeenNthCalledWith(2, { role: 'assistant', content: 'Hello!' });
|
||||
});
|
||||
|
||||
it('supports cancellation during single-turn model wait', async () => {
|
||||
let release!: () => void;
|
||||
const blocked = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
|
||||
const mockClient: ModelClient = {
|
||||
chat: vi.fn(async () => {
|
||||
await blocked;
|
||||
return {
|
||||
content: 'Late response',
|
||||
stopReason: 'end_turn',
|
||||
usage: { inputTokens: 10, outputTokens: 5 },
|
||||
} satisfies ChatResponse;
|
||||
}),
|
||||
};
|
||||
|
||||
const agent = new NativeAgent({
|
||||
modelClient: mockClient,
|
||||
systemPrompt: 'You are helpful.',
|
||||
});
|
||||
|
||||
const pending = agent.process('Please wait');
|
||||
await new Promise<void>((resolve) => queueMicrotask(resolve));
|
||||
expect(agent.isCancellable()).toBe(true);
|
||||
|
||||
agent.cancel();
|
||||
release();
|
||||
|
||||
const response = await pending;
|
||||
expect(response).toBe('Operation cancelled by user.');
|
||||
expect(agent.isCancellable()).toBe(false);
|
||||
|
||||
const history = agent.getHistory();
|
||||
expect(history[history.length - 1]).toEqual({ role: 'assistant', content: 'Operation cancelled by user.' });
|
||||
});
|
||||
});
|
||||
|
||||
// Simple test tool
|
||||
|
||||
Reference in New Issue
Block a user