forked from laurentenhoor/devclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ts
More file actions
139 lines (132 loc) · 5.18 KB
/
setup.ts
File metadata and controls
139 lines (132 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* setup — Agent-driven DevClaw setup.
*
* Creates agent, configures model levels, writes workspace files.
* Thin wrapper around lib/setup/.
*/
import { jsonResult } from "openclaw/plugin-sdk";
import type { ToolContext } from "../../types.js";
import type { PluginContext } from "../../context.js";
import { runSetup, type SetupOpts } from "../../setup/index.js";
import { writeAllDefaults } from "../../setup/workspace.js";
import { getAllDefaultModels, getAllRoleIds, getLevelsForRole } from "../../roles/index.js";
import { ExecutionMode } from "../../workflow/index.js";
export function createSetupTool(ctx: PluginContext) {
return (toolCtx: ToolContext) => ({
name: "setup",
label: "Setup",
description: `Execute DevClaw setup. Creates AGENTS.md, HEARTBEAT.md, TOOLS.md, devclaw/projects.json, devclaw/prompts/, and model level config. Optionally creates a new agent with channel binding. Called after onboard collects configuration.`,
parameters: {
type: "object",
properties: {
newAgentName: {
type: "string",
description:
"Create a new agent. Omit to configure current workspace.",
},
channelBinding: {
type: "string",
enum: ["telegram", "whatsapp"],
description: "Channel to bind (optional, with newAgentName only).",
},
migrateFrom: {
type: "string",
description:
"Agent ID to migrate channel binding from. Check openclaw.json bindings first.",
},
models: {
type: "object",
description: "Model overrides per role and level.",
properties: Object.fromEntries(
getAllRoleIds().map((role) => [role, {
type: "object",
description: `${role.toUpperCase()} level models`,
properties: Object.fromEntries(
getLevelsForRole(role).map((level) => [level, {
type: "string",
description: `Default: ${getAllDefaultModels()[role]?.[level] ?? "auto"}`,
}]),
),
}]),
),
},
projectExecution: {
type: "string",
enum: Object.values(ExecutionMode),
description: "Project execution mode. Default: parallel.",
},
ejectDefaults: {
type: "boolean",
description: "Write all package defaults to workspace. Skips files that already exist.",
},
resetDefaults: {
type: "boolean",
description: "Force-write all package defaults to workspace, overwriting existing files. Creates .bak backups.",
},
},
},
async execute(_id: string, params: Record<string, unknown>) {
// Handle --eject-defaults and --reset-defaults (standalone operations)
if (params.ejectDefaults || params.resetDefaults) {
const workspacePath = toolCtx.workspaceDir;
if (!workspacePath) throw new Error("No workspace directory available");
const force = !!params.resetDefaults;
const written = await writeAllDefaults(workspacePath, force);
const action = force ? "Reset (force-wrote)" : "Ejected (wrote missing)";
return jsonResult({
success: true,
action: force ? "reset-defaults" : "eject-defaults",
filesWritten: written,
summary: written.length > 0
? `${action} ${written.length} file(s):\n${written.map(f => ` ${f}`).join("\n")}`
: "All files already exist — nothing to write.",
});
}
const result = await runSetup({
runtime: ctx.runtime,
newAgentName: params.newAgentName as string | undefined,
channelBinding:
(params.channelBinding as "telegram" | "whatsapp") ?? null,
migrateFrom: params.migrateFrom as string | undefined,
agentId: params.newAgentName ? undefined : toolCtx.agentId,
workspacePath: params.newAgentName ? undefined : toolCtx.workspaceDir,
models: params.models as SetupOpts["models"],
projectExecution: params.projectExecution as
| ExecutionMode
| undefined,
runCommand: ctx.runCommand,
});
const lines = [
result.agentCreated
? `Agent "${result.agentId}" created`
: `Configured "${result.agentId}"`,
"",
];
if (result.bindingMigrated) {
lines.push(
`✅ Binding migrated: ${result.bindingMigrated.channel} (${result.bindingMigrated.from} → ${result.agentId})`,
"",
);
}
lines.push("Models:");
for (const [role, levels] of Object.entries(result.models)) {
for (const [level, model] of Object.entries(levels)) {
lines.push(` ${role}.${level}: ${model}`);
}
}
lines.push("");
lines.push("Files:", ...result.filesWritten.map((f) => ` ${f}`));
if (result.warnings.length > 0)
lines.push("", "Warnings:", ...result.warnings.map((w) => ` ${w}`));
lines.push(
"",
"Next: register a project, then create issues and pick them up.",
);
return jsonResult({
success: true,
...result,
summary: lines.join("\n"),
});
},
});
}