diff --git a/README.md b/README.md new file mode 100644 index 0000000..b98bb01 --- /dev/null +++ b/README.md @@ -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: 刷新用量` | 手动刷新用量数据 | diff --git a/src/extension.ts b/src/extension.ts index 1061c89..f68e206 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -14,9 +14,21 @@ export async function activate(context: vscode.ExtensionContext) { storageService = new StorageService(context); authService = new AuthService(storageService); 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(); if (autoLoggedIn) { @@ -24,14 +36,6 @@ export async function activate(context: vscode.ExtensionContext) { } else { 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 { diff --git a/src/statusBar.ts b/src/statusBar.ts index 59b0e1f..34ce9a4 100644 --- a/src/statusBar.ts +++ b/src/statusBar.ts @@ -3,10 +3,11 @@ import { QuotaResponse, QuotaDisplay, QUOTA_LIMITS } from './types'; export class StatusBarManager { private statusBarItem: vscode.StatusBarItem; - private onDidClick: () => void; + private currentPanel: vscode.WebviewPanel | undefined; + private onRefresh: () => Promise; - constructor(onDidClick: () => void) { - this.onDidClick = onDidClick; + constructor(onRefresh: () => Promise) { + this.onRefresh = onRefresh; this.statusBarItem = vscode.window.createStatusBarItem( vscode.StatusBarAlignment.Right, 100 @@ -80,7 +81,7 @@ export class StatusBarManager { return; } - this.showQuotaQuickPick(data); + this.showQuotaPanel(data); } private showLoginPrompt(): void { @@ -88,7 +89,7 @@ export class StatusBarManager { [{ label: '$(sign-in) 登录', id: 'login' }], { placeHolder: '请先登录 GetDeepin AI' } ).then(selection => { - if (selection?.id === 'login') { + if ((selection as any)?.id === 'login') { vscode.commands.executeCommand('getdeepin-quota.login'); } }); @@ -102,49 +103,194 @@ export class StatusBarManager { ], { placeHolder: '获取用量数据失败' } ).then(selection => { - if (selection?.id === 'refresh') { + const item = selection as any; + if (item?.id === 'refresh') { vscode.commands.executeCommand('getdeepin-quota.refresh'); - } else if (selection?.id === 'login') { + } else if (item?.id === '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 formatPercent = (n: number) => n.toFixed(1); - const items: vscode.QuickPickItem[] = [ - { - label: `$(account) ${data.user.display_name} (${data.user.job_id})`, - description: data.user.email - }, - { label: '', kind: vscode.QuickPickItemKind.Separator }, - { - label: `$(globe) 国内模型: ${formatPercent(data.domestic.percent)}%`, - description: `已用 ${formatNumber(data.domestic.used)} / ${formatNumber(data.domestic.total)}`, - detail: data.domestic.models.join(', ') - }, - { - label: `$(world) 国外模型: ${formatPercent(data.global.percent)}%`, - description: `已用 ${formatNumber(data.global.used)} / ${formatNumber(data.global.total)}`, - detail: data.global.models.join(', ') - }, - { label: '', kind: vscode.QuickPickItemKind.Separator }, - { label: '$(refresh) 刷新', id: 'refresh' } as vscode.QuickPickItem & { id: string }, - { label: '$(sign-out) 登出', id: 'logout' } as vscode.QuickPickItem & { id: string } - ]; + return ` + + + + + + + +
+

AI 模型用量统计

+ +
- vscode.window.showQuickPick(items, { - placeHolder: 'AI 模型用量详情' - }).then(selection => { - const item = selection as vscode.QuickPickItem & { id?: string }; - if (item?.id === 'refresh') { - vscode.commands.executeCommand('getdeepin-quota.refresh'); - } else if (item?.id === 'logout') { - vscode.commands.executeCommand('getdeepin-quota.logout'); - } - }); +
+

国内模型用量

+
+ ${formatPercent(data.domestic.percent)}% + + 已用 ${formatNumber(data.domestic.used)} / ${formatNumber(data.domestic.total)} + +
+
+
+
+
+ ${data.domestic.models.map(m => `${m}`).join('')} +
+
+ +
+

国外模型用量

+
+ ${formatPercent(data.global.percent)}% + + 已用 ${formatNumber(data.global.used)} / ${formatNumber(data.global.total)} + +
+
+
+
+
+ ${data.global.models.map(m => `${m}`).join('')} +
+
+ +
+ +
+ + + + + `; } dispose(): void {