-
Notifications
You must be signed in to change notification settings - Fork 18
feat(chat): wire stop button to POST /api/chat/{chatId}/stop #1770
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
9
commits into
test
Choose a base branch
from
feat/wire-stop-button-to-backend
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
904df41
feat(chat): wire stop button to POST /api/chat/{chatId}/stop
arpitgupta1214 3adb323
fix(chat): await stop POST and skip aiStop for workflow chats
arpitgupta1214 e470b81
chore(chat): trim verbose stop wrapper comment
arpitgupta1214 b99ded4
feat(chat): instant stop feedback via isStopping flag
arpitgupta1214 1c69684
fix(chat): render cancelled tool-calls with stop icon, not spinner
arpitgupta1214 81a4549
refactor(chat): extract useStopChatWorkflow hook
arpitgupta1214 8f9d9f4
refactor(chat): useStopChatWorkflow on react-query useMutation
arpitgupta1214 a05055b
Merge remote-tracking branch 'origin/test' into feat/wire-stop-button…
arpitgupta1214 82c88a2
Merge remote-tracking branch 'origin/test' into feat/wire-stop-button…
sweetmantech 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,29 @@ | ||
| import { useMutation } from "@tanstack/react-query"; | ||
| import { usePrivy } from "@privy-io/react-auth"; | ||
| import { stopChatWorkflow } from "@/lib/chat/stopChatWorkflow"; | ||
|
|
||
| /** | ||
| * Wraps the `POST /api/chat/{chatId}/stop` round-trip with a React Query | ||
| * mutation. Consumers read `isStopping` (`mutation.isPending`) to flip | ||
| * the submit button to a "stopping" state the instant the user clicks, | ||
| * so the UI doesn't sit dead for the 1–2s while the backend cancels the | ||
| * workflow and the SSE watcher closes the stream. | ||
| * | ||
| * Workflow-chat-only: legacy `/api/chat` aborts locally via the AI SDK's | ||
| * `stop()` and never hits this hook. | ||
| */ | ||
| export function useStopChatWorkflow(chatId: string) { | ||
| const { getAccessToken } = usePrivy(); | ||
|
|
||
| const mutation = useMutation({ | ||
| mutationFn: async () => { | ||
| const token = await getAccessToken().catch(() => null); | ||
| await stopChatWorkflow(chatId, token); | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| stop: mutation.mutateAsync, | ||
| isStopping: mutation.isPending, | ||
| }; | ||
| } |
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,54 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { stopChatWorkflow } from "../stopChatWorkflow"; | ||
| import { NEW_API_BASE_URL } from "../../consts"; | ||
|
|
||
| const mockFetch = vi.fn(); | ||
| global.fetch = mockFetch; | ||
|
|
||
| describe("stopChatWorkflow", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("POSTs to the chat stop endpoint with a bearer token", async () => { | ||
| mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) }); | ||
|
|
||
| await stopChatWorkflow("chat-1", "tok-123"); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `${NEW_API_BASE_URL}/api/chat/chat-1/stop`, | ||
| { | ||
| method: "POST", | ||
| headers: { Authorization: "Bearer tok-123" }, | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| it("omits the Authorization header when unauthenticated", async () => { | ||
| mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) }); | ||
|
|
||
| await stopChatWorkflow("chat-1", null); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `${NEW_API_BASE_URL}/api/chat/chat-1/stop`, | ||
| { method: "POST", headers: {} }, | ||
| ); | ||
| }); | ||
|
|
||
| it("url-encodes the chat id", async () => { | ||
| mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) }); | ||
|
|
||
| await stopChatWorkflow("a/b c", "tok"); | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `${NEW_API_BASE_URL}/api/chat/a%2Fb%20c/stop`, | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
|
|
||
| it("never throws when the request fails", async () => { | ||
| mockFetch.mockRejectedValueOnce(new Error("network down")); | ||
|
|
||
| await expect(stopChatWorkflow("chat-1", "tok")).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
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,30 @@ | ||
| import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; | ||
|
|
||
| /** | ||
| * Cancels the in-flight workflow run for a chat via recoup-api | ||
| * `POST /api/chat/{chatId}/stop`. | ||
| * | ||
| * The client AI SDK `stop()` only aborts the local fetch; the durable | ||
| * workflow run keeps streaming (and billing) server-side until it's | ||
| * cancelled here. Callers should fire this without blocking the UI stop. | ||
| * | ||
| * @param chatId - Chat row id (the workflow run is keyed off it). | ||
| * @param accessToken - Privy access token; omitted when unauthenticated. | ||
| * @returns Resolves once the request settles; never throws. | ||
| */ | ||
| export async function stopChatWorkflow( | ||
| chatId: string, | ||
| accessToken: string | null, | ||
| ): Promise<void> { | ||
| try { | ||
| await fetch( | ||
| `${getClientApiBaseUrl()}/api/chat/${encodeURIComponent(chatId)}/stop`, | ||
| { | ||
| method: "POST", | ||
| headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : {}, | ||
| }, | ||
| ); | ||
| } catch { | ||
| // Best-effort: the run also self-cancels when its slot is cleared. | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.