@tokenroll/llmdoc 3.6.0 → 3.9.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,35 +1,43 @@
1
1
  import { loadWorkspace } from "../lib/workspace.js";
2
- import { matchesCodePathPattern } from "../lib/search.js";
3
2
  import { CliError } from "../lib/errors.js";
4
3
  import { formatPaginationSummary } from "../lib/format.js";
5
4
  import { paginate, paginationMetadata } from "../lib/pagination.js";
6
- import { normalizeRepoRelativePath, resolveInsideRoot } from "../lib/fs.js";
7
5
  import { estimateTokens } from "../lib/markdown.js";
6
+ import { getDocumentRequires, matchLocalContextFiles, resolveContextFileInput, resolveContextFiles } from "../lib/routing.js";
7
+ import { formatDocumentPath, isSourceRef, parseSourceDocId } from "../lib/source-id.js";
8
+ import { ensureSourcesForRead } from "../lib/sources.js";
9
+ import { recordTelemetryEvent } from "../lib/telemetry.js";
8
10
  export function runContext(options) {
9
11
  if (options.files.length === 0) {
10
12
  throw new CliError("context requires at least one --files input.");
11
13
  }
12
14
  const workspace = loadWorkspace(options.cwd);
13
- const normalizedFiles = options.files.map((file) => {
14
- const normalized = normalizeRepoRelativePath(file);
15
- // 允许尚不存在的路径:"我要新建/删除这个文件,该先读哪些文档"是合法问法;
16
- // resolveInsideRoot 仍负责拦截越界与 symlink 逃逸。
17
- const absolutePath = resolveInsideRoot(workspace.rootDir, normalized, { allowMissing: true });
18
- return {
19
- normalized,
20
- absolutePath
21
- };
15
+ const normalizedFiles = options.files.map((file) => resolveContextFileInput(workspace.rootDir, file));
16
+ const prematchedLocal = matchLocalContextFiles(workspace, normalizedFiles);
17
+ const { localImpacted, unmappedFiles } = prematchedLocal;
18
+ const warnings = [];
19
+ const warnedAliases = new Set();
20
+ const relevantGitAliases = collectRelevantGitAliases(workspace, localImpacted);
21
+ if (relevantGitAliases.length > 0) {
22
+ const cold = ensureSourcesForRead(workspace, relevantGitAliases);
23
+ warnings.push(...cold.warnings);
24
+ for (const alias of relevantGitAliases) {
25
+ const source = workspace.sourcesByName.get(alias);
26
+ if (source && source.status !== "ready") {
27
+ warnedAliases.add(alias);
28
+ }
29
+ }
30
+ }
31
+ for (const source of workspace.sources) {
32
+ if (source.kind === "git" && source.status !== "ready" && !warnedAliases.has(source.name)) {
33
+ warnedAliases.add(source.name);
34
+ warnings.push(`Source @${source.name} was not searched because it is unavailable: ${source.error ?? "no local snapshot"}.${source.remedy ? ` ${source.remedy}` : ""}`);
35
+ }
36
+ }
37
+ const { sourceImpacted, impacted, prerequisites, missingPrerequisites } = resolveContextFiles(workspace, normalizedFiles, {
38
+ includeSources: true,
39
+ prematchedLocal
22
40
  });
23
- const documentsByFile = normalizedFiles.map((file) => ({
24
- file,
25
- documents: workspace.documents.filter((document) => (document.frontmatter.code?.paths ?? []).some((pattern) => matchesCodePathPattern(pattern, file.normalized)))
26
- }));
27
- const impactedPaths = new Set(documentsByFile.flatMap(({ documents }) => documents.map((document) => document.llmdocPath)));
28
- const impacted = workspace.documents.filter((document) => impactedPaths.has(document.llmdocPath));
29
- const unmappedFiles = documentsByFile
30
- .filter(({ documents }) => documents.length === 0)
31
- .map(({ file }) => file.normalized);
32
- const prerequisites = collectRequires(workspace, impacted);
33
41
  const rows = [
34
42
  ...impacted.map((document) => ({ role: "impacted", document })),
35
43
  ...prerequisites.map((document) => ({ role: "requires", document }))
@@ -39,53 +47,85 @@ export function runContext(options) {
39
47
  estimate: (row) => estimateTokens(JSON.stringify({ role: row.role, ...toPayload(row.document) })),
40
48
  options
41
49
  });
50
+ recordTelemetryEvent(workspace.rootDir, {
51
+ type: "context",
52
+ docs: localImpacted.map((document) => document.llmdocPath),
53
+ unmapped: unmappedFiles
54
+ });
42
55
  if (options.json) {
43
56
  return {
44
57
  impacted: result.items.filter((row) => row.role === "impacted").map((row) => toPayload(row.document)),
45
58
  prerequisites: result.items.filter((row) => row.role === "requires").map((row) => toPayload(row.document)),
59
+ ...(missingPrerequisites.length > 0 ? { missingPrerequisites } : {}),
46
60
  unmappedFiles,
61
+ ...(warnings.length > 0 ? { warnings } : {}),
47
62
  pagination: paginationMetadata(result)
48
63
  };
49
64
  }
50
- const lines = [`${impacted.length} documents impacted, ${prerequisites.length} recommended prerequisites:`, ""];
51
- for (const row of result.items) {
52
- const prefix = row.role === "requires" ? " requires ->" : " ";
53
- const document = row.document;
54
- lines.push(`${prefix} llmdoc/${document.llmdocPath} [${document.frontmatter.kind}]`);
55
- lines.push(` ${document.frontmatter.description}`);
65
+ const impactedWord = impacted.length === 1 ? "document" : "documents";
66
+ const prereqWord = prerequisites.length === 1 ? "prerequisite" : "prerequisites";
67
+ const lines = [`${impacted.length} ${impactedWord} impacted, ${prerequisites.length} recommended ${prereqWord}:`];
68
+ if (result.items.length > 0) {
69
+ lines.push("");
70
+ for (const row of result.items) {
71
+ const prefix = row.role === "requires" ? " requires -> " : " ";
72
+ const document = row.document;
73
+ lines.push(`${prefix}${formatDocumentPath(document)} [${document.frontmatter.kind}]`);
74
+ lines.push(` ${document.frontmatter.description}`);
75
+ }
76
+ }
77
+ if (missingPrerequisites.length > 0) {
78
+ lines.push("", `missing external prerequisites: ${missingPrerequisites.length}`);
79
+ for (const missing of missingPrerequisites) {
80
+ lines.push(` missing -> ${missing.path} (${missing.reason})`);
81
+ }
82
+ }
83
+ if (warnings.length > 0) {
84
+ lines.push("");
85
+ for (const warning of warnings) {
86
+ lines.push(`warning: ${warning}`);
87
+ }
56
88
  }
57
- lines.push("", `unmapped files: ${unmappedFiles.length}`);
89
+ const hasExternalHitsWithUnmapped = sourceImpacted.length > 0 && unmappedFiles.length > 0;
90
+ lines.push("", hasExternalHitsWithUnmapped
91
+ ? `unmapped files: ${unmappedFiles.length} (no local llmdoc owner)`
92
+ : `unmapped files: ${unmappedFiles.length}`);
58
93
  for (const file of unmappedFiles) {
59
94
  lines.push(` unmapped -> ${file}`);
60
95
  }
61
96
  lines.push("", ...formatPaginationSummary(result));
62
97
  return lines.join("\n");
63
98
  }
64
- function collectRequires(workspace, impacted) {
65
- const seen = new Set(impacted.map((document) => document.llmdocPath));
66
- const queue = [...impacted];
67
- const results = [];
68
- while (queue.length > 0) {
69
- const current = queue.shift();
70
- for (const requirement of current.frontmatter.relations?.requires ?? []) {
71
- if (seen.has(requirement)) {
99
+ function collectRelevantGitAliases(workspace, localImpacted) {
100
+ const aliases = new Set();
101
+ for (const source of workspace.sources) {
102
+ if (source.kind === "git" && source.status === "missing") {
103
+ aliases.add(source.name);
104
+ }
105
+ }
106
+ for (const document of localImpacted) {
107
+ for (const req of getDocumentRequires(document)) {
108
+ if (!isSourceRef(req)) {
72
109
  continue;
73
110
  }
74
- const next = workspace.documentsByLlmdocPath.get(requirement);
75
- if (!next) {
111
+ try {
112
+ const parsed = parseSourceDocId(req);
113
+ if (workspace.sourcesByName.get(parsed.alias)?.kind === "git") {
114
+ aliases.add(parsed.alias);
115
+ }
116
+ }
117
+ catch {
76
118
  continue;
77
119
  }
78
- seen.add(requirement);
79
- results.push(next);
80
- queue.push(next);
81
120
  }
82
121
  }
83
- return results.sort((left, right) => left.llmdocPath.localeCompare(right.llmdocPath));
122
+ return [...aliases];
84
123
  }
85
124
  function toPayload(document) {
86
125
  return {
87
- path: `llmdoc/${document.llmdocPath}`,
126
+ path: formatDocumentPath(document),
88
127
  kind: document.frontmatter.kind,
89
- description: document.frontmatter.description
128
+ description: document.frontmatter.description,
129
+ ...(document.sourceName ? { source: document.sourceName } : {})
90
130
  };
91
131
  }