Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions server/ai/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ const constructCollectionFileContext = (
isSelectedFiles?: boolean,
isMsgWithSources?: boolean,
): string => {

if ((!maxSummaryChunks && !isSelectedFiles) || isMsgWithSources) {
maxSummaryChunks = fields.chunks_summary?.length
}
Expand Down Expand Up @@ -625,13 +626,67 @@ const constructCollectionFileContext = (
.join("\n")
}

let imageChunks: ScoredChunk[] = []
const maxImageChunks =
fields.image_chunks_summary?.length &&
fields.image_chunks_summary?.length < 5
? fields.image_chunks_summary?.length
: 5


if (fields.matchfeatures) {

const summaryStrings = fields.image_chunks_summary?.map((c) =>
typeof c === "string" ? c : c.chunk,
) || []

imageChunks = getSortedScoredImageChunks(
fields.matchfeatures,
fields.image_chunks_pos_summary as number[],
summaryStrings as string[],
fields.docId,
)
} else {
const imageChunksPos = fields.image_chunks_pos_summary as number[]

imageChunks =
fields.image_chunks_summary?.map((chunk, idx) => {
const result = {
chunk: `${fields.docId}_${imageChunksPos[idx]}`,
index: idx,
score: 0,
}
return result
}) || []
}

let imageContent = ""
if (isSelectedFiles && fields?.matchfeatures) {

imageContent = imageChunks
.slice(0, maxImageChunks)
.sort((a, b) => a.index - b.index)
.map((v) => v.chunk)
.join("\n")
} else if (isSelectedFiles) {
imageContent = imageChunks.map((v) => v.chunk).join("\n")
} else {

imageContent = imageChunks
.slice(0, maxImageChunks)
.map((v) => v.chunk)
.join("\n")
}


return `Source: Knowledge Base
File: ${fields.fileName || "N/A"}
Knowledge Base ID: ${fields.clId || "N/A"}
Mime Type: ${fields.mimeType || "N/A"}
${fields.fileSize ? `File Size: ${fields.fileSize} bytes` : ""}${typeof fields.createdAt === "number" && isFinite(fields.createdAt) ? `\nCreated: ${getRelativeTime(fields.createdAt)}` : ""}${typeof fields.updatedAt === "number" && isFinite(fields.updatedAt) ? `\nUpdated At: ${getRelativeTime(fields.updatedAt)}` : ""}
${fields.createdBy ? `Uploaded By: ${fields.createdBy}` : ""}
${content ? `Content: ${content}` : ""}
${fields.image_chunks_summary && fields.image_chunks_summary.length ? `Image File Names: ${imageContent}` : ""}
\nvespa relevance score: ${relevance}\n`
}

Expand Down
20 changes: 17 additions & 3 deletions server/lib/chunkByOCR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Logger = getLogger(Subsystem.Integrations).child({
const DEFAULT_MAX_CHUNK_BYTES = 1024
const DEFAULT_IMAGE_DIR = "downloads/xyne_images_db"
const DEFAULT_LAYOUT_PARSING_BASE_URL = "http://localhost:8000"
const LAYOUT_PARSING_API_PATH = "/v2/models/layout-parsing/infer"
const LAYOUT_PARSING_API_PATH = "v2/models/layout-parsing/infer"

type LayoutParsingBlock = {
block_label?: string
Expand Down Expand Up @@ -298,7 +298,7 @@ async function callLayoutParsingApi(
Number.parseInt(process.env.LAYOUT_PARSING_FILE_TYPE ?? "0", 10) || 0
const visualize = process.env.LAYOUT_PARSING_VISUALIZE === "false"
const timeoutMs = Number.parseInt(
process.env.LAYOUT_PARSING_TIMEOUT_MS ?? "120000",
process.env.LAYOUT_PARSING_TIMEOUT_MS ?? "300000",
10,
)

Expand Down Expand Up @@ -352,7 +352,7 @@ async function callLayoutParsingApi(
`Layout parsing API request failed (${response.status}): ${responseText.slice(0, 200)}`,
)
}

Logger.info("Layout parsing API request succeeded, parsing response")
const envelope = (await response.json()) as LayoutParsingApiEnvelope

const outputPayload = envelope.outputs?.[0]?.data?.[0]
Expand All @@ -375,6 +375,20 @@ async function callLayoutParsingApi(
}

return result
} catch (error) {
// Log the layout parsing API failure with context
Logger.error(error, `Layout parsing API call failed for file: ${fileName}`, {
fileName,
fileSize: buffer.length,
apiUrl,
})

// Re-throw with enhanced error message for better debugging
if (error instanceof Error) {
throw new Error(`Layout parsing API failed for "${fileName}": ${error.message}`)
} else {
throw new Error(`Layout parsing API failed for "${fileName}": Unknown error occurred`)
}
} finally {
if (timer) {
clearTimeout(timer)
Expand Down
8 changes: 3 additions & 5 deletions server/services/fileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,9 @@ export class FileProcessorService {
// Log the processing failure with error details and context
Logger.error(error, `File processing failed for ${fileName} (${baseMimeType}, ${buffer.length} bytes)`)

// Create basic chunk on processing error
chunks = [
`File: ${fileName}, Type: ${baseMimeType}, Size: ${buffer.length} bytes`,
]
chunks_pos = [0]
// Re-throw the error to ensure proper error handling upstream
// This allows callers to handle failures appropriately (retries, status updates, etc.)
throw new Error(`Failed to process file "${fileName}": ${getErrorMessage(error)}`)
}

// For non-PDF files, create empty chunks_map and image_chunks_map for backward compatibility
Expand Down
Loading