@dxos/compute 0.8.4-main.406dc2a → 0.8.4-main.422d1c7879
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-C7KLV2IU.mjs → neutral/chunk-JRV42MW3.mjs} +120 -163
- package/dist/lib/neutral/chunk-JRV42MW3.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/index.mjs +1 -1
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/{browser → neutral}/testing/index.mjs +37 -24
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/compute-graph-registry.d.ts +2 -2
- package/dist/types/src/compute-graph-registry.d.ts.map +1 -1
- package/dist/types/src/compute-graph.d.ts +7 -7
- package/dist/types/src/compute-graph.d.ts.map +1 -1
- package/dist/types/src/functions/async-function.d.ts +3 -3
- 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/test-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-runtime.d.ts +2 -2
- package/dist/types/src/testing/test-runtime.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +29 -34
- package/src/compute-graph-registry.test.ts +51 -22
- package/src/compute-graph-registry.ts +2 -2
- package/src/compute-graph.test.ts +61 -10
- package/src/compute-graph.ts +37 -33
- package/src/functions/async-function.ts +2 -2
- package/src/functions/edge-function.ts +13 -19
- package/src/index.ts +0 -27
- package/src/testing/test-builder.ts +0 -1
- package/src/testing/test-runtime.ts +30 -15
- package/dist/lib/browser/chunk-C7KLV2IU.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-K37OSZ5O.mjs +0 -3146
- package/dist/lib/node-esm/chunk-K37OSZ5O.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -56
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -207
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- /package/dist/lib/{browser → neutral}/index.mjs.map +0 -0
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.422d1c7879",
|
|
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,
|
|
@@ -12,58 +16,49 @@
|
|
|
12
16
|
".": {
|
|
13
17
|
"source": "./src/index.ts",
|
|
14
18
|
"types": "./dist/types/src/index.d.ts",
|
|
15
|
-
"
|
|
16
|
-
"node": "./dist/lib/node-esm/index.mjs"
|
|
19
|
+
"default": "./dist/lib/neutral/index.mjs"
|
|
17
20
|
},
|
|
18
21
|
"./testing": {
|
|
19
22
|
"source": "./src/testing/index.ts",
|
|
20
23
|
"types": "./dist/types/src/testing/index.d.ts",
|
|
21
|
-
"
|
|
22
|
-
"node": "./dist/lib/node-esm/testing/index.mjs"
|
|
24
|
+
"default": "./dist/lib/neutral/testing/index.mjs"
|
|
23
25
|
}
|
|
24
26
|
},
|
|
25
27
|
"types": "dist/types/src/index.d.ts",
|
|
26
|
-
"typesVersions": {
|
|
27
|
-
"*": {
|
|
28
|
-
"testing": [
|
|
29
|
-
"dist/types/src/testing/index.d.ts"
|
|
30
|
-
]
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
28
|
"files": [
|
|
34
29
|
"dist",
|
|
35
30
|
"src"
|
|
36
31
|
],
|
|
37
32
|
"dependencies": {
|
|
38
|
-
"
|
|
39
|
-
"effect": "3.18.3",
|
|
33
|
+
"effect": "3.20.0",
|
|
40
34
|
"lodash.defaultsdeep": "^4.6.1",
|
|
41
|
-
"@dxos/
|
|
42
|
-
"@dxos/
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/context": "0.8.4-main.
|
|
46
|
-
"@dxos/
|
|
47
|
-
"@dxos/
|
|
48
|
-
"@dxos/invariant": "0.8.4-main.
|
|
49
|
-
"@dxos/keys": "0.8.4-main.
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/
|
|
52
|
-
"@dxos/
|
|
53
|
-
"@dxos/
|
|
54
|
-
"@dxos/util": "0.8.4-main.
|
|
55
|
-
"@dxos/
|
|
56
|
-
"@dxos/
|
|
35
|
+
"@dxos/client": "0.8.4-main.422d1c7879",
|
|
36
|
+
"@dxos/ai": "0.8.4-main.422d1c7879",
|
|
37
|
+
"@dxos/async": "0.8.4-main.422d1c7879",
|
|
38
|
+
"@dxos/debug": "0.8.4-main.422d1c7879",
|
|
39
|
+
"@dxos/context": "0.8.4-main.422d1c7879",
|
|
40
|
+
"@dxos/functions": "0.8.4-main.422d1c7879",
|
|
41
|
+
"@dxos/echo": "0.8.4-main.422d1c7879",
|
|
42
|
+
"@dxos/invariant": "0.8.4-main.422d1c7879",
|
|
43
|
+
"@dxos/keys": "0.8.4-main.422d1c7879",
|
|
44
|
+
"@dxos/functions-runtime": "0.8.4-main.422d1c7879",
|
|
45
|
+
"@dxos/log": "0.8.4-main.422d1c7879",
|
|
46
|
+
"@dxos/operation": "0.8.4-main.422d1c7879",
|
|
47
|
+
"@dxos/protocols": "0.8.4-main.422d1c7879",
|
|
48
|
+
"@dxos/util": "0.8.4-main.422d1c7879",
|
|
49
|
+
"@dxos/schema": "0.8.4-main.422d1c7879",
|
|
50
|
+
"@dxos/plugin-automation": "0.8.4-main.422d1c7879",
|
|
51
|
+
"@dxos/vendor-hyperformula": "0.8.4-main.422d1c7879"
|
|
57
52
|
},
|
|
58
53
|
"devDependencies": {
|
|
59
54
|
"@tldraw/indices": "^2.0.0-alpha.14",
|
|
60
55
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
61
|
-
"@dxos/echo": "0.8.4-main.
|
|
62
|
-
"@dxos/echo-db": "0.8.4-main.
|
|
63
|
-
"@dxos/random": "0.8.4-main.
|
|
56
|
+
"@dxos/echo": "0.8.4-main.422d1c7879",
|
|
57
|
+
"@dxos/echo-db": "0.8.4-main.422d1c7879",
|
|
58
|
+
"@dxos/random": "0.8.4-main.422d1c7879"
|
|
64
59
|
},
|
|
65
60
|
"peerDependencies": {
|
|
66
|
-
"effect": "
|
|
61
|
+
"effect": "3.20.0"
|
|
67
62
|
},
|
|
68
63
|
"publishConfig": {
|
|
69
64
|
"access": "public"
|
|
@@ -2,29 +2,45 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
5
6
|
import * as Schema from 'effect/Schema';
|
|
6
7
|
import { describe, expect, onTestFinished, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { Trigger } from '@dxos/async';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
10
|
+
import { Obj } from '@dxos/echo';
|
|
11
|
+
import { Operation, OperationHandlerSet } from '@dxos/operation';
|
|
11
12
|
|
|
12
13
|
import { ComputeGraphRegistry, defaultPlugins } from './compute-graph-registry';
|
|
13
14
|
import { TestBuilder, createMockedComputeRuntimeProvider } from './testing';
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
const AddDefinition = Operation.make({
|
|
17
|
+
meta: {
|
|
17
18
|
key: 'add',
|
|
18
19
|
name: 'add',
|
|
19
20
|
description: 'Adds two numbers',
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
},
|
|
22
|
+
input: Schema.Struct({ a: Schema.Number, b: Schema.Number }),
|
|
23
|
+
output: Schema.Number,
|
|
24
|
+
services: [],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const add = AddDefinition.pipe(
|
|
28
|
+
Operation.withHandler(
|
|
29
|
+
Effect.fn(function* ({ a, b }) {
|
|
30
|
+
return a + b;
|
|
31
|
+
}),
|
|
32
|
+
),
|
|
33
|
+
);
|
|
24
34
|
|
|
35
|
+
describe('ComputeGraphRegistry', () => {
|
|
25
36
|
test('invokes user function through compute graph', async () => {
|
|
26
|
-
const computeRuntime = createMockedComputeRuntimeProvider({
|
|
27
|
-
|
|
37
|
+
const computeRuntime = createMockedComputeRuntimeProvider({
|
|
38
|
+
functions: OperationHandlerSet.make(add),
|
|
39
|
+
});
|
|
40
|
+
const testBuilder = new TestBuilder({
|
|
41
|
+
types: [Operation.PersistentOperation],
|
|
42
|
+
computeRuntime,
|
|
43
|
+
});
|
|
28
44
|
await testBuilder.open();
|
|
29
45
|
onTestFinished(async () => {
|
|
30
46
|
await testBuilder.close();
|
|
@@ -33,7 +49,10 @@ describe('ComputeGraphRegistry', () => {
|
|
|
33
49
|
const space = await testBuilder.client.spaces.create();
|
|
34
50
|
|
|
35
51
|
// Register Edge function plugin and runtime in the registry.
|
|
36
|
-
const registry = new ComputeGraphRegistry({
|
|
52
|
+
const registry = new ComputeGraphRegistry({
|
|
53
|
+
plugins: defaultPlugins,
|
|
54
|
+
computeRuntime,
|
|
55
|
+
});
|
|
37
56
|
await registry.open();
|
|
38
57
|
onTestFinished(async () => {
|
|
39
58
|
await registry.close();
|
|
@@ -45,9 +64,10 @@ describe('ComputeGraphRegistry', () => {
|
|
|
45
64
|
await graph.close();
|
|
46
65
|
});
|
|
47
66
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
67
|
+
const functionObj = Operation.serialize(add);
|
|
68
|
+
Obj.change(functionObj, (functionObj) => {
|
|
69
|
+
functionObj.binding = 'ADD';
|
|
70
|
+
});
|
|
51
71
|
space.db.add(functionObj);
|
|
52
72
|
|
|
53
73
|
const node = await graph.getOrCreateNode('sheet').open();
|
|
@@ -69,12 +89,17 @@ describe('ComputeGraphRegistry', () => {
|
|
|
69
89
|
|
|
70
90
|
// Sanity check: mapping to fully qualified id works as well.
|
|
71
91
|
const bindingId = graph.mapFunctionBindingToId('ADD(2, 3)');
|
|
72
|
-
expect(bindingId.startsWith(`${
|
|
92
|
+
expect(bindingId.startsWith(`${Obj.getDXN(functionObj).toString()}`)).to.be.true;
|
|
73
93
|
});
|
|
74
94
|
|
|
75
95
|
test('adding a function binding updates autocomplete and enables execution', async () => {
|
|
76
|
-
const computeRuntime = createMockedComputeRuntimeProvider({
|
|
77
|
-
|
|
96
|
+
const computeRuntime = createMockedComputeRuntimeProvider({
|
|
97
|
+
functions: OperationHandlerSet.make(add),
|
|
98
|
+
});
|
|
99
|
+
const testBuilder = new TestBuilder({
|
|
100
|
+
types: [Operation.PersistentOperation],
|
|
101
|
+
computeRuntime,
|
|
102
|
+
});
|
|
78
103
|
await testBuilder.open();
|
|
79
104
|
onTestFinished(async () => {
|
|
80
105
|
await testBuilder.close();
|
|
@@ -82,7 +107,10 @@ describe('ComputeGraphRegistry', () => {
|
|
|
82
107
|
|
|
83
108
|
const space = await testBuilder.client.spaces.create();
|
|
84
109
|
|
|
85
|
-
const registry = new ComputeGraphRegistry({
|
|
110
|
+
const registry = new ComputeGraphRegistry({
|
|
111
|
+
plugins: defaultPlugins,
|
|
112
|
+
computeRuntime,
|
|
113
|
+
});
|
|
86
114
|
await registry.open();
|
|
87
115
|
onTestFinished(async () => {
|
|
88
116
|
await registry.close();
|
|
@@ -106,15 +134,16 @@ describe('ComputeGraphRegistry', () => {
|
|
|
106
134
|
});
|
|
107
135
|
onTestFinished(unsubscribe);
|
|
108
136
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
137
|
+
const functionObj = Operation.serialize(add);
|
|
138
|
+
Obj.change(functionObj, (functionObj) => {
|
|
139
|
+
functionObj.binding = 'ADD';
|
|
140
|
+
});
|
|
112
141
|
space.db.add(functionObj);
|
|
113
142
|
|
|
114
143
|
await functionsUpdated.wait();
|
|
115
144
|
|
|
116
145
|
// Autocomplete should now include the binding.
|
|
117
|
-
const echoFunctions = graph.getFunctions({ standard: false, echo: true }).map((
|
|
146
|
+
const echoFunctions = graph.getFunctions({ standard: false, echo: true }).map((fn) => fn.name);
|
|
118
147
|
expect(echoFunctions).toContain('ADD');
|
|
119
148
|
|
|
120
149
|
// And the binding should execute when used in a formula.
|
|
@@ -7,9 +7,9 @@ import defaultsDeep from 'lodash.defaultsdeep';
|
|
|
7
7
|
|
|
8
8
|
import { type Space, type SpaceId } from '@dxos/client/echo';
|
|
9
9
|
import { Resource } from '@dxos/context';
|
|
10
|
-
import { type FunctionInvocationService } from '@dxos/functions';
|
|
11
10
|
import { invariant } from '@dxos/invariant';
|
|
12
11
|
import { log } from '@dxos/log';
|
|
12
|
+
import type { Operation } from '@dxos/operation';
|
|
13
13
|
import type { ConfigParams, FunctionPluginDefinition, FunctionTranslationsPackage } from '@dxos/vendor-hyperformula';
|
|
14
14
|
import { HyperFormula } from '@dxos/vendor-hyperformula';
|
|
15
15
|
|
|
@@ -22,7 +22,7 @@ export type ComputeGraphPlugin = {
|
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
export type FunctionsRuntimeProvider = {
|
|
25
|
-
getRuntime(spaceId: SpaceId): ManagedRuntime.ManagedRuntime<
|
|
25
|
+
getRuntime(spaceId: SpaceId): ManagedRuntime.ManagedRuntime<Operation.Service, never>;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
export type ComputeGraphOptions = {
|
|
@@ -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 { Operation } from '@dxos/operation';
|
|
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: [Operation.PersistentOperation] });
|
|
20
19
|
await testBuilder.open();
|
|
21
20
|
});
|
|
22
21
|
afterEach(async () => {
|
|
@@ -31,13 +30,45 @@ 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(
|
|
34
|
+
Obj.make(Operation.PersistentOperation, { name: 'test', version: '0.0.1', binding: 'TEST' }),
|
|
35
|
+
);
|
|
35
36
|
await trigger.wait();
|
|
36
37
|
const functions = graph.getFunctions({ echo: true });
|
|
37
38
|
expect(functions).to.toHaveLength(1);
|
|
38
39
|
|
|
39
40
|
const id = graph.mapFunctionBindingToId('TEST()');
|
|
40
|
-
expect(id).to.eq(`${
|
|
41
|
+
expect(id).to.eq(`${Obj.getDXN(functionObject).toString()}()`);
|
|
42
|
+
|
|
43
|
+
const restored = graph.mapFunctionBindingFromId(id);
|
|
44
|
+
expect(restored).to.eq('TEST()');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('map functions roundtrip with args', async () => {
|
|
48
|
+
const space = await testBuilder.client.spaces.create();
|
|
49
|
+
const graph = testBuilder.registry.createGraph(space);
|
|
50
|
+
await graph.open();
|
|
51
|
+
|
|
52
|
+
const trigger = new Trigger();
|
|
53
|
+
graph.update.once(() => trigger.wake());
|
|
54
|
+
space.db.add(Obj.make(Operation.PersistentOperation, { name: 'forex', version: '0.0.1', binding: 'FOREX' }));
|
|
55
|
+
await trigger.wait();
|
|
56
|
+
|
|
57
|
+
const stored = graph.mapFunctionBindingToId('=FOREX(C6,C7)');
|
|
58
|
+
expect(stored).to.include('dxn:echo:');
|
|
59
|
+
expect(stored).not.to.include('FOREX');
|
|
60
|
+
|
|
61
|
+
const restored = graph.mapFunctionBindingFromId(stored);
|
|
62
|
+
expect(restored).to.eq('=FOREX(C6,C7)');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('map functions unknown binding returns undefined', async () => {
|
|
66
|
+
const space = await testBuilder.client.spaces.create();
|
|
67
|
+
const graph = testBuilder.registry.createGraph(space);
|
|
68
|
+
await graph.open();
|
|
69
|
+
|
|
70
|
+
const result = graph.mapFunctionBindingFromId('=dxn:echo:@:01ABCDEF01ABCDEF01ABCDEF01(A1,B1)');
|
|
71
|
+
expect(result).to.be.undefined;
|
|
41
72
|
});
|
|
42
73
|
|
|
43
74
|
test('cross-node references', async () => {
|
|
@@ -52,22 +83,38 @@ describe('ComputeGraph', () => {
|
|
|
52
83
|
{
|
|
53
84
|
node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, [[100, 200, 300, '=SUM(A1:C1)']]);
|
|
54
85
|
node2.graph.hf.setCellContents({ sheet: node2.sheetId, row: 0, col: 0 }, "='node-1'!D1");
|
|
55
|
-
const value1 = node1.graph.hf.getCellValue({
|
|
56
|
-
|
|
86
|
+
const value1 = node1.graph.hf.getCellValue({
|
|
87
|
+
sheet: node1.sheetId,
|
|
88
|
+
col: 3,
|
|
89
|
+
row: 0,
|
|
90
|
+
});
|
|
91
|
+
const value2 = node2.graph.hf.getCellValue({
|
|
92
|
+
sheet: node2.sheetId,
|
|
93
|
+
col: 0,
|
|
94
|
+
row: 0,
|
|
95
|
+
});
|
|
57
96
|
expect(value1).to.eq(value2);
|
|
58
97
|
}
|
|
59
98
|
|
|
60
99
|
// Get updated event.
|
|
61
100
|
const trigger = new Trigger<CellValue>();
|
|
62
101
|
node2.update.on(({ change }) => {
|
|
63
|
-
const value = node2.graph.hf.getCellValue({
|
|
102
|
+
const value = node2.graph.hf.getCellValue({
|
|
103
|
+
sheet: node2.sheetId,
|
|
104
|
+
col: 0,
|
|
105
|
+
row: 0,
|
|
106
|
+
});
|
|
64
107
|
expect(value).to.eq(change?.newValue);
|
|
65
108
|
trigger.wake(value);
|
|
66
109
|
});
|
|
67
110
|
|
|
68
111
|
{
|
|
69
112
|
node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, 400);
|
|
70
|
-
const value1 = node1.graph.hf.getCellValue({
|
|
113
|
+
const value1 = node1.graph.hf.getCellValue({
|
|
114
|
+
sheet: node1.sheetId,
|
|
115
|
+
col: 3,
|
|
116
|
+
row: 0,
|
|
117
|
+
});
|
|
71
118
|
const value2 = await trigger.wait();
|
|
72
119
|
expect(value1).to.eq(value2);
|
|
73
120
|
}
|
|
@@ -81,7 +128,11 @@ describe('ComputeGraph', () => {
|
|
|
81
128
|
|
|
82
129
|
const node1 = await graph.getOrCreateNode('node-1').open();
|
|
83
130
|
node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, "='node-2'!A1");
|
|
84
|
-
const value1 = node1.graph.hf.getCellValue({
|
|
131
|
+
const value1 = node1.graph.hf.getCellValue({
|
|
132
|
+
sheet: node1.sheetId,
|
|
133
|
+
col: 0,
|
|
134
|
+
row: 0,
|
|
135
|
+
});
|
|
85
136
|
expect(value1).to.be.instanceof(DetailedCellError);
|
|
86
137
|
expect((value1 as DetailedCellError).type).to.eq('REF');
|
|
87
138
|
});
|
package/src/compute-graph.ts
CHANGED
|
@@ -5,14 +5,13 @@
|
|
|
5
5
|
import type * as ManagedRuntime from 'effect/ManagedRuntime';
|
|
6
6
|
|
|
7
7
|
import { Event } from '@dxos/async';
|
|
8
|
-
import { Filter, type Space
|
|
9
|
-
import { FQ_ID_LENGTH } from '@dxos/client/echo';
|
|
8
|
+
import { Filter, type Space } from '@dxos/client/echo';
|
|
10
9
|
import { Resource } from '@dxos/context';
|
|
11
|
-
import {
|
|
12
|
-
import { type FunctionInvocationService, FunctionType } from '@dxos/functions';
|
|
10
|
+
import { Obj } from '@dxos/echo';
|
|
13
11
|
import { invariant } from '@dxos/invariant';
|
|
14
12
|
import { PublicKey } from '@dxos/keys';
|
|
15
13
|
import { log } from '@dxos/log';
|
|
14
|
+
import { Operation } from '@dxos/operation';
|
|
16
15
|
import { isNonNullable } from '@dxos/util';
|
|
17
16
|
import type { Listeners } from '@dxos/vendor-hyperformula';
|
|
18
17
|
import { ExportedCellChange, type HyperFormula } from '@dxos/vendor-hyperformula';
|
|
@@ -37,7 +36,7 @@ type ObjectRef = { type: string; id: string };
|
|
|
37
36
|
|
|
38
37
|
/**
|
|
39
38
|
* Marker for sheets that are managed by an ECHO object.
|
|
40
|
-
* Sheet ID: `dxos.
|
|
39
|
+
* Sheet ID: `org.dxos.type.sheet-type@1234`
|
|
41
40
|
*/
|
|
42
41
|
export const createSheetName = ({ type, id }: ObjectRef) => `${type}@${id}`;
|
|
43
42
|
export const parseSheetName = (name: string): Partial<ObjectRef> => {
|
|
@@ -60,7 +59,7 @@ export class ComputeGraph extends Resource {
|
|
|
60
59
|
private readonly _nodes = new Map<number, ComputeNode>();
|
|
61
60
|
|
|
62
61
|
// Cached function objects.
|
|
63
|
-
private _remoteFunctions:
|
|
62
|
+
private _remoteFunctions: Operation.PersistentOperation[] = [];
|
|
64
63
|
|
|
65
64
|
public readonly update = new Event<{ type: ComputeGraphEvent }>();
|
|
66
65
|
|
|
@@ -69,7 +68,7 @@ export class ComputeGraph extends Resource {
|
|
|
69
68
|
|
|
70
69
|
constructor(
|
|
71
70
|
private readonly _hf: HyperFormula,
|
|
72
|
-
private readonly _runtime: ManagedRuntime.ManagedRuntime<
|
|
71
|
+
private readonly _runtime: ManagedRuntime.ManagedRuntime<Operation.Service, never>,
|
|
73
72
|
private readonly _space?: Space,
|
|
74
73
|
private readonly _options?: Partial<FunctionContextOptions>,
|
|
75
74
|
) {
|
|
@@ -106,7 +105,10 @@ export class ComputeGraph extends Resource {
|
|
|
106
105
|
}
|
|
107
106
|
|
|
108
107
|
getFunctions(
|
|
109
|
-
{ standard, echo }: { standard?: boolean; echo?: boolean } = {
|
|
108
|
+
{ standard, echo }: { standard?: boolean; echo?: boolean } = {
|
|
109
|
+
standard: true,
|
|
110
|
+
echo: true,
|
|
111
|
+
},
|
|
110
112
|
): FunctionDefinition[] {
|
|
111
113
|
return [
|
|
112
114
|
...(standard
|
|
@@ -150,7 +152,7 @@ export class ComputeGraph extends Resource {
|
|
|
150
152
|
formula
|
|
151
153
|
//
|
|
152
154
|
// Map cross-sheet references by name onto sheet stored by ECHO object/model.
|
|
153
|
-
// Example: "Test Sheet"!A0 => "dxos.
|
|
155
|
+
// Example: "Test Sheet"!A0 => "org.dxos.type.sheet-type@1234"!A0
|
|
154
156
|
// https://hyperformula.handsontable.com/guide/cell-references.html#cell-references
|
|
155
157
|
//
|
|
156
158
|
.replace(/['"]?([ \w]+)['"]?!/, (_match, name) => {
|
|
@@ -166,7 +168,7 @@ export class ComputeGraph extends Resource {
|
|
|
166
168
|
|
|
167
169
|
for (const obj of objects) {
|
|
168
170
|
if (obj.name === name) {
|
|
169
|
-
const type = getTypename(obj)!;
|
|
171
|
+
const type = Obj.getTypename(obj)!;
|
|
170
172
|
// NOTE: Names must be single quoted.
|
|
171
173
|
return `'${createSheetName({ type, id: obj.id })}'!`;
|
|
172
174
|
}
|
|
@@ -195,8 +197,8 @@ export class ComputeGraph extends Resource {
|
|
|
195
197
|
}
|
|
196
198
|
|
|
197
199
|
/**
|
|
198
|
-
* Map from binding to fully qualified ECHO
|
|
199
|
-
* E.g., HELLO() => spaceId:objectId()
|
|
200
|
+
* Map from binding to fully qualified ECHO DXN (to store).
|
|
201
|
+
* E.g., HELLO() => dxn:echo:spaceId:objectId()
|
|
200
202
|
*/
|
|
201
203
|
mapFunctionBindingToId(formula: string): string {
|
|
202
204
|
return formula.replace(/(\w+)\((.*)\)/g, (match, binding, args) => {
|
|
@@ -206,7 +208,7 @@ export class ComputeGraph extends Resource {
|
|
|
206
208
|
|
|
207
209
|
const fn = this._remoteFunctions.find((fn) => fn.binding === binding);
|
|
208
210
|
if (fn) {
|
|
209
|
-
const id =
|
|
211
|
+
const id = Obj.getDXN(fn).toString();
|
|
210
212
|
return `${id}(${args})`;
|
|
211
213
|
} else {
|
|
212
214
|
return match;
|
|
@@ -215,23 +217,22 @@ export class ComputeGraph extends Resource {
|
|
|
215
217
|
}
|
|
216
218
|
|
|
217
219
|
/**
|
|
218
|
-
* Map from fully qualified ECHO
|
|
219
|
-
* E.g., spaceId:objectId() => HELLO()
|
|
220
|
+
* Map from fully qualified ECHO DXN to binding (from store).
|
|
221
|
+
* E.g., dxn:echo:spaceId:objectId() => HELLO()
|
|
220
222
|
*/
|
|
221
223
|
mapFunctionBindingFromId(formula: string): string | undefined {
|
|
222
|
-
const binding = formula.replace(
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
});
|
|
224
|
+
const binding = formula.replace(
|
|
225
|
+
/dxn:([^:]+):([^:(]+):([a-zA-Z0-9]+)\((.*)\)/g,
|
|
226
|
+
(_match, kind, spaceTag, objectId, args) => {
|
|
227
|
+
const dxn = `dxn:${kind}:${spaceTag}:${objectId}`;
|
|
228
|
+
const fn = this._remoteFunctions.find((fn) => Obj.getDXN(fn).toString() === dxn);
|
|
229
|
+
if (fn?.binding) {
|
|
230
|
+
return `${fn.binding}(${args})`;
|
|
231
|
+
} else {
|
|
232
|
+
return UNKNOWN_BINDING;
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
);
|
|
235
236
|
|
|
236
237
|
if (binding.startsWith(`=${UNKNOWN_BINDING}`)) {
|
|
237
238
|
return undefined;
|
|
@@ -243,11 +244,14 @@ export class ComputeGraph extends Resource {
|
|
|
243
244
|
protected override async _open(): Promise<void> {
|
|
244
245
|
if (this._space) {
|
|
245
246
|
// Subscribe to remote function definitions.
|
|
246
|
-
const query = this._space.db.query(Filter.type(
|
|
247
|
-
const unsubscribe = query.subscribe(
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
247
|
+
const query = this._space.db.query(Filter.type(Operation.PersistentOperation));
|
|
248
|
+
const unsubscribe = query.subscribe(
|
|
249
|
+
() => {
|
|
250
|
+
this._remoteFunctions = query.results.filter((fn) => fn.binding);
|
|
251
|
+
this.update.emit({ type: 'functionsUpdated' });
|
|
252
|
+
},
|
|
253
|
+
{ fire: true },
|
|
254
|
+
);
|
|
251
255
|
|
|
252
256
|
this._ctx.onDispose(unsubscribe);
|
|
253
257
|
}
|
|
@@ -7,8 +7,8 @@ import defaultsDeep from 'lodash.defaultsdeep';
|
|
|
7
7
|
|
|
8
8
|
import { type CleanupFn, debounce } from '@dxos/async';
|
|
9
9
|
import type { Space } from '@dxos/client/echo';
|
|
10
|
-
import { type FunctionInvocationService } from '@dxos/functions';
|
|
11
10
|
import { log } from '@dxos/log';
|
|
11
|
+
import type { Operation } from '@dxos/operation';
|
|
12
12
|
import type { RawInterpreterValue, SimpleCellAddress } from '@dxos/vendor-hyperformula';
|
|
13
13
|
import type { InterpreterState } from '@dxos/vendor-hyperformula';
|
|
14
14
|
import type { InterpreterValue } from '@dxos/vendor-hyperformula';
|
|
@@ -77,7 +77,7 @@ export class FunctionContext {
|
|
|
77
77
|
|
|
78
78
|
constructor(
|
|
79
79
|
private readonly _hf: HyperFormula,
|
|
80
|
-
private readonly _runtime: ManagedRuntime.ManagedRuntime<
|
|
80
|
+
private readonly _runtime: ManagedRuntime.ManagedRuntime<Operation.Service, never>,
|
|
81
81
|
private readonly _space: Space | undefined,
|
|
82
82
|
_options?: Partial<FunctionContextOptions>,
|
|
83
83
|
) {
|
|
@@ -2,15 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { effect } from '@preact/signals-core';
|
|
6
5
|
import * as Effect from 'effect/Effect';
|
|
7
6
|
import * as SchemaAST from 'effect/SchemaAST';
|
|
8
7
|
|
|
9
8
|
import { Filter } from '@dxos/client/echo';
|
|
10
|
-
import {
|
|
11
|
-
import { ComputeEventLogger, FunctionInvocationService, FunctionType, TracingService } from '@dxos/functions';
|
|
12
|
-
import { FunctionDefinition } from '@dxos/functions';
|
|
9
|
+
import { JsonSchema, Obj } from '@dxos/echo';
|
|
13
10
|
import { log } from '@dxos/log';
|
|
11
|
+
import { Operation } from '@dxos/operation';
|
|
14
12
|
import { isNonNullable } from '@dxos/util';
|
|
15
13
|
import { type ProcedureAst } from '@dxos/vendor-hyperformula';
|
|
16
14
|
import { type InterpreterState } from '@dxos/vendor-hyperformula';
|
|
@@ -36,33 +34,31 @@ export class EdgeFunctionPlugin extends AsyncFunctionPlugin {
|
|
|
36
34
|
return new CellError(ErrorType.REF, 'Missing space');
|
|
37
35
|
}
|
|
38
36
|
|
|
39
|
-
const {
|
|
40
|
-
objects: [fn],
|
|
41
|
-
} = await space.db.query(Filter.type(FunctionType, { binding })).run();
|
|
37
|
+
const [fn] = await space.db.query(Filter.type(Operation.PersistentOperation, { binding })).run();
|
|
42
38
|
if (!fn) {
|
|
43
39
|
log.info('Function not found', { binding });
|
|
44
40
|
return new CellError(ErrorType.REF, 'Function not found');
|
|
45
41
|
}
|
|
46
42
|
|
|
47
43
|
if (subscribe) {
|
|
48
|
-
const unsubscribe =
|
|
44
|
+
const unsubscribe = Obj.subscribe(fn, () => {
|
|
49
45
|
log('function changed', { fn });
|
|
50
|
-
const _ = fn?.version;
|
|
51
|
-
|
|
52
46
|
// TODO(wittjosiah): `ttl` should be 0 to force a recalculation when a new version is deployed.
|
|
53
47
|
// This needs a ttl to prevent a binding change from causing the function not to be found.
|
|
54
|
-
this.runAsyncFunction(ast, state, handler(false), {
|
|
48
|
+
this.runAsyncFunction(ast, state, handler(false), {
|
|
49
|
+
ttl: FUNCTION_TTL,
|
|
50
|
+
});
|
|
55
51
|
});
|
|
56
52
|
|
|
57
53
|
this.context.createSubscription(ast.procedureName, unsubscribe);
|
|
58
54
|
}
|
|
59
55
|
|
|
60
56
|
const runtime = this.context.runtime;
|
|
61
|
-
const functionDef =
|
|
57
|
+
const functionDef = Operation.deserialize(fn);
|
|
62
58
|
// If input schema exists, construct an object from args using the schema props order, otherwise pass { args }.
|
|
63
59
|
let input: any;
|
|
64
60
|
if (fn.inputSchema) {
|
|
65
|
-
const schema = toEffectSchema(fn.inputSchema);
|
|
61
|
+
const schema = JsonSchema.toEffectSchema(fn.inputSchema);
|
|
66
62
|
const props = SchemaAST.getPropertySignatures(schema.ast);
|
|
67
63
|
input = {} as any;
|
|
68
64
|
props.forEach(({ name }, index) => {
|
|
@@ -71,15 +67,13 @@ export class EdgeFunctionPlugin extends AsyncFunctionPlugin {
|
|
|
71
67
|
} else {
|
|
72
68
|
input = { args: args.filter(isNonNullable) };
|
|
73
69
|
}
|
|
74
|
-
const result = runtime.runPromise(
|
|
75
|
-
Effect.gen(function* () {
|
|
76
|
-
return yield* FunctionInvocationService.invokeFunction(functionDef, input);
|
|
77
|
-
}).pipe(Effect.provide(ComputeEventLogger.layerFromTracing), Effect.provide(TracingService.layerNoop)),
|
|
78
|
-
);
|
|
70
|
+
const result = runtime.runPromise(Operation.invoke(functionDef, input).pipe(Effect.orDie));
|
|
79
71
|
return result as any;
|
|
80
72
|
};
|
|
81
73
|
|
|
82
|
-
return this.runAsyncFunction(ast, state, handler(true), {
|
|
74
|
+
return this.runAsyncFunction(ast, state, handler(true), {
|
|
75
|
+
ttl: FUNCTION_TTL,
|
|
76
|
+
});
|
|
83
77
|
}
|
|
84
78
|
}
|
|
85
79
|
|
package/src/index.ts
CHANGED
|
@@ -2,33 +2,6 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
// TODO(burdon): Separate hyperformula (client compute) from remote/edge functions.
|
|
6
|
-
|
|
7
|
-
// TODO(burdon): Reconcile: types.
|
|
8
|
-
// - dxos/graph
|
|
9
|
-
// - dxos/compute
|
|
10
|
-
// - dxos/functions
|
|
11
|
-
// - dxos/assistant
|
|
12
|
-
// - dxos/plugin-automation
|
|
13
|
-
// - dxos/plugin-script
|
|
14
|
-
// - dxos/plugin-sheet
|
|
15
|
-
|
|
16
|
-
// TODO(burdon): Tech debt.
|
|
17
|
-
// - move hyperformula defs from sheet to compute.
|
|
18
|
-
// - standard from ./types exports for plugins.
|
|
19
|
-
// - dxos/agent
|
|
20
|
-
// - dxos/agent-functions
|
|
21
|
-
// - dxos/mosaic, dxos-card
|
|
22
|
-
|
|
23
|
-
// TODO(burdon): Reconcile UX.
|
|
24
|
-
// - dxos/plugin-explorer
|
|
25
|
-
// - dxos/plugin-canvas
|
|
26
|
-
// - dxos/gem-*
|
|
27
|
-
|
|
28
|
-
// TODO(burdon): Next
|
|
29
|
-
// - dxos/plugin-search
|
|
30
|
-
// - dxos/plugin-threads
|
|
31
|
-
|
|
32
5
|
export * from './compute-graph';
|
|
33
6
|
export * from './compute-graph-registry';
|
|
34
7
|
export * from './compute-node';
|