MoonshotAI / MoonshotAI/kimi-code
[Bug] TUI crashes on assistant.delta events with missing delta payload (Cannot read properties of undefined (reading 'trim'))
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 7.5k
- Forks
- 1.2k
- Avg merge
- 11h 53m
- Merged PRs (30d)
- 350
Description
Environment
- kimi-code TUI on the agent-core-v2 engine (current main, also reproducible on 0.34.x)
- Model served through an Anthropic-compatible relay/proxy (not the first-party Anthropic API)
Error
The TUI throws and logs an unexpected error while streaming:
[unexpected] TypeError: Cannot read properties of undefined (reading 'trim')
at SessionEventHandler.handleAssistantDelta (.../main.cjs)
at SessionEventHandler.handleEvent (.../main.cjs)
at SDKRpcClientV2.receiveEvent (.../main.cjs)
Root cause
Three small gaps line up:
- Parser: in
packages/agent-core-v2/src/kosong/provider/bases/anthropic/anthropic.ts, the stream converter trusts the SDK types:content_block_startyields{ type: 'text', text: block.text }andtext_deltayields{ type: 'text', text: delta.text }. Some relays omit thetextfield entirely, so the engine produces a text part withtext: undefined. - Transport:
loopService.tspublishes{ type: 'assistant.delta', turnId, delta: part.text }. When the event crosses JSON serialization, theundefinedvalue silently drops thedeltakey, so the TUI receives an event with nodeltaat all. - TUI:
handleAssistantDeltainapps/kimi-code/src/tui/controllers/session-event-handler.tsimmediately callsevent.delta.trim(), which throws onundefined.
Minimal reproduction
Have an Anthropic-compatible endpoint stream:
{"type":"content_block_start","index":0,"content_block":{"type":"text"}}
{"type":"content_block_delta","index":0,"delta":{"type":"text_delta"}}
(both omit text). Every such event crashes the TUI handler.
Suggested fix
- Parser:
block.text ?? ''/delta.text ?? ''(same pattern already used forthinking ?? ''two cases below). - TUI: coerce a non-string
event.deltato''before use, so a malformed wire record can never crash the handler again.
Happy to submit a PR with tests if the approach looks good.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Anthropic stream converter in packages/agent-core-v2/src/kosong/provider/bases/anthropic/anthropic.ts and the event handler in apps/kimi-code/src/tui/controllers/session-event-handler.ts, using the two-event reproduction as the first check. Trace the related publishing path in loopService.ts; done means missing text fields produce safe empty values and the TUI no longer crashes on malformed assistant.delta events, with tests added for the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100