Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

3.5 KB
Raw Blame
import type { ReposSummaryResponse } from "@repowise-dev/types/repos";
import { apiGet, apiPost, apiPatch, apiDelete } from "./client";
import type {
  RepoCreate,
  RepoUpdate,
  RepoResponse,
  RepoStatsResponse,
  PreflightResponse,
  GenerateRequest,
  GenerateEstimate,
  JobLaunchResponse,
} from "./types";

export async function listRepos(): Promise<RepoResponse[]> {
  return apiGet<RepoResponse[]>("/api/repos");
}

/** One-call payload for the multi-repo dashboard: every registered repo's
 *  headline figures. Replaces `listRepos` plus a `getRepoStats` and a
 *  `getGitSummary` per repo, whose cost grew with the repository count. */
export async function getReposSummary(): Promise<ReposSummaryResponse> {
  return apiGet<ReposSummaryResponse>("/api/repos/summary");
}

export async function getRepo(repoId: string): Promise<RepoResponse> {
  return apiGet<RepoResponse>(`/api/repos/${repoId}`);
}

export async function createRepo(data: RepoCreate): Promise<RepoResponse> {
  return apiPost<RepoResponse>("/api/repos", data);
}

export async function updateRepo(repoId: string, data: RepoUpdate): Promise<RepoResponse> {
  return apiPatch<RepoResponse>(`/api/repos/${repoId}`, data);
}

/** POST a repo job endpoint that answers 202 with `{job_id, stream_token}`. */
function launchRepoJob(repoId: string, action: string): Promise<JobLaunchResponse> {
  return apiPost<JobLaunchResponse>(`/api/repos/${repoId}/${action}`);
}

export async function syncRepo(repoId: string): Promise<JobLaunchResponse> {
  return launchRepoJob(repoId, "sync");
}

export async function fullResyncRepo(repoId: string): Promise<JobLaunchResponse> {
  return launchRepoJob(repoId, "full-resync");
}

/** Start the first full index (docs included) for a registered repo.
 * Returns 409 when a job is already active for it. */
export async function startIndexJob(repoId: string): Promise<JobLaunchResponse> {
  return launchRepoJob(repoId, "index");
}

/** Provider connectivity smoke test + page/cost estimate for a first index. */
export async function preflightIndex(
  repoId: string,
  coveragePct?: number,
): Promise<PreflightResponse> {
  return apiPost<PreflightResponse>(
    `/api/repos/${repoId}/preflight`,
    undefined,
    undefined,
    coveragePct !== undefined ? { coverage_pct: coveragePct } : undefined,
  );
}

/** Cost + page counts for a generate selection, cascade fallout included.
 *  Heavy (rehydrates the graph and re-parses), so fetch it lazily — never on
 *  every render. `provider` prices a provider the repo does not use yet. */
export async function generateEstimate(
  repoId: string,
  body: GenerateRequest,
  options: { provider?: string } = {},
): Promise<GenerateEstimate> {
  const qs = options.provider ? `?provider=${encodeURIComponent(options.provider)}` : "";
  return apiPost<GenerateEstimate>(`/api/repos/${repoId}/generate/estimate${qs}`, body);
}

/** Launch a scoped generate job (writes the selected pages with a model).
 *  Returns the job id + a short-lived stream token to watch progress. */
export async function generatePages(
  repoId: string,
  body: GenerateRequest,
): Promise<JobLaunchResponse> {
  return apiPost<JobLaunchResponse>(`/api/repos/${repoId}/generate`, body);
}

export async function deleteRepo(repoId: string): Promise<{ ok: boolean; deleted_pages: number }> {
  return apiDelete<{ ok: boolean; deleted_pages: number }>(`/api/repos/${repoId}`);
}

export async function getRepoStats(repoId: string): Promise<RepoStatsResponse> {
  return apiGet<RepoStatsResponse>(`/api/repos/${repoId}/stats`);
}