Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit 7519603

Browse files
author
catlog22
committed
feat: 更新 CLI 工具配置管理,优化配置读取和保存策略
1 parent bafc322 commit 7519603

2 files changed

Lines changed: 42 additions & 32 deletions

File tree

.claude/CLAUDE.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# Claude Instructions
22

3-
4-
- **CLI Tools Usage**: @~/.claude/workflows/cli-tools-usage.md
53
- **Coding Philosophy**: @~/.claude/workflows/coding-philosophy.md
6-
- **Context Requirements**: @~/.claude/workflows/context-tools.md
7-
- **File Modification**: @~/.claude/workflows/file-modification.md
8-
- **CLI Endpoints Config**: @.claude/cli-tools.json
94

105
## CLI Endpoints
116

12-
**Strictly follow the @.claude/cli-tools.json configuration**
7+
- **CLI Tools Usage**: @~/.claude/workflows/cli-tools-usage.md
8+
- **CLI Endpoints Config**: @~/.claude/cli-tools.json
9+
10+
**Strictly follow the cli-tools.json configuration**
1311

1412
Available CLI endpoints are dynamically defined by the config file:
1513
- Built-in tools and their enable/disable status
1614
- Custom API endpoints registered via the Dashboard
1715
- Managed through the CCW Dashboard Status page
1816

17+
18+
1919
## Tool Execution
2020

21+
- **Context Requirements**: @~/.claude/workflows/context-tools.md
22+
- **File Modification**: @~/.claude/workflows/file-modification.md
23+
2124
### Agent Calls
2225
- **Always use `run_in_background: false`** for Task tool agent calls: `Task({ subagent_type: "xxx", prompt: "...", run_in_background: false })` to ensure synchronous execution and immediate result visibility
2326
- **TaskOutput usage**: Only use `TaskOutput({ task_id: "xxx", block: false })` + sleep loop to poll completion status. NEVER read intermediate output during agent/CLI execution - wait for final result only

ccw/src/tools/claude-cli-tools.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
/**
22
* Claude CLI Tools Configuration Manager
33
* Manages .claude/cli-tools.json (tools) and .claude/cli-settings.json (settings)
4-
* with fallback:
5-
* 1. Project workspace: {projectDir}/.claude/ (priority)
4+
*
5+
* Configuration Strategy:
6+
* - READ: Project → Global → Default (fallback chain)
7+
* - CREATE: Always in ~/.claude/ (global user-level config)
8+
* - SAVE: Based on source (project config saves to project, others to global)
9+
*
10+
* Read priority:
11+
* 1. Project workspace: {projectDir}/.claude/ (if exists)
612
* 2. Global: ~/.claude/ (fallback)
713
*/
814
import * as fs from 'fs';
@@ -295,38 +301,25 @@ function migrateConfig(config: any, projectDir: string): ClaudeCliToolsConfig {
295301

296302
/**
297303
* Ensure CLI tools configuration file exists
298-
* Creates default config if missing (auto-rebuild feature)
299-
* @param projectDir - Project directory path
300-
* @param createInProject - If true, create in project dir; if false, create in global dir
304+
* Creates default config in global ~/.claude directory (user-level config)
305+
* @param projectDir - Project directory path (used for reading existing project config)
306+
* @param createInProject - DEPRECATED: Always creates in global dir. Kept for backward compatibility.
301307
* @returns The config that was created/exists
302308
*/
303-
export function ensureClaudeCliTools(projectDir: string, createInProject: boolean = true): ClaudeCliToolsConfig & { _source?: string } {
309+
export function ensureClaudeCliTools(projectDir: string, createInProject: boolean = false): ClaudeCliToolsConfig & { _source?: string } {
304310
const resolved = resolveConfigPath(projectDir);
305311

306312
if (resolved.source !== 'default') {
307313
// Config exists, load and return it
308314
return loadClaudeCliTools(projectDir);
309315
}
310316

311-
// Config doesn't exist - create default
312-
console.log('[claude-cli-tools] Config not found, creating default cli-tools.json');
317+
// Config doesn't exist - create in global directory only
318+
console.log('[claude-cli-tools] Config not found, creating default cli-tools.json in ~/.claude');
313319

314320
const defaultConfig: ClaudeCliToolsConfig = { ...DEFAULT_TOOLS_CONFIG };
315321

316-
if (createInProject) {
317-
// Create in project directory
318-
ensureClaudeDir(projectDir);
319-
const projectPath = getProjectConfigPath(projectDir);
320-
try {
321-
fs.writeFileSync(projectPath, JSON.stringify(defaultConfig, null, 2), 'utf-8');
322-
console.log(`[claude-cli-tools] Created default config at: ${projectPath}`);
323-
return { ...defaultConfig, _source: 'project' };
324-
} catch (err) {
325-
console.error('[claude-cli-tools] Failed to create project config:', err);
326-
}
327-
}
328-
329-
// Fallback: create in global directory
322+
// Always create in global directory (user-level config)
330323
const globalDir = path.join(os.homedir(), '.claude');
331324
if (!fs.existsSync(globalDir)) {
332325
fs.mkdirSync(globalDir, { recursive: true });
@@ -405,14 +398,28 @@ export function loadClaudeCliTools(projectDir: string): ClaudeCliToolsConfig & {
405398
}
406399

407400
/**
408-
* Save CLI tools configuration to project .claude/cli-tools.json
401+
* Save CLI tools configuration
402+
* - If config was loaded from project, saves to project
403+
* - Otherwise saves to global ~/.claude/cli-tools.json
409404
*/
410405
export function saveClaudeCliTools(projectDir: string, config: ClaudeCliToolsConfig & { _source?: string }): void {
411-
ensureClaudeDir(projectDir);
412-
const configPath = getProjectConfigPath(projectDir);
413-
414406
const { _source, ...configToSave } = config;
415407

408+
// Determine save location based on source
409+
let configPath: string;
410+
if (_source === 'project') {
411+
// Config was loaded from project, save back to project
412+
ensureClaudeDir(projectDir);
413+
configPath = getProjectConfigPath(projectDir);
414+
} else {
415+
// Default: save to global directory
416+
const globalDir = path.join(os.homedir(), '.claude');
417+
if (!fs.existsSync(globalDir)) {
418+
fs.mkdirSync(globalDir, { recursive: true });
419+
}
420+
configPath = getGlobalConfigPath();
421+
}
422+
416423
try {
417424
fs.writeFileSync(configPath, JSON.stringify(configToSave, null, 2), 'utf-8');
418425
console.log(`[claude-cli-tools] Saved tools config to: ${configPath}`);

0 commit comments

Comments
 (0)