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

projectThreadStartTurn.ts

91 lines3.0 KB

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