refactor: integrate OpenClaw API for configuration management and CLI registration
This commit is contained in:
@@ -5,18 +5,16 @@ import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
function openclawConfigPath(): string {
|
||||
return path.join(process.env.HOME ?? "/home/lauren", ".openclaw", "openclaw.json");
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new agent via `openclaw agents add`.
|
||||
* Cleans up .git and BOOTSTRAP.md from the workspace, updates display name.
|
||||
*/
|
||||
export async function createAgent(
|
||||
api: OpenClawPluginApi,
|
||||
name: string,
|
||||
channelBinding?: "telegram" | "whatsapp" | null,
|
||||
): Promise<{ agentId: string; workspacePath: string }> {
|
||||
@@ -25,13 +23,7 @@ export async function createAgent(
|
||||
.replace(/[^a-z0-9]+/g, "-")
|
||||
.replace(/^-|-$/g, "");
|
||||
|
||||
const workspacePath = path.join(
|
||||
process.env.HOME ?? "/home/lauren",
|
||||
".openclaw",
|
||||
`workspace-${agentId}`,
|
||||
);
|
||||
|
||||
const args = ["agents", "add", agentId, "--workspace", workspacePath, "--non-interactive"];
|
||||
const args = ["agents", "add", agentId, "--non-interactive"];
|
||||
if (channelBinding) args.push("--bind", channelBinding);
|
||||
|
||||
try {
|
||||
@@ -40,20 +32,19 @@ export async function createAgent(
|
||||
throw new Error(`Failed to create agent "${name}": ${(err as Error).message}`);
|
||||
}
|
||||
|
||||
const workspacePath = resolveWorkspacePath(api, agentId);
|
||||
await cleanupWorkspace(workspacePath);
|
||||
await updateAgentDisplayName(agentId, name);
|
||||
await updateAgentDisplayName(api, agentId, name);
|
||||
|
||||
return { agentId, workspacePath };
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve workspace path from an agent ID by reading openclaw.json.
|
||||
* Resolve workspace path from an agent ID via OpenClaw config API.
|
||||
*/
|
||||
export async function resolveWorkspacePath(agentId: string): Promise<string> {
|
||||
const raw = await fs.readFile(openclawConfigPath(), "utf-8");
|
||||
const config = JSON.parse(raw);
|
||||
|
||||
const agent = config.agents?.list?.find((a: { id: string }) => a.id === agentId);
|
||||
export function resolveWorkspacePath(api: OpenClawPluginApi, agentId: string): string {
|
||||
const config = api.runtime.config.loadConfig();
|
||||
const agent = config.agents?.list?.find((a) => a.id === agentId);
|
||||
if (!agent?.workspace) {
|
||||
throw new Error(`Agent "${agentId}" not found in openclaw.json or has no workspace configured.`);
|
||||
}
|
||||
@@ -71,15 +62,14 @@ async function cleanupWorkspace(workspacePath: string): Promise<void> {
|
||||
try { await fs.unlink(path.join(workspacePath, "BOOTSTRAP.md")); } catch { /* may not exist */ }
|
||||
}
|
||||
|
||||
async function updateAgentDisplayName(agentId: string, name: string): Promise<void> {
|
||||
async function updateAgentDisplayName(api: OpenClawPluginApi, agentId: string, name: string): Promise<void> {
|
||||
if (name === agentId) return;
|
||||
try {
|
||||
const configPath = openclawConfigPath();
|
||||
const config = JSON.parse(await fs.readFile(configPath, "utf-8"));
|
||||
const agent = config.agents?.list?.find((a: { id: string }) => a.id === agentId);
|
||||
const config = api.runtime.config.loadConfig();
|
||||
const agent = config.agents?.list?.find((a) => a.id === agentId);
|
||||
if (agent) {
|
||||
agent.name = name;
|
||||
await fs.writeFile(configPath, JSON.stringify(config, null, 2) + "\n", "utf-8");
|
||||
(agent as any).name = name;
|
||||
await api.runtime.config.writeConfigFile(config);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn(`Warning: Could not update display name: ${(err as Error).message}`);
|
||||
|
||||
@@ -3,16 +3,11 @@
|
||||
*
|
||||
* Handles: model level config, devClawAgentIds, tool restrictions, subagent cleanup.
|
||||
*/
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
||||
import { HEARTBEAT_DEFAULTS } from "../services/heartbeat.js";
|
||||
|
||||
type ModelConfig = { dev: Record<string, string>; qa: Record<string, string> };
|
||||
|
||||
function openclawConfigPath(): string {
|
||||
return path.join(process.env.HOME ?? "/home/lauren", ".openclaw", "openclaw.json");
|
||||
}
|
||||
|
||||
/**
|
||||
* Write DevClaw model level config and devClawAgentIds to openclaw.json plugins section.
|
||||
*
|
||||
@@ -23,18 +18,18 @@ function openclawConfigPath(): string {
|
||||
* Read-modify-write to preserve existing config.
|
||||
*/
|
||||
export async function writePluginConfig(
|
||||
api: OpenClawPluginApi,
|
||||
models: ModelConfig,
|
||||
agentId?: string,
|
||||
projectExecution?: "parallel" | "sequential",
|
||||
): Promise<void> {
|
||||
const configPath = openclawConfigPath();
|
||||
const config = JSON.parse(await fs.readFile(configPath, "utf-8"));
|
||||
const config = api.runtime.config.loadConfig() as Record<string, unknown>;
|
||||
|
||||
ensurePluginStructure(config);
|
||||
config.plugins.entries.devclaw.config.models = models;
|
||||
(config as any).plugins.entries.devclaw.config.models = models;
|
||||
|
||||
if (projectExecution) {
|
||||
config.plugins.entries.devclaw.config.projectExecution = projectExecution;
|
||||
(config as any).plugins.entries.devclaw.config.projectExecution = projectExecution;
|
||||
}
|
||||
|
||||
ensureHeartbeatDefaults(config);
|
||||
@@ -45,9 +40,7 @@ export async function writePluginConfig(
|
||||
addToolRestrictions(config, agentId);
|
||||
}
|
||||
|
||||
const tmpPath = configPath + ".tmp";
|
||||
await fs.writeFile(tmpPath, JSON.stringify(config, null, 2) + "\n", "utf-8");
|
||||
await fs.rename(tmpPath, configPath);
|
||||
await api.runtime.config.writeConfigFile(config as any);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Coordinates: agent creation → model config → workspace scaffolding.
|
||||
* Used by both the `setup` tool and the `openclaw devclaw setup` CLI command.
|
||||
*/
|
||||
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
|
||||
import { DEFAULT_MODELS } from "../tiers.js";
|
||||
import { migrateChannelBinding } from "../binding-manager.js";
|
||||
import { createAgent, resolveWorkspacePath } from "./agent.js";
|
||||
@@ -13,6 +14,8 @@ import { scaffoldWorkspace } from "./workspace.js";
|
||||
export type ModelConfig = { dev: Record<string, string>; qa: Record<string, string> };
|
||||
|
||||
export type SetupOpts = {
|
||||
/** OpenClaw plugin API for config access. */
|
||||
api: OpenClawPluginApi;
|
||||
/** Create a new agent with this name. Mutually exclusive with agentId. */
|
||||
newAgentName?: string;
|
||||
/** Channel binding for new agent. Only used when newAgentName is set. */
|
||||
@@ -56,7 +59,7 @@ export async function runSetup(opts: SetupOpts): Promise<SetupResult> {
|
||||
await resolveOrCreateAgent(opts, warnings);
|
||||
|
||||
const models = buildModelConfig(opts.models);
|
||||
await writePluginConfig(models, agentId, opts.projectExecution);
|
||||
await writePluginConfig(opts.api, models, agentId, opts.projectExecution);
|
||||
|
||||
const filesWritten = await scaffoldWorkspace(workspacePath);
|
||||
|
||||
@@ -77,13 +80,13 @@ async function resolveOrCreateAgent(
|
||||
bindingMigrated?: SetupResult["bindingMigrated"];
|
||||
}> {
|
||||
if (opts.newAgentName) {
|
||||
const { agentId, workspacePath } = await createAgent(opts.newAgentName, opts.channelBinding);
|
||||
const { agentId, workspacePath } = await createAgent(opts.api, opts.newAgentName, opts.channelBinding);
|
||||
const bindingMigrated = await tryMigrateBinding(opts, agentId, warnings);
|
||||
return { agentId, workspacePath, agentCreated: true, bindingMigrated };
|
||||
}
|
||||
|
||||
if (opts.agentId) {
|
||||
const workspacePath = opts.workspacePath ?? await resolveWorkspacePath(opts.agentId);
|
||||
const workspacePath = opts.workspacePath ?? resolveWorkspacePath(opts.api, opts.agentId);
|
||||
return { agentId: opts.agentId, workspacePath, agentCreated: false };
|
||||
}
|
||||
|
||||
@@ -101,7 +104,7 @@ async function tryMigrateBinding(
|
||||
): Promise<SetupResult["bindingMigrated"]> {
|
||||
if (!opts.migrateFrom || !opts.channelBinding) return undefined;
|
||||
try {
|
||||
await migrateChannelBinding(opts.channelBinding, opts.migrateFrom, agentId);
|
||||
await migrateChannelBinding(opts.api, opts.channelBinding, opts.migrateFrom, agentId);
|
||||
return { from: opts.migrateFrom, channel: opts.channelBinding };
|
||||
} catch (err) {
|
||||
warnings.push(`Failed to migrate binding from "${opts.migrateFrom}": ${(err as Error).message}`);
|
||||
|
||||
Reference in New Issue
Block a user