2026-04-22 15:28:45 +08:00
|
|
|
import * as vscode from 'vscode';
|
2026-04-27 10:03:22 +08:00
|
|
|
import { QuotaResponse, QuotaDisplay } from './types';
|
2026-04-22 15:28:45 +08:00
|
|
|
|
|
|
|
|
export class StatusBarManager {
|
|
|
|
|
private statusBarItem: vscode.StatusBarItem;
|
2026-04-22 16:51:03 +08:00
|
|
|
private currentPanel: vscode.WebviewPanel | undefined;
|
|
|
|
|
private onRefresh: () => Promise<QuotaDisplay | null>;
|
2026-04-22 15:28:45 +08:00
|
|
|
|
2026-04-22 16:51:03 +08:00
|
|
|
constructor(onRefresh: () => Promise<QuotaDisplay | null>) {
|
|
|
|
|
this.onRefresh = onRefresh;
|
2026-04-22 15:28:45 +08:00
|
|
|
this.statusBarItem = vscode.window.createStatusBarItem(
|
|
|
|
|
vscode.StatusBarAlignment.Right,
|
|
|
|
|
100
|
|
|
|
|
);
|
|
|
|
|
this.statusBarItem.command = 'getdeepin-quota.showPopup';
|
|
|
|
|
this.statusBarItem.text = '$(cloud) AI用量';
|
|
|
|
|
this.statusBarItem.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateNotLoggedIn(): void {
|
|
|
|
|
this.statusBarItem.text = '$(cloud) 未登录';
|
|
|
|
|
this.statusBarItem.tooltip = new vscode.MarkdownString('**点击登录 GetDeepin AI**');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateWithError(): void {
|
|
|
|
|
this.statusBarItem.text = '$(cloud) 获取失败';
|
|
|
|
|
this.statusBarItem.tooltip = new vscode.MarkdownString('**获取数据失败**\n\n点击重试或重新登录');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateWithQuota(data: QuotaDisplay): void {
|
2026-04-27 10:03:22 +08:00
|
|
|
const domesticPercent = Math.round(data.domestic.daily.percent);
|
|
|
|
|
const globalPercent = Math.round(data.global.monthly.percent);
|
2026-04-22 15:28:45 +08:00
|
|
|
|
|
|
|
|
this.statusBarItem.text = `$(cloud) 国内 ${domesticPercent}% | 国外 ${globalPercent}%`;
|
|
|
|
|
this.statusBarItem.tooltip = this.generateTooltipMarkdown(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private generateTooltipMarkdown(data: QuotaDisplay): vscode.MarkdownString {
|
|
|
|
|
const md = new vscode.MarkdownString();
|
|
|
|
|
md.supportHtml = true;
|
|
|
|
|
|
|
|
|
|
const formatNumber = (n: number) => n.toLocaleString('zh-CN');
|
|
|
|
|
const formatPercent = (n: number) => n.toFixed(1);
|
|
|
|
|
const getProgressBar = (percent: number) => {
|
|
|
|
|
const filled = Math.round(percent / 5);
|
|
|
|
|
const empty = 20 - filled;
|
|
|
|
|
return '█'.repeat(filled) + '░'.repeat(empty);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
md.appendMarkdown(`### AI 模型用量统计\n\n`);
|
|
|
|
|
md.appendMarkdown(`**${data.user.display_name}** (${data.user.job_id}) \n`);
|
|
|
|
|
md.appendMarkdown(`${data.user.email}\n\n`);
|
|
|
|
|
md.appendMarkdown('---\n\n');
|
|
|
|
|
|
2026-04-27 10:03:22 +08:00
|
|
|
const domesticDailyWarning = data.domestic.daily.percent > 80 ? '⚠️ ' : '';
|
|
|
|
|
md.appendMarkdown(`#### ${domesticDailyWarning}${data.domestic.label} (日额度)\n\n`);
|
|
|
|
|
md.appendMarkdown(`\`${getProgressBar(data.domestic.daily.percent)}\` \`${formatPercent(data.domestic.daily.percent)}%\`\n\n`);
|
|
|
|
|
md.appendMarkdown(`已用 **${formatNumber(data.domestic.daily.used)}** / ${formatNumber(data.domestic.daily.total)}\n\n`);
|
|
|
|
|
if (data.domestic.hasMonthlyLimit) {
|
|
|
|
|
md.appendMarkdown(`月额度: ${formatPercent(data.domestic.monthly.percent)}% (${formatNumber(data.domestic.monthly.used)} / ${formatNumber(data.domestic.monthly.total)})\n\n`);
|
|
|
|
|
}
|
2026-04-22 15:28:45 +08:00
|
|
|
md.appendMarkdown(`支持模型: ${data.domestic.models.map(m => `\`${m}\``).join(' ')}\n\n`);
|
|
|
|
|
|
2026-04-27 10:03:22 +08:00
|
|
|
const globalWarning = data.global.monthly.percent > 80 ? '⚠️ ' : '';
|
|
|
|
|
md.appendMarkdown(`#### ${globalWarning}${data.global.label}\n\n`);
|
|
|
|
|
md.appendMarkdown(`\`${getProgressBar(data.global.monthly.percent)}\` \`${formatPercent(data.global.monthly.percent)}%\`\n\n`);
|
|
|
|
|
md.appendMarkdown(`已用 **${formatNumber(data.global.monthly.used)}** / ${formatNumber(data.global.monthly.total)}\n\n`);
|
2026-04-22 15:28:45 +08:00
|
|
|
md.appendMarkdown(`支持模型: ${data.global.models.map(m => `\`${m}\``).join(' ')}\n\n`);
|
|
|
|
|
|
|
|
|
|
md.appendMarkdown('---\n\n');
|
|
|
|
|
md.appendMarkdown('*点击查看更多操作*');
|
|
|
|
|
|
|
|
|
|
return md;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showPopup(data: QuotaDisplay | null, isLoggedIn: boolean): void {
|
|
|
|
|
if (!isLoggedIn) {
|
|
|
|
|
this.showLoginPrompt();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
|
this.showErrorPrompt();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 16:51:03 +08:00
|
|
|
this.showQuotaPanel(data);
|
2026-04-22 15:28:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private showLoginPrompt(): void {
|
|
|
|
|
vscode.window.showQuickPick(
|
|
|
|
|
[{ label: '$(sign-in) 登录', id: 'login' }],
|
|
|
|
|
{ placeHolder: '请先登录 GetDeepin AI' }
|
|
|
|
|
).then(selection => {
|
2026-04-22 16:51:03 +08:00
|
|
|
if ((selection as any)?.id === 'login') {
|
2026-04-22 15:28:45 +08:00
|
|
|
vscode.commands.executeCommand('getdeepin-quota.login');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private showErrorPrompt(): void {
|
|
|
|
|
vscode.window.showQuickPick(
|
|
|
|
|
[
|
|
|
|
|
{ label: '$(refresh) 重试', id: 'refresh' },
|
|
|
|
|
{ label: '$(sign-in) 重新登录', id: 'login' }
|
|
|
|
|
],
|
|
|
|
|
{ placeHolder: '获取用量数据失败' }
|
|
|
|
|
).then(selection => {
|
2026-04-22 16:51:03 +08:00
|
|
|
const item = selection as any;
|
|
|
|
|
if (item?.id === 'refresh') {
|
2026-04-22 15:28:45 +08:00
|
|
|
vscode.commands.executeCommand('getdeepin-quota.refresh');
|
2026-04-22 16:51:03 +08:00
|
|
|
} else if (item?.id === 'login') {
|
2026-04-22 15:28:45 +08:00
|
|
|
vscode.commands.executeCommand('getdeepin-quota.login');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 16:51:03 +08:00
|
|
|
private showQuotaPanel(data: QuotaDisplay): void {
|
|
|
|
|
if (this.currentPanel) {
|
|
|
|
|
this.currentPanel.reveal(vscode.ViewColumn.Active);
|
|
|
|
|
this.currentPanel.webview.html = this.generatePanelHtml(data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const panel = vscode.window.createWebviewPanel(
|
|
|
|
|
'getdeepin-quota',
|
|
|
|
|
'AI 模型用量详情',
|
|
|
|
|
vscode.ViewColumn.Active,
|
|
|
|
|
{ enableScripts: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.currentPanel = panel;
|
|
|
|
|
panel.webview.html = this.generatePanelHtml(data);
|
|
|
|
|
|
|
|
|
|
panel.webview.onDidReceiveMessage(async (message) => {
|
|
|
|
|
if (message.command === 'refresh') {
|
|
|
|
|
const newData = await this.onRefresh();
|
|
|
|
|
if (newData && this.currentPanel) {
|
|
|
|
|
this.currentPanel.webview.html = this.generatePanelHtml(newData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
panel.onDidDispose(() => {
|
|
|
|
|
this.currentPanel = undefined;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private generatePanelHtml(data: QuotaDisplay): string {
|
2026-04-22 15:28:45 +08:00
|
|
|
const formatNumber = (n: number) => n.toLocaleString('zh-CN');
|
|
|
|
|
const formatPercent = (n: number) => n.toFixed(1);
|
|
|
|
|
|
2026-04-22 16:51:03 +08:00
|
|
|
return `
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
font-family: var(--vscode-font-family);
|
|
|
|
|
padding: 20px;
|
|
|
|
|
color: var(--vscode-foreground);
|
|
|
|
|
background-color: var(--vscode-editor-background);
|
|
|
|
|
}
|
|
|
|
|
.header {
|
|
|
|
|
margin-bottom: 24px;
|
|
|
|
|
padding-bottom: 16px;
|
|
|
|
|
border-bottom: 1px solid var(--vscode-panel-border);
|
|
|
|
|
}
|
|
|
|
|
.header h1 {
|
|
|
|
|
margin: 0 0 8px 0;
|
|
|
|
|
font-size: 24px;
|
|
|
|
|
}
|
|
|
|
|
.user-info {
|
|
|
|
|
color: var(--vscode-descriptionForeground);
|
|
|
|
|
}
|
|
|
|
|
.quota-section {
|
|
|
|
|
margin-bottom: 24px;
|
|
|
|
|
}
|
|
|
|
|
.quota-section h2 {
|
|
|
|
|
margin: 0 0 12px 0;
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
}
|
2026-04-27 10:03:22 +08:00
|
|
|
.quota-row {
|
2026-04-22 16:51:03 +08:00
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
gap: 12px;
|
2026-04-27 10:03:22 +08:00
|
|
|
margin-bottom: 8px;
|
|
|
|
|
}
|
|
|
|
|
.quota-label {
|
|
|
|
|
min-width: 60px;
|
|
|
|
|
color: var(--vscode-descriptionForeground);
|
2026-04-22 16:51:03 +08:00
|
|
|
}
|
|
|
|
|
.quota-percent {
|
2026-04-27 10:03:22 +08:00
|
|
|
font-size: 20px;
|
2026-04-22 16:51:03 +08:00
|
|
|
font-weight: bold;
|
2026-04-27 10:03:22 +08:00
|
|
|
min-width: 70px;
|
2026-04-22 16:51:03 +08:00
|
|
|
}
|
|
|
|
|
.quota-details {
|
|
|
|
|
color: var(--vscode-descriptionForeground);
|
|
|
|
|
}
|
|
|
|
|
.progress-container {
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 8px;
|
|
|
|
|
background-color: var(--vscode-progressBar-background);
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
margin-bottom: 12px;
|
|
|
|
|
}
|
|
|
|
|
.progress-bar {
|
|
|
|
|
height: 100%;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
transition: width 0.3s ease;
|
|
|
|
|
}
|
|
|
|
|
.models-list {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
gap: 8px;
|
2026-04-27 10:03:22 +08:00
|
|
|
margin-top: 12px;
|
2026-04-22 16:51:03 +08:00
|
|
|
}
|
|
|
|
|
.model-tag {
|
|
|
|
|
padding: 4px 8px;
|
|
|
|
|
background-color: var(--vscode-badge-background);
|
|
|
|
|
color: var(--vscode-badge-foreground);
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
}
|
|
|
|
|
.warning { color: #f59e0b; }
|
|
|
|
|
.progress-ok { background-color: #10b981; }
|
|
|
|
|
.progress-warning { background-color: #f59e0b; }
|
|
|
|
|
.actions {
|
|
|
|
|
margin-top: 24px;
|
|
|
|
|
padding-top: 16px;
|
|
|
|
|
border-top: 1px solid var(--vscode-panel-border);
|
|
|
|
|
}
|
|
|
|
|
.refresh-btn {
|
|
|
|
|
padding: 8px 16px;
|
|
|
|
|
background-color: var(--vscode-button-background);
|
|
|
|
|
color: var(--vscode-button-foreground);
|
|
|
|
|
border: none;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
}
|
|
|
|
|
.refresh-btn:hover {
|
|
|
|
|
background-color: var(--vscode-button-hoverBackground);
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div class="header">
|
|
|
|
|
<h1>AI 模型用量统计</h1>
|
|
|
|
|
<div class="user-info">
|
|
|
|
|
<strong>${data.user.display_name}</strong> (${data.user.job_id}) · ${data.user.email}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-04-22 15:28:45 +08:00
|
|
|
|
2026-04-22 16:51:03 +08:00
|
|
|
<div class="quota-section">
|
2026-04-27 10:03:22 +08:00
|
|
|
<h2>${data.domestic.label}</h2>
|
|
|
|
|
${data.domestic.hasDailyLimit ? `
|
|
|
|
|
<div class="quota-row">
|
|
|
|
|
<span class="quota-label">日额度</span>
|
|
|
|
|
<span class="quota-percent ${data.domestic.daily.percent > 80 ? 'warning' : ''}">${formatPercent(data.domestic.daily.percent)}%</span>
|
|
|
|
|
<span class="quota-details">已用 ${formatNumber(data.domestic.daily.used)} / ${formatNumber(data.domestic.daily.total)}</span>
|
2026-04-22 16:51:03 +08:00
|
|
|
</div>
|
|
|
|
|
<div class="progress-container">
|
2026-04-27 10:03:22 +08:00
|
|
|
<div class="progress-bar ${data.domestic.daily.percent > 80 ? 'progress-warning' : 'progress-ok'}" style="width: ${Math.min(data.domestic.daily.percent, 100)}%;"></div>
|
2026-04-22 16:51:03 +08:00
|
|
|
</div>
|
2026-04-27 10:03:22 +08:00
|
|
|
` : ''}
|
|
|
|
|
${data.domestic.hasMonthlyLimit ? `
|
|
|
|
|
<div class="quota-row">
|
|
|
|
|
<span class="quota-label">月额度</span>
|
|
|
|
|
<span class="quota-percent ${data.domestic.monthly.percent > 80 ? 'warning' : ''}">${formatPercent(data.domestic.monthly.percent)}%</span>
|
|
|
|
|
<span class="quota-details">已用 ${formatNumber(data.domestic.monthly.used)} / ${formatNumber(data.domestic.monthly.total)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="progress-container">
|
|
|
|
|
<div class="progress-bar ${data.domestic.monthly.percent > 80 ? 'progress-warning' : 'progress-ok'}" style="width: ${Math.min(data.domestic.monthly.percent, 100)}%;"></div>
|
|
|
|
|
</div>
|
|
|
|
|
` : ''}
|
2026-04-22 16:51:03 +08:00
|
|
|
<div class="models-list">
|
|
|
|
|
${data.domestic.models.map(m => `<span class="model-tag">${m}</span>`).join('')}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="quota-section">
|
2026-04-27 10:03:22 +08:00
|
|
|
<h2>${data.global.label}</h2>
|
|
|
|
|
${data.global.hasDailyLimit ? `
|
|
|
|
|
<div class="quota-row">
|
|
|
|
|
<span class="quota-label">日额度</span>
|
|
|
|
|
<span class="quota-percent ${data.global.daily.percent > 80 ? 'warning' : ''}">${formatPercent(data.global.daily.percent)}%</span>
|
|
|
|
|
<span class="quota-details">已用 ${formatNumber(data.global.daily.used)} / ${formatNumber(data.global.daily.total)}</span>
|
2026-04-22 16:51:03 +08:00
|
|
|
</div>
|
|
|
|
|
<div class="progress-container">
|
2026-04-27 10:03:22 +08:00
|
|
|
<div class="progress-bar ${data.global.daily.percent > 80 ? 'progress-warning' : 'progress-ok'}" style="width: ${Math.min(data.global.daily.percent, 100)}%;"></div>
|
2026-04-22 16:51:03 +08:00
|
|
|
</div>
|
2026-04-27 10:03:22 +08:00
|
|
|
` : ''}
|
|
|
|
|
${data.global.hasMonthlyLimit ? `
|
|
|
|
|
<div class="quota-row">
|
|
|
|
|
<span class="quota-label">月额度</span>
|
|
|
|
|
<span class="quota-percent ${data.global.monthly.percent > 80 ? 'warning' : ''}">${formatPercent(data.global.monthly.percent)}%</span>
|
|
|
|
|
<span class="quota-details">已用 ${formatNumber(data.global.monthly.used)} / ${formatNumber(data.global.monthly.total)}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="progress-container">
|
|
|
|
|
<div class="progress-bar ${data.global.monthly.percent > 80 ? 'progress-warning' : 'progress-ok'}" style="width: ${Math.min(data.global.monthly.percent, 100)}%;"></div>
|
|
|
|
|
</div>
|
|
|
|
|
` : ''}
|
2026-04-22 16:51:03 +08:00
|
|
|
<div class="models-list">
|
|
|
|
|
${data.global.models.map(m => `<span class="model-tag">${m}</span>`).join('')}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="actions">
|
|
|
|
|
<button class="refresh-btn" onclick="refresh()">刷新数据</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
const vscode = acquireVsCodeApi();
|
|
|
|
|
function refresh() {
|
|
|
|
|
vscode.postMessage({ command: 'refresh' });
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
`;
|
2026-04-22 15:28:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dispose(): void {
|
|
|
|
|
this.statusBarItem.dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function parseQuotaResponse(response: QuotaResponse): QuotaDisplay {
|
|
|
|
|
const domestic = response.groups.find(g => g.key === 'domestic')!;
|
|
|
|
|
const global = response.groups.find(g => g.key === 'global')!;
|
|
|
|
|
|
2026-04-27 10:03:22 +08:00
|
|
|
const parseQuota = (usedStr: string, totalStr: string) => {
|
|
|
|
|
const used = parseFloat(usedStr) || 0;
|
|
|
|
|
const total = parseFloat(totalStr) || 1;
|
|
|
|
|
const percent = total > 0 ? (used / total) * 100 : 0;
|
|
|
|
|
return { used, total, percent };
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-22 15:28:45 +08:00
|
|
|
return {
|
|
|
|
|
user: response.user,
|
|
|
|
|
domestic: {
|
2026-04-27 10:03:22 +08:00
|
|
|
label: domestic.label,
|
|
|
|
|
models: domestic.models,
|
|
|
|
|
hasDailyLimit: domestic.has_daily_limit,
|
|
|
|
|
hasMonthlyLimit: domestic.has_monthly_limit,
|
|
|
|
|
daily: parseQuota(domestic.usage.daily_amount, domestic.effective_daily_amount),
|
|
|
|
|
monthly: parseQuota(domestic.usage.month_amount, domestic.effective_monthly_amount)
|
2026-04-22 15:28:45 +08:00
|
|
|
},
|
|
|
|
|
global: {
|
2026-04-27 10:03:22 +08:00
|
|
|
label: global.label,
|
|
|
|
|
models: global.models,
|
|
|
|
|
hasDailyLimit: global.has_daily_limit,
|
|
|
|
|
hasMonthlyLimit: global.has_monthly_limit,
|
|
|
|
|
daily: parseQuota(global.usage.daily_amount, global.effective_daily_amount),
|
|
|
|
|
monthly: parseQuota(global.usage.month_amount, global.effective_monthly_amount)
|
2026-04-22 15:28:45 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|