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

chatThreadActions.ts

103 lines3.0 KB

1import { scopeProjectRef } from "@v12/client-runtime/environment";
2import type { EnvironmentId, ProjectId, ScopedProjectRef } from "@v12/contracts";
3import type { DraftThreadEnvMode } from "../composerDraftStore";
4​
5interface ThreadContextLike {
6 environmentId: EnvironmentId;
7 projectId: ProjectId;
8 branch: string | null;
9 worktreePath: string | null;
11​
12interface DraftThreadContextLike extends ThreadContextLike {
13 envMode: DraftThreadEnvMode;
14 startFromOrigin: boolean;
16​
17interface NewThreadHandler {
18 (
19 projectRef: ScopedProjectRef,
20 options?: {
21 branch?: string | null;
22 worktreePath?: string | null;
23 envMode?: DraftThreadEnvMode;
24 startFromOrigin?: boolean;
25 },
26 ): Promise<void>;
28​
29type NewThreadOptions = NonNullable<Parameters<NewThreadHandler>[1]>;
30​
31export interface ChatThreadActionContext {
32 readonly activeDraftThread: DraftThreadContextLike | null;
33 readonly activeThread: ThreadContextLike | undefined;
34 readonly defaultProjectRef: ScopedProjectRef | null;
35 readonly handleNewThread: NewThreadHandler;
37​
38export function resolveNewDraftStartFromOrigin(input: {
39 envMode: DraftThreadEnvMode;
40 newWorktreesStartFromOrigin: boolean;
41}): boolean {
42 return input.envMode === "worktree" && input.newWorktreesStartFromOrigin;
44​
45export function resolveThreadActionProjectRef(
46 context: ChatThreadActionContext,
47): ScopedProjectRef | null {
48 if (context.activeThread) {
49 return scopeProjectRef(context.activeThread.environmentId, context.activeThread.projectId);
50 }
51 if (context.activeDraftThread) {
52 return scopeProjectRef(
53 context.activeDraftThread.environmentId,
54 context.activeDraftThread.projectId,
55 );
56 }
57 return context.defaultProjectRef;
59​
60function buildContextualThreadOptions(context: ChatThreadActionContext): NewThreadOptions {
61 return {
62 branch: context.activeThread?.branch ?? context.activeDraftThread?.branch ?? null,
63 worktreePath:
64 context.activeThread?.worktreePath ?? context.activeDraftThread?.worktreePath ?? null,
65 envMode:
66 context.activeDraftThread?.envMode ??
67 (context.activeThread?.worktreePath ? "worktree" : "local"),
68 ...(context.activeDraftThread
69 ? { startFromOrigin: context.activeDraftThread.startFromOrigin }
70 : {}),
71 };
73​
74export async function startNewThreadInProjectFromContext(
75 context: ChatThreadActionContext,
76 projectRef: ScopedProjectRef,
77): Promise<void> {
78 await context.handleNewThread(projectRef, buildContextualThreadOptions(context));
80​
81export async function startNewThreadFromContext(
82 context: ChatThreadActionContext,
83): Promise<boolean> {
84 const projectRef = resolveThreadActionProjectRef(context);
85 if (!projectRef) {
86 return false;
87 }
88​
89 await startNewThreadInProjectFromContext(context, projectRef);
90 return true;
92​
93export async function startNewLocalThreadFromContext(
94 context: ChatThreadActionContext,
95): Promise<boolean> {
96 const projectRef = resolveThreadActionProjectRef(context);
97 if (!projectRef) {
98 return false;
99 }
100​
101 await context.handleNewThread(projectRef);
102 return true;