adapt to new api

This commit is contained in:
blumia committed 2026-04-27 10:03:22 +08:00
1 parent 35da5bb8a0
commit 9f77975766
5 files changed
+239 -50

No files matched your search

+1
View File
@@ -0,0 +1 @@
著作权归 UnionTech 所有,此文件仅供让 vsce package 高兴
+111
View File
@@ -0,0 +1,111 @@
# VS Code 大模型用量插件开发计划
## 项目概述
开发一个 VS Code 扩展,在状态栏显示用户的大模型资源用量情况。
## 技术栈
- TypeScript
- VS Code Extension API
- Node.js fetch API(内置,无需额外依赖)
- VS Code Secret Storage(安全存储密码)
## 功能需求
### 1. 状态栏图标
- 在 VS Code 右下角状态栏显示一个图标
- 显示当前用量概览(如:国内 96% / 国外 100%)
### 2. Popup 弹窗
- 点击或悬停状态栏图标时显示
- 展示详细的用量信息:
- 用户信息(工号、姓名、邮箱)
- 国内模型用量(配额 30,000,000)
- 国外模型用量(配额 10,000,000)
- 支持的模型列表
- 包含刷新按钮
### 3. 登录功能
- 首次使用/未登录时,点击状态栏弹出登录表单
- 登录表单字段:
- username(用户名)
- password(密码)
- remember_me(自动勾选)
- next_path(固定值 `/`)
- 登录成功后保存 cookie 和用户名密码(用于自动重登录)
### 4. 数据获取
- 接口地址:`https://ai.getdeepin.org/api/me/quota`
- 手动刷新模式:每次打开 popup 时请求最新数据
- 自动重登录:会话过期时使用保存的凭据重新登录
## 项目结构
```
vscode-getdeepin-quota/
├── src/
│ ├── extension.ts # 扩展入口
│ ├── statusBar.ts # 状态栏管理
│ ├── quotaService.ts # API 请求服务
│ ├── authService.ts # 登录认证服务
│ ├── storageService.ts # 数据存储服务
│ └── types.ts # 类型定义
├── package.json # 扩展配置
├── tsconfig.json # TypeScript 配置
└── README.md
```
## 开发步骤
### Phase 1: 项目初始化
- [x] 初始化 VS Code 扩展项目
- [x] 配置 TypeScript
- [x] 配置依赖
### Phase 2: 基础架构
- [x] 定义类型接口(QuotaResponse, User, Group 等)
- [x] 实现 StorageService(使用 VS Code Secret Storage)
- [x] 实现 AuthService(登录、cookie 管理、自动重登录)
- [x] 实现 QuotaService(获取用量数据)
### Phase 3: UI 实现
- [x] 实现状态栏图标显示
- [x] 实现 Popup 内容(使用 Webview)
- [x] 实现登录表单(使用 VS Code InputBox)
### Phase 4: 交互逻辑
- [x] 状态栏点击事件处理
- [x] 未登录状态处理
- [x] 会话过期自动重登录
- [x] 手动刷新功能
### Phase 5: 测试与发布
- [ ] 本地测试
- [ ] 打包发布
## API 接口说明
### 登录接口
- URL: `POST https://ai.getdeepin.org/login/form`
- Content-Type: `application/x-www-form-urlencoded`
- 请求体:
```
next_path=/&username=xxx&password=xxx&remember_me=on
```
- 响应: 重定向或设置 cookie
### 用量查询接口
- URL: `GET https://ai.getdeepin.org/api/me/quota`
- 认证: 携带登录时获取的 cookie
- 响应: JSON 格式用量数据
## 配额说明
| 类型 | 总配额 | Key |
|------|--------|-----|
| 国内模型用量 | 30,000,000 | domestic |
| 国外模型用量 | 10,000,000 | global |
用量百分比计算:`已用量 = 总配额 - remaining_tokens`
使用率:`已用量 / 总配额 * 100%`
+2 -1
View File
@@ -2,8 +2,9 @@
"name": "getdeepin-quota",
"displayName": "GetDeepin AI Quota",
"description": "显示大模型资源用量情况",
"version": "0.0.1",
"version": "0.0.2",
"publisher": "getdeepin",
"repository": "https://gitlabwh.uniontech.com/wangzichong/vscode-getdeepin-quota",
"engines": {
"vscode": "^1.85.0"
},
+78 -36
View File
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { QuotaResponse, QuotaDisplay, QUOTA_LIMITS } from './types';
import { QuotaResponse, QuotaDisplay } from './types';
export class StatusBarManager {
private statusBarItem: vscode.StatusBarItem;
@@ -28,8 +28,8 @@ export class StatusBarManager {
}
updateWithQuota(data: QuotaDisplay): void {
const domesticPercent = Math.round(data.domestic.percent);
const globalPercent = Math.round(data.global.percent);
const domesticPercent = Math.round(data.domestic.daily.percent);
const globalPercent = Math.round(data.global.monthly.percent);
this.statusBarItem.text = `$(cloud) 国内 ${domesticPercent}% | 国外 ${globalPercent}%`;
this.statusBarItem.tooltip = this.generateTooltipMarkdown(data);
@@ -52,16 +52,19 @@ export class StatusBarManager {
md.appendMarkdown(`${data.user.email}\n\n`);
md.appendMarkdown('---\n\n');
const domesticWarning = data.domestic.percent > 80 ? '⚠️ ' : '';
md.appendMarkdown(`#### ${domesticWarning}国内模型用量\n\n`);
md.appendMarkdown(`\`${getProgressBar(data.domestic.percent)}\` \`${formatPercent(data.domestic.percent)}%\`\n\n`);
md.appendMarkdown(`已用 **${formatNumber(data.domestic.used)}** / ${formatNumber(data.domestic.total)}\n\n`);
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`);
}
md.appendMarkdown(`支持模型: ${data.domestic.models.map(m => `\`${m}\``).join(' ')}\n\n`);
const globalWarning = data.global.percent > 80 ? '⚠️ ' : '';
md.appendMarkdown(`#### ${globalWarning}国外模型用量\n\n`);
md.appendMarkdown(`\`${getProgressBar(data.global.percent)}\` \`${formatPercent(data.global.percent)}%\`\n\n`);
md.appendMarkdown(`已用 **${formatNumber(data.global.used)}** / ${formatNumber(data.global.total)}\n\n`);
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`);
md.appendMarkdown(`支持模型: ${data.global.models.map(m => `\`${m}\``).join(' ')}\n\n`);
md.appendMarkdown('---\n\n');
@@ -178,15 +181,20 @@ export class StatusBarManager {
margin: 0 0 12px 0;
font-size: 16px;
}
.quota-stats {
.quota-row {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 12px;
margin-bottom: 8px;
}
.quota-label {
min-width: 60px;
color: var(--vscode-descriptionForeground);
}
.quota-percent {
font-size: 28px;
font-size: 20px;
font-weight: bold;
min-width: 70px;
}
.quota-details {
color: var(--vscode-descriptionForeground);
@@ -208,6 +216,7 @@ export class StatusBarManager {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 12px;
}
.model-tag {
padding: 4px 8px;
@@ -247,32 +256,54 @@ export class StatusBarManager {
</div>
<div class="quota-section">
<h2>国内模型用量</h2>
<div class="quota-stats">
<span class="quota-percent ${data.domestic.percent > 80 ? 'warning' : ''}">${formatPercent(data.domestic.percent)}%</span>
<span class="quota-details">
已用 ${formatNumber(data.domestic.used)} / ${formatNumber(data.domestic.total)}
</span>
<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>
</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 class="progress-bar ${data.domestic.daily.percent > 80 ? 'progress-warning' : 'progress-ok'}" style="width: ${Math.min(data.domestic.daily.percent, 100)}%;"></div>
</div>
` : ''}
${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>
` : ''}
<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>
<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>
</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 class="progress-bar ${data.global.daily.percent > 80 ? 'progress-warning' : 'progress-ok'}" style="width: ${Math.min(data.global.daily.percent, 100)}%;"></div>
</div>
` : ''}
${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>
` : ''}
<div class="models-list">
${data.global.models.map(m => `<span class="model-tag">${m}</span>`).join('')}
</div>
@@ -302,19 +333,30 @@ export function parseQuotaResponse(response: QuotaResponse): QuotaDisplay {
const domestic = response.groups.find(g => g.key === 'domestic')!;
const global = response.groups.find(g => g.key === 'global')!;
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 };
};
return {
user: response.user,
domestic: {
used: QUOTA_LIMITS.domestic - domestic.remaining_tokens,
total: QUOTA_LIMITS.domestic,
percent: ((QUOTA_LIMITS.domestic - domestic.remaining_tokens) / QUOTA_LIMITS.domestic) * 100,
models: domestic.models
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)
},
global: {
used: QUOTA_LIMITS.global - global.remaining_tokens,
total: QUOTA_LIMITS.global,
percent: ((QUOTA_LIMITS.global - global.remaining_tokens) / QUOTA_LIMITS.global) * 100,
models: global.models
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)
}
};
}
+47 -13
View File
@@ -4,12 +4,31 @@ export interface User {
email: string;
}
export interface Usage {
total_tokens: number;
daily_amount: string;
month_amount: string;
daily_amount_available: boolean;
month_amount_available: boolean;
unpriced_models: string[];
}
export interface Group {
key: 'domestic' | 'global';
label: string;
query_model: string;
models: string[];
remaining_tokens: number;
default_daily_amount: string;
effective_daily_amount: string;
effective_monthly_amount: string;
monthly_limit_amount: string;
has_daily_limit: boolean;
has_monthly_limit: boolean;
remaining_daily_amount: string;
remaining_monthly_amount: string;
remaining_daily_ratio: number | null;
remaining_monthly_ratio: number;
remaining_ratio: number | null;
usage: Usage;
}
export interface QuotaResponse {
@@ -25,20 +44,35 @@ export interface Credentials {
export interface QuotaDisplay {
user: User;
domestic: {
used: number;
total: number;
percent: number;
label: string;
models: string[];
hasDailyLimit: boolean;
hasMonthlyLimit: boolean;
daily: {
used: number;
total: number;
percent: number;
};
monthly: {
used: number;
total: number;
percent: number;
};
};
global: {
used: number;
total: number;
percent: number;
label: string;
models: string[];
hasDailyLimit: boolean;
hasMonthlyLimit: boolean;
daily: {
used: number;
total: number;
percent: number;
};
monthly: {
used: number;
total: number;
percent: number;
};
};
}
export const QUOTA_LIMITS = {
domestic: 30000000,
global: 10000000
} as const;