Fix/issue 367 kimi k2 streaming - #477
Conversation
…runtime errors - Add safety check for lineInfo.lineStartCols existence before accessing it - Prevents 'undefined is not an object' error when OpenTUI TextBufferView doesn't populate lineStartCols - Fixes issue where CLI would crash on startup with TypeError
- Fixed incorrect property name reference at line 804 - All references use correct lineStartCols (not lineStarts) - Resolves TypeScript compilation errors
…reaming responses
- Add attemptJSONParse() function with 4 fallback recovery strategies for truncated JSON
1. Try parsing as-is
2. Try trimming trailing whitespace
3. Try closing incomplete JSON structures ({}, ]}, }]}, [])
4. Return null if all attempts fail
- Integrate defensive parsing into handleLine() function:
- Replace simple JSON.parse() with attemptJSONParse() with null checks
- Add error-like pattern detection to handle error responses with malformed schema
- Enhanced logging showing raw response preview and recovery attempts
- Detailed debug logs for truncated/malformed chunks
- Improve handleStreamChunk() robustness:
- Add defensive check for null/undefined/non-array choices
- Better logging with diagnostic information for empty choices
This fixes issue CodebuffAI#367 where kimi-k2 and similar models that send truncated JSON at newline boundaries would cause silent parsing failures and agent crashes.
…#367) - Add attemptJSONParse() function with recovery strategies for truncated JSON - Attempts to parse as-is, then trims whitespace, then closes incomplete structures - Handles truncated newline boundaries gracefully - Remove redundant error-like fallback path (handleResponse already returns {state} for errors) - Improve logging for schema validation failures - Fixes issue where kimi-k2 model breaks due to JSON chunk truncation
74d22f3 to
c2b99f5
Compare
|
One concern with the new If the real complete SSE frame arrives on the next read, the same token span can be counted twice or with corrupted content. That feels riskier than just skipping the malformed fragment. A safer recovery path would be to keep an unfinished |
|
Thanks for digging into #367 — streaming breakage with kimi-k2 is a real and annoying bug. Unfortunately this PR can't be ported as-is for a few reasons:
If you want to pursue this further, I'd suggest: (a) isolate the fix to just the streaming/parsing logic, no unrelated dependency or formatting changes, (b) find where lines are split on newlines and confirm whether partial JSON is being handed to |
Description
Fixes #367 - The "kimi-k2" Model breaks off after use at the beginning
Problem
The kimi-k2 model was breaking off during streaming responses due to truncated JSON chunks. When JSON is sent at newline boundaries, it can arrive incomplete, causing parsing failures and stream termination.
Solution
This PR adds defensive JSON parsing and error recovery mechanisms to gracefully handle truncated or malformed JSON responses from OpenRouter:
attemptJSONParse()function - Implements multi-stage JSON recovery:Enhanced error handling - Improved logging and validation:
Import cleanup - Reorganized imports for consistency and removed unused types
Testing
Related
Fixes #367