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