Handle chunk SSE events for token-by-token streaming#10
Merged
Conversation
Greptile SummaryAdded support for token-by-token streaming via a new Key Changes:
Issues Found:
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant useChat
participant Server
participant UI
User->>useChat: sendMessage(content)
useChat->>UI: Add user message
useChat->>UI: Create placeholder assistant message
useChat->>Server: sendMessage() via API
Server-->>useChat: SSE: session event
useChat->>useChat: updateSessionId()
loop Token-by-token streaming
Server-->>useChat: SSE: chunk event
useChat->>UI: Append content to message
Note over useChat,UI: content += chunk.content<br/>status = 'streaming'
end
Server-->>useChat: SSE: done event
useChat->>UI: Update status to 'sent'
UI->>User: Display complete message
|
Comment on lines
+131
to
+144
| case 'chunk': { | ||
| // Streaming chunk - append to existing message content | ||
| const data = event.data as { content: string }; | ||
| if (data.content) { | ||
| setMessages((prev) => | ||
| prev.map((m) => | ||
| m.id === messageId | ||
| ? { ...m, content: m.content + data.content, status: 'streaming' as const } | ||
| : m | ||
| ) | ||
| ); | ||
| } | ||
| break; | ||
| } |
There was a problem hiding this comment.
logic: 'chunk' is not defined in the SSEEventType union in src/types/index.ts. TypeScript will allow this code but it breaks type safety.
Suggested change
| case 'chunk': { | |
| // Streaming chunk - append to existing message content | |
| const data = event.data as { content: string }; | |
| if (data.content) { | |
| setMessages((prev) => | |
| prev.map((m) => | |
| m.id === messageId | |
| ? { ...m, content: m.content + data.content, status: 'streaming' as const } | |
| : m | |
| ) | |
| ); | |
| } | |
| break; | |
| } | |
| case 'chunk': { | |
| // Streaming chunk - append to existing message content | |
| const data = event.data as { content: string }; | |
| if (data.content) { | |
| setMessages((prev) => | |
| prev.map((m) => | |
| m.id === messageId | |
| ? { ...m, content: m.content + data.content, status: 'streaming' as const } | |
| : m | |
| ) | |
| ); | |
| } | |
| break; | |
| } |
Add 'chunk' to the type definition:
export type SSEEventType =
| 'session'
| 'message'
| 'chunk' // Token-by-token streaming
| 'tool_call'
| 'waiting'
| 'done'
| 'completed'
| 'error';Prompt To Fix With AI
This is a comment left during a code review.
Path: src/hooks/useChat.ts
Line: 131:144
Comment:
**logic:** `'chunk'` is not defined in the `SSEEventType` union in `src/types/index.ts`. TypeScript will allow this code but it breaks type safety.
```suggestion
case 'chunk': {
// Streaming chunk - append to existing message content
const data = event.data as { content: string };
if (data.content) {
setMessages((prev) =>
prev.map((m) =>
m.id === messageId
? { ...m, content: m.content + data.content, status: 'streaming' as const }
: m
)
);
}
break;
}
```
Add `'chunk'` to the type definition:
```typescript
export type SSEEventType =
| 'session'
| 'message'
| 'chunk' // Token-by-token streaming
| 'tool_call'
| 'waiting'
| 'done'
| 'completed'
| 'error';
```
How can I resolve this? If you propose a fix, please make it concise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.