@@ -0,0 +1,401 @@
15+ SourcegraphToolName = "sourcegraph"
16+ sourcegraphToolDescription = `Search code across public repositories using Sourcegraph's GraphQL API.
18+WHEN TO USE THIS TOOL:
19+- Use when you need to find code examples or implementations across public repositories
20+- Helpful for researching how others have solved similar problems
21+- Useful for discovering patterns and best practices in open source code
24+- Provide a search query using Sourcegraph's query syntax
25+- Optionally specify the number of results to return (default: 10)
26+- Optionally set a timeout for the request
29+- Basic search: "fmt.Println" searches for exact matches
30+- File filters: "file:.go fmt.Println" limits to Go files
31+- Repository filters: "repo:^github\.com/golang/go$ fmt.Println" limits to specific repos
32+- Language filters: "lang:go fmt.Println" limits to Go code
33+- Boolean operators: "fmt.Println AND log.Fatal" for combined terms
34+- Regular expressions: "fmt\.(Print|Printf|Println)" for pattern matching
35+- Quoted strings: "\"exact phrase\"" for exact phrase matching
36+- Exclude filters: "-file:test" or "-repo:forks" to exclude matches
40+ * "repo:name" - Match repositories with name containing "name"
41+ * "repo:^github\.com/org/repo$" - Exact repository match
42+ * "repo:org/repo@branch" - Search specific branch
43+ * "repo:org/repo rev:branch" - Alternative branch syntax
44+ * "-repo:name" - Exclude repositories
45+ * "fork:yes" or "fork:only" - Include or only show forks
46+ * "archived:yes" or "archived:only" - Include or only show archived repos
47+ * "visibility:public" or "visibility:private" - Filter by visibility
50+ * "file:\.js$" - Files with .js extension
51+ * "file:internal/" - Files in internal directory
52+ * "-file:test" - Exclude test files
53+ * "file:has.content(Copyright)" - Files containing "Copyright"
54+ * "file:has.contributor([email protected])" - Files with specific contributor
57+ * "content:\"exact string\"" - Search for exact string
58+ * "-content:\"unwanted\"" - Exclude files with unwanted content
59+ * "case:yes" - Case-sensitive search
62+ * "type:symbol" - Search for symbols (functions, classes, etc.)
63+ * "type:file" - Search file content only
64+ * "type:path" - Search filenames only
65+ * "type:diff" - Search code changes
66+ * "type:commit" - Search commit messages
69+ * "after:\"1 month ago\"" - Commits after date
70+ * "before:\"2023-01-01\"" - Commits before date
71+ * "author:name" - Commits by author
72+ * "message:\"fix bug\"" - Commits with message
75+ * "select:repo" - Show only repository names
76+ * "select:file" - Show only file paths
77+ * "select:content" - Show only matching content
78+ * "select:symbol" - Show only matching symbols
81+ * "count:100" - Return up to 100 results
82+ * "count:all" - Return all results
83+ * "timeout:30s" - Set search timeout
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
94+- "term1 AND term2" - Results containing both terms
95+- "term1 OR term2" - Results containing either term
96+- "term1 NOT term2" - Results with term1 but not term2
97+- "term1 and (term2 or term3)" - Grouping with parentheses
100+- Only searches public repositories
101+- Rate limits may apply
102+- Complex queries may take longer to execute
103+- Maximum of 20 results per query
106+- Use specific file extensions to narrow results
107+- Add repo: filters for more targeted searches
108+- Use type:symbol to find function/method definitions
109+- Use type:file to find relevant files
110+- For more details on query syntax, visit: https://docs.sourcegraph.com/code_search/queries`
113+type SourcegraphParams struct {
114+ Query string `json:"query"`
115+ Count int `json:"count,omitempty"`
116+ Timeout int `json:"timeout,omitempty"`
119+type SourcegraphPermissionsParams struct {
120+ Query string `json:"query"`
121+ Count int `json:"count,omitempty"`
122+ Timeout int `json:"timeout,omitempty"`
125+type sourcegraphTool struct {
129+func NewSourcegraphTool() BaseTool {
130+ return &sourcegraphTool{
131+ client: &http.Client{
132+ Timeout: 30 * time.Second,
137+func (t *sourcegraphTool) Info() ToolInfo {
139+ Name: SourcegraphToolName,
140+ Description: sourcegraphToolDescription,
141+ Parameters: map[string]any{
142+ "query": map[string]any{
144+ "description": "The Sourcegraph search query",
146+ "count": map[string]any{
148+ "description": "Optional number of results to return (default: 10, max: 20)",
150+ "timeout": map[string]any{
152+ "description": "Optional timeout in seconds (max 120)",
155+ Required: []string{"query"},
159+func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
160+ var params SourcegraphParams
161+ if err := json.Unmarshal([]byte(call.Input), ¶ms); err != nil {
162+ return NewTextErrorResponse("Failed to parse sourcegraph parameters: " + err.Error()), nil
165+ if params.Query == "" {
166+ return NewTextErrorResponse("Query parameter is required"), nil
169+ // Set default count if not specified
170+ if params.Count <= 0 {
172+ } else if params.Count > 20 {
173+ params.Count = 20 // Limit to 20 results
177+ if params.Timeout > 0 {
178+ maxTimeout := 120 // 2 minutes
179+ if params.Timeout > maxTimeout {
180+ params.Timeout = maxTimeout
182+ client = &http.Client{
183+ Timeout: time.Duration(params.Timeout) * time.Second,
187+ // GraphQL query for Sourcegraph search
188+ // Create a properly escaped JSON structure
189+ type graphqlRequest struct {
190+ Query string `json:"query"`
192+ Query string `json:"query"`
193+ } `json:"variables"`
196+ request := graphqlRequest{
197+ Query: "query Search($query: String!) { search(query: $query, version: V2, patternType: standard ) { results { matchCount, limitHit, resultCount, approximateResultCount, missing { name }, timedout { name }, indexUnavailable, results { __typename, ... on FileMatch { repository { name }, file { path, url, content }, lineMatches { preview, lineNumber, offsetAndLengths } } } } } }",
199+ request.Variables.Query = params.Query
201+ // Marshal to JSON to ensure proper escaping
202+ graphqlQueryBytes, err := json.Marshal(request)
204+ return NewTextErrorResponse("Failed to create GraphQL request: " + err.Error()), nil
206+ graphqlQuery := string(graphqlQueryBytes)
208+ // Create request to Sourcegraph API
209+ req, err := http.NewRequestWithContext(
212+ "https://sourcegraph.com/.api/graphql",
213+ bytes.NewBuffer([]byte(graphqlQuery)),
216+ return NewTextErrorResponse("Failed to create request: " + err.Error()), nil
219+ req.Header.Set("Content-Type", "application/json")
220+ req.Header.Set("User-Agent", "termai/1.0")
222+ resp, err := client.Do(req)
224+ return NewTextErrorResponse("Failed to execute request: " + err.Error()), nil
226+ defer resp.Body.Close()
228+ if resp.StatusCode != http.StatusOK {
229+ // log the error response
230+ body, _ := io.ReadAll(resp.Body)
232+ return NewTextErrorResponse(fmt.Sprintf("Request failed with status code: %d, response: %s", resp.StatusCode, string(body))), nil
235+ return NewTextErrorResponse(fmt.Sprintf("Request failed with status code: %d", resp.StatusCode)), nil
237+ body, err := io.ReadAll(resp.Body)
239+ return NewTextErrorResponse("Failed to read response body: " + err.Error()), nil
242+ // Parse the GraphQL response
243+ var result map[string]any
244+ if err = json.Unmarshal(body, &result); err != nil {
245+ return NewTextErrorResponse("Failed to parse response: " + err.Error()), nil
248+ // Format the results in a readable way
249+ formattedResults, err := formatSourcegraphResults(result)
251+ return NewTextErrorResponse("Failed to format results: " + err.Error()), nil
254+ return NewTextResponse(formattedResults), nil
257+func formatSourcegraphResults(result map[string]any) (string, error) {
258+ var buffer strings.Builder
260+ // Check for errors in the GraphQL response
261+ if errors, ok := result["errors"].([]any); ok && len(errors) > 0 {
262+ buffer.WriteString("## Sourcegraph API Error\n\n")
263+ for _, err := range errors {
264+ if errMap, ok := err.(map[string]any); ok {
265+ if message, ok := errMap["message"].(string); ok {
266+ buffer.WriteString(fmt.Sprintf("- %s\n", message))
270+ return buffer.String(), nil
273+ // Extract data from the response
274+ data, ok := result["data"].(map[string]any)
276+ return "", fmt.Errorf("invalid response format: missing data field")
279+ search, ok := data["search"].(map[string]any)
281+ return "", fmt.Errorf("invalid response format: missing search field")
284+ searchResults, ok := search["results"].(map[string]any)
286+ return "", fmt.Errorf("invalid response format: missing results field")
289+ // Write search metadata
290+ matchCount, _ := searchResults["matchCount"].(float64)
291+ resultCount, _ := searchResults["resultCount"].(float64)
292+ limitHit, _ := searchResults["limitHit"].(bool)
294+ buffer.WriteString("# Sourcegraph Search Results\n\n")
295+ buffer.WriteString(fmt.Sprintf("Found %d matches across %d results\n", int(matchCount), int(resultCount)))
298+ buffer.WriteString("(Result limit reached, try a more specific query)\n")
301+ buffer.WriteString("\n")
304+ results, ok := searchResults["results"].([]any)
305+ if !ok || len(results) == 0 {
306+ buffer.WriteString("No results found. Try a different query.\n")
307+ return buffer.String(), nil
310+ // Limit to 10 results
312+ if len(results) > maxResults {
313+ results = results[:maxResults]
316+ // Process each result
317+ for i, res := range results {
318+ fileMatch, ok := res.(map[string]any)
323+ // Skip non-FileMatch results
324+ typeName, _ := fileMatch["__typename"].(string)
325+ if typeName != "FileMatch" {
329+ // Extract repository and file information
330+ repo, _ := fileMatch["repository"].(map[string]any)
331+ file, _ := fileMatch["file"].(map[string]any)
332+ lineMatches, _ := fileMatch["lineMatches"].([]any)
334+ if repo == nil || file == nil {
338+ repoName, _ := repo["name"].(string)
339+ filePath, _ := file["path"].(string)
340+ fileURL, _ := file["url"].(string)
341+ fileContent, _ := file["content"].(string)
343+ buffer.WriteString(fmt.Sprintf("## Result %d: %s/%s\n\n", i+1, repoName, filePath))
346+ buffer.WriteString(fmt.Sprintf("URL: %s\n\n", fileURL))
349+ // Show line matches with context
350+ if len(lineMatches) > 0 {
351+ for _, lm := range lineMatches {
352+ lineMatch, ok := lm.(map[string]any)
357+ lineNumber, _ := lineMatch["lineNumber"].(float64)
358+ preview, _ := lineMatch["preview"].(string)
360+ // Extract context from file content if available
361+ if fileContent != "" {
362+ lines := strings.Split(fileContent, "\n")
364+ buffer.WriteString("```\n")
366+ // Display context before the match (up to 10 lines)
368+ startLine := max(1, int(lineNumber)-contextBefore)
370+ for j := startLine - 1; j < int(lineNumber)-1 && j < len(lines); j++ {
372+ buffer.WriteString(fmt.Sprintf("%d| %s\n", j+1, lines[j]))
376+ // Display the matching line (highlighted)
377+ buffer.WriteString(fmt.Sprintf("%d| %s\n", int(lineNumber), preview))
379+ // Display context after the match (up to 10 lines)
381+ endLine := int(lineNumber) + contextAfter
383+ for j := int(lineNumber); j < endLine && j < len(lines); j++ {
385+ buffer.WriteString(fmt.Sprintf("%d| %s\n", j+1, lines[j]))
389+ buffer.WriteString("```\n\n")
391+ // If file content is not available, just show the preview
392+ buffer.WriteString("```\n")
393+ buffer.WriteString(fmt.Sprintf("%d| %s\n", int(lineNumber), preview))
394+ buffer.WriteString("```\n\n")
400+ return buffer.String(), nil