Sfoglia il codice sorgente

support OPENCODE_CONFIG_CONTENT

Dax Raad 5 mesi fa
parent
commit
8952b3d246

+ 5 - 0
packages/opencode/src/config/config.ts

@@ -34,6 +34,11 @@ export namespace Config {
       log.debug("loaded custom config", { path: Flag.OPENCODE_CONFIG })
     }
 
+    if (Flag.OPENCODE_CONFIG_CONTENT) {
+      result = mergeDeep(result, JSON.parse(Flag.OPENCODE_CONFIG_CONTENT))
+      log.debug("loaded custom config from OPENCODE_CONFIG_CONTENT")
+    }
+
     for (const [key, value] of Object.entries(auth)) {
       if (value.type === "wellknown") {
         process.env[value.key] = value.token

+ 1 - 0
packages/opencode/src/flag/flag.ts

@@ -2,6 +2,7 @@ export namespace Flag {
   export const OPENCODE_AUTO_SHARE = truthy("OPENCODE_AUTO_SHARE")
   export const OPENCODE_DISABLE_WATCHER = truthy("OPENCODE_DISABLE_WATCHER")
   export const OPENCODE_CONFIG = process.env["OPENCODE_CONFIG"]
+  export const OPENCODE_CONFIG_CONTENT = process.env["OPENCODE_CONFIG_CONTENT"]
   export const OPENCODE_DISABLE_AUTOUPDATE = truthy("OPENCODE_DISABLE_AUTOUPDATE")
   export const OPENCODE_PERMISSION = process.env["OPENCODE_PERMISSION"]
   export const OPENCODE_DISABLE_DEFAULT_PLUGINS = truthy("OPENCODE_DISABLE_DEFAULT_PLUGINS")

+ 16 - 10
packages/sdk/js/src/server.ts

@@ -1,30 +1,36 @@
 import { spawn } from "node:child_process"
+import { Config } from "./gen/types.gen.js"
 
-export type ServerConfig = {
+export type ServerOptions = {
   hostname?: string
   port?: number
   signal?: AbortSignal
   timeout?: number
+  config?: Config
 }
 
-export async function createOpencodeServer(config?: ServerConfig) {
-  config = Object.assign(
+export async function createOpencodeServer(options?: ServerOptions) {
+  options = Object.assign(
     {
       hostname: "127.0.0.1",
       port: 4096,
       timeout: 5000,
     },
-    config ?? {},
+    options ?? {},
   )
 
-  const proc = spawn(`opencode`, [`serve`, `--hostname=${config.hostname}`, `--port=${config.port}`], {
-    signal: config.signal,
+  const proc = spawn(`opencode`, [`serve`, `--hostname=${options.hostname}`, `--port=${options.port}`], {
+    signal: options.signal,
+    env: {
+      ...process.env,
+      OPENCODE_CONFIG_CONTENT: JSON.stringify(options.config ?? {}),
+    },
   })
 
   const url = await new Promise<string>((resolve, reject) => {
     const id = setTimeout(() => {
-      reject(new Error(`Timeout waiting for server to start after ${config.timeout}ms`))
-    }, config.timeout)
+      reject(new Error(`Timeout waiting for server to start after ${options.timeout}ms`))
+    }, options.timeout)
     let output = ""
     proc.stdout?.on("data", (chunk) => {
       output += chunk.toString()
@@ -56,8 +62,8 @@ export async function createOpencodeServer(config?: ServerConfig) {
       clearTimeout(id)
       reject(error)
     })
-    if (config.signal) {
-      config.signal.addEventListener("abort", () => {
+    if (options.signal) {
+      options.signal.addEventListener("abort", () => {
         clearTimeout(id)
         reject(new Error("Aborted"))
       })