0.0.1 插件端开发完成
This commit is contained in:
parent
8bba879ce6
commit
ad11e4b5c7
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@ node_modules
|
||||
.vscode-test/
|
||||
*.vsix
|
||||
.env
|
||||
resources
|
@ -16,3 +16,5 @@ renderer/**
|
||||
service/**
|
||||
test/**
|
||||
servers/**
|
||||
scripts/**
|
||||
*.sh
|
7
build_service.sh
Executable file
7
build_service.sh
Executable 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
1415
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
11
package.json
11
package.json
@ -33,9 +33,16 @@
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
"@modelcontextprotocol/sdk": "^1.9.0",
|
||||
"axios": "^1.7.7",
|
||||
"bson": "^6.8.0",
|
||||
"openai": "^4.93.0",
|
||||
"pako": "^2.1.0"
|
||||
},
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
{
|
||||
"name": "openmcp-test-backend",
|
||||
"version": "1.0.0",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "dist/main.js",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"serve": "ts-node src/main.ts",
|
||||
"build": "tsc && cp -R src/public dist/ 2> /dev/null || :",
|
||||
"build": "tsc",
|
||||
"build:watch": "tsc --watch",
|
||||
"start": "node dist/main.js",
|
||||
"start:prod": "NODE_ENV=production node dist/main.js",
|
||||
|
3
service/src/index.ts
Normal file
3
service/src/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export { messageController } from './controller';
|
||||
export { VSCodeWebViewLike } from './adapter';
|
||||
export type { VSCodeMessage, MessageHandler } from './main';
|
@ -6,8 +6,10 @@
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"outDir": "./dist"
|
||||
"outDir": "./dist",
|
||||
"declaration": true, // 新增
|
||||
"declarationMap": true // 新增
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules"]
|
||||
"exclude": ["node_modules", "src/main.ts"] // 排除 main.ts
|
||||
}
|
@ -1,9 +1,51 @@
|
||||
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) {
|
||||
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(
|
||||
vscode.window.registerWebviewViewProvider('webview-sidebar.view', provider)
|
||||
@ -11,39 +53,40 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
}
|
||||
|
||||
class WebviewViewProvider implements vscode.WebviewViewProvider {
|
||||
constructor(private readonly _extensionUri: vscode.Uri) {}
|
||||
private _view?: vscode.WebviewView;
|
||||
|
||||
constructor(private readonly context: vscode.ExtensionContext) {}
|
||||
|
||||
public resolveWebviewView(
|
||||
webviewView: vscode.WebviewView,
|
||||
_context: vscode.WebviewViewResolveContext,
|
||||
_token: vscode.CancellationToken,
|
||||
) {
|
||||
console.log('resolveWebviewView called'); // 确保方法被调用
|
||||
this._view = webviewView;
|
||||
webviewView.webview.options = {
|
||||
enableScripts: true,
|
||||
};
|
||||
|
||||
const html = getWebviewContent();
|
||||
console.log('WebView HTML:', html); // 检查 HTML 内容
|
||||
webviewView.webview.html = html;
|
||||
}
|
||||
// 设置HTML内容
|
||||
const html = getWebviewContent(this.context);
|
||||
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 {
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<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>
|
||||
`;
|
||||
private sendMessageToWebview(message: any) {
|
||||
if (this._view) {
|
||||
this._view.webview.postMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function deactivate() {}
|
@ -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));
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user