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

projectThreadStartTurn.ts

122 lines4.3 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 { trimCutPrefix } from "@t3tools/shared/String";
18import { msg, plural, part } from "gt-react-native";
19​
20import { m, type MessageResolver } from "../i18n/messages";
21import type { UploadedMobileAttachment } from "./attachmentUpload";
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 titleText?: string;
32 readonly context?: OrchestrationMessageContext;
33 readonly uploadedAttachments: ReadonlyArray<UploadedMobileAttachment | ChatAttachment>;
34 readonly modelSelection: ModelSelection;
35 readonly runtimeMode: RuntimeMode;
36 readonly interactionMode: ProviderInteractionMode;
37 readonly workspaceMode: "local" | "worktree";
38 readonly branch: string | null;
39 readonly worktreePath: string | null;
40 readonly startFromOrigin: boolean;
41 /** Tracked repositories a new worktree takes, by path; absent leaves the choice to the server. */
42 readonly worktreeRepositories?: ReadonlyArray<string>;
43 readonly worktreeBranchName: string;
45​
46export function buildProjectThreadStartTurnInput(spec: ProjectThreadStartTurnSpec) {
47 const title = deriveThreadTitleSeed(
48 { text: spec.titleText ?? spec.text, attachments: spec.uploadedAttachments },
49 m,
50 );
51 const isWorktree = spec.workspaceMode === "worktree";
52 return {
53 commandId: CommandId.make(spec.commandId),
54 creationSource: "mobile" as const,
55 threadId: ThreadId.make(spec.threadId),
56 message: {
57 messageId: MessageId.make(spec.messageId),
58 role: "user" as const,
59 text: spec.text,
60 ...(spec.context ? { context: spec.context } : {}),
61 attachments: spec.uploadedAttachments,
62 },
63 modelSelection: spec.modelSelection,
64 titleSeed: title,
65 runtimeMode: spec.runtimeMode,
66 interactionMode: spec.interactionMode,
67 bootstrap: {
68 createThread: {
69 projectId: spec.projectId,
70 title,
71 modelSelection: spec.modelSelection,
72 runtimeMode: spec.runtimeMode,
73 interactionMode: spec.interactionMode,
74 branch: spec.branch,
75 worktreePath: isWorktree ? null : spec.worktreePath,
76 createdAt: spec.createdAt,
77 },
78 ...(isWorktree
79 ? {
80 prepareWorktree: {
81 projectCwd: spec.projectCwd,
82 // The launch needs a base ref; the server ignores it for a plain project folder.
83 baseBranch: spec.branch ?? "HEAD",
84 branch: spec.worktreeBranchName,
85 ...(spec.startFromOrigin ? { startFromOrigin: true } : {}),
86 ...(spec.worktreeRepositories === undefined
87 ? {}
88 : { repositories: spec.worktreeRepositories.map((path) => ({ path })) }),
89 },
90 runSetupScript: true,
91 }
92 : {}),
93 },
94 createdAt: spec.createdAt,
95 };
97​
98export function deriveThreadTitleFromPrompt(value: string, resolve: MessageResolver = m): string {
99 const title = threadTitleTextFromPrompt(value, resolve) || resolve(msg("New thread"));
100 if (title.length <= 72) return title;
101 const end = splitsSurrogatePair(title, 69) ? 68 : 69;
102 return `${trimCutPrefix(title.slice(0, end), title.slice(end))}...`;
104​
105/** Titles a new task the server has not created yet; one with only attachments shows their count. */
106export function deriveUnsentThreadTitle(
107 input: { readonly text: string; readonly attachments: ReadonlyArray<unknown> },
108 resolve: MessageResolver = m,
109): string {
110 if (input.text.trim().length > 0 || input.attachments.length === 0) {
111 return deriveThreadTitleFromPrompt(input.text, resolve);
112 }
113 return resolve(
114 msg`${plural(
115 { count: input.attachments.length },
116 {
117 one: part`${{ count: input.attachments.length }} attachment`,
118 other: part`${{ count: input.attachments.length }} attachments`,
119 },
120 )}`,
121 );