@tanzerfe/page-use 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 TanzerFE Team
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,182 @@
1
+ # @tanzerfe/page-use
2
+
3
+ 基于 [page-agent](https://github.com/alibaba/page-agent) 的页面操作智能体:接收自然语言指令,在当前页面中模拟点击与输入完成操作,并返回结论。
4
+
5
+ - **框架无关**:核心不依赖 Vue / React,UI 为 Lit Web Component,在 Shadow DOM 中与宿主样式隔离。
6
+ - **可供其它智能体调用**:支持模块 API、function calling 工具、结构化指令块与全局对象 `window.__TZ_PAGE_USE__`。
7
+ - **Vue 适配层**:`@tanzerfe/page-use/vue` 提供 `PageUseHost` 组件。
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ pnpm add @tanzerfe/page-use zod
13
+ ```
14
+
15
+ `zod` 为必需的 peer 依赖;`vue` / `vue-router` 仅在使用 `@tanzerfe/page-use/vue` 时需要。
16
+
17
+ ## 快速开始
18
+
19
+ ### 非 Vue 项目
20
+
21
+ ```ts
22
+ import { createPageUse, mountPageUseUI, readLauncherSwitch } from '@tanzerfe/page-use'
23
+
24
+ const handle = createPageUse({
25
+ llm: { baseURL: '/api/llm-proxy/v1', model: 'deepseek-chat' },
26
+ appName: '订单管理系统',
27
+ getMenus: () => myMenus, // [{ label, path, hidden?, children? }]
28
+ routerMode: 'hash', // 或 'history'
29
+ })
30
+
31
+ // 调试面板:URL 参数 ?pageUseDebug=1 / 0 可临时开关,否则使用 fallback
32
+ const unmount = mountPageUseUI({ launcher: readLauncherSwitch(import.meta.env.DEV) })
33
+
34
+ // 登出 / 卸载时
35
+ unmount()
36
+ handle.destroy()
37
+ ```
38
+
39
+ ### Vue 3 项目
40
+
41
+ ```vue
42
+ <template>
43
+ <PageUseHost v-if="options.llm.baseURL" :options="options" :launcher="launcher" />
44
+ </template>
45
+
46
+ <script setup lang="ts">
47
+ import { PageUseHost, readLauncherSwitch, type PageUseOptions } from '@tanzerfe/page-use/vue'
48
+
49
+ const options: PageUseOptions = {
50
+ llm: { baseURL: import.meta.env.VITE_PAGE_USE_BASE_URL },
51
+ appName: '订单管理系统',
52
+ getMenus: () => permissionStore.menus,
53
+ }
54
+ const launcher = readLauncherSwitch(import.meta.env.DEV) && { scenarios: [/* ... */] }
55
+ </script>
56
+ ```
57
+
58
+ `PageUseHost` 自动完成初始化、UI 挂载与卸载销毁,并从 vue-router 推导 `routerMode`、`getCurrentPath`、`resolveRouteName`(显式传入优先)。UI 挂在 `document.body` 下,无需配置 `isCustomElement`。
59
+
60
+ ## 配置 `PageUseOptions`
61
+
62
+ | 选项 | 类型 | 默认 | 说明 |
63
+ |---|---|---|---|
64
+ | `llm.baseURL` | `string` | — | OpenAI 兼容端点(到版本前缀为止),可用相对路径。**为空时能力不可用** |
65
+ | `llm.apiKey` | `string` | 空 | 前端直连时使用,生产环境建议留空 |
66
+ | `llm.model` | `string` | `deepseek-chat` | 需支持 function calling。默认值仅为示例,使用其它端点时应显式指定 |
67
+ | `llm.maxSteps` | `number` | `20` | 单任务最大步数 |
68
+ | `llm.disableNamedToolChoice` | `boolean` | `false` | 模型不支持对象形式 `tool_choice` 时开启 |
69
+ | `llm.extraBody` | `object` | `{}` | 合并进请求体 |
70
+ | `llm.sessionHeader` | `string \| false` | `x-opencode-session` | 会话 ID 请求头,`false` 不发送 |
71
+ | `appName` | `string` | — | 写入系统提示 |
72
+ | `getMenus` | `() => PageUseMenuNode[]` | 空 | 可见菜单树,用于生成站点地图 |
73
+ | `routerMode` | `'hash' \| 'history'` | `'hash'` | 路由模式 |
74
+ | `getCurrentPath` | `() => string` | 从 location 读取 | 当前路由路径,不含 `#` |
75
+ | `resolveRouteName` | `(path) => string` | — | 由路径解析路由名,用于匹配 `routeHints` |
76
+ | `routeHints` | `Record<string, string>` | `{}` | 按页操作提示,key 为路由名或路径(不带前导 `/`) |
77
+ | `layoutHint` | `string` | 通用描述 | 替换系统提示中的「页面结构」段落 |
78
+ | `beforeTask` | `() => void \| Promise<void>` | — | 任务开始前的预处理,如展开侧栏 |
79
+ | `beforeTaskLabel` | `string` | — | `beforeTask` 在设置面板中的显示名 |
80
+ | `storageKeyPrefix` | `string` | `tz-page-use` | 本地存储键前缀 |
81
+ | `pageAgentOptions` | `Partial<PageAgentConfig>` | `{}` | 透传给 page-agent |
82
+ | `exposeGlobal` | `boolean` | `true` | 是否安装 `window.__TZ_PAGE_USE__` |
83
+
84
+ ### 调试面板 `PageUseLauncherConfig`
85
+
86
+ | 选项 | 说明 |
87
+ |---|---|
88
+ | `title` | 标题,默认 `Page Use` |
89
+ | `presets` | 追加到「常用」分组的快捷指令 `{ label, task }[]` |
90
+ | `scenarios` | 「复杂场景」分组的快捷指令 |
91
+ | `menuPresetLimit` | 由菜单生成「打开 XX」的数量上限,默认 6 |
92
+ | `sourceLabels` | 任务来源到展示名的映射,如 `{ assistant: 'AI 助手' }` |
93
+
94
+ ## 对接方式
95
+
96
+ ### 1. 模块 API
97
+
98
+ ```ts
99
+ import { pageUseCapability } from '@tanzerfe/page-use'
100
+
101
+ if (pageUseCapability.isReady()) {
102
+ const result = await pageUseCapability.run('打开订单列表,筛选待发货并查询,告诉我第一条订单的收货地址', {
103
+ source: 'my-agent',
104
+ signal: controller.signal,
105
+ onProgress: (event) => { if (event.type === 'activity') console.log(event.text) },
106
+ onAskUser: async (question) => askInMyChat(question), // 可选
107
+ })
108
+ // result.status: completed | failed | stopped | error;result.summary 为结论
109
+ }
110
+ ```
111
+
112
+ `run()` 不会 reject。任务未能开始时 `status='error'` 并带 `errorCode`:
113
+
114
+ | errorCode | 含义 |
115
+ |---|---|
116
+ | `NOT_CONFIGURED` | 未初始化或未配置 `llm.baseURL` |
117
+ | `NOT_READY` | 已 `destroy` |
118
+ | `BUSY` | 已有任务在执行(同一时刻仅一个任务) |
119
+ | `EMPTY_TASK` | 指令为空 |
120
+ | `AGENT_ERROR` | agent 构造或钩子抛出错误 |
121
+
122
+ - 模型提问时依次交由 `onAskUser`、调试面板处理;两者都不可用时自动回复「无法补充信息」。
123
+ - 进度可订阅 `pageUseStore` 获取,Vue 中使用 `usePageUseState()`。
124
+
125
+ ### 2. LLM function calling
126
+
127
+ ```ts
128
+ import { PAGE_USE_TOOL, PAGE_USE_TOOL_NAME, runPageUseTool } from '@tanzerfe/page-use'
129
+
130
+ // tools: [PAGE_USE_TOOL, ...]
131
+ // 模型返回的 tool_call.name === PAGE_USE_TOOL_NAME 时:
132
+ const { content } = await runPageUseTool(toolCall.function.arguments, { source: 'my-agent' })
133
+ // content 作为 role=tool 的消息回给模型
134
+ ```
135
+
136
+ ### 3. 结构化指令块
137
+
138
+ 模型在回复中输出 `{ "type": "pageAction", "instruction": "..." }`,前端用 `isPageUseCommand(value)` 校验后调用 `pageUseCapability.run(value.instruction)`。
139
+
140
+ ### 4. 全局对象
141
+
142
+ `window.__TZ_PAGE_USE__` 包含 `pageUseCapability` 的全部方法及 `tool`、`runTool`,可用于控制台调试、E2E 脚本和同源外部脚本:
143
+
144
+ ```js
145
+ await window.__TZ_PAGE_USE__.run('打开首页')
146
+ ```
147
+
148
+ ## API Key 安全
149
+
150
+ `llm.apiKey` 会出现在浏览器请求和前端产物中。**生产环境应由网关或代理注入 Key**,前端只传代理地址。
151
+
152
+ 本地开发可用 Vite 代理规避 CORS:
153
+
154
+ ```ts
155
+ // vite.config.ts
156
+ server: {
157
+ proxy: {
158
+ '/__page-use-llm': {
159
+ target: 'https://api.deepseek.com/v1',
160
+ changeOrigin: true,
161
+ rewrite: (p) => p.replace(/^\/__page-use-llm/, ''),
162
+ },
163
+ },
164
+ }
165
+ ```
166
+
167
+ ## 主题
168
+
169
+ 在宿主页面设置 CSS 变量调整配色,例如 `:root { --tz-page-use-primary: #1677ff }`:
170
+
171
+ | 变量 | 默认 | 用途 |
172
+ |---|---|---|
173
+ | `--tz-page-use-primary` | `#0879ff` | 主色:入口、按钮、光晕 |
174
+ | `--tz-page-use-cyan` / `--tz-page-use-indigo` | `#36cfc9` / `#597ef7` | 光晕渐变 |
175
+ | `--tz-page-use-warning` | `#fa8c16` | 光晕误触警示 |
176
+ | `--tz-page-use-success` / `--tz-page-use-error` / `--tz-page-use-warning-text` | — | 面板状态色 |
177
+
178
+ 模拟光标配色见导出常量 `PAGE_USE_MASK_COLORS`。
179
+
180
+ ## License
181
+
182
+ MIT