Files
ds2api/internal/sse/dedupe.go
d407ccb773 perf(streaming): optimize TTFT and reduce buffering latency
Core changes:
- stream.go: New accumulation buffer architecture with scanner goroutine
  + select loop, MinChars=16, MaxWait=10ms, first-flush-immediate
- dedupe.go: Add TrimContinuationOverlapFromBuilder to avoid string copies
- claude/stream_runtime_core.go: Integrate toolstream for incremental text
- claude/stream_runtime_finalize.go: toolstream flush support
- stream_emitter.js: Reduce DeltaCoalescer thresholds (160->16 chars, 80->20ms)
- empty_retry: Add thinking-aware empty output detection
- Fix reasoning_content leak and finish_reason=null in edge cases
- Fix tail content truncation when max_tokens exceeded

Tests: sync test expectations with upstream for thinking content
2026-05-02 20:28:30 +08:00

55 lines
1.1 KiB
Go

package sse
import "strings"
const minContinuationSnapshotLen = 32
func TrimContinuationOverlap(existing, incoming string) string {
if incoming == "" {
return ""
}
if existing == "" {
return incoming
}
if len(incoming) < minContinuationSnapshotLen {
return incoming
}
if len(incoming) > len(existing) {
if strings.HasPrefix(incoming, existing) {
return incoming[len(existing):]
}
return incoming
}
if len(incoming) < len(existing) && strings.HasPrefix(existing, incoming) {
return ""
}
return incoming
}
func TrimContinuationOverlapFromBuilder(existing *strings.Builder, incoming string) string {
if incoming == "" {
return ""
}
if existing == nil || existing.Len() == 0 {
return incoming
}
if len(incoming) < minContinuationSnapshotLen {
return incoming
}
existingLen := existing.Len()
if len(incoming) > existingLen {
existingStr := existing.String()
if strings.HasPrefix(incoming, existingStr) {
return incoming[existingLen:]
}
return incoming
}
if len(incoming) < existingLen {
existingStr := existing.String()
if strings.HasPrefix(existingStr, incoming) {
return ""
}
}
return incoming
}