- 将 1346 行的单文件拆分为 7 个职责单一的模块 - authHelper: 认证和登录检查 - userInfoHelper: 用户信息管理 - conversationHelper: 会话历史加载 - vcdHelper: VCD 文件处理 - contextHelper: 上下文管理 - fileHelper: 文件操作 - messageRouter: 消息路由分发 - 主组件精简至 157 行,提升可维护性
157 lines
3.8 KiB
TypeScript
157 lines
3.8 KiB
TypeScript
/**
|
||
* IC 助手面板主组件
|
||
* 功能:创建和管理 IC Coder 聊天面板
|
||
* 依赖:vscode, webviewContent, helpers
|
||
* 使用场景:用户打开 IC Coder 聊天界面
|
||
*/
|
||
import * as vscode from "vscode";
|
||
import { getWebviewContent } from "../views/webviewContent";
|
||
import { ChatHistoryManager } from "../utils/chatHistoryManager";
|
||
import { checkAuthAndPromptLogin } from "./helpers/authHelper";
|
||
import {
|
||
sendUserInfoToWebview,
|
||
setupBalanceUpdateCallback,
|
||
} from "./helpers/userInfoHelper";
|
||
import { handleWebviewMessage } from "./helpers/messageRouter";
|
||
|
||
function getIconUris(
|
||
webview: vscode.Webview,
|
||
context: vscode.ExtensionContext,
|
||
) {
|
||
return {
|
||
icon: webview.asWebviewUri(
|
||
vscode.Uri.joinPath(context.extensionUri, "media", "icon.png"),
|
||
),
|
||
auto: webview.asWebviewUri(
|
||
vscode.Uri.joinPath(
|
||
context.extensionUri,
|
||
"dist",
|
||
"assets",
|
||
"model",
|
||
"Auto.png",
|
||
),
|
||
),
|
||
lite: webview.asWebviewUri(
|
||
vscode.Uri.joinPath(
|
||
context.extensionUri,
|
||
"dist",
|
||
"assets",
|
||
"model",
|
||
"lite.png",
|
||
),
|
||
),
|
||
sy: webview.asWebviewUri(
|
||
vscode.Uri.joinPath(
|
||
context.extensionUri,
|
||
"dist",
|
||
"assets",
|
||
"model",
|
||
"Sy.png",
|
||
),
|
||
),
|
||
max: webview.asWebviewUri(
|
||
vscode.Uri.joinPath(
|
||
context.extensionUri,
|
||
"dist",
|
||
"assets",
|
||
"model",
|
||
"Max.png",
|
||
),
|
||
),
|
||
qrCode: webview.asWebviewUri(
|
||
vscode.Uri.joinPath(
|
||
context.extensionUri,
|
||
"dist",
|
||
"assets",
|
||
"QRCode",
|
||
"wx.png",
|
||
),
|
||
),
|
||
logo: webview.asWebviewUri(
|
||
vscode.Uri.joinPath(context.extensionUri, "media", "homepage-logo.png"),
|
||
),
|
||
};
|
||
}
|
||
|
||
export async function showICHelperPanel(
|
||
context: vscode.ExtensionContext,
|
||
viewColumn?: vscode.ViewColumn,
|
||
) {
|
||
if (!(await checkAuthAndPromptLogin(context))) {
|
||
return;
|
||
}
|
||
|
||
const panel = vscode.window.createWebviewPanel(
|
||
"icCoder",
|
||
"IC Coder",
|
||
viewColumn || vscode.ViewColumn.Beside,
|
||
{
|
||
enableScripts: true,
|
||
retainContextWhenHidden: true,
|
||
localResourceRoots: [
|
||
vscode.Uri.joinPath(context.extensionUri, "media"),
|
||
vscode.Uri.joinPath(context.extensionUri, "dist", "assets"),
|
||
],
|
||
},
|
||
);
|
||
|
||
(global as any).currentICHelperPanel = panel;
|
||
|
||
const panelId = `panel_${Date.now()}_${Math.random()
|
||
.toString(36)
|
||
.substr(2, 9)}`;
|
||
(panel as any).__uniqueId = panelId;
|
||
(panel as any).__context = context;
|
||
|
||
panel.iconPath = vscode.Uri.joinPath(
|
||
context.extensionUri,
|
||
"media",
|
||
"icon.png",
|
||
);
|
||
|
||
const icons = getIconUris(panel.webview, context);
|
||
panel.webview.html = getWebviewContent(
|
||
icons.icon.toString(),
|
||
icons.auto.toString(),
|
||
icons.lite.toString(),
|
||
icons.sy.toString(),
|
||
icons.max.toString(),
|
||
icons.qrCode.toString(),
|
||
icons.logo.toString(),
|
||
);
|
||
|
||
await sendUserInfoToWebview(panel, context);
|
||
setupBalanceUpdateCallback(panel, context);
|
||
|
||
const pendingMessage = context.globalState.get("pendingMessage") as any;
|
||
if (pendingMessage) {
|
||
await context.globalState.update("pendingMessage", undefined);
|
||
setTimeout(() => {
|
||
panel.webview.postMessage({
|
||
command: "autoSendMessage",
|
||
text: pendingMessage.text,
|
||
mode: pendingMessage.mode,
|
||
serviceTier: pendingMessage.serviceTier,
|
||
});
|
||
}, 500);
|
||
}
|
||
|
||
panel.webview.onDidReceiveMessage(
|
||
async (message) => {
|
||
await handleWebviewMessage(message, panel, context);
|
||
},
|
||
undefined,
|
||
context.subscriptions,
|
||
);
|
||
|
||
panel.onDidDispose(
|
||
() => {
|
||
const historyManager = ChatHistoryManager.getInstance();
|
||
const panelId = (panel as any).__uniqueId;
|
||
historyManager.removePanelTask(panelId);
|
||
},
|
||
undefined,
|
||
context.subscriptions,
|
||
);
|
||
}
|