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

projectThreadStartTurn.ts

96 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 { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations";
11​
12import { toUploadChatImageAttachments, type DraftComposerAttachment } from "./composerImages";
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()}...`;
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 attachments: ReadonlyArray<DraftComposerAttachment>;
34 readonly uploadedAttachments?: ReadonlyArray<UploadedMobileAttachment>;
35 readonly modelSelection: ModelSelection;
36 readonly runtimeMode: RuntimeMode;
37 readonly interactionMode: ProviderInteractionMode;
38 readonly workspaceMode: "local" | "worktree";
39 readonly branch: string | null;
40 readonly worktreePath: string | null;
41 readonly startFromOrigin: boolean;
42 /** Generated temp branch for worktree mode; unused for local mode. */
43 readonly worktreeBranchName: string;
45​
46/**
47 * Single source of the `thread.turn.start` bootstrap payload used to create a
48 * thread from a project draft — shared by the immediate send path and the
49 * offline outbox drain so both deliver identical commands.
50 */
51export function buildProjectThreadStartTurnInput(spec: ProjectThreadStartTurnSpec) {
52 const title = deriveThreadTitleFromPrompt(spec.text);
53 const isWorktree = spec.workspaceMode === "worktree";
54 return {
55 commandId: CommandId.make(spec.commandId),
56 threadId: ThreadId.make(spec.threadId),
57 message: {
58 messageId: MessageId.make(spec.messageId),
59 role: "user" as const,
60 text: spec.text,
61 attachments:
62 spec.uploadedAttachments ??
63 toUploadChatImageAttachments(
64 spec.attachments.filter((attachment) => attachment.type === "image"),
65 ),
66 },
67 modelSelection: spec.modelSelection,
68 titleSeed: title,
69 runtimeMode: spec.runtimeMode,
70 interactionMode: spec.interactionMode,
71 bootstrap: {
72 createThread: {
73 projectId: spec.projectId,
74 title,
75 modelSelection: spec.modelSelection,
76 runtimeMode: spec.runtimeMode,
77 interactionMode: spec.interactionMode,
78 branch: spec.branch,
79 worktreePath: isWorktree ? null : spec.worktreePath,
80 createdAt: spec.createdAt,
81 },
82 ...(isWorktree
83 ? {
84 prepareWorktree: {
85 projectCwd: spec.projectCwd,
86 baseBranch: spec.branch!,
87 branch: spec.worktreeBranchName,
88 ...(spec.startFromOrigin ? { startFromOrigin: true } : {}),
89 },
90 runSetupScript: true,
91 }
92 : {}),
93 },
94 createdAt: spec.createdAt,
95 };