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

projectThreadStartTurn.ts

95 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";
10​
11import { toUploadChatImageAttachments, type DraftComposerAttachment } from "./composerImages";
12import type { UploadedMobileAttachment } from "./attachmentUpload";
13​
14export function deriveThreadTitleFromPrompt(value: string): string {
15 const trimmed = 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 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
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:
61 spec.uploadedAttachments ??
62 toUploadChatImageAttachments(
63 spec.attachments.filter((attachment) => attachment.type === "image"),
64 ),
65 },
66 modelSelection: spec.modelSelection,
67 titleSeed: title,
68 runtimeMode: spec.runtimeMode,
69 interactionMode: spec.interactionMode,
70 bootstrap: {
71 createThread: {
72 projectId: spec.projectId,
73 title,
74 modelSelection: spec.modelSelection,
75 runtimeMode: spec.runtimeMode,
76 interactionMode: spec.interactionMode,
77 branch: spec.branch,
78 worktreePath: isWorktree ? null : spec.worktreePath,
79 createdAt: spec.createdAt,
80 },
81 ...(isWorktree
82 ? {
83 prepareWorktree: {
84 projectCwd: spec.projectCwd,
85 baseBranch: spec.branch!,
86 branch: spec.worktreeBranchName,
87 ...(spec.startFromOrigin ? { startFromOrigin: true } : {}),
88 },
89 runSetupScript: true,
90 }
91 : {}),
92 },
93 createdAt: spec.createdAt,
94 };