63 lines
2 KiB
TypeScript
63 lines
2 KiB
TypeScript
import { Type } from "@sinclair/typebox";
|
|
|
|
import { loadConfig } from "../../config/config.js";
|
|
import { callGateway } from "../../gateway/call.js";
|
|
import type { AnyAgentTool } from "./common.js";
|
|
import { jsonResult, readStringParam } from "./common.js";
|
|
import {
|
|
resolveDisplaySessionKey,
|
|
resolveInternalSessionKey,
|
|
resolveMainSessionAlias,
|
|
stripToolMessages,
|
|
} from "./sessions-helpers.js";
|
|
|
|
const SessionsHistoryToolSchema = Type.Object({
|
|
sessionKey: Type.String(),
|
|
limit: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
includeTools: Type.Optional(Type.Boolean()),
|
|
});
|
|
|
|
export function createSessionsHistoryTool(): AnyAgentTool {
|
|
return {
|
|
label: "Session History",
|
|
name: "sessions_history",
|
|
description: "Fetch message history for a session.",
|
|
parameters: SessionsHistoryToolSchema,
|
|
execute: async (_toolCallId, args) => {
|
|
const params = args as Record<string, unknown>;
|
|
const sessionKey = readStringParam(params, "sessionKey", {
|
|
required: true,
|
|
});
|
|
const cfg = loadConfig();
|
|
const { mainKey, alias } = resolveMainSessionAlias(cfg);
|
|
const resolvedKey = resolveInternalSessionKey({
|
|
key: sessionKey,
|
|
alias,
|
|
mainKey,
|
|
});
|
|
const limit =
|
|
typeof params.limit === "number" && Number.isFinite(params.limit)
|
|
? Math.max(1, Math.floor(params.limit))
|
|
: undefined;
|
|
const includeTools = Boolean(params.includeTools);
|
|
const result = (await callGateway({
|
|
method: "chat.history",
|
|
params: { sessionKey: resolvedKey, limit },
|
|
})) as { messages?: unknown[] };
|
|
const rawMessages = Array.isArray(result?.messages)
|
|
? result.messages
|
|
: [];
|
|
const messages = includeTools
|
|
? rawMessages
|
|
: stripToolMessages(rawMessages);
|
|
return jsonResult({
|
|
sessionKey: resolveDisplaySessionKey({
|
|
key: sessionKey,
|
|
alias,
|
|
mainKey,
|
|
}),
|
|
messages,
|
|
});
|
|
},
|
|
};
|
|
}
|