152 lines
4.3 KiB
TypeScript
Raw Normal View History

2025-04-13 14:22:32 +08:00
import { createApp } from 'vue';
import App from './App.vue';
import { createPinia } from 'pinia';
import router from './router';
import Antd from 'ant-design-vue';
import { Bubble, Sender, Conversations, ThoughtChain, Prompts } from 'ant-design-x-vue';
import 'ant-design-vue/dist/reset.css';
import './styles/global.scss';
import './styles/antdx-override.scss';
// 设置全局VSCode API
declare global {
interface Window {
vscodeApi: any;
__VSCODE_API_INITIALIZED__: boolean;
}
}
// VSCode API初始化 - 必须在全局范围获取一次
console.log('main.ts: 初始化VSCode API');
let vscodeGlobal = window.vscodeApi; // 直接使用window.vscodeApi它已经由ChatViewProvider初始化
// 记录API状态
if (vscodeGlobal) {
console.log('main.ts: VSCode API已由扩展初始化直接使用');
2025-04-14 23:22:12 +08:00
// 发送初始化成功消息到VSCode
vscodeGlobal.postMessage({
type: 'log',
message: '[主程序] VSCode API初始化成功WebView已加载完成'
});
2025-04-13 14:22:32 +08:00
window.__VSCODE_API_INITIALIZED__ = true;
} else {
console.warn('main.ts: VSCode API未在window中找到');
window.__VSCODE_API_INITIALIZED__ = false;
2025-04-14 23:22:12 +08:00
// 尝试获取VSCode API
try {
if (typeof acquireVsCodeApi === 'function') {
window.vscodeApi = acquireVsCodeApi();
console.log('main.ts: 成功通过acquireVsCodeApi()获取VSCode API');
// 发送到VSCode
window.vscodeApi.postMessage({
type: 'log',
message: '[主程序] 通过acquireVsCodeApi()成功获取VSCode API'
});
window.__VSCODE_API_INITIALIZED__ = true;
} else {
console.error('main.ts: acquireVsCodeApi不是函数');
}
} catch (err) {
console.error('main.ts: 获取VSCode API失败', err);
}
2025-04-13 14:22:32 +08:00
// 处理情况如果在dev环境中提供一个虚拟的VSCode API
// 警告:这只用于开发,不应该在生产环境使用
if (process.env.NODE_ENV === 'development') {
console.log('main.ts: 创建开发环境下的模拟VSCode API');
window.vscodeApi = {
postMessage: (msg: any) => {
console.log('DEV模式: 模拟VSCode消息', msg);
// 模拟VSCode响应
if (msg.type === 'getSettings') {
setTimeout(() => {
window.dispatchEvent(new MessageEvent('message', {
data: {
type: 'settings',
settings: {
apiHost: 'ws://localhost:8080',
apiKey: 'dev_api_key'
}
}
}));
}, 100);
} else if (msg.type === 'saveSettings') {
setTimeout(() => {
window.dispatchEvent(new MessageEvent('message', {
data: {
type: 'settingsSaved',
success: true
}
}));
}, 100);
}
}
};
console.log('main.ts: 已创建模拟VSCode API');
}
}
2025-04-14 23:22:12 +08:00
// 每5秒检查一次VSCode API状态并通知VSCode仅前3次
let checkCount = 0;
const apiCheckInterval = setInterval(() => {
const isAvailable = !!window.vscodeApi;
console.log('VSCode API状态:', isAvailable ? '可用' : '不可用');
if (isAvailable && window.vscodeApi) {
window.vscodeApi.postMessage({
type: 'log',
message: `[主程序] VSCode API状态检查 #${checkCount+1}: 可用`
});
}
checkCount++;
if (checkCount >= 3) {
clearInterval(apiCheckInterval);
}
2025-04-13 14:22:32 +08:00
}, 5000);
// 创建Vue应用
const app = createApp(App);
// 添加favicon以防止404错误
const link = document.createElement('link');
link.rel = 'icon';
link.href = 'data:,'; // 空数据URL不加载任何图标
document.head.appendChild(link);
// 注册Pinia状态管理
app.use(createPinia());
// 注册Ant Design Vue
app.use(Antd);
// 注册Ant Design X Vue组件
app.component('a-x-bubble', Bubble);
app.component('a-x-sender', Sender);
app.component('a-x-conversations', Conversations);
app.component('a-x-thought-chain', ThoughtChain);
app.component('a-x-prompts', Prompts);
// 注册路由
app.use(router);
// 错误处理
app.config.errorHandler = (err, instance, info) => {
console.error('Vue Error:', err, info);
if (window.vscodeApi) {
window.vscodeApi.postMessage({
type: 'error',
message: err instanceof Error ? err.message : String(err),
info: info
});
}
};
// 挂载应用
app.mount('#app');