Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update defImages function to support multiple file types and handle Blob conversion #705

Merged
merged 6 commits into from
Sep 11, 2024
6 changes: 4 additions & 2 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,31 +235,51 @@
) => {
appendChild(node, createSchemaNode(name, schema, defOptions))

return name
}

const defImages = (files: StringLike, defOptions?: DefImagesOptions) => {
const defImages = (
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,
defOptions?: DefImagesOptions
) => {
const { detail } = defOptions || {}
if (Array.isArray(files))
files.forEach((file) => defImages(file, defOptions))
else if (typeof files === "string")
appendChild(node, createImageNode({ url: files, detail }))
else if (files instanceof Buffer) {
const buffer: Buffer = files
appendChild(
node,
createImageNode(
(async () => {
const mime = await fileTypeFromBuffer(buffer)
const b64 = await buffer.toString("base64")
const url = `data:${mime.mime};base64,${b64}`
return {
url,
detail,
}
})()
)
)
} else if (files instanceof Blob) {
const blob: Blob = files
appendChild(
node,
createImageNode(
(async () => {
const buffer = Buffer.from(await blob.arrayBuffer())
const mime = await fileTypeFromBuffer(buffer)
const b64 = await buffer.toString("base64")
const url = `data:${mime.mime};base64,${b64}`

Check failure on line 275 in packages/core/src/runpromptcontext.ts

View workflow job for this annotation

GitHub Actions / build

You are creating an async function but not awaiting it immediately, which can lead to unexpected behavior. Consider refactoring this to await the promise immediately or handle it properly.
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
return {
url,
detail,
}
})()
)
)

Check failure on line 282 in packages/core/src/runpromptcontext.ts

View workflow job for this annotation

GitHub Actions / build

You are creating an async function inside a loop, which can lead to performance issues. Consider refactoring this to use Promise.all to handle multiple promises concurrently.

Check failure on line 282 in packages/core/src/runpromptcontext.ts

View workflow job for this annotation

GitHub Actions / build

There is no error handling for the async function. Consider adding a try-catch block to handle potential errors.
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function defImages is missing a return type. It's a good practice to always specify a return type for functions to improve code readability and prevent potential bugs.

generated by pr-review-commit missing_return_type

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are creating an async function but not awaiting it, which can lead to unexpected behavior. Consider adding an await keyword or returning the promise.

generated by pr-review-commit async_without_await

} else {
const file: WorkspaceFile = files
appendChild(
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
type OptionsOrString<TOptions extends string> = (string & {}) | TOptions

type ElementOrArray<T> = T | T[]

interface PromptGenerationConsole {
log(...data: any[]): void
warn(...data: any[]): void
Expand Down Expand Up @@ -1537,7 +1539,7 @@ interface ChatGenerationContext extends ChatTurnGenerationContext {
options?: DefSchemaOptions
): string
defImages(
files: StringLike | Buffer | Blob,
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in the type of the 'files' parameter from 'StringLike | Buffer | Blob' to 'ElementOrArray<string | WorkspaceFile | Buffer | Blob>' in the 'defImages' function is a breaking change. This can cause issues in the existing code where this function is used.

generated by pr-review-commit breaking_change

options?: DefImagesOptions
): void
defTool(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/prompt_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ declare function defSchema(
* @param options
*/
declare function defImages(
files: StringLike | Buffer | Blob,
files: ElementOrArray<string | WorkspaceFile | Buffer | Blob>,
options?: DefImagesOptions
): void

Expand Down
6 changes: 4 additions & 2 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/genaisrc/style/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/src/aici/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/src/errors/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/src/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/src/makecode/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/src/tla/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/sample/src/vision/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading