@promptev/context-engine 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +93 -5
  2. package/dist/cli.js +1761 -501
  3. package/dist/cli.js.map +1 -1
  4. package/dist/{config-Bt9bUQqU.d.ts → config-CNnASw5X.d.cts} +42 -5
  5. package/dist/{config-Bl9U789m.d.cts → config-CdlSkKgV.d.ts} +42 -5
  6. package/dist/express.cjs +925 -151
  7. package/dist/express.cjs.map +1 -1
  8. package/dist/express.d.cts +11 -4
  9. package/dist/express.d.ts +11 -4
  10. package/dist/express.js +926 -152
  11. package/dist/express.js.map +1 -1
  12. package/dist/fastify.cjs +923 -151
  13. package/dist/fastify.cjs.map +1 -1
  14. package/dist/fastify.d.cts +8 -4
  15. package/dist/fastify.d.ts +8 -4
  16. package/dist/fastify.js +924 -152
  17. package/dist/fastify.js.map +1 -1
  18. package/dist/{governance-BDkcv4qZ.d.cts → governance-D8g6Wyvb.d.cts} +8 -2
  19. package/dist/{governance-XIScatRO.d.ts → governance-XFVgtEdV.d.ts} +8 -2
  20. package/dist/graph/index.cjs +122 -43
  21. package/dist/graph/index.cjs.map +1 -1
  22. package/dist/graph/index.d.cts +5 -3
  23. package/dist/graph/index.d.ts +5 -3
  24. package/dist/graph/index.js +122 -43
  25. package/dist/graph/index.js.map +1 -1
  26. package/dist/hono.cjs +923 -151
  27. package/dist/hono.cjs.map +1 -1
  28. package/dist/hono.d.cts +8 -4
  29. package/dist/hono.d.ts +8 -4
  30. package/dist/hono.js +924 -152
  31. package/dist/hono.js.map +1 -1
  32. package/dist/index.cjs +2186 -910
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +70 -107
  35. package/dist/index.d.ts +70 -107
  36. package/dist/index.js +2186 -908
  37. package/dist/index.js.map +1 -1
  38. package/dist/mcp.cjs +100 -14
  39. package/dist/mcp.cjs.map +1 -1
  40. package/dist/mcp.d.cts +5 -0
  41. package/dist/mcp.d.ts +5 -0
  42. package/dist/mcp.js +100 -14
  43. package/dist/mcp.js.map +1 -1
  44. package/dist/migrations/sql/0003_tools.sql +2 -0
  45. package/dist/migrations/sql/0004_acl_indexes.sql +23 -2
  46. package/dist/{redaction-BmDSWJ7h.d.cts → redaction-BqD_DEUQ.d.cts} +22 -1
  47. package/dist/{redaction-BmDSWJ7h.d.ts → redaction-BqD_DEUQ.d.ts} +22 -1
  48. package/dist/redaction-presidio.d.cts +1 -1
  49. package/dist/redaction-presidio.d.ts +1 -1
  50. package/dist/{router-CrxZ2y_Z.d.ts → router-B_DTkQgU.d.ts} +17 -2
  51. package/dist/{router-OPgSoYAB.d.cts → router-Dsv3fv0R.d.cts} +17 -2
  52. package/dist/storage-DU1JRno5.d.cts +164 -0
  53. package/dist/storage-Dvt2ZxsV.d.ts +164 -0
  54. package/package.json +61 -23
  55. package/src/migrations/sql/0003_tools.sql +2 -0
  56. package/src/migrations/sql/0004_acl_indexes.sql +23 -2
  57. package/dist/embeddings-B-jZ42mk.d.cts +0 -67
  58. package/dist/embeddings-DaSdAZN3.d.ts +0 -67
package/dist/index.d.cts CHANGED
@@ -1,91 +1,29 @@
1
1
  import { Pool } from 'pg';
2
- import { C as ContextEngineConfig, L as LLMConfig, R as RerankerConfig } from './config-Bl9U789m.cjs';
3
- export { a as ContextEngineConfigInit, E as EmbeddingConfig, F as FusionConfig, G as GraphConfig, S as StorageConfig } from './config-Bl9U789m.cjs';
4
- import { H as Hooks, R as RedactionPolicy, U as UsageEvent, I as IngestReport } from './redaction-BmDSWJ7h.cjs';
5
- export { D as DocumentReport, a as RedactionRule, b as RedactionRuleInit, c as applyRedaction, e as emitError, d as emitToolCall, f as emitUsage, g as graphUnits, u as unitsForFile } from './redaction-BmDSWJ7h.cjs';
6
- import { E as Embedder, F as FetchImpl } from './embeddings-B-jZ42mk.cjs';
7
- export { a as EmbedKind, b as buildEmbedder } from './embeddings-B-jZ42mk.cjs';
8
- import { T as ToolEngine, C as CanonicalTool, b as ToolHttpClient, a as ToolConfig } from './governance-BDkcv4qZ.cjs';
9
- export { c as ToolKind, d as configSchema } from './governance-BDkcv4qZ.cjs';
2
+ import { C as ContextEngineConfig, L as LLMConfig, E as ExtractionConfig, R as RerankerConfig } from './config-CNnASw5X.cjs';
3
+ export { a as ContextEngineConfigInit, b as EmbeddingConfig, F as FusionConfig, G as GraphConfig, S as StorageConfig } from './config-CNnASw5X.cjs';
4
+ import { H as Hooks, R as RedactionPolicy, U as UsageEvent, P as ProgressEvent, I as IngestReport } from './redaction-BqD_DEUQ.cjs';
5
+ export { D as DocumentReport, a as RedactionRule, b as RedactionRuleInit, c as applyRedaction, e as emitError, d as emitProgress, f as emitToolCall, g as emitUsage, h as graphUnits, u as unitsForFile } from './redaction-BqD_DEUQ.cjs';
6
+ import { E as Embedder, S as StorageBackend, F as FetchImpl } from './storage-DU1JRno5.cjs';
7
+ export { C as ChunkRow, a as EmbedKind, P as PostgresBackend, b as SearchScope, c as buildEmbedder } from './storage-DU1JRno5.cjs';
8
+ import { T as ToolEngine, C as CanonicalTool, b as ToolHttpClient, a as ToolConfig } from './governance-D8g6Wyvb.cjs';
9
+ export { c as ToolKind, d as configSchema } from './governance-D8g6Wyvb.cjs';
10
10
  export { createMcpApp } from './mcp.cjs';
11
11
  import 'zod';
12
12
  import 'node:http';
13
13
 
14
- interface ChunkRow {
15
- idx: number;
16
- text: string;
17
- lang?: string | null;
18
- embedding?: number[] | null;
19
- meta?: Record<string, unknown>;
20
- }
21
- interface SearchScope {
22
- sourceIds?: string[] | null;
23
- principals?: string[] | null;
24
- limit?: number;
25
- }
26
- interface StorageBackend {
27
- supportsFts: boolean;
28
- supportsTrgm: boolean;
29
- supportsAnn: boolean;
30
- upsertChunks(documentId: string, sourceId: string | null, acl: string[] | null, chunks: readonly ChunkRow[]): Promise<number>;
31
- deleteChunks(documentId: string): Promise<void>;
32
- updateChunkEmbeddings(documentId: string, vectors: readonly number[][]): Promise<number>;
33
- updateChunkAcl(documentId: string, acl: string[] | null): Promise<number>;
34
- filterIds(chunkIds: readonly string[], scope: SearchScope): Promise<string[]>;
35
- ftsSearch(query: string, scope: SearchScope): Promise<string[]>;
36
- trgmSearch(query: string, scope: SearchScope): Promise<string[]>;
37
- annSearch(vector: readonly number[], scope: SearchScope): Promise<string[]>;
38
- }
39
- declare class PostgresBackend implements StorageBackend {
40
- supportsFts: boolean;
41
- supportsTrgm: boolean;
42
- supportsAnn: boolean;
43
- private readonly pool;
44
- private readonly exactThresholdOverride;
45
- /** Tri-state: null = not yet checked. */
46
- private iterativeScan;
47
- constructor(pool: Pool, exactThreshold?: number | null);
48
- private get exactThreshold();
49
- /**
50
- * Bind values for SCOPE + the row limit.
51
- * `principals` is passed through UNCHANGED: null (trusted) and [] (anonymous)
52
- * mean different things.
53
- */
54
- private scopeParams;
55
- upsertChunks(documentId: string, sourceId: string | null, acl: string[] | null, chunks: readonly ChunkRow[]): Promise<number>;
56
- deleteChunks(documentId: string): Promise<void>;
57
- updateChunkEmbeddings(documentId: string, vectors: readonly number[][]): Promise<number>;
58
- updateChunkAcl(documentId: string, acl: string[] | null): Promise<number>;
59
- filterIds(chunkIds: readonly string[], scope: SearchScope): Promise<string[]>;
60
- ftsSearch(query: string, scope: SearchScope): Promise<string[]>;
61
- trgmSearch(query: string, scope: SearchScope): Promise<string[]>;
62
- /**
63
- * Set pg_trgm's similarity threshold for this query via the `set_limit()` /
64
- * `show_limit()` FUNCTIONS — never the GUC `pg_trgm.similarity_threshold`.
65
- *
66
- * The GUC is registered lazily, only once pg_trgm's shared library is loaded
67
- * into the backend process. On a fresh pooled connection that has not yet
68
- * run any pg_trgm operation, `SHOW pg_trgm.similarity_threshold` raises
69
- * "unrecognized configuration parameter" — and because that error aborts the
70
- * surrounding transaction, a fallback issued on the same connection then
71
- * fails with "current transaction is aborted". Every connection a pool hands
72
- * out is fresh, so this is the common case, not an edge case. Calling
73
- * `set_limit()` loads the library and does not depend on the GUC. This
74
- * mirrors the Python backend, which was hardened against exactly this.
75
- *
76
- * `set_limit()` is connection-scoped and survives COMMIT and the return to
77
- * the pool, so the previous value is captured here and restored by the
78
- * caller.
79
- */
80
- private applyTrgmThreshold;
81
- private restoreTrgmLimit;
82
- annSearch(vector: readonly number[], scope: SearchScope): Promise<string[]>;
83
- private eligibleIsSmall;
84
- private tuneAnnScan;
85
- private scanBudget;
86
- private enableIterativeScan;
87
- private supportsIterativeScan;
88
- }
14
+ /**
15
+ * Sentinels distinguishing omitted arguments from real values, including null.
16
+ *
17
+ * UNSET: "argument omitted" vs null (e.g. updateDocument acl=null means unrestricted).
18
+ * TRUSTED: trusted caller, ACL filtering disabled. Truthy on purpose so
19
+ * `if (principals)` does not treat a trusted caller as anonymous.
20
+ */
21
+ declare const UNSET: unique symbol;
22
+ type Unset = typeof UNSET;
23
+ declare const TRUSTED: unique symbol;
24
+ type Trusted = typeof TRUSTED;
25
+ type Principals = string[] | null | typeof TRUSTED | undefined;
26
+ declare function resolvePrincipals(value: Principals, method: string): string[] | null;
89
27
 
90
28
  type Mode$1 = "hybrid" | "graph";
91
29
  interface IngestRequest {
@@ -204,7 +142,7 @@ declare function compute(instruction: string, opts: {
204
142
  hooks: Hooks;
205
143
  sourceIds?: string[] | null;
206
144
  principals?: string[] | null;
207
- docIds?: string[] | null;
145
+ documentIds?: string[] | null;
208
146
  modelCfg?: LLMConfig | null;
209
147
  timeout?: number;
210
148
  }): Promise<Record<string, unknown>>;
@@ -317,6 +255,7 @@ interface RunSearchOpts {
317
255
  embedder: Embedder;
318
256
  pool: Pool;
319
257
  sourceIds?: string[] | null;
258
+ documentIds?: string[] | null;
320
259
  principals?: string[] | null;
321
260
  topK?: number;
322
261
  mode?: Mode;
@@ -392,25 +331,6 @@ declare function redactHits(hits: Hit[], policy: RedactionPolicy, opts: {
392
331
  */
393
332
  declare function runSearch(query: string, opts: RunSearchOpts): Promise<SearchResult>;
394
333
 
395
- /**
396
- * Sentinels distinguishing omitted arguments from real values, including null.
397
- *
398
- * UNSET: "argument omitted" vs null (e.g. updateDocument acl=null means unrestricted).
399
- * TRUSTED: trusted caller, ACL filtering disabled. Truthy on purpose so
400
- * `if (principals)` does not treat a trusted caller as anonymous.
401
- */
402
- declare const UNSET: unique symbol;
403
- type Unset = typeof UNSET;
404
- declare class TrustedSentinel {
405
- readonly [Symbol.toStringTag] = "TRUSTED";
406
- toString(): string;
407
- valueOf(): boolean;
408
- }
409
- declare const TRUSTED: TrustedSentinel;
410
- type Trusted = typeof TRUSTED;
411
- type Principals = string[] | null | typeof TRUSTED | undefined;
412
- declare function resolvePrincipals(value: Principals, method: string): string[] | null;
413
-
414
334
  /**
415
335
  * `ContextEngine` — the package's public object.
416
336
  *
@@ -439,6 +359,7 @@ declare class ContextEngine implements ToolEngine {
439
359
  constructor(config: ContextEngineConfig, opts?: {
440
360
  onUsage?: ((e: UsageEvent) => void) | null;
441
361
  onError?: ((e: unknown, ctx: Record<string, unknown>) => void) | null;
362
+ onProgress?: ((e: ProgressEvent) => void) | null;
442
363
  });
443
364
  private ensurePool;
444
365
  /** The embedding client, built on first use. */
@@ -486,6 +407,8 @@ declare class ContextEngine implements ToolEngine {
486
407
  */
487
408
  search(query: string, opts?: {
488
409
  sourceIds?: string[] | null;
410
+ /** Narrows WITHIN a source; INTERSECTS with `sourceIds`. */
411
+ documentIds?: string[] | null;
489
412
  principals?: Principals;
490
413
  topK?: number;
491
414
  mode?: "hybrid" | "graph";
@@ -539,7 +462,7 @@ declare class ContextEngine implements ToolEngine {
539
462
  compute(instruction: string, opts?: {
540
463
  sourceIds?: string[] | null;
541
464
  principals?: Principals;
542
- docIds?: string[] | null;
465
+ documentIds?: string[] | null;
543
466
  modelCfg?: LLMConfig | null;
544
467
  timeout?: number;
545
468
  }): Promise<Record<string, unknown>>;
@@ -566,6 +489,9 @@ declare class ContextEngine implements ToolEngine {
566
489
  principals?: Principals;
567
490
  actor?: Record<string, unknown> | null;
568
491
  source?: string;
492
+ /** Opaque claim scope for approvals (e.g. a run id) — separate from
493
+ * `sourceId`. See `governance.executeTool`. */
494
+ approvalScope?: string | null;
569
495
  }): Promise<Record<string, unknown>>;
570
496
  }
571
497
 
@@ -576,6 +502,23 @@ declare class ContextEngine implements ToolEngine {
576
502
  * reported via `hooks.onError` and degrade to whatever partial text was recovered.
577
503
  */
578
504
 
505
+ /**
506
+ * WHY a document came back with less text than it has, when the answer is
507
+ * actionable. Mirrors the Python client's `Extracted.unreadable_reason` —
508
+ * that docstring is the canonical rationale; only the value semantics are
509
+ * restated here:
510
+ *
511
+ * - `needs_vision` — pages had no usable text layer and no vision model was
512
+ * configured. FIXABLE by configuring one (the OCR fallback may have tried
513
+ * and read nothing; a vision model is still the actionable upgrade).
514
+ * - `vision_failed` — a vision model was configured and produced nothing for
515
+ * those pages. An ops problem, not a configuration one.
516
+ *
517
+ * `null` means nothing was lost — a page a reader READ and found empty is
518
+ * blank, not unreadable. A closed set on purpose (callers branch on it), and
519
+ * NOT an error channel; `mediaOnly` stays set alongside it for whole files.
520
+ */
521
+ type UnreadableReason = "needs_vision" | "vision_failed";
579
522
  declare class Extracted {
580
523
  text: string;
581
524
  pages: number | null;
@@ -584,6 +527,10 @@ declare class Extracted {
584
527
  providerTokens: Record<string, number>;
585
528
  llmPictures: number;
586
529
  isMarkdown: boolean;
530
+ /** See `UnreadableReason`. `mediaOnly` is the same idea for whole files and
531
+ * stays set alongside this, so existing callers keep working. */
532
+ unreadableReason: UnreadableReason | null;
533
+ unreadablePages: number;
587
534
  constructor(init: {
588
535
  text: string;
589
536
  pages?: number | null;
@@ -592,6 +539,8 @@ declare class Extracted {
592
539
  providerTokens?: Record<string, number>;
593
540
  llmPictures?: number;
594
541
  isMarkdown?: boolean;
542
+ unreadableReason?: UnreadableReason | null;
543
+ unreadablePages?: number;
595
544
  });
596
545
  }
597
546
  /**
@@ -601,6 +550,7 @@ declare class Extracted {
601
550
  declare function extract(content: Buffer, filename: string, mime: string | null, opts: {
602
551
  visionLlm?: LLMConfig | null;
603
552
  hooks: Hooks;
553
+ extraction?: ExtractionConfig | null;
604
554
  }): Promise<Extracted>;
605
555
 
606
556
  declare const DEFAULT_LEG_WEIGHT = 1;
@@ -620,7 +570,8 @@ declare function rrfFuse(rankedLists: Record<string, string[]>, opts: {
620
570
  * `callLlm(cfg, { system, user, jsonMode, images, client })` is the single entry
621
571
  * point. Returns `[text, { input, output }]`.
622
572
  *
623
- * Images are always PNG. 30s timeout, no retries. Injectable `client` / `fetch`.
573
+ * Images are always PNG. 240s call timeout (every provider), no retries.
574
+ * Injectable `client` / `fetch`.
624
575
  * `callLlm` owns and closes the client it builds unless one is passed in.
625
576
  */
626
577
 
@@ -660,6 +611,17 @@ type LlmCallOpts = {
660
611
  user: string;
661
612
  jsonMode?: boolean;
662
613
  images?: ImageBytes[] | null;
614
+ maxTokens?: number | null;
615
+ /** Reasoning tokens are drawn from `maxTokens`, so a caller that wants
616
+ * transcription rather than deliberation must be able to spend the whole
617
+ * budget on output. Omitted = the provider's default. Applied per provider
618
+ * capability: gemini and anthropic take it directly (anthropic thinking is
619
+ * opt-in, so 0 sends nothing and a positive budget drops `temperature` —
620
+ * the API rejects the combination); bedrock forwards a positive budget as
621
+ * the anthropic-style passthrough; the openai family has no portable
622
+ * thinking control. */
623
+ thinkingBudget?: number | null;
624
+ temperature?: number | null;
663
625
  };
664
626
  type LLMClientOpts = {
665
627
  client?: ChatClient | null;
@@ -817,6 +779,7 @@ interface ApprovalRecord {
817
779
  toolName: string;
818
780
  toolArgsFrozen: Record<string, unknown>;
819
781
  sourceId: string | null;
782
+ approvalScope: string | null;
820
783
  principals: unknown;
821
784
  status: string;
822
785
  approver: string | null;
@@ -835,7 +798,7 @@ type EngineLike = {
835
798
  };
836
799
  declare function shouldRequireApproval(ct: Pick<CanonicalTool, "requiresApproval" | "approvalPolicy">, args: Record<string, unknown>): boolean;
837
800
  declare function resolveApproval(engine: EngineLike, approvalId: string, decision: "approved" | "rejected", approver: string, meta?: Record<string, unknown> | null, opts?: {
838
- principals?: string[] | null;
801
+ principals?: string[] | null | Trusted;
839
802
  }): Promise<ApprovalRecord>;
840
803
 
841
804
  /**
@@ -848,4 +811,4 @@ declare function functionTool(fn: (...args: never[]) => unknown): CanonicalTool;
848
811
  /** Bumped by CI on every main merge; 0.0.0 = pre-first-release. */
849
812
  declare const __version__ = "0.0.0";
850
813
 
851
- export { ApprovalExpired, ApprovalNotPending, type ApprovalRecord, CeleryRunner, type ChunkRow, CodeExecutionError, CodeExecutionTimeout, ContextEngine, ContextEngineConfig, DEFAULT_LEG_WEIGHT, DocumentNotFoundError, EXTRACTION_VERSION, Embedder, EngineActionError, ExtraMissingError, Extracted, type ExtractionResult, GraphLegUnavailable, type Hit, Hooks, InProcessRunner, IngestReport, LLMClient, LLMConfig, PostgresBackend, type Principals, RedactionPolicy, RerankerConfig, type SearchResult, type SearchScope, type StorageBackend, TRUSTED, type TaskRunner, type TaskStatus, ToolConfig, type Trusted, UNSET, type Unset, UsageEvent, __version__, buildLlmClient, callLlm, compute, decryptDict, encryptDict, extract, extractStructuredData, functionTool, getDocumentText, getSecretKey, listDocuments, queryStructured, redactHits, rerank, resolveApproval, resolveFields, resolvePrincipals, rrfFuse, runMigrate, runSearch, shouldRequireApproval, upsertRegistry };
814
+ export { ApprovalExpired, ApprovalNotPending, type ApprovalRecord, CeleryRunner, CodeExecutionError, CodeExecutionTimeout, ContextEngine, ContextEngineConfig, DEFAULT_LEG_WEIGHT, DocumentNotFoundError, EXTRACTION_VERSION, Embedder, EngineActionError, ExtraMissingError, Extracted, ExtractionConfig, type ExtractionResult, GraphLegUnavailable, type Hit, Hooks, InProcessRunner, IngestReport, LLMClient, LLMConfig, type Principals, ProgressEvent, RedactionPolicy, RerankerConfig, type SearchResult, StorageBackend, TRUSTED, type TaskRunner, type TaskStatus, ToolConfig, type Trusted, UNSET, type Unset, UsageEvent, __version__, buildLlmClient, callLlm, compute, decryptDict, encryptDict, extract, extractStructuredData, functionTool, getDocumentText, getSecretKey, listDocuments, queryStructured, redactHits, rerank, resolveApproval, resolveFields, resolvePrincipals, rrfFuse, runMigrate, runSearch, shouldRequireApproval, upsertRegistry };
package/dist/index.d.ts CHANGED
@@ -1,91 +1,29 @@
1
1
  import { Pool } from 'pg';
2
- import { C as ContextEngineConfig, L as LLMConfig, R as RerankerConfig } from './config-Bt9bUQqU.js';
3
- export { a as ContextEngineConfigInit, E as EmbeddingConfig, F as FusionConfig, G as GraphConfig, S as StorageConfig } from './config-Bt9bUQqU.js';
4
- import { H as Hooks, R as RedactionPolicy, U as UsageEvent, I as IngestReport } from './redaction-BmDSWJ7h.js';
5
- export { D as DocumentReport, a as RedactionRule, b as RedactionRuleInit, c as applyRedaction, e as emitError, d as emitToolCall, f as emitUsage, g as graphUnits, u as unitsForFile } from './redaction-BmDSWJ7h.js';
6
- import { E as Embedder, F as FetchImpl } from './embeddings-DaSdAZN3.js';
7
- export { a as EmbedKind, b as buildEmbedder } from './embeddings-DaSdAZN3.js';
8
- import { T as ToolEngine, C as CanonicalTool, b as ToolHttpClient, a as ToolConfig } from './governance-XIScatRO.js';
9
- export { c as ToolKind, d as configSchema } from './governance-XIScatRO.js';
2
+ import { C as ContextEngineConfig, L as LLMConfig, E as ExtractionConfig, R as RerankerConfig } from './config-CdlSkKgV.js';
3
+ export { a as ContextEngineConfigInit, b as EmbeddingConfig, F as FusionConfig, G as GraphConfig, S as StorageConfig } from './config-CdlSkKgV.js';
4
+ import { H as Hooks, R as RedactionPolicy, U as UsageEvent, P as ProgressEvent, I as IngestReport } from './redaction-BqD_DEUQ.js';
5
+ export { D as DocumentReport, a as RedactionRule, b as RedactionRuleInit, c as applyRedaction, e as emitError, d as emitProgress, f as emitToolCall, g as emitUsage, h as graphUnits, u as unitsForFile } from './redaction-BqD_DEUQ.js';
6
+ import { E as Embedder, S as StorageBackend, F as FetchImpl } from './storage-Dvt2ZxsV.js';
7
+ export { C as ChunkRow, a as EmbedKind, P as PostgresBackend, b as SearchScope, c as buildEmbedder } from './storage-Dvt2ZxsV.js';
8
+ import { T as ToolEngine, C as CanonicalTool, b as ToolHttpClient, a as ToolConfig } from './governance-XFVgtEdV.js';
9
+ export { c as ToolKind, d as configSchema } from './governance-XFVgtEdV.js';
10
10
  export { createMcpApp } from './mcp.js';
11
11
  import 'zod';
12
12
  import 'node:http';
13
13
 
14
- interface ChunkRow {
15
- idx: number;
16
- text: string;
17
- lang?: string | null;
18
- embedding?: number[] | null;
19
- meta?: Record<string, unknown>;
20
- }
21
- interface SearchScope {
22
- sourceIds?: string[] | null;
23
- principals?: string[] | null;
24
- limit?: number;
25
- }
26
- interface StorageBackend {
27
- supportsFts: boolean;
28
- supportsTrgm: boolean;
29
- supportsAnn: boolean;
30
- upsertChunks(documentId: string, sourceId: string | null, acl: string[] | null, chunks: readonly ChunkRow[]): Promise<number>;
31
- deleteChunks(documentId: string): Promise<void>;
32
- updateChunkEmbeddings(documentId: string, vectors: readonly number[][]): Promise<number>;
33
- updateChunkAcl(documentId: string, acl: string[] | null): Promise<number>;
34
- filterIds(chunkIds: readonly string[], scope: SearchScope): Promise<string[]>;
35
- ftsSearch(query: string, scope: SearchScope): Promise<string[]>;
36
- trgmSearch(query: string, scope: SearchScope): Promise<string[]>;
37
- annSearch(vector: readonly number[], scope: SearchScope): Promise<string[]>;
38
- }
39
- declare class PostgresBackend implements StorageBackend {
40
- supportsFts: boolean;
41
- supportsTrgm: boolean;
42
- supportsAnn: boolean;
43
- private readonly pool;
44
- private readonly exactThresholdOverride;
45
- /** Tri-state: null = not yet checked. */
46
- private iterativeScan;
47
- constructor(pool: Pool, exactThreshold?: number | null);
48
- private get exactThreshold();
49
- /**
50
- * Bind values for SCOPE + the row limit.
51
- * `principals` is passed through UNCHANGED: null (trusted) and [] (anonymous)
52
- * mean different things.
53
- */
54
- private scopeParams;
55
- upsertChunks(documentId: string, sourceId: string | null, acl: string[] | null, chunks: readonly ChunkRow[]): Promise<number>;
56
- deleteChunks(documentId: string): Promise<void>;
57
- updateChunkEmbeddings(documentId: string, vectors: readonly number[][]): Promise<number>;
58
- updateChunkAcl(documentId: string, acl: string[] | null): Promise<number>;
59
- filterIds(chunkIds: readonly string[], scope: SearchScope): Promise<string[]>;
60
- ftsSearch(query: string, scope: SearchScope): Promise<string[]>;
61
- trgmSearch(query: string, scope: SearchScope): Promise<string[]>;
62
- /**
63
- * Set pg_trgm's similarity threshold for this query via the `set_limit()` /
64
- * `show_limit()` FUNCTIONS — never the GUC `pg_trgm.similarity_threshold`.
65
- *
66
- * The GUC is registered lazily, only once pg_trgm's shared library is loaded
67
- * into the backend process. On a fresh pooled connection that has not yet
68
- * run any pg_trgm operation, `SHOW pg_trgm.similarity_threshold` raises
69
- * "unrecognized configuration parameter" — and because that error aborts the
70
- * surrounding transaction, a fallback issued on the same connection then
71
- * fails with "current transaction is aborted". Every connection a pool hands
72
- * out is fresh, so this is the common case, not an edge case. Calling
73
- * `set_limit()` loads the library and does not depend on the GUC. This
74
- * mirrors the Python backend, which was hardened against exactly this.
75
- *
76
- * `set_limit()` is connection-scoped and survives COMMIT and the return to
77
- * the pool, so the previous value is captured here and restored by the
78
- * caller.
79
- */
80
- private applyTrgmThreshold;
81
- private restoreTrgmLimit;
82
- annSearch(vector: readonly number[], scope: SearchScope): Promise<string[]>;
83
- private eligibleIsSmall;
84
- private tuneAnnScan;
85
- private scanBudget;
86
- private enableIterativeScan;
87
- private supportsIterativeScan;
88
- }
14
+ /**
15
+ * Sentinels distinguishing omitted arguments from real values, including null.
16
+ *
17
+ * UNSET: "argument omitted" vs null (e.g. updateDocument acl=null means unrestricted).
18
+ * TRUSTED: trusted caller, ACL filtering disabled. Truthy on purpose so
19
+ * `if (principals)` does not treat a trusted caller as anonymous.
20
+ */
21
+ declare const UNSET: unique symbol;
22
+ type Unset = typeof UNSET;
23
+ declare const TRUSTED: unique symbol;
24
+ type Trusted = typeof TRUSTED;
25
+ type Principals = string[] | null | typeof TRUSTED | undefined;
26
+ declare function resolvePrincipals(value: Principals, method: string): string[] | null;
89
27
 
90
28
  type Mode$1 = "hybrid" | "graph";
91
29
  interface IngestRequest {
@@ -204,7 +142,7 @@ declare function compute(instruction: string, opts: {
204
142
  hooks: Hooks;
205
143
  sourceIds?: string[] | null;
206
144
  principals?: string[] | null;
207
- docIds?: string[] | null;
145
+ documentIds?: string[] | null;
208
146
  modelCfg?: LLMConfig | null;
209
147
  timeout?: number;
210
148
  }): Promise<Record<string, unknown>>;
@@ -317,6 +255,7 @@ interface RunSearchOpts {
317
255
  embedder: Embedder;
318
256
  pool: Pool;
319
257
  sourceIds?: string[] | null;
258
+ documentIds?: string[] | null;
320
259
  principals?: string[] | null;
321
260
  topK?: number;
322
261
  mode?: Mode;
@@ -392,25 +331,6 @@ declare function redactHits(hits: Hit[], policy: RedactionPolicy, opts: {
392
331
  */
393
332
  declare function runSearch(query: string, opts: RunSearchOpts): Promise<SearchResult>;
394
333
 
395
- /**
396
- * Sentinels distinguishing omitted arguments from real values, including null.
397
- *
398
- * UNSET: "argument omitted" vs null (e.g. updateDocument acl=null means unrestricted).
399
- * TRUSTED: trusted caller, ACL filtering disabled. Truthy on purpose so
400
- * `if (principals)` does not treat a trusted caller as anonymous.
401
- */
402
- declare const UNSET: unique symbol;
403
- type Unset = typeof UNSET;
404
- declare class TrustedSentinel {
405
- readonly [Symbol.toStringTag] = "TRUSTED";
406
- toString(): string;
407
- valueOf(): boolean;
408
- }
409
- declare const TRUSTED: TrustedSentinel;
410
- type Trusted = typeof TRUSTED;
411
- type Principals = string[] | null | typeof TRUSTED | undefined;
412
- declare function resolvePrincipals(value: Principals, method: string): string[] | null;
413
-
414
334
  /**
415
335
  * `ContextEngine` — the package's public object.
416
336
  *
@@ -439,6 +359,7 @@ declare class ContextEngine implements ToolEngine {
439
359
  constructor(config: ContextEngineConfig, opts?: {
440
360
  onUsage?: ((e: UsageEvent) => void) | null;
441
361
  onError?: ((e: unknown, ctx: Record<string, unknown>) => void) | null;
362
+ onProgress?: ((e: ProgressEvent) => void) | null;
442
363
  });
443
364
  private ensurePool;
444
365
  /** The embedding client, built on first use. */
@@ -486,6 +407,8 @@ declare class ContextEngine implements ToolEngine {
486
407
  */
487
408
  search(query: string, opts?: {
488
409
  sourceIds?: string[] | null;
410
+ /** Narrows WITHIN a source; INTERSECTS with `sourceIds`. */
411
+ documentIds?: string[] | null;
489
412
  principals?: Principals;
490
413
  topK?: number;
491
414
  mode?: "hybrid" | "graph";
@@ -539,7 +462,7 @@ declare class ContextEngine implements ToolEngine {
539
462
  compute(instruction: string, opts?: {
540
463
  sourceIds?: string[] | null;
541
464
  principals?: Principals;
542
- docIds?: string[] | null;
465
+ documentIds?: string[] | null;
543
466
  modelCfg?: LLMConfig | null;
544
467
  timeout?: number;
545
468
  }): Promise<Record<string, unknown>>;
@@ -566,6 +489,9 @@ declare class ContextEngine implements ToolEngine {
566
489
  principals?: Principals;
567
490
  actor?: Record<string, unknown> | null;
568
491
  source?: string;
492
+ /** Opaque claim scope for approvals (e.g. a run id) — separate from
493
+ * `sourceId`. See `governance.executeTool`. */
494
+ approvalScope?: string | null;
569
495
  }): Promise<Record<string, unknown>>;
570
496
  }
571
497
 
@@ -576,6 +502,23 @@ declare class ContextEngine implements ToolEngine {
576
502
  * reported via `hooks.onError` and degrade to whatever partial text was recovered.
577
503
  */
578
504
 
505
+ /**
506
+ * WHY a document came back with less text than it has, when the answer is
507
+ * actionable. Mirrors the Python client's `Extracted.unreadable_reason` —
508
+ * that docstring is the canonical rationale; only the value semantics are
509
+ * restated here:
510
+ *
511
+ * - `needs_vision` — pages had no usable text layer and no vision model was
512
+ * configured. FIXABLE by configuring one (the OCR fallback may have tried
513
+ * and read nothing; a vision model is still the actionable upgrade).
514
+ * - `vision_failed` — a vision model was configured and produced nothing for
515
+ * those pages. An ops problem, not a configuration one.
516
+ *
517
+ * `null` means nothing was lost — a page a reader READ and found empty is
518
+ * blank, not unreadable. A closed set on purpose (callers branch on it), and
519
+ * NOT an error channel; `mediaOnly` stays set alongside it for whole files.
520
+ */
521
+ type UnreadableReason = "needs_vision" | "vision_failed";
579
522
  declare class Extracted {
580
523
  text: string;
581
524
  pages: number | null;
@@ -584,6 +527,10 @@ declare class Extracted {
584
527
  providerTokens: Record<string, number>;
585
528
  llmPictures: number;
586
529
  isMarkdown: boolean;
530
+ /** See `UnreadableReason`. `mediaOnly` is the same idea for whole files and
531
+ * stays set alongside this, so existing callers keep working. */
532
+ unreadableReason: UnreadableReason | null;
533
+ unreadablePages: number;
587
534
  constructor(init: {
588
535
  text: string;
589
536
  pages?: number | null;
@@ -592,6 +539,8 @@ declare class Extracted {
592
539
  providerTokens?: Record<string, number>;
593
540
  llmPictures?: number;
594
541
  isMarkdown?: boolean;
542
+ unreadableReason?: UnreadableReason | null;
543
+ unreadablePages?: number;
595
544
  });
596
545
  }
597
546
  /**
@@ -601,6 +550,7 @@ declare class Extracted {
601
550
  declare function extract(content: Buffer, filename: string, mime: string | null, opts: {
602
551
  visionLlm?: LLMConfig | null;
603
552
  hooks: Hooks;
553
+ extraction?: ExtractionConfig | null;
604
554
  }): Promise<Extracted>;
605
555
 
606
556
  declare const DEFAULT_LEG_WEIGHT = 1;
@@ -620,7 +570,8 @@ declare function rrfFuse(rankedLists: Record<string, string[]>, opts: {
620
570
  * `callLlm(cfg, { system, user, jsonMode, images, client })` is the single entry
621
571
  * point. Returns `[text, { input, output }]`.
622
572
  *
623
- * Images are always PNG. 30s timeout, no retries. Injectable `client` / `fetch`.
573
+ * Images are always PNG. 240s call timeout (every provider), no retries.
574
+ * Injectable `client` / `fetch`.
624
575
  * `callLlm` owns and closes the client it builds unless one is passed in.
625
576
  */
626
577
 
@@ -660,6 +611,17 @@ type LlmCallOpts = {
660
611
  user: string;
661
612
  jsonMode?: boolean;
662
613
  images?: ImageBytes[] | null;
614
+ maxTokens?: number | null;
615
+ /** Reasoning tokens are drawn from `maxTokens`, so a caller that wants
616
+ * transcription rather than deliberation must be able to spend the whole
617
+ * budget on output. Omitted = the provider's default. Applied per provider
618
+ * capability: gemini and anthropic take it directly (anthropic thinking is
619
+ * opt-in, so 0 sends nothing and a positive budget drops `temperature` —
620
+ * the API rejects the combination); bedrock forwards a positive budget as
621
+ * the anthropic-style passthrough; the openai family has no portable
622
+ * thinking control. */
623
+ thinkingBudget?: number | null;
624
+ temperature?: number | null;
663
625
  };
664
626
  type LLMClientOpts = {
665
627
  client?: ChatClient | null;
@@ -817,6 +779,7 @@ interface ApprovalRecord {
817
779
  toolName: string;
818
780
  toolArgsFrozen: Record<string, unknown>;
819
781
  sourceId: string | null;
782
+ approvalScope: string | null;
820
783
  principals: unknown;
821
784
  status: string;
822
785
  approver: string | null;
@@ -835,7 +798,7 @@ type EngineLike = {
835
798
  };
836
799
  declare function shouldRequireApproval(ct: Pick<CanonicalTool, "requiresApproval" | "approvalPolicy">, args: Record<string, unknown>): boolean;
837
800
  declare function resolveApproval(engine: EngineLike, approvalId: string, decision: "approved" | "rejected", approver: string, meta?: Record<string, unknown> | null, opts?: {
838
- principals?: string[] | null;
801
+ principals?: string[] | null | Trusted;
839
802
  }): Promise<ApprovalRecord>;
840
803
 
841
804
  /**
@@ -848,4 +811,4 @@ declare function functionTool(fn: (...args: never[]) => unknown): CanonicalTool;
848
811
  /** Bumped by CI on every main merge; 0.0.0 = pre-first-release. */
849
812
  declare const __version__ = "0.0.0";
850
813
 
851
- export { ApprovalExpired, ApprovalNotPending, type ApprovalRecord, CeleryRunner, type ChunkRow, CodeExecutionError, CodeExecutionTimeout, ContextEngine, ContextEngineConfig, DEFAULT_LEG_WEIGHT, DocumentNotFoundError, EXTRACTION_VERSION, Embedder, EngineActionError, ExtraMissingError, Extracted, type ExtractionResult, GraphLegUnavailable, type Hit, Hooks, InProcessRunner, IngestReport, LLMClient, LLMConfig, PostgresBackend, type Principals, RedactionPolicy, RerankerConfig, type SearchResult, type SearchScope, type StorageBackend, TRUSTED, type TaskRunner, type TaskStatus, ToolConfig, type Trusted, UNSET, type Unset, UsageEvent, __version__, buildLlmClient, callLlm, compute, decryptDict, encryptDict, extract, extractStructuredData, functionTool, getDocumentText, getSecretKey, listDocuments, queryStructured, redactHits, rerank, resolveApproval, resolveFields, resolvePrincipals, rrfFuse, runMigrate, runSearch, shouldRequireApproval, upsertRegistry };
814
+ export { ApprovalExpired, ApprovalNotPending, type ApprovalRecord, CeleryRunner, CodeExecutionError, CodeExecutionTimeout, ContextEngine, ContextEngineConfig, DEFAULT_LEG_WEIGHT, DocumentNotFoundError, EXTRACTION_VERSION, Embedder, EngineActionError, ExtraMissingError, Extracted, ExtractionConfig, type ExtractionResult, GraphLegUnavailable, type Hit, Hooks, InProcessRunner, IngestReport, LLMClient, LLMConfig, type Principals, ProgressEvent, RedactionPolicy, RerankerConfig, type SearchResult, StorageBackend, TRUSTED, type TaskRunner, type TaskStatus, ToolConfig, type Trusted, UNSET, type Unset, UsageEvent, __version__, buildLlmClient, callLlm, compute, decryptDict, encryptDict, extract, extractStructuredData, functionTool, getDocumentText, getSecretKey, listDocuments, queryStructured, redactHits, rerank, resolveApproval, resolveFields, resolvePrincipals, rrfFuse, runMigrate, runSearch, shouldRequireApproval, upsertRegistry };