GeezerVison is showing github.com — feat(web): attach svg files to messages (#21355) · ItzJsimp/leonidas-app@efea686
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/assets/AssetAccess.test.ts +38Lines changed: 38 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -979,6 +979,44 @@ describe("AssetAccess", () => { | 979 | 979 | }).pipe(Effect.provide(layerTest)), | 980 | 980 | ); | 981 | 981 |
| | 982 | + it.effect("serves document attachments inline when a viewer requests it", () => | | 983 | + Effect.gen(function* () { | | 984 | + const config = yield* ServerConfig.ServerConfig; | | 985 | + const fileSystem = yield* FileSystem.FileSystem; | | 986 | + const path = yield* Path.Path; | | 987 | + yield* fileSystem.makeDirectory(config.attachmentsDir, { recursive: true }); | | 988 | + for (const [extension, mimeType] of [ | | 989 | + ["pdf", "application/pdf"], | | 990 | + ["svg", "image/svg+xml"], | | 991 | + ] as const) { | | 992 | + const attachmentId = `thread-1-00000000-0000-4000-8000-000000000001-${extension}`; | | 993 | + const attachmentPath = path.join(config.attachmentsDir, `${attachmentId}.${extension}`); | | 994 | + yield* fileSystem.writeFile(attachmentPath, new Uint8Array([1, 2, 3])); | | 995 | + | | 996 | + const result = yield* issueAssetUrl({ | | 997 | + resource: { | | 998 | + _tag: "attachment", | | 999 | + attachmentId, | | 1000 | + fileName: `report.${extension}`, | | 1001 | + mimeType, | | 1002 | + disposition: "inline", | | 1003 | + }, | | 1004 | + }); | | 1005 | + const suffix = result.relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length); | | 1006 | + const separatorIndex = suffix.indexOf("/"); | | 1007 | + | | 1008 | + expect( | | 1009 | + yield* resolveAsset(suffix.slice(0, separatorIndex), suffix.slice(separatorIndex + 1)), | | 1010 | + ).toEqual({ | | 1011 | + kind: "file", | | 1012 | + path: attachmentPath, | | 1013 | + fileName: `report.${extension}`, | | 1014 | + mimeType, | | 1015 | + }); | | 1016 | + } | | 1017 | + }).pipe(Effect.provide(layerTest)), | | 1018 | + ); | | 1019 | + | 982 | 1020 | it.effect("keeps inline requests for other attachment types as downloads", () => | 983 | 1021 | Effect.gen(function* () { | 984 | 1022 | const config = yield* ServerConfig.ServerConfig; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/assets/AssetAccess.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,6 +82,7 @@ const PROJECT_FAVICON_ICNS_MIN_SIZE = 96; | 82 | 82 | const INLINE_VIDEO_MIME_TYPE_PATTERN = /^video\/[\w!#$&^.+-]+$/i; | 83 | 83 | // Use server-assigned attachment extensions, never client MIME types. | 84 | 84 | const INLINE_PREVIEW_MIME_TYPES: Record<string, string> = { | | 85 | + svg: "image/svg+xml", | 85 | 86 | pdf: "application/pdf", | 86 | 87 | html: "text/html", | 87 | 88 | htm: "text/html", | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/assets/AttachmentUpload.test.ts +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import * as NodePath from "node:path"; | 4 | 4 |
| 5 | 5 | import * as NodeServices from "@effect/platform-node/NodeServices"; | 6 | 6 | import { describe, expect, it } from "@effect/vitest"; | | 7 | +import { ChatAttachmentId } from "@t3tools/contracts"; | 7 | 8 | import * as Deferred from "effect/Deferred"; | 8 | 9 | import * as Effect from "effect/Effect"; | 9 | 10 | import * as Fiber from "effect/Fiber"; | @@ -14,6 +15,10 @@ import * as Stream from "effect/Stream"; | 14 | 15 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; | 15 | 16 | import { base64UrlEncode, signPayload } from "../auth/utils.ts"; | 16 | 17 | import * as ServerConfig from "../config.ts"; | | 18 | +import { | | 19 | + claimPendingAttachments, | | 20 | + releaseClaimedAttachments, | | 21 | +} from "../orchestration-v2/AttachmentClaims.ts"; | 17 | 22 | import { | 18 | 23 | ATTACHMENT_UPLOAD_ROUTE_PREFIX, | 19 | 24 | deletePendingAttachment, | @@ -139,6 +144,47 @@ describe("AttachmentUpload", () => { | 139 | 144 | }).pipe(Effect.provide(layerTest)), | 140 | 145 | ); | 141 | 146 |
| | 147 | + it.effect("stores MIME-identified SVG files with a previewable signed extension", () => | | 148 | + Effect.gen(function* () { | | 149 | + const config = yield* ServerConfig.ServerConfig; | | 150 | + const bytes = new TextEncoder().encode('<svg xmlns="http://www.w3.org/2000/svg"/>'); | | 151 | + for (const name of ["diagram", "diagram.bin"]) { | | 152 | + const issued = yield* issueAttachmentUploadUrl({ | | 153 | + type: "file", | | 154 | + name, | | 155 | + mimeType: "image/svg+xml", | | 156 | + sizeBytes: bytes.length, | | 157 | + }); | | 158 | + const token = issued.relativeUrl.slice(`${ATTACHMENT_UPLOAD_ROUTE_PREFIX}/`.length); | | 159 | + const claims = yield* validateAttachmentUploadToken(token); | | 160 | + if (!claims) throw new Error("Expected valid SVG upload claims."); | | 161 | + expect(claims.name).toBe(`${name}.svg`); | | 162 | + expect(issued.attachmentId).toMatch(/-svg$/); | | 163 | + expect(yield* storeAttachmentUpload(claims, bytes)).toEqual({ ok: true }); | | 164 | + expect( | | 165 | + NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${issued.attachmentId}.svg`)), | | 166 | + ).toEqual(Buffer.from(bytes)); | | 167 | + const claimed = yield* claimPendingAttachments({ | | 168 | + threadId: "svg-upload-thread", | | 169 | + attachments: [ | | 170 | + { | | 171 | + type: "file", | | 172 | + id: ChatAttachmentId.make(issued.attachmentId), | | 173 | + name, | | 174 | + mimeType: "image/svg+xml", | | 175 | + sizeBytes: bytes.length, | | 176 | + }, | | 177 | + ], | | 178 | + }); | | 179 | + expect(claimed.attachments[0]?.name).toBe(`${name}.svg`); | | 180 | + expect(NodeFS.readFileSync(claimed.claimedPaths[0]!)).toEqual(Buffer.from(bytes)); | | 181 | + yield* releaseClaimedAttachments(claimed.claimedPaths); | | 182 | + yield* deletePendingAttachment(issued.attachmentId); | | 183 | + } | | 184 | + expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([]); | | 185 | + }).pipe(Effect.provide(layerTest)), | | 186 | + ); | | 187 | + | 142 | 188 | it.effect("removes partial streamed uploads that exceed their signed size", () => | 143 | 189 | Effect.gen(function* () { | 144 | 190 | const config = yield* ServerConfig.ServerConfig; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/assets/AttachmentUpload.ts +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | type AttachmentCreateUploadUrlInput, | 4 | 4 | AttachmentUploadSigningKeyError, | 5 | 5 | } from "@t3tools/contracts"; | | 6 | +import { isSvgAttachment } from "@t3tools/shared/image"; | 6 | 7 | import * as Clock from "effect/Clock"; | 7 | 8 | import * as Crypto from "effect/Crypto"; | 8 | 9 | import * as Effect from "effect/Effect"; | @@ -93,8 +94,12 @@ export const issueAttachmentUploadUrl = Effect.fn("AttachmentUpload.issueUrl")(f | 93 | 94 | } | 94 | 95 |
| 95 | 96 | const attachmentType = input.type ?? "image"; | | 97 | + const name = | | 98 | + attachmentType === "file" && isSvgAttachment(input) && !/\.svg$/i.test(input.name) | | 99 | + ? `${input.name}.svg` | | 100 | + : input.name; | 96 | 101 | const attachmentId = createPendingAttachmentId( | 97 | | - attachmentType === "file" ? attachmentFileExtension(input.name) : undefined, | | 102 | + attachmentType === "file" ? attachmentFileExtension(name) : undefined, | 98 | 103 | ); | 99 | 104 | const expiresAt = nowMs + ATTACHMENT_UPLOAD_URL_TTL_MS; | 100 | 105 | const encodedPayload = base64UrlEncode( | @@ -103,7 +108,7 @@ export const issueAttachmentUploadUrl = Effect.fn("AttachmentUpload.issueUrl")(f | 103 | 108 | kind: "attachment-upload", | 104 | 109 | type: attachmentType, | 105 | 110 | attachmentId, | 106 | | - name: input.name, | | 111 | + name, | 107 | 112 | mimeType: input.mimeType, | 108 | 113 | sizeBytes: input.sizeBytes, | 109 | 114 | expiresAt, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/AttachmentClaims.ts +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | } from "@t3tools/contracts"; | 7 | 7 | import * as Effect from "effect/Effect"; | 8 | 8 | import * as Schema from "effect/Schema"; | | 9 | +import { isSvgAttachment } from "@t3tools/shared/image"; | 9 | 10 |
| 10 | 11 | import { | 11 | 12 | parseThreadSegmentFromAttachmentId, | @@ -106,6 +107,12 @@ export const claimPendingAttachments = Effect.fn("AttachmentClaims.claimPendingA | 106 | 107 | const normalized: ChatAttachment = { | 107 | 108 | ...attachment, | 108 | 109 | id: ChatAttachmentId.make(claim.finalId), | | 110 | + name: | | 111 | + attachment.type === "file" && | | 112 | + isSvgAttachment(attachment) && | | 113 | + !/\.svg$/i.test(attachment.name) | | 114 | + ? `${attachment.name}.svg` | | 115 | + : attachment.name, | 109 | 116 | mimeType: attachment.mimeType.toLowerCase(), | 110 | 117 | }; | 111 | 118 | const expectedPath = resolveAttachmentPath({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/UserMessageContextChips.tsx +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { isSvgAttachment } from "@t3tools/shared/image"; | 1 | 2 | import { useRenderedAppearance } from "../../hooks/useTheme"; | 2 | 3 | import { PullRequestGlyph } from "../pullRequest/pullRequestIcons"; | 3 | 4 | import { ReadOnlySourcePreview } from "../files/AttachmentFilePreview"; | @@ -268,6 +269,7 @@ function UserMessageElementDetails({ | 268 | 269 | } | 269 | 270 |
| 270 | 271 | interface UserMessageContextRenderContext { | | 272 | + environmentId: import("@t3tools/contracts").EnvironmentId; | 271 | 273 | reference: ChatMarkdownContextReference; | 272 | 274 | annotationImage: ChatImageAttachment | null; | 273 | 275 | attachment: ChatImageAttachment | ChatFileAttachment | null; | @@ -378,6 +380,11 @@ const userMessageContextPresentationRegistry = createContextPresentationRegistry | 378 | 380 | return ( | 379 | 381 | <FileChip | 380 | 382 | name={record.name} | | 383 | + svgPreview={ | | 384 | + isSvgAttachment(attachment) | | 385 | + ? { environmentId: context.environmentId, attachmentId: attachment.id } | | 386 | + : undefined | | 387 | + } | 381 | 388 | size={size} | 382 | 389 | isVideo={isVideo} | 383 | 390 | theme={context.resolvedTheme} | @@ -509,13 +516,14 @@ export function UserMessageContextReferenceChip(props: { | 509 | 516 | onExpandVideo: (file: ChatFileAttachment) => void; | 510 | 517 | onOpenFile: (file: ChatFileAttachment) => void; | 511 | 518 | }) { | 512 | | - const { resolvedTheme } = use(TimelineRowCtx); | | 519 | + const { resolvedTheme, activeThreadEnvironmentId } = use(TimelineRowCtx); | 513 | 520 | const copyMarkdown = formatComposerContextReference({ | 514 | 521 | kind: props.reference.kind, | 515 | 522 | contextId: props.reference.contextId as ComposerContextId, | 516 | 523 | label: props.reference.label, | 517 | 524 | }); | 518 | 525 | return userMessageContextPresentationRegistry.render(props.reference.kind, props.record, { | | 526 | + environmentId: activeThreadEnvironmentId, | 519 | 527 | reference: props.reference, | 520 | 528 | annotationImage: props.annotationImage, | 521 | 529 | attachment: props.attachment, | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/composerAttachmentFiles.test.ts +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,22 @@ import { | 18 | 18 | } from "./composerAttachmentFiles"; | 19 | 19 |
| 20 | 20 | describe("composer attachment files", () => { | | 21 | + it("attaches SVG as a file and rejects other unsupported images", () => { | | 22 | + expect(classifyComposerAttachmentFile({ name: "diagram.svg", type: "image/svg+xml" })).toBe( | | 23 | + "file", | | 24 | + ); | | 25 | + expect(classifyComposerAttachmentFile({ name: "diagram.SVG", type: "" })).toBe("file"); | | 26 | + expect( | | 27 | + classifyComposerAttachmentFile({ name: "diagram.svg", type: "application/octet-stream" }), | | 28 | + ).toBe("file"); | | 29 | + expect(classifyComposerAttachmentFile({ name: "photo.tiff", type: "image/tiff" })).toBe( | | 30 | + "unsupported-image", | | 31 | + ); | | 32 | + expect(classifyComposerAttachmentFile({ name: "report.pdf", type: "application/pdf" })).toBe( | | 33 | + "file", | | 34 | + ); | | 35 | + }); | | 36 | + | 21 | 37 | it("keeps DNG originals on the file path without changing their bytes", async () => { | 22 | 38 | const bytes = new Uint8Array([0x49, 0x49, 0x2a, 0x00, 0x08, 0, 0, 0, 0xfe, 0xff, 0x00, 0x80]); | 23 | 39 | const files = [ | @@ -96,6 +112,22 @@ describe("composer attachment files", () => { | 96 | 112 | ).toBe(false); | 97 | 113 | }); | 98 | 114 |
| | 115 | + it("claims SVG and unsupported image pastes even when text is present", () => { | | 116 | + const images = [ | | 117 | + new File(["svg"], "diagram.svg", { type: "image/svg+xml" }), | | 118 | + new File(["tiff"], "photo.tiff", { type: "image/tiff" }), | | 119 | + ]; | | 120 | + | | 121 | + for (const image of images) { | | 122 | + expect( | | 123 | + shouldHandleComposerAttachmentPaste({ | | 124 | + files: [image], | | 125 | + plainText: "Image caption", | | 126 | + }), | | 127 | + ).toBe(true); | | 128 | + } | | 129 | + }); | | 130 | + | 99 | 131 | it("uses the hard local limit while server config is unknown", () => { | 100 | 132 | expect( | 101 | 133 | fileAttachmentStagingLimit({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/chat/composerAttachmentFiles.ts +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ import { | 11 | 11 | import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore"; | 12 | 12 | import { isHeicImageFile } from "../../lib/imageCompression"; | 13 | 13 | import { isVideoAttachment, videoMimeType } from "../../types"; | | 14 | +import { isSvgAttachment } from "@t3tools/shared/image"; | 14 | 15 |
| 15 | 16 | type ComposerAttachmentFileKind = "image" | "file" | "unsupported-image"; | 16 | 17 |
| @@ -72,6 +73,7 @@ function isDngFile(file: Pick<File, "name" | "type">): boolean { | 72 | 73 | export function composerFileAttachmentMimeType(file: Pick<File, "name" | "type">): string { | 73 | 74 | // Downstream image detection uses MIME, even for attachments staged as files. | 74 | 75 | if (isDngFile(file)) return "image/x-adobe-dng"; | | 76 | + if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "image/svg+xml"; | 75 | 77 | return ( | 76 | 78 | videoMimeType({ name: file.name, mimeType: file.type }) ?? | 77 | 79 | (file.type || "application/octet-stream") | @@ -81,6 +83,7 @@ export function composerFileAttachmentMimeType(file: Pick<File, "name" | "type"> | 81 | 83 | export function classifyComposerAttachmentFile( | 82 | 84 | file: Pick<File, "name" | "type">, | 83 | 85 | ): ComposerAttachmentFileKind { | | 86 | + if (isSvgAttachment({ name: file.name, mimeType: file.type })) return "file"; | 84 | 87 | if (isDngFile(file)) return "file"; | 85 | 88 | if (isHeicImageFile(file)) { | 86 | 89 | return "image"; | @@ -164,7 +167,11 @@ export function shouldHandleComposerAttachmentPaste(input: { | 164 | 167 | input.files.some((file) => { | 165 | 168 | if (isDngFile(file)) return true; | 166 | 169 | const classification = classifyComposerAttachmentFile(file); | 167 | | - return classification === "image" || classification === "unsupported-image"; | | 170 | + return ( | | 171 | + classification === "image" || | | 172 | + classification === "unsupported-image" || | | 173 | + isSvgAttachment({ name: file.name, mimeType: file.type }) | | 174 | + ); | 168 | 175 | }) | 169 | 176 | ) { | 170 | 177 | return true; | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/composerContextPresentation.tsx +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import ChatMarkdown from "./ChatMarkdown"; | | 2 | +import { isSvgAttachment } from "@t3tools/shared/image"; | 2 | 3 | import { ReadOnlySourcePreview } from "./files/AttachmentFilePreview"; | 3 | 4 | import type { PreviewAnnotationPayload, ThreadContextRecord } from "@t3tools/contracts"; | 4 | 5 | import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments"; | @@ -222,6 +223,15 @@ function FileContextChip(props: { | 222 | 223 | return ( | 223 | 224 | <FileChip | 224 | 225 | name={props.record.name} | | 226 | + svgPreview={ | | 227 | + isSvgAttachment(props.record) | | 228 | + ? { | | 229 | + file: props.record.file, | | 230 | + environmentId: props.record.uploadEnvironmentId, | | 231 | + attachmentId: props.record.uploadedAttachmentId, | | 232 | + } | | 233 | + : undefined | | 234 | + } | 225 | 235 | size={size} | 226 | 236 | isVideo={isVideo} | 227 | 237 | theme={resolvedTheme} | |
|