mirror of
https://github.com/cexll/myclaude.git
synced 2026-02-11 03:23:50 +08:00
fix: parser critical bugs and add PR #86 compatibility
Fixes 3 critical bugs in parser.go:
1. Gemini detection: Change Delta from bool to *bool for proper field
presence detection. Fixes issue where delta:false events were lost.
2. Codex detection: Tighten logic to only classify as Codex if
thread_id exists OR item.type is non-empty. Prevents Claude events
with item:null/item:{} from being misclassified and dropped.
3. Performance: Move itemHeader parsing inside item.completed case,
only parse when actually needed.
Additional changes:
- Implement PR #86's Gemini notifyMessage trigger on Status field
instead of Content, resolving parser.go conflict between PRs
- Add regression tests for all fixed scenarios
- All tests pass: go test ./...
Co-authored-by: Codeagent (Codex)
Generated with SWE-Agent.ai
Co-Authored-By: SWE-Agent.ai <noreply@swe-agent.ai>
This commit is contained in:
@@ -1582,6 +1582,34 @@ func TestBackendParseJSONStream_ClaudeEvents(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendParseJSONStream_ClaudeEvents_ItemDoesNotForceCodex(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
}{
|
||||
{
|
||||
name: "null item",
|
||||
input: `{"type":"result","result":"OK","session_id":"abc123","item":null}`,
|
||||
},
|
||||
{
|
||||
name: "empty object item",
|
||||
input: `{"type":"result","subtype":"x","result":"OK","session_id":"abc123","item":{}}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
message, threadID := parseJSONStream(strings.NewReader(tt.input))
|
||||
if message != "OK" {
|
||||
t.Fatalf("message=%q, want %q", message, "OK")
|
||||
}
|
||||
if threadID != "abc123" {
|
||||
t.Fatalf("threadID=%q, want %q", threadID, "abc123")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendParseJSONStream_GeminiEvents(t *testing.T) {
|
||||
input := `{"type":"init","session_id":"xyz789"}
|
||||
{"type":"message","role":"assistant","content":"Hi","delta":true,"session_id":"xyz789"}
|
||||
@@ -1598,6 +1626,43 @@ func TestBackendParseJSONStream_GeminiEvents(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendParseJSONStream_GeminiEvents_DeltaFalseStillDetected(t *testing.T) {
|
||||
input := `{"type":"init","session_id":"xyz789"}
|
||||
{"type":"message","content":"Hi","delta":false,"session_id":"xyz789"}
|
||||
{"type":"result","status":"success","session_id":"xyz789"}`
|
||||
|
||||
message, threadID := parseJSONStream(strings.NewReader(input))
|
||||
|
||||
if message != "Hi" {
|
||||
t.Fatalf("message=%q, want %q", message, "Hi")
|
||||
}
|
||||
if threadID != "xyz789" {
|
||||
t.Fatalf("threadID=%q, want %q", threadID, "xyz789")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendParseJSONStream_GeminiEvents_OnMessageTriggeredOnStatus(t *testing.T) {
|
||||
input := `{"type":"init","session_id":"xyz789"}
|
||||
{"type":"message","role":"assistant","content":"Hi","delta":true,"session_id":"xyz789"}
|
||||
{"type":"message","content":" there","delta":true}
|
||||
{"type":"result","status":"success","session_id":"xyz789"}`
|
||||
|
||||
var called int
|
||||
message, threadID := parseJSONStreamInternal(strings.NewReader(input), nil, nil, func() {
|
||||
called++
|
||||
})
|
||||
|
||||
if message != "Hi there" {
|
||||
t.Fatalf("message=%q, want %q", message, "Hi there")
|
||||
}
|
||||
if threadID != "xyz789" {
|
||||
t.Fatalf("threadID=%q, want %q", threadID, "xyz789")
|
||||
}
|
||||
if called != 1 {
|
||||
t.Fatalf("onMessage called=%d, want %d", called, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendParseJSONStreamWithWarn_InvalidLine(t *testing.T) {
|
||||
var warnings []string
|
||||
warnFn := func(msg string) { warnings = append(warnings, msg) }
|
||||
|
||||
Reference in New Issue
Block a user