import { api } from "https://esm.town/v/pomdtr/api";
import { email } from "https://esm.town/v/std/email?v=12";
import { OpenAI } from "https://esm.town/v/std/OpenAI";
async function getValByAlias({ author, name }: { author: string; name: string }) {
const { id, code, readme } = await api(`/v1/alias/${author}/${name}`, {
authenticated: true,
});
return { id, code };
}
async function updateValCode({ id, code }: { id: string; code: string }) {
console.log({ id, code });
await api(`/v1/vals/${id}/versions`, {
authenticated: true,
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
code,
}),
});
}
async function sendEmail({ subject, text }: { subject: string; text: string }) {
await email({
subject,
text,
});
}
const prompt = `
You are an helpful code assistant that has a deep understanding of javascript.
You help the user maintaining his vals, whose are runnable snippets of javascript.
Vals are often referred by slug.
Ex: @pomdtr/blog or pomdtr/blog or pomdtr.blog all refers to the val named blog created by the user pomdtr.
Here are some instructions on how to write vals:
- You can use in JavaScript, TypeScript, JSX, or TSX
- Utilize Deno-style import syntax for NPM packages, prefixed with "npm:"
- Prefer native fetch for HTTP requests instead of axios
- Prefer double quotes to single quotes
You can import from the standard library for email, blob storage,
sqlite, which have these types:
import { execute, batch } from "https://esm.town/v/std/sqlite";
await execute('SELECT 1 from table');
await batch(['SELECT 1 from table');
import { get, set } from "https://esm.town/v/std/blob";
const blob_contents: Response = await get('key_name');
// second argument has the type BodyInit
await set('key', 'test');
import { email } from "https://esm.town/v/std/email";
await email({
to: 'address@email.com',
text: 'message text',
subject: 'subject line',
});
Do not communicate with the user directly.
Instead, use the provided functions to accomplish your task on the behalf of the user.
For example, if you need to modify the code of a val, use the updateValCode function.
`;
type Tool = {
type: "function";
function: {
function: (...args: any[]) => any;
description: string;
parse: (...args: any[]) => any;
required: string[];
parameters: {
type: "object";
properties: Record<string, {
type: "string";
}>;
};
};
};
export function askAI(content: string) {
const client = new OpenAI();
const runner = client.beta.chat.completions.runTools({
model: "gpt-3.5-turbo",
messages: [{ role: "system", content: prompt }, {
role: "user",
content,
}],
tools: [{
type: "function",
function: {
function: getValByAlias,