Files
opencode-manager/frontend/src/api/repos.ts
T
Chris Scott dacde879d6 feat(opencode): migrate OpenCode Manager to OpenCode 2 (#359)
* feat(opencode): migrate OpenCode Manager to OpenCode 2

Port the backend lifecycle, generated plugins, event aggregation,
schedules, worktrees, provider credentials, MCP, config handling, the
frontend chat model, and the ocm CLI to OpenCode 2.

Adopt @opencode/client 2.0.15 as the single typed V2 API contract,
require OpenCode 2.0.0 or newer (pinned 2.0.15), and delete every
V1-only code path.

* refactor(opencode): reload config in place and drop v1 compatibility shims

Route config/skill/AGENTS.md changes through the OpenCode location reload
API instead of a full server restart, stop auto-resuming interrupted
sessions, remove the restart coordinator and v1 MCP/version shims, and pin
support to OpenCode >=2.0.15 <3.0.0. Adds integration-ready retries,
service-mode launch, ocm-cli repo proxy, and new frontend form/MCP helpers.

* refactor(opencode): consolidate v2 migration helpers and harden install, proxy, and session flows

- extract shared semver and model-ref utilities, reuse across version checks
- share the OpenCode release download script across Docker, entrypoint, dev setup
- unify install/upgrade failure recovery and guard externally managed OPENCODE_BIN
- scope the repo proxy location rewrite and surface ocm proxy errors
- rework frontend session projection, permission lookup, and cache invalidation

* fix(schedules): reject unavailable agents before creating a session

* feat(opencode): add ocm install, schedule agent fallback, and session move guards
2026-09-26 13:16:45 -04:00

199 lines
6.0 KiB
TypeScript

import type { Repo } from './types'
import { FetchError, fetchWrapper, fetchWrapperVoid, fetchWrapperBlob } from './fetchWrapper'
import { API_BASE_URL } from '@/config'
import { saveFile } from '@/lib/download'
import type { DiscoverReposResponse, AssistantModeStatus, AssistantModeInitRequest } from '@opencode-manager/shared/types'
export interface CreateRepoOptions {
repoUrl?: string
localPath?: string
branch?: string
directoryName?: string
useWorktree?: boolean
skipSSHVerification?: boolean
baseBranch?: string
}
export async function createRepo(options: CreateRepoOptions = {}): Promise<Repo> {
return fetchWrapper(`${API_BASE_URL}/api/repos`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options),
})
}
export async function listRepos(): Promise<Repo[]> {
return fetchWrapper(`${API_BASE_URL}/api/repos`)
}
export async function discoverRepos(rootPath: string, maxDepth?: number): Promise<DiscoverReposResponse> {
return fetchWrapper(`${API_BASE_URL}/api/repos/discover`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ rootPath, maxDepth }),
})
}
export async function getRepo(id: number): Promise<Repo> {
return fetchWrapper(`${API_BASE_URL}/api/repos/${id}`)
}
export type RepoSibling = Repo & {
currentBranch?: string
worktreeStrategy?: string
}
export function workspaceLabel(workspace: RepoSibling): string {
return workspace.currentBranch || workspace.branch || workspace.localPath || 'workspace'
}
export interface RepoWorktree {
directory: string
}
export async function getRepoSiblings(id: number): Promise<RepoSibling[]> {
return fetchWrapper(`${API_BASE_URL}/api/repos/${id}/siblings`)
}
export async function deleteRepoWorkspace(repoId: number, directory: string): Promise<void> {
return fetchWrapperVoid(`${API_BASE_URL}/api/repos/${repoId}/workspaces`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ directory }),
})
}
export async function createRepoWorkspace(repoId: number): Promise<RepoWorktree> {
return fetchWrapper(`${API_BASE_URL}/api/repos/${repoId}/workspaces`, {
method: 'POST',
})
}
export async function deleteRepo(id: number): Promise<void> {
return fetchWrapperVoid(`${API_BASE_URL}/api/repos/${id}`, {
method: 'DELETE',
})
}
export async function updateRepoGitCredential(id: number, credentialId?: string): Promise<Repo> {
return fetchWrapper(`${API_BASE_URL}/api/repos/${id}/git-credential`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ credentialId }),
})
}
export async function renameRepo(id: number, name: string | null): Promise<Repo> {
return fetchWrapper(`${API_BASE_URL}/api/repos/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: name ?? '' }),
})
}
export class GitAuthError extends Error {
code: string
constructor(message: string, code: string) {
super(message)
this.name = 'GitAuthError'
this.code = code
}
}
export async function switchBranch(id: number, branch: string): Promise<Repo> {
try {
return await fetchWrapper(`${API_BASE_URL}/api/repos/${id}/branch/switch`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ branch }),
})
} catch (error) {
if (error instanceof FetchError && error.code === 'AUTH_FAILED') {
throw new GitAuthError(error.message, error.code)
}
throw error
}
}
interface GitBranch {
name: string
type: 'local' | 'remote'
current: boolean
upstream?: string
ahead?: number
behind?: number
isWorktree?: boolean
}
export async function listBranches(id: number): Promise<{ branches: GitBranch[], status: { ahead: number, behind: number } }> {
return fetchWrapper(`${API_BASE_URL}/api/repos/${id}/git/branches`)
}
export async function createBranch(id: number, branch: string): Promise<Repo> {
try {
return await fetchWrapper(`${API_BASE_URL}/api/repos/${id}/branch/create`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ branch }),
})
} catch (error) {
if (error instanceof FetchError && error.code === 'AUTH_FAILED') {
throw new GitAuthError(error.message, error.code)
}
throw error
}
}
export interface DownloadOptions {
includeGit?: boolean
includePaths?: string[]
}
export async function downloadRepo(id: number, repoName: string, options?: DownloadOptions): Promise<void> {
const params = new URLSearchParams()
if (options?.includeGit) params.append('includeGit', 'true')
if (options?.includePaths?.length) params.append('includePaths', options.includePaths.join(','))
const url = `${API_BASE_URL}/api/repos/${id}/download${params.toString() ? '?' + params.toString() : ''}`
const blob = await fetchWrapperBlob(url)
await saveFile(blob, `${repoName}.zip`)
}
export async function updateRepoOrder(order: number[]): Promise<void> {
return fetchWrapperVoid(`${API_BASE_URL}/api/repos/order`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ order }),
})
}
export async function resetRepoPermissions(id: number): Promise<void> {
return fetchWrapperVoid(`${API_BASE_URL}/api/repos/${id}/reset-permissions`, {
method: 'POST',
})
}
export async function touchRepoActivity(id: number): Promise<void> {
return fetchWrapperVoid(`${API_BASE_URL}/api/repos/${id}/access`, {
method: 'POST',
})
}
export async function getAssistantModeStatus(id: number): Promise<AssistantModeStatus> {
return fetchWrapper(`${API_BASE_URL}/api/repos/${id}/assistant-mode`, {
method: 'GET',
})
}
export async function initializeAssistantMode(
id: number,
options?: AssistantModeInitRequest
): Promise<AssistantModeStatus> {
return fetchWrapper(`${API_BASE_URL}/api/repos/${id}/assistant-mode`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(options ?? {}),
})
}