实现 tesseract 的核心部分打包

This commit is contained in:
锦恢 2025-05-03 01:56:41 +08:00
parent 73a5b05a5d
commit 1c92b6fddd
16 changed files with 207 additions and 35 deletions

5
.gitignore vendored
View File

@ -4,9 +4,10 @@ node_modules
.vscode-test/
*.vsix
.env
resources
openmcp-sdk
.DS_Store
.exe
.idea
*.traineddata
resources/ocr/*.js
resources/ocr/*.wasm

View File

@ -1,7 +1,7 @@
# 创建并清理资源目录
New-Item -ItemType Directory -Path ./resources -Force
Remove-Item -Recurse -Force ./resources/* -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path ./resources -Force
New-Item -ItemType Directory -Path ./openmcp-sdk -Force
Remove-Item -Recurse -Force ./openmcp-sdk/* -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path ./openmcp-sdk -Force
# 获取当前工作目录的绝对路径
$currentDir = (Get-Location).Path
@ -11,23 +11,23 @@ $rendererJob = Start-Job -ScriptBlock {
param($workDir)
Set-Location -Path "$workDir\renderer"
npm run build
Move-Item -Path "./dist" -Destination "$workDir\resources\renderer" -Force
Move-Item -Path "./dist" -Destination "$workDir\openmcp-sdk\renderer" -Force
} -ArgumentList $currentDir
$serviceJob = Start-Job -ScriptBlock {
param($workDir)
Set-Location -Path "$workDir\service"
npm run build
Move-Item -Path "./dist" -Destination "$workDir\resources\service" -Force
Move-Item -Path "./dist" -Destination "$workDir\openmcp-sdk\service" -Force
} -ArgumentList $currentDir
# 等待任务完成
$rendererJob | Wait-Job | Receive-Job
$serviceJob | Wait-Job | Receive-Job
# 将 resources 目录复制到 software/resources
New-Item -ItemType Directory -Path ./software/resources -Force
Remove-Item -Recurse -Force ./software/resources/* -ErrorAction SilentlyContinue
Copy-Item -Recurse -Path ./resources -Destination ./software/ -Force
# 将 openmcp-sdk 目录复制到 software/openmcp-sdk
New-Item -ItemType Directory -Path ./software/openmcp-sdk -Force
Remove-Item -Recurse -Force ./software/openmcp-sdk/* -ErrorAction SilentlyContinue
Copy-Item -Recurse -Path ./openmcp-sdk -Destination ./software/ -Force
Write-Output "finish building services in ./resources"
Write-Output "finish building services in ./openmcp-sdk"

View File

@ -1,16 +1,16 @@
#!/bin/bash
mkdir -p ./resources
rm -rf ./resources/
mkdir -p ./resources
mkdir -p ./openmcp-sdk
rm -rf ./openmcp-sdk/
mkdir -p ./openmcp-sdk
(cd ./renderer && npm run build && mv ./dist ../resources/renderer) &
(cd ./service && npm run build && mv ./dist ../resources/service) &
(cd ./renderer && npm run build && mv ./dist ../openmcp-sdk/renderer) &
(cd ./service && npm run build && mv ./dist ../openmcp-sdk/service) &
wait
mkdir -p ./software/resources
rm -rf ./software/resources
cp -r ./resources ./software/
mkdir -p ./software/openmcp-sdk
rm -rf ./software/openmcp-sdk
cp -r ./openmcp-sdk ./software/
echo "finish building services in ./resources"
echo "finish building services in ./openmcp-sdk"

3
configure.sh Normal file → Executable file
View File

@ -1,4 +1,5 @@
cd renderer && npm i && cd ..
cd service && npm i && node patch-mcp-sdk.js && cd ..
cd service && npm i && cd ..
cd servers && uv sync
npm i
npm run prepare:ocr

116
package-lock.json generated
View File

@ -23,6 +23,7 @@
"@types/pako": "^2.0.3",
"@types/showdown": "^2.0.0",
"@types/vscode": "^1.72.0",
"copy-webpack-plugin": "^13.0.0",
"ts-loader": "^9.5.1",
"typescript": "^5.4.2",
"webpack": "^5.99.5",
@ -850,6 +851,30 @@
"node": ">=6.6.0"
}
},
"node_modules/copy-webpack-plugin": {
"version": "13.0.0",
"resolved": "https://registry.npmjs.org/copy-webpack-plugin/-/copy-webpack-plugin-13.0.0.tgz",
"integrity": "sha512-FgR/h5a6hzJqATDGd9YG41SeDViH+0bkHn6WNXCi5zKAZkeESeSxLySSsFLHqLEVCh0E+rITmCf0dusXWYukeQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"glob-parent": "^6.0.1",
"normalize-path": "^3.0.0",
"schema-utils": "^4.2.0",
"serialize-javascript": "^6.0.2",
"tinyglobby": "^0.2.12"
},
"engines": {
"node": ">= 18.12.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/webpack"
},
"peerDependencies": {
"webpack": "^5.1.0"
}
},
"node_modules/cors": {
"version": "2.8.5",
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
@ -1450,6 +1475,19 @@
"node": ">= 0.4"
}
},
"node_modules/glob-parent": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz",
"integrity": "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==",
"dev": true,
"license": "ISC",
"dependencies": {
"is-glob": "^4.0.3"
},
"engines": {
"node": ">=10.13.0"
}
},
"node_modules/glob-to-regexp": {
"version": "0.4.1",
"resolved": "https://registry.npmmirror.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz",
@ -1669,6 +1707,16 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/is-extglob": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz",
"integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/is-generator-function": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.1.0.tgz",
@ -1687,6 +1735,19 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/is-glob": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
"integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==",
"dev": true,
"license": "MIT",
"dependencies": {
"is-extglob": "^2.1.1"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/is-number": {
"version": "7.0.0",
"resolved": "https://registry.npmmirror.com/is-number/-/is-number-7.0.0.tgz",
@ -1993,6 +2054,16 @@
"integrity": "sha512-xxOWJsBKtzAq7DY0J+DTzuz58K8e7sJbdgwkbMWQe8UYB6ekmsQ45q0M/tJDsGaZmbC+l7n57UV8Hl5tHxO9uw==",
"dev": true
},
"node_modules/normalize-path": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
"integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
@ -2781,6 +2852,51 @@
"integrity": "sha512-1Qncm/9oKM7xgrQXZXNB+NRh19qiXGhxlrR8EwFbK5SaUbPZnS5OMtP/ghtqfd23hsr1ZvZbZjeuAGcMxd/ooA==",
"license": "Apache-2.0"
},
"node_modules/tinyglobby": {
"version": "0.2.13",
"resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.13.tgz",
"integrity": "sha512-mEwzpUgrLySlveBwEVDMKk5B57bhLPYovRfPAXD5gA/98Opn0rCDj3GtLwFvCvH5RK9uPCExUROW5NjDwvqkxw==",
"dev": true,
"license": "MIT",
"dependencies": {
"fdir": "^6.4.4",
"picomatch": "^4.0.2"
},
"engines": {
"node": ">=12.0.0"
},
"funding": {
"url": "https://github.com/sponsors/SuperchupuDev"
}
},
"node_modules/tinyglobby/node_modules/fdir": {
"version": "6.4.4",
"resolved": "https://registry.npmjs.org/fdir/-/fdir-6.4.4.tgz",
"integrity": "sha512-1NZP+GK4GfuAv3PqKvxQRDMjdSRZjnkq7KfhlNrCNNlZ0ygQFpebfrnfnq/W7fpUnAv9aGWmY1zKx7FYL3gwhg==",
"dev": true,
"license": "MIT",
"peerDependencies": {
"picomatch": "^3 || ^4"
},
"peerDependenciesMeta": {
"picomatch": {
"optional": true
}
}
},
"node_modules/tinyglobby/node_modules/picomatch": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.2.tgz",
"integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/jonschlinkert"
}
},
"node_modules/to-regex-range": {
"version": "5.0.1",
"resolved": "https://registry.npmmirror.com/to-regex-range/-/to-regex-range-5.0.1.tgz",

View File

@ -218,7 +218,8 @@
"watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
"test": "node ./out/test/runTest.js",
"prepare:ocr": "webpack --config webpack/webpack.tesseract.js"
},
"dependencies": {
"@modelcontextprotocol/sdk": "^1.10.2",
@ -236,6 +237,7 @@
"@types/pako": "^2.0.3",
"@types/showdown": "^2.0.0",
"@types/vscode": "^1.72.0",
"copy-webpack-plugin": "^13.0.0",
"ts-loader": "^9.5.1",
"typescript": "^5.4.2",
"webpack": "^5.99.5",

Binary file not shown.

Binary file not shown.

2
software/.gitignore vendored
View File

@ -4,7 +4,7 @@ node_modules
.vscode-test/
*.vsix
.env
resources
openmcp-sdk
.DS_Store
.exe

View File

@ -39,6 +39,7 @@
},
"files": [
"dist/**/*",
"openmcp-sdk/**/*",
"resources/**/*",
"icons/**/*"
],

View File

@ -1,5 +1,5 @@
import { app, BrowserWindow, ipcMain } from 'electron';
import * as OpenMCPService from '../resources/service';
import * as OpenMCPService from '../openmcp-sdk/service';
import * as path from 'path';
import { ElectronIPCLike, getInitConnectionOption, ILaunchSigature, updateConnectionOption } from './util';
@ -75,7 +75,7 @@ function createWindow(): void {
});
const indexPath = path.join(__dirname, '..', 'resources/renderer/index.html');
const indexPath = path.join(__dirname, '..', 'openmcp-sdk/renderer/index.html');
mainWindow.loadFile(indexPath);
setTimeout(() => {

View File

@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import * as OpenMCPService from '../resources/service';
import * as OpenMCPService from '../openmcp-sdk/service';
import { launch } from './common/entry';
export function activate(context: vscode.ExtensionContext) {

View File

@ -13,6 +13,12 @@ export class HookController {
console.log('test ocr begin');
const cachePath = context.extensionPath;
const workerPath = path.join(cachePath, 'resources', 'ocr', 'tesseract-core.js');
const corePath = path.join(cachePath, 'resources', 'ocr', 'tesseract-core.wasm');
console.log('workerPath', workerPath);
console.log('corePath', corePath);
console.log('cachePath', context.extensionPath);
const { data: { text } } = await Tesseract.recognize(
@ -23,7 +29,9 @@ export class HookController {
langPath: './',
gzip: false,
cacheMethod: 'cache',
cachePath: context.extensionPath
cachePath,
workerPath,
corePath
}
);

View File

@ -2,10 +2,10 @@ import * as vscode from 'vscode';
import * as fs from 'fs';
import * as fspath from 'path';
import { IConnectionItem, ILaunchSigature, panels, updateInstalledConnectionConfig, updateWorkspaceConnectionConfig } from '../global';
import * as OpenMCPService from '../../resources/service';
import * as OpenMCPService from '../../openmcp-sdk/service';
export function getWebviewContent(context: vscode.ExtensionContext, panel: vscode.WebviewPanel): string | undefined {
const viewRoot = fspath.join(context.extensionPath, 'resources', 'renderer');
const viewRoot = fspath.join(context.extensionPath, 'openmcp-sdk', 'renderer');
const htmlIndexPath = fspath.join(viewRoot, 'index.html');
const html = fs.readFileSync(htmlIndexPath, { encoding: 'utf-8' })?.replace(/(<link.+?href="|<script.+?src="|<img.+?src="|url\()(.+?)(\)|")/g, (m, $1, $2) => {
const absLocalPath = fspath.resolve(viewRoot, $2);
@ -58,7 +58,7 @@ export function revealOpenMcpWebviewPanel(
// 设置HTML内容
const html = getWebviewContent(context, panel);
panel.webview.html = html || '';
panel.iconPath = vscode.Uri.file(fspath.join(context.extensionPath, 'resources', 'renderer', 'images', 'openmcp.png'));
panel.iconPath = vscode.Uri.file(fspath.join(context.extensionPath, 'openmcp-sdk', 'renderer', 'images', 'openmcp.png'));
// 处理来自webview的消息
panel.webview.onDidReceiveMessage(message => {

View File

@ -9,9 +9,9 @@
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
// 访 resources
// 访 openmcp-sdk
"paths": {
"@resources/*": ["./resources/*"]
"@openmcp-sdk/*": ["./openmcp-sdk/*"]
}
},
"include": ["src/**/*"],

View File

@ -0,0 +1,43 @@
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: './node_modules/tesseract.js/src/worker-script/node/index.js',
output: {
path: path.resolve(__dirname, '..', 'resources', 'ocr'),
filename: 'worker.js',
libraryTarget: 'commonjs2'
},
resolve: {
fallback: {
bufferutil: false,
'utf-8-validate': false
}
},
mode: 'production',
target: 'node',
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, '..', 'node_modules', 'tesseract.js-core', 'tesseract*'),
to: path.resolve(__dirname, '..', 'resources', 'ocr', '[name][ext]'),
},
],
}),
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
],
},
};