handle errors correctly in the other tools

This commit is contained in:
Kujtim Hoxha 2025-04-14 11:24:36 +02:00
parent 921f5ee5bd
commit 0b3e5f5bd4
8 changed files with 72 additions and 26 deletions

View file

@ -18,6 +18,11 @@ type SourcegraphParams struct {
Timeout int `json:"timeout,omitempty"`
}
type SourcegraphMetadata struct {
NumberOfMatches int `json:"number_of_matches"`
Truncated bool `json:"truncated"`
}
type sourcegraphTool struct {
client *http.Client
}
@ -198,7 +203,7 @@ func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
graphqlQueryBytes, err := json.Marshal(request)
if err != nil {
return NewTextErrorResponse("Failed to create GraphQL request: " + err.Error()), nil
return ToolResponse{}, fmt.Errorf("failed to marshal GraphQL request: %w", err)
}
graphqlQuery := string(graphqlQueryBytes)
@ -209,7 +214,7 @@ func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
bytes.NewBuffer([]byte(graphqlQuery)),
)
if err != nil {
return NewTextErrorResponse("Failed to create request: " + err.Error()), nil
return ToolResponse{}, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
@ -217,7 +222,7 @@ func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
resp, err := client.Do(req)
if err != nil {
return NewTextErrorResponse("Failed to execute request: " + err.Error()), nil
return ToolResponse{}, fmt.Errorf("failed to fetch URL: %w", err)
}
defer resp.Body.Close()
@ -231,12 +236,12 @@ func (t *sourcegraphTool) Run(ctx context.Context, call ToolCall) (ToolResponse,
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return NewTextErrorResponse("Failed to read response body: " + err.Error()), nil
return ToolResponse{}, fmt.Errorf("failed to read response body: %w", err)
}
var result map[string]any
if err = json.Unmarshal(body, &result); err != nil {
return NewTextErrorResponse("Failed to parse response: " + err.Error()), nil
return ToolResponse{}, fmt.Errorf("failed to unmarshal response: %w", err)
}
formattedResults, err := formatSourcegraphResults(result, params.ContextWindow)