@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 110 lines 4.06 KB
Raw
1 import { EnvironmentId, type ServerConfig } from "@t3tools/contracts";
2 import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments";
3 import type { AttachmentUploadStatus } from "@t3tools/client-runtime/state/attachment-uploads";
4 import { msg } from "gt-react-native";
5 import { m, type MessageResolver } from "../i18n/messages";
6
7 import { parseLegacyNewTaskDraftKey } from "../state/new-task-draft-key";
8 import type { DraftComposerAttachment } from "./composerImages";
9
10 export interface ComposerAttachmentUploadRequest {
11 readonly environmentId: EnvironmentId;
12 readonly attachment: DraftComposerAttachment;
13 }
14
15 export type ComposerAttachmentUploadState = AttachmentUploadStatus;
16
17 export function composerAttachmentUploadKey(
18 environmentId: EnvironmentId,
19 attachmentId: string,
20 ): string {
21 return `${environmentId}:${attachmentId}`;
22 }
23
24 /** Environment and thread ids may both contain colons, so a thread draft key resolves to the longest known environment it starts with. */
25 export function composerDraftEnvironmentId(
26 draftKey: string,
27 environmentIds: Iterable<EnvironmentId>,
28 queuedMessages: ReadonlyArray<{
29 readonly messageId: string;
30 readonly environmentId: EnvironmentId;
31 }>,
32 draft?: { readonly project?: { readonly environmentId: EnvironmentId } },
33 ): EnvironmentId | null {
34 if (draftKey.startsWith("pending-task:")) {
35 return (
36 queuedMessages.find((message) => `pending-task:${message.messageId}` === draftKey)
37 ?.environmentId ?? null
38 );
39 }
40 if (draftKey.startsWith("new-task:")) {
41 if (draft?.project) {
42 return draft.project.environmentId;
43 }
44 const legacy = parseLegacyNewTaskDraftKey(draftKey);
45 return legacy === null ? null : EnvironmentId.make(legacy.environmentId);
46 }
47 let match: EnvironmentId | null = null;
48 for (const environmentId of environmentIds) {
49 if (
50 draftKey.startsWith(`${environmentId}:`) &&
51 (match === null || environmentId.length > match.length)
52 ) {
53 match = environmentId;
54 }
55 }
56 return match;
57 }
58
59 type UploadServerConfig = {
60 readonly environment: {
61 readonly capabilities: Pick<ServerConfig["environment"]["capabilities"], "fileAttachments">;
62 };
63 };
64
65 export function canUploadComposerAttachment(
66 attachment: DraftComposerAttachment,
67 config: UploadServerConfig | null | undefined,
68 ): boolean {
69 const capabilities = config?.environment.capabilities;
70 return (
71 capabilities !== undefined &&
72 (attachment.type === "image" ||
73 (capabilities.fileAttachments !== undefined &&
74 attachment.sizeBytes <=
75 clampFileAttachmentUploadBytes(capabilities.fileAttachments.maxUploadBytes)))
76 );
77 }
78
79 /** Render callers pass `useMessages()` so the reason follows a language switch. */
80 export function composerAttachmentUploadBlockReason(
81 input: {
82 readonly environmentId: EnvironmentId;
83 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
84 readonly connected: boolean;
85 readonly serverConfig: UploadServerConfig | null;
86 readonly states: Readonly<Record<string, ComposerAttachmentUploadState>>;
87 },
88 resolve: MessageResolver = m,
89 ): string | null {
90 if (!input.connected) return null;
91 for (const attachment of input.attachments) {
92 if (!canUploadComposerAttachment(attachment, input.serverConfig)) continue;
93 const state = input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)];
94 if (state?.status === "failed") return resolve(msg("Retry or remove the failed attachment"));
95 }
96 return null;
97 }
98
99 export function composerAttachmentsStillUploading(input: {
100 readonly environmentId: EnvironmentId;
101 readonly attachments: ReadonlyArray<DraftComposerAttachment>;
102 readonly serverConfig: UploadServerConfig | null;
103 readonly states: Readonly<Record<string, ComposerAttachmentUploadState>>;
104 }): boolean {
105 return input.attachments.some((attachment) => {
106 if (!canUploadComposerAttachment(attachment, input.serverConfig)) return false;
107 const state = input.states[composerAttachmentUploadKey(input.environmentId, attachment.id)];
108 return state?.status !== "ready" && state?.status !== "failed";
109 });
110 }