|
| 1 | +import { CopilotKitProvider, useFrontendTool } from "@copilotkit/react-core/v2"; |
| 2 | +import { |
| 3 | + type InfiniteData, |
| 4 | + QueryClientProvider, |
| 5 | + useQuery, |
| 6 | +} from "@tanstack/react-query"; |
| 7 | +import { |
| 8 | + createBrowserHistory, |
| 9 | + createRootRoute, |
| 10 | + createRoute, |
| 11 | + createRouter, |
| 12 | + RouterProvider, |
| 13 | +} from "@tanstack/react-router"; |
| 14 | +import { useEffect } from "react"; |
| 15 | +import { createRoot } from "react-dom/client"; |
| 16 | +import { z } from "zod"; |
| 17 | +import { AppSidebar } from "@/components/app-sidebar/app-sidebar"; |
| 18 | +import { ChannelChat } from "@/components/channels/channel-chat"; |
| 19 | +import { SidebarShell } from "@/components/layout/sidebar-shell"; |
| 20 | +import { SidebarInset } from "@/components/ui/sidebar"; |
| 21 | +import { TooltipProvider } from "@/components/ui/tooltip"; |
| 22 | +import { approvalKeys } from "@/lib/approvals"; |
| 23 | +import { |
| 24 | + type AgentChannel, |
| 25 | + type ChannelPage, |
| 26 | + type ChannelWork, |
| 27 | + channelKeys, |
| 28 | +} from "@/lib/channels/queries"; |
| 29 | +import { ActiveBotProvider } from "@/lib/copilot/active-bot"; |
| 30 | +import { BotOffers } from "@/lib/copilot/bot-offer"; |
| 31 | +import { ComputerTools } from "@/lib/copilot/computer-tools"; |
| 32 | +import { quietRunPeriod } from "@/lib/copilot/quiet-run"; |
| 33 | +import { setCurrentOrganization } from "@/lib/organizations/current"; |
| 34 | +import { queryClient } from "@/query-client"; |
| 35 | + |
| 36 | +/** |
| 37 | + * A conversation whose turn the server can carry on by itself, as a page of its own for |
| 38 | + * carry-on-carried-turn-browser.test.tsx to bundle and load in a browser: the real conversation in |
| 39 | + * the app's real frame, as run-ended-unheard.fixture.tsx draws it. |
| 40 | + * |
| 41 | + * The one thing it adds is what the app's socket does. The test's server says what the |
| 42 | + * conversation's work is now and when the person's approvals changed, at `/fixture/live`, and this |
| 43 | + * page puts that where the socket's busy event and its `approvals` hint put it: on the |
| 44 | + * conversation's row in the roster, and by reading the approvals again. Everything else is the app's. |
| 45 | + * |
| 46 | + * The quiet period is the test's (`?quietMs=`), as in run-ended-unheard.fixture.tsx. |
| 47 | + */ |
| 48 | +setCurrentOrganization("default"); |
| 49 | +quietRunPeriod.ms = Number( |
| 50 | + new URLSearchParams(location.search).get("quietMs") ?? quietRunPeriod.ms, |
| 51 | +); |
| 52 | + |
| 53 | +const CONVERSATION: AgentChannel = { |
| 54 | + id: "launch-conversation", |
| 55 | + name: "General Assistant", |
| 56 | + agentIds: ["general-assistant"], |
| 57 | + threadId: "launch-thread", |
| 58 | + active: true, |
| 59 | + lastMessageAt: "2026-11-25T18:00:00.000Z", |
| 60 | +}; |
| 61 | + |
| 62 | +queryClient.setQueryDefaults(channelKeys.list(), { staleTime: Infinity }); |
| 63 | +queryClient.setQueryData<InfiniteData<ChannelPage>>(channelKeys.list(), { |
| 64 | + pages: [ |
| 65 | + { |
| 66 | + channels: [ |
| 67 | + { |
| 68 | + ...CONVERSATION, |
| 69 | + summary: "Launch plan", |
| 70 | + lastMessage: null, |
| 71 | + lastMessageAgentId: null, |
| 72 | + createdAt: "2026-11-25T18:00:00.000Z", |
| 73 | + pinned: false, |
| 74 | + lastReadAt: "2026-11-25T18:00:00.000Z", |
| 75 | + }, |
| 76 | + ], |
| 77 | + nextCursor: null, |
| 78 | + }, |
| 79 | + ], |
| 80 | + pageParams: [""], |
| 81 | +}); |
| 82 | + |
| 83 | +/** A step the Bot asks this browser to take, answered here as a step on the computer is. */ |
| 84 | +function BrowserStep() { |
| 85 | + useFrontendTool({ |
| 86 | + name: "fixture_step", |
| 87 | + description: "A step the browser carries out.", |
| 88 | + parameters: z.object({}).passthrough(), |
| 89 | + handler: async () => ({ ok: true }), |
| 90 | + }); |
| 91 | + return null; |
| 92 | +} |
| 93 | + |
| 94 | +type Live = { work: ChannelWork[]; approvals: number }; |
| 95 | +const NO_WORK: ChannelWork[] = []; |
| 96 | + |
| 97 | +/** The conversation, with its work as the socket last said it. */ |
| 98 | +function Conversation() { |
| 99 | + const live = useQuery({ |
| 100 | + queryKey: ["fixture", "live"], |
| 101 | + queryFn: async (): Promise<Live> => |
| 102 | + (await fetch("/fixture/live")).json() as Promise<Live>, |
| 103 | + refetchInterval: 150, |
| 104 | + }).data; |
| 105 | + const work = live?.work ?? NO_WORK; |
| 106 | + const approvals = live?.approvals ?? 0; |
| 107 | + useEffect(() => { |
| 108 | + queryClient.setQueryData<InfiniteData<ChannelPage>>( |
| 109 | + channelKeys.list(), |
| 110 | + (list) => |
| 111 | + list && { |
| 112 | + ...list, |
| 113 | + pages: list.pages.map((page) => ({ |
| 114 | + ...page, |
| 115 | + channels: page.channels.map((row) => |
| 116 | + row.id === CONVERSATION.id ? { ...row, work } : row, |
| 117 | + ), |
| 118 | + })), |
| 119 | + }, |
| 120 | + ); |
| 121 | + }, [work]); |
| 122 | + useEffect(() => { |
| 123 | + if (approvals > 0) |
| 124 | + void queryClient.invalidateQueries({ queryKey: approvalKeys.all }); |
| 125 | + }, [approvals]); |
| 126 | + return ( |
| 127 | + <ChannelChat |
| 128 | + channel={work.length > 0 ? { ...CONVERSATION, work } : CONVERSATION} |
| 129 | + runtimeAgentId="general-assistant" |
| 130 | + /> |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +const rootRoute = createRootRoute({ |
| 135 | + component: () => ( |
| 136 | + <SidebarShell className="h-svh overflow-hidden"> |
| 137 | + <AppSidebar /> |
| 138 | + <SidebarInset className="min-h-0 overflow-hidden"> |
| 139 | + <ActiveBotProvider> |
| 140 | + <BotOffers /> |
| 141 | + <ComputerTools /> |
| 142 | + <BrowserStep /> |
| 143 | + <Conversation /> |
| 144 | + </ActiveBotProvider> |
| 145 | + </SidebarInset> |
| 146 | + </SidebarShell> |
| 147 | + ), |
| 148 | +}); |
| 149 | +const router = createRouter({ |
| 150 | + history: createBrowserHistory(), |
| 151 | + routeTree: rootRoute.addChildren([ |
| 152 | + createRoute({ |
| 153 | + getParentRoute: () => rootRoute, |
| 154 | + path: "$", |
| 155 | + component: () => null, |
| 156 | + }), |
| 157 | + ]), |
| 158 | +}); |
| 159 | + |
| 160 | +const root = document.getElementById("root"); |
| 161 | +if (root) |
| 162 | + createRoot(root).render( |
| 163 | + <QueryClientProvider client={queryClient}> |
| 164 | + <CopilotKitProvider credentials="include" runtimeUrl="/api/copilotkit"> |
| 165 | + <TooltipProvider> |
| 166 | + <RouterProvider router={router as never} /> |
| 167 | + </TooltipProvider> |
| 168 | + </CopilotKitProvider> |
| 169 | + </QueryClientProvider>, |
| 170 | + ); |
0 commit comments