@xyo-network/diviner-stateful 7.4.1 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"Diviner.d.ts","sourceRoot":"","sources":["../../src/Diviner.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,eAAe,EAAE,wBAAwB,EAAE,uBAAuB,EACnE,MAAM,kCAAkC,CAAA;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAA;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAA;AAGnE,OAAO,KAAK,EACV,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,sBAAsB,EAAE,aAAa,EACrF,MAAM,gCAAgC,CAAA;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAA;AACjE,OAAO,KAAK,EACV,eAAe,EAAE,WAAW,EAAE,qBAAqB,EAAE,eAAe,EACrE,MAAM,+BAA+B,CAAA;AAEtC,OAAO,KAAK,EACV,OAAO,EAAE,MAAM,EAEhB,MAAM,2BAA2B,CAAA;AAMlC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AAIxD;;GAEG;AACH,8BAAsB,eAAe,CACnC,OAAO,SAAS,qBAAqB,GAAG,qBAAqB,EAC7D,GAAG,SAAS,OAAO,GAAG,OAAO,EAC7B,IAAI,SAAS,OAAO,GAAG,OAAO,EAC9B,UAAU,SAAS,sBAAsB,CAAC,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,sBAAsB,CAChH,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,EACnC,GAAG,EACH,IAAI,CACL,EACD,MAAM,SAAS,eAAe,GAAG,eAAe,CAChD,SAAQ,eAAe,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,UAAU,C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C,aAAa,CAAC,eAAe,CAAC,aAAa,CAAC,CAAC,EAAE,sBAAsB,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAMtL;;;;OAIG;cACa,8BAA8B,IAAI,OAAO,CAAC,cAAc,CAAC,aAAa,CAAC,aAAa,CAAC,eAAe,CAAC,aAAa,CAAC,CAAC,EAAE,sBAAsB,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAMjL;;;OAGG;cACa,aAAa,IAAI,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;CA4C1E"}
@@ -58,7 +58,8 @@ var StatefulDiviner = class extends AbstractDiviner {
58
58
  if (toSafeJson(nextState.state) === toSafeJson(this._lastState?.state)) return;
59
59
  this._lastState = nextState;
60
60
  const archivist = await this.getArchivistForStateStore();
61
- const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build();
61
+ const boundWitnessBuilder = new BoundWitnessBuilder();
62
+ const [bw] = await boundWitnessBuilder.payload(nextState).signer(this.account).build();
62
63
  await archivist.insert([bw, nextState]);
63
64
  }
64
65
  /**
@@ -99,7 +100,8 @@ var StatefulDiviner = class extends AbstractDiviner {
99
100
  if (this._lastState) return this._lastState;
100
101
  let hash = null;
101
102
  const diviner = await this.getBoundWitnessDivinerForStateStore();
102
- const query = new PayloadBuilder({ schema: BoundWitnessDivinerQuerySchema }).fields({
103
+ const payloadBuilder = new PayloadBuilder({ schema: BoundWitnessDivinerQuerySchema });
104
+ const query = payloadBuilder.fields({
103
105
  address: this.account.address,
104
106
  limit: 1,
105
107
  cursor: SequenceConstants.minLocalSequence,
@@ -110,15 +112,20 @@ var StatefulDiviner = class extends AbstractDiviner {
110
112
  if (boundWitnesses.length > 0) {
111
113
  const boundWitness = boundWitnesses[0];
112
114
  if (isBoundWitness(boundWitness)) {
113
- hash = boundWitness.addresses.map((address, index) => ({ address, index })).filter(({ address }) => address === this.account.address).reduce(
114
- (prev, curr) => boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev,
115
- null
116
- );
115
+ const matchingAddresses = boundWitness.addresses.map((address, index) => ({ address, index })).filter(({ address }) => address === this.account.address);
116
+ let foundHash = null;
117
+ for (const curr of matchingAddresses) {
118
+ if (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema) {
119
+ foundHash = boundWitness.payload_hashes[curr?.index];
120
+ }
121
+ }
122
+ hash = foundHash;
117
123
  }
118
124
  }
119
125
  if (hash !== null) {
120
126
  const archivist = await this.getArchivistForStateStore();
121
- const payload = (await archivist.get([hash])).find(isModuleState);
127
+ const storedPayloads = await archivist.get([hash]);
128
+ const payload = storedPayloads.find(isModuleState);
122
129
  if (payload) {
123
130
  return payload;
124
131
  }
@@ -145,6 +152,8 @@ import {
145
152
  var moduleName2 = "StatefulModuleMixin";
146
153
  var StatefulModuleMixin = (ModuleBase) => {
147
154
  class StatefulModuleBase extends ModuleBase {
155
+ // Private fields cannot be # on an exported mixin class (TS4094).
156
+ // eslint-disable-next-line unicorn/prefer-private-class-fields
148
157
  _lastState;
149
158
  /**
150
159
  * Commit the internal state of the Diviner process. This is similar
@@ -157,7 +166,8 @@ var StatefulModuleMixin = (ModuleBase) => {
157
166
  if (toSafeJson2(nextState.state) === toSafeJson2(this._lastState?.state)) return;
158
167
  this._lastState = nextState;
159
168
  const archivist = await this.getArchivistForStore();
160
- const [bw] = await new BoundWitnessBuilder2().payload(nextState).build();
169
+ const boundWitnessBuilder = new BoundWitnessBuilder2();
170
+ const [bw] = await boundWitnessBuilder.payload(nextState).build();
161
171
  await archivist.insert([bw, nextState]);
162
172
  }
163
173
  /**
@@ -204,7 +214,8 @@ var StatefulModuleMixin = (ModuleBase) => {
204
214
  if (this._lastState) return this._lastState;
205
215
  let hash = null;
206
216
  const diviner = await this.getBoundWitnessDivinerForStore();
207
- const query = new PayloadBuilder2({ schema: BoundWitnessDivinerQuerySchema2 }).fields({
217
+ const payloadBuilder = new PayloadBuilder2({ schema: BoundWitnessDivinerQuerySchema2 });
218
+ const query = payloadBuilder.fields({
208
219
  // address: this.account.address,
209
220
  limit: 1,
210
221
  cursor: SequenceConstants2.minLocalSequence,
@@ -215,15 +226,20 @@ var StatefulModuleMixin = (ModuleBase) => {
215
226
  if (boundWitnesses.length > 0) {
216
227
  const boundWitness = boundWitnesses[0];
217
228
  if (isBoundWitness2(boundWitness)) {
218
- hash = boundWitness.addresses.map((address, index) => ({ address, index })).reduce(
219
- (prev, curr) => boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema2 ? boundWitness.payload_hashes[curr?.index] : prev,
220
- null
221
- );
229
+ const indexedAddresses = boundWitness.addresses.map((address, index) => ({ address, index }));
230
+ let foundHash = null;
231
+ for (const curr of indexedAddresses) {
232
+ if (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema2) {
233
+ foundHash = boundWitness.payload_hashes[curr?.index];
234
+ }
235
+ }
236
+ hash = foundHash;
222
237
  }
223
238
  }
224
239
  if (hash !== null) {
225
240
  const archivist = await this.getArchivistForStore();
226
- const payload = (await archivist.get([hash])).find(isModuleState2);
241
+ const storedPayloads = await archivist.get([hash]);
242
+ const payload = storedPayloads.find(isModuleState2);
227
243
  if (payload) {
228
244
  return payload;
229
245
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/Config.ts", "../../src/Schema.ts", "../../src/Diviner.ts", "../../src/DivinerMixin.ts"],
4
- "sourcesContent": ["import {\n zodAsFactory, zodIsFactory, zodToFactory,\n} from '@ariestools/sdk'\nimport type { DivinerConfig } from '@xyo-network/sdk/diviner-model'\nimport { DivinerConfigZod } from '@xyo-network/sdk/diviner-model'\nimport type { ModuleIdentifier } from '@xyo-network/sdk/module-model'\nimport { asSchema } from '@xyo-network/sdk-protocol'\nimport * as z from 'zod/mini'\n\nimport { StatefulDivinerSchema } from './Schema.ts'\n\n/**\n * The schema for a Stateful Diviner config\n */\nexport const StatefulDivinerConfigSchema: string & {\n readonly __schema: true\n} = asSchema(`${StatefulDivinerSchema}.config`, true)\n/**\n * The schema for a Stateful Diviner config\n */\nexport type StatefulDivinerConfigSchema = typeof StatefulDivinerConfigSchema\n\nexport const StatefulDivinerConfigZod: z.ZodMiniObject<\n Omit<(typeof DivinerConfigZod)['shape'], 'schema'> & {\n schema: z.ZodMiniLiteral<StatefulDivinerConfigSchema>\n stateStore: z.ZodMiniObject<{\n archivist: z.ZodMiniCustom<ModuleIdentifier, ModuleIdentifier>\n boundWitnessDiviner: z.ZodMiniCustom<ModuleIdentifier, ModuleIdentifier>\n payloadDiviner: z.ZodMiniCustom<ModuleIdentifier, ModuleIdentifier>\n }, z.core.$strip>\n },\n z.core.$strip\n> = z.extend(DivinerConfigZod, {\n schema: z.literal(StatefulDivinerConfigSchema),\n stateStore: z.object({\n archivist: z.custom<ModuleIdentifier>(),\n boundWitnessDiviner: z.custom<ModuleIdentifier>(),\n payloadDiviner: z.custom<ModuleIdentifier>(),\n }),\n})\n\nexport type StatefulDivinerConfigBase = z.infer<typeof StatefulDivinerConfigZod>\n\nexport const isStatefulDivinerConfig: ReturnType<typeof zodIsFactory<StatefulDivinerConfigBase>> = zodIsFactory(StatefulDivinerConfigZod)\nexport const asStatefulDivinerConfig: ReturnType<typeof zodAsFactory<StatefulDivinerConfigBase>> = zodAsFactory(StatefulDivinerConfigZod, 'asStatefulDivinerConfig')\nexport const toStatefulDivinerConfig: ReturnType<typeof zodToFactory<StatefulDivinerConfigBase>> = zodToFactory(StatefulDivinerConfigZod, 'toStatefulDivinerConfig')\n\n/**\n * The config for a Stateful Diviner\n */\nexport type StatefulDivinerConfig = DivinerConfig<{\n schema: StatefulDivinerConfigSchema\n stateStore: {\n archivist: ModuleIdentifier\n boundWitnessDiviner: ModuleIdentifier\n payloadDiviner: ModuleIdentifier\n }\n}>\n", "import { asSchema } from '@xyo-network/sdk-protocol'\n\nexport const StatefulDivinerSchema: 'network.xyo.diviner.stateful' & {\n readonly __schema: true\n} = asSchema('network.xyo.diviner.stateful', true)\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n", "import type { Hash } from '@ariestools/sdk'\nimport { assertEx, toSafeJson } from '@ariestools/sdk'\nimport type {\n ArchivistConfig, ArchivistModuleEventData, ArchivistModuleInstance,\n} from '@xyo-network/sdk/archivist-model'\nimport { ArchivistWrapper } from '@xyo-network/sdk/archivist-wrapper'\nimport { AbstractDiviner } from '@xyo-network/sdk/diviner-abstract'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/sdk/diviner-boundwitness/model'\nimport { BoundWitnessDivinerQuerySchema } from '@xyo-network/sdk/diviner-boundwitness/model'\nimport type {\n DivinerConfig, DivinerInstance, DivinerModule, DivinerModuleEventData, DivinerParams,\n} from '@xyo-network/sdk/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/sdk/diviner-wrapper'\nimport type {\n AnyConfigSchema, ModuleState, QueryableModuleParams, StateDictionary,\n} from '@xyo-network/sdk/module-model'\nimport { isModuleState, ModuleStateSchema } from '@xyo-network/sdk/module-model'\nimport type {\n Payload, Schema,\n WithStorageMeta,\n} from '@xyo-network/sdk-protocol'\nimport {\n BoundWitnessBuilder, isBoundWitness, PayloadBuilder, SequenceConstants,\n} from '@xyo-network/sdk-protocol'\n\nimport { StatefulDivinerConfigSchema } from './Config.ts'\nimport type { StatefulDivinerParams } from './Params.ts'\n\nconst moduleName = 'StatefulDiviner'\n\n/**\n * A Diviner that maintains state\n */\nexport abstract class StatefulDiviner<\n TParams extends StatefulDivinerParams = StatefulDivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, StatefulDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = StatefulDivinerConfigSchema\n\n /**\n * The last state\n */\n protected _lastState?: 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>): Promise<void> {\n // Don't commit state if no state has changed\n if (toSafeJson(nextState.state) === toSafeJson(this._lastState?.state)) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStateStore()\n const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n protected async getArchivistForStateStore(): Promise<ArchivistWrapper<ArchivistModuleInstance<QueryableModuleParams<AnyConfigSchema<ArchivistConfig>>, ArchivistModuleEventData>>> {\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(): Promise<DivinerWrapper<DivinerModule<DivinerParams<AnyConfigSchema<DivinerConfig>>, DivinerModuleEventData>, Payload, Payload>> {\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(): Promise<DivinerWrapper<DivinerModule<DivinerParams<AnyConfigSchema<DivinerConfig>>, DivinerModuleEventData>, Payload, Payload>> {\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: Hash | null = null\n const diviner = await this.getBoundWitnessDivinerForStateStore()\n const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n address: this.account.address,\n limit: 1,\n cursor: SequenceConstants.minLocalSequence,\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\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n null as Hash | null,\n )\n }\n }\n\n // If we able to located the last state\n if (hash !== null) {\n // Get last state\n const archivist = await this.getArchivistForStateStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as WithStorageMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n}\n", "import type { Hash } from '@ariestools/sdk'\nimport { assertEx, toSafeJson } from '@ariestools/sdk'\nimport { asArchivistInstance } from '@xyo-network/sdk/archivist-model'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/sdk/diviner-boundwitness/model'\nimport { BoundWitnessDivinerQuerySchema } from '@xyo-network/sdk/diviner-boundwitness/model'\nimport { asDivinerInstance } from '@xyo-network/sdk/diviner-model'\nimport type {\n AnyConfigSchema,\n ModuleInstance,\n ModuleState,\n QueryableModuleParams,\n StateDictionary,\n} from '@xyo-network/sdk/module-model'\nimport {\n isModuleState,\n ModuleStateSchema,\n} from '@xyo-network/sdk/module-model'\nimport type { WithStorageMeta } from '@xyo-network/sdk-protocol'\nimport {\n BoundWitnessBuilder, isBoundWitness, PayloadBuilder, SequenceConstants,\n} from '@xyo-network/sdk-protocol'\n\nimport type { StatefulDivinerConfig } from './Config.ts'\n\nexport type StatefulModuleParams = QueryableModuleParams<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 * @internal\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 (toSafeJson(nextState.state) === toSafeJson(this._lastState?.state)) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStore()\n // const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n const [bw] = await 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 /**\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 /**\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: Hash | null = null\n const diviner = await this.getBoundWitnessDivinerForStore()\n const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n .fields({\n // address: this.account.address,\n limit: 1,\n cursor: SequenceConstants.minLocalSequence,\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\n .reduce(\n (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),\n null as Hash | null,\n )\n }\n }\n\n // If we able to located the last state\n if (hash !== null) {\n // Get last state\n const archivist = await this.getArchivistForStore()\n const payload = (await archivist.get([hash])).find(isModuleState<TState>)\n if (payload) {\n return payload as WithStorageMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n }\n return StatefulModuleBase\n}\n"],
5
- "mappings": ";AAAA;AAAA,EACE;AAAA,EAAc;AAAA,EAAc;AAAA,OACvB;AAEP,SAAS,wBAAwB;AAEjC,SAAS,YAAAA,iBAAgB;AACzB,YAAY,OAAO;;;ACPnB,SAAS,gBAAgB;AAElB,IAAM,wBAET,SAAS,gCAAgC,IAAI;;;ADU1C,IAAM,8BAETC,UAAS,GAAG,qBAAqB,WAAW,IAAI;AAM7C,IAAM,2BAUP,SAAO,kBAAkB;AAAA,EAC7B,QAAU,UAAQ,2BAA2B;AAAA,EAC7C,YAAc,SAAO;AAAA,IACnB,WAAa,SAAyB;AAAA,IACtC,qBAAuB,SAAyB;AAAA,IAChD,gBAAkB,SAAyB;AAAA,EAC7C,CAAC;AACH,CAAC;AAIM,IAAM,0BAAsF,aAAa,wBAAwB;AACjI,IAAM,0BAAsF,aAAa,0BAA0B,yBAAyB;AAC5J,IAAM,0BAAsF,aAAa,0BAA0B,yBAAyB;;;AE5CnK,SAAS,UAAU,kBAAkB;AAIrC,SAAS,wBAAwB;AACjC,SAAS,uBAAuB;AAEhC,SAAS,sCAAsC;AAI/C,SAAS,sBAAsB;AAI/B,SAAS,eAAe,yBAAyB;AAKjD;AAAA,EACE;AAA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,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;",
4
+ "sourcesContent": ["import {\n zodAsFactory, zodIsFactory, zodToFactory,\n} from '@ariestools/sdk'\nimport type { DivinerConfig } from '@xyo-network/sdk/diviner-model'\nimport { DivinerConfigZod } from '@xyo-network/sdk/diviner-model'\nimport type { ModuleIdentifier } from '@xyo-network/sdk/module-model'\nimport { asSchema } from '@xyo-network/sdk-protocol'\nimport * as z from 'zod/mini'\n\nimport { StatefulDivinerSchema } from './Schema.ts'\n\n/**\n * The schema for a Stateful Diviner config\n */\nexport const StatefulDivinerConfigSchema: string & {\n readonly __schema: true\n} = asSchema(`${StatefulDivinerSchema}.config`, true)\n/**\n * The schema for a Stateful Diviner config\n */\nexport type StatefulDivinerConfigSchema = typeof StatefulDivinerConfigSchema\n\nexport const StatefulDivinerConfigZod: z.ZodMiniObject<\n Omit<(typeof DivinerConfigZod)['shape'], 'schema'> & {\n schema: z.ZodMiniLiteral<StatefulDivinerConfigSchema>\n stateStore: z.ZodMiniObject<{\n archivist: z.ZodMiniCustom<ModuleIdentifier, ModuleIdentifier>\n boundWitnessDiviner: z.ZodMiniCustom<ModuleIdentifier, ModuleIdentifier>\n payloadDiviner: z.ZodMiniCustom<ModuleIdentifier, ModuleIdentifier>\n }, z.core.$strip>\n },\n z.core.$strip\n> = z.extend(DivinerConfigZod, {\n schema: z.literal(StatefulDivinerConfigSchema),\n stateStore: z.object({\n archivist: z.custom<ModuleIdentifier>(),\n boundWitnessDiviner: z.custom<ModuleIdentifier>(),\n payloadDiviner: z.custom<ModuleIdentifier>(),\n }),\n})\n\nexport type StatefulDivinerConfigBase = z.infer<typeof StatefulDivinerConfigZod>\n\nexport const isStatefulDivinerConfig: ReturnType<typeof zodIsFactory<StatefulDivinerConfigBase>> = zodIsFactory(StatefulDivinerConfigZod)\nexport const asStatefulDivinerConfig: ReturnType<typeof zodAsFactory<StatefulDivinerConfigBase>> = zodAsFactory(StatefulDivinerConfigZod, 'asStatefulDivinerConfig')\nexport const toStatefulDivinerConfig: ReturnType<typeof zodToFactory<StatefulDivinerConfigBase>> = zodToFactory(StatefulDivinerConfigZod, 'toStatefulDivinerConfig')\n\n/**\n * The config for a Stateful Diviner\n */\nexport type StatefulDivinerConfig = DivinerConfig<{\n schema: StatefulDivinerConfigSchema\n stateStore: {\n archivist: ModuleIdentifier\n boundWitnessDiviner: ModuleIdentifier\n payloadDiviner: ModuleIdentifier\n }\n}>\n", "import { asSchema } from '@xyo-network/sdk-protocol'\n\nexport const StatefulDivinerSchema: 'network.xyo.diviner.stateful' & {\n readonly __schema: true\n} = asSchema('network.xyo.diviner.stateful', true)\nexport type StatefulDivinerSchema = typeof StatefulDivinerSchema\n", "import type { Hash } from '@ariestools/sdk'\nimport { assertEx, toSafeJson } from '@ariestools/sdk'\nimport type {\n ArchivistConfig, ArchivistModuleEventData, ArchivistModuleInstance,\n} from '@xyo-network/sdk/archivist-model'\nimport { ArchivistWrapper } from '@xyo-network/sdk/archivist-wrapper'\nimport { AbstractDiviner } from '@xyo-network/sdk/diviner-abstract'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/sdk/diviner-boundwitness/model'\nimport { BoundWitnessDivinerQuerySchema } from '@xyo-network/sdk/diviner-boundwitness/model'\nimport type {\n DivinerConfig, DivinerInstance, DivinerModule, DivinerModuleEventData, DivinerParams,\n} from '@xyo-network/sdk/diviner-model'\nimport { DivinerWrapper } from '@xyo-network/sdk/diviner-wrapper'\nimport type {\n AnyConfigSchema, ModuleState, QueryableModuleParams, StateDictionary,\n} from '@xyo-network/sdk/module-model'\nimport { isModuleState, ModuleStateSchema } from '@xyo-network/sdk/module-model'\nimport type {\n Payload, Schema,\n WithStorageMeta,\n} from '@xyo-network/sdk-protocol'\nimport {\n BoundWitnessBuilder, isBoundWitness, PayloadBuilder, SequenceConstants,\n} from '@xyo-network/sdk-protocol'\n\nimport { StatefulDivinerConfigSchema } from './Config.ts'\nimport type { StatefulDivinerParams } from './Params.ts'\n\nconst moduleName = 'StatefulDiviner'\n\n/**\n * A Diviner that maintains state\n */\nexport abstract class StatefulDiviner<\n TParams extends StatefulDivinerParams = StatefulDivinerParams,\n TIn extends Payload = Payload,\n TOut extends Payload = Payload,\n TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<\n DivinerInstance<TParams, TIn, TOut>,\n TIn,\n TOut\n >,\n TState extends StateDictionary = StateDictionary,\n> extends AbstractDiviner<TParams, TIn, TOut, TEventData> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, StatefulDivinerConfigSchema]\n static override readonly defaultConfigSchema: Schema = StatefulDivinerConfigSchema\n\n /**\n * The last state\n */\n protected _lastState?: 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>): Promise<void> {\n // Don't commit state if no state has changed\n if (toSafeJson(nextState.state) === toSafeJson(this._lastState?.state)) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStateStore()\n const boundWitnessBuilder = new BoundWitnessBuilder()\n const [bw] = await boundWitnessBuilder.payload(nextState).signer(this.account).build()\n await archivist.insert([bw, nextState])\n }\n\n /**\n * Retrieves the archivist for the specified store\n * @param store The store to retrieve the archivist for\n * @returns The archivist for the specified store\n */\n protected async getArchivistForStateStore(): Promise<ArchivistWrapper<ArchivistModuleInstance<QueryableModuleParams<AnyConfigSchema<ArchivistConfig>>, ArchivistModuleEventData>>> {\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(): Promise<DivinerWrapper<DivinerModule<DivinerParams<AnyConfigSchema<DivinerConfig>>, DivinerModuleEventData>, Payload, Payload>> {\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(): Promise<DivinerWrapper<DivinerModule<DivinerParams<AnyConfigSchema<DivinerConfig>>, DivinerModuleEventData>, Payload, Payload>> {\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: Hash | null = null\n const diviner = await this.getBoundWitnessDivinerForStateStore()\n const payloadBuilder = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n const query = payloadBuilder\n .fields({\n address: this.account.address,\n limit: 1,\n cursor: SequenceConstants.minLocalSequence,\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 const matchingAddresses = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n .filter(({ address }) => address === this.account.address)\n let foundHash: Hash | null = null\n for (const curr of matchingAddresses) {\n if (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema) {\n foundHash = boundWitness.payload_hashes[curr?.index]\n }\n }\n hash = foundHash\n }\n }\n\n // If we able to located the last state\n if (hash !== null) {\n // Get last state\n const archivist = await this.getArchivistForStateStore()\n const storedPayloads = await archivist.get([hash])\n const payload = storedPayloads.find(isModuleState<TState>)\n if (payload) {\n return payload as WithStorageMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n}\n", "import type { Hash } from '@ariestools/sdk'\nimport { assertEx, toSafeJson } from '@ariestools/sdk'\nimport { asArchivistInstance } from '@xyo-network/sdk/archivist-model'\nimport type { BoundWitnessDivinerQueryPayload } from '@xyo-network/sdk/diviner-boundwitness/model'\nimport { BoundWitnessDivinerQuerySchema } from '@xyo-network/sdk/diviner-boundwitness/model'\nimport { asDivinerInstance } from '@xyo-network/sdk/diviner-model'\nimport type {\n AnyConfigSchema,\n ModuleInstance,\n ModuleState,\n QueryableModuleParams,\n StateDictionary,\n} from '@xyo-network/sdk/module-model'\nimport {\n isModuleState,\n ModuleStateSchema,\n} from '@xyo-network/sdk/module-model'\nimport type { WithStorageMeta } from '@xyo-network/sdk-protocol'\nimport {\n BoundWitnessBuilder, isBoundWitness, PayloadBuilder, SequenceConstants,\n} from '@xyo-network/sdk-protocol'\n\nimport type { StatefulDivinerConfig } from './Config.ts'\n\nexport type StatefulModuleParams = QueryableModuleParams<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 * @internal\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 // Private fields cannot be # on an exported mixin class (TS4094).\n // eslint-disable-next-line unicorn/prefer-private-class-fields\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 (toSafeJson(nextState.state) === toSafeJson(this._lastState?.state)) return\n this._lastState = nextState\n const archivist = await this.getArchivistForStore()\n // const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()\n const boundWitnessBuilder = new BoundWitnessBuilder()\n const [bw] = await 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 /**\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 /**\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: Hash | null = null\n const diviner = await this.getBoundWitnessDivinerForStore()\n const payloadBuilder = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })\n const query = payloadBuilder\n .fields({\n // address: this.account.address,\n limit: 1,\n cursor: SequenceConstants.minLocalSequence,\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 const indexedAddresses = boundWitness.addresses\n .map((address, index) => ({ address, index }))\n // .filter(({ address }) => address === this.account.address)\n let foundHash: Hash | null = null\n for (const curr of indexedAddresses) {\n if (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema) {\n foundHash = boundWitness.payload_hashes[curr?.index]\n }\n }\n hash = foundHash\n }\n }\n\n // If we able to located the last state\n if (hash !== null) {\n // Get last state\n const archivist = await this.getArchivistForStore()\n const storedPayloads = await archivist.get([hash])\n const payload = storedPayloads.find(isModuleState<TState>)\n if (payload) {\n return payload as WithStorageMeta<ModuleState<TState>>\n }\n }\n return undefined\n }\n }\n return Statef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", "isModuleState", "ModuleStateSchema", "BoundWitnessBuilder", "isBoundWitness", "PayloadBuilder", "SequenceConstants", "moduleName"]
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/diviner-stateful",
3
- "version": "7.4.1",
3
+ "version": "8.0.0",
4
4
  "description": "Primary SDK for using XYO Protocol 2.0",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -34,30 +34,30 @@
34
34
  "README.md"
35
35
  ],
36
36
  "dependencies": {
37
- "@xyo-network/sdk": "~7.4.1"
37
+ "@xyo-network/sdk": "~8.0.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@ariestools/sdk": "~8.3.0",
41
- "@ariestools/testing": "~8.3.0",
42
- "@ariestools/toolchain": "~9.2.1",
43
- "@ariestools/tsconfig": "~9.2.1",
40
+ "@ariestools/sdk": "~9.0.0",
41
+ "@ariestools/testing": "~9.0.0",
42
+ "@ariestools/toolchain": "~10.0.7",
43
+ "@ariestools/tsconfig": "~10.0.7",
44
44
  "@opentelemetry/api": "~1.9.1",
45
- "@xyo-network/sdk-protocol": "~7.4.2",
45
+ "@xyo-network/sdk-protocol": "~8.0.0",
46
46
  "eslint": "~10.10.0",
47
47
  "eslint-import-resolver-typescript": "~4.4.5",
48
48
  "tslib": "~2.8.1",
49
49
  "typescript": "~6.0.3",
50
- "vite": "~8.2.2",
51
- "vitest": "~5.0.0",
52
- "zod": "~4.5.4"
50
+ "vite": "~8.3.0",
51
+ "vitest": "~5.0.1",
52
+ "zod": "~4.6.5"
53
53
  },
54
54
  "peerDependencies": {
55
- "@ariestools/sdk": "^8.1",
56
- "@xyo-network/sdk-protocol": "^7.4",
57
- "zod": "^4.4"
55
+ "@ariestools/sdk": "^9.0",
56
+ "@xyo-network/sdk-protocol": "^8.0",
57
+ "zod": "^4.6"
58
58
  },
59
59
  "engines": {
60
- "node": "^24"
60
+ "node": "^26"
61
61
  },
62
62
  "publishConfig": {
63
63
  "access": "public"