@dxos/compute 0.8.4-main.28f8d3d → 0.8.4-main.2c6827d

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 (55) hide show
  1. package/dist/lib/browser/{chunk-X2FWMHD4.mjs → chunk-CPBXT2HX.mjs} +87 -80
  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 +27 -6
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/{chunk-RP7HSSCA.mjs → chunk-D3RM3FQW.mjs} +87 -80
  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 +27 -6
  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 -15
  42. package/src/compute-node.ts +2 -5
  43. package/src/functions/async-function.ts +20 -15
  44. package/src/functions/edge-function.ts +25 -23
  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/lib/browser/chunk-X2FWMHD4.mjs.map +0 -7
  54. package/dist/lib/node-esm/chunk-RP7HSSCA.mjs.map +0 -7
  55. 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,13 +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
82
  _graph;
83
83
  sheetId;
84
- update;
84
+ update = new Event();
85
85
  constructor(_graph, sheetId) {
86
- super(), this._graph = _graph, this.sheetId = sheetId, this.update = new Event();
86
+ super(), this._graph = _graph, this.sheetId = sheetId;
87
87
  }
88
88
  get graph() {
89
89
  return this._graph;
@@ -123,15 +123,15 @@ var ComputeNode = class extends Resource {
123
123
  import defaultsDeep from "lodash.defaultsdeep";
124
124
  import { debounce } from "@dxos/async";
125
125
  import { log } from "@dxos/log";
126
- import { CellError, EmptyValue, ErrorType, FunctionPlugin } from "#hyperformula";
126
+ import { CellError, EmptyValue, ErrorType, FunctionPlugin } from "@dxos/vendor-hyperformula";
127
127
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/compute/src/functions/async-function.ts";
128
128
  var defaultFunctionContextOptions = {
129
129
  defaultTtl: 5e3,
130
- debounceDelay: 200,
131
- remoteFunctionUrl: "https://edge.dxos.workers.dev/functions"
130
+ debounceDelay: 200
132
131
  };
133
132
  var FunctionContext = class _FunctionContext {
134
133
  _hf;
134
+ _runtime;
135
135
  _space;
136
136
  // Mangle name with params.
137
137
  static createInvocationKey(name, ...args) {
@@ -142,23 +142,20 @@ var FunctionContext = class _FunctionContext {
142
142
  }
143
143
  // TODO(wittjosiah): Persist cached values.
144
144
  // Cached values for cell.
145
- _cache;
145
+ _cache = /* @__PURE__ */ new Map();
146
146
  // Active requests.
147
- _pending;
147
+ _pending = /* @__PURE__ */ new Map();
148
148
  // Query subscriptions.
149
- _subscriptions;
149
+ _subscriptions = /* @__PURE__ */ new Map();
150
150
  // Invocation count.
151
- _invocations;
151
+ _invocations = {};
152
152
  _options;
153
153
  // Debounced update handler.
154
154
  _onUpdate;
155
- constructor(_hf, _space, _options) {
155
+ constructor(_hf, _runtime, _space, _options) {
156
156
  this._hf = _hf;
157
+ this._runtime = _runtime;
157
158
  this._space = _space;
158
- this._cache = /* @__PURE__ */ new Map();
159
- this._pending = /* @__PURE__ */ new Map();
160
- this._subscriptions = /* @__PURE__ */ new Map();
161
- this._invocations = {};
162
159
  this._options = defaultsDeep(_options ?? {}, defaultFunctionContextOptions);
163
160
  this._onUpdate = debounce((update) => {
164
161
  log("update", update, {
@@ -174,15 +171,15 @@ var FunctionContext = class _FunctionContext {
174
171
  get space() {
175
172
  return this._space;
176
173
  }
177
- get remoteFunctionUrl() {
178
- return this._options.remoteFunctionUrl;
179
- }
180
174
  get info() {
181
175
  return {
182
176
  cache: this._cache.size,
183
177
  invocations: this._invocations
184
178
  };
185
179
  }
180
+ get runtime() {
181
+ return this._runtime;
182
+ }
186
183
  flush() {
187
184
  this._cache.clear();
188
185
  this._invocations = {};
@@ -278,13 +275,15 @@ var AsyncFunctionPlugin = class extends FunctionPlugin {
278
275
 
279
276
  // src/functions/edge-function.ts
280
277
  import { effect } from "@preact/signals-core";
281
- import { SchemaAST } from "effect";
282
- import { Filter, getMeta } from "@dxos/client/echo";
283
- import { toEffectSchema } from "@dxos/echo-schema";
284
- 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 { Function, FunctionInvocationService, TracingService } from "@dxos/functions";
283
+ import { FunctionDefinition } from "@dxos/functions";
285
284
  import { log as log2 } from "@dxos/log";
286
285
  import { isNonNullable } from "@dxos/util";
287
- import { CellError as CellError2, ErrorType as ErrorType2, FunctionArgumentType } from "#hyperformula";
286
+ import { CellError as CellError2, ErrorType as ErrorType2, FunctionArgumentType } from "@dxos/vendor-hyperformula";
288
287
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/compute/src/functions/edge-function.ts";
289
288
  var EDGE_FUNCTION_NAME = "DX";
290
289
  var FUNCTION_TTL = 1e4;
@@ -295,7 +294,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
295
294
  if (!space) {
296
295
  return new CellError2(ErrorType2.REF, "Missing space");
297
296
  }
298
- const { objects: [fn] } = await space.db.query(Filter.type(FunctionType, {
297
+ const { objects: [fn] } = await space.db.query(Filter.type(Function.Function, {
299
298
  binding
300
299
  })).run();
301
300
  if (!fn) {
@@ -303,7 +302,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
303
302
  binding
304
303
  }, {
305
304
  F: __dxlog_file3,
306
- L: 42,
305
+ L: 43,
307
306
  S: this,
308
307
  C: (f, a) => f(...a)
309
308
  });
@@ -315,7 +314,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
315
314
  fn
316
315
  }, {
317
316
  F: __dxlog_file3,
318
- L: 48,
317
+ L: 49,
319
318
  S: this,
320
319
  C: (f, a) => f(...a)
321
320
  });
@@ -326,32 +325,24 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
326
325
  });
327
326
  this.context.createSubscription(ast.procedureName, unsubscribe);
328
327
  }
329
- const body = {};
328
+ const runtime = this.context.runtime;
329
+ const functionDef = FunctionDefinition.deserialize(fn);
330
+ let input;
330
331
  if (fn.inputSchema) {
331
332
  const schema = toEffectSchema(fn.inputSchema);
332
- SchemaAST.getPropertySignatures(schema.ast).forEach(({ name }, index) => {
333
- 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];
334
337
  });
335
338
  } else {
336
- body.args = args.filter(isNonNullable);
339
+ input = {
340
+ args: args.filter(isNonNullable)
341
+ };
337
342
  }
338
- const path = getUserFunctionUrlInMetadata(getMeta(fn));
339
- const response = await fetch(`${this.context.remoteFunctionUrl}${path}`, {
340
- method: "POST",
341
- headers: {
342
- "Content-Type": "application/json"
343
- },
344
- body: JSON.stringify(body)
345
- });
346
- const result = await response.text();
347
- log2("function executed", {
348
- result
349
- }, {
350
- F: __dxlog_file3,
351
- L: 75,
352
- S: this,
353
- C: (f, a) => f(...a)
354
- });
343
+ const result = runtime.runPromise(Effect.gen(function* () {
344
+ return yield* FunctionInvocationService.invokeFunction(functionDef, input);
345
+ }).pipe(Effect.provide(TracingService.layerNoop)));
355
346
  return result;
356
347
  };
357
348
  return this.runAsyncFunction(ast, state, handler(true), {
@@ -2788,17 +2779,19 @@ var defaultFunctions = [
2788
2779
  ];
2789
2780
 
2790
2781
  // src/compute-graph.ts
2782
+ import { effect as effect2 } from "@preact/signals-core";
2791
2783
  import { Event as Event2 } from "@dxos/async";
2792
- import { Filter as Filter2, fullyQualifiedId } from "@dxos/client/echo";
2784
+ import { Filter as Filter2 } from "@dxos/client/echo";
2793
2785
  import { FQ_ID_LENGTH } from "@dxos/client/echo";
2794
2786
  import { Resource as Resource2 } from "@dxos/context";
2795
- import { getTypename } from "@dxos/echo-schema";
2796
- import { FunctionType as FunctionType2 } from "@dxos/functions";
2787
+ import { Obj } from "@dxos/echo";
2788
+ import { getTypename } from "@dxos/echo/internal";
2789
+ import { Function as Function2 } from "@dxos/functions";
2797
2790
  import { invariant as invariant2 } from "@dxos/invariant";
2798
2791
  import { PublicKey } from "@dxos/keys";
2799
2792
  import { log as log3 } from "@dxos/log";
2800
2793
  import { isNonNullable as isNonNullable2 } from "@dxos/util";
2801
- import { ExportedCellChange as ExportedCellChange2 } from "#hyperformula";
2794
+ import { ExportedCellChange as ExportedCellChange2 } from "@dxos/vendor-hyperformula";
2802
2795
  var __dxlog_file4 = "/__w/dxos/dxos/packages/core/compute/src/compute-graph.ts";
2803
2796
  var UNKNOWN_BINDING = "__UNKNOWN__";
2804
2797
  var createSheetName = ({ type, id }) => `${type}@${id}`;
@@ -2813,19 +2806,20 @@ var parseSheetName = (name) => {
2813
2806
  };
2814
2807
  var ComputeGraph = class extends Resource2 {
2815
2808
  _hf;
2809
+ _runtime;
2816
2810
  _space;
2817
2811
  _options;
2818
- id;
2812
+ id = `graph-${PublicKey.random().truncate()}`;
2819
2813
  // Map of nodes indexed by sheet number.
2820
- _nodes;
2814
+ _nodes = /* @__PURE__ */ new Map();
2821
2815
  // Cached function objects.
2822
- _remoteFunctions;
2823
- update;
2816
+ _remoteFunctions = [];
2817
+ update = new Event2();
2824
2818
  // The context is passed to all functions.
2825
2819
  context;
2826
- constructor(_hf, _space, _options) {
2827
- 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();
2828
- this.context = new FunctionContext(this._hf, this._space, {
2820
+ constructor(_hf, _runtime, _space, _options) {
2821
+ super(), this._hf = _hf, this._runtime = _runtime, this._space = _space, this._options = _options;
2822
+ this.context = new FunctionContext(this._hf, this._runtime, this._space, {
2829
2823
  ...this._options,
2830
2824
  onUpdate: (update) => {
2831
2825
  this._options?.onUpdate?.(update);
@@ -2881,7 +2875,7 @@ var ComputeGraph = class extends Resource2 {
2881
2875
  getOrCreateNode(name) {
2882
2876
  invariant2(name.length, void 0, {
2883
2877
  F: __dxlog_file4,
2884
- L: 129,
2878
+ L: 135,
2885
2879
  S: this,
2886
2880
  A: [
2887
2881
  "name.length",
@@ -2894,7 +2888,7 @@ var ComputeGraph = class extends Resource2 {
2894
2888
  sheet: name
2895
2889
  }, {
2896
2890
  F: __dxlog_file4,
2897
- L: 131,
2891
+ L: 137,
2898
2892
  S: this,
2899
2893
  C: (f, a) => f(...a)
2900
2894
  });
@@ -2903,7 +2897,7 @@ var ComputeGraph = class extends Resource2 {
2903
2897
  const sheetId = this._hf.getSheetId(name);
2904
2898
  invariant2(sheetId !== void 0, void 0, {
2905
2899
  F: __dxlog_file4,
2906
- L: 136,
2900
+ L: 142,
2907
2901
  S: this,
2908
2902
  A: [
2909
2903
  "sheetId !== undefined",
@@ -2959,7 +2953,7 @@ var ComputeGraph = class extends Resource2 {
2959
2953
  }
2960
2954
  const fn = this._remoteFunctions.find((fn2) => fn2.binding === binding);
2961
2955
  if (fn) {
2962
- const id = fullyQualifiedId(fn);
2956
+ const id = Obj.getDXN(fn).toString();
2963
2957
  return `${id}(${args})`;
2964
2958
  } else {
2965
2959
  return match;
@@ -2976,7 +2970,7 @@ var ComputeGraph = class extends Resource2 {
2976
2970
  if (id.length !== FQ_ID_LENGTH) {
2977
2971
  return match;
2978
2972
  }
2979
- const fn = this._remoteFunctions.find((fn2) => fullyQualifiedId(fn2) === id);
2973
+ const fn = this._remoteFunctions.find((fn2) => Obj.getDXN(fn2).toString() === id);
2980
2974
  if (fn?.binding) {
2981
2975
  return `${fn.binding}(${args})`;
2982
2976
  } else {
@@ -2991,14 +2985,18 @@ var ComputeGraph = class extends Resource2 {
2991
2985
  }
2992
2986
  async _open() {
2993
2987
  if (this._space) {
2994
- const query = this._space.db.query(Filter2.type(FunctionType2));
2995
- const unsubscribe = query.subscribe(({ objects }) => {
2996
- this._remoteFunctions = objects.filter(({ binding }) => binding);
2988
+ const query = this._space.db.query(Filter2.type(Function2.Function));
2989
+ const unsubscribe = query.subscribe();
2990
+ const dispose = effect2(() => {
2991
+ this._remoteFunctions = query.objects.filter(({ binding }) => binding);
2997
2992
  this.update.emit({
2998
2993
  type: "functionsUpdated"
2999
2994
  });
3000
2995
  });
3001
- this._ctx.onDispose(unsubscribe);
2996
+ this._ctx.onDispose(() => {
2997
+ unsubscribe();
2998
+ dispose();
2999
+ });
3002
3000
  }
3003
3001
  }
3004
3002
  async _close() {
@@ -3013,25 +3011,24 @@ import defaultsDeep2 from "lodash.defaultsdeep";
3013
3011
  import { Resource as Resource3 } from "@dxos/context";
3014
3012
  import { invariant as invariant3 } from "@dxos/invariant";
3015
3013
  import { log as log4 } from "@dxos/log";
3016
- import { HyperFormula } from "#hyperformula";
3014
+ import { HyperFormula } from "@dxos/vendor-hyperformula";
3017
3015
  var __dxlog_file5 = "/__w/dxos/dxos/packages/core/compute/src/compute-graph-registry.ts";
3018
- var defaultOptions = {
3019
- licenseKey: "gpl-v3"
3020
- };
3021
3016
  var defaultPlugins = [
3022
3017
  {
3023
3018
  plugin: EdgeFunctionPlugin,
3024
3019
  translations: EdgeFunctionPluginTranslations
3025
3020
  }
3026
3021
  ];
3022
+ var defaultOptions = {
3023
+ licenseKey: "gpl-v3"
3024
+ };
3027
3025
  var ComputeGraphRegistry = class extends Resource3 {
3028
3026
  _graphs = /* @__PURE__ */ new Map();
3029
3027
  _options;
3030
- constructor(options = {
3031
- plugins: defaultPlugins
3032
- }) {
3028
+ constructor(options) {
3033
3029
  super();
3034
3030
  this._options = defaultsDeep2({}, options, defaultOptions);
3031
+ this._options.plugins = this._options.plugins ?? defaultPlugins;
3035
3032
  this._options.plugins?.forEach(({ plugin, translations }) => {
3036
3033
  HyperFormula.registerFunctionPlugin(plugin, translations);
3037
3034
  });
@@ -3046,7 +3043,7 @@ var ComputeGraphRegistry = class extends Resource3 {
3046
3043
  space: space.id
3047
3044
  }, {
3048
3045
  F: __dxlog_file5,
3049
- L: 70,
3046
+ L: 73,
3050
3047
  S: this,
3051
3048
  C: (f, a) => f(...a)
3052
3049
  });
@@ -3057,7 +3054,7 @@ var ComputeGraphRegistry = class extends Resource3 {
3057
3054
  createGraph(space) {
3058
3055
  invariant3(!this._graphs.has(space.id), `ComputeGraph already exists for space: ${space.id}`, {
3059
3056
  F: __dxlog_file5,
3060
- L: 78,
3057
+ L: 81,
3061
3058
  S: this,
3062
3059
  A: [
3063
3060
  "!this._graphs.has(space.id)",
@@ -3065,7 +3062,17 @@ var ComputeGraphRegistry = class extends Resource3 {
3065
3062
  ]
3066
3063
  });
3067
3064
  const hf = HyperFormula.buildEmpty(this._options);
3068
- const graph = new ComputeGraph(hf, space, this._options);
3065
+ invariant3(this._options.computeRuntime, "ComputeRuntime is required", {
3066
+ F: __dxlog_file5,
3067
+ L: 83,
3068
+ S: this,
3069
+ A: [
3070
+ "this._options.computeRuntime",
3071
+ "'ComputeRuntime is required'"
3072
+ ]
3073
+ });
3074
+ const runtime = this._options.computeRuntime.getRuntime(space.id);
3075
+ const graph = new ComputeGraph(hf, runtime, space, this._options);
3069
3076
  this._graphs.set(space.id, graph);
3070
3077
  return graph;
3071
3078
  }
@@ -3100,8 +3107,8 @@ export {
3100
3107
  createSheetName,
3101
3108
  parseSheetName,
3102
3109
  ComputeGraph,
3103
- defaultOptions,
3104
3110
  defaultPlugins,
3111
+ defaultOptions,
3105
3112
  ComputeGraphRegistry
3106
3113
  };
3107
- //# sourceMappingURL=chunk-X2FWMHD4.mjs.map
3114
+ //# sourceMappingURL=chunk-CPBXT2HX.mjs.map