UNPKG

@synstack/reforge

Version:

Runtime tools for interactive DevX with the ReForge IDE extension

246 lines (242 loc) 8.2 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/vscode.index.ts var vscode_index_exports = {}; __export(vscode_index_exports, { VscodeSymbolKind: () => VscodeSymbolKind, VscodeSymbolTag: () => VscodeSymbolTag, callHierarchyItemSchema: () => callHierarchyItemSchema, executeCommand: () => executeCommand, executeCommandConfig: () => executeCommandConfig, positionSchema: () => positionSchema, rangeSchema: () => rangeSchema }); module.exports = __toCommonJS(vscode_index_exports); // src/vscode.lib.ts var import_v42 = require("zod/v4"); // src/tool.utils.ts var import_json = require("@synstack/json"); var net = __toESM(require("net"), 1); var import_pako = __toESM(require("pako"), 1); var import_v4 = require("zod/v4"); var RESPONSE_SUFFIX = "_RESPONSE"; var memoize = (fn) => { let cache = void 0; return (...args) => { if (cache === void 0) cache = { data: fn(...args) }; return cache.data; }; }; var getIpcClient = memoize(() => { return new Promise((resolve, reject) => { const port = process.env.REFORGE_IPC_PORT; if (!port) throw new Error("No IPC port provided, cannot connect to parent process"); const parsedPort = typeof port === "string" ? parseInt(port) : port; const client = net.connect(parsedPort, "localhost", () => { client.removeListener("error", reject); resolve(client); }); client.once("error", reject); }); }); var baseResponseSchema = import_v4.z.object({ type: import_v4.z.string(), id: import_v4.z.string(), status: import_v4.z.union([import_v4.z.literal("ok"), import_v4.z.literal("error")]) }); var toolFactory = (toolConfig) => { const responseName = `${toolConfig.name}${RESPONSE_SUFFIX}`; const responseSchema = import_v4.z.discriminatedUnion("status", [ import_v4.z.object({ status: import_v4.z.literal("ok"), type: import_v4.z.literal(responseName), id: import_v4.z.string(), data: toolConfig.responseSchema }), import_v4.z.object({ status: import_v4.z.literal("error"), type: import_v4.z.literal(responseName), id: import_v4.z.string(), data: import_v4.z.string() }) ]); const exec = async (data) => { const validatedData = toolConfig.requestSchema ? toolConfig.requestSchema.parse(data) : void 0; const client = await getIpcClient(); const id = crypto.randomUUID(); return new Promise((resolve, reject) => { const errorHandler = (error) => { client.removeListener("error", errorHandler); client.removeListener("data", responseHandler); reject(error); }; const responseHandler = (response) => { const resData = import_json.json.deserialize( import_pako.default.inflate(response, { to: "string" }) ); const baseResponse = baseResponseSchema.parse(resData); if (baseResponse.type === responseName && baseResponse.id === id) { const parsedResponse = responseSchema.parse(resData); client.removeListener("error", errorHandler); client.removeListener("data", responseHandler); if (parsedResponse.status === "ok") resolve(parsedResponse.data); else reject(new Error(parsedResponse.data)); } }; client.once("error", errorHandler); client.on("data", responseHandler); client.write( import_pako.default.deflate( import_json.json.serialize({ type: toolConfig.name, id, data: validatedData }) ) ); }); }; return exec; }; // src/vscode.lib.ts var VscodeSymbolKind = { File: 0, Module: 1, Namespace: 2, Package: 3, Class: 4, Method: 5, Property: 6, Field: 7, Constructor: 8, Enum: 9, Interface: 10, Function: 11, Variable: 12, Constant: 13, String: 14, Number: 15, Boolean: 16, Array: 17, Object: 18, Key: 19, Null: 20, EnumMember: 21, Struct: 22, Event: 23, Operator: 24, TypeParameter: 25 }; var VscodeSymbolTag = { Deprecated: 1 }; var positionSchema = import_v42.z.object({ /** Zero-based line number */ line: import_v42.z.number().int().min(0), /** Zero-based character offset on the line */ character: import_v42.z.number().int().min(0) }); var rangeSchema = import_v42.z.object({ /** The start position of the range */ start: positionSchema, /** The end position of the range */ end: positionSchema }); var callHierarchyItemSchema = import_v42.z.object({ /** The symbol kind of the item */ kind: import_v42.z.number().int().min(0).max(25), /** The name of the item */ name: import_v42.z.string(), /** Additional details about the item */ detail: import_v42.z.string(), /** The URI of the document containing the item */ uri: import_v42.z.string(), /** Optional tags associated with the item */ tags: import_v42.z.array(import_v42.z.number().int()).optional(), /** The full range of the item */ range: rangeSchema, /** The range that should be selected when navigating to the item */ selectionRange: rangeSchema }); var executeCommandConfig = { name: "EXECUTE_COMMAND", requestSchema: import_v42.z.object({ /** The vscode command to execute */ command: import_v42.z.string(), /** List of args to be passed to the command */ args: import_v42.z.array( import_v42.z.discriminatedUnion("type", [ import_v42.z.object({ type: import_v42.z.literal("path"), value: import_v42.z.string() }), import_v42.z.object({ type: import_v42.z.literal("Uri"), value: import_v42.z.string().describe("Absolute path to the file") }), import_v42.z.object({ type: import_v42.z.literal("Range"), value: rangeSchema }), import_v42.z.object({ type: import_v42.z.literal("Position"), value: positionSchema }), import_v42.z.object({ type: import_v42.z.literal("CallHierarchyItem"), value: callHierarchyItemSchema }), import_v42.z.object({ type: import_v42.z.literal("primitive"), value: import_v42.z.union([ import_v42.z.string(), import_v42.z.number(), import_v42.z.boolean(), import_v42.z.record(import_v42.z.string(), import_v42.z.any()), import_v42.z.array(import_v42.z.any()) ]) }) ]) ).optional().default([]) }), responseSchema: import_v42.z.any().optional() }; var executeCommand = toolFactory(executeCommandConfig); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { VscodeSymbolKind, VscodeSymbolTag, callHierarchyItemSchema, executeCommand, executeCommandConfig, positionSchema, rangeSchema }); //# sourceMappingURL=vscode.index.cjs.map