Public
Script
Readme

Breadboard As a Service

A simple board runner that allows hosting board as a service.

The service can be public (accessible to everyone) or private (locked behind a service key).

See chatbot example for a public service and generatevoice for private.

Make sure to add all the necessary secrets to your environment variables.

The resulting HTTP endpoint can be then used as a Service URL for Core Kit's service node.

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
import {
type HarnessRunResult,
run,
} from "npm:@google-labs/breadboard/harness";
import {
asRuntimeKit,
createLoader,
inflateData,
inspect,
} from "npm:@google-labs/breadboard@latest";
import { fromManifest, KitManifest } from "npm:@google-labs/breadboard/kits";
import { getDataStore } from "npm:@breadboard-ai/data-store";
import AgentKit from "npm:@google-labs/agent-kit/agent.kit.json" with {
type: "json",
};
import Core from "npm:@google-labs/core-kit";
import GeminiKit from "npm:@google-labs/gemini-kit";
import JSONKit from "npm:@google-labs/json-kit";
import TemplateKit from "npm:@google-labs/template-kit";
export type ServiceOptions = {
/**
* If true, the service will ask for a key to access. The value of the key
* is stored as BB_SERVICE_KEY environment variable.
* Default: false.
*/
private?: boolean;
/**
* A way to specify inputs, overriding the default inputs provided by `invoke`.
* Default: undefined.
*/
inputs?: Record<string, any>;
};
const kits = [
asRuntimeKit(Core),
asRuntimeKit(JSONKit),
asRuntimeKit(TemplateKit),
asRuntimeKit(GeminiKit),
fromManifest(AgentKit as KitManifest),
];
import {
addKeyInput,
boardFromDescriberResult,
type NodeDescriberResult,
} from "https://esm.town/v/dglazkov/servicefactory";
const FRONTEND_MODULE = "https://esm.town/v/dglazkov/bbaasfe";
const formatRunError = (e: unknown) => {
if (typeof e === "string") {
return e;
}
if (e instanceof Error) {
return e.message;
}
// Presume it's an ErrorObject.
const error = (e as { error: unknown }).error;
if (typeof error === "string") {
return error;
}
if (error instanceof Error) {
return error.message;
}
return JSON.stringify(error);
};
export const describe = async (url: string, options?: ServiceOptions) => {
const loader = createLoader();
const graph = await loader.load(url, { base: new URL(import.meta.url) });
const inspector = inspect(graph);
const { title, description, metadata } = graph;
const describeResult = await inspector.describe() as NodeDescriberResult;
if (options?.private) {
addKeyInput(describeResult);
}
return {
...describeResult,
title,
description,
metadata,
} as NodeDescriberResult;
};
export const invoke = async (
url: string,
inputs: Record<string, any>,
options?: ServiceOptions,
) => {
if (options?.private) {
if (!inputs.$key || inputs.$key != Deno.env.get("BB_SERVICE_KEY")) {
return {
$error: "Must provide an API key to access the service.",
};
}
delete inputs.$key;
}
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!
August 26, 2024