pi-para 0.6.7 → 0.7.1

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 (133) hide show
  1. package/ARCHITECTURE.md +4 -6
  2. package/CHANGELOG.md +19 -0
  3. package/CONFIGURATION.md +1 -16
  4. package/README.md +2 -13
  5. package/SETUP.md +1 -7
  6. package/STRUCTURE.md +7 -18
  7. package/dist/cli-legacy.d.ts +2 -1
  8. package/dist/cli-legacy.d.ts.map +1 -1
  9. package/dist/cli-legacy.js +13 -25
  10. package/dist/cli-legacy.js.map +1 -1
  11. package/dist/cli.d.ts +2 -24
  12. package/dist/cli.d.ts.map +1 -1
  13. package/dist/cli.js +29 -271
  14. package/dist/cli.js.map +1 -1
  15. package/dist/commands.d.ts.map +1 -1
  16. package/dist/commands.js +1 -28
  17. package/dist/commands.js.map +1 -1
  18. package/dist/config.d.ts +1 -5
  19. package/dist/config.d.ts.map +1 -1
  20. package/dist/config.js +0 -5
  21. package/dist/config.js.map +1 -1
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +6 -64
  24. package/dist/index.js.map +1 -1
  25. package/dist/link-utils.d.ts +1 -1
  26. package/dist/link-utils.js +1 -1
  27. package/dist/processor.d.ts +7 -0
  28. package/dist/processor.d.ts.map +1 -1
  29. package/dist/processor.js +29 -15
  30. package/dist/processor.js.map +1 -1
  31. package/dist/settings.d.ts +0 -3
  32. package/dist/settings.d.ts.map +1 -1
  33. package/dist/settings.js +0 -11
  34. package/dist/settings.js.map +1 -1
  35. package/dist/tools/edit-read.d.ts +18 -0
  36. package/dist/tools/edit-read.d.ts.map +1 -0
  37. package/dist/tools/edit-read.js +161 -0
  38. package/dist/tools/edit-read.js.map +1 -0
  39. package/dist/tools/ingest.d.ts +12 -0
  40. package/dist/tools/ingest.d.ts.map +1 -0
  41. package/dist/tools/ingest.js +48 -0
  42. package/dist/tools/ingest.js.map +1 -0
  43. package/dist/tools/lint.d.ts +10 -0
  44. package/dist/tools/lint.d.ts.map +1 -0
  45. package/dist/tools/lint.js +57 -0
  46. package/dist/tools/lint.js.map +1 -0
  47. package/dist/tools/migrate.d.ts +6 -0
  48. package/dist/tools/migrate.d.ts.map +1 -0
  49. package/dist/tools/migrate.js +36 -0
  50. package/dist/tools/migrate.js.map +1 -0
  51. package/dist/tools/move.d.ts +9 -0
  52. package/dist/tools/move.d.ts.map +1 -0
  53. package/dist/tools/move.js +61 -0
  54. package/dist/tools/move.js.map +1 -0
  55. package/dist/tools/query.d.ts +12 -0
  56. package/dist/tools/query.d.ts.map +1 -0
  57. package/dist/tools/query.js +47 -0
  58. package/dist/tools/query.js.map +1 -0
  59. package/dist/tools/schemas.d.ts +109 -0
  60. package/dist/tools/schemas.d.ts.map +1 -0
  61. package/dist/tools/schemas.js +80 -0
  62. package/dist/tools/schemas.js.map +1 -0
  63. package/dist/tools/shared.d.ts +14 -0
  64. package/dist/tools/shared.d.ts.map +1 -0
  65. package/dist/tools/shared.js +25 -0
  66. package/dist/tools/shared.js.map +1 -0
  67. package/dist/tools/summarize.d.ts +8 -0
  68. package/dist/tools/summarize.d.ts.map +1 -0
  69. package/dist/tools/summarize.js +82 -0
  70. package/dist/tools/summarize.js.map +1 -0
  71. package/dist/tools/write.d.ts +23 -0
  72. package/dist/tools/write.d.ts.map +1 -0
  73. package/dist/tools/write.js +215 -0
  74. package/dist/tools/write.js.map +1 -0
  75. package/dist/tools.d.ts +1 -8
  76. package/dist/tools.d.ts.map +1 -1
  77. package/dist/tools.js +13 -795
  78. package/dist/tools.js.map +1 -1
  79. package/dist/wiki.d.ts.map +1 -1
  80. package/dist/wiki.js +1 -0
  81. package/dist/wiki.js.map +1 -1
  82. package/package.json +2 -6
  83. package/src/cli-legacy.ts +14 -26
  84. package/src/cli.ts +29 -293
  85. package/src/commands.ts +0 -21
  86. package/src/config.ts +1 -6
  87. package/src/index.ts +5 -66
  88. package/src/link-utils.ts +1 -1
  89. package/src/processor.ts +28 -13
  90. package/src/settings.ts +0 -14
  91. package/src/tools/edit-read.ts +193 -0
  92. package/src/tools/ingest.ts +63 -0
  93. package/src/tools/lint.ts +74 -0
  94. package/src/tools/migrate.ts +44 -0
  95. package/src/tools/move.ts +79 -0
  96. package/src/tools/query.ts +63 -0
  97. package/src/tools/schemas.ts +157 -0
  98. package/src/tools/shared.ts +35 -0
  99. package/src/tools/summarize.ts +89 -0
  100. package/src/tools/write.ts +252 -0
  101. package/src/tools.ts +18 -1046
  102. package/src/wiki.ts +1 -0
  103. package/dist/daemon.d.ts +0 -49
  104. package/dist/daemon.d.ts.map +0 -1
  105. package/dist/daemon.js +0 -226
  106. package/dist/daemon.js.map +0 -1
  107. package/dist/link-discovery.d.ts +0 -40
  108. package/dist/link-discovery.d.ts.map +0 -1
  109. package/dist/link-discovery.js +0 -122
  110. package/dist/link-discovery.js.map +0 -1
  111. package/dist/maintainer.d.ts +0 -24
  112. package/dist/maintainer.d.ts.map +0 -1
  113. package/dist/maintainer.js +0 -358
  114. package/dist/maintainer.js.map +0 -1
  115. package/dist/watcher.d.ts +0 -25
  116. package/dist/watcher.d.ts.map +0 -1
  117. package/dist/watcher.js +0 -84
  118. package/dist/watcher.js.map +0 -1
  119. package/dist/webui/client/dist/index.html +0 -1885
  120. package/dist/webui/server.d.ts +0 -31
  121. package/dist/webui/server.d.ts.map +0 -1
  122. package/dist/webui/server.js +0 -566
  123. package/dist/webui/server.js.map +0 -1
  124. package/scripts/build-assets.mjs +0 -17
  125. package/src/daemon.ts +0 -288
  126. package/src/link-discovery.ts +0 -170
  127. package/src/maintainer.ts +0 -404
  128. package/src/python/__pycache__/minimax_lm.cpython-312.pyc +0 -0
  129. package/src/python/minimax_lm.py +0 -198
  130. package/src/python/rlm_capture.py +0 -215
  131. package/src/watcher.ts +0 -101
  132. package/src/webui/client/dist/index.html +0 -1885
  133. package/src/webui/server.ts +0 -765
package/dist/tools.js CHANGED
@@ -1,799 +1,17 @@
1
- /**
2
- * Tool registrations — wiki tools for both pi's session agent and standalone mini-agent.
3
- *
4
- * Tool implementations are shared between both consumers. Only the registration
5
- * wrapper differs (pi.registerTool vs AgentTool).
6
- *
7
- * Factory functions (createXxxExecute) close over wikiDir and store — no ctx dependency.
8
- */
9
- import { StringEnum } from "@earendil-works/pi-ai";
1
+ /** Tool registration composition for pi sessions and standalone mini-agents. */
10
2
  import { Text } from "@earendil-works/pi-tui";
11
- import { Type } from "typebox";
12
- import { resolveSource, truncateSource, detectSourceType } from "./ingest.js";
13
- import { queryWiki as queryWikiLib, formatQueryResults, formatFreshness } from "./query.js";
14
- import { readPage, writePage, movePage, listPages, listPageSlugs, readIndex, writeIndex, readSchema, appendLog, gitCommit, rebuildIndex, PARA_CATEGORIES, } from "./wiki.js";
15
- import { validateFrontmatter, parseFrontmatter, CURRENT_SCHEMA_VERSION, migrateToLatest, } from "./frontmatter.js";
16
- import { extractWikilinks, autoLinkSlugs, syncFrontmatterLinks } from "./link-utils.js";
17
- import { normalizeTags, normalizeScopes } from "./tag-registry.js";
18
- import { redactSecrets } from "./redact.js";
19
- import { lintWiki } from "./lint.js";
20
- import { generateOverviewPrompt } from "./summarize.js";
21
- import { INGEST_PROMPT, QUERY_PROMPT, LINT_PROMPT, } from "./templates/prompts.js";
22
- import { enqueueWikiMaintenance } from "./scheduler/index.js";
23
- import { WIKI_TOOL_DESCRIPTIONS, WIKI_TOOL_SNIPPETS, getWikiToolGuidelines, } from "./wiki-tool-guidance.js";
24
- // -- Parameter schemas -------------------------------------------------------
25
- const PARA_ENUM = StringEnum(["projects", "areas", "resources", "archives"], { description: "PARA category" });
26
- const PARA_ENUM_NO_ARCHIVES = StringEnum(["projects", "areas", "resources"], { description: "PARA category (archives excluded for new content)" });
27
- const WikiIngestParams = Type.Object({
28
- source: Type.String({ description: "URL, file path, or raw text to ingest" }),
29
- sourceType: Type.Optional(StringEnum(["url", "file", "text"], {
30
- description: "Source type (auto-detected if omitted)",
31
- })),
32
- category: Type.Optional(PARA_ENUM_NO_ARCHIVES),
33
- scope: Type.Optional(Type.Array(Type.String(), { description: "Scope tags for the ingested content" })),
34
- });
35
- const WikiQueryParams = Type.Object({
36
- query: Type.String({ description: "Natural language search query" }),
37
- global: Type.Optional(Type.Boolean({ description: "Search all scopes, not just current project" })),
38
- category: Type.Optional(PARA_ENUM),
39
- limit: Type.Optional(Type.Number({ description: "Max results (default 10)" })),
40
- });
41
- const WikiEditSchema = Type.Object({
42
- oldText: Type.String({ description: "Exact text to find in the page body" }),
43
- newText: Type.String({ description: "Replacement text" }),
44
- });
45
- const WikiWritePageSchema = Type.Object({
46
- category: PARA_ENUM,
47
- slug: Type.String({ description: "Page slug (lowercase, hyphens)" }),
48
- title: Type.String({ description: "Page title" }),
49
- scope: Type.Array(Type.String(), { description: "Scope tags" }),
50
- tags: Type.Array(Type.String(), { description: "Topic tags" }),
51
- body: Type.String({ description: "Page body in markdown (wiki summary format)" }),
52
- mode: StringEnum(["create", "replace", "append", "edit"], {
53
- description: "Write mode: create new, replace existing, append to existing, or edit specific sections",
54
- }),
55
- edits: Type.Optional(Type.Array(WikiEditSchema, {
56
- description: "For mode=edit: targeted text replacements (oldText→newText). Page must exist.",
57
- })),
58
- });
59
- const WikiWriteParams = Type.Object({
60
- pages: Type.Array(WikiWritePageSchema, {
61
- description: "Pages to write or update",
62
- }),
63
- // DEPRECATED: indexContent is ignored. Index is auto-rebuilt from all pages on disk.
64
- // Kept in schema to avoid breaking existing callers.
65
- indexContent: Type.Optional(Type.String({ description: "Updated index.md content (full replacement)" })),
66
- logSummary: Type.Optional(Type.String({ description: "One-line summary for log.md" })),
67
- });
68
- const WikiEditParams = Type.Object({
69
- path: Type.String({ description: "Existing page path (e.g. 'resources/ssl-cert-gotchas')" }),
70
- edits: Type.Array(WikiEditSchema, {
71
- description: "Atomic exact text replacements. Every oldText must appear exactly once or no changes are written.",
72
- }),
73
- title: Type.Optional(Type.String({ description: "Optional replacement page title" })),
74
- scope: Type.Optional(Type.Array(Type.String(), { description: "Optional replacement scope tags" })),
75
- tags: Type.Optional(Type.Array(Type.String(), { description: "Optional replacement topic tags" })),
76
- logSummary: Type.Optional(Type.String({ description: "One-line summary for log.md" })),
77
- });
78
- const WikiReadParams = Type.Object({
79
- path: Type.String({
80
- description: "Page path (e.g. 'projects/auth-refactor') or page title",
81
- }),
82
- });
83
- const WikiMoveParams = Type.Object({
84
- path: Type.String({
85
- description: "Current page path (e.g. 'projects/auth-refactor')",
86
- }),
87
- to: PARA_ENUM,
88
- });
89
- const WikiLintParams = Type.Object({
90
- autoFix: Type.Optional(Type.Boolean({ description: "Auto-fix simple issues (default true)" })),
91
- });
92
- const WikiMigrateParams = Type.Object({});
93
- const WikiSummarizeParams = Type.Object({
94
- target: Type.String({
95
- description: "Page path, category name (e.g. 'projects'), or 'all'",
96
- }),
97
- depth: Type.Optional(StringEnum(["brief", "detailed"], {
98
- description: "Summary depth (default brief)",
99
- })),
100
- });
101
- // -- Source resolution helpers -----------------------------------------------
102
- /** Resolve a page path like "projects/auth-refactor" into category + slug. */
103
- function parsePagePath(path) {
104
- const parts = path.split("/");
105
- if (parts.length === 2) {
106
- const cat = parts[0];
107
- if (PARA_CATEGORIES.includes(cat)) {
108
- const slug = parts[1].replace(/\.md$/, "");
109
- return { category: cat, slug };
110
- }
111
- }
112
- return null;
113
- }
114
- // -- Factory: wiki_ingest execute --------------------------------------------
115
- function createIngestExecute(wikiDir, _store, getScope) {
116
- return async (params) => {
117
- const scope = getScope();
118
- const resolved = await resolveSource(wikiDir, {
119
- source: params.source,
120
- sourceType: params.sourceType,
121
- scope: params.scope,
122
- category: params.category,
123
- }, scope);
124
- const categoryHint = resolved.categoryHint
125
- ? `\nSuggested PARA category: ${resolved.categoryHint}`
126
- : "";
127
- // Assemble the tool result with instructions for the LLM
128
- const toolResultText = [
129
- INGEST_PROMPT,
130
- "",
131
- `Current scope: ${resolved.scopeName} (tags: ${resolved.scopeTags.join(", ")})`,
132
- categoryHint,
133
- resolved.rawPath ? `Raw source saved to: ${resolved.rawPath}` : "",
134
- "",
135
- "<wiki-schema>",
136
- resolved.schema,
137
- "</wiki-schema>",
138
- "",
139
- "<wiki-index>",
140
- resolved.index,
141
- "</wiki-index>",
142
- "",
143
- "<source-content>",
144
- truncateSource(resolved.content),
145
- "</source-content>",
146
- "",
147
- "Now analyze the source content and use wiki_write to create/update pages, update the index, and log the operation.",
148
- ].join("\n");
149
- return {
150
- content: [{ type: "text", text: toolResultText }],
151
- details: {
152
- sourceType: resolved.sourceType,
153
- rawPath: resolved.rawPath,
154
- sourceLength: resolved.content.length,
155
- },
156
- };
157
- };
158
- }
159
- // -- Factory: wiki_query execute ---------------------------------------------
160
- function createQueryExecute(_wikiDir, store, getScope, getGraphBoost) {
161
- return async (params) => {
162
- const scope = getScope();
163
- const result = await queryWikiLib(store, {
164
- query: params.query,
165
- global: params.global,
166
- category: params.category,
167
- limit: params.limit,
168
- graphBoost: getGraphBoost?.() ?? true,
169
- }, scope);
170
- if (result.results.length === 0) {
171
- return {
172
- content: [
173
- {
174
- type: "text",
175
- text: `No wiki pages found for "${params.query}". The wiki may not have content on this topic yet.`,
176
- },
177
- ],
178
- details: {
179
- resultCount: 0,
180
- scopeUsed: result.wasGlobal ? "global" : scope.name,
181
- },
182
- };
183
- }
184
- const formatted = formatQueryResults(result.results);
185
- const toolResultText = [
186
- QUERY_PROMPT,
187
- "",
188
- `Query: "${params.query}"`,
189
- `Scope: ${result.wasGlobal ? "global (all scopes)" : scope.name}`,
190
- `Results: ${result.results.length}`,
191
- "",
192
- formatted,
193
- ].join("\n");
194
- return {
195
- content: [{ type: "text", text: toolResultText }],
196
- details: {
197
- resultCount: result.results.length,
198
- scopeUsed: result.wasGlobal ? "global" : scope.name,
199
- },
200
- };
201
- };
202
- }
203
- // -- Background maintenance --------------------------------------------------
204
- /**
205
- * Schedule index rebuild + QMD reindex after latency-sensitive edits.
206
- * Debounced per wiki directory and coordinated through the scheduler queue so
207
- * multiple Pi processes do not run the same maintenance task concurrently.
208
- */
209
- function scheduleWikiMaintenance(wikiDir, store, markDirty) {
210
- enqueueWikiMaintenance(wikiDir, store, markDirty);
211
- }
212
- // -- Factory: wiki_write execute ---------------------------------------------
213
- function createWriteExecute(wikiDir, store, getScope, markDirty) {
214
- return async (params) => {
215
- const pagesWritten = [];
216
- const pagesSkipped = [];
217
- const now = new Date().toISOString();
218
- const scope = getScope();
219
- // Gather all existing slugs for auto-linking (slug-only readdir scan;
220
- // reading/parsing every page here made wiki_write scale poorly)
221
- const allSlugs = await listPageSlugs(wikiDir);
222
- for (const pageSpec of params.pages) {
223
- // Sanitize slug
224
- const slug = pageSpec.slug
225
- .toLowerCase()
226
- .replace(/[^a-z0-9-]/g, "-")
227
- .replace(/-+/g, "-")
228
- .replace(/^-|-$/g, "");
229
- if (!slug) {
230
- continue; // skip invalid slugs
231
- }
232
- const existing = await readPage(wikiDir, pageSpec.category, slug);
233
- const pagePath = `${pageSpec.category}/${slug}`;
234
- // Track this slug for auto-linking other pages in this batch
235
- allSlugs.add(slug);
236
- if (pageSpec.mode === "create" && existing) {
237
- pagesSkipped.push(`${pagePath} (already exists; use mode=replace, mode=append, or wiki_edit)`);
238
- continue;
239
- }
240
- if (pageSpec.mode === "edit" && !existing) {
241
- pagesSkipped.push(`${pagePath} (missing; wiki_write mode=edit requires an existing page)`);
242
- continue;
243
- }
244
- if (pageSpec.mode === "edit" && existing) {
245
- // Legacy edit mode: keep for compatibility, but make it atomic like wiki_edit.
246
- const edits = pageSpec.edits ?? [];
247
- if (edits.length === 0) {
248
- continue; // no edits to apply
249
- }
250
- const errors = [];
251
- for (const edit of edits) {
252
- const first = existing.body.indexOf(edit.oldText);
253
- const last = existing.body.lastIndexOf(edit.oldText);
254
- if (first === -1) {
255
- errors.push(`oldText not found: "${edit.oldText.slice(0, 60)}..."`);
256
- }
257
- else if (first !== last) {
258
- errors.push(`oldText is not unique: "${edit.oldText.slice(0, 60)}..."`);
259
- }
260
- }
261
- if (errors.length > 0) {
262
- pagesSkipped.push(`${pagePath} (${errors.join("; ")})`);
263
- continue;
264
- }
265
- let editedBody = existing.body;
266
- for (const edit of edits) {
267
- editedBody = editedBody.replace(edit.oldText, edit.newText);
268
- }
269
- const linkedBody = redactSecrets(autoLinkSlugs(editedBody, allSlugs, slug)).text;
270
- const rawScope = pageSpec.scope.length > 0 ? pageSpec.scope : existing.frontmatter.scope;
271
- const newScope = normalizeScopes(rawScope);
272
- const newTags = pageSpec.tags.length > 0 ? pageSpec.tags : existing.frontmatter.tags;
273
- const updatedPage = {
274
- category: pageSpec.category,
275
- slug,
276
- frontmatter: {
277
- ...existing.frontmatter,
278
- title: pageSpec.title || existing.frontmatter.title,
279
- scope: newScope,
280
- tags: normalizeTags(newTags, newScope),
281
- updated: now,
282
- links: syncFrontmatterLinks(linkedBody),
283
- },
284
- body: linkedBody,
285
- };
286
- await writePage(wikiDir, updatedPage);
287
- pagesWritten.push(pagePath + (errors.length > 0 ? ` (${errors.length} edit(s) failed)` : ""));
288
- }
289
- else if (pageSpec.mode === "append" && existing) {
290
- // Append to existing page body
291
- const combinedBody = existing.body.trimEnd() + "\n\n" + pageSpec.body;
292
- const linkedBody = redactSecrets(autoLinkSlugs(combinedBody, allSlugs, slug)).text;
293
- const mergedTags = [...new Set([...existing.frontmatter.tags, ...pageSpec.tags])];
294
- const mergedScope = normalizeScopes([...new Set([...existing.frontmatter.scope, ...pageSpec.scope])]);
295
- const updatedPage = {
296
- ...existing,
297
- body: linkedBody,
298
- frontmatter: {
299
- ...existing.frontmatter,
300
- updated: now,
301
- tags: normalizeTags(mergedTags, mergedScope),
302
- scope: mergedScope,
303
- links: syncFrontmatterLinks(linkedBody),
304
- },
305
- };
306
- await writePage(wikiDir, updatedPage);
307
- pagesWritten.push(pagePath);
308
- }
309
- else if (pageSpec.mode === "replace" && existing) {
310
- // Replace existing page body, update frontmatter
311
- const linkedBody = redactSecrets(autoLinkSlugs(pageSpec.body, allSlugs, slug)).text;
312
- const rawScope = pageSpec.scope.length > 0 ? pageSpec.scope : existing.frontmatter.scope;
313
- const newScope = normalizeScopes(rawScope);
314
- const newTags = pageSpec.tags.length > 0 ? pageSpec.tags : existing.frontmatter.tags;
315
- const updatedPage = {
316
- category: pageSpec.category,
317
- slug,
318
- frontmatter: {
319
- ...existing.frontmatter,
320
- title: pageSpec.title,
321
- para: pageSpec.category,
322
- scope: newScope,
323
- tags: normalizeTags(newTags, newScope),
324
- updated: now,
325
- links: syncFrontmatterLinks(linkedBody),
326
- },
327
- body: linkedBody,
328
- };
329
- await writePage(wikiDir, updatedPage);
330
- pagesWritten.push(pagePath);
331
- }
332
- else {
333
- // Create new page (or mode=create/replace on non-existing)
334
- const linkedBody = redactSecrets(autoLinkSlugs(pageSpec.body, allSlugs, slug)).text;
335
- const rawScope = pageSpec.scope.length > 0 ? pageSpec.scope : [scope.name];
336
- const newScope = normalizeScopes(rawScope);
337
- const frontmatter = validateFrontmatter({
338
- title: pageSpec.title,
339
- para: pageSpec.category,
340
- scope: newScope,
341
- tags: normalizeTags(pageSpec.tags, newScope),
342
- sources: [],
343
- created: now,
344
- updated: now,
345
- links: syncFrontmatterLinks(linkedBody),
346
- });
347
- const newPage = {
348
- category: pageSpec.category,
349
- slug,
350
- frontmatter,
351
- body: linkedBody,
352
- };
353
- await writePage(wikiDir, newPage);
354
- pagesWritten.push(pagePath);
355
- }
356
- }
357
- // Always auto-rebuild index from disk.
358
- // The LLM's indexContent is unreliable — it only knows about pages
359
- // in its context, so it produces partial indexes that overwrite
360
- // the full one. indexContent parameter is deprecated and ignored.
361
- let indexUpdated = false;
362
- if (pagesWritten.length > 0) {
363
- await rebuildIndex(wikiDir);
364
- indexUpdated = true;
365
- }
366
- // Append to log.md if summary provided
367
- let logAppended = false;
368
- if (params.logSummary && pagesWritten.length > 0) {
369
- await appendLog(wikiDir, {
370
- date: now.split("T")[0],
371
- operation: "ingest",
372
- summary: params.logSummary,
373
- pages: pagesWritten,
374
- });
375
- logAppended = true;
376
- }
377
- // Defer QMD BM25 reindex to the debounced background maintenance queue
378
- // (same as wiki_edit). Immediate post-write queries are safe: queryWiki
379
- // retries with a fresh store.update() before returning zero results.
380
- if (pagesWritten.length > 0) {
381
- scheduleWikiMaintenance(wikiDir, store, markDirty);
382
- }
383
- // Mark context cache dirty so before_agent_start rebuilds
384
- markDirty();
385
- // Git auto-commit
386
- if (pagesWritten.length > 0) {
387
- const commitMsg = params.logSummary
388
- ? params.logSummary
389
- : `wiki: ${pagesWritten.join(", ")}`;
390
- await gitCommit(wikiDir, commitMsg);
391
- }
392
- const summary = [
393
- pagesWritten.length > 0
394
- ? `Wrote ${pagesWritten.length} page(s): ${pagesWritten.join(", ")}`
395
- : "No pages written.",
396
- pagesSkipped.length > 0 ? `Skipped ${pagesSkipped.length} page(s): ${pagesSkipped.join(", ")}` : "",
397
- ].filter(Boolean).join("\n");
398
- return {
399
- content: [
400
- {
401
- type: "text",
402
- text: [
403
- summary,
404
- indexUpdated ? "Updated index.md." : "",
405
- logAppended ? `Logged: ${params.logSummary}` : "",
406
- ]
407
- .filter(Boolean)
408
- .join("\n"),
409
- },
410
- ],
411
- details: {
412
- pagesWritten,
413
- indexUpdated,
414
- logAppended,
415
- pagesSkipped,
416
- },
417
- };
418
- };
419
- }
420
- // -- Factory: wiki_read execute ----------------------------------------------
421
- function createEditExecute(wikiDir, store, markDirty) {
422
- return async (params) => {
423
- const parsed = parsePagePath(params.path);
424
- if (!parsed) {
425
- return {
426
- content: [{ type: "text", text: `Invalid page path: ${params.path}` }],
427
- details: { pagesWritten: [], pagesSkipped: [params.path], indexUpdated: false, logAppended: false },
428
- };
429
- }
430
- const existing = await readPage(wikiDir, parsed.category, parsed.slug);
431
- const pagePath = `${parsed.category}/${parsed.slug}`;
432
- if (!existing) {
433
- return {
434
- content: [{ type: "text", text: `Page not found: ${pagePath}` }],
435
- details: { pagesWritten: [], pagesSkipped: [pagePath], indexUpdated: false, logAppended: false },
436
- };
437
- }
438
- if (params.edits.length === 0) {
439
- return {
440
- content: [{ type: "text", text: "No edits supplied." }],
441
- details: { pagesWritten: [], pagesSkipped: [pagePath], indexUpdated: false, logAppended: false },
442
- };
443
- }
444
- // Atomic validation: all oldText strings must exist exactly once before writing.
445
- const errors = [];
446
- for (const edit of params.edits) {
447
- const first = existing.body.indexOf(edit.oldText);
448
- const last = existing.body.lastIndexOf(edit.oldText);
449
- if (first === -1) {
450
- errors.push(`oldText not found: "${edit.oldText.slice(0, 60)}..."`);
451
- }
452
- else if (first !== last) {
453
- errors.push(`oldText is not unique: "${edit.oldText.slice(0, 60)}..."`);
454
- }
455
- }
456
- if (errors.length > 0) {
457
- return {
458
- content: [{ type: "text", text: `No changes written.\n${errors.join("\n")}` }],
459
- details: { pagesWritten: [], pagesSkipped: [pagePath], indexUpdated: false, logAppended: false },
460
- };
461
- }
462
- let editedBody = existing.body;
463
- for (const edit of params.edits) {
464
- editedBody = editedBody.replace(edit.oldText, edit.newText);
465
- }
466
- // Surgical edit path: do not scan/autolink/rebuild/reindex the whole wiki.
467
- // wiki_edit is often called mid-task and must stay fast; broader maintenance
468
- // can run via wiki_lint/rebuildIndex or on startup.
469
- const linkedBody = redactSecrets(editedBody).text;
470
- const now = new Date().toISOString();
471
- const rawScope = params.scope ?? existing.frontmatter.scope;
472
- const newScope = normalizeScopes(rawScope);
473
- const rawTags = params.tags ?? existing.frontmatter.tags;
474
- const updatedPage = {
475
- category: parsed.category,
476
- slug: parsed.slug,
477
- frontmatter: {
478
- ...existing.frontmatter,
479
- title: params.title ?? existing.frontmatter.title,
480
- scope: newScope,
481
- tags: normalizeTags(rawTags, newScope),
482
- updated: now,
483
- links: syncFrontmatterLinks(linkedBody),
484
- },
485
- body: linkedBody,
486
- };
487
- await writePage(wikiDir, updatedPage);
488
- if (params.logSummary) {
489
- await appendLog(wikiDir, {
490
- date: now.split("T")[0],
491
- operation: "edit",
492
- summary: params.logSummary,
493
- pages: [pagePath],
494
- });
495
- }
496
- markDirty();
497
- await gitCommit(wikiDir, params.logSummary ?? `wiki_edit: ${pagePath}`);
498
- scheduleWikiMaintenance(wikiDir, store, markDirty);
499
- return {
500
- content: [{ type: "text", text: `Edited ${pagePath}. Scheduled background index/search refresh.` }],
501
- details: {
502
- pagesWritten: [pagePath],
503
- pagesSkipped: [],
504
- indexUpdated: false,
505
- logAppended: !!params.logSummary,
506
- },
507
- };
508
- };
509
- }
510
- function createReadExecute(wikiDir) {
511
- return async (params) => {
512
- const now = Date.now();
513
- const normalizedPath = params.path.trim().toLowerCase().replace(/^\/+/, "");
514
- if (normalizedPath === "index" || normalizedPath === "index.md" || normalizedPath === "wiki-index") {
515
- const index = await readIndex(wikiDir);
516
- return {
517
- content: [{ type: "text", text: index }],
518
- details: { found: true, path: "index.md" },
519
- };
520
- }
521
- // Try parsing as category/slug path
522
- const parsed = parsePagePath(params.path);
523
- if (parsed) {
524
- const page = await readPage(wikiDir, parsed.category, parsed.slug);
525
- if (page) {
526
- const fm = page.frontmatter;
527
- const freshness = formatFreshness(fm.updated, now);
528
- const header = [
529
- `# ${fm.title}`,
530
- `PARA: ${fm.para} | Scope: ${fm.scope.join(", ")} | Tags: ${fm.tags.join(", ")}`,
531
- `Created: ${fm.created} | Updated: ${fm.updated} | ${freshness}`,
532
- fm.sources.length > 0 ? `Sources: ${fm.sources.join(", ")}` : "",
533
- fm.links.length > 0 ? `Links: ${fm.links.map((l) => `[[${l}]]`).join(", ")}` : "",
534
- ]
535
- .filter(Boolean)
536
- .join("\n");
537
- return {
538
- content: [{ type: "text", text: `${header}\n\n${page.body}` }],
539
- details: { found: true, path: `${parsed.category}/${parsed.slug}` },
540
- };
541
- }
542
- }
543
- // Try finding by title (case-insensitive search across all pages)
544
- const allPages = await listPages(wikiDir);
545
- const titleLower = params.path.toLowerCase();
546
- for (const ref of allPages) {
547
- if (ref.title.toLowerCase() === titleLower || ref.slug === titleLower) {
548
- const page = await readPage(wikiDir, ref.category, ref.slug);
549
- if (page) {
550
- const fm = page.frontmatter;
551
- const freshness = formatFreshness(fm.updated, now);
552
- const header = [
553
- `# ${fm.title}`,
554
- `PARA: ${fm.para} | Scope: ${fm.scope.join(", ")} | Tags: ${fm.tags.join(", ")}`,
555
- `Created: ${fm.created} | Updated: ${fm.updated} | ${freshness}`,
556
- fm.sources.length > 0 ? `Sources: ${fm.sources.join(", ")}` : "",
557
- fm.links.length > 0 ? `Links: ${fm.links.map((l) => `[[${l}]]`).join(", ")}` : "",
558
- ]
559
- .filter(Boolean)
560
- .join("\n");
561
- return {
562
- content: [{ type: "text", text: `${header}\n\n${page.body}` }],
563
- details: { found: true, path: `${ref.category}/${ref.slug}` },
564
- };
565
- }
566
- }
567
- }
568
- return {
569
- content: [{ type: "text", text: `Page not found: "${params.path}"` }],
570
- details: { found: false },
571
- };
572
- };
573
- }
574
- // -- Factory: wiki_move execute ----------------------------------------------
575
- function createMoveExecute(wikiDir, store, markDirty) {
576
- return async (params) => {
577
- const parsed = parsePagePath(params.path);
578
- if (!parsed) {
579
- throw new Error(`Invalid page path: "${params.path}". Expected format: category/slug`);
580
- }
581
- const page = await readPage(wikiDir, parsed.category, parsed.slug);
582
- if (!page) {
583
- throw new Error(`Page not found: "${params.path}"`);
584
- }
585
- if (parsed.category === params.to) {
586
- return {
587
- content: [
588
- {
589
- type: "text",
590
- text: `Page "${params.path}" is already in ${params.to}. No move needed.`,
591
- },
592
- ],
593
- details: { from: params.path, to: `${params.to}/${parsed.slug}` },
594
- };
595
- }
596
- const ref = {
597
- category: parsed.category,
598
- slug: parsed.slug,
599
- title: page.frontmatter.title,
600
- path: `${parsed.category}/${parsed.slug}.md`,
601
- };
602
- await movePage(wikiDir, ref, params.to);
603
- // Rebuild index.md inline (fast, single-pass); defer QMD reindex to the
604
- // debounced background maintenance queue like wiki_write/wiki_edit.
605
- await rebuildIndex(wikiDir);
606
- scheduleWikiMaintenance(wikiDir, store, markDirty);
607
- markDirty();
608
- // Git commit
609
- await gitCommit(wikiDir, `move: ${page.frontmatter.title} → ${params.to}`);
610
- // Log the move
611
- const now = new Date().toISOString();
612
- await appendLog(wikiDir, {
613
- date: now.split("T")[0],
614
- operation: "move",
615
- summary: `Moved ${page.frontmatter.title} from ${parsed.category} to ${params.to}`,
616
- pages: [`${params.to}/${parsed.slug}`],
617
- });
618
- return {
619
- content: [
620
- {
621
- type: "text",
622
- text: `Moved "${page.frontmatter.title}" from ${parsed.category}/ to ${params.to}/`,
623
- },
624
- ],
625
- details: {
626
- from: `${parsed.category}/${parsed.slug}`,
627
- to: `${params.to}/${parsed.slug}`,
628
- },
629
- };
630
- };
631
- }
632
- // -- Factory: wiki_lint execute ----------------------------------------------
633
- function createLintExecute(wikiDir, store, markDirty, getLintConfig) {
634
- return async (params) => {
635
- const configured = getLintConfig?.();
636
- const report = await lintWiki(wikiDir, {
637
- autoFix: params.autoFix ?? configured?.autoFix ?? true,
638
- staleDays: configured?.staleDays ?? 90,
639
- });
640
- if (report.fixed.length > 0) {
641
- scheduleWikiMaintenance(wikiDir, store, markDirty);
642
- }
643
- const { issues, fixed, stats } = report;
644
- // Format report for the LLM
645
- const parts = [];
646
- parts.push("## Wiki Lint Report\n");
647
- parts.push(`Total pages: ${stats.totalPages}`);
648
- parts.push(`By category: ${PARA_CATEGORIES.map((c) => `${c}: ${stats.byCategory[c]}`).join(", ")}`);
649
- parts.push(`Total links: ${stats.totalLinks} (broken: ${stats.brokenLinks})`);
650
- parts.push(`Orphan pages: ${stats.orphanPages}`);
651
- if (stats.oldestPage)
652
- parts.push(`Oldest page: ${stats.oldestPage}`);
653
- if (stats.newestPage)
654
- parts.push(`Newest page: ${stats.newestPage}`);
655
- if (stats.lastIngest)
656
- parts.push(`Last ingest: ${stats.lastIngest}`);
657
- if (fixed.length > 0) {
658
- parts.push(`\n## Auto-fixed (${fixed.length})\n`);
659
- for (const f of fixed) {
660
- parts.push(`- [${f.severity}] ${f.page ?? ""}: ${f.message}`);
661
- }
662
- }
663
- if (issues.length > 0) {
664
- parts.push(`\n## Remaining Issues (${issues.length})\n`);
665
- for (const issue of issues) {
666
- parts.push(`- [${issue.severity}] ${issue.category}${issue.page ? ` (${issue.page})` : ""}: ${issue.message}`);
667
- }
668
- parts.push("");
669
- parts.push(LINT_PROMPT);
670
- }
671
- else {
672
- parts.push("\nNo remaining issues. Wiki is healthy.");
673
- }
674
- return {
675
- content: [{ type: "text", text: parts.join("\n") }],
676
- details: {
677
- issueCount: issues.length,
678
- fixedCount: fixed.length,
679
- stats,
680
- },
681
- };
682
- };
683
- }
684
- // -- Factory: wiki_summarize execute -----------------------------------------
685
- function createSummarizeExecute(wikiDir, getScope) {
686
- return async (params) => {
687
- const scope = getScope();
688
- const pages = [];
689
- if (params.target === "all") {
690
- // Summarize entire wiki
691
- const refs = await listPages(wikiDir);
692
- for (const ref of refs) {
693
- const page = await readPage(wikiDir, ref.category, ref.slug);
694
- if (page)
695
- pages.push(page);
696
- }
697
- }
698
- else if (PARA_CATEGORIES.includes(params.target)) {
699
- // Summarize a category
700
- const refs = await listPages(wikiDir, params.target);
701
- for (const ref of refs) {
702
- const page = await readPage(wikiDir, ref.category, ref.slug);
703
- if (page)
704
- pages.push(page);
705
- }
706
- }
707
- else {
708
- // Summarize a single page
709
- const parsed = parsePagePath(params.target);
710
- if (parsed) {
711
- const page = await readPage(wikiDir, parsed.category, parsed.slug);
712
- if (page)
713
- pages.push(page);
714
- }
715
- else {
716
- // Try finding by slug alone across categories
717
- const allPages = await listPages(wikiDir);
718
- const targetLower = params.target.toLowerCase();
719
- for (const ref of allPages) {
720
- if (ref.slug === targetLower || ref.title.toLowerCase() === targetLower) {
721
- const page = await readPage(wikiDir, ref.category, ref.slug);
722
- if (page)
723
- pages.push(page);
724
- break;
725
- }
726
- }
727
- }
728
- }
729
- if (pages.length === 0) {
730
- return {
731
- content: [
732
- {
733
- type: "text",
734
- text: `No pages found for target "${params.target}". Nothing to summarize.`,
735
- },
736
- ],
737
- details: { target: params.target, pageCount: 0 },
738
- };
739
- }
740
- // Generate a bounded overview prompt for the LLM. For large targets like
741
- // `all`, brief mode must not dump every full page body into model context.
742
- const depth = params.depth ?? "brief";
743
- const overviewPrompt = generateOverviewPrompt(pages, scope, { depth });
744
- const depthNote = depth === "detailed"
745
- ? "\nProvide a DETAILED summary with full Key Facts, Insights, and Connections for each theme."
746
- : "\nProvide a BRIEF overview — one paragraph per theme, focus on high-level patterns.";
747
- return {
748
- content: [
749
- {
750
- type: "text",
751
- text: overviewPrompt + depthNote +
752
- "\n\nYou may optionally create a summary page via wiki_write if the overview is worth preserving.",
753
- },
754
- ],
755
- details: {
756
- target: params.target,
757
- pageCount: pages.length,
758
- },
759
- };
760
- };
761
- }
762
- // -- Factory: wiki_migrate execute -------------------------------------------
763
- function createMigrateExecute(wikiDir) {
764
- return async () => {
765
- const allRefs = await listPages(wikiDir);
766
- let migratedCount = 0;
767
- for (const ref of allRefs) {
768
- const page = await readPage(wikiDir, ref.category, ref.slug);
769
- if (!page)
770
- continue;
771
- const version = page.frontmatter.schemaVersion ?? 1;
772
- if (version >= CURRENT_SCHEMA_VERSION)
773
- continue;
774
- const rawObj = page.frontmatter;
775
- const result = migrateToLatest(rawObj, page.body);
776
- const migratedFm = result.fm;
777
- migratedFm.updated = new Date().toISOString();
778
- await writePage(wikiDir, {
779
- category: ref.category,
780
- slug: ref.slug,
781
- frontmatter: migratedFm,
782
- body: result.body,
783
- });
784
- migratedCount++;
785
- }
786
- const summary = migratedCount > 0
787
- ? `Migrated ${migratedCount} page(s) to schema version ${CURRENT_SCHEMA_VERSION}.`
788
- : `All ${allRefs.length} page(s) already at schema version ${CURRENT_SCHEMA_VERSION}.`;
789
- return {
790
- content: [{ type: "text", text: summary }],
791
- details: { migratedCount, totalPages: allRefs.length },
792
- };
793
- };
794
- }
795
- // -- Wikilink extraction helper (re-exported from link-utils) ----------------
796
- // extractWikilinks, autoLinkSlugs, syncFrontmatterLinks imported above
3
+ import { detectSourceType } from "./ingest.js";
4
+ import { CURRENT_SCHEMA_VERSION } from "./frontmatter.js";
5
+ import { WIKI_TOOL_DESCRIPTIONS, WIKI_TOOL_SNIPPETS, getWikiToolGuidelines } from "./wiki-tool-guidance.js";
6
+ import { createIngestExecute } from "./tools/ingest.js";
7
+ import { createQueryExecute } from "./tools/query.js";
8
+ import { createWriteExecute } from "./tools/write.js";
9
+ import { createEditExecute, createReadExecute } from "./tools/edit-read.js";
10
+ import { createMoveExecute } from "./tools/move.js";
11
+ import { createLintExecute } from "./tools/lint.js";
12
+ import { createMigrateExecute } from "./tools/migrate.js";
13
+ import { createSummarizeExecute } from "./tools/summarize.js";
14
+ import { WikiIngestParams, WikiQueryParams, WikiEditParams, WikiWriteParams, WikiReadParams, WikiMoveParams, WikiLintParams, WikiMigrateParams, WikiSummarizeParams, } from "./tools/schemas.js";
797
15
  // -- Public API: register tools for pi session -------------------------------
798
16
  /**
799
17
  * Register all wiki tools with the pi extension API.