0.0.1 插件端开发完成

This commit is contained in:
锦恢 2025-04-10 21:28:24 +08:00
parent 8bba879ce6
commit ad11e4b5c7
10 changed files with 1497 additions and 69 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ node_modules
.vscode-test/ .vscode-test/
*.vsix *.vsix
.env .env
resources

View File

@ -16,3 +16,5 @@ renderer/**
service/** service/**
test/** test/**
servers/** servers/**
scripts/**
*.sh

7
build_service.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
mkdir -p ./resources
(cd ./renderer && npm run build && mv ./dist ../resources/renderer) &
(cd ./service && npm run build && mv ./dist ../resources/service) &
wait
echo "构建完成dist文件已移动到resources目录"

1415
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -33,9 +33,16 @@
} }
}, },
"dependencies": { "dependencies": {
"@modelcontextprotocol/sdk": "^1.9.0",
"axios": "^1.7.7",
"bson": "^6.8.0",
"openai": "^4.93.0",
"pako": "^2.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/markdown-it": "^14.1.2" "@types/node": "16.x",
"@types/pako": "^2.0.3",
"@types/showdown": "^2.0.0",
"@types/vscode": "^1.72.0"
} }
} }

View File

@ -1,11 +1,12 @@
{ {
"name": "openmcp-test-backend", "name": "openmcp-test-backend",
"version": "1.0.0", "version": "0.0.1",
"description": "", "description": "",
"main": "dist/main.js", "main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": { "scripts": {
"serve": "ts-node src/main.ts", "serve": "ts-node src/main.ts",
"build": "tsc && cp -R src/public dist/ 2> /dev/null || :", "build": "tsc",
"build:watch": "tsc --watch", "build:watch": "tsc --watch",
"start": "node dist/main.js", "start": "node dist/main.js",
"start:prod": "NODE_ENV=production node dist/main.js", "start:prod": "NODE_ENV=production node dist/main.js",

3
service/src/index.ts Normal file
View File

@ -0,0 +1,3 @@
export { messageController } from './controller';
export { VSCodeWebViewLike } from './adapter';
export type { VSCodeMessage, MessageHandler } from './main';

View File

@ -6,8 +6,10 @@
"esModuleInterop": true, "esModuleInterop": true,
"skipLibCheck": true, "skipLibCheck": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"outDir": "./dist" "outDir": "./dist",
"declaration": true, //
"declarationMap": true //
}, },
"include": ["src/**/*"], "include": ["src/**/*"],
"exclude": ["node_modules"] "exclude": ["node_modules", "src/main.ts"] // main.ts
} }

View File

@ -1,9 +1,51 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import * as fspath from 'path';
import OpenMCPService from '../resources/service';
function getWebviewContent(context: vscode.ExtensionContext, panel?: vscode.WebviewPanel): string | undefined {
const viewRoot = fspath.join(context.extensionPath, 'resources', 'renderer');
const htmlIndexPath = fspath.join(viewRoot, 'index.html');
const html = fs.readFileSync(htmlIndexPath, { encoding: 'utf-8' })?.replace(/(<link.+?href="|<script.+?src="|<img.+?src=")(.+?)"/g, (m, $1, $2) => {
const absLocalPath = fspath.resolve(viewRoot, $2);
const webviewUri = panel?.webview.asWebviewUri(vscode.Uri.file(absLocalPath));
const replaceHref = $1 + webviewUri?.toString() + '"';
return replaceHref;
});
return html;
}
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
console.log('activate openmcp'); // 确保插件已激活 console.log('activate openmcp');
const provider = new WebviewViewProvider(context.extensionUri); // 注册 showOpenMCP 命令
context.subscriptions.push(
vscode.commands.registerCommand('openmcp.showOpenMCP', async () => {
const htmlPath = path.join(context.extensionPath, 'resources', 'renderer', 'index.html');
if (!fs.existsSync(htmlPath)) {
vscode.window.showErrorMessage('未找到 index.html 文件');
return;
}
const panel = vscode.window.createWebviewPanel(
'openmcpView',
'OpenMCP',
vscode.ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true
}
);
const htmlContent = fs.readFileSync(htmlPath, 'utf8');
panel.webview.html = htmlContent;
})
);
const provider = new WebviewViewProvider(context);
context.subscriptions.push( context.subscriptions.push(
vscode.window.registerWebviewViewProvider('webview-sidebar.view', provider) vscode.window.registerWebviewViewProvider('webview-sidebar.view', provider)
@ -11,39 +53,40 @@ export function activate(context: vscode.ExtensionContext) {
} }
class WebviewViewProvider implements vscode.WebviewViewProvider { class WebviewViewProvider implements vscode.WebviewViewProvider {
constructor(private readonly _extensionUri: vscode.Uri) {} private _view?: vscode.WebviewView;
constructor(private readonly context: vscode.ExtensionContext) {}
public resolveWebviewView( public resolveWebviewView(
webviewView: vscode.WebviewView, webviewView: vscode.WebviewView,
_context: vscode.WebviewViewResolveContext, _context: vscode.WebviewViewResolveContext,
_token: vscode.CancellationToken, _token: vscode.CancellationToken,
) { ) {
console.log('resolveWebviewView called'); // 确保方法被调用 this._view = webviewView;
webviewView.webview.options = { webviewView.webview.options = {
enableScripts: true, enableScripts: true,
}; };
const html = getWebviewContent(); // 设置HTML内容
console.log('WebView HTML:', html); // 检查 HTML 内容 const html = getWebviewContent(this.context);
webviewView.webview.html = html; webviewView.webview.html = html || '';
}
// 处理来自webview的消息
webviewView.webview.onDidReceiveMessage(message => {
const { command, data } = message;
OpenMCPService.messageController(command, data, webviewView.webview as any);
});
// 向webview发送消息的示例
this.sendMessageToWebview({ command: 'init', data: 'Hello from extension' });
} }
function getWebviewContent(): string { private sendMessageToWebview(message: any) {
return ` if (this._view) {
<!DOCTYPE html> this._view.webview.postMessage(message);
<html lang="en"> }
<head> }
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebView</title>
</head>
<body>
<h1>Hello, WebView!</h1>
<p>This is a custom WebView in VS Code Sidebar.</p>
</body>
</html>
`;
} }
export function deactivate() {} export function deactivate() {}

View File

@ -1,15 +0,0 @@
import * as assert from 'assert';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
// import * as myExtension from '../../extension';
suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
test('Sample test', () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
});
});