mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 01:07:46 +00:00
Sync upstream Gemini-CLI v0.8.2 (#838)
This commit is contained in:
@@ -5,30 +5,57 @@
|
||||
*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { IdeContextNotificationSchema } from '@qwen-code/qwen-code-core';
|
||||
import {
|
||||
CloseDiffRequestSchema,
|
||||
IdeContextNotificationSchema,
|
||||
OpenDiffRequestSchema,
|
||||
} from '@qwen-code/qwen-code-core/src/ide/types.js';
|
||||
import { isInitializeRequest } from '@modelcontextprotocol/sdk/types.js';
|
||||
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||
import express, { type Request, type Response } from 'express';
|
||||
import express, {
|
||||
type Request,
|
||||
type Response,
|
||||
type NextFunction,
|
||||
} from 'express';
|
||||
import cors from 'cors';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { type Server as HTTPServer } from 'node:http';
|
||||
import * as path from 'node:path';
|
||||
import * as fs from 'node:fs/promises';
|
||||
import * as os from 'node:os';
|
||||
import { z } from 'zod';
|
||||
import type { z } from 'zod';
|
||||
import type { DiffManager } from './diff-manager.js';
|
||||
import { OpenFilesManager } from './open-files-manager.js';
|
||||
|
||||
class CORSError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'CORSError';
|
||||
}
|
||||
}
|
||||
|
||||
const MCP_SESSION_ID_HEADER = 'mcp-session-id';
|
||||
const IDE_SERVER_PORT_ENV_VAR = 'QWEN_CODE_IDE_SERVER_PORT';
|
||||
const IDE_WORKSPACE_PATH_ENV_VAR = 'QWEN_CODE_IDE_WORKSPACE_PATH';
|
||||
|
||||
function writePortAndWorkspace(
|
||||
context: vscode.ExtensionContext,
|
||||
port: number,
|
||||
portFile: string,
|
||||
log: (message: string) => void,
|
||||
): Promise<void> {
|
||||
interface WritePortAndWorkspaceArgs {
|
||||
context: vscode.ExtensionContext;
|
||||
port: number;
|
||||
portFile: string;
|
||||
ppidPortFile: string;
|
||||
authToken: string;
|
||||
log: (message: string) => void;
|
||||
}
|
||||
|
||||
async function writePortAndWorkspace({
|
||||
context,
|
||||
port,
|
||||
portFile,
|
||||
ppidPortFile,
|
||||
authToken,
|
||||
log,
|
||||
}: WritePortAndWorkspaceArgs): Promise<void> {
|
||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||
const workspacePath =
|
||||
workspaceFolders && workspaceFolders.length > 0
|
||||
@@ -44,13 +71,27 @@ function writePortAndWorkspace(
|
||||
workspacePath,
|
||||
);
|
||||
|
||||
const content = JSON.stringify({
|
||||
port,
|
||||
workspacePath,
|
||||
ppid: process.ppid,
|
||||
authToken,
|
||||
});
|
||||
|
||||
log(`Writing port file to: ${portFile}`);
|
||||
return fs
|
||||
.writeFile(portFile, JSON.stringify({ port, workspacePath }))
|
||||
.catch((err) => {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
log(`Failed to write port to file: ${message}`);
|
||||
});
|
||||
log(`Writing ppid port file to: ${ppidPortFile}`);
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
fs.writeFile(portFile, content).then(() => fs.chmod(portFile, 0o600)),
|
||||
fs
|
||||
.writeFile(ppidPortFile, content)
|
||||
.then(() => fs.chmod(ppidPortFile, 0o600)),
|
||||
]);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
log(`Failed to write port to file: ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function sendIdeContextUpdateNotification(
|
||||
@@ -80,44 +121,85 @@ export class IDEServer {
|
||||
private server: HTTPServer | undefined;
|
||||
private context: vscode.ExtensionContext | undefined;
|
||||
private log: (message: string) => void;
|
||||
private portFile: string;
|
||||
private portFile: string | undefined;
|
||||
private ppidPortFile: string | undefined;
|
||||
private port: number | undefined;
|
||||
private authToken: string | undefined;
|
||||
private transports: { [sessionId: string]: StreamableHTTPServerTransport } =
|
||||
{};
|
||||
private openFilesManager: OpenFilesManager | undefined;
|
||||
diffManager: DiffManager;
|
||||
|
||||
constructor(log: (message: string) => void, diffManager: DiffManager) {
|
||||
this.log = log;
|
||||
this.diffManager = diffManager;
|
||||
this.portFile = path.join(
|
||||
os.tmpdir(),
|
||||
`gemini-ide-server-${process.ppid}.json`,
|
||||
);
|
||||
}
|
||||
|
||||
start(context: vscode.ExtensionContext): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
this.context = context;
|
||||
this.authToken = randomUUID();
|
||||
const sessionsWithInitialNotification = new Set<string>();
|
||||
const transports: { [sessionId: string]: StreamableHTTPServerTransport } =
|
||||
{};
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use(express.json({ limit: '10mb' }));
|
||||
|
||||
app.use(
|
||||
cors({
|
||||
origin: (origin, callback) => {
|
||||
// Only allow non-browser requests with no origin.
|
||||
if (!origin) {
|
||||
return callback(null, true);
|
||||
}
|
||||
return callback(
|
||||
new CORSError('Request denied by CORS policy.'),
|
||||
false,
|
||||
);
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
app.use((req, res, next) => {
|
||||
const host = req.headers.host || '';
|
||||
const allowedHosts = [
|
||||
`localhost:${this.port}`,
|
||||
`127.0.0.1:${this.port}`,
|
||||
];
|
||||
if (!allowedHosts.includes(host)) {
|
||||
return res.status(403).json({ error: 'Invalid Host header' });
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
app.use((req, res, next) => {
|
||||
const authHeader = req.headers.authorization;
|
||||
if (authHeader) {
|
||||
const parts = authHeader.split(' ');
|
||||
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
||||
this.log('Malformed Authorization header. Rejecting request.');
|
||||
res.status(401).send('Unauthorized');
|
||||
return;
|
||||
}
|
||||
const token = parts[1];
|
||||
if (token !== this.authToken) {
|
||||
this.log('Invalid auth token provided. Rejecting request.');
|
||||
res.status(401).send('Unauthorized');
|
||||
return;
|
||||
}
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
const mcpServer = createMcpServer(this.diffManager);
|
||||
|
||||
const openFilesManager = new OpenFilesManager(context);
|
||||
const onDidChangeSubscription = openFilesManager.onDidChange(() => {
|
||||
for (const transport of Object.values(transports)) {
|
||||
sendIdeContextUpdateNotification(
|
||||
transport,
|
||||
this.log.bind(this),
|
||||
openFilesManager,
|
||||
);
|
||||
}
|
||||
this.openFilesManager = new OpenFilesManager(context);
|
||||
const onDidChangeSubscription = this.openFilesManager.onDidChange(() => {
|
||||
this.broadcastIdeContextUpdate();
|
||||
});
|
||||
context.subscriptions.push(onDidChangeSubscription);
|
||||
const onDidChangeDiffSubscription = this.diffManager.onDidChange(
|
||||
(notification) => {
|
||||
for (const transport of Object.values(transports)) {
|
||||
for (const transport of Object.values(this.transports)) {
|
||||
transport.send(notification);
|
||||
}
|
||||
},
|
||||
@@ -130,14 +212,14 @@ export class IDEServer {
|
||||
| undefined;
|
||||
let transport: StreamableHTTPServerTransport;
|
||||
|
||||
if (sessionId && transports[sessionId]) {
|
||||
transport = transports[sessionId];
|
||||
if (sessionId && this.transports[sessionId]) {
|
||||
transport = this.transports[sessionId];
|
||||
} else if (!sessionId && isInitializeRequest(req.body)) {
|
||||
transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
onsessioninitialized: (newSessionId) => {
|
||||
this.log(`New session initialized: ${newSessionId}`);
|
||||
transports[newSessionId] = transport;
|
||||
this.transports[newSessionId] = transport;
|
||||
},
|
||||
});
|
||||
const keepAlive = setInterval(() => {
|
||||
@@ -149,14 +231,14 @@ export class IDEServer {
|
||||
);
|
||||
clearInterval(keepAlive);
|
||||
}
|
||||
}, 60000); // 60 sec
|
||||
}, 30000); // 30 sec
|
||||
|
||||
transport.onclose = () => {
|
||||
clearInterval(keepAlive);
|
||||
if (transport.sessionId) {
|
||||
this.log(`Session closed: ${transport.sessionId}`);
|
||||
sessionsWithInitialNotification.delete(transport.sessionId);
|
||||
delete transports[transport.sessionId];
|
||||
delete this.transports[transport.sessionId];
|
||||
}
|
||||
};
|
||||
mcpServer.connect(transport);
|
||||
@@ -199,13 +281,13 @@ export class IDEServer {
|
||||
const sessionId = req.headers[MCP_SESSION_ID_HEADER] as
|
||||
| string
|
||||
| undefined;
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
if (!sessionId || !this.transports[sessionId]) {
|
||||
this.log('Invalid or missing session ID');
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
return;
|
||||
}
|
||||
|
||||
const transport = transports[sessionId];
|
||||
const transport = this.transports[sessionId];
|
||||
try {
|
||||
await transport.handleRequest(req, res);
|
||||
} catch (error) {
|
||||
@@ -217,11 +299,14 @@ export class IDEServer {
|
||||
}
|
||||
}
|
||||
|
||||
if (!sessionsWithInitialNotification.has(sessionId)) {
|
||||
if (
|
||||
this.openFilesManager &&
|
||||
!sessionsWithInitialNotification.has(sessionId)
|
||||
) {
|
||||
sendIdeContextUpdateNotification(
|
||||
transport,
|
||||
this.log.bind(this),
|
||||
openFilesManager,
|
||||
this.openFilesManager,
|
||||
);
|
||||
sessionsWithInitialNotification.add(sessionId);
|
||||
}
|
||||
@@ -229,34 +314,78 @@ export class IDEServer {
|
||||
|
||||
app.get('/mcp', handleSessionRequest);
|
||||
|
||||
this.server = app.listen(0, async () => {
|
||||
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
|
||||
if (err instanceof CORSError) {
|
||||
res.status(403).json({ error: 'Request denied by CORS policy.' });
|
||||
} else {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
this.server = app.listen(0, '127.0.0.1', async () => {
|
||||
const address = (this.server as HTTPServer).address();
|
||||
if (address && typeof address !== 'string') {
|
||||
this.port = address.port;
|
||||
this.log(`IDE server listening on port ${this.port}`);
|
||||
await writePortAndWorkspace(
|
||||
context,
|
||||
this.port,
|
||||
this.portFile,
|
||||
this.log,
|
||||
this.portFile = path.join(
|
||||
os.tmpdir(),
|
||||
`qwen-code-ide-server-${this.port}.json`,
|
||||
);
|
||||
this.ppidPortFile = path.join(
|
||||
os.tmpdir(),
|
||||
`qwen-code-ide-server-${process.ppid}.json`,
|
||||
);
|
||||
this.log(`IDE server listening on http://127.0.0.1:${this.port}`);
|
||||
|
||||
if (this.authToken) {
|
||||
await writePortAndWorkspace({
|
||||
context,
|
||||
port: this.port,
|
||||
portFile: this.portFile,
|
||||
ppidPortFile: this.ppidPortFile,
|
||||
authToken: this.authToken,
|
||||
log: this.log,
|
||||
});
|
||||
}
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async updateWorkspacePath(): Promise<void> {
|
||||
if (this.context && this.port) {
|
||||
await writePortAndWorkspace(
|
||||
this.context,
|
||||
this.port,
|
||||
this.portFile,
|
||||
this.log,
|
||||
broadcastIdeContextUpdate() {
|
||||
if (!this.openFilesManager) {
|
||||
return;
|
||||
}
|
||||
for (const transport of Object.values(this.transports)) {
|
||||
sendIdeContextUpdateNotification(
|
||||
transport,
|
||||
this.log.bind(this),
|
||||
this.openFilesManager,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async syncEnvVars(): Promise<void> {
|
||||
if (
|
||||
this.context &&
|
||||
this.server &&
|
||||
this.port &&
|
||||
this.portFile &&
|
||||
this.ppidPortFile &&
|
||||
this.authToken
|
||||
) {
|
||||
await writePortAndWorkspace({
|
||||
context: this.context,
|
||||
port: this.port,
|
||||
portFile: this.portFile,
|
||||
ppidPortFile: this.ppidPortFile,
|
||||
authToken: this.authToken,
|
||||
log: this.log,
|
||||
});
|
||||
this.broadcastIdeContextUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
async stop(): Promise<void> {
|
||||
if (this.server) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
@@ -275,10 +404,19 @@ export class IDEServer {
|
||||
if (this.context) {
|
||||
this.context.environmentVariableCollection.clear();
|
||||
}
|
||||
try {
|
||||
await fs.unlink(this.portFile);
|
||||
} catch (_err) {
|
||||
// Ignore errors if the file doesn't exist.
|
||||
if (this.portFile) {
|
||||
try {
|
||||
await fs.unlink(this.portFile);
|
||||
} catch (_err) {
|
||||
// Ignore errors if the file doesn't exist.
|
||||
}
|
||||
}
|
||||
if (this.ppidPortFile) {
|
||||
try {
|
||||
await fs.unlink(this.ppidPortFile);
|
||||
} catch (_err) {
|
||||
// Ignore errors if the file doesn't exist.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,40 +434,27 @@ const createMcpServer = (diffManager: DiffManager) => {
|
||||
{
|
||||
description:
|
||||
'(IDE Tool) Open a diff view to create or modify a file. Returns a notification once the diff has been accepted or rejcted.',
|
||||
inputSchema: z.object({
|
||||
filePath: z.string(),
|
||||
// TODO(chrstn): determine if this should be required or not.
|
||||
newContent: z.string().optional(),
|
||||
}).shape,
|
||||
inputSchema: OpenDiffRequestSchema.shape,
|
||||
},
|
||||
async ({
|
||||
filePath,
|
||||
newContent,
|
||||
}: {
|
||||
filePath: string;
|
||||
newContent?: string;
|
||||
}) => {
|
||||
await diffManager.showDiff(filePath, newContent ?? '');
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: `Showing diff for ${filePath}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
async ({ filePath, newContent }: z.infer<typeof OpenDiffRequestSchema>) => {
|
||||
await diffManager.showDiff(filePath, newContent);
|
||||
return { content: [] };
|
||||
},
|
||||
);
|
||||
server.registerTool(
|
||||
'closeDiff',
|
||||
{
|
||||
description: '(IDE Tool) Close an open diff view for a specific file.',
|
||||
inputSchema: z.object({
|
||||
filePath: z.string(),
|
||||
}).shape,
|
||||
inputSchema: CloseDiffRequestSchema.shape,
|
||||
},
|
||||
async ({ filePath }: { filePath: string }) => {
|
||||
const content = await diffManager.closeDiff(filePath);
|
||||
async ({
|
||||
filePath,
|
||||
suppressNotification,
|
||||
}: z.infer<typeof CloseDiffRequestSchema>) => {
|
||||
const content = await diffManager.closeDiff(
|
||||
filePath,
|
||||
suppressNotification,
|
||||
);
|
||||
const response = { content: content ?? undefined };
|
||||
return {
|
||||
content: [
|
||||
|
||||
Reference in New Issue
Block a user