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

projectThreadStartTurn.ts

102 lines3.5 KB

1import {
2 CommandId,
3 MessageId,
4 ThreadId,
5 type ChatAttachment,
6 type ModelSelection,
7 type OrchestrationMessageContext,
8 type ProjectId,
9 type ProviderInteractionMode,
10 type RuntimeMode,
11} from "@t3tools/contracts";
12import {
13 deriveThreadTitleSeed,
14 threadTitleTextFromPrompt,
15} from "@t3tools/client-runtime/operations";
16import { splitsSurrogatePair } from "@t3tools/shared/assistantCitations";
17import { msg } from "gt-react-native";
18​
19import { m } from "../i18n/messages";
20import type { UploadedMobileAttachment } from "./attachmentUpload";
21​
22export interface ProjectThreadStartTurnSpec {
23 readonly projectId: ProjectId;
24 readonly projectCwd: string;
25 readonly threadId: string;
26 readonly commandId: string;
27 readonly messageId: string;
28 readonly createdAt: string;
29 readonly text: string;
30 readonly titleText?: string;
31 readonly context?: OrchestrationMessageContext;
32 readonly uploadedAttachments: ReadonlyArray<UploadedMobileAttachment | ChatAttachment>;
33 readonly modelSelection: ModelSelection;
34 readonly runtimeMode: RuntimeMode;
35 readonly interactionMode: ProviderInteractionMode;
36 readonly workspaceMode: "local" | "worktree";
37 readonly branch: string | null;
38 readonly worktreePath: string | null;
39 readonly startFromOrigin: boolean;
40 /** Tracked repositories a new worktree takes, by path; absent leaves the choice to the server. */
41 readonly worktreeRepositories?: ReadonlyArray<string>;
42 readonly worktreeBranchName: string;
44​
45export function buildProjectThreadStartTurnInput(spec: ProjectThreadStartTurnSpec) {
46 const title = deriveThreadTitleSeed(
47 { text: spec.titleText ?? spec.text, attachments: spec.uploadedAttachments },
48 m,
49 );
50 const isWorktree = spec.workspaceMode === "worktree";
51 return {
52 commandId: CommandId.make(spec.commandId),
53 creationSource: "mobile" as const,
54 threadId: ThreadId.make(spec.threadId),
55 message: {
56 messageId: MessageId.make(spec.messageId),
57 role: "user" as const,
58 text: spec.text,
59 ...(spec.context ? { context: spec.context } : {}),
60 attachments: spec.uploadedAttachments,
61 },
62 modelSelection: spec.modelSelection,
63 titleSeed: title,
64 runtimeMode: spec.runtimeMode,
65 interactionMode: spec.interactionMode,
66 bootstrap: {
67 createThread: {
68 projectId: spec.projectId,
69 title,
70 modelSelection: spec.modelSelection,
71 runtimeMode: spec.runtimeMode,
72 interactionMode: spec.interactionMode,
73 branch: spec.branch,
74 worktreePath: isWorktree ? null : spec.worktreePath,
75 createdAt: spec.createdAt,
76 },
77 ...(isWorktree
78 ? {
79 prepareWorktree: {
80 projectCwd: spec.projectCwd,
81 // The launch needs a base ref; the server ignores it for a plain project folder.
82 baseBranch: spec.branch ?? "HEAD",
83 branch: spec.worktreeBranchName,
84 ...(spec.startFromOrigin ? { startFromOrigin: true } : {}),
85 ...(spec.worktreeRepositories === undefined
86 ? {}
87 : { repositories: spec.worktreeRepositories.map((path) => ({ path })) }),
88 },
89 runSetupScript: true,
90 }
91 : {}),
92 },
93 createdAt: spec.createdAt,
94 };
96​
97export function deriveThreadTitleFromPrompt(value: string): string {
98 const title = threadTitleTextFromPrompt(value, m) || m(msg("New thread"));
99 if (title.length <= 72) return title;
100 const end = splitsSurrogatePair(title, 69) ? 68 : 69;
101 return `${title.slice(0, end).trimEnd()}...`;