Skip to content

Commit

Permalink
feat: ✨ add file size limit and improve buffer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Feb 5, 2025
1 parent 61123b3 commit 4b671f8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/bufferlike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export async function resolveBufferLike(
else if (bufferLike instanceof Uint8Array) return Buffer.from(bufferLike)
else if (
typeof bufferLike === "object" &&
(bufferLike as WorkspaceFile).content
)
(bufferLike as WorkspaceFile).filename
) {
return Buffer.from(
(bufferLike as WorkspaceFile).content,
(bufferLike as WorkspaceFile).encoding || "utf-8"
await resolveFileBytes(bufferLike as WorkspaceFile, options)
)
}
console.log(bufferLike)
throw new Error("Unsupported buffer-like object")
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const MODEL_PROVIDER_OPENAI_HOSTS = Object.freeze([
MODEL_PROVIDER_OPENAI,
MODEL_PROVIDER_GITHUB,
MODEL_PROVIDER_AZURE_OPENAI,
MODEL_PROVIDER_AZURE_SERVERLESS_OPENAI
MODEL_PROVIDER_AZURE_SERVERLESS_OPENAI,
])

export const TRACE_FILE_PREVIEW_MAX_LENGTH = 240
Expand Down Expand Up @@ -344,3 +344,5 @@ export const WS_MAX_FRAME_LENGTH = 1000000

export const SCHEMA_DEFAULT_FORMAT = "json"
export const THINK_REGEX = /<think>(.*?)($|<\/think>)/gis

export const MAX_FILE_CONTENT_SIZE = 1024 * 1024 * 2 // 2MB
13 changes: 8 additions & 5 deletions packages/core/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
DOCX_MIME_TYPE,
DOCX_REGEX,
HTTPS_REGEX,
MAX_FILE_CONTENT_SIZE,
PDF_MIME_TYPE,
PDF_REGEX,
XLSX_MIME_TYPE,
Expand All @@ -37,9 +38,9 @@ import { tidyData } from "./tidy"
*/
export async function resolveFileContent(
file: WorkspaceFile,
options?: TraceOptions
options?: TraceOptions & { maxFileSize?: number }
) {
const { trace } = options || {}
const { trace, maxFileSize = MAX_FILE_CONTENT_SIZE } = options || {}

// decode known files
if (file.encoding === "base64") {
Expand Down Expand Up @@ -120,7 +121,7 @@ export async function resolveFileContent(
if (!isBinary) file.content = await readText(filename)
else {
const info = await host.statFile(filename)
if (info.size < 1000000) {
if (!maxFileSize || info.size < maxFileSize) {
const bytes: Uint8Array = await host.readFile(filename)
file.encoding = "base64"
file.content = toBase64(bytes)
Expand Down Expand Up @@ -200,8 +201,10 @@ export async function resolveFileBytes(
options?: TraceOptions
): Promise<Uint8Array> {
if (typeof filename === "object") {
if (filename.encoding === "base64" && filename.content)
return fromBase64(filename.content)
if (filename.encoding && filename.content)
return new Uint8Array(
Buffer.from(filename.content, filename.encoding)
)
filename = filename.filename
}

Expand Down
4 changes: 2 additions & 2 deletions packages/sample/genaisrc/defimages.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ script({
files: "src/robots.jpg",
model: "vision",
})
defImages(env.files, { detail: "low", autoCrop: true })
defImages(env.files, { detail: "low",})
$`give keywords describing for each image`
defImages(env.files, {
autoCrop: true,
greyscale: true,
maxHeight: 1024,
maxHeight: 400,
rotate: 90,
scale: 0.9,
crop: { x: 0, y: 0, w: 400, h: 400 },
Expand Down
Binary file added packages/sample/src/vision/galaxy.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4b671f8

Please sign in to comment.