From e9bb7c6bb4e528c4647c2dcaf339a8c9ca44f8ae Mon Sep 17 00:00:00 2001 From: brightzhli <864345220@qq.com> Date: Mon, 19 Oct 2026 11:09:35 +0800 Subject: [PATCH 1/6] feat(chat): support AGUI Interrupts --- .../chat-engine/_example/agui-interrupt.tsx | 362 ++++++++++++++++++ .../chat/chat-engine/hooks/useInterrupt.ts | 193 ++++++++++ .../pro-components/chat/chat-engine/index.ts | 10 + 3 files changed, 565 insertions(+) create mode 100644 packages/pro-components/chat/chat-engine/_example/agui-interrupt.tsx create mode 100644 packages/pro-components/chat/chat-engine/hooks/useInterrupt.ts diff --git a/packages/pro-components/chat/chat-engine/_example/agui-interrupt.tsx b/packages/pro-components/chat/chat-engine/_example/agui-interrupt.tsx new file mode 100644 index 0000000000..d975cf6eb5 --- /dev/null +++ b/packages/pro-components/chat/chat-engine/_example/agui-interrupt.tsx @@ -0,0 +1,362 @@ +import React, { useMemo, useRef, useState } from 'react'; +import { Button, Card, InputNumber, MessagePlugin, Select, Space, Tag } from 'tdesign-react'; +import { ChatList, ChatMessage, ChatSender } from '@tdesign-react/chat'; + +import { useChat, useInterrupt } from '../index'; + +import type { + ChatJSONValue, + ChatRequestParams, + Interrupt, + TdChatMessageConfig, + TdChatSenderParams, +} from '@tdesign-react/chat'; +import type { InterruptResponseStatus } from '../hooks/useInterrupt'; + +// 本地 chat-agent-api 服务地址(默认端口 9000,见 chat-agent-api/config.js) +const MOCK_BASE_URL = 'http://127.0.0.1:9000'; + +// resumeInterrupt 透传的中断恢复扩展参数 +type InterruptRequestParams = ChatRequestParams<{ + resume?: unknown; + threadId?: string; + runId?: string; +}>; + +// ==================== 中断下发结构(示例层约定) ==================== + +/** confirmation 类中断通过 metadata.options 下发的选项 */ +interface InterruptOption { + label: string; + theme?: 'primary' | 'default'; + /** resolved 时原样回传给后端的响应载荷 */ + payload?: ChatJSONValue; + /** 为 true 表示放弃该中断(cancelled,不携带 payload) */ + cancel?: boolean; +} + +/** input_required 类中断携带的 JSON Schema 字段 */ +interface SchemaProp { + type?: 'string' | 'number'; + title?: string; + enum?: string[]; +} +interface ResponseSchema { + type?: string; + properties?: Record; + required?: string[]; +} + +const REASON_LABEL: Record = { + tool_call: '工具调用确认', + input_required: '结构化输入', + confirmation: '确认', +}; + +function getOptions(interrupt: Interrupt): InterruptOption[] { + const options = (interrupt.metadata as { options?: InterruptOption[] } | undefined)?.options; + return Array.isArray(options) ? options : []; +} + +// ==================== 单个中断卡片 ==================== + +interface InterruptCardProps { + interrupt: Interrupt; + status: InterruptResponseStatus; + disabled: boolean; + onResolve: (payload?: ChatJSONValue) => void; + onCancel: () => void; +} + +/** + * 根据中断类型渲染不同交互: + * - 携带 responseSchema → 结构化表单 + * - 携带 metadata.options → 选项按钮 + * - 兜底 → 确认 / 取消 + */ +const InterruptCard: React.FC = ({ interrupt, status, disabled, onResolve, onCancel }) => { + const schema = interrupt.responseSchema as ResponseSchema | undefined; + const options = getOptions(interrupt); + const responded = status !== 'pending'; + const [form, setForm] = useState>({}); + + const setField = (key: string, value: ChatJSONValue) => setForm((prev) => ({ ...prev, [key]: value })); + + const renderField = (key: string, prop: SchemaProp) => { + const fieldDisabled = disabled || responded; + if (prop.enum) { + return ( +