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

ChatView.logic.ts

143 lines4.2 KB

1import { ProjectId, type ProviderKind, type ThreadId } from "@t3tools/contracts";
2import { type ChatMessage, type Thread } from "../types";
3import { randomUUID } from "~/lib/utils";
4import { getAppModelOptions } from "../appSettings";
5import { type ComposerImageAttachment, type DraftThreadState } from "../composerDraftStore";
6import { Schema } from "effect";
7​
8export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project";
9const WORKTREE_BRANCH_PREFIX = "t3code";
10​
11export const LastInvokedScriptByProjectSchema = Schema.Record(ProjectId, Schema.String);
12​
13export function buildLocalDraftThread(
14 threadId: ThreadId,
15 draftThread: DraftThreadState,
16 fallbackModel: string,
17 error: string | null,
18): Thread {
19 return {
20 id: threadId,
21 codexThreadId: null,
22 projectId: draftThread.projectId,
23 title: "New thread",
24 model: fallbackModel,
25 runtimeMode: draftThread.runtimeMode,
26 interactionMode: draftThread.interactionMode,
27 session: null,
28 messages: [],
29 error,
30 createdAt: draftThread.createdAt,
31 latestTurn: null,
32 lastVisitedAt: draftThread.createdAt,
33 branch: draftThread.branch,
34 worktreePath: draftThread.worktreePath,
35 turnDiffSummaries: [],
36 activities: [],
37 proposedPlans: [],
38 };
40​
41export function revokeBlobPreviewUrl(previewUrl: string | undefined): void {
42 if (!previewUrl || typeof URL === "undefined" || !previewUrl.startsWith("blob:")) {
43 return;
44 }
45 URL.revokeObjectURL(previewUrl);
47​
48export function revokeUserMessagePreviewUrls(message: ChatMessage): void {
49 if (message.role !== "user" || !message.attachments) {
50 return;
51 }
52 for (const attachment of message.attachments) {
53 if (attachment.type !== "image") {
54 continue;
55 }
56 revokeBlobPreviewUrl(attachment.previewUrl);
57 }
59​
60export function collectUserMessageBlobPreviewUrls(message: ChatMessage): string[] {
61 if (message.role !== "user" || !message.attachments) {
62 return [];
63 }
64 const previewUrls: string[] = [];
65 for (const attachment of message.attachments) {
66 if (attachment.type !== "image") continue;
67 if (!attachment.previewUrl || !attachment.previewUrl.startsWith("blob:")) continue;
68 previewUrls.push(attachment.previewUrl);
69 }
70 return previewUrls;
72​
73export type SendPhase = "idle" | "preparing-worktree" | "sending-turn";
74​
75export interface PullRequestDialogState {
76 initialReference: string | null;
77 key: number;
79​
80export function readFileAsDataUrl(file: File): Promise<string> {
81 return new Promise((resolve, reject) => {
82 const reader = new FileReader();
83 reader.addEventListener("load", () => {
84 if (typeof reader.result === "string") {
85 resolve(reader.result);
86 return;
87 }
88 reject(new Error("Could not read image data."));
89 });
90 reader.addEventListener("error", () => {
91 reject(reader.error ?? new Error("Failed to read image."));
92 });
93 reader.readAsDataURL(file);
94 });
96​
97export function buildTemporaryWorktreeBranchName(): string {
98 // Keep the 8-hex suffix shape for backend temporary-branch detection.
99 const token = randomUUID().slice(0, 8).toLowerCase();
100 return `${WORKTREE_BRANCH_PREFIX}/${token}`;
102​
103export function cloneComposerImageForRetry(
104 image: ComposerImageAttachment,
105): ComposerImageAttachment {
106 if (typeof URL === "undefined" || !image.previewUrl.startsWith("blob:")) {
107 return image;
108 }
109 try {
110 return {
111 ...image,
112 previewUrl: URL.createObjectURL(image.file),
113 };
114 } catch {
115 return image;
116 }
118​
119export function getCustomModelOptionsByProvider(settings: {
120 customCodexModels: readonly string[];
121 customClaudeModels: readonly string[];
122}): Record<ProviderKind, ReadonlyArray<{ slug: string; name: string }>> {
123 return {
124 codex: getAppModelOptions("codex", settings.customCodexModels),
125 claudeCode: getAppModelOptions("claudeCode", settings.customClaudeModels),
126 };
128​
129export function getCustomModelSlugsForProvider(
130 settings: {
131 customCodexModels: readonly string[];
132 customClaudeModels: readonly string[];
133 },
134 provider: ProviderKind,
135): readonly string[] {
136 switch (provider) {
137 case "claudeCode":
138 return settings.customClaudeModels;
139 case "codex":
140 default:
141 return settings.customCodexModels;
142 }