@dxos/compute 0.8.4-main.84f28bd → 0.8.4-main.ae835ea

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 (56) hide show
  1. package/dist/lib/browser/{chunk-2PBHA7EI.mjs → chunk-CY35BVJC.mjs} +104 -69
  2. package/dist/lib/browser/chunk-CY35BVJC.mjs.map +7 -0
  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 +29 -6
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/{chunk-GTPQNWA3.mjs → chunk-BECNONV7.mjs} +104 -69
  8. package/dist/lib/node-esm/chunk-BECNONV7.mjs.map +7 -0
  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 +29 -6
  12. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  13. package/dist/types/src/compute-graph-registry.d.ts +10 -7
  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 +24 -28
  38. package/src/compute-graph-registry.test.ts +138 -0
  39. package/src/compute-graph-registry.ts +20 -13
  40. package/src/compute-graph.test.ts +5 -6
  41. package/src/compute-graph.ts +20 -13
  42. package/src/compute-node.ts +2 -4
  43. package/src/functions/async-function.ts +21 -16
  44. package/src/functions/edge-function.ts +25 -22
  45. package/src/hyperformula.test.ts +2 -2
  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 -4
  50. package/src/testing/test-runtime.ts +38 -0
  51. package/src/testing/util.ts +1 -1
  52. package/src/types/hyperformula.ts +8 -8
  53. package/src/types/types.test.ts +2 -2
  54. package/dist/lib/browser/chunk-2PBHA7EI.mjs.map +0 -7
  55. package/dist/lib/node-esm/chunk-GTPQNWA3.mjs.map +0 -7
  56. 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.84f28bd",
3
+ "version": "0.8.4-main.ae835ea",
4
4
  "description": "Low-level compute API",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -8,19 +8,15 @@
8
8
  "author": "DXOS.org",
9
9
  "sideEffects": true,
10
10
  "type": "module",
11
- "imports": {
12
- "#hyperformula": {
13
- "types": "hyperformula",
14
- "default": "./dist/vendor/hyperformula.mjs"
15
- }
16
- },
17
11
  "exports": {
18
12
  ".": {
13
+ "source": "./src/index.ts",
19
14
  "types": "./dist/types/src/index.d.ts",
20
15
  "browser": "./dist/lib/browser/index.mjs",
21
16
  "node": "./dist/lib/node-esm/index.mjs"
22
17
  },
23
18
  "./testing": {
19
+ "source": "./src/testing/index.ts",
24
20
  "types": "./dist/types/src/testing/index.d.ts",
25
21
  "browser": "./dist/lib/browser/testing/index.mjs",
26
22
  "node": "./dist/lib/node-esm/testing/index.mjs"
@@ -39,32 +35,32 @@
39
35
  "src"
40
36
  ],
41
37
  "dependencies": {
42
- "@preact/signals-core": "^1.9.0",
43
- "effect": "3.16.13",
44
- "hyperformula": "^2.7.1",
38
+ "@preact/signals-core": "^1.12.1",
39
+ "effect": "3.18.3",
45
40
  "lodash.defaultsdeep": "^4.6.1",
46
- "@dxos/async": "0.8.4-main.84f28bd",
47
- "@dxos/client": "0.8.4-main.84f28bd",
48
- "@dxos/context": "0.8.4-main.84f28bd",
49
- "@dxos/debug": "0.8.4-main.84f28bd",
50
- "@dxos/echo": "0.8.4-main.84f28bd",
51
- "@dxos/echo-db": "0.8.4-main.84f28bd",
52
- "@dxos/echo-schema": "0.8.4-main.84f28bd",
53
- "@dxos/functions": "0.8.4-main.84f28bd",
54
- "@dxos/invariant": "0.8.4-main.84f28bd",
55
- "@dxos/keys": "0.8.4-main.84f28bd",
56
- "@dxos/live-object": "0.8.4-main.84f28bd",
57
- "@dxos/log": "0.8.4-main.84f28bd",
58
- "@dxos/protocols": "0.8.4-main.84f28bd",
59
- "@dxos/schema": "0.8.4-main.84f28bd",
60
- "@dxos/util": "0.8.4-main.84f28bd"
41
+ "@dxos/ai": "0.8.4-main.ae835ea",
42
+ "@dxos/async": "0.8.4-main.ae835ea",
43
+ "@dxos/client": "0.8.4-main.ae835ea",
44
+ "@dxos/debug": "0.8.4-main.ae835ea",
45
+ "@dxos/echo": "0.8.4-main.ae835ea",
46
+ "@dxos/functions": "0.8.4-main.ae835ea",
47
+ "@dxos/context": "0.8.4-main.ae835ea",
48
+ "@dxos/keys": "0.8.4-main.ae835ea",
49
+ "@dxos/live-object": "0.8.4-main.ae835ea",
50
+ "@dxos/invariant": "0.8.4-main.ae835ea",
51
+ "@dxos/log": "0.8.4-main.ae835ea",
52
+ "@dxos/protocols": "0.8.4-main.ae835ea",
53
+ "@dxos/plugin-automation": "0.8.4-main.ae835ea",
54
+ "@dxos/schema": "0.8.4-main.ae835ea",
55
+ "@dxos/util": "0.8.4-main.ae835ea",
56
+ "@dxos/vendor-hyperformula": "0.8.4-main.ae835ea"
61
57
  },
62
58
  "devDependencies": {
63
59
  "@tldraw/indices": "^2.0.0-alpha.14",
64
60
  "@types/lodash.defaultsdeep": "^4.6.6",
65
- "@dxos/echo-db": "0.8.4-main.84f28bd",
66
- "@dxos/random": "0.8.4-main.84f28bd",
67
- "@dxos/echo-schema": "0.8.4-main.84f28bd"
61
+ "@dxos/echo": "0.8.4-main.ae835ea",
62
+ "@dxos/random": "0.8.4-main.ae835ea",
63
+ "@dxos/echo-db": "0.8.4-main.ae835ea"
68
64
  },
69
65
  "peerDependencies": {
70
66
  "effect": "^3.13.3"
@@ -0,0 +1,138 @@
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 { fullyQualifiedId } from '@dxos/client/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({ functions: [add] });
27
+ const testBuilder = new TestBuilder({ types: [Function.Function], computeRuntime });
28
+ await testBuilder.open();
29
+ onTestFinished(async () => {
30
+ await testBuilder.close();
31
+ });
32
+
33
+ const space = await testBuilder.client.spaces.create();
34
+
35
+ // Register Edge function plugin and runtime in the registry.
36
+ const registry = new ComputeGraphRegistry({ plugins: defaultPlugins, computeRuntime });
37
+ await registry.open();
38
+ onTestFinished(async () => {
39
+ await registry.close();
40
+ });
41
+
42
+ const graph = registry.createGraph(space);
43
+ await graph.open();
44
+ onTestFinished(async () => {
45
+ await graph.close();
46
+ });
47
+
48
+ // Create a function object in ECHO with binding that maps to DX calls.
49
+ const functionObj = FunctionDefinition.serialize(add);
50
+ functionObj.binding = 'ADD';
51
+ space.db.add(functionObj);
52
+
53
+ const node = await graph.getOrCreateNode('sheet').open();
54
+
55
+ // Wait for value update after async invocation completes.
56
+ const trigger = new Trigger<number>();
57
+ node.update.on(() => {
58
+ const value = node.getValue({ row: 0, col: 0 });
59
+ if (typeof value === 'number') {
60
+ trigger.wake(value);
61
+ }
62
+ });
63
+
64
+ // Set formula using binding; graph will map to DX("ADD", ...)
65
+ node.setValue({ row: 0, col: 0 }, '=ADD(2, 3)');
66
+
67
+ const result = await trigger.wait();
68
+ expect(result).to.equal(5);
69
+
70
+ // Sanity check: mapping to fully qualified id works as well.
71
+ const bindingId = graph.mapFunctionBindingToId('ADD(2, 3)');
72
+ expect(bindingId.startsWith(`${fullyQualifiedId(functionObj)}`)).to.be.true;
73
+ });
74
+
75
+ test('adding a function binding updates autocomplete and enables execution', async () => {
76
+ const computeRuntime = createMockedComputeRuntimeProvider({ functions: [add] });
77
+ const testBuilder = new TestBuilder({ types: [Function.Function], computeRuntime });
78
+ await testBuilder.open();
79
+ onTestFinished(async () => {
80
+ await testBuilder.close();
81
+ });
82
+
83
+ const space = await testBuilder.client.spaces.create();
84
+
85
+ const registry = new ComputeGraphRegistry({ plugins: defaultPlugins, computeRuntime });
86
+ await registry.open();
87
+ onTestFinished(async () => {
88
+ await registry.close();
89
+ });
90
+
91
+ const graph = registry.createGraph(space);
92
+ await graph.open();
93
+ onTestFinished(async () => {
94
+ await graph.close();
95
+ });
96
+
97
+ // Initially there should be no echo functions.
98
+ expect(graph.getFunctions({ standard: false, echo: true }).length).to.equal(0);
99
+
100
+ // Subscribe for function updates.
101
+ const functionsUpdated = new Trigger<void>();
102
+ const unsubscribe = graph.update.on((e) => {
103
+ if (e.type === 'functionsUpdated') {
104
+ functionsUpdated.wake();
105
+ }
106
+ });
107
+ onTestFinished(unsubscribe);
108
+
109
+ // Add a bound function to the space; graph should pick it up.
110
+ const functionObj = FunctionDefinition.serialize(add);
111
+ functionObj.binding = 'ADD';
112
+ space.db.add(functionObj);
113
+
114
+ await functionsUpdated.wait();
115
+
116
+ // Autocomplete should now include the binding.
117
+ const echoFunctions = graph.getFunctions({ standard: false, echo: true }).map((f) => f.name);
118
+ expect(echoFunctions).toContain('ADD');
119
+
120
+ // And the binding should execute when used in a formula.
121
+ const node = await graph.getOrCreateNode('sheet').open();
122
+ onTestFinished(async () => {
123
+ await node.close();
124
+ });
125
+
126
+ const valueSettled = new Trigger<number>();
127
+ node.update.on(() => {
128
+ const value = node.getValue({ row: 0, col: 0 });
129
+ if (typeof value === 'number') {
130
+ valueSettled.wake(value);
131
+ }
132
+ });
133
+
134
+ node.setValue({ row: 0, col: 0 }, '=ADD(2, 3)');
135
+ const result = await valueSettled.wait();
136
+ expect(result).to.equal(5);
137
+ });
138
+ });
@@ -2,17 +2,17 @@
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
- import { type SpaceId, type Space } from '@dxos/client/echo';
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';
13
+ import type { ConfigParams, FunctionPluginDefinition, FunctionTranslationsPackage } from '@dxos/vendor-hyperformula';
14
+ import { HyperFormula } from '@dxos/vendor-hyperformula';
14
15
 
15
- import { HyperFormula } from '#hyperformula';
16
16
  import { ComputeGraph } from './compute-graph';
17
17
  import { EdgeFunctionPlugin, EdgeFunctionPluginTranslations, type FunctionContextOptions } from './functions';
18
18
 
@@ -21,15 +21,16 @@ export type ComputeGraphPlugin = {
21
21
  translations: FunctionTranslationsPackage;
22
22
  };
23
23
 
24
+ export type FunctionsRuntimeProvider = {
25
+ getRuntime(spaceId: SpaceId): ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>;
26
+ };
27
+
24
28
  export type ComputeGraphOptions = {
25
29
  plugins?: ComputeGraphPlugin[];
30
+ computeRuntime: FunctionsRuntimeProvider;
26
31
  } & Partial<FunctionContextOptions> &
27
32
  Partial<ConfigParams>;
28
33
 
29
- export const defaultOptions: ComputeGraphOptions = {
30
- licenseKey: 'gpl-v3',
31
- };
32
-
33
34
  export const defaultPlugins: ComputeGraphPlugin[] = [
34
35
  {
35
36
  plugin: EdgeFunctionPlugin,
@@ -37,23 +38,26 @@ export const defaultPlugins: ComputeGraphPlugin[] = [
37
38
  },
38
39
  ];
39
40
 
41
+ export const defaultOptions: Partial<ComputeGraphOptions> = {
42
+ licenseKey: 'gpl-v3',
43
+ };
44
+
40
45
  /**
41
46
  * Manages a collection of ComputeGraph instances for each space.
42
47
  *
43
48
  * [ComputePlugin] => [ComputeGraphRegistry] => [ComputeGraph(Space)] => [ComputeNode(Object)]
44
49
  *
45
50
  * NOTE: The ComputeGraphRegistry manages the hierarchy of resources via its root Context.
46
- * NOTE: The package.json file defines the packaged #hyperformula module.
47
51
  */
48
52
  // TODO(burdon): Move graph into separate plugin; isolate HF deps.
49
53
  export class ComputeGraphRegistry extends Resource {
50
54
  private readonly _graphs = new Map<SpaceId, ComputeGraph>();
51
-
52
55
  private readonly _options: ComputeGraphOptions;
53
56
 
54
- constructor(options: ComputeGraphOptions = { plugins: defaultPlugins }) {
57
+ constructor(options: ComputeGraphOptions) {
55
58
  super();
56
59
  this._options = defaultsDeep({}, options, defaultOptions);
60
+ this._options.plugins = this._options.plugins ?? defaultPlugins;
57
61
  this._options.plugins?.forEach(({ plugin, translations }) => {
58
62
  HyperFormula.registerFunctionPlugin(plugin, translations);
59
63
  });
@@ -76,7 +80,10 @@ export class ComputeGraphRegistry extends Resource {
76
80
  createGraph(space: Space): ComputeGraph {
77
81
  invariant(!this._graphs.has(space.id), `ComputeGraph already exists for space: ${space.id}`);
78
82
  const hf = HyperFormula.buildEmpty(this._options);
79
- 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);
80
87
  this._graphs.set(space.id, graph);
81
88
  return graph;
82
89
  }
@@ -2,21 +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
8
  import { fullyQualifiedId } from '@dxos/client/echo';
10
- import { Obj } from '@dxos/echo';
11
- import { FunctionType } from '@dxos/functions';
9
+ import { Function } from '@dxos/functions';
10
+ import { type CellValue } from '@dxos/vendor-hyperformula';
11
+ import { DetailedCellError } from '@dxos/vendor-hyperformula';
12
12
 
13
- import { DetailedCellError } from '#hyperformula';
14
13
  import { TestBuilder } from './testing';
15
14
 
16
15
  describe('ComputeGraph', () => {
17
16
  let testBuilder: TestBuilder;
18
17
  beforeEach(async () => {
19
- testBuilder = new TestBuilder({ types: [FunctionType] });
18
+ testBuilder = new TestBuilder({ types: [Function.Function] });
20
19
  await testBuilder.open();
21
20
  });
22
21
  afterEach(async () => {
@@ -31,7 +30,7 @@ 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(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' }));
35
34
  await trigger.wait();
36
35
  const functions = graph.getFunctions({ echo: true });
37
36
  expect(functions).to.toHaveLength(1);
@@ -2,27 +2,29 @@
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 { type Space, Filter, fullyQualifiedId } from '@dxos/client/echo';
9
+ import { Filter, type Space, fullyQualifiedId } 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 { getTypename } from '@dxos/echo/internal';
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';
18
+ import type { Listeners } from '@dxos/vendor-hyperformula';
19
+ import { ExportedCellChange, type HyperFormula } from '@dxos/vendor-hyperformula';
17
20
 
18
- import { ExportedCellChange, type HyperFormula } from '#hyperformula';
19
21
  import { ComputeNode } from './compute-node';
20
22
  import {
21
- defaultFunctions,
23
+ EDGE_FUNCTION_NAME,
22
24
  FunctionContext,
23
25
  type FunctionContextOptions,
24
26
  type FunctionDefinition,
25
- EDGE_FUNCTION_NAME,
27
+ defaultFunctions,
26
28
  } from './functions';
27
29
 
28
30
  // TODO(burdon): Factor out compute-graph.
@@ -59,7 +61,7 @@ export class ComputeGraph extends Resource {
59
61
  private readonly _nodes = new Map<number, ComputeNode>();
60
62
 
61
63
  // Cached function objects.
62
- private _remoteFunctions: FunctionType[] = [];
64
+ private _remoteFunctions: Function.Function[] = [];
63
65
 
64
66
  public readonly update = new Event<{ type: ComputeGraphEvent }>();
65
67
 
@@ -68,12 +70,13 @@ export class ComputeGraph extends Resource {
68
70
 
69
71
  constructor(
70
72
  private readonly _hf: HyperFormula,
73
+ private readonly _runtime: ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>,
71
74
  private readonly _space?: Space,
72
75
  private readonly _options?: Partial<FunctionContextOptions>,
73
76
  ) {
74
77
  super();
75
78
 
76
- this.context = new FunctionContext(this._hf, this._space, {
79
+ this.context = new FunctionContext(this._hf, this._runtime, this._space, {
77
80
  ...this._options,
78
81
  onUpdate: (update) => {
79
82
  this._options?.onUpdate?.(update);
@@ -241,13 +244,17 @@ export class ComputeGraph extends Resource {
241
244
  protected override async _open(): Promise<void> {
242
245
  if (this._space) {
243
246
  // Subscribe to remote function definitions.
244
- const query = this._space.db.query(Filter.type(FunctionType));
245
- const unsubscribe = query.subscribe(({ objects }) => {
246
- this._remoteFunctions = objects.filter(({ binding }) => binding);
247
+ const query = this._space.db.query(Filter.type(Function.Function));
248
+ const unsubscribe = query.subscribe();
249
+ const dispose = effect(() => {
250
+ this._remoteFunctions = query.objects.filter(({ binding }) => binding);
247
251
  this.update.emit({ type: 'functionsUpdated' });
248
252
  });
249
253
 
250
- this._ctx.onDispose(unsubscribe);
254
+ this._ctx.onDispose(() => {
255
+ unsubscribe();
256
+ dispose();
257
+ });
251
258
  }
252
259
  }
253
260
 
@@ -2,13 +2,11 @@
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';
7
+ import { type ExportedCellChange, type Listeners } from '@dxos/vendor-hyperformula';
8
+ import { DetailedCellError } from '@dxos/vendor-hyperformula';
10
9
 
11
- import { DetailedCellError } from '#hyperformula';
12
10
  import { type ComputeGraph } from './compute-graph';
13
11
  import { type CellAddress, type CellScalarValue, isFormula } from './types';
14
12
 
@@ -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
- import { debounce, type CleanupFn } from '@dxos/async';
12
- import { type Space } from '@dxos/client/echo';
8
+ import { type CleanupFn, debounce } from '@dxos/async';
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, ErrorType, EmptyValue, 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) => {
@@ -3,17 +3,19 @@
3
3
  //
4
4
 
5
5
  import { effect } from '@preact/signals-core';
6
- import { SchemaAST } from 'effect';
7
- import { type InterpreterState } from 'hyperformula/typings/interpreter/InterpreterState';
8
- import { type ProcedureAst } from 'hyperformula/typings/parser';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as SchemaAST from 'effect/SchemaAST';
9
8
 
10
- import { Filter, getMeta } from '@dxos/client/echo';
11
- import { toEffectSchema } from '@dxos/echo-schema';
12
- import { FunctionType, getUserFunctionUrlInMetadata } from '@dxos/functions';
9
+ import { Filter } from '@dxos/client/echo';
10
+ import { toEffectSchema } from '@dxos/echo/internal';
11
+ import { ComputeEventLogger, Function, FunctionInvocationService, TracingService } from '@dxos/functions';
12
+ import { FunctionDefinition } from '@dxos/functions';
13
13
  import { log } from '@dxos/log';
14
14
  import { isNonNullable } from '@dxos/util';
15
+ import { type ProcedureAst } from '@dxos/vendor-hyperformula';
16
+ import { type InterpreterState } from '@dxos/vendor-hyperformula';
17
+ import { CellError, ErrorType, FunctionArgumentType } from '@dxos/vendor-hyperformula';
15
18
 
16
- import { CellError, ErrorType, FunctionArgumentType } from '#hyperformula';
17
19
  import { type AsyncFunction, AsyncFunctionPlugin } from './async-function';
18
20
 
19
21
  export const EDGE_FUNCTION_NAME = 'DX';
@@ -36,7 +38,7 @@ export class EdgeFunctionPlugin extends AsyncFunctionPlugin {
36
38
 
37
39
  const {
38
40
  objects: [fn],
39
- } = await space.db.query(Filter.type(FunctionType, { binding })).run();
41
+ } = await space.db.query(Filter.type(Function.Function, { binding })).run();
40
42
  if (!fn) {
41
43
  log.info('Function not found', { binding });
42
44
  return new CellError(ErrorType.REF, 'Function not found');
@@ -55,25 +57,26 @@ export class EdgeFunctionPlugin extends AsyncFunctionPlugin {
55
57
  this.context.createSubscription(ast.procedureName, unsubscribe);
56
58
  }
57
59
 
58
- const body: Record<string, any> = {};
60
+ const runtime = this.context.runtime;
61
+ const functionDef = FunctionDefinition.deserialize(fn);
62
+ // If input schema exists, construct an object from args using the schema props order, otherwise pass { args }.
63
+ let input: any;
59
64
  if (fn.inputSchema) {
60
65
  const schema = toEffectSchema(fn.inputSchema);
61
- SchemaAST.getPropertySignatures(schema.ast).forEach(({ name }, index) => {
62
- body[name.toString()] = args[index];
66
+ const props = SchemaAST.getPropertySignatures(schema.ast);
67
+ input = {} as any;
68
+ props.forEach(({ name }, index) => {
69
+ input[name.toString()] = args[index];
63
70
  });
64
71
  } else {
65
- body.args = args.filter(isNonNullable);
72
+ input = { args: args.filter(isNonNullable) };
66
73
  }
67
- const path = getUserFunctionUrlInMetadata(getMeta(fn));
68
- const response = await fetch(`${this.context.remoteFunctionUrl}${path}`, {
69
- method: 'POST',
70
- headers: { 'Content-Type': 'application/json' },
71
- body: JSON.stringify(body),
72
- });
73
- const result = await response.text();
74
- log('function executed', { result });
75
-
76
- return result;
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
+ );
79
+ return result as any;
77
80
  };
78
81
 
79
82
  return this.runAsyncFunction(ast, state, handler(true), { ttl: FUNCTION_TTL });
@@ -2,9 +2,9 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { describe, test, expect } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
6
6
 
7
- import { HyperFormula } from '#hyperformula';
7
+ import { HyperFormula } from '@dxos/vendor-hyperformula';
8
8
 
9
9
  describe('hyperformula', () => {
10
10
  test('sanity test', async () => {
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';
@@ -5,3 +5,4 @@
5
5
  export * from './test-builder';
6
6
  export * from './test-plugin';
7
7
  export * from './util';
8
+ export * from './test-runtime';