mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 11:54:46 +00:00
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import type { GitDiffResult } from '../../../shared/git/diff/git-diff-compare-types'
|
|
import { getLargeDiffRenderLimit } from '../../../shared/large-diff-render-limit'
|
|
import {
|
|
IMAGE_AND_PDF_PREVIEW_MIME_TYPES,
|
|
filePreviewExtension
|
|
} from '../../../shared/file-preview/file-preview-kind'
|
|
|
|
export function buildDiffResult(
|
|
originalContent: string,
|
|
modifiedContent: string,
|
|
originalIsBinary: boolean,
|
|
modifiedIsBinary: boolean,
|
|
filePath?: string,
|
|
/** A side was over the read cap rather than proven binary. */
|
|
tooLarge = false
|
|
): GitDiffResult {
|
|
if (originalIsBinary || modifiedIsBinary) {
|
|
const mimeType = filePath
|
|
? IMAGE_AND_PDF_PREVIEW_MIME_TYPES[filePreviewExtension(filePath)]
|
|
: undefined
|
|
const sides = originalIsBinary
|
|
? { originalIsBinary: true as const, modifiedIsBinary }
|
|
: { originalIsBinary: false, modifiedIsBinary: true as const }
|
|
return {
|
|
kind: 'binary',
|
|
originalContent,
|
|
modifiedContent,
|
|
...sides,
|
|
// Why: renderer still checks legacy `isImage` before previewing, so set it for PDFs too until the contract is renamed.
|
|
...(mimeType ? { isImage: true, mimeType } : {}),
|
|
...(tooLarge ? { tooLarge: true } : {})
|
|
}
|
|
}
|
|
|
|
// Why: over the render limit, return metadata instead of huge text so the renderer can show fallback UI.
|
|
const largeDiffRenderLimit = getLargeDiffRenderLimit({ originalContent, modifiedContent })
|
|
if (largeDiffRenderLimit.limited) {
|
|
return {
|
|
kind: 'text',
|
|
originalContent: '',
|
|
modifiedContent: '',
|
|
originalIsBinary: false,
|
|
modifiedIsBinary: false,
|
|
largeDiffRenderLimit
|
|
}
|
|
}
|
|
|
|
return {
|
|
kind: 'text',
|
|
originalContent,
|
|
modifiedContent,
|
|
originalIsBinary: false,
|
|
modifiedIsBinary: false
|
|
}
|
|
}
|