Skip to content

Commit

Permalink
[board-server] Teach Board Server about sandboxed runModule.
Browse files Browse the repository at this point in the history
- **Export Node and Web sandboxes separately.**
- **Move `addSandboxedRunModule` to Core Kit.**
- **Teach board server about sandboxed runModule.**
- **docs(changeset): Teach Board Server about sandboxed runModule.**

Progress on #3572
  • Loading branch information
dglazkov authored Nov 4, 2024
1 parent 1131130 commit d42ab17
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 26 deletions.
8 changes: 8 additions & 0 deletions .changeset/strange-deers-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@breadboard-ai/board-server": minor
"@breadboard-ai/visual-editor": patch
"@google-labs/core-kit": patch
"@breadboard-ai/jsandbox": patch
---

Teach Board Server about sandboxed runModule.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion packages/board-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"../agent-kit:build:tsc",
"../palm-kit:build:tsc",
"../core-kit:build:tsc",
"../google-drive-kit:build:tsc",
"../jsandbox:build:tsc",
"../json-kit:build:tsc",
"../template-kit:build:tsc",
"../node-nursery-web:build:tsc",
Expand Down Expand Up @@ -83,7 +85,9 @@
"../core-kit:build",
"../json-kit:build",
"../template-kit:build",
"../node-nursery-web:build"
"../node-nursery-web:build",
"../google-drive-kit:build",
"../jsandbox:build"
],
"clean": "if-file-deleted"
},
Expand Down Expand Up @@ -187,6 +191,8 @@
"dependencies": {
"@breadboard-ai/connection-client": "0.1.0",
"@breadboard-ai/data-store": "0.2.5",
"@breadboard-ai/google-drive-kit": "0.3.0",
"@breadboard-ai/jsandbox": "0.1.0",
"@breadboard-ai/types": "0.2.0",
"@google-cloud/firestore": "^7.10.0",
"@google-cloud/secret-manager": "^5.6.0",
Expand Down
22 changes: 13 additions & 9 deletions packages/board-server/src/server/boards/utils/create-kits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { NodeSandbox } from "@breadboard-ai/jsandbox/node";
import AgentKit from "@google-labs/agent-kit/agent.kit.json" with { type: "json" };
import GoogleDriveKit from "@breadboard-ai/google-drive-kit/google-drive.kit.json" with { type: "json" };
import {
asRuntimeKit,
type Kit,
type KitManifest,
} from "@google-labs/breadboard";
import { fromManifest } from "@google-labs/breadboard/kits";
import Core from "@google-labs/core-kit";
import Core, { addSandboxedRunModule } from "@google-labs/core-kit";
import GeminiKit from "@google-labs/gemini-kit";
import JSONKit from "@google-labs/json-kit";
import TemplateKit from "@google-labs/template-kit";

export const createKits = (overrides: Kit[] = []) => [
...overrides,
asRuntimeKit(Core),
asRuntimeKit(JSONKit),
asRuntimeKit(TemplateKit),
asRuntimeKit(GeminiKit),
fromManifest(AgentKit as KitManifest),
];
export const createKits = (overrides: Kit[] = []) =>
addSandboxedRunModule(new NodeSandbox(), [
...overrides,
asRuntimeKit(Core),
asRuntimeKit(JSONKit),
asRuntimeKit(TemplateKit),
asRuntimeKit(GeminiKit),
fromManifest(AgentKit as KitManifest),
fromManifest(GoogleDriveKit as KitManifest),
]);
2 changes: 2 additions & 0 deletions packages/core-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export { unnest, unnestNode } from "./nodes/unnest.js";
import { storeDataNode } from "./nodes/storeData.js";
import { retrieveDataNode } from "./nodes/retrieveData.js";

export { addSandboxedRunModule } from "./sandboxed-run-module.js";

const metadata = {
title: "Core Kit",
description: "A Breadboard kit that enables composition and reuse of boards",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {
import type {
InputValues,
NodeDescriberContext,
NodeDescriberResult,
NodeHandlerContext,
NodeHandlerObject,
Schema,
type Kit,
Kit,
} from "@google-labs/breadboard";

import {
Capability,
type Capability,
type Sandbox,
SandboxedModule,
WebSandbox,
} from "@breadboard-ai/jsandbox";
import wasm from "/sandbox.wasm?url";

export { addSandboxedRunModule };

Expand Down Expand Up @@ -57,9 +56,7 @@ function getHandler(handlerName: string, context: NodeHandlerContext) {
}) as Capability;
}

function addSandboxedRunModule(kits: Kit[]): Kit[] {
const sandbox = new WebSandbox(new URL(wasm, window.location.href));

function addSandboxedRunModule(sandbox: Sandbox, kits: Kit[]): Kit[] {
const existingRunModule = findHandler("runModule", kits);
const originalDescriber =
(existingRunModule &&
Expand Down
4 changes: 3 additions & 1 deletion packages/jsandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "./dist/src/index.js",
"exports": {
".": "./dist/src/index.js",
"./sandbox.wasm": "./sandbox.wasm"
"./sandbox.wasm": "./sandbox.wasm",
"./web": "./dist/src/web-exports.js",
"./node": "./dist/src/node-exports.js"
},
"types": "dist/src/index.d.ts",
"type": "module",
Expand Down
7 changes: 7 additions & 0 deletions packages/jsandbox/src/node-exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

export { NodeSandbox } from "./node.js";
6 changes: 3 additions & 3 deletions packages/jsandbox/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import {
File as WasiFile,
} from "@bjorn3/browser_wasi_shim";
import { readFile } from "fs/promises";
import { join } from "path";
import { fileURLToPath } from "node:url";
import factory from "./factory.js";
import { ModuleSpec, Sandbox, UUID } from "./types.js";

export { NodeSandbox };

async function loadRuntime(): Promise<Buffer> {
const path = join(import.meta.dirname, "..", "..", "sandbox.wasm");
return readFile(path);
const file = import.meta.resolve("@breadboard-ai/jsandbox/sandbox.wasm");
return readFile(fileURLToPath(file));
}

class NodeSandbox implements Sandbox {
Expand Down
7 changes: 7 additions & 0 deletions packages/jsandbox/src/web-exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

export { WebSandbox } from "./web.js";
18 changes: 14 additions & 4 deletions packages/visual-editor/src/runtime/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ import {
getBoardServers,
} from "@breadboard-ai/board-server-management";
import { TokenVendor } from "@breadboard-ai/connection-client";
import { addSandboxedRunModule } from "../sandbox";
import { addSandboxedRunModule } from "@google-labs/core-kit";

import wasm from "/sandbox.wasm?url";
import { WebSandbox } from "@breadboard-ai/jsandbox/web";

function withRunModule(kits: Kit[]): Kit[] {
return addSandboxedRunModule(
new WebSandbox(new URL(wasm, window.location.href)),
kits
);
}

export class Board extends EventTarget {
#tabs = new Map<TabId, Tab>();
Expand Down Expand Up @@ -318,7 +328,7 @@ export class Board extends EventTarget {
const id = globalThis.crypto.randomUUID();
this.#tabs.set(id, {
id,
kits: addSandboxedRunModule(this.kits),
kits: withRunModule(this.kits),
name: descriptor.title ?? "Untitled board",
graph: descriptor,
subGraphId: null,
Expand Down Expand Up @@ -357,7 +367,7 @@ export class Board extends EventTarget {
const id = globalThis.crypto.randomUUID();
this.#tabs.set(id, {
id,
kits: addSandboxedRunModule(this.kits),
kits: withRunModule(this.kits),
name: descriptor.title ?? "Untitled board",
graph: descriptor,
subGraphId: null,
Expand Down Expand Up @@ -442,7 +452,7 @@ export class Board extends EventTarget {
const id = globalThis.crypto.randomUUID();
this.#tabs.set(id, {
id,
kits: addSandboxedRunModule(kits),
kits: withRunModule(kits),
name: graph.title ?? "Untitled board",
graph,
subGraphId: null,
Expand Down

0 comments on commit d42ab17

Please sign in to comment.