diff --git a/.changeset/strange-deers-raise.md b/.changeset/strange-deers-raise.md new file mode 100644 index 00000000000..72f61afe106 --- /dev/null +++ b/.changeset/strange-deers-raise.md @@ -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. diff --git a/package-lock.json b/package-lock.json index b27be26a9b4..e4e8d2222a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24842,6 +24842,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", diff --git a/packages/board-server/package.json b/packages/board-server/package.json index d959569508f..23297191367 100644 --- a/packages/board-server/package.json +++ b/packages/board-server/package.json @@ -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", @@ -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" }, @@ -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", diff --git a/packages/board-server/src/server/boards/utils/create-kits.ts b/packages/board-server/src/server/boards/utils/create-kits.ts index 600bd4c3a93..9f2e9379fc1 100644 --- a/packages/board-server/src/server/boards/utils/create-kits.ts +++ b/packages/board-server/src/server/boards/utils/create-kits.ts @@ -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), + ]); diff --git a/packages/core-kit/src/index.ts b/packages/core-kit/src/index.ts index 79c005423e9..fa34dfebffc 100644 --- a/packages/core-kit/src/index.ts +++ b/packages/core-kit/src/index.ts @@ -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", diff --git a/packages/visual-editor/src/sandbox/index.ts b/packages/core-kit/src/sandboxed-run-module.ts similarity index 95% rename from packages/visual-editor/src/sandbox/index.ts rename to packages/core-kit/src/sandboxed-run-module.ts index c26af31eeae..5dade2c9f5e 100644 --- a/packages/visual-editor/src/sandbox/index.ts +++ b/packages/core-kit/src/sandboxed-run-module.ts @@ -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 }; @@ -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 && diff --git a/packages/jsandbox/package.json b/packages/jsandbox/package.json index 1e3b3d244f7..71eb31efdc8 100644 --- a/packages/jsandbox/package.json +++ b/packages/jsandbox/package.json @@ -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", diff --git a/packages/jsandbox/src/node-exports.ts b/packages/jsandbox/src/node-exports.ts new file mode 100644 index 00000000000..83e9bd05fd2 --- /dev/null +++ b/packages/jsandbox/src/node-exports.ts @@ -0,0 +1,7 @@ +/** + * @license + * Copyright 2024 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +export { NodeSandbox } from "./node.js"; diff --git a/packages/jsandbox/src/node.ts b/packages/jsandbox/src/node.ts index 505361473d1..32a05fabcc5 100644 --- a/packages/jsandbox/src/node.ts +++ b/packages/jsandbox/src/node.ts @@ -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 { - 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 { diff --git a/packages/jsandbox/src/web-exports.ts b/packages/jsandbox/src/web-exports.ts new file mode 100644 index 00000000000..b4bf714a15f --- /dev/null +++ b/packages/jsandbox/src/web-exports.ts @@ -0,0 +1,7 @@ +/** + * @license + * Copyright 2024 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +export { WebSandbox } from "./web.js"; diff --git a/packages/visual-editor/src/runtime/board.ts b/packages/visual-editor/src/runtime/board.ts index b558b3a9216..6371d2ec470 100644 --- a/packages/visual-editor/src/runtime/board.ts +++ b/packages/visual-editor/src/runtime/board.ts @@ -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(); @@ -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, @@ -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, @@ -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,