personal-bot 0.2.19 → 0.3.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/.claude-plugin/marketplace.json +3 -3
- package/.claude-plugin/plugin.json +2 -5
- package/.mcp.json +11 -0
- package/CONTEXT.md +17 -73
- package/README.md +41 -120
- package/README.zh-CN.md +41 -120
- package/docs/README.md +31 -23
- package/docs/architecture/design.md +74 -0
- package/docs/architecture/extensibility.md +78 -0
- package/docs/architecture/model-context.md +31 -0
- package/docs/architecture/model-instruction-guidelines.md +34 -0
- package/docs/architecture/principles.md +33 -0
- package/docs/guides/capabilities.md +36 -0
- package/docs/guides/cross-harness-workers.md +83 -0
- package/docs/guides/history.md +25 -0
- package/docs/guides/workspace-configuration.md +102 -0
- package/docs/hosts/antigravity.md +52 -0
- package/docs/hosts/claude-code.md +47 -0
- package/docs/hosts/dsh.md +67 -0
- package/docs/hosts/pi.md +49 -0
- package/docs/maintainers/release.md +59 -0
- package/hooks/hooks.json +0 -3
- package/lib/antigravity-plugin/hooks.json +23 -0
- package/lib/antigravity-plugin/lib/claude.d.ts +1 -0
- package/lib/antigravity-plugin/lib/claude.js +13 -0
- package/lib/antigravity-plugin/lib/common/json.d.ts +6 -0
- package/lib/antigravity-plugin/lib/common/json.js +16 -0
- package/lib/antigravity-plugin/lib/common/paths.d.ts +1 -0
- package/lib/antigravity-plugin/lib/common/paths.js +2 -0
- package/lib/antigravity-plugin/lib/common/validation.d.ts +3 -0
- package/lib/antigravity-plugin/lib/common/validation.js +8 -0
- package/lib/antigravity-plugin/lib/common/version.d.ts +3 -0
- package/lib/antigravity-plugin/lib/common/version.js +13 -0
- package/lib/antigravity-plugin/lib/extension/acp-worker.js +11597 -0
- package/lib/antigravity-plugin/lib/extension/mcp.js +2715 -0
- package/lib/antigravity-plugin/lib/extension/plugin-runtime.d.ts +10 -0
- package/lib/antigravity-plugin/lib/extension/plugin-runtime.js +67 -0
- package/lib/antigravity-plugin/lib/integrations/agent-resources.d.ts +16 -0
- package/lib/antigravity-plugin/lib/integrations/agent-resources.js +56 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.d.ts +4 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/antigravity.js +79 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/claude.d.ts +3 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/claude.js +60 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/registry.d.ts +15 -0
- package/lib/antigravity-plugin/lib/integrations/hosts/registry.js +34 -0
- package/lib/antigravity-plugin/lib/model/acp.d.ts +164 -0
- package/lib/antigravity-plugin/lib/model/acp.js +12 -0
- package/lib/antigravity-plugin/lib/model/plugin-host.d.ts +48 -0
- package/lib/antigravity-plugin/lib/model/workspace-config.d.ts +24 -0
- package/lib/antigravity-plugin/lib/model/workspace-config.js +1 -0
- package/lib/antigravity-plugin/lib/model/workspace-mcp.d.ts +51 -0
- package/lib/antigravity-plugin/lib/model/workspace-mcp.js +4 -0
- package/lib/antigravity-plugin/lib/plugin.d.ts +1 -0
- package/lib/antigravity-plugin/lib/plugin.js +17 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-config.d.ts +8 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-config.js +93 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-preparation.d.ts +3 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-preparation.js +26 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.d.ts +8 -0
- package/lib/antigravity-plugin/lib/services/workspace/workspace-skills.js +91 -0
- package/lib/antigravity-plugin/lib/storage/atomic-json.d.ts +11 -0
- package/lib/antigravity-plugin/lib/storage/atomic-json.js +101 -0
- package/lib/antigravity-plugin/mcp_config.json +13 -0
- package/lib/antigravity-plugin/package.json +6 -0
- package/lib/antigravity-plugin/plugin.json +7 -0
- package/lib/antigravity-plugin/resources/agents/coordinator.md +11 -0
- package/{resources/claude → lib/antigravity-plugin/resources}/agents/general-purpose.md +0 -5
- package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -0
- package/lib/antigravity-plugin/resources/prompts/workspace.md +31 -0
- package/lib/antigravity-plugin/resources/references/compute-finance.md +22 -0
- package/lib/antigravity-plugin/resources/references/compute.md +16 -0
- package/lib/antigravity-plugin/resources/references/task-conductor.md +25 -0
- package/lib/antigravity-plugin/resources/templates/workspace-agents.md +21 -0
- package/lib/antigravity-plugin/skills/pbot/SKILL.md +16 -0
- package/lib/antigravity-plugin/skills/project-maintenance/SKILL.md +35 -0
- package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +42 -0
- package/lib/claude.js +4 -11
- package/lib/client.js +14 -531
- package/lib/common/markdown.js +2 -3
- package/lib/common/validation.d.ts +2 -0
- package/lib/common/validation.js +4 -0
- package/lib/extension/acp-member.d.ts +2 -0
- package/lib/extension/acp-member.js +9 -8
- package/lib/extension/acp-worker.js +11578 -121
- package/lib/extension/dsh/acp-member.js +26 -2
- package/lib/extension/dsh/command.js +19 -57
- package/lib/extension/dsh/history.js +2 -4
- package/lib/extension/dsh/management.d.ts +0 -2
- package/lib/extension/dsh/management.js +27 -126
- package/lib/extension/dsh/workspace.d.ts +5 -47
- package/lib/extension/dsh/workspace.js +76 -108
- package/lib/extension/mcp-bridge.d.ts +41 -0
- package/lib/extension/mcp-bridge.js +412 -0
- package/lib/extension/mcp-workers.d.ts +980 -0
- package/lib/extension/mcp-workers.js +882 -0
- package/lib/extension/mcp.js +2715 -0
- package/lib/extension/plugin-runtime.d.ts +10 -0
- package/lib/extension/plugin-runtime.js +67 -0
- package/lib/extension/setup-cli.js +215 -2
- package/lib/integrations/acp/agents.d.ts +10 -11
- package/lib/integrations/acp/agents.js +65 -33
- package/lib/integrations/acp/claude.d.ts +2 -2
- package/lib/integrations/acp/claude.js +5 -3
- package/lib/integrations/acp/connection.d.ts +17 -4
- package/lib/integrations/acp/connection.js +53 -27
- package/lib/integrations/acp/executable.d.ts +3 -0
- package/lib/integrations/acp/executable.js +42 -0
- package/lib/integrations/acp/harnesses/claude.d.ts +2 -0
- package/lib/integrations/acp/harnesses/claude.js +49 -0
- package/lib/integrations/acp/harnesses/dsh.d.ts +2 -0
- package/lib/integrations/acp/harnesses/dsh.js +35 -0
- package/lib/integrations/acp/harnesses/generic.d.ts +13 -0
- package/lib/integrations/acp/harnesses/generic.js +37 -0
- package/lib/integrations/acp/harnesses/pi.d.ts +2 -0
- package/lib/integrations/acp/harnesses/pi.js +34 -0
- package/lib/integrations/acp/harnesses/presets.d.ts +3 -0
- package/lib/integrations/acp/harnesses/presets.js +20 -0
- package/lib/integrations/acp/launch.d.ts +33 -4
- package/lib/integrations/acp/launch.js +60 -48
- package/lib/integrations/acp/mcp.d.ts +6 -0
- package/lib/integrations/acp/mcp.js +40 -0
- package/lib/integrations/acp/model-selection.d.ts +9 -2
- package/lib/integrations/acp/model-selection.js +73 -27
- package/lib/integrations/acp/registry.d.ts +18 -0
- package/lib/integrations/acp/registry.js +50 -0
- package/lib/integrations/agent-resources.d.ts +16 -1
- package/lib/integrations/agent-resources.js +45 -9
- package/lib/integrations/dsh/mcp.d.ts +4 -0
- package/lib/integrations/dsh/mcp.js +28 -0
- package/lib/integrations/dsh/resources.d.ts +3 -3
- package/lib/integrations/dsh/resources.js +21 -12
- package/lib/integrations/dsh/skills.d.ts +5 -1
- package/lib/integrations/dsh/skills.js +10 -6
- package/lib/integrations/hosts/antigravity.d.ts +4 -0
- package/lib/integrations/hosts/antigravity.js +79 -0
- package/lib/integrations/hosts/claude.d.ts +3 -0
- package/lib/integrations/hosts/claude.js +60 -0
- package/lib/integrations/hosts/registry.d.ts +15 -0
- package/lib/integrations/hosts/registry.js +34 -0
- package/lib/model/acp.d.ts +107 -2
- package/lib/model/acp.js +12 -1
- package/lib/model/agent.d.ts +35 -33
- package/lib/model/plugin-host.d.ts +48 -0
- package/lib/model/plugin-host.js +1 -0
- package/lib/model/workspace-config.d.ts +12 -0
- package/lib/model/workspace-mcp.d.ts +51 -0
- package/lib/model/workspace-mcp.js +4 -0
- package/lib/plugin.d.ts +1 -0
- package/lib/plugin.js +17 -0
- package/lib/services/agents/acp-run.d.ts +13 -1
- package/lib/services/agents/acp-run.js +30 -2
- package/lib/services/agents/agent-service.d.ts +35 -120
- package/lib/services/agents/agent-service.js +156 -160
- package/lib/services/installation/setup-service.js +17 -1
- package/lib/services/workspace/workspace-config.d.ts +2 -1
- package/lib/services/workspace/workspace-config.js +39 -7
- package/lib/services/workspace/workspace-mcp.d.ts +14 -0
- package/lib/services/workspace/workspace-mcp.js +200 -0
- package/lib/services/workspace/workspace-skills.d.ts +8 -0
- package/lib/services/workspace/workspace-skills.js +91 -0
- package/lib/storage/acp-files.d.ts +3 -1
- package/lib/storage/acp-files.js +32 -2
- package/lib/storage/acp-history.d.ts +3 -2
- package/lib/storage/acp-history.js +19 -7
- package/lib/storage/agent-files.d.ts +8 -8
- package/lib/storage/agent-files.js +27 -47
- package/lib/tools/history/contract.d.ts +1 -1
- package/lib/tools/history/contract.js +17 -11
- package/lib/ui/dsh/index.js +8 -3
- package/lib/ui/dsh/management.d.ts +1 -1
- package/lib/ui/dsh/management.js +24 -456
- package/lib/ui/dsh/remote.d.ts +0 -2
- package/lib/ui/dsh/remote.js +1 -1
- package/package.json +12 -7
- package/resources/agents/coordinator.md +2 -2
- package/resources/antigravity/skills/pbot/SKILL.md +16 -0
- package/resources/claude/skills/pbot/SKILL.md +1 -1
- package/resources/prompts/history-coordinator.md +1 -0
- package/resources/prompts/plugin-coordinator.md +1 -0
- package/resources/prompts/workspace.md +9 -8
- package/resources/references/compute.md +9 -11
- package/resources/references/task-conductor.md +12 -26
- package/resources/skills/project-maintenance/SKILL.md +17 -22
- package/resources/skills/provider-connections/SKILL.md +24 -40
- package/resources/templates/workspace-agents.md +8 -0
- package/scripts/README.md +17 -34
- package/src/claude.ts +4 -8
- package/src/common/markdown.ts +2 -3
- package/src/common/validation.ts +6 -0
- package/src/extension/acp-member.ts +9 -8
- package/src/extension/acp-worker.ts +239 -97
- package/src/extension/command.ts +38 -36
- package/src/extension/dsh/acp-member.ts +21 -2
- package/src/extension/dsh/command.ts +21 -51
- package/src/extension/dsh/files.ts +1 -0
- package/src/extension/dsh/history.ts +9 -6
- package/src/extension/dsh/management.ts +27 -58
- package/src/extension/dsh/workspace.ts +88 -99
- package/src/extension/events.ts +13 -10
- package/src/extension/history.ts +2 -50
- package/src/extension/lifecycle.ts +37 -22
- package/src/extension/mcp-bridge.ts +412 -0
- package/src/extension/mcp-workers.ts +941 -0
- package/src/extension/pi-acp-member.ts +29 -5
- package/src/extension/plugin-runtime.ts +63 -0
- package/src/extension/setup-cli.ts +186 -2
- package/src/integrations/acp/agents.ts +70 -24
- package/src/integrations/acp/claude.ts +11 -4
- package/src/integrations/acp/connection.ts +63 -25
- package/src/integrations/acp/executable.ts +41 -0
- package/src/integrations/acp/harnesses/claude.ts +50 -0
- package/src/integrations/acp/harnesses/dsh.ts +40 -0
- package/src/integrations/acp/harnesses/generic.ts +52 -0
- package/src/integrations/acp/harnesses/pi.ts +35 -0
- package/src/integrations/acp/harnesses/presets.ts +22 -0
- package/src/integrations/acp/launch.ts +69 -47
- package/src/integrations/acp/mcp.ts +43 -0
- package/src/integrations/acp/model-selection.ts +68 -25
- package/src/integrations/acp/registry.ts +60 -0
- package/src/integrations/agent-resources.ts +70 -6
- package/src/integrations/dsh/mcp.ts +30 -0
- package/src/integrations/dsh/resources.ts +28 -13
- package/src/integrations/dsh/skills.ts +16 -6
- package/src/integrations/hosts/antigravity.ts +83 -0
- package/src/integrations/hosts/claude.ts +62 -0
- package/src/integrations/hosts/registry.ts +42 -0
- package/src/integrations/pi/mcp.ts +26 -0
- package/src/mcp.ts +14 -0
- package/src/model/acp.ts +113 -3
- package/src/model/agent.ts +18 -14
- package/src/model/plugin-host.ts +39 -0
- package/src/model/workspace-config.ts +14 -0
- package/src/model/workspace-mcp.ts +64 -0
- package/src/pbot.ts +69 -20
- package/src/plugin.ts +19 -0
- package/src/services/agents/acp-run.ts +35 -3
- package/src/services/agents/agent-service.ts +180 -160
- package/src/services/installation/setup-service.ts +17 -2
- package/src/services/workspace/workspace-config.ts +42 -6
- package/src/services/workspace/workspace-mcp.ts +242 -0
- package/src/services/workspace/workspace-skills.ts +78 -0
- package/src/storage/acp-files.ts +32 -3
- package/src/storage/acp-history.ts +18 -7
- package/src/storage/agent-files.ts +27 -41
- package/src/tools/history/contract.ts +17 -11
- package/src/tools/register.ts +4 -8
- package/src/ui/dsh/index.ts +8 -3
- package/src/ui/dsh/management.ts +40 -282
- package/src/ui/dsh/remote.ts +2 -2
- package/docs/adr/0001-claude-skill-discovery.md +0 -3
- package/docs/adr/0002-claude-native-collaboration.md +0 -3
- package/docs/agent-collaboration.md +0 -190
- package/docs/architecture-guidelines.md +0 -37
- package/docs/capability-design.md +0 -55
- package/docs/claude-code-design.md +0 -44
- package/docs/claude-code.md +0 -57
- package/docs/dsh.md +0 -337
- package/docs/file-storage.md +0 -97
- package/docs/history.md +0 -54
- package/docs/installation-design.md +0 -86
- package/docs/interaction-design.md +0 -21
- package/docs/model-context.md +0 -76
- package/docs/model-instruction-guidelines.md +0 -51
- package/docs/release.md +0 -108
- package/docs/technical-design.md +0 -163
- package/docs/workspace-configuration.md +0 -79
- package/lib/extension/claude-hooks.d.ts +0 -9
- package/lib/extension/claude-hooks.js +0 -78
- package/lib/extension/dsh/agent-events.d.ts +0 -24
- package/lib/extension/dsh/agent-events.js +0 -242
- package/lib/extension/dsh/agent-runtime.d.ts +0 -49
- package/lib/extension/dsh/agent-runtime.js +0 -351
- package/lib/integrations/dsh/dsh-agents.d.ts +0 -30
- package/lib/integrations/dsh/dsh-agents.js +0 -104
- package/lib/integrations/dsh/input.d.ts +0 -21
- package/lib/integrations/dsh/input.js +0 -62
- package/lib/integrations/dsh/notifications.d.ts +0 -8
- package/lib/integrations/dsh/notifications.js +0 -55
- package/lib/integrations/dsh/session.d.ts +0 -14
- package/lib/integrations/dsh/session.js +0 -60
- package/lib/integrations/team-context.d.ts +0 -18
- package/lib/integrations/team-context.js +0 -61
- package/lib/model/team.d.ts +0 -85
- package/lib/services/agents/agent-notices.d.ts +0 -13
- package/lib/services/agents/agent-notices.js +0 -25
- package/lib/services/agents/start-options.d.ts +0 -4
- package/lib/services/agents/start-options.js +0 -20
- package/lib/services/agents/team-service.d.ts +0 -168
- package/lib/services/agents/team-service.js +0 -345
- package/lib/storage/team-files.d.ts +0 -16
- package/lib/storage/team-files.js +0 -111
- package/lib/tools/agents/actions.d.ts +0 -62
- package/lib/tools/agents/actions.js +0 -62
- package/lib/tools/agents/contract.d.ts +0 -181
- package/lib/tools/agents/contract.js +0 -84
- package/lib/tools/agents/dsh-tool.d.ts +0 -5
- package/lib/tools/agents/dsh-tool.js +0 -34
- package/lib/tools/result.d.ts +0 -18
- package/lib/tools/result.js +0 -10
- package/lib/tools/teams/actions.d.ts +0 -23
- package/lib/tools/teams/actions.js +0 -40
- package/lib/tools/teams/contract.d.ts +0 -201
- package/lib/tools/teams/contract.js +0 -91
- package/lib/tools/teams/dsh-tool.d.ts +0 -8
- package/lib/tools/teams/dsh-tool.js +0 -39
- package/lib/ui/dsh/attachments.d.ts +0 -5
- package/lib/ui/dsh/attachments.js +0 -39
- package/resources/prompts/claude-coordinator.md +0 -1
- package/resources/prompts/native-coordinator.md +0 -1
- package/resources/prompts/team-member.md +0 -7
- package/src/extension/agent-events.ts +0 -210
- package/src/extension/agents-command.ts +0 -89
- package/src/extension/claude-hooks.ts +0 -74
- package/src/extension/dsh/agent-events.ts +0 -148
- package/src/extension/dsh/agent-runtime.ts +0 -252
- package/src/extension/subagent-entry.ts +0 -270
- package/src/extension/teams-command.ts +0 -67
- package/src/integrations/dsh/dsh-agents.ts +0 -97
- package/src/integrations/dsh/input.ts +0 -66
- package/src/integrations/dsh/notifications.ts +0 -46
- package/src/integrations/pi/agent-context.ts +0 -18
- package/src/integrations/pi/agent-input.ts +0 -19
- package/src/integrations/pi/pi-agents.ts +0 -84
- package/src/integrations/pi/team-context.ts +0 -26
- package/src/integrations/team-context.ts +0 -64
- package/src/model/team.ts +0 -29
- package/src/services/agents/agent-notices.ts +0 -28
- package/src/services/agents/start-options.ts +0 -19
- package/src/services/agents/team-service.ts +0 -275
- package/src/storage/team-files.ts +0 -93
- package/src/tools/agents/actions.ts +0 -57
- package/src/tools/agents/contract.ts +0 -101
- package/src/tools/agents/dsh-tool.ts +0 -37
- package/src/tools/agents/tool.ts +0 -20
- package/src/tools/teams/actions.ts +0 -34
- package/src/tools/teams/contract.ts +0 -109
- package/src/tools/teams/dsh-tool.ts +0 -45
- package/src/tools/teams/tool.ts +0 -29
- package/src/ui/agent-display.ts +0 -14
- package/src/ui/dsh/attachments.ts +0 -42
- package/src/ui/pi/menu.ts +0 -37
- package/src/ui/pi/reader.ts +0 -46
- /package/lib/{model/team.js → antigravity-plugin/lib/model/plugin-host.js} +0 -0
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process";
|
|
2
|
-
import { fileURLToPath } from "node:url";
|
|
3
|
-
import { existsSync, lstatSync, readFileSync } from "node:fs";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { CURRENT_SESSION_VERSION, getAgentDir, getPackageDir, parseSessionEntries, SessionManager } from "@earendil-works/pi-coding-agent";
|
|
6
|
-
import { requireThat } from "../../common/validation.js";
|
|
7
|
-
import type { AgentDescriptor, AgentOptions, AgentSessionReference } from "../../model/agent.js";
|
|
8
|
-
import type { AgentHost } from "../../services/agents/agent-service.js";
|
|
9
|
-
|
|
10
|
-
export const agentDirectory = () => join(getAgentDir(), "pbot/subagents");
|
|
11
|
-
export const tmuxName = (id: string) => "pbot-" + id;
|
|
12
|
-
// Read through the native parser and tree without opening a writable manager or migrating files.
|
|
13
|
-
export function readAgentSession(d: AgentDescriptor, file: string, strict = false) {
|
|
14
|
-
requireThat(lstatSync(file).isFile(), "原生会话必须是普通文件。");
|
|
15
|
-
const content = readFileSync(file, "utf8"), entries = parseSessionEntries(content), header = entries[0];
|
|
16
|
-
// Report reads can race an append after their fixed leaf. Resume must not discard malformed history.
|
|
17
|
-
if (strict) requireThat(entries.length === content.split("\n").filter(line => line.trim()).length, "原生会话包含无效记录。");
|
|
18
|
-
requireThat(header?.type === "session" && header.version === CURRENT_SESSION_VERSION &&
|
|
19
|
-
header.id === d.agentId && header.cwd === d.cwd, "原生会话身份或工作目录不匹配。");
|
|
20
|
-
return SessionManager.inMemory(d.cwd, undefined, entries);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class PiAgents implements AgentHost {
|
|
24
|
-
readonly kind = "pi";
|
|
25
|
-
private file(reference: AgentSessionReference) {
|
|
26
|
-
requireThat(reference.host === "pi", "不能接管其他宿主的会话。");
|
|
27
|
-
return reference.file;
|
|
28
|
-
}
|
|
29
|
-
validateOptions(options: AgentOptions) {
|
|
30
|
-
requireThat(options.thinking === undefined || ["off", "minimal", "low", "medium", "high", "xhigh", "max"].includes(options.thinking), "Pi 思考级别无效。");
|
|
31
|
-
}
|
|
32
|
-
available() {
|
|
33
|
-
requireThat(spawnSync("tmux", ["-V"], { encoding: "utf8" }).status === 0, "子会话需要 tmux。");
|
|
34
|
-
requireThat(existsSync(join(getPackageDir(), "dist/cli.js")), "无法定位 Pi CLI。");
|
|
35
|
-
}
|
|
36
|
-
alive(id: string) { return spawnSync("tmux", ["has-session", "-t", "=" + tmuxName(id)], { stdio: "ignore" }).status === 0; }
|
|
37
|
-
release(id: string, _generation: string) { spawnSync("tmux", ["kill-session", "-t", "=" + tmuxName(id)], { stdio: "ignore" }); }
|
|
38
|
-
validateSession(d: AgentDescriptor, reference: AgentSessionReference) {
|
|
39
|
-
const file = this.file(reference); if (file !== null) readAgentSession(d, file, true);
|
|
40
|
-
}
|
|
41
|
-
launch(d: AgentDescriptor, directory: string, generation: string, reference: AgentSessionReference, _resume: boolean, signal: AbortSignal) {
|
|
42
|
-
signal.throwIfAborted();
|
|
43
|
-
const sessionFile = this.file(reference);
|
|
44
|
-
this.validateSession(d, reference);
|
|
45
|
-
const o = d.options;
|
|
46
|
-
const entry = fileURLToPath(new URL("../../extension/subagent-entry.ts", import.meta.url));
|
|
47
|
-
const compiled = fileURLToPath(new URL("../../extension/subagent-entry.js", import.meta.url));
|
|
48
|
-
const args = [...(sessionFile === null ? ["--session-id", d.agentId] : ["--session", sessionFile]),
|
|
49
|
-
"--extension", existsSync(entry) ? entry : compiled];
|
|
50
|
-
if (o.noExtensions !== false) args.push("--no-extensions",
|
|
51
|
-
"--extension", "builtin:mcp", "--extension", "builtin:codemode", "--extension", "builtin:tool-search");
|
|
52
|
-
if (o.noSkills) args.push("--no-skills");
|
|
53
|
-
if (o.noPromptTemplates) args.push("--no-prompt-templates");
|
|
54
|
-
if (o.noContextFiles) args.push("--no-context-files");
|
|
55
|
-
// Resume restores native model choices. Explicit model options apply only to a new transcript.
|
|
56
|
-
if (sessionFile === null) {
|
|
57
|
-
args.push("--provider", o.provider!, "--model", o.model!);
|
|
58
|
-
if (o.thinking) args.push("--thinking", o.thinking);
|
|
59
|
-
}
|
|
60
|
-
if (o.tools) args.push("--tools", o.tools.join(","));
|
|
61
|
-
const environment: NodeJS.ProcessEnv = { ...process.env, PWD: d.cwd,
|
|
62
|
-
PBOT_SUBAGENT_RUN_ID: d.agentId, PBOT_SUBAGENT_DIR: directory, PBOT_SUBAGENT_GENERATION: generation,
|
|
63
|
-
PI_CODING_AGENT_DIR: getAgentDir() };
|
|
64
|
-
const terminalKeys = ["TERM", "TMUX", "TMUX_PANE", "TERM_PROGRAM", "TERM_PROGRAM_VERSION"];
|
|
65
|
-
for (const key of terminalKeys) delete environment[key];
|
|
66
|
-
const cached = spawnSync("tmux", ["show-environment", "-g"], { encoding: "utf8" }).stdout ?? "";
|
|
67
|
-
const removed = cached.split("\n").map(line => line.replace(/^-/, "").split("=", 1)[0]!)
|
|
68
|
-
.filter(key => key && environment[key] === undefined && !terminalKeys.includes(key));
|
|
69
|
-
// Session environment values stay out of the pane command; clear values cached only by the server.
|
|
70
|
-
const result = spawnSync("tmux", ["new-session", "-d", "-s", tmuxName(d.agentId), "-x", "120", "-y", "40", "-c", d.cwd,
|
|
71
|
-
...Object.entries(environment).flatMap(([key, value]) => value === undefined ? [] : ["-e", `${key}=${value}`]), "--",
|
|
72
|
-
"/usr/bin/env", ...removed.flatMap(key => ["-u", key]), process.execPath, join(getPackageDir(), "dist/cli.js"), ...args], { encoding: "utf8" });
|
|
73
|
-
requireThat(result.status === 0, result.stderr || "子会话启动失败。");
|
|
74
|
-
}
|
|
75
|
-
report(d: AgentDescriptor, reference: AgentSessionReference, leaf: string) {
|
|
76
|
-
const file = this.file(reference);
|
|
77
|
-
requireThat(file !== null, "完成记录缺少 Pi 原生会话文件引用。");
|
|
78
|
-
const manager = readAgentSession(d, file);
|
|
79
|
-
requireThat(manager.getEntry(leaf), "报告叶节点不存在。");
|
|
80
|
-
const last = manager.getBranch(leaf).filter(e => e.type === "message" && e.message.role === "assistant").at(-1);
|
|
81
|
-
return last?.type === "message" && last.message.role === "assistant"
|
|
82
|
-
? last.message.content.filter(p => p.type === "text").map(p => p.text).join("\n") : "";
|
|
83
|
-
}
|
|
84
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { CustomMessageEntryDraft, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { TeamLink } from "../../model/team.js";
|
|
3
|
-
import { createTeamContext } from "../team-context.js";
|
|
4
|
-
|
|
5
|
-
/** Branch-local context, not a durable read receipt. */
|
|
6
|
-
export function teamContext(link: TeamLink, ctx: ExtensionContext, throughSeq = link.team.messages.length): CustomMessageEntryDraft | undefined {
|
|
7
|
-
const seen = new Set<number>();
|
|
8
|
-
for (const entry of ctx.sessionManager.getBranch()) {
|
|
9
|
-
if (entry.type !== "custom_message" || entry.customType !== "pbot.team-context") continue;
|
|
10
|
-
const data = entry.details as { teamId?: string; seqs?: number[] } | undefined;
|
|
11
|
-
if (data?.teamId === link.team.teamId) for (const seq of data.seqs ?? []) seen.add(seq);
|
|
12
|
-
}
|
|
13
|
-
const value = createTeamContext(link, seen, throughSeq);
|
|
14
|
-
if (!value) return;
|
|
15
|
-
return { type: "custom_message", customType: "pbot.team-context", display: true,
|
|
16
|
-
content: value.content, details: { teamId: link.team.teamId, seqs: value.seqs } };
|
|
17
|
-
}
|
|
18
|
-
export function deliveredTeams(ctx: ExtensionContext) {
|
|
19
|
-
const keys = new Set<string>();
|
|
20
|
-
for (const entry of ctx.sessionManager.getBranch()) {
|
|
21
|
-
if (entry.type !== "custom_message" || entry.customType !== "pbot.team-results") continue;
|
|
22
|
-
const details = entry.details as { keys?: unknown } | undefined;
|
|
23
|
-
if (Array.isArray(details?.keys)) for (const key of details.keys) if (typeof key === "string") keys.add(key);
|
|
24
|
-
}
|
|
25
|
-
return keys;
|
|
26
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import type { TeamLink, TeamMessage } from "../model/team.js";
|
|
4
|
-
import { markdownBlock } from "../common/markdown.js";
|
|
5
|
-
import { resourceRoot } from "../common/paths.js";
|
|
6
|
-
|
|
7
|
-
type MessagePage = Pick<TeamMessage, "seq" | "author" | "text" | "replyTo" | "runId" | "source"> & {
|
|
8
|
-
offset: number; totalLength: number; nextOffset: number | null;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
function messageMarkdown(message: MessagePage): string {
|
|
12
|
-
return [
|
|
13
|
-
"## 消息 " + message.seq, "",
|
|
14
|
-
"- 作者:" + message.author.name + "(" + message.author.kind + ")",
|
|
15
|
-
...(message.author.agentId ? ["- `agentId`: `" + message.author.agentId + "`"] : []),
|
|
16
|
-
...(message.replyTo === undefined ? [] : ["- `replyTo`: `" + message.replyTo + "`"]),
|
|
17
|
-
...(message.runId ? ["- `runId`: `" + message.runId + "`"] : []),
|
|
18
|
-
...(message.source ? ["- `requestId`: `" + message.source.requestId + "`", "- `cycleId`: `" + message.source.cycleId + "`"] : []),
|
|
19
|
-
"- `offset`: `" + message.offset + "`;`totalLength`: `" + message.totalLength + "`;`nextOffset`: `" + message.nextOffset + "`",
|
|
20
|
-
"", markdownBlock(message.text),
|
|
21
|
-
...(message.nextOffset === null ? [] : ["", "正文未完。用 pbot_team_message read 续读:`seq: " + message.seq + ", offset: " + message.nextOffset + "`。"]),
|
|
22
|
-
].join("\n");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function teamMessagesMarkdown(page: { teamId: string; messages: MessagePage[]; lastSeq: number; nextAfterSeq: number | null }): string {
|
|
26
|
-
return ["# 团队公共消息", "", "- `teamId`: `" + page.teamId + "`", "- `lastSeq`: `" + page.lastSeq + "`",
|
|
27
|
-
"- `nextAfterSeq`: `" + page.nextAfterSeq + "`", "",
|
|
28
|
-
"nextOffset 非空时先续读该条正文,再读取后续消息。", "",
|
|
29
|
-
page.messages.map(messageMarkdown).join("\n\n")].join("\n");
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function teamBrief(link: TeamLink): string {
|
|
33
|
-
return ["# 团队协作", "", "- 团队:" + link.team.name, "- `teamId`: `" + link.team.teamId + "`",
|
|
34
|
-
"- 当前成员 `agentId`: `" + link.request.agentId + "`", "- `runId`: `" + link.run.runId + "`",
|
|
35
|
-
"- 轮次:" + link.request.round + " / " + link.run.rounds, "", "## 共同目标", "", link.team.brief,
|
|
36
|
-
"", "## 成员职责", "", ...link.team.members.flatMap(member => ["### " + member.agentId, "", member.role, ""]),
|
|
37
|
-
readFileSync(join(resourceRoot, "prompts/team-member.md"), "utf8").trim()].join("\n");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function createTeamContext(link: TeamLink, seen: ReadonlySet<number>, throughSeq = link.team.messages.length) {
|
|
41
|
-
const pending = link.team.messages.filter(m => m.seq <= throughSeq && !seen.has(m.seq));
|
|
42
|
-
if (!pending.length) return;
|
|
43
|
-
// Count rendered Markdown, including fences and read positions; reserve room for the document header.
|
|
44
|
-
let budget = 15000;
|
|
45
|
-
const messages: MessagePage[] = [];
|
|
46
|
-
for (const m of pending) {
|
|
47
|
-
const part = (length: number): MessagePage => ({ ...m, text: m.text.slice(0, length),
|
|
48
|
-
offset: 0, totalLength: m.text.length, nextOffset: length < m.text.length ? length : null });
|
|
49
|
-
let low = 0, high = Math.min(m.text.length, budget);
|
|
50
|
-
while (low < high) {
|
|
51
|
-
const mid = Math.ceil((low + high) / 2);
|
|
52
|
-
if (messageMarkdown(part(mid)).length + 2 <= budget) low = mid; else high = mid - 1;
|
|
53
|
-
}
|
|
54
|
-
const value = part(low), size = messageMarkdown(value).length + 2;
|
|
55
|
-
if (size > budget || (!low && m.text.length)) break;
|
|
56
|
-
messages.push(value); budget -= size;
|
|
57
|
-
}
|
|
58
|
-
const seqs = messages.map(m => m.seq);
|
|
59
|
-
return { content: ["# 团队公共上下文", "", "- `teamId`: `" + link.team.teamId + "`", "- `throughSeq`: `" + throughSeq + "`",
|
|
60
|
-
"- `remainingFromSeq`: `" + (pending[messages.length]?.seq ?? null) + "`", "",
|
|
61
|
-
"未包含或被截断的正文用 pbot_team_message read 读取。remainingFromSeq 非空时,用 afterSeq: remainingFromSeq - 1 读取未包含的消息。", "",
|
|
62
|
-
messages.map(messageMarkdown).join("\n\n")].join("\n"),
|
|
63
|
-
seqs };
|
|
64
|
-
}
|
package/src/model/team.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { AgentOwner, AgentView } from "./agent.js";
|
|
2
|
-
|
|
3
|
-
export interface TeamRequest {
|
|
4
|
-
requestId: string; agentId: string; generation: string; round: number; throughSeq: number;
|
|
5
|
-
}
|
|
6
|
-
export interface TeamRun {
|
|
7
|
-
runId: string; instruction: string; mode: "roundRobin" | "parallel"; rounds: number;
|
|
8
|
-
maxConcurrent: number; deadlineAt: number; generations: Record<string, string>;
|
|
9
|
-
state: "running" | "paused" | "finished"; reason?: string; issued: TeamRequest[];
|
|
10
|
-
}
|
|
11
|
-
export interface TeamMessage {
|
|
12
|
-
seq: number; operationId: string;
|
|
13
|
-
author: { kind: "user" | "coordinator" | "member"; agentId?: string; name: string };
|
|
14
|
-
text: string; replyTo?: number; runId?: string; source?: { requestId: string; cycleId: string };
|
|
15
|
-
}
|
|
16
|
-
export interface TeamRecord {
|
|
17
|
-
teamId: string; release: string; workspace: string; parentSessionId: string; parentLeaf: string | null;
|
|
18
|
-
name: string; brief: string; closed: boolean; members: { agentId: string; role: string }[];
|
|
19
|
-
runs: TeamRun[]; messages: TeamMessage[];
|
|
20
|
-
}
|
|
21
|
-
export interface TeamReady { generation: string; ready: boolean }
|
|
22
|
-
export type TeamAgent = AgentView & { alive: boolean };
|
|
23
|
-
export type TeamActor = { kind: "user" | "coordinator"; owner: AgentOwner }
|
|
24
|
-
| { kind: "member"; agentId: string; generation: string; requestId?: string };
|
|
25
|
-
export interface TeamInput extends TeamRequest { teamId: string; runId: string; message: string }
|
|
26
|
-
export interface TeamLink { team: TeamRecord; run: TeamRun; request: TeamRequest }
|
|
27
|
-
export interface TeamRunOptions {
|
|
28
|
-
instruction: string; mode: TeamRun["mode"]; rounds?: number; maxConcurrent?: number; timeoutSeconds?: number;
|
|
29
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { AgentCompletion } from "../../model/agent.js";
|
|
2
|
-
import type { TeamLink } from "../../model/team.js";
|
|
3
|
-
import type { TeamService } from "./team-service.js";
|
|
4
|
-
import { json } from "../../common/json.js";
|
|
5
|
-
import { markdownFields } from "../../common/markdown.js";
|
|
6
|
-
|
|
7
|
-
export function teamNotice(values: ReturnType<TeamService["notifications"]>, known: ReadonlySet<string>) {
|
|
8
|
-
const notes = values.filter(value => !known.has(value.key)).slice(0, 8);
|
|
9
|
-
if (!notes.length) return;
|
|
10
|
-
return { kind: "teams" as const, keys: notes.map(value => value.key), content: ["# 团队批次状态", "",
|
|
11
|
-
"团队批次状态已变化,用 pbot_team status 与 pbot_team_message read 查看状态和结果。暂停时可能仍有在途工作,重跑前先查看原请求结果。", "",
|
|
12
|
-
notes.map(value => ["## " + value.name, "", "- `teamId`: `" + value.teamId + "`", "- `runId`: `" + value.runId + "`",
|
|
13
|
-
"- 状态:" + value.state, "- 在途工作:" + value.activeCount, ...(value.reason ? ["", "### 原因", "", value.reason] : [])].join("\n")).join("\n\n")].join("\n") };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function agentNotice(values: AgentCompletion[], links: ReadonlyMap<string, TeamLink>, known: ReadonlySet<string>) {
|
|
17
|
-
const batch = values.filter(value => !value.cycle.requestIds.some(id => links.has(id)) &&
|
|
18
|
-
!(value.cycle.cycleId === value.cycle.generation && [...links.values()].some(link => link.request.agentId === value.agentId && link.request.generation === value.cycle.generation)) &&
|
|
19
|
-
!known.has(value.agentId + ":" + value.cycle.cycleId)).slice(0, 8);
|
|
20
|
-
if (!batch.length) return;
|
|
21
|
-
return { kind: "agents" as const, keys: batch.map(value => value.agentId + ":" + value.cycle.cycleId),
|
|
22
|
-
content: "# 子会话工作结果\n\n子会话工作已结束。完整报告用 pbot_agent read 读取,指定对应 agentId 和 cycle.cycleId;nextOffset 非空时沿用该 cycleId 续读。\n\n" +
|
|
23
|
-
(batch.some(value => value.cycle.source !== "parent") ? "部分结果包含人工子会话输入,来源见 cycle.source。\n\n" : "") +
|
|
24
|
-
batch.map(value => "## " + value.name + "\n\n" + markdownFields(json({ agentId: value.agentId,
|
|
25
|
-
cycle: { cycleId: value.cycle.cycleId, state: value.cycle.state, source: value.cycle.source, requestIds: value.cycle.requestIds,
|
|
26
|
-
...(value.cycle.error === undefined ? {} : { error: value.cycle.error }) },
|
|
27
|
-
report: value.report.slice(0, 2000), truncated: value.report.length > 2000 }), 3)).join("\n\n") };
|
|
28
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { AcpBackend } from "../../model/acp.js";
|
|
2
|
-
import type { AgentOptions, NativeAgentHost } from "../../model/agent.js";
|
|
3
|
-
|
|
4
|
-
const piOnly = ["noExtensions", "noPromptTemplates", "noContextFiles"] as const;
|
|
5
|
-
|
|
6
|
-
/** Keep only options the effective target can use; declared-but-inapplicable fields are inert. */
|
|
7
|
-
export function applicableStartOptions(kind: NativeAgentHost, backend: AcpBackend | undefined, options: AgentOptions): AgentOptions {
|
|
8
|
-
const result = { ...options };
|
|
9
|
-
if (backend) {
|
|
10
|
-
if (backend !== "pi") {
|
|
11
|
-
for (const key of piOnly) delete result[key];
|
|
12
|
-
delete result.noSkills; delete result.tools;
|
|
13
|
-
}
|
|
14
|
-
if (backend === "claude") delete result.provider;
|
|
15
|
-
} else if (kind !== "pi") {
|
|
16
|
-
for (const key of piOnly) delete result[key];
|
|
17
|
-
}
|
|
18
|
-
return result;
|
|
19
|
-
}
|
|
@@ -1,275 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { requireThat } from "../../common/validation.js";
|
|
3
|
-
import { packageVersion, releaseLine } from "../../common/version.js";
|
|
4
|
-
import { AgentFiles, descriptor, launch, status, message } from "../../storage/agent-files.js";
|
|
5
|
-
import { TeamFiles } from "../../storage/team-files.js";
|
|
6
|
-
import type { AgentCompletion, AgentOwner } from "../../model/agent.js";
|
|
7
|
-
import type { TeamActor, TeamAgent, TeamInput, TeamLink, TeamMessage, TeamRecord, TeamRunOptions } from "../../model/team.js";
|
|
8
|
-
|
|
9
|
-
/** Team decisions only. The parent dispatches returned inputs through AgentService. */
|
|
10
|
-
export class TeamService {
|
|
11
|
-
#failure: unknown;
|
|
12
|
-
#disposed = false;
|
|
13
|
-
readonly #recovered = new Set<string>();
|
|
14
|
-
constructor(readonly root: string, readonly agents: AgentFiles, readonly files: TeamFiles) {}
|
|
15
|
-
check() { requireThat(!this.#disposed, "团队服务已关闭。"); this.files.check(); if (this.#failure) throw this.#failure; }
|
|
16
|
-
fail(error: unknown) { this.#failure = error; }
|
|
17
|
-
dispose() { this.#disposed = true; }
|
|
18
|
-
private owned(team: TeamRecord, owner: AgentOwner) {
|
|
19
|
-
return team.workspace === this.root && team.parentSessionId === owner.sessionId && (team.parentLeaf === null || owner.branch.includes(team.parentLeaf));
|
|
20
|
-
}
|
|
21
|
-
private records(sessionId?: string) {
|
|
22
|
-
this.check();
|
|
23
|
-
return this.files.list().filter(t => t.workspace === this.root && releaseLine(t.release) === releaseLine(packageVersion) &&
|
|
24
|
-
(!sessionId || t.parentSessionId === sessionId));
|
|
25
|
-
}
|
|
26
|
-
private get(id: string, actor: TeamActor) {
|
|
27
|
-
this.check(); const team = this.files.read(id);
|
|
28
|
-
requireThat(team.workspace === this.root && releaseLine(team.release) === releaseLine(packageVersion), "团队工作区或版本不匹配。");
|
|
29
|
-
if (actor.kind === "member") {
|
|
30
|
-
const d = this.agents.read(actor.agentId, "descriptor.json", descriptor), run = this.agents.read(actor.agentId, "launch.json", launch);
|
|
31
|
-
requireThat(d.workspace === this.root && d.parentSessionId === team.parentSessionId && team.members.some(m => m.agentId === actor.agentId) &&
|
|
32
|
-
run.generation === actor.generation && !run.stop, "团队成员身份已失效。");
|
|
33
|
-
} else requireThat(this.owned(team, actor.owner), "团队不属于当前会话分支。");
|
|
34
|
-
return team;
|
|
35
|
-
}
|
|
36
|
-
private member(id: string, owner: AgentOwner, views: TeamAgent[]) {
|
|
37
|
-
const v = views.find(v => v.agentId === id), d = this.agents.read(id, "descriptor.json", descriptor);
|
|
38
|
-
requireThat(v && v.owned && d.workspace === this.root && d.parentSessionId === owner.sessionId &&
|
|
39
|
-
(d.parentLeaf === null || owner.branch.includes(d.parentLeaf)), "成员不属于当前分支:" + id);
|
|
40
|
-
requireThat(releaseLine(d.release) === releaseLine(packageVersion), "成员版本不兼容:" + id);
|
|
41
|
-
return v;
|
|
42
|
-
}
|
|
43
|
-
private requests(team: TeamRecord, views: TeamAgent[]) {
|
|
44
|
-
const states = new Map(team.members.map(m => [m.agentId, { view: views.find(v => v.agentId === m.agentId),
|
|
45
|
-
saved: this.agents.optional(m.agentId, "status.json", status),
|
|
46
|
-
cycles: new Map(this.agents.completions(m.agentId).flatMap(c => c.requestIds.map(id => [c.generation + ":" + id, c] as const))) }]));
|
|
47
|
-
return team.runs.flatMap(run => run.issued.map(q => {
|
|
48
|
-
const s = states.get(q.agentId)!, ended = s.cycles.get(q.generation + ":" + q.requestId);
|
|
49
|
-
const accepted = s.saved?.generation === q.generation && !!s.saved.accepted?.includes(q.requestId);
|
|
50
|
-
const active = !ended && accepted && !!s.view?.alive && s.view.generation === q.generation;
|
|
51
|
-
let outcome: string | undefined = ended?.state;
|
|
52
|
-
if (ended && ended.source !== "parent") outcome = ended.source;
|
|
53
|
-
else if (outcome === "completed" && !team.messages.some(m => m.source?.requestId === q.requestId)) outcome = "unpublished";
|
|
54
|
-
return { requestId: q.requestId, agentId: q.agentId, runId: run.runId, round: q.round, active,
|
|
55
|
-
state: outcome ?? (active ? "accepted" : accepted ? "uncertain" : run.state === "running" ? "queued" : "cancelled"),
|
|
56
|
-
...(ended ? { cycleId: ended.cycleId } : {}) };
|
|
57
|
-
}));
|
|
58
|
-
}
|
|
59
|
-
private active(team: TeamRecord, views: TeamAgent[]) { return this.requests(team, views).filter(q => q.active); }
|
|
60
|
-
private available(id: string, owner: AgentOwner, views: TeamAgent[], links: Map<string, TeamLink>) {
|
|
61
|
-
const v = this.member(id, owner, views), run = this.agents.read(id, "launch.json", launch);
|
|
62
|
-
const saved = this.agents.optional(id, "status.json", status), ready = this.files.ready(id);
|
|
63
|
-
const available = (condition: unknown, message: string): void => {
|
|
64
|
-
if (!condition) throw Object.assign(new Error(message), { code: "TEAM_NOT_READY" });
|
|
65
|
-
};
|
|
66
|
-
available(v.alive && v.state === "idle" && !run.stop && v.generation === run.generation && saved?.generation === run.generation && saved.state === "idle", "成员尚未空闲:" + id);
|
|
67
|
-
available(ready?.ready && ready.generation === run.generation, "成员需加载新版子入口:" + id);
|
|
68
|
-
available(!this.agents.messages(id).some(q => q.generation === run.generation && !saved?.accepted?.includes(q.requestId) && !links.has(q.requestId)), "成员有待处理私人输入:" + id);
|
|
69
|
-
return v;
|
|
70
|
-
}
|
|
71
|
-
create(owner: AgentOwner, input: Pick<TeamRecord, "name" | "brief" | "members">, views: TeamAgent[]) {
|
|
72
|
-
this.check();
|
|
73
|
-
return this.agents.control(() => {
|
|
74
|
-
const records = this.records();
|
|
75
|
-
for (const m of input.members) {
|
|
76
|
-
this.member(m.agentId, owner, views);
|
|
77
|
-
requireThat(!records.some(t => !t.closed && t.members.some(v => v.agentId === m.agentId)), "成员已属于未关闭团队:" + m.agentId);
|
|
78
|
-
}
|
|
79
|
-
const team: TeamRecord = { ...input, members: structuredClone(input.members), teamId: randomUUID(), release: packageVersion,
|
|
80
|
-
workspace: this.root, parentSessionId: owner.sessionId, parentLeaf: owner.leaf, closed: false, runs: [], messages: [] };
|
|
81
|
-
this.files.save(team, true); return team;
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
run(id: string, owner: AgentOwner, options: TeamRunOptions, views: TeamAgent[]) {
|
|
85
|
-
this.check();
|
|
86
|
-
return this.agents.control(() => {
|
|
87
|
-
const team = this.get(id, { kind: "coordinator", owner }), links = this.files.links(this.records());
|
|
88
|
-
requireThat(!team.closed && !team.runs.some(r => r.state === "running") && !this.active(team, views).length, "团队仍在工作或已关闭。");
|
|
89
|
-
const members = team.members.map(m => this.available(m.agentId, owner, views, links));
|
|
90
|
-
requireThat(options.mode !== "roundRobin" || options.maxConcurrent === undefined || options.maxConcurrent === 1, "轮流讨论并发须为 1。");
|
|
91
|
-
const rounds = options.rounds ?? 2, maxConcurrent = options.mode === "roundRobin" ? 1 : options.maxConcurrent ?? Math.min(2, members.length);
|
|
92
|
-
requireThat(Number.isInteger(rounds) && rounds >= 1 && rounds <= 10 && Number.isInteger(maxConcurrent) && maxConcurrent >= 1 && maxConcurrent <= members.length, "轮次或并发无效。");
|
|
93
|
-
const seconds = options.timeoutSeconds ?? 900;
|
|
94
|
-
requireThat(Number.isInteger(seconds) && seconds >= 1 && seconds <= 86400, "期限须为 1–86400 秒。");
|
|
95
|
-
requireThat(options.instruction.trim().length > 0, "新批次需要新的明确要求。");
|
|
96
|
-
const run = { runId: randomUUID(), instruction: options.instruction, mode: options.mode, rounds, maxConcurrent,
|
|
97
|
-
deadlineAt: Date.now() + seconds * 1000, generations: Object.fromEntries(members.map(v => [v.agentId, v.generation])),
|
|
98
|
-
state: "running" as const, issued: [] };
|
|
99
|
-
team.runs.push(run); this.files.save(team); return run;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
pause(id: string, owner: AgentOwner, reason = "主会话暂停") {
|
|
103
|
-
this.check(); this.agents.control(() => { const team = this.get(id, { kind: "coordinator", owner }); this.pauseRecord(team, reason); });
|
|
104
|
-
}
|
|
105
|
-
private pauseRecord(team: TeamRecord, reason: string) {
|
|
106
|
-
let changed = false;
|
|
107
|
-
for (const run of team.runs) if (run.state === "running") { run.state = "paused"; run.reason = reason; changed = true; }
|
|
108
|
-
if (changed) this.files.save(team);
|
|
109
|
-
}
|
|
110
|
-
pauseAll(owner: AgentOwner, reason: string) {
|
|
111
|
-
if (!this.records(owner.sessionId).length) return;
|
|
112
|
-
this.agents.control(() => { for (const team of this.records(owner.sessionId)) this.pauseRecord(team, reason); });
|
|
113
|
-
}
|
|
114
|
-
recover(owner: AgentOwner) {
|
|
115
|
-
if (this.#recovered.has(owner.sessionId)) return;
|
|
116
|
-
this.pauseAll(owner, "主会话恢复:未自动续排旧批次"); this.#recovered.add(owner.sessionId);
|
|
117
|
-
}
|
|
118
|
-
pauseMember(agentId: string, reason: string) {
|
|
119
|
-
this.check(); this.agents.control(() => { for (const team of this.records()) if (team.members.some(m => m.agentId === agentId)) this.pauseRecord(team, reason); });
|
|
120
|
-
}
|
|
121
|
-
close(id: string, owner: AgentOwner, views: TeamAgent[]) {
|
|
122
|
-
this.check(); this.agents.control(() => {
|
|
123
|
-
const team = this.get(id, { kind: "coordinator", owner });
|
|
124
|
-
requireThat(!team.runs.some(r => r.state === "running") && !this.active(team, views).length, "先暂停团队并等待在途工作结束。");
|
|
125
|
-
if (!team.closed) { team.closed = true; this.files.save(team); }
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
post(id: string, actor: TeamActor, operationId: string, text: string, replyTo?: number) {
|
|
129
|
-
this.check(); requireThat(text.trim().length > 0 && text.length <= 24000 && operationId.length > 0, "消息须为 1–24000 字符。");
|
|
130
|
-
operationId = actor.kind === "member" ? "post:" + actor.agentId + ":" + actor.generation + ":" + operationId : "post:" + actor.kind + ":" + actor.owner.sessionId + ":" + operationId;
|
|
131
|
-
return this.agents.control(() => {
|
|
132
|
-
const team = this.get(id, actor);
|
|
133
|
-
const prior = team.messages.find(m => m.operationId === operationId);
|
|
134
|
-
if (prior) {
|
|
135
|
-
requireThat(prior.text === text && prior.replyTo === replyTo && prior.author.kind === actor.kind &&
|
|
136
|
-
(actor.kind !== "member" || prior.author.agentId === actor.agentId), "发布操作 ID 冲突。"); return prior;
|
|
137
|
-
}
|
|
138
|
-
requireThat(!team.closed, "团队已关闭。");
|
|
139
|
-
requireThat(replyTo === undefined || team.messages.some(m => m.seq === replyTo), "回复目标不存在。");
|
|
140
|
-
let runId: string | undefined;
|
|
141
|
-
if (actor.kind === "member") {
|
|
142
|
-
const run = team.runs.find(r => r.issued.some(q => q.requestId === actor.requestId && q.agentId === actor.agentId && q.generation === actor.generation));
|
|
143
|
-
const saved = this.agents.optional(actor.agentId, "status.json", status);
|
|
144
|
-
requireThat(run && saved?.generation === actor.generation && saved.cycle?.state === "running" && saved.cycle.source === "parent" &&
|
|
145
|
-
!!actor.requestId && saved.cycle.requestIds.includes(actor.requestId), "只能在自己的团队工作中发言。"); runId = run.runId;
|
|
146
|
-
}
|
|
147
|
-
const value: TeamMessage = { seq: team.messages.length + 1, operationId, text,
|
|
148
|
-
author: actor.kind === "member" ? { kind: "member", agentId: actor.agentId, name: this.agents.read(actor.agentId, "launch.json", launch).name }
|
|
149
|
-
: { kind: actor.kind, name: actor.kind === "user" ? "用户" : "主会话" },
|
|
150
|
-
...(replyTo === undefined ? {} : { replyTo }), ...(runId ? { runId } : {}) };
|
|
151
|
-
team.messages.push(value); this.files.save(team); return value;
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
read(id: string, actor: TeamActor, page: { afterSeq?: number; seq?: number; offset?: number; limit?: number } = {}) {
|
|
155
|
-
const team = this.get(id, actor), limit = page.limit ?? 16000, after = page.afterSeq ?? 0, offset = page.offset ?? 0;
|
|
156
|
-
requireThat(Number.isInteger(limit) && limit >= 1 && limit <= 16000 && Number.isInteger(after) && after >= 0 && after <= team.messages.length &&
|
|
157
|
-
Number.isInteger(offset) && offset >= 0 && (page.seq !== undefined || offset === 0), "分页参数无效。");
|
|
158
|
-
const values = page.seq === undefined ? team.messages.filter(m => m.seq > after) : team.messages.filter(m => m.seq === page.seq);
|
|
159
|
-
requireThat(page.seq === undefined || values.length === 1, "消息不存在。");
|
|
160
|
-
let left = limit;
|
|
161
|
-
const messages = [];
|
|
162
|
-
for (const m of values) {
|
|
163
|
-
const at = page.seq === undefined ? 0 : offset;
|
|
164
|
-
requireThat(at <= m.text.length, "消息分页位置超出范围。");
|
|
165
|
-
const text = m.text.slice(at, at + left); left -= Math.max(1, text.length);
|
|
166
|
-
messages.push({ ...m, text, offset: at, totalLength: m.text.length, nextOffset: at + text.length < m.text.length ? at + text.length : null });
|
|
167
|
-
if (left <= 0) break;
|
|
168
|
-
}
|
|
169
|
-
const last = messages.at(-1)?.seq ?? after;
|
|
170
|
-
return { teamId: id, messages, lastSeq: team.messages.length, nextAfterSeq: page.seq === undefined && last < team.messages.length ? last : null };
|
|
171
|
-
}
|
|
172
|
-
status(id: string, owner: AgentOwner, views: TeamAgent[]) {
|
|
173
|
-
return this.summary(this.get(id, { kind: "coordinator", owner }), owner, views);
|
|
174
|
-
}
|
|
175
|
-
list(owner: AgentOwner, views: TeamAgent[]) { return this.records(owner.sessionId).map(t => this.summary(t, owner, views)); }
|
|
176
|
-
private summary(team: TeamRecord, owner: AgentOwner, views: TeamAgent[]) {
|
|
177
|
-
const identity = { teamId: team.teamId, name: team.name, closed: team.closed };
|
|
178
|
-
if (!this.owned(team, owner)) return { ...identity, owned: false as const };
|
|
179
|
-
const run = team.runs.at(-1), requests = this.requests(team, views), activeCount = requests.filter(q => q.active).length;
|
|
180
|
-
return { ...identity, brief: team.brief, owned: true as const,
|
|
181
|
-
members: team.members.map(m => { const v = views.find(v => v.agentId === m.agentId); return { ...m, name: v?.name ?? m.agentId, state: v?.state ?? "stopped" }; }),
|
|
182
|
-
lastSeq: team.messages.length, activeCount, unresolved: requests.filter(q => q.state !== "completed"),
|
|
183
|
-
...(run ? { run: { runId: run.runId, instruction: run.instruction, mode: run.mode, state: run.state, rounds: run.rounds,
|
|
184
|
-
maxConcurrent: run.maxConcurrent, deadlineAt: run.deadlineAt, scheduled: run.issued.length,
|
|
185
|
-
published: team.messages.filter(m => m.runId === run.runId && m.source).length, ...(run.reason ? { reason: run.reason } : {}) } } : {}) };
|
|
186
|
-
}
|
|
187
|
-
links() { return this.files.links(this.records()); }
|
|
188
|
-
/** The callback persists the existing child receipt while the shared control lock is held. */
|
|
189
|
-
claim(agentId: string, generation: string, requestId: string, commit: () => void): TeamLink | undefined {
|
|
190
|
-
this.check();
|
|
191
|
-
return this.agents.control(() => {
|
|
192
|
-
const link = this.links().get(requestId), run = this.agents.read(agentId, "launch.json", launch), ready = this.files.ready(agentId);
|
|
193
|
-
const saved = this.agents.optional(agentId, "status.json", status), input = this.agents.optional(agentId, "inbox/" + requestId + ".json", message);
|
|
194
|
-
if (!link || link.request.agentId !== agentId || link.request.generation !== generation || run.generation !== generation || run.stop ||
|
|
195
|
-
!ready?.ready || ready.generation !== generation || saved?.generation !== generation || saved.state !== "idle" ||
|
|
196
|
-
saved.accepted?.includes(requestId) || input?.generation !== generation || link.team.closed || link.run.state !== "running" || link.run.deadlineAt <= Date.now()) return;
|
|
197
|
-
for (const member of link.team.members) {
|
|
198
|
-
const memberRun = this.agents.read(member.agentId, "launch.json", launch), memberReady = this.files.ready(member.agentId);
|
|
199
|
-
if (memberRun.stop || memberRun.generation !== link.run.generations[member.agentId] || !memberReady?.ready || memberReady.generation !== memberRun.generation) return;
|
|
200
|
-
}
|
|
201
|
-
commit(); return link;
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
advance(owner: AgentOwner, views: TeamAgent[], completions: AgentCompletion[]): TeamInput[] {
|
|
205
|
-
if (!this.records(owner.sessionId).length) return [];
|
|
206
|
-
return this.agents.control(() => {
|
|
207
|
-
const inputs: TeamInput[] = [], records = this.records(owner.sessionId), links = this.files.links(records);
|
|
208
|
-
for (const team of records) {
|
|
209
|
-
let changed = false;
|
|
210
|
-
for (const run of team.runs) {
|
|
211
|
-
for (const q of run.issued) {
|
|
212
|
-
const done = completions.find(v => v.agentId === q.agentId && v.cycle.generation === q.generation && v.cycle.requestIds.includes(q.requestId));
|
|
213
|
-
if (!done) continue;
|
|
214
|
-
if (done.cycle.state !== "completed" || done.cycle.source !== "parent" || done.cycle.requestIds.length !== 1 || !done.report.trim()) {
|
|
215
|
-
if (run.state === "running") { run.state = "paused"; run.reason = "成员失败或人工接管:" + q.agentId; changed = true; }
|
|
216
|
-
} else if (!team.messages.some(m => m.source?.requestId === q.requestId)) {
|
|
217
|
-
team.messages.push({ seq: team.messages.length + 1, operationId: "result:" + q.requestId,
|
|
218
|
-
author: { kind: "member", agentId: q.agentId, name: done.name }, text: done.report, runId: run.runId,
|
|
219
|
-
source: { requestId: q.requestId, cycleId: done.cycle.cycleId } }); changed = true;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (run.state !== "running") continue;
|
|
223
|
-
const unavailable = team.members.find(m => {
|
|
224
|
-
const v = views.find(v => v.agentId === m.agentId), ready = this.files.ready(m.agentId);
|
|
225
|
-
return !v?.alive || !v.owned || v.generation !== run.generations[m.agentId] || !ready?.ready || ready.generation !== v.generation ||
|
|
226
|
-
["stopping", "stopped", "error"].includes(v.state) || (v.cycle?.state === "running" && v.cycle.source !== "parent");
|
|
227
|
-
});
|
|
228
|
-
if (!this.owned(team, owner) || unavailable || Date.now() >= run.deadlineAt) {
|
|
229
|
-
run.state = "paused"; run.reason = !this.owned(team, owner) ? "当前分支无团队归属" : unavailable ? "成员不可用:" + unavailable.agentId : "已到期限";
|
|
230
|
-
changed = true; continue;
|
|
231
|
-
}
|
|
232
|
-
const finished = new Set(team.messages.filter(m => m.runId === run.runId && m.source).map(m => m.source!.requestId));
|
|
233
|
-
const outstanding = run.issued.filter(q => !finished.has(q.requestId));
|
|
234
|
-
const round = Math.max(1, ...run.issued.map(q => q.round));
|
|
235
|
-
const thisRound = run.issued.filter(q => q.round === round);
|
|
236
|
-
const nextRound = thisRound.length === team.members.length && thisRound.every(q => finished.has(q.requestId)) ? round + 1 : round;
|
|
237
|
-
if (nextRound > run.rounds) { run.state = "finished"; changed = true; continue; }
|
|
238
|
-
let slots = run.maxConcurrent - outstanding.length;
|
|
239
|
-
for (const m of team.members) {
|
|
240
|
-
if (slots <= 0) break;
|
|
241
|
-
if (run.issued.some(q => q.round === nextRound && q.agentId === m.agentId)) continue;
|
|
242
|
-
// Waiting here does not create a second work queue. Unexpected private work pauses the batch.
|
|
243
|
-
const view = views.find(v => v.agentId === m.agentId);
|
|
244
|
-
if (view?.state === "busy" && view.cycle?.requestIds.some(id => run.issued.some(q => q.requestId === id))) break;
|
|
245
|
-
try { this.available(m.agentId, owner, views, links); }
|
|
246
|
-
catch (error) {
|
|
247
|
-
if ((error as NodeJS.ErrnoException).code !== "TEAM_NOT_READY") throw error;
|
|
248
|
-
run.state = "paused"; run.reason = String(error); changed = true; break;
|
|
249
|
-
}
|
|
250
|
-
const q = { requestId: randomUUID(), agentId: m.agentId, generation: run.generations[m.agentId]!, round: nextRound, throughSeq: team.messages.length };
|
|
251
|
-
run.issued.push(q); slots--; changed = true;
|
|
252
|
-
inputs.push({ ...q, teamId: team.teamId, runId: run.runId,
|
|
253
|
-
message: ["# 团队发言任务", "", "执行本次团队发言安排。最终答复将公开给全体成员。", "",
|
|
254
|
-
"- `teamId`: `" + team.teamId + "`", "- `runId`: `" + run.runId + "`", "- 轮次:" + q.round,
|
|
255
|
-
"", "## 本批次要求", "", run.instruction].join("\n") });
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
if (changed) this.files.save(team);
|
|
259
|
-
}
|
|
260
|
-
return inputs.filter(input => records.find(t => t.teamId === input.teamId)?.runs.find(r => r.runId === input.runId)?.state === "running");
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
notifications(owner: AgentOwner, views: TeamAgent[]) {
|
|
264
|
-
return this.records(owner.sessionId).filter(t => this.owned(t, owner) && t.members.every(m => views.some(v => v.agentId === m.agentId && v.owned)))
|
|
265
|
-
.flatMap(team => {
|
|
266
|
-
const requests = this.requests(team, views);
|
|
267
|
-
return team.runs.filter(r => r.state !== "running").map(run => {
|
|
268
|
-
const activeCount = requests.filter(q => q.runId === run.runId && q.active).length;
|
|
269
|
-
return { key: team.teamId + ":" + run.runId + ":" + run.state + (activeCount ? ":draining" : ":settled"),
|
|
270
|
-
teamId: team.teamId, name: team.name, runId: run.runId, state: run.state, activeCount,
|
|
271
|
-
...(run.reason ? { reason: run.reason } : {}) };
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { existsSync, lstatSync, mkdirSync, readdirSync } from "node:fs";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { object } from "../common/json.js";
|
|
4
|
-
import { requireThat } from "../common/validation.js";
|
|
5
|
-
import type { TeamRecord, TeamReady, TeamLink } from "../model/team.js";
|
|
6
|
-
import { AgentFiles } from "./agent-files.js";
|
|
7
|
-
import { readJsonFile, writeJsonFile } from "./atomic-json.js";
|
|
8
|
-
import { fileId, privateDirectory } from "./file-access.js";
|
|
9
|
-
|
|
10
|
-
const text = (v: unknown): v is string => typeof v === "string" && v.length > 0;
|
|
11
|
-
const integer = (v: unknown, min = 0): v is number => Number.isSafeInteger(v) && (v as number) >= min;
|
|
12
|
-
export function teamReady(v: unknown): TeamReady {
|
|
13
|
-
requireThat(object(v) && text(v.generation) && typeof v.ready === "boolean", "团队就绪标记无效。");
|
|
14
|
-
return v as unknown as TeamReady;
|
|
15
|
-
}
|
|
16
|
-
export function teamRecord(v: unknown): TeamRecord {
|
|
17
|
-
requireThat(object(v) && text(v.teamId) && text(v.release) && text(v.workspace) && text(v.parentSessionId) &&
|
|
18
|
-
(v.parentLeaf === null || text(v.parentLeaf)) && text(v.name) && text(v.brief) && typeof v.closed === "boolean" &&
|
|
19
|
-
Array.isArray(v.members) && v.members.length > 0 && v.members.length <= 50 && Array.isArray(v.runs) && Array.isArray(v.messages), "团队记录无效。");
|
|
20
|
-
fileId(v.teamId);
|
|
21
|
-
const members = new Set<string>(), runs = new Set<string>(), requests = new Map<string, { agentId: string; runId: string }>(), operations = new Set<string>();
|
|
22
|
-
for (const m of v.members) {
|
|
23
|
-
requireThat(object(m) && text(m.agentId) && text(m.role) && !members.has(m.agentId), "团队成员无效或重复。");
|
|
24
|
-
fileId(m.agentId); members.add(m.agentId);
|
|
25
|
-
}
|
|
26
|
-
let running = 0;
|
|
27
|
-
for (const r of v.runs) {
|
|
28
|
-
requireThat(object(r) && text(r.runId) && !runs.has(r.runId) && text(r.instruction) &&
|
|
29
|
-
["roundRobin", "parallel"].includes(String(r.mode)) && integer(r.rounds, 1) && r.rounds <= 10 &&
|
|
30
|
-
integer(r.maxConcurrent, 1) && r.maxConcurrent <= members.size && (r.mode !== "roundRobin" || r.maxConcurrent === 1) &&
|
|
31
|
-
integer(r.deadlineAt, 1) && ["running", "paused", "finished"].includes(String(r.state)) &&
|
|
32
|
-
(r.reason === undefined || text(r.reason)) && object(r.generations) && Array.isArray(r.issued), "团队批次无效。");
|
|
33
|
-
fileId(r.runId); runs.add(r.runId);
|
|
34
|
-
requireThat(Object.keys(r.generations).length === members.size && [...members].every(id => text((r.generations as Record<string, unknown>)[id])), "团队代次无效。");
|
|
35
|
-
if (r.state === "running") running++;
|
|
36
|
-
const turns = new Set<string>();
|
|
37
|
-
for (const q of r.issued) {
|
|
38
|
-
requireThat(object(q) && text(q.requestId) && !requests.has(q.requestId) && text(q.agentId) && members.has(q.agentId) &&
|
|
39
|
-
q.generation === r.generations[q.agentId] && integer(q.round, 1) && q.round <= r.rounds &&
|
|
40
|
-
integer(q.throughSeq) && q.throughSeq <= v.messages.length && !turns.has(q.agentId + ":" + q.round), "团队请求关联无效。");
|
|
41
|
-
fileId(q.requestId); requests.set(q.requestId, { agentId: q.agentId, runId: r.runId }); turns.add(q.agentId + ":" + q.round);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
requireThat(running <= 1 && (!v.closed || running === 0), "团队运行状态冲突。");
|
|
45
|
-
for (const [i, m] of v.messages.entries()) {
|
|
46
|
-
requireThat(object(m) && m.seq === i + 1 && text(m.operationId) && !operations.has(m.operationId) && typeof m.text === "string" &&
|
|
47
|
-
object(m.author) && ["user", "coordinator", "member"].includes(String(m.author.kind)) && text(m.author.name) &&
|
|
48
|
-
(m.author.kind === "member" ? text(m.author.agentId) && members.has(m.author.agentId) : m.author.agentId === undefined) &&
|
|
49
|
-
(m.replyTo === undefined || (integer(m.replyTo, 1) && m.replyTo <= i)) &&
|
|
50
|
-
(m.runId === undefined || (text(m.runId) && runs.has(m.runId))), "团队消息无效。");
|
|
51
|
-
if (m.source !== undefined) requireThat(object(m.source) && text(m.source.requestId) && text(m.source.cycleId) &&
|
|
52
|
-
requests.get(m.source.requestId)?.agentId === m.author.agentId && requests.get(m.source.requestId)?.runId === m.runId, "团队报告关联无效。");
|
|
53
|
-
operations.add(m.operationId);
|
|
54
|
-
}
|
|
55
|
-
return v as unknown as TeamRecord;
|
|
56
|
-
}
|
|
57
|
-
export class TeamFiles {
|
|
58
|
-
readonly root: string;
|
|
59
|
-
constructor(readonly agents: AgentFiles) { this.root = join(dirname(agents.root), "teams"); }
|
|
60
|
-
check() { this.agents.check(); }
|
|
61
|
-
read(id: string) {
|
|
62
|
-
const value = readJsonFile(this.root, fileId(id) + "/team.json", teamRecord);
|
|
63
|
-
requireThat(value.teamId === id, "团队路径与身份不匹配。"); return value;
|
|
64
|
-
}
|
|
65
|
-
list() {
|
|
66
|
-
if (!existsSync(this.root)) return [];
|
|
67
|
-
return readdirSync(this.root, { withFileTypes: true }).filter(e => !e.name.startsWith(".")).map(e => this.read(e.name));
|
|
68
|
-
}
|
|
69
|
-
save(value: TeamRecord, create = false) {
|
|
70
|
-
this.check(); teamRecord(value);
|
|
71
|
-
let published: boolean;
|
|
72
|
-
try {
|
|
73
|
-
mkdirSync(this.root, { recursive: true, mode: 0o700 });
|
|
74
|
-
const directory = lstatSync(this.root);
|
|
75
|
-
requireThat(directory.isDirectory() && !directory.isSymbolicLink(), "团队状态必须使用普通目录。");
|
|
76
|
-
if (create) privateDirectory(this.root, fileId(value.teamId));
|
|
77
|
-
published = writeJsonFile(this.root, fileId(value.teamId) + "/team.json", value, teamRecord, create);
|
|
78
|
-
} catch (error) { this.agents.fail(error); throw error; }
|
|
79
|
-
requireThat(published, "团队已存在。");
|
|
80
|
-
}
|
|
81
|
-
ready(id: string) { return this.agents.optional(id, "team-ready.json", teamReady); }
|
|
82
|
-
setReady(id: string, generation: string, ready: boolean) {
|
|
83
|
-
this.agents.write(id, "team-ready.json", { generation, ready }, teamReady);
|
|
84
|
-
}
|
|
85
|
-
links(records = this.list()): Map<string, TeamLink> {
|
|
86
|
-
const result = new Map<string, TeamLink>();
|
|
87
|
-
for (const team of records) for (const run of team.runs) for (const request of run.issued) {
|
|
88
|
-
requireThat(!result.has(request.requestId), "团队请求 ID 重复。");
|
|
89
|
-
result.set(request.requestId, { team, run, request });
|
|
90
|
-
}
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
}
|