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

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,181 +0,0 @@
1
- /**
2
- * A2UISurface - A2UI 声明式 UI 渲染组件
3
- *
4
- * 基于 Google A2UI 协议,将 Agent 生成的声明式 JSON 消息
5
- * 渲染为原生 React 组件。支持流式增量更新和自定义组件注册。
6
- *
7
- */
8
-
9
- import React, { useEffect, useRef, useCallback } from 'react';
10
- import styled from 'styled-components';
11
- import {
12
- A2UIProvider,
13
- A2UIRenderer,
14
- A2UIViewer,
15
- useA2UI,
16
- initializeDefaultCatalog,
17
- ComponentRegistry,
18
- } from '@a2ui/react';
19
- import type {
20
- ServerToClientMessage,
21
- OnActionCallback,
22
- A2UIViewerProps,
23
- } from '@a2ui/react';
24
-
25
- // 初始化默认组件目录(全局只需执行一次)
26
- initializeDefaultCatalog();
27
-
28
- // ==================== Types ====================
29
-
30
- /** A2UI 协议消息(v0.8 格式,透传给 @a2ui/react 处理) */
31
- export type A2UIMessage = ServerToClientMessage;
32
-
33
- export interface A2UISurfaceProps {
34
- /** A2UI JSON 消息数组(来自 Agent 的响应) */
35
- messages: A2UIMessage[];
36
- /** 渲染的 Surface ID(默认 'main') */
37
- surfaceId?: string;
38
- /** 用户在 A2UI 组件上的交互回调 */
39
- onAction?: OnActionCallback;
40
- /** 自定义组件注册表(可选,默认使用内置组件) */
41
- registry?: ComponentRegistry;
42
- /** 自定义类名 */
43
- className?: string;
44
- /** 自定义样式 */
45
- style?: React.CSSProperties;
46
- }
47
-
48
- /** A2UIViewer 的 Props,用于静态 JSON 渲染场景 */
49
- export type A2UIStaticViewerProps = A2UIViewerProps;
50
-
51
- // ==================== Styled Components ====================
52
-
53
- const A2UIContainer = styled.div`
54
- margin-top: 12px;
55
- width: 100%;
56
- max-width: 100%;
57
- `;
58
-
59
- // ==================== Internal Component ====================
60
-
61
- /**
62
- * 内部组件:负责消息处理和 Surface 渲染
63
- * 必须在 A2UIProvider 内部使用,以获取 useA2UI hook 的上下文
64
- */
65
- const A2UISurfaceInner: React.FC<{
66
- messages: A2UIMessage[];
67
- surfaceId: string;
68
- registry?: ComponentRegistry;
69
- className?: string;
70
- style?: React.CSSProperties;
71
- }> = ({ messages, surfaceId, registry, className, style }) => {
72
- const { processMessages, getSurface } = useA2UI();
73
- const processedCountRef = useRef(0);
74
-
75
- // 增量处理新到达的消息(支持流式场景)
76
- useEffect(() => {
77
- if (messages.length > processedCountRef.current) {
78
- const newMessages = messages.slice(processedCountRef.current);
79
- processMessages(newMessages);
80
- processedCountRef.current = messages.length;
81
- }
82
- }, [messages, processMessages]);
83
-
84
- // 当消息被清空时重置计数器
85
- useEffect(() => {
86
- if (messages.length === 0) {
87
- processedCountRef.current = 0;
88
- }
89
- }, [messages.length]);
90
-
91
- const surface = getSurface(surfaceId);
92
-
93
- if (!surface) {
94
- return null;
95
- }
96
-
97
- return (
98
- <A2UIContainer className={className} style={style}>
99
- <A2UIRenderer
100
- surfaceId={surfaceId}
101
- registry={registry}
102
- />
103
- </A2UIContainer>
104
- );
105
- };
106
-
107
- // ==================== Exported Components ====================
108
-
109
- /**
110
- * A2UISurface - A2UI 声明式 UI 渲染组件(流式消息场景)
111
- *
112
- * 接收 Agent 发送的 A2UI 协议消息数组,增量处理并渲染对应的 Surface。
113
- * 内部封装了 A2UIProvider,可直接作为 BubbleContent 的 children 使用。
114
- *
115
- * @example
116
- * ```tsx
117
- * // 作为 BubbleContent 的 children 使用
118
- * <BubbleContent content={content} status={status}>
119
- * <A2UISurface
120
- * messages={a2uiMessages}
121
- * surfaceId="main"
122
- * onAction={(msg) => console.log('用户操作:', msg)}
123
- * />
124
- * </BubbleContent>
125
- * ```
126
- */
127
- export const A2UISurface: React.FC<A2UISurfaceProps> = ({
128
- messages,
129
- surfaceId = 'main',
130
- onAction,
131
- registry,
132
- className,
133
- style,
134
- }) => {
135
- const handleAction: OnActionCallback = useCallback((message) => {
136
- onAction?.(message);
137
- }, [onAction]);
138
-
139
- return (
140
- <A2UIProvider onAction={handleAction}>
141
- <A2UISurfaceInner
142
- messages={messages}
143
- surfaceId={surfaceId}
144
- registry={registry}
145
- className={className}
146
- style={style}
147
- />
148
- </A2UIProvider>
149
- );
150
- };
151
-
152
- /**
153
- * A2UIStaticViewer - A2UI 静态 JSON 渲染组件
154
- *
155
- * 用于直接从静态的组件定义和数据渲染 UI,无需流式消息。
156
- * 适用于已有完整 A2UI 组件树的场景。
157
- *
158
- * @example
159
- * ```tsx
160
- * const components = [
161
- * { id: 'root', component: { Card: { child: 'text' } } },
162
- * { id: 'text', component: { Text: { text: { path: '/message' } } } },
163
- * ];
164
- *
165
- * <A2UIStaticViewer
166
- * root="root"
167
- * components={components}
168
- * data={{ message: 'Hello World!' }}
169
- * onAction={(action) => console.log('Action:', action)}
170
- * />
171
- * ```
172
- */
173
- export const A2UIStaticViewer: React.FC<A2UIStaticViewerProps> = (props) => {
174
- return (
175
- <A2UIContainer>
176
- <A2UIViewer {...props} />
177
- </A2UIContainer>
178
- );
179
- };
180
-
181
- export default A2UISurface;
@@ -1 +0,0 @@
1
- export { A2UISurface, A2UIStaticViewer, type A2UISurfaceProps, type A2UIStaticViewerProps, type A2UIMessage } from './A2UIRenderer';