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

projectThreadStartTurn.ts

97 lines3.2 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";
11​
12import { toUploadChatImageAttachments, type DraftComposerAttachment } from "./composerImages";
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 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
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.attachments });
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:
53 spec.uploadedAttachments ??
54 toUploadChatImageAttachments(
55 spec.attachments.filter((attachment) => attachment.type === "image"),
56 ),
57 },
58 modelSelection: spec.modelSelection,
59 titleSeed: title,
60 runtimeMode: spec.runtimeMode,
61 interactionMode: spec.interactionMode,
62 bootstrap: {
63 createThread: {
64 projectId: spec.projectId,
65 title,
66 modelSelection: spec.modelSelection,
67 runtimeMode: spec.runtimeMode,
68 interactionMode: spec.interactionMode,
69 branch: spec.branch,
70 worktreePath: isWorktree ? null : spec.worktreePath,
71 createdAt: spec.createdAt,
72 },
73 ...(isWorktree
74 ? {
75 prepareWorktree: {
76 projectCwd: spec.projectCwd,
77 baseBranch: spec.branch!,
78 branch: spec.worktreeBranchName,
79 ...(spec.startFromOrigin ? { startFromOrigin: true } : {}),
80 },
81 runSetupScript: true,
82 }
83 : {}),
84 },
85 createdAt: spec.createdAt,
86 };
88​
89export function deriveThreadTitleFromPrompt(value: string): string {
90 const trimmed = value.trim();
91 if (trimmed.length === 0) {
92 return "New thread";
93 }
94​
95 const compact = trimmed.replace(/\s+/g, " ");
96 return compact.length <= 72 ? compact : `${compact.slice(0, 69).trimEnd()}...`;