Spaces:
Paused
Paused
File size: 1,806 Bytes
48d903a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package filter
import "strings"
type ThinkingFilter struct {
HasSeenFirstThinking bool
Buffer string
LastOutputChunk string
LastPhase string
ThinkingRoundCount int
}
func (f *ThinkingFilter) ProcessThinking(deltaContent string) string {
if !f.HasSeenFirstThinking {
f.HasSeenFirstThinking = true
if idx := strings.Index(deltaContent, "> "); idx != -1 {
deltaContent = deltaContent[idx+2:]
} else {
return ""
}
}
content := f.Buffer + deltaContent
f.Buffer = ""
content = strings.ReplaceAll(content, "\n> ", "\n")
if strings.HasSuffix(content, "\n>") {
f.Buffer = "\n>"
return content[:len(content)-2]
}
if strings.HasSuffix(content, "\n") {
f.Buffer = "\n"
return content[:len(content)-1]
}
return content
}
func (f *ThinkingFilter) Flush() string {
result := f.Buffer
f.Buffer = ""
return result
}
func (f *ThinkingFilter) ExtractCompleteThinking(editContent string) string {
startIdx := strings.Index(editContent, "> ")
if startIdx == -1 {
return ""
}
startIdx += 2
endIdx := strings.Index(editContent, "\n</details>")
if endIdx == -1 {
return ""
}
content := editContent[startIdx:endIdx]
content = strings.ReplaceAll(content, "\n> ", "\n")
return content
}
func (f *ThinkingFilter) ExtractIncrementalThinking(editContent string) string {
completeThinking := f.ExtractCompleteThinking(editContent)
if completeThinking == "" {
return ""
}
if f.LastOutputChunk == "" {
return completeThinking
}
idx := strings.Index(completeThinking, f.LastOutputChunk)
if idx == -1 {
return completeThinking
}
incrementalPart := completeThinking[idx+len(f.LastOutputChunk):]
return incrementalPart
}
func (f *ThinkingFilter) ResetForNewRound() {
f.LastOutputChunk = ""
f.HasSeenFirstThinking = false
}
|