reduce complexity, get working

This commit is contained in:
ocasta181 committed 2025-02-28 18:35:04 -08:00
1 parent 9f418b10f6
commit 8313fa099c
6 files changed
+91 -53

No files matched your search

+4 -2
View File
@@ -4,6 +4,7 @@ import fs from "fs/promises"
import path from "path"
import os from "os"
import { ContentTooLargeError } from "../../shared/errors"
import { calculateMaxAllowedSize } from "../../utils/content-size"
const CONTEXT_LIMIT = 1000
const USED_CONTEXT = 200
@@ -29,8 +30,9 @@ describe("extract-text", () => {
}
})
it("throws ContentTooLargeError when file would exceed limit", async () => {
const largeContent = "x".repeat(3000) // 3000 bytes = ~750 tokens
it("throws ContentTooLargeError when file would exceed half of context limit", async () => {
const halfContextLimit = calculateMaxAllowedSize(CONTEXT_LIMIT) // 500 tokens
const largeContent = "x".repeat(halfContextLimit * 4 + 4) // Just over half context limit in tokens
await fs.writeFile(tempFilePath, largeContent)
try {
+9 -4
View File
@@ -4,7 +4,7 @@ import pdf from "pdf-parse/lib/pdf-parse"
import mammoth from "mammoth"
import fs from "fs/promises"
import { isBinaryFile } from "isbinaryfile"
import { estimateFileSize } from "../../utils/content-size"
import { estimateFileSize, wouldExceedSizeLimit } from "../../utils/content-size"
import { ContentTooLargeError } from "../../shared/errors"
export async function extractTextFromFile(filePath: string, contextLimit: number, usedContext: number = 0): Promise<string> {
@@ -14,9 +14,14 @@ export async function extractTextFromFile(filePath: string, contextLimit: number
throw new Error(`File not found: ${filePath}`)
}
// Check file size before attempting to read
const sizeEstimate = await estimateFileSize(filePath, contextLimit, usedContext)
if (sizeEstimate.wouldExceedLimit) {
// Get file stats to check size
const stats = await fs.stat(filePath)
// Check if file size would exceed limit before attempting to read
// This is more efficient than creating a full SizeEstimate object when we just need a boolean check
if (wouldExceedSizeLimit(stats.size, contextLimit)) {
// Only create the full size estimate when we need it for the error
const sizeEstimate = await estimateFileSize(filePath, contextLimit, usedContext)
throw new ContentTooLargeError({
type: "file",
path: filePath,