import { agentsSection } from "@refs/protocol"; import { commitBody, gitObject, pushRef, treeBody } from "./gitwrite.ts"; const encoder = new TextEncoder(); /** The files a new repository starts with, so agents that open it know to work over the ref protocol. */ export function starterFiles(input: { readonly repo: string; readonly name: string; readonly description: string; readonly origin: string; readonly branch: string }): { readonly path: string; readonly text: string }[] { const agents = `# AGENTS.md\n\n${agentsSection(input)}`; return [ { path: "AGENTS.md", text: agents }, { path: "CLAUDE.md", text: "@AGENTS.md\n" }, { path: "README.md", text: `# ${input.name}\n${input.description ? `\n${input.description}\n` : ""}` }, ]; } /** Writes `files` as the first commit of the empty branch `branch`. Fails, without changing anything, when the branch already exists. */ export async function writeFirstCommit( handle: ArtifactsRepo, branch: string, files: readonly { readonly path: string; readonly text: string }[], author: { readonly name: string; readonly email: string }, at: number, message: string, ): Promise<{ ok: true; commit: string } | { ok: false; reason: string }> { const blobs = await Promise.all(files.map(async (file) => ({ path: file.path, blob: await gitObject("blob", encoder.encode(file.text)) }))); const tree = await gitObject("tree", treeBody(blobs.map(({ path, blob }) => ({ mode: "100644", name: path, hash: blob.hash })))); const commit = await gitObject("commit", commitBody({ tree: tree.hash, parents: [], author, at, message })); const pushed = await pushRef(handle, `refs/heads/${branch}`, "0".repeat(40), commit.hash, [...blobs.map(({ blob }) => blob), tree, commit]); return pushed.ok ? { ok: true, commit: commit.hash } : pushed; }