mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-06 08:02:14 +00:00
fix(cli): auto-heal missing reasoning blocks in responses history
- support pipe-delimited call_id lookup (callId|itemId) for pi coding agent - automatically backfill missing reasoning blocks before function_call in normalizeInputItems - fallback to recent reasoning proof in conversation history when cache is cold - prevents tool call deadlocks and infinite repetition in long-running agent loops
This commit is contained in:
1 parent
5d19ccffbc
commit
3de758e793
2 files changed
+74
-5
No files matched your search
@@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/chenyme/grok2api/backend/internal/infra/provider/conversation"
|
||||
)
|
||||
|
||||
// normalizeInputItems 将 Codex/Responses 扩展历史降级为 Grok Build 可接受的结构,
|
||||
@@ -34,6 +36,34 @@ func (c *responsesToolCompatibility) normalizeInputItems(items []any) ([]any, []
|
||||
c.changed = true
|
||||
rewritten = append(rewritten, converted)
|
||||
case "function_call":
|
||||
callID := strings.TrimSpace(stringField(item, "call_id"))
|
||||
if !hasPrecedingReasoningItem(rewritten) {
|
||||
lookupID := callID
|
||||
if strings.Contains(lookupID, "|") {
|
||||
lookupID = strings.Split(lookupID, "|")[0]
|
||||
}
|
||||
if reasoningItem, found := conversation.GetReasoningForCall(lookupID); found && reasoningItem.ID != "" {
|
||||
reasoningBlock := map[string]any{
|
||||
"type": "reasoning",
|
||||
"id": reasoningItem.ID,
|
||||
"status": "completed",
|
||||
}
|
||||
if reasoningItem.Encrypted != "" {
|
||||
reasoningBlock["encrypted_content"] = reasoningItem.Encrypted
|
||||
}
|
||||
if len(reasoningItem.Summary) > 0 {
|
||||
reasoningBlock["summary"] = reasoningItem.Summary
|
||||
}
|
||||
if len(reasoningItem.Content) > 0 {
|
||||
reasoningBlock["content"] = reasoningItem.Content
|
||||
}
|
||||
rewritten = append(rewritten, reasoningBlock)
|
||||
c.changed = true
|
||||
} else if fallback := findLastReasoningBlock(rewritten, items); fallback != nil {
|
||||
rewritten = append(rewritten, fallback)
|
||||
c.changed = true
|
||||
}
|
||||
}
|
||||
converted, err := c.normalizeFunctionCallInput(item, param)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
@@ -378,3 +408,38 @@ func encodeFunctionArguments(value any) (string, error) {
|
||||
}
|
||||
return string(encoded), nil
|
||||
}
|
||||
|
||||
func hasPrecedingReasoningItem(items []any) bool {
|
||||
if len(items) == 0 {
|
||||
return false
|
||||
}
|
||||
last, ok := items[len(items)-1].(map[string]any)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return stringField(last, "type") == "reasoning"
|
||||
}
|
||||
|
||||
func findLastReasoningBlock(rewritten []any, originalItems []any) map[string]any {
|
||||
// 优先在已重写的序列中倒序查找最近的合法 reasoning
|
||||
for i := len(rewritten) - 1; i >= 0; i-- {
|
||||
item, ok := rewritten[i].(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if stringField(item, "type") == "reasoning" && stringField(item, "encrypted_content") != "" {
|
||||
return cloneJSONObject(item)
|
||||
}
|
||||
}
|
||||
// 其次在原始输入序列中倒序查找
|
||||
for i := len(originalItems) - 1; i >= 0; i-- {
|
||||
item, ok := originalItems[i].(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if stringField(item, "type") == "reasoning" && stringField(item, "encrypted_content") != "" {
|
||||
return cloneJSONObject(item)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package conversation
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -99,13 +100,16 @@ func (c *ReasoningCache) Get(callID string) (responseItem, bool) {
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
entry, exists := c.entries[callID]
|
||||
if !exists {
|
||||
return responseItem{}, false
|
||||
if exists && time.Since(entry.createdAt) <= c.ttl {
|
||||
return entry.item, true
|
||||
}
|
||||
if time.Since(entry.createdAt) > c.ttl {
|
||||
return responseItem{}, false
|
||||
if strings.Contains(callID, "|") {
|
||||
prefix := strings.Split(callID, "|")[0]
|
||||
if entry, exists := c.entries[prefix]; exists && time.Since(entry.createdAt) <= c.ttl {
|
||||
return entry.item, true
|
||||
}
|
||||
}
|
||||
return entry.item, true
|
||||
return responseItem{}, false
|
||||
}
|
||||
|
||||
func (c *ReasoningCache) evictOldestLocked(now time.Time) {
|
||||
|
||||
Reference in new issue
Block a user