111 lines
3.2 KiB
TypeScript
111 lines
3.2 KiB
TypeScript
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已由扩展初始化,直接使用');
|
||
window.__VSCODE_API_INITIALIZED__ = true;
|
||
} else {
|
||
console.warn('main.ts: VSCode API未在window中找到');
|
||
window.__VSCODE_API_INITIALIZED__ = false;
|
||
|
||
// 处理情况:如果在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');
|
||
}
|
||
}
|
||
|
||
// 每5秒检查一次VSCode API状态(调试用)
|
||
setInterval(() => {
|
||
console.log('VSCode API状态:', window.vscodeApi ? '可用' : '不可用');
|
||
}, 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');
|