-
Notifications
You must be signed in to change notification settings - Fork 18
feat(chat): resume in-flight workflow stream on chat mount #1771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arpitgupta1214
wants to merge
2
commits into
test
Choose a base branch
from
feat/resume-chat-stream-on-mount
base: test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { getChatSnapshot } from "../getChatSnapshot"; | ||
| import { NEW_API_BASE_URL } from "../../consts"; | ||
|
|
||
| const mockFetch = vi.fn(); | ||
| global.fetch = mockFetch; | ||
|
|
||
| describe("getChatSnapshot", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("fetches the session-scoped chat with a bearer token", async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ messages: [], isStreaming: false }), | ||
| }); | ||
|
|
||
| await getChatSnapshot("sess-1", "chat-1", "tok-123"); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `${NEW_API_BASE_URL}/api/sessions/sess-1/chats/chat-1`, | ||
| { headers: { Authorization: "Bearer tok-123" } }, | ||
| ); | ||
| }); | ||
|
|
||
| it("returns messages and isStreaming from the response", async () => { | ||
| const messages = [{ id: "m1", role: "user", parts: [] }]; | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ messages, isStreaming: true }), | ||
| }); | ||
|
|
||
| const result = await getChatSnapshot("sess-1", "chat-1", "tok"); | ||
|
|
||
| expect(result).toEqual({ messages, isStreaming: true }); | ||
| }); | ||
|
|
||
| it("defaults isStreaming to false when absent", async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ messages: [] }), | ||
| }); | ||
|
|
||
| const result = await getChatSnapshot("sess-1", "chat-1", "tok"); | ||
|
|
||
| expect(result).toEqual({ messages: [], isStreaming: false }); | ||
| }); | ||
|
|
||
| it("throws with the server's error text on a non-ok response", async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 404, | ||
| text: async () => "Chat not found", | ||
| json: async () => ({}), | ||
| }); | ||
|
|
||
| await expect(getChatSnapshot("sess-1", "chat-1", "tok")).rejects.toThrow( | ||
| "Chat not found", | ||
| ); | ||
| }); | ||
|
|
||
| it("throws a generic HTTP error when the body has no text", async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: false, | ||
| status: 500, | ||
| text: async () => "", | ||
| }); | ||
|
|
||
| await expect(getChatSnapshot("sess-1", "chat-1", "tok")).rejects.toThrow( | ||
| "HTTP 500", | ||
| ); | ||
| }); | ||
|
|
||
| it("url-encodes both ids", async () => { | ||
| mockFetch.mockResolvedValueOnce({ | ||
| ok: true, | ||
| json: async () => ({ messages: [], isStreaming: false }), | ||
| }); | ||
|
|
||
| await getChatSnapshot("a/b", "c d", "tok"); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `${NEW_API_BASE_URL}/api/sessions/a%2Fb/chats/c%20d`, | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
| }); |
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { UIMessage } from "ai"; | ||
| import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; | ||
|
|
||
| export interface ChatSnapshot { | ||
| /** Persisted UI messages; each row's `parts` blob is a full UIMessage. */ | ||
| messages: UIMessage[]; | ||
| /** True when a workflow run is still in flight and can be resumed. */ | ||
| isStreaming: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Fetch a session-scoped chat snapshot from recoup-api | ||
| * `GET /api/sessions/{sessionId}/chats/{chatId}`: the persisted messages | ||
| * plus whether a workflow run is still streaming (so the caller can | ||
| * reconnect via `GET /api/chat/{chatId}/stream`). | ||
| */ | ||
| export async function getChatSnapshot( | ||
| sessionId: string, | ||
| chatId: string, | ||
| accessToken: string, | ||
| ): Promise<ChatSnapshot> { | ||
| const response = await fetch( | ||
| `${getClientApiBaseUrl()}/api/sessions/${encodeURIComponent(sessionId)}/chats/${encodeURIComponent(chatId)}`, | ||
| { | ||
| headers: { Authorization: `Bearer ${accessToken}` }, | ||
| }, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text().catch(() => ""); | ||
| throw new Error( | ||
| errorText || `Failed to fetch chat snapshot: HTTP ${response.status}`, | ||
| ); | ||
| } | ||
|
|
||
| const data = (await response.json()) as { | ||
| messages?: UIMessage[]; | ||
| isStreaming?: boolean; | ||
| }; | ||
|
|
||
| return { | ||
| messages: data.messages ?? [], | ||
| isStreaming: data.isStreaming ?? false, | ||
| }; | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
void resumeStream()silently swallows the promise rejection path.resumeStream()returns aPromise<void>that can reject (e.g., network failure during the transport's GET stream reconnect, server error before stream negotiation). Usingvoiddiscards this, producing an unhandled promise rejection in the browser.Prompt for AI agents