feat(profile): add config.json support for profile configuration

Add ProfileConfig interface with tools, provider, model, and
thinkingLevel settings. ProfileManager now exposes getToolsConfig()
and getProfileConfig() methods.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jiang Bohan 2026-01-31 01:57:47 +08:00
parent 11e2564aac
commit 9467ac9fff
3 changed files with 65 additions and 4 deletions

View file

@ -4,7 +4,8 @@
* agent
*/
import type { AgentProfile, CreateProfileOptions, ProfileManagerOptions } from "./types.js";
import type { AgentProfile, CreateProfileOptions, ProfileConfig, ProfileManagerOptions } from "./types.js";
import type { ToolsConfig } from "../tools/policy.js";
import { DEFAULT_TEMPLATES } from "./templates.js";
import {
ensureProfileDir,
@ -14,7 +15,7 @@ import {
saveProfile,
} from "./storage.js";
export { type AgentProfile, type CreateProfileOptions, type ProfileManagerOptions } from "./types.js";
export { type AgentProfile, type CreateProfileOptions, type ProfileConfig, type ProfileManagerOptions } from "./types.js";
export { DEFAULT_TEMPLATES } from "./templates.js";
export { getProfileDir, profileExists } from "./storage.js";
@ -152,4 +153,16 @@ export class ProfileManager {
return parts.join("\n\n");
}
/** 获取 tools 配置 */
getToolsConfig(): ToolsConfig | undefined {
const profile = this.getProfile();
return profile?.config?.tools;
}
/** 获取完整的 profile config */
getProfileConfig(): ProfileConfig | undefined {
const profile = this.getProfile();
return profile?.config;
}
}

View file

@ -4,7 +4,7 @@
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { PROFILE_FILES, type AgentProfile } from "./types.js";
import { PROFILE_FILES, type AgentProfile, type ProfileConfig } from "./types.js";
import { DATA_DIR } from "../../shared/index.js";
const DEFAULT_BASE_DIR = join(DATA_DIR, "agent-profiles");
@ -60,6 +60,33 @@ export function writeProfileFile(
writeFileSync(filePath, content, "utf-8");
}
/** 读取 config.json */
export function readProfileConfig(
profileId: string,
options?: StorageOptions,
): ProfileConfig | undefined {
const content = readProfileFile(profileId, PROFILE_FILES.config, options);
if (!content) {
return undefined;
}
try {
return JSON.parse(content) as ProfileConfig;
} catch {
// Invalid JSON, return undefined
return undefined;
}
}
/** 写入 config.json */
export function writeProfileConfig(
profileId: string,
config: ProfileConfig,
options?: StorageOptions,
): void {
const content = JSON.stringify(config, null, 2);
writeProfileFile(profileId, PROFILE_FILES.config, content, options);
}
/** 加载完整的 AgentProfile */
export function loadProfile(profileId: string, options?: StorageOptions): AgentProfile {
return {
@ -69,12 +96,13 @@ export function loadProfile(profileId: string, options?: StorageOptions): AgentP
tools: readProfileFile(profileId, PROFILE_FILES.tools, options),
memory: readProfileFile(profileId, PROFILE_FILES.memory, options),
bootstrap: readProfileFile(profileId, PROFILE_FILES.bootstrap, options),
config: readProfileConfig(profileId, options),
};
}
/** 保存 AgentProfile只写入非空字段 */
export function saveProfile(profile: AgentProfile, options?: StorageOptions): void {
const { id, soul, identity, tools, memory, bootstrap } = profile;
const { id, soul, identity, tools, memory, bootstrap, config } = profile;
if (soul !== undefined) {
writeProfileFile(id, PROFILE_FILES.soul, soul, options);
@ -91,4 +119,7 @@ export function saveProfile(profile: AgentProfile, options?: StorageOptions): vo
if (bootstrap !== undefined) {
writeProfileFile(id, PROFILE_FILES.bootstrap, bootstrap, options);
}
if (config !== undefined) {
writeProfileConfig(id, config, options);
}
}

View file

@ -2,6 +2,8 @@
* Agent Profile Type Definitions
*/
import type { ToolsConfig } from "../tools/policy.js";
/** Profile filename constants */
export const PROFILE_FILES = {
soul: "soul.md",
@ -9,8 +11,21 @@ export const PROFILE_FILES = {
tools: "tools.md",
memory: "memory.md",
bootstrap: "bootstrap.md",
config: "config.json",
} as const;
/** Profile config.json structure */
export interface ProfileConfig {
/** Tools policy configuration */
tools?: ToolsConfig;
/** Default LLM provider */
provider?: string;
/** Default model */
model?: string;
/** Default thinking level */
thinkingLevel?: string;
}
/** Agent Profile configuration */
export interface AgentProfile {
/** Profile ID */
@ -25,6 +40,8 @@ export interface AgentProfile {
memory?: string | undefined;
/** Initial context - guidance information for each conversation */
bootstrap?: string | undefined;
/** Profile configuration (from config.json) */
config?: ProfileConfig | undefined;
}
/** Profile Manager options */