Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

8.9 KB
Raw Blame
/**
 * The Git tab's one read: a workspace repository's branches with what each is
 * merged into, its pull requests and its CI (`GET /api/agentic-ide/git/overview`).
 *
 * Mirrors `dodaios/agentic_ide/git_overview.py`. Merged, pull request and CI
 * are three separate facts on purpose — passing checks never mean "merged".
 */

import { fill, translate } from "@/i18n";
import type { DiffHunk } from "../explorer/explorerApi";

export type PullRequestState = "draft" | "open" | "queued" | "merged" | "closed";
export type CiState = "none" | "pending" | "running" | "success" | "failure";

export interface CiStatus {
  state: CiState;
  /** The failing / running workflow run, or the pull request's checks page. */
  url: string;
  total: number;
  passed: number;
  failed: number;
  running: number;
  pending: number;
  /** The commit the checks ran on (12 chars). */
  commit: string;
  /** Failed checks, or the running ones while nothing failed. */
  names: string[];
}

export interface PullRequest {
  number: number;
  title: string;
  url: string;
  state: PullRequestState;
  base: string;
  head: string;
  head_oid: string;
  merged_at: string;
  updated_at: string;
  queue_position: number | null;
  ci: CiStatus;
  /** GitHub cannot merge it as it is: it conflicts with its base. */
  conflicting?: boolean;
  /** `approved` | `changes_requested` | `review_required` | "" (no review rule). */
  review?: string;
  author?: string;
  /** Opened from a fork: its branch is not one of this repository's. */
  fork?: boolean;
}

export interface MergedInto {
  target: string;
  /** `git`: its commits are in the target; `pull_request`: GitHub merged its PR. */
  via: "git" | "pull_request";
  number: number | null;
  url: string;
}

export interface BranchRow {
  name: string;
  current: boolean;
  remote_only: boolean;
  upstream: string;
  ahead: number;
  behind: number;
  head: string;
  committed_at: number;
  /** Another checkout this branch is open in. */
  worktree: string;
  on_github: boolean;
  merged_into: MergedInto[];
  /** Most relevant first: queued, open, draft, merged, closed. */
  pull_requests: PullRequest[];
  ci: CiStatus;
  /** Local commits CI has not seen yet. */
  ci_stale: boolean;
  /** The upstream is configured but was deleted on the remote. */
  upstream_gone?: boolean;
  /** GitHub has commits here that were not fetched yet; `behind` undercounts until a fetch. */
  unfetched?: boolean;
  /** Commits it has that the default branch lacks, and the other way round; null when unknown. */
  base_ahead?: number | null;
  base_behind?: number | null;
}

/** The folder's own state: what is uncommitted and how it stands to its remote. */
export interface Checkout {
  branch: string;
  detached: boolean;
  unborn: boolean;
  head: string;
  upstream: string;
  ahead: number;
  behind: number;
  /** Files with any uncommitted change. */
  files: number;
  staged: number;
  unstaged: number;
  untracked: number;
  conflicted: number;
  /** `merge` | `rebase` | `cherry-pick` | `revert` | `bisect` while one is unfinished. */
  operation: string;
  stashes: number;
  /** Epoch seconds of the last fetch; 0 when never. */
  last_fetch: number;
  has_remote: boolean;
}

export interface GitHubState {
  available: boolean;
  reason: string;
  /** The next step the tab offers: connect GitHub, pick the folder's repository, or a failure code. */
  code: "" | "not_connected" | "needs_repo" | string;
  /** The `owner/name` picked for this folder. */
  repo: string;
  /** What the folder's git remote points at — the picker's recommendation. */
  suggested_repo: string;
  /** `app` (Plugins → GitHub) or `gh` (the GitHub CLI). */
  source: string;
  repo_url: string;
  /** Epoch seconds of the GitHub answer in use. */
  fetched_at: number;
  /** A newer answer is being read right now; ask again shortly. */
  refreshing?: boolean;
}

export interface RepoOverview {
  available: boolean;
  reason: string;
  root: string;
  branch: string;
  detached: boolean;
  head: string;
  default_branch: string;
  branches: BranchRow[];
  remote_branches: BranchRow[];
  truncated: boolean;
  github: GitHubState;
  /** Missing from a backend that predates the control center (until the next app start). */
  checkout?: Checkout;
  /** What `base_ahead` / `base_behind` compare with (`origin/main`). */
  base_ref?: string;
  /** Every open pull request, the ones needing a look first. */
  pull_requests?: PullRequest[];
}

/** A failed call; `routeMissing` means the running backend predates this tab (restart once). */
export class GitOverviewError extends Error {
  readonly routeMissing: boolean;

  constructor(message: string, routeMissing = false) {
    super(message);
    this.routeMissing = routeMissing;
  }
}

async function call<T>(url: string, init?: RequestInit): Promise<T> {
  const res = await fetch(url, init);
  if (!res.ok) {
    let message = fill(translate("ide_panes.request_failed"), { status: res.status });
    let detail: unknown;
    try {
      detail = ((await res.json()) as { detail?: unknown }).detail;
      if (typeof detail === "string") message = detail;
    } catch {
      /* keep the status-code message */
    }
    // FastAPI's own 404 for an unknown path says exactly "Not Found"; this
    // API's 404s always explain themselves ("Workspace not found.").
    throw new GitOverviewError(message, res.status === 404 && detail === "Not Found");
  }
  return (await res.json()) as T;
}

export function fetchGitOverview(workspaceId: string, refresh = false): Promise<RepoOverview> {
  const query = new URLSearchParams({ workspace_id: workspaceId });
  if (refresh) query.set("refresh", "true");
  return call<RepoOverview>(`/api/agentic-ide/git/overview?${query.toString()}`);
}

export interface GitHubRepoChoice {
  /** `owner/name`. */
  name: string;
  description: string;
  private: boolean;
  fork: boolean;
  url: string;
  pushed_at: string;
}

export interface GitHubRepoList {
  connected: boolean;
  source: string;
  login: string;
  repos: GitHubRepoChoice[];
  reason: string;
}

export function fetchGitHubRepos(refresh = false): Promise<GitHubRepoList> {
  return call<GitHubRepoList>(`/api/agentic-ide/git/github/repos${refresh ? "?refresh=true" : ""}`);
}

/** Remember which repository this workspace's folder is; `""` forgets it. */
export function bindGitHubRepo(workspaceId: string, repo: string): Promise<{ ok: boolean; repo: string }> {
  return call(`/api/agentic-ide/git/github/binding`, {
    method: "PUT",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ workspace_id: workspaceId, repo }),
  });
}

export interface BranchEditors {
  /** False on a headless host or a remote browser: nothing opens on this screen. */
  file_manager: boolean;
  editors: { id: string; label: string }[];
}

export function fetchBranchEditors(): Promise<BranchEditors> {
  return call<BranchEditors>(`/api/agentic-ide/git/branch/editors`);
}

/** Open the folder `branch` is checked out in, in the file manager (`folder`) or an editor. */
export function openBranchCheckout(workspaceId: string, branch: string, target: string): Promise<{ opened: boolean; path: string }> {
  return call(`/api/agentic-ide/git/branch/open`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ workspace_id: workspaceId, branch, target }),
  });
}

export interface BranchCommit {
  sha: string;
  subject: string;
  author: string;
  committed_at: number;
  /** GitHub has this commit; false while it exists only on this computer. */
  on_github: boolean;
}

export interface BranchFile {
  path: string;
  status: "added" | "deleted" | "modified";
  /** null for a binary file. */
  added: number | null;
  removed: number | null;
}

export interface BranchContents {
  available: boolean;
  branch: string;
  /** What the branch is compared with (`origin/main`); "" for the default branch itself. */
  base: string;
  commits: BranchCommit[];
  commits_truncated: boolean;
  files: BranchFile[];
  files_truncated: boolean;
  reason: string;
}

export interface BranchFileDiff {
  path: string;
  status: string;
  binary: boolean;
  added: number;
  removed: number;
  hunks: DiffHunk[];
  truncated: boolean;
}

function branchQuery(workspaceId: string, branch: string, base: string, remote: boolean, extra: Record<string, string> = {}): string {
  const query = new URLSearchParams({ workspace_id: workspaceId, branch, base, ...extra });
  if (remote) query.set("remote", "true");
  return query.toString();
}

export function fetchBranchContents(workspaceId: string, branch: string, base: string, remote: boolean): Promise<BranchContents> {
  return call<BranchContents>(`/api/agentic-ide/git/branch/contents?${branchQuery(workspaceId, branch, base, remote)}`);
}

export function fetchBranchFileDiff(workspaceId: string, branch: string, base: string, remote: boolean, path: string): Promise<BranchFileDiff> {
  return call<BranchFileDiff>(`/api/agentic-ide/git/branch/diff?${branchQuery(workspaceId, branch, base, remote, { path })}`);
}