main.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "github.com/kujtimiihoxha/opencode/internal/config"
  7. "github.com/kujtimiihoxha/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. // Add MCP servers
  70. schema["properties"].(map[string]any)["mcpServers"] = map[string]any{
  71. "type": "object",
  72. "description": "Model Control Protocol server configurations",
  73. "additionalProperties": map[string]any{
  74. "type": "object",
  75. "description": "MCP server configuration",
  76. "properties": map[string]any{
  77. "command": map[string]any{
  78. "type": "string",
  79. "description": "Command to execute for the MCP server",
  80. },
  81. "env": map[string]any{
  82. "type": "array",
  83. "description": "Environment variables for the MCP server",
  84. "items": map[string]any{
  85. "type": "string",
  86. },
  87. },
  88. "args": map[string]any{
  89. "type": "array",
  90. "description": "Command arguments for the MCP server",
  91. "items": map[string]any{
  92. "type": "string",
  93. },
  94. },
  95. "type": map[string]any{
  96. "type": "string",
  97. "description": "Type of MCP server",
  98. "enum": []string{"stdio", "sse"},
  99. "default": "stdio",
  100. },
  101. "url": map[string]any{
  102. "type": "string",
  103. "description": "URL for SSE type MCP servers",
  104. },
  105. "headers": map[string]any{
  106. "type": "object",
  107. "description": "HTTP headers for SSE type MCP servers",
  108. "additionalProperties": map[string]any{
  109. "type": "string",
  110. },
  111. },
  112. },
  113. "required": []string{"command"},
  114. },
  115. }
  116. // Add providers
  117. providerSchema := map[string]any{
  118. "type": "object",
  119. "description": "LLM provider configurations",
  120. "additionalProperties": map[string]any{
  121. "type": "object",
  122. "description": "Provider configuration",
  123. "properties": map[string]any{
  124. "apiKey": map[string]any{
  125. "type": "string",
  126. "description": "API key for the provider",
  127. },
  128. "disabled": map[string]any{
  129. "type": "boolean",
  130. "description": "Whether the provider is disabled",
  131. "default": false,
  132. },
  133. },
  134. },
  135. }
  136. // Add known providers
  137. knownProviders := []string{
  138. string(models.ProviderAnthropic),
  139. string(models.ProviderOpenAI),
  140. string(models.ProviderGemini),
  141. string(models.ProviderGROQ),
  142. string(models.ProviderBedrock),
  143. }
  144. providerSchema["additionalProperties"].(map[string]any)["properties"].(map[string]any)["provider"] = map[string]any{
  145. "type": "string",
  146. "description": "Provider type",
  147. "enum": knownProviders,
  148. }
  149. schema["properties"].(map[string]any)["providers"] = providerSchema
  150. // Add agents
  151. agentSchema := map[string]any{
  152. "type": "object",
  153. "description": "Agent configurations",
  154. "additionalProperties": map[string]any{
  155. "type": "object",
  156. "description": "Agent configuration",
  157. "properties": map[string]any{
  158. "model": map[string]any{
  159. "type": "string",
  160. "description": "Model ID for the agent",
  161. },
  162. "maxTokens": map[string]any{
  163. "type": "integer",
  164. "description": "Maximum tokens for the agent",
  165. "minimum": 1,
  166. },
  167. "reasoningEffort": map[string]any{
  168. "type": "string",
  169. "description": "Reasoning effort for models that support it (OpenAI, Anthropic)",
  170. "enum": []string{"low", "medium", "high"},
  171. },
  172. },
  173. "required": []string{"model"},
  174. },
  175. }
  176. // Add model enum
  177. modelEnum := []string{}
  178. for modelID := range models.SupportedModels {
  179. modelEnum = append(modelEnum, string(modelID))
  180. }
  181. agentSchema["additionalProperties"].(map[string]any)["properties"].(map[string]any)["model"].(map[string]any)["enum"] = modelEnum
  182. // Add specific agent properties
  183. agentProperties := map[string]any{}
  184. knownAgents := []string{
  185. string(config.AgentCoder),
  186. string(config.AgentTask),
  187. string(config.AgentTitle),
  188. }
  189. for _, agentName := range knownAgents {
  190. agentProperties[agentName] = map[string]any{
  191. "$ref": "#/definitions/agent",
  192. }
  193. }
  194. // Create a combined schema that allows both specific agents and additional ones
  195. combinedAgentSchema := map[string]any{
  196. "type": "object",
  197. "description": "Agent configurations",
  198. "properties": agentProperties,
  199. "additionalProperties": agentSchema["additionalProperties"],
  200. }
  201. schema["properties"].(map[string]any)["agents"] = combinedAgentSchema
  202. schema["definitions"] = map[string]any{
  203. "agent": agentSchema["additionalProperties"],
  204. }
  205. // Add LSP configuration
  206. schema["properties"].(map[string]any)["lsp"] = map[string]any{
  207. "type": "object",
  208. "description": "Language Server Protocol configurations",
  209. "additionalProperties": map[string]any{
  210. "type": "object",
  211. "description": "LSP configuration for a language",
  212. "properties": map[string]any{
  213. "disabled": map[string]any{
  214. "type": "boolean",
  215. "description": "Whether the LSP is disabled",
  216. "default": false,
  217. },
  218. "command": map[string]any{
  219. "type": "string",
  220. "description": "Command to execute for the LSP server",
  221. },
  222. "args": map[string]any{
  223. "type": "array",
  224. "description": "Command arguments for the LSP server",
  225. "items": map[string]any{
  226. "type": "string",
  227. },
  228. },
  229. "options": map[string]any{
  230. "type": "object",
  231. "description": "Additional options for the LSP server",
  232. },
  233. },
  234. "required": []string{"command"},
  235. },
  236. }
  237. return schema
  238. }