support ai-mook
This commit is contained in:
commit
c366494637
@ -15,5 +15,6 @@
|
||||
"join-project": "Participate in the project",
|
||||
"comment-plugin": "Comment Plugin",
|
||||
"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."
|
||||
}
|
@ -15,5 +15,7 @@
|
||||
"join-project": "プロジェクトに参加する",
|
||||
"comment-plugin": "コメントプラグイン",
|
||||
"preset-env-sync": "プリセット環境変数の同期が完了しました",
|
||||
"preset-env-sync.fail": "プリセット環境変数の同期に失敗しました"
|
||||
"preset-env-sync.fail": "プリセット環境変数の同期に失敗しました",
|
||||
"error.notOpenWorkspace": "現在、VSCode でワークスペースが開かれていません。まずワークスペース(例:フォルダーを開く)を開いてください。"
|
||||
|
||||
}
|
@ -15,5 +15,6 @@
|
||||
"join-project": "参与项目",
|
||||
"comment-plugin": "评论插件",
|
||||
"preset-env-sync": "预设环境变量同步完成",
|
||||
"preset-env-sync.fail": "预设环境变量同步失败"
|
||||
"preset-env-sync.fail": "预设环境变量同步失败",
|
||||
"error.notOpenWorkspace": "当前VScode没有打开工作区,请先打开工作区(例如打开文件夹)"
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { setRunningCWD, setVscodeWorkspace } from '../openmcp-sdk/service/index.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) {
|
||||
console.log('activate openmcp');
|
||||
@ -13,6 +13,11 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
setVscodeWorkspace(workspace);
|
||||
setRunningCWD(context.extensionPath);
|
||||
initialiseI18n(context);
|
||||
|
||||
// 添加i18n调试信息
|
||||
console.log('Current language:', vscode.env.language);
|
||||
console.log('Available i18n keys:', getAvailableKeys().length);
|
||||
|
||||
launch(context);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ import * as vscode from 'vscode';
|
||||
import * as os from 'os';
|
||||
import * as fspath from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { t } from './i18n';
|
||||
|
||||
export type FsPath = string;
|
||||
export const panels = new Map<FsPath, vscode.WebviewPanel>();
|
||||
@ -96,7 +97,7 @@ export function getConnectionConfig() {
|
||||
export function getWorkspaceConnectionConfigPath() {
|
||||
const workspace = getWorkspacePath();
|
||||
if (!workspace) {
|
||||
throw new Error('No workspace found. Please open a folder in VSCode first.');
|
||||
return null; // 如果没有工作区,则返回 null
|
||||
}
|
||||
const configDir = fspath.join(workspace, '.openmcp');
|
||||
if (!fs.existsSync(configDir)) {
|
||||
@ -110,14 +111,14 @@ export function getWorkspaceConnectionConfigPath() {
|
||||
* @description 获取工作区的连接信息,工作区的连接文件的路径都是相对路径,以 {workspace} 开头
|
||||
* @param workspace
|
||||
*/
|
||||
export function getWorkspaceConnectionConfig() {
|
||||
export function getWorkspaceConnectionConfig():IConnectionConfig| null {
|
||||
if (_workspaceConnectionConfig) {
|
||||
return _workspaceConnectionConfig;
|
||||
}
|
||||
|
||||
const workspace = getWorkspacePath();
|
||||
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 connectionConfig = fspath.join(configDir, CONNECTION_CONFIG_NAME);
|
||||
@ -228,6 +229,10 @@ export function updateWorkspaceConnectionConfig(
|
||||
) {
|
||||
const connectionItem = getWorkspaceConnectionConfigItemByName(name);
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
if (!workspaceConnectionConfig) {
|
||||
console.error('没有工作区连接配置文件,请先创建一个工作区连接');
|
||||
return;
|
||||
}
|
||||
|
||||
data.forEach(item => {
|
||||
item.cwd = item.cwd?.replace(/\\/g, '/');
|
||||
@ -326,6 +331,10 @@ export function getWorkspacePath() {
|
||||
export function getWorkspaceConnectionConfigItemByPath(absPath: string) {
|
||||
const workspacePath = getWorkspacePath();
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
if (!workspaceConnectionConfig) {
|
||||
return null; // 如果没有工作区连接配置文件,则返回 null
|
||||
}
|
||||
|
||||
|
||||
const normaliseAbsPath = absPath.replace(/\\/g, '/');
|
||||
for (let item of workspaceConnectionConfig.items) {
|
||||
@ -345,9 +354,10 @@ export function getWorkspaceConnectionConfigItemByPath(absPath: string) {
|
||||
* @param absPath
|
||||
*/
|
||||
export function getWorkspaceConnectionConfigItemByName(name: string) {
|
||||
const workspacePath = getWorkspacePath();
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
|
||||
if (!workspaceConnectionConfig) {
|
||||
return null; // 如果没有工作区连接配置文件,则返回 null
|
||||
}
|
||||
for (let item of workspaceConnectionConfig.items) {
|
||||
const nItem = Array.isArray(item) ? item[0] : item;
|
||||
if (nItem.name === name) {
|
||||
|
@ -1,26 +1,81 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
const defaultBundle: Record<string, string> = {}
|
||||
|
||||
export function initialiseI18n(context: vscode.ExtensionContext) {
|
||||
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>;
|
||||
Object.assign(defaultBundle, bundle);
|
||||
// 获取用户的语言设置
|
||||
const userLanguage = vscode.env.language;
|
||||
|
||||
// 尝试加载用户语言对应的语言包
|
||||
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 {
|
||||
if (vscode.l10n.bundle === undefined) {
|
||||
// 使用自定义的语言包
|
||||
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]);
|
||||
}
|
||||
|
||||
return translateMessage;
|
||||
} else {
|
||||
// 使用 VS Code 的 l10n API
|
||||
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;
|
||||
}
|
@ -4,6 +4,7 @@ import { getWorkspaceConnectionConfig, getWorkspaceConnectionConfigPath, getWork
|
||||
import { ConnectionViewItem } from './common.js';
|
||||
import { revealOpenMcpWebviewPanel } from '../webview/webview.service.js';
|
||||
import { acquireUserCustomConnection, deleteUserConnection } from './workspace.service.js';
|
||||
import { t } from '../i18n/index.js';
|
||||
|
||||
@RegisterTreeDataProvider('openmcp.sidebar.workspace-connection')
|
||||
export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<ConnectionViewItem> {
|
||||
@ -60,14 +61,18 @@ export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<Conn
|
||||
|
||||
@RegisterCommand('addConnection')
|
||||
public async addConnection(context: vscode.ExtensionContext) {
|
||||
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
if (!workspaceConnectionConfig) {
|
||||
vscode.window.showErrorMessage('OpenMCP: ' + t('error.notOpenWorkspace'));
|
||||
return;
|
||||
}
|
||||
const item = await acquireUserCustomConnection();
|
||||
|
||||
if (item.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
|
||||
workspaceConnectionConfig.items.push(item);
|
||||
saveWorkspaceConnectionConfig(getWorkspacePath());
|
||||
|
||||
@ -78,6 +83,10 @@ export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<Conn
|
||||
@RegisterCommand('openConfiguration')
|
||||
public async openConfiguration(context: vscode.ExtensionContext, view: ConnectionViewItem) {
|
||||
const configPath = getWorkspaceConnectionConfigPath();
|
||||
if (!configPath) {
|
||||
vscode.window.showErrorMessage('OpenMCP: ' + t('error.notOpenWorkspace'));
|
||||
return;
|
||||
}
|
||||
const uri = vscode.Uri.file(configPath);
|
||||
vscode.commands.executeCommand('vscode.open', uri);
|
||||
}
|
||||
|
@ -22,6 +22,10 @@ export async function deleteUserConnection(item: McpOptions[] | McpOptions) {
|
||||
}
|
||||
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
if (!workspaceConnectionConfig) {
|
||||
vscode.window.showErrorMessage(t('error.notOpenWorkspace'));
|
||||
return; // 没有打开工作区
|
||||
}
|
||||
|
||||
// 从配置中移除该连接项
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user