config.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { Log } from "../util/log"
  2. import path from "path"
  3. import { z } from "zod"
  4. import { App } from "../app/app"
  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. export namespace Config {
  13. const log = Log.create({ service: "config" })
  14. export const state = App.state("config", async (app) => {
  15. let result = await global()
  16. for (const file of ["opencode.jsonc", "opencode.json"]) {
  17. const found = await Filesystem.findUp(file, app.path.cwd, app.path.root)
  18. for (const resolved of found.toReversed()) {
  19. result = mergeDeep(result, await load(resolved))
  20. }
  21. }
  22. log.info("loaded", result)
  23. return result
  24. })
  25. export const McpLocal = z
  26. .object({
  27. type: z.literal("local").describe("Type of MCP server connection"),
  28. command: z.string().array().describe("Command and arguments to run the MCP server"),
  29. environment: z
  30. .record(z.string(), z.string())
  31. .optional()
  32. .describe("Environment variables to set when running the MCP server"),
  33. enabled: z.boolean().optional().describe("Enable or disable the MCP server on startup"),
  34. })
  35. .strict()
  36. .openapi({
  37. ref: "McpLocalConfig",
  38. })
  39. export const McpRemote = z
  40. .object({
  41. type: z.literal("remote").describe("Type of MCP server connection"),
  42. url: z.string().describe("URL of the remote MCP server"),
  43. enabled: z.boolean().optional().describe("Enable or disable the MCP server on startup"),
  44. })
  45. .strict()
  46. .openapi({
  47. ref: "McpRemoteConfig",
  48. })
  49. export const Mcp = z.discriminatedUnion("type", [McpLocal, McpRemote])
  50. export type Mcp = z.infer<typeof Mcp>
  51. export const Mode = z
  52. .object({
  53. model: z.string().optional(),
  54. prompt: z.string().optional(),
  55. tools: z.record(z.string(), z.boolean()).optional(),
  56. })
  57. .openapi({
  58. ref: "ModeConfig",
  59. })
  60. export type Mode = z.infer<typeof Mode>
  61. export const Keybinds = z
  62. .object({
  63. leader: z.string().optional().default("ctrl+x").describe("Leader key for keybind combinations"),
  64. app_help: z.string().optional().default("<leader>h").describe("Show help dialog"),
  65. switch_mode: z.string().optional().default("tab").describe("Switch mode"),
  66. editor_open: z.string().optional().default("<leader>e").describe("Open external editor"),
  67. session_new: z.string().optional().default("<leader>n").describe("Create a new session"),
  68. session_list: z.string().optional().default("<leader>l").describe("List all sessions"),
  69. session_share: z.string().optional().default("<leader>s").describe("Share current session"),
  70. session_unshare: z.string().optional().default("<leader>u").describe("Unshare current session"),
  71. session_interrupt: z.string().optional().default("esc").describe("Interrupt current session"),
  72. session_compact: z.string().optional().default("<leader>c").describe("Compact the session"),
  73. tool_details: z.string().optional().default("<leader>d").describe("Toggle tool details"),
  74. model_list: z.string().optional().default("<leader>m").describe("List available models"),
  75. theme_list: z.string().optional().default("<leader>t").describe("List available themes"),
  76. file_list: z.string().optional().default("<leader>f").describe("List files"),
  77. file_close: z.string().optional().default("esc").describe("Close file"),
  78. file_search: z.string().optional().default("<leader>/").describe("Search file"),
  79. file_diff_toggle: z.string().optional().default("<leader>v").describe("Split/unified diff"),
  80. project_init: z.string().optional().default("<leader>i").describe("Create/update AGENTS.md"),
  81. input_clear: z.string().optional().default("ctrl+c").describe("Clear input field"),
  82. input_paste: z.string().optional().default("ctrl+v").describe("Paste from clipboard"),
  83. input_submit: z.string().optional().default("enter").describe("Submit input"),
  84. input_newline: z.string().optional().default("shift+enter,ctrl+j").describe("Insert newline in input"),
  85. messages_page_up: z.string().optional().default("pgup").describe("Scroll messages up by one page"),
  86. messages_page_down: z.string().optional().default("pgdown").describe("Scroll messages down by one page"),
  87. messages_half_page_up: z.string().optional().default("ctrl+alt+u").describe("Scroll messages up by half page"),
  88. messages_half_page_down: z
  89. .string()
  90. .optional()
  91. .default("ctrl+alt+d")
  92. .describe("Scroll messages down by half page"),
  93. messages_previous: z.string().optional().default("ctrl+up").describe("Navigate to previous message"),
  94. messages_next: z.string().optional().default("ctrl+down").describe("Navigate to next message"),
  95. messages_first: z.string().optional().default("ctrl+g").describe("Navigate to first message"),
  96. messages_last: z.string().optional().default("ctrl+alt+g").describe("Navigate to last message"),
  97. messages_layout_toggle: z.string().optional().default("<leader>p").describe("Toggle layout"),
  98. messages_copy: z.string().optional().default("<leader>y").describe("Copy message"),
  99. messages_revert: z.string().optional().default("<leader>r").describe("Revert message"),
  100. app_exit: z.string().optional().default("ctrl+c,<leader>q").describe("Exit the application"),
  101. })
  102. .strict()
  103. .openapi({
  104. ref: "KeybindsConfig",
  105. })
  106. export const Info = z
  107. .object({
  108. $schema: z.string().optional().describe("JSON schema reference for configuration validation"),
  109. theme: z.string().optional().describe("Theme name to use for the interface"),
  110. keybinds: Keybinds.optional().describe("Custom keybind configurations"),
  111. share: z.enum(["auto", "disabled"]).optional().describe("Control sharing behavior: 'auto' enables automatic sharing, 'disabled' disables all sharing"),
  112. autoshare: z.boolean().optional().describe("@deprecated Use 'share' field instead. Share newly created sessions automatically"),
  113. autoupdate: z.boolean().optional().describe("Automatically update to the latest version"),
  114. disabled_providers: z.array(z.string()).optional().describe("Disable providers that are loaded automatically"),
  115. model: z.string().describe("Model to use in the format of provider/model, eg anthropic/claude-2").optional(),
  116. username: z.string().optional().describe("Custom username to display in conversations instead of system username"),
  117. mode: z
  118. .object({
  119. build: Mode.optional(),
  120. plan: Mode.optional(),
  121. })
  122. .catchall(Mode)
  123. .optional(),
  124. log_level: Log.Level.optional().describe("Minimum log level to write to log files"),
  125. provider: z
  126. .record(
  127. ModelsDev.Provider.partial().extend({
  128. models: z.record(ModelsDev.Model.partial()),
  129. options: z.record(z.any()).optional(),
  130. }),
  131. )
  132. .optional()
  133. .describe("Custom provider configurations and model overrides"),
  134. mcp: z.record(z.string(), Mcp).optional().describe("MCP (Model Context Protocol) server configurations"),
  135. instructions: z.array(z.string()).optional().describe("Additional instruction files or patterns to include"),
  136. experimental: z
  137. .object({
  138. hook: z
  139. .object({
  140. file_edited: z
  141. .record(
  142. z.string(),
  143. z
  144. .object({
  145. command: z.string().array(),
  146. environment: z.record(z.string(), z.string()).optional(),
  147. })
  148. .array(),
  149. )
  150. .optional(),
  151. session_completed: z
  152. .object({
  153. command: z.string().array(),
  154. environment: z.record(z.string(), z.string()).optional(),
  155. })
  156. .array()
  157. .optional(),
  158. })
  159. .optional(),
  160. })
  161. .optional(),
  162. })
  163. .strict()
  164. .openapi({
  165. ref: "Config",
  166. })
  167. export type Info = z.output<typeof Info>
  168. export const global = lazy(async () => {
  169. let result = pipe(
  170. {},
  171. mergeDeep(await load(path.join(Global.Path.config, "config.json"))),
  172. mergeDeep(await load(path.join(Global.Path.config, "opencode.json"))),
  173. )
  174. await import(path.join(Global.Path.config, "config"), {
  175. with: {
  176. type: "toml",
  177. },
  178. })
  179. .then(async (mod) => {
  180. const { provider, model, ...rest } = mod.default
  181. if (provider && model) result.model = `${provider}/${model}`
  182. result["$schema"] = "https://opencode.ai/config.json"
  183. result = mergeDeep(result, rest)
  184. await Bun.write(path.join(Global.Path.config, "config.json"), JSON.stringify(result, null, 2))
  185. await fs.unlink(path.join(Global.Path.config, "config"))
  186. })
  187. .catch(() => {})
  188. return result
  189. })
  190. async function load(configPath: string) {
  191. let text = await Bun.file(configPath)
  192. .text()
  193. .catch((err) => {
  194. if (err.code === "ENOENT") return
  195. throw new JsonError({ path: configPath }, { cause: err })
  196. })
  197. if (!text) return {}
  198. text = text.replace(/\{env:([^}]+)\}/g, (_, varName) => {
  199. return process.env[varName] || ""
  200. })
  201. const fileMatches = text.match(/"?\{file:([^}]+)\}"?/g)
  202. if (fileMatches) {
  203. const configDir = path.dirname(configPath)
  204. for (const match of fileMatches) {
  205. const filePath = match.replace(/^"?\{file:/, "").replace(/\}"?$/, "")
  206. const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(configDir, filePath)
  207. const fileContent = await Bun.file(resolvedPath).text()
  208. text = text.replace(match, JSON.stringify(fileContent))
  209. }
  210. }
  211. let data: any
  212. try {
  213. data = JSON.parse(text)
  214. } catch (err) {
  215. throw new JsonError({ path: configPath }, { cause: err as Error })
  216. }
  217. const parsed = Info.safeParse(data)
  218. if (parsed.success) {
  219. // Handle migration from autoshare to share field
  220. if (parsed.data.autoshare === true && !parsed.data.share) {
  221. parsed.data.share = "auto"
  222. }
  223. if (!parsed.data.$schema) {
  224. parsed.data.$schema = "https://opencode.ai/config.json"
  225. await Bun.write(configPath, JSON.stringify(parsed.data, null, 2))
  226. }
  227. return parsed.data
  228. }
  229. throw new InvalidError({ path: configPath, issues: parsed.error.issues })
  230. }
  231. export const JsonError = NamedError.create(
  232. "ConfigJsonError",
  233. z.object({
  234. path: z.string(),
  235. }),
  236. )
  237. export const InvalidError = NamedError.create(
  238. "ConfigInvalidError",
  239. z.object({
  240. path: z.string(),
  241. issues: z.custom<z.ZodIssue[]>().optional(),
  242. }),
  243. )
  244. export function get() {
  245. return state()
  246. }
  247. }