superagent请求问题修复
This commit is contained in:
1 parent
ee9d2d7cf1
commit
7243319bbb
19 files changed
+388
-74
No files matched your search
@@ -28,13 +28,16 @@ var (
|
||||
)
|
||||
|
||||
type streamState struct {
|
||||
sessionID string
|
||||
sessionID string
|
||||
requireRunCompleted bool
|
||||
|
||||
endSeen bool
|
||||
runCompleted bool
|
||||
runID string
|
||||
runURL string
|
||||
lastEventID string
|
||||
endSeen bool
|
||||
runCompleted bool
|
||||
finalAIStopSeen bool
|
||||
topLevelFinalSeen bool
|
||||
runID string
|
||||
runURL string
|
||||
lastEventID string
|
||||
|
||||
profileID string
|
||||
profileVersionID string
|
||||
@@ -56,11 +59,12 @@ type streamState struct {
|
||||
streamBytes int64
|
||||
}
|
||||
|
||||
func newStreamState(sessionID string) *streamState {
|
||||
func newStreamState(sessionID string, requireRunCompleted bool) *streamState {
|
||||
return &streamState{
|
||||
sessionID: sessionID,
|
||||
processedIDs: make(map[string]struct{}),
|
||||
eventTypeSet: make(map[string]struct{}),
|
||||
sessionID: sessionID,
|
||||
requireRunCompleted: requireRunCompleted,
|
||||
processedIDs: make(map[string]struct{}),
|
||||
eventTypeSet: make(map[string]struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,6 +195,21 @@ func (s *streamState) consumeFrame(frame sseFrame, traceHandler TraceHandler) er
|
||||
return err
|
||||
}
|
||||
return s.consumeMessages(payload)
|
||||
case "message.final":
|
||||
payload, err := decodeJSONObject(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
text := stringValue(payload, "text")
|
||||
if strings.TrimSpace(text) == "" {
|
||||
return nil
|
||||
}
|
||||
if len(text) > maxAnswerBytes {
|
||||
return fmt.Errorf("%w: final answer exceeds size limit", ErrProtocol)
|
||||
}
|
||||
s.runID = valueOrExisting(safeValue(stringValue(payload, "run_id")), s.runID)
|
||||
s.finalContent = text
|
||||
s.topLevelFinalSeen = true
|
||||
case "values":
|
||||
payload, err := decodeJSONObject(data)
|
||||
if err != nil {
|
||||
@@ -277,16 +296,10 @@ func (s *streamState) consumeMessages(value any) error {
|
||||
if nested, exists := current["messages"]; exists {
|
||||
return s.consumeMessages(nested)
|
||||
}
|
||||
if stringValue(current, "type") != "ai" {
|
||||
messageType := stringValue(current, "type")
|
||||
if messageType != "ai" && messageType != "AIMessageChunk" {
|
||||
return nil
|
||||
}
|
||||
content := contentText(current["content"])
|
||||
if strings.TrimSpace(content) == "" {
|
||||
return nil
|
||||
}
|
||||
if len(content) > maxAnswerBytes {
|
||||
return fmt.Errorf("%w: final answer exceeds size limit", ErrProtocol)
|
||||
}
|
||||
responseMetadata, _ := current["response_metadata"].(map[string]any)
|
||||
usageMetadata, _ := current["usage_metadata"].(map[string]any)
|
||||
modelName := valueOrExisting(safeTrace(stringValue(responseMetadata, "model_name")), s.modelName)
|
||||
@@ -295,10 +308,22 @@ func (s *streamState) consumeMessages(value any) error {
|
||||
Output: int64Value(usageMetadata, "output_tokens"),
|
||||
Total: int64Value(usageMetadata, "total_tokens"),
|
||||
}
|
||||
if stringValue(responseMetadata, "finish_reason") == "stop" {
|
||||
s.finalContent = content
|
||||
stopSeen := stringValue(responseMetadata, "finish_reason") == "stop"
|
||||
if stopSeen {
|
||||
s.finalAIStopSeen = true
|
||||
s.modelName = modelName
|
||||
s.usage = usage
|
||||
}
|
||||
|
||||
content := contentText(current["content"])
|
||||
if strings.TrimSpace(content) == "" {
|
||||
return nil
|
||||
}
|
||||
if len(content) > maxAnswerBytes {
|
||||
return fmt.Errorf("%w: final answer exceeds size limit", ErrProtocol)
|
||||
}
|
||||
if stopSeen {
|
||||
s.finalContent = content
|
||||
return nil
|
||||
}
|
||||
s.fallbackContent = content
|
||||
@@ -315,14 +340,20 @@ func (s *streamState) result() (Result, error) {
|
||||
if !s.endSeen {
|
||||
return Result{}, fmt.Errorf("%w: missing end event", ErrProtocol)
|
||||
}
|
||||
if !s.runCompleted {
|
||||
if s.requireRunCompleted && !s.runCompleted {
|
||||
return Result{}, fmt.Errorf("%w: missing successful run.completed event", ErrProtocol)
|
||||
}
|
||||
if !s.requireRunCompleted && !s.finalAIStopSeen {
|
||||
return Result{}, fmt.Errorf("%w: missing AI stop finish reason", ErrProtocol)
|
||||
}
|
||||
if !s.requireRunCompleted && !s.topLevelFinalSeen {
|
||||
return Result{}, fmt.Errorf("%w: missing message.final event", ErrProtocol)
|
||||
}
|
||||
answer := s.finalContent
|
||||
if strings.TrimSpace(answer) == "" {
|
||||
if s.requireRunCompleted && strings.TrimSpace(answer) == "" {
|
||||
answer = s.deltaContent.String()
|
||||
}
|
||||
if strings.TrimSpace(answer) == "" {
|
||||
if s.requireRunCompleted && strings.TrimSpace(answer) == "" {
|
||||
answer = s.fallbackContent
|
||||
s.modelName = s.fallbackModel
|
||||
s.usage = s.fallbackUsage
|
||||
|
||||
Reference in new issue
Block a user