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

projectThreadStartTurn.ts

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