mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-04-29 20:20:57 +00:00
Merge pull request #2696 from QwenLM/feat/hooks-refactor-ui-event
refactor(ui): improve hook event handling with dedicated history items
This commit is contained in:
commit
06a0f4797d
12 changed files with 639 additions and 28 deletions
|
|
@ -3226,4 +3226,287 @@ describe('useGeminiStream', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('UserPromptSubmitBlocked Event', () => {
|
||||
it('should handle UserPromptSubmitBlocked event and add blocked history item', async () => {
|
||||
mockSendMessageStream.mockReturnValue(
|
||||
(async function* () {
|
||||
yield {
|
||||
type: ServerGeminiEventType.UserPromptSubmitBlocked,
|
||||
value: {
|
||||
reason: 'Hook blocked due to security policy',
|
||||
originalPrompt: 'This is the original user prompt',
|
||||
},
|
||||
};
|
||||
})(),
|
||||
);
|
||||
|
||||
const { result } = renderTestHook();
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitQuery('This is the original user prompt');
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'user_prompt_submit_blocked',
|
||||
reason: 'Hook blocked due to security policy',
|
||||
originalPrompt: 'This is the original user prompt',
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
|
||||
// Verify streaming state transitions correctly
|
||||
expect(result.current.streamingState).toBe(StreamingState.Idle);
|
||||
});
|
||||
|
||||
it('should move pending history item before adding UserPromptSubmitBlocked event', async () => {
|
||||
mockSendMessageStream.mockReturnValue(
|
||||
(async function* () {
|
||||
yield {
|
||||
type: ServerGeminiEventType.Content,
|
||||
value: 'Partial response before block',
|
||||
};
|
||||
yield {
|
||||
type: ServerGeminiEventType.UserPromptSubmitBlocked,
|
||||
value: {
|
||||
reason: 'Security violation detected',
|
||||
originalPrompt: 'Execute system command',
|
||||
},
|
||||
};
|
||||
})(),
|
||||
);
|
||||
|
||||
const { result } = renderTestHook();
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitQuery('Execute system command');
|
||||
});
|
||||
|
||||
// Verify content was added first
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'gemini',
|
||||
text: 'Partial response before block',
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
|
||||
// Then verify blocked event was added
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'user_prompt_submit_blocked',
|
||||
reason: 'Security violation detected',
|
||||
originalPrompt: 'Execute system command',
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('StopHookLoop Event', () => {
|
||||
it('should handle StopHookLoop event and add stop hook loop history item', async () => {
|
||||
mockSendMessageStream.mockReturnValue(
|
||||
(async function* () {
|
||||
yield {
|
||||
type: ServerGeminiEventType.StopHookLoop,
|
||||
value: {
|
||||
iterationCount: 3,
|
||||
reasons: [
|
||||
'Reason 1: Continue analysis',
|
||||
'Reason 2: More details needed',
|
||||
'Reason 3: Incomplete response',
|
||||
],
|
||||
stopHookCount: 3,
|
||||
},
|
||||
};
|
||||
})(),
|
||||
);
|
||||
|
||||
const { result } = renderTestHook();
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitQuery('test query with stop hooks');
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'stop_hook_loop',
|
||||
iterationCount: 3,
|
||||
reasons: [
|
||||
'Reason 1: Continue analysis',
|
||||
'Reason 2: More details needed',
|
||||
'Reason 3: Incomplete response',
|
||||
],
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
|
||||
// Verify streaming state transitions correctly
|
||||
expect(result.current.streamingState).toBe(StreamingState.Idle);
|
||||
});
|
||||
|
||||
it('should move pending history item before adding StopHookLoop event', async () => {
|
||||
mockSendMessageStream.mockReturnValue(
|
||||
(async function* () {
|
||||
yield {
|
||||
type: ServerGeminiEventType.Content,
|
||||
value: 'Initial response before loop',
|
||||
};
|
||||
yield {
|
||||
type: ServerGeminiEventType.StopHookLoop,
|
||||
value: {
|
||||
iterationCount: 5,
|
||||
reasons: ['Hook reason 1', 'Hook reason 2'],
|
||||
stopHookCount: 2,
|
||||
},
|
||||
};
|
||||
})(),
|
||||
);
|
||||
|
||||
const { result } = renderTestHook();
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitQuery('query triggering stop hooks');
|
||||
});
|
||||
|
||||
// Verify content was added first
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'gemini',
|
||||
text: 'Initial response before loop',
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
|
||||
// Then verify stop hook loop event was added
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'stop_hook_loop',
|
||||
iterationCount: 5,
|
||||
reasons: ['Hook reason 1', 'Hook reason 2'],
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle single iteration StopHookLoop event', async () => {
|
||||
mockSendMessageStream.mockReturnValue(
|
||||
(async function* () {
|
||||
yield {
|
||||
type: ServerGeminiEventType.StopHookLoop,
|
||||
value: {
|
||||
iterationCount: 1,
|
||||
reasons: ['Single hook execution'],
|
||||
},
|
||||
};
|
||||
})(),
|
||||
);
|
||||
|
||||
const { result } = renderTestHook();
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitQuery('single iteration query');
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'stop_hook_loop',
|
||||
iterationCount: 1,
|
||||
reasons: ['Single hook execution'],
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('HookSystemMessage Event', () => {
|
||||
it('should handle HookSystemMessage event and add stop_hook_system_message history item', async () => {
|
||||
mockSendMessageStream.mockReturnValue(
|
||||
(async function* () {
|
||||
yield {
|
||||
type: ServerGeminiEventType.HookSystemMessage,
|
||||
value: '🔄 Ralph iteration 3 | No completion promise set',
|
||||
};
|
||||
})(),
|
||||
);
|
||||
|
||||
const { result } = renderTestHook();
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitQuery('test query with hook system message');
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'stop_hook_system_message',
|
||||
message: '🔄 Ralph iteration 3 | No completion promise set',
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
|
||||
expect(result.current.streamingState).toBe(StreamingState.Idle);
|
||||
});
|
||||
|
||||
it('should display HookSystemMessage after content', async () => {
|
||||
mockSendMessageStream.mockReturnValue(
|
||||
(async function* () {
|
||||
yield {
|
||||
type: ServerGeminiEventType.Content,
|
||||
value: 'Here is the response',
|
||||
};
|
||||
yield {
|
||||
type: ServerGeminiEventType.HookSystemMessage,
|
||||
value: 'Stop hook feedback message',
|
||||
};
|
||||
})(),
|
||||
);
|
||||
|
||||
const { result } = renderTestHook();
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitQuery(
|
||||
'query with response and hook message',
|
||||
);
|
||||
});
|
||||
|
||||
// Verify content was added
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'gemini',
|
||||
text: 'Here is the response',
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
|
||||
// Verify hook system message was added
|
||||
await waitFor(() => {
|
||||
expect(mockAddItem).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
type: 'stop_hook_system_message',
|
||||
message: 'Stop hook feedback message',
|
||||
}),
|
||||
expect.any(Number),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -972,6 +972,53 @@ export const useGeminiStream = (
|
|||
});
|
||||
}, [handleLoopDetectionConfirmation]);
|
||||
|
||||
const handleUserPromptSubmitBlockedEvent = useCallback(
|
||||
(
|
||||
value: { reason: string; originalPrompt: string },
|
||||
userMessageTimestamp: number,
|
||||
) => {
|
||||
if (pendingHistoryItemRef.current) {
|
||||
addItem(pendingHistoryItemRef.current, userMessageTimestamp);
|
||||
setPendingHistoryItem(null);
|
||||
}
|
||||
addItem(
|
||||
{
|
||||
type: 'user_prompt_submit_blocked',
|
||||
reason: value.reason,
|
||||
originalPrompt: value.originalPrompt,
|
||||
} as HistoryItemWithoutId,
|
||||
userMessageTimestamp,
|
||||
);
|
||||
},
|
||||
[addItem, pendingHistoryItemRef, setPendingHistoryItem],
|
||||
);
|
||||
|
||||
const handleStopHookLoopEvent = useCallback(
|
||||
(
|
||||
value: {
|
||||
iterationCount: number;
|
||||
reasons: string[];
|
||||
stopHookCount: number;
|
||||
},
|
||||
userMessageTimestamp: number,
|
||||
) => {
|
||||
if (pendingHistoryItemRef.current) {
|
||||
addItem(pendingHistoryItemRef.current, userMessageTimestamp);
|
||||
setPendingHistoryItem(null);
|
||||
}
|
||||
addItem(
|
||||
{
|
||||
type: 'stop_hook_loop',
|
||||
iterationCount: value.iterationCount,
|
||||
reasons: value.reasons,
|
||||
stopHookCount: value.stopHookCount,
|
||||
} as HistoryItemWithoutId,
|
||||
userMessageTimestamp,
|
||||
);
|
||||
},
|
||||
[addItem, pendingHistoryItemRef, setPendingHistoryItem],
|
||||
);
|
||||
|
||||
const processGeminiStreamEvents = useCallback(
|
||||
async (
|
||||
stream: AsyncIterable<GeminiEvent>,
|
||||
|
|
@ -1053,14 +1100,24 @@ export const useGeminiStream = (
|
|||
}
|
||||
break;
|
||||
case ServerGeminiEventType.HookSystemMessage:
|
||||
// Display system message from hooks (e.g., Ralph Loop iteration info)
|
||||
// This is handled as a content event to show in the UI
|
||||
geminiMessageBuffer = handleContentEvent(
|
||||
event.value + '\n',
|
||||
geminiMessageBuffer,
|
||||
// Display system message from Stop hooks with "Stop says:" prefix
|
||||
addItem(
|
||||
{
|
||||
type: 'stop_hook_system_message',
|
||||
message: event.value,
|
||||
} as HistoryItemWithoutId,
|
||||
userMessageTimestamp,
|
||||
);
|
||||
break;
|
||||
case ServerGeminiEventType.UserPromptSubmitBlocked:
|
||||
handleUserPromptSubmitBlockedEvent(
|
||||
event.value,
|
||||
userMessageTimestamp,
|
||||
);
|
||||
break;
|
||||
case ServerGeminiEventType.StopHookLoop:
|
||||
handleStopHookLoopEvent(event.value, userMessageTimestamp);
|
||||
break;
|
||||
default: {
|
||||
// enforces exhaustive switch-case
|
||||
const unreachable: never = event;
|
||||
|
|
@ -1089,6 +1146,9 @@ export const useGeminiStream = (
|
|||
setThought,
|
||||
pendingHistoryItemRef,
|
||||
setPendingHistoryItem,
|
||||
handleUserPromptSubmitBlockedEvent,
|
||||
handleStopHookLoopEvent,
|
||||
addItem,
|
||||
],
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue