| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- import type { OutputStyle, OutputStyleConfig } from "@zcode/shared";
- import type { IOutputStyleService } from "./outputStyle.js";
- import { access, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
- import { existsSync } from "node:fs";
- import { homedir } from "node:os";
- import { join } from "node:path";
- const DEFAULT_OUTPUT_STYLE = "default";
- const SETTINGS_FILE = "settings.json";
- const BUILT_IN_STYLES: OutputStyle[] = [
- {
- id: "default",
- name: "Default",
- description:
- "Completes coding tasks efficiently and provides concise responses",
- content: "",
- isBuiltIn: true,
- enabled: true,
- },
- {
- id: "explanatory",
- name: "Explanatory",
- description:
- "Explains implementation choices and codebase patterns",
- content: "",
- isBuiltIn: true,
- enabled: false,
- },
- {
- id: "learning",
- name: "Learning",
- description:
- "Pauses and asks you to write small pieces of code for hands-on practice",
- content: "",
- isBuiltIn: true,
- enabled: false,
- },
- ];
- function resolveUserHomeDir(): string {
- const envHome = process.env.HOME?.trim() || process.env.USERPROFILE?.trim();
- return envHome && envHome.length > 0 ? envHome : homedir();
- }
- function getUserOutputStylesDir(): string {
- return join(resolveUserHomeDir(), ".claude", "output-styles");
- }
- function getUserClaudeSettingsPath(): string {
- return join(resolveUserHomeDir(), ".claude", SETTINGS_FILE);
- }
- interface ClaudeSettingsConfig {
- outputStyle?: string;
- hooks?: unknown;
- [key: string]: unknown;
- }
- /**
- * Read JSON file safely
- */
- async function readJsonFile<T>(filePath: string): Promise<T | null> {
- try {
- if (!existsSync(filePath)) {
- return null;
- }
- const content = await readFile(filePath, "utf-8");
- return JSON.parse(content) as T;
- } catch (error) {
- console.error(`Failed to read JSON file ${filePath}:`, error);
- return null;
- }
- }
- /**
- * Write JSON file safely
- */
- async function writeJsonFile<T>(filePath: string, data: T): Promise<void> {
- try {
- await writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
- } catch (error) {
- console.error(`Failed to write JSON file ${filePath}:`, error);
- throw error;
- }
- }
- /**
- * 解析 markdown 格式的 output style 文件
- */
- function parseMarkdownStyle(
- content: string,
- filename?: string,
- ): { name: string; description: string } {
- const nameMatch = content.match(/^name:\s*(.+)$/m);
- const descMatch = content.match(/^description:\s*(.+)$/m);
- let name = nameMatch?.[1]?.trim() ?? "";
- if (!name && filename) {
- name = filename.replace(/\.md$/, "");
- }
- if (!name) {
- name = "Custom Style";
- }
- const description = descMatch?.[1]?.trim() ?? "";
- return { name, description };
- }
- /**
- * 创建 OutputStyle 服务实例
- */
- export function createOutputStyleService(): IOutputStyleService {
- async function getActiveStyle(): Promise<{ styleId: string | null }> {
- const settingsPath = getUserClaudeSettingsPath();
- const settings = await readJsonFile<ClaudeSettingsConfig>(settingsPath);
- return { styleId: settings?.outputStyle ?? null };
- }
- async function setActiveStyle(params: { styleId: string }): Promise<void> {
- const { styleId } = params;
- const settingsPath = getUserClaudeSettingsPath();
- const claudeDir = join(resolveUserHomeDir(), ".claude");
- // Ensure directory exists
- if (!existsSync(claudeDir)) {
- await mkdir(claudeDir, { recursive: true });
- }
- // Read existing settings.json
- const existingSettings = (await readJsonFile<ClaudeSettingsConfig>(settingsPath)) || {};
- // Update outputStyle
- const newSettings: ClaudeSettingsConfig = {
- ...existingSettings,
- outputStyle: styleId,
- };
- await writeJsonFile(settingsPath, newSettings);
- }
- async function listStyles(): Promise<{ styles: OutputStyle[] }> {
- const stylesDir = getUserOutputStylesDir();
- const customStyles: OutputStyle[] = [];
- // Get active style from settings.local.json, default to "default"
- const { styleId } = await getActiveStyle();
- const activeStyleId = styleId ?? "default";
- try {
- await access(stylesDir);
- const entries = await readdir(stylesDir, { withFileTypes: true });
- for (const entry of entries) {
- if (entry.isFile() && entry.name.endsWith(".md")) {
- try {
- const filePath = join(stylesDir, entry.name);
- const content = await readFile(filePath, "utf-8");
- const { name, description } = parseMarkdownStyle(content, entry.name);
- const styleId = `custom-${entry.name.replace(".md", "")}`;
- customStyles.push({
- id: styleId,
- name,
- description,
- content,
- isBuiltIn: false,
- enabled: styleId === activeStyleId,
- filePath,
- });
- } catch (error) {
- console.error(`Failed to read output style file ${entry.name}:`, error);
- }
- }
- }
- } catch {
- // 目录不存在或无法访问,返回内置 styles
- }
- // 按名称排序
- customStyles.sort((a, b) => a.name.localeCompare(b.name));
- // 合并内置和自定义 styles,标记激活状态
- const allStyles: OutputStyle[] = [
- ...BUILT_IN_STYLES.map((style) => ({
- ...style,
- enabled: style.id === activeStyleId,
- })),
- ...customStyles,
- ];
- return { styles: allStyles };
- }
- async function addStyle(params: { config: OutputStyleConfig }): Promise<void> {
- const { config } = params;
- const stylesDir = getUserOutputStylesDir();
- try {
- await mkdir(stylesDir, { recursive: true });
- } catch (error) {
- const err = error as NodeJS.ErrnoException;
- if (err.code !== "EEXIST") {
- throw error;
- }
- }
- const filename = `${config.name.toLowerCase().replace(/\s+/g, "-")}.md`;
- const filePath = join(stylesDir, filename);
- // 生成文件内容
- const content = `name: ${config.name}
- description: ${config.description}
- ${config.content}`;
- await writeFile(filePath, content, "utf-8");
- }
- async function updateStyle(params: {
- id: string;
- config: OutputStyleConfig;
- }): Promise<void> {
- const { id, config } = params;
- // 移除 'custom-' 前缀获取文件名
- const baseId = id.startsWith("custom-") ? id.slice(7) : id;
- const stylesDir = getUserOutputStylesDir();
- const filename = `${baseId}.md`;
- const filePath = join(stylesDir, filename);
- // 生成文件内容
- const content = `name: ${config.name}
- description: ${config.description}
- ${config.content}`;
- await writeFile(filePath, content, "utf-8");
- }
- async function deleteStyle(params: { id: string }): Promise<void> {
- const { id } = params;
- // 移除 'custom-' 前缀获取文件名
- const baseId = id.startsWith("custom-") ? id.slice(7) : id;
- const stylesDir = getUserOutputStylesDir();
- const filename = `${baseId}.md`;
- const filePath = join(stylesDir, filename);
- await rm(filePath, { force: true });
- }
- async function getUserStylesDirectory(): Promise<{ path: string }> {
- const stylesDir = getUserOutputStylesDir();
- try {
- await mkdir(stylesDir, { recursive: true });
- } catch {
- // 目录已存在
- }
- return { path: stylesDir };
- }
- return {
- listStyles,
- addStyle,
- updateStyle,
- deleteStyle,
- getUserStylesDirectory,
- setActiveStyle,
- getActiveStyle,
- };
- }
|