Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/core/src/core/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2988,6 +2988,7 @@ ${JSON.stringify(
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenCalledWith(
partToString(request),
'Hook Response',
false,
);

// Map should be empty
Expand Down Expand Up @@ -3029,6 +3030,7 @@ ${JSON.stringify(
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenCalledWith(
partToString(request),
'Response 1\nResponse 2',
false,
);

expect(client['hookStateMap'].size).toBe(0);
Expand Down Expand Up @@ -3059,6 +3061,7 @@ ${JSON.stringify(
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenCalledWith(
partToString(request), // Should be 'Do something'
expect.stringContaining('Ok'),
false,
);
});

Expand Down Expand Up @@ -3229,6 +3232,21 @@ ${JSON.stringify(
expect.anything(),
undefined,
);

// First call should have stopHookActive=false, retry should have stopHookActive=true
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenCalledTimes(2);
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenNthCalledWith(
1,
expect.any(String),
expect.any(String),
false,
);
expect(mockHookSystem.fireAfterAgentEvent).toHaveBeenNthCalledWith(
2,
expect.any(String),
expect.any(String),
true,
);
});

it('should call resetChat when AfterAgent hook returns shouldClearContext: true', async () => {
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export class GeminiClient {
currentRequest: PartListUnion,
prompt_id: string,
turn?: Turn,
stopHookActive: boolean = false,
): Promise<DefaultHookOutput | undefined> {
const hookState = this.hookStateMap.get(prompt_id);
// Only fire on the outermost call (when activeCalls is 1)
Expand All @@ -202,7 +203,11 @@ export class GeminiClient {

const hookOutput = await this.config
.getHookSystem()
?.fireAfterAgentEvent(partToString(finalRequest), finalResponseText);
?.fireAfterAgentEvent(
partToString(finalRequest),
finalResponseText,
stopHookActive,
);

return hookOutput;
}
Expand Down Expand Up @@ -793,6 +798,7 @@ export class GeminiClient {
turns: number = MAX_TURNS,
isInvalidStreamRetry: boolean = false,
displayContent?: PartListUnion,
stopHookActive: boolean = false,
): AsyncGenerator<ServerGeminiStreamEvent, Turn> {
if (!isInvalidStreamRetry) {
this.config.resetTurn();
Expand Down Expand Up @@ -857,6 +863,7 @@ export class GeminiClient {
request,
prompt_id,
turn,
stopHookActive,
);

// Cast to AfterAgentHookOutput for access to shouldClearContext()
Expand Down Expand Up @@ -894,6 +901,13 @@ export class GeminiClient {
if (contextCleared) {
await this.resetChat();
}
// Decrement activeCalls so the inner sendMessageStream sees
// activeCalls === 1 and fires the AfterAgent hook again with
// stopHookActive=true, allowing the hook to break the retry loop.
const retryHookState = this.hookStateMap.get(prompt_id);
if (retryHookState) {
retryHookState.activeCalls--;
}
const continueRequest = [{ text: continueReason }];
yield* this.sendMessageStream(
continueRequest,
Expand All @@ -902,6 +916,7 @@ export class GeminiClient {
boundedTurns - 1,
false,
displayContent,
true, // stopHookActive: signal retry to AfterAgent hooks
);
}
}
Expand Down
Loading