import { v } from "convex/values";

export const ARTIFACT_PATHS = [
  "agent.md",
  "work.md",
  "steps.md",
  "artifacts.md",
  "signals.md",
  "agent-manager.md",
  "context.md",
  "card.md",
] as const;

export type ArtifactPath = (typeof ARTIFACT_PATHS)[number];

export const artifactPath = v.union(
  v.literal("agent.md"),
  v.literal("work.md"),
  v.literal("steps.md"),
  v.literal("artifacts.md"),
  v.literal("signals.md"),
  v.literal("agent-manager.md"),
  v.literal("context.md"),
  v.literal("card.md")
);

export const createInitialArtifacts = (): readonly {
  path: ArtifactPath;
  content: string;
}[] => [
  {
    content:
      "# Agent\n\n## Role\n\nOwn one project issue at a time. Read the project artifacts before editing code, ask a focused question when blocked, and support completion claims with command output.\n\n## Guardrails\n\n- Keep issue work isolated in its AgentOS workspace.\n- Update work.md, steps.md, artifacts.md, and context.md as facts change.\n- Never mark work complete without a relevant runtime check.\n",
    path: "agent.md",
  },
  {
    content:
      "# Work\n\nNo issue has entered the agent queue yet. New issues are appended here by the control plane.\n",
    path: "work.md",
  },
  {
    content:
      "# Steps\n\n1. Read the issue and project artifacts.\n2. Inspect the repository context.\n3. Ask for missing decisions only when work cannot continue safely.\n4. Implement the smallest complete change.\n5. Run the relevant command or scenario.\n6. Publish changed artifacts and the final signal.\n",
    path: "steps.md",
  },
  {
    content:
      "# Artifacts\n\nThis file indexes concrete outputs from issue runs, including changed paths, command results, and preview links.\n",
    path: "artifacts.md",
  },
  {
    content:
      "# Project events\n\nThe agent manager emits `issue.queued`, `agent.started`, `agent.needs-input`, `agent.completed`, and `agent.failed`. These are durable project events and drive the web status.\n",
    path: "signals.md",
  },
  {
    content:
      "# Agent manager\n\n## State machine\n\n`open -> queued -> working -> completed`\n\nA blocked run moves from `working` to `needs-input`; a resumed run returns to `queued`. An unrecoverable run moves to `failed`.\n\nEach issue owns one Flue agent identity and one AgentOS actor key, so conversation and workspace state remain isolated.\n",
    path: "agent-manager.md",
  },
  {
    content:
      "# Context\n\nCanonical project knowledge is staged under /workspace/context. Issue-specific facts belong below.\n",
    path: "context.md",
  },
  {
    content:
      "# Project card\n\nThe web project workspace reads and writes Projects, context documents, artifacts, and issues through the generated Convex interface. Agent work uses the issue-scoped workspace and durable daemon commands.\n",
    path: "card.md",
  },
];
