window.HomeTab = { name: 'HomeTab', props: { sites: { type: Object, default: () => ({}) }, browserStatus: { type: Object, default: () => ({ connected: false }) }, systemStats: { type: Object, default: () => ({}) }, records: { type: Array, default: () => [] } }, emits: ['change-tab', 'add-site', 'notify'], computed: { siteCount() { return Object.keys(this.sites || {}).length; }, sortedRecords() { return (Array.isArray(this.records) ? this.records : []).slice().sort((a, b) => { return this.recordTimestamp(b) - this.recordTimestamp(a); }); }, recentRecords() { return this.sortedRecords.slice(0, 3); }, totalRequests() { const reported = Number(this.systemStats && this.systemStats.total_requests || 0); return reported > 0 ? reported : this.sortedRecords.length; }, averageDurationMs() { const completed = this.sortedRecords.filter(record => { return record && record.success && Number(record.duration_ms || 0) > 0; }); if (!completed.length) return 0; return Math.round(completed.reduce((total, record) => total + Number(record.duration_ms || 0), 0) / completed.length); }, baseUrl() { const origin = typeof window !== 'undefined' ? window.location.origin : 'http://127.0.0.1:8199'; return origin + '/v1'; }, heroTitle() { return this.browserStatus && this.browserStatus.connected ? '本地 API 已准备就绪' : '本地 API 正在等待浏览器'; }, browserLabel() { if (!(this.browserStatus && this.browserStatus.connected)) return '等待浏览器连接'; // 修复:此前读的 tab_title / tab_url 后端从未返回(health_check 只产出 // status/connected/port/tab_pool/error),导致这里恒为兜底文案。 // 改用 health 里真实存在的 tab_pool 概览。 const pool = this.browserStatus.tab_pool; if (pool && typeof pool === 'object') { const total = Number(pool.total || 0); const busy = Number(pool.busy || 0); const idle = Number(pool.idle || 0); if (total > 0) return `受控浏览器已连接 · ${total} 个标签页(忙碌 ${busy} / 空闲 ${idle})`; } return '受控浏览器已连接'; }, latestRecord() { return this.sortedRecords[0] || null; } }, methods: { recordTimestamp(record) { return Number(record && (record.finished_at || record.started_at || record.created_at || record.timestamp) || 0); }, recordDomain(record) { return String(record && (record.domain || record.site_domain || record.target_domain || record.model || record.route_group) || '未知站点'); }, recordStatus(record) { if (record && record.success) return '成功'; if (record && record.status === 'cancelled') return '已取消'; return '失败'; }, recordStatusClass(record) { if (record && record.success) return 'is-success'; if (record && record.status === 'cancelled') return 'is-cancelled'; return 'is-failed'; }, recordDuration(record) { const value = Number(record && record.duration_ms || 0); if (!value) return '-'; return value >= 1000 ? (value / 1000).toFixed(value >= 10000 ? 1 : 2) + 's' : Math.round(value) + 'ms'; }, recordTime(record) { const timestamp = this.recordTimestamp(record); if (!timestamp) return '-'; return new Date(timestamp * 1000).toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' }); }, averageDurationText() { if (!this.averageDurationMs) return '-'; return this.recordDuration({ duration_ms: this.averageDurationMs }); }, async copyBaseUrl() { try { await navigator.clipboard.writeText(this.baseUrl); this.$emit('notify', { message: 'API 地址已复制', type: 'success' }); } catch (error) { this.$emit('notify', { message: '复制失败,请手动复制地址', type: 'error' }); } } }, template: `
Web to API, ready

{{ heroTitle }}

连接已登录的 AI 网页,把常用模型作为兼容 OpenAI / Anthropic 的本地接口使用。

服务状态{{ browserStatus.connected ? '运行正常' : '等待浏览器' }}
可用站点{{ siteCount }} 个站点
累计请求{{ totalRequests.toLocaleString('zh-CN') }} 次
样本平均响应{{ averageDurationText() }}

开始使用

选择当前最需要处理的任务。

本机

连接 API 客户端

复制当前服务 Base URL,在兼容 OpenAI 的客户端中直接使用。

{{ baseUrl }}
{{ browserStatus.connected ? '已连接' : '未连接' }}

管理受控浏览器

检查当前浏览器连接与标签页占用情况,快速定位失效会话。

{{ browserStatus.connected ? '浏览器在线' : '浏览器离线' }}
{{ browserLabel }}
实时

检查最近请求

查看模型路由、响应耗时与异常详情,定位客户端接入问题。

{{ recordDomain(record) }}{{ recordStatus(record) }}
暂无请求记录
最近活动{{ recordDomain(latestRecord) }} · {{ recordStatus(latestRecord) }}
{{ latestRecord.endpoint || latestRecord.request_type || 'API 请求' }}{{ recordDuration(latestRecord) }}
` };