config.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. import { Log } from "../util/log"
  2. import path from "path"
  3. import os from "os"
  4. import z from "zod/v4"
  5. import { Filesystem } from "../util/filesystem"
  6. import { ModelsDev } from "../provider/models"
  7. import { mergeDeep, pipe } from "remeda"
  8. import { Global } from "../global"
  9. import fs from "fs/promises"
  10. import { lazy } from "../util/lazy"
  11. import { NamedError } from "../util/error"
  12. import matter from "gray-matter"
  13. import { Flag } from "../flag/flag"
  14. import { Auth } from "../auth"
  15. import { type ParseError as JsoncParseError, parse as parseJsonc, printParseErrorCode } from "jsonc-parser"
  16. import { Instance } from "../project/instance"
  17. import { LSPServer } from "../lsp/server"
  18. import { BunProc } from "@/bun"
  19. import { Installation } from "@/installation"
  20. export namespace Config {
  21. const log = Log.create({ service: "config" })
  22. export const state = Instance.state(async () => {
  23. const auth = await Auth.all()
  24. let result = await global()
  25. for (const file of ["opencode.jsonc", "opencode.json"]) {
  26. const found = await Filesystem.findUp(file, Instance.directory, Instance.worktree)
  27. for (const resolved of found.toReversed()) {
  28. result = mergeDeep(result, await loadFile(resolved))
  29. }
  30. }
  31. // Override with custom config if provided
  32. if (Flag.OPENCODE_CONFIG) {
  33. result = mergeDeep(result, await loadFile(Flag.OPENCODE_CONFIG))
  34. log.debug("loaded custom config", { path: Flag.OPENCODE_CONFIG })
  35. }
  36. if (Flag.OPENCODE_CONFIG_CONTENT) {
  37. result = mergeDeep(result, JSON.parse(Flag.OPENCODE_CONFIG_CONTENT))
  38. log.debug("loaded custom config from OPENCODE_CONFIG_CONTENT")
  39. }
  40. for (const [key, value] of Object.entries(auth)) {
  41. if (value.type === "wellknown") {
  42. process.env[value.key] = value.token
  43. const wellknown = await fetch(`${key}/.well-known/opencode`).then((x) => x.json())
  44. result = mergeDeep(result, await load(JSON.stringify(wellknown.config ?? {}), process.cwd()))
  45. }
  46. }
  47. result.agent = result.agent || {}
  48. result.mode = result.mode || {}
  49. result.plugin = result.plugin || []
  50. const directories = [
  51. Global.Path.config,
  52. ...(await Array.fromAsync(
  53. Filesystem.up({ targets: [".opencode"], start: Instance.directory, stop: Instance.worktree }),
  54. )),
  55. ]
  56. for (const dir of directories) {
  57. await assertValid(dir)
  58. installDependencies(dir)
  59. result.command = mergeDeep(result.command ?? {}, await loadCommand(dir))
  60. result.agent = mergeDeep(result.agent, await loadAgent(dir))
  61. result.agent = mergeDeep(result.agent, await loadMode(dir))
  62. result.plugin.push(...(await loadPlugin(dir)))
  63. }
  64. // Migrate deprecated mode field to agent field
  65. for (const [name, mode] of Object.entries(result.mode)) {
  66. result.agent = mergeDeep(result.agent ?? {}, {
  67. [name]: {
  68. ...mode,
  69. mode: "primary" as const,
  70. },
  71. })
  72. }
  73. if (Flag.OPENCODE_PERMISSION) {
  74. result.permission = mergeDeep(result.permission ?? {}, JSON.parse(Flag.OPENCODE_PERMISSION))
  75. }
  76. if (!result.username) result.username = os.userInfo().username
  77. // Handle migration from autoshare to share field
  78. if (result.autoshare === true && !result.share) {
  79. result.share = "auto"
  80. }
  81. if (result.keybinds?.messages_revert && !result.keybinds.messages_undo) {
  82. result.keybinds.messages_undo = result.keybinds.messages_revert
  83. }
  84. // Handle migration from autoshare to share field
  85. if (result.autoshare === true && !result.share) {
  86. result.share = "auto"
  87. }
  88. if (result.keybinds?.messages_revert && !result.keybinds.messages_undo) {
  89. result.keybinds.messages_undo = result.keybinds.messages_revert
  90. }
  91. if (result.keybinds?.switch_mode && !result.keybinds.switch_agent) {
  92. result.keybinds.switch_agent = result.keybinds.switch_mode
  93. }
  94. if (result.keybinds?.switch_mode_reverse && !result.keybinds.switch_agent_reverse) {
  95. result.keybinds.switch_agent_reverse = result.keybinds.switch_mode_reverse
  96. }
  97. if (result.keybinds?.switch_agent && !result.keybinds.agent_cycle) {
  98. result.keybinds.agent_cycle = result.keybinds.switch_agent
  99. }
  100. if (result.keybinds?.switch_agent_reverse && !result.keybinds.agent_cycle_reverse) {
  101. result.keybinds.agent_cycle_reverse = result.keybinds.switch_agent_reverse
  102. }
  103. return {
  104. config: result,
  105. directories,
  106. }
  107. })
  108. const INVALID_DIRS = new Bun.Glob(`{${["agents", "commands", "plugins", "tools"].join(",")}}/`)
  109. async function assertValid(dir: string) {
  110. const invalid = await Array.fromAsync(
  111. INVALID_DIRS.scan({
  112. onlyFiles: false,
  113. cwd: dir,
  114. }),
  115. )
  116. for (const item of invalid) {
  117. throw new DirectoryError({
  118. path: dir,
  119. dir: item,
  120. suggestion: item.substring(0, item.length - 1),
  121. })
  122. }
  123. }
  124. async function installDependencies(dir: string) {
  125. if (Installation.isLocal()) return
  126. const pkg = path.join(dir, "package.json")
  127. if (!(await Bun.file(pkg).exists())) {
  128. await Bun.write(pkg, "{}")
  129. }
  130. const gitignore = path.join(dir, ".gitignore")
  131. const hasGitIgnore = await Bun.file(gitignore).exists()
  132. if (!hasGitIgnore) await Bun.write(gitignore, ["node_modules", "package.json", "bun.lock", ".gitignore"].join("\n"))
  133. await BunProc.run(
  134. ["add", "@opencode-ai/plugin@" + (Installation.isLocal() ? "latest" : Installation.VERSION), "--exact"],
  135. {
  136. cwd: dir,
  137. },
  138. )
  139. }
  140. const COMMAND_GLOB = new Bun.Glob("command/**/*.md")
  141. async function loadCommand(dir: string) {
  142. const result: Record<string, Command> = {}
  143. for await (const item of COMMAND_GLOB.scan({ absolute: true, followSymlinks: true, dot: true, cwd: dir })) {
  144. const content = await Bun.file(item).text()
  145. const md = matter(content)
  146. if (!md.data) continue
  147. const name = (() => {
  148. const patterns = ["/.opencode/command/", "/command/"]
  149. const pattern = patterns.find((p) => item.includes(p))
  150. if (pattern) {
  151. const index = item.indexOf(pattern)
  152. return item.slice(index + pattern.length, -3)
  153. }
  154. return path.basename(item, ".md")
  155. })()
  156. const config = {
  157. name,
  158. ...md.data,
  159. template: md.content.trim(),
  160. }
  161. const parsed = Command.safeParse(config)
  162. if (parsed.success) {
  163. result[config.name] = parsed.data
  164. continue
  165. }
  166. throw new InvalidError({ path: item }, { cause: parsed.error })
  167. }
  168. return result
  169. }
  170. const AGENT_GLOB = new Bun.Glob("agent/**/*.md")
  171. async function loadAgent(dir: string) {
  172. const result: Record<string, Agent> = {}
  173. for await (const item of AGENT_GLOB.scan({ absolute: true, followSymlinks: true, dot: true, cwd: dir })) {
  174. const content = await Bun.file(item).text()
  175. const md = matter(content)
  176. if (!md.data) continue
  177. // Extract relative path from agent folder for nested agents
  178. let agentName = path.basename(item, ".md")
  179. const agentFolderPath = item.includes("/.opencode/agent/")
  180. ? item.split("/.opencode/agent/")[1]
  181. : item.includes("/agent/")
  182. ? item.split("/agent/")[1]
  183. : agentName + ".md"
  184. // If agent is in a subfolder, include folder path in name
  185. if (agentFolderPath.includes("/")) {
  186. const relativePath = agentFolderPath.replace(".md", "")
  187. const pathParts = relativePath.split("/")
  188. agentName = pathParts.slice(0, -1).join("/") + "/" + pathParts[pathParts.length - 1]
  189. }
  190. const config = {
  191. name: agentName,
  192. ...md.data,
  193. prompt: md.content.trim(),
  194. }
  195. const parsed = Agent.safeParse(config)
  196. if (parsed.success) {
  197. result[config.name] = parsed.data
  198. continue
  199. }
  200. throw new InvalidError({ path: item }, { cause: parsed.error })
  201. }
  202. return result
  203. }
  204. const MODE_GLOB = new Bun.Glob("mode/*.md")
  205. async function loadMode(dir: string) {
  206. const result: Record<string, Agent> = {}
  207. for await (const item of MODE_GLOB.scan({ absolute: true, followSymlinks: true, dot: true, cwd: dir })) {
  208. const content = await Bun.file(item).text()
  209. const md = matter(content)
  210. if (!md.data) continue
  211. const config = {
  212. name: path.basename(item, ".md"),
  213. ...md.data,
  214. prompt: md.content.trim(),
  215. }
  216. const parsed = Agent.safeParse(config)
  217. if (parsed.success) {
  218. result[config.name] = {
  219. ...parsed.data,
  220. mode: "primary" as const,
  221. }
  222. continue
  223. }
  224. }
  225. return result
  226. }
  227. const PLUGIN_GLOB = new Bun.Glob("plugin/*.{ts,js}")
  228. async function loadPlugin(dir: string) {
  229. const plugins: string[] = []
  230. for await (const item of PLUGIN_GLOB.scan({ absolute: true, followSymlinks: true, dot: true, cwd: dir })) {
  231. plugins.push("file://" + item)
  232. }
  233. return plugins
  234. }
  235. export const McpLocal = z
  236. .object({
  237. type: z.literal("local").describe("Type of MCP server connection"),
  238. command: z.string().array().describe("Command and arguments to run the MCP server"),
  239. environment: z
  240. .record(z.string(), z.string())
  241. .optional()
  242. .describe("Environment variables to set when running the MCP server"),
  243. enabled: z.boolean().optional().describe("Enable or disable the MCP server on startup"),
  244. })
  245. .strict()
  246. .meta({
  247. ref: "McpLocalConfig",
  248. })
  249. export const McpRemote = z
  250. .object({
  251. type: z.literal("remote").describe("Type of MCP server connection"),
  252. url: z.string().describe("URL of the remote MCP server"),
  253. enabled: z.boolean().optional().describe("Enable or disable the MCP server on startup"),
  254. headers: z.record(z.string(), z.string()).optional().describe("Headers to send with the request"),
  255. })
  256. .strict()
  257. .meta({
  258. ref: "McpRemoteConfig",
  259. })
  260. export const Mcp = z.discriminatedUnion("type", [McpLocal, McpRemote])
  261. export type Mcp = z.infer<typeof Mcp>
  262. export const Permission = z.union([z.literal("ask"), z.literal("allow"), z.literal("deny")])
  263. export type Permission = z.infer<typeof Permission>
  264. export const Command = z.object({
  265. template: z.string(),
  266. description: z.string().optional(),
  267. agent: z.string().optional(),
  268. model: z.string().optional(),
  269. subtask: z.boolean().optional(),
  270. })
  271. export type Command = z.infer<typeof Command>
  272. export const Agent = z
  273. .object({
  274. model: z.string().optional(),
  275. temperature: z.number().optional(),
  276. top_p: z.number().optional(),
  277. prompt: z.string().optional(),
  278. tools: z.record(z.string(), z.boolean()).optional(),
  279. disable: z.boolean().optional(),
  280. description: z.string().optional().describe("Description of when to use the agent"),
  281. mode: z.union([z.literal("subagent"), z.literal("primary"), z.literal("all")]).optional(),
  282. permission: z
  283. .object({
  284. edit: Permission.optional(),
  285. bash: z.union([Permission, z.record(z.string(), Permission)]).optional(),
  286. webfetch: Permission.optional(),
  287. })
  288. .optional(),
  289. })
  290. .catchall(z.any())
  291. .meta({
  292. ref: "AgentConfig",
  293. })
  294. export type Agent = z.infer<typeof Agent>
  295. export const Keybinds = z
  296. .object({
  297. leader: z.string().optional().default("ctrl+x").describe("Leader key for keybind combinations"),
  298. app_help: z.string().optional().default("<leader>h").describe("Show help dialog"),
  299. app_exit: z.string().optional().default("ctrl+c,<leader>q").describe("Exit the application"),
  300. editor_open: z.string().optional().default("<leader>e").describe("Open external editor"),
  301. theme_list: z.string().optional().default("<leader>t").describe("List available themes"),
  302. project_init: z.string().optional().default("<leader>i").describe("Create/update AGENTS.md"),
  303. tool_details: z.string().optional().default("<leader>d").describe("Toggle tool details"),
  304. thinking_blocks: z.string().optional().default("<leader>b").describe("Toggle thinking blocks"),
  305. session_export: z.string().optional().default("<leader>x").describe("Export session to editor"),
  306. session_new: z.string().optional().default("<leader>n").describe("Create a new session"),
  307. session_list: z.string().optional().default("<leader>l").describe("List all sessions"),
  308. session_timeline: z.string().optional().default("<leader>g").describe("Show session timeline"),
  309. session_share: z.string().optional().default("<leader>s").describe("Share current session"),
  310. session_unshare: z.string().optional().default("none").describe("Unshare current session"),
  311. session_interrupt: z.string().optional().default("esc").describe("Interrupt current session"),
  312. session_compact: z.string().optional().default("<leader>c").describe("Compact the session"),
  313. session_child_cycle: z.string().optional().default("ctrl+right").describe("Cycle to next child session"),
  314. session_child_cycle_reverse: z
  315. .string()
  316. .optional()
  317. .default("ctrl+left")
  318. .describe("Cycle to previous child session"),
  319. messages_page_up: z.string().optional().default("pgup").describe("Scroll messages up by one page"),
  320. messages_page_down: z.string().optional().default("pgdown").describe("Scroll messages down by one page"),
  321. messages_half_page_up: z.string().optional().default("ctrl+alt+u").describe("Scroll messages up by half page"),
  322. messages_half_page_down: z
  323. .string()
  324. .optional()
  325. .default("ctrl+alt+d")
  326. .describe("Scroll messages down by half page"),
  327. messages_first: z.string().optional().default("ctrl+g").describe("Navigate to first message"),
  328. messages_last: z.string().optional().default("ctrl+alt+g").describe("Navigate to last message"),
  329. messages_copy: z.string().optional().default("<leader>y").describe("Copy message"),
  330. messages_undo: z.string().optional().default("<leader>u").describe("Undo message"),
  331. messages_redo: z.string().optional().default("<leader>r").describe("Redo message"),
  332. model_list: z.string().optional().default("<leader>m").describe("List available models"),
  333. model_cycle_recent: z.string().optional().default("f2").describe("Next recent model"),
  334. model_cycle_recent_reverse: z.string().optional().default("shift+f2").describe("Previous recent model"),
  335. agent_list: z.string().optional().default("<leader>a").describe("List agents"),
  336. agent_cycle: z.string().optional().default("tab").describe("Next agent"),
  337. agent_cycle_reverse: z.string().optional().default("shift+tab").describe("Previous agent"),
  338. input_clear: z.string().optional().default("ctrl+c").describe("Clear input field"),
  339. input_paste: z.string().optional().default("ctrl+v").describe("Paste from clipboard"),
  340. input_submit: z.string().optional().default("enter").describe("Submit input"),
  341. input_newline: z.string().optional().default("shift+enter,ctrl+j").describe("Insert newline in input"),
  342. // Deprecated commands
  343. switch_mode: z.string().optional().default("none").describe("@deprecated use agent_cycle. Next mode"),
  344. switch_mode_reverse: z
  345. .string()
  346. .optional()
  347. .default("none")
  348. .describe("@deprecated use agent_cycle_reverse. Previous mode"),
  349. switch_agent: z.string().optional().default("tab").describe("@deprecated use agent_cycle. Next agent"),
  350. switch_agent_reverse: z
  351. .string()
  352. .optional()
  353. .default("shift+tab")
  354. .describe("@deprecated use agent_cycle_reverse. Previous agent"),
  355. file_list: z.string().optional().default("none").describe("@deprecated Currently not available. List files"),
  356. file_close: z.string().optional().default("none").describe("@deprecated Close file"),
  357. file_search: z.string().optional().default("none").describe("@deprecated Search file"),
  358. file_diff_toggle: z.string().optional().default("none").describe("@deprecated Split/unified diff"),
  359. messages_previous: z.string().optional().default("none").describe("@deprecated Navigate to previous message"),
  360. messages_next: z.string().optional().default("none").describe("@deprecated Navigate to next message"),
  361. messages_layout_toggle: z.string().optional().default("none").describe("@deprecated Toggle layout"),
  362. messages_revert: z.string().optional().default("none").describe("@deprecated use messages_undo. Revert message"),
  363. })
  364. .strict()
  365. .meta({
  366. ref: "KeybindsConfig",
  367. })
  368. export const TUI = z.object({
  369. scroll_speed: z.number().min(1).optional().default(2).describe("TUI scroll speed"),
  370. })
  371. export const Layout = z.enum(["auto", "stretch"]).meta({
  372. ref: "LayoutConfig",
  373. })
  374. export type Layout = z.infer<typeof Layout>
  375. export const Info = z
  376. .object({
  377. $schema: z.string().optional().describe("JSON schema reference for configuration validation"),
  378. theme: z.string().optional().describe("Theme name to use for the interface"),
  379. keybinds: Keybinds.optional().describe("Custom keybind configurations"),
  380. tui: TUI.optional().describe("TUI specific settings"),
  381. command: z
  382. .record(z.string(), Command)
  383. .optional()
  384. .describe("Command configuration, see https://opencode.ai/docs/commands"),
  385. watcher: z
  386. .object({
  387. ignore: z.array(z.string()).optional(),
  388. })
  389. .optional(),
  390. plugin: z.string().array().optional(),
  391. snapshot: z.boolean().optional(),
  392. share: z
  393. .enum(["manual", "auto", "disabled"])
  394. .optional()
  395. .describe(
  396. "Control sharing behavior:'manual' allows manual sharing via commands, 'auto' enables automatic sharing, 'disabled' disables all sharing",
  397. ),
  398. autoshare: z
  399. .boolean()
  400. .optional()
  401. .describe("@deprecated Use 'share' field instead. Share newly created sessions automatically"),
  402. autoupdate: z.boolean().optional().describe("Automatically update to the latest version"),
  403. disabled_providers: z.array(z.string()).optional().describe("Disable providers that are loaded automatically"),
  404. model: z.string().describe("Model to use in the format of provider/model, eg anthropic/claude-2").optional(),
  405. small_model: z
  406. .string()
  407. .describe("Small model to use for tasks like title generation in the format of provider/model")
  408. .optional(),
  409. username: z
  410. .string()
  411. .optional()
  412. .describe("Custom username to display in conversations instead of system username"),
  413. mode: z
  414. .object({
  415. build: Agent.optional(),
  416. plan: Agent.optional(),
  417. })
  418. .catchall(Agent)
  419. .optional()
  420. .describe("@deprecated Use `agent` field instead."),
  421. agent: z
  422. .object({
  423. plan: Agent.optional(),
  424. build: Agent.optional(),
  425. general: Agent.optional(),
  426. })
  427. .catchall(Agent)
  428. .optional()
  429. .describe("Agent configuration, see https://opencode.ai/docs/agent"),
  430. provider: z
  431. .record(
  432. z.string(),
  433. ModelsDev.Provider.partial()
  434. .extend({
  435. models: z.record(z.string(), ModelsDev.Model.partial()).optional(),
  436. options: z
  437. .object({
  438. apiKey: z.string().optional(),
  439. baseURL: z.string().optional(),
  440. timeout: z
  441. .union([
  442. z
  443. .number()
  444. .int()
  445. .positive()
  446. .describe(
  447. "Timeout in milliseconds for requests to this provider. Default is 300000 (5 minutes). Set to false to disable timeout.",
  448. ),
  449. z.literal(false).describe("Disable timeout for this provider entirely."),
  450. ])
  451. .optional()
  452. .describe(
  453. "Timeout in milliseconds for requests to this provider. Default is 300000 (5 minutes). Set to false to disable timeout.",
  454. ),
  455. })
  456. .catchall(z.any())
  457. .optional(),
  458. })
  459. .strict(),
  460. )
  461. .optional()
  462. .describe("Custom provider configurations and model overrides"),
  463. mcp: z.record(z.string(), Mcp).optional().describe("MCP (Model Context Protocol) server configurations"),
  464. formatter: z
  465. .record(
  466. z.string(),
  467. z.object({
  468. disabled: z.boolean().optional(),
  469. command: z.array(z.string()).optional(),
  470. environment: z.record(z.string(), z.string()).optional(),
  471. extensions: z.array(z.string()).optional(),
  472. }),
  473. )
  474. .optional(),
  475. lsp: z
  476. .record(
  477. z.string(),
  478. z.union([
  479. z.object({
  480. disabled: z.literal(true),
  481. }),
  482. z.object({
  483. command: z.array(z.string()),
  484. extensions: z.array(z.string()).optional(),
  485. disabled: z.boolean().optional(),
  486. env: z.record(z.string(), z.string()).optional(),
  487. initialization: z.record(z.string(), z.any()).optional(),
  488. }),
  489. ]),
  490. )
  491. .optional()
  492. .refine(
  493. (data) => {
  494. if (!data) return true
  495. const serverIds = new Set(Object.values(LSPServer).map((s) => s.id))
  496. return Object.entries(data).every(([id, config]) => {
  497. if (config.disabled) return true
  498. if (serverIds.has(id)) return true
  499. return Boolean(config.extensions)
  500. })
  501. },
  502. {
  503. error: "For custom LSP servers, 'extensions' array is required.",
  504. },
  505. ),
  506. instructions: z.array(z.string()).optional().describe("Additional instruction files or patterns to include"),
  507. layout: Layout.optional().describe("@deprecated Always uses stretch layout."),
  508. permission: z
  509. .object({
  510. edit: Permission.optional(),
  511. bash: z.union([Permission, z.record(z.string(), Permission)]).optional(),
  512. webfetch: Permission.optional(),
  513. })
  514. .optional(),
  515. tools: z.record(z.string(), z.boolean()).optional(),
  516. experimental: z
  517. .object({
  518. hook: z
  519. .object({
  520. file_edited: z
  521. .record(
  522. z.string(),
  523. z
  524. .object({
  525. command: z.string().array(),
  526. environment: z.record(z.string(), z.string()).optional(),
  527. })
  528. .array(),
  529. )
  530. .optional(),
  531. session_completed: z
  532. .object({
  533. command: z.string().array(),
  534. environment: z.record(z.string(), z.string()).optional(),
  535. })
  536. .array()
  537. .optional(),
  538. })
  539. .optional(),
  540. disable_paste_summary: z.boolean().optional(),
  541. })
  542. .optional(),
  543. })
  544. .strict()
  545. .meta({
  546. ref: "Config",
  547. })
  548. export type Info = z.output<typeof Info>
  549. export const global = lazy(async () => {
  550. let result: Info = pipe(
  551. {},
  552. mergeDeep(await loadFile(path.join(Global.Path.config, "config.json"))),
  553. mergeDeep(await loadFile(path.join(Global.Path.config, "opencode.json"))),
  554. mergeDeep(await loadFile(path.join(Global.Path.config, "opencode.jsonc"))),
  555. )
  556. await import(path.join(Global.Path.config, "config"), {
  557. with: {
  558. type: "toml",
  559. },
  560. })
  561. .then(async (mod) => {
  562. const { provider, model, ...rest } = mod.default
  563. if (provider && model) result.model = `${provider}/${model}`
  564. result["$schema"] = "https://opencode.ai/config.json"
  565. result = mergeDeep(result, rest)
  566. await Bun.write(path.join(Global.Path.config, "config.json"), JSON.stringify(result, null, 2))
  567. await fs.unlink(path.join(Global.Path.config, "config"))
  568. })
  569. .catch(() => {})
  570. return result
  571. })
  572. async function loadFile(filepath: string): Promise<Info> {
  573. log.info("loading", { path: filepath })
  574. let text = await Bun.file(filepath)
  575. .text()
  576. .catch((err) => {
  577. if (err.code === "ENOENT") return
  578. throw new JsonError({ path: filepath }, { cause: err })
  579. })
  580. if (!text) return {}
  581. return load(text, filepath)
  582. }
  583. async function load(text: string, configFilepath: string) {
  584. text = text.replace(/\{env:([^}]+)\}/g, (_, varName) => {
  585. return process.env[varName] || ""
  586. })
  587. const fileMatches = text.match(/\{file:[^}]+\}/g)
  588. if (fileMatches) {
  589. const configDir = path.dirname(configFilepath)
  590. const lines = text.split("\n")
  591. for (const match of fileMatches) {
  592. const lineIndex = lines.findIndex((line) => line.includes(match))
  593. if (lineIndex !== -1 && lines[lineIndex].trim().startsWith("//")) {
  594. continue // Skip if line is commented
  595. }
  596. let filePath = match.replace(/^\{file:/, "").replace(/\}$/, "")
  597. if (filePath.startsWith("~/")) {
  598. filePath = path.join(os.homedir(), filePath.slice(2))
  599. }
  600. const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(configDir, filePath)
  601. const fileContent = (
  602. await Bun.file(resolvedPath)
  603. .text()
  604. .catch((error) => {
  605. const errMsg = `bad file reference: "${match}"`
  606. if (error.code === "ENOENT") {
  607. throw new InvalidError(
  608. { path: configFilepath, message: errMsg + ` ${resolvedPath} does not exist` },
  609. { cause: error },
  610. )
  611. }
  612. throw new InvalidError({ path: configFilepath, message: errMsg }, { cause: error })
  613. })
  614. ).trim()
  615. // escape newlines/quotes, strip outer quotes
  616. text = text.replace(match, JSON.stringify(fileContent).slice(1, -1))
  617. }
  618. }
  619. const errors: JsoncParseError[] = []
  620. const data = parseJsonc(text, errors, { allowTrailingComma: true })
  621. if (errors.length) {
  622. const lines = text.split("\n")
  623. const errorDetails = errors
  624. .map((e) => {
  625. const beforeOffset = text.substring(0, e.offset).split("\n")
  626. const line = beforeOffset.length
  627. const column = beforeOffset[beforeOffset.length - 1].length + 1
  628. const problemLine = lines[line - 1]
  629. const error = `${printParseErrorCode(e.error)} at line ${line}, column ${column}`
  630. if (!problemLine) return error
  631. return `${error}\n Line ${line}: ${problemLine}\n${"".padStart(column + 9)}^`
  632. })
  633. .join("\n")
  634. throw new JsonError({
  635. path: configFilepath,
  636. message: `\n--- JSONC Input ---\n${text}\n--- Errors ---\n${errorDetails}\n--- End ---`,
  637. })
  638. }
  639. const parsed = Info.safeParse(data)
  640. if (parsed.success) {
  641. if (!parsed.data.$schema) {
  642. parsed.data.$schema = "https://opencode.ai/config.json"
  643. await Bun.write(configFilepath, JSON.stringify(parsed.data, null, 2))
  644. }
  645. const data = parsed.data
  646. if (data.plugin) {
  647. for (let i = 0; i < data.plugin.length; i++) {
  648. const plugin = data.plugin[i]
  649. try {
  650. data.plugin[i] = import.meta.resolve!(plugin, configFilepath)
  651. } catch (err) {}
  652. }
  653. }
  654. return data
  655. }
  656. throw new InvalidError({ path: configFilepath, issues: parsed.error.issues })
  657. }
  658. export const JsonError = NamedError.create(
  659. "ConfigJsonError",
  660. z.object({
  661. path: z.string(),
  662. message: z.string().optional(),
  663. }),
  664. )
  665. export const DirectoryError = NamedError.create(
  666. "ConfigDirectoryError",
  667. z.object({
  668. path: z.string(),
  669. dir: z.string(),
  670. suggestion: z.string(),
  671. }),
  672. )
  673. export const InvalidError = NamedError.create(
  674. "ConfigInvalidError",
  675. z.object({
  676. path: z.string(),
  677. issues: z.custom<z.core.$ZodIssue[]>().optional(),
  678. message: z.string().optional(),
  679. }),
  680. )
  681. export async function get() {
  682. return state().then((x) => x.config)
  683. }
  684. export async function update(config: Info) {
  685. const filepath = path.join(Instance.directory, "config.json")
  686. const existing = await loadFile(filepath)
  687. await Bun.write(filepath, JSON.stringify(mergeDeep(existing, config), null, 2))
  688. await Instance.dispose()
  689. }
  690. export async function directories() {
  691. return state().then((x) => x.directories)
  692. }
  693. }