@joe-broadhead/opencode-mem 0.8.1 → 0.8.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.
@@ -19,6 +19,15 @@
19
19
  * `fromJsonSchemaDocument`) refuses to register a schema that has one ("Patterns
20
20
  * may block validation and are disabled"). The handlers check ids themselves
21
21
  * (`ID_PATTERN`) and refuse a bad one in words.
22
+ *
23
+ * **Who asked (contract v1.5 §6.1).** Every method's input names the optional
24
+ * string `jellySpeaker`. A Jelly host of v1.5 or later (Jelly 0.3.3) sees it in
25
+ * the declared schema and fills it, per call, with a speaker id naming the
26
+ * person whose device made the call; the handler asks for that person's token
27
+ * with it and strips it before anything else reads the input
28
+ * (`src/views/rpc.js`). It is never `required`: a host before v1.5 sends
29
+ * nothing there, and the call is answered as before. A page cannot set it: the
30
+ * host refuses a page's input that carries it.
22
31
  */
23
32
 
24
33
  /** A memory, space, wiki or page id as the views pass it back: visible ASCII, bounded. */
@@ -38,7 +47,24 @@ export const LIST_MAX = 100
38
47
  /** The most commitments one answer lists (the Home card shows fewer). */
39
48
  export const COMMITMENTS_MAX = 100
40
49
 
41
- const empty = { type: "object", additionalProperties: false, properties: {} }
50
+ /** The input property a Jelly host of contract v1.5 or later fills with the caller's speaker id. */
51
+ export const SPEAKER_FIELD = "jellySpeaker"
52
+
53
+ /**
54
+ * A method's input: its own properties and the optional `jellySpeaker`, which
55
+ * is never required.
56
+ *
57
+ * @param {Record<string, object>} [properties]
58
+ * @param {string[]} [required]
59
+ */
60
+ const viewInput = (properties = {}, required = []) => ({
61
+ type: "object",
62
+ additionalProperties: false,
63
+ ...(required.length > 0 ? { required } : {}),
64
+ properties: { ...properties, [SPEAKER_FIELD]: { type: "string" } },
65
+ })
66
+
67
+ const empty = viewInput()
42
68
  const id = { type: "string", maxLength: 200 }
43
69
  const line = (max) => ({ type: "string", maxLength: max })
44
70
  const space = {
@@ -102,11 +128,9 @@ const spacesOutput = {
102
128
  },
103
129
  }
104
130
 
105
- const recentInput = {
106
- type: "object",
107
- additionalProperties: false,
108
- properties: { offset: { type: "integer", minimum: 0, maximum: RECENT_MAX_OFFSET, multipleOf: RECENT_PAGE } },
109
- }
131
+ const recentInput = viewInput({
132
+ offset: { type: "integer", minimum: 0, maximum: RECENT_MAX_OFFSET, multipleOf: RECENT_PAGE },
133
+ })
110
134
 
111
135
  const recentOutput = {
112
136
  type: "object",
@@ -178,14 +202,10 @@ const wikiReadOutput = {
178
202
  properties: { ref: id, content: line(PAGE_TEXT_CHARS), clipped: { type: "boolean" } },
179
203
  }
180
204
 
181
- const commitmentsInput = {
182
- type: "object",
183
- additionalProperties: false,
184
- properties: {
185
- /** open (default): what is still to do; closed: done or cancelled, newest first. */
186
- state: { type: "string", enum: ["open", "closed"] },
187
- },
188
- }
205
+ const commitmentsInput = viewInput({
206
+ /** open (default): what is still to do; closed: done or cancelled, newest first. */
207
+ state: { type: "string", enum: ["open", "closed"] },
208
+ })
189
209
 
190
210
  const commitmentsOutput = {
191
211
  type: "object",
@@ -247,24 +267,14 @@ export const VIEW_METHODS = Object.freeze([
247
267
  name: "wiki.pages",
248
268
  label: "list a wiki's pages",
249
269
  permission: "mem_wiki",
250
- input: {
251
- type: "object",
252
- additionalProperties: false,
253
- required: ["wiki"],
254
- properties: { wiki: id },
255
- },
270
+ input: viewInput({ wiki: id }, ["wiki"]),
256
271
  output: wikiPagesOutput,
257
272
  },
258
273
  {
259
274
  name: "wiki.read",
260
275
  label: "read a wiki page",
261
276
  permission: "mem_wiki",
262
- input: {
263
- type: "object",
264
- additionalProperties: false,
265
- required: ["wiki", "ref"],
266
- properties: { wiki: id, ref: id },
267
- },
277
+ input: viewInput({ wiki: id, ref: id }, ["wiki", "ref"]),
268
278
  output: wikiReadOutput,
269
279
  },
270
280
  {
package/src/views/rpc.js CHANGED
@@ -1,4 +1,4 @@
1
- import { originOf, PACKAGE_NAME, SOURCES } from "../credentials.js"
1
+ import { UNLINKED_SENTENCE, asView, inView, isUnlinked, originOf, PACKAGE_NAME, SOURCES } from "../credentials.js"
2
2
  import { dayIn, dueDay, filterItems, formatDay, formatDue } from "../records.js"
3
3
  import { isRecordsWikiName } from "../tools/knowledge.js"
4
4
  import { oneLine } from "../util.js"
@@ -10,6 +10,7 @@ import {
10
10
  PAGE_TEXT_CHARS,
11
11
  RECENT_MAX_OFFSET,
12
12
  RECENT_PAGE,
13
+ SPEAKER_FIELD,
13
14
  VIEW_ERROR,
14
15
  VIEW_METHODS,
15
16
  } from "./contracts.js"
@@ -34,8 +35,20 @@ import {
34
35
  * Jelly checks each call's input and answer against the declared schemas; the
35
36
  * handlers check the input again and clip every answer to them, so a long row
36
37
  * is shortened here rather than refused there.
38
+ *
39
+ * **Whose memory (0.8.3, contract v1.5 §6.1).** A Jelly host of v1.5 or later
40
+ * sends each call a speaker id in `jellySpeaker`, naming the person whose
41
+ * device made it. The handler takes it out of the input before the read sees
42
+ * the input, and runs the read in `asView(id)`: every request of that call
43
+ * carries that person's own token, asked for the call and dropped with it
44
+ * (`src/credentials.js`). A person who has not linked a Jelly account on this
45
+ * host gets `VIEW_UNLINKED_SENTENCE` and the call sends nothing. With no id (a
46
+ * host before v1.5) the call reads as it did in 0.8.2.
37
47
  */
38
48
 
49
+ /** What a view says to a person who opened it without a Jelly account linked on this host. */
50
+ export const VIEW_UNLINKED_SENTENCE = "Memory is unavailable: you have not linked a Jelly account on this host."
51
+
39
52
  /** Primary agents read per call; the rest are left out. */
40
53
  const MAX_AGENTS = 4
41
54
  /** The engine's agent list is read again after this long. */
@@ -151,9 +164,21 @@ export function createViewReads({ runtime, agents = async () => [] }) {
151
164
  return scopes
152
165
  }
153
166
 
167
+ /**
168
+ * Why memory is unavailable for this call, or "". In a view call that names
169
+ * its speaker, Jelly is asked for that person's token first (`availability`),
170
+ * so an unlinked person is known before any request is sent; without one
171
+ * this knows only what Jelly already answered, as in 0.8.2.
172
+ */
173
+ async function unavailableNow() {
174
+ let reason = runtime.unavailable?.() ?? ""
175
+ if (!reason && inView() && typeof runtime.availability === "function") reason = await runtime.availability()
176
+ return viewWords(reason)
177
+ }
178
+
154
179
  /** Refuse before any request while memory is unavailable. */
155
- function available() {
156
- const reason = runtime.unavailable?.() ?? ""
180
+ async function available() {
181
+ const reason = await unavailableNow()
157
182
  if (reason) refuse(reason)
158
183
  }
159
184
 
@@ -172,7 +197,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
172
197
  health: "unknown",
173
198
  ...(address ? { address: clip(address, 300) } : {}),
174
199
  }
175
- const reason = runtime.unavailable?.() ?? ""
200
+ const reason = await unavailableNow()
176
201
  if (reason) return { connected: false, reason: clip(reason, 400), ...credentialState(), ...base }
177
202
  let scopes
178
203
  try {
@@ -182,7 +207,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
182
207
  // Asked of the credential after the request, so a refusal it met shows.
183
208
  return {
184
209
  connected: false,
185
- reason: clip(failureWords(error), 400),
210
+ reason: clip(viewFailureWords(error), 400),
186
211
  ...credentialState(),
187
212
  ...base,
188
213
  }
@@ -222,7 +247,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
222
247
  }
223
248
 
224
249
  async function spacesList() {
225
- available()
250
+ await available()
226
251
  if (!runtime.control) return { spaces: [{ ...spaceOf(null), role: "owner" }] }
227
252
  const spaces = await runtime.control.listSpaces({ userId: config.user })
228
253
  return {
@@ -245,7 +270,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
245
270
  * which is as deep as the screen goes (two pages, `RECENT_MAX_OFFSET`).
246
271
  */
247
272
  async function recent(input) {
248
- available()
273
+ await available()
249
274
  const offset = input?.offset ?? 0
250
275
  if (typeof offset !== "number" || !Number.isInteger(offset) || offset < 0 || offset > RECENT_MAX_OFFSET || offset % RECENT_PAGE !== 0) {
251
276
  refuse(`offset is a multiple of ${RECENT_PAGE} from 0 to ${RECENT_MAX_OFFSET}`)
@@ -284,7 +309,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
284
309
  }
285
310
 
286
311
  async function wikiList() {
287
- available()
312
+ await available()
288
313
  const scope = await knowledgeScope()
289
314
  const data = await backend.listWiki({ scope })
290
315
  return {
@@ -312,7 +337,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
312
337
  }
313
338
 
314
339
  async function wikiPages(input) {
315
- available()
340
+ await available()
316
341
  const scope = await knowledgeScope()
317
342
  const wiki = await visibleWiki(scope, input?.wiki)
318
343
  const data = await backend.listWikiPages({ scope, wikiID: wiki })
@@ -325,7 +350,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
325
350
  }
326
351
 
327
352
  async function wikiRead(input) {
328
- available()
353
+ await available()
329
354
  const scope = await knowledgeScope()
330
355
  const wiki = await visibleWiki(scope, input?.wiki)
331
356
  const ref = idOf(input?.ref)
@@ -344,7 +369,7 @@ export function createViewReads({ runtime, agents = async () => [] }) {
344
369
  * space is read; a view can change nothing (closing one opens the agent).
345
370
  */
346
371
  async function commitmentsList(input) {
347
- available()
372
+ await available()
348
373
  const state = input?.state === "closed" ? "closed" : "open"
349
374
  const [first] = await personalScopes()
350
375
  const records = (await runtime.records.read(first.scope)).records
@@ -388,6 +413,21 @@ export function createViewReads({ runtime, agents = async () => [] }) {
388
413
  }
389
414
  }
390
415
 
416
+ /**
417
+ * The unlinked speaker's sentence in a view's own words when the call names
418
+ * its speaker; any other reason unchanged.
419
+ *
420
+ * @param {string} reason
421
+ */
422
+ function viewWords(reason) {
423
+ return reason === UNLINKED_SENTENCE && inView() ? VIEW_UNLINKED_SENTENCE : reason
424
+ }
425
+
426
+ /** A failed read in words; a view call's unlinked speaker in the view's words. */
427
+ function viewFailureWords(error) {
428
+ return isUnlinked(error) && inView() ? VIEW_UNLINKED_SENTENCE : failureWords(error)
429
+ }
430
+
391
431
  /**
392
432
  * A failed read in words a person can act on; the detail goes to the log.
393
433
  * `status` is the HTTP status, 0 when memory could not be reached and 408 for
@@ -425,6 +465,19 @@ export function engineAgents(ctx, now = Date.now) {
425
465
  }
426
466
  }
427
467
 
468
+ /**
469
+ * A view call's input split into the speaker id the host put in `jellySpeaker`
470
+ * (`undefined` when there is none) and the page's own input without it: the
471
+ * only input a read, a request or a log ever sees.
472
+ *
473
+ * @param {unknown} input
474
+ * @returns {{ speaker: unknown, rest: Record<string, unknown> }}
475
+ */
476
+ export function splitSpeaker(input) {
477
+ const { [SPEAKER_FIELD]: speaker, ...rest } = input && typeof input === "object" && !Array.isArray(input) ? input : {}
478
+ return { speaker, rest }
479
+ }
480
+
428
481
  /**
429
482
  * Register the views' RPC with the engine, under the package name: the only id
430
483
  * Jelly calls for this package's views. A host without `ctx.rpc` (OpenCode
@@ -434,6 +487,10 @@ export function engineAgents(ctx, now = Date.now) {
434
487
  * words; on the pinned engine a thrown error would reach it only as "RPC call
435
488
  * failed".
436
489
  *
490
+ * `jellySpeaker` is taken out of the input here, before the read, the log or a
491
+ * request can see it, and the read runs as that speaker (`asView`); a call
492
+ * without it runs exactly as before.
493
+ *
437
494
  * @param {any} ctx
438
495
  * @param {any} runtime
439
496
  */
@@ -445,13 +502,17 @@ export async function registerViewRpc(ctx, runtime) {
445
502
  for (const method of VIEW_METHODS) {
446
503
  methods[method.name] = { input: method.input, output: method.output, errors: { [VIEW_ERROR]: { type: "object" } } }
447
504
  handlers[method.name] = async (input, call) => {
448
- try {
449
- return await reads[method.name](input ?? {})
450
- } catch (error) {
451
- if (!(error instanceof ViewRefusal)) runtime.log?.warn?.(`view read ${method.name} failed`, error)
452
- const words = error instanceof ViewRefusal ? error.message : failureWords(error)
453
- return call.error(VIEW_ERROR, clip(words, 200), {})
505
+ const { speaker, rest } = splitSpeaker(input)
506
+ const read = async () => {
507
+ try {
508
+ return await reads[method.name](rest)
509
+ } catch (error) {
510
+ if (!(error instanceof ViewRefusal)) runtime.log?.warn?.(`view read ${method.name} failed`, error)
511
+ const words = error instanceof ViewRefusal ? error.message : viewFailureWords(error)
512
+ return call.error(VIEW_ERROR, clip(words, 200), {})
513
+ }
454
514
  }
515
+ return typeof speaker === "string" ? asView(speaker, read) : read()
455
516
  }
456
517
  }
457
518
  return ctx.rpc.register({ id: PACKAGE_NAME, events: {}, methods }, handlers)