@dxos/compute 0.8.4-main.1da679c → 0.8.4-main.21d9917
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/{chunk-XQYPCRZP.mjs → chunk-KGBGT4NT.mjs} +151 -185
- package/dist/lib/browser/chunk-KGBGT4NT.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +32 -20
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node-esm/{chunk-2XWWSGAU.mjs → chunk-2MATJWLB.mjs} +151 -185
- package/dist/lib/node-esm/chunk-2MATJWLB.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +1 -1
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +32 -20
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/compute-graph-registry.d.ts +8 -3
- package/dist/types/src/compute-graph-registry.d.ts.map +1 -1
- package/dist/types/src/compute-graph-registry.test.d.ts +2 -0
- package/dist/types/src/compute-graph-registry.test.d.ts.map +1 -0
- package/dist/types/src/compute-graph.d.ts +4 -1
- package/dist/types/src/compute-graph.d.ts.map +1 -1
- package/dist/types/src/functions/async-function.d.ts +11 -9
- package/dist/types/src/functions/async-function.d.ts.map +1 -1
- package/dist/types/src/functions/edge-function.d.ts.map +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +1 -1
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-runtime.d.ts +10 -0
- package/dist/types/src/testing/test-runtime.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +26 -23
- package/src/compute-graph-registry.test.ts +158 -0
- package/src/compute-graph-registry.ts +17 -8
- package/src/compute-graph.test.ts +29 -10
- package/src/compute-graph.ts +19 -13
- package/src/functions/async-function.ts +13 -12
- package/src/functions/edge-function.ts +30 -28
- package/src/index.ts +0 -27
- package/src/testing/index.ts +1 -0
- package/src/testing/test-builder.ts +8 -2
- package/src/testing/test-runtime.ts +34 -0
- package/dist/lib/browser/chunk-XQYPCRZP.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-2XWWSGAU.mjs.map +0 -7
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/compute",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.21d9917",
|
|
4
4
|
"description": "Low-level compute API",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
13
|
"sideEffects": true,
|
|
@@ -35,35 +39,34 @@
|
|
|
35
39
|
"src"
|
|
36
40
|
],
|
|
37
41
|
"dependencies": {
|
|
38
|
-
"
|
|
39
|
-
"effect": "3.17.7",
|
|
42
|
+
"effect": "3.19.11",
|
|
40
43
|
"lodash.defaultsdeep": "^4.6.1",
|
|
41
|
-
"@dxos/async": "0.8.4-main.
|
|
42
|
-
"@dxos/
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/echo": "0.8.4-main.
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/
|
|
47
|
-
"@dxos/
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/log": "0.8.4-main.
|
|
51
|
-
"@dxos/
|
|
52
|
-
"@dxos/protocols": "0.8.4-main.
|
|
53
|
-
"@dxos/
|
|
54
|
-
"@dxos/
|
|
55
|
-
"@dxos/
|
|
56
|
-
"@dxos/
|
|
44
|
+
"@dxos/async": "0.8.4-main.21d9917",
|
|
45
|
+
"@dxos/client": "0.8.4-main.21d9917",
|
|
46
|
+
"@dxos/context": "0.8.4-main.21d9917",
|
|
47
|
+
"@dxos/echo": "0.8.4-main.21d9917",
|
|
48
|
+
"@dxos/debug": "0.8.4-main.21d9917",
|
|
49
|
+
"@dxos/functions": "0.8.4-main.21d9917",
|
|
50
|
+
"@dxos/functions-runtime": "0.8.4-main.21d9917",
|
|
51
|
+
"@dxos/ai": "0.8.4-main.21d9917",
|
|
52
|
+
"@dxos/invariant": "0.8.4-main.21d9917",
|
|
53
|
+
"@dxos/log": "0.8.4-main.21d9917",
|
|
54
|
+
"@dxos/keys": "0.8.4-main.21d9917",
|
|
55
|
+
"@dxos/protocols": "0.8.4-main.21d9917",
|
|
56
|
+
"@dxos/plugin-automation": "0.8.4-main.21d9917",
|
|
57
|
+
"@dxos/schema": "0.8.4-main.21d9917",
|
|
58
|
+
"@dxos/vendor-hyperformula": "0.8.4-main.21d9917",
|
|
59
|
+
"@dxos/util": "0.8.4-main.21d9917"
|
|
57
60
|
},
|
|
58
61
|
"devDependencies": {
|
|
59
62
|
"@tldraw/indices": "^2.0.0-alpha.14",
|
|
60
63
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
61
|
-
"@dxos/echo
|
|
62
|
-
"@dxos/
|
|
63
|
-
"@dxos/
|
|
64
|
+
"@dxos/echo": "0.8.4-main.21d9917",
|
|
65
|
+
"@dxos/random": "0.8.4-main.21d9917",
|
|
66
|
+
"@dxos/echo-db": "0.8.4-main.21d9917"
|
|
64
67
|
},
|
|
65
68
|
"peerDependencies": {
|
|
66
|
-
"effect": "
|
|
69
|
+
"effect": "3.19.11"
|
|
67
70
|
},
|
|
68
71
|
"publishConfig": {
|
|
69
72
|
"access": "public"
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { Trigger } from '@dxos/async';
|
|
9
|
+
import { Obj } from '@dxos/echo';
|
|
10
|
+
import { Function, FunctionDefinition } from '@dxos/functions';
|
|
11
|
+
|
|
12
|
+
import { ComputeGraphRegistry, defaultPlugins } from './compute-graph-registry';
|
|
13
|
+
import { TestBuilder, createMockedComputeRuntimeProvider } from './testing';
|
|
14
|
+
|
|
15
|
+
describe('ComputeGraphRegistry', () => {
|
|
16
|
+
const add = FunctionDefinition.make({
|
|
17
|
+
key: 'add',
|
|
18
|
+
name: 'add',
|
|
19
|
+
description: 'Adds two numbers',
|
|
20
|
+
inputSchema: Schema.Struct({ a: Schema.Number, b: Schema.Number }),
|
|
21
|
+
outputSchema: Schema.Number,
|
|
22
|
+
handler: ({ data }) => data.a + data.b,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('invokes user function through compute graph', async () => {
|
|
26
|
+
const computeRuntime = createMockedComputeRuntimeProvider({
|
|
27
|
+
functions: [add],
|
|
28
|
+
});
|
|
29
|
+
const testBuilder = new TestBuilder({
|
|
30
|
+
types: [Function.Function],
|
|
31
|
+
computeRuntime,
|
|
32
|
+
});
|
|
33
|
+
await testBuilder.open();
|
|
34
|
+
onTestFinished(async () => {
|
|
35
|
+
await testBuilder.close();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const space = await testBuilder.client.spaces.create();
|
|
39
|
+
|
|
40
|
+
// Register Edge function plugin and runtime in the registry.
|
|
41
|
+
const registry = new ComputeGraphRegistry({
|
|
42
|
+
plugins: defaultPlugins,
|
|
43
|
+
computeRuntime,
|
|
44
|
+
});
|
|
45
|
+
await registry.open();
|
|
46
|
+
onTestFinished(async () => {
|
|
47
|
+
await registry.close();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const graph = registry.createGraph(space);
|
|
51
|
+
await graph.open();
|
|
52
|
+
onTestFinished(async () => {
|
|
53
|
+
await graph.close();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Create a function object in ECHO with binding that maps to DX calls.
|
|
57
|
+
const functionObj = FunctionDefinition.serialize(add);
|
|
58
|
+
Obj.change(functionObj, (f) => {
|
|
59
|
+
f.binding = 'ADD';
|
|
60
|
+
});
|
|
61
|
+
space.db.add(functionObj);
|
|
62
|
+
|
|
63
|
+
const node = await graph.getOrCreateNode('sheet').open();
|
|
64
|
+
|
|
65
|
+
// Wait for value update after async invocation completes.
|
|
66
|
+
const trigger = new Trigger<number>();
|
|
67
|
+
node.update.on(() => {
|
|
68
|
+
const value = node.getValue({ row: 0, col: 0 });
|
|
69
|
+
if (typeof value === 'number') {
|
|
70
|
+
trigger.wake(value);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Set formula using binding; graph will map to DX("ADD", ...)
|
|
75
|
+
node.setValue({ row: 0, col: 0 }, '=ADD(2, 3)');
|
|
76
|
+
|
|
77
|
+
const result = await trigger.wait();
|
|
78
|
+
expect(result).to.equal(5);
|
|
79
|
+
|
|
80
|
+
// Sanity check: mapping to fully qualified id works as well.
|
|
81
|
+
const bindingId = graph.mapFunctionBindingToId('ADD(2, 3)');
|
|
82
|
+
expect(bindingId.startsWith(`${Obj.getDXN(functionObj).toString()}`)).to.be.true;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('adding a function binding updates autocomplete and enables execution', async () => {
|
|
86
|
+
const computeRuntime = createMockedComputeRuntimeProvider({
|
|
87
|
+
functions: [add],
|
|
88
|
+
});
|
|
89
|
+
const testBuilder = new TestBuilder({
|
|
90
|
+
types: [Function.Function],
|
|
91
|
+
computeRuntime,
|
|
92
|
+
});
|
|
93
|
+
await testBuilder.open();
|
|
94
|
+
onTestFinished(async () => {
|
|
95
|
+
await testBuilder.close();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const space = await testBuilder.client.spaces.create();
|
|
99
|
+
|
|
100
|
+
const registry = new ComputeGraphRegistry({
|
|
101
|
+
plugins: defaultPlugins,
|
|
102
|
+
computeRuntime,
|
|
103
|
+
});
|
|
104
|
+
await registry.open();
|
|
105
|
+
onTestFinished(async () => {
|
|
106
|
+
await registry.close();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const graph = registry.createGraph(space);
|
|
110
|
+
await graph.open();
|
|
111
|
+
onTestFinished(async () => {
|
|
112
|
+
await graph.close();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Initially there should be no echo functions.
|
|
116
|
+
expect(graph.getFunctions({ standard: false, echo: true }).length).to.equal(0);
|
|
117
|
+
|
|
118
|
+
// Subscribe for function updates.
|
|
119
|
+
const functionsUpdated = new Trigger<void>();
|
|
120
|
+
const unsubscribe = graph.update.on((e) => {
|
|
121
|
+
if (e.type === 'functionsUpdated') {
|
|
122
|
+
functionsUpdated.wake();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
onTestFinished(unsubscribe);
|
|
126
|
+
|
|
127
|
+
// Add a bound function to the space; graph should pick it up.
|
|
128
|
+
const functionObj = FunctionDefinition.serialize(add);
|
|
129
|
+
Obj.change(functionObj, (f) => {
|
|
130
|
+
f.binding = 'ADD';
|
|
131
|
+
});
|
|
132
|
+
space.db.add(functionObj);
|
|
133
|
+
|
|
134
|
+
await functionsUpdated.wait();
|
|
135
|
+
|
|
136
|
+
// Autocomplete should now include the binding.
|
|
137
|
+
const echoFunctions = graph.getFunctions({ standard: false, echo: true }).map((fn) => fn.name);
|
|
138
|
+
expect(echoFunctions).toContain('ADD');
|
|
139
|
+
|
|
140
|
+
// And the binding should execute when used in a formula.
|
|
141
|
+
const node = await graph.getOrCreateNode('sheet').open();
|
|
142
|
+
onTestFinished(async () => {
|
|
143
|
+
await node.close();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const valueSettled = new Trigger<number>();
|
|
147
|
+
node.update.on(() => {
|
|
148
|
+
const value = node.getValue({ row: 0, col: 0 });
|
|
149
|
+
if (typeof value === 'number') {
|
|
150
|
+
valueSettled.wake(value);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
node.setValue({ row: 0, col: 0 }, '=ADD(2, 3)');
|
|
155
|
+
const result = await valueSettled.wait();
|
|
156
|
+
expect(result).to.equal(5);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import type * as ManagedRuntime from 'effect/ManagedRuntime';
|
|
5
6
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
6
7
|
|
|
7
8
|
import { type Space, type SpaceId } from '@dxos/client/echo';
|
|
8
9
|
import { Resource } from '@dxos/context';
|
|
10
|
+
import { type FunctionInvocationService } from '@dxos/functions';
|
|
9
11
|
import { invariant } from '@dxos/invariant';
|
|
10
12
|
import { log } from '@dxos/log';
|
|
11
13
|
import type { ConfigParams, FunctionPluginDefinition, FunctionTranslationsPackage } from '@dxos/vendor-hyperformula';
|
|
@@ -19,15 +21,16 @@ export type ComputeGraphPlugin = {
|
|
|
19
21
|
translations: FunctionTranslationsPackage;
|
|
20
22
|
};
|
|
21
23
|
|
|
24
|
+
export type FunctionsRuntimeProvider = {
|
|
25
|
+
getRuntime(spaceId: SpaceId): ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>;
|
|
26
|
+
};
|
|
27
|
+
|
|
22
28
|
export type ComputeGraphOptions = {
|
|
23
29
|
plugins?: ComputeGraphPlugin[];
|
|
30
|
+
computeRuntime: FunctionsRuntimeProvider;
|
|
24
31
|
} & Partial<FunctionContextOptions> &
|
|
25
32
|
Partial<ConfigParams>;
|
|
26
33
|
|
|
27
|
-
export const defaultOptions: ComputeGraphOptions = {
|
|
28
|
-
licenseKey: 'gpl-v3',
|
|
29
|
-
};
|
|
30
|
-
|
|
31
34
|
export const defaultPlugins: ComputeGraphPlugin[] = [
|
|
32
35
|
{
|
|
33
36
|
plugin: EdgeFunctionPlugin,
|
|
@@ -35,23 +38,26 @@ export const defaultPlugins: ComputeGraphPlugin[] = [
|
|
|
35
38
|
},
|
|
36
39
|
];
|
|
37
40
|
|
|
41
|
+
export const defaultOptions: Partial<ComputeGraphOptions> = {
|
|
42
|
+
licenseKey: 'gpl-v3',
|
|
43
|
+
};
|
|
44
|
+
|
|
38
45
|
/**
|
|
39
46
|
* Manages a collection of ComputeGraph instances for each space.
|
|
40
47
|
*
|
|
41
48
|
* [ComputePlugin] => [ComputeGraphRegistry] => [ComputeGraph(Space)] => [ComputeNode(Object)]
|
|
42
49
|
*
|
|
43
50
|
* NOTE: The ComputeGraphRegistry manages the hierarchy of resources via its root Context.
|
|
44
|
-
* NOTE: The package.json file defines the packaged #hyperformula module.
|
|
45
51
|
*/
|
|
46
52
|
// TODO(burdon): Move graph into separate plugin; isolate HF deps.
|
|
47
53
|
export class ComputeGraphRegistry extends Resource {
|
|
48
54
|
private readonly _graphs = new Map<SpaceId, ComputeGraph>();
|
|
49
|
-
|
|
50
55
|
private readonly _options: ComputeGraphOptions;
|
|
51
56
|
|
|
52
|
-
constructor(options: ComputeGraphOptions
|
|
57
|
+
constructor(options: ComputeGraphOptions) {
|
|
53
58
|
super();
|
|
54
59
|
this._options = defaultsDeep({}, options, defaultOptions);
|
|
60
|
+
this._options.plugins = this._options.plugins ?? defaultPlugins;
|
|
55
61
|
this._options.plugins?.forEach(({ plugin, translations }) => {
|
|
56
62
|
HyperFormula.registerFunctionPlugin(plugin, translations);
|
|
57
63
|
});
|
|
@@ -74,7 +80,10 @@ export class ComputeGraphRegistry extends Resource {
|
|
|
74
80
|
createGraph(space: Space): ComputeGraph {
|
|
75
81
|
invariant(!this._graphs.has(space.id), `ComputeGraph already exists for space: ${space.id}`);
|
|
76
82
|
const hf = HyperFormula.buildEmpty(this._options);
|
|
77
|
-
|
|
83
|
+
invariant(this._options.computeRuntime, 'ComputeRuntime is required');
|
|
84
|
+
const runtime = this._options.computeRuntime.getRuntime(space.id);
|
|
85
|
+
|
|
86
|
+
const graph = new ComputeGraph(hf, runtime, space, this._options);
|
|
78
87
|
this._graphs.set(space.id, graph);
|
|
79
88
|
return graph;
|
|
80
89
|
}
|
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
7
|
import { Trigger } from '@dxos/async';
|
|
8
|
-
import { fullyQualifiedId } from '@dxos/client/echo';
|
|
9
8
|
import { Obj } from '@dxos/echo';
|
|
10
|
-
import {
|
|
9
|
+
import { Function } from '@dxos/functions';
|
|
11
10
|
import { type CellValue } from '@dxos/vendor-hyperformula';
|
|
12
11
|
import { DetailedCellError } from '@dxos/vendor-hyperformula';
|
|
13
12
|
|
|
@@ -16,7 +15,7 @@ import { TestBuilder } from './testing';
|
|
|
16
15
|
describe('ComputeGraph', () => {
|
|
17
16
|
let testBuilder: TestBuilder;
|
|
18
17
|
beforeEach(async () => {
|
|
19
|
-
testBuilder = new TestBuilder({ types: [
|
|
18
|
+
testBuilder = new TestBuilder({ types: [Function.Function] });
|
|
20
19
|
await testBuilder.open();
|
|
21
20
|
});
|
|
22
21
|
afterEach(async () => {
|
|
@@ -31,13 +30,13 @@ describe('ComputeGraph', () => {
|
|
|
31
30
|
// Create script.
|
|
32
31
|
const trigger = new Trigger();
|
|
33
32
|
graph.update.once(() => trigger.wake());
|
|
34
|
-
const functionObject = space.db.add(
|
|
33
|
+
const functionObject = space.db.add(Function.make({ name: 'test', version: '0.0.1', binding: 'TEST' }));
|
|
35
34
|
await trigger.wait();
|
|
36
35
|
const functions = graph.getFunctions({ echo: true });
|
|
37
36
|
expect(functions).to.toHaveLength(1);
|
|
38
37
|
|
|
39
38
|
const id = graph.mapFunctionBindingToId('TEST()');
|
|
40
|
-
expect(id).to.eq(`${
|
|
39
|
+
expect(id).to.eq(`${Obj.getDXN(functionObject).toString()}()`);
|
|
41
40
|
});
|
|
42
41
|
|
|
43
42
|
test('cross-node references', async () => {
|
|
@@ -52,22 +51,38 @@ describe('ComputeGraph', () => {
|
|
|
52
51
|
{
|
|
53
52
|
node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, [[100, 200, 300, '=SUM(A1:C1)']]);
|
|
54
53
|
node2.graph.hf.setCellContents({ sheet: node2.sheetId, row: 0, col: 0 }, "='node-1'!D1");
|
|
55
|
-
const value1 = node1.graph.hf.getCellValue({
|
|
56
|
-
|
|
54
|
+
const value1 = node1.graph.hf.getCellValue({
|
|
55
|
+
sheet: node1.sheetId,
|
|
56
|
+
col: 3,
|
|
57
|
+
row: 0,
|
|
58
|
+
});
|
|
59
|
+
const value2 = node2.graph.hf.getCellValue({
|
|
60
|
+
sheet: node2.sheetId,
|
|
61
|
+
col: 0,
|
|
62
|
+
row: 0,
|
|
63
|
+
});
|
|
57
64
|
expect(value1).to.eq(value2);
|
|
58
65
|
}
|
|
59
66
|
|
|
60
67
|
// Get updated event.
|
|
61
68
|
const trigger = new Trigger<CellValue>();
|
|
62
69
|
node2.update.on(({ change }) => {
|
|
63
|
-
const value = node2.graph.hf.getCellValue({
|
|
70
|
+
const value = node2.graph.hf.getCellValue({
|
|
71
|
+
sheet: node2.sheetId,
|
|
72
|
+
col: 0,
|
|
73
|
+
row: 0,
|
|
74
|
+
});
|
|
64
75
|
expect(value).to.eq(change?.newValue);
|
|
65
76
|
trigger.wake(value);
|
|
66
77
|
});
|
|
67
78
|
|
|
68
79
|
{
|
|
69
80
|
node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, 400);
|
|
70
|
-
const value1 = node1.graph.hf.getCellValue({
|
|
81
|
+
const value1 = node1.graph.hf.getCellValue({
|
|
82
|
+
sheet: node1.sheetId,
|
|
83
|
+
col: 3,
|
|
84
|
+
row: 0,
|
|
85
|
+
});
|
|
71
86
|
const value2 = await trigger.wait();
|
|
72
87
|
expect(value1).to.eq(value2);
|
|
73
88
|
}
|
|
@@ -81,7 +96,11 @@ describe('ComputeGraph', () => {
|
|
|
81
96
|
|
|
82
97
|
const node1 = await graph.getOrCreateNode('node-1').open();
|
|
83
98
|
node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, "='node-2'!A1");
|
|
84
|
-
const value1 = node1.graph.hf.getCellValue({
|
|
99
|
+
const value1 = node1.graph.hf.getCellValue({
|
|
100
|
+
sheet: node1.sheetId,
|
|
101
|
+
col: 0,
|
|
102
|
+
row: 0,
|
|
103
|
+
});
|
|
85
104
|
expect(value1).to.be.instanceof(DetailedCellError);
|
|
86
105
|
expect((value1 as DetailedCellError).type).to.eq('REF');
|
|
87
106
|
});
|
package/src/compute-graph.ts
CHANGED
|
@@ -2,17 +2,19 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import type * as ManagedRuntime from 'effect/ManagedRuntime';
|
|
6
|
+
|
|
5
7
|
import { Event } from '@dxos/async';
|
|
6
|
-
import { Filter, type Space
|
|
8
|
+
import { Filter, type Space } from '@dxos/client/echo';
|
|
7
9
|
import { FQ_ID_LENGTH } from '@dxos/client/echo';
|
|
8
10
|
import { Resource } from '@dxos/context';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
+
import { Obj } from '@dxos/echo';
|
|
12
|
+
import { Function, type FunctionInvocationService } from '@dxos/functions';
|
|
11
13
|
import { invariant } from '@dxos/invariant';
|
|
12
14
|
import { PublicKey } from '@dxos/keys';
|
|
13
15
|
import { log } from '@dxos/log';
|
|
14
16
|
import { isNonNullable } from '@dxos/util';
|
|
15
|
-
import {
|
|
17
|
+
import type { Listeners } from '@dxos/vendor-hyperformula';
|
|
16
18
|
import { ExportedCellChange, type HyperFormula } from '@dxos/vendor-hyperformula';
|
|
17
19
|
|
|
18
20
|
import { ComputeNode } from './compute-node';
|
|
@@ -58,7 +60,7 @@ export class ComputeGraph extends Resource {
|
|
|
58
60
|
private readonly _nodes = new Map<number, ComputeNode>();
|
|
59
61
|
|
|
60
62
|
// Cached function objects.
|
|
61
|
-
private _remoteFunctions:
|
|
63
|
+
private _remoteFunctions: Function.Function[] = [];
|
|
62
64
|
|
|
63
65
|
public readonly update = new Event<{ type: ComputeGraphEvent }>();
|
|
64
66
|
|
|
@@ -67,12 +69,13 @@ export class ComputeGraph extends Resource {
|
|
|
67
69
|
|
|
68
70
|
constructor(
|
|
69
71
|
private readonly _hf: HyperFormula,
|
|
72
|
+
private readonly _runtime: ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>,
|
|
70
73
|
private readonly _space?: Space,
|
|
71
74
|
private readonly _options?: Partial<FunctionContextOptions>,
|
|
72
75
|
) {
|
|
73
76
|
super();
|
|
74
77
|
|
|
75
|
-
this.context = new FunctionContext(this._hf, this._space, {
|
|
78
|
+
this.context = new FunctionContext(this._hf, this._runtime, this._space, {
|
|
76
79
|
...this._options,
|
|
77
80
|
onUpdate: (update) => {
|
|
78
81
|
this._options?.onUpdate?.(update);
|
|
@@ -103,7 +106,10 @@ export class ComputeGraph extends Resource {
|
|
|
103
106
|
}
|
|
104
107
|
|
|
105
108
|
getFunctions(
|
|
106
|
-
{ standard, echo }: { standard?: boolean; echo?: boolean } = {
|
|
109
|
+
{ standard, echo }: { standard?: boolean; echo?: boolean } = {
|
|
110
|
+
standard: true,
|
|
111
|
+
echo: true,
|
|
112
|
+
},
|
|
107
113
|
): FunctionDefinition[] {
|
|
108
114
|
return [
|
|
109
115
|
...(standard
|
|
@@ -163,7 +169,7 @@ export class ComputeGraph extends Resource {
|
|
|
163
169
|
|
|
164
170
|
for (const obj of objects) {
|
|
165
171
|
if (obj.name === name) {
|
|
166
|
-
const type = getTypename(obj)!;
|
|
172
|
+
const type = Obj.getTypename(obj)!;
|
|
167
173
|
// NOTE: Names must be single quoted.
|
|
168
174
|
return `'${createSheetName({ type, id: obj.id })}'!`;
|
|
169
175
|
}
|
|
@@ -203,7 +209,7 @@ export class ComputeGraph extends Resource {
|
|
|
203
209
|
|
|
204
210
|
const fn = this._remoteFunctions.find((fn) => fn.binding === binding);
|
|
205
211
|
if (fn) {
|
|
206
|
-
const id =
|
|
212
|
+
const id = Obj.getDXN(fn).toString();
|
|
207
213
|
return `${id}(${args})`;
|
|
208
214
|
} else {
|
|
209
215
|
return match;
|
|
@@ -222,7 +228,7 @@ export class ComputeGraph extends Resource {
|
|
|
222
228
|
return match;
|
|
223
229
|
}
|
|
224
230
|
|
|
225
|
-
const fn = this._remoteFunctions.find((fn) =>
|
|
231
|
+
const fn = this._remoteFunctions.find((fn) => Obj.getDXN(fn).toString() === id);
|
|
226
232
|
if (fn?.binding) {
|
|
227
233
|
return `${fn.binding}(${args})`;
|
|
228
234
|
} else {
|
|
@@ -240,9 +246,9 @@ export class ComputeGraph extends Resource {
|
|
|
240
246
|
protected override async _open(): Promise<void> {
|
|
241
247
|
if (this._space) {
|
|
242
248
|
// Subscribe to remote function definitions.
|
|
243
|
-
const query = this._space.db.query(Filter.type(
|
|
244
|
-
const unsubscribe = query.subscribe((
|
|
245
|
-
this._remoteFunctions =
|
|
249
|
+
const query = this._space.db.query(Filter.type(Function.Function));
|
|
250
|
+
const unsubscribe = query.subscribe(() => {
|
|
251
|
+
this._remoteFunctions = query.results.filter((fn) => fn.binding);
|
|
246
252
|
this.update.emit({ type: 'functionsUpdated' });
|
|
247
253
|
});
|
|
248
254
|
|
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import type * as ManagedRuntime from 'effect/ManagedRuntime';
|
|
5
6
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
6
7
|
|
|
7
8
|
import { type CleanupFn, debounce } from '@dxos/async';
|
|
8
|
-
import {
|
|
9
|
+
import type { Space } from '@dxos/client/echo';
|
|
10
|
+
import { type FunctionInvocationService } from '@dxos/functions';
|
|
9
11
|
import { log } from '@dxos/log';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
12
|
+
import type { RawInterpreterValue, SimpleCellAddress } from '@dxos/vendor-hyperformula';
|
|
13
|
+
import type { InterpreterState } from '@dxos/vendor-hyperformula';
|
|
14
|
+
import type { InterpreterValue } from '@dxos/vendor-hyperformula';
|
|
15
|
+
import type { ProcedureAst } from '@dxos/vendor-hyperformula';
|
|
14
16
|
import { CellError, EmptyValue, ErrorType, FunctionPlugin, type HyperFormula } from '@dxos/vendor-hyperformula';
|
|
15
17
|
|
|
16
18
|
// TODO(burdon): Create API gateways:
|
|
@@ -33,14 +35,12 @@ export type FunctionOptions = {
|
|
|
33
35
|
export type FunctionContextOptions = {
|
|
34
36
|
defaultTtl: number;
|
|
35
37
|
debounceDelay: number;
|
|
36
|
-
remoteFunctionUrl: string;
|
|
37
38
|
onUpdate?: (update: FunctionUpdateEvent) => void;
|
|
38
39
|
};
|
|
39
40
|
|
|
40
|
-
export const defaultFunctionContextOptions: FunctionContextOptions = {
|
|
41
|
+
export const defaultFunctionContextOptions: Pick<FunctionContextOptions, 'defaultTtl' | 'debounceDelay'> = {
|
|
41
42
|
defaultTtl: 5_000,
|
|
42
43
|
debounceDelay: 200,
|
|
43
|
-
remoteFunctionUrl: 'https://edge.dxos.workers.dev/functions', // TODO(burdon): Config.
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -77,6 +77,7 @@ export class FunctionContext {
|
|
|
77
77
|
|
|
78
78
|
constructor(
|
|
79
79
|
private readonly _hf: HyperFormula,
|
|
80
|
+
private readonly _runtime: ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>,
|
|
80
81
|
private readonly _space: Space | undefined,
|
|
81
82
|
_options?: Partial<FunctionContextOptions>,
|
|
82
83
|
) {
|
|
@@ -93,14 +94,14 @@ export class FunctionContext {
|
|
|
93
94
|
return this._space;
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
get remoteFunctionUrl() {
|
|
97
|
-
return this._options.remoteFunctionUrl;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
97
|
get info() {
|
|
101
98
|
return { cache: this._cache.size, invocations: this._invocations };
|
|
102
99
|
}
|
|
103
100
|
|
|
101
|
+
get runtime() {
|
|
102
|
+
return this._runtime;
|
|
103
|
+
}
|
|
104
|
+
|
|
104
105
|
flush(): void {
|
|
105
106
|
this._cache.clear();
|
|
106
107
|
this._invocations = {};
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
7
7
|
|
|
8
|
-
import { Filter
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
8
|
+
import { Filter } from '@dxos/client/echo';
|
|
9
|
+
import { JsonSchema, Obj } from '@dxos/echo';
|
|
10
|
+
import { Function, FunctionInvocationService, TracingService } from '@dxos/functions';
|
|
11
|
+
import { FunctionDefinition } from '@dxos/functions';
|
|
11
12
|
import { log } from '@dxos/log';
|
|
12
13
|
import { isNonNullable } from '@dxos/util';
|
|
13
14
|
import { type ProcedureAst } from '@dxos/vendor-hyperformula';
|
|
@@ -34,49 +35,50 @@ export class EdgeFunctionPlugin extends AsyncFunctionPlugin {
|
|
|
34
35
|
return new CellError(ErrorType.REF, 'Missing space');
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
const {
|
|
38
|
-
objects: [fn],
|
|
39
|
-
} = await space.db.query(Filter.type(FunctionType, { binding })).run();
|
|
38
|
+
const [fn] = await space.db.query(Filter.type(Function.Function, { binding })).run();
|
|
40
39
|
if (!fn) {
|
|
41
40
|
log.info('Function not found', { binding });
|
|
42
41
|
return new CellError(ErrorType.REF, 'Function not found');
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
if (subscribe) {
|
|
46
|
-
const unsubscribe =
|
|
45
|
+
const unsubscribe = Obj.subscribe(fn, () => {
|
|
47
46
|
log('function changed', { fn });
|
|
48
|
-
const _ = fn?.version;
|
|
49
|
-
|
|
50
47
|
// TODO(wittjosiah): `ttl` should be 0 to force a recalculation when a new version is deployed.
|
|
51
48
|
// This needs a ttl to prevent a binding change from causing the function not to be found.
|
|
52
|
-
this.runAsyncFunction(ast, state, handler(false), {
|
|
49
|
+
this.runAsyncFunction(ast, state, handler(false), {
|
|
50
|
+
ttl: FUNCTION_TTL,
|
|
51
|
+
});
|
|
53
52
|
});
|
|
54
53
|
|
|
55
54
|
this.context.createSubscription(ast.procedureName, unsubscribe);
|
|
56
55
|
}
|
|
57
56
|
|
|
58
|
-
const
|
|
57
|
+
const runtime = this.context.runtime;
|
|
58
|
+
const functionDef = FunctionDefinition.deserialize(fn);
|
|
59
|
+
// If input schema exists, construct an object from args using the schema props order, otherwise pass { args }.
|
|
60
|
+
let input: any;
|
|
59
61
|
if (fn.inputSchema) {
|
|
60
|
-
const schema = toEffectSchema(fn.inputSchema);
|
|
61
|
-
SchemaAST.getPropertySignatures(schema.ast)
|
|
62
|
-
|
|
62
|
+
const schema = JsonSchema.toEffectSchema(fn.inputSchema);
|
|
63
|
+
const props = SchemaAST.getPropertySignatures(schema.ast);
|
|
64
|
+
input = {} as any;
|
|
65
|
+
props.forEach(({ name }, index) => {
|
|
66
|
+
input[name.toString()] = args[index];
|
|
63
67
|
});
|
|
64
68
|
} else {
|
|
65
|
-
|
|
69
|
+
input = { args: args.filter(isNonNullable) };
|
|
66
70
|
}
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const result = await response.text();
|
|
74
|
-
log('function executed', { result });
|
|
75
|
-
|
|
76
|
-
return result;
|
|
71
|
+
const result = runtime.runPromise(
|
|
72
|
+
Effect.gen(function* () {
|
|
73
|
+
return yield* FunctionInvocationService.invokeFunction(functionDef, input);
|
|
74
|
+
}).pipe(Effect.provide(TracingService.layerNoop)),
|
|
75
|
+
);
|
|
76
|
+
return result as any;
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
return this.runAsyncFunction(ast, state, handler(true), {
|
|
79
|
+
return this.runAsyncFunction(ast, state, handler(true), {
|
|
80
|
+
ttl: FUNCTION_TTL,
|
|
81
|
+
});
|
|
80
82
|
}
|
|
81
83
|
}
|
|
82
84
|
|