设置保存
This commit is contained in:
parent
2c643107ee
commit
05080e2b72
@ -35,7 +35,11 @@ const sendPing = () => {
|
|||||||
|
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
|
// 初始化 css
|
||||||
setDefaultCss();
|
setDefaultCss();
|
||||||
|
|
||||||
|
// 初始化 设置
|
||||||
|
|
||||||
document.addEventListener('click', () => {
|
document.addEventListener('click', () => {
|
||||||
Connection.showPanel = false;
|
Connection.showPanel = false;
|
||||||
});
|
});
|
||||||
|
14
app/src/hook/setting.ts
Normal file
14
app/src/hook/setting.ts
Normal 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();
|
||||||
|
|
||||||
|
}
|
@ -21,12 +21,10 @@
|
|||||||
import { defineComponent, ref } from 'vue';
|
import { defineComponent, ref } from 'vue';
|
||||||
import { languageSetting } from './language';
|
import { languageSetting } from './language';
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
import { globalSetting } from '@/hook/global';
|
|
||||||
|
|
||||||
defineComponent({ name: 'appearance' });
|
defineComponent({ name: 'appearance' });
|
||||||
|
|
||||||
const { t, locale } = useI18n();
|
const { t, locale } = useI18n();
|
||||||
locale.value = globalSetting.language;
|
|
||||||
|
|
||||||
const currentLanguage = ref('简体中文');
|
const currentLanguage = ref('简体中文');
|
||||||
|
|
||||||
|
@ -102,13 +102,3 @@ export function onmodelchange() {
|
|||||||
console.log();
|
console.log();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function save() {
|
|
||||||
console.log();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function load() {
|
|
||||||
console.log();
|
|
||||||
|
|
||||||
}
|
|
@ -2,6 +2,7 @@
|
|||||||
import { VSCodeWebViewLike } from '../adapter';
|
import { VSCodeWebViewLike } from '../adapter';
|
||||||
import { connect, MCPClient, type MCPOptions } from './connect';
|
import { connect, MCPClient, type MCPOptions } from './connect';
|
||||||
import { callTool, getPrompt, listPrompts, listResources, listResourceTemplates, listTools, readResource } from './handler';
|
import { callTool, getPrompt, listPrompts, listResources, listResourceTemplates, listTools, readResource } from './handler';
|
||||||
|
import { settingLoadHandler, settingSaveHandler } from './setting';
|
||||||
import { ping } from './util';
|
import { ping } from './util';
|
||||||
|
|
||||||
|
|
||||||
@ -69,6 +70,14 @@ export function messageController(command: string, data: any, webview: VSCodeWeb
|
|||||||
ping(client, webview);
|
ping(client, webview);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'setting/save':
|
||||||
|
settingSaveHandler(client, data, webview);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'setting/load':
|
||||||
|
settingLoadHandler(client, webview);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
69
test/src/controller/setting.ts
Normal file
69
test/src/controller/setting.ts
Normal 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}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,82 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
import * as os from 'os';
|
||||||
|
|
||||||
function getConfigurationPath() {
|
function getConfigurationPath() {
|
||||||
// 如果是 vscode 插件下,则修改为 ~/.openmcp/config.json
|
// 如果是 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';
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user