main.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "github.com/sst/opencode/internal/config"
  7. "github.com/sst/opencode/internal/llm/models"
  8. )
  9. // JSONSchemaType represents a JSON Schema type
  10. type JSONSchemaType struct {
  11. Type string `json:"type,omitempty"`
  12. Description string `json:"description,omitempty"`
  13. Properties map[string]any `json:"properties,omitempty"`
  14. Required []string `json:"required,omitempty"`
  15. AdditionalProperties any `json:"additionalProperties,omitempty"`
  16. Enum []any `json:"enum,omitempty"`
  17. Items map[string]any `json:"items,omitempty"`
  18. OneOf []map[string]any `json:"oneOf,omitempty"`
  19. AnyOf []map[string]any `json:"anyOf,omitempty"`
  20. Default any `json:"default,omitempty"`
  21. }
  22. func main() {
  23. schema := generateSchema()
  24. // Pretty print the schema
  25. encoder := json.NewEncoder(os.Stdout)
  26. encoder.SetIndent("", " ")
  27. if err := encoder.Encode(schema); err != nil {
  28. fmt.Fprintf(os.Stderr, "Error encoding schema: %v\n", err)
  29. os.Exit(1)
  30. }
  31. }
  32. func generateSchema() map[string]any {
  33. schema := map[string]any{
  34. "$schema": "http://json-schema.org/draft-07/schema#",
  35. "title": "OpenCode Configuration",
  36. "description": "Configuration schema for the OpenCode application",
  37. "type": "object",
  38. "properties": map[string]any{},
  39. }
  40. // Add Data configuration
  41. schema["properties"].(map[string]any)["data"] = map[string]any{
  42. "type": "object",
  43. "description": "Storage configuration",
  44. "properties": map[string]any{
  45. "directory": map[string]any{
  46. "type": "string",
  47. "description": "Directory where application data is stored",
  48. "default": ".opencode",
  49. },
  50. },
  51. "required": []string{"directory"},
  52. }
  53. // Add working directory
  54. schema["properties"].(map[string]any)["wd"] = map[string]any{
  55. "type": "string",
  56. "description": "Working directory for the application",
  57. }
  58. // Add debug flags
  59. schema["properties"].(map[string]any)["debug"] = map[string]any{
  60. "type": "boolean",
  61. "description": "Enable debug mode",
  62. "default": false,
  63. }
  64. schema["properties"].(map[string]any)["debugLSP"] = map[string]any{
  65. "type": "boolean",
  66. "description": "Enable LSP debug mode",
  67. "default": false,
  68. }
  69. schema["properties"].(map[string]any)["contextPaths"] = map[string]any{
  70. "type": "array",
  71. "description": "Context paths for the application",
  72. "items": map[string]any{
  73. "type": "string",
  74. },
  75. "default": []string{
  76. ".github/copilot-instructions.md",
  77. ".cursorrules",
  78. ".cursor/rules/",
  79. "CLAUDE.md",
  80. "CLAUDE.local.md",
  81. "opencode.md",
  82. "opencode.local.md",
  83. "OpenCode.md",
  84. "OpenCode.local.md",
  85. "OPENCODE.md",
  86. "OPENCODE.local.md",
  87. },
  88. }
  89. schema["properties"].(map[string]any)["tui"] = map[string]any{
  90. "type": "object",
  91. "description": "Terminal User Interface configuration",
  92. "properties": map[string]any{
  93. "theme": map[string]any{
  94. "type": "string",
  95. "description": "TUI theme name",
  96. "default": "opencode",
  97. "enum": []string{
  98. "opencode",
  99. "ayu",
  100. "catppuccin",
  101. "dracula",
  102. "flexoki",
  103. "gruvbox",
  104. "monokai",
  105. "onedark",
  106. "tokyonight",
  107. "tron",
  108. "custom",
  109. },
  110. },
  111. "customTheme": map[string]any{
  112. "type": "object",
  113. "description": "Custom theme color definitions",
  114. "additionalProperties": map[string]any{
  115. "oneOf": []map[string]any{
  116. {
  117. "type": "string",
  118. "pattern": "^#[0-9a-fA-F]{6}$",
  119. },
  120. {
  121. "type": "object",
  122. "properties": map[string]any{
  123. "dark": map[string]any{
  124. "type": "string",
  125. "pattern": "^#[0-9a-fA-F]{6}$",
  126. },
  127. "light": map[string]any{
  128. "type": "string",
  129. "pattern": "^#[0-9a-fA-F]{6}$",
  130. },
  131. },
  132. "required": []string{"dark", "light"},
  133. "additionalProperties": false,
  134. },
  135. },
  136. },
  137. },
  138. },
  139. }
  140. // Add MCP servers
  141. schema["properties"].(map[string]any)["mcpServers"] = map[string]any{
  142. "type": "object",
  143. "description": "Model Control Protocol server configurations",
  144. "additionalProperties": map[string]any{
  145. "type": "object",
  146. "description": "MCP server configuration",
  147. "properties": map[string]any{
  148. "command": map[string]any{
  149. "type": "string",
  150. "description": "Command to execute for the MCP server",
  151. },
  152. "env": map[string]any{
  153. "type": "array",
  154. "description": "Environment variables for the MCP server",
  155. "items": map[string]any{
  156. "type": "string",
  157. },
  158. },
  159. "args": map[string]any{
  160. "type": "array",
  161. "description": "Command arguments for the MCP server",
  162. "items": map[string]any{
  163. "type": "string",
  164. },
  165. },
  166. "type": map[string]any{
  167. "type": "string",
  168. "description": "Type of MCP server",
  169. "enum": []string{"stdio", "sse"},
  170. "default": "stdio",
  171. },
  172. "url": map[string]any{
  173. "type": "string",
  174. "description": "URL for SSE type MCP servers",
  175. },
  176. "headers": map[string]any{
  177. "type": "object",
  178. "description": "HTTP headers for SSE type MCP servers",
  179. "additionalProperties": map[string]any{
  180. "type": "string",
  181. },
  182. },
  183. },
  184. "required": []string{"command"},
  185. },
  186. }
  187. // Add providers
  188. providerSchema := map[string]any{
  189. "type": "object",
  190. "description": "LLM provider configurations",
  191. "additionalProperties": map[string]any{
  192. "type": "object",
  193. "description": "Provider configuration",
  194. "properties": map[string]any{
  195. "apiKey": map[string]any{
  196. "type": "string",
  197. "description": "API key for the provider",
  198. },
  199. "disabled": map[string]any{
  200. "type": "boolean",
  201. "description": "Whether the provider is disabled",
  202. "default": false,
  203. },
  204. },
  205. },
  206. }
  207. // Add known providers
  208. knownProviders := []string{
  209. string(models.ProviderAnthropic),
  210. string(models.ProviderOpenAI),
  211. string(models.ProviderGemini),
  212. string(models.ProviderGROQ),
  213. string(models.ProviderOpenRouter),
  214. string(models.ProviderBedrock),
  215. string(models.ProviderAzure),
  216. string(models.ProviderVertexAI),
  217. }
  218. providerSchema["additionalProperties"].(map[string]any)["properties"].(map[string]any)["provider"] = map[string]any{
  219. "type": "string",
  220. "description": "Provider type",
  221. "enum": knownProviders,
  222. }
  223. schema["properties"].(map[string]any)["providers"] = providerSchema
  224. // Add agents
  225. agentSchema := map[string]any{
  226. "type": "object",
  227. "description": "Agent configurations",
  228. "additionalProperties": map[string]any{
  229. "type": "object",
  230. "description": "Agent configuration",
  231. "properties": map[string]any{
  232. "model": map[string]any{
  233. "type": "string",
  234. "description": "Model ID for the agent",
  235. },
  236. "maxTokens": map[string]any{
  237. "type": "integer",
  238. "description": "Maximum tokens for the agent",
  239. "minimum": 1,
  240. },
  241. "reasoningEffort": map[string]any{
  242. "type": "string",
  243. "description": "Reasoning effort for models that support it (OpenAI, Anthropic)",
  244. "enum": []string{"low", "medium", "high"},
  245. },
  246. },
  247. "required": []string{"model"},
  248. },
  249. }
  250. // Add model enum
  251. modelEnum := []string{}
  252. for modelID := range models.SupportedModels {
  253. modelEnum = append(modelEnum, string(modelID))
  254. }
  255. agentSchema["additionalProperties"].(map[string]any)["properties"].(map[string]any)["model"].(map[string]any)["enum"] = modelEnum
  256. // Add specific agent properties
  257. agentProperties := map[string]any{}
  258. knownAgents := []string{
  259. string(config.AgentPrimary),
  260. string(config.AgentTask),
  261. string(config.AgentTitle),
  262. }
  263. for _, agentName := range knownAgents {
  264. agentProperties[agentName] = map[string]any{
  265. "$ref": "#/definitions/agent",
  266. }
  267. }
  268. // Create a combined schema that allows both specific agents and additional ones
  269. combinedAgentSchema := map[string]any{
  270. "type": "object",
  271. "description": "Agent configurations",
  272. "properties": agentProperties,
  273. "additionalProperties": agentSchema["additionalProperties"],
  274. }
  275. schema["properties"].(map[string]any)["agents"] = combinedAgentSchema
  276. schema["definitions"] = map[string]any{
  277. "agent": agentSchema["additionalProperties"],
  278. }
  279. // Add LSP configuration
  280. schema["properties"].(map[string]any)["lsp"] = map[string]any{
  281. "type": "object",
  282. "description": "Language Server Protocol configurations",
  283. "additionalProperties": map[string]any{
  284. "type": "object",
  285. "description": "LSP configuration for a language",
  286. "properties": map[string]any{
  287. "disabled": map[string]any{
  288. "type": "boolean",
  289. "description": "Whether the LSP is disabled",
  290. "default": false,
  291. },
  292. "command": map[string]any{
  293. "type": "string",
  294. "description": "Command to execute for the LSP server",
  295. },
  296. "args": map[string]any{
  297. "type": "array",
  298. "description": "Command arguments for the LSP server",
  299. "items": map[string]any{
  300. "type": "string",
  301. },
  302. },
  303. "options": map[string]any{
  304. "type": "object",
  305. "description": "Additional options for the LSP server",
  306. },
  307. },
  308. "required": []string{"command"},
  309. },
  310. }
  311. return schema
  312. }