Files
openchamber/packages/ui/src/lib/surfaces/registry.ts
T
Bohdan Triapitsyn 34e8a24b20 feat(knowledge): rebuild the project notes panel as Project knowledge (#2973)
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.
2026-08-18 02:59:04 +03:00

228 lines
6.9 KiB
TypeScript

import type { IconName } from '@/components/icon/icons';
import type { I18nKey } from '@/lib/i18n';
import type { ContextPanelMode } from '@/stores/useUIStore';
export type ContextSurfaceId =
| 'editor'
| 'git'
| 'pr'
| 'diff'
| 'walkthrough'
| 'terminal'
| 'plan'
| 'notes'
| 'context'
| 'browser'
| 'chat';
export type ContextSurfaceDescriptor = {
id: ContextSurfaceId;
/** The context panel tab mode this surface activates. 1:1 in the current model. */
mode: ContextPanelMode;
icon: IconName;
labelKey: I18nKey;
/**
* 'always' surfaces can be opened empty from the rail.
* 'has-content' surfaces are content-driven: they need an existing tab of
* their mode (a split session, a diff to show) and stay hidden on the rail
* until one exists.
*/
availability: 'always' | 'has-content';
/** Short tooltip explanation shown on the rail. */
descriptionKey: I18nKey;
/**
* Default panel width as a fraction of the available content area, used
* until the user manually resizes this surface.
*/
defaultWidthFraction: number;
};
export const CONTEXT_SURFACES: readonly ContextSurfaceDescriptor[] = [
{
id: 'context',
descriptionKey: 'contextRail.surface.context.description',
defaultWidthFraction: 0.45,
mode: 'context',
icon: 'donut-chart-fill',
labelKey: 'contextPanel.mode.context',
availability: 'always',
},
{
id: 'git',
descriptionKey: 'contextRail.surface.git.description',
defaultWidthFraction: 2 / 5,
mode: 'git',
icon: 'git-branch',
labelKey: 'layout.rightSidebar.git',
availability: 'always',
},
{
id: 'pr',
descriptionKey: 'contextRail.surface.pr.description',
defaultWidthFraction: 0.45,
mode: 'pr',
icon: 'github',
labelKey: 'contextPanel.mode.pr',
availability: 'always',
},
{
id: 'diff',
descriptionKey: 'contextRail.surface.diff.description',
defaultWidthFraction: 3 / 5,
mode: 'diff',
icon: 'arrow-left-right',
labelKey: 'contextPanel.mode.diff',
availability: 'always',
},
{
id: 'walkthrough',
descriptionKey: 'contextRail.surface.walkthrough.description',
defaultWidthFraction: 3 / 5,
mode: 'walkthrough',
icon: 'route',
labelKey: 'contextPanel.mode.walkthrough',
availability: 'always',
},
{
id: 'editor',
descriptionKey: 'contextRail.surface.editor.description',
defaultWidthFraction: 3 / 5,
mode: 'file',
icon: 'braces',
labelKey: 'contextPanel.mode.files',
availability: 'always',
},
{
id: 'terminal',
descriptionKey: 'contextRail.surface.terminal.description',
defaultWidthFraction: 3 / 5,
mode: 'terminal',
icon: 'terminal-box',
labelKey: 'layout.mainTab.terminal',
availability: 'always',
},
{
id: 'notes',
descriptionKey: 'contextRail.surface.notes.description',
// As wide as the files surface: this panel now carries a sidebar and a
// content column, and a third of the window leaves the content column too
// narrow to read a note in.
defaultWidthFraction: 3 / 5,
mode: 'notes',
icon: 'book-marked',
labelKey: 'contextRail.surface.notes',
availability: 'always',
},
{
id: 'plan',
descriptionKey: 'contextRail.surface.plan.description',
defaultWidthFraction: 0.45,
mode: 'plan',
icon: 'file-text',
labelKey: 'contextPanel.mode.plan',
availability: 'always',
},
{
id: 'browser',
descriptionKey: 'contextRail.surface.browser.description',
defaultWidthFraction: 0.45,
mode: 'browser',
icon: 'global',
labelKey: 'contextPanel.mode.browser',
availability: 'always',
},
{
id: 'chat',
descriptionKey: 'contextRail.surface.chat.description',
defaultWidthFraction: 0.45,
mode: 'chat',
icon: 'chat-4',
labelKey: 'contextPanel.mode.chat',
availability: 'has-content',
},
];
const SURFACE_BY_ID = new Map(CONTEXT_SURFACES.map((surface) => [surface.id, surface]));
const FRACTION_BY_MODE = new Map(CONTEXT_SURFACES.map((surface) => [surface.mode, surface.defaultWidthFraction]));
// Tablet width and up: below this the walkthrough cannot show a stop and its
// code side by side, which is the whole point of the surface.
export const WALKTHROUGH_MIN_WIDTH = 768;
export const getContextSurfaceWidthFraction = (mode: ContextPanelMode): number => {
return FRACTION_BY_MODE.get(mode) ?? 1 / 2;
};
const isContextSurfaceId = (value: unknown): value is ContextSurfaceId => {
return typeof value === 'string' && SURFACE_BY_ID.has(value as ContextSurfaceId);
};
/**
* Applies a persisted user reorder on top of the default registry order:
* unknown ids are dropped, missing surfaces are appended in default order.
*/
export const sortContextSurfaces = (railOrder: readonly string[]): ContextSurfaceDescriptor[] => {
const ordered: ContextSurfaceDescriptor[] = [];
const seen = new Set<ContextSurfaceId>();
for (const id of railOrder) {
if (!isContextSurfaceId(id) || seen.has(id)) {
continue;
}
const surface = SURFACE_BY_ID.get(id);
if (surface) {
seen.add(id);
ordered.push(surface);
}
}
for (const surface of CONTEXT_SURFACES) {
if (!seen.has(surface.id)) {
ordered.push(surface);
}
}
return ordered;
};
type VisibleRailSurfacesOptions = {
railOrder: readonly string[];
planModeEnabled: boolean;
isVSCode: boolean;
screenWidth: number;
tabs: readonly { mode: ContextPanelMode }[];
};
/**
* The context panel rail's visible, user-ordered surfaces. Shared by the rail
* (for rendering and number badges) and the global surface-switch shortcut so
* both agree on which surface each digit maps to.
*
* Content-driven surfaces are hidden (not disabled) until content exists; an
* existing tab keeps them visible even if the content source went away.
*/
export const getVisibleContextRailSurfaces = (options: VisibleRailSurfacesOptions): ContextSurfaceDescriptor[] => {
return sortContextSurfaces(options.railOrder).filter((surface) => {
if (surface.id === 'plan' && !options.planModeEnabled) {
return false;
}
// The walkthrough needs room for a stop list beside real code, and its
// diffs come from OpenChamber's Git routes, which VS Code does not serve.
if (surface.id === 'walkthrough' && (options.isVSCode || options.screenWidth < WALKTHROUGH_MIN_WIDTH)) {
return false;
}
// VS Code already is an editor with a browser next to it. What OpenChamber
// could add there is a bare frame: no annotation, no agent control, no
// remote dev servers — all of which need a Chromium host the extension does
// not have. Offering the surface anyway would promise the panel people see
// on the desktop.
if (surface.id === 'browser' && options.isVSCode) {
return false;
}
if (surface.availability === 'has-content') {
return options.tabs.some((tab) => tab.mode === surface.mode);
}
return true;
});
};