| 1 | import type { User, Viewer } from "./identity"; |
| 2 | import type { RepoPath } from "./repos"; |
| 3 | import type { Result } from "./result"; |
| 4 | |
| 5 | /** |
| 6 | * The filter on lists of issues and pull requests. An open pull request is |
| 7 | * a draft or one ready for review; a closed one was merged or closed |
| 8 | * without merging. |
| 9 | */ |
| 10 | export type State = "open" | "closed"; |
| 11 | |
| 12 | /** Why an issue was closed. */ |
| 13 | export type IssueReason = "completed" | "not_planned"; |
| 14 | |
| 15 | /** |
| 16 | * Something that should change in a repository: a bug, a feature, a |
| 17 | * question. Opened by a person, an agent or an integration. Pull requests |
| 18 | * are made against it; the one that is merged resolves it. |
| 19 | * |
| 20 | * Issues and pull requests share one sequence of numbers per repository. |
| 21 | */ |
| 22 | export type Issue = { |
| 23 | id: string; |
| 24 | repoId: string; |
| 25 | /** Shown as `#12`. */ |
| 26 | number: number; |
| 27 | title: string; |
| 28 | /** Markdown. Also what an agent is given to work from. */ |
| 29 | body: string; |
| 30 | labels: string[]; |
| 31 | /** Commands that must pass for a pull request to be accepted. */ |
| 32 | checks: string[]; |
| 33 | state: State; |
| 34 | /** Set when closed. */ |
| 35 | reason: IssueReason | null; |
| 36 | /** The number of the pull request whose merge closed this issue. */ |
| 37 | resolvedBy: number | null; |
| 38 | author: User; |
| 39 | /** RFC 3339. */ |
| 40 | createdAt: string; |
| 41 | /** RFC 3339. */ |
| 42 | updatedAt: string; |
| 43 | /** RFC 3339. */ |
| 44 | closedAt: string | null; |
| 45 | /** Pull requests made against this issue, in any state. */ |
| 46 | pullCount: number; |
| 47 | commentCount: number; |
| 48 | }; |
| 49 | |
| 50 | /** `draft` is still being worked on; `open` is ready for review. */ |
| 51 | export type PullStatus = "draft" | "open" | "merged" | "closed"; |
| 52 | |
| 53 | /** Where the agent runs: on g1t's sandboxes, or in someone's own session. */ |
| 54 | export type Runtime = "hosted" | "external"; |
| 55 | |
| 56 | /** |
| 57 | * A proposed change. It is made either in a fork created for it, which is |
| 58 | * how agents work, or on a branch pushed to the repository itself. |
| 59 | */ |
| 60 | export type Pull = { |
| 61 | id: string; |
| 62 | repoId: string; |
| 63 | /** Shown as `#12`. */ |
| 64 | number: number; |
| 65 | /** The number of the issue this is for, if any. */ |
| 66 | issue: number | null; |
| 67 | title: string; |
| 68 | /** Markdown: what changed and why. Set when marked ready. */ |
| 69 | body: string | null; |
| 70 | /** A label for the agent doing the work, e.g. `claude-code`. */ |
| 71 | agent: string; |
| 72 | runtime: Runtime; |
| 73 | status: PullStatus; |
| 74 | /** The fork holding the change, unless it is on a branch. */ |
| 75 | fork: RepoPath | null; |
| 76 | /** The fork's repository id. */ |
| 77 | forkRepoId: string | null; |
| 78 | /** The branch of the repository holding the change, unless it is in a fork. */ |
| 79 | branch: string | null; |
| 80 | headCommit: string | null; |
| 81 | /** |
| 82 | * For a merged pull request, what the branch pointed to before the merge. |
| 83 | * Comparing against it shows what the pull request changed. |
| 84 | */ |
| 85 | mergeBase: string | null; |
| 86 | /** Username of whoever merged it. */ |
| 87 | mergedBy: string | null; |
| 88 | /** RFC 3339. */ |
| 89 | mergedAt: string | null; |
| 90 | /** |
| 91 | * Set on a pull request closed because another one for the same issue was |
| 92 | * merged: that one's number. |
| 93 | */ |
| 94 | supersededBy: number | null; |
| 95 | author: User; |
| 96 | /** RFC 3339. */ |
| 97 | createdAt: string; |
| 98 | /** RFC 3339. */ |
| 99 | updatedAt: string; |
| 100 | }; |
| 101 | |
| 102 | export type Comment = { |
| 103 | id: string; |
| 104 | author: User; |
| 105 | /** Markdown. */ |
| 106 | body: string; |
| 107 | /** RFC 3339. */ |
| 108 | createdAt: string; |
| 109 | }; |
| 110 | |
| 111 | export type SessionEntryKind = "prompt" | "message" | "tool_call" | "tool_result" | "note"; |
| 112 | |
| 113 | /** One step of an agent's session: the "why" behind a pull request's commits. */ |
| 114 | export type SessionEntry = { |
| 115 | seq: number; |
| 116 | kind: SessionEntryKind; |
| 117 | text: string; |
| 118 | /** For tool calls and results. */ |
| 119 | tool: string | null; |
| 120 | /** The fork's head commit when this entry was recorded, if known. */ |
| 121 | commit: string | null; |
| 122 | /** RFC 3339. */ |
| 123 | at: string; |
| 124 | }; |
| 125 | |
| 126 | export type NewSessionEntry = Pick<SessionEntry, "kind" | "text"> & |
| 127 | Partial<Pick<SessionEntry, "tool" | "commit">>; |
| 128 | |
| 129 | export type IssueDetail = { |
| 130 | issue: Issue; |
| 131 | /** Every pull request made against it, oldest first. */ |
| 132 | pulls: Pull[]; |
| 133 | comments: Comment[]; |
| 134 | }; |
| 135 | |
| 136 | export type PullDetail = { |
| 137 | pull: Pull; |
| 138 | /** The issue it is for, if any. */ |
| 139 | issue: Issue | null; |
| 140 | comments: Comment[]; |
| 141 | }; |
| 142 | |
| 143 | export type OpenIssueInput = { |
| 144 | title: string; |
| 145 | body: string; |
| 146 | labels?: string[]; |
| 147 | checks?: string[]; |
| 148 | }; |
| 149 | |
| 150 | export type UpdateIssueInput = { title?: string; body?: string; labels?: string[] }; |
| 151 | |
| 152 | export type OpenPullInput = { |
| 153 | /** The number of the issue this is for. */ |
| 154 | issue?: number; |
| 155 | /** Defaults to the issue's title; required without an issue. */ |
| 156 | title?: string; |
| 157 | /** What changed and why. Usually set later, when a draft is marked ready. */ |
| 158 | body?: string; |
| 159 | /** |
| 160 | * A branch of the repository that already holds the change. The pull |
| 161 | * request is then ready for review at once and has no fork. |
| 162 | */ |
| 163 | branch?: string; |
| 164 | agent: string; |
| 165 | runtime: Runtime; |
| 166 | }; |
| 167 | |
| 168 | /** Issues, pull requests, comments and sessions. */ |
| 169 | export interface WorkApi { |
| 170 | openIssue(actor: User, repo: RepoPath, input: OpenIssueInput): Promise<Result<Issue>>; |
| 171 | /** Newest first. */ |
| 172 | listIssues( |
| 173 | repo: RepoPath, |
| 174 | viewer: Viewer, |
| 175 | filter?: { state?: State; label?: string }, |
| 176 | ): Promise<Result<Issue[]>>; |
| 177 | getIssue(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<IssueDetail>>; |
| 178 | /** The author or a member of the workspace may. */ |
| 179 | updateIssue(actor: User, repo: RepoPath, number: number, input: UpdateIssueInput): Promise<Result<Issue>>; |
| 180 | closeIssue(actor: User, repo: RepoPath, number: number, reason?: IssueReason): Promise<Result<Issue>>; |
| 181 | reopenIssue(actor: User, repo: RepoPath, number: number): Promise<Result<Issue>>; |
| 182 | /** The default labels, then every other label in use on the repository. */ |
| 183 | listLabels(repo: RepoPath, viewer: Viewer): Promise<Result<string[]>>; |
| 184 | /** How many issues and pull requests are open. */ |
| 185 | counts(repo: RepoPath, viewer: Viewer): Promise<Result<{ issues: number; pulls: number }>>; |
| 186 | |
| 187 | /** On an issue or a pull request. */ |
| 188 | addComment(actor: User, repo: RepoPath, number: number, body: string): Promise<Result<Comment>>; |
| 189 | |
| 190 | /** |
| 191 | * Opens a pull request: a draft with a fork to push to, or, given a |
| 192 | * branch, one ready for review. |
| 193 | */ |
| 194 | openPull(actor: User, repo: RepoPath, input: OpenPullInput): Promise<Result<Pull>>; |
| 195 | /** Newest first. */ |
| 196 | listPulls(repo: RepoPath, viewer: Viewer, state?: State): Promise<Result<Pull[]>>; |
| 197 | getPull(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<PullDetail>>; |
| 198 | /** Marks a draft ready for review and sets its description. */ |
| 199 | readyPull(actor: User, repo: RepoPath, number: number, summary: string): Promise<Result<Pull>>; |
| 200 | closePull(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>; |
| 201 | /** |
| 202 | * Lands the pull request on the repository's default branch. Unless |
| 203 | * `keepIssueOpen`, that resolves the issue it was for: the issue closes |
| 204 | * naming this pull request, and the others still in progress for it close |
| 205 | * as superseded. Only members of the repository's workspace may merge. |
| 206 | */ |
| 207 | mergePull(actor: User, repo: RepoPath, number: number, keepIssueOpen?: boolean): Promise<Result<Pull>>; |
| 208 | /** Drafts and open pull requests the viewer started, most recently active first. */ |
| 209 | listActivePulls(viewer: Viewer): Promise<{ pull: Pull; issue: Issue | null }[]>; |
| 210 | |
| 211 | appendSession(actor: User, repo: RepoPath, number: number, entries: NewSessionEntry[]): Promise<Result<{ count: number }>>; |
| 212 | readSession(repo: RepoPath, number: number, viewer: Viewer, afterSeq?: number): Promise<Result<SessionEntry[]>>; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * What to pass `ReposApi.compare` to see what a pull request changes. |
| 217 | * |
| 218 | * A fork is compared as a whole. A branch is compared by name while the |
| 219 | * pull request is open, and by the commit it was merged or closed at |
| 220 | * afterwards, so later pushes to the branch do not change the record. |
| 221 | */ |
| 222 | export function pullComparison(pull: Pull): { |
| 223 | repoId: string; |
| 224 | base: string | null; |
| 225 | head: string | null; |
| 226 | } { |
| 227 | if (pull.forkRepoId) return { repoId: pull.forkRepoId, base: pull.mergeBase, head: null }; |
| 228 | const settled = pull.status === "merged" || pull.status === "closed"; |
| 229 | return { |
| 230 | repoId: pull.repoId, |
| 231 | base: pull.mergeBase, |
| 232 | head: (settled && pull.headCommit) || pull.branch, |
| 233 | }; |
| 234 | } |