fix(main): improve buffer size and streamline message extraction

This commit is contained in:
freespace8
2025-11-28 15:10:39 +08:00
parent 291a4e3d0a
commit aea19f0e1f

View File

@@ -24,9 +24,9 @@ const (
// Test hooks for dependency injection // Test hooks for dependency injection
var ( var (
stdinReader io.Reader = os.Stdin stdinReader io.Reader = os.Stdin
isTerminalFn = defaultIsTerminal isTerminalFn = defaultIsTerminal
codexCommand = "codex" codexCommand = "codex"
) )
// Config holds CLI configuration // Config holds CLI configuration
@@ -347,9 +347,7 @@ func runCodexProcess(codexArgs []string, taskText string, useStdin bool, timeout
func parseJSONStream(r io.Reader) (message, threadID string) { func parseJSONStream(r io.Reader) (message, threadID string) {
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
// Set larger buffer for long lines scanner.Buffer(make([]byte, 64*1024), 10*1024*1024)
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 1024*1024)
for scanner.Scan() { for scanner.Scan() {
line := strings.TrimSpace(scanner.Text()) line := strings.TrimSpace(scanner.Text())
@@ -369,18 +367,15 @@ func parseJSONStream(r io.Reader) (message, threadID string) {
} }
// Capture agent_message // Capture agent_message
if event.Type == "item.completed" && event.Item != nil { if event.Type == "item.completed" && event.Item != nil && event.Item.Type == "agent_message" {
if event.Item.Type == "agent_message" { if text := normalizeText(event.Item.Text); text != "" {
text := normalizeText(event.Item.Text) message = text
if text != "" {
message = text
}
} }
} }
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil && err != io.EOF {
logWarn("Scanner error: " + err.Error()) logWarn("Read stdout error: " + err.Error())
} }
return message, threadID return message, threadID