@joe-broadhead/opencode-mem 0.8.0 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1340 -0
- package/README.md +56 -26
- package/SECURITY.md +1 -1
- package/package.json +11 -7
- package/skills/{mem-usage → mem.usage}/SKILL.md +4 -1
- package/src/backend/v3.js +20 -5
- package/src/call-shapes.js +1 -0
- package/src/commands.js +8 -5
- package/src/credentials.js +413 -78
- package/src/errors.js +89 -0
- package/src/events.js +63 -20
- package/src/hooks.js +29 -17
- package/src/index.js +15 -3
- package/src/permissions.js +67 -0
- package/src/runtime.js +66 -7
- package/src/skills.js +1 -1
- package/src/spaces.js +41 -19
- package/src/spool.js +91 -4
- package/src/tools/commitments.js +2 -1
- package/src/tools/index.js +10 -5
- package/src/tools/memory.js +35 -4
- package/src/tools/shared.js +3 -1
- package/views/memory.html +2 -2
package/src/spaces.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { errorMessage, isPlainObject, oneLine } from "./util.js"
|
|
2
|
+
import { codedMessage, codedParts, failureFor } from "./errors.js"
|
|
2
3
|
|
|
3
4
|
const DEFAULT_TIMEOUT_MS = 10_000
|
|
4
5
|
|
|
@@ -11,15 +12,31 @@ export class ControlError extends Error {
|
|
|
11
12
|
* @param {string} operation
|
|
12
13
|
* @param {number} status
|
|
13
14
|
* @param {string} message
|
|
15
|
+
* @param {{ cause?: unknown, errorCode?: string, next?: string }} [options]
|
|
16
|
+
* `cause`: a credential error whose code and next step this one keeps;
|
|
17
|
+
* `errorCode` and `next`: words for a failure the status does not name.
|
|
14
18
|
*/
|
|
15
|
-
constructor(operation, status, message) {
|
|
16
|
-
|
|
19
|
+
constructor(operation, status, message, options = {}) {
|
|
20
|
+
const inner = codedParts(options.cause)
|
|
21
|
+
const failure = inner ?? { ...failureFor(status, "SPACES"), ...(options.errorCode ? { code: options.errorCode } : {}), ...(options.next ? { next: options.next } : {}) }
|
|
22
|
+
const what = `${operation}: ${inner ? inner.what : message}`
|
|
23
|
+
super(codedMessage(failure.code, what, failure.next))
|
|
17
24
|
this.name = "ControlError"
|
|
18
25
|
this.operation = operation
|
|
19
26
|
this.status = status
|
|
27
|
+
this.errorCode = failure.code
|
|
28
|
+
this.what = what
|
|
29
|
+
this.next = failure.next
|
|
20
30
|
}
|
|
21
31
|
}
|
|
22
32
|
|
|
33
|
+
/** A required value the caller did not pass: a fault in the call, not in the service. */
|
|
34
|
+
const missing = (operation, what) =>
|
|
35
|
+
new ControlError(operation, 0, `${what} is required`, { errorCode: "SPACES_INPUT_MISSING", next: `pass ${what}, then call again` })
|
|
36
|
+
|
|
37
|
+
/** The control plane answered without what the plugin needs to place the request. */
|
|
38
|
+
const NO_PLACEMENT = { errorCode: "SPACES_NO_PLACEMENT", next: "try again in a moment; if it keeps failing, tell the Memory server's operator" }
|
|
39
|
+
|
|
23
40
|
/**
|
|
24
41
|
* A small client for the spaces control plane.
|
|
25
42
|
*
|
|
@@ -57,7 +74,7 @@ export function createControlClient(input) {
|
|
|
57
74
|
try {
|
|
58
75
|
return await credentials.bearer()
|
|
59
76
|
} catch (error) {
|
|
60
|
-
throw new ControlError(operation, Number(error?.status) || 0, oneLine(errorMessage(error)))
|
|
77
|
+
throw new ControlError(operation, Number(error?.status) || 0, oneLine(errorMessage(error)), { cause: error })
|
|
61
78
|
}
|
|
62
79
|
}
|
|
63
80
|
|
|
@@ -78,7 +95,12 @@ export function createControlClient(input) {
|
|
|
78
95
|
*/
|
|
79
96
|
async function request(operation, method, route, options = {}) {
|
|
80
97
|
const controlURL = controlURLOf()
|
|
81
|
-
if (!controlURL)
|
|
98
|
+
if (!controlURL) {
|
|
99
|
+
throw new ControlError(operation, 0, "no memory address is known (controlURL)", {
|
|
100
|
+
errorCode: "SPACES_NOT_CONFIGURED",
|
|
101
|
+
next: "set the Memory address in Memory's settings, or sign in to Jelly",
|
|
102
|
+
})
|
|
103
|
+
}
|
|
82
104
|
const url = new URL(`${controlURL}${route}`)
|
|
83
105
|
for (const [key, value] of Object.entries(options.query ?? {})) {
|
|
84
106
|
if (value !== undefined && value !== null && value !== "") url.searchParams.set(key, String(value))
|
|
@@ -149,7 +171,7 @@ export function createControlClient(input) {
|
|
|
149
171
|
* @param {{ userId: string }} input
|
|
150
172
|
*/
|
|
151
173
|
async function listSpaces({ userId } = {}) {
|
|
152
|
-
if (!userId) throw
|
|
174
|
+
if (!userId) throw missing("spaces", "user_id")
|
|
153
175
|
const payload = await request("spaces", "GET", "/v1/spaces", { query: { user_id: String(userId) } })
|
|
154
176
|
return extractSpaceList(payload).map(normalizeSpace).filter((space) => space.space_id)
|
|
155
177
|
}
|
|
@@ -165,7 +187,7 @@ export function createControlClient(input) {
|
|
|
165
187
|
* }} input
|
|
166
188
|
*/
|
|
167
189
|
async function resolveContext({ userId, spaceId, sessionId, agent } = {}) {
|
|
168
|
-
if (!userId) throw
|
|
190
|
+
if (!userId) throw missing("context", "user_id")
|
|
169
191
|
const body = { user_id: String(userId) }
|
|
170
192
|
if (spaceId) body.space_id = String(spaceId)
|
|
171
193
|
if (sessionId) body.session_id = String(sessionId)
|
|
@@ -184,8 +206,8 @@ export function createControlClient(input) {
|
|
|
184
206
|
* @param {{ userId: string, resourceId: string }} input
|
|
185
207
|
*/
|
|
186
208
|
async function effectiveAccess({ userId, resourceId } = {}) {
|
|
187
|
-
if (!userId) throw
|
|
188
|
-
if (!resourceId) throw
|
|
209
|
+
if (!userId) throw missing("effective", "user_id")
|
|
210
|
+
if (!resourceId) throw missing("effective", "resource_id")
|
|
189
211
|
const payload = await request("effective", "GET", "/v1/effective", {
|
|
190
212
|
query: { user_id: String(userId), resource_id: String(resourceId) },
|
|
191
213
|
})
|
|
@@ -199,8 +221,8 @@ export function createControlClient(input) {
|
|
|
199
221
|
* @param {{ userId: string, spaceId: string, resource: Record<string, unknown> }} input
|
|
200
222
|
*/
|
|
201
223
|
async function createResource({ userId, spaceId, resource } = {}) {
|
|
202
|
-
if (!userId) throw
|
|
203
|
-
if (!spaceId) throw
|
|
224
|
+
if (!userId) throw missing("resource", "user_id")
|
|
225
|
+
if (!spaceId) throw missing("resource", "space_id")
|
|
204
226
|
const payload = await request("resource", "POST", `/v1/spaces/${encodeURIComponent(String(spaceId))}/resources`, {
|
|
205
227
|
body: resource ?? {},
|
|
206
228
|
})
|
|
@@ -217,9 +239,9 @@ export function createControlClient(input) {
|
|
|
217
239
|
* @returns {Promise<{ resource?: object, deleted?: boolean, upstream?: string, dry_run?: boolean }>}
|
|
218
240
|
*/
|
|
219
241
|
async function deleteResource({ userId, spaceId, resourceId, dryRun = false } = {}) {
|
|
220
|
-
if (!userId) throw
|
|
221
|
-
if (!spaceId) throw
|
|
222
|
-
if (!resourceId) throw
|
|
242
|
+
if (!userId) throw missing("resource", "user_id")
|
|
243
|
+
if (!spaceId) throw missing("resource", "space_id")
|
|
244
|
+
if (!resourceId) throw missing("resource", "resource_id")
|
|
223
245
|
const route = `/v1/spaces/${encodeURIComponent(String(spaceId))}/resources/${encodeURIComponent(String(resourceId))}`
|
|
224
246
|
const payload = await request("resource", "DELETE", route, dryRun ? { query: { dry_run: "true" } } : {})
|
|
225
247
|
return isPlainObject(payload) ? payload : {}
|
|
@@ -236,8 +258,8 @@ export function createControlClient(input) {
|
|
|
236
258
|
* @param {{ userId: string, spaceId: string }} input
|
|
237
259
|
*/
|
|
238
260
|
async function listGrants({ userId, spaceId } = {}) {
|
|
239
|
-
if (!userId) throw
|
|
240
|
-
if (!spaceId) throw
|
|
261
|
+
if (!userId) throw missing("grants", "user_id")
|
|
262
|
+
if (!spaceId) throw missing("grants", "space_id")
|
|
241
263
|
const payload = await request("grants", "GET", "/v1/grants", {
|
|
242
264
|
query: { space_id: String(spaceId) },
|
|
243
265
|
})
|
|
@@ -251,8 +273,8 @@ export function createControlClient(input) {
|
|
|
251
273
|
* @param {{ userId: string, spaceId: string }} input
|
|
252
274
|
*/
|
|
253
275
|
async function listMembers({ userId, spaceId } = {}) {
|
|
254
|
-
if (!userId) throw
|
|
255
|
-
if (!spaceId) throw
|
|
276
|
+
if (!userId) throw missing("members", "user_id")
|
|
277
|
+
if (!spaceId) throw missing("members", "space_id")
|
|
256
278
|
const payload = await request(
|
|
257
279
|
"members",
|
|
258
280
|
"GET",
|
|
@@ -397,9 +419,9 @@ function normalizeContext(payload, requested) {
|
|
|
397
419
|
)
|
|
398
420
|
if (!containerID) {
|
|
399
421
|
if (routing !== "gateway") {
|
|
400
|
-
throw new ControlError("context", 0, "control plane returned no container placement")
|
|
422
|
+
throw new ControlError("context", 0, "control plane returned no container placement", NO_PLACEMENT)
|
|
401
423
|
}
|
|
402
|
-
throw new ControlError("context", 0, "gateway context returned no space placement")
|
|
424
|
+
throw new ControlError("context", 0, "gateway context returned no space placement", NO_PLACEMENT)
|
|
403
425
|
}
|
|
404
426
|
|
|
405
427
|
const collectionID = first(collection.id, collection.collection_id, root.collection_id, root.collectionId)
|
package/src/spool.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "node:fs"
|
|
2
2
|
import path from "node:path"
|
|
3
|
+
import { asSession, currentSession, isUnlinked } from "./credentials.js"
|
|
3
4
|
import { errorMessage, oneLine, redactSecrets, shortHash } from "./util.js"
|
|
4
5
|
|
|
5
6
|
/** At most this many sends per flush; entries that are not due do not count. */
|
|
@@ -27,7 +28,7 @@ const CLAIM_SUFFIX = /\.(\d+)-(\d+)\.inflight$/
|
|
|
27
28
|
* quarantined with the reason.
|
|
28
29
|
* - `unreachable`: no answer from the backend (transport error, timeout, 502,
|
|
29
30
|
* 503, 504). The rest of the queue would fail the same way.
|
|
30
|
-
* - `transient`: anything else, including 401 (a credential problem, fixed by
|
|
31
|
+
* - `transient`: an entry whose session has another speaker now, and anything else, including 401 (a credential problem, fixed by
|
|
31
32
|
* the operator, not by the entry), 404 (a route the edge does not forward),
|
|
32
33
|
* 408, 425, 429, other 5xx, and every control-plane failure: placement is
|
|
33
34
|
* resolved again on the next attempt.
|
|
@@ -36,6 +37,7 @@ const CLAIM_SUFFIX = /\.(\d+)-(\d+)\.inflight$/
|
|
|
36
37
|
* @returns {"permanent" | "unreachable" | "transient"}
|
|
37
38
|
*/
|
|
38
39
|
export function classifyFailure(error) {
|
|
40
|
+
if (error?.speakerChanged) return "transient"
|
|
39
41
|
const status = Number(error?.status)
|
|
40
42
|
if (!Number.isFinite(status) || status === 0 || status === 408) return "unreachable"
|
|
41
43
|
if (status === 502 || status === 503 || status === 504) return "unreachable"
|
|
@@ -75,12 +77,30 @@ function backoffMs(attempts) {
|
|
|
75
77
|
* version is ignored on replay. In local direct mode the entry carries the
|
|
76
78
|
* fixed local scope and is written to `conversation/add`.
|
|
77
79
|
*
|
|
80
|
+
* **Whose credential sends an entry.** Each entry records `credentialSession`:
|
|
81
|
+
* the OpenCode session it was captured or written in ("" for work with no
|
|
82
|
+
* session). A replay runs as that session (`asSession`), so inside Jelly it is
|
|
83
|
+
* sent with that session's token, the token that names the person who spoke
|
|
84
|
+
* there, never with the token of the session whose turn happened to start the
|
|
85
|
+
* flush. An entry written before 0.8.2 has no such field: a turn replays as its
|
|
86
|
+
* own `sessionID`, anything else as work with no session. When Jelly says the
|
|
87
|
+
* person speaking in an entry's session has not linked a Jelly account on this
|
|
88
|
+
* host, the entry is deleted and the log says so, without its content: no
|
|
89
|
+
* other credential may send it. An entry also records `credentialSubject`, the
|
|
90
|
+
* account its token named; when its session's token names someone else at
|
|
91
|
+
* replay (another person spoke there since), it is kept for later, never sent
|
|
92
|
+
* with that token.
|
|
93
|
+
*
|
|
78
94
|
* @param {{ config: any, log: any, backend: any, captureTurn?: Function | null, now?: () => number }} input
|
|
79
95
|
*/
|
|
80
96
|
export function createSpool(input) {
|
|
81
97
|
const { config, log, backend } = input
|
|
82
98
|
const captureTurn = typeof input.captureTurn === "function" ? input.captureTurn : null
|
|
83
99
|
const now = typeof input.now === "function" ? input.now : Date.now
|
|
100
|
+
/** The account the current session's token names, or "": recorded with an entry. */
|
|
101
|
+
const subject = typeof input.subject === "function" ? input.subject : () => ""
|
|
102
|
+
/** The account the current session's token names now, asking Jelly when needed: checked before a replay. */
|
|
103
|
+
const speaker = typeof input.speaker === "function" ? input.speaker : async () => ""
|
|
84
104
|
const spoolDir = path.join(config.stateDir, "spool")
|
|
85
105
|
const claimsDir = path.join(config.stateDir, "claims")
|
|
86
106
|
let sequence = 0
|
|
@@ -133,7 +153,7 @@ export function createSpool(input) {
|
|
|
133
153
|
try {
|
|
134
154
|
ensureDir()
|
|
135
155
|
const file = path.join(spoolDir, newName(data))
|
|
136
|
-
writePrivate(file,
|
|
156
|
+
writePrivate(file, entryOf(kind, data, scope, meta), "wx")
|
|
137
157
|
return file
|
|
138
158
|
} catch (error) {
|
|
139
159
|
log.error("spool write failed", errorMessage(error))
|
|
@@ -141,6 +161,20 @@ export function createSpool(input) {
|
|
|
141
161
|
}
|
|
142
162
|
}
|
|
143
163
|
|
|
164
|
+
/** The record written for a capture: the session whose credential will send it comes with it. */
|
|
165
|
+
function entryOf(kind, data, scope, meta) {
|
|
166
|
+
const credentialSubject = subject()
|
|
167
|
+
return {
|
|
168
|
+
kind,
|
|
169
|
+
data,
|
|
170
|
+
scope,
|
|
171
|
+
credentialSession: currentSession(),
|
|
172
|
+
...(credentialSubject ? { credentialSubject } : {}),
|
|
173
|
+
...meta,
|
|
174
|
+
queuedAt: new Date(now()).toISOString(),
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
144
178
|
/**
|
|
145
179
|
* Persist a capture that is about to be sent by this process. It is written
|
|
146
180
|
* already claimed, so a concurrent flush cannot send it too. On success the
|
|
@@ -153,7 +187,7 @@ export function createSpool(input) {
|
|
|
153
187
|
try {
|
|
154
188
|
ensureDir()
|
|
155
189
|
const file = claimName(path.join(spoolDir, newName(data)))
|
|
156
|
-
writePrivate(file,
|
|
190
|
+
writePrivate(file, entryOf(kind, data, scope, meta), "wx")
|
|
157
191
|
return file
|
|
158
192
|
} catch (error) {
|
|
159
193
|
log.error("spool write failed", errorMessage(error))
|
|
@@ -240,9 +274,35 @@ export function createSpool(input) {
|
|
|
240
274
|
return "kept"
|
|
241
275
|
}
|
|
242
276
|
|
|
277
|
+
/**
|
|
278
|
+
* Delete an entry whose session's speaker has no Jelly account linked on
|
|
279
|
+
* this host. The log names the file and kind, never the content.
|
|
280
|
+
*/
|
|
281
|
+
function dropUnlinked(file, entry) {
|
|
282
|
+
try {
|
|
283
|
+
fs.unlinkSync(file)
|
|
284
|
+
} catch {
|
|
285
|
+
/* already gone */
|
|
286
|
+
}
|
|
287
|
+
log.warn("spooled entry dropped: the person speaking in its session hasn't linked a Jelly account on this host, and no other credential may send it", {
|
|
288
|
+
file: path.basename(pendingOf(file)),
|
|
289
|
+
kind: entry?.kind ?? null,
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
|
|
243
293
|
/** Return a staged capture after a failed live send. */
|
|
244
294
|
function releaseSpool(claimed, error) {
|
|
245
295
|
if (!claimed) return null
|
|
296
|
+
if (isUnlinked(error)) {
|
|
297
|
+
let entry = null
|
|
298
|
+
try {
|
|
299
|
+
entry = JSON.parse(fs.readFileSync(claimed, "utf8"))
|
|
300
|
+
} catch {
|
|
301
|
+
/* the kind is only for the log */
|
|
302
|
+
}
|
|
303
|
+
dropUnlinked(claimed, entry)
|
|
304
|
+
return "dropped"
|
|
305
|
+
}
|
|
246
306
|
try {
|
|
247
307
|
const entry = JSON.parse(fs.readFileSync(claimed, "utf8"))
|
|
248
308
|
return recordFailure(claimed, entry, error)
|
|
@@ -275,6 +335,15 @@ export function createSpool(input) {
|
|
|
275
335
|
return entry.sessionID ?? entry.scope?.session_id ?? null
|
|
276
336
|
}
|
|
277
337
|
|
|
338
|
+
/**
|
|
339
|
+
* The session whose credential sends an entry: the one it recorded, else
|
|
340
|
+
* (an entry from before 0.8.2) a turn's own session, else none.
|
|
341
|
+
*/
|
|
342
|
+
function credentialSessionOf(entry) {
|
|
343
|
+
if (typeof entry.credentialSession === "string") return entry.credentialSession
|
|
344
|
+
return entry.kind === "session" ? (turnSessionOf(entry) ?? "") : ""
|
|
345
|
+
}
|
|
346
|
+
|
|
278
347
|
/**
|
|
279
348
|
* Cross-process, cross-instance claim on a capture. Multiple plugin instances
|
|
280
349
|
* can observe the same global event stream, so turn identity has to be
|
|
@@ -369,6 +438,18 @@ export function createSpool(input) {
|
|
|
369
438
|
}
|
|
370
439
|
|
|
371
440
|
async function send(entry) {
|
|
441
|
+
// Someone else may speak in the session now. An entry captured under one
|
|
442
|
+
// person's token is never sent with another's: it waits, with backoff,
|
|
443
|
+
// until its session's token names that person again, or the caps
|
|
444
|
+
// quarantine it.
|
|
445
|
+
if (typeof entry.credentialSubject === "string" && entry.credentialSubject) {
|
|
446
|
+
const named = await speaker()
|
|
447
|
+
if (named && named !== entry.credentialSubject) {
|
|
448
|
+
throw Object.assign(new Error("the person speaking in this entry's session has changed since it was captured; it waits for them"), {
|
|
449
|
+
speakerChanged: true,
|
|
450
|
+
})
|
|
451
|
+
}
|
|
452
|
+
}
|
|
372
453
|
if (entry.kind === "session") {
|
|
373
454
|
if (captureTurn) {
|
|
374
455
|
// Control mode: the gateway places the turn from the visibility it
|
|
@@ -460,11 +541,17 @@ export function createSpool(input) {
|
|
|
460
541
|
|
|
461
542
|
sends += 1
|
|
462
543
|
try {
|
|
463
|
-
|
|
544
|
+
// As the session the entry was captured in: its token, no other.
|
|
545
|
+
await asSession(credentialSessionOf(entry), () => send(entry))
|
|
464
546
|
fs.unlinkSync(claimed)
|
|
465
547
|
result.flushed += 1
|
|
466
548
|
unreachable = 0
|
|
467
549
|
} catch (error) {
|
|
550
|
+
if (isUnlinked(error)) {
|
|
551
|
+
dropUnlinked(claimed, entry)
|
|
552
|
+
result.dropped = (result.dropped ?? 0) + 1
|
|
553
|
+
continue
|
|
554
|
+
}
|
|
468
555
|
result.failed += 1
|
|
469
556
|
if (recordFailure(claimed, entry, error) === "quarantined") result.quarantined += 1
|
|
470
557
|
if (classifyFailure(error) === "unreachable") {
|
package/src/tools/commitments.js
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
validateFields,
|
|
13
13
|
} from "../records.js"
|
|
14
14
|
import { errorMessage, oneLine } from "../util.js"
|
|
15
|
+
import { withNote } from "../errors.js"
|
|
15
16
|
|
|
16
17
|
/** The list answer shows at most this many items; the rest are counted. */
|
|
17
18
|
const LIST_MAX = 50
|
|
@@ -177,7 +178,7 @@ export function registerCommitments(editor, toolkit) {
|
|
|
177
178
|
})
|
|
178
179
|
} catch (error) {
|
|
179
180
|
log.warn("commitments write failed", error)
|
|
180
|
-
return fail(
|
|
181
|
+
return fail(withNote(errorMessage(error), "nothing was saved"))
|
|
181
182
|
}
|
|
182
183
|
if (result.error) return text(`${result.conflict ? "not saved (conflict)" : "not saved"}: ${result.error}`)
|
|
183
184
|
// The Due list in the pinned block is rebuilt on the next model call.
|
package/src/tools/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MEM_NAMESPACE_DESCRIPTION, PINNED_TOOLS } from "../call-shapes.js"
|
|
2
|
+
import { asSession } from "../credentials.js"
|
|
2
3
|
import { createToolkit } from "./shared.js"
|
|
3
4
|
import { registerRecall } from "./recall.js"
|
|
4
5
|
import { registerWiki } from "./wiki.js"
|
|
@@ -16,6 +17,9 @@ import { registerSpaces } from "./spaces.js"
|
|
|
16
17
|
* The tools are registered even when memory is unavailable (a gateway or
|
|
17
18
|
* Jelly install with no credential): each then answers the one sentence
|
|
18
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.
|
|
19
23
|
*
|
|
20
24
|
* @param {any} ctx
|
|
21
25
|
* @param {any} runtime
|
|
@@ -47,11 +51,12 @@ function gateEditor(editor, runtime) {
|
|
|
47
51
|
target.add({
|
|
48
52
|
...definition,
|
|
49
53
|
options: { ...definition.options, ...(PINNED_TOOLS.includes(definition.name) ? { pinned: true } : {}) },
|
|
50
|
-
execute:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
execute: (input, context) =>
|
|
55
|
+
asSession(context?.sessionID, async () => {
|
|
56
|
+
const reason = (runtime.availability ? await runtime.availability() : runtime.unavailable?.()) ?? ""
|
|
57
|
+
if (reason) return { content: reason }
|
|
58
|
+
return definition.execute(input, context)
|
|
59
|
+
}),
|
|
55
60
|
})
|
|
56
61
|
},
|
|
57
62
|
})
|
package/src/tools/memory.js
CHANGED
|
@@ -16,8 +16,17 @@ import {
|
|
|
16
16
|
rememberParts,
|
|
17
17
|
} from "../copies.js"
|
|
18
18
|
import { errorMessage, neutralizeFenceClosers, oneLine, redactSecrets, shortHash } from "../util.js"
|
|
19
|
+
import { isCoded, withNote } from "../errors.js"
|
|
19
20
|
import { TASK_INPUT, canWriteShared } from "./shared.js"
|
|
20
21
|
|
|
22
|
+
/**
|
|
23
|
+
* The permission mem_forget asserts before it deletes: the tool's own engine
|
|
24
|
+
* id, declared `effect: "ask"` in `jelly.tools`. Not an approval action
|
|
25
|
+
* (`jelly.asks`), so it is a name here, not a literal the scan in
|
|
26
|
+
* scripts/approval-actions.mjs would read as one.
|
|
27
|
+
*/
|
|
28
|
+
const FORGET_PERMISSION = "mem_forget"
|
|
29
|
+
|
|
21
30
|
const KINDS = Object.freeze(["fact", "decision", "constraint", "preference", "procedure"])
|
|
22
31
|
/** A supersede replaces a few atoms, not a cleanup: more is a forget. */
|
|
23
32
|
const MAX_SUPERSEDES = 10
|
|
@@ -217,7 +226,12 @@ export function registerMemory(editor, toolkit) {
|
|
|
217
226
|
} catch (error) {
|
|
218
227
|
if (!isTransientFailure(error)) {
|
|
219
228
|
log.warn("remember rejected by the backend", error)
|
|
220
|
-
|
|
229
|
+
const message = errorMessage(error)
|
|
230
|
+
return text(
|
|
231
|
+
isCoded(message)
|
|
232
|
+
? withNote(message, "not stored, and nothing was queued")
|
|
233
|
+
: `not stored: the backend rejected the write (${message}). Nothing was queued.`,
|
|
234
|
+
)
|
|
221
235
|
}
|
|
222
236
|
const spooled = toolkit.runtime.enqueueSpool("conversation", { sessionID, messages, scope: scoped })
|
|
223
237
|
if (spooled) {
|
|
@@ -229,9 +243,7 @@ export function registerMemory(editor, toolkit) {
|
|
|
229
243
|
)
|
|
230
244
|
}
|
|
231
245
|
log.warn("remember failed and could not be spooled", error)
|
|
232
|
-
return fail(
|
|
233
|
-
`${errorMessage(error)}; local spooling is disabled or unavailable, so the memory was not stored.`,
|
|
234
|
-
)
|
|
246
|
+
return fail(withNote(errorMessage(error), "local spooling is disabled or unavailable, so the memory was not stored"))
|
|
235
247
|
}
|
|
236
248
|
},
|
|
237
249
|
})
|
|
@@ -806,6 +818,25 @@ export function registerMemory(editor, toolkit) {
|
|
|
806
818
|
const label = displayScope(spaceContext, scoped)
|
|
807
819
|
|
|
808
820
|
const preview = Boolean(match) && input?.confirm !== true
|
|
821
|
+
// Forget asks (D17): a delete cannot be undone, so the call asserts the
|
|
822
|
+
// tool's own permission before anything is deleted, and the plugin's
|
|
823
|
+
// default rule (src/permissions.js) or Jelly's declared `ask` makes the
|
|
824
|
+
// host ask. A preview deletes nothing and does not ask.
|
|
825
|
+
if (!preview) {
|
|
826
|
+
try {
|
|
827
|
+
await toolkit.assert({
|
|
828
|
+
sessionID: context?.sessionID,
|
|
829
|
+
agent: context?.agent,
|
|
830
|
+
source: { type: "tool", messageID: context?.messageID, id: context?.id },
|
|
831
|
+
action: FORGET_PERMISSION,
|
|
832
|
+
resources: [visibility],
|
|
833
|
+
save: [visibility],
|
|
834
|
+
metadata: { visibility, layer, space: label, ...(ids.length > 0 ? { ids: ids.length } : {}), ...(match ? { erase: true } : {}) },
|
|
835
|
+
})
|
|
836
|
+
} catch (error) {
|
|
837
|
+
return text(`forgetting denied: ${errorMessage(error)}. Nothing was deleted.`)
|
|
838
|
+
}
|
|
839
|
+
}
|
|
809
840
|
const approvalMetadata = { layer, space: spaceContext?.space_id ?? "team", ...(match ? { erase: true } : {}) }
|
|
810
841
|
// An id delete asks once it knows every row it will delete: the named ids
|
|
811
842
|
// and the mem_remember rows that hold only their text. Nothing is
|
package/src/tools/shared.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { contextScopeLabel, scopeLabel, taskKey } from "../scope.js"
|
|
2
2
|
import { errorMessage, oneLine } from "../util.js"
|
|
3
|
+
import { isCoded } from "../errors.js"
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Roles that may write to a shared space. The plugin reads this from the
|
|
@@ -31,7 +32,8 @@ export function createToolkit(ctx, runtime) {
|
|
|
31
32
|
const control = runtime.control
|
|
32
33
|
|
|
33
34
|
const text = (content) => ({ content })
|
|
34
|
-
|
|
35
|
+
// A coded failure (src/errors.js) already says what happened and what to do.
|
|
36
|
+
const fail = (message) => text(isCoded(message) ? message : `memory unavailable: ${message}`)
|
|
35
37
|
|
|
36
38
|
/** Shared-asset scope (skills, wiki). Agent is intentionally absent. */
|
|
37
39
|
function sharedScope(context) {
|
package/views/memory.html
CHANGED
|
@@ -204,7 +204,7 @@ const plural = (count, one, many) => `${count.toLocaleString("en-GB")} ${count =
|
|
|
204
204
|
function notConnected(overview) {
|
|
205
205
|
const steps = [];
|
|
206
206
|
if (overview.credential === "none")
|
|
207
|
-
steps.push("Use Jelly with an account that offers
|
|
207
|
+
steps.push("Use Jelly with an account that offers Memory, or paste the key from Memory's /account page into Memory key, in this plugin's settings.");
|
|
208
208
|
if (!overview.address) steps.push("If you use your own memory service, put its address in Memory address, in this plugin's settings.");
|
|
209
209
|
if (steps.length === 0) steps.push("Check Memory address and Memory key in this plugin's settings, then try again.");
|
|
210
210
|
return el("div", { class: "note", role: "status" },
|
|
@@ -332,7 +332,7 @@ async function spacesTab() {
|
|
|
332
332
|
),
|
|
333
333
|
),
|
|
334
334
|
p(spaces.some((space) => space.kind === "shared")
|
|
335
|
-
? "Who can use a shared space is managed on
|
|
335
|
+
? "Who can use a shared space is managed on Memory's site for now."
|
|
336
336
|
: "Team spaces will show here once your team has one.", "muted"),
|
|
337
337
|
);
|
|
338
338
|
}
|