1import { PuppeteerDeno } from "https://deno.land/x/puppeteer@16.2.0/src/deno/Puppeteer.ts";
2// 1st stable version is v13
3const BROWSERBASE_API_KEY = Deno.env.get("BROWSERBASE_API_KEY");
4const TIMEOUT = 30000; // 30 seconds timeout
5
16 let browser;
17 try {
18 if (!BROWSERBASE_API_KEY) {
19 throw new Error("BROWSERBASE_API_KEY is not set");
20 }
21
22 const puppeteer = new PuppeteerDeno({ productName: "chrome" });
23 const wsEndpoint = `wss://connect.browserbase.com?apiKey=${BROWSERBASE_API_KEY}&enableProxy=true`;
24
25 console.log("Attempting to connect to Browserbase...");
80 let errorMessage = error.message;
81 if (error.message.includes("400 Bad Request")) {
82 errorMessage = "Failed to connect to Browserbase. Please check your API key and account status.";
83 }
84 return new Response(JSON.stringify({ error: errorMessage }), {
86 if (!inputs.$key || inputs.$key != Deno.env.get("BB_SERVICE_KEY")) {
87 return {
88 $error: "Must provide an API key to access the service.",
89 };
90 }
1# OpenAI Proxy
2
3This OpenAI API proxy injects Val Town's API keys. For usage documentation, check out https://www.val.town/v/std/openai
1import { parseBearerString } from "https://esm.town/v/andreterron/parseBearerString";
2import { API_URL } from "https://esm.town/v/std/API_URL?v=5";
3import { OpenAIUsage } from "https://esm.town/v/std/OpenAIUsage";
4import { RateLimit } from "npm:@rlimit/http";
28 const authHeader = req.headers.get("Proxy-Authorization") || req.headers.get("Authorization");
29 const token = authHeader ? parseBearerString(authHeader) : undefined;
30 const meRes = await fetch(`${API_URL}/v1/me`, { headers: { Authorization: `Bearer ${token}` } });
31 if (!meRes.ok) {
32 return new Response("Unauthorized", { status: 401 });
42
43 // Proxy the request
44 const url = new URL("." + pathname, "https://api.openai.com");
45 url.search = search;
46
47 const headers = new Headers(req.headers);
48 headers.set("Host", url.hostname);
49 headers.set("Authorization", `Bearer ${Deno.env.get("OPENAI_API_KEY")}`);
50 headers.set("OpenAI-Organization", Deno.env.get("OPENAI_API_ORG"));
51
52 const modifiedBody = await limitFreeModel(req, user);
58 * Indicates that the input or output port represents base structured
59 * datatype containing multi-part content of a message, generated by an LLM.
60 * See [Content](https://ai.google.dev/api/rest/v1beta/Content) for details
61 * on the datatype.
62 */
86 if (!inputs.$key || inputs.$key != Deno.env.get("BB_SERVICE_KEY")) {
87 return {
88 $error: "Must provide an API key to access the service.",
89 };
90 }
131
132const boardToEndpoint = (board: string) => {
133 return board.replace(/\.json$/, ".api/run");
134};
135
4
5For now, requires you to be running a board server. This harness actually acts
6as a proxy to the board server [run API endpoint](https://breadboard-ai.github.io/breadboard/docs/reference/board-run-api-endpoint/),
7and puts a nice (well, somewhat nice) frontend on top of it.
8
11
12The script will look for the `BB_LIVE_KEY` in your Val Town environment, which
13must contain your board server API key.
14
15To use, create an HTTP val, then import the `proxy` function from this script and call it like this:
4
5For now, requires you to be running a board server. This harness actually acts
6as a proxy to the board server [run API endpoint](https://breadboard-ai.github.io/breadboard/docs/reference/board-run-api-endpoint/),
7and puts a nice (well, somewhat nice) frontend on top of it.
8
11
12The script will look for the `BB_LIVE_KEY` in your Val Town environment, which
13must contain your board server API key.
14
15To use, create an HTTP val, then import the `proxy` function from this script and call it like this:
68) {
69 return async (req: Request) => {
70 const { api } = await import("https://esm.town/v/pomdtr/api");
71 const { deleteCookie, getCookies, setCookie } = await import("jsr:@std/http/cookie");
72