mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
refactor: remove proxy service and add opencode client implementation
This commit is contained in:
1 parent
5abaaa5cc0
commit
568d44832c
24 files changed
+1478
-1030
No files matched your search
@@ -20,18 +20,18 @@ import { getWorkspacePath, getOpenCodeConfigFilePath, ENV } from '@opencode-mana
|
||||
import { parseJsonc } from '@opencode-manager/shared/utils'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import { compareVersions } from '../utils/version-utils'
|
||||
import { patchOpenCodeConfig } from './proxy'
|
||||
import { patchConfigWithRecovery } from './opencode/config-recovery'
|
||||
import type { OpenCodeClient } from './opencode/client'
|
||||
import { writeFileContent } from './file-operations'
|
||||
|
||||
const OPENCODE_SERVER_PORT = ENV.OPENCODE.PORT
|
||||
const OPENCODE_SERVER_HOST = ENV.OPENCODE.HOST
|
||||
const OPENCODE_SERVER_PUBLIC_URL = ENV.OPENCODE.PUBLIC_URL
|
||||
const OPENCODE_SERVER_PASSWORD = ENV.OPENCODE.SERVER_PASSWORD
|
||||
const OPENCODE_SERVER_USERNAME = ENV.OPENCODE.SERVER_USERNAME
|
||||
const OPENCODE_BASIC_AUTH = OPENCODE_SERVER_PASSWORD
|
||||
? `Basic ${Buffer.from(`${OPENCODE_SERVER_USERNAME}:${OPENCODE_SERVER_PASSWORD}`).toString('base64')}`
|
||||
: ''
|
||||
const MIN_OPENCODE_VERSION = '1.0.137'
|
||||
const MAX_STDERR_SIZE = 10240
|
||||
const HEALTH_CHECK_TIMEOUT_MS = 3000
|
||||
|
||||
type StartupValidationIssue = {
|
||||
path: string
|
||||
@@ -101,6 +101,7 @@ class OpenCodeServerManager {
|
||||
private version: string | null = null
|
||||
private lastStartupError: string | null = null
|
||||
private opInProgress: boolean = false
|
||||
private openCodeClient: OpenCodeClient | null = null
|
||||
|
||||
private constructor() {}
|
||||
|
||||
@@ -108,6 +109,17 @@ class OpenCodeServerManager {
|
||||
this.db = db
|
||||
}
|
||||
|
||||
setOpenCodeClient(client: OpenCodeClient) {
|
||||
this.openCodeClient = client
|
||||
}
|
||||
|
||||
private requireClient(): OpenCodeClient {
|
||||
if (!this.openCodeClient) {
|
||||
throw new Error('OpenCodeClient not configured on OpenCodeServerManager. Call setOpenCodeClient() during startup.')
|
||||
}
|
||||
return this.openCodeClient
|
||||
}
|
||||
|
||||
static getInstance(): OpenCodeServerManager {
|
||||
if (!OpenCodeServerManager.instance) {
|
||||
OpenCodeServerManager.instance = new OpenCodeServerManager()
|
||||
@@ -480,10 +492,10 @@ class OpenCodeServerManager {
|
||||
try {
|
||||
const configPath = getOpenCodeConfigFilePath()
|
||||
const fileContent = await fs.readFile(configPath, 'utf-8')
|
||||
const fileConfig = JSON.parse(fileContent) as Record<string, unknown>
|
||||
const fileConfig = parseJsonc(fileContent) as Record<string, unknown>
|
||||
logger.info(`Read config from file for reload: ${configPath}`)
|
||||
|
||||
const patchResult = await patchOpenCodeConfig(fileConfig)
|
||||
const patchResult = await patchConfigWithRecovery(this.requireClient(), fileConfig)
|
||||
if (!patchResult.success) {
|
||||
const errorMessage = patchResult.error || 'Failed to reload config'
|
||||
const validationIssues = patchResult.details || []
|
||||
@@ -498,6 +510,11 @@ class OpenCodeServerManager {
|
||||
throw new ConfigReloadError(errorMessage, validationIssues, removedFields)
|
||||
}
|
||||
|
||||
if (patchResult.removedFields && patchResult.removedFields.length > 0 && patchResult.appliedConfig) {
|
||||
await writeFileContent(configPath, JSON.stringify(patchResult.appliedConfig, null, 2))
|
||||
logger.info(`Persisted cleaned config to ${configPath} after removing fields: ${patchResult.removedFields.join(', ')}`)
|
||||
}
|
||||
|
||||
logger.info('OpenCode configuration reloaded successfully')
|
||||
await new Promise(r => setTimeout(r, 500))
|
||||
const healthy = await this.checkHealth()
|
||||
@@ -544,14 +561,14 @@ class OpenCodeServerManager {
|
||||
}
|
||||
|
||||
async checkHealth(): Promise<boolean> {
|
||||
if (!this.openCodeClient) {
|
||||
return false
|
||||
}
|
||||
try {
|
||||
const headers: Record<string, string> = {}
|
||||
if (OPENCODE_BASIC_AUTH) {
|
||||
headers.Authorization = OPENCODE_BASIC_AUTH
|
||||
}
|
||||
const response = await fetch(`http://${OPENCODE_SERVER_HOST}:${OPENCODE_SERVER_PORT}/doc`, {
|
||||
signal: AbortSignal.timeout(3000),
|
||||
headers
|
||||
const response = await this.openCodeClient.forward({
|
||||
method: 'GET',
|
||||
path: '/doc',
|
||||
signal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS),
|
||||
})
|
||||
return response.ok
|
||||
} catch {
|
||||
|
||||
Reference in new issue
Block a user