feat: 添加 knowledge_save/load 工具
- api.ts: 添加 knowledge_save/load 类型定义 - toolExecutor.ts: 实现知识图谱保存和加载功能
This commit is contained in:
@ -24,7 +24,9 @@ import type {
|
|||||||
FileListArgs,
|
FileListArgs,
|
||||||
SyntaxCheckArgs,
|
SyntaxCheckArgs,
|
||||||
SimulationArgs,
|
SimulationArgs,
|
||||||
WaveformSummaryArgs
|
WaveformSummaryArgs,
|
||||||
|
KnowledgeSaveArgs,
|
||||||
|
KnowledgeLoadArgs
|
||||||
} from '../types/api';
|
} from '../types/api';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,6 +79,12 @@ export async function executeToolCall(
|
|||||||
case 'waveform_summary':
|
case 'waveform_summary':
|
||||||
resultText = await executeWaveformSummary(args as unknown as WaveformSummaryArgs);
|
resultText = await executeWaveformSummary(args as unknown as WaveformSummaryArgs);
|
||||||
break;
|
break;
|
||||||
|
case 'knowledge_save':
|
||||||
|
resultText = await executeKnowledgeSave(args as unknown as KnowledgeSaveArgs);
|
||||||
|
break;
|
||||||
|
case 'knowledge_load':
|
||||||
|
resultText = await executeKnowledgeLoad();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error(`未知工具: ${toolName}`);
|
throw new Error(`未知工具: ${toolName}`);
|
||||||
}
|
}
|
||||||
@ -287,6 +295,53 @@ async function executeWaveformSummary(args: WaveformSummaryArgs): Promise<string
|
|||||||
return `波形分析功能暂未实现。\n请求参数:\n- VCD文件: ${args.vcdPath}\n- 信号: ${args.signals}\n- 检查点: ${args.checkpoints || '无'}`;
|
return `波形分析功能暂未实现。\n请求参数:\n- VCD文件: ${args.vcdPath}\n- 信号: ${args.signals}\n- 检查点: ${args.checkpoints || '无'}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 knowledge_save 工具
|
||||||
|
* 保存知识图谱到 .iccoder/knowledge.json
|
||||||
|
*/
|
||||||
|
async function executeKnowledgeSave(args: KnowledgeSaveArgs): Promise<string> {
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
|
throw new Error('请先打开一个工作区');
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspacePath = workspaceFolders[0].uri.fsPath;
|
||||||
|
const iccoderDir = path.join(workspacePath, '.iccoder');
|
||||||
|
const knowledgePath = path.join(iccoderDir, 'knowledge.json');
|
||||||
|
|
||||||
|
// 确保 .iccoder 目录存在
|
||||||
|
if (!fs.existsSync(iccoderDir)) {
|
||||||
|
fs.mkdirSync(iccoderDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入知识图谱
|
||||||
|
fs.writeFileSync(knowledgePath, args.data, 'utf-8');
|
||||||
|
|
||||||
|
return `知识图谱已保存: .iccoder/knowledge.json`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行 knowledge_load 工具
|
||||||
|
* 从 .iccoder/knowledge.json 加载知识图谱
|
||||||
|
*/
|
||||||
|
async function executeKnowledgeLoad(): Promise<string> {
|
||||||
|
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||||
|
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||||
|
throw new Error('请先打开一个工作区');
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspacePath = workspaceFolders[0].uri.fsPath;
|
||||||
|
const knowledgePath = path.join(workspacePath, '.iccoder', 'knowledge.json');
|
||||||
|
|
||||||
|
// 如果文件不存在,返回空图谱
|
||||||
|
if (!fs.existsSync(knowledgePath)) {
|
||||||
|
return JSON.stringify({ directed: true, nodes: [], links: [] });
|
||||||
|
}
|
||||||
|
|
||||||
|
const content = fs.readFileSync(knowledgePath, 'utf-8');
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 iverilog 路径
|
* 获取 iverilog 路径
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -196,7 +196,9 @@ export type ToolName =
|
|||||||
| 'file_list'
|
| 'file_list'
|
||||||
| 'syntax_check'
|
| 'syntax_check'
|
||||||
| 'simulation'
|
| 'simulation'
|
||||||
| 'waveform_summary';
|
| 'waveform_summary'
|
||||||
|
| 'knowledge_save'
|
||||||
|
| 'knowledge_load';
|
||||||
|
|
||||||
/** file_read 工具参数 */
|
/** file_read 工具参数 */
|
||||||
export interface FileReadArgs {
|
export interface FileReadArgs {
|
||||||
@ -240,6 +242,17 @@ export interface WaveformSummaryArgs {
|
|||||||
checkpoints?: string;
|
checkpoints?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** knowledge_save 工具参数 */
|
||||||
|
export interface KnowledgeSaveArgs {
|
||||||
|
/** 知识图谱 JSON 数据 */
|
||||||
|
data: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** knowledge_load 工具参数 */
|
||||||
|
export interface KnowledgeLoadArgs {
|
||||||
|
// 无参数,直接读取 .iccoder/knowledge.json
|
||||||
|
}
|
||||||
|
|
||||||
/** 工具参数联合类型 */
|
/** 工具参数联合类型 */
|
||||||
export type ToolArgs =
|
export type ToolArgs =
|
||||||
| FileReadArgs
|
| FileReadArgs
|
||||||
@ -248,4 +261,6 @@ export type ToolArgs =
|
|||||||
| FileListArgs
|
| FileListArgs
|
||||||
| SyntaxCheckArgs
|
| SyntaxCheckArgs
|
||||||
| SimulationArgs
|
| SimulationArgs
|
||||||
| WaveformSummaryArgs;
|
| WaveformSummaryArgs
|
||||||
|
| KnowledgeSaveArgs
|
||||||
|
| KnowledgeLoadArgs;
|
||||||
|
|||||||
Reference in New Issue
Block a user