@xyo-network/diviner-stateful 2.106.0 → 2.107.0

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 (85) hide show
  1. package/dist/browser/Config.d.cts +0 -9
  2. package/dist/browser/Config.d.cts.map +1 -1
  3. package/dist/browser/Config.d.mts +0 -9
  4. package/dist/browser/Config.d.mts.map +1 -1
  5. package/dist/browser/Config.d.ts +0 -9
  6. package/dist/browser/Config.d.ts.map +1 -1
  7. package/dist/browser/Diviner.d.cts +21 -53
  8. package/dist/browser/Diviner.d.cts.map +1 -1
  9. package/dist/browser/Diviner.d.mts +21 -53
  10. package/dist/browser/Diviner.d.mts.map +1 -1
  11. package/dist/browser/Diviner.d.ts +21 -53
  12. package/dist/browser/Diviner.d.ts.map +1 -1
  13. package/dist/browser/DivinerMixin.d.cts +195 -370
  14. package/dist/browser/DivinerMixin.d.cts.map +1 -1
  15. package/dist/browser/DivinerMixin.d.mts +195 -370
  16. package/dist/browser/DivinerMixin.d.mts.map +1 -1
  17. package/dist/browser/DivinerMixin.d.ts +195 -370
  18. package/dist/browser/DivinerMixin.d.ts.map +1 -1
  19. package/dist/browser/Params.d.cts +0 -3
  20. package/dist/browser/Params.d.cts.map +1 -1
  21. package/dist/browser/Params.d.mts +0 -3
  22. package/dist/browser/Params.d.mts.map +1 -1
  23. package/dist/browser/Params.d.ts +0 -3
  24. package/dist/browser/Params.d.ts.map +1 -1
  25. package/dist/browser/index.cjs +1 -262
  26. package/dist/browser/index.cjs.map +1 -1
  27. package/dist/browser/index.js +1 -241
  28. package/dist/browser/index.js.map +1 -1
  29. package/dist/neutral/Config.d.cts +0 -9
  30. package/dist/neutral/Config.d.cts.map +1 -1
  31. package/dist/neutral/Config.d.mts +0 -9
  32. package/dist/neutral/Config.d.mts.map +1 -1
  33. package/dist/neutral/Config.d.ts +0 -9
  34. package/dist/neutral/Config.d.ts.map +1 -1
  35. package/dist/neutral/Diviner.d.cts +21 -53
  36. package/dist/neutral/Diviner.d.cts.map +1 -1
  37. package/dist/neutral/Diviner.d.mts +21 -53
  38. package/dist/neutral/Diviner.d.mts.map +1 -1
  39. package/dist/neutral/Diviner.d.ts +21 -53
  40. package/dist/neutral/Diviner.d.ts.map +1 -1
  41. package/dist/neutral/DivinerMixin.d.cts +195 -370
  42. package/dist/neutral/DivinerMixin.d.cts.map +1 -1
  43. package/dist/neutral/DivinerMixin.d.mts +195 -370
  44. package/dist/neutral/DivinerMixin.d.mts.map +1 -1
  45. package/dist/neutral/DivinerMixin.d.ts +195 -370
  46. package/dist/neutral/DivinerMixin.d.ts.map +1 -1
  47. package/dist/neutral/Params.d.cts +0 -3
  48. package/dist/neutral/Params.d.cts.map +1 -1
  49. package/dist/neutral/Params.d.mts +0 -3
  50. package/dist/neutral/Params.d.mts.map +1 -1
  51. package/dist/neutral/Params.d.ts +0 -3
  52. package/dist/neutral/Params.d.ts.map +1 -1
  53. package/dist/neutral/index.cjs +1 -262
  54. package/dist/neutral/index.cjs.map +1 -1
  55. package/dist/neutral/index.js +1 -241
  56. package/dist/neutral/index.js.map +1 -1
  57. package/dist/node/Config.d.cts +0 -9
  58. package/dist/node/Config.d.cts.map +1 -1
  59. package/dist/node/Config.d.mts +0 -9
  60. package/dist/node/Config.d.mts.map +1 -1
  61. package/dist/node/Config.d.ts +0 -9
  62. package/dist/node/Config.d.ts.map +1 -1
  63. package/dist/node/Diviner.d.cts +21 -53
  64. package/dist/node/Diviner.d.cts.map +1 -1
  65. package/dist/node/Diviner.d.mts +21 -53
  66. package/dist/node/Diviner.d.mts.map +1 -1
  67. package/dist/node/Diviner.d.ts +21 -53
  68. package/dist/node/Diviner.d.ts.map +1 -1
  69. package/dist/node/DivinerMixin.d.cts +195 -370
  70. package/dist/node/DivinerMixin.d.cts.map +1 -1
  71. package/dist/node/DivinerMixin.d.mts +195 -370
  72. package/dist/node/DivinerMixin.d.mts.map +1 -1
  73. package/dist/node/DivinerMixin.d.ts +195 -370
  74. package/dist/node/DivinerMixin.d.ts.map +1 -1
  75. package/dist/node/Params.d.cts +0 -3
  76. package/dist/node/Params.d.cts.map +1 -1
  77. package/dist/node/Params.d.mts +0 -3
  78. package/dist/node/Params.d.mts.map +1 -1
  79. package/dist/node/Params.d.ts +0 -3
  80. package/dist/node/Params.d.ts.map +1 -1
  81. package/dist/node/index.cjs +1 -285
  82. package/dist/node/index.cjs.map +1 -1
  83. package/dist/node/index.js +1 -257
  84. package/dist/node/index.js.map +1 -1
  85. package/package.json +22 -22
@@ -1 +1 @@
1
- {"version":3,"file":"DivinerMixin.d.ts","sourceRoot":"","sources":["../../src/DivinerMixin.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,eAAe,EAEf,cAAc,EACd,YAAY,EACZ,WAAW,EAEX,eAAe,EAChB,MAAM,2BAA2B,CAAA;AAElC,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAA;AAErD,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAEhD,MAAM,MAAM,oBAAoB,GAAG,YAAY,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAA;AAGvF,MAAM,MAAM,SAAS,CAAC,OAAO,SAAS,oBAAoB,GAAG,oBAAoB,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,cAAc,CAAC,OAAO,CAAC,CAAA;AAIpI;;;;GAIG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0HAKlB,OAAO,6BAd8E,GAAG,EAAE;;IAmBpG;;;;;;OAMG;2BAC0B,SAAS,YAAY,MAAM,CAAC,CAAC;IAU1D;;;;OAIG;;;;;;;;;;;;;;;;;;;;;IASH;;;;OAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAWH;;;;OAIG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAQH;;;OAGG;qBACoB,QAAQ,SAAS,YAAY,MAAM,CAA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erConfig } from './Config';
4
- /**
5
- * The parameters for a Stateful Diviner
6
- */
7
4
  export type StatefulDivinerParams = DivinerParams<AnyConfigSchema<StatefulDivinerConfig>>;
8
5
  //# sourceMappingURL=Params.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Params.d.ts","sourceRoot":"","sources":["../../src/Params.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAE3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAEhD;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,aAAa,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAA"}
1
+ {"version":3,"file":"Params.d.ts","sourceRoot":"","sources":["../../src/Params.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAE3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAKhD,MAAM,MAAM,qBAAqB,GAAG,aAAa,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAA"}
@@ -1,8 +1,5 @@
1
1
  import { DivinerParams } from '@xyo-network/diviner-model';
2
2
  import { AnyConfigSchema } from '@xyo-network/module-model';
3
3
  import { StatefulDivinerConfig } from './Config';
4
- /**
5
- * The parameters for a Stateful Diviner
6
- */
7
4
  export type StatefulDivinerParams = DivinerParams<AnyConfigSchema<StatefulDivinerConfig>>;
8
5
  //# sourceMappingURL=Params.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Params.d.ts","sourceRoot":"","sources":["../../src/Params.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAE3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAEhD;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,aAAa,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAA"}
1
+ {"version":3,"file":"Params.d.ts","sourceRoot":"","sources":["../../src/Params.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAE3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAKhD,MAAM,MAAM,qBAAqB,GAAG,aAAa,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAA"}
@@ -1,8 +1,5 @@
1
1
  import { DivinerParams } from '@xyo-network/diviner-model';
2
2
  import { AnyConfigSchema } from '@xyo-network/module-model';
3
3
  import { StatefulDivinerConfig } from './Config';
4
- /**
5
- * The parameters for a Stateful Diviner
6
- */
7
4
  export type StatefulDivinerParams = DivinerParams<AnyConfigSchema<StatefulDivinerConfig>>;
8
5
  //# sourceMappingURL=Params.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Params.d.ts","sourceRoot":"","sources":["../../src/Params.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAE3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAEhD;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,aAAa,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAA"}
1
+ {"version":3,"file":"Params.d.ts","sourceRoot":"","sources":["../../src/Params.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAE3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAKhD,MAAM,MAAM,qBAAqB,GAAG,aAAa,CAAC,eAAe,CAAC,qBAAqB,CAAC,CAAC,CAAA"}
@@ -1,286 +1,2 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __reflectGet = Reflect.get;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
- var __export = (target, all) => {
11
- for (var name in all)
12
- __defProp(target, name, { get: all[name], enumerable: true });
13
- };
14
- var __copyProps = (to, from, except, desc) => {
15
- if (from && typeof from === "object" || typeof from === "function") {
16
- for (let key of __getOwnPropNames(from))
17
- if (!__hasOwnProp.call(to, key) && key !== except)
18
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
- }
20
- return to;
21
- };
22
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
- var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
25
-
26
- // src/index.ts
27
- var src_exports = {};
28
- __export(src_exports, {
29
- StatefulDiviner: () => StatefulDiviner,
30
- StatefulDivinerConfigSchema: () => StatefulDivinerConfigSchema,
31
- StatefulDivinerSchema: () => StatefulDivinerSchema,
32
- StatefulModuleMixin: () => StatefulModuleMixin
33
- });
34
- module.exports = __toCommonJS(src_exports);
35
-
36
- // src/Schema.ts
37
- var StatefulDivinerSchema = "network.xyo.diviner.stateful";
38
-
39
- // src/Config.ts
40
- var StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config`;
41
-
42
- // src/Diviner.ts
43
- var import_assert = require("@xylabs/assert");
44
- var import_archivist_wrapper = require("@xyo-network/archivist-wrapper");
45
- var import_boundwitness_builder = require("@xyo-network/boundwitness-builder");
46
- var import_boundwitness_model = require("@xyo-network/boundwitness-model");
47
- var import_diviner_abstract = require("@xyo-network/diviner-abstract");
48
- var import_diviner_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
49
- var import_diviner_wrapper = require("@xyo-network/diviner-wrapper");
50
- var import_module_model = require("@xyo-network/module-model");
51
- var import_payload_builder = require("@xyo-network/payload-builder");
52
- var moduleName = "StatefulDiviner";
53
- var _StatefulDiviner = class _StatefulDiviner extends import_diviner_abstract.AbstractDiviner {
54
- /**
55
- * The last state
56
- */
57
- _lastState;
58
- /**
59
- * Commit the internal state of the Diviner process. This is similar
60
- * to a transaction completion in a database and should only be called
61
- * when results have been successfully persisted to the appropriate
62
- * external stores.
63
- * @param nextState The state to commit
64
- */
65
- async commitState(nextState) {
66
- var _a;
67
- if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset)) return;
68
- this._lastState = nextState;
69
- const archivist = await this.getArchivistForStateStore();
70
- const [bw] = await new import_boundwitness_builder.BoundWitnessBuilder().payload(nextState).signer(this.account).build();
71
- await archivist.insert([
72
- bw,
73
- nextState
74
- ]);
75
- }
76
- /**
77
- * Retrieves the archivist for the specified store
78
- * @param store The store to retrieve the archivist for
79
- * @returns The archivist for the specified store
80
- */
81
- async getArchivistForStateStore() {
82
- var _a;
83
- const name = (0, import_assert.assertEx)((_a = this.config) == null ? void 0 : _a.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
84
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
85
- return import_archivist_wrapper.ArchivistWrapper.wrap(mod, this.account);
86
- }
87
- /**
88
- * Retrieves the BoundWitness Diviner for the specified store
89
- * @param store The store to retrieve the BoundWitness Diviner for
90
- * @returns The BoundWitness Diviner for the specified store
91
- */
92
- async getBoundWitnessDivinerForStateStore() {
93
- var _a;
94
- const name = (0, import_assert.assertEx)((_a = this.config) == null ? void 0 : _a.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
95
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
96
- return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
97
- }
98
- /**
99
- * Retrieves the Payload Diviner for the specified store
100
- * @param store The store to retrieve the Payload Diviner for
101
- * @returns The Payload Diviner for the specified store
102
- */
103
- async getPayloadDivinerForStateStore() {
104
- var _a, _b;
105
- const name = (0, import_assert.assertEx)((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`);
106
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
107
- return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
108
- }
109
- /**
110
- * Retrieves the last state of the Diviner process. Used to recover state after
111
- * preemptions, reboots, etc.
112
- */
113
- async retrieveState() {
114
- if (this._lastState) return this._lastState;
115
- let hash = "";
116
- const diviner = await this.getBoundWitnessDivinerForStateStore();
117
- const query = await new import_payload_builder.PayloadBuilder({
118
- schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema
119
- }).fields({
120
- address: this.account.address,
121
- limit: 1,
122
- offset: 0,
123
- order: "desc",
124
- payload_schemas: [
125
- import_module_model.ModuleStateSchema
126
- ]
127
- }).build();
128
- const boundWitnesses = await diviner.divine([
129
- query
130
- ]);
131
- if (boundWitnesses.length > 0) {
132
- const boundWitness = boundWitnesses[0];
133
- if ((0, import_boundwitness_model.isBoundWitness)(boundWitness)) {
134
- hash = boundWitness.addresses.map((address, index) => ({
135
- address,
136
- index
137
- })).filter(({ address }) => address === this.account.address).reduce((prev, curr) => {
138
- var _a;
139
- return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === import_module_model.ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
140
- }, "");
141
- }
142
- }
143
- if (hash) {
144
- const archivist = await this.getArchivistForStateStore();
145
- const payload = (await archivist.get([
146
- hash
147
- ])).find(import_module_model.isModuleState);
148
- if (payload) {
149
- return payload;
150
- }
151
- }
152
- return void 0;
153
- }
154
- };
155
- __name(_StatefulDiviner, "StatefulDiviner");
156
- __publicField(_StatefulDiviner, "configSchemas", [
157
- ...__superGet(_StatefulDiviner, _StatefulDiviner, "configSchemas"),
158
- StatefulDivinerConfigSchema
159
- ]);
160
- __publicField(_StatefulDiviner, "defaultConfigSchema", StatefulDivinerConfigSchema);
161
- var StatefulDiviner = _StatefulDiviner;
162
-
163
- // src/DivinerMixin.ts
164
- var import_assert2 = require("@xylabs/assert");
165
- var import_archivist_model = require("@xyo-network/archivist-model");
166
- var import_boundwitness_builder2 = require("@xyo-network/boundwitness-builder");
167
- var import_boundwitness_model2 = require("@xyo-network/boundwitness-model");
168
- var import_diviner_boundwitness_model2 = require("@xyo-network/diviner-boundwitness-model");
169
- var import_diviner_model = require("@xyo-network/diviner-model");
170
- var import_module_model2 = require("@xyo-network/module-model");
171
- var import_payload_builder2 = require("@xyo-network/payload-builder");
172
- var moduleName2 = "StatefulModuleMixin";
173
- var StatefulModuleMixin = /* @__PURE__ */ __name((ModuleBase) => {
174
- var _a;
175
- let StatefulModuleBase = (_a = class extends ModuleBase {
176
- _lastState;
177
- /**
178
- * Commit the internal state of the Diviner process. This is similar
179
- * to a transaction completion in a database and should only be called
180
- * when results have been successfully persisted to the appropriate
181
- * external stores.
182
- * @param nextState The state to commit
183
- */
184
- async commitState(nextState) {
185
- var _a2;
186
- if (nextState.state.offset === ((_a2 = this._lastState) == null ? void 0 : _a2.state.offset)) return;
187
- this._lastState = nextState;
188
- const archivist = await this.getArchivistForStore();
189
- const [bw] = await (await new import_boundwitness_builder2.BoundWitnessBuilder().payload(nextState)).build();
190
- await archivist.insert([
191
- bw,
192
- nextState
193
- ]);
194
- }
195
- /**
196
- * Retrieves the archivist for the specified store
197
- * @param store The store to retrieve the archivist for
198
- * @returns The archivist for the specified store
199
- */
200
- async getArchivistForStore() {
201
- var _a2, _b;
202
- const name = (0, import_assert2.assertEx)((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
203
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
204
- const instance = (0, import_archivist_model.asArchivistInstance)(mod);
205
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
206
- }
207
- /**
208
- * Retrieves the BoundWitness Diviner for the specified store
209
- * @param store The store to retrieve the BoundWitness Diviner for
210
- * @returns The BoundWitness Diviner for the specified store
211
- */
212
- async getBoundWitnessDivinerForStore() {
213
- var _a2, _b;
214
- const name = (0, import_assert2.assertEx)((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.boundWitnessDiviner, () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`);
215
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
216
- const instance = (0, import_diviner_model.asDivinerInstance)(mod);
217
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
218
- }
219
- /**
220
- * Retrieves the Payload Diviner for the specified store
221
- * @param store The store to retrieve the Payload Diviner for
222
- * @returns The Payload Diviner for the specified store
223
- */
224
- async getPayloadDivinerForStateStore() {
225
- var _a2, _b;
226
- const name = (0, import_assert2.assertEx)((_b = (_a2 = this.config) == null ? void 0 : _a2.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
227
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
228
- const instance = (0, import_diviner_model.asDivinerInstance)(mod);
229
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
230
- }
231
- /**
232
- * Retrieves the last state of the Diviner process. Used to recover state after
233
- * preemptions, reboots, etc.
234
- */
235
- async retrieveState() {
236
- if (this._lastState) return this._lastState;
237
- let hash = "";
238
- const diviner = await this.getBoundWitnessDivinerForStore();
239
- const query = await new import_payload_builder2.PayloadBuilder({
240
- schema: import_diviner_boundwitness_model2.BoundWitnessDivinerQuerySchema
241
- }).fields({
242
- // address: this.account.address,
243
- limit: 1,
244
- offset: 0,
245
- order: "desc",
246
- payload_schemas: [
247
- import_module_model2.ModuleStateSchema
248
- ]
249
- }).build();
250
- const boundWitnesses = await diviner.divine([
251
- query
252
- ]);
253
- if (boundWitnesses.length > 0) {
254
- const boundWitness = boundWitnesses[0];
255
- if ((0, import_boundwitness_model2.isBoundWitness)(boundWitness)) {
256
- hash = boundWitness.addresses.map((address, index) => ({
257
- address,
258
- index
259
- })).reduce((prev, curr) => {
260
- var _a2;
261
- return ((_a2 = boundWitness.payload_schemas) == null ? void 0 : _a2[curr == null ? void 0 : curr.index]) === import_module_model2.ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
262
- }, "");
263
- }
264
- }
265
- if (hash) {
266
- const archivist = await this.getArchivistForStore();
267
- const payload = (await archivist.get([
268
- hash
269
- ])).find(import_module_model2.isModuleState);
270
- if (payload) {
271
- return payload;
272
- }
273
- }
274
- return void 0;
275
- }
276
- }, __name(_a, "StatefulModuleBase"), _a);
277
- return StatefulModuleBase;
278
- }, "StatefulModuleMixin");
279
- // Annotate the CommonJS export names for ESM import in node:
280
- 0 && (module.exports = {
281
- StatefulDiviner,
282
- StatefulDivinerConfigSchema,
283
- StatefulDivinerSchema,
284
- StatefulModuleMixin
285
- });
1
+ "use strict";var S=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var G=Object.getPrototypeOf,H=Object.prototype.hasOwnProperty;var J=Reflect.get;var K=(o,t,i)=>t in o?S(o,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):o[t]=i;var u=(o,t)=>S(o,"name",{value:t,configurable:!0});var L=(o,t)=>{for(var i in t)S(o,i,{get:t[i],enumerable:!0})},O=(o,t,i,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of z(t))!H.call(o,e)&&e!==i&&S(o,e,{get:()=>t[e],enumerable:!(r=j(t,e))||r.enumerable});return o};var R=o=>O(S({},"__esModule",{value:!0}),o);var D=(o,t,i)=>K(o,typeof t!="symbol"?t+"":t,i);var x=(o,t,i)=>J(G(o),i,t);var U={};L(U,{StatefulDiviner:()=>b,StatefulDivinerConfigSchema:()=>y,StatefulDivinerSchema:()=>W,StatefulModuleMixin:()=>T});module.exports=R(U);var W="network.xyo.diviner.stateful";var y=`${W}.config`;var m=require("@xylabs/assert"),M=require("@xyo-network/archivist-wrapper"),_=require("@xyo-network/boundwitness-builder"),A=require("@xyo-network/boundwitness-model"),C=require("@xyo-network/diviner-abstract"),P=require("@xyo-network/diviner-boundwitness-model"),F=require("@xyo-network/diviner-wrapper"),w=require("@xyo-network/module-model"),q=require("@xyo-network/payload-builder");var p="StatefulDiviner",h=class h extends C.AbstractDiviner{_lastState;async commitState(t){var e;if(t.state.offset===((e=this._lastState)==null?void 0:e.state.offset))return;this._lastState=t;let i=await this.getArchivistForStateStore(),[r]=await new _.BoundWitnessBuilder().payload(t).signer(this.account).build();await i.insert([r,t])}async getArchivistForStateStore(){var r;let t=(0,m.assertEx)((r=this.config)==null?void 0:r.stateStore.archivist,()=>`${p}: Config for stateStore.archivist not specified`),i=(0,m.assertEx)(await this.resolve(t),()=>`${p}: Failed to resolve stateStore.archivist`);return M.ArchivistWrapper.wrap(i,this.account)}async getBoundWitnessDivinerForStateStore(){var r;let t=(0,m.assertEx)((r=this.config)==null?void 0:r.stateStore.boundWitnessDiviner,()=>`${p}: Config for stateStore.boundWitnessDiviner not specified`),i=(0,m.assertEx)(await this.resolve(t),()=>`${p}: Failed to resolve stateStore.boundWitnessDiviner`);return F.DivinerWrapper.wrap(i,this.account)}async getPayloadDivinerForStateStore(){var r,e;let t=(0,m.assertEx)((e=(r=this.config)==null?void 0:r.stateStore)==null?void 0:e.payloadDiviner,()=>`${p}: Config for stateStore.payloadDiviner not specified`),i=(0,m.assertEx)(await this.resolve(t),()=>`${p}: Failed to resolve stateStore.payloadDiviner`);return F.DivinerWrapper.wrap(i,this.account)}async retrieveState(){if(this._lastState)return this._lastState;let t="",i=await this.getBoundWitnessDivinerForStateStore(),r=await new q.PayloadBuilder({schema:P.BoundWitnessDivinerQuerySchema}).fields({address:this.account.address,limit:1,offset:0,order:"desc",payload_schemas:[w.ModuleStateSchema]}).build(),e=await i.divine([r]);if(e.length>0){let d=e[0];(0,A.isBoundWitness)(d)&&(t=d.addresses.map((a,s)=>({address:a,index:s})).filter(({address:a})=>a===this.account.address).reduce((a,s)=>{var n;return((n=d.payload_schemas)==null?void 0:n[s==null?void 0:s.index])===w.ModuleStateSchema?d.payload_hashes[s==null?void 0:s.index]:a},""))}if(t){let a=(await(await this.getArchivistForStateStore()).get([t])).find(w.isModuleState);if(a)return a}}};u(h,"StatefulDiviner"),D(h,"configSchemas",[...x(h,h,"configSchemas"),y]),D(h,"defaultConfigSchema",y);var b=h;var c=require("@xylabs/assert"),E=require("@xyo-network/archivist-model"),I=require("@xyo-network/boundwitness-builder"),N=require("@xyo-network/boundwitness-model"),Q=require("@xyo-network/diviner-boundwitness-model"),B=require("@xyo-network/diviner-model"),g=require("@xyo-network/module-model"),k=require("@xyo-network/payload-builder");var f="StatefulModuleMixin",T=u(o=>{var i;return i=class extends o{_lastState;async commitState(e){var s;if(e.state.offset===((s=this._lastState)==null?void 0:s.state.offset))return;this._lastState=e;let d=await this.getArchivistForStore(),[a]=await(await new I.BoundWitnessBuilder().payload(e)).build();await d.insert([a,e])}async getArchivistForStore(){var s,n;let e=(0,c.assertEx)((n=(s=this.config)==null?void 0:s.stateStore)==null?void 0:n.archivist,()=>`${f}: Config for stateStore.archivist not specified`),d=(0,c.assertEx)(await this.resolve(e),()=>`${f}: Failed to resolve stateStore.archivist`),a=(0,E.asArchivistInstance)(d);return(0,c.assertEx)(a,()=>`${f}: Failed to wrap archivist instance`)}async getBoundWitnessDivinerForStore(){var s,n;let e=(0,c.assertEx)((n=(s=this.config)==null?void 0:s.stateStore)==null?void 0:n.boundWitnessDiviner,()=>`${f}: Config for stateStore.boundWitnessDiviner not specified`),d=(0,c.assertEx)(await this.resolve(e),()=>`${f}: Failed to resolve stateStore.boundWitnessDiviner`),a=(0,B.asDivinerInstance)(d);return(0,c.assertEx)(a,()=>`${f}: Failed to wrap diviner instance`)}async getPayloadDivinerForStateStore(){var s,n;let e=(0,c.assertEx)((n=(s=this.config)==null?void 0:s.stateStore)==null?void 0:n.payloadDiviner,()=>`${f}: Config for stateStore.payloadDiviner not specified`),d=(0,c.assertEx)(await this.resolve(e),()=>`${f}: Failed to resolve stateStore.payloadDiviner`),a=(0,B.asDivinerInstance)(d);return(0,c.assertEx)(a,()=>`${f}: Failed to wrap diviner instance`)}async retrieveState(){if(this._lastState)return this._lastState;let e="",d=await this.getBoundWitnessDivinerForStore(),a=await new k.PayloadBuilder({schema:Q.BoundWitnessDivinerQuerySchema}).fields({limit:1,offset:0,order:"desc",payload_schemas:[g.ModuleStateSchema]}).build(),s=await d.divine([a]);if(s.length>0){let n=s[0];(0,N.isBoundWitness)(n)&&(e=n.addresses.map((v,l)=>({address:v,index:l})).reduce((v,l)=>{var $;return(($=n.payload_schemas)==null?void 0:$[l==null?void 0:l.index])===g.ModuleStateSchema?n.payload_hashes[l==null?void 0:l.index]:v},""))}if(e){let v=(await(await this.getArchivistForStore()).get([e])).find(g.isModuleState);if(v)return v}}},u(i,"StatefulModuleBase"),i},"StatefulModuleMixin");0&&(module.exports={StatefulDiviner,StatefulDivinerConfigSchema,StatefulDivinerSchema,StatefulModuleMixin});
286
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './DivinerMixin'\nexport * from './Params'\nexport * from './Schema'\n","export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import { DivinerConfig } from '@xyo-network/diviner-model'\nimport { ModuleIdentifier } from '@xyo-network/module-model'\n\nimport { StatefulDivinerSchema } from './Schema'\n\n/**\n * The schema for a Stateful Diviner config\n */\nexport const StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config` as const\n/**\n * The schema for a Stateful Diviner config\n */\nexport type StatefulDivinerConfigSchema = typeof StatefulDivinerConfigSchema\n\n/**\n * The config for a Stateful Diviner\n */\nexport type StatefulDivinerConfig = DivinerConfig<{\n schema: StatefulDivinerConfigSchema\n stateStore: {\n archivist: ModuleIdentifier\n boundWitnessDiviner: ModuleIdentifier\n payloadDiviner: ModuleIdentifier\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport { isModuleState, ModuleState, ModuleStateSchema, StateDictionary } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfigSchema } from './Config'\nimport { StatefulDivinerParams } from './Params'\n\nconst moduleName = 'StatefulDiviner'\n\n/**\n * A Diviner that maintains state\n */\nexport abstract class StatefulDiviner<\n TParams extends StatefulDivinerParams = StatefulDivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, StatefulDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = StatefulDivinerConfigSchema\n\n /**\n * The last state\n */\n protected _lastState?: WithMeta<ModuleState<TState>>\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n protected async commitState(nextState: WithMeta<ModuleState<TState>>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStateStore()\n const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n protected async getArchivistForStateStore() {\n const name = assertEx(this.config?.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)\n return ArchivistWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n protected async getBoundWitnessDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n protected async getPayloadDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n protected async retrieveState(): Promise<WithMeta<ModuleState<TState>> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStateStore()\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const archivist = await this.getArchivistForStateStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as WithMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { asArchivistInstance } from '@xyo-network/archivist-model'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport {\n AnyConfigSchema,\n isModuleState,\n ModuleInstance,\n ModuleParams,\n ModuleState,\n ModuleStateSchema,\n StateDictionary,\n} from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { WithMeta } from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfig } from './Config'\n\nexport type StatefulModuleParams = ModuleParams<AnyConfigSchema<StatefulDivinerConfig>>\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyModule<TParams extends StatefulModuleParams = StatefulModuleParams> = new (...args: any[]) => ModuleInstance<TParams>\n\nconst moduleName = 'StatefulModuleMixin'\n\n/**\n * @ignore Inherit from StatefulDiviner instead\n * @param ModuleBase\n * @returns\n */\nexport const StatefulModuleMixin = <\n TParams extends StatefulModuleParams = StatefulModuleParams,\n TModule extends AnyModule<TParams> = AnyModule<TParams>,\n TState extends StateDictionary = StateDictionary,\n>(\n ModuleBase: TModule,\n) => {\n abstract class StatefulModuleBase extends ModuleBase {\n _lastState?: WithMeta<ModuleState<TState>>\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n async commitState(nextState: WithMeta<ModuleState<TState>>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStore()\n // const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n const [bw] = await (await new BoundWitnessBuilder().payload(nextState)).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n async getArchivistForStore() {\n const name = assertEx(this.config?.stateStore?.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)\n // return ArchivistWrapper.wrap(mod, this.account)\n const instance = asArchivistInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap archivist instance`)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n async getBoundWitnessDivinerForStore() {\n const name = assertEx(\n this.config?.stateStore?.boundWitnessDiviner,\n () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`,\n )\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)\n // return DivinerWrapper.wrap(mod, this.account)\n const instance = asDivinerInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)\n }\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n async getPayloadDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)\n // return DivinerWrapper.wrap(mod, this.account)\n const instance = asDivinerInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)\n }\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n async retrieveState(): Promise<WithMeta<ModuleState<TState>> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStore()\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n // address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n // .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const archivist = await this.getArchivistForStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as WithMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;ACAO,IAAMA,wBAAwB;;;ACQ9B,IAAMC,8BAA8B,GAAGC,qBAAAA;;;ACR9C,oBAAyB;AAEzB,+BAAiC;AACjC,kCAAoC;AACpC,gCAA+B;AAC/B,8BAAgC;AAChC,wCAAgF;AAEhF,6BAA+B;AAC/B,0BAA+E;AAC/E,6BAA+B;AAM/B,IAAMC,aAAa;AAKZ,IAAeC,mBAAf,MAAeA,yBAUZC,wCAAAA;;;;EAOEC;;;;;;;;EASV,MAAgBC,YAAYC,WAA0C;AA/CxE;AAiDI,QAAIA,UAAUC,MAAMC,aAAW,UAAKJ,eAAL,mBAAiBG,MAAMC,QAAQ;AAC9D,SAAKJ,aAAaE;AAClB,UAAMG,YAAY,MAAM,KAAKC,0BAAyB;AACtD,UAAM,CAACC,EAAAA,IAAM,MAAM,IAAIC,gDAAAA,EAAsBC,QAAQ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AO,MAAM,GAAGjB,WAAAA,+CAAyD;AAEjH,YAAMuB,eAAWI,wCAAkBN,GAAAA;AACnC,iBAAOH,yBAASK,UAAU,MAAM,GAAGvB,WAAAA,mCAA6C;IAClF;;;;;IAKA,MAAM8B,gBAAoE;AACxE,UAAI,KAAK1B,WAAY,QAAO,KAAKA;AACjC,UAAI2B,OAAa;AACjB,YAAMC,UAAU,MAAM,KAAKP,+BAA8B;AACzD,YAAMQ,QAAQ,MAAM,IAAIC,uCAAgD;QAAEC,QAAQC;MAA+B,CAAA,EAC9GC,OAAO;;QAENC,OAAO;QACP7B,QAAQ;QACR8B,OAAO;QACPC,iBAAiB;UAACC;;MACpB,CAAA,EACC1B,MAAK;AACR,YAAM2B,iBAAiB,MAAMV,QAAQW,OAAO;QAACV;OAAM;AACnD,UAAIS,eAAeE,SAAS,GAAG;AAC7B,cAAMC,eAAeH,eAAe,CAAA;AACpC,gBAAII,2CAAeD,YAAAA,GAAe;AAEhCd,iBAAOc,aAAaE,UACjBC,IAAI,CAACC,SAASC,WAAW;YAAED;YAASC;UAAM,EAAA,EAG1CC,OACC,CAACC,MAAMC,SAAAA;AA/HrB,gBAAA9C;AA+H+BsC,qBAAAA,MAAAA,aAAaL,oBAAbK,gBAAAA,IAA+BQ,6BAAMH,YAAWT,yCAAoBI,aAAaS,eAAeD,6BAAMH,KAAAA,IAASE;aAChI,EAAA;QAEN;MACF;AAGA,UAAIrB,MAAM;AAER,cAAMrB,YAAY,MAAM,KAAKC,qBAAoB;AACjD,cAAMG,WAAW,MAAMJ,UAAU6C,IAAI;UAACxB;SAAK,GAAGyB,KAAKC,kCAAAA;AACnD,YAAI3C,SAAS;AACX,iBAAOA;QACT;MACF;AACA,aAAO4C;IACT;EACF,GAxG0CxD,kCAA1C;AAyGA,SAAOC;AACT,GAjHmC;","names":["StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","moduleName","StatefulDiviner","AbstractDiviner","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStateStore","bw","BoundWitnessBuilder","payload","signer","account","build","insert","name","assertEx","config","stateStore","mod","resolve","ArchivistWrapper","wrap","getBoundWitnessDivinerForStateStore","boundWitnessDiviner","DivinerWrapper","getPayloadDivinerForStateStore","payloadDiviner","retrieveState","hash","diviner","query","PayloadBuilder","schema","BoundWitnessDivinerQuerySchema","fields","address","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","divine","length","boundWitness","isBoundWitness","addresses","map","index","filter","reduce","prev","curr","payload_hashes","get","find","isModuleState","undefined","configSchemas","StatefulDivinerConfigSchema","defaultConfigSchema","import_assert","import_boundwitness_builder","import_boundwitness_model","import_diviner_boundwitness_model","import_module_model","import_payload_builder","moduleName","StatefulModuleMixin","ModuleBase","StatefulModuleBase","_lastState","commitState","nextState","_a","state","offset","archivist","getArchivistForStore","bw","BoundWitnessBuilder","payload","build","insert","name","assertEx","config","stateStore","mod","resolve","instance","asArchivistInstance","getBoundWitnessDivinerForStore","boundWitnessDiviner","asDivinerInstance","getPayloadDivinerForStateStore","payloadDiviner","retrieveState","hash","diviner","query","PayloadBuilder","schema","BoundWitnessDivinerQuerySchema","fields","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","divine","length","boundWitness","isBoundWitness","addresses","map","address","index","reduce","prev","curr","payload_hashes","get","find","isModuleState","undefined"]}
1
+ {"version":3,"sources":["../../src/index.ts","../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["export * from './Config'\nexport * from './Diviner'\nexport * from './DivinerMixin'\nexport * from './Params'\nexport * from './Schema'\n","export const StatefulDivinerSchema = 'network.xyo.diviner.stateful' as const\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n","import { DivinerConfig } from '@xyo-network/diviner-model'\nimport { ModuleIdentifier } from '@xyo-network/module-model'\n\nimport { StatefulDivinerSchema } from './Schema'\n\n/**\n * The schema for a Stateful Diviner config\n */\nexport const StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config` as const\n/**\n * The schema for a Stateful Diviner config\n */\nexport type StatefulDivinerConfigSchema = typeof StatefulDivinerConfigSchema\n\n/**\n * The config for a Stateful Diviner\n */\nexport type StatefulDivinerConfig = DivinerConfig<{\n schema: StatefulDivinerConfigSchema\n stateStore: {\n archivist: ModuleIdentifier\n boundWitnessDiviner: ModuleIdentifier\n payloadDiviner: ModuleIdentifier\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { AbstractDiviner } from '@xyo-network/diviner-abstract'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/diviner-wrapper'\nimport { isModuleState, ModuleState, ModuleStateSchema, StateDictionary } from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { Payload, Schema, WithMeta } from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfigSchema } from './Config'\nimport { StatefulDivinerParams } from './Params'\n\nconst moduleName = 'StatefulDiviner'\n\n/**\n * A Diviner that maintains state\n */\nexport abstract class StatefulDiviner<\n TParams extends StatefulDivinerParams = StatefulDivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, StatefulDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = StatefulDivinerConfigSchema\n\n /**\n * The last state\n */\n protected _lastState?: WithMeta<ModuleState<TState>>\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n protected async commitState(nextState: WithMeta<ModuleState<TState>>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStateStore()\n const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n protected async getArchivistForStateStore() {\n const name = assertEx(this.config?.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)\n return ArchivistWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n protected async getBoundWitnessDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n protected async getPayloadDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)\n return DivinerWrapper.wrap(mod, this.account)\n }\n\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n protected async retrieveState(): Promise<WithMeta<ModuleState<TState>> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStateStore()\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const archivist = await this.getArchivistForStateStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as WithMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { Hash } from '@xylabs/hex'\nimport { asArchivistInstance } from '@xyo-network/archivist-model'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { asDivinerInstance } from '@xyo-network/diviner-model'\nimport {\n AnyConfigSchema,\n isModuleState,\n ModuleInstance,\n ModuleParams,\n ModuleState,\n ModuleStateSchema,\n StateDictionary,\n} from '@xyo-network/module-model'\nimport { PayloadBuilder } from '@xyo-network/payload-builder'\nimport { WithMeta } from '@xyo-network/payload-model'\n\nimport { StatefulDivinerConfig } from './Config'\n\nexport type StatefulModuleParams = ModuleParams<AnyConfigSchema<StatefulDivinerConfig>>\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type AnyModule<TParams extends StatefulModuleParams = StatefulModuleParams> = new (...args: any[]) => ModuleInstance<TParams>\n\nconst moduleName = 'StatefulModuleMixin'\n\n/**\n * @ignore Inherit from StatefulDiviner instead\n * @param ModuleBase\n * @returns\n */\nexport const StatefulModuleMixin = <\n TParams extends StatefulModuleParams = StatefulModuleParams,\n TModule extends AnyModule<TParams> = AnyModule<TParams>,\n TState extends StateDictionary = StateDictionary,\n>(\n ModuleBase: TModule,\n) => {\n abstract class StatefulModuleBase extends ModuleBase {\n _lastState?: WithMeta<ModuleState<TState>>\n\n /**\n * Commit the internal state of the Diviner process. This is similar\n * to a transaction completion in a database and should only be called\n * when results have been successfully persisted to the appropriate\n * external stores.\n * @param nextState The state to commit\n */\n async commitState(nextState: WithMeta<ModuleState<TState>>) {\n // Don't commit state if no state has changed\n if (nextState.state.offset === this._lastState?.state.offset) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStore()\n // const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n const [bw] = await (await new BoundWitnessBuilder().payload(nextState)).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n async getArchivistForStore() {\n const name = assertEx(this.config?.stateStore?.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)\n // return ArchivistWrapper.wrap(mod, this.account)\n const instance = asArchivistInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap archivist instance`)\n }\n\n /**\n * Retrieves the BoundWitness Diviner for the specified store\n * @param store The store to retrieve the BoundWitness Diviner for\n * @returns The BoundWitness Diviner for the specified store\n */\n async getBoundWitnessDivinerForStore() {\n const name = assertEx(\n this.config?.stateStore?.boundWitnessDiviner,\n () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`,\n )\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)\n // return DivinerWrapper.wrap(mod, this.account)\n const instance = asDivinerInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)\n }\n /**\n * Retrieves the Payload Diviner for the specified store\n * @param store The store to retrieve the Payload Diviner for\n * @returns The Payload Diviner for the specified store\n */\n async getPayloadDivinerForStateStore() {\n const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)\n const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)\n // return DivinerWrapper.wrap(mod, this.account)\n const instance = asDivinerInstance(mod)\n return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)\n }\n /**\n * Retrieves the last state of the Diviner process. Used to recover state after\n * preemptions, reboots, etc.\n */\n async retrieveState(): Promise<WithMeta<ModuleState<TState>> | undefined> {\n if (this._lastState) return this._lastState\n let hash: Hash = ''\n const diviner = await this.getBoundWitnessDivinerForStore()\n const query = await new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n // address: this.account.address,\n limit: 1,\n offset: 0,\n order: 'desc',\n payload_schemas: [ModuleStateSchema],\n })\n .build()\n const boundWitnesses = await diviner.divine([query])\n if (boundWitnesses.length > 0) {\n const boundWitness = boundWitnesses[0]\n if (isBoundWitness(boundWitness)) {\n // Find the index for this address in the BoundWitness that is a ModuleState\n hash = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n // .filter(({ address }) => address === this.account.address)\n // eslint-disable-next-line unicorn/no-array-reduce\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '' as Hash,\n )\n }\n }\n\n // If we able to located the last state\n if (hash) {\n // Get last state\n const archivist = await this.getArchivistForStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as WithMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":"yqBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,EAAA,gCAAAC,EAAA,0BAAAC,EAAA,wBAAAC,IAAA,eAAAC,EAAAN,GCAO,IAAMO,EAAwB,+BCQ9B,IAAMC,EAA8B,GAAGC,CAAAA,UCR9C,IAAAC,EAAyB,0BAEzBC,EAAiC,0CACjCC,EAAoC,6CACpCC,EAA+B,2CAC/BC,EAAgC,yCAChCC,EAAgF,mDAEhFC,EAA+B,wCAC/BC,EAA+E,qCAC/EC,EAA+B,wCAM/B,IAAMC,EAAa,kBAKGC,EAAf,MAAeA,UAUZC,iBAAAA,CAOEC,WASV,MAAgBC,YAAYC,EAA0C,CA/CxE,IAAAC,EAiDI,GAAID,EAAUE,MAAMC,WAAWF,EAAA,KAAKH,aAAL,YAAAG,EAAiBC,MAAMC,QAAQ,OAC9D,KAAKL,WAAaE,EAClB,IAAMI,EAAY,MAAM,KAAKC,0BAAyB,EAChD,CAACC,CAAAA,EAAM,MAAM,IAAIC,sBAAAA,EAAsBC,QAAQR,CAAAA,EAAWS,OAAO,KAAKC,OAAO,EAAEC,MAAK,EAC1F,MAAMP,EAAUQ,OAAO,CAACN,EAAIN,EAAU,CACxC,CAOA,MAAgBK,2BAA4B,CA7D9C,IAAAJ,EA8DI,IAAMY,KAAOC,aAASb,EAAA,KAAKc,SAAL,YAAAd,EAAae,WAAWZ,UAAW,IAAM,GAAGT,CAAAA,iDAA2D,EACvHsB,KAAMH,YAAS,MAAM,KAAKI,QAAQL,CAAAA,EAAO,IAAM,GAAGlB,CAAAA,0CAAoD,EAC5G,OAAOwB,mBAAiBC,KAAKH,EAAK,KAAKP,OAAO,CAChD,CAOA,MAAgBW,qCAAsC,CAxExD,IAAApB,EAyEI,IAAMY,KAAOC,aAASb,EAAA,KAA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,IAAMC,EAAeH,EAAe,CAAA,KAChCI,kBAAeD,CAAAA,IAEjBd,EAAOc,EAAaE,UACjBC,IAAI,CAACC,EAASC,KAAW,CAAED,QAAAA,EAASC,MAAAA,CAAM,EAAA,EAG1CC,OACC,CAACC,EAAMC,IAAAA,CA/HrB,IAAAlD,EA+H+B0C,QAAAA,EAAAA,EAAaL,kBAAbK,YAAAA,EAA+BQ,GAAAA,YAAAA,EAAMH,UAAWT,oBAAoBI,EAAaS,eAAeD,GAAAA,YAAAA,EAAMH,KAAAA,EAASE,GAChI,EAAA,EAGR,CAGA,GAAIrB,EAAM,CAGR,IAAMlB,GAAW,MADC,MAAM,KAAKH,qBAAoB,GAChB6C,IAAI,CAACxB,EAAK,GAAGyB,KAAKC,eAAAA,EACnD,GAAI5C,EACF,OAAOA,CAEX,CAEF,CACF,EAxG0CX,EAAAA,EAAAA,sBAA1CC,CA0GF,EAjHmC","names":["src_exports","__export","StatefulDiviner","StatefulDivinerConfigSchema","StatefulDivinerSchema","StatefulModuleMixin","__toCommonJS","StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","import_assert","import_archivist_wrapper","import_boundwitness_builder","import_boundwitness_model","import_diviner_abstract","import_diviner_boundwitness_model","import_diviner_wrapper","import_module_model","import_payload_builder","moduleName","StatefulDiviner","AbstractDiviner","_lastState","commitState","nextState","_a","state","offset","archivist","getArchivistForStateStore","bw","BoundWitnessBuilder","payload","signer","account","build","insert","name","assertEx","config","stateStore","mod","resolve","ArchivistWrapper","wrap","getBoundWitnessDivinerForStateStore","boundWitnessDiviner","DivinerWrapper","getPayloadDivinerForStateStore","_b","payloadDiviner","retrieveState","hash","diviner","query","PayloadBuilder","schema","BoundWitnessDivinerQuerySchema","fields","address","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","divine","length","boundWitness","isBoundWitness","addresses","map","index","filter","reduce","prev","curr","payload_hashes","get","find","isModuleState","__publicField","configSchemas","__superGet","_StatefulDiviner","StatefulDivinerConfigSchema","defaultConfigSchema","import_assert","import_archivist_model","import_boundwitness_builder","import_boundwitness_model","import_diviner_boundwitness_model","import_diviner_model","import_module_model","import_payload_builder","moduleName","StatefulModuleMixin","__name","ModuleBase","_a","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStore","bw","BoundWitnessBuilder","payload","build","insert","_b","name","assertEx","config","stateStore","mod","resolve","instance","asArchivistInstance","getBoundWitnessDivinerForStore","boundWitnessDiviner","asDivinerInstance","getPayloadDivinerForStateStore","payloadDiviner","retrieveState","hash","diviner","query","PayloadBuilder","schema","BoundWitnessDivinerQuerySchema","fields","limit","order","payload_schemas","ModuleStateSchema","boundWitnesses","divine","length","boundWitness","isBoundWitness","addresses","map","address","index","reduce","prev","curr","payload_hashes","get","find","isModuleState"]}