/** @module
@summary Helpers for building user-facing agents: ask the user a question through an interrupt, and manage a todo list.
Helpers for building user-facing agents. This module offers two
independent tools:
- `question` asks the user something through an interrupt the host UI
can render, and
- `todoWrite` / `todoList` give the LLM a small todo
list to track multi-step work across turns.
*/

type Todo = {
  id: string;
  text: string;
  status: "pending" | "in_progress" | "completed"
}

let _todos: Todo[] = []

effect std::question {
  prompt: string
}

export def todoAdd(todo: Todo): Todo[] {
  """
  Add a new todo to the list.
  """
  _todos.push(todo)
  return _todos
}

export def todoUpdate(
  id: string,
  status: "pending" | "in_progress" | "completed",
): Todo[] {
  """
  Update the status of a todo by id.
  """
  for (todo in _todos) {
    if (todo.id == id) {
      todo.status = status
      break
    }
  }
  return _todos
}

export def todoWrite(todos: Todo[]): Todo[] {
  """
  Replace the current todo list.
  """
  _todos = todos
  return _todos
}

export def todoList(): Todo[] {
  """
  Return the current todo list.
  """
  return _todos
}

/**
Alternative to the `input` function, raises an interrupt instead,
so it can be used anywhere (eg a web server) instead of just the CLI.
*/
export def question(prompt: string): string {
  """
  Ask the user a question and wait for their reply.

  @param prompt - The question to show the user
  """
  let answer = interrupt std::question(prompt, {
    prompt: prompt
  })
  return answer
}
