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