圆桌 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
@@ -160,6 +160,14 @@ export interface StreamIntentRequest {
|
||||
toolName?: string,
|
||||
args?: Record<string, unknown>,
|
||||
) => void;
|
||||
/**
|
||||
* Fires for every `ask_clarification` tool message parsed out of the live
|
||||
* stream (the intent agent may emit several in a single turn). Lets the UI
|
||||
* surface each assistance question in real-time instead of waiting for the
|
||||
* terminal frame — which only carries the *first* question. `index` is the
|
||||
* 0-based arrival order within this turn.
|
||||
*/
|
||||
onClarification?: (clarification: ClarificationPayload, index: number) => void;
|
||||
}
|
||||
|
||||
export interface StreamIntentResult {
|
||||
@@ -172,8 +180,16 @@ export interface StreamIntentResult {
|
||||
/**
|
||||
* Populated when the intent agent called ask_clarification — render option
|
||||
* cards + custom input instead of treating the turn as plain text only.
|
||||
* This is the *first* clarification (kept for backward compat); prefer
|
||||
* `clarifications` which carries the full set.
|
||||
*/
|
||||
clarification: ClarificationPayload | null;
|
||||
/**
|
||||
* All `ask_clarification` questions the agent asked this turn, in arrival
|
||||
* order. Collected from the live `type:"tool"` frames (the terminal frame
|
||||
* only carries the first). Empty unless status === "clarification".
|
||||
*/
|
||||
clarifications: ClarificationPayload[];
|
||||
/**
|
||||
* Populated only when status === "error" and the backend emitted a
|
||||
* structured diagnostic (new in `_diagnose_exception`). UI uses this to show
|
||||
@@ -241,6 +257,54 @@ function extractAiTextDelta(parsed: unknown): { text: string; id: string } | nul
|
||||
return { text, id };
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a live `type:"tool"` `ask_clarification` frame into a ClarificationPayload.
|
||||
*
|
||||
* The intent agent emits one such frame per question (ClarificationMiddleware
|
||||
* formats them), each carrying a fully-formed clarification in
|
||||
* `additional_kwargs.clarification` — options already normalized to
|
||||
* `{id, label}` objects, snake_case keys. Returns null for any other frame
|
||||
* (AI text chunks, tool_call_chunks, value snapshots).
|
||||
*/
|
||||
function extractClarificationToolMessage(
|
||||
parsed: unknown,
|
||||
): { payload: ClarificationPayload; toolCallId: string } | null {
|
||||
if (!Array.isArray(parsed) || parsed.length < 1) return null;
|
||||
const msg = parsed[0];
|
||||
if (!msg || typeof msg !== "object") return null;
|
||||
const obj = msg as Record<string, unknown>;
|
||||
if (String(obj.type ?? "").toLowerCase() !== "tool") return null;
|
||||
if (obj.name !== "ask_clarification") return null;
|
||||
const kwargs = obj.additional_kwargs;
|
||||
if (!kwargs || typeof kwargs !== "object") return null;
|
||||
const clar = (kwargs as Record<string, unknown>).clarification;
|
||||
if (!clar || typeof clar !== "object") return null;
|
||||
const c = clar as Record<string, unknown>;
|
||||
const toolCallId =
|
||||
typeof obj.tool_call_id === "string" && obj.tool_call_id
|
||||
? obj.tool_call_id
|
||||
: typeof obj.id === "string"
|
||||
? obj.id
|
||||
: "";
|
||||
const clarificationType =
|
||||
typeof c.clarification_type === "string" ? c.clarification_type : "";
|
||||
return {
|
||||
toolCallId,
|
||||
payload: {
|
||||
id: toolCallId || undefined,
|
||||
question: typeof c.question === "string" ? c.question : "",
|
||||
clarificationType,
|
||||
context: typeof c.context === "string" ? c.context : "",
|
||||
options: Array.isArray(c.options) ? c.options : [],
|
||||
allowCustom: c.allow_custom !== false,
|
||||
allowMultiple: resolveAllowMultiple(
|
||||
typeof c.allow_multiple === "boolean" ? c.allow_multiple : undefined,
|
||||
clarificationType,
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Open an SSE connection to /api/intent/stream and resolve when the upstream
|
||||
* stream completes. Each call is one user turn; reuse the same threadId
|
||||
@@ -288,9 +352,24 @@ export async function streamIntent(req: StreamIntentRequest): Promise<StreamInte
|
||||
content: "",
|
||||
summary: null,
|
||||
clarification: null,
|
||||
clarifications: [],
|
||||
errorDetail: null,
|
||||
};
|
||||
|
||||
// Clarifications collected live from `type:"tool"` frames, deduped by
|
||||
// tool_call_id. The terminal frame only carries the first question, so this
|
||||
// is the source of truth for multi-question turns.
|
||||
const seenClarificationIds = new Set<string>();
|
||||
const collectClarification = (payload: ClarificationPayload, toolCallId: string) => {
|
||||
const dedupeKey = toolCallId || `idx-${result.clarifications.length}`;
|
||||
if (seenClarificationIds.has(dedupeKey)) return;
|
||||
seenClarificationIds.add(dedupeKey);
|
||||
const index = result.clarifications.length;
|
||||
result.clarifications.push(payload);
|
||||
if (!result.clarification) result.clarification = payload;
|
||||
req.onClarification?.(payload, index);
|
||||
};
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
@@ -331,10 +410,15 @@ export async function streamIntent(req: StreamIntentRequest): Promise<StreamInte
|
||||
if (parsed.status === "error") {
|
||||
result.errorDetail = parseIntentErrorDetail(parsed.error_detail);
|
||||
}
|
||||
if (parsed.status === "clarification") {
|
||||
if (parsed.status === "clarification" && result.clarifications.length === 0) {
|
||||
// Fallback path: no live `type:"tool"` clarification frames were seen
|
||||
// (older backend, or single-question turn) — synthesize one from the
|
||||
// terminal frame so the UI still gets a card. When tool frames *were*
|
||||
// seen, `clarifications` already holds the full (possibly multi-question)
|
||||
// set and we keep it.
|
||||
const clarificationType =
|
||||
typeof parsed.clarification_type === "string" ? parsed.clarification_type : "";
|
||||
result.clarification = {
|
||||
const fallback: ClarificationPayload = {
|
||||
question: typeof parsed.question === "string" ? parsed.question : "",
|
||||
clarificationType,
|
||||
context:
|
||||
@@ -346,11 +430,22 @@ export async function streamIntent(req: StreamIntentRequest): Promise<StreamInte
|
||||
clarificationType,
|
||||
),
|
||||
};
|
||||
result.clarification = fallback;
|
||||
result.clarifications = [fallback];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lastEvent === "messages") {
|
||||
// `ask_clarification` tool result frame → collect + surface in real-time.
|
||||
// These carry the fully-formed clarification (one per question); the
|
||||
// agent may emit several in a single turn.
|
||||
const toolClar = extractClarificationToolMessage(parsed);
|
||||
if (toolClar) {
|
||||
collectClarification(toolClar.payload, toolClar.toolCallId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Phase inference from LangGraph messages-tuple frames. Tool calls
|
||||
// (typically ask_clarification) flip phase to tool_calling; AI text
|
||||
// chunks flip to streaming. Everything else leaves phase alone.
|
||||
|
||||
Reference in new issue
Block a user