@zhin.js/agent 0.1.14 → 0.1.16
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/README.md +80 -43
- package/lib/builtin/activate-skill-tool.d.ts +21 -0
- package/lib/builtin/activate-skill-tool.d.ts.map +1 -0
- package/lib/builtin/activate-skill-tool.js +57 -0
- package/lib/builtin/activate-skill-tool.js.map +1 -0
- package/lib/builtin/ask-user-tool.d.ts +28 -0
- package/lib/builtin/ask-user-tool.d.ts.map +1 -0
- package/lib/builtin/ask-user-tool.js +182 -0
- package/lib/builtin/ask-user-tool.js.map +1 -0
- package/lib/builtin/bash-tool.d.ts +23 -0
- package/lib/builtin/bash-tool.d.ts.map +1 -0
- package/lib/builtin/bash-tool.js +64 -0
- package/lib/builtin/bash-tool.js.map +1 -0
- package/lib/builtin/bing-search-html.d.ts +37 -0
- package/lib/builtin/bing-search-html.d.ts.map +1 -0
- package/lib/builtin/bing-search-html.js +116 -0
- package/lib/builtin/bing-search-html.js.map +1 -0
- package/lib/builtin/builtin-base-tool.d.ts +25 -0
- package/lib/builtin/builtin-base-tool.d.ts.map +1 -0
- package/lib/builtin/builtin-base-tool.js +30 -0
- package/lib/builtin/builtin-base-tool.js.map +1 -0
- package/lib/builtin/edit-file-tool.d.ts +13 -0
- package/lib/builtin/edit-file-tool.d.ts.map +1 -0
- package/lib/builtin/edit-file-tool.js +81 -0
- package/lib/builtin/edit-file-tool.js.map +1 -0
- package/lib/builtin/file-edit-quote-utils.d.ts +24 -0
- package/lib/builtin/file-edit-quote-utils.d.ts.map +1 -0
- package/lib/builtin/file-edit-quote-utils.js +81 -0
- package/lib/builtin/file-edit-quote-utils.js.map +1 -0
- package/lib/builtin/glob-tool.d.ts +23 -0
- package/lib/builtin/glob-tool.d.ts.map +1 -0
- package/lib/builtin/glob-tool.js +54 -0
- package/lib/builtin/glob-tool.js.map +1 -0
- package/lib/builtin/grep-tool.d.ts +23 -0
- package/lib/builtin/grep-tool.d.ts.map +1 -0
- package/lib/builtin/grep-tool.js +118 -0
- package/lib/builtin/grep-tool.js.map +1 -0
- package/lib/builtin/install-skill-tool.d.ts +24 -0
- package/lib/builtin/install-skill-tool.d.ts.map +1 -0
- package/lib/builtin/install-skill-tool.js +76 -0
- package/lib/builtin/install-skill-tool.js.map +1 -0
- package/lib/builtin/list-dir-tool.d.ts +13 -0
- package/lib/builtin/list-dir-tool.d.ts.map +1 -0
- package/lib/builtin/list-dir-tool.js +59 -0
- package/lib/builtin/list-dir-tool.js.map +1 -0
- package/lib/builtin/read-file-tool.d.ts +14 -0
- package/lib/builtin/read-file-tool.d.ts.map +1 -0
- package/lib/builtin/read-file-tool.js +77 -0
- package/lib/builtin/read-file-tool.js.map +1 -0
- package/lib/builtin/read-memory-tool.d.ts +14 -0
- package/lib/builtin/read-memory-tool.d.ts.map +1 -0
- package/lib/builtin/read-memory-tool.js +49 -0
- package/lib/builtin/read-memory-tool.js.map +1 -0
- package/lib/builtin/spawn-task-tool.d.ts +20 -0
- package/lib/builtin/spawn-task-tool.d.ts.map +1 -0
- package/lib/builtin/spawn-task-tool.js +57 -0
- package/lib/builtin/spawn-task-tool.js.map +1 -0
- package/lib/builtin/todo-read-tool.d.ts +14 -0
- package/lib/builtin/todo-read-tool.d.ts.map +1 -0
- package/lib/builtin/todo-read-tool.js +56 -0
- package/lib/builtin/todo-read-tool.js.map +1 -0
- package/lib/builtin/todo-write-tool.d.ts +14 -0
- package/lib/builtin/todo-write-tool.d.ts.map +1 -0
- package/lib/builtin/todo-write-tool.js +54 -0
- package/lib/builtin/todo-write-tool.js.map +1 -0
- package/lib/builtin/web-fetch-tool.d.ts +19 -0
- package/lib/builtin/web-fetch-tool.d.ts.map +1 -0
- package/lib/builtin/web-fetch-tool.js +89 -0
- package/lib/builtin/web-fetch-tool.js.map +1 -0
- package/lib/builtin/web-search-locale.d.ts +16 -0
- package/lib/builtin/web-search-locale.d.ts.map +1 -0
- package/lib/builtin/web-search-locale.js +73 -0
- package/lib/builtin/web-search-locale.js.map +1 -0
- package/lib/builtin/web-search-tool.d.ts +20 -0
- package/lib/builtin/web-search-tool.d.ts.map +1 -0
- package/lib/builtin/web-search-tool.js +105 -0
- package/lib/builtin/web-search-tool.js.map +1 -0
- package/lib/builtin/web-tool-utils.d.ts +4 -0
- package/lib/builtin/web-tool-utils.d.ts.map +1 -0
- package/lib/builtin/web-tool-utils.js +4 -0
- package/lib/builtin/web-tool-utils.js.map +1 -0
- package/lib/builtin/write-file-tool.d.ts +13 -0
- package/lib/builtin/write-file-tool.d.ts.map +1 -0
- package/lib/builtin/write-file-tool.js +51 -0
- package/lib/builtin/write-file-tool.js.map +1 -0
- package/lib/builtin/write-memory-tool.d.ts +14 -0
- package/lib/builtin/write-memory-tool.d.ts.map +1 -0
- package/lib/builtin/write-memory-tool.js +50 -0
- package/lib/builtin/write-memory-tool.js.map +1 -0
- package/lib/builtin-tools.d.ts +10 -11
- package/lib/builtin-tools.d.ts.map +1 -1
- package/lib/builtin-tools.js +44 -862
- package/lib/builtin-tools.js.map +1 -1
- package/lib/defaults/tools.d.ts +3 -6
- package/lib/defaults/tools.d.ts.map +1 -1
- package/lib/defaults/tools.js +3 -11
- package/lib/defaults/tools.js.map +1 -1
- package/lib/index.d.ts +25 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +24 -3
- package/lib/index.js.map +1 -1
- package/lib/init/create-zhin-agent.d.ts.map +1 -1
- package/lib/init/create-zhin-agent.js +4 -3
- package/lib/init/create-zhin-agent.js.map +1 -1
- package/lib/init/message-media.d.ts +8 -0
- package/lib/init/message-media.d.ts.map +1 -0
- package/lib/init/message-media.js +75 -0
- package/lib/init/message-media.js.map +1 -0
- package/lib/init/output-renderer.d.ts +3 -0
- package/lib/init/output-renderer.d.ts.map +1 -0
- package/lib/init/output-renderer.js +38 -0
- package/lib/init/output-renderer.js.map +1 -0
- package/lib/init/register-ai-trigger.d.ts +1 -1
- package/lib/init/register-ai-trigger.d.ts.map +1 -1
- package/lib/init/register-ai-trigger.js +35 -159
- package/lib/init/register-ai-trigger.js.map +1 -1
- package/lib/init/register-builtin-tools.d.ts.map +1 -1
- package/lib/init/register-builtin-tools.js +9 -5
- package/lib/init/register-builtin-tools.js.map +1 -1
- package/lib/orchestrator/index.d.ts +2 -0
- package/lib/orchestrator/index.d.ts.map +1 -1
- package/lib/orchestrator/index.js +1 -0
- package/lib/orchestrator/index.js.map +1 -1
- package/lib/orchestrator/resource-registry.d.ts +1 -0
- package/lib/orchestrator/resource-registry.d.ts.map +1 -1
- package/lib/orchestrator/resource-registry.js +6 -0
- package/lib/orchestrator/resource-registry.js.map +1 -1
- package/lib/orchestrator/tool-registry.d.ts +5 -11
- package/lib/orchestrator/tool-registry.d.ts.map +1 -1
- package/lib/orchestrator/tool-registry.js +30 -75
- package/lib/orchestrator/tool-registry.js.map +1 -1
- package/lib/orchestrator/tool-selection.d.ts +39 -0
- package/lib/orchestrator/tool-selection.d.ts.map +1 -0
- package/lib/orchestrator/tool-selection.js +319 -0
- package/lib/orchestrator/tool-selection.js.map +1 -0
- package/lib/orchestrator/types.d.ts +2 -0
- package/lib/orchestrator/types.d.ts.map +1 -1
- package/lib/reserved-tools.d.ts +3 -0
- package/lib/reserved-tools.d.ts.map +1 -0
- package/lib/reserved-tools.js +30 -0
- package/lib/reserved-tools.js.map +1 -0
- package/lib/service.d.ts +9 -5
- package/lib/service.d.ts.map +1 -1
- package/lib/service.js +42 -36
- package/lib/service.js.map +1 -1
- package/lib/subagent.d.ts +6 -0
- package/lib/subagent.d.ts.map +1 -1
- package/lib/subagent.js +33 -15
- package/lib/subagent.js.map +1 -1
- package/lib/task-executor.d.ts +1 -0
- package/lib/task-executor.d.ts.map +1 -1
- package/lib/task-executor.js +15 -8
- package/lib/task-executor.js.map +1 -1
- package/lib/zhin-agent/builtin-tools.d.ts +1 -3
- package/lib/zhin-agent/builtin-tools.d.ts.map +1 -1
- package/lib/zhin-agent/builtin-tools.js +4 -41
- package/lib/zhin-agent/builtin-tools.js.map +1 -1
- package/lib/zhin-agent/config.d.ts +7 -0
- package/lib/zhin-agent/config.d.ts.map +1 -1
- package/lib/zhin-agent/config.js +12 -7
- package/lib/zhin-agent/config.js.map +1 -1
- package/lib/zhin-agent/context-budget.d.ts +27 -0
- package/lib/zhin-agent/context-budget.d.ts.map +1 -0
- package/lib/zhin-agent/context-budget.js +50 -0
- package/lib/zhin-agent/context-budget.js.map +1 -0
- package/lib/zhin-agent/index.d.ts +10 -0
- package/lib/zhin-agent/index.d.ts.map +1 -1
- package/lib/zhin-agent/index.js +120 -84
- package/lib/zhin-agent/index.js.map +1 -1
- package/lib/zhin-agent/model-harness.d.ts +29 -0
- package/lib/zhin-agent/model-harness.d.ts.map +1 -0
- package/lib/zhin-agent/model-harness.js +67 -0
- package/lib/zhin-agent/model-harness.js.map +1 -0
- package/lib/zhin-agent/pre-exec.d.ts +7 -0
- package/lib/zhin-agent/pre-exec.d.ts.map +1 -0
- package/lib/zhin-agent/pre-exec.js +25 -0
- package/lib/zhin-agent/pre-exec.js.map +1 -0
- package/lib/zhin-agent/prompt.d.ts +10 -8
- package/lib/zhin-agent/prompt.d.ts.map +1 -1
- package/lib/zhin-agent/prompt.js +37 -30
- package/lib/zhin-agent/prompt.js.map +1 -1
- package/lib/zhin-agent/text-sanitize.d.ts +8 -0
- package/lib/zhin-agent/text-sanitize.d.ts.map +1 -0
- package/lib/zhin-agent/text-sanitize.js +19 -0
- package/lib/zhin-agent/text-sanitize.js.map +1 -0
- package/lib/zhin-agent/tool-runtime.d.ts +31 -0
- package/lib/zhin-agent/tool-runtime.d.ts.map +1 -0
- package/lib/zhin-agent/tool-runtime.js +49 -0
- package/lib/zhin-agent/tool-runtime.js.map +1 -0
- package/package.json +8 -6
- package/lib/tools.d.ts +0 -45
- package/lib/tools.d.ts.map +0 -1
- package/lib/tools.js +0 -205
- package/lib/tools.js.map +0 -1
- package/lib/zhin-agent/tool-collector.d.ts +0 -22
- package/lib/zhin-agent/tool-collector.d.ts.map +0 -1
- package/lib/zhin-agent/tool-collector.js +0 -225
- package/lib/zhin-agent/tool-collector.js.map +0 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ Zhin AI Agent 组合层:在 `@zhin.js/core` 的类型与 Provider 之上,提
|
|
|
11
11
|
- 🔄 **模型自动降级**:首选模型失败时自动切换到次优模型,支持 Chat / Vision / Agent 三条路径
|
|
12
12
|
- 🛡️ **6 层 Bash 安全**:`ExecPolicy` 纵深防御(危险黑名单、环境变量剥离、wrapper 剥离、复合命令拆分、只读放行、交互式审批)
|
|
13
13
|
- 📂 **文件访问安全**:`FilePolicy` 路径检查、设备路径拦截、命令读写分类
|
|
14
|
-
- 📋 **
|
|
14
|
+
- 📋 **11 段系统提示词**:`PromptBuilder` 结构化 prompt(Identity、System、Discipline、Tasks、Actions、Tools、Communication、Skills、Active Skills、Memory、Bootstrap)
|
|
15
15
|
- 🔌 **框架挂载**:`initAgentModule()` 注册 `ctx.ai`、定时任务、DB 模型等
|
|
16
16
|
- 📦 **上下文与记忆**:`ContextManager`、`ConversationMemory`、`UserProfileStore`
|
|
17
17
|
- ⏰ **跟进与定时**:`FollowUpManager`、`PersistentCronEngine`、cron 工具
|
|
@@ -47,7 +47,7 @@ import {
|
|
|
47
47
|
AIService,
|
|
48
48
|
SessionManager,
|
|
49
49
|
registerAIHook,
|
|
50
|
-
|
|
50
|
+
createBuiltinTools,
|
|
51
51
|
} from 'zhin.js'
|
|
52
52
|
|
|
53
53
|
// 使用 ctx.ai (AIService)
|
|
@@ -82,14 +82,14 @@ const result = await agent.run('你好')
|
|
|
82
82
|
| Agent | `Agent`, `createAgent`, `formatToolTitle` |
|
|
83
83
|
| 模型管理 | `ModelRegistry`, `computeTierScore`, `extractModelRoot`, `AIModelInfo` |
|
|
84
84
|
| 服务与会话 | `AIService`, `SessionManager`, `MemorySessionManager`, `DatabaseSessionManager`, `createMemorySessionManager`, `createDatabaseSessionManager` |
|
|
85
|
-
| ZhinAgent | `ZhinAgent`,以及 config / exec-policy / file-policy / tool-
|
|
85
|
+
| ZhinAgent | `ZhinAgent`,以及 config / exec-policy / file-policy / tool-runtime / prompt / builtin-tools 等子模块 |
|
|
86
86
|
| 安全策略 | `checkExecPolicy`, `applyExecPolicyToTools`, `isDangerousCommand`, `stripEnvVarPrefix`, `stripSafeWrappers`, `splitCompoundCommand`, `extractCommandName`, `ExecPolicyResult`, `checkFileAccess`, `classifyBashCommand`, `isBlockedDevicePath` |
|
|
87
87
|
| 提示词构建 | `buildRichSystemPrompt`, `buildEnhancedPersona`, `buildUserMessageWithHistory`, `buildContextHint` |
|
|
88
88
|
| 上下文与记忆 | `ContextManager`, `createContextManager`, `ConversationMemory`, `UserProfileStore` |
|
|
89
89
|
| 跟进与定时 | `FollowUpManager`, `PersistentCronEngine`, `createCronTools`, `setCronManager`, `getCronManager` |
|
|
90
90
|
| 压缩与 Bootstrap | `compactSession`, `estimateTokens`, `loadBootstrapFiles`, `loadSoulPersona`, `loadToolsGuide`, `loadAgentsMemory` |
|
|
91
91
|
| Hook | `registerAIHook`, `unregisterAIHook`, `triggerAIHook`, `createAIHookEvent` |
|
|
92
|
-
|
|
|
92
|
+
| IM 内置工具工厂 | `createBuiltinTools`、`BuiltinBaseTool`;具体工具见 `src/builtin/*` |
|
|
93
93
|
| 输出与检测 | `parseOutput`, `renderToPlainText`, `renderToSatori`, `detectTone` |
|
|
94
94
|
| 子代理 | `SubagentManager` |
|
|
95
95
|
| 限流 | `RateLimiter` |
|
|
@@ -117,13 +117,25 @@ declare module '@zhin.js/core' {
|
|
|
117
117
|
|
|
118
118
|
### 1. 主 Agent + 子 Agent(内置)
|
|
119
119
|
|
|
120
|
-
框架已提供 **SubagentManager**:主 ZhinAgent 通过工具 `spawn_task` 把复杂/耗时任务派给**后台子 Agent** 异步执行,子 Agent
|
|
120
|
+
框架已提供 **SubagentManager**:主 ZhinAgent 通过工具 `spawn_task` 把复杂/耗时任务派给**后台子 Agent** 异步执行,子 Agent 默认仅用受限工具集(文件、Shell、网络搜索等),完成后通过回调把结果发回主会话。
|
|
121
121
|
|
|
122
122
|
- 主对话不阻塞,用户可继续聊天。
|
|
123
123
|
- 子 Agent 由 `ZhinAgent.initSubagentManager(createTools)` 在 init 时挂好,主 Agent 在回复里提到「后台 / 子任务 / spawn」时会注入 `spawn_task` 工具。
|
|
124
124
|
- 用户说「后台帮我整理这份文档」时,主 Agent 可调用 `spawn_task({ task: '...', label: '...' })`,子任务在后台跑完后再通知用户。
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
无需额外配置即可使用;若需放宽子 Agent 的工具范围,使用 `ai.agent.subagentTools` 显式追加白名单(不会自动继承主会话全部 skill/tool)。
|
|
127
|
+
|
|
128
|
+
## 工具命名策略
|
|
129
|
+
|
|
130
|
+
- 保留/内置工具名(如 `bash`、`read_file`、`spawn_task`)不可被插件或文件化工具覆盖。
|
|
131
|
+
- 非保留工具同名时采用 **后注册覆盖前注册**。
|
|
132
|
+
- 冲突统一以 warn 记录:包含 `name`、`source`、`action`(`ignored`/`overridden`)。
|
|
133
|
+
|
|
134
|
+
## Model harness(第一期)
|
|
135
|
+
|
|
136
|
+
- 默认参数通过 TypeScript 表维护:`src/zhin-agent/model-harness.ts`。
|
|
137
|
+
- 为新 provider/model 增补默认值时,直接在该表新增行并附测试。
|
|
138
|
+
- 第一阶段不引入新的 YAML 配置键;后续若支持 YAML 覆盖,TS 表仍作为默认层。
|
|
127
139
|
|
|
128
140
|
### 2. 用 AIService 创建多个不同配置的 Agent
|
|
129
141
|
|
|
@@ -181,46 +193,71 @@ useContext('ai', async (ai) => {
|
|
|
181
193
|
|
|
182
194
|
```
|
|
183
195
|
src/
|
|
184
|
-
├── index.ts
|
|
185
|
-
|
|
186
|
-
├──
|
|
187
|
-
├──
|
|
188
|
-
├──
|
|
189
|
-
├──
|
|
190
|
-
├──
|
|
191
|
-
├──
|
|
192
|
-
├──
|
|
196
|
+
├── index.ts # 导出 AgentOrchestrator + 五类 Registry + ZhinAgent + init
|
|
197
|
+
│
|
|
198
|
+
├── orchestrator/ # ★ 核心:编排中枢
|
|
199
|
+
│ ├── index.ts # AgentOrchestrator class
|
|
200
|
+
│ ├── types.ts # ResourceScope, ResourceEntry, Skill, SubAgentDef, AIHook, McpServerEntry
|
|
201
|
+
│ ├── resource-registry.ts # ResourceRegistry<T> 基类
|
|
202
|
+
│ ├── tool-registry.ts # ToolRegistry(含 ZhinTool, defineTool, 权限过滤)
|
|
203
|
+
│ ├── skill-registry.ts # SkillRegistry(含搜索)
|
|
204
|
+
│ ├── subagent-registry.ts # SubAgentRegistry
|
|
205
|
+
│ ├── mcp-registry.ts # McpRegistry
|
|
206
|
+
│ └── hook-registry.ts # HookRegistry
|
|
207
|
+
│
|
|
208
|
+
├── mcp-client/ # ★ MCP 客户端
|
|
209
|
+
│ ├── index.ts # McpClientManager
|
|
210
|
+
│ ├── connection.ts # 单连接生命周期
|
|
211
|
+
│ └── bridge.ts # MCP -> AgentTool/Resource/Prompt 桥接
|
|
212
|
+
│
|
|
213
|
+
├── service.ts # AIService(保留,对接 Orchestrator)
|
|
214
|
+
│
|
|
215
|
+
├── zhin-agent/ # 主对话 Agent
|
|
216
|
+
│ ├── index.ts # ZhinAgent(改造:从 Orchestrator 获取资源)
|
|
217
|
+
│ ├── config.ts
|
|
218
|
+
│ ├── prompt.ts
|
|
219
|
+
│ ├── tool-runtime.ts # 运行时工具收集与执行路径规划
|
|
220
|
+
│ └── context-tools.ts # chat_history, user_profile, spawn_task
|
|
221
|
+
│
|
|
222
|
+
├── discovery/ # ★ 文件化资源发现
|
|
223
|
+
│ ├── index.ts
|
|
224
|
+
│ ├── utils.ts
|
|
225
|
+
│ ├── tools.ts # *.tool.md 发现
|
|
226
|
+
│ ├── skills.ts # SKILL.md 发现
|
|
227
|
+
│ └── agents.ts # *.agent.md 发现
|
|
228
|
+
│
|
|
229
|
+
├── security/ # ★ 安全策略
|
|
230
|
+
│ ├── file-policy.ts
|
|
231
|
+
│ └── exec-policy.ts
|
|
232
|
+
│
|
|
233
|
+
├── builtin/ # IM 内置工具(BuiltinBaseTool + 各 *-tool.ts)
|
|
234
|
+
├── builtin-tools.ts # createBuiltinTools() 聚合
|
|
235
|
+
│
|
|
236
|
+
├── defaults/ # ★ 各注册表的默认资源
|
|
237
|
+
│ ├── skills.ts # 默认 common skills
|
|
238
|
+
│ ├── hooks.ts # 默认 common hooks
|
|
239
|
+
│ └── subagents.ts # 默认 subagent 模板
|
|
240
|
+
│
|
|
241
|
+
├── common-adapter-tools.ts # ← 从 core 迁移:群管工具工厂
|
|
193
242
|
├── subagent.ts
|
|
194
|
-
├──
|
|
243
|
+
├── task-executor.ts
|
|
195
244
|
├── cron-engine.ts
|
|
196
|
-
├── compaction.ts
|
|
197
245
|
├── bootstrap.ts
|
|
198
|
-
├──
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
├──
|
|
202
|
-
├──
|
|
203
|
-
├──
|
|
204
|
-
├──
|
|
205
|
-
├──
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
│ ├── register-db-upgrade.ts
|
|
214
|
-
│ ├── register-message-recorder.ts
|
|
215
|
-
│ ├── register-management-tools.ts
|
|
216
|
-
│ └── register-builtin-tools.ts
|
|
217
|
-
└── zhin-agent/ # ZhinAgent 及子模块
|
|
218
|
-
├── index.ts # ZhinAgent 主类
|
|
219
|
-
├── config.ts # 配置与常量(chatModel、visionModel、ModelSizeHint 等)
|
|
220
|
-
├── exec-policy.ts # Bash 执行安全(6 层纵深防御)
|
|
221
|
-
├── tool-collector.ts # 工具收集与过滤
|
|
222
|
-
├── prompt.ts # 系统提示词构建器(10 段结构化架构)
|
|
223
|
-
└── builtin-tools.ts # ZhinAgent 专用内置工具
|
|
246
|
+
├── user-profile.ts
|
|
247
|
+
│
|
|
248
|
+
└── init/
|
|
249
|
+
├── index.ts # initAgentModule
|
|
250
|
+
├── types.ts # declare Plugin.Contexts.agent: AgentOrchestrator
|
|
251
|
+
├── shared-refs.ts
|
|
252
|
+
├── register-orchestrator.ts # ★ 新增:provide(new AgentOrchestrator())
|
|
253
|
+
├── register-db-models.ts
|
|
254
|
+
├── register-ai-service.ts
|
|
255
|
+
├── create-zhin-agent.ts
|
|
256
|
+
├── register-ai-trigger.ts
|
|
257
|
+
├── register-db-upgrade.ts
|
|
258
|
+
├── register-message-recorder.ts
|
|
259
|
+
├── register-management-tools.ts
|
|
260
|
+
└── register-builtin-tools.ts # 改造:通过 Orchestrator 注册默认资源
|
|
224
261
|
```
|
|
225
262
|
|
|
226
263
|
### 构建
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Tool, ToolContext, ToolParametersSchema, ToolResult } from '@zhin.js/core';
|
|
2
|
+
import { BuiltinBaseTool } from './builtin-base-tool.js';
|
|
3
|
+
export interface ActivateSkillToolOptions {
|
|
4
|
+
/** 按名称查找已注册技能的 SKILL.md 绝对路径 */
|
|
5
|
+
skillFileLookup?: (name: string) => string | undefined;
|
|
6
|
+
/** 技能根目录列表(每个根下为 `<skillName>/SKILL.md`) */
|
|
7
|
+
skillDirList: () => string[];
|
|
8
|
+
skillMaxChars: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const ACTIVATE_SKILL_PARAMETERS: ToolParametersSchema;
|
|
11
|
+
export declare class ActivateSkillBuiltinTool extends BuiltinBaseTool {
|
|
12
|
+
private readonly opts;
|
|
13
|
+
readonly name = "activate_skill";
|
|
14
|
+
readonly description = "\u6309\u540D\u79F0\u6FC0\u6D3B\u6280\u80FD\uFF0C\u52A0\u8F7D\u5176\u5B8C\u6574\u6307\u4EE4\u3002\u5F53\u5224\u65AD\u67D0\u4E2A\u6280\u80FD\u4E0E\u7528\u6237\u8BF7\u6C42\u76F8\u5173\u65F6\u4F7F\u7528";
|
|
15
|
+
readonly parameters: ToolParametersSchema<Record<string, any>>;
|
|
16
|
+
readonly kind = "skill";
|
|
17
|
+
constructor(opts: ActivateSkillToolOptions);
|
|
18
|
+
run(args: Record<string, unknown>, _context?: ToolContext): Promise<ToolResult>;
|
|
19
|
+
}
|
|
20
|
+
export declare function createActivateSkillTool(opts: ActivateSkillToolOptions): Tool;
|
|
21
|
+
//# sourceMappingURL=activate-skill-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"activate-skill-tool.d.ts","sourceRoot":"","sources":["../../src/builtin/activate-skill-tool.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,oBAAoB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGzF,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,MAAM,WAAW,wBAAwB;IACvC,gCAAgC;IAChC,eAAe,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;IACvD,4CAA4C;IAC5C,YAAY,EAAE,MAAM,MAAM,EAAE,CAAC;IAC7B,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,eAAO,MAAM,yBAAyB,EAAE,oBAMvC,CAAC;AAEF,qBAAa,wBAAyB,SAAQ,eAAe;IAO/C,OAAO,CAAC,QAAQ,CAAC,IAAI;IANjC,QAAQ,CAAC,IAAI,oBAAoB;IACjC,QAAQ,CAAC,WAAW,4MACkB;IACtC,QAAQ,CAAC,UAAU,4CAA6B;IAChD,QAAQ,CAAC,IAAI,WAAW;gBAEK,IAAI,EAAE,wBAAwB;IAMrD,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;CAwBtF;AAED,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,wBAAwB,GAAG,IAAI,CAE5E"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* activate_skill — 按名称加载技能指令
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from 'node:fs';
|
|
5
|
+
import * as path from 'node:path';
|
|
6
|
+
import { checkSkillDeps, extractSkillInstructions } from '../discovery/skills.js';
|
|
7
|
+
import { errMsg } from '../discovery/utils.js';
|
|
8
|
+
import { BuiltinBaseTool } from './builtin-base-tool.js';
|
|
9
|
+
export const ACTIVATE_SKILL_PARAMETERS = {
|
|
10
|
+
type: 'object',
|
|
11
|
+
properties: {
|
|
12
|
+
name: { type: 'string', description: '技能名称' },
|
|
13
|
+
},
|
|
14
|
+
required: ['name'],
|
|
15
|
+
};
|
|
16
|
+
export class ActivateSkillBuiltinTool extends BuiltinBaseTool {
|
|
17
|
+
opts;
|
|
18
|
+
name = 'activate_skill';
|
|
19
|
+
description = '按名称激活技能,加载其完整指令。当判断某个技能与用户请求相关时使用';
|
|
20
|
+
parameters = ACTIVATE_SKILL_PARAMETERS;
|
|
21
|
+
kind = 'skill';
|
|
22
|
+
constructor(opts) {
|
|
23
|
+
super();
|
|
24
|
+
this.opts = opts;
|
|
25
|
+
this.tags.push('skill', 'activate');
|
|
26
|
+
this.keywords.push('技能', '激活', '启用', '使用', 'skill', 'activate', 'use');
|
|
27
|
+
}
|
|
28
|
+
async run(args, _context) {
|
|
29
|
+
try {
|
|
30
|
+
const name = String(args.name);
|
|
31
|
+
const registeredPath = this.opts.skillFileLookup?.(name);
|
|
32
|
+
if (registeredPath && fs.existsSync(registeredPath)) {
|
|
33
|
+
const fullContent = await fs.promises.readFile(registeredPath, 'utf-8');
|
|
34
|
+
const depWarning = await checkSkillDeps(fullContent);
|
|
35
|
+
const instructions = extractSkillInstructions(name, fullContent, this.opts.skillMaxChars);
|
|
36
|
+
return depWarning ? `${depWarning}\n\n${instructions}` : instructions;
|
|
37
|
+
}
|
|
38
|
+
for (const dir of this.opts.skillDirList()) {
|
|
39
|
+
const skillPath = path.join(dir, name, 'SKILL.md');
|
|
40
|
+
if (fs.existsSync(skillPath)) {
|
|
41
|
+
const fullContent = await fs.promises.readFile(skillPath, 'utf-8');
|
|
42
|
+
const depWarning = await checkSkillDeps(fullContent);
|
|
43
|
+
const instructions = extractSkillInstructions(name, fullContent, this.opts.skillMaxChars);
|
|
44
|
+
return depWarning ? `${depWarning}\n\n${instructions}` : instructions;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return `Skill '${name}' not found. Check skills/ directory.`;
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
return `Error: ${errMsg(e)}`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function createActivateSkillTool(opts) {
|
|
55
|
+
return new ActivateSkillBuiltinTool(opts).toTool();
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=activate-skill-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"activate-skill-tool.js","sourceRoot":"","sources":["../../src/builtin/activate-skill-tool.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,OAAO,EAAE,cAAc,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAClF,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAUzD,MAAM,CAAC,MAAM,yBAAyB,GAAyB;IAC7D,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE;KAC9C;IACD,QAAQ,EAAE,CAAC,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,OAAO,wBAAyB,SAAQ,eAAe;IAO9B;IANpB,IAAI,GAAG,gBAAgB,CAAC;IACxB,WAAW,GAClB,mCAAmC,CAAC;IAC7B,UAAU,GAAG,yBAAyB,CAAC;IACvC,IAAI,GAAG,OAAO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AM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;oBACnE,MAAM,UAAU,GAAG,MAAM,cAAc,CAAC,WAAW,CAAC,CAAC;oBACrD,MAAM,YAAY,GAAG,wBAAwB,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;oBAC1F,OAAO,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,OAAO,YAAY,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC;gBACxE,CAAC;YACH,CAAC;YACD,OAAO,UAAU,IAAI,uCAAuC,CAAC;QAC/D,CAAC;QAAC,OAAO,CAAU,EAAE,CAAC;YACpB,OAAO,UAAU,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;QAC/B,CAAC;IACH,CAAC;CACF;AAED,MAAM,UAAU,uBAAuB,CAAC,IAA8B;IACpE,OAAO,IAAI,wBAAwB,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ask_user — 基于 Prompt / 私聊 向 Bot Owner 确认或提问
|
|
3
|
+
*/
|
|
4
|
+
import { type Plugin, type Tool, type ToolContext, type ToolParametersSchema, type ToolResult } from '@zhin.js/core';
|
|
5
|
+
import { BuiltinBaseTool } from './builtin-base-tool.js';
|
|
6
|
+
/**
|
|
7
|
+
* 私聊 Owner 场景:使用 Prompt 类直接交互(原有行为)
|
|
8
|
+
*/
|
|
9
|
+
export declare function askViaPrompt(plugin: Plugin, message: any, args: Record<string, any>, questionType: string, timeoutMs: number): Promise<string>;
|
|
10
|
+
/**
|
|
11
|
+
* 将 Owner 私聊回复格式化为对应类型的结果
|
|
12
|
+
*/
|
|
13
|
+
export declare function formatOwnerResponse(raw: string, questionType: string, args: Record<string, any>): string;
|
|
14
|
+
export declare const ASK_USER_PARAMETERS: ToolParametersSchema;
|
|
15
|
+
/**
|
|
16
|
+
* 工厂:`createAskUserTool(plugin)` 或 `createAskUserTool({ plugin })`
|
|
17
|
+
*/
|
|
18
|
+
export declare function createAskUserTool(plugin: Plugin): Tool;
|
|
19
|
+
export declare class AskUserBuiltinTool extends BuiltinBaseTool {
|
|
20
|
+
private readonly plugin;
|
|
21
|
+
readonly name = "ask_user";
|
|
22
|
+
readonly description = "\u5411 Bot Owner \u53D1\u9001\u95EE\u9898\u5E76\u7B49\u5F85\u56DE\u590D\u3002\u7528\u4E8E\u9700\u8981\u786E\u8BA4\u3001\u8865\u5145\u4FE1\u606F\u6216\u505A\u51FA\u9009\u62E9\u65F6\u3002\u5728\u7FA4\u804A\u4E2D\u59CB\u7EC8\u901A\u8FC7\u79C1\u804A\u5411 Owner \u786E\u8BA4\uFF0C\u786E\u4FDD\u5B89\u5168\u6027\u3002";
|
|
23
|
+
readonly parameters: ToolParametersSchema<Record<string, any>>;
|
|
24
|
+
readonly kind = "interaction";
|
|
25
|
+
constructor(plugin: Plugin);
|
|
26
|
+
run(args: Record<string, unknown>, context?: ToolContext): Promise<ToolResult>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=ask-user-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ask-user-tool.d.ts","sourceRoot":"","sources":["../../src/builtin/ask-user-tool.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAGL,KAAK,MAAM,EAGX,KAAK,IAAI,EACT,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,UAAU,EAChB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAMzD;;GAEG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,GAAG,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzB,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CAkCjB;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAgBxG;AAED,eAAO,MAAM,mBAAmB,EAAE,oBAgBjC,CAAC;AAEF;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAEtD;AAED,qBAAa,kBAAmB,SAAQ,eAAe;IAOzC,OAAO,CAAC,QAAQ,CAAC,MAAM;IANnC,QAAQ,CAAC,IAAI,cAAc;IAC3B,QAAQ,CAAC,WAAW,8TACoD;IACxE,QAAQ,CAAC,UAAU,4CAAuB;IAC1C,QAAQ,CAAC,IAAI,iBAAiB;gBAED,MAAM,EAAE,MAAM;IAgBrC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;CA6ErF"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ask_user — 基于 Prompt / 私聊 向 Bot Owner 确认或提问
|
|
3
|
+
*/
|
|
4
|
+
import { Prompt, } from '@zhin.js/core';
|
|
5
|
+
import { errMsg } from '../discovery/utils.js';
|
|
6
|
+
import { BuiltinBaseTool } from './builtin-base-tool.js';
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// Prompt / Owner 回复格式化(原 builtin-tools 顶部辅助函数)
|
|
9
|
+
// ============================================================================
|
|
10
|
+
/**
|
|
11
|
+
* 私聊 Owner 场景:使用 Prompt 类直接交互(原有行为)
|
|
12
|
+
*/
|
|
13
|
+
export async function askViaPrompt(plugin, message, args, questionType, timeoutMs) {
|
|
14
|
+
const prompt = new Prompt(plugin, message);
|
|
15
|
+
try {
|
|
16
|
+
switch (questionType) {
|
|
17
|
+
case 'number': {
|
|
18
|
+
const defaultNum = args.default_value != null ? Number(args.default_value) : undefined;
|
|
19
|
+
const result = await prompt.number(args.question, timeoutMs, defaultNum, '输入超时,已取消');
|
|
20
|
+
return String(result);
|
|
21
|
+
}
|
|
22
|
+
case 'confirm': {
|
|
23
|
+
const result = await prompt.confirm(args.question, 'yes', timeoutMs, false, '确认超时,已取消');
|
|
24
|
+
return result ? 'yes' : 'no';
|
|
25
|
+
}
|
|
26
|
+
case 'pick': {
|
|
27
|
+
if (!args.options?.length) {
|
|
28
|
+
return 'Error: type=pick 时必须提供 options 选项列表';
|
|
29
|
+
}
|
|
30
|
+
const pickOptions = args.options.map((o) => ({ label: o, value: o }));
|
|
31
|
+
const result = await prompt.pick(args.question, {
|
|
32
|
+
type: 'text',
|
|
33
|
+
options: pickOptions,
|
|
34
|
+
timeout: timeoutMs,
|
|
35
|
+
}, '选择超时,已取消');
|
|
36
|
+
return String(result);
|
|
37
|
+
}
|
|
38
|
+
case 'text':
|
|
39
|
+
default: {
|
|
40
|
+
const result = await prompt.text(args.question, timeoutMs, args.default_value || '', '输入超时,已取消');
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
return `Owner 未响应或输入错误: ${errMsg(e)}`;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 将 Owner 私聊回复格式化为对应类型的结果
|
|
51
|
+
*/
|
|
52
|
+
export function formatOwnerResponse(raw, questionType, args) {
|
|
53
|
+
switch (questionType) {
|
|
54
|
+
case 'confirm':
|
|
55
|
+
return raw.trim().toLowerCase() === 'yes' ? 'yes' : 'no';
|
|
56
|
+
case 'number':
|
|
57
|
+
return String(Number(raw) || 0);
|
|
58
|
+
case 'pick': {
|
|
59
|
+
const idx = Number(raw.trim());
|
|
60
|
+
const options = args.options || [];
|
|
61
|
+
if (idx >= 1 && idx <= options.length)
|
|
62
|
+
return options[idx - 1];
|
|
63
|
+
return raw;
|
|
64
|
+
}
|
|
65
|
+
case 'text':
|
|
66
|
+
default:
|
|
67
|
+
return raw;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export const ASK_USER_PARAMETERS = {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
question: { type: 'string', description: '要向 Owner 提出的问题文本' },
|
|
74
|
+
type: {
|
|
75
|
+
type: 'string',
|
|
76
|
+
description: '问题类型: text(文本输入)、number(数字输入)、confirm(是/否确认)、pick(选项选择)。默认 text',
|
|
77
|
+
},
|
|
78
|
+
options: {
|
|
79
|
+
type: 'array',
|
|
80
|
+
description: '选项列表(type=pick 时必填),每项为字符串,如 ["选项A","选项B","选项C"]',
|
|
81
|
+
},
|
|
82
|
+
default_value: { type: 'string', description: 'Owner 超时未回复时使用的默认值' },
|
|
83
|
+
timeout: { type: 'number', description: '等待 Owner 回复的超时时间(秒),默认 120' },
|
|
84
|
+
},
|
|
85
|
+
required: ['question'],
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* 工厂:`createAskUserTool(plugin)` 或 `createAskUserTool({ plugin })`
|
|
89
|
+
*/
|
|
90
|
+
export function createAskUserTool(plugin) {
|
|
91
|
+
return new AskUserBuiltinTool(plugin).toTool();
|
|
92
|
+
}
|
|
93
|
+
export class AskUserBuiltinTool extends BuiltinBaseTool {
|
|
94
|
+
plugin;
|
|
95
|
+
name = 'ask_user';
|
|
96
|
+
description = '向 Bot Owner 发送问题并等待回复。用于需要确认、补充信息或做出选择时。在群聊中始终通过私聊向 Owner 确认,确保安全性。';
|
|
97
|
+
parameters = ASK_USER_PARAMETERS;
|
|
98
|
+
kind = 'interaction';
|
|
99
|
+
constructor(plugin) {
|
|
100
|
+
super();
|
|
101
|
+
this.plugin = plugin;
|
|
102
|
+
this.tags.push('interaction', 'prompt');
|
|
103
|
+
this.keywords.push('询问', '确认', '提问', '用户输入', 'ask', 'confirm', 'prompt', '选择', '请问');
|
|
104
|
+
}
|
|
105
|
+
async run(args, context) {
|
|
106
|
+
if (!context?.message) {
|
|
107
|
+
return 'Error: 当前上下文没有消息来源,无法向 Owner 提问。请改为在回复中直接询问。';
|
|
108
|
+
}
|
|
109
|
+
if (!this.plugin) {
|
|
110
|
+
return 'Error: 插件实例不可用,无法创建交互式提问。请改为在回复中直接询问。';
|
|
111
|
+
}
|
|
112
|
+
const timeoutMs = (args.timeout ?? 120) * 1000;
|
|
113
|
+
const questionType = args.type || 'text';
|
|
114
|
+
const recordArgs = args;
|
|
115
|
+
const platform = context.platform;
|
|
116
|
+
const botId = context.botId;
|
|
117
|
+
const adapter = this.plugin.inject(platform);
|
|
118
|
+
const bot = adapter?.bots?.get(botId);
|
|
119
|
+
const botOwner = bot?.$config?.owner;
|
|
120
|
+
const isPrivateOwner = context.scope === 'private'
|
|
121
|
+
&& botOwner != null && String(context.senderId) === String(botOwner);
|
|
122
|
+
if (isPrivateOwner) {
|
|
123
|
+
return askViaPrompt(this.plugin, context.message, recordArgs, questionType, timeoutMs);
|
|
124
|
+
}
|
|
125
|
+
if (!botOwner) {
|
|
126
|
+
return 'Error: 当前 Bot 未配置 owner,无法进行安全确认。请在 bots 配置中设置 owner 字段。';
|
|
127
|
+
}
|
|
128
|
+
if (!adapter || typeof adapter.sendMessage !== 'function') {
|
|
129
|
+
return `Error: 无法获取适配器 ${platform},无法向 Owner 发送私聊确认。`;
|
|
130
|
+
}
|
|
131
|
+
const sourceInfo = context.scope !== 'private'
|
|
132
|
+
? `来源: ${context.scope}(${context.sceneId}) 用户: ${context.senderId}`
|
|
133
|
+
: `来源: 私聊 用户: ${context.senderId}`;
|
|
134
|
+
let questionText = `🔐 AI 安全确认\n${sourceInfo}\n\n${args.question}`;
|
|
135
|
+
if (questionType === 'confirm') {
|
|
136
|
+
questionText += '\n输入"yes"以确认';
|
|
137
|
+
}
|
|
138
|
+
else if (questionType === 'pick' && args.options?.length) {
|
|
139
|
+
questionText += '\n' + args.options.map((o, i) => `${i + 1}.${o}`).join('\n');
|
|
140
|
+
}
|
|
141
|
+
else if (questionType === 'number') {
|
|
142
|
+
questionText += '\n(请输入数字)';
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
await adapter.sendMessage({
|
|
146
|
+
context: platform,
|
|
147
|
+
bot: botId,
|
|
148
|
+
id: botOwner,
|
|
149
|
+
type: 'private',
|
|
150
|
+
content: questionText,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
return `Error: 无法向 Owner 发送私聊消息: ${errMsg(e)}`;
|
|
155
|
+
}
|
|
156
|
+
return new Promise((resolve) => {
|
|
157
|
+
const middleware = async (message, next) => {
|
|
158
|
+
if (message.$channel?.type !== 'private')
|
|
159
|
+
return next();
|
|
160
|
+
if (String(message.$sender.id) !== String(botOwner))
|
|
161
|
+
return next();
|
|
162
|
+
if (String(message.$bot) !== String(botId))
|
|
163
|
+
return next();
|
|
164
|
+
dispose();
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
const raw = message.$raw;
|
|
167
|
+
resolve(formatOwnerResponse(raw, questionType, recordArgs));
|
|
168
|
+
};
|
|
169
|
+
const dispose = this.plugin.addMiddleware(middleware);
|
|
170
|
+
const timer = setTimeout(() => {
|
|
171
|
+
dispose();
|
|
172
|
+
if (args.default_value != null) {
|
|
173
|
+
resolve(String(args.default_value));
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
resolve('Owner 未在规定时间内响应,操作已取消。');
|
|
177
|
+
}
|
|
178
|
+
}, timeoutMs);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=ask-user-tool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ask-user-tool.js","sourceRoot":"","sources":["../../src/builtin/ask-user-tool.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EACL,MAAM,GASP,MAAM,eAAe,CAAC;AACvB,OAAO,EA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EAAE,IAAI,EAAE,EAAE;gBAC5D,IAAI,OAAO,CAAC,QAAQ,EAAE,IAAI,KAAK,SAAS;oBAAE,OAAO,IAAI,EAAE,CAAC;gBACxD,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,MAAM,CAAC,QAAQ,CAAC;oBAAE,OAAO,IAAI,EAAE,CAAC;gBACnE,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,MAAM,CAAC,KAAK,CAAC;oBAAE,OAAO,IAAI,EAAE,CAAC;gBAC1D,OAAO,EAAE,CAAC;gBACV,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;gBACzB,OAAO,CAAC,mBAAmB,CAAC,GAAG,EAAE,YAAY,EAAE,UAAU,CAAC,CAAC,CAAC;YAC9D,CAAC,CAAC;YACF,MAAM,OAAO,GAAG,IAAI,CAAC,MAAO,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;YACvD,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;gBAC5B,OAAO,EAAE,CAAC;gBACV,IAAI,IAAI,CAAC,aAAa,IAAI,IAAI,EAAE,CAAC;oBAC/B,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC;gBACtC,CAAC;qBAAM,CAAC;oBACN,OAAO,CAAC,wBAAwB,CAAC,CAAC;gBACpC,CAAC;YACH,CAAC,EAAE,SAAS,CAAC,CAAC;QAChB,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bash — Shell 执行(安全检查 + 命令读写分类)
|
|
3
|
+
*/
|
|
4
|
+
import { type ExecOptions } from 'node:child_process';
|
|
5
|
+
import type { Tool, ToolContext, ToolParametersSchema, ToolResult } from '@zhin.js/core';
|
|
6
|
+
import { BuiltinBaseTool } from './builtin-base-tool.js';
|
|
7
|
+
/** 可注入以便单测(默认 `promisify(exec)`) */
|
|
8
|
+
export type BashExecAsync = (command: string, options?: ExecOptions) => Promise<{
|
|
9
|
+
stdout: string;
|
|
10
|
+
stderr: string;
|
|
11
|
+
}>;
|
|
12
|
+
export declare const BASH_PARAMETERS: ToolParametersSchema;
|
|
13
|
+
export declare class BashBuiltinTool extends BuiltinBaseTool {
|
|
14
|
+
private readonly execAsync;
|
|
15
|
+
readonly name = "bash";
|
|
16
|
+
readonly description = "\u6267\u884C Shell \u547D\u4EE4\uFF08\u5E26\u8D85\u65F6\u4FDD\u62A4\u548C\u547D\u4EE4\u5206\u7C7B\uFF09\u3002\u8FD4\u56DE\u7ED3\u679C\u4E2D\u4F1A\u6807\u6CE8\u547D\u4EE4\u7C7B\u578B\uFF08\u53EA\u8BFB/\u641C\u7D22/\u5199\u5165\uFF09\u3002";
|
|
17
|
+
readonly parameters: ToolParametersSchema<Record<string, any>>;
|
|
18
|
+
readonly kind = "shell";
|
|
19
|
+
constructor(execAsync?: BashExecAsync);
|
|
20
|
+
run(args: Record<string, unknown>, _context?: ToolContext): Promise<ToolResult>;
|
|
21
|
+
}
|
|
22
|
+
export declare function createBashTool(): Tool;
|
|
23
|
+
//# sourceMappingURL=bash-tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bash-tool.d.ts","sourceRoot":"","sources":["../../src/builtin/bash-tool.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAQ,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAE5D,OAAO,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,oBAAoB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAMzF,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAIzD,oCAAoC;AACpC,MAAM,MAAM,aAAa,GAAG,CAC1B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,WAAW,KAClB,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAEjD,eAAO,MAAM,eAAe,EAAE,oBAQ7B,CAAC;AAEF,qBAAa,eAAgB,SAAQ,eAAe;IAOtC,OAAO,CAAC,QAAQ,CAAC,SAAS;IANtC,QAAQ,CAAC,IAAI,UAAU;IACvB,QAAQ,CAAC,WAAW,mPACgC;IACpD,QAAQ,CAAC,UAAU,4CAAmB;IACtC,QAAQ,CAAC,IAAI,WAAW;gBAEK,SAAS,GAAE,aAAgC;IAMlE,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;CAyBtF;AAED,wBAAgB,cAAc,IAAI,IAAI,CAErC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bash — Shell 执行(安全检查 + 命令读写分类)
|
|
3
|
+
*/
|
|
4
|
+
import { exec } from 'node:child_process';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
import { checkBashCommandSafety, classifyBashCommand, } from '../security/file-policy.js';
|
|
7
|
+
import { errMsg } from '../discovery/utils.js';
|
|
8
|
+
import { BuiltinBaseTool } from './builtin-base-tool.js';
|
|
9
|
+
const defaultExecAsync = promisify(exec);
|
|
10
|
+
export const BASH_PARAMETERS = {
|
|
11
|
+
type: 'object',
|
|
12
|
+
properties: {
|
|
13
|
+
command: { type: 'string', description: 'Shell 命令' },
|
|
14
|
+
cwd: { type: 'string', description: '工作目录' },
|
|
15
|
+
timeout: { type: 'number', description: '超时毫秒数(默认 30000)' },
|
|
16
|
+
},
|
|
17
|
+
required: ['command'],
|
|
18
|
+
};
|
|
19
|
+
export class BashBuiltinTool extends BuiltinBaseTool {
|
|
20
|
+
execAsync;
|
|
21
|
+
name = 'bash';
|
|
22
|
+
description = '执行 Shell 命令(带超时保护和命令分类)。返回结果中会标注命令类型(只读/搜索/写入)。';
|
|
23
|
+
parameters = BASH_PARAMETERS;
|
|
24
|
+
kind = 'shell';
|
|
25
|
+
constructor(execAsync = defaultExecAsync) {
|
|
26
|
+
super();
|
|
27
|
+
this.execAsync = execAsync;
|
|
28
|
+
this.tags.push('shell', 'exec');
|
|
29
|
+
this.keywords.push('执行', '运行', '命令', '终端', 'shell', 'bash');
|
|
30
|
+
}
|
|
31
|
+
async run(args, _context) {
|
|
32
|
+
try {
|
|
33
|
+
const timeout = args.timeout ?? 30000;
|
|
34
|
+
const cmd = String(args.command || '');
|
|
35
|
+
const safety = checkBashCommandSafety(cmd);
|
|
36
|
+
if (!safety.safe)
|
|
37
|
+
return `Error: ${safety.reason}`;
|
|
38
|
+
const classification = classifyBashCommand(cmd);
|
|
39
|
+
const cwd = args.cwd || process.cwd();
|
|
40
|
+
const { stdout, stderr } = await this.execAsync(cmd, {
|
|
41
|
+
cwd,
|
|
42
|
+
timeout,
|
|
43
|
+
maxBuffer: 1024 * 1024,
|
|
44
|
+
});
|
|
45
|
+
let result = '';
|
|
46
|
+
const tag = classification.isReadOnly
|
|
47
|
+
? (classification.isSearch ? '[搜索]' : classification.isList ? '[列出]' : '[只读]')
|
|
48
|
+
: '[执行]';
|
|
49
|
+
if (stdout.trim())
|
|
50
|
+
result += `STDOUT:\n${stdout.trim()}`;
|
|
51
|
+
if (stderr.trim())
|
|
52
|
+
result += `${result ? '\n' : ''}STDERR:\n${stderr.trim()}`;
|
|
53
|
+
return `${tag} ${result || '(no output)'}`;
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
const err = e;
|
|
57
|
+
return `Error (exit ${err.code || '?'}): ${errMsg(e)}\nSTDOUT:\n${err.stdout || ''}\nSTDERR:\n${err.stderr || ''}`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export function createBashTool() {
|
|
62
|
+
return new BashBuiltinTool().toTool();
|
|
63
|
+
}
|
|
64
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AE,EAAE,CAAC;YAC9E,OAAO,GAAG,GAAG,IAAI,MAAM,IAAI,aAAa,EAAE,CAAC;QAC7C,CAAC;QAAC,OAAO,CAAU,EAAE,CAAC;YACpB,MAAM,GAAG,GAAG,CAA0E,CAAC;YACvF,OAAO,eAAe,GAAG,CAAC,IAAI,IAAI,GAAG,MAAM,MAAM,CAAC,CAAC,CAAC,cAAc,GAAG,CAAC,MAAM,IAAI,EAAE,cAAc,GAAG,CAAC,MAAM,IAAI,EAAE,EAAE,CAAC;QACrH,CAAC;IACH,CAAC;CACF;AAED,MAAM,UAAU,cAAc;IAC5B,OAAO,IAAI,eAAe,EAAE,CAAC,MAAM,EAAE,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface BingSearchResultRow {
|
|
2
|
+
title: string;
|
|
3
|
+
url: string;
|
|
4
|
+
snippet?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const decodeHtmlEntities: (s: string) => string;
|
|
7
|
+
/**
|
|
8
|
+
* 从 Bing 结果页 HTML 提取有机结果。
|
|
9
|
+
* 结果位于 <ol id="b_results"> 内 <li class="b_algo"> 块中。
|
|
10
|
+
*/
|
|
11
|
+
export declare function extractBingResults(html: string): BingSearchResultRow[];
|
|
12
|
+
/**
|
|
13
|
+
* 将 Bing 跳转链接解析为真实 URL(/ck/a?...&u=a1aHR0cHM6Ly9...)
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveBingUrl(rawUrl: string): string | undefined;
|
|
16
|
+
/** @deprecated 单测兼容:请用 {@link bingSearchFetchHeaders};默认语言为 en-US 旧行为已弃用 */
|
|
17
|
+
export declare const BING_SEARCH_BROWSER_HEADERS: {
|
|
18
|
+
readonly 'Accept-Language': "en-US,en;q=0.9";
|
|
19
|
+
readonly 'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0";
|
|
20
|
+
readonly Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8";
|
|
21
|
+
readonly 'Accept-Encoding': "gzip, deflate, br";
|
|
22
|
+
readonly 'Cache-Control': "no-cache";
|
|
23
|
+
readonly Pragma: "no-cache";
|
|
24
|
+
readonly 'Sec-Ch-Ua': "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"";
|
|
25
|
+
readonly 'Sec-Ch-Ua-Mobile': "?0";
|
|
26
|
+
readonly 'Sec-Ch-Ua-Platform': "\"macOS\"";
|
|
27
|
+
readonly 'Sec-Fetch-Dest': "document";
|
|
28
|
+
readonly 'Sec-Fetch-Mode': "navigate";
|
|
29
|
+
readonly 'Sec-Fetch-Site': "none";
|
|
30
|
+
readonly 'Sec-Fetch-User': "?1";
|
|
31
|
+
readonly 'Upgrade-Insecure-Requests': "1";
|
|
32
|
+
};
|
|
33
|
+
export declare function bingSearchFetchHeaders(market: string): Record<string, string>;
|
|
34
|
+
export declare const BING_SEARCH_FETCH_TIMEOUT_MS = 30000;
|
|
35
|
+
export declare function buildBingSearchUrl(query: string, market?: string): string;
|
|
36
|
+
export declare function hostnameMatchesList(hostname: string, domain: string): boolean;
|
|
37
|
+
//# sourceMappingURL=bing-search-html.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bing-search-html.d.ts","sourceRoot":"","sources":["../../src/builtin/bing-search-html.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,mBAAmB;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,eAAO,MAAM,kBAAkB,GAAI,GAAG,MAAM,KAAG,MAAsB,CAAC;AAEtE;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,mBAAmB,EAAE,CAsBtE;AA0BD;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAoBjE;AAqBD,4EAA4E;AAC5E,eAAO,MAAM,2BAA2B;;;;;;;;;;;;;;;CAG9B,CAAC;AAEX,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAK7E;AAED,eAAO,MAAM,4BAA4B,QAAS,CAAC;AAEnD,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,EACb,MAAM,GAAE,MAAkC,GACzC,MAAM,CAGR;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAI7E"}
|