@joe-broadhead/opencode-mem 0.9.2 → 0.9.5

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.
@@ -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. */
@@ -220,20 +224,6 @@ function proposedText(proposal) {
220
224
  return String(proposal?.title ?? "")
221
225
  }
222
226
 
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
227
  /** The fact a memory record states: its body before "Why:", else its title (as `mem_recall` reads it). */
238
228
  function factOf(record) {
239
229
  const body = String(record?.body ?? "").split(/\n\s*\nWhy:/)[0].trim()
@@ -254,9 +244,11 @@ export function memoriesIn(stats) {
254
244
  }
255
245
 
256
246
  /**
257
- * @param {{ runtime: any, personalScopes: () => Promise<Array<{ context: any }>>, available: () => Promise<void> }} input
247
+ * @param {{ runtime: any, personalScopes: () => Promise<Array<{ context: any }>>, available: () => Promise<void>, describe?: (error: unknown) => string }} input
248
+ * `describe` says a failed read in words (a space that could not be read is
249
+ * reported with them); without it the records failure words, or a plain line.
258
250
  */
259
- export function createRecordsReads({ runtime, personalScopes, available }) {
251
+ export function createRecordsReads({ runtime, personalScopes, available, describe = (error) => recordsFailureWords(error) || "Memory could not answer." }) {
260
252
  /** A records call's options for one of `personalScopes()`'s entries. */
261
253
  const whereOf = (entry) => ({
262
254
  ...(entry?.scope ? { scope: entry.scope } : {}),
@@ -310,10 +302,88 @@ export function createRecordsReads({ runtime, personalScopes, available }) {
310
302
  return refuse("Name a page or a record.")
311
303
  }
312
304
 
305
+ /** The kinds of `stated_by` a row may name. */
306
+ const rowStatedBy = (value) => (["person", "agent", "inferred", "external"].includes(value) ? { stated_by: value } : {})
307
+ const rowSource = (source) => {
308
+ const label = sourceLabel(source)
309
+ return label ? { source: clip(label, SOURCE_CHARS) } : {}
310
+ }
311
+
312
+ /** A memory record as one row of the recent list. */
313
+ function memoryRow(record, space) {
314
+ return {
315
+ id: idOf(record.id),
316
+ text: clip(factOf(record), MEMORY_TEXT_CHARS),
317
+ type: clip(record.type ?? "memory", 40),
318
+ at: clip(record.updated ?? record.created ?? "", 40),
319
+ space,
320
+ status: clip(record.status || "current", STATUS_CHARS),
321
+ ...rowStatedBy(record.stated_by),
322
+ ...rowSource(record.source),
323
+ }
324
+ }
325
+
326
+ /** A proposal that would create a memory, as a row: held for review, not saved as a memory yet. */
327
+ function heldMemoryRow(proposal, space) {
328
+ const payload = proposal?.fields?.payload
329
+ const text = typeof payload?.title === "string" && payload.title.trim() ? payload.title : String(proposal?.title ?? "").replace(/^create:\s*/, "")
330
+ return {
331
+ id: idOf(proposal.id),
332
+ text: clip(text, MEMORY_TEXT_CHARS),
333
+ type: clip(payload?.type ?? "memory", 40),
334
+ at: clip(proposal.created ?? "", 40),
335
+ space,
336
+ status: "proposed",
337
+ ...rowStatedBy(proposal.stated_by),
338
+ ...rowSource(proposal.source),
339
+ }
340
+ }
341
+
342
+ /** Whether a proposal is a memory held for review (not a page, a commitment or a share). */
343
+ const isHeldMemory = (proposal) => proposal?.type === "create" && proposal?.fields?.payload?.kind === "memory" && idOf(proposal?.id) !== ""
344
+
313
345
  /**
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:".
346
+ * The spaces a recent list covers, each checked for this person: their own
347
+ * personal space, then the shared spaces control lists for them, up to
348
+ * `RECENT_SPACES_MAX`. Nothing is read in a space control does not list for
349
+ * the caller (control answers for the speaker's own token, so another
350
+ * person's spaces never appear here), and the gateway checks access again on
351
+ * every read. Another personal space is never read. Spaces past the cap are
352
+ * returned as `skipped`, to be reported, not dropped.
353
+ */
354
+ async function recentTargets(input) {
355
+ if (input?.space !== undefined) return { targets: [await target(input)], skipped: [] }
356
+ const [first] = await personalScopes()
357
+ const own = { where: whereOf(first), space: spaceOf(first?.context ?? null) }
358
+ if (!runtime.control) return { targets: [own], skipped: [] }
359
+ const listed = (await runtime.control.listSpaces({ userId: runtime.config?.user })) ?? []
360
+ const scope = first?.scope
361
+ const others = []
362
+ const seen = new Set([own.space.id])
363
+ for (const space of listed) {
364
+ const spaceId = idOf(space?.space_id)
365
+ if (!spaceId || seen.has(spaceId) || space?.kind === "personal") continue
366
+ seen.add(spaceId)
367
+ others.push({
368
+ where: { ...(scope ? { scope } : {}), space: spaceId },
369
+ space: { id: spaceId, name: clip(space.name || spaceId, 200), kind: SPACE_KINDS.has(space.kind) ? space.kind : "unknown" },
370
+ })
371
+ }
372
+ const room = Math.max(0, RECENT_SPACES_MAX - 1)
373
+ return { targets: [own, ...others.slice(0, room)], skipped: others.slice(room) }
374
+ }
375
+
376
+ /**
377
+ * The newest memories across every space the person can read, newest first:
378
+ * records of kind `memory` (current and contested), and the memories held
379
+ * for review (`status: "proposed"`). 50 a page, two pages deep, as 0.8's
380
+ * list of atoms was. A memory's text is the fact it states, without its
381
+ * "Why:", and each row names its space, who said it and where it came from
382
+ * where the record says.
383
+ *
384
+ * Each space is read on its own: one that cannot be read now is named in
385
+ * `incomplete` with the reason, and the rest are shown. Only when no space
386
+ * could be read is the call a refusal.
317
387
  */
318
388
  async function memoriesRecent(input) {
319
389
  await available()
@@ -321,17 +391,39 @@ export function createRecordsReads({ runtime, personalScopes, available }) {
321
391
  if (typeof offset !== "number" || !Number.isInteger(offset) || offset < 0 || offset > RECENT_MAX_OFFSET || offset % RECENT_PAGE !== 0) {
322
392
  refuse(`offset is a multiple of ${RECENT_PAGE} from 0 to ${RECENT_MAX_OFFSET}`)
323
393
  }
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))
394
+ const { targets, skipped } = await recentTargets(input)
395
+ // The newest `offset + 51` of each space hold the newest of them all.
396
+ const want = offset + RECENT_PAGE + 1
397
+ const results = await mapLimit(targets, RECENT_CONCURRENCY, async (entry) => {
398
+ const row = { id: entry.space.id, name: entry.space.name }
399
+ const [memories, held] = await Promise.allSettled([
400
+ records("records.list", { kind: "memory", limit: want }, entry.where),
401
+ records("review.list", { limit: HELD_MEMORIES_MAX }, entry.where),
402
+ ])
403
+ const rows = []
404
+ if (memories.status === "fulfilled") {
405
+ for (const record of memories.value?.records ?? []) if (idOf(record?.id)) rows.push(memoryRow(record, row))
406
+ }
407
+ if (held.status === "fulfilled") {
408
+ for (const proposal of held.value?.proposals ?? []) if (isHeldMemory(proposal)) rows.push(heldMemoryRow(proposal, row))
409
+ }
410
+ const error = [memories, held].find((read) => read.status === "rejected")?.reason
411
+ if (error) runtime.log?.warn?.(`view memories.recent: a read in space ${entry.space.id} failed`, error)
412
+ return { entry, rows, error, read: memories.status === "fulfilled" || held.status === "fulfilled" }
413
+ })
414
+ const failed = results.filter((result) => result.error !== undefined)
415
+ // Nothing could be read in any space: the first failure is the answer, in its own words.
416
+ if (!results.some((result) => result.read)) throw failed[0].error
417
+ const rows = results.flatMap((result) => result.rows).sort((a, b) => b.at.localeCompare(a.at) || b.id.localeCompare(a.id))
327
418
  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
- })),
419
+ space: targets[0].space,
420
+ // Every space something was read from; one that failed in part is here and in `incomplete`.
421
+ spaces: results.filter((result) => result.read).map((result) => result.entry.space),
422
+ incomplete: [
423
+ ...failed.map((result) => ({ id: result.entry.space.id, name: result.entry.space.name, reason: clip(describe(result.error), 200) })),
424
+ ...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) })),
425
+ ].slice(0, LIST_MAX),
426
+ items: rows.slice(offset, offset + RECENT_PAGE),
335
427
  more: offset + RECENT_PAGE <= RECENT_MAX_OFFSET && rows.length > offset + RECENT_PAGE,
336
428
  }
337
429
  }
@@ -436,11 +528,13 @@ export function createRecordsReads({ runtime, personalScopes, available }) {
436
528
  .slice(0, EVIDENCE_MAX)
437
529
  return {
438
530
  id: idOf(proposal.id),
531
+ status: clip(proposal.status || "proposed", STATUS_CHARS),
439
532
  action: clip(fields.action || proposal.type || "", 40),
440
533
  title: clip(proposal.title ?? "", 200),
441
534
  target: clip(fields.target_path || fields.target_id || "", 200),
442
535
  reason: clip(fields.reason ?? "", 300),
443
536
  stated_by: stated,
537
+ ...rowSource(proposal.source),
444
538
  external: stated === "external",
445
539
  evidence,
446
540
  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.