@xyo-network/diviner-stateful 2.84.1 → 2.84.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.
@@ -0,0 +1,259 @@
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 __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ StatefulDiviner: () => StatefulDiviner,
24
+ StatefulDivinerConfigSchema: () => StatefulDivinerConfigSchema,
25
+ StatefulDivinerSchema: () => StatefulDivinerSchema,
26
+ StatefulModuleMixin: () => StatefulModuleMixin
27
+ });
28
+ module.exports = __toCommonJS(src_exports);
29
+
30
+ // src/Schema.ts
31
+ var StatefulDivinerSchema = "network.xyo.diviner.stateful";
32
+
33
+ // src/Config.ts
34
+ var StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config`;
35
+
36
+ // src/Diviner.ts
37
+ var import_assert = require("@xylabs/assert");
38
+ var import_abstract_diviner = require("@xyo-network/abstract-diviner");
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_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
43
+ var import_diviner_model = require("@xyo-network/diviner-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_abstract_diviner.AbstractDiviner {
49
+ static configSchemas = [import_diviner_model.DivinerConfigSchema, StatefulDivinerConfigSchema];
50
+ /**
51
+ * The last state
52
+ */
53
+ _lastState;
54
+ /**
55
+ * Commit the internal state of the Diviner process. This is similar
56
+ * to a transaction completion in a database and should only be called
57
+ * when results have been successfully persisted to the appropriate
58
+ * external stores.
59
+ * @param nextState The state to commit
60
+ */
61
+ async commitState(nextState) {
62
+ var _a;
63
+ if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset))
64
+ return;
65
+ this._lastState = nextState;
66
+ const archivist = await this.getArchivistForStateStore();
67
+ const [bw] = await new import_boundwitness_builder.BoundWitnessBuilder().payload(nextState).witness(this.account).build();
68
+ await archivist.insert([bw, nextState]);
69
+ }
70
+ /**
71
+ * Retrieves the archivist for the specified store
72
+ * @param store The store to retrieve the archivist for
73
+ * @returns The archivist for the specified store
74
+ */
75
+ async getArchivistForStateStore() {
76
+ var _a;
77
+ const name = (0, import_assert.assertEx)((_a = this.config) == null ? void 0 : _a.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
78
+ const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
79
+ return import_archivist_wrapper.ArchivistWrapper.wrap(mod, this.account);
80
+ }
81
+ /**
82
+ * Retrieves the BoundWitness Diviner for the specified store
83
+ * @param store The store to retrieve the BoundWitness Diviner for
84
+ * @returns The BoundWitness Diviner for the specified store
85
+ */
86
+ async getBoundWitnessDivinerForStateStore() {
87
+ var _a;
88
+ const name = (0, import_assert.assertEx)((_a = this.config) == null ? void 0 : _a.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
89
+ const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
90
+ return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
91
+ }
92
+ /**
93
+ * Retrieves the Payload Diviner for the specified store
94
+ * @param store The store to retrieve the Payload Diviner for
95
+ * @returns The Payload Diviner for the specified store
96
+ */
97
+ async getPayloadDivinerForStateStore() {
98
+ var _a, _b;
99
+ 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`);
100
+ const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
101
+ return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
102
+ }
103
+ /**
104
+ * Retrieves the last state of the Diviner process. Used to recover state after
105
+ * preemptions, reboots, etc.
106
+ */
107
+ async retrieveState() {
108
+ if (this._lastState)
109
+ return this._lastState;
110
+ let hash = "";
111
+ const diviner = await this.getBoundWitnessDivinerForStateStore();
112
+ const query = await new import_payload_builder.PayloadBuilder({ schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema }).fields({
113
+ address: this.account.address,
114
+ limit: 1,
115
+ offset: 0,
116
+ order: "desc",
117
+ payload_schemas: [import_module_model.ModuleStateSchema]
118
+ }).build();
119
+ const boundWitnesses = await diviner.divine([query]);
120
+ if (boundWitnesses.length > 0) {
121
+ const boundWitness = boundWitnesses[0];
122
+ if ((0, import_boundwitness_model.isBoundWitness)(boundWitness)) {
123
+ hash = boundWitness.addresses.map((address, index) => ({ address, index })).filter(({ address }) => address === this.account.address).reduce(
124
+ (prev, curr) => {
125
+ var _a;
126
+ 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;
127
+ },
128
+ ""
129
+ );
130
+ }
131
+ }
132
+ if (hash) {
133
+ const archivist = await this.getArchivistForStateStore();
134
+ const payload = (await archivist.get([hash])).find(import_module_model.isModuleState);
135
+ if (payload) {
136
+ return payload;
137
+ }
138
+ }
139
+ return void 0;
140
+ }
141
+ };
142
+
143
+ // src/DivinerMixin.ts
144
+ var import_assert2 = require("@xylabs/assert");
145
+ var import_archivist = require("@xyo-network/archivist");
146
+ var import_boundwitness_builder2 = require("@xyo-network/boundwitness-builder");
147
+ var import_boundwitness_model2 = require("@xyo-network/boundwitness-model");
148
+ var import_diviner_boundwitness_model2 = require("@xyo-network/diviner-boundwitness-model");
149
+ var import_diviner_model2 = require("@xyo-network/diviner-model");
150
+ var import_module_model2 = require("@xyo-network/module-model");
151
+ var import_payload_builder2 = require("@xyo-network/payload-builder");
152
+ var moduleName2 = "StatefulModuleMixin";
153
+ var StatefulModuleMixin = (ModuleBase) => {
154
+ class StatefulModuleBase extends ModuleBase {
155
+ _lastState;
156
+ /**
157
+ * Commit the internal state of the Diviner process. This is similar
158
+ * to a transaction completion in a database and should only be called
159
+ * when results have been successfully persisted to the appropriate
160
+ * external stores.
161
+ * @param nextState The state to commit
162
+ */
163
+ async commitState(nextState) {
164
+ var _a;
165
+ if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset))
166
+ return;
167
+ this._lastState = nextState;
168
+ const archivist = await this.getArchivistForStore();
169
+ const [bw] = await new import_boundwitness_builder2.BoundWitnessBuilder().payload(nextState).build();
170
+ await archivist.insert([bw, nextState]);
171
+ }
172
+ /**
173
+ * Retrieves the archivist for the specified store
174
+ * @param store The store to retrieve the archivist for
175
+ * @returns The archivist for the specified store
176
+ */
177
+ async getArchivistForStore() {
178
+ var _a, _b;
179
+ const name = (0, import_assert2.assertEx)((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
180
+ const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
181
+ const instance = (0, import_archivist.asArchivistInstance)(mod);
182
+ return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
183
+ }
184
+ /**
185
+ * Retrieves the BoundWitness Diviner for the specified store
186
+ * @param store The store to retrieve the BoundWitness Diviner for
187
+ * @returns The BoundWitness Diviner for the specified store
188
+ */
189
+ async getBoundWitnessDivinerForStore() {
190
+ var _a, _b;
191
+ const name = (0, import_assert2.assertEx)(
192
+ (_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.boundWitnessDiviner,
193
+ () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`
194
+ );
195
+ const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
196
+ const instance = (0, import_diviner_model2.asDivinerInstance)(mod);
197
+ return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
198
+ }
199
+ /**
200
+ * Retrieves the Payload Diviner for the specified store
201
+ * @param store The store to retrieve the Payload Diviner for
202
+ * @returns The Payload Diviner for the specified store
203
+ */
204
+ async getPayloadDivinerForStateStore() {
205
+ var _a, _b;
206
+ const name = (0, import_assert2.assertEx)((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
207
+ const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
208
+ const instance = (0, import_diviner_model2.asDivinerInstance)(mod);
209
+ return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
210
+ }
211
+ /**
212
+ * Retrieves the last state of the Diviner process. Used to recover state after
213
+ * preemptions, reboots, etc.
214
+ */
215
+ async retrieveState() {
216
+ if (this._lastState)
217
+ return this._lastState;
218
+ let hash = "";
219
+ const diviner = await this.getBoundWitnessDivinerForStore();
220
+ const query = await new import_payload_builder2.PayloadBuilder({ schema: import_diviner_boundwitness_model2.BoundWitnessDivinerQuerySchema }).fields({
221
+ // address: this.account.address,
222
+ limit: 1,
223
+ offset: 0,
224
+ order: "desc",
225
+ payload_schemas: [import_module_model2.ModuleStateSchema]
226
+ }).build();
227
+ const boundWitnesses = await diviner.divine([query]);
228
+ if (boundWitnesses.length > 0) {
229
+ const boundWitness = boundWitnesses[0];
230
+ if ((0, import_boundwitness_model2.isBoundWitness)(boundWitness)) {
231
+ hash = boundWitness.addresses.map((address, index) => ({ address, index })).reduce(
232
+ (prev, curr) => {
233
+ var _a;
234
+ return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === import_module_model2.ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
235
+ },
236
+ ""
237
+ );
238
+ }
239
+ }
240
+ if (hash) {
241
+ const archivist = await this.getArchivistForStore();
242
+ const payload = (await archivist.get([hash])).find(import_module_model2.isModuleState);
243
+ if (payload) {
244
+ return payload;
245
+ }
246
+ }
247
+ return void 0;
248
+ }
249
+ }
250
+ return StatefulModuleBase;
251
+ };
252
+ // Annotate the CommonJS export names for ESM import in node:
253
+ 0 && (module.exports = {
254
+ StatefulDiviner,
255
+ StatefulDivinerConfigSchema,
256
+ StatefulDivinerSchema,
257
+ StatefulModuleMixin
258
+ });
259
+ //# 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'\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: string\n boundWitnessDiviner: string\n payloadDiviner: string\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport { AbstractDiviner } from '@xyo-network/abstract-diviner'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerConfigSchema, DivinerModule, 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 } 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<DivinerModule<TParams>, TIn, TOut> = DivinerModuleEventData<DivinerModule<TParams>, TIn, TOut>,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: string[] = [DivinerConfigSchema, StatefulDivinerConfigSchema]\n\n /**\n * The last state\n */\n protected _lastState?: 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: 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).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<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: string = ''\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 .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\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\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { asArchivistInstance } from '@xyo-network/archivist'\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'\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?: 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: 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 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<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: string = ''\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 .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\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\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,wBAAwB;;;ACO9B,IAAM,8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,YAAW,yCAAoB,aAAa,eAAe,6BAAM,KAAK,IAAI;AAAA;AAAA,YAChI;AAAA,UACF;AAAA,QACJ;AAAA,MACF;AAGA,UAAI,MAAM;AAER,cAAM,YAAY,MAAM,KAAK,qBAAqB;AAClD,cAAM,WAAW,MAAM,UAAU,IAAI,CAAC,IAAI,CAAC,GAAG,KAAK,kCAAqB;AACxE,YAAI,SAAS;AACX,iBAAO;AAAA,QACT;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["import_assert","import_boundwitness_builder","import_boundwitness_model","import_diviner_boundwitness_model","import_diviner_model","import_module_model","import_payload_builder","moduleName"]}
@@ -1,32 +1,3 @@
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 __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- StatefulDiviner: () => StatefulDiviner,
24
- StatefulDivinerConfigSchema: () => StatefulDivinerConfigSchema,
25
- StatefulDivinerSchema: () => StatefulDivinerSchema,
26
- StatefulModuleMixin: () => StatefulModuleMixin
27
- });
28
- module.exports = __toCommonJS(src_exports);
29
-
30
1
  // src/Schema.ts
31
2
  var StatefulDivinerSchema = "network.xyo.diviner.stateful";
32
3
 
@@ -34,19 +5,19 @@ var StatefulDivinerSchema = "network.xyo.diviner.stateful";
34
5
  var StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config`;
35
6
 
36
7
  // src/Diviner.ts
37
- var import_assert = require("@xylabs/assert");
38
- var import_abstract_diviner = require("@xyo-network/abstract-diviner");
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_boundwitness_model = require("@xyo-network/diviner-boundwitness-model");
43
- var import_diviner_model = require("@xyo-network/diviner-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");
8
+ import { assertEx } from "@xylabs/assert";
9
+ import { AbstractDiviner } from "@xyo-network/abstract-diviner";
10
+ import { ArchivistWrapper } from "@xyo-network/archivist-wrapper";
11
+ import { BoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
12
+ import { isBoundWitness } from "@xyo-network/boundwitness-model";
13
+ import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
14
+ import { DivinerConfigSchema } from "@xyo-network/diviner-model";
15
+ import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
16
+ import { isModuleState, ModuleStateSchema } from "@xyo-network/module-model";
17
+ import { PayloadBuilder } from "@xyo-network/payload-builder";
47
18
  var moduleName = "StatefulDiviner";
48
- var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
49
- static configSchemas = [import_diviner_model.DivinerConfigSchema, StatefulDivinerConfigSchema];
19
+ var StatefulDiviner = class extends AbstractDiviner {
20
+ static configSchemas = [DivinerConfigSchema, StatefulDivinerConfigSchema];
50
21
  /**
51
22
  * The last state
52
23
  */
@@ -64,7 +35,7 @@ var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
64
35
  return;
65
36
  this._lastState = nextState;
66
37
  const archivist = await this.getArchivistForStateStore();
67
- const [bw] = await new import_boundwitness_builder.BoundWitnessBuilder().payload(nextState).witness(this.account).build();
38
+ const [bw] = await new BoundWitnessBuilder().payload(nextState).witness(this.account).build();
68
39
  await archivist.insert([bw, nextState]);
69
40
  }
70
41
  /**
@@ -74,9 +45,9 @@ var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
74
45
  */
75
46
  async getArchivistForStateStore() {
76
47
  var _a;
77
- const name = (0, import_assert.assertEx)((_a = this.config) == null ? void 0 : _a.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
78
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
79
- return import_archivist_wrapper.ArchivistWrapper.wrap(mod, this.account);
48
+ const name = assertEx((_a = this.config) == null ? void 0 : _a.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
49
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
50
+ return ArchivistWrapper.wrap(mod, this.account);
80
51
  }
81
52
  /**
82
53
  * Retrieves the BoundWitness Diviner for the specified store
@@ -85,9 +56,9 @@ var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
85
56
  */
86
57
  async getBoundWitnessDivinerForStateStore() {
87
58
  var _a;
88
- const name = (0, import_assert.assertEx)((_a = this.config) == null ? void 0 : _a.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
89
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
90
- return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
59
+ const name = assertEx((_a = this.config) == null ? void 0 : _a.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
60
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
61
+ return DivinerWrapper.wrap(mod, this.account);
91
62
  }
92
63
  /**
93
64
  * Retrieves the Payload Diviner for the specified store
@@ -96,9 +67,9 @@ var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
96
67
  */
97
68
  async getPayloadDivinerForStateStore() {
98
69
  var _a, _b;
99
- 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`);
100
- const mod = (0, import_assert.assertEx)(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
101
- return import_diviner_wrapper.DivinerWrapper.wrap(mod, this.account);
70
+ const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`);
71
+ const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
72
+ return DivinerWrapper.wrap(mod, this.account);
102
73
  }
103
74
  /**
104
75
  * Retrieves the last state of the Diviner process. Used to recover state after
@@ -109,21 +80,21 @@ var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
109
80
  return this._lastState;
110
81
  let hash = "";
111
82
  const diviner = await this.getBoundWitnessDivinerForStateStore();
112
- const query = await new import_payload_builder.PayloadBuilder({ schema: import_diviner_boundwitness_model.BoundWitnessDivinerQuerySchema }).fields({
83
+ const query = await new PayloadBuilder({ schema: BoundWitnessDivinerQuerySchema }).fields({
113
84
  address: this.account.address,
114
85
  limit: 1,
115
86
  offset: 0,
116
87
  order: "desc",
117
- payload_schemas: [import_module_model.ModuleStateSchema]
88
+ payload_schemas: [ModuleStateSchema]
118
89
  }).build();
119
90
  const boundWitnesses = await diviner.divine([query]);
120
91
  if (boundWitnesses.length > 0) {
121
92
  const boundWitness = boundWitnesses[0];
122
- if ((0, import_boundwitness_model.isBoundWitness)(boundWitness)) {
93
+ if (isBoundWitness(boundWitness)) {
123
94
  hash = boundWitness.addresses.map((address, index) => ({ address, index })).filter(({ address }) => address === this.account.address).reduce(
124
95
  (prev, curr) => {
125
96
  var _a;
126
- 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;
97
+ return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
127
98
  },
128
99
  ""
129
100
  );
@@ -131,7 +102,7 @@ var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
131
102
  }
132
103
  if (hash) {
133
104
  const archivist = await this.getArchivistForStateStore();
134
- const payload = (await archivist.get([hash])).find(import_module_model.isModuleState);
105
+ const payload = (await archivist.get([hash])).find(isModuleState);
135
106
  if (payload) {
136
107
  return payload;
137
108
  }
@@ -141,14 +112,17 @@ var StatefulDiviner = class extends import_abstract_diviner.AbstractDiviner {
141
112
  };
142
113
 
143
114
  // src/DivinerMixin.ts
144
- var import_assert2 = require("@xylabs/assert");
145
- var import_archivist = require("@xyo-network/archivist");
146
- var import_boundwitness_builder2 = require("@xyo-network/boundwitness-builder");
147
- var import_boundwitness_model2 = require("@xyo-network/boundwitness-model");
148
- var import_diviner_boundwitness_model2 = require("@xyo-network/diviner-boundwitness-model");
149
- var import_diviner_model2 = require("@xyo-network/diviner-model");
150
- var import_module_model2 = require("@xyo-network/module-model");
151
- var import_payload_builder2 = require("@xyo-network/payload-builder");
115
+ import { assertEx as assertEx2 } from "@xylabs/assert";
116
+ import { asArchivistInstance } from "@xyo-network/archivist";
117
+ import { BoundWitnessBuilder as BoundWitnessBuilder2 } from "@xyo-network/boundwitness-builder";
118
+ import { isBoundWitness as isBoundWitness2 } from "@xyo-network/boundwitness-model";
119
+ import { BoundWitnessDivinerQuerySchema as BoundWitnessDivinerQuerySchema2 } from "@xyo-network/diviner-boundwitness-model";
120
+ import { asDivinerInstance } from "@xyo-network/diviner-model";
121
+ import {
122
+ isModuleState as isModuleState2,
123
+ ModuleStateSchema as ModuleStateSchema2
124
+ } from "@xyo-network/module-model";
125
+ import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
152
126
  var moduleName2 = "StatefulModuleMixin";
153
127
  var StatefulModuleMixin = (ModuleBase) => {
154
128
  class StatefulModuleBase extends ModuleBase {
@@ -166,7 +140,7 @@ var StatefulModuleMixin = (ModuleBase) => {
166
140
  return;
167
141
  this._lastState = nextState;
168
142
  const archivist = await this.getArchivistForStore();
169
- const [bw] = await new import_boundwitness_builder2.BoundWitnessBuilder().payload(nextState).build();
143
+ const [bw] = await new BoundWitnessBuilder2().payload(nextState).build();
170
144
  await archivist.insert([bw, nextState]);
171
145
  }
172
146
  /**
@@ -176,10 +150,10 @@ var StatefulModuleMixin = (ModuleBase) => {
176
150
  */
177
151
  async getArchivistForStore() {
178
152
  var _a, _b;
179
- const name = (0, import_assert2.assertEx)((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
180
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
181
- const instance = (0, import_archivist.asArchivistInstance)(mod);
182
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
153
+ const name = assertEx2((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
154
+ const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
155
+ const instance = asArchivistInstance(mod);
156
+ return assertEx2(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
183
157
  }
184
158
  /**
185
159
  * Retrieves the BoundWitness Diviner for the specified store
@@ -188,13 +162,13 @@ var StatefulModuleMixin = (ModuleBase) => {
188
162
  */
189
163
  async getBoundWitnessDivinerForStore() {
190
164
  var _a, _b;
191
- const name = (0, import_assert2.assertEx)(
165
+ const name = assertEx2(
192
166
  (_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.boundWitnessDiviner,
193
167
  () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`
194
168
  );
195
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
196
- const instance = (0, import_diviner_model2.asDivinerInstance)(mod);
197
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
169
+ const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
170
+ const instance = asDivinerInstance(mod);
171
+ return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
198
172
  }
199
173
  /**
200
174
  * Retrieves the Payload Diviner for the specified store
@@ -203,10 +177,10 @@ var StatefulModuleMixin = (ModuleBase) => {
203
177
  */
204
178
  async getPayloadDivinerForStateStore() {
205
179
  var _a, _b;
206
- const name = (0, import_assert2.assertEx)((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
207
- const mod = (0, import_assert2.assertEx)(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
208
- const instance = (0, import_diviner_model2.asDivinerInstance)(mod);
209
- return (0, import_assert2.assertEx)(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
180
+ const name = assertEx2((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
181
+ const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
182
+ const instance = asDivinerInstance(mod);
183
+ return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
210
184
  }
211
185
  /**
212
186
  * Retrieves the last state of the Diviner process. Used to recover state after
@@ -217,21 +191,21 @@ var StatefulModuleMixin = (ModuleBase) => {
217
191
  return this._lastState;
218
192
  let hash = "";
219
193
  const diviner = await this.getBoundWitnessDivinerForStore();
220
- const query = await new import_payload_builder2.PayloadBuilder({ schema: import_diviner_boundwitness_model2.BoundWitnessDivinerQuerySchema }).fields({
194
+ const query = await new PayloadBuilder2({ schema: BoundWitnessDivinerQuerySchema2 }).fields({
221
195
  // address: this.account.address,
222
196
  limit: 1,
223
197
  offset: 0,
224
198
  order: "desc",
225
- payload_schemas: [import_module_model2.ModuleStateSchema]
199
+ payload_schemas: [ModuleStateSchema2]
226
200
  }).build();
227
201
  const boundWitnesses = await diviner.divine([query]);
228
202
  if (boundWitnesses.length > 0) {
229
203
  const boundWitness = boundWitnesses[0];
230
- if ((0, import_boundwitness_model2.isBoundWitness)(boundWitness)) {
204
+ if (isBoundWitness2(boundWitness)) {
231
205
  hash = boundWitness.addresses.map((address, index) => ({ address, index })).reduce(
232
206
  (prev, curr) => {
233
207
  var _a;
234
- return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === import_module_model2.ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
208
+ return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === ModuleStateSchema2 ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
235
209
  },
236
210
  ""
237
211
  );
@@ -239,7 +213,7 @@ var StatefulModuleMixin = (ModuleBase) => {
239
213
  }
240
214
  if (hash) {
241
215
  const archivist = await this.getArchivistForStore();
242
- const payload = (await archivist.get([hash])).find(import_module_model2.isModuleState);
216
+ const payload = (await archivist.get([hash])).find(isModuleState2);
243
217
  if (payload) {
244
218
  return payload;
245
219
  }
@@ -249,11 +223,10 @@ var StatefulModuleMixin = (ModuleBase) => {
249
223
  }
250
224
  return StatefulModuleBase;
251
225
  };
252
- // Annotate the CommonJS export names for ESM import in node:
253
- 0 && (module.exports = {
226
+ export {
254
227
  StatefulDiviner,
255
228
  StatefulDivinerConfigSchema,
256
229
  StatefulDivinerSchema,
257
230
  StatefulModuleMixin
258
- });
231
+ };
259
232
  //# sourceMappingURL=index.js.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'\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: string\n boundWitnessDiviner: string\n payloadDiviner: string\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport { AbstractDiviner } from '@xyo-network/abstract-diviner'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerConfigSchema, DivinerModule, 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 } 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<DivinerModule<TParams>, TIn, TOut> = DivinerModuleEventData<DivinerModule<TParams>, TIn, TOut>,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: string[] = [DivinerConfigSchema, StatefulDivinerConfigSchema]\n\n /**\n * The last state\n */\n protected _lastState?: 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: 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).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<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: string = ''\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 .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\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\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { asArchivistInstance } from '@xyo-network/archivist'\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'\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?: 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: 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 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<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: string = ''\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 .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\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\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,wBAAwB;;;ACO9B,IAAM,8BAA8B,GAAG,qBAAqB;;;ACPnE,oBAAyB;AACzB,8BAAgC;AAChC,+BAAiC;AACjC,kCAAoC;AACpC,gCAA+B;AAC/B,wCAAgF;AAChF,2BAA2E;AAC3E,6BAA+B;AAC/B,0BAA+E;AAC/E,6BAA+B;AAM/B,IAAM,aAAa;AAKZ,IAAe,kBAAf,cAMG,wCAAgD;AAAA,EACxD,OAAyB,gBAA0B,CAAC,0CAAqB,2BAA2B;AAAA;AAAA;AAAA;AAAA,EAK1F;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASV,MAAgB,YAAY,WAAgC;AAzC9D;AA2CI,QAAI,UAAU,MAAM,aAAW,UAAK,eAAL,mBAAiB,MAAM;AAAQ;AAC9D,SAAK,aAAa;AAClB,UAAM,YAAY,MAAM,KAAK,0BAA0B;AACvD,UAAM,CAAC,EAAE,IAAI,MAAM,IAAI,gDAAoB,EAAE,QAAQ,SAAS,EAAE,QAAQ,KAAK,OAAO,EAAE,MAAM;AAC5F,UAAM,UAAU,OAAO,CAAC,IAAI,SAAS,CAAC;AAAA,EACxC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAgB,4BAA4B;AAvD9C;AAwDI,UAAM,WAAO,yBAAS,UAAK,WAAL,mBAAa,WA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OAAO;AAAA;AAAA,QAEN,OAAO;AAAA,QACP,QAAQ;AAAA,QACR,OAAO;AAAA,QACP,iBAAiB,CAAC,sCAAiB;AAAA,MACrC,CAAC,EACA,MAAM;AACT,YAAM,iBAAiB,MAAM,QAAQ,OAAO,CAAC,KAAK,CAAC;AACnD,UAAI,eAAe,SAAS,GAAG;AAC7B,cAAM,eAAe,eAAe,CAAC;AACrC,gBAAI,2CAAe,YAAY,GAAG;AAEhC,iBAAO,aAAa,UACjB,IAAI,CAAC,SAAS,WAAW,EAAE,SAAS,MAAM,EAAE,EAE5C;AAAA,YACC,CAAC,MAAM,SAAM;AA5H3B;AA4H+B,yCAAa,oBAAb,mBAA+B,6BAAM,YAAW,yCAAoB,aAAa,eAAe,6BAAM,KAAK,IAAI;AAAA;AAAA,YAChI;AAAA,UACF;AAAA,QACJ;AAAA,MACF;AAGA,UAAI,MAAM;AAER,cAAM,YAAY,MAAM,KAAK,qBAAqB;AAClD,cAAM,WAAW,MAAM,UAAU,IAAI,CAAC,IAAI,CAAC,GAAG,KAAK,kCAAqB;AACxE,YAAI,SAAS;AACX,iBAAO;AAAA,QACT;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["import_assert","import_boundwitness_builder","import_boundwitness_model","import_diviner_boundwitness_model","import_diviner_model","import_module_model","import_payload_builder","moduleName"]}
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'\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: string\n boundWitnessDiviner: string\n payloadDiviner: string\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport { AbstractDiviner } from '@xyo-network/abstract-diviner'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerConfigSchema, DivinerModule, 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 } 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<DivinerModule<TParams>, TIn, TOut> = DivinerModuleEventData<DivinerModule<TParams>, TIn, TOut>,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: string[] = [DivinerConfigSchema, StatefulDivinerConfigSchema]\n\n /**\n * The last state\n */\n protected _lastState?: 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: 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).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<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: string = ''\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 .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\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\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { asArchivistInstance } from '@xyo-network/archivist'\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'\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?: 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: 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 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<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: string = ''\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 .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\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\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";AAAO,IAAM,wBAAwB;;;ACO9B,IAAM,8BAA8B,GAAG,qBAAqB;;;ACPnE,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,wBAAwB;AACjC,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAC/B,SAA0C,sCAAsC;AAChF,SAAS,2BAAkE;AAC3E,SAAS,sBAAsB;AAC/B,SAAS,eAA4B,yBAA0C;AAC/E,SAAS,sBAAsB;AAM/B,IAAM,aAAa;AAKZ,IAAe,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AAK,qBAAqB;AAClD,cAAM,WAAW,MAAM,UAAU,IAAI,CAAC,IAAI,CAAC,GAAG,KAAKD,cAAqB;AACxE,YAAI,SAAS;AACX,iBAAO;AAAA,QACT;AAAA,MACF;AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["assertEx","BoundWitnessBuilder","isBoundWitness","BoundWitnessDivinerQuerySchema","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName"]}
package/package.json CHANGED
@@ -11,31 +11,30 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@xylabs/assert": "^2.13.20",
14
- "@xyo-network/abstract-diviner": "~2.84.1",
15
- "@xyo-network/archivist": "~2.84.1",
16
- "@xyo-network/archivist-wrapper": "~2.84.1",
17
- "@xyo-network/boundwitness-builder": "~2.84.1",
18
- "@xyo-network/boundwitness-model": "~2.84.1",
19
- "@xyo-network/diviner-boundwitness-model": "~2.84.1",
20
- "@xyo-network/diviner-model": "~2.84.1",
21
- "@xyo-network/diviner-wrapper": "~2.84.1",
22
- "@xyo-network/module-model": "~2.84.1",
23
- "@xyo-network/payload-builder": "~2.84.1",
24
- "@xyo-network/payload-model": "~2.84.1"
14
+ "@xyo-network/abstract-diviner": "~2.84.3",
15
+ "@xyo-network/archivist": "~2.84.3",
16
+ "@xyo-network/archivist-wrapper": "~2.84.3",
17
+ "@xyo-network/boundwitness-builder": "~2.84.3",
18
+ "@xyo-network/boundwitness-model": "~2.84.3",
19
+ "@xyo-network/diviner-boundwitness-model": "~2.84.3",
20
+ "@xyo-network/diviner-model": "~2.84.3",
21
+ "@xyo-network/diviner-wrapper": "~2.84.3",
22
+ "@xyo-network/module-model": "~2.84.3",
23
+ "@xyo-network/payload-builder": "~2.84.3",
24
+ "@xyo-network/payload-model": "~2.84.3"
25
25
  },
26
26
  "devDependencies": {
27
- "@xylabs/ts-scripts-yarn3": "^3.2.10",
28
- "@xylabs/tsconfig": "^3.2.10",
29
- "@xyo-network/account": "~2.84.1",
30
- "@xyo-network/diviner-boundwitness-memory": "~2.84.1",
31
- "@xyo-network/diviner-payload-memory": "~2.84.1",
32
- "@xyo-network/manifest": "~2.84.1",
33
- "@xyo-network/memory-archivist": "~2.84.1",
34
- "@xyo-network/node-memory": "~2.84.1",
27
+ "@xylabs/ts-scripts-yarn3": "^3.2.19",
28
+ "@xylabs/tsconfig": "^3.2.19",
29
+ "@xyo-network/account": "~2.84.3",
30
+ "@xyo-network/diviner-boundwitness-memory": "~2.84.3",
31
+ "@xyo-network/diviner-payload-memory": "~2.84.3",
32
+ "@xyo-network/manifest": "~2.84.3",
33
+ "@xyo-network/memory-archivist": "~2.84.3",
34
+ "@xyo-network/node-memory": "~2.84.3",
35
35
  "typescript": "^5.3.3"
36
36
  },
37
37
  "description": "Primary SDK for using XYO Protocol 2.0",
38
- "docs": "dist/docs.json",
39
38
  "types": "dist/node/index.d.ts",
40
39
  "exports": {
41
40
  ".": {
@@ -51,19 +50,19 @@
51
50
  },
52
51
  "node": {
53
52
  "require": {
54
- "types": "./dist/node/index.d.ts",
55
- "default": "./dist/node/index.js"
53
+ "types": "./dist/node/index.d.cts",
54
+ "default": "./dist/node/index.cjs"
56
55
  },
57
56
  "import": {
58
57
  "types": "./dist/node/index.d.mts",
59
- "default": "./dist/node/index.mjs"
58
+ "default": "./dist/node/index.js"
60
59
  }
61
60
  }
62
61
  },
63
62
  "./package.json": "./package.json"
64
63
  },
65
- "main": "dist/node/index.js",
66
- "module": "dist/node/index.mjs",
64
+ "main": "dist/node/index.cjs",
65
+ "module": "dist/node/index.js",
67
66
  "homepage": "https://xyo.network",
68
67
  "license": "LGPL-3.0-only",
69
68
  "publishConfig": {
@@ -74,5 +73,6 @@
74
73
  "url": "https://github.com/XYOracleNetwork/sdk-xyo-client-js.git"
75
74
  },
76
75
  "sideEffects": false,
77
- "version": "2.84.1"
76
+ "version": "2.84.3",
77
+ "type": "module"
78
78
  }
@@ -1,232 +0,0 @@
1
- // src/Schema.ts
2
- var StatefulDivinerSchema = "network.xyo.diviner.stateful";
3
-
4
- // src/Config.ts
5
- var StatefulDivinerConfigSchema = `${StatefulDivinerSchema}.config`;
6
-
7
- // src/Diviner.ts
8
- import { assertEx } from "@xylabs/assert";
9
- import { AbstractDiviner } from "@xyo-network/abstract-diviner";
10
- import { ArchivistWrapper } from "@xyo-network/archivist-wrapper";
11
- import { BoundWitnessBuilder } from "@xyo-network/boundwitness-builder";
12
- import { isBoundWitness } from "@xyo-network/boundwitness-model";
13
- import { BoundWitnessDivinerQuerySchema } from "@xyo-network/diviner-boundwitness-model";
14
- import { DivinerConfigSchema } from "@xyo-network/diviner-model";
15
- import { DivinerWrapper } from "@xyo-network/diviner-wrapper";
16
- import { isModuleState, ModuleStateSchema } from "@xyo-network/module-model";
17
- import { PayloadBuilder } from "@xyo-network/payload-builder";
18
- var moduleName = "StatefulDiviner";
19
- var StatefulDiviner = class extends AbstractDiviner {
20
- static configSchemas = [DivinerConfigSchema, StatefulDivinerConfigSchema];
21
- /**
22
- * The last state
23
- */
24
- _lastState;
25
- /**
26
- * Commit the internal state of the Diviner process. This is similar
27
- * to a transaction completion in a database and should only be called
28
- * when results have been successfully persisted to the appropriate
29
- * external stores.
30
- * @param nextState The state to commit
31
- */
32
- async commitState(nextState) {
33
- var _a;
34
- if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset))
35
- return;
36
- this._lastState = nextState;
37
- const archivist = await this.getArchivistForStateStore();
38
- const [bw] = await new BoundWitnessBuilder().payload(nextState).witness(this.account).build();
39
- await archivist.insert([bw, nextState]);
40
- }
41
- /**
42
- * Retrieves the archivist for the specified store
43
- * @param store The store to retrieve the archivist for
44
- * @returns The archivist for the specified store
45
- */
46
- async getArchivistForStateStore() {
47
- var _a;
48
- const name = assertEx((_a = this.config) == null ? void 0 : _a.stateStore.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`);
49
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`);
50
- return ArchivistWrapper.wrap(mod, this.account);
51
- }
52
- /**
53
- * Retrieves the BoundWitness Diviner for the specified store
54
- * @param store The store to retrieve the BoundWitness Diviner for
55
- * @returns The BoundWitness Diviner for the specified store
56
- */
57
- async getBoundWitnessDivinerForStateStore() {
58
- var _a;
59
- const name = assertEx((_a = this.config) == null ? void 0 : _a.stateStore.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`);
60
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`);
61
- return DivinerWrapper.wrap(mod, this.account);
62
- }
63
- /**
64
- * Retrieves the Payload Diviner for the specified store
65
- * @param store The store to retrieve the Payload Diviner for
66
- * @returns The Payload Diviner for the specified store
67
- */
68
- async getPayloadDivinerForStateStore() {
69
- var _a, _b;
70
- const name = assertEx((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`);
71
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`);
72
- return DivinerWrapper.wrap(mod, this.account);
73
- }
74
- /**
75
- * Retrieves the last state of the Diviner process. Used to recover state after
76
- * preemptions, reboots, etc.
77
- */
78
- async retrieveState() {
79
- if (this._lastState)
80
- return this._lastState;
81
- let hash = "";
82
- const diviner = await this.getBoundWitnessDivinerForStateStore();
83
- const query = await new PayloadBuilder({ schema: BoundWitnessDivinerQuerySchema }).fields({
84
- address: this.account.address,
85
- limit: 1,
86
- offset: 0,
87
- order: "desc",
88
- payload_schemas: [ModuleStateSchema]
89
- }).build();
90
- const boundWitnesses = await diviner.divine([query]);
91
- if (boundWitnesses.length > 0) {
92
- const boundWitness = boundWitnesses[0];
93
- if (isBoundWitness(boundWitness)) {
94
- hash = boundWitness.addresses.map((address, index) => ({ address, index })).filter(({ address }) => address === this.account.address).reduce(
95
- (prev, curr) => {
96
- var _a;
97
- return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === ModuleStateSchema ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
98
- },
99
- ""
100
- );
101
- }
102
- }
103
- if (hash) {
104
- const archivist = await this.getArchivistForStateStore();
105
- const payload = (await archivist.get([hash])).find(isModuleState);
106
- if (payload) {
107
- return payload;
108
- }
109
- }
110
- return void 0;
111
- }
112
- };
113
-
114
- // src/DivinerMixin.ts
115
- import { assertEx as assertEx2 } from "@xylabs/assert";
116
- import { asArchivistInstance } from "@xyo-network/archivist";
117
- import { BoundWitnessBuilder as BoundWitnessBuilder2 } from "@xyo-network/boundwitness-builder";
118
- import { isBoundWitness as isBoundWitness2 } from "@xyo-network/boundwitness-model";
119
- import { BoundWitnessDivinerQuerySchema as BoundWitnessDivinerQuerySchema2 } from "@xyo-network/diviner-boundwitness-model";
120
- import { asDivinerInstance } from "@xyo-network/diviner-model";
121
- import {
122
- isModuleState as isModuleState2,
123
- ModuleStateSchema as ModuleStateSchema2
124
- } from "@xyo-network/module-model";
125
- import { PayloadBuilder as PayloadBuilder2 } from "@xyo-network/payload-builder";
126
- var moduleName2 = "StatefulModuleMixin";
127
- var StatefulModuleMixin = (ModuleBase) => {
128
- class StatefulModuleBase extends ModuleBase {
129
- _lastState;
130
- /**
131
- * Commit the internal state of the Diviner process. This is similar
132
- * to a transaction completion in a database and should only be called
133
- * when results have been successfully persisted to the appropriate
134
- * external stores.
135
- * @param nextState The state to commit
136
- */
137
- async commitState(nextState) {
138
- var _a;
139
- if (nextState.state.offset === ((_a = this._lastState) == null ? void 0 : _a.state.offset))
140
- return;
141
- this._lastState = nextState;
142
- const archivist = await this.getArchivistForStore();
143
- const [bw] = await new BoundWitnessBuilder2().payload(nextState).build();
144
- await archivist.insert([bw, nextState]);
145
- }
146
- /**
147
- * Retrieves the archivist for the specified store
148
- * @param store The store to retrieve the archivist for
149
- * @returns The archivist for the specified store
150
- */
151
- async getArchivistForStore() {
152
- var _a, _b;
153
- const name = assertEx2((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.archivist, () => `${moduleName2}: Config for stateStore.archivist not specified`);
154
- const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.archivist`);
155
- const instance = asArchivistInstance(mod);
156
- return assertEx2(instance, () => `${moduleName2}: Failed to wrap archivist instance`);
157
- }
158
- /**
159
- * Retrieves the BoundWitness Diviner for the specified store
160
- * @param store The store to retrieve the BoundWitness Diviner for
161
- * @returns The BoundWitness Diviner for the specified store
162
- */
163
- async getBoundWitnessDivinerForStore() {
164
- var _a, _b;
165
- const name = assertEx2(
166
- (_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.boundWitnessDiviner,
167
- () => `${moduleName2}: Config for stateStore.boundWitnessDiviner not specified`
168
- );
169
- const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.boundWitnessDiviner`);
170
- const instance = asDivinerInstance(mod);
171
- return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
172
- }
173
- /**
174
- * Retrieves the Payload Diviner for the specified store
175
- * @param store The store to retrieve the Payload Diviner for
176
- * @returns The Payload Diviner for the specified store
177
- */
178
- async getPayloadDivinerForStateStore() {
179
- var _a, _b;
180
- const name = assertEx2((_b = (_a = this.config) == null ? void 0 : _a.stateStore) == null ? void 0 : _b.payloadDiviner, () => `${moduleName2}: Config for stateStore.payloadDiviner not specified`);
181
- const mod = assertEx2(await this.resolve(name), () => `${moduleName2}: Failed to resolve stateStore.payloadDiviner`);
182
- const instance = asDivinerInstance(mod);
183
- return assertEx2(instance, () => `${moduleName2}: Failed to wrap diviner instance`);
184
- }
185
- /**
186
- * Retrieves the last state of the Diviner process. Used to recover state after
187
- * preemptions, reboots, etc.
188
- */
189
- async retrieveState() {
190
- if (this._lastState)
191
- return this._lastState;
192
- let hash = "";
193
- const diviner = await this.getBoundWitnessDivinerForStore();
194
- const query = await new PayloadBuilder2({ schema: BoundWitnessDivinerQuerySchema2 }).fields({
195
- // address: this.account.address,
196
- limit: 1,
197
- offset: 0,
198
- order: "desc",
199
- payload_schemas: [ModuleStateSchema2]
200
- }).build();
201
- const boundWitnesses = await diviner.divine([query]);
202
- if (boundWitnesses.length > 0) {
203
- const boundWitness = boundWitnesses[0];
204
- if (isBoundWitness2(boundWitness)) {
205
- hash = boundWitness.addresses.map((address, index) => ({ address, index })).reduce(
206
- (prev, curr) => {
207
- var _a;
208
- return ((_a = boundWitness.payload_schemas) == null ? void 0 : _a[curr == null ? void 0 : curr.index]) === ModuleStateSchema2 ? boundWitness.payload_hashes[curr == null ? void 0 : curr.index] : prev;
209
- },
210
- ""
211
- );
212
- }
213
- }
214
- if (hash) {
215
- const archivist = await this.getArchivistForStore();
216
- const payload = (await archivist.get([hash])).find(isModuleState2);
217
- if (payload) {
218
- return payload;
219
- }
220
- }
221
- return void 0;
222
- }
223
- }
224
- return StatefulModuleBase;
225
- };
226
- export {
227
- StatefulDiviner,
228
- StatefulDivinerConfigSchema,
229
- StatefulDivinerSchema,
230
- StatefulModuleMixin
231
- };
232
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
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'\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: string\n boundWitnessDiviner: string\n payloadDiviner: string\n }\n}>\n","import { assertEx } from '@xylabs/assert'\nimport { AbstractDiviner } from '@xyo-network/abstract-diviner'\nimport { ArchivistWrapper } from '@xyo-network/archivist-wrapper'\nimport { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'\nimport { isBoundWitness } from '@xyo-network/boundwitness-model'\nimport { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'\nimport { DivinerConfigSchema, DivinerModule, 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 } 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<DivinerModule<TParams>, TIn, TOut> = DivinerModuleEventData<DivinerModule<TParams>, TIn, TOut>,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: string[] = [DivinerConfigSchema, StatefulDivinerConfigSchema]\n\n /**\n * The last state\n */\n protected _lastState?: 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: 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).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<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: string = ''\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 .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\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\n }\n }\n return undefined\n }\n}\n","import { assertEx } from '@xylabs/assert'\nimport { asArchivistInstance } from '@xyo-network/archivist'\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'\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?: 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: 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 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<ModuleState<TState> | undefined> {\n if (this._lastState) return this._lastState\n let hash: string = ''\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 .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n '',\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\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],"mappings":";AAAO,IAAM,wBAAwB;;;ACO9B,IAAM,8BAA8B,GAAG,qBAAqB;;;ACPnE,SAAS,gBAAgB;AACzB,SAAS,uBAAuB;AAChC,SAAS,wBAAwB;AACjC,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAC/B,SAA0C,sCAAsC;AAChF,SAAS,2BAAkE;AAC3E,SAAS,sBAAsB;AAC/B,SAAS,eAA4B,yBAA0C;AAC/E,SAAS,sBAAsB;AAM/B,IAAM,aAAa;AAKZ,IAAe,kBAAf,cAMG,gBAAgD;AAAA,EACxD,OAAyB,gBAA0B,CAAC,qBAAqB,2BAA2B;AAAA;AAAA;AAAA;AAAA,EAK1F;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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AACA,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["assertEx","BoundWitnessBuilder","isBoundWitness","BoundWitnessDivinerQuerySchema","isModuleState","ModuleStateSchema","PayloadBuilder","moduleName"]}