Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

3.0 KB
Raw Blame
import { MAX_STORED_PROMPT_CHARS, MEDIA_PROMPT_PLACEHOLDER } from '../services/sqlite/prompt-storage.js';
import { stripMemoryTags, TAG_NAMES } from '../utils/tag-stripping.js';

/**
 * A session's opening prompt as a Sessions row serves it: `first_prompt` on GET /api/sessions, from the worker's
 * SQLite catalog (SessionStore.getSessionCatalog) and the server runtime's Postgres one (ServerViewerRoutes) alike. The
 * viewer names a session by it when the session has no custom title and no summary, labelled "First prompt"
 * (sessionName in src/ui/viewer/utils/sessions.ts). It is the session's first prompt as recorded; the app does not
 * record who typed it, so it can be an agent's instructions as well as a person's words.
 */

/**
 * The longest opening prompt a row carries, in characters: a title's length, the cut the server runtime's sessions
 * route already made when it showed the prompt as a title (reused, not a new limit). A stored prompt can be 4,000
 * characters (MAX_STORED_PROMPT_CHARS), so without it a 100-row page would carry up to 400 KB of prompt text that a row
 * never shows. A longer prompt is cut with "…", so the row says it was cut.
 */
export const SESSION_FIRST_PROMPT_MAX_CHARS = 120;

/** A memory tag left open: what follows it may be private words whose closing tag the store cut off. */
const OPEN_MEMORY_TAG = new RegExp(`<(?:${TAG_NAMES.join('|')})(?=[\\s>/])`, 'i');

/**
 * A stored prompt with nothing private left in it. Today the worker strips a session's opening prompt of its memory
 * tags before storing it (`sdk_sessions.user_prompt`, through normalizeStoredPromptText), but older rows, imports and
 * synced copies may still hold them. So a catalog query reads the whole stored prompt, up to MAX_STORED_PROMPT_CHARS
 * (the SQLite store's own cap, which the server runtime's read reuses), and this removes every balanced memory block
 * (stripMemoryTags), then drops everything from a tag still open, which a cut can leave inside a block.
 */
export function promptWithoutMemoryTags(stored: string): string {
  const stripped = stripMemoryTags(stored.length > MAX_STORED_PROMPT_CHARS ? stored.slice(0, MAX_STORED_PROMPT_CHARS) : stored);
  const open = stripped.search(OPEN_MEMORY_TAG);
  return open < 0 ? stripped : stripped.slice(0, open);
}

/**
 * The `first_prompt` a row serves, from the whole stored prompt: its memory tags removed (promptWithoutMemoryTags),
 * trimmed; null when it has no words or is the stand-in session-init stores for a send with no words (an image-only
 * prompt); cut to SESSION_FIRST_PROMPT_MAX_CHARS with "…" when it is longer. Characters are
 * code points.
 */
export function sessionFirstPrompt(stored: string | null | undefined): string | null {
  if (!stored) return null;
  const words = promptWithoutMemoryTags(stored).trim();
  if (!words || words === MEDIA_PROMPT_PLACEHOLDER) return null;
  const characters = Array.from(words);
  if (characters.length <= SESSION_FIRST_PROMPT_MAX_CHARS) return words;
  return `${characters.slice(0, SESSION_FIRST_PROMPT_MAX_CHARS - 1).join('').trimEnd()}…`;
}