Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,31 @@ export namespace Config {
for (const [key, value] of Object.entries(auth)) {
if (value.type === "wellknown") {
process.env[value.key] = value.token
log.debug("fetching remote config", { url: `${key}/.well-known/opencode` })
const response = await fetch(`${key}/.well-known/opencode`)
if (!response.ok) {
throw new Error(`failed to fetch remote config from ${key}: ${response.status}`)
const url = `${key}/.well-known/opencode`
const cached = path.join(Global.Path.cache, "wellknown", key.replaceAll(/[^a-zA-Z0-9.-]/g, "_") + ".json")
log.debug("fetching remote config", { url })
let wellknown: any
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const response = await fetch(url, { signal: AbortSignal.timeout(10_000) })
if (!response.ok) throw new Error(`${response.status}`)
wellknown = await response.json()
await Filesystem.writeJson(cached, wellknown)
} catch (e) {
log.warn("failed to fetch remote config, trying cache", { url, error: e })
wellknown = await Filesystem.readJson(cached).catch(() => undefined)
if (!wellknown) {
log.warn("no cached remote config available", { url })
continue
}
log.debug("using cached remote config", { url })
}
const wellknown = (await response.json()) as any
const remoteConfig = wellknown.config ?? {}
// Add $schema to prevent load() from trying to write back to a non-existent file
if (!remoteConfig.$schema) remoteConfig.$schema = "https://opencode.ai/config.json"
result = merge(
result,
await load(JSON.stringify(remoteConfig), {
dir: path.dirname(`${key}/.well-known/opencode`),
source: `${key}/.well-known/opencode`,
dir: path.dirname(url),
source: url,
}),
)
log.debug("loaded remote config from well-known", { url: key })
Expand Down
Loading