@agentproto/corpus-cli 0.4.0 → 0.5.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/cli.mjs +191 -34
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.ts +67 -2
- package/dist/index.mjs +88 -2
- package/dist/index.mjs.map +1 -1
- package/dist/specs/resources/aip-45/draft/AGENT-CLI.schema.json +21 -2
- package/package.json +1 -1
package/dist/cli.mjs
CHANGED
|
@@ -5,10 +5,10 @@ import { createRequire } from 'module';
|
|
|
5
5
|
import path, { dirname, resolve, basename, join, extname } from 'path';
|
|
6
6
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
7
7
|
import { z } from 'zod';
|
|
8
|
-
import { randomBytes, randomUUID } from 'crypto';
|
|
8
|
+
import { randomBytes, randomUUID, createHash } from 'crypto';
|
|
9
9
|
import { readFileSync, existsSync } from 'fs';
|
|
10
|
-
import { buildDistillPrompt, parseItems, isRefinedKind, SyncRunner, resolveKnowledge, DistillRunner, systemClock, WebImporter, ImporterRunner, CorpusEventEmitter, CorpusWorkspaceReader, CorpusLinter, CorpusValidator, ReadOnlyFs, normalizeLanguageTag } from '@agentproto/corpus';
|
|
11
|
-
import
|
|
10
|
+
import { buildDistillPrompt, parseItems, isRefinedKind, SyncRunner, resolveKnowledge, DistillIndex, DistillRunner, systemClock, WebImporter, ImporterRunner, CorpusEventEmitter, CorpusWorkspaceReader, CorpusLinter, CorpusValidator, ReadOnlyFs, normalizeLanguageTag, lensAspect } from '@agentproto/corpus';
|
|
11
|
+
import matter3 from 'gray-matter';
|
|
12
12
|
import { execFile } from 'child_process';
|
|
13
13
|
import { promisify } from 'util';
|
|
14
14
|
import { parseClaudeJsonOutput, spawnWithStdin } from '@agentproto/cli-exec';
|
|
@@ -1868,7 +1868,7 @@ async function scanIngestedUrls(workspaceRoot) {
|
|
|
1868
1868
|
if (!e.isFile() || !e.name.endsWith(".md")) continue;
|
|
1869
1869
|
try {
|
|
1870
1870
|
const raw = await readFile(join(e.parentPath, e.name), "utf-8");
|
|
1871
|
-
const fm = SOURCE_URL_FRONTMATTER.parse(
|
|
1871
|
+
const fm = SOURCE_URL_FRONTMATTER.parse(matter3(raw).data);
|
|
1872
1872
|
const url = fm.metadata?.corpus?.originalUrl ?? fm.metadata?.corpus?.importerSourceUrl;
|
|
1873
1873
|
if (url) seen.add(url);
|
|
1874
1874
|
} catch {
|
|
@@ -2571,6 +2571,91 @@ usage \u2014 ${records.length} metered call(s):
|
|
|
2571
2571
|
};
|
|
2572
2572
|
}
|
|
2573
2573
|
|
|
2574
|
+
// src/lenses/builtin.ts
|
|
2575
|
+
var CRAFT_LENS = {
|
|
2576
|
+
id: "craft",
|
|
2577
|
+
label: "Writing craft",
|
|
2578
|
+
aspect: "craft",
|
|
2579
|
+
mode: "log",
|
|
2580
|
+
// The distiller emits {kind,title,body,...}; a craft move is a technique
|
|
2581
|
+
// (pattern), a rule (principle), a shallow default it avoids (critique), or a
|
|
2582
|
+
// specific instance (example). "summary" is dropped — a summary is topic
|
|
2583
|
+
// content, not a transferable craft move.
|
|
2584
|
+
kinds: ["pattern", "principle", "critique", "example"],
|
|
2585
|
+
prompt: [
|
|
2586
|
+
"Extract TRANSFERABLE WRITING-CRAFT MOVES from the source \u2014 reusable techniques a writer could apply to ANY topic. Study HOW the piece is written; IGNORE what it is about. Do NOT extract facts, claims, or lessons about the subject matter \u2014 only the craft.",
|
|
2587
|
+
"",
|
|
2588
|
+
"Focus on: how the first line hooks; structure and transitions; how depth is manufactured (specificity, tension, the earned turn, surprising-but-true facts); how voice and edge are earned without cringe; sentence rhythm and variation; how it ends. Prioritise the moves that separate memorable writing from generic filler.",
|
|
2589
|
+
"",
|
|
2590
|
+
"For each move:",
|
|
2591
|
+
'- kind: "pattern" for a technique, "principle" for a rule, "critique" for a shallow/generic default the move avoids, "example" for a specific instance.',
|
|
2592
|
+
'- title: name the move crisply and imperatively (e.g. "Delay the thesis until after a concrete image").',
|
|
2593
|
+
"- body: state the reusable technique in 1-2 topic-agnostic sentences; name the generic / AI-default it beats; and include a <=30-word verbatim snippet from the source where the move happens (omit the quote only when the move is purely structural)."
|
|
2594
|
+
].join("\n")
|
|
2595
|
+
};
|
|
2596
|
+
var BUILTIN_LENSES = {
|
|
2597
|
+
[CRAFT_LENS.id]: CRAFT_LENS
|
|
2598
|
+
};
|
|
2599
|
+
function builtinLensIds() {
|
|
2600
|
+
return Object.keys(BUILTIN_LENSES);
|
|
2601
|
+
}
|
|
2602
|
+
|
|
2603
|
+
// src/lenses/resolve.ts
|
|
2604
|
+
var LensError = class extends Error {
|
|
2605
|
+
constructor(message) {
|
|
2606
|
+
super(message);
|
|
2607
|
+
this.name = "LensError";
|
|
2608
|
+
}
|
|
2609
|
+
};
|
|
2610
|
+
var LENS_FRONTMATTER = z.object({
|
|
2611
|
+
id: z.string().optional().catch(void 0),
|
|
2612
|
+
label: z.string().optional().catch(void 0),
|
|
2613
|
+
aspect: z.string().optional().catch(void 0),
|
|
2614
|
+
prompt: z.string().optional().catch(void 0),
|
|
2615
|
+
mode: z.string().optional().catch(void 0),
|
|
2616
|
+
kinds: z.array(z.string()).optional().catch(void 0),
|
|
2617
|
+
synthesisPath: z.string().optional().catch(void 0)
|
|
2618
|
+
}).loose();
|
|
2619
|
+
function parseLensDoc(raw, fallbackId) {
|
|
2620
|
+
const parsed = matter3(raw);
|
|
2621
|
+
const fm = LENS_FRONTMATTER.parse(parsed.data);
|
|
2622
|
+
const prompt = (parsed.content.trim() || fm.prompt?.trim()) ?? "";
|
|
2623
|
+
if (!prompt) {
|
|
2624
|
+
throw new LensError(
|
|
2625
|
+
`lens "${fm.id ?? fallbackId}" has no prompt \u2014 put the extraction instruction in the markdown body or a "prompt:" frontmatter key.`
|
|
2626
|
+
);
|
|
2627
|
+
}
|
|
2628
|
+
const kinds = (fm.kinds ?? []).filter(isRefinedKind);
|
|
2629
|
+
const mode = fm.mode === "synthesis" ? "synthesis" : "log";
|
|
2630
|
+
return {
|
|
2631
|
+
id: fm.id ?? fallbackId,
|
|
2632
|
+
label: fm.label ?? fm.id ?? fallbackId,
|
|
2633
|
+
prompt,
|
|
2634
|
+
mode,
|
|
2635
|
+
...fm.aspect ? { aspect: fm.aspect } : {},
|
|
2636
|
+
...kinds.length > 0 ? { kinds } : {},
|
|
2637
|
+
...fm.synthesisPath ? { synthesisPath: fm.synthesisPath } : {}
|
|
2638
|
+
};
|
|
2639
|
+
}
|
|
2640
|
+
async function resolveLens(id, workspaceRoot) {
|
|
2641
|
+
const wsPath = join(workspaceRoot, "lenses", `${id}.md`);
|
|
2642
|
+
if (existsSync(wsPath)) {
|
|
2643
|
+
return parseLensDoc(await readFile(wsPath, "utf8"), id);
|
|
2644
|
+
}
|
|
2645
|
+
const builtin = BUILTIN_LENSES[id];
|
|
2646
|
+
if (builtin) return builtin;
|
|
2647
|
+
throw new LensError(
|
|
2648
|
+
`unknown lens "${id}". Built-in: ${builtinLensIds().join(", ") || "(none)"}. Declare one at ${join("lenses", `${id}.md`)} in the workspace, or pass --lens-file <path>.`
|
|
2649
|
+
);
|
|
2650
|
+
}
|
|
2651
|
+
async function resolveLensFile(path6) {
|
|
2652
|
+
if (!existsSync(path6)) {
|
|
2653
|
+
throw new LensError(`--lens-file not found: ${path6}`);
|
|
2654
|
+
}
|
|
2655
|
+
const fallbackId = basename(path6).replace(/\.[^.]+$/, "");
|
|
2656
|
+
return parseLensDoc(await readFile(path6, "utf8"), fallbackId);
|
|
2657
|
+
}
|
|
2658
|
+
|
|
2574
2659
|
// src/commands/distill.ts
|
|
2575
2660
|
var SOURCE_FRONTMATTER = z.object({
|
|
2576
2661
|
id: z.string().optional().catch(void 0),
|
|
@@ -2589,7 +2674,7 @@ var DEFAULT_ENGINE = "anthropic-api";
|
|
|
2589
2674
|
async function readEntryLayout(target) {
|
|
2590
2675
|
try {
|
|
2591
2676
|
const raw = await readFile(join(target, "KNOWLEDGE.md"), "utf8");
|
|
2592
|
-
const layout =
|
|
2677
|
+
const layout = matter3(raw).data?.metadata;
|
|
2593
2678
|
const corpus = layout?.["corpus"];
|
|
2594
2679
|
const v = corpus?.["entryLayout"];
|
|
2595
2680
|
return v === "flat" || v === "dated" ? v : void 0;
|
|
@@ -2631,7 +2716,9 @@ function parse4(args) {
|
|
|
2631
2716
|
throttleMs: 1e3,
|
|
2632
2717
|
model: void 0,
|
|
2633
2718
|
engine: DEFAULT_ENGINE,
|
|
2634
|
-
lang: void 0
|
|
2719
|
+
lang: void 0,
|
|
2720
|
+
lens: void 0,
|
|
2721
|
+
lensFile: void 0
|
|
2635
2722
|
};
|
|
2636
2723
|
for (let i = 0; i < args.length; i++) {
|
|
2637
2724
|
const a = args[i];
|
|
@@ -2661,6 +2748,12 @@ function parse4(args) {
|
|
|
2661
2748
|
case "--lang":
|
|
2662
2749
|
out.lang = next();
|
|
2663
2750
|
break;
|
|
2751
|
+
case "--lens":
|
|
2752
|
+
out.lens = next();
|
|
2753
|
+
break;
|
|
2754
|
+
case "--lens-file":
|
|
2755
|
+
out.lensFile = next();
|
|
2756
|
+
break;
|
|
2664
2757
|
default:
|
|
2665
2758
|
if (!a.startsWith("-") && out.workspace === void 0) out.workspace = a;
|
|
2666
2759
|
}
|
|
@@ -2680,14 +2773,16 @@ async function readSources(root) {
|
|
|
2680
2773
|
if (!e.isFile() || !e.name.endsWith(".md")) continue;
|
|
2681
2774
|
const path6 = join(e.parentPath, e.name);
|
|
2682
2775
|
try {
|
|
2683
|
-
const parsed =
|
|
2776
|
+
const parsed = matter3(await readFile(path6, "utf-8"));
|
|
2684
2777
|
const fm = SOURCE_FRONTMATTER.parse(parsed.data);
|
|
2685
2778
|
if (!fm.id || !parsed.content.trim()) continue;
|
|
2779
|
+
const body = parsed.content.trim();
|
|
2686
2780
|
out.push({
|
|
2687
2781
|
path: path6,
|
|
2688
2782
|
id: fm.id,
|
|
2689
2783
|
title: fm.title ?? fm.id,
|
|
2690
|
-
body
|
|
2784
|
+
body,
|
|
2785
|
+
contentHash: "sha256:" + createHash("sha256").update(body).digest("hex"),
|
|
2691
2786
|
...fm.tags ? { tags: fm.tags } : {},
|
|
2692
2787
|
...fm.metadata?.corpus?.access ? { access: fm.metadata.corpus.access } : {},
|
|
2693
2788
|
...fm.metadata?.corpus?.domain ? { domain: fm.metadata.corpus.domain } : {}
|
|
@@ -2710,7 +2805,7 @@ async function scanDistilledSourceIds(root) {
|
|
|
2710
2805
|
if (!e.isFile() || !e.name.endsWith(".md")) continue;
|
|
2711
2806
|
try {
|
|
2712
2807
|
const fm = ENTRY_SOURCES_FRONTMATTER.parse(
|
|
2713
|
-
|
|
2808
|
+
matter3(await readFile(join(e.parentPath, e.name), "utf-8")).data
|
|
2714
2809
|
);
|
|
2715
2810
|
if (fm.sources) for (const s of fm.sources) seen.add(s);
|
|
2716
2811
|
} catch {
|
|
@@ -2719,29 +2814,77 @@ async function scanDistilledSourceIds(root) {
|
|
|
2719
2814
|
return seen;
|
|
2720
2815
|
}
|
|
2721
2816
|
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
2722
|
-
|
|
2817
|
+
function applyLens(src, lens) {
|
|
2818
|
+
return {
|
|
2819
|
+
...src,
|
|
2820
|
+
instruction: lens.prompt,
|
|
2821
|
+
...lens.kinds ? { kinds: lens.kinds } : {},
|
|
2822
|
+
aspect: lensAspect(lens)
|
|
2823
|
+
};
|
|
2824
|
+
}
|
|
2825
|
+
async function runDistill(args, deps = {}) {
|
|
2723
2826
|
const parsed = parse4(args);
|
|
2724
2827
|
const target = resolveWorkspacePath(parsed.workspace);
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
return fail(
|
|
2728
|
-
`unknown --engine "${parsed.engine}". Valid: ${Object.keys(DISTILLER_ENGINES).join(", ")}.`,
|
|
2729
|
-
2
|
|
2730
|
-
);
|
|
2828
|
+
if (parsed.lens && parsed.lensFile) {
|
|
2829
|
+
return fail("--lens and --lens-file are mutually exclusive.", 2);
|
|
2731
2830
|
}
|
|
2732
|
-
|
|
2733
|
-
if (
|
|
2734
|
-
|
|
2831
|
+
let lens;
|
|
2832
|
+
if (parsed.lens || parsed.lensFile) {
|
|
2833
|
+
try {
|
|
2834
|
+
lens = parsed.lensFile ? await resolveLensFile(parsed.lensFile) : await resolveLens(parsed.lens, target);
|
|
2835
|
+
} catch (e) {
|
|
2836
|
+
if (e instanceof LensError) return fail(e.message, 2);
|
|
2837
|
+
throw e;
|
|
2838
|
+
}
|
|
2839
|
+
}
|
|
2840
|
+
const usage = createUsageSink({ runName: basename(target) });
|
|
2841
|
+
let distiller;
|
|
2842
|
+
let engineLabel;
|
|
2843
|
+
if (deps.distiller) {
|
|
2844
|
+
distiller = deps.distiller;
|
|
2845
|
+
engineLabel = parsed.engine;
|
|
2846
|
+
} else {
|
|
2847
|
+
const engine = DISTILLER_ENGINES[parsed.engine];
|
|
2848
|
+
if (!engine) {
|
|
2849
|
+
return fail(
|
|
2850
|
+
`unknown --engine "${parsed.engine}". Valid: ${Object.keys(DISTILLER_ENGINES).join(", ")}.`,
|
|
2851
|
+
2
|
|
2852
|
+
);
|
|
2853
|
+
}
|
|
2854
|
+
const apiKey = process.env.ANTHROPIC_API_KEY;
|
|
2855
|
+
if (engine.needsApiKey && !apiKey) {
|
|
2856
|
+
return fail(`distill engine "${engine.id}" needs ANTHROPIC_API_KEY in the environment.`, 2);
|
|
2857
|
+
}
|
|
2858
|
+
distiller = engine.create({
|
|
2859
|
+
...apiKey ? { apiKey } : {},
|
|
2860
|
+
...parsed.model ? { model: parsed.model } : {},
|
|
2861
|
+
onUsage: usage.record,
|
|
2862
|
+
...parsed.lang ? { lang: parsed.lang } : {}
|
|
2863
|
+
});
|
|
2864
|
+
engineLabel = engine.id;
|
|
2735
2865
|
}
|
|
2866
|
+
const fs = new NodeFsAdapter({ root: target });
|
|
2736
2867
|
const all = await readSources(target);
|
|
2737
2868
|
if (all.length === 0) return fail("no sources found under sources/ \u2014 run import-web first.", 2);
|
|
2738
|
-
const
|
|
2739
|
-
|
|
2869
|
+
const index = new DistillIndex({ fs });
|
|
2870
|
+
let pool;
|
|
2871
|
+
if (parsed.sourceId) {
|
|
2872
|
+
pool = all.filter((s) => s.id === parsed.sourceId);
|
|
2873
|
+
} else if (lens) {
|
|
2874
|
+
pool = [];
|
|
2875
|
+
for (const s of all) {
|
|
2876
|
+
if (!await index.isDistilled(s.id, s.contentHash, lens.id)) pool.push(s);
|
|
2877
|
+
}
|
|
2878
|
+
} else {
|
|
2879
|
+
const done = await scanDistilledSourceIds(target);
|
|
2880
|
+
pool = all.filter((s) => !done.has(s.id));
|
|
2881
|
+
}
|
|
2740
2882
|
const batch = parsed.max !== void 0 ? pool.slice(0, parsed.max) : pool;
|
|
2741
2883
|
process.stdout.write(
|
|
2742
2884
|
`distill \u2192 ${target}
|
|
2743
|
-
engine: ${
|
|
2744
|
-
|
|
2885
|
+
engine: ${engineLabel}
|
|
2886
|
+
` + (lens ? ` lens: ${lens.id} (aspect:${lensAspect(lens)})
|
|
2887
|
+
` : "") + ` sources: ${all.length} total \xB7 ${all.length - pool.length} already distilled \xB7 ${pool.length} to do
|
|
2745
2888
|
this run: ${batch.length}${parsed.max !== void 0 ? ` (--max ${parsed.max})` : ""}
|
|
2746
2889
|
`
|
|
2747
2890
|
);
|
|
@@ -2749,30 +2892,37 @@ async function runDistill(args) {
|
|
|
2749
2892
|
process.stdout.write(" nothing to do.\n");
|
|
2750
2893
|
return 0;
|
|
2751
2894
|
}
|
|
2752
|
-
const usage = createUsageSink({ runName: basename(target) });
|
|
2753
2895
|
const layout = await readEntryLayout(target);
|
|
2754
2896
|
const runner = new DistillRunner({
|
|
2755
|
-
fs
|
|
2897
|
+
fs,
|
|
2756
2898
|
clock: systemClock,
|
|
2757
2899
|
...layout ? { layout } : {},
|
|
2758
|
-
distiller
|
|
2759
|
-
...apiKey ? { apiKey } : {},
|
|
2760
|
-
...parsed.model ? { model: parsed.model } : {},
|
|
2761
|
-
onUsage: usage.record,
|
|
2762
|
-
...parsed.lang ? { lang: parsed.lang } : {}
|
|
2763
|
-
})
|
|
2900
|
+
distiller
|
|
2764
2901
|
});
|
|
2765
2902
|
let totalEntries = 0;
|
|
2766
2903
|
for (let i = 0; i < batch.length; i++) {
|
|
2767
|
-
const
|
|
2904
|
+
const raw = batch[i];
|
|
2905
|
+
const src = lens ? applyLens(raw, lens) : raw;
|
|
2768
2906
|
if (i > 0 && parsed.throttleMs > 0) await sleep(parsed.throttleMs);
|
|
2769
2907
|
try {
|
|
2770
2908
|
const report = await runner.run(src);
|
|
2771
2909
|
totalEntries += report.entryPaths.length;
|
|
2772
|
-
|
|
2910
|
+
if (lens) {
|
|
2911
|
+
await index.record({
|
|
2912
|
+
sourceId: raw.id,
|
|
2913
|
+
lensId: lens.id,
|
|
2914
|
+
title: raw.title,
|
|
2915
|
+
distilledAt: systemClock.now().toISOString(),
|
|
2916
|
+
engine: engineLabel,
|
|
2917
|
+
contentHash: raw.contentHash,
|
|
2918
|
+
entryCount: report.entryPaths.length,
|
|
2919
|
+
...report.entryPaths.length ? { entryPaths: report.entryPaths } : {}
|
|
2920
|
+
});
|
|
2921
|
+
}
|
|
2922
|
+
process.stdout.write(` \u2713 ${raw.id} \u2192 ${report.entryPaths.length} entries
|
|
2773
2923
|
`);
|
|
2774
2924
|
} catch (e) {
|
|
2775
|
-
process.stdout.write(` ! ${
|
|
2925
|
+
process.stdout.write(` ! ${raw.id} \u2014 ${e instanceof Error ? e.message : String(e)}
|
|
2776
2926
|
`);
|
|
2777
2927
|
}
|
|
2778
2928
|
}
|
|
@@ -3377,9 +3527,16 @@ Commands:
|
|
|
3377
3527
|
walled/JS pages, ahead of plain readability.
|
|
3378
3528
|
--diarize: AssemblyAI speaker labels (interviews).
|
|
3379
3529
|
distill [path] [--source id --max n --throttle ms --model m --lang l --engine e]
|
|
3530
|
+
[--lens <id> | --lens-file <path>]
|
|
3380
3531
|
Distill raw sources \u2192 refined entries
|
|
3381
3532
|
(principle/pattern/\u2026) with sources:[id] provenance.
|
|
3382
3533
|
Resumable: skips already-distilled sources.
|
|
3534
|
+
--lens reads every source THROUGH one aspect:
|
|
3535
|
+
its prompt drives extraction, its kinds constrain
|
|
3536
|
+
output, entries are tagged aspect:<value>, and the
|
|
3537
|
+
resume ledger keys by (source, lens). Resolves a
|
|
3538
|
+
workspace lenses/<id>.md (override) or a built-in
|
|
3539
|
+
(craft); --lens-file points at an ad-hoc lens doc.
|
|
3383
3540
|
knowledge [path] --tags a,b [--kind k --access scope --max n]
|
|
3384
3541
|
Preview what a skill's knowledge: binding resolves to
|
|
3385
3542
|
\u2014 refined entries + their provenance (filesystem).
|