Skip to content

Commit 6374967

Browse files
authored
Configurable eval mode for code blocks (#76)
Customize SES, WASM or CC on a per block basis
1 parent cc89bf4 commit 6374967

File tree

4 files changed

+97
-60
lines changed

4 files changed

+97
-60
lines changed

typescript/packages/lookslike-prototype/src/components/modules/com-module-code.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { LitElement, html, css } from "lit-element";
22
import { customElement, property } from "lit/decorators.js";
33
import { RecipeNode } from "../../data.js";
4+
import {
5+
WASM_SANDBOX,
6+
SES_SANDBOX,
7+
CONFIDENTIAL_COMPUTE_SANDBOX
8+
} from "@commontools/runtime";
49

510
const styles = css``;
611

@@ -16,6 +21,9 @@ export class ComModuleCode extends LitElement {
1621
return html`<pre>loading...</pre>`;
1722
}
1823

24+
// HACK: force SES by default
25+
this.node.evalMode = this.node.evalMode || SES_SANDBOX;
26+
1927
const codeChanged = (ev: CustomEvent) => {
2028
if (!this.node) return;
2129

@@ -38,12 +46,29 @@ export class ComModuleCode extends LitElement {
3846
this.dispatchEvent(event);
3947
};
4048

49+
const onChangeEvalMode = (ev: Event) => {
50+
const select = ev.target as HTMLSelectElement;
51+
this.node.evalMode = select.value as any;
52+
this.requestUpdate();
53+
};
54+
4155
return html`
4256
<com-code .code=${this.node.body} @updated=${codeChanged}></com-code>
4357
<com-data
4458
.data=${JSON.stringify(this.value, null, 2)}
4559
@updated=${dataChanged}
4660
></com-data>
61+
<select
62+
name="evalMode"
63+
@change=${onChangeEvalMode}
64+
.value=${this.node.evalMode}
65+
>
66+
<option value=${SES_SANDBOX}>SES</option>
67+
<option value=${WASM_SANDBOX}>WASM</option>
68+
<option value=${CONFIDENTIAL_COMPUTE_SANDBOX}>
69+
CONFIDENTIAL COMPUTE
70+
</option>
71+
</select>
4772
`;
4873
}
4974
}
Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,105 @@
1-
export type Recipe = RecipeNode[]
2-
export type NodePath = [string, string]
1+
import {
2+
CONFIDENTIAL_COMPUTE_SANDBOX,
3+
SES_SANDBOX,
4+
WASM_SANDBOX
5+
} from "@commontools/runtime";
6+
7+
export type Recipe = RecipeNode[];
8+
export type NodePath = [string, string];
39
export type InputMap = { [port: string]: NodePath };
410

11+
export type EvalMode =
12+
| typeof WASM_SANDBOX
13+
| typeof SES_SANDBOX
14+
| typeof CONFIDENTIAL_COMPUTE_SANDBOX;
15+
516
export type RecipeNode = {
6-
id: string, // an actual unique ID
7-
messages?: Message[], // could be empty if a user hand-authored the node etc.
8-
contentType: string,
9-
outputType: any,
10-
in: InputMap,
11-
body: string | object
17+
id: string; // an actual unique ID
18+
messages?: Message[]; // could be empty if a user hand-authored the node etc.
19+
contentType: string;
20+
outputType: any;
21+
in: InputMap;
22+
body: string | object;
23+
evalMode?: EvalMode;
1224
};
1325

1426
export type Message = {
15-
role: 'user' | 'assistant',
16-
content: string,
17-
}
27+
role: "user" | "assistant";
28+
content: string;
29+
};
1830

1931
export const emptyGraph: Recipe = [];
2032
export const todoAppMockup: Recipe = [
2133
{
22-
"id": "todos",
23-
"messages": [
34+
id: "todos",
35+
messages: [
2436
{
25-
"role": "user",
26-
"content": "get my todos"
37+
role: "user",
38+
content: "get my todos"
2739
},
2840
{
29-
"role": "assistant",
30-
"content": "..."
41+
role: "assistant",
42+
content: "..."
3143
}
3244
],
33-
"contentType": "text/javascript",
34-
"in": {},
35-
"outputType": {
36-
"$id": "https://common.tools/stream.schema.json",
37-
"type": {
38-
"$id": "https://common.tools/todos.json"
45+
contentType: "text/javascript",
46+
in: {},
47+
outputType: {
48+
$id: "https://common.tools/stream.schema.json",
49+
type: {
50+
$id: "https://common.tools/todos.json"
3951
}
4052
},
41-
"body": "return system.get('todos')"
53+
body: "return system.get('todos')"
4254
},
4355
{
44-
"id": "todoUi",
45-
"messages": [
56+
id: "todoUi",
57+
messages: [
4658
{
47-
"role": "user",
48-
"content": "render todo"
59+
role: "user",
60+
content: "render todo"
4961
},
5062
{
51-
"role": "assistant",
52-
"content": "..."
63+
role: "assistant",
64+
content: "..."
5365
}
5466
],
55-
"contentType": "application/json+vnd.common.ui",
56-
"in": {
57-
"todos": [".", "todos"]
67+
contentType: "application/json+vnd.common.ui",
68+
in: {
69+
todos: [".", "todos"]
5870
},
59-
"outputType": {
60-
"$id": "https://common.tools/ui.schema.json"
71+
outputType: {
72+
$id: "https://common.tools/ui.schema.json"
6173
},
62-
"body": {
63-
"tag": "ul",
64-
"props": {
65-
"className": "todo"
74+
body: {
75+
tag: "ul",
76+
props: {
77+
className: "todo"
6678
},
67-
"children": {
68-
"type": "repeat",
69-
"binding": "todos",
70-
"template": {
71-
"tag": "li",
72-
"props": {},
73-
"children": [
79+
children: {
80+
type: "repeat",
81+
binding: "todos",
82+
template: {
83+
tag: "li",
84+
props: {},
85+
children: [
7486
{
75-
"tag": "input",
76-
"props": {
77-
"type": "checkbox",
78-
"checked": { type: 'boolean', binding: 'checked' }
87+
tag: "input",
88+
props: {
89+
type: "checkbox",
90+
checked: { type: "boolean", binding: "checked" }
7991
}
8092
},
8193
{
82-
"tag": "span",
83-
"props": {
84-
"className": "todo-label"
94+
tag: "span",
95+
props: {
96+
className: "todo-label"
8597
},
86-
"children": [
87-
{ type: 'string', binding: 'label' }
88-
]
98+
children: [{ type: "string", binding: "label" }]
8999
}
90100
]
91101
}
92102
}
93103
}
94104
}
95-
]
105+
];

typescript/packages/lookslike-prototype/src/eval.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
WASM_SANDBOX,
66
SES_SANDBOX
77
} from "@commontools/runtime";
8+
import { EvalMode } from "./data.js";
89

910
export function prepare(code: string) {
1011
const func = new Function(
@@ -23,7 +24,8 @@ export function serializationBoundary(obj: any) {
2324
export async function run(
2425
id: string,
2526
src: string,
26-
inputs: { [key: string]: any }
27+
inputs: { [key: string]: any },
28+
evalMode: EvalMode = "ses"
2729
) {
2830
console.group("eval(" + id + ")");
2931
const rt = new Runtime();
@@ -33,7 +35,7 @@ export async function run(
3335

3436
const module = await rt.eval(
3537
id,
36-
SES_SANDBOX,
38+
evalMode,
3739
"text/javascript",
3840
code(src),
3941
new Input(storage, Object.keys(inputs))

typescript/packages/lookslike-prototype/src/graph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async function executeNode(
152152
if (typeof node.body !== "string") {
153153
throw new Error("Expected a string");
154154
}
155-
const result = await run(node.id, node.body, inputs);
155+
const result = await run(node.id, node.body, inputs, node.evalMode);
156156
outputs[node.id].send(result);
157157
break;
158158
}

0 commit comments

Comments
 (0)