@agentproto/adapter-knowledge-files 0.2.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 +202 -0
- package/README.md +79 -0
- package/dist/index.d.ts +297 -0
- package/dist/index.mjs +646 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# @agentproto/adapter-knowledge-files
|
|
2
|
+
|
|
3
|
+
The pure, zero-runtime **files** backing for the
|
|
4
|
+
[`@agentproto/knowledge-engine`](../../packages/knowledge-engine)
|
|
5
|
+
`IKnowledgeProvider` contract.
|
|
6
|
+
|
|
7
|
+
Knowledge retrieval from local workspace files via an **in-process BM25
|
|
8
|
+
index** — no embeddings, no vector store, no network, no API key. It works the
|
|
9
|
+
instant a workspace has files to index, which makes it the natural default
|
|
10
|
+
corpus backing where AIP-10 entries already live as markdown: the entries
|
|
11
|
+
_are_ the index, with no separate store to keep in sync.
|
|
12
|
+
|
|
13
|
+
## What's in the box
|
|
14
|
+
|
|
15
|
+
| Export | Role |
|
|
16
|
+
| --- | --- |
|
|
17
|
+
| `FilesKnowledgeAdapter` | The `IKnowledgeProvider` implementation. Consumes an injected `FsPort`; knows nothing about `node:fs` or `process.env`. |
|
|
18
|
+
| `LocalFs` | A `node:fs`-backed `FsPort` (from `@agentproto/corpus`) so the adapter runs standalone. Paths are workspace-relative and can't escape the root. |
|
|
19
|
+
| `createStandaloneFilesAdapter()` | Wires `LocalFs` + the typed env into a ready-to-use adapter. |
|
|
20
|
+
| BM25 (`tokenize`, `buildIndex`, `buildIndexYielding`, `score`) | The dependency-free ranking core. Confined to this package. |
|
|
21
|
+
| provider-kit family (`KNOWLEDGE_FILES_CATALOG`, `makeKnowledgeFilesResolver`, `resolveKnowledgeBackend`) | Registers the `files` backend under the `@agentproto/adapter-knowledge-*` discovery convention. |
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
Standalone, driven by env:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { createStandaloneFilesAdapter } from "@agentproto/adapter-knowledge-files"
|
|
29
|
+
|
|
30
|
+
const kb = createStandaloneFilesAdapter()
|
|
31
|
+
const { hits } = await kb.query({ query: "usage based pricing", topK: 5 })
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
With a host-provided `FsPort` (e.g. a studio guild workspace):
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { FilesKnowledgeAdapter } from "@agentproto/adapter-knowledge-files"
|
|
38
|
+
|
|
39
|
+
const kb = new FilesKnowledgeAdapter({ fs, workspacePath: "knowledge" })
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Through the provider-kit resolver (discovery / catalogs):
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { makeKnowledgeFilesResolver } from "@agentproto/adapter-knowledge-files"
|
|
46
|
+
|
|
47
|
+
const handle = await makeKnowledgeFilesResolver()("files")
|
|
48
|
+
const provider = handle?.provider()
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Environment
|
|
52
|
+
|
|
53
|
+
Read once through the typed `loadFilesKnowledgeEnv()` — the only place these
|
|
54
|
+
names live:
|
|
55
|
+
|
|
56
|
+
| Var | Default | Meaning |
|
|
57
|
+
| --- | --- | --- |
|
|
58
|
+
| `KNOWLEDGE_FILES_ROOT` | `process.cwd()` | Absolute host path the `LocalFs` is rooted at. |
|
|
59
|
+
| `KNOWLEDGE_FILES_PATH` | `knowledge` | Workspace-relative folder walked and indexed (recursively). |
|
|
60
|
+
|
|
61
|
+
## Indexing model
|
|
62
|
+
|
|
63
|
+
- **Indexed extensions:** `.md`, `.markdown`, `.txt`, `.yaml`, `.yml`, `.json`.
|
|
64
|
+
YAML frontmatter is stripped before tokenizing so schema fences don't pollute
|
|
65
|
+
the vocabulary; frontmatter is still surfaced as each hit's `metadata` (and a
|
|
66
|
+
`slug` mirrors to `entrySlug` for corpus hydration).
|
|
67
|
+
- **Ranking:** standard BM25 (k1=1.5, b=0.75) with `+1`-smoothed IDF.
|
|
68
|
+
- **Cache:** the index is built lazily on first query and cached permanently —
|
|
69
|
+
it's rebuilt only when the adapter `invalidate()`s (via `ingest`/`delete`) or
|
|
70
|
+
a fresh instance is created. Hosts wanting second-precision freshness on
|
|
71
|
+
external edits can wire fs-watch into a new instance.
|
|
72
|
+
|
|
73
|
+
## Capabilities
|
|
74
|
+
|
|
75
|
+
`vectorSearch: false`, `hybridSearch: false`, `citations: true` — BM25 returns
|
|
76
|
+
ranked hits with a file path + byte span, usable as citations even without a
|
|
77
|
+
vector concept.
|
|
78
|
+
|
|
79
|
+
Apache-2.0.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { FsPort, FsStat, FsLockHandle } from '@agentproto/corpus';
|
|
2
|
+
import { KnowledgeCapabilities, IKnowledgeProvider, KnowledgeIngestInput, KnowledgeSource, KnowledgeQuery, KnowledgeQueryResult, ListSourcesFilter } from '@agentproto/knowledge-engine';
|
|
3
|
+
import { AdapterCatalog, AdapterHandle, AdapterResolver } from '@agentproto/provider-kit';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* FilesKnowledgeAdapter — the no-runtime, no-embedding KB engine.
|
|
7
|
+
*
|
|
8
|
+
* Indexes workspace files in process with BM25. Zero setup: works the
|
|
9
|
+
* instant a guild has a system workspace, no E2B sandbox, no API key,
|
|
10
|
+
* no cold start. Best suited as the DEFAULT corpus backing where the
|
|
11
|
+
* AIP-10 entries already live as markdown files — the entries ARE the
|
|
12
|
+
* index, no separate vector store to keep in sync.
|
|
13
|
+
*
|
|
14
|
+
* Index lifecycle: built lazily on first query, cached for 30s, also
|
|
15
|
+
* invalidated explicitly by `ingest()`. External edits (a user typing
|
|
16
|
+
* in the workspace UI) become visible on the next rebuild — for
|
|
17
|
+
* second-precision freshness, hosts can wire fs-watch into the
|
|
18
|
+
* `invalidate()` method.
|
|
19
|
+
*
|
|
20
|
+
* Lifted VERBATIM from the studio integration package
|
|
21
|
+
* (`packages/integration/knowledge/src/providers/files/adapter.ts`) — the
|
|
22
|
+
* class already carried no `@guilde/@simone/@agstudio` edge (it consumes an
|
|
23
|
+
* injected `FsPort` and the vendor-neutral `IKnowledgeProvider` contract), so
|
|
24
|
+
* it re-homes as-is. The ONLY change is the two import paths: the contract +
|
|
25
|
+
* data types now come from `@agentproto/knowledge-engine` (was
|
|
26
|
+
* `../base-knowledge.provider` / `../../types/knowledge.types`). The guild-side
|
|
27
|
+
* `KnowledgeEngineDescriptor` (studio `descriptor.ts`) is NOT lifted — it
|
|
28
|
+
* belonged to the guild resolution layer; this package registers as a
|
|
29
|
+
* provider-kit family instead (see `handle.ts`).
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
declare const FILES_ENGINE_ID: "files";
|
|
33
|
+
declare const FILES_CAPABILITIES: KnowledgeCapabilities;
|
|
34
|
+
interface FilesAdapterConfig {
|
|
35
|
+
/** Workspace-relative root the engine indexes (walks recursively). */
|
|
36
|
+
readonly workspacePath: string;
|
|
37
|
+
}
|
|
38
|
+
interface FilesKnowledgeAdapterOptions extends FilesAdapterConfig {
|
|
39
|
+
/** FsPort scoped to the workspace the engine lives in. */
|
|
40
|
+
readonly fs: FsPort;
|
|
41
|
+
}
|
|
42
|
+
declare class FilesKnowledgeAdapter implements IKnowledgeProvider {
|
|
43
|
+
readonly id: "files";
|
|
44
|
+
readonly capabilities: KnowledgeCapabilities;
|
|
45
|
+
private readonly fs;
|
|
46
|
+
private readonly workspacePath;
|
|
47
|
+
private cachedIndex;
|
|
48
|
+
private cachedAt;
|
|
49
|
+
private buildInFlight;
|
|
50
|
+
constructor(opts: FilesKnowledgeAdapterOptions);
|
|
51
|
+
ingest(input: KnowledgeIngestInput): Promise<KnowledgeSource>;
|
|
52
|
+
query(q: KnowledgeQuery): Promise<KnowledgeQueryResult>;
|
|
53
|
+
listSources(filter?: ListSourcesFilter): Promise<readonly KnowledgeSource[]>;
|
|
54
|
+
getSource(id: string): Promise<KnowledgeSource | null>;
|
|
55
|
+
deleteSource(id: string): Promise<void>;
|
|
56
|
+
healthCheck(): Promise<boolean>;
|
|
57
|
+
dispose(): Promise<void>;
|
|
58
|
+
/** Drop the cached BM25 index — next query rebuilds. */
|
|
59
|
+
private invalidate;
|
|
60
|
+
private ensureIndex;
|
|
61
|
+
private _buildIndex;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* LocalFs — a `FsPort` (from `@agentproto/corpus`) backed by node:fs.
|
|
66
|
+
*
|
|
67
|
+
* The studio files provider never shipped its own `FsPort`: the guild host
|
|
68
|
+
* resolved one (`resolveWorkspaceFs`) and injected it. agentproto-side that
|
|
69
|
+
* host is gone (the guild registry stays studio-side per the Ph4 plan), so
|
|
70
|
+
* this package ships the minimal local backing that lets the adapter run
|
|
71
|
+
* standalone — the same role the code-brain adapter's own gbrain client plays.
|
|
72
|
+
*
|
|
73
|
+
* Mirrors `@agentproto/corpus-cli`'s `NodeFsAdapter` (that class lives in a
|
|
74
|
+
* `-cli` package; a library adapter must not take a `-cli` edge, so the small
|
|
75
|
+
* node:fs surface the files engine needs is re-implemented here). Paths are
|
|
76
|
+
* workspace-relative and resolved under `root`; `resolve()` refuses to climb
|
|
77
|
+
* out of it. `walk()` returns workspace-relative paths (readable back through
|
|
78
|
+
* `readFile`) and skips hidden segments, exactly as `FsPort` specifies. `lock`
|
|
79
|
+
* is a single-process no-op — this backing is local CLI, which the port
|
|
80
|
+
* contract explicitly permits.
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
interface LocalFsOptions {
|
|
84
|
+
/**
|
|
85
|
+
* Absolute path to the workspace root. Every `FsPort` path is resolved
|
|
86
|
+
* relative to this — the adapter never sees a host filesystem path.
|
|
87
|
+
*/
|
|
88
|
+
readonly root: string;
|
|
89
|
+
}
|
|
90
|
+
declare class LocalFs implements FsPort {
|
|
91
|
+
private readonly root;
|
|
92
|
+
constructor(opts: LocalFsOptions);
|
|
93
|
+
exists(p: string): Promise<boolean>;
|
|
94
|
+
readFile(p: string): Promise<string>;
|
|
95
|
+
/**
|
|
96
|
+
* Atomic write: write to `${target}.tmp-{nonce}`, then rename. Rename is
|
|
97
|
+
* atomic on the same filesystem. Creates parent dirs as needed.
|
|
98
|
+
*/
|
|
99
|
+
writeFile(p: string, content: string): Promise<void>;
|
|
100
|
+
appendFile(p: string, content: string): Promise<void>;
|
|
101
|
+
readdir(p: string): Promise<readonly string[]>;
|
|
102
|
+
walk(p: string): Promise<readonly string[]>;
|
|
103
|
+
stat(p: string): Promise<FsStat | null>;
|
|
104
|
+
/**
|
|
105
|
+
* Advisory lock — a single-process no-op. The files engine never calls
|
|
106
|
+
* `lock()` (it only reads/writes whole source files), and this backing is
|
|
107
|
+
* local CLI, for which the `FsPort` contract permits a no-op lock.
|
|
108
|
+
*/
|
|
109
|
+
lock(_p: string): Promise<FsLockHandle>;
|
|
110
|
+
/**
|
|
111
|
+
* Resolve a workspace-relative path to an absolute host path, refusing to
|
|
112
|
+
* climb out of the workspace root (defense against `../../etc/passwd`-style
|
|
113
|
+
* abuse). The files engine's paths must stay inside the workspace.
|
|
114
|
+
*/
|
|
115
|
+
private resolve;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Standalone wiring for {@link FilesKnowledgeAdapter}.
|
|
120
|
+
*
|
|
121
|
+
* The adapter class itself is backing-agnostic: it consumes an injected
|
|
122
|
+
* `FsPort` and knows nothing about `node:fs` or `process.env`. This module
|
|
123
|
+
* is the ONE place that binds it to the local filesystem — it reads the
|
|
124
|
+
* typed `KNOWLEDGE_FILES_*` env (via {@link loadFilesKnowledgeEnv}) and roots
|
|
125
|
+
* a {@link LocalFs} at it, exactly the role the code-brain adapter's
|
|
126
|
+
* `gbrainLocalProvider` factory plays for its backend. Hosts that already
|
|
127
|
+
* hold an `FsPort` (e.g. a studio guild host) should construct
|
|
128
|
+
* `FilesKnowledgeAdapter` directly and skip this factory.
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Build a {@link FilesKnowledgeAdapter} backed by the local filesystem,
|
|
133
|
+
* rooted and scoped from the ambient `KNOWLEDGE_FILES_*` env. Each call
|
|
134
|
+
* returns a fresh adapter (and thus a fresh, empty BM25 cache) — callers
|
|
135
|
+
* that want a shared warm index should hold onto the instance.
|
|
136
|
+
*/
|
|
137
|
+
declare function createStandaloneFilesAdapter(): FilesKnowledgeAdapter;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Typed environment module for the files knowledge adapter.
|
|
141
|
+
*
|
|
142
|
+
* Every `process.env` read in this package flows through here — no raw
|
|
143
|
+
* `process.env.X` at a call site (mirrors the code-brain adapter's `env.ts`).
|
|
144
|
+
* Each loader parses the ambient env into a typed, defaulted config once, so
|
|
145
|
+
* the rest of the adapter is a pure function of its inputs. This is the ONE
|
|
146
|
+
* place the files-engine env names (`KNOWLEDGE_FILES_*`) live.
|
|
147
|
+
*/
|
|
148
|
+
/** Config for the standalone files backend — a local root + a folder to index. */
|
|
149
|
+
interface FilesKnowledgeEnv {
|
|
150
|
+
/**
|
|
151
|
+
* Absolute host path to the workspace root the {@link LocalFs} is rooted at.
|
|
152
|
+
* Default: the process cwd.
|
|
153
|
+
*/
|
|
154
|
+
readonly root: string;
|
|
155
|
+
/**
|
|
156
|
+
* Workspace-relative folder the engine walks and indexes (recursively).
|
|
157
|
+
* Default `"knowledge"`.
|
|
158
|
+
*/
|
|
159
|
+
readonly workspacePath: string;
|
|
160
|
+
}
|
|
161
|
+
/** Load {@link FilesKnowledgeEnv} from `process.env` with defaults. */
|
|
162
|
+
declare function loadFilesKnowledgeEnv(): FilesKnowledgeEnv;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* In-process BM25 — a tiny, dependency-free implementation tuned for
|
|
166
|
+
* corpus-sized data (hundreds to low thousands of markdown files).
|
|
167
|
+
*
|
|
168
|
+
* Tokenizer: lowercase, split on non-alphanum, length 2-32, drop a small
|
|
169
|
+
* English stopword list. Frontmatter is stripped before tokenization so
|
|
170
|
+
* fence values like `schema: knowledge.entry/v1` don't pollute the
|
|
171
|
+
* vocabulary.
|
|
172
|
+
*
|
|
173
|
+
* Scoring: standard BM25 with k1=1.5, b=0.75 — Lucene defaults. IDF
|
|
174
|
+
* uses the "BM25+1" smoothing so terms appearing in every doc still
|
|
175
|
+
* contribute a tiny positive score (avoids the negative-IDF foot-gun
|
|
176
|
+
* for high-frequency terms).
|
|
177
|
+
*/
|
|
178
|
+
declare function tokenize(text: string): string[];
|
|
179
|
+
interface Bm25Doc {
|
|
180
|
+
/** Stable id — typically the workspace-relative path. */
|
|
181
|
+
readonly id: string;
|
|
182
|
+
/** Original text (kept for snippet extraction at query time). */
|
|
183
|
+
readonly text: string;
|
|
184
|
+
/** Token count — used for length-normalization in BM25. */
|
|
185
|
+
readonly length: number;
|
|
186
|
+
/** Term frequency table for this doc. */
|
|
187
|
+
readonly tf: ReadonlyMap<string, number>;
|
|
188
|
+
}
|
|
189
|
+
interface Bm25Index {
|
|
190
|
+
readonly docs: readonly Bm25Doc[];
|
|
191
|
+
/** Number of docs containing each term — used in IDF. */
|
|
192
|
+
readonly df: ReadonlyMap<string, number>;
|
|
193
|
+
/** Average document length across the index, in tokens. */
|
|
194
|
+
readonly avgDocLength: number;
|
|
195
|
+
}
|
|
196
|
+
interface BuildDocInput {
|
|
197
|
+
readonly id: string;
|
|
198
|
+
readonly content: string;
|
|
199
|
+
}
|
|
200
|
+
declare function buildIndex(inputs: readonly BuildDocInput[]): Bm25Index;
|
|
201
|
+
/**
|
|
202
|
+
* Async, event-loop-friendly variant of {@link buildIndex}. Identical output,
|
|
203
|
+
* but yields to the macrotask queue every `yieldEvery` docs.
|
|
204
|
+
*
|
|
205
|
+
* Why this matters: the caller (`FilesKnowledgeAdapter._buildIndex`) reads docs
|
|
206
|
+
* from an in-memory/cached fs, so the awaited reads resolve as MICROTASKS and
|
|
207
|
+
* never hand control back to the event loop. Without an explicit yield, the
|
|
208
|
+
* tokenization of every doc runs as one uninterruptible synchronous chain —
|
|
209
|
+
* which (a) freezes every concurrent recall on this single-process, multi-tenant
|
|
210
|
+
* instance for the full build and (b) defeats `ensureIndex`'s non-blocking
|
|
211
|
+
* `setTimeout` deadline (the timer can't fire while the loop is blocked). The
|
|
212
|
+
* `setImmediate` yield lets sibling turns progress and the deadline fire during a
|
|
213
|
+
* cold build.
|
|
214
|
+
*/
|
|
215
|
+
declare function buildIndexYielding(inputs: readonly BuildDocInput[], yieldEvery?: number): Promise<Bm25Index>;
|
|
216
|
+
interface ScoredHit {
|
|
217
|
+
readonly doc: Bm25Doc;
|
|
218
|
+
readonly score: number;
|
|
219
|
+
/** Char range in `doc.text` around the first matching token. */
|
|
220
|
+
readonly snippet?: {
|
|
221
|
+
readonly start: number;
|
|
222
|
+
readonly end: number;
|
|
223
|
+
readonly text: string;
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Score every doc against the query tokens, return top-k.
|
|
228
|
+
* Docs with score <= 0 are dropped (no terms matched).
|
|
229
|
+
*/
|
|
230
|
+
declare function score(index: Bm25Index, queryTokens: readonly string[], opts?: {
|
|
231
|
+
readonly topK?: number;
|
|
232
|
+
readonly minScore?: number;
|
|
233
|
+
}): ScoredHit[];
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Family key used for the knowledge backend creds/ledger store path.
|
|
237
|
+
*
|
|
238
|
+
* The retrieval sibling of the `code-brain` family: the provider-kit
|
|
239
|
+
* discovery convention (`@agentproto/adapter-knowledge-*`) points every
|
|
240
|
+
* knowledge backend at its providing package.
|
|
241
|
+
*/
|
|
242
|
+
declare const KNOWLEDGE_FAMILY: "knowledge";
|
|
243
|
+
/** The single backend slug this adapter provides. */
|
|
244
|
+
declare const FILES_BM25_SLUG: "files";
|
|
245
|
+
/**
|
|
246
|
+
* Static catalog of knowledge backends shipped by this adapter. The `files`
|
|
247
|
+
* slug resolves from THIS package (`@agentproto/adapter-knowledge-files`) —
|
|
248
|
+
* an in-process BM25 index over local workspace files, no embeddings, no
|
|
249
|
+
* network, no credential.
|
|
250
|
+
*/
|
|
251
|
+
declare const KNOWLEDGE_FILES_CATALOG: AdapterCatalog;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* provider-kit family wiring for the files knowledge backend.
|
|
255
|
+
*
|
|
256
|
+
* The studio home-grown `createEngineRegistry`-based knowledge registry is
|
|
257
|
+
* NOT ported (it was the guild resolution layer, which stays studio-side per
|
|
258
|
+
* the Ph4 plan). Instead the files backend registers as a provider-kit family
|
|
259
|
+
* (`TInfo = KnowledgeFilesInfo`, `THandle = KnowledgeHandle extends
|
|
260
|
+
* AdapterHandle`), reusing the exact consumer shape the code-brain adapter
|
|
261
|
+
* established — catalog + resolver + a safe `info()` descriptor. Discovery
|
|
262
|
+
* flows through the kit's `@agentproto/adapter-knowledge-*` naming convention.
|
|
263
|
+
*/
|
|
264
|
+
|
|
265
|
+
/** Safe descriptor for a knowledge backend — NEVER carries a secret. */
|
|
266
|
+
interface KnowledgeFilesInfo {
|
|
267
|
+
readonly slug: string;
|
|
268
|
+
/** The retrieval engine this backend uses. */
|
|
269
|
+
readonly engine: "bm25";
|
|
270
|
+
readonly capabilities: {
|
|
271
|
+
/** True when the backend performs dense/vector retrieval. */
|
|
272
|
+
readonly vectorSearch: boolean;
|
|
273
|
+
/** True when the backend needs a credential to authenticate. */
|
|
274
|
+
readonly needsCreds: boolean;
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/** A resolved knowledge backend handle. */
|
|
278
|
+
interface KnowledgeHandle extends AdapterHandle {
|
|
279
|
+
/** Safe descriptor — NEVER a secret. */
|
|
280
|
+
info(): KnowledgeFilesInfo;
|
|
281
|
+
/** Build the live {@link IKnowledgeProvider}. */
|
|
282
|
+
provider(): IKnowledgeProvider;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Resolve a catalog slug to a concrete knowledge handle. Throws for unknown
|
|
286
|
+
* slugs so the kit's resolver wrapper folds the miss to `null`. `provider()`
|
|
287
|
+
* is lazy and caches a single standalone adapter — repeated calls share one
|
|
288
|
+
* warm BM25 index rather than each cold-building their own.
|
|
289
|
+
*/
|
|
290
|
+
declare function resolveKnowledgeBackend(slug: string): KnowledgeHandle;
|
|
291
|
+
/**
|
|
292
|
+
* Build the knowledge resolver: resolves a catalog slug to a handle,
|
|
293
|
+
* returning `null` when the slug is unknown (via the kit's wrapper).
|
|
294
|
+
*/
|
|
295
|
+
declare function makeKnowledgeFilesResolver(): AdapterResolver<KnowledgeHandle>;
|
|
296
|
+
|
|
297
|
+
export { type Bm25Doc, type Bm25Index, type BuildDocInput, FILES_BM25_SLUG, FILES_CAPABILITIES, FILES_ENGINE_ID, type FilesAdapterConfig, FilesKnowledgeAdapter, type FilesKnowledgeAdapterOptions, type FilesKnowledgeEnv, KNOWLEDGE_FAMILY, KNOWLEDGE_FILES_CATALOG, type KnowledgeFilesInfo, type KnowledgeHandle, LocalFs, type LocalFsOptions, type ScoredHit, buildIndex, buildIndexYielding, createStandaloneFilesAdapter, loadFilesKnowledgeEnv, makeKnowledgeFilesResolver, resolveKnowledgeBackend, score, tokenize };
|