@xyo-network/diviner-stateful 2.99.1 → 2.99.3

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 (41) hide show
  1. package/dist/neutral/Config.d.cts +22 -0
  2. package/dist/neutral/Config.d.cts.map +1 -0
  3. package/dist/neutral/Config.d.mts +22 -0
  4. package/dist/neutral/Config.d.mts.map +1 -0
  5. package/dist/neutral/Config.d.ts +22 -0
  6. package/dist/neutral/Config.d.ts.map +1 -0
  7. package/dist/neutral/Diviner.d.cts +100 -0
  8. package/dist/neutral/Diviner.d.cts.map +1 -0
  9. package/dist/neutral/Diviner.d.mts +100 -0
  10. package/dist/neutral/Diviner.d.mts.map +1 -0
  11. package/dist/neutral/Diviner.d.ts +100 -0
  12. package/dist/neutral/Diviner.d.ts.map +1 -0
  13. package/dist/neutral/DivinerMixin.d.cts +515 -0
  14. package/dist/neutral/DivinerMixin.d.cts.map +1 -0
  15. package/dist/neutral/DivinerMixin.d.mts +515 -0
  16. package/dist/neutral/DivinerMixin.d.mts.map +1 -0
  17. package/dist/neutral/DivinerMixin.d.ts +515 -0
  18. package/dist/neutral/DivinerMixin.d.ts.map +1 -0
  19. package/dist/neutral/Params.d.cts +8 -0
  20. package/dist/neutral/Params.d.cts.map +1 -0
  21. package/dist/neutral/Params.d.mts +8 -0
  22. package/dist/neutral/Params.d.mts.map +1 -0
  23. package/dist/neutral/Params.d.ts +8 -0
  24. package/dist/neutral/Params.d.ts.map +1 -0
  25. package/dist/neutral/Schema.d.cts +3 -0
  26. package/dist/neutral/Schema.d.cts.map +1 -0
  27. package/dist/neutral/Schema.d.mts +3 -0
  28. package/dist/neutral/Schema.d.mts.map +1 -0
  29. package/dist/neutral/Schema.d.ts +3 -0
  30. package/dist/neutral/Schema.d.ts.map +1 -0
  31. package/dist/neutral/index.cjs +267 -0
  32. package/dist/neutral/index.cjs.map +1 -0
  33. package/dist/neutral/index.d.cts +6 -0
  34. package/dist/neutral/index.d.cts.map +1 -0
  35. package/dist/neutral/index.d.mts +6 -0
  36. package/dist/neutral/index.d.mts.map +1 -0
  37. package/dist/neutral/index.d.ts +6 -0
  38. package/dist/neutral/index.d.ts.map +1 -0
  39. package/dist/neutral/index.js +246 -0
  40. package/dist/neutral/index.js.map +1 -0
  41. package/package.json +23 -23
@@ -0,0 +1,267 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var src_exports = {};
23
+ __export(src_exports, {
24
+ StatefulDiviner: () => StatefulDiviner,
25
+ StatefulDivinerConfigSchema: () => StatefulDivinerConfigSchema,
26
+ StatefulDivinerSchema: () => StatefulDivinerSchema,
27
+ StatefulModuleMixin: () => StatefulModuleMixin
28
+ });
29
+ module.exports = __toCommonJS(src_exports);
30
+
31
+ // src/Schema.ts
32
+ var StatefulDivinerSchema = "network.xyo.diviner.stateful";
33
+
34
+ // src/Config.ts
35
+ var StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config`;
36
+
37
+ // src/Diviner.ts
38
+ var import_assert = require("@xylabs/assert");
39
+ var import_archivist_wrapper = require("@xyo-network/archivist-wrapper");
40
+ var import_boundwitness_builder = require("@xyo-network/boundwitness-builder");
41
+ var import_boundwitness_model = require("@xyo-network/boundwitness-model");
42
+ var import_diviner_abstract = require("@xyo-network/diviner-abstract");
43
+ var import_diviner_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
44
+ var import_diviner_wrapper = require("@xyo-network/diviner-wrapper");
45
+ var import_module_model = require("@xyo-network/module-model");
46
+ var import_payload_builder = require("@xyo-network/payload-builder");
47
+ var moduleName = "StatefulDiviner";
48
+ var StatefulDiviner = class extends import_diviner_abstract.AbstractDiviner {
49
+ static {
50
+ __name(this, "StatefulDiviner");
51
+ }
52
+ static configSchemas = [
53
+ ...super.configSchemas,
54
+ StatefulDivinerConfigSchema
55
+ ];
56
+ static defaultConfigSchema = StatefulDivinerConfigSchema;
57
+ /**
58
+ * The last state
59
+ */
60
+ _lastState;
61
+ /**
62
+ * Commit the internal state of the Diviner process. This is similar
63
+ * to a transaction completion in a database and should only be called
64
+ * when results have been successfully persisted to the appropriate
65
+ * external stores.
66
+ * @param nextState The state to commit
67
+ */
68
+ async commitState(nextState) {
69
+ if (nextState.state.offset === this._lastState?.state.offset)
70
+ return;
71
+ this._lastState = nextState;
72
+ const archivist = await this.getArchivistForStateStore();
73
+ const [bw] = await (await new import_boundwitness_builder.BoundWitnessBuilder().payload(nextState)).witness(this.account).build();
74
+ await archivist.insert([
75
+ bw,
76
+ nextState
77
+ ]);
78
+ }
79
+ /**
80
+ * Retrieves the archivist for the specified store
81
+ * @param store The store to retrieve the archivist for
82
+ * @returns The archivist for the specified store
83
+ */
84
+ async getArchivistForStateStore() {
85
+ const name = (0, import_assert.assertEx)(this.config?.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
86
+ const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
87
+ return import_archivist_wrapper.ArchivistWrapper.wrap(mod, this.account);
88
+ }
89
+ /**
90
+ * Retrieves the BoundWitness Diviner for the specified store
91
+ * @param store The store to retrieve the BoundWitness Diviner for
92
+ * @returns The BoundWitness Diviner for the specified store
93
+ */
94
+ async getBoundWitnessDivinerForStateStore() {
95
+ const name = (0, import_assert.assertEx)(this.config?.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
96
+ const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
97
+ return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
98
+ }
99
+ /**
100
+ * Retrieves the Payload Diviner for the specified store
101
+ * @param store The store to retrieve the Payload Diviner for
102
+ * @returns The Payload Diviner for the specified store
103
+ */
104
+ async getPayloadDivinerForStateStore() {
105
+ const name = (0, import_assert.assertEx)(this.config?.stateStore?.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)
115
+ return this._lastState;
116
+ let hash = "";
117
+ const diviner = await this.getBoundWitnessDivinerForStateStore();
118
+ const query = await new import_payload_builder.PayloadBuilder({
119
+ schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema
120
+ }).fields({
121
+ address: this.account.address,
122
+ limit: 1,
123
+ offset: 0,
124
+ order: "desc",
125
+ payload_schemas: [
126
+ import_module_model.ModuleStateSchema
127
+ ]
128
+ }).build();
129
+ const boundWitnesses = await diviner.divine([
130
+ query
131
+ ]);
132
+ if (boundWitnesses.length > 0) {
133
+ const boundWitness = boundWitnesses[0];
134
+ if ((0, import_boundwitness_model.isBoundWitness)(boundWitness)) {
135
+ hash = boundWitness.addresses.map((address, index) => ({
136
+ address,
137
+ index
138
+ })).filter(({ address }) => address === this.account.address).reduce((prev, curr) => boundWitness.payload_schemas?.[curr?.index] === import_module_model.ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev, "");
139
+ }
140
+ }
141
+ if (hash) {
142
+ const archivist = await this.getArchivistForStateStore();
143
+ const payload = (await archivist.get([
144
+ hash
145
+ ])).find(import_module_model.isModuleState);
146
+ if (payload) {
147
+ return payload;
148
+ }
149
+ }
150
+ return void 0;
151
+ }
152
+ };
153
+
154
+ // src/DivinerMixin.ts
155
+ var import_assert2 = require("@xylabs/assert");
156
+ var import_archivist_model = require("@xyo-network/archivist-model");
157
+ var import_boundwitness_builder2 = require("@xyo-network/boundwitness-builder");
158
+ var import_boundwitness_model2 = require("@xyo-network/boundwitness-model");
159
+ var import_diviner_boundwitness_model2 = require("@xyo-network/diviner-boundwitness-model");
160
+ var import_diviner_model = require("@xyo-network/diviner-model");
161
+ var import_module_model2 = require("@xyo-network/module-model");
162
+ var import_payload_builder2 = require("@xyo-network/payload-builder");
163
+ var moduleName2 = "StatefulModuleMixin";
164
+ var StatefulModuleMixin = /* @__PURE__ */ __name((ModuleBase) => {
165
+ let StatefulModuleBase = class StatefulModuleBase extends ModuleBase {
166
+ static {
167
+ __name(this, "StatefulModuleBase");
168
+ }
169
+ _lastState;
170
+ /**
171
+ * Commit the internal state of the Diviner process. This is similar
172
+ * to a transaction completion in a database and should only be called
173
+ * when results have been successfully persisted to the appropriate
174
+ * external stores.
175
+ * @param nextState The state to commit
176
+ */
177
+ async commitState(nextState) {
178
+ if (nextState.state.offset === this._lastState?.state.offset)
179
+ return;
180
+ this._lastState = nextState;
181
+ const archivist = await this.getArchivistForStore();
182
+ const [bw] = await (await new import_boundwitness_builder2.BoundWitnessBuilder().payload(nextState)).build();
183
+ await archivist.insert([
184
+ bw,
185
+ nextState
186
+ ]);
187
+ }
188
+ /**
189
+ * Retrieves the archivist for the specified store
190
+ * @param store The store to retrieve the archivist for
191
+ * @returns The archivist for the specified store
192
+ */
193
+ async getArchivistForStore() {
194
+ const name = (0, import_assert2.assertEx)(this.config?.stateStore?.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
195
+ const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
196
+ const instance = (0, import_archivist_model.asArchivistInstance)(mod);
197
+ return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
198
+ }
199
+ /**
200
+ * Retrieves the BoundWitness Diviner for the specified store
201
+ * @param store The store to retrieve the BoundWitness Diviner for
202
+ * @returns The BoundWitness Diviner for the specified store
203
+ */
204
+ async getBoundWitnessDivinerForStore() {
205
+ const name = (0, import_assert2.assertEx)(this.config?.stateStore?.boundWitnessDiviner, () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`);
206
+ const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
207
+ const instance = (0, import_diviner_model.asDivinerInstance)(mod);
208
+ return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
209
+ }
210
+ /**
211
+ * Retrieves the Payload Diviner for the specified store
212
+ * @param store The store to retrieve the Payload Diviner for
213
+ * @returns The Payload Diviner for the specified store
214
+ */
215
+ async getPayloadDivinerForStateStore() {
216
+ const name = (0, import_assert2.assertEx)(this.config?.stateStore?.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
217
+ const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
218
+ const instance = (0, import_diviner_model.asDivinerInstance)(mod);
219
+ return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
220
+ }
221
+ /**
222
+ * Retrieves the last state of the Diviner process. Used to recover state after
223
+ * preemptions, reboots, etc.
224
+ */
225
+ async retrieveState() {
226
+ if (this._lastState)
227
+ return this._lastState;
228
+ let hash = "";
229
+ const diviner = await this.getBoundWitnessDivinerForStore();
230
+ const query = await new import_payload_builder2.PayloadBuilder({
231
+ schema: import_diviner_boundwitness_model2.BoundWitnessDivinerQuerySchema
232
+ }).fields({
233
+ // address: this.account.address,
234
+ limit: 1,
235
+ offset: 0,
236
+ order: "desc",
237
+ payload_schemas: [
238
+ import_module_model2.ModuleStateSchema
239
+ ]
240
+ }).build();
241
+ const boundWitnesses = await diviner.divine([
242
+ query
243
+ ]);
244
+ if (boundWitnesses.length > 0) {
245
+ const boundWitness = boundWitnesses[0];
246
+ if ((0, import_boundwitness_model2.isBoundWitness)(boundWitness)) {
247
+ hash = boundWitness.addresses.map((address, index) => ({
248
+ address,
249
+ index
250
+ })).reduce((prev, curr) => boundWitness.payload_schemas?.[curr?.index] === import_module_model2.ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev, "");
251
+ }
252
+ }
253
+ if (hash) {
254
+ const archivist = await this.getArchivistForStore();
255
+ const payload = (await archivist.get([
256
+ hash
257
+ ])).find(import_module_model2.isModuleState);
258
+ if (payload) {
259
+ return payload;
260
+ }
261
+ }
262
+ return void 0;
263
+ }
264
+ };
265
+ return StatefulModuleBase;
266
+ }, "StatefulModuleMixin");
267
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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 (await new BoundWitnessBuilder().payload(nextState)).witness(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).witness(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) {\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UAACxB;SAAK,GAAGyB,KAAKC,kCAAAA;AACnD,YAAI3C,SAAS;AACX,iBAAOA;QACT;MACF;AACA,aAAO4C;IACT;EACF;AACA,SAAOtD;AACT,GAjHmC;","names":["StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","moduleName","StatefulDiviner","AbstractDiviner","configSchemas","StatefulDivinerConfigSchema","defaultConfigSchema","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStateStore","bw","BoundWitnessBuilder","payload","witness","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","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","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"]}
@@ -0,0 +1,6 @@
1
+ export * from './Config';
2
+ export * from './Diviner';
3
+ export * from './DivinerMixin';
4
+ export * from './Params';
5
+ export * from './Schema';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA"}
@@ -0,0 +1,6 @@
1
+ export * from './Config';
2
+ export * from './Diviner';
3
+ export * from './DivinerMixin';
4
+ export * from './Params';
5
+ export * from './Schema';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA"}
@@ -0,0 +1,6 @@
1
+ export * from './Config';
2
+ export * from './Diviner';
3
+ export * from './DivinerMixin';
4
+ export * from './Params';
5
+ export * from './Schema';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,WAAW,CAAA;AACzB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA"}
@@ -0,0 +1,246 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/Schema.ts
5
+ var StatefulDivinerSchema = "network.xyo.diviner.stateful";
6
+
7
+ // src/Config.ts
8
+ var StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config`;
9
+
10
+ // src/Diviner.ts
11
+ import { assertEx } from "@xylabs/assert";
12
+ import { ArchivistWrapper } from "@xyo-network/archivist-wrapper";
13
+ import { BoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
14
+ import { isBoundWitness } from "@xyo-network/boundwitness-model";
15
+ import { AbstractDiviner } from "@xyo-network/diviner-abstract";
16
+ import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
17
+ import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
18
+ import { isModuleState, ModuleStateSchema } from "@xyo-network/module-model";
19
+ import { PayloadBuilder } from "@xyo-network/payload-builder";
20
+ var moduleName = "StatefulDiviner";
21
+ var StatefulDiviner = class extends AbstractDiviner {
22
+ static {
23
+ __name(this, "StatefulDiviner");
24
+ }
25
+ static configSchemas = [
26
+ ...super.configSchemas,
27
+ StatefulDivinerConfigSchema
28
+ ];
29
+ static defaultConfigSchema = StatefulDivinerConfigSchema;
30
+ /**
31
+ * The last state
32
+ */
33
+ _lastState;
34
+ /**
35
+ * Commit the internal state of the Diviner process. This is similar
36
+ * to a transaction completion in a database and should only be called
37
+ * when results have been successfully persisted to the appropriate
38
+ * external stores.
39
+ * @param nextState The state to commit
40
+ */
41
+ async commitState(nextState) {
42
+ if (nextState.state.offset === this._lastState?.state.offset)
43
+ return;
44
+ this._lastState = nextState;
45
+ const archivist = await this.getArchivistForStateStore();
46
+ const [bw] = await (await new BoundWitnessBuilder().payload(nextState)).witness(this.account).build();
47
+ await archivist.insert([
48
+ bw,
49
+ nextState
50
+ ]);
51
+ }
52
+ /**
53
+ * Retrieves the archivist for the specified store
54
+ * @param store The store to retrieve the archivist for
55
+ * @returns The archivist for the specified store
56
+ */
57
+ async getArchivistForStateStore() {
58
+ const name = assertEx(this.config?.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
59
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
60
+ return ArchivistWrapper.wrap(mod, this.account);
61
+ }
62
+ /**
63
+ * Retrieves the BoundWitness Diviner for the specified store
64
+ * @param store The store to retrieve the BoundWitness Diviner for
65
+ * @returns The BoundWitness Diviner for the specified store
66
+ */
67
+ async getBoundWitnessDivinerForStateStore() {
68
+ const name = assertEx(this.config?.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
69
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
70
+ return DivinerWrapper.wrap(mod, this.account);
71
+ }
72
+ /**
73
+ * Retrieves the Payload Diviner for the specified store
74
+ * @param store The store to retrieve the Payload Diviner for
75
+ * @returns The Payload Diviner for the specified store
76
+ */
77
+ async getPayloadDivinerForStateStore() {
78
+ const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`);
79
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
80
+ return DivinerWrapper.wrap(mod, this.account);
81
+ }
82
+ /**
83
+ * Retrieves the last state of the Diviner process. Used to recover state after
84
+ * preemptions, reboots, etc.
85
+ */
86
+ async retrieveState() {
87
+ if (this._lastState)
88
+ return this._lastState;
89
+ let hash = "";
90
+ const diviner = await this.getBoundWitnessDivinerForStateStore();
91
+ const query = await new PayloadBuilder({
92
+ schema: BoundWitnessDivinerQuerySchema
93
+ }).fields({
94
+ address: this.account.address,
95
+ limit: 1,
96
+ offset: 0,
97
+ order: "desc",
98
+ payload_schemas: [
99
+ ModuleStateSchema
100
+ ]
101
+ }).build();
102
+ const boundWitnesses = await diviner.divine([
103
+ query
104
+ ]);
105
+ if (boundWitnesses.length > 0) {
106
+ const boundWitness = boundWitnesses[0];
107
+ if (isBoundWitness(boundWitness)) {
108
+ hash = boundWitness.addresses.map((address, index) => ({
109
+ address,
110
+ index
111
+ })).filter(({ address }) => address === this.account.address).reduce((prev, curr) => boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev, "");
112
+ }
113
+ }
114
+ if (hash) {
115
+ const archivist = await this.getArchivistForStateStore();
116
+ const payload = (await archivist.get([
117
+ hash
118
+ ])).find(isModuleState);
119
+ if (payload) {
120
+ return payload;
121
+ }
122
+ }
123
+ return void 0;
124
+ }
125
+ };
126
+
127
+ // src/DivinerMixin.ts
128
+ import { assertEx as assertEx2 } from "@xylabs/assert";
129
+ import { asArchivistInstance } from "@xyo-network/archivist-model";
130
+ import { BoundWitnessBuilder as BoundWitnessBuilder2 } from "@xyo-network/boundwitness-builder";
131
+ import { isBoundWitness as isBoundWitness2 } from "@xyo-network/boundwitness-model";
132
+ import { BoundWitnessDivinerQuerySchema as BoundWitnessDivinerQuerySchema2 } from "@xyo-network/diviner-boundwitness-model";
133
+ import { asDivinerInstance } from "@xyo-network/diviner-model";
134
+ import { isModuleState as isModuleState2, ModuleStateSchema as ModuleStateSchema2 } from "@xyo-network/module-model";
135
+ import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
136
+ var moduleName2 = "StatefulModuleMixin";
137
+ var StatefulModuleMixin = /* @__PURE__ */ __name((ModuleBase) => {
138
+ let StatefulModuleBase = class StatefulModuleBase extends ModuleBase {
139
+ static {
140
+ __name(this, "StatefulModuleBase");
141
+ }
142
+ _lastState;
143
+ /**
144
+ * Commit the internal state of the Diviner process. This is similar
145
+ * to a transaction completion in a database and should only be called
146
+ * when results have been successfully persisted to the appropriate
147
+ * external stores.
148
+ * @param nextState The state to commit
149
+ */
150
+ async commitState(nextState) {
151
+ if (nextState.state.offset === this._lastState?.state.offset)
152
+ return;
153
+ this._lastState = nextState;
154
+ const archivist = await this.getArchivistForStore();
155
+ const [bw] = await (await new BoundWitnessBuilder2().payload(nextState)).build();
156
+ await archivist.insert([
157
+ bw,
158
+ nextState
159
+ ]);
160
+ }
161
+ /**
162
+ * Retrieves the archivist for the specified store
163
+ * @param store The store to retrieve the archivist for
164
+ * @returns The archivist for the specified store
165
+ */
166
+ async getArchivistForStore() {
167
+ const name = assertEx2(this.config?.stateStore?.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
168
+ const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
169
+ const instance = asArchivistInstance(mod);
170
+ return assertEx2(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
171
+ }
172
+ /**
173
+ * Retrieves the BoundWitness Diviner for the specified store
174
+ * @param store The store to retrieve the BoundWitness Diviner for
175
+ * @returns The BoundWitness Diviner for the specified store
176
+ */
177
+ async getBoundWitnessDivinerForStore() {
178
+ const name = assertEx2(this.config?.stateStore?.boundWitnessDiviner, () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`);
179
+ const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
180
+ const instance = asDivinerInstance(mod);
181
+ return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
182
+ }
183
+ /**
184
+ * Retrieves the Payload Diviner for the specified store
185
+ * @param store The store to retrieve the Payload Diviner for
186
+ * @returns The Payload Diviner for the specified store
187
+ */
188
+ async getPayloadDivinerForStateStore() {
189
+ const name = assertEx2(this.config?.stateStore?.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
190
+ const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
191
+ const instance = asDivinerInstance(mod);
192
+ return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
193
+ }
194
+ /**
195
+ * Retrieves the last state of the Diviner process. Used to recover state after
196
+ * preemptions, reboots, etc.
197
+ */
198
+ async retrieveState() {
199
+ if (this._lastState)
200
+ return this._lastState;
201
+ let hash = "";
202
+ const diviner = await this.getBoundWitnessDivinerForStore();
203
+ const query = await new PayloadBuilder2({
204
+ schema: BoundWitnessDivinerQuerySchema2
205
+ }).fields({
206
+ // address: this.account.address,
207
+ limit: 1,
208
+ offset: 0,
209
+ order: "desc",
210
+ payload_schemas: [
211
+ ModuleStateSchema2
212
+ ]
213
+ }).build();
214
+ const boundWitnesses = await diviner.divine([
215
+ query
216
+ ]);
217
+ if (boundWitnesses.length > 0) {
218
+ const boundWitness = boundWitnesses[0];
219
+ if (isBoundWitness2(boundWitness)) {
220
+ hash = boundWitness.addresses.map((address, index) => ({
221
+ address,
222
+ index
223
+ })).reduce((prev, curr) => boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema2 ? boundWitness.payload_hashes[curr?.index] : prev, "");
224
+ }
225
+ }
226
+ if (hash) {
227
+ const archivist = await this.getArchivistForStore();
228
+ const payload = (await archivist.get([
229
+ hash
230
+ ])).find(isModuleState2);
231
+ if (payload) {
232
+ return payload;
233
+ }
234
+ }
235
+ return void 0;
236
+ }
237
+ };
238
+ return StatefulModuleBase;
239
+ }, "StatefulModuleMixin");
240
+ export {
241
+ StatefulDiviner,
242
+ StatefulDivinerConfigSchema,
243
+ StatefulDivinerSchema,
244
+ StatefulModuleMixin
245
+ };
246
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/Schema.ts","../../src/Config.ts","../../src/Diviner.ts","../../src/DivinerMixin.ts"],"sourcesContent":["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 (await new BoundWitnessBuilder().payload(nextState)).witness(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).witness(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":";;;;AAAO,IAAMA,wBAAwB;;;ACQ9B,IAAMC,8BAA8B,GAAGC,qBAAAA;;;ACR9C,SAASC,gBAAgB;AAEzB,SAASC,wBAAwB;AACjC,SAASC,2BAA2B;AACpC,SAASC,sBAAsB;AAC/B,SAASC,uBAAuB;AAChC,SAA0CC,sCAAsC;AAEhF,SAASC,sBAAsB;AAC/B,SAASC,eAA4BC,yBAA0C;AAC/E,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AACxI,YAAMoB,MAAMH,UAAS,MAAM,KAAKI,QAAQL,IAAAA,GAAO,MAAM,GAAGhB,WAAAA,+CAAyD;AAEjH,YAAMsB,WAAWI,kBAAkBN,GAAAA;AACnC,aAAOH,UAASK,UAAU,MAAM,GAAGtB,WAAAA,mCAA6C;IAClF;;;;;IAKA,MAAM6B,gBAAoE;AACxE,UAAI,KAAKzB;AAAY,eAAO,KAAKA;AACjC,UAAI0B,OAAa;AACjB,YAAMC,UAAU,MAAM,KAAKP,+BAA8B;AACzD,YAAMQ,QAAQ,MAAM,IAAIC,gBAAgD;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,YAAII,gBAAeD,YAAAA,GAAe;AAEhCd,iBAAOc,aAAaE,UACjBC,IAAI,CAACC,SAASC,WAAW;YAAED;YAASC;UAAM,EAAA,EAG1CC,OACC,CAACC,MAAMC,SAAUR,aAAaL,kBAAkBa,MAAMH,KAAAA,MAAWT,qBAAoBI,aAAaS,eAAeD,MAAMH,KAAAA,IAASE,MAChI,EAAA;QAEN;MACF;AAGA,UAAIrB,MAAM;AAER,cAAMrB,YAAY,MAAM,KAAKC,qBAAoB;AACjD,cAAMG,WAAW,MAAMJ,UAAU6C,IAAI;UAACxB;SAAK,GAAGyB,KAAKC,cAAAA;AACnD,YAAI3C,SAAS;AACX,iBAAOA;QACT;MACF;AACA,aAAO4C;IACT;EACF;AACA,SAAOtD;AACT,GAjHmC;","names":["StatefulDivinerSchema","StatefulDivinerConfigSchema","StatefulDivinerSchema","assertEx","ArchivistWrapper","BoundWitnessBuilder","isBoundWitness","AbstractDiviner","BoundWitnessDivinerQuerySchema","DivinerWrapper","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName","StatefulDiviner","AbstractDiviner","configSchemas","StatefulDivinerConfigSchema","defaultConfigSchema","_lastState","commitState","nextState","state","offset","archivist","getArchivistForStateStore","bw","BoundWitnessBuilder","payload","witness","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","assertEx","asArchivistInstance","BoundWitnessBuilder","isBoundWitness","BoundWitnessDivinerQuerySchema","asDivinerInstance","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName","StatefulModuleMixin","ModuleBase","StatefulModuleBase","_lastState","commitState","nextState","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"]}