Intermediate 25 min

Implementing Tools (Code)

Create the Tools File

Create src/tools.ts:

type Todo = {
  id: number;
  text: string;
  done: boolean;
  dueDate?: string;
};

let nextId = 1;
const todos: Todo[] = [];

export function addTodo(text: string, dueDate?: string): Todo {
  const todo: Todo = {
    id: nextId++,
    text,
    done: false,
    dueDate,
  };
  todos.push(todo);
  return todo;
}

export function listTodos(): Todo[] {
  return todos;
}

export function completeTodo(id: number): Todo | null {
  const todo = todos.find((t) => t.id === id);
  if (!todo) {
    return null;
  }
  todo.done = true;
  return todo;
}

Understanding the Code

addTodo: Creates a new todo with an auto-incrementing ID, adds it to the array, and returns it.

listTodos: Returns all todos in the array.

completeTodo: Finds a todo by ID, marks it as done, and returns it. Returns null if not found.

Test the Tools

Create src/tools.demo.ts to test without the AI:

import { addTodo, listTodos, completeTodo } from './tools';

// Add two todos
const todo1 = addTodo('Buy groceries');
const todo2 = addTodo('Call dentist', '2025-11-25');

console.log('Added todos:', todo1, todo2);

// List all todos
const allTodos = listTodos();
console.log('All todos:', allTodos);

// Complete one
const completed = completeTodo(todo1.id);
console.log('Completed:', completed);

// List again
const remaining = listTodos();
console.log('Remaining todos:', remaining);

Run it:

npx ts-node src/tools.demo.ts

You should see the todos being added, listed, and completed.


🔷 TypeScript Test the Tools
📟 Console Output
Run code to see output...

Try modifying the code above. Add more todos, complete different ones, see how it works.

Next, we’ll wire these tools to the LLM.