support ai-mook

This commit is contained in:
锦恢 2025-06-30 20:29:10 +08:00
commit c366494637
8 changed files with 102 additions and 15 deletions

View File

@ -15,5 +15,6 @@
"join-project": "Participate in the project", "join-project": "Participate in the project",
"comment-plugin": "Comment Plugin", "comment-plugin": "Comment Plugin",
"preset-env-sync": "Preset environment variables synchronized successfully", "preset-env-sync": "Preset environment variables synchronized successfully",
"preset-env-sync.fail": "Failed to sync preset environment variables" "preset-env-sync.fail": "Failed to sync preset environment variables",
"error.notOpenWorkspace": "No workspace is currently open in VSCode. Please open a workspace (e.g., open a folder) first."
} }

View File

@ -15,5 +15,7 @@
"join-project": "プロジェクトに参加する", "join-project": "プロジェクトに参加する",
"comment-plugin": "コメントプラグイン", "comment-plugin": "コメントプラグイン",
"preset-env-sync": "プリセット環境変数の同期が完了しました", "preset-env-sync": "プリセット環境変数の同期が完了しました",
"preset-env-sync.fail": "プリセット環境変数の同期に失敗しました" "preset-env-sync.fail": "プリセット環境変数の同期に失敗しました",
"error.notOpenWorkspace": "現在、VSCode でワークスペースが開かれていません。まずワークスペース(例:フォルダーを開く)を開いてください。"
} }

View File

@ -15,5 +15,6 @@
"join-project": "参与项目", "join-project": "参与项目",
"comment-plugin": "评论插件", "comment-plugin": "评论插件",
"preset-env-sync": "预设环境变量同步完成", "preset-env-sync": "预设环境变量同步完成",
"preset-env-sync.fail": "预设环境变量同步失败" "preset-env-sync.fail": "预设环境变量同步失败",
"error.notOpenWorkspace": "当前VScode没有打开工作区请先打开工作区例如打开文件夹"
} }

View File

@ -1,7 +1,7 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { setRunningCWD, setVscodeWorkspace } from '../openmcp-sdk/service/index.js'; import { setRunningCWD, setVscodeWorkspace } from '../openmcp-sdk/service/index.js';
import { launch } from './common/entry.js'; import { launch } from './common/entry.js';
import { initialiseI18n } from './i18n/index.js'; import { initialiseI18n, getAvailableKeys } from './i18n/index.js';
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
console.log('activate openmcp'); console.log('activate openmcp');
@ -13,6 +13,11 @@ export function activate(context: vscode.ExtensionContext) {
setVscodeWorkspace(workspace); setVscodeWorkspace(workspace);
setRunningCWD(context.extensionPath); setRunningCWD(context.extensionPath);
initialiseI18n(context); initialiseI18n(context);
// 添加i18n调试信息
console.log('Current language:', vscode.env.language);
console.log('Available i18n keys:', getAvailableKeys().length);
launch(context); launch(context);
} }

View File

@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import * as os from 'os'; import * as os from 'os';
import * as fspath from 'path'; import * as fspath from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import { t } from './i18n';
export type FsPath = string; export type FsPath = string;
export const panels = new Map<FsPath, vscode.WebviewPanel>(); export const panels = new Map<FsPath, vscode.WebviewPanel>();
@ -96,7 +97,7 @@ export function getConnectionConfig() {
export function getWorkspaceConnectionConfigPath() { export function getWorkspaceConnectionConfigPath() {
const workspace = getWorkspacePath(); const workspace = getWorkspacePath();
if (!workspace) { if (!workspace) {
throw new Error('No workspace found. Please open a folder in VSCode first.'); return null; // 如果没有工作区,则返回 null
} }
const configDir = fspath.join(workspace, '.openmcp'); const configDir = fspath.join(workspace, '.openmcp');
if (!fs.existsSync(configDir)) { if (!fs.existsSync(configDir)) {
@ -110,14 +111,14 @@ export function getWorkspaceConnectionConfigPath() {
* @description {workspace} * @description {workspace}
* @param workspace * @param workspace
*/ */
export function getWorkspaceConnectionConfig() { export function getWorkspaceConnectionConfig():IConnectionConfig| null {
if (_workspaceConnectionConfig) { if (_workspaceConnectionConfig) {
return _workspaceConnectionConfig; return _workspaceConnectionConfig;
} }
const workspace = getWorkspacePath(); const workspace = getWorkspacePath();
if (!workspace) { if (!workspace) {
throw new Error('No workspace found. Please open a folder in VSCode first.'); return null; // 如果没有工作区,则返回 null
} }
const configDir = fspath.join(workspace, '.openmcp'); const configDir = fspath.join(workspace, '.openmcp');
const connectionConfig = fspath.join(configDir, CONNECTION_CONFIG_NAME); const connectionConfig = fspath.join(configDir, CONNECTION_CONFIG_NAME);
@ -228,6 +229,10 @@ export function updateWorkspaceConnectionConfig(
) { ) {
const connectionItem = getWorkspaceConnectionConfigItemByName(name); const connectionItem = getWorkspaceConnectionConfigItemByName(name);
const workspaceConnectionConfig = getWorkspaceConnectionConfig(); const workspaceConnectionConfig = getWorkspaceConnectionConfig();
if (!workspaceConnectionConfig) {
console.error('没有工作区连接配置文件,请先创建一个工作区连接');
return;
}
data.forEach(item => { data.forEach(item => {
item.cwd = item.cwd?.replace(/\\/g, '/'); item.cwd = item.cwd?.replace(/\\/g, '/');
@ -326,6 +331,10 @@ export function getWorkspacePath() {
export function getWorkspaceConnectionConfigItemByPath(absPath: string) { export function getWorkspaceConnectionConfigItemByPath(absPath: string) {
const workspacePath = getWorkspacePath(); const workspacePath = getWorkspacePath();
const workspaceConnectionConfig = getWorkspaceConnectionConfig(); const workspaceConnectionConfig = getWorkspaceConnectionConfig();
if (!workspaceConnectionConfig) {
return null; // 如果没有工作区连接配置文件,则返回 null
}
const normaliseAbsPath = absPath.replace(/\\/g, '/'); const normaliseAbsPath = absPath.replace(/\\/g, '/');
for (let item of workspaceConnectionConfig.items) { for (let item of workspaceConnectionConfig.items) {
@ -345,9 +354,10 @@ export function getWorkspaceConnectionConfigItemByPath(absPath: string) {
* @param absPath * @param absPath
*/ */
export function getWorkspaceConnectionConfigItemByName(name: string) { export function getWorkspaceConnectionConfigItemByName(name: string) {
const workspacePath = getWorkspacePath();
const workspaceConnectionConfig = getWorkspaceConnectionConfig(); const workspaceConnectionConfig = getWorkspaceConnectionConfig();
if (!workspaceConnectionConfig) {
return null; // 如果没有工作区连接配置文件,则返回 null
}
for (let item of workspaceConnectionConfig.items) { for (let item of workspaceConnectionConfig.items) {
const nItem = Array.isArray(item) ? item[0] : item; const nItem = Array.isArray(item) ? item[0] : item;
if (nItem.name === name) { if (nItem.name === name) {

View File

@ -1,26 +1,81 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path';
const defaultBundle: Record<string, string> = {} const defaultBundle: Record<string, string> = {}
export function initialiseI18n(context: vscode.ExtensionContext) { export function initialiseI18n(context: vscode.ExtensionContext) {
if (vscode.l10n.bundle === undefined) { if (vscode.l10n.bundle === undefined) {
const bundlePath = context.asAbsolutePath('l10n/bundle.l10n.en.json'); // 获取用户的语言设置
const bundle = JSON.parse(fs.readFileSync(bundlePath, { encoding: 'utf-8' })) as Record<string, string>; const userLanguage = vscode.env.language;
Object.assign(defaultBundle, bundle);
// 尝试加载用户语言对应的语言包
let bundlePath = context.asAbsolutePath(`l10n/bundle.l10n.${userLanguage}.json`);
// 如果用户语言的语言包不存在,回退到英语
if (!fs.existsSync(bundlePath)) {
bundlePath = context.asAbsolutePath('l10n/bundle.l10n.en.json');
}
try {
const bundle = JSON.parse(fs.readFileSync(bundlePath, { encoding: 'utf-8' })) as Record<string, string>;
Object.assign(defaultBundle, bundle);
} catch (error) {
console.error('Failed to load i18n bundle:', error);
// 如果加载失败,尝试加载英语包作为最后的回退
try {
const fallbackPath = context.asAbsolutePath('l10n/bundle.l10n.en.json');
const fallbackBundle = JSON.parse(fs.readFileSync(fallbackPath, { encoding: 'utf-8' })) as Record<string, string>;
Object.assign(defaultBundle, fallbackBundle);
} catch (fallbackError) {
console.error('Failed to load fallback i18n bundle:', fallbackError);
}
}
} }
} }
export function t(message: string, ...args: string[]): string { export function t(message: string, ...args: string[]): string {
if (vscode.l10n.bundle === undefined) { if (vscode.l10n.bundle === undefined) {
// 使用自定义的语言包
let translateMessage = defaultBundle[message] || message; let translateMessage = defaultBundle[message] || message;
for (let i = 0; i < args.length; ++ i) { // 替换占位符 {0}, {1}, {2} 等
for (let i = 0; i < args.length; i++) {
translateMessage = translateMessage.replace(`{${i}}`, args[i]); translateMessage = translateMessage.replace(`{${i}}`, args[i]);
} }
return translateMessage; return translateMessage;
} else { } else {
// 使用 VS Code 的 l10n API
return vscode.l10n.t(message, ...args); return vscode.l10n.t(message, ...args);
} }
} }
/**
* 使
*/
export function getCurrentLanguage(): string {
return vscode.env.language;
}
/**
*
*/
export function getAvailableKeys(): string[] {
if (vscode.l10n.bundle === undefined) {
return Object.keys(defaultBundle);
}
return [];
}
/**
*
*/
export function hasTranslation(key: string): boolean {
if (vscode.l10n.bundle === undefined) {
return key in defaultBundle;
}
// VS Code 的 l10n API 没有直接的方法检查,我们尝试翻译并比较
const translated = vscode.l10n.t(key);
return translated !== key;
}

View File

@ -4,6 +4,7 @@ import { getWorkspaceConnectionConfig, getWorkspaceConnectionConfigPath, getWork
import { ConnectionViewItem } from './common.js'; import { ConnectionViewItem } from './common.js';
import { revealOpenMcpWebviewPanel } from '../webview/webview.service.js'; import { revealOpenMcpWebviewPanel } from '../webview/webview.service.js';
import { acquireUserCustomConnection, deleteUserConnection } from './workspace.service.js'; import { acquireUserCustomConnection, deleteUserConnection } from './workspace.service.js';
import { t } from '../i18n/index.js';
@RegisterTreeDataProvider('openmcp.sidebar.workspace-connection') @RegisterTreeDataProvider('openmcp.sidebar.workspace-connection')
export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<ConnectionViewItem> { export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<ConnectionViewItem> {
@ -60,14 +61,18 @@ export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<Conn
@RegisterCommand('addConnection') @RegisterCommand('addConnection')
public async addConnection(context: vscode.ExtensionContext) { public async addConnection(context: vscode.ExtensionContext) {
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
if (!workspaceConnectionConfig) {
vscode.window.showErrorMessage('OpenMCP: ' + t('error.notOpenWorkspace'));
return;
}
const item = await acquireUserCustomConnection(); const item = await acquireUserCustomConnection();
if (item.length === 0) { if (item.length === 0) {
return; return;
} }
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
workspaceConnectionConfig.items.push(item); workspaceConnectionConfig.items.push(item);
saveWorkspaceConnectionConfig(getWorkspacePath()); saveWorkspaceConnectionConfig(getWorkspacePath());
@ -78,6 +83,10 @@ export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<Conn
@RegisterCommand('openConfiguration') @RegisterCommand('openConfiguration')
public async openConfiguration(context: vscode.ExtensionContext, view: ConnectionViewItem) { public async openConfiguration(context: vscode.ExtensionContext, view: ConnectionViewItem) {
const configPath = getWorkspaceConnectionConfigPath(); const configPath = getWorkspaceConnectionConfigPath();
if (!configPath) {
vscode.window.showErrorMessage('OpenMCP: ' + t('error.notOpenWorkspace'));
return;
}
const uri = vscode.Uri.file(configPath); const uri = vscode.Uri.file(configPath);
vscode.commands.executeCommand('vscode.open', uri); vscode.commands.executeCommand('vscode.open', uri);
} }

View File

@ -22,6 +22,10 @@ export async function deleteUserConnection(item: McpOptions[] | McpOptions) {
} }
const workspaceConnectionConfig = getWorkspaceConnectionConfig(); const workspaceConnectionConfig = getWorkspaceConnectionConfig();
if (!workspaceConnectionConfig) {
vscode.window.showErrorMessage(t('error.notOpenWorkspace'));
return; // 没有打开工作区
}
// 从配置中移除该连接项 // 从配置中移除该连接项