@wevu/a2ui 0.0.0-alpha.0
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.
- package/LICENSE +21 -0
- package/README.md +168 -0
- package/dist/catalog.d.mts +15 -0
- package/dist/catalog.mjs +2 -0
- package/dist/catalog.mjs.map +1 -0
- package/dist/compat.d.mts +8 -0
- package/dist/compat.mjs +2 -0
- package/dist/compat.mjs.map +1 -0
- package/dist/envelope.d.mts +29 -0
- package/dist/envelope.mjs +2 -0
- package/dist/envelope.mjs.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +1 -0
- package/dist/projection.mjs +2 -0
- package/dist/projection.mjs.map +1 -0
- package/dist/session.d.mts +8 -0
- package/dist/session.mjs +2 -0
- package/dist/session.mjs.map +1 -0
- package/dist/types.d.mts +66 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ice breaker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# @wevu/a2ui
|
|
2
|
+
|
|
3
|
+
A2UI 1.0 的组件无关小程序渲染运行时。复用 `@a2ui/web_core@0.16.1` 的消息处理、数据绑定与节点解析,把 surface 投影为纯 JSON 节点树,由消费方决定组件外观与交互入口。
|
|
4
|
+
|
|
5
|
+
当前为实验性 workspace 包,尚未发布到 npm;只承诺 A2UI 1.0 的四种 surface 状态消息与微信小程序目标。不在 AppService 中生成模板、执行模型返回的代码,也不引入 lit。
|
|
6
|
+
|
|
7
|
+
## 定位
|
|
8
|
+
|
|
9
|
+
与 `@wevu/json-render` 一样,本包只做运行时,不提供 UI 组件:
|
|
10
|
+
|
|
11
|
+
- catalog 由消费方以 A2UI 1.0 catalog 文档注入,组件集合不受本包限制;
|
|
12
|
+
- 投影结果是 `A2uiNode` JSON 树,渲染层用预编译递归组件按 `node.type` 静态分支,业务组件在同一组件内注册;
|
|
13
|
+
- 数据模型由服务端 surface 权威维护,输入写回绑定,动作以 `ActionPayload` 外发给调用方。
|
|
14
|
+
|
|
15
|
+
## 安装与构建配置
|
|
16
|
+
|
|
17
|
+
仓库内的消费项目添加 `"@wevu/a2ui": "workspace:*"` 与 `"@a2ui/web_core": "0.16.1"`,保留 `wevu` 依赖(渲染宿主使用)。原生 npm 依赖若打进小程序,需按应用的 npm 构建配置处理:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
pnpm --filter @wevu/a2ui build
|
|
21
|
+
pnpm --filter wevu-a2ui-demo build
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`src/compat.ts` 导出 `installA2uiRuntimeCompat()`,由 `createA2uiCatalog` 在进入 Core 前显式调用:Core 0.15 起使用 `Object.hasOwn`,0.16 起在动作解析路径构造 `AbortController`,而部分小程序基础库与 AppService 尚未提供这些内建。兼容层复用 `@wevu/web-apis/abort` 的 polyfill,宿主已有同名内建时保持原实现。这里刻意不用裸副作用导入,因为小程序构建会把无引用的副作用模块裁掉,`sideEffects` 声明并不能保证保留。
|
|
25
|
+
|
|
26
|
+
## 会话 API
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { createA2uiSession } from '@wevu/a2ui'
|
|
30
|
+
import catalog from './catalog.json'
|
|
31
|
+
|
|
32
|
+
const session = createA2uiSession({
|
|
33
|
+
catalog,
|
|
34
|
+
onSurfaces(views) {
|
|
35
|
+
// views: A2uiSurfaceView[],每项为 { id, root: A2uiNode }
|
|
36
|
+
render(views)
|
|
37
|
+
},
|
|
38
|
+
onAction(message) {
|
|
39
|
+
// message: { version: '1.0', action: ActionPayload }
|
|
40
|
+
socket.send(message)
|
|
41
|
+
},
|
|
42
|
+
onError(error) {
|
|
43
|
+
report(error)
|
|
44
|
+
},
|
|
45
|
+
functionImplementations: {
|
|
46
|
+
async format(args) {
|
|
47
|
+
return String(args.value)
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
session.receive(message) // 单个 A2UI 1.0 状态消息
|
|
53
|
+
await session.interact(surfaceId, { type: 'input', nodeId, bindingName: 'value', value: 'hello' })
|
|
54
|
+
await session.interact(surfaceId, { type: 'action', nodeId, actionName: 'action' })
|
|
55
|
+
session.dispose()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
| API | 行为 |
|
|
59
|
+
| --- | --- |
|
|
60
|
+
| `receive(message)` | 校验并应用 `createSurface`、`updateComponents`、`updateDataModel`、`deleteSurface`;RPC、内部操作、批量消息及空 `surfaceId` 直接拒绝 |
|
|
61
|
+
| `interact(surfaceId, event)` | `input` 写回选定的可写绑定;`action` 执行选定的 Core 动作并把产生的 `ActionPayload` 按顺序交给 `onAction` |
|
|
62
|
+
| `onSurfaces(views)` | 每次可见状态变化后整体推送;`updateDataModel` 的标量写入与 `null` 删除只更新绑定值,不替换节点 |
|
|
63
|
+
| `onAction(message)` | 可返回 Promise;单个动作产生的消息按序串行等待,不同 `interact` 的异步回调可能交错;本地函数动作不会外发 |
|
|
64
|
+
| `onError(error)` | 可选;接收 Core surface、消息处理与表达式错误,可返回 Promise |
|
|
65
|
+
| `dispose()` | 幂等;停止投影、释放 resolver 与 processor,之后不再回调 |
|
|
66
|
+
|
|
67
|
+
`functionImplementations` 为可选能力扩展。catalog 中声明的函数会保留完整签名并照常参与校验,未提供实现的函数在调用时抛出表达式错误,并通过 `onError` 上报;构造会话时提供未声明函数的实现会立即失败。只按对象自身属性查找实现,`toString` 等继承成员不会被误当作实现。
|
|
68
|
+
|
|
69
|
+
实现只接收已校验的参数字典,不接收 Core `DataContext` 或 `AbortSignal`,因此 `dispose()` 不会强制取消已启动的异步实现。动作 handler 不要 `await session.interact(...)`:动作捕获区通过会话级队列串行,等待同一队列会挂起。`functionCall` 动作在本地执行且不外发 `onAction`;本包不接收 `callRendererFunction` / `agentFunctionResponse`。
|
|
70
|
+
|
|
71
|
+
## 视图投影
|
|
72
|
+
|
|
73
|
+
`A2uiNode` 的结构:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
interface A2uiNode {
|
|
77
|
+
readonly id: string // `${节点ID}-v${generation}`,仅在当前 generation 内稳定
|
|
78
|
+
readonly componentId: string
|
|
79
|
+
readonly type: string // catalog 组件名
|
|
80
|
+
readonly props: { readonly [key: string]: A2uiPropValue }
|
|
81
|
+
readonly children: readonly A2uiNode[]
|
|
82
|
+
readonly actions: readonly string[] // 值为动作的属性名
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`A2uiPropValue` 是 JSON 值、`A2uiNode` 或包含两者的递归数组/对象。直接作为属性值的组件节点与纯组件节点数组进入 `children`;嵌套在对象或混合数组中的节点保留在 `props` 原位置,例如 Basic Catalog 的 `Tabs.tabs[].child`。这些嵌套节点同样进入操作表,可执行其动作与顶层可写绑定。
|
|
87
|
+
|
|
88
|
+
- `ResolvedBinding` 投影为其当前值;`WritableBinding` 除投影值外按 catalog 属性名登记为可写输入,一个节点可以保留多个;
|
|
89
|
+
- 可写输入与动作分别通过 `bindingName` / `actionName` 选择;省略时仅在候选数量为 1 时回退,多个候选或显式未知名称会抛出 `TypeError`;
|
|
90
|
+
- 仅直接暴露在节点顶层的可写绑定可选择;深层绑定只投影当前值,深层函数属性也不会被登记为动作;
|
|
91
|
+
- 同一节点的可写绑定与动作可同时存在;动作执行通过会话级队列串行,异步动作完成前不会提前清理消息捕获状态;
|
|
92
|
+
- `updateComponents` 或非 `null` 对象型 `updateDataModel` 会递增 generation 并清空旧操作表,视图层可通过 id 变化重建引用;标量写入与 `null` 删除只更新绑定值。
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
interface A2uiSession {
|
|
96
|
+
receive: (message: unknown) => void
|
|
97
|
+
interact: (surfaceId: string, event: A2uiInteraction) => Promise<void>
|
|
98
|
+
dispose: () => void
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
type A2uiInteraction
|
|
102
|
+
= | { type: 'input', nodeId: string, value: A2uiJsonValue, bindingName?: string }
|
|
103
|
+
| { type: 'action', nodeId: string, actionName?: string }
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`interact` 使用投影时的操作表;节点已被替换或 surface 已删除时静默忽略。
|
|
107
|
+
|
|
108
|
+
## 渲染宿主
|
|
109
|
+
|
|
110
|
+
小程序模板不支持 `<component :is>`,渲染层需要预编译一个递归组件,为目录里的每个类型提供静态分支:
|
|
111
|
+
|
|
112
|
+
```vue
|
|
113
|
+
<script setup lang="ts">
|
|
114
|
+
import type { A2uiInteraction, A2uiNode } from '@wevu/a2ui'
|
|
115
|
+
|
|
116
|
+
const props = defineProps<{ node: A2uiNode }>()
|
|
117
|
+
const emit = defineEmits<{ (event: 'interaction', payload: A2uiInteraction): void }>()
|
|
118
|
+
defineComponentJson({
|
|
119
|
+
usingComponents: { 'a2ui-node': './index', 'a2ui-order': '/components/a2ui-order/index' },
|
|
120
|
+
})
|
|
121
|
+
</script>
|
|
122
|
+
|
|
123
|
+
<template>
|
|
124
|
+
<view v-if="props.node.type === 'Column' || props.node.type === 'List'">
|
|
125
|
+
<a2ui-node
|
|
126
|
+
v-for="child in props.node.children"
|
|
127
|
+
:key="child.id"
|
|
128
|
+
:node="child"
|
|
129
|
+
@interaction="emit('interaction', $event)"
|
|
130
|
+
/>
|
|
131
|
+
</view>
|
|
132
|
+
<text v-else-if="props.node.type === 'Text'">{{ props.node.props.text }}</text>
|
|
133
|
+
<input
|
|
134
|
+
v-else-if="props.node.type === 'TextField'"
|
|
135
|
+
:value="props.node.props.value"
|
|
136
|
+
@input="emit('interaction', { type: 'input', nodeId: props.node.id, bindingName: 'value', value: $event.detail.value })"
|
|
137
|
+
>
|
|
138
|
+
<button v-else-if="props.node.type === 'Button'" @tap="emit('interaction', { type: 'action', nodeId: props.node.id })">
|
|
139
|
+
{{ props.node.children[0]?.props.text }}
|
|
140
|
+
</button>
|
|
141
|
+
<a2ui-order v-else-if="props.node.type === 'OrderSummary'" :number="props.node.props.number" />
|
|
142
|
+
<text v-else>未支持组件:{{ props.node.type }}</text>
|
|
143
|
+
</template>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
宿主按 `node.type` 显式分支,未覆盖的类型渲染兜底节点;业务组件在同一递归组件的 `usingComponents` 中注册,同样按 `node.children` 渲染子节点。宿主组件的 `v-for` 使用 `node.id`(含 generation 后缀)以保证替换后重建,页面 `usingComponents` 仍按路径注册。本包不提供任何具体组件实现,`apps/wevu-a2ui-demo` 给出一份可直接参考的宿主组件。
|
|
147
|
+
|
|
148
|
+
Chat 消息编排的参考实现采用同样的 envelope 校验、generation 操作表与 `WritableBinding.set` 回写语义,组件层则直接绑定各自 UI 库的输入、按钮与卡片;本包把组件层完全留给消费方。
|
|
149
|
+
|
|
150
|
+
## 协议与边界
|
|
151
|
+
|
|
152
|
+
- 消息:仅 `version: '1.0'` 的 `createSurface`、`updateComponents`、`updateDataModel`、`deleteSurface`;使用 `STRICT_VALIDATION` 与 1.0 目标版本。
|
|
153
|
+
- `updateComponents` 按 id upsert 且必须保持整树可达:协议不删除单个组件,隐藏节点通过绑定值表达,否则 Core 抛出可达性完整性错误。
|
|
154
|
+
- 节点 ID 遵循 Core 的 UAX #31 校验;模板 `ChildList`(`{ componentId, path }`)由 Core 解析,数据更新会导致实例重建。
|
|
155
|
+
- 支持通过宿主实现 catalog 函数;不支持 checks、RPC 与 agent 函数响应,事件动作只外发且不等待 agent 回写。
|
|
156
|
+
- 不包含 Markdown 渲染、图片/勾选等目录扩展;catalog 能声明哪些组件由消费方决定,但视图层必须为每个类型提供宿主分支。
|
|
157
|
+
- `updateDataModel` 失败时回滚目标 surface 的整份数据快照,避免 Core 部分写入后留下半成品;失败仍会向调用方抛出。快照是 O(N) 深拷贝,数据模型很大时应优先使用更小的增量路径。
|
|
158
|
+
- 运行时只接受 JSON 数据;投影会丢弃未登记的函数与 `undefined`,将 `NaN`/`Infinity` 转为 `null`,并保留 props 中的嵌套组件节点。
|
|
159
|
+
- 无 lit、无动态求值;兼容层只补齐 `Object.hasOwn`、`AbortController` 与 `AbortSignal`。
|
|
160
|
+
- 需要显式调用 `dispose()`;`receive` 不接管 transport、重连或排队。
|
|
161
|
+
|
|
162
|
+
## 验证与发布边界
|
|
163
|
+
|
|
164
|
+
包级 22 项单测覆盖投影绑定、多绑定/多动作选择、嵌套槽位、输入回写、动作外发、异步动作隔离、本地 `functionCall`、函数错误与继承成员边界、模板列表 respawn、路径删除、消息校验、失败回滚与幂等销毁;tsd 锁定公开类型。构建产物为 ESM + d.mts,`@a2ui/web_core` 作为外部依赖由宿主应用打包。
|
|
165
|
+
|
|
166
|
+
`apps/wevu-a2ui-demo` 与 `e2e/ide/wevu-a2ui.runtime.test.ts` 提供 headless 与真实微信 DevTools 的同场景验收,覆盖基础目录页、递归宿主、输入写回、增量替换后的输入保留、异常消息保留上一版界面和卸载清理。应用的 `test/noEval.test.ts` 从公共入口打包,在禁用 `Function` / `eval` 的 `vm` 上下文执行。
|
|
167
|
+
|
|
168
|
+
尚未提供:内置组件包与构建期 resolver(宿主组件目前由 `apps/wevu-a2ui-demo` 自带)、catalog 子集对齐(Row/Image/ChoicePicker 等)以及 checks/RPC 长尾。本包也未执行 npm 发布。
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { A2uiFunctionHandler } from "./types.mjs";
|
|
2
|
+
import { Catalog, ComponentApi, FunctionImplementation } from "@a2ui/web_core/catalog";
|
|
3
|
+
/** 当前实现只接受 v1.0 消息与目录。 */
|
|
4
|
+
export declare const A2UI_PROTOCOL_VERSION: "1.0";
|
|
5
|
+
export interface A2uiCatalog {
|
|
6
|
+
readonly id: string;
|
|
7
|
+
readonly catalog: Catalog<ComponentApi, FunctionImplementation>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 从消费方的 catalog 文档构建可执行目录。
|
|
11
|
+
*
|
|
12
|
+
* Core 的 `fromJson` 产物是 schema-only 目录,`NodeResolver` 需要可执行函数目录。
|
|
13
|
+
* 保留函数签名供消息校验;未实现函数会在调用时通过 `onError` 明确报错。
|
|
14
|
+
*/
|
|
15
|
+
export declare function createA2uiCatalog(document: Record<string, any>, functionImplementations?: Readonly<Record<string, A2uiFunctionHandler>>): A2uiCatalog;
|
package/dist/catalog.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{installA2uiRuntimeCompat as e}from"./compat.mjs";import{Catalog as t,createFunctionImplementation as n}from"@a2ui/web_core/catalog";import{A2uiExpressionError as r}from"@a2ui/web_core/errors";const i=`1.0`;function a(i,a={}){e();let o=t.fromJson(i),s=String(o.protocolVersion);if(s!==`1.0`&&s!==`v1.0`)throw TypeError(`@wevu/a2ui 仅支持 A2UI 1.0 catalog,收到 ${s}。`);for(let e of Object.keys(a))if(!o.functions.has(e))throw TypeError(`catalog '${o.id}' 未声明函数 '${e}'。`);let c=[...o.functions.values()].map(e=>n(e,t=>{let n=Object.hasOwn(a,e.name)?a[e.name]:void 0;if(!n)throw new r(`@wevu/a2ui 未实现 catalog 函数 '${e.name}'。`,e.name);return n(t)})),l=new t(o.id,`1.0`,[...o.components.values()],c,{defs:o.defs});return{id:o.id,catalog:l}}export{i as A2UI_PROTOCOL_VERSION,a as createA2uiCatalog};
|
|
2
|
+
//# sourceMappingURL=catalog.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.mjs","names":["A2UI_PROTOCOL_VERSION","createA2uiCatalog","installA2uiRuntimeCompat","schemaCatalog","Catalog","document","protocolVersion","name","functionImplementations","functions","createFunctionImplementation","api","implementation","A2uiExpressionError","args","catalog"],"sources":["../src/catalog.ts"],"sourcesContent":["import type { ComponentApi, FunctionImplementation } from '@a2ui/web_core/catalog'\nimport type { A2uiFunctionHandler } from './types'\nimport { Catalog, createFunctionImplementation } from '@a2ui/web_core/catalog'\nimport { A2uiExpressionError } from '@a2ui/web_core/errors'\nimport { installA2uiRuntimeCompat } from './compat'\n\n/** 当前实现只接受 v1.0 消息与目录。 */\nexport const A2UI_PROTOCOL_VERSION = '1.0' as const\n\nexport interface A2uiCatalog {\n readonly id: string\n readonly catalog: Catalog<ComponentApi, FunctionImplementation>\n}\n\n/**\n * 从消费方的 catalog 文档构建可执行目录。\n *\n * Core 的 `fromJson` 产物是 schema-only 目录,`NodeResolver` 需要可执行函数目录。\n * 保留函数签名供消息校验;未实现函数会在调用时通过 `onError` 明确报错。\n */\nexport function createA2uiCatalog(\n document: Record<string, any>,\n functionImplementations: Readonly<Record<string, A2uiFunctionHandler>> = {},\n): A2uiCatalog {\n installA2uiRuntimeCompat()\n const schemaCatalog = Catalog.fromJson(document)\n const protocolVersion = String(schemaCatalog.protocolVersion)\n if (protocolVersion !== '1.0' && protocolVersion !== 'v1.0') {\n throw new TypeError(`@wevu/a2ui 仅支持 A2UI 1.0 catalog,收到 ${protocolVersion}。`)\n }\n for (const name of Object.keys(functionImplementations)) {\n if (!schemaCatalog.functions.has(name)) {\n throw new TypeError(`catalog '${schemaCatalog.id}' 未声明函数 '${name}'。`)\n }\n }\n const functions = [...schemaCatalog.functions.values()].map(api => createFunctionImplementation(api, (args) => {\n const implementation = Object.hasOwn(functionImplementations, api.name)\n ? functionImplementations[api.name]\n : undefined\n if (!implementation) {\n throw new A2uiExpressionError(`@wevu/a2ui 未实现 catalog 函数 '${api.name}'。`, api.name)\n }\n return implementation(args)\n }))\n const catalog = new Catalog(\n schemaCatalog.id,\n A2UI_PROTOCOL_VERSION,\n [...schemaCatalog.components.values()],\n functions,\n { defs: schemaCatalog.defs },\n )\n return { id: schemaCatalog.id, catalog }\n}\n"],"mappings":"A,uMAOA,MAAaA,EAAwB,MAarC,SAAgBC,EACd,EACA,EAAyE,CAAC,EAC7D,CACbC,EAAyB,EACzB,IAAMC,EAAgBC,EAAQ,SAASC,CAAQ,EACzCC,EAAkB,OAAOH,EAAc,eAAe,EAC5D,GAAIG,IAAoB,OAASA,IAAoB,OACnD,MAAU,UAAU,sCAAsCA,EAAgB,EAAE,EAE9E,IAAK,IAAMC,KAAQ,OAAO,KAAKC,CAAuB,EACpD,GAAI,CAACL,EAAc,UAAU,IAAII,CAAI,EACnC,MAAU,UAAU,YAAYJ,EAAc,GAAG,WAAWI,EAAK,GAAG,EAGxE,IAAME,EAAY,CAAC,GAAGN,EAAc,UAAU,OAAO,CAAC,CAAC,CAAC,IAAI,GAAOO,EAA6BC,EAAM,GAAS,CAC7G,IAAMC,EAAiB,OAAO,OAAOJ,EAAyBG,EAAI,IAAI,EAClEH,EAAwBG,EAAI,MAC5B,IAAA,GACJ,GAAI,CAACC,EACH,MAAM,IAAIC,EAAoB,8BAA8BF,EAAI,KAAK,IAAKA,EAAI,IAAI,EAEpF,OAAOC,EAAeE,CAAI,CAC5B,CAAC,CAAC,EACIC,EAAU,IAAIX,EAClBD,EAAc,GAAA,MAEd,CAAC,GAAGA,EAAc,WAAW,OAAO,CAAC,EACrCM,EACA,CAAE,KAAMN,EAAc,IAAK,CAC7B,EACA,MAAO,CAAE,GAAIA,EAAc,GAAI,SAAQ,CACzC,C"}
|
package/dist/compat.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{AbortControllerPolyfill as e,AbortSignalPolyfill as t}from"@wevu/web-apis/abort";function n(){let n=globalThis;typeof n.AbortController!=`function`&&(n.AbortController=e),typeof n.AbortSignal!=`function`&&(n.AbortSignal=t),typeof Object.hasOwn!=`function`&&Object.defineProperty(Object,"hasOwn",{configurable:!0,writable:!0,value(e,t){if(e==null)throw TypeError(`Object.hasOwn requires a non-null object`);return Object.prototype.hasOwnProperty.call(e,t)}})}export{n as installA2uiRuntimeCompat};
|
|
2
|
+
//# sourceMappingURL=compat.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compat.mjs","names":["installA2uiRuntimeCompat","host","AbortControllerPolyfill","AbortSignalPolyfill","object","key"],"sources":["../src/compat.ts"],"sourcesContent":["import { AbortControllerPolyfill, AbortSignalPolyfill } from '@wevu/web-apis/abort'\n\n/**\n * 安装 A2UI Core 需要、但小程序 AppService 未必提供的内建。\n *\n * Core 0.15 起使用 `Object.hasOwn`,0.16 起在动作解析路径构造 `AbortController`;\n * 宿主已提供同名内建时保持原实现,重复调用无副作用。入口显式调用本函数而不是依赖\n * 裸副作用导入,避免打包阶段把兼容层当作无引用模块裁剪掉。\n */\nexport function installA2uiRuntimeCompat() {\n const host = globalThis as Record<string, any>\n if (typeof host.AbortController !== 'function') {\n host.AbortController = AbortControllerPolyfill\n }\n if (typeof host.AbortSignal !== 'function') {\n host.AbortSignal = AbortSignalPolyfill\n }\n if (typeof Object.hasOwn !== 'function') {\n Object.defineProperty(Object, 'hasOwn', {\n configurable: true,\n writable: true,\n value(object: unknown, key: PropertyKey) {\n if (object == null) {\n throw new TypeError('Object.hasOwn requires a non-null object')\n }\n return Object.prototype.hasOwnProperty.call(object, key)\n },\n })\n }\n}\n"],"mappings":"A,wFASA,SAAgBA,GAA2B,CACzC,IAAMC,EAAO,WACT,OAAOA,EAAK,iBAAoB,aAClC,EAAK,gBAAkBC,GAErB,OAAOD,EAAK,aAAgB,aAC9B,EAAK,YAAcE,GAEjB,OAAO,OAAO,QAAW,YAC3B,OAAO,eAAe,OAAQ,SAAU,CACtC,aAAc,GACd,SAAU,GACV,MAAM,EAAiB,EAAkB,CACvC,GAAIC,GAAU,KACZ,MAAU,UAAU,0CAA0C,EAEhE,OAAO,OAAO,UAAU,eAAe,KAAKA,EAAQC,CAAG,CACzD,CACF,CAAC,CAEL,C"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare const A2UI_STATE_MESSAGES: readonly ["createSurface", "updateComponents", "updateDataModel", "deleteSurface"];
|
|
2
|
+
export type A2uiStateMessageType = typeof A2UI_STATE_MESSAGES[number];
|
|
3
|
+
type A2uiEnvelopeMessage = {
|
|
4
|
+
readonly version: '1.0';
|
|
5
|
+
readonly createSurface: unknown;
|
|
6
|
+
} | {
|
|
7
|
+
readonly version: '1.0';
|
|
8
|
+
readonly updateComponents: unknown;
|
|
9
|
+
} | {
|
|
10
|
+
readonly version: '1.0';
|
|
11
|
+
readonly updateDataModel: unknown;
|
|
12
|
+
} | {
|
|
13
|
+
readonly version: '1.0';
|
|
14
|
+
readonly deleteSurface: unknown;
|
|
15
|
+
};
|
|
16
|
+
export interface A2uiEnvelope {
|
|
17
|
+
readonly type: A2uiStateMessageType;
|
|
18
|
+
readonly surfaceId: string | undefined;
|
|
19
|
+
/** 组件或数据被整体替换,需要重建绑定。 */
|
|
20
|
+
readonly replacesBindings: boolean;
|
|
21
|
+
readonly message: A2uiEnvelopeMessage;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 校验外部消息为单个 A2UI 1.0 状态消息。
|
|
25
|
+
*
|
|
26
|
+
* 只接受 `version` 与四种 surface 状态消息之一;RPC、内部操作与批量消息一律拒绝。
|
|
27
|
+
*/
|
|
28
|
+
export declare function readA2uiEnvelope(message: unknown): A2uiEnvelope;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{A2uiValidationError as e}from"@a2ui/web_core/errors";const t=[`createSurface`,`updateComponents`,`updateDataModel`,`deleteSurface`];function n(n){let r=()=>new e(`Expected one A2UI 1.0 surface state message.`);if(typeof n!=`object`||!n||Array.isArray(n))throw r();let i=Reflect.ownKeys(n),a=n,o=t.find(e=>i.includes(e));if(i.length!==2||!i.includes(`version`)||a.version!==`1.0`||!o)throw r();let s=a[o],c=typeof s==`object`&&s&&`surfaceId`in s&&typeof s.surfaceId==`string`?s.surfaceId:void 0;if(c===``)throw new e(`A2UI surfaceId must not be empty.`);return{type:o,surfaceId:c,replacesBindings:o===`updateComponents`||o===`updateDataModel`&&typeof s==`object`&&!!s&&`value`in s&&s.value!==null&&typeof s.value==`object`,message:{version:`1.0`,[o]:s}}}export{t as A2UI_STATE_MESSAGES,n as readA2uiEnvelope};
|
|
2
|
+
//# sourceMappingURL=envelope.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envelope.mjs","names":["A2UI_STATE_MESSAGES","readA2uiEnvelope","invalid","A2uiValidationError","message","keys","envelope","type","key","payload","surfaceId"],"sources":["../src/envelope.ts"],"sourcesContent":["import { A2uiValidationError } from '@a2ui/web_core/errors'\n\nexport const A2UI_STATE_MESSAGES = ['createSurface', 'updateComponents', 'updateDataModel', 'deleteSurface'] as const\n\nexport type A2uiStateMessageType = typeof A2UI_STATE_MESSAGES[number]\n\ntype A2uiEnvelopeMessage\n = | { readonly version: '1.0', readonly createSurface: unknown }\n | { readonly version: '1.0', readonly updateComponents: unknown }\n | { readonly version: '1.0', readonly updateDataModel: unknown }\n | { readonly version: '1.0', readonly deleteSurface: unknown }\n\nexport interface A2uiEnvelope {\n readonly type: A2uiStateMessageType\n readonly surfaceId: string | undefined\n /** 组件或数据被整体替换,需要重建绑定。 */\n readonly replacesBindings: boolean\n readonly message: A2uiEnvelopeMessage\n}\n\n/**\n * 校验外部消息为单个 A2UI 1.0 状态消息。\n *\n * 只接受 `version` 与四种 surface 状态消息之一;RPC、内部操作与批量消息一律拒绝。\n */\nexport function readA2uiEnvelope(message: unknown): A2uiEnvelope {\n const invalid = () => new A2uiValidationError('Expected one A2UI 1.0 surface state message.')\n if (typeof message !== 'object' || message === null || Array.isArray(message)) {\n throw invalid()\n }\n const keys = Reflect.ownKeys(message)\n const envelope = message as Record<string, unknown>\n const type = A2UI_STATE_MESSAGES.find(key => keys.includes(key))\n if (keys.length !== 2 || !keys.includes('version') || envelope.version !== '1.0' || !type) {\n throw invalid()\n }\n const payload = envelope[type]\n const surfaceId = typeof payload === 'object' && payload !== null && 'surfaceId' in payload\n && typeof payload.surfaceId === 'string'\n ? payload.surfaceId\n : undefined\n // Core 允许创建空 ID,但后续更新与删除会忽略它。\n if (surfaceId === '') {\n throw new A2uiValidationError('A2UI surfaceId must not be empty.')\n }\n return {\n type,\n surfaceId,\n replacesBindings: type === 'updateComponents' || (type === 'updateDataModel'\n && typeof payload === 'object' && payload !== null && 'value' in payload\n && (payload as Record<string, unknown>).value !== null\n && typeof (payload as Record<string, unknown>).value === 'object'),\n message: { version: '1.0', [type]: payload } as A2uiEnvelopeMessage,\n }\n}\n"],"mappings":"A,4DAEA,MAAaA,EAAsB,CAAC,gBAAiB,mBAAoB,kBAAmB,eAAe,EAuB3G,SAAgBC,EAAiB,EAAgC,CAC/D,IAAMC,MAAgB,IAAIC,EAAoB,8CAA8C,EAC5F,GAAI,OAAOC,GAAY,WAAYA,GAAoB,MAAM,QAAQA,CAAO,EAC1E,MAAMF,EAAQ,EAEhB,IAAMG,EAAO,QAAQ,QAAQD,CAAO,EAC9BE,EAAWF,EACXG,EAAOP,EAAoB,KAAK,GAAOK,EAAK,SAASG,CAAG,CAAC,EAC/D,GAAIH,EAAK,SAAW,GAAK,CAACA,EAAK,SAAS,SAAS,GAAKC,EAAS,UAAY,OAAS,CAACC,EACnF,MAAML,EAAQ,EAEhB,IAAMO,EAAUH,EAASC,GACnBG,EAAY,OAAOD,GAAY,UAAYA,GAAoB,cAAeA,GAC/E,OAAOA,EAAQ,WAAc,SAC9BA,EAAQ,UACR,IAAA,GAEJ,GAAIC,IAAc,GAChB,MAAM,IAAIP,EAAoB,mCAAmC,EAEnE,MAAO,CACL,OACA,YACA,iBAAkBI,IAAS,oBAAuBA,IAAS,mBACtD,OAAOE,GAAY,YAAYA,GAAoB,UAAWA,GAC7DA,EAAoC,QAAU,MAC/C,OAAQA,EAAoC,OAAU,SAC3D,QAAS,CAAE,QAAS,OAAQF,GAAOE,CAAQ,CAC7C,CACF,C"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { A2UI_PROTOCOL_VERSION, A2uiCatalog, createA2uiCatalog } from "./catalog.mjs";
|
|
2
|
+
import { installA2uiRuntimeCompat } from "./compat.mjs";
|
|
3
|
+
import { A2UI_STATE_MESSAGES, A2uiEnvelope, A2uiStateMessageType, readA2uiEnvelope } from "./envelope.mjs";
|
|
4
|
+
import { createA2uiSession } from "./session.mjs";
|
|
5
|
+
import { A2uiActionMessage, A2uiErrorMessage, A2uiFunctionHandler, A2uiInteraction, A2uiJsonValue, A2uiNode, A2uiPropValue, A2uiSession, A2uiSessionListeners, A2uiSessionOptions, A2uiSurfaceView } from "./types.mjs";
|
|
6
|
+
export { A2UI_PROTOCOL_VERSION, A2UI_STATE_MESSAGES, type A2uiActionMessage, type A2uiCatalog, type A2uiEnvelope, type A2uiErrorMessage, type A2uiFunctionHandler, type A2uiInteraction, type A2uiJsonValue, type A2uiNode, type A2uiPropValue, type A2uiSession, type A2uiSessionListeners, type A2uiSessionOptions, type A2uiStateMessageType, type A2uiSurfaceView, createA2uiCatalog, createA2uiSession, installA2uiRuntimeCompat, readA2uiEnvelope };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{A2UI_PROTOCOL_VERSION as e,createA2uiCatalog as t}from"./catalog.mjs";import{installA2uiRuntimeCompat as n}from"./compat.mjs";import{A2UI_STATE_MESSAGES as r,readA2uiEnvelope as i}from"./envelope.mjs";import{createA2uiSession as a}from"./session.mjs";export{e as A2UI_PROTOCOL_VERSION,r as A2UI_STATE_MESSAGES,t as createA2uiCatalog,a as createA2uiSession,n as installA2uiRuntimeCompat,i as readA2uiEnvelope};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{getValue as e}from"@a2ui/web_core/reactivity";import{ResolvedBinding as t,WritableBinding as n,isComponentNode as r}from"@a2ui/web_core/resolution";function i(e,t){let n=/* @__PURE__ */ new Map;return{root:a(e,n,t),operations:n}}function a(i,s,c){if(!r(i)||i.disposed||i.isPlaceholder)return null;let l=`${i.id}-v${c}`,u={},d=[],f=[],p=/* @__PURE__ */ new Map,m=/* @__PURE__ */ new Map;for(let[l,_]of Object.entries(e(i.props))){if(r(_)){let e=a(_,s,c);e&&d.push(e);continue}if(Array.isArray(_)&&_.length>0&&_.every(r)){for(let e of _){let t=a(e,s,c);t&&d.push(t)}continue}if(typeof _==`function`){f.push(l),m.set(l,_);continue}if(_ instanceof n){var h;p.set(l,_),u[l]=(h=o(_.value,s,c))==null?null:h;continue}if(_ instanceof t){var g;u[l]=(g=o(_.value,s,c))==null?null:g;continue}let e=o(_,s,c);e!==void 0&&(u[l]=e)}return(p.size>0||m.size>0)&&s.set(l,{inputs:p,actions:m}),{id:l,componentId:i.componentId,type:i.type,props:u,children:d,actions:f}}function o(e,n,i){if(e===null||typeof e==`string`||typeof e==`boolean`)return e;if(typeof e==`number`)return Number.isFinite(e)?e:null;if(e instanceof t)return o(e.value,n,i);if(r(e)){var s;return(s=a(e,n,i))==null?null:s}if(Array.isArray(e))return e.map(e=>{var t;return(t=o(e,n,i))==null?null:t});if(typeof e==`object`){let t={};for(let[r,a]of Object.entries(e)){let e=o(a,n,i);e!==void 0&&(t[r]=e)}return t}}export{i as projectA2uiSurface};
|
|
2
|
+
//# sourceMappingURL=projection.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"projection.mjs","names":["projectA2uiSurface","operations","projectNode","root","generation","isComponentNode","value","id","props","children","actionNames","inputs","actions","name","raw","getValue","child","item","WritableBinding","_toA2uiPropValue","toA2uiPropValue","ResolvedBinding","_toA2uiPropValue2","converted","_projectNode","_toA2uiPropValue3","result","key"],"sources":["../src/projection.ts"],"sourcesContent":["/* eslint-disable ts/no-use-before-define -- 节点投影与值转换互递归,函数声明由模块初始化提升。 */\nimport type { NodeAction } from '@a2ui/web_core/resolution'\nimport type { A2uiNode, A2uiPropValue } from './types'\nimport { getValue } from '@a2ui/web_core/reactivity'\nimport { isComponentNode, ResolvedBinding, WritableBinding } from '@a2ui/web_core/resolution'\n\n/** 节点上可回写的数据绑定与可执行动作;按属性名收集,避免静默选择第一项。 */\nexport interface A2uiNodeOperation {\n readonly inputs: ReadonlyMap<string, WritableBinding<unknown>>\n readonly actions: ReadonlyMap<string, NodeAction>\n}\n\nexport interface A2uiProjectedSurface {\n readonly root: A2uiNode | null\n readonly operations: Map<string, A2uiNodeOperation>\n}\n\n/**\n * 把 Core 解析出的节点树投影为组件无关的 JSON 树。\n *\n * 直接子节点按属性出现顺序收集到 children;对象或数组深处的槽位节点保留在 props 中。\n * 一个组件可以同时声明多个输入绑定与动作。\n */\nexport function projectA2uiSurface(root: unknown, generation: number): A2uiProjectedSurface {\n const operations = new Map<string, A2uiNodeOperation>()\n return { root: projectNode(root, operations, generation), operations }\n}\n\nfunction projectNode(value: unknown, operations: Map<string, A2uiNodeOperation>, generation: number): A2uiNode | null {\n if (!isComponentNode(value) || value.disposed || value.isPlaceholder) {\n return null\n }\n const id = `${value.id}-v${generation}`\n const props: Record<string, A2uiPropValue> = {}\n const children: A2uiNode[] = []\n const actionNames: string[] = []\n const inputs = new Map<string, WritableBinding<unknown>>()\n const actions = new Map<string, NodeAction>()\n for (const [name, raw] of Object.entries(getValue(value.props))) {\n if (isComponentNode(raw)) {\n const child = projectNode(raw, operations, generation)\n if (child) {\n children.push(child)\n }\n continue\n }\n if (Array.isArray(raw) && raw.length > 0 && raw.every(isComponentNode)) {\n for (const item of raw) {\n const child = projectNode(item, operations, generation)\n if (child) {\n children.push(child)\n }\n }\n continue\n }\n if (typeof raw === 'function') {\n actionNames.push(name)\n actions.set(name, raw as NodeAction)\n continue\n }\n if (raw instanceof WritableBinding) {\n inputs.set(name, raw as WritableBinding<unknown>)\n props[name] = toA2uiPropValue(raw.value, operations, generation) ?? null\n continue\n }\n if (raw instanceof ResolvedBinding) {\n props[name] = toA2uiPropValue(raw.value, operations, generation) ?? null\n continue\n }\n const converted = toA2uiPropValue(raw, operations, generation)\n if (converted !== undefined) {\n props[name] = converted\n }\n }\n if (inputs.size > 0 || actions.size > 0) {\n operations.set(id, { inputs, actions })\n }\n return { id, componentId: value.componentId, type: value.type, props, children, actions: actionNames }\n}\n\nfunction toA2uiPropValue(value: unknown, operations: Map<string, A2uiNodeOperation>, generation: number): A2uiPropValue | undefined {\n if (value === null || typeof value === 'string' || typeof value === 'boolean') {\n return value\n }\n if (typeof value === 'number') {\n return Number.isFinite(value) ? value : null\n }\n if (value instanceof ResolvedBinding) {\n return toA2uiPropValue(value.value, operations, generation)\n }\n if (isComponentNode(value)) {\n return projectNode(value, operations, generation) ?? null\n }\n if (Array.isArray(value)) {\n return value.map(item => toA2uiPropValue(item, operations, generation) ?? null)\n }\n if (typeof value === 'object') {\n const result: Record<string, A2uiPropValue> = {}\n for (const [key, item] of Object.entries(value as Record<string, unknown>)) {\n const converted = toA2uiPropValue(item, operations, generation)\n if (converted !== undefined) {\n result[key] = converted\n }\n }\n return result\n }\n return undefined\n}\n"],"mappings":"A,2JAuBA,SAAgBA,EAAmB,EAAe,EAA0C,CAC1F,IAAMC,kBAAa,IAAI,IACvB,MAAO,CAAE,KAAMC,EAAYC,EAAMF,EAAYG,CAAU,EAAG,YAAW,CACvE,CAEA,SAASF,EAAY,EAAgB,EAA4C,EAAqC,CACpH,GAAI,CAACG,EAAgBC,CAAK,GAAKA,EAAM,UAAYA,EAAM,cACrD,OAAO,KAET,IAAMC,EAAK,GAAGD,EAAM,GAAG,IAAIF,IACrBI,EAAuC,CAAC,EACxCC,EAAuB,CAAC,EACxBC,EAAwB,CAAC,EACzBC,kBAAS,IAAI,IACbC,kBAAU,IAAI,IACpB,IAAK,GAAM,CAACC,EAAMC,KAAQ,OAAO,QAAQC,EAAST,EAAM,KAAK,CAAC,EAAG,CAC/D,GAAID,EAAgBS,CAAG,EAAG,CACxB,IAAME,EAAQd,EAAYY,EAAKb,EAAYG,CAAU,EACjDY,GACFP,EAAS,KAAKO,CAAK,EAErB,QACF,CACA,GAAI,MAAM,QAAQF,CAAG,GAAKA,EAAI,OAAS,GAAKA,EAAI,MAAMT,CAAe,EAAG,CACtE,IAAK,IAAMY,KAAQH,EAAK,CACtB,IAAME,EAAQd,EAAYe,EAAMhB,EAAYG,CAAU,EAClDY,GACFP,EAAS,KAAKO,CAAK,CAEvB,CACA,QACF,CACA,GAAI,OAAOF,GAAQ,WAAY,CAC7BJ,EAAY,KAAKG,CAAI,EACrBD,EAAQ,IAAIC,EAAMC,CAAiB,EACnC,QACF,CACA,GAAIA,aAAeI,EAAiB,C,MAClCP,EAAO,IAAIE,EAAMC,CAA+B,EAChD,EAAMD,IAAA,EAAQO,EAAgBN,EAAI,MAAOb,EAAYG,CAAU,IAAA,KAAK,KAALe,EAC/D,QACF,CACA,GAAIL,aAAeO,EAAiB,C,MAClC,EAAMR,IAAA,EAAQO,EAAgBN,EAAI,MAAOb,EAAYG,CAAU,IAAA,KAAK,KAALkB,EAC/D,QACF,CACA,IAAMC,EAAYH,EAAgBN,EAAKb,EAAYG,CAAU,EACzDmB,IAAc,IAAA,KAChB,EAAMV,GAAQU,EAElB,CAIA,OAHIZ,EAAO,KAAO,GAAKC,EAAQ,KAAO,IACpCX,EAAW,IAAIM,EAAI,CAAE,SAAQ,SAAQ,CAAC,EAEjC,CAAE,KAAI,YAAaD,EAAM,YAAa,KAAMA,EAAM,KAAM,QAAO,WAAU,QAASI,CAAY,CACvG,CAEA,SAASU,EAAgB,EAAgB,EAA4C,EAA+C,CAClI,GAAId,IAAU,MAAQ,OAAOA,GAAU,UAAY,OAAOA,GAAU,UAClE,OAAOA,EAET,GAAI,OAAOA,GAAU,SACnB,OAAO,OAAO,SAASA,CAAK,EAAIA,EAAQ,KAE1C,GAAIA,aAAiBe,EACnB,OAAOD,EAAgBd,EAAM,MAAOL,EAAYG,CAAU,EAE5D,GAAIC,EAAgBC,CAAK,EAAG,C,MAC1B,OAAA,EAAOJ,EAAYI,EAAOL,EAAYG,CAAU,IAAA,KAAK,KAALoB,CAClD,CACA,GAAI,MAAM,QAAQlB,CAAK,EACrB,OAAOA,EAAM,IAAI,GAAQ,C,MAAgB,OAAA,EAAAc,EAAAH,EAAMhB,EAAYG,CAAU,IAAA,KAAK,KAALqB,CAAQ,CAAC,EAEhF,GAAI,OAAOnB,GAAU,SAAU,CAC7B,IAAMoB,EAAwC,CAAC,EAC/C,IAAK,GAAM,CAACC,EAAKV,KAAS,OAAO,QAAQX,CAAgC,EAAG,CAC1E,IAAMiB,EAAYH,EAAgBH,EAAMhB,EAAYG,CAAU,EAC1DmB,IAAc,IAAA,KAChB,EAAOI,GAAOJ,EAElB,CACA,OAAOG,CACT,CAEF,C"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { A2uiSession, A2uiSessionOptions } from "./types.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* 创建与页面生命周期解耦的 A2UI 渲染会话。
|
|
4
|
+
*
|
|
5
|
+
* 会话只处理 surface 状态消息,不接管 transport、agent 或 RPC;用户交互通过
|
|
6
|
+
* `interact` 进入,动作经 `onAction` 交给调用方发送给 agent。
|
|
7
|
+
*/
|
|
8
|
+
export declare function createA2uiSession(options: A2uiSessionOptions): A2uiSession;
|
package/dist/session.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{createA2uiCatalog as e}from"./catalog.mjs";import{A2UI_STATE_MESSAGES as t,readA2uiEnvelope as n}from"./envelope.mjs";import{projectA2uiSurface as r}from"./projection.mjs";import{MessageProcessor as i,STRICT_VALIDATION as a}from"@a2ui/web_core/processing";import{batchWrite as o,effect as s,getValue as c,peekValue as l,setValue as u,signal as d}from"@a2ui/web_core/reactivity";import{NodeResolver as f}from"@a2ui/web_core/resolution";function p(e){return JSON.parse(JSON.stringify(e))}function m(e){if(Array.isArray(e)){let t=[];t.length=e.length;for(let n=0;n<e.length;n++)n in e&&(t[n]=m(e[n]));return t}if(typeof e==`object`&&e){let t={};for(let[n,r]of Object.entries(e))Object.defineProperty(t,n,{value:m(r),enumerable:!0,configurable:!0,writable:!0});return t}return e}function h(e,t,n,r){if(t!==void 0){let i=e.get(t);if(!i)throw TypeError(`节点 '${r}' 不存在名为 '${t}' 的${n===`binding`?`输入绑定`:`动作`}。`);return i}if(e.size===1)return e.values().next().value;if(e.size>1)throw TypeError(`节点 '${r}' 有多个${n===`binding`?`输入绑定`:`动作`},请指定${n===`binding`?` bindingName`:` actionName`}。`)}function g(g){let{catalog:_}=e(g.catalog,g.functionImplementations),v=new i([_],void 0,{validationConfig:{...a,targetVersion:`1.0`,allowedMessages:[...t]}}),y=/* @__PURE__ */ new Map,b=d(0),x=!1,S=!1,C,w=Promise.resolve(),T=v.model.onAction.subscribe(e=>{!x&&C&&C.push({version:`1.0`,action:p(e)})}),E=v.model.onError.subscribe(e=>{if(!x){var t;return(t=g.onError)==null?void 0:t.call(g,p(e))}}),D=s(()=>{c(b);let e=[];for(let[t,n]of y){let i=r(c(n.resolver.rootNode),n.generation);n.operations=i.operations,i.root&&e.push({id:t,root:i.root})}x||!S&&e.length===0||(S=e.length>0,g.onSurfaces(e))});function O(e){y.set(e.id,{resolver:new f(e),generation:0,operations:/* @__PURE__ */ new Map})}function k(e){let t=y.get(e);t&&(y.delete(e),t.resolver.dispose(),t.operations.clear())}return{receive(e){if(x)return;let t=n(e),{type:r,surfaceId:i}=t;o(()=>{let e=i===void 0?void 0:v.getSurface(i),n=r===`updateDataModel`&&e?{surface:e,data:m(e.dataModel.get(`/`))}:void 0;try{v.processMessages(t.message)}catch(t){throw n&&n.surface.dataModel.set(`/`,n.data),r===`createSurface`&&!e&&i!==void 0&&v.model.deleteSurface(i),t}if(i!==void 0){if(r===`createSurface`){let e=v.getSurface(i);e&&O(e)}else if(r===`deleteSurface`)k(i);else if(t.replacesBindings){let e=y.get(i);e&&(e.generation+=1,e.operations.clear())}(r!==`updateDataModel`||t.replacesBindings)&&u(b,l(b)+1)}})},async interact(e,t){var n;if(x)return;let r=(n=y.get(e))==null?void 0:n.operations.get(t.nodeId);if(!r)return;if(t.type===`input`){let e=h(r.inputs,t.bindingName,`binding`,t.nodeId);e&&e.value!==t.value&&o(()=>e.set(t.value));return}let i=h(r.actions,t.actionName,`action`,t.nodeId);if(!i)return;let a=[],s=w.then(async()=>{if(!x){C=a;try{await i()}finally{C===a&&(C=void 0)}}});w=s.catch(()=>{}),await s;for(let e of a){if(x)return;await g.onAction(e)}},dispose(){if(!x){x=!0,D(),T.unsubscribe(),E.unsubscribe();for(let e of y.keys())k(e);v.model.dispose(),v.dispose()}}}}export{g as createA2uiSession};
|
|
2
|
+
//# sourceMappingURL=session.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.mjs","names":["clonePayload","payload","cloneDataModelValue","value","result","index","key","item","selectOperation","name","selected","operations","nodeId","kind","createA2uiSession","createA2uiCatalog","options","processor","MessageProcessor","catalog","STRICT_VALIDATION","A2UI_STATE_MESSAGES","surfaces","revision","signal","disposed","published","dispatch","actionQueue","actionSubscription","action","errorSubscription","_options$onError","error","stopProjection","effect","getValue","views","id","active","projected","projectA2uiSurface","attach","surface","NodeResolver","detach","surfaceId","envelope","readA2uiEnvelope","message","batchWrite","previous","rollback","type","setValue","peekValue","_surfaces$get","operation","event","binding","messages","execution"],"sources":["../src/session.ts"],"sourcesContent":["import type { ComponentApi } from '@a2ui/web_core/catalog'\nimport type { SurfaceModel } from '@a2ui/web_core/state'\nimport type { A2uiNodeOperation, A2uiProjectedSurface } from './projection'\nimport type { A2uiActionMessage, A2uiInteraction, A2uiSession, A2uiSessionOptions, A2uiSurfaceView } from './types'\nimport { MessageProcessor, STRICT_VALIDATION } from '@a2ui/web_core/processing'\nimport { batchWrite, effect, getValue, peekValue, setValue, signal } from '@a2ui/web_core/reactivity'\nimport { NodeResolver } from '@a2ui/web_core/resolution'\nimport { createA2uiCatalog } from './catalog'\nimport { A2UI_STATE_MESSAGES, readA2uiEnvelope } from './envelope'\nimport { projectA2uiSurface } from './projection'\n\nfunction clonePayload<T>(payload: T): T {\n return JSON.parse(JSON.stringify(payload)) as T\n}\n\nfunction cloneDataModelValue(value: unknown): unknown {\n if (Array.isArray(value)) {\n const result: unknown[] = []\n result.length = value.length\n for (let index = 0; index < value.length; index++) {\n if (index in value) {\n result[index] = cloneDataModelValue(value[index])\n }\n }\n return result\n }\n if (value !== null && typeof value === 'object') {\n const result: Record<string, unknown> = {}\n for (const [key, item] of Object.entries(value)) {\n Object.defineProperty(result, key, {\n value: cloneDataModelValue(item),\n enumerable: true,\n configurable: true,\n writable: true,\n })\n }\n return result\n }\n return value\n}\n\nfunction selectOperation<T>(operations: ReadonlyMap<string, T>, name: string | undefined, kind: 'binding' | 'action', nodeId: string): T | undefined {\n if (name !== undefined) {\n const selected = operations.get(name)\n if (!selected) {\n throw new TypeError(`节点 '${nodeId}' 不存在名为 '${name}' 的${kind === 'binding' ? '输入绑定' : '动作'}。`)\n }\n return selected\n }\n if (operations.size === 1) {\n return operations.values().next().value\n }\n if (operations.size > 1) {\n throw new TypeError(`节点 '${nodeId}' 有多个${kind === 'binding' ? '输入绑定' : '动作'},请指定${kind === 'binding' ? ' bindingName' : ' actionName'}。`)\n }\n return undefined\n}\n\ninterface ResolvedSurface<T extends ComponentApi> {\n readonly resolver: NodeResolver<T>\n generation: number\n operations: Map<string, A2uiNodeOperation>\n}\n\n/**\n * 创建与页面生命周期解耦的 A2UI 渲染会话。\n *\n * 会话只处理 surface 状态消息,不接管 transport、agent 或 RPC;用户交互通过\n * `interact` 进入,动作经 `onAction` 交给调用方发送给 agent。\n */\nexport function createA2uiSession(options: A2uiSessionOptions): A2uiSession {\n const { catalog } = createA2uiCatalog(options.catalog, options.functionImplementations)\n const processor = new MessageProcessor([catalog], undefined, {\n validationConfig: { ...STRICT_VALIDATION, targetVersion: '1.0', allowedMessages: [...A2UI_STATE_MESSAGES] },\n })\n const surfaces = new Map<string, ResolvedSurface<ComponentApi>>()\n const revision = signal(0)\n let disposed = false\n let published = false\n let dispatch: A2uiActionMessage[] | undefined\n let actionQueue: Promise<void> = Promise.resolve()\n const actionSubscription = processor.model.onAction.subscribe((action) => {\n if (!disposed && dispatch) {\n dispatch.push({ version: '1.0', action: clonePayload(action) })\n }\n })\n const errorSubscription = processor.model.onError.subscribe((error) => {\n if (!disposed) {\n return options.onError?.(clonePayload(error))\n }\n })\n const stopProjection = effect(() => {\n getValue(revision)\n const views: A2uiSurfaceView[] = []\n for (const [id, active] of surfaces) {\n const projected: A2uiProjectedSurface = projectA2uiSurface(getValue(active.resolver.rootNode), active.generation)\n active.operations = projected.operations\n if (projected.root) {\n views.push({ id, root: projected.root })\n }\n }\n if (disposed || (!published && views.length === 0)) {\n return\n }\n published = views.length > 0\n options.onSurfaces(views)\n })\n\n function attach(surface: SurfaceModel<ComponentApi>) {\n surfaces.set(surface.id, {\n resolver: new NodeResolver(surface) as unknown as NodeResolver<ComponentApi>,\n generation: 0,\n operations: new Map(),\n })\n }\n\n function detach(surfaceId: string) {\n const active = surfaces.get(surfaceId)\n if (!active) {\n return\n }\n surfaces.delete(surfaceId)\n active.resolver.dispose()\n active.operations.clear()\n }\n\n return {\n receive(message) {\n if (disposed) {\n return\n }\n const envelope = readA2uiEnvelope(message)\n const { type, surfaceId } = envelope\n batchWrite(() => {\n const previous = surfaceId === undefined ? undefined : processor.getSurface(surfaceId)\n const rollback = type === 'updateDataModel' && previous\n ? { surface: previous, data: cloneDataModelValue(previous.dataModel.get('/')) }\n : undefined\n try {\n processor.processMessages(envelope.message as Parameters<typeof processor.processMessages>[0])\n }\n catch (error) {\n if (rollback) {\n rollback.surface.dataModel.set('/', rollback.data)\n }\n // Core 会先注册内联 createSurface,再校验其组件批次。\n if (type === 'createSurface' && !previous && surfaceId !== undefined) {\n processor.model.deleteSurface(surfaceId)\n }\n throw error\n }\n if (surfaceId === undefined) {\n return\n }\n if (type === 'createSurface') {\n const surface = processor.getSurface(surfaceId)\n if (surface) {\n attach(surface)\n }\n }\n else if (type === 'deleteSurface') {\n detach(surfaceId)\n }\n else if (envelope.replacesBindings) {\n const active = surfaces.get(surfaceId)\n if (active) {\n active.generation += 1\n active.operations.clear()\n }\n }\n if (type !== 'updateDataModel' || envelope.replacesBindings) {\n setValue(revision, peekValue(revision) + 1)\n }\n })\n },\n async interact(surfaceId, event: A2uiInteraction) {\n if (disposed) {\n return\n }\n const operation = surfaces.get(surfaceId)?.operations.get(event.nodeId)\n if (!operation) {\n return\n }\n if (event.type === 'input') {\n const binding = selectOperation(operation.inputs, event.bindingName, 'binding', event.nodeId)\n if (binding && binding.value !== event.value) {\n batchWrite(() => binding.set(event.value))\n }\n return\n }\n const action = selectOperation(operation.actions, event.actionName, 'action', event.nodeId)\n if (!action) {\n return\n }\n // Core 的动作消息没有调用上下文,消息捕获必须串行;handler 内 await interact 会等待当前队列,由调用方避免。\n const messages: A2uiActionMessage[] = []\n const execution = actionQueue.then(async () => {\n if (disposed) {\n return\n }\n dispatch = messages\n try {\n await action()\n }\n finally {\n if (dispatch === messages) {\n dispatch = undefined\n }\n }\n })\n actionQueue = execution.catch(() => {})\n await execution\n for (const message of messages) {\n if (disposed) {\n return\n }\n await options.onAction(message)\n }\n },\n dispose() {\n if (disposed) {\n return\n }\n disposed = true\n stopProjection()\n actionSubscription.unsubscribe()\n errorSubscription.unsubscribe()\n for (const id of surfaces.keys()) {\n detach(id)\n }\n processor.model.dispose()\n processor.dispose()\n },\n }\n}\n"],"mappings":"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"}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ActionPayload } from "@a2ui/web_core/state";
|
|
2
|
+
/** 可安全交给小程序视图层的 JSON 值。 */
|
|
3
|
+
export type A2uiJsonValue = string | number | boolean | null | A2uiJsonValue[] | {
|
|
4
|
+
[key: string]: A2uiJsonValue;
|
|
5
|
+
};
|
|
6
|
+
/** 组件属性值;嵌套对象或数组可以携带投影后的子节点。 */
|
|
7
|
+
export type A2uiPropValue = A2uiJsonValue | A2uiNode | A2uiPropValue[] | {
|
|
8
|
+
[key: string]: A2uiPropValue;
|
|
9
|
+
};
|
|
10
|
+
/** 由协议树投影出的组件无关节点;id 仅在当前 generation 内稳定。 */
|
|
11
|
+
export interface A2uiNode {
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly componentId: string;
|
|
14
|
+
readonly type: string;
|
|
15
|
+
readonly props: {
|
|
16
|
+
readonly [key: string]: A2uiPropValue;
|
|
17
|
+
};
|
|
18
|
+
readonly children: readonly A2uiNode[];
|
|
19
|
+
/** 携带动作的属性名;触发方式由消费方组件决定。 */
|
|
20
|
+
readonly actions: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
export interface A2uiSurfaceView {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly root: A2uiNode;
|
|
25
|
+
}
|
|
26
|
+
export type A2uiInteraction = {
|
|
27
|
+
readonly type: 'input';
|
|
28
|
+
readonly nodeId: string;
|
|
29
|
+
readonly value: A2uiJsonValue;
|
|
30
|
+
readonly bindingName?: string;
|
|
31
|
+
} | {
|
|
32
|
+
readonly type: 'action';
|
|
33
|
+
readonly nodeId: string;
|
|
34
|
+
readonly actionName?: string;
|
|
35
|
+
};
|
|
36
|
+
export interface A2uiActionMessage {
|
|
37
|
+
readonly version: '1.0';
|
|
38
|
+
readonly action: ActionPayload;
|
|
39
|
+
}
|
|
40
|
+
/** Core 通过 surface 事件上报的运行时或消息处理错误。 */
|
|
41
|
+
export interface A2uiErrorMessage {
|
|
42
|
+
readonly code: string;
|
|
43
|
+
readonly message: string;
|
|
44
|
+
readonly surfaceId?: string;
|
|
45
|
+
readonly functionCallId?: string;
|
|
46
|
+
readonly expression?: string;
|
|
47
|
+
readonly details?: Record<string, unknown>;
|
|
48
|
+
readonly [key: string]: unknown;
|
|
49
|
+
}
|
|
50
|
+
export interface A2uiSessionListeners {
|
|
51
|
+
onSurfaces: (views: A2uiSurfaceView[]) => void;
|
|
52
|
+
onAction: (message: A2uiActionMessage) => void | Promise<void>;
|
|
53
|
+
onError?: (error: A2uiErrorMessage) => void | Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
export type A2uiFunctionHandler = (args: Record<string, unknown>) => unknown | Promise<unknown>;
|
|
56
|
+
export interface A2uiSessionOptions extends A2uiSessionListeners {
|
|
57
|
+
/** A2UI 1.0 catalog 文档;组件与函数由消费方声明。 */
|
|
58
|
+
catalog: Record<string, any>;
|
|
59
|
+
/** 可选 catalog 函数实现;未提供的已声明函数在调用时通过 onError 报错。 */
|
|
60
|
+
functionImplementations?: Readonly<Record<string, A2uiFunctionHandler>>;
|
|
61
|
+
}
|
|
62
|
+
export interface A2uiSession {
|
|
63
|
+
receive: (message: unknown) => void;
|
|
64
|
+
interact: (surfaceId: string, event: A2uiInteraction) => Promise<void>;
|
|
65
|
+
dispose: () => void;
|
|
66
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wevu/a2ui",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0-alpha.0",
|
|
5
|
+
"description": "A2UI v1.0 的 Wevu 小程序渲染适配层",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/weapp-vite/weapp-vite#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/weapp-vite/weapp-vite.git",
|
|
11
|
+
"directory": "packages-runtime/a2ui"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/weapp-vite/weapp-vite/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"wevu",
|
|
18
|
+
"a2ui",
|
|
19
|
+
"mini-program",
|
|
20
|
+
"generative-ui",
|
|
21
|
+
"agent"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"import": "./dist/index.mjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/index.mjs",
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"files": [
|
|
33
|
+
"README.md",
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": "^22.23.4 || ^24.22.1 || >=26.12.1"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"wevu": "7.12.2"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@a2ui/web_core": "0.16.1",
|
|
44
|
+
"@wevu/web-apis": "1.5.4"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"miniprogram-api-typings": "^5.3.1",
|
|
48
|
+
"wevu": "7.12.2"
|
|
49
|
+
},
|
|
50
|
+
"tsd": {
|
|
51
|
+
"compilerOptions": {
|
|
52
|
+
"module": "NodeNext",
|
|
53
|
+
"moduleResolution": "NodeNext",
|
|
54
|
+
"paths": {}
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public",
|
|
59
|
+
"registry": "https://registry.npmjs.org/",
|
|
60
|
+
"tag": "alpha"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"dev": "tsdown -w",
|
|
64
|
+
"build": "tsdown",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"test:types": "pnpm build && pnpm run test:types:check && pnpm run test:types:consumer",
|
|
67
|
+
"test:types:check": "tsd --files \"test-d/**/*.test-d.ts\"",
|
|
68
|
+
"test:types:consumer": "tsc -p test-d/tsconfig.strict.json",
|
|
69
|
+
"typecheck": "tsc --noEmit",
|
|
70
|
+
"lint": "eslint .",
|
|
71
|
+
"lint:fix": "eslint . --fix"
|
|
72
|
+
}
|
|
73
|
+
}
|