File size: 1,023 Bytes
c2df9c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Tool } from 'openai-function-calling-tools';
import { z } from 'zod';

function createUrlSurfer() {
  const paramsSchema = z.object({
    input: z.string(),
  });
  const name = 'surfer';
  const description = 'A custom URL navigator. Useful when a URL is provided with a question. Input should be a prompt with a URL. Outputs a JSON array of relevant results.';

  const execute = async ({ input }: z.infer<typeof paramsSchema>) => {
    try {
      const res = await fetch('/api/surfer', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ prompt: input }),
      });

      if (!res.ok) {
        throw new Error(`HTTP error! status: ${res.status}`);
      }

      const data = await res.json();
      return data;
    } catch (error) {
      // @ts-ignore
      throw new Error(`Error in UrlSurfer: ${error.message}`);
    }
  };

  return new Tool(paramsSchema, name, description, execute).tool;
}

export { createUrlSurfer };