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
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
import { OPENAI_COMPAT_REASONING_EFFORTS, QUOTA_FALLBACK_OPTIONS, type QuotaFallbackOption } from '../../provider-options.js';
import React, { useState, useCallback, useId, useLayoutEffect, useRef } from 'react';
import { useI18n, interpolate, getLocale } from '../../i18n';
import { translateSettingsStatus } from '../../i18n/errors';
import type { SaveOutcome, SaveStatus } from '../hooks/useSettings';
import { replaceAddress, type SettingsDraft } from '../hooks/useSettingsDraft';
import type { Settings } from '../types';
import { InlineConfirm } from './InlineConfirm';
import { AppearanceSettings } from './AppearanceSettings';
import { APPEARANCE_WORDS as APPEARANCE_WORDS_SETTINGS } from '../../theme-appearance';
import type { ResolvedTheme } from '../../theme-choice';
import { ArrowRightIcon, CheckCircleIcon, ChevronLeftIcon, EyeIcon, EyeOffIcon, LockIcon, WarningIcon } from './icons';
import { useBackupStatus } from '../hooks/useBackupStatus';
import { DEFAULT_SETTINGS } from '../constants/settings';
import { isClaudeMemObserverBaseUrl } from '../utils/observer-endpoint';
import { claudeProviderOptionLabel } from '../utils/claude-auth-method';
import { OPENAI_COMPAT_PRESET_OPTIONS, openAICompatPresetOption } from '../constants/openai-compat-presets';
import { formatCount, formatShortTime } from '../utils/formatters';
import {
SETTINGS_SECTIONS, SETTING_RANGES, outOfRange, settingsFooterState,
type RangedSetting, type SettingsFooterState, type SettingsSection,
} from '../utils/settings-form';
import { contextPreviewHash, settingsHash, timelineHash } from '../utils/memory-route';
import { isPlainClick } from '../utils/links';
interface SettingsPageProps {
/** The category shown, from the address (#/settings/<category>). */
section: SettingsSection;
/** The saved settings. The fields edit the draft (`draft.values`), held in App by useSettingsDraft. */
settings: Settings;
/** False until GET /api/settings succeeds; `settings` holds defaults until then. */
isLoaded: boolean;
/** Why the initial GET failed, as the network or the worker said it ("HTTP 500"), or null. */
loadError: string | null;
onRetryLoad: () => void;
onSave: (settings: Settings) => void;
isSaving: boolean;
/** How the last save ended (useSettings is its one writer). */
saveStatus: SaveStatus;
draft: SettingsDraft;
/** Saves a theme choice (App's useTheme): choosing a reading palette also chooses the theme it belongs to. */
onThemeChange: (theme: ResolvedTheme) => void;
}
type Translate = (english: string) => string;
/** Each section's name in the list and its heading and line in the panel. Literal t() keys, so tests/ui-i18n.test.ts sees them. */
export const SECTION_WORDS: Record<SettingsSection, { title: (t: Translate) => string; description: (t: Translate) => string }> = {
loading: { title: t => t('Loading'), description: t => t('How many observations to inject') },
display: { title: t => t('Display'), description: t => t('What to show in context tables') },
context: { title: t => t('Context'), description: t => t('What else a session gets from memory') },
images: { title: t => t('Images'), description: t => t("Screenshots and images from your agent's tools") },
backups: { title: t => t('Backups'), description: t => t('Automatic snapshots of your memory database') },
provider: { title: t => t('AI provider'), description: t => t('AI provider and model selection') },
fallbacks: { title: t => t('Fallbacks'), description: t => t('What takes over while the provider or its credential cannot serve') },
advanced: { title: t => t('Advanced'), description: t => t('My Work summaries, the Claude Code path and the worker port') },
};
interface FooterDetail { changed: number; savedAt: number | null }
/**
* What the footer says in each state (settingsFooterState in utils/settings-form.ts is the one writer). A failed load
* says so in plain words; its raw cause is folded under Details (G17).
*/
const FOOTER_WORDS: Record<SettingsFooterState, (t: Translate, detail: FooterDetail) => string> = {
loading: t => t('Loading settings…'),
'load-failed': t => t('Could not load settings.'),
clean: t => t('No changes to save.'),
dirty: (t, detail) => interpolate(detail.changed === 1 ? t('{count} unsaved change') : t('{count} unsaved changes'), { count: formatCount(detail.changed) }),
saving: t => t('Saving...'),
saved: (t, detail) => interpolate(t('Saved at {time}'), { time: detail.savedAt === null ? '' : formatShortTime(detail.savedAt) }),
};
/**
* The line a save's outcome adds to the footer. A save that went through says so in the footer's own "saved" state
* ("Saved at …"); a refused one says it here, its reason folded under Details.
*/
const SAVE_OUTCOME_WORDS: Record<SaveOutcome, ((t: Translate) => string) | null> = {
none: null,
saved: null,
failed: t => t('Could not save settings.'),
};
/** The props a field's control takes from its FormField: the id its label names, and what describes it. */
interface FieldControl {
id: string;
'aria-describedby'?: string;
'aria-invalid'?: true;
}
/** A labelled field: the label above, the control, then why its value is refused (if it is) and its help, both in view. */
function FormField({ label, help, error, children }: {
label: string;
help?: React.ReactNode;
error?: string | null;
children: (control: FieldControl) => React.ReactNode;
}) {
const id = useId();
const helpId = help ? `${id}-help` : undefined;
const errorId = error ? `${id}-error` : undefined;
const describedBy = [errorId, helpId].filter(Boolean).join(' ') || undefined;
return (
<div className={`form-field${error ? ' form-field--error' : ''}`}>
<label className="form-field-label" htmlFor={id}>{label}</label>
{children({ id, 'aria-describedby': describedBy, ...(error ? { 'aria-invalid': true as const } : {}) })}
{error && <p className="field-error" id={errorId}><WarningIcon size={14} /><span>{error}</span></p>}
{help && <div className="field-help" id={helpId}>{help}</div>}
</div>
);
}
/**
* A number field that says under itself when the worker would refuse its value (SETTING_RANGES). Its help is a
* template: {min} and {max} are filled in from SETTING_RANGES, so the help never types the bounds again.
*/
function NumberField({ label, help, setting, value, onChange }: {
label: string;
help?: string;
setting: RangedSetting;
value: string;
onChange: (value: string) => void;
}) {
const { t } = useI18n();
const { min, max, decimals } = SETTING_RANGES[setting];
const bounds = { min: formatCount(min), max: formatCount(max) };
const error = outOfRange(value, setting)
? interpolate(decimals ? t('Enter a number from {min} to {max}.') : t('Enter a whole number from {min} to {max}.'), bounds)
: null;
return (
<FormField label={label} help={help === undefined ? undefined : interpolate(help, bounds)} error={error}>
{control => (
<input {...control} type="number" inputMode={decimals ? 'decimal' : 'numeric'} step={decimals ? 'any' : 1}
min={min} max={max} value={value} onChange={event => onChange(event.target.value)} />
)}
</FormField>
);
}
/** A secret (an API key) hidden by default, with a button that shows it while pressed. */
function SecretInput({ control, label, value, placeholder, onChange }: {
control: FieldControl;
label: string;
value: string;
placeholder: string;
onChange: (value: string) => void;
}) {
const { t } = useI18n();
const [shown, setShown] = useState(false);
const toggleLabel = interpolate(t('Show {field}'), { field: label });
return (
<div className="field-secret">
<input {...control} type={shown ? 'text' : 'password'} value={value} placeholder={placeholder}
autoComplete="off" spellCheck={false} onChange={event => onChange(event.target.value)} />
<button type="button" className="field-secret-toggle" aria-pressed={shown} aria-label={toggleLabel} title={toggleLabel}
onClick={() => setShown(value => !value)}>
{shown ? <EyeOffIcon size={16} /> : <EyeIcon size={16} />}
</button>
</div>
);
}
/** A value set outside the viewer: a lock, a dashed edge, and read-only (it can still be focused, read and copied). */
function ReadOnlyInput({ control, value, placeholder }: { control: FieldControl; value: string; placeholder?: string }) {
return (
<div className="field-readonly">
<LockIcon size={16} />
<input {...control} type="text" value={value} placeholder={placeholder} readOnly />
</div>
);
}
/** An on/off setting: the switch says On or Off in words beside its track, not by colour and position only. */
function ToggleSwitch({ id, label, description, checked, onChange }: {
id: string;
label: string;
description?: string;
checked: boolean;
onChange: (checked: boolean) => void;
}) {
const { t } = useI18n();
return (
<div className="toggle-row">
<div className="toggle-info">
<label htmlFor={id} id={`${id}-label`} className="toggle-label">{label}</label>
{description && <span className="toggle-description" id={`${id}-description`}>{description}</span>}
</div>
<button
type="button"
id={id}
role="switch"
aria-checked={checked}
aria-labelledby={`${id}-label`}
aria-describedby={description ? `${id}-description` : undefined}
className={`toggle-switch${checked ? ' on' : ''}`}
onClick={() => onChange(!checked)}
>
<span className="toggle-track" aria-hidden="true"><span className="toggle-knob" /></span>
<span className="toggle-word" aria-hidden="true">{checked ? t('On') : t('Off')}</span>
</button>
</div>
);
}
function Subsection({ label, children }: { label: string; children: React.ReactNode }) {
return (
<div className="display-subsection">
<span className="subsection-label">{label}</span>
{children}
</div>
);
}
function formatEpochMs(epochMs: number | null | undefined, t: Translate): string {
if (typeof epochMs !== 'number' || !Number.isFinite(epochMs)) return t('never');
return new Date(epochMs).toLocaleString(getLocale());
}
/** One label/value line in the backup status readout (toggle-row styling). */
function BackupStatusRow({ label, value }: { label: string; value: React.ReactNode }) {
return (
<div className="toggle-row">
<div className="toggle-info">
<span className="toggle-label">{label}</span>
</div>
<span className="toggle-description">{value}</span>
</div>
);
}
/**
* Live backup status readout (GET /api/backup/status). Mounted only while the Backups section is the one on
* screen, so it fetches only when someone looks at it.
*/
function BackupStatusPanel() {
const { t } = useI18n();
const { status, isLoading, error } = useBackupStatus();
if (isLoading) {
return <span className="toggle-description">{t('Loading backup status...')}</span>;
}
if (error) {
return <span className="toggle-description">{t(error)}</span>;
}
if (!status) return null;
if (!status.configured) {
return (
<span className="toggle-description">
{t('Backups are not running. Turn on automatic snapshots, save, and restart the worker.')}
</span>
);
}
return (
<div className="toggle-group">
<BackupStatusRow label={t('Last snapshot')} value={formatEpochMs(status.lastSnapshotAt, t)} />
<BackupStatusRow label={t('Snapshots on disk')} value={formatCount(status.snapshotCount ?? 0)} />
{status.lastError && <BackupStatusRow label={t('Last error')} value={t(status.lastError)} />}
</div>
);
}
/** The question asked in the page before the settings pages are left with unsaved changes (useSettingsDraft asks it). */
export function LeaveQuestion({ draft }: { draft: SettingsDraft }) {
const { t } = useI18n();
if (!draft.leaveQuestion) return null;
const count = draft.changedKeys.length;
return (
<InlineConfirm
question={count === 1
? t('Leave without saving? Your 1 unsaved change will be lost.')
: interpolate(t('Leave without saving? Your {count} unsaved changes will be lost.'), { count: formatCount(count) })}
confirmLabel={t('Discard changes')}
busyLabel={t('Discard changes')}
busy={false}
onConfirm={draft.discardAndLeave}
onCancel={draft.keepEditing}
/>
);
}
/**
* What a category's name adds after it: Images says whether images are kept (#5426), once the saved settings are read.
* `keepImages` is null before then, when the form holds defaults that must not be presented as this install's.
*/
export function categoryHint(section: SettingsSection, keepImages: boolean | null, t: Translate): string | null {
return section === 'images' && keepImages !== null ? (keepImages ? t('On') : t('Off')) : null;
}
/** A category's name as the phone's select shows it, with its hint. */
export function categoryLabel(section: SettingsSection, keepImages: boolean | null, t: Translate): string {
const title = SECTION_WORDS[section].title(t);
const hint = categoryHint(section, keepImages, t);
return hint ? `${title} · ${hint}` : title;
}
/**
* Memory settings as a page (#/settings/<category>; design pass Phase 11, Codex D19, Claude D20), not a dialog over
* the timeline. One category at a time, chosen in a column on a desktop and in a select on a phone (the template's
* 767px breakpoint draws one or the other); a choice replaces the address, so Back leaves the settings rather than
* stepping through the categories. The saved context preview is its own page, one link from every category, under the
* column on a desktop and beside the select on a phone. The draft, its save state and the leave question live in App
* (useSettingsDraft), so the trip to the preview keeps them; the breadcrumb and Cancel ask it before leaving.
*/
export function SettingsPage({
section: activeSection,
settings,
isLoaded,
loadError,
onRetryLoad,
onSave,
isSaving,
saveStatus,
draft,
onThemeChange,
}: SettingsPageProps) {
const { t } = useI18n();
const idPrefix = useId();
const formState = draft.values;
const { updateSetting, toggleBoolean, changedKeys, savedAt } = draft;
// From the saved settings, not the form: the field stays editable while a
// user types any other URL, and read-only for the observer's own endpoint.
const observerManagesBaseUrl = isClaudeMemObserverBaseUrl(settings.CLAUDE_MEM_OPENROUTER_BASE_URL);
const isDirty = changedKeys.length > 0;
const failureWords = !isSaving && saveStatus !== draft.setAsideFailure ? SAVE_OUTCOME_WORDS[saveStatus.outcome]?.(t) ?? null : null;
/** Saves the changed settings; a dimmed Save (nothing changed) does nothing. */
const handleSave = useCallback(() => {
if (!isDirty) return;
onSave(formState);
}, [formState, isDirty, onSave]);
/** A category chosen in the column or the select replaces the address (one history entry for the settings). */
const chooseCategory = (section: SettingsSection) => replaceAddress(settingsHash(section));
/** A link out of the settings asks first when changes would be lost, before the address moves (useSettingsDraft). */
const leaveByLink = (hash: string) => (event: React.MouseEvent<HTMLAnchorElement>) => {
if (!isPlainClick(event)) return;
event.preventDefault();
draft.requestLeave(hash);
};
// Another category opens at its heading: the page stays mounted between categories, scrolled where the last one was.
const mainRef = useRef<HTMLElement>(null);
useLayoutEffect(() => { mainRef.current?.scrollTo({ top: 0 }); }, [activeSection]);
const footerState = settingsFooterState({ isLoaded, loadError, isSaving, changedCount: changedKeys.length, savedAt });
const footerWords = FOOTER_WORDS[footerState](t, { changed: changedKeys.length, savedAt });
const footerLineId = `${idPrefix}-footer-line`;
const categorySelectId = `${idPrefix}-category`;
const keepImages = formState.CLAUDE_MEM_MEDIA_CAPTURE_ENABLED === 'true';
// Before the saved settings are read the form holds defaults: the category list says nothing about them until then.
const imagesKeptHint = isLoaded ? keepImages : null;
const presetOption = openAICompatPresetOption(formState.CLAUDE_MEM_OPENAI_COMPAT_PRESET);
// The placeholder must follow the implicit default, which may come from the override or the preset.
const defaultOverride = (formState.CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL ?? '').trim();
const effectiveDefault = defaultOverride.toLowerCase() === 'none'
? ''
: defaultOverride || presetOption.defaultModel;
const panel = (section: SettingsSection, children: React.ReactNode) => (
<section
key={section}
id={`${idPrefix}-panel-${section}`}
aria-labelledby={`${idPrefix}-heading-${section}`}
hidden={activeSection !== section}
className="mem-panel settings-category-panel"
>
<div className="settings-category-heading">
<h2 className="mem-panel-title" id={`${idPrefix}-heading-${section}`}>{SECTION_WORDS[section].title(t)}</h2>
<p>{SECTION_WORDS[section].description(t)}</p>
</div>
{children}
</section>
);
return (
<main ref={mainRef} className="mem-scroll settings-page" aria-labelledby={`${idPrefix}-title`}>
<div className="mem-page mem-page--reading">
<nav className="mem-crumbs" aria-label={t('Breadcrumb')}>
<a href={timelineHash()} onClick={leaveByLink(timelineHash())}><ChevronLeftIcon size={14} />{t('Memory')}</a>
</nav>
<header className="mem-pagehead">
<h1 className="mem-title" id={`${idPrefix}-title`} tabIndex={-1}>{t('Memory settings')}</h1>
<p className="mem-leadtext">{t('Choose the context your next session receives.')}</p>
</header>
<div className="settings-layout">
{/* The categories and the preview: a column on a desktop; at 767px and below the select takes the column's
place, with the preview beside it. */}
<div className="settings-side">
<nav className="settings-categories" aria-label={t('Settings category')}>
<ul>
{SETTINGS_SECTIONS.map(section => {
const hint = categoryHint(section, imagesKeptHint, t);
return (
<li key={section}>
<a
href={settingsHash(section)}
aria-current={activeSection === section ? 'page' : undefined}
onClick={event => {
if (!isPlainClick(event)) return;
event.preventDefault();
chooseCategory(section);
}}
>
<span>{SECTION_WORDS[section].title(t)}</span>
{/* Images are kept by default (#5426), so whether they are is in view from the list itself. */}
{hint && <span className="settings-category-hint">{hint}</span>}
</a>
</li>
);
})}
</ul>
</nav>
<div className="mem-formrow settings-category-select">
<label htmlFor={categorySelectId}>{t('Settings category')}</label>
<select id={categorySelectId} className="mem-select" value={activeSection}
onChange={event => chooseCategory(event.target.value as SettingsSection)}>
{SETTINGS_SECTIONS.map(section => <option key={section} value={section}>{categoryLabel(section, imagesKeptHint, t)}</option>)}
</select>
</div>
<section className="settings-preview-link" aria-labelledby={`${idPrefix}-preview`}>
<h2 className="settings-preview-link-title" id={`${idPrefix}-preview`}>{t('Saved context preview')}</h2>
<p className="visually-hidden" id={`${idPrefix}-preview-text`}>{t('See what the saved settings include for a source and project.')}</p>
<a className="mem-textbtn" href={contextPreviewHash()} aria-describedby={`${idPrefix}-preview-text`}>
{t('Open preview')}<ArrowRightIcon size={18} />
</a>
</section>
</div>
{/* Before the initial load the form holds defaults; saving them would overwrite settings.json. */}
<fieldset className="settings-column" disabled={isSaving || !isLoaded}
style={{ border: 0, margin: 0, padding: 0, minWidth: 0 }}>
{panel('loading', <>
<NumberField
label={t('Observations')}
help={t('Number of recent observations to include in context ({min}-{max})')}
setting="CLAUDE_MEM_CONTEXT_OBSERVATIONS"
value={formState.CLAUDE_MEM_CONTEXT_OBSERVATIONS || DEFAULT_SETTINGS.CLAUDE_MEM_CONTEXT_OBSERVATIONS}
onChange={value => updateSetting('CLAUDE_MEM_CONTEXT_OBSERVATIONS', value || DEFAULT_SETTINGS.CLAUDE_MEM_CONTEXT_OBSERVATIONS)}
/>
<ToggleSwitch
id="session-start-all-sources"
label={t('Include all sources at session start')}
description={t('Show observations from Claude, Codex, and other harnesses in startup context')}
checked={formState.CLAUDE_MEM_SESSION_START_INCLUDE_ALL_SOURCES === 'true'}
onChange={() => toggleBoolean('CLAUDE_MEM_SESSION_START_INCLUDE_ALL_SOURCES')}
/>
<NumberField
label={t('Sessions')}
help={t('Number of recent sessions to pull observations from ({min}-{max})')}
setting="CLAUDE_MEM_CONTEXT_SESSION_COUNT"
value={formState.CLAUDE_MEM_CONTEXT_SESSION_COUNT || DEFAULT_SETTINGS.CLAUDE_MEM_CONTEXT_SESSION_COUNT}
onChange={value => updateSetting('CLAUDE_MEM_CONTEXT_SESSION_COUNT', value || DEFAULT_SETTINGS.CLAUDE_MEM_CONTEXT_SESSION_COUNT)}
/>
</>)}
{panel('display', <>
<Subsection label={t(APPEARANCE_WORDS_SETTINGS.appearance)}>
<AppearanceSettings onThemeChange={onThemeChange} />
</Subsection>
<Subsection label={t('Full Observations')}>
<NumberField
label={t('Count')}
help={t('How many observations show expanded details ({min}-{max})')}
setting="CLAUDE_MEM_CONTEXT_FULL_COUNT"
value={formState.CLAUDE_MEM_CONTEXT_FULL_COUNT || DEFAULT_SETTINGS.CLAUDE_MEM_CONTEXT_FULL_COUNT}
onChange={value => updateSetting('CLAUDE_MEM_CONTEXT_FULL_COUNT', value || DEFAULT_SETTINGS.CLAUDE_MEM_CONTEXT_FULL_COUNT)}
/>
<FormField label={t('Field')} help={t('Which field to expand for full observations')}>
{control => (
<select {...control} value={formState.CLAUDE_MEM_CONTEXT_FULL_FIELD || 'narrative'}
onChange={(e) => updateSetting('CLAUDE_MEM_CONTEXT_FULL_FIELD', e.target.value)}>
<option value="narrative">{t('Narrative')}</option>
<option value="facts">{t('Facts')}</option>
</select>
)}
</FormField>
</Subsection>
<Subsection label={t('Token Economics')}>
<div className="toggle-group">
<ToggleSwitch
id="show-read-tokens"
label={t('Read cost')}
description={t('Tokens to read this observation')}
checked={formState.CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS === 'true'}
onChange={() => toggleBoolean('CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS')}
/>
<ToggleSwitch
id="show-work-tokens"
label={t('Work investment')}
description={t('Tokens spent creating this observation')}
checked={formState.CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS === 'true'}
onChange={() => toggleBoolean('CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS')}
/>
</div>
</Subsection>
</>)}
{/* The three context switches, moved out of Advanced (they sat under the AI provider and API key). */}
{panel('context', (
<div className="toggle-group">
<ToggleSwitch
id="show-last-summary"
label={t('Include last summary')}
description={t("Add previous session's summary to context")}
checked={formState.CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY === 'true'}
onChange={() => toggleBoolean('CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY')}
/>
<ToggleSwitch
id="show-last-message"
label={t('Include last message')}
description={t("Add previous session's final message")}
checked={formState.CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE === 'true'}
onChange={() => toggleBoolean('CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE')}
/>
<ToggleSwitch
id="file-read-gate"
label={t('Block full-file reads')}
description={t('Send Claude to smart_outline/smart_unfold and past observations instead of reading whole code files of 32 KB and up that have history')}
checked={formState.CLAUDE_MEM_FILE_READ_GATE_ENABLED !== 'false'}
onChange={(checked) => updateSetting('CLAUDE_MEM_FILE_READ_GATE_ENABLED', checked ? 'true' : 'false')}
/>
</div>
))}
{panel('images', (
<div className="toggle-group">
<ToggleSwitch
id="media-capture-enabled"
label={t('Keep images')}
description={t('Keep the screenshots and images your agent reads or makes, so they show on your requests in My Work. Images stay on this computer until their event is deleted. With Cloud Sync on, they are also uploaded privately to your cmem.ai account. This switch never sends images to an AI model. Turning it off stops keeping new images; images already kept stay.')}
checked={keepImages}
onChange={(checked) => updateSetting('CLAUDE_MEM_MEDIA_CAPTURE_ENABLED', checked ? 'true' : 'false')}
/>
<ToggleSwitch
id="review-stack-image-previews"
label={t('Share file previews in Review Stack')}
description={t('Add thumbnails of saved PNG, JPEG and WebP deliverables to your review cards, including older files. Shared My Work syncs these thumbnails to your account. Turning this off stops adding previews.')}
checked={formState.CLAUDE_MEM_REVIEW_STACK_IMAGE_PREVIEWS === 'true'}
onChange={checked => updateSetting('CLAUDE_MEM_REVIEW_STACK_IMAGE_PREVIEWS', checked ? 'true' : 'false')}
/>
</div>
))}
{panel('backups', <>
<div className="toggle-group">
<ToggleSwitch
id="backup-enabled"
label={t('Automatic snapshots')}
description={t('Snapshot the database on a schedule (restart the worker to apply)')}
checked={formState.CLAUDE_MEM_BACKUP_ENABLED === 'true'}
onChange={() => toggleBoolean('CLAUDE_MEM_BACKUP_ENABLED')}
/>
</div>
<NumberField
label={t('Interval (hours)')}
help={t('Hours between automatic snapshots ({min}-{max})')}
setting="CLAUDE_MEM_BACKUP_INTERVAL_HOURS"
value={formState.CLAUDE_MEM_BACKUP_INTERVAL_HOURS || DEFAULT_SETTINGS.CLAUDE_MEM_BACKUP_INTERVAL_HOURS}
onChange={value => updateSetting('CLAUDE_MEM_BACKUP_INTERVAL_HOURS', value || DEFAULT_SETTINGS.CLAUDE_MEM_BACKUP_INTERVAL_HOURS)}
/>
<NumberField
label={t('Snapshots to keep')}
help={t('How many local snapshots to retain before old ones are deleted ({min}-{max})')}
setting="CLAUDE_MEM_BACKUP_RETAIN_COUNT"
value={formState.CLAUDE_MEM_BACKUP_RETAIN_COUNT || DEFAULT_SETTINGS.CLAUDE_MEM_BACKUP_RETAIN_COUNT}
onChange={value => updateSetting('CLAUDE_MEM_BACKUP_RETAIN_COUNT', value || DEFAULT_SETTINGS.CLAUDE_MEM_BACKUP_RETAIN_COUNT)}
/>
<Subsection label={t('Status')}>
{activeSection === 'backups' && <BackupStatusPanel />}
</Subsection>
<p className="toggle-description">
{t('Restore from a snapshot:')} <code>npx claude-mem restore <file></code>
</p>
</>)}
{panel('provider', <>
<div className="toggle-group">
<ToggleSwitch
id="observer-enabled"
label={t('Memory capture')}
description={t('Generate new observations and session summaries with the selected AI provider. Turn this off to stop those capture calls; existing memories, search, and SessionStart context remain available.')}
checked={formState.CLAUDE_MEM_OBSERVER_ENABLED !== 'false'}
onChange={(checked) => updateSetting('CLAUDE_MEM_OBSERVER_ENABLED', checked ? 'true' : 'false')}
/>
</div>
<FormField
label={t('AI Provider')}
help={t('Choose the provider that generates observations: Claude (via Agent SDK), Gemini (via REST API), OpenRouter (also used by the claude-mem observer), Codex (your ChatGPT subscription), or any OpenAI-compatible endpoint')}
>
{control => (
<select {...control} value={formState.CLAUDE_MEM_PROVIDER || 'claude'}
onChange={(e) => updateSetting('CLAUDE_MEM_PROVIDER', e.target.value)}>
<option value="claude">{t(claudeProviderOptionLabel(formState.CLAUDE_MEM_CLAUDE_AUTH_METHOD))}</option>
<option value="gemini">{t('Gemini (uses API key)')}</option>
<option value="openrouter">{t('OpenRouter / claude-mem observer')}</option>
<option value="codex">{t('Codex (uses your ChatGPT subscription)')}</option>
<option value="openai-compatible">{t('OpenAI-compatible endpoint (BYOK)')}</option>
</select>
)}
</FormField>
{formState.CLAUDE_MEM_PROVIDER === 'claude' && (
<>
<FormField label={t('Claude Model')} help={t('Claude model used for generating observations')}>
{control => (
<select {...control} value={formState.CLAUDE_MEM_MODEL || 'haiku'}
onChange={(e) => updateSetting('CLAUDE_MEM_MODEL', e.target.value)}>
<option value="haiku">{t('haiku (fastest)')}</option>
<option value="sonnet">{t('sonnet (balanced)')}</option>
<option value="opus">{t('opus (highest quality)')}</option>
</select>
)}
</FormField>
<FormField
label={t('Claude Auth Method')}
help={t("Records which Claude credential this install uses: your Claude account login (subscription), an ANTHROPIC_API_KEY (api-key), a custom ANTHROPIC_BASE_URL endpoint (gateway), or Claude Code's own credentials (cli). The credential itself comes from ~/.claude-mem/.env: an ANTHROPIC_BASE_URL or ANTHROPIC_API_KEY there is used even when this says subscription. Shared with the Quota Fallback Claude option.")}
>
{control => (
<select {...control} value={formState.CLAUDE_MEM_CLAUDE_AUTH_METHOD || 'subscription'}
onChange={(e) => updateSetting('CLAUDE_MEM_CLAUDE_AUTH_METHOD', e.target.value)}>
<option value="subscription">{t('subscription (Claude account login)')}</option>
<option value="api-key">{t('api-key (ANTHROPIC_API_KEY)')}</option>
<option value="gateway">{t('gateway (custom endpoint + token)')}</option>
<option value="cli">{t('cli (Claude Code credentials)')}</option>
</select>
)}
</FormField>
</>
)}
{(formState.CLAUDE_MEM_PROVIDER === 'codex' || formState.CLAUDE_MEM_QUOTA_FALLBACK_PROVIDER === 'codex') && (
<FormField
label={t('Codex Model')}
help={t('Optional model available through your Codex subscription; leave empty for the Codex default. Run codex login before starting the worker.')}
>
{control => (
<input {...control} type="text" value={formState.CLAUDE_MEM_CODEX_MODEL || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_CODEX_MODEL', e.target.value)}
placeholder={t('Codex default (e.g. gpt-6-luna)')} />
)}
</FormField>
)}
{formState.CLAUDE_MEM_PROVIDER === 'gemini' && (
<>
<FormField label={t('Gemini API Key')} help={t('Your Google AI Studio API key (or set GEMINI_API_KEY env var)')}>
{control => (
<SecretInput control={control} label={t('Gemini API Key')} value={formState.CLAUDE_MEM_GEMINI_API_KEY || ''}
placeholder={t('Enter Gemini API key...')} onChange={value => updateSetting('CLAUDE_MEM_GEMINI_API_KEY', value)} />
)}
</FormField>
<FormField label={t('Gemini Model')} help={t('Gemini model used for generating observations')}>
{control => (
<select {...control} value={formState.CLAUDE_MEM_GEMINI_MODEL || 'gemini-flash-latest'}
onChange={(e) => updateSetting('CLAUDE_MEM_GEMINI_MODEL', e.target.value)}>
<option value="gemini-flash-latest">{t('gemini-flash-latest (default, latest GA Flash)')}</option>
<option value="gemini-flash-lite-latest">{t('gemini-flash-lite-latest (latest GA Flash-Lite)')}</option>
<option value="gemini-3.5-flash">{t('gemini-3.5-flash')}</option>
<option value="gemini-3.1-flash-lite">{t('gemini-3.1-flash-lite')}</option>
<option value="gemini-3-flash-preview">{t('gemini-3-flash-preview (preview)')}</option>
</select>
)}
</FormField>
<div className="toggle-group">
<ToggleSwitch
id="gemini-rate-limiting"
label={t('Rate Limiting')}
description={t('Enable for free tier (10-30 RPM). Disable if you have billing set up (1000+ RPM).')}
checked={formState.CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED === 'true'}
onChange={(checked) => updateSetting('CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED', checked ? 'true' : 'false')}
/>
</div>
</>
)}
{formState.CLAUDE_MEM_PROVIDER === 'openrouter' && (
<>
<FormField label={t('OpenRouter API Key')} help={t('Your OpenRouter API key from openrouter.ai (or set OPENROUTER_API_KEY env var)')}>
{control => (
<SecretInput control={control} label={t('OpenRouter API Key')} value={formState.CLAUDE_MEM_OPENROUTER_API_KEY || ''}
placeholder={t('Enter OpenRouter API key...')} onChange={value => updateSetting('CLAUDE_MEM_OPENROUTER_API_KEY', value)} />
)}
</FormField>
<FormField
label={t('OpenRouter Model')}
help={t('Model identifier from openrouter.ai/models (e.g., anthropic/claude-haiku-4.5, google/gemini-2.5-flash)')}
>
{control => (
// The stored value, the default as the placeholder: empty posts '' and the worker uses its default model.
<input {...control} type="text"
value={formState.CLAUDE_MEM_OPENROUTER_MODEL ?? ''}
onChange={(e) => updateSetting('CLAUDE_MEM_OPENROUTER_MODEL', e.target.value)}
placeholder={interpolate(t('e.g., {model}'), { model: DEFAULT_SETTINGS.CLAUDE_MEM_OPENROUTER_MODEL })} />
)}
</FormField>
<FormField
label={t('OpenRouter Base URL')}
help={<>
<p>{observerManagesBaseUrl
? t('Managed by the claude-mem observer. Run npx claude-mem install to use your own endpoint.')
: t('Configured in settings.json or the worker environment. Empty uses openrouter.ai.')}</p>
<p>
{t('Set')} <code>CLAUDE_MEM_OPENROUTER_BASE_URL</code>{' '}{t('in')}{' '}
<code>~/.claude-mem/settings.json</code>{' '}{t('or the worker environment. The viewer cannot change where your API key is sent.')}
</p>
</>}
>
{control => <ReadOnlyInput control={control} value={formState.CLAUDE_MEM_OPENROUTER_BASE_URL || ''} placeholder="https://openrouter.ai/api/v1" />}
</FormField>
{!observerManagesBaseUrl && (
<FormField
label={t('Reasoning effort')}
help={t('openrouter.ai only. Empty (the default) sends reasoning off. Set an effort to opt back in. A custom gateway does not get this field until an empty length-truncated reply retries once.')}
>
{control => (
<select {...control} value={formState.CLAUDE_MEM_OPENROUTER_REASONING_EFFORT || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_OPENROUTER_REASONING_EFFORT', e.target.value)}>
<option value="">{t('Off (default)')}</option>
<option value="none">{t('None (reasoning off)')}</option>
<option value="minimal">{t('Minimal')}</option>
<option value="low">{t('Low')}</option>
<option value="medium">{t('Medium')}</option>
<option value="high">{t('High')}</option>
</select>
)}
</FormField>
)}
<FormField label={t('Site URL (Optional)')} help={t('Your site URL for OpenRouter analytics (optional)')}>
{control => (
<input {...control} type="text" value={formState.CLAUDE_MEM_OPENROUTER_SITE_URL || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_OPENROUTER_SITE_URL', e.target.value)}
placeholder="https://yoursite.com" />
)}
</FormField>
<FormField label={t('App Name (Optional)')} help={t('Your app name for OpenRouter analytics (optional)')}>
{control => (
// The stored value, the default as the placeholder: empty posts '' and the worker uses its default name.
<input {...control} type="text" value={formState.CLAUDE_MEM_OPENROUTER_APP_NAME ?? ''}
onChange={(e) => updateSetting('CLAUDE_MEM_OPENROUTER_APP_NAME', e.target.value)}
placeholder={DEFAULT_SETTINGS.CLAUDE_MEM_OPENROUTER_APP_NAME} />
)}
</FormField>
</>
)}
{formState.CLAUDE_MEM_PROVIDER === 'openai-compatible' && (
<>
<FormField
label={t('Endpoint preset')}
help={<>
<p>{t('Configured outside the viewer. Supplies the base URL and default model unless explicitly overridden.')}</p>
{presetOption.note && <p>{t(presetOption.note)}</p>}
</>}
>
{control => <ReadOnlyInput control={control} value={t(presetOption.label)} />}
</FormField>
<FormField
label={t('Base URL')}
help={<>
<p>{t("Configured in settings.json or the worker environment. Empty uses the preset's endpoint.")}</p>
<p>
{t('Set the endpoint and preset in')} <code>~/.claude-mem/settings.json</code>{' '}
{t('or the worker environment. They determine where your API key is sent.')}
</p>
</>}
>
{control => (
<ReadOnlyInput control={control} value={formState.CLAUDE_MEM_OPENAI_COMPAT_BASE_URL || ''}
placeholder={presetOption.baseUrl || 'https://my-gateway.example.com/v1'} />
)}
</FormField>
<FormField label={t('Model')} help={t("Model id, passed verbatim. Leave blank to use the preset's default")}>
{control => (
<input {...control} type="text" value={formState.CLAUDE_MEM_OPENAI_COMPAT_MODEL || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_OPENAI_COMPAT_MODEL', e.target.value)}
placeholder={effectiveDefault || t('model id')} />
)}
</FormField>
<FormField
label={t('Implicit default model override')}
help={t('empty keeps the current fallback chain (last served model, then the preset default); `none` removes every implicit default; any other id replaces both and is sent verbatim.')}
>
{control => (
<input {...control} type="text" value={formState.CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL ?? ''}
onChange={(e) => updateSetting('CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL', e.target.value)}
placeholder={presetOption.defaultModel || t('model id')} />
)}
</FormField>
<FormField
label={t('Reasoning')}
help={t('Off (default) sends reasoning disabled so a reasoning model cannot spend the output cap thinking. On leaves it to the endpoint. A server that rejects the field is retried once without it.')}
>
{control => (
<select {...control} value={formState.CLAUDE_MEM_OPENAI_COMPAT_REASONING || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_OPENAI_COMPAT_REASONING', e.target.value)}>
<option value="">{t('Off (default)')}</option>
<option value="on">{t('On (endpoint decides)')}</option>
</select>
)}
</FormField>
<FormField label={t('Reasoning effort')}
help={t('Select an effort supported by your model to override the reasoning setting for the observer and API agent.')}>
{control => (
<select {...control} value={formState.CLAUDE_MEM_OPENAI_COMPAT_REASONING_EFFORT || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_OPENAI_COMPAT_REASONING_EFFORT', e.target.value)}>
<option value="">{t('No override')}</option>
{OPENAI_COMPAT_REASONING_EFFORTS.map(effort => <option key={effort} value={effort}>{effort}</option>)}
</select>
)}
</FormField>
<p className="toggle-description">
{t('The API key is set outside the viewer:')} <code>CLAUDE_MEM_OPENAI_COMPAT_API_KEY</code>{' '}{t('in')}{' '}
<code>~/.claude-mem/settings.json</code>{t(', or')} <code>OPENAI_COMPAT_API_KEY</code>{' '}{t('in')}{' '}
<code>~/.claude-mem/.env</code>{t('. Local servers need none.')}
</p>
</>
)}
</>)}
{/* Moved out of the old Advanced section, which held the provider, the fallbacks and the context switches. */}
{panel('fallbacks', <>
<FormField
label={t('Quota Fallback')}
help={t('While the selected provider cannot serve (allowance spent, rate limited or over capacity past its retries, unavailable, or repeatedly returning unusable replies), send observation work here until it recovers. Claude (your Anthropic plan) is the default and is used only when a Claude login or key exists. Off: work waits for the provider.')}
>
{control => (
<select {...control} value={formState.CLAUDE_MEM_QUOTA_FALLBACK_PROVIDER || 'claude'}
onChange={(e) => updateSetting('CLAUDE_MEM_QUOTA_FALLBACK_PROVIDER', e.target.value)}>
{QUOTA_FALLBACK_OPTIONS.map(provider => (
<option key={provider} value={provider}>{({
claude: t(claudeProviderOptionLabel(formState.CLAUDE_MEM_CLAUDE_AUTH_METHOD)),
off: t('Off (wait for the provider)'),
gemini: t('Gemini (uses API key)'),
openrouter: t('OpenRouter / claude-mem observer'),
'openai-compatible': t('OpenAI-compatible endpoint'),
codex: t('Codex (uses your ChatGPT subscription)'),
} satisfies Record<QuotaFallbackOption, string>)[provider]}</option>
))}
</select>
)}
</FormField>
{(formState.CLAUDE_MEM_QUOTA_FALLBACK_PROVIDER || 'claude') === 'claude' && (
<>
<FormField
label={t('Claude Auth Method')}
help={t("The same CLAUDE_MEM_CLAUDE_AUTH_METHOD setting as the AI Provider's Claude Auth Method. Fallback Claude runs use the same credential, which comes from ~/.claude-mem/.env when an ANTHROPIC_BASE_URL or ANTHROPIC_API_KEY is set there.")}
>
{control => (
<select {...control} value={formState.CLAUDE_MEM_CLAUDE_AUTH_METHOD || 'subscription'}
onChange={(e) => updateSetting('CLAUDE_MEM_CLAUDE_AUTH_METHOD', e.target.value)}>
<option value="subscription">{t('subscription (Claude account login)')}</option>
<option value="api-key">{t('api-key (ANTHROPIC_API_KEY)')}</option>
<option value="gateway">{t('gateway (custom endpoint + token)')}</option>
<option value="cli">{t('cli (Claude Code credentials)')}</option>
</select>
)}
</FormField>
<FormField
label={t('Fallback Claude Model')}
help={t('Claude model for fallback runs only. Blank uses the Claude Model setting and tier routing.')}
>
{control => (
<input {...control} type="text" value={formState.CLAUDE_MEM_QUOTA_FALLBACK_MODEL || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_QUOTA_FALLBACK_MODEL', e.target.value)}
placeholder={t('e.g., haiku')} />
)}
</FormField>
</>
)}
<FormField
label={t('Claude Credential Fallback')}
help={t('For a Claude observer: the other credential to run on while the primary one is held for quota, tried before the Quota Fallback provider. Subscription: a spent ANTHROPIC_API_KEY (e.g. credit balance too low) hands over to your Claude Code login, still protected by the quota guard. API key: a login that is spent or paused by the quota guard hands over to ANTHROPIC_FALLBACK_API_KEY in ~/.claude-mem/.env, billed per request. Either returns to the primary once it recovers; neither applies under ANTHROPIC_BASE_URL.')}
>
{control => (
<select {...control} value={formState.CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_QUOTA_FALLBACK_AUTH_METHOD', e.target.value)}>
<option value="">{t('Off (wait for the primary credential)')}</option>
<option value="subscription">{t('Subscription (Claude Code login)')}</option>
<option value="api-key">{t('API key (ANTHROPIC_FALLBACK_API_KEY)')}</option>
</select>
)}
</FormField>
</>)}
{panel('advanced', <>
<FormField
label={t('My Work summary provider')}
help={t('Generated card wording uses this endpoint. Empty uses OpenRouter only when it is the primary observer provider; any other endpoint, or a fallback key, is not used unless you select its provider here. Explicit provider selection may incur that provider\'s usual charges.')}
>
{control => (
<select {...control} value={formState.CLAUDE_MEM_WORK_VIEW_COPY_PROVIDER || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_WORK_VIEW_COPY_PROVIDER', e.target.value)}>
<option value="">{t('OpenRouter when it is the primary provider')}</option>
<option value="openrouter">{t('OpenRouter')}</option>
<option value="openai-compatible">{t('OpenAI-compatible endpoint')}</option>
<option value="off">{t('Off (recorded wording only)')}</option>
</select>
)}
</FormField>
<FormField
label={t('My Work summary model override')}
help={t("Optional model id for generated card wording. Empty uses the selected provider's configured model.")}
>
{control => (
<input {...control} type="text" maxLength={200} value={formState.CLAUDE_MEM_WORK_VIEW_COPY_MODEL || ''}
onChange={(e) => updateSetting('CLAUDE_MEM_WORK_VIEW_COPY_MODEL', e.target.value)}
placeholder={t('Use the provider\'s configured model')} />
)}
</FormField>
<FormField
label={t('Claude Code CLI path')}
help={<>
<p>{t('Executable path for the Claude Code CLI. File/env only — edit ~/.claude-mem/settings.json or set CLAUDE_CODE_PATH in the environment, then restart the worker.')}</p>
<p>{t('Read-only here. Set')} <code>CLAUDE_CODE_PATH</code> {t('in')} <code>~/.claude-mem/settings.json</code> {t('or the environment.')}</p>
</>}
>
{control => (
<ReadOnlyInput control={control} value={formState.CLAUDE_CODE_PATH || ''}
placeholder={t('Auto-detect (set via settings.json or env)')} />
)}
</FormField>
<FormField
label={t('Worker Port')}
help={<>
<p>{t('Port for the background worker service. File/env only — edit ~/.claude-mem/settings.json or set CLAUDE_MEM_WORKER_PORT in the environment, then restart the worker.')}</p>
<p>{t('Read-only here. Set')} <code>CLAUDE_MEM_WORKER_PORT</code> {t('in')} <code>~/.claude-mem/settings.json</code> {t('or the environment.')}</p>
</>}
>
{control => (
// The saved value only: the viewer's 37700 is not this install's port (the worker
// adds a per-install offset, getDefaultWorkerPort), so an unstored port shows the
// placeholder instead of a number that was never bound.
<ReadOnlyInput control={control} value={settings.CLAUDE_MEM_WORKER_PORT || ''}
placeholder={t('Derived per install (set via settings.json or env)')} />
)}
</FormField>
</>)}
</fieldset>
</div>
{/* Stays in view at the foot of the window however long the category is, as the dialog's footer did. */}
<footer className="settings-footer">
<LeaveQuestion draft={draft} />
<div className="mem-footeracts settings-footer-acts" hidden={!!draft.leaveQuestion}>
<button
type="button"
className="mem-btn mem-btn--primary save-btn"
onClick={handleSave}
disabled={isSaving || !isLoaded}
aria-disabled={!isDirty || undefined}
aria-describedby={footerLineId}
>
{isSaving ? t('Saving...') : t('Save settings')}
</button>
{/* Leaving asks first when changes would be lost (useSettingsDraft). */}
<a className="mem-textbtn settings-cancel" href={timelineHash()} onClick={leaveByLink(timelineHash())}>{t('Cancel')}</a>
<div className="settings-footer-state" id={footerLineId}>
{(footerState === 'clean' || footerState === 'dirty') && (
<p className={`settings-changes settings-changes--${footerState}`}>{footerWords}</p>
)}
<div className="save-status" role="status" aria-live="polite">
{footerState === 'loading' && <span>{footerWords}</span>}
{footerState === 'load-failed' && (
// G17: the plain words, the raw cause folded under Details in monospace, then Retry. A refused read
// (the Fly host's gate refuses /api/settings) lands here: the fields stay disabled on their defaults.
<span className="error" role="alert">
{footerWords}
{loadError && (
<details className="settings-error-details">
<summary>{t('Details')}</summary>
<pre>{loadError}</pre>
</details>
)}
<button type="button" className="mem-btn" onClick={onRetryLoad}>{t('Retry')}</button>
</span>
)}
{footerState === 'saving' && <span>{footerWords}</span>}
{footerState === 'saved' && <span className="success"><CheckCircleIcon size={16} />{footerWords}</span>}
{failureWords && footerState !== 'saving' && (
<span className="error">
{failureWords}
{saveStatus.reason && (
<details className="settings-error-details">
<summary>{t('Details')}</summary>
<pre>{translateSettingsStatus(saveStatus.reason, t)}</pre>
</details>
)}
</span>
)}
</div>
</div>
</div>
</footer>
</div>
</main>
);
}