mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-09 09:42:30 +08:00
🔗 Add URL Content Extraction Feature (#514)
* feat: add URL content extraction for AI diagram generation * Changes made as recommended by Claude: 1. Added a request timeout to prevent server resources from being tied up (route.ts) 2. Implemented runtime validation for the API response shape (url-utils.ts) 3. Removed hardcoded English error messages and replaced them with localized strings (url-input-dialog.tsx) 4. Fixed the incorrect i18n namespace (changed from pdf.* to url.*) (url-input-dialog.tsx and en/ja/zh.json) * chore: restore package.json and package-lock.json * fix: use i18n strings for URL dialog error messages --------- Co-authored-by: dayuan.jiang <jdy.toh@gmail.com>
This commit is contained in:
@@ -34,6 +34,7 @@ import { findCachedResponse } from "@/lib/cached-responses"
|
||||
import { formatMessage } from "@/lib/i18n/utils"
|
||||
import { isPdfFile, isTextFile } from "@/lib/pdf-utils"
|
||||
import { sanitizeMessages } from "@/lib/session-storage"
|
||||
import type { UrlData } from "@/lib/url-utils"
|
||||
import { type FileData, useFileProcessor } from "@/lib/use-file-processor"
|
||||
import { useQuotaManager } from "@/lib/use-quota-manager"
|
||||
import { cn, formatXML, isRealDiagram } from "@/lib/utils"
|
||||
@@ -158,6 +159,7 @@ export default function ChatPanel({
|
||||
|
||||
// File processing using extracted hook
|
||||
const { files, pdfData, handleFileChange, setFiles } = useFileProcessor()
|
||||
const [urlData, setUrlData] = useState<Map<string, UrlData>>(new Map())
|
||||
|
||||
const [showSettingsDialog, setShowSettingsDialog] = useState(false)
|
||||
const [showModelConfigDialog, setShowModelConfigDialog] = useState(false)
|
||||
@@ -710,6 +712,8 @@ export default function ChatPanel({
|
||||
input,
|
||||
files,
|
||||
pdfData,
|
||||
undefined,
|
||||
urlData,
|
||||
)
|
||||
|
||||
setMessages([
|
||||
@@ -735,6 +739,7 @@ export default function ChatPanel({
|
||||
setInput("")
|
||||
sessionStorage.removeItem(SESSION_STORAGE_INPUT_KEY)
|
||||
setFiles([])
|
||||
setUrlData(new Map())
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -755,6 +760,7 @@ export default function ChatPanel({
|
||||
files,
|
||||
pdfData,
|
||||
parts,
|
||||
urlData,
|
||||
)
|
||||
|
||||
// Add the combined text as the first part
|
||||
@@ -779,6 +785,7 @@ export default function ChatPanel({
|
||||
setInput("")
|
||||
sessionStorage.removeItem(SESSION_STORAGE_INPUT_KEY)
|
||||
setFiles([])
|
||||
setUrlData(new Map())
|
||||
} catch (error) {
|
||||
console.error("Error fetching chart data:", error)
|
||||
}
|
||||
@@ -854,6 +861,7 @@ export default function ChatPanel({
|
||||
clearDiagram()
|
||||
setDiagramHistory([])
|
||||
handleFileChange([]) // Use handleFileChange to also clear pdfData
|
||||
setUrlData(new Map())
|
||||
const newSessionId = `session-${Date.now()}-${Math.random()
|
||||
.toString(36)
|
||||
.slice(2, 9)}`
|
||||
@@ -972,6 +980,7 @@ export default function ChatPanel({
|
||||
files: File[],
|
||||
pdfData: Map<File, FileData>,
|
||||
imageParts?: any[],
|
||||
urlDataParam?: Map<string, UrlData>,
|
||||
): Promise<string> => {
|
||||
let userText = baseText
|
||||
|
||||
@@ -1002,6 +1011,14 @@ export default function ChatPanel({
|
||||
}
|
||||
}
|
||||
|
||||
if (urlDataParam) {
|
||||
for (const [url, data] of urlDataParam) {
|
||||
if (data.content) {
|
||||
userText += `\n\n[URL: ${url}]\nTitle: ${data.title}\n\n${data.content}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return userText
|
||||
}
|
||||
|
||||
@@ -1264,6 +1281,8 @@ export default function ChatPanel({
|
||||
files={files}
|
||||
onFileChange={handleFileChange}
|
||||
pdfData={pdfData}
|
||||
urlData={urlData}
|
||||
onUrlChange={setUrlData}
|
||||
sessionId={sessionId}
|
||||
error={error}
|
||||
models={modelConfig.models}
|
||||
|
||||
Reference in New Issue
Block a user