Files
IC-Coder-Plugin/src/extension.ts
Roe-xin f9c9fa1840 feat(auth): 添加登录状态检查,
- 未登录时不会自动打开面板命令打开也会显示需要登录
- 登录之后就回自动打开对话面板
2025-12-29 18:52:56 +08:00

182 lines
5.3 KiB
TypeScript

import * as vscode from "vscode";
import { ICViewProvider } from "./views/ICViewProvider";
import { showICHelperPanel } from "./panels/ICHelperPanel";
import { VCDViewerPanel } from "./panels/VCDViewerPanel";
import { ChatHistoryManager } from "./utils/chatHistoryManager";
import { ICCoderAuthenticationProvider } from "./services/icCoderAuthProvider";
export function activate(context: vscode.ExtensionContext) {
console.log("🎉 IC Coder 插件已激活!");
// 注册 Authentication Provider
const authProvider = new ICCoderAuthenticationProvider(context);
context.subscriptions.push(
vscode.authentication.registerAuthenticationProvider(
"iccoder",
"IC Coder",
authProvider
)
);
// 检查登录状态,如果已登录则自动打开聊天面板
vscode.authentication.getSession("iccoder", [], { createIfNone: false })
.then((session) => {
if (session) {
vscode.commands.executeCommand("ic-coder.openChat");
}
}, () => {
// 未登录,不做任何操作
});
// 注册命令:打开助手面板
const openPanelCommand = vscode.commands.registerCommand(
"ic-coder.openPanel",
async () => {
await showICHelperPanel(context);
}
);
// 注册命令:打开聊天(用于侧边栏)
const openChatCommand = vscode.commands.registerCommand(
"ic-coder.openChat",
async () => {
await showICHelperPanel(context);
}
);
// 注册命令:打开 VCD 波形查看器
const openVCDViewerCommand = vscode.commands.registerCommand(
"ic-coder.openVCDViewer",
async (vcdFilePath?: string) => {
if (!vcdFilePath) {
// 如果没有提供文件路径,让用户选择 VCD 文件
const fileUri = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
filters: {
"VCD 文件": ["vcd"],
"所有文件": ["*"],
},
title: "选择 VCD 文件",
});
if (fileUri && fileUri[0]) {
vcdFilePath = fileUri[0].fsPath;
} else {
return;
}
}
VCDViewerPanel.createOrShow(context.extensionUri, vcdFilePath);
}
);
// 注册命令:用户登录
const loginCommand = vscode.commands.registerCommand(
"ic-coder.login",
async () => {
try {
await vscode.authentication.getSession("iccoder", [], { createIfNone: true });
} catch (error) {
vscode.window.showErrorMessage(`登录失败: ${error}`);
}
}
);
// 注册命令:用户登出
const logoutCommand = vscode.commands.registerCommand(
"ic-coder.logout",
async () => {
try {
const session = await vscode.authentication.getSession("iccoder", [], { createIfNone: false });
if (session) {
// 通过创建新会话并清除偏好来实现登出
await vscode.authentication.getSession("iccoder", [], {
clearSessionPreference: true,
forceNewSession: true
});
vscode.window.showInformationMessage("已退出登录");
} else {
vscode.window.showInformationMessage("当前未登录");
}
} catch (error) {
vscode.window.showInformationMessage("当前未登录");
}
}
);
// 注册命令:查看会话历史
// TODO: 这些命令需要根据新的任务架构重新实现
// 暂时注释掉,等待重新实现
/*
const viewHistoryCommand = vscode.commands.registerCommand(
"ic-coder.viewHistory",
() => {
vscode.window.showInformationMessage("此功能正在重构中,敬请期待");
}
);
const newSessionCommand = vscode.commands.registerCommand(
"ic-coder.newSession",
() => {
vscode.window.showInformationMessage("此功能正在重构中,敬请期待");
}
);
const exportSessionCommand = vscode.commands.registerCommand(
"ic-coder.exportSession",
() => {
vscode.window.showInformationMessage("此功能正在重构中,敬请期待");
}
);
const deleteSessionCommand = vscode.commands.registerCommand(
"ic-coder.deleteSession",
() => {
vscode.window.showInformationMessage("此功能正在重构中,敬请期待");
}
);
const clearHistoryCommand = vscode.commands.registerCommand(
"ic-coder.clearHistory",
() => {
vscode.window.showInformationMessage("此功能正在重构中,敬请期待");
}
);
const searchSessionCommand = vscode.commands.registerCommand(
"ic-coder.searchSession",
() => {
vscode.window.showInformationMessage("此功能正在重构中,敬请期待");
}
);
*/
// 注册侧边栏视图
const viewProvider = new ICViewProvider(context.extensionUri, context);
const viewRegistration = vscode.window.registerWebviewViewProvider(
"ic-coder.mainView",
viewProvider
);
// 添加到订阅
context.subscriptions.push(
openPanelCommand,
openChatCommand,
openVCDViewerCommand,
loginCommand,
logoutCommand,
// TODO: 等待重新实现这些命令
// viewHistoryCommand,
// newSessionCommand,
// exportSessionCommand,
// deleteSessionCommand,
// clearHistoryCommand,
// searchSessionCommand,
viewRegistration
);
}
export function deactivate() {}