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
| 1 | import { msg } from "gt-react"; |
| 2 | import { m } from "~/i18n/messages"; |
| 3 | import { |
| 4 | ConnectionBlockedError, |
| 5 | ConnectionTransientError, |
| 6 | } from "@t3tools/client-runtime/connection"; |
| 7 | import { EnvironmentRpcUnavailableError } from "@t3tools/client-runtime/rpc"; |
| 8 | import * as Cause from "effect/Cause"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import type { RefObject } from "react"; |
| 11 | import type { ChatComposerProps } from "./ChatComposer"; |
| 12 | import { |
| 13 | type ComposerContextClipboardFragment, |
| 14 | type ComposerContextRecord, |
| 15 | type EnvironmentId, |
| 16 | PROVIDER_SEND_TURN_MAX_ATTACHMENTS, |
| 17 | } from "@t3tools/contracts"; |
| 18 | import { |
| 19 | type ComposerFileAttachment, |
| 20 | type ComposerImageAttachment, |
| 21 | type DraftId, |
| 22 | composerFileMatchesReattachMarker, |
| 23 | composerFileNeedsReattach, |
| 24 | useComposerDraftStore, |
| 25 | } from "../../composerDraftStore"; |
| 26 | import { type PendingUserInput } from "../../session-logic"; |
| 27 | import { |
| 28 | asKnownContextRecord, |
| 29 | composerContextImportLookupIds, |
| 30 | isSameComposerContextPayload, |
| 31 | previewAnnotationContextId, |
| 32 | previewAnnotationContextRecord, |
| 33 | previewAnnotationFromRecord, |
| 34 | reviewCommentContextId, |
| 35 | reviewCommentContextRecord, |
| 36 | reviewCommentFromRecord, |
| 37 | terminalContextDraftFromRecord, |
| 38 | terminalContextRecord, |
| 39 | terminalContextReference, |
| 40 | } from "~/lib/composerContextRecords"; |
| 41 | import { assetEnvironment } from "~/state/assets"; |
| 42 | import { copiedComposerAttachmentFile } from "~/lib/copiedComposerAttachments"; |
| 43 | import { countQuestionAttachments, questionAttachmentDraftId } from "../../questionAttachments"; |
| 44 | import { collectInlineContextIds } from "../../lib/composerContextReferences"; |
| 45 | import { elementContextToPreviewAnnotation } from "../../lib/elementContext"; |
| 46 | import { orchestrationEnvironment } from "~/state/orchestration"; |
| 47 | import { pendingDraftWork } from "./pendingDraftWork"; |
| 48 | import { readComposerImageBytes } from "./composerAttachmentFiles"; |
| 49 | import { randomUUID } from "~/lib/utils"; |
| 50 | import { readPreparedConnection } from "~/state/session"; |
| 51 | import { resolveAssetUrl } from "~/assets/assetUrls"; |
| 52 | import { toKindScopedComposerContextId } from "@t3tools/shared/composerContextReferences"; |
| 53 | import { toastManager } from "../ui/toast"; |
| 54 | import { |
| 55 | type ComposerDraftContextRecords, |
| 56 | uploadedContextRecordFromDraft, |
| 57 | } from "../composerContextPresentation"; |
| 58 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 59 | import { useAtomQueryRunner } from "~/state/use-atom-query-runner"; |
| 60 | import { useStableCallback } from "../../hooks/useStableCallback"; |
| 61 | import { countPreparingComposerImages } from "./preparingComposerImages"; |
| 62 | |
| 63 | const isUnreachable = Schema.is( |
| 64 | Schema.Union([ConnectionTransientError, ConnectionBlockedError, EnvironmentRpcUnavailableError]), |
| 65 | ); |
| 66 | |
| 67 | async function downloadAttachmentBlob(url: string): Promise<Blob | null> { |
| 68 | try { |
| 69 | const response = await fetch(url, { signal: AbortSignal.timeout(60_000) }); |
| 70 | return response.ok ? await response.blob() : null; |
| 71 | } catch { |
| 72 | return null; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Imports copied composer context (terminal output, review comments, annotations, threads, |
| 78 | * images and files) into the open draft, and counts the attachment slots already spoken for. |
| 79 | */ |
| 80 | export function useComposerContextImport(input: { |
| 81 | readonly environmentId: EnvironmentId; |
| 82 | readonly linkedEnvironments: ReadonlyMap<EnvironmentId, string>; |
| 83 | readonly activeThread: ChatComposerProps["activeThread"]; |
| 84 | readonly activeThreadId: ChatComposerProps["activeThreadId"]; |
| 85 | readonly composerDraftTarget: ChatComposerProps["composerDraftTarget"]; |
| 86 | readonly attachmentDraftTarget: ChatComposerProps["composerDraftTarget"]; |
| 87 | readonly attachmentTargetKey: string; |
| 88 | readonly attachmentTargetKeyRef: RefObject<string>; |
| 89 | readonly questionAttachmentTarget: DraftId | null; |
| 90 | readonly pendingUserInputs: ReadonlyArray<PendingUserInput>; |
| 91 | readonly composerContextRecords: ComposerDraftContextRecords; |
| 92 | readonly composerImages: ReadonlyArray<ComposerImageAttachment>; |
| 93 | readonly composerFilesRef: ChatComposerProps["composerFilesRef"]; |
| 94 | readonly promptRef: RefObject<string>; |
| 95 | readonly pendingThreadImportsRef: RefObject<Map<string, number>>; |
| 96 | readonly addComposerFilesToDraft: (files: ComposerFileAttachment[]) => string[]; |
| 97 | }) { |
| 98 | const { |
| 99 | environmentId, |
| 100 | linkedEnvironments, |
| 101 | activeThread, |
| 102 | activeThreadId, |
| 103 | composerDraftTarget, |
| 104 | attachmentDraftTarget, |
| 105 | attachmentTargetKey, |
| 106 | attachmentTargetKeyRef, |
| 107 | questionAttachmentTarget, |
| 108 | pendingUserInputs, |
| 109 | composerContextRecords, |
| 110 | composerImages, |
| 111 | composerFilesRef, |
| 112 | promptRef, |
| 113 | pendingThreadImportsRef, |
| 114 | addComposerFilesToDraft, |
| 115 | } = input; |
| 116 | const getComposerDraft = useComposerDraftStore((store) => store.getComposerDraft); |
| 117 | const addComposerDraftImages = useComposerDraftStore((store) => store.addImages); |
| 118 | const addComposerDraftTerminalContexts = useComposerDraftStore( |
| 119 | (store) => store.addTerminalContexts, |
| 120 | ); |
| 121 | const addComposerDraftReviewComment = useComposerDraftStore((store) => store.addReviewComment); |
| 122 | const addComposerDraftPreviewAnnotation = useComposerDraftStore( |
| 123 | (store) => store.addPreviewAnnotation, |
| 124 | ); |
| 125 | const addComposerDraftThreadContexts = useComposerDraftStore((store) => store.addThreadContexts); |
| 126 | const addComposerDraftEnvironmentContexts = useComposerDraftStore( |
| 127 | (store) => store.addEnvironmentContexts, |
| 128 | ); |
| 129 | const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, { reportFailure: false }); |
| 130 | const loadThreadTranscript = useAtomCommand(orchestrationEnvironment.threadTranscript, { |
| 131 | reportFailure: false, |
| 132 | }); |
| 133 | const countReservedAttachments = () => { |
| 134 | const questionRequest = pendingUserInputs[0]; |
| 135 | const otherQuestionKeys = |
| 136 | questionAttachmentTarget && questionRequest && activeThreadId |
| 137 | ? questionRequest.questions |
| 138 | .map((question) => |
| 139 | questionAttachmentDraftId( |
| 140 | environmentId, |
| 141 | activeThreadId, |
| 142 | questionRequest.requestId, |
| 143 | question.id, |
| 144 | ), |
| 145 | ) |
| 146 | .filter((key) => key !== questionAttachmentTarget) |
| 147 | : []; |
| 148 | const draft = getComposerDraft(attachmentDraftTarget); |
| 149 | return ( |
| 150 | (draft?.images.length ?? 0) + |
| 151 | (draft?.files.length ?? 0) + |
| 152 | countPreparingComposerImages(attachmentTargetKey) + |
| 153 | (pendingThreadImportsRef.current.get(attachmentTargetKey) ?? 0) + |
| 154 | countQuestionAttachments(otherQuestionKeys) |
| 155 | ); |
| 156 | }; |
| 157 | const countReservedAttachmentsStable = useStableCallback(countReservedAttachments); |
| 158 | const runAttachmentImport = async ( |
| 159 | record: Extract<ComposerContextRecord, { kind: "image" | "file" }>, |
| 160 | localId: string, |
| 161 | sourceEnvironmentId: EnvironmentId, |
| 162 | importTargetKey: string, |
| 163 | ) => { |
| 164 | const fail = (reason: string) => { |
| 165 | toastManager.add({ |
| 166 | type: "error", |
| 167 | title: m(msg("Couldn't bring {name} into this message", { name: record.name })), |
| 168 | description: m(msg("{reason} Remove the chip or attach the file again.", { reason })), |
| 169 | }); |
| 170 | }; |
| 171 | let file = copiedComposerAttachmentFile(sourceEnvironmentId, record.attachmentId); |
| 172 | if (!file) { |
| 173 | const sourceConnection = readPreparedConnection(sourceEnvironmentId); |
| 174 | if (!sourceConnection) { |
| 175 | fail(m(msg("The environment it came from is not connected."))); |
| 176 | return; |
| 177 | } |
| 178 | const result = await createAssetUrl({ |
| 179 | environmentId: sourceEnvironmentId, |
| 180 | input: { resource: { _tag: "attachment", attachmentId: record.attachmentId } }, |
| 181 | }); |
| 182 | const url = |
| 183 | result._tag === "Success" |
| 184 | ? resolveAssetUrl(sourceConnection.httpBaseUrl, result.value.relativeUrl) |
| 185 | : null; |
| 186 | if (!url) { |
| 187 | const cause = result._tag === "Failure" ? Cause.squash(result.cause) : null; |
| 188 | fail( |
| 189 | isUnreachable(cause) |
| 190 | ? m(msg("The environment it came from is not reachable right now.")) |
| 191 | : m(msg("The original attachment is no longer available.")), |
| 192 | ); |
| 193 | return; |
| 194 | } |
| 195 | const blob = await downloadAttachmentBlob(url); |
| 196 | if (!blob) { |
| 197 | fail(m(msg("Downloading it from the source failed."))); |
| 198 | return; |
| 199 | } |
| 200 | file = new File([blob], record.name, { type: record.mimeType || blob.type }); |
| 201 | if (record.kind === "image") await readComposerImageBytes(file); |
| 202 | // Question attachments have no inline reference; annotations can own their screenshots. |
| 203 | const references = new Set(collectInlineContextIds(promptRef.current)); |
| 204 | if ( |
| 205 | !questionAttachmentTarget && |
| 206 | !references.has(toKindScopedComposerContextId(record.kind, localId)) && |
| 207 | !( |
| 208 | record.kind === "image" && |
| 209 | references.has(previewAnnotationContextId(localId)) && |
| 210 | getComposerDraft(composerDraftTarget)?.previewAnnotations.some( |
| 211 | (annotation) => annotation.id === localId, |
| 212 | ) |
| 213 | ) |
| 214 | ) |
| 215 | return; |
| 216 | } |
| 217 | // The draft these bytes belong to may have been sent or switched away from while they |
| 218 | // downloaded. Dropping them here keeps them out of whatever draft is open now. |
| 219 | if (attachmentTargetKeyRef.current !== importTargetKey) return; |
| 220 | const replacesFileMarker = |
| 221 | record.kind === "file" && |
| 222 | composerFilesRef.current.some( |
| 223 | (candidate) => |
| 224 | composerFileNeedsReattach(candidate) && |
| 225 | composerFileMatchesReattachMarker(candidate, { |
| 226 | name: record.name, |
| 227 | mimeType: file.type, |
| 228 | sizeBytes: file.size, |
| 229 | }), |
| 230 | ); |
| 231 | if ( |
| 232 | !replacesFileMarker && |
| 233 | (pendingThreadImportsRef.current.get(importTargetKey) ?? 0) > 0 && |
| 234 | countReservedAttachments() >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS |
| 235 | ) { |
| 236 | fail(m(msg("The attachment limit has been reached."))); |
| 237 | return; |
| 238 | } |
| 239 | if (record.kind === "image") { |
| 240 | const accepted = addComposerDraftImages( |
| 241 | attachmentDraftTarget, |
| 242 | [ |
| 243 | { |
| 244 | type: "image", |
| 245 | id: localId, |
| 246 | name: record.name, |
| 247 | mimeType: file.type, |
| 248 | sizeBytes: file.size, |
| 249 | previewUrl: URL.createObjectURL(file), |
| 250 | file, |
| 251 | ...(record.source ? { source: record.source } : {}), |
| 252 | }, |
| 253 | ], |
| 254 | { |
| 255 | allowDuplicates: |
| 256 | getComposerDraft(composerDraftTarget)?.previewAnnotations.some( |
| 257 | (annotation) => annotation.id === localId, |
| 258 | ) ?? false, |
| 259 | }, |
| 260 | ); |
| 261 | if (!accepted.includes(localId)) |
| 262 | fail(m(msg("The draft rejected this attachment (duplicate or attachment limit reached)."))); |
| 263 | } else { |
| 264 | const accepted = addComposerFilesToDraft([ |
| 265 | { |
| 266 | type: "file", |
| 267 | id: localId, |
| 268 | name: record.name, |
| 269 | mimeType: file.type, |
| 270 | sizeBytes: file.size, |
| 271 | file, |
| 272 | }, |
| 273 | ]); |
| 274 | if (!accepted.includes(localId)) |
| 275 | fail(m(msg("The draft rejected this attachment (duplicate or attachment limit reached)."))); |
| 276 | } |
| 277 | }; |
| 278 | const importAttachmentRecord = async ( |
| 279 | record: Extract<ComposerContextRecord, { kind: "image" | "file" }>, |
| 280 | localId: string, |
| 281 | sourceEnvironmentId: EnvironmentId, |
| 282 | ) => { |
| 283 | // The chip lands in the draft immediately while these bytes are still downloading. Count |
| 284 | // the transfer against its own draft so a send cannot snapshot a message whose chip has no |
| 285 | // attachment behind it, and so bytes for an abandoned draft never enter the next one. |
| 286 | const importTargetKey = attachmentTargetKey; |
| 287 | pendingDraftWork.begin(importTargetKey); |
| 288 | await runAttachmentImport(record, localId, sourceEnvironmentId, importTargetKey).finally(() => |
| 289 | pendingDraftWork.end(importTargetKey), |
| 290 | ); |
| 291 | }; |
| 292 | const importContextRecords = ( |
| 293 | records: ReadonlyArray<ComposerContextRecord>, |
| 294 | sourceEnvironmentId: EnvironmentId | null, |
| 295 | threadDraftTarget = attachmentDraftTarget, |
| 296 | ): ReadonlyMap<string, string> => { |
| 297 | const rewritten = new Map<string, string>(); |
| 298 | const dependentAttachmentLocalIds = new Map<string, string>(); |
| 299 | const skippedDependentAttachmentIds = new Set<string>(); |
| 300 | // Resolve annotations before their dependent screenshot records even if a foreign |
| 301 | // clipboard producer emitted the records in a different order. |
| 302 | const orderedRecords = records.toSorted((left, right) => |
| 303 | left.kind === "preview-annotation" && right.kind !== "preview-annotation" |
| 304 | ? -1 |
| 305 | : right.kind === "preview-annotation" && left.kind !== "preview-annotation" |
| 306 | ? 1 |
| 307 | : 0, |
| 308 | ); |
| 309 | for (const candidate of orderedRecords) { |
| 310 | // Producer ids fold into context ids, so two different excerpts can collide. Only skip |
| 311 | // when the draft already holds the same payload; a colliding but different record is |
| 312 | // re-minted under a fresh id so both survive the paste. Threads resolve below. |
| 313 | const record = asKnownContextRecord(candidate); |
| 314 | if (!record) continue; |
| 315 | // A reminted annotation needs its screenshot under the new annotation id. |
| 316 | const existing = dependentAttachmentLocalIds.has(record.contextId) |
| 317 | ? undefined |
| 318 | : composerContextImportLookupIds(record).flatMap((contextId) => { |
| 319 | const found = composerContextRecords.get(contextId); |
| 320 | return found ? [found] : []; |
| 321 | })[0]; |
| 322 | // Upload completion can change the server id since copy; the local bytes still match. |
| 323 | if ( |
| 324 | sourceEnvironmentId !== null && |
| 325 | (record.kind === "image" || record.kind === "file") && |
| 326 | (existing?.kind === "image" || existing?.kind === "file") && |
| 327 | existing.record.file !== null && |
| 328 | existing.record.file === |
| 329 | copiedComposerAttachmentFile(sourceEnvironmentId, record.attachmentId) |
| 330 | ) |
| 331 | continue; |
| 332 | const existingRecord = |
| 333 | existing?.kind === "terminal" |
| 334 | ? terminalContextRecord(existing.record) |
| 335 | : existing?.kind === "review-comment" |
| 336 | ? reviewCommentContextRecord(existing.record) |
| 337 | : existing?.kind === "preview-annotation" |
| 338 | ? previewAnnotationContextRecord(existing.record, { |
| 339 | screenshotContextId: composerImages.some( |
| 340 | (image) => image.id === existing.record.id, |
| 341 | ) |
| 342 | ? existing.record.id |
| 343 | : undefined, |
| 344 | }) |
| 345 | : existing?.kind === "thread" || existing?.kind === "environment" |
| 346 | ? existing.record |
| 347 | : existing |
| 348 | ? (uploadedContextRecordFromDraft(existing) ?? undefined) |
| 349 | : undefined; |
| 350 | if ( |
| 351 | existingRecord && |
| 352 | isSameComposerContextPayload(existingRecord, record) && |
| 353 | record.kind !== "thread" |
| 354 | ) { |
| 355 | if (record.kind === "preview-annotation" && record.screenshotContextId) { |
| 356 | skippedDependentAttachmentIds.add(record.screenshotContextId); |
| 357 | } |
| 358 | continue; |
| 359 | } |
| 360 | const conflicts = existing !== undefined; |
| 361 | switch (record.kind) { |
| 362 | case "terminal": { |
| 363 | const threadId = activeThread?.id ?? activeThreadId; |
| 364 | if (!threadId) break; |
| 365 | const imported = terminalContextDraftFromRecord(record, threadId); |
| 366 | const draft = conflicts ? { ...imported, id: randomUUID() } : imported; |
| 367 | addComposerDraftTerminalContexts(composerDraftTarget, [draft], { |
| 368 | appendReference: false, |
| 369 | }); |
| 370 | rewritten.set(record.contextId, terminalContextReference(draft).contextId); |
| 371 | break; |
| 372 | } |
| 373 | case "review-comment": { |
| 374 | const imported = reviewCommentFromRecord(record); |
| 375 | const comment = conflicts ? { ...imported, id: randomUUID() } : imported; |
| 376 | addComposerDraftReviewComment(composerDraftTarget, comment, { |
| 377 | appendReference: false, |
| 378 | }); |
| 379 | rewritten.set(record.contextId, reviewCommentContextId(comment.id)); |
| 380 | break; |
| 381 | } |
| 382 | case "element": |
| 383 | case "preview-annotation": { |
| 384 | const imported = |
| 385 | record.kind === "element" |
| 386 | ? elementContextToPreviewAnnotation(record, randomUUID(), new Date().toISOString()) |
| 387 | : previewAnnotationFromRecord(record); |
| 388 | const annotation = conflicts ? { ...imported, id: randomUUID() } : imported; |
| 389 | if (record.kind === "preview-annotation" && record.screenshotContextId) { |
| 390 | dependentAttachmentLocalIds.set(record.screenshotContextId, annotation.id); |
| 391 | } |
| 392 | addComposerDraftPreviewAnnotation(composerDraftTarget, annotation, { |
| 393 | appendReference: false, |
| 394 | }); |
| 395 | rewritten.set(record.contextId, previewAnnotationContextId(annotation.id)); |
| 396 | break; |
| 397 | } |
| 398 | case "thread": { |
| 399 | // The agent reads threads on its own server and the machines it links to; a pasted |
| 400 | // thread from any other machine is dropped. |
| 401 | if ( |
| 402 | record.environmentId !== environmentId && |
| 403 | !linkedEnvironments.has(record.environmentId) |
| 404 | ) |
| 405 | break; |
| 406 | // Threads reuse their destination record by identity, reminting only on an ID collision. |
| 407 | const threads = getComposerDraft(threadDraftTarget)?.threadContexts ?? []; |
| 408 | const attached = threads.find( |
| 409 | (thread) => |
| 410 | thread.environmentId === record.environmentId && thread.threadId === record.threadId, |
| 411 | ); |
| 412 | const imported = |
| 413 | attached ?? |
| 414 | (threads.some((thread) => thread.contextId === record.contextId) |
| 415 | ? { ...record, contextId: toKindScopedComposerContextId("thread", randomUUID()) } |
| 416 | : record); |
| 417 | addComposerDraftThreadContexts(threadDraftTarget, [imported], { |
| 418 | appendReference: false, |
| 419 | }); |
| 420 | rewritten.set(record.contextId, imported.contextId); |
| 421 | break; |
| 422 | } |
| 423 | case "environment": { |
| 424 | // A machine means the same machine from any thread, so a pasted one is kept, |
| 425 | // but not in an answer to an agent's question, which carries no machines. |
| 426 | if (questionAttachmentTarget !== null) break; |
| 427 | addComposerDraftEnvironmentContexts(composerDraftTarget, [record], { |
| 428 | appendReference: false, |
| 429 | }); |
| 430 | rewritten.set(record.contextId, record.contextId); |
| 431 | break; |
| 432 | } |
| 433 | case "image": |
| 434 | case "file": { |
| 435 | if (skippedDependentAttachmentIds.has(record.contextId)) break; |
| 436 | if (sourceEnvironmentId === null && !conflicts) { |
| 437 | rewritten.set(record.contextId, record.contextId); |
| 438 | break; |
| 439 | } |
| 440 | if (sourceEnvironmentId === null) break; |
| 441 | const localId = dependentAttachmentLocalIds.get(record.contextId) ?? randomUUID(); |
| 442 | rewritten.set(record.contextId, toKindScopedComposerContextId(record.kind, localId)); |
| 443 | void importAttachmentRecord(record, localId, sourceEnvironmentId); |
| 444 | break; |
| 445 | } |
| 446 | default: |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | return rewritten; |
| 451 | }; |
| 452 | const importContextFragment = ( |
| 453 | fragment: ComposerContextClipboardFragment, |
| 454 | ): ReadonlyMap<string, string> => |
| 455 | importContextRecords(fragment.records, fragment.source.environmentId); |
| 456 | const importContextFragmentStable = useStableCallback(importContextFragment); |
| 457 | |
| 458 | return { |
| 459 | loadThreadTranscript, |
| 460 | countReservedAttachments, |
| 461 | countReservedAttachmentsStable, |
| 462 | importContextRecords, |
| 463 | importContextFragment, |
| 464 | importContextFragmentStable, |
| 465 | }; |
| 466 | } |