mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-04 00:37:48 +08:00
- Template dialogs no longer submit the outer chat form - Sending is blocked while files or URLs are still extracting - File and URL extraction no longer drop or resurrect entries - IME composition Enter no longer sends - Tool call cards show the error text; keyboard handling on cards fixed - Template import available when empty, edit dialog resets, saved templates refresh - Only png/jpeg/gif/webp images accepted, SVG sent as text; PDF objects released - parse-url request sends the access code
79 lines
1.7 KiB
TypeScript
79 lines
1.7 KiB
TypeScript
import { extractText } from "unpdf"
|
|
|
|
// Maximum characters allowed for extracted text (configurable via env)
|
|
const DEFAULT_MAX_EXTRACTED_CHARS = 150000 // 150k chars
|
|
export const MAX_EXTRACTED_CHARS =
|
|
Number(process.env.NEXT_PUBLIC_MAX_EXTRACTED_CHARS) ||
|
|
DEFAULT_MAX_EXTRACTED_CHARS
|
|
|
|
// Text file extensions we support
|
|
const TEXT_EXTENSIONS = [
|
|
".txt",
|
|
".md",
|
|
".markdown",
|
|
".json",
|
|
".csv",
|
|
".xml",
|
|
".svg",
|
|
".html",
|
|
".css",
|
|
".js",
|
|
".ts",
|
|
".jsx",
|
|
".tsx",
|
|
".py",
|
|
".java",
|
|
".c",
|
|
".cpp",
|
|
".h",
|
|
".go",
|
|
".rs",
|
|
".yaml",
|
|
".yml",
|
|
".toml",
|
|
".ini",
|
|
".log",
|
|
".sh",
|
|
".bash",
|
|
".zsh",
|
|
]
|
|
|
|
/**
|
|
* Extract text content from a PDF file
|
|
* Uses unpdf library for client-side extraction
|
|
*/
|
|
export async function extractPdfText(file: File): Promise<string> {
|
|
const buffer = await file.arrayBuffer()
|
|
// Pass raw bytes so unpdf destroys the PDF document when it is done
|
|
const { text } = await extractText(new Uint8Array(buffer), {
|
|
mergePages: true,
|
|
})
|
|
return text as string
|
|
}
|
|
|
|
/**
|
|
* Check if a file is a PDF
|
|
*/
|
|
export function isPdfFile(file: File): boolean {
|
|
return file.type === "application/pdf" || file.name.endsWith(".pdf")
|
|
}
|
|
|
|
/**
|
|
* Check if a file is a text file
|
|
*/
|
|
export function isTextFile(file: File): boolean {
|
|
const name = file.name.toLowerCase()
|
|
return (
|
|
file.type.startsWith("text/") ||
|
|
file.type === "application/json" ||
|
|
TEXT_EXTENSIONS.some((ext) => name.endsWith(ext))
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Extract text content from a text file
|
|
*/
|
|
export async function extractTextFileContent(file: File): Promise<string> {
|
|
return await file.text()
|
|
}
|