diff --git a/vendor/dsh-brain/lib/config.js b/vendor/dsh-brain/lib/config.js new file mode 100644 index 0000000..f6b0b70 --- /dev/null +++ b/vendor/dsh-brain/lib/config.js @@ -0,0 +1,110 @@ +/** + * Settings model: the cordis row config supplies defaults, the user-editable + * sidecar (`$DSH_HOME/dsh-brain.json`) overrides them, and secrets live ONLY + * in the sidecar. Nothing here ever reaches the conversation or a model context. + */ +/** Defaults written into cordis.patch.yml; also the fallback for missing keys. */ +export const DEFAULT_SETTINGS = { + enabled: true, + baseUrl: 'https://your-memory-host/api/longmemory', + clientId: 'dsh-desktop', + requestTimeoutMs: 5000, + defaultK: 10, + summaryEnabled: true, + summaryMaxChars: 6000, + skillsSyncEnabled: true, + skillsDir: '', +}; +const MIN_TIMEOUT_MS = 1000; +const MAX_TIMEOUT_MS = 60_000; +/** Trim truncation guard for the free-form identifiers. */ +function shortString(value) { + if (typeof value !== 'string') + return undefined; + const trimmed = value.trim(); + if (trimmed === '') + return undefined; + return trimmed.slice(0, 128); +} +/** + * Normalize a base URL: require http(s), drop trailing slashes and any query. + * Returns undefined when the input is unusable, so callers can fall back. + */ +export function normalizeBaseUrl(value) { + if (typeof value !== 'string' || value.trim() === '') + return undefined; + try { + const url = new URL(value.trim()); + if (url.protocol !== 'http:' && url.protocol !== 'https:') + return undefined; + return `${url.origin}${url.pathname.replace(/\/+$/, '')}`; + } + catch { + return undefined; + } +} +/** + * A packaged default that was never replaced by a real service. + * + * `cordis.patch.yml` ships `https://your-memory-host/...` so a fresh profile + * boots without touching a production endpoint. Such an address must be shown + * as "未配置服务" — never as a healthy connection. + */ +export function isPlaceholderBaseUrl(value) { + const normalized = normalizeBaseUrl(value); + if (normalized === undefined) + return true; + try { + const host = new URL(normalized).hostname.toLowerCase(); + return host === 'your-memory-host' || host === 'example.com'; + } + catch { + return true; + } +} +/** Service origin of one base URL, or '' when it cannot be parsed. */ +export function originOf(value) { + try { + return new URL(value).origin; + } + catch { + return ''; + } +} +/** Clamp a numeric config value into an integer range, else undefined. */ +function boundedInt(value, min, max) { + if (typeof value !== 'number' || !Number.isFinite(value)) + return undefined; + return Math.min(max, Math.max(min, Math.round(value))); +} +/** Extract the recognized settings from an untrusted row config object. */ +export function settingsFromConfig(raw) { + if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) + return {}; + const source = raw; + const result = {}; + if (typeof source.enabled === 'boolean') + result.enabled = source.enabled; + const baseUrl = normalizeBaseUrl(source.baseUrl); + if (baseUrl !== undefined) + result.baseUrl = baseUrl; + const clientId = shortString(source.clientId); + if (clientId !== undefined) + result.clientId = clientId; + const timeout = boundedInt(source.requestTimeoutMs, MIN_TIMEOUT_MS, MAX_TIMEOUT_MS); + if (timeout !== undefined) + result.requestTimeoutMs = timeout; + const defaultK = boundedInt(source.defaultK, 1, 100); + if (defaultK !== undefined) + result.defaultK = defaultK; + if (typeof source.summaryEnabled === 'boolean') + result.summaryEnabled = source.summaryEnabled; + const summaryMaxChars = boundedInt(source.summaryMaxChars, 1000, 16_000); + if (summaryMaxChars !== undefined) + result.summaryMaxChars = summaryMaxChars; + if (typeof source.skillsSyncEnabled === 'boolean') + result.skillsSyncEnabled = source.skillsSyncEnabled; + if (typeof source.skillsDir === 'string') + result.skillsDir = source.skillsDir.trim().slice(0, 512); + return result; +}