@dxos/compute 0.8.4-main.ae835ea → 0.8.4-main.bc2380dfbc

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 (174) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +36 -1
  3. package/dist/lib/neutral/StorageService.mjs +22 -0
  4. package/dist/lib/neutral/chunk-AKVCVI6N.mjs +25 -0
  5. package/dist/lib/neutral/chunk-AKVCVI6N.mjs.map +7 -0
  6. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  7. package/dist/lib/neutral/chunk-JTZYM2QW.mjs +144 -0
  8. package/dist/lib/neutral/chunk-JTZYM2QW.mjs.map +7 -0
  9. package/dist/lib/neutral/chunk-LRCABAWU.mjs +17 -0
  10. package/dist/lib/neutral/chunk-LRCABAWU.mjs.map +7 -0
  11. package/dist/lib/neutral/chunk-RCLYF66G.mjs +21 -0
  12. package/dist/lib/neutral/chunk-RCLYF66G.mjs.map +7 -0
  13. package/dist/lib/neutral/chunk-SEBWOTOU.mjs +235 -0
  14. package/dist/lib/neutral/chunk-SEBWOTOU.mjs.map +7 -0
  15. package/dist/lib/neutral/chunk-VSICPGGY.mjs +53 -0
  16. package/dist/lib/neutral/chunk-VSICPGGY.mjs.map +7 -0
  17. package/dist/lib/neutral/chunk-XEVFBI6S.mjs +62 -0
  18. package/dist/lib/neutral/chunk-XEVFBI6S.mjs.map +7 -0
  19. package/dist/lib/neutral/fib-ZSJYREBW.mjs +10 -0
  20. package/dist/lib/neutral/fib-ZSJYREBW.mjs.map +7 -0
  21. package/dist/lib/neutral/index.mjs +1146 -0
  22. package/dist/lib/neutral/index.mjs.map +7 -0
  23. package/dist/lib/neutral/meta.json +1 -0
  24. package/dist/lib/neutral/reply-YH3BVOUT.mjs +10 -0
  25. package/dist/lib/neutral/reply-YH3BVOUT.mjs.map +7 -0
  26. package/dist/lib/neutral/sleep-TSPIQ5EY.mjs +10 -0
  27. package/dist/lib/neutral/sleep-TSPIQ5EY.mjs.map +7 -0
  28. package/dist/lib/neutral/testing/index.mjs +32 -0
  29. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  30. package/dist/types/src/Blueprint.d.ts +192 -0
  31. package/dist/types/src/Blueprint.d.ts.map +1 -0
  32. package/dist/types/src/Credential.d.ts +27 -0
  33. package/dist/types/src/Credential.d.ts.map +1 -0
  34. package/dist/types/src/McpServer.d.ts +27 -0
  35. package/dist/types/src/McpServer.d.ts.map +1 -0
  36. package/dist/types/src/Operation.d.ts +340 -0
  37. package/dist/types/src/Operation.d.ts.map +1 -0
  38. package/dist/types/src/OperationHandlerSet.d.ts +60 -0
  39. package/dist/types/src/OperationHandlerSet.d.ts.map +1 -0
  40. package/dist/types/src/OperationRegistry.d.ts +23 -0
  41. package/dist/types/src/OperationRegistry.d.ts.map +1 -0
  42. package/dist/types/src/Process.d.ts +247 -0
  43. package/dist/types/src/Process.d.ts.map +1 -0
  44. package/dist/types/src/Routine.d.ts +63 -0
  45. package/dist/types/src/Routine.d.ts.map +1 -0
  46. package/dist/types/src/Script.d.ts +22 -0
  47. package/dist/types/src/Script.d.ts.map +1 -0
  48. package/dist/types/src/ServiceResolver.d.ts +74 -0
  49. package/dist/types/src/ServiceResolver.d.ts.map +1 -0
  50. package/dist/types/src/StorageService.d.ts +58 -0
  51. package/dist/types/src/StorageService.d.ts.map +1 -0
  52. package/dist/types/src/Template.d.ts +62 -0
  53. package/dist/types/src/Template.d.ts.map +1 -0
  54. package/dist/types/src/Template.test.d.ts +2 -0
  55. package/dist/types/src/Template.test.d.ts.map +1 -0
  56. package/dist/types/src/Trace.d.ts +216 -0
  57. package/dist/types/src/Trace.d.ts.map +1 -0
  58. package/dist/types/src/Trigger.d.ts +150 -0
  59. package/dist/types/src/Trigger.d.ts.map +1 -0
  60. package/dist/types/src/TriggerEvent.d.ts +75 -0
  61. package/dist/types/src/TriggerEvent.d.ts.map +1 -0
  62. package/dist/types/src/errors.d.ts +308 -0
  63. package/dist/types/src/errors.d.ts.map +1 -0
  64. package/dist/types/src/index.d.ts +16 -5
  65. package/dist/types/src/index.d.ts.map +1 -1
  66. package/dist/types/src/testing/definitions.d.ts +11 -0
  67. package/dist/types/src/testing/definitions.d.ts.map +1 -0
  68. package/dist/types/src/testing/fib.d.ts +8 -0
  69. package/dist/types/src/testing/fib.d.ts.map +1 -0
  70. package/dist/types/src/testing/index.d.ts +6 -4
  71. package/dist/types/src/testing/index.d.ts.map +1 -1
  72. package/dist/types/src/testing/reply.d.ts +4 -0
  73. package/dist/types/src/testing/reply.d.ts.map +1 -0
  74. package/dist/types/src/testing/sleep.d.ts +6 -0
  75. package/dist/types/src/testing/sleep.d.ts.map +1 -0
  76. package/dist/types/src/umbrella.test.d.ts +2 -0
  77. package/dist/types/src/umbrella.test.d.ts.map +1 -0
  78. package/dist/types/tsconfig.tsbuildinfo +1 -1
  79. package/package.json +28 -40
  80. package/src/Blueprint.ts +303 -0
  81. package/src/Credential.ts +53 -0
  82. package/src/McpServer.ts +44 -0
  83. package/src/Operation.ts +593 -0
  84. package/src/OperationHandlerSet.ts +134 -0
  85. package/src/OperationRegistry.ts +50 -0
  86. package/src/Process.ts +490 -0
  87. package/src/Routine.ts +75 -0
  88. package/src/Script.ts +40 -0
  89. package/src/ServiceResolver.ts +174 -0
  90. package/src/StorageService.ts +101 -0
  91. package/src/Template.test.ts +151 -0
  92. package/src/Template.ts +122 -0
  93. package/src/Trace.ts +283 -0
  94. package/src/Trigger.ts +209 -0
  95. package/src/TriggerEvent.ts +64 -0
  96. package/src/errors.ts +71 -0
  97. package/src/index.ts +18 -6
  98. package/src/testing/definitions.ts +49 -0
  99. package/src/testing/fib.ts +22 -0
  100. package/src/testing/index.ts +13 -5
  101. package/src/testing/reply.ts +18 -0
  102. package/src/testing/sleep.ts +16 -0
  103. package/src/umbrella.test.ts +51 -0
  104. package/dist/lib/browser/chunk-CY35BVJC.mjs +0 -3113
  105. package/dist/lib/browser/chunk-CY35BVJC.mjs.map +0 -7
  106. package/dist/lib/browser/index.mjs +0 -55
  107. package/dist/lib/browser/meta.json +0 -1
  108. package/dist/lib/browser/testing/index.mjs +0 -196
  109. package/dist/lib/browser/testing/index.mjs.map +0 -7
  110. package/dist/lib/node-esm/chunk-BECNONV7.mjs +0 -3115
  111. package/dist/lib/node-esm/chunk-BECNONV7.mjs.map +0 -7
  112. package/dist/lib/node-esm/index.mjs +0 -56
  113. package/dist/lib/node-esm/meta.json +0 -1
  114. package/dist/lib/node-esm/testing/index.mjs +0 -197
  115. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  116. package/dist/types/src/compute-graph-registry.d.ts +0 -37
  117. package/dist/types/src/compute-graph-registry.d.ts.map +0 -1
  118. package/dist/types/src/compute-graph-registry.test.d.ts +0 -2
  119. package/dist/types/src/compute-graph-registry.test.d.ts.map +0 -1
  120. package/dist/types/src/compute-graph.d.ts +0 -67
  121. package/dist/types/src/compute-graph.d.ts.map +0 -1
  122. package/dist/types/src/compute-graph.test.d.ts +0 -2
  123. package/dist/types/src/compute-graph.test.d.ts.map +0 -1
  124. package/dist/types/src/compute-node.d.ts +0 -24
  125. package/dist/types/src/compute-node.d.ts.map +0 -1
  126. package/dist/types/src/functions/async-function.d.ts +0 -68
  127. package/dist/types/src/functions/async-function.d.ts.map +0 -1
  128. package/dist/types/src/functions/edge-function.d.ts +0 -20
  129. package/dist/types/src/functions/edge-function.d.ts.map +0 -1
  130. package/dist/types/src/functions/function-defs.d.ts +0 -11
  131. package/dist/types/src/functions/function-defs.d.ts.map +0 -1
  132. package/dist/types/src/functions/index.d.ts +0 -4
  133. package/dist/types/src/functions/index.d.ts.map +0 -1
  134. package/dist/types/src/hyperformula.test.d.ts +0 -2
  135. package/dist/types/src/hyperformula.test.d.ts.map +0 -1
  136. package/dist/types/src/testing/test-builder.d.ts +0 -15
  137. package/dist/types/src/testing/test-builder.d.ts.map +0 -1
  138. package/dist/types/src/testing/test-plugin.d.ts +0 -35
  139. package/dist/types/src/testing/test-plugin.d.ts.map +0 -1
  140. package/dist/types/src/testing/test-runtime.d.ts +0 -10
  141. package/dist/types/src/testing/test-runtime.d.ts.map +0 -1
  142. package/dist/types/src/testing/util.d.ts +0 -2
  143. package/dist/types/src/testing/util.d.ts.map +0 -1
  144. package/dist/types/src/types/hyperformula.d.ts +0 -8
  145. package/dist/types/src/types/hyperformula.d.ts.map +0 -1
  146. package/dist/types/src/types/index.d.ts +0 -3
  147. package/dist/types/src/types/index.d.ts.map +0 -1
  148. package/dist/types/src/types/types.d.ts +0 -20
  149. package/dist/types/src/types/types.d.ts.map +0 -1
  150. package/dist/types/src/types/types.test.d.ts +0 -2
  151. package/dist/types/src/types/types.test.d.ts.map +0 -1
  152. package/dist/types/src/util.d.ts +0 -2
  153. package/dist/types/src/util.d.ts.map +0 -1
  154. package/src/compute-graph-registry.test.ts +0 -138
  155. package/src/compute-graph-registry.ts +0 -97
  156. package/src/compute-graph.test.ts +0 -87
  157. package/src/compute-graph.ts +0 -266
  158. package/src/compute-node.ts +0 -59
  159. package/src/functions/async-function.ts +0 -184
  160. package/src/functions/edge-function.ts +0 -114
  161. package/src/functions/function-defs.ts +0 -2427
  162. package/src/functions/index.ts +0 -7
  163. package/src/hyperformula.test.ts +0 -14
  164. package/src/testing/test-builder.ts +0 -60
  165. package/src/testing/test-plugin.ts +0 -98
  166. package/src/testing/test-runtime.ts +0 -38
  167. package/src/testing/util.ts +0 -9
  168. package/src/types/hyperformula.ts +0 -19
  169. package/src/types/index.ts +0 -6
  170. package/src/types/types.test.ts +0 -91
  171. package/src/types/types.ts +0 -80
  172. package/src/util.ts +0 -8
  173. /package/dist/lib/{browser/index.mjs.map → neutral/StorageService.mjs.map} +0 -0
  174. /package/dist/lib/{node-esm/index.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
@@ -1,184 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import type * as ManagedRuntime from 'effect/ManagedRuntime';
6
- import defaultsDeep from 'lodash.defaultsdeep';
7
-
8
- import { type CleanupFn, debounce } from '@dxos/async';
9
- import type { Space } from '@dxos/client/echo';
10
- import { type FunctionInvocationService } from '@dxos/functions';
11
- import { log } from '@dxos/log';
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';
17
-
18
- // TODO(burdon): Create API gateways:
19
- // https://publicapis.io
20
- // https://api-ninjas.com/api/cryptoprice
21
- // https://developers.google.com/apis-explorer
22
- // https://publicapis.io/coin-desk-api
23
-
24
- export type AsyncFunction = (...args: any) => Promise<InterpreterValue>;
25
-
26
- export type FunctionUpdateEvent = {
27
- name: string;
28
- cell: SimpleCellAddress;
29
- };
30
-
31
- export type FunctionOptions = {
32
- ttl?: number;
33
- };
34
-
35
- export type FunctionContextOptions = {
36
- defaultTtl: number;
37
- debounceDelay: number;
38
- onUpdate?: (update: FunctionUpdateEvent) => void;
39
- };
40
-
41
- export const defaultFunctionContextOptions: Pick<FunctionContextOptions, 'defaultTtl' | 'debounceDelay'> = {
42
- defaultTtl: 5_000,
43
- debounceDelay: 200,
44
- };
45
-
46
- /**
47
- * The context singleton for the model is passed into custom functions.
48
- *
49
- * HyperFormula does not support async functions.
50
- * - https://hyperformula.handsontable.com/guide/custom-functions.html
51
- * - https://hyperformula.handsontable.com/guide/known-limitations.html#known-limitations
52
- * - https://github.com/handsontable/hyperformula/issues/892
53
- */
54
- export class FunctionContext {
55
- // Mangle name with params.
56
- static createInvocationKey(name: string, ...args: any): string {
57
- return JSON.stringify({ name, ...args });
58
- }
59
-
60
- // TODO(wittjosiah): Persist cached values.
61
- // Cached values for cell.
62
- private readonly _cache = new Map<string, { value: InterpreterValue; ts: number }>();
63
-
64
- // Active requests.
65
- private readonly _pending = new Map<string, number>();
66
-
67
- // Query subscriptions.
68
- private readonly _subscriptions = new Map<string, CleanupFn>();
69
-
70
- // Invocation count.
71
- private _invocations: Record<string, number> = {};
72
-
73
- private readonly _options: FunctionContextOptions;
74
-
75
- // Debounced update handler.
76
- private readonly _onUpdate: (update: FunctionUpdateEvent) => void;
77
-
78
- constructor(
79
- private readonly _hf: HyperFormula,
80
- private readonly _runtime: ManagedRuntime.ManagedRuntime<FunctionInvocationService, never>,
81
- private readonly _space: Space | undefined,
82
- _options?: Partial<FunctionContextOptions>,
83
- ) {
84
- this._options = defaultsDeep(_options ?? {}, defaultFunctionContextOptions);
85
- this._onUpdate = debounce((update) => {
86
- log('update', update);
87
- // TODO(burdon): Better way to trigger recalculation? (NOTE: rebuildAndRecalculate resets the undo history.)
88
- this._hf.resumeEvaluation();
89
- this._options.onUpdate?.(update);
90
- }, this._options.debounceDelay);
91
- }
92
-
93
- get space() {
94
- return this._space;
95
- }
96
-
97
- get info() {
98
- return { cache: this._cache.size, invocations: this._invocations };
99
- }
100
-
101
- get runtime() {
102
- return this._runtime;
103
- }
104
-
105
- flush(): void {
106
- this._cache.clear();
107
- this._invocations = {};
108
- this._subscriptions.forEach((unsubscribe) => unsubscribe());
109
- this._subscriptions.clear();
110
- }
111
-
112
- createSubscription(name: string, unsubscribe: CleanupFn): void {
113
- this._subscriptions.get(name)?.();
114
- this._subscriptions.set(name, unsubscribe);
115
- }
116
-
117
- /**
118
- * Exec the function if TTL has expired.
119
- * Return the cached value.
120
- */
121
- invokeFunction(
122
- name: string,
123
- state: InterpreterState,
124
- args: any[],
125
- cb: AsyncFunction,
126
- options?: FunctionOptions,
127
- ): InterpreterValue | undefined {
128
- const ttl = options?.ttl ?? this._options.defaultTtl;
129
-
130
- const { formulaAddress: cell } = state;
131
- const invocationKey = FunctionContext.createInvocationKey(name, ...args);
132
- const { value = undefined, ts = 0 } = this._cache.get(invocationKey) ?? {};
133
-
134
- const now = Date.now();
135
- const delta = now - ts;
136
- if ((!ts || delta > ttl) && !this._pending.has(invocationKey)) {
137
- this._pending.set(invocationKey, now);
138
- setTimeout(async () => {
139
- this._invocations[name] = (this._invocations[name] ?? 0) + 1;
140
- try {
141
- // Exec function.
142
- const value = await cb(...args);
143
- this._cache.set(invocationKey, { value, ts: Date.now() });
144
- log('set', { cell, value });
145
- this._onUpdate({ name, cell });
146
- } catch (err) {
147
- // TODO(burdon): Show error to user.
148
- log.warn('failed', { cell, err });
149
- this._cache.set(invocationKey, { value: new CellError(ErrorType.ERROR, 'Function failed.'), ts: Date.now() });
150
- } finally {
151
- this._pending.delete(invocationKey);
152
- }
153
- });
154
- }
155
-
156
- log.info('invoke', { cell, name, args, cache: value });
157
- return value;
158
- }
159
- }
160
-
161
- /**
162
- * Base class for async functions.
163
- */
164
- export class AsyncFunctionPlugin extends FunctionPlugin {
165
- get context() {
166
- return this.config.context as FunctionContext;
167
- }
168
-
169
- /**
170
- * Immediately returns cached value then runs the async function.
171
- */
172
- protected runAsyncFunction(
173
- ast: ProcedureAst,
174
- state: InterpreterState,
175
- cb: AsyncFunction,
176
- options?: FunctionOptions,
177
- ): RawInterpreterValue {
178
- const { procedureName } = ast;
179
- const metadata = this.metadata(procedureName);
180
- return this.runFunction(ast.args, state, metadata, (...args: any) => {
181
- return this.context.invokeFunction(procedureName, state, args, cb, options) ?? EmptyValue;
182
- });
183
- }
184
- }
@@ -1,114 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { effect } from '@preact/signals-core';
6
- import * as Effect from 'effect/Effect';
7
- import * as SchemaAST from 'effect/SchemaAST';
8
-
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
- import { log } from '@dxos/log';
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';
18
-
19
- import { type AsyncFunction, AsyncFunctionPlugin } from './async-function';
20
-
21
- export const EDGE_FUNCTION_NAME = 'DX';
22
-
23
- const FUNCTION_TTL = 10_000;
24
-
25
- /**
26
- * A hyperformula function plugin for calling remote (EDGE) functions.
27
- * https://hyperformula.handsontable.com/guide/custom-functions.html#add-a-simple-custom-function
28
- */
29
- export class EdgeFunctionPlugin extends AsyncFunctionPlugin {
30
- dx(ast: ProcedureAst, state: InterpreterState) {
31
- const handler =
32
- (subscribe = false): AsyncFunction =>
33
- async (binding: string, ...args: any) => {
34
- const space = this.context.space;
35
- if (!space) {
36
- return new CellError(ErrorType.REF, 'Missing space');
37
- }
38
-
39
- const {
40
- objects: [fn],
41
- } = await space.db.query(Filter.type(Function.Function, { binding })).run();
42
- if (!fn) {
43
- log.info('Function not found', { binding });
44
- return new CellError(ErrorType.REF, 'Function not found');
45
- }
46
-
47
- if (subscribe) {
48
- const unsubscribe = effect(() => {
49
- log('function changed', { fn });
50
- const _ = fn?.version;
51
-
52
- // TODO(wittjosiah): `ttl` should be 0 to force a recalculation when a new version is deployed.
53
- // This needs a ttl to prevent a binding change from causing the function not to be found.
54
- this.runAsyncFunction(ast, state, handler(false), { ttl: FUNCTION_TTL });
55
- });
56
-
57
- this.context.createSubscription(ast.procedureName, unsubscribe);
58
- }
59
-
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;
64
- if (fn.inputSchema) {
65
- const schema = toEffectSchema(fn.inputSchema);
66
- const props = SchemaAST.getPropertySignatures(schema.ast);
67
- input = {} as any;
68
- props.forEach(({ name }, index) => {
69
- input[name.toString()] = args[index];
70
- });
71
- } else {
72
- input = { args: args.filter(isNonNullable) };
73
- }
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;
80
- };
81
-
82
- return this.runAsyncFunction(ast, state, handler(true), { ttl: FUNCTION_TTL });
83
- }
84
- }
85
-
86
- EdgeFunctionPlugin.implementedFunctions = {
87
- [EDGE_FUNCTION_NAME]: {
88
- method: 'dx',
89
- parameters: [
90
- // Binding
91
- { argumentType: FunctionArgumentType.STRING },
92
-
93
- // Remote function arguments (currently supporting up to 8).
94
- { argumentType: FunctionArgumentType.ANY, optionalArg: true },
95
- { argumentType: FunctionArgumentType.ANY, optionalArg: true },
96
- { argumentType: FunctionArgumentType.ANY, optionalArg: true },
97
- { argumentType: FunctionArgumentType.ANY, optionalArg: true },
98
- { argumentType: FunctionArgumentType.ANY, optionalArg: true },
99
- { argumentType: FunctionArgumentType.ANY, optionalArg: true },
100
- { argumentType: FunctionArgumentType.ANY, optionalArg: true },
101
- { argumentType: FunctionArgumentType.ANY, optionalArg: true },
102
- ],
103
- isVolatile: true,
104
- },
105
- };
106
-
107
- export const EdgeFunctionPluginTranslations = {
108
- enGB: {
109
- [EDGE_FUNCTION_NAME]: 'Remote function',
110
- },
111
- enUS: {
112
- [EDGE_FUNCTION_NAME]: 'Remote function',
113
- },
114
- };