@teamclaws/teamclaw 2026.8.0 → 2026.11.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/cli.mjs +46 -15
- package/dist/api.js +1 -0
- package/dist/index.js +325 -0
- package/dist/src/config.js +412 -0
- package/dist/src/controller/clarification-routes.js +315 -0
- package/dist/src/controller/controller-capacity.js +14 -0
- package/dist/src/controller/controller-intake.js +895 -0
- package/dist/src/controller/controller-planning.js +813 -0
- package/dist/src/controller/controller-runs.js +175 -0
- package/dist/src/controller/controller-service.js +624 -0
- package/dist/src/controller/controller-tools.js +587 -0
- package/dist/src/controller/delivery-report.js +458 -0
- package/dist/src/controller/github-auth.js +564 -0
- package/dist/src/controller/http-server.js +284 -0
- package/dist/src/controller/kickoff-orchestrator.js +246 -0
- package/dist/src/controller/managed-gateway-process.js +286 -0
- package/dist/src/controller/message-delivery.js +28 -0
- package/dist/src/controller/message-router.js +38 -0
- package/dist/src/controller/message-routes.js +114 -0
- package/dist/src/controller/orchestration-manifest.js +151 -0
- package/dist/src/controller/preview-manager.js +586 -0
- package/dist/src/controller/preview-proxy.js +98 -0
- package/dist/src/controller/profile-switch.js +289 -0
- package/dist/src/controller/project-registry.js +294 -0
- package/dist/src/controller/prompt-injector.js +197 -0
- package/dist/src/controller/repo-routes.js +63 -0
- package/dist/src/controller/role-inference.js +246 -0
- package/dist/src/controller/route-table.js +1353 -0
- package/dist/src/controller/task-assignment.js +508 -0
- package/dist/src/controller/task-cleanup.js +258 -0
- package/dist/src/controller/task-execution.js +240 -0
- package/dist/src/controller/task-results.js +461 -0
- package/dist/src/controller/task-router.js +137 -0
- package/dist/src/controller/task-routes.js +703 -0
- package/dist/src/controller/teamauto-routes.js +124 -0
- package/dist/src/controller/tunnel-manager.js +548 -0
- package/dist/src/controller/ui-assets.js +68 -0
- package/dist/src/controller/websocket.js +42 -0
- package/dist/src/controller/worker-provisioning.js +1674 -0
- package/dist/src/controller/worker-routes.js +188 -0
- package/dist/src/discovery.js +86 -0
- package/dist/src/git-collaboration.js +694 -0
- package/dist/src/identity.js +132 -0
- package/dist/src/install-defaults.js +1 -0
- package/dist/src/interaction-contracts.js +531 -0
- package/dist/src/networking.js +91 -0
- package/dist/src/openclaw-workspace.js +925 -0
- package/dist/src/profile-loader.js +486 -0
- package/dist/src/profiles/banking.json +178 -0
- package/dist/src/profiles/crossborder-ecom.json +253 -0
- package/dist/src/profiles/insurance.json +157 -0
- package/dist/src/profiles/knowledge/hs-structure.md +97 -0
- package/dist/src/profiles/knowledge/incoterms-2020.md +188 -0
- package/dist/src/profiles/knowledge/vat-principles.md +85 -0
- package/dist/src/profiles/schema.json +201 -0
- package/dist/src/profiles/securities.json +137 -0
- package/dist/src/profiles/software-rd.json +264 -0
- package/dist/src/profiles/trading-co.json +200 -0
- package/dist/src/prompt-policy.js +386 -0
- package/dist/src/protocol.js +58 -0
- package/dist/src/role-ids.js +29 -0
- package/dist/src/roles.js +390 -0
- package/dist/src/session-roles.js +14 -0
- package/dist/src/state.js +361 -0
- package/dist/src/task-executor.js +1023 -0
- package/dist/src/teamauto/github-client.js +210 -0
- package/dist/src/teamauto/norms.js +85 -0
- package/dist/src/teamauto/repo-sync.js +423 -0
- package/dist/src/teamauto/scanner.js +207 -0
- package/dist/src/teamauto/schedule.js +123 -0
- package/dist/src/teamauto/scheduler.js +907 -0
- package/dist/src/teamauto/store.js +133 -0
- package/dist/src/types.js +290 -0
- package/dist/src/ui/app.js +4860 -0
- package/dist/src/ui/assets/teamclaw-app-icon.png +0 -0
- package/dist/src/ui/github-login.html +296 -0
- package/dist/src/ui/index.html +447 -0
- package/dist/src/ui/style.css +2805 -0
- package/dist/src/worker/http-handler.js +146 -0
- package/dist/src/worker/message-queue.js +24 -0
- package/dist/src/worker/prompt-injector.js +78 -0
- package/dist/src/worker/skill-installer.js +255 -0
- package/dist/src/worker/tools.js +541 -0
- package/dist/src/worker/worker-service.js +307 -0
- package/dist/src/workspace-browser.js +269 -0
- package/openclaw.plugin.json +28 -1
- package/package.json +3 -2
- package/src/config.ts +33 -2
- package/src/controller/clarification-routes.ts +404 -0
- package/src/controller/controller-intake.ts +1113 -0
- package/src/controller/controller-planning.ts +1029 -0
- package/src/controller/controller-runs.ts +239 -0
- package/src/controller/controller-service.ts +295 -41
- package/src/controller/controller-tools.ts +11 -7
- package/src/controller/http-server.ts +294 -6971
- package/src/controller/kickoff-orchestrator.ts +70 -7
- package/src/controller/message-delivery.ts +49 -0
- package/src/controller/message-routes.ts +137 -0
- package/src/controller/orchestration-manifest.ts +6 -29
- package/src/controller/preview-proxy.ts +127 -0
- package/src/controller/profile-switch.ts +366 -0
- package/src/controller/project-registry.ts +361 -0
- package/src/controller/prompt-injector.ts +23 -8
- package/src/controller/repo-routes.ts +91 -0
- package/src/controller/role-inference.ts +260 -22
- package/src/controller/route-table.ts +1616 -0
- package/src/controller/task-assignment.ts +618 -0
- package/src/controller/task-cleanup.ts +344 -0
- package/src/controller/task-execution.ts +356 -0
- package/src/controller/task-results.ts +544 -0
- package/src/controller/task-router.ts +98 -6
- package/src/controller/task-routes.ts +812 -0
- package/src/controller/teamauto-routes.ts +121 -0
- package/src/controller/ui-assets.ts +70 -0
- package/src/controller/websocket.ts +13 -1
- package/src/controller/worker-provisioning.ts +32 -4
- package/src/controller/worker-routes.ts +241 -0
- package/src/identity.ts +3 -2
- package/src/interaction-contracts.ts +7 -7
- package/src/openclaw-workspace.ts +44 -18
- package/src/profile-loader.ts +650 -0
- package/src/profiles/banking.json +178 -0
- package/src/profiles/crossborder-ecom.json +253 -0
- package/src/profiles/insurance.json +157 -0
- package/src/profiles/knowledge/hs-structure.md +97 -0
- package/src/profiles/knowledge/incoterms-2020.md +188 -0
- package/src/profiles/knowledge/vat-principles.md +85 -0
- package/src/profiles/schema.json +201 -0
- package/src/profiles/securities.json +137 -0
- package/src/profiles/software-rd.json +264 -0
- package/src/profiles/trading-co.json +200 -0
- package/src/prompt-policy.ts +27 -13
- package/src/role-ids.ts +31 -0
- package/src/roles.ts +60 -14
- package/src/session-roles.ts +14 -0
- package/src/state.ts +236 -9
- package/src/task-executor.ts +7 -18
- package/src/teamauto/github-client.ts +21 -0
- package/src/teamauto/repo-sync.ts +246 -6
- package/src/teamauto/scanner.ts +47 -13
- package/src/teamauto/scheduler.ts +259 -16
- package/src/teamauto/store.ts +12 -0
- package/src/types.ts +133 -16
- package/src/ui/app.js +592 -24
- package/src/ui/index.html +44 -11
- package/src/ui/style.css +194 -0
- package/src/worker/prompt-injector.ts +48 -8
- package/src/worker/skill-installer.ts +3 -1
- package/src/worker/worker-service.ts +83 -3
|
@@ -0,0 +1,925 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
const execFileAsync = promisify(execFile);
|
|
6
|
+
import fs from "node:fs/promises";
|
|
7
|
+
import os from "node:os";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import JSON5 from "json5";
|
|
10
|
+
// Dependency-free canonical role id list (Phase 1.2); see role-ids.ts for why
|
|
11
|
+
// this module cannot value-import roles.ts.
|
|
12
|
+
import { ROLE_IDS } from "./role-ids.js";
|
|
13
|
+
export const TEAMCLAW_AGENT_ID = "teamclaw";
|
|
14
|
+
export const TEAMCLAW_ISOLATION_MODE_INDEPENDENT = "independent";
|
|
15
|
+
export const TEAMCLAW_ISOLATION_MODE_MAIN = "main";
|
|
16
|
+
const TEAMCLAW_RECOMMENDED_EXEC_SECURITY = "full";
|
|
17
|
+
const TEAMCLAW_RECOMMENDED_EXEC_ASK = "off";
|
|
18
|
+
const DEFAULT_AGENTS_MD = `# AGENTS.md
|
|
19
|
+
|
|
20
|
+
This workspace is shared by TeamClaw controller and workers.
|
|
21
|
+
|
|
22
|
+
Rules:
|
|
23
|
+
- Treat task-provided file paths as hints; verify they exist before reading or editing.
|
|
24
|
+
- Use the shared \`memory/\` directory for lightweight notes when useful.
|
|
25
|
+
- Check \`memory/patterns.md\` for previously discovered codebase patterns before starting work.
|
|
26
|
+
- Check for \`.teamclaw-notes.md\` files in directories you work on for prior context.
|
|
27
|
+
- Report meaningful progress during longer tasks.
|
|
28
|
+
- If requirements or environment details are missing and work cannot continue safely, request clarification instead of guessing.
|
|
29
|
+
`;
|
|
30
|
+
const DEFAULT_BOOTSTRAP_MD = `# BOOTSTRAP.md
|
|
31
|
+
|
|
32
|
+
This is a TeamClaw workspace bootstrap file.
|
|
33
|
+
|
|
34
|
+
If the project files you expect are missing:
|
|
35
|
+
1. Search the workspace before assuming the path is correct.
|
|
36
|
+
2. Call out missing artifacts explicitly.
|
|
37
|
+
3. Ask for clarification when the missing artifact blocks the task.
|
|
38
|
+
`;
|
|
39
|
+
const DEFAULT_HEARTBEAT_MD = `# HEARTBEAT.md
|
|
40
|
+
|
|
41
|
+
# Keep this file empty (or with only comments) to skip heartbeat API calls.
|
|
42
|
+
`;
|
|
43
|
+
const DEFAULT_PATTERNS_MD = `# Codebase Patterns
|
|
44
|
+
|
|
45
|
+
Reusable patterns discovered by TeamClaw workers during task execution.
|
|
46
|
+
This file is automatically maintained — new patterns are appended as workers complete tasks.
|
|
47
|
+
Read this file before starting work to benefit from previously discovered knowledge.
|
|
48
|
+
`;
|
|
49
|
+
function isRecord(value) {
|
|
50
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
51
|
+
}
|
|
52
|
+
function normalizeAgentId(value) {
|
|
53
|
+
const trimmed = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
54
|
+
if (!trimmed) {
|
|
55
|
+
return "main";
|
|
56
|
+
}
|
|
57
|
+
return trimmed.replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "") || "main";
|
|
58
|
+
}
|
|
59
|
+
function expandUserPath(value, homedir = os.homedir) {
|
|
60
|
+
const trimmed = value.trim();
|
|
61
|
+
if (!trimmed) {
|
|
62
|
+
return "";
|
|
63
|
+
}
|
|
64
|
+
if (trimmed === "~") {
|
|
65
|
+
return homedir();
|
|
66
|
+
}
|
|
67
|
+
if (trimmed.startsWith("~/")) {
|
|
68
|
+
return path.join(homedir(), trimmed.slice(2));
|
|
69
|
+
}
|
|
70
|
+
return path.resolve(trimmed);
|
|
71
|
+
}
|
|
72
|
+
function resolveConfiguredOpenClawWorkspaceDir(env = process.env, homedir = os.homedir) {
|
|
73
|
+
const parsed = loadOpenClawConfigRecord(env, homedir);
|
|
74
|
+
if (!parsed) {
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
77
|
+
const agents = isRecord(parsed.agents) ? parsed.agents : null;
|
|
78
|
+
const defaults = agents && isRecord(agents.defaults) ? agents.defaults : null;
|
|
79
|
+
if (defaults && typeof defaults.workspace === "string" && defaults.workspace.trim()) {
|
|
80
|
+
return expandUserPath(defaults.workspace, homedir);
|
|
81
|
+
}
|
|
82
|
+
return "";
|
|
83
|
+
}
|
|
84
|
+
function resolveConfiguredTeamClawIsolationMode(env = process.env, homedir = os.homedir) {
|
|
85
|
+
const parsed = loadOpenClawConfigRecord(env, homedir);
|
|
86
|
+
if (!parsed) {
|
|
87
|
+
return TEAMCLAW_ISOLATION_MODE_INDEPENDENT;
|
|
88
|
+
}
|
|
89
|
+
const plugins = isRecord(parsed.plugins) ? parsed.plugins : null;
|
|
90
|
+
const entries = plugins && isRecord(plugins.entries) ? plugins.entries : null;
|
|
91
|
+
const teamclawEntry = entries && isRecord(entries[TEAMCLAW_AGENT_ID]) ? entries[TEAMCLAW_AGENT_ID] : null;
|
|
92
|
+
const teamclawConfig = teamclawEntry && isRecord(teamclawEntry.config) ? teamclawEntry.config : null;
|
|
93
|
+
return teamclawConfig?.agentIsolationMode === TEAMCLAW_ISOLATION_MODE_MAIN
|
|
94
|
+
? TEAMCLAW_ISOLATION_MODE_MAIN
|
|
95
|
+
: TEAMCLAW_ISOLATION_MODE_INDEPENDENT;
|
|
96
|
+
}
|
|
97
|
+
function resolveExplicitTeamClawWorkspaceDir(env = process.env, homedir = os.homedir) {
|
|
98
|
+
const override = env.TEAMCLAW_WORKSPACE_DIR?.trim();
|
|
99
|
+
if (!override) {
|
|
100
|
+
return "";
|
|
101
|
+
}
|
|
102
|
+
return expandUserPath(override, homedir);
|
|
103
|
+
}
|
|
104
|
+
function loadOpenClawConfigRecord(env = process.env, homedir = os.homedir) {
|
|
105
|
+
const configPath = resolveDefaultOpenClawConfigPath(env, homedir);
|
|
106
|
+
try {
|
|
107
|
+
const raw = readFileSync(configPath, "utf8");
|
|
108
|
+
const parsed = JSON5.parse(raw);
|
|
109
|
+
return isRecord(parsed) ? parsed : null;
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function resolveConfiguredAgentEntry(agentId, env = process.env, homedir = os.homedir) {
|
|
116
|
+
const parsed = loadOpenClawConfigRecord(env, homedir);
|
|
117
|
+
if (!parsed) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
const agents = isRecord(parsed.agents) ? parsed.agents : null;
|
|
121
|
+
const normalizedAgentId = normalizeAgentId(agentId);
|
|
122
|
+
// OpenClaw 2026.10+ keyed agents.entries take precedence; legacy hosts
|
|
123
|
+
// (<2026.10) only have the array-shaped agents.list.
|
|
124
|
+
const entries = agents && isRecord(agents.entries) ? agents.entries : null;
|
|
125
|
+
const keyedEntry = entries ? entries[normalizedAgentId] : null;
|
|
126
|
+
if (isRecord(keyedEntry)) {
|
|
127
|
+
return keyedEntry;
|
|
128
|
+
}
|
|
129
|
+
const list = agents && Array.isArray(agents.list) ? agents.list : [];
|
|
130
|
+
for (const entry of list) {
|
|
131
|
+
if (!isRecord(entry)) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (normalizeAgentId(entry.id) === normalizedAgentId) {
|
|
135
|
+
return entry;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
function resolveConfiguredDefaultModelValue(env = process.env, homedir = os.homedir) {
|
|
141
|
+
const parsed = loadOpenClawConfigRecord(env, homedir);
|
|
142
|
+
if (!parsed) {
|
|
143
|
+
return "";
|
|
144
|
+
}
|
|
145
|
+
const agents = isRecord(parsed.agents) ? parsed.agents : null;
|
|
146
|
+
const defaults = agents && isRecord(agents.defaults) ? agents.defaults : null;
|
|
147
|
+
const model = defaults?.model;
|
|
148
|
+
if (typeof model === "string") {
|
|
149
|
+
return model.trim();
|
|
150
|
+
}
|
|
151
|
+
if (isRecord(model) && typeof model.primary === "string") {
|
|
152
|
+
return model.primary.trim();
|
|
153
|
+
}
|
|
154
|
+
return "";
|
|
155
|
+
}
|
|
156
|
+
function resolveConfiguredTeamClawModelValue(env = process.env, homedir = os.homedir) {
|
|
157
|
+
const teamclawEntry = resolveConfiguredAgentEntry(TEAMCLAW_AGENT_ID, env, homedir);
|
|
158
|
+
const model = teamclawEntry?.model;
|
|
159
|
+
if (typeof model === "string") {
|
|
160
|
+
return model.trim();
|
|
161
|
+
}
|
|
162
|
+
if (isRecord(model) && typeof model.primary === "string") {
|
|
163
|
+
return model.primary.trim();
|
|
164
|
+
}
|
|
165
|
+
return resolveConfiguredDefaultModelValue(env, homedir);
|
|
166
|
+
}
|
|
167
|
+
function cloneJsonValue(value) {
|
|
168
|
+
return value == null ? value : JSON.parse(JSON.stringify(value));
|
|
169
|
+
}
|
|
170
|
+
function resolveConfiguredDefaultAgentId(env = process.env, homedir = os.homedir) {
|
|
171
|
+
const parsed = loadOpenClawConfigRecord(env, homedir);
|
|
172
|
+
if (!parsed) {
|
|
173
|
+
return "main";
|
|
174
|
+
}
|
|
175
|
+
const agents = isRecord(parsed.agents) ? parsed.agents : null;
|
|
176
|
+
const list = agents && Array.isArray(agents.list) ? agents.list.filter(isRecord) : [];
|
|
177
|
+
if (list.length === 0) {
|
|
178
|
+
return "main";
|
|
179
|
+
}
|
|
180
|
+
const defaultEntry = list.find((entry) => entry.default === true) ?? list[0];
|
|
181
|
+
return normalizeAgentId(defaultEntry.id);
|
|
182
|
+
}
|
|
183
|
+
export function resolveDefaultOpenClawHomeDir(env = process.env, homedir = os.homedir) {
|
|
184
|
+
const baseHome = env.OPENCLAW_HOME?.trim() || env.HOME?.trim() || homedir();
|
|
185
|
+
return path.resolve(baseHome);
|
|
186
|
+
}
|
|
187
|
+
export function resolveDefaultOpenClawStateDir(env = process.env, homedir = os.homedir) {
|
|
188
|
+
const stateDirOverride = env.OPENCLAW_STATE_DIR?.trim();
|
|
189
|
+
if (stateDirOverride) {
|
|
190
|
+
return path.resolve(stateDirOverride);
|
|
191
|
+
}
|
|
192
|
+
return path.join(resolveDefaultOpenClawHomeDir(env, homedir), ".openclaw");
|
|
193
|
+
}
|
|
194
|
+
export function resolveDefaultOpenClawConfigPath(env = process.env, homedir = os.homedir) {
|
|
195
|
+
const configPathOverride = env.OPENCLAW_CONFIG_PATH?.trim();
|
|
196
|
+
if (configPathOverride) {
|
|
197
|
+
return path.resolve(configPathOverride);
|
|
198
|
+
}
|
|
199
|
+
return path.join(resolveDefaultOpenClawStateDir(env, homedir), "openclaw.json");
|
|
200
|
+
}
|
|
201
|
+
export function resolveDefaultOpenClawWorkspaceDir(env = process.env, homedir = os.homedir) {
|
|
202
|
+
const configuredWorkspaceDir = resolveConfiguredOpenClawWorkspaceDir(env, homedir);
|
|
203
|
+
if (configuredWorkspaceDir) {
|
|
204
|
+
return configuredWorkspaceDir;
|
|
205
|
+
}
|
|
206
|
+
const stateDir = resolveDefaultOpenClawStateDir(env, homedir);
|
|
207
|
+
const profile = env.OPENCLAW_PROFILE?.trim();
|
|
208
|
+
if (profile && profile.toLowerCase() !== "default") {
|
|
209
|
+
return path.join(stateDir, `workspace-${profile}`);
|
|
210
|
+
}
|
|
211
|
+
return path.join(stateDir, "workspace");
|
|
212
|
+
}
|
|
213
|
+
export function resolveTeamClawAgentDir(env = process.env, homedir = os.homedir) {
|
|
214
|
+
if (resolveConfiguredTeamClawIsolationMode(env, homedir) === TEAMCLAW_ISOLATION_MODE_MAIN) {
|
|
215
|
+
return resolveDefaultAgentDir(env, homedir);
|
|
216
|
+
}
|
|
217
|
+
const configuredEntry = resolveConfiguredAgentEntry(TEAMCLAW_AGENT_ID, env, homedir);
|
|
218
|
+
if (configuredEntry && typeof configuredEntry.agentDir === "string" && configuredEntry.agentDir.trim()) {
|
|
219
|
+
return expandUserPath(configuredEntry.agentDir, homedir);
|
|
220
|
+
}
|
|
221
|
+
return path.join(resolveDefaultOpenClawStateDir(env, homedir), "agents", TEAMCLAW_AGENT_ID, "agent");
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* OpenClaw 2026.10+ moved model-provider auth out of the legacy per-agent
|
|
225
|
+
* `auth-profiles.json` file and into the host config. Two host shapes exist:
|
|
226
|
+
* 2026.10 wrote `models.providers[*]` with apiKey/auth (optionally backed by
|
|
227
|
+
* the secrets store); 2026.11 onboard writes `auth.profiles[*]` entries
|
|
228
|
+
* (`provider` + `mode`, the secret itself in the secrets store) and models
|
|
229
|
+
* reference profiles via the `<model>@<profile>` suffix. Treat either shape
|
|
230
|
+
* as ready so the readiness gate does not false-negative on current hosts.
|
|
231
|
+
*/
|
|
232
|
+
function hostConfiguredProviderAuthExists(env = process.env, homedir = os.homedir) {
|
|
233
|
+
try {
|
|
234
|
+
const configPath = resolveDefaultOpenClawConfigPath(env, homedir);
|
|
235
|
+
const parsed = JSON.parse(readFileSync(configPath, "utf8"));
|
|
236
|
+
const providers = parsed.models?.providers ?? parsed.providers ?? {};
|
|
237
|
+
const inlineProviderAuth = Object.values(providers).some((value) => Boolean(value) &&
|
|
238
|
+
typeof value === "object" &&
|
|
239
|
+
("apiKey" in value || "auth" in value));
|
|
240
|
+
if (inlineProviderAuth) {
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
const authProfiles = Object.values(parsed.auth?.profiles ?? {});
|
|
244
|
+
return authProfiles.some((value) => Boolean(value) && typeof value === "object");
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
export function getTeamClawModelReadiness(env = process.env, homedir = os.homedir) {
|
|
251
|
+
const configuredModel = resolveConfiguredTeamClawModelValue(env, homedir);
|
|
252
|
+
const authCandidates = [
|
|
253
|
+
path.join(resolveTeamClawAgentDir(env, homedir), "auth-profiles.json"),
|
|
254
|
+
path.join(resolveDefaultAgentDir(env, homedir), "auth-profiles.json"),
|
|
255
|
+
].filter((value, index, values) => values.indexOf(value) === index);
|
|
256
|
+
const authPath = authCandidates.find((candidatePath) => {
|
|
257
|
+
try {
|
|
258
|
+
return existsSync(candidatePath);
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
}) ?? authCandidates[0] ?? "";
|
|
264
|
+
const hasAuthProfiles = Boolean(authPath) && existsSync(authPath);
|
|
265
|
+
const hasHostProviderAuth = !hasAuthProfiles && hostConfiguredProviderAuthExists(env, homedir);
|
|
266
|
+
const authReady = hasAuthProfiles || hasHostProviderAuth;
|
|
267
|
+
const hasConfiguredModel = Boolean(configuredModel);
|
|
268
|
+
const status = hasConfiguredModel && authReady ? "ready" : "missing";
|
|
269
|
+
const missingParts = [];
|
|
270
|
+
if (!hasConfiguredModel) {
|
|
271
|
+
missingParts.push("no model is configured");
|
|
272
|
+
}
|
|
273
|
+
if (!authReady) {
|
|
274
|
+
missingParts.push("no auth-profiles.json was found");
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
status,
|
|
278
|
+
hasConfiguredModel,
|
|
279
|
+
hasAuthProfiles: authReady,
|
|
280
|
+
configuredModel,
|
|
281
|
+
authPath,
|
|
282
|
+
message: status === "ready"
|
|
283
|
+
? `TeamClaw is ready with model ${configuredModel}.`
|
|
284
|
+
: `TeamClaw cannot work yet because ${missingParts.join(" and ")}.`,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
export function resolveDefaultAgentDir(env = process.env, homedir = os.homedir) {
|
|
288
|
+
const defaultAgentId = resolveConfiguredDefaultAgentId(env, homedir);
|
|
289
|
+
const configuredEntry = resolveConfiguredAgentEntry(defaultAgentId, env, homedir);
|
|
290
|
+
if (configuredEntry && typeof configuredEntry.agentDir === "string" && configuredEntry.agentDir.trim()) {
|
|
291
|
+
return expandUserPath(configuredEntry.agentDir, homedir);
|
|
292
|
+
}
|
|
293
|
+
return path.join(resolveDefaultOpenClawStateDir(env, homedir), "agents", defaultAgentId, "agent");
|
|
294
|
+
}
|
|
295
|
+
export function resolveTeamClawAgentWorkspaceRootDir(env = process.env, homedir = os.homedir) {
|
|
296
|
+
const explicitWorkspaceDir = resolveExplicitTeamClawWorkspaceDir(env, homedir);
|
|
297
|
+
if (explicitWorkspaceDir) {
|
|
298
|
+
return explicitWorkspaceDir;
|
|
299
|
+
}
|
|
300
|
+
if (resolveConfiguredTeamClawIsolationMode(env, homedir) === TEAMCLAW_ISOLATION_MODE_MAIN) {
|
|
301
|
+
return resolveDefaultOpenClawWorkspaceDir(env, homedir);
|
|
302
|
+
}
|
|
303
|
+
const configuredEntry = resolveConfiguredAgentEntry(TEAMCLAW_AGENT_ID, env, homedir);
|
|
304
|
+
if (configuredEntry && typeof configuredEntry.workspace === "string" && configuredEntry.workspace.trim()) {
|
|
305
|
+
return expandUserPath(configuredEntry.workspace, homedir);
|
|
306
|
+
}
|
|
307
|
+
return path.join(resolveDefaultOpenClawStateDir(env, homedir), `workspace-${TEAMCLAW_AGENT_ID}`);
|
|
308
|
+
}
|
|
309
|
+
export function buildTeamClawAgentSessionKey(input, env = process.env, homedir = os.homedir) {
|
|
310
|
+
const trimmed = input.trim();
|
|
311
|
+
const agentMatch = trimmed.match(/^agent:[^:]+:(.+)$/i);
|
|
312
|
+
const logicalKey = (agentMatch?.[1] ?? trimmed).trim().toLowerCase() || "main";
|
|
313
|
+
if (resolveConfiguredTeamClawIsolationMode(env, homedir) === TEAMCLAW_ISOLATION_MODE_MAIN) {
|
|
314
|
+
return logicalKey;
|
|
315
|
+
}
|
|
316
|
+
return `agent:${TEAMCLAW_AGENT_ID}:${logicalKey}`;
|
|
317
|
+
}
|
|
318
|
+
export function resolveDefaultTeamClawRuntimeRootDir(env = process.env, homedir = os.homedir) {
|
|
319
|
+
return path.join(resolveDefaultOpenClawStateDir(env, homedir), "teamclaw-runtimes");
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* TeamClaw-specific workspace directory.
|
|
323
|
+
*
|
|
324
|
+
* By default this resolves to a dedicated OpenClaw agent workspace sibling such as
|
|
325
|
+
* `<stateDir>/workspace-teamclaw`.
|
|
326
|
+
*
|
|
327
|
+
* When `agentIsolationMode` is set to `main`, TeamClaw falls back to the legacy
|
|
328
|
+
* shared layout and uses `<default-workspace>/teamclaw`.
|
|
329
|
+
*/
|
|
330
|
+
export function resolveTeamClawWorkspaceDir(env = process.env, homedir = os.homedir) {
|
|
331
|
+
if (resolveConfiguredTeamClawIsolationMode(env, homedir) === TEAMCLAW_ISOLATION_MODE_MAIN) {
|
|
332
|
+
return path.join(resolveTeamClawAgentWorkspaceRootDir(env, homedir), TEAMCLAW_AGENT_ID);
|
|
333
|
+
}
|
|
334
|
+
return resolveTeamClawAgentWorkspaceRootDir(env, homedir);
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* True when the teamclaw workspace is still the implicit default (nothing
|
|
338
|
+
* explicitly configured) - the UI uses this to offer a one-time workspace
|
|
339
|
+
* relocation prompt on first load.
|
|
340
|
+
*/
|
|
341
|
+
export function teamClawWorkspaceIsDefault(env = process.env, homedir = os.homedir) {
|
|
342
|
+
if (resolveExplicitTeamClawWorkspaceDir(env, homedir)) {
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
const entry = resolveConfiguredAgentEntry(TEAMCLAW_AGENT_ID, env, homedir);
|
|
346
|
+
return typeof entry?.workspace !== "string";
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Persist a new teamclaw workspace path into the host config, writing the
|
|
350
|
+
* 2026.10 keyed agents.entries shape (legacy agents.list folded + removed,
|
|
351
|
+
* ownership=explicit for multi-agent rosters). Legacy list-only hosts keep
|
|
352
|
+
* the array shape.
|
|
353
|
+
*/
|
|
354
|
+
export async function updateTeamClawWorkspaceDir(workspaceDir, logger, env = process.env, homedir = os.homedir) {
|
|
355
|
+
if (!workspaceDir.trim()) {
|
|
356
|
+
throw new Error("workspace path is required");
|
|
357
|
+
}
|
|
358
|
+
// Reject relative paths: path.resolve() would silently join them onto the
|
|
359
|
+
// gateway process cwd (which can be the workspace itself), producing a
|
|
360
|
+
// nested garbage path in the host config.
|
|
361
|
+
if (!workspaceDir.trim().startsWith("/") && !workspaceDir.trim().startsWith("~/") && workspaceDir.trim() !== "~") {
|
|
362
|
+
throw new Error("workspace path must be absolute (start with / or ~/)");
|
|
363
|
+
}
|
|
364
|
+
const normalized = path.resolve(expandUserPath(workspaceDir.trim(), homedir));
|
|
365
|
+
const configPath = resolveDefaultOpenClawConfigPath(env, homedir);
|
|
366
|
+
let parsed;
|
|
367
|
+
try {
|
|
368
|
+
parsed = JSON5.parse(await fs.readFile(configPath, "utf8"));
|
|
369
|
+
if (!isRecord(parsed)) {
|
|
370
|
+
throw new Error("host config is not an object");
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
throw new Error(`failed to read host config at ${configPath}: ${err instanceof Error ? err.message : String(err)}`);
|
|
375
|
+
}
|
|
376
|
+
const agents = isRecord(parsed.agents) ? parsed.agents : {};
|
|
377
|
+
const patch = { workspace: normalized };
|
|
378
|
+
const legacyList = Array.isArray(agents.list) ? agents.list.filter(isRecord) : [];
|
|
379
|
+
if (isRecord(agents.entries) || legacyList.length === 0) {
|
|
380
|
+
const entries = isRecord(agents.entries) ? agents.entries : {};
|
|
381
|
+
for (const legacyEntry of legacyList) {
|
|
382
|
+
const legacyId = typeof legacyEntry.id === "string" ? legacyEntry.id : "";
|
|
383
|
+
if (!legacyId || isRecord(entries[legacyId])) {
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
const migrated = cloneJsonValue(legacyEntry);
|
|
387
|
+
delete migrated.id;
|
|
388
|
+
entries[legacyId] = migrated;
|
|
389
|
+
}
|
|
390
|
+
const existing = isRecord(entries[TEAMCLAW_AGENT_ID]) ? entries[TEAMCLAW_AGENT_ID] : {};
|
|
391
|
+
entries[TEAMCLAW_AGENT_ID] = { ...cloneJsonValue(existing), ...patch };
|
|
392
|
+
agents.entries = entries;
|
|
393
|
+
delete agents.list;
|
|
394
|
+
if (Object.keys(entries).length > 1 && agents.ownership !== "explicit") {
|
|
395
|
+
agents.ownership = "explicit";
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
const nextList = [...legacyList];
|
|
400
|
+
const index = nextList.findIndex((entry) => entry.id === TEAMCLAW_AGENT_ID);
|
|
401
|
+
if (index >= 0) {
|
|
402
|
+
nextList[index] = { ...cloneJsonValue(nextList[index]), ...patch };
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
nextList.push({ id: TEAMCLAW_AGENT_ID, ...patch });
|
|
406
|
+
}
|
|
407
|
+
agents.list = nextList;
|
|
408
|
+
}
|
|
409
|
+
parsed.agents = agents;
|
|
410
|
+
await fs.writeFile(configPath, `${JSON.stringify(parsed, null, 2)}\n`, "utf8");
|
|
411
|
+
logger?.warn?.(`TeamClaw: workspace updated to ${normalized} (restart the gateway to apply)`);
|
|
412
|
+
return { configPath, agentEntryPath: normalized };
|
|
413
|
+
}
|
|
414
|
+
// Phase 1.2: the catalog's id set and order derive from the canonical
|
|
415
|
+
// role-ids.ts list (openclaw-workspace.ts sits below roles.ts in the module
|
|
416
|
+
// graph, so it cannot value-import the registry itself). The labels are the
|
|
417
|
+
// compact names the model-settings dialog has always shown; they
|
|
418
|
+
// intentionally differ from the full registry labels in roles.ts (e.g.
|
|
419
|
+
// "Project Manager" vs "Product Manager"), so they stay explicit here.
|
|
420
|
+
// tests/test-role-single-source.mjs pins the derived output to the exact
|
|
421
|
+
// pre-Phase-1.2 value.
|
|
422
|
+
const TEAMCLAW_ROLE_CATALOG_LABELS = {
|
|
423
|
+
pm: "Project Manager",
|
|
424
|
+
architect: "Architect",
|
|
425
|
+
developer: "Developer",
|
|
426
|
+
qa: "QA Engineer",
|
|
427
|
+
"release-engineer": "Release Engineer",
|
|
428
|
+
"infra-engineer": "Infra Engineer",
|
|
429
|
+
devops: "DevOps Engineer",
|
|
430
|
+
"security-engineer": "Security Engineer",
|
|
431
|
+
designer: "Designer",
|
|
432
|
+
marketing: "Marketing",
|
|
433
|
+
"data-engineer": "Data Engineer",
|
|
434
|
+
};
|
|
435
|
+
export const TEAMCLAW_ROLE_CATALOG = ROLE_IDS.map((id) => ({
|
|
436
|
+
id,
|
|
437
|
+
label: TEAMCLAW_ROLE_CATALOG_LABELS[id] ?? id,
|
|
438
|
+
}));
|
|
439
|
+
/**
|
|
440
|
+
* Model catalog for the UI settings dialog: provider/model ids from the host
|
|
441
|
+
* config (names only - never secrets), plus the current teamclaw default and
|
|
442
|
+
* per-role overrides.
|
|
443
|
+
*/
|
|
444
|
+
/**
|
|
445
|
+
* Live host model catalog via `openclaw models list --json` (the gateway's
|
|
446
|
+
* own resolved catalog: provider plugins, dynamic entries, availability).
|
|
447
|
+
* Cached briefly because the CLI spawn costs ~seconds; falls back to the
|
|
448
|
+
* static config models when unavailable.
|
|
449
|
+
*/
|
|
450
|
+
let liveCatalogCache = null;
|
|
451
|
+
let liveCatalogInFlight = null;
|
|
452
|
+
const LIVE_CATALOG_TTL_MS = 30_000;
|
|
453
|
+
/**
|
|
454
|
+
* Resolve how to invoke the openclaw CLI from inside the gateway process.
|
|
455
|
+
* LaunchAgent processes may lack /opt/homebrew/bin on PATH, so prefer
|
|
456
|
+
* node + the host package entry resolved through the plugin's node_modules
|
|
457
|
+
* link (the host links itself there at install time); fall back to the bare
|
|
458
|
+
* command name.
|
|
459
|
+
*/
|
|
460
|
+
function resolveOpenClawCliInvocation() {
|
|
461
|
+
if (process.execPath) {
|
|
462
|
+
try {
|
|
463
|
+
const moduleRequire = createRequire(import.meta.url);
|
|
464
|
+
const hostPkgDir = path.dirname(moduleRequire.resolve("openclaw/package.json"));
|
|
465
|
+
const entry = path.join(hostPkgDir, "openclaw.mjs");
|
|
466
|
+
if (existsSync(entry)) {
|
|
467
|
+
return { command: process.execPath, args: [entry] };
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
catch (resolveErr) {
|
|
471
|
+
// Resolution failure is expected on hosts without the node_modules link;
|
|
472
|
+
// fall through to the bare CLI name.
|
|
473
|
+
void resolveErr;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return { command: "openclaw", args: [] };
|
|
477
|
+
}
|
|
478
|
+
function buildCatalogCliEnv(env) {
|
|
479
|
+
// The gateway LaunchAgent environment carries runtime variables (e.g.
|
|
480
|
+
// OPENCLAW_GATEWAY_PORT) that hijack the CLI's behavior, and may lack the
|
|
481
|
+
// package manager bin dirs. Pass a minimal, PATH-augmented env instead.
|
|
482
|
+
const clean = {
|
|
483
|
+
PATH: ["/opt/homebrew/bin", "/usr/local/bin", env.PATH || "/usr/bin:/bin"].join(path.delimiter),
|
|
484
|
+
};
|
|
485
|
+
for (const key of ["HOME", "USER", "LANG", "OPENCLAW_CONFIG_PATH", "OPENCLAW_STATE_DIR", "OPENCLAW_HOME"]) {
|
|
486
|
+
if (typeof env[key] === "string") {
|
|
487
|
+
clean[key] = env[key];
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return clean;
|
|
491
|
+
}
|
|
492
|
+
async function readLiveModelCatalogProviders(env) {
|
|
493
|
+
const now = Date.now();
|
|
494
|
+
if (liveCatalogCache && now - liveCatalogCache.at < LIVE_CATALOG_TTL_MS) {
|
|
495
|
+
return liveCatalogCache.providers;
|
|
496
|
+
}
|
|
497
|
+
if (liveCatalogInFlight) {
|
|
498
|
+
return liveCatalogInFlight;
|
|
499
|
+
}
|
|
500
|
+
liveCatalogInFlight = (async () => {
|
|
501
|
+
try {
|
|
502
|
+
const invocation = resolveOpenClawCliInvocation();
|
|
503
|
+
// IMPORTANT: `openclaw models list` is a gateway RPC client. Running it
|
|
504
|
+
// synchronously (spawnSync) from inside a gateway plugin service
|
|
505
|
+
// deadlocks: the CLI cannot reach the gateway whose event loop is
|
|
506
|
+
// blocked waiting on it. Async execFile keeps the loop free.
|
|
507
|
+
const { stdout } = await execFileAsync(invocation.command, [...invocation.args, "models", "list", "--json"], {
|
|
508
|
+
timeout: 20_000,
|
|
509
|
+
maxBuffer: 8 * 1024 * 1024,
|
|
510
|
+
env: buildCatalogCliEnv(env),
|
|
511
|
+
});
|
|
512
|
+
const parsed = JSON.parse(stdout);
|
|
513
|
+
const byProvider = new Map();
|
|
514
|
+
if (Array.isArray(parsed.models)) {
|
|
515
|
+
for (const entry of parsed.models) {
|
|
516
|
+
const key = isRecord(entry) && typeof entry.key === "string"
|
|
517
|
+
? entry.key
|
|
518
|
+
: "";
|
|
519
|
+
if (!key || entry.available === false) {
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
const slash = key.indexOf("/");
|
|
523
|
+
const providerId = slash > 0 ? key.slice(0, slash) : key;
|
|
524
|
+
const modelId = slash > 0 ? key.slice(slash + 1) : "";
|
|
525
|
+
if (!modelId) {
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
if (!byProvider.has(providerId)) {
|
|
529
|
+
byProvider.set(providerId, new Set());
|
|
530
|
+
}
|
|
531
|
+
byProvider.get(providerId).add(modelId);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
const providers = [...byProvider.entries()]
|
|
535
|
+
.map(([id, models]) => ({ id, models: [...models].sort() }))
|
|
536
|
+
.sort((left, right) => left.id.localeCompare(right.id));
|
|
537
|
+
liveCatalogCache = { at: now, providers };
|
|
538
|
+
return providers;
|
|
539
|
+
}
|
|
540
|
+
catch (err) {
|
|
541
|
+
console.warn(`[teamclaw] live model catalog unavailable: ${err instanceof Error ? err.message : String(err)}`);
|
|
542
|
+
liveCatalogCache = { at: now, providers: [] };
|
|
543
|
+
return [];
|
|
544
|
+
}
|
|
545
|
+
finally {
|
|
546
|
+
liveCatalogInFlight = null;
|
|
547
|
+
}
|
|
548
|
+
})();
|
|
549
|
+
return liveCatalogInFlight;
|
|
550
|
+
}
|
|
551
|
+
export async function getTeamClawModelCatalog(env = process.env, homedir = os.homedir) {
|
|
552
|
+
const parsedConfig = loadOpenClawConfigRecord(env, homedir);
|
|
553
|
+
const staticProviders = [];
|
|
554
|
+
const modelsSection = parsedConfig && isRecord(parsedConfig.models) ? parsedConfig.models : null;
|
|
555
|
+
const providerMap = modelsSection && isRecord(modelsSection.providers)
|
|
556
|
+
? modelsSection.providers
|
|
557
|
+
: null;
|
|
558
|
+
if (providerMap) {
|
|
559
|
+
for (const [providerId, providerValue] of Object.entries(providerMap)) {
|
|
560
|
+
if (!isRecord(providerValue)) {
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
const rawModels = Array.isArray(providerValue.models)
|
|
564
|
+
? providerValue.models
|
|
565
|
+
: [];
|
|
566
|
+
const models = rawModels
|
|
567
|
+
.map((entry) => {
|
|
568
|
+
if (typeof entry === "string") {
|
|
569
|
+
return entry;
|
|
570
|
+
}
|
|
571
|
+
const id = isRecord(entry) ? entry.id : undefined;
|
|
572
|
+
return typeof id === "string" ? id : "";
|
|
573
|
+
})
|
|
574
|
+
.filter(Boolean);
|
|
575
|
+
staticProviders.push({ id: providerId, models });
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
// Live catalog first; merge static config entries in (dedup) so models the
|
|
579
|
+
// user pinned in config but the live resolver has not surfaced still show.
|
|
580
|
+
const merged = new Map();
|
|
581
|
+
for (const provider of [...(await readLiveModelCatalogProviders(env)), ...staticProviders]) {
|
|
582
|
+
if (!merged.has(provider.id)) {
|
|
583
|
+
merged.set(provider.id, new Set());
|
|
584
|
+
}
|
|
585
|
+
const target = merged.get(provider.id);
|
|
586
|
+
for (const model of provider.models) {
|
|
587
|
+
target.add(model);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
const providers = [...merged.entries()]
|
|
591
|
+
.map(([id, models]) => ({ id, models: [...models].sort() }))
|
|
592
|
+
.sort((left, right) => left.id.localeCompare(right.id));
|
|
593
|
+
let roleModelOverrides = {};
|
|
594
|
+
const pluginsSection = parsedConfig && isRecord(parsedConfig.plugins) ? parsedConfig.plugins : null;
|
|
595
|
+
const pluginEntries = pluginsSection && isRecord(pluginsSection.entries)
|
|
596
|
+
? pluginsSection.entries
|
|
597
|
+
: null;
|
|
598
|
+
const teamclawPlugin = pluginEntries ? pluginEntries[TEAMCLAW_AGENT_ID] : null;
|
|
599
|
+
const pluginConfig = isRecord(teamclawPlugin) ? teamclawPlugin.config : null;
|
|
600
|
+
const overrides = pluginConfig && isRecord(pluginConfig) ? pluginConfig.roleModelOverrides : null;
|
|
601
|
+
if (overrides && isRecord(overrides)) {
|
|
602
|
+
roleModelOverrides = { ...overrides };
|
|
603
|
+
}
|
|
604
|
+
return {
|
|
605
|
+
defaultModel: resolveConfiguredTeamClawModelValue(env, homedir),
|
|
606
|
+
roleModelOverrides,
|
|
607
|
+
roles: TEAMCLAW_ROLE_CATALOG,
|
|
608
|
+
providers,
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Persist model configuration: the teamclaw agent default model (host config
|
|
613
|
+
* agents.entries.teamclaw.model) and per-role overrides (plugin config
|
|
614
|
+
* roleModelOverrides). The gateway hot-reloads config changes; a restart is
|
|
615
|
+
* only needed if a change does not take effect.
|
|
616
|
+
*/
|
|
617
|
+
export async function updateTeamClawModelConfig(update, logger, env = process.env, homedir = os.homedir) {
|
|
618
|
+
const configPath = resolveDefaultOpenClawConfigPath(env, homedir);
|
|
619
|
+
let parsed;
|
|
620
|
+
try {
|
|
621
|
+
parsed = JSON5.parse(await fs.readFile(configPath, "utf8"));
|
|
622
|
+
if (!isRecord(parsed)) {
|
|
623
|
+
throw new Error("host config is not an object");
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
catch (err) {
|
|
627
|
+
throw new Error(`failed to read host config at ${configPath}: ${err instanceof Error ? err.message : String(err)}`);
|
|
628
|
+
}
|
|
629
|
+
if (typeof update.defaultModel === "string" && update.defaultModel.trim()) {
|
|
630
|
+
const agents = isRecord(parsed.agents) ? parsed.agents : {};
|
|
631
|
+
const entries = isRecord(agents.entries) ? agents.entries : {};
|
|
632
|
+
const teamclaw = isRecord(entries[TEAMCLAW_AGENT_ID]) ? entries[TEAMCLAW_AGENT_ID] : {};
|
|
633
|
+
entries[TEAMCLAW_AGENT_ID] = { ...teamclaw, model: update.defaultModel.trim() };
|
|
634
|
+
agents.entries = entries;
|
|
635
|
+
if (Object.keys(entries).length > 1 && agents.ownership !== "explicit") {
|
|
636
|
+
agents.ownership = "explicit";
|
|
637
|
+
}
|
|
638
|
+
parsed.agents = agents;
|
|
639
|
+
}
|
|
640
|
+
if (update.roleModelOverrides !== undefined && update.roleModelOverrides !== null) {
|
|
641
|
+
const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
|
|
642
|
+
const pluginEntries = isRecord(plugins.entries) ? plugins.entries : {};
|
|
643
|
+
const teamclawPlugin = isRecord(pluginEntries[TEAMCLAW_AGENT_ID]) ? pluginEntries[TEAMCLAW_AGENT_ID] : { enabled: true };
|
|
644
|
+
const pluginConfig = isRecord(teamclawPlugin.config)
|
|
645
|
+
? teamclawPlugin.config
|
|
646
|
+
: {};
|
|
647
|
+
pluginEntries[TEAMCLAW_AGENT_ID] = {
|
|
648
|
+
...teamclawPlugin,
|
|
649
|
+
config: { ...pluginConfig, roleModelOverrides: update.roleModelOverrides },
|
|
650
|
+
};
|
|
651
|
+
plugins.entries = pluginEntries;
|
|
652
|
+
parsed.plugins = plugins;
|
|
653
|
+
}
|
|
654
|
+
await fs.writeFile(configPath, `${JSON.stringify(parsed, null, 2)}\n`, "utf8");
|
|
655
|
+
logger?.warn?.("TeamClaw: model config updated (hot-reload applies; restart the gateway if a change does not take effect)");
|
|
656
|
+
return { configPath };
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Resolve the `projects/` root inside the TeamClaw workspace.
|
|
660
|
+
* Each orchestration run or ad-hoc task gets its own subdirectory here.
|
|
661
|
+
*/
|
|
662
|
+
export function resolveTeamClawProjectsDir(env, homedir) {
|
|
663
|
+
return path.join(resolveTeamClawWorkspaceDir(env, homedir), "projects");
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Resolve a project path relative to the active TeamClaw workspace.
|
|
667
|
+
* Workers operate inside the TeamClaw workspace itself, so project-local file
|
|
668
|
+
* operations should use `projects/<projectDir>` regardless of isolation mode.
|
|
669
|
+
*/
|
|
670
|
+
export function buildTeamClawProjectWorkspacePath(projectDir) {
|
|
671
|
+
const normalized = String(projectDir || "").trim().replace(/^\/+|\/+$/gu, "");
|
|
672
|
+
return normalized ? `projects/${normalized}` : "projects";
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Resolve a project path relative to the TeamClaw agent workspace root.
|
|
676
|
+
* Preview metadata is evaluated from the agent workspace root, which differs
|
|
677
|
+
* between `independent` and `main` isolation modes.
|
|
678
|
+
*/
|
|
679
|
+
export function buildTeamClawProjectAgentRelativePath(projectDir, env = process.env, homedir = os.homedir) {
|
|
680
|
+
const workspaceRelative = buildTeamClawProjectWorkspacePath(projectDir);
|
|
681
|
+
const agentWorkspaceRoot = resolveTeamClawAgentWorkspaceRootDir(env, homedir);
|
|
682
|
+
const teamclawWorkspaceDir = resolveTeamClawWorkspaceDir(env, homedir);
|
|
683
|
+
const absoluteProjectPath = path.join(teamclawWorkspaceDir, workspaceRelative);
|
|
684
|
+
const relativeToAgentRoot = path.relative(agentWorkspaceRoot, absoluteProjectPath).replace(/\\/gu, "/");
|
|
685
|
+
return relativeToAgentRoot || ".";
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Derive a filesystem-safe project slug from free-form text.
|
|
689
|
+
*
|
|
690
|
+
* 1. Lower-case, replace non-alphanumeric runs with hyphens, trim to ~50 chars.
|
|
691
|
+
* 2. Append a short random suffix to avoid collisions.
|
|
692
|
+
*
|
|
693
|
+
* Example: "Build a payment system with Stripe" → "build-a-payment-system-with-stripe-k3f9m2"
|
|
694
|
+
*/
|
|
695
|
+
export function deriveStableProjectKey(text) {
|
|
696
|
+
return text
|
|
697
|
+
.toLowerCase()
|
|
698
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
699
|
+
.replace(/^-+|-+$/g, "")
|
|
700
|
+
.slice(0, 50)
|
|
701
|
+
.replace(/-+$/g, "");
|
|
702
|
+
}
|
|
703
|
+
export function deriveProjectSlug(text) {
|
|
704
|
+
const slug = deriveStableProjectKey(text);
|
|
705
|
+
const suffix = randomSuffix(6);
|
|
706
|
+
return slug ? `${slug}-${suffix}` : suffix;
|
|
707
|
+
}
|
|
708
|
+
function randomSuffix(length) {
|
|
709
|
+
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
710
|
+
let result = "";
|
|
711
|
+
for (let i = 0; i < length; i++) {
|
|
712
|
+
result += chars[Math.floor(Math.random() * chars.length)];
|
|
713
|
+
}
|
|
714
|
+
return result;
|
|
715
|
+
}
|
|
716
|
+
export async function ensureOpenClawWorkspaceMemoryDir(logger) {
|
|
717
|
+
await ensureTeamClawAgentBootstrap(logger);
|
|
718
|
+
const workspaceDir = resolveTeamClawWorkspaceDir();
|
|
719
|
+
const memoryDir = path.join(workspaceDir, "memory");
|
|
720
|
+
try {
|
|
721
|
+
await fs.mkdir(workspaceDir, { recursive: true });
|
|
722
|
+
await fs.mkdir(memoryDir, { recursive: true });
|
|
723
|
+
await ensureFileIfMissing(path.join(workspaceDir, "AGENTS.md"), DEFAULT_AGENTS_MD);
|
|
724
|
+
await ensureFileIfMissing(path.join(workspaceDir, "BOOTSTRAP.md"), DEFAULT_BOOTSTRAP_MD);
|
|
725
|
+
await ensureFileIfMissing(path.join(workspaceDir, "HEARTBEAT.md"), DEFAULT_HEARTBEAT_MD);
|
|
726
|
+
await ensureFileIfMissing(path.join(memoryDir, "patterns.md"), DEFAULT_PATTERNS_MD);
|
|
727
|
+
}
|
|
728
|
+
catch (err) {
|
|
729
|
+
logger.warn(`TeamClaw: failed to ensure OpenClaw workspace memory dir at ${memoryDir}: ${err instanceof Error ? err.message : String(err)}`);
|
|
730
|
+
}
|
|
731
|
+
return memoryDir;
|
|
732
|
+
}
|
|
733
|
+
export async function ensureTeamClawAgentBootstrap(logger) {
|
|
734
|
+
const teamclawAgentDir = resolveTeamClawAgentDir();
|
|
735
|
+
const teamclawWorkspaceRoot = resolveTeamClawAgentWorkspaceRootDir();
|
|
736
|
+
const isolationMode = resolveConfiguredTeamClawIsolationMode();
|
|
737
|
+
try {
|
|
738
|
+
await fs.mkdir(teamclawWorkspaceRoot, { recursive: true });
|
|
739
|
+
if (isolationMode === TEAMCLAW_ISOLATION_MODE_MAIN) {
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
await ensureTeamClawAgentConfigBootstrap(logger);
|
|
743
|
+
await fs.mkdir(teamclawAgentDir, { recursive: true });
|
|
744
|
+
const sourceAgentDirs = [
|
|
745
|
+
resolveDefaultAgentDir(),
|
|
746
|
+
path.join(resolveDefaultOpenClawStateDir(), "agents", "main", "agent"),
|
|
747
|
+
].filter((value, index, values) => values.indexOf(value) === index);
|
|
748
|
+
for (const sourceAgentDir of sourceAgentDirs) {
|
|
749
|
+
const sourceAuthProfilesPath = path.join(sourceAgentDir, "auth-profiles.json");
|
|
750
|
+
try {
|
|
751
|
+
await fs.access(sourceAuthProfilesPath);
|
|
752
|
+
}
|
|
753
|
+
catch {
|
|
754
|
+
continue;
|
|
755
|
+
}
|
|
756
|
+
const targetAuthProfilesPath = path.join(teamclawAgentDir, "auth-profiles.json");
|
|
757
|
+
await fs.copyFile(sourceAuthProfilesPath, targetAuthProfilesPath);
|
|
758
|
+
logger.info(`TeamClaw: bootstrapped dedicated agent auth from ${sourceAuthProfilesPath}`);
|
|
759
|
+
break;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
catch (err) {
|
|
763
|
+
logger.warn(`TeamClaw: failed to bootstrap dedicated agent runtime: ${err instanceof Error ? err.message : String(err)}`);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
async function ensureTeamClawAgentConfigBootstrap(logger) {
|
|
767
|
+
const configPath = resolveDefaultOpenClawConfigPath();
|
|
768
|
+
let raw;
|
|
769
|
+
try {
|
|
770
|
+
raw = await fs.readFile(configPath, "utf8");
|
|
771
|
+
}
|
|
772
|
+
catch {
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
let parsed;
|
|
776
|
+
try {
|
|
777
|
+
const candidate = JSON5.parse(raw);
|
|
778
|
+
if (!isRecord(candidate)) {
|
|
779
|
+
return;
|
|
780
|
+
}
|
|
781
|
+
parsed = candidate;
|
|
782
|
+
}
|
|
783
|
+
catch {
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
const agents = isRecord(parsed.agents) ? parsed.agents : {};
|
|
787
|
+
const defaults = isRecord(agents.defaults) ? agents.defaults : {};
|
|
788
|
+
const rootTools = isRecord(parsed.tools) ? parsed.tools : {};
|
|
789
|
+
const rootExec = isRecord(rootTools.exec) ? rootTools.exec : {};
|
|
790
|
+
const plugins = isRecord(parsed.plugins) ? parsed.plugins : {};
|
|
791
|
+
const pluginEntries = isRecord(plugins.entries) ? plugins.entries : {};
|
|
792
|
+
const teamclawPluginEntry = isRecord(pluginEntries[TEAMCLAW_AGENT_ID]) ? pluginEntries[TEAMCLAW_AGENT_ID] : {};
|
|
793
|
+
const existingPluginConfig = isRecord(teamclawPluginEntry.config) ? teamclawPluginEntry.config : {};
|
|
794
|
+
const teamclawAgentDir = path.join(resolveDefaultOpenClawStateDir(), "agents", TEAMCLAW_AGENT_ID, "agent");
|
|
795
|
+
// OpenClaw 2026.10+ rejects agents.list ("moved to keyed agents.entries")
|
|
796
|
+
// and rejects tools.exec objects mixing mode with security/ask. Write the
|
|
797
|
+
// keyed shape whenever the host already uses it, and never reintroduce the
|
|
798
|
+
// legacy list on modern hosts.
|
|
799
|
+
const agentEntries = isRecord(agents.entries) ? agents.entries : {};
|
|
800
|
+
const legacyList = Array.isArray(agents.list) ? agents.list.filter(isRecord) : [];
|
|
801
|
+
const modernSchema = isRecord(agents.entries) || legacyList.length === 0;
|
|
802
|
+
const findExistingAgentEntry = () => {
|
|
803
|
+
const keyed = agentEntries[TEAMCLAW_AGENT_ID];
|
|
804
|
+
if (isRecord(keyed)) {
|
|
805
|
+
return { entry: keyed, source: "entries" };
|
|
806
|
+
}
|
|
807
|
+
const fromList = legacyList.find((entry) => normalizeAgentId(entry.id) === TEAMCLAW_AGENT_ID);
|
|
808
|
+
if (fromList) {
|
|
809
|
+
return { entry: fromList, source: "list" };
|
|
810
|
+
}
|
|
811
|
+
return { entry: {}, source: "none" };
|
|
812
|
+
};
|
|
813
|
+
const existing = findExistingAgentEntry();
|
|
814
|
+
const existingEntry = existing.entry;
|
|
815
|
+
const existingEntryTools = isRecord(existingEntry.tools) ? existingEntry.tools : {};
|
|
816
|
+
const existingEntryExec = isRecord(existingEntryTools.exec) ? existingEntryTools.exec : {};
|
|
817
|
+
const nextEntryExec = {
|
|
818
|
+
...cloneJsonValue(rootExec),
|
|
819
|
+
...cloneJsonValue(existingEntryExec),
|
|
820
|
+
};
|
|
821
|
+
if (typeof nextEntryExec.mode === "string" && nextEntryExec.mode.trim()) {
|
|
822
|
+
// Keyed-schema hosts forbid combining mode with security/ask.
|
|
823
|
+
delete nextEntryExec.security;
|
|
824
|
+
delete nextEntryExec.ask;
|
|
825
|
+
}
|
|
826
|
+
else {
|
|
827
|
+
if (typeof nextEntryExec.security !== "string" || !nextEntryExec.security.trim()) {
|
|
828
|
+
nextEntryExec.security = typeof rootExec.security === "string" && rootExec.security.trim()
|
|
829
|
+
? rootExec.security.trim()
|
|
830
|
+
: TEAMCLAW_RECOMMENDED_EXEC_SECURITY;
|
|
831
|
+
}
|
|
832
|
+
if (typeof nextEntryExec.ask !== "string" || !nextEntryExec.ask.trim()) {
|
|
833
|
+
nextEntryExec.ask = typeof rootExec.ask === "string" && rootExec.ask.trim()
|
|
834
|
+
? rootExec.ask.trim()
|
|
835
|
+
: TEAMCLAW_RECOMMENDED_EXEC_ASK;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
const nextEntry = {
|
|
839
|
+
...cloneJsonValue(existingEntry),
|
|
840
|
+
agentDir: typeof existingEntry.agentDir === "string" && existingEntry.agentDir.trim()
|
|
841
|
+
? existingEntry.agentDir
|
|
842
|
+
: teamclawAgentDir,
|
|
843
|
+
tools: {
|
|
844
|
+
...cloneJsonValue(existingEntryTools),
|
|
845
|
+
exec: nextEntryExec,
|
|
846
|
+
},
|
|
847
|
+
};
|
|
848
|
+
// Keep the workspace implicit when unset: materializing the default here
|
|
849
|
+
// would make "default" and "explicitly configured" indistinguishable, which
|
|
850
|
+
// the UI workspace-relocation prompt depends on. The runtime resolver falls
|
|
851
|
+
// back to <stateDir>/workspace-teamclaw either way.
|
|
852
|
+
if (typeof existingEntry.workspace === "string" && existingEntry.workspace.trim()) {
|
|
853
|
+
nextEntry.workspace = existingEntry.workspace;
|
|
854
|
+
}
|
|
855
|
+
if (!modernSchema) {
|
|
856
|
+
nextEntry.id = TEAMCLAW_AGENT_ID;
|
|
857
|
+
}
|
|
858
|
+
if (nextEntry.model == null && defaults.model != null) {
|
|
859
|
+
nextEntry.model = cloneJsonValue(defaults.model);
|
|
860
|
+
}
|
|
861
|
+
const nextPluginConfig = {
|
|
862
|
+
...existingPluginConfig,
|
|
863
|
+
};
|
|
864
|
+
if (nextPluginConfig.mode === "controller"
|
|
865
|
+
&& nextPluginConfig.processModel === "multi"
|
|
866
|
+
&& nextPluginConfig.workerProvisioningType === "none"
|
|
867
|
+
&& nextPluginConfig.workerProvisioningDisabled !== true) {
|
|
868
|
+
nextPluginConfig.workerProvisioningType = "process";
|
|
869
|
+
}
|
|
870
|
+
const entryChanged = JSON.stringify(existingEntry) !== JSON.stringify(nextEntry)
|
|
871
|
+
|| (modernSchema && (legacyList.length > 0 || !isRecord(agentEntries[TEAMCLAW_AGENT_ID])));
|
|
872
|
+
const pluginConfigChanged = JSON.stringify(existingPluginConfig) !== JSON.stringify(nextPluginConfig);
|
|
873
|
+
if (!entryChanged && !pluginConfigChanged) {
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
if (modernSchema) {
|
|
877
|
+
// Fold any legacy list entries into the keyed map, then drop the list so
|
|
878
|
+
// 2026.10 hosts stop rejecting the config.
|
|
879
|
+
for (const legacyEntry of legacyList) {
|
|
880
|
+
const legacyId = normalizeAgentId(legacyEntry.id);
|
|
881
|
+
if (!legacyId || isRecord(agentEntries[legacyId])) {
|
|
882
|
+
continue;
|
|
883
|
+
}
|
|
884
|
+
const migrated = cloneJsonValue(legacyEntry);
|
|
885
|
+
delete migrated.id;
|
|
886
|
+
agentEntries[legacyId] = migrated;
|
|
887
|
+
}
|
|
888
|
+
const keyedNextEntry = cloneJsonValue(nextEntry);
|
|
889
|
+
delete keyedNextEntry.id;
|
|
890
|
+
agentEntries[TEAMCLAW_AGENT_ID] = keyedNextEntry;
|
|
891
|
+
agents.entries = agentEntries;
|
|
892
|
+
delete agents.list;
|
|
893
|
+
if (Object.keys(agentEntries).length > 1 && agents.ownership !== "explicit") {
|
|
894
|
+
agents.ownership = "explicit";
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
else {
|
|
898
|
+
const nextList = [...legacyList];
|
|
899
|
+
const existingIndex = nextList.findIndex((entry) => isRecord(entry) && normalizeAgentId(entry.id) === TEAMCLAW_AGENT_ID);
|
|
900
|
+
if (existingIndex >= 0) {
|
|
901
|
+
nextList[existingIndex] = nextEntry;
|
|
902
|
+
}
|
|
903
|
+
else {
|
|
904
|
+
nextList.push(nextEntry);
|
|
905
|
+
}
|
|
906
|
+
agents.list = nextList;
|
|
907
|
+
}
|
|
908
|
+
parsed.agents = agents;
|
|
909
|
+
if (pluginConfigChanged) {
|
|
910
|
+
teamclawPluginEntry.config = nextPluginConfig;
|
|
911
|
+
pluginEntries[TEAMCLAW_AGENT_ID] = teamclawPluginEntry;
|
|
912
|
+
plugins.entries = pluginEntries;
|
|
913
|
+
parsed.plugins = plugins;
|
|
914
|
+
}
|
|
915
|
+
await fs.writeFile(configPath, `${JSON.stringify(parsed, null, 2)}\n`, "utf8");
|
|
916
|
+
logger.info(`TeamClaw: bootstrapped dedicated agent config into ${configPath}`);
|
|
917
|
+
}
|
|
918
|
+
async function ensureFileIfMissing(filePath, content) {
|
|
919
|
+
try {
|
|
920
|
+
await fs.access(filePath);
|
|
921
|
+
}
|
|
922
|
+
catch {
|
|
923
|
+
await fs.writeFile(filePath, content, "utf8");
|
|
924
|
+
}
|
|
925
|
+
}
|