Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import type { OrchestrationV2ThreadShell, ScopedThreadRef } from "@t3tools/contracts";
3
4import type { ToolCallDiffTarget } from "../diffPanelStore";
5import { useRightPanelStore } from "../rightPanelStore";
6
7/** Whether an event started in the side chat pane (`data-side-chat-pane`), which the main chat ignores. */
8export function isInSideChatPane(target: EventTarget | null): boolean {
9 return target instanceof Element && target.closest("[data-side-chat-pane]") !== null;
10}
11
12/**
13 * Opens an embedded side chat's tool diffs in its parent's diff panel and runs its shell commands
14 * in the parent's terminal drawer, since the embedded view hides its own. Other panel actions reach
15 * the parent through `hostRightPanel`; diff selection and the drawer are not panel state.
16 */
17export interface SideChatParentPanel {
18 readonly openToolDiff: (target: ToolCallDiffTarget) => void;
19 readonly runShellCommand: (command: string) => void;
20}
21
22/**
23 * Where opening `threadRef` should route. A side chat shows as a tab of its parent, so this opens
24 * that tab and returns the parent; any other thread returns `threadRef`.
25 */
26export function resolveThreadOpenRoute(
27 threadRef: ScopedThreadRef,
28 thread: Pick<OrchestrationV2ThreadShell, "lineage"> | null,
29): ScopedThreadRef {
30 const parentThreadId = thread?.lineage.sideChat === true ? thread.lineage.parentThreadId : null;
31 if (parentThreadId === null) return threadRef;
32 const parentRef = scopeThreadRef(threadRef.environmentId, parentThreadId);
33 useRightPanelStore.getState().openSideChat(parentRef, threadRef.threadId);
34 return parentRef;
35}