Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions apps/code/src/main/trpc/routers/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type { IAppMeta } from "@posthog/platform/app-meta";
import type { DialogSeverity, IDialog } from "@posthog/platform/dialog";
import type { IImageProcessor } from "@posthog/platform/image-processor";
import type { IUrlLauncher } from "@posthog/platform/url-launcher";
import { IMAGE_MIME_TYPES } from "@shared/constants/image";
import {
ALLOWED_IMAGE_MIME_TYPES,
IMAGE_MIME_TYPES,
isRasterImageFile,
} from "@posthog/shared";
import { z } from "zod";
import { container } from "../../di/container";
import { MAIN_TOKENS } from "../../di/tokens";
Expand Down Expand Up @@ -293,7 +297,8 @@ export const osRouter = router({
if (stat.size > input.maxSizeBytes) return null;

const ext = path.extname(input.filePath).toLowerCase().slice(1);
const mime = IMAGE_MIME_TYPES[ext] ?? "application/octet-stream";
const mime = IMAGE_MIME_TYPES[ext];
if (!mime || !ALLOWED_IMAGE_MIME_TYPES.has(mime)) return null;

const buffer = await fsPromises.readFile(input.filePath);
return `data:${mime};base64,${buffer.toString("base64")}`;
Expand Down Expand Up @@ -354,7 +359,7 @@ export const osRouter = router({
.input(z.object({ filePath: z.string().min(1) }))
.mutation(async ({ input }) => {
const ext = path.extname(input.filePath).toLowerCase().slice(1);
if (!IMAGE_MIME_TYPES[ext]) {
if (!isRasterImageFile(input.filePath)) {
throw new Error(`Unsupported image type: .${ext}`);
}

Expand Down
7 changes: 5 additions & 2 deletions apps/code/src/renderer/components/ui/SafeImagePreview.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Flex, Text } from "@radix-ui/themes";
import {
buildImageDataUrl,
isAllowedImageMimeType,
MAX_IMAGE_BASE64_LENGTH,
} from "@shared/utils/imageDataUrl";
} from "@posthog/shared";
import { Flex, Text } from "@radix-ui/themes";
import { useState } from "react";

interface SafeImagePreviewProps {
Expand All @@ -12,6 +12,7 @@ interface SafeImagePreviewProps {
mimeType: string;
alt?: string;
className?: string;
style?: React.CSSProperties;
/** Rendered when the image fails to decode or has a disallowed mime type. */
fallback?: React.ReactNode;
}
Expand All @@ -33,6 +34,7 @@ export function SafeImagePreview({
mimeType,
alt,
className,
style,
fallback,
}: SafeImagePreviewProps) {
const [hasError, setHasError] = useState(false);
Expand All @@ -57,6 +59,7 @@ export function SafeImagePreview({
src={buildImageDataUrl(mimeType, base64)}
alt={alt ?? "image preview"}
className={className ?? "max-h-full max-w-full object-contain"}
style={style}
onError={() => setHasError(true)}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import { useFileTreeStore } from "@features/right-sidebar/stores/fileTreeStore";
import { useCwd } from "@features/sidebar/hooks/useCwd";
import { useIsWorkspaceCloudRun } from "@features/workspace/hooks/useWorkspace";
import { Check, Copy } from "@phosphor-icons/react";
import {
getImageMimeType,
isRasterImageFile,
parseImageDataUrl,
} from "@posthog/shared";
import { Box, Flex, IconButton, Text } from "@radix-ui/themes";
import { trpcClient, useTRPC } from "@renderer/trpc/client";
import { getImageMimeType, isImageFile } from "@shared/constants/image";
import type { Task } from "@shared/types";
import { parseImageDataUrl } from "@shared/utils/imageDataUrl";

import { useQuery } from "@tanstack/react-query";
import { useCallback, useMemo, useState } from "react";
Expand Down Expand Up @@ -73,7 +76,7 @@ export function CodeEditorPanel({
const repoPath = useCwd(taskId);
const isInsideRepo = !!repoPath && absolutePath.startsWith(repoPath);
const filePath = getRelativePath(absolutePath, repoPath);
const isImage = isImageFile(absolutePath);
const isImage = isRasterImageFile(absolutePath);
const isMarkdown = isMarkdownFile(absolutePath);
const openFileInSplit = usePanelLayoutStore((s) => s.openFileInSplit);
const expandToFile = useFileTreeStore((s) => s.expandToFile);
Expand Down
40 changes: 20 additions & 20 deletions apps/code/src/renderer/features/editor/utils/cloud-prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@ const mockFs = vi.hoisted(() => ({
readFileAsBase64: { query: vi.fn() },
}));

vi.mock("@shared/constants/image", async () => {
const actual = await vi.importActual<
typeof import("@shared/constants/image")
>("@shared/constants/image");
return {
...actual,
getImageMimeType: (name: string) => {
const ext = name.split(".").pop()?.toLowerCase();
const map: Record<string, string> = {
png: "image/png",
jpg: "image/jpeg",
jpeg: "image/jpeg",
gif: "image/gif",
webp: "image/webp",
};
return map[ext ?? ""] ?? "image/png";
},
};
});

vi.mock("@renderer/trpc/client", () => ({
trpcClient: {
fs: mockFs,
Expand Down Expand Up @@ -172,6 +152,26 @@ describe("cloud-prompt", () => {
).rejects.toThrow(/Unsupported image/);
});

it("treats SVG attachments as text resource links", async () => {
const blocks = await buildCloudPromptBlocks(
'see <file path="/tmp/icon.svg" />',
);
expect(blocks[1]).toMatchObject({
type: "resource_link",
name: "icon.svg",
});
expect(mockFs.readFileAsBase64.query).not.toHaveBeenCalled();
});

it("rejects HEIC and HEIF as unsupported attachments (not images)", async () => {
await expect(
buildCloudPromptBlocks('see <file path="/tmp/photo.heic" />'),
).rejects.toThrow(/Unsupported attachment/);
await expect(
buildCloudPromptBlocks('see <file path="/tmp/photo.heif" />'),
).rejects.toThrow(/Unsupported attachment/);
});

it("does not rely on readAbsoluteFile for txt attachments", async () => {
const blocks = await buildCloudPromptBlocks(
'read <file path="/tmp/maybe-missing-on-disk.txt" />',
Expand Down
19 changes: 9 additions & 10 deletions apps/code/src/renderer/features/editor/utils/cloud-prompt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { ContentBlock } from "@agentclientprotocol/sdk";
import { CLOUD_PROMPT_PREFIX, serializeCloudPrompt } from "@posthog/shared";
import {
CLOUD_PROMPT_PREFIX,
getImageMimeType,
isClaudeImageFile,
isRasterImageFile,
serializeCloudPrompt,
} from "@posthog/shared";
import { trpcClient } from "@renderer/trpc/client";
import { getImageMimeType, isImageFile } from "@shared/constants/image";
import {
getFileExtension,
getFileName,
Expand Down Expand Up @@ -61,8 +66,6 @@ const TEXT_FILENAMES = new Set([
"README",
"README.md",
]);
const CLOUD_IMAGE_EXTENSIONS = new Set(["png", "jpg", "jpeg", "gif", "webp"]);

const MAX_EMBEDDED_IMAGE_BYTES = 5 * 1024 * 1024;

function isTextAttachment(filePath: string): boolean {
Expand All @@ -71,10 +74,6 @@ function isTextAttachment(filePath: string): boolean {
return TEXT_FILENAMES.has(fileName) || TEXT_EXTENSIONS.has(ext);
}

export function isSupportedCloudImageAttachment(filePath: string): boolean {
return CLOUD_IMAGE_EXTENSIONS.has(getFileExtension(filePath));
}

export function isSupportedCloudTextAttachment(filePath: string): boolean {
return isTextAttachment(filePath);
}
Expand Down Expand Up @@ -163,7 +162,7 @@ async function buildAttachmentBlock(filePath: string): Promise<ContentBlock> {
const fileName = getFileName(filePath);
const uri = pathToFileUri(filePath);

if (isSupportedCloudImageAttachment(fileName)) {
if (isClaudeImageFile(fileName)) {
const base64 = await trpcClient.fs.readFileAsBase64.query({ filePath });
if (!base64) {
throw new Error(`Unable to read attached image ${fileName}`);
Expand All @@ -183,7 +182,7 @@ async function buildAttachmentBlock(filePath: string): Promise<ContentBlock> {
};
}

if (isImageFile(fileName)) {
if (isRasterImageFile(fileName)) {
throw new Error(
`Cloud image attachments currently support PNG, JPG, GIF, and WebP. Unsupported image: ${fileName}`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
DropdownMenuItem,
DropdownMenuTrigger,
} from "@posthog/quill";
import { isRasterImageFile } from "@posthog/shared";
import { trpcClient, useTRPC } from "@renderer/trpc/client";
import { toast } from "@renderer/utils/toast";
import { isImageFile } from "@shared/constants/image";
import { useQuery } from "@tanstack/react-query";
import { useRef, useState } from "react";
import {
Expand Down Expand Up @@ -123,7 +123,7 @@ export function AttachmentMenu({
try {
const results = await trpcClient.os.selectAttachments.query({ mode });
for (const { path: filePath, kind } of results) {
if (kind === "file" && isImageFile(filePath)) {
if (kind === "file" && isRasterImageFile(filePath)) {
try {
const attachment = await persistImageFilePath(filePath);
onAddAttachment(attachment);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { File, X } from "@phosphor-icons/react";
import { isGifFile, isRasterImageFile } from "@posthog/shared";
import { Dialog, Flex, IconButton, Text } from "@radix-ui/themes";
import { useTRPC } from "@renderer/trpc/client";
import { isGifFile, isImageFile } from "@shared/constants/image";
import { useQuery } from "@tanstack/react-query";
import { useEffect, useRef } from "react";
import type { FileAttachment } from "../utils/content";
Expand Down Expand Up @@ -151,7 +151,7 @@ export function AttachmentsBar({ attachments, onRemove }: AttachmentsBarProps) {
return (
<Flex gap="1" align="center" className="flex-wrap pb-1.5">
{attachments.map((att) =>
isImageFile(att.label) ? (
isRasterImageFile(att.label) ? (
<ImageThumbnail
key={att.id}
attachment={att}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ vi.mock("@renderer/trpc/client", () => ({
},
}));

vi.mock("@shared/constants/image", async () => {
const actual = await vi.importActual<
typeof import("@shared/constants/image")
>("@shared/constants/image");
vi.mock("@posthog/shared", async () => {
const actual =
await vi.importActual<typeof import("@posthog/shared")>("@posthog/shared");
return { ...actual, getImageMimeType: () => "image/png" };
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getImageMimeType, isRasterImageFile } from "@posthog/shared";
import { trpcClient } from "@renderer/trpc/client";
import { toast } from "@renderer/utils/toast";
import { getImageMimeType, isImageFile } from "@shared/constants/image";
import { getFilePath } from "@utils/getFilePath";
import type { FileAttachment } from "./content";

Expand Down Expand Up @@ -74,7 +74,7 @@ export async function resolveDroppedFile(
const filePath = getFilePath(file);
if (!filePath) return null;

if (isImageFile(file.name)) {
if (isRasterImageFile(file.name)) {
try {
return await persistImageFilePath(filePath);
} catch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EditorView } from "@codemirror/view";
import { SafeImagePreview } from "@components/ui/SafeImagePreview";
import { MultiFileDiff } from "@pierre/diffs/react";
import { parseImageDataUrl } from "@posthog/shared";
import { Code } from "@radix-ui/themes";
import { parseImageDataUrl } from "@shared/utils/imageDataUrl";
import { useThemeStore } from "@stores/themeStore";
import { compactHomePath } from "@utils/path";
import { useEffect, useMemo, useRef } from "react";
Expand Down
47 changes: 3 additions & 44 deletions apps/code/src/renderer/utils/generateTitle.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,15 @@
import { fetchAuthState } from "@features/auth/hooks/authQueries";
import { xmlToContent } from "@features/message-editor/utils/content";
import { isBinaryFile } from "@posthog/shared";
import { trpcClient } from "@renderer/trpc";
import { logger } from "@utils/logger";
import { getFileName } from "@utils/path";

const log = logger.scope("title-generator");

const ATTACHED_FILES_REGEX = /^\[?Attached files:.*]?$/gm;
const PASTED_TEXT_SNIPPET_LIMIT = 500;

const BINARY_EXTENSIONS = new Set([
"png",
"jpg",
"jpeg",
"gif",
"webp",
"bmp",
"ico",
"svg",
"mp3",
"mp4",
"wav",
"avi",
"mov",
"mkv",
"pdf",
"zip",
"tar",
"gz",
"rar",
"7z",
"exe",
"dll",
"so",
"dylib",
"wasm",
"ttf",
"otf",
"woff",
"woff2",
"eot",
]);

function getExtension(filePath: string): string {
const dot = filePath.lastIndexOf(".");
return dot >= 0 ? filePath.slice(dot + 1).toLowerCase() : "";
}

function getFileName(filePath: string): string {
const slash = filePath.lastIndexOf("/");
return slash >= 0 ? filePath.slice(slash + 1) : filePath;
}

export async function enrichDescriptionWithFileContent(
description: string,
filePaths: string[] = [],
Expand All @@ -74,7 +33,7 @@ export async function enrichDescriptionWithFileContent(

const parts = await Promise.all(
paths.map(async (filePath) => {
if (BINARY_EXTENSIONS.has(getExtension(filePath))) {
if (isBinaryFile(filePath)) {
return `[Attached: ${getFileName(filePath)}]`;
}
try {
Expand Down
27 changes: 0 additions & 27 deletions apps/code/src/shared/constants/image.ts

This file was deleted.

Loading
Loading