@llms-sdk/diff-server
Version:
LLMS SDK Diff server for file diff viewing and collaboration
214 lines (212 loc) • 6.88 kB
JavaScript
// ../../../../node_modules/tsup/assets/esm_shims.js
import path from "path";
import { fileURLToPath } from "url";
var getFilename = () => fileURLToPath(import.meta.url);
var getDirname = () => path.dirname(getFilename());
var __dirname = /* @__PURE__ */ getDirname();
// src/test-cli.ts
import { spawn } from "child_process";
import { StdioClientTransport, Client } from "@modelcontextprotocol/sdk/client/index.js";
import * as readline from "readline";
import * as path2 from "path";
var LlmsSdkDiffTestCLI = class {
client;
transport;
rl;
async start() {
console.log("\u{1F9EA} LLMS SDK Diff Test CLI");
console.log("====================");
try {
await this.connectToServer();
await this.startInteractiveSession();
} catch (error) {
console.error("\u274C Failed to start test CLI:", error);
process.exit(1);
}
}
async connectToServer() {
console.log("\u{1F50C} Connecting to LLMS SDK Diff server...");
const serverPath = path2.join(__dirname, "index.js");
const serverProcess = spawn("node", [serverPath], {
stdio: ["pipe", "pipe", "inherit"]
});
this.transport = new StdioClientTransport({
reader: serverProcess.stdout,
writer: serverProcess.stdin
});
this.client = new Client({ name: "llms-sdk-diff-test-cli", version: "1.0.0" }, { capabilities: {} });
await this.client.connect(this.transport);
const tools = await this.client.listTools();
console.log("\u2705 Connected! Available tools:");
tools.tools.forEach((tool) => {
console.log(` \u2022 ${tool.name}: ${tool.description}`);
});
console.log("");
}
async startInteractiveSession() {
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "> "
});
console.log("Commands:");
console.log(" open <path> <panel> [branch] - Open file in panel (0=left, 1=right)");
console.log(" close <path> - Close file");
console.log(" highlight <path> <start> [end] - Highlight lines");
console.log(" refresh - Refresh all files");
console.log(" help - Show this help");
console.log(" exit - Exit CLI");
console.log("");
this.rl.prompt();
this.rl.on("line", async (input) => {
const line = input.trim();
if (!line) {
this.rl.prompt();
return;
}
try {
await this.handleCommand(line);
} catch (error) {
console.error("\u274C Command failed:", error);
}
this.rl.prompt();
});
this.rl.on("close", () => {
console.log("\n\u{1F44B} Goodbye!");
this.cleanup();
process.exit(0);
});
}
async handleCommand(command) {
const parts = command.split(" ");
const cmd = parts[0].toLowerCase();
switch (cmd) {
case "help":
this.showHelp();
break;
case "open":
await this.handleOpen(parts.slice(1));
break;
case "close":
await this.handleClose(parts.slice(1));
break;
case "highlight":
await this.handleHighlight(parts.slice(1));
break;
case "refresh":
await this.handleRefresh();
break;
case "exit":
this.rl.close();
break;
default:
console.log(`\u274C Unknown command: ${cmd}`);
this.showHelp();
}
}
showHelp() {
console.log("Available commands:");
console.log(" open <path> <panel> [branch] - Open file in panel (0=left, 1=right)");
console.log(" close <path> - Close file");
console.log(" highlight <path> <start> [end] - Highlight lines");
console.log(" refresh - Refresh all files");
console.log(" help - Show this help");
console.log(" exit - Exit CLI");
}
async handleOpen(args) {
if (args.length < 2) {
console.log("\u274C Usage: open <path> <panel> [branch]");
return;
}
const [absolutePath, panelStr, branch] = args;
const panel = parseInt(panelStr);
if (panel !== 0 && panel !== 1) {
console.log("\u274C Panel must be 0 (left) or 1 (right)");
return;
}
const resolvedPath = path2.resolve(absolutePath);
const params = {
absolutePath: resolvedPath,
panel,
...branch && { branch }
};
console.log(`\u{1F4C2} Opening: ${resolvedPath} in panel ${panel}${branch ? ` (vs ${branch})` : ""}`);
const result = await this.client.callTool({
name: "open",
arguments: params
});
if (result.content && result.content[0]) {
console.log(result.content[0].text);
}
}
async handleClose(args) {
if (args.length < 1) {
console.log("\u274C Usage: close <path>");
return;
}
const absolutePath = path2.resolve(args[0]);
console.log(`\u{1F5D1}\uFE0F Closing: ${absolutePath}`);
const result = await this.client.callTool({
name: "close",
arguments: { absolutePath }
});
if (result.content && result.content[0]) {
console.log(result.content[0].text);
}
}
async handleHighlight(args) {
if (args.length < 2) {
console.log("\u274C Usage: highlight <path> <start> [end]");
return;
}
const [pathArg, startStr, endStr] = args;
const absolutePath = path2.resolve(pathArg);
const startLine = parseInt(startStr);
const endLine = endStr ? parseInt(endStr) : void 0;
if (isNaN(startLine) || startLine < 1) {
console.log("\u274C Start line must be a positive number");
return;
}
if (endLine !== void 0 && (isNaN(endLine) || endLine < startLine)) {
console.log("\u274C End line must be >= start line");
return;
}
const lineRange = endLine ? `${startLine}-${endLine}` : `${startLine}`;
console.log(`\u{1F3AF} Highlighting: ${absolutePath} lines ${lineRange}`);
const params = {
absolutePath,
startLine,
...endLine && { endLine }
};
const result = await this.client.callTool({
name: "highlight",
arguments: params
});
if (result.content && result.content[0]) {
console.log(result.content[0].text);
}
}
async handleRefresh() {
console.log("\u{1F504} Refreshing all files...");
const result = await this.client.callTool({
name: "refresh",
arguments: {}
});
if (result.content && result.content[0]) {
console.log(result.content[0].text);
}
}
cleanup() {
try {
this.transport?.close();
} catch (error) {
console.error("Error during cleanup:", error);
}
}
};
if (import.meta.url === `file://${process.argv[1]}`) {
const cli = new LlmsSdkDiffTestCLI();
cli.start().catch(console.error);
}
//# sourceMappingURL=test-cli.js.map