import { ComputeJSON, ComputeText, sb, Substrate } from "npm:substrate";
import { z } from "npm:zod";
import { zodToJsonSchema } from "npm:zod-to-json-schema";
const original = {
personalInfo: {
name: "John Doe",
age: 30,
},
occupation: "Software Engineer",
fullAddress: "123 Main St, Anytown",
address: {
street: "123 Main St",
city: "Anytown",
},
};
const substrate = new Substrate({ apiKey: Deno.env.get("SUBSTRATE_API_KEY") });
const TargetSchema = z.object({
fullName: z.string(),
yearsOld: z.number().int().nonnegative(),
profession: z.string(),
location: z.object({
streetAddress: z.string(),
cityName: z.string(),
}),
});
const json = new ComputeJSON({
prompt: `Translate the following JSON object to the target schema.
${JSON.stringify(original)}`,
json_schema: zodToJsonSchema(TargetSchema),
});
const res = await substrate.run(json);
export default async function render(req: Request): Promise<Response> {
if (new URL(req.url).pathname === "/robots.txt") {
return new Response("User-agent: *\nDisallow: /");
}
return new Response(`<pre>${JSON.stringify(res.get(json).json_object, null, 2)}</pre>`, {
headers: {
"Content-Type": "text/html",
},
});
}