/**
* Task management tool handlers.
* @module
*/
import type {
LogseqOperations,
GetTasksOptions,
SearchTasksOptions,
GetTasksDueSoonOptions,
} from "@logseq-ai/core";
import type { ToolResult } from "../types.js";
import { validate } from "../validation/index.js";
import {
getTasksSchema,
createTaskSchema,
markTaskSchema,
markTasksSchema,
setTaskPrioritySchema,
setTaskDeadlineSchema,
setTaskScheduledSchema,
searchTasksSchema,
getOverdueTasksSchema,
getTasksDueSoonSchema,
getTaskStatsSchema,
} from "../validation/task-schemas.js";
export async function handleTaskTools(
ops: LogseqOperations,
name: string,
args: Record<string, unknown> | undefined
): Promise<ToolResult | null> {
switch (name) {
case "get_tasks": {
const validated = validate(getTasksSchema, args);
const tasks = await ops.getTasks(validated as GetTasksOptions);
return {
content: [{ type: "text", text: JSON.stringify(tasks, null, 2) }],
};
}
case "create_task": {
const { pageName, content, priority, deadline, scheduled } = validate(createTaskSchema, args);
const task = await ops.createTask({
pageName,
content,
...(priority && { priority }),
...(deadline && { deadline }),
...(scheduled && { scheduled }),
});
return {
content: [{ type: "text", text: `Created task: ${task.content}\nUUID: ${task.uuid}` }],
};
}
case "mark_task": {
const { uuid, marker } = validate(markTaskSchema, args);
await ops.markTask(uuid, marker);
return {
content: [{ type: "text", text: `Marked task ${uuid} as ${marker}` }],
};
}
case "mark_tasks": {
const { uuids, marker } = validate(markTasksSchema, args);
const result = await ops.markTasks(uuids, marker);
const lines: string[] = [
`Marked ${result.success.length}/${uuids.length} tasks as ${marker}`,
];
if (result.failed.length > 0) {
lines.push(`\nFailed (${result.failed.length}):`);
for (const f of result.failed) {
lines.push(` • ${f.uuid}: ${f.error}`);
}
}
return {
content: [{ type: "text", text: lines.join("\n") }],
isError: result.failed.length > 0 && result.success.length === 0,
};
}
case "set_task_priority": {
const { uuid, priority } = validate(setTaskPrioritySchema, args);
await ops.setTaskPriority(uuid, priority);
const message = priority
? `Set priority of task ${uuid} to [#${priority}]`
: `Removed priority from task ${uuid}`;
return {
content: [{ type: "text", text: message }],
};
}
case "set_task_deadline": {
const { uuid, date } = validate(setTaskDeadlineSchema, args);
await ops.setTaskDeadline({ uuid, date });
const message = date
? `Set deadline of task ${uuid} to ${date}`
: `Removed deadline from task ${uuid}`;
return {
content: [{ type: "text", text: message }],
};
}
case "set_task_scheduled": {
const { uuid, date } = validate(setTaskScheduledSchema, args);
await ops.setTaskScheduled({ uuid, date });
const message = date
? `Set scheduled date of task ${uuid} to ${date}`
: `Removed scheduled date from task ${uuid}`;
return {
content: [{ type: "text", text: message }],
};
}
case "search_tasks": {
const validated = validate(searchTasksSchema, args);
const searchResults = await ops.searchTasks(validated as SearchTasksOptions);
return {
content: [{ type: "text", text: JSON.stringify(searchResults, null, 2) }],
};
}
case "get_overdue_tasks": {
validate(getOverdueTasksSchema, args);
const overdueTasks = await ops.getOverdueTasks();
return {
content: [{ type: "text", text: JSON.stringify(overdueTasks, null, 2) }],
};
}
case "get_tasks_due_soon": {
const validated = validate(getTasksDueSoonSchema, args);
const dueSoonTasks = await ops.getTasksDueSoon(validated as GetTasksDueSoonOptions);
return {
content: [{ type: "text", text: JSON.stringify(dueSoonTasks, null, 2) }],
};
}
case "get_task_stats": {
const { pageName } = validate(getTaskStatsSchema, args);
const stats = await ops.getTaskStats(pageName);
return {
content: [{ type: "text", text: JSON.stringify(stats, null, 2) }],
};
}
default:
return null;
}
}