@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,93 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { groupSyncDispatchStatePath, groupSyncMockCommandsPath } from '../paths.js';
|
|
4
|
+
import { GroupSyncCommandReceivedSchema } from './contracts.js';
|
|
5
|
+
import { createInlineGroupSyncDispatchTransport, dispatchGroupSyncCommand } from './dispatch.js';
|
|
6
|
+
import { syncGroupCommand } from './service.js';
|
|
7
|
+
function atomicWrite(path, content) {
|
|
8
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
9
|
+
const temp = `${path}.tmp`;
|
|
10
|
+
writeFileSync(temp, content);
|
|
11
|
+
renameSync(temp, path);
|
|
12
|
+
}
|
|
13
|
+
function readCommandRecords(path) {
|
|
14
|
+
if (!existsSync(path))
|
|
15
|
+
return [];
|
|
16
|
+
try {
|
|
17
|
+
const parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
18
|
+
return Array.isArray(parsed) ? parsed.map((item) => GroupSyncCommandReceivedSchema.parse(item)) : [];
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function readDispatchState(path) {
|
|
25
|
+
if (!existsSync(path))
|
|
26
|
+
return {};
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(readFileSync(path, 'utf8'));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function writeDispatchState(path, state) {
|
|
35
|
+
atomicWrite(path, JSON.stringify(state, null, 2));
|
|
36
|
+
}
|
|
37
|
+
export function startGroupSyncScheduler(options) {
|
|
38
|
+
const nowFn = options.nowFn ?? Date.now;
|
|
39
|
+
const dispatchTransport = options.dispatchTransport ?? createInlineGroupSyncDispatchTransport(options.onCommand);
|
|
40
|
+
const commandFile = groupSyncMockCommandsPath(options.dataDir);
|
|
41
|
+
const dispatchStateFile = groupSyncDispatchStatePath(options.dataDir);
|
|
42
|
+
let timer = null;
|
|
43
|
+
let inFlight = false;
|
|
44
|
+
async function tick() {
|
|
45
|
+
if (inFlight)
|
|
46
|
+
return;
|
|
47
|
+
inFlight = true;
|
|
48
|
+
try {
|
|
49
|
+
const dispatchState = readDispatchState(dispatchStateFile);
|
|
50
|
+
const now = nowFn();
|
|
51
|
+
const nowIso = new Date(now).toISOString();
|
|
52
|
+
for (const record of readCommandRecords(commandFile)) {
|
|
53
|
+
if (Date.parse(record.scheduled_at) > now)
|
|
54
|
+
continue;
|
|
55
|
+
if (dispatchState[record.request_id])
|
|
56
|
+
continue;
|
|
57
|
+
dispatchState[record.request_id] = nowIso;
|
|
58
|
+
writeDispatchState(dispatchStateFile, dispatchState);
|
|
59
|
+
await syncGroupCommand({
|
|
60
|
+
dataDir: options.dataDir,
|
|
61
|
+
botId: options.botId,
|
|
62
|
+
command: record,
|
|
63
|
+
config: options.config,
|
|
64
|
+
transport: options.transport,
|
|
65
|
+
});
|
|
66
|
+
await dispatchGroupSyncCommand({
|
|
67
|
+
dataDir: options.dataDir,
|
|
68
|
+
botId: options.botId,
|
|
69
|
+
command: record,
|
|
70
|
+
transport: dispatchTransport,
|
|
71
|
+
nowFn,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
inFlight = false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (options.config.enabled) {
|
|
80
|
+
timer = setInterval(() => {
|
|
81
|
+
void tick();
|
|
82
|
+
}, options.config.sweep_interval_ms);
|
|
83
|
+
timer.unref?.();
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
tick,
|
|
87
|
+
stop() {
|
|
88
|
+
if (timer)
|
|
89
|
+
clearInterval(timer);
|
|
90
|
+
timer = null;
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { runGroupSyncStatePath } from '../paths.js';
|
|
4
|
+
import { GroupSyncCommandReceivedSchema, GroupSyncStateSchema } from './contracts.js';
|
|
5
|
+
import { withTimeout } from './transport.js';
|
|
6
|
+
function atomicWrite(path, content) {
|
|
7
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
8
|
+
const temp = `${path}.tmp`;
|
|
9
|
+
writeFileSync(temp, content);
|
|
10
|
+
renameSync(temp, path);
|
|
11
|
+
}
|
|
12
|
+
function readState(path, input) {
|
|
13
|
+
if (!existsSync(path)) {
|
|
14
|
+
return GroupSyncStateSchema.parse({
|
|
15
|
+
request_id: input.request_id,
|
|
16
|
+
session_id: input.session_id,
|
|
17
|
+
run_id: input.run_id,
|
|
18
|
+
revision: input.revision,
|
|
19
|
+
thread_id: input.thread_id ?? null,
|
|
20
|
+
message_id: input.message_id ?? null,
|
|
21
|
+
scheduled_at: input.scheduled_at,
|
|
22
|
+
last_synced_at: null,
|
|
23
|
+
sync_failed: false,
|
|
24
|
+
last_error: null,
|
|
25
|
+
updated_at: new Date().toISOString(),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return GroupSyncStateSchema.parse(JSON.parse(readFileSync(path, 'utf8')));
|
|
29
|
+
}
|
|
30
|
+
function writeState(path, state) {
|
|
31
|
+
atomicWrite(path, JSON.stringify(state, null, 2));
|
|
32
|
+
}
|
|
33
|
+
export async function syncGroupCommand(options) {
|
|
34
|
+
const command = GroupSyncCommandReceivedSchema.parse(options.command);
|
|
35
|
+
const statePath = runGroupSyncStatePath(options.dataDir, command.run_id, options.botId ?? 'default');
|
|
36
|
+
const state = readState(statePath, command);
|
|
37
|
+
if (state.run_id === command.run_id
|
|
38
|
+
&& state.revision >= command.revision
|
|
39
|
+
&& state.request_id === command.request_id
|
|
40
|
+
&& state.last_synced_at !== null
|
|
41
|
+
&& state.sync_failed === false) {
|
|
42
|
+
return { sync_failed: false, state };
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
await withTimeout(options.transport.syncCommand(command), options.config.rpc_timeout_ms, 'group sync timeout');
|
|
46
|
+
const nextState = GroupSyncStateSchema.parse({
|
|
47
|
+
request_id: command.request_id,
|
|
48
|
+
session_id: command.session_id,
|
|
49
|
+
run_id: command.run_id,
|
|
50
|
+
revision: command.revision,
|
|
51
|
+
thread_id: command.thread_id ?? null,
|
|
52
|
+
message_id: command.message_id ?? null,
|
|
53
|
+
scheduled_at: command.scheduled_at,
|
|
54
|
+
last_synced_at: new Date().toISOString(),
|
|
55
|
+
sync_failed: false,
|
|
56
|
+
last_error: null,
|
|
57
|
+
updated_at: new Date().toISOString(),
|
|
58
|
+
});
|
|
59
|
+
writeState(statePath, nextState);
|
|
60
|
+
return { sync_failed: false, state: nextState };
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
const nextState = GroupSyncStateSchema.parse({
|
|
64
|
+
request_id: command.request_id,
|
|
65
|
+
session_id: command.session_id,
|
|
66
|
+
run_id: command.run_id,
|
|
67
|
+
revision: command.revision,
|
|
68
|
+
thread_id: command.thread_id ?? null,
|
|
69
|
+
message_id: command.message_id ?? null,
|
|
70
|
+
scheduled_at: command.scheduled_at,
|
|
71
|
+
last_synced_at: null,
|
|
72
|
+
sync_failed: true,
|
|
73
|
+
last_error: error instanceof Error ? error.message : String(error),
|
|
74
|
+
updated_at: new Date().toISOString(),
|
|
75
|
+
});
|
|
76
|
+
writeState(statePath, nextState);
|
|
77
|
+
return { sync_failed: true, state: nextState };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs';
|
|
2
|
+
import { dirname } from 'node:path';
|
|
3
|
+
import { groupSyncMockLedgerPath } from '../paths.js';
|
|
4
|
+
import { appendNdjsonLine } from '../util/ndjson-rotation.js';
|
|
5
|
+
import { GroupSyncAutoDocInputSchema, GroupSyncCommandReceivedSchema, GroupSyncCrossGroupReferenceSchema, GroupSyncReviewDiffReplySchema, } from './contracts.js';
|
|
6
|
+
import { groupSyncMockAutoDocLedgerPath } from './autodoc.js';
|
|
7
|
+
import { groupSyncMockReferenceLedgerPath } from './xref.js';
|
|
8
|
+
import { groupSyncMockReviewDiffLedgerPath } from './revdiff.js';
|
|
9
|
+
export function createMockGroupSyncTransport(dataDir) {
|
|
10
|
+
const ledgerPath = groupSyncMockLedgerPath(dataDir);
|
|
11
|
+
return {
|
|
12
|
+
async syncCommand(input) {
|
|
13
|
+
mkdirSync(dirname(ledgerPath), { recursive: true });
|
|
14
|
+
appendNdjsonLine(ledgerPath, JSON.stringify(GroupSyncCommandReceivedSchema.parse(input)));
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function withTimeout(promise, timeoutMs, message) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const timer = setTimeout(() => reject(new Error(message)), timeoutMs);
|
|
21
|
+
void promise.then((value) => {
|
|
22
|
+
clearTimeout(timer);
|
|
23
|
+
resolve(value);
|
|
24
|
+
}, (error) => {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
reject(error);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export function createGuardedCliGroupSyncTransport() {
|
|
31
|
+
return {
|
|
32
|
+
async syncCommand() {
|
|
33
|
+
throw new Error('group sync real egress requires explicit real-device authorization');
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export function createMockGroupSyncReferenceTransport(dataDir) {
|
|
38
|
+
const ledgerPath = groupSyncMockReferenceLedgerPath(dataDir);
|
|
39
|
+
return {
|
|
40
|
+
async publishReference(input) {
|
|
41
|
+
mkdirSync(dirname(ledgerPath), { recursive: true });
|
|
42
|
+
appendNdjsonLine(ledgerPath, JSON.stringify(GroupSyncCrossGroupReferenceSchema.parse(input)));
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function createGuardedCliGroupSyncReferenceTransport() {
|
|
47
|
+
return {
|
|
48
|
+
async publishReference() {
|
|
49
|
+
throw new Error('group sync cross-group reference egress requires explicit real-device authorization');
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export function createMockGroupSyncReviewDiffTransport(dataDir) {
|
|
54
|
+
const ledgerPath = groupSyncMockReviewDiffLedgerPath(dataDir);
|
|
55
|
+
return {
|
|
56
|
+
async publishReviewDiff(input) {
|
|
57
|
+
mkdirSync(dirname(ledgerPath), { recursive: true });
|
|
58
|
+
appendNdjsonLine(ledgerPath, JSON.stringify(GroupSyncReviewDiffReplySchema.parse(input)));
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export function createGuardedCliGroupSyncReviewDiffTransport() {
|
|
63
|
+
return {
|
|
64
|
+
async publishReviewDiff() {
|
|
65
|
+
throw new Error('group sync review diff reply egress requires explicit real-device authorization');
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export function createMockGroupSyncAutoDocTransport(dataDir) {
|
|
70
|
+
const ledgerPath = groupSyncMockAutoDocLedgerPath(dataDir);
|
|
71
|
+
return {
|
|
72
|
+
async create(input) {
|
|
73
|
+
const url = `https://mock.larkoffice.com/suite/docx/${encodeURIComponent(input.runId)}`;
|
|
74
|
+
mkdirSync(dirname(ledgerPath), { recursive: true });
|
|
75
|
+
appendNdjsonLine(ledgerPath, JSON.stringify(GroupSyncAutoDocInputSchema.parse({
|
|
76
|
+
request_id: `mock-${input.runId}`,
|
|
77
|
+
session_id: `mock-${input.runId}`,
|
|
78
|
+
run_id: input.runId,
|
|
79
|
+
revision: 0,
|
|
80
|
+
artifact_id: input.runId,
|
|
81
|
+
artifact_label: input.title,
|
|
82
|
+
artifact_path: null,
|
|
83
|
+
sink_run_id: input.runId,
|
|
84
|
+
terminal_status: 'succeeded',
|
|
85
|
+
existing_doc_urls: [url],
|
|
86
|
+
})));
|
|
87
|
+
return { url, rawOutput: url };
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function createGuardedCliGroupSyncAutoDocTransport() {
|
|
92
|
+
return {
|
|
93
|
+
async create() {
|
|
94
|
+
throw new Error('group sync auto doc egress requires explicit real-device authorization');
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { groupSyncDir, runDir } from '../paths.js';
|
|
4
|
+
import { GroupSyncCrossGroupReferenceInputSchema, GroupSyncCrossGroupReferenceSchema, GroupSyncCrossGroupReferenceSetSchema, } from './contracts.js';
|
|
5
|
+
function atomicWrite(path, content) {
|
|
6
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
7
|
+
const temp = `${path}.tmp`;
|
|
8
|
+
writeFileSync(temp, content);
|
|
9
|
+
renameSync(temp, path);
|
|
10
|
+
}
|
|
11
|
+
function withTimeout(promise, timeoutMs, message) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const timer = setTimeout(() => reject(new Error(message)), timeoutMs);
|
|
14
|
+
void promise.then((value) => {
|
|
15
|
+
clearTimeout(timer);
|
|
16
|
+
resolve(value);
|
|
17
|
+
}, (error) => {
|
|
18
|
+
clearTimeout(timer);
|
|
19
|
+
reject(error);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function runGroupSyncReferencesPath(dataDir, runId, botId = 'default') {
|
|
24
|
+
return join(runDir(dataDir, runId, botId), 'group-sync-xrefs.json');
|
|
25
|
+
}
|
|
26
|
+
function readReferenceSet(path, input, nowIso) {
|
|
27
|
+
if (!existsSync(path)) {
|
|
28
|
+
return GroupSyncCrossGroupReferenceSetSchema.parse({
|
|
29
|
+
session_id: input.session_id,
|
|
30
|
+
run_id: input.run_id,
|
|
31
|
+
last_revision: input.revision,
|
|
32
|
+
updated_at: nowIso,
|
|
33
|
+
references: [],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return GroupSyncCrossGroupReferenceSetSchema.parse(JSON.parse(readFileSync(path, 'utf8')));
|
|
37
|
+
}
|
|
38
|
+
function writeReferenceSet(path, state) {
|
|
39
|
+
atomicWrite(path, JSON.stringify(state, null, 2));
|
|
40
|
+
}
|
|
41
|
+
function sanitizeIdPart(value) {
|
|
42
|
+
const raw = value?.trim() ?? '';
|
|
43
|
+
if (raw.length === 0)
|
|
44
|
+
return 'none';
|
|
45
|
+
return raw.replace(/[^a-zA-Z0-9._-]+/g, '_');
|
|
46
|
+
}
|
|
47
|
+
function buildReferenceId(input) {
|
|
48
|
+
return [
|
|
49
|
+
'xref',
|
|
50
|
+
sanitizeIdPart(input.request_id),
|
|
51
|
+
sanitizeIdPart(input.artifact_kind),
|
|
52
|
+
sanitizeIdPart(input.target_thread_id),
|
|
53
|
+
sanitizeIdPart(input.target_message_id ?? null),
|
|
54
|
+
].join(':');
|
|
55
|
+
}
|
|
56
|
+
function toReference(input, nowIso, patch = {}) {
|
|
57
|
+
return GroupSyncCrossGroupReferenceSchema.parse({
|
|
58
|
+
request_id: input.request_id,
|
|
59
|
+
session_id: input.session_id,
|
|
60
|
+
run_id: input.run_id,
|
|
61
|
+
revision: input.revision,
|
|
62
|
+
source_thread_id: input.source_thread_id,
|
|
63
|
+
source_message_id: input.source_message_id ?? null,
|
|
64
|
+
target_thread_id: input.target_thread_id,
|
|
65
|
+
target_message_id: input.target_message_id ?? null,
|
|
66
|
+
artifact_kind: input.artifact_kind,
|
|
67
|
+
artifact_label: input.artifact_label,
|
|
68
|
+
artifact_path: input.artifact_path ?? null,
|
|
69
|
+
reference_id: buildReferenceId(input),
|
|
70
|
+
created_at: patch.created_at ?? nowIso,
|
|
71
|
+
synced_at: patch.synced_at ?? null,
|
|
72
|
+
sync_failed: patch.sync_failed ?? false,
|
|
73
|
+
last_error: patch.last_error ?? null,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function mergeReferenceSet(state, reference, nowIso) {
|
|
77
|
+
const references = state.references.filter((item) => item.reference_id !== reference.reference_id);
|
|
78
|
+
references.push(reference);
|
|
79
|
+
references.sort((left, right) => left.reference_id.localeCompare(right.reference_id));
|
|
80
|
+
return GroupSyncCrossGroupReferenceSetSchema.parse({
|
|
81
|
+
session_id: state.session_id,
|
|
82
|
+
run_id: state.run_id,
|
|
83
|
+
last_revision: Math.max(state.last_revision, reference.revision),
|
|
84
|
+
updated_at: nowIso,
|
|
85
|
+
references,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export function groupSyncMockReferenceLedgerPath(dataDir) {
|
|
89
|
+
return join(groupSyncDir(dataDir), 'mock-xrefs.ndjson');
|
|
90
|
+
}
|
|
91
|
+
export async function publishCrossGroupReference(options) {
|
|
92
|
+
const input = GroupSyncCrossGroupReferenceInputSchema.parse(options.reference);
|
|
93
|
+
const nowFn = options.nowFn ?? Date.now;
|
|
94
|
+
const nowIso = new Date(nowFn()).toISOString();
|
|
95
|
+
const statePath = runGroupSyncReferencesPath(options.dataDir, input.run_id, options.botId ?? 'default');
|
|
96
|
+
const current = readReferenceSet(statePath, input, nowIso);
|
|
97
|
+
const existing = current.references.find((item) => item.reference_id === buildReferenceId(input));
|
|
98
|
+
if (existing && existing.sync_failed === false) {
|
|
99
|
+
return {
|
|
100
|
+
sync_failed: false,
|
|
101
|
+
reference: existing,
|
|
102
|
+
state: current,
|
|
103
|
+
statePath,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
const reference = toReference(input, nowIso, {
|
|
108
|
+
created_at: existing?.created_at ?? nowIso,
|
|
109
|
+
synced_at: nowIso,
|
|
110
|
+
sync_failed: false,
|
|
111
|
+
last_error: null,
|
|
112
|
+
});
|
|
113
|
+
await withTimeout(options.transport.publishReference(reference), options.config.rpc_timeout_ms, 'group sync xref timeout');
|
|
114
|
+
const nextState = mergeReferenceSet(current, reference, nowIso);
|
|
115
|
+
writeReferenceSet(statePath, nextState);
|
|
116
|
+
return {
|
|
117
|
+
sync_failed: false,
|
|
118
|
+
reference,
|
|
119
|
+
state: nextState,
|
|
120
|
+
statePath,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
const reference = toReference(input, nowIso, {
|
|
125
|
+
created_at: existing?.created_at ?? nowIso,
|
|
126
|
+
synced_at: null,
|
|
127
|
+
sync_failed: true,
|
|
128
|
+
last_error: error instanceof Error ? error.message : String(error),
|
|
129
|
+
});
|
|
130
|
+
const nextState = mergeReferenceSet(current, reference, nowIso);
|
|
131
|
+
writeReferenceSet(statePath, nextState);
|
|
132
|
+
return {
|
|
133
|
+
sync_failed: true,
|
|
134
|
+
reference,
|
|
135
|
+
state: nextState,
|
|
136
|
+
statePath,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export * from './agent/index.js';
|
|
2
|
+
export { WbRunner, CURRENT_SCHEMA_VERSION, HEARTBEAT_INTERVAL_MS, STOP_POLL_INTERVAL_MS, STOP_GRACE_MS, validateRunCwd, validateSpecId, } from './runner/wb-runner.js';
|
|
3
|
+
export { replayEventsFromNdjson, SUPPORTED_SCHEMA_VERSION } from './runner/event-replay.js';
|
|
4
|
+
export { startAuditPipe } from './audit-pipe/index.js';
|
|
5
|
+
export * from './audit-pipe/contracts.js';
|
|
6
|
+
export * from './run-registry/index.js';
|
|
7
|
+
export * from './calendar-hook/index.js';
|
|
8
|
+
export * from './group-sync/index.js';
|
|
9
|
+
export * from './periodic-tasks/index.js';
|
|
10
|
+
export * from './approval-gate/contracts.js';
|
|
11
|
+
export * from './approval-gate/matcher.js';
|
|
12
|
+
export * from './approval-gate/audit.js';
|
|
13
|
+
export * from './captain/index.js';
|
|
14
|
+
export * from './paths.js';
|
|
15
|
+
export * from './config/errors.js';
|
|
16
|
+
export * from './config/registry.js';
|
|
17
|
+
export * from './errors/codes.js';
|
|
18
|
+
// M12-D (2026-08-04): IpcClient + error 类, 让 wb-cli `watch`/`attach` 不必走
|
|
19
|
+
// 深层相对路径。不在此处导出 reverse-channel/contracts,因为那些 schema 与
|
|
20
|
+
// captain contracts 互相引用,会造成 runtime circular import;调用方按需
|
|
21
|
+
// `import { IpcMessageSchema } from '../../packages/core/src/reverse-channel/contracts.js'`.
|
|
22
|
+
export { IpcClient } from './reverse-channel/ipc-client.js';
|
|
23
|
+
export { ReverseChannelError, REVERSE_CHANNEL_ERROR_CODES } from './reverse-channel/contracts.js';
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { MinutesContentSchema, MinutesFetchRequestSchema, MinutesRunArtifactSchema, MinutesRunStateSchema, } from './contracts.js';
|
|
5
|
+
import { runMinutesArtifactDir, runMinutesStatePath } from './client.js';
|
|
6
|
+
function atomicWrite(path, content) {
|
|
7
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
8
|
+
const temp = `${path}.tmp`;
|
|
9
|
+
writeFileSync(temp, content);
|
|
10
|
+
renameSync(temp, path);
|
|
11
|
+
}
|
|
12
|
+
function readMinutesState(path, request, nowIso) {
|
|
13
|
+
if (!existsSync(path)) {
|
|
14
|
+
return MinutesRunStateSchema.parse({
|
|
15
|
+
session_id: request.session_id,
|
|
16
|
+
run_id: request.run_id,
|
|
17
|
+
last_revision: request.revision,
|
|
18
|
+
updated_at: nowIso,
|
|
19
|
+
artifacts: [],
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return MinutesRunStateSchema.parse(JSON.parse(readFileSync(path, 'utf8')));
|
|
23
|
+
}
|
|
24
|
+
function writeMinutesState(path, state) {
|
|
25
|
+
atomicWrite(path, JSON.stringify(state, null, 2));
|
|
26
|
+
}
|
|
27
|
+
function sanitizeIdPart(value) {
|
|
28
|
+
return value.trim().replace(/[^a-zA-Z0-9._-]+/g, '_');
|
|
29
|
+
}
|
|
30
|
+
function buildAttachId(request) {
|
|
31
|
+
return ['minutes', sanitizeIdPart(request.minute_id), sanitizeIdPart(request.content_kind)].join(':');
|
|
32
|
+
}
|
|
33
|
+
function buildArtifactPath(dataDir, runId, attachId, botId = 'default') {
|
|
34
|
+
return join(runMinutesArtifactDir(dataDir, runId, botId), `${sanitizeIdPart(attachId)}.md`);
|
|
35
|
+
}
|
|
36
|
+
function buildArtifactBody(request, content) {
|
|
37
|
+
const header = [
|
|
38
|
+
`# ${content.title}`,
|
|
39
|
+
'',
|
|
40
|
+
`- minute_id: ${request.minute_id}`,
|
|
41
|
+
`- run_id: ${request.run_id}`,
|
|
42
|
+
`- session_id: ${request.session_id}`,
|
|
43
|
+
`- revision: ${request.revision}`,
|
|
44
|
+
`- content_kind: ${request.content_kind}`,
|
|
45
|
+
`- source_url: ${content.source_url ?? 'none'}`,
|
|
46
|
+
'',
|
|
47
|
+
];
|
|
48
|
+
return `${header.join('\n')}${content.content.trim()}\n`;
|
|
49
|
+
}
|
|
50
|
+
function mergeMinutesState(state, artifact, nowIso) {
|
|
51
|
+
const artifacts = state.artifacts.filter((item) => item.attach_id !== artifact.attach_id);
|
|
52
|
+
artifacts.push(artifact);
|
|
53
|
+
artifacts.sort((left, right) => left.attach_id.localeCompare(right.attach_id));
|
|
54
|
+
return MinutesRunStateSchema.parse({
|
|
55
|
+
session_id: state.session_id,
|
|
56
|
+
run_id: state.run_id,
|
|
57
|
+
last_revision: Math.max(state.last_revision, artifact.revision),
|
|
58
|
+
updated_at: nowIso,
|
|
59
|
+
artifacts,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function existingSuccessfulArtifact(state, attachId) {
|
|
63
|
+
return state.artifacts.find((item) => item.attach_id === attachId && item.attach_failed === false);
|
|
64
|
+
}
|
|
65
|
+
function toArtifact(request, nowIso, patch) {
|
|
66
|
+
return MinutesRunArtifactSchema.parse({
|
|
67
|
+
request_id: request.request_id,
|
|
68
|
+
session_id: request.session_id,
|
|
69
|
+
run_id: request.run_id,
|
|
70
|
+
revision: request.revision,
|
|
71
|
+
minute_id: request.minute_id,
|
|
72
|
+
content_kind: request.content_kind,
|
|
73
|
+
requested_at: request.requested_at,
|
|
74
|
+
attach_id: buildAttachId(request),
|
|
75
|
+
minute_title: patch.minute_title ?? request.minute_id,
|
|
76
|
+
source_url: patch.source_url ?? null,
|
|
77
|
+
fetched_at: patch.fetched_at ?? null,
|
|
78
|
+
artifact_path: patch.artifact_path ?? null,
|
|
79
|
+
content_digest: patch.content_digest ?? null,
|
|
80
|
+
attached_at: nowIso,
|
|
81
|
+
attach_failed: patch.attach_failed ?? false,
|
|
82
|
+
last_error: patch.last_error ?? null,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
export async function attachMinutesContent(options) {
|
|
86
|
+
const request = MinutesFetchRequestSchema.parse(options.request);
|
|
87
|
+
const content = MinutesContentSchema.parse(options.content);
|
|
88
|
+
const nowFn = options.nowFn ?? Date.now;
|
|
89
|
+
const nowIso = new Date(nowFn()).toISOString();
|
|
90
|
+
const botId = options.botId ?? 'default';
|
|
91
|
+
const statePath = runMinutesStatePath(options.dataDir, request.run_id, botId);
|
|
92
|
+
const current = readMinutesState(statePath, request, nowIso);
|
|
93
|
+
const attachId = buildAttachId(request);
|
|
94
|
+
const existing = existingSuccessfulArtifact(current, attachId);
|
|
95
|
+
if (existing) {
|
|
96
|
+
return {
|
|
97
|
+
attach_failed: false,
|
|
98
|
+
artifact: existing,
|
|
99
|
+
state: current,
|
|
100
|
+
statePath,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const artifactPath = buildArtifactPath(options.dataDir, request.run_id, attachId, botId);
|
|
105
|
+
const body = buildArtifactBody(request, content);
|
|
106
|
+
atomicWrite(artifactPath, body);
|
|
107
|
+
const artifact = toArtifact(request, nowIso, {
|
|
108
|
+
minute_title: content.title,
|
|
109
|
+
source_url: content.source_url,
|
|
110
|
+
fetched_at: content.fetched_at,
|
|
111
|
+
artifact_path: artifactPath,
|
|
112
|
+
content_digest: createHash('sha1').update(body).digest('hex'),
|
|
113
|
+
attach_failed: false,
|
|
114
|
+
last_error: null,
|
|
115
|
+
});
|
|
116
|
+
const nextState = mergeMinutesState(current, artifact, nowIso);
|
|
117
|
+
writeMinutesState(statePath, nextState);
|
|
118
|
+
return {
|
|
119
|
+
attach_failed: false,
|
|
120
|
+
artifact,
|
|
121
|
+
state: nextState,
|
|
122
|
+
statePath,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
const artifact = toArtifact(request, nowIso, {
|
|
127
|
+
minute_title: content.title,
|
|
128
|
+
source_url: content.source_url,
|
|
129
|
+
fetched_at: content.fetched_at,
|
|
130
|
+
artifact_path: null,
|
|
131
|
+
content_digest: null,
|
|
132
|
+
attach_failed: true,
|
|
133
|
+
last_error: error instanceof Error ? error.message : String(error),
|
|
134
|
+
});
|
|
135
|
+
const nextState = mergeMinutesState(current, artifact, nowIso);
|
|
136
|
+
writeMinutesState(statePath, nextState);
|
|
137
|
+
return {
|
|
138
|
+
attach_failed: true,
|
|
139
|
+
artifact,
|
|
140
|
+
state: nextState,
|
|
141
|
+
statePath,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export async function fetchMinutesToRun(options) {
|
|
146
|
+
const request = MinutesFetchRequestSchema.parse(options.request);
|
|
147
|
+
const nowFn = options.nowFn ?? Date.now;
|
|
148
|
+
const nowIso = new Date(nowFn()).toISOString();
|
|
149
|
+
const botId = options.botId ?? 'default';
|
|
150
|
+
const statePath = runMinutesStatePath(options.dataDir, request.run_id, botId);
|
|
151
|
+
const current = readMinutesState(statePath, request, nowIso);
|
|
152
|
+
const attachId = buildAttachId(request);
|
|
153
|
+
const existing = existingSuccessfulArtifact(current, attachId);
|
|
154
|
+
if (existing) {
|
|
155
|
+
return {
|
|
156
|
+
attach_failed: false,
|
|
157
|
+
artifact: existing,
|
|
158
|
+
state: current,
|
|
159
|
+
statePath,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
const content = await options.client.fetchMinutes(request);
|
|
164
|
+
return attachMinutesContent({
|
|
165
|
+
dataDir: options.dataDir,
|
|
166
|
+
botId,
|
|
167
|
+
request,
|
|
168
|
+
content,
|
|
169
|
+
nowFn,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
const artifact = toArtifact(request, nowIso, {
|
|
174
|
+
minute_title: request.minute_id,
|
|
175
|
+
attach_failed: true,
|
|
176
|
+
last_error: error instanceof Error ? error.message : String(error),
|
|
177
|
+
});
|
|
178
|
+
const nextState = mergeMinutesState(current, artifact, nowIso);
|
|
179
|
+
writeMinutesState(statePath, nextState);
|
|
180
|
+
return {
|
|
181
|
+
attach_failed: true,
|
|
182
|
+
artifact,
|
|
183
|
+
state: nextState,
|
|
184
|
+
statePath,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { runDir } from '../paths.js';
|
|
4
|
+
import { appendNdjsonLine } from '../util/ndjson-rotation.js';
|
|
5
|
+
import { MinutesContentSchema, MinutesFetchRequestSchema } from './contracts.js';
|
|
6
|
+
function minutesDir(dataDir) {
|
|
7
|
+
return join(dataDir, 'minutes');
|
|
8
|
+
}
|
|
9
|
+
export function minutesMockFetchLedgerPath(dataDir) {
|
|
10
|
+
return join(minutesDir(dataDir), 'mock-fetch.ndjson');
|
|
11
|
+
}
|
|
12
|
+
export function runMinutesStatePath(dataDir, runId, botId = 'default') {
|
|
13
|
+
return join(runDir(dataDir, runId, botId), 'minutes-context.json');
|
|
14
|
+
}
|
|
15
|
+
export function runMinutesArtifactDir(dataDir, runId, botId = 'default') {
|
|
16
|
+
return join(runDir(dataDir, runId, botId), 'minutes');
|
|
17
|
+
}
|
|
18
|
+
export function createInlineMinutesClient(handler) {
|
|
19
|
+
return {
|
|
20
|
+
async fetchMinutes(request) {
|
|
21
|
+
return MinutesContentSchema.parse(await handler(MinutesFetchRequestSchema.parse(request)));
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export function createMockMinutesClient(dataDir, fixtures) {
|
|
26
|
+
const ledgerPath = minutesMockFetchLedgerPath(dataDir);
|
|
27
|
+
return {
|
|
28
|
+
async fetchMinutes(request) {
|
|
29
|
+
const parsed = MinutesFetchRequestSchema.parse(request);
|
|
30
|
+
mkdirSync(dirname(ledgerPath), { recursive: true });
|
|
31
|
+
appendNdjsonLine(ledgerPath, JSON.stringify(parsed));
|
|
32
|
+
const fixture = fixtures[parsed.minute_id];
|
|
33
|
+
if (!fixture) {
|
|
34
|
+
throw new Error(`mock minutes fixture not found: ${parsed.minute_id}`);
|
|
35
|
+
}
|
|
36
|
+
return MinutesContentSchema.parse(fixture);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function createGuardedMinutesClient() {
|
|
41
|
+
return {
|
|
42
|
+
async fetchMinutes() {
|
|
43
|
+
throw new Error('minutes fetch requires explicit auth and real-device authorization');
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|