Open History to see the last commit
1import {
2 CommandId,
3 MessageId,
4 ThreadId,
5 type ModelSelection,
6 type ProjectId,
7 type ProviderInteractionMode,
8 type RuntimeMode,
9} from "@t3tools/contracts";
10
11import { toUploadChatImageAttachments, type DraftComposerImageAttachment } from "./composerImages";
12
13export function deriveThreadTitleFromPrompt(value: string): string {
14 const trimmed = value.trim();
15 if (trimmed.length === 0) {
16 return "New thread";
17 }
18
19 const compact = trimmed.replace(/\s+/g, " ");
20 return compact.length <= 72 ? compact : `${compact.slice(0, 69).trimEnd()}...`;
21}
22
23export interface ProjectThreadStartTurnSpec {
24 readonly projectId: ProjectId;
25 readonly projectCwd: string;
26 readonly threadId: string;
27 readonly commandId: string;
28 readonly messageId: string;
29 readonly createdAt: string;
30 readonly text: string;
31 readonly attachments: ReadonlyArray<DraftComposerImageAttachment>;
32 readonly modelSelection: ModelSelection;
33 readonly runtimeMode: RuntimeMode;
34 readonly interactionMode: ProviderInteractionMode;
35 readonly workspaceMode: "local" | "worktree";
36 readonly branch: string | null;
37 readonly worktreePath: string | null;
38 readonly startFromOrigin: boolean;
39 /** Generated temp branch for worktree mode; unused for local mode. */
40 readonly worktreeBranchName: string;
41}
42
43/**
44 * Single source of the `thread.turn.start` bootstrap payload used to create a
45 * thread from a project draft — shared by the immediate send path and the
46 * offline outbox drain so both deliver identical commands.
47 */
48export function buildProjectThreadStartTurnInput(spec: ProjectThreadStartTurnSpec) {
49 const title = deriveThreadTitleFromPrompt(spec.text);
50 const isWorktree = spec.workspaceMode === "worktree";
51 return {
52 commandId: CommandId.make(spec.commandId),
53 creationSource: "mobile" as const,
54 threadId: ThreadId.make(spec.threadId),
55 message: {
56 messageId: MessageId.make(spec.messageId),
57 role: "user" as const,
58 text: spec.text,
59 attachments: toUploadChatImageAttachments(spec.attachments),
60 },
61 modelSelection: spec.modelSelection,
62 titleSeed: title,
63 runtimeMode: spec.runtimeMode,
64 interactionMode: spec.interactionMode,
65 bootstrap: {
66 createThread: {
67 projectId: spec.projectId,
68 title,
69 modelSelection: spec.modelSelection,
70 runtimeMode: spec.runtimeMode,
71 interactionMode: spec.interactionMode,
72 branch: spec.branch,
73 worktreePath: isWorktree ? null : spec.worktreePath,
74 createdAt: spec.createdAt,
75 },
76 ...(isWorktree
77 ? {
78 prepareWorktree: {
79 projectCwd: spec.projectCwd,
80 baseBranch: spec.branch!,
81 branch: spec.worktreeBranchName,
82 ...(spec.startFromOrigin ? { startFromOrigin: true } : {}),
83 },
84 runSetupScript: true,
85 }
86 : {}),
87 },
88 createdAt: spec.createdAt,
89 };
90}