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 // The opened draft's identity, which most callers have no use for.
20 ): Promise<unknown>;
21}
22
23export interface ChatThreadActionContext {
24 readonly activeDraftThread: ThreadContextLike | null;
25 readonly activeThread: ThreadContextLike | undefined;
26 readonly defaultProjectRef: ScopedProjectRef | null;
27 readonly handleNewThread: NewThreadHandler;
28}
29
30export function resolveNewDraftStartFromOrigin(input: {
31 envMode: DraftThreadEnvMode;
32 newWorktreesStartFromOrigin: boolean;
33}): boolean {
34 return input.envMode === "worktree" && input.newWorktreesStartFromOrigin;
35}
36
37export function resolveThreadActionProjectRef(
38 context: ChatThreadActionContext,
39): ScopedProjectRef | null {
40 if (context.activeThread) {
41 return scopeProjectRef(context.activeThread.environmentId, context.activeThread.projectId);
42 }
43 if (context.activeDraftThread) {
44 return scopeProjectRef(
45 context.activeDraftThread.environmentId,
46 context.activeDraftThread.projectId,
47 );
48 }
49 return context.defaultProjectRef;
50}
51
52// New threads inherit only the *project* from the current context. Branch,
53// worktree, and env mode always come from the user's configured defaults —
54// carrying them over from the viewed thread meant "new thread" silently
55// reused checkouts and branches. Explicit affordances (branch toolbar's
56// "new thread in this worktree") pass those options to handleNewThread
57// directly instead.
58export async function startNewThreadFromContext(
59 context: ChatThreadActionContext,
60): Promise<boolean> {
61 const projectRef = resolveThreadActionProjectRef(context);
62 if (!projectRef) {
63 return false;
64 }
65
66 await context.handleNewThread(projectRef);
67 return true;
68}