/**
* Journal and discovery tool handlers.
* @module
*/
import type { LogseqOperations, GetRecentJournalsOptions } from "@logseq-ai/core";
import type { ToolResult } from "../types.js";
import { validate } from "../validation/index.js";
import {
getTodaySchema,
appendToTodaySchema,
getRecentJournalsSchema,
findRelatedPagesSchema,
getBlockBacklinksSchema,
} from "../validation/journal-schemas.js";
export async function handleJournalTools(
ops: LogseqOperations,
name: string,
args: Record<string, unknown> | undefined
): Promise<ToolResult | null> {
switch (name) {
case "get_today": {
validate(getTodaySchema, args);
const today = await ops.getToday();
return {
content: [{ type: "text", text: JSON.stringify(today, null, 2) }],
};
}
case "append_to_today": {
const { content } = validate(appendToTodaySchema, args);
const block = await ops.appendToToday(content);
return {
content: [
{ type: "text", text: `Appended to today's journal.\nBlock UUID: ${block.uuid}` },
],
};
}
case "get_recent_journals": {
const validated = validate(getRecentJournalsSchema, args);
const journals = await ops.getRecentJournals(validated as GetRecentJournalsOptions);
return {
content: [{ type: "text", text: JSON.stringify(journals, null, 2) }],
};
}
case "find_related_pages": {
const { pageName } = validate(findRelatedPagesSchema, args);
const links = await ops.findRelatedPages(pageName);
return {
content: [{ type: "text", text: JSON.stringify(links, null, 2) }],
};
}
case "get_block_backlinks": {
const { uuid } = validate(getBlockBacklinksSchema, args);
const backlinks = await ops.getBlockBacklinks(uuid);
return {
content: [{ type: "text", text: JSON.stringify(backlinks, null, 2) }],
};
}
default:
return null;
}
}