feat: 添加工具调用支持,增强 CLI 工具和 MCP 管理功能

This commit is contained in:
catlog22
2026-01-08 23:32:27 +08:00
parent 311ce2e4bc
commit 84168825d6
11 changed files with 297 additions and 76 deletions

View File

@@ -216,6 +216,55 @@ function ensureToolTags(tool: Partial<ClaudeCliTool>): ClaudeCliTool {
};
}
/**
* Ensure CLI tools configuration file exists
* Creates default config if missing (auto-rebuild feature)
* @param projectDir - Project directory path
* @param createInProject - If true, create in project dir; if false, create in global dir
* @returns The config that was created/exists
*/
export function ensureClaudeCliTools(projectDir: string, createInProject: boolean = true): ClaudeCliToolsConfig & { _source?: string } {
const resolved = resolveConfigPath(projectDir);
if (resolved.source !== 'default') {
// Config exists, load and return it
return loadClaudeCliTools(projectDir);
}
// Config doesn't exist - create default
console.log('[claude-cli-tools] Config not found, creating default cli-tools.json');
const defaultConfig: ClaudeCliToolsConfig = { ...DEFAULT_TOOLS_CONFIG };
if (createInProject) {
// Create in project directory
ensureClaudeDir(projectDir);
const projectPath = getProjectConfigPath(projectDir);
try {
fs.writeFileSync(projectPath, JSON.stringify(defaultConfig, null, 2), 'utf-8');
console.log(`[claude-cli-tools] Created default config at: ${projectPath}`);
return { ...defaultConfig, _source: 'project' };
} catch (err) {
console.error('[claude-cli-tools] Failed to create project config:', err);
}
}
// Fallback: create in global directory
const globalDir = path.join(os.homedir(), '.claude');
if (!fs.existsSync(globalDir)) {
fs.mkdirSync(globalDir, { recursive: true });
}
const globalPath = getGlobalConfigPath();
try {
fs.writeFileSync(globalPath, JSON.stringify(defaultConfig, null, 2), 'utf-8');
console.log(`[claude-cli-tools] Created default config at: ${globalPath}`);
return { ...defaultConfig, _source: 'global' };
} catch (err) {
console.error('[claude-cli-tools] Failed to create global config:', err);
return { ...defaultConfig, _source: 'default' };
}
}
/**
* Load CLI tools configuration with fallback:
* 1. Project: {projectDir}/.claude/cli-tools.json

View File

@@ -19,7 +19,8 @@ export type CliOutputUnitType =
| 'file_diff' // File modification diff
| 'progress' // Progress updates
| 'metadata' // Session/execution metadata
| 'system'; // System events/messages
| 'system' // System events/messages
| 'tool_call'; // Tool invocation/result (Gemini tool_use/tool_result)
/**
* Intermediate Representation unit
@@ -295,6 +296,38 @@ export class JsonLinesParser implements IOutputParser {
};
}
// Gemini tool_use: {"type":"tool_use","timestamp":"...","tool_name":"...","tool_id":"...","parameters":{...}}
if (json.type === 'tool_use' && json.tool_name) {
return {
type: 'tool_call',
content: {
tool: 'gemini',
action: 'invoke',
toolName: json.tool_name,
toolId: json.tool_id,
parameters: json.parameters,
raw: json
},
timestamp
};
}
// Gemini tool_result: {"type":"tool_result","timestamp":"...","tool_id":"...","status":"...","output":"..."}
if (json.type === 'tool_result' && json.tool_id) {
return {
type: 'tool_call',
content: {
tool: 'gemini',
action: 'result',
toolId: json.tool_id,
status: json.status,
output: json.output,
raw: json
},
timestamp
};
}
// ========== Codex CLI --json format ==========
// {"type":"thread.started","thread_id":"..."}
// {"type":"turn.started"}
@@ -733,6 +766,20 @@ export function flattenOutputUnits(
}
break;
case 'tool_call':
// Format tool call/result
if (unit.content.action === 'invoke') {
const params = unit.content.parameters ? JSON.stringify(unit.content.parameters) : '';
text += `[Tool] ${unit.content.toolName}(${params})`;
} else if (unit.content.action === 'result') {
const status = unit.content.status || 'unknown';
const output = unit.content.output ? `: ${unit.content.output.substring(0, 200)}${unit.content.output.length > 200 ? '...' : ''}` : '';
text += `[Tool Result] ${status}${output}`;
} else {
text += JSON.stringify(unit.content);
}
break;
case 'metadata':
case 'system':
// Metadata and system events are typically excluded from prompt context