feat(agent): enhance agent management with runtime and workspace directory removal, and improve agent ID listing (#387)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { access, copyFile, mkdir, readdir } from 'fs/promises';
|
||||
import { access, copyFile, mkdir, readdir, rm } from 'fs/promises';
|
||||
import { constants } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { join, normalize } from 'path';
|
||||
import { listConfiguredChannels, readOpenClawConfig, writeOpenClawConfig } from './channel-config';
|
||||
import { expandPath, getOpenClawConfigDir } from './paths';
|
||||
import * as logger from './logger';
|
||||
@@ -208,7 +208,8 @@ function getSimpleChannelBindingMap(bindings: unknown): Map<string, string> {
|
||||
for (const binding of bindings) {
|
||||
if (!isSimpleChannelBinding(binding)) continue;
|
||||
const agentId = normalizeAgentIdForBinding(binding.agentId!);
|
||||
if (agentId) owners.set(binding.match.channel!, agentId);
|
||||
const channel = binding.match?.channel;
|
||||
if (agentId && channel) owners.set(channel, agentId);
|
||||
}
|
||||
|
||||
return owners;
|
||||
@@ -221,7 +222,7 @@ function upsertBindingsForChannel(
|
||||
): BindingConfig[] | undefined {
|
||||
const nextBindings = Array.isArray(bindings)
|
||||
? [...bindings as BindingConfig[]].filter((binding) => !(
|
||||
isSimpleChannelBinding(binding) && binding.match.channel === channelType
|
||||
isSimpleChannelBinding(binding) && binding.match?.channel === channelType
|
||||
))
|
||||
: [];
|
||||
|
||||
@@ -252,6 +253,55 @@ async function listExistingAgentIdsOnDisk(): Promise<Set<string>> {
|
||||
return ids;
|
||||
}
|
||||
|
||||
async function removeAgentRuntimeDirectory(agentId: string): Promise<void> {
|
||||
const runtimeDir = join(getOpenClawConfigDir(), 'agents', agentId);
|
||||
try {
|
||||
await rm(runtimeDir, { recursive: true, force: true });
|
||||
} catch (error) {
|
||||
logger.warn('Failed to remove agent runtime directory', {
|
||||
agentId,
|
||||
runtimeDir,
|
||||
error: String(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function trimTrailingSeparators(path: string): string {
|
||||
return path.replace(/[\\/]+$/, '');
|
||||
}
|
||||
|
||||
function getManagedWorkspaceDirectory(agent: AgentListEntry): string | null {
|
||||
if (agent.id === MAIN_AGENT_ID) return null;
|
||||
|
||||
const configuredWorkspace = expandPath(agent.workspace || `~/.openclaw/workspace-${agent.id}`);
|
||||
const managedWorkspace = join(getOpenClawConfigDir(), `workspace-${agent.id}`);
|
||||
const normalizedConfigured = trimTrailingSeparators(normalize(configuredWorkspace));
|
||||
const normalizedManaged = trimTrailingSeparators(normalize(managedWorkspace));
|
||||
|
||||
return normalizedConfigured === normalizedManaged ? configuredWorkspace : null;
|
||||
}
|
||||
|
||||
async function removeAgentWorkspaceDirectory(agent: AgentListEntry): Promise<void> {
|
||||
const workspaceDir = getManagedWorkspaceDirectory(agent);
|
||||
if (!workspaceDir) {
|
||||
logger.warn('Skipping agent workspace deletion for unmanaged path', {
|
||||
agentId: agent.id,
|
||||
workspace: agent.workspace,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await rm(workspaceDir, { recursive: true, force: true });
|
||||
} catch (error) {
|
||||
logger.warn('Failed to remove agent workspace directory', {
|
||||
agentId: agent.id,
|
||||
workspaceDir,
|
||||
error: String(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function copyBootstrapFiles(sourceWorkspace: string, targetWorkspace: string): Promise<void> {
|
||||
await ensureDir(targetWorkspace);
|
||||
|
||||
@@ -336,6 +386,13 @@ export async function listAgentsSnapshot(): Promise<AgentsSnapshot> {
|
||||
return buildSnapshotFromConfig(config);
|
||||
}
|
||||
|
||||
export async function listConfiguredAgentIds(): Promise<string[]> {
|
||||
const config = await readOpenClawConfig() as AgentConfigDocument;
|
||||
const { entries } = normalizeAgentsConfig(config);
|
||||
const ids = [...new Set(entries.map((entry) => entry.id.trim()).filter(Boolean))];
|
||||
return ids.length > 0 ? ids : [MAIN_AGENT_ID];
|
||||
}
|
||||
|
||||
export async function createAgent(name: string): Promise<AgentsSnapshot> {
|
||||
const config = await readOpenClawConfig() as AgentConfigDocument;
|
||||
const { agentsConfig, entries, syntheticMain } = normalizeAgentsConfig(config);
|
||||
@@ -350,7 +407,7 @@ export async function createAgent(name: string): Promise<AgentsSnapshot> {
|
||||
suffix += 1;
|
||||
}
|
||||
|
||||
const nextEntries = syntheticMain ? [createImplicitMainEntry(config), ...entries.filter((entry, index) => index > 0)] : [...entries];
|
||||
const nextEntries = syntheticMain ? [createImplicitMainEntry(config), ...entries.filter((_, index) => index > 0)] : [...entries];
|
||||
const newAgent: AgentListEntry = {
|
||||
id: nextId,
|
||||
name: normalizedName,
|
||||
@@ -405,8 +462,9 @@ export async function deleteAgentConfig(agentId: string): Promise<AgentsSnapshot
|
||||
|
||||
const config = await readOpenClawConfig() as AgentConfigDocument;
|
||||
const { agentsConfig, entries, defaultAgentId } = normalizeAgentsConfig(config);
|
||||
const removedEntry = entries.find((entry) => entry.id === agentId);
|
||||
const nextEntries = entries.filter((entry) => entry.id !== agentId);
|
||||
if (nextEntries.length === entries.length) {
|
||||
if (!removedEntry || nextEntries.length === entries.length) {
|
||||
throw new Error(`Agent "${agentId}" not found`);
|
||||
}
|
||||
|
||||
@@ -426,6 +484,8 @@ export async function deleteAgentConfig(agentId: string): Promise<AgentsSnapshot
|
||||
}
|
||||
|
||||
await writeOpenClawConfig(config);
|
||||
await removeAgentRuntimeDirectory(agentId);
|
||||
await removeAgentWorkspaceDirectory(removedEntry);
|
||||
logger.info('Deleted agent config entry', { agentId });
|
||||
return buildSnapshotFromConfig(config);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* OpenClaw Auth Profiles Utility
|
||||
* Writes API keys to ~/.openclaw/agents/main/agent/auth-profiles.json
|
||||
* Writes API keys to configured OpenClaw agent auth-profiles.json files
|
||||
* so the OpenClaw Gateway can load them for AI provider calls.
|
||||
*
|
||||
* All file I/O is asynchronous (fs/promises) to avoid blocking the
|
||||
@@ -8,10 +8,11 @@
|
||||
* equivalents could stall for 500 ms – 2 s+ per call, causing "Not
|
||||
* Responding" hangs.
|
||||
*/
|
||||
import { access, mkdir, readFile, writeFile, readdir } from 'fs/promises';
|
||||
import { constants, Dirent } from 'fs';
|
||||
import { access, mkdir, readFile, writeFile } from 'fs/promises';
|
||||
import { constants } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { listConfiguredAgentIds } from './agent-config';
|
||||
import {
|
||||
getProviderEnvVar,
|
||||
getProviderDefaultModel,
|
||||
@@ -120,14 +121,7 @@ async function discoverAgentIds(): Promise<string[]> {
|
||||
const agentsDir = join(homedir(), '.openclaw', 'agents');
|
||||
try {
|
||||
if (!(await fileExists(agentsDir))) return ['main'];
|
||||
const entries: Dirent[] = await readdir(agentsDir, { withFileTypes: true });
|
||||
const ids: string[] = [];
|
||||
for (const d of entries) {
|
||||
if (d.isDirectory() && await fileExists(join(agentsDir, d.name, 'agent'))) {
|
||||
ids.push(d.name);
|
||||
}
|
||||
}
|
||||
return ids.length > 0 ? ids : ['main'];
|
||||
return await listConfiguredAgentIds();
|
||||
} catch {
|
||||
return ['main'];
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { readdir, readFile, stat } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { getOpenClawConfigDir } from './paths';
|
||||
import { logger } from './logger';
|
||||
import { listConfiguredAgentIds } from './agent-config';
|
||||
import { parseUsageEntriesFromJsonl, type TokenUsageHistoryEntry } from './token-usage-core';
|
||||
|
||||
export { parseUsageEntriesFromJsonl, type TokenUsageHistoryEntry } from './token-usage-core';
|
||||
@@ -11,7 +12,7 @@ async function listRecentSessionFiles(): Promise<Array<{ filePath: string; sessi
|
||||
const agentsDir = join(openclawDir, 'agents');
|
||||
|
||||
try {
|
||||
const agentEntries = await readdir(agentsDir);
|
||||
const agentEntries = await listConfiguredAgentIds();
|
||||
const files: Array<{ filePath: string; sessionId: string; agentId: string; mtimeMs: number }> = [];
|
||||
|
||||
for (const agentId of agentEntries) {
|
||||
|
||||
Reference in New Issue
Block a user