Created
July 19, 2024 14:45
-
-
Save mharris717/695e4e9572998e70d65c74221abce40d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class HttpTool { | |
constructor(readonly name: string) {} | |
url() { | |
const n = this.name.toLowerCase().replace("/", "-"); | |
return `https://${n}.web.val.run`; | |
} | |
async call(ops: any): Promise<string> { | |
const url = this.url(); | |
const response = await fetch(url, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify(ops), | |
}); | |
const parsed = await response.json(); | |
return parsed.result; | |
} | |
async zodSchema(): Promise<z.ZodTypeAny> { | |
const response = await fetch(`${this.url()}/schema`); | |
const j = await response.json(); | |
const s = jsonSchemaToZod(j); | |
return evalZodSchema(s); | |
} | |
async toolDesc(): Promise<CoreTool> { | |
const schema = await this.zodSchema(); | |
return { | |
description: this.name, | |
parameters: schema, | |
execute: async (ops: any) => { | |
const parsed = schema.parse(ops); | |
return this.call(parsed); | |
}, | |
}; | |
} | |
} | |
const evalZodSchema = (s: string) => { | |
const fullStr = `const { z } = require("zod");\n${s}`; | |
return eval(fullStr); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment