File size: 994 Bytes
39b5447
57e16da
 
 
988ce90
 
 
 
57e16da
39b5447
65d7bbe
988ce90
65d7bbe
988ce90
57e16da
65d7bbe
 
 
 
 
57e16da
65d7bbe
 
57e16da
65d7bbe
39b5447
65d7bbe
 
 
39b5447
65d7bbe
 
 
 
 
 
 
988ce90
 
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
import type { Tool } from "$lib/types";

// 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 && files.length > 0) {
      // @ts-ignore
      globalThis["file"] = await files[0];
    }

    // add tools to context
    for (const tool of tools) {
      // @ts-ignore
      globalThis[tool.name] = tool.call;
    }

    // @ts-ignore
    globalThis["message"] = updateCallback;

    const returnString = "\nreturn await generate(file);";

    await Object.getPrototypeOf(async function () {}).constructor(
      code + returnString
    )();

    // clean up tools
    for (const tool of tools) {
      // @ts-ignore
      delete globalThis[tool.name];
      // @ts-ignore
      delete globalThis["file"];
    }
  }
  return wrapperEval;
}