@dxos/compute 0.8.4-main.5ea62a8 → 0.8.4-main.66e292d

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.
Files changed (53) hide show
  1. package/dist/lib/browser/{chunk-OFPP3ZEX.mjs → chunk-ILJV45WQ.mjs} +159 -186
  2. package/dist/lib/browser/{chunk-OFPP3ZEX.mjs.map → chunk-ILJV45WQ.mjs.map} +3 -3
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +33 -22
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/{chunk-6GVHPKPB.mjs → chunk-QDJ6J7GT.mjs} +159 -186
  8. package/dist/lib/node-esm/{chunk-6GVHPKPB.mjs.map → chunk-QDJ6J7GT.mjs.map} +3 -3
  9. package/dist/lib/node-esm/index.mjs +1 -1
  10. package/dist/lib/node-esm/meta.json +1 -1
  11. package/dist/lib/node-esm/testing/index.mjs +33 -22
  12. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  13. package/dist/types/src/compute-graph-registry.d.ts +9 -6
  14. package/dist/types/src/compute-graph-registry.d.ts.map +1 -1
  15. package/dist/types/src/compute-graph-registry.test.d.ts +2 -0
  16. package/dist/types/src/compute-graph-registry.test.d.ts.map +1 -0
  17. package/dist/types/src/compute-graph.d.ts +5 -2
  18. package/dist/types/src/compute-graph.d.ts.map +1 -1
  19. package/dist/types/src/compute-node.d.ts +1 -2
  20. package/dist/types/src/compute-node.d.ts.map +1 -1
  21. package/dist/types/src/functions/async-function.d.ts +13 -11
  22. package/dist/types/src/functions/async-function.d.ts.map +1 -1
  23. package/dist/types/src/functions/edge-function.d.ts +3 -3
  24. package/dist/types/src/functions/edge-function.d.ts.map +1 -1
  25. package/dist/types/src/index.d.ts.map +1 -1
  26. package/dist/types/src/testing/index.d.ts +1 -0
  27. package/dist/types/src/testing/index.d.ts.map +1 -1
  28. package/dist/types/src/testing/test-builder.d.ts +1 -1
  29. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  30. package/dist/types/src/testing/test-plugin.d.ts +4 -5
  31. package/dist/types/src/testing/test-plugin.d.ts.map +1 -1
  32. package/dist/types/src/testing/test-runtime.d.ts +10 -0
  33. package/dist/types/src/testing/test-runtime.d.ts.map +1 -0
  34. package/dist/types/src/types/hyperformula.d.ts +3 -6
  35. package/dist/types/src/types/hyperformula.d.ts.map +1 -1
  36. package/dist/types/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +23 -29
  38. package/src/compute-graph-registry.test.ts +154 -0
  39. package/src/compute-graph-registry.ts +19 -13
  40. package/src/compute-graph.test.ts +31 -13
  41. package/src/compute-graph.ts +25 -16
  42. package/src/compute-node.ts +2 -5
  43. package/src/functions/async-function.ts +20 -15
  44. package/src/functions/edge-function.ts +26 -24
  45. package/src/hyperformula.test.ts +1 -1
  46. package/src/index.ts +0 -27
  47. package/src/testing/index.ts +1 -0
  48. package/src/testing/test-builder.ts +8 -2
  49. package/src/testing/test-plugin.ts +2 -5
  50. package/src/testing/test-runtime.ts +33 -0
  51. package/src/testing/util.ts +1 -1
  52. package/src/types/hyperformula.ts +8 -8
  53. package/dist/vendor/hyperformula.mjs +0 -36400
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/compute",
3
- "version": "0.8.4-main.5ea62a8",
3
+ "version": "0.8.4-main.66e292d",
4
4
  "description": "Low-level compute API",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -8,13 +8,6 @@
8
8
  "author": "DXOS.org",
9
9
  "sideEffects": true,
10
10
  "type": "module",
11
- "imports": {
12
- "#hyperformula": {
13
- "source": "hyperformula",
14
- "types": "hyperformula",
15
- "default": "./dist/vendor/hyperformula.mjs"
16
- }
17
- },
18
11
  "exports": {
19
12
  ".": {
20
13
  "source": "./src/index.ts",
@@ -42,32 +35,33 @@
42
35
  "src"
43
36
  ],
44
37
  "dependencies": {
45
- "@preact/signals-core": "^1.9.0",
46
- "effect": "3.17.7",
47
- "hyperformula": "^2.7.1",
38
+ "@preact/signals-core": "^1.12.1",
39
+ "effect": "3.18.3",
48
40
  "lodash.defaultsdeep": "^4.6.1",
49
- "@dxos/async": "0.8.4-main.5ea62a8",
50
- "@dxos/client": "0.8.4-main.5ea62a8",
51
- "@dxos/debug": "0.8.4-main.5ea62a8",
52
- "@dxos/echo": "0.8.4-main.5ea62a8",
53
- "@dxos/context": "0.8.4-main.5ea62a8",
54
- "@dxos/echo-db": "0.8.4-main.5ea62a8",
55
- "@dxos/echo-schema": "0.8.4-main.5ea62a8",
56
- "@dxos/functions": "0.8.4-main.5ea62a8",
57
- "@dxos/invariant": "0.8.4-main.5ea62a8",
58
- "@dxos/keys": "0.8.4-main.5ea62a8",
59
- "@dxos/live-object": "0.8.4-main.5ea62a8",
60
- "@dxos/log": "0.8.4-main.5ea62a8",
61
- "@dxos/protocols": "0.8.4-main.5ea62a8",
62
- "@dxos/schema": "0.8.4-main.5ea62a8",
63
- "@dxos/util": "0.8.4-main.5ea62a8"
41
+ "@dxos/ai": "0.8.4-main.66e292d",
42
+ "@dxos/async": "0.8.4-main.66e292d",
43
+ "@dxos/context": "0.8.4-main.66e292d",
44
+ "@dxos/debug": "0.8.4-main.66e292d",
45
+ "@dxos/functions": "0.8.4-main.66e292d",
46
+ "@dxos/echo": "0.8.4-main.66e292d",
47
+ "@dxos/client": "0.8.4-main.66e292d",
48
+ "@dxos/functions-runtime": "0.8.4-main.66e292d",
49
+ "@dxos/invariant": "0.8.4-main.66e292d",
50
+ "@dxos/keys": "0.8.4-main.66e292d",
51
+ "@dxos/live-object": "0.8.4-main.66e292d",
52
+ "@dxos/log": "0.8.4-main.66e292d",
53
+ "@dxos/plugin-automation": "0.8.4-main.66e292d",
54
+ "@dxos/protocols": "0.8.4-main.66e292d",
55
+ "@dxos/schema": "0.8.4-main.66e292d",
56
+ "@dxos/vendor-hyperformula": "0.8.4-main.66e292d",
57
+ "@dxos/util": "0.8.4-main.66e292d"
64
58
  },
65
59
  "devDependencies": {
66
60
  "@tldraw/indices": "^2.0.0-alpha.14",
67
61
  "@types/lodash.defaultsdeep": "^4.6.6",
68
- "@dxos/echo-schema": "0.8.4-main.5ea62a8",
69
- "@dxos/random": "0.8.4-main.5ea62a8",
70
- "@dxos/echo-db": "0.8.4-main.5ea62a8"
62
+ "@dxos/echo": "0.8.4-main.66e292d",
63
+ "@dxos/echo-db": "0.8.4-main.66e292d",
64
+ "@dxos/random": "0.8.4-main.66e292d"
71
65
  },
72
66
  "peerDependencies": {
73
67
  "effect": "^3.13.3"
@@ -0,0 +1,154 @@
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
+ functionObj.binding = 'ADD';
59
+ space.db.add(functionObj);
60
+
61
+ const node = await graph.getOrCreateNode('sheet').open();
62
+
63
+ // Wait for value update after async invocation completes.
64
+ const trigger = new Trigger<number>();
65
+ node.update.on(() => {
66
+ const value = node.getValue({ row: 0, col: 0 });
67
+ if (typeof value === 'number') {
68
+ trigger.wake(value);
69
+ }
70
+ });
71
+
72
+ // Set formula using binding; graph will map to DX("ADD", ...)
73
+ node.setValue({ row: 0, col: 0 }, '=ADD(2, 3)');
74
+
75
+ const result = await trigger.wait();
76
+ expect(result).to.equal(5);
77
+
78
+ // Sanity check: mapping to fully qualified id works as well.
79
+ const bindingId = graph.mapFunctionBindingToId('ADD(2, 3)');
80
+ expect(bindingId.startsWith(`${Obj.getDXN(functionObj).toString()}`)).to.be.true;
81
+ });
82
+
83
+ test('adding a function binding updates autocomplete and enables execution', async () => {
84
+ const computeRuntime = createMockedComputeRuntimeProvider({
85
+ functions: [add],
86
+ });
87
+ const testBuilder = new TestBuilder({
88
+ types: [Function.Function],
89
+ computeRuntime,
90
+ });
91
+ await testBuilder.open();
92
+ onTestFinished(async () => {
93
+ await testBuilder.close();
94
+ });
95
+
96
+ const space = await testBuilder.client.spaces.create();
97
+
98
+ const registry = new ComputeGraphRegistry({
99
+ plugins: defaultPlugins,
100
+ computeRuntime,
101
+ });
102
+ await registry.open();
103
+ onTestFinished(async () => {
104
+ await registry.close();
105
+ });
106
+
107
+ const graph = registry.createGraph(space);
108
+ await graph.open();
109
+ onTestFinished(async () => {
110
+ await graph.close();
111
+ });
112
+
113
+ // Initially there should be no echo functions.
114
+ expect(graph.getFunctions({ standard: false, echo: true }).length).to.equal(0);
115
+
116
+ // Subscribe for function updates.
117
+ const functionsUpdated = new Trigger<void>();
118
+ const unsubscribe = graph.update.on((e) => {
119
+ if (e.type === 'functionsUpdated') {
120
+ functionsUpdated.wake();
121
+ }
122
+ });
123
+ onTestFinished(unsubscribe);
124
+
125
+ // Add a bound function to the space; graph should pick it up.
126
+ const functionObj = FunctionDefinition.serialize(add);
127
+ functionObj.binding = 'ADD';
128
+ space.db.add(functionObj);
129
+
130
+ await functionsUpdated.wait();
131
+
132
+ // Autocomplete should now include the binding.
133
+ const echoFunctions = graph.getFunctions({ standard: false, echo: true }).map((fn) => fn.name);
134
+ expect(echoFunctions).toContain('ADD');
135
+
136
+ // And the binding should execute when used in a formula.
137
+ const node = await graph.getOrCreateNode('sheet').open();
138
+ onTestFinished(async () => {
139
+ await node.close();
140
+ });
141
+
142
+ const valueSettled = new Trigger<number>();
143
+ node.update.on(() => {
144
+ const value = node.getValue({ row: 0, col: 0 });
145
+ if (typeof value === 'number') {
146
+ valueSettled.wake(value);
147
+ }
148
+ });
149
+
150
+ node.setValue({ row: 0, col: 0 }, '=ADD(2, 3)');
151
+ const result = await valueSettled.wait();
152
+ expect(result).to.equal(5);
153
+ });
154
+ });
@@ -2,17 +2,16 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import type { FunctionPluginDefinition } from 'hyperformula';
6
- import type { ConfigParams } from 'hyperformula/typings/ConfigParams';
7
- import type { FunctionTranslationsPackage } from 'hyperformula/typings/interpreter';
5
+ import type * as ManagedRuntime from 'effect/ManagedRuntime';
8
6
  import defaultsDeep from 'lodash.defaultsdeep';
9
7
 
10
8
  import { type Space, type SpaceId } from '@dxos/client/echo';
11
9
  import { Resource } from '@dxos/context';
10
+ import { type FunctionInvocationService } from '@dxos/functions';
12
11
  import { invariant } from '@dxos/invariant';
13
12
  import { log } from '@dxos/log';
14
-
15
- import { HyperFormula } from '#hyperformula';
13
+ import type { ConfigParams, FunctionPluginDefinition, FunctionTranslationsPackage } from '@dxos/vendor-hyperformula';
14
+ import { HyperFormula } from '@dxos/vendor-hyperformula';
16
15
 
17
16
  import { ComputeGraph } from './compute-graph';
18
17
  import { EdgeFunctionPlugin, EdgeFunctionPluginTranslations, type FunctionContextOptions } from './functions';
@@ -22,15 +21,16 @@ export type ComputeGraphPlugin = {
22
21
  translations: FunctionTranslationsPackage;
23
22
  };
24
23
 
24
+ export type FunctionsRuntimeProvider = {
25
+ getRuntime(spaceId: SpaceId): ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>;
26
+ };
27
+
25
28
  export type ComputeGraphOptions = {
26
29
  plugins?: ComputeGraphPlugin[];
30
+ computeRuntime: FunctionsRuntimeProvider;
27
31
  } & Partial<FunctionContextOptions> &
28
32
  Partial<ConfigParams>;
29
33
 
30
- export const defaultOptions: ComputeGraphOptions = {
31
- licenseKey: 'gpl-v3',
32
- };
33
-
34
34
  export const defaultPlugins: ComputeGraphPlugin[] = [
35
35
  {
36
36
  plugin: EdgeFunctionPlugin,
@@ -38,23 +38,26 @@ export const defaultPlugins: ComputeGraphPlugin[] = [
38
38
  },
39
39
  ];
40
40
 
41
+ export const defaultOptions: Partial<ComputeGraphOptions> = {
42
+ licenseKey: 'gpl-v3',
43
+ };
44
+
41
45
  /**
42
46
  * Manages a collection of ComputeGraph instances for each space.
43
47
  *
44
48
  * [ComputePlugin] => [ComputeGraphRegistry] => [ComputeGraph(Space)] => [ComputeNode(Object)]
45
49
  *
46
50
  * NOTE: The ComputeGraphRegistry manages the hierarchy of resources via its root Context.
47
- * NOTE: The package.json file defines the packaged #hyperformula module.
48
51
  */
49
52
  // TODO(burdon): Move graph into separate plugin; isolate HF deps.
50
53
  export class ComputeGraphRegistry extends Resource {
51
54
  private readonly _graphs = new Map<SpaceId, ComputeGraph>();
52
-
53
55
  private readonly _options: ComputeGraphOptions;
54
56
 
55
- constructor(options: ComputeGraphOptions = { plugins: defaultPlugins }) {
57
+ constructor(options: ComputeGraphOptions) {
56
58
  super();
57
59
  this._options = defaultsDeep({}, options, defaultOptions);
60
+ this._options.plugins = this._options.plugins ?? defaultPlugins;
58
61
  this._options.plugins?.forEach(({ plugin, translations }) => {
59
62
  HyperFormula.registerFunctionPlugin(plugin, translations);
60
63
  });
@@ -77,7 +80,10 @@ export class ComputeGraphRegistry extends Resource {
77
80
  createGraph(space: Space): ComputeGraph {
78
81
  invariant(!this._graphs.has(space.id), `ComputeGraph already exists for space: ${space.id}`);
79
82
  const hf = HyperFormula.buildEmpty(this._options);
80
- const graph = new ComputeGraph(hf, space, this._options);
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);
81
87
  this._graphs.set(space.id, graph);
82
88
  return graph;
83
89
  }
@@ -2,22 +2,20 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type CellValue } from 'hyperformula';
6
5
  import { afterEach, beforeEach, describe, expect, test } from 'vitest';
7
6
 
8
7
  import { Trigger } from '@dxos/async';
9
- import { fullyQualifiedId } from '@dxos/client/echo';
10
8
  import { Obj } from '@dxos/echo';
11
- import { FunctionType } from '@dxos/functions';
12
-
13
- import { DetailedCellError } from '#hyperformula';
9
+ import { Function } from '@dxos/functions';
10
+ import { type CellValue } from '@dxos/vendor-hyperformula';
11
+ import { DetailedCellError } from '@dxos/vendor-hyperformula';
14
12
 
15
13
  import { TestBuilder } from './testing';
16
14
 
17
15
  describe('ComputeGraph', () => {
18
16
  let testBuilder: TestBuilder;
19
17
  beforeEach(async () => {
20
- testBuilder = new TestBuilder({ types: [FunctionType] });
18
+ testBuilder = new TestBuilder({ types: [Function.Function] });
21
19
  await testBuilder.open();
22
20
  });
23
21
  afterEach(async () => {
@@ -32,13 +30,13 @@ describe('ComputeGraph', () => {
32
30
  // Create script.
33
31
  const trigger = new Trigger();
34
32
  graph.update.once(() => trigger.wake());
35
- const functionObject = space.db.add(Obj.make(FunctionType, { name: 'test', version: '0.0.1', binding: 'TEST' }));
33
+ const functionObject = space.db.add(Function.make({ name: 'test', version: '0.0.1', binding: 'TEST' }));
36
34
  await trigger.wait();
37
35
  const functions = graph.getFunctions({ echo: true });
38
36
  expect(functions).to.toHaveLength(1);
39
37
 
40
38
  const id = graph.mapFunctionBindingToId('TEST()');
41
- expect(id).to.eq(`${fullyQualifiedId(functionObject)}()`);
39
+ expect(id).to.eq(`${Obj.getDXN(functionObject).toString()}()`);
42
40
  });
43
41
 
44
42
  test('cross-node references', async () => {
@@ -53,22 +51,38 @@ describe('ComputeGraph', () => {
53
51
  {
54
52
  node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, [[100, 200, 300, '=SUM(A1:C1)']]);
55
53
  node2.graph.hf.setCellContents({ sheet: node2.sheetId, row: 0, col: 0 }, "='node-1'!D1");
56
- const value1 = node1.graph.hf.getCellValue({ sheet: node1.sheetId, col: 3, row: 0 });
57
- const value2 = node2.graph.hf.getCellValue({ sheet: node2.sheetId, col: 0, row: 0 });
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
+ });
58
64
  expect(value1).to.eq(value2);
59
65
  }
60
66
 
61
67
  // Get updated event.
62
68
  const trigger = new Trigger<CellValue>();
63
69
  node2.update.on(({ change }) => {
64
- const value = node2.graph.hf.getCellValue({ sheet: node2.sheetId, col: 0, row: 0 });
70
+ const value = node2.graph.hf.getCellValue({
71
+ sheet: node2.sheetId,
72
+ col: 0,
73
+ row: 0,
74
+ });
65
75
  expect(value).to.eq(change?.newValue);
66
76
  trigger.wake(value);
67
77
  });
68
78
 
69
79
  {
70
80
  node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, 400);
71
- const value1 = node1.graph.hf.getCellValue({ sheet: node1.sheetId, col: 3, row: 0 });
81
+ const value1 = node1.graph.hf.getCellValue({
82
+ sheet: node1.sheetId,
83
+ col: 3,
84
+ row: 0,
85
+ });
72
86
  const value2 = await trigger.wait();
73
87
  expect(value1).to.eq(value2);
74
88
  }
@@ -82,7 +96,11 @@ describe('ComputeGraph', () => {
82
96
 
83
97
  const node1 = await graph.getOrCreateNode('node-1').open();
84
98
  node1.graph.hf.setCellContents({ sheet: node1.sheetId, row: 0, col: 0 }, "='node-2'!A1");
85
- const value1 = node1.graph.hf.getCellValue({ sheet: node1.sheetId, col: 0, row: 0 });
99
+ const value1 = node1.graph.hf.getCellValue({
100
+ sheet: node1.sheetId,
101
+ col: 0,
102
+ row: 0,
103
+ });
86
104
  expect(value1).to.be.instanceof(DetailedCellError);
87
105
  expect((value1 as DetailedCellError).type).to.eq('REF');
88
106
  });
@@ -2,20 +2,21 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Listeners } from 'hyperformula/typings/Emitter';
5
+ import { effect } from '@preact/signals-core';
6
+ import type * as ManagedRuntime from 'effect/ManagedRuntime';
6
7
 
7
8
  import { Event } from '@dxos/async';
8
- import { Filter, type Space, fullyQualifiedId } from '@dxos/client/echo';
9
+ import { Filter, type Space } from '@dxos/client/echo';
9
10
  import { FQ_ID_LENGTH } from '@dxos/client/echo';
10
11
  import { Resource } from '@dxos/context';
11
- import { getTypename } from '@dxos/echo-schema';
12
- import { FunctionType } from '@dxos/functions';
12
+ import { Obj } from '@dxos/echo';
13
+ import { Function, type FunctionInvocationService } from '@dxos/functions';
13
14
  import { invariant } from '@dxos/invariant';
14
15
  import { PublicKey } from '@dxos/keys';
15
16
  import { log } from '@dxos/log';
16
17
  import { isNonNullable } from '@dxos/util';
17
-
18
- import { ExportedCellChange, type HyperFormula } from '#hyperformula';
18
+ import type { Listeners } from '@dxos/vendor-hyperformula';
19
+ import { ExportedCellChange, type HyperFormula } from '@dxos/vendor-hyperformula';
19
20
 
20
21
  import { ComputeNode } from './compute-node';
21
22
  import {
@@ -60,7 +61,7 @@ export class ComputeGraph extends Resource {
60
61
  private readonly _nodes = new Map<number, ComputeNode>();
61
62
 
62
63
  // Cached function objects.
63
- private _remoteFunctions: FunctionType[] = [];
64
+ private _remoteFunctions: Function.Function[] = [];
64
65
 
65
66
  public readonly update = new Event<{ type: ComputeGraphEvent }>();
66
67
 
@@ -69,12 +70,13 @@ export class ComputeGraph extends Resource {
69
70
 
70
71
  constructor(
71
72
  private readonly _hf: HyperFormula,
73
+ private readonly _runtime: ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>,
72
74
  private readonly _space?: Space,
73
75
  private readonly _options?: Partial<FunctionContextOptions>,
74
76
  ) {
75
77
  super();
76
78
 
77
- this.context = new FunctionContext(this._hf, this._space, {
79
+ this.context = new FunctionContext(this._hf, this._runtime, this._space, {
78
80
  ...this._options,
79
81
  onUpdate: (update) => {
80
82
  this._options?.onUpdate?.(update);
@@ -105,7 +107,10 @@ export class ComputeGraph extends Resource {
105
107
  }
106
108
 
107
109
  getFunctions(
108
- { standard, echo }: { standard?: boolean; echo?: boolean } = { standard: true, echo: true },
110
+ { standard, echo }: { standard?: boolean; echo?: boolean } = {
111
+ standard: true,
112
+ echo: true,
113
+ },
109
114
  ): FunctionDefinition[] {
110
115
  return [
111
116
  ...(standard
@@ -165,7 +170,7 @@ export class ComputeGraph extends Resource {
165
170
 
166
171
  for (const obj of objects) {
167
172
  if (obj.name === name) {
168
- const type = getTypename(obj)!;
173
+ const type = Obj.getTypename(obj)!;
169
174
  // NOTE: Names must be single quoted.
170
175
  return `'${createSheetName({ type, id: obj.id })}'!`;
171
176
  }
@@ -205,7 +210,7 @@ export class ComputeGraph extends Resource {
205
210
 
206
211
  const fn = this._remoteFunctions.find((fn) => fn.binding === binding);
207
212
  if (fn) {
208
- const id = fullyQualifiedId(fn);
213
+ const id = Obj.getDXN(fn).toString();
209
214
  return `${id}(${args})`;
210
215
  } else {
211
216
  return match;
@@ -224,7 +229,7 @@ export class ComputeGraph extends Resource {
224
229
  return match;
225
230
  }
226
231
 
227
- const fn = this._remoteFunctions.find((fn) => fullyQualifiedId(fn) === id);
232
+ const fn = this._remoteFunctions.find((fn) => Obj.getDXN(fn).toString() === id);
228
233
  if (fn?.binding) {
229
234
  return `${fn.binding}(${args})`;
230
235
  } else {
@@ -242,13 +247,17 @@ export class ComputeGraph extends Resource {
242
247
  protected override async _open(): Promise<void> {
243
248
  if (this._space) {
244
249
  // Subscribe to remote function definitions.
245
- const query = this._space.db.query(Filter.type(FunctionType));
246
- const unsubscribe = query.subscribe(({ objects }) => {
247
- this._remoteFunctions = objects.filter(({ binding }) => binding);
250
+ const query = this._space.db.query(Filter.type(Function.Function));
251
+ const unsubscribe = query.subscribe();
252
+ const dispose = effect(() => {
253
+ this._remoteFunctions = query.objects.filter(({ binding }) => binding);
248
254
  this.update.emit({ type: 'functionsUpdated' });
249
255
  });
250
256
 
251
- this._ctx.onDispose(unsubscribe);
257
+ this._ctx.onDispose(() => {
258
+ unsubscribe();
259
+ dispose();
260
+ });
252
261
  }
253
262
  }
254
263
 
@@ -2,13 +2,10 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Listeners } from 'hyperformula/typings/Emitter';
6
- import { type ExportedCellChange } from 'hyperformula/typings/Exporter';
7
-
8
5
  import { Event } from '@dxos/async';
9
6
  import { Resource } from '@dxos/context';
10
-
11
- import { DetailedCellError } from '#hyperformula';
7
+ import { type ExportedCellChange, type Listeners } from '@dxos/vendor-hyperformula';
8
+ import { DetailedCellError } from '@dxos/vendor-hyperformula';
12
9
 
13
10
  import { type ComputeGraph } from './compute-graph';
14
11
  import { type CellAddress, type CellScalarValue, isFormula } from './types';
@@ -2,17 +2,18 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type SimpleCellAddress } from 'hyperformula/typings/Cell';
6
- import { type InterpreterState } from 'hyperformula/typings/interpreter/InterpreterState';
7
- import { type InterpreterValue } from 'hyperformula/typings/interpreter/InterpreterValue';
8
- import { type ProcedureAst } from 'hyperformula/typings/parser';
5
+ import type * as ManagedRuntime from 'effect/ManagedRuntime';
9
6
  import defaultsDeep from 'lodash.defaultsdeep';
10
7
 
11
8
  import { type CleanupFn, debounce } from '@dxos/async';
12
- import { type Space } from '@dxos/client/echo';
9
+ import type { Space } from '@dxos/client/echo';
10
+ import { type FunctionInvocationService } from '@dxos/functions';
13
11
  import { log } from '@dxos/log';
14
-
15
- import { CellError, EmptyValue, ErrorType, FunctionPlugin, type HyperFormula } from '#hyperformula';
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';
16
+ import { CellError, EmptyValue, ErrorType, FunctionPlugin, type HyperFormula } from '@dxos/vendor-hyperformula';
16
17
 
17
18
  // TODO(burdon): Create API gateways:
18
19
  // https://publicapis.io
@@ -34,14 +35,12 @@ export type FunctionOptions = {
34
35
  export type FunctionContextOptions = {
35
36
  defaultTtl: number;
36
37
  debounceDelay: number;
37
- remoteFunctionUrl: string;
38
38
  onUpdate?: (update: FunctionUpdateEvent) => void;
39
39
  };
40
40
 
41
- export const defaultFunctionContextOptions: FunctionContextOptions = {
41
+ export const defaultFunctionContextOptions: Pick<FunctionContextOptions, 'defaultTtl' | 'debounceDelay'> = {
42
42
  defaultTtl: 5_000,
43
43
  debounceDelay: 200,
44
- remoteFunctionUrl: 'https://edge.dxos.workers.dev/functions', // TODO(burdon): Config.
45
44
  };
46
45
 
47
46
  /**
@@ -78,6 +77,7 @@ export class FunctionContext {
78
77
 
79
78
  constructor(
80
79
  private readonly _hf: HyperFormula,
80
+ private readonly _runtime: ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>,
81
81
  private readonly _space: Space | undefined,
82
82
  _options?: Partial<FunctionContextOptions>,
83
83
  ) {
@@ -94,14 +94,14 @@ export class FunctionContext {
94
94
  return this._space;
95
95
  }
96
96
 
97
- get remoteFunctionUrl() {
98
- return this._options.remoteFunctionUrl;
99
- }
100
-
101
97
  get info() {
102
98
  return { cache: this._cache.size, invocations: this._invocations };
103
99
  }
104
100
 
101
+ get runtime() {
102
+ return this._runtime;
103
+ }
104
+
105
105
  flush(): void {
106
106
  this._cache.clear();
107
107
  this._invocations = {};
@@ -169,7 +169,12 @@ export class AsyncFunctionPlugin extends FunctionPlugin {
169
169
  /**
170
170
  * Immediately returns cached value then runs the async function.
171
171
  */
172
- protected runAsyncFunction(ast: ProcedureAst, state: InterpreterState, cb: AsyncFunction, options?: FunctionOptions) {
172
+ protected runAsyncFunction(
173
+ ast: ProcedureAst,
174
+ state: InterpreterState,
175
+ cb: AsyncFunction,
176
+ options?: FunctionOptions,
177
+ ): RawInterpreterValue {
173
178
  const { procedureName } = ast;
174
179
  const metadata = this.metadata(procedureName);
175
180
  return this.runFunction(ast.args, state, metadata, (...args: any) => {