error reporting fixes
This commit is contained in:
+18
-3
@@ -133,7 +133,7 @@ func (s *Sidekick) runStep(ctx context.Context, history *[]Message,
|
||||
}
|
||||
msg, err := CallLLM(ctx, s.Model, *history, s.ToolRegistry, sc, rc)
|
||||
if err != nil {
|
||||
return false, "", fmt.Errorf("LLM call failed: %w", err)
|
||||
return false, "", err
|
||||
}
|
||||
s.logIntermediate(msg)
|
||||
*history = append(*history, msg)
|
||||
@@ -141,10 +141,11 @@ func (s *Sidekick) runStep(ctx context.Context, history *[]Message,
|
||||
act, params, parsed, err := s.parseAction(msg)
|
||||
if err != nil {
|
||||
if !parsed {
|
||||
// Not intended as JSON, just treat as RESPOND
|
||||
act, params = ActionTypeRespond, map[string]interface{}{"response": msg.Content}
|
||||
} else {
|
||||
*history = append(*history, Message{Role: MessageRoleUser, Content: fmt.Sprintf("Error: %v", err)})
|
||||
return false, "", nil
|
||||
// Intended as structured action but failed validation/parsing
|
||||
return false, "", fmt.Errorf("the AI returned an invalid response: %w", err)
|
||||
}
|
||||
}
|
||||
if act == ActionTypeRespond {
|
||||
@@ -184,6 +185,9 @@ func (s *Sidekick) dispatch(ctx context.Context, a ActionType, p map[string]inte
|
||||
}
|
||||
s.IntermediateHandler(fmt.Sprintf("Tool Result: %s", truncRes))
|
||||
}
|
||||
if tName == "read_file" || tName == "grep_file" {
|
||||
return res, nil
|
||||
}
|
||||
return BufferGate(res, tf, s.Config.ToolResponseThreshold)
|
||||
}
|
||||
if a == ActionTypeDelegate {
|
||||
@@ -202,12 +206,23 @@ func (s *Sidekick) parseAction(msg Message) (ActionType, map[string]interface{},
|
||||
}
|
||||
var env ActionEnvelope
|
||||
c := strings.TrimSpace(msg.Content)
|
||||
isProbablyJSON := strings.HasPrefix(c, "{")
|
||||
if strings.HasPrefix(c, "```json") && strings.HasSuffix(c, "```") {
|
||||
c = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(c, "```json"), "```"))
|
||||
isProbablyJSON = true
|
||||
}
|
||||
if err := json.Unmarshal([]byte(c), &env); err != nil {
|
||||
if isProbablyJSON {
|
||||
return "", nil, true, fmt.Errorf("malformed JSON action: %w", err)
|
||||
}
|
||||
return "", nil, false, err
|
||||
}
|
||||
if env.Action == "" {
|
||||
if isProbablyJSON {
|
||||
return "", nil, true, fmt.Errorf("AI response is missing the 'action' field")
|
||||
}
|
||||
return ActionTypeRespond, map[string]interface{}{"response": msg.Content}, false, nil
|
||||
}
|
||||
return env.Action, env.Params, true, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user