@joe-broadhead/opencode-mem 0.6.2 → 0.7.0
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/README.md +70 -5
- package/package.json +446 -5
- package/src/index.js +10 -0
- package/src/views/contracts.js +239 -0
- package/src/views/rpc.js +412 -0
- package/views/home.html +184 -0
- package/views/memory.html +487 -0
package/views/home.html
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en-GB">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>Memory</title>
|
|
6
|
+
<style>
|
|
7
|
+
:root {
|
|
8
|
+
color: var(--jelly-text, CanvasText);
|
|
9
|
+
background: var(--jelly-surface, Canvas);
|
|
10
|
+
font: 14px/1.4 var(--jelly-font, system-ui, sans-serif);
|
|
11
|
+
}
|
|
12
|
+
* { box-sizing: border-box; }
|
|
13
|
+
body { margin: 0; padding: 2px; }
|
|
14
|
+
p { margin: 0; }
|
|
15
|
+
.muted { color: var(--jelly-muted, GrayText); font-size: 12px; }
|
|
16
|
+
.state { font-weight: 600; display: flex; align-items: center; gap: 6px; }
|
|
17
|
+
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--jelly-accent, Highlight); flex: none; }
|
|
18
|
+
.dot.off { background: transparent; border: 1.5px solid var(--jelly-muted, GrayText); }
|
|
19
|
+
.dot.danger { background: var(--jelly-danger, CanvasText); }
|
|
20
|
+
.figure { font-size: 26px; font-weight: 600; line-height: 1.15; margin: 6px 0 2px; }
|
|
21
|
+
.button {
|
|
22
|
+
font: inherit; font-size: 12px; color: var(--jelly-text, CanvasText); background: var(--jelly-raised, Canvas);
|
|
23
|
+
border: 1px solid var(--jelly-border, GrayText); border-radius: var(--jelly-radius, 8px); padding: 2px 8px; margin-top: 8px; cursor: pointer;
|
|
24
|
+
}
|
|
25
|
+
.button:focus-visible { outline: 2px solid var(--jelly-accent, Highlight); outline-offset: 2px; }
|
|
26
|
+
</style>
|
|
27
|
+
</head>
|
|
28
|
+
<body>
|
|
29
|
+
<main id="tile" aria-live="polite"><p class="muted">Loading…</p></main>
|
|
30
|
+
<script>
|
|
31
|
+
// jelly-view.js: a client of Jelly's view bridge. Paste it into the page's own <script>:
|
|
32
|
+
// a sealed page loads no script by URL and is given no global. Call jellyView() as the
|
|
33
|
+
// script starts; it resolves with the client when the app says hello.
|
|
34
|
+
function jellyView() {
|
|
35
|
+
return new Promise((resolve) => {
|
|
36
|
+
addEventListener("message", function hello(event) {
|
|
37
|
+
const said = event.data;
|
|
38
|
+
if (event.source !== parent || said?.v !== 1 || said.kind !== "hello" || !event.ports[0])
|
|
39
|
+
return;
|
|
40
|
+
removeEventListener("message", hello);
|
|
41
|
+
const port = event.ports[0];
|
|
42
|
+
const listeners = new Set();
|
|
43
|
+
const latest = new Map();
|
|
44
|
+
const queue = [];
|
|
45
|
+
let sequence = 0;
|
|
46
|
+
let current;
|
|
47
|
+
const theme = (tokens) => {
|
|
48
|
+
const style = document.documentElement.style;
|
|
49
|
+
for (const [name, value] of Object.entries(tokens))
|
|
50
|
+
if (name === "scheme") style.colorScheme = value;
|
|
51
|
+
else style.setProperty(`--jelly-${name}`, value);
|
|
52
|
+
};
|
|
53
|
+
// The app answers one request at a time, so requests go in turn.
|
|
54
|
+
const next = () => {
|
|
55
|
+
if (current !== undefined || queue.length === 0) return;
|
|
56
|
+
current = queue.shift();
|
|
57
|
+
current.timer = setTimeout(
|
|
58
|
+
() => settle({ ok: false, error: "No answer from the app" }),
|
|
59
|
+
15000,
|
|
60
|
+
);
|
|
61
|
+
port.postMessage(current.message);
|
|
62
|
+
};
|
|
63
|
+
const settle = (answer) => {
|
|
64
|
+
const done = current;
|
|
65
|
+
current = undefined;
|
|
66
|
+
clearTimeout(done.timer);
|
|
67
|
+
if (answer.ok) done.resolve(answer.value);
|
|
68
|
+
else done.reject(new Error(answer.error));
|
|
69
|
+
next();
|
|
70
|
+
};
|
|
71
|
+
port.onmessage = ({ data }) => {
|
|
72
|
+
if (data?.v !== 1) return;
|
|
73
|
+
if (typeof data.kind !== "string") {
|
|
74
|
+
if (current !== undefined && data.re === current.message.id) settle(data);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
// A call waiting for the person is answered by `until`, not in 15 s.
|
|
78
|
+
if (data.kind === "rpc.waiting" && data.re === current?.message.id) {
|
|
79
|
+
clearTimeout(current.timer);
|
|
80
|
+
current.timer = setTimeout(
|
|
81
|
+
() => settle({ ok: false, error: "No answer from the app" }),
|
|
82
|
+
Math.max(0, Date.parse(data.until) - Date.now()) + 15000,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (data.kind === "theme.changed") theme(data.theme);
|
|
86
|
+
// A wait is news only while it lasts, so a later listener is not told of it.
|
|
87
|
+
if (data.kind !== "rpc.waiting") latest.set(data.kind, data);
|
|
88
|
+
for (const listener of listeners) listener(data);
|
|
89
|
+
};
|
|
90
|
+
port.start();
|
|
91
|
+
theme(said.theme);
|
|
92
|
+
const request = (op, fields = {}) =>
|
|
93
|
+
new Promise((resolve, reject) => {
|
|
94
|
+
queue.push({ resolve, reject, message: { ...fields, v: 1, id: String(++sequence), op } });
|
|
95
|
+
next();
|
|
96
|
+
});
|
|
97
|
+
resolve({
|
|
98
|
+
hello: said,
|
|
99
|
+
// A listener first hears the latest push of each kind that came before it.
|
|
100
|
+
on(listener) {
|
|
101
|
+
for (const push of latest.values()) listener(push);
|
|
102
|
+
listeners.add(listener);
|
|
103
|
+
return () => listeners.delete(listener);
|
|
104
|
+
},
|
|
105
|
+
request,
|
|
106
|
+
data: () => request("data.get"),
|
|
107
|
+
state: () => request("state.get"),
|
|
108
|
+
setState: (revision, data) => request("state.set", { revision, data }),
|
|
109
|
+
call: (method, input = {}) => request("rpc.call", { method, input }),
|
|
110
|
+
openAgent: (prefill) => request("open-agent", prefill === undefined ? {} : { prefill }),
|
|
111
|
+
resize: (height) =>
|
|
112
|
+
request("resize", { height: Math.min(1200, Math.max(1, Math.ceil(height))) }),
|
|
113
|
+
decline: () => request("decline"),
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
</script>
|
|
119
|
+
<script>
|
|
120
|
+
// Memory's Home tile: whether memory is on, and how much it holds. Nothing is
|
|
121
|
+
// fetched; the numbers come from the plugin over the bridge.
|
|
122
|
+
const tile = document.getElementById("tile");
|
|
123
|
+
let view = null;
|
|
124
|
+
|
|
125
|
+
function el(tag, className, text) {
|
|
126
|
+
const node = document.createElement(tag);
|
|
127
|
+
if (className) node.className = className;
|
|
128
|
+
if (text !== undefined) node.textContent = text;
|
|
129
|
+
return node;
|
|
130
|
+
}
|
|
131
|
+
function state(words, dot) {
|
|
132
|
+
const line = el("p", "state");
|
|
133
|
+
const mark = el("span", `dot ${dot}`);
|
|
134
|
+
mark.setAttribute("aria-hidden", "true");
|
|
135
|
+
line.append(mark, words);
|
|
136
|
+
return line;
|
|
137
|
+
}
|
|
138
|
+
function fit() {
|
|
139
|
+
// The body's own height: the document is at least as tall as the frame, so it never shrinks.
|
|
140
|
+
if (view) view.resize(document.body.getBoundingClientRect().height).catch(() => undefined);
|
|
141
|
+
}
|
|
142
|
+
function show(...nodes) {
|
|
143
|
+
tile.replaceChildren(...nodes);
|
|
144
|
+
tile.removeAttribute("aria-busy");
|
|
145
|
+
fit();
|
|
146
|
+
}
|
|
147
|
+
const count = (value, one, many) => `${value.toLocaleString("en-GB")} ${value === 1 ? one : many}`;
|
|
148
|
+
|
|
149
|
+
async function draw() {
|
|
150
|
+
tile.setAttribute("aria-busy", "true");
|
|
151
|
+
let overview;
|
|
152
|
+
try {
|
|
153
|
+
overview = await view.call("memory.overview");
|
|
154
|
+
} catch (error) {
|
|
155
|
+
const retry = el("button", "button", "Try again");
|
|
156
|
+
retry.type = "button";
|
|
157
|
+
retry.addEventListener("click", draw);
|
|
158
|
+
return show(state("Couldn't read memory", "danger"), el("p", "muted", error.message), retry);
|
|
159
|
+
}
|
|
160
|
+
if (!overview.connected)
|
|
161
|
+
return show(
|
|
162
|
+
state("Not connected", "off"),
|
|
163
|
+
el("p", "muted", "Open Settings, then Plugins, then Memory, to connect it."),
|
|
164
|
+
);
|
|
165
|
+
const counts = overview.counts ?? { memories: 0, conversations: 0 };
|
|
166
|
+
const lines = [
|
|
167
|
+
state(overview.capture ? "Remembering" : "Not saving conversations", overview.capture ? "" : "off"),
|
|
168
|
+
el("p", "figure", counts.memories.toLocaleString("en-GB")),
|
|
169
|
+
el("p", "muted", `${counts.memories === 1 ? "memory" : "memories"} kept, from ${count(counts.conversations, "conversation", "conversations")}`),
|
|
170
|
+
];
|
|
171
|
+
if (overview.health !== "ok") lines.push(el("p", "muted", "Memory can't be reached just now."));
|
|
172
|
+
show(...lines);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
jellyView().then((client) => {
|
|
176
|
+
view = client;
|
|
177
|
+
view.on((push) => {
|
|
178
|
+
if (push.kind === "rpc.waiting") show(state("Waiting for you", "off"), el("p", "muted", "Allow this in Jelly to see your memory."));
|
|
179
|
+
});
|
|
180
|
+
draw();
|
|
181
|
+
});
|
|
182
|
+
</script>
|
|
183
|
+
</body>
|
|
184
|
+
</html>
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en-GB">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>Your memory</title>
|
|
6
|
+
<style>
|
|
7
|
+
:root {
|
|
8
|
+
color: var(--jelly-text, CanvasText);
|
|
9
|
+
background: var(--jelly-surface, Canvas);
|
|
10
|
+
font: 14px/1.45 var(--jelly-font, system-ui, sans-serif);
|
|
11
|
+
}
|
|
12
|
+
* { box-sizing: border-box; }
|
|
13
|
+
body { margin: 0; padding: 4px 2px 12px; }
|
|
14
|
+
h2 { font-size: 15px; margin: 0 0 6px; }
|
|
15
|
+
p { margin: 0 0 8px; }
|
|
16
|
+
.muted { color: var(--jelly-muted, GrayText); }
|
|
17
|
+
.danger { color: var(--jelly-danger, CanvasText); }
|
|
18
|
+
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--jelly-border, GrayText); margin-bottom: 12px; }
|
|
19
|
+
.tab {
|
|
20
|
+
font: inherit; color: var(--jelly-muted, GrayText); background: none; border: 0;
|
|
21
|
+
border-bottom: 2px solid transparent; padding: 6px 10px; margin-bottom: -1px; cursor: pointer;
|
|
22
|
+
}
|
|
23
|
+
.tab[aria-selected="true"] { color: var(--jelly-text, CanvasText); border-bottom-color: var(--jelly-accent, Highlight); font-weight: 600; }
|
|
24
|
+
button:focus-visible, a:focus-visible { outline: 2px solid var(--jelly-accent, Highlight); outline-offset: 2px; }
|
|
25
|
+
.button {
|
|
26
|
+
font: inherit; font-size: 13px; color: var(--jelly-text, CanvasText); background: var(--jelly-raised, Canvas);
|
|
27
|
+
border: 1px solid var(--jelly-border, GrayText); border-radius: var(--jelly-radius, 8px); padding: 4px 10px; cursor: pointer;
|
|
28
|
+
}
|
|
29
|
+
.button.primary { background: var(--jelly-accent, Highlight); color: var(--jelly-on-accent, HighlightText); border-color: transparent; }
|
|
30
|
+
.link { font: inherit; color: var(--jelly-accent, LinkText); background: none; border: 0; padding: 0; cursor: pointer; text-align: left; }
|
|
31
|
+
dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 12px 0 0; }
|
|
32
|
+
dt { color: var(--jelly-muted, GrayText); }
|
|
33
|
+
dd { margin: 0; }
|
|
34
|
+
ul.rows { list-style: none; margin: 0; padding: 0; }
|
|
35
|
+
ul.rows > li { border-top: 1px solid var(--jelly-border, GrayText); padding: 10px 0; display: flex; gap: 12px; align-items: flex-start; }
|
|
36
|
+
ul.rows > li:first-child { border-top: 0; }
|
|
37
|
+
.grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
|
|
38
|
+
.meta { font-size: 12px; color: var(--jelly-muted, GrayText); }
|
|
39
|
+
.badge { font-size: 12px; border: 1px solid var(--jelly-border, GrayText); border-radius: 999px; padding: 0 8px; white-space: nowrap; }
|
|
40
|
+
.note { border: 1px solid var(--jelly-border, GrayText); border-radius: var(--jelly-radius, 8px); padding: 10px 12px; margin: 8px 0; }
|
|
41
|
+
.page { white-space: pre-wrap; overflow-wrap: anywhere; border: 1px solid var(--jelly-border, GrayText); border-radius: var(--jelly-radius, 8px); padding: 10px 12px; margin-top: 8px; }
|
|
42
|
+
.actions { display: flex; gap: 8px; margin-top: 10px; align-items: center; flex-wrap: wrap; }
|
|
43
|
+
.spaced { margin-top: 10px; }
|
|
44
|
+
[hidden] { display: none !important; }
|
|
45
|
+
</style>
|
|
46
|
+
</head>
|
|
47
|
+
<body>
|
|
48
|
+
<div class="tabs" role="tablist" aria-label="Memory">
|
|
49
|
+
<button class="tab" role="tab" id="tab-overview" aria-controls="panel" aria-selected="true" data-tab="overview">Overview</button>
|
|
50
|
+
<button class="tab" role="tab" id="tab-spaces" aria-controls="panel" aria-selected="false" tabindex="-1" data-tab="spaces">Spaces</button>
|
|
51
|
+
<button class="tab" role="tab" id="tab-wiki" aria-controls="panel" aria-selected="false" tabindex="-1" data-tab="wiki">Wiki</button>
|
|
52
|
+
<button class="tab" role="tab" id="tab-recent" aria-controls="panel" aria-selected="false" tabindex="-1" data-tab="recent">Recent</button>
|
|
53
|
+
</div>
|
|
54
|
+
<main id="panel" role="tabpanel" aria-labelledby="tab-overview" aria-live="polite"></main>
|
|
55
|
+
<script>
|
|
56
|
+
// jelly-view.js: a client of Jelly's view bridge. Paste it into the page's own <script>:
|
|
57
|
+
// a sealed page loads no script by URL and is given no global. Call jellyView() as the
|
|
58
|
+
// script starts; it resolves with the client when the app says hello.
|
|
59
|
+
function jellyView() {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
addEventListener("message", function hello(event) {
|
|
62
|
+
const said = event.data;
|
|
63
|
+
if (event.source !== parent || said?.v !== 1 || said.kind !== "hello" || !event.ports[0])
|
|
64
|
+
return;
|
|
65
|
+
removeEventListener("message", hello);
|
|
66
|
+
const port = event.ports[0];
|
|
67
|
+
const listeners = new Set();
|
|
68
|
+
const latest = new Map();
|
|
69
|
+
const queue = [];
|
|
70
|
+
let sequence = 0;
|
|
71
|
+
let current;
|
|
72
|
+
const theme = (tokens) => {
|
|
73
|
+
const style = document.documentElement.style;
|
|
74
|
+
for (const [name, value] of Object.entries(tokens))
|
|
75
|
+
if (name === "scheme") style.colorScheme = value;
|
|
76
|
+
else style.setProperty(`--jelly-${name}`, value);
|
|
77
|
+
};
|
|
78
|
+
// The app answers one request at a time, so requests go in turn.
|
|
79
|
+
const next = () => {
|
|
80
|
+
if (current !== undefined || queue.length === 0) return;
|
|
81
|
+
current = queue.shift();
|
|
82
|
+
current.timer = setTimeout(
|
|
83
|
+
() => settle({ ok: false, error: "No answer from the app" }),
|
|
84
|
+
15000,
|
|
85
|
+
);
|
|
86
|
+
port.postMessage(current.message);
|
|
87
|
+
};
|
|
88
|
+
const settle = (answer) => {
|
|
89
|
+
const done = current;
|
|
90
|
+
current = undefined;
|
|
91
|
+
clearTimeout(done.timer);
|
|
92
|
+
if (answer.ok) done.resolve(answer.value);
|
|
93
|
+
else done.reject(new Error(answer.error));
|
|
94
|
+
next();
|
|
95
|
+
};
|
|
96
|
+
port.onmessage = ({ data }) => {
|
|
97
|
+
if (data?.v !== 1) return;
|
|
98
|
+
if (typeof data.kind !== "string") {
|
|
99
|
+
if (current !== undefined && data.re === current.message.id) settle(data);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
// A call waiting for the person is answered by `until`, not in 15 s.
|
|
103
|
+
if (data.kind === "rpc.waiting" && data.re === current?.message.id) {
|
|
104
|
+
clearTimeout(current.timer);
|
|
105
|
+
current.timer = setTimeout(
|
|
106
|
+
() => settle({ ok: false, error: "No answer from the app" }),
|
|
107
|
+
Math.max(0, Date.parse(data.until) - Date.now()) + 15000,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (data.kind === "theme.changed") theme(data.theme);
|
|
111
|
+
// A wait is news only while it lasts, so a later listener is not told of it.
|
|
112
|
+
if (data.kind !== "rpc.waiting") latest.set(data.kind, data);
|
|
113
|
+
for (const listener of listeners) listener(data);
|
|
114
|
+
};
|
|
115
|
+
port.start();
|
|
116
|
+
theme(said.theme);
|
|
117
|
+
const request = (op, fields = {}) =>
|
|
118
|
+
new Promise((resolve, reject) => {
|
|
119
|
+
queue.push({ resolve, reject, message: { ...fields, v: 1, id: String(++sequence), op } });
|
|
120
|
+
next();
|
|
121
|
+
});
|
|
122
|
+
resolve({
|
|
123
|
+
hello: said,
|
|
124
|
+
// A listener first hears the latest push of each kind that came before it.
|
|
125
|
+
on(listener) {
|
|
126
|
+
for (const push of latest.values()) listener(push);
|
|
127
|
+
listeners.add(listener);
|
|
128
|
+
return () => listeners.delete(listener);
|
|
129
|
+
},
|
|
130
|
+
request,
|
|
131
|
+
data: () => request("data.get"),
|
|
132
|
+
state: () => request("state.get"),
|
|
133
|
+
setState: (revision, data) => request("state.set", { revision, data }),
|
|
134
|
+
call: (method, input = {}) => request("rpc.call", { method, input }),
|
|
135
|
+
openAgent: (prefill) => request("open-agent", prefill === undefined ? {} : { prefill }),
|
|
136
|
+
resize: (height) =>
|
|
137
|
+
request("resize", { height: Math.min(1200, Math.max(1, Math.ceil(height))) }),
|
|
138
|
+
decline: () => request("decline"),
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
</script>
|
|
144
|
+
<script>
|
|
145
|
+
// Memory's Settings section. Everything shown comes over the bridge; nothing is
|
|
146
|
+
// fetched. Text from memory is set as text, never parsed as markup.
|
|
147
|
+
const panel = document.getElementById("panel");
|
|
148
|
+
const tabs = [...document.querySelectorAll(".tab")];
|
|
149
|
+
let view = null;
|
|
150
|
+
let settings = null;
|
|
151
|
+
let shown = "overview";
|
|
152
|
+
let waiting = false;
|
|
153
|
+
|
|
154
|
+
function el(tag, attributes = {}, ...children) {
|
|
155
|
+
const node = document.createElement(tag);
|
|
156
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
157
|
+
if (key === "onclick") node.addEventListener("click", value);
|
|
158
|
+
else if (value !== false && value !== undefined) node.setAttribute(key, value === true ? "" : String(value));
|
|
159
|
+
}
|
|
160
|
+
for (const child of children.flat()) if (child !== null && child !== undefined) node.append(child);
|
|
161
|
+
return node;
|
|
162
|
+
}
|
|
163
|
+
const p = (text, className) => el("p", className ? { class: className } : {}, text);
|
|
164
|
+
const button = (label, onclick, className = "button") => el("button", { class: className, type: "button", onclick }, label);
|
|
165
|
+
|
|
166
|
+
function fit() {
|
|
167
|
+
// The body's own height: the document is at least as tall as the frame, so it never shrinks.
|
|
168
|
+
if (view) view.resize(document.body.getBoundingClientRect().height).catch(() => undefined);
|
|
169
|
+
}
|
|
170
|
+
function show(...nodes) {
|
|
171
|
+
waiting = false;
|
|
172
|
+
panel.replaceChildren(...nodes.flat().filter((node) => node !== null && node !== undefined && node !== false));
|
|
173
|
+
panel.removeAttribute("aria-busy");
|
|
174
|
+
fit();
|
|
175
|
+
}
|
|
176
|
+
function loading(words = "Loading…") {
|
|
177
|
+
panel.setAttribute("aria-busy", "true");
|
|
178
|
+
panel.replaceChildren(p(words, "muted"));
|
|
179
|
+
fit();
|
|
180
|
+
}
|
|
181
|
+
function failed(error, retry) {
|
|
182
|
+
const words = String(error?.message || error || "Something went wrong.");
|
|
183
|
+
show(
|
|
184
|
+
el("div", { class: "note", role: "alert" },
|
|
185
|
+
el("p", {}, el("strong", { class: "danger" }, "Couldn't read your memory. "), words),
|
|
186
|
+
el("div", { class: "actions" }, button("Try again", retry)),
|
|
187
|
+
),
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
async function call(method, input) {
|
|
191
|
+
return view.call(method, input);
|
|
192
|
+
}
|
|
193
|
+
function date(value) {
|
|
194
|
+
const at = Date.parse(value);
|
|
195
|
+
if (!Number.isFinite(at)) return "";
|
|
196
|
+
return new Date(at).toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric" });
|
|
197
|
+
}
|
|
198
|
+
const plural = (count, one, many) => `${count.toLocaleString("en-GB")} ${count === 1 ? one : many}`;
|
|
199
|
+
|
|
200
|
+
/* ------------------------------------------------------------- not connected */
|
|
201
|
+
|
|
202
|
+
function notConnected(overview) {
|
|
203
|
+
const steps = [];
|
|
204
|
+
if (overview.credential === "none")
|
|
205
|
+
steps.push("Use Jelly with an account that offers memory, or paste the key from the memory site's account page into Memory key, in this plugin's settings.");
|
|
206
|
+
if (!overview.address) steps.push("If you use your own memory service, put its address in Memory address, in this plugin's settings.");
|
|
207
|
+
if (steps.length === 0) steps.push("Check Memory address and Memory key in this plugin's settings, then try again.");
|
|
208
|
+
return el("div", { class: "note", role: "status" },
|
|
209
|
+
el("h2", {}, "Memory isn't connected"),
|
|
210
|
+
p(overview.reason || "Memory can't be used from this agent yet."),
|
|
211
|
+
el("p", { class: "muted" }, "What to do:"),
|
|
212
|
+
el("ul", {}, steps.map((step) => el("li", {}, step))),
|
|
213
|
+
el("div", { class: "actions" }, button("Check again", () => open("overview"))),
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/* ------------------------------------------------------------------ overview */
|
|
218
|
+
|
|
219
|
+
/** Why Jelly gave no memory token, in words (the codes are Jelly's). */
|
|
220
|
+
const REFUSED = {
|
|
221
|
+
unavailable: "Jelly couldn't sign you in to memory just now",
|
|
222
|
+
not_enrolled: "This Jelly isn't linked to a Jelly account",
|
|
223
|
+
invalid_target: "Your Jelly account doesn't offer memory",
|
|
224
|
+
access_denied: "Your Jelly account is turned off",
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const SIGNED_IN = {
|
|
228
|
+
jelly: "Your Jelly account",
|
|
229
|
+
key: "The memory key saved in this plugin's settings",
|
|
230
|
+
option: "A key in this plugin's options",
|
|
231
|
+
none: "Nothing yet",
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
function captureRows(overview) {
|
|
235
|
+
const saved = typeof settings?.values?.capture === "boolean" ? settings.values.capture : null;
|
|
236
|
+
const rows = [
|
|
237
|
+
overview.capture
|
|
238
|
+
? "On: finished conversations are saved to your private memory."
|
|
239
|
+
: "Off: nothing is saved unless you ask an agent to remember it.",
|
|
240
|
+
];
|
|
241
|
+
if (saved !== null && saved !== overview.capture)
|
|
242
|
+
rows.push(`Saved as ${saved ? "on" : "off"}; this takes effect when the agent next starts.`);
|
|
243
|
+
rows.push("Change it with Remember conversations, in this plugin's settings.");
|
|
244
|
+
return rows.map((row, index) => el("div", index === 0 ? {} : { class: "meta" }, row));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function overviewTab() {
|
|
248
|
+
loading();
|
|
249
|
+
let overview;
|
|
250
|
+
try {
|
|
251
|
+
overview = await call("memory.overview");
|
|
252
|
+
} catch (error) {
|
|
253
|
+
return failed(error, () => open("overview"));
|
|
254
|
+
}
|
|
255
|
+
if (!overview.connected) return show(notConnected(overview));
|
|
256
|
+
const counts = overview.counts;
|
|
257
|
+
const signedIn = [SIGNED_IN[overview.credential] ?? SIGNED_IN.none];
|
|
258
|
+
if (overview.refused) signedIn.push(`${REFUSED[overview.refused] ?? "Jelly couldn't sign you in"}, so the saved key is used.`);
|
|
259
|
+
show(
|
|
260
|
+
p("Memory keeps your decisions, preferences and notes, so your agents can use them in later conversations."),
|
|
261
|
+
el("dl", {},
|
|
262
|
+
el("dt", {}, "Status"),
|
|
263
|
+
el("dd", {}, overview.health === "ok" ? "Connected and working" : el("span", {}, el("strong", { class: "danger" }, "Connected, but memory can't be reached just now. "), "Your agents will say so if they need it.")),
|
|
264
|
+
el("dt", {}, "Signed in with"),
|
|
265
|
+
el("dd", {}, signedIn.map((row, index) => el("div", index === 0 ? {} : { class: "meta" }, row))),
|
|
266
|
+
overview.address ? [el("dt", {}, "Address"), el("dd", {}, overview.address)] : null,
|
|
267
|
+
el("dt", {}, "Remember conversations"),
|
|
268
|
+
el("dd", {}, captureRows(overview)),
|
|
269
|
+
el("dt", {}, "Your space"),
|
|
270
|
+
el("dd", {}, overview.space?.name ?? "Your memory"),
|
|
271
|
+
el("dt", {}, "Remembered"),
|
|
272
|
+
el("dd", {},
|
|
273
|
+
counts
|
|
274
|
+
? [
|
|
275
|
+
el("div", {}, `${plural(counts.memories, "memory", "memories")}, ${plural(counts.conversations, "conversation", "conversations")}`),
|
|
276
|
+
el("div", { class: "meta" }, `${plural(counts.summaries, "summary", "summaries")}; ${counts.profile ? "a profile of you is kept" : "no profile yet"}`),
|
|
277
|
+
]
|
|
278
|
+
: "Not known",
|
|
279
|
+
),
|
|
280
|
+
),
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/* -------------------------------------------------------------------- spaces */
|
|
285
|
+
|
|
286
|
+
const KINDS = { personal: "Personal", shared: "Shared", unknown: "Other" };
|
|
287
|
+
const ROLES = { owner: "Owner", admin: "Admin", editor: "Editor", viewer: "Viewer" };
|
|
288
|
+
|
|
289
|
+
async function spacesTab() {
|
|
290
|
+
loading();
|
|
291
|
+
let answer;
|
|
292
|
+
try {
|
|
293
|
+
answer = await call("spaces.list");
|
|
294
|
+
} catch (error) {
|
|
295
|
+
return failed(error, () => open("spaces"));
|
|
296
|
+
}
|
|
297
|
+
const spaces = [...answer.spaces].sort((a, b) => (a.kind === "personal" ? -1 : b.kind === "personal" ? 1 : 0));
|
|
298
|
+
show(
|
|
299
|
+
spaces.length === 0
|
|
300
|
+
? p("No spaces yet. Your personal space appears here once memory sets it up.", "muted")
|
|
301
|
+
: el("ul", { class: "rows" },
|
|
302
|
+
spaces.map((space) =>
|
|
303
|
+
el("li", {},
|
|
304
|
+
el("div", { class: "grow" },
|
|
305
|
+
el("div", {}, space.kind === "personal" ? "Your personal space" : space.name),
|
|
306
|
+
el("div", { class: "meta" }, space.kind === "personal" ? "Only you and your agents can read it." : space.name === space.id ? space.id : ""),
|
|
307
|
+
),
|
|
308
|
+
el("span", { class: "badge" }, KINDS[space.kind] ?? KINDS.unknown),
|
|
309
|
+
space.role ? el("span", { class: "badge" }, ROLES[space.role] ?? space.role) : null,
|
|
310
|
+
),
|
|
311
|
+
),
|
|
312
|
+
),
|
|
313
|
+
p(spaces.some((space) => space.kind === "shared")
|
|
314
|
+
? "Who can use a shared space is managed on the memory site for now."
|
|
315
|
+
: "Team spaces will show here once your team has one.", "muted"),
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/* ---------------------------------------------------------------------- wiki */
|
|
320
|
+
|
|
321
|
+
async function wikiTab() {
|
|
322
|
+
loading();
|
|
323
|
+
let answer;
|
|
324
|
+
try {
|
|
325
|
+
answer = await call("wiki.list");
|
|
326
|
+
} catch (error) {
|
|
327
|
+
return failed(error, () => open("wiki"));
|
|
328
|
+
}
|
|
329
|
+
show(
|
|
330
|
+
answer.wikis.length === 0
|
|
331
|
+
? p("No wiki yet. Ask an agent to save a page to your wiki, and it shows here.", "muted")
|
|
332
|
+
: el("ul", { class: "rows" },
|
|
333
|
+
answer.wikis.map((wiki) =>
|
|
334
|
+
el("li", {},
|
|
335
|
+
el("div", { class: "grow" },
|
|
336
|
+
button(wiki.name, () => wikiPages(wiki), "link"),
|
|
337
|
+
el("div", { class: "meta" }, plural(wiki.pages, "page", "pages")),
|
|
338
|
+
),
|
|
339
|
+
),
|
|
340
|
+
),
|
|
341
|
+
),
|
|
342
|
+
p("Read only. To change a page, ask an agent.", "muted"),
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async function wikiPages(wiki) {
|
|
347
|
+
loading();
|
|
348
|
+
let answer;
|
|
349
|
+
try {
|
|
350
|
+
answer = await call("wiki.pages", { wiki: wiki.id });
|
|
351
|
+
} catch (error) {
|
|
352
|
+
return failed(error, () => wikiPages(wiki));
|
|
353
|
+
}
|
|
354
|
+
show(
|
|
355
|
+
el("div", { class: "actions" }, button("Back to wikis", () => open("wiki"))),
|
|
356
|
+
el("h2", { class: "spaced" }, wiki.name),
|
|
357
|
+
answer.pages.length === 0
|
|
358
|
+
? p("This wiki has no pages yet. New pages show once memory has read them in.", "muted")
|
|
359
|
+
: el("ul", { class: "rows" },
|
|
360
|
+
answer.pages.map((page) => el("li", {}, el("div", { class: "grow" }, button(page.title, () => wikiRead(wiki, page), "link"), el("div", { class: "meta" }, page.ref)))),
|
|
361
|
+
),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async function wikiRead(wiki, page) {
|
|
366
|
+
loading();
|
|
367
|
+
let answer;
|
|
368
|
+
try {
|
|
369
|
+
answer = await call("wiki.read", { wiki: wiki.id, ref: page.ref });
|
|
370
|
+
} catch (error) {
|
|
371
|
+
return failed(error, () => wikiRead(wiki, page));
|
|
372
|
+
}
|
|
373
|
+
show(
|
|
374
|
+
el("div", { class: "actions" }, button(`Back to ${wiki.name}`, () => wikiPages(wiki))),
|
|
375
|
+
el("h2", { class: "spaced" }, page.title),
|
|
376
|
+
el("div", { class: "page" }, answer.content),
|
|
377
|
+
answer.clipped ? p("This page is long, so only its start is shown. Ask an agent to read the rest.", "muted") : null,
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/* -------------------------------------------------------------------- recent */
|
|
382
|
+
|
|
383
|
+
// The message the person sends. The id is what mem_forget needs; it finds the
|
|
384
|
+
// memory under the agent the person is talking to, which is the agent these
|
|
385
|
+
// views read under, so no agent name is written.
|
|
386
|
+
function forgetMessage(memory) {
|
|
387
|
+
const words = memory.text.replace(/\s+/g, " ").trim();
|
|
388
|
+
const start = words.length > 80 ? `${words.slice(0, 79)}…` : words.replace(/[.!?;:,]+$/, "");
|
|
389
|
+
return `Forget this memory: “${start}” (${memory.id}).`;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function recentRow(memory, status) {
|
|
393
|
+
const forget = button("Forget…", async () => {
|
|
394
|
+
try {
|
|
395
|
+
await view.openAgent(forgetMessage(memory));
|
|
396
|
+
status.textContent = "Your message is ready. Send it, and the agent forgets this memory.";
|
|
397
|
+
} catch (error) {
|
|
398
|
+
status.textContent = `Couldn't start the message: ${error.message}`;
|
|
399
|
+
}
|
|
400
|
+
fit();
|
|
401
|
+
});
|
|
402
|
+
forget.setAttribute("aria-label", `Forget: ${memory.text.slice(0, 60)}`);
|
|
403
|
+
return el("li", {},
|
|
404
|
+
el("div", { class: "grow" },
|
|
405
|
+
el("div", {}, memory.text),
|
|
406
|
+
el("div", { class: "meta" }, [memory.type.replaceAll("_", " "), date(memory.at)].filter(Boolean).join(" · ")),
|
|
407
|
+
),
|
|
408
|
+
forget,
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
async function recentTab(offset = 0, list = null) {
|
|
413
|
+
if (list === null) loading();
|
|
414
|
+
let answer;
|
|
415
|
+
try {
|
|
416
|
+
answer = await call("memories.recent", { offset });
|
|
417
|
+
} catch (error) {
|
|
418
|
+
if (list === null) return failed(error, () => open("recent"));
|
|
419
|
+
list.after(p(`Couldn't read more: ${error.message}`, "danger"));
|
|
420
|
+
return fit();
|
|
421
|
+
}
|
|
422
|
+
const status = el("p", { class: "muted", role: "status" });
|
|
423
|
+
if (list === null) {
|
|
424
|
+
if (answer.items.length === 0)
|
|
425
|
+
return show(p("Nothing remembered yet. Memories show here as your agents save them.", "muted"));
|
|
426
|
+
list = el("ul", { class: "rows" });
|
|
427
|
+
show(p("Your newest memories, newest first. Forget starts a message for you to send."), status, list);
|
|
428
|
+
} else {
|
|
429
|
+
panel.querySelector(".more")?.remove();
|
|
430
|
+
}
|
|
431
|
+
list.append(...answer.items.map((memory) => recentRow(memory, panel.querySelector("[role=status]") ?? status)));
|
|
432
|
+
if (answer.more) {
|
|
433
|
+
const more = el("div", { class: "actions more" }, button("Show more", () => recentTab(offset + 50, list)));
|
|
434
|
+
list.after(more);
|
|
435
|
+
} else if (offset > 0 || answer.items.length === 50) {
|
|
436
|
+
list.after(p("That's as far back as this list goes. Ask an agent to find older memories.", "muted more"));
|
|
437
|
+
}
|
|
438
|
+
fit();
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/* ---------------------------------------------------------------------- tabs */
|
|
442
|
+
|
|
443
|
+
const SCREENS = { overview: overviewTab, spaces: spacesTab, wiki: wikiTab, recent: () => recentTab() };
|
|
444
|
+
|
|
445
|
+
function open(name) {
|
|
446
|
+
shown = name;
|
|
447
|
+
for (const tab of tabs) {
|
|
448
|
+
const selected = tab.dataset.tab === name;
|
|
449
|
+
tab.setAttribute("aria-selected", String(selected));
|
|
450
|
+
tab.tabIndex = selected ? 0 : -1;
|
|
451
|
+
if (selected) panel.setAttribute("aria-labelledby", tab.id);
|
|
452
|
+
}
|
|
453
|
+
if (view) SCREENS[name]();
|
|
454
|
+
}
|
|
455
|
+
tabs.forEach((tab, index) => {
|
|
456
|
+
tab.addEventListener("click", () => open(tab.dataset.tab));
|
|
457
|
+
tab.addEventListener("keydown", (event) => {
|
|
458
|
+
const step = event.key === "ArrowRight" ? 1 : event.key === "ArrowLeft" ? -1 : 0;
|
|
459
|
+
if (step === 0) return;
|
|
460
|
+
const next = tabs[(index + step + tabs.length) % tabs.length];
|
|
461
|
+
next.focus();
|
|
462
|
+
open(next.dataset.tab);
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
loading("Connecting to Jelly…");
|
|
467
|
+
jellyView().then(async (client) => {
|
|
468
|
+
view = client;
|
|
469
|
+
view.on((push) => {
|
|
470
|
+
if (push.kind === "data.changed") settings = push.data;
|
|
471
|
+
if (push.kind === "rpc.waiting" && !waiting) {
|
|
472
|
+
waiting = true;
|
|
473
|
+
panel.prepend(p("Waiting for you to allow this in Jelly.", "muted"));
|
|
474
|
+
fit();
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
try {
|
|
478
|
+
settings = await view.data();
|
|
479
|
+
} catch {
|
|
480
|
+
settings = null;
|
|
481
|
+
}
|
|
482
|
+
new ResizeObserver(fit).observe(document.body);
|
|
483
|
+
open(shown);
|
|
484
|
+
});
|
|
485
|
+
</script>
|
|
486
|
+
</body>
|
|
487
|
+
</html>
|