Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

Show the private sign-in button while a Bot waits in the open chat (#777)

David McKayb96807e3d96b
diff --git a/app/src/lib/copilot/sign-in-tool.tsx b/app/src/lib/copilot/sign-in-tool.tsx
index 19721591..5ed2eb68 100644
--- a/app/src/lib/copilot/sign-in-tool.tsx
+++ b/app/src/lib/copilot/sign-in-tool.tsx
@@ -1,4 +1,5 @@
import { IconLock } from "@tabler/icons-react";
+import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useState } from "react";
import { z } from "zod";
import { ToolLine } from "@/components/channels/tool-line";
@@ -13,7 +14,11 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
import { readSignIn, requestSignIn } from "@/lib/passwords/mutations";
-import { FINAL_SIGN_IN } from "@/lib/passwords/queries";
+import {
+ FINAL_SIGN_IN,
+ passwordKeys,
+ pendingSignInRequestsQueryOptions,
+} from "@/lib/passwords/queries";
import { useActiveBotHolder } from "./active-bot";
import { useFrontendTool } from "./approval-tools";
import { useComputerAvailable } from "./computer-available";
@@ -52,11 +57,16 @@ function parse(result: string | undefined): Outcome {
* private form in a dialog. The form posts to the server, never into this tool call, so neither the
* arguments nor the result the model reads ever carry a credential. An unattended turn uses the
* headless tool of the same name, and its owner gets the form as a link on their delivery channels.
+ *
+ * THE CARD FINDS ITS REQUEST ON THE SERVER, by the tool call that asked, never from state the
+ * handler sets here. The SDK keeps the renderer from this component's first render and does not
+ * re-register it when it changes (see approval-tools.tsx), so the request id the handler used to put
+ * in state never reached the card, and it waited with no way to open the form.
*/
export function SignInTool() {
const bot = useActiveBotHolder();
- // The request the handler opened, so the render can show its form before the call finishes.
- const [pending, setPending] = useState<Record<string, string>>({});
+ // Stable for the provider's life, so the SDK's once-registered handler may keep it.
+ const queryClient = useQueryClient();
// A sign-in is typed into the Bot's computer; see useComputerAvailable.
const available = useComputerAvailable();
useFrontendTool({
@@ -90,10 +100,9 @@ export function SignInTool() {
);
if (!asked.ok) return { ok: false, reason: asked.reason };
const id = asked.request.id;
- if (toolCall?.id) {
- const callId = toolCall.id;
- setPending((current) => ({ ...current, [callId]: id }));
- }
+ // Draw the button now: a background tab does not poll, and the card may have looked before
+ // the request existed.
+ void queryClient.invalidateQueries({ queryKey: passwordKeys.pending() });
const deadline = Date.now() + WAIT_MS;
while (Date.now() < deadline) {
if (signal?.aborted)
@@ -124,35 +133,46 @@ export function SignInTool() {
result: "Nobody signed in. Do not ask for the login another way.",
};
},
- render: ({ args, result, status, toolCallId }) => {
- const outcome = parse(result);
- const requestId =
- outcome.requestId ?? (toolCallId ? pending[toolCallId] : undefined);
- return (
- <SignInCard
- site={typeof args?.site === "string" ? args.site : ""}
- requestId={requestId}
- running={status !== "complete"}
- outcome={outcome}
- />
- );
- },
+ render: ({ args, result, status, toolCallId }) => (
+ <SignInCard
+ site={typeof args?.site === "string" ? args.site : ""}
+ toolCallId={toolCallId}
+ running={status !== "complete"}
+ outcome={parse(result)}
+ />
+ ),
});
return null;
}
function SignInCard({
site,
- requestId,
+ toolCallId,
running,
outcome,
}: {
site: string;
- requestId?: string;
+ toolCallId?: string;
running: boolean;
outcome: Outcome;
}) {
const [open, setOpen] = useState(false);
+ // While the call waits there is no result to carry the id, so the open request that this call
+ // asked for is looked up instead, polled until the handler has opened it.
+ const lookup = running && !outcome.requestId && Boolean(toolCallId);
+ const { data: requests } = useQuery({
+ ...pendingSignInRequestsQueryOptions(),
+ enabled: lookup,
+ refetchInterval: (query) =>
+ query.state.data?.some((request) => request.toolCallId === toolCallId)
+ ? false
+ : POLL_MS,
+ });
+ const requestId =
+ outcome.requestId ??
+ (lookup
+ ? requests?.find((request) => request.toolCallId === toolCallId)?.id
+ : undefined);
return (
<div className="flex flex-col gap-2">
<ToolLine
diff --git a/app/src/lib/passwords/queries.ts b/app/src/lib/passwords/queries.ts
index 89c83216..35f07dd4 100644
--- a/app/src/lib/passwords/queries.ts
+++ b/app/src/lib/passwords/queries.ts
@@ -25,6 +25,8 @@ export type SignInRequest = {
method: "typed" | "saved" | "takeover" | null;
/** The last thing that happened, e.g. why a submitted login did not work. */
outcome: string | null;
+ /** The open chat's tool call that asked, so its card can find this request. */
+ toolCallId: string | null;
controlRequestId: string | null;
createdAt: string;
expiresAt: string;
@@ -46,6 +48,7 @@ export const passwordKeys = {
all: ["passwords"] as const,
logins: () => ["passwords", "logins"] as const,
signIn: (id: string) => ["passwords", "sign-in", id] as const,
+ pending: () => ["passwords", "sign-in-requests"] as const,
};
export const FINAL_SIGN_IN: readonly SignInStatus[] = [
@@ -67,6 +70,21 @@ export function signInRequestQueryOptions(id: string) {
});
}
+/** This person's open sign-in requests, newest first. */
+export function pendingSignInRequestsQueryOptions() {
+ return queryOptions({
+ queryKey: passwordKeys.pending(),
+ queryFn: async (): Promise<SignInRequest[]> =>
+ (
+ (await (
+ await client("/api/sign-in-requests", {
+ fallback: "Could not load your sign-in requests",
+ })
+ ).json()) as { requests: SignInRequest[] }
+ ).requests,
+ });
+}
+
export function savedLoginsQueryOptions() {
return queryOptions({
queryKey: passwordKeys.logins(),
diff --git a/app/tests/approval-tools.test.tsx b/app/tests/approval-tools.test.tsx
index 3e9f2d33..8945763f 100644
--- a/app/tests/approval-tools.test.tsx
+++ b/app/tests/approval-tools.test.tsx
@@ -1,7 +1,10 @@
-import { afterAll, afterEach, beforeAll, expect, mock, test } from "bun:test";
+import { afterAll, afterEach, beforeAll, expect, spyOn, test } from "bun:test";
+import * as ReactCoreV2 from "@copilotkit/react-core/v2";
import { GlobalRegistrator } from "@happy-dom/global-registrator";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { cleanup, render } from "@testing-library/react";
+import { observeApprovalAgent } from "@/lib/copilot/approval-context";
+import { useFrontendTool } from "@/lib/copilot/approval-tools";
/**
* The handler the SDK keeps for a computer change.
@@ -12,24 +15,35 @@ import { cleanup, render } from "@testing-library/react";
* sent every change straight to the computer outside any run; the server then answered "This action
* needs an authenticated run before it can be approved." This drives the handler the SDK actually
* kept, captured at first render, with no inbox loaded at all.
+ *
+ * Spies on the module namespace rather than `mock.module`, because bun cannot take a module mock
+ * back off: it would stay installed for every test file walked into the same process afterwards,
+ * and those files would get this fake SDK instead of the real one. These spies are restored below.
*/
-const real = await import("@copilotkit/react-core/v2");
-type Registered = Parameters<typeof real.useFrontendTool>[0];
+type Registered = Parameters<typeof ReactCoreV2.useFrontendTool>[0];
const registered: Registered[] = [];
const stopped: unknown[] = [];
-mock.module("@copilotkit/react-core/v2", () => ({
- ...real,
- useCopilotKit: () => ({
- copilotkit: { stopAgent: (input: unknown) => stopped.push(input) },
- }),
- useFrontendTool: (tool: Registered) => {
- if (!registered.some((entry) => entry.name === tool.name))
- registered.push(tool);
- },
-}));
-const { useFrontendTool } = await import("@/lib/copilot/approval-tools");
-const { observeApprovalAgent } = await import("@/lib/copilot/approval-context");
+const useCopilotKitSpy = spyOn(ReactCoreV2, "useCopilotKit").mockImplementation(
+ () =>
+ ({
+ copilotkit: { stopAgent: (input: unknown) => stopped.push(input) },
+ }) as never,
+);
+function register<T extends Record<string, unknown>>(
+ tool: ReactCoreV2.ReactFrontendTool<T>,
+) {
+ if (!registered.some((entry) => entry.name === tool.name))
+ registered.push(tool as unknown as Registered);
+}
+const useFrontendToolSpy = spyOn(
+ ReactCoreV2,
+ "useFrontendTool",
+).mockImplementation(register);
+afterAll(() => {
+ useCopilotKitSpy.mockRestore();
+ useFrontendToolSpy.mockRestore();
+});
beforeAll(() => GlobalRegistrator.register());
afterEach(cleanup);
diff --git a/app/tests/sign-in-card.test.tsx b/app/tests/sign-in-card.test.tsx
new file mode 100644
index 00000000..b3b26a63
--- /dev/null
+++ b/app/tests/sign-in-card.test.tsx
@@ -0,0 +1,163 @@
+import { afterAll, afterEach, beforeAll, expect, test } from "bun:test";
+import { GlobalRegistrator } from "@happy-dom/global-registrator";
+import { CopilotKitProvider, useCopilotKit } from "@copilotkit/react-core/v2";
+import {
+ focusManager,
+ QueryClient,
+ QueryClientProvider,
+} from "@tanstack/react-query";
+import { cleanup, fireEvent, render, waitFor } from "@testing-library/react";
+import type { ComponentType, ReactNode } from "react";
+import { SignInTool } from "@/lib/copilot/sign-in-tool";
+import { settleReactWork } from "./settle-react-work";
+
+/**
+ * The open chat's sign-in card, drawn by the renderer the SDK actually keeps.
+ *
+ * The SDK registers a frontend tool's renderer once and does not re-register it when the function
+ * changes, so anything the renderer reads from the registering component's render is frozen at first
+ * render. The card used to take its request id from state the handler set, which the kept renderer
+ * never saw: the card said "Waiting for you to sign in" with no way to open the form. This uses the
+ * real provider and the real hook, so an SDK that changes how it keeps renderers is caught here too.
+ */
+
+beforeAll(() => GlobalRegistrator.register());
+afterEach(cleanup);
+afterAll(async () => {
+ await settleReactWork();
+ GlobalRegistrator.unregister();
+});
+
+const originalFetch = global.fetch;
+afterEach(() => {
+ global.fetch = originalFetch;
+ focusManager.setFocused(undefined);
+});
+
+const TOOL_CALL_ID = "call-sign-in";
+const SITE = "https://shop.example";
+const signInRequest = {
+ id: "request-1",
+ botId: "default",
+ origin: SITE,
+ reason: null,
+ status: "pending",
+ method: null,
+ outcome: null,
+ threadId: null,
+ toolCallId: TOOL_CALL_ID,
+ controlRequestId: null,
+ createdAt: "2026-10-12T10:00:00Z",
+ expiresAt: "2026-10-12T10:15:00Z",
+ resolvedAt: null,
+ passwordManager: false,
+ savedLogins: [],
+};
+
+function serving(open: (typeof signInRequest)[]) {
+ global.fetch = Object.assign(
+ async (input: RequestInfo | URL, init?: RequestInit) => {
+ const path = new URL(String(input), "http://127.0.0.1").pathname;
+ const method = init?.method ?? "GET";
+ if (path === "/api/sign-in-requests" && method === "POST")
+ return Response.json(signInRequest, { status: 201 });
+ if (path === "/api/sign-in-requests")
+ return Response.json({ requests: open });
+ if (path === `/api/sign-in-requests/${signInRequest.id}`)
+ return Response.json(signInRequest);
+ return Response.json({ error: "Not found" }, { status: 404 });
+ },
+ { preconnect: originalFetch.preconnect },
+ );
+}
+
+type Renderer = ComponentType<Record<string, unknown>>;
+type Kept = {
+ renderer?: Renderer;
+ handler?: (args: unknown, context: unknown) => Promise<unknown>;
+};
+
+function Capture({ kept }: { kept: Kept }) {
+ const { copilotkit } = useCopilotKit();
+ const entry = copilotkit.renderToolCalls.find(
+ (candidate) => candidate.name === "computer_request_sign_in",
+ );
+ kept.renderer = entry?.render as Renderer | undefined;
+ kept.handler = copilotkit.getTool({
+ toolName: "computer_request_sign_in",
+ })?.handler as Kept["handler"];
+ return null;
+}
+
+function Providers({
+ queries,
+ children,
+}: {
+ queries: QueryClient;
+ children: ReactNode;
+}) {
+ return (
+ <QueryClientProvider client={queries}>
+ <CopilotKitProvider>{children}</CopilotKitProvider>
+ </QueryClientProvider>
+ );
+}
+
+test("the waiting card offers the private sign-in once the request exists", async () => {
+ serving([]);
+ const queries = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ const kept: Kept = {};
+ const tree = render(
+ <Providers queries={queries}>
+ <SignInTool />
+ <Capture kept={kept} />
+ </Providers>,
+ );
+ await waitFor(() => expect(kept.handler).toBeDefined());
+
+ // The chat draws the call as soon as it streams in, before the handler has opened the request.
+ const Kept = kept.renderer as Renderer;
+ const drawn = (
+ <Providers queries={queries}>
+ <SignInTool />
+ <Capture kept={kept} />
+ <Kept
+ name="computer_request_sign_in"
+ args={{ site: SITE }}
+ status="executing"
+ toolCallId={TOOL_CALL_ID}
+ />
+ </Providers>
+ );
+ serving([]);
+ tree.rerender(drawn);
+ await tree.findByText("Waiting for you to sign in");
+ // Long enough for a card that looks the request up to have looked once and found nothing.
+ await new Promise((resolve) => setTimeout(resolve, 100));
+
+ // In a background tab nothing polls, so the request has to reach the card without a timer.
+ focusManager.setFocused(false);
+ serving([signInRequest]);
+
+ // The model calls the tool: the handler opens the request on the server and waits on it.
+ const stop = new AbortController();
+ const answered = kept.handler?.(
+ { site: SITE },
+ { signal: stop.signal, toolCall: { id: TOOL_CALL_ID } },
+ );
+
+ const open = await tree.findByRole(
+ "button",
+ { name: /Open private sign-in/ },
+ { timeout: 4000 },
+ );
+ focusManager.setFocused(undefined);
+ fireEvent.click(open);
+ await tree.findByText(`Sign in to ${SITE}`);
+ await tree.findByText(/Goes to your Bot's browser/);
+
+ stop.abort();
+ await answered;
+});