diff --git a/packages/common b/packages/common index 33455513e2..d3cfc0ea5e 160000 --- a/packages/common +++ b/packages/common @@ -1 +1 @@ -Subproject commit 33455513e2df4a8066ce49ca1bec8ff3655f79a9 +Subproject commit d3cfc0ea5e375ac235333c203e9fda604e357548 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..0ce6f705b8 --- /dev/null +++ b/packages/pro-components/chat/chat-engine/_example/agui-interrupt.tsx @@ -0,0 +1,359 @@ +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'; + +// 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 ( +