Public
HTTP
janpaul123-valtownchatgpt2.web.val.run
July 15, 2024
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource https://esm.sh/react */
import getValsContextWindow from "https://esm.town/v/janpaul123/getValsContextWindow";
import { updateValByName } from "https://esm.town/v/nbbaier/updateValByName?v=14";
import { passwordAuth } from "https://esm.town/v/pomdtr/password_auth?v=84";
import { verifyToken } from "https://esm.town/v/pomdtr/verifyToken";
import ValTown from "npm:@valtown/sdk";
import { Hono } from "npm:hono@3";
import OpenAI from "npm:openai";
import { renderToString } from "npm:react-dom/server";
const vt = new ValTown({ bearerToken: Deno.env.get("valtown") });
const username = "janpaul123";
const app = new Hono();
const jsxResponse = (jsx) => {
return new Response(renderToString(jsx), { headers: { "Content-Type": "text/html" } });
};
function renderForm(
valName: string,
prompt: string,
type: string,
code: string,
expandedPrompts: any = [],
iterations: number = 10,
) {
return (
<form action="/" method="post" className="generateForm">
<input type="hidden" name="code" value={code} />
<input type="hidden" name="expandedPrompts" value={JSON.stringify(expandedPrompts)} />
<div>
Val Name: <input name="valName" defaultValue={valName}></input>
</div>
<div>
Iterations: <input name="iterations" defaultValue={iterations}></input>
</div>
<div>Prompt:</div>
<textarea
name="prompt"
defaultValue={prompt}
style={{ width: "100%", height: "100px" }}
>
</textarea>
<div>
<select name="type" defaultValue={type}>
<option value="new">New</option>
<option value="update">Update</option>
<option value="iterate">Iterate</option>
<option value="iterate-inline">Iterate inline</option>
</select>
</div>
<div>
<button type="submit">Generate</button>
</div>
</form>
);
}
app.get("/", async (c) => {
return jsxResponse(
renderForm("valwriter_output", "Create a script val that returns the current weather in Brooklyn.", "new", ""),
);
});
app.post("/save", async (c) => {
const body = await c.req.parseBody();
const valName: any = body.valName;
const code: any = body.code;
try {
await updateValByName({
token: Deno.env.get("valtown"),
code,
name: valName,
});
} catch (err) {
return jsxResponse(<span>Error: {err}</span>);
}
return jsxResponse(<span>✅ Saved</span>);
});
app.post("/", async (c) => {
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();
const textEncoder = new TextEncoder();
function write(text) {
writer.write(textEncoder.encode(text));
}
const body = await c.req.parseBody();
const prompt: any = body.prompt;
const valName: any = body.valName;
const iterations: any = body.iterations;
const type: any = body.type;
const previousExpandedPrompts: any = JSON.parse(body.expandedPrompts as any);
let existingValCode;
if (type === "update") {
const val = await vt.alias.username.valName.retrieve(username, valName);
existingValCode = val.code;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 15, 2024