Skip to content

Commit 9165266

Browse files
committed
improvement(copilot): only ask for a preview derivative on image thumbnails
A video has no derivative path, so preview=1 there only spent a brand sniff per request. Adds the missing test coverage for the helper.
1 parent 6e1de2c commit 9165266

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { describe, expect, it } from 'vitest'
5+
import { getMothershipAttachmentPreviewUrl } from './attachment-preview'
6+
7+
describe('getMothershipAttachmentPreviewUrl', () => {
8+
it('asks for a preview derivative on images', () => {
9+
const url = getMothershipAttachmentPreviewUrl({
10+
key: 'copilot/a.heic',
11+
media_type: 'image/heic',
12+
})
13+
expect(url).toContain('preview=1')
14+
})
15+
16+
it('omits preview on videos, which have no derivative path', () => {
17+
const url = getMothershipAttachmentPreviewUrl({ key: 'copilot/a.mp4', media_type: 'video/mp4' })
18+
expect(url).not.toContain('preview')
19+
expect(url).toContain('context=mothership')
20+
})
21+
22+
it('returns undefined for a non-renderable attachment', () => {
23+
expect(
24+
getMothershipAttachmentPreviewUrl({ key: 'copilot/a.pdf', media_type: 'application/pdf' })
25+
).toBeUndefined()
26+
})
27+
28+
it('encodes the key so a slashed storage key stays one path segment', () => {
29+
const url = getMothershipAttachmentPreviewUrl({
30+
key: 'copilot/nested/a b.png',
31+
media_type: 'image/png',
32+
})
33+
expect(url).toContain(encodeURIComponent('copilot/nested/a b.png'))
34+
})
35+
})

apps/sim/lib/copilot/chat/attachment-preview.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ export function getMothershipAttachmentPreviewUrl(file: {
22
key: string
33
media_type: string
44
}): string | undefined {
5-
if (!file.media_type.startsWith('image/') && !file.media_type.startsWith('video/')) {
5+
const isImage = file.media_type.startsWith('image/')
6+
if (!isImage && !file.media_type.startsWith('video/')) {
67
return undefined
78
}
8-
// `preview=1`: this URL only ever backs a rendered thumbnail, so the serve route may
9-
// substitute a browser-renderable derivative for a format no browser decodes (HEIC).
10-
return `/api/files/serve/${encodeURIComponent(file.key)}?context=mothership&preview=1`
9+
// `preview=1` only for images: this URL backs a rendered thumbnail, so the serve route
10+
// may substitute a browser-renderable derivative for a format no browser decodes (HEIC).
11+
// A video has no derivative path, so asking would only spend a brand sniff per request.
12+
const preview = isImage ? '&preview=1' : ''
13+
return `/api/files/serve/${encodeURIComponent(file.key)}?context=mothership${preview}`
1114
}

0 commit comments

Comments
 (0)