@oyasmi/pipiclaw 0.9.5 → 0.9.6-beta.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/README.en.md +300 -142
- package/README.md +295 -140
- package/dist/agent/builtin-commands.d.ts +1 -1
- package/dist/agent/builtin-commands.js +1 -1
- package/dist/agent/channel-runner.d.ts +4 -3
- package/dist/agent/channel-runner.js +17 -28
- package/dist/agent/maintenance-context.js +2 -2
- package/dist/agent/prompt/builder.d.ts +1 -1
- package/dist/agent/prompt/builder.js +1 -1
- package/dist/agent/prompt/extension.d.ts +1 -1
- package/dist/agent/prompt/extension.js +1 -1
- package/dist/agent/prompt/manifest.d.ts +6 -1
- package/dist/agent/prompt/manifest.js +12 -5
- package/dist/agent/prompt/resources.d.ts +0 -3
- package/dist/agent/prompt/resources.js +0 -6
- package/dist/agent/prompt/sections.d.ts +2 -2
- package/dist/agent/prompt/sections.js +36 -26
- package/dist/agent/prompt/types.d.ts +3 -2
- package/dist/agent/runner-factory.js +1 -1
- package/dist/agent/session/command-extension.js +3 -1
- package/dist/agent/session/compaction-summary.js +3 -6
- package/dist/agent/session/compaction.d.ts +2 -0
- package/dist/agent/session/compaction.js +22 -14
- package/dist/agent/session/event-guards.d.ts +2 -1
- package/dist/agent/session/event-guards.js +6 -0
- package/dist/agent/session/model-selection.d.ts +2 -1
- package/dist/agent/session/model-selection.js +5 -5
- package/dist/agent/session/session-events.js +31 -4
- package/dist/agent/session/session-host.d.ts +5 -3
- package/dist/agent/session/session-host.js +20 -16
- package/dist/agent/status-render.js +3 -0
- package/dist/agent/stop-watchdog.js +5 -1
- package/dist/agent/turn/task-digest.d.ts +2 -5
- package/dist/agent/turn/task-digest.js +25 -45
- package/dist/agent/turn/turn-context.js +4 -6
- package/dist/agent/turn/turn-error.d.ts +1 -1
- package/dist/agent/turn/turn-error.js +9 -0
- package/dist/agent/turn/turn-pipeline.d.ts +10 -5
- package/dist/agent/turn/turn-pipeline.js +36 -7
- package/dist/agent/turn/turn-prompt.d.ts +2 -0
- package/dist/agent/turn/turn-prompt.js +2 -1
- package/dist/agent/turn/turn-recovery.js +4 -1
- package/dist/agent/turn/turn-state.d.ts +2 -0
- package/dist/agent/turn/turn-state.js +2 -1
- package/dist/agent/types.d.ts +23 -3
- package/dist/agent/types.js +2 -0
- package/dist/app/auth-command.d.ts +25 -0
- package/dist/app/auth-command.js +200 -67
- package/dist/app/home.d.ts +7 -8
- package/dist/app/home.js +143 -56
- package/dist/channel/attachments.d.ts +4 -0
- package/dist/channel/attachments.js +4 -0
- package/dist/channel/channel-context.d.ts +6 -0
- package/dist/channel/config.d.ts +3 -0
- package/dist/channel/config.js +11 -5
- package/dist/channel/store.d.ts +1 -1
- package/dist/commands/catalog.js +42 -10
- package/dist/commands/router.js +5 -1
- package/dist/desk/commands.d.ts +44 -5
- package/dist/desk/commands.js +162 -28
- package/dist/desk/paths.d.ts +6 -0
- package/dist/desk/paths.js +21 -1
- package/dist/desk/registry.d.ts +17 -2
- package/dist/desk/registry.js +122 -12
- package/dist/desk/types.d.ts +83 -1
- package/dist/dingtalk/bot.d.ts +1 -1
- package/dist/dingtalk/bot.js +4 -3
- package/dist/dingtalk/config.js +10 -10
- package/dist/dingtalk/delivery.js +15 -8
- package/dist/dingtalk/inbound-media.d.ts +36 -14
- package/dist/dingtalk/inbound-media.js +71 -12
- package/dist/dingtalk/inbound.d.ts +9 -2
- package/dist/dingtalk/inbound.js +17 -2
- package/dist/dingtalk/media.d.ts +4 -10
- package/dist/dingtalk/media.js +58 -32
- package/dist/events/commands.d.ts +2 -0
- package/dist/events/commands.js +13 -8
- package/dist/events/history.d.ts +9 -0
- package/dist/events/history.js +66 -0
- package/dist/events/parse.js +7 -3
- package/dist/events/types.d.ts +3 -0
- package/dist/events/watcher.d.ts +4 -8
- package/dist/events/watcher.js +53 -58
- package/dist/jobs/kill.d.ts +23 -0
- package/dist/jobs/kill.js +65 -0
- package/dist/jobs/manager.d.ts +19 -18
- package/dist/jobs/manager.js +36 -103
- package/dist/main.js +15 -3
- package/dist/memory/boundary-work.d.ts +2 -1
- package/dist/memory/boundary-work.js +1 -1
- package/dist/memory/lifecycle.d.ts +2 -1
- package/dist/memory/lifecycle.js +2 -2
- package/dist/memory/reflect-job.d.ts +2 -1
- package/dist/memory/reflect-job.js +1 -1
- package/dist/memory/reflect.d.ts +2 -1
- package/dist/memory/reflect.js +25 -4
- package/dist/memory/scheduler.d.ts +2 -1
- package/dist/memory/scheduler.js +1 -1
- package/dist/memory/session-corpus.d.ts +1 -1
- package/dist/memory/session-corpus.js +76 -1
- package/dist/memory/session-search.d.ts +2 -1
- package/dist/memory/session-search.js +14 -12
- package/dist/memory/sidecar-worker.d.ts +2 -1
- package/dist/memory/sidecar-worker.js +2 -4
- package/dist/memory/store.d.ts +2 -0
- package/dist/memory/store.js +19 -9
- package/dist/models/credential-work.d.ts +7 -0
- package/dist/models/credential-work.js +36 -0
- package/dist/models/login-ui.js +4 -4
- package/dist/models/provider-login.js +7 -6
- package/dist/models/requests.d.ts +8 -0
- package/dist/models/requests.js +69 -0
- package/dist/models/runtime.d.ts +3 -1
- package/dist/models/runtime.js +9 -2
- package/dist/playbooks/agent-delegation.md +20 -45
- package/dist/playbooks/background-jobs.md +1 -1
- package/dist/playbooks/catalog.js +1 -1
- package/dist/playbooks/delegation-inline.md +25 -0
- package/dist/playbooks/delegation-recovery.md +28 -0
- package/dist/playbooks/event-scheduling.md +18 -37
- package/dist/playbooks/event-sensors.md +26 -0
- package/dist/playbooks/event-task-template.md +22 -0
- package/dist/playbooks/learning-and-skills.md +26 -0
- package/dist/playbooks/memory-operations.md +36 -0
- package/dist/playbooks/outbound-media.md +2 -2
- package/dist/playbooks/runtime-files.md +29 -0
- package/dist/playbooks/runtime-operations.md +25 -0
- package/dist/playbooks/runtime-orientation.md +18 -53
- package/dist/playbooks/task-lead.md +22 -68
- package/dist/playbooks/task-manage.md +38 -0
- package/dist/playbooks/task-recovery.md +24 -0
- package/dist/playbooks/task-verify.md +22 -0
- package/dist/runtime/bootstrap.d.ts +10 -0
- package/dist/runtime/bootstrap.js +93 -29
- package/dist/runtime/daemon-lock.js +3 -3
- package/dist/runtime/desk-runtime.d.ts +40 -7
- package/dist/runtime/desk-runtime.js +330 -44
- package/dist/runtime/durable-dispatch.d.ts +14 -2
- package/dist/runtime/durable-dispatch.js +207 -65
- package/dist/runtime/ingress-busy.js +17 -8
- package/dist/runtime/ingress-commands.js +24 -4
- package/dist/runtime/ingress-deps.d.ts +4 -1
- package/dist/runtime/ingress.js +6 -1
- package/dist/runtime/request-execution.js +7 -6
- package/dist/runtime/shutdown.d.ts +4 -1
- package/dist/runtime/shutdown.js +88 -35
- package/dist/runtime/task-close.d.ts +1 -1
- package/dist/runtime/task-close.js +6 -3
- package/dist/runtime/task-control.d.ts +2 -3
- package/dist/runtime/task-control.js +38 -11
- package/dist/runtime/task-input.js +1 -1
- package/dist/runtime/task-lane-types.d.ts +12 -2
- package/dist/runtime/task-lane-types.js +9 -0
- package/dist/runtime/task-lane.d.ts +2 -0
- package/dist/runtime/task-lane.js +38 -20
- package/dist/runtime/task-step.d.ts +3 -3
- package/dist/runtime/task-step.js +34 -16
- package/dist/runtime/transports.js +6 -5
- package/dist/runtime/turn-run.js +103 -66
- package/dist/security/command-guard.d.ts +1 -1
- package/dist/security/command-guard.js +259 -314
- package/dist/security/path-guard.js +48 -7
- package/dist/security/shell-parse.d.ts +10 -0
- package/dist/security/shell-parse.js +224 -0
- package/dist/security/types.d.ts +2 -0
- package/dist/subagents/commands.d.ts +1 -1
- package/dist/subagents/commands.js +14 -5
- package/dist/subagents/completion-wake.js +6 -0
- package/dist/subagents/detached-discovery.d.ts +1 -1
- package/dist/subagents/discovery.d.ts +18 -199
- package/dist/subagents/discovery.js +35 -289
- package/dist/subagents/dispatch.d.ts +1 -2
- package/dist/subagents/dispatch.js +50 -29
- package/dist/subagents/external/antigravity-cli.d.ts +2 -0
- package/dist/subagents/external/antigravity-cli.js +517 -0
- package/dist/subagents/external/claude-code.js +8 -17
- package/dist/subagents/external/codex-cli.js +8 -18
- package/dist/subagents/external/cursor-cli.js +34 -31
- package/dist/subagents/external/dispatch.d.ts +21 -7
- package/dist/subagents/external/dispatch.js +146 -12
- package/dist/subagents/external/exec.js +3 -0
- package/dist/subagents/external/harness.d.ts +57 -33
- package/dist/subagents/external/harness.js +69 -40
- package/dist/subagents/external/reconciliation.js +1 -0
- package/dist/subagents/external/registry.js +4 -1
- package/dist/subagents/external/role-warnings.d.ts +4 -13
- package/dist/subagents/external/role-warnings.js +4 -28
- package/dist/subagents/external/run.d.ts +1 -2
- package/dist/subagents/external/run.js +16 -26
- package/dist/subagents/external/settlement.d.ts +2 -0
- package/dist/subagents/external/settlement.js +13 -3
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/internal/outcome.d.ts +1 -2
- package/dist/subagents/internal/outcome.js +19 -12
- package/dist/subagents/internal/provider-retry.d.ts +1 -2
- package/dist/subagents/internal/provider-retry.js +2 -4
- package/dist/subagents/internal/run.d.ts +2 -3
- package/dist/subagents/internal/run.js +27 -4
- package/dist/subagents/internal/state.d.ts +2 -5
- package/dist/subagents/internal/state.js +1 -2
- package/dist/subagents/internal/worker.d.ts +1 -2
- package/dist/subagents/internal/worker.js +20 -10
- package/dist/subagents/resolve.d.ts +56 -0
- package/dist/subagents/resolve.js +202 -0
- package/dist/subagents/result.d.ts +6 -4
- package/dist/subagents/result.js +39 -6
- package/dist/subagents/run-record.d.ts +54 -2
- package/dist/subagents/run-record.js +7 -0
- package/dist/subagents/runs.d.ts +6 -31
- package/dist/subagents/runs.js +26 -31
- package/dist/subagents/task-envelope.d.ts +1 -2
- package/dist/subagents/task-envelope.js +13 -4
- package/dist/subagents/task-settlement.js +1 -0
- package/dist/subagents/types.d.ts +105 -6
- package/dist/subagents/types.js +16 -1
- package/dist/subagents/workspace-lease.d.ts +41 -24
- package/dist/subagents/workspace-lease.js +159 -24
- package/dist/subagents/worktree.d.ts +52 -0
- package/dist/subagents/worktree.js +363 -0
- package/dist/tasks/archive.js +4 -0
- package/dist/tasks/brief.d.ts +5 -1
- package/dist/tasks/brief.js +19 -5
- package/dist/tasks/commands.d.ts +6 -1
- package/dist/tasks/commands.js +32 -7
- package/dist/tasks/control.d.ts +11 -0
- package/dist/tasks/delivery.d.ts +2 -0
- package/dist/tasks/delivery.js +141 -21
- package/dist/tasks/loop-log.d.ts +12 -0
- package/dist/tasks/loop-log.js +10 -1
- package/dist/tasks/receipts.d.ts +7 -3
- package/dist/tasks/receipts.js +34 -5
- package/dist/tools/bash.d.ts +3 -0
- package/dist/tools/bash.js +29 -15
- package/dist/tools/contract/presentation.js +7 -5
- package/dist/tools/edit-diff.d.ts +11 -0
- package/dist/tools/edit-diff.js +148 -0
- package/dist/tools/edit-match.d.ts +18 -0
- package/dist/tools/edit-match.js +242 -0
- package/dist/tools/edit.d.ts +10 -2
- package/dist/tools/edit.js +91 -361
- package/dist/tools/event-manage.d.ts +2 -1
- package/dist/tools/event-manage.js +46 -39
- package/dist/tools/file-observations.d.ts +9 -0
- package/dist/tools/file-observations.js +50 -0
- package/dist/tools/glob-match.d.ts +3 -1
- package/dist/tools/glob-match.js +40 -17
- package/dist/tools/glob.d.ts +1 -0
- package/dist/tools/glob.js +39 -19
- package/dist/tools/grep-command.d.ts +13 -0
- package/dist/tools/grep-command.js +77 -0
- package/dist/tools/grep.js +96 -201
- package/dist/tools/index.d.ts +6 -2
- package/dist/tools/index.js +9 -4
- package/dist/tools/job.js +12 -11
- package/dist/tools/read.d.ts +2 -0
- package/dist/tools/read.js +10 -2
- package/dist/tools/registry.d.ts +16 -11
- package/dist/tools/registry.js +32 -12
- package/dist/tools/send-media.js +9 -5
- package/dist/tools/session-search.d.ts +2 -1
- package/dist/tools/session-search.js +15 -5
- package/dist/tools/skill.js +13 -12
- package/dist/tools/subagent-manage.d.ts +1 -1
- package/dist/tools/subagent-manage.js +25 -152
- package/dist/tools/subagent.d.ts +24 -2
- package/dist/tools/subagent.js +24 -18
- package/dist/tools/task-manage/control-tools.js +1 -1
- package/dist/tools/task-manage/factory.d.ts +2 -2
- package/dist/tools/task-manage/factory.js +40 -4
- package/dist/tools/task-manage/lifecycle.d.ts +3 -3
- package/dist/tools/task-manage/lifecycle.js +56 -13
- package/dist/tools/task-manage/schema.d.ts +4 -1
- package/dist/tools/task-manage/schema.js +4 -1
- package/dist/tools/task-manage/shared.d.ts +1 -1
- package/dist/tools/task-manage/types.d.ts +5 -1
- package/dist/tools/write-content.d.ts +7 -1
- package/dist/tools/write-content.js +18 -6
- package/dist/tools/write.d.ts +3 -0
- package/dist/tools/write.js +6 -2
- package/dist/tui/cli.d.ts +6 -6
- package/dist/tui/cli.js +120 -28
- package/dist/tui/client.d.ts +7 -0
- package/dist/tui/client.js +67 -16
- package/dist/tui/frontend.d.ts +2 -0
- package/dist/tui/pitui-frontend.d.ts +4 -0
- package/dist/tui/pitui-frontend.js +27 -0
- package/dist/tui/render.d.ts +1 -0
- package/dist/tui/render.js +5 -0
- package/dist/tui/session.js +6 -0
- package/dist/usage/ledger.d.ts +1 -1
- package/dist/usage/render.js +1 -1
- package/dist/web/client.d.ts +1 -0
- package/dist/web/client.js +11 -0
- package/dist/web/fetch.js +6 -3
- package/dist/webapi/auth.d.ts +0 -3
- package/dist/webapi/auth.js +3 -7
- package/dist/webapi/config.js +17 -14
- package/dist/webapi/key-command.js +41 -41
- package/dist/webapi/response-state.d.ts +7 -1
- package/dist/webapi/response-state.js +166 -3
- package/dist/webapi/routes/websocket.js +24 -1
- package/dist/webapi/server.d.ts +3 -1
- package/dist/webapi/server.js +32 -1
- package/dist/webapi/service.js +8 -0
- package/dist/webapi/transport.d.ts +2 -0
- package/dist/webapi/transport.js +11 -1
- package/examples/sub-agents/README.md +43 -75
- package/examples/sub-agents/agy-main.md +21 -0
- package/examples/sub-agents/claude-high.md +21 -0
- package/examples/sub-agents/claude-main.md +21 -0
- package/examples/sub-agents/codex-flash.md +21 -0
- package/examples/sub-agents/codex-high.md +21 -0
- package/examples/sub-agents/codex-main.md +21 -0
- package/examples/sub-agents/codex-review.md +20 -0
- package/examples/sub-agents/cursor-high.md +20 -0
- package/examples/sub-agents/cursor-main.md +20 -0
- package/examples/sub-agents/glm-flash.md +21 -0
- package/examples/sub-agents/glm-high.md +21 -0
- package/examples/sub-agents/pi-scout.md +20 -0
- package/package.json +11 -12
- package/dist/agent/session/request-tools.d.ts +0 -3
- package/dist/agent/session/request-tools.js +0 -20
- package/dist/models/api-keys.d.ts +0 -11
- package/dist/models/api-keys.js +0 -22
- package/dist/playbooks/memory-and-learning.md +0 -55
- package/examples/sub-agents/agents/claude-high.md +0 -23
- package/examples/sub-agents/agents/claude-main.md +0 -23
- package/examples/sub-agents/agents/codex-flash.md +0 -23
- package/examples/sub-agents/agents/codex-high.md +0 -23
- package/examples/sub-agents/agents/codex-main.md +0 -23
- package/examples/sub-agents/agents/codex-review.md +0 -23
- package/examples/sub-agents/agents/cursor-high.md +0 -22
- package/examples/sub-agents/agents/cursor-main.md +0 -22
- package/examples/sub-agents/agents/glm-flash.md +0 -23
- package/examples/sub-agents/agents/glm-high.md +0 -23
- package/examples/sub-agents/agents/scout.md +0 -30
- package/examples/sub-agents/roles/builder-hard.md +0 -34
- package/examples/sub-agents/roles/builder.md +0 -34
- package/examples/sub-agents/roles/explorer.md +0 -39
- package/examples/sub-agents/roles/git-committer.md +0 -43
- package/examples/sub-agents/roles/planner.md +0 -33
- package/examples/sub-agents/roles/reviewer.md +0 -33
- package/examples/sub-agents/roles/verifier.md +0 -35
- package/examples/sub-agents/roles/worker.md +0 -34
package/dist/webapi/config.js
CHANGED
|
@@ -39,55 +39,58 @@ export function parseWebApiConfig(section) {
|
|
|
39
39
|
const raw = isPlainObject(section) ? section : {};
|
|
40
40
|
const socket = raw.socket === undefined ? true : raw.socket;
|
|
41
41
|
if (typeof socket !== "boolean")
|
|
42
|
-
issues.push("webapi: `socket`
|
|
42
|
+
issues.push("webapi: `socket` 必须是布尔值(true 或 false)。");
|
|
43
43
|
let tcp;
|
|
44
|
-
if (raw.tcp !== undefined) {
|
|
44
|
+
if (raw.tcp !== undefined && raw.tcp !== null && raw.tcp !== "") {
|
|
45
45
|
tcp = typeof raw.tcp === "string" ? parseTcpAddress(raw.tcp) : undefined;
|
|
46
46
|
if (!tcp)
|
|
47
|
-
issues.push("webapi: `tcp`
|
|
47
|
+
issues.push("webapi: `tcp` 格式必须是 `host:port`(如 `127.0.0.1:8787`,IPv6 需加方括号如 `[::1]:8787`),或设为 null 关闭 TCP 监听。");
|
|
48
48
|
}
|
|
49
49
|
let tls;
|
|
50
|
-
if (raw.tls !== undefined) {
|
|
50
|
+
if (raw.tls !== undefined && raw.tls !== null) {
|
|
51
51
|
const cert = isPlainObject(raw.tls) ? raw.tls.cert : undefined;
|
|
52
52
|
const key = isPlainObject(raw.tls) ? raw.tls.key : undefined;
|
|
53
|
-
if (
|
|
53
|
+
if (cert === "" && key === "") {
|
|
54
|
+
tls = undefined;
|
|
55
|
+
}
|
|
56
|
+
else if (typeof cert === "string" && typeof key === "string" && isAbsolute(cert) && isAbsolute(key)) {
|
|
54
57
|
tls = { cert, key };
|
|
55
58
|
}
|
|
56
59
|
else {
|
|
57
|
-
issues.push("webapi: `tls`
|
|
60
|
+
issues.push("webapi: `tls` 需要填写绝对路径的 `cert` 与 `key`(不启用 TLS 请设为 null)。");
|
|
58
61
|
}
|
|
59
62
|
}
|
|
60
63
|
if (socket === false && !tcp && issues.length === 0) {
|
|
61
|
-
issues.push("webapi:
|
|
64
|
+
issues.push("webapi: 未开启任何监听地址——请将 `socket` 设为 true,或为 `tcp` 配置 `host:port`。");
|
|
62
65
|
}
|
|
63
66
|
const keys = [];
|
|
64
67
|
if (raw.keys !== undefined && !Array.isArray(raw.keys)) {
|
|
65
|
-
issues.push("webapi: `keys`
|
|
68
|
+
issues.push("webapi: `keys` 必须是数组。");
|
|
66
69
|
}
|
|
67
70
|
const seen = new Set();
|
|
68
71
|
for (const [index, entry] of (Array.isArray(raw.keys) ? raw.keys : []).entries()) {
|
|
69
72
|
const where = `webapi: keys[${index}]`;
|
|
70
73
|
if (!isPlainObject(entry)) {
|
|
71
|
-
issues.push(`${where}
|
|
74
|
+
issues.push(`${where} 必须是 JSON 对象。`);
|
|
72
75
|
continue;
|
|
73
76
|
}
|
|
74
77
|
const { name, sha256, desks } = entry;
|
|
75
78
|
if (typeof name !== "string" || !KEY_NAME_PATTERN.test(name)) {
|
|
76
|
-
issues.push(`${where}.name
|
|
79
|
+
issues.push(`${where}.name 必须匹配 ${KEY_NAME_PATTERN}。`);
|
|
77
80
|
}
|
|
78
81
|
else if (name === RESERVED_KEY_NAME) {
|
|
79
|
-
issues.push(`${where}.name \`${name}\`
|
|
82
|
+
issues.push(`${where}.name \`${name}\` 为本机 socket 保留名称,不可用于 TCP key。`);
|
|
80
83
|
}
|
|
81
84
|
else if (seen.has(name)) {
|
|
82
|
-
issues.push(`${where}.name \`${name}\`
|
|
85
|
+
issues.push(`${where}.name \`${name}\` 重复定义。`);
|
|
83
86
|
}
|
|
84
87
|
if (typeof sha256 !== "string" || !SHA256_HEX.test(sha256)) {
|
|
85
|
-
issues.push(`${where}.sha256
|
|
88
|
+
issues.push(`${where}.sha256 必须是 64 位小写十六进制字符(请使用 \`pipiclaw webapi key add\` 生成)。`);
|
|
86
89
|
}
|
|
87
90
|
if (!Array.isArray(desks) ||
|
|
88
91
|
desks.length === 0 ||
|
|
89
92
|
!desks.every((desk) => typeof desk === "string" && isDeskName(desk))) {
|
|
90
|
-
issues.push(`${where}.desks
|
|
93
|
+
issues.push(`${where}.desks 必须至少包含一个合法的工位名称(如 ["default"])。`);
|
|
91
94
|
}
|
|
92
95
|
if (typeof name === "string" && typeof sha256 === "string" && Array.isArray(desks)) {
|
|
93
96
|
seen.add(name);
|
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `pipiclaw webapi key add|list|remove` (spec 061, D3): the deployer's way to manage the keys in
|
|
3
|
-
* `channel.json`. A key is generated here, printed once, and only its hash is written down.
|
|
4
|
-
* running daemon reads keys at start, so a change takes effect on its next restart.
|
|
3
|
+
* `channel.json`. A key is generated here, printed once, and only its hash is written down.
|
|
5
4
|
*/
|
|
6
5
|
import { readFileSync } from "node:fs";
|
|
7
|
-
import { BootstrapExitError, DEFAULT_BOOTSTRAP_PATHS } from "../app/home.js";
|
|
8
|
-
import {
|
|
6
|
+
import { BootstrapExitError, bootstrapAppHome, DEFAULT_BOOTSTRAP_PATHS, printBootstrapSummary, } from "../app/home.js";
|
|
7
|
+
import { generateWebApiKey, hashWebApiKey } from "../channel/config.js";
|
|
8
|
+
import { DEFAULT_DESK, isDeskName } from "../desk/paths.js";
|
|
9
9
|
import { writeFileAtomically } from "../shared/atomic-file.js";
|
|
10
10
|
import { errorMessage } from "../shared/text-utils.js";
|
|
11
11
|
import { isPlainObject } from "../shared/type-guards.js";
|
|
12
|
-
import { generateKey, hashKey } from "./auth.js";
|
|
13
12
|
import { KEY_NAME_PATTERN, RESERVED_KEY_NAME } from "./config.js";
|
|
14
13
|
const USAGE = [
|
|
15
|
-
"
|
|
16
|
-
" pipiclaw webapi key add
|
|
17
|
-
" pipiclaw webapi key list
|
|
18
|
-
" pipiclaw webapi key remove
|
|
14
|
+
"用法:",
|
|
15
|
+
" pipiclaw webapi key add <名称> [--desk <工位>…] 创建访问密钥(明文仅展示一次;省略 --desk 时默认授权 default 工位)",
|
|
16
|
+
" pipiclaw webapi key list 查看已创建的密钥名称及授权工位",
|
|
17
|
+
" pipiclaw webapi key remove <名称> 删除指定密钥",
|
|
19
18
|
];
|
|
20
19
|
function fail(io, message) {
|
|
21
20
|
io.error(message);
|
|
@@ -26,15 +25,17 @@ function readConfig(paths, io) {
|
|
|
26
25
|
const parsed = JSON.parse(readFileSync(paths.channelConfigPath, "utf-8"));
|
|
27
26
|
if (isPlainObject(parsed))
|
|
28
27
|
return parsed;
|
|
29
|
-
return fail(io, `${paths.channelConfigPath}
|
|
28
|
+
return fail(io, `${paths.channelConfigPath} 内容不是合法的 JSON 对象。`);
|
|
30
29
|
}
|
|
31
30
|
catch (error) {
|
|
32
|
-
return fail(io,
|
|
31
|
+
return fail(io, `无法读取 ${paths.channelConfigPath}:${errorMessage(error)}`);
|
|
33
32
|
}
|
|
34
33
|
}
|
|
35
34
|
/** The `webapi.keys` array of `config`, created (with the section) when absent. */
|
|
36
35
|
function keysOf(config) {
|
|
37
|
-
const section = isPlainObject(config.webapi)
|
|
36
|
+
const section = isPlainObject(config.webapi)
|
|
37
|
+
? config.webapi
|
|
38
|
+
: { enabled: true, socket: true, tcp: "127.0.0.1:8787" };
|
|
38
39
|
config.webapi = section;
|
|
39
40
|
const keys = Array.isArray(section.keys) ? section.keys : [];
|
|
40
41
|
section.keys = keys;
|
|
@@ -48,63 +49,62 @@ function parseDesks(args, io) {
|
|
|
48
49
|
const desks = [];
|
|
49
50
|
for (let index = 0; index < args.length; index += 1) {
|
|
50
51
|
if (args[index] !== "--desk")
|
|
51
|
-
fail(io,
|
|
52
|
+
fail(io, `未知参数:${args[index]}\n${USAGE.join("\n")}`);
|
|
52
53
|
const desk = args[++index];
|
|
53
54
|
if (!desk || !isDeskName(desk))
|
|
54
|
-
fail(io, `--desk
|
|
55
|
+
fail(io, `--desk 需要合法的工位名称(小写字母、数字、连字符 '-'),收到:${desk ?? "空"}`);
|
|
55
56
|
if (!desks.includes(desk))
|
|
56
57
|
desks.push(desk);
|
|
57
58
|
}
|
|
58
|
-
return desks;
|
|
59
|
+
return desks.length > 0 ? desks : [DEFAULT_DESK];
|
|
59
60
|
}
|
|
60
61
|
export async function runWebApiCommand(argv, paths = DEFAULT_BOOTSTRAP_PATHS, io = console) {
|
|
61
62
|
const [group, action, name, ...rest] = argv.slice(3);
|
|
62
63
|
if (argv[2] !== "webapi" || group !== "key")
|
|
63
64
|
fail(io, USAGE.join("\n"));
|
|
65
|
+
if (action !== "list" && action !== "add" && action !== "remove") {
|
|
66
|
+
fail(io, USAGE.join("\n"));
|
|
67
|
+
}
|
|
68
|
+
printBootstrapSummary(bootstrapAppHome(paths), io, paths);
|
|
64
69
|
if (action === "list") {
|
|
65
70
|
const keys = keysOf(readConfig(paths, io));
|
|
66
71
|
if (keys.length === 0)
|
|
67
|
-
io.log("
|
|
72
|
+
io.log("当前未配置任何密钥。可执行 `pipiclaw webapi key add default` 创建。");
|
|
68
73
|
for (const key of keys)
|
|
69
|
-
io.log(`${key.name}\
|
|
74
|
+
io.log(`${key.name}\t工位: ${key.desks.join(", ")}`);
|
|
70
75
|
return;
|
|
71
76
|
}
|
|
72
77
|
if (action === "add") {
|
|
73
78
|
if (name === RESERVED_KEY_NAME) {
|
|
74
|
-
fail(io,
|
|
79
|
+
fail(io, `名称 '${RESERVED_KEY_NAME}' 为本机 socket 保留标识,请换一个名称。`);
|
|
75
80
|
}
|
|
76
81
|
if (!name || !KEY_NAME_PATTERN.test(name)) {
|
|
77
|
-
fail(io,
|
|
82
|
+
fail(io, `密钥名称需为 1–32 位的小写字母、数字或 '-',且以字母或数字开头。\n${USAGE.join("\n")}`);
|
|
78
83
|
}
|
|
79
84
|
const desks = parseDesks(rest, io);
|
|
80
|
-
if (desks.length === 0)
|
|
81
|
-
fail(io, `Give at least one desk the key may use: --desk <desk>.\n${USAGE.join("\n")}`);
|
|
82
85
|
const config = readConfig(paths, io);
|
|
83
86
|
const keys = keysOf(config);
|
|
84
|
-
if (keys.some((key) => key.name === name))
|
|
85
|
-
fail(io,
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
if (keys.some((key) => key.name === name)) {
|
|
88
|
+
fail(io, `名为 "${name}" 的密钥已存在;如需重置请先执行 \`pipiclaw webapi key remove ${name}\`。`);
|
|
89
|
+
}
|
|
90
|
+
const key = generateWebApiKey();
|
|
91
|
+
keys.push({ name, sha256: hashWebApiKey(key), desks });
|
|
88
92
|
await save(paths, config);
|
|
89
|
-
io.log(
|
|
93
|
+
io.log(`✅ 已创建密钥 "${name}"(授权工位:${desks.join(", ")})。明文仅显示这一次,请立即保存:`);
|
|
90
94
|
io.log("");
|
|
91
95
|
io.log(` ${key}`);
|
|
92
96
|
io.log("");
|
|
93
|
-
io.log(
|
|
97
|
+
io.log("已写入 channel.json,正在运行的服务会自动加载生效。");
|
|
94
98
|
return;
|
|
95
99
|
}
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
io.log(`Removed key "${name}". Restart the daemon for it to stop working.`);
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
fail(io, USAGE.join("\n"));
|
|
100
|
+
if (!name || rest.length > 0)
|
|
101
|
+
fail(io, USAGE.join("\n"));
|
|
102
|
+
const config = readConfig(paths, io);
|
|
103
|
+
const keys = keysOf(config);
|
|
104
|
+
const index = keys.findIndex((key) => key.name === name);
|
|
105
|
+
if (index < 0)
|
|
106
|
+
fail(io, `未找到名为 "${name}" 的密钥。`);
|
|
107
|
+
keys.splice(index, 1);
|
|
108
|
+
await save(paths, config);
|
|
109
|
+
io.log(`已移除密钥 "${name}",正在运行的服务会自动生效。`);
|
|
110
110
|
}
|
|
@@ -29,6 +29,8 @@ export declare class LiveResponse {
|
|
|
29
29
|
private announced;
|
|
30
30
|
private readonly subscribers;
|
|
31
31
|
private readonly toolItems;
|
|
32
|
+
private streamingReasoning?;
|
|
33
|
+
private streamingMessage?;
|
|
32
34
|
private readonly ended;
|
|
33
35
|
private resolveEnded;
|
|
34
36
|
private persisting;
|
|
@@ -64,8 +66,12 @@ export declare class LiveResponse {
|
|
|
64
66
|
/** A tool call begins: an `mcp_call` item, in progress, with its arguments. */
|
|
65
67
|
beginToolCall(callId: string, name: string, args: unknown): void;
|
|
66
68
|
endToolCall(callId: string, ok: boolean, output: string, failure?: string): void;
|
|
69
|
+
/** Stream an incremental reasoning delta before the reasoning block finishes. */
|
|
70
|
+
appendReasoning(delta: string): void;
|
|
67
71
|
/** The model's reasoning, as a reasoning item with a one-part summary. */
|
|
68
72
|
addReasoning(text: string): void;
|
|
73
|
+
/** Stream an incremental assistant text delta before the message finishes. */
|
|
74
|
+
appendText(delta: string): void;
|
|
69
75
|
/** A finished assistant message: commentary between tool calls, or the final answer. */
|
|
70
76
|
addMessage(phase: "commentary" | "final_answer", text: string, level?: CommentaryLevel): void;
|
|
71
77
|
/** Output items produced so far, for a caller that needs to know whether the turn said anything. */
|
|
@@ -84,7 +90,7 @@ export declare class LiveResponse {
|
|
|
84
90
|
/** The response is over: store the final record and let waiters go. */
|
|
85
91
|
private settle;
|
|
86
92
|
private save;
|
|
87
|
-
/** Tool calls still running when the response ends are closed as `incomplete
|
|
93
|
+
/** Tool calls or streaming items still running when the response ends are closed as `incomplete`. */
|
|
88
94
|
private closeDanglingCalls;
|
|
89
95
|
private markDanglingCallsIncomplete;
|
|
90
96
|
}
|
|
@@ -166,9 +166,68 @@ export class LiveResponse {
|
|
|
166
166
|
});
|
|
167
167
|
this.emit({ type: "response.output_item.done", output_index: index, item: structuredClone(item) });
|
|
168
168
|
}
|
|
169
|
+
/** Stream an incremental reasoning delta before the reasoning block finishes. */
|
|
170
|
+
appendReasoning(delta) {
|
|
171
|
+
if (this.terminal || !delta)
|
|
172
|
+
return;
|
|
173
|
+
if (!this.streamingReasoning) {
|
|
174
|
+
if (!delta.trim())
|
|
175
|
+
return;
|
|
176
|
+
const item = { id: newId("rs"), type: "reasoning", summary: [], status: "in_progress" };
|
|
177
|
+
const index = this.record.output.push(item) - 1;
|
|
178
|
+
this.streamingReasoning = { index, item, text: "" };
|
|
179
|
+
this.emit({ type: "response.output_item.added", output_index: index, item: structuredClone(item) });
|
|
180
|
+
this.emit({
|
|
181
|
+
type: "response.reasoning_summary_part.added",
|
|
182
|
+
output_index: index,
|
|
183
|
+
item_id: item.id,
|
|
184
|
+
summary_index: 0,
|
|
185
|
+
part: { type: "summary_text", text: "" },
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
const active = this.streamingReasoning;
|
|
189
|
+
active.text += delta;
|
|
190
|
+
this.emit({
|
|
191
|
+
type: "response.reasoning_summary_text.delta",
|
|
192
|
+
output_index: active.index,
|
|
193
|
+
item_id: active.item.id,
|
|
194
|
+
summary_index: 0,
|
|
195
|
+
delta,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
169
198
|
/** The model's reasoning, as a reasoning item with a one-part summary. */
|
|
170
199
|
addReasoning(text) {
|
|
171
|
-
if (this.terminal
|
|
200
|
+
if (this.terminal)
|
|
201
|
+
return;
|
|
202
|
+
if (this.streamingReasoning) {
|
|
203
|
+
const active = this.streamingReasoning;
|
|
204
|
+
this.streamingReasoning = undefined;
|
|
205
|
+
const finalText = text.trim() ? text : active.text;
|
|
206
|
+
this.emit({
|
|
207
|
+
type: "response.reasoning_summary_text.done",
|
|
208
|
+
output_index: active.index,
|
|
209
|
+
item_id: active.item.id,
|
|
210
|
+
summary_index: 0,
|
|
211
|
+
text: finalText,
|
|
212
|
+
});
|
|
213
|
+
const part = { type: "summary_text", text: finalText };
|
|
214
|
+
this.emit({
|
|
215
|
+
type: "response.reasoning_summary_part.done",
|
|
216
|
+
output_index: active.index,
|
|
217
|
+
item_id: active.item.id,
|
|
218
|
+
summary_index: 0,
|
|
219
|
+
part,
|
|
220
|
+
});
|
|
221
|
+
active.item.summary = [part];
|
|
222
|
+
active.item.status = "completed";
|
|
223
|
+
this.emit({
|
|
224
|
+
type: "response.output_item.done",
|
|
225
|
+
output_index: active.index,
|
|
226
|
+
item: structuredClone(active.item),
|
|
227
|
+
});
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (!text.trim())
|
|
172
231
|
return;
|
|
173
232
|
const item = { id: newId("rs"), type: "reasoning", summary: [], status: "in_progress" };
|
|
174
233
|
const index = this.record.output.push(item) - 1;
|
|
@@ -206,9 +265,79 @@ export class LiveResponse {
|
|
|
206
265
|
item.status = "completed";
|
|
207
266
|
this.emit({ type: "response.output_item.done", output_index: index, item: structuredClone(item) });
|
|
208
267
|
}
|
|
268
|
+
/** Stream an incremental assistant text delta before the message finishes. */
|
|
269
|
+
appendText(delta) {
|
|
270
|
+
if (this.terminal || !delta)
|
|
271
|
+
return;
|
|
272
|
+
if (!this.streamingMessage) {
|
|
273
|
+
if (!delta.trim())
|
|
274
|
+
return;
|
|
275
|
+
const id = newId("msg");
|
|
276
|
+
const item = {
|
|
277
|
+
id,
|
|
278
|
+
type: "message",
|
|
279
|
+
role: "assistant",
|
|
280
|
+
status: "in_progress",
|
|
281
|
+
phase: "final_answer",
|
|
282
|
+
content: [],
|
|
283
|
+
};
|
|
284
|
+
const index = this.record.output.push(item) - 1;
|
|
285
|
+
this.streamingMessage = { index, item, text: "" };
|
|
286
|
+
this.emit({ type: "response.output_item.added", output_index: index, item: structuredClone(item) });
|
|
287
|
+
this.emit({
|
|
288
|
+
type: "response.content_part.added",
|
|
289
|
+
output_index: index,
|
|
290
|
+
item_id: id,
|
|
291
|
+
content_index: 0,
|
|
292
|
+
part: { type: "output_text", text: "", annotations: [] },
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
const active = this.streamingMessage;
|
|
296
|
+
active.text += delta;
|
|
297
|
+
this.emit({
|
|
298
|
+
type: "response.output_text.delta",
|
|
299
|
+
output_index: active.index,
|
|
300
|
+
item_id: active.item.id,
|
|
301
|
+
content_index: 0,
|
|
302
|
+
delta,
|
|
303
|
+
logprobs: [],
|
|
304
|
+
});
|
|
305
|
+
}
|
|
209
306
|
/** A finished assistant message: commentary between tool calls, or the final answer. */
|
|
210
307
|
addMessage(phase, text, level) {
|
|
211
|
-
if (this.terminal
|
|
308
|
+
if (this.terminal)
|
|
309
|
+
return;
|
|
310
|
+
if (level === undefined && this.streamingMessage) {
|
|
311
|
+
const active = this.streamingMessage;
|
|
312
|
+
this.streamingMessage = undefined;
|
|
313
|
+
const finalText = text.trim() ? text : active.text;
|
|
314
|
+
active.item.phase = phase;
|
|
315
|
+
this.emit({
|
|
316
|
+
type: "response.output_text.done",
|
|
317
|
+
output_index: active.index,
|
|
318
|
+
item_id: active.item.id,
|
|
319
|
+
content_index: 0,
|
|
320
|
+
text: finalText,
|
|
321
|
+
logprobs: [],
|
|
322
|
+
});
|
|
323
|
+
const part = { type: "output_text", text: finalText, annotations: [] };
|
|
324
|
+
this.emit({
|
|
325
|
+
type: "response.content_part.done",
|
|
326
|
+
output_index: active.index,
|
|
327
|
+
item_id: active.item.id,
|
|
328
|
+
content_index: 0,
|
|
329
|
+
part,
|
|
330
|
+
});
|
|
331
|
+
active.item.content = [part];
|
|
332
|
+
active.item.status = "completed";
|
|
333
|
+
this.emit({
|
|
334
|
+
type: "response.output_item.done",
|
|
335
|
+
output_index: active.index,
|
|
336
|
+
item: structuredClone(active.item),
|
|
337
|
+
});
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (!text.trim())
|
|
212
341
|
return;
|
|
213
342
|
const id = newId("msg");
|
|
214
343
|
const empty = {
|
|
@@ -326,8 +455,30 @@ export class LiveResponse {
|
|
|
326
455
|
});
|
|
327
456
|
this.persisting = Promise.all([this.persisting, written]).then(() => undefined);
|
|
328
457
|
}
|
|
329
|
-
/** Tool calls still running when the response ends are closed as `incomplete
|
|
458
|
+
/** Tool calls or streaming items still running when the response ends are closed as `incomplete`. */
|
|
330
459
|
closeDanglingCalls() {
|
|
460
|
+
if (this.streamingReasoning) {
|
|
461
|
+
const active = this.streamingReasoning;
|
|
462
|
+
this.streamingReasoning = undefined;
|
|
463
|
+
active.item.summary = [{ type: "summary_text", text: active.text }];
|
|
464
|
+
active.item.status = "incomplete";
|
|
465
|
+
this.emit({
|
|
466
|
+
type: "response.output_item.done",
|
|
467
|
+
output_index: active.index,
|
|
468
|
+
item: structuredClone(active.item),
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
if (this.streamingMessage) {
|
|
472
|
+
const active = this.streamingMessage;
|
|
473
|
+
this.streamingMessage = undefined;
|
|
474
|
+
active.item.content = [{ type: "output_text", text: active.text, annotations: [] }];
|
|
475
|
+
active.item.status = "incomplete";
|
|
476
|
+
this.emit({
|
|
477
|
+
type: "response.output_item.done",
|
|
478
|
+
output_index: active.index,
|
|
479
|
+
item: structuredClone(active.item),
|
|
480
|
+
});
|
|
481
|
+
}
|
|
331
482
|
for (const [callId, index] of [...this.toolItems]) {
|
|
332
483
|
this.toolItems.delete(callId);
|
|
333
484
|
const item = this.record.output[index];
|
|
@@ -336,6 +487,18 @@ export class LiveResponse {
|
|
|
336
487
|
}
|
|
337
488
|
}
|
|
338
489
|
markDanglingCallsIncomplete() {
|
|
490
|
+
if (this.streamingReasoning) {
|
|
491
|
+
this.streamingReasoning.item.summary = [{ type: "summary_text", text: this.streamingReasoning.text }];
|
|
492
|
+
this.streamingReasoning.item.status = "incomplete";
|
|
493
|
+
this.streamingReasoning = undefined;
|
|
494
|
+
}
|
|
495
|
+
if (this.streamingMessage) {
|
|
496
|
+
this.streamingMessage.item.content = [
|
|
497
|
+
{ type: "output_text", text: this.streamingMessage.text, annotations: [] },
|
|
498
|
+
];
|
|
499
|
+
this.streamingMessage.item.status = "incomplete";
|
|
500
|
+
this.streamingMessage = undefined;
|
|
501
|
+
}
|
|
339
502
|
for (const [callId, index] of [...this.toolItems]) {
|
|
340
503
|
this.toolItems.delete(callId);
|
|
341
504
|
this.record.output[index].status = "incomplete";
|
|
@@ -94,8 +94,11 @@ class Connection {
|
|
|
94
94
|
case "response.steer":
|
|
95
95
|
await this.steer(event);
|
|
96
96
|
return;
|
|
97
|
+
case "response.cancel":
|
|
98
|
+
await this.cancel(event);
|
|
99
|
+
return;
|
|
97
100
|
default:
|
|
98
|
-
throw badRequest("invalid_value", "Only `response.create` and `response.
|
|
101
|
+
throw badRequest("invalid_value", "Only `response.create`, `response.steer`, and `response.cancel` events are accepted.", "type");
|
|
99
102
|
}
|
|
100
103
|
}
|
|
101
104
|
catch (error) {
|
|
@@ -306,6 +309,26 @@ class Connection {
|
|
|
306
309
|
// The response it follows has already been forwarded to its end: start a new forwarding.
|
|
307
310
|
void this.follow(successor, this.laneOf.get(target.id));
|
|
308
311
|
}
|
|
312
|
+
// ---- response.cancel -------------------------------------------------------------------
|
|
313
|
+
async cancel(event) {
|
|
314
|
+
const targetId = event.response_id;
|
|
315
|
+
if (targetId !== undefined) {
|
|
316
|
+
if (typeof targetId !== "string" || !targetId) {
|
|
317
|
+
throw badRequest("invalid_value", "`response_id` must be a non-empty string.", "response_id");
|
|
318
|
+
}
|
|
319
|
+
const target = this.responses.get(targetId);
|
|
320
|
+
if (!target) {
|
|
321
|
+
throw badRequest("response_not_found", "That response is not on this connection.", "response_id");
|
|
322
|
+
}
|
|
323
|
+
if (!target.terminal)
|
|
324
|
+
await this.service.cancel(target);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
for (const live of this.responses.values()) {
|
|
328
|
+
if (!live.terminal)
|
|
329
|
+
await this.service.cancel(live);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
309
332
|
}
|
|
310
333
|
export function registerWebSocketRoute(app, context) {
|
|
311
334
|
const { service, maxWebSocketMs } = context;
|
package/dist/webapi/server.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type FastifyInstance } from "fastify";
|
|
2
2
|
import type { SecurityLogEvent } from "../security/types.js";
|
|
3
|
-
import type
|
|
3
|
+
import { type WebApiConfig } from "./config.js";
|
|
4
4
|
import { type Listener } from "./http.js";
|
|
5
5
|
import type { WebApiService } from "./service.js";
|
|
6
6
|
export interface ServerDeps {
|
|
@@ -8,6 +8,8 @@ export interface ServerDeps {
|
|
|
8
8
|
service: WebApiService;
|
|
9
9
|
/** Where the local socket is created; its directory is made private. */
|
|
10
10
|
socketPath: string;
|
|
11
|
+
/** When present, `webapi.keys` is reloaded whenever `channel.json`'s file stamp changes. */
|
|
12
|
+
channelConfigPath?: string;
|
|
11
13
|
audit: (event: SecurityLogEvent) => void;
|
|
12
14
|
maxWebSocketMs?: number;
|
|
13
15
|
}
|
package/dist/webapi/server.js
CHANGED
|
@@ -4,14 +4,18 @@
|
|
|
4
4
|
* decides whether a key and a Host check apply); everything that guards the door lives in the
|
|
5
5
|
* `onRequest` hook, which runs before the body is read (INV-6).
|
|
6
6
|
*/
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
7
8
|
import { chmod, mkdir, readFile, rm } from "node:fs/promises";
|
|
8
9
|
import { dirname } from "node:path";
|
|
9
10
|
import rateLimit from "@fastify/rate-limit";
|
|
10
11
|
import websocket from "@fastify/websocket";
|
|
11
12
|
import Fastify from "fastify";
|
|
12
13
|
import * as log from "../log.js";
|
|
14
|
+
import { fileStamp } from "../shared/format.js";
|
|
13
15
|
import { errorMessage } from "../shared/text-utils.js";
|
|
16
|
+
import { isPlainObject } from "../shared/type-guards.js";
|
|
14
17
|
import { authenticate, LOCAL_PRINCIPAL } from "./auth.js";
|
|
18
|
+
import { parseWebApiConfig } from "./config.js";
|
|
15
19
|
import { badRequest, forbidden, notFound, serverError, tooManyRequests, unauthorized, WebApiError } from "./errors.js";
|
|
16
20
|
import { sendError } from "./http.js";
|
|
17
21
|
import { KEEP_ALIVE_TIMEOUT_MS, MAX_BODY_BYTES, MAX_CONNECTIONS, MAX_WS_CONNECTION_MS, RATE_LIMIT_PER_MINUTE, REQUEST_TIMEOUT_MS, } from "./limits.js";
|
|
@@ -19,6 +23,32 @@ import { registerConversationRoutes } from "./routes/conversations.js";
|
|
|
19
23
|
import { registerModelRoutes } from "./routes/models.js";
|
|
20
24
|
import { registerResponseRoutes } from "./routes/responses.js";
|
|
21
25
|
import { registerWebSocketRoute } from "./routes/websocket.js";
|
|
26
|
+
function createKeyResolver(initialKeys, channelConfigPath) {
|
|
27
|
+
if (!channelConfigPath)
|
|
28
|
+
return () => initialKeys;
|
|
29
|
+
let currentKeys = initialKeys;
|
|
30
|
+
let lastStamp = fileStamp(channelConfigPath);
|
|
31
|
+
return () => {
|
|
32
|
+
const stamp = fileStamp(channelConfigPath);
|
|
33
|
+
if (stamp === lastStamp)
|
|
34
|
+
return currentKeys;
|
|
35
|
+
lastStamp = stamp;
|
|
36
|
+
if (!stamp)
|
|
37
|
+
return currentKeys;
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(readFileSync(channelConfigPath, "utf-8"));
|
|
40
|
+
if (isPlainObject(parsed) && isPlainObject(parsed.webapi)) {
|
|
41
|
+
currentKeys = parseWebApiConfig(parsed.webapi).keys;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
log.logWarn("webapi.keys.reload_failed", "Web API keys reload failed; keeping previous keys", {
|
|
46
|
+
reason: errorMessage(error),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return currentKeys;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
22
52
|
/** Hosts a loopback TCP listener answers to: a rebinding page cannot make a browser send these. */
|
|
23
53
|
function allowedHosts(config) {
|
|
24
54
|
const tcp = config.tcp;
|
|
@@ -100,6 +130,7 @@ async function buildApp(listener, deps, https) {
|
|
|
100
130
|
});
|
|
101
131
|
}
|
|
102
132
|
const hosts = allowedHosts(deps.config);
|
|
133
|
+
const resolveKeys = createKeyResolver(deps.config.keys, deps.channelConfigPath);
|
|
103
134
|
const remoteOf = (request) => (listener === "socket" ? "socket" : request.ip);
|
|
104
135
|
const refuse = (request, reply, reason, error) => {
|
|
105
136
|
deps.audit({
|
|
@@ -133,7 +164,7 @@ async function buildApp(listener, deps, https) {
|
|
|
133
164
|
return;
|
|
134
165
|
}
|
|
135
166
|
const header = request.headers.authorization;
|
|
136
|
-
const principal = authenticate(header,
|
|
167
|
+
const principal = authenticate(header, resolveKeys());
|
|
137
168
|
if (!principal)
|
|
138
169
|
return refuse(request, reply, header ? "invalid_key" : "missing_key", unauthorized());
|
|
139
170
|
request.principal = principal;
|
package/dist/webapi/service.js
CHANGED
|
@@ -513,10 +513,18 @@ class ResponseTurn {
|
|
|
513
513
|
if (!this.quiet)
|
|
514
514
|
live.endToolCall(event.callId, event.ok, event.output, event.failure);
|
|
515
515
|
return;
|
|
516
|
+
case "reasoning.delta":
|
|
517
|
+
if (!this.quiet)
|
|
518
|
+
live.appendReasoning(event.delta);
|
|
519
|
+
return;
|
|
516
520
|
case "reasoning":
|
|
517
521
|
if (!this.quiet)
|
|
518
522
|
live.addReasoning(event.text);
|
|
519
523
|
return;
|
|
524
|
+
case "text.delta":
|
|
525
|
+
if (!this.quiet)
|
|
526
|
+
live.appendText(event.delta);
|
|
527
|
+
return;
|
|
520
528
|
case "commentary":
|
|
521
529
|
if (!this.quiet)
|
|
522
530
|
live.addMessage("commentary", event.text, event.level);
|
|
@@ -8,6 +8,8 @@ export interface WebApiTransportDeps {
|
|
|
8
8
|
config: WebApiConfig;
|
|
9
9
|
/** `<app home>/state/webapi` */
|
|
10
10
|
stateDir: string;
|
|
11
|
+
/** Optional path to `channel.json` so `webapi.keys` updates take effect without restarting the daemon. */
|
|
12
|
+
channelConfigPath?: string;
|
|
11
13
|
/** Where requests and refused requests are recorded (the security audit log). */
|
|
12
14
|
audit: (event: SecurityLogEvent) => void;
|
|
13
15
|
/** Test overrides. */
|
package/dist/webapi/transport.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* about HTTP; this file and its neighbours never learn what a command means.
|
|
6
6
|
*/
|
|
7
7
|
import { join } from "node:path";
|
|
8
|
+
import { StartupRefusal } from "../desk/startup-error.js";
|
|
8
9
|
import { conversationIdOf } from "./ids.js";
|
|
9
10
|
import { WebApiServer } from "./server.js";
|
|
10
11
|
import { WebApiService } from "./service.js";
|
|
@@ -29,7 +30,7 @@ export class WebApiTransport {
|
|
|
29
30
|
}
|
|
30
31
|
async start(ingress) {
|
|
31
32
|
if (this.deps.config.socket && Buffer.byteLength(this.socketPath) > MAX_SOCKET_PATH_BYTES) {
|
|
32
|
-
throw new
|
|
33
|
+
throw new StartupRefusal(`Web API 本机 socket 路径过长(${this.socketPath}),超出系统 Unix Socket 长度限制;请通过 PIPICLAW_HOME 指定更短的目录路径。`);
|
|
33
34
|
}
|
|
34
35
|
const store = await WebApiStore.open(this.deps.stateDir);
|
|
35
36
|
const service = new WebApiService({
|
|
@@ -41,6 +42,7 @@ export class WebApiTransport {
|
|
|
41
42
|
config: this.deps.config,
|
|
42
43
|
service,
|
|
43
44
|
socketPath: this.socketPath,
|
|
45
|
+
channelConfigPath: this.deps.channelConfigPath,
|
|
44
46
|
audit: this.deps.audit,
|
|
45
47
|
...(this.deps.maxWebSocketMs ? { maxWebSocketMs: this.deps.maxWebSocketMs } : {}),
|
|
46
48
|
});
|
|
@@ -51,6 +53,14 @@ export class WebApiTransport {
|
|
|
51
53
|
// Nothing is kept on `this`, so `stop()` could not reach these: release them here.
|
|
52
54
|
await service.shutdown().catch(() => undefined);
|
|
53
55
|
await store.close().catch(() => undefined);
|
|
56
|
+
const code = error && typeof error === "object" && "code" in error ? error.code : undefined;
|
|
57
|
+
if (this.deps.config.tcp && (code === "EADDRINUSE" || code === "EACCES")) {
|
|
58
|
+
const address = `${this.deps.config.tcp.host}:${this.deps.config.tcp.port}`;
|
|
59
|
+
const where = this.deps.channelConfigPath ?? "channel.json";
|
|
60
|
+
throw new StartupRefusal(code === "EADDRINUSE"
|
|
61
|
+
? `Web API 无法监听 ${address}:该端口已被其他进程占用。\n请关闭占用该端口的程序,或在 ${where} 中修改 \`webapi.tcp\` 端口(若仅使用终端 TUI,也可将 \`webapi.tcp\` 设为 null)。`
|
|
62
|
+
: `Web API 无权限监听 ${address}。\n请在 ${where} 中将 \`webapi.tcp\` 修改为 1024 以上的可用端口。`);
|
|
63
|
+
}
|
|
54
64
|
throw error;
|
|
55
65
|
}
|
|
56
66
|
this.service = service;
|