Open History to see the last commit
1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
3import type {
4 EnvironmentId,
5 ModelSelection,
6 ProjectId,
7 ScopedProjectRef,
8} from "@t3tools/contracts";
9import type { ComposerThreadDraftState, DraftThreadEnvMode } from "../composerDraftStore";
10
11type ComposerModelSelectionState = Pick<
12 ComposerThreadDraftState,
13 "activeProvider" | "modelSelectionByProvider" | "modelSelectionExplicit"
14>;
15
16interface ThreadContextLike {
17 environmentId: EnvironmentId;
18 projectId: ProjectId;
19}
20
21interface NewThreadHandler {
22 (
23 projectRef: ScopedProjectRef,
24 options?: {
25 branch?: string | null;
26 worktreePath?: string | null;
27 envMode?: DraftThreadEnvMode;
28 startFromOrigin?: boolean;
29 },
30 ): Promise<unknown>;
31}
32
33export interface ChatThreadActionContext {
34 readonly activeDraftThread: ThreadContextLike | null;
35 readonly activeThread: ThreadContextLike | undefined;
36 readonly defaultProjectRef: ScopedProjectRef | null;
37 readonly handleNewThread: NewThreadHandler;
38}
39
40export function resolveNewDraftStartFromOrigin(input: {
41 envMode: DraftThreadEnvMode;
42 newWorktreesStartFromOrigin: boolean;
43}): boolean {
44 return input.envMode === "worktree" && input.newWorktreesStartFromOrigin;
45}
46
47export function resolveNewThreadModelSelectionOverride(input: {
48 readonly projectDefaultSelection: ModelSelection | null;
49 readonly carrySelection: ModelSelection | null;
50 readonly carrySourceDraftId: string | null;
51 readonly destinationDraftId: string;
52}): ModelSelection | null {
53 return (
54 input.projectDefaultSelection ??
55 (input.carrySourceDraftId === input.destinationDraftId ? null : input.carrySelection)
56 );
57}
58
59export function hasExplicitComposerModelSelection(
60 draft: ComposerModelSelectionState | null | undefined,
61): boolean {
62 const activeProvider = draft?.activeProvider;
63 return (
64 draft?.modelSelectionExplicit === true &&
65 activeProvider !== null &&
66 activeProvider !== undefined &&
67 draft.modelSelectionByProvider[activeProvider] !== undefined
68 );
69}
70
71export function threadHasStarted(
72 thread: Pick<EnvironmentThreadShell, "latestRun" | "itemCount" | "runtime"> | null | undefined,
73): boolean {
74 return Boolean(thread && (thread.latestRun !== null || thread.itemCount > 0 || thread.runtime));
75}
76
77/**
78 * A draft is seeded from sticky state, project defaults, prompt links, or a carried selection.
79 * That seed is what an unstarted thread sends; once the thread has started, its own selection
80 * wins unless the user picked a model or traits for it.
81 */
82export function draftModelSelectionApplies(
83 draft: Pick<ComposerThreadDraftState, "modelSelectionExplicit"> | null | undefined,
84 threadStarted: boolean,
85): boolean {
86 return !threadStarted || draft?.modelSelectionExplicit === true;
87}
88
89export function resolveThreadActionProjectRef(
90 context: ChatThreadActionContext,
91): ScopedProjectRef | null {
92 if (context.activeThread) {
93 return scopeProjectRef(context.activeThread.environmentId, context.activeThread.projectId);
94 }
95 if (context.activeDraftThread) {
96 return scopeProjectRef(
97 context.activeDraftThread.environmentId,
98 context.activeDraftThread.projectId,
99 );
100 }
101 return context.defaultProjectRef;
102}
103
104export async function startNewThreadFromContext(
105 context: ChatThreadActionContext,
106): Promise<boolean> {
107 const projectRef = resolveThreadActionProjectRef(context);
108 if (!projectRef) {
109 return false;
110 }
111
112 await context.handleNewThread(projectRef);
113 return true;
114}