圆桌 Step1:支持多澄清实时展示与合并提交
流式解析 ask_clarification 逐题渲染澄清卡;草稿恢复 selectedAgents 修复参与角色;Step3 隐藏左侧栏让报告区全宽。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
838cae0d2d
commit
23d6f9efc2
9 files changed
+376
-136
No files matched your search
@@ -22,7 +22,11 @@ import {
|
||||
type IntentStreamPhase,
|
||||
type IntentSummary,
|
||||
} from "@/roundtable-planning/api/intent";
|
||||
import { formatIntentClarificationAnswer, type IntentClarificationSelection } from "@/roundtable-planning/components/IntentClarificationCard";
|
||||
import {
|
||||
clarificationKey,
|
||||
formatIntentClarificationAnswer,
|
||||
type IntentClarificationSelection,
|
||||
} from "@/roundtable-planning/components/IntentClarificationCard";
|
||||
import type { ClarificationPayload } from "@/roundtable-planning/api/multi-agent";
|
||||
import {
|
||||
nowTime,
|
||||
@@ -38,7 +42,15 @@ export type InteractiveMessage = {
|
||||
time: string;
|
||||
streaming?: boolean;
|
||||
phase?: string;
|
||||
/** First clarification (back-compat); prefer `clarifications`. */
|
||||
clarification?: ClarificationPayload;
|
||||
/**
|
||||
* All `ask_clarification` questions the agent asked this turn (the agent may
|
||||
* ask several at once). Populated live from the stream so each question card
|
||||
* appears in real-time. Each carries a stable `id` (tool_call_id) for keying
|
||||
* per-question selections.
|
||||
*/
|
||||
clarifications?: ClarificationPayload[];
|
||||
/**
|
||||
* ChainOfThought 折叠卡的步骤累积。每次 `onPhaseChange` 都会把当前 phase + toolName
|
||||
* 拼成 `phase:toolName` 作为 key,与 Step 2 的 `Step2Dialogue.steps` 同形。
|
||||
@@ -86,23 +98,30 @@ export interface UseStep1IntentReturn {
|
||||
intentInitErrorDetail: IntentErrorDetail | null;
|
||||
isIntentSending: boolean;
|
||||
pendingIntentClarificationMsgId: number | null;
|
||||
intentClarificationSelections: Map<number, IntentClarificationSelection>;
|
||||
/** Per-question selections, keyed by `clarificationKey(msgId, clarId, index)`. */
|
||||
intentClarificationSelections: Map<string, IntentClarificationSelection>;
|
||||
interactiveMessages: InteractiveMessage[];
|
||||
timelineEntries: TimelineEntry[];
|
||||
// ─── Mutators ────────────────────────────────────────────────────────
|
||||
setIntentClarificationSelections: React.Dispatch<
|
||||
React.SetStateAction<Map<number, IntentClarificationSelection>>
|
||||
React.SetStateAction<Map<string, IntentClarificationSelection>>
|
||||
>;
|
||||
// ─── Handlers ────────────────────────────────────────────────────────
|
||||
/** 清空当前 Step 1 状态并重新拉取 intent thread。 */
|
||||
runStep1Init: () => void;
|
||||
/** 把 `userText` 作为用户气泡发给 intent agent,流式渲染回复。 */
|
||||
sendIntentMessage: (userText: string) => Promise<void>;
|
||||
/** 提交澄清卡片的选项 / 自定义回答。 */
|
||||
submitIntentClarification: (
|
||||
/**
|
||||
* 提交某条 agent 消息的全部澄清问题答案 —— 逐题取各自选择,拼成一条合并消息
|
||||
* 发回。至少答一题才会发送。单题时退化为发送该题答案。
|
||||
*
|
||||
* `overrideByKey`:单题「点击即发送」用 —— 单选卡点击选项时选择还没写进 state
|
||||
* map(setState 异步),把该题的即时选择按 `clarificationKey` 传进来直接用。
|
||||
*/
|
||||
submitIntentClarifications: (
|
||||
msgId: number,
|
||||
payload: ClarificationPayload,
|
||||
overrideSelection?: IntentClarificationSelection,
|
||||
clarifications: ClarificationPayload[],
|
||||
overrideByKey?: Record<string, IntentClarificationSelection>,
|
||||
) => void;
|
||||
/** Abort 当前 intent 流。气泡尾部追加「(已暂停)」并 toast。 */
|
||||
stopIntentStream: () => void;
|
||||
@@ -143,7 +162,7 @@ export function useStep1Intent(options: UseStep1IntentOptions): UseStep1IntentRe
|
||||
const [pendingIntentClarificationMsgId, setPendingIntentClarificationMsgId] =
|
||||
useState<number | null>(null);
|
||||
const [intentClarificationSelections, setIntentClarificationSelections] = useState<
|
||||
Map<number, IntentClarificationSelection>
|
||||
Map<string, IntentClarificationSelection>
|
||||
>(new Map());
|
||||
|
||||
// 原始页面带了一段写死的 demo 对话,只是首次进入 Step 1 还没初始化前的占位;
|
||||
@@ -355,10 +374,53 @@ export function useStep1Intent(options: UseStep1IntentOptions): UseStep1IntentRe
|
||||
}),
|
||||
);
|
||||
},
|
||||
onClarification: (clarification, index) => {
|
||||
// 实时把每条澄清问题灌进气泡 —— 用户能边流边看到「协助过程」逐个出现,
|
||||
// 而不是等终态帧(终态帧只带第 1 个问题)。同时往步骤链补一条
|
||||
// 「追问 N:<问题>」,与「强化步骤链」诉求对齐。
|
||||
const clarId = clarification.id ?? `idx-${index}`;
|
||||
const stepKey = `clarification:${clarId}`;
|
||||
const qLabel = clarification.question
|
||||
? `追问 ${index + 1}:${
|
||||
clarification.question.length > 24
|
||||
? clarification.question.slice(0, 24) + "…"
|
||||
: clarification.question
|
||||
}`
|
||||
: `追问 ${index + 1}`;
|
||||
setInteractiveMessages((prev) =>
|
||||
prev.map((m) => {
|
||||
if (m.id !== agentMsgId) return m;
|
||||
const list = m.clarifications ?? [];
|
||||
// 按 id 去重 / 覆盖,流式重发同一条时不重复 push。
|
||||
const existingIdx = list.findIndex(
|
||||
(c) => (c.id ?? "") === (clarification.id ?? ""),
|
||||
);
|
||||
const nextList =
|
||||
existingIdx >= 0 && clarification.id
|
||||
? list.map((c, i) => (i === existingIdx ? clarification : c))
|
||||
: [...list, clarification];
|
||||
const steps = m.steps ?? [];
|
||||
const hasStep = steps.some((s) => s.key === stepKey);
|
||||
const nextSteps = hasStep
|
||||
? steps
|
||||
: [
|
||||
...steps,
|
||||
{
|
||||
id: `${agentMsgId}-clar-${clarId}`,
|
||||
key: stepKey,
|
||||
label: qLabel,
|
||||
ts: nowTime(),
|
||||
},
|
||||
];
|
||||
return { ...m, clarifications: nextList, steps: nextSteps };
|
||||
}),
|
||||
);
|
||||
},
|
||||
});
|
||||
const parsedFromContent = splitIntentReadyBlock(res.content || "").summary;
|
||||
const resolvedSummary = res.summary ?? parsedFromContent;
|
||||
|
||||
const finalClarifications = resolvedSummary ? [] : res.clarifications;
|
||||
setInteractiveMessages((prev) =>
|
||||
prev.map((m) =>
|
||||
m.id === agentMsgId
|
||||
@@ -366,9 +428,13 @@ export function useStep1Intent(options: UseStep1IntentOptions): UseStep1IntentRe
|
||||
...m,
|
||||
content: res.content || m.content || "(无文本输出)",
|
||||
streaming: false,
|
||||
// 终态以 res.clarifications 为权威集合(覆盖流式期增量构建的),
|
||||
// 既保证多问题完整,也在解析出 summary 时清空澄清卡。
|
||||
clarification: resolvedSummary
|
||||
? undefined
|
||||
: res.clarification ?? undefined,
|
||||
clarifications:
|
||||
finalClarifications.length > 0 ? finalClarifications : undefined,
|
||||
}
|
||||
: m,
|
||||
),
|
||||
@@ -390,11 +456,14 @@ export function useStep1Intent(options: UseStep1IntentOptions): UseStep1IntentRe
|
||||
},
|
||||
]);
|
||||
triggerToast("✅ 任务理解完成,可进入多智能体圆桌研讨");
|
||||
} else if (res.status === "clarification" && res.clarification) {
|
||||
} else if (res.status === "clarification" && res.clarifications.length > 0) {
|
||||
setPendingIntentClarificationMsgId(agentMsgId);
|
||||
setIntentClarificationSelections((prev) => {
|
||||
const next = new Map(prev);
|
||||
next.set(agentMsgId, { selectedIds: [], custom: "" });
|
||||
res.clarifications.forEach((c, i) => {
|
||||
const key = clarificationKey(agentMsgId, c.id, i);
|
||||
if (!next.has(key)) next.set(key, { selectedIds: [], custom: "" });
|
||||
});
|
||||
return next;
|
||||
});
|
||||
} else if (res.status === "error") {
|
||||
@@ -448,24 +517,37 @@ export function useStep1Intent(options: UseStep1IntentOptions): UseStep1IntentRe
|
||||
[intentThreadId, triggerToast],
|
||||
);
|
||||
|
||||
const submitIntentClarification = useCallback(
|
||||
const submitIntentClarifications = useCallback(
|
||||
(
|
||||
msgId: number,
|
||||
payload: ClarificationPayload,
|
||||
overrideSelection?: IntentClarificationSelection,
|
||||
clarifications: ClarificationPayload[],
|
||||
overrideByKey?: Record<string, IntentClarificationSelection>,
|
||||
) => {
|
||||
const selection =
|
||||
overrideSelection ??
|
||||
intentClarificationSelections.get(msgId) ?? {
|
||||
selectedIds: [],
|
||||
custom: "",
|
||||
};
|
||||
const answer = formatIntentClarificationAnswer(payload, selection);
|
||||
if (!answer) {
|
||||
triggerToast("请先选择选项或输入回答");
|
||||
// 逐题取选择并格式化;只把答了的题拼进去。
|
||||
const answered = clarifications
|
||||
.map((payload, index) => {
|
||||
const key = clarificationKey(msgId, payload.id, index);
|
||||
const selection =
|
||||
overrideByKey?.[key] ??
|
||||
intentClarificationSelections.get(key) ?? { selectedIds: [], custom: "" };
|
||||
const answer = formatIntentClarificationAnswer(payload, selection);
|
||||
return answer ? { question: payload.question, answer } : null;
|
||||
})
|
||||
.filter((x): x is { question: string; answer: string } => x !== null);
|
||||
|
||||
if (answered.length === 0) {
|
||||
triggerToast("请至少回答一个问题");
|
||||
return;
|
||||
}
|
||||
void sendIntentMessage(answer);
|
||||
|
||||
// 单题:直接发答案(与旧行为一致)。多题:带上问题做编号 Q/A,合并成一条消息。
|
||||
const combined =
|
||||
answered.length === 1
|
||||
? answered[0].answer
|
||||
: answered
|
||||
.map((qa, i) => `${i + 1}. ${qa.question}\n答:${qa.answer}`)
|
||||
.join("\n\n");
|
||||
void sendIntentMessage(combined);
|
||||
},
|
||||
[intentClarificationSelections, sendIntentMessage, triggerToast],
|
||||
);
|
||||
@@ -499,7 +581,7 @@ export function useStep1Intent(options: UseStep1IntentOptions): UseStep1IntentRe
|
||||
setIntentClarificationSelections,
|
||||
runStep1Init,
|
||||
sendIntentMessage,
|
||||
submitIntentClarification,
|
||||
submitIntentClarifications,
|
||||
stopIntentStream,
|
||||
hydrateFromDraft,
|
||||
appendLocalMessage,
|
||||
|
||||
Reference in new issue
Block a user