Files
ChatLab/electron/main/ai/tools/definitions/get-session-messages.ts
digua f7c427df50 refactor(tools): modularize tool system with AgentTool + TypeBox + i18n
- Delete monolithic registry.ts (−1185 lines)
- Add tools/definitions/ with 12 individual tool files + index.ts,
  each using AgentTool interface and TypeBox schemas
- Add tools/utils/ with shared helpers (format.ts, schemas.ts, time-params.ts)
- Rewrite tools/index.ts to provide getAllTools() factory
- Clean up tools/types.ts, keep only ToolContext and OwnerInfo
- Use i18n keys for tool descriptions, preserve Chinese as comments
2026-02-26 21:05:39 +08:00

52 lines
2.1 KiB
TypeScript

import { Type } from '@mariozechner/pi-ai'
import type { AgentTool } from '@mariozechner/pi-agent-core'
import type { ToolContext } from '../types'
import * as workerManager from '../../../worker/workerManager'
import { isChineseLocale, formatMessageCompact } from '../utils/format'
const schema = Type.Object({
session_id: Type.Number({ description: 'ai.tools.get_session_messages.params.session_id' }),
limit: Type.Optional(Type.Number({ description: 'ai.tools.get_session_messages.params.limit' })),
})
/** 获取指定会话的完整消息列表。用于在 search_sessions 找到相关会话后,获取该会话的完整上下文。返回会话的所有消息及参与者信息。 */
export function createTool(context: ToolContext): AgentTool<typeof schema> {
return {
name: 'get_session_messages',
label: 'get_session_messages',
description: 'ai.tools.get_session_messages.desc',
parameters: schema,
execute: async (_toolCallId, params) => {
const { sessionId, maxMessagesLimit, locale } = context
const limit = maxMessagesLimit || params.limit || 1000
const result = await workerManager.getSessionMessages(sessionId, params.session_id, limit)
let data: Record<string, unknown>
if (!result) {
data = {
error: isChineseLocale(locale) ? '未找到指定的会话' : 'Session not found',
sessionId: params.session_id,
}
} else {
const localeStr = isChineseLocale(locale) ? 'zh-CN' : 'en-US'
const startTime = new Date(result.startTs * 1000).toLocaleString(localeStr)
const endTime = new Date(result.endTs * 1000).toLocaleString(localeStr)
data = {
sessionId: result.sessionId,
time: `${startTime} ~ ${endTime}`,
messageCount: result.messageCount,
returnedCount: result.returnedCount,
participants: result.participants,
messages: result.messages.map((m) => formatMessageCompact(m, locale)),
}
}
return {
content: [{ type: 'text', text: JSON.stringify(data) }],
details: data,
}
},
}
}