73 lines
2.8 KiB
TypeScript
73 lines
2.8 KiB
TypeScript
export type Attachment = {
|
|
id: string;
|
|
kind: 'file' | 'lines' | 'paste';
|
|
filename: string;
|
|
language: string | null;
|
|
content: string;
|
|
range?: [number, number];
|
|
source: '@' | 'line-select' | 'drop' | 'paste';
|
|
};
|
|
|
|
// v1.7: caps shared between drag-drop and paste-as-attachment so both paths
|
|
// reject the same way. Match the existing 10-attachment cap in
|
|
// ChatInput.addAttachment.
|
|
export const MAX_FILE_SIZE_BYTES = 5 * 1024 * 1024; // 5 MB
|
|
export const PASTE_INLINE_MAX_LINES = 8;
|
|
|
|
// First-8KB null-byte scan. Returns true if the content looks binary.
|
|
// Accepts a string (post-decode), an ArrayBuffer (pre-decode), or a Uint8Array.
|
|
// For binary files like PNG, scanning bytes is more reliable than scanning
|
|
// post-UTF-8-decode strings because invalid sequences may be replaced rather
|
|
// than preserved.
|
|
export function looksBinary(content: string | ArrayBuffer | Uint8Array): boolean {
|
|
const SCAN_BYTES = 8192;
|
|
if (typeof content === 'string') {
|
|
const max = Math.min(content.length, SCAN_BYTES);
|
|
for (let i = 0; i < max; i++) {
|
|
if (content.charCodeAt(i) === 0) return true;
|
|
}
|
|
return false;
|
|
}
|
|
const bytes = content instanceof Uint8Array ? content : new Uint8Array(content);
|
|
const max = Math.min(bytes.length, SCAN_BYTES);
|
|
for (let i = 0; i < max; i++) {
|
|
if (bytes[i] === 0) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
export const LANG_MAP: Record<string, string> = {
|
|
ts: 'typescript', tsx: 'tsx', js: 'javascript', jsx: 'jsx',
|
|
mjs: 'javascript', cjs: 'javascript',
|
|
py: 'python', go: 'go', rs: 'rust', rb: 'ruby', java: 'java',
|
|
c: 'c', h: 'c', cpp: 'cpp', cc: 'cpp', hpp: 'cpp', cs: 'csharp',
|
|
php: 'php', sh: 'bash', bash: 'bash', zsh: 'bash',
|
|
yml: 'yaml', yaml: 'yaml', json: 'json', toml: 'toml',
|
|
md: 'markdown', markdown: 'markdown', sql: 'sql', dockerfile: 'dockerfile',
|
|
html: 'html', htm: 'html', css: 'css', scss: 'scss',
|
|
};
|
|
|
|
export function inferLanguage(filename: string): string | null {
|
|
const base = filename.split('/').pop() ?? filename;
|
|
if (base.toLowerCase() === 'dockerfile') return 'dockerfile';
|
|
const m = base.match(/\.([^.]+)$/);
|
|
return m ? (LANG_MAP[m[1]!.toLowerCase()] ?? null) : null;
|
|
}
|
|
|
|
export function flattenToMessage(attachments: Attachment[], text: string): string {
|
|
if (attachments.length === 0) return text;
|
|
const blocks = attachments.map(a => {
|
|
const fence = '```' + (a.language ?? '');
|
|
let header: string;
|
|
if (a.kind === 'lines') {
|
|
header = `// from: ${a.filename}:${a.range?.[0] ?? '?'}-${a.range?.[1] ?? '?'}`;
|
|
} else if (a.kind === 'paste') {
|
|
header = `// from: pasted text (${a.content.split('\n').length} lines)`;
|
|
} else {
|
|
header = `// from: ${a.filename}`;
|
|
}
|
|
return `${fence}\n${header}\n${a.content}\n\`\`\``;
|
|
});
|
|
return [...blocks, text].filter(Boolean).join('\n\n');
|
|
}
|