diff --git a/README.md b/README.md index 0486a12..f103d36 100644 --- a/README.md +++ b/README.md @@ -49,10 +49,10 @@ The dashboard is organized into four tabs, with a top header to switch conversat - **By Step**: Lists every step in the selected conversation with its model, TPS, TTFT, context/output tokens, cache hit rate, and step cost. - **By Model**: Aggregates call counts, mean speed, total tokens, and cost per model within the current session and selected agent scope. - **Preferences** - - **Mount Mode**: Dual-mount (Top Bar + Sidebar) or Sidebar only. + - **Mount Mode & Popover Width**: Choose Dual-mount (Top Bar + Sidebar), Composer mount, or Sidebar only; the floating popover defaults to **Auto Fit** (expanding automatically on wide step tables within viewport bounds to avoid horizontal scrolling) or accepts a custom fixed width (`440–1080 px`). - **Top-Bar & CLI Capsule Customization**: Independently toggle the live running-sessions indicator and the Antigravity CLI bottom status capsule (auto-provisioned via `~/.gemini/antigravity-cli/settings.json` `statusLine`), and customize 1 to 5 metric slots and their left-to-right order (defaulting to Output Speed, TTFT, and Context Usage). - - **Call Details Display**: Under **Layout -> Step Table**, independently toggle main/subagent badges, the turn rail (off by default), and **Speed Multi-Color** grading (off by default so step speeds render in clean monochrome when generation speed fluctuates). - - **Model Name Format**: Compact short alias (e.g. `argon-4-high`) or raw upstream model ID (e.g. `gemini-3.8-flash-n`), plus an independent Orchestrator series ID selector (`orchestrator-argon` / `worker-agy-hydrogen` compact mode by default, or full annotated IDs). + - **Call Details & Custom Token Combos**: Under **Structure -> Step Table**, independently toggle main/subagent badges, the turn rail (off by default), and **Speed Multi-Color** grading (off by default); keep the default `Ctx / Think / Reply` combo column, switch to the billing-aligned `In / Out / Cache` preset (with per-segment cost breakdown on hover), or enter a custom format string such as `context:new:cache` or `in:out:cache`. + - **Model Name Format**: Choose among Compact short alias (e.g. `argon-4-high`), full raw upstream ID, or Trimmed raw ID (hiding `gemini-`, `claude-`, and `gpt-` prefixes to save single-line space, e.g. `3.8-flash-n`, `sonnet-4-6`, `5.4-mini`), with compaction suffixes (`·compact`) protected from ellipsis truncation; Orchestrator series IDs remain independently configurable. - **Per-model context windows**: In **Other → Model Context Settings**, choose a shared limit for all thinking levels of a model or select **Follow host settings**. The model list shows the saved limit and the most recent value reported by the host. At startup, the App / IDE window bridge reads the latest model catalog and reapplies saved limits to future messages. The CLI uses `AGY_CLI_AUTO_COMPACT_OVERRIDE`. - **Version Updates & Pricing Sync**: Dynamically sync [`pricing-catalog.json`](pricing-catalog.json) from GitHub, with three update modes: *Notify only, manual update* (default), *Auto update*, and *Off*. diff --git a/README.zh-CN.md b/README.zh-CN.md index 2b352ff..0060049 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -49,10 +49,10 @@ - 按步查看:列出当前会话全部步骤的模型、输出速度、首字延迟、上下文/输出 Token、缓存命中率与单步费用。 - 按模型汇总:按当前所选会话(及所选主/子代理范围)统计各模型的调用次数、均速、总 Token 与费用。 - **偏好设置** - - 选择面板挂载方式:双栖挂载(顶部栏 + 侧边栏)或仅侧边栏。 + - 选择面板挂载方式与悬浮窗宽度:支持双栖挂载(顶部栏 + 侧边栏)、输入框挂载与仅侧边栏;悬浮面板默认按表格内容自动适配增宽(免水平滚动且自动受窗口边界约束),也可手动指定 `440~1080 px` 固定宽度。 - 顶部栏与 CLI 胶囊定制:独立开关实时运行会话入口与 Antigravity CLI 底部状态胶囊(自动写入 `~/.gemini/antigravity-cli/settings.json` 的 `statusLine`),并自由定义 1 至 5 个指标槽位的显示内容与排列顺序(默认为输出速度、首字延迟、上下文占用)。 - - 调用明细显示:在「界面编排 -> 调用明细字段」可独立开关主/子代理标识、对话轮次导轨(默认关闭)与输出速度多色分级(默认关闭,以清爽单色显示输出速度,避免速度波动时色彩繁杂)。 - - 选择模型名称格式:精简代号或原始模型 ID,并可独立设置 Orchestrator 系列显示精简代号(如 `orchestrator-argon`、`worker-agy-hydrogen`)或原始完整 ID。 + - 调用明细显示与自定义 Token 组合:在「界面编排 -> 调用明细字段」可独立开关主/子代理标识、对话轮次导轨(默认关闭)与输出速度多色分级(默认关闭);默认保留 `上下文 / 思考 / 正文` 组合列,同时提供对齐计费维度的 `输入 / 输出 / 缓存` 可选组合列(悬停显示对应三项费用拆解),并支持输入 `context:new:cache`、`in:out:cache` 等自定义格式字符串自由拼装单列指标。 + - 选择模型名称格式:提供精简代号(如 `argon-4-high`)、原始模型 ID 与去前缀原始 ID(自动隐藏 `gemini-` / `claude-` / `gpt-` 前缀以节省单行空间,如 `3.8-flash-n`、`sonnet-4-6`、`5.4-mini`)三档,且自动压缩尾标(`·压缩`)独立保护永不截断;Orchestrator 系列仍可独立设置精简或完整 ID。 - 按模型设置上下文:在「其他 → 模型上下文设置」为同一模型的各推理档统一设置上限,或切回「跟随宿主设置」。列表右侧显示已设置的上限和宿主最近报告的数值。App / IDE 窗口桥接在插件启动时重新读取宿主模型目录并应用已保存的设置,适用于之后发送的消息。CLI 使用 `AGY_CLI_AUTO_COMPACT_OVERRIDE`。 - 版本更新与价格表同步:支持从云端 [`pricing-catalog.json`](pricing-catalog.json) 动态同步最新模型价格表,并提供「仅提示新版本,手动更新」(默认)、「自动更新」与「关闭更新」三档更新方式。 diff --git a/sidecars/panel/app.js b/sidecars/panel/app.js index 3233072..6562470 100644 --- a/sidecars/panel/app.js +++ b/sidecars/panel/app.js @@ -321,6 +321,7 @@ const I18N_MESSAGES_ZH_CN = Object.freeze({ 'fmt.resetMins': '{m}分钟后重置', 'fmt.selectTime': '选择时间', 'fmt.unknownModel': '模型未知', + 'fmt.compactionSuffix': '·压缩', 'fmt.mainAgent': '主代理', 'fmt.mainBadge': '主', 'fmt.subAgent': '子代理', @@ -523,6 +524,21 @@ const I18N_MESSAGES_ZH_CN = Object.freeze({ 'steps.badgeBackAll': '点击切回全部汇总', 'steps.badgeFilterAgent': '点击仅过滤查看 {agent}(按住 Ctrl/Shift 可加减多选)', 'steps.ctxTitle': '上下文 {ctx} · 思考 {think} · 正文 {resp}', + 'steps.ioCacheTitle': '新输入 {inPart} · 输出 {outPart} · 缓存读 {cachePart}', + 'steps.tokSeg.context.th': '上下文', + 'steps.tokSeg.context.tip': '上下文', + 'steps.tokSeg.in.th': '输入', + 'steps.tokSeg.in.tip': '新输入', + 'steps.tokSeg.out.th': '输出', + 'steps.tokSeg.out.tip': '输出', + 'steps.tokSeg.cache.th': '缓存', + 'steps.tokSeg.cache.tip': '缓存读', + 'steps.tokSeg.think.th': '思考', + 'steps.tokSeg.think.tip': '思考', + 'steps.tokSeg.reply.th': '正文', + 'steps.tokSeg.reply.tip': '正文', + 'steps.tokSeg.total.th': '总计', + 'steps.tokSeg.total.tip': '总计', 'steps.modelCallsShare': '{calls}次 · {pct}%', 'steps.modelCallsOnly': '{calls}次', 'steps.modelsSummaryPrefix': '{count} 模型 · ', @@ -548,6 +564,9 @@ const I18N_MESSAGES_ZH_CN = Object.freeze({ 'stepCol.duration.opt': '端到端耗时 (s)', 'stepCol.tokens_combo.th': '上下文 / 思考 / 正文', 'stepCol.tokens_combo.opt': '上下文 / 思考 / 正文 (组合)', + 'stepCol.tokens_io_cache.th': '输入 / 输出 / 缓存', + 'stepCol.tokens_io_cache.opt': '输入 / 输出 / 缓存 (组合)', + 'stepCol.tokens_custom.opt': '自定义组合 ({fmt})', 'stepCol.input_tokens.th': '新输入', 'stepCol.input_tokens.opt': '新输入 Token', 'stepCol.cache_tokens.th': '缓存读', @@ -684,6 +703,10 @@ const I18N_MESSAGES_ZH_CN = Object.freeze({ 'settings.str.mountDual': '双栖挂载 (顶部栏 + 侧边栏)', 'settings.str.mountComposer': '输入框挂载 (输入框 + 侧边栏)', 'settings.str.mountSidebar': '仅侧边栏', + 'settings.str.popoverWidthLabel': '悬浮面板宽度', + 'settings.str.popoverWidthAuto': '自适应', + 'settings.str.popoverWidthCustom': '指定宽度', + 'settings.str.popoverWidthHint': '自适应模式下在调用明细等宽表格视图自动撑宽免水平滚动;指定宽度范围 440~1080 px', 'settings.str.multiToggle': '实时运行会话入口', 'settings.str.cliCapsuleToggle': 'CLI 底部状态胶囊', 'settings.str.show': '显示', @@ -710,6 +733,9 @@ const I18N_MESSAGES_ZH_CN = Object.freeze({ 'settings.str.stepTurnRailToggle': '对话轮次导轨', 'settings.str.stepAgentBadgeToggle': '主/子代理标识', 'settings.str.stepSpeedColorToggle': '输出速度多色分级', + 'settings.str.stepTokenComboLabel': '自定义 Token 组合格式', + 'settings.str.stepTokenComboPlaceholder': '例如 context:new:cache 或 in:out:cache', + 'settings.str.stepTokenComboHint': '可用字段:context(上下文)、in/new(新输入)、out(输出)、cache(缓存)、think(思考)、reply(正文)、total(总计),以 : 或 / 分隔(1~4 项)', 'settings.str.stepsColsLabel': '「按步查看」显示列 (左 → 右,至少 2 列,可拖拽)', 'settings.str.addStepCol': '添加字段 ({count}/{max})', 'settings.slot.dragHint': '按住上下拖动调整顺序', @@ -728,6 +754,7 @@ const I18N_MESSAGES_ZH_CN = Object.freeze({ 'settings.oth.modelFormat': '模型名称格式', 'settings.oth.modelShort': '精简代号 (如 argon-4-high)', 'settings.oth.modelUpstream': '原始模型 ID (如 gemini-3.8-flash-n)', + 'settings.oth.modelUpstreamCompact': '去前缀原始 ID (如 3.8-flash-n / sonnet-4-6 / 5.4-mini)', 'settings.oth.orchFormat': 'Orchestrator 系列 ID', 'settings.oth.orchCompact': '精简代号 (如 orchestrator-argon)', 'settings.oth.orchFull': '原始完整 ID (如 gemini-pro-orchestrator)', @@ -815,6 +842,7 @@ const I18N_MESSAGES_EN_US = Object.freeze({ 'fmt.resetMins': 'Resets in {m}m', 'fmt.selectTime': 'Select time', 'fmt.unknownModel': 'Unknown model', + 'fmt.compactionSuffix': '·compact', 'fmt.mainAgent': 'Main Agent', 'fmt.mainBadge': 'Main', 'fmt.subAgent': 'Subagent', @@ -1017,6 +1045,21 @@ const I18N_MESSAGES_EN_US = Object.freeze({ 'steps.badgeBackAll': 'Click to return to All Agents', 'steps.badgeFilterAgent': 'Click to filter {agent} (Ctrl/Shift-click to multi-select)', 'steps.ctxTitle': 'Context {ctx} · Thinking {think} · Reply {resp}', + 'steps.ioCacheTitle': 'New In {inPart} · Out {outPart} · Cache Read {cachePart}', + 'steps.tokSeg.context.th': 'Ctx', + 'steps.tokSeg.context.tip': 'Context', + 'steps.tokSeg.in.th': 'In', + 'steps.tokSeg.in.tip': 'New In', + 'steps.tokSeg.out.th': 'Out', + 'steps.tokSeg.out.tip': 'Output', + 'steps.tokSeg.cache.th': 'Cache', + 'steps.tokSeg.cache.tip': 'Cache Read', + 'steps.tokSeg.think.th': 'Think', + 'steps.tokSeg.think.tip': 'Thinking', + 'steps.tokSeg.reply.th': 'Reply', + 'steps.tokSeg.reply.tip': 'Reply', + 'steps.tokSeg.total.th': 'Total', + 'steps.tokSeg.total.tip': 'Total', 'steps.modelCallsShare': '{calls} calls · {pct}%', 'steps.modelCallsOnly': '{calls} calls', 'steps.modelsSummaryPrefix': '{count} models · ', @@ -1042,6 +1085,9 @@ const I18N_MESSAGES_EN_US = Object.freeze({ 'stepCol.duration.opt': 'Total Duration (s)', 'stepCol.tokens_combo.th': 'Ctx / Think / Reply', 'stepCol.tokens_combo.opt': 'Ctx / Think / Reply (Combo)', + 'stepCol.tokens_io_cache.th': 'In / Out / Cache', + 'stepCol.tokens_io_cache.opt': 'In / Out / Cache (Combo)', + 'stepCol.tokens_custom.opt': 'Custom Combo ({fmt})', 'stepCol.input_tokens.th': 'Input', 'stepCol.input_tokens.opt': 'New Input Tokens', 'stepCol.cache_tokens.th': 'CacheTok', @@ -1178,6 +1224,10 @@ const I18N_MESSAGES_EN_US = Object.freeze({ 'settings.str.mountDual': 'Dual Mount (Titlebar + Sidebar)', 'settings.str.mountComposer': 'Composer Mount (Input Box + Sidebar)', 'settings.str.mountSidebar': 'Sidebar Only', + 'settings.str.popoverWidthLabel': 'Popover Width', + 'settings.str.popoverWidthAuto': 'Auto Fit', + 'settings.str.popoverWidthCustom': 'Custom', + 'settings.str.popoverWidthHint': 'Auto Fit widens wide tables like Step Records to avoid horizontal scroll; Custom accepts 440–1080 px', 'settings.str.multiToggle': 'Active Sessions Pill', 'settings.str.cliCapsuleToggle': 'CLI Bottom Capsule', 'settings.str.show': 'Show', @@ -1204,6 +1254,9 @@ const I18N_MESSAGES_EN_US = Object.freeze({ 'settings.str.stepTurnRailToggle': 'Turn Rail & Preview', 'settings.str.stepAgentBadgeToggle': 'Main/Sub Agent Badge', 'settings.str.stepSpeedColorToggle': 'Speed Multi-Color', + 'settings.str.stepTokenComboLabel': 'Custom Token Combo Format', + 'settings.str.stepTokenComboPlaceholder': 'e.g. context:new:cache or in:out:cache', + 'settings.str.stepTokenComboHint': 'Tokens: context, in/new, out, cache, think, reply, total — separated by : or / (1–4 items)', 'settings.str.stepsColsLabel': '"Steps" Visible Columns (Left → Right, min 2, draggable)', 'settings.str.addStepCol': 'Add Column ({count}/{max})', 'settings.slot.dragHint': 'Drag up or down to reorder', @@ -1222,6 +1275,7 @@ const I18N_MESSAGES_EN_US = Object.freeze({ 'settings.oth.modelFormat': 'Model Name Format', 'settings.oth.modelShort': 'Short Alias (e.g. argon-4-high)', 'settings.oth.modelUpstream': 'Raw Upstream ID (e.g. gemini-3.8-flash-n)', + 'settings.oth.modelUpstreamCompact': 'Trimmed Raw ID (hide gemini- / claude- / gpt-)', 'settings.oth.orchFormat': 'Orchestrator Series ID', 'settings.oth.orchCompact': 'Compact Alias (e.g. orchestrator-argon)', 'settings.oth.orchFull': 'Full Upstream ID (e.g. gemini-pro-orchestrator)', @@ -1862,6 +1916,42 @@ function formatElapsedSec(seconds) { return `${h}h ${remM}m`; } +/** + * 归一化普通模型显示模式(`'short'` 精简代号 | `'upstream_compact'` 去厂商前缀原始 ID | `'upstream'` 完整原始 ID)。 + * + * @param {unknown} mode + * @param {unknown} [legacyShowUpstream] + * @returns {'short'|'upstream_compact'|'upstream'} + */ +function normalizeModelDisplayMode(mode, legacyShowUpstream) { + const m = String(mode ?? '').trim().toLowerCase(); + if (m === 'upstream' || m === 'raw' || m === 'full') return 'upstream'; + if (m === 'upstream_compact' || m === 'compact_upstream' || m === 'trimmed') { + return 'upstream_compact'; + } + if (m === 'short') return 'short'; + if (legacyShowUpstream === true) return 'upstream'; + return 'short'; +} + +/** + * 隐藏原始模型 ID 开头的 `gemini-`、`claude-`、`gpt-` 厂商前缀以节省单行表格空间(确保剥离后非空)。 + * + * @param {string} rawText + * @returns {string} + */ +function stripVendorModelPrefix(rawText) { + return String(rawText || '') + .trim() + .replace(/^(?:gemini|claude|gpt)-(?=.+)/i, ''); +} + +function stripCompactionMarker(rawText) { + return String(rawText || '') + .replace(/(?:·(?:压缩|compact)|\[自动压缩\]|\[auto[- ]?compact\])\s*$/iu, '') + .trim(); +} + /** * 归一化 Orchestrator 系列 ID 显示格式枚举(`'compact'` 精简代号 vs `'full'` 原始完整 ID,默认 `'compact'`)。 * @@ -1878,17 +1968,23 @@ function normalizeOrchestratorIdDisplayMode(mode) { * 规则: * - Orchestrator 系列(`Orchestrator (Carbon)`、`Orchestrator (Argon)`、`gemini-flash-worker-agy` / `M669`) * 独立遵循 `orchestratorIdMode`(`'compact'` 显示 `orchestrator-carbon` / `orchestrator-argon` / `worker-agy-hydrogen`; - * `'full'` 显示带括号标注的完整上游 ID),不受普通模型“精简代号 / 原始模型 ID”切换影响; - * - 其余普通模型继续按 `showUpstreamModel` 与视图选项切换。 + * `'full'` 显示带括号标注的完整上游 ID),不受普通模型显示模式影响; + * - 其余普通模型遵循 `modelDisplayMode`(`'short'` | `'upstream_compact'` | `'upstream'`,兼容旧版布尔 `showUpstreamModel`); + * - 独立拆分 `baseText` 与 `compactionSuffix`(`·压缩`),供步骤表单独保护压缩尾标永不截断。 * * @param {object|null|undefined} target - * @param {{ showUpstreamModel?: boolean, orchestratorIdMode?: string }|boolean} [settings] + * @param {{ modelDisplayMode?: string, showUpstreamModel?: boolean, orchestratorIdMode?: string }|boolean} [settings] * @param {{ preferLabelInShort?: boolean, fallback?: string }} [options] - * @returns {{ text: string, title: string, isOrchestratorSeries: boolean }} + * @returns {{ text: string, baseText: string, compactionSuffix: string, isCompaction: boolean, title: string, isOrchestratorSeries: boolean }} */ function resolveModelDisplayInfo(target, settings = {}, options = {}) { - const showUpstream = - typeof settings === 'boolean' ? settings : Boolean(settings?.showUpstreamModel); + const modelDisplayMode = + typeof settings === 'boolean' + ? settings + ? 'upstream' + : 'short' + : normalizeModelDisplayMode(settings?.modelDisplayMode, settings?.showUpstreamModel); + const showUpstream = modelDisplayMode !== 'short'; const orchestratorMode = normalizeOrchestratorIdDisplayMode( typeof settings === 'object' && settings ? settings.orchestratorIdMode : 'compact' ); @@ -1896,7 +1992,14 @@ function resolveModelDisplayInfo(target, settings = {}, options = {}) { const fallback = options?.fallback ?? t('fmt.unknownModel'); if (!target || typeof target !== 'object') { - return { text: fallback, title: fallback, isOrchestratorSeries: false }; + return { + text: fallback, + baseText: fallback, + compactionSuffix: '', + isCompaction: false, + title: fallback, + isOrchestratorSeries: false, + }; } const shortText = String( @@ -1913,9 +2016,12 @@ function resolveModelDisplayInfo(target, settings = {}, options = {}) { const isCompaction = Boolean(target.isCompactionStep) || - /·压缩\s*$/.test(shortText) || - /·压缩\s*$/.test(upstreamText) || - /·压缩\s*$/.test(labelText); + /·(?:压缩|compact)\s*$/iu.test(shortText) || + /·(?:压缩|compact)\s*$/iu.test(upstreamText) || + /·(?:压缩|compact)\s*$/iu.test(labelText) || + /\[自动压缩\]\s*$/u.test(labelText); + + const compactionSuffix = isCompaction ? t('fmt.compactionSuffix') : ''; const combined = `${modelKeyText} ${shortText} ${labelText} ${upstreamText} ${responseText}`; const isOrchWorker = @@ -1938,27 +2044,50 @@ function resolveModelDisplayInfo(target, settings = {}, options = {}) { compactId = 'orchestrator-argon'; fullId = 'gemini-pro-orchestrator-argon (argon-4)'; } - const suffix = isCompaction ? '·压缩' : ''; - const text = `${orchestratorMode === 'full' ? fullId : compactId}${suffix}`; + const baseText = orchestratorMode === 'full' ? fullId : compactId; + const text = `${baseText}${compactionSuffix}`; const multiUpstream = Array.isArray(target.upstreamModels) && target.upstreamModels.length > 1 ? target.upstreamModels.join(' / ') : ''; - const title = multiUpstream || `${fullId}${suffix}`; - return { text, title, isOrchestratorSeries: true }; + const title = multiUpstream || `${fullId}${compactionSuffix}`; + return { + text, + baseText, + compactionSuffix, + isCompaction, + title, + isOrchestratorSeries: true, + }; } - const standardText = + const rawSelected = (showUpstream ? upstreamText || responseText || shortText || labelText : preferLabelInShort ? labelText || shortText : shortText || labelText) || fallback; + const cleanRawBase = stripCompactionMarker(rawSelected) || fallback; + const baseText = + modelDisplayMode === 'upstream_compact' + ? stripVendorModelPrefix(cleanRawBase) || cleanRawBase + : cleanRawBase; + const text = `${baseText}${compactionSuffix}`; + const fullUpstreamWithSuffix = `${cleanRawBase}${compactionSuffix}`; const standardTitle = showUpstream && Array.isArray(target.upstreamModels) && target.upstreamModels.length > 1 ? target.upstreamModels.join(' / ') - : standardText; - return { text: standardText, title: standardTitle, isOrchestratorSeries: false }; + : showUpstream + ? fullUpstreamWithSuffix + : text; + return { + text, + baseText, + compactionSuffix, + isCompaction, + title: standardTitle, + isOrchestratorSeries: false, + }; } /** @@ -1974,7 +2103,9 @@ function resolveModelDisplayInfo(target, settings = {}, options = {}) { function mergeModelsByDisplayName(models = [], settings = {}, options = {}) { if (!Array.isArray(models) || models.length === 0) return []; const showUpstream = - typeof settings === 'boolean' ? settings : Boolean(settings?.showUpstreamModel); + typeof settings === 'boolean' + ? settings + : normalizeModelDisplayMode(settings?.modelDisplayMode, settings?.showUpstreamModel) !== 'short'; const byDisplay = new Map(); for (const m of models) { @@ -2247,7 +2378,7 @@ function callViaCdpBridge(bridge, path, { method, body, signal, cdpTimeoutMs }) }); } -async function callSidecarApi(path, { method = 'GET', body, abortKey = '', cdpTimeoutMs = 8000 } = {}) { +async function callSidecarApi(path, { method = 'GET', body, abortKey = '', cdpTimeoutMs = 3500 } = {}) { const upperMethod = method.toUpperCase(); const dedupKey = upperMethod === 'GET' && !abortKey ? path : ''; @@ -2267,43 +2398,27 @@ async function callSidecarApi(path, { method = 'GET', body, abortKey = '', cdpTi signal = requestController.signal; } - const runRequest = async () => { + const execHttpOnce = async (activeBridge) => { const sidecar = typeof window !== 'undefined' ? window.sidecar : null; - const bridge = !sidecar?.fetch ? getWindowBridgeConfig() : null; const fetchFn = sidecar?.fetch ? sidecar.fetch.bind(sidecar) : typeof window !== 'undefined' && typeof window.fetch === 'function' ? window.fetch.bind(window) : globalThis.fetch.bind(globalThis); - - const requestUrl = bridge ? `http://127.0.0.1:${bridge.port}${path}` : path; + const requestUrl = activeBridge ? `http://127.0.0.1:${activeBridge.port}${path}` : path; const headers = {}; - if (body !== undefined || !bridge) { + if (body !== undefined || !activeBridge) { headers['Content-Type'] = 'application/json'; } - if (bridge) { - headers['X-Usage-Monitor-Token'] = bridge.token; - if (bridge.transportMode === 'cdp') { - return callViaCdpBridge(bridge, path, { method: upperMethod, body, signal, cdpTimeoutMs }); - } - } - - let res; - try { - res = await fetchFn(requestUrl, { - method: upperMethod, - headers, - body: body === undefined ? undefined : JSON.stringify(body), - signal, - }); - } catch (fetchErr) { - if (!bridge || signal?.aborted || fetchErr?.name === 'AbortError') { - throw fetchErr; - } - // 当宿主环境的 CSP connect-src、混合内容策略或本地代理阻断 HTTP 直连时,无缝切至 CDP 通道 - bridge.transportMode = 'cdp'; - return callViaCdpBridge(bridge, path, { method: upperMethod, body, signal, cdpTimeoutMs }); + if (activeBridge) { + headers['X-Usage-Monitor-Token'] = activeBridge.token; } + const res = await fetchFn(requestUrl, { + method: upperMethod, + headers, + body: body === undefined ? undefined : JSON.stringify(body), + signal, + }); if (signal?.aborted) { const abortErr = new Error('Request aborted'); abortErr.name = 'AbortError'; @@ -2318,9 +2433,60 @@ async function callSidecarApi(path, { method = 'GET', body, abortKey = '', cdpTi if (!res.ok) { throw new Error(data.error || `HTTP ${res.status}`); } + if (activeBridge) { + activeBridge.transportMode = 'http'; + } return data; }; + const fallbackViaCdpWithRecovery = async (initialBridge) => { + initialBridge.transportMode = 'cdp'; + try { + return await callViaCdpBridge(initialBridge, path, { + method: upperMethod, + body, + signal, + cdpTimeoutMs, + }); + } catch (cdpErr) { + if (signal?.aborted || cdpErr?.name === 'AbortError') { + throw cdpErr; + } + // 热更新或守护重启期间若 CDP 尚未轮询投递,自动重置回 HTTP 并用最新桥接端口重试一次 + const latestBridge = getWindowBridgeConfig() || initialBridge; + latestBridge.transportMode = 'http'; + return execHttpOnce(latestBridge); + } + }; + + const runRequest = async () => { + const sidecar = typeof window !== 'undefined' ? window.sidecar : null; + const bridge = !sidecar?.fetch ? getWindowBridgeConfig() : null; + if (bridge && bridge.transportMode === 'cdp') { + return fallbackViaCdpWithRecovery(bridge); + } + + try { + return await execHttpOnce(bridge); + } catch (fetchErr) { + if (signal?.aborted || fetchErr?.name === 'AbortError') { + throw fetchErr; + } + if (!bridge) { + // 辅助栏 Webview 在 Worker 热替换瞬间若有极短暂波动,延迟 180ms 自动无感重试一次 + if (upperMethod === 'GET') { + await new Promise((r) => setTimeout(r, 180)); + if (!signal?.aborted) { + return execHttpOnce(null); + } + } + throw fetchErr; + } + // 当宿主环境的 CSP connect-src、混合内容策略或瞬态端口切换阻断直连时,切至带 HTTP 自愈恢复的 CDP 通道 + return fallbackViaCdpWithRecovery(bridge); + } + }; + const promise = runRequest().finally(() => { if (dedupKey) { inFlightGets.delete(dedupKey); @@ -2471,6 +2637,7 @@ let feedbackTimer = null; let activeBundleVersion = 0; let latestRequestSeq = 0; let activeForceCount = 0; +let consecutivePollFailures = 0; let inFlightTelemetryPoll = null; let lastReceivedSteps = null; let loopActive = false; @@ -2538,6 +2705,8 @@ const CLIENT_STEP_COLUMN_KEYS = Object.freeze([ 'ttft', 'duration', 'tokens_combo', + 'tokens_io_cache', + 'tokens_custom', 'input_tokens', 'cache_tokens', 'output_tokens', @@ -2555,6 +2724,94 @@ const DEFAULT_CLIENT_STEP_COLUMNS = Object.freeze([ 'cost', ]); +const DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT = 'context:in:cache'; + +const CLIENT_STEP_TOKEN_ALIASES = Object.freeze({ + context: 'context', + ctx: 'context', + '上下文': 'context', + in: 'in', + new: 'in', + input: 'in', + '输入': 'in', + '新输入': 'in', + out: 'out', + output: 'out', + '输出': 'out', + '总输出': 'out', + cache: 'cache', + cached: 'cache', + cache_read: 'cache', + cacheread: 'cache', + '缓存': 'cache', + '缓存读': 'cache', + think: 'think', + thinking: 'think', + '思考': 'think', + reply: 'reply', + body: 'reply', + resp: 'reply', + response: 'reply', + text: 'reply', + '正文': 'reply', + '回复': 'reply', + total: 'total', + all: 'total', + sum: 'total', + '总计': 'total', + '总token': 'total', +}); + +function parseClientStepTokenComboSegments(raw) { + const str = String(raw ?? '').trim(); + const out = []; + const seen = new Set(); + if (str) { + for (const part of str.split(/[:/,|]+/)) { + const key = part.trim().toLowerCase(); + if (!key) continue; + const mapped = Object.prototype.hasOwnProperty.call(CLIENT_STEP_TOKEN_ALIASES, key) + ? CLIENT_STEP_TOKEN_ALIASES[key] + : null; + if (!mapped || seen.has(mapped)) continue; + seen.add(mapped); + out.push(mapped); + if (out.length >= 4) break; + } + } + return out.length > 0 ? out : ['context', 'in', 'cache']; +} + +function normalizeClientStepTokenComboFormat(raw) { + return parseClientStepTokenComboSegments(raw).join(':'); +} + +const MIN_CLIENT_POPOVER_WIDTH = 440; +const MAX_CLIENT_POPOVER_WIDTH = 1080; +const DEFAULT_CLIENT_POPOVER_WIDTH = 580; + +function normalizeClientPopoverWidthMode(mode) { + const m = String(mode ?? '').trim().toLowerCase(); + return m === 'custom' || m === 'fixed' ? 'custom' : 'auto'; +} + +function normalizeClientPopoverWidth(width) { + const num = Number(width); + if (!Number.isFinite(num) || num <= 0) return DEFAULT_CLIENT_POPOVER_WIDTH; + return Math.max(MIN_CLIENT_POPOVER_WIDTH, Math.min(MAX_CLIENT_POPOVER_WIDTH, Math.round(num))); +} + +function normalizeClientModelDisplayMode(mode, legacyShowUpstream) { + const m = String(mode ?? '').trim().toLowerCase(); + if (m === 'upstream' || m === 'raw' || m === 'full') return 'upstream'; + if (m === 'upstream_compact' || m === 'compact_upstream' || m === 'trimmed') { + return 'upstream_compact'; + } + if (m === 'short') return 'short'; + if (legacyShowUpstream === true) return 'upstream'; + return 'short'; +} + function normalizeClientOrderedList(raw, { allowedKeys, defaultItems, minLen, maxLen }) { if (!Array.isArray(raw)) { return [...defaultItems]; @@ -2652,7 +2909,7 @@ function normalizeClientStepColumns(raw) { allowedKeys: CLIENT_STEP_COLUMN_KEYS, defaultItems: DEFAULT_CLIENT_STEP_COLUMNS, minLen: 2, - maxLen: 12, + maxLen: CLIENT_STEP_COLUMN_KEYS.length, }); } @@ -2727,7 +2984,42 @@ function isEquivalentTelemetry(prev, next) { ) { return false; } + const normModelMode = (mode, legacyShowUpstream) => { + const m = String(mode ?? '').trim().toLowerCase(); + if (m === 'upstream' || m === 'raw' || m === 'full') return 'upstream'; + if (m === 'upstream_compact' || m === 'compact_upstream' || m === 'trimmed') { + return 'upstream_compact'; + } + if (m === 'short') return 'short'; + return legacyShowUpstream === true ? 'upstream' : 'short'; + }; + const normPopMode = (mode) => { + const m = String(mode ?? '').trim().toLowerCase(); + return m === 'custom' || m === 'fixed' ? 'custom' : 'auto'; + }; + const normPopW = (w) => { + const n = Number(w); + return Number.isFinite(n) && n > 0 ? Math.max(440, Math.min(1080, Math.round(n))) : 580; + }; + if ( + normModelMode(pSet?.modelDisplayMode, pSet?.showUpstreamModel) !== + normModelMode(nSet?.modelDisplayMode, nSet?.showUpstreamModel) + ) { + return false; + } if (pSet?.showUpstreamModel !== nSet?.showUpstreamModel) return false; + if (normPopMode(pSet?.popoverWidthMode) !== normPopMode(nSet?.popoverWidthMode)) { + return false; + } + if (normPopW(pSet?.popoverWidth) !== normPopW(nSet?.popoverWidth)) { + return false; + } + if ( + String(pSet?.stepTokenComboFormat ?? 'context:in:cache').trim() !== + String(nSet?.stepTokenComboFormat ?? 'context:in:cache').trim() + ) { + return false; + } const prevOrchIdMode = String(pSet?.orchestratorIdMode ?? 'compact').trim().toLowerCase() === 'full' ? 'full' @@ -3311,6 +3603,7 @@ async function requestTelemetry(params, force) { } activeBundleVersion = payload.uiBundleVersion; } + consecutivePollFailures = 0; const latestSt = telemetryStore.getState(); const previousAccount = latestSt.telemetry?.account; @@ -3351,7 +3644,10 @@ async function requestTelemetry(params, force) { return payload; } catch (err) { if (err?.name === 'AbortError' || reqSeq !== latestRequestSeq) return null; - telemetryStore.setState({ syncState: 'error' }); + consecutivePollFailures += 1; + if (force || consecutivePollFailures >= 2 || !telemetryStore.getState().telemetry) { + telemetryStore.setState({ syncState: 'error' }); + } return null; } finally { if (force) { @@ -3409,6 +3705,28 @@ async function saveSettingsPatch(patch) { if (Object.prototype.hasOwnProperty.call(safePatch, 'stepTableColumns')) { safePatch.stepTableColumns = normalizeClientStepColumns(safePatch.stepTableColumns); } + if (Object.prototype.hasOwnProperty.call(safePatch, 'stepTokenComboFormat')) { + safePatch.stepTokenComboFormat = normalizeClientStepTokenComboFormat( + safePatch.stepTokenComboFormat + ); + } + if (Object.prototype.hasOwnProperty.call(safePatch, 'modelDisplayMode')) { + const normDisp = normalizeClientModelDisplayMode( + safePatch.modelDisplayMode, + safePatch.showUpstreamModel + ); + safePatch.modelDisplayMode = normDisp; + safePatch.showUpstreamModel = normDisp !== 'short'; + } else if (Object.prototype.hasOwnProperty.call(safePatch, 'showUpstreamModel')) { + safePatch.showUpstreamModel = Boolean(safePatch.showUpstreamModel); + safePatch.modelDisplayMode = safePatch.showUpstreamModel ? 'upstream' : 'short'; + } + if (Object.prototype.hasOwnProperty.call(safePatch, 'popoverWidthMode')) { + safePatch.popoverWidthMode = normalizeClientPopoverWidthMode(safePatch.popoverWidthMode); + } + if (Object.prototype.hasOwnProperty.call(safePatch, 'popoverWidth')) { + safePatch.popoverWidth = normalizeClientPopoverWidth(safePatch.popoverWidth); + } if (Object.prototype.hasOwnProperty.call(safePatch, 'orchestratorIdMode')) { safePatch.orchestratorIdMode = normalizeClientOrchestratorIdMode(safePatch.orchestratorIdMode); } @@ -10262,6 +10580,8 @@ const STEP_COLUMN_KEYS = Object.freeze([ 'ttft', 'duration', 'tokens_combo', + 'tokens_io_cache', + 'tokens_custom', 'input_tokens', 'cache_tokens', 'output_tokens', @@ -10279,6 +10599,74 @@ const DEFAULT_STEP_COLUMN_KEYS = Object.freeze([ 'cost', ]); +function resolveStepTokenParts(st) { + const inTok = Math.max(0, Number(st?.inputTokens || 0)); + const cacheTok = Math.max(0, Number(st?.cacheReadTokens || 0)); + const outTok = Math.max(0, Number(st?.outputTokens || 0)); + const thinkTok = Math.max(0, Number(st?.thinkingTokens || 0)); + const respTok = + st?.responseTokens != null + ? Math.max(0, Number(st.responseTokens)) + : Math.max(0, outTok - thinkTok); + const totalTok = inTok + cacheTok + outTok; + const ctxTok = Math.max(0, Number(st?.contextTokens ?? totalTok)); + return { inTok, cacheTok, outTok, thinkTok, respTok, totalTok, ctxTok }; +} + +function formatPartWithOptionalCost(tokCount, costVal) { + const countStr = Math.round(Number(tokCount || 0)).toLocaleString(); + if (costVal != null && Number.isFinite(Number(costVal))) { + return `${countStr} (${formatUsd(Number(costVal), { precise: true })})`; + } + return countStr; +} + +function formatCustomTokenComboHeader(rawFormat) { + const segs = parseClientStepTokenComboSegments(rawFormat); + return segs.map((seg) => t(`steps.tokSeg.${seg}.th`)).join(' / '); +} + +function resolveSegmentDisplay(seg, parts, st) { + switch (seg) { + case 'context': + return { + short: formatTokens(parts.ctxTok), + tip: `${t('steps.tokSeg.context.tip')} ${Math.round(parts.ctxTok).toLocaleString()}`, + }; + case 'in': + return { + short: formatTokens(parts.inTok), + tip: `${t('steps.tokSeg.in.tip')} ${formatPartWithOptionalCost(parts.inTok, st?.inCostUsd)}`, + }; + case 'out': + return { + short: formatTokens(parts.outTok), + tip: `${t('steps.tokSeg.out.tip')} ${formatPartWithOptionalCost(parts.outTok, st?.outCostUsd)}`, + }; + case 'cache': + return { + short: formatTokens(parts.cacheTok), + tip: `${t('steps.tokSeg.cache.tip')} ${formatPartWithOptionalCost(parts.cacheTok, st?.cacheCostUsd)}`, + }; + case 'think': + return { + short: formatTokens(parts.thinkTok), + tip: `${t('steps.tokSeg.think.tip')} ${Math.round(parts.thinkTok).toLocaleString()}`, + }; + case 'reply': + return { + short: formatTokens(parts.respTok), + tip: `${t('steps.tokSeg.reply.tip')} ${Math.round(parts.respTok).toLocaleString()}`, + }; + case 'total': + default: + return { + short: formatTokens(parts.totalTok), + tip: `${t('steps.tokSeg.total.tip')} ${formatPartWithOptionalCost(parts.totalTok, st?.costUsd)}`, + }; + } +} + function formatStepClockTime(createdAtMs) { const ms = Number(createdAtMs || 0); if (!(ms > 0) || !Number.isFinite(ms)) { @@ -10330,19 +10718,21 @@ const STEP_COLUMN_REGISTRY = Object.freeze({ optLabel: () => t('stepCol.model.opt'), sigPart: (st, ctx) => { const disp = resolveModelDisplayInfo(st, ctx.displaySettings, { fallback: '—' }); - return `${disp.text}:${disp.title}`; + return `${disp.baseText}:${disp.compactionSuffix}:${disp.title}`; }, createCell: () => { - const valEl = h('span', { - style: - 'display: inline-block; max-width: 120px; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom;', - }); - const td = h('td', {}, [valEl]); - return { td, refs: { td, valEl } }; + const nameEl = h('span', { className: 'step-model-name' }); + const compactEl = h('span', { className: 'step-model-compact', hidden: true }); + const wrapEl = h('span', { className: 'step-model-cell' }, [nameEl, compactEl]); + const td = h('td', {}, [wrapEl]); + return { td, refs: { td, wrapEl, nameEl, compactEl } }; }, updateCell: (refs, st, ctx) => { const modelDisplay = resolveModelDisplayInfo(st, ctx.displaySettings, { fallback: '—' }); - patchText(refs.valEl, modelDisplay.text); + patchText(refs.nameEl, modelDisplay.baseText); + const suffix = modelDisplay.compactionSuffix || ''; + patchText(refs.compactEl, suffix); + patchAttr(refs.compactEl, 'hidden', !suffix); patchAttr(refs.td, 'title', modelDisplay.title); }, }, @@ -10437,28 +10827,82 @@ const STEP_COLUMN_REGISTRY = Object.freeze({ return { td, refs: { td, valEl } }; }, updateCell: (refs, st) => { - const outTok = Math.max(0, Number(st.outputTokens || 0)); - const thinkTok = Math.max(0, Number(st.thinkingTokens || 0)); - const respTok = - st.responseTokens != null - ? Math.max(0, Number(st.responseTokens)) - : Math.max(0, outTok - thinkTok); + const parts = resolveStepTokenParts(st); patchText( refs.valEl, - `${formatTokens(st.contextTokens)} / ${formatTokens(thinkTok)} / ${formatTokens(respTok)}` + `${formatTokens(parts.ctxTok)} / ${formatTokens(parts.thinkTok)} / ${formatTokens(parts.respTok)}` ); patchAttr( refs.td, 'title', t('steps.ctxTitle', { - ctx: Math.round(Number(st.contextTokens || 0)), - think: Math.round(thinkTok), - resp: Math.round(respTok), + ctx: Math.round(parts.ctxTok), + think: Math.round(parts.thinkTok), + resp: Math.round(parts.respTok), }) ); }, }, + tokens_io_cache: { + key: 'tokens_io_cache', + thLabel: () => t('stepCol.tokens_io_cache.th'), + optLabel: () => t('stepCol.tokens_io_cache.opt'), + sigPart: (st) => + `${st.inputTokens || 0}:${st.outputTokens || 0}:${st.cacheReadTokens || 0}:${st.inCostUsd ?? ''}:${st.outCostUsd ?? ''}:${st.cacheCostUsd ?? ''}`, + createCell: () => { + const valEl = h('span'); + const td = h('td', {}, [valEl]); + return { td, refs: { td, valEl } }; + }, + updateCell: (refs, st) => { + const parts = resolveStepTokenParts(st); + patchText( + refs.valEl, + `${formatTokens(parts.inTok)} / ${formatTokens(parts.outTok)} / ${formatTokens(parts.cacheTok)}` + ); + patchAttr( + refs.td, + 'title', + t('steps.ioCacheTitle', { + inPart: formatPartWithOptionalCost(parts.inTok, st?.inCostUsd), + outPart: formatPartWithOptionalCost(parts.outTok, st?.outCostUsd), + cachePart: formatPartWithOptionalCost(parts.cacheTok, st?.cacheCostUsd), + }) + ); + }, + }, + + tokens_custom: { + key: 'tokens_custom', + thLabel: (ctx) => formatCustomTokenComboHeader(ctx?.stepTokenComboFormat), + optLabel: (ctx) => + t('stepCol.tokens_custom.opt', { + fmt: formatCustomTokenComboHeader(ctx?.stepTokenComboFormat), + }), + sigPart: (st, ctx) => + `${normalizeClientStepTokenComboFormat(ctx?.stepTokenComboFormat)}:${st.contextTokens || 0}:${st.inputTokens || 0}:${st.cacheReadTokens || 0}:${st.outputTokens || 0}:${st.thinkingTokens || 0}:${st.responseTokens ?? ''}:${st.costUsd ?? ''}`, + createCell: () => { + const valEl = h('span'); + const td = h('td', {}, [valEl]); + return { td, refs: { td, valEl } }; + }, + updateCell: (refs, st, ctx) => { + const segs = parseClientStepTokenComboSegments(ctx?.stepTokenComboFormat); + const parts = resolveStepTokenParts(st); + const rendered = segs.map((seg) => resolveSegmentDisplay(seg, parts, st)); + patchText( + refs.valEl, + rendered.map((r) => r.short).join(' / ') + ); + patchAttr( + refs.td, + 'title', + rendered.map((r) => r.tip).join(' · ') + ); + }, + }, + input_tokens: { key: 'input_tokens', thLabel: () => t('stepCol.input_tokens.th'), @@ -10548,10 +10992,13 @@ const STEP_COLUMN_REGISTRY = Object.freeze({ }, }); -function getStepColumnOptions() { +function getStepColumnOptions( + stepTokenComboFormat = DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT +) { + const ctx = { stepTokenComboFormat }; return STEP_COLUMN_KEYS.map((key) => ({ value: key, - label: STEP_COLUMN_REGISTRY[key].optLabel(), + label: STEP_COLUMN_REGISTRY[key].optLabel(ctx), })); } @@ -10943,17 +11390,19 @@ const StepTableWidget = defineComponent({ const stepsTheadRow = h('tr'); const stepsTbody = h('tbody'); - function syncTableHeaderColumns(colKeys) { + function syncTableHeaderColumns(colKeys, headerCtx = {}) { const locale = getEffectiveLocale(); + const fmtKey = normalizeClientStepTokenComboFormat(headerCtx?.stepTokenComboFormat); + const ctx = { stepTokenComboFormat: fmtKey }; patchKeyedList(stepsTheadRow, colKeys, { keyFn: (colKey) => colKey, - sigFn: (colKey) => `${locale}:${colKey}:${STEP_COLUMN_REGISTRY[colKey].thLabel()}`, + sigFn: (colKey) => `${locale}:${colKey}:${fmtKey}:${STEP_COLUMN_REGISTRY[colKey].thLabel(ctx)}`, createRow: (colKey) => { - const th = h('th', { text: STEP_COLUMN_REGISTRY[colKey].thLabel() }); + const th = h('th', { text: STEP_COLUMN_REGISTRY[colKey].thLabel(ctx) }); return { el: th, refs: { th } }; }, updateRow: (refs, colKey) => { - patchText(refs.th, STEP_COLUMN_REGISTRY[colKey].thLabel()); + patchText(refs.th, STEP_COLUMN_REGISTRY[colKey].thLabel(ctx)); }, }); } @@ -11067,13 +11516,16 @@ const StepTableWidget = defineComponent({ showStepTurnRail = false, showStepAgentBadge = true, showStepSpeedColor = false, + stepTokenComboFormat = undefined, displaySettings = {}, } = {} ) { const colOrderSig = colKeys.join(','); + const normTokenFmt = normalizeClientStepTokenComboFormat(stepTokenComboFormat); const renderOptions = JSON.stringify([ cascadeId, locale, colOrderSig, effectiveScopes, hasSubagents, isMergedMultiAgents, - showStepTurnRail, showStepAgentBadge, showStepSpeedColor, displaySettings.showUpstreamModel, + showStepTurnRail, showStepAgentBadge, showStepSpeedColor, normTokenFmt, + displaySettings.modelDisplayMode, displaySettings.showUpstreamModel, displaySettings.orchestratorIdMode, ]); if (lastRenderOptions === renderOptions && lastRenderedRows && @@ -11082,13 +11534,14 @@ const StepTableWidget = defineComponent({ return; } const wantedSet = new Set(colKeys); - syncTableHeaderColumns(colKeys); + syncTableHeaderColumns(colKeys, { stepTokenComboFormat: normTokenFmt }); const showAgentBadge = Boolean(hasSubagents && showStepAgentBadge); const cellCtx = { hasSubagents, showAgentBadge, showStepSpeedColor, + stepTokenComboFormat: normTokenFmt, effectiveScopes, scopeSigKey: effectiveScopes.join(','), displaySettings, @@ -11392,8 +11845,9 @@ function buildStructurePreviewTelemetry(baseSettings = {}, draftOverrides = {}) agentBadge: '主', agentLabel: t('fmt.mainAgent'), modelLabel: 'Gemini 4 Carbon (High)', - modelShort: 'gemini-4-carbon', + modelShort: 'carbon-4-high', rawModel: 'MODEL_PLACEHOLDER_M37', + modelUpstream: 'gemini-4-carbon-high', upstreamModel: 'gemini-4-carbon-high', tokPerSec: 68.4, ttftSec: 1.15, @@ -11407,6 +11861,9 @@ function buildStructurePreviewTelemetry(baseSettings = {}, draftOverrides = {}) responseTokens: 560, cacheHitPct: 94.5, costUsd: 0.042, + inCostUsd: 0.0156, + cacheCostUsd: 0.0134, + outCostUsd: 0.0142, createdAtMs: anchorMinMs - 14_000, turnIndex: 2, rootTurnIndex: 2, @@ -11427,8 +11884,9 @@ function buildStructurePreviewTelemetry(baseSettings = {}, draftOverrides = {}) agentRole: subRole, agentLabel: subRole, modelLabel: 'Gemini 3.8 Flash', - modelShort: 'gemini-3.8-flash', + modelShort: 'flash-3.8', rawModel: 'MODEL_PLACEHOLDER_M42', + modelUpstream: 'gemini-3.8-flash-n', upstreamModel: 'gemini-3.8-flash-n', tokPerSec: 96.2, ttftSec: 0.68, @@ -11442,6 +11900,9 @@ function buildStructurePreviewTelemetry(baseSettings = {}, draftOverrides = {}) responseTokens: 464, cacheHitPct: 88.1, costUsd: 0.009, + inCostUsd: 0.0065, + cacheCostUsd: 0.0005, + outCostUsd: 0.0026, createdAtMs: anchorMinMs - 42_000, turnIndex: 1, rootTurnIndex: 2, @@ -11460,9 +11921,11 @@ function buildStructurePreviewTelemetry(baseSettings = {}, draftOverrides = {}) isSubagent: false, agentBadge: '主', agentLabel: t('fmt.mainAgent'), - modelLabel: 'Gemini 4 Carbon (High)', - modelShort: 'gemini-4-carbon', + isCompactionStep: true, + modelLabel: 'Gemini 4 Carbon (High) [自动压缩]', + modelShort: 'carbon-4-high·压缩', rawModel: 'MODEL_PLACEHOLDER_M37', + modelUpstream: 'gemini-4-carbon-high·压缩', upstreamModel: 'gemini-4-carbon-high', tokPerSec: 46.5, ttftSec: 3.4, @@ -11476,6 +11939,9 @@ function buildStructurePreviewTelemetry(baseSettings = {}, draftOverrides = {}) responseTokens: 380, cacheHitPct: 61.4, costUsd: 0.068, + inCostUsd: 0.0492, + cacheCostUsd: 0.0078, + outCostUsd: 0.011, createdAtMs: anchorMinMs - 88_000, turnIndex: 2, rootTurnIndex: 2, @@ -12937,6 +13403,13 @@ function getEnableToggleOptions() { ]; } +function getPopoverWidthModeOptions() { + return [ + { value: 'auto', label: t('settings.str.popoverWidthAuto') }, + { value: 'custom', label: t('settings.str.popoverWidthCustom') }, + ]; +} + function isSameList(a, b) { if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false; for (let i = 0; i < a.length; i += 1) { @@ -12950,6 +13423,8 @@ const SettingsStructureTab = defineComponent({ setup({ props, onCleanup }) { let activeThirdTab = normalizeStructureTabKey(uiStore.getState().settingsStructureTab); let draftWindowMountMode = 'dual'; + let draftPopoverWidthMode = 'auto'; + let draftPopoverWidth = DEFAULT_CLIENT_POPOVER_WIDTH; let draftEnableCliStatusLine = true; let draftShowTopbarMulti = true; let draftTopbarSlots = [...DEFAULT_CLIENT_TOPBAR_SLOTS]; @@ -12959,6 +13434,8 @@ const SettingsStructureTab = defineComponent({ let draftShowStepTurnRail = false; let draftShowStepAgentBadge = true; let draftShowStepSpeedColor = false; + let draftStepTokenComboFormat = DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT; + let rawStepTokenComboText = DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT; let draftStepColumns = [...DEFAULT_CLIENT_STEP_COLUMNS]; let latestSettings = null; @@ -12971,6 +13448,8 @@ const SettingsStructureTab = defineComponent({ return buildStructurePreviewTelemetry(latestSettings, { windowMountMode: draftWindowMountMode, enableWindowOverlay: draftWindowMountMode !== 'sidebar', + popoverWidthMode: draftPopoverWidthMode, + popoverWidth: draftPopoverWidth, enableCliStatusLine: draftEnableCliStatusLine, showTopbarMultiSession: draftShowTopbarMulti, topbarCapsuleSlots: draftTopbarSlots, @@ -12980,6 +13459,7 @@ const SettingsStructureTab = defineComponent({ showStepTurnRail: draftShowStepTurnRail, showStepAgentBadge: draftShowStepAgentBadge, showStepSpeedColor: draftShowStepSpeedColor, + stepTokenComboFormat: draftStepTokenComboFormat, stepTableColumns: draftStepColumns, }); } @@ -13046,6 +13526,14 @@ const SettingsStructureTab = defineComponent({ className: 'form-label', text: t('settings.str.mountMode'), }); + const popoverWidthLabelEl = h('span', { + className: 'form-label', + text: t('settings.str.popoverWidthLabel'), + }); + const popoverWidthHintEl = h('span', { + className: 'settings-hint-text', + text: t('settings.str.popoverWidthHint'), + }); const multiToggleLabelEl = h('span', { className: 'form-label', text: t('settings.str.multiToggle'), @@ -13075,6 +13563,54 @@ const SettingsStructureTab = defineComponent({ }, }); + const popoverWidthInput = h('input', { + type: 'number', + className: 'settings-text-input', + min: String(MIN_CLIENT_POPOVER_WIDTH), + max: String(MAX_CLIENT_POPOVER_WIDTH), + step: '10', + value: String(draftPopoverWidth), + 'aria-label': t('settings.str.popoverWidthLabel'), + onChange: () => { + const nextWidth = normalizeClientPopoverWidth(popoverWidthInput.value); + draftPopoverWidth = nextWidth; + popoverWidthInput.value = String(nextWidth); + syncMountSectionMeta(); + props.onLayoutChange?.(); + void persistStructurePatch(mountSection, { + popoverWidthMode: draftPopoverWidthMode, + popoverWidth: draftPopoverWidth, + }); + }, + onKeydown: (e) => { + if (e?.key === 'Enter') { + e.preventDefault(); + popoverWidthInput.blur(); + } + }, + }); + + const popoverWidthNumWrap = h( + 'span', + { className: 'settings-number-wrap', hidden: draftPopoverWidthMode !== 'custom' }, + [popoverWidthInput, h('span', { className: 'settings-number-unit', text: 'px' })] + ); + + const popoverWidthModeToggle = SegmentedControl({ + ariaLabel: t('settings.str.popoverWidthLabel'), + modelValue: draftPopoverWidthMode, + options: getPopoverWidthModeOptions(), + onUpdateModelValue: (val) => { + draftPopoverWidthMode = normalizeClientPopoverWidthMode(val); + syncMountSectionMeta(); + props.onLayoutChange?.(); + void persistStructurePatch(mountSection, { + popoverWidthMode: draftPopoverWidthMode, + popoverWidth: draftPopoverWidth, + }); + }, + }); + const multiSessionToggle = SegmentedControl({ ariaLabel: t('settings.str.multiToggle'), modelValue: 'show', @@ -13128,6 +13664,9 @@ const SettingsStructureTab = defineComponent({ badgeText: t('settings.str.mountBadge', { count: draftTopbarSlots.length }), onReset: () => { draftWindowMountMode = 'dual'; + draftPopoverWidthMode = 'auto'; + draftPopoverWidth = DEFAULT_CLIENT_POPOVER_WIDTH; + popoverWidthInput.value = String(DEFAULT_CLIENT_POPOVER_WIDTH); draftEnableCliStatusLine = true; draftShowTopbarMulti = true; draftTopbarSlots = [...DEFAULT_CLIENT_TOPBAR_SLOTS]; @@ -13138,6 +13677,8 @@ const SettingsStructureTab = defineComponent({ { windowMountMode: 'dual', enableWindowOverlay: true, + popoverWidthMode: 'auto', + popoverWidth: DEFAULT_CLIENT_POPOVER_WIDTH, enableCliStatusLine: true, showTopbarMultiSession: true, topbarCapsuleSlots: [...DEFAULT_CLIENT_TOPBAR_SLOTS], @@ -13149,6 +13690,16 @@ const SettingsStructureTab = defineComponent({ mountPreviewCanvas, h('div', { className: 'settings-divider' }), h('div', { className: 'form-row' }, [mountModeLabelEl, windowMountSelect.el]), + h('div', { className: 'form-row' }, [ + h('div', { className: 'settings-inline-row' }, [ + popoverWidthLabelEl, + h('div', { className: 'settings-inline-controls' }, [ + popoverWidthModeToggle.el, + popoverWidthNumWrap, + ]), + ]), + popoverWidthHintEl, + ]), h('div', { className: 'settings-inline-row' }, [ multiToggleLabelEl, multiSessionToggle.el, @@ -13171,6 +13722,14 @@ const SettingsStructureTab = defineComponent({ modelValue: draftWindowMountMode, options: getMountModeOptions(), }); + popoverWidthModeToggle.setProps({ + modelValue: draftPopoverWidthMode, + options: getPopoverWidthModeOptions(), + }); + patchAttr(popoverWidthNumWrap, 'hidden', draftPopoverWidthMode !== 'custom'); + if (document?.activeElement !== popoverWidthInput) { + popoverWidthInput.value = String(draftPopoverWidth); + } multiSessionToggle.setProps({ modelValue: draftShowTopbarMulti ? 'show' : 'hide', options: getMultiToggleOptions(), @@ -13189,6 +13748,8 @@ const SettingsStructureTab = defineComponent({ ); const isDefault = draftWindowMountMode === 'dual' && + draftPopoverWidthMode === 'auto' && + draftPopoverWidth === DEFAULT_CLIENT_POPOVER_WIDTH && draftEnableCliStatusLine === true && draftShowTopbarMulti === true && isSameList(draftTopbarSlots, DEFAULT_CLIENT_TOPBAR_SLOTS); @@ -13454,6 +14015,14 @@ const SettingsStructureTab = defineComponent({ className: 'form-label', text: t('settings.str.stepSpeedColorToggle'), }); + const stepTokenComboLabelEl = h('span', { + className: 'form-label', + text: t('settings.str.stepTokenComboLabel'), + }); + const stepTokenComboHintEl = h('span', { + className: 'settings-hint-text', + text: t('settings.str.stepTokenComboHint'), + }); const stepColsLabelEl = h('span', { className: 'form-label', text: t('settings.str.stepsColsLabel'), @@ -13498,11 +14067,65 @@ const SettingsStructureTab = defineComponent({ }, }); + function commitStepTokenComboInput() { + const rawTrimmed = String(stepTokenComboInput.value || '').trim(); + const normFmt = normalizeClientStepTokenComboFormat(rawTrimmed); + rawStepTokenComboText = normFmt; + draftStepTokenComboFormat = normFmt; + stepTokenComboInput.value = normFmt; + if (rawTrimmed && !draftStepColumns.includes('tokens_custom')) { + const comboIdx = draftStepColumns.findIndex( + (k) => k === 'tokens_combo' || k === 'tokens_io_cache' + ); + if (comboIdx >= 0) { + const nextCols = [...draftStepColumns]; + nextCols[comboIdx] = 'tokens_custom'; + draftStepColumns = normalizeClientStepColumns(nextCols); + } else { + draftStepColumns = normalizeClientStepColumns([...draftStepColumns, 'tokens_custom']); + } + } + syncStepsSectionMeta(); + props.onLayoutChange?.(); + void persistStructurePatch(stepsSection, { + stepTokenComboFormat: draftStepTokenComboFormat, + stepTableColumns: draftStepColumns, + }); + } + + const stepTokenComboInput = h('input', { + type: 'text', + className: 'settings-text-input', + spellcheck: 'false', + value: rawStepTokenComboText, + placeholder: t('settings.str.stepTokenComboPlaceholder'), + 'aria-label': t('settings.str.stepTokenComboLabel'), + onInput: () => { + rawStepTokenComboText = stepTokenComboInput.value; + draftStepTokenComboFormat = normalizeClientStepTokenComboFormat(rawStepTokenComboText); + stepColumnsList.refs.setProps({ + items: draftStepColumns, + options: getStepColumnOptions(draftStepTokenComboFormat), + addBtnTextFn: (count, max) => t('settings.str.addStepCol', { count, max }), + }); + refreshStepsPreview(); + }, + onChange: () => { + commitStepTokenComboInput(); + }, + onKeydown: (e) => { + if (e?.key === 'Enter') { + e.preventDefault(); + stepTokenComboInput.blur(); + } + }, + }); + const stepColumnsList = EditableSlotList({ items: draftStepColumns, - options: getStepColumnOptions(), + options: getStepColumnOptions(draftStepTokenComboFormat), minItems: 2, - maxItems: 12, + maxItems: 15, addBtnTextFn: (count, max) => t('settings.str.addStepCol', { count, max }), onPreview: (previewItems) => { draftStepColumns = normalizeClientStepColumns(previewItems); @@ -13527,6 +14150,9 @@ const SettingsStructureTab = defineComponent({ draftShowStepTurnRail = false; draftShowStepAgentBadge = true; draftShowStepSpeedColor = false; + draftStepTokenComboFormat = DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT; + rawStepTokenComboText = DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT; + stepTokenComboInput.value = DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT; draftStepColumns = [...DEFAULT_CLIENT_STEP_COLUMNS]; syncStepsSectionMeta(); props.onLayoutChange?.(); @@ -13536,6 +14162,7 @@ const SettingsStructureTab = defineComponent({ showStepTurnRail: false, showStepAgentBadge: true, showStepSpeedColor: false, + stepTokenComboFormat: DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT, stepTableColumns: [...DEFAULT_CLIENT_STEP_COLUMNS], }, 'settings.resetDone' @@ -13557,6 +14184,11 @@ const SettingsStructureTab = defineComponent({ stepSpeedColorToggle.el, ]), h('div', { className: 'settings-divider' }), + h('div', { className: 'form-row' }, [ + stepTokenComboLabelEl, + stepTokenComboInput, + stepTokenComboHintEl, + ]), h('div', { className: 'form-row' }, [stepColsLabelEl, stepColumnsList.el]), ], }); @@ -13565,10 +14197,22 @@ const SettingsStructureTab = defineComponent({ if (!isStructureVisible() || activeThirdTab !== 'steps') return; const mockData = buildDraftTelemetry(); const mockSteps = mockData.activeConversation?.recentSteps || []; + const previewCols = + document?.activeElement === stepTokenComboInput && + String(rawStepTokenComboText || '').trim() && + !draftStepColumns.includes('tokens_custom') + ? normalizeClientStepColumns( + draftStepColumns.some((k) => k === 'tokens_combo' || k === 'tokens_io_cache') + ? draftStepColumns.map((k) => + k === 'tokens_combo' || k === 'tokens_io_cache' ? 'tokens_custom' : k + ) + : [...draftStepColumns, 'tokens_custom'] + ) + : draftStepColumns; stepTablePreview.refs.renderStepTable( mockSteps, 'mock-preview-conv', - draftStepColumns, + previewCols, getEffectiveLocale(), { effectiveScopes: ['all'], @@ -13577,6 +14221,7 @@ const SettingsStructureTab = defineComponent({ showStepTurnRail: draftShowStepTurnRail, showStepAgentBadge: draftShowStepAgentBadge, showStepSpeedColor: draftShowStepSpeedColor, + stepTokenComboFormat: draftStepTokenComboFormat, displaySettings: mockData.settings, } ); @@ -13595,9 +14240,12 @@ const SettingsStructureTab = defineComponent({ modelValue: draftShowStepSpeedColor ? 'on' : 'off', options: getEnableToggleOptions(), }); + if (document?.activeElement !== stepTokenComboInput) { + stepTokenComboInput.value = rawStepTokenComboText; + } stepColumnsList.refs.setProps({ items: draftStepColumns, - options: getStepColumnOptions(), + options: getStepColumnOptions(draftStepTokenComboFormat), addBtnTextFn: (count, max) => t('settings.str.addStepCol', { count, max }), }); stepsSection.refs.setBadge(t('settings.str.stepsBadge', { count: draftStepColumns.length })); @@ -13605,6 +14253,7 @@ const SettingsStructureTab = defineComponent({ draftShowStepTurnRail === false && draftShowStepAgentBadge === true && draftShowStepSpeedColor === false && + draftStepTokenComboFormat === DEFAULT_CLIENT_STEP_TOKEN_COMBO_FORMAT && isSameList(draftStepColumns, DEFAULT_CLIENT_STEP_COLUMNS); stepsSection.refs.setResetDisabled(isDefault); refreshStepsPreview(); @@ -13666,6 +14315,8 @@ const SettingsStructureTab = defineComponent({ settings.windowMountMode, settings.enableWindowOverlay ); + draftPopoverWidthMode = normalizeClientPopoverWidthMode(settings.popoverWidthMode); + draftPopoverWidth = normalizeClientPopoverWidth(settings.popoverWidth); draftEnableCliStatusLine = settings.enableCliStatusLine !== false; draftShowTopbarMulti = settings.showTopbarMultiSession !== false; draftTopbarSlots = normalizeClientTopbarSlots(settings.topbarCapsuleSlots); @@ -13675,6 +14326,10 @@ const SettingsStructureTab = defineComponent({ draftShowStepTurnRail = settings.showStepTurnRail === true; draftShowStepAgentBadge = settings.showStepAgentBadge !== false; draftShowStepSpeedColor = settings.showStepSpeedColor === true; + draftStepTokenComboFormat = normalizeClientStepTokenComboFormat( + settings.stepTokenComboFormat + ); + rawStepTokenComboText = draftStepTokenComboFormat; draftStepColumns = normalizeClientStepColumns(settings.stepTableColumns); syncMountSectionMeta(); @@ -13692,6 +14347,9 @@ const SettingsStructureTab = defineComponent({ mountSection.refs.setTitle(t('settings.str.mountSection')); mountSection.refs.setResetLabel(t('settings.resetBtn')); patchText(mountModeLabelEl, t('settings.str.mountMode')); + patchText(popoverWidthLabelEl, t('settings.str.popoverWidthLabel')); + patchText(popoverWidthHintEl, t('settings.str.popoverWidthHint')); + patchAttr(popoverWidthInput, 'aria-label', t('settings.str.popoverWidthLabel')); patchText(multiToggleLabelEl, t('settings.str.multiToggle')); patchText(cliCapsuleLabelEl, t('settings.str.cliCapsuleToggle')); patchText(capsuleSlotsLabelEl, t('settings.str.capsuleSlotsLabel')); @@ -13711,6 +14369,10 @@ const SettingsStructureTab = defineComponent({ patchText(stepTurnRailLabelEl, t('settings.str.stepTurnRailToggle')); patchText(stepAgentBadgeLabelEl, t('settings.str.stepAgentBadgeToggle')); patchText(stepSpeedColorLabelEl, t('settings.str.stepSpeedColorToggle')); + patchText(stepTokenComboLabelEl, t('settings.str.stepTokenComboLabel')); + patchText(stepTokenComboHintEl, t('settings.str.stepTokenComboHint')); + patchAttr(stepTokenComboInput, 'placeholder', t('settings.str.stepTokenComboPlaceholder')); + patchAttr(stepTokenComboInput, 'aria-label', t('settings.str.stepTokenComboLabel')); patchText(stepColsLabelEl, t('settings.str.stepsColsLabel')); syncMountSectionMeta(); @@ -13755,6 +14417,7 @@ const SettingsStructureTab = defineComponent({ overviewPreviewHost.destroy(); stepTablePreview.destroy(); windowMountSelect.destroy(); + popoverWidthModeToggle.destroy(); multiSessionToggle.destroy(); cliCapsuleToggle.destroy(); topbarSlotsList.destroy(); @@ -14045,6 +14708,7 @@ function getStepModelDisplayOptions() { return [ { value: 'short', label: t('settings.oth.modelShort') }, { value: 'upstream', label: t('settings.oth.modelUpstream') }, + { value: 'upstream_compact', label: t('settings.oth.modelUpstreamCompact') }, ]; } @@ -14058,7 +14722,7 @@ function getOrchestratorIdDisplayOptions() { const SettingsOtherTab = defineComponent({ name: 'SettingsOtherTab', setup({ onCleanup, getProps }) { - let draftShowUpstreamModel = false; + let draftModelDisplayMode = 'short'; let draftOrchestratorIdMode = 'compact'; let latestSettings = null; let contextWindowModels = []; @@ -14127,12 +14791,15 @@ const SettingsOtherTab = defineComponent({ icon: 'table', options: getStepModelDisplayOptions(), onUpdateModelValue: async (val) => { - const nextUpstream = val === 'upstream'; - draftShowUpstreamModel = nextUpstream; - stepModelSelect.setValue(nextUpstream ? 'upstream' : 'short'); + const nextMode = normalizeClientModelDisplayMode(val); + draftModelDisplayMode = nextMode; + stepModelSelect.setValue(nextMode); showModelFeedback(t('settings.saving'), 0); try { - await saveSettingsPatch({ showUpstreamModel: nextUpstream }); + await saveSettingsPatch({ + modelDisplayMode: nextMode, + showUpstreamModel: nextMode !== 'short', + }); showModelFeedback(t('settings.saved'), 1500); } catch { if (latestSettings) renderSettings(latestSettings); @@ -14432,10 +15099,13 @@ const SettingsOtherTab = defineComponent({ if (!settings) return; latestSettings = settings; contextWindowModels = models; - draftShowUpstreamModel = Boolean(settings.showUpstreamModel); + draftModelDisplayMode = normalizeClientModelDisplayMode( + settings.modelDisplayMode, + settings.showUpstreamModel + ); draftOrchestratorIdMode = normalizeClientOrchestratorIdMode(settings.orchestratorIdMode); stepModelSelect.setProps({ - modelValue: draftShowUpstreamModel ? 'upstream' : 'short', + modelValue: draftModelDisplayMode, options: getStepModelDisplayOptions(), }); orchestratorIdSelect.setProps({ @@ -14468,7 +15138,7 @@ const SettingsOtherTab = defineComponent({ } stepModelSelect.setProps({ - modelValue: draftShowUpstreamModel ? 'upstream' : 'short', + modelValue: draftModelDisplayMode, options: getStepModelDisplayOptions(), }); orchestratorIdSelect.setProps({ @@ -15459,8 +16129,9 @@ const ConsumptionView = defineComponent({ const locale = i18nStore.getState().locale; // 额度与会话状态也会发布遥测更新,只对用量页实际展示的数据重绘。 const signature = JSON.stringify([ - locale, slice.range, slice.customStartMs, slice.customEndMs, slice.showUpstreamModel, - slice.orchestratorIdMode, dateSpan, preset?.label || rangeInfo.label, acct, + locale, slice.range, slice.customStartMs, slice.customEndMs, slice.modelDisplayMode, + slice.showUpstreamModel, slice.orchestratorIdMode, dateSpan, + preset?.label || rangeInfo.label, acct, cons.trendSeries, slice.modelBreakdown, cons.sourcesBreakdown, monthlyYear, currentMonthIdx, cons.monthlySeries, ]); @@ -15574,6 +16245,10 @@ const ConsumptionView = defineComponent({ // 3. 同步热门模型列表(按最终展示名称去重归并后按总 Token 数降序排列并执行键值增量对账) const localeSig = i18nStore.getState().locale; const displaySettings = { + modelDisplayMode: normalizeModelDisplayMode( + slice.modelDisplayMode, + slice.showUpstreamModel + ), showUpstreamModel: Boolean(slice.showUpstreamModel), orchestratorIdMode: slice.orchestratorIdMode, }; @@ -15660,6 +16335,10 @@ const ConsumptionView = defineComponent({ customEndMs: st.customEndMs, consumption: st.telemetry?.consumption, modelBreakdown: st.telemetry?.modelBreakdown, + modelDisplayMode: normalizeModelDisplayMode( + st.telemetry?.settings?.modelDisplayMode, + st.telemetry?.settings?.showUpstreamModel + ), showUpstreamModel: Boolean(st.telemetry?.settings?.showUpstreamModel), orchestratorIdMode: normalizeOrchestratorIdDisplayMode( st.telemetry?.settings?.orchestratorIdMode @@ -16316,6 +16995,10 @@ const StepsDetailView = defineComponent({ } const displaySettings = { + modelDisplayMode: normalizeModelDisplayMode( + slice.modelDisplayMode, + slice.showUpstreamModel + ), showUpstreamModel: Boolean(slice.showUpstreamModel), orchestratorIdMode: slice.orchestratorIdMode, }; @@ -16441,6 +17124,7 @@ const StepsDetailView = defineComponent({ showStepTurnRail: slice.showStepTurnRail === true, showStepAgentBadge: slice.showStepAgentBadge !== false, showStepSpeedColor: slice.showStepSpeedColor === true, + stepTokenComboFormat: slice.stepTokenComboFormat, displaySettings, }); stepsScroll.refresh(); @@ -16757,6 +17441,10 @@ const StepsDetailView = defineComponent({ cascadeId: st.telemetry?.activeConversation?.cascadeId, recentSteps: st.telemetry?.activeConversation?.recentSteps, sessionModelBreakdown: st.telemetry?.activeConversation?.modelBreakdown, + modelDisplayMode: normalizeModelDisplayMode( + st.telemetry?.settings?.modelDisplayMode, + st.telemetry?.settings?.showUpstreamModel + ), showUpstreamModel: Boolean(st.telemetry?.settings?.showUpstreamModel), orchestratorIdMode: normalizeOrchestratorIdDisplayMode( st.telemetry?.settings?.orchestratorIdMode @@ -16764,6 +17452,9 @@ const StepsDetailView = defineComponent({ showStepTurnRail: st.telemetry?.settings?.showStepTurnRail === true, showStepAgentBadge: st.telemetry?.settings?.showStepAgentBadge !== false, showStepSpeedColor: st.telemetry?.settings?.showStepSpeedColor === true, + stepTokenComboFormat: normalizeClientStepTokenComboFormat( + st.telemetry?.settings?.stepTokenComboFormat + ), stepTableColumns: st.telemetry?.settings?.stepTableColumns, }), (slice) => { @@ -17183,6 +17874,8 @@ const App = defineComponent({ const CAPSULE_HOST_ID = 'ag-usage-monitor-capsule'; const PORTAL_HOST_ID = 'ag-usage-monitor-portal'; const FLOATING_CARD_WIDTH = 522; +const POPOVER_BASE_WIDTH = 540; +const POPOVER_MAX_AUTO_WIDTH = 960; const MULTI_PANEL_WIDTH = 464; /** @@ -19103,11 +19796,72 @@ function mountWindowOverlay() { : bRect.right; if (popoverOpen) { + const maxPopCardW = Math.max(280, vw - minLeft - 12); + const curSettings = telemetryStore.getState().telemetry?.settings; + const popWidthMode = normalizeClientPopoverWidthMode(curSettings?.popoverWidthMode); + let desiredPopW = Math.min(POPOVER_BASE_WIDTH, maxPopCardW); + if (popWidthMode === 'custom') { + desiredPopW = Math.min( + normalizeClientPopoverWidth(curSettings?.popoverWidth), + maxPopCardW + ); + } else if (typeof popoverEl.querySelectorAll === 'function') { + const panes = popoverEl.querySelectorAll('.table-viewport.is-step-table'); + for (const pane of panes) { + let anc = pane; + let inHiddenTree = false; + while (anc && anc !== popoverEl) { + if (anc.hasAttribute?.('hidden')) { + inHiddenTree = true; + break; + } + anc = anc.parentElement || anc.parentNode; + } + if (inHiddenTree) continue; + const tableEl = pane.querySelector?.('.steps-data-table') || pane.firstElementChild; + if (!tableEl) continue; + const paneClientW = Number(pane.clientWidth || 0); + const curPopW = Number( + popoverEl.offsetWidth || + popoverEl.getBoundingClientRect?.().width || + POPOVER_BASE_WIDTH + ); + let naturalTableW = 0; + if (tableEl.style) { + const prevW = tableEl.style.width || ''; + tableEl.style.width = 'max-content'; + naturalTableW = Math.ceil( + Number( + tableEl.getBoundingClientRect?.().width || + tableEl.scrollWidth || + tableEl.offsetWidth || + 0 + ) + ); + tableEl.style.width = prevW; + } else { + naturalTableW = Math.ceil(Number(tableEl.scrollWidth || tableEl.offsetWidth || 0)); + } + if (naturalTableW > 0) { + const chromeW = + paneClientW > 0 && curPopW >= paneClientW + ? Math.max(24, curPopW - paneClientW) + : 34; + desiredPopW = Math.max( + desiredPopW, + Math.min(POPOVER_MAX_AUTO_WIDTH, maxPopCardW, naturalTableW + chromeW + 4) + ); + } + } + } + popoverEl.style.setProperty?.('--wm-popover-w', `${desiredPopW}px`); + popoverEl.style.width = `${desiredPopW}px`; + const rawW = Number(popoverEl.offsetWidth) || Number(popoverEl.getBoundingClientRect?.().width) || - defaultCardW; - const panelW = Math.min(rawW, Math.max(280, vw - minLeft - 12)); + desiredPopW; + const panelW = Math.min(rawW, maxPopCardW); const spaceBelowPop = vh - bRect.bottom - 12; const spaceAbovePop = bRect.top - 16; const openPopAbove = @@ -19938,6 +20692,9 @@ function mountWindowOverlay() { } lastAppliedMountMode = nextMountMode; renderCapsuleTelemetry(); + if (popoverOpen) { + positionFloatingElements(); + } } ); @@ -19945,8 +20702,25 @@ function mountWindowOverlay() { (st) => ({ selectedAgentScope: st.selectedAgentScope, ctxInspectorAgentId: st.ctxInspectorAgentId, + activeView: st.activeView, + stepsSubTab: st.stepsSubTab, + settingsSubTab: st.settingsSubTab, + settingsStructureTab: st.settingsStructureTab, }), - () => renderCapsuleTelemetry() + () => { + renderCapsuleTelemetry(); + if (popoverOpen) { + positionFloatingElements(); + if (typeof requestAnimationFrame === 'function') { + requestAnimationFrame(() => { + if (!disposed && popoverOpen) { + positionFloatingElements(); + refreshActiveViewScrollbars(); + } + }); + } + } + } ); const unsubI18n = i18nStore.subscribe( @@ -20134,10 +20908,10 @@ function mountWindowOverlay() { const token = String(nextBridge.token || ''); if (!Number.isInteger(port) || port <= 0 || !token) return false; const cur = window.__AGUsageMonitorBridge || {}; - const portChanged = Number(cur.port || 0) !== port; + const portChanged = Number(cur.port || 0) !== port || String(cur.token || '') !== token; cur.port = port; cur.token = token; - if (portChanged && cur.transportMode !== 'cdp') { + if (portChanged) { cur.transportMode = 'http'; } window.__AGUsageMonitorBridge = cur; diff --git a/sidecars/panel/backend/api-actions.mjs b/sidecars/panel/backend/api-actions.mjs index 81d77f6..7866ec9 100644 --- a/sidecars/panel/backend/api-actions.mjs +++ b/sidecars/panel/backend/api-actions.mjs @@ -22,6 +22,7 @@ import { loadSettings, lsRuntimeEndpoint, normalizeFullSettings, + normalizeModelDisplayMode, normalizePluginUpdateMode, normalizeWindowMountMode, saveJson, @@ -38,6 +39,7 @@ import { callLsRpc, getContextModelCatalog } from './ls-rpc.mjs'; import { performPluginUpdate, scheduleSidecarRestart, + setSidecarRestartHandler, syncCloudCatalog, } from './plugin-updater.mjs'; import { getCloudStatus } from './pricing-catalog.mjs'; @@ -124,19 +126,19 @@ function projectDisplayStep(st) { stepIndex, createdAtMs, isCompactionStep, modelKey, modelShort, modelLabel, modelUpstream, responseModel, modelSource, tokPerSec, ttftSec, totalDurationSec, streamDurationSec, selectedSpeedReason, contextTokens, inputTokens, cacheReadTokens, - outputTokens, thinkingTokens, responseTokens, cacheHitPct, costUsd, agentId, - agentCascadeId, agentLabel, agentBadge, agentRole, agentIndex, isSubagent, - turnIndex, isTurnStart, isLatestTurn, turnPrompt, rootTurnIndex, isRootTurnStart, - isRootLatestTurn, rootTurnPrompt, + outputTokens, thinkingTokens, responseTokens, cacheHitPct, costUsd, inCostUsd, + cacheCostUsd, outCostUsd, agentId, agentCascadeId, agentLabel, agentBadge, + agentRole, agentIndex, isSubagent, turnIndex, isTurnStart, isLatestTurn, + turnPrompt, rootTurnIndex, isRootTurnStart, isRootLatestTurn, rootTurnPrompt, } = st; return { stepIndex, createdAtMs, isCompactionStep, modelKey, modelShort, modelLabel, modelUpstream, responseModel, modelSource, tokPerSec, ttftSec, totalDurationSec, streamDurationSec, selectedSpeedReason, contextTokens, inputTokens, cacheReadTokens, - outputTokens, thinkingTokens, responseTokens, cacheHitPct, costUsd, agentId, - agentCascadeId, agentLabel, agentBadge, agentRole, agentIndex, isSubagent, - turnIndex, isTurnStart, isLatestTurn, turnPrompt, rootTurnIndex, isRootTurnStart, - isRootLatestTurn, rootTurnPrompt, + outputTokens, thinkingTokens, responseTokens, cacheHitPct, costUsd, inCostUsd, + cacheCostUsd, outCostUsd, agentId, agentCascadeId, agentLabel, agentBadge, + agentRole, agentIndex, isSubagent, turnIndex, isTurnStart, isLatestTurn, + turnPrompt, rootTurnIndex, isRootTurnStart, isRootLatestTurn, rootTurnPrompt, }; } @@ -288,6 +290,14 @@ export function executeSettingsAction(data) { : 'dual' : 'sidebar' : normalizeWindowMountMode(current.windowMountMode, current.enableWindowOverlay); + const nextModelDisplayMode = + payload.modelDisplayMode !== undefined + ? normalizeModelDisplayMode(payload.modelDisplayMode) + : typeof payload.showUpstreamModel === 'boolean' + ? payload.showUpstreamModel + ? 'upstream' + : 'short' + : normalizeModelDisplayMode(current.modelDisplayMode, current.showUpstreamModel); const updated = normalizeFullSettings({ ...current, ...payload, @@ -300,6 +310,7 @@ export function executeSettingsAction(data) { payload.weeklyCostBudget, current.weeklyCostBudget ), + modelDisplayMode: nextModelDisplayMode, pluginUpdateMode: nextPluginUpdateMode, windowMountMode: nextWindowMountMode, }); @@ -528,6 +539,9 @@ export function bootWindowBridgeRuntime({ allowDynamicLs = false, onRestart = nu const triggerRestart = typeof onRestart === 'function' ? onRestart : () => scheduleSidecarRestart(150); + if (typeof onRestart === 'function') { + setSidecarRestartHandler(onRestart); + } if (lsRuntimeEndpoint.baseUrl) { syncCloudCatalog({ force: false }).catch(() => {}); diff --git a/sidecars/panel/backend/config.mjs b/sidecars/panel/backend/config.mjs index b913c9d..83dcc04 100644 --- a/sidecars/panel/backend/config.mjs +++ b/sidecars/panel/backend/config.mjs @@ -18,16 +18,22 @@ import { DEFAULT_HOVER_MODULES, DEFAULT_OVERVIEW_METRICS, DEFAULT_OVERVIEW_MODULES, + DEFAULT_POPOVER_WIDTH, DEFAULT_SETTINGS_SCHEMA, DEFAULT_STEP_TABLE_COLUMNS, + DEFAULT_STEP_TOKEN_COMBO_FORMAT, DEFAULT_TOPBAR_SLOTS, FONT_SCALE_KEYS, + MAX_POPOVER_WIDTH, MAX_TOPBAR_SLOTS, + MIN_POPOVER_WIDTH, MIN_TOPBAR_SLOTS, + MODEL_DISPLAY_MODES, ORCHESTRATOR_ID_MODES, OVERVIEW_METRIC_KEYS, OVERVIEW_MODULE_KEYS, PLUGIN_UPDATE_MODES, + POPOVER_WIDTH_MODES, STEP_TABLE_COLUMN_KEYS, SUPPORTED_LANGUAGES, THEME_ACCENT_KEYS, @@ -38,32 +44,43 @@ import { normalizeFullSettings, normalizeHoverModules, normalizeLanguageSetting, + normalizeModelDisplayMode, normalizeOrchestratorIdMode, normalizeOverviewMetrics, normalizeOverviewModules, normalizePluginUpdateMode, + normalizePopoverWidth, + normalizePopoverWidthMode, normalizeStandalonePort, normalizeStepTableColumns, + normalizeStepTokenComboFormat, normalizeThemeAccent, normalizeTopbarCapsuleSlots, normalizeWeeklyLimitMode, normalizeWindowMountMode, + parseStepTokenComboSegments, } from './settings-schema.mjs'; export { DEFAULT_HOVER_MODULES, DEFAULT_OVERVIEW_METRICS, DEFAULT_OVERVIEW_MODULES, + DEFAULT_POPOVER_WIDTH, DEFAULT_SETTINGS_SCHEMA, DEFAULT_STEP_TABLE_COLUMNS, + DEFAULT_STEP_TOKEN_COMBO_FORMAT, DEFAULT_TOPBAR_SLOTS, FONT_SCALE_KEYS, + MAX_POPOVER_WIDTH, MAX_TOPBAR_SLOTS, + MIN_POPOVER_WIDTH, MIN_TOPBAR_SLOTS, + MODEL_DISPLAY_MODES, ORCHESTRATOR_ID_MODES, OVERVIEW_METRIC_KEYS, OVERVIEW_MODULE_KEYS, PLUGIN_UPDATE_MODES, + POPOVER_WIDTH_MODES, STEP_TABLE_COLUMN_KEYS, SUPPORTED_LANGUAGES, THEME_ACCENT_KEYS, @@ -74,16 +91,21 @@ export { normalizeFullSettings, normalizeHoverModules, normalizeLanguageSetting, + normalizeModelDisplayMode, normalizeOrchestratorIdMode, normalizeOverviewMetrics, normalizeOverviewModules, normalizePluginUpdateMode, + normalizePopoverWidth, + normalizePopoverWidthMode, normalizeStandalonePort, normalizeStepTableColumns, + normalizeStepTokenComboFormat, normalizeThemeAccent, normalizeTopbarCapsuleSlots, normalizeWeeklyLimitMode, normalizeWindowMountMode, + parseStepTokenComboSegments, } from './settings-schema.mjs'; /** @@ -292,7 +314,7 @@ export function loadJson(filePath, fallback) { */ export function saveJson(filePath, data) { try { - mkdirSync(DATA_DIR, { recursive: true }); + mkdirSync(dirname(filePath), { recursive: true }); if (filePath === CACHE_FILE) { writeTrajectoryCacheFile(filePath, data); } else { diff --git a/sidecars/panel/backend/plugin-updater.mjs b/sidecars/panel/backend/plugin-updater.mjs index 2533705..6b27f03 100644 --- a/sidecars/panel/backend/plugin-updater.mjs +++ b/sidecars/panel/backend/plugin-updater.mjs @@ -72,15 +72,39 @@ function runExecFile(cmd, args, opts = {}) { }); } +let customSidecarRestartHandler = null; + /** - * 更新完成后调度 Sidecar 进程平滑退出,让宿主进程监督器自动重启新代码。 + * 注册自定义运行时热重载回调(例如 `main.mjs` 的 `Worker` 热替换或 `bridge-daemon.mjs` 的平滑退出), + * 避免直接调用 `process.exit(0)` 导致宿主 `SidecarApp` 固定端口失联。 + * + * @param {(() => void)|null} handler + */ +export function setSidecarRestartHandler(handler) { + customSidecarRestartHandler = typeof handler === 'function' ? handler : null; +} + +/** + * 更新完成后调度 Sidecar 运行时热重载(若未注册自定义处理器,则回退平滑退出供父监督器拉起)。 * 在单元测试环境(`!LS_ADDR || LS_ADDR === 'test.invalid'`)下自动跳过。 * * @param {number} [delayMs=350] 延迟毫秒数,确保当前 HTTP 响应先发回前端 */ export function scheduleSidecarRestart(delayMs = 350) { const activeAddr = lsRuntimeEndpoint.addr || LS_ADDR; - if (!activeAddr || activeAddr === 'test.invalid' || typeof process?.exit !== 'function') return; + if (!activeAddr || activeAddr === 'test.invalid') return; + if (typeof customSidecarRestartHandler === 'function') { + const fn = customSidecarRestartHandler; + setTimeout(() => { + try { + fn(); + } catch { + // 忽略自定义热重载回调异常 + } + }, delayMs); + return; + } + if (typeof process?.exit !== 'function') return; setTimeout(() => { try { process.exit(0); diff --git a/sidecars/panel/backend/settings-schema.mjs b/sidecars/panel/backend/settings-schema.mjs index febac65..e3a610b 100644 --- a/sidecars/panel/backend/settings-schema.mjs +++ b/sidecars/panel/backend/settings-schema.mjs @@ -84,6 +84,8 @@ export const STEP_TABLE_COLUMN_KEYS = Object.freeze([ 'ttft', 'duration', 'tokens_combo', + 'tokens_io_cache', + 'tokens_custom', 'input_tokens', 'cache_tokens', 'output_tokens', @@ -101,12 +103,57 @@ export const DEFAULT_STEP_TABLE_COLUMNS = Object.freeze([ 'cost', ]); +export const DEFAULT_STEP_TOKEN_COMBO_FORMAT = 'context:in:cache'; + +export const STEP_TOKEN_SEGMENT_ALIASES = Object.freeze({ + context: 'context', + ctx: 'context', + '上下文': 'context', + in: 'in', + new: 'in', + input: 'in', + '输入': 'in', + '新输入': 'in', + out: 'out', + output: 'out', + '输出': 'out', + '总输出': 'out', + cache: 'cache', + cached: 'cache', + cache_read: 'cache', + cacheread: 'cache', + '缓存': 'cache', + '缓存读': 'cache', + think: 'think', + thinking: 'think', + '思考': 'think', + reply: 'reply', + body: 'reply', + resp: 'reply', + response: 'reply', + text: 'reply', + '正文': 'reply', + '回复': 'reply', + total: 'total', + all: 'total', + sum: 'total', + '总计': 'total', + '总token': 'total', +}); + export const PLUGIN_UPDATE_MODES = Object.freeze(['off', 'manual', 'auto']); +export const MODEL_DISPLAY_MODES = Object.freeze(['short', 'upstream', 'upstream_compact']); + export const ORCHESTRATOR_ID_MODES = Object.freeze(['compact', 'full']); export const WINDOW_MOUNT_MODES = Object.freeze(['dual', 'composer', 'sidebar']); +export const POPOVER_WIDTH_MODES = Object.freeze(['auto', 'custom']); +export const MIN_POPOVER_WIDTH = 440; +export const MAX_POPOVER_WIDTH = 1080; +export const DEFAULT_POPOVER_WIDTH = 580; + function isSchemaPlainObject(v) { return Boolean(v) && typeof v === 'object' && !Array.isArray(v); } @@ -171,6 +218,25 @@ export function normalizeWindowMountMode(mode, legacyEnableOverlay) { return 'dual'; } +/** + * 归一化模型名称展示模式(`'short'` 精简代号 | `'upstream_compact'` 去前缀原始 ID | `'upstream'` 完整原始 ID), + * 兼容历史 `showUpstreamModel` 布尔开关,默认 `'short'`。 + * + * @param {unknown} mode + * @param {unknown} [legacyShowUpstream] + * @returns {'short'|'upstream_compact'|'upstream'} + */ +export function normalizeModelDisplayMode(mode, legacyShowUpstream) { + const m = String(mode ?? '').trim().toLowerCase(); + if (m === 'upstream' || m === 'raw' || m === 'full') return 'upstream'; + if (m === 'upstream_compact' || m === 'compact_upstream' || m === 'trimmed') { + return 'upstream_compact'; + } + if (m === 'short') return 'short'; + if (legacyShowUpstream === true) return 'upstream'; + return 'short'; +} + /** * 归一化 Orchestrator 系列模型 ID 显示风格(`'compact'` 精简代号 vs `'full'` 完整模型 ID,默认 `'compact'`)。 * @@ -182,6 +248,65 @@ export function normalizeOrchestratorIdMode(mode) { return m === 'full' ? 'full' : 'compact'; } +/** + * 归一化悬浮主面板宽度模式(`'auto'` 自适应内容宽度 | `'custom'` 指定像素宽度,默认 `'auto'`)。 + * + * @param {unknown} mode + * @returns {'auto'|'custom'} + */ +export function normalizePopoverWidthMode(mode) { + const m = String(mode ?? '').trim().toLowerCase(); + return m === 'custom' || m === 'fixed' ? 'custom' : 'auto'; +} + +/** + * 归一化悬浮主面板自定义宽度像素值(范围 `440..1080`,默认 `580`)。 + * + * @param {unknown} width + * @returns {number} + */ +export function normalizePopoverWidth(width) { + const num = Number(width); + if (!Number.isFinite(num) || num <= 0) return DEFAULT_POPOVER_WIDTH; + return Math.max(MIN_POPOVER_WIDTH, Math.min(MAX_POPOVER_WIDTH, Math.round(num))); +} + +/** + * 解析步骤表自定义 Token 组合表达式,支持 `:`、`/`、`,`、`|` 分隔与中英文别名(1~4 项不重复)。 + * + * @param {unknown} raw + * @returns {Array<'context'|'in'|'out'|'cache'|'think'|'reply'|'total'>} + */ +export function parseStepTokenComboSegments(raw) { + const str = String(raw ?? '').trim(); + const out = []; + const seen = new Set(); + if (str) { + for (const part of str.split(/[:/,|]+/)) { + const key = part.trim().toLowerCase(); + if (!key) continue; + const mapped = Object.prototype.hasOwnProperty.call(STEP_TOKEN_SEGMENT_ALIASES, key) + ? STEP_TOKEN_SEGMENT_ALIASES[key] + : null; + if (!mapped || seen.has(mapped)) continue; + seen.add(mapped); + out.push(/** @type {any} */ (mapped)); + if (out.length >= 4) break; + } + } + return out.length > 0 ? out : ['context', 'in', 'cache']; +} + +/** + * 归一化步骤表自定义 Token 组合格式字符串(如 `'context:new:cache'` -> `'context:in:cache'`)。 + * + * @param {unknown} raw + * @returns {string} + */ +export function normalizeStepTokenComboFormat(raw) { + return parseStepTokenComboSegments(raw).join(':'); +} + /** * 归一化插件更新策略枚举(兼容历史 `autoUpdatePlugin` 布尔开关,默认 `'manual'`)。 * @@ -362,12 +487,15 @@ export const DEFAULT_SETTINGS_SCHEMA = Object.freeze({ weeklyTokenBudget: 150000000, weeklyCostBudget: 100, weeklyLimitMode: 'official', + modelDisplayMode: 'short', showUpstreamModel: false, orchestratorIdMode: 'compact', pluginUpdateMode: 'manual', autoUpdatePlugin: false, windowMountMode: 'dual', enableWindowOverlay: true, + popoverWidthMode: 'auto', + popoverWidth: DEFAULT_POPOVER_WIDTH, enableCliStatusLine: true, standalonePort: 0, showTopbarMultiSession: true, @@ -378,6 +506,7 @@ export const DEFAULT_SETTINGS_SCHEMA = Object.freeze({ showStepTurnRail: false, showStepAgentBadge: true, showStepSpeedColor: false, + stepTokenComboFormat: DEFAULT_STEP_TOKEN_COMBO_FORMAT, stepTableColumns: DEFAULT_STEP_TABLE_COLUMNS, modelContextWindows: Object.freeze({}), }); @@ -393,12 +522,15 @@ export function createDefaultSettingsObject() { weeklyTokenBudget: DEFAULT_SETTINGS_SCHEMA.weeklyTokenBudget, weeklyCostBudget: DEFAULT_SETTINGS_SCHEMA.weeklyCostBudget, weeklyLimitMode: DEFAULT_SETTINGS_SCHEMA.weeklyLimitMode, + modelDisplayMode: DEFAULT_SETTINGS_SCHEMA.modelDisplayMode, showUpstreamModel: DEFAULT_SETTINGS_SCHEMA.showUpstreamModel, orchestratorIdMode: DEFAULT_SETTINGS_SCHEMA.orchestratorIdMode, pluginUpdateMode: DEFAULT_SETTINGS_SCHEMA.pluginUpdateMode, autoUpdatePlugin: DEFAULT_SETTINGS_SCHEMA.autoUpdatePlugin, windowMountMode: DEFAULT_SETTINGS_SCHEMA.windowMountMode, enableWindowOverlay: DEFAULT_SETTINGS_SCHEMA.enableWindowOverlay, + popoverWidthMode: DEFAULT_SETTINGS_SCHEMA.popoverWidthMode, + popoverWidth: DEFAULT_SETTINGS_SCHEMA.popoverWidth, enableCliStatusLine: DEFAULT_SETTINGS_SCHEMA.enableCliStatusLine, standalonePort: DEFAULT_SETTINGS_SCHEMA.standalonePort, showTopbarMultiSession: DEFAULT_SETTINGS_SCHEMA.showTopbarMultiSession, @@ -409,6 +541,7 @@ export function createDefaultSettingsObject() { showStepTurnRail: DEFAULT_SETTINGS_SCHEMA.showStepTurnRail, showStepAgentBadge: DEFAULT_SETTINGS_SCHEMA.showStepAgentBadge, showStepSpeedColor: DEFAULT_SETTINGS_SCHEMA.showStepSpeedColor, + stepTokenComboFormat: DEFAULT_SETTINGS_SCHEMA.stepTokenComboFormat, stepTableColumns: [...DEFAULT_STEP_TABLE_COLUMNS], modelContextWindows: {}, }; @@ -429,6 +562,10 @@ export function normalizeFullSettings(rawInput) { safeRaw.windowMountMode, safeRaw.enableWindowOverlay ); + const modelDisplayMode = normalizeModelDisplayMode( + safeRaw.modelDisplayMode, + safeRaw.showUpstreamModel + ); return { language: normalizeLanguageSetting(safeRaw.language), themeAccent: normalizeThemeAccent(safeRaw.themeAccent), @@ -442,12 +579,15 @@ export function normalizeFullSettings(rawInput) { DEFAULT_SETTINGS_SCHEMA.weeklyCostBudget ), weeklyLimitMode: normalizeWeeklyLimitMode(safeRaw.weeklyLimitMode), - showUpstreamModel: safeRaw.showUpstreamModel === true, + modelDisplayMode, + showUpstreamModel: modelDisplayMode !== 'short', orchestratorIdMode: normalizeOrchestratorIdMode(safeRaw.orchestratorIdMode), pluginUpdateMode, autoUpdatePlugin: pluginUpdateMode === 'auto', windowMountMode, enableWindowOverlay: windowMountMode !== 'sidebar', + popoverWidthMode: normalizePopoverWidthMode(safeRaw.popoverWidthMode), + popoverWidth: normalizePopoverWidth(safeRaw.popoverWidth), enableCliStatusLine: safeRaw.enableCliStatusLine !== false, standalonePort: normalizeStandalonePort(safeRaw.standalonePort), showTopbarMultiSession: safeRaw.showTopbarMultiSession !== false, @@ -458,6 +598,7 @@ export function normalizeFullSettings(rawInput) { showStepTurnRail: safeRaw.showStepTurnRail === true, showStepAgentBadge: safeRaw.showStepAgentBadge !== false, showStepSpeedColor: safeRaw.showStepSpeedColor === true, + stepTokenComboFormat: normalizeStepTokenComboFormat(safeRaw.stepTokenComboFormat), stepTableColumns: normalizeStepTableColumns(safeRaw.stepTableColumns), modelContextWindows: normalizeModelContextWindows(safeRaw.modelContextWindows), }; diff --git a/sidecars/panel/backend/sidecar-worker.mjs b/sidecars/panel/backend/sidecar-worker.mjs new file mode 100644 index 0000000..80c5b6d --- /dev/null +++ b/sidecars/panel/backend/sidecar-worker.mjs @@ -0,0 +1,103 @@ +/** + * @fileoverview Hot-reloadable backend Worker runtime (`sidecar-worker.mjs`) for `main.mjs`. + * + * Isolates all ESM backend modules (`api-actions.mjs`, `telemetry-builder.mjs`, `window-bridge.mjs`, + * `pricing-catalog.mjs`, etc.) inside a dedicated V8 Worker isolate so plugin updates (in-app Git/ZIP + * or external file overwrite while Antigravity is open) can tear down and respawn a fresh Worker + * without ever exiting the parent `SidecarApp` process or losing its host-assigned HTTP port. + */ + +import { parentPort } from 'node:worker_threads'; +import { + bootBundleVersion, + bootWindowBridgeRuntime, + dispatchBridgeApiRequest, +} from './api-actions.mjs'; +import { computeRuntimeCodeVersion } from './config.mjs'; + +let shuttingDown = false; +let reloadRequested = false; + +function requestWorkerReload() { + if (shuttingDown || reloadRequested) return; + reloadRequested = true; + try { + parentPort?.postMessage({ type: 'reload_request' }); + } catch { + // Ignore postMessage errors if parent is already closing + } +} + +const bridgeController = bootWindowBridgeRuntime({ + allowDynamicLs: true, + onRestart: requestWorkerReload, +}); + +const driftWatchTimer = setInterval(() => { + if (shuttingDown || reloadRequested) return; + try { + const diskVersion = String(computeRuntimeCodeVersion() || ''); + if (bootBundleVersion && diskVersion && diskVersion !== String(bootBundleVersion)) { + requestWorkerReload(); + } + } catch { + // Ignore transient file stat errors while files are being overwritten + } +}, 3000); +driftWatchTimer.unref?.(); + +async function gracefulStop() { + if (shuttingDown) return; + shuttingDown = true; + clearInterval(driftWatchTimer); + try { + await bridgeController?.stop?.({ disposeWindows: false }); + } catch { + // Ignore shutdown errors + } +} + +parentPort?.on('message', async (msg) => { + if (!msg || typeof msg !== 'object') return; + if (msg.type === 'shutdown') { + await gracefulStop(); + try { + parentPort?.postMessage({ type: 'shutdown_ack', id: msg.id }); + } catch { + // Ignore closed channel + } + return; + } + + if (msg.type === 'api') { + const reqId = msg.id; + try { + const out = await dispatchBridgeApiRequest( + String(msg.pathname || ''), + String(msg.method || 'GET'), + msg.data, + { onRestart: requestWorkerReload } + ); + parentPort?.postMessage({ + type: 'api_result', + id: reqId, + ok: true, + response: { + status: out?.status || 200, + contentType: out?.contentType || 'application/json', + headers: out?.headers || { 'Cache-Control': 'no-store' }, + body: typeof out?.body === 'string' ? out.body : JSON.stringify(out?.body ?? {}), + }, + }); + } catch (err) { + parentPort?.postMessage({ + type: 'api_result', + id: reqId, + ok: false, + error: err?.message || 'Worker API execution failed', + }); + } + } +}); + +parentPort?.postMessage({ type: 'ready', bundleVersion: bootBundleVersion }); diff --git a/sidecars/panel/backend/window-bridge.mjs b/sidecars/panel/backend/window-bridge.mjs index 446e32d..fcfc254 100644 --- a/sidecars/panel/backend/window-bridge.mjs +++ b/sidecars/panel/backend/window-bridge.mjs @@ -29,6 +29,7 @@ const ALLOWED_BRIDGE_PATHS = new Set([ '/api/telemetry', '/api/settings', '/api/settings/reset', + '/api/cache/rebuild', '/api/accounts', '/api/accounts/update', '/api/context-window', @@ -789,8 +790,13 @@ export function createWindowBridgeController(options) { } serverStartPromise = (async () => { const { http, crypto } = await loadNodeDeps(); + const persistedLease = leaseFilePath ? loadJson(leaseFilePath, null) : null; + const persistedToken = String(persistedLease?.bridgeToken || '').trim(); + const persistedPort = Number(persistedLease?.bridgePort || 0); if (!bridgeToken) { - bridgeToken = crypto.randomBytes(32).toString('hex'); + bridgeToken = /^[0-9a-f]{64}$/i.test(persistedToken) + ? persistedToken + : crypto.randomBytes(32).toString('hex'); } const srv = http.createServer((req, res) => { @@ -915,17 +921,47 @@ export function createWindowBridgeController(options) { }); }); - await new Promise((resolve, reject) => { - srv.once('error', reject); - srv.listen(0, '127.0.0.1', () => { - srv.removeListener('error', reject); - resolve(); + const listenOnLoopback = (targetPort) => + new Promise((resolve, reject) => { + const onErr = (err) => { + srv.removeListener('listening', onListening); + reject(err); + }; + const onListening = () => { + srv.removeListener('error', onErr); + resolve(); + }; + srv.once('error', onErr); + srv.once('listening', onListening); + srv.listen(targetPort, '127.0.0.1'); }); - }); + + const preferredPort = + Number.isInteger(persistedPort) && persistedPort >= 1024 && persistedPort <= 65535 + ? persistedPort + : 0; + if (preferredPort > 0) { + try { + await listenOnLoopback(preferredPort); + } catch { + await listenOnLoopback(0); + } + } else { + await listenOnLoopback(0); + } srv.unref?.(); const addr = srv.address(); bridgePort = typeof addr === 'object' && addr ? Number(addr.port || 0) : 0; server = srv; + if (leaseFilePath && bridgePort > 0) { + saveJson(leaseFilePath, { + pid: process.pid, + bundleVersion: String(getBundleVersionFn() || ''), + bridgePort, + bridgeToken, + updatedAtMs: Date.now(), + }); + } return { port: bridgePort, token: bridgeToken }; })().finally(() => { serverStartPromise = null; @@ -1133,6 +1169,8 @@ export function createWindowBridgeController(options) { saveJson(leaseFilePath, { pid: process.pid, bundleVersion, + bridgePort: bridgePort > 0 ? bridgePort : Number(lease?.bridgePort || 0) || undefined, + bridgeToken: bridgeToken || String(lease?.bridgeToken || '') || undefined, updatedAtMs: Date.now(), }); } @@ -1462,6 +1500,17 @@ export function createWindowBridgeController(options) { if (initialTimer) clearTimeout(initialTimer); if (pollTimer) clearTimeout(pollTimer); + if (leaseFilePath) { + const curSettings = getSettingsFn() || {}; + const mountMode = normalizeWindowMountMode( + curSettings.windowMountMode, + curSettings.enableWindowOverlay + ); + if (curSettings.enableWindowOverlay !== false && mountMode !== 'sidebar') { + ensureBridgeServer().catch(() => {}); + } + } + initialTimer = setTimeout(() => { initialTimer = null; syncAllWindows() @@ -1469,7 +1518,7 @@ export function createWindowBridgeController(options) { .finally(() => { scheduleNextPoll(); }); - }, 350); + }, 120); initialTimer.unref?.(); } diff --git a/sidecars/panel/bridge-daemon.mjs b/sidecars/panel/bridge-daemon.mjs index 01f68e1..ee71055 100644 --- a/sidecars/panel/bridge-daemon.mjs +++ b/sidecars/panel/bridge-daemon.mjs @@ -12,10 +12,15 @@ import { resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { + bootBundleVersion, bootWindowBridgeRuntime, setStandalonePortListener, } from './backend/api-actions.mjs'; -import { loadSettings, normalizeStandalonePort } from './backend/config.mjs'; +import { + computeRuntimeCodeVersion, + loadSettings, + normalizeStandalonePort, +} from './backend/config.mjs'; import { createStandaloneWebServer, parseStandaloneCliArgs, @@ -42,6 +47,19 @@ export function startBridgeDaemon() { onRestart: triggerRestart, }); + const driftWatchTimer = setInterval(() => { + if (shuttingDown) return; + try { + const diskVersion = String(computeRuntimeCodeVersion() || ''); + if (bootBundleVersion && diskVersion && diskVersion !== String(bootBundleVersion)) { + triggerRestart(); + } + } catch { + // 忽略文件覆盖期间瞬态 stat 异常 + } + }, 3000); + driftWatchTimer.unref?.(); + function resolveDesiredStandalonePort(settingsOverride = null) { if (envPinnedPort !== null) { return envPinnedPort; @@ -102,6 +120,7 @@ export function startBridgeDaemon() { async function shutdown({ disposeWindows = false, exitCode = 0 } = {}) { if (shuttingDown) return; shuttingDown = true; + clearInterval(driftWatchTimer); setStandalonePortListener(null); const forceTimer = setTimeout(() => { process.exit(exitCode); diff --git a/sidecars/panel/index.html b/sidecars/panel/index.html index e05cd9f..253bee6 100644 --- a/sidecars/panel/index.html +++ b/sidecars/panel/index.html @@ -4,11 +4,11 @@