@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
@@ -1,5 +1,5 @@
1
1
  // src/types/hyperformula.ts
2
- import { DetailedCellError, ExportedCellChange } from "#hyperformula";
2
+ import { DetailedCellError, ExportedCellChange } from "@dxos/vendor-hyperformula";
3
3
 
4
4
  // src/types/types.ts
5
5
  import { invariant } from "@dxos/invariant";
@@ -77,10 +77,13 @@ var inRange = (range, cell) => {
77
77
  // src/compute-node.ts
78
78
  import { Event } from "@dxos/async";
79
79
  import { Resource } from "@dxos/context";
80
- import { DetailedCellError as DetailedCellError2 } from "#hyperformula";
80
+ import { DetailedCellError as DetailedCellError2 } from "@dxos/vendor-hyperformula";
81
81
  var ComputeNode = class extends Resource {
82
+ _graph;
83
+ sheetId;
84
+ update = new Event();
82
85
  constructor(_graph, sheetId) {
83
- super(), this._graph = _graph, this.sheetId = sheetId, this.update = new Event();
86
+ super(), this._graph = _graph, this.sheetId = sheetId;
84
87
  }
85
88
  get graph() {
86
89
  return this._graph;
@@ -120,14 +123,16 @@ var ComputeNode = class extends Resource {
120
123
  import defaultsDeep from "lodash.defaultsdeep";
121
124
  import { debounce } from "@dxos/async";
122
125
  import { log } from "@dxos/log";
123
- import { CellError, ErrorType, EmptyValue, FunctionPlugin } from "#hyperformula";
126
+ import { CellError, EmptyValue, ErrorType, FunctionPlugin } from "@dxos/vendor-hyperformula";
124
127
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/compute/src/functions/async-function.ts";
125
128
  var defaultFunctionContextOptions = {
126
129
  defaultTtl: 5e3,
127
- debounceDelay: 200,
128
- remoteFunctionUrl: "https://edge.dxos.workers.dev/functions"
130
+ debounceDelay: 200
129
131
  };
130
132
  var FunctionContext = class _FunctionContext {
133
+ _hf;
134
+ _runtime;
135
+ _space;
131
136
  // Mangle name with params.
132
137
  static createInvocationKey(name, ...args) {
133
138
  return JSON.stringify({
@@ -135,13 +140,22 @@ var FunctionContext = class _FunctionContext {
135
140
  ...args
136
141
  });
137
142
  }
138
- constructor(_hf, _space, _options) {
143
+ // TODO(wittjosiah): Persist cached values.
144
+ // Cached values for cell.
145
+ _cache = /* @__PURE__ */ new Map();
146
+ // Active requests.
147
+ _pending = /* @__PURE__ */ new Map();
148
+ // Query subscriptions.
149
+ _subscriptions = /* @__PURE__ */ new Map();
150
+ // Invocation count.
151
+ _invocations = {};
152
+ _options;
153
+ // Debounced update handler.
154
+ _onUpdate;
155
+ constructor(_hf, _runtime, _space, _options) {
139
156
  this._hf = _hf;
157
+ this._runtime = _runtime;
140
158
  this._space = _space;
141
- this._cache = /* @__PURE__ */ new Map();
142
- this._pending = /* @__PURE__ */ new Map();
143
- this._subscriptions = /* @__PURE__ */ new Map();
144
- this._invocations = {};
145
159
  this._options = defaultsDeep(_options ?? {}, defaultFunctionContextOptions);
146
160
  this._onUpdate = debounce((update) => {
147
161
  log("update", update, {
@@ -157,15 +171,15 @@ var FunctionContext = class _FunctionContext {
157
171
  get space() {
158
172
  return this._space;
159
173
  }
160
- get remoteFunctionUrl() {
161
- return this._options.remoteFunctionUrl;
162
- }
163
174
  get info() {
164
175
  return {
165
176
  cache: this._cache.size,
166
177
  invocations: this._invocations
167
178
  };
168
179
  }
180
+ get runtime() {
181
+ return this._runtime;
182
+ }
169
183
  flush() {
170
184
  this._cache.clear();
171
185
  this._invocations = {};
@@ -261,13 +275,15 @@ var AsyncFunctionPlugin = class extends FunctionPlugin {
261
275
 
262
276
  // src/functions/edge-function.ts
263
277
  import { effect } from "@preact/signals-core";
264
- import { SchemaAST } from "effect";
265
- import { Filter, getMeta } from "@dxos/client/echo";
266
- import { toEffectSchema } from "@dxos/echo-schema";
267
- import { FunctionType, getUserFunctionUrlInMetadata } from "@dxos/functions";
278
+ import * as Effect from "effect/Effect";
279
+ import * as SchemaAST from "effect/SchemaAST";
280
+ import { Filter } from "@dxos/client/echo";
281
+ import { toEffectSchema } from "@dxos/echo/internal";
282
+ import { ComputeEventLogger, Function, FunctionInvocationService, TracingService } from "@dxos/functions";
283
+ import { FunctionDefinition } from "@dxos/functions";
268
284
  import { log as log2 } from "@dxos/log";
269
285
  import { isNonNullable } from "@dxos/util";
270
- import { CellError as CellError2, ErrorType as ErrorType2, FunctionArgumentType } from "#hyperformula";
286
+ import { CellError as CellError2, ErrorType as ErrorType2, FunctionArgumentType } from "@dxos/vendor-hyperformula";
271
287
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/compute/src/functions/edge-function.ts";
272
288
  var EDGE_FUNCTION_NAME = "DX";
273
289
  var FUNCTION_TTL = 1e4;
@@ -278,7 +294,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
278
294
  if (!space) {
279
295
  return new CellError2(ErrorType2.REF, "Missing space");
280
296
  }
281
- const { objects: [fn] } = await space.db.query(Filter.type(FunctionType, {
297
+ const { objects: [fn] } = await space.db.query(Filter.type(Function.Function, {
282
298
  binding
283
299
  })).run();
284
300
  if (!fn) {
@@ -286,7 +302,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
286
302
  binding
287
303
  }, {
288
304
  F: __dxlog_file3,
289
- L: 41,
305
+ L: 43,
290
306
  S: this,
291
307
  C: (f, a) => f(...a)
292
308
  });
@@ -298,7 +314,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
298
314
  fn
299
315
  }, {
300
316
  F: __dxlog_file3,
301
- L: 47,
317
+ L: 49,
302
318
  S: this,
303
319
  C: (f, a) => f(...a)
304
320
  });
@@ -309,32 +325,24 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
309
325
  });
310
326
  this.context.createSubscription(ast.procedureName, unsubscribe);
311
327
  }
312
- const body = {};
328
+ const runtime = this.context.runtime;
329
+ const functionDef = FunctionDefinition.deserialize(fn);
330
+ let input;
313
331
  if (fn.inputSchema) {
314
332
  const schema = toEffectSchema(fn.inputSchema);
315
- SchemaAST.getPropertySignatures(schema.ast).forEach(({ name }, index) => {
316
- body[name.toString()] = args[index];
333
+ const props = SchemaAST.getPropertySignatures(schema.ast);
334
+ input = {};
335
+ props.forEach(({ name }, index) => {
336
+ input[name.toString()] = args[index];
317
337
  });
318
338
  } else {
319
- body.args = args.filter(isNonNullable);
339
+ input = {
340
+ args: args.filter(isNonNullable)
341
+ };
320
342
  }
321
- const path = getUserFunctionUrlInMetadata(getMeta(fn));
322
- const response = await fetch(`${this.context.remoteFunctionUrl}${path}`, {
323
- method: "POST",
324
- headers: {
325
- "Content-Type": "application/json"
326
- },
327
- body: JSON.stringify(body)
328
- });
329
- const result = await response.text();
330
- log2("function executed", {
331
- result
332
- }, {
333
- F: __dxlog_file3,
334
- L: 74,
335
- S: this,
336
- C: (f, a) => f(...a)
337
- });
343
+ const result = runtime.runPromise(Effect.gen(function* () {
344
+ return yield* FunctionInvocationService.invokeFunction(functionDef, input);
345
+ }).pipe(Effect.provide(ComputeEventLogger.layerFromTracing), Effect.provide(TracingService.layerNoop)));
338
346
  return result;
339
347
  };
340
348
  return this.runAsyncFunction(ast, state, handler(true), {
@@ -2771,17 +2779,18 @@ var defaultFunctions = [
2771
2779
  ];
2772
2780
 
2773
2781
  // src/compute-graph.ts
2782
+ import { effect as effect2 } from "@preact/signals-core";
2774
2783
  import { Event as Event2 } from "@dxos/async";
2775
2784
  import { Filter as Filter2, fullyQualifiedId } from "@dxos/client/echo";
2776
2785
  import { FQ_ID_LENGTH } from "@dxos/client/echo";
2777
2786
  import { Resource as Resource2 } from "@dxos/context";
2778
- import { getTypename } from "@dxos/echo-schema";
2779
- import { FunctionType as FunctionType2 } from "@dxos/functions";
2787
+ import { getTypename } from "@dxos/echo/internal";
2788
+ import { Function as Function2 } from "@dxos/functions";
2780
2789
  import { invariant as invariant2 } from "@dxos/invariant";
2781
2790
  import { PublicKey } from "@dxos/keys";
2782
2791
  import { log as log3 } from "@dxos/log";
2783
2792
  import { isNonNullable as isNonNullable2 } from "@dxos/util";
2784
- import { ExportedCellChange as ExportedCellChange2 } from "#hyperformula";
2793
+ import { ExportedCellChange as ExportedCellChange2 } from "@dxos/vendor-hyperformula";
2785
2794
  var __dxlog_file4 = "/__w/dxos/dxos/packages/core/compute/src/compute-graph.ts";
2786
2795
  var UNKNOWN_BINDING = "__UNKNOWN__";
2787
2796
  var createSheetName = ({ type, id }) => `${type}@${id}`;
@@ -2795,9 +2804,21 @@ var parseSheetName = (name) => {
2795
2804
  };
2796
2805
  };
2797
2806
  var ComputeGraph = class extends Resource2 {
2798
- constructor(_hf, _space, _options) {
2799
- super(), this._hf = _hf, this._space = _space, this._options = _options, this.id = `graph-${PublicKey.random().truncate()}`, this._nodes = /* @__PURE__ */ new Map(), this._remoteFunctions = [], this.update = new Event2();
2800
- this.context = new FunctionContext(this._hf, this._space, {
2807
+ _hf;
2808
+ _runtime;
2809
+ _space;
2810
+ _options;
2811
+ id = `graph-${PublicKey.random().truncate()}`;
2812
+ // Map of nodes indexed by sheet number.
2813
+ _nodes = /* @__PURE__ */ new Map();
2814
+ // Cached function objects.
2815
+ _remoteFunctions = [];
2816
+ update = new Event2();
2817
+ // The context is passed to all functions.
2818
+ context;
2819
+ constructor(_hf, _runtime, _space, _options) {
2820
+ super(), this._hf = _hf, this._runtime = _runtime, this._space = _space, this._options = _options;
2821
+ this.context = new FunctionContext(this._hf, this._runtime, this._space, {
2801
2822
  ...this._options,
2802
2823
  onUpdate: (update) => {
2803
2824
  this._options?.onUpdate?.(update);
@@ -2853,7 +2874,7 @@ var ComputeGraph = class extends Resource2 {
2853
2874
  getOrCreateNode(name) {
2854
2875
  invariant2(name.length, void 0, {
2855
2876
  F: __dxlog_file4,
2856
- L: 128,
2877
+ L: 131,
2857
2878
  S: this,
2858
2879
  A: [
2859
2880
  "name.length",
@@ -2866,7 +2887,7 @@ var ComputeGraph = class extends Resource2 {
2866
2887
  sheet: name
2867
2888
  }, {
2868
2889
  F: __dxlog_file4,
2869
- L: 130,
2890
+ L: 133,
2870
2891
  S: this,
2871
2892
  C: (f, a) => f(...a)
2872
2893
  });
@@ -2875,7 +2896,7 @@ var ComputeGraph = class extends Resource2 {
2875
2896
  const sheetId = this._hf.getSheetId(name);
2876
2897
  invariant2(sheetId !== void 0, void 0, {
2877
2898
  F: __dxlog_file4,
2878
- L: 135,
2899
+ L: 138,
2879
2900
  S: this,
2880
2901
  A: [
2881
2902
  "sheetId !== undefined",
@@ -2963,14 +2984,18 @@ var ComputeGraph = class extends Resource2 {
2963
2984
  }
2964
2985
  async _open() {
2965
2986
  if (this._space) {
2966
- const query = this._space.db.query(Filter2.type(FunctionType2));
2967
- const unsubscribe = query.subscribe(({ objects }) => {
2968
- this._remoteFunctions = objects.filter(({ binding }) => binding);
2987
+ const query = this._space.db.query(Filter2.type(Function2.Function));
2988
+ const unsubscribe = query.subscribe();
2989
+ const dispose = effect2(() => {
2990
+ this._remoteFunctions = query.objects.filter(({ binding }) => binding);
2969
2991
  this.update.emit({
2970
2992
  type: "functionsUpdated"
2971
2993
  });
2972
2994
  });
2973
- this._ctx.onDispose(unsubscribe);
2995
+ this._ctx.onDispose(() => {
2996
+ unsubscribe();
2997
+ dispose();
2998
+ });
2974
2999
  }
2975
3000
  }
2976
3001
  async _close() {
@@ -2985,24 +3010,24 @@ import defaultsDeep2 from "lodash.defaultsdeep";
2985
3010
  import { Resource as Resource3 } from "@dxos/context";
2986
3011
  import { invariant as invariant3 } from "@dxos/invariant";
2987
3012
  import { log as log4 } from "@dxos/log";
2988
- import { HyperFormula } from "#hyperformula";
3013
+ import { HyperFormula } from "@dxos/vendor-hyperformula";
2989
3014
  var __dxlog_file5 = "/__w/dxos/dxos/packages/core/compute/src/compute-graph-registry.ts";
2990
- var defaultOptions = {
2991
- licenseKey: "gpl-v3"
2992
- };
2993
3015
  var defaultPlugins = [
2994
3016
  {
2995
3017
  plugin: EdgeFunctionPlugin,
2996
3018
  translations: EdgeFunctionPluginTranslations
2997
3019
  }
2998
3020
  ];
3021
+ var defaultOptions = {
3022
+ licenseKey: "gpl-v3"
3023
+ };
2999
3024
  var ComputeGraphRegistry = class extends Resource3 {
3000
- constructor(options = {
3001
- plugins: defaultPlugins
3002
- }) {
3025
+ _graphs = /* @__PURE__ */ new Map();
3026
+ _options;
3027
+ constructor(options) {
3003
3028
  super();
3004
- this._graphs = /* @__PURE__ */ new Map();
3005
3029
  this._options = defaultsDeep2({}, options, defaultOptions);
3030
+ this._options.plugins = this._options.plugins ?? defaultPlugins;
3006
3031
  this._options.plugins?.forEach(({ plugin, translations }) => {
3007
3032
  HyperFormula.registerFunctionPlugin(plugin, translations);
3008
3033
  });
@@ -3017,7 +3042,7 @@ var ComputeGraphRegistry = class extends Resource3 {
3017
3042
  space: space.id
3018
3043
  }, {
3019
3044
  F: __dxlog_file5,
3020
- L: 69,
3045
+ L: 73,
3021
3046
  S: this,
3022
3047
  C: (f, a) => f(...a)
3023
3048
  });
@@ -3028,7 +3053,7 @@ var ComputeGraphRegistry = class extends Resource3 {
3028
3053
  createGraph(space) {
3029
3054
  invariant3(!this._graphs.has(space.id), `ComputeGraph already exists for space: ${space.id}`, {
3030
3055
  F: __dxlog_file5,
3031
- L: 77,
3056
+ L: 81,
3032
3057
  S: this,
3033
3058
  A: [
3034
3059
  "!this._graphs.has(space.id)",
@@ -3036,7 +3061,17 @@ var ComputeGraphRegistry = class extends Resource3 {
3036
3061
  ]
3037
3062
  });
3038
3063
  const hf = HyperFormula.buildEmpty(this._options);
3039
- const graph = new ComputeGraph(hf, space, this._options);
3064
+ invariant3(this._options.computeRuntime, "ComputeRuntime is required", {
3065
+ F: __dxlog_file5,
3066
+ L: 83,
3067
+ S: this,
3068
+ A: [
3069
+ "this._options.computeRuntime",
3070
+ "'ComputeRuntime is required'"
3071
+ ]
3072
+ });
3073
+ const runtime = this._options.computeRuntime.getRuntime(space.id);
3074
+ const graph = new ComputeGraph(hf, runtime, space, this._options);
3040
3075
  this._graphs.set(space.id, graph);
3041
3076
  return graph;
3042
3077
  }
@@ -3071,8 +3106,8 @@ export {
3071
3106
  createSheetName,
3072
3107
  parseSheetName,
3073
3108
  ComputeGraph,
3074
- defaultOptions,
3075
3109
  defaultPlugins,
3110
+ defaultOptions,
3076
3111
  ComputeGraphRegistry
3077
3112
  };
3078
- //# sourceMappingURL=chunk-2PBHA7EI.mjs.map
3113
+ //# sourceMappingURL=chunk-CY35BVJC.mjs.map