import type { Grant, Person, Role } from "./actor.ts"; import type { Id, Outcome, Page, PageRequest } from "./common.ts"; import type { CiSettings, NamespaceSecrets, RepoCi } from "./ci.ts"; import type { TimelineItem } from "./events.ts"; import type { AccessToken, AgentConsentView, AgentGrant, BranchSummary, Choice, ChangeSet, CodeMapState, CodeMapSummary, PullCodeMap, CommitDetail, CommitSummary, LineRange, Mergeability, PullCounts, PullBranch, PullDetail, PullSummary, File, Invitation, Label, LabelColor, LabelWithUsage, LastChanges, Location, Member, MentionState, OnlineAgent, PermissionRequest, Org, RawFile, RefList, ReviewThread, RepoSummary, TaskCounts, TaskDetail, TaskStatus, TaskSummary, Tree, Viewer, Visibility, AdminOverview, NamespaceRef, HostedAgent, AgentRunView, } from "./views.ts"; /** * The backend's entire public surface. Everything a caller can do starts here, and * every permission check happens behind it: a handle only exists if the actor may use it. */ export interface Backend { /** * Resolves the request's session cookie or access token, then opens a handle bound to that actor. Anonymous requests get an anonymous handle. * Namespace-wide work (repositories, tasks, labels, members) happens in `namespace`, or in the viewer's own namespace when it is omitted. */ refs(request: Request, namespace?: string): Promise; /** Operations that happen before there is a signed-in actor. */ gate(): Gate; /** Serves a Git smart-HTTP request, or returns null when the request is not a Git URL. */ git(request: Request): Promise; /** Serves a ref protocol request under `/api/`, or returns null for any other URL. */ api(request: Request): Promise; /** Upgrades `//live` to a socket on the repository's hub, or returns null for any other request. */ live(request: Request): Promise; /** The OAuth consent step for an agent client, such as Claude Code or Codex, asking to act for the signed-in human. */ agentConsent(request: Request): AgentConsent; } /** A consent step either continues, or ends in a redirect back to the client. Send `headers` with the response. */ export type ConsentStep = Outcome; export interface AgentConsent { /** Throws `sign_in_required` unless a human is signed in with a browser session. */ begin(): Promise>; approve(handle: string): Promise>; deny(handle: string): Promise>; } export interface Session { readonly token: string; readonly maxAge: number; } export interface SignUpInput { readonly name: string; /** The person's own namespace, created with the account. */ readonly handle: string; readonly email: string; readonly password: string; /** The client address, used to rate-limit attempts. */ readonly ip: string; } export interface SignInInput { readonly email: string; readonly password: string; /** The client address, used to rate-limit attempts. */ readonly ip: string; } export interface Gate { /** Creates the account and its namespace, and signs the person in. */ signUp(input: SignUpInput): Promise>; signIn(input: SignInInput): Promise>; /** Emails a one-hour reset link when the address has an account. Succeeds either way, so it does not reveal who has an account. */ requestPasswordReset(input: { readonly email: string; readonly ip: string }): Promise; /** Sets a new password from a reset link, ends every other session, and signs the person in. The link works once. */ resetPassword(input: { readonly token: string; readonly password: string }): Promise>; signOut(sessionToken: string): Promise; /** Throws `not_found` when the token is unknown, used or expired. */ invitation(token: string): Promise; } export interface PendingInvitation { readonly org: Org; readonly email: string; readonly role: Role; /** True when the invited email already has an account, which joins by signing in and calling `Refs.acceptInvitation`. */ readonly existingAccount: boolean; /** Creates the account with its username and personal namespace, then joins the inviting namespace. */ accept(input: { readonly name: string; readonly handle: string; readonly password: string }): Promise>; } export interface NewRepo { readonly name: string; readonly description: string; readonly visibility: Visibility; /** Copy an existing public HTTPS Git repository instead of starting empty. */ readonly importFrom?: string; /** Without `importFrom`: start with a first commit of AGENTS.md, CLAUDE.md and README.md that set agents up for refs, instead of empty. */ readonly setUp?: boolean; /** The slug of a namespace the viewer belongs to. Defaults to the handle's namespace for new repositories, and to the viewer's own account for forks. */ readonly owner?: string; } export interface NewOrganization { readonly slug: string; readonly name: string; } export interface TaskQuery { readonly status: TaskStatus; readonly repo?: string; readonly label?: string; readonly assignee?: "me" | Id; readonly page?: PageRequest; } export interface NewTask { readonly title: string; readonly body: string; readonly repo: string; readonly assignee: Id | null; readonly labels: readonly Id[]; } /** * Everything the current actor can reach. Anonymous actors can read public repositories * and nothing else; any method that needs more throws `sign_in_required`. */ export interface Refs { readonly viewer: Viewer; repos(): Promise; /** Throws `not_found` when the repository is missing or the actor may not see it. */ repo(name: string): Promise; createRepo(input: NewRepo): Promise>; /** Creates an organization owned by the signed-in person. Its slug shares one space with usernames. */ createOrganization(input: NewOrganization): Promise>; /** Joins the invitation's namespace as the signed-in person, whose email must be the invited one. Throws `not_found` when the token is unknown, used or expired. */ acceptInvitation(token: string): Promise>; tasks(query: TaskQuery): Promise<{ readonly page: Page; readonly counts: TaskCounts }>; /** * Throws `not_found` when the task is missing or outside the actor's grant. A task that was * transferred is still found by its old repository and number; `detail` then holds its * current location, so callers can redirect. */ task(repo: string, number: number): Promise; createTask(input: NewTask): Promise>; /** The repositories, people and labels a task can be linked to. */ taskChoices(): Promise<{ readonly repos: Choice[]; readonly people: Choice[]; readonly labels: Label[] }>; labels(): Promise; /** Throws `sign_in_required` for anonymous actors. */ me(): Promise; /** Throws `not_found` unless the actor may manage members. */ members(): Promise; /** Usage across the whole service. Only the service's admins (ADMIN_EMAILS) get it; everyone else gets `not_found`. */ adminOverview(): Promise; /** Throws `not_found` unless the actor is an owner or admin of the namespace. */ namespaceSecrets(): Promise; namespaceAgents(): Promise; /** A narrower handle for the same actor. Grants only ever intersect, so this can never widen access. */ attenuate(grant: Grant): Refs; } export type RepoHome = | { readonly kind: "preparing" } | { readonly kind: "empty" } | { readonly kind: "tree"; readonly refs: RefList; readonly tree: Tree }; /** * One repository the actor can see. Paths are passed as the URL segments after the view * (for example `["feature", "x", "src", "app.ts"]`); the handle works out which part is * the branch, tag or commit and which part is the path. */ export interface Repo { readonly summary: RepoSummary; home(): Promise; refs(): Promise; /** Every branch, the default branch first, then the newest commit first. */ branches(): Promise; /** Deletes a branch. Refused for the default branch, and while an open pull request or an agent uses the branch. */ deleteBranch(name: string): Promise; /** Each throws `not_found` when the ref or path does not exist. */ tree(at: readonly string[]): Promise; file(at: readonly string[]): Promise; raw(at: readonly string[]): Promise; history(at: readonly string[], page?: PageRequest): Promise<{ readonly location: Location; readonly page: Page }>; lastChanges(at: readonly string[]): Promise; /** What merging `head` into `base` would change. Both are branch names. */ compare(base: string, head: string): Promise; pulls(query: PullQuery): Promise<{ readonly items: PullSummary[]; readonly counts: PullCounts }>; /** Throws `not_found` when the pull request is missing or outside the actor's grant. */ pull(number: number): Promise; openPull(input: NewPull): Promise>; commit(hash: string): Promise; /** Whether the code map at a branch, a tag or a commit (the default branch when `ref` is absent) is ready. Starts building it when it is not. Throws `not_found` when the ref does not exist. */ codeMap(ref?: string): Promise<{ readonly ref: string; readonly map: CodeMapSummary }>; /** The full code map of one commit. Commits never change, so a ready map can be cached for good. */ codeGraph(commit: string): Promise; /** Agent sessions connected to this repository right now, idle or on a branch. Empty for anonymous viewers. */ agents(): Promise; /** Tool approvals agents on this repository are waiting for. */ permissions(): Promise; /** The newest event on this repository, and on one of its tasks if given. Changes whenever a page showing them should refresh. */ latest(task?: number): Promise; /** Answers one; only the agent's own human may. */ decidePermission(session: Id, requestId: string, behavior: "allow" | "deny"): Promise; fork(input: Omit): Promise>; /** Throws `not_found` unless the actor may administer this repository. */ settings(): Promise; ci(): RepoCi; hostedAgents(): Promise; /** The recent runs of hosted agents here. `open` also loads the steps of that finished run. */ agentRuns(open: Id | null): Promise; } export interface RepoSettings { readonly description: string; readonly visibility: Visibility; update(changes: { readonly description?: string; readonly visibility?: Visibility }): Promise; /** Permanently deletes the repository and its history. `confirmName` must equal the repository's name. */ delete(confirmName: string): Promise; readonly ci: CiSettings; } /** * A task is a plain tracker item. It has no link to agent sessions, branches, pull requests or decisions: * agents do not take tasks, and nothing outside the task changes it. */ export interface Task { readonly detail: TaskDetail; timeline(): Promise; /** Adds a comment. With `then`, also closes or reopens the task in the same change. */ comment(body: string, then?: TaskStatus): Promise; setStatus(status: TaskStatus): Promise; rename(title: string): Promise; describe(body: string): Promise; assign(person: Id | null): Promise; /** Moves the task to another repository, where it takes the next number. Its old address keeps resolving to it. */ transfer(repo: string): Promise>; setLabels(labels: readonly Id[]): Promise; editComment(comment: Id, body: string): Promise; deleteComment(comment: Id): Promise; } export interface PullQuery { /** `closed` lists closed and merged pull requests. */ readonly status: "open" | "closed"; } export interface NewPull { readonly title: string; readonly body: string; readonly base: string; readonly head: string; } export interface ReviewCommentInput { readonly revision: string; readonly path: string; readonly range: LineRange; readonly body: string; readonly assign?: boolean; } export interface SteerInput { readonly revision: string; readonly decision: string; readonly body: string; readonly assign?: boolean; } const REVIEW_VERDICTS = ["comment", "approve", "request-changes"] as const; export type ReviewVerdict = (typeof REVIEW_VERDICTS)[number]; export function isReviewVerdict(value: string): value is ReviewVerdict { return (REVIEW_VERDICTS as readonly string[]).includes(value); } export interface ReviewInput { readonly revision: string; readonly verdict: ReviewVerdict; readonly body: string; } export interface BranchInput { /** The commit the new branch starts at: the pull request's merge base or one of its commits. */ readonly from: string; readonly title: string; /** What the agents should try from there. Mentioned agents receive it, and take the new branch with `assign`. */ readonly instruction: string; readonly assign?: boolean; } export interface Pull { readonly detail: PullDetail; timeline(): Promise; /** While open, the live branches; once merged or closed, the commits they pointed at then. Throws `not_found` when an open pull request's branch is gone. */ changes(): Promise; /** The head commit's code map and the lines each changed file changed, so the map can mark them. */ codeMap(): Promise; /** * With `then`, also closes or reopens the pull request in the same change. Agents it mentions receive it; * with `assign`, they also take the pull request's head branch. */ comment(body: string, then?: "open" | "closed", assign?: boolean): Promise; /** Starts a review thread on a range of lines in one file of the diff at `revision`. Fails when `revision` is no longer the head. */ reviewComment(input: ReviewCommentInput): Promise>; /** Starts a review thread on one of the decision records in the diff at `revision`. */ steer(input: SteerInput): Promise>; /** A human's verdict on the diff at `revision`, posted as a comment. Fails when `revision` is no longer the head. */ review(input: ReviewInput): Promise; threads(): Promise; /** * Explores another path from a point in this pull request's reasoning: a new branch, named from the title, that * starts at `from`, and a pull request for it into the same base. Only people with write access may branch. */ branch(input: BranchInput): Promise>; /** Pull requests branched from this one. */ branches(): Promise; /** Agents mentioned in the reply receive it with the thread's lines; their answer comes back into the thread. */ replyToThread(thread: number, body: string, assign?: boolean): Promise; mergeability(): Promise; /** * Merges the head into the base with a merge commit, as the person merging. Only a human with write access may * merge. Fails without changing anything when files conflict or the base moved since the check. */ merge(): Promise>; /** Only a human with write access may resolve or reopen a thread. */ resolveThread(thread: number): Promise; reopenThread(thread: number): Promise; /** Delivery state of agent mentions made in this pull request's comments. */ mentions(): Promise; /** Tool approvals agents working on the head branch are waiting for. */ permissions(): Promise; setStatus(status: "open" | "closed"): Promise; rename(title: string): Promise; } export interface LabelInput { readonly name: string; readonly color: LabelColor; readonly description: string; } export interface Labels { list(): Promise; create(input: LabelInput): Promise>; update(label: Id, input: LabelInput): Promise>; delete(label: Id): Promise; } export interface Me { readonly person: Person; tokens(): Promise; /** `secret` is returned once and never stored in readable form. */ createToken(name: string): Promise>; revokeToken(token: Id): Promise; /** Agent clients this person has authorized over OAuth. */ agentGrants(): Promise; revokeAgentGrant(grant: Id): Promise; } /** The hosted agents of a namespace or a repository. Everyone who can see them can mention them; admins change them. */ export interface HostedAgents { readonly canEdit: boolean; list(): Promise; /** * Adds or changes an agent at this level. With a provider, the key is tested with one small request first, and a key * that fails is not stored. An empty key keeps the saved key of the same provider. */ save(input: { readonly handle: string; readonly description: string; readonly instructions: string; readonly provider: string; readonly model: string; readonly key: string }): Promise; remove(handle: string): Promise; } export interface Members { /** The roles this actor may hand out. */ readonly invitableRoles: readonly Role[]; list(): Promise; invitations(): Promise; /** `link` is returned once; only its hash is stored. */ /** `invitee` is an email address or the username of an existing person. */ invite(input: { readonly invitee: string; readonly role: Role }): Promise>; revokeInvitation(invitation: Id): Promise; }