@joe-broadhead/opencode-mem 0.8.5 → 0.9.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 (58) hide show
  1. package/CHANGELOG.md +512 -0
  2. package/README.md +127 -61
  3. package/docs/agent-protocol.md +67 -177
  4. package/package.json +1453 -100
  5. package/skills/mem.pages/SKILL.md +146 -0
  6. package/skills/mem.sharing/SKILL.md +91 -0
  7. package/skills/mem.sources/SKILL.md +94 -0
  8. package/skills/mem.usage/SKILL.md +129 -492
  9. package/src/backend/records.js +285 -0
  10. package/src/backend/v3.js +3 -0
  11. package/src/bootstrap.js +49 -45
  12. package/src/call-shapes.js +41 -10
  13. package/src/collector-lock.js +162 -0
  14. package/src/collector-service.js +441 -0
  15. package/src/collector.js +381 -0
  16. package/src/commands.js +3 -3
  17. package/src/config.js +15 -1
  18. package/src/credentials.js +26 -1
  19. package/src/curation.js +1 -1
  20. package/src/permissions.js +87 -10
  21. package/src/protocol.js +29 -20
  22. package/src/recall.js +104 -27
  23. package/src/records-store.js +267 -121
  24. package/src/records.js +63 -11
  25. package/src/runtime.js +23 -14
  26. package/src/skills.js +44 -18
  27. package/src/sources.js +58 -0
  28. package/src/spaces.js +93 -10
  29. package/src/tools/aliases.js +164 -0
  30. package/src/tools/commitments.js +12 -9
  31. package/src/tools/held-write.js +82 -0
  32. package/src/tools/index.js +100 -23
  33. package/src/tools/ingest.js +277 -0
  34. package/src/tools/knowledge.js +6 -1
  35. package/src/tools/memory.js +722 -229
  36. package/src/tools/page.js +423 -0
  37. package/src/tools/recall.js +371 -113
  38. package/src/tools/records-kit.js +142 -0
  39. package/src/tools/share.js +1042 -0
  40. package/src/tools/shared.js +16 -2
  41. package/src/tools/spaces.js +431 -143
  42. package/src/tools/status.js +87 -58
  43. package/src/tools/wiki.js +8 -4
  44. package/src/views/contracts.js +293 -3
  45. package/src/views/inbox-decision.js +59 -0
  46. package/src/views/recall-result.js +12 -0
  47. package/src/views/records-reads.js +675 -0
  48. package/src/views/rpc.js +58 -99
  49. package/src/views/shape.js +61 -0
  50. package/views/commitments.html +4 -0
  51. package/views/graph.html +753 -0
  52. package/views/home.html +11 -5
  53. package/views/journal.html +621 -0
  54. package/views/memory.html +7 -1
  55. package/views/pages.html +636 -0
  56. package/views/recall.html +123 -0
  57. package/views/review.html +406 -0
  58. package/views/today.html +309 -0
@@ -1,42 +1,111 @@
1
- import { MEM_NAMESPACE_DESCRIPTION, PINNED_TOOLS } from "../call-shapes.js"
2
- import { asSession } from "../credentials.js"
1
+ import { MEM_NAMESPACE_DESCRIPTION, PINNED_TOOLS, WIKI_DOCUMENTS_NOTE } from "../call-shapes.js"
2
+ import { asSession, asVerifiedSession } from "../credentials.js"
3
+ import { createCollectorService } from "../collector-service.js"
3
4
  import { createToolkit } from "./shared.js"
4
- import { registerRecall } from "./recall.js"
5
5
  import { registerWiki } from "./wiki.js"
6
- import { registerMemory } from "./memory.js"
7
- import { registerCommitments } from "./commitments.js"
8
- import { registerStatus } from "./status.js"
6
+ import { registerShare } from "./share.js"
9
7
  import { registerSpaces } from "./spaces.js"
8
+ import { registerCollect, registerCollectCommand, registerIngest } from "./ingest.js"
9
+ import { LEGACY_TOOLS, registerAliases } from "./aliases.js"
10
+
11
+ // ── Lane PLUG-A: the records-backed core tools ──────────────────────────────
12
+ // recall, remember, revise, page and commitments (contract §4.1). Until lane
13
+ // PLUG-A lands, today's registrations stand in. The integrator replaces the
14
+ // three import lines and CORE_TOOLS below, and nothing else, with imports of
15
+ // registerRecall, registerRemember, registerRevise, registerPage and
16
+ // registerCommitments (from the modules PLUG-A chose) and
17
+ //
18
+ // CORE_TOOLS = Object.freeze([registerRecall, registerRemember, registerRevise, registerPage, registerCommitments])
19
+ //
20
+ // A core registration that still adds
21
+ // forget, promote, status, grants or members is captured as the alias's
22
+ // legacy path, never registered twice (LEGACY_TOOLS).
23
+ import { registerRecall } from "./recall.js"
24
+ import { registerRemember, registerRevise } from "./memory.js"
25
+ import { registerPage } from "./page.js"
26
+ import { registerCommitments } from "./commitments.js"
27
+ export const CORE_TOOLS = Object.freeze([registerRecall, registerRemember, registerRevise, registerPage, registerCommitments])
28
+ // ── end of the lane PLUG-A block ────────────────────────────────────────────
10
29
 
11
30
  /**
12
- * Register the `mem_*` tool family.
31
+ * Register the `mem_*` tool family and the `/mem-collect` command.
13
32
  *
14
33
  * Recall is deliberately a tool rather than an automatic injection: pull-based
15
34
  * retrieval is cheaper, attributable, and does not invalidate prompt caches.
16
35
  *
17
36
  * The tools are registered even when memory is unavailable (a gateway or
18
37
  * Jelly install with no credential): each then answers the one sentence
19
- * `runtime.unavailable()` gives, `mem_status` included, and makes no request.
20
- * Each call runs as its session (`context.sessionID`, `asSession`): inside
21
- * Jelly it uses that session's token, and in a session whose speaker has no
22
- * Jelly account linked on the host it answers one sentence saying so.
38
+ * `runtime.unavailable()` gives and makes no request. Each call runs as its
39
+ * session (`context.sessionID`, `asSession`): inside Jelly it uses that
40
+ * session's token, and in a session whose speaker has no Jelly account linked
41
+ * on the host it answers one sentence saying so.
23
42
  *
24
43
  * @param {any} ctx
25
44
  * @param {any} runtime
45
+ * @param {{ core?: readonly Function[] }} [options] `core`: the core registrations (tests pass contract stubs)
26
46
  */
27
- export async function registerTools(ctx, runtime) {
47
+ export async function registerTools(ctx, runtime, { core = CORE_TOOLS } = {}) {
28
48
  if (!runtime.config.tools) return null
29
49
  const toolkit = createToolkit(ctx, runtime)
30
- return ctx.tool.transform((editor) => {
50
+ toolkit.collector = createCollectorService({ ctx, runtime, toolkit })
51
+ const tools = await ctx.tool.transform((editor) => {
31
52
  editor.namespace({ name: "mem", description: MEM_NAMESPACE_DESCRIPTION })
32
- const gated = gateEditor(editor, runtime)
33
- registerRecall(gated, toolkit)
34
- registerWiki(gated, toolkit)
35
- registerMemory(gated, toolkit)
36
- registerCommitments(gated, toolkit)
37
- registerStatus(gated, toolkit)
38
- registerSpaces(gated, toolkit)
53
+ buildToolset(gateEditor(editor, runtime), toolkit, core)
54
+ })
55
+ let command = null
56
+ try {
57
+ command = await registerCollectCommand(ctx, runtime, toolkit, { asSession })
58
+ } catch (error) {
59
+ runtime.log?.warn?.("the /mem-collect command could not be registered", error)
60
+ }
61
+ // The in-plugin collector's timers (src/collector-service.js): unref'd, and
62
+ // every callback is caught, so they never hold the host open or throw into it.
63
+ try {
64
+ toolkit.collector.start()
65
+ } catch (error) {
66
+ runtime.log?.warn?.("the collector could not start", error)
67
+ }
68
+ return {
69
+ dispose: async () => {
70
+ await toolkit.collector.stop()
71
+ await tools?.dispose?.()
72
+ await command?.dispose?.()
73
+ },
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Register every tool on `editor`: the core tools, the documents tool, share,
79
+ * spaces, the collector's ingest and cursor, then the deprecated aliases. A
80
+ * core registration of a legacy name is kept aside for its alias.
81
+ *
82
+ * @returns {{ registered: Map<string, any>, legacy: Map<string, any> }}
83
+ */
84
+ export function buildToolset(editor, toolkit, core = CORE_TOOLS) {
85
+ const registered = new Map()
86
+ const legacy = new Map()
87
+ const capturing = new Proxy(editor, {
88
+ get(target, key, receiver) {
89
+ if (key !== "add") return Reflect.get(target, key, receiver)
90
+ return (definition) => {
91
+ if (LEGACY_TOOLS.includes(definition?.name)) {
92
+ legacy.set(definition.name, definition)
93
+ return
94
+ }
95
+ const adjusted = definition?.name === "wiki" ? { ...definition, description: `${WIKI_DOCUMENTS_NOTE} ${definition.description}` } : definition
96
+ registered.set(adjusted.name, adjusted)
97
+ target.add(adjusted)
98
+ }
99
+ },
39
100
  })
101
+ for (const register of core) register(capturing, toolkit)
102
+ registerWiki(capturing, toolkit)
103
+ const share = registerShare(capturing, toolkit)
104
+ const spaces = registerSpaces(capturing, toolkit)
105
+ registerIngest(capturing, toolkit)
106
+ registerCollect(capturing, toolkit)
107
+ registerAliases(editor, toolkit, { registered, legacy, share, spaces })
108
+ return { registered, legacy }
40
109
  }
41
110
 
42
111
  /**
@@ -51,12 +120,20 @@ function gateEditor(editor, runtime) {
51
120
  target.add({
52
121
  ...definition,
53
122
  options: { ...definition.options, ...(PINNED_TOOLS.includes(definition.name) ? { pinned: true } : {}) },
54
- execute: (input, context) =>
55
- asSession(context?.sessionID, async () => {
123
+ execute: (input, context) => {
124
+ const held = runtime.config.heldTeamWrites && (
125
+ (definition.name === "remember" && String(input?.visibility ?? "").toLowerCase() === "team") ||
126
+ (definition.name === "page" && input?.action === "write") ||
127
+ (definition.name === "share" && input?.action === "copy") ||
128
+ (definition.name === "promote" && !input?.revoke)
129
+ )
130
+ const run = async () => {
56
131
  const reason = (runtime.availability ? await runtime.availability() : runtime.unavailable?.()) ?? ""
57
132
  if (reason) return { content: reason }
58
133
  return definition.execute(input, context)
59
- }),
134
+ }
135
+ return held ? asVerifiedSession(context?.sessionID, run) : asSession(context?.sessionID, run)
136
+ },
60
137
  })
61
138
  },
62
139
  })
@@ -0,0 +1,277 @@
1
+ import { DEFAULT_ACCOUNT, isAccountName, isSourceName } from "../collector.js"
2
+ import { describeResult } from "../collector-service.js"
3
+ import { errorMessage, isPlainObject, oneLine } from "../util.js"
4
+ import { RECORDS_MISSING, recordsFailure, recordsOf } from "./share.js"
5
+ import { unboundOwner } from "../backend/records.js"
6
+
7
+ /** Events in one ingest call (contract §2.4). */
8
+ export const MAX_INGEST_EVENTS = 500
9
+
10
+ /**
11
+ * `mem_ingest` and `mem_cursor`: the collector's two calls
12
+ * (docs/design/memory-v1-contracts.md §4.1, §4.4). The `memory-collect`
13
+ * schedule's Code Mode check and `/mem-collect` use them; agents never call
14
+ * them, and their descriptions say so.
15
+ *
16
+ * Both work in the caller's personal space, where raw events stay private
17
+ * (memory-funnel-v1.md §10). Each answers structured `output` (so the check's
18
+ * code reads `c.cursor` and `saved.cursor_saved`) and a one-line `content`.
19
+ *
20
+ * A refused ingest (409 `source_unbound`: the binding is missing, or the
21
+ * signed-in account changed) is not an error to the check: it answers
22
+ * `cursor_saved: false` with the owner the records service reported, and the
23
+ * collector remembers that owner so `mem_spaces` can show it when the person
24
+ * binds the source.
25
+ *
26
+ * @param {any} editor
27
+ * @param {ReturnType<import("./shared.js").createToolkit>} toolkit
28
+ */
29
+ export function registerIngest(editor, toolkit) {
30
+ const { text, log } = toolkit
31
+
32
+ /** The personal space the events belong to, as a records call's `where`. */
33
+ async function personalWhere(context) {
34
+ if (!toolkit.control) return { where: { sessionID: context?.sessionID } }
35
+ const resolved = await toolkit.resolvePrivateSpace({}, context)
36
+ if (resolved.invalid) return { error: resolved.invalid }
37
+ if (resolved.error) return { error: resolved.error }
38
+ return { where: { space: resolved.context.space_id } }
39
+ }
40
+
41
+ function bindingOf(input) {
42
+ const source = String(input?.source ?? "").trim()
43
+ const account = String(input?.account ?? "").trim() || DEFAULT_ACCOUNT
44
+ if (!isSourceName(source)) return { error: `source "${oneLine(source || "(none)", 60)}" is not a source name` }
45
+ if (!isAccountName(account)) return { error: `account "${oneLine(account, 60)}" is not an account name` }
46
+ return { source, account }
47
+ }
48
+
49
+ const refuse = (reason, extra = {}) => ({ output: { accepted: 0, duplicates: 0, refused: 0, cursor_saved: false, error: reason, ...extra }, content: reason })
50
+
51
+ editor.add({
52
+ name: "ingest",
53
+ description:
54
+ "For the memory collector only (the memory-collect schedule and /mem-collect); agents never call it. Sends one page of a source's events to the person's private event log, with the source's new cursor. The records service keeps events only from the account the person bound, for 30 days, never as memories.",
55
+ input: {
56
+ type: "object",
57
+ properties: {
58
+ source: { type: "string", description: "The source's Code Mode namespace, such as gmail" },
59
+ account: { type: "string", description: 'The account within the source; default "default"' },
60
+ owner: {
61
+ type: "object",
62
+ description: "The signed-in account events_list reported",
63
+ properties: { id: { type: "string" }, label: { type: "string" } },
64
+ required: ["id"],
65
+ },
66
+ events: { type: "array", items: { type: "object" }, description: `events_list's events, oldest first, at most ${MAX_INGEST_EVENTS}` },
67
+ cursor: { type: "string", description: "events_list's cursor after these events" },
68
+ },
69
+ required: ["source", "owner", "events", "cursor"],
70
+ additionalProperties: false,
71
+ },
72
+ output: {
73
+ type: "object",
74
+ properties: {
75
+ accepted: { type: "integer" },
76
+ duplicates: { type: "integer" },
77
+ refused: { type: "integer" },
78
+ cursor_saved: { type: "boolean" },
79
+ error: { type: "string" },
80
+ owner: { type: "object" },
81
+ },
82
+ required: ["accepted", "duplicates", "refused", "cursor_saved"],
83
+ },
84
+ options: { namespace: "mem" },
85
+ execute: async (input, context) => {
86
+ const records = recordsOf(toolkit)
87
+ if (!records) return refuse(RECORDS_MISSING)
88
+ const binding = bindingOf(input)
89
+ if (binding.error) return refuse(`not ingested: ${binding.error}`)
90
+ if (!isPlainObject(input?.owner) || !String(input.owner.id ?? "").trim()) return refuse("not ingested: owner.id is required (events_list's owner)")
91
+ if (!Array.isArray(input?.events)) return refuse("not ingested: events must be an array")
92
+ if (input.events.length > MAX_INGEST_EVENTS) return refuse(`not ingested: at most ${MAX_INGEST_EVENTS} events per call`)
93
+ if (typeof input?.cursor !== "string" || !input.cursor) return refuse("not ingested: cursor is required (events_list's cursor)")
94
+ const target = await personalWhere(context)
95
+ if (target.error) return refuse(`not ingested: ${target.error}`)
96
+ const owner = { id: oneLine(input.owner.id, 300), label: oneLine(input.owner.label ?? input.owner.id, 300) }
97
+ try {
98
+ const answer = await records.call(
99
+ "events.ingest",
100
+ { source: binding.source, account: binding.account, owner, events: input.events, cursor: input.cursor },
101
+ target.where,
102
+ )
103
+ const output = {
104
+ accepted: Number(answer?.accepted) || 0,
105
+ duplicates: Number(answer?.duplicates) || 0,
106
+ refused: Number(answer?.refused) || 0,
107
+ cursor_saved: answer?.cursor_saved === true,
108
+ }
109
+ return {
110
+ output,
111
+ content: `${binding.source}:${binding.account}: ${output.accepted} accepted, ${output.duplicates} duplicate(s), ${output.refused} refused; cursor ${output.cursor_saved ? "saved" : "not saved"}.`,
112
+ }
113
+ } catch (error) {
114
+ const code = String(error?.code ?? error?.error?.code ?? "")
115
+ if (code === "source_unbound") {
116
+ const reported = unboundOwner(error) ?? owner
117
+ toolkit.collector?.noteOwner(binding.source, binding.account, reported)
118
+ return refuse(
119
+ `not ingested: ${binding.source}:${binding.account} is not bound to the account signed in (${oneLine(reported?.label ?? reported?.id ?? "unknown", 120)}). The person confirms it with mem_spaces ("action": "sources", "bind").`,
120
+ { owner: { id: oneLine(reported?.id ?? "", 300), label: oneLine(reported?.label ?? reported?.id ?? "", 300) } },
121
+ )
122
+ }
123
+ log.warn("ingest failed", { source: binding.source, error: errorMessage(error) })
124
+ return refuse(`not ingested: ${recordsFailure(error)}`)
125
+ }
126
+ },
127
+ })
128
+
129
+ editor.add({
130
+ name: "cursor",
131
+ description:
132
+ "For the memory collector only (the memory-collect schedule and /mem-collect); agents never call it. Reads where collection from one source stopped, and whether the person enabled and confirmed that source.",
133
+ input: {
134
+ type: "object",
135
+ properties: {
136
+ source: { type: "string", description: "The source's Code Mode namespace, such as gmail" },
137
+ account: { type: "string", description: 'The account within the source; default "default"' },
138
+ },
139
+ required: ["source"],
140
+ additionalProperties: false,
141
+ },
142
+ output: {
143
+ type: "object",
144
+ properties: {
145
+ cursor: { type: "string" },
146
+ cursor_at: { type: "string" },
147
+ enabled: { type: "boolean" },
148
+ status: { type: "string" },
149
+ error: { type: "string" },
150
+ },
151
+ required: ["enabled", "status"],
152
+ },
153
+ options: { namespace: "mem" },
154
+ execute: async (input, context) => {
155
+ const closed = (reason) => ({ output: { cursor: "", enabled: false, status: "unavailable", error: reason }, content: reason })
156
+ const records = recordsOf(toolkit)
157
+ if (!records) return closed(RECORDS_MISSING)
158
+ const binding = bindingOf(input)
159
+ if (binding.error) return closed(`no cursor: ${binding.error}`)
160
+ const target = await personalWhere(context)
161
+ if (target.error) return closed(`no cursor: ${target.error}`)
162
+ try {
163
+ const answer = await records.call("events.cursor", { source: binding.source, account: binding.account }, target.where)
164
+ const output = {
165
+ cursor: typeof answer?.cursor === "string" ? answer.cursor : "",
166
+ cursor_at: typeof answer?.cursor_at === "string" ? answer.cursor_at : "",
167
+ enabled: answer?.enabled === true || answer?.enabled === 1,
168
+ status: typeof answer?.status === "string" ? answer.status : "unknown",
169
+ }
170
+ return {
171
+ output,
172
+ content: `${binding.source}:${binding.account}: ${output.status}, ${output.enabled ? "enabled" : "disabled"}, cursor ${output.cursor ? `at ${oneLine(output.cursor_at || "an earlier run", 40)}` : "not set"}.`,
173
+ }
174
+ } catch (error) {
175
+ return closed(`no cursor: ${recordsFailure(error)}`)
176
+ }
177
+ },
178
+ })
179
+ }
180
+
181
+ /**
182
+ * `mem_collect`: collect now. For routines (Jelly's `collect` routine calls
183
+ * `await tools.mem.collect({})` and then returns false) and the person's
184
+ * explicit request. It never asks, and it answers at once: Jelly's whole
185
+ * check has 30 seconds. The run goes through the same one-per-machine lease
186
+ * as the timer (src/collector-service.js), and its counts appear in
187
+ * `mem_spaces` health; `wait: true` waits up to 25 seconds and reports them.
188
+ *
189
+ * @param {any} editor
190
+ * @param {ReturnType<import("./shared.js").createToolkit>} toolkit
191
+ */
192
+ export function registerCollect(editor, toolkit) {
193
+ editor.add({
194
+ name: "collect",
195
+ description:
196
+ "Collect new events from the person's bound sources (mail, chat, meetings) into Memory now. Call it only for routines and for the person's explicit request (\"collect my mail now\"); never on your own initiative: Memory already collects on its own while OpenCode runs (every 30 minutes by default). It never asks. It starts the collection in the background and answers at once: started, running (one is already going), or skipped with the reason; the counts appear in mem_spaces health. wait: true waits up to 25 seconds and reports the counts, or the progress so far.",
197
+ input: {
198
+ type: "object",
199
+ properties: {
200
+ source: { type: "string", description: "Collect only this source (gmail, slack, …); default every bound source" },
201
+ wait: { type: "boolean", description: "true: wait up to 25 seconds and report the counts (the person's explicit request); omitted: answer at once (routines)" },
202
+ },
203
+ additionalProperties: false,
204
+ },
205
+ output: {
206
+ type: "object",
207
+ properties: {
208
+ started: { type: "boolean" },
209
+ running: { type: "boolean" },
210
+ skipped: { type: "string" },
211
+ done: { type: "boolean" },
212
+ sources: { type: "array", items: { type: "object" } },
213
+ },
214
+ },
215
+ options: { namespace: "mem" },
216
+ execute: async (input, context) => {
217
+ const outcome = await toolkit.collector.trigger({ source: input?.source, wait: input?.wait === true, sessionID: context?.sessionID, from: "tool" })
218
+ return { output: collectOutput(outcome), content: describeOutcome(outcome) }
219
+ },
220
+ })
221
+ }
222
+
223
+ /** The structured answer: started, running or skipped, and the counts when it waited. */
224
+ function collectOutput(outcome) {
225
+ const output = {}
226
+ if (outcome.started) output.started = true
227
+ if (outcome.running) output.running = true
228
+ if (outcome.skipped) output.skipped = outcome.skipped
229
+ if ("done" in outcome) output.done = Boolean(outcome.done)
230
+ const sources = outcome.run?.sources ?? outcome.progress
231
+ if (Array.isArray(sources)) output.sources = sources.map(({ source, account, pages, accepted, duplicates, refused, more, error }) => ({ source, account, pages, accepted, duplicates, refused, more, ...(error ? { error } : {}) }))
232
+ return output
233
+ }
234
+
235
+ /** One line, or one line per source when the run was waited for. */
236
+ export function describeOutcome(outcome) {
237
+ if (outcome.skipped) return `collection skipped: ${outcome.skipped}`
238
+ const head = outcome.started ? "collection started" : `a collection is already running${outcome.since ? ` (since ${outcome.since})` : ""}`
239
+ if (!("done" in outcome)) return `${head}; its counts appear in mem_spaces health.`
240
+ if (!outcome.done) {
241
+ const progress = (outcome.progress ?? []).map((result) => `- ${describeResult(result)}${result.finished ? "" : " (so far)"}`)
242
+ return [`${head}; still running after 25 seconds, so these are the counts so far (the rest appear in mem_spaces health):`, ...progress].join("\n")
243
+ }
244
+ const run = outcome.run
245
+ if (!run) return `${head}; it finished.`
246
+ if (run.error) return `${head}; it failed: ${run.error}`
247
+ if (run.none || !run.sources?.length) return `${head}; nothing to collect: no source is enabled and confirmed. Bind one with mem_spaces ("action": "sources") when the person asks.`
248
+ return ["Memory collection:", ...run.sources.map((result) => `- ${describeResult(result)}`)].join("\n")
249
+ }
250
+
251
+ /**
252
+ * `/mem-collect`: the person's "collect now". Never model-callable; it waits
253
+ * up to 25 seconds for the run (the same one `mem_collect` starts) and replies
254
+ * with a synthetic message.
255
+ *
256
+ * @param {any} ctx
257
+ * @param {any} runtime
258
+ * @param {any} toolkit
259
+ */
260
+ export async function registerCollectCommand(ctx, runtime, toolkit, { asSession = (_id, fn) => fn() } = {}) {
261
+ if (!runtime.config.commands || typeof ctx?.command?.transform !== "function") return null
262
+ const reply = async (sessionID, message) => {
263
+ if (typeof ctx?.session?.synthetic !== "function") return
264
+ await ctx.session.synthetic({ sessionID, text: message }).catch(() => {})
265
+ }
266
+ return ctx.command.transform((editor) => {
267
+ editor.add({
268
+ name: "mem-collect",
269
+ description: "Collect now from the sources you bound to Memory (mail, chat, meetings), without a model turn",
270
+ execute: ({ sessionID }) =>
271
+ asSession(sessionID, async () => {
272
+ const outcome = await toolkit.collector.trigger({ wait: true, sessionID, from: "command" })
273
+ await reply(sessionID, describeOutcome(outcome))
274
+ }),
275
+ })
276
+ })
277
+ }
@@ -16,7 +16,7 @@ export function isRecordsWikiName(name) {
16
16
  * table is the index, but wiki and code-graph services are also first-class and
17
17
  * are not always mirrored into it, so both sources are merged.
18
18
  */
19
- export async function discoverKnowledge(backend, log, assetType, scope) {
19
+ export async function discoverKnowledge(backend, log, assetType, scope, options = {}) {
20
20
  const ids = []
21
21
  try {
22
22
  const data = await backend.listAccessibleAssets({ scope, assetType })
@@ -30,6 +30,7 @@ export async function discoverKnowledge(backend, log, assetType, scope) {
30
30
  }
31
31
  } catch (error) {
32
32
  log.debug("knowledge asset discovery failed", { assetType, error: errorMessage(error) })
33
+ options.onFailure?.("asset catalog", error)
33
34
  }
34
35
 
35
36
  if (!assetType || assetType === "llm_wiki") {
@@ -38,6 +39,7 @@ export async function discoverKnowledge(backend, log, assetType, scope) {
38
39
  for (const item of data?.items ?? []) if (item.wiki_id && !isRecordsWikiName(item.name)) ids.push(item.wiki_id)
39
40
  } catch (error) {
40
41
  log.debug("wiki discovery failed", { error: errorMessage(error) })
42
+ options.onFailure?.("wiki catalog", error)
41
43
  }
42
44
  }
43
45
  if (!assetType || assetType === "code_graph") {
@@ -46,6 +48,7 @@ export async function discoverKnowledge(backend, log, assetType, scope) {
46
48
  for (const item of data?.items ?? []) if (item.code_graph_id) ids.push(item.code_graph_id)
47
49
  } catch (error) {
48
50
  log.debug("code-graph discovery failed", { error: errorMessage(error) })
51
+ options.onFailure?.("code-graph catalog", error)
49
52
  }
50
53
  }
51
54
  return [...new Set(ids)]
@@ -143,6 +146,7 @@ export async function searchKnowledge(backend, ids, query, scoped, options = {})
143
146
  params: { query, limit: 5 },
144
147
  scope: scoped,
145
148
  })
149
+ options.onSearch?.(knowledgeID, true)
146
150
  const rendered = renderSearchHits(result)
147
151
  // A planted command in a page is data: it is removed, marked, and noted
148
152
  // outside the fence, as recall does for atoms.
@@ -155,6 +159,7 @@ export async function searchKnowledge(backend, ids, query, scoped, options = {})
155
159
  blocks.push(block)
156
160
  used += block.length + 2
157
161
  } catch (error) {
162
+ options.onSearch?.(knowledgeID, false)
158
163
  const block = neutralizeFenceClosers(`(knowledge ${oneLine(knowledgeID, 60)} failed: ${errorMessage(error)})`)
159
164
  blocks.push(block)
160
165
  used += block.length + 2