Skip to content

Commit 90f9f96

Browse files
committed
Export createKnowledgePlane for out-of-band capture and search
The KnowledgePlane type was exported but not its constructor, and the exports map only allows ".", "./migrations", and "./config" — so there was no way in. That left mountKnowledgeEngine as the only way to obtain a plane, and it requires a Hono app and mounts three HTTP routes as a side effect. Any caller wanting to capture or search outside a request had nowhere to go: CLI seeders, batch ingesters, tests that exercise capture without standing up an app. Odd given knowledge.ts advertises the plane as the no-HTTP-hop path ("Wraps the capture and hybrid-search services directly") — you could only reach it by going through the thing that adds HTTP. Exports createKnowledgePlane, KnowledgeError, and the param types. The README now shows both the in-process case (plane returned from mountKnowledgeEngine) and the standalone case, and notes that the standalone path bypasses requireGrant — so a caller acting for a user has to check the capability itself. Per-document visibility is not a substitute for that. Refs CL-4508 Claude-Session: https://claude.ai/code/session_017GTgGzn5xAwvkU2GAPAHpF
1 parent 8d29d5f commit 90f9f96

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { loadKnowledgeConfig } from "@corbits/knowledge-engine/config";
4141
// `app` is your Interchange createApp (Hono<TenantEnv>). Pass the same grant
4242
// store + condition registry you give createApp/createRequireGrant.
4343
mountKnowledgeEngine(app, {
44-
config: loadKnowledgeConfig(), // or build the object yourself
44+
config: loadKnowledgeConfig(), // or build the object yourself
4545
grants: { grantStore, conditionRegistry },
4646
});
4747
```
@@ -51,6 +51,51 @@ That mounts `POST /api/knowledge/capture`, `POST /api/knowledge/search`, and
5151
`requireGrant("knowledge", <action>)`. Clients never send tenant or principal —
5252
identity is the context principal.
5353

54+
### Capturing and searching outside a request
55+
56+
`mountKnowledgeEngine` returns the `KnowledgePlane` it built, and the plane takes
57+
identity as data — so an in-process caller passes `{tenantId, principalId}`
58+
explicitly rather than faking a request context:
59+
60+
```ts
61+
const { knowledge } = mountKnowledgeEngine(app, { config, grants });
62+
63+
await knowledge.search({ tenantId, principalId, query: "…", k: 6 });
64+
```
65+
66+
For a CLI seeder, a batch ingester, or a test with no app at all, construct a
67+
plane directly:
68+
69+
```ts
70+
import {
71+
createKnowledgePlane,
72+
loadKnowledgeConfig,
73+
} from "@corbits/knowledge-engine";
74+
75+
const knowledge = createKnowledgePlane(loadKnowledgeConfig());
76+
await knowledge.capture({ tenantId, principalId, title, text });
77+
await knowledge.close();
78+
```
79+
80+
Note that this path bypasses the `requireGrant` route guard, since there is no
81+
request. If the caller is acting for a user rather than as an operator, check the
82+
capability yourself — the per-document visibility the engine applies is not a
83+
substitute for "may this principal search at all":
84+
85+
```ts
86+
import { authorize } from "@intx/authz";
87+
88+
const decision = await authorize(
89+
grantStore,
90+
principalId,
91+
tenantId,
92+
"knowledge",
93+
"search",
94+
conditionRegistry,
95+
);
96+
if (decision.effect !== "allow") throw new Error("not permitted");
97+
```
98+
5499
Apply the knowledge/vector schema once (idempotent):
55100

56101
```ts

‎src/index.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,20 @@ export type { KnowledgeConfig } from "./mount-config.ts";
2323
export { loadKnowledgeConfig } from "./mount-config.ts";
2424
export type { EngineConfig } from "./config.ts";
2525
// Knowledge plane + capture log
26-
export type { KnowledgePlane } from "./knowledge.ts";
26+
//
27+
// `createKnowledgePlane` is exported so a host can capture or search outside a
28+
// request — a CLI seeder, a batch ingester, or a test — without standing up a
29+
// Hono app just to get a plane. Callers acting on behalf of a user are
30+
// responsible for the capability check `requireGrant` would have performed; see
31+
// the README.
32+
export { createKnowledgePlane } from "./knowledge.ts";
33+
export type {
34+
KnowledgeCaptureParams,
35+
KnowledgeIdentity,
36+
KnowledgePlane,
37+
KnowledgeSearchParams,
38+
} from "./knowledge.ts";
39+
export { KnowledgeError } from "./knowledge.ts";
2740
export { CaptureLog, type CaptureEvent } from "./capture-log.ts";
2841
// Migrations
2942
export { runKnowledgeMigrations } from "./migrations.ts";

0 commit comments

Comments
 (0)