@agentproto/corpus-cli 0.5.0 → 0.6.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.
@@ -1,5 +1,5 @@
1
1
  export { N as NodeFsAdapter, a as NodeFsAdapterOptions, O as OsIdentityAdapter, b as OsIdentityAdapterOptions } from '../os-identity.adapter-CLBmnRoD.js';
2
- import { FetcherPort, FetchedSource, DistillPort, DistillInput, DistilledItem, SinkPort, SinkItem, SinkPushResult } from '@agentproto/corpus';
2
+ import { FetcherPort, FetchedSource, PrSourcePort, PrQuery, PrDoc, DistillPort, DistillInput, DistilledItem, SinkPort, SinkItem, SinkPushResult } from '@agentproto/corpus';
3
3
  export { buildDistillPrompt, parseItems } from '@agentproto/corpus';
4
4
  import { z } from 'zod';
5
5
 
@@ -357,6 +357,45 @@ declare class HttpReadabilityFetcher implements FetcherPort {
357
357
  fetch(url: string): Promise<FetchedSource | null>;
358
358
  }
359
359
 
360
+ /**
361
+ * GhPrSourceAdapter — a PrSourcePort backed by the GitHub CLI (`gh`). This is
362
+ * how the local `corpus` runtime resolves a repo query to its pull requests:
363
+ * the SAME `gh` binary the rest of the repo already talks to GitHub through
364
+ * (see the daemon's `gh pr create` lane), reusing the user's existing `gh`
365
+ * auth — no new HTTP client, no token plumbing in the corpus layer.
366
+ *
367
+ * query → gh pr list --repo <repo> --json number … (enumerate)
368
+ * → gh pr view <n> --repo <repo> --json … (per-PR detail)
369
+ *
370
+ * Per-PR resilience lives HERE, not in the pure importer: a single PR that
371
+ * `gh pr view` cannot fetch (deleted, permission) is skipped with a stderr
372
+ * notice and simply not yielded, so one bad PR never aborts the batch. A
373
+ * failure to even *list* PRs (bad repo, `gh` unauthenticated) throws — that is
374
+ * a hard batch abort the importer surfaces.
375
+ *
376
+ * The `gh` invocation is injected as `run` so the adapter is unit-testable with
377
+ * a fake runner — no binary, no network. All `gh` JSON is parsed through zod;
378
+ * no `any` crosses the boundary.
379
+ */
380
+
381
+ /** Run a `gh` subcommand and resolve its stdout. Injectable for tests. */
382
+ type GhRunner = (args: readonly string[]) => Promise<string>;
383
+ interface GhPrSourceAdapterOptions {
384
+ /** Defaults to a `gh` subprocess runner. */
385
+ readonly run?: GhRunner;
386
+ /** Path to the `gh` binary (default: "gh" on PATH). */
387
+ readonly ghBin?: string;
388
+ }
389
+ declare class GhPrSourceAdapter implements PrSourcePort {
390
+ private readonly run;
391
+ constructor(opts?: GhPrSourceAdapterOptions);
392
+ listPullRequests(query: PrQuery): AsyncIterable<PrDoc>;
393
+ /** Resolve the query to a concrete, capped list of PR numbers. */
394
+ private resolveNumbers;
395
+ /** Fetch one PR's detail and shape it into a PrDoc, or null if empty. */
396
+ private viewPr;
397
+ }
398
+
360
399
  /**
361
400
  * Usage telemetry for the metered distill path — cost visibility + optional
362
401
  * Langfuse export. SDK-free (raw fetch to Langfuse's ingestion API), mirroring
@@ -570,4 +609,4 @@ declare class McpSink implements SinkPort {
570
609
  push(item: SinkItem): Promise<SinkPushResult>;
571
610
  }
572
611
 
573
- export { AnthropicDistiller, type AnthropicDistillerOptions, AssemblyAiStt, type AssemblyAiSttOptions, type AudioDownload, type AudioDownloader, type AudioSplitter, BrowserMcpFetcher, type BrowserMcpFetcherOptions, type BrowserMcpLike, CLI_ENGINES, ChunkedStt, type ChunkedSttOptions, CliAgentDistiller, type CliAgentDistillerOptions, type CliEngine, CompositeFetcher, type ConnectMcpHttpOptions, HttpReadabilityFetcher, type HttpReadabilityFetcherOptions, type McpClientLike, McpSink, type McpSinkConfig, OpenAiWhisperStt, type OpenAiWhisperSttOptions, ScrapeMcpFetcher, type ScrapeMcpFetcherOptions, type SttPort, ThrottleFetcher, type ThrottleFetcherOptions, type Transcript, YtDlpWhisperFetcher, type YtDlpWhisperFetcherOptions, connectMcpHttp };
612
+ export { AnthropicDistiller, type AnthropicDistillerOptions, AssemblyAiStt, type AssemblyAiSttOptions, type AudioDownload, type AudioDownloader, type AudioSplitter, BrowserMcpFetcher, type BrowserMcpFetcherOptions, type BrowserMcpLike, CLI_ENGINES, ChunkedStt, type ChunkedSttOptions, CliAgentDistiller, type CliAgentDistillerOptions, type CliEngine, CompositeFetcher, type ConnectMcpHttpOptions, GhPrSourceAdapter, type GhPrSourceAdapterOptions, type GhRunner, HttpReadabilityFetcher, type HttpReadabilityFetcherOptions, type McpClientLike, McpSink, type McpSinkConfig, OpenAiWhisperStt, type OpenAiWhisperSttOptions, ScrapeMcpFetcher, type ScrapeMcpFetcherOptions, type SttPort, ThrottleFetcher, type ThrottleFetcherOptions, type Transcript, YtDlpWhisperFetcher, type YtDlpWhisperFetcherOptions, connectMcpHttp };
@@ -761,6 +761,186 @@ function extractReadable(html) {
761
761
  function decodeEntities(s) {
762
762
  return s.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&nbsp;/g, " ").replace(/&#(\d+);/g, (_, n) => String.fromCharCode(Number(n)));
763
763
  }
764
+ var execFileAsync3 = promisify(execFile);
765
+ var GH_ACTOR = z.object({ login: z.string().optional(), name: z.string().optional() }).loose();
766
+ var GH_PR_LIST_ITEM = z.object({ number: z.number() }).loose();
767
+ var GH_PR_LIST = z.array(GH_PR_LIST_ITEM);
768
+ var GH_COMMENT = z.object({
769
+ author: GH_ACTOR.nullish(),
770
+ body: z.string().optional(),
771
+ path: z.string().optional(),
772
+ createdAt: z.string().optional(),
773
+ submittedAt: z.string().optional()
774
+ }).loose();
775
+ var GH_FILE = z.object({
776
+ path: z.string(),
777
+ additions: z.number().optional(),
778
+ deletions: z.number().optional()
779
+ }).loose();
780
+ var GH_PR_VIEW = z.object({
781
+ number: z.number(),
782
+ title: z.string().optional(),
783
+ body: z.string().optional(),
784
+ author: GH_ACTOR.nullish(),
785
+ url: z.string().optional(),
786
+ state: z.string().optional(),
787
+ mergedAt: z.string().nullish(),
788
+ reviews: z.array(GH_COMMENT).optional(),
789
+ comments: z.array(GH_COMMENT).optional(),
790
+ reviewRequests: z.array(GH_ACTOR).optional(),
791
+ additions: z.number().optional(),
792
+ deletions: z.number().optional(),
793
+ changedFiles: z.number().optional(),
794
+ files: z.array(GH_FILE).optional()
795
+ }).loose();
796
+ var VIEW_FIELDS = [
797
+ "number",
798
+ "title",
799
+ "body",
800
+ "author",
801
+ "url",
802
+ "state",
803
+ "mergedAt",
804
+ "reviews",
805
+ "comments",
806
+ "reviewRequests",
807
+ "additions",
808
+ "deletions",
809
+ "changedFiles",
810
+ "files"
811
+ ].join(",");
812
+ var GhPrSourceAdapter = class {
813
+ run;
814
+ constructor(opts = {}) {
815
+ this.run = opts.run ?? defaultGhRunner(opts.ghBin ?? "gh");
816
+ }
817
+ async *listPullRequests(query) {
818
+ const numbers = await this.resolveNumbers(query);
819
+ for (const n of numbers) {
820
+ let pr;
821
+ try {
822
+ pr = await this.viewPr(query, n);
823
+ } catch (e) {
824
+ process.stderr.write(
825
+ `corpus: skipping ${query.repo}#${n} \u2014 ${msg2(e)}
826
+ `
827
+ );
828
+ continue;
829
+ }
830
+ if (pr) yield pr;
831
+ }
832
+ }
833
+ /** Resolve the query to a concrete, capped list of PR numbers. */
834
+ async resolveNumbers(query) {
835
+ const cap = query.maxPRs ?? 1e3;
836
+ if (query.prNumbers && query.prNumbers.length > 0) {
837
+ return query.prNumbers.slice(0, cap);
838
+ }
839
+ const args = [
840
+ "pr",
841
+ "list",
842
+ "--repo",
843
+ query.repo,
844
+ "--state",
845
+ "all",
846
+ "--limit",
847
+ String(cap),
848
+ "--json",
849
+ "number"
850
+ ];
851
+ if (query.since) args.push("--search", `updated:>=${query.since}`);
852
+ const out = await this.run(args);
853
+ return GH_PR_LIST.parse(parseJson(out)).map((item) => item.number).sort((a, b) => a - b);
854
+ }
855
+ /** Fetch one PR's detail and shape it into a PrDoc, or null if empty. */
856
+ async viewPr(query, number) {
857
+ const out = await this.run([
858
+ "pr",
859
+ "view",
860
+ String(number),
861
+ "--repo",
862
+ query.repo,
863
+ "--json",
864
+ VIEW_FIELDS
865
+ ]);
866
+ const v = GH_PR_VIEW.parse(parseJson(out));
867
+ const reviewComments = toReviewComments([
868
+ ...v.reviews ?? [],
869
+ ...v.comments ?? []
870
+ ]);
871
+ const reviewers = uniqueLogins([
872
+ ...(v.reviews ?? []).map((r) => r.author),
873
+ ...v.reviewRequests ?? []
874
+ ]);
875
+ const doc = {
876
+ number: v.number,
877
+ title: v.title ?? "",
878
+ body: v.body ?? "",
879
+ ...actorLabel(v.author) ? { author: actorLabel(v.author) } : {},
880
+ ...v.url ? { url: v.url } : {},
881
+ ...v.state ? { state: v.state.toLowerCase() } : {},
882
+ ...v.mergedAt ? { mergedAt: v.mergedAt } : {},
883
+ ...reviewers.length > 0 ? { reviewers } : {},
884
+ ...reviewComments.length > 0 ? { reviewComments } : {},
885
+ ...query.includeDiffSummary ? { diffSummary: buildDiffSummary(v) } : {}
886
+ };
887
+ return doc;
888
+ }
889
+ };
890
+ function actorLabel(actor) {
891
+ const label = actor?.login ?? actor?.name;
892
+ return label && label.length > 0 ? label : void 0;
893
+ }
894
+ function toReviewComments(raw) {
895
+ const out = [];
896
+ for (const c of raw) {
897
+ const body = (c.body ?? "").trim();
898
+ if (!body) continue;
899
+ const author = actorLabel(c.author);
900
+ const at = c.submittedAt ?? c.createdAt;
901
+ out.push({
902
+ body,
903
+ ...author ? { author } : {},
904
+ ...c.path ? { path: c.path } : {},
905
+ ...at ? { at } : {}
906
+ });
907
+ }
908
+ return out;
909
+ }
910
+ function uniqueLogins(actors) {
911
+ const seen = /* @__PURE__ */ new Set();
912
+ for (const a of actors) {
913
+ const label = actorLabel(a);
914
+ if (label) seen.add(label);
915
+ }
916
+ return [...seen];
917
+ }
918
+ function buildDiffSummary(v) {
919
+ const lines = [];
920
+ for (const f of v.files ?? []) {
921
+ const add = f.additions ?? 0;
922
+ const del = f.deletions ?? 0;
923
+ lines.push(`${f.path} | +${add} -${del}`);
924
+ }
925
+ const totals = v.changedFiles !== void 0 ? `${v.changedFiles} files changed, +${v.additions ?? 0} -${v.deletions ?? 0}` : void 0;
926
+ return [...totals ? [totals] : [], ...lines].join("\n");
927
+ }
928
+ function parseJson(out) {
929
+ const trimmed = out.trim();
930
+ if (!trimmed) return [];
931
+ return JSON.parse(trimmed);
932
+ }
933
+ function msg2(e) {
934
+ return e instanceof Error ? e.message : String(e);
935
+ }
936
+ function defaultGhRunner(bin) {
937
+ return async (args) => {
938
+ const { stdout } = await execFileAsync3(bin, [...args], {
939
+ maxBuffer: 32 * 1024 * 1024
940
+ });
941
+ return stdout;
942
+ };
943
+ }
764
944
 
765
945
  // src/ports/anthropic-distiller.ts
766
946
  var ANTHROPIC_RESPONSE = z.object({
@@ -1084,10 +1264,10 @@ var SseMcpClient = class {
1084
1264
  try {
1085
1265
  const parsed = RPC_MESSAGE.safeParse(JSON.parse(payload));
1086
1266
  if (!parsed.success) return;
1087
- const msg3 = parsed.data;
1088
- if (typeof msg3.id === "number" && this.pending.has(msg3.id)) {
1089
- this.pending.get(msg3.id)(msg3);
1090
- this.pending.delete(msg3.id);
1267
+ const msg4 = parsed.data;
1268
+ if (typeof msg4.id === "number" && this.pending.has(msg4.id)) {
1269
+ this.pending.get(msg4.id)(msg4);
1270
+ this.pending.delete(msg4.id);
1091
1271
  }
1092
1272
  } catch {
1093
1273
  }
@@ -1154,7 +1334,7 @@ var McpSink = class {
1154
1334
  try {
1155
1335
  client = await this.ensure();
1156
1336
  } catch (e) {
1157
- return { uri: item.uri, ok: false, error: `connect: ${msg2(e)}` };
1337
+ return { uri: item.uri, ok: false, error: `connect: ${msg3(e)}` };
1158
1338
  }
1159
1339
  const args = template(this.config.args, item);
1160
1340
  const call = this.config.importedAlias ? { name: "mcp_imported_call", args: { alias: this.config.importedAlias, toolName: this.config.tool, args } } : { name: this.config.tool, args };
@@ -1163,7 +1343,7 @@ var McpSink = class {
1163
1343
  if (res.isError) return { uri: item.uri, ok: false, error: "tool returned isError" };
1164
1344
  return { uri: item.uri, ok: true };
1165
1345
  } catch (e) {
1166
- return { uri: item.uri, ok: false, error: msg2(e) };
1346
+ return { uri: item.uri, ok: false, error: msg3(e) };
1167
1347
  }
1168
1348
  }
1169
1349
  };
@@ -1207,10 +1387,10 @@ function stringify(v) {
1207
1387
  if (Array.isArray(v)) return v.join(", ");
1208
1388
  return v == null ? "" : String(v);
1209
1389
  }
1210
- function msg2(e) {
1390
+ function msg3(e) {
1211
1391
  return e instanceof Error ? e.message : String(e);
1212
1392
  }
1213
1393
 
1214
- export { AnthropicDistiller, AssemblyAiStt, BrowserMcpFetcher, CLI_ENGINES, ChunkedStt, CliAgentDistiller, CompositeFetcher, HttpReadabilityFetcher, McpSink, NodeFsAdapter, OpenAiWhisperStt, OsIdentityAdapter, ScrapeMcpFetcher, ThrottleFetcher, YtDlpWhisperFetcher, connectMcpHttp };
1394
+ export { AnthropicDistiller, AssemblyAiStt, BrowserMcpFetcher, CLI_ENGINES, ChunkedStt, CliAgentDistiller, CompositeFetcher, GhPrSourceAdapter, HttpReadabilityFetcher, McpSink, NodeFsAdapter, OpenAiWhisperStt, OsIdentityAdapter, ScrapeMcpFetcher, ThrottleFetcher, YtDlpWhisperFetcher, connectMcpHttp };
1215
1395
  //# sourceMappingURL=index.mjs.map
1216
1396
  //# sourceMappingURL=index.mjs.map