@tangle-network/agent-knowledge 0.1.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { c as KnowledgeWriteParseResult, S as SourceRecord, d as KnowledgePage, e as SourceRegistry, b as KnowledgeGraph, f as KnowledgeIndex, g as KnowledgeSearchResult, h as KnowledgeLintFinding, i as KnowledgeBaseCandidate } from './types-C1FBNRIN.js';
2
- export { C as ClaimRef, j as KnowledgeClaim, K as KnowledgeGraphEdge, a as KnowledgeGraphNode, k as KnowledgeId, l as KnowledgePolicy, m as KnowledgeRelation, n as KnowledgeUnit, o as KnowledgeWriteBlock, p as SourceAnchor } from './types-C1FBNRIN.js';
3
- import { MultiShotVariant, MultiShotOptimizationConfig, MultiShotRunner, MultiShotScorer, MultiShotMutateAdapter, MultiShotOptimizationResult } from '@tangle-network/agent-eval';
1
+ import { c as KnowledgeWriteParseResult, S as SourceRecord, d as KnowledgeEventType, e as KnowledgeEvent, f as KnowledgePage, g as KnowledgeIndex, h as SourceRegistry, b as KnowledgeGraph, i as KnowledgeSearchResult, j as KnowledgeLintFinding, k as KnowledgeBaseCandidate, l as KnowledgeRelease } from './types-CWbf3JSE.js';
2
+ export { C as ClaimRef, m as KnowledgeClaim, K as KnowledgeGraphEdge, a as KnowledgeGraphNode, n as KnowledgeId, o as KnowledgePolicy, p as KnowledgeRelation, q as KnowledgeUnit, r as KnowledgeWriteBlock, s as SourceAnchor } from './types-CWbf3JSE.js';
3
+ import { z } from 'zod';
4
+ import { MultiShotVariant, MultiShotOptimizationConfig, MultiShotRunner, MultiShotScorer, MultiShotMutateAdapter, MultiShotOptimizationResult, ReleaseConfidenceScorecard, RunRecord, KnowledgeRequirementCategory, KnowledgeAcquisitionMode, KnowledgeImportance, KnowledgeFreshness, KnowledgeSensitivity, KnowledgeRequirement, KnowledgeBundle, KnowledgeReadinessReport, UserQuestion, DataAcquisitionPlan } from '@tangle-network/agent-eval';
4
5
 
5
6
  declare function sha256(text: string): string;
6
7
  declare function slugify(input: string): string;
@@ -48,6 +49,255 @@ interface ApplyWriteBlocksResult {
48
49
  declare function applyKnowledgeWriteBlocks(root: string, proposalText: string): Promise<ApplyWriteBlocksResult>;
49
50
  declare function applyKnowledgeWriteBlocksFile(root: string, proposalPath: string): Promise<ApplyWriteBlocksResult>;
50
51
 
52
+ declare const SourceAnchorSchema: z.ZodObject<{
53
+ id: z.ZodString;
54
+ sourceId: z.ZodString;
55
+ label: z.ZodOptional<z.ZodString>;
56
+ page: z.ZodOptional<z.ZodNumber>;
57
+ lineStart: z.ZodOptional<z.ZodNumber>;
58
+ lineEnd: z.ZodOptional<z.ZodNumber>;
59
+ charStart: z.ZodOptional<z.ZodNumber>;
60
+ charEnd: z.ZodOptional<z.ZodNumber>;
61
+ timestampMs: z.ZodOptional<z.ZodNumber>;
62
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
63
+ }, z.core.$strip>;
64
+ declare const SourceRecordSchema: z.ZodObject<{
65
+ id: z.ZodString;
66
+ uri: z.ZodString;
67
+ title: z.ZodOptional<z.ZodString>;
68
+ mediaType: z.ZodOptional<z.ZodString>;
69
+ contentHash: z.ZodString;
70
+ text: z.ZodOptional<z.ZodString>;
71
+ anchors: z.ZodOptional<z.ZodArray<z.ZodObject<{
72
+ id: z.ZodString;
73
+ sourceId: z.ZodString;
74
+ label: z.ZodOptional<z.ZodString>;
75
+ page: z.ZodOptional<z.ZodNumber>;
76
+ lineStart: z.ZodOptional<z.ZodNumber>;
77
+ lineEnd: z.ZodOptional<z.ZodNumber>;
78
+ charStart: z.ZodOptional<z.ZodNumber>;
79
+ charEnd: z.ZodOptional<z.ZodNumber>;
80
+ timestampMs: z.ZodOptional<z.ZodNumber>;
81
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
82
+ }, z.core.$strip>>>;
83
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
84
+ createdAt: z.ZodString;
85
+ }, z.core.$strip>;
86
+ declare const KnowledgePageSchema: z.ZodObject<{
87
+ id: z.ZodString;
88
+ path: z.ZodString;
89
+ title: z.ZodString;
90
+ text: z.ZodString;
91
+ frontmatter: z.ZodRecord<z.ZodString, z.ZodUnknown>;
92
+ sourceIds: z.ZodArray<z.ZodString>;
93
+ tags: z.ZodArray<z.ZodString>;
94
+ outLinks: z.ZodArray<z.ZodString>;
95
+ }, z.core.$strip>;
96
+ declare const KnowledgeGraphNodeSchema: z.ZodObject<{
97
+ id: z.ZodString;
98
+ title: z.ZodString;
99
+ path: z.ZodString;
100
+ tags: z.ZodArray<z.ZodString>;
101
+ sourceIds: z.ZodArray<z.ZodString>;
102
+ outDegree: z.ZodNumber;
103
+ inDegree: z.ZodNumber;
104
+ }, z.core.$strip>;
105
+ declare const KnowledgeGraphEdgeSchema: z.ZodObject<{
106
+ source: z.ZodString;
107
+ target: z.ZodString;
108
+ weight: z.ZodNumber;
109
+ reasons: z.ZodArray<z.ZodString>;
110
+ }, z.core.$strip>;
111
+ declare const KnowledgeIndexSchema: z.ZodObject<{
112
+ root: z.ZodString;
113
+ generatedAt: z.ZodString;
114
+ sources: z.ZodArray<z.ZodObject<{
115
+ id: z.ZodString;
116
+ uri: z.ZodString;
117
+ title: z.ZodOptional<z.ZodString>;
118
+ mediaType: z.ZodOptional<z.ZodString>;
119
+ contentHash: z.ZodString;
120
+ text: z.ZodOptional<z.ZodString>;
121
+ anchors: z.ZodOptional<z.ZodArray<z.ZodObject<{
122
+ id: z.ZodString;
123
+ sourceId: z.ZodString;
124
+ label: z.ZodOptional<z.ZodString>;
125
+ page: z.ZodOptional<z.ZodNumber>;
126
+ lineStart: z.ZodOptional<z.ZodNumber>;
127
+ lineEnd: z.ZodOptional<z.ZodNumber>;
128
+ charStart: z.ZodOptional<z.ZodNumber>;
129
+ charEnd: z.ZodOptional<z.ZodNumber>;
130
+ timestampMs: z.ZodOptional<z.ZodNumber>;
131
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
132
+ }, z.core.$strip>>>;
133
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
134
+ createdAt: z.ZodString;
135
+ }, z.core.$strip>>;
136
+ pages: z.ZodArray<z.ZodObject<{
137
+ id: z.ZodString;
138
+ path: z.ZodString;
139
+ title: z.ZodString;
140
+ text: z.ZodString;
141
+ frontmatter: z.ZodRecord<z.ZodString, z.ZodUnknown>;
142
+ sourceIds: z.ZodArray<z.ZodString>;
143
+ tags: z.ZodArray<z.ZodString>;
144
+ outLinks: z.ZodArray<z.ZodString>;
145
+ }, z.core.$strip>>;
146
+ graph: z.ZodObject<{
147
+ nodes: z.ZodArray<z.ZodObject<{
148
+ id: z.ZodString;
149
+ title: z.ZodString;
150
+ path: z.ZodString;
151
+ tags: z.ZodArray<z.ZodString>;
152
+ sourceIds: z.ZodArray<z.ZodString>;
153
+ outDegree: z.ZodNumber;
154
+ inDegree: z.ZodNumber;
155
+ }, z.core.$strip>>;
156
+ edges: z.ZodArray<z.ZodObject<{
157
+ source: z.ZodString;
158
+ target: z.ZodString;
159
+ weight: z.ZodNumber;
160
+ reasons: z.ZodArray<z.ZodString>;
161
+ }, z.core.$strip>>;
162
+ }, z.core.$strip>;
163
+ }, z.core.$strip>;
164
+ declare const KnowledgeEventSchema: z.ZodObject<{
165
+ id: z.ZodString;
166
+ type: z.ZodEnum<{
167
+ "source.added": "source.added";
168
+ "proposal.applied": "proposal.applied";
169
+ "index.built": "index.built";
170
+ "lint.run": "lint.run";
171
+ "optimization.run": "optimization.run";
172
+ "release.promoted": "release.promoted";
173
+ "release.rejected": "release.rejected";
174
+ }>;
175
+ createdAt: z.ZodString;
176
+ actor: z.ZodOptional<z.ZodString>;
177
+ target: z.ZodOptional<z.ZodString>;
178
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
179
+ }, z.core.$strip>;
180
+ declare const KnowledgeBaseCandidateSchema: z.ZodObject<{
181
+ id: z.ZodString;
182
+ units: z.ZodArray<z.ZodObject<{
183
+ id: z.ZodString;
184
+ title: z.ZodString;
185
+ text: z.ZodString;
186
+ claims: z.ZodOptional<z.ZodArray<z.ZodObject<{
187
+ id: z.ZodString;
188
+ text: z.ZodString;
189
+ refs: z.ZodArray<z.ZodObject<{
190
+ sourceId: z.ZodString;
191
+ anchorId: z.ZodOptional<z.ZodString>;
192
+ quote: z.ZodOptional<z.ZodString>;
193
+ }, z.core.$strip>>;
194
+ confidence: z.ZodOptional<z.ZodNumber>;
195
+ status: z.ZodOptional<z.ZodEnum<{
196
+ draft: "draft";
197
+ active: "active";
198
+ superseded: "superseded";
199
+ rejected: "rejected";
200
+ }>>;
201
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
202
+ }, z.core.$strip>>>;
203
+ relations: z.ZodOptional<z.ZodArray<z.ZodObject<{
204
+ sourceId: z.ZodString;
205
+ targetId: z.ZodString;
206
+ predicate: z.ZodString;
207
+ weight: z.ZodOptional<z.ZodNumber>;
208
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
209
+ }, z.core.$strip>>>;
210
+ sourceIds: z.ZodOptional<z.ZodArray<z.ZodString>>;
211
+ tags: z.ZodOptional<z.ZodArray<z.ZodString>>;
212
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
213
+ updatedAt: z.ZodOptional<z.ZodString>;
214
+ }, z.core.$strip>>;
215
+ retrievalPolicy: z.ZodOptional<z.ZodString>;
216
+ synthesisPolicy: z.ZodOptional<z.ZodString>;
217
+ questionPolicy: z.ZodOptional<z.ZodString>;
218
+ updatePolicy: z.ZodOptional<z.ZodString>;
219
+ metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
220
+ }, z.core.$strip>;
221
+
222
+ interface KnowledgeEventQuery {
223
+ type?: KnowledgeEventType;
224
+ target?: string;
225
+ limit?: number;
226
+ }
227
+ declare function createKnowledgeEvent(input: {
228
+ type: KnowledgeEventType;
229
+ actor?: string;
230
+ target?: string;
231
+ metadata?: Record<string, unknown>;
232
+ now?: () => Date;
233
+ }): KnowledgeEvent;
234
+
235
+ interface KbStore {
236
+ putSource(source: SourceRecord): Promise<void>;
237
+ getSource(id: string): Promise<SourceRecord | null>;
238
+ listSources(): Promise<SourceRecord[]>;
239
+ putPage(page: KnowledgePage): Promise<void>;
240
+ getPage(idOrPath: string): Promise<KnowledgePage | null>;
241
+ listPages(): Promise<KnowledgePage[]>;
242
+ putIndex(index: KnowledgeIndex): Promise<void>;
243
+ getIndex(): Promise<KnowledgeIndex | null>;
244
+ putEvent(event: KnowledgeEvent): Promise<void>;
245
+ listEvents(query?: KnowledgeEventQuery): Promise<KnowledgeEvent[]>;
246
+ }
247
+ declare class MemoryKbStore implements KbStore {
248
+ private readonly sources;
249
+ private readonly pages;
250
+ private readonly events;
251
+ private index;
252
+ putSource(source: SourceRecord): Promise<void>;
253
+ getSource(id: string): Promise<SourceRecord | null>;
254
+ listSources(): Promise<SourceRecord[]>;
255
+ putPage(page: KnowledgePage): Promise<void>;
256
+ getPage(idOrPath: string): Promise<KnowledgePage | null>;
257
+ listPages(): Promise<KnowledgePage[]>;
258
+ putIndex(index: KnowledgeIndex): Promise<void>;
259
+ getIndex(): Promise<KnowledgeIndex | null>;
260
+ putEvent(event: KnowledgeEvent): Promise<void>;
261
+ listEvents(query?: KnowledgeEventQuery): Promise<KnowledgeEvent[]>;
262
+ }
263
+ declare class FileSystemKbStore extends MemoryKbStore {
264
+ private readonly dir;
265
+ constructor(dir: string);
266
+ putIndex(index: KnowledgeIndex): Promise<void>;
267
+ getIndex(): Promise<KnowledgeIndex | null>;
268
+ putEvent(event: KnowledgeEvent): Promise<void>;
269
+ }
270
+
271
+ interface DiscoveryTask {
272
+ id: string;
273
+ goal: string;
274
+ query?: string;
275
+ sourceHints?: string[];
276
+ metadata?: Record<string, unknown>;
277
+ }
278
+ interface DiscoveryResult {
279
+ taskId: string;
280
+ summary: string;
281
+ sourceUris?: string[];
282
+ claims?: Array<{
283
+ text: string;
284
+ sourceUri?: string;
285
+ confidence?: number;
286
+ }>;
287
+ followUpTasks?: DiscoveryTask[];
288
+ metadata?: Record<string, unknown>;
289
+ }
290
+ interface KnowledgeDiscoveryWorker {
291
+ run(task: DiscoveryTask, signal?: AbortSignal): Promise<DiscoveryResult> | DiscoveryResult;
292
+ }
293
+ interface KnowledgeDiscoveryDispatcher {
294
+ dispatch(tasks: DiscoveryTask[], options?: {
295
+ concurrency?: number;
296
+ signal?: AbortSignal;
297
+ }): Promise<DiscoveryResult[]>;
298
+ }
299
+ declare function createLocalDiscoveryDispatcher(worker: KnowledgeDiscoveryWorker): KnowledgeDiscoveryDispatcher;
300
+
51
301
  interface ChunkingOptions {
52
302
  targetChars: number;
53
303
  maxChars: number;
@@ -78,6 +328,23 @@ interface KnowledgeLayout {
78
328
  cacheDir: string;
79
329
  }
80
330
  declare function layoutFor(root: string): KnowledgeLayout;
331
+ /**
332
+ * Filenames that `initKnowledgeBase` writes as human-navigation scaffolding.
333
+ * These are excluded from the page index — they exist on disk so authors can
334
+ * curate their vault, but they are not searchable content.
335
+ *
336
+ * Add new scaffold filenames here (and only here) to keep lint, validate, viz,
337
+ * and the indexer consistent.
338
+ */
339
+ declare const SCAFFOLD_PAGE_BASENAMES: readonly string[];
340
+ /**
341
+ * True when a knowledge-relative path points at a scaffold file rather than
342
+ * authored content. Accepts both repo-relative paths (`knowledge/index.md`)
343
+ * and any nested `<dir>/index.md` or `<dir>/log.md` (for example
344
+ * `knowledge/concepts/index.md`) so subdirectory README-style scaffolds are
345
+ * also excluded.
346
+ */
347
+ declare function isScaffoldPath(path: string): boolean;
81
348
  declare function initKnowledgeBase(root: string): Promise<KnowledgeLayout>;
82
349
  declare function loadKnowledgePages(root: string): Promise<KnowledgePage[]>;
83
350
  declare function writeJson(path: string, value: unknown): Promise<void>;
@@ -136,6 +403,15 @@ interface KnowledgeExplanation {
136
403
  }
137
404
  declare function explainKnowledgeTarget(index: KnowledgeIndex, target: string): KnowledgeExplanation;
138
405
 
406
+ interface ValidateKnowledgeOptions {
407
+ strict?: boolean;
408
+ }
409
+ interface ValidateKnowledgeResult {
410
+ ok: boolean;
411
+ findings: KnowledgeLintFinding[];
412
+ }
413
+ declare function validateKnowledgeIndex(index: KnowledgeIndex, options?: ValidateKnowledgeOptions): ValidateKnowledgeResult;
414
+
139
415
  type KnowledgeBaseVariant = MultiShotVariant<KnowledgeBaseCandidate>;
140
416
  interface KnowledgeOptimizationConfig extends Omit<MultiShotOptimizationConfig<KnowledgeBaseCandidate>, 'runner' | 'scorer' | 'mutateAdapter' | 'target'> {
141
417
  target?: string;
@@ -150,4 +426,50 @@ declare function knowledgeVariantFromCandidate(candidate: KnowledgeBaseCandidate
150
426
  generation?: number;
151
427
  }): KnowledgeBaseVariant;
152
428
 
153
- export { type AddSourceOptions, type ApplyWriteBlocksResult, type ChunkingOptions, KnowledgeBaseCandidate, type KnowledgeBaseVariant, type KnowledgeChunk, type KnowledgeExplanation, KnowledgeGraph, KnowledgeIndex, type KnowledgeInspection, type KnowledgeLayout, KnowledgeLintFinding, type KnowledgeOptimizationConfig, KnowledgePage, KnowledgeSearchResult, KnowledgeWriteParseResult, type ParsedFrontmatter, type SourceAdapter, type SourceAdapterInput, type SourceAdapterOutput, SourceRecord, SourceRegistry, WIKILINK_REGEX, addSourcePath, applyKnowledgeWriteBlocks, applyKnowledgeWriteBlocksFile, buildKnowledgeGraph, buildKnowledgeIndex, chunkMarkdown, explainKnowledgeTarget, extractWikilinks, formatFrontmatter, initKnowledgeBase, inspectKnowledgeIndex, isSafeKnowledgePath, knowledgeVariantFromCandidate, layoutFor, lintKnowledgeIndex, loadKnowledgePages, loadSourceRegistry, mediaTypeFor, normalizeLinkTarget, parseFrontmatter, parseKnowledgeWriteBlocks, reciprocalRankFusion, runKnowledgeBaseOptimization, searchKnowledge, sha256, slugify, sourceRegistryPath, stableId, stripFrontmatter, textSourceAdapter, tokenizeQuery, writeJson, writeKnowledgeIndex, writeSourceRegistry };
429
+ interface KnowledgeReleaseReport {
430
+ release: KnowledgeRelease;
431
+ scorecard: ReleaseConfidenceScorecard;
432
+ candidateRuns: RunRecord[];
433
+ baselineRuns: RunRecord[];
434
+ }
435
+ declare function knowledgeReleaseReportFromOptimization(result: MultiShotOptimizationResult<KnowledgeBaseCandidate>, options?: {
436
+ runRecords?: RunRecord[];
437
+ createdAt?: string;
438
+ minScore?: number;
439
+ }): KnowledgeReleaseReport;
440
+
441
+ interface KnowledgeReadinessSpec {
442
+ id: string;
443
+ description: string;
444
+ query: string;
445
+ requiredFor: string[];
446
+ category: KnowledgeRequirementCategory;
447
+ acquisitionMode: KnowledgeAcquisitionMode;
448
+ importance: KnowledgeImportance;
449
+ freshness: KnowledgeFreshness;
450
+ sensitivity: KnowledgeSensitivity;
451
+ confidenceNeeded: number;
452
+ fallbackPolicy?: KnowledgeRequirement['fallbackPolicy'];
453
+ minSources?: number;
454
+ minHits?: number;
455
+ metadata?: Record<string, unknown>;
456
+ }
457
+ interface BuildEvalKnowledgeBundleOptions {
458
+ taskId: string;
459
+ index: KnowledgeIndex;
460
+ specs: KnowledgeReadinessSpec[];
461
+ userAnswers?: Record<string, string>;
462
+ searchLimit?: number;
463
+ metadata?: Record<string, unknown>;
464
+ }
465
+ interface EvalKnowledgeBundleBuildResult {
466
+ bundle: KnowledgeBundle;
467
+ report: KnowledgeReadinessReport;
468
+ requirements: KnowledgeRequirement[];
469
+ searchResultsByRequirement: Record<string, KnowledgeSearchResult[]>;
470
+ questions: UserQuestion[];
471
+ acquisitionPlans: DataAcquisitionPlan[];
472
+ }
473
+ declare function buildEvalKnowledgeBundle(options: BuildEvalKnowledgeBundleOptions): EvalKnowledgeBundleBuildResult;
474
+
475
+ export { type AddSourceOptions, type ApplyWriteBlocksResult, type BuildEvalKnowledgeBundleOptions, type ChunkingOptions, type DiscoveryResult, type DiscoveryTask, type EvalKnowledgeBundleBuildResult, FileSystemKbStore, type KbStore, KnowledgeBaseCandidate, KnowledgeBaseCandidateSchema, type KnowledgeBaseVariant, type KnowledgeChunk, type KnowledgeDiscoveryDispatcher, type KnowledgeDiscoveryWorker, KnowledgeEvent, type KnowledgeEventQuery, KnowledgeEventSchema, KnowledgeEventType, type KnowledgeExplanation, KnowledgeGraph, KnowledgeGraphEdgeSchema, KnowledgeGraphNodeSchema, KnowledgeIndex, KnowledgeIndexSchema, type KnowledgeInspection, type KnowledgeLayout, KnowledgeLintFinding, type KnowledgeOptimizationConfig, KnowledgePage, KnowledgePageSchema, type KnowledgeReadinessSpec, KnowledgeRelease, type KnowledgeReleaseReport, KnowledgeSearchResult, KnowledgeWriteParseResult, MemoryKbStore, type ParsedFrontmatter, SCAFFOLD_PAGE_BASENAMES, type SourceAdapter, type SourceAdapterInput, type SourceAdapterOutput, SourceAnchorSchema, SourceRecord, SourceRecordSchema, SourceRegistry, type ValidateKnowledgeOptions, type ValidateKnowledgeResult, WIKILINK_REGEX, addSourcePath, applyKnowledgeWriteBlocks, applyKnowledgeWriteBlocksFile, buildEvalKnowledgeBundle, buildKnowledgeGraph, buildKnowledgeIndex, chunkMarkdown, createKnowledgeEvent, createLocalDiscoveryDispatcher, explainKnowledgeTarget, extractWikilinks, formatFrontmatter, initKnowledgeBase, inspectKnowledgeIndex, isSafeKnowledgePath, isScaffoldPath, knowledgeReleaseReportFromOptimization, knowledgeVariantFromCandidate, layoutFor, lintKnowledgeIndex, loadKnowledgePages, loadSourceRegistry, mediaTypeFor, normalizeLinkTarget, parseFrontmatter, parseKnowledgeWriteBlocks, reciprocalRankFusion, runKnowledgeBaseOptimization, searchKnowledge, sha256, slugify, sourceRegistryPath, stableId, stripFrontmatter, textSourceAdapter, tokenizeQuery, validateKnowledgeIndex, writeJson, writeKnowledgeIndex, writeSourceRegistry };
package/dist/index.js CHANGED
@@ -1,4 +1,13 @@
1
1
  import {
2
+ KnowledgeBaseCandidateSchema,
3
+ KnowledgeEventSchema,
4
+ KnowledgeGraphEdgeSchema,
5
+ KnowledgeGraphNodeSchema,
6
+ KnowledgeIndexSchema,
7
+ KnowledgePageSchema,
8
+ SCAFFOLD_PAGE_BASENAMES,
9
+ SourceAnchorSchema,
10
+ SourceRecordSchema,
2
11
  WIKILINK_REGEX,
3
12
  addSourcePath,
4
13
  applyKnowledgeWriteBlocks,
@@ -11,6 +20,7 @@ import {
11
20
  initKnowledgeBase,
12
21
  inspectKnowledgeIndex,
13
22
  isSafeKnowledgePath,
23
+ isScaffoldPath,
14
24
  layoutFor,
15
25
  lintKnowledgeIndex,
16
26
  loadKnowledgePages,
@@ -27,10 +37,121 @@ import {
27
37
  stableId,
28
38
  textSourceAdapter,
29
39
  tokenizeQuery,
40
+ validateKnowledgeIndex,
30
41
  writeJson,
31
42
  writeKnowledgeIndex,
32
43
  writeSourceRegistry
33
- } from "./chunk-XDXIBHPS.js";
44
+ } from "./chunk-BEFAI2HO.js";
45
+
46
+ // src/events.ts
47
+ function createKnowledgeEvent(input) {
48
+ const createdAt = (input.now ?? (() => /* @__PURE__ */ new Date()))().toISOString();
49
+ return {
50
+ id: stableId("evt", `${input.type}:${input.target ?? ""}:${createdAt}:${JSON.stringify(input.metadata ?? {})}`),
51
+ type: input.type,
52
+ createdAt,
53
+ actor: input.actor,
54
+ target: input.target,
55
+ metadata: input.metadata
56
+ };
57
+ }
58
+
59
+ // src/kb-store.ts
60
+ import { mkdir, readFile, writeFile } from "fs/promises";
61
+ import { dirname, join } from "path";
62
+ var MemoryKbStore = class {
63
+ sources = /* @__PURE__ */ new Map();
64
+ pages = /* @__PURE__ */ new Map();
65
+ events = [];
66
+ index = null;
67
+ async putSource(source) {
68
+ this.sources.set(source.id, clone(source));
69
+ }
70
+ async getSource(id) {
71
+ return clone(this.sources.get(id) ?? null);
72
+ }
73
+ async listSources() {
74
+ return [...this.sources.values()].map(clone);
75
+ }
76
+ async putPage(page) {
77
+ this.pages.set(page.id, clone(page));
78
+ }
79
+ async getPage(idOrPath) {
80
+ return clone(this.pages.get(idOrPath) ?? [...this.pages.values()].find((page) => page.path === idOrPath) ?? null);
81
+ }
82
+ async listPages() {
83
+ return [...this.pages.values()].map(clone);
84
+ }
85
+ async putIndex(index) {
86
+ this.index = clone(index);
87
+ }
88
+ async getIndex() {
89
+ if (this.index) return clone(this.index);
90
+ const pages = await this.listPages();
91
+ const sources = await this.listSources();
92
+ return { root: "memory", generatedAt: (/* @__PURE__ */ new Date()).toISOString(), sources, pages, graph: buildKnowledgeGraph(pages) };
93
+ }
94
+ async putEvent(event) {
95
+ this.events.push(clone(event));
96
+ }
97
+ async listEvents(query = {}) {
98
+ let out = this.events;
99
+ if (query.type) out = out.filter((event) => event.type === query.type);
100
+ if (query.target) out = out.filter((event) => event.target === query.target);
101
+ out = [...out].sort((a, b) => a.createdAt.localeCompare(b.createdAt));
102
+ return out.slice(-(query.limit ?? out.length)).map(clone);
103
+ }
104
+ };
105
+ var FileSystemKbStore = class extends MemoryKbStore {
106
+ constructor(dir) {
107
+ super();
108
+ this.dir = dir;
109
+ }
110
+ dir;
111
+ async putIndex(index) {
112
+ await super.putIndex(index);
113
+ await writeJson2(join(this.dir, "index.json"), index);
114
+ }
115
+ async getIndex() {
116
+ try {
117
+ return JSON.parse(await readFile(join(this.dir, "index.json"), "utf8"));
118
+ } catch {
119
+ return await super.getIndex();
120
+ }
121
+ }
122
+ async putEvent(event) {
123
+ await super.putEvent(event);
124
+ const events = await this.listEvents();
125
+ await writeJson2(join(this.dir, "events.json"), events);
126
+ }
127
+ };
128
+ async function writeJson2(path, value) {
129
+ await mkdir(dirname(path), { recursive: true });
130
+ await writeFile(path, JSON.stringify(value, null, 2) + "\n", "utf8");
131
+ }
132
+ function clone(value) {
133
+ return value == null ? value : JSON.parse(JSON.stringify(value));
134
+ }
135
+
136
+ // src/discovery.ts
137
+ function createLocalDiscoveryDispatcher(worker) {
138
+ return {
139
+ async dispatch(tasks, options = {}) {
140
+ const concurrency = Math.max(1, options.concurrency ?? 4);
141
+ const results = [];
142
+ let cursor = 0;
143
+ async function runNext() {
144
+ while (cursor < tasks.length) {
145
+ if (options.signal?.aborted) throw new Error("Discovery dispatch aborted");
146
+ const task = tasks[cursor++];
147
+ results.push(await worker.run(task, options.signal));
148
+ }
149
+ }
150
+ await Promise.all(Array.from({ length: Math.min(concurrency, tasks.length) }, runNext));
151
+ return results.sort((a, b) => tasks.findIndex((task) => task.id === a.taskId) - tasks.findIndex((task) => task.id === b.taskId));
152
+ }
153
+ };
154
+ }
34
155
 
35
156
  // src/chunking.ts
36
157
  var DEFAULT_OPTIONS = {
@@ -179,20 +300,159 @@ function knowledgeVariantFromCandidate(candidate, options = {}) {
179
300
  payload: candidate
180
301
  };
181
302
  }
303
+
304
+ // src/release.ts
305
+ import {
306
+ evaluateReleaseConfidence,
307
+ releaseTraceEvidenceFromMultiShotTrials,
308
+ validateRunRecord
309
+ } from "@tangle-network/agent-eval";
310
+ function knowledgeReleaseReportFromOptimization(result, options = {}) {
311
+ const trials = result.evolution.generations.flatMap((generation) => generation.trials);
312
+ const traceEvidence = releaseTraceEvidenceFromMultiShotTrials(trials);
313
+ const runRecords = (options.runRecords ?? [
314
+ ...result.gate?.candidateRuns ?? [],
315
+ ...result.gate?.baselineRuns ?? []
316
+ ]).map(validateRunRecord);
317
+ const scorecard = evaluateReleaseConfidence({
318
+ target: "agent-knowledge-base",
319
+ candidateId: result.promotedVariant.id,
320
+ baselineId: "baseline",
321
+ traces: traceEvidence,
322
+ runs: runRecords,
323
+ gateDecision: result.gate?.decision ?? null,
324
+ thresholds: {
325
+ requireCorpus: false,
326
+ requireHoldout: Boolean(result.gate),
327
+ minHoldoutRuns: result.gate ? 1 : 0,
328
+ minSearchRuns: 1,
329
+ minMeanScore: options.minScore ?? 0.7
330
+ }
331
+ });
332
+ const release = {
333
+ id: stableId("krel", `${result.promotedVariant.id}:${options.createdAt ?? (/* @__PURE__ */ new Date()).toISOString()}`),
334
+ candidateId: result.promotedVariant.id,
335
+ createdAt: options.createdAt ?? (/* @__PURE__ */ new Date()).toISOString(),
336
+ promoted: scorecard.status !== "fail" && result.promotedVariant.id === result.searchBestVariant.id,
337
+ scorecard,
338
+ runRecordIds: runRecords.map((record) => record.runId)
339
+ };
340
+ return {
341
+ release,
342
+ scorecard,
343
+ candidateRuns: result.gate?.candidateRuns ?? [],
344
+ baselineRuns: result.gate?.baselineRuns ?? []
345
+ };
346
+ }
347
+
348
+ // src/eval-readiness.ts
349
+ import {
350
+ acquisitionPlansForKnowledgeGaps,
351
+ scoreKnowledgeReadiness,
352
+ userQuestionsForKnowledgeGaps
353
+ } from "@tangle-network/agent-eval";
354
+ function buildEvalKnowledgeBundle(options) {
355
+ const searchLimit = options.searchLimit ?? 5;
356
+ const searchResultsByRequirement = {};
357
+ const requirements = options.specs.map((spec) => {
358
+ const results = searchKnowledge(options.index, spec.query, searchLimit);
359
+ searchResultsByRequirement[spec.id] = results;
360
+ return requirementFromSearch(spec, results);
361
+ });
362
+ const report = scoreKnowledgeReadiness({
363
+ taskId: options.taskId,
364
+ requirements,
365
+ userAnswers: options.userAnswers,
366
+ evidenceIds: requirements.flatMap((requirement) => requirement.evidenceIds),
367
+ claimIds: [],
368
+ wikiPageIds: unique(requirements.flatMap((requirement) => pageIdsFromResults(searchResultsByRequirement[requirement.id] ?? []))),
369
+ metadata: options.metadata
370
+ });
371
+ const questions = userQuestionsForKnowledgeGaps(report.blockingMissingRequirements);
372
+ const acquisitionPlans = acquisitionPlansForKnowledgeGaps([
373
+ ...report.blockingMissingRequirements,
374
+ ...report.nonBlockingGaps
375
+ ]);
376
+ return {
377
+ bundle: report.bundle,
378
+ report,
379
+ requirements,
380
+ searchResultsByRequirement,
381
+ questions,
382
+ acquisitionPlans
383
+ };
384
+ }
385
+ function requirementFromSearch(spec, results) {
386
+ const hitCount = results.length;
387
+ const sourceIds = unique(results.flatMap((result) => result.page.sourceIds));
388
+ const bestScore = results[0]?.normalizedScore ?? 0;
389
+ const sourceCoverage = spec.minSources ? Math.min(1, sourceIds.length / spec.minSources) : sourceIds.length > 0 ? 1 : 0;
390
+ const hitCoverage = spec.minHits ? Math.min(1, hitCount / spec.minHits) : hitCount > 0 ? 1 : 0;
391
+ const currentConfidence = round(Math.min(bestScore, sourceCoverage, hitCoverage));
392
+ return {
393
+ id: spec.id,
394
+ description: spec.description,
395
+ requiredFor: spec.requiredFor,
396
+ category: spec.category,
397
+ acquisitionMode: spec.acquisitionMode,
398
+ importance: spec.importance,
399
+ freshness: spec.freshness,
400
+ sensitivity: spec.sensitivity,
401
+ confidenceNeeded: spec.confidenceNeeded,
402
+ currentConfidence,
403
+ evidenceIds: unique([
404
+ ...sourceIds.map((sourceId) => `source:${sourceId}`),
405
+ ...results.map((result) => `page:${result.page.id}`)
406
+ ]),
407
+ fallbackPolicy: spec.fallbackPolicy ?? (spec.importance === "blocking" ? "block" : "continue_with_caveat"),
408
+ metadata: {
409
+ ...spec.metadata,
410
+ query: spec.query,
411
+ hitCount,
412
+ sourceCount: sourceIds.length,
413
+ bestNormalizedScore: bestScore
414
+ }
415
+ };
416
+ }
417
+ function pageIdsFromResults(results) {
418
+ return results.map((result) => result.page.id);
419
+ }
420
+ function unique(items) {
421
+ return [...new Set(items)];
422
+ }
423
+ function round(value) {
424
+ return Math.round(value * 1e3) / 1e3;
425
+ }
182
426
  export {
427
+ FileSystemKbStore,
428
+ KnowledgeBaseCandidateSchema,
429
+ KnowledgeEventSchema,
430
+ KnowledgeGraphEdgeSchema,
431
+ KnowledgeGraphNodeSchema,
432
+ KnowledgeIndexSchema,
433
+ KnowledgePageSchema,
434
+ MemoryKbStore,
435
+ SCAFFOLD_PAGE_BASENAMES,
436
+ SourceAnchorSchema,
437
+ SourceRecordSchema,
183
438
  WIKILINK_REGEX,
184
439
  addSourcePath,
185
440
  applyKnowledgeWriteBlocks,
186
441
  applyKnowledgeWriteBlocksFile,
442
+ buildEvalKnowledgeBundle,
187
443
  buildKnowledgeGraph,
188
444
  buildKnowledgeIndex,
189
445
  chunkMarkdown,
446
+ createKnowledgeEvent,
447
+ createLocalDiscoveryDispatcher,
190
448
  explainKnowledgeTarget,
191
449
  extractWikilinks,
192
450
  formatFrontmatter,
193
451
  initKnowledgeBase,
194
452
  inspectKnowledgeIndex,
195
453
  isSafeKnowledgePath,
454
+ isScaffoldPath,
455
+ knowledgeReleaseReportFromOptimization,
196
456
  knowledgeVariantFromCandidate,
197
457
  layoutFor,
198
458
  lintKnowledgeIndex,
@@ -212,6 +472,7 @@ export {
212
472
  stripFrontmatter,
213
473
  textSourceAdapter,
214
474
  tokenizeQuery,
475
+ validateKnowledgeIndex,
215
476
  writeJson,
216
477
  writeKnowledgeIndex,
217
478
  writeSourceRegistry