@xyo-network/diviner-indexing 2.79.5 → 2.79.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/dist/browser/index.cjs +3 -200
  2. package/dist/browser/index.cjs.map +1 -1
  3. package/dist/browser/index.d.cts +2 -7
  4. package/dist/browser/index.d.cts.map +1 -1
  5. package/dist/browser/index.d.mts +2 -7
  6. package/dist/browser/index.d.mts.map +1 -1
  7. package/dist/browser/index.d.ts +2 -7
  8. package/dist/browser/index.d.ts.map +1 -1
  9. package/dist/browser/index.js +3 -195
  10. package/dist/browser/index.js.map +1 -1
  11. package/dist/node/index.d.cts +2 -7
  12. package/dist/node/index.d.cts.map +1 -1
  13. package/dist/node/index.d.mts +2 -7
  14. package/dist/node/index.d.mts.map +1 -1
  15. package/dist/node/index.d.ts +2 -7
  16. package/dist/node/index.d.ts.map +1 -1
  17. package/dist/node/index.js +5 -212
  18. package/dist/node/index.js.map +1 -1
  19. package/dist/node/index.mjs +3 -203
  20. package/dist/node/index.mjs.map +1 -1
  21. package/package.json +3 -12
  22. package/src/index.ts +2 -7
  23. package/dist/browser/Config.d.cts +0 -32
  24. package/dist/browser/Config.d.cts.map +0 -1
  25. package/dist/browser/Config.d.mts +0 -32
  26. package/dist/browser/Config.d.mts.map +0 -1
  27. package/dist/browser/Config.d.ts +0 -32
  28. package/dist/browser/Config.d.ts.map +0 -1
  29. package/dist/browser/Diviner.d.cts +0 -71
  30. package/dist/browser/Diviner.d.cts.map +0 -1
  31. package/dist/browser/Diviner.d.mts +0 -71
  32. package/dist/browser/Diviner.d.mts.map +0 -1
  33. package/dist/browser/Diviner.d.ts +0 -71
  34. package/dist/browser/Diviner.d.ts.map +0 -1
  35. package/dist/browser/Labels.d.cts +0 -9
  36. package/dist/browser/Labels.d.cts.map +0 -1
  37. package/dist/browser/Labels.d.mts +0 -9
  38. package/dist/browser/Labels.d.mts.map +0 -1
  39. package/dist/browser/Labels.d.ts +0 -9
  40. package/dist/browser/Labels.d.ts.map +0 -1
  41. package/dist/browser/Params.d.cts +0 -5
  42. package/dist/browser/Params.d.cts.map +0 -1
  43. package/dist/browser/Params.d.mts +0 -5
  44. package/dist/browser/Params.d.mts.map +0 -1
  45. package/dist/browser/Params.d.ts +0 -5
  46. package/dist/browser/Params.d.ts.map +0 -1
  47. package/dist/browser/Schema.d.cts +0 -3
  48. package/dist/browser/Schema.d.cts.map +0 -1
  49. package/dist/browser/Schema.d.mts +0 -3
  50. package/dist/browser/Schema.d.mts.map +0 -1
  51. package/dist/browser/Schema.d.ts +0 -3
  52. package/dist/browser/Schema.d.ts.map +0 -1
  53. package/dist/browser/SearchableStorage.d.cts +0 -10
  54. package/dist/browser/SearchableStorage.d.cts.map +0 -1
  55. package/dist/browser/SearchableStorage.d.mts +0 -10
  56. package/dist/browser/SearchableStorage.d.mts.map +0 -1
  57. package/dist/browser/SearchableStorage.d.ts +0 -10
  58. package/dist/browser/SearchableStorage.d.ts.map +0 -1
  59. package/dist/browser/Stage.d.cts +0 -29
  60. package/dist/browser/Stage.d.cts.map +0 -1
  61. package/dist/browser/Stage.d.mts +0 -29
  62. package/dist/browser/Stage.d.mts.map +0 -1
  63. package/dist/browser/Stage.d.ts +0 -29
  64. package/dist/browser/Stage.d.ts.map +0 -1
  65. package/dist/browser/State.d.cts +0 -5
  66. package/dist/browser/State.d.cts.map +0 -1
  67. package/dist/browser/State.d.mts +0 -5
  68. package/dist/browser/State.d.mts.map +0 -1
  69. package/dist/browser/State.d.ts +0 -5
  70. package/dist/browser/State.d.ts.map +0 -1
  71. package/dist/node/Config.d.cts +0 -32
  72. package/dist/node/Config.d.cts.map +0 -1
  73. package/dist/node/Config.d.mts +0 -32
  74. package/dist/node/Config.d.mts.map +0 -1
  75. package/dist/node/Config.d.ts +0 -32
  76. package/dist/node/Config.d.ts.map +0 -1
  77. package/dist/node/Diviner.d.cts +0 -71
  78. package/dist/node/Diviner.d.cts.map +0 -1
  79. package/dist/node/Diviner.d.mts +0 -71
  80. package/dist/node/Diviner.d.mts.map +0 -1
  81. package/dist/node/Diviner.d.ts +0 -71
  82. package/dist/node/Diviner.d.ts.map +0 -1
  83. package/dist/node/Labels.d.cts +0 -9
  84. package/dist/node/Labels.d.cts.map +0 -1
  85. package/dist/node/Labels.d.mts +0 -9
  86. package/dist/node/Labels.d.mts.map +0 -1
  87. package/dist/node/Labels.d.ts +0 -9
  88. package/dist/node/Labels.d.ts.map +0 -1
  89. package/dist/node/Params.d.cts +0 -5
  90. package/dist/node/Params.d.cts.map +0 -1
  91. package/dist/node/Params.d.mts +0 -5
  92. package/dist/node/Params.d.mts.map +0 -1
  93. package/dist/node/Params.d.ts +0 -5
  94. package/dist/node/Params.d.ts.map +0 -1
  95. package/dist/node/Schema.d.cts +0 -3
  96. package/dist/node/Schema.d.cts.map +0 -1
  97. package/dist/node/Schema.d.mts +0 -3
  98. package/dist/node/Schema.d.mts.map +0 -1
  99. package/dist/node/Schema.d.ts +0 -3
  100. package/dist/node/Schema.d.ts.map +0 -1
  101. package/dist/node/SearchableStorage.d.cts +0 -10
  102. package/dist/node/SearchableStorage.d.cts.map +0 -1
  103. package/dist/node/SearchableStorage.d.mts +0 -10
  104. package/dist/node/SearchableStorage.d.mts.map +0 -1
  105. package/dist/node/SearchableStorage.d.ts +0 -10
  106. package/dist/node/SearchableStorage.d.ts.map +0 -1
  107. package/dist/node/Stage.d.cts +0 -29
  108. package/dist/node/Stage.d.cts.map +0 -1
  109. package/dist/node/Stage.d.mts +0 -29
  110. package/dist/node/Stage.d.mts.map +0 -1
  111. package/dist/node/Stage.d.ts +0 -29
  112. package/dist/node/Stage.d.ts.map +0 -1
  113. package/dist/node/State.d.cts +0 -5
  114. package/dist/node/State.d.cts.map +0 -1
  115. package/dist/node/State.d.mts +0 -5
  116. package/dist/node/State.d.mts.map +0 -1
  117. package/dist/node/State.d.ts +0 -5
  118. package/dist/node/State.d.ts.map +0 -1
  119. package/src/Config.ts +0 -35
  120. package/src/Diviner.ts +0 -228
  121. package/src/Labels.ts +0 -13
  122. package/src/Params.ts +0 -6
  123. package/src/Schema.ts +0 -2
  124. package/src/SearchableStorage.ts +0 -9
  125. package/src/Stage.ts +0 -30
  126. package/src/State.ts +0 -5
package/src/Diviner.ts DELETED
@@ -1,228 +0,0 @@
1
- import { assertEx } from '@xylabs/assert'
2
- import { AbstractDiviner } from '@xyo-network/abstract-diviner'
3
- import { ArchivistWrapper } from '@xyo-network/archivist-wrapper'
4
- import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
5
- import { isBoundWitness } from '@xyo-network/boundwitness-model'
6
- import { BoundWitnessDivinerQueryPayload, BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'
7
- import { asDivinerInstance, DivinerConfigSchema, DivinerModule, DivinerModuleEventData } from '@xyo-network/diviner-model'
8
- import { DivinerWrapper } from '@xyo-network/diviner-wrapper'
9
- import { isModuleState, ModuleState, ModuleStateSchema } from '@xyo-network/module-model'
10
- import { PayloadBuilder } from '@xyo-network/payload-builder'
11
- import { Payload } from '@xyo-network/payload-model'
12
-
13
- import { IndexingDivinerConfig, IndexingDivinerConfigSchema } from './Config'
14
- import { IndexingDivinerParams } from './Params'
15
- import { IndexingDivinerStage } from './Stage'
16
- import { IndexingDivinerState } from './State'
17
-
18
- type ConfigStoreKey = 'indexStore' | 'stateStore'
19
-
20
- type ConfigStore = Extract<keyof IndexingDivinerConfig, ConfigStoreKey>
21
-
22
- const moduleName = 'IndexingDiviner'
23
-
24
- export class IndexingDiviner<
25
- TParams extends IndexingDivinerParams = IndexingDivinerParams,
26
- TIn extends Payload = Payload,
27
- TOut extends Payload = Payload,
28
- TEventData extends DivinerModuleEventData<DivinerModule<TParams>, TIn, TOut> = DivinerModuleEventData<DivinerModule<TParams>, TIn, TOut>,
29
- > extends AbstractDiviner<TParams, TIn, TOut, TEventData> {
30
- static override readonly configSchemas = [IndexingDivinerConfigSchema, DivinerConfigSchema]
31
-
32
- private _lastState?: ModuleState<IndexingDivinerState>
33
- private _pollId?: string | number | NodeJS.Timeout
34
-
35
- get payloadDivinerLimit() {
36
- return this.config.payloadDivinerLimit ?? 1_000
37
- }
38
-
39
- get pollFrequency() {
40
- return this.config.pollFrequency ?? 10_000
41
- }
42
-
43
- /**
44
- * Works via batched iteration of the source archivist to populate the index.
45
- * @returns A promise that resolves when the background process is complete
46
- */
47
- protected backgroundDivine = async (): Promise<void> => {
48
- // Load last state
49
- const lastState = await this.retrieveState()
50
- // Get next batch of results
51
- const indexCandidateDiviner = await this.getIndexingDivinerStage('stateToIndexCandidateDiviner')
52
- const results = lastState ? await indexCandidateDiviner.divine([lastState]) : await indexCandidateDiviner.divine()
53
- // Filter next state out from results
54
- const nextState = results.find(isModuleState<IndexingDivinerState>)
55
- const indexCandidates = results.filter((x) => !isModuleState(x))
56
- // Transform candidates to indexes
57
- const toIndexTransformDiviner = await this.getIndexingDivinerStage('indexCandidateToIndexDiviner')
58
- const indexes = await toIndexTransformDiviner.divine(indexCandidates)
59
- // Insert index results
60
- const indexArchivist = await this.getArchivistForStore('indexStore')
61
- await indexArchivist.insert(indexes)
62
- // Update state
63
- if (nextState) {
64
- await this.commitState(nextState)
65
- }
66
- }
67
-
68
- /**
69
- * Commit the internal state of the Diviner process. This is similar
70
- * to a transaction completion in a database and should only be called
71
- * when results have been successfully persisted to the appropriate
72
- * external stores.
73
- * @param nextState The state to commit
74
- */
75
- protected async commitState(nextState: ModuleState<IndexingDivinerState>) {
76
- // Don't commit state if no state has changed
77
- if (nextState.state.offset === this._lastState?.state.offset) return
78
- this._lastState = nextState
79
- const archivist = await this.getArchivistForStore('stateStore')
80
- const [bw] = await new BoundWitnessBuilder().payload(nextState).witness(this.account).build()
81
- await archivist.insert([bw, nextState])
82
- }
83
-
84
- protected override async divineHandler(payloads: TIn[] = []): Promise<TOut[]> {
85
- const indexPayloadDiviner = await this.getPayloadDivinerForStore('indexStore')
86
- const divinerQueryToIndexQueryDiviner = await this.getIndexingDivinerStage('divinerQueryToIndexQueryDiviner')
87
- const indexQueryResponseToDivinerQueryResponseDiviner = await this.getIndexingDivinerStage('indexQueryResponseToDivinerQueryResponseDiviner')
88
- const results = (
89
- await Promise.all(
90
- payloads.map(async (payload) => {
91
- const indexQuery = await divinerQueryToIndexQueryDiviner.divine([payload])
92
- // Divine the results
93
- const indexedResults = await indexPayloadDiviner.divine(indexQuery)
94
- // Transform the results to the response shape
95
- const response = await Promise.all(
96
- indexedResults.flat().map((indexedResult) => indexQueryResponseToDivinerQueryResponseDiviner.divine([payload, indexedResult])),
97
- )
98
- return response.flat()
99
- }),
100
- )
101
- ).flat()
102
- // TODO: Infer this type over casting to this type
103
- return results as TOut[]
104
- }
105
-
106
- /**
107
- * Retrieves the archivist for the specified store
108
- * @param store The store to retrieve the archivist for
109
- * @returns The archivist for the specified store
110
- */
111
- protected async getArchivistForStore(store: ConfigStore) {
112
- const name = assertEx(this.config?.[store]?.archivist, () => `${moduleName}: Config for ${store}.archivist not specified`)
113
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.archivist`)
114
- return ArchivistWrapper.wrap(mod, this.account)
115
- }
116
-
117
- /**
118
- * Retrieves the BoundWitness Diviner for the specified store
119
- * @param store The store to retrieve the BoundWitness Diviner for
120
- * @returns The BoundWitness Diviner for the specified store
121
- */
122
- protected async getBoundWitnessDivinerForStore(store: ConfigStore) {
123
- const name = assertEx(this.config?.[store]?.boundWitnessDiviner, () => `${moduleName}: Config for ${store}.boundWitnessDiviner not specified`)
124
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.boundWitnessDiviner`)
125
- return DivinerWrapper.wrap(mod, this.account)
126
- }
127
-
128
- /**
129
- * Gets the Diviner for the supplied Indexing Diviner stage
130
- * @param transform The Indexing Diviner stage
131
- * @returns The diviner corresponding to the Indexing Diviner stage
132
- */
133
- protected async getIndexingDivinerStage(transform: IndexingDivinerStage) {
134
- const nameOrAddress = assertEx(
135
- this.config?.indexingDivinerStages?.[transform],
136
- () => `${moduleName}: Config for indexingDivinerStages.${transform} not specified`,
137
- )
138
- const mod = await this.resolve(nameOrAddress)
139
- return assertEx(asDivinerInstance(mod), () => `${moduleName}: Failed to resolve indexing diviner stage for ${transform}`)
140
- }
141
-
142
- /**
143
- * Retrieves the Payload Diviner for the specified store
144
- * @param store The store to retrieve the Payload Diviner for
145
- * @returns The Payload Diviner for the specified store
146
- */
147
- protected async getPayloadDivinerForStore(store: ConfigStore) {
148
- const name = assertEx(this.config?.[store]?.payloadDiviner, () => `${moduleName}: Config for ${store}.payloadDiviner not specified`)
149
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve ${store}.payloadDiviner`)
150
- return DivinerWrapper.wrap(mod, this.account)
151
- }
152
-
153
- /**
154
- * Retrieves the last state of the Diviner process. Used to recover state after
155
- * preemptions, reboots, etc.
156
- */
157
- protected async retrieveState(): Promise<ModuleState<IndexingDivinerState> | undefined> {
158
- if (this._lastState) return this._lastState
159
- let hash: string = ''
160
- const diviner = await this.getBoundWitnessDivinerForStore('stateStore')
161
- const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })
162
- .fields({
163
- address: this.account.address,
164
- limit: 1,
165
- offset: 0,
166
- order: 'desc',
167
- payload_schemas: [ModuleStateSchema],
168
- })
169
- .build()
170
- const boundWitnesses = await diviner.divine([query])
171
- if (boundWitnesses.length > 0) {
172
- const boundWitness = boundWitnesses[0]
173
- if (isBoundWitness(boundWitness)) {
174
- // Find the index for this address in the BoundWitness that is a ModuleState
175
- hash = boundWitness.addresses
176
- .map((address, index) => ({ address, index }))
177
- .filter(({ address }) => address === this.account.address)
178
- .reduce(
179
- (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),
180
- '',
181
- )
182
- }
183
- }
184
-
185
- // If we able to located the last state
186
- if (hash) {
187
- // Get last state
188
- const archivist = await this.getArchivistForStore('stateStore')
189
- const payload = (await archivist.get([hash])).find(isModuleState<IndexingDivinerState>)
190
- if (payload) {
191
- return payload
192
- }
193
- }
194
- return undefined
195
- }
196
-
197
- protected override async startHandler(): Promise<boolean> {
198
- await super.startHandler()
199
- this.poll()
200
- return true
201
- }
202
-
203
- protected override async stopHandler(_timeout?: number | undefined): Promise<boolean> {
204
- if (this._pollId) {
205
- clearTimeout(this._pollId)
206
- this._pollId = undefined
207
- }
208
- return await super.stopHandler()
209
- }
210
-
211
- /**
212
- * Runs the background divine process on a loop with a delay
213
- * specified by the `config.pollFrequency`
214
- */
215
- private poll() {
216
- this._pollId = setTimeout(async () => {
217
- try {
218
- await this.backgroundDivine()
219
- } catch (e) {
220
- console.log(e)
221
- } finally {
222
- if (this._pollId) clearTimeout(this._pollId)
223
- this._pollId = undefined
224
- this.poll()
225
- }
226
- }, this.pollFrequency)
227
- }
228
- }
package/src/Labels.ts DELETED
@@ -1,13 +0,0 @@
1
- import { Labels } from '@xyo-network/module-model'
2
-
3
- import { DivinerStageSchema, IndexingDivinerStage } from './Stage'
4
-
5
- /**
6
- * Labels for Indexing Diviner Stage Diviners
7
- */
8
- export type IndexingDivinerStageLabels = Labels & {
9
- /**
10
- * Labels for the stage of the Indexing Diviner Stage Diviner
11
- */
12
- [key in DivinerStageSchema]: IndexingDivinerStage
13
- }
package/src/Params.ts DELETED
@@ -1,6 +0,0 @@
1
- import { DivinerParams } from '@xyo-network/diviner-model'
2
- import { AnyConfigSchema } from '@xyo-network/module-model'
3
-
4
- import { IndexingDivinerConfig } from './Config'
5
-
6
- export type IndexingDivinerParams = DivinerParams<AnyConfigSchema<IndexingDivinerConfig>>
package/src/Schema.ts DELETED
@@ -1,2 +0,0 @@
1
- export const IndexingDivinerSchema = 'network.xyo.diviner.indexing' as const
2
- export type ImageThumbnailDivinerSchema = typeof IndexingDivinerSchema
@@ -1,9 +0,0 @@
1
- /**
2
- * Describes an Archivist/Diviner combination
3
- * that enables searching signed payloads
4
- */
5
- export interface SearchableStorage {
6
- archivist: string
7
- boundWitnessDiviner: string
8
- payloadDiviner: string
9
- }
package/src/Stage.ts DELETED
@@ -1,30 +0,0 @@
1
- export type DivinerStageSchema = 'network.xyo.diviner.stage'
2
- export const DivinerStageSchema: DivinerStageSchema = 'network.xyo.diviner.stage'
3
-
4
- /**
5
- * The diviners for each stage of an indexing diviner
6
- */
7
- export type IndexingDivinerStage =
8
- /**
9
- * Transforms a diviner query into an index query
10
- */
11
- | 'divinerQueryToIndexQueryDiviner'
12
- /**
13
- * Transforms the index candidates into indexes
14
- */
15
- | 'indexCandidateToIndexDiviner'
16
- /**
17
- * Transforms an index query response into a diviner divine query response
18
- */
19
- | 'indexQueryResponseToDivinerQueryResponseDiviner'
20
- /**
21
- * Uses the current state to determine the next batch of index candidates
22
- */
23
- | 'stateToIndexCandidateDiviner'
24
-
25
- /**
26
- * Config section for declaring each indexing diviner stage
27
- */
28
- export type IndexingDivinerStageConfig = {
29
- [key in IndexingDivinerStage]: string
30
- }
package/src/State.ts DELETED
@@ -1,5 +0,0 @@
1
- import { StateDictionary } from '@xyo-network/module-model'
2
-
3
- export type IndexingDivinerState = StateDictionary & {
4
- offset: number
5
- }