feat(agents): add option to inherit main agent workspace when creating new agent (#639)

This commit is contained in:
paisley
2026-03-23 18:00:35 +08:00
committed by GitHub
Unverified
parent 6b82c6ccb4
commit c6021cedf4
7 changed files with 53 additions and 23 deletions

View File

@@ -117,8 +117,8 @@ export async function handleAgentRoutes(
if (url.pathname === '/api/agents' && req.method === 'POST') {
try {
const body = await parseJsonBody<{ name: string }>(req);
const snapshot = await createAgent(body.name);
const body = await parseJsonBody<{ name: string; inheritWorkspace?: boolean }>(req);
const snapshot = await createAgent(body.name, { inheritWorkspace: body.inheritWorkspace });
// Sync provider API keys to the new agent's auth-profiles.json so the
// embedded runner can authenticate with LLM providers when messages
// arrive via channel bots (e.g. Feishu). Without this, the copied

View File

@@ -386,7 +386,11 @@ async function copyRuntimeFiles(sourceAgentDir: string, targetAgentDir: string):
}
}
async function provisionAgentFilesystem(config: AgentConfigDocument, agent: AgentListEntry): Promise<void> {
async function provisionAgentFilesystem(
config: AgentConfigDocument,
agent: AgentListEntry,
options?: { inheritWorkspace?: boolean },
): Promise<void> {
const { entries } = normalizeAgentsConfig(config);
const mainEntry = entries.find((entry) => entry.id === MAIN_AGENT_ID) ?? createImplicitMainEntry(config);
const sourceWorkspace = expandPath(mainEntry.workspace || getDefaultWorkspacePath(config));
@@ -399,7 +403,11 @@ async function provisionAgentFilesystem(config: AgentConfigDocument, agent: Agen
await ensureDir(targetAgentDir);
await ensureDir(targetSessionsDir);
if (targetWorkspace !== sourceWorkspace) {
// When inheritWorkspace is true, copy the main agent's workspace bootstrap
// files (SOUL.md, AGENTS.md, etc.) so the new agent inherits the same
// personality / instructions. When false (default), leave the workspace
// empty and let OpenClaw Gateway seed the default bootstrap files on startup.
if (options?.inheritWorkspace && targetWorkspace !== sourceWorkspace) {
await copyBootstrapFiles(sourceWorkspace, targetWorkspace);
}
if (targetAgentDir !== sourceAgentDir) {
@@ -521,7 +529,10 @@ export async function listConfiguredAgentIds(): Promise<string[]> {
return ids.length > 0 ? ids : [MAIN_AGENT_ID];
}
export async function createAgent(name: string): Promise<AgentsSnapshot> {
export async function createAgent(
name: string,
options?: { inheritWorkspace?: boolean },
): Promise<AgentsSnapshot> {
return withConfigLock(async () => {
const config = await readOpenClawConfig() as AgentConfigDocument;
const { agentsConfig, entries, syntheticMain } = normalizeAgentsConfig(config);
@@ -554,9 +565,9 @@ export async function createAgent(name: string): Promise<AgentsSnapshot> {
list: nextEntries,
};
await provisionAgentFilesystem(config, newAgent);
await provisionAgentFilesystem(config, newAgent, { inheritWorkspace: options?.inheritWorkspace });
await writeOpenClawConfig(config);
logger.info('Created agent config entry', { agentId: nextId });
logger.info('Created agent config entry', { agentId: nextId, inheritWorkspace: !!options?.inheritWorkspace });
return buildSnapshotFromConfig(config);
});
}