todo.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { BusEvent } from "@/bus/bus-event"
  2. import { Bus } from "@/bus"
  3. import z from "zod"
  4. import { Storage } from "../storage/storage"
  5. export namespace Todo {
  6. export const Info = z
  7. .object({
  8. content: z.string().describe("Brief description of the task"),
  9. status: z.string().describe("Current status of the task: pending, in_progress, completed, cancelled"),
  10. priority: z.string().describe("Priority level of the task: high, medium, low"),
  11. id: z.string().describe("Unique identifier for the todo item"),
  12. })
  13. .meta({ ref: "Todo" })
  14. export type Info = z.infer<typeof Info>
  15. export const Event = {
  16. Updated: BusEvent.define(
  17. "todo.updated",
  18. z.object({
  19. sessionID: z.string(),
  20. todos: z.array(Info),
  21. }),
  22. ),
  23. }
  24. export async function update(input: { sessionID: string; todos: Info[] }) {
  25. await Storage.write(["todo", input.sessionID], input.todos)
  26. Bus.publish(Event.Updated, input)
  27. }
  28. export async function get(sessionID: string) {
  29. return Storage.read<Info[]>(["todo", sessionID])
  30. .then((x) => x || [])
  31. .catch(() => [])
  32. }
  33. }