Files
MyGoNavi/frontend/src/components/ai/aiLocalToolRuntime.ts
Syngnat f7ed6f8e61 ♻️ refactor(ai-tools): 拆分本地工具执行链路
- 抽离 AI 本地工具默认运行时封装

- 拆分数据库工具执行器并保留现有返回协议

- 收敛 aiLocalToolExecutor 为工具路由与结果包装
2026-06-09 00:16:57 +08:00

102 lines
4.1 KiB
TypeScript

import { DBGetAllColumns, DBGetDatabases, DBGetTables } from '../../../wailsjs/go/app/App';
import type { AISnapshotInspectionRuntime } from './aiSnapshotInspectionToolExecutor';
export interface AIToolContextEntry {
connectionId: string;
dbName: string;
tables: string[];
}
export interface AILocalToolRuntime extends AISnapshotInspectionRuntime {
getDatabases: (config: any) => Promise<any>;
getTables: (config: any, dbName: string) => Promise<any>;
getAllColumns: (config: any, dbName: string) => Promise<any>;
getColumns: (config: any, dbName: string, tableName: string) => Promise<any>;
getIndexes: (config: any, dbName: string, tableName: string) => Promise<any>;
getForeignKeys: (config: any, dbName: string, tableName: string) => Promise<any>;
getTriggers: (config: any, dbName: string, tableName: string) => Promise<any>;
showCreateTable: (config: any, dbName: string, tableName: string) => Promise<any>;
query: (config: any, dbName: string, sql: string) => Promise<any>;
checkSQL?: (sql: string) => Promise<{ allowed?: boolean; operationType?: string } | undefined>;
callMCPTool?: (name: string, args: string) => Promise<{ content?: string; isError?: boolean } | undefined>;
}
const getAIService = () => (window as any).go?.aiservice?.Service;
export const buildDefaultLocalToolRuntime = (): AILocalToolRuntime => ({
getDatabases: DBGetDatabases,
getTables: DBGetTables,
getAllColumns: DBGetAllColumns,
getColumns: async (config, dbName, tableName) => {
const mod = await import('../../../wailsjs/go/app/App');
return mod.DBGetColumns(config, dbName, tableName);
},
getIndexes: async (config, dbName, tableName) => {
const mod = await import('../../../wailsjs/go/app/App');
return mod.DBGetIndexes(config, dbName, tableName);
},
getForeignKeys: async (config, dbName, tableName) => {
const mod = await import('../../../wailsjs/go/app/App');
return mod.DBGetForeignKeys(config, dbName, tableName);
},
getTriggers: async (config, dbName, tableName) => {
const mod = await import('../../../wailsjs/go/app/App');
return mod.DBGetTriggers(config, dbName, tableName);
},
showCreateTable: async (config, dbName, tableName) => {
const mod = await import('../../../wailsjs/go/app/App');
return mod.DBShowCreateTable(config, dbName, tableName);
},
query: async (config, dbName, sql) => {
const mod = await import('../../../wailsjs/go/app/App');
return mod.DBQuery(config, dbName, sql);
},
checkSQL: async (sql) => {
const service = getAIService();
if (typeof service?.AICheckSQL !== 'function') {
return undefined;
}
return service.AICheckSQL(sql);
},
callMCPTool: async (name, args) => {
const service = getAIService();
if (typeof service?.AICallMCPTool !== 'function') {
return undefined;
}
return service.AICallMCPTool(name, args);
},
getAIRuntimeState: async () => {
const service = getAIService();
if (!service) {
return undefined;
}
const [providers, activeProviderId, safetyLevel, contextLevel] = await Promise.all([
typeof service.AIGetProviders === 'function' ? service.AIGetProviders() : Promise.resolve([]),
typeof service.AIGetActiveProvider === 'function' ? service.AIGetActiveProvider() : Promise.resolve(''),
typeof service.AIGetSafetyLevel === 'function' ? service.AIGetSafetyLevel() : Promise.resolve(''),
typeof service.AIGetContextLevel === 'function' ? service.AIGetContextLevel() : Promise.resolve(''),
]);
return {
providers: Array.isArray(providers) ? providers : [],
activeProviderId: String(activeProviderId || '').trim(),
safetyLevel: String(safetyLevel || '').trim(),
contextLevel: String(contextLevel || '').trim(),
};
},
getMCPServers: async () => {
const service = getAIService();
if (typeof service?.AIGetMCPServers !== 'function') {
return undefined;
}
return service.AIGetMCPServers();
},
getMCPClientInstallStatuses: async () => {
const service = getAIService();
if (typeof service?.AIGetMCPClientInstallStatuses !== 'function') {
return undefined;
}
return service.AIGetMCPClientInstallStatuses();
},
});