Repository navigation
Expand file tree
/
Copy pathcontext.js
More file actions
356 lines (326 loc) · 19.8 KB
/
Copy pathcontext.js
File metadata and controls
356 lines (326 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Context page: what agents keep about a project (context.py, local context.db). Read-first: a "where things stand"
// summary, facts, decisions and recent activity in plain words; editing opens only when asked. Not polled.
import { $, api, esc, icon, loadingHTML, toast, toastError } from "./ui.js";
import { S, findSession, machineProjects, orderedProjects, showSession } from "./app.js";
let selected = null; // a shelldeck project's id, or the name of a project only the context store knows
let choices = []; // [{value, label}] for the project picker
let handoffs = []; // the selected project's hand-offs, newest first
let last = null; // the loaded context package
let editing = false; // the task/state form is open
let search = null; // {q, agent, model, results, expanded} while showing search results
let helpers = null; // installed agents and their models, for "widen the search with" (/api/context/recall-agents)
let pickedAgent = null; // the user's choice in the search box ("off" = plain keywords)
let pickedModel = "";
const TASK_STATUSES = [["TODO", "To do"], ["IN_PROGRESS", "In progress"], ["BLOCKED", "Blocked"], ["DONE", "Done"], ["CANCELLED", "Cancelled"]];
const STATUS_LABEL = Object.fromEntries(TASK_STATUSES);
export async function renderContext(el) {
if (!el.dataset.wired) {
el.dataset.wired = "1";
el.addEventListener("click", (e) => onClick(e, el));
el.addEventListener("submit", (e) => onSubmit(e, el));
el.addEventListener("change", (e) => {
if (e.target.matches("[data-recall-agent]")) {
pickedAgent = e.target.value;
pickedModel = "";
$("[data-recall-model]", el)?.replaceWith(modelSelect());
return;
}
if (e.target.matches("[data-recall-model]")) {
pickedModel = e.target.value;
return;
}
if (!e.target.matches("[data-ctx-project]")) return;
selected = e.target.value;
editing = false;
search = null;
renderContext(el);
});
}
const known = await api("/api/context/projects").then((r) => r.projects).catch(() => []);
if (!helpers) {
const r = await api("/api/context/recall-agents").catch(() => ({ agents: [], default: "off" }));
helpers = r.agents;
const def = r.default === "auto" ? helpers[0]?.agent : r.default;
pickedAgent = helpers.some((h) => h.agent === def) ? def : "off";
}
// the sidebar's machine and workspace; ids, because two workspaces can each have an "api"
const mine = machineProjects().filter((p) => !p.remote_id);
const taken = new Set(orderedProjects().map((p) => p.name));
choices = [...mine.map((p) => ({ value: p.id, label: p.name })), ...(S.workspace ? [] : known.filter((p) => !taken.has(p.name)).map((p) => ({ value: p.name, label: p.name })))];
if (!choices.some((c) => c.value === selected)) {
const focused = findSession(S.focused)?.project?.id; // the terminal you were just in
selected = (choices.find((c) => c.value === focused) || choices[0])?.value || null;
}
last = null;
handoffs = [];
if (selected) {
[last, handoffs] = await Promise.all([
api(`/api/context?project=${encodeURIComponent(selected)}`).catch(() => null),
mine.some((p) => p.id === selected) ? api(`/api/handoffs?project_id=${selected}`).then((r) => r.handoffs).catch(() => []) : [],
]);
}
el.innerHTML = `${header()}
${!selected ? empty("No projects yet", "Add a project from the sidebar. Agents fill this page as they work.")
: search ? results()
: !last ? empty("Nothing recorded yet", `Agents add to it with <code>sd remember</code> and <code>sd task update</code>, or add a fact below.`)
: body(last)}`;
}
function header() {
const related = last?.related_projects || [];
return `<div class="cx-head">
<div class="cx-title"><h1>Context</h1>
${choices.length ? `<select class="cx-project" data-ctx-project aria-label="Project">${choices.map((c) => `<option value="${esc(c.value)}" ${c.value === selected ? "selected" : ""}>${esc(c.label)}</option>`).join("")}</select>` : ""}
<button class="icon-btn" data-cx="refresh" title="Refresh" aria-label="Refresh">${icon("refresh")}</button></div>
${selected ? `<form class="cx-search" data-form="recall" role="search">
${icon("search")}<input name="q" value="${esc(search?.q || "")}" placeholder="Search what every project knows…" aria-label="Search context" />
${helpers?.length ? `<span class="cx-widen" title="The chosen agent and model suggest related words before searching (a few tokens per new search; cached)">
<select data-recall-agent aria-label="Widen the search with"><option value="off">Exact words</option>${helpers.map((h) => `<option value="${h.agent}" ${h.agent === pickedAgent ? "selected" : ""}>+ ${esc(h.label)}</option>`).join("")}</select>
${modelSelect().outerHTML}</span>` : ""}
</form>` : ""}
${related.length ? `<p class="cx-related faint">Related: ${related.map((r) => `<b>${esc(r.name)}</b> <span>(${esc(r.relation.replace(/-/g, " "))})</span>`).join(", ")}</p>` : ""}
</div>`;
}
function empty(title, text) {
return `<div class="card cx-empty"><h2>${title}</h2><p class="faint">${text}</p>
<p class="faint small">What agents remember about a project, so the next one picks up where the last stopped. Kept only on this computer.</p>
<form data-form="remember" class="cx-add"><input name="text" placeholder="Add a fact about this project, then press Enter" aria-label="New fact" /></form></div>`;
}
function body(c) {
return `<div class="cx-grid">
<div class="cx-main">${standing(c)}${facts(c)}</div>
<div class="card cx-side">${delegated()}${decisions(c)}${activity(c)}${relate(c)}</div>
</div>`;
}
// ---------------------------------------------------------------- where things stand
function standing(c) {
const t = c.task || {};
const s = c.state || {};
const g = c.git_state || {};
if (editing) return editForm(t, s);
const tests = s.tests ? `<span class="cx-pill ${/^failed/i.test(s.tests) ? "bad" : "good"}">${/^failed/i.test(s.tests) ? "Tests failing" : "Tests passing"}</span>` : "";
const rows = [
["Next", s.next_action],
["Now", s.step],
["Tests", s.tests && s.tests.replace(/^(passed|failed):\s*/i, "")],
["Last error", s.last_error],
].filter(([, v]) => v);
return `<section class="card cx-standing" aria-label="Where things stand">
<div class="cx-standing-top">
<div><span class="cx-label">Working on</span>
<h2>${t.title ? esc(t.title) : `<span class="faint">No task set</span>`}</h2></div>
<div class="cx-chips">${t.status ? `<span class="cx-pill">${esc(STATUS_LABEL[t.status] || t.status)}</span>` : ""}${tests}
<button class="btn sm" data-cx="edit">${icon("pencil")}Edit</button></div>
</div>
${t.objective ? `<p class="cx-objective">${esc(t.objective)}</p>` : ""}
${rows.length ? `<dl class="cx-rows">${rows.map(([k, v]) => `<dt>${k}</dt><dd>${esc(v)}</dd>`).join("")}</dl>` : `<p class="faint">Nothing recorded about progress yet.</p>`}
<p class="cx-meta faint">${g.branch ? `${esc(g.branch)} · ${g.dirty ? `${g.changed.length} file${g.changed.length === 1 ? "" : "s"} changed` : "clean"}` : "Not a git repository"}${s.updated_at ? ` · updated ${ago(s.updated_at)}${s.agent ? ` by ${esc(s.agent)}` : ""}` : ""}</p>
</section>`;
}
function editForm(t, s) {
const f = (name, label, value, hint = "") => `<label class="field"><span>${label}</span><input name="${name}" value="${esc(value || "")}" placeholder="${esc(hint)}" /></label>`;
return `<form class="card cx-standing" data-form="state" aria-label="Edit where things stand">
<h2>Where things stand</h2>
${f("task", "Working on", t.title, "e.g. Add login")}
<div class="field-row">
<label class="field"><span>Status</span><select name="status">${TASK_STATUSES.map(([v, l]) => `<option value="${v}" ${v === (t.status || "IN_PROGRESS") ? "selected" : ""}>${l}</option>`).join("")}</select></label>
${f("next_action", "Next step", s.next_action, "What the next agent should do first")}
</div>
${f("step", "Doing now", s.step)}
<label class="field"><span>Goal</span><textarea name="objective" rows="2">${esc(t.objective || "")}</textarea></label>
<details class="cx-more-fields"><summary class="faint">Plan, tests and errors</summary>
<label class="field"><span>Plan</span><textarea name="plan" rows="3">${esc(t.plan || "")}</textarea></label>
${f("tests", "Tests", s.tests, "e.g. passed: pytest -q")}
${f("last_error", "Last error", s.last_error)}
</details>
<div class="row"><button class="btn primary">Save</button><button type="button" class="btn" data-cx="cancel">Cancel</button></div>
</form>`;
}
// ---------------------------------------------------------------- facts & decisions
function facts(c) {
const badge = { STALE: ["warn", "May be out of date"], VERIFIED: ["good", "Checked"], REVIEWED: ["good", "Reviewed"] };
const items = c.memory.map((k) => {
const b = badge[k.status];
const src = k.sources.length ? `<span class="faint"> · ${k.sources.map((x) => esc(x.file_path)).join(", ")}</span>` : "";
const verify = ["VERIFIED", "REVIEWED"].includes(k.status) ? "" : `<button class="icon-btn sm" data-verify="${k.id}" title="${k.status === "STALE" ? "Still true" : "Mark as checked"}" aria-label="Mark as checked">${icon("eye")}</button>`;
return `<li class="cx-item">
<div class="cx-text">${b ? `<span class="cx-tag ${b[0]}">${b[1]}</span> ` : ""}${esc(k.content)}${src}</div>
<div class="cx-actions">${verify}<button class="icon-btn sm" data-invalidate="${k.id}" title="Not true any more" aria-label="Remove">${icon("trash")}</button></div>
</li>`;
}).join("");
return `<section class="card cx-section" aria-label="Facts">
${adder("Facts", c.memory.length, "remember", "text", "A fact about this project, then Enter")}
${items ? `<ul class="cx-list">${items}</ul>` : `<p class="faint">No facts yet. Agents add what they learn; you can too.</p>`}
</section>`;
}
function decisions(c) {
const items = c.decisions.map((d) => `<li class="cx-item"><div class="cx-text"><b>${esc(d.title)}</b>${d.reason ? `<br><span class="faint">${esc(d.reason)}</span>` : ""}</div>
<span class="faint small">${esc(d.created_at.slice(0, 10))}</span></li>`).join("");
return `<section class="cx-section" aria-label="Decisions">
${adder("Decisions", c.decisions.length, "decide", "title", "A settled choice, then Enter")}
${items ? `<ul class="cx-list">${items}</ul>` : `<p class="faint">Settled choices go here, so no one reopens them.</p>`}
</section>`;
}
/** A section heading with its count and an Add that opens the one field (the page re-renders shut after saving). */
function adder(title, count, form, field, hint) {
return `<details class="cx-adder"><summary><h2>${title} <span class="faint">${count || ""}</span></h2><span class="cx-plus">${icon("plus")}Add</span></summary>
<form data-form="${form}" class="cx-add"><input name="${field}" placeholder="${esc(hint)}" aria-label="${esc(hint)}" /></form></details>`;
}
// ---------------------------------------------------------------- hand-offs
/** Work handed between agents in this project: everything still open, then the latest results. */
function delegated() {
const open = handoffs.filter((h) => h.status === "open");
if (!open.length) return "";
const items = open.map((h) => `<li class="cx-item ${findSession(h.to_sid) ? "cx-link" : ""}" ${findSession(h.to_sid) ? `data-open-sid="${esc(h.to_sid)}" title="Open ${esc(h.to_nick || "")}'s terminal"` : ""}>
<div class="cx-text">${esc(h.from_nick || "You")} → <b>${esc(h.to_nick || "")}</b><br><span class="faint">${esc(clip(h.task, 110))}</span></div>
<span class="faint small">${ago(h.updated_at)}</span></li>`).join("");
return `<section class="cx-section" aria-label="Open hand-offs">
<h2>In progress <span class="faint">${open.length}</span></h2>
<ul class="cx-list">${items}</ul>
</section>`;
}
const clip = (s, n = 160) => (s.length > n ? `${s.slice(0, n)}…` : s);
// ---------------------------------------------------------------- activity in plain words
function describe(e) {
let p = {};
try {
p = JSON.parse(e.payload || "{}");
} catch {}
const who = e.agent ? cap(e.agent) : "Someone";
switch (e.type) {
case "SESSION_START": return [`${who} started`, ""];
case "SESSION_STOP": return [`${who} finished`, ""];
case "TEST_RESULT": return [p.ok ? "Tests passed" : "Tests failed", p.command, p.ok ? "good" : "bad"];
case "ERROR": return ["A command failed", p.command, "bad"];
case "COMMAND": return ["Ran a command", p.command];
case "FILE_EDIT": return ["Edited a file", p.file];
case "FILE_CREATE": return ["Created a file", p.file];
case "NOTE": case "DISCOVERY": return ["Added a fact", ""];
case "DECISION": return ["Recorded a decision", ""];
case "TASK_START": return ["Started a task", ""];
case "TASK_COMPLETE": return ["Finished the task", "", "good"];
case "TASK_UPDATE": return ["Updated progress", ""];
case "HANDOFF": return [`${cap(p.from || "someone")} handed a task to ${p.to || "an agent"}`, p.text];
case "HANDOFF_DONE": return [`${p.to || "An agent"}'s hand-off: ${p.status || "closed"}`, p.text, p.status === "done" ? "good" : "bad"];
default: return [cap(e.type.toLowerCase().replace(/_/g, " ")), ""];
}
}
function activity(c) {
if (!c.recent_events.length) return "";
const items = c.recent_events.slice(0, 6).map((e) => {
const [what, detail, tone] = describe(e);
return `<li class="cx-event ${tone || ""}"><span class="cx-dot" aria-hidden="true"></span><div><span>${esc(what)}</span>${detail ? `<code>${esc(detail)}</code>` : ""}</div><time class="faint">${ago(e.timestamp)}</time></li>`;
}).join("");
return `<section class="cx-section" aria-label="Recent activity"><h2>Recent activity</h2><ul class="cx-events">${items}</ul></section>`;
}
function relate(c) {
const others = orderedProjects().map((p) => p.name).filter((n) => n !== c.project.name);
if (!others.length) return "";
return `<details class="cx-section cx-relate"><summary><h2>Link another project</h2></summary>
<p class="faint">Linked projects rank higher in search, so an agent here finds their knowledge first.</p>
<form data-form="relate" class="cx-add">
<select name="target" aria-label="Project">${others.map((n) => `<option>${esc(n)}</option>`).join("")}</select>
<select name="relation" aria-label="How they relate">${[["related-to", "is related to"], ["depends-on", "depends on"], ["uses-pattern", "uses a pattern from"], ["shares-database-with", "shares a database with"]].map(([v, l]) => `<option value="${v}">${l}</option>`).join("")}</select>
<button class="btn sm">Link</button></form></details>`;
}
// ---------------------------------------------------------------- search
function results() {
const s = search;
const also = s.expanded?.length ? `<p class="faint small">Also searched for ${s.expanded.map(esc).join(", ")} (suggested by ${esc(s.agent)} ${esc(s.model || "")})</p>`
: s.agent ? `<p class="faint small">${esc(s.agent)} suggested no extra words (or couldn't run); matched the words as typed.</p>` : "";
const items = s.results.map((r) => `<li class="cx-item"><div class="cx-text">
<span class="cx-tag">${esc(r.project || "")}</span> <b>${esc(r.title)}</b>${r.status === "STALE" ? ` <span class="cx-tag warn">may be out of date</span>` : ""}
${r.snippet !== r.title ? `<br><span>${esc(r.snippet)}</span>` : ""}
${r.sources.length ? `<br><span class="faint small">from ${r.sources.map(esc).join(", ")}</span>` : ""}</div></li>`).join("");
return `<section class="card cx-section">
<div class="cx-results-head"><h2>${s.results.length} result${s.results.length === 1 ? "" : "s"} for “${esc(s.q)}”</h2><button class="btn sm" data-cx="back">Back</button></div>
${also}
${items ? `<ul class="cx-list">${items}</ul><p class="faint small">From other projects these are references: check the source and adapt, don't copy.</p>` : `<p class="faint">Nothing found. Try other words, or let an agent widen the search.</p>`}
</section>`;
}
// ---------------------------------------------------------------- events
async function onClick(e, el) {
const act = e.target.closest("[data-cx]")?.dataset.cx;
if (act === "edit" || act === "cancel") {
editing = act === "edit";
return renderContext(el);
}
if (act === "back") {
search = null;
return renderContext(el);
}
if (act === "refresh") return renderContext(el);
const sid = e.target.closest("[data-open-sid]")?.dataset.openSid;
if (sid) return showSession(sid);
const id = e.target.closest("[data-verify]")?.dataset.verify || e.target.closest("[data-invalidate]")?.dataset.invalidate;
if (!id) return;
const invalid = !!e.target.closest("[data-invalidate]");
const status = invalid ? "INVALIDATED" : last?.memory.find((k) => k.id === id)?.status === "STALE" ? "REVIEWED" : "VERIFIED";
try {
await api(`/api/context/knowledge/${id}/status`, { method: "POST", body: { status } });
if (invalid) toast({ title: "Removed from facts" });
renderContext(el);
} catch (err) {
toastError(err);
}
}
async function onSubmit(e, el) {
const form = e.target.closest("form[data-form]");
if (!form) return;
e.preventDefault();
const f = Object.fromEntries(new FormData(form).entries());
const where = { project: selected };
try {
if (form.dataset.form === "recall") {
if (!f.q.trim()) {
search = null;
return renderContext(el);
}
const params = new URLSearchParams({ q: f.q, project: selected || "", agent: pickedAgent || "off" });
if (pickedAgent !== "off" && pickedModel) params.set("model", pickedModel);
form.nextElementSibling?.classList.contains("loading-row") || form.insertAdjacentHTML("afterend", loadingHTML(pickedAgent !== "off" ? "Searching (widening the query with the agent)…" : "Searching…"));
const r = await api(`/api/context/recall?${params}`);
search = { q: f.q, ...r };
return renderContext(el);
}
if (form.dataset.form === "state") {
await api("/api/context/state", { method: "POST", body: { ...where, ...f } });
editing = false;
}
if (form.dataset.form === "remember") {
if (!f.text.trim()) return;
await api("/api/context/memory", { method: "POST", body: { ...where, text: f.text } });
}
if (form.dataset.form === "decide") {
if (!f.title.trim()) return;
await api("/api/context/decisions", { method: "POST", body: { ...where, title: f.title } });
}
if (form.dataset.form === "relate") await api("/api/context/relationships", { method: "POST", body: { ...where, target: f.target, relation: f.relation } });
toast({ title: "Saved" });
await renderContext(el);
const again = $(`form[data-form="${form.dataset.form}"] input`, el);
again?.closest("details")?.setAttribute("open", "");
again?.focus();
} catch (err) {
toastError(err);
}
}
// ---------------------------------------------------------------- helpers
/** Models of the agent picked in the search box; its cheapest first (the default). Hidden for exact-word search. */
function modelSelect() {
const sel = document.createElement("select");
sel.dataset.recallModel = "";
sel.setAttribute("aria-label", "Model");
const h = helpers?.find((x) => x.agent === pickedAgent);
sel.hidden = !h;
sel.innerHTML = (h?.models || []).map((m) => `<option value="${esc(m)}" ${m === (pickedModel || h.default) ? "selected" : ""}>${esc(m)}${m === h.default ? " (cheapest)" : ""}</option>`).join("");
return sel;
}
const cap = (s) => s.charAt(0).toUpperCase() + s.slice(1);
function ago(iso) {
const s = Math.max(0, (Date.now() - Date.parse(iso)) / 1000);
if (!Number.isFinite(s)) return "";
if (s < 60) return "just now";
if (s < 3600) return `${Math.floor(s / 60)} min ago`;
if (s < 86400) return `${Math.floor(s / 3600)} h ago`;
return `${Math.floor(s / 86400)} d ago`;
}