@zyaochen/weaverbird 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.en.md +418 -0
- package/README.md +418 -2
- package/apps/wb-bot/README.md +118 -0
- package/dist/apps/wb-bot/src/agent-display.js +24 -0
- package/dist/apps/wb-bot/src/agent-registry.js +188 -0
- package/dist/apps/wb-bot/src/approval-gate/audit.js +37 -0
- package/dist/apps/wb-bot/src/approval-gate/cards.js +94 -0
- package/dist/apps/wb-bot/src/approval-gate/hook-env.js +15 -0
- package/dist/apps/wb-bot/src/approval-gate/hook-files.js +311 -0
- package/dist/apps/wb-bot/src/approval-gate/hook-main.js +147 -0
- package/dist/apps/wb-bot/src/approval-gate/notifier.js +29 -0
- package/dist/apps/wb-bot/src/approval-gate/store.js +157 -0
- package/dist/apps/wb-bot/src/binding/migration-runner.js +35 -0
- package/dist/apps/wb-bot/src/binding/migrations/001-add-run-id-agent-id.js +8 -0
- package/dist/apps/wb-bot/src/binding/migrations/002-add-bot-id.js +34 -0
- package/dist/apps/wb-bot/src/binding/migrations/003-inflight-card-agent-id-pk.js +31 -0
- package/dist/apps/wb-bot/src/binding/migrations/004-thread-alias-topic-key.js +31 -0
- package/dist/apps/wb-bot/src/binding/migrations/005-card-entity.js +21 -0
- package/dist/apps/wb-bot/src/binding/migrations/006-group-setting.js +17 -0
- package/dist/apps/wb-bot/src/binding/schema.js +58 -0
- package/dist/apps/wb-bot/src/binding/sleep-sync.js +5 -0
- package/dist/apps/wb-bot/src/binding/store.js +270 -0
- package/dist/apps/wb-bot/src/binding/task-index.js +127 -0
- package/dist/apps/wb-bot/src/boot/entry-path.js +9 -0
- package/dist/apps/wb-bot/src/boot/integrity.js +47 -0
- package/dist/apps/wb-bot/src/boot/logger.js +44 -0
- package/dist/apps/wb-bot/src/captain/bot-discovery.js +42 -0
- package/dist/apps/wb-bot/src/captain/captain-context.js +26 -0
- package/dist/apps/wb-bot/src/captain/captain-gate.js +9 -0
- package/dist/apps/wb-bot/src/captain/captain-status.js +101 -0
- package/dist/apps/wb-bot/src/captain/captain-stop-all.js +72 -0
- package/dist/apps/wb-bot/src/captain/list-bots.js +39 -0
- package/dist/apps/wb-bot/src/cli/doctor.js +105 -0
- package/dist/apps/wb-bot/src/cli/init.js +122 -0
- package/dist/apps/wb-bot/src/cli/parse-args.js +31 -0
- package/dist/apps/wb-bot/src/cli/preflight.js +9 -0
- package/dist/apps/wb-bot/src/cli/setup-checklist.js +10 -0
- package/dist/apps/wb-bot/src/commands/health.js +328 -0
- package/dist/apps/wb-bot/src/commands/history.js +100 -0
- package/dist/apps/wb-bot/src/commands/logs.js +91 -0
- package/dist/apps/wb-bot/src/commands/parser.js +43 -0
- package/dist/apps/wb-bot/src/commands/queue-metrics.js +24 -0
- package/dist/apps/wb-bot/src/commands/resume.js +48 -0
- package/dist/apps/wb-bot/src/commands/router.js +299 -0
- package/dist/apps/wb-bot/src/commands/status.js +77 -0
- package/dist/apps/wb-bot/src/commands/tasks.js +106 -0
- package/dist/apps/wb-bot/src/commands/wb-approval.js +86 -0
- package/dist/apps/wb-bot/src/commands/wb-audit.js +46 -0
- package/dist/apps/wb-bot/src/commands/wb-mute.js +84 -0
- package/dist/apps/wb-bot/src/commands/wb-sync.js +58 -0
- package/dist/apps/wb-bot/src/config/approval-code-owners.js +95 -0
- package/dist/apps/wb-bot/src/config/bot-env.js +80 -0
- package/dist/apps/wb-bot/src/config/config-paths.js +16 -0
- package/dist/apps/wb-bot/src/config/env.js +43 -0
- package/dist/apps/wb-bot/src/config/lark-credentials.js +14 -0
- package/dist/apps/wb-bot/src/e2e/preflight-env.js +147 -0
- package/dist/apps/wb-bot/src/e2e/preflight-main.js +12 -0
- package/dist/apps/wb-bot/src/feishu/admin-alert.js +94 -0
- package/dist/apps/wb-bot/src/feishu/artifacts.js +95 -0
- package/dist/apps/wb-bot/src/feishu/bot-identity.js +38 -0
- package/dist/apps/wb-bot/src/feishu/card-pipeline.js +121 -0
- package/dist/apps/wb-bot/src/feishu/card-renderer.js +360 -0
- package/dist/apps/wb-bot/src/feishu/card-transport.js +294 -0
- package/dist/apps/wb-bot/src/feishu/chat-mode-cache.js +47 -0
- package/dist/apps/wb-bot/src/feishu/completion-push.js +30 -0
- package/dist/apps/wb-bot/src/feishu/doc-comment.js +183 -0
- package/dist/apps/wb-bot/src/feishu/event-handler.js +2634 -0
- package/dist/apps/wb-bot/src/feishu/events-file-reader.js +162 -0
- package/dist/apps/wb-bot/src/feishu/inbound-context.js +53 -0
- package/dist/apps/wb-bot/src/feishu/keepalive.js +85 -0
- package/dist/apps/wb-bot/src/feishu/log-redact.js +20 -0
- package/dist/apps/wb-bot/src/feishu/markdown-style.js +95 -0
- package/dist/apps/wb-bot/src/feishu/media-cache.js +92 -0
- package/dist/apps/wb-bot/src/feishu/message-parser.js +126 -0
- package/dist/apps/wb-bot/src/feishu/pending-queue.js +133 -0
- package/dist/apps/wb-bot/src/feishu/reaction.js +44 -0
- package/dist/apps/wb-bot/src/feishu/reverse-at-im-handler.js +345 -0
- package/dist/apps/wb-bot/src/feishu/run-state.js +30 -0
- package/dist/apps/wb-bot/src/feishu/session-short-id.js +9 -0
- package/dist/apps/wb-bot/src/feishu/stream-state.js +72 -0
- package/dist/apps/wb-bot/src/feishu/topic-key.js +34 -0
- package/dist/apps/wb-bot/src/feishu/transport.js +768 -0
- package/dist/apps/wb-bot/src/feishu/wb-runner-spawner.js +84 -0
- package/dist/apps/wb-bot/src/heartbeat/bot-heartbeat.js +20 -0
- package/dist/apps/wb-bot/src/main.js +144 -0
- package/dist/apps/wb-bot/src/multi/discovery.js +53 -0
- package/dist/apps/wb-bot/src/multi/main.js +57 -0
- package/dist/apps/wb-bot/src/multi/parse-args.js +17 -0
- package/dist/apps/wb-bot/src/multi/supervisor.js +288 -0
- package/dist/apps/wb-bot/src/run-observability/orchestrator.js +61 -0
- package/dist/apps/wb-bot/src/security/bot-id-validator.js +18 -0
- package/dist/apps/wb-bot/src/security/cwd-guard.js +72 -0
- package/dist/apps/wb-bot/src/security/env-whitelist.js +94 -0
- package/dist/apps/wb-bot/src/server/wire-up.js +125 -0
- package/dist/apps/wb-cli/src/commands/approval.js +101 -0
- package/dist/apps/wb-cli/src/commands/attach.js +31 -0
- package/dist/apps/wb-cli/src/commands/audit.js +71 -0
- package/dist/apps/wb-cli/src/commands/captain.js +72 -0
- package/dist/apps/wb-cli/src/commands/debug.js +22 -0
- package/dist/apps/wb-cli/src/commands/history.js +75 -0
- package/dist/apps/wb-cli/src/commands/list.js +24 -0
- package/dist/apps/wb-cli/src/commands/logs.js +122 -0
- package/dist/apps/wb-cli/src/commands/resume.js +50 -0
- package/dist/apps/wb-cli/src/commands/runs.js +75 -0
- package/dist/apps/wb-cli/src/commands/status.js +35 -0
- package/dist/apps/wb-cli/src/commands/watch.js +73 -0
- package/dist/apps/wb-cli/src/main.js +312 -0
- package/dist/packages/core/src/agent/claude-code.js +354 -0
- package/dist/packages/core/src/agent/cli-subprocess.js +244 -0
- package/dist/packages/core/src/agent/codex.js +231 -0
- package/dist/packages/core/src/agent/index.js +5 -0
- package/dist/packages/core/src/agent/registry.js +47 -0
- package/dist/packages/core/src/agent/types.js +1 -0
- package/dist/packages/core/src/approval-gate/audit.js +16 -0
- package/dist/packages/core/src/approval-gate/contracts.js +26 -0
- package/dist/packages/core/src/approval-gate/matcher.js +112 -0
- package/dist/packages/core/src/audit-pipe/audit-pipe-main.js +38 -0
- package/dist/packages/core/src/audit-pipe/bitable-sink.js +156 -0
- package/dist/packages/core/src/audit-pipe/config.js +105 -0
- package/dist/packages/core/src/audit-pipe/contracts.js +33 -0
- package/dist/packages/core/src/audit-pipe/index.js +2 -0
- package/dist/packages/core/src/audit-pipe/mirror-sink.js +21 -0
- package/dist/packages/core/src/audit-pipe/normalizer.js +129 -0
- package/dist/packages/core/src/audit-pipe/orchestrator.js +252 -0
- package/dist/packages/core/src/audit-pipe/parser.js +24 -0
- package/dist/packages/core/src/audit-pipe/tailer.js +46 -0
- package/dist/packages/core/src/audit-pipe/wal.js +71 -0
- package/dist/packages/core/src/calendar-hook/config.js +45 -0
- package/dist/packages/core/src/calendar-hook/contracts.js +66 -0
- package/dist/packages/core/src/calendar-hook/index.js +5 -0
- package/dist/packages/core/src/calendar-hook/scheduler.js +169 -0
- package/dist/packages/core/src/calendar-hook/service.js +189 -0
- package/dist/packages/core/src/calendar-hook/transport.js +66 -0
- package/dist/packages/core/src/captain/captain-service.js +160 -0
- package/dist/packages/core/src/captain/command-catalog.js +41 -0
- package/dist/packages/core/src/captain/concurrency-config.js +10 -0
- package/dist/packages/core/src/captain/concurrency-queue-index.js +94 -0
- package/dist/packages/core/src/captain/concurrency-queue-store.js +43 -0
- package/dist/packages/core/src/captain/contracts.js +102 -0
- package/dist/packages/core/src/captain/cwd-inference.js +29 -0
- package/dist/packages/core/src/captain/enqueue-or-spawn.js +63 -0
- package/dist/packages/core/src/captain/index.js +27 -0
- package/dist/packages/core/src/captain/queue-metrics/aggregator.js +98 -0
- package/dist/packages/core/src/captain/queue-metrics/contracts.js +42 -0
- package/dist/packages/core/src/captain/queue-metrics/event-store.js +26 -0
- package/dist/packages/core/src/captain/session-store.js +429 -0
- package/dist/packages/core/src/captain/toml-loader.js +86 -0
- package/dist/packages/core/src/config/errors.js +15 -0
- package/dist/packages/core/src/config/registry.js +34 -0
- package/dist/packages/core/src/errors/codes.js +98 -0
- package/dist/packages/core/src/group-sync/autodoc.js +166 -0
- package/dist/packages/core/src/group-sync/config.js +45 -0
- package/dist/packages/core/src/group-sync/contracts.js +126 -0
- package/dist/packages/core/src/group-sync/dispatch.js +163 -0
- package/dist/packages/core/src/group-sync/index.js +9 -0
- package/dist/packages/core/src/group-sync/revdiff.js +138 -0
- package/dist/packages/core/src/group-sync/scheduler.js +93 -0
- package/dist/packages/core/src/group-sync/service.js +79 -0
- package/dist/packages/core/src/group-sync/transport.js +97 -0
- package/dist/packages/core/src/group-sync/xref.js +139 -0
- package/dist/packages/core/src/index.js +23 -0
- package/dist/packages/core/src/minutes/adapter.js +187 -0
- package/dist/packages/core/src/minutes/client.js +46 -0
- package/dist/packages/core/src/minutes/contracts.js +36 -0
- package/dist/packages/core/src/minutes/index.js +3 -0
- package/dist/packages/core/src/paths.js +158 -0
- package/dist/packages/core/src/periodic-tasks/config.js +45 -0
- package/dist/packages/core/src/periodic-tasks/contracts.js +75 -0
- package/dist/packages/core/src/periodic-tasks/index.js +4 -0
- package/dist/packages/core/src/periodic-tasks/orchestrator.js +249 -0
- package/dist/packages/core/src/periodic-tasks/state.js +29 -0
- package/dist/packages/core/src/reverse-channel/attach-handler.js +108 -0
- package/dist/packages/core/src/reverse-channel/audit-encoder.js +61 -0
- package/dist/packages/core/src/reverse-channel/contracts.js +136 -0
- package/dist/packages/core/src/reverse-channel/echo-tailer.js +36 -0
- package/dist/packages/core/src/reverse-channel/handoff.js +238 -0
- package/dist/packages/core/src/reverse-channel/index.js +5 -0
- package/dist/packages/core/src/reverse-channel/ipc-client.js +110 -0
- package/dist/packages/core/src/reverse-channel/ipc-control-handler.js +27 -0
- package/dist/packages/core/src/reverse-channel/ipc-server.js +127 -0
- package/dist/packages/core/src/reverse-channel/mute-registry.js +113 -0
- package/dist/packages/core/src/reverse-channel/pid-resolver.js +41 -0
- package/dist/packages/core/src/reverse-channel/reverse-at-handler.js +19 -0
- package/dist/packages/core/src/reverse-channel/sla-tracker.js +51 -0
- package/dist/packages/core/src/run-observability/alert-card-renderer.js +24 -0
- package/dist/packages/core/src/run-observability/alert-pusher.js +47 -0
- package/dist/packages/core/src/run-observability/card-refresher.js +31 -0
- package/dist/packages/core/src/run-observability/card-renderer-progress.js +33 -0
- package/dist/packages/core/src/run-observability/config-loader.js +23 -0
- package/dist/packages/core/src/run-observability/contracts.js +107 -0
- package/dist/packages/core/src/run-observability/exit-code-detector.js +16 -0
- package/dist/packages/core/src/run-observability/finalizer.js +114 -0
- package/dist/packages/core/src/run-observability/index.js +12 -0
- package/dist/packages/core/src/run-observability/obs-toggle-handler.js +29 -0
- package/dist/packages/core/src/run-observability/progress-collector.js +53 -0
- package/dist/packages/core/src/run-observability/progress-store.js +100 -0
- package/dist/packages/core/src/run-observability/silent-timeout-detector.js +12 -0
- package/dist/packages/core/src/run-registry/bitable-writer.js +308 -0
- package/dist/packages/core/src/run-registry/config.js +77 -0
- package/dist/packages/core/src/run-registry/contracts.js +114 -0
- package/dist/packages/core/src/run-registry/doc-sink-classifier.js +0 -0
- package/dist/packages/core/src/run-registry/doc-sink-transport.js +63 -0
- package/dist/packages/core/src/run-registry/doc-sink.js +200 -0
- package/dist/packages/core/src/run-registry/extractor.js +40 -0
- package/dist/packages/core/src/run-registry/index.js +11 -0
- package/dist/packages/core/src/run-registry/inherit.js +11 -0
- package/dist/packages/core/src/run-registry/orchestrator.js +86 -0
- package/dist/packages/core/src/run-registry/run-registry-main.js +38 -0
- package/dist/packages/core/src/run-registry/tailer.js +390 -0
- package/dist/packages/core/src/run-registry/wal.js +106 -0
- package/dist/packages/core/src/runner/event-replay.js +65 -0
- package/dist/packages/core/src/runner/wb-runner-main.js +27 -0
- package/dist/packages/core/src/runner/wb-runner.js +237 -0
- package/dist/packages/core/src/util/entry-module.js +21 -0
- package/dist/packages/core/src/util/ndjson-rotation.js +99 -0
- package/package.json +76 -4
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { BindingStore } from '../binding/store.js';
|
|
2
|
+
import { isCaptainEnabled } from '../captain/captain-gate.js';
|
|
3
|
+
import { resolveCaptainContext } from '../captain/captain-context.js';
|
|
4
|
+
import { renderListBotsCard } from '../captain/list-bots.js';
|
|
5
|
+
import { renderCaptainStatusCard } from '../captain/captain-status.js';
|
|
6
|
+
import { captainStopAll, renderCaptainStopAllCard } from '../captain/captain-stop-all.js';
|
|
7
|
+
import { parseCommand } from './parser.js';
|
|
8
|
+
import { renderStatus } from './status.js';
|
|
9
|
+
import { renderLogs } from './logs.js';
|
|
10
|
+
import { renderHealth } from './health.js';
|
|
11
|
+
import { handleHistory } from './history.js';
|
|
12
|
+
import { renderQueueMetrics } from './queue-metrics.js';
|
|
13
|
+
import { handleResume } from './resume.js';
|
|
14
|
+
import { handleWbSync } from './wb-sync.js';
|
|
15
|
+
import { handleWbAudit } from './wb-audit.js';
|
|
16
|
+
import { handleWbApproval } from './wb-approval.js';
|
|
17
|
+
import { handleWbMute, handleWbUnmute } from './wb-mute.js';
|
|
18
|
+
import { renderTasks } from './tasks.js';
|
|
19
|
+
import { validateCwd, CWD_DENYLIST } from '../security/cwd-guard.js';
|
|
20
|
+
function summarizeCapabilities(caps) {
|
|
21
|
+
const parts = [caps.type, caps.context.costClass];
|
|
22
|
+
if (caps.output.streaming)
|
|
23
|
+
parts.push('streaming');
|
|
24
|
+
if (caps.output.thinking)
|
|
25
|
+
parts.push('thinking');
|
|
26
|
+
if (caps.output.toolUse)
|
|
27
|
+
parts.push('tool-use');
|
|
28
|
+
parts.push(caps.session.resumable ? 'resumable' : 'stateless');
|
|
29
|
+
return parts.join('/');
|
|
30
|
+
}
|
|
31
|
+
// Per-context cache keyed by (botId, agentAllowed) — 避免不同 bot / 不同 agent 白名单互相覆盖结果.
|
|
32
|
+
// 行为不变量: TTL 仍 10_000 ms, resetAgentListCache() 仍清整个 cache. (W2.5-Hotfix-batch-D D2)
|
|
33
|
+
const cachedAgentList = new Map();
|
|
34
|
+
function agentListCacheKey(ctx) {
|
|
35
|
+
const agentIds = ctx.registry.list().map((a) => a.id).slice().sort().join(',');
|
|
36
|
+
return `${ctx.botId}|${agentIds}`;
|
|
37
|
+
}
|
|
38
|
+
export function resetAgentListCache() {
|
|
39
|
+
cachedAgentList.clear();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* /help 分模式输出 (W2.5-Hotfix-batch-C P1-5):
|
|
43
|
+
* - single mode: 不暴露 /list-bots 等 Captain-only 命令, 不写 "W2.5 启用后..." 之类
|
|
44
|
+
* multi-bot 心智泄漏 (符合 docs/product-strategy.md §3 "单 agent 视图不暴露 multi 概念").
|
|
45
|
+
* - Captain mode (multi-bot + default + 未禁用): 增加 [Captain 跨 bot] 分组,
|
|
46
|
+
* 把跨 bot 控制面命令清晰隔离.
|
|
47
|
+
*
|
|
48
|
+
* 不影响命令本身是否被路由处理 — 没 Captain 时用户输 /list-bots 依然有 degrade
|
|
49
|
+
* fallback, 这里只是 /help 默认展示面收紧.
|
|
50
|
+
*/
|
|
51
|
+
function renderHelp(ctx) {
|
|
52
|
+
const captainEnabled = isCaptainEnabled({
|
|
53
|
+
mode: process.env['WB_MODE'] === 'multi-bot' ? 'multi-bot' : 'single',
|
|
54
|
+
botId: ctx.botId,
|
|
55
|
+
disableCaptainEnv: process.env['WB_DISABLE_CAPTAIN'],
|
|
56
|
+
});
|
|
57
|
+
const core = [
|
|
58
|
+
'[核心]',
|
|
59
|
+
'/use <agent>',
|
|
60
|
+
'/list-agents (alias: /agents)',
|
|
61
|
+
'/now',
|
|
62
|
+
'/reset-context',
|
|
63
|
+
'/stop',
|
|
64
|
+
'/follow — 标记当前 thread, 完成时通过新消息额外通知',
|
|
65
|
+
'/help',
|
|
66
|
+
];
|
|
67
|
+
const cwd = [
|
|
68
|
+
'',
|
|
69
|
+
'[工作目录]',
|
|
70
|
+
'/cwd <abs-or-alias>',
|
|
71
|
+
'/cwd-add <name> <abs-path>',
|
|
72
|
+
'/cwd-list',
|
|
73
|
+
'/cwd-rm <name>',
|
|
74
|
+
];
|
|
75
|
+
const group = [
|
|
76
|
+
'',
|
|
77
|
+
'[群设置]',
|
|
78
|
+
'/require-mention on|off — 本群是否必须 @bot 才响应 (群主 / 群管理员)',
|
|
79
|
+
];
|
|
80
|
+
const localControl = [
|
|
81
|
+
'',
|
|
82
|
+
'[本 bot 控制]',
|
|
83
|
+
'/status — 本 bot active run 摘要',
|
|
84
|
+
'/stop-all — 给本 bot 所有 active run 发 stop 信号',
|
|
85
|
+
'/logs [tail=N] — 本 bot 最近日志',
|
|
86
|
+
'/health — 本 bot 控制面 + WAL 健康',
|
|
87
|
+
'/tasks [N] [<agentId>] — 本 bot 任务历史',
|
|
88
|
+
'/queue-metrics — 当前 session 队列指标',
|
|
89
|
+
'/wb mute <sid> <duration> — session 维度静音 (30s/15m/2h/7d)',
|
|
90
|
+
'/wb unmute <sid> — 解除 session 静音',
|
|
91
|
+
'/wb mute list — 查看当前静音列表',
|
|
92
|
+
];
|
|
93
|
+
if (!captainEnabled) {
|
|
94
|
+
return [...core, ...cwd, ...group, ...localControl].join('\n');
|
|
95
|
+
}
|
|
96
|
+
const captain = [
|
|
97
|
+
'',
|
|
98
|
+
'[Captain 跨 bot]',
|
|
99
|
+
'/status — 所有 bot 汇总摘要 (per-bot 摘要视图)',
|
|
100
|
+
'/stop-all — 给所有 bot 异步派发 stop 信号',
|
|
101
|
+
'/health — Captain CP + 其它 bot 心跳状态',
|
|
102
|
+
'/list-bots — 列出所有已配置 bot 及在线状态',
|
|
103
|
+
];
|
|
104
|
+
return [...core, ...cwd, ...group, ...localControl, ...captain].join('\n');
|
|
105
|
+
}
|
|
106
|
+
async function renderAgents(ctx) {
|
|
107
|
+
const now = Date.now();
|
|
108
|
+
const key = agentListCacheKey(ctx);
|
|
109
|
+
const hit = cachedAgentList.get(key);
|
|
110
|
+
if (hit && hit.expiresAt > now)
|
|
111
|
+
return hit.value;
|
|
112
|
+
const items = await Promise.all(ctx.registry.list().map(async (agent) => {
|
|
113
|
+
const [caps, available] = await Promise.all([
|
|
114
|
+
agent.getCapabilities(),
|
|
115
|
+
ctx.registry.isAvailable(agent.id),
|
|
116
|
+
]);
|
|
117
|
+
return `- ${agent.displayName} (${agent.id}) · ${available ? 'available' : 'unavailable'} · ${summarizeCapabilities(caps)}`;
|
|
118
|
+
}));
|
|
119
|
+
const value = items.join('\n');
|
|
120
|
+
cachedAgentList.set(key, { value, expiresAt: now + 10_000 });
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
export async function runCommand(input, ctx) {
|
|
124
|
+
const trimmed = input.trimStart();
|
|
125
|
+
if (!trimmed.startsWith('/'))
|
|
126
|
+
return null;
|
|
127
|
+
const [command, ...args] = parseCommand(trimmed);
|
|
128
|
+
if (!command)
|
|
129
|
+
return null;
|
|
130
|
+
if (command === '/list-agents' || command === '/agents')
|
|
131
|
+
return renderAgents(ctx);
|
|
132
|
+
if (command === '/help') {
|
|
133
|
+
return renderHelp(ctx);
|
|
134
|
+
}
|
|
135
|
+
if (command === '/status') {
|
|
136
|
+
const captain = resolveCaptainContext({ botId: ctx.botId });
|
|
137
|
+
if (!captain.enabled) {
|
|
138
|
+
const sessions = ctx.activeSessions ? ctx.activeSessions() : [];
|
|
139
|
+
return renderStatus(sessions, ctx.controlPlane);
|
|
140
|
+
}
|
|
141
|
+
return renderCaptainStatusCard({ bots: captain.bots, store: ctx.store, dataDir: captain.dataDir, captainBotId: ctx.botId });
|
|
142
|
+
}
|
|
143
|
+
if (command === '/stop-all') {
|
|
144
|
+
const captain = resolveCaptainContext({ botId: ctx.botId });
|
|
145
|
+
if (!captain.enabled) {
|
|
146
|
+
if (!ctx.stopAllSessions || !ctx.activeSessions) {
|
|
147
|
+
return '⚠️ stop-all 不可用 (control plane 未启动)';
|
|
148
|
+
}
|
|
149
|
+
const before = ctx.activeSessions();
|
|
150
|
+
if (before.length === 0) {
|
|
151
|
+
return '✅ 当前没有 active run';
|
|
152
|
+
}
|
|
153
|
+
await ctx.stopAllSessions();
|
|
154
|
+
const lines = ['✅ 已向 ' + before.length + ' 个 active run 发送 stop 信号', '─────────────────────────────────────'];
|
|
155
|
+
for (const session of before) {
|
|
156
|
+
const runShort = session.runId ? session.runId.slice(0, 12) : '(no run)';
|
|
157
|
+
lines.push(`- ${session.agentId} / ${runShort} : signal sent`);
|
|
158
|
+
}
|
|
159
|
+
return lines.join('\n');
|
|
160
|
+
}
|
|
161
|
+
const result = captainStopAll({ bots: captain.bots, store: ctx.store, dataDir: captain.dataDir });
|
|
162
|
+
return renderCaptainStopAllCard(result);
|
|
163
|
+
}
|
|
164
|
+
if (command === '/logs') {
|
|
165
|
+
if (!ctx.controlPlane)
|
|
166
|
+
return '⚠️ /logs 不可用 (control plane 未启动)';
|
|
167
|
+
return renderLogs(args, ctx.controlPlane);
|
|
168
|
+
}
|
|
169
|
+
if (command === '/health') {
|
|
170
|
+
if (!ctx.controlPlane)
|
|
171
|
+
return '⚠️ /health 不可用 (control plane 未启动)';
|
|
172
|
+
const captain = resolveCaptainContext({ botId: ctx.botId });
|
|
173
|
+
if (!captain.enabled) {
|
|
174
|
+
return await renderHealth(ctx.controlPlane, ctx.store);
|
|
175
|
+
}
|
|
176
|
+
const otherBots = captain.bots.filter((bot) => bot.botId !== ctx.botId);
|
|
177
|
+
return await renderHealth({ cp: ctx.controlPlane, store: ctx.store, bots: otherBots });
|
|
178
|
+
}
|
|
179
|
+
if (command === '/tasks') {
|
|
180
|
+
if (!ctx.controlPlane)
|
|
181
|
+
return '⚠️ /tasks 不可用 (control plane 未启动)';
|
|
182
|
+
return renderTasks(args, ctx.controlPlane, ctx.registry);
|
|
183
|
+
}
|
|
184
|
+
if (command === '/queue-metrics') {
|
|
185
|
+
if (args.includes('--all'))
|
|
186
|
+
return 'queue-metrics unavailable';
|
|
187
|
+
if (!ctx.queueMetricsSnapshot)
|
|
188
|
+
return 'queue-metrics unavailable';
|
|
189
|
+
return renderQueueMetrics(ctx.queueMetricsSnapshot());
|
|
190
|
+
}
|
|
191
|
+
if (command === '/wb' && args[0] === 'history') {
|
|
192
|
+
return handleHistory(args.slice(1), ctx);
|
|
193
|
+
}
|
|
194
|
+
if (command === '/wb' && args[0] === 'resume') {
|
|
195
|
+
return handleResume(args, ctx);
|
|
196
|
+
}
|
|
197
|
+
if (command === '/wb' && args[0] === 'sync') {
|
|
198
|
+
return handleWbSync(args, ctx);
|
|
199
|
+
}
|
|
200
|
+
if (command === '/wb' && args[0] === 'audit') {
|
|
201
|
+
return handleWbAudit(args.slice(1), ctx);
|
|
202
|
+
}
|
|
203
|
+
if (command === '/wb' && args[0] === 'approval') {
|
|
204
|
+
return handleWbApproval(args.slice(1), ctx);
|
|
205
|
+
}
|
|
206
|
+
if (command === '/wb' && args[0] === 'mute') {
|
|
207
|
+
if (!ctx.muteRegistry)
|
|
208
|
+
return '⚠️ /wb mute 不可用 (MuteRegistry 未注入)';
|
|
209
|
+
return handleWbMute(args.slice(1), ctx, ctx.muteRegistry);
|
|
210
|
+
}
|
|
211
|
+
if (command === '/wb' && args[0] === 'unmute') {
|
|
212
|
+
if (!ctx.muteRegistry)
|
|
213
|
+
return '⚠️ /wb unmute 不可用 (MuteRegistry 未注入)';
|
|
214
|
+
return handleWbUnmute(args.slice(1), ctx, ctx.muteRegistry);
|
|
215
|
+
}
|
|
216
|
+
if (command === '/list-bots') {
|
|
217
|
+
const captain = resolveCaptainContext({ botId: ctx.botId });
|
|
218
|
+
if (!captain.enabled) {
|
|
219
|
+
const botId = ctx.controlPlane?.botId ?? 'default';
|
|
220
|
+
const agentNames = ctx.registry.list().map((a) => a.id).join(', ') || '(none)';
|
|
221
|
+
return [
|
|
222
|
+
'📋 已配置 bot (single-bot mode)',
|
|
223
|
+
'─────────────────────────────────────',
|
|
224
|
+
`${botId} (Captain) · agents=[${agentNames}] · 在线 ✓ (本进程)`,
|
|
225
|
+
'─────────────────────────────────────',
|
|
226
|
+
'(当前为 single-bot 模式 · 启用 multi-bot 后会列出全部已配置 bot)',
|
|
227
|
+
].join('\n');
|
|
228
|
+
}
|
|
229
|
+
return renderListBotsCard(captain.bots, ctx.botId);
|
|
230
|
+
}
|
|
231
|
+
if (command === '/use') {
|
|
232
|
+
const agentId = args[0] ?? '';
|
|
233
|
+
if (!ctx.registry.lookup(agentId))
|
|
234
|
+
return `未知 agent: ${agentId}`;
|
|
235
|
+
ctx.store.setCurrentAgent(ctx.botId, ctx.userOpenId, ctx.threadId, agentId);
|
|
236
|
+
const token = ctx.store.getContinuation(ctx.botId, ctx.userOpenId, ctx.threadId, agentId);
|
|
237
|
+
return token ? `已切换到 ${agentId}; 继续之前 ${agentId} 会话` : `已切换到 ${agentId}; ${agentId} 上下文从头开始`;
|
|
238
|
+
}
|
|
239
|
+
if (command === '/now') {
|
|
240
|
+
const binding = ctx.store.getThreadBinding(ctx.botId, ctx.userOpenId, ctx.threadId);
|
|
241
|
+
return `agent: ${binding?.agentId ?? 'claude-code'}\ncwd: ${binding?.cwd ?? '-'}\n上下文: ${binding && ctx.store.getContinuation(ctx.botId, ctx.userOpenId, ctx.threadId, binding.agentId) ? '有' : '无'}`;
|
|
242
|
+
}
|
|
243
|
+
if (command === '/reset-context') {
|
|
244
|
+
const binding = ctx.store.getThreadBinding(ctx.botId, ctx.userOpenId, ctx.threadId);
|
|
245
|
+
const agentId = binding?.agentId ?? 'claude-code';
|
|
246
|
+
const cleared = ctx.store.clearContinuation(ctx.botId, ctx.userOpenId, ctx.threadId, agentId);
|
|
247
|
+
return cleared ? `已清空 ${agentId} 上下文` : `${agentId} 当前没有可清空的上下文`;
|
|
248
|
+
}
|
|
249
|
+
if (command === '/cwd-add') {
|
|
250
|
+
const [name, ...rest] = args;
|
|
251
|
+
const path = rest.join(' ');
|
|
252
|
+
if (!name || !path)
|
|
253
|
+
return 'Usage: /cwd-add <name> <abs-path>';
|
|
254
|
+
// /cwd-add extends the allowlist, so we only check against the denylist (not the allowlist)
|
|
255
|
+
const result = validateCwd(path, [path], CWD_DENYLIST);
|
|
256
|
+
if (!result.ok)
|
|
257
|
+
return `❌ 路径不允许: ${path}. ${result.reason}`;
|
|
258
|
+
ctx.store.setAlias(ctx.userOpenId, name, result.path);
|
|
259
|
+
return `已保存 cwd alias: ${name} -> ${result.path}`;
|
|
260
|
+
}
|
|
261
|
+
if (command === '/cwd-list') {
|
|
262
|
+
const aliases = ctx.store.listAliases(ctx.userOpenId);
|
|
263
|
+
return aliases.length > 0 ? aliases.map((item) => `- ${item.name}: ${item.path}`).join('\n') : '没有已保存的 cwd alias';
|
|
264
|
+
}
|
|
265
|
+
if (command === '/cwd-rm') {
|
|
266
|
+
const name = args[0] ?? '';
|
|
267
|
+
return ctx.store.removeAlias(ctx.userOpenId, name) ? `已删除 alias: ${name}` : `alias 不存在: ${name}`;
|
|
268
|
+
}
|
|
269
|
+
if (command === '/cwd') {
|
|
270
|
+
const raw = args.join(' ');
|
|
271
|
+
if (!raw)
|
|
272
|
+
return 'Usage: /cwd <abs-or-alias>';
|
|
273
|
+
const alias = ctx.store.listAliases(ctx.userOpenId).find((item) => item.name === raw);
|
|
274
|
+
const target = alias?.path ?? raw;
|
|
275
|
+
// If the path came from a saved alias it was already denylist-checked at /cwd-add time;
|
|
276
|
+
// for raw paths apply the full allowlist check so operators must explicitly /cwd-add first.
|
|
277
|
+
const result = alias
|
|
278
|
+
? validateCwd(target, [target], CWD_DENYLIST)
|
|
279
|
+
: validateCwd(target);
|
|
280
|
+
if (!result.ok)
|
|
281
|
+
return `❌ 路径不允许: ${target}. ${result.reason}`;
|
|
282
|
+
ctx.store.setCurrentCwd(ctx.botId, ctx.userOpenId, ctx.threadId, result.path);
|
|
283
|
+
return `已设置 cwd: ${result.path}`;
|
|
284
|
+
}
|
|
285
|
+
if (command === '/stop')
|
|
286
|
+
return '当前没有可停止的运行';
|
|
287
|
+
if (command === '/follow') {
|
|
288
|
+
if (!ctx.activeSessionsForThread || !ctx.setFollowFlag) {
|
|
289
|
+
return '⚠️ /follow 不可用';
|
|
290
|
+
}
|
|
291
|
+
const sessions = ctx.activeSessionsForThread(ctx.userOpenId, ctx.threadId);
|
|
292
|
+
if (sessions.length === 0) {
|
|
293
|
+
return '⚠️ 当前 thread 无 active run, 请先发任务再 `/follow`';
|
|
294
|
+
}
|
|
295
|
+
ctx.setFollowFlag(ctx.threadId);
|
|
296
|
+
return `⏰ /follow 已设置, 当前 thread (${sessions.length} 个 active run) 完成时将额外通知`;
|
|
297
|
+
}
|
|
298
|
+
return `未知命令 ${command}, 支持的命令: /use /list-agents /agents /now /reset-context /stop /stop-all /follow /cwd /cwd-add /cwd-list /cwd-rm /status /logs /health /tasks /queue-metrics /list-bots /help`;
|
|
299
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
function formatElapsed(ms) {
|
|
4
|
+
const total = Math.max(0, Math.floor(ms / 1000));
|
|
5
|
+
const h = Math.floor(total / 3600);
|
|
6
|
+
const m = Math.floor((total % 3600) / 60);
|
|
7
|
+
const s = total % 60;
|
|
8
|
+
if (h > 0)
|
|
9
|
+
return `${h}h${m}m`;
|
|
10
|
+
if (m > 0)
|
|
11
|
+
return `${m}m${s}s`;
|
|
12
|
+
return `${s}s`;
|
|
13
|
+
}
|
|
14
|
+
function readHeartbeat(runDir) {
|
|
15
|
+
const path = join(runDir, 'heartbeat');
|
|
16
|
+
if (!existsSync(path))
|
|
17
|
+
return null;
|
|
18
|
+
try {
|
|
19
|
+
const ts = parseInt(readFileSync(path, 'utf8').trim(), 10);
|
|
20
|
+
if (!Number.isFinite(ts))
|
|
21
|
+
return null;
|
|
22
|
+
return ts * 1000;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function readStatusFile(runDir) {
|
|
29
|
+
const path = join(runDir, 'status');
|
|
30
|
+
if (!existsSync(path))
|
|
31
|
+
return null;
|
|
32
|
+
try {
|
|
33
|
+
return readFileSync(path, 'utf8').trim();
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export function renderStatus(sessions, cp) {
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
const ts = new Date(now).toISOString().replace('T', ' ').slice(0, 19);
|
|
42
|
+
const lines = [];
|
|
43
|
+
lines.push(`📊 Weaverbird Status (single-bot mode, ${ts})`);
|
|
44
|
+
lines.push('─────────────────────────────────────');
|
|
45
|
+
if (sessions.length === 0) {
|
|
46
|
+
lines.push('active runs: 0');
|
|
47
|
+
lines.push('(无 active run)');
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
lines.push(`active runs: ${sessions.length}`);
|
|
51
|
+
for (const session of sessions) {
|
|
52
|
+
const runShort = session.runId ? session.runId.slice(0, 12) : '(no run)';
|
|
53
|
+
const elapsed = formatElapsed(now - session.startedAt);
|
|
54
|
+
const sessionIdPart = session.sessionShortId ? `#${session.sessionShortId} · ` : '';
|
|
55
|
+
const parts = [
|
|
56
|
+
`- ${session.agentId} · ${sessionIdPart}${runShort} · ${elapsed}`,
|
|
57
|
+
];
|
|
58
|
+
if (cp && session.runId) {
|
|
59
|
+
const runDir = join(cp.dataDir, 'runs', cp.botId, session.runId);
|
|
60
|
+
const hbMs = readHeartbeat(runDir);
|
|
61
|
+
if (hbMs !== null) {
|
|
62
|
+
const age = Math.floor((now - hbMs) / 1000);
|
|
63
|
+
parts.push(`heartbeat ${age}s 前`);
|
|
64
|
+
}
|
|
65
|
+
const statusValue = readStatusFile(runDir);
|
|
66
|
+
if (statusValue) {
|
|
67
|
+
parts.push(`status=${statusValue}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
parts.push(`state=${session.terminalState}/${session.stopState}`);
|
|
71
|
+
lines.push(parts.join(' · '));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
lines.push('─────────────────────────────────────');
|
|
75
|
+
lines.push('(single-bot 模式: 仅显示本进程 active runs)');
|
|
76
|
+
return lines.join('\n');
|
|
77
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
const DEFAULT_N = 20;
|
|
4
|
+
const MAX_N = 100;
|
|
5
|
+
function parseTaskArgs(args) {
|
|
6
|
+
let n = DEFAULT_N;
|
|
7
|
+
let agentFilter = null;
|
|
8
|
+
let botIgnored = false;
|
|
9
|
+
let positional = 0;
|
|
10
|
+
for (const arg of args) {
|
|
11
|
+
if (!arg)
|
|
12
|
+
continue;
|
|
13
|
+
if (arg.startsWith('bot=')) {
|
|
14
|
+
botIgnored = true;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (positional === 0 && /^\d+$/.test(arg)) {
|
|
18
|
+
n = Math.min(MAX_N, Math.max(1, parseInt(arg, 10)));
|
|
19
|
+
positional++;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (positional <= 1) {
|
|
23
|
+
agentFilter = arg;
|
|
24
|
+
positional = Math.max(positional, 2);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return { n, agentFilter, botIgnored };
|
|
28
|
+
}
|
|
29
|
+
function emojiForStatus(status) {
|
|
30
|
+
switch (status) {
|
|
31
|
+
case 'done': return '✅';
|
|
32
|
+
case 'stopped': return '⛔';
|
|
33
|
+
case 'error': return '❌';
|
|
34
|
+
case 'running': return '▶️';
|
|
35
|
+
default: return '•';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function shortCwd(cwd) {
|
|
39
|
+
if (!cwd)
|
|
40
|
+
return '-';
|
|
41
|
+
const home = homedir();
|
|
42
|
+
if (cwd.startsWith(home))
|
|
43
|
+
return '~' + cwd.slice(home.length);
|
|
44
|
+
return cwd;
|
|
45
|
+
}
|
|
46
|
+
function formatStartedAt(ts) {
|
|
47
|
+
if (!ts)
|
|
48
|
+
return '--:--';
|
|
49
|
+
const date = new Date(ts);
|
|
50
|
+
if (Number.isNaN(date.getTime()))
|
|
51
|
+
return '--:--';
|
|
52
|
+
const hh = String(date.getHours()).padStart(2, '0');
|
|
53
|
+
const mm = String(date.getMinutes()).padStart(2, '0');
|
|
54
|
+
return `${hh}:${mm}`;
|
|
55
|
+
}
|
|
56
|
+
function durationMin(entry) {
|
|
57
|
+
if (!entry.startedAt)
|
|
58
|
+
return '?min';
|
|
59
|
+
const startMs = new Date(entry.startedAt).getTime();
|
|
60
|
+
if (!Number.isFinite(startMs))
|
|
61
|
+
return '?min';
|
|
62
|
+
const ms = Math.max(0, entry.lastModified - startMs);
|
|
63
|
+
const min = Math.floor(ms / 60000);
|
|
64
|
+
return min < 1 ? '<1min' : `${min}min`;
|
|
65
|
+
}
|
|
66
|
+
export function renderTasks(args, cp, registry) {
|
|
67
|
+
const parsed = parseTaskArgs(args);
|
|
68
|
+
if (parsed.agentFilter && !registry.lookup(parsed.agentFilter)) {
|
|
69
|
+
return `❌ agent ${parsed.agentFilter} 未注册. 用 /list-agents 查看可用 agent.`;
|
|
70
|
+
}
|
|
71
|
+
cp.taskIndex.scan();
|
|
72
|
+
const all = cp.taskIndex.list();
|
|
73
|
+
const filtered = parsed.agentFilter
|
|
74
|
+
? all.filter((entry) => entry.agentId === parsed.agentFilter)
|
|
75
|
+
: all;
|
|
76
|
+
const list = filtered.slice(0, parsed.n);
|
|
77
|
+
const header = [`📋 Task History (近 7 日, tail ${parsed.n}${parsed.agentFilter ? ', agent=' + parsed.agentFilter : ''})`, '─────────────────────────────────────'];
|
|
78
|
+
const body = [];
|
|
79
|
+
if (list.length === 0) {
|
|
80
|
+
body.push('(无任务历史; 单 bot 模式仅扫本进程 runs/ 目录)');
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
for (const entry of list) {
|
|
84
|
+
const start = formatStartedAt(entry.startedAt);
|
|
85
|
+
const runShort = entry.runId.slice(0, 12);
|
|
86
|
+
const cwd = shortCwd(entry.cwd);
|
|
87
|
+
const dur = durationMin(entry);
|
|
88
|
+
const icon = emojiForStatus(entry.status);
|
|
89
|
+
body.push(`${icon} ${start} ${entry.agentId} · ${cwd} · ${dur} · ${entry.status} (${runShort})`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const footer = [
|
|
93
|
+
'─────────────────────────────────────',
|
|
94
|
+
'未列出:',
|
|
95
|
+
'- 当前 active 任务 (用 /status 查)',
|
|
96
|
+
'- 历史详情 (用 /logs 查; 当前仍是本 bot 视角)',
|
|
97
|
+
'- 无 spec.json 的孤儿 run 目录',
|
|
98
|
+
];
|
|
99
|
+
if (parsed.botIgnored) {
|
|
100
|
+
footer.push('');
|
|
101
|
+
footer.push('⚠️ single-bot 模式忽略 bot=<id> 子参数; 启用 multi-bot 后才生效');
|
|
102
|
+
}
|
|
103
|
+
return [...header, ...body, ...footer].join('\n');
|
|
104
|
+
}
|
|
105
|
+
// Helper not exported, kept for future test if needed
|
|
106
|
+
export const __test = { parseTaskArgs, formatStartedAt, durationMin, basename };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { readFileSync, existsSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { approvalGateDir, approvalRequestsPath, approvalResolutionPath } from '../../../../packages/core/src/paths.js';
|
|
4
|
+
function readRequests(dataDir) {
|
|
5
|
+
const path = approvalRequestsPath(dataDir);
|
|
6
|
+
if (!existsSync(path))
|
|
7
|
+
return [];
|
|
8
|
+
const deduped = new Map();
|
|
9
|
+
for (const line of readFileSync(path, 'utf8').split('\n')) {
|
|
10
|
+
if (!line.trim())
|
|
11
|
+
continue;
|
|
12
|
+
try {
|
|
13
|
+
const entry = JSON.parse(line);
|
|
14
|
+
if (entry.op !== 'request_created' && entry.op !== 'request_resolved')
|
|
15
|
+
continue;
|
|
16
|
+
deduped.set(entry.request.requestId, entry.request);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return [...deduped.values()];
|
|
23
|
+
}
|
|
24
|
+
function readResolutions(dataDir) {
|
|
25
|
+
const dir = join(approvalGateDir(dataDir), 'resolutions');
|
|
26
|
+
if (!existsSync(dir))
|
|
27
|
+
return new Map();
|
|
28
|
+
const out = new Map();
|
|
29
|
+
for (const file of readdirSync(dir)) {
|
|
30
|
+
if (!file.endsWith('.json'))
|
|
31
|
+
continue;
|
|
32
|
+
try {
|
|
33
|
+
const raw = JSON.parse(readFileSync(join(dir, file), 'utf8'));
|
|
34
|
+
out.set(raw.requestId, { resolutionReason: raw.reason, resolvedBy: raw.resolvedBy });
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* M13-F: `/wb approval [--all|--pending|--resolved|--expired|--limit N]`
|
|
44
|
+
* IM 侧只读 approval 队列。不发起审批决策(那是卡片按钮),只是把 runbook 列表化,
|
|
45
|
+
* 让 group 里人也能看懂 pending 状态。
|
|
46
|
+
*/
|
|
47
|
+
export function handleWbApproval(args, ctx) {
|
|
48
|
+
const dataDir = ctx.controlPlane?.dataDir;
|
|
49
|
+
if (!dataDir)
|
|
50
|
+
return '⚠️ /wb approval 不可用 (control plane 未启动)';
|
|
51
|
+
const filterAll = args.includes('--all');
|
|
52
|
+
const filterPending = args.includes('--pending');
|
|
53
|
+
const filterResolved = args.includes('--resolved');
|
|
54
|
+
const filterExpired = args.includes('--expired');
|
|
55
|
+
const limitIdx = args.indexOf('--limit');
|
|
56
|
+
const limit = limitIdx >= 0 ? Number.parseInt(args[limitIdx + 1] ?? '', 10) : 20;
|
|
57
|
+
const requests = readRequests(dataDir);
|
|
58
|
+
const resolutions = readResolutions(dataDir);
|
|
59
|
+
const rows = requests
|
|
60
|
+
.map((request) => ({ ...request, resolved: resolutions.get(request.requestId) }))
|
|
61
|
+
.filter((row) => {
|
|
62
|
+
if (filterAll)
|
|
63
|
+
return true;
|
|
64
|
+
if (filterPending)
|
|
65
|
+
return row.status === 'pending_dispatch' || row.status === 'pending_approval';
|
|
66
|
+
if (filterResolved)
|
|
67
|
+
return Boolean(row.resolved);
|
|
68
|
+
if (filterExpired)
|
|
69
|
+
return row.status === 'expired';
|
|
70
|
+
return true;
|
|
71
|
+
})
|
|
72
|
+
.sort((a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt))
|
|
73
|
+
.slice(0, limit);
|
|
74
|
+
if (rows.length === 0)
|
|
75
|
+
return '(没有 approval 记录)';
|
|
76
|
+
return [
|
|
77
|
+
`📋 approval 队列 (${rows.length})`,
|
|
78
|
+
...rows.map((row) => [
|
|
79
|
+
row.createdAt.slice(0, 19).replace('T', ' '),
|
|
80
|
+
row.requestId.slice(0, 8),
|
|
81
|
+
row.anchor,
|
|
82
|
+
row.status,
|
|
83
|
+
row.resolved?.resolutionReason ?? '-',
|
|
84
|
+
].join('\t')),
|
|
85
|
+
].join('\n');
|
|
86
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { readFileSync, existsSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { resolveDataDir, auditMirrorPath, approvalGateDir, approvalRequestsPath } from '../../../../packages/core/src/paths.js';
|
|
3
|
+
/**
|
|
4
|
+
* M13-F (2026-08-04 owner-authorized): IM 侧 audit 平价。
|
|
5
|
+
* `/wb audit [--limit N] [--session-id SID]` 打本机 audit-mirror 最近 N 行。
|
|
6
|
+
* 与 wb-cli audit 共享同一份读取路径,不回远端不发起新 audit 调用。
|
|
7
|
+
*/
|
|
8
|
+
export function handleWbAudit(args, ctx) {
|
|
9
|
+
const limit = Number.parseInt(args[args.indexOf('--limit') + 1] ?? '', 10) || 20;
|
|
10
|
+
const sessionIdx = args.indexOf('--session-id');
|
|
11
|
+
const sessionId = sessionIdx >= 0 ? args[sessionIdx + 1] : undefined;
|
|
12
|
+
const dataDir = ctx.controlPlane?.dataDir;
|
|
13
|
+
if (!dataDir)
|
|
14
|
+
return '⚠️ /wb audit 不可用 (control plane 未启动)';
|
|
15
|
+
const mirror = auditMirrorPath(dataDir);
|
|
16
|
+
if (!existsSync(mirror))
|
|
17
|
+
return '(audit mirror 还没生成)';
|
|
18
|
+
const lines = readFileSync(mirror, 'utf8').split('\n').filter(Boolean);
|
|
19
|
+
const kept = [];
|
|
20
|
+
for (const line of lines) {
|
|
21
|
+
try {
|
|
22
|
+
const record = JSON.parse(line);
|
|
23
|
+
if (sessionId && record['session_id'] !== sessionId)
|
|
24
|
+
continue;
|
|
25
|
+
kept.push(record);
|
|
26
|
+
if (kept.length >= limit)
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (kept.length === 0)
|
|
34
|
+
return '(audit mirror 空)';
|
|
35
|
+
return [
|
|
36
|
+
`🧾 最近 ${kept.length} 条 audit (mirror=${mirror})`,
|
|
37
|
+
...kept.reverse().map((record) => [
|
|
38
|
+
String(record['ts'] ?? '-'),
|
|
39
|
+
String(record['agent_id'] ?? '-'),
|
|
40
|
+
String(record['action'] ?? '-'),
|
|
41
|
+
record['env_filter_hit'] ? 'env' : '',
|
|
42
|
+
String(record['run_id'] ?? '-'),
|
|
43
|
+
String(record['resource'] ?? '-'),
|
|
44
|
+
].filter(Boolean).join(' · ')),
|
|
45
|
+
].join('\n');
|
|
46
|
+
}
|