feat: side chats and forking #9396
Closedamanthanvi(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/9396/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/orchestration/Layers/ProviderCommandReactor.ts line 836Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -780,0 +808,21 @@808+ if (thread.fork != null && !forkHasOwnResumeCursor) {809+ const sessionFork = (yield* providerService.getCapabilities(desiredInstanceId)).sessionFork;810+ if (sessionFork === "latest-turn") {811+ const source = yield* resolveThread(thread.fork.sourceThreadId);812+ const sourceLatestTurn = source?.latestTurn ?? null;813+ const sourceStillAtRecordedBoundary =814+ thread.fork.sourceTurnId === null815+ ? sourceLatestTurn === null816+ : sourceLatestTurn?.turnId === thread.fork.sourceTurnId &&817+ sourceLatestTurn.state === "completed";818+ if (!sourceStillAtRecordedBoundary) {819+ return yield* new ProviderAdapterRequestError({820+ provider: preferredProvider,821+ method: "thread.turn.start",822+ detail: `Source thread '${thread.fork.sourceThreadId}' advanced after fork '${threadId}' was created. Latest-turn providers can only fork from the source's current completed head.`,823+ });824+ }825+ }826+ }827+828+ const startedSession = yield* startProviderSession(apps/mobile/src/features/threads/ThreadRouteScreen.tsx line 853Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -639,0 +826,21 @@826+ onPress: handleDeleteSideChat,827+ });828+ }829+ if (items.length === 0) return null;830+ return withNativeGlassHeaderItem({831+ type: "menu",832+ label: "",833+ accessibilityLabel: "Thread actions",834+ icon: { type: "sfSymbol", name: "ellipsis" },835+ menu: { title: "Thread", items },836+ });837+ }, [838+ handleDeleteSideChat,839+ handlePromoteSideChat,840+ openSideChatThread,841+ selectedThread?.sideChat,842+ sideChatMenuItems,843+ ]);844+ const gitCenterHeaderItems = useThreadGitCenterHeaderItems(threadGitControlProps);845+ const gitRightHeaderItems = useThreadGitRightHeaderItems(threadGitControlProps);846+ const threadCenterHeaderItems = useMemo<NativeHeaderItems>(apps/web/src/components/ChatView.tsx line 7396Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -7200,6 +7382,5 @@72007382 );72017383 const onOpenTurnDiff = useCallback(72027384 (turnId: TurnId, filePath?: string) => {7203- if (!isServerThread || !activeThreadRef) return;7204- useDiffPanelStore.getState().selectTurn(activeThreadRef, turnId, filePath);7205- useRightPanelStore.getState().open(activeThreadRef, "diff");7385+ if (!isServerThread || !panelActionThreadRef) return;7386+ useDiffPanelStore.getState().selectTurn(panelActionThreadRef, turnId, filePath);packages/client-runtime/src/state/threadShell.ts lines 152 to 158Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -100,3 +132,15 @@100132 }).pipe(Atom.withLabel(`environment-thread-refs-by-project:${environmentId}`));101133 });102134135+ const environmentSideChatsByParentAtom = Atom.family((environmentId: EnvironmentId) => {136+ let previous: ReadonlyMap<137+ ThreadId,138+ ReadonlyArray<EnvironmentThreadShell>139+ > = EMPTY_SIDE_CHATS_BY_PARENT;140+ let previousSources: ReadonlyMap<ThreadId, ReadonlyArray<OrchestrationThreadShell>> = new Map();141+ return Atom.make((get): ReadonlyMap<ThreadId, ReadonlyArray<EnvironmentThreadShell>> => {142+ const grouped = new Map<ThreadId, OrchestrationThreadShell[]>();143+ for (const thread of get(environmentThreadsAtom(environmentId))) {144+ const origin = forkOrigin(thread);145+ if (!isSideChat(thread) || origin === null) continue;146+ const siblings = grouped.get(origin.sourceThreadId);apps/web/src/components/ChatView.tsx lines 3836 to 3838OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3659,12 +3827,12 @@3659- if (!activeThreadRef || !isServerThread || !isGitRepo) return;3660- useRightPanelStore.getState().open(activeThreadRef, "diff");3827+ if (!panelActionThreadRef || !isServerThread || !isGitRepo) return;3828+ useRightPanelStore.getState().open(panelActionThreadRef, "diff");36613829 onDiffPanelOpen?.();3662- }, [activeThreadRef, isGitRepo, isServerThread, onDiffPanelOpen]);3830+ }, [isGitRepo, isServerThread, onDiffPanelOpen, panelActionThreadRef]);36633831 const addFilesSurface = useCallback(() => {3664- if (!activeThreadRef || !activeProject) return;3665- useRightPanelStore.getState().open(activeThreadRef, "files");3666- }, [activeProject, activeThreadRef]);3832+ if (!panelActionThreadRef || !activeProject) return;3833+ useRightPanelStore.getState().open(panelActionThreadRef, "files");3834+ }, [activeProject, panelActionThreadRef]);36673835 const addAgentsSurface = useCallback(() => {3668- if (!activeThreadRef) return;3669- useRightPanelStore.getState().open(activeThreadRef, "agents");3670- }, [activeThreadRef]);3836+ if (!panelActionThreadRef) return;3837+ useRightPanelStore.getState().open(panelActionThreadRef, "agents");3838+ }, [panelActionThreadRef]);apps/web/src/components/ChatView.tsx line 7776Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -7546,3 +7758,7 @@75467758 hideEmptyPlaceholder={isDraftHeroState || threadDetailLoading}75477759 topFadeEnabled={!hasTimelineTopBanner}75487760 loadEarlier={loadEarlierTurns}7761+ forkCapability={threadFork.capability}7762+ latestCompletedTurnId={threadFork.latest.target?.turnId ?? null}7763+ onForkAssistantMessage={threadFork.onForkAssistantMessage}7764+ transcriptHeader={transcriptHeader}apps/server/src/orchestration/decider.ts line 911Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -821,3 +902,4 @@821902 ...(command.linkedPullRequest !== undefined822903 ? { linkedPullRequest: command.linkedPullRequest }823904 : {}),905+ ...(command.sideChat !== undefined ? { sideChat: command.sideChat } : {}),apps/web/src/components/ChatView.tsx line 4058Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3860,10 +4040,10 @@38604040 }38614041 }, [activeThreadRef]);38624042 const addTerminalSurface = useCallback(() => {3863- if (!activeThreadRef || !activeThreadId || !activeProject) return;4043+ if (!panelActionThreadRef || !activeThreadRef || !activeThreadId || !activeProject) return;38644044 const cwd = gitCwd ?? activeProject.workspaceRoot;38654045 const terminalId = nextTerminalId(allocatableActiveTerminalIds);3866- useRightPanelStore.getState().openTerminal(activeThreadRef, terminalId);4046+ useRightPanelStore.getState().openTerminal(panelActionThreadRef, terminalId);38674047 setTerminalFocusRequestId((value) => value + 1);38684048 void openTerminal({38694049 environmentId: activeThreadRef.environmentId,apps/web/src/hooks/useThreadFork.ts line 64Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +42,21 @@42+ serverConfigs: ReadonlyMap<EnvironmentId, ServerConfig>,43+) {44+ const instanceId = thread.session?.providerInstanceId ?? thread.modelSelection.instanceId;45+ return serverConfigs46+ .get(thread.environmentId)47+ ?.providers.find((provider) => provider.instanceId === instanceId);48+}49+50+export function useThreadForkActions(51+ sourceThread: ForkableThread | null | undefined,52+ options?: { readonly panelHostThreadId?: ThreadId | null },53+) {54+ const navigate = useNavigate();55+ const serverConfigs = useServerConfigs();56+ const forkCommand = useAtomCommand(threadEnvironment.fork, { reportFailure: false });57+ const capability = sourceThread58+ ? providerConfigForThread(sourceThread, serverConfigs)?.sessionFork59+ : undefined;60+ const latest = useMemo(61+ () =>62+ resolveForkEntryAvailability({@@ -62,3 +71,4 @@6271 },6372 ]6473 : []),74+ ...(state.forkingapps/mobile/src/features/threads/ThreadRouteScreen.tsx line 568OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -498,2 +548,21 @@498548 }, [interruptThreadTurn, selectedThread]);499549550+ const handleForkAssistantMessage = useCallback(551+ async (input: {552+ readonly messageId: MessageId;553+ readonly turnId: TurnId;554+ readonly sideChat: boolean;555+ }) => {556+ const sourceThread = forkSourceThreadRef.current;557+ if (558+ !sourceThread ||559+ !canForkMobileAssistantMessage({560+ capability: forkCapabilityRef.current,561+ completed: true,562+ messageTurnId: input.turnId,563+ latestTurn: latestForkTurnRef.current,564+ })565+ ) {566+ return;567+ }568+ const nextThreadId = ThreadId.make(uuidv4());
- apps/web/src/hooks/useThreadFork.ts lines 145 to 156Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +125,21 @@125+ const forkedThreadRef = scopeThreadRef(currentSourceThread.environmentId, threadId);126+ try {127+ await waitForThreadShell(forkedThreadRef);128+ } catch (error) {129+ toastManager.add(130+ stackedThreadToast({131+ type: "error",132+ title: sideChat ? "Side chat created but not opened" : "Fork created but not opened",133+ description: error instanceof Error ? error.message : "The thread is still syncing.",134+ }),135+ );136+ return false;137+ }138+139+ if (sideChat) {140+ const hostRef = scopeThreadRef(141+ currentSourceThread.environmentId,142+ sourceOverride?.id ?? panelHostThreadIdRef.current ?? currentSourceThread.id,143+ );144+ useRightPanelStore.getState().openSideChat(hostRef, threadId);145+ return true;apps/web/src/hooks/useThreadFork.ts line 142OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +122,21 @@122+ return false;123+ }124+125+ const forkedThreadRef = scopeThreadRef(currentSourceThread.environmentId, threadId);126+ try {127+ await waitForThreadShell(forkedThreadRef);128+ } catch (error) {129+ toastManager.add(130+ stackedThreadToast({131+ type: "error",132+ title: sideChat ? "Side chat created but not opened" : "Fork created but not opened",133+ description: error instanceof Error ? error.message : "The thread is still syncing.",134+ }),135+ );136+ return false;137+ }138+139+ if (sideChat) {140+ const hostRef = scopeThreadRef(141+ currentSourceThread.environmentId,142+ sourceOverride?.id ?? panelHostThreadIdRef.current ?? currentSourceThread.id,
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed