tweak behavior and add README

This commit is contained in:
blumia committed 2026-04-22 16:51:03 +08:00
1 parent 4d67f0c8f9
commit 35da5bb8a0
3 files changed
+282 -49

No files matched your search

+83
View File
@@ -0,0 +1,83 @@
# GetDeepin AI Quota
VS Code 扩展,在状态栏显示 GetDeepin AI 平台的大模型资源用量情况。
## 功能
- 状态栏显示国内/国外模型用量百分比
- Hover 显示详细信息(已用量、总额度、支持模型)
- 点击打开详情面板
- 支持手动刷新数据
- 自动保存登录凭据(使用 VS Code Secret Storage)
- 登录过期自动重新登录
## 构建
### 前置要求
- Node.js 18+
- npm
### 安装依赖
```bash
npm install
```
### 编译
```bash
npm run compile
```
### 开发模式(监听文件变化)
```bash
npm run watch
```
## 打包 VSIX
### 安装 vsce 工具
```bash
npm install -g @vscode/vsce
```
### 打包
```bash
vsce package
```
打包完成后会在当前目录生成 `getdeepin-quota-0.0.1.vsix` 文件。
## 安装扩展
### 从 VSIX 安装
1. 打开 VS Code
2. 按 `Ctrl+Shift+P` 打开命令面板
3. 输入 `Extensions: Install from VSIX...`
4. 选择生成的 `.vsix` 文件
### 命令行安装
```bash
code --install-extension getdeepin-quota-0.0.1.vsix
```
## 使用
1. 扩展启动后,状态栏右侧显示"未登录"
2. 点击状态栏或运行命令 `GetDeepin AI: 登录`
3. 输入 GetDeepin AI 用户名和密码
4. 登录成功后显示用量信息
## 命令
| 命令 | 说明 |
|------|------|
| `GetDeepin AI: 登录` | 登录 GetDeepin AI |
| `GetDeepin AI: 登出` | 登出当前账号 |
| `GetDeepin AI: 刷新用量` | 手动刷新用量数据 |
+15 -11
View File
@@ -14,9 +14,21 @@ export async function activate(context: vscode.ExtensionContext) {
storageService = new StorageService(context); storageService = new StorageService(context);
authService = new AuthService(storageService); authService = new AuthService(storageService);
quotaService = new QuotaService(authService); quotaService = new QuotaService(authService);
statusBar = new StatusBarManager(() => {
vscode.commands.executeCommand('getdeepin-quota.showPopup'); context.subscriptions.push(
}); vscode.commands.registerCommand('getdeepin-quota.login', loginCommand),
vscode.commands.registerCommand('getdeepin-quota.logout', logoutCommand),
vscode.commands.registerCommand('getdeepin-quota.refresh', refreshQuota),
vscode.commands.registerCommand('getdeepin-quota.showPopup', showPopup)
);
statusBar = new StatusBarManager(
async () => {
await refreshQuota();
return currentQuota;
}
);
context.subscriptions.push(statusBar);
const autoLoggedIn = await authService.autoLogin(); const autoLoggedIn = await authService.autoLogin();
if (autoLoggedIn) { if (autoLoggedIn) {
@@ -24,14 +36,6 @@ export async function activate(context: vscode.ExtensionContext) {
} else { } else {
statusBar.updateNotLoggedIn(); statusBar.updateNotLoggedIn();
} }
context.subscriptions.push(
vscode.commands.registerCommand('getdeepin-quota.login', loginCommand),
vscode.commands.registerCommand('getdeepin-quota.logout', logoutCommand),
vscode.commands.registerCommand('getdeepin-quota.refresh', refreshQuota),
vscode.commands.registerCommand('getdeepin-quota.showPopup', showPopup),
statusBar
);
} }
async function loginCommand(): Promise<void> { async function loginCommand(): Promise<void> {
+184 -38
View File
@@ -3,10 +3,11 @@ import { QuotaResponse, QuotaDisplay, QUOTA_LIMITS } from './types';
export class StatusBarManager { export class StatusBarManager {
private statusBarItem: vscode.StatusBarItem; private statusBarItem: vscode.StatusBarItem;
private onDidClick: () => void; private currentPanel: vscode.WebviewPanel | undefined;
private onRefresh: () => Promise<QuotaDisplay | null>;
constructor(onDidClick: () => void) { constructor(onRefresh: () => Promise<QuotaDisplay | null>) {
this.onDidClick = onDidClick; this.onRefresh = onRefresh;
this.statusBarItem = vscode.window.createStatusBarItem( this.statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right, vscode.StatusBarAlignment.Right,
100 100
@@ -80,7 +81,7 @@ export class StatusBarManager {
return; return;
} }
this.showQuotaQuickPick(data); this.showQuotaPanel(data);
} }
private showLoginPrompt(): void { private showLoginPrompt(): void {
@@ -88,7 +89,7 @@ export class StatusBarManager {
[{ label: '$(sign-in) 登录', id: 'login' }], [{ label: '$(sign-in) 登录', id: 'login' }],
{ placeHolder: '请先登录 GetDeepin AI' } { placeHolder: '请先登录 GetDeepin AI' }
).then(selection => { ).then(selection => {
if (selection?.id === 'login') { if ((selection as any)?.id === 'login') {
vscode.commands.executeCommand('getdeepin-quota.login'); vscode.commands.executeCommand('getdeepin-quota.login');
} }
}); });
@@ -102,49 +103,194 @@ export class StatusBarManager {
], ],
{ placeHolder: '获取用量数据失败' } { placeHolder: '获取用量数据失败' }
).then(selection => { ).then(selection => {
if (selection?.id === 'refresh') { const item = selection as any;
if (item?.id === 'refresh') {
vscode.commands.executeCommand('getdeepin-quota.refresh'); vscode.commands.executeCommand('getdeepin-quota.refresh');
} else if (selection?.id === 'login') { } else if (item?.id === 'login') {
vscode.commands.executeCommand('getdeepin-quota.login'); vscode.commands.executeCommand('getdeepin-quota.login');
} }
}); });
} }
private showQuotaQuickPick(data: QuotaDisplay): void { 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 {
const formatNumber = (n: number) => n.toLocaleString('zh-CN'); const formatNumber = (n: number) => n.toLocaleString('zh-CN');
const formatPercent = (n: number) => n.toFixed(1); const formatPercent = (n: number) => n.toFixed(1);
const items: vscode.QuickPickItem[] = [ return `
{ <!DOCTYPE html>
label: `$(account) ${data.user.display_name} (${data.user.job_id})`, <html>
description: data.user.email <head>
}, <meta charset="UTF-8">
{ label: '', kind: vscode.QuickPickItemKind.Separator }, <style>
{ body {
label: `$(globe) 国内模型: ${formatPercent(data.domestic.percent)}%`, font-family: var(--vscode-font-family);
description: `已用 ${formatNumber(data.domestic.used)} / ${formatNumber(data.domestic.total)}`, padding: 20px;
detail: data.domestic.models.join(', ') color: var(--vscode-foreground);
}, background-color: var(--vscode-editor-background);
{ }
label: `$(world) 国外模型: ${formatPercent(data.global.percent)}%`, .header {
description: `已用 ${formatNumber(data.global.used)} / ${formatNumber(data.global.total)}`, margin-bottom: 24px;
detail: data.global.models.join(', ') padding-bottom: 16px;
}, border-bottom: 1px solid var(--vscode-panel-border);
{ label: '', kind: vscode.QuickPickItemKind.Separator }, }
{ label: '$(refresh) 刷新', id: 'refresh' } as vscode.QuickPickItem & { id: string }, .header h1 {
{ label: '$(sign-out) 登出', id: 'logout' } as vscode.QuickPickItem & { id: string } 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;
}
.quota-stats {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 12px;
}
.quota-percent {
font-size: 28px;
font-weight: bold;
}
.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;
}
.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>
vscode.window.showQuickPick(items, { <div class="quota-section">
placeHolder: 'AI 模型用量详情' <h2>国内模型用量</h2>
}).then(selection => { <div class="quota-stats">
const item = selection as vscode.QuickPickItem & { id?: string }; <span class="quota-percent ${data.domestic.percent > 80 ? 'warning' : ''}">${formatPercent(data.domestic.percent)}%</span>
if (item?.id === 'refresh') { <span class="quota-details">
vscode.commands.executeCommand('getdeepin-quota.refresh'); 已用 ${formatNumber(data.domestic.used)} / ${formatNumber(data.domestic.total)}
} else if (item?.id === 'logout') { </span>
vscode.commands.executeCommand('getdeepin-quota.logout'); </div>
} <div class="progress-container">
}); <div class="progress-bar ${data.domestic.percent > 80 ? 'progress-warning' : 'progress-ok'}" style="width: ${Math.min(data.domestic.percent, 100)}%;"></div>
</div>
<div class="models-list">
${data.domestic.models.map(m => `<span class="model-tag">${m}</span>`).join('')}
</div>
</div>
<div class="quota-section">
<h2>国外模型用量</h2>
<div class="quota-stats">
<span class="quota-percent ${data.global.percent > 80 ? 'warning' : ''}">${formatPercent(data.global.percent)}%</span>
<span class="quota-details">
已用 ${formatNumber(data.global.used)} / ${formatNumber(data.global.total)}
</span>
</div>
<div class="progress-container">
<div class="progress-bar ${data.global.percent > 80 ? 'progress-warning' : 'progress-ok'}" style="width: ${Math.min(data.global.percent, 100)}%;"></div>
</div>
<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>
`;
} }
dispose(): void { dispose(): void {