Spaces:
Paused
Paused
File size: 1,130 Bytes
57e16da |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import type { Tool } from './tools/tool';
export type Update = {
message: string;
data: undefined | string | Blob;
};
// this function passes the tools & files to the context before calling eval
export async function evalBuilder(
code: string,
tools: Tool<any, any>[],
files: FileList | null,
updateCallback: (message: string, data: undefined | string | Blob) => void
) {
async function wrapperEval() {
if (files) {
if (files[0].type.startsWith('image')) {
// @ts-ignore
globalThis['image'] = await files[0].arrayBuffer();
} else if (files[0].type.startsWith('audio')) {
// @ts-ignore
globalThis['audio'] = await files[0].arrayBuffer();
}
}
// add tools to context
for (const tool of tools.filter((el) => el.name !== 'message')) {
// @ts-ignore
globalThis[tool.name] = tool.call;
}
// @ts-ignore
globalThis['message'] = updateCallback;
await Object.getPrototypeOf(async function () {}).constructor(`
${code}
return await generate();
`)();
// clean up tools
for (const tool of tools) {
// @ts-ignore
delete globalThis[tool.name];
}
}
return wrapperEval;
}
|