File size: 809 Bytes
39b5447
57e16da
988ce90
 
57e16da
 
988ce90
 
39b5447
 
57e16da
988ce90
 
 
 
 
 
57e16da
39b5447
57e16da
1e32534
 
 
57e16da
1e32534
 
 
 
 
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
import type { LLM, Tool } from "$lib/types";

import { generatePrompt } from "./promptGeneration";
import { messageTool } from "./tools/message";

export async function generateCode(
  prompt: string,
  tools: Tool<any, any>[],
  files: FileList | null,
  llm: LLM
) {
  const fullprompt = generatePrompt(
    prompt,
    [...tools, messageTool],
    !!files && files[0].type.startsWith("image"),
    !!files && files[0].type.startsWith("audio")
  );

  const textAnswer = await llm.call(fullprompt);

  try {
    const regex = /```(.*?)```/gs;
    const matches = [...textAnswer.matchAll(regex)];

    const codeBlocks = matches.map((match) => match[1]);
    return codeBlocks[0].replace("js\n", "") ?? "nothing";
  } catch {
    throw new Error("The generated text doesn't contain any code blocks.");
  }
}