refactor: split tool registry into MCP and Agent, align agent tools with Electron

- Rename TOOL_REGISTRY to MCP_TOOL_REGISTRY (MCP Server only)
- Create AGENT_TOOL_REGISTRY with full Electron-aligned tool set (27 tools)
- MCP-only tools (list_sessions, get_session_info, search_keyword) excluded
  from agent registry
- Update ai.ts to use AGENT_TOOL_REGISTRY, mcp.ts to use MCP_TOOL_REGISTRY
- Add truncation strategies for newly registered agent tools

Note: Most newly registered agent tools require CoreDataProvider method
implementations (currently NotImplementedError). See follow-up task doc.
This commit is contained in:
digua committed 2026-05-15 22:03:40 +08:00
1 parent c789df5495
commit 74ea5563cb
5 files changed
+65 -14

No files matched your search

+5
View File
@@ -21,7 +21,12 @@ const DEFAULT_MAX_TOOL_RESULT_TOKENS = 8000
const TOOL_TRUNCATION_STRATEGY: Record<string, TruncationStrategy> = {
search_keyword: 'keep_first',
search_messages: 'keep_first',
deep_search_messages: 'keep_first',
get_recent_messages: 'keep_last',
get_message_context: 'keep_last',
get_session_messages: 'keep_last',
get_conversation_between: 'keep_last',
}
export interface ServerToolContext {
+2 -2
View File
@@ -16,7 +16,7 @@ import type { DatabaseManager, AIConversationManager } from '@openchatlab/node-r
import { parseAssistantFile, parseSkillFile, SkillManager, createActivateSkillTool } from '@openchatlab/node-runtime'
import { BUILTIN_TOOL_CATALOG, BUILTIN_PROVIDERS, BUILTIN_MODELS, getBuiltinModelsByProvider } from '@openchatlab/core'
import type { AssistantSummary, SkillSummary } from '@openchatlab/node-runtime'
import { TOOL_REGISTRY } from '@openchatlab/tools'
import { AGENT_TOOL_REGISTRY } from '@openchatlab/tools'
import { adaptToolsForAgent } from '../../ai/tool-adapter'
import { loadAssistantConfig } from '../../ai/assistant-loader'
import { runServerAgent, type AgentStreamEvent } from '../../ai/agent'
@@ -518,7 +518,7 @@ export function registerAiRoutes(
const db = (dbManager as any).open?.(sessionId)
const agentTools = db
? adaptToolsForAgent(TOOL_REGISTRY, () => ({
? adaptToolsForAgent(AGENT_TOOL_REGISTRY, () => ({
db,
sessionId,
locale,
+2 -2
View File
@@ -12,7 +12,7 @@ import { z } from 'zod'
import { loadConfig } from '@openchatlab/config'
import { NodePathProvider, DatabaseManager } from '@openchatlab/node-runtime'
import { getSessionMeta, getSessionOverview, getDatabaseSchema } from '@openchatlab/core'
import { TOOL_REGISTRY, CoreDataProvider } from '@openchatlab/tools'
import { MCP_TOOL_REGISTRY, CoreDataProvider } from '@openchatlab/tools'
import type { SessionListContext } from '@openchatlab/tools/src/definitions/sessions'
const MCP_TOOL_PREFIX = 'chatlab_'
@@ -78,7 +78,7 @@ export async function startMcpServer(): Promise<void> {
// --- 注册 Tools ---
for (const tool of TOOL_REGISTRY) {
for (const tool of MCP_TOOL_REGISTRY) {
const mcpName = `${MCP_TOOL_PREFIX}${tool.name}`
if (tool.name === 'list_sessions') {
+2 -2
View File
@@ -5,8 +5,8 @@
* 提供平台无关的工具定义和 handler,服务于 MCP Server、HTTP API 和 Electron Agent。
*/
// === Registry (Server/MCP 使用) ===
export { TOOL_REGISTRY, getToolByName } from './registry'
// === Registry ===
export { MCP_TOOL_REGISTRY, AGENT_TOOL_REGISTRY, getToolByName } from './registry'
// === Providers ===
export { CoreDataProvider } from './providers/core-data-provider'
+54 -8
View File
@@ -1,8 +1,9 @@
/**
* 工具注册表
*
* 所有平台无关工具的统一注册表。
* MCP Server 和 HTTP API 从此注册表加载工具。
* MCP_TOOL_REGISTRY: MCP Server 专用,包含会话发现等 MCP 特有工具。
* AGENT_TOOL_REGISTRY: Agent 共用(Server Agent / Electron Agent),
* 与 Electron TOOL_REGISTRY 保持一致的完整工具集。
*/
import type { ToolDefinition } from './types'
@@ -14,14 +15,27 @@ import { recentMessagesTool } from './definitions/recent-messages'
import { sqlQueryTool, schemaTool } from './definitions/sql-query'
import { sessionInfoTool } from './definitions/session-info'
import { sessionsListTool } from './definitions/sessions'
import { chatOverviewTool } from './definitions/chat-overview'
import { searchMessagesTool } from './definitions/search-messages'
import { deepSearchMessagesTool } from './definitions/deep-search-messages'
import { getMessageContextTool } from './definitions/get-message-context'
import { searchSessionsTool } from './definitions/search-sessions'
import { getSessionMessagesTool } from './definitions/get-session-messages'
import { getMembersTool } from './definitions/get-members'
import { getMemberNameHistoryTool } from './definitions/get-member-name-history'
import { getConversationBetweenTool } from './definitions/get-conversation-between'
import { getSessionSummariesTool } from './definitions/get-session-summaries'
import { responseTimeAnalysisTool } from './definitions/response-time-analysis'
import { keywordFrequencyTool } from './definitions/keyword-frequency'
import { SQL_TOOL_DEFS, createAllSqlToolDefinitions } from './sql'
/**
* 所有可用工具的注册表
* MCP Server 专用注册表
*
* sessions 和 session_info 为会话管理工具;
* 其余为数据查询/分析工具。
* 包含会话发现/管理工具(list_sessions、get_session_info)和简化搜索(search_keyword),
* 这些工具在 Agent 上下文中不适用(Agent 已有选定的会话和更完整的搜索工具)。
*/
export const TOOL_REGISTRY: ToolDefinition[] = [
export const MCP_TOOL_REGISTRY: ToolDefinition[] = [
sessionsListTool,
sessionInfoTool,
searchTool,
@@ -33,8 +47,40 @@ export const TOOL_REGISTRY: ToolDefinition[] = [
]
/**
* 按名称查找工具
* Agent 共用注册表
*
* 与 Electron TOOL_REGISTRY 完全对齐的工具集,在"已选定会话"上下文中使用。
* 不包含 MCP 特有的会话发现工具(list_sessions、get_session_info、search_keyword)。
* 额外包含 execute_sql 作为通用自定义 SQL 查询能力。
*/
export const AGENT_TOOL_REGISTRY: ToolDefinition[] = [
// Core
chatOverviewTool,
searchMessagesTool,
deepSearchMessagesTool,
recentMessagesTool,
getMessageContextTool,
searchSessionsTool,
getSessionMessagesTool,
getMembersTool,
// Analysis
memberStatsTool,
timeStatsTool,
getMemberNameHistoryTool,
getConversationBetweenTool,
getSessionSummariesTool,
responseTimeAnalysisTool,
keywordFrequencyTool,
// SQL
sqlQueryTool,
...createAllSqlToolDefinitions(SQL_TOOL_DEFS),
]
/**
* 按名称查找工具(在所有注册表中查找)
*/
export function getToolByName(name: string): ToolDefinition | undefined {
return TOOL_REGISTRY.find((t) => t.name === name)
return AGENT_TOOL_REGISTRY.find((t) => t.name === name) || MCP_TOOL_REGISTRY.find((t) => t.name === name)
}