mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Switch MCP management to use OpenCode server API with version requirement >=1.0.137
This commit is contained in:
1 parent
9608d1ce19
commit
b739c5266f
8 files changed
+547
-138
No files matched your search
@@ -17,7 +17,10 @@ export function createHealthRoutes(db: Database) {
|
||||
timestamp: new Date().toISOString(),
|
||||
database: dbCheck ? 'connected' : 'disconnected',
|
||||
opencode: opencodeHealthy ? 'healthy' : 'unhealthy',
|
||||
opencodePort: opencodeServerManager.getPort()
|
||||
opencodePort: opencodeServerManager.getPort(),
|
||||
opencodeVersion: opencodeServerManager.getVersion(),
|
||||
opencodeMinVersion: opencodeServerManager.getMinVersion(),
|
||||
opencodeVersionSupported: opencodeServerManager.isVersionSupported()
|
||||
})
|
||||
} catch (error) {
|
||||
return c.json({
|
||||
|
||||
@@ -27,25 +27,7 @@ const UpdateOpenCodeConfigSchema = z.object({
|
||||
isDefault: z.boolean().optional(),
|
||||
})
|
||||
|
||||
function hasMcpChanged(oldContent: Record<string, unknown>, newContent: Record<string, unknown>): boolean {
|
||||
const oldMcp = oldContent.mcp as Record<string, any> || {}
|
||||
const newMcp = newContent.mcp as Record<string, any> || {}
|
||||
|
||||
const oldKeys = Object.keys(oldMcp).sort()
|
||||
const newKeys = Object.keys(newMcp).sort()
|
||||
|
||||
if (JSON.stringify(oldKeys) !== JSON.stringify(newKeys)) {
|
||||
return true
|
||||
}
|
||||
|
||||
for (const key of oldKeys) {
|
||||
if (JSON.stringify(oldMcp[key]) !== JSON.stringify(newMcp[key])) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
const CreateCustomCommandSchema = z.object({
|
||||
name: z.string().min(1).max(255),
|
||||
@@ -170,11 +152,6 @@ export function createSettingsRoutes(db: Database) {
|
||||
logger.info(`Wrote default config to: ${configPath}`)
|
||||
|
||||
await patchOpenCodeConfig(config.content)
|
||||
|
||||
if (existingConfig && hasMcpChanged(existingConfig.content, config.content)) {
|
||||
logger.info('MCP configuration changed, restarting OpenCode server')
|
||||
await opencodeServerManager.restart()
|
||||
}
|
||||
}
|
||||
|
||||
return c.json(config)
|
||||
|
||||
@@ -8,13 +8,28 @@ import type { Database } from 'bun:sqlite'
|
||||
const OPENCODE_SERVER_PORT = ENV.OPENCODE.PORT
|
||||
const OPENCODE_SERVER_DIRECTORY = getWorkspacePath()
|
||||
const OPENCODE_CONFIG_PATH = getOpenCodeConfigFilePath()
|
||||
const MIN_OPENCODE_VERSION = '1.0.137'
|
||||
|
||||
function compareVersions(v1: string, v2: string): number {
|
||||
const parts1 = v1.split('.').map(Number)
|
||||
const parts2 = v2.split('.').map(Number)
|
||||
|
||||
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
|
||||
const p1 = parts1[i] || 0
|
||||
const p2 = parts2[i] || 0
|
||||
if (p1 > p2) return 1
|
||||
if (p1 < p2) return -1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
class OpenCodeServerManager {
|
||||
private static instance: OpenCodeServerManager
|
||||
private serverProcess: any = null
|
||||
private serverProcess: ReturnType<typeof spawn> | null = null
|
||||
private serverPid: number | null = null
|
||||
private isHealthy: boolean = false
|
||||
private db: Database | null = null
|
||||
private version: string | null = null
|
||||
|
||||
private constructor() {}
|
||||
|
||||
@@ -105,7 +120,7 @@ class OpenCodeServerManager {
|
||||
}
|
||||
)
|
||||
|
||||
this.serverPid = this.serverProcess.pid
|
||||
this.serverPid = this.serverProcess.pid ?? null
|
||||
|
||||
logger.info(`OpenCode server started with PID ${this.serverPid}`)
|
||||
|
||||
@@ -116,6 +131,15 @@ class OpenCodeServerManager {
|
||||
|
||||
this.isHealthy = true
|
||||
logger.info('OpenCode server is healthy')
|
||||
|
||||
await this.fetchVersion()
|
||||
if (this.version) {
|
||||
logger.info(`OpenCode version: ${this.version}`)
|
||||
if (!this.isVersionSupported()) {
|
||||
logger.warn(`OpenCode version ${this.version} is below minimum required version ${MIN_OPENCODE_VERSION}`)
|
||||
logger.warn('Some features like MCP management may not work correctly')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async stop(): Promise<void> {
|
||||
@@ -152,6 +176,19 @@ class OpenCodeServerManager {
|
||||
return OPENCODE_SERVER_PORT
|
||||
}
|
||||
|
||||
getVersion(): string | null {
|
||||
return this.version
|
||||
}
|
||||
|
||||
getMinVersion(): string {
|
||||
return MIN_OPENCODE_VERSION
|
||||
}
|
||||
|
||||
isVersionSupported(): boolean {
|
||||
if (!this.version) return false
|
||||
return compareVersions(this.version, MIN_OPENCODE_VERSION) >= 0
|
||||
}
|
||||
|
||||
async checkHealth(): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(`http://127.0.0.1:${OPENCODE_SERVER_PORT}/doc`, {
|
||||
@@ -163,6 +200,20 @@ class OpenCodeServerManager {
|
||||
}
|
||||
}
|
||||
|
||||
async fetchVersion(): Promise<string | null> {
|
||||
try {
|
||||
const result = execSync('opencode --version 2>&1', { encoding: 'utf8' })
|
||||
const match = result.match(/(\d+\.\d+\.\d+)/)
|
||||
if (match && match[1]) {
|
||||
this.version = match[1]
|
||||
return this.version
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn('Failed to get OpenCode version:', error)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private async waitForHealth(timeoutMs: number): Promise<boolean> {
|
||||
const start = Date.now()
|
||||
while (Date.now() - start < timeoutMs) {
|
||||
|
||||
Reference in new issue
Block a user