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

chatThreadActions.ts

85 lines2.6 KB

1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import type {
3 EnvironmentId,
4 ModelSelection,
5 ProjectId,
6 ScopedProjectRef,
7} from "@t3tools/contracts";
8import type { DraftThreadEnvMode } from "../composerDraftStore";
9​
10interface ThreadContextLike {
11 environmentId: EnvironmentId;
12 projectId: ProjectId;
14​
15interface NewThreadHandler {
16 (
17 projectRef: ScopedProjectRef,
18 options?: {
19 branch?: string | null;
20 worktreePath?: string | null;
21 envMode?: DraftThreadEnvMode;
22 startFromOrigin?: boolean;
23 },
24 // The opened draft's identity, which most callers have no use for.
25 ): Promise<unknown>;
27​
28export interface ChatThreadActionContext {
29 readonly activeDraftThread: ThreadContextLike | null;
30 readonly activeThread: ThreadContextLike | undefined;
31 readonly defaultProjectRef: ScopedProjectRef | null;
32 readonly handleNewThread: NewThreadHandler;
34​
35export function resolveNewDraftStartFromOrigin(input: {
36 envMode: DraftThreadEnvMode;
37 newWorktreesStartFromOrigin: boolean;
38}): boolean {
39 return input.envMode === "worktree" && input.newWorktreesStartFromOrigin;
41​
42export function resolveNewThreadModelSelectionOverride(input: {
43 readonly projectDefaultSelection: ModelSelection | null;
44 readonly carrySelection: ModelSelection | null;
45 readonly carrySourceDraftId: string | null;
46 readonly destinationDraftId: string;
47}): ModelSelection | null {
48 return (
49 input.projectDefaultSelection ??
50 (input.carrySourceDraftId === input.destinationDraftId ? null : input.carrySelection)
51 );
53​
54export function resolveThreadActionProjectRef(
55 context: ChatThreadActionContext,
56): ScopedProjectRef | null {
57 if (context.activeThread) {
58 return scopeProjectRef(context.activeThread.environmentId, context.activeThread.projectId);
59 }
60 if (context.activeDraftThread) {
61 return scopeProjectRef(
62 context.activeDraftThread.environmentId,
63 context.activeDraftThread.projectId,
64 );
65 }
66 return context.defaultProjectRef;
68​
69// New threads inherit only the *project* from the current context. Branch,
70// worktree, and env mode always come from the user's configured defaults —
71// carrying them over from the viewed thread meant "new thread" silently
72// reused checkouts and branches. Explicit affordances (branch toolbar's
73// "new thread in this worktree") pass those options to handleNewThread
74// directly instead.
75export async function startNewThreadFromContext(
76 context: ChatThreadActionContext,
77): Promise<boolean> {
78 const projectRef = resolveThreadActionProjectRef(context);
79 if (!projectRef) {
80 return false;
81 }
82​
83 await context.handleNewThread(projectRef);
84 return true;