mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-04-29 08:12:41 +08:00
- 新增 preprocessor 模块:数据清洗(XML卡片)、黑名单过滤、智能去噪、连续发言合并、数据脱敏
- 内置多国脱敏规则(中国手机号/身份证、美国SSN、日韩号码等)+ 自定义规则支持
- 工具层统一 wrapWithPreprocessing 包装,自动对 rawMessages 执行预处理+格式化
- 昵称匿名化:用 U{id} 替代真实昵称,跨工具调用一致
- SQL 查询补充 senderId/senderPlatformId
- PreprocessConfig 类型定义(preload + 主进程)
50 lines
1.8 KiB
TypeScript
50 lines
1.8 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 { t } from '../utils/format'
|
|
|
|
const schema = Type.Object({
|
|
message_ids: Type.Array(Type.Number(), { description: 'ai.tools.get_message_context.params.message_ids' }),
|
|
context_size: Type.Optional(Type.Number({ description: 'ai.tools.get_message_context.params.context_size' })),
|
|
})
|
|
|
|
/** 根据消息 ID 获取前后的上下文消息。适用于需要查看某条消息前后聊天内容的场景,比如"这条消息的前后在聊什么"、"查看某条消息的上下文"等。支持单个或批量消息 ID。 */
|
|
export function createTool(context: ToolContext): AgentTool<typeof schema> {
|
|
return {
|
|
name: 'get_message_context',
|
|
label: 'get_message_context',
|
|
description: 'ai.tools.get_message_context.desc',
|
|
parameters: schema,
|
|
execute: async (_toolCallId, params) => {
|
|
const { sessionId, locale } = context
|
|
const contextSize = params.context_size || 20
|
|
|
|
const messages = await workerManager.getMessageContext(sessionId, params.message_ids, contextSize)
|
|
|
|
if (messages.length === 0) {
|
|
const data = {
|
|
error: t('noMessageContext', locale) as string,
|
|
messageIds: params.message_ids,
|
|
}
|
|
return {
|
|
content: [{ type: 'text', text: JSON.stringify(data) }],
|
|
details: data,
|
|
}
|
|
}
|
|
|
|
const data = {
|
|
totalMessages: messages.length,
|
|
contextSize: contextSize,
|
|
requestedMessageIds: params.message_ids,
|
|
rawMessages: messages,
|
|
}
|
|
|
|
return {
|
|
content: [{ type: 'text', text: JSON.stringify(data) }],
|
|
details: data,
|
|
}
|
|
},
|
|
}
|
|
}
|