import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
import {
  ManageTodoListParamsSchema,
  buildExecutionReminder,
  createExecutionStateFromTodos,
  createTodoWidgetView,
  executeManageTodoList,
  executionStateToTodos,
  restoreExecutionState
} from "../../../chunks/chunk-SW6G4XW2.js";
import "../../../chunks/chunk-D7NAXU7F.js";
import "../../../chunks/chunk-TG2EQLX2.js";

// builtin/extensions/todo/index.ts
var EXECUTION_METADATA_KEY = "executionState";
var TOOL_DESCRIPTION = `Manage a session-local todo list. Use when the user explicitly asks to use the todo tool, create/manage todos, track tasks, follow a todo list, show tasks, or mark a task done.
NEVER use for: internal planning, tracking your own progress, remembering subtasks, or creating checklists for yourself.
Use action=create with items to create a list; the tool starts item 1 automatically. Use action=complete_current after finishing the current item; the tool advances to the next item or clears the list after the final item.
When a todo list exists, keep executing from the current in-progress item until it is cleared. If the work cannot continue, explain the failure and use action=clear with a reason.
Actions: create, read, complete_current, clear.`;
function persistExecutionState(ctx, state) {
  const nextState = state ?? restoreExecutionState(null);
  ctx.setSessionMetadata(EXECUTION_METADATA_KEY, nextState);
  ctx.setExecutionState(nextState);
  ctx.setExecutionReminder(buildExecutionReminder(nextState) ?? void 0);
  return nextState;
}
function notifyWidget(ctx, state) {
  if (!ctx.hasUI || !ctx.ui) return;
  const steps = createTodoWidgetView(state);
  if (steps.length === 0) return;
  const completed = steps.filter((step) => step.status === "completed").length;
  ctx.ui.notify(`Todo tracking: ${completed}/${steps.length} completed`, "info");
}
function todoExtension(ctx) {
  ctx.registerTool({
    name: "manage_todo_list",
    label: "manage_todo_list",
    description: TOOL_DESCRIPTION,
    parameters: ManageTodoListParamsSchema,
    async execute(args) {
      const currentTodos = executionStateToTodos(ctx.getExecutionState());
      const execution = executeManageTodoList(args, currentTodos);
      const nextState = createExecutionStateFromTodos(execution.nextTodos, "todo", "user_todo");
      persistExecutionState(ctx, nextState);
      notifyWidget(ctx, nextState);
      return execution.result;
    }
  });
  ctx.on("session:changed", async () => {
    const state = restoreExecutionState(ctx.getSessionMetadata()?.[EXECUTION_METADATA_KEY]);
    persistExecutionState(ctx, state);
  });
  ctx.on("tool:execution:end", async (event) => {
    if (event.toolName !== "manage_todo_list") return;
    const details = event.result?.details;
    if (!details?.todos) return;
    const state = createExecutionStateFromTodos(details.todos, "todo", "user_todo");
    persistExecutionState(ctx, state);
  });
  ctx.on("agent:end", async () => {
    const state = ctx.getExecutionState();
    if (state.mode !== "todo") return;
    if (state.lifecycle === "completed") {
      persistExecutionState(ctx, null);
    }
  });
  ctx.log("Builtin todo extension ready");
}
export {
  todoExtension as default
};
