fix(chat): 重新生成前撤销旧消息
Build Push and Deploy / docker-image (push) Successful in 1m47s
Build Push and Deploy / deploy-fallback-log (push) Has been skipped

This commit is contained in:
2026-06-08 14:38:52 +08:00
parent f7cd5ebfa7
commit 40cc355fff
5 changed files with 45 additions and 7 deletions
+8 -7
View File
@@ -251,7 +251,7 @@ export const AgentComposer = React.forwardRef<AgentComposerHandle, AgentComposer
</IconButton>
)
) : null}
<FormControl size="small" sx={{ minWidth: 102 }}>
<FormControl size="small" sx={{ minWidth: 96 }}>
<Select
value={approvalMode}
onChange={(event) =>
@@ -260,13 +260,13 @@ export const AgentComposer = React.forwardRef<AgentComposerHandle, AgentComposer
disabled={isHydrating || isStreaming}
aria-label="权限批准模式"
renderValue={(val) => (
<Box sx={{ display: "flex", alignItems: "center", gap: 0.5 }}>
<Box sx={{ display: "flex", alignItems: "center", gap: 0.45 }}>
{val === "always" ? (
<AdminPanelSettingsRounded sx={{ fontSize: 16, color: "inherit" }} />
<AdminPanelSettingsRounded sx={{ fontSize: 18, color: "inherit" }} />
) : (
<VerifiedUserRounded sx={{ fontSize: 16, color: "inherit" }} />
<VerifiedUserRounded sx={{ fontSize: 18, color: "inherit" }} />
)}
<Typography sx={{ fontSize: "0.78rem", fontWeight: 700, color: "inherit" }}>
<Typography sx={{ fontSize: "0.75rem", fontWeight: 600, color: "inherit" }}>
{val === "always" ? "始终允许" : "请求批准"}
</Typography>
</Box>
@@ -312,6 +312,7 @@ export const AgentComposer = React.forwardRef<AgentComposerHandle, AgentComposer
py: 0,
pl: 1,
pr: "28px !important",
minHeight: 36,
display: "flex",
alignItems: "center",
},
@@ -326,14 +327,14 @@ export const AgentComposer = React.forwardRef<AgentComposerHandle, AgentComposer
}}
>
<MenuItem value="request">
<VerifiedUserRounded className="icon" sx={{ mr: 1.5, mt: 0.2, fontSize: 18, color: "text.secondary" }} />
<VerifiedUserRounded className="icon" sx={{ mr: 1.5, mt: 0.15, fontSize: 18, color: "text.secondary" }} />
<Box>
<Typography className="title" sx={{ fontSize: "0.85rem", fontWeight: 700, color: "text.primary", mb: 0.2 }}></Typography>
<Typography sx={{ fontSize: "0.7rem", fontWeight: 500, color: "text.secondary", lineHeight: 1.3 }}></Typography>
</Box>
</MenuItem>
<MenuItem value="always">
<AdminPanelSettingsRounded className="icon" sx={{ mr: 1.5, mt: 0.2, fontSize: 18, color: "text.secondary" }} />
<AdminPanelSettingsRounded className="icon" sx={{ mr: 1.5, mt: 0.15, fontSize: 18, color: "text.secondary" }} />
<Box>
<Typography className="title" sx={{ fontSize: "0.85rem", fontWeight: 700, color: "text.primary", mb: 0.2 }}></Typography>
<Typography sx={{ fontSize: "0.7rem", fontWeight: 500, color: "text.secondary", lineHeight: 1.3 }}></Typography>
@@ -521,4 +521,33 @@ describe("useAgentChatSession", () => {
expect.anything(),
);
});
it("asks the backend to undo the previous user turn before regenerating", async () => {
listChatSessions.mockResolvedValue([]);
const { result } = renderHook(() =>
useAgentChatSession({
projectId: "project-1",
onToolCall: jest.fn(),
}),
);
await waitFor(() => expect(result.current.isHydrating).toBe(false));
await act(async () => {
await result.current.sendPrompt("重新分析压力异常");
});
await act(async () => {
await result.current.regenerate();
});
expect(streamAgentChat).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
message: "重新分析压力异常",
regenerateFromMessageIndex: 0,
}),
);
});
});
@@ -56,6 +56,7 @@ type UseAgentChatSessionOptions = {
type PromptRunOptions = {
prompt: string;
sessionIdOverride?: string;
regenerateFromMessageIndex?: number;
preparedMessages?: Message[];
userMessage?: Message;
assistantMessage?: Message;
@@ -609,6 +610,7 @@ export const useAgentChatSession = ({
async ({
prompt: rawPrompt,
sessionIdOverride,
regenerateFromMessageIndex,
preparedMessages,
userMessage,
assistantMessage,
@@ -644,6 +646,7 @@ export const useAgentChatSession = ({
sessionId: sessionIdOverride ?? sessionIdRef.current,
model: getModel?.(),
approvalMode: getApprovalMode?.(),
regenerateFromMessageIndex,
signal: controller.signal,
onEvent: (event) =>
applyStreamEvent(event, {
@@ -991,6 +994,7 @@ export const useAgentChatSession = ({
setMessages(nextMessages);
await runPrompt({
prompt: lastUserContent,
regenerateFromMessageIndex: lastUserIndex,
preparedMessages: [
...nextMessages,
nextUserMessage,