The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { AuthOrchestrationOperateScope, type EnvironmentId } from "@t3tools/contracts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import { |
| 6 | attachmentUploadError, |
| 7 | createAttachmentUploadRegistry, |
| 8 | } from "@t3tools/client-runtime/state/attachment-uploads"; |
| 9 | import { verifyPersistedAttachmentUpload } from "@t3tools/client-runtime/state/attachments"; |
| 10 | import { Atom } from "effect/reactivity"; |
| 11 | |
| 12 | import { uploadComposerAttachment } from "../lib/attachmentUpload"; |
| 13 | import { retainComposerAttachmentFileForPreview } from "../lib/composerAttachmentPreviewRetention"; |
| 14 | import { |
| 15 | isFileBackedComposerAttachment, |
| 16 | type DraftComposerAttachment, |
| 17 | } from "../lib/composerImages"; |
| 18 | import { |
| 19 | composerAttachmentUploadKey, |
| 20 | composerDraftEnvironmentId, |
| 21 | canUploadComposerAttachment, |
| 22 | type ComposerAttachmentUploadState, |
| 23 | } from "../lib/composerAttachmentUploadQueue"; |
| 24 | import { appAtomRegistry } from "./atom-registry"; |
| 25 | import { assetEnvironment } from "./assets"; |
| 26 | import { environmentServerConfigsAtom } from "./server"; |
| 27 | import { |
| 28 | flattenQueuedThreadMessages, |
| 29 | threadOutboxManager, |
| 30 | modifyThreadOutboxMessage, |
| 31 | type QueuedThreadMessage, |
| 32 | } from "./thread-outbox"; |
| 33 | import { editingQueuedMessageIdsAtom } from "./use-thread-outbox"; |
| 34 | import { |
| 35 | ComposerDraftPersistenceError, |
| 36 | type ComposerDraft, |
| 37 | composerDraftsAtom, |
| 38 | composerDraftUploadSourcesAtom, |
| 39 | ensureComposerDraftsLoaded, |
| 40 | flushComposerDrafts, |
| 41 | setComposerDraftAttachmentUpload, |
| 42 | } from "./use-composer-drafts"; |
| 43 | import { environmentSession, readEnvironmentScope, sessionHasScope } from "./session"; |
| 44 | import { workspaceConnections } from "./workspace"; |
| 45 | |
| 46 | const isComposerDraftPersistenceError = Schema.is(ComposerDraftPersistenceError); |
| 47 | |
| 48 | export { |
| 49 | composerAttachmentUploadBlockReason, |
| 50 | composerAttachmentsStillUploading, |
| 51 | } from "../lib/composerAttachmentUploadQueue"; |
| 52 | |
| 53 | export const composerAttachmentUploadsAtom = Atom.make< |
| 54 | Readonly<Record<string, ComposerAttachmentUploadState>> |
| 55 | >({}).pipe(Atom.keepAlive); |
| 56 | const uploadStateAtom = Atom.family((key: string) => |
| 57 | Atom.map(composerAttachmentUploadsAtom, (states) => states[key]), |
| 58 | ); |
| 59 | let uploadQueue: ReturnType<typeof createComposerUploadRegistry> | null = null; |
| 60 | |
| 61 | export function useComposerAttachmentUploadState( |
| 62 | environmentId: EnvironmentId | undefined, |
| 63 | attachmentId: string, |
| 64 | ) { |
| 65 | return useAtomValue( |
| 66 | uploadStateAtom(environmentId ? composerAttachmentUploadKey(environmentId, attachmentId) : ""), |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | export function retryComposerAttachmentUpload(environmentId: EnvironmentId, attachmentId: string) { |
| 71 | if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return; |
| 72 | uploadQueue?.retry(composerAttachmentUploadKey(environmentId, attachmentId)); |
| 73 | } |
| 74 | |
| 75 | type ComposerAttachmentUploadRequest = { |
| 76 | readonly environmentId: EnvironmentId; |
| 77 | readonly attachment: DraftComposerAttachment; |
| 78 | }; |
| 79 | |
| 80 | export function composerAttachmentUploadRequests(input: { |
| 81 | readonly drafts: Readonly<Record<string, Pick<ComposerDraft, "attachments" | "project">>>; |
| 82 | readonly queuedMessagesByThreadKey: Parameters<typeof flattenQueuedThreadMessages>[0]; |
| 83 | /** An edited message's attachments upload through its editor draft. */ |
| 84 | readonly editingQueuedMessageIds: Readonly<Record<string, true>>; |
| 85 | /** Every environment the client knows, so a draft key never resolves to a shorter connected prefix. */ |
| 86 | readonly knownEnvironmentIds: Iterable<EnvironmentId>; |
| 87 | readonly connectedEnvironmentIds: ReadonlySet<EnvironmentId>; |
| 88 | readonly operableEnvironmentIds: ReadonlySet<EnvironmentId>; |
| 89 | readonly serverConfigs: ReadonlyMap< |
| 90 | EnvironmentId, |
| 91 | Parameters<typeof canUploadComposerAttachment>[1] |
| 92 | >; |
| 93 | }): ReadonlyArray<ComposerAttachmentUploadRequest> { |
| 94 | const queued = flattenQueuedThreadMessages(input.queuedMessagesByThreadKey); |
| 95 | const uploadable = ( |
| 96 | environmentId: EnvironmentId | null, |
| 97 | attachments: ReadonlyArray<DraftComposerAttachment>, |
| 98 | ): ReadonlyArray<ComposerAttachmentUploadRequest> => |
| 99 | environmentId === null || |
| 100 | !input.connectedEnvironmentIds.has(environmentId) || |
| 101 | !input.operableEnvironmentIds.has(environmentId) |
| 102 | ? [] |
| 103 | : attachments |
| 104 | .filter((attachment) => |
| 105 | canUploadComposerAttachment(attachment, input.serverConfigs.get(environmentId)), |
| 106 | ) |
| 107 | .map((attachment) => ({ environmentId, attachment })); |
| 108 | return [ |
| 109 | ...Object.entries(input.drafts).flatMap(([key, draft]) => |
| 110 | uploadable( |
| 111 | composerDraftEnvironmentId(key, input.knownEnvironmentIds, queued, draft), |
| 112 | draft.attachments, |
| 113 | ), |
| 114 | ), |
| 115 | // Sending mid-upload hands the attachment to the outbox; requesting it here keeps that |
| 116 | // upload running instead of aborting it for the outbox to start over. |
| 117 | ...queued.flatMap((message) => |
| 118 | input.editingQueuedMessageIds[message.messageId] |
| 119 | ? [] |
| 120 | : uploadable( |
| 121 | message.environmentId, |
| 122 | message.attachments.filter( |
| 123 | (attachment) => attachment.uploadedAttachmentId === undefined, |
| 124 | ), |
| 125 | ), |
| 126 | ), |
| 127 | ]; |
| 128 | } |
| 129 | |
| 130 | function withUploadedAttachment( |
| 131 | message: QueuedThreadMessage, |
| 132 | uploaded: DraftComposerAttachment, |
| 133 | ): QueuedThreadMessage | null { |
| 134 | const awaiting = (attachment: DraftComposerAttachment) => |
| 135 | attachment.id === uploaded.id && attachment.uploadedAttachmentId === undefined; |
| 136 | if (!message.attachments.some(awaiting)) return null; |
| 137 | return { |
| 138 | ...message, |
| 139 | attachments: message.attachments.map((attachment) => |
| 140 | awaiting(attachment) ? uploaded : attachment, |
| 141 | ), |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | const recordQueuedAttachmentUpload = Effect.fn("ComposerAttachments.recordQueuedUpload")(function* ( |
| 146 | environmentId: EnvironmentId, |
| 147 | uploaded: DraftComposerAttachment, |
| 148 | ) { |
| 149 | const editing = appAtomRegistry.get(editingQueuedMessageIdsAtom); |
| 150 | const holders = flattenQueuedThreadMessages( |
| 151 | appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), |
| 152 | ).filter( |
| 153 | (message) => |
| 154 | message.environmentId === environmentId && |
| 155 | !editing[message.messageId] && |
| 156 | withUploadedAttachment(message, uploaded) !== null, |
| 157 | ); |
| 158 | yield* Effect.forEach( |
| 159 | holders, |
| 160 | (message) => |
| 161 | Effect.tryPromise({ |
| 162 | // An editor that opened meanwhile owns the message; its draft records the upload. |
| 163 | try: () => |
| 164 | modifyThreadOutboxMessage(message.messageId, (current) => |
| 165 | appAtomRegistry.get(editingQueuedMessageIdsAtom)[current.messageId] |
| 166 | ? null |
| 167 | : withUploadedAttachment(current, uploaded), |
| 168 | ), |
| 169 | catch: attachmentUploadError, |
| 170 | }), |
| 171 | { concurrency: "unbounded", discard: true }, |
| 172 | ); |
| 173 | }); |
| 174 | |
| 175 | // Each connected environment's session is read here so a granted or revoked |
| 176 | // operate scope re-syncs the queue. |
| 177 | const composerAttachmentUploadRequestsAtom = Atom.make((get) => { |
| 178 | const environments = get(workspaceConnections.environmentsAtom); |
| 179 | const connected = environments.filter( |
| 180 | (environment) => environment.connectionState === "connected", |
| 181 | ); |
| 182 | const operableEnvironmentIds = new Set<EnvironmentId>(); |
| 183 | for (const { environmentId } of connected) { |
| 184 | if ( |
| 185 | sessionHasScope( |
| 186 | get(environmentSession.sessionStateAtom(environmentId)), |
| 187 | AuthOrchestrationOperateScope, |
| 188 | ) |
| 189 | ) { |
| 190 | operableEnvironmentIds.add(environmentId); |
| 191 | } |
| 192 | } |
| 193 | return composerAttachmentUploadRequests({ |
| 194 | drafts: get(composerDraftUploadSourcesAtom), |
| 195 | queuedMessagesByThreadKey: get(threadOutboxManager.queuedMessagesByThreadKeyAtom), |
| 196 | editingQueuedMessageIds: get(editingQueuedMessageIdsAtom), |
| 197 | knownEnvironmentIds: environments.map((environment) => environment.environmentId), |
| 198 | connectedEnvironmentIds: new Set(connected.map((environment) => environment.environmentId)), |
| 199 | operableEnvironmentIds, |
| 200 | serverConfigs: get(environmentServerConfigsAtom), |
| 201 | }); |
| 202 | }).pipe(Atom.withLabel("mobile:composer-attachment-upload-requests")); |
| 203 | |
| 204 | /** Saves a finished upload's id on every draft and queued message holding the attachment. */ |
| 205 | const recordComposerAttachmentUpload = Effect.fn("ComposerAttachments.recordUpload")(function* ( |
| 206 | request: ComposerAttachmentUploadRequest, |
| 207 | uploadedId: string, |
| 208 | ) { |
| 209 | const { environmentId, attachment } = request; |
| 210 | const uploaded: DraftComposerAttachment = { |
| 211 | ...attachment, |
| 212 | uploadedAttachmentId: uploadedId, |
| 213 | uploadEnvironmentId: environmentId, |
| 214 | }; |
| 215 | const queued = flattenQueuedThreadMessages( |
| 216 | appAtomRegistry.get(threadOutboxManager.queuedMessagesByThreadKeyAtom), |
| 217 | ); |
| 218 | const knownEnvironmentIds = appAtomRegistry |
| 219 | .get(workspaceConnections.environmentsAtom) |
| 220 | .map((environment) => environment.environmentId); |
| 221 | let draftsChanged = false; |
| 222 | for (const [key, draft] of Object.entries(appAtomRegistry.get(composerDraftsAtom))) { |
| 223 | if ( |
| 224 | composerDraftEnvironmentId(key, knownEnvironmentIds, queued, draft) === environmentId && |
| 225 | draft.attachments.some((candidate) => candidate.id === attachment.id) |
| 226 | ) { |
| 227 | draftsChanged = setComposerDraftAttachmentUpload(key, uploaded) || draftsChanged; |
| 228 | } |
| 229 | } |
| 230 | yield* recordQueuedAttachmentUpload(environmentId, uploaded); |
| 231 | if (!draftsChanged) return; |
| 232 | // A failed draft save only costs a re-upload after restart; the outbox saves its own references. |
| 233 | yield* Effect.tryPromise({ try: () => flushComposerDrafts(), catch: attachmentUploadError }).pipe( |
| 234 | Effect.catchIf( |
| 235 | (error) => isComposerDraftPersistenceError(error.cause), |
| 236 | (error) => |
| 237 | Effect.logWarning( |
| 238 | "[composer-attachments] could not save the uploaded reference", |
| 239 | error.cause, |
| 240 | ), |
| 241 | ), |
| 242 | ); |
| 243 | }); |
| 244 | |
| 245 | const createComposerUploadRegistry = () => |
| 246 | createAttachmentUploadRegistry<ComposerAttachmentUploadRequest>({ |
| 247 | key: (request) => composerAttachmentUploadKey(request.environmentId, request.attachment.id), |
| 248 | environmentId: (request) => request.environmentId, |
| 249 | concurrencyPerEnvironment: 3, |
| 250 | onChange: (states) => appAtomRegistry.set(composerAttachmentUploadsAtom, states), |
| 251 | // The file stays on disk while its upload reads it, even if its draft lets go meanwhile. |
| 252 | upload: ({ environmentId, attachment }, reportProgress) => |
| 253 | Effect.acquireUseRelease( |
| 254 | Effect.sync(() => |
| 255 | isFileBackedComposerAttachment(attachment) |
| 256 | ? retainComposerAttachmentFileForPreview(attachment) |
| 257 | : undefined, |
| 258 | ), |
| 259 | () => uploadComposerAttachment(environmentId, attachment, reportProgress), |
| 260 | (release) => Effect.sync(() => release?.()), |
| 261 | ), |
| 262 | onUploaded: recordComposerAttachmentUpload, |
| 263 | }); |
| 264 | |
| 265 | function composerAttachmentUploads() { |
| 266 | uploadQueue ??= createComposerUploadRegistry(); |
| 267 | return uploadQueue; |
| 268 | } |
| 269 | |
| 270 | /** Every send uploads through here, so it joins the composer's upload of the same attachment. */ |
| 271 | export const uploadComposerAttachmentShared = Effect.fn("ComposerAttachments.uploadForSend")( |
| 272 | function* (environmentId: EnvironmentId, attachment: DraftComposerAttachment) { |
| 273 | const uploads = composerAttachmentUploads(); |
| 274 | const request = { environmentId, attachment }; |
| 275 | const key = composerAttachmentUploadKey(environmentId, attachment.id); |
| 276 | // An upload that finished earlier can outlive the server's pending-upload sweep. |
| 277 | const finishedEarlier = uploads.status(key)?.status === "ready"; |
| 278 | const uploadedId = yield* uploads.upload(request); |
| 279 | if (!finishedEarlier) return uploadedId; |
| 280 | const verification = yield* verifyPersistedAttachmentUpload({ |
| 281 | registry: appAtomRegistry, |
| 282 | createAssetUrl: assetEnvironment.createUrl, |
| 283 | environmentId, |
| 284 | attachmentId: uploadedId, |
| 285 | }); |
| 286 | if (verification.status === "verified") return uploadedId; |
| 287 | if (verification.status === "failed") return yield* attachmentUploadError(verification.error); |
| 288 | // Starting over verifies the saved id again and uploads the bytes when it is gone. |
| 289 | uploads.restart(key); |
| 290 | return yield* uploads.upload(request); |
| 291 | }, |
| 292 | ); |
| 293 | |
| 294 | /** |
| 295 | * Uploads draft and queued attachments to their environment as soon as it is connected and |
| 296 | * operable. The queue follows the requests atom for its whole lifetime. |
| 297 | */ |
| 298 | export function createComposerAttachmentUploadWorker( |
| 299 | requestsAtom: Atom.Atom< |
| 300 | ReadonlyArray<ComposerAttachmentUploadRequest> |
| 301 | > = composerAttachmentUploadRequestsAtom, |
| 302 | ) { |
| 303 | ensureComposerDraftsLoaded(); |
| 304 | const queue = composerAttachmentUploads(); |
| 305 | const unsubscribe = appAtomRegistry.subscribe(requestsAtom, (requests) => queue.sync(requests), { |
| 306 | immediate: true, |
| 307 | }); |
| 308 | return { |
| 309 | queue, |
| 310 | dispose: () => { |
| 311 | unsubscribe(); |
| 312 | queue.dispose(); |
| 313 | if (uploadQueue === queue) uploadQueue = null; |
| 314 | }, |
| 315 | }; |
| 316 | } |
| 317 | |
| 318 | export function installComposerAttachmentUploads(): () => void { |
| 319 | return createComposerAttachmentUploadWorker().dispose; |
| 320 | } |