feat:实现Windows系统通知功能

- 集成node-notifier实现跨平台系统通知
   - AI响应完成时自动弹出Windows Toast通知
   - 支持通知防抖机制,避免频繁弹窗
   - 添加通知配置项:启用/禁用、声音、超时时间
   - 移除VS Code内置弹窗,仅在系统通知失败时作为备用
This commit is contained in:
Roe-xin
2026-01-26 22:44:17 +08:00
parent 9296b10150
commit 885e2cef75
7 changed files with 1339 additions and 2 deletions

View File

@ -8,10 +8,15 @@ import { VCDFileServer } from "./services/vcdFileServer";
import { initUserService } from "./services/userService";
import { initCreditsService } from "./services/creditsService";
import { isTokenExpired } from "./utils/jwtUtils";
import { NotificationService } from "./services/notificationService";
export async function activate(context: vscode.ExtensionContext) {
console.log("🎉 IC Coder 插件已激活!");
// 初始化通知服务
const notificationService = NotificationService.getInstance(context);
console.log('[Extension] 通知服务已初始化');
// 【关键】在创建 AuthProvider 之前,先检查并清除过期的 session
const storedSessions = context.globalState.get<any[]>('icCoderSessions', []);
console.log('[Extension] 检查 sessions 数量:', storedSessions.length);
@ -191,6 +196,28 @@ export async function activate(context: vscode.ExtensionContext) {
}
);
// 注册命令:测试系统通知
const testNotificationCommand = vscode.commands.registerCommand(
"ic-coder.testNotification",
() => {
console.log('[Extension] ========== 测试通知命令被调用 ==========');
// 先显示 VS Code 通知确认命令执行
vscode.window.showInformationMessage('正在测试系统通知...');
// 发送系统通知
notificationService.success(
'IC Coder - 测试通知',
'系统通知功能正常工作!',
() => {
vscode.window.showInformationMessage('您点击了系统通知!');
}
);
console.log('[Extension] 测试通知命令执行完成');
}
);
// 注册命令:查看会话历史
// TODO: 这些命令需要根据新的任务架构重新实现
// 暂时注释掉,等待重新实现
@ -256,6 +283,7 @@ export async function activate(context: vscode.ExtensionContext) {
openVCDViewerInBrowserCommand,
loginCommand,
logoutCommand,
testNotificationCommand,
// TODO: 等待重新实现这些命令
// viewHistoryCommand,
// newSessionCommand,

View File

@ -0,0 +1,257 @@
import * as vscode from 'vscode';
import * as path from 'path';
// 使用 require 导入 node-notifier
const notifier = require('node-notifier');
/**
* 通知类型枚举
*/
export enum NotificationType {
INFO = 'info',
SUCCESS = 'success',
WARNING = 'warning',
ERROR = 'error'
}
/**
* 通知选项接口
*/
export interface NotificationOptions {
/** 通知标题 */
title: string;
/** 通知消息 */
message: string;
/** 通知类型 */
type?: NotificationType;
/** 是否播放声音 */
sound?: boolean;
/** 超时时间0 表示不自动消失 */
timeout?: number;
/** 自定义图标路径 */
icon?: string;
/** 点击通知时的回调 */
onClick?: () => void;
}
/**
* 系统通知服务类
*/
export class NotificationService {
private static instance: NotificationService;
private readonly extensionPath: string;
private readonly iconPath: string;
private lastNotificationTime: Map<string, number> = new Map();
private readonly DEBOUNCE_INTERVAL = 3000; // 3 秒防抖
private constructor(context: vscode.ExtensionContext) {
this.extensionPath = context.extensionPath;
this.iconPath = path.join(this.extensionPath, 'media', 'icon.png');
console.log('[NotificationService] 初始化通知服务');
console.log('[NotificationService] 扩展路径:', this.extensionPath);
console.log('[NotificationService] 图标路径:', this.iconPath);
}
/**
* 获取单例实例
*/
public static getInstance(context?: vscode.ExtensionContext): NotificationService {
if (!NotificationService.instance && context) {
NotificationService.instance = new NotificationService(context);
}
return NotificationService.instance;
}
/**
* 检查是否启用系统通知
*/
private isSystemNotificationEnabled(): boolean {
const config = vscode.workspace.getConfiguration('ic-coder');
return config.get<boolean>('enableSystemNotification', true);
}
/**
* 检查是否应该发送通知(防抖)
*/
private shouldSendNotification(key: string): boolean {
const now = Date.now();
const lastTime = this.lastNotificationTime.get(key) || 0;
if (now - lastTime < this.DEBOUNCE_INTERVAL) {
return false;
}
this.lastNotificationTime.set(key, now);
return true;
}
/**
* 发送系统通知
*/
public sendNotification(options: NotificationOptions): void {
console.log('[NotificationService] ========== 开始发送通知 ==========');
console.log('[NotificationService] 通知选项:', options);
// 检查用户配置
if (!this.isSystemNotificationEnabled()) {
console.log('[NotificationService] 系统通知已禁用');
return;
}
console.log('[NotificationService] 系统通知已启用');
const {
title,
message,
type = NotificationType.INFO,
onClick
} = options;
// 防抖检查
const notificationKey = `${title}-${message}`;
if (!this.shouldSendNotification(notificationKey)) {
console.log('[NotificationService] 通知被防抖机制拦截');
return;
}
console.log('[NotificationService] 通过防抖检查');
// 使用 node-notifier 发送系统通知
console.log('[NotificationService] 使用 node-notifier 发送系统通知');
try {
const notificationConfig: any = {
title: title,
message: message,
sound: true,
wait: false,
timeout: 10,
appID: 'IC Coder'
};
// Windows 特定配置
if (process.platform === 'win32') {
notificationConfig.icon = this.iconPath;
console.log('[NotificationService] Windows 平台,图标路径:', this.iconPath);
}
console.log('[NotificationService] 通知配置:', notificationConfig);
notifier.notify(notificationConfig, (err: any, response: any, metadata: any) => {
if (err) {
console.error('[NotificationService] ❌ node-notifier 失败:', err);
} else {
console.log('[NotificationService] ✅ node-notifier 成功');
console.log('[NotificationService] 响应:', response);
console.log('[NotificationService] 元数据:', metadata);
}
});
if (onClick) {
notifier.on('click', () => {
console.log('[NotificationService] 用户点击了系统通知');
onClick();
});
}
} catch (error) {
console.error('[NotificationService] ❌ node-notifier 异常:', error);
// 如果系统通知失败,显示 VS Code 内置通知作为备用
console.log('[NotificationService] 系统通知失败,显示 VS Code 内置通知');
this.showVSCodeNotification(title, message, type, onClick);
}
}
/**
* 显示 VS Code 内置通知
*/
private showVSCodeNotification(
title: string,
message: string,
type: NotificationType,
onClick?: () => void
): void {
const fullMessage = `${title}: ${message}`;
console.log('[NotificationService] 显示 VS Code 通知:', fullMessage);
let notificationPromise: Thenable<string | undefined>;
switch (type) {
case NotificationType.ERROR:
notificationPromise = vscode.window.showErrorMessage(fullMessage, '查看详情');
break;
case NotificationType.WARNING:
notificationPromise = vscode.window.showWarningMessage(fullMessage, '查看详情');
break;
case NotificationType.SUCCESS:
case NotificationType.INFO:
default:
notificationPromise = vscode.window.showInformationMessage(fullMessage, '查看详情');
break;
}
// 处理点击事件
if (onClick) {
notificationPromise.then((selection) => {
if (selection === '查看详情') {
console.log('[NotificationService] 用户点击了通知');
onClick();
}
});
}
}
/**
* 发送成功通知
*/
public success(title: string, message: string, onClick?: () => void): void {
this.sendNotification({
title,
message,
type: NotificationType.SUCCESS,
sound: true,
timeout: 10,
onClick
});
}
/**
* 发送错误通知
*/
public error(title: string, message: string, onClick?: () => void): void {
this.sendNotification({
title,
message,
type: NotificationType.ERROR,
sound: true,
timeout: 15,
onClick
});
}
/**
* 发送警告通知
*/
public warning(title: string, message: string, onClick?: () => void): void {
this.sendNotification({
title,
message,
type: NotificationType.WARNING,
sound: true,
timeout: 10,
onClick
});
}
/**
* 发送信息通知
*/
public info(title: string, message: string, onClick?: () => void): void {
this.sendNotification({
title,
message,
type: NotificationType.INFO,
sound: false,
timeout: 8,
onClick
});
}
}

View File

@ -24,6 +24,7 @@ import {
fetchBalance,
} from "../services/creditsService";
import { optimizePrompt } from "../services/promptOptimizeService";
import { NotificationService } from "../services/notificationService";
import type { RunMode, ServiceTier } from "../types/api";
@ -333,6 +334,17 @@ async function handleUserMessageWithBackend(
isComplete: true,
});
console.log("[MessageHandler] postMessage 返回值:", result);
// 发送系统通知 - AI 响应完成
const notificationService = NotificationService.getInstance();
notificationService.success(
'IC Coder - AI 响应完成',
'您的问题已得到回复,点击查看详情',
() => {
// 点击通知时聚焦到面板
panel.reveal();
}
);
} catch (error) {
console.warn("[MessageHandler] 更新面板失败(面板可能已关闭):", error);
}
@ -845,6 +857,16 @@ export async function handleCreateFile(
message: " 文件创建成功",
});
vscode.window.showInformationMessage(`文件创建成功: ${filePath}`);
// 发送系统通知
const notificationService = NotificationService.getInstance();
notificationService.success(
'IC Coder - 文件创建',
`文件已创建: ${path.basename(filePath)}`,
() => {
vscode.commands.executeCommand('vscode.open', vscode.Uri.file(filePath));
}
);
} catch (error) {
panel.webview.postMessage({
command: "fileCreateError",
@ -872,6 +894,13 @@ export async function handleUpdateFile(
message: " 文件更新成功",
});
vscode.window.showInformationMessage(`文件更新成功: ${filePath}`);
// 发送系统通知
const notificationService = NotificationService.getInstance();
notificationService.info(
'IC Coder - 文件更新',
`文件已更新: ${path.basename(filePath)}`
);
} catch (error) {
panel.webview.postMessage({
command: "fileUpdateError",
@ -1079,6 +1108,17 @@ async function handleVCDGeneration(
});
vscode.window.showInformationMessage(`VCD 文件生成成功: ${fileName}`);
// 发送系统通知
const notificationService = NotificationService.getInstance();
notificationService.success(
'IC Coder - 仿真完成',
`VCD 文件已生成: ${fileName}`,
() => {
// 点击通知时打开 VCD 查看器
vscode.commands.executeCommand('ic-coder.openVCDViewer', result.vcdFilePath);
}
);
} else {
panel.webview.postMessage({
command: "receiveMessage",
@ -1102,6 +1142,17 @@ async function handleVCDGeneration(
});
vscode.window.showErrorMessage("VCD 文件生成失败");
// 发送系统通知
const notificationService = NotificationService.getInstance();
notificationService.error(
'IC Coder - 仿真失败',
'VCD 文件生成失败,请查看错误信息',
() => {
// 点击通知时聚焦到面板
panel.reveal();
}
);
}
} catch (error) {
const errorMsg = `❌ 生成 VCD 文件时出错: ${
@ -1114,6 +1165,16 @@ async function handleVCDGeneration(
});
vscode.window.showErrorMessage(errorMsg);
// 发送系统通知
const notificationService = NotificationService.getInstance();
notificationService.error(
'IC Coder - 仿真错误',
error instanceof Error ? error.message : '生成 VCD 文件时出错',
() => {
panel.reveal();
}
);
}
}