Compare commits
16 Commits
7c2d5d9ad9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c661ec06a | |||
| 872df0fd65 | |||
| 505881edef | |||
| c5591f8936 | |||
| b7a73c4b15 | |||
| 4b74fa615a | |||
|
|
07bb207ff3 | ||
| 501a38b796 | |||
| 55c3bdfdfc | |||
| 5892abf513 | |||
| 7dc0f11e6c | |||
| a2f474e1ca | |||
| dcf190067c | |||
| 7a19e09b1d | |||
| a816e9cd91 | |||
| afd408a4b8 |
@@ -13,12 +13,15 @@
|
||||
"warn",
|
||||
{
|
||||
"selector": "import",
|
||||
"format": [ "camelCase", "PascalCase" ]
|
||||
"format": [
|
||||
"camelCase",
|
||||
"PascalCase"
|
||||
]
|
||||
}
|
||||
],
|
||||
"@typescript-eslint/semi": "warn",
|
||||
"@typescript-eslint/semi": "off",
|
||||
"curly": "warn",
|
||||
"eqeqeq": "warn",
|
||||
"eqeqeq": "off",
|
||||
"no-throw-literal": "warn",
|
||||
"semi": "off"
|
||||
},
|
||||
|
||||
@@ -23,8 +23,8 @@ jobs:
|
||||
- name: 拉取代码
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
fetch-depth: 0
|
||||
ref: ${{ github.head_ref }}
|
||||
|
||||
- name: 配置 Git
|
||||
run: |
|
||||
@@ -33,32 +33,51 @@ jobs:
|
||||
git config --global --add safe.directory /github/workspace
|
||||
|
||||
# 配置用户信息
|
||||
git config --global user.name "github-actions[bot]"
|
||||
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
||||
git config --global user.name "devstar"
|
||||
git config --global user.email "devstar@noreply.devstar.cn"
|
||||
|
||||
- name: 自动递增版本号
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
- name: Check and bump version
|
||||
if: github.event_name == 'pull_request'
|
||||
run: |
|
||||
apk add --no-cache jq
|
||||
CURRENT_VERSION=$(jq -r '.version' package.json)
|
||||
echo "当前版本: $CURRENT_VERSION"
|
||||
# 获取远程 main 分支版本
|
||||
git fetch origin main
|
||||
MAIN_VERSION=$(git show origin/main:package.json | jq -r '.version')
|
||||
PR_VERSION=$(jq -r '.version' package.json)
|
||||
|
||||
# 分解版本号
|
||||
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
|
||||
echo "Main 分支版本: $MAIN_VERSION"
|
||||
echo "当前 PR 版本: $PR_VERSION"
|
||||
|
||||
# 递增补丁版本号
|
||||
NEW_PATCH=$((PATCH + 1))
|
||||
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
|
||||
echo "新版本: $NEW_VERSION"
|
||||
|
||||
# 更新 package.json
|
||||
jq --arg version "$NEW_VERSION" '.version = $version' package.json > package.json.tmp
|
||||
mv package.json.tmp package.json
|
||||
|
||||
# 提交版本变更
|
||||
git add package.json
|
||||
git commit -m "chore: bump version to $NEW_VERSION [skip ci]"
|
||||
git push
|
||||
# 如果版本号相同,则递增
|
||||
if [ "$MAIN_VERSION" = "$PR_VERSION" ]; then
|
||||
echo "版本号未变更,开始递增..."
|
||||
|
||||
# 分解版本号
|
||||
MAJOR=$(echo "$PR_VERSION" | cut -d'.' -f1)
|
||||
MINOR=$(echo "$PR_VERSION" | cut -d'.' -f2)
|
||||
PATCH=$(echo "$PR_VERSION" | cut -d'.' -f3)
|
||||
|
||||
# 递增补丁版本号
|
||||
NEW_PATCH=$((PATCH + 1))
|
||||
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
|
||||
echo "新版本: $NEW_VERSION"
|
||||
|
||||
# 更新 package.json
|
||||
jq --arg version "$NEW_VERSION" '.version = $version' package.json > package.json.tmp
|
||||
mv package.json.tmp package.json
|
||||
|
||||
# 提交版本变更
|
||||
git config user.name "devstar-actions"
|
||||
git config user.email "devstar-actions@devstar.cn"
|
||||
git add package.json
|
||||
git commit -m "chore: bump version to $NEW_VERSION [skip ci]"
|
||||
|
||||
# 推送到当前分支
|
||||
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
|
||||
git push origin $BRANCH_NAME || git push origin HEAD:refs/heads/$BRANCH_NAME
|
||||
else
|
||||
echo "版本号已更新,跳过递增"
|
||||
fi
|
||||
|
||||
- name: 安装依赖
|
||||
run: |
|
||||
@@ -73,4 +92,4 @@ jobs:
|
||||
run: |
|
||||
npm run publish
|
||||
env:
|
||||
VSCE_PAT: ${{ secrets.VSCE_PAT }}
|
||||
VSCE_PAT: ${{ secrets.VSCE_PAT }}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "devstar",
|
||||
"displayName": "%displayName%",
|
||||
"description": "%description%",
|
||||
"version": "0.4.3",
|
||||
"version": "0.5.3",
|
||||
"keywords": [],
|
||||
"publisher": "mengning",
|
||||
"engines": {
|
||||
@@ -42,6 +42,10 @@
|
||||
{
|
||||
"protocol": "vscode",
|
||||
"path": "/openProject"
|
||||
},
|
||||
{
|
||||
"protocol": "trae",
|
||||
"path": "/openProject"
|
||||
}
|
||||
],
|
||||
"views": {
|
||||
|
||||
@@ -25,6 +25,46 @@ export default class RemoteContainer {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建打开项目的命令
|
||||
* 根据终端类型生成对应的命令
|
||||
* 支持 Windows PowerShell/CMD 和 Linux/macOS Bash/Zsh
|
||||
*/
|
||||
private buildOpenProjectCommand(
|
||||
host: string,
|
||||
hostname: string,
|
||||
port: number,
|
||||
username: string,
|
||||
path: string,
|
||||
devstarDomain: string | undefined,
|
||||
terminal: vscode.Terminal
|
||||
): string {
|
||||
const baseUrl = `vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}`;
|
||||
const url = devstarDomain ? `${baseUrl}&devstar_domain=${devstarDomain}` : baseUrl;
|
||||
|
||||
// 获取本地操作系统类型
|
||||
// 在远程环境下,os.platform() 返回的是远程系统,但这里我们需要的是本地系统
|
||||
// 所以通过终端的 shell 路径来推断
|
||||
const shellPath = (terminal.creationOptions as any)?.shellPath || '';
|
||||
console.log('终端 shell 路径:', shellPath);
|
||||
|
||||
// 检测 Windows(Windows shell 路径包含反斜杠或特定关键字)
|
||||
const isWindows = shellPath.includes('\\') ||
|
||||
shellPath.toLowerCase().includes('powershell') ||
|
||||
shellPath.toLowerCase().includes('pwsh') ||
|
||||
shellPath.toLowerCase().includes('cmd');
|
||||
|
||||
if (isWindows) {
|
||||
// Windows PowerShell/CMD: 使用分号分隔,URL 需要特殊的引号处理
|
||||
console.log('检测到 Windows 本地系统,使用 PowerShell/CMD 语法');
|
||||
return `code --new-window; code --open-url '"${url}"'`;
|
||||
} else {
|
||||
// macOS/Linux: 使用 && 分隔,标准双引号
|
||||
console.log('检测到 Unix 本地系统(macOS/Linux),使用 Bash/Zsh 语法');
|
||||
return `code --new-window && code --open-url "${url}"`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 第一次打开远程项目
|
||||
*/
|
||||
@@ -40,29 +80,8 @@ export default class RemoteContainer {
|
||||
devstarDomain = undefined;
|
||||
}
|
||||
|
||||
const semver = require('semver');
|
||||
const powershellVersion = context.globalState.get('powershellVersion');
|
||||
const powershell_semver_compatible_version = semver.coerce(powershellVersion);
|
||||
|
||||
let command = '';
|
||||
if (devstarDomain === undefined) {
|
||||
if (powershellVersion === undefined) {
|
||||
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||
} else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
||||
command = `code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||
} else {
|
||||
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||
}
|
||||
} else {
|
||||
if (powershellVersion === undefined) {
|
||||
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||
} else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
||||
command = `code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||
} else {
|
||||
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||
}
|
||||
}
|
||||
|
||||
// 根据终端类型生成对应的命令
|
||||
const command = this.buildOpenProjectCommand(host, hostname, port, username, path, devstarDomain, terminal);
|
||||
terminal.sendText(command);
|
||||
} else {
|
||||
vscode.window.showErrorMessage('无法创建终端,请检查终端是否可用。');
|
||||
@@ -276,18 +295,6 @@ export default class RemoteContainer {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
// 由于使用了 stdio: 'ignore',这些事件监听器不再需要
|
||||
// sshProcess.stdout.on('data', (data: Buffer) => {
|
||||
// console.log(`[SSH stdout] ${data.toString()}`);
|
||||
// });
|
||||
|
||||
// sshProcess.stderr.on('data', (data: Buffer) => {
|
||||
// console.error(`[SSH stderr] ${data.toString()}`);
|
||||
// });
|
||||
|
||||
// 注意:由于进程已 detached 和 unref,不再需要保存到 sshProcesses Map
|
||||
// 因为我们无法也不需要控制这些独立进程的生命周期
|
||||
|
||||
// 等待 SSH 连接建立
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
@@ -372,6 +379,22 @@ export default class RemoteContainer {
|
||||
} catch (portError) {
|
||||
vscode.window.showWarningMessage('端口映射设置失败,但容器连接已建立');
|
||||
}
|
||||
|
||||
// 自动配置本地 VSCode 的 MCP Server
|
||||
try {
|
||||
await this.configureMCPServerLocally();
|
||||
} catch (mcpError) {
|
||||
console.error('[MCP] 本地 MCP 配置失败:', mcpError);
|
||||
// 不阻塞主流程,仅记录错误
|
||||
}
|
||||
|
||||
// 自动配置容器内 AI IDE 的 MCP Server
|
||||
try {
|
||||
await this.configureMCPServerInContainer(sshConfig.hostname, port);
|
||||
} catch (mcpError) {
|
||||
console.error('[MCP] 容器内 MCP 配置失败:', mcpError);
|
||||
// 不阻塞主流程,仅记录错误
|
||||
}
|
||||
}
|
||||
|
||||
// 使用 VSCode Extension API 打开远程连接
|
||||
@@ -471,6 +494,232 @@ export default class RemoteContainer {
|
||||
console.log('未找到 forwardPorts 参数,跳过端口映射设置。');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在容器内配置 AI IDE 的 MCP Server
|
||||
* 支持 Trae IDE 等 AI IDE 的 MCP 自动配置
|
||||
*/
|
||||
private async configureMCPServerInContainer(hostname: string, sshPort: number): Promise<void> {
|
||||
// 获取用户 token
|
||||
const userToken = this.user.getUserTokenFromLocal();
|
||||
if (!userToken) {
|
||||
console.log('[MCP] 用户未登录,跳过 MCP Server 配置');
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取 DevStar 域名
|
||||
const devstarDomain = this.user.getDevstarDomain();
|
||||
const mcpUrl = `${devstarDomain}/api/mcp`;
|
||||
|
||||
console.log(`[MCP] 开始配置 MCP Server: ${mcpUrl}`);
|
||||
|
||||
// Trae IDE 的 MCP 配置路径
|
||||
const traeMcpPath = '/root/.trae-server/data/Machine/mcp.json';
|
||||
|
||||
// MCP 配置内容(注意:使用 "mcpServers" 格式)
|
||||
const mcpConfig = {
|
||||
mcpServers: {
|
||||
devstar: {
|
||||
type: 'http',
|
||||
url: mcpUrl,
|
||||
headers: {
|
||||
Authorization: `Bearer ${userToken}`
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const configJson = JSON.stringify(mcpConfig, null, 2);
|
||||
|
||||
// 使用 SSH 连接到容器并配置
|
||||
const ssh = new NodeSSH();
|
||||
|
||||
try {
|
||||
await ssh.connect({
|
||||
host: hostname,
|
||||
username: 'root',
|
||||
port: sshPort,
|
||||
privateKeyPath: this.user.getUserPrivateKeyPath(),
|
||||
readyTimeout: 10000,
|
||||
});
|
||||
|
||||
console.log('[MCP] SSH 连接成功');
|
||||
|
||||
// 检查现有配置
|
||||
const checkScript = `
|
||||
if [ -f "${traeMcpPath}" ]; then
|
||||
cat "${traeMcpPath}"
|
||||
else
|
||||
echo "FILE_NOT_EXISTS"
|
||||
fi
|
||||
`;
|
||||
|
||||
const checkResult = await ssh.execCommand(checkScript);
|
||||
|
||||
let needUpdate = true;
|
||||
|
||||
if (checkResult.stdout !== 'FILE_NOT_EXISTS') {
|
||||
try {
|
||||
const existingConfig = JSON.parse(checkResult.stdout);
|
||||
const existingDevstar = existingConfig.mcpServers?.devstar;
|
||||
|
||||
if (existingDevstar) {
|
||||
// 检查 URL 和 token 是否匹配
|
||||
const urlMatch = existingDevstar.url === mcpUrl;
|
||||
const tokenMatch = existingDevstar.headers?.Authorization === `Bearer ${userToken}`;
|
||||
|
||||
if (urlMatch && tokenMatch) {
|
||||
console.log('[MCP] DevStar MCP 配置已存在且正确,无需更新');
|
||||
needUpdate = false;
|
||||
} else {
|
||||
console.log(`[MCP] DevStar MCP 配置需要更新 (URL匹配: ${urlMatch}, Token匹配: ${tokenMatch})`);
|
||||
}
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.log('[MCP] 解析现有配置失败,将创建新配置');
|
||||
}
|
||||
} else {
|
||||
console.log('[MCP] 容器内无配置文件,需要创建');
|
||||
}
|
||||
|
||||
// 创建或更新配置文件
|
||||
if (needUpdate) {
|
||||
const setupScript = `
|
||||
mkdir -p /root/.trae-server/data/Machine && \
|
||||
cat > ${traeMcpPath} << 'EOF'
|
||||
${configJson}
|
||||
EOF
|
||||
echo "MCP 配置已更新"
|
||||
`;
|
||||
|
||||
const result = await ssh.execCommand(setupScript);
|
||||
|
||||
if (result.code === 0) {
|
||||
console.log('[MCP] DevStar MCP 配置成功');
|
||||
} else {
|
||||
console.error(`[MCP] 配置失败: ${result.stderr}`);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('[MCP] SSH 连接或配置失败:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
try {
|
||||
await ssh.dispose();
|
||||
} catch (e) {
|
||||
// ignore dispose error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在本地 VSCode 中配置 MCP Server
|
||||
* 支持 GitHub Copilot 等使用本地 MCP 配置
|
||||
*/
|
||||
private async configureMCPServerLocally(): Promise<void> {
|
||||
// 获取用户 token
|
||||
const userToken = this.user.getUserTokenFromLocal();
|
||||
if (!userToken) {
|
||||
console.log('[MCP] 用户未登录,跳过本地 MCP Server 配置');
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取 DevStar 域名
|
||||
const devstarDomain = this.user.getDevstarDomain();
|
||||
const mcpUrl = `${devstarDomain}/api/mcp`;
|
||||
|
||||
console.log(`[MCP] 开始配置本地 MCP Server: ${mcpUrl}`);
|
||||
|
||||
// 根据操作系统确定配置文件路径
|
||||
let mcpConfigPath: string;
|
||||
const platform = os.platform();
|
||||
const homedir = os.homedir();
|
||||
|
||||
if (platform === 'darwin') {
|
||||
// macOS
|
||||
mcpConfigPath = path.join(homedir, 'Library/Application Support/Code/User/mcp.json');
|
||||
} else if (platform === 'win32') {
|
||||
// Windows
|
||||
mcpConfigPath = path.join(homedir, 'AppData/Roaming/Code/User/mcp.json');
|
||||
} else {
|
||||
// Linux
|
||||
mcpConfigPath = path.join(homedir, '.config/Code/User/mcp.json');
|
||||
}
|
||||
|
||||
console.log(`[MCP] 配置文件路径: ${mcpConfigPath}`);
|
||||
|
||||
// MCP 配置内容(本地 VSCode 使用 "servers" 格式)
|
||||
const mcpConfig = {
|
||||
servers: {
|
||||
devstar: {
|
||||
type: 'http',
|
||||
url: mcpUrl,
|
||||
headers: {
|
||||
Authorization: `Bearer ${userToken}`
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const configJson = JSON.stringify(mcpConfig, null, 2);
|
||||
|
||||
try {
|
||||
// 检查现有配置
|
||||
let existingConfig: any = {};
|
||||
let needUpdate = true;
|
||||
|
||||
if (fs.existsSync(mcpConfigPath)) {
|
||||
try {
|
||||
const content = fs.readFileSync(mcpConfigPath, 'utf8');
|
||||
existingConfig = JSON.parse(content);
|
||||
const existingDevstar = existingConfig.servers?.devstar;
|
||||
|
||||
if (existingDevstar) {
|
||||
// 检查 URL 和 token 是否匹配
|
||||
const urlMatch = existingDevstar.url === mcpUrl;
|
||||
const tokenMatch = existingDevstar.headers?.Authorization === `Bearer ${userToken}`;
|
||||
|
||||
if (urlMatch && tokenMatch) {
|
||||
console.log('[MCP] 本地 DevStar MCP 配置已存在且正确,无需更新');
|
||||
needUpdate = false;
|
||||
} else {
|
||||
console.log(`[MCP] 本地 DevStar MCP 配置需要更新 (URL匹配: ${urlMatch}, Token匹配: ${tokenMatch})`);
|
||||
}
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.log('[MCP] 解析现有配置失败,将创建新配置');
|
||||
}
|
||||
} else {
|
||||
console.log('[MCP] 本地无配置文件,需要创建');
|
||||
}
|
||||
|
||||
// 创建或更新配置文件
|
||||
if (needUpdate) {
|
||||
// 确保目录存在
|
||||
const configDir = path.dirname(mcpConfigPath);
|
||||
if (!fs.existsSync(configDir)) {
|
||||
fs.mkdirSync(configDir, { recursive: true });
|
||||
}
|
||||
|
||||
// 合并现有配置(保留其他 MCP servers)
|
||||
const newConfig = {
|
||||
...existingConfig,
|
||||
servers: {
|
||||
...existingConfig.servers,
|
||||
...mcpConfig.servers
|
||||
}
|
||||
};
|
||||
|
||||
fs.writeFileSync(mcpConfigPath, JSON.stringify(newConfig, null, 2), 'utf8');
|
||||
console.log('[MCP] 本地 DevStar MCP 配置成功');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('[MCP] 本地 MCP 配置失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
35
src/utils.ts
35
src/utils.ts
@@ -1,12 +1,7 @@
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import * as vscode from 'vscode';
|
||||
import * as os from 'os';
|
||||
import { exec } from 'child_process';
|
||||
|
||||
const {
|
||||
generateKeyPairSync,
|
||||
} = require('node:crypto')
|
||||
const axios = require('axios');
|
||||
const cheerio = require('cheerio');
|
||||
|
||||
@@ -23,36 +18,6 @@ export function isMacOS(): boolean {
|
||||
return os.platform() === 'darwin';
|
||||
}
|
||||
|
||||
|
||||
export function fetch(url: string): Promise<string> {
|
||||
// determine the library to use (based on the url protocol)
|
||||
const lib = url.startsWith('https://') ? https : http;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
lib.get(url, (response) => {
|
||||
// make sure the status code is 200
|
||||
if (response.statusCode !== 200) {
|
||||
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
|
||||
return;
|
||||
}
|
||||
|
||||
let data = '';
|
||||
response.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
response.on('end', () => {
|
||||
resolve(data);
|
||||
});
|
||||
}).on('error', (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export const Sleep = (ms: number) => {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
export async function getVsCodeCommitId(): Promise<string> {
|
||||
if (isLinux() || isMacOS()) {
|
||||
return new Promise<string>((resolve) => {
|
||||
|
||||
Reference in New Issue
Block a user