Open History to see the last commit
1import { scopeProjectRef } from "@t3tools/client-runtime";
2import type { EnvironmentId, ProjectId, ScopedProjectRef } from "@t3tools/contracts";
3import type { DraftThreadEnvMode } from "../composerDraftStore";
4
5interface ThreadContextLike {
6 environmentId: EnvironmentId;
7 projectId: ProjectId;
8 branch: string | null;
9 worktreePath: string | null;
10}
11
12interface DraftThreadContextLike extends ThreadContextLike {
13 envMode: DraftThreadEnvMode;
14}
15
16interface NewThreadHandler {
17 (
18 projectRef: ScopedProjectRef,
19 options?: {
20 branch?: string | null;
21 worktreePath?: string | null;
22 envMode?: DraftThreadEnvMode;
23 },
24 ): Promise<void>;
25}
26
27type NewThreadOptions = NonNullable<Parameters<NewThreadHandler>[1]>;
28
29export interface ChatThreadActionContext {
30 readonly activeDraftThread: DraftThreadContextLike | null;
31 readonly activeThread: ThreadContextLike | undefined;
32 readonly defaultProjectRef: ScopedProjectRef | null;
33 readonly defaultThreadEnvMode: DraftThreadEnvMode;
34 readonly handleNewThread: NewThreadHandler;
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
52function buildContextualThreadOptions(context: ChatThreadActionContext): NewThreadOptions {
53 return {
54 branch: context.activeThread?.branch ?? context.activeDraftThread?.branch ?? null,
55 worktreePath:
56 context.activeThread?.worktreePath ?? context.activeDraftThread?.worktreePath ?? null,
57 envMode:
58 context.activeDraftThread?.envMode ??
59 (context.activeThread?.worktreePath ? "worktree" : "local"),
60 };
61}
62
63function buildDefaultThreadOptions(context: ChatThreadActionContext): NewThreadOptions {
64 return {
65 envMode: context.defaultThreadEnvMode,
66 };
67}
68
69export async function startNewThreadInProjectFromContext(
70 context: ChatThreadActionContext,
71 projectRef: ScopedProjectRef,
72): Promise<void> {
73 await context.handleNewThread(projectRef, buildContextualThreadOptions(context));
74}
75
76export async function startNewThreadFromContext(
77 context: ChatThreadActionContext,
78): Promise<boolean> {
79 const projectRef = resolveThreadActionProjectRef(context);
80 if (!projectRef) {
81 return false;
82 }
83
84 await startNewThreadInProjectFromContext(context, projectRef);
85 return true;
86}
87
88export async function startNewLocalThreadFromContext(
89 context: ChatThreadActionContext,
90): Promise<boolean> {
91 const projectRef = resolveThreadActionProjectRef(context);
92 if (!projectRef) {
93 return false;
94 }
95
96 await context.handleNewThread(projectRef, buildDefaultThreadOptions(context));
97 return true;
98}