@joe-broadhead/opencode-mem 0.11.2 → 0.11.3
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/CHANGELOG.md +51 -0
- package/README.md +1 -0
- package/package.json +504 -2
- package/src/config.js +9 -1
- package/src/tools/commitments.js +50 -32
- package/src/tools/held-write.js +15 -7
- package/src/tools/index.js +1 -0
- package/src/tools/ingest.js +11 -0
- package/src/tools/memory.js +26 -18
- package/src/tools/page.js +118 -87
- package/src/tools/recall.js +12 -2
- package/src/tools/shared.js +40 -2
- package/src/tools/spaces.js +8 -1
- package/src/tools/wiki.js +198 -113
- package/src/views/contracts.js +152 -2
- package/src/views/receipt.js +26 -1
- package/src/views/records-reads.js +165 -3
- package/src/views/rpc.js +35 -17
- package/src/views/write-state.js +89 -0
- package/src/wiki-ingest.js +215 -0
- package/src/wiki-state.js +228 -0
package/src/tools/wiki.js
CHANGED
|
@@ -2,7 +2,10 @@ import { quarantineNote, quarantineText } from "../curation.js"
|
|
|
2
2
|
import { RECORDS_FILE, RECORDS_WIKI, isRecordsText } from "../records.js"
|
|
3
3
|
import { codedMessage } from "../errors.js"
|
|
4
4
|
import { errorMessage, neutralizeFenceClosers, oneLine, redactSecrets } from "../util.js"
|
|
5
|
+
import { createWikiIngest, WIKI_INGEST_DELAY_MS } from "../wiki-ingest.js"
|
|
6
|
+
import { wikiCache, wikiErrorOf, wikiStateOf } from "../wiki-state.js"
|
|
5
7
|
import { discoverKnowledge, isRecordsWikiName, searchKnowledge } from "./knowledge.js"
|
|
8
|
+
import { failedAnswer, refusedAnswer, wikiPlace, withWriteState } from "../views/write-state.js"
|
|
6
9
|
|
|
7
10
|
const MAX_FILE_BYTES = 512 * 1024
|
|
8
11
|
const DEFAULT_FILE = "README.md"
|
|
@@ -81,6 +84,33 @@ export function registerWiki(editor, toolkit) {
|
|
|
81
84
|
runtime,
|
|
82
85
|
} = toolkit
|
|
83
86
|
|
|
87
|
+
// 0.11.3 (jelly's #89 ask A4): a write's answer opens with its state line,
|
|
88
|
+
// and a refusal opens with `state: refused` and names nothing.
|
|
89
|
+
const refuse = (content) => refusedAnswer(text(content))
|
|
90
|
+
const failedCall = (message) => failedAnswer(fail(message))
|
|
91
|
+
|
|
92
|
+
/** The last ready page set of each wiki, and why an ingest failed (jelly #89 A1, A2). */
|
|
93
|
+
const cache = wikiCache({ stateDir: config.stateDir, log })
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The write path (jelly #89 A3): every write schedules one debounced ingest,
|
|
97
|
+
* and a write the hub refuses during an ingest is queued, never refused.
|
|
98
|
+
* `toolkit.wikiIngest` lets the engine stop its timers with the tool set's.
|
|
99
|
+
*/
|
|
100
|
+
const ingest =
|
|
101
|
+
toolkit.wikiIngest ??
|
|
102
|
+
createWikiIngest({
|
|
103
|
+
backend,
|
|
104
|
+
log,
|
|
105
|
+
delay: config.wikiIngestMs ?? WIKI_INGEST_DELAY_MS,
|
|
106
|
+
rememberError: (wikiID, message) => cache.rememberError(wikiID, message),
|
|
107
|
+
clearError: (wikiID) => cache.clearError(wikiID),
|
|
108
|
+
rememberPending: (wikiID, scope) => cache.rememberPending(wikiID, scope),
|
|
109
|
+
clearPending: (wikiID) => cache.clearPending(wikiID),
|
|
110
|
+
pending: () => cache.pending(),
|
|
111
|
+
})
|
|
112
|
+
toolkit.wikiIngest = ingest
|
|
113
|
+
|
|
84
114
|
/** The visibility the call asked for: private, team, or auto (the default). */
|
|
85
115
|
function requestedVisibility(input) {
|
|
86
116
|
const value = String(input?.visibility ?? "")
|
|
@@ -187,15 +217,15 @@ export function registerWiki(editor, toolkit) {
|
|
|
187
217
|
}
|
|
188
218
|
|
|
189
219
|
/**
|
|
190
|
-
* The hub refuses
|
|
191
|
-
*
|
|
192
|
-
*
|
|
220
|
+
* The hub refuses a delete while a wiki ingests (`409 wiki is processing`).
|
|
221
|
+
* Writes no longer wait on that: a write during an ingest is queued
|
|
222
|
+
* (src/wiki-ingest.js), and the answer says so.
|
|
193
223
|
*/
|
|
194
224
|
function busyMessage(wikiID, asked, done = "written") {
|
|
195
225
|
return codedMessage(
|
|
196
226
|
"MEMORY_WIKI_BUSY",
|
|
197
|
-
`wiki ${oneLine(wikiID, 80)} is ingesting an earlier write (status: processing) and refuses
|
|
198
|
-
"retry once mem_wiki status shows it ready
|
|
227
|
+
`wiki ${oneLine(wikiID, 80)} is ingesting an earlier write (status: processing) and refuses deletes until it finishes, usually within a few minutes; nothing was ${asked ? "" : "asked or "}${done}`,
|
|
228
|
+
"retry once mem_wiki status shows it ready",
|
|
199
229
|
)
|
|
200
230
|
}
|
|
201
231
|
|
|
@@ -203,10 +233,10 @@ export function registerWiki(editor, toolkit) {
|
|
|
203
233
|
function hubFailure(error, wikiID, filename) {
|
|
204
234
|
const message = errorMessage(error)
|
|
205
235
|
const what = String(error?.what ?? message)
|
|
206
|
-
if (Number(error?.status) === 409 || /wiki is processing/i.test(what)) return
|
|
236
|
+
if (Number(error?.status) === 409 || /wiki is processing/i.test(what)) return refuse(busyMessage(wikiID, true))
|
|
207
237
|
// The hub's ENOENT names a path on its own disk: never repeat it.
|
|
208
238
|
if (/\bENOENT\b/.test(what)) {
|
|
209
|
-
return
|
|
239
|
+
return refuse(
|
|
210
240
|
codedMessage(
|
|
211
241
|
"MEMORY_WIKI_NO_FOLDERS",
|
|
212
242
|
`the wiki cannot create a folder for "${oneLine(filename, 120)}"; nothing was written`,
|
|
@@ -240,24 +270,34 @@ export function registerWiki(editor, toolkit) {
|
|
|
240
270
|
})
|
|
241
271
|
}
|
|
242
272
|
|
|
243
|
-
async function writeRaw(scope, wikiID, filename, content
|
|
244
|
-
|
|
245
|
-
if (ingest === false) return { ingested: false }
|
|
246
|
-
try {
|
|
247
|
-
await backend.ingestWiki({ scope, wikiID })
|
|
248
|
-
return { ingested: true }
|
|
249
|
-
} catch (error) {
|
|
250
|
-
return { ingested: false, ingestError: errorMessage(error) }
|
|
251
|
-
}
|
|
273
|
+
async function writeRaw(scope, wikiID, filename, content) {
|
|
274
|
+
return ingest.write({ scope, wikiID, filename, content })
|
|
252
275
|
}
|
|
253
276
|
|
|
254
|
-
/**
|
|
277
|
+
/**
|
|
278
|
+
/**
|
|
279
|
+
* The state a written source file's answer opens with (0.11.3): the file is
|
|
280
|
+
* saved at once, so `processing` while the ingest it started makes search and
|
|
281
|
+
* the pages derived from it current, `failed` when that ingest did not start,
|
|
282
|
+
* and `stored` when no ingest was asked for.
|
|
283
|
+
*/
|
|
284
|
+
function writtenState(written) {
|
|
285
|
+
if (written?.queued) return "queued"
|
|
286
|
+
// A3 starts an ingest after every write (ingest: false is ignored), so a
|
|
287
|
+
// write that landed is `processing` until that ingest finishes.
|
|
288
|
+
if (written?.ingestError) return "failed"
|
|
289
|
+
return "processing"
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* What happens after a write. `ingest: false` is accepted and ignored: the
|
|
294
|
+
* ingest starts on its own, so nothing here asks the agent to batch.
|
|
295
|
+
*/
|
|
255
296
|
function ingestNote(written) {
|
|
256
|
-
if (written.
|
|
257
|
-
return "
|
|
297
|
+
if (written.queued) {
|
|
298
|
+
return "the wiki is ingesting an earlier write, so this write is queued and is written when that ingest finishes, with one ingest after it; the queue lives only in this Memory process, so a restart before then loses this write, and the person should check the wiki afterwards"
|
|
258
299
|
}
|
|
259
|
-
|
|
260
|
-
return "ingestion not started (ingest: false); pass ingest: true on the last write so search finds the changes"
|
|
300
|
+
return "ingestion starts on its own a moment after the last write, so a burst of writes makes one ingest; search finds the changes once it finishes"
|
|
261
301
|
}
|
|
262
302
|
|
|
263
303
|
/**
|
|
@@ -346,10 +386,18 @@ export function registerWiki(editor, toolkit) {
|
|
|
346
386
|
}
|
|
347
387
|
}
|
|
348
388
|
|
|
389
|
+
/** A page/ls answer as the cache keeps it: `{ ref, title }`, bounded. */
|
|
390
|
+
function listedPages(data) {
|
|
391
|
+
return (data?.items ?? [])
|
|
392
|
+
.map((item) => ({ ref: String(item?.path ?? item?.id ?? ""), title: oneLine(item?.title || item?.path || item?.id || "", 200) }))
|
|
393
|
+
.filter((page) => page.ref)
|
|
394
|
+
.slice(0, 100)
|
|
395
|
+
}
|
|
396
|
+
|
|
349
397
|
/**
|
|
350
398
|
* What exists, so the next read can name it. The hub lists no pages while a
|
|
351
|
-
* wiki ingests (page/ls answers [] unless it is ready)
|
|
352
|
-
*
|
|
399
|
+
* wiki ingests (page/ls answers [] unless it is ready); the last ready set
|
|
400
|
+
* this engine saw is listed instead, marked stale (jelly #89 A2).
|
|
353
401
|
*/
|
|
354
402
|
async function notFound(scoped, wikiID, name) {
|
|
355
403
|
const failed = Symbol("failed")
|
|
@@ -359,7 +407,9 @@ export function registerWiki(editor, toolkit) {
|
|
|
359
407
|
orNull(backend.getWiki({ scope: scoped, wikiID })).catch(() => null),
|
|
360
408
|
])
|
|
361
409
|
const status = String(wiki?.status ?? "")
|
|
410
|
+
const state = status ? wikiStateOf(wiki) : ""
|
|
362
411
|
const pageNames = pages === failed ? [] : (pages?.items ?? []).map((item) => item?.path ?? item?.id).filter(Boolean).slice(0, 15)
|
|
412
|
+
if (pages !== failed && state === "ready") cache.rememberPages(wikiID, listedPages(pages))
|
|
363
413
|
const sourceNames = sources === failed
|
|
364
414
|
? []
|
|
365
415
|
: (Array.isArray(sources) ? sources : sources?.items ?? [])
|
|
@@ -368,10 +418,13 @@ export function registerWiki(editor, toolkit) {
|
|
|
368
418
|
.sort()
|
|
369
419
|
.slice(0, 15)
|
|
370
420
|
const list = (names) => names.map((entry) => oneLine(entry, 120)).join(", ")
|
|
421
|
+
const staleNames = state && state !== "ready" ? cache.pages(wikiID).map((page) => page.ref).slice(0, 15) : []
|
|
371
422
|
let pageLine
|
|
372
423
|
if (pageNames.length) pageLine = list(pageNames)
|
|
373
424
|
else if (pages === failed) pageLine = "could not be listed now"
|
|
374
|
-
else if (
|
|
425
|
+
else if (staleNames.length) {
|
|
426
|
+
pageLine = `${list(staleNames)}. The wiki is ${oneLine(state, 40)}, so these are the last ready set, stale until it finishes; source files read at once`
|
|
427
|
+
} else if (status && state !== "ready") pageLine = `none listed while the wiki is ${oneLine(status, 40)}; pages are listed again once ingestion finishes (mem_wiki status shows ready). Source files read at once`
|
|
375
428
|
else if (Number(wiki?.page_count) > 0) pageLine = `none listed, though the wiki reports ${Number(wiki.page_count)}; read by filename meanwhile`
|
|
376
429
|
else pageLine = "none"
|
|
377
430
|
const sourceLine = sourceNames.length ? list(sourceNames) : sources === failed ? "could not be listed now" : "none"
|
|
@@ -408,20 +461,20 @@ export function registerWiki(editor, toolkit) {
|
|
|
408
461
|
/** A refusal of a delete by the hub or the gateway, in words that say what to do. */
|
|
409
462
|
function deleteFailure(error, wikiID, target) {
|
|
410
463
|
const what = String(error?.what ?? errorMessage(error))
|
|
411
|
-
if (Number(error?.status) === 409 || /wiki is processing/i.test(what)) return
|
|
464
|
+
if (Number(error?.status) === 409 || /wiki is processing/i.test(what)) return refuse(busyMessage(wikiID, true, "deleted"))
|
|
412
465
|
if (/endpoint_not_allowed|not available through the gateway/i.test(what)) {
|
|
413
|
-
return
|
|
466
|
+
return refuse(
|
|
414
467
|
codedMessage("MEMORY_WIKI_DELETE_UNAVAILABLE", "this Memory server does not allow wiki deletes yet (its gateway predates them); nothing was deleted", "ask the Memory server's operator to upgrade the gateway"),
|
|
415
468
|
)
|
|
416
469
|
}
|
|
417
470
|
if (/forbidden path/i.test(what)) {
|
|
418
|
-
return
|
|
471
|
+
return refuse(codedMessage("MEMORY_WIKI_STRUCTURAL", `"${oneLine(target, 120)}" is one of the wiki's structural pages (index, schema, purpose); nothing was deleted`, "delete a source file or another page"))
|
|
419
472
|
}
|
|
420
|
-
if (/invalid path/i.test(what)) return
|
|
473
|
+
if (/invalid path/i.test(what)) return refuse(`not deleted: ${oneLine(target, 120)} was not found in wiki ${oneLine(wikiID, 80)}; nothing was deleted.`)
|
|
421
474
|
if (/resource_delete_forbidden|resource owner or a space owner or admin/i.test(what)) {
|
|
422
|
-
return
|
|
475
|
+
return refuse(`not deleted: only the wiki's owner or a space owner or admin can delete it. Nothing was deleted.`)
|
|
423
476
|
}
|
|
424
|
-
return
|
|
477
|
+
return failedCall(errorMessage(error))
|
|
425
478
|
}
|
|
426
479
|
|
|
427
480
|
/** Names of pages a delete removed or rewrote, bounded. */
|
|
@@ -443,43 +496,43 @@ export function registerWiki(editor, toolkit) {
|
|
|
443
496
|
const rawFilename = input?.filename === undefined || input?.filename === null ? "" : String(input.filename).trim()
|
|
444
497
|
const rawRef = String(input?.ref ?? input?.path ?? "").trim()
|
|
445
498
|
if (rawFilename && rawRef) {
|
|
446
|
-
return
|
|
499
|
+
return refuse("not deleted: delete takes one target: filename (a source file), ref (a generated page), or neither with wiki_id (the whole wiki). Nothing was asked or deleted.")
|
|
447
500
|
}
|
|
448
501
|
const kind = rawFilename ? "source" : rawRef ? "page" : "wiki"
|
|
449
502
|
const givenWiki = String(input?.wiki_id ?? "").trim()
|
|
450
503
|
if (kind === "wiki" && !givenWiki) {
|
|
451
|
-
return
|
|
504
|
+
return refuse(
|
|
452
505
|
"not deleted: name what to delete. filename deletes a source file, ref a generated page, and wiki_id alone the whole wiki (mem_wiki list shows the ids). Nothing was asked or deleted.",
|
|
453
506
|
)
|
|
454
507
|
}
|
|
455
508
|
let target = ""
|
|
456
509
|
if (kind === "source") {
|
|
457
510
|
target = filenameOf({ filename: rawFilename.replace(/^\/*wiki\//, "") })
|
|
458
|
-
if (!target) return
|
|
459
|
-
if (target === RECORDS_FILE) return
|
|
511
|
+
if (!target) return refuse("invalid filename")
|
|
512
|
+
if (target === RECORDS_FILE) return refuse(`not deleted: ${RECORDS_REFUSAL}`)
|
|
460
513
|
} else if (kind === "page") {
|
|
461
|
-
if (rawRef.split("/").some((part) => part === "..")) return
|
|
514
|
+
if (rawRef.split("/").some((part) => part === "..")) return refuse("invalid ref")
|
|
462
515
|
target = oneLine(rawRef, 240)
|
|
463
516
|
if (STRUCTURAL_PAGES.has(target.replace(/^\/*wiki\//, "").replace(/\.md$/i, ""))) {
|
|
464
|
-
return
|
|
517
|
+
return refuse(`not deleted: "${target}" is one of the wiki's structural pages (index, schema, purpose), which the wiki keeps. Nothing was asked or deleted.`)
|
|
465
518
|
}
|
|
466
519
|
}
|
|
467
|
-
if (toolkit.runtime.records?.isRecordsWiki(givenWiki)) return
|
|
520
|
+
if (toolkit.runtime.records?.isRecordsWiki(givenWiki)) return refuse(`not deleted: ${RECORDS_REFUSAL}`)
|
|
468
521
|
|
|
469
522
|
const chosenVisibility = await writeVisibility(input, context)
|
|
470
|
-
if (chosenVisibility.error) return
|
|
523
|
+
if (chosenVisibility.error) return failedCall(chosenVisibility.error)
|
|
471
524
|
const { visibility } = chosenVisibility
|
|
472
525
|
const resolved = await resolveWrite(input, context, visibility)
|
|
473
|
-
if (resolved.invalid) return
|
|
474
|
-
if (resolved.error) return
|
|
526
|
+
if (resolved.invalid) return refuse(resolved.invalid)
|
|
527
|
+
if (resolved.error) return failedCall(resolved.error)
|
|
475
528
|
const { spaceContext, scoped } = resolved
|
|
476
529
|
const where = spaceContext ? toolkit.describeSpace(spaceContext) : "the local scope"
|
|
477
530
|
|
|
478
531
|
try {
|
|
479
532
|
const found = kind === "wiki" ? { wikiID: givenWiki, note: "" } : await wikiFor(input, scoped, "delete")
|
|
480
|
-
if (found.error) return
|
|
533
|
+
if (found.error) return refuse(found.error)
|
|
481
534
|
const { wikiID, note } = found
|
|
482
|
-
if (toolkit.runtime.records?.isRecordsWiki(wikiID)) return
|
|
535
|
+
if (toolkit.runtime.records?.isRecordsWiki(wikiID)) return refuse(`not deleted: ${RECORDS_REFUSAL}`)
|
|
483
536
|
|
|
484
537
|
// What would go, read now, so the preview and the approval name it.
|
|
485
538
|
let subject
|
|
@@ -489,30 +542,30 @@ export function registerWiki(editor, toolkit) {
|
|
|
489
542
|
try {
|
|
490
543
|
wiki = await backend.getWiki({ scope: scoped, wikiID })
|
|
491
544
|
} catch (error) {
|
|
492
|
-
if ([403, 404].includes(Number(error?.status))) return
|
|
545
|
+
if ([403, 404].includes(Number(error?.status))) return refuse(`not deleted: wiki ${oneLine(wikiID, 80)} was not found in ${where}. Nothing was asked or deleted.`)
|
|
493
546
|
throw error
|
|
494
547
|
}
|
|
495
548
|
if (isRecordsWikiName(wiki?.name)) {
|
|
496
549
|
toolkit.runtime.records?.noteRecordsWiki(wikiID)
|
|
497
|
-
return
|
|
550
|
+
return refuse(`not deleted: ${RECORDS_REFUSAL}`)
|
|
498
551
|
}
|
|
499
552
|
if (spaceContext && control) {
|
|
500
553
|
resourceID = await catalogResourceOf(scoped, wikiID)
|
|
501
|
-
if (!resourceID) return
|
|
554
|
+
if (!resourceID) return refuse(`not deleted: wiki ${oneLine(wikiID, 80)} is not in ${where}'s catalog, so it cannot be deleted from here. Nothing was asked or deleted.`)
|
|
502
555
|
const userId = spaceContext.user_id || runtime.config.user
|
|
503
556
|
try {
|
|
504
557
|
await control.deleteResource({ userId, spaceId: spaceContext.space_id, resourceId: resourceID, dryRun: true })
|
|
505
558
|
} catch (error) {
|
|
506
|
-
if ([400, 403, 404].includes(Number(error?.status))) return
|
|
559
|
+
if ([400, 403, 404].includes(Number(error?.status))) return refuse(`not deleted: ${errorMessage(error)}. Nothing was asked or deleted.`)
|
|
507
560
|
throw error
|
|
508
561
|
}
|
|
509
562
|
}
|
|
510
563
|
const pages = Number(wiki?.page_count) || 0
|
|
511
564
|
subject = `the whole wiki ${oneLine(wikiID, 80)} (${oneLine(wiki?.name ?? "", 120)}${pages ? `, ${pages} pages` : ""}): every source file and page${resourceID ? ", and its catalog entry and grants" : ""}`
|
|
512
565
|
} else {
|
|
513
|
-
if ((await wikiStatus(scoped, wikiID)) === "processing") return
|
|
566
|
+
if ((await wikiStatus(scoped, wikiID)) === "processing") return refuse(busyMessage(wikiID, false, "deleted"))
|
|
514
567
|
const existing = kind === "source" ? await readSource(scoped, wikiID, target) : await readPage(scoped, wikiID, target)
|
|
515
|
-
if (!existing) return
|
|
568
|
+
if (!existing) return refuse(`${note}${await notFound(scoped, wikiID, target)}\nNothing was asked or deleted.`)
|
|
516
569
|
subject =
|
|
517
570
|
kind === "source"
|
|
518
571
|
? `source file ${target} (${Buffer.byteLength(existing.content)} bytes) from wiki ${oneLine(wikiID, 80)}; the wiki also deletes the pages ingested only from it and rewrites the pages that cite it`
|
|
@@ -520,7 +573,7 @@ export function registerWiki(editor, toolkit) {
|
|
|
520
573
|
}
|
|
521
574
|
|
|
522
575
|
if (input?.confirm !== true) {
|
|
523
|
-
return
|
|
576
|
+
return refuse(
|
|
524
577
|
`${note}Preview only: nothing was deleted and nothing was asked. In ${where}, mem_wiki delete with confirm: true would delete ${subject}. That call asks the person first, then reads the deletion back. Only delete it if the person asked: a wiki or file you created by mistake is reported, and removed only if the person asks.`,
|
|
525
578
|
)
|
|
526
579
|
}
|
|
@@ -536,14 +589,14 @@ export function registerWiki(editor, toolkit) {
|
|
|
536
589
|
metadata: { kind: "wiki", target: kind, wiki: wikiID, ...(target ? { name: target } : {}), space: spaceContext?.space_id ?? "local", visibility },
|
|
537
590
|
})
|
|
538
591
|
} catch (error) {
|
|
539
|
-
return
|
|
592
|
+
return refuse(`wiki delete denied: ${errorMessage(error)}. Nothing was deleted.`)
|
|
540
593
|
}
|
|
541
594
|
|
|
542
595
|
if (kind === "wiki") {
|
|
543
596
|
if (resourceID) {
|
|
544
597
|
try {
|
|
545
598
|
const result = await control.deleteResource({ userId: spaceContext.user_id || runtime.config.user, spaceId: spaceContext.space_id, resourceId: resourceID })
|
|
546
|
-
if (result?.deleted !== true) return
|
|
599
|
+
if (result?.deleted !== true) return failedCall(`wiki ${oneLine(wikiID, 80)} was not deleted: the gateway did not confirm the delete.`)
|
|
547
600
|
} catch (error) {
|
|
548
601
|
return deleteFailure(error, wikiID, wikiID)
|
|
549
602
|
}
|
|
@@ -556,12 +609,12 @@ export function registerWiki(editor, toolkit) {
|
|
|
556
609
|
}
|
|
557
610
|
const deleted = (data?.deleted_ids ?? []).map(String).includes(wikiID)
|
|
558
611
|
const missing = (data?.failed ?? []).some((entry) => String(entry?.id) === wikiID && /not\s*found/i.test(String(entry?.reason ?? "")))
|
|
559
|
-
if (!deleted && !missing) return
|
|
612
|
+
if (!deleted && !missing) return failedCall(`wiki ${oneLine(wikiID, 80)} was not deleted: ${oneLine(errorMessage((data?.failed ?? [])[0]?.reason ?? "the wiki did not confirm it"), 200)}.`)
|
|
560
613
|
}
|
|
561
614
|
const back = await wikiGone(scoped, wikiID)
|
|
562
615
|
log.info("wiki deleted", { wikiID, catalog: Boolean(resourceID), verified: back.gone })
|
|
563
|
-
if (back.gone === false) return
|
|
564
|
-
return text(`deleted the whole wiki ${oneLine(wikiID, 80)} from ${where}${resourceID ? ", with its catalog entry" : ""}. Read back: ${back.text}.`)
|
|
616
|
+
if (back.gone === false) return failedCall(`wiki ${oneLine(wikiID, 80)}: the delete was reported, but ${back.text}. Report this to the Memory server's operator.`)
|
|
617
|
+
return withWriteState(text(`deleted the whole wiki ${oneLine(wikiID, 80)} from ${where}${resourceID ? ", with its catalog entry" : ""}. Read back: ${back.text}.`), { state: "ready" })
|
|
565
618
|
}
|
|
566
619
|
|
|
567
620
|
let data
|
|
@@ -583,20 +636,21 @@ export function registerWiki(editor, toolkit) {
|
|
|
583
636
|
]
|
|
584
637
|
: [Array.isArray(data?.rewritten_files) && data.rewritten_files.length ? `It removed links to it from ${data.rewritten_files.length} file(s).` : ""]
|
|
585
638
|
log.info("wiki file deleted", { wikiID, kind, verified: !still })
|
|
586
|
-
if (still) return
|
|
587
|
-
return
|
|
588
|
-
`${note}deleted ${kind === "source" ? "source file" : "page"} ${target} from wiki ${oneLine(wikiID, 80)}. ${cascade.filter(Boolean).join(" ")}${cascade.some(Boolean) ? " " : ""}Read back: it is gone
|
|
639
|
+
if (still) return failedCall(`${kind === "source" ? "source file" : "page"} ${target} in wiki ${oneLine(wikiID, 80)}: the delete was reported, but it still reads back. Report this to the Memory server's operator.`)
|
|
640
|
+
return withWriteState(
|
|
641
|
+
text(`${note}deleted ${kind === "source" ? "source file" : "page"} ${target} from wiki ${oneLine(wikiID, 80)}. ${cascade.filter(Boolean).join(" ")}${cascade.some(Boolean) ? " " : ""}Read back: it is gone.`),
|
|
642
|
+
{ state: "ready" },
|
|
589
643
|
)
|
|
590
644
|
} catch (error) {
|
|
591
645
|
log.warn("wiki delete failed", error)
|
|
592
|
-
return
|
|
646
|
+
return failedCall(errorMessage(error))
|
|
593
647
|
}
|
|
594
648
|
}
|
|
595
649
|
|
|
596
650
|
const wiki = {
|
|
597
651
|
name: "wiki",
|
|
598
652
|
description:
|
|
599
|
-
"When asked to read a named document or wiki file, use mem_wiki read (or search to find it), even if mem_recall might answer. Call list, search and read for documentation, architecture, ownership and call-graph questions about what was uploaded or indexed. Write only documents the person gave you, as flat source files (\"notes.md\"; a name with a folder is refused). A space has a knowledge wiki only once action create makes one (name it \"My knowledge\" in the personal space); list shows which wikis exist. A write starts
|
|
653
|
+
"When asked to read a named document or wiki file, use mem_wiki read (or search to find it), even if mem_recall might answer. Call list, search and read for documentation, architecture, ownership and call-graph questions about what was uploaded or indexed. Write only documents the person gave you, as flat source files (\"notes.md\"; a name with a folder is refused). A space has a knowledge wiki only once action create makes one (name it \"My knowledge\" in the personal space); list shows which wikis exist. A write starts one ingest by itself a moment after the last one; a write during an ingest is queued, not refused. Like mem_recall it uses the session's space by default: your personal space, or a shared space bound to the session. A write to a shared space asks the person and shows the file and an excerpt; a write to your personal space does not. delete removes one source file (filename), one generated page (ref), or a whole wiki (wiki_id alone): without confirm it only previews; with confirm: true it always asks the person, then deletes and reads it back. Never put things to do (mem_commitments), where a project stands, or person and project pages (mem_page) here.",
|
|
600
654
|
input: {
|
|
601
655
|
type: "object",
|
|
602
656
|
properties: {
|
|
@@ -629,7 +683,7 @@ export function registerWiki(editor, toolkit) {
|
|
|
629
683
|
ingest: {
|
|
630
684
|
type: "boolean",
|
|
631
685
|
description:
|
|
632
|
-
"
|
|
686
|
+
"Ignored: ingestion always starts on its own a short while after the last write, so a burst of writes makes one ingest",
|
|
633
687
|
},
|
|
634
688
|
ref: {
|
|
635
689
|
type: "string",
|
|
@@ -662,22 +716,28 @@ export function registerWiki(editor, toolkit) {
|
|
|
662
716
|
execute: async (input, context) => {
|
|
663
717
|
const action = String(input?.action ?? "list")
|
|
664
718
|
const asked = requestedVisibility(input)
|
|
719
|
+
// 0.11.3 (jelly's #89 ask A4): a write's answer opens with its state
|
|
720
|
+
// line, and a refusal opens with `state: refused` and names nothing. A
|
|
721
|
+
// read's answer is exactly what it was.
|
|
722
|
+
const isWrite = FILE_WRITE_ACTIONS.includes(action)
|
|
723
|
+
const answer = (content) => (isWrite ? refusedAnswer(text(content)) : text(content))
|
|
724
|
+
const failure = (message) => (isWrite ? failedAnswer(fail(message)) : fail(message))
|
|
665
725
|
|
|
666
726
|
if (action === "delete") return deleteAction(input, context)
|
|
667
727
|
|
|
668
728
|
if (FILE_WRITE_ACTIONS.includes(action)) {
|
|
669
729
|
const content = String(input?.content ?? "")
|
|
670
|
-
if (action !== "create" && !content.trim()) return
|
|
671
|
-
if (Buffer.byteLength(content) > MAX_FILE_BYTES) return
|
|
730
|
+
if (action !== "create" && !content.trim()) return answer(`nothing to ${action}: content is empty`)
|
|
731
|
+
if (Buffer.byteLength(content) > MAX_FILE_BYTES) return answer("wiki content is too large (max 512 KB)")
|
|
672
732
|
// Everything that can be refused from the call alone is refused before
|
|
673
733
|
// anything is asked: a person never approves a write that cannot run.
|
|
674
|
-
if (action === "create" && !oneLine(input?.name, 200).trim()) return
|
|
734
|
+
if (action === "create" && !oneLine(input?.name, 200).trim()) return answer("wiki name is required")
|
|
675
735
|
const createName = action === "create" ? filenameOf(input) : ""
|
|
676
736
|
const chosen = action !== "create" ? writeTarget(input) : createName ? { filename: createName } : { invalid: true }
|
|
677
|
-
if (chosen.missing) return
|
|
678
|
-
if (chosen.invalid) return
|
|
737
|
+
if (chosen.missing) return answer(`filename is required for ${action}: the source file, for example "project-harbour.md"`)
|
|
738
|
+
if (chosen.invalid) return answer("invalid filename")
|
|
679
739
|
const target = chosen.filename
|
|
680
|
-
if (target.includes("/")) return
|
|
740
|
+
if (target.includes("/")) return answer(nestedRefusal(action, target))
|
|
681
741
|
// The records wiki is mem_commitments' store: never written here.
|
|
682
742
|
if (
|
|
683
743
|
(action === "create" && isRecordsWikiName(oneLine(input?.name, 200).trim())) ||
|
|
@@ -685,22 +745,22 @@ export function registerWiki(editor, toolkit) {
|
|
|
685
745
|
target === RECORDS_FILE ||
|
|
686
746
|
isRecordsText(content)
|
|
687
747
|
) {
|
|
688
|
-
return
|
|
748
|
+
return answer(`not written: ${RECORDS_REFUSAL}`)
|
|
689
749
|
}
|
|
690
750
|
|
|
691
751
|
const chosenVisibility = await writeVisibility(input, context)
|
|
692
|
-
if (chosenVisibility.error) return
|
|
752
|
+
if (chosenVisibility.error) return failure(chosenVisibility.error)
|
|
693
753
|
const { visibility } = chosenVisibility
|
|
694
754
|
const resolved = await resolveWrite(input, context, visibility)
|
|
695
|
-
if (resolved.invalid) return
|
|
696
|
-
if (resolved.error) return
|
|
755
|
+
if (resolved.invalid) return answer(resolved.invalid)
|
|
756
|
+
if (resolved.error) return failure(resolved.error)
|
|
697
757
|
|
|
698
758
|
try {
|
|
699
759
|
if (action === "create") {
|
|
700
760
|
const name = oneLine(input?.name, 200).trim()
|
|
701
761
|
const filename = target
|
|
702
762
|
const denied = await approveWrite(context, visibility, resolved.spaceContext, approvalOf(action, input, target, content))
|
|
703
|
-
if (denied) return
|
|
763
|
+
if (denied) return answer(`wiki write denied: ${denied}`)
|
|
704
764
|
const wiki = await backend.createWiki({ scope: resolved.scoped, name })
|
|
705
765
|
const wikiID = String(wiki?.wiki_id ?? "")
|
|
706
766
|
if (!wikiID) throw new Error("wiki/create returned no wiki_id")
|
|
@@ -708,60 +768,69 @@ export function registerWiki(editor, toolkit) {
|
|
|
708
768
|
try {
|
|
709
769
|
resource = await registerWikiResource(resolved.spaceContext, wiki, name)
|
|
710
770
|
} catch (error) {
|
|
711
|
-
return
|
|
771
|
+
return failure(
|
|
712
772
|
`wiki ${wikiID} was created but resource registration failed: ${errorMessage(error)}. An operator must register it before the gateway can expose it.`,
|
|
713
773
|
)
|
|
714
774
|
}
|
|
715
775
|
let written
|
|
716
776
|
try {
|
|
717
|
-
written = await writeRaw(resolved.scoped, wikiID, filename, content || `# ${name}\n
|
|
777
|
+
written = await writeRaw(resolved.scoped, wikiID, filename, content || `# ${name}\n`)
|
|
718
778
|
} catch (error) {
|
|
719
779
|
log.warn("wiki create write failed", error)
|
|
720
780
|
return hubFailure(error, wikiID, filename)
|
|
721
781
|
}
|
|
722
782
|
log.info("wiki created", { wikiID, visibility, resource: resource?.id ?? null })
|
|
723
|
-
return
|
|
724
|
-
`created wiki ${wikiID} (${name}); wrote ${filename}; ${ingestNote(written)}${resource?.id ? `; resource ${resource.id}` : ""}
|
|
783
|
+
return withWriteState(
|
|
784
|
+
text(`created wiki ${wikiID} (${name}); wrote ${filename}; ${ingestNote(written)}${resource?.id ? `; resource ${resource.id}` : ""}`),
|
|
785
|
+
{ state: writtenState(written), id: `${wikiID}/${filename}`, link: wikiPlace(wikiID, filename) },
|
|
725
786
|
)
|
|
726
787
|
}
|
|
727
788
|
|
|
728
789
|
// As read does: with one visible wiki, it is the target, and the
|
|
729
790
|
// answer names it. With none or several, the refusal lists them.
|
|
730
791
|
const found = await wikiFor(input, resolved.scoped, action)
|
|
731
|
-
if (found.error) return
|
|
792
|
+
if (found.error) return answer(found.error)
|
|
732
793
|
const { wikiID, note } = found
|
|
733
|
-
if (toolkit.runtime.records?.isRecordsWiki(wikiID)) return
|
|
794
|
+
if (toolkit.runtime.records?.isRecordsWiki(wikiID)) return answer(`not written: ${RECORDS_REFUSAL}`)
|
|
734
795
|
const filename = target
|
|
735
|
-
// An ingesting wiki refuses the write: say so before anyone is asked.
|
|
736
|
-
if ((await wikiStatus(resolved.scoped, wikiID)) === "processing") return text(busyMessage(wikiID, false))
|
|
737
|
-
|
|
738
796
|
let body = content
|
|
739
797
|
if (action === "append") {
|
|
740
798
|
const existing = await backend.readWikiRaw({ scope: resolved.scoped, wikiID, filenames: [filename] })
|
|
741
799
|
const current = (existing?.items ?? []).find((item) => item?.filename === filename)?.content ?? ""
|
|
742
800
|
body = current ? `${current.replace(/\s+$/, "")}\n\n${content}` : content
|
|
743
|
-
if (Buffer.byteLength(body) > MAX_FILE_BYTES) return
|
|
801
|
+
if (Buffer.byteLength(body) > MAX_FILE_BYTES) return answer("wiki file is too large after append")
|
|
744
802
|
}
|
|
745
803
|
|
|
746
804
|
const denied = await approveWrite(context, visibility, resolved.spaceContext, approvalOf(action, input, target, content))
|
|
747
|
-
if (denied) return
|
|
805
|
+
if (denied) return answer(`wiki write denied: ${denied}`)
|
|
748
806
|
let written
|
|
749
807
|
try {
|
|
750
|
-
written = await writeRaw(resolved.scoped, wikiID, filename, body
|
|
808
|
+
written = await writeRaw(resolved.scoped, wikiID, filename, body)
|
|
751
809
|
} catch (error) {
|
|
752
810
|
log.warn("wiki write failed", error)
|
|
753
811
|
return hubFailure(error, wikiID, filename)
|
|
754
812
|
}
|
|
755
813
|
// Say what the write guarantees, so the agent does not read its own
|
|
756
814
|
// write back to check it: the source file now holds exactly this.
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
815
|
+
// A queued write guarantees nothing yet: the wiki is ingesting.
|
|
816
|
+
if (written.queued) {
|
|
817
|
+
const what = action === "append" ? "the append to" : ""
|
|
818
|
+
return withWriteState(
|
|
819
|
+
text(`${note}queued ${what ? `${what} ` : ""}${filename} in wiki ${wikiID} (${Buffer.byteLength(body)} bytes); ${ingestNote(written)}`),
|
|
820
|
+
{ state: writtenState(written), id: `${wikiID}/${filename}`, link: wikiPlace(wikiID, filename) },
|
|
821
|
+
)
|
|
822
|
+
}
|
|
823
|
+
return withWriteState(
|
|
824
|
+
text(
|
|
825
|
+
`${note}${action === "append" ? "appended to" : "wrote"} ${filename} in wiki ${wikiID}: the source file now holds ${
|
|
826
|
+
action === "append" ? "its earlier text and then" : "exactly"
|
|
827
|
+
} the content you sent (${Buffer.byteLength(body)} bytes), and a read of it returns that; ${ingestNote(written)}`,
|
|
828
|
+
),
|
|
829
|
+
{ state: writtenState(written), id: `${wikiID}/${filename}`, link: wikiPlace(wikiID, filename) },
|
|
761
830
|
)
|
|
762
831
|
} catch (error) {
|
|
763
832
|
log.warn("wiki write failed", error)
|
|
764
|
-
return
|
|
833
|
+
return failure(errorMessage(error))
|
|
765
834
|
}
|
|
766
835
|
}
|
|
767
836
|
|
|
@@ -773,8 +842,8 @@ export function registerWiki(editor, toolkit) {
|
|
|
773
842
|
: asked === "auto" && control
|
|
774
843
|
? await resolveReadSpace(input, context, "auto")
|
|
775
844
|
: await resolveSpace(input, context)
|
|
776
|
-
if (resolved.invalid) return
|
|
777
|
-
if (resolved.error) return
|
|
845
|
+
if (resolved.invalid) return answer(resolved.invalid)
|
|
846
|
+
if (resolved.error) return failure(resolved.error)
|
|
778
847
|
const spaceContext = resolved.context
|
|
779
848
|
const scoped = spaceContext
|
|
780
849
|
? toolkit.scopeFor(spaceContext, {
|
|
@@ -830,6 +899,7 @@ export function registerWiki(editor, toolkit) {
|
|
|
830
899
|
)}`,
|
|
831
900
|
)
|
|
832
901
|
}
|
|
902
|
+
const wikiByID = new Map()
|
|
833
903
|
for (const wiki of wikis?.items ?? []) {
|
|
834
904
|
const wikiID = String(wiki.wiki_id ?? "")
|
|
835
905
|
if (!wikiID || seen.has(wikiID)) continue
|
|
@@ -839,10 +909,11 @@ export function registerWiki(editor, toolkit) {
|
|
|
839
909
|
continue
|
|
840
910
|
}
|
|
841
911
|
seen.add(wikiID)
|
|
912
|
+
wikiByID.set(wikiID, wiki)
|
|
842
913
|
lines.push(
|
|
843
914
|
`- ${neutralizeFenceClosers(oneLine(wikiID, 120))} [wiki] ${neutralizeFenceClosers(
|
|
844
915
|
oneLine(wiki.name ?? "", 200),
|
|
845
|
-
)} (${neutralizeFenceClosers(oneLine(wiki
|
|
916
|
+
)} (${neutralizeFenceClosers(oneLine(wikiStateOf(wiki), 40))}${wiki.page_count ? `, ${wiki.page_count} pages` : ""})`,
|
|
846
917
|
)
|
|
847
918
|
}
|
|
848
919
|
for (const graph of graphs?.items ?? []) {
|
|
@@ -855,7 +926,7 @@ export function registerWiki(editor, toolkit) {
|
|
|
855
926
|
)} (${neutralizeFenceClosers(oneLine(graph.status ?? "?", 40))})`,
|
|
856
927
|
)
|
|
857
928
|
}
|
|
858
|
-
if (lines.length === 0) return
|
|
929
|
+
if (lines.length === 0) return answer(emptyKnowledge(input, spaceContext))
|
|
859
930
|
// Each wiki's source files, so the next call can read a page by name
|
|
860
931
|
// (filename) instead of searching for its path first. Bounded: the
|
|
861
932
|
// first five wikis, fifteen names each.
|
|
@@ -863,21 +934,33 @@ export function registerWiki(editor, toolkit) {
|
|
|
863
934
|
const listed = await Promise.all(wikiRows.slice(0, 5).map((row) => sourceFiles(scoped, row.id)))
|
|
864
935
|
wikiRows.slice(0, 5).forEach((row, index) => {
|
|
865
936
|
const { names, more } = listed[index]
|
|
866
|
-
|
|
867
|
-
|
|
937
|
+
const extra = []
|
|
938
|
+
if (names.length > 0) {
|
|
939
|
+
extra.push(` files (pass as filename): ${names.map((name) => neutralizeFenceClosers(oneLine(name, 120))).join(", ")}${more ? `, +${more} more` : ""}`)
|
|
940
|
+
}
|
|
941
|
+
// While a wiki ingests the hub lists no pages; the last ready set
|
|
942
|
+
// this engine saw is listed instead, saying it is stale (A2).
|
|
943
|
+
const state = wikiByID.has(row.id) ? wikiStateOf(wikiByID.get(row.id)) : "ready"
|
|
944
|
+
const stale = state === "ready" ? [] : cache.pages(row.id).slice(0, 10)
|
|
945
|
+
if (stale.length > 0) {
|
|
946
|
+
extra.push(
|
|
947
|
+
` pages (pass as ref): ${stale.map((page) => neutralizeFenceClosers(oneLine(page.ref, 120))).join(", ")}. The wiki is ${oneLine(state, 40)}, so these are the last ready set, stale until it finishes.`,
|
|
948
|
+
)
|
|
949
|
+
}
|
|
950
|
+
if (extra.length > 0) lines[row.index] = `${row.line}\n${extra.join("\n")}`
|
|
868
951
|
})
|
|
869
|
-
return
|
|
952
|
+
return answer(["<knowledge_assets>", ...lines, "</knowledge_assets>"].join("\n"))
|
|
870
953
|
}
|
|
871
954
|
|
|
872
955
|
if (action === "read") {
|
|
873
956
|
const target = await wikiFor(input, scoped, "read")
|
|
874
|
-
if (target.empty) return
|
|
875
|
-
if (target.error) return
|
|
957
|
+
if (target.empty) return answer(emptyKnowledge(input, spaceContext))
|
|
958
|
+
if (target.error) return answer(target.error)
|
|
876
959
|
const { wikiID, note } = target
|
|
877
|
-
if (toolkit.runtime.records?.isRecordsWiki(wikiID)) return
|
|
960
|
+
if (toolkit.runtime.records?.isRecordsWiki(wikiID)) return answer(RECORDS_REFUSAL)
|
|
878
961
|
const ref = String(input?.ref ?? input?.path ?? "").trim()
|
|
879
962
|
const name = ref || filenameOf(input)
|
|
880
|
-
if (!name) return
|
|
963
|
+
if (!name) return answer("invalid filename")
|
|
881
964
|
// The source file first: it is what the agent writes, and it is
|
|
882
965
|
// current at once, while the extracted page lags until ingestion
|
|
883
966
|
// finishes (a model that re-read the page after a write saw the old
|
|
@@ -894,25 +977,25 @@ export function registerWiki(editor, toolkit) {
|
|
|
894
977
|
const found = await read(scoped, wikiID, target)
|
|
895
978
|
if (found && isRecordsText(found.content)) {
|
|
896
979
|
toolkit.runtime.records?.noteRecordsWiki(wikiID)
|
|
897
|
-
return
|
|
980
|
+
return answer(RECORDS_REFUSAL)
|
|
898
981
|
}
|
|
899
982
|
if (found) {
|
|
900
983
|
const safe = quarantineText(found.content.slice(0, 12_000))
|
|
901
984
|
const after = quarantineNote(safe.removed)
|
|
902
|
-
return
|
|
985
|
+
return answer(
|
|
903
986
|
`${note}<knowledge source="${neutralizeFenceClosers(oneLine(found.source, 120))}">\n${neutralizeFenceClosers(
|
|
904
987
|
safe.text,
|
|
905
988
|
)}\n</knowledge>${after ? `\n${after}` : ""}`,
|
|
906
989
|
)
|
|
907
990
|
}
|
|
908
991
|
}
|
|
909
|
-
return
|
|
992
|
+
return answer(`${note}${await notFound(scoped, wikiID, name)}`)
|
|
910
993
|
}
|
|
911
994
|
|
|
912
995
|
if (action === "status") {
|
|
913
996
|
const target = await wikiFor(input, scoped, "status")
|
|
914
|
-
if (target.empty) return
|
|
915
|
-
if (target.error) return
|
|
997
|
+
if (target.empty) return answer(emptyKnowledge(input, spaceContext))
|
|
998
|
+
if (target.error) return answer(target.error)
|
|
916
999
|
const { wikiID } = target
|
|
917
1000
|
const [wiki, pages] = await Promise.all([
|
|
918
1001
|
backend.getWiki({ scope: scoped, wikiID }),
|
|
@@ -921,28 +1004,30 @@ export function registerWiki(editor, toolkit) {
|
|
|
921
1004
|
// While it ingests the hub lists no pages; its own count is the answer then.
|
|
922
1005
|
const listed = Array.isArray(pages?.items) ? pages.items.length : 0
|
|
923
1006
|
const pageCount = listed || Number(wiki?.page_count) || 0
|
|
924
|
-
const
|
|
1007
|
+
const state = wikiStateOf(wiki)
|
|
1008
|
+
if (state === "ready" && Array.isArray(pages?.items)) cache.rememberPages(wikiID, listedPages(pages))
|
|
1009
|
+
const error = state === "failed" ? wikiErrorOf(wiki, cache.error(wikiID)) : ""
|
|
925
1010
|
return text(
|
|
926
|
-
`wiki: ${oneLine(wiki?.wiki_id ?? wikiID, 120)}\nname: ${oneLine(wiki?.name ?? "", 200)}\nstatus: ${oneLine(
|
|
927
|
-
|
|
928
|
-
}\npages: ${pageCount}`,
|
|
1011
|
+
`wiki: ${oneLine(wiki?.wiki_id ?? wikiID, 120)}\nname: ${oneLine(wiki?.name ?? "", 200)}\nstatus: ${oneLine(state, 40)}${
|
|
1012
|
+
state === "processing" ? " (ingesting: a write during it is queued until it finishes)" : ""
|
|
1013
|
+
}\npages: ${pageCount}${error ? `\nerror: ${oneLine(error, 400)}` : ""}`,
|
|
929
1014
|
)
|
|
930
1015
|
}
|
|
931
1016
|
|
|
932
1017
|
const query = String(input?.query ?? "").trim()
|
|
933
|
-
if (!query) return
|
|
1018
|
+
if (!query) return answer("nothing to search: query is empty")
|
|
934
1019
|
const ids = input?.knowledge_id
|
|
935
1020
|
? [String(input.knowledge_id)]
|
|
936
1021
|
: await discoverKnowledge(backend, log, assetType, scoped)
|
|
937
|
-
if (ids.length === 0) return
|
|
1022
|
+
if (ids.length === 0) return answer(emptyKnowledge(input, spaceContext))
|
|
938
1023
|
const result = await searchKnowledge(backend, ids.slice(0, 8), query, scoped, {
|
|
939
1024
|
maxChars: config.recallChars,
|
|
940
1025
|
})
|
|
941
|
-
if (!result) return
|
|
942
|
-
return
|
|
1026
|
+
if (!result) return answer("no knowledge results")
|
|
1027
|
+
return answer(assetType === "code_graph" ? result : `${result}\n\n${READ_HINT}`)
|
|
943
1028
|
} catch (error) {
|
|
944
1029
|
log.warn("wiki failed", error)
|
|
945
|
-
return
|
|
1030
|
+
return failure(errorMessage(error))
|
|
946
1031
|
}
|
|
947
1032
|
},
|
|
948
1033
|
}
|