@joe-broadhead/opencode-mem 0.9.2 → 0.9.12

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 (64) hide show
  1. package/CHANGELOG.md +1605 -0
  2. package/README.md +135 -43
  3. package/docs/agent-protocol.md +29 -28
  4. package/docs/permissions.md +148 -0
  5. package/package.json +277 -16
  6. package/skills/mem.pages/SKILL.md +35 -16
  7. package/skills/mem.sharing/SKILL.md +39 -6
  8. package/skills/mem.sources/SKILL.md +41 -10
  9. package/skills/mem.usage/SKILL.md +47 -16
  10. package/src/backend/records.js +413 -50
  11. package/src/backend/v3.js +15 -10
  12. package/src/call-shapes.js +18 -25
  13. package/src/capture-pause.js +136 -0
  14. package/src/capture.js +7 -4
  15. package/src/collector-service.js +212 -35
  16. package/src/collector.js +186 -47
  17. package/src/commands.js +37 -0
  18. package/src/compaction.js +4 -2
  19. package/src/config.js +162 -1
  20. package/src/connect.js +353 -0
  21. package/src/copies.js +28 -3
  22. package/src/credentials.js +35 -7
  23. package/src/curation.js +33 -14
  24. package/src/events.js +144 -36
  25. package/src/forget-purge.js +377 -0
  26. package/src/forgotten.js +356 -0
  27. package/src/hooks.js +9 -4
  28. package/src/index.js +25 -6
  29. package/src/permissions.js +116 -38
  30. package/src/protocol.js +21 -11
  31. package/src/records.js +4 -1
  32. package/src/runtime.js +207 -16
  33. package/src/shapes.js +63 -0
  34. package/src/sources.js +183 -12
  35. package/src/spaces.js +58 -1
  36. package/src/spool.js +442 -64
  37. package/src/stated.js +334 -0
  38. package/src/storable.js +477 -0
  39. package/src/tools/aliases.js +36 -147
  40. package/src/tools/commitments.js +69 -12
  41. package/src/tools/held-write.js +9 -1
  42. package/src/tools/index.js +29 -44
  43. package/src/tools/ingest.js +33 -128
  44. package/src/tools/memory.js +594 -901
  45. package/src/tools/page-sections.js +147 -0
  46. package/src/tools/page.js +314 -82
  47. package/src/tools/recall-browse.js +173 -0
  48. package/src/tools/recall.js +79 -19
  49. package/src/tools/share.js +494 -131
  50. package/src/tools/shared.js +97 -19
  51. package/src/tools/spaces.js +215 -104
  52. package/src/tools/status.js +31 -3
  53. package/src/tools/wiki.js +5 -13
  54. package/src/util.js +10 -20
  55. package/src/views/contracts.js +137 -6
  56. package/src/views/overdue.js +125 -0
  57. package/src/views/profile.js +154 -0
  58. package/src/views/records-reads.js +132 -33
  59. package/src/views/rpc.js +63 -12
  60. package/src/views/shape.js +49 -0
  61. package/views/commitments.html +31 -4
  62. package/views/home.html +22 -3
  63. package/views/memory.html +126 -24
  64. package/src/erase.js +0 -36
@@ -5,21 +5,27 @@ import {
5
5
  FOLDERS_MAX,
6
6
  GRAPH_EDGES_MAX,
7
7
  GRAPH_NODES_MAX,
8
+ HELD_MEMORIES_MAX,
8
9
  HISTORY_MAX,
9
10
  JOURNAL_MAX,
10
11
  JOURNAL_SOURCES_MAX,
11
12
  LINEAGE_MAX,
12
13
  LINKS_MAX,
14
+ LIST_MAX,
13
15
  MEMORY_TEXT_CHARS,
14
16
  PAGES_MAX,
17
+ RECENT_CONCURRENCY,
15
18
  RECENT_MAX_OFFSET,
16
19
  RECENT_PAGE,
20
+ RECENT_SPACES_MAX,
17
21
  RECORDS_OFF,
18
22
  RECORD_BODY_CHARS,
19
23
  REVIEW_MAX,
24
+ SOURCE_CHARS,
25
+ STATUS_CHARS,
20
26
  SUMMARY_CHARS,
21
27
  } from "./contracts.js"
22
- import { SPACE_KINDS, clip, clipText, idOf, jsonBytes, refuse, spaceOf } from "./shape.js"
28
+ import { ANSWER_BYTES, SPACE_KINDS, clip, clipText, fitList, idOf, jsonBytes, mapLimit, refuse, sourceLabel, spaceOf } from "./shape.js"
23
29
 
24
30
  /**
25
31
  * The views' reads of the records service (docs/design/memory-v1-contracts.md
@@ -56,8 +62,6 @@ export const VIEW_RECORDS_METHODS = Object.freeze([
56
62
  "space.stats",
57
63
  ])
58
64
 
59
- /** Each answer is held under this many bytes of JSON, below Jelly's 64 KB cap. */
60
- const ANSWER_BYTES = 56_000
61
65
  /** A journal's body is at most this many bytes, so its sources fit beside it. */
62
66
  const JOURNAL_BODY_BYTES = 36_000
63
67
  /** The newest pages the Pages view's top level counts folders from: three reads of 200. */
@@ -185,6 +189,9 @@ export function splitJournal(body) {
185
189
  return { body: lines.join("\n"), sources, footer }
186
190
  }
187
191
 
192
+ /** Fields a writer sets about where a record came from; nothing checks them. */
193
+ const UNVERIFIED_FIELDS = new Set(["moved_from", "copied_from"])
194
+
188
195
  /** A record's fields as name and value lines, for the page's field list. */
189
196
  export function fieldLines(fields) {
190
197
  const out = []
@@ -203,7 +210,9 @@ export function fieldLines(fields) {
203
210
  for (const [name, inner] of Object.entries(fields && typeof fields === "object" ? fields : {})) {
204
211
  if (HIDDEN_FIELDS.has(name)) continue
205
212
  const text = value(inner)
206
- if (text) out.push({ name: clip(name.replaceAll("_", " "), 40), value: clip(text, 300) })
213
+ // Provenance any writer can send is shown as a claim.
214
+ const shown = UNVERIFIED_FIELDS.has(name) ? `${name.replaceAll("_", " ")} (not verified)` : name.replaceAll("_", " ")
215
+ if (text) out.push({ name: clip(shown, 40), value: clip(text, 300) })
207
216
  if (out.length >= FIELDS_MAX) break
208
217
  }
209
218
  return out
@@ -220,20 +229,6 @@ function proposedText(proposal) {
220
229
  return String(proposal?.title ?? "")
221
230
  }
222
231
 
223
- /** Keep adding items while the whole answer stays under the byte cap; whether any were left out. */
224
- function fitList(answer, key, items) {
225
- let fitted = false
226
- for (const item of items) {
227
- answer[key].push(item)
228
- if (jsonBytes(answer) > ANSWER_BYTES) {
229
- answer[key].pop()
230
- fitted = true
231
- break
232
- }
233
- }
234
- return fitted
235
- }
236
-
237
232
  /** The fact a memory record states: its body before "Why:", else its title (as `mem_recall` reads it). */
238
233
  function factOf(record) {
239
234
  const body = String(record?.body ?? "").split(/\n\s*\nWhy:/)[0].trim()
@@ -254,9 +249,11 @@ export function memoriesIn(stats) {
254
249
  }
255
250
 
256
251
  /**
257
- * @param {{ runtime: any, personalScopes: () => Promise<Array<{ context: any }>>, available: () => Promise<void> }} input
252
+ * @param {{ runtime: any, personalScopes: () => Promise<Array<{ context: any }>>, available: () => Promise<void>, describe?: (error: unknown) => string }} input
253
+ * `describe` says a failed read in words (a space that could not be read is
254
+ * reported with them); without it the records failure words, or a plain line.
258
255
  */
259
- export function createRecordsReads({ runtime, personalScopes, available }) {
256
+ export function createRecordsReads({ runtime, personalScopes, available, describe = (error) => recordsFailureWords(error) || "Memory could not answer." }) {
260
257
  /** A records call's options for one of `personalScopes()`'s entries. */
261
258
  const whereOf = (entry) => ({
262
259
  ...(entry?.scope ? { scope: entry.scope } : {}),
@@ -310,10 +307,88 @@ export function createRecordsReads({ runtime, personalScopes, available }) {
310
307
  return refuse("Name a page or a record.")
311
308
  }
312
309
 
310
+ /** The kinds of `stated_by` a row may name. */
311
+ const rowStatedBy = (value) => (["person", "agent", "inferred", "external"].includes(value) ? { stated_by: value } : {})
312
+ const rowSource = (source) => {
313
+ const label = sourceLabel(source)
314
+ return label ? { source: clip(label, SOURCE_CHARS) } : {}
315
+ }
316
+
317
+ /** A memory record as one row of the recent list. */
318
+ function memoryRow(record, space) {
319
+ return {
320
+ id: idOf(record.id),
321
+ text: clip(factOf(record), MEMORY_TEXT_CHARS),
322
+ type: clip(record.type ?? "memory", 40),
323
+ at: clip(record.updated ?? record.created ?? "", 40),
324
+ space,
325
+ status: clip(record.status || "current", STATUS_CHARS),
326
+ ...rowStatedBy(record.stated_by),
327
+ ...rowSource(record.source),
328
+ }
329
+ }
330
+
331
+ /** A proposal that would create a memory, as a row: held for review, not saved as a memory yet. */
332
+ function heldMemoryRow(proposal, space) {
333
+ const payload = proposal?.fields?.payload
334
+ const text = typeof payload?.title === "string" && payload.title.trim() ? payload.title : String(proposal?.title ?? "").replace(/^create:\s*/, "")
335
+ return {
336
+ id: idOf(proposal.id),
337
+ text: clip(text, MEMORY_TEXT_CHARS),
338
+ type: clip(payload?.type ?? "memory", 40),
339
+ at: clip(proposal.created ?? "", 40),
340
+ space,
341
+ status: "proposed",
342
+ ...rowStatedBy(proposal.stated_by),
343
+ ...rowSource(proposal.source),
344
+ }
345
+ }
346
+
347
+ /** Whether a proposal is a memory held for review (not a page, a commitment or a share). */
348
+ const isHeldMemory = (proposal) => proposal?.type === "create" && proposal?.fields?.payload?.kind === "memory" && idOf(proposal?.id) !== ""
349
+
313
350
  /**
314
- * The newest memories of the personal space, newest first: records of kind
315
- * `memory` (current and contested), 50 a page, two pages deep, as 0.8's list
316
- * of atoms was. A memory's text is the fact it states, without its "Why:".
351
+ * The spaces a recent list covers, each checked for this person: their own
352
+ * personal space, then the shared spaces control lists for them, up to
353
+ * `RECENT_SPACES_MAX`. Nothing is read in a space control does not list for
354
+ * the caller (control answers for the speaker's own token, so another
355
+ * person's spaces never appear here), and the gateway checks access again on
356
+ * every read. Another personal space is never read. Spaces past the cap are
357
+ * returned as `skipped`, to be reported, not dropped.
358
+ */
359
+ async function recentTargets(input) {
360
+ if (input?.space !== undefined) return { targets: [await target(input)], skipped: [] }
361
+ const [first] = await personalScopes()
362
+ const own = { where: whereOf(first), space: spaceOf(first?.context ?? null) }
363
+ if (!runtime.control) return { targets: [own], skipped: [] }
364
+ const listed = (await runtime.control.listSpaces({ userId: runtime.config?.user })) ?? []
365
+ const scope = first?.scope
366
+ const others = []
367
+ const seen = new Set([own.space.id])
368
+ for (const space of listed) {
369
+ const spaceId = idOf(space?.space_id)
370
+ if (!spaceId || seen.has(spaceId) || space?.kind === "personal") continue
371
+ seen.add(spaceId)
372
+ others.push({
373
+ where: { ...(scope ? { scope } : {}), space: spaceId },
374
+ space: { id: spaceId, name: clip(space.name || spaceId, 200), kind: SPACE_KINDS.has(space.kind) ? space.kind : "unknown" },
375
+ })
376
+ }
377
+ const room = Math.max(0, RECENT_SPACES_MAX - 1)
378
+ return { targets: [own, ...others.slice(0, room)], skipped: others.slice(room) }
379
+ }
380
+
381
+ /**
382
+ * The newest memories across every space the person can read, newest first:
383
+ * records of kind `memory` (current and contested), and the memories held
384
+ * for review (`status: "proposed"`). 50 a page, two pages deep, as 0.8's
385
+ * list of atoms was. A memory's text is the fact it states, without its
386
+ * "Why:", and each row names its space, who said it and where it came from
387
+ * where the record says.
388
+ *
389
+ * Each space is read on its own: one that cannot be read now is named in
390
+ * `incomplete` with the reason, and the rest are shown. Only when no space
391
+ * could be read is the call a refusal.
317
392
  */
318
393
  async function memoriesRecent(input) {
319
394
  await available()
@@ -321,17 +396,39 @@ export function createRecordsReads({ runtime, personalScopes, available }) {
321
396
  if (typeof offset !== "number" || !Number.isInteger(offset) || offset < 0 || offset > RECENT_MAX_OFFSET || offset % RECENT_PAGE !== 0) {
322
397
  refuse(`offset is a multiple of ${RECENT_PAGE} from 0 to ${RECENT_MAX_OFFSET}`)
323
398
  }
324
- const where = await target({})
325
- const answer = await records("records.list", { kind: "memory", limit: offset + RECENT_PAGE + 1 }, where.where)
326
- const rows = (answer?.records ?? []).filter((record) => idOf(record?.id))
399
+ const { targets, skipped } = await recentTargets(input)
400
+ // The newest `offset + 51` of each space hold the newest of them all.
401
+ const want = offset + RECENT_PAGE + 1
402
+ const results = await mapLimit(targets, RECENT_CONCURRENCY, async (entry) => {
403
+ const row = { id: entry.space.id, name: entry.space.name }
404
+ const [memories, held] = await Promise.allSettled([
405
+ records("records.list", { kind: "memory", limit: want }, entry.where),
406
+ records("review.list", { limit: HELD_MEMORIES_MAX }, entry.where),
407
+ ])
408
+ const rows = []
409
+ if (memories.status === "fulfilled") {
410
+ for (const record of memories.value?.records ?? []) if (idOf(record?.id)) rows.push(memoryRow(record, row))
411
+ }
412
+ if (held.status === "fulfilled") {
413
+ for (const proposal of held.value?.proposals ?? []) if (isHeldMemory(proposal)) rows.push(heldMemoryRow(proposal, row))
414
+ }
415
+ const error = [memories, held].find((read) => read.status === "rejected")?.reason
416
+ if (error) runtime.log?.warn?.(`view memories.recent: a read in space ${entry.space.id} failed`, error)
417
+ return { entry, rows, error, read: memories.status === "fulfilled" || held.status === "fulfilled" }
418
+ })
419
+ const failed = results.filter((result) => result.error !== undefined)
420
+ // Nothing could be read in any space: the first failure is the answer, in its own words.
421
+ if (!results.some((result) => result.read)) throw failed[0].error
422
+ const rows = results.flatMap((result) => result.rows).sort((a, b) => b.at.localeCompare(a.at) || b.id.localeCompare(a.id))
327
423
  return {
328
- space: where.space,
329
- items: rows.slice(offset, offset + RECENT_PAGE).map((record) => ({
330
- id: idOf(record.id),
331
- text: clip(factOf(record), MEMORY_TEXT_CHARS),
332
- type: clip(record.type ?? "memory", 40),
333
- at: clip(record.updated ?? record.created ?? "", 40),
334
- })),
424
+ space: targets[0].space,
425
+ // Every space something was read from; one that failed in part is here and in `incomplete`.
426
+ spaces: results.filter((result) => result.read).map((result) => result.entry.space),
427
+ incomplete: [
428
+ ...failed.map((result) => ({ id: result.entry.space.id, name: result.entry.space.name, reason: clip(describe(result.error), 200) })),
429
+ ...skipped.map((entry) => ({ id: entry.space.id, name: entry.space.name, reason: clip(`Not read: a list covers at most ${RECENT_SPACES_MAX} spaces. Choose this space to see its memories.`, 200) })),
430
+ ].slice(0, LIST_MAX),
431
+ items: rows.slice(offset, offset + RECENT_PAGE),
335
432
  more: offset + RECENT_PAGE <= RECENT_MAX_OFFSET && rows.length > offset + RECENT_PAGE,
336
433
  }
337
434
  }
@@ -436,11 +533,13 @@ export function createRecordsReads({ runtime, personalScopes, available }) {
436
533
  .slice(0, EVIDENCE_MAX)
437
534
  return {
438
535
  id: idOf(proposal.id),
536
+ status: clip(proposal.status || "proposed", STATUS_CHARS),
439
537
  action: clip(fields.action || proposal.type || "", 40),
440
538
  title: clip(proposal.title ?? "", 200),
441
539
  target: clip(fields.target_path || fields.target_id || "", 200),
442
540
  reason: clip(fields.reason ?? "", 300),
443
541
  stated_by: stated,
542
+ ...rowSource(proposal.source),
444
543
  external: stated === "external",
445
544
  evidence,
446
545
  proposed: clipText(proposedText(proposal), 600, 1800).text,
package/src/views/rpc.js CHANGED
@@ -10,6 +10,8 @@ import {
10
10
  VIEW_METHODS,
11
11
  } from "./contracts.js"
12
12
  import { createRecordsReads, recordsFailureWords } from "./records-reads.js"
13
+ import { oneLinePlain, overdueInboxItems } from "./overdue.js"
14
+ import { createProfileReads } from "./profile.js"
13
15
  import { SPACE_KINDS, ViewRefusal, clip, clipText, idOf, refuse, spaceOf } from "./shape.js"
14
16
  import { createHeldInboxDecision } from "./inbox-decision.js"
15
17
 
@@ -70,6 +72,7 @@ const CREDENTIALS = Object.freeze({
70
72
  [SOURCES.identity]: "jelly",
71
73
  [SOURCES.secret]: "key",
72
74
  [SOURCES.option]: "option",
75
+ [SOURCES.connect]: "opencode",
73
76
  })
74
77
 
75
78
  /**
@@ -133,6 +136,8 @@ export function createViewReads({ runtime, agents = async () => [] }) {
133
136
  */
134
137
  async function unavailableNow() {
135
138
  let reason = runtime.unavailable?.() ?? ""
139
+ // A /connect sign-in made since setup (src/connect.js): ask OpenCode once.
140
+ if (reason && (await credentials?.probeSignIn?.())) reason = runtime.unavailable?.() ?? ""
136
141
  if (!reason && inView() && typeof runtime.availability === "function") reason = await runtime.availability()
137
142
  return viewWords(reason)
138
143
  }
@@ -151,7 +156,20 @@ export function createViewReads({ runtime, agents = async () => [] }) {
151
156
  }
152
157
  }
153
158
 
154
- const recordsReads = createRecordsReads({ runtime, personalScopes, available })
159
+ const recordsReads = createRecordsReads({ runtime, personalScopes, available, describe: viewFailureWords })
160
+ const profileReads = createProfileReads({ runtime, personalScopes, available, describe: viewFailureWords })
161
+
162
+ /**
163
+ * The overview of a memory that can't be used now. `state` tells a page which
164
+ * of three things to say: nothing set up (the person has something to do in
165
+ * Settings), a credential memory turned down (the key), or a server that is
166
+ * only not answering (wait and try again, nothing to change).
167
+ */
168
+ function notConnected(reason, base, status = 0) {
169
+ const held = credentialState()
170
+ const state = overviewState({ ...held, address: base.address, status })
171
+ return { connected: false, reason: clip(reason, 400), state, ...held, ...base }
172
+ }
155
173
 
156
174
  async function overview() {
157
175
  const address = originOf(String(config.controlURL ?? "")) || originOf(String(config.baseURL ?? "").replace(/\/core$/, ""))
@@ -162,19 +180,14 @@ export function createViewReads({ runtime, agents = async () => [] }) {
162
180
  ...(address ? { address: clip(address, 300) } : {}),
163
181
  }
164
182
  const reason = await unavailableNow()
165
- if (reason) return { connected: false, reason: clip(reason, 400), ...credentialState(), ...base }
183
+ if (reason) return notConnected(reason, base)
166
184
  let scopes
167
185
  try {
168
186
  scopes = await personalScopes()
169
187
  } catch (error) {
170
188
  runtime.log?.warn?.("view overview failed", error)
171
189
  // Asked of the credential after the request, so a refusal it met shows.
172
- return {
173
- connected: false,
174
- reason: clip(viewFailureWords(error), 400),
175
- ...credentialState(),
176
- ...base,
177
- }
190
+ return notConnected(viewFailureWords(error), base, Number(error?.status) || 0)
178
191
  }
179
192
  // Memories are records from 0.9 (the space's count); conversations,
180
193
  // summaries and the profile are still the core's, per agent.
@@ -335,14 +348,35 @@ export function createViewReads({ runtime, agents = async () => [] }) {
335
348
  }
336
349
  }
337
350
 
351
+ /**
352
+ * What waits for the person, for a host that declares an Inbox (`jelly.inbox`):
353
+ * the personal space's pending proposals (`proposal:<id>`), then one item for
354
+ * each overdue commitment (`commitment:<id>`, `overdue.js`).
355
+ *
356
+ * Memory only answers: the host asks when the person reads the Inbox, so a
357
+ * host without that capability never calls this, nothing is published, and
358
+ * OpenCode keeps its Due list (`mem_commitments`, the pinned block). The host
359
+ * replaces its whole copy with each answer, so a part that cannot be read
360
+ * fails the whole read (the host then keeps what it had) rather than answer a
361
+ * list that would drop items still true.
362
+ */
338
363
  async function inboxList() {
339
- const review = await recordsReads.methods["review.list"]({})
340
- return { items: review.items.flatMap((item) => {
364
+ await available()
365
+ const [review, overdue] = await Promise.all([recordsReads.methods["review.list"]({}), overdueNotices()])
366
+ const proposals = review.items.flatMap((item) => {
341
367
  if (!/^rec_[0-9A-Za-z]{1,64}$/.test(item.id)) return []
342
368
  if (!inboxTime(item.created)) return []
343
- const title = String(item.title).replace(/[\p{Cc}\p{Cf}<>]/gu, " ").replace(/\s+/g, " ").trim().slice(0, 120)
369
+ const title = oneLinePlain(item.title, 120)
344
370
  return [{ key: `proposal:${item.id}`, category: "request", label: "Proposal", name: "Memory", line: title || "A proposal is waiting for review", at: item.created }]
345
- }) }
371
+ })
372
+ return { items: [...proposals, ...overdue] }
373
+ }
374
+
375
+ /** One item for each of the person's overdue commitments: the same rule `commitments.list` marks `overdue`. */
376
+ async function overdueNotices() {
377
+ const [first] = await personalScopes()
378
+ const records = (await runtime.records.read(first.scope)).records
379
+ return overdueInboxItems(records.items, { now: runtime.clock.now(), zone: runtime.clock.zone })
346
380
  }
347
381
 
348
382
  return {
@@ -354,6 +388,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
354
388
  "wiki.pages": wikiPages,
355
389
  "wiki.read": wikiRead,
356
390
  ...recordsReads.methods,
391
+ ...profileReads,
357
392
  }
358
393
  }
359
394
 
@@ -367,6 +402,22 @@ function viewWords(reason) {
367
402
  return reason === UNLINKED_SENTENCE && inView() ? VIEW_UNLINKED_SENTENCE : reason
368
403
  }
369
404
 
405
+ /**
406
+ * Why `memory.overview` is `connected: false`, as the page tells it: "unconfigured"
407
+ * (no credential, no address, or no linked Jelly account), "refused" (a credential
408
+ * memory or Jelly turned down: a refusal code, or a 401 or 403), otherwise
409
+ * "unreachable". Jelly's "unavailable" code is its own outage, not a refusal.
410
+ *
411
+ * @param {{ credential: string, address?: string, refused?: string, status?: number }} facts
412
+ * @returns {"unconfigured" | "refused" | "unreachable"}
413
+ */
414
+ export function overviewState({ credential, address, refused, status }) {
415
+ // Not signed in is nothing set up yet (a Jelly account to link), not a key turned down.
416
+ if (credential === "none" || !address || refused === "not_signed_in") return "unconfigured"
417
+ if ((refused && refused !== "unavailable") || status === 401 || status === 403) return "refused"
418
+ return "unreachable"
419
+ }
420
+
370
421
  /** A failed read in words; a view call's unlinked speaker in the view's words. */
371
422
  function viewFailureWords(error) {
372
423
  if (isUnlinked(error) && inView()) return VIEW_UNLINKED_SENTENCE
@@ -59,3 +59,52 @@ export function spaceOf(context) {
59
59
 
60
60
  /** UTF-8 bytes of a value as JSON. */
61
61
  export const jsonBytes = (value) => Buffer.byteLength(JSON.stringify(value))
62
+
63
+ /** Each answer is held under this many bytes of JSON, below Jelly's 64 KB cap. */
64
+ export const ANSWER_BYTES = 56_000
65
+
66
+ /**
67
+ * Keep adding `items` to `answer[key]` while the whole answer stays under the
68
+ * byte cap; whether any were left out.
69
+ */
70
+ export function fitList(answer, key, items) {
71
+ let fitted = false
72
+ for (const item of items) {
73
+ answer[key].push(item)
74
+ if (jsonBytes(answer) > ANSWER_BYTES) {
75
+ answer[key].pop()
76
+ fitted = true
77
+ break
78
+ }
79
+ }
80
+ return fitted
81
+ }
82
+
83
+ /**
84
+ * Run `work` over `list` with at most `limit` at a time, keeping the order of
85
+ * the answers. `work` is expected to settle its own failures.
86
+ */
87
+ export async function mapLimit(list, limit, work) {
88
+ const out = new Array(list.length)
89
+ let next = 0
90
+ const worker = async () => {
91
+ for (;;) {
92
+ const at = next++
93
+ if (at >= list.length) return
94
+ out[at] = await work(list[at], at)
95
+ }
96
+ }
97
+ await Promise.all(Array.from({ length: Math.min(limit, list.length) }, worker))
98
+ return out
99
+ }
100
+
101
+ /** A where-it-came-from line for a record's `source` ({ event, turn, session, routine, record, url }), or "". */
102
+ export function sourceLabel(source) {
103
+ if (!source || typeof source !== "object") return ""
104
+ for (const key of ["event", "turn", "session", "routine", "record"]) {
105
+ const ref = typeof source[key] === "string" ? source[key].trim() : ""
106
+ if (ref && /^[\w.:@/-]{1,120}$/.test(ref)) return `${key} ${ref}`
107
+ }
108
+ const url = typeof source.url === "string" ? source.url.trim() : ""
109
+ return /^https?:\/\/[^\s<>"']{1,190}$/.test(url) ? `url ${url}` : ""
110
+ }
@@ -2,7 +2,7 @@
2
2
  <html lang="en-GB">
3
3
  <head>
4
4
  <meta charset="utf-8">
5
- <meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'sha256-5dtPKkjegOPG8bawdSzGBjixnIaSCLYcZOUAwbRWFAc=' 'sha256-ok3CmSN9UFtjvZkYSjfg8g06g5+3ayhEW8txJ9m7sXw='; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'">
5
+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'sha256-5dtPKkjegOPG8bawdSzGBjixnIaSCLYcZOUAwbRWFAc=' 'sha256-EAE0IDrikWE0chPIVODVlKBvBg1sDxCO9UEtFartZJY=' 'sha256-1S0YL3xJhJNv3HZDaqKXlt343g7Vx93lOVeHtK/Fts8='; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'">
6
6
  <title>Commitments</title>
7
7
  <style>
8
8
  :root {
@@ -124,6 +124,33 @@ function jellyView() {
124
124
  }
125
125
  </script>
126
126
  <script>
127
+ // done-message.js: the message "Mark done" drafts, for Memory's Commitments views. Paste it into
128
+ // the page's own <script>, as jelly-view.js is. The person reads and sends this text, so it holds
129
+ // no record id: "Mark 'Send the Q4 numbers' as done." The agent finds the commitment by its
130
+ // words (mem_commitments list). Only when another open commitment reads the same (spacing and
131
+ // case aside) does it add what tells them apart, in this order: the due date, the owner, the
132
+ // trigger. Only when two still match exactly does it fall back to the id.
133
+ function doneMessage(item, others) {
134
+ const words = (value) => String(value ?? "").replace(/\s+/g, " ").trim();
135
+ const fold = (value) => words(value).toLowerCase();
136
+ const said = words(item.what);
137
+ const same = (others || []).filter((other) => other !== item && other.id !== item.id && other.state === "open" && fold(other.what) === fold(item.what));
138
+ const draft = (detail) => `Mark '${said}'${detail ? ` (${detail})` : ""} as done.`;
139
+ if (same.length === 0) return draft("");
140
+ const owner = (value) => (value === "user" ? "yours" : value === "agent" ? "your agent's" : `waiting on ${words(value)}`);
141
+ const kinds = [
142
+ [(one) => words(one.when), (one) => `due ${words(one.when)}`],
143
+ [(one) => fold(one.owner), (one) => owner(one.owner)],
144
+ [(one) => fold(one.trigger), (one) => `when ${words(one.trigger)}`],
145
+ ];
146
+ for (const [read, say] of kinds) {
147
+ const mine = read(item);
148
+ if (mine && same.every((other) => read(other) !== mine)) return draft(say(item));
149
+ }
150
+ return `Mark '${said}' as done (${item.id}).`;
151
+ }
152
+ </script>
153
+ <script>
127
154
  // Memory's Commitments card: what you promised, what your agent promised, and
128
155
  // what you're waiting on, overdue first. Nothing is fetched; the list comes
129
156
  // from the plugin over the bridge. Marking one done starts a message for the
@@ -169,7 +196,7 @@ function dueWords(item) {
169
196
  if (item.when) return item.when;
170
197
  return item.trigger ? `${item.trigger.charAt(0).toUpperCase()}${item.trigger.slice(1)}` : "No date";
171
198
  }
172
- function row(item, status) {
199
+ function row(item, status, all) {
173
200
  const line = el("li");
174
201
  const words = el("div", "grow");
175
202
  words.append(el("div", "", item.what), el("div", item.overdue ? "when overdue" : "when", `${dueWords(item)} · ${ownerWords(item.owner)}`));
@@ -178,7 +205,7 @@ function row(item, status) {
178
205
  done.setAttribute("aria-label", `Mark done: ${item.what.slice(0, 60)}`);
179
206
  done.addEventListener("click", async () => {
180
207
  try {
181
- await view.openAgent(`Mark '${item.what.replace(/\s+/g, " ").trim()}' as done (${item.id}).`);
208
+ await view.openAgent(doneMessage(item, all));
182
209
  status.textContent = "Your message is ready. Send it, and the agent marks this as done.";
183
210
  } catch (error) {
184
211
  status.textContent = `Couldn't start the message: ${error.message}`;
@@ -208,7 +235,7 @@ async function draw() {
208
235
  const status = el("p", "muted");
209
236
  status.setAttribute("role", "status");
210
237
  const list = el("ul");
211
- list.append(...answer.items.slice(0, SHOWN).map((item) => row(item, status)));
238
+ list.append(...answer.items.slice(0, SHOWN).map((item) => row(item, status, answer.items)));
212
239
  const head = overdue > 0
213
240
  ? state(`${overdue} overdue, ${answer.items.length} open`, "danger")
214
241
  : state(`${answer.items.length} open · today is ${answer.today}`, "");
package/views/home.html CHANGED
@@ -2,7 +2,7 @@
2
2
  <html lang="en-GB">
3
3
  <head>
4
4
  <meta charset="utf-8">
5
- <meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'sha256-5dtPKkjegOPG8bawdSzGBjixnIaSCLYcZOUAwbRWFAc=' 'sha256-vhQuH/EWSlGque8dkQL2uHtrgUCB4sWo+YfdgAuixMI='; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'">
5
+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'sha256-5dtPKkjegOPG8bawdSzGBjixnIaSCLYcZOUAwbRWFAc=' 'sha256-22O46M2mU2RIOdoFEj4fhYvcXdOjuNJ87aUdLWSkqio='; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'">
6
6
  <title>Memory</title>
7
7
  <style>
8
8
  :root {
@@ -150,6 +150,11 @@ function show(...nodes) {
150
150
  const RECORDS_OFF = "This Memory server has no records service yet, so memories, journals, pages and review can't be shown. Ask whoever runs it to turn it on.";
151
151
  const count = (value, one, many) => `${value.toLocaleString("en-GB")} ${value === 1 ? one : many}`;
152
152
 
153
+ // The reason, without repeating the headline it sits under.
154
+ function unreachableDetail(overview) {
155
+ return String(overview.reason || "").replace(/^\s*memory\s+can['\u2019]t\s+be\s+reached\s+just\s+now\s*[.:!]?\s*/i, "") || "Try again in a moment.";
156
+ }
157
+
153
158
  async function draw() {
154
159
  tile.setAttribute("aria-busy", "true");
155
160
  let overview;
@@ -161,11 +166,25 @@ async function draw() {
161
166
  retry.addEventListener("click", draw);
162
167
  return show(state("Couldn't read memory", "danger"), el("p", "muted", error.message), retry);
163
168
  }
164
- if (!overview.connected)
169
+ if (!overview.connected) {
170
+ // Memory set up and not answering is only that: nothing to change in Settings, so a retry, as Commitments has.
171
+ if (overview.state === "unreachable") {
172
+ const retry = el("button", "button", "Try again");
173
+ retry.type = "button";
174
+ retry.addEventListener("click", draw);
175
+ return show(state("Memory can't be reached just now", "danger"), el("p", "muted", unreachableDetail(overview)), retry);
176
+ }
165
177
  return show(
166
178
  state("Not connected", "off"),
167
- el("p", "muted", "Open Settings, then Plugins, then Memory, to connect it."),
179
+ el(
180
+ "p",
181
+ "muted",
182
+ overview.state === "refused"
183
+ ? "Memory refused the key. Open Settings, then Plugins, then Memory, to check it."
184
+ : "Open Settings, then Plugins, then Memory, to connect it.",
185
+ ),
168
186
  );
187
+ }
169
188
  const counts = overview.counts ?? { memories: 0, conversations: 0 };
170
189
  const lines = [state(overview.capture ? "Remembering" : "Not saving conversations", overview.capture ? "" : "off")];
171
190
  // Memories are records from 0.9: a server without the records service says so in one line.