main.go 8.6 KB

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