@promptev/context-engine 0.0.2 → 0.0.3

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.
@@ -0,0 +1,204 @@
1
+ import * as node_http from 'node:http';
2
+
3
+ /**
4
+ * Sentinels distinguishing omitted arguments from real values, including null.
5
+ *
6
+ * UNSET: "argument omitted" vs null (e.g. updateDocument acl=null means unrestricted).
7
+ * TRUSTED: trusted caller, ACL filtering disabled. Truthy on purpose so
8
+ * `if (principals)` does not treat a trusted caller as anonymous.
9
+ */
10
+ declare const UNSET: unique symbol;
11
+ type Unset = typeof UNSET;
12
+ declare const TRUSTED: unique symbol;
13
+ declare const UNSCOPED: unique symbol;
14
+ type Trusted = typeof TRUSTED;
15
+ type Principals = string[] | null | typeof TRUSTED | undefined;
16
+ declare function resolvePrincipals(value: Principals, method: string): string[] | null;
17
+
18
+ /**
19
+ * `search_knowledge_base` — ONE tool with actions, as a plain library call.
20
+ *
21
+ * The four knowledge tools became sub-actions of one because that is better
22
+ * for the model calling them: one description carries the decision rules once,
23
+ * `discover` says what exists before it guesses, and there is one name to
24
+ * route through. Nothing here knows what MCP is.
25
+ *
26
+ * **Why it lives here and not in `mcp.ts`.** MCP is one way to reach the tool,
27
+ * not the only one. `engine.searchKnowledgeBase(...)` calls this directly;
28
+ * `mcp.ts` registers a tool that resolves the caller's identity and scope and
29
+ * then calls exactly the same function. Two doors, one implementation, so they
30
+ * cannot drift apart.
31
+ *
32
+ * **Identity and scope are arguments, never defaults.** This module invents
33
+ * neither a caller nor a ceiling. Both come from the program that mounted the
34
+ * tool — a model may ask for any source id it likes, and a tool that believed
35
+ * it would let one customer read another's files.
36
+ */
37
+
38
+ declare const KNOWLEDGE_ACTIONS: readonly ["discover", "search", "get_doc", "list", "query_meta", "compute", "get_chunks", "get_docs", "map_reduce", "traverse", "find_related", "get_neighbors", "community_summary"];
39
+ type KnowledgeAction = (typeof KNOWLEDGE_ACTIONS)[number];
40
+ declare const KNOWLEDGE_TOOL_DESCRIPTION: string;
41
+ /**
42
+ * The tool as data: name, description and input schema as JSON Schema.
43
+ *
44
+ * Exported so a host wiring this into its own agent loop, or into a protocol
45
+ * this library does not speak, never hand-writes the schema — that would be a
46
+ * third copy of the action list, and a third place for it to fall behind.
47
+ *
48
+ * `readOnlyHint` is false on purpose. Most actions only read, but `compute`
49
+ * runs generated code, and a host deciding whether to auto-approve a call must
50
+ * not read "knowledge base" and assume a reader.
51
+ */
52
+ declare function knowledgeToolDefinition(): {
53
+ name: string;
54
+ description: string;
55
+ input_schema: Record<string, unknown>;
56
+ annotations: Record<string, boolean>;
57
+ };
58
+ /**
59
+ * The documents a mounted tool may ever reach.
60
+ *
61
+ * Deliberately only the nouns the engine already knows: source ids, and
62
+ * optionally some document ids within them. Not project, workspace, tenant or
63
+ * agent — those are a host's words, and baking one in would make every other
64
+ * host translate its word into ours.
65
+ */
66
+ type Scope = {
67
+ sourceIds: string[] | null;
68
+ documentIds: string[] | null;
69
+ };
70
+ type ScopeInput = typeof UNSCOPED | string[] | Partial<Scope> | null | undefined;
71
+ /**
72
+ * Normalise what a host supplies into a `Scope`. `UNSCOPED` means no ceiling.
73
+ * `null`/omitted throws: it is what a host that forgot looks like, and
74
+ * defaulting it to "no ceiling" is the corpus-wide search this exists to stop.
75
+ */
76
+ declare function resolveScope(value: ScopeInput): Scope;
77
+ /**
78
+ * Intersect what the caller asked for with what the host allows.
79
+ *
80
+ * Narrow, never widen. `null` requested means the whole ceiling, never the
81
+ * whole corpus. An id outside the ceiling is DROPPED rather than refused,
82
+ * because an error would tell the caller which ids are real.
83
+ *
84
+ * The return value distinguishes two things that must never be confused: an
85
+ * array (possibly EMPTY — the caller asked only for things it may not have, so
86
+ * the answer is nothing) from `null` (no ceiling and no narrowing, no filter).
87
+ */
88
+ declare function narrowToCeiling(requested: string[] | null | undefined, ceiling: string[] | null): string[] | null;
89
+ /**
90
+ * A host's own compute. Running generated code is where a host has its own
91
+ * rules about permission, billing and approval, so it can replace ours rather
92
+ * than intercept the action before the tool is reached.
93
+ */
94
+ type KnowledgeComputeFn = (instruction: string, opts: {
95
+ sourceIds: string[] | null;
96
+ documentIds: string[] | null;
97
+ principals: unknown;
98
+ }) => Promise<Record<string, unknown>>;
99
+ type Engine$1 = {
100
+ config: {
101
+ llm?: unknown;
102
+ enableCodeExecution?: boolean;
103
+ graph?: {
104
+ enabled?: boolean;
105
+ };
106
+ };
107
+ search: (query: string, opts?: Record<string, unknown>) => Promise<{
108
+ hits: unknown[];
109
+ usage?: unknown;
110
+ }>;
111
+ getDocument: (id: string, opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
112
+ listDocuments: (opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
113
+ queryStructured?: (question: string, opts?: Record<string, unknown>) => Promise<unknown>;
114
+ compute?: (instruction: string, opts?: Record<string, unknown>) => Promise<unknown>;
115
+ ensurePool?: () => Promise<unknown>;
116
+ embedder?: unknown;
117
+ getChunks?: (id: string, opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
118
+ getDocuments?: (ids: string[], opts?: Record<string, unknown>) => Promise<Array<Record<string, unknown>>>;
119
+ mapReduce?: (instruction: string, opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
120
+ };
121
+ type KnowledgeToolArgs = {
122
+ action: string;
123
+ principals: unknown;
124
+ scope: ScopeInput;
125
+ query?: string | null;
126
+ document_id?: string | null;
127
+ source_ids?: string[] | null;
128
+ document_ids?: string[] | null;
129
+ entity?: string | null;
130
+ depth?: number | null;
131
+ category?: string | null;
132
+ label?: string | null;
133
+ entity_type?: string | null;
134
+ top_k?: number | null;
135
+ mode?: string | null;
136
+ limit?: number | null;
137
+ cursor?: unknown;
138
+ /** Overrides the deployment's configured policy for this call only. */
139
+ redaction?: unknown;
140
+ /** Replaces the built-in compute action. */
141
+ compute?: KnowledgeComputeFn | null;
142
+ /** Replaces the built-in map_reduce action — the other expensive one. */
143
+ map_reduce?: ((instruction: string, opts: Record<string, unknown>) => Promise<Record<string, unknown>>) | null;
144
+ start?: number | null;
145
+ end?: number | null;
146
+ max_chars?: number | null;
147
+ };
148
+ /** Run one action of the knowledge tool. */
149
+ declare function callKnowledgeTool(engine: Engine$1, args: KnowledgeToolArgs): Promise<Record<string, unknown>>;
150
+
151
+ type PrincipalsFn = () => unknown | Promise<unknown>;
152
+ /** Zero-argument, sync or async, resolved fresh per execution — the same
153
+ * contract as `PrincipalsFn`. Returns the opaque approval scope (a run id is
154
+ * the expected shape) or `null` for the unscoped legacy path. */
155
+ type ApprovalScopeFn = () => unknown | Promise<unknown>;
156
+
157
+ type Engine = {
158
+ config: {
159
+ llm?: unknown;
160
+ enableCodeExecution?: boolean;
161
+ };
162
+ search: (query: string, opts?: Record<string, unknown>) => Promise<{
163
+ hits: unknown[];
164
+ usage?: unknown;
165
+ }>;
166
+ getDocument: (id: string, opts?: Record<string, unknown>) => Promise<unknown>;
167
+ listDocuments: (opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
168
+ queryStructured?: (question: string, opts?: Record<string, unknown>) => Promise<unknown>;
169
+ compute?: (instruction: string, opts?: Record<string, unknown>) => Promise<unknown>;
170
+ searchTools: (query: string, opts?: Record<string, unknown>) => Promise<unknown[]>;
171
+ executeTool: (name: string, args: Record<string, unknown> | null, opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
172
+ };
173
+ declare function createMcpApp(engine: Engine, opts: {
174
+ principals: PrincipalsFn;
175
+ /**
176
+ * REQUIRED ceiling of source ids (or a `Scope`) this mounted tool may
177
+ * ever reach — resolved fresh per call like `principals` and NEVER a tool
178
+ * argument. A model can ask for any source id it likes, and a tool that
179
+ * believed it would let one caller read another's documents. A host with
180
+ * one shared corpus says so on purpose with `scope: UNSCOPED`.
181
+ */
182
+ scope: ScopeInput | (() => ScopeInput | Promise<ScopeInput>);
183
+ /**
184
+ * Overrides `config.redaction` per call, so a per-project or
185
+ * per-customer policy reaches this tool the way it already reaches
186
+ * `search`. Never a tool argument.
187
+ */
188
+ redaction?: unknown | (() => unknown | Promise<unknown>);
189
+ /**
190
+ * Optionally REPLACES the built-in compute action with the host's own
191
+ * callable. Running generated code is where a host has its own rules
192
+ * about permission, billing and approval; supplying one here means it
193
+ * does not have to intercept the action before the tool is reached, and
194
+ * makes the action available whatever `enableCodeExecution` says.
195
+ */
196
+ compute?: KnowledgeComputeFn | null;
197
+ /** The same seam for the other expensive action — one LLM call per document. */
198
+ mapReduce?: ((instruction: string, opts: Record<string, unknown>) => Promise<Record<string, unknown>>) | null;
199
+ approvalScope?: ApprovalScopeFn | null;
200
+ }): Promise<((req: node_http.IncomingMessage, res: node_http.ServerResponse) => Promise<void>) & {
201
+ mcp: unknown;
202
+ }>;
203
+
204
+ export { type KnowledgeComputeFn as K, type Principals as P, type ScopeInput as S, type Trusted as T, type Unset as U, KNOWLEDGE_ACTIONS as a, KNOWLEDGE_TOOL_DESCRIPTION as b, type KnowledgeAction as c, type Scope as d, TRUSTED as e, UNSCOPED as f, UNSET as g, callKnowledgeTool as h, createMcpApp as i, resolveScope as j, knowledgeToolDefinition as k, narrowToCeiling as n, resolvePrincipals as r };
@@ -0,0 +1,204 @@
1
+ import * as node_http from 'node:http';
2
+
3
+ /**
4
+ * Sentinels distinguishing omitted arguments from real values, including null.
5
+ *
6
+ * UNSET: "argument omitted" vs null (e.g. updateDocument acl=null means unrestricted).
7
+ * TRUSTED: trusted caller, ACL filtering disabled. Truthy on purpose so
8
+ * `if (principals)` does not treat a trusted caller as anonymous.
9
+ */
10
+ declare const UNSET: unique symbol;
11
+ type Unset = typeof UNSET;
12
+ declare const TRUSTED: unique symbol;
13
+ declare const UNSCOPED: unique symbol;
14
+ type Trusted = typeof TRUSTED;
15
+ type Principals = string[] | null | typeof TRUSTED | undefined;
16
+ declare function resolvePrincipals(value: Principals, method: string): string[] | null;
17
+
18
+ /**
19
+ * `search_knowledge_base` — ONE tool with actions, as a plain library call.
20
+ *
21
+ * The four knowledge tools became sub-actions of one because that is better
22
+ * for the model calling them: one description carries the decision rules once,
23
+ * `discover` says what exists before it guesses, and there is one name to
24
+ * route through. Nothing here knows what MCP is.
25
+ *
26
+ * **Why it lives here and not in `mcp.ts`.** MCP is one way to reach the tool,
27
+ * not the only one. `engine.searchKnowledgeBase(...)` calls this directly;
28
+ * `mcp.ts` registers a tool that resolves the caller's identity and scope and
29
+ * then calls exactly the same function. Two doors, one implementation, so they
30
+ * cannot drift apart.
31
+ *
32
+ * **Identity and scope are arguments, never defaults.** This module invents
33
+ * neither a caller nor a ceiling. Both come from the program that mounted the
34
+ * tool — a model may ask for any source id it likes, and a tool that believed
35
+ * it would let one customer read another's files.
36
+ */
37
+
38
+ declare const KNOWLEDGE_ACTIONS: readonly ["discover", "search", "get_doc", "list", "query_meta", "compute", "get_chunks", "get_docs", "map_reduce", "traverse", "find_related", "get_neighbors", "community_summary"];
39
+ type KnowledgeAction = (typeof KNOWLEDGE_ACTIONS)[number];
40
+ declare const KNOWLEDGE_TOOL_DESCRIPTION: string;
41
+ /**
42
+ * The tool as data: name, description and input schema as JSON Schema.
43
+ *
44
+ * Exported so a host wiring this into its own agent loop, or into a protocol
45
+ * this library does not speak, never hand-writes the schema — that would be a
46
+ * third copy of the action list, and a third place for it to fall behind.
47
+ *
48
+ * `readOnlyHint` is false on purpose. Most actions only read, but `compute`
49
+ * runs generated code, and a host deciding whether to auto-approve a call must
50
+ * not read "knowledge base" and assume a reader.
51
+ */
52
+ declare function knowledgeToolDefinition(): {
53
+ name: string;
54
+ description: string;
55
+ input_schema: Record<string, unknown>;
56
+ annotations: Record<string, boolean>;
57
+ };
58
+ /**
59
+ * The documents a mounted tool may ever reach.
60
+ *
61
+ * Deliberately only the nouns the engine already knows: source ids, and
62
+ * optionally some document ids within them. Not project, workspace, tenant or
63
+ * agent — those are a host's words, and baking one in would make every other
64
+ * host translate its word into ours.
65
+ */
66
+ type Scope = {
67
+ sourceIds: string[] | null;
68
+ documentIds: string[] | null;
69
+ };
70
+ type ScopeInput = typeof UNSCOPED | string[] | Partial<Scope> | null | undefined;
71
+ /**
72
+ * Normalise what a host supplies into a `Scope`. `UNSCOPED` means no ceiling.
73
+ * `null`/omitted throws: it is what a host that forgot looks like, and
74
+ * defaulting it to "no ceiling" is the corpus-wide search this exists to stop.
75
+ */
76
+ declare function resolveScope(value: ScopeInput): Scope;
77
+ /**
78
+ * Intersect what the caller asked for with what the host allows.
79
+ *
80
+ * Narrow, never widen. `null` requested means the whole ceiling, never the
81
+ * whole corpus. An id outside the ceiling is DROPPED rather than refused,
82
+ * because an error would tell the caller which ids are real.
83
+ *
84
+ * The return value distinguishes two things that must never be confused: an
85
+ * array (possibly EMPTY — the caller asked only for things it may not have, so
86
+ * the answer is nothing) from `null` (no ceiling and no narrowing, no filter).
87
+ */
88
+ declare function narrowToCeiling(requested: string[] | null | undefined, ceiling: string[] | null): string[] | null;
89
+ /**
90
+ * A host's own compute. Running generated code is where a host has its own
91
+ * rules about permission, billing and approval, so it can replace ours rather
92
+ * than intercept the action before the tool is reached.
93
+ */
94
+ type KnowledgeComputeFn = (instruction: string, opts: {
95
+ sourceIds: string[] | null;
96
+ documentIds: string[] | null;
97
+ principals: unknown;
98
+ }) => Promise<Record<string, unknown>>;
99
+ type Engine$1 = {
100
+ config: {
101
+ llm?: unknown;
102
+ enableCodeExecution?: boolean;
103
+ graph?: {
104
+ enabled?: boolean;
105
+ };
106
+ };
107
+ search: (query: string, opts?: Record<string, unknown>) => Promise<{
108
+ hits: unknown[];
109
+ usage?: unknown;
110
+ }>;
111
+ getDocument: (id: string, opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
112
+ listDocuments: (opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
113
+ queryStructured?: (question: string, opts?: Record<string, unknown>) => Promise<unknown>;
114
+ compute?: (instruction: string, opts?: Record<string, unknown>) => Promise<unknown>;
115
+ ensurePool?: () => Promise<unknown>;
116
+ embedder?: unknown;
117
+ getChunks?: (id: string, opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
118
+ getDocuments?: (ids: string[], opts?: Record<string, unknown>) => Promise<Array<Record<string, unknown>>>;
119
+ mapReduce?: (instruction: string, opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
120
+ };
121
+ type KnowledgeToolArgs = {
122
+ action: string;
123
+ principals: unknown;
124
+ scope: ScopeInput;
125
+ query?: string | null;
126
+ document_id?: string | null;
127
+ source_ids?: string[] | null;
128
+ document_ids?: string[] | null;
129
+ entity?: string | null;
130
+ depth?: number | null;
131
+ category?: string | null;
132
+ label?: string | null;
133
+ entity_type?: string | null;
134
+ top_k?: number | null;
135
+ mode?: string | null;
136
+ limit?: number | null;
137
+ cursor?: unknown;
138
+ /** Overrides the deployment's configured policy for this call only. */
139
+ redaction?: unknown;
140
+ /** Replaces the built-in compute action. */
141
+ compute?: KnowledgeComputeFn | null;
142
+ /** Replaces the built-in map_reduce action — the other expensive one. */
143
+ map_reduce?: ((instruction: string, opts: Record<string, unknown>) => Promise<Record<string, unknown>>) | null;
144
+ start?: number | null;
145
+ end?: number | null;
146
+ max_chars?: number | null;
147
+ };
148
+ /** Run one action of the knowledge tool. */
149
+ declare function callKnowledgeTool(engine: Engine$1, args: KnowledgeToolArgs): Promise<Record<string, unknown>>;
150
+
151
+ type PrincipalsFn = () => unknown | Promise<unknown>;
152
+ /** Zero-argument, sync or async, resolved fresh per execution — the same
153
+ * contract as `PrincipalsFn`. Returns the opaque approval scope (a run id is
154
+ * the expected shape) or `null` for the unscoped legacy path. */
155
+ type ApprovalScopeFn = () => unknown | Promise<unknown>;
156
+
157
+ type Engine = {
158
+ config: {
159
+ llm?: unknown;
160
+ enableCodeExecution?: boolean;
161
+ };
162
+ search: (query: string, opts?: Record<string, unknown>) => Promise<{
163
+ hits: unknown[];
164
+ usage?: unknown;
165
+ }>;
166
+ getDocument: (id: string, opts?: Record<string, unknown>) => Promise<unknown>;
167
+ listDocuments: (opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
168
+ queryStructured?: (question: string, opts?: Record<string, unknown>) => Promise<unknown>;
169
+ compute?: (instruction: string, opts?: Record<string, unknown>) => Promise<unknown>;
170
+ searchTools: (query: string, opts?: Record<string, unknown>) => Promise<unknown[]>;
171
+ executeTool: (name: string, args: Record<string, unknown> | null, opts?: Record<string, unknown>) => Promise<Record<string, unknown>>;
172
+ };
173
+ declare function createMcpApp(engine: Engine, opts: {
174
+ principals: PrincipalsFn;
175
+ /**
176
+ * REQUIRED ceiling of source ids (or a `Scope`) this mounted tool may
177
+ * ever reach — resolved fresh per call like `principals` and NEVER a tool
178
+ * argument. A model can ask for any source id it likes, and a tool that
179
+ * believed it would let one caller read another's documents. A host with
180
+ * one shared corpus says so on purpose with `scope: UNSCOPED`.
181
+ */
182
+ scope: ScopeInput | (() => ScopeInput | Promise<ScopeInput>);
183
+ /**
184
+ * Overrides `config.redaction` per call, so a per-project or
185
+ * per-customer policy reaches this tool the way it already reaches
186
+ * `search`. Never a tool argument.
187
+ */
188
+ redaction?: unknown | (() => unknown | Promise<unknown>);
189
+ /**
190
+ * Optionally REPLACES the built-in compute action with the host's own
191
+ * callable. Running generated code is where a host has its own rules
192
+ * about permission, billing and approval; supplying one here means it
193
+ * does not have to intercept the action before the tool is reached, and
194
+ * makes the action available whatever `enableCodeExecution` says.
195
+ */
196
+ compute?: KnowledgeComputeFn | null;
197
+ /** The same seam for the other expensive action — one LLM call per document. */
198
+ mapReduce?: ((instruction: string, opts: Record<string, unknown>) => Promise<Record<string, unknown>>) | null;
199
+ approvalScope?: ApprovalScopeFn | null;
200
+ }): Promise<((req: node_http.IncomingMessage, res: node_http.ServerResponse) => Promise<void>) & {
201
+ mcp: unknown;
202
+ }>;
203
+
204
+ export { type KnowledgeComputeFn as K, type Principals as P, type ScopeInput as S, type Trusted as T, type Unset as U, KNOWLEDGE_ACTIONS as a, KNOWLEDGE_TOOL_DESCRIPTION as b, type KnowledgeAction as c, type Scope as d, TRUSTED as e, UNSCOPED as f, UNSET as g, callKnowledgeTool as h, createMcpApp as i, resolveScope as j, knowledgeToolDefinition as k, narrowToCeiling as n, resolvePrincipals as r };