import { useRegisterState } from 'cedar-os';
import { z } from 'zod';
// Define argument schemas for state setters
const addTodoSchema = z.object({
text: z.string().min(1, 'Todo text cannot be empty'),
priority: z.enum(['low', 'medium', 'high']).default('medium'),
dueDate: z.string().datetime().optional(),
tags: z.array(z.string()).default([]),
});
const updateTodoSchema = z.object({
id: z.number().positive(),
updates: z.object({
text: z.string().min(1).optional(),
completed: z.boolean().optional(),
priority: z.enum(['low', 'medium', 'high']).optional(),
dueDate: z.string().datetime().nullable().optional(),
tags: z.array(z.string()).optional(),
}),
});
function TodoComponent() {
const [todos, setTodos] = useState([]);
useRegisterState({
key: 'todos',
value: todos,
setValue: setTodos,
description: 'List of user todos with priorities and due dates',
stateSetters: {
addTodo: {
name: 'addTodo',
description: 'Add a new todo item',
argsSchema: addTodoSchema,
execute: (currentTodos, args) => {
// args is fully typed: { text: string; priority: 'low' | 'medium' | 'high'; dueDate?: string; tags: string[] }
const newTodo = {
id: Date.now(),
...args,
completed: false,
createdAt: new Date().toISOString(),
};
setTodos([...currentTodos, newTodo]);
},
},
},
});
}
// Type inference from schemas
type AddTodoArgs = z.infer<typeof addTodoSchema>;
type UpdateTodoArgs = z.infer<typeof updateTodoSchema>;