@alicloud/appflow-chat 0.0.4-alpha.1 → 0.0.4-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,73 +1,5 @@
1
- import { A2UIViewerProps } from '@a2ui/react';
2
- import { ComponentRegistry } from '@a2ui/react';
3
1
  import { default as default_2 } from 'react';
4
- import { OnActionCallback } from '@a2ui/react';
5
2
  import { Provider } from 'react';
6
- import { ServerToClientMessage } from '@a2ui/react';
7
-
8
- /** A2UI 协议消息(v0.8 格式,透传给 @a2ui/react 处理) */
9
- export declare type A2UIMessage = ServerToClientMessage;
10
-
11
- /**
12
- * A2UIStaticViewer - A2UI 静态 JSON 渲染组件
13
- *
14
- * 用于直接从静态的组件定义和数据渲染 UI,无需流式消息。
15
- * 适用于已有完整 A2UI 组件树的场景。
16
- *
17
- * @example
18
- * ```tsx
19
- * const components = [
20
- * { id: 'root', component: { Card: { child: 'text' } } },
21
- * { id: 'text', component: { Text: { text: { path: '/message' } } } },
22
- * ];
23
- *
24
- * <A2UIStaticViewer
25
- * root="root"
26
- * components={components}
27
- * data={{ message: 'Hello World!' }}
28
- * onAction={(action) => console.log('Action:', action)}
29
- * />
30
- * ```
31
- */
32
- export declare const A2UIStaticViewer: default_2.FC<A2UIStaticViewerProps>;
33
-
34
- /** A2UIViewer 的 Props,用于静态 JSON 渲染场景 */
35
- export declare type A2UIStaticViewerProps = A2UIViewerProps;
36
-
37
- /**
38
- * A2UISurface - A2UI 声明式 UI 渲染组件(流式消息场景)
39
- *
40
- * 接收 Agent 发送的 A2UI 协议消息数组,增量处理并渲染对应的 Surface。
41
- * 内部封装了 A2UIProvider,可直接作为 BubbleContent 的 children 使用。
42
- *
43
- * @example
44
- * ```tsx
45
- * // 作为 BubbleContent 的 children 使用
46
- * <BubbleContent content={content} status={status}>
47
- * <A2UISurface
48
- * messages={a2uiMessages}
49
- * surfaceId="main"
50
- * onAction={(msg) => console.log('用户操作:', msg)}
51
- * />
52
- * </BubbleContent>
53
- * ```
54
- */
55
- export declare const A2UISurface: default_2.FC<A2UISurfaceProps>;
56
-
57
- export declare interface A2UISurfaceProps {
58
- /** A2UI JSON 消息数组(来自 Agent 的响应) */
59
- messages: A2UIMessage[];
60
- /** 渲染的 Surface ID(默认 'main') */
61
- surfaceId?: string;
62
- /** 用户在 A2UI 组件上的交互回调 */
63
- onAction?: OnActionCallback;
64
- /** 自定义组件注册表(可选,默认使用内置组件) */
65
- registry?: ComponentRegistry;
66
- /** 自定义类名 */
67
- className?: string;
68
- /** 自定义样式 */
69
- style?: default_2.CSSProperties;
70
- }
71
3
 
72
4
  /**
73
5
  * AssociationPropertyMetadata 类型定义
@@ -119,6 +51,26 @@ export declare interface BubbleContentProps {
119
51
  waitingMessage?: string;
120
52
  }
121
53
 
54
+ /** 附件信息(上传完成后携带下载URL) */
55
+ export declare interface ChatAttachment {
56
+ /** 文件唯一标识 */
57
+ uid: string;
58
+ /** 文件名 */
59
+ name: string;
60
+ /** 上传状态 */
61
+ status: 'uploading' | 'done' | 'error';
62
+ /** 文件类型:image 或 file */
63
+ type: 'image' | 'file';
64
+ /** 上传后的下载URL */
65
+ url?: string;
66
+ /** 本地预览URL(图片) */
67
+ thumbUrl?: string;
68
+ /** 原始文件对象 */
69
+ originFile?: File;
70
+ /** 文件ID(仅文件类型有) */
71
+ fileId?: string;
72
+ }
73
+
122
74
  export declare interface ChatConfig {
123
75
  welcome: string;
124
76
  questions: string[];
@@ -136,12 +88,96 @@ export declare interface ChatConfig {
136
88
  export declare interface ChatMessage {
137
89
  text?: string;
138
90
  images?: string[];
139
- files?: string[];
91
+ files?: Array<{
92
+ url: string;
93
+ name: string;
94
+ fileId?: string;
95
+ }>;
140
96
  audio?: string;
141
97
  modelId?: string;
142
98
  webSearch?: boolean;
143
99
  }
144
100
 
101
+ /**
102
+ * ChatSender - 聊天输入框组件
103
+ *
104
+ * 集成了文本输入、文件/图片上传、语音输入、联网搜索、模型选择等功能。
105
+ * 根据模型能力自动控制功能按钮的显隐。
106
+ *
107
+ * @example
108
+ * ```tsx
109
+ * <ChatSender
110
+ * loading={isLoading}
111
+ * models={config.models}
112
+ * capabilities={chatService.getModelCapabilities(modelId)}
113
+ * onSubmit={({ text, images, files, modelId, webSearch }) => {
114
+ * chatService.chat({ text, images, files, modelId, webSearch });
115
+ * }}
116
+ * onCancel={() => chatService.cancel()}
117
+ * onClear={() => chatService.clear()}
118
+ * onUpload={(file) => chatService.upload(file)}
119
+ * />
120
+ * ```
121
+ */
122
+ export declare const ChatSender: default_2.FC<ChatSenderProps>;
123
+
124
+ export declare interface ChatSenderProps {
125
+ /** 是否处于加载状态(AI正在回复) */
126
+ loading?: boolean;
127
+ /** 是否禁用 */
128
+ disabled?: boolean;
129
+ /** 输入框占位文本 */
130
+ placeholder?: string;
131
+ /** 提交方式:'enter' 回车发送 | 'shiftEnter' Shift+回车发送 */
132
+ submitType?: 'enter' | 'shiftEnter';
133
+ /** 可用模型列表,传入且长度>1时显示模型选择下拉框 */
134
+ models?: ModelInfo[];
135
+ /** 当前选中的模型ID */
136
+ modelId?: string;
137
+ /** 默认选中的模型ID(非受控) */
138
+ defaultModelId?: string;
139
+ /** 模型切换回调 */
140
+ onModelChange?: (modelId: string) => void;
141
+ /**
142
+ * 模型能力配置,控制功能按钮的显隐
143
+ * 不传时默认所有功能关闭
144
+ */
145
+ capabilities?: ModelCapabilities;
146
+ /** 提交消息回调 */
147
+ onSubmit?: (data: ChatSenderSubmitData) => void;
148
+ /** 取消当前请求 */
149
+ onCancel?: () => void;
150
+ /** 文件上传方法,返回下载URL和可选的fileId */
151
+ onUpload?: (file: File) => Promise<{
152
+ downloadUrl: string;
153
+ fileId?: string;
154
+ }>;
155
+ /** 自定义类名 */
156
+ className?: string;
157
+ /** 自定义样式 */
158
+ style?: default_2.CSSProperties;
159
+ }
160
+
161
+ /** 提交时的消息数据 */
162
+ export declare interface ChatSenderSubmitData {
163
+ /** 文本内容 */
164
+ text: string;
165
+ /** 图片URL列表 */
166
+ images: string[];
167
+ /** 文件列表(包含文件名、URL和可选的fileId) */
168
+ files: {
169
+ name: string;
170
+ url: string;
171
+ fileId?: string;
172
+ }[];
173
+ /** 语音文件URL(录音上传后的下载地址) */
174
+ audio?: string;
175
+ /** 选中的模型ID */
176
+ modelId?: string;
177
+ /** 是否启用联网搜索 */
178
+ webSearch: boolean;
179
+ }
180
+
145
181
  export declare class ChatService {
146
182
  private config;
147
183
  private setupConfig;
@@ -172,10 +208,20 @@ export declare class ChatService {
172
208
  * 发送消息的内部实现
173
209
  */
174
210
  private sendMessage;
211
+ /**
212
+ * 发送上传事件(通用方法)
213
+ * 封装向服务端发送上传相关事件的SSE请求逻辑,支持 uploadToken 和 uploadFile 两种事件类型
214
+ * @param eventType 事件类型:'uploadToken' 获取预签名URL | 'uploadFile' 获取文件ID
215
+ * @param fileName 文件名
216
+ * @param extraData 额外数据(如 uploadFile 时需要传 content: downloadUrl)
217
+ */
218
+ private sendUploadEvent;
175
219
  /**
176
220
  * 上传文件
221
+ * 非图片文件会额外获取 fileId(用于服务端文件关联)
222
+ * @returns 上传结果,包含 downloadUrl 和可选的 fileId
177
223
  */
178
- upload(file: File): Promise<string>;
224
+ upload(file: File): Promise<UploadResult>;
179
225
  /**
180
226
  * 清除会话
181
227
  */
@@ -465,6 +511,7 @@ export declare interface HistoryMessage {
465
511
  name: string;
466
512
  url: string;
467
513
  }[];
514
+ audio?: string;
468
515
  }
469
516
 
470
517
  /**
@@ -590,6 +637,15 @@ export declare interface MessageBubbleProps {
590
637
  status?: 'Running' | 'Success' | 'Error';
591
638
  /** 参考资料列表 */
592
639
  references?: DocReferenceItem[];
640
+ /** 图片URL列表(用户消息中上传的图片) */
641
+ images?: string[];
642
+ /** 文件列表(用户消息中上传的文件) */
643
+ files?: {
644
+ name: string;
645
+ url: string;
646
+ }[];
647
+ /** 语音消息URL */
648
+ audio?: string;
593
649
  /** 自定义类名 */
594
650
  className?: string;
595
651
  /** 自定义样式 */
@@ -598,18 +654,14 @@ export declare interface MessageBubbleProps {
598
654
  onReferenceClick?: (item: DocReferenceItem) => void;
599
655
  /** 点击网页搜索结果回调(不传则使用默认实现) */
600
656
  onWebSearchClick?: (items: DocReferenceItem[]) => void;
601
- /** 事件类型(用于特殊消息如 humanVerify、historyCard、a2ui) */
602
- eventType?: 'humanVerify' | 'historyCard' | 'a2ui';
657
+ /** 事件类型(用于特殊消息如 humanVerify、historyCard) */
658
+ eventType?: 'humanVerify' | 'historyCard';
603
659
  /** HumanVerify 相关数据 */
604
660
  humanVerifyData?: HumanVerifyData;
605
661
  /** HistoryCard 相关数据(历史对话中的审核卡片) */
606
662
  historyCardData?: HistoryCardData;
607
663
  /** HumanVerify 提交回调 */
608
664
  onHumanVerifySubmit?: (data: HumanVerifySubmitData) => void;
609
- /** A2UI 消息数组(Agent 生成的声明式 UI 描述) */
610
- a2uiMessages?: A2UIMessage[];
611
- /** A2UI 用户交互回调 */
612
- onA2UIAction?: (action: any) => void;
613
665
  }
614
666
 
615
667
  export declare interface ModelCapabilities {
@@ -630,6 +682,7 @@ export declare interface ModelInfo {
630
682
  config?: {
631
683
  image?: boolean;
632
684
  file?: boolean;
685
+ audio?: boolean;
633
686
  webSearch?: boolean;
634
687
  fileConfig?: string;
635
688
  };
@@ -838,6 +891,14 @@ declare interface UploadRequestParams {
838
891
  content?: string;
839
892
  }
840
893
 
894
+ /** 上传结果 */
895
+ export declare interface UploadResult {
896
+ /** 文件下载URL */
897
+ downloadUrl: string;
898
+ /** 文件ID(仅文件类型有,图片无) */
899
+ fileId?: string;
900
+ }
901
+
841
902
  /**
842
903
  * 上传发送方法类型
843
904
  * 用于发送上传相关的事件请求
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alicloud/appflow-chat",
3
- "version": "0.0.4-alpha.1",
3
+ "version": "0.0.4-alpha.10",
4
4
  "description": "Appflow-Chat AI聊天机器人组件库,提供聊天服务和UI组件",
5
5
  "type": "module",
6
6
  "main": "./dist/appflow-chat.cjs.js",
@@ -58,11 +58,10 @@
58
58
  "vite-plugin-dts": "^4.3.0"
59
59
  },
60
60
  "peerDependencies": {
61
- "@a2ui/react": "^0.8.0",
62
- "@a2ui/web_core": "^0.8.0",
63
61
  "antd": "^5.0.0",
64
62
  "react": "^18.0.0",
65
- "react-dom": "^18.0.0"
63
+ "react-dom": "^18.0.0",
64
+ "@ant-design/x": "1.6.1"
66
65
  },
67
66
  "files": [
68
67
  "dist",
package/src/App.tsx ADDED
@@ -0,0 +1,193 @@
1
+ import { useState } from 'react';
2
+ import { ConfigProvider } from 'antd';
3
+ import { ChatSender } from './components/ChatSender';
4
+ import { MessageBubble } from './components/MessageBubble';
5
+ import type { ChatSenderSubmitData } from './components/ChatSender';
6
+ import type { ModelInfo, ModelCapabilities } from './services/ChatService';
7
+
8
+ // 模拟模型列表
9
+ const mockModels: ModelInfo[] = [
10
+ { id: 'model-1', name: '通义千问-VL', config: { image: true, file: true, webSearch: true , audio: true } },
11
+ { id: 'model-2', name: '通义千问-Max', config: { image: false, file: false, webSearch: true } },
12
+ { id: 'model-3', name: 'DeepSeek-R1', config: { image: false, file: false, webSearch: false } },
13
+ ];
14
+
15
+ // 模拟上传
16
+ const mockUpload = async (file: File): Promise<{ downloadUrl: string; fileId?: string }> => {
17
+ return new Promise((resolve) => {
18
+ setTimeout(() => {
19
+ // 音频文件返回 blob URL,以便本地测试播放
20
+ if (file.type.startsWith('audio/')) {
21
+ resolve({ downloadUrl: URL.createObjectURL(file) });
22
+ } else if (file.type.startsWith('image/')) {
23
+ resolve({ downloadUrl: `https://example.com/files/${file.name}` });
24
+ } else {
25
+ // 非图片文件模拟返回 fileId
26
+ resolve({ downloadUrl: `https://example.com/files/${file.name}`, fileId: `mock_fid_${Date.now()}` });
27
+ }
28
+ }, 1500);
29
+ });
30
+ };
31
+
32
+ function App() {
33
+ const [loading, setLoading] = useState(false);
34
+ const [selectedModelId, setSelectedModelId] = useState(mockModels[0].id);
35
+ const [logs, setLogs] = useState<string[]>([]);
36
+
37
+ // 根据选中模型计算能力
38
+ const currentModel = mockModels.find(m => m.id === selectedModelId) || mockModels[0];
39
+ const capabilities: ModelCapabilities = {
40
+ image: currentModel.config?.image ?? false,
41
+ file: currentModel.config?.file ?? false,
42
+ audio: currentModel.config?.audio ?? false,
43
+ webSearch: currentModel.config?.webSearch ?? false,
44
+ };
45
+
46
+ const addLog = (message: string) => {
47
+ setLogs(prev => [`[${new Date().toLocaleTimeString()}] ${message}`, ...prev].slice(0, 50));
48
+ };
49
+
50
+ // 消息列表
51
+ interface Message {
52
+ id: string;
53
+ role: 'user' | 'bot';
54
+ content: string;
55
+ status: 'Running' | 'Success' | 'Error';
56
+ images?: string[];
57
+ files?: { name: string; url: string }[];
58
+ audio?: string;
59
+ }
60
+
61
+ const [messages, setMessages] = useState<Message[]>([]);
62
+
63
+ const handleSubmit = (data: ChatSenderSubmitData) => {
64
+ addLog(`发送消息: text="${data.text}", model=${data.modelId}, images=${data.images.length}, files=${data.files.length}, audio=${data.audio || 'none'}, webSearch=${data.webSearch}`);
65
+
66
+ // 构建用户消息(包含图片、文件和语音)
67
+ const userMsg: Message = {
68
+ id: `msg-${Date.now()}`,
69
+ role: 'user',
70
+ content: data.audio ? '' : data.text,
71
+ status: 'Success',
72
+ images: data.images.length > 0 ? data.images : undefined,
73
+ files: data.files.length > 0 ? data.files : undefined,
74
+ audio: data.audio || undefined,
75
+ };
76
+
77
+ // 构建 bot 占位消息
78
+ const botMsg: Message = {
79
+ id: `msg-${Date.now() + 1}`,
80
+ role: 'bot',
81
+ content: '',
82
+ status: 'Running',
83
+ };
84
+
85
+ setMessages(prev => [...prev, userMsg, botMsg]);
86
+ setLoading(true);
87
+
88
+ // 模拟 AI 回复
89
+ setTimeout(() => {
90
+ setMessages(prev => prev.map(m =>
91
+ m.id === botMsg.id
92
+ ? { ...m, content: '收到你的消息!这是一条模拟的 AI 回复。', status: 'Success' as const }
93
+ : m
94
+ ));
95
+ setLoading(false);
96
+ addLog('AI 回复完成');
97
+ }, 2000);
98
+ };
99
+
100
+ const handleCancel = () => {
101
+ setLoading(false);
102
+ addLog('取消请求');
103
+ };
104
+
105
+ // 切换能力的控制面板
106
+ const [showUpload, setShowUpload] = useState(true);
107
+ const [showAudio, setShowAudio] = useState(false);
108
+
109
+ const adjustedCapabilities: ModelCapabilities = {
110
+ ...capabilities,
111
+ image: showUpload && capabilities.image,
112
+ file: showUpload && capabilities.file,
113
+ audio: showAudio,
114
+ };
115
+
116
+ return (
117
+ <ConfigProvider>
118
+ <div style={{ maxWidth: 800, margin: '40px auto', padding: '0 20px', fontFamily: '-apple-system, BlinkMacSystemFont, sans-serif' }}>
119
+ <h2 style={{ marginBottom: 24, color: '#333' }}>ChatSender 组件预览</h2>
120
+
121
+ {/* 控制面板 */}
122
+ <div style={{ marginBottom: 24, padding: 16, background: '#f5f5f5', borderRadius: 8 }}>
123
+ <h4 style={{ margin: '0 0 12px 0', color: '#666' }}>功能开关(模拟 capabilities 控制)</h4>
124
+ <div style={{ display: 'flex', gap: 16, flexWrap: 'wrap' }}>
125
+ <label style={{ display: 'flex', alignItems: 'center', gap: 4, cursor: 'pointer' }}>
126
+ <input type="checkbox" checked={showUpload} onChange={e => setShowUpload(e.target.checked)} />
127
+ 文件/图片上传
128
+ </label>
129
+ <label style={{ display: 'flex', alignItems: 'center', gap: 4, cursor: 'pointer' }}>
130
+ <input type="checkbox" checked={showAudio} onChange={e => setShowAudio(e.target.checked)} />
131
+ 语音输入
132
+ </label>
133
+ <span style={{ color: '#999', fontSize: 13 }}>
134
+ 当前模型: <strong>{currentModel.name}</strong>
135
+ {capabilities.image && ' | 支持图片'}
136
+ {capabilities.file && ' | 支持文件'}
137
+ </span>
138
+ </div>
139
+ </div>
140
+
141
+ {/* 消息列表 */}
142
+ <div style={{ marginBottom: 24, padding: 16, background: '#fff', borderRadius: 8, minHeight: 200, maxHeight: 500, overflow: 'auto', border: '1px solid #f0f0f0' }}>
143
+ {messages.length === 0 && (
144
+ <div style={{ color: '#999', textAlign: 'center', padding: 40 }}>
145
+ 发送消息后,消息气泡将在此展示(支持图片和文件)
146
+ </div>
147
+ )}
148
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
149
+ {messages.map(msg => (
150
+ <MessageBubble
151
+ key={msg.id}
152
+ content={msg.content}
153
+ role={msg.role}
154
+ status={msg.status}
155
+ images={msg.images}
156
+ files={msg.files}
157
+ audio={msg.audio}
158
+ />
159
+ ))}
160
+ </div>
161
+ </div>
162
+
163
+ {/* ChatSender 组件 */}
164
+ <div style={{ marginBottom: 24 }}>
165
+ <ChatSender
166
+ loading={loading}
167
+ models={mockModels}
168
+ modelId={selectedModelId}
169
+ onModelChange={setSelectedModelId}
170
+ capabilities={adjustedCapabilities}
171
+ placeholder="输入消息,按 Enter 发送..."
172
+ onSubmit={handleSubmit}
173
+ onCancel={handleCancel}
174
+ onUpload={mockUpload}
175
+ />
176
+ </div>
177
+
178
+ {/* 日志区域 */}
179
+ <div style={{ padding: 16, background: '#1e1e1e', borderRadius: 8, maxHeight: 300, overflow: 'auto' }}>
180
+ <h4 style={{ margin: '0 0 8px 0', color: '#888', fontSize: 13 }}>事件日志</h4>
181
+ {logs.length === 0 && <div style={{ color: '#666', fontSize: 13 }}>暂无日志,尝试发送消息或上传文件...</div>}
182
+ {logs.map((log, index) => (
183
+ <div key={index} style={{ color: '#4ec9b0', fontSize: 13, lineHeight: 1.6, fontFamily: 'monospace' }}>
184
+ {log}
185
+ </div>
186
+ ))}
187
+ </div>
188
+ </div>
189
+ </ConfigProvider>
190
+ );
191
+ }
192
+
193
+ export default App;