sourcegraph.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package tools
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. type SourcegraphParams struct {
  13. Query string `json:"query"`
  14. Count int `json:"count,omitempty"`
  15. ContextWindow int `json:"context_window,omitempty"`
  16. Timeout int `json:"timeout,omitempty"`
  17. }
  18. type SourcegraphResponseMetadata struct {
  19. NumberOfMatches int `json:"number_of_matches"`
  20. Truncated bool `json:"truncated"`
  21. }
  22. type sourcegraphTool struct {
  23. client *http.Client
  24. }
  25. const (
  26. SourcegraphToolName = "sourcegraph"
  27. sourcegraphToolDescription = `Search code across public repositories using Sourcegraph's GraphQL API.
  28. WHEN TO USE THIS TOOL:
  29. - Use when you need to find code examples or implementations across public repositories
  30. - Helpful for researching how others have solved similar problems
  31. - Useful for discovering patterns and best practices in open source code
  32. HOW TO USE:
  33. - Provide a search query using Sourcegraph's query syntax
  34. - Optionally specify the number of results to return (default: 10)
  35. - Optionally set a timeout for the request
  36. QUERY SYNTAX:
  37. - Basic search: "fmt.Println" searches for exact matches
  38. - File filters: "file:.go fmt.Println" limits to Go files
  39. - Repository filters: "repo:^github\.com/golang/go$ fmt.Println" limits to specific repos
  40. - Language filters: "lang:go fmt.Println" limits to Go code
  41. - Boolean operators: "fmt.Println AND log.Fatal" for combined terms
  42. - Regular expressions: "fmt\.(Print|Printf|Println)" for pattern matching
  43. - Quoted strings: "\"exact phrase\"" for exact phrase matching
  44. - Exclude filters: "-file:test" or "-repo:forks" to exclude matches
  45. ADVANCED FILTERS:
  46. - Repository filters:
  47. * "repo:name" - Match repositories with name containing "name"
  48. * "repo:^github\.com/org/repo$" - Exact repository match
  49. * "repo:org/repo@branch" - Search specific branch
  50. * "repo:org/repo rev:branch" - Alternative branch syntax
  51. * "-repo:name" - Exclude repositories
  52. * "fork:yes" or "fork:only" - Include or only show forks
  53. * "archived:yes" or "archived:only" - Include or only show archived repos
  54. * "visibility:public" or "visibility:private" - Filter by visibility
  55. - File filters:
  56. * "file:\.js$" - Files with .js extension
  57. * "file:internal/" - Files in internal directory
  58. * "-file:test" - Exclude test files
  59. * "file:has.content(Copyright)" - Files containing "Copyright"
  60. * "file:has.contributor([email protected])" - Files with specific contributor
  61. - Content filters:
  62. * "content:\"exact string\"" - Search for exact string
  63. * "-content:\"unwanted\"" - Exclude files with unwanted content
  64. * "case:yes" - Case-sensitive search
  65. - Type filters:
  66. * "type:symbol" - Search for symbols (functions, classes, etc.)
  67. * "type:file" - Search file content only
  68. * "type:path" - Search filenames only
  69. * "type:diff" - Search code changes
  70. * "type:commit" - Search commit messages
  71. - Commit/diff search:
  72. * "after:\"1 month ago\"" - Commits after date
  73. * "before:\"2023-01-01\"" - Commits before date
  74. * "author:name" - Commits by author
  75. * "message:\"fix bug\"" - Commits with message
  76. - Result selection:
  77. * "select:repo" - Show only repository names
  78. * "select:file" - Show only file paths
  79. * "select:content" - Show only matching content
  80. * "select:symbol" - Show only matching symbols
  81. - Result control:
  82. * "count:100" - Return up to 100 results
  83. * "count:all" - Return all results
  84. * "timeout:30s" - Set search timeout
  85. EXAMPLES:
  86. - "file:.go context.WithTimeout" - Find Go code using context.WithTimeout
  87. - "lang:typescript useState type:symbol" - Find TypeScript React useState hooks
  88. - "repo:^github\.com/kubernetes/kubernetes$ pod list type:file" - Find Kubernetes files related to pod listing
  89. - "repo:sourcegraph/sourcegraph$ after:\"3 months ago\" type:diff database" - Recent changes to database code
  90. - "file:Dockerfile (alpine OR ubuntu) -content:alpine:latest" - Dockerfiles with specific base images
  91. - "repo:has.path(\.py) file:requirements.txt tensorflow" - Python projects using TensorFlow
  92. BOOLEAN OPERATORS:
  93. - "term1 AND term2" - Results containing both terms
  94. - "term1 OR term2" - Results containing either term
  95. - "term1 NOT term2" - Results with term1 but not term2
  96. - "term1 and (term2 or term3)" - Grouping with parentheses
  97. LIMITATIONS:
  98. - Only searches public repositories
  99. - Rate limits may apply
  100. - Complex queries may take longer to execute
  101. - Maximum of 20 results per query
  102. TIPS:
  103. - Use specific file extensions to narrow results
  104. - Add repo: filters for more targeted searches
  105. - Use type:symbol to find function/method definitions
  106. - Use type:file to find relevant files`
  107. )
  108. func NewSourcegraphTool() BaseTool {
  109. return &sourcegraphTool{
  110. client: &http.Client{
  111. Timeout: 30 * time.Second,
  112. },
  113. }
  114. }
  115. func (t *sourcegraphTool) Info() ToolInfo {
  116. return ToolInfo{
  117. Name: SourcegraphToolName,
  118. Description: sourcegraphToolDescription,
  119. Parameters: map[string]any{
  120. "query": map[string]any{
  121. "type": "string",
  122. "description": "The Sourcegraph search query",
  123. },
  124. "count": map[string]any{
  125. "type": "number",
  126. "description": "Optional number of results to return (default: 10, max: 20)",
  127. },
  128. "context_window": map[string]any{
  129. "type": "number",
  130. "description": "The context around the match to return (default: 10 lines)",
  131. },
  132. "timeout": map[string]any{
  133. "type": "number",
  134. "description": "Optional timeout in seconds (max 120)",
  135. },
  136. },
  137. Required: []string{"query"},
  138. }
  139. }
  140. func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
  141. var params SourcegraphParams
  142. if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
  143. return NewTextErrorResponse("Failed to parse sourcegraph parameters: " + err.Error()), nil
  144. }
  145. if params.Query == "" {
  146. return NewTextErrorResponse("Query parameter is required"), nil
  147. }
  148. if params.Count <= 0 {
  149. params.Count = 10
  150. } else if params.Count > 20 {
  151. params.Count = 20 // Limit to 20 results
  152. }
  153. if params.ContextWindow <= 0 {
  154. params.ContextWindow = 10 // Default context window
  155. }
  156. client := t.client
  157. if params.Timeout > 0 {
  158. maxTimeout := 120 // 2 minutes
  159. if params.Timeout > maxTimeout {
  160. params.Timeout = maxTimeout
  161. }
  162. client = &http.Client{
  163. Timeout: time.Duration(params.Timeout) * time.Second,
  164. }
  165. }
  166. type graphqlRequest struct {
  167. Query string `json:"query"`
  168. Variables struct {
  169. Query string `json:"query"`
  170. } `json:"variables"`
  171. }
  172. request := graphqlRequest{
  173. Query: "query Search($query: String!) { search(query: $query, version: V2, patternType: keyword ) { results { matchCount, limitHit, resultCount, approximateResultCount, missing { name }, timedout { name }, indexUnavailable, results { __typename, ... on FileMatch { repository { name }, file { path, url, content }, lineMatches { preview, lineNumber, offsetAndLengths } } } } } }",
  174. }
  175. request.Variables.Query = params.Query
  176. graphqlQueryBytes, err := json.Marshal(request)
  177. if err != nil {
  178. return ToolResponse{}, fmt.Errorf("failed to marshal GraphQL request: %w", err)
  179. }
  180. graphqlQuery := string(graphqlQueryBytes)
  181. req, err := http.NewRequestWithContext(
  182. ctx,
  183. "POST",
  184. "https://sourcegraph.com/.api/graphql",
  185. bytes.NewBuffer([]byte(graphqlQuery)),
  186. )
  187. if err != nil {
  188. return ToolResponse{}, fmt.Errorf("failed to create request: %w", err)
  189. }
  190. req.Header.Set("Content-Type", "application/json")
  191. req.Header.Set("User-Agent", "opencode/1.0")
  192. resp, err := client.Do(req)
  193. if err != nil {
  194. return ToolResponse{}, fmt.Errorf("failed to fetch URL: %w", err)
  195. }
  196. defer resp.Body.Close()
  197. if resp.StatusCode != http.StatusOK {
  198. body, _ := io.ReadAll(resp.Body)
  199. if len(body) > 0 {
  200. return NewTextErrorResponse(fmt.Sprintf("Request failed with status code: %d, response: %s", resp.StatusCode, string(body))), nil
  201. }
  202. return NewTextErrorResponse(fmt.Sprintf("Request failed with status code: %d", resp.StatusCode)), nil
  203. }
  204. body, err := io.ReadAll(resp.Body)
  205. if err != nil {
  206. return ToolResponse{}, fmt.Errorf("failed to read response body: %w", err)
  207. }
  208. var result map[string]any
  209. if err = json.Unmarshal(body, &result); err != nil {
  210. return ToolResponse{}, fmt.Errorf("failed to unmarshal response: %w", err)
  211. }
  212. formattedResults, err := formatSourcegraphResults(result, params.ContextWindow)
  213. if err != nil {
  214. return NewTextErrorResponse("Failed to format results: " + err.Error()), nil
  215. }
  216. return NewTextResponse(formattedResults), nil
  217. }
  218. func formatSourcegraphResults(result map[string]any, contextWindow int) (string, error) {
  219. var buffer strings.Builder
  220. if errors, ok := result["errors"].([]any); ok && len(errors) > 0 {
  221. buffer.WriteString("## Sourcegraph API Error\n\n")
  222. for _, err := range errors {
  223. if errMap, ok := err.(map[string]any); ok {
  224. if message, ok := errMap["message"].(string); ok {
  225. buffer.WriteString(fmt.Sprintf("- %s\n", message))
  226. }
  227. }
  228. }
  229. return buffer.String(), nil
  230. }
  231. data, ok := result["data"].(map[string]any)
  232. if !ok {
  233. return "", fmt.Errorf("invalid response format: missing data field")
  234. }
  235. search, ok := data["search"].(map[string]any)
  236. if !ok {
  237. return "", fmt.Errorf("invalid response format: missing search field")
  238. }
  239. searchResults, ok := search["results"].(map[string]any)
  240. if !ok {
  241. return "", fmt.Errorf("invalid response format: missing results field")
  242. }
  243. matchCount, _ := searchResults["matchCount"].(float64)
  244. resultCount, _ := searchResults["resultCount"].(float64)
  245. limitHit, _ := searchResults["limitHit"].(bool)
  246. buffer.WriteString("# Sourcegraph Search Results\n\n")
  247. buffer.WriteString(fmt.Sprintf("Found %d matches across %d results\n", int(matchCount), int(resultCount)))
  248. if limitHit {
  249. buffer.WriteString("(Result limit reached, try a more specific query)\n")
  250. }
  251. buffer.WriteString("\n")
  252. results, ok := searchResults["results"].([]any)
  253. if !ok || len(results) == 0 {
  254. buffer.WriteString("No results found. Try a different query.\n")
  255. return buffer.String(), nil
  256. }
  257. maxResults := 10
  258. if len(results) > maxResults {
  259. results = results[:maxResults]
  260. }
  261. for i, res := range results {
  262. fileMatch, ok := res.(map[string]any)
  263. if !ok {
  264. continue
  265. }
  266. typeName, _ := fileMatch["__typename"].(string)
  267. if typeName != "FileMatch" {
  268. continue
  269. }
  270. repo, _ := fileMatch["repository"].(map[string]any)
  271. file, _ := fileMatch["file"].(map[string]any)
  272. lineMatches, _ := fileMatch["lineMatches"].([]any)
  273. if repo == nil || file == nil {
  274. continue
  275. }
  276. repoName, _ := repo["name"].(string)
  277. filePath, _ := file["path"].(string)
  278. fileURL, _ := file["url"].(string)
  279. fileContent, _ := file["content"].(string)
  280. buffer.WriteString(fmt.Sprintf("## Result %d: %s/%s\n\n", i+1, repoName, filePath))
  281. if fileURL != "" {
  282. buffer.WriteString(fmt.Sprintf("URL: %s\n\n", fileURL))
  283. }
  284. if len(lineMatches) > 0 {
  285. for _, lm := range lineMatches {
  286. lineMatch, ok := lm.(map[string]any)
  287. if !ok {
  288. continue
  289. }
  290. lineNumber, _ := lineMatch["lineNumber"].(float64)
  291. preview, _ := lineMatch["preview"].(string)
  292. if fileContent != "" {
  293. lines := strings.Split(fileContent, "\n")
  294. buffer.WriteString("```\n")
  295. startLine := max(1, int(lineNumber)-contextWindow)
  296. for j := startLine - 1; j < int(lineNumber)-1 && j < len(lines); j++ {
  297. if j >= 0 {
  298. buffer.WriteString(fmt.Sprintf("%d| %s\n", j+1, lines[j]))
  299. }
  300. }
  301. buffer.WriteString(fmt.Sprintf("%d| %s\n", int(lineNumber), preview))
  302. endLine := int(lineNumber) + contextWindow
  303. for j := int(lineNumber); j < endLine && j < len(lines); j++ {
  304. if j < len(lines) {
  305. buffer.WriteString(fmt.Sprintf("%d| %s\n", j+1, lines[j]))
  306. }
  307. }
  308. buffer.WriteString("```\n\n")
  309. } else {
  310. buffer.WriteString("```\n")
  311. buffer.WriteString(fmt.Sprintf("%d| %s\n", int(lineNumber), preview))
  312. buffer.WriteString("```\n\n")
  313. }
  314. }
  315. }
  316. }
  317. return buffer.String(), nil
  318. }