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

projectThreadStartTurn.ts

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