@jini-ai/search 0.0.0-stage → 0.1.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.
- package/LICENSE +201 -0
- package/README.md +76 -2
- package/dist/api.d.ts +92 -0
- package/dist/api.js +2 -0
- package/dist/contract.d.ts +219 -0
- package/dist/contract.js +46 -0
- package/dist/contribution.d.ts +61 -0
- package/dist/contribution.js +124 -0
- package/dist/digest.d.ts +13 -0
- package/dist/digest.js +55 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/indexer.d.ts +50 -0
- package/dist/indexer.js +402 -0
- package/dist/ports.d.ts +194 -0
- package/dist/ports.js +2 -0
- package/dist/registry.d.ts +33 -0
- package/dist/registry.js +81 -0
- package/dist/service.d.ts +37 -0
- package/dist/service.js +118 -0
- package/dist/testkit/assert.d.ts +14 -0
- package/dist/testkit/assert.js +20 -0
- package/dist/testkit/conformance.d.ts +32 -0
- package/dist/testkit/conformance.js +453 -0
- package/dist/testkit/fakes.d.ts +56 -0
- package/dist/testkit/fakes.js +114 -0
- package/dist/testkit/index.d.ts +9 -0
- package/dist/testkit/index.js +9 -0
- package/dist/testkit/memory-provider.d.ts +23 -0
- package/dist/testkit/memory-provider.js +237 -0
- package/dist/tokens.d.ts +9 -0
- package/dist/tokens.js +12 -0
- package/dist/tools.d.ts +74 -0
- package/dist/tools.js +194 -0
- package/package.json +75 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,77 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @jini-ai/search
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A generic site-search capability: one provider contract every search backend implements, a
|
|
4
|
+
registry for those providers, a single-writer indexer, a query service that re-checks access before
|
|
5
|
+
showing a hit, agent tools, and a conformance kit that every provider must pass unchanged.
|
|
6
|
+
|
|
7
|
+
The engine holds no product wiring. A host supplies its document sources, its index bookkeeping
|
|
8
|
+
table, its per-site target pointer and its access check as ports (`src/ports.ts`), and binds the
|
|
9
|
+
results through the composition tokens.
|
|
10
|
+
|
|
11
|
+
## Entry points
|
|
12
|
+
|
|
13
|
+
| Import | Runtime | What it gives you |
|
|
14
|
+
|---|---|---|
|
|
15
|
+
| `@jini-ai/search` | universal | The contract (`SearchProvider`, documents, keys, revisions, tombstones), host ports, the `SearchService`/`SearchIndexer` APIs, `createSearchProviderRegistry`, `validateSearchProvider`, and the tokens `SearchProviderRegistryToken`, `SearchServiceToken`, `SearchIndexerToken`, `SearchDocumentSourceToken` (many). |
|
|
16
|
+
| `@jini-ai/search/tools` | universal | Agent tools over a `SearchService`. |
|
|
17
|
+
| `@jini-ai/search/contribution` | node | `defineSearchProviderContribution({ filename })` for plugin-contributed providers, loaded through `@jini-ai/agent-plugins`' executable contribution loader (an optional peer dependency), plus `contributeProviders`. |
|
|
18
|
+
| `@jini-ai/search/testkit` | universal | `runSearchProviderConformance`, the in-memory reference provider, and fakes (`createFakeHttpClient`, `createUnavailableHttpClient`, `createInMemorySearchState`, `createStaticAccessPort`, `createPredicateAccessPort`). Test-only. |
|
|
19
|
+
|
|
20
|
+
## Plugin providers
|
|
21
|
+
|
|
22
|
+
A plugin ships a manifest at its package root, under the filename the host chooses:
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{ "schemaVersion": 1, "providers": [{ "id": "meilisearch", "label": "Meilisearch", "module": "dist/provider.mjs" }] }
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The module's default export must be a `SearchProvider` whose `id` equals the manifest id. Plugin
|
|
29
|
+
providers are public-only: one that declares `capabilities.acceptsRestricted: true` is refused,
|
|
30
|
+
because audience-restricted content may only live in the host's own built-in provider. Every fault
|
|
31
|
+
becomes a refusal line, never a boot crash.
|
|
32
|
+
|
|
33
|
+
## Running the conformance kit
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { test } from 'vitest';
|
|
37
|
+
import { runSearchProviderConformance, createUnavailableHttpClient } from '@jini-ai/search/testkit';
|
|
38
|
+
|
|
39
|
+
runSearchProviderConformance({
|
|
40
|
+
name: 'my provider',
|
|
41
|
+
test,
|
|
42
|
+
makeProvider: () => createMyProvider(),
|
|
43
|
+
makeContext: ({ siteId, indexName }) => ({ workspaceId: 'w', siteId, indexName, mode: 'test', credentials, httpClient, timeoutMs: 5000, now: Date.now }),
|
|
44
|
+
}, { makeUnavailableContext: input => ({ ...contextFor(input), httpClient: createUnavailableHttpClient() }) });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The runner is injected, so the kit works with vitest, node:test or jest. Each case uses its own
|
|
48
|
+
index name, drops and re-creates it first, and settles `pending` writes through `awaitTask` when the
|
|
49
|
+
provider declares `asyncIndexing`.
|
|
50
|
+
|
|
51
|
+
## What a built-in provider must implement
|
|
52
|
+
|
|
53
|
+
The owner's built-in provider is a plain `SearchProvider`, registered by the composition root as a
|
|
54
|
+
core provider (`createSearchProviderRegistry({}, { seed: [{ id: 'builtin', provider }] })`). It must:
|
|
55
|
+
|
|
56
|
+
- use `id: 'builtin'` (`BUILTIN_SEARCH_PROVIDER_ID`, also the registry's default) and
|
|
57
|
+
`credentialKeys: []`;
|
|
58
|
+
- declare honest capabilities. `acceptsRestricted: true` is allowed here and only here, since the
|
|
59
|
+
built-in is the one provider that may hold audience-restricted documents. It must then return a
|
|
60
|
+
restricted document only to a query whose `audiences` intersect the document's;
|
|
61
|
+
- keep its data in its own tables, reached through `ctx.storage`, created by its own TS migration.
|
|
62
|
+
It never reads or writes host tables;
|
|
63
|
+
- partition everything by `ctx.indexName`, so two sites or two generations never see each
|
|
64
|
+
other's documents;
|
|
65
|
+
- follow the revision rules. Revisions are numbers. A write (document or tombstone) whose revision
|
|
66
|
+
is not strictly greater than what is held for the key is `stale` and changes nothing. `remove`
|
|
67
|
+
writes a versioned tombstone, also for a key that was never indexed, so a delayed lower-revision
|
|
68
|
+
upsert can never resurrect a removed document. `revisionOf` reports absent as
|
|
69
|
+
`{ revision: null, deleted: false }` and a tombstone as `{ revision, deleted: true }`;
|
|
70
|
+
- report one write outcome per input, in input order;
|
|
71
|
+
- return plain-text snippets (never HTML) with highlight ranges as UTF-16 offsets inside the snippet;
|
|
72
|
+
- reject empty or whitespace query text as `invalid_request`, and paginate with an opaque cursor that
|
|
73
|
+
gives stable pages with no overlap;
|
|
74
|
+
- never throw. A definite refusal is `rejected`. An indeterminate outcome (timeout, storage down) is
|
|
75
|
+
`unknown`;
|
|
76
|
+
- pass `runSearchProviderConformance` unchanged. The in-memory reference provider in the testkit
|
|
77
|
+
shows the expected semantics, but it is not the built-in and must never be registered in a host.
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The two runtime objects a host consumes (FROZEN for this build, 2026-10-21; change additively).
|
|
3
|
+
*
|
|
4
|
+
* Consumers (a public `/api/search` route, a theme search block, agent tools) only ever receive a
|
|
5
|
+
* {@link SearchService}. The composition root alone holds the {@link SearchIndexer} and wires it to
|
|
6
|
+
* the host event bus and a background job.
|
|
7
|
+
*/
|
|
8
|
+
import type { SearchPage, SearchQuery } from './contract.js';
|
|
9
|
+
import type { SearchSiteTargets, SearchSourceEvent, SearchStatusCounts } from './ports.js';
|
|
10
|
+
export type SearchUnavailableReason = 'not_configured' | 'timeout' | 'provider_unavailable' | 'provider_error' | 'access_check_failed';
|
|
11
|
+
export type SearchServiceResult = {
|
|
12
|
+
readonly status: 'ok';
|
|
13
|
+
readonly providerId: string;
|
|
14
|
+
readonly page: SearchPage;
|
|
15
|
+
} | {
|
|
16
|
+
readonly status: 'invalid';
|
|
17
|
+
readonly message: string;
|
|
18
|
+
} | {
|
|
19
|
+
readonly status: 'unavailable';
|
|
20
|
+
readonly reason: SearchUnavailableReason;
|
|
21
|
+
};
|
|
22
|
+
export interface SearchTargetStatus {
|
|
23
|
+
readonly providerId: string;
|
|
24
|
+
readonly generation: number;
|
|
25
|
+
readonly registered: boolean;
|
|
26
|
+
readonly counts: SearchStatusCounts;
|
|
27
|
+
}
|
|
28
|
+
export interface SearchStatus {
|
|
29
|
+
readonly siteId: string;
|
|
30
|
+
readonly active: SearchTargetStatus | null;
|
|
31
|
+
readonly building: SearchTargetStatus | null;
|
|
32
|
+
}
|
|
33
|
+
export interface SearchService {
|
|
34
|
+
/**
|
|
35
|
+
* Run a query against the site's ACTIVE target, then drop every hit the access port does not
|
|
36
|
+
* confirm as visible right now. `audiences` comes only from the host's viewer resolution; it is
|
|
37
|
+
* ignored (public only) when the active provider does not accept restricted content.
|
|
38
|
+
*/
|
|
39
|
+
query(required: {
|
|
40
|
+
readonly siteId: string;
|
|
41
|
+
readonly workspaceId: string;
|
|
42
|
+
readonly query: Omit<SearchQuery, 'audiences'>;
|
|
43
|
+
}, optional?: {
|
|
44
|
+
readonly audiences?: readonly string[];
|
|
45
|
+
}): Promise<SearchServiceResult>;
|
|
46
|
+
status(required: {
|
|
47
|
+
readonly siteId: string;
|
|
48
|
+
}): Promise<SearchStatus>;
|
|
49
|
+
}
|
|
50
|
+
export interface SearchBackfillResult {
|
|
51
|
+
/** Every source scanned to completion for the building (or active) target. */
|
|
52
|
+
readonly done: boolean;
|
|
53
|
+
readonly written: number;
|
|
54
|
+
/** The building target was promoted to active in this call. */
|
|
55
|
+
readonly promoted: boolean;
|
|
56
|
+
}
|
|
57
|
+
export interface SearchIndexer {
|
|
58
|
+
/** Event names to subscribe to (union of every source's `events`). */
|
|
59
|
+
readonly eventNames: readonly string[];
|
|
60
|
+
/** Bus handler: re-load the named document and write it (or its tombstone) to every target. */
|
|
61
|
+
handleEvent(required: {
|
|
62
|
+
readonly event: SearchSourceEvent;
|
|
63
|
+
}): Promise<void>;
|
|
64
|
+
/** Retry `failed` rows and settle `pending`/`unknown` rows (awaitTask / revisionOf). Bounded. */
|
|
65
|
+
reconcile(required: {
|
|
66
|
+
readonly siteId: string;
|
|
67
|
+
readonly workspaceId: string;
|
|
68
|
+
}, optional?: {
|
|
69
|
+
readonly limit?: number;
|
|
70
|
+
}): Promise<{
|
|
71
|
+
readonly settled: number;
|
|
72
|
+
readonly remaining: number;
|
|
73
|
+
}>;
|
|
74
|
+
/**
|
|
75
|
+
* Resumable backfill of the building target (else the active one) from each source's `scan`,
|
|
76
|
+
* continuing from the stored watermark; promotes building→active atomically once every source is
|
|
77
|
+
* done and nothing is pending/unknown. Bounded by `optional.pageBudget` pages.
|
|
78
|
+
*/
|
|
79
|
+
backfill(required: {
|
|
80
|
+
readonly siteId: string;
|
|
81
|
+
readonly workspaceId: string;
|
|
82
|
+
}, optional?: {
|
|
83
|
+
readonly pageBudget?: number;
|
|
84
|
+
}): Promise<SearchBackfillResult>;
|
|
85
|
+
/** Start a rebuild into a new generation of `providerId` (a provider switch or a full reindex). */
|
|
86
|
+
startRebuild(required: {
|
|
87
|
+
readonly siteId: string;
|
|
88
|
+
readonly workspaceId: string;
|
|
89
|
+
readonly providerId: string;
|
|
90
|
+
}): Promise<SearchSiteTargets>;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=api.d.ts.map
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The site-search provider contract (FROZEN for this build, 2026-10-21; change additively).
|
|
3
|
+
*
|
|
4
|
+
* One interface every search backend implements: the owner's built-in provider, vendor plugins
|
|
5
|
+
* (Meilisearch first) and the in-memory reference provider the conformance kit runs against. The
|
|
6
|
+
* host never talks to a backend except through {@link SearchProvider}; the indexer
|
|
7
|
+
* (`indexer.ts`) is the ONE writer and the query service (`service.ts`) the ONE reader.
|
|
8
|
+
*
|
|
9
|
+
* Decisions folded in from the 2026-10-21 provider gap check (they override the first design):
|
|
10
|
+
* - Keys are namespaced site/workspace/collection/document, so two sites can never collide even when
|
|
11
|
+
* they share a vendor index or a document id.
|
|
12
|
+
* - `revision` is a NUMBER (a string version sorts "10" before "9"). A write whose revision is not
|
|
13
|
+
* strictly greater than what the provider holds for that key — including a tombstone — is `stale`
|
|
14
|
+
* and changes nothing, so a delayed write can never resurrect a removed document.
|
|
15
|
+
* - Removal writes a versioned tombstone, also for a key that was never indexed.
|
|
16
|
+
* - Vendors are PUBLIC-ONLY: only a provider declaring `acceptsRestricted` may receive a document
|
|
17
|
+
* with restricted visibility (the indexer turns such a write into a removal for every other
|
|
18
|
+
* provider). Snippets are plain text plus highlight ranges, never HTML.
|
|
19
|
+
* - Facets are not in v1 (declared-but-never-returned was a contract bug).
|
|
20
|
+
* - Methods never throw: definite refusals are `rejected`, indeterminate outcomes (timeout, network,
|
|
21
|
+
* provider down) are `unknown` and are reconciled, never blindly retried.
|
|
22
|
+
*/
|
|
23
|
+
import type { HttpClientPort } from '@jini-ai/core/primitives';
|
|
24
|
+
import type { ProviderContext, ProviderResult } from '@jini-ai/core/provider-registry';
|
|
25
|
+
/** The contract name used by the provider registry and plugin contributions. */
|
|
26
|
+
export declare const SEARCH_CONTRACT = "search";
|
|
27
|
+
/** The id the owner's in-process provider registers under; also the per-site default. */
|
|
28
|
+
export declare const BUILTIN_SEARCH_PROVIDER_ID = "builtin";
|
|
29
|
+
/** A document's identity. Every part is part of the key; none is optional. */
|
|
30
|
+
export interface SearchDocumentKey {
|
|
31
|
+
readonly siteId: string;
|
|
32
|
+
readonly workspaceId: string;
|
|
33
|
+
/** e.g. `post`, `page`, `product`. Grammar `^[a-z][a-z0-9_]{0,63}$`. */
|
|
34
|
+
readonly collection: string;
|
|
35
|
+
readonly documentId: string;
|
|
36
|
+
}
|
|
37
|
+
/** Who may see a document. Restricted documents reach only providers with `acceptsRestricted`. */
|
|
38
|
+
export type SearchVisibility = {
|
|
39
|
+
readonly kind: 'public';
|
|
40
|
+
} | {
|
|
41
|
+
readonly kind: 'restricted';
|
|
42
|
+
readonly audiences: readonly string[];
|
|
43
|
+
};
|
|
44
|
+
export type SearchFieldValue = string | number | boolean | readonly string[];
|
|
45
|
+
export interface SearchDocument {
|
|
46
|
+
readonly key: SearchDocumentKey;
|
|
47
|
+
/** Non-negative safe integer, strictly increasing per key across every change incl. removal. */
|
|
48
|
+
readonly revision: number;
|
|
49
|
+
readonly locale?: string;
|
|
50
|
+
/** Site-relative public URL, e.g. `/blog/hello`. */
|
|
51
|
+
readonly url: string;
|
|
52
|
+
/** Plain text; a provider must never interpret it as markup. */
|
|
53
|
+
readonly title: string;
|
|
54
|
+
/** Plain text; a provider must never interpret it as markup. */
|
|
55
|
+
readonly body: string;
|
|
56
|
+
/** Filterable attributes (exact match only in v1). */
|
|
57
|
+
readonly fields?: Readonly<Record<string, SearchFieldValue>>;
|
|
58
|
+
readonly visibility: SearchVisibility;
|
|
59
|
+
}
|
|
60
|
+
/** A versioned removal. A later upsert must carry a strictly greater revision to revive the key. */
|
|
61
|
+
export interface SearchTombstone {
|
|
62
|
+
readonly key: SearchDocumentKey;
|
|
63
|
+
readonly revision: number;
|
|
64
|
+
}
|
|
65
|
+
export interface SearchQuery {
|
|
66
|
+
/** Required, trimmed non-empty; an empty text is `rejected` with `invalid_request`. */
|
|
67
|
+
readonly text: string;
|
|
68
|
+
readonly collections?: readonly string[];
|
|
69
|
+
readonly locale?: string;
|
|
70
|
+
/** Exact-match filters on `SearchDocument.fields`. */
|
|
71
|
+
readonly filters?: Readonly<Record<string, string | number | boolean>>;
|
|
72
|
+
/** Set ONLY by the host from the viewer's principal, never from user input. Absent = public only. */
|
|
73
|
+
readonly audiences?: readonly string[];
|
|
74
|
+
/** 1..100; default 10. */
|
|
75
|
+
readonly limit?: number;
|
|
76
|
+
/** Opaque cursor from a previous page of the same query. */
|
|
77
|
+
readonly cursor?: string;
|
|
78
|
+
}
|
|
79
|
+
/** UTF-16 code-unit offsets into `SearchHit.snippet`, `start < end <= snippet.length`. */
|
|
80
|
+
export interface SearchHighlight {
|
|
81
|
+
readonly start: number;
|
|
82
|
+
readonly end: number;
|
|
83
|
+
}
|
|
84
|
+
export interface SearchHit {
|
|
85
|
+
readonly key: SearchDocumentKey;
|
|
86
|
+
/** The revision the provider returned this hit from (the service revalidates it). */
|
|
87
|
+
readonly revision: number;
|
|
88
|
+
readonly url: string;
|
|
89
|
+
readonly title: string;
|
|
90
|
+
/** Plain text excerpt of the body (no HTML). May be empty only when the body is empty. */
|
|
91
|
+
readonly snippet: string;
|
|
92
|
+
readonly highlights: readonly SearchHighlight[];
|
|
93
|
+
/** Provider-private relevance; only the ordering within one page is meaningful. */
|
|
94
|
+
readonly score: number;
|
|
95
|
+
}
|
|
96
|
+
export interface SearchPage {
|
|
97
|
+
readonly hits: readonly SearchHit[];
|
|
98
|
+
readonly nextCursor?: string;
|
|
99
|
+
}
|
|
100
|
+
export interface SearchCapabilities {
|
|
101
|
+
/** Only the in-process built-in provider may set this; vendors must declare `false`. */
|
|
102
|
+
readonly acceptsRestricted: boolean;
|
|
103
|
+
readonly typoTolerance: boolean;
|
|
104
|
+
readonly locales: readonly string[] | 'any';
|
|
105
|
+
/** Max documents/tombstones per write call. */
|
|
106
|
+
readonly maxBatch: number;
|
|
107
|
+
/** Writes may answer `pending` with a `taskId`; the provider then implements `awaitTask`. */
|
|
108
|
+
readonly asyncIndexing: boolean;
|
|
109
|
+
}
|
|
110
|
+
/** Per-key outcome of one write call. A write call reports one outcome per input, in input order. */
|
|
111
|
+
export type SearchWriteStatus =
|
|
112
|
+
/** Durable and visible to queries. */
|
|
113
|
+
'applied'
|
|
114
|
+
/** Ignored: the provider already holds an equal or greater revision (document or tombstone). */
|
|
115
|
+
| 'stale'
|
|
116
|
+
/** Accepted asynchronously; settle with `awaitTask(taskId)`. */
|
|
117
|
+
| 'pending'
|
|
118
|
+
/** Refused for this key only (e.g. too large); the message is caller-safe. */
|
|
119
|
+
| 'rejected';
|
|
120
|
+
export interface SearchWriteOutcome {
|
|
121
|
+
readonly key: SearchDocumentKey;
|
|
122
|
+
readonly revision: number;
|
|
123
|
+
readonly status: SearchWriteStatus;
|
|
124
|
+
readonly message?: string;
|
|
125
|
+
}
|
|
126
|
+
export interface SearchWriteResult {
|
|
127
|
+
readonly outcomes: readonly SearchWriteOutcome[];
|
|
128
|
+
/** Set when any outcome is `pending`. */
|
|
129
|
+
readonly taskId?: string;
|
|
130
|
+
}
|
|
131
|
+
export type SearchTaskState = 'pending' | 'succeeded' | 'failed';
|
|
132
|
+
/** What the provider holds for a key: absent (never written), a live document or a tombstone. */
|
|
133
|
+
export interface SearchRevisionState {
|
|
134
|
+
readonly key: SearchDocumentKey;
|
|
135
|
+
readonly revision: number | null;
|
|
136
|
+
readonly deleted: boolean;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Pre-resolved per call by the host. Credentials come from the host's vault, never from the plugin
|
|
140
|
+
* manifest; the HTTP client is the host's guarded egress client (no raw fetch in a provider).
|
|
141
|
+
*/
|
|
142
|
+
export interface SearchProviderContext extends ProviderContext {
|
|
143
|
+
readonly siteId: string;
|
|
144
|
+
/** Unique per install + site + provider + generation (see `searchIndexName`). */
|
|
145
|
+
readonly indexName: string;
|
|
146
|
+
readonly credentials: Readonly<Record<string, string>>;
|
|
147
|
+
readonly httpClient: HttpClientPort;
|
|
148
|
+
/** Hard per-request budget; a provider maps an overrun to `unknown`/`timeout`. */
|
|
149
|
+
readonly timeoutMs: number;
|
|
150
|
+
readonly now: () => number;
|
|
151
|
+
/** Opaque host storage handle for in-process providers (the built-in); vendors ignore it. */
|
|
152
|
+
readonly storage?: unknown;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* A search backend. Stateless between calls (all state lives in the backend, keyed by
|
|
156
|
+
* `ctx.indexName`), never throws, and never reads or writes host tables except through
|
|
157
|
+
* `ctx.storage` on its own tables.
|
|
158
|
+
*/
|
|
159
|
+
export interface SearchProvider {
|
|
160
|
+
readonly id: string;
|
|
161
|
+
readonly label: string;
|
|
162
|
+
readonly capabilities: SearchCapabilities;
|
|
163
|
+
/** Names of the credential values the host must supply in `ctx.credentials`. */
|
|
164
|
+
readonly credentialKeys: readonly string[];
|
|
165
|
+
/** Create the index if missing and apply settings. Idempotent. */
|
|
166
|
+
ensureIndex(ctx: SearchProviderContext, required?: Record<string, never>): Promise<ProviderResult<void>>;
|
|
167
|
+
/** Delete the whole index (an old generation after a switch). Absent index is `ok`. */
|
|
168
|
+
dropIndex(ctx: SearchProviderContext, required?: Record<string, never>): Promise<ProviderResult<void>>;
|
|
169
|
+
upsert(ctx: SearchProviderContext, required: {
|
|
170
|
+
readonly documents: readonly SearchDocument[];
|
|
171
|
+
}): Promise<ProviderResult<SearchWriteResult>>;
|
|
172
|
+
remove(ctx: SearchProviderContext, required: {
|
|
173
|
+
readonly tombstones: readonly SearchTombstone[];
|
|
174
|
+
}): Promise<ProviderResult<SearchWriteResult>>;
|
|
175
|
+
/** Only documents keyed to `ctx.siteId` and `ctx.workspaceId` match, even when other tenants share the index. */
|
|
176
|
+
query(ctx: SearchProviderContext, required: {
|
|
177
|
+
readonly query: SearchQuery;
|
|
178
|
+
}): Promise<ProviderResult<SearchPage>>;
|
|
179
|
+
revisionOf(ctx: SearchProviderContext, required: {
|
|
180
|
+
readonly keys: readonly SearchDocumentKey[];
|
|
181
|
+
}): Promise<ProviderResult<readonly SearchRevisionState[]>>;
|
|
182
|
+
/** Required when `capabilities.asyncIndexing`; reports the state of an earlier `pending` write. */
|
|
183
|
+
awaitTask?(ctx: SearchProviderContext, required: {
|
|
184
|
+
readonly taskId: string;
|
|
185
|
+
}): Promise<ProviderResult<SearchTaskState>>;
|
|
186
|
+
}
|
|
187
|
+
/** What a plugin module's default export must be (validated by `contribution.ts`). */
|
|
188
|
+
export type SearchProviderModule = SearchProvider;
|
|
189
|
+
/**
|
|
190
|
+
* The single string form of a key, for maps, state rows and vendor document ids. Each part is
|
|
191
|
+
* `encodeURIComponent`-ed so a `/` inside a part can never forge another key.
|
|
192
|
+
* @complexity O(total key length).
|
|
193
|
+
*/
|
|
194
|
+
export declare function encodeSearchKey(required: {
|
|
195
|
+
readonly key: SearchDocumentKey;
|
|
196
|
+
}): string;
|
|
197
|
+
/** Inverse of {@link encodeSearchKey}; `undefined` for anything that is not exactly four parts. */
|
|
198
|
+
export declare function decodeSearchKey(required: {
|
|
199
|
+
readonly encoded: string;
|
|
200
|
+
}): SearchDocumentKey | undefined;
|
|
201
|
+
/**
|
|
202
|
+
* The backend index name for one install, site, provider and generation. Derived from the install
|
|
203
|
+
* id (never the site slug) so two installs sharing a vendor account cannot collide, and the
|
|
204
|
+
* generation suffix is what makes a provider switch or full rebuild an atomic pointer flip.
|
|
205
|
+
* The identity is a SHA-256 digest (160 bits kept) of the COMPLETE, unambiguously encoded
|
|
206
|
+
* (installId, siteId, providerId) tuple: a lossy slug (`site-a` vs `site_a`, two long ids sharing a
|
|
207
|
+
* prefix) would give two sites one index, and dropping one site's generation would delete the
|
|
208
|
+
* other's. The generation stays a distinct, readable suffix, never truncated; the provider prefix is
|
|
209
|
+
* only a readable label.
|
|
210
|
+
* Output grammar `[a-z0-9_]`, at most 120 chars (fits Meilisearch and Algolia index-name rules).
|
|
211
|
+
* @complexity O(input length).
|
|
212
|
+
*/
|
|
213
|
+
export declare function searchIndexName(required: {
|
|
214
|
+
readonly installId: string;
|
|
215
|
+
readonly siteId: string;
|
|
216
|
+
readonly providerId: string;
|
|
217
|
+
readonly generation: number;
|
|
218
|
+
}): string;
|
|
219
|
+
//# sourceMappingURL=contract.d.ts.map
|
package/dist/contract.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { sha256Hex } from './digest.js';
|
|
2
|
+
/** The contract name used by the provider registry and plugin contributions. */
|
|
3
|
+
export const SEARCH_CONTRACT = 'search';
|
|
4
|
+
/** The id the owner's in-process provider registers under; also the per-site default. */
|
|
5
|
+
export const BUILTIN_SEARCH_PROVIDER_ID = 'builtin';
|
|
6
|
+
const SEP = '/';
|
|
7
|
+
/**
|
|
8
|
+
* The single string form of a key, for maps, state rows and vendor document ids. Each part is
|
|
9
|
+
* `encodeURIComponent`-ed so a `/` inside a part can never forge another key.
|
|
10
|
+
* @complexity O(total key length).
|
|
11
|
+
*/
|
|
12
|
+
export function encodeSearchKey(required) {
|
|
13
|
+
const { siteId, workspaceId, collection, documentId } = required.key;
|
|
14
|
+
return [siteId, workspaceId, collection, documentId].map(part => encodeURIComponent(part)).join(SEP);
|
|
15
|
+
}
|
|
16
|
+
/** Inverse of {@link encodeSearchKey}; `undefined` for anything that is not exactly four parts. */
|
|
17
|
+
export function decodeSearchKey(required) {
|
|
18
|
+
const parts = required.encoded.split(SEP);
|
|
19
|
+
if (parts.length !== 4)
|
|
20
|
+
return undefined;
|
|
21
|
+
try {
|
|
22
|
+
const [siteId, workspaceId, collection, documentId] = parts.map(part => decodeURIComponent(part));
|
|
23
|
+
return { siteId, workspaceId, collection, documentId };
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The backend index name for one install, site, provider and generation. Derived from the install
|
|
31
|
+
* id (never the site slug) so two installs sharing a vendor account cannot collide, and the
|
|
32
|
+
* generation suffix is what makes a provider switch or full rebuild an atomic pointer flip.
|
|
33
|
+
* The identity is a SHA-256 digest (160 bits kept) of the COMPLETE, unambiguously encoded
|
|
34
|
+
* (installId, siteId, providerId) tuple: a lossy slug (`site-a` vs `site_a`, two long ids sharing a
|
|
35
|
+
* prefix) would give two sites one index, and dropping one site's generation would delete the
|
|
36
|
+
* other's. The generation stays a distinct, readable suffix, never truncated; the provider prefix is
|
|
37
|
+
* only a readable label.
|
|
38
|
+
* Output grammar `[a-z0-9_]`, at most 120 chars (fits Meilisearch and Algolia index-name rules).
|
|
39
|
+
* @complexity O(input length).
|
|
40
|
+
*/
|
|
41
|
+
export function searchIndexName(required) {
|
|
42
|
+
const label = required.providerId.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '').slice(0, 24) || 'x';
|
|
43
|
+
const identity = sha256Hex({ text: JSON.stringify([required.installId, required.siteId, required.providerId]) }).slice(0, 40);
|
|
44
|
+
return `js_${label}_${identity}_g${required.generation}`;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=contract.js.map
|