import { Agent, callable } from "agents";
import type { GeneratedEnv } from "@ayjnt/env";
type Note = { id: string; text: string; createdAt: number };
type State = { notes: Note[] };
export default class NotesAgent extends Agent<GeneratedEnv, State> {
override initialState: State = { notes: [] };
/**
* Add a note. The agent generates the id; only the server can.
* @callable
*/
@callable({ description: "Add a new note." })
async addNote(text: string): Promise<Note> {
const note = { id: crypto.randomUUID(), text, createdAt: Date.now() };
this.setState({ notes: [...this.state.notes, note] });
return note;
}
/**
* Delete a note by id. Returns true if it existed.
* @callable
*/
@callable({ description: "Delete a note by id." })
async deleteNote(id: string): Promise<boolean> {
const before = this.state.notes.length;
this.setState({ notes: this.state.notes.filter((n) => n.id !== id) });
return this.state.notes.length < before;
}
}