The panel stored notes, todos and plans inside one shared JSON file that six unrelated domains also wrote to, synchronised itself through window CustomEvents, and could only read plans. It is now Project knowledge: server-owned storage with explicit routes, a store with rollback, a section sidebar, plans that open and edit in place, and search across all of it. Notes and plans the user pins travel with every message sent in that project. Pinning is project state, not an attachment to one message, so it holds until unpinned and the work status panel names what is riding along and can detach it. Agent memory is added alongside, in two scopes: what is true about the user, and what is true about this codebase. The split is not cosmetic — a wrong project fact costs one project and is noticed, while a wrong global fact quietly shapes every session everywhere and the user has no code to check it against. It stays separate from notes so an agent mistake cannot land in what the user wrote. Sessions receive an index of titles only; bodies are read on demand, because an index carrying full text grows until it crowds out the conversation. Deciding what a session must be told, and whether it has been told, now lives on the server. The client owned it before, which meant sessions started without a UI — scheduled tasks, sessions the agent dispatches — received nothing at all, and a tab's record of what it had sent outlived the conversation: after compaction the agent no longer held the block while the tab went on believing it did. What was delivered is recorded in the session's own metadata, and compaction restores it through the runtime that already restores pinned messages, in the same turn. Agent memory ships dark behind OPENCHAMBER_MEMORY_ENABLE: unset, there is no tool, no routes, no session index, no settings row and no panel tab. Absent rather than switched off, so nothing invites turning on a feature that has not been announced. Pinned notes and plans are unaffected and ship as normal.
88 lines
3.5 KiB
JavaScript
88 lines
3.5 KiB
JavaScript
/**
|
|
* What a session still owes in project knowledge, and the record that it was
|
|
* delivered.
|
|
*
|
|
* Two calls rather than one, because only the sender knows whether the message
|
|
* carrying the block actually went out. Handing over the text and recording it
|
|
* as delivered in the same request would leave a failed send believing the
|
|
* agent has context it never received.
|
|
*
|
|
* The body parser is attached per route: there is no global one, because the
|
|
* generic OpenCode proxy needs an unread request stream.
|
|
*/
|
|
|
|
import express from 'express';
|
|
|
|
const parseJsonBody = express.json({ limit: '1mb' });
|
|
|
|
const isRecord = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
|
|
const asNonEmptyString = (value) => (
|
|
typeof value === 'string' && value.trim().length > 0 ? value.trim() : ''
|
|
);
|
|
|
|
export const registerSessionKnowledgeRoutes = (app, dependencies) => {
|
|
const { sessionKnowledgeRuntime } = dependencies;
|
|
|
|
/**
|
|
* Answers with the text to attach and the signature to report back once it
|
|
* has gone. An empty text means the session is already carrying it.
|
|
*/
|
|
app.get('/api/session-knowledge', async (req, res) => {
|
|
const directory = asNonEmptyString(req.query.directory);
|
|
const sessionId = asNonEmptyString(req.query.sessionId);
|
|
if (!directory) {
|
|
return res.status(400).json({ error: 'directory is required' });
|
|
}
|
|
|
|
try {
|
|
const pending = sessionId
|
|
? await sessionKnowledgeRuntime.resolvePendingForSession(sessionId, directory)
|
|
// A session that does not exist yet — a draft about to be created —
|
|
// has been told nothing, so everything is still owed.
|
|
: await sessionKnowledgeRuntime.resolvePending(directory, '');
|
|
return res.json(pending);
|
|
} catch (error) {
|
|
// Never fails the caller's send: a message without its background is far
|
|
// better than no message at all.
|
|
return res.json({ text: '', signature: '', unavailable: true, reason: error?.message ?? 'unknown' });
|
|
}
|
|
});
|
|
|
|
/** Counts and names for the work status panel; assembles no text. */
|
|
app.get('/api/session-knowledge/summary', async (req, res) => {
|
|
const directory = asNonEmptyString(req.query.directory);
|
|
if (!directory) {
|
|
return res.json({ notes: [], plans: [], memory: { global: 0, project: 0 } });
|
|
}
|
|
try {
|
|
return res.json(await sessionKnowledgeRuntime.collectSummary(directory));
|
|
} catch {
|
|
// A panel that cannot read this shows nothing rather than an error.
|
|
return res.json({ notes: [], plans: [], memory: { global: 0, project: 0 } });
|
|
}
|
|
});
|
|
|
|
app.post('/api/session-knowledge/delivered', parseJsonBody, async (req, res) => {
|
|
const body = req.body;
|
|
if (!isRecord(body)) {
|
|
return res.status(400).json({ error: 'Body must be an object' });
|
|
}
|
|
const sessionId = asNonEmptyString(body.sessionId);
|
|
const directory = asNonEmptyString(body.directory);
|
|
const signature = asNonEmptyString(body.signature);
|
|
if (!sessionId || !directory || !signature) {
|
|
return res.status(400).json({ error: 'sessionId, directory and signature are required' });
|
|
}
|
|
|
|
try {
|
|
await sessionKnowledgeRuntime.recordDelivered(sessionId, directory, signature);
|
|
return res.json({ recorded: true });
|
|
} catch (error) {
|
|
// The message is already sent; failing here only means the block may be
|
|
// sent once more, which is far better than reporting the send as failed.
|
|
return res.json({ recorded: false, reason: error?.message ?? 'unknown' });
|
|
}
|
|
});
|
|
};
|