@tokenroll/llmdoc 3.5.3 → 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.
Files changed (48) hide show
  1. package/assets/viewer-app.js +11 -11
  2. package/assets/viewer-detail.js +4 -4
  3. package/assets/viewer-graph.js +3 -3
  4. package/assets/viewer.html +19 -19
  5. package/dist/cli.js +147 -71
  6. package/dist/commands/adopt.js +10 -6
  7. package/dist/commands/commit.js +232 -80
  8. package/dist/commands/context.js +85 -45
  9. package/dist/commands/dream.js +608 -0
  10. package/dist/commands/eval.js +379 -0
  11. package/dist/commands/fingerprint.js +9 -3
  12. package/dist/commands/hook.js +135 -28
  13. package/dist/commands/index.js +52 -7
  14. package/dist/commands/init-state.js +5 -5
  15. package/dist/commands/mv.js +33 -12
  16. package/dist/commands/new.js +12 -5
  17. package/dist/commands/prune.js +14 -3
  18. package/dist/commands/search.js +67 -4
  19. package/dist/commands/serve.js +1 -1
  20. package/dist/commands/show.js +40 -6
  21. package/dist/commands/status.js +46 -18
  22. package/dist/commands/tree.js +94 -6
  23. package/dist/commands/upgrade.js +15 -15
  24. package/dist/lib/config.js +502 -0
  25. package/dist/lib/constants.js +23 -0
  26. package/dist/lib/doc-shape.js +5 -5
  27. package/dist/lib/frontmatter.js +47 -0
  28. package/dist/lib/fs.js +8 -8
  29. package/dist/lib/git.js +147 -42
  30. package/dist/lib/markdown.js +8 -7
  31. package/dist/lib/output-schema.js +3 -3
  32. package/dist/lib/package-root.js +1 -1
  33. package/dist/lib/pagination.js +2 -2
  34. package/dist/lib/rewrite.js +9 -3
  35. package/dist/lib/routing.js +152 -0
  36. package/dist/lib/schema.js +16 -0
  37. package/dist/lib/search.js +35 -16
  38. package/dist/lib/source-id.js +49 -0
  39. package/dist/lib/sources.js +760 -0
  40. package/dist/lib/state.js +74 -25
  41. package/dist/lib/telemetry.js +726 -0
  42. package/dist/lib/viewer-http.js +2 -2
  43. package/dist/lib/workspace.js +187 -43
  44. package/package.json +1 -1
  45. package/schemas/config.schema.json +116 -0
  46. package/schemas/meta.schema.json +5 -1
  47. package/schemas/output.schema.json +442 -0
  48. package/schemas/probes.schema.json +90 -0
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import { Minimatch } from "minimatch";
3
3
  import { CACHE_DIR, SEARCH_CACHE_FILE } from "./constants.js";
4
4
  import { ensureDirectory, resolveInsideRoot } from "./fs.js";
5
+ import { formatDocumentPath, matchesDocumentTopic } from "./source-id.js";
5
6
  const WORD_SEGMENTER = new Intl.Segmenter("zh-CN", { granularity: "word" });
6
7
  const CJK_CHARACTER = /^[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]$/u;
7
8
  const CJK_RUN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]{2,}/gu;
@@ -31,9 +32,14 @@ const QUERY_STOP_WORDS = new Set([
31
32
  export function searchDocuments(workspace, query, filters) {
32
33
  const normalizedQuery = query.trim().toLowerCase();
33
34
  const lexicalTokens = tokenizeQuery(query).map((value) => ({ value, weight: 1 }));
34
- const cache = loadSearchCache(workspace);
35
- const filteredDocs = workspace.documents.filter((document) => {
36
- if (filters?.topic && document.topic !== filters.topic) {
35
+ const includeSources = filters?.includeSources ?? true;
36
+ const useCache = filters?.useCache ?? true;
37
+ const allDocuments = includeSources ? [...workspace.documents, ...workspace.sourceDocuments] : [...workspace.documents];
38
+ // Persist only local workspace documents to the repository search cache so external private
39
+ // source bodies and ~/.llmdoc paths are never copied into the consumer repository.
40
+ const cache = useCache ? loadSearchCache(workspace, workspace.documents) : { version: 3, entries: {} };
41
+ const filteredDocs = allDocuments.filter((document) => {
42
+ if (filters?.topic && !matchesDocumentTopic(document, filters.topic)) {
37
43
  return false;
38
44
  }
39
45
  if (filters?.kind && document.frontmatter.kind !== filters.kind) {
@@ -72,7 +78,8 @@ function scoreDocuments(documents, cache, tokens, options) {
72
78
  for (const token of tokens) {
73
79
  let count = 0;
74
80
  for (const document of documents) {
75
- const searchableText = cache.entries[document.llmdocPath]?.searchableText ?? buildSearchableText(document);
81
+ const cacheKey = formatDocumentPath(document);
82
+ const searchableText = cache.entries[cacheKey]?.searchableText ?? buildSearchableText(document);
76
83
  if (searchableText.includes(token.value)) {
77
84
  count += 1;
78
85
  }
@@ -81,8 +88,9 @@ function scoreDocuments(documents, cache, tokens, options) {
81
88
  }
82
89
  return documents
83
90
  .map((document) => {
84
- const searchableText = cache.entries[document.llmdocPath]?.searchableText ?? buildSearchableText(document);
85
- const wordCount = cache.entries[document.llmdocPath]?.wordCount ?? countWords(searchableText);
91
+ const cacheKey = formatDocumentPath(document);
92
+ const searchableText = cache.entries[cacheKey]?.searchableText ?? buildSearchableText(document);
93
+ const wordCount = cache.entries[cacheKey]?.wordCount ?? countWords(searchableText);
86
94
  let score = 0;
87
95
  let matchedTokenCount = 0;
88
96
  for (const token of tokens) {
@@ -129,24 +137,35 @@ function normalizeCodePathPattern(pattern) {
129
137
  }
130
138
  return normalized.replace(/\/+$/, "");
131
139
  }
132
- function loadSearchCache(workspace) {
140
+ function loadSearchCache(workspace, documents) {
133
141
  const next = {
134
- version: 2,
142
+ version: 3,
135
143
  entries: {}
136
144
  };
137
145
  const cachePath = resolveCachePath(workspace);
138
146
  const existing = cachePath ? readCache(cachePath) : null;
139
- for (const document of workspace.documents) {
140
- const stats = fs.statSync(document.absolutePath);
141
- const cachedEntry = existing?.entries[document.llmdocPath];
142
- if (existing?.version === 2 && cachedEntry && cachedEntry.mtimeMs === stats.mtimeMs) {
143
- next.entries[document.llmdocPath] = cachedEntry;
147
+ for (const document of documents) {
148
+ let mtimeMs = 0;
149
+ try {
150
+ mtimeMs = fs.statSync(document.absolutePath).mtimeMs;
151
+ }
152
+ catch {
153
+ continue;
154
+ }
155
+ const cacheKey = formatDocumentPath(document);
156
+ const cachedEntry = existing?.entries[cacheKey];
157
+ if (existing?.version === 3 &&
158
+ cachedEntry &&
159
+ cachedEntry.mtimeMs === mtimeMs &&
160
+ cachedEntry.absolutePath === document.absolutePath) {
161
+ next.entries[cacheKey] = cachedEntry;
144
162
  continue;
145
163
  }
146
164
  const searchableText = buildSearchableText(document);
147
- next.entries[document.llmdocPath] = {
148
- llmdocPath: document.llmdocPath,
149
- mtimeMs: stats.mtimeMs,
165
+ next.entries[cacheKey] = {
166
+ llmdocPath: cacheKey,
167
+ absolutePath: document.absolutePath,
168
+ mtimeMs,
150
169
  searchableText,
151
170
  wordCount: countWords(searchableText)
152
171
  };
@@ -0,0 +1,49 @@
1
+ import { CliError } from "./errors.js";
2
+ import { normalizeRepoRelativePath } from "./fs.js";
3
+ export const SOURCE_ALIAS_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
4
+ export function isValidSourceAlias(alias) {
5
+ return SOURCE_ALIAS_PATTERN.test(alias);
6
+ }
7
+ export function isSourceRef(input) {
8
+ return input.trim().startsWith("@");
9
+ }
10
+ export function formatDocumentPath(doc) {
11
+ return doc.sourceName ? doc.llmdocPath : `llmdoc/${doc.llmdocPath}`;
12
+ }
13
+ function relativePath(input) {
14
+ const normalized = input.replaceAll("\\", "/");
15
+ if (/[\p{Cc}<>:"|?*]/u.test(normalized) || normalized.split("/").includes(".")) {
16
+ throw new CliError(`Invalid source path: ${input}`);
17
+ }
18
+ return normalizeRepoRelativePath(normalized);
19
+ }
20
+ export function normalizeSourceSubdir(input) {
21
+ return relativePath(input.replaceAll("\\", "/").replace(/\/$/u, ""));
22
+ }
23
+ function splitRef(input) {
24
+ const [prefix, ...rest] = input.trim().replaceAll("\\", "/").split("/");
25
+ const alias = prefix.slice(1);
26
+ if (!prefix.startsWith("@") || !isValidSourceAlias(alias)) {
27
+ throw new CliError(`Invalid source alias: ${prefix}`);
28
+ }
29
+ return { alias, rest };
30
+ }
31
+ export function parseSourceDocId(input) {
32
+ const { alias, rest } = splitRef(input);
33
+ const normalized = relativePath(rest.join("/"));
34
+ if (!/\.mdx?$/u.test(normalized)) {
35
+ throw new CliError("A source document ID must end in .md or .mdx.");
36
+ }
37
+ return { alias, relativePath: normalized, canonicalId: `@${alias}/${normalized}` };
38
+ }
39
+ export function parseSourceTopicFilter(input) {
40
+ const { alias, rest } = splitRef(input);
41
+ return { alias, subdir: rest.length === 0 ? null : normalizeSourceSubdir(rest.join("/")) };
42
+ }
43
+ export function matchesDocumentTopic(doc, topicFilter) {
44
+ if (!isSourceRef(topicFilter))
45
+ return !doc.sourceName && doc.topic === topicFilter;
46
+ const { alias, subdir } = parseSourceTopicFilter(topicFilter);
47
+ return doc.sourceName === alias && (subdir === null || doc.sourceRelativePath === subdir ||
48
+ doc.sourceRelativePath?.startsWith(`${subdir}/`) === true);
49
+ }