-
Notifications
You must be signed in to change notification settings - Fork 11
feat(cas): add helper method for authenticated citation sources #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b8b1549
ef895ef
40297a9
79d6af5
f99a437
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * Helpers for resolving a citation media source into a downloadable document. | ||
| */ | ||
| import type { CitationSourceMedia } from '@/models/conversational-agent'; | ||
|
|
||
| // The file extensions the context service can reference, mapped to their MIME | ||
| // types. Used only as a fallback for the octet-stream case below, so the | ||
| // extension set mirrors exactly what the reference service supports. | ||
| const EXTENSION_TO_MIME_TYPE: Record<string, string> = { | ||
| '.pdf': 'application/pdf', | ||
| '.csv': 'text/csv', | ||
| '.json': 'application/json', | ||
| '.ods': 'application/vnd.oasis.opendocument.spreadsheet', | ||
| '.xls': 'application/vnd.ms-excel', | ||
| '.xlsm': 'application/vnd.ms-excel.sheet.macroEnabled.12', | ||
| '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
| '.txt': 'text/plain', | ||
| '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', | ||
| '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', | ||
| '.png': 'image/png', | ||
| '.jpg': 'image/jpeg', | ||
| '.jpeg': 'image/jpeg', | ||
| '.html': 'text/html' // ECS lists it, but clamped to octet-stream below | ||
| }; | ||
|
|
||
| const OCTET_STREAM = 'application/octet-stream'; | ||
|
|
||
| // MIME types that execute script when a downloaded blob is previewed inline: a | ||
| // blob URL runs in the embedder's origin, so an <iframe>/window.open preview of | ||
| // citation HTML could run its script in the consuming app. We never hand these | ||
| // back with a renderable type — forcing octet-stream makes the browser download | ||
| // rather than execute (matching how the reference service itself serves HTML). | ||
| const UNSAFE_INLINE_TYPES = new Set(['text/html', 'application/xhtml+xml']); | ||
|
|
||
| /** | ||
| * Determines the best MIME type for the downloaded document. Prefers the | ||
| * source's declared `mimeType`, then the server's Content-Type, and finally | ||
| * falls back to the file extension in the title — because the context service | ||
| * reference endpoint frequently responds with `application/octet-stream`. | ||
| * | ||
| * HTML-family types are always downgraded to `application/octet-stream` so a | ||
| * consumer previewing the blob inline can't be made to execute citation markup. | ||
| */ | ||
| export function resolveCitationMimeType( | ||
| source: CitationSourceMedia, | ||
| responseType: string | ||
| ): string { | ||
| const resolved = selectMimeType(source, responseType); | ||
| const essence = resolved.split(';')[0].trim().toLowerCase(); | ||
| return UNSAFE_INLINE_TYPES.has(essence) ? OCTET_STREAM : resolved; | ||
| } | ||
|
|
||
| function selectMimeType(source: CitationSourceMedia, responseType: string): string { | ||
| if (source.mimeType) { | ||
| return source.mimeType; | ||
| } | ||
| if (responseType && responseType !== OCTET_STREAM) { | ||
| return responseType; | ||
| } | ||
| const extension = source.title?.toLowerCase().match(/\.[^.]*$/)?.[0]; | ||
|
Check warning on line 60 in src/services/conversational-agent/helpers/citation.ts
|
||
| return (extension && EXTENSION_TO_MIME_TYPE[extension]) || responseType || OCTET_STREAM; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test file path does not mirror the
The file should be moved to match the source tree. |
||
| import { resolveCitationMimeType } from '@/services/conversational-agent/helpers/citation'; | ||
| import type { CitationSourceMedia } from '@/models/conversational-agent'; | ||
|
|
||
| const media = (over: Partial<CitationSourceMedia> = {}): CitationSourceMedia => ({ | ||
| title: 'doc.pdf', | ||
| number: 1, | ||
| downloadUrl: 'https://alpha.uipath.com/org/tenant/ecs_/v1.1/reference/abc', | ||
| ...over, | ||
| }); | ||
|
|
||
| describe('resolveCitationMimeType', () => { | ||
| it('prefers the source mimeType', () => { | ||
| expect(resolveCitationMimeType(media({ mimeType: 'application/pdf' }), 'text/html')).toBe( | ||
| 'application/pdf', | ||
| ); | ||
| }); | ||
|
|
||
| it('uses the response Content-Type when no source mimeType and it is meaningful', () => { | ||
| expect(resolveCitationMimeType(media({ mimeType: undefined }), 'image/png')).toBe('image/png'); | ||
| }); | ||
|
|
||
| it('falls back to the title extension when the response is octet-stream', () => { | ||
| expect( | ||
| resolveCitationMimeType(media({ mimeType: undefined, title: 'report.pdf' }), 'application/octet-stream'), | ||
| ).toBe('application/pdf'); | ||
| }); | ||
|
|
||
| it('falls back to octet-stream when nothing else is known', () => { | ||
| expect( | ||
| resolveCitationMimeType(media({ mimeType: undefined, title: 'noext' }), 'application/octet-stream'), | ||
| ).toBe('application/octet-stream'); | ||
| }); | ||
|
|
||
| it('downgrades an .html extension fallback to octet-stream (no inline execution)', () => { | ||
| expect( | ||
| resolveCitationMimeType(media({ mimeType: undefined, title: 'page.html' }), 'application/octet-stream'), | ||
| ).toBe('application/octet-stream'); | ||
| }); | ||
|
|
||
| it('downgrades a declared text/html source mimeType to octet-stream', () => { | ||
| expect(resolveCitationMimeType(media({ mimeType: 'text/html', title: 'page.html' }), '')).toBe( | ||
| 'application/octet-stream', | ||
| ); | ||
| }); | ||
|
|
||
| it('downgrades a text/html response Content-Type (with charset) to octet-stream', () => { | ||
| expect( | ||
| resolveCitationMimeType(media({ mimeType: undefined, title: 'page' }), 'text/html; charset=utf-8'), | ||
| ).toBe('application/octet-stream'); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.