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

projectThreadStartTurn.ts

96 lines3.3 KB

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