rework config

This commit is contained in:
Dax Raad
2025-06-18 22:20:03 -04:00
parent 1e8a681de9
commit e5e9b3e3c0
14 changed files with 785 additions and 203 deletions

View File

@@ -4,11 +4,116 @@
"$schema": {
"type": "string"
},
"theme": {
"type": "string"
},
"keybinds": {
"type": "object",
"properties": {
"leader": {
"type": "string"
},
"help": {
"type": "string"
},
"editor_open": {
"type": "string"
},
"session_new": {
"type": "string"
},
"session_list": {
"type": "string"
},
"session_share": {
"type": "string"
},
"session_interrupt": {
"type": "string"
},
"session_compact": {
"type": "string"
},
"tool_details": {
"type": "string"
},
"model_list": {
"type": "string"
},
"theme_list": {
"type": "string"
},
"project_init": {
"type": "string"
},
"input_clear": {
"type": "string"
},
"input_paste": {
"type": "string"
},
"input_submit": {
"type": "string"
},
"input_newline": {
"type": "string"
},
"history_previous": {
"type": "string"
},
"history_next": {
"type": "string"
},
"messages_page_up": {
"type": "string"
},
"messages_page_down": {
"type": "string"
},
"messages_half_page_up": {
"type": "string"
},
"messages_half_page_down": {
"type": "string"
},
"messages_previous": {
"type": "string"
},
"messages_next": {
"type": "string"
},
"messages_first": {
"type": "string"
},
"messages_last": {
"type": "string"
},
"app_exit": {
"type": "string"
}
},
"additionalProperties": false
},
"autoshare": {
"type": "boolean"
},
"autoupdate": {
"type": "boolean"
},
"disabled_providers": {
"type": "array",
"items": {
"type": "string"
}
},
"provider": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"api": {
"type": "string"
},
"name": {
"type": "string"
},
@@ -50,18 +155,16 @@
"output": {
"type": "number"
},
"inputCached": {
"cache_read": {
"type": "number"
},
"outputCached": {
"cache_write": {
"type": "number"
}
},
"required": [
"input",
"output",
"inputCached",
"outputCached"
"output"
],
"additionalProperties": false
},

View File

@@ -7,8 +7,8 @@ import { Share } from "../../share/share"
import { Message } from "../../session/message"
import { UI } from "../ui"
import { cmd } from "./cmd"
import { GlobalConfig } from "../../global/config"
import { Flag } from "../../flag/flag"
import { Config } from "../../config/config"
const TOOL: Record<string, [string, string]> = {
opencode_todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD],
@@ -60,7 +60,7 @@ export const RunCommand = cmd({
UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", message)
UI.empty()
const cfg = await GlobalConfig.get()
const cfg = await Config.get()
if (cfg.autoshare || Flag.OPENCODE_AUTO_SHARE || args.share) {
await Session.share(session.id)
UI.println(

View File

@@ -6,15 +6,14 @@ import { Filesystem } from "../util/filesystem"
import { ModelsDev } from "../provider/models"
import { mergeDeep } from "remeda"
import { Global } from "../global"
import fs from "fs/promises"
import { lazy } from "../util/lazy"
export namespace Config {
const log = Log.create({ service: "config" })
export const state = App.state("config", async (app) => {
let result = await Bun.file(path.join(Global.Path.config, "config.json"))
.json()
.then((mod) => Info.parse(mod))
.catch(() => ({}) as Info)
let result = await global()
for (const file of ["opencode.jsonc", "opencode.json"]) {
const [resolved] = await Filesystem.findUp(
file,
@@ -43,16 +42,24 @@ export namespace Config {
return result
})
export const McpLocal = z.object({
type: z.literal("local"),
command: z.string().array(),
environment: z.record(z.string(), z.string()).optional(),
})
export const McpLocal = z
.object({
type: z.literal("local"),
command: z.string().array(),
environment: z.record(z.string(), z.string()).optional(),
})
.openapi({
ref: "Config.McpLocal",
})
export const McpRemote = z.object({
type: z.literal("remote"),
url: z.string(),
})
export const McpRemote = z
.object({
type: z.literal("remote"),
url: z.string(),
})
.openapi({
ref: "Config.McpRemote",
})
export const Mcp = z.discriminatedUnion("type", [McpLocal, McpRemote])
export type Mcp = z.infer<typeof Mcp>
@@ -60,6 +67,41 @@ export namespace Config {
export const Info = z
.object({
$schema: z.string().optional(),
theme: z.string().optional(),
keybinds: z
.object({
leader: z.string().optional(),
help: z.string().optional(),
editor_open: z.string().optional(),
session_new: z.string().optional(),
session_list: z.string().optional(),
session_share: z.string().optional(),
session_interrupt: z.string().optional(),
session_compact: z.string().optional(),
tool_details: z.string().optional(),
model_list: z.string().optional(),
theme_list: z.string().optional(),
project_init: z.string().optional(),
input_clear: z.string().optional(),
input_paste: z.string().optional(),
input_submit: z.string().optional(),
input_newline: z.string().optional(),
history_previous: z.string().optional(),
history_next: z.string().optional(),
messages_page_up: z.string().optional(),
messages_page_down: z.string().optional(),
messages_half_page_up: z.string().optional(),
messages_half_page_down: z.string().optional(),
messages_previous: z.string().optional(),
messages_next: z.string().optional(),
messages_first: z.string().optional(),
messages_last: z.string().optional(),
app_exit: z.string().optional(),
})
.optional(),
autoshare: z.boolean().optional(),
autoupdate: z.boolean().optional(),
disabled_providers: z.array(z.string()).optional(),
provider: z
.record(
ModelsDev.Provider.partial().extend({
@@ -70,10 +112,37 @@ export namespace Config {
.optional(),
mcp: z.record(z.string(), Mcp).optional(),
})
.strict()
.openapi({
ref: "Config.Info",
})
export type Info = z.output<typeof Info>
export const global = lazy(async () => {
let result = await Bun.file(path.join(Global.Path.config, "config.json"))
.json()
.then((mod) => Info.parse(mod))
.catch(() => ({}) as Info)
await import(path.join(Global.Path.config, "config"), {
with: {
type: "toml",
},
})
.then(async (mod) => {
delete mod.default.provider
delete mod.default.model
result = mergeDeep(result, mod.default)
await Bun.write(
path.join(Global.Path.config, "config.json"),
JSON.stringify(result, null, 2),
)
await fs.unlink(path.join(Global.Path.config, "config"))
})
.catch(() => {})
return Info.parse(result)
})
export function get() {
return state()
}

View File

@@ -1,26 +0,0 @@
import { z } from "zod"
import { Global } from "."
import { lazy } from "../util/lazy"
import path from "path"
export namespace GlobalConfig {
export const Info = z.object({
provider: z.string().optional(),
model: z.string().optional(),
autoupdate: z.boolean().optional(),
autoshare: z.boolean().optional(),
disabled_providers: z.array(z.string()).optional(),
})
export type Info = z.infer<typeof Info>
export const get = lazy(async () => {
const toml = await import(path.join(Global.Path.config, "config"), {
with: {
type: "toml",
},
})
.then((mod) => mod.default)
.catch(() => ({}))
return Info.parse(toml)
})
}

View File

@@ -15,9 +15,9 @@ import { AuthCommand, AuthLoginCommand } from "./cli/cmd/auth"
import { UpgradeCommand } from "./cli/cmd/upgrade"
import { Provider } from "./provider/provider"
import { UI } from "./cli/ui"
import { GlobalConfig } from "./global/config"
import { Installation } from "./installation"
import { Bus } from "./bus"
import { Config } from "./config/config"
const cli = yargs(hideBin(process.argv))
.scriptName("opencode")
@@ -87,7 +87,7 @@ const cli = yargs(hideBin(process.argv))
;(async () => {
if (Installation.VERSION === "dev") return
if (Installation.isSnapshot()) return
const config = await GlobalConfig.get()
const config = await Config.global()
if (config.autoupdate === false) return
const latest = await Installation.latest()
if (Installation.VERSION === latest) return

View File

@@ -24,7 +24,6 @@ import { ModelsDev } from "./models"
import { NamedError } from "../util/error"
import { Auth } from "../auth"
import { TaskTool } from "../tool/task"
import { GlobalConfig } from "../global/config"
import { Global } from "../global"
export namespace Provider {
@@ -179,7 +178,7 @@ export namespace Provider {
database[providerID] = parsed
}
const disabled = await GlobalConfig.get().then(
const disabled = await Config.get().then(
(cfg) => new Set(cfg.disabled_providers ?? []),
)
// load env
@@ -300,7 +299,7 @@ export namespace Provider {
}
export async function defaultModel() {
const cfg = await GlobalConfig.get()
const cfg = await Config.get()
const provider = await list()
.then((val) => Object.values(val))
.then((x) => x.find((p) => !cfg.provider || cfg.provider === p.info.id))

View File

@@ -15,6 +15,7 @@ import { NamedError } from "../util/error"
import { ModelsDev } from "../provider/models"
import { Ripgrep } from "../external/ripgrep"
import { Installation } from "../installation"
import { Config } from "../config/config"
const ERRORS = {
400: {
@@ -140,6 +141,25 @@ export namespace Server {
return c.json(App.info())
},
)
.post(
"/config_get",
describeRoute({
description: "Get config info",
responses: {
200: {
description: "Get config info",
content: {
"application/json": {
schema: resolver(Config.Info),
},
},
},
},
}),
async (c) => {
return c.json(await Config.get())
},
)
.post(
"/app_initialize",
describeRoute({

View File

@@ -30,8 +30,8 @@ import type { Tool } from "../tool/tool"
import { SystemPrompt } from "./system"
import { Flag } from "../flag/flag"
import type { ModelsDev } from "../provider/models"
import { GlobalConfig } from "../global/config"
import { Installation } from "../installation"
import { Config } from "../config/config"
export namespace Session {
const log = Log.create({ service: "session" })
@@ -114,7 +114,7 @@ export namespace Session {
log.info("created", result)
state().sessions.set(result.id, result)
await Storage.writeJSON("session/info/" + result.id, result)
const cfg = await GlobalConfig.get()
const cfg = await Config.get()
if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.autoshare))
share(result.id).then((share) => {
update(result.id, (draft) => {