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
54 changes: 53 additions & 1 deletion apps/desktop/src/app/chat/composer/text-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'

import { detectTrigger } from './text-utils'
import { blobDedupeKey, detectTrigger, extractClipboardImageBlobs } from './text-utils'

describe('detectTrigger', () => {
it('detects a bare slash trigger with an empty query', () => {
Expand All @@ -23,3 +23,55 @@ describe('detectTrigger', () => {
expect(detectTrigger('hello there')).toBeNull()
})
})

describe('extractClipboardImageBlobs', () => {
it('dedupes the same image exposed on both items and files', () => {
const image = new File([new Uint8Array([1, 2, 3])], 'paste.png', {
type: 'image/png',
lastModified: 1_700_000_000_000
})

const clipboard = {
files: {
length: 1,
item: (index: number) => (index === 0 ? image : null)
},
getData: () => '',
items: [
{
kind: 'file',
type: 'image/png',
getAsFile: () => image
}
]
} as unknown as DataTransfer

expect(extractClipboardImageBlobs(clipboard)).toEqual([image])
})

it('falls back to files when items has no image', () => {
const image = new File([new Uint8Array([4, 5])], 'shot.jpg', {
type: 'image/jpeg',
lastModified: 1_700_000_000_001
})

const clipboard = {
files: {
length: 1,
item: (index: number) => (index === 0 ? image : null)
},
getData: () => '',
items: []
} as unknown as DataTransfer

expect(extractClipboardImageBlobs(clipboard)).toEqual([image])
})
})

describe('blobDedupeKey', () => {
it('uses file metadata for File blobs', () => {
const file = new File([], 'a.png', { type: 'image/png', lastModified: 42 })

expect(blobDedupeKey(file)).toBe('file:a.png:0:image/png:42')
})
})
24 changes: 20 additions & 4 deletions apps/desktop/src/app/chat/composer/text-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@ export interface TriggerState {

const TRIGGER_RE = /(?:^|[\s])([@/])([^\s@/]*)$/

/** Stable key for paste dedupe — `items` and `files` often mirror the same image as different objects. */
export function blobDedupeKey(blob: Blob): string {
if (blob instanceof File) {
return `file:${blob.name}:${blob.size}:${blob.type}:${blob.lastModified}`
}

return `blob:${blob.size}:${blob.type}`
}

export function extractClipboardImageBlobs(clipboard: DataTransfer): Blob[] {
const blobs: Blob[] = []
const seen = new Set<Blob>()
const seen = new Set<string>()

const push = (blob: Blob | null) => {
if (!blob || blob.size === 0 || seen.has(blob)) {
if (!blob || blob.size === 0) {
return
}

const key = blobDedupeKey(blob)

if (seen.has(key)) {
return
}

seen.add(blob)
seen.add(key)
blobs.push(blob)
}

Expand All @@ -29,7 +44,8 @@ export function extractClipboardImageBlobs(clipboard: DataTransfer): Blob[] {
}
}

if (clipboard.files?.length) {
// Chromium/Electron expose the same pasted image on both `items` and `files`.
if (blobs.length === 0 && clipboard.files?.length) {
for (let i = 0; i < clipboard.files.length; i += 1) {
const file = clipboard.files.item(i)

Expand Down
Loading