index.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import z from "zod"
  2. import { Config } from "../config/config"
  3. import { Instance } from "../project/instance"
  4. import PROMPT_INITIALIZE from "./template/initialize.txt"
  5. import { Bus } from "../bus"
  6. import { Identifier } from "../id/id"
  7. export namespace Command {
  8. export const Default = {
  9. INIT: "init",
  10. } as const
  11. export const Event = {
  12. Executed: Bus.event(
  13. "command.executed",
  14. z.object({
  15. name: z.string(),
  16. sessionID: Identifier.schema("session"),
  17. arguments: z.string(),
  18. messageID: Identifier.schema("message"),
  19. }),
  20. ),
  21. }
  22. export const Info = z
  23. .object({
  24. name: z.string(),
  25. description: z.string().optional(),
  26. agent: z.string().optional(),
  27. model: z.string().optional(),
  28. template: z.string(),
  29. subtask: z.boolean().optional(),
  30. })
  31. .meta({
  32. ref: "Command",
  33. })
  34. export type Info = z.infer<typeof Info>
  35. const state = Instance.state(async () => {
  36. const cfg = await Config.get()
  37. const result: Record<string, Info> = {}
  38. for (const [name, command] of Object.entries(cfg.command ?? {})) {
  39. result[name] = {
  40. name,
  41. agent: command.agent,
  42. model: command.model,
  43. description: command.description,
  44. template: command.template,
  45. subtask: command.subtask,
  46. }
  47. }
  48. if (result[Default.INIT] === undefined) {
  49. result[Default.INIT] = {
  50. name: Default.INIT,
  51. description: "create/update AGENTS.md",
  52. template: PROMPT_INITIALIZE.replace("${path}", Instance.worktree),
  53. }
  54. }
  55. return result
  56. })
  57. export async function get(name: string) {
  58. return state().then((x) => x[name])
  59. }
  60. export async function list() {
  61. return state().then((x) => Object.values(x))
  62. }
  63. }