Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sideChatPane.ts

113 lines4.4 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import {
3 resolveSideThreadOpenTarget,
4 type SideThreadOpenIntent,
5} from "@t3tools/client-runtime/side-thread";
6import type {
7 OrchestrationV2ThreadShell,
8 ScopedThreadRef,
9 SideThreadOpenTarget,
10} from "@t3tools/contracts";
11​
12import type { ToolCallDiffTarget } from "../diffPanelStore";
13import type { LinkedThreadTarget } from "@t3tools/client-runtime/linked-thread";
14import type { useNavigate } from "@tanstack/react-router";
15​
16import { getClientSettings } from "../hooks/useSettings";
17import { linkedThreadRouteParams } from "../linkedThreadSurface";
18import { rightPanelHostRef, useRightPanelStore } from "../rightPanelStore";
19​
20/** Whether an event started in the side chat pane (`data-side-chat-pane`), which the main chat ignores. */
21export function isInSideChatPane(target: EventTarget | null): boolean {
22 return target instanceof Element && target.closest("[data-side-chat-pane]") !== null;
24​
25/**
26 * Opens an embedded side thread's tool diffs in its parent's diff panel and runs its shell commands
27 * in the parent's terminal drawer, since the embedded view hides its own. Other panel actions reach
28 * the parent through `hostRightPanel`; diff selection and the drawer are not panel state.
29 */
30export interface SideChatParentPanel {
31 /** The thread whose panel shows the embedded one. */
32 readonly hostRef: ScopedThreadRef;
33 readonly openToolDiff: (target: ToolCallDiffTarget) => void;
34 readonly runShellCommand: (command: string) => void;
36​
37/**
38 * Where opening `threadRef` should route. A side chat shows as a tab of its parent, so this opens
39 * that tab and returns the parent; any other thread returns `threadRef`.
40 */
41export function resolveThreadOpenRoute(
42 threadRef: ScopedThreadRef,
43 thread: Pick<OrchestrationV2ThreadShell, "lineage"> | null,
44): ScopedThreadRef {
45 const parentThreadId = thread?.lineage.sideChat === true ? thread.lineage.parentThreadId : null;
46 if (parentThreadId === null) return threadRef;
47 const parentRef = scopeThreadRef(threadRef.environmentId, parentThreadId);
48 useRightPanelStore.getState().openThread(parentRef, threadRef.threadId);
49 return parentRef;
51​
52/**
53 * Whether `panelRef`'s right panel can show `threadRef` as a tab: a thread of the same environment
54 * that is not the one the panel belongs to.
55 */
56function canOpenThreadInPanel(
57 threadRef: ScopedThreadRef,
58 panelRef: ScopedThreadRef | null,
59): panelRef is ScopedThreadRef {
60 if (panelRef === null) return false;
61 const host = rightPanelHostRef(panelRef);
62 return host.environmentId === threadRef.environmentId && host.threadId !== threadRef.threadId;
64​
65/**
66 * Opens a side chat or subagent where the click asked: as a tab beside `panelRef`, the thread the
67 * click happened in, or on the main screen through `navigate`. Returns where it went.
68 */
69export function openSideThread(input: {
70 readonly threadRef: ScopedThreadRef;
71 readonly panelRef: ScopedThreadRef | null;
72 readonly intent: SideThreadOpenIntent;
73 readonly navigate: (threadRef: ScopedThreadRef) => void;
74}): SideThreadOpenTarget {
75 const { threadRef, panelRef } = input;
76 const target = resolveSideThreadOpenTarget({
77 preference: getClientSettings().sideThreadOpenTarget,
78 intent: input.intent,
79 panelAvailable: canOpenThreadInPanel(threadRef, panelRef),
80 });
81 if (target === "panel" && panelRef !== null) {
82 useRightPanelStore.getState().openThread(panelRef, threadRef.threadId);
83 } else {
84 input.navigate(threadRef);
85 }
86 return target;
88​
89/**
90 * Opens the read-only view of a linked environment's thread where the click asked: as a tab in
91 * `panelRef`'s right panel, or on the main screen, which is also where it goes without a panel.
92 */
93export function openLinkedThreadRead(input: {
94 readonly panelRef: ScopedThreadRef | null;
95 readonly target: LinkedThreadTarget;
96 readonly intent: SideThreadOpenIntent;
97 readonly navigate: ReturnType<typeof useNavigate>;
98}) {
99 const { panelRef, target } = input;
100 const place = resolveSideThreadOpenTarget({
101 preference: getClientSettings().sideThreadOpenTarget,
102 intent: input.intent,
103 panelAvailable: panelRef !== null,
104 });
105 if (place === "panel" && panelRef !== null) {
106 useRightPanelStore.getState().openLinkedThread(panelRef, target);
107 return;
108 }
109 void input.navigate({
110 to: "/linked/$viaEnvironmentId/$linkedEnvironmentId/$linkedThreadId",
111 params: linkedThreadRouteParams(target),
112 });