@dxos/compute 0.8.4-main.e098934 → 0.8.4-main.e8ec1fe

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 (42) hide show
  1. package/dist/lib/browser/{chunk-XQYPCRZP.mjs → chunk-CPBXT2HX.mjs} +155 -181
  2. package/dist/lib/browser/chunk-CPBXT2HX.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 +31 -20
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/{chunk-2XWWSGAU.mjs → chunk-D3RM3FQW.mjs} +155 -181
  8. package/dist/lib/node-esm/chunk-D3RM3FQW.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 +31 -20
  12. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  13. package/dist/types/src/compute-graph-registry.d.ts +8 -3
  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 +4 -1
  18. package/dist/types/src/compute-graph.d.ts.map +1 -1
  19. package/dist/types/src/functions/async-function.d.ts +11 -9
  20. package/dist/types/src/functions/async-function.d.ts.map +1 -1
  21. package/dist/types/src/functions/edge-function.d.ts.map +1 -1
  22. package/dist/types/src/index.d.ts.map +1 -1
  23. package/dist/types/src/testing/index.d.ts +1 -0
  24. package/dist/types/src/testing/index.d.ts.map +1 -1
  25. package/dist/types/src/testing/test-builder.d.ts +1 -1
  26. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  27. package/dist/types/src/testing/test-runtime.d.ts +10 -0
  28. package/dist/types/src/testing/test-runtime.d.ts.map +1 -0
  29. package/dist/types/tsconfig.tsbuildinfo +1 -1
  30. package/package.json +23 -22
  31. package/src/compute-graph-registry.test.ts +154 -0
  32. package/src/compute-graph-registry.ts +17 -8
  33. package/src/compute-graph.test.ts +29 -10
  34. package/src/compute-graph.ts +25 -13
  35. package/src/functions/async-function.ts +13 -12
  36. package/src/functions/edge-function.ts +22 -19
  37. package/src/index.ts +0 -27
  38. package/src/testing/index.ts +1 -0
  39. package/src/testing/test-builder.ts +8 -2
  40. package/src/testing/test-runtime.ts +33 -0
  41. package/dist/lib/browser/chunk-XQYPCRZP.mjs.map +0 -7
  42. package/dist/lib/node-esm/chunk-2XWWSGAU.mjs.map +0 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/compute",
3
- "version": "0.8.4-main.e098934",
3
+ "version": "0.8.4-main.e8ec1fe",
4
4
  "description": "Low-level compute API",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -35,32 +35,33 @@
35
35
  "src"
36
36
  ],
37
37
  "dependencies": {
38
- "@preact/signals-core": "^1.9.0",
39
- "effect": "3.17.7",
38
+ "@preact/signals-core": "^1.12.1",
39
+ "effect": "3.18.3",
40
40
  "lodash.defaultsdeep": "^4.6.1",
41
- "@dxos/async": "0.8.4-main.e098934",
42
- "@dxos/context": "0.8.4-main.e098934",
43
- "@dxos/client": "0.8.4-main.e098934",
44
- "@dxos/debug": "0.8.4-main.e098934",
45
- "@dxos/echo-db": "0.8.4-main.e098934",
46
- "@dxos/echo-schema": "0.8.4-main.e098934",
47
- "@dxos/functions": "0.8.4-main.e098934",
48
- "@dxos/invariant": "0.8.4-main.e098934",
49
- "@dxos/echo": "0.8.4-main.e098934",
50
- "@dxos/live-object": "0.8.4-main.e098934",
51
- "@dxos/keys": "0.8.4-main.e098934",
52
- "@dxos/log": "0.8.4-main.e098934",
53
- "@dxos/protocols": "0.8.4-main.e098934",
54
- "@dxos/schema": "0.8.4-main.e098934",
55
- "@dxos/vendor-hyperformula": "0.8.4-main.e098934",
56
- "@dxos/util": "0.8.4-main.e098934"
41
+ "@dxos/ai": "0.8.4-main.e8ec1fe",
42
+ "@dxos/async": "0.8.4-main.e8ec1fe",
43
+ "@dxos/debug": "0.8.4-main.e8ec1fe",
44
+ "@dxos/echo": "0.8.4-main.e8ec1fe",
45
+ "@dxos/functions": "0.8.4-main.e8ec1fe",
46
+ "@dxos/client": "0.8.4-main.e8ec1fe",
47
+ "@dxos/functions-runtime": "0.8.4-main.e8ec1fe",
48
+ "@dxos/invariant": "0.8.4-main.e8ec1fe",
49
+ "@dxos/keys": "0.8.4-main.e8ec1fe",
50
+ "@dxos/log": "0.8.4-main.e8ec1fe",
51
+ "@dxos/live-object": "0.8.4-main.e8ec1fe",
52
+ "@dxos/plugin-automation": "0.8.4-main.e8ec1fe",
53
+ "@dxos/context": "0.8.4-main.e8ec1fe",
54
+ "@dxos/protocols": "0.8.4-main.e8ec1fe",
55
+ "@dxos/schema": "0.8.4-main.e8ec1fe",
56
+ "@dxos/vendor-hyperformula": "0.8.4-main.e8ec1fe",
57
+ "@dxos/util": "0.8.4-main.e8ec1fe"
57
58
  },
58
59
  "devDependencies": {
59
60
  "@tldraw/indices": "^2.0.0-alpha.14",
60
61
  "@types/lodash.defaultsdeep": "^4.6.6",
61
- "@dxos/echo-db": "0.8.4-main.e098934",
62
- "@dxos/random": "0.8.4-main.e098934",
63
- "@dxos/echo-schema": "0.8.4-main.e098934"
62
+ "@dxos/echo": "0.8.4-main.e8ec1fe",
63
+ "@dxos/echo-db": "0.8.4-main.e8ec1fe",
64
+ "@dxos/random": "0.8.4-main.e8ec1fe"
64
65
  },
65
66
  "peerDependencies": {
66
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,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 = { plugins: defaultPlugins }) {
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
- 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);
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 { FunctionType } from '@dxos/functions';
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: [FunctionType] });
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(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);
38
37
 
39
38
  const id = graph.mapFunctionBindingToId('TEST()');
40
- expect(id).to.eq(`${fullyQualifiedId(functionObject)}()`);
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({ sheet: node1.sheetId, col: 3, row: 0 });
56
- 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
+ });
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({ 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
+ });
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({ 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
+ });
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({ 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
+ });
85
104
  expect(value1).to.be.instanceof(DetailedCellError);
86
105
  expect((value1 as DetailedCellError).type).to.eq('REF');
87
106
  });
@@ -2,17 +2,21 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
+ import { effect } from '@preact/signals-core';
6
+ import type * as ManagedRuntime from 'effect/ManagedRuntime';
7
+
5
8
  import { Event } from '@dxos/async';
6
- import { Filter, type Space, fullyQualifiedId } from '@dxos/client/echo';
9
+ import { Filter, type Space } from '@dxos/client/echo';
7
10
  import { FQ_ID_LENGTH } from '@dxos/client/echo';
8
11
  import { Resource } from '@dxos/context';
9
- import { getTypename } from '@dxos/echo-schema';
10
- import { FunctionType } from '@dxos/functions';
12
+ import { Obj } from '@dxos/echo';
13
+ import { getTypename } from '@dxos/echo/internal';
14
+ import { Function, type FunctionInvocationService } from '@dxos/functions';
11
15
  import { invariant } from '@dxos/invariant';
12
16
  import { PublicKey } from '@dxos/keys';
13
17
  import { log } from '@dxos/log';
14
18
  import { isNonNullable } from '@dxos/util';
15
- import { type Listeners } from '@dxos/vendor-hyperformula';
19
+ import type { Listeners } from '@dxos/vendor-hyperformula';
16
20
  import { ExportedCellChange, type HyperFormula } from '@dxos/vendor-hyperformula';
17
21
 
18
22
  import { ComputeNode } from './compute-node';
@@ -58,7 +62,7 @@ export class ComputeGraph extends Resource {
58
62
  private readonly _nodes = new Map<number, ComputeNode>();
59
63
 
60
64
  // Cached function objects.
61
- private _remoteFunctions: FunctionType[] = [];
65
+ private _remoteFunctions: Function.Function[] = [];
62
66
 
63
67
  public readonly update = new Event<{ type: ComputeGraphEvent }>();
64
68
 
@@ -67,12 +71,13 @@ export class ComputeGraph extends Resource {
67
71
 
68
72
  constructor(
69
73
  private readonly _hf: HyperFormula,
74
+ private readonly _runtime: ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>,
70
75
  private readonly _space?: Space,
71
76
  private readonly _options?: Partial<FunctionContextOptions>,
72
77
  ) {
73
78
  super();
74
79
 
75
- this.context = new FunctionContext(this._hf, this._space, {
80
+ this.context = new FunctionContext(this._hf, this._runtime, this._space, {
76
81
  ...this._options,
77
82
  onUpdate: (update) => {
78
83
  this._options?.onUpdate?.(update);
@@ -103,7 +108,10 @@ export class ComputeGraph extends Resource {
103
108
  }
104
109
 
105
110
  getFunctions(
106
- { standard, echo }: { standard?: boolean; echo?: boolean } = { standard: true, echo: true },
111
+ { standard, echo }: { standard?: boolean; echo?: boolean } = {
112
+ standard: true,
113
+ echo: true,
114
+ },
107
115
  ): FunctionDefinition[] {
108
116
  return [
109
117
  ...(standard
@@ -203,7 +211,7 @@ export class ComputeGraph extends Resource {
203
211
 
204
212
  const fn = this._remoteFunctions.find((fn) => fn.binding === binding);
205
213
  if (fn) {
206
- const id = fullyQualifiedId(fn);
214
+ const id = Obj.getDXN(fn).toString();
207
215
  return `${id}(${args})`;
208
216
  } else {
209
217
  return match;
@@ -222,7 +230,7 @@ export class ComputeGraph extends Resource {
222
230
  return match;
223
231
  }
224
232
 
225
- const fn = this._remoteFunctions.find((fn) => fullyQualifiedId(fn) === id);
233
+ const fn = this._remoteFunctions.find((fn) => Obj.getDXN(fn).toString() === id);
226
234
  if (fn?.binding) {
227
235
  return `${fn.binding}(${args})`;
228
236
  } else {
@@ -240,13 +248,17 @@ export class ComputeGraph extends Resource {
240
248
  protected override async _open(): Promise<void> {
241
249
  if (this._space) {
242
250
  // Subscribe to remote function definitions.
243
- const query = this._space.db.query(Filter.type(FunctionType));
244
- const unsubscribe = query.subscribe(({ objects }) => {
245
- this._remoteFunctions = objects.filter(({ binding }) => binding);
251
+ const query = this._space.db.query(Filter.type(Function.Function));
252
+ const unsubscribe = query.subscribe();
253
+ const dispose = effect(() => {
254
+ this._remoteFunctions = query.objects.filter(({ binding }) => binding);
246
255
  this.update.emit({ type: 'functionsUpdated' });
247
256
  });
248
257
 
249
- this._ctx.onDispose(unsubscribe);
258
+ this._ctx.onDispose(() => {
259
+ unsubscribe();
260
+ dispose();
261
+ });
250
262
  }
251
263
  }
252
264
 
@@ -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 { type Space } from '@dxos/client/echo';
9
+ import type { Space } from '@dxos/client/echo';
10
+ import { type FunctionInvocationService } from '@dxos/functions';
9
11
  import { log } from '@dxos/log';
10
- import { type RawInterpreterValue, type SimpleCellAddress } from '@dxos/vendor-hyperformula';
11
- import { type InterpreterState } from '@dxos/vendor-hyperformula';
12
- import { type InterpreterValue } from '@dxos/vendor-hyperformula';
13
- import { type ProcedureAst } from '@dxos/vendor-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';
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 = {};
@@ -3,11 +3,13 @@
3
3
  //
4
4
 
5
5
  import { effect } from '@preact/signals-core';
6
- import { SchemaAST } from 'effect';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as SchemaAST from 'effect/SchemaAST';
7
8
 
8
- import { Filter, getMeta } from '@dxos/client/echo';
9
- import { toEffectSchema } from '@dxos/echo-schema';
10
- import { FunctionType, getUserFunctionIdInMetadata } from '@dxos/functions';
9
+ import { Filter } from '@dxos/client/echo';
10
+ import { toEffectSchema } from '@dxos/echo/internal';
11
+ import { Function, FunctionInvocationService, TracingService } from '@dxos/functions';
12
+ import { FunctionDefinition } from '@dxos/functions';
11
13
  import { log } from '@dxos/log';
12
14
  import { isNonNullable } from '@dxos/util';
13
15
  import { type ProcedureAst } from '@dxos/vendor-hyperformula';
@@ -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 id = getUserFunctionIdInMetadata(getMeta(fn));
68
- const response = await fetch(`${this.context.remoteFunctionUrl}/${id}`, {
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(TracingService.layerNoop)),
78
+ );
79
+ return result as any;
77
80
  };
78
81
 
79
82
  return this.runAsyncFunction(ast, state, handler(true), { ttl: FUNCTION_TTL });
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';