Skip to content

Commit b20bc9d

Browse files
committed
feat: improve model request retry flow
1 parent f645b75 commit b20bc9d

23 files changed

Lines changed: 533 additions & 58 deletions

File tree

‎apps/kimi-code/src/app/appEventSubscriptions.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type { SideChatState } from "../components/sidebars/ChatSidebars";
2626
import { mergeDesktopInventory } from "../desktopInventory";
2727
import { t } from "../i18n";
2828
import { isSameLiveUserMessage } from "../liveUserMessage";
29+
import { isRetryConfirmationPayload } from "../retryConfirmation";
2930
import { loadDesktopState } from "../store";
3031
import {
3132
isSubagentEvent,
@@ -782,7 +783,9 @@ export function subscribeToAppEvents({
782783
notifyConversation({
783784
sessionId: event.payload.sessionId,
784785
kind: attention.kind,
785-
content: attention.content,
786+
content: isRetryConfirmationPayload(interaction.payload)
787+
? t("retryConfirmation.message")
788+
: attention.content,
786789
});
787790
}
788791
setInteractions((current) => ({

‎apps/kimi-code/src/chat/eventParsing.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const CHAT_EVENT_TYPES = new Set([
88
"turn.step.started",
99
"turn.step.completed",
1010
"turn.step.interrupted",
11+
"turn.step.retrying",
1112
"assistant.delta",
1213
"assistant.content",
1314
"thinking.delta",

‎apps/kimi-code/src/chat/liveTurns.ts‎

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { AgentPromptSubmitStatus } from "../agentRpc";
22
import { projectLiveUserMessage } from "../liveUserMessage";
33
import { parseSkillPromptDisplay } from "../prompt/skills";
4-
import type { PluginCommandDisplay } from "./messages";
54
import { conciseError } from "../utils/errors";
5+
import type { PluginCommandDisplay } from "./messages";
6+
import { clearRetryStatus, normalizeRetryAttempt } from "./retryStatus";
67
import type {
78
AgentChatEvent,
89
AgentContentPart,
@@ -53,6 +54,10 @@ export interface LiveStep {
5354
interruption?: string;
5455
}
5556

57+
export interface LiveRetryState {
58+
attempt: number;
59+
}
60+
5661
export interface InFlightTurn {
5762
promptId?: string;
5863
userMessageId?: string;
@@ -65,6 +70,7 @@ export interface InFlightTurn {
6570
status: LiveTurnStatus;
6671
durationMs?: number;
6772
steps: LiveStep[];
73+
retry?: LiveRetryState;
6874
fileChanges?: readonly TurnFileChange[];
6975
error?: string;
7076
historyBoundaryId?: string;
@@ -369,6 +375,7 @@ export function reduceAgentChatEvent(
369375
return {
370376
...next,
371377
status: event.reason,
378+
retry: undefined,
372379
durationMs:
373380
event.durationMs ??
374381
Math.max(0, Date.now() - Date.parse(next.createdAt)),
@@ -428,7 +435,7 @@ export function reduceAgentChatEvent(
428435
}
429436
: step,
430437
);
431-
return { ...next, steps };
438+
return { ...next, steps, retry: undefined };
432439
}
433440
case "turn.step.interrupted": {
434441
const steps = next.steps.map((step) =>
@@ -440,32 +447,52 @@ export function reduceAgentChatEvent(
440447
}
441448
: step,
442449
);
443-
return { ...next, steps };
450+
return { ...next, steps, retry: undefined };
444451
}
452+
case "turn.step.retrying":
453+
return {
454+
...next,
455+
status: "running",
456+
retry: {
457+
attempt: normalizeRetryAttempt(event.failedAttempt),
458+
},
459+
};
445460
case "assistant.delta":
446-
return appendLiveContent(next, "text", event.delta);
461+
return appendLiveContent(clearRetryStatus(next), "text", event.delta);
447462
case "assistant.content":
448-
return withCurrentStep(next, (step) => ({
463+
return withCurrentStep(clearRetryStatus(next), (step) => ({
449464
...step,
450465
blocks: [...step.blocks, { kind: "content", content: event.content }],
451466
}));
452467
case "thinking.delta":
453-
return appendLiveContent(next, "thinking", event.delta);
468+
return appendLiveContent(
469+
clearRetryStatus(next),
470+
"thinking",
471+
event.delta,
472+
);
454473
case "tool.call.delta":
455-
return updateLiveTool(next, event.toolCallId, (tool) => ({
456-
...tool,
457-
name: event.name ?? tool.name,
458-
argumentsText: tool.argumentsText + (event.argumentsPart ?? ""),
459-
}));
474+
return updateLiveTool(
475+
clearRetryStatus(next),
476+
event.toolCallId,
477+
(tool) => ({
478+
...tool,
479+
name: event.name ?? tool.name,
480+
argumentsText: tool.argumentsText + (event.argumentsPart ?? ""),
481+
}),
482+
);
460483
case "tool.call.started":
461-
return updateLiveTool(next, event.toolCallId, (tool) => ({
462-
...tool,
463-
name: event.name,
464-
input: event.args,
465-
description: event.description,
466-
display: event.display,
467-
status: "running",
468-
}));
484+
return updateLiveTool(
485+
clearRetryStatus(next),
486+
event.toolCallId,
487+
(tool) => ({
488+
...tool,
489+
name: event.name,
490+
input: event.args,
491+
description: event.description,
492+
display: event.display,
493+
status: "running",
494+
}),
495+
);
469496
case "tool.progress":
470497
return updateLiveTool(next, event.toolCallId, (tool) => ({
471498
...tool,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function normalizeRetryAttempt(failedAttempt: number): number {
2+
if (!Number.isFinite(failedAttempt)) return 1;
3+
return Math.max(1, Math.trunc(failedAttempt));
4+
}
5+
6+
export function clearRetryStatus<T extends { retry?: unknown }>(value: T): T {
7+
if (value.retry === undefined) return value;
8+
return { ...value, retry: undefined };
9+
}
10+
11+
export function isVisibleRetryStep(
12+
step: {
13+
step: number;
14+
stepId?: string;
15+
blocks: readonly unknown[];
16+
interruption?: string;
17+
},
18+
steeredPrompts: readonly { anchorStepKey?: string }[],
19+
): boolean {
20+
if (step.blocks.length > 0 || Boolean(step.interruption)) return true;
21+
const stepKey = step.stepId ?? `step-${step.step}`;
22+
return steeredPrompts.some((item) => item.anchorStepKey === stepKey);
23+
}

‎apps/kimi-code/src/components/ComposerDock.tsx‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ import {
6161
ApprovalCard,
6262
PlanReviewCard,
6363
QuestionCard,
64+
RetryConfirmationCard,
6465
isPlanReviewInteraction,
66+
isRetryConfirmationInteraction,
6567
} from "./InteractionCards";
6668
import { RemixSparklingLineIcon } from "./RemixSparklingLineIcon";
6769
import { McpStatusPopover } from "./McpStatusPopover";
@@ -242,14 +244,27 @@ export function ComposerDock({
242244
return (
243245
<div className="composer-dock">
244246
{activeQuestion && (
245-
<QuestionCard
246-
key={activeQuestion.id}
247-
interaction={activeQuestion}
248-
busy={resolvingInteraction === activeQuestion.id}
249-
onRespond={(response) =>
250-
void respondToInteraction(activeQuestion, response)
251-
}
252-
/>
247+
isRetryConfirmationInteraction(activeQuestion) ? (
248+
<RetryConfirmationCard
249+
key={activeQuestion.id}
250+
busy={resolvingInteraction === activeQuestion.id}
251+
onCancel={() =>
252+
void respondToInteraction(activeQuestion, null)
253+
}
254+
onContinue={(response) =>
255+
void respondToInteraction(activeQuestion, response)
256+
}
257+
/>
258+
) : (
259+
<QuestionCard
260+
key={activeQuestion.id}
261+
interaction={activeQuestion}
262+
busy={resolvingInteraction === activeQuestion.id}
263+
onRespond={(response) =>
264+
void respondToInteraction(activeQuestion, response)
265+
}
266+
/>
267+
)
253268
)}
254269
{activeApproval && isPlanReviewInteraction(activeApproval) ? (
255270
<PlanReviewCard

‎apps/kimi-code/src/components/InteractionCards.tsx‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import {
99
} from "lucide-react";
1010

1111
import { t } from "../i18n";
12+
import {
13+
isRetryConfirmationPayload,
14+
retryConfirmationResponse,
15+
} from "../retryConfirmation";
1216
import type {
1317
AgentInteraction,
1418
ApprovalPayload,
@@ -23,6 +27,57 @@ export function isPlanReviewInteraction(interaction: AgentInteraction): boolean
2327
return payload.display?.kind === "plan_review";
2428
}
2529

30+
export function isRetryConfirmationInteraction(
31+
interaction: AgentInteraction,
32+
): boolean {
33+
return isRetryConfirmationPayload(interaction.payload);
34+
}
35+
36+
export function RetryConfirmationCard({
37+
busy,
38+
onCancel,
39+
onContinue,
40+
}: {
41+
busy: boolean;
42+
onCancel: () => void;
43+
onContinue: (response: QuestionResponse) => void;
44+
}) {
45+
return (
46+
<section
47+
className="interaction-card retry-confirmation-card"
48+
aria-live="polite"
49+
>
50+
<div className="interaction-card-heading">
51+
<span className="interaction-card-icon">
52+
<RefreshCw size={18} />
53+
</span>
54+
<div>
55+
<strong>{t("retryConfirmation.message")}</strong>
56+
</div>
57+
</div>
58+
<div className="interaction-card-actions">
59+
<button
60+
type="button"
61+
className="interaction-secondary"
62+
disabled={busy}
63+
onClick={onCancel}
64+
>
65+
{t("common.cancel")}
66+
</button>
67+
<button
68+
type="button"
69+
className="interaction-primary"
70+
disabled={busy}
71+
onClick={() => onContinue(retryConfirmationResponse())}
72+
>
73+
{busy ? <span className="spinner light" /> : <RefreshCw size={14} />}
74+
{t("retryConfirmation.continue")}
75+
</button>
76+
</div>
77+
</section>
78+
);
79+
}
80+
2681
export function QuestionCard({
2782
interaction,
2883
busy,

‎apps/kimi-code/src/components/chat/ConversationViews.tsx‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
type QueuedPrompt,
4444
type RemoteQueuedPrompt,
4545
} from "../../chat/liveTurns";
46+
import { isVisibleRetryStep } from "../../chat/retryStatus";
4647
import {
4748
mediaSourceUrl,
4849
messagePluginCommand,
@@ -164,6 +165,9 @@ export function LiveTurnView({
164165
onPluginCommandOpen: (command: PluginCommandDetail) => void;
165166
}) {
166167
const hasBlocks = turn.steps.some((step) => step.blocks.length > 0);
168+
const visibleSteps = turn.steps.filter((step) =>
169+
isVisibleRetryStep(step, turn.steeredPrompts),
170+
);
167171
const streaming = isTurnRunning(turn);
168172

169173
return (
@@ -200,7 +204,7 @@ export function LiveTurnView({
200204
</article>
201205
<article className={`message assistant-message live-turn ${turn.status}`}>
202206
<div className="assistant-body">
203-
{turn.steps.map((step) => {
207+
{visibleSteps.map((step) => {
204208
const stepKey = liveStepKey(step.step, step.stepId);
205209
const steeredPrompts = turn.steeredPrompts.filter(
206210
(item) => item.anchorStepKey === stepKey,
@@ -302,6 +306,7 @@ export function LiveTurnView({
302306
<AssistantResponseStatus
303307
running={isTurnRunning(turn)}
304308
durationMs={turn.durationMs}
309+
retryAttempt={turn.retry?.attempt}
305310
/>
306311
</div>
307312
</article>
@@ -504,27 +509,35 @@ export function QueuedPromptList({
504509
export function AssistantResponseStatus({
505510
running,
506511
durationMs,
512+
retryAttempt,
507513
}: {
508514
running: boolean;
509515
durationMs?: number;
516+
retryAttempt?: number;
510517
}) {
511518
if (!running && durationMs === undefined) return null;
512519
return (
513-
<div
514-
className={`assistant-response-status ${running ? "thinking" : "elapsed"}`}
515-
aria-live="polite"
516-
>
517-
{running ? (
518-
<>
519-
<span>{t("assistant.thinking")}</span>
520-
<span className="assistant-thinking-dots" aria-hidden="true">
521-
<i />
522-
<i />
523-
<i />
524-
</span>
525-
</>
526-
) : (
527-
<span>{t("assistant.elapsed", { duration: formatElapsedDuration(durationMs ?? 0) })}</span>
520+
<div className="assistant-response-status-group" aria-live="polite">
521+
<div
522+
className={`assistant-response-status ${running ? "thinking" : "elapsed"}`}
523+
>
524+
{running ? (
525+
<>
526+
<span>{t("assistant.thinking")}</span>
527+
<span className="assistant-thinking-dots" aria-hidden="true">
528+
<i />
529+
<i />
530+
<i />
531+
</span>
532+
</>
533+
) : (
534+
<span>{t("assistant.elapsed", { duration: formatElapsedDuration(durationMs ?? 0) })}</span>
535+
)}
536+
</div>
537+
{running && retryAttempt !== undefined && (
538+
<div className="assistant-retry-status">
539+
{t("assistant.retrying", { attempt: retryAttempt })}
540+
</div>
528541
)}
529542
</div>
530543
);

0 commit comments

Comments
 (0)