@jini-ai/search 0.7.2 → 0.7.3-next.1
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/README.md +1 -1
- package/dist/contribution.d.ts +51 -11
- package/dist/contribution.js +13 -9
- package/dist/http-query.d.ts +34 -0
- package/dist/http-query.js +75 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/indexer-backfill.d.ts +80 -0
- package/dist/indexer-backfill.js +123 -0
- package/dist/indexer-reconcile.d.ts +78 -0
- package/dist/indexer-reconcile.js +141 -0
- package/dist/indexer-write.d.ts +86 -0
- package/dist/indexer-write.js +174 -0
- package/dist/indexer.d.ts +2 -4
- package/dist/indexer.js +59 -374
- package/dist/ports.d.ts +2 -1
- package/dist/service.js +19 -16
- package/dist/testkit/conformance.js +31 -27
- package/dist/testkit/fakes.js +7 -1
- package/dist/testkit/memory-provider.js +111 -95
- package/dist/tools.js +36 -30
- package/package.json +9 -14
|
@@ -108,85 +108,87 @@ function resolveOptions(optional) {
|
|
|
108
108
|
const { id = 'memory', label = 'In-memory reference provider', acceptsRestricted = true, asyncIndexing = false, typoTolerance = false, locales = 'any', maxBatch = 100, available = () => true, } = optional;
|
|
109
109
|
return { id, label, acceptsRestricted, asyncIndexing, typoTolerance, locales, maxBatch, available };
|
|
110
110
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const restrictedRefusal = (entry) => !acceptsRestricted && entry.document?.visibility.kind === 'restricted' ? 'this provider accepts public documents only' : undefined;
|
|
136
|
-
function outcomeOf(index, entry, pending) {
|
|
137
|
-
const base = { key: entry.key, revision: entry.revision };
|
|
138
|
-
const refusal = isRevision(entry.revision) ? restrictedRefusal(entry) : 'revision must be a non-negative safe integer';
|
|
139
|
-
if (refusal !== undefined)
|
|
140
|
-
return { ...base, status: 'rejected', message: refusal };
|
|
141
|
-
const current = index.get(encodeSearchKey({ key: entry.key }));
|
|
142
|
-
if (current !== undefined && current.revision >= entry.revision)
|
|
143
|
-
return { ...base, status: 'stale' };
|
|
144
|
-
if (!asyncIndexing) {
|
|
145
|
-
store(index, entry);
|
|
146
|
-
return { ...base, status: 'applied' };
|
|
147
|
-
}
|
|
148
|
-
pending.push(entry);
|
|
149
|
-
return { ...base, status: 'pending' };
|
|
150
|
-
}
|
|
151
|
-
function write(ctx, entries) {
|
|
152
|
-
if (!available({ ctx }))
|
|
153
|
-
return unavailable();
|
|
154
|
-
if (entries.length > maxBatch)
|
|
155
|
-
return providerRejected({ code: 'invalid_request', message: `at most ${maxBatch} items per write` });
|
|
156
|
-
const index = indexes.get(ctx.indexName);
|
|
157
|
-
if (index === undefined)
|
|
158
|
-
return missing(ctx.indexName);
|
|
159
|
-
const pending = [];
|
|
160
|
-
const outcomes = entries.map(entry => outcomeOf(index, entry, pending));
|
|
161
|
-
if (pending.length === 0)
|
|
162
|
-
return providerOk({ value: { outcomes } });
|
|
163
|
-
taskCounter += 1;
|
|
164
|
-
const taskId = `task_${taskCounter}`;
|
|
165
|
-
queued.set(taskId, { indexName: ctx.indexName, entries: pending });
|
|
166
|
-
return providerOk({ value: { outcomes, taskId } });
|
|
167
|
-
}
|
|
168
|
-
/** Settle every queued task up to and including `taskId`, in write order. */
|
|
169
|
-
function settleThrough(taskId) {
|
|
170
|
-
const ids = [...queued.keys()];
|
|
171
|
-
// awaitTask calls this only when `taskId` is queued, so indexOf is never -1.
|
|
172
|
-
for (const queuedId of ids.slice(0, ids.indexOf(taskId) + 1)) {
|
|
173
|
-
const task = queued.get(queuedId);
|
|
174
|
-
const index = indexes.get(task.indexName);
|
|
175
|
-
// A task whose index was dropped meanwhile has nothing left to write into.
|
|
176
|
-
if (index !== undefined)
|
|
177
|
-
for (const entry of task.entries)
|
|
178
|
-
store(index, entry);
|
|
179
|
-
queued.delete(queuedId);
|
|
180
|
-
settled.add(queuedId);
|
|
181
|
-
}
|
|
111
|
+
const unavailable = () => providerUnknown({ code: 'provider_unavailable', message: 'the in-memory provider is unavailable for this context' });
|
|
112
|
+
const missing = (indexName) => providerRejected({ code: 'not_found', message: `index '${indexName}' does not exist` });
|
|
113
|
+
function storeEntry(index, entry) {
|
|
114
|
+
const encoded = encodeSearchKey({ key: entry.key });
|
|
115
|
+
const current = index.get(encoded);
|
|
116
|
+
if (current !== undefined && current.revision >= entry.revision)
|
|
117
|
+
return false;
|
|
118
|
+
index.set(encoded, entry);
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
function restrictedRefusal({ opts }, entry) {
|
|
122
|
+
return !opts.acceptsRestricted && entry.document?.visibility.kind === 'restricted' ? 'this provider accepts public documents only' : undefined;
|
|
123
|
+
}
|
|
124
|
+
function outcomeOf({ state, index, entry, pending }) {
|
|
125
|
+
const base = { key: entry.key, revision: entry.revision };
|
|
126
|
+
const refusal = isRevision(entry.revision) ? restrictedRefusal(state, entry) : 'revision must be a non-negative safe integer';
|
|
127
|
+
if (refusal !== undefined)
|
|
128
|
+
return { ...base, status: 'rejected', message: refusal };
|
|
129
|
+
const current = index.get(encodeSearchKey({ key: entry.key }));
|
|
130
|
+
if (current !== undefined && current.revision >= entry.revision)
|
|
131
|
+
return { ...base, status: 'stale' };
|
|
132
|
+
if (!state.opts.asyncIndexing) {
|
|
133
|
+
storeEntry(index, entry);
|
|
134
|
+
return { ...base, status: 'applied' };
|
|
182
135
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
136
|
+
pending.push(entry);
|
|
137
|
+
return { ...base, status: 'pending' };
|
|
138
|
+
}
|
|
139
|
+
function writeEntries({ state, ctx, entries }) {
|
|
140
|
+
const { maxBatch, available } = state.opts;
|
|
141
|
+
if (!available({ ctx }))
|
|
142
|
+
return unavailable();
|
|
143
|
+
if (entries.length > maxBatch)
|
|
144
|
+
return providerRejected({ code: 'invalid_request', message: `at most ${maxBatch} items per write` });
|
|
145
|
+
const index = state.indexes.get(ctx.indexName);
|
|
146
|
+
if (index === undefined)
|
|
147
|
+
return missing(ctx.indexName);
|
|
148
|
+
const pending = [];
|
|
149
|
+
const outcomes = entries.map(entry => outcomeOf({ state, index, entry, pending }));
|
|
150
|
+
if (pending.length === 0)
|
|
151
|
+
return providerOk({ value: { outcomes } });
|
|
152
|
+
state.taskCounter += 1;
|
|
153
|
+
const taskId = `task_${state.taskCounter}`;
|
|
154
|
+
state.queued.set(taskId, { indexName: ctx.indexName, entries: pending });
|
|
155
|
+
return providerOk({ value: { outcomes, taskId } });
|
|
156
|
+
}
|
|
157
|
+
/** Settle every queued task up to and including `taskId`, in write order. */
|
|
158
|
+
function settleThrough({ queued, indexes, settled }, { taskId }) {
|
|
159
|
+
const ids = [...queued.keys()];
|
|
160
|
+
// awaitTask calls this only when `taskId` is queued, so indexOf is never -1.
|
|
161
|
+
for (const queuedId of ids.slice(0, ids.indexOf(taskId) + 1)) {
|
|
162
|
+
const task = queued.get(queuedId);
|
|
163
|
+
const index = indexes.get(task.indexName);
|
|
164
|
+
// A task whose index was dropped meanwhile has nothing left to write into.
|
|
165
|
+
if (index !== undefined)
|
|
166
|
+
for (const entry of task.entries)
|
|
167
|
+
storeEntry(index, entry);
|
|
168
|
+
queued.delete(queuedId);
|
|
169
|
+
settled.add(queuedId);
|
|
188
170
|
}
|
|
189
|
-
|
|
171
|
+
}
|
|
172
|
+
async function guarded({ opts, indexes }, { ctx, run }) {
|
|
173
|
+
if (!opts.available({ ctx }))
|
|
174
|
+
return unavailable();
|
|
175
|
+
const index = indexes.get(ctx.indexName);
|
|
176
|
+
return index === undefined ? missing(ctx.indexName) : run(index);
|
|
177
|
+
}
|
|
178
|
+
async function awaitProviderTask({ state, ctx, taskId }) {
|
|
179
|
+
if (!state.opts.available({ ctx }))
|
|
180
|
+
return unavailable();
|
|
181
|
+
if (state.queued.has(taskId))
|
|
182
|
+
settleThrough(state, { taskId });
|
|
183
|
+
if (state.settled.has(taskId))
|
|
184
|
+
return providerOk({ value: 'succeeded' });
|
|
185
|
+
return providerRejected({ code: 'not_found', message: `task '${taskId}' is not known` });
|
|
186
|
+
}
|
|
187
|
+
function createBaseProvider(state) {
|
|
188
|
+
const { id, label, acceptsRestricted, asyncIndexing, typoTolerance, locales, maxBatch, available } = state.opts;
|
|
189
|
+
const { indexes } = state;
|
|
190
|
+
const capabilities = { acceptsRestricted, typoTolerance, locales, maxBatch, asyncIndexing };
|
|
191
|
+
return {
|
|
190
192
|
id, label, capabilities, credentialKeys: [],
|
|
191
193
|
async ensureIndex(ctx) {
|
|
192
194
|
if (!available({ ctx }))
|
|
@@ -202,36 +204,50 @@ export function createInMemorySearchProvider(_required = {}, optional = {}) {
|
|
|
202
204
|
return providerOk({ value: undefined });
|
|
203
205
|
},
|
|
204
206
|
async upsert(ctx, { documents }) {
|
|
205
|
-
return
|
|
207
|
+
return writeEntries({
|
|
208
|
+
state,
|
|
209
|
+
ctx,
|
|
210
|
+
entries: documents.map(document => ({ key: document.key, revision: document.revision, document })),
|
|
211
|
+
});
|
|
206
212
|
},
|
|
207
213
|
async remove(ctx, { tombstones }) {
|
|
208
|
-
return
|
|
214
|
+
return writeEntries({
|
|
215
|
+
state,
|
|
216
|
+
ctx,
|
|
217
|
+
entries: tombstones.map((tombstone) => ({ key: tombstone.key, revision: tombstone.revision })),
|
|
218
|
+
});
|
|
209
219
|
},
|
|
210
220
|
async query(ctx, { query }) {
|
|
211
|
-
return guarded(ctx, index => runQuery(index, query, typoTolerance, ctx));
|
|
221
|
+
return guarded(state, { ctx, run: index => runQuery(index, query, typoTolerance, ctx) });
|
|
212
222
|
},
|
|
213
223
|
async revisionOf(ctx, { keys }) {
|
|
214
|
-
return guarded(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
224
|
+
return guarded(state, {
|
|
225
|
+
ctx,
|
|
226
|
+
run: index => providerOk({
|
|
227
|
+
value: keys.map((key) => {
|
|
228
|
+
const entry = index.get(encodeSearchKey({ key }));
|
|
229
|
+
return entry === undefined ? { key, revision: null, deleted: false } : { key, revision: entry.revision, deleted: entry.document === undefined };
|
|
230
|
+
}),
|
|
218
231
|
}),
|
|
219
|
-
})
|
|
232
|
+
});
|
|
220
233
|
},
|
|
221
234
|
};
|
|
222
|
-
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* The reference provider. Case-insensitive word matching over title + body (every query word must
|
|
238
|
+
* match), plain-text snippet windows with highlight ranges, versioned tombstones, cursor = offset.
|
|
239
|
+
* With `asyncIndexing`, a non-stale write answers `pending` with a task id and becomes visible only
|
|
240
|
+
* when that task (and every earlier one) is settled by the next `awaitTask` call.
|
|
241
|
+
* @complexity Writes O(batch); queries O(documents in the index × words).
|
|
242
|
+
*/
|
|
243
|
+
export function createInMemorySearchProvider(_required = {}, optional = {}) {
|
|
244
|
+
const state = { opts: resolveOptions(optional), indexes: new Map(), queued: new Map(), settled: new Set(), taskCounter: 0 };
|
|
245
|
+
const provider = createBaseProvider(state);
|
|
246
|
+
if (!state.opts.asyncIndexing)
|
|
223
247
|
return provider;
|
|
224
248
|
return {
|
|
225
249
|
...provider,
|
|
226
|
-
|
|
227
|
-
if (!available({ ctx }))
|
|
228
|
-
return unavailable();
|
|
229
|
-
if (queued.has(taskId))
|
|
230
|
-
settleThrough(taskId);
|
|
231
|
-
if (settled.has(taskId))
|
|
232
|
-
return providerOk({ value: 'succeeded' });
|
|
233
|
-
return providerRejected({ code: 'not_found', message: `task '${taskId}' is not known` });
|
|
234
|
-
},
|
|
250
|
+
awaitTask: (ctx, { taskId }) => awaitProviderTask({ state, ctx, taskId }),
|
|
235
251
|
};
|
|
236
252
|
}
|
|
237
253
|
//# sourceMappingURL=memory-provider.js.map
|
package/dist/tools.js
CHANGED
|
@@ -120,38 +120,10 @@ export function buildSearchToolRegistrations(required, optional = {}) {
|
|
|
120
120
|
const allow = (ctx, permission) => required.requirePermission({ principalId: ctx.principal.id, permission });
|
|
121
121
|
const activeProviderId = async () => (await service.status({ siteId })).active?.providerId ?? null;
|
|
122
122
|
/** Shared approval lifecycle for the two writes; `run` re-checks permission after the human wait. */
|
|
123
|
-
const approvalTool = (
|
|
124
|
-
prepare: ({ ctx }) => spec.prepare(ctx),
|
|
125
|
-
describe: ({ prepared }) => {
|
|
126
|
-
if ('refusal' in prepared)
|
|
127
|
-
return { ask: false, description: undefined };
|
|
128
|
-
const key = spec.rememberKey(prepared.providerId);
|
|
129
|
-
const description = { action: spec.action, siteId, ...prepared, summary: spec.summary(prepared) };
|
|
130
|
-
return { ask: true, description, ...(key === undefined ? {} : { rememberKey: key }) };
|
|
131
|
-
},
|
|
132
|
-
askHuman: ({ ctx, description }, options) => required.askHuman({ ctx, description: description }, options),
|
|
133
|
-
notConfirmed: ({ reason }) => ({ started: false, reason }),
|
|
134
|
-
run: async ({ ctx, prepared }) => {
|
|
135
|
-
if ('refusal' in prepared)
|
|
136
|
-
return prepared.refusal;
|
|
137
|
-
await allow(ctx, SEARCH_MANAGE_PERMISSION);
|
|
138
|
-
await spec.effect(prepared.providerId, ctx);
|
|
139
|
-
const targets = await indexer.startRebuild({ siteId, workspaceId, providerId: prepared.providerId });
|
|
140
|
-
return { started: true, providerId: prepared.providerId, generation: targets.building?.generation ?? null };
|
|
141
|
-
},
|
|
142
|
-
}, optional.remembered ? { remembered: optional.remembered } : {});
|
|
123
|
+
const approvalTool = createSearchApprovalTool(required, optional, { siteId, workspaceId, indexer, allow });
|
|
143
124
|
const reindex = approvalTool({
|
|
144
125
|
action: 'reindex',
|
|
145
|
-
prepare:
|
|
146
|
-
requireNoInput({ input: ctx.input });
|
|
147
|
-
await allow(ctx, SEARCH_MANAGE_PERMISSION);
|
|
148
|
-
const providerId = await activeProviderId();
|
|
149
|
-
if (providerId === null)
|
|
150
|
-
return { refusal: { started: false, code: 'not_configured', message: 'search has no active provider yet; use search_set_provider' } };
|
|
151
|
-
if (!registry.has({ id: providerId }))
|
|
152
|
-
return { refusal: { started: false, code: 'provider_not_registered', message: `the active search provider '${providerId}' is not loaded` } };
|
|
153
|
-
return { providerId, fromProviderId: providerId };
|
|
154
|
-
},
|
|
126
|
+
prepare: createSearchReindexPreparation({ registry, allow, activeProviderId }),
|
|
155
127
|
// A reindex of the same provider is a repeatable cost decision, so a grant may be remembered per provider.
|
|
156
128
|
rememberKey: providerId => `search_reindex:${providerId}`,
|
|
157
129
|
summary: ({ providerId }) => `Rebuild the search index in '${providerId}' (writes every public document to it).`,
|
|
@@ -192,4 +164,38 @@ export function buildSearchToolRegistrations(required, optional = {}) {
|
|
|
192
164
|
},
|
|
193
165
|
});
|
|
194
166
|
}
|
|
167
|
+
function createSearchReindexPreparation({ registry, allow, activeProviderId }) {
|
|
168
|
+
return async (ctx) => {
|
|
169
|
+
requireNoInput({ input: ctx.input });
|
|
170
|
+
await allow(ctx, SEARCH_MANAGE_PERMISSION);
|
|
171
|
+
const providerId = await activeProviderId();
|
|
172
|
+
if (providerId === null)
|
|
173
|
+
return { refusal: { started: false, code: 'not_configured', message: 'search has no active provider yet; use search_set_provider' } };
|
|
174
|
+
if (!registry.has({ id: providerId }))
|
|
175
|
+
return { refusal: { started: false, code: 'provider_not_registered', message: `the active search provider '${providerId}' is not loaded` } };
|
|
176
|
+
return { providerId, fromProviderId: providerId };
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function createSearchApprovalTool(required, optional, { siteId, workspaceId, indexer, allow }) {
|
|
180
|
+
return (spec) => createApprovalHandler({
|
|
181
|
+
prepare: ({ ctx }) => spec.prepare(ctx),
|
|
182
|
+
describe: ({ prepared }) => {
|
|
183
|
+
if ('refusal' in prepared)
|
|
184
|
+
return { ask: false, description: undefined };
|
|
185
|
+
const key = spec.rememberKey(prepared.providerId);
|
|
186
|
+
const description = { action: spec.action, siteId, ...prepared, summary: spec.summary(prepared) };
|
|
187
|
+
return { ask: true, description, ...(key === undefined ? {} : { rememberKey: key }) };
|
|
188
|
+
},
|
|
189
|
+
askHuman: ({ ctx, description }, options) => required.askHuman({ ctx, description: description }, options),
|
|
190
|
+
notConfirmed: ({ reason }) => ({ started: false, reason }),
|
|
191
|
+
run: async ({ ctx, prepared }) => {
|
|
192
|
+
if ('refusal' in prepared)
|
|
193
|
+
return prepared.refusal;
|
|
194
|
+
await allow(ctx, SEARCH_MANAGE_PERMISSION);
|
|
195
|
+
await spec.effect(prepared.providerId, ctx);
|
|
196
|
+
const targets = await indexer.startRebuild({ siteId, workspaceId, providerId: prepared.providerId });
|
|
197
|
+
return { started: true, providerId: prepared.providerId, generation: targets.building?.generation ?? null };
|
|
198
|
+
},
|
|
199
|
+
}, optional.remembered ? { remembered: optional.remembered } : {});
|
|
200
|
+
}
|
|
195
201
|
//# sourceMappingURL=tools.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jini-ai/search",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3-next.1",
|
|
4
4
|
"description": "Generic site-search contract: provider interface, registry, single-writer indexer, query service with access revalidation, agent tools and a provider conformance test-kit.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -15,11 +15,9 @@
|
|
|
15
15
|
"entries": {
|
|
16
16
|
".": "universal",
|
|
17
17
|
"./tools": "universal",
|
|
18
|
+
"./http-query": "universal",
|
|
18
19
|
"./testkit": "universal",
|
|
19
20
|
"./contribution": "node"
|
|
20
|
-
},
|
|
21
|
-
"optionalPeers": {
|
|
22
|
-
"@jini-ai/agent-plugins": "opt-in: agent plugin integrations; import @jini-ai/search/contribution — Agent plugin integrations could not load @jini-ai/agent-plugins. Install @jini-ai/agent-plugins to enable this feature, or check its installed version and native binaries."
|
|
23
21
|
}
|
|
24
22
|
},
|
|
25
23
|
"type": "module",
|
|
@@ -30,6 +28,11 @@
|
|
|
30
28
|
"import": "./dist/index.js",
|
|
31
29
|
"default": "./dist/index.js"
|
|
32
30
|
},
|
|
31
|
+
"./http-query": {
|
|
32
|
+
"types": "./dist/http-query.d.ts",
|
|
33
|
+
"import": "./dist/http-query.js",
|
|
34
|
+
"default": "./dist/http-query.js"
|
|
35
|
+
},
|
|
33
36
|
"./tools": {
|
|
34
37
|
"types": "./dist/tools.d.ts",
|
|
35
38
|
"import": "./dist/tools.js",
|
|
@@ -58,20 +61,12 @@
|
|
|
58
61
|
"registry": "https://registry.npmjs.org"
|
|
59
62
|
},
|
|
60
63
|
"dependencies": {
|
|
61
|
-
"@jini-ai/core": "^0.7.
|
|
64
|
+
"@jini-ai/core": "^0.7.3-next.1"
|
|
62
65
|
},
|
|
63
66
|
"devDependencies": {
|
|
64
67
|
"@vitest/coverage-v8": "^2.1.9",
|
|
65
68
|
"vitest": "^2.1.9",
|
|
66
|
-
"@jini-ai/agent-plugins": "0.7.
|
|
67
|
-
},
|
|
68
|
-
"peerDependencies": {
|
|
69
|
-
"@jini-ai/agent-plugins": "^0.7.0"
|
|
70
|
-
},
|
|
71
|
-
"peerDependenciesMeta": {
|
|
72
|
-
"@jini-ai/agent-plugins": {
|
|
73
|
-
"optional": true
|
|
74
|
-
}
|
|
69
|
+
"@jini-ai/agent-plugins": "0.7.3-next.1"
|
|
75
70
|
},
|
|
76
71
|
"scripts": {
|
|
77
72
|
"build": "tsc -p tsconfig.json",
|