index.ts 2.0 KB

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