@inspecto-dev/plugin 0.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/index.cjs +1098 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +34 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +1058 -0
- package/dist/index.js.map +1 -0
- package/dist/legacy/rspack/index.cjs +696 -0
- package/dist/legacy/rspack/index.cjs.map +1 -0
- package/dist/legacy/rspack/index.d.cts +10 -0
- package/dist/legacy/rspack/index.d.ts +10 -0
- package/dist/legacy/rspack/index.js +668 -0
- package/dist/legacy/rspack/index.js.map +1 -0
- package/dist/legacy/rspack/loader.cjs +310 -0
- package/dist/legacy/rspack/loader.cjs.map +1 -0
- package/dist/legacy/rspack/loader.d.cts +3 -0
- package/dist/legacy/rspack/loader.d.ts +3 -0
- package/dist/legacy/rspack/loader.js +277 -0
- package/dist/legacy/rspack/loader.js.map +1 -0
- package/dist/rollup.cjs +1098 -0
- package/dist/rollup.cjs.map +1 -0
- package/dist/rollup.d.cts +5 -0
- package/dist/rollup.d.ts +5 -0
- package/dist/rollup.js +1058 -0
- package/dist/rollup.js.map +1 -0
- package/dist/rspack.cjs +1098 -0
- package/dist/rspack.cjs.map +1 -0
- package/dist/rspack.d.cts +5 -0
- package/dist/rspack.d.ts +5 -0
- package/dist/rspack.js +1058 -0
- package/dist/rspack.js.map +1 -0
- package/dist/vite.cjs +1098 -0
- package/dist/vite.cjs.map +1 -0
- package/dist/vite.d.cts +5 -0
- package/dist/vite.d.ts +5 -0
- package/dist/vite.js +1058 -0
- package/dist/vite.js.map +1 -0
- package/dist/webpack.cjs +1098 -0
- package/dist/webpack.cjs.map +1 -0
- package/dist/webpack.d.cts +5 -0
- package/dist/webpack.d.ts +5 -0
- package/dist/webpack.js +1058 -0
- package/dist/webpack.js.map +1 -0
- package/legacy/rspack/index.cjs +1 -0
- package/legacy/rspack/index.d.ts +1 -0
- package/legacy/rspack/index.js +2 -0
- package/legacy/rspack/package.json +5 -0
- package/package.json +97 -0
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
// ../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_typescript@5.9.3_yaml@2.8.3/node_modules/tsup/assets/esm_shims.js
|
|
9
|
+
import path from "path";
|
|
10
|
+
import { fileURLToPath } from "url";
|
|
11
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
12
|
+
var getDirname = () => path.dirname(getFilename());
|
|
13
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
14
|
+
|
|
15
|
+
// src/server/index.ts
|
|
16
|
+
import http from "http";
|
|
17
|
+
import fs3 from "fs";
|
|
18
|
+
import path4 from "path";
|
|
19
|
+
import os2 from "os";
|
|
20
|
+
import { execSync, execFileSync } from "child_process";
|
|
21
|
+
import portfinder from "portfinder";
|
|
22
|
+
import { launchIDE } from "launch-ide";
|
|
23
|
+
|
|
24
|
+
// src/server/snippet.ts
|
|
25
|
+
import * as fs from "fs";
|
|
26
|
+
import * as path2 from "path";
|
|
27
|
+
import * as parser from "@babel/parser";
|
|
28
|
+
import traverse_ from "@babel/traverse";
|
|
29
|
+
var traverse = typeof traverse_ === "function" ? traverse_ : traverse_.default || traverse_;
|
|
30
|
+
var snippetCache = /* @__PURE__ */ new Map();
|
|
31
|
+
var DEFAULT_MAX_LINES = 100;
|
|
32
|
+
var DEFAULT_CONTEXT_LINES_BEFORE = 5;
|
|
33
|
+
async function extractSnippet(req) {
|
|
34
|
+
const { file, line, column, maxLines = DEFAULT_MAX_LINES } = req;
|
|
35
|
+
const absolutePath = path2.resolve(file);
|
|
36
|
+
let stat;
|
|
37
|
+
try {
|
|
38
|
+
stat = await fs.promises.stat(absolutePath);
|
|
39
|
+
} catch {
|
|
40
|
+
throw new Error(`FILE_NOT_FOUND: ${absolutePath}`);
|
|
41
|
+
}
|
|
42
|
+
const mtime = stat.mtimeMs;
|
|
43
|
+
let lines;
|
|
44
|
+
const cached = snippetCache.get(absolutePath);
|
|
45
|
+
if (cached && cached.mtime === mtime) {
|
|
46
|
+
lines = cached.lines;
|
|
47
|
+
} else {
|
|
48
|
+
const source = await fs.promises.readFile(absolutePath, "utf-8");
|
|
49
|
+
lines = source.split("\n");
|
|
50
|
+
snippetCache.set(absolutePath, { mtime, lines });
|
|
51
|
+
}
|
|
52
|
+
let snippetLines;
|
|
53
|
+
let startLine;
|
|
54
|
+
let componentName;
|
|
55
|
+
try {
|
|
56
|
+
const result = extractComponentBoundary(lines.join("\n"), line, column, maxLines);
|
|
57
|
+
snippetLines = result.lines;
|
|
58
|
+
startLine = result.startLine;
|
|
59
|
+
componentName = result.name;
|
|
60
|
+
} catch {
|
|
61
|
+
const before = Math.max(0, line - 1 - DEFAULT_CONTEXT_LINES_BEFORE);
|
|
62
|
+
const after = Math.min(lines.length, before + maxLines);
|
|
63
|
+
snippetLines = lines.slice(before, after);
|
|
64
|
+
startLine = before + 1;
|
|
65
|
+
}
|
|
66
|
+
if (snippetLines.length > maxLines) {
|
|
67
|
+
snippetLines = snippetLines.slice(0, maxLines);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
snippet: snippetLines.join("\n"),
|
|
71
|
+
startLine,
|
|
72
|
+
file: absolutePath,
|
|
73
|
+
...componentName ? { name: componentName } : {}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function extractComponentBoundary(source, targetLine, _targetColumn, maxLines) {
|
|
77
|
+
const ast = parser.parse(source, {
|
|
78
|
+
sourceType: "module",
|
|
79
|
+
plugins: ["jsx", "typescript", "decorators-legacy", "classProperties"],
|
|
80
|
+
errorRecovery: true
|
|
81
|
+
});
|
|
82
|
+
const allLines = source.split("\n");
|
|
83
|
+
let bestStart = 0;
|
|
84
|
+
let bestEnd = allLines.length - 1;
|
|
85
|
+
let bestName;
|
|
86
|
+
traverse(ast, {
|
|
87
|
+
"FunctionDeclaration|FunctionExpression|ArrowFunctionExpression|ClassMethod"(nodePath) {
|
|
88
|
+
const node = nodePath.node;
|
|
89
|
+
if (!node.loc) return;
|
|
90
|
+
const nodeStart = node.loc.start.line;
|
|
91
|
+
const nodeEnd = node.loc.end.line;
|
|
92
|
+
if (targetLine < nodeStart || targetLine > nodeEnd) return;
|
|
93
|
+
if (nodeEnd - nodeStart < bestEnd - bestStart) {
|
|
94
|
+
bestStart = nodeStart - 1;
|
|
95
|
+
bestEnd = nodeEnd - 1;
|
|
96
|
+
bestName = extractFunctionName(nodePath);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
let sliceStart = bestStart;
|
|
101
|
+
let sliceEnd = bestEnd + 1;
|
|
102
|
+
if (sliceEnd - sliceStart > maxLines) {
|
|
103
|
+
const targetIdx = targetLine - 1;
|
|
104
|
+
sliceStart = Math.max(bestStart, targetIdx - Math.floor(maxLines / 3));
|
|
105
|
+
sliceEnd = sliceStart + maxLines;
|
|
106
|
+
if (sliceEnd > bestEnd + 1) {
|
|
107
|
+
sliceEnd = bestEnd + 1;
|
|
108
|
+
sliceStart = Math.max(0, sliceEnd - maxLines);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
lines: allLines.slice(sliceStart, sliceEnd),
|
|
113
|
+
startLine: sliceStart + 1,
|
|
114
|
+
...bestName ? { name: bestName } : {}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function extractFunctionName(nodePath) {
|
|
118
|
+
const node = nodePath.node;
|
|
119
|
+
if (node.type === "FunctionDeclaration" && node.id) {
|
|
120
|
+
return node.id.name;
|
|
121
|
+
}
|
|
122
|
+
const parent = nodePath.parent;
|
|
123
|
+
if ((node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && parent.type === "VariableDeclarator" && parent.id.type === "Identifier") {
|
|
124
|
+
return parent.id.name;
|
|
125
|
+
}
|
|
126
|
+
if (node.type === "ClassMethod" && node.key.type === "Identifier") {
|
|
127
|
+
return node.key.name;
|
|
128
|
+
}
|
|
129
|
+
return void 0;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// src/config.ts
|
|
133
|
+
import fs2 from "fs";
|
|
134
|
+
import path3 from "path";
|
|
135
|
+
import os from "os";
|
|
136
|
+
import { createDefu } from "defu";
|
|
137
|
+
import { DEFAULT_TOOL_MODE, VALID_MODES } from "@inspecto-dev/types";
|
|
138
|
+
var loadedConfig = null;
|
|
139
|
+
var loadedPrompts = null;
|
|
140
|
+
var isWatching = false;
|
|
141
|
+
var arrayReplaceMerge = createDefu((obj, key, val) => {
|
|
142
|
+
if (Array.isArray(val)) {
|
|
143
|
+
obj[key] = val;
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
function resolveConfigRoots(cwd, gitRoot) {
|
|
148
|
+
const roots = [];
|
|
149
|
+
let current = cwd;
|
|
150
|
+
const isUnderOrEqual = current === gitRoot || current.startsWith(gitRoot + path3.sep);
|
|
151
|
+
if (!isUnderOrEqual) {
|
|
152
|
+
if (fs2.existsSync(path3.join(cwd, ".inspecto"))) roots.push(cwd);
|
|
153
|
+
return roots;
|
|
154
|
+
}
|
|
155
|
+
while (true) {
|
|
156
|
+
if (fs2.existsSync(path3.join(current, ".inspecto"))) {
|
|
157
|
+
roots.push(current);
|
|
158
|
+
}
|
|
159
|
+
if (current === gitRoot) break;
|
|
160
|
+
const parent = path3.dirname(current);
|
|
161
|
+
if (parent === current) break;
|
|
162
|
+
current = parent;
|
|
163
|
+
}
|
|
164
|
+
return roots;
|
|
165
|
+
}
|
|
166
|
+
function loadUserConfigSync(force = false, cwd = process.cwd(), gitRoot) {
|
|
167
|
+
if (loadedConfig && !force) return loadedConfig;
|
|
168
|
+
loadedConfig = null;
|
|
169
|
+
const layers = [];
|
|
170
|
+
const roots = resolveConfigRoots(cwd, gitRoot ?? cwd);
|
|
171
|
+
for (const root of roots) {
|
|
172
|
+
layers.push(readJsonSafely(path3.join(root, ".inspecto", "settings.local.json")));
|
|
173
|
+
layers.push(readJsonSafely(path3.join(root, ".inspecto", "settings.json")));
|
|
174
|
+
}
|
|
175
|
+
layers.push(readJsonSafely(path3.join(os.homedir(), ".inspecto", "settings.json")));
|
|
176
|
+
layers.push({ providers: {} });
|
|
177
|
+
const validLayers = layers.filter((l) => l !== null);
|
|
178
|
+
loadedConfig = arrayReplaceMerge(...validLayers);
|
|
179
|
+
return loadedConfig;
|
|
180
|
+
}
|
|
181
|
+
async function loadPromptsConfig(force = false, cwd = process.cwd(), gitRoot) {
|
|
182
|
+
if (loadedPrompts && !force) return loadedPrompts;
|
|
183
|
+
const layers = [];
|
|
184
|
+
const roots = resolveConfigRoots(cwd, gitRoot ?? cwd);
|
|
185
|
+
for (const root of roots) {
|
|
186
|
+
const localPath = path3.join(root, ".inspecto", "prompts.local.json");
|
|
187
|
+
const jsonPath = path3.join(root, ".inspecto", "prompts.json");
|
|
188
|
+
layers.push(readJsonSafely(localPath));
|
|
189
|
+
layers.push(readJsonSafely(jsonPath));
|
|
190
|
+
}
|
|
191
|
+
layers.push(readJsonSafely(path3.join(os.homedir(), ".inspecto", "prompts.json")));
|
|
192
|
+
let finalPrompts = [];
|
|
193
|
+
for (const layer of layers) {
|
|
194
|
+
if (Array.isArray(layer) && layer.length > 0) {
|
|
195
|
+
finalPrompts = layer;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
if (layer && typeof layer === "object" && layer.$replace === true && Array.isArray(layer.items)) {
|
|
199
|
+
finalPrompts = layer;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
loadedPrompts = finalPrompts;
|
|
204
|
+
return loadedPrompts;
|
|
205
|
+
}
|
|
206
|
+
function readJsonSafely(filePath) {
|
|
207
|
+
try {
|
|
208
|
+
if (fs2.existsSync(filePath)) {
|
|
209
|
+
const content = fs2.readFileSync(filePath, "utf-8").trim();
|
|
210
|
+
if (!content) return null;
|
|
211
|
+
const parsed = JSON.parse(content);
|
|
212
|
+
if (!Array.isArray(parsed) && parsed.prompts && Array.isArray(parsed.prompts)) {
|
|
213
|
+
return parsed.prompts;
|
|
214
|
+
}
|
|
215
|
+
return parsed;
|
|
216
|
+
}
|
|
217
|
+
} catch (e) {
|
|
218
|
+
if (e instanceof SyntaxError) {
|
|
219
|
+
console.warn(`[inspecto] Failed to parse config at ${filePath}: Invalid JSON`);
|
|
220
|
+
} else {
|
|
221
|
+
console.warn(`[inspecto] Failed to read config at ${filePath}:`, e);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
function resolveTargetTool(config) {
|
|
227
|
+
if (config.prefer) {
|
|
228
|
+
return config.prefer;
|
|
229
|
+
}
|
|
230
|
+
if (config.providers && Object.keys(config.providers).length > 0) {
|
|
231
|
+
return Object.keys(config.providers)[0];
|
|
232
|
+
}
|
|
233
|
+
return "github-copilot";
|
|
234
|
+
}
|
|
235
|
+
function resolveToolMode(tool, ide, config) {
|
|
236
|
+
let requestedType = void 0;
|
|
237
|
+
if (config.providers && config.providers[tool] && config.providers[tool].type) {
|
|
238
|
+
const type = config.providers[tool].type;
|
|
239
|
+
if (type === "plugin" || type === "cli") {
|
|
240
|
+
requestedType = type;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
requestedType = requestedType ?? DEFAULT_TOOL_MODE[tool];
|
|
244
|
+
const valid = VALID_MODES[tool] || [DEFAULT_TOOL_MODE[tool]];
|
|
245
|
+
return requestedType && valid.includes(requestedType) ? requestedType : valid[0];
|
|
246
|
+
}
|
|
247
|
+
function extractToolOverrides(ide, config) {
|
|
248
|
+
const result = {};
|
|
249
|
+
if (!config.providers) return result;
|
|
250
|
+
for (const [tool, cfg] of Object.entries(config.providers)) {
|
|
251
|
+
if (!cfg) continue;
|
|
252
|
+
const overrides = {
|
|
253
|
+
type: cfg.type || DEFAULT_TOOL_MODE[tool] || "plugin"
|
|
254
|
+
};
|
|
255
|
+
if (cfg.bin) overrides.binaryPath = cfg.bin;
|
|
256
|
+
if (cfg.args) overrides.args = cfg.args;
|
|
257
|
+
if (cfg.cwd) overrides.cwd = cfg.cwd;
|
|
258
|
+
if (cfg.autoSend !== void 0) overrides.autoSend = cfg.autoSend;
|
|
259
|
+
result[tool] = overrides;
|
|
260
|
+
}
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
var watchers = [];
|
|
264
|
+
function watchConfig(onReload, cwd = process.cwd(), gitRoot) {
|
|
265
|
+
if (isWatching) return;
|
|
266
|
+
isWatching = true;
|
|
267
|
+
const watchDirs = [path3.join(os.homedir(), ".inspecto")];
|
|
268
|
+
const roots = resolveConfigRoots(cwd, gitRoot ?? cwd);
|
|
269
|
+
for (const root of roots) {
|
|
270
|
+
watchDirs.push(path3.join(root, ".inspecto"));
|
|
271
|
+
}
|
|
272
|
+
const CONFIG_FILES = /* @__PURE__ */ new Set([
|
|
273
|
+
"settings.json",
|
|
274
|
+
"settings.local.json",
|
|
275
|
+
"prompts.json",
|
|
276
|
+
"prompts.local.json"
|
|
277
|
+
]);
|
|
278
|
+
for (const dir of watchDirs) {
|
|
279
|
+
if (!fs2.existsSync(dir)) continue;
|
|
280
|
+
try {
|
|
281
|
+
const watcher = fs2.watch(dir, async (eventType, filename) => {
|
|
282
|
+
if (!filename || !CONFIG_FILES.has(filename)) return;
|
|
283
|
+
loadedConfig = null;
|
|
284
|
+
loadedPrompts = null;
|
|
285
|
+
loadUserConfigSync(true, cwd, gitRoot);
|
|
286
|
+
await loadPromptsConfig(true, cwd, gitRoot);
|
|
287
|
+
onReload();
|
|
288
|
+
});
|
|
289
|
+
watcher.unref();
|
|
290
|
+
watchers.push(watcher);
|
|
291
|
+
} catch (e) {
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// src/server/index.ts
|
|
297
|
+
var serverState = {
|
|
298
|
+
port: null,
|
|
299
|
+
running: false,
|
|
300
|
+
projectRoot: "",
|
|
301
|
+
configRoot: "",
|
|
302
|
+
cwd: process.cwd()
|
|
303
|
+
};
|
|
304
|
+
var serverInstance = null;
|
|
305
|
+
function resolveProjectRoot() {
|
|
306
|
+
let gitRoot;
|
|
307
|
+
try {
|
|
308
|
+
gitRoot = execSync("git rev-parse --show-toplevel", { encoding: "utf-8" }).trim();
|
|
309
|
+
} catch {
|
|
310
|
+
gitRoot = process.cwd();
|
|
311
|
+
}
|
|
312
|
+
let current = gitRoot;
|
|
313
|
+
while (true) {
|
|
314
|
+
if (fs3.existsSync(path4.join(current, ".inspecto"))) return current;
|
|
315
|
+
const parent = path4.dirname(current);
|
|
316
|
+
if (parent === current) break;
|
|
317
|
+
current = parent;
|
|
318
|
+
}
|
|
319
|
+
return gitRoot;
|
|
320
|
+
}
|
|
321
|
+
function launchURI(uri) {
|
|
322
|
+
try {
|
|
323
|
+
if (process.platform === "darwin") {
|
|
324
|
+
execFileSync("open", [uri]);
|
|
325
|
+
} else if (process.platform === "win32") {
|
|
326
|
+
execFileSync("cmd", ["/c", "start", '""', uri]);
|
|
327
|
+
} else {
|
|
328
|
+
execFileSync("xdg-open", [uri]);
|
|
329
|
+
}
|
|
330
|
+
} catch (e) {
|
|
331
|
+
console.error("[inspecto] Failed to launch URI via execFileSync, falling back to launchIDE:", e);
|
|
332
|
+
launchIDE({ file: uri });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
async function startServer() {
|
|
336
|
+
if (serverState.running && serverState.port !== null) {
|
|
337
|
+
return serverState.port;
|
|
338
|
+
}
|
|
339
|
+
serverState.projectRoot = resolveProjectRoot();
|
|
340
|
+
serverState.configRoot = serverState.projectRoot;
|
|
341
|
+
serverState.cwd = process.cwd();
|
|
342
|
+
portfinder.basePort = 5678;
|
|
343
|
+
const port = await portfinder.getPortPromise();
|
|
344
|
+
watchConfig(
|
|
345
|
+
() => {
|
|
346
|
+
console.log("[inspecto] user config reloaded.");
|
|
347
|
+
},
|
|
348
|
+
serverState.cwd,
|
|
349
|
+
serverState.configRoot
|
|
350
|
+
);
|
|
351
|
+
serverInstance = http.createServer((req, res) => {
|
|
352
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
353
|
+
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
354
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
355
|
+
if (req.method === "OPTIONS") {
|
|
356
|
+
res.writeHead(204);
|
|
357
|
+
res.end();
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const url = new URL(req.url ?? "/", `http://localhost:${port}`);
|
|
361
|
+
handleRequest(url, req, res).catch((err) => {
|
|
362
|
+
console.error("[inspecto] server error:", err);
|
|
363
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
364
|
+
res.end(JSON.stringify({ success: false, error: String(err) }));
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
await new Promise((resolve2, reject) => {
|
|
368
|
+
serverInstance.listen(port, "127.0.0.1", () => {
|
|
369
|
+
serverInstance.unref();
|
|
370
|
+
resolve2();
|
|
371
|
+
});
|
|
372
|
+
serverInstance.once("error", reject);
|
|
373
|
+
});
|
|
374
|
+
serverInstance.on("error", (err) => {
|
|
375
|
+
console.error("[inspecto] persistent server error:", err);
|
|
376
|
+
});
|
|
377
|
+
serverState.port = port;
|
|
378
|
+
serverState.running = true;
|
|
379
|
+
const portFile = path4.join(os2.tmpdir(), "inspecto.port");
|
|
380
|
+
try {
|
|
381
|
+
fs3.writeFileSync(portFile, String(port), "utf-8");
|
|
382
|
+
} catch {
|
|
383
|
+
}
|
|
384
|
+
process.once("exit", () => {
|
|
385
|
+
try {
|
|
386
|
+
fs3.unlinkSync(portFile);
|
|
387
|
+
} catch {
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
console.log(`[inspecto] server running at http://127.0.0.1:${port}`);
|
|
391
|
+
return port;
|
|
392
|
+
}
|
|
393
|
+
async function readBody(req) {
|
|
394
|
+
return new Promise((resolve2, reject) => {
|
|
395
|
+
const chunks = [];
|
|
396
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
397
|
+
req.on("end", () => resolve2(Buffer.concat(chunks).toString("utf-8")));
|
|
398
|
+
req.on("error", reject);
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
async function handleRequest(url, req, res) {
|
|
402
|
+
const pathname = url.pathname;
|
|
403
|
+
if (pathname === "/health" && req.method === "GET") {
|
|
404
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
405
|
+
res.end(JSON.stringify({ ok: true, port: serverState.port }));
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (pathname === "/config" && req.method === "GET") {
|
|
409
|
+
const userConfig = loadUserConfigSync(false, serverState.cwd, serverState.configRoot);
|
|
410
|
+
const promptsConfig = await loadPromptsConfig(false, serverState.cwd, serverState.configRoot);
|
|
411
|
+
const effectiveIde = userConfig.ide ?? "vscode";
|
|
412
|
+
let info;
|
|
413
|
+
if (!serverState.ideInfo) {
|
|
414
|
+
const fallbackTargets = userConfig.providers ? Object.keys(userConfig.providers) : ["claude-code", "gemini", "coco", "codex"];
|
|
415
|
+
info = {
|
|
416
|
+
ide: effectiveIde,
|
|
417
|
+
providers: fallbackTargets.reduce(
|
|
418
|
+
(acc, target) => {
|
|
419
|
+
acc[target] = {
|
|
420
|
+
mode: resolveToolMode(target, effectiveIde, userConfig),
|
|
421
|
+
installed: false
|
|
422
|
+
};
|
|
423
|
+
return acc;
|
|
424
|
+
},
|
|
425
|
+
{}
|
|
426
|
+
)
|
|
427
|
+
};
|
|
428
|
+
} else {
|
|
429
|
+
const { scheme: _scheme, ...rest } = serverState.ideInfo;
|
|
430
|
+
info = rest;
|
|
431
|
+
}
|
|
432
|
+
const resolvedProviders = { ...info.providers };
|
|
433
|
+
for (const tool in resolvedProviders) {
|
|
434
|
+
resolvedProviders[tool].mode = resolveToolMode(tool, info.ide, userConfig);
|
|
435
|
+
}
|
|
436
|
+
const config = {
|
|
437
|
+
...info,
|
|
438
|
+
providers: resolvedProviders,
|
|
439
|
+
providerOverrides: extractToolOverrides(info.ide, userConfig),
|
|
440
|
+
prompts: promptsConfig,
|
|
441
|
+
hotKeys: userConfig.hotKeys,
|
|
442
|
+
includeSnippet: userConfig.includeSnippet
|
|
443
|
+
};
|
|
444
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
445
|
+
res.end(JSON.stringify(config));
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (pathname === "/config" && req.method === "POST") {
|
|
449
|
+
try {
|
|
450
|
+
const body = JSON.parse(await readBody(req));
|
|
451
|
+
serverState.ideInfo = body;
|
|
452
|
+
console.log(`[inspecto] Received IDE info from extension:`, body);
|
|
453
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
454
|
+
res.end(JSON.stringify({ success: true }));
|
|
455
|
+
} catch (e) {
|
|
456
|
+
console.error("[inspecto] Error parsing /config POST request:", e);
|
|
457
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
458
|
+
res.end(JSON.stringify({ error: "Invalid JSON body" }));
|
|
459
|
+
}
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if (pathname === "/open" && req.method === "POST") {
|
|
463
|
+
let body;
|
|
464
|
+
try {
|
|
465
|
+
body = JSON.parse(await readBody(req));
|
|
466
|
+
} catch (e) {
|
|
467
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
468
|
+
res.end(JSON.stringify({ error: "Invalid JSON body" }));
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
const absolutePath = path4.isAbsolute(body.file) ? body.file : path4.resolve(serverState.cwd, body.file);
|
|
472
|
+
const userConfig = loadUserConfigSync(false, serverState.cwd, serverState.configRoot);
|
|
473
|
+
const ide = userConfig.ide ?? "vscode";
|
|
474
|
+
const editorHint = "code";
|
|
475
|
+
launchIDE({
|
|
476
|
+
file: absolutePath,
|
|
477
|
+
line: body.line,
|
|
478
|
+
column: body.column,
|
|
479
|
+
editor: editorHint,
|
|
480
|
+
type: process.platform === "darwin" ? "open" : "exec"
|
|
481
|
+
});
|
|
482
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
483
|
+
res.end(JSON.stringify({ success: true }));
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (pathname === "/snippet" && req.method === "GET") {
|
|
487
|
+
const file = url.searchParams.get("file") ?? "";
|
|
488
|
+
const line = parseInt(url.searchParams.get("line") ?? "1", 10);
|
|
489
|
+
const column = parseInt(url.searchParams.get("column") ?? "1", 10);
|
|
490
|
+
const maxLines = parseInt(url.searchParams.get("maxLines") ?? "100", 10);
|
|
491
|
+
try {
|
|
492
|
+
const absolutePath = path4.isAbsolute(file) ? file : path4.resolve(serverState.cwd, file);
|
|
493
|
+
const result = await extractSnippet({ file: absolutePath, line, column, maxLines });
|
|
494
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
495
|
+
res.end(JSON.stringify(result));
|
|
496
|
+
} catch (err) {
|
|
497
|
+
const message = String(err.message || err);
|
|
498
|
+
const errorCode = message.startsWith("FILE_NOT_FOUND") ? "FILE_NOT_FOUND" : "UNKNOWN";
|
|
499
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
500
|
+
res.end(JSON.stringify({ success: false, error: message, errorCode }));
|
|
501
|
+
}
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (pathname === "/send-to-ai" && req.method === "POST") {
|
|
505
|
+
try {
|
|
506
|
+
const rawBody = await readBody(req);
|
|
507
|
+
const body = JSON.parse(rawBody);
|
|
508
|
+
const result = await dispatchToAi(body);
|
|
509
|
+
res.writeHead(result.success ? 200 : 500, { "Content-Type": "application/json" });
|
|
510
|
+
res.end(JSON.stringify(result));
|
|
511
|
+
} catch (e) {
|
|
512
|
+
console.error("[inspecto] Error parsing /send-to-ai request:", e);
|
|
513
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
514
|
+
res.end(JSON.stringify({ success: false, error: String(e), errorCode: "INTERNAL_ERROR" }));
|
|
515
|
+
}
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
519
|
+
res.end(JSON.stringify({ error: "not found" }));
|
|
520
|
+
}
|
|
521
|
+
async function dispatchToAi(req) {
|
|
522
|
+
const { location, snippet, prompt } = req;
|
|
523
|
+
const userConfig = loadUserConfigSync(false, serverState.cwd, serverState.configRoot);
|
|
524
|
+
const ide = userConfig.ide ?? "vscode";
|
|
525
|
+
const resolvedTarget = resolveTargetTool(userConfig);
|
|
526
|
+
const formattedPrompt = prompt ?? `Please help me with this code from \`${location.file}\` (line ${location.line}):
|
|
527
|
+
|
|
528
|
+
\`\`\`
|
|
529
|
+
${snippet}
|
|
530
|
+
\`\`\`
|
|
531
|
+
`;
|
|
532
|
+
const params = new URLSearchParams();
|
|
533
|
+
params.set("target", resolvedTarget);
|
|
534
|
+
const overrides = extractToolOverrides(ide, userConfig)[resolvedTarget];
|
|
535
|
+
if (overrides) {
|
|
536
|
+
params.set("overrides", JSON.stringify(overrides));
|
|
537
|
+
}
|
|
538
|
+
params.set("prompt", formattedPrompt);
|
|
539
|
+
params.set("file", location.file);
|
|
540
|
+
params.set("line", String(location.line));
|
|
541
|
+
params.set("col", String(location.column));
|
|
542
|
+
params.set("snippet", snippet);
|
|
543
|
+
const scheme = serverState.ideInfo?.scheme || "vscode";
|
|
544
|
+
const uri = `${scheme}://inspecto.inspecto/send?${params.toString()}`;
|
|
545
|
+
console.log(`[inspecto] dispatchToAi: Generated URI: ${uri}`);
|
|
546
|
+
launchURI(uri);
|
|
547
|
+
return { success: true };
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// src/injectors/utils.ts
|
|
551
|
+
import { createRequire } from "module";
|
|
552
|
+
var resolveClientModule = () => {
|
|
553
|
+
try {
|
|
554
|
+
return createRequire(import.meta.url).resolve("@inspecto-dev/core");
|
|
555
|
+
} catch {
|
|
556
|
+
try {
|
|
557
|
+
return __require.resolve("@inspecto-dev/core");
|
|
558
|
+
} catch {
|
|
559
|
+
console.warn("[inspecto] Could not resolve @inspecto-dev/core \u2014 falling back to bare specifier");
|
|
560
|
+
return "@inspecto-dev/core";
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
// src/injectors/webpack.ts
|
|
566
|
+
function getWebpackHtmlScript(serverPort2) {
|
|
567
|
+
return `
|
|
568
|
+
window.__AI_INSPECTOR_PORT__ = ${serverPort2};
|
|
569
|
+
window.addEventListener('load', () => {
|
|
570
|
+
if (window.InspectoClient) {
|
|
571
|
+
window.InspectoClient.mountInspector({
|
|
572
|
+
serverUrl: 'http://127.0.0.1:' + window.__AI_INSPECTOR_PORT__,
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
`;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// src/legacy/rspack/index.ts
|
|
580
|
+
import path5 from "path";
|
|
581
|
+
var _dirname = typeof __dirname !== "undefined" ? __dirname : __dirname;
|
|
582
|
+
var DEFAULT_OPTIONS = {
|
|
583
|
+
include: [],
|
|
584
|
+
exclude: [],
|
|
585
|
+
escapeTags: [],
|
|
586
|
+
pathType: "absolute",
|
|
587
|
+
attributeName: "data-inspecto"
|
|
588
|
+
};
|
|
589
|
+
var serverPort = null;
|
|
590
|
+
var ensureServer = async () => {
|
|
591
|
+
if (serverPort === null) {
|
|
592
|
+
serverPort = await startServer();
|
|
593
|
+
}
|
|
594
|
+
return serverPort;
|
|
595
|
+
};
|
|
596
|
+
var InspectoRspackLegacyPlugin = class {
|
|
597
|
+
constructor(options = {}) {
|
|
598
|
+
this.options = { ...DEFAULT_OPTIONS, ...options };
|
|
599
|
+
}
|
|
600
|
+
apply(compiler) {
|
|
601
|
+
if (process.env["NODE_ENV"] === "production") return;
|
|
602
|
+
compiler.hooks.beforeCompile.tapPromise("inspecto-overlay", async () => {
|
|
603
|
+
await ensureServer().catch(console.error);
|
|
604
|
+
});
|
|
605
|
+
const clientPath = resolveClientModule();
|
|
606
|
+
new compiler.webpack.EntryPlugin(compiler.context, clientPath, {}).apply(compiler);
|
|
607
|
+
compiler.hooks.compilation.tap("inspecto-overlay", (compilation) => {
|
|
608
|
+
const HtmlRspackPlugin = compiler.options.plugins.find(
|
|
609
|
+
(p) => p && p.constructor && p.constructor.name === "HtmlRspackPlugin"
|
|
610
|
+
);
|
|
611
|
+
if (HtmlRspackPlugin && HtmlRspackPlugin.constructor.getHooks) {
|
|
612
|
+
const hooks = HtmlRspackPlugin.constructor.getHooks(compilation);
|
|
613
|
+
hooks.alterAssetTagGroups.tapPromise("inspecto-overlay", async (data) => {
|
|
614
|
+
const port = await ensureServer();
|
|
615
|
+
data.headTags.unshift({
|
|
616
|
+
tagName: "script",
|
|
617
|
+
voidTag: false,
|
|
618
|
+
meta: { plugin: "inspecto-overlay" },
|
|
619
|
+
innerHTML: getWebpackHtmlScript(port)
|
|
620
|
+
});
|
|
621
|
+
return data;
|
|
622
|
+
});
|
|
623
|
+
} else {
|
|
624
|
+
compilation.hooks.processAssets.tapPromise(
|
|
625
|
+
{
|
|
626
|
+
name: "inspecto-overlay",
|
|
627
|
+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE
|
|
628
|
+
},
|
|
629
|
+
async (assets) => {
|
|
630
|
+
const port = await ensureServer();
|
|
631
|
+
const scriptTag = `<script>${getWebpackHtmlScript(port)}</script>`;
|
|
632
|
+
for (const filename of Object.keys(assets)) {
|
|
633
|
+
if (filename.endsWith(".html")) {
|
|
634
|
+
const source = assets[filename].source();
|
|
635
|
+
if (typeof source === "string") {
|
|
636
|
+
const newSource = source.replace("</head>", `${scriptTag}</head>`);
|
|
637
|
+
assets[filename] = new compiler.webpack.sources.RawSource(newSource);
|
|
638
|
+
} else if (Buffer.isBuffer(source)) {
|
|
639
|
+
const sourceStr = source.toString("utf-8");
|
|
640
|
+
const newSource = sourceStr.replace("</head>", `${scriptTag}</head>`);
|
|
641
|
+
assets[filename] = new compiler.webpack.sources.RawSource(newSource);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
const loaderPath = path5.resolve(_dirname, "./loader.cjs");
|
|
650
|
+
compiler.options.module.rules.push({
|
|
651
|
+
test: /\.[jt]sx?$/,
|
|
652
|
+
use: [
|
|
653
|
+
{
|
|
654
|
+
loader: loaderPath,
|
|
655
|
+
options: this.options
|
|
656
|
+
}
|
|
657
|
+
]
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
var rspackPlugin = (options) => new InspectoRspackLegacyPlugin(options);
|
|
662
|
+
var rspack_default = rspackPlugin;
|
|
663
|
+
export {
|
|
664
|
+
InspectoRspackLegacyPlugin,
|
|
665
|
+
rspack_default as default,
|
|
666
|
+
rspackPlugin
|
|
667
|
+
};
|
|
668
|
+
//# sourceMappingURL=index.js.map
|