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') { if (url.pathname === '/api/agents' && req.method === 'POST') {
try { try {
const body = await parseJsonBody<{ name: string }>(req); const body = await parseJsonBody<{ name: string; inheritWorkspace?: boolean }>(req);
const snapshot = await createAgent(body.name); const snapshot = await createAgent(body.name, { inheritWorkspace: body.inheritWorkspace });
// Sync provider API keys to the new agent's auth-profiles.json so the // Sync provider API keys to the new agent's auth-profiles.json so the
// embedded runner can authenticate with LLM providers when messages // embedded runner can authenticate with LLM providers when messages
// arrive via channel bots (e.g. Feishu). Without this, the copied // 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 { entries } = normalizeAgentsConfig(config);
const mainEntry = entries.find((entry) => entry.id === MAIN_AGENT_ID) ?? createImplicitMainEntry(config); const mainEntry = entries.find((entry) => entry.id === MAIN_AGENT_ID) ?? createImplicitMainEntry(config);
const sourceWorkspace = expandPath(mainEntry.workspace || getDefaultWorkspacePath(config)); const sourceWorkspace = expandPath(mainEntry.workspace || getDefaultWorkspacePath(config));
@@ -399,7 +403,11 @@ async function provisionAgentFilesystem(config: AgentConfigDocument, agent: Agen
await ensureDir(targetAgentDir); await ensureDir(targetAgentDir);
await ensureDir(targetSessionsDir); 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); await copyBootstrapFiles(sourceWorkspace, targetWorkspace);
} }
if (targetAgentDir !== sourceAgentDir) { if (targetAgentDir !== sourceAgentDir) {
@@ -521,7 +529,10 @@ export async function listConfiguredAgentIds(): Promise<string[]> {
return ids.length > 0 ? ids : [MAIN_AGENT_ID]; 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 () => { return withConfigLock(async () => {
const config = await readOpenClawConfig() as AgentConfigDocument; const config = await readOpenClawConfig() as AgentConfigDocument;
const { agentsConfig, entries, syntheticMain } = normalizeAgentsConfig(config); const { agentsConfig, entries, syntheticMain } = normalizeAgentsConfig(config);
@@ -554,9 +565,9 @@ export async function createAgent(name: string): Promise<AgentsSnapshot> {
list: nextEntries, list: nextEntries,
}; };
await provisionAgentFilesystem(config, newAgent); await provisionAgentFilesystem(config, newAgent, { inheritWorkspace: options?.inheritWorkspace });
await writeOpenClawConfig(config); 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); return buildSnapshotFromConfig(config);
}); });
} }

View File

@@ -1,6 +1,6 @@
{ {
"title": "Agents", "title": "Agents",
"subtitle": "When adding a new Agent, ClawX will copy the main Agent's workspace files and runtime auth setup. The configuration can be modified through a dialog.", "subtitle": "Create a new Agent to route specific channels to a separate personality or workspace.",
"refresh": "Refresh", "refresh": "Refresh",
"addAgent": "Add Agent", "addAgent": "Add Agent",
"gatewayWarning": "Gateway service is not running. Agent/channel changes may take a moment to apply.", "gatewayWarning": "Gateway service is not running. Agent/channel changes may take a moment to apply.",
@@ -14,9 +14,11 @@
"creating": "Creating...", "creating": "Creating...",
"createDialog": { "createDialog": {
"title": "Add Agent", "title": "Add Agent",
"description": "Create a new agent by name. ClawX will copy the main agent's workspace bootstrap files and runtime auth setup.", "description": "Create a new agent by name. You can optionally inherit the main agent's workspace bootstrap files.",
"nameLabel": "Agent Name", "nameLabel": "Agent Name",
"namePlaceholder": "Coding Helper" "namePlaceholder": "Coding Helper",
"inheritWorkspaceLabel": "Inherit main agent workspace",
"inheritWorkspaceDescription": "Copy SOUL.md, AGENTS.md, etc. from the main agent"
}, },
"deleteDialog": { "deleteDialog": {
"title": "Delete Agent", "title": "Delete Agent",

View File

@@ -1,6 +1,6 @@
{ {
"title": "Agents", "title": "Agents",
"subtitle": "新しい Agent を追加すると、ClawX はメイン Agent のワークスペース初期ファイルと実行時認証設定をコピーします。これらの設定は対話形式で編集できます", "subtitle": "新しい Agent を作成し、特定のチャンネルを異なるパーソナリティやワークスペースにルーティングできます",
"refresh": "更新", "refresh": "更新",
"addAgent": "Agent を追加", "addAgent": "Agent を追加",
"gatewayWarning": "Gateway サービスが停止しています。Agent または Channel の変更が反映されるまで少し時間がかかる場合があります。", "gatewayWarning": "Gateway サービスが停止しています。Agent または Channel の変更が反映されるまで少し時間がかかる場合があります。",
@@ -14,9 +14,11 @@
"creating": "作成中...", "creating": "作成中...",
"createDialog": { "createDialog": {
"title": "Agent を追加", "title": "Agent を追加",
"description": "名前だけで新しい Agent を作成できます。ClawX はメイン Agent のワークスペース初期ファイルと認証設定をコピーします。", "description": "名前だけで新しい Agent を作成できます。メイン Agent のワークスペース初期ファイルを引き継ぐかどうかも選択できます。",
"nameLabel": "Agent 名", "nameLabel": "Agent 名",
"namePlaceholder": "Coding Helper" "namePlaceholder": "Coding Helper",
"inheritWorkspaceLabel": "メイン Agent のワークスペースを引き継ぐ",
"inheritWorkspaceDescription": "SOUL.md、AGENTS.md などの初期ファイルをコピーします"
}, },
"deleteDialog": { "deleteDialog": {
"title": "Agent を削除", "title": "Agent を削除",

View File

@@ -1,6 +1,6 @@
{ {
"title": "Agents", "title": "Agents",
"subtitle": "添加新的 AgentClawX 会复制主 Agent 的工作区引导文件和运行时认证配置, 配置可以通过以对话的形式进行修改", "subtitle": "创建新的 Agent,可以将特定频道路由到不同的人格配置或工作区。",
"refresh": "刷新", "refresh": "刷新",
"addAgent": "添加 Agent", "addAgent": "添加 Agent",
"gatewayWarning": "Gateway 服务未运行。Agent 或频道变更可能需要一点时间生效。", "gatewayWarning": "Gateway 服务未运行。Agent 或频道变更可能需要一点时间生效。",
@@ -14,9 +14,11 @@
"creating": "创建中...", "creating": "创建中...",
"createDialog": { "createDialog": {
"title": "添加 Agent", "title": "添加 Agent",
"description": "只需输入名称即可创建新 Agent。ClawX 会复制主 Agent 的工作区引导文件和运行时认证配置。", "description": "输入名称即可创建新 Agent,可选择是否继承主 Agent 的工作区引导文件。",
"nameLabel": "Agent 名称", "nameLabel": "Agent 名称",
"namePlaceholder": "Coding Helper" "namePlaceholder": "Coding Helper",
"inheritWorkspaceLabel": "继承主 Agent 工作区",
"inheritWorkspaceDescription": "从主 Agent 复制 SOUL.md、AGENTS.md 等引导文件"
}, },
"deleteDialog": { "deleteDialog": {
"title": "删除 Agent", "title": "删除 Agent",

View File

@@ -6,6 +6,7 @@ import { Label } from '@/components/ui/label';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import { ConfirmDialog } from '@/components/ui/confirm-dialog'; import { ConfirmDialog } from '@/components/ui/confirm-dialog';
import { Switch } from '@/components/ui/switch';
import { LoadingSpinner } from '@/components/common/LoadingSpinner'; import { LoadingSpinner } from '@/components/common/LoadingSpinner';
import { useAgentsStore } from '@/stores/agents'; import { useAgentsStore } from '@/stores/agents';
import { useGatewayStore } from '@/stores/gateway'; import { useGatewayStore } from '@/stores/gateway';
@@ -179,8 +180,8 @@ export function Agents() {
{showAddDialog && ( {showAddDialog && (
<AddAgentDialog <AddAgentDialog
onClose={() => setShowAddDialog(false)} onClose={() => setShowAddDialog(false)}
onCreate={async (name) => { onCreate={async (name, options) => {
await createAgent(name); await createAgent(name, options);
setShowAddDialog(false); setShowAddDialog(false);
toast.success(t('toast.agentCreated')); toast.success(t('toast.agentCreated'));
}} }}
@@ -345,17 +346,18 @@ function AddAgentDialog({
onCreate, onCreate,
}: { }: {
onClose: () => void; onClose: () => void;
onCreate: (name: string) => Promise<void>; onCreate: (name: string, options: { inheritWorkspace: boolean }) => Promise<void>;
}) { }) {
const { t } = useTranslation('agents'); const { t } = useTranslation('agents');
const [name, setName] = useState(''); const [name, setName] = useState('');
const [inheritWorkspace, setInheritWorkspace] = useState(false);
const [saving, setSaving] = useState(false); const [saving, setSaving] = useState(false);
const handleSubmit = async () => { const handleSubmit = async () => {
if (!name.trim()) return; if (!name.trim()) return;
setSaving(true); setSaving(true);
try { try {
await onCreate(name.trim()); await onCreate(name.trim(), { inheritWorkspace });
} catch (error) { } catch (error) {
toast.error(t('toast.agentCreateFailed', { error: String(error) })); toast.error(t('toast.agentCreateFailed', { error: String(error) }));
setSaving(false); setSaving(false);
@@ -386,6 +388,17 @@ function AddAgentDialog({
className={inputClasses} className={inputClasses}
/> />
</div> </div>
<div className="flex items-center justify-between">
<div className="space-y-0.5">
<Label htmlFor="inherit-workspace" className={labelClasses}>{t('createDialog.inheritWorkspaceLabel')}</Label>
<p className="text-[13px] text-foreground/60">{t('createDialog.inheritWorkspaceDescription')}</p>
</div>
<Switch
id="inherit-workspace"
checked={inheritWorkspace}
onCheckedChange={setInheritWorkspace}
/>
</div>
<div className="flex justify-end gap-2"> <div className="flex justify-end gap-2">
<Button <Button
variant="outline" variant="outline"

View File

@@ -12,7 +12,7 @@ interface AgentsState {
loading: boolean; loading: boolean;
error: string | null; error: string | null;
fetchAgents: () => Promise<void>; fetchAgents: () => Promise<void>;
createAgent: (name: string) => Promise<void>; createAgent: (name: string, options?: { inheritWorkspace?: boolean }) => Promise<void>;
updateAgent: (agentId: string, name: string) => Promise<void>; updateAgent: (agentId: string, name: string) => Promise<void>;
deleteAgent: (agentId: string) => Promise<void>; deleteAgent: (agentId: string) => Promise<void>;
assignChannel: (agentId: string, channelType: ChannelType) => Promise<void>; assignChannel: (agentId: string, channelType: ChannelType) => Promise<void>;
@@ -52,12 +52,12 @@ export const useAgentsStore = create<AgentsState>((set) => ({
} }
}, },
createAgent: async (name: string) => { createAgent: async (name: string, options?: { inheritWorkspace?: boolean }) => {
set({ error: null }); set({ error: null });
try { try {
const snapshot = await hostApiFetch<AgentsSnapshot & { success?: boolean }>('/api/agents', { const snapshot = await hostApiFetch<AgentsSnapshot & { success?: boolean }>('/api/agents', {
method: 'POST', method: 'POST',
body: JSON.stringify({ name }), body: JSON.stringify({ name, inheritWorkspace: options?.inheritWorkspace }),
}); });
set(applySnapshot(snapshot)); set(applySnapshot(snapshot));
} catch (error) { } catch (error) {