-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.ts
More file actions
835 lines (730 loc) · 25 KB
/
Copy pathcompiler.ts
File metadata and controls
835 lines (730 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
import { join } from "node:path";
import { createBackup } from "../backup.js";
import { DEFAULTS, GRAPH_FILE, INDEX_FILE } from "../constants.js";
import { hash } from "../hash.js";
import { countWords } from "../ingest/normalize.js";
import { withLock } from "../lockfile.js";
import type {
CompileResult,
FileOperation,
LLMProvider,
Manifest,
SourceTokenUsage,
VaultConfig,
} from "../types.js";
import {
appendLog,
deleteFile,
listImageAssets,
loadManifest,
readIndex,
readRaw,
readWiki,
saveManifest,
writeWiki,
} from "../vault.js";
import { buildLinkGraph, generateGraphMd } from "./backlinks.js";
import { CompileCache } from "./cache.js";
import { extractWikilinks, parseCompileOutput, parseFrontmatter } from "./diff.js";
import { enrichCrossReferences } from "./enrichment.js";
import { computeStats, generateIndexMd } from "./index-manager.js";
import { compileSystemPrompt, compileUserPrompt } from "./prompts.js";
/** Emitted for each article as it is created, updated, or deleted. */
export interface ArticleEvent {
op: "create" | "update" | "delete";
title: string;
path: string;
source: string;
}
export interface CompileOptions {
/** Recompile all sources regardless of state */
force?: boolean;
/** Only compile this specific source path */
sourceFilter?: string;
/** Max sources to compile in this pass */
maxSources?: number;
/** Don't actually write files, just return what would happen */
dryRun?: boolean;
/** Callback for progress updates */
onProgress?: (msg: string) => void;
/** Callback fired for each article as it is processed */
onArticle?: (event: ArticleEvent) => void;
}
// ─── Token estimation ──────────────────────────────────────────
/** Rough token count estimate from character length */
function estimateTokens(text: string): number {
return Math.ceil(text.length * DEFAULTS.tokensPerChar);
}
// ─── Source summarization ──────────────────────────────────────
/**
* Summarize a large source by chunking and keeping key sections.
* Preserves title/headings and truncates body with a note.
*/
function truncateSource(content: string, maxTokens: number): { text: string; truncated: boolean } {
const estimated = estimateTokens(content);
if (estimated <= maxTokens) {
return { text: content, truncated: false };
}
// Keep approximately maxTokens worth of characters
const maxChars = Math.floor(maxTokens / DEFAULTS.tokensPerChar);
// Try to keep the beginning (title, headings) and cut at a paragraph boundary
const cutPoint = content.lastIndexOf("\n\n", maxChars);
const sliceEnd = cutPoint > maxChars * 0.5 ? cutPoint : maxChars;
const truncated = content.slice(0, sliceEnd);
return {
text: `${truncated}\n\n---\n[Source truncated: original was ~${estimated} tokens, showing first ~${estimateTokens(truncated)} tokens]`,
truncated: true,
};
}
// ─── Smart context selection ───────────────────────────────────
/**
* For large vaults, send article summaries instead of full content.
* Returns compact context when existing articles exceed the budget.
*/
function selectContext(
existingArticles: { path: string; content: string }[],
manifest: Manifest,
contextBudget: number,
): { path: string; content: string }[] {
if (existingArticles.length === 0) return [];
const totalTokens = existingArticles.reduce((sum, a) => sum + estimateTokens(a.content), 0);
// If existing articles fit in the budget, send them in full
if (totalTokens <= contextBudget) {
return existingArticles;
}
// Otherwise, send compact summaries
return existingArticles.map((article) => {
const relPath = article.path.replace(/^wiki\//, "").replace(/\.md$/, "");
const slug = relPath.split("/").pop() ?? relPath;
const entry = manifest.articles[slug];
if (entry) {
const summary = `---\ntitle: ${slug}\ncategory: ${entry.category}\ntags: [${entry.tags.join(", ")}]\nsummary: ${entry.summary}\n---\n\n(Full article omitted for context budget — ${entry.wordCount} words)`;
return { path: article.path, content: summary };
}
return article;
});
}
// ─── Duplicate detection ───────────────────────────────────────
/**
* Check if newly created articles overlap with existing ones.
* Returns merge suggestions as warnings.
*/
function detectDuplicateArticles(operations: FileOperation[], manifest: Manifest): string[] {
const warnings: string[] = [];
for (const op of operations) {
if (op.op !== "create" || !op.content) continue;
const { frontmatter } = parseFrontmatter(op.content);
const newSlug = (frontmatter.slug as string) ?? "";
const newTags = Array.isArray(frontmatter.tags) ? (frontmatter.tags as string[]) : [];
const newTitle = ((frontmatter.title as string) ?? "").toLowerCase();
for (const [existingSlug, existingArticle] of Object.entries(manifest.articles)) {
if (existingSlug === newSlug) continue; // same article = update, not dup
// Check title similarity (simple substring match)
const existingTitle = existingArticle.summary.split(".")[0]?.toLowerCase() ?? "";
const titleOverlap =
(newTitle && existingTitle && newTitle.includes(existingTitle)) ||
(newTitle && existingTitle?.includes(newTitle));
// Check tag overlap (>= 3 shared tags = likely same topic)
const sharedTags = newTags.filter((t) => existingArticle.tags.includes(t));
if (titleOverlap || sharedTags.length >= 3) {
warnings.push(
`Potential duplicate: new "${newSlug}" overlaps with existing "${existingSlug}" (${sharedTags.length} shared tags${titleOverlap ? ", similar title" : ""})`,
);
}
}
}
return warnings;
}
// ─── Retry with adjusted prompt ────────────────────────────────
const MAX_RETRIES = 2;
async function compileWithRetry(
provider: LLMProvider,
system: string,
userPrompt: string,
maxTokens: number,
cache: CompileCache | null,
onProgress?: (msg: string) => void,
): Promise<{ content: string; inputTokens: number; outputTokens: number; cached: boolean }> {
// Check cache first
if (cache) {
const cacheKey = await cache.key(system, userPrompt);
const cached = await cache.get(cacheKey);
if (cached) {
return {
content: cached.content,
inputTokens: cached.usage.inputTokens,
outputTokens: cached.usage.outputTokens,
cached: true,
};
}
}
let lastError: Error | null = null;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
const prompt =
attempt === 0
? userPrompt
: `${userPrompt}\n\nIMPORTANT: Your previous response was not valid JSON. You MUST respond with ONLY a raw JSON array of file operations. No markdown, no explanation, no code fences. Just the JSON array starting with [ and ending with ].`;
try {
const result = await provider.complete({
system,
messages: [{ role: "user", content: prompt }],
temperature: 0,
maxTokens,
});
// Validate it parses before returning
parseCompileOutput(result.content);
// Cache the successful response
if (cache) {
const cacheKey = await cache.key(system, userPrompt);
await cache.set(cacheKey, result.content, result.usage);
}
return {
content: result.content,
inputTokens: result.usage.inputTokens,
outputTokens: result.usage.outputTokens,
cached: false,
};
} catch (err) {
lastError = err as Error;
if (attempt < MAX_RETRIES) {
onProgress?.(`Parse failed (attempt ${attempt + 1}/${MAX_RETRIES + 1}), retrying...`);
}
}
}
throw lastError!;
}
// ─── Single source compilation ─────────────────────────────────
interface SourceCompileResult {
operations: FileOperation[];
created: number;
updated: number;
deleted: number;
tokenUsage: SourceTokenUsage;
warnings: string[];
}
async function compileSingleSource(
root: string,
provider: LLMProvider,
manifest: Manifest,
sourceId: string,
sourcePath: string,
config: VaultConfig,
indexContent: string,
cache: CompileCache | null,
options: CompileOptions & { onArticle?: (event: ArticleEvent) => void },
imageAssets?: string[],
): Promise<SourceCompileResult> {
const categories = config.compile.categories;
const contextWindow = config.compile.context_window;
const maxSourceTokens = config.compile.max_source_tokens;
const warnings: string[] = [];
// Read the raw source content
let sourceContent = await readRaw(root, sourcePath);
// Estimate tokens and warn/truncate if needed
const sourceTokens = estimateTokens(sourceContent);
if (sourceTokens > contextWindow * 0.8) {
warnings.push(
`Source "${sourcePath}" (~${sourceTokens} tokens) approaches the context window (${contextWindow}). Output quality may degrade.`,
);
}
let truncated = false;
if (sourceTokens > maxSourceTokens) {
const result = truncateSource(sourceContent, maxSourceTokens);
sourceContent = result.text;
truncated = result.truncated;
if (truncated) {
warnings.push(
`Source "${sourcePath}" was truncated from ~${sourceTokens} to ~${maxSourceTokens} tokens.`,
);
}
}
// Read existing articles this source produced (for context)
const rawExistingArticles = await loadExistingArticles(root, manifest, sourceId);
// Smart context selection: use summaries if articles are too large
const contextBudget = Math.floor(contextWindow * 0.3); // 30% of context for existing articles
const existingArticles = selectContext(rawExistingArticles, manifest, contextBudget);
// Build the compile prompt
const today = new Date().toISOString().split("T")[0]!;
const userPrompt = compileUserPrompt({
indexContent,
sourceContent,
sourcePath: `raw/${sourcePath}`,
existingArticles,
today,
});
// Call the LLM with retry and cache
const system = compileSystemPrompt(categories, {
imageAssets: imageAssets && imageAssets.length > 0 ? imageAssets : undefined,
});
const result = await compileWithRetry(
provider,
system,
userPrompt,
8192,
cache,
options.onProgress,
);
const operations = parseCompileOutput(result.content);
// Detect potential duplicates
const dupWarnings = detectDuplicateArticles(operations, manifest);
warnings.push(...dupWarnings);
let created = 0;
let updated = 0;
let deleted = 0;
if (!options.dryRun) {
const producedArticles: string[] = [];
for (const op of operations) {
const wikiRelPath = op.path.replace(/^wiki\//, "");
if (op.op === "create" || op.op === "update") {
if (!op.content) continue;
await writeWiki(root, wikiRelPath, op.content);
producedArticles.push(op.path);
// Update article entry in manifest
const { frontmatter, body } = parseFrontmatter(op.content);
const articleSlug = (frontmatter.slug as string) ?? wikiRelPath.replace(/\.md$/, "");
const contentHash = await hash(op.content);
const wikilinks = extractWikilinks(op.content);
const now = new Date().toISOString();
manifest.articles[articleSlug] = {
hash: contentHash,
createdAt: op.op === "create" ? now : (manifest.articles[articleSlug]?.createdAt ?? now),
lastUpdated: now,
derivedFrom: [`raw/${sourcePath}`],
backlinks: [], // will be computed after all sources are compiled
forwardLinks: wikilinks,
tags: Array.isArray(frontmatter.tags) ? (frontmatter.tags as string[]) : [],
summary: (frontmatter.summary as string) ?? "",
wordCount: countWords(body),
category: normalizeCategory((frontmatter.category as string) ?? "topic"),
};
const articleTitle = (frontmatter.title as string) ?? articleSlug;
options.onArticle?.({ op: op.op, title: articleTitle, path: op.path, source: sourcePath });
if (op.op === "create") created++;
else updated++;
} else if (op.op === "delete") {
const fullPath = join(root, op.path);
await deleteFile(fullPath);
deleted++;
// Remove from manifest
const slug = wikiRelPath.replace(/\.md$/, "");
const deletedTitle = manifest.articles[slug]?.summary?.split(".")[0] ?? slug;
options.onArticle?.({
op: "delete",
title: deletedTitle,
path: op.path,
source: sourcePath,
});
delete manifest.articles[slug];
}
}
// Update source entry
manifest.sources[sourceId]!.lastCompiled = new Date().toISOString();
manifest.sources[sourceId]!.producedArticles = producedArticles;
} else {
for (const op of operations) {
if (op.op === "create" || op.op === "update") {
const { frontmatter } = op.content
? parseFrontmatter(op.content)
: { frontmatter: {} as Record<string, unknown> };
const slug =
op.path
.replace(/^wiki\//, "")
.replace(/\.md$/, "")
.split("/")
.pop() ?? op.path;
const title = ((frontmatter as Record<string, unknown>).title as string) ?? slug;
options.onArticle?.({ op: op.op, title, path: op.path, source: sourcePath });
} else if (op.op === "delete") {
const slug =
op.path
.replace(/^wiki\//, "")
.replace(/\.md$/, "")
.split("/")
.pop() ?? op.path;
options.onArticle?.({ op: "delete", title: slug, path: op.path, source: sourcePath });
}
if (op.op === "create") created++;
else if (op.op === "update") updated++;
else if (op.op === "delete") deleted++;
}
}
return {
operations,
created,
updated,
deleted,
tokenUsage: {
sourceId,
sourcePath,
inputTokens: result.inputTokens,
outputTokens: result.outputTokens,
cached: result.cached,
truncated,
},
warnings,
};
}
// ─── Main compile function ─────────────────────────────────────
/**
* Compile pending raw sources into wiki articles.
*/
export async function compileVault(
root: string,
provider: LLMProvider,
config: VaultConfig,
options: CompileOptions = {},
): Promise<CompileResult> {
// Dry runs don't write — skip locking and backups
if (options.dryRun) {
return compileVaultInner(root, provider, config, options);
}
return withLock(root, "compile", async () => {
// Back up manifest before force-recompile (destructive operation)
if (options.force) {
await createBackup(root);
}
return compileVaultInner(root, provider, config, options);
});
}
async function compileVaultInner(
root: string,
provider: LLMProvider,
config: VaultConfig,
options: CompileOptions,
): Promise<CompileResult> {
const manifest = await loadManifest(root);
// Find sources that need compilation
const pendingSources = findPendingSources(manifest, options);
if (pendingSources.length === 0) {
return {
sourcesCompiled: 0,
articlesCreated: 0,
articlesUpdated: 0,
articlesDeleted: 0,
articlesEnriched: 0,
operations: [],
};
}
// Limit sources per pass
const maxSources = options.maxSources ?? config.compile.max_sources_per_pass;
const sourcesToCompile = pendingSources.slice(0, maxSources);
// Read current INDEX.md for context
const indexContent = await readIndex(root);
// Load available image assets for reference in articles
const imageAssets = await listImageAssets(root);
// Initialize compile cache
const cache = new CompileCache(root, {
enabled: config.cache.enabled,
ttlHours: config.cache.ttl_hours,
});
let totalCreated = 0;
let totalUpdated = 0;
let totalDeleted = 0;
let totalEnriched = 0;
const allOperations: CompileResult["operations"] = [];
const newlyChangedSlugs = new Set<string>();
const perSourceUsage: SourceTokenUsage[] = [];
const allWarnings: string[] = [];
// Token budget tracking
let totalInputTokens = 0;
const maxTokensPerPass = config.compile.max_tokens_per_pass;
// Compile sources (parallel or sequential)
const useParallel = config.compile.parallel && sourcesToCompile.length > 1;
if (useParallel) {
const maxParallel = config.compile.max_parallel;
options.onProgress?.(
`Compiling ${sourcesToCompile.length} sources (${maxParallel} parallel)...`,
);
// Process in batches
for (let i = 0; i < sourcesToCompile.length; i += maxParallel) {
// Check token budget before starting a new batch
if (maxTokensPerPass && totalInputTokens >= maxTokensPerPass) {
allWarnings.push(
`Token budget exhausted (${totalInputTokens}/${maxTokensPerPass}). Stopping after ${i} sources.`,
);
break;
}
const batch = sourcesToCompile.slice(i, i + maxParallel);
const results = await Promise.allSettled(
batch.map(([sourceId, sourcePath]) =>
compileSingleSource(
root,
provider,
manifest,
sourceId,
sourcePath,
config,
indexContent,
cache,
options,
imageAssets,
),
),
);
for (let j = 0; j < results.length; j++) {
const result = results[j]!;
const [_sourceId, sourcePath] = batch[j]!;
if (result.status === "fulfilled") {
const r = result.value;
totalCreated += r.created;
totalUpdated += r.updated;
totalDeleted += r.deleted;
allOperations.push(...r.operations);
perSourceUsage.push(r.tokenUsage);
allWarnings.push(...r.warnings);
totalInputTokens += r.tokenUsage.inputTokens;
// Track changed slugs for enrichment
for (const op of r.operations) {
if (op.op === "create" || op.op === "update") {
const slug = op.path
.replace(/^wiki\//, "")
.replace(/\.md$/, "")
.split("/")
.pop();
if (slug) newlyChangedSlugs.add(slug);
}
}
} else {
const msg = (result.reason as Error).message ?? String(result.reason);
if (
msg.includes("401") ||
msg.includes("404") ||
msg.includes("authentication") ||
msg.includes("No LLM provider")
) {
throw result.reason;
}
allWarnings.push(`Failed to compile ${sourcePath}: ${msg}`);
options.onProgress?.(`Failed to compile ${sourcePath}: ${msg}`);
}
}
}
} else {
// Sequential compilation (original behavior)
for (const [sourceId, sourcePath] of sourcesToCompile) {
// Check token budget
if (maxTokensPerPass && totalInputTokens >= maxTokensPerPass) {
allWarnings.push(
`Token budget exhausted (${totalInputTokens}/${maxTokensPerPass}). Skipping remaining sources.`,
);
break;
}
options.onProgress?.(`Compiling ${sourcePath}...`);
try {
const result = await compileSingleSource(
root,
provider,
manifest,
sourceId,
sourcePath,
config,
indexContent,
cache,
options,
imageAssets,
);
totalCreated += result.created;
totalUpdated += result.updated;
totalDeleted += result.deleted;
allOperations.push(...result.operations);
perSourceUsage.push(result.tokenUsage);
allWarnings.push(...result.warnings);
totalInputTokens += result.tokenUsage.inputTokens;
// Track changed slugs for enrichment
for (const op of result.operations) {
if (op.op === "create" || op.op === "update") {
const slug = op.path
.replace(/^wiki\//, "")
.replace(/\.md$/, "")
.split("/")
.pop();
if (slug) newlyChangedSlugs.add(slug);
}
}
} catch (err) {
const msg = (err as Error).message ?? String(err);
if (
msg.includes("401") ||
msg.includes("404") ||
msg.includes("authentication") ||
msg.includes("No LLM provider")
) {
throw err;
}
allWarnings.push(`Failed to compile ${sourcePath}: ${msg}`);
options.onProgress?.(`Failed to compile ${sourcePath}: ${msg}`);
}
}
}
if (!options.dryRun) {
// Cross-reference enrichment pass
if (config.compile.enrich_cross_refs !== false && newlyChangedSlugs.size > 0) {
options.onProgress?.("Enriching cross-references...");
try {
const enrichResult = await enrichCrossReferences(
root,
provider,
manifest,
newlyChangedSlugs,
{
maxArticles: config.compile.max_enrich_articles,
onProgress: options.onProgress,
},
);
totalEnriched += enrichResult.articlesEnriched;
allOperations.push(...enrichResult.operations);
} catch {
options.onProgress?.("Cross-reference enrichment failed, continuing...");
}
}
// Rebuild link graph (backlinks)
options.onProgress?.("Updating backlinks...");
const graph = await buildLinkGraph(root);
// Update backlinks in manifest
for (const [slug, backlinksSet] of graph.backlinks) {
if (manifest.articles[slug]) {
manifest.articles[slug]!.backlinks = [...backlinksSet];
}
}
for (const [slug, forwardSet] of graph.forwardLinks) {
if (manifest.articles[slug]) {
manifest.articles[slug]!.forwardLinks = [...forwardSet];
}
}
// Regenerate INDEX.md
if (config.compile.auto_index) {
options.onProgress?.("Updating INDEX.md...");
const indexMd = await generateIndexMd(root);
await writeWiki(root, INDEX_FILE, indexMd);
}
// Regenerate GRAPH.md
if (config.compile.auto_graph) {
options.onProgress?.("Updating GRAPH.md...");
const graphMd = generateGraphMd(graph);
await writeWiki(root, GRAPH_FILE, graphMd);
}
// Update manifest stats
const stats = await computeStats(root);
manifest.stats.totalArticles = stats.totalArticles;
manifest.stats.totalWords = stats.totalWords;
manifest.stats.totalSources = Object.keys(manifest.sources).length;
manifest.vault.lastCompiled = new Date().toISOString();
await saveManifest(root, manifest);
// Log compile activity
const parts = [`${sourcesToCompile.length} sources compiled`];
if (totalCreated > 0) parts.push(`${totalCreated} articles created`);
if (totalUpdated > 0) parts.push(`${totalUpdated} articles updated`);
if (totalEnriched > 0) parts.push(`${totalEnriched} articles enriched`);
const cachedCount = perSourceUsage.filter((u) => u.cached).length;
if (cachedCount > 0) parts.push(`${cachedCount} cache hits`);
await appendLog(root, "compile", parts.join(", "));
}
// Compute total token usage
const totalOutputTokens = perSourceUsage.reduce((sum, u) => sum + u.outputTokens, 0);
return {
sourcesCompiled: perSourceUsage.length,
articlesCreated: totalCreated,
articlesUpdated: totalUpdated,
articlesDeleted: totalDeleted,
articlesEnriched: totalEnriched,
operations: allOperations,
tokenUsage: {
totalInputTokens,
totalOutputTokens,
perSource: perSourceUsage,
},
warnings: allWarnings.length > 0 ? allWarnings : undefined,
};
}
/**
* Find sources that need compilation.
*/
function findPendingSources(manifest: Manifest, options: CompileOptions): [string, string][] {
const pending: [string, string][] = [];
for (const [sourceId, source] of Object.entries(manifest.sources)) {
// If filtering to a specific source
if (options.sourceFilter) {
const matchesId = sourceId === options.sourceFilter;
const matchesPath = source.producedArticles.some((p) => p.includes(options.sourceFilter!));
if (!matchesId && !matchesPath) continue;
}
// Determine the raw file path from sourceId
// Source was written to raw/{category}/{slug}.md
const rawPath = findRawPath(manifest, sourceId);
if (!rawPath) continue;
if (options.force || !source.lastCompiled || source.lastCompiled < source.ingestedAt) {
pending.push([sourceId, rawPath]);
}
}
return pending;
}
/**
* Find the raw file path for a source.
* Sources are tracked by ID; we need to find which raw/ file they correspond to.
*/
function findRawPath(manifest: Manifest, sourceId: string): string | null {
const source = manifest.sources[sourceId];
if (!source) return null;
// The raw path is derived from the source metadata
const title = source.metadata.title ?? sourceId;
const slug = title
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, "")
.replace(/\s+/g, "-")
.replace(/-+/g, "-")
.replace(/^-|-$/g, "")
.slice(0, 80);
const category = categoryForSourceType(source.sourceType);
return `${category}/${slug}.md`;
}
function categoryForSourceType(sourceType: string): string {
switch (sourceType) {
case "pdf":
return "papers";
case "youtube":
return "transcripts";
case "github":
return "repos";
case "image":
return "images";
default:
return "articles";
}
}
type ArticleCategory = "concept" | "topic" | "reference" | "output";
/** Normalize category from plural directory name to singular schema value */
function normalizeCategory(raw: string): ArticleCategory {
const map: Record<string, ArticleCategory> = {
concepts: "concept",
concept: "concept",
topics: "topic",
topic: "topic",
references: "reference",
reference: "reference",
outputs: "output",
output: "output",
};
return map[raw.toLowerCase()] ?? "topic";
}
/**
* Load existing wiki articles that a source previously produced.
*/
async function loadExistingArticles(
root: string,
manifest: Manifest,
sourceId: string,
): Promise<{ path: string; content: string }[]> {
const source = manifest.sources[sourceId];
if (!source?.producedArticles.length) return [];
const articles: { path: string; content: string }[] = [];
for (const articlePath of source.producedArticles) {
try {
const relPath = articlePath.replace(/^wiki\//, "");
const content = await readWiki(root, relPath);
articles.push({ path: articlePath, content });
} catch {
// Article might have been deleted
}
}
return articles;
}