Files
opencode-manager/ocm-cli/src/repo-proxy.ts
T

105 lines
3.7 KiB
TypeScript

import { z } from 'zod'
import { parseJsonText } from '@opencode-manager/shared/utils'
import packageJson from '../package.json' with { type: 'json' }
import { delay } from './delay.js'
import { describeCause } from './token-store.js'
export const OCM_VERSION = packageJson.version
export const MIN_MANAGER_VERSION = '0.19.0'
export class ManagerTooOldError extends Error {
constructor(managerUrl: string) {
super(`OpenCode Manager at ${managerUrl} is too old for ocm ${OCM_VERSION}; upgrade the Manager to >= ${MIN_MANAGER_VERSION}`)
this.name = 'ManagerTooOldError'
}
}
export class RepoProxyNotFoundError extends Error {
constructor(managerUrl: string, repoId: number) {
super(`repo ${repoId} is not available on OpenCode Manager at ${managerUrl} (missing or not ready)`)
this.name = 'RepoProxyNotFoundError'
}
}
export class ManagerTokenInvalidError extends Error {
constructor(managerUrl: string) {
super(`OpenCode Manager rejected the token for ${managerUrl} (HTTP 401). The token may have been rotated: copy the current one from Settings -> Manager Internal Token, then run \`ocm login ${managerUrl}\` to store it again.`)
this.name = 'ManagerTokenInvalidError'
}
}
export class RepoProxyResponseError extends Error {
constructor(managerUrl: string, status: number, body: string) {
const detail = body.trim()
super(`OpenCode Manager at ${managerUrl} returned HTTP ${status} from the repo proxy${detail ? `: ${detail}` : ''}`)
this.name = 'RepoProxyResponseError'
}
}
export class RepoProxyUnreachableError extends Error {
constructor(managerUrl: string, cause: unknown) {
super(`could not reach OpenCode Manager at ${managerUrl}: ${describeCause(cause)}`, { cause })
this.name = 'RepoProxyUnreachableError'
}
}
export type RepoProxyFetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>
export type WarmRepoProxyOptions = {
attempts?: number
fetch?: RepoProxyFetch
wait?: (ms: number) => Promise<void>
}
export function repoProxyBaseUrl(managerUrl: string): string {
return `${managerUrl}/api/opencode-proxy`
}
export function repoProxyUrl(managerUrl: string, repoId: number): string {
return `${repoProxyBaseUrl(managerUrl)}/repos/${repoId}`
}
const RepoProxyErrorBodySchema = z.object({ error: z.string().optional() })
function isRepoNotFoundBody(body: string): boolean {
const parsed = RepoProxyErrorBodySchema.safeParse(parseJsonText(body))
return parsed.success && parsed.data.error === 'Repo not found'
}
export async function warmRepoProxy(managerUrl: string, token: string, repoId: number, options: WarmRepoProxyOptions = {}): Promise<void> {
const fetchImpl = options.fetch ?? fetch
const attempts = Math.max(1, options.attempts ?? 3)
const wait = options.wait ?? delay
const url = `${repoProxyUrl(managerUrl, repoId)}/api/session?limit=1`
for (let attempt = 1; attempt <= attempts; attempt++) {
let res: Response | null = null
let networkError: unknown
try {
res = await fetchImpl(url, { headers: { Authorization: `Bearer ${token}` } })
} catch (err) {
networkError = err
}
const isFinalAttempt = attempt === attempts
if (res) {
const body = await res.text().catch(() => '')
if (res.ok) return
if (res.status === 404) {
throw isRepoNotFoundBody(body) ? new RepoProxyNotFoundError(managerUrl, repoId) : new ManagerTooOldError(managerUrl)
}
if (res.status === 401) {
throw new ManagerTokenInvalidError(managerUrl)
}
if (isFinalAttempt) {
throw new RepoProxyResponseError(managerUrl, res.status, body)
}
} else if (isFinalAttempt) {
throw new RepoProxyUnreachableError(managerUrl, networkError)
}
await wait(attempt * 500)
}
}