From b0a875b7c94ed1b7066194de81705c84cc6f9f9e Mon Sep 17 00:00:00 2001 From: Ben Follington <5009316+bfollington@users.noreply.github.com> Date: Thu, 20 Jun 2024 14:09:53 -0700 Subject: [PATCH] Configurable eval mode for code blocks Customize SES, WASM or CC on a per block basis --- .../src/components/modules/com-module-code.ts | 25 ++++ .../packages/lookslike-prototype/src/data.ts | 124 ++++++++++-------- .../packages/lookslike-prototype/src/eval.ts | 6 +- .../packages/lookslike-prototype/src/graph.ts | 2 +- 4 files changed, 97 insertions(+), 60 deletions(-) diff --git a/typescript/packages/lookslike-prototype/src/components/modules/com-module-code.ts b/typescript/packages/lookslike-prototype/src/components/modules/com-module-code.ts index 4127750c2..2da5cd13f 100644 --- a/typescript/packages/lookslike-prototype/src/components/modules/com-module-code.ts +++ b/typescript/packages/lookslike-prototype/src/components/modules/com-module-code.ts @@ -1,6 +1,11 @@ import { LitElement, html, css } from "lit-element"; import { customElement, property } from "lit/decorators.js"; import { RecipeNode } from "../../data.js"; +import { + WASM_SANDBOX, + SES_SANDBOX, + CONFIDENTIAL_COMPUTE_SANDBOX +} from "@commontools/runtime"; const styles = css``; @@ -16,6 +21,9 @@ export class ComModuleCode extends LitElement { return html`
loading...
`; } + // HACK: force SES by default + this.node.evalMode = this.node.evalMode || SES_SANDBOX; + const codeChanged = (ev: CustomEvent) => { if (!this.node) return; @@ -38,12 +46,29 @@ export class ComModuleCode extends LitElement { this.dispatchEvent(event); }; + const onChangeEvalMode = (ev: Event) => { + const select = ev.target as HTMLSelectElement; + this.node.evalMode = select.value as any; + this.requestUpdate(); + }; + return html` + `; } } diff --git a/typescript/packages/lookslike-prototype/src/data.ts b/typescript/packages/lookslike-prototype/src/data.ts index 0866205bb..04c7db1b8 100644 --- a/typescript/packages/lookslike-prototype/src/data.ts +++ b/typescript/packages/lookslike-prototype/src/data.ts @@ -1,95 +1,105 @@ -export type Recipe = RecipeNode[] -export type NodePath = [string, string] +import { + CONFIDENTIAL_COMPUTE_SANDBOX, + SES_SANDBOX, + WASM_SANDBOX +} from "@commontools/runtime"; + +export type Recipe = RecipeNode[]; +export type NodePath = [string, string]; export type InputMap = { [port: string]: NodePath }; +export type EvalMode = + | typeof WASM_SANDBOX + | typeof SES_SANDBOX + | typeof CONFIDENTIAL_COMPUTE_SANDBOX; + export type RecipeNode = { - id: string, // an actual unique ID - messages?: Message[], // could be empty if a user hand-authored the node etc. - contentType: string, - outputType: any, - in: InputMap, - body: string | object + id: string; // an actual unique ID + messages?: Message[]; // could be empty if a user hand-authored the node etc. + contentType: string; + outputType: any; + in: InputMap; + body: string | object; + evalMode?: EvalMode; }; export type Message = { - role: 'user' | 'assistant', - content: string, -} + role: "user" | "assistant"; + content: string; +}; export const emptyGraph: Recipe = []; export const todoAppMockup: Recipe = [ { - "id": "todos", - "messages": [ + id: "todos", + messages: [ { - "role": "user", - "content": "get my todos" + role: "user", + content: "get my todos" }, { - "role": "assistant", - "content": "..." + role: "assistant", + content: "..." } ], - "contentType": "text/javascript", - "in": {}, - "outputType": { - "$id": "https://common.tools/stream.schema.json", - "type": { - "$id": "https://common.tools/todos.json" + contentType: "text/javascript", + in: {}, + outputType: { + $id: "https://common.tools/stream.schema.json", + type: { + $id: "https://common.tools/todos.json" } }, - "body": "return system.get('todos')" + body: "return system.get('todos')" }, { - "id": "todoUi", - "messages": [ + id: "todoUi", + messages: [ { - "role": "user", - "content": "render todo" + role: "user", + content: "render todo" }, { - "role": "assistant", - "content": "..." + role: "assistant", + content: "..." } ], - "contentType": "application/json+vnd.common.ui", - "in": { - "todos": [".", "todos"] + contentType: "application/json+vnd.common.ui", + in: { + todos: [".", "todos"] }, - "outputType": { - "$id": "https://common.tools/ui.schema.json" + outputType: { + $id: "https://common.tools/ui.schema.json" }, - "body": { - "tag": "ul", - "props": { - "className": "todo" + body: { + tag: "ul", + props: { + className: "todo" }, - "children": { - "type": "repeat", - "binding": "todos", - "template": { - "tag": "li", - "props": {}, - "children": [ + children: { + type: "repeat", + binding: "todos", + template: { + tag: "li", + props: {}, + children: [ { - "tag": "input", - "props": { - "type": "checkbox", - "checked": { type: 'boolean', binding: 'checked' } + tag: "input", + props: { + type: "checkbox", + checked: { type: "boolean", binding: "checked" } } }, { - "tag": "span", - "props": { - "className": "todo-label" + tag: "span", + props: { + className: "todo-label" }, - "children": [ - { type: 'string', binding: 'label' } - ] + children: [{ type: "string", binding: "label" }] } ] } } } } -] +]; diff --git a/typescript/packages/lookslike-prototype/src/eval.ts b/typescript/packages/lookslike-prototype/src/eval.ts index 0c509b06a..39e198ab7 100644 --- a/typescript/packages/lookslike-prototype/src/eval.ts +++ b/typescript/packages/lookslike-prototype/src/eval.ts @@ -5,6 +5,7 @@ import { WASM_SANDBOX, SES_SANDBOX } from "@commontools/runtime"; +import { EvalMode } from "./data.js"; export function prepare(code: string) { const func = new Function( @@ -23,7 +24,8 @@ export function serializationBoundary(obj: any) { export async function run( id: string, src: string, - inputs: { [key: string]: any } + inputs: { [key: string]: any }, + evalMode: EvalMode = "ses" ) { console.group("eval(" + id + ")"); const rt = new Runtime(); @@ -33,7 +35,7 @@ export async function run( const module = await rt.eval( id, - SES_SANDBOX, + evalMode, "text/javascript", code(src), new Input(storage, Object.keys(inputs)) diff --git a/typescript/packages/lookslike-prototype/src/graph.ts b/typescript/packages/lookslike-prototype/src/graph.ts index 375f9a631..1d6483baa 100644 --- a/typescript/packages/lookslike-prototype/src/graph.ts +++ b/typescript/packages/lookslike-prototype/src/graph.ts @@ -152,7 +152,7 @@ async function executeNode( if (typeof node.body !== "string") { throw new Error("Expected a string"); } - const result = await run(node.id, node.body, inputs); + const result = await run(node.id, node.body, inputs, node.evalMode); outputs[node.id].send(result); break; }