Open History to see the last commit
1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId, ProjectId, ScopedProjectRef } from "@t3tools/contracts";
3import type { DraftThreadEnvMode } from "../composerDraftStore";
4
5interface ThreadContextLike {
6 environmentId: EnvironmentId;
7 projectId: ProjectId;
8}
9
10interface NewThreadHandler {
11 (
12 projectRef: ScopedProjectRef,
13 options?: {
14 branch?: string | null;
15 worktreePath?: string | null;
16 envMode?: DraftThreadEnvMode;
17 startFromOrigin?: boolean;
18 },
19 ): Promise<void>;
20}
21
22export interface ChatThreadActionContext {
23 readonly activeDraftThread: ThreadContextLike | null;
24 readonly activeThread: ThreadContextLike | undefined;
25 readonly defaultProjectRef: ScopedProjectRef | null;
26 readonly handleNewThread: NewThreadHandler;
27}
28
29export function resolveNewDraftStartFromOrigin(input: {
30 envMode: DraftThreadEnvMode;
31 newWorktreesStartFromOrigin: boolean;
32}): boolean {
33 return input.envMode === "worktree" && input.newWorktreesStartFromOrigin;
34}
35
36export function resolveThreadActionProjectRef(
37 context: ChatThreadActionContext,
38): ScopedProjectRef | null {
39 if (context.activeThread) {
40 return scopeProjectRef(context.activeThread.environmentId, context.activeThread.projectId);
41 }
42 if (context.activeDraftThread) {
43 return scopeProjectRef(
44 context.activeDraftThread.environmentId,
45 context.activeDraftThread.projectId,
46 );
47 }
48 return context.defaultProjectRef;
49}
50
51// New threads inherit only the *project* from the current context. Branch,
52// worktree, and env mode always come from the user's configured defaults —
53// carrying them over from the viewed thread meant "new thread" silently
54// reused checkouts and branches. Explicit affordances (branch toolbar's
55// "new thread in this worktree") pass those options to handleNewThread
56// directly instead.
57export async function startNewThreadFromContext(
58 context: ChatThreadActionContext,
59): Promise<boolean> {
60 const projectRef = resolveThreadActionProjectRef(context);
61 if (!projectRef) {
62 return false;
63 }
64
65 await context.handleNewThread(projectRef);
66 return true;
67}