feat: 实现发送消息前余额检测

- creditsService.ts: 新增余额缓存和检测服务
- apiClient.ts: 新增 getCreditBalance() API 调用
- dialogService.ts: SSE credit_update 事件更新余额缓存
- messageHandler.ts: 发送消息前检测余额,低于5点阻止发送
This commit is contained in:
XiaoFeng
2026-01-10 21:45:41 +08:00
parent 52e4522ed0
commit bdc55c727a
4 changed files with 165 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import { ChatHistoryManager } from "./chatHistoryManager";
import { dialogManager, DialogSession } from "../services/dialogService";
import { userInteractionManager } from "../services/userInteraction";
import { healthCheck } from "../services/apiClient";
import { checkBalanceBeforeSend } from "../services/creditsService";
import type { RunMode, ServiceTier } from "../types/api";
@ -90,6 +91,27 @@ export async function handleUserMessage(
return;
}
// 发送前检测余额
const balanceCheck = await checkBalanceBeforeSend();
if (!balanceCheck.allowed) {
console.warn("[MessageHandler] 余额不足,阻止发送:", balanceCheck.message);
// 显示错误提示
const selection = await vscode.window.showWarningMessage(
balanceCheck.message || "资源点余额不足",
"去充值"
);
if (selection === "去充值") {
vscode.env.openExternal(vscode.Uri.parse("https://iccoder.com/recharge"));
}
// 恢复输入状态
panel.webview.postMessage({
command: "updateSegments",
segments: [],
isComplete: true,
});
return;
}
// 尝试使用后端服务
if (useBackendService && extensionPath) {
try {