@xyo-network/diviner-indexing-memory 5.3.22 → 5.3.24

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/diviner-indexing-memory",
3
- "version": "5.3.22",
3
+ "version": "5.3.24",
4
4
  "description": "Primary SDK for using XYO Protocol 2.0",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -30,53 +30,49 @@
30
30
  "types": "dist/neutral/index.d.ts",
31
31
  "files": [
32
32
  "dist",
33
- "src",
34
33
  "!**/*.bench.*",
35
34
  "!**/*.spec.*",
36
35
  "!**/*.test.*",
37
36
  "README.md"
38
37
  ],
39
38
  "dependencies": {
40
- "@xyo-network/archivist-wrapper": "~5.3.22",
41
- "@xyo-network/boundwitness-builder": "~5.3.22",
42
- "@xyo-network/boundwitness-model": "~5.3.22",
43
- "@xyo-network/diviner-abstract": "~5.3.22",
44
- "@xyo-network/diviner-boundwitness-model": "~5.3.22",
45
- "@xyo-network/diviner-indexing-model": "~5.3.22",
46
- "@xyo-network/diviner-model": "~5.3.22",
47
- "@xyo-network/diviner-wrapper": "~5.3.22",
48
- "@xyo-network/module-model": "~5.3.22",
49
- "@xyo-network/payload-builder": "~5.3.22",
50
- "@xyo-network/payload-model": "~5.3.22"
39
+ "@xyo-network/boundwitness-model": "~5.3.24",
40
+ "@xyo-network/archivist-wrapper": "~5.3.24",
41
+ "@xyo-network/boundwitness-builder": "~5.3.24",
42
+ "@xyo-network/diviner-abstract": "~5.3.24",
43
+ "@xyo-network/diviner-boundwitness-model": "~5.3.24",
44
+ "@xyo-network/diviner-model": "~5.3.24",
45
+ "@xyo-network/module-model": "~5.3.24",
46
+ "@xyo-network/diviner-wrapper": "~5.3.24",
47
+ "@xyo-network/diviner-indexing-model": "~5.3.24",
48
+ "@xyo-network/payload-model": "~5.3.24",
49
+ "@xyo-network/payload-builder": "~5.3.24"
51
50
  },
52
51
  "devDependencies": {
53
52
  "@opentelemetry/api": "^1.9.1",
54
53
  "@types/node": "^25.5.0",
55
- "@xylabs/sdk-js": "^5.0.91",
56
- "@xylabs/ts-scripts-common": "~7.6.8",
57
- "@xylabs/ts-scripts-yarn3": "~7.6.8",
58
- "@xylabs/tsconfig": "~7.6.8",
59
- "@xyo-network/archivist-wrapper": "~5.3.22",
60
- "@xyo-network/boundwitness-builder": "~5.3.22",
61
- "@xyo-network/boundwitness-model": "~5.3.22",
62
- "@xyo-network/diviner-abstract": "~5.3.22",
63
- "@xyo-network/diviner-boundwitness-model": "~5.3.22",
64
- "@xyo-network/diviner-indexing-model": "~5.3.22",
65
- "@xyo-network/diviner-model": "~5.3.22",
66
- "@xyo-network/diviner-wrapper": "~5.3.22",
67
- "@xyo-network/module-model": "~5.3.22",
68
- "@xyo-network/payload-builder": "~5.3.22",
69
- "@xyo-network/payload-model": "~5.3.22",
54
+ "@xylabs/sdk-js": "^5.0.93",
55
+ "@xylabs/ts-scripts-common": "~7.6.16",
56
+ "@xylabs/ts-scripts-pnpm": "~7.6.16",
57
+ "@xylabs/tsconfig": "~7.6.16",
70
58
  "acorn": "^8.16.0",
71
59
  "axios": "^1.14.0",
72
- "cosmiconfig": "^9.0.1",
73
- "esbuild": "^0.27.4",
74
- "eslint": "^10.1.0",
60
+ "esbuild": "^0.28.0",
75
61
  "ethers": "^6.16.0",
76
- "rollup": "^4.60.1",
77
62
  "tslib": "^2.8.1",
78
63
  "typescript": "~5.9.3",
79
- "zod": "^4.3.6"
64
+ "zod": "^4.3.6",
65
+ "@xyo-network/archivist-wrapper": "~5.3.24",
66
+ "@xyo-network/boundwitness-builder": "~5.3.24",
67
+ "@xyo-network/diviner-abstract": "~5.3.24",
68
+ "@xyo-network/diviner-boundwitness-model": "~5.3.24",
69
+ "@xyo-network/boundwitness-model": "~5.3.24",
70
+ "@xyo-network/diviner-indexing-model": "~5.3.24",
71
+ "@xyo-network/diviner-wrapper": "~5.3.24",
72
+ "@xyo-network/diviner-model": "~5.3.24",
73
+ "@xyo-network/module-model": "~5.3.24",
74
+ "@xyo-network/payload-builder": "~5.3.24",
75
+ "@xyo-network/payload-model": "~5.3.24"
80
76
  },
81
77
  "peerDependencies": {
82
78
  "@xylabs/sdk-js": "^5",
@@ -87,4 +83,4 @@
87
83
  "publishConfig": {
88
84
  "access": "public"
89
85
  }
90
- }
86
+ }
package/src/Diviner.ts DELETED
@@ -1,247 +0,0 @@
1
- import {
2
- assertEx, clearTimeoutEx, Hash, isString, setTimeoutEx,
3
- } from '@xylabs/sdk-js'
4
- import { ArchivistWrapper } from '@xyo-network/archivist-wrapper'
5
- import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
6
- import { isBoundWitness } from '@xyo-network/boundwitness-model'
7
- import { AbstractDiviner } from '@xyo-network/diviner-abstract'
8
- import { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'
9
- import {
10
- IndexingDivinerConfig,
11
- IndexingDivinerConfigSchema,
12
- IndexingDivinerParams,
13
- IndexingDivinerStage,
14
- IndexingDivinerState,
15
- } from '@xyo-network/diviner-indexing-model'
16
- import {
17
- asDivinerInstance, DivinerInstance, DivinerModuleEventData,
18
- } from '@xyo-network/diviner-model'
19
- import { DivinerWrapper } from '@xyo-network/diviner-wrapper'
20
- import {
21
- creatableModule, isModuleState, ModuleState, ModuleStateSchema,
22
- } from '@xyo-network/module-model'
23
- import { PayloadBuilder } from '@xyo-network/payload-builder'
24
- import {
25
- Payload, Schema, SequenceConstants, WithStorageMeta,
26
- } from '@xyo-network/payload-model'
27
-
28
- export type ConfigStoreKey = 'indexStore' | 'stateStore'
29
-
30
- export type ConfigStore = Extract<keyof IndexingDivinerConfig, ConfigStoreKey>
31
-
32
- const moduleName = 'IndexingDiviner'
33
-
34
- @creatableModule<IndexingDiviner>()
35
- export class IndexingDiviner<
36
- TParams extends IndexingDivinerParams = IndexingDivinerParams,
37
- TIn extends Payload = Payload,
38
- TOut extends Payload = Payload,
39
- TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<
40
- DivinerInstance<TParams, TIn, TOut>,
41
- TIn,
42
- TOut
43
- >,
44
- > extends AbstractDiviner<TParams, TIn, TOut, TEventData> {
45
- static override readonly allowRandomAccount = true
46
- static override readonly configSchemas: Schema[] = [...super.configSchemas, IndexingDivinerConfigSchema]
47
- static override readonly defaultConfigSchema: Schema = IndexingDivinerConfigSchema
48
-
49
- private _lastState?: ModuleState<IndexingDivinerState>
50
- private _pollId?: string
51
-
52
- get payloadDivinerLimit() {
53
- return this.config.payloadDivinerLimit ?? 1000
54
- }
55
-
56
- get pollFrequency() {
57
- return this.config.pollFrequency ?? 10_000
58
- }
59
-
60
- /**
61
- * Works via batched iteration of the source archivist to populate the index.
62
- * @returns A promise that resolves when the background process is complete
63
- */
64
- protected backgroundDivine = async (): Promise<void> => {
65
- // Load last state
66
- const lastState = await this.retrieveState()
67
- // Get next batch of results
68
- const indexCandidateDiviner = await this.getIndexingDivinerStage('stateToIndexCandidateDiviner')
69
- const results = lastState ? await indexCandidateDiviner.divine([lastState]) : await indexCandidateDiviner.divine()
70
- // Filter next state out from results
71
- const nextState = results.find(isModuleState<IndexingDivinerState>)
72
- const indexCandidates = results.filter(x => !isModuleState(x))
73
- // Transform candidates to indexes
74
- const toIndexTransformDiviner = await this.getIndexingDivinerStage('indexCandidateToIndexDiviner')
75
- const indexes = await toIndexTransformDiviner.divine(indexCandidates)
76
- // Insert index results
77
- const indexArchivist = await this.getArchivistForStore('indexStore')
78
- await indexArchivist.insert(indexes)
79
- // Update state
80
- if (nextState) {
81
- await this.commitState(nextState)
82
- }
83
- }
84
-
85
- /**
86
- * Commit the internal state of the Diviner process. This is similar
87
- * to a transaction completion in a database and should only be called
88
- * when results have been successfully persisted to the appropriate
89
- * external stores.
90
- * @param nextState The state to commit
91
- */
92
- protected async commitState(nextState: ModuleState<IndexingDivinerState>) {
93
- // Don't commit state if no state has changed
94
- if (nextState.state.cursor === this._lastState?.state.cursor) return
95
- this._lastState = nextState
96
- const archivist = await this.getArchivistForStore('stateStore')
97
- const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()
98
- await archivist.insert([bw, nextState])
99
- }
100
-
101
- protected override async divineHandler(payloads: TIn[] = []): Promise<TOut[]> {
102
- const indexPayloadDiviner = await this.getPayloadDivinerForStore('indexStore')
103
- const divinerQueryToIndexQueryDiviner = await this.getIndexingDivinerStage('divinerQueryToIndexQueryDiviner')
104
- const indexQueryResponseToDivinerQueryResponseDiviner = await this.getIndexingDivinerStage('indexQueryResponseToDivinerQueryResponseDiviner')
105
- const results = (
106
- await Promise.all(
107
- payloads.map(async (payload) => {
108
- const indexQuery = await divinerQueryToIndexQueryDiviner.divine([payload])
109
- // Divine the results
110
- const indexedResults = await indexPayloadDiviner.divine(indexQuery)
111
- // Transform the results to the response shape
112
- const response = await Promise.all(
113
- indexedResults.flat().map(indexedResult => indexQueryResponseToDivinerQueryResponseDiviner.divine([payload, indexedResult])),
114
- )
115
- return response.flat()
116
- }),
117
- )
118
- ).flat()
119
- // TODO: Infer this type over casting to this type
120
- return results as TOut[]
121
- }
122
-
123
- /**
124
- * Retrieves the archivist for the specified store
125
- * @param store The store to retrieve the archivist for
126
- * @returns The archivist for the specified store
127
- */
128
- protected async getArchivistForStore(store: ConfigStore): Promise<ArchivistWrapper> {
129
- const name = assertEx(this.config?.[store]?.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`)
130
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist [${name}]`)
131
- return ArchivistWrapper.wrap(mod, this.account)
132
- }
133
-
134
- /**
135
- * Retrieves the BoundWitness Diviner for the specified store
136
- * @param store The store to retrieve the BoundWitness Diviner for
137
- * @returns The BoundWitness Diviner for the specified store
138
- */
139
- protected async getBoundWitnessDivinerForStore(store: ConfigStore) {
140
- const name = assertEx(this.config?.[store]?.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`)
141
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner [${name}]`)
142
- return DivinerWrapper.wrap(mod, this.account)
143
- }
144
-
145
- /**
146
- * Gets the Diviner for the supplied Indexing Diviner stage
147
- * @param transform The Indexing Diviner stage
148
- * @returns The diviner corresponding to the Indexing Diviner stage
149
- */
150
- protected async getIndexingDivinerStage(transform: IndexingDivinerStage) {
151
- const nameOrAddress = assertEx(
152
- this.config?.indexingDivinerStages?.[transform],
153
- () => `${moduleName}: Config for indexingDivinerStages.${transform} not specified`,
154
- )
155
- const mod = await this.resolve(nameOrAddress)
156
- return assertEx(asDivinerInstance(mod), () => `${moduleName}: Failed to resolve indexing diviner stage for ${transform}`) as DivinerInstance
157
- }
158
-
159
- /**
160
- * Retrieves the Payload Diviner for the specified store
161
- * @param store The store to retrieve the Payload Diviner for
162
- * @returns The Payload Diviner for the specified store
163
- */
164
- protected async getPayloadDivinerForStore(store: ConfigStore) {
165
- const name = assertEx(this.config?.[store]?.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`)
166
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner [${name}]`)
167
- return DivinerWrapper.wrap(mod, this.account)
168
- }
169
-
170
- /**
171
- * Retrieves the last state of the Diviner process. Used to recover state after
172
- * preemptions, reboots, etc.
173
- */
174
- protected async retrieveState(): Promise<ModuleState<IndexingDivinerState> | undefined> {
175
- const accountAddress = this.account.address
176
- if (this._lastState) return this._lastState
177
- let hash = '' as Hash
178
- const diviner = await this.getBoundWitnessDivinerForStore('stateStore')
179
- const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })
180
- .fields({
181
- address: accountAddress,
182
- limit: 1,
183
- cursor: SequenceConstants.minLocalSequence,
184
- order: 'desc',
185
- payload_schemas: [ModuleStateSchema],
186
- })
187
- .build()
188
- const boundWitnesses = await diviner.divine([query])
189
- if (boundWitnesses.length > 0) {
190
- const boundWitness = boundWitnesses[0]
191
- if (isBoundWitness(boundWitness)) {
192
- // Find the index for this address in the BoundWitness that is a ModuleState
193
- hash = boundWitness.addresses
194
- .map((address, index) => ({ address, index }))
195
- .filter(({ address }) => address === accountAddress)
196
- // eslint-disable-next-line unicorn/no-array-reduce
197
- .reduce(
198
- (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),
199
- '' as Hash,
200
- )
201
- }
202
- }
203
-
204
- // If we able to located the last state
205
- if (hash !== '') {
206
- // Get last state
207
- const archivist = await this.getArchivistForStore('stateStore')
208
- const payload = (await archivist.get([hash])).find(isModuleState<IndexingDivinerState>)
209
- if (payload) {
210
- return payload as WithStorageMeta<ModuleState<IndexingDivinerState>>
211
- }
212
- }
213
- return undefined
214
- }
215
-
216
- protected override async startHandler() {
217
- await super.startHandler()
218
- this.poll()
219
- }
220
-
221
- protected override async stopHandler(_timeout?: number | undefined) {
222
- if (isString(this._pollId)) {
223
- clearTimeout(this._pollId)
224
- this._pollId = undefined
225
- }
226
- await super.stopHandler()
227
- }
228
-
229
- /**
230
- * Runs the background divine process on a loop with a delay
231
- * specified by the `config.pollFrequency`
232
- */
233
- private poll() {
234
- this._pollId = setTimeoutEx(async () => {
235
- try {
236
- await Promise.resolve()
237
- await this.backgroundDivine()
238
- } catch (e) {
239
- console.log(e)
240
- } finally {
241
- if (this._pollId) clearTimeoutEx(this._pollId)
242
- this._pollId = undefined
243
- this.poll()
244
- }
245
- }, this.pollFrequency)
246
- }
247
- }
package/src/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './Diviner.ts'