mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-12 23:36:55 +08:00
change v1
This commit is contained in:
49
agent/stream_text.go
Normal file
49
agent/stream_text.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package agent
|
||||
|
||||
import "strings"
|
||||
|
||||
func emitStreamText(onEvent func(event, data string), text string) {
|
||||
if onEvent == nil {
|
||||
return
|
||||
}
|
||||
for _, chunk := range splitStreamText(text) {
|
||||
onEvent(StreamEventDelta, chunk)
|
||||
}
|
||||
}
|
||||
|
||||
func splitStreamText(text string) []string {
|
||||
text = strings.TrimSpace(text)
|
||||
if text == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
lines := strings.Split(text, "\n")
|
||||
chunks := make([]string, 0, len(lines)*2)
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
start := 0
|
||||
for i, r := range line {
|
||||
switch r {
|
||||
case '。', '!', '?', '.', '!', '?', ';', ';', ':', ':', ',', ',':
|
||||
part := strings.TrimSpace(line[start : i+len(string(r))])
|
||||
if part != "" {
|
||||
chunks = append(chunks, part)
|
||||
}
|
||||
start = i + len(string(r))
|
||||
}
|
||||
}
|
||||
if start < len(line) {
|
||||
part := strings.TrimSpace(line[start:])
|
||||
if part != "" {
|
||||
chunks = append(chunks, part)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(chunks) == 0 {
|
||||
return []string{text}
|
||||
}
|
||||
return chunks
|
||||
}
|
||||
Reference in New Issue
Block a user