qlogicagent 2.18.11 → 2.19.1

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.
Files changed (37) hide show
  1. package/README.md +382 -382
  2. package/dist/agent-contract.js +1 -0
  3. package/dist/cli.js +1 -1
  4. package/dist/default-project-knowledge/INSTRUCTIONS.md +7 -7
  5. package/dist/default-project-knowledge/rules/project-workflow.md +6 -6
  6. package/dist/host-contract.js +1 -1
  7. package/dist/index.js +340 -340
  8. package/dist/protocol.js +1 -1
  9. package/dist/skills/builtin/desktop-screenshot/SKILL.md +62 -62
  10. package/dist/skills/builtin/office-doc-reading/SKILL.md +19 -19
  11. package/dist/types/cli/acp-extended-handlers.d.ts +6 -0
  12. package/dist/types/cli/handlers/agents-handler.d.ts +5 -0
  13. package/dist/types/host-contract/index.d.ts +2 -1
  14. package/dist/types/orchestration/agent-instance.d.ts +7 -3
  15. package/dist/types/orchestration/agent-roster.d.ts +6 -2
  16. package/dist/types/orchestration/delegation-coordinator.d.ts +2 -2
  17. package/dist/types/protocol/agent-contract.d.ts +44 -0
  18. package/dist/types/protocol/notifications.d.ts +1 -1
  19. package/dist/types/protocol/wire/acp-protocol.d.ts +3 -0
  20. package/dist/types/protocol/wire/agent-events.d.ts +1 -32
  21. package/dist/types/protocol/wire/index.d.ts +1 -1
  22. package/dist/types/protocol/wire/notification-payloads.d.ts +63 -0
  23. package/dist/types/runtime/infra/acp-detector.d.ts +2 -0
  24. package/dist/vendor/hatch-pet/LICENSE.txt +201 -201
  25. package/dist/vendor/hatch-pet/NOTICE.md +25 -25
  26. package/dist/vendor/hatch-pet/references/animation-rows.md +29 -29
  27. package/dist/vendor/hatch-pet/references/codex-pet-contract.md +35 -35
  28. package/dist/vendor/hatch-pet/references/qa-rubric.md +66 -66
  29. package/dist/vendor/hatch-pet/scripts/compose_atlas.py +169 -169
  30. package/dist/vendor/hatch-pet/scripts/derive_running_left_from_running_right.py +150 -150
  31. package/dist/vendor/hatch-pet/scripts/extract_strip_frames.py +408 -408
  32. package/dist/vendor/hatch-pet/scripts/inspect_frames.py +256 -256
  33. package/dist/vendor/hatch-pet/scripts/make_contact_sheet.py +96 -96
  34. package/dist/vendor/hatch-pet/scripts/prepare_pet_run.py +834 -834
  35. package/dist/vendor/hatch-pet/scripts/render_animation_previews.py +78 -78
  36. package/dist/vendor/hatch-pet/scripts/validate_atlas.py +157 -157
  37. package/package.json +9 -1
package/README.md CHANGED
@@ -1,382 +1,382 @@
1
- # qlogicagent
2
-
3
- ## Community Red-Team Reports
4
-
5
- Generate the community desensitization red-team report from the same detectors used by local community flows:
6
-
7
- ```bash
8
- pnpm redteam:community-desensitization -- --out=../qlogicagent-hub/reports/community-go-live/red-team/desensitization.json
9
- ```
10
-
11
- The report contains case ids, rule/finding ids, counts, and failures only; it does not store raw emails, secrets, paths, phone numbers, memories, skill text, tool commands, or file paths.
12
-
13
- The generic `skill` meta-tool no longer exposes raw URL installation. New community skills must enter through the v0.15 registry install flow so manifest kind, source tier, risk tier, dependencies, checksum, and local `SKILL.md` rescan all run on the same path.
14
-
15
- Community skill installation also rescans the downloaded `SKILL.md` entry after checksum verification and before moving it into the owner profile. Prompt-injection hits fail closed, leave no installed skill or lifecycle record, and return only a generic scan failure.
16
-
17
- Pet/Soul data is display-only. Runtime load/save sanitization strips fields and Chinese/English text that claim the pet is a desktop agent, execution actor, decision layer, permission subject, or can alter agent tools, permissions, memory, retrieval, planning, security policy, or system prompts.
18
-
19
- 小智 Claw 的本地 AI Agent 运行时。作为独立进程运行,通过 **ACP (Agent Client Protocol) + Native JSON-RPC over stdio** 对外提供 Agent 能力。openclaw 智能网关作为 ACP Client 连接本进程。
20
-
21
- ## 功能概览
22
-
23
- - **44 个内置工具**: 文件操作、Shell 执行、代码编辑、Web 搜索/抓取、图片/视频/音乐生成、MCP 桥接等
24
- - **多 LLM Provider**: OpenAI、Anthropic、DeepSeek 等,通过统一 `LLMTransport` 接口切换
25
- - **编排策略**: failover 降级、指数退避重试、并行工具执行、14+ 上下文压缩策略、多代理团队协作
26
- - **权限系统**: 命令安全分类、规则引擎、用户审批、破坏性命令警告
27
- - **记忆系统**: 进程内分层记忆 — memdir 文件记忆 (L1 项目级,INDEX.md + 主题文件) + 本地 SQLite 语义长期记忆 (L2,本地 embedding,recall-on-demand) + Dream 整合,无外部服务依赖
28
- - **会话持久化**: JSONL 对话转录 + 状态快照 + 会话恢复
29
- - **MCP 协议**: stdio/HTTP 传输、动态工具注入、资源发现
30
- - **插件系统**: 用户级/项目级插件加载、插件 API
31
- - **技能系统**: SKILL.md 发现/调用/管理、自主技能学习
32
- - **Hook 系统**: 30+ 生命周期钩子点,可扩展不侵入核心
33
-
34
- ## 快速开始
35
-
36
- ### 1. 环境准备
37
-
38
- ```bash
39
- # Node.js 22+
40
- node --version # v22.x.x+
41
-
42
- # 安装依赖
43
- pnpm install
44
-
45
- # 构建
46
- pnpm build
47
- ```
48
-
49
- ### 2. 配置环境变量
50
-
51
- ```bash
52
- cp .env.example .env
53
- ```
54
-
55
- 编辑 `.env`:
56
-
57
- ```bash
58
- # 日志级别
59
- LOG_LEVEL=info
60
- ```
61
-
62
- 记忆系统为进程内(memdir 文件记忆 + 本地 SQLite 语义记忆 + 本地 embedding),无需启动任何外部服务,开箱即用。
63
-
64
- ### 3. 运行
65
-
66
- qlogicagent 作为独立进程运行,由 Electron 桌面壳管理生命周期,openclaw 通过 ACP 协议连接:
67
-
68
- ```bash
69
- # 独立启动(开发调试用)
70
- node dist/cli.js
71
-
72
- # 以 ACP Server 模式启动
73
- node dist/cli.js --acp
74
- ```
75
-
76
- 启动后 agent 等待 stdin 接收 JSON-RPC 请求。
77
-
78
- ---
79
-
80
- ## JSON-RPC 协议
81
-
82
- ### 请求方法
83
-
84
- > ⚠️ 方法名权威清单以 `src/cli/rpc-registry.ts`(私有 agent-RPC)+ `src/protocol/wire/acp-protocol.ts`(ACP)为准。一轮对话由 **ACP `session/prompt`** 触发(本仓不存在 `thread.turn`)。
85
-
86
- | 方法 | 说明 | 参数 |
87
- |------|------|------|
88
- | `initialize` | 协议握手 | `{ protocolVersion, host: { name, version } }` |
89
- | `agent.ping` | 心跳检测 | — |
90
- | `session/prompt` (ACP) | 执行一轮对话 | 见下方详细说明 |
91
- | `agent.cancel` | 中止当前 turn | `{ turnId }` |
92
- | `thread.list` | 列出历史会话 | `{ limit? }` |
93
- | `thread.create` | 创建新线程 | `{ id?, title?, cwd? }` |
94
- | `session.resume` | 恢复会话 | `{ sessionId }` |
95
- | `memory.dream` | 触发记忆整合 | — |
96
- | `thread.user_response` | 回复中途提问 / 工具审批 | `{ turnId, response }`(子代理审批走 `agents.approvalResponse`) |
97
-
98
- ### 一轮对话参数(ACP `session/prompt`)
99
-
100
- ```json
101
- {
102
- "jsonrpc": "2.0",
103
- "id": 1,
104
- "method": "session/prompt",
105
- "params": {
106
- "turnId": "turn-001",
107
- "sessionId": "sess-001",
108
- "messages": [
109
- { "role": "user", "content": "帮我分析 src/ 目录结构" }
110
- ],
111
- "tools": [],
112
- "config": {
113
- "provider": "deepseek",
114
- "model": "deepseek-chat",
115
- "apiKey": "sk-...",
116
- "baseUrl": "https://api.deepseek.com",
117
- "maxRounds": 25,
118
- "temperature": 0.7,
119
- "workdir": "/path/to/project",
120
- "mcpServers": {
121
- "filesystem": {
122
- "command": "npx",
123
- "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
124
- }
125
- },
126
- "skillPaths": ["/path/to/skills"],
127
- "systemPrompt": "You are a helpful assistant."
128
- }
129
- }
130
- }
131
- ```
132
-
133
- ### 事件流(Agent → Host)
134
-
135
- | 事件 | 说明 |
136
- |------|------|
137
- | `turn.start` | 轮次开始 |
138
- | `turn.delta` | 文本流式增量 `{ text }` |
139
- | `turn.end` | 轮次结束 `{ content, usage, model, provider }` |
140
- | `turn.error` | 执行错误 `{ error, code }` |
141
- | `turn.tool_call` | 工具调用 `{ callId, name }` |
142
- | `turn.tool_result` | 工具结果 `{ callId, name, ok }` |
143
- | `turn.tool_blocked` | 权限拦截 `{ callId, name, reason }` |
144
- | `turn.skill_instruction` | 技能指令 `{ instruction }` |
145
- | `turn.sidechain_started` | 子代理启动 `{ depth, role }` |
146
- | `turn.sidechain_completed` | 子代理完成 `{ depth, toolCallCount }` |
147
- | `turn.recovery` | 错误恢复 `{ action }` |
148
- | `turn.plan_update` | 计划更新 `{ slug, content }` |
149
- | `tool.approval.request` | 请求审批 `{ approvalId, toolName, arguments, message }` |
150
-
151
- ---
152
-
153
- ## 模块结构
154
-
155
- > LLM 通信不在本仓:Provider 实现来自外部包 `@xiaozhiclaw/provider-core`,仓内仅保留传输端口
156
- > (`runtime/ports/model-transport-contracts.ts`) 与门面 (`cli/provider-core-facade.ts`)。记忆同样进程内,无独立 HTTP 服务。
157
-
158
- ```
159
- src/
160
- ├── cli/ # 组合根:JSON-RPC + ACP 协议 + 全模块装配
161
- │ ├── main.ts # 进程入口
162
- │ ├── stdio-server.ts # StdioServer (JSON-RPC 派发 + 运行时状态容器)
163
- │ ├── rpc-registry.ts # RPC 方法 → handler 注册表
164
- │ ├── handlers/ # 各域 RPC handler (session/memory/pet/agents/...)
165
- │ ├── provider-core-facade.ts # LLMTransport / 媒体客户端门面 (provider-core 边界)
166
- │ └── *-coordinator / *-bootstrap.ts # 会话/记忆/工具/权限/MCP 等装配
167
-
168
- ├── agent/ # Agent 核心:推理 + 工具循环
169
- │ ├── agent.ts # Agent 类 (async generator run() — turn 生命周期薄壳)
170
- │ ├── tool-loop.ts # 工具调用循环状态机 (CC query loop 对齐)
171
- │ ├── tool-loop/ # 循环子阶段 (loop/recovery/single-round/事件 helpers)
172
- │ ├── memory-recall-context.ts # 召回记忆 → 系统提示 (纯函数 + 冲突检测)
173
- │ ├── memory-recall-injection.ts # 召回编排 (hook + 字节封顶 + after_recall)
174
- │ ├── tunable-defaults.ts # 运行参数默认/上限
175
- │ └── types.ts # 核心类型 (ChatMessage, TurnEvent, ToolInvoker, LLMTransport...)
176
-
177
- ├── orchestration/ # 编排策略层(纯函数,零外部依赖)
178
- │ ├── tool-loop/ # tool-loop-state / conversation-repair / tool-choice-policy / tool-schema
179
- │ ├── context/ # context-collapse 等上下文压缩策略
180
- │ ├── subagent/ # agent-registry / fork-subagent (内置代理 + 子代理 fork)
181
- │ ├── workflow/ # 工作流引擎 / 调度 / 预算门控
182
- │ └── ... # 错误分类、重试、团队编排、solo/product 等
183
-
184
- ├── runtime/ # 运行时服务层
185
- │ ├── session/ # session-state / session-persistence / session-memory / locator
186
- │ ├── hooks/ # hook-registry / memory-hooks / 上下文压缩 hooks
187
- │ ├── execution/ # forked-agent / streaming-tool-executor
188
- │ ├── prompt/ # instruction-loader / system-prompt-sections / task-domain
189
- │ ├── infra/ # model-registry / task-runtime / background-tasks / agent-paths
190
- │ ├── ports/ # 端口契约 (memory / model-transport / permission ...)
191
- │ └── config/ # tunable-defaults 等运行时配置
192
-
193
- ├── skills/ # 工具与技能层
194
- │ ├── tools/ # 内置工具 (shell / read / write / edit / search / web / agent / team / mcp ...)
195
- │ ├── memory/ # memdir (文件记忆) + 本地 SQLite store + 本地 embedding + 整合/召回
196
- │ ├── mcp/ # MCP 桥接
197
- │ ├── permissions/ # 权限规则引擎 / 设置 / sandbox overlay
198
- │ ├── plugins/ # 插件系统
199
- │ └── skill-system/ # SKILL.md 发现 / 生命周期 / 校验
200
-
201
- ├── protocol/ # 线协议:wire/ (Gateway RPC 契约) + notifications + methods
202
- ├── transport/ # ACP server + 事件映射
203
- ├── contracts/ # 纯类型契约(hooks 等,零依赖)
204
- └── config/ # CLI 参数解析
205
- ```
206
-
207
- ---
208
-
209
- ## 记忆系统
210
-
211
- qlogicagent 的记忆全部进程内,无外部服务依赖,开箱即用。
212
-
213
- ### 记忆层次
214
-
215
- | 层 | 说明 | 存储 |
216
- |----|------|------|
217
- | **L1 memdir** | 项目级文件记忆,Agent 主动管理 (INDEX.md + 主题文件) | `<project>/.qlogicagent/memory/` |
218
- | **L2 长期记忆** | 语义向量记忆,自动提取 + 语义召回 | 本地 SQLite + 本地 embedding(进程内) |
219
- | **会话转录** | JSONL 对话记录,用于会话恢复 | `~/.qlogicagent/profiles/<owner>/projectData/<projectId>/sessions/<id>/` |
220
-
221
- ### 工作方式
222
-
223
- 无需任何配置或外部服务。Agent 自动:
224
- - 在 `memory.before_recall` hook 预取相关长期记忆(按 category 过滤 + 每轮字节封顶后注入系统提示)
225
- - 通过 `memory` 工具查询 / 写入语义记忆
226
- - 会话过程中自动提取关键信息写入 L2
227
- - 空闲或达到阈值后触发 Dream 整合(4 阶段:Orient → Gather → Consolidate → Prune)
228
-
229
- L1/L2 冲突时不静默合并:召回上下文会标注冲突并提示向用户确认(见 `agent/memory-recall-context.ts`)。
230
-
231
- ---
232
-
233
- ## 配置文件
234
-
235
- ### 环境变量
236
-
237
- | 变量 | 说明 | 默认值 |
238
- |------|------|--------|
239
- | `LOG_LEVEL` | 日志级别 (debug/info/warn/error) | `info` |
240
- | `QLOGICAGENT_HOME` | Agent 数据根目录(覆盖默认 `~/.qlogicagent/`)| `~/.qlogicagent/` |
241
- | `QLOGICAGENT_IDLE_DREAM_MS` | 空闲多久后自动触发 Dream 整合(毫秒)| `300000` (5 min) |
242
- | `QLOGICAGENT_DREAM_COOLDOWN_MS` | 两次 Dream 之间的最短间隔(毫秒)| `1800000` (30 min) |
243
-
244
- #### QLOGICAGENT_HOME
245
-
246
- 设置后,所有 Agent 数据(sessions、memory、settings、plugins 等)存储到指定目录而非 `~/.qlogicagent/`。
247
-
248
- 典型场景:
249
- - **测试隔离**:每个测试用例使用独立 `QLOGICAGENT_HOME` 避免互相污染
250
- - **多实例部署**:同一机器运行多个 agent 实例时各自独立数据目录
251
-
252
- ```bash
253
- # 测试隔离示例
254
- QLOGICAGENT_HOME=/tmp/test-agent-1 node dist/cli.js
255
- ```
256
-
257
- #### Idle Dream(空闲记忆整合)
258
-
259
- qlogicagent 内置 idle dream 机制:当用户空闲超过 `QLOGICAGENT_IDLE_DREAM_MS` 毫秒后,自动触发 Dream 记忆整合流程(Orient → Gather → Consolidate → Prune)。
260
-
261
- 特性:
262
- - **自治管理**:不依赖外部调度器,agent 进程内部自动计时
263
- - **冷却保护**:两次 Dream 之间必须间隔 `QLOGICAGENT_DREAM_COOLDOWN_MS`,防止频繁触发
264
- - **用户活动中止**:Dream 执行中如果收到新的 `session/prompt`,自动中止 Dream
265
- - **最大时长限制**:单次 Dream 最长 120 秒,超时自动终止
266
- - **触发门控**:需满足记忆数据充足等前置条件才会真正执行
267
-
268
- ### 用户级配置(`~/.qlogicagent/`)
269
-
270
- | 路径 | 说明 |
271
- |------|------|
272
- | `~/.qlogicagent/settings.json` | 权限规则、行为模式 |
273
- | `~/.qlogicagent/mcp.json` | 全局 MCP 服务器配置 |
274
- | `~/.qlogicagent/plugins/` | 用户级插件目录 |
275
- | `~/.qlogicagent/INSTRUCTIONS.md` | 用户级指令 |
276
- | `~/.qlogicagent/session-memory.md` | MD 记忆文件 |
277
- | `~/.qlogicagent/profiles/<owner>/projectData/<projectId>/sessions/` | 会话持久化目录(按 projectId 集中,P1 迁移后)|
278
- | `~/.qlogicagent/.credentials.json` | 安全凭据存储 (0o600) |
279
-
280
- ### 项目级配置
281
-
282
- | 路径 | 说明 |
283
- |------|------|
284
- | `.qlogicagent/settings.json` | 项目权限规则 |
285
- | `.qlogicagent/plugins/` | 项目级插件 |
286
- | `.qlogicagent/INSTRUCTIONS.md` | 项目指令 |
287
- | `INSTRUCTIONS.md` | 项目指令 (根目录) |
288
- | `INSTRUCTIONS.local.md` | 本地私有指令 |
289
-
290
- ---
291
-
292
- ## MCP 配置
293
-
294
- ### 全局 MCP(`~/.qlogicagent/mcp.json`)
295
-
296
- ```json
297
- {
298
- "filesystem": {
299
- "command": "npx",
300
- "args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user"]
301
- },
302
- "github": {
303
- "command": "npx",
304
- "args": ["-y", "@modelcontextprotocol/server-github"],
305
- "env": { "GITHUB_TOKEN": "ghp_..." }
306
- }
307
- }
308
- ```
309
-
310
- ### Per-Turn MCP(通过 `session/prompt` config)
311
-
312
- ```json
313
- {
314
- "config": {
315
- "mcpServers": {
316
- "project-db": {
317
- "type": "http",
318
- "url": "http://localhost:3100/mcp"
319
- }
320
- }
321
- }
322
- }
323
- ```
324
-
325
- MCP 工具自动注入到 Agent 工具列表,前缀格式:`mcp__servername__toolname`。
326
-
327
- ---
328
-
329
- ## 权限配置
330
-
331
- ### `~/.qlogicagent/settings.json`
332
-
333
- ```json
334
- {
335
- "permissions": {
336
- "mode": "default",
337
- "defaultBehavior": "ask",
338
- "rules": [
339
- { "pattern": "read", "behavior": "allow" },
340
- { "pattern": "exec", "behavior": "ask" },
341
- { "pattern": "write:**/node_modules/**", "behavior": "deny" }
342
- ]
343
- }
344
- }
345
- ```
346
-
347
- `behavior` 可选值:`allow`(自动允许)、`ask`(请求审批)、`deny`(拒绝)。
348
-
349
- ---
350
-
351
- ## 开发命令
352
-
353
- ```bash
354
- pnpm install # 安装依赖
355
- pnpm build # esbuild 构建 (scripts/build.mjs)
356
- pnpm dev # tsx watch 开发模式
357
- pnpm test # vitest 运行测试
358
- pnpm test:watch # vitest watch 模式
359
- pnpm lint # oxlint 代码检查
360
- pnpm start # 启动 CLI (node dist/cli.js)
361
- ```
362
-
363
- ## 测试
364
-
365
- ```bash
366
- pnpm test # 全部测试
367
- pnpm test -- test/e2e # E2E 协议测试
368
- pnpm test -- --coverage # 覆盖率
369
- ```
370
-
371
- - 单元测试:`test/` — LLM provider、orchestration、tool、permission
372
- - E2E 测试:`test/e2e/` — spawn CLI 进程验证 JSON-RPC 协议
373
-
374
- ## 发布
375
-
376
- ```bash
377
- # 版本已在 package.json 中更新后:
378
- git tag v0.4.0
379
- git push origin master
380
- git push origin v0.4.0
381
- # Gitee 流水线自动: build → test → npm publish → Gitee Release
382
- ```
1
+ # qlogicagent
2
+
3
+ ## Community Red-Team Reports
4
+
5
+ Generate the community desensitization red-team report from the same detectors used by local community flows:
6
+
7
+ ```bash
8
+ pnpm redteam:community-desensitization -- --out=../qlogicagent-hub/reports/community-go-live/red-team/desensitization.json
9
+ ```
10
+
11
+ The report contains case ids, rule/finding ids, counts, and failures only; it does not store raw emails, secrets, paths, phone numbers, memories, skill text, tool commands, or file paths.
12
+
13
+ The generic `skill` meta-tool no longer exposes raw URL installation. New community skills must enter through the v0.15 registry install flow so manifest kind, source tier, risk tier, dependencies, checksum, and local `SKILL.md` rescan all run on the same path.
14
+
15
+ Community skill installation also rescans the downloaded `SKILL.md` entry after checksum verification and before moving it into the owner profile. Prompt-injection hits fail closed, leave no installed skill or lifecycle record, and return only a generic scan failure.
16
+
17
+ Pet/Soul data is display-only. Runtime load/save sanitization strips fields and Chinese/English text that claim the pet is a desktop agent, execution actor, decision layer, permission subject, or can alter agent tools, permissions, memory, retrieval, planning, security policy, or system prompts.
18
+
19
+ 小智 Claw 的本地 AI Agent 运行时。作为独立进程运行,通过 **ACP (Agent Client Protocol) + Native JSON-RPC over stdio** 对外提供 Agent 能力。openclaw 智能网关作为 ACP Client 连接本进程。
20
+
21
+ ## 功能概览
22
+
23
+ - **44 个内置工具**: 文件操作、Shell 执行、代码编辑、Web 搜索/抓取、图片/视频/音乐生成、MCP 桥接等
24
+ - **多 LLM Provider**: OpenAI、Anthropic、DeepSeek 等,通过统一 `LLMTransport` 接口切换
25
+ - **编排策略**: failover 降级、指数退避重试、并行工具执行、14+ 上下文压缩策略、多代理团队协作
26
+ - **权限系统**: 命令安全分类、规则引擎、用户审批、破坏性命令警告
27
+ - **记忆系统**: 进程内分层记忆 — memdir 文件记忆 (L1 项目级,INDEX.md + 主题文件) + 本地 SQLite 语义长期记忆 (L2,本地 embedding,recall-on-demand) + Dream 整合,无外部服务依赖
28
+ - **会话持久化**: JSONL 对话转录 + 状态快照 + 会话恢复
29
+ - **MCP 协议**: stdio/HTTP 传输、动态工具注入、资源发现
30
+ - **插件系统**: 用户级/项目级插件加载、插件 API
31
+ - **技能系统**: SKILL.md 发现/调用/管理、自主技能学习
32
+ - **Hook 系统**: 30+ 生命周期钩子点,可扩展不侵入核心
33
+
34
+ ## 快速开始
35
+
36
+ ### 1. 环境准备
37
+
38
+ ```bash
39
+ # Node.js 22+
40
+ node --version # v22.x.x+
41
+
42
+ # 安装依赖
43
+ pnpm install
44
+
45
+ # 构建
46
+ pnpm build
47
+ ```
48
+
49
+ ### 2. 配置环境变量
50
+
51
+ ```bash
52
+ cp .env.example .env
53
+ ```
54
+
55
+ 编辑 `.env`:
56
+
57
+ ```bash
58
+ # 日志级别
59
+ LOG_LEVEL=info
60
+ ```
61
+
62
+ 记忆系统为进程内(memdir 文件记忆 + 本地 SQLite 语义记忆 + 本地 embedding),无需启动任何外部服务,开箱即用。
63
+
64
+ ### 3. 运行
65
+
66
+ qlogicagent 作为独立进程运行,由 Electron 桌面壳管理生命周期,openclaw 通过 ACP 协议连接:
67
+
68
+ ```bash
69
+ # 独立启动(开发调试用)
70
+ node dist/cli.js
71
+
72
+ # 以 ACP Server 模式启动
73
+ node dist/cli.js --acp
74
+ ```
75
+
76
+ 启动后 agent 等待 stdin 接收 JSON-RPC 请求。
77
+
78
+ ---
79
+
80
+ ## JSON-RPC 协议
81
+
82
+ ### 请求方法
83
+
84
+ > ⚠️ 方法名权威清单以 `src/cli/rpc-registry.ts`(私有 agent-RPC)+ `src/protocol/wire/acp-protocol.ts`(ACP)为准。一轮对话由 **ACP `session/prompt`** 触发(本仓不存在 `thread.turn`)。
85
+
86
+ | 方法 | 说明 | 参数 |
87
+ |------|------|------|
88
+ | `initialize` | 协议握手 | `{ protocolVersion, host: { name, version } }` |
89
+ | `agent.ping` | 心跳检测 | — |
90
+ | `session/prompt` (ACP) | 执行一轮对话 | 见下方详细说明 |
91
+ | `agent.cancel` | 中止当前 turn | `{ turnId }` |
92
+ | `thread.list` | 列出历史会话 | `{ limit? }` |
93
+ | `thread.create` | 创建新线程 | `{ id?, title?, cwd? }` |
94
+ | `session.resume` | 恢复会话 | `{ sessionId }` |
95
+ | `memory.dream` | 触发记忆整合 | — |
96
+ | `thread.user_response` | 回复中途提问 / 工具审批 | `{ turnId, response }`(子代理审批走 `agents.approvalResponse`) |
97
+
98
+ ### 一轮对话参数(ACP `session/prompt`)
99
+
100
+ ```json
101
+ {
102
+ "jsonrpc": "2.0",
103
+ "id": 1,
104
+ "method": "session/prompt",
105
+ "params": {
106
+ "turnId": "turn-001",
107
+ "sessionId": "sess-001",
108
+ "messages": [
109
+ { "role": "user", "content": "帮我分析 src/ 目录结构" }
110
+ ],
111
+ "tools": [],
112
+ "config": {
113
+ "provider": "deepseek",
114
+ "model": "deepseek-chat",
115
+ "apiKey": "sk-...",
116
+ "baseUrl": "https://api.deepseek.com",
117
+ "maxRounds": 25,
118
+ "temperature": 0.7,
119
+ "workdir": "/path/to/project",
120
+ "mcpServers": {
121
+ "filesystem": {
122
+ "command": "npx",
123
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
124
+ }
125
+ },
126
+ "skillPaths": ["/path/to/skills"],
127
+ "systemPrompt": "You are a helpful assistant."
128
+ }
129
+ }
130
+ }
131
+ ```
132
+
133
+ ### 事件流(Agent → Host)
134
+
135
+ | 事件 | 说明 |
136
+ |------|------|
137
+ | `turn.start` | 轮次开始 |
138
+ | `turn.delta` | 文本流式增量 `{ text }` |
139
+ | `turn.end` | 轮次结束 `{ content, usage, model, provider }` |
140
+ | `turn.error` | 执行错误 `{ error, code }` |
141
+ | `turn.tool_call` | 工具调用 `{ callId, name }` |
142
+ | `turn.tool_result` | 工具结果 `{ callId, name, ok }` |
143
+ | `turn.tool_blocked` | 权限拦截 `{ callId, name, reason }` |
144
+ | `turn.skill_instruction` | 技能指令 `{ instruction }` |
145
+ | `turn.sidechain_started` | 子代理启动 `{ depth, role }` |
146
+ | `turn.sidechain_completed` | 子代理完成 `{ depth, toolCallCount }` |
147
+ | `turn.recovery` | 错误恢复 `{ action }` |
148
+ | `turn.plan_update` | 计划更新 `{ slug, content }` |
149
+ | `tool.approval.request` | 请求审批 `{ approvalId, toolName, arguments, message }` |
150
+
151
+ ---
152
+
153
+ ## 模块结构
154
+
155
+ > LLM 通信不在本仓:Provider 实现来自外部包 `@xiaozhiclaw/provider-core`,仓内仅保留传输端口
156
+ > (`runtime/ports/model-transport-contracts.ts`) 与门面 (`cli/provider-core-facade.ts`)。记忆同样进程内,无独立 HTTP 服务。
157
+
158
+ ```
159
+ src/
160
+ ├── cli/ # 组合根:JSON-RPC + ACP 协议 + 全模块装配
161
+ │ ├── main.ts # 进程入口
162
+ │ ├── stdio-server.ts # StdioServer (JSON-RPC 派发 + 运行时状态容器)
163
+ │ ├── rpc-registry.ts # RPC 方法 → handler 注册表
164
+ │ ├── handlers/ # 各域 RPC handler (session/memory/pet/agents/...)
165
+ │ ├── provider-core-facade.ts # LLMTransport / 媒体客户端门面 (provider-core 边界)
166
+ │ └── *-coordinator / *-bootstrap.ts # 会话/记忆/工具/权限/MCP 等装配
167
+
168
+ ├── agent/ # Agent 核心:推理 + 工具循环
169
+ │ ├── agent.ts # Agent 类 (async generator run() — turn 生命周期薄壳)
170
+ │ ├── tool-loop.ts # 工具调用循环状态机 (CC query loop 对齐)
171
+ │ ├── tool-loop/ # 循环子阶段 (loop/recovery/single-round/事件 helpers)
172
+ │ ├── memory-recall-context.ts # 召回记忆 → 系统提示 (纯函数 + 冲突检测)
173
+ │ ├── memory-recall-injection.ts # 召回编排 (hook + 字节封顶 + after_recall)
174
+ │ ├── tunable-defaults.ts # 运行参数默认/上限
175
+ │ └── types.ts # 核心类型 (ChatMessage, TurnEvent, ToolInvoker, LLMTransport...)
176
+
177
+ ├── orchestration/ # 编排策略层(纯函数,零外部依赖)
178
+ │ ├── tool-loop/ # tool-loop-state / conversation-repair / tool-choice-policy / tool-schema
179
+ │ ├── context/ # context-collapse 等上下文压缩策略
180
+ │ ├── subagent/ # agent-registry / fork-subagent (内置代理 + 子代理 fork)
181
+ │ ├── workflow/ # 工作流引擎 / 调度 / 预算门控
182
+ │ └── ... # 错误分类、重试、团队编排、solo/product 等
183
+
184
+ ├── runtime/ # 运行时服务层
185
+ │ ├── session/ # session-state / session-persistence / session-memory / locator
186
+ │ ├── hooks/ # hook-registry / memory-hooks / 上下文压缩 hooks
187
+ │ ├── execution/ # forked-agent / streaming-tool-executor
188
+ │ ├── prompt/ # instruction-loader / system-prompt-sections / task-domain
189
+ │ ├── infra/ # model-registry / task-runtime / background-tasks / agent-paths
190
+ │ ├── ports/ # 端口契约 (memory / model-transport / permission ...)
191
+ │ └── config/ # tunable-defaults 等运行时配置
192
+
193
+ ├── skills/ # 工具与技能层
194
+ │ ├── tools/ # 内置工具 (shell / read / write / edit / search / web / agent / team / mcp ...)
195
+ │ ├── memory/ # memdir (文件记忆) + 本地 SQLite store + 本地 embedding + 整合/召回
196
+ │ ├── mcp/ # MCP 桥接
197
+ │ ├── permissions/ # 权限规则引擎 / 设置 / sandbox overlay
198
+ │ ├── plugins/ # 插件系统
199
+ │ └── skill-system/ # SKILL.md 发现 / 生命周期 / 校验
200
+
201
+ ├── protocol/ # 线协议:wire/ (Gateway RPC 契约) + notifications + methods
202
+ ├── transport/ # ACP server + 事件映射
203
+ ├── contracts/ # 纯类型契约(hooks 等,零依赖)
204
+ └── config/ # CLI 参数解析
205
+ ```
206
+
207
+ ---
208
+
209
+ ## 记忆系统
210
+
211
+ qlogicagent 的记忆全部进程内,无外部服务依赖,开箱即用。
212
+
213
+ ### 记忆层次
214
+
215
+ | 层 | 说明 | 存储 |
216
+ |----|------|------|
217
+ | **L1 memdir** | 项目级文件记忆,Agent 主动管理 (INDEX.md + 主题文件) | `<project>/.qlogicagent/memory/` |
218
+ | **L2 长期记忆** | 语义向量记忆,自动提取 + 语义召回 | 本地 SQLite + 本地 embedding(进程内) |
219
+ | **会话转录** | JSONL 对话记录,用于会话恢复 | `~/.qlogicagent/profiles/<owner>/projectData/<projectId>/sessions/<id>/` |
220
+
221
+ ### 工作方式
222
+
223
+ 无需任何配置或外部服务。Agent 自动:
224
+ - 在 `memory.before_recall` hook 预取相关长期记忆(按 category 过滤 + 每轮字节封顶后注入系统提示)
225
+ - 通过 `memory` 工具查询 / 写入语义记忆
226
+ - 会话过程中自动提取关键信息写入 L2
227
+ - 空闲或达到阈值后触发 Dream 整合(4 阶段:Orient → Gather → Consolidate → Prune)
228
+
229
+ L1/L2 冲突时不静默合并:召回上下文会标注冲突并提示向用户确认(见 `agent/memory-recall-context.ts`)。
230
+
231
+ ---
232
+
233
+ ## 配置文件
234
+
235
+ ### 环境变量
236
+
237
+ | 变量 | 说明 | 默认值 |
238
+ |------|------|--------|
239
+ | `LOG_LEVEL` | 日志级别 (debug/info/warn/error) | `info` |
240
+ | `QLOGICAGENT_HOME` | Agent 数据根目录(覆盖默认 `~/.qlogicagent/`)| `~/.qlogicagent/` |
241
+ | `QLOGICAGENT_IDLE_DREAM_MS` | 空闲多久后自动触发 Dream 整合(毫秒)| `300000` (5 min) |
242
+ | `QLOGICAGENT_DREAM_COOLDOWN_MS` | 两次 Dream 之间的最短间隔(毫秒)| `1800000` (30 min) |
243
+
244
+ #### QLOGICAGENT_HOME
245
+
246
+ 设置后,所有 Agent 数据(sessions、memory、settings、plugins 等)存储到指定目录而非 `~/.qlogicagent/`。
247
+
248
+ 典型场景:
249
+ - **测试隔离**:每个测试用例使用独立 `QLOGICAGENT_HOME` 避免互相污染
250
+ - **多实例部署**:同一机器运行多个 agent 实例时各自独立数据目录
251
+
252
+ ```bash
253
+ # 测试隔离示例
254
+ QLOGICAGENT_HOME=/tmp/test-agent-1 node dist/cli.js
255
+ ```
256
+
257
+ #### Idle Dream(空闲记忆整合)
258
+
259
+ qlogicagent 内置 idle dream 机制:当用户空闲超过 `QLOGICAGENT_IDLE_DREAM_MS` 毫秒后,自动触发 Dream 记忆整合流程(Orient → Gather → Consolidate → Prune)。
260
+
261
+ 特性:
262
+ - **自治管理**:不依赖外部调度器,agent 进程内部自动计时
263
+ - **冷却保护**:两次 Dream 之间必须间隔 `QLOGICAGENT_DREAM_COOLDOWN_MS`,防止频繁触发
264
+ - **用户活动中止**:Dream 执行中如果收到新的 `session/prompt`,自动中止 Dream
265
+ - **最大时长限制**:单次 Dream 最长 120 秒,超时自动终止
266
+ - **触发门控**:需满足记忆数据充足等前置条件才会真正执行
267
+
268
+ ### 用户级配置(`~/.qlogicagent/`)
269
+
270
+ | 路径 | 说明 |
271
+ |------|------|
272
+ | `~/.qlogicagent/settings.json` | 权限规则、行为模式 |
273
+ | `~/.qlogicagent/mcp.json` | 全局 MCP 服务器配置 |
274
+ | `~/.qlogicagent/plugins/` | 用户级插件目录 |
275
+ | `~/.qlogicagent/INSTRUCTIONS.md` | 用户级指令 |
276
+ | `~/.qlogicagent/session-memory.md` | MD 记忆文件 |
277
+ | `~/.qlogicagent/profiles/<owner>/projectData/<projectId>/sessions/` | 会话持久化目录(按 projectId 集中,P1 迁移后)|
278
+ | `~/.qlogicagent/.credentials.json` | 安全凭据存储 (0o600) |
279
+
280
+ ### 项目级配置
281
+
282
+ | 路径 | 说明 |
283
+ |------|------|
284
+ | `.qlogicagent/settings.json` | 项目权限规则 |
285
+ | `.qlogicagent/plugins/` | 项目级插件 |
286
+ | `.qlogicagent/INSTRUCTIONS.md` | 项目指令 |
287
+ | `INSTRUCTIONS.md` | 项目指令 (根目录) |
288
+ | `INSTRUCTIONS.local.md` | 本地私有指令 |
289
+
290
+ ---
291
+
292
+ ## MCP 配置
293
+
294
+ ### 全局 MCP(`~/.qlogicagent/mcp.json`)
295
+
296
+ ```json
297
+ {
298
+ "filesystem": {
299
+ "command": "npx",
300
+ "args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user"]
301
+ },
302
+ "github": {
303
+ "command": "npx",
304
+ "args": ["-y", "@modelcontextprotocol/server-github"],
305
+ "env": { "GITHUB_TOKEN": "ghp_..." }
306
+ }
307
+ }
308
+ ```
309
+
310
+ ### Per-Turn MCP(通过 `session/prompt` config)
311
+
312
+ ```json
313
+ {
314
+ "config": {
315
+ "mcpServers": {
316
+ "project-db": {
317
+ "type": "http",
318
+ "url": "http://localhost:3100/mcp"
319
+ }
320
+ }
321
+ }
322
+ }
323
+ ```
324
+
325
+ MCP 工具自动注入到 Agent 工具列表,前缀格式:`mcp__servername__toolname`。
326
+
327
+ ---
328
+
329
+ ## 权限配置
330
+
331
+ ### `~/.qlogicagent/settings.json`
332
+
333
+ ```json
334
+ {
335
+ "permissions": {
336
+ "mode": "default",
337
+ "defaultBehavior": "ask",
338
+ "rules": [
339
+ { "pattern": "read", "behavior": "allow" },
340
+ { "pattern": "exec", "behavior": "ask" },
341
+ { "pattern": "write:**/node_modules/**", "behavior": "deny" }
342
+ ]
343
+ }
344
+ }
345
+ ```
346
+
347
+ `behavior` 可选值:`allow`(自动允许)、`ask`(请求审批)、`deny`(拒绝)。
348
+
349
+ ---
350
+
351
+ ## 开发命令
352
+
353
+ ```bash
354
+ pnpm install # 安装依赖
355
+ pnpm build # esbuild 构建 (scripts/build.mjs)
356
+ pnpm dev # tsx watch 开发模式
357
+ pnpm test # vitest 运行测试
358
+ pnpm test:watch # vitest watch 模式
359
+ pnpm lint # oxlint 代码检查
360
+ pnpm start # 启动 CLI (node dist/cli.js)
361
+ ```
362
+
363
+ ## 测试
364
+
365
+ ```bash
366
+ pnpm test # 全部测试
367
+ pnpm test -- test/e2e # E2E 协议测试
368
+ pnpm test -- --coverage # 覆盖率
369
+ ```
370
+
371
+ - 单元测试:`test/` — LLM provider、orchestration、tool、permission
372
+ - E2E 测试:`test/e2e/` — spawn CLI 进程验证 JSON-RPC 协议
373
+
374
+ ## 发布
375
+
376
+ ```bash
377
+ # 版本已在 package.json 中更新后:
378
+ git tag v0.4.0
379
+ git push origin master
380
+ git push origin v0.4.0
381
+ # Gitee 流水线自动: build → test → npm publish → Gitee Release
382
+ ```