设置保存

This commit is contained in:
锦恢 2025-04-05 14:45:04 +08:00
parent 2c643107ee
commit 05080e2b72
7 changed files with 170 additions and 16 deletions

View File

@ -35,7 +35,11 @@ const sendPing = () => {
onMounted(() => {
// css
setDefaultCss();
//
document.addEventListener('click', () => {
Connection.showPanel = false;
});

14
app/src/hook/setting.ts Normal file
View File

@ -0,0 +1,14 @@
import { useMessageBridge } from "@/api/message-bridge";
export function loadSetting() {
const bridge = useMessageBridge();
bridge.addCommandListener('setting/load', () => {
}, { once: true });
}
export function saveSetting() {
const bridge = useMessageBridge();
}

View File

@ -21,12 +21,10 @@
import { defineComponent, ref } from 'vue';
import { languageSetting } from './language';
import { useI18n } from 'vue-i18n';
import { globalSetting } from '@/hook/global';
defineComponent({ name: 'appearance' });
const { t, locale } = useI18n();
locale.value = globalSetting.language;
const currentLanguage = ref('简体中文');

View File

@ -101,14 +101,4 @@ export const llmManager = reactive({
export function onmodelchange() {
console.log();
}
export async function save() {
console.log();
}
export async function load() {
console.log();
}

View File

@ -2,6 +2,7 @@
import { VSCodeWebViewLike } from '../adapter';
import { connect, MCPClient, type MCPOptions } from './connect';
import { callTool, getPrompt, listPrompts, listResources, listResourceTemplates, listTools, readResource } from './handler';
import { settingLoadHandler, settingSaveHandler } from './setting';
import { ping } from './util';
@ -68,7 +69,15 @@ export function messageController(command: string, data: any, webview: VSCodeWeb
case 'ping':
ping(client, webview);
break;
case 'setting/save':
settingSaveHandler(client, data, webview);
break;
case 'setting/load':
settingLoadHandler(client, webview);
break;
default:
break;
}

View File

@ -0,0 +1,69 @@
import { VSCodeWebViewLike } from '../adapter';
import { loadConfig, saveConfig } from '../util';
import { MCPClient } from './connect';
export async function settingSaveHandler(client: MCPClient | undefined, data: any, webview: VSCodeWebViewLike) {
if (!client) {
const connectResult = {
code: 501,
msg: 'mcp client 尚未连接'
};
webview.postMessage({ command: 'ping', data: connectResult });
return;
}
try {
// 保存配置
saveConfig(data);
webview.postMessage({
command: 'setting/save',
data: {
code: 200,
msg: 'Settings saved successfully'
}
});
} catch (error) {
webview.postMessage({
command: 'setting/save',
data: {
code: 500,
msg: `Failed to save settings: ${(error as Error).message}`
}
});
}
}
export async function settingLoadHandler(client: MCPClient | undefined, webview: VSCodeWebViewLike) {
if (!client) {
const connectResult = {
code: 501,
msg: 'mcp client 尚未连接'
};
webview.postMessage({ command: 'ping', data: connectResult });
return;
}
try {
// 加载配置
const config = loadConfig();
webview.postMessage({
command: 'setting/load',
data: {
code: 200,
msg: config // 直接返回配置对象
}
});
} catch (error) {
webview.postMessage({
command: 'setting/load',
data: {
code: 500,
msg: `Failed to load settings: ${(error as Error).message}`
}
});
}
}

View File

@ -1,12 +1,82 @@
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
function getConfigurationPath() {
// 如果是 vscode 插件下,则修改为 ~/.openmcp/config.json
if (process.env.VSCODE_PID) {
// 在 VSCode 插件环境下
const homeDir = os.homedir();
const configDir = path.join(homeDir, '.openmcp');
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
return path.join(configDir, 'config.json');
}
return 'config.json';
}
export function loadConfig() {
interface IConfig {
MODEL_BASE_URL: string;
MODEL_NAME: string;
API_TOKEN: string;
[key: string]: any;
}
export function saveConfig() {
const DEFAULT_CONFIG: IConfig = {
MODEL_BASE_URL: '',
MODEL_NAME: '',
API_TOKEN: ''
};
function createConfig(): IConfig {
const configPath = getConfigurationPath();
const configDir = path.dirname(configPath);
// 确保配置目录存在
if (configDir && !fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
// 写入默认配置
fs.writeFileSync(configPath, JSON.stringify(DEFAULT_CONFIG, null, 2), 'utf-8');
return DEFAULT_CONFIG;
}
export function loadConfig(): IConfig {
const configPath = getConfigurationPath();
if (!fs.existsSync(configPath)) {
return createConfig();
}
try {
const configData = fs.readFileSync(configPath, 'utf-8');
return JSON.parse(configData) as IConfig;
} catch (error) {
console.error('Error loading config file, creating new one:', error);
return createConfig();
}
}
export function saveConfig(config: Partial<IConfig>, merge: boolean = true): void {
const configPath = getConfigurationPath();
let currentConfig: IConfig = DEFAULT_CONFIG;
if (merge && fs.existsSync(configPath)) {
try {
currentConfig = loadConfig();
} catch (error) {
console.error('Error loading existing config:', error);
}
}
const newConfig = { ...currentConfig, ...config };
try {
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2), 'utf-8');
} catch (error) {
console.error('Error saving config file:', error);
throw error;
}
}