dsh-brain/lib/raw-memory/tools.js

122 lines
6.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function renderJson(_args, value) {
const text = typeof value === 'string' ? value : JSON.stringify(value, null, 2);
return [{ type: 'text', text }];
}
function searchTool(deps) {
return {
name: 'memory_raw_search',
description: '在原始对话记录中按关键词搜索原话(不是记忆条目,也不是摘要)。返回会话 id、事件 seq、说话角色、时间和原文片段;' +
'需要完整上下文时用 memory_raw_read 按 session_id + seq 读取。纯本地关键词匹配。',
parameters: {
type: 'object',
properties: {
query: { type: 'string', description: '检索关键词或短语(必填)。' },
k: { type: 'integer', description: '最多返回条数(1..50,默认取配置值)。' },
session_id: { type: 'string', description: '可选:只在这个会话内搜索。' },
role: { type: 'string', enum: ['user', 'assistant'], description: '可选:只看用户或助手原话。' },
},
required: ['query'],
additionalProperties: false,
},
output: {
schema: {
type: 'object',
properties: {
query: { type: 'string' },
total: { type: 'number' },
truncated: { type: 'boolean' },
sessions_scanned: { type: 'number' },
matches: { type: 'array', items: { type: 'object' } },
},
required: ['query', 'matches'],
additionalProperties: true,
},
render: renderJson,
},
async execute(args) {
const settings = deps.settings();
if (!settings.enabled)
throw new Error('原始记录层当前已禁用(dsh-brain-raw 行未启用)。');
const query = typeof args.query === 'string' ? args.query.trim() : '';
if (query === '')
throw new Error('query 不能为空。');
const requested = typeof args.k === 'number' && Number.isFinite(args.k) ? Math.trunc(args.k) : settings.maxSearchResults;
const k = Math.min(50, Math.max(1, requested));
const sessionId = typeof args.session_id === 'string' && args.session_id !== '' ? args.session_id : undefined;
const role = args.role === 'user' || args.role === 'assistant' ? args.role : undefined;
const result = await deps.store.search(query, { k, ...(sessionId === undefined ? {} : { sessionId }), ...(role === undefined ? {} : { role }) });
return {
query,
total: result.total,
truncated: result.truncated,
sessions_scanned: result.sessionsScanned,
matches: result.matches,
};
},
};
}
function readTool(deps) {
return {
name: 'memory_raw_read',
description: '读取某个会话的原始记录:给 seq 时返回那一条原话,否则按 offset/limit 分页浏览。' +
'返回 sessionId、seq、turn、role、text 与时间。用于核对记忆条目、追查原话出处。',
parameters: {
type: 'object',
properties: {
session_id: { type: 'string', description: '会话 id(必填,可用 memory_raw_search 获得)。' },
seq: { type: 'integer', description: '可选:精确读取该事件 seq 对应的那一条。' },
offset: { type: 'integer', description: '可选:从第几条开始(默认 0)。' },
limit: { type: 'integer', description: '可选:读取条数(1..500,默认取配置值)。' },
},
required: ['session_id'],
additionalProperties: false,
},
output: {
schema: {
type: 'object',
properties: {
session_id: { type: 'string' },
total: { type: 'number' },
items: { type: 'array', items: { type: 'object' } },
},
required: ['session_id', 'items'],
additionalProperties: true,
},
render: renderJson,
},
async execute(args) {
const settings = deps.settings();
if (!settings.enabled)
throw new Error('原始记录层当前已禁用(dsh-brain-raw 行未启用)。');
const sessionId = typeof args.session_id === 'string' ? args.session_id.trim() : '';
if (sessionId === '')
throw new Error('session_id 不能为空。');
const seq = typeof args.seq === 'number' && Number.isFinite(args.seq) ? Math.trunc(args.seq) : undefined;
const offset = typeof args.offset === 'number' && Number.isFinite(args.offset) ? Math.trunc(args.offset) : 0;
const requested = typeof args.limit === 'number' && Number.isFinite(args.limit) ? Math.trunc(args.limit) : settings.maxReadRecords;
const limit = Math.min(500, Math.max(1, requested));
const result = await deps.store.readSession(sessionId, { ...(seq === undefined ? {} : { seq }), offset, limit });
return { session_id: result.sessionId, total: result.total, items: result.items };
},
};
}
function statusTool(deps) {
return {
name: 'memory_raw_status',
description: '查看原始记录层状态:保存目录、会话数、记录数、占用空间、最早/最新时间与保留天数。',
parameters: { type: 'object', properties: {}, additionalProperties: false },
output: {
schema: { type: 'object', properties: {}, required: [], additionalProperties: true },
render: renderJson,
},
async execute() {
const settings = deps.settings();
return deps.store.status(settings.enabled, settings.retentionDays);
},
};
}
/** Build the three raw-layer tools. */
export function createRawMemoryTools(deps) {
return [searchTool(deps), readTool(deps), statusTool(deps)];
}