more interactive refresh and ux improvements
This commit is contained in:
1 parent
3f834ebd3d
commit
6afd4647ae
5 files changed
+234
-61
No files matched your search
Generated
+8
-8
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "getdeepin-quota",
|
||||
"version": "0.0.1",
|
||||
"version": "0.1.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "getdeepin-quota",
|
||||
"version": "0.0.1",
|
||||
"version": "0.1.0",
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.10.0",
|
||||
"@types/vscode": "^1.85.0",
|
||||
@@ -17,9 +17,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "20.19.39",
|
||||
"resolved": "https://registry.npmmirror.com/@types/node/-/node-20.19.39.tgz",
|
||||
"integrity": "sha512-orrrD74MBUyK8jOAD/r0+lfa1I2MO6I+vAkmAWzMYbCcgrN4lCrmK52gRFQq/JRxfYPfonkr4b0jcY7Olqdqbw==",
|
||||
"version": "20.19.40",
|
||||
"resolved": "https://registry.npmmirror.com/@types/node/-/node-20.19.40.tgz",
|
||||
"integrity": "sha512-xxx6M2IpSTnnKcR0cMvIiohkiCx20/oRPtWGbenFygKCGl3zqUzdNjQ/1V4solq1LU+dgv0nQzeGOuqkqZGg0Q==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
@@ -27,9 +27,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@types/vscode": {
|
||||
"version": "1.116.0",
|
||||
"resolved": "https://registry.npmmirror.com/@types/vscode/-/vscode-1.116.0.tgz",
|
||||
"integrity": "sha512-sYHp4MO6BqJ2PD7Hjt0hlIS3tMaYsVPJrd0RUjDJ8HtOYnyJIEej0bLSccM8rE77WrC+Xox/kdBwEFDO8MsxNA==",
|
||||
"version": "1.118.0",
|
||||
"resolved": "https://registry.npmmirror.com/@types/vscode/-/vscode-1.118.0.tgz",
|
||||
"integrity": "sha512-Ah6eTlqDcwIMELEVwQMO++rJAFBRz/oLluLD/vWdYrH1KuI9kfpaM+7pg0OvvascgcJy+ghLCERAYouM4QbzGw==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
"name": "getdeepin-quota",
|
||||
"displayName": "GetDeepin AI Quota",
|
||||
"description": "显示大模型资源用量情况",
|
||||
"version": "0.0.3",
|
||||
"version": "0.1.0",
|
||||
"publisher": "getdeepin",
|
||||
"repository": "https://gitlabwh.uniontech.com/wangzichong/vscode-getdeepin-quota",
|
||||
"engines": {
|
||||
|
||||
+11
-6
@@ -48,6 +48,10 @@ export class AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
export type FetchQuotaResult =
|
||||
| { ok: true; response: Response }
|
||||
| { ok: false; error: string };
|
||||
|
||||
export class QuotaService {
|
||||
private authService: AuthService;
|
||||
|
||||
@@ -55,9 +59,9 @@ export class QuotaService {
|
||||
this.authService = authService;
|
||||
}
|
||||
|
||||
async fetchQuota(): Promise<Response | null> {
|
||||
async fetchQuota(): Promise<FetchQuotaResult> {
|
||||
if (!this.authService.isLoggedIn()) {
|
||||
return null;
|
||||
return { ok: false, error: '未设置 Token' };
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -69,17 +73,18 @@ export class QuotaService {
|
||||
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
await this.authService.logout();
|
||||
return null;
|
||||
return { ok: false, error: 'Token 无效或已过期' };
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
return { ok: false, error: `请求失败(HTTP ${response.status})` };
|
||||
}
|
||||
|
||||
return response;
|
||||
return { ok: true, response };
|
||||
} catch (error) {
|
||||
console.error('Fetch quota failed:', error);
|
||||
return null;
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
return { ok: false, error: `网络请求失败:${message}` };
|
||||
}
|
||||
}
|
||||
}
|
||||
+51
-14
@@ -9,6 +9,20 @@ let authService: AuthService;
|
||||
let quotaService: QuotaService;
|
||||
let statusBar: StatusBarManager;
|
||||
let currentQuota: QuotaDisplay | null = null;
|
||||
let lastRefreshAt: Date | null = null;
|
||||
let lastRefreshError: string | null = null;
|
||||
|
||||
function getConfiguredBaseUrl(): string {
|
||||
const config = vscode.workspace.getConfiguration('getdeepin-quota');
|
||||
const url = config.get<string>('baseUrl', 'https://ai.getdeepin.org/');
|
||||
return url.endsWith('/') ? url : `${url}/`;
|
||||
}
|
||||
|
||||
type RefreshResult = {
|
||||
data: QuotaDisplay | null;
|
||||
lastRefreshAt: Date | null;
|
||||
error: string | null;
|
||||
};
|
||||
|
||||
export async function activate(context: vscode.ExtensionContext) {
|
||||
storageService = new StorageService(context);
|
||||
@@ -24,8 +38,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||
|
||||
statusBar = new StatusBarManager(
|
||||
async () => {
|
||||
await refreshQuota();
|
||||
return currentQuota;
|
||||
return await refreshQuota();
|
||||
}
|
||||
);
|
||||
context.subscriptions.push(statusBar);
|
||||
@@ -39,9 +52,10 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||
}
|
||||
|
||||
async function loginCommand(): Promise<void> {
|
||||
const baseUrl = getConfiguredBaseUrl();
|
||||
const token = await vscode.window.showInputBox({
|
||||
prompt: '请输入 Bearer Token',
|
||||
placeHolder: '在 https://ai.getdeepin.org/me 生成 Token',
|
||||
placeHolder: `在 ${baseUrl}me 生成 Token`,
|
||||
password: true
|
||||
});
|
||||
|
||||
@@ -50,21 +64,26 @@ async function loginCommand(): Promise<void> {
|
||||
}
|
||||
|
||||
await authService.setToken(token);
|
||||
const response = await quotaService.fetchQuota();
|
||||
const result = await quotaService.fetchQuota();
|
||||
|
||||
if (!response) {
|
||||
vscode.window.showErrorMessage('Token 无效或已过期,请检查后重试');
|
||||
if (!result.ok) {
|
||||
vscode.window.showErrorMessage(result.error);
|
||||
await authService.logout();
|
||||
statusBar.updateNotLoggedIn();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await response.json() as QuotaResponse;
|
||||
const data = await result.response.json() as QuotaResponse;
|
||||
currentQuota = parseQuotaResponse(data);
|
||||
lastRefreshAt = new Date();
|
||||
lastRefreshError = null;
|
||||
statusBar.updateWithQuota(currentQuota);
|
||||
vscode.window.showInformationMessage('Token 验证成功');
|
||||
} catch {
|
||||
} catch (error) {
|
||||
console.error('Parse quota response failed:', error);
|
||||
currentQuota = null;
|
||||
lastRefreshError = '解析用量数据失败';
|
||||
vscode.window.showErrorMessage('解析用量数据失败');
|
||||
statusBar.updateWithError();
|
||||
}
|
||||
@@ -73,30 +92,48 @@ async function loginCommand(): Promise<void> {
|
||||
async function logoutCommand(): Promise<void> {
|
||||
await authService.logout();
|
||||
currentQuota = null;
|
||||
lastRefreshAt = null;
|
||||
lastRefreshError = null;
|
||||
statusBar.updateNotLoggedIn();
|
||||
vscode.window.showInformationMessage('已移除 Token');
|
||||
}
|
||||
|
||||
async function refreshQuota(): Promise<void> {
|
||||
const response = await quotaService.fetchQuota();
|
||||
async function refreshQuota(): Promise<RefreshResult> {
|
||||
const result = await quotaService.fetchQuota();
|
||||
|
||||
if (!response) {
|
||||
if (!result.ok) {
|
||||
currentQuota = null;
|
||||
lastRefreshError = result.error;
|
||||
statusBar.updateWithError();
|
||||
return;
|
||||
return {
|
||||
data: null,
|
||||
lastRefreshAt,
|
||||
error: lastRefreshError
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await response.json() as QuotaResponse;
|
||||
const data = await result.response.json() as QuotaResponse;
|
||||
currentQuota = parseQuotaResponse(data);
|
||||
lastRefreshAt = new Date();
|
||||
lastRefreshError = null;
|
||||
statusBar.updateWithQuota(currentQuota);
|
||||
} catch (error) {
|
||||
console.error('Parse quota response failed:', error);
|
||||
currentQuota = null;
|
||||
lastRefreshError = '解析用量数据失败';
|
||||
statusBar.updateWithError();
|
||||
}
|
||||
|
||||
return {
|
||||
data: currentQuota,
|
||||
lastRefreshAt,
|
||||
error: lastRefreshError
|
||||
};
|
||||
}
|
||||
|
||||
function showPopup(): void {
|
||||
statusBar.showPopup(currentQuota, authService.isLoggedIn());
|
||||
statusBar.showPopup(currentQuota, authService.isLoggedIn(), lastRefreshAt, lastRefreshError);
|
||||
}
|
||||
|
||||
export function deactivate() {}
|
||||
+163
-32
@@ -4,9 +4,17 @@ import { QuotaResponse, QuotaDisplay } from './types';
|
||||
export class StatusBarManager {
|
||||
private statusBarItem: vscode.StatusBarItem;
|
||||
private currentPanel: vscode.WebviewPanel | undefined;
|
||||
private onRefresh: () => Promise<QuotaDisplay | null>;
|
||||
private onRefresh: () => Promise<{
|
||||
data: QuotaDisplay | null;
|
||||
lastRefreshAt: Date | null;
|
||||
error: string | null;
|
||||
}>;
|
||||
|
||||
constructor(onRefresh: () => Promise<QuotaDisplay | null>) {
|
||||
constructor(onRefresh: () => Promise<{
|
||||
data: QuotaDisplay | null;
|
||||
lastRefreshAt: Date | null;
|
||||
error: string | null;
|
||||
}>) {
|
||||
this.onRefresh = onRefresh;
|
||||
this.statusBarItem = vscode.window.createStatusBarItem(
|
||||
vscode.StatusBarAlignment.Right,
|
||||
@@ -78,18 +86,23 @@ export class StatusBarManager {
|
||||
return md;
|
||||
}
|
||||
|
||||
showPopup(data: QuotaDisplay | null, isLoggedIn: boolean): void {
|
||||
showPopup(
|
||||
data: QuotaDisplay | null,
|
||||
isLoggedIn: boolean,
|
||||
lastRefreshAt: Date | null,
|
||||
lastRefreshError: string | null
|
||||
): void {
|
||||
if (!isLoggedIn) {
|
||||
this.showLoginPrompt();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
this.showErrorPrompt();
|
||||
this.showErrorPrompt(lastRefreshAt, lastRefreshError);
|
||||
return;
|
||||
}
|
||||
|
||||
this.showQuotaPanel(data);
|
||||
this.showQuotaPanel(data, lastRefreshAt, lastRefreshError);
|
||||
}
|
||||
|
||||
private showLoginPrompt(): void {
|
||||
@@ -103,27 +116,38 @@ export class StatusBarManager {
|
||||
});
|
||||
}
|
||||
|
||||
private showErrorPrompt(): void {
|
||||
private showErrorPrompt(lastRefreshAt: Date | null, lastRefreshError: string | null): void {
|
||||
vscode.window.showQuickPick(
|
||||
[
|
||||
{ label: '$(refresh) 重试', id: 'refresh' },
|
||||
{ label: '$(preview) 查看详情', id: 'details' },
|
||||
{ label: '$(key) 重新设置 Token', id: 'login' }
|
||||
],
|
||||
{ placeHolder: '获取用量数据失败' }
|
||||
{
|
||||
placeHolder: lastRefreshError
|
||||
? `获取用量数据失败: ${lastRefreshError}`
|
||||
: '获取用量数据失败'
|
||||
}
|
||||
).then(selection => {
|
||||
const item = selection as any;
|
||||
if (item?.id === 'refresh') {
|
||||
vscode.commands.executeCommand('getdeepin-quota.refresh');
|
||||
} else if (item?.id === 'details') {
|
||||
this.showQuotaPanel(null, lastRefreshAt, lastRefreshError);
|
||||
} else if (item?.id === 'login') {
|
||||
vscode.commands.executeCommand('getdeepin-quota.login');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private showQuotaPanel(data: QuotaDisplay): void {
|
||||
private showQuotaPanel(
|
||||
data: QuotaDisplay | null,
|
||||
lastRefreshAt: Date | null,
|
||||
lastRefreshError: string | null
|
||||
): void {
|
||||
if (this.currentPanel) {
|
||||
this.currentPanel.reveal(vscode.ViewColumn.Active);
|
||||
this.currentPanel.webview.html = this.generatePanelHtml(data);
|
||||
this.currentPanel.webview.html = this.generatePanelHtml(data, lastRefreshAt, lastRefreshError);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -135,13 +159,24 @@ export class StatusBarManager {
|
||||
);
|
||||
|
||||
this.currentPanel = panel;
|
||||
panel.webview.html = this.generatePanelHtml(data);
|
||||
panel.webview.html = this.generatePanelHtml(data, lastRefreshAt, lastRefreshError);
|
||||
|
||||
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);
|
||||
try {
|
||||
const result = await this.onRefresh();
|
||||
if (this.currentPanel === panel) {
|
||||
this.currentPanel.webview.html = this.generatePanelHtml(
|
||||
result.data,
|
||||
result.lastRefreshAt,
|
||||
result.error
|
||||
);
|
||||
return;
|
||||
}
|
||||
} finally {
|
||||
if (this.currentPanel === panel) {
|
||||
void this.currentPanel.webview.postMessage({ command: 'refreshFinished' });
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -151,9 +186,16 @@ export class StatusBarManager {
|
||||
});
|
||||
}
|
||||
|
||||
private generatePanelHtml(data: QuotaDisplay): string {
|
||||
private generatePanelHtml(
|
||||
data: QuotaDisplay | null,
|
||||
lastRefreshAt: Date | null,
|
||||
lastRefreshError: string | null
|
||||
): string {
|
||||
const formatTokens = (n: number) => n.toLocaleString('zh-CN');
|
||||
const formatPercent = (n: number) => n.toFixed(1);
|
||||
const formatTime = (time: Date | null) => time
|
||||
? time.toLocaleString('zh-CN', { hour12: false })
|
||||
: '暂无';
|
||||
|
||||
const renderQuotaSection = (
|
||||
label: string,
|
||||
@@ -203,6 +245,39 @@ export class StatusBarManager {
|
||||
return html;
|
||||
};
|
||||
|
||||
const refreshMetaHtml = `
|
||||
<div class="meta-row">
|
||||
<span class="meta-label">最后刷新</span>
|
||||
<span class="meta-value">${formatTime(lastRefreshAt)}</span>
|
||||
</div>
|
||||
${lastRefreshError ? `
|
||||
<div class="error-banner">
|
||||
<strong>刷新失败</strong>
|
||||
<div>${lastRefreshError}</div>
|
||||
</div>` : ''}`;
|
||||
|
||||
const quotaContentHtml = data ? `
|
||||
${renderQuotaSection(
|
||||
data.domestic.label,
|
||||
data.domestic.remainingDailyPercent,
|
||||
data.domestic.remainingMonthlyPercent,
|
||||
data.domestic.hasDailyLimit,
|
||||
data.domestic.totalTokens,
|
||||
data.domestic.models
|
||||
)}
|
||||
|
||||
${renderQuotaSection(
|
||||
data.global.label,
|
||||
data.global.remainingDailyPercent,
|
||||
data.global.remainingMonthlyPercent,
|
||||
data.global.hasDailyLimit,
|
||||
data.global.totalTokens,
|
||||
data.global.models
|
||||
)}` : `
|
||||
<div class="empty-state">
|
||||
当前没有可展示的额度数据,请检查 Token 或稍后重试。
|
||||
</div>`;
|
||||
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -227,6 +302,37 @@ export class StatusBarManager {
|
||||
.user-info {
|
||||
color: var(--vscode-descriptionForeground);
|
||||
}
|
||||
.meta-row {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin-bottom: 16px;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
}
|
||||
.meta-label {
|
||||
min-width: 60px;
|
||||
}
|
||||
.meta-value {
|
||||
color: var(--vscode-foreground);
|
||||
}
|
||||
.error-banner {
|
||||
margin-bottom: 20px;
|
||||
padding: 12px 14px;
|
||||
border-radius: 6px;
|
||||
border: 1px solid var(--vscode-inputValidation-errorBorder);
|
||||
background-color: var(--vscode-inputValidation-errorBackground);
|
||||
color: var(--vscode-inputValidation-errorForeground);
|
||||
}
|
||||
.error-banner strong {
|
||||
display: block;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
.empty-state {
|
||||
margin: 20px 0 24px;
|
||||
padding: 16px;
|
||||
border: 1px dashed var(--vscode-panel-border);
|
||||
border-radius: 6px;
|
||||
color: var(--vscode-descriptionForeground);
|
||||
}
|
||||
.quota-section {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
@@ -294,45 +400,70 @@ export class StatusBarManager {
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
}
|
||||
.refresh-btn:hover {
|
||||
background-color: var(--vscode-button-hoverBackground);
|
||||
}
|
||||
.refresh-btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
@keyframes spin {
|
||||
from { transform: rotate(0deg); }
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
.spinner {
|
||||
display: inline-block;
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
border: 2px solid var(--vscode-button-foreground);
|
||||
border-top-color: transparent;
|
||||
border-radius: 50%;
|
||||
animation: spin 0.6s linear infinite;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<h1>AI 模型用量统计</h1>
|
||||
${data ? `
|
||||
<div class="user-info">
|
||||
<strong>${data.user.display_name}</strong> (${data.user.job_id}) · ${data.user.email}
|
||||
</div>
|
||||
</div>` : ''}
|
||||
</div>
|
||||
|
||||
${renderQuotaSection(
|
||||
data.domestic.label,
|
||||
data.domestic.remainingDailyPercent,
|
||||
data.domestic.remainingMonthlyPercent,
|
||||
data.domestic.hasDailyLimit,
|
||||
data.domestic.totalTokens,
|
||||
data.domestic.models
|
||||
)}
|
||||
${refreshMetaHtml}
|
||||
|
||||
${renderQuotaSection(
|
||||
data.global.label,
|
||||
data.global.remainingDailyPercent,
|
||||
data.global.remainingMonthlyPercent,
|
||||
data.global.hasDailyLimit,
|
||||
data.global.totalTokens,
|
||||
data.global.models
|
||||
)}
|
||||
${quotaContentHtml}
|
||||
|
||||
<div class="actions">
|
||||
<button class="refresh-btn" onclick="refresh()">刷新数据</button>
|
||||
<button id="refresh-btn" class="refresh-btn" onclick="refresh()">刷新数据</button>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const vscode = acquireVsCodeApi();
|
||||
function setRefreshing(refreshing) {
|
||||
const btn = document.getElementById('refresh-btn');
|
||||
if (!btn) {
|
||||
return;
|
||||
}
|
||||
btn.disabled = refreshing;
|
||||
btn.innerHTML = refreshing
|
||||
? '<span class="spinner"></span>刷新中...'
|
||||
: '刷新数据';
|
||||
}
|
||||
|
||||
window.addEventListener('message', (event) => {
|
||||
if (event.data?.command === 'refreshFinished') {
|
||||
setRefreshing(false);
|
||||
}
|
||||
});
|
||||
|
||||
function refresh() {
|
||||
setRefreshing(true);
|
||||
vscode.postMessage({ command: 'refresh' });
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in new issue
Block a user