main.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. "catppuccin",
  100. "dracula",
  101. "flexoki",
  102. "gruvbox",
  103. "monokai",
  104. "onedark",
  105. "tokyonight",
  106. "tron",
  107. "custom",
  108. },
  109. },
  110. "customTheme": map[string]any{
  111. "type": "object",
  112. "description": "Custom theme color definitions",
  113. "additionalProperties": map[string]any{
  114. "oneOf": []map[string]any{
  115. {
  116. "type": "string",
  117. "pattern": "^#[0-9a-fA-F]{6}$",
  118. },
  119. {
  120. "type": "object",
  121. "properties": map[string]any{
  122. "dark": map[string]any{
  123. "type": "string",
  124. "pattern": "^#[0-9a-fA-F]{6}$",
  125. },
  126. "light": map[string]any{
  127. "type": "string",
  128. "pattern": "^#[0-9a-fA-F]{6}$",
  129. },
  130. },
  131. "required": []string{"dark", "light"},
  132. "additionalProperties": false,
  133. },
  134. },
  135. },
  136. },
  137. },
  138. }
  139. // Add MCP servers
  140. schema["properties"].(map[string]any)["mcpServers"] = map[string]any{
  141. "type": "object",
  142. "description": "Model Control Protocol server configurations",
  143. "additionalProperties": map[string]any{
  144. "type": "object",
  145. "description": "MCP server configuration",
  146. "properties": map[string]any{
  147. "command": map[string]any{
  148. "type": "string",
  149. "description": "Command to execute for the MCP server",
  150. },
  151. "env": map[string]any{
  152. "type": "array",
  153. "description": "Environment variables for the MCP server",
  154. "items": map[string]any{
  155. "type": "string",
  156. },
  157. },
  158. "args": map[string]any{
  159. "type": "array",
  160. "description": "Command arguments for the MCP server",
  161. "items": map[string]any{
  162. "type": "string",
  163. },
  164. },
  165. "type": map[string]any{
  166. "type": "string",
  167. "description": "Type of MCP server",
  168. "enum": []string{"stdio", "sse"},
  169. "default": "stdio",
  170. },
  171. "url": map[string]any{
  172. "type": "string",
  173. "description": "URL for SSE type MCP servers",
  174. },
  175. "headers": map[string]any{
  176. "type": "object",
  177. "description": "HTTP headers for SSE type MCP servers",
  178. "additionalProperties": map[string]any{
  179. "type": "string",
  180. },
  181. },
  182. },
  183. "required": []string{"command"},
  184. },
  185. }
  186. // Add providers
  187. providerSchema := map[string]any{
  188. "type": "object",
  189. "description": "LLM provider configurations",
  190. "additionalProperties": map[string]any{
  191. "type": "object",
  192. "description": "Provider configuration",
  193. "properties": map[string]any{
  194. "apiKey": map[string]any{
  195. "type": "string",
  196. "description": "API key for the provider",
  197. },
  198. "disabled": map[string]any{
  199. "type": "boolean",
  200. "description": "Whether the provider is disabled",
  201. "default": false,
  202. },
  203. },
  204. },
  205. }
  206. // Add known providers
  207. knownProviders := []string{
  208. string(models.ProviderAnthropic),
  209. string(models.ProviderOpenAI),
  210. string(models.ProviderGemini),
  211. string(models.ProviderGROQ),
  212. string(models.ProviderOpenRouter),
  213. string(models.ProviderBedrock),
  214. string(models.ProviderAzure),
  215. string(models.ProviderVertexAI),
  216. }
  217. providerSchema["additionalProperties"].(map[string]any)["properties"].(map[string]any)["provider"] = map[string]any{
  218. "type": "string",
  219. "description": "Provider type",
  220. "enum": knownProviders,
  221. }
  222. schema["properties"].(map[string]any)["providers"] = providerSchema
  223. // Add agents
  224. agentSchema := map[string]any{
  225. "type": "object",
  226. "description": "Agent configurations",
  227. "additionalProperties": map[string]any{
  228. "type": "object",
  229. "description": "Agent configuration",
  230. "properties": map[string]any{
  231. "model": map[string]any{
  232. "type": "string",
  233. "description": "Model ID for the agent",
  234. },
  235. "maxTokens": map[string]any{
  236. "type": "integer",
  237. "description": "Maximum tokens for the agent",
  238. "minimum": 1,
  239. },
  240. "reasoningEffort": map[string]any{
  241. "type": "string",
  242. "description": "Reasoning effort for models that support it (OpenAI, Anthropic)",
  243. "enum": []string{"low", "medium", "high"},
  244. },
  245. },
  246. "required": []string{"model"},
  247. },
  248. }
  249. // Add model enum
  250. modelEnum := []string{}
  251. for modelID := range models.SupportedModels {
  252. modelEnum = append(modelEnum, string(modelID))
  253. }
  254. agentSchema["additionalProperties"].(map[string]any)["properties"].(map[string]any)["model"].(map[string]any)["enum"] = modelEnum
  255. // Add specific agent properties
  256. agentProperties := map[string]any{}
  257. knownAgents := []string{
  258. string(config.AgentPrimary),
  259. string(config.AgentTask),
  260. string(config.AgentTitle),
  261. }
  262. for _, agentName := range knownAgents {
  263. agentProperties[agentName] = map[string]any{
  264. "$ref": "#/definitions/agent",
  265. }
  266. }
  267. // Create a combined schema that allows both specific agents and additional ones
  268. combinedAgentSchema := map[string]any{
  269. "type": "object",
  270. "description": "Agent configurations",
  271. "properties": agentProperties,
  272. "additionalProperties": agentSchema["additionalProperties"],
  273. }
  274. schema["properties"].(map[string]any)["agents"] = combinedAgentSchema
  275. schema["definitions"] = map[string]any{
  276. "agent": agentSchema["additionalProperties"],
  277. }
  278. // Add LSP configuration
  279. schema["properties"].(map[string]any)["lsp"] = map[string]any{
  280. "type": "object",
  281. "description": "Language Server Protocol configurations",
  282. "additionalProperties": map[string]any{
  283. "type": "object",
  284. "description": "LSP configuration for a language",
  285. "properties": map[string]any{
  286. "disabled": map[string]any{
  287. "type": "boolean",
  288. "description": "Whether the LSP is disabled",
  289. "default": false,
  290. },
  291. "command": map[string]any{
  292. "type": "string",
  293. "description": "Command to execute for the LSP server",
  294. },
  295. "args": map[string]any{
  296. "type": "array",
  297. "description": "Command arguments for the LSP server",
  298. "items": map[string]any{
  299. "type": "string",
  300. },
  301. },
  302. "options": map[string]any{
  303. "type": "object",
  304. "description": "Additional options for the LSP server",
  305. },
  306. },
  307. "required": []string{"command"},
  308. },
  309. }
  310. return schema
  311. }