dsh-brain/src-client/format.ts

133 lines
4.1 KiB
TypeScript

// dsh-brain client formatting helpers and label tables.
export function formatTime(value) {
if (typeof value !== 'number' || value <= 0) return '—'
var date = new Date(value)
var pad = function (n) {
return n < 10 ? '0' + String(n) : String(n)
}
return pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds())
}
export function formatIso(value) {
if (typeof value !== 'string' || value === '') return '—'
var stamp = Date.parse(value)
if (!Number.isFinite(stamp)) return value
return formatTime(stamp)
}
// "11-27 18:33" — the short stamp used in readable source labels.
export function formatShort(value) {
if (typeof value !== 'number' || value <= 0) return '—'
var date = new Date(value)
var pad = function (n) {
return n < 10 ? '0' + String(n) : String(n)
}
return pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes())
}
// Readable category label shared by the overview mix and the memory rows.
export function memoryCategoryLabel(category) {
if (typeof category !== 'string' || category === '') return '无分类'
var map = { rule: '规则', preference: '偏好', identity: '身份' }
return map[category] === undefined ? '其他' : map[category]
}
// Readable memory source. Session ids are never shown in full: a conversation
// source renders as "11-27 18:33 的对话" when the creation time is verifiable,
// and as a plain "来自对话" when it is not (no guessed timestamps).
export function memorySourceLabel(source, createdAt) {
if (typeof source !== 'string' || source === '') return '本地记录'
if (source.indexOf('session:') === 0) {
var stamp = typeof createdAt === 'string' ? Date.parse(createdAt) : NaN
return Number.isFinite(stamp) ? formatShort(stamp) + ' 的对话' : '对话 · 时间不可查'
}
if (source === 'mnemon') return 'Mnemon · 来源待核实'
if (source === 'memory_tool') return '助手写入'
if (source === 'mnemon-native') return 'Mnemon 写入'
if (source.indexOf('legacy:') === 0) return '旧数据导入'
if (source.indexOf('department:') === 0) return '部门空间'
if (source === 'local') return '本地手动'
return '其他来源'
}
export function formatBytes(value) {
if (typeof value !== 'number' || value < 0) return '—'
if (value < 1024) return String(value) + ' B'
if (value < 1024 * 1024) return (value / 1024).toFixed(1) + ' KB'
return (value / (1024 * 1024)).toFixed(2) + ' MB'
}
export function shortHash(value) {
if (typeof value !== 'string' || value === '') return '—'
return value.slice(0, 12)
}
export function readStored(key, fallback) {
try {
var raw = window.localStorage.getItem('dsh-brain:' + key)
if (raw === null || raw === '') raw = window.localStorage.getItem('dsh-cloud-memory:' + key)
return raw === null || raw === '' ? fallback : raw
} catch {
return fallback
}
}
export function store(key, value) {
try {
window.localStorage.setItem('dsh-brain:' + key, String(value))
} catch {
/* a locked-down window keeps the value in memory only */
}
}
export const OUTCOME_LABELS = {
recorded: '已上报',
duplicate: '重复忽略',
failed: '失败',
queued: '待重试',
skipped: '已跳过',
}
export const OUTCOME_TONES = {
recorded: 'ok',
duplicate: 'muted',
failed: 'bad',
queued: 'warn',
skipped: 'muted',
}
export const ORIGIN_LABELS = {
cloud: '云端下发',
uploaded: '本地已上传',
local: '本机安装',
unknown: '来源未知',
}
export const CHECK_TONES = {
ok: 'ok',
unconfigured: 'muted',
invalid: 'bad',
forbidden: 'bad',
unreachable: 'bad',
error: 'bad',
}
export const CHECK_LABELS = {
ok: '可用',
unconfigured: '未配置',
invalid: '令牌失效',
forbidden: '权限不足',
unreachable: '不可达',
error: '出错',
}
export const MCP_STATE_LABELS = {
connected: '已连接',
connecting: '连接中',
disabled: '已停用',
error: '异常',
expired: '授权过期',
unknown: '未知',
needs_auth: '待授权',
}