import { keepPreviousData, useInfiniteQuery, useQuery as useTanstackQuery } from "@tanstack/react-query"; import { createContext, useCallback, useContext } from "react"; import { topicsFor } from "./topics"; import type { CardEdit, Preapproval, NextPage, ReleaseAction, DecisionKind, AccessOverview, McpTokenCreated, McpTokens, GithubAppStatus, GithubSetupLink, AccountInput, AgentRunDetail, ActivityPage, ActivityQuery, ItemTrace, Now, NowFeed, ItemTraceDetail, ItemTraceQuery, ItemTraceUnchanged, AgentRunFilter, AgentRunPage, ClaudeAccount, UsagePause, JevStatus, Rates, AnswerInput, AuditFilter, AuditPage, NeedsPage, Contributor, ContributorFilter, ContributorList, ClassifierReport, CostReport, Decision, RepoAreas, Flow, DecisionComment, DecisionFilter, DecisionList, DisagreementFilter, DisagreementList, Item, ItemFilter, ItemList, OverrideInput, MainCi, Progress, PromptsReport, BatchFilter, BatchDetail, BatchList, QueueFilter, QueuePage, QueuePageFilter, QueueList, WorkFilter, WorkList, RepoName, Role, RulePreview, SearchHit, SimilarHit, TrackedRepo, FindingAction, FindingView, ScanView, SecurityOverview, User, WhitelistRule, Job, JobDetail, JobFilter, JobList, JobParams, PipelineOverview, PipelineState, StageLimit, BuildEnv, BuildSummary, BuildTask, BuildTaskDetail, EnvCreate, EnvInput, TaskFilter, TaskPage, AreaDef, AreaEditBody, AreaStep, Conversation, ConversationCard, } from "./types"; /** The dashboard API as the frontend sees it. The mock and the HTTP client both implement it. */ export interface Api { me(): Promise; repos(): Promise; /** Owners of the visible repos that are not storytold's, by repo name. */ repoOwners(): Promise>; decisions: { list(filter: DecisionFilter): Promise; get(id: string): Promise; /** The decision a readable link opens: the item's first open one, else its latest; null when it has none. */ forItem(repo: RepoName, number: number, kind?: DecisionKind): Promise; answer(id: string, input: AnswerInput): Promise; undo(id: string): Promise; /** Operator: runs the GitHub write of a waiting or failed answer again. */ retry(id: string): Promise; /** Operator: clear a Backlog answer so the decision is open again. */ reopen(id: string): Promise; /** Operator: snooze (`on`) or wake an open decision. */ snooze(id: string, on: boolean): Promise; /** The feedback box: an optional pick (replaces the user's live one) and free text, at least one. Text alone joins the thread. */ feedback(id: string, input: { option?: string | null; text?: string }): Promise; retract(id: string): Promise; /** Operator only: answer with the opinion's option. */ adopt(id: string, opinionId: string, textsHash?: string): Promise; claim(id: string): Promise; thread(id: string): Promise; }; /** Opus conversations, one per item. Writes are for operators; a message is refused while Opus has not answered the last one. */ conversations: { get(repo: RepoName, number: number): Promise; /** `ask: false` posts the message without asking Opus (no reply is expected). */ say(repo: RepoName, number: number, text: string, ask?: boolean): Promise; /** Includes (`on`) or excludes a maintainer post for Opus. Removing an include that was already sent is refused. */ include(repo: RepoName, number: number, source: string, on: boolean): Promise; /** A proposed card's editable drafts: the comment `text`, the open-issue step's title and body, the fix-up step's instructions. Fields left out stay as they are. */ editCard(id: number, edit: CardEdit): Promise; /** `skip`: indexes into `steps` the operator removed; the rest run in order. `note`: kept with the choice (on the decision it settles, in the thread), never posted to GitHub; at most 500 characters. */ doCard(id: number, opts?: { skip?: number[]; note?: string }): Promise; dismissCard(id: number): Promise; /** Operator only: stops the item's running pre-approved merge (state becomes "cancelled"); the PR's open question, if any, stays. */ cancelPreapproval(repo: RepoName, number: number): Promise; }; /** A repo's area list. Reading is for everyone who sees the repo; every write is operator only. */ areas: { get(repo: RepoName): Promise; /** Each edit saves a new list version and returns the repo's areas. Adding or removing an area also queues a re-scan. */ edit(repo: RepoName, edit: AreaEditBody): Promise; /** Queues the model step; returns the job id. */ run(repo: RepoName, step: AreaStep): Promise<{ jobId: number }>; /** A "propose" proposal: accepts the stored list or the edited `areas`. A "suggest" proposal: accepts the named areas and ignores the others in `ignore`. Starts a re-scan when a version was saved. */ accept(id: number, body?: { areas?: AreaDef[]; accept?: string[]; ignore?: string[] }): Promise; dismiss(id: number): Promise; }; /** Health problems, everything in progress and the last things that happened, for the home page. */ now(): Promise; /** The next page of "Just happened", older than `before` (a `nextCursor`). */ nowFeed(before: string): Promise; item(repo: RepoName, number: number): Promise; /** * Every event of the item, oldest first, with the header (now, next, who). `since` is the `stamp` of an earlier answer: * when nothing in the header or the first page changed, the answer is `{ unchanged: true, stamp }`. */ itemTrace(repo: RepoName, number: number, q?: ItemTraceQuery): Promise; /** What the list leaves out of an event whose kind has no detail endpoint of its own (plan, job, review, check, step, write). */ itemTraceDetail(repo: RepoName, number: number, id: string): Promise; /** Every item's trace events in one stream, newest first; `since` as for `itemTrace`. */ activity(q?: ActivityQuery): Promise; /** Operator only: review the current head again, plan Verify again, or try the issue fix again. Writes nothing to GitHub. */ release(repo: RepoName, number: number, action: ReleaseAction): Promise; /** Top bar: jumps by number, logins, words in titles and bodies. */ search(q: string): Promise; /** The Items browser: every tracked repo, full-text search over titles, bodies and comments. */ items(filter: ItemFilter): Promise; progress(): Promise; /** CI on the default branch of every synced repo the viewer may see. */ mainCi(): Promise; /** Anyone signed in reads; writes and the preview are for operators. Override notes come only to operators. */ contributors: { list(filter: ContributorFilter): Promise; previewRule(rule: WhitelistRule): Promise; setRule(rule: WhitelistRule): Promise; override(login: string, input: OverrideInput): Promise; }; /** Security deep scan. Reading is for operators and maintainers (viewers get 404); the writes are operator only and may answer with the server's error message. */ security: { overview(repo: RepoName): Promise; startScan(repo: RepoName, capUsd: number | null): Promise; stopScan(id: number): Promise; finding(repo: RepoName, number: number): Promise; act(repo: RepoName, number: number, action: FindingAction, reason?: string): Promise; }; /** Operator only: every org repo and whether it is tracked. */ repoAdmin: { list(): Promise; track(name: RepoName): Promise; untrack(name: RepoName): Promise; /** Lets the Label stage write labels to this repo on GitHub, or stops it. */ approveLabels(name: RepoName, approved: boolean): Promise; /** Lets the pipeline merge pull requests in this repo, or stops it. */ approveMerging(name: RepoName, approved: boolean): Promise; /** Most ready pull requests of a repo that merge one at a time instead of in a batch; 0 always batches. */ singleMerge(): Promise<{ max: number }>; setSingleMerge(max: number): Promise<{ max: number }>; /** Lets the Fix-up step push commits to contributor branches here, or stops it. */ approvePushing(name: RepoName, approved: boolean): Promise; /** Lets the Act step comment on and close items here, or stops it. */ approveCommenting(name: RepoName, approved: boolean): Promise; /** Lets the Fix issues step push fixes, open pull requests and comment on issues here, or stops it. */ approveFixing(name: RepoName, approved: boolean): Promise; /** Lets the Fix red main step rerun failed jobs on a red default branch here and open an issue on an operator's answer, or stops it. */ approveMainFixing(name: RepoName, approved: boolean): Promise; /** Lets the CI-approve step approve the CI runs GitHub holds for reviewed outside pull requests here, or stops it. */ approveCiRuns(name: RepoName, approved: boolean): Promise; setOptionalCheck(name: RepoName, check: string, on: boolean): Promise; /** Switches the pipeline stages on or off for this repo (off: sync only). */ setPipeline(name: RepoName, on: boolean): Promise; /** Sets who makes the pipeline's GitHub writes here; `app` needs the GitHub App registered and installed on the repo. */ setIdentity(name: RepoName, identity: "legacy" | "app"): Promise; }; /** The signed-in user's own tokens for the Inbox MCP server: maintainers and operators make them; anyone lists and revokes their own. */ mcp: { tokens(): Promise; /** `answer`: operators only; the token's agent may also answer decisions, and it lasts 7 days. */ create(label: string, answer?: boolean): Promise; revoke(id: number): Promise; }; /** Operator only. */ access: { overview(): Promise; add(input: { login: string; role: Role; note?: string }): Promise; remove(login: string): Promise; setRole(login: string, role: Role): Promise; revokeSession(id: number): Promise; revokeToken(id: number): Promise; audit(filter: AuditFilter): Promise; }; /** Operator only. */ githubApp: { status(): Promise; /** The link's URL is in this answer only; it is a path when the server has no base URL. */ createLink(org: string): Promise<{ link: GithubSetupLink; url: string }>; revokeLink(id: string): Promise<{ links: GithubSetupLink[] }>; /** Clears the app's keys; `settingsUrl` is where to delete the app on GitHub. */ remove(): Promise<{ settingsUrl: string }>; }; classifiers: { /** `run`, `a` and `b` default to the latest run and its first pair. */ report(sel: { run?: string; a?: string; b?: string }): Promise; disagreements(filter: DisagreementFilter): Promise; }; /** Token use and spend for one run, or all runs when `run` is empty. */ cost(run?: string): Promise; /** The merge queue: every PR with a take or take-fix review at its head, and what it waits on. */ queue(filter: QueueFilter): Promise; /** Per pipeline step, the items its next job takes, in order, and what each step waits on. */ next(): Promise; /** What waits on a person: counts for the tabs, feedback nobody answered yet (newest first, paged by cursor) and stopped stages. */ needs(filter: { feedback?: boolean; cursor?: string }): Promise; /** Integration batches of the merge queue, newest first, with their member PRs. */ queueBatches(filter: BatchFilter): Promise; /** One integration batch with its members, CI and history. */ batch(id: number): Promise; /** Steps, jobs, the on/off switch and the stage limits: everyone reads; run, cancel, switch and limits are for operators. */ pipeline: { overview(): Promise; jobs(filter: JobFilter): Promise; job(id: number): Promise; run(step: string, params: JobParams): Promise; cancel(id: number): Promise; state(): Promise; setEnabled(enabled: boolean): Promise; setLimit(step: string, limit: { perDay: number | null }): Promise; /** Counts the stage's items today from zero again; `all` resets every stage. */ resetLimit(step: string): Promise; /** Clears a stage the circuit breaker paused. */ resumeTrip(stage: string): Promise; /** Operator only: the model of the prepare step, one of `judgementModels`. */ setJudgementModel(model: string): Promise; /** Limits the whole pipeline to these items ("repo#number"); an empty list clears the pilot. */ setPilot(items: string[]): Promise; }; /** The pipeline as a flowchart: every stage with its branches and counts, for every tracked repo or one. */ flow(repo?: RepoName): Promise; /** Enabled accounts for everyone; operators also see disabled ones and edit label, enabled and max concurrent. Never an email or path. */ accounts: { list(): Promise; pause(): Promise; jev(): Promise; /** Operators: switches Jev on or off. */ setJev(input: { enabled: boolean }): Promise; /** Operators. */ setPause(input: UsagePause): Promise; update(id: string, input: AccountInput): Promise; /** Operators: a new, disabled, signed-out account with this label. */ add(label: string): Promise; /** Starts a sign-in; the answer's `login.url` is the page to open. */ loginStart(id: string): Promise; /** Sends the code the sign-in page showed. */ loginCode(id: string, code: string): Promise; loginCancel(id: string): Promise; /** Starts a request for a long-lived token; the answer's `login.url` is the page to open. */ tokenStart(id: string): Promise; /** Sends the code the token page showed; the token is stored on the server and never returned. */ tokenCode(id: string, code: string): Promise; tokenCancel(id: string): Promise; /** Deletes the account's long-lived token. */ tokenRemove(id: string): Promise; signOut(id: string): Promise; /** Reads sign-in state and plan usage now. */ refresh(id: string): Promise; }; /** Agent runs (one model call each): keyset-paged, filtered on the server. */ agents: { list(filter: AgentRunFilter): Promise; get(id: number): Promise; }; /** Build environments (workers that run untrusted code) and their tasks. Everyone reads; operators write. A worker key is returned once. */ envs: { list(): Promise; create(input: EnvCreate): Promise<{ env: BuildEnv; key: string }>; update(id: string, input: EnvInput): Promise; rotateKey(id: string): Promise<{ key: string }>; }; tasks: { list(filter: TaskFilter): Promise; get(id: number): Promise; cancel(id: number): Promise; /** Queued and running tasks and free slots, for the flowchart's Verify node. */ summary(): Promise; }; /** Items moved per stage and account use over time. */ rates(window: "24h" | "7d"): Promise; /** Every registered model prompt, with the server's commit. */ prompts(): Promise; signOut(): Promise; /** Called after any write so open views can refetch. Returns an unsubscribe function. */ subscribe(fn: () => void): () => void; /** Nearest items by embedding across the tracked repos, best first, leaving out the item itself. */ similar(repo: RepoName, number: number, limit?: number): Promise; /** Search by meaning: the query's embedding against every item's. */ semanticSearch(q: string): Promise; /** Open items in work-score order with the parts of each score and the next pipeline step. Read-only. */ work(filter: WorkFilter): Promise; /** Repos whose pipeline stages are on. */ pipelineRepos(): Promise; /** Fast-tracked items as `repo#number`, first to run first. */ fastTrack(): Promise; /** PRs whose merge an operator holds, as `repo#number`. */ heldMerges(): Promise; /** One tab of the Queue page over a repo filter. */ queuePage(filter: QueuePageFilter): Promise; /** Operator controls of the Queue page; items are `repo#number`. */ queueControl: { fastTrackAdd(item: string): Promise; fastTrackRemove(item: string): Promise; hold(item: string): Promise; release(item: string): Promise; batchRemove(item: string): Promise; stopBatch(id: number): Promise; }; } export const ApiContext = createContext(null); export function useApi(): Api { const api = useContext(ApiContext); if (!api) throw new Error("ApiContext missing"); return api; } export interface Query { data?: T; error?: Error; loading: boolean; refetch: () => void; } /** * A server view in the shared cache. `key` identifies the view (it must contain every input of `fn`); * the topics it reads come from api/topics.ts, and the server's change events refetch it. The * previous data stays while a refetch is in flight so lists don't flash. `pollMs` is for views that * change without a database write (a log file being written); it runs only while the tab is visible. */ export function useQuery(key: string, fn: (api: Api) => Promise, opts: { pollMs?: number | false | ((data: T | undefined) => number | false); enabled?: boolean } = {}): Query { const api = useApi(); const q = useTanstackQuery({ queryKey: [key], queryFn: () => fn(api), meta: { topics: topicsFor(key) }, placeholderData: keepPreviousData, enabled: opts.enabled ?? true, refetchInterval: typeof opts.pollMs === "function" ? (query) => (opts.pollMs as (d: T | undefined) => number | false)(query.state.data) : opts.pollMs || false, }); const { refetch } = q; const again = useCallback(() => void refetch(), [refetch]); // A pushed refetch replaces the data in place; only a first load or a new key (data shown is the old key's) counts as loading. return { data: q.data, error: q.error ?? undefined, loading: q.isPending || q.isPlaceholderData, refetch: again }; } export interface PagedQuery

{ /** Every loaded page, first to last; undefined until the first one arrives. */ pages?: P[]; error?: Error; /** True while the first page loads, or while a new key's pages load over the old key's; pushed refetches don't count. */ loading: boolean; hasMore: boolean; loadingMore: boolean; loadMore: () => void; refetch: () => void; } /** * A cursor-paged list in the shared cache. A change event refetches every loaded page in order, so * "Show more" keeps its place. `next` gives the cursor of the page after `page`, or null on the last one. */ export function usePagedQuery

(key: string, fn: (api: Api, cursor: string | undefined) => Promise

, next: (page: P) => string | null | undefined): PagedQuery

{ const api = useApi(); const q = useInfiniteQuery({ queryKey: [key], queryFn: ({ pageParam }) => fn(api, pageParam), initialPageParam: undefined, getNextPageParam: (last) => next(last) ?? undefined, meta: { topics: topicsFor(key) }, placeholderData: keepPreviousData, }); const { refetch, fetchNextPage } = q; return { pages: q.data?.pages, error: q.error ?? undefined, loading: q.isPending || q.isPlaceholderData, hasMore: !!q.hasNextPage, loadingMore: q.isFetchingNextPage, loadMore: useCallback(() => void fetchNextPage(), [fetchNextPage]), refetch: useCallback(() => void refetch(), [refetch]), }; }