From 4df39448bec7e65dc81584da652d9f908d2dfe71 Mon Sep 17 00:00:00 2001 From: Jiang Bohan Date: Wed, 4 Feb 2026 14:42:23 +0800 Subject: [PATCH] feat(agent): add communication style setting to profile - Add AgentStyle type with 6 preset options (professional, friendly, etc.) - Add getStyle/setStyle methods to ProfileManager - Update soul.md template to include style section - Add reloadSystemPrompt support for style changes Co-Authored-By: Claude Opus 4.5 --- src/agent/async-agent.ts | 14 ++++++++++ src/agent/profile/index.ts | 56 ++++++++++++++++++++++++++++++++++++++ src/agent/profile/types.ts | 12 ++++++++ src/agent/runner.ts | 14 ++++++++++ 4 files changed, 96 insertions(+) diff --git a/src/agent/async-agent.ts b/src/agent/async-agent.ts index b95fdc50..9c928232 100644 --- a/src/agent/async-agent.ts +++ b/src/agent/async-agent.ts @@ -177,6 +177,20 @@ export class AsyncAgent { this.agent.setUserContent(content); } + /** + * Get agent communication style from profile config. + */ + getAgentStyle(): string | undefined { + return this.agent.getAgentStyle(); + } + + /** + * Update agent communication style in profile config. + */ + setAgentStyle(style: string): void { + this.agent.setAgentStyle(style); + } + /** * Reload profile from disk and rebuild system prompt. * Call this after updating profile files to apply changes immediately. diff --git a/src/agent/profile/index.ts b/src/agent/profile/index.ts index a8c6dd9d..b0ce0158 100644 --- a/src/agent/profile/index.ts +++ b/src/agent/profile/index.ts @@ -297,4 +297,60 @@ export class ProfileManager { this.profile.user = content; } } + + /** 获取 Agent 风格 */ + getStyle(): string | undefined { + const profile = this.getProfile(); + return profile?.config?.style; + } + + /** 更新 Agent 风格 */ + updateStyle(style: string): void { + const profile = this.getOrCreateProfile(false); + const currentConfig = profile.config ?? {}; + const newConfig: ProfileConfig = { + ...currentConfig, + style: style as ProfileConfig["style"], + }; + profile.config = newConfig; + this.profile = profile; + writeProfileConfig(this.profileId, newConfig, { baseDir: this.baseDir }); + + // Also update soul.md to include the style + this.updateSoulWithStyle(style); + } + + /** 更新 soul.md,确保包含 Agent 风格 */ + private updateSoulWithStyle(style: string): void { + const profile = this.getOrCreateProfile(true); + let soulContent = profile.soul ?? DEFAULT_TEMPLATES.soul; + + // 替换 soul.md 中的 Style 字段 + // 匹配 "- **Style:** xxx" 格式 + const stylePattern = /- \*\*Style:\*\* .*/; + const newStyleLine = `- **Style:** ${style}`; + + if (stylePattern.test(soulContent)) { + soulContent = soulContent.replace(stylePattern, newStyleLine); + } else { + // 如果没有找到 Style 字段,在 Identity 部分的 Role 后添加 + const rolePattern = /(- \*\*Role:\*\* .*)/; + if (rolePattern.test(soulContent)) { + soulContent = soulContent.replace(rolePattern, `$1\n${newStyleLine}`); + } else { + // 如果没有 Role,尝试在 Name 后添加 + const namePattern = /(- \*\*Name:\*\* .*)/; + if (namePattern.test(soulContent)) { + soulContent = soulContent.replace(namePattern, `$1\n${newStyleLine}`); + } + } + } + + // 保存更新后的 soul.md + writeProfileFile(this.profileId, PROFILE_FILES.soul, soulContent, { baseDir: this.baseDir }); + // 更新缓存 + if (this.profile) { + this.profile.soul = soulContent; + } + } } diff --git a/src/agent/profile/types.ts b/src/agent/profile/types.ts index db313ceb..f1de243d 100644 --- a/src/agent/profile/types.ts +++ b/src/agent/profile/types.ts @@ -13,10 +13,22 @@ export const PROFILE_FILES = { config: "config.json", } as const; +/** Available style options for agent personality */ +export const AGENT_STYLES = [ + "concise", // 简洁直接 + "warm", // 温暖友好 + "playful", // 轻松活泼 + "professional", // 专业正式 +] as const; + +export type AgentStyle = (typeof AGENT_STYLES)[number]; + /** Profile config.json structure */ export interface ProfileConfig { /** Agent display name */ name?: string; + /** Agent communication style */ + style?: AgentStyle; /** Tools policy configuration */ tools?: ToolsConfig; /** Default LLM provider */ diff --git a/src/agent/runner.ts b/src/agent/runner.ts index 0efda920..7acb3709 100644 --- a/src/agent/runner.ts +++ b/src/agent/runner.ts @@ -564,6 +564,20 @@ export class Agent { this.profile?.updateUserContent(content); } + /** + * Get agent communication style from profile config. + */ + getAgentStyle(): string | undefined { + return this.profile?.getStyle(); + } + + /** + * Update agent communication style in profile config. + */ + setAgentStyle(style: string): void { + this.profile?.updateStyle(style); + } + /** * Reload profile from disk and rebuild system prompt. * Call this after updating profile files to apply changes immediately.