added some guards

This commit is contained in:
Luxferre
2026-08-11 11:36:04 +03:00
parent d60798f1a3
commit f039985c6f
7 changed files with 117 additions and 20 deletions
+20 -1
View File
@@ -173,6 +173,11 @@ func sanitizeMessages(msgs []Message) {
}
}
func isInvalidAssistantErr(err error) bool {
s := err.Error()
return strings.Contains(s, "Invalid assistant message") || strings.Contains(s, "content or tool_calls must be set")
}
func llm(cfg *Cfg, msgs []Message, tools []map[string]any) (Message, error) {
sanitizeMessages(msgs)
p := map[string]any{"model": cfg.Model, "temperature": cfg.Temperature, "messages": msgs, "stream": cfg.Stream}
@@ -324,7 +329,21 @@ func AL(cfg *Cfg, msgs []Message, sp string, depth int) ([]Message, error) {
done := false
for i := 0; i < cfg.MaxALIterations && !done; i++ {
m, err := llm(cfg, msgs, TOOLS)
if err != nil { return msgs, err }
if err != nil {
if isInvalidAssistantErr(err) {
stripped := false
for j := len(msgs) - 1; j >= 0; j-- {
if msgs[j].Role == "assistant" {
fmt.Println(c("[stripped malformed assistant message]", 33))
msgs = append(msgs[:j], msgs[j+1:]...)
stripped = true
break
}
}
if stripped { continue }
}
return msgs, err
}
for j := range m.ToolCalls {
tc := &m.ToolCalls[j]
astr := tc.Function.Arguments