feat: interrupt running agent via card button with confirm dialog

Add a  中断 button to live streaming cards; Feishu's native confirm
dialog is the confirmation step, so no extra card round-trip is needed.

- builder.ts: render interrupt action (danger button + confirm) while the
  run is live; new `interrupted` flag renders a 已中断 footer instead of
  完成/失败 on the complete card.
- streaming-card.ts: finish(text, { interrupted }) threads the flag into
  the final patch; overflow cards suppress the button.
- runner.ts: RunRequest gains abortController, passed to the SDK query;
  abort surfaces as RunStatus "interrupted" (no error) in the catch.
- trigger.ts: activeRuns registry maps runId → AbortController; onCardAction
  resolves the interrupt button, authorizes agent.cancel, aborts the run.
  Interrupted runs persist as CANCELED (spec RunState.canceled, terminal →
  lock released per ADR-0002).
- authorizer.ts: agent.cancel now consults PermissionSettings.agentCancel
  (MANAGE_ONLY/DISABLED) — previously only agentTrigger was honored, but
  spec/PermissionGrant deliberately splits these knobs ("谁能触发" ≠
  "谁能取消"). Default role remains MANAGE (Capability.normalCancel).

Tests: runner abort unit test, trigger interrupt + denied integration
tests, three agent.cancel authorization tests (manage allowed, edit denied,
DISABLED policy denies even manage). 26 files / 175 tests pass.
This commit is contained in:
2026-07-09 18:59:31 +08:00
parent d6724e5a83
commit d01ea0e1cb
8 changed files with 383 additions and 34 deletions
+34 -2
View File
@@ -59,9 +59,10 @@ export function buildAgentCard(params: {
toolUseSteps: ToolUseTraceStep[];
toolUseElapsedMs: number | undefined;
isError: boolean | undefined;
interrupted: boolean | undefined;
runId: string | undefined;
}): Record<string, unknown> {
const { phase, text, reasoningText, toolUseSteps, toolUseElapsedMs, isError } = params;
const { phase, text, reasoningText, toolUseSteps, toolUseElapsedMs, isError, interrupted } = params;
const elements: unknown[] = [];
// Tool-use panel (always present if there are steps)
@@ -100,10 +101,19 @@ export function buildAgentCard(params: {
});
}
// Interrupt button while the run is live. The button carries the run id in
// its action value; the card action handler aborts the run. Feishu's native
// `confirm` dialog is the "are you sure" step — the action only fires after
// the user confirms, so no second round-trip is needed.
if (phase !== "complete" && params.runId !== undefined) {
elements.push(buildInterruptAction(params.runId));
}
// Footer for complete state
if (phase === "complete") {
const footerParts: string[] = [];
if (isError === true) footerParts.push("\u274C \u5931\u8D25");
if (interrupted === true) footerParts.push("\u{1F6D1} \u5DF2\u4E2D\u65AD");
else if (isError === true) footerParts.push("\u274C \u5931\u8D25");
else footerParts.push("\u2705 \u5B8C\u6210");
if (toolUseElapsedMs !== undefined) {
footerParts.push(formatElapsed(toolUseElapsedMs));
@@ -125,6 +135,28 @@ export function buildAgentCard(params: {
};
}
/** Action row with a single danger "interrupt" button for a live run. */
function buildInterruptAction(runId: string): unknown {
return {
tag: "action",
actions: [
{
tag: "button",
text: { tag: "plain_text", content: "\u{1F6D1} \u4E2D\u65AD" },
type: "danger",
value: { interrupt_run: runId },
confirm: {
title: { tag: "plain_text", content: "\u786E\u8BA4\u4E2D\u65AD" },
text: {
tag: "plain_text",
content: "\u786E\u5B9A\u8981\u4E2D\u65AD\u5F53\u524D\u8FD0\u884C\u5417\uFF1F\u4E2D\u65AD\u540E\u65E0\u6CD5\u6062\u590D\uFF0C\u5DF2\u4EA7\u51FA\u7684\u5185\u5BB9\u4F1A\u4FDD\u7559\u3002",
},
},
},
],
};
}
// ---------------------------------------------------------------------------
// Tool-use panel
// ---------------------------------------------------------------------------
+8 -4
View File
@@ -56,6 +56,7 @@ export class StreamingAgentCard {
private flushChain: Promise<void> = Promise.resolve();
private flushScheduled = false;
private lastPatchAt = 0;
private interrupted = false;
private readonly runId: string;
private readonly rt: FeishuRuntime;
@@ -102,9 +103,9 @@ export class StreamingAgentCard {
recordToolUseEnd({ runId: this.runId, ...params });
this.scheduleFlush();
}
async finish(fallbackText: string): Promise<void> {
async finish(fallbackText: string, options: { readonly interrupted?: boolean } = {}): Promise<void> {
await this.flushChain;
this.interrupted = options.interrupted === true;
if (this.text.length > 0) {
await this.flushCard("complete", this.text);
return;
@@ -164,6 +165,7 @@ export class StreamingAgentCard {
toolUseSteps,
toolUseElapsedMs: this.toolUseElapsedMs,
isError,
interrupted: this.interrupted,
runId: this.runId,
});
@@ -178,7 +180,8 @@ export class StreamingAgentCard {
toolUseSteps: [],
toolUseElapsedMs: undefined,
isError,
runId: this.runId,
interrupted: this.interrupted,
runId: undefined,
});
this.currentMessageId = await sendCard(this.rt, this.chatId, overflowCard);
}
@@ -193,7 +196,8 @@ export class StreamingAgentCard {
toolUseSteps: [],
toolUseElapsedMs: undefined,
isError,
runId: this.runId,
interrupted: this.interrupted,
runId: undefined,
});
this.currentMessageId = await sendCard(this.rt, this.chatId, overflowCard);
}