Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Starter template for Substrate demos

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
import promptHeader from "https://esm.town/v/substrate/promptHeader";
import wrapper from "https://esm.town/v/substrate/substrateBadgeMiddleware";
import { ComputeJSON, ComputeText, GenerateImage, sb, Substrate } from "npm:substrate";
async function handler(req: Request): Promise<Response> {
const searchParams = new URL(req.url).searchParams;
const { prompt = "modernism" } = Object.fromEntries(searchParams);
// Substrate graph
const substrate = new Substrate({ apiKey: Deno.env.get("SUBSTRATE_API_KEY") });
const startup_name = new ComputeText({
prompt:
`Give me a unique startup name inside <name></name> tags related to ${prompt}. Example: <name>Hyphae</name><description>A startup that ... </description>`,
});
const haiku = new ComputeText({
prompt: sb.concat(
"Write a haiku about the startup names: ",
startup_name.future.text,
),
});
const stream = await substrate.stream(haiku);
async function* content() {
yield new TextEncoder().encode(promptHeader());
for await (const event of stream) {
if (event.object === "node.result") {
if (event.data.text) {
yield new TextEncoder().encode(
`<div style="background-color: #f0f0f0; border-radius: 8px; padding: 4px; margin: 8px;">${event.data.text}</div>`,
);
}
}
}
}
const responseStream = ReadableStream.from(content());
return new Response(responseStream, { headers: { "Content-Type": "text/html" } });
}
export default wrapper(handler, import.meta.url);
liam-template.web.val.run
July 10, 2024