/**
* Template tool handlers.
* @module
*/
import type { LogseqOperations } from "@logseq-ai/core";
import type { ToolResult } from "../types.js";
export async function handleTemplateTools(
ops: LogseqOperations,
name: string,
args: Record<string, unknown> | undefined
): Promise<ToolResult | null> {
switch (name) {
case "list_templates": {
const result = await ops.listTemplates();
const lines = [
`📋 Found ${result.total} templates:`,
``,
...result.templates.map((t) => {
const fh = t.hasFullHouseFeatures ? " 🏛️" : "";
const props = t.properties.length > 0 ? ` (${t.properties.join(", ")})` : "";
return ` • **${t.name}**${fh}${props}`;
}),
];
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
case "get_template": {
const templateName = args?.name as string;
const template = await ops.getTemplate(templateName);
if (!template) {
return {
content: [{ type: "text", text: `Template not found: ${templateName}` }],
isError: true,
};
}
const lines = [
`📄 Template: **${template.name}**`,
``,
`**Full House Features:** ${template.hasFullHouseFeatures ? "Yes 🏛️" : "No"}`,
];
if (template.fullHouseExpressions.length > 0) {
lines.push(`**Expressions:** ${template.fullHouseExpressions.join(", ")}`);
}
if (template.fillableProperties.length > 0) {
lines.push(`**Fillable Properties:** ${template.fillableProperties.join(", ")}`);
}
lines.push(``, `**Structure:**`);
const formatBlocks = (blocks: typeof template.blocks, indent = 0): string[] => {
const result: string[] = [];
for (const block of blocks) {
const prefix = " ".repeat(indent) + "- ";
const content =
block.content.length > 60 ? block.content.substring(0, 60) + "..." : block.content;
result.push(prefix + content.replace(/\n/g, " "));
if (block.children.length > 0) {
result.push(...formatBlocks(block.children, indent + 1));
}
}
return result;
};
lines.push(...formatBlocks(template.blocks));
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
case "create_page_from_template": {
const pageName = args?.pageName as string;
const template = args?.template as string;
const variables = (args?.variables as Record<string, string>) || {};
const result = await ops.createPageFromTemplate({
pageName,
template,
variables,
});
const lines = [
`✅ Created page "**${pageName}**" from template "${result.templateName}"`,
``,
`**Blocks created:** ${result.blocksCreated}`,
];
if (result.filledProperties.length > 0) {
lines.push(`**Properties filled:** ${result.filledProperties.join(", ")}`);
}
if (result.hasUnprocessedExpressions) {
lines.push(
``,
`⚠️ Template contains Full House expressions that will be rendered when you view the page in Logseq.`
);
}
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
case "create_blocks_from_template": {
const pageName = args?.pageName as string;
const template = args?.template as string;
const insertPosition =
(args?.insertPosition as "append" | "prepend" | "under-heading") || "append";
const heading = args?.heading as string | undefined;
const variables = (args?.variables as Record<string, string>) || {};
const result = await ops.createBlocksFromTemplate({
pageName,
template,
insertPosition,
heading,
variables,
});
const lines = [
`✅ Added blocks from template "${result.templateName}" to "**${pageName}**"`,
``,
`**Blocks created:** ${result.blocksCreated}`,
];
if (insertPosition === "under-heading" && heading) {
lines.push(`**Inserted under:** ${heading}`);
}
if (result.filledProperties.length > 0) {
lines.push(`**Properties filled:** ${result.filledProperties.join(", ")}`);
}
if (result.hasUnprocessedExpressions) {
lines.push(
``,
`⚠️ Template contains Full House expressions that will be rendered when you view the page in Logseq.`
);
}
return {
content: [{ type: "text", text: lines.join("\n") }],
};
}
default:
return null;
}
}