@agentproto/corpus-cli 0.5.0 → 0.7.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 +604 -17
- package/dist/cli.mjs.map +1 -1
- package/dist/ports/index.d.ts +115 -2
- package/dist/ports/index.mjs +310 -9
- package/dist/ports/index.mjs.map +1 -1
- package/package.json +4 -3
package/dist/cli.mjs
CHANGED
|
@@ -7,10 +7,11 @@ import { fileURLToPath, pathToFileURL } from 'url';
|
|
|
7
7
|
import { z } from 'zod';
|
|
8
8
|
import { randomBytes, randomUUID, createHash } from 'crypto';
|
|
9
9
|
import { readFileSync, existsSync } from 'fs';
|
|
10
|
-
import { buildDistillPrompt, parseItems, isRefinedKind, SyncRunner, resolveKnowledge, DistillIndex, DistillRunner, systemClock, WebImporter, ImporterRunner, CorpusEventEmitter, CorpusWorkspaceReader, CorpusLinter, CorpusValidator, ReadOnlyFs, normalizeLanguageTag, lensAspect } from '@agentproto/corpus';
|
|
10
|
+
import { buildDistillPrompt, parseItems, isRefinedKind, SyncRunner, resolveKnowledge, DistillIndex, DistillRunner, systemClock, WebImporter, ImporterRunner, CorpusEventEmitter, CorpusWorkspaceReader, CorpusLinter, CorpusValidator, ReadOnlyFs, normalizeLanguageTag, CodeImporter, PrReviewImporter, lensAspect } from '@agentproto/corpus';
|
|
11
11
|
import matter3 from 'gray-matter';
|
|
12
12
|
import { execFile } from 'child_process';
|
|
13
13
|
import { promisify } from 'util';
|
|
14
|
+
import { getDocumentProxy, extractText as extractText$1, getMeta } from 'unpdf';
|
|
14
15
|
import { parseClaudeJsonOutput, spawnWithStdin } from '@agentproto/cli-exec';
|
|
15
16
|
import { stitchReport, buildPacks, lintReportConfig, reportConfigSchema } from '@agentproto/corpus/report';
|
|
16
17
|
|
|
@@ -404,8 +405,8 @@ function resolveWorkspacePath(arg) {
|
|
|
404
405
|
if (!arg) return process.cwd();
|
|
405
406
|
return path.resolve(process.cwd(), arg);
|
|
406
407
|
}
|
|
407
|
-
function fail(
|
|
408
|
-
process.stderr.write(`corpus: ${
|
|
408
|
+
function fail(msg9, code = 1) {
|
|
409
|
+
process.stderr.write(`corpus: ${msg9}
|
|
409
410
|
`);
|
|
410
411
|
return code;
|
|
411
412
|
}
|
|
@@ -1602,6 +1603,126 @@ function extractReadable(html) {
|
|
|
1602
1603
|
function decodeEntities(s) {
|
|
1603
1604
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/'/g, "'").replace(/ /g, " ").replace(/&#(\d+);/g, (_, n) => String.fromCharCode(Number(n)));
|
|
1604
1605
|
}
|
|
1606
|
+
var PDF_MAGIC = "%PDF-";
|
|
1607
|
+
var DEFAULT_UA2 = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0 Safari/537.36";
|
|
1608
|
+
async function defaultPdfExtractor(bytes) {
|
|
1609
|
+
const pdf = await getDocumentProxy(bytes);
|
|
1610
|
+
const [extracted, meta] = await Promise.all([
|
|
1611
|
+
extractText$1(pdf, { mergePages: false }),
|
|
1612
|
+
getMeta(pdf, { parseDates: true })
|
|
1613
|
+
]);
|
|
1614
|
+
return { totalPages: extracted.totalPages, pageTexts: extracted.text, info: meta.info };
|
|
1615
|
+
}
|
|
1616
|
+
var PdfFetcher = class {
|
|
1617
|
+
ua;
|
|
1618
|
+
timeoutMs;
|
|
1619
|
+
extract;
|
|
1620
|
+
constructor(opts = {}) {
|
|
1621
|
+
this.ua = opts.userAgent ?? DEFAULT_UA2;
|
|
1622
|
+
this.timeoutMs = opts.timeoutMs ?? 6e4;
|
|
1623
|
+
this.extract = opts.extract ?? defaultPdfExtractor;
|
|
1624
|
+
}
|
|
1625
|
+
async fetch(url) {
|
|
1626
|
+
let res;
|
|
1627
|
+
try {
|
|
1628
|
+
res = await fetch(url, {
|
|
1629
|
+
headers: { "user-agent": this.ua },
|
|
1630
|
+
redirect: "follow",
|
|
1631
|
+
signal: AbortSignal.timeout(this.timeoutMs)
|
|
1632
|
+
});
|
|
1633
|
+
} catch {
|
|
1634
|
+
return null;
|
|
1635
|
+
}
|
|
1636
|
+
if (!res.ok) return null;
|
|
1637
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
1638
|
+
const byExtension = isPdfExtension(url);
|
|
1639
|
+
const byContentType = contentType.toLowerCase().includes("application/pdf");
|
|
1640
|
+
if (!byExtension && !byContentType) return null;
|
|
1641
|
+
const buffer = await res.arrayBuffer();
|
|
1642
|
+
const bytes = new Uint8Array(buffer);
|
|
1643
|
+
const magic = decodeAscii(bytes.slice(0, 5));
|
|
1644
|
+
if (magic !== PDF_MAGIC) {
|
|
1645
|
+
throw new Error(
|
|
1646
|
+
`not a PDF: ${url} (magic bytes: ${JSON.stringify(magic)}, content-type: ${contentType || "none"})`
|
|
1647
|
+
);
|
|
1648
|
+
}
|
|
1649
|
+
const sha256 = "sha256:" + createHash("sha256").update(bytes).digest("hex");
|
|
1650
|
+
let extraction;
|
|
1651
|
+
try {
|
|
1652
|
+
extraction = await this.extract(bytes);
|
|
1653
|
+
} catch (e) {
|
|
1654
|
+
if (isPasswordProtected(e)) {
|
|
1655
|
+
throw new Error(`PDF is password-protected/encrypted: ${url}`);
|
|
1656
|
+
}
|
|
1657
|
+
throw new Error(`failed to open PDF: ${url} (${msg3(e)})`);
|
|
1658
|
+
}
|
|
1659
|
+
const { totalPages, pageTexts, info } = extraction;
|
|
1660
|
+
const hasText = pageTexts.some((t) => t.trim().length > 0);
|
|
1661
|
+
if (!hasText) {
|
|
1662
|
+
throw new Error(
|
|
1663
|
+
`PDF has no extractable text layer (scanned document, not OCR'd): ${url} \u2014 ${totalPages} page(s), 0 characters extracted`
|
|
1664
|
+
);
|
|
1665
|
+
}
|
|
1666
|
+
const text = pageTexts.map((t, i) => `--- page ${i + 1} of ${totalPages} ---
|
|
1667
|
+
|
|
1668
|
+
${t.trim()}`).join("\n\n");
|
|
1669
|
+
const pdfTitle = stringField(info, "Title");
|
|
1670
|
+
const pdfAuthor = stringField(info, "Author");
|
|
1671
|
+
const pdfProducer = stringField(info, "Producer");
|
|
1672
|
+
const pdfCreatedAt = dateField(info, "CreationDate");
|
|
1673
|
+
const pdfModifiedAt = dateField(info, "ModDate");
|
|
1674
|
+
return {
|
|
1675
|
+
title: pdfTitle || titleFromUrl(url),
|
|
1676
|
+
text,
|
|
1677
|
+
kind: "pdf",
|
|
1678
|
+
via: "extraction",
|
|
1679
|
+
metadata: {
|
|
1680
|
+
pdfPageCount: totalPages,
|
|
1681
|
+
pdfSha256: sha256,
|
|
1682
|
+
...pdfTitle ? { pdfTitle } : {},
|
|
1683
|
+
...pdfAuthor ? { pdfAuthor } : {},
|
|
1684
|
+
...pdfProducer ? { pdfProducer } : {},
|
|
1685
|
+
...pdfCreatedAt ? { pdfCreatedAt } : {},
|
|
1686
|
+
...pdfModifiedAt ? { pdfModifiedAt } : {}
|
|
1687
|
+
}
|
|
1688
|
+
};
|
|
1689
|
+
}
|
|
1690
|
+
};
|
|
1691
|
+
function isPdfExtension(url) {
|
|
1692
|
+
try {
|
|
1693
|
+
return /\.pdf$/i.test(new URL(url).pathname);
|
|
1694
|
+
} catch {
|
|
1695
|
+
return false;
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
function decodeAscii(bytes) {
|
|
1699
|
+
let out = "";
|
|
1700
|
+
for (const b of bytes) out += String.fromCharCode(b);
|
|
1701
|
+
return out;
|
|
1702
|
+
}
|
|
1703
|
+
function isPasswordProtected(e) {
|
|
1704
|
+
if (!(e instanceof Error)) return false;
|
|
1705
|
+
return e.name === "PasswordException" || /password/i.test(e.message);
|
|
1706
|
+
}
|
|
1707
|
+
function stringField(info, key) {
|
|
1708
|
+
const v = info[key];
|
|
1709
|
+
return typeof v === "string" && v.trim() ? v.trim() : void 0;
|
|
1710
|
+
}
|
|
1711
|
+
function dateField(info, key) {
|
|
1712
|
+
const v = info[key];
|
|
1713
|
+
return v instanceof Date && !Number.isNaN(v.getTime()) ? v.toISOString() : void 0;
|
|
1714
|
+
}
|
|
1715
|
+
function titleFromUrl(url) {
|
|
1716
|
+
try {
|
|
1717
|
+
const last = new URL(url).pathname.split("/").filter(Boolean).pop();
|
|
1718
|
+
return last ? decodeURIComponent(last) : url;
|
|
1719
|
+
} catch {
|
|
1720
|
+
return url;
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
function msg3(e) {
|
|
1724
|
+
return e instanceof Error ? e.message : String(e);
|
|
1725
|
+
}
|
|
1605
1726
|
|
|
1606
1727
|
// src/ports/composite-fetcher.ts
|
|
1607
1728
|
var CompositeFetcher = class {
|
|
@@ -1949,12 +2070,13 @@ ${plan}`);
|
|
|
1949
2070
|
...parsed.ffmpegLocation ? { ffmpegLocation: parsed.ffmpegLocation } : {}
|
|
1950
2071
|
})
|
|
1951
2072
|
);
|
|
2073
|
+
chain.push(new PdfFetcher());
|
|
1952
2074
|
if (parsed.scrapeMcp) {
|
|
1953
2075
|
try {
|
|
1954
2076
|
const client = await connectBrowserMcp({ endpoint: parsed.scrapeMcp });
|
|
1955
2077
|
chain.push(new ScrapeMcpFetcher({ client }));
|
|
1956
2078
|
} catch (e) {
|
|
1957
|
-
return fail(`could not connect to scrape MCP at ${parsed.scrapeMcp}: ${
|
|
2079
|
+
return fail(`could not connect to scrape MCP at ${parsed.scrapeMcp}: ${msg4(e)}`, 1);
|
|
1958
2080
|
}
|
|
1959
2081
|
}
|
|
1960
2082
|
if (parsed.browserMcp) {
|
|
@@ -1963,7 +2085,7 @@ ${plan}`);
|
|
|
1963
2085
|
browser = await connectBrowserMcp({ endpoint: parsed.browserMcp });
|
|
1964
2086
|
chain.push(new BrowserMcpFetcher({ browser }));
|
|
1965
2087
|
} catch (e) {
|
|
1966
|
-
return fail(`could not connect to browser MCP at ${parsed.browserMcp}: ${
|
|
2088
|
+
return fail(`could not connect to browser MCP at ${parsed.browserMcp}: ${msg4(e)}`, 1);
|
|
1967
2089
|
}
|
|
1968
2090
|
}
|
|
1969
2091
|
chain.push(new HttpReadabilityFetcher());
|
|
@@ -1994,7 +2116,7 @@ ${plan}`);
|
|
|
1994
2116
|
}
|
|
1995
2117
|
});
|
|
1996
2118
|
} catch (e) {
|
|
1997
|
-
return fail(`import failed: ${
|
|
2119
|
+
return fail(`import failed: ${msg4(e)}`, 1);
|
|
1998
2120
|
}
|
|
1999
2121
|
process.stdout.write(
|
|
2000
2122
|
`import-web \u2192 ${target}
|
|
@@ -2014,10 +2136,457 @@ knowledge engine (RAG) via the WriterPort. Import only stages the sources.
|
|
|
2014
2136
|
);
|
|
2015
2137
|
return 0;
|
|
2016
2138
|
}
|
|
2017
|
-
function
|
|
2139
|
+
function msg4(e) {
|
|
2018
2140
|
return e instanceof Error ? e.message : String(e);
|
|
2019
2141
|
}
|
|
2020
2142
|
function parse3(args) {
|
|
2143
|
+
const out = {
|
|
2144
|
+
workspace: void 0,
|
|
2145
|
+
root: void 0,
|
|
2146
|
+
include: [],
|
|
2147
|
+
granularity: "file",
|
|
2148
|
+
max: void 0,
|
|
2149
|
+
tags: [],
|
|
2150
|
+
lang: void 0,
|
|
2151
|
+
importerId: "code",
|
|
2152
|
+
dryRun: false
|
|
2153
|
+
};
|
|
2154
|
+
for (let i = 0; i < args.length; i++) {
|
|
2155
|
+
const a = args[i];
|
|
2156
|
+
const next = () => args[++i];
|
|
2157
|
+
switch (a) {
|
|
2158
|
+
case "--root":
|
|
2159
|
+
out.root = next();
|
|
2160
|
+
break;
|
|
2161
|
+
case "--include": {
|
|
2162
|
+
const v = next();
|
|
2163
|
+
if (v) out.include.push(v);
|
|
2164
|
+
break;
|
|
2165
|
+
}
|
|
2166
|
+
case "--granularity": {
|
|
2167
|
+
const v = next();
|
|
2168
|
+
if (v === "file" || v === "module") out.granularity = v;
|
|
2169
|
+
break;
|
|
2170
|
+
}
|
|
2171
|
+
case "--max": {
|
|
2172
|
+
const v = next();
|
|
2173
|
+
if (v) out.max = Number(v);
|
|
2174
|
+
break;
|
|
2175
|
+
}
|
|
2176
|
+
case "--tags": {
|
|
2177
|
+
const v = next();
|
|
2178
|
+
if (v) out.tags.push(...v.split(",").map((s) => s.trim()).filter(Boolean));
|
|
2179
|
+
break;
|
|
2180
|
+
}
|
|
2181
|
+
case "--lang":
|
|
2182
|
+
out.lang = next();
|
|
2183
|
+
break;
|
|
2184
|
+
case "--importer-id":
|
|
2185
|
+
out.importerId = next() ?? "code";
|
|
2186
|
+
break;
|
|
2187
|
+
case "--dry-run":
|
|
2188
|
+
out.dryRun = true;
|
|
2189
|
+
break;
|
|
2190
|
+
default:
|
|
2191
|
+
if (!a.startsWith("-") && out.workspace === void 0) out.workspace = a;
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
return out;
|
|
2195
|
+
}
|
|
2196
|
+
async function runImportCode(args) {
|
|
2197
|
+
const parsed = parse3(args);
|
|
2198
|
+
const target = resolveWorkspacePath(parsed.workspace);
|
|
2199
|
+
if (!parsed.root) {
|
|
2200
|
+
return fail("import-code requires --root <path> (workspace-relative source tree).", 2);
|
|
2201
|
+
}
|
|
2202
|
+
const fs = new NodeFsAdapter({ root: target });
|
|
2203
|
+
const importer = new CodeImporter({ fs });
|
|
2204
|
+
const importerTarget = {
|
|
2205
|
+
importerId: parsed.importerId,
|
|
2206
|
+
config: {
|
|
2207
|
+
rootPath: parsed.root,
|
|
2208
|
+
...parsed.include.length ? { include: parsed.include } : {},
|
|
2209
|
+
granularity: parsed.granularity,
|
|
2210
|
+
...parsed.max !== void 0 ? { maxFiles: parsed.max } : {},
|
|
2211
|
+
...parsed.tags.length ? { tags: parsed.tags } : {},
|
|
2212
|
+
...parsed.lang ? { language: parsed.lang } : {}
|
|
2213
|
+
}
|
|
2214
|
+
};
|
|
2215
|
+
const plan = ` workspace: ${target}
|
|
2216
|
+
root: ${parsed.root}
|
|
2217
|
+
include: ${parsed.include.length ? parsed.include.join(", ") : "**/*.ts (default)"}
|
|
2218
|
+
granularity: ${parsed.granularity}
|
|
2219
|
+
` + (parsed.max !== void 0 ? ` max: ${parsed.max}
|
|
2220
|
+
` : "");
|
|
2221
|
+
if (parsed.dryRun) {
|
|
2222
|
+
process.stdout.write(`import-code (dry run)
|
|
2223
|
+
${plan}`);
|
|
2224
|
+
let count = 0;
|
|
2225
|
+
try {
|
|
2226
|
+
for await (const source of importer.enumerate(importerTarget)) {
|
|
2227
|
+
if (count < 20) process.stdout.write(` - ${source.slug} (${source.title})
|
|
2228
|
+
`);
|
|
2229
|
+
count++;
|
|
2230
|
+
}
|
|
2231
|
+
} catch (e) {
|
|
2232
|
+
return fail(`enumerate failed: ${msg5(e)}`, 1);
|
|
2233
|
+
}
|
|
2234
|
+
if (count > 20) process.stdout.write(` \u2026 +${count - 20} more
|
|
2235
|
+
`);
|
|
2236
|
+
process.stdout.write(` units: ${count}
|
|
2237
|
+
`);
|
|
2238
|
+
return 0;
|
|
2239
|
+
}
|
|
2240
|
+
process.stdout.write(`import-code
|
|
2241
|
+
${plan}`);
|
|
2242
|
+
const runner = new ImporterRunner({
|
|
2243
|
+
// fs rooted AT the workspace → corpus paths are relative to it, so
|
|
2244
|
+
// workspacePath is "" (root), NOT the absolute target.
|
|
2245
|
+
fs,
|
|
2246
|
+
clock: systemClock,
|
|
2247
|
+
identity: new OsIdentityAdapter({ workspaceRoot: target }),
|
|
2248
|
+
workspacePath: ""
|
|
2249
|
+
});
|
|
2250
|
+
let report;
|
|
2251
|
+
try {
|
|
2252
|
+
report = await runner.run(importer, importerTarget);
|
|
2253
|
+
} catch (e) {
|
|
2254
|
+
return fail(`import failed: ${msg5(e)}`, 1);
|
|
2255
|
+
}
|
|
2256
|
+
process.stdout.write(
|
|
2257
|
+
`import-code \u2192 ${target}
|
|
2258
|
+
batch: ${report.batchId}
|
|
2259
|
+
archived: ${report.archivedSlugs.length}
|
|
2260
|
+
duplicates: ${report.duplicateSlugs.length}
|
|
2261
|
+
candidates: ${report.candidateIds.length}
|
|
2262
|
+
`
|
|
2263
|
+
);
|
|
2264
|
+
for (const w of report.warnings.slice(0, 20)) process.stdout.write(` ! ${w}
|
|
2265
|
+
`);
|
|
2266
|
+
process.stdout.write(
|
|
2267
|
+
`
|
|
2268
|
+
Notes-only: this archives code notes as knowledge sources. Symbol/caller/callee
|
|
2269
|
+
graph construction is the code-brain subsystem's job, not this importer.
|
|
2270
|
+
`
|
|
2271
|
+
);
|
|
2272
|
+
return 0;
|
|
2273
|
+
}
|
|
2274
|
+
function msg5(e) {
|
|
2275
|
+
return e instanceof Error ? e.message : String(e);
|
|
2276
|
+
}
|
|
2277
|
+
var execFileAsync4 = promisify(execFile);
|
|
2278
|
+
var GH_ACTOR = z.object({ login: z.string().optional(), name: z.string().optional() }).loose();
|
|
2279
|
+
var GH_PR_LIST_ITEM = z.object({ number: z.number() }).loose();
|
|
2280
|
+
var GH_PR_LIST = z.array(GH_PR_LIST_ITEM);
|
|
2281
|
+
var GH_COMMENT = z.object({
|
|
2282
|
+
author: GH_ACTOR.nullish(),
|
|
2283
|
+
body: z.string().optional(),
|
|
2284
|
+
path: z.string().optional(),
|
|
2285
|
+
createdAt: z.string().optional(),
|
|
2286
|
+
submittedAt: z.string().optional()
|
|
2287
|
+
}).loose();
|
|
2288
|
+
var GH_FILE = z.object({
|
|
2289
|
+
path: z.string(),
|
|
2290
|
+
additions: z.number().optional(),
|
|
2291
|
+
deletions: z.number().optional()
|
|
2292
|
+
}).loose();
|
|
2293
|
+
var GH_PR_VIEW = z.object({
|
|
2294
|
+
number: z.number(),
|
|
2295
|
+
title: z.string().optional(),
|
|
2296
|
+
body: z.string().optional(),
|
|
2297
|
+
author: GH_ACTOR.nullish(),
|
|
2298
|
+
url: z.string().optional(),
|
|
2299
|
+
state: z.string().optional(),
|
|
2300
|
+
mergedAt: z.string().nullish(),
|
|
2301
|
+
reviews: z.array(GH_COMMENT).optional(),
|
|
2302
|
+
comments: z.array(GH_COMMENT).optional(),
|
|
2303
|
+
reviewRequests: z.array(GH_ACTOR).optional(),
|
|
2304
|
+
additions: z.number().optional(),
|
|
2305
|
+
deletions: z.number().optional(),
|
|
2306
|
+
changedFiles: z.number().optional(),
|
|
2307
|
+
files: z.array(GH_FILE).optional()
|
|
2308
|
+
}).loose();
|
|
2309
|
+
var VIEW_FIELDS = [
|
|
2310
|
+
"number",
|
|
2311
|
+
"title",
|
|
2312
|
+
"body",
|
|
2313
|
+
"author",
|
|
2314
|
+
"url",
|
|
2315
|
+
"state",
|
|
2316
|
+
"mergedAt",
|
|
2317
|
+
"reviews",
|
|
2318
|
+
"comments",
|
|
2319
|
+
"reviewRequests",
|
|
2320
|
+
"additions",
|
|
2321
|
+
"deletions",
|
|
2322
|
+
"changedFiles",
|
|
2323
|
+
"files"
|
|
2324
|
+
].join(",");
|
|
2325
|
+
var GhPrSourceAdapter = class {
|
|
2326
|
+
run;
|
|
2327
|
+
constructor(opts = {}) {
|
|
2328
|
+
this.run = opts.run ?? defaultGhRunner(opts.ghBin ?? "gh");
|
|
2329
|
+
}
|
|
2330
|
+
async *listPullRequests(query) {
|
|
2331
|
+
const numbers = await this.resolveNumbers(query);
|
|
2332
|
+
for (const n of numbers) {
|
|
2333
|
+
let pr;
|
|
2334
|
+
try {
|
|
2335
|
+
pr = await this.viewPr(query, n);
|
|
2336
|
+
} catch (e) {
|
|
2337
|
+
process.stderr.write(
|
|
2338
|
+
`corpus: skipping ${query.repo}#${n} \u2014 ${msg6(e)}
|
|
2339
|
+
`
|
|
2340
|
+
);
|
|
2341
|
+
continue;
|
|
2342
|
+
}
|
|
2343
|
+
if (pr) yield pr;
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
/** Resolve the query to a concrete, capped list of PR numbers. */
|
|
2347
|
+
async resolveNumbers(query) {
|
|
2348
|
+
const cap = query.maxPRs ?? 1e3;
|
|
2349
|
+
if (query.prNumbers && query.prNumbers.length > 0) {
|
|
2350
|
+
return query.prNumbers.slice(0, cap);
|
|
2351
|
+
}
|
|
2352
|
+
const args = [
|
|
2353
|
+
"pr",
|
|
2354
|
+
"list",
|
|
2355
|
+
"--repo",
|
|
2356
|
+
query.repo,
|
|
2357
|
+
"--state",
|
|
2358
|
+
"all",
|
|
2359
|
+
"--limit",
|
|
2360
|
+
String(cap),
|
|
2361
|
+
"--json",
|
|
2362
|
+
"number"
|
|
2363
|
+
];
|
|
2364
|
+
if (query.since) args.push("--search", `updated:>=${query.since}`);
|
|
2365
|
+
const out = await this.run(args);
|
|
2366
|
+
return GH_PR_LIST.parse(parseJson(out)).map((item) => item.number).sort((a, b) => a - b);
|
|
2367
|
+
}
|
|
2368
|
+
/** Fetch one PR's detail and shape it into a PrDoc, or null if empty. */
|
|
2369
|
+
async viewPr(query, number) {
|
|
2370
|
+
const out = await this.run([
|
|
2371
|
+
"pr",
|
|
2372
|
+
"view",
|
|
2373
|
+
String(number),
|
|
2374
|
+
"--repo",
|
|
2375
|
+
query.repo,
|
|
2376
|
+
"--json",
|
|
2377
|
+
VIEW_FIELDS
|
|
2378
|
+
]);
|
|
2379
|
+
const v = GH_PR_VIEW.parse(parseJson(out));
|
|
2380
|
+
const reviewComments = toReviewComments([
|
|
2381
|
+
...v.reviews ?? [],
|
|
2382
|
+
...v.comments ?? []
|
|
2383
|
+
]);
|
|
2384
|
+
const reviewers = uniqueLogins([
|
|
2385
|
+
...(v.reviews ?? []).map((r) => r.author),
|
|
2386
|
+
...v.reviewRequests ?? []
|
|
2387
|
+
]);
|
|
2388
|
+
const doc = {
|
|
2389
|
+
number: v.number,
|
|
2390
|
+
title: v.title ?? "",
|
|
2391
|
+
body: v.body ?? "",
|
|
2392
|
+
...actorLabel(v.author) ? { author: actorLabel(v.author) } : {},
|
|
2393
|
+
...v.url ? { url: v.url } : {},
|
|
2394
|
+
...v.state ? { state: v.state.toLowerCase() } : {},
|
|
2395
|
+
...v.mergedAt ? { mergedAt: v.mergedAt } : {},
|
|
2396
|
+
...reviewers.length > 0 ? { reviewers } : {},
|
|
2397
|
+
...reviewComments.length > 0 ? { reviewComments } : {},
|
|
2398
|
+
...query.includeDiffSummary ? { diffSummary: buildDiffSummary(v) } : {}
|
|
2399
|
+
};
|
|
2400
|
+
return doc;
|
|
2401
|
+
}
|
|
2402
|
+
};
|
|
2403
|
+
function actorLabel(actor) {
|
|
2404
|
+
const label = actor?.login ?? actor?.name;
|
|
2405
|
+
return label && label.length > 0 ? label : void 0;
|
|
2406
|
+
}
|
|
2407
|
+
function toReviewComments(raw) {
|
|
2408
|
+
const out = [];
|
|
2409
|
+
for (const c of raw) {
|
|
2410
|
+
const body = (c.body ?? "").trim();
|
|
2411
|
+
if (!body) continue;
|
|
2412
|
+
const author = actorLabel(c.author);
|
|
2413
|
+
const at = c.submittedAt ?? c.createdAt;
|
|
2414
|
+
out.push({
|
|
2415
|
+
body,
|
|
2416
|
+
...author ? { author } : {},
|
|
2417
|
+
...c.path ? { path: c.path } : {},
|
|
2418
|
+
...at ? { at } : {}
|
|
2419
|
+
});
|
|
2420
|
+
}
|
|
2421
|
+
return out;
|
|
2422
|
+
}
|
|
2423
|
+
function uniqueLogins(actors) {
|
|
2424
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2425
|
+
for (const a of actors) {
|
|
2426
|
+
const label = actorLabel(a);
|
|
2427
|
+
if (label) seen.add(label);
|
|
2428
|
+
}
|
|
2429
|
+
return [...seen];
|
|
2430
|
+
}
|
|
2431
|
+
function buildDiffSummary(v) {
|
|
2432
|
+
const lines = [];
|
|
2433
|
+
for (const f of v.files ?? []) {
|
|
2434
|
+
const add = f.additions ?? 0;
|
|
2435
|
+
const del = f.deletions ?? 0;
|
|
2436
|
+
lines.push(`${f.path} | +${add} -${del}`);
|
|
2437
|
+
}
|
|
2438
|
+
const totals = v.changedFiles !== void 0 ? `${v.changedFiles} files changed, +${v.additions ?? 0} -${v.deletions ?? 0}` : void 0;
|
|
2439
|
+
return [...totals ? [totals] : [], ...lines].join("\n");
|
|
2440
|
+
}
|
|
2441
|
+
function parseJson(out) {
|
|
2442
|
+
const trimmed = out.trim();
|
|
2443
|
+
if (!trimmed) return [];
|
|
2444
|
+
return JSON.parse(trimmed);
|
|
2445
|
+
}
|
|
2446
|
+
function msg6(e) {
|
|
2447
|
+
return e instanceof Error ? e.message : String(e);
|
|
2448
|
+
}
|
|
2449
|
+
function defaultGhRunner(bin) {
|
|
2450
|
+
return async (args) => {
|
|
2451
|
+
const { stdout } = await execFileAsync4(bin, [...args], {
|
|
2452
|
+
maxBuffer: 32 * 1024 * 1024
|
|
2453
|
+
});
|
|
2454
|
+
return stdout;
|
|
2455
|
+
};
|
|
2456
|
+
}
|
|
2457
|
+
|
|
2458
|
+
// src/commands/import-prs.ts
|
|
2459
|
+
function parse4(args) {
|
|
2460
|
+
const out = {
|
|
2461
|
+
workspace: void 0,
|
|
2462
|
+
repo: void 0,
|
|
2463
|
+
prNumbers: [],
|
|
2464
|
+
since: void 0,
|
|
2465
|
+
includeDiff: false,
|
|
2466
|
+
tags: [],
|
|
2467
|
+
lang: void 0,
|
|
2468
|
+
max: void 0,
|
|
2469
|
+
importerId: "pr-review",
|
|
2470
|
+
dryRun: false
|
|
2471
|
+
};
|
|
2472
|
+
for (let i = 0; i < args.length; i++) {
|
|
2473
|
+
const a = args[i];
|
|
2474
|
+
const next = () => args[++i];
|
|
2475
|
+
switch (a) {
|
|
2476
|
+
case "--repo":
|
|
2477
|
+
out.repo = next();
|
|
2478
|
+
break;
|
|
2479
|
+
case "--pr": {
|
|
2480
|
+
const v = next();
|
|
2481
|
+
if (v && Number.isFinite(Number(v))) out.prNumbers.push(Number(v));
|
|
2482
|
+
break;
|
|
2483
|
+
}
|
|
2484
|
+
case "--since":
|
|
2485
|
+
out.since = next();
|
|
2486
|
+
break;
|
|
2487
|
+
case "--include-diff":
|
|
2488
|
+
out.includeDiff = true;
|
|
2489
|
+
break;
|
|
2490
|
+
case "--tags": {
|
|
2491
|
+
const v = next();
|
|
2492
|
+
if (v) out.tags.push(...v.split(",").map((s) => s.trim()).filter(Boolean));
|
|
2493
|
+
break;
|
|
2494
|
+
}
|
|
2495
|
+
case "--lang":
|
|
2496
|
+
out.lang = next();
|
|
2497
|
+
break;
|
|
2498
|
+
case "--max": {
|
|
2499
|
+
const v = next();
|
|
2500
|
+
if (v) out.max = Number(v);
|
|
2501
|
+
break;
|
|
2502
|
+
}
|
|
2503
|
+
case "--importer-id":
|
|
2504
|
+
out.importerId = next() ?? "pr-review";
|
|
2505
|
+
break;
|
|
2506
|
+
case "--dry-run":
|
|
2507
|
+
out.dryRun = true;
|
|
2508
|
+
break;
|
|
2509
|
+
default:
|
|
2510
|
+
if (!a.startsWith("-") && out.workspace === void 0) out.workspace = a;
|
|
2511
|
+
}
|
|
2512
|
+
}
|
|
2513
|
+
return out;
|
|
2514
|
+
}
|
|
2515
|
+
async function runImportPrs(args) {
|
|
2516
|
+
const parsed = parse4(args);
|
|
2517
|
+
const target = resolveWorkspacePath(parsed.workspace);
|
|
2518
|
+
if (!parsed.repo) {
|
|
2519
|
+
return fail("import-prs requires --repo <owner/name> (e.g. --repo agentproto/ts).", 2);
|
|
2520
|
+
}
|
|
2521
|
+
const selection = parsed.prNumbers.length > 0 ? `PRs ${parsed.prNumbers.join(", ")}` : parsed.since ? `updated since ${parsed.since}` : "all PRs";
|
|
2522
|
+
const plan = ` workspace: ${target}
|
|
2523
|
+
repo: ${parsed.repo}
|
|
2524
|
+
select: ${selection}${parsed.max !== void 0 ? ` (--max ${parsed.max})` : ""}
|
|
2525
|
+
diff: ${parsed.includeDiff ? "included" : "omitted"}
|
|
2526
|
+
`;
|
|
2527
|
+
if (parsed.dryRun) {
|
|
2528
|
+
process.stdout.write(`import-prs (dry run)
|
|
2529
|
+
${plan}`);
|
|
2530
|
+
for (const n of parsed.prNumbers.slice(0, 20)) {
|
|
2531
|
+
process.stdout.write(` - ${parsed.repo}#${n}
|
|
2532
|
+
`);
|
|
2533
|
+
}
|
|
2534
|
+
if (parsed.prNumbers.length > 20) {
|
|
2535
|
+
process.stdout.write(` \u2026 +${parsed.prNumbers.length - 20} more
|
|
2536
|
+
`);
|
|
2537
|
+
}
|
|
2538
|
+
return 0;
|
|
2539
|
+
}
|
|
2540
|
+
process.stdout.write(`import-prs
|
|
2541
|
+
${plan}`);
|
|
2542
|
+
const importer = new PrReviewImporter({ source: new GhPrSourceAdapter() });
|
|
2543
|
+
const runner = new ImporterRunner({
|
|
2544
|
+
// The fs is rooted AT the workspace, so corpus paths are relative to it —
|
|
2545
|
+
// workspacePath is "" (root), NOT the absolute target.
|
|
2546
|
+
fs: new NodeFsAdapter({ root: target }),
|
|
2547
|
+
clock: systemClock,
|
|
2548
|
+
identity: new OsIdentityAdapter({ workspaceRoot: target }),
|
|
2549
|
+
workspacePath: ""
|
|
2550
|
+
});
|
|
2551
|
+
let report;
|
|
2552
|
+
try {
|
|
2553
|
+
report = await runner.run(importer, {
|
|
2554
|
+
importerId: parsed.importerId,
|
|
2555
|
+
config: {
|
|
2556
|
+
repo: parsed.repo,
|
|
2557
|
+
...parsed.prNumbers.length > 0 ? { prNumbers: parsed.prNumbers } : {},
|
|
2558
|
+
...parsed.since ? { since: parsed.since } : {},
|
|
2559
|
+
...parsed.includeDiff ? { includeDiffSummary: true } : {},
|
|
2560
|
+
...parsed.tags.length ? { tags: parsed.tags } : {},
|
|
2561
|
+
...parsed.lang ? { language: parsed.lang } : {},
|
|
2562
|
+
...parsed.max !== void 0 ? { maxPRs: parsed.max } : {}
|
|
2563
|
+
}
|
|
2564
|
+
});
|
|
2565
|
+
} catch (e) {
|
|
2566
|
+
return fail(`import failed: ${msg7(e)}`, 1);
|
|
2567
|
+
}
|
|
2568
|
+
process.stdout.write(
|
|
2569
|
+
`import-prs \u2192 ${target}
|
|
2570
|
+
batch: ${report.batchId}
|
|
2571
|
+
archived: ${report.archivedSlugs.length}
|
|
2572
|
+
duplicates: ${report.duplicateSlugs.length}
|
|
2573
|
+
candidates: ${report.candidateIds.length}
|
|
2574
|
+
`
|
|
2575
|
+
);
|
|
2576
|
+
for (const w of report.warnings.slice(0, 20)) process.stdout.write(` ! ${w}
|
|
2577
|
+
`);
|
|
2578
|
+
process.stdout.write(
|
|
2579
|
+
`
|
|
2580
|
+
Next: review candidates, then distill them \u2014 distillation turns the raw PR
|
|
2581
|
+
sources into refined knowledge.entry/v1 (pattern/critique/summary).
|
|
2582
|
+
`
|
|
2583
|
+
);
|
|
2584
|
+
return 0;
|
|
2585
|
+
}
|
|
2586
|
+
function msg7(e) {
|
|
2587
|
+
return e instanceof Error ? e.message : String(e);
|
|
2588
|
+
}
|
|
2589
|
+
function parse5(args) {
|
|
2021
2590
|
const out = {
|
|
2022
2591
|
topic: void 0,
|
|
2023
2592
|
outputPath: void 0,
|
|
@@ -2192,7 +2761,7 @@ async function readExistingUrls(outFile) {
|
|
|
2192
2761
|
}
|
|
2193
2762
|
}
|
|
2194
2763
|
async function runDiscover(args) {
|
|
2195
|
-
const parsed =
|
|
2764
|
+
const parsed = parse5(args);
|
|
2196
2765
|
if (!parsed.topic) {
|
|
2197
2766
|
return fail("discover requires a <topic> argument. Usage: corpus discover <topic> [path] [--max N] [--channels web,youtube,social] [--lang fr] [--tags t] [--import] [--fresh]", 2);
|
|
2198
2767
|
}
|
|
@@ -2708,7 +3277,7 @@ var DISTILLER_ENGINES = {
|
|
|
2708
3277
|
])
|
|
2709
3278
|
)
|
|
2710
3279
|
};
|
|
2711
|
-
function
|
|
3280
|
+
function parse6(args) {
|
|
2712
3281
|
const out = {
|
|
2713
3282
|
workspace: void 0,
|
|
2714
3283
|
sourceId: void 0,
|
|
@@ -2823,7 +3392,7 @@ function applyLens(src, lens) {
|
|
|
2823
3392
|
};
|
|
2824
3393
|
}
|
|
2825
3394
|
async function runDistill(args, deps = {}) {
|
|
2826
|
-
const parsed =
|
|
3395
|
+
const parsed = parse6(args);
|
|
2827
3396
|
const target = resolveWorkspacePath(parsed.workspace);
|
|
2828
3397
|
if (parsed.lens && parsed.lensFile) {
|
|
2829
3398
|
return fail("--lens and --lens-file are mutually exclusive.", 2);
|
|
@@ -3274,10 +3843,10 @@ var SseMcpClient = class {
|
|
|
3274
3843
|
try {
|
|
3275
3844
|
const parsed = RPC_MESSAGE.safeParse(JSON.parse(payload));
|
|
3276
3845
|
if (!parsed.success) return;
|
|
3277
|
-
const
|
|
3278
|
-
if (typeof
|
|
3279
|
-
this.pending.get(
|
|
3280
|
-
this.pending.delete(
|
|
3846
|
+
const msg9 = parsed.data;
|
|
3847
|
+
if (typeof msg9.id === "number" && this.pending.has(msg9.id)) {
|
|
3848
|
+
this.pending.get(msg9.id)(msg9);
|
|
3849
|
+
this.pending.delete(msg9.id);
|
|
3281
3850
|
}
|
|
3282
3851
|
} catch {
|
|
3283
3852
|
}
|
|
@@ -3346,7 +3915,7 @@ var McpSink = class {
|
|
|
3346
3915
|
try {
|
|
3347
3916
|
client = await this.ensure();
|
|
3348
3917
|
} catch (e) {
|
|
3349
|
-
return { uri: item.uri, ok: false, error: `connect: ${
|
|
3918
|
+
return { uri: item.uri, ok: false, error: `connect: ${msg8(e)}` };
|
|
3350
3919
|
}
|
|
3351
3920
|
const args = template(this.config.args, item);
|
|
3352
3921
|
const call = this.config.importedAlias ? { name: "mcp_imported_call", args: { alias: this.config.importedAlias, toolName: this.config.tool, args } } : { name: this.config.tool, args };
|
|
@@ -3355,7 +3924,7 @@ var McpSink = class {
|
|
|
3355
3924
|
if (res.isError) return { uri: item.uri, ok: false, error: "tool returned isError" };
|
|
3356
3925
|
return { uri: item.uri, ok: true };
|
|
3357
3926
|
} catch (e) {
|
|
3358
|
-
return { uri: item.uri, ok: false, error:
|
|
3927
|
+
return { uri: item.uri, ok: false, error: msg8(e) };
|
|
3359
3928
|
}
|
|
3360
3929
|
}
|
|
3361
3930
|
};
|
|
@@ -3399,7 +3968,7 @@ function stringify(v) {
|
|
|
3399
3968
|
if (Array.isArray(v)) return v.join(", ");
|
|
3400
3969
|
return v == null ? "" : String(v);
|
|
3401
3970
|
}
|
|
3402
|
-
function
|
|
3971
|
+
function msg8(e) {
|
|
3403
3972
|
return e instanceof Error ? e.message : String(e);
|
|
3404
3973
|
}
|
|
3405
3974
|
|
|
@@ -3526,6 +4095,20 @@ Commands:
|
|
|
3526
4095
|
scrape MCP server (stealth + clean Markdown) for
|
|
3527
4096
|
walled/JS pages, ahead of plain readability.
|
|
3528
4097
|
--diarize: AssemblyAI speaker labels (interviews).
|
|
4098
|
+
import-code [path] --root <dir> [--include glob \u2026 --granularity file|module --max n --tags t --lang l --dry-run]
|
|
4099
|
+
Import a source tree as knowledge sources, one note
|
|
4100
|
+
per code unit (file or module). Notes-only: ZERO
|
|
4101
|
+
code indexing, no symbol/caller/callee graph \u2014
|
|
4102
|
+
that is the code-brain subsystem's job. --dry-run
|
|
4103
|
+
lists the units without writing.
|
|
4104
|
+
import-prs [path] --repo <owner/name> [--pr n ... | --since <iso>] [--include-diff --max n --tags t --lang l --dry-run]
|
|
4105
|
+
Import a repo's pull requests as sources via the
|
|
4106
|
+
user's gh CLI (description + review discussion +
|
|
4107
|
+
optional diff summary). Each PR \u2192 one AIP-10 source
|
|
4108
|
+
(authority: secondary), deduped by content_hash so
|
|
4109
|
+
re-runs skip unchanged PRs. --pr selects explicit PRs
|
|
4110
|
+
(repeatable); --since imports PRs updated since a
|
|
4111
|
+
date; --include-diff appends a diffstat summary.
|
|
3529
4112
|
distill [path] [--source id --max n --throttle ms --model m --lang l --engine e]
|
|
3530
4113
|
[--lens <id> | --lens-file <path>]
|
|
3531
4114
|
Distill raw sources \u2192 refined entries
|
|
@@ -3585,6 +4168,10 @@ async function main(argv2) {
|
|
|
3585
4168
|
return await runEventsTail(rest);
|
|
3586
4169
|
case "import-web":
|
|
3587
4170
|
return await runImportWeb(rest);
|
|
4171
|
+
case "import-code":
|
|
4172
|
+
return await runImportCode(rest);
|
|
4173
|
+
case "import-prs":
|
|
4174
|
+
return await runImportPrs(rest);
|
|
3588
4175
|
case "discover":
|
|
3589
4176
|
return await runDiscover(rest);
|
|
3590
4177
|
case "distill":
|