@dxos/compute 0.8.4-main.f5c0578 → 0.8.4-main.fcfe5033a5

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 (58) hide show
  1. package/dist/lib/{browser/chunk-X2FWMHD4.mjs → neutral/chunk-JRV42MW3.mjs} +88 -94
  2. package/dist/lib/neutral/chunk-JRV42MW3.mjs.map +7 -0
  3. package/dist/lib/{browser → neutral}/index.mjs +1 -1
  4. package/dist/lib/neutral/meta.json +1 -0
  5. package/dist/lib/{browser → neutral}/testing/index.mjs +49 -6
  6. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  7. package/dist/types/src/compute-graph-registry.d.ts +9 -6
  8. package/dist/types/src/compute-graph-registry.d.ts.map +1 -1
  9. package/dist/types/src/compute-graph-registry.test.d.ts +2 -0
  10. package/dist/types/src/compute-graph-registry.test.d.ts.map +1 -0
  11. package/dist/types/src/compute-graph.d.ts +10 -7
  12. package/dist/types/src/compute-graph.d.ts.map +1 -1
  13. package/dist/types/src/compute-node.d.ts +1 -2
  14. package/dist/types/src/compute-node.d.ts.map +1 -1
  15. package/dist/types/src/functions/async-function.d.ts +13 -11
  16. package/dist/types/src/functions/async-function.d.ts.map +1 -1
  17. package/dist/types/src/functions/edge-function.d.ts +3 -3
  18. package/dist/types/src/functions/edge-function.d.ts.map +1 -1
  19. package/dist/types/src/index.d.ts.map +1 -1
  20. package/dist/types/src/testing/index.d.ts +1 -0
  21. package/dist/types/src/testing/index.d.ts.map +1 -1
  22. package/dist/types/src/testing/test-builder.d.ts +1 -1
  23. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  24. package/dist/types/src/testing/test-plugin.d.ts +4 -5
  25. package/dist/types/src/testing/test-plugin.d.ts.map +1 -1
  26. package/dist/types/src/testing/test-runtime.d.ts +10 -0
  27. package/dist/types/src/testing/test-runtime.d.ts.map +1 -0
  28. package/dist/types/src/types/hyperformula.d.ts +3 -6
  29. package/dist/types/src/types/hyperformula.d.ts.map +1 -1
  30. package/dist/types/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +29 -34
  32. package/src/compute-graph-registry.test.ts +167 -0
  33. package/src/compute-graph-registry.ts +19 -13
  34. package/src/compute-graph.test.ts +63 -13
  35. package/src/compute-graph.ts +41 -36
  36. package/src/compute-node.ts +2 -5
  37. package/src/functions/async-function.ts +20 -15
  38. package/src/functions/edge-function.ts +28 -32
  39. package/src/hyperformula.test.ts +1 -1
  40. package/src/index.ts +0 -27
  41. package/src/testing/index.ts +1 -0
  42. package/src/testing/test-builder.ts +7 -2
  43. package/src/testing/test-plugin.ts +2 -5
  44. package/src/testing/test-runtime.ts +53 -0
  45. package/src/testing/util.ts +1 -1
  46. package/src/types/hyperformula.ts +8 -8
  47. package/dist/lib/browser/chunk-X2FWMHD4.mjs.map +0 -7
  48. package/dist/lib/browser/meta.json +0 -1
  49. package/dist/lib/browser/testing/index.mjs.map +0 -7
  50. package/dist/lib/node-esm/chunk-RP7HSSCA.mjs +0 -3109
  51. package/dist/lib/node-esm/chunk-RP7HSSCA.mjs.map +0 -7
  52. package/dist/lib/node-esm/index.mjs +0 -56
  53. package/dist/lib/node-esm/index.mjs.map +0 -7
  54. package/dist/lib/node-esm/meta.json +0 -1
  55. package/dist/lib/node-esm/testing/index.mjs +0 -177
  56. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  57. package/dist/vendor/hyperformula.mjs +0 -36400
  58. /package/dist/lib/{browser → neutral}/index.mjs.map +0 -0
@@ -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 = {};
@@ -277,14 +274,14 @@ var AsyncFunctionPlugin = class extends FunctionPlugin {
277
274
  };
278
275
 
279
276
  // src/functions/edge-function.ts
280
- 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";
277
+ import * as Effect from "effect/Effect";
278
+ import * as SchemaAST from "effect/SchemaAST";
279
+ import { Filter } from "@dxos/client/echo";
280
+ import { JsonSchema, Obj } from "@dxos/echo";
285
281
  import { log as log2 } from "@dxos/log";
282
+ import { Operation } from "@dxos/operation";
286
283
  import { isNonNullable } from "@dxos/util";
287
- import { CellError as CellError2, ErrorType as ErrorType2, FunctionArgumentType } from "#hyperformula";
284
+ import { CellError as CellError2, ErrorType as ErrorType2, FunctionArgumentType } from "@dxos/vendor-hyperformula";
288
285
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/compute/src/functions/edge-function.ts";
289
286
  var EDGE_FUNCTION_NAME = "DX";
290
287
  var FUNCTION_TTL = 1e4;
@@ -295,7 +292,7 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
295
292
  if (!space) {
296
293
  return new CellError2(ErrorType2.REF, "Missing space");
297
294
  }
298
- const { objects: [fn] } = await space.db.query(Filter.type(FunctionType, {
295
+ const [fn] = await space.db.query(Filter.type(Operation.PersistentOperation, {
299
296
  binding
300
297
  })).run();
301
298
  if (!fn) {
@@ -303,55 +300,44 @@ var EdgeFunctionPlugin = class extends AsyncFunctionPlugin {
303
300
  binding
304
301
  }, {
305
302
  F: __dxlog_file3,
306
- L: 42,
303
+ L: 39,
307
304
  S: this,
308
305
  C: (f, a) => f(...a)
309
306
  });
310
307
  return new CellError2(ErrorType2.REF, "Function not found");
311
308
  }
312
309
  if (subscribe) {
313
- const unsubscribe = effect(() => {
310
+ const unsubscribe = Obj.subscribe(fn, () => {
314
311
  log2("function changed", {
315
312
  fn
316
313
  }, {
317
314
  F: __dxlog_file3,
318
- L: 48,
315
+ L: 45,
319
316
  S: this,
320
317
  C: (f, a) => f(...a)
321
318
  });
322
- const _ = fn?.version;
323
319
  this.runAsyncFunction(ast, state, handler(false), {
324
320
  ttl: FUNCTION_TTL
325
321
  });
326
322
  });
327
323
  this.context.createSubscription(ast.procedureName, unsubscribe);
328
324
  }
329
- const body = {};
325
+ const runtime = this.context.runtime;
326
+ const functionDef = Operation.deserialize(fn);
327
+ let input;
330
328
  if (fn.inputSchema) {
331
- const schema = toEffectSchema(fn.inputSchema);
332
- SchemaAST.getPropertySignatures(schema.ast).forEach(({ name }, index) => {
333
- body[name.toString()] = args[index];
329
+ const schema = JsonSchema.toEffectSchema(fn.inputSchema);
330
+ const props = SchemaAST.getPropertySignatures(schema.ast);
331
+ input = {};
332
+ props.forEach(({ name }, index) => {
333
+ input[name.toString()] = args[index];
334
334
  });
335
335
  } else {
336
- body.args = args.filter(isNonNullable);
336
+ input = {
337
+ args: args.filter(isNonNullable)
338
+ };
337
339
  }
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
- });
340
+ const result = runtime.runPromise(Operation.invoke(functionDef, input).pipe(Effect.orDie));
355
341
  return result;
356
342
  };
357
343
  return this.runAsyncFunction(ast, state, handler(true), {
@@ -2789,16 +2775,15 @@ var defaultFunctions = [
2789
2775
 
2790
2776
  // src/compute-graph.ts
2791
2777
  import { Event as Event2 } from "@dxos/async";
2792
- import { Filter as Filter2, fullyQualifiedId } from "@dxos/client/echo";
2793
- import { FQ_ID_LENGTH } from "@dxos/client/echo";
2778
+ import { Filter as Filter2 } from "@dxos/client/echo";
2794
2779
  import { Resource as Resource2 } from "@dxos/context";
2795
- import { getTypename } from "@dxos/echo-schema";
2796
- import { FunctionType as FunctionType2 } from "@dxos/functions";
2780
+ import { Obj as Obj2 } from "@dxos/echo";
2797
2781
  import { invariant as invariant2 } from "@dxos/invariant";
2798
2782
  import { PublicKey } from "@dxos/keys";
2799
2783
  import { log as log3 } from "@dxos/log";
2784
+ import { Operation as Operation2 } from "@dxos/operation";
2800
2785
  import { isNonNullable as isNonNullable2 } from "@dxos/util";
2801
- import { ExportedCellChange as ExportedCellChange2 } from "#hyperformula";
2786
+ import { ExportedCellChange as ExportedCellChange2 } from "@dxos/vendor-hyperformula";
2802
2787
  var __dxlog_file4 = "/__w/dxos/dxos/packages/core/compute/src/compute-graph.ts";
2803
2788
  var UNKNOWN_BINDING = "__UNKNOWN__";
2804
2789
  var createSheetName = ({ type, id }) => `${type}@${id}`;
@@ -2813,19 +2798,20 @@ var parseSheetName = (name) => {
2813
2798
  };
2814
2799
  var ComputeGraph = class extends Resource2 {
2815
2800
  _hf;
2801
+ _runtime;
2816
2802
  _space;
2817
2803
  _options;
2818
- id;
2804
+ id = `graph-${PublicKey.random().truncate()}`;
2819
2805
  // Map of nodes indexed by sheet number.
2820
- _nodes;
2806
+ _nodes = /* @__PURE__ */ new Map();
2821
2807
  // Cached function objects.
2822
- _remoteFunctions;
2823
- update;
2808
+ _remoteFunctions = [];
2809
+ update = new Event2();
2824
2810
  // The context is passed to all functions.
2825
2811
  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, {
2812
+ constructor(_hf, _runtime, _space, _options) {
2813
+ super(), this._hf = _hf, this._runtime = _runtime, this._space = _space, this._options = _options;
2814
+ this.context = new FunctionContext(this._hf, this._runtime, this._space, {
2829
2815
  ...this._options,
2830
2816
  onUpdate: (update) => {
2831
2817
  this._options?.onUpdate?.(update);
@@ -2881,7 +2867,7 @@ var ComputeGraph = class extends Resource2 {
2881
2867
  getOrCreateNode(name) {
2882
2868
  invariant2(name.length, void 0, {
2883
2869
  F: __dxlog_file4,
2884
- L: 129,
2870
+ L: 132,
2885
2871
  S: this,
2886
2872
  A: [
2887
2873
  "name.length",
@@ -2894,7 +2880,7 @@ var ComputeGraph = class extends Resource2 {
2894
2880
  sheet: name
2895
2881
  }, {
2896
2882
  F: __dxlog_file4,
2897
- L: 131,
2883
+ L: 134,
2898
2884
  S: this,
2899
2885
  C: (f, a) => f(...a)
2900
2886
  });
@@ -2903,7 +2889,7 @@ var ComputeGraph = class extends Resource2 {
2903
2889
  const sheetId = this._hf.getSheetId(name);
2904
2890
  invariant2(sheetId !== void 0, void 0, {
2905
2891
  F: __dxlog_file4,
2906
- L: 136,
2892
+ L: 139,
2907
2893
  S: this,
2908
2894
  A: [
2909
2895
  "sheetId !== undefined",
@@ -2927,7 +2913,7 @@ var ComputeGraph = class extends Resource2 {
2927
2913
  }).filter(isNonNullable2);
2928
2914
  for (const obj of objects) {
2929
2915
  if (obj.name === name) {
2930
- const type = getTypename(obj);
2916
+ const type = Obj2.getTypename(obj);
2931
2917
  return `'${createSheetName({
2932
2918
  type,
2933
2919
  id: obj.id
@@ -2949,8 +2935,8 @@ var ComputeGraph = class extends Resource2 {
2949
2935
  });
2950
2936
  }
2951
2937
  /**
2952
- * Map from binding to fully qualified ECHO ID (to store).
2953
- * E.g., HELLO() => spaceId:objectId()
2938
+ * Map from binding to fully qualified ECHO DXN (to store).
2939
+ * E.g., HELLO() => dxn:echo:spaceId:objectId()
2954
2940
  */
2955
2941
  mapFunctionBindingToId(formula) {
2956
2942
  return formula.replace(/(\w+)\((.*)\)/g, (match, binding, args) => {
@@ -2959,7 +2945,7 @@ var ComputeGraph = class extends Resource2 {
2959
2945
  }
2960
2946
  const fn = this._remoteFunctions.find((fn2) => fn2.binding === binding);
2961
2947
  if (fn) {
2962
- const id = fullyQualifiedId(fn);
2948
+ const id = Obj2.getDXN(fn).toString();
2963
2949
  return `${id}(${args})`;
2964
2950
  } else {
2965
2951
  return match;
@@ -2967,16 +2953,13 @@ var ComputeGraph = class extends Resource2 {
2967
2953
  });
2968
2954
  }
2969
2955
  /**
2970
- * Map from fully qualified ECHO ID to binding (from store).
2971
- * E.g., spaceId:objectId() => HELLO()
2956
+ * Map from fully qualified ECHO DXN to binding (from store).
2957
+ * E.g., dxn:echo:spaceId:objectId() => HELLO()
2972
2958
  */
2973
2959
  mapFunctionBindingFromId(formula) {
2974
- const binding = formula.replace(/(\w+):([a-zA-Z0-9]+)\((.*)\)/g, (match, spaceId, objectId, args) => {
2975
- const id = `${spaceId}:${objectId}`;
2976
- if (id.length !== FQ_ID_LENGTH) {
2977
- return match;
2978
- }
2979
- const fn = this._remoteFunctions.find((fn2) => fullyQualifiedId(fn2) === id);
2960
+ const binding = formula.replace(/dxn:([^:]+):([^:(]+):([a-zA-Z0-9]+)\((.*)\)/g, (_match, kind, spaceTag, objectId, args) => {
2961
+ const dxn = `dxn:${kind}:${spaceTag}:${objectId}`;
2962
+ const fn = this._remoteFunctions.find((fn2) => Obj2.getDXN(fn2).toString() === dxn);
2980
2963
  if (fn?.binding) {
2981
2964
  return `${fn.binding}(${args})`;
2982
2965
  } else {
@@ -2991,12 +2974,14 @@ var ComputeGraph = class extends Resource2 {
2991
2974
  }
2992
2975
  async _open() {
2993
2976
  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);
2977
+ const query = this._space.db.query(Filter2.type(Operation2.PersistentOperation));
2978
+ const unsubscribe = query.subscribe(() => {
2979
+ this._remoteFunctions = query.results.filter((fn) => fn.binding);
2997
2980
  this.update.emit({
2998
2981
  type: "functionsUpdated"
2999
2982
  });
2983
+ }, {
2984
+ fire: true
3000
2985
  });
3001
2986
  this._ctx.onDispose(unsubscribe);
3002
2987
  }
@@ -3013,25 +2998,24 @@ import defaultsDeep2 from "lodash.defaultsdeep";
3013
2998
  import { Resource as Resource3 } from "@dxos/context";
3014
2999
  import { invariant as invariant3 } from "@dxos/invariant";
3015
3000
  import { log as log4 } from "@dxos/log";
3016
- import { HyperFormula } from "#hyperformula";
3001
+ import { HyperFormula } from "@dxos/vendor-hyperformula";
3017
3002
  var __dxlog_file5 = "/__w/dxos/dxos/packages/core/compute/src/compute-graph-registry.ts";
3018
- var defaultOptions = {
3019
- licenseKey: "gpl-v3"
3020
- };
3021
3003
  var defaultPlugins = [
3022
3004
  {
3023
3005
  plugin: EdgeFunctionPlugin,
3024
3006
  translations: EdgeFunctionPluginTranslations
3025
3007
  }
3026
3008
  ];
3009
+ var defaultOptions = {
3010
+ licenseKey: "gpl-v3"
3011
+ };
3027
3012
  var ComputeGraphRegistry = class extends Resource3 {
3028
3013
  _graphs = /* @__PURE__ */ new Map();
3029
3014
  _options;
3030
- constructor(options = {
3031
- plugins: defaultPlugins
3032
- }) {
3015
+ constructor(options) {
3033
3016
  super();
3034
3017
  this._options = defaultsDeep2({}, options, defaultOptions);
3018
+ this._options.plugins = this._options.plugins ?? defaultPlugins;
3035
3019
  this._options.plugins?.forEach(({ plugin, translations }) => {
3036
3020
  HyperFormula.registerFunctionPlugin(plugin, translations);
3037
3021
  });
@@ -3046,7 +3030,7 @@ var ComputeGraphRegistry = class extends Resource3 {
3046
3030
  space: space.id
3047
3031
  }, {
3048
3032
  F: __dxlog_file5,
3049
- L: 70,
3033
+ L: 73,
3050
3034
  S: this,
3051
3035
  C: (f, a) => f(...a)
3052
3036
  });
@@ -3057,7 +3041,7 @@ var ComputeGraphRegistry = class extends Resource3 {
3057
3041
  createGraph(space) {
3058
3042
  invariant3(!this._graphs.has(space.id), `ComputeGraph already exists for space: ${space.id}`, {
3059
3043
  F: __dxlog_file5,
3060
- L: 78,
3044
+ L: 81,
3061
3045
  S: this,
3062
3046
  A: [
3063
3047
  "!this._graphs.has(space.id)",
@@ -3065,7 +3049,17 @@ var ComputeGraphRegistry = class extends Resource3 {
3065
3049
  ]
3066
3050
  });
3067
3051
  const hf = HyperFormula.buildEmpty(this._options);
3068
- const graph = new ComputeGraph(hf, space, this._options);
3052
+ invariant3(this._options.computeRuntime, "ComputeRuntime is required", {
3053
+ F: __dxlog_file5,
3054
+ L: 83,
3055
+ S: this,
3056
+ A: [
3057
+ "this._options.computeRuntime",
3058
+ "'ComputeRuntime is required'"
3059
+ ]
3060
+ });
3061
+ const runtime = this._options.computeRuntime.getRuntime(space.id);
3062
+ const graph = new ComputeGraph(hf, runtime, space, this._options);
3069
3063
  this._graphs.set(space.id, graph);
3070
3064
  return graph;
3071
3065
  }
@@ -3100,8 +3094,8 @@ export {
3100
3094
  createSheetName,
3101
3095
  parseSheetName,
3102
3096
  ComputeGraph,
3103
- defaultOptions,
3104
3097
  defaultPlugins,
3098
+ defaultOptions,
3105
3099
  ComputeGraphRegistry
3106
3100
  };
3107
- //# sourceMappingURL=chunk-X2FWMHD4.mjs.map
3101
+ //# sourceMappingURL=chunk-JRV42MW3.mjs.map