Skip to content

The open-source app everyone uses to manage agents at work

fix(ui): use latest issue runtime callbacks (#14863)

Dotta569c7203aa24
diff --git a/ui/src/hooks/usePaperclipIssueRuntime.test.tsx b/ui/src/hooks/usePaperclipIssueRuntime.test.tsx
index 4d51c8ce24..81a38deb26 100644
--- a/ui/src/hooks/usePaperclipIssueRuntime.test.tsx
+++ b/ui/src/hooks/usePaperclipIssueRuntime.test.tsx
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
-import { act } from "react";
+import { act, useLayoutEffect } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import type {
@@ -10,9 +10,22 @@ import type {
} from "@assistant-ui/react";
import { usePaperclipIssueRuntime } from "./usePaperclipIssueRuntime";
-const { useExternalStoreRuntimeMock } = vi.hoisted(() => ({
- useExternalStoreRuntimeMock: vi.fn(() => ({ kind: "runtime" })),
-}));
+const { externalStoreRuntimeState, useExternalStoreRuntimeMock } = vi.hoisted(
+ () => {
+ const state: {
+ adapter: ExternalStoreAdapter<ThreadMessage> | undefined;
+ } = { adapter: undefined };
+ return {
+ externalStoreRuntimeState: state,
+ useExternalStoreRuntimeMock: vi.fn(
+ (adapter: ExternalStoreAdapter<ThreadMessage>) => {
+ state.adapter = adapter;
+ return { kind: "runtime" };
+ },
+ ),
+ };
+ },
+);
vi.mock("@assistant-ui/react", () => ({
useExternalStoreRuntime: useExternalStoreRuntimeMock,
@@ -26,6 +39,7 @@ function HookHarness({
isRunning,
onSend,
onCancel,
+ onLayout,
}: {
messages: readonly ThreadMessage[];
isRunning: boolean;
@@ -38,6 +52,7 @@ function HookHarness({
};
}) => Promise<void>;
onCancel?: (() => Promise<void>) | undefined;
+ onLayout?: ((adapter: ExternalStoreAdapter<ThreadMessage>) => void) | undefined;
}) {
usePaperclipIssueRuntime({
messages,
@@ -45,6 +60,11 @@ function HookHarness({
onSend,
onCancel,
});
+ useLayoutEffect(() => {
+ if (externalStoreRuntimeState.adapter && onLayout) {
+ onLayout(externalStoreRuntimeState.adapter);
+ }
+ }, [onLayout]);
return null;
}
@@ -86,6 +106,7 @@ function createAssistantMessage(id: string, text: string): ThreadMessage {
describe("usePaperclipIssueRuntime", () => {
afterEach(() => {
useExternalStoreRuntimeMock.mockReset();
+ externalStoreRuntimeState.adapter = undefined;
});
it("forwards explicit upload selection separately from Markdown content", async () => {
@@ -177,6 +198,63 @@ describe("usePaperclipIssueRuntime", () => {
container.remove();
});
+ it("uses the latest callbacks before passive effects run", async () => {
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+ const root = createRoot(container);
+ const messages: ThreadMessage[] = [createUserMessage("message-1", "hello")];
+ const firstOnSend = vi.fn(async () => {});
+ const secondOnSend = vi.fn(async () => {});
+ const firstOnCancel = vi.fn(async () => {});
+ const secondOnCancel = vi.fn(async () => {});
+
+ act(() => {
+ root.render(
+ <HookHarness
+ messages={messages}
+ isRunning={false}
+ onSend={firstOnSend}
+ onCancel={firstOnCancel}
+ />,
+ );
+ });
+
+ let immediateSend: Promise<void> | undefined;
+ let immediateCancel: Promise<void> | undefined;
+
+ act(() => {
+ root.render(
+ <HookHarness
+ messages={messages}
+ isRunning={false}
+ onSend={secondOnSend}
+ onCancel={secondOnCancel}
+ onLayout={(adapter) => {
+ immediateSend = adapter.onNew?.(
+ createAppendMessage("layout runtime callback"),
+ );
+ immediateCancel = adapter.onCancel?.();
+ }}
+ />,
+ );
+ });
+ await Promise.all([immediateSend, immediateCancel]);
+
+ expect(firstOnSend).not.toHaveBeenCalled();
+ expect(firstOnCancel).not.toHaveBeenCalled();
+ expect(secondOnSend).toHaveBeenCalledWith({
+ body: "layout runtime callback",
+ reopen: undefined,
+ reassignment: undefined,
+ });
+ expect(secondOnCancel).toHaveBeenCalledOnce();
+
+ act(() => {
+ root.unmount();
+ });
+ container.remove();
+ });
+
it("rebuilds the adapter when thread data changes", () => {
const container = document.createElement("div");
document.body.appendChild(container);
diff --git a/ui/src/hooks/usePaperclipIssueRuntime.ts b/ui/src/hooks/usePaperclipIssueRuntime.ts
index 494e3a444b..19d6fdf66f 100644
--- a/ui/src/hooks/usePaperclipIssueRuntime.ts
+++ b/ui/src/hooks/usePaperclipIssueRuntime.ts
@@ -1,4 +1,4 @@
-import { useEffect, useMemo, useRef } from "react";
+import { useMemo, useRef } from "react";
import {
useExternalStoreRuntime,
type ThreadMessage,
@@ -52,14 +52,8 @@ export function usePaperclipIssueRuntime({
}: UsePaperclipIssueRuntimeOptions) {
const onSendRef = useRef(onSend);
const onCancelRef = useRef(onCancel);
-
- useEffect(() => {
- onSendRef.current = onSend;
- }, [onSend]);
-
- useEffect(() => {
- onCancelRef.current = onCancel;
- }, [onCancel]);
+ onSendRef.current = onSend;
+ onCancelRef.current = onCancel;
const adapter = useMemo<ExternalStoreAdapter<ThreadMessage>>(
() => ({