cwm-assistant 0.3.11 → 0.4.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.
|
@@ -6,9 +6,9 @@ var __export = (target, all) => {
|
|
|
6
6
|
|
|
7
7
|
// src/cli.ts
|
|
8
8
|
import { spawn as spawn8 } from "node:child_process";
|
|
9
|
-
import
|
|
9
|
+
import fs26 from "node:fs";
|
|
10
10
|
import os9 from "node:os";
|
|
11
|
-
import
|
|
11
|
+
import path29 from "node:path";
|
|
12
12
|
import { createInterface } from "node:readline/promises";
|
|
13
13
|
|
|
14
14
|
// ../../node_modules/@sinclair/typebox/build/esm/type/guard/value.mjs
|
|
@@ -2619,7 +2619,7 @@ var Type = type_exports2;
|
|
|
2619
2619
|
|
|
2620
2620
|
// ../../packages/contracts/src/assistant.ts
|
|
2621
2621
|
var ASSISTANT_PROTOCOL = 1;
|
|
2622
|
-
var ASSISTANT_VERSION = "0.
|
|
2622
|
+
var ASSISTANT_VERSION = "0.4.0";
|
|
2623
2623
|
var ASSISTANT_PORT = 46322;
|
|
2624
2624
|
|
|
2625
2625
|
// ../../packages/contracts/src/index.ts
|
|
@@ -2866,6 +2866,158 @@ function verifyGrant(token, publicKey) {
|
|
|
2866
2866
|
return grant;
|
|
2867
2867
|
}
|
|
2868
2868
|
|
|
2869
|
+
// ../../packages/runtime/src/ai/push-plan.ts
|
|
2870
|
+
var endpoint = (baseUrl, route) => {
|
|
2871
|
+
const base = baseUrl.trim().replace(/\/+$/, "");
|
|
2872
|
+
return base + (base.endsWith("/v1") ? "" : "/v1") + route;
|
|
2873
|
+
};
|
|
2874
|
+
async function completeChat(settings, messages, timeout = 9e4) {
|
|
2875
|
+
if (!settings.baseUrl || !settings.model) throw new Error("\u8BF7\u5148\u914D\u7F6E AI \u6A21\u578B");
|
|
2876
|
+
if (!settings.apiKey) throw new Error("\u8BF7\u5148\u914D\u7F6E AI \u6A21\u578B\u5BC6\u94A5");
|
|
2877
|
+
const response = await fetch(endpoint(settings.baseUrl, "/chat/completions"), {
|
|
2878
|
+
method: "POST",
|
|
2879
|
+
headers: { Authorization: "Bearer " + settings.apiKey, "Content-Type": "application/json" },
|
|
2880
|
+
body: JSON.stringify({ model: settings.model, messages, stream: false }),
|
|
2881
|
+
redirect: "error",
|
|
2882
|
+
signal: AbortSignal.timeout(timeout)
|
|
2883
|
+
});
|
|
2884
|
+
if (!response.ok)
|
|
2885
|
+
throw new Error(
|
|
2886
|
+
"\u6A21\u578B\u7F51\u5173\u8BF7\u6C42\u5931\u8D25\uFF08HTTP " + response.status + "\uFF09\uFF0C\u8BF7\u68C0\u67E5\u5730\u5740\u3001\u5BC6\u94A5\u4E0E\u6A21\u578B\u6743\u9650\u662F\u5426\u517C\u5BB9 Chat Completions \u534F\u8BAE"
|
|
2887
|
+
);
|
|
2888
|
+
const result = await response.json();
|
|
2889
|
+
const content = result.choices?.[0]?.message?.content;
|
|
2890
|
+
if (typeof content !== "string" || !content.trim())
|
|
2891
|
+
throw new Error("\u6A21\u578B\u672A\u8FD4\u56DE\u6709\u6548\u6587\u672C\uFF0C\u8BF7\u786E\u8BA4\u7F51\u5173\u517C\u5BB9 Chat Completions \u534F\u8BAE");
|
|
2892
|
+
return content;
|
|
2893
|
+
}
|
|
2894
|
+
var SYSTEM_PROMPT = [
|
|
2895
|
+
"\u4F60\u662F\u53D1\u5E03\u4EA4\u63A5\u6587\u6863\u7684\u6574\u7406\u52A9\u624B\u3002\u7528\u6237\u7ED9\u51FA\u4E00\u4E2A\u7248\u672C\u7684\u9700\u6C42\u6E05\u5355\u3001\u4ED3\u5E93\u5206\u652F\u548C\u9700\u6C42\u6B63\u6587\u3002",
|
|
2896
|
+
"\u8BF7\u53EA\u8F93\u51FA\u4E00\u4E2A JSON \u5BF9\u8C61\uFF0C\u4E0D\u8981\u8F93\u51FA\u89E3\u91CA\u3001\u4E0D\u8981\u4F7F\u7528 markdown \u4EE3\u7801\u5757\uFF0C\u5B57\u6BB5\u5982\u4E0B\uFF1A",
|
|
2897
|
+
'{"wiki": string, "scripts": string, "configs": string}',
|
|
2898
|
+
"- wiki\uFF1A\u9700\u6C42\u6B63\u6587\u4E2D\u51FA\u73B0\u8FC7\u7684 wiki / \u6587\u6863\u94FE\u63A5\uFF0C\u6BCF\u884C\u4E00\u4E2A\uFF1B\u6CA1\u6709\u5C31\u8F93\u51FA\u7A7A\u5B57\u7B26\u4E32\u3002",
|
|
2899
|
+
"- scripts\uFF1A\u9700\u8981\u6267\u884C\u7684 SQL\uFF08MySQL\u3001MongoDB\u3001PostgreSQL\uFF09\u811A\u672C\uFF1B\u6309\u6570\u636E\u5E93\u5206\u8282\uFF0C\u539F\u6587\u7167\u5F55\uFF0C\u6CA1\u6709\u5C31\u8F93\u51FA\u7A7A\u5B57\u7B26\u4E32\u3002",
|
|
2900
|
+
"- configs\uFF1A\u9700\u8981\u4FEE\u6539\u6216\u65B0\u589E\u7684\u914D\u7F6E\u9879\uFF08\u914D\u7F6E\u4E2D\u5FC3\u3001\u73AF\u5883\u53D8\u91CF\u3001\u5F00\u5173\u3001\u5B9A\u65F6\u4EFB\u52A1\u7B49\uFF09\uFF0C\u9010\u6761\u5217\u51FA\uFF1B\u6CA1\u6709\u5C31\u8F93\u51FA\u7A7A\u5B57\u7B26\u4E32\u3002",
|
|
2901
|
+
"\u4E25\u7981\u7F16\u9020\u9700\u6C42\u6B63\u6587\u91CC\u6CA1\u6709\u51FA\u73B0\u7684\u811A\u672C\u3001\u914D\u7F6E\u548C\u94FE\u63A5\u3002\u4E0D\u786E\u5B9A\u5C31\u7559\u7A7A\u3002"
|
|
2902
|
+
].join("\n");
|
|
2903
|
+
function pushPlanPrompt(source) {
|
|
2904
|
+
const requirements = source.requirements.length ? source.requirements.map(
|
|
2905
|
+
(item) => "### \u3010" + item.key + "\u3011" + item.title + "\n" + (item.contentMd?.trim() || "\uFF08\u6B63\u6587\u4E3A\u7A7A\uFF09")
|
|
2906
|
+
).join("\n\n") : "\uFF08\u672C\u7248\u672C\u6CA1\u6709\u5173\u8054\u9700\u6C42\uFF09";
|
|
2907
|
+
const repositories = source.repositories.length ? source.repositories.map((repository) => "- " + repository.alias + "\uFF1A" + repository.branches.join("\u3001")).join("\n") : "\uFF08\u6CA1\u6709\u5173\u8054\u4ED3\u5E93\uFF09";
|
|
2908
|
+
return [
|
|
2909
|
+
"\u7248\u672C\u4FE1\u606F",
|
|
2910
|
+
"\u9879\u76EE\u540D\u79F0\uFF1A" + source.projectName,
|
|
2911
|
+
"\u7248\u672C\u53F7\uFF1A" + source.versionName,
|
|
2912
|
+
"\u7248\u672C\u7C7B\u578B\uFF1A" + source.versionType,
|
|
2913
|
+
"\u53D1\u7248\u65E5\u671F\uFF1A" + (source.releaseDate || "\u5F85\u5B9A"),
|
|
2914
|
+
"",
|
|
2915
|
+
"\u4ED3\u5E93\u4E0E\u5206\u652F",
|
|
2916
|
+
repositories,
|
|
2917
|
+
"",
|
|
2918
|
+
"\u9700\u6C42\u6B63\u6587",
|
|
2919
|
+
requirements
|
|
2920
|
+
].join("\n");
|
|
2921
|
+
}
|
|
2922
|
+
function parsePushPlanSuggestion(content) {
|
|
2923
|
+
const start = content.indexOf("{"), end = content.lastIndexOf("}");
|
|
2924
|
+
if (start < 0 || end <= start) throw new Error("\u6A21\u578B\u8FD4\u56DE\u5185\u5BB9\u4E0D\u662F JSON\uFF0C\u8BF7\u91CD\u8BD5\u6216\u66F4\u6362\u6A21\u578B");
|
|
2925
|
+
let value;
|
|
2926
|
+
try {
|
|
2927
|
+
value = JSON.parse(content.slice(start, end + 1));
|
|
2928
|
+
} catch {
|
|
2929
|
+
throw new Error("\u6A21\u578B\u8FD4\u56DE\u5185\u5BB9\u4E0D\u662F\u5408\u6CD5 JSON\uFF0C\u8BF7\u91CD\u8BD5\u6216\u66F4\u6362\u6A21\u578B");
|
|
2930
|
+
}
|
|
2931
|
+
const record = value ?? {};
|
|
2932
|
+
const text = (key) => typeof record[key] === "string" ? record[key] : "";
|
|
2933
|
+
return {
|
|
2934
|
+
wiki: text("wiki").trim(),
|
|
2935
|
+
scripts: text("scripts").trim(),
|
|
2936
|
+
configs: text("configs").trim()
|
|
2937
|
+
};
|
|
2938
|
+
}
|
|
2939
|
+
async function suggestPushPlan(settings, source) {
|
|
2940
|
+
const content = await completeChat(settings, [
|
|
2941
|
+
{ role: "system", content: SYSTEM_PROMPT },
|
|
2942
|
+
{ role: "user", content: pushPlanPrompt(source) }
|
|
2943
|
+
]);
|
|
2944
|
+
return parsePushPlanSuggestion(content);
|
|
2945
|
+
}
|
|
2946
|
+
|
|
2947
|
+
// src/ai-routes.ts
|
|
2948
|
+
function validBaseUrl(value) {
|
|
2949
|
+
const url = new URL(value.trim());
|
|
2950
|
+
if (url.protocol !== "https:" || url.username || url.password || url.search || url.hash)
|
|
2951
|
+
throw new Error("Base URL \u5FC5\u987B\u662F\u4E0D\u542B\u51ED\u8BC1\u548C\u67E5\u8BE2\u53C2\u6570\u7684 HTTPS \u5730\u5740");
|
|
2952
|
+
return url.toString().replace(/\/+$/, "");
|
|
2953
|
+
}
|
|
2954
|
+
function aiSettings(store) {
|
|
2955
|
+
const saved = store.state.ai || { baseUrl: "", model: "", apiKey: "" };
|
|
2956
|
+
return { baseUrl: saved.baseUrl, model: saved.model, apiKey: saved.apiKey };
|
|
2957
|
+
}
|
|
2958
|
+
function publicSettings(store) {
|
|
2959
|
+
const settings = aiSettings(store);
|
|
2960
|
+
return {
|
|
2961
|
+
baseUrl: settings.baseUrl,
|
|
2962
|
+
model: settings.model,
|
|
2963
|
+
hasApiKey: Boolean(settings.apiKey),
|
|
2964
|
+
configured: Boolean(settings.baseUrl && settings.model && settings.apiKey)
|
|
2965
|
+
};
|
|
2966
|
+
}
|
|
2967
|
+
function registerAiRoutes(app, store) {
|
|
2968
|
+
app.get("/v1/ai/settings", async () => publicSettings(store));
|
|
2969
|
+
app.patch("/v1/ai/settings", async (request) => {
|
|
2970
|
+
const body = request.body ?? {};
|
|
2971
|
+
if (body.baseUrl !== void 0 && typeof body.baseUrl !== "string" || body.model !== void 0 && typeof body.model !== "string" || body.apiKey !== void 0 && typeof body.apiKey !== "string")
|
|
2972
|
+
throw new Error("AI \u914D\u7F6E\u683C\u5F0F\u65E0\u6548");
|
|
2973
|
+
const current = aiSettings(store);
|
|
2974
|
+
const baseUrl = body.baseUrl === void 0 ? current.baseUrl : validBaseUrl(body.baseUrl);
|
|
2975
|
+
if (current.apiKey && baseUrl !== current.baseUrl && !body.apiKey?.trim() && !body.clearApiKey)
|
|
2976
|
+
throw new Error("\u4FEE\u6539\u7F51\u5173\u5730\u5740\u65F6\u8BF7\u91CD\u65B0\u586B\u5199\u5BC6\u94A5\uFF0C\u907F\u514D\u628A\u65E7\u5BC6\u94A5\u53D1\u5F80\u65B0\u5730\u5740");
|
|
2977
|
+
store.state.ai = {
|
|
2978
|
+
baseUrl,
|
|
2979
|
+
model: body.model === void 0 ? current.model : body.model.trim(),
|
|
2980
|
+
apiKey: body.clearApiKey ? "" : body.apiKey?.trim() ? body.apiKey.trim() : current.apiKey
|
|
2981
|
+
};
|
|
2982
|
+
store.save();
|
|
2983
|
+
return publicSettings(store);
|
|
2984
|
+
});
|
|
2985
|
+
app.post("/v1/ai/test", async () => {
|
|
2986
|
+
try {
|
|
2987
|
+
await completeChat(
|
|
2988
|
+
aiSettings(store),
|
|
2989
|
+
[{ role: "user", content: "Reply with OK only." }],
|
|
2990
|
+
3e4
|
|
2991
|
+
);
|
|
2992
|
+
return { success: true };
|
|
2993
|
+
} catch (error) {
|
|
2994
|
+
throw new Error(
|
|
2995
|
+
"\u8FDE\u63A5\u6D4B\u8BD5\u5931\u8D25\uFF1A" + (error instanceof Error ? error.message : "\u8BF7\u68C0\u67E5\u5730\u5740\u3001\u6A21\u578B\u4E0E\u5BC6\u94A5") + "\uFF1B\u8BF7\u6C42\u8D85\u65F6\u4E0A\u9650\u4E3A 30 \u79D2"
|
|
2996
|
+
);
|
|
2997
|
+
}
|
|
2998
|
+
});
|
|
2999
|
+
app.post("/v1/ai/push-plan", async (request) => {
|
|
3000
|
+
const body = request.body ?? {};
|
|
3001
|
+
if (!Array.isArray(body.requirements) || !Array.isArray(body.repositories))
|
|
3002
|
+
throw new Error("\u63A8\u9001\u8BA1\u5212\u8F93\u5165\u65E0\u6548");
|
|
3003
|
+
return suggestPushPlan(aiSettings(store), {
|
|
3004
|
+
projectName: String(body.projectName || ""),
|
|
3005
|
+
versionName: String(body.versionName || ""),
|
|
3006
|
+
versionType: String(body.versionType || ""),
|
|
3007
|
+
releaseDate: body.releaseDate || null,
|
|
3008
|
+
requirements: body.requirements.map((item) => ({
|
|
3009
|
+
key: String(item?.key || ""),
|
|
3010
|
+
title: String(item?.title || ""),
|
|
3011
|
+
contentMd: String(item?.contentMd || "")
|
|
3012
|
+
})),
|
|
3013
|
+
repositories: body.repositories.map((item) => ({
|
|
3014
|
+
alias: String(item?.alias || ""),
|
|
3015
|
+
branches: (item?.branches || []).map(String)
|
|
3016
|
+
}))
|
|
3017
|
+
});
|
|
3018
|
+
});
|
|
3019
|
+
}
|
|
3020
|
+
|
|
2869
3021
|
// src/delete-route.ts
|
|
2870
3022
|
import path13 from "node:path";
|
|
2871
3023
|
|
|
@@ -2913,15 +3065,18 @@ function removeLocalWorkspace(options) {
|
|
|
2913
3065
|
const extraFiles = [];
|
|
2914
3066
|
const warnings = [];
|
|
2915
3067
|
const isFinderMetadata = (p) => path2.basename(p) === ".DS_Store" && fs2.lstatSync(p).isFile();
|
|
2916
|
-
|
|
3068
|
+
const blocking = otherMounts.filter((m) => {
|
|
2917
3069
|
try {
|
|
2918
3070
|
const p = fs2.realpathSync(m.folder_path);
|
|
2919
3071
|
return p === root || inside(root, p);
|
|
2920
3072
|
} catch {
|
|
2921
3073
|
return false;
|
|
2922
3074
|
}
|
|
2923
|
-
})
|
|
2924
|
-
|
|
3075
|
+
});
|
|
3076
|
+
if (blocking.length)
|
|
3077
|
+
throw new Error(
|
|
3078
|
+
blocking.some((m) => m.workspaceId !== id) ? "\u76EE\u5F55\u4ECD\u88AB\u5176\u4ED6 Workspace \u6302\u8F7D\uFF0C\u8BF7\u5148\u89E3\u9664\u5F15\u7528" : "\u8BE5\u76EE\u5F55\u8FD8\u88AB\u540C\u4E00 Workspace \u5728\u5176\u4ED6\u8D26\u53F7\u4E0B\u7684\u672C\u673A\u526F\u672C\u5F15\u7528\uFF0C\u8BF7\u5148\u5207\u6362\u5230\u90A3\u4E2A\u8D26\u53F7\u79FB\u9664"
|
|
3079
|
+
);
|
|
2925
3080
|
const inspect = (directory) => {
|
|
2926
3081
|
for (const entry of fs2.readdirSync(directory, { withFileTypes: true })) {
|
|
2927
3082
|
const p = path2.join(directory, entry.name);
|
|
@@ -3337,6 +3492,13 @@ function macApplication(name) {
|
|
|
3337
3492
|
(candidate) => fs5.existsSync(candidate)
|
|
3338
3493
|
) || "";
|
|
3339
3494
|
}
|
|
3495
|
+
function appBundleBinary(bundle, binary) {
|
|
3496
|
+
const candidate = path6.join(bundle, "Contents/Resources/app/bin", binary);
|
|
3497
|
+
return fs5.existsSync(candidate) ? candidate : "";
|
|
3498
|
+
}
|
|
3499
|
+
function macBundleBinary(app, binary) {
|
|
3500
|
+
return [path6.join("/Applications", app), path6.join(os3.homedir(), "Applications", app)].map((bundle) => appBundleBinary(bundle, binary)).find(Boolean) || "";
|
|
3501
|
+
}
|
|
3340
3502
|
function detectToolCommands() {
|
|
3341
3503
|
if (process.platform === "win32") {
|
|
3342
3504
|
return {
|
|
@@ -3350,8 +3512,8 @@ function detectToolCommands() {
|
|
|
3350
3512
|
};
|
|
3351
3513
|
}
|
|
3352
3514
|
return {
|
|
3353
|
-
vscode: commandOnPath("code") ||
|
|
3354
|
-
cursor: commandOnPath("cursor") ||
|
|
3515
|
+
vscode: commandOnPath("code") || macBundleBinary("Visual Studio Code.app", "code"),
|
|
3516
|
+
cursor: commandOnPath("cursor") || macBundleBinary("Cursor.app", "cursor"),
|
|
3355
3517
|
idea: toolboxIdeaScript() || commandOnPath("idea") || macApplication("IntelliJ IDEA.app"),
|
|
3356
3518
|
claude: commandOnPath("claude"),
|
|
3357
3519
|
codex: commandOnPath("codex"),
|
|
@@ -3521,14 +3683,10 @@ function launchIde(workspaceFile, ide, configuredCommand = "", targetKind = "wor
|
|
|
3521
3683
|
if (ide === "idea") return launchIntelliJIdea(workingDirectory, configuredCommand);
|
|
3522
3684
|
const cursorLike = ide === "cursor" || ide === "cursor-agent";
|
|
3523
3685
|
let command = configuredCommand || (ide === "codex" ? process.env.CWM_CODEX_PATH : cursorLike ? process.env.CWM_CURSOR_PATH : process.env.CWM_VSCODE_PATH) || "";
|
|
3686
|
+
const bundleBinary = cursorLike ? "cursor" : "code";
|
|
3687
|
+
if (command) command = appBundleBinary(command, bundleBinary) || command;
|
|
3524
3688
|
if (!command && ide !== "codex" && process.platform === "darwin") {
|
|
3525
|
-
|
|
3526
|
-
const binary = cursorLike ? "cursor" : "code";
|
|
3527
|
-
const candidates = [
|
|
3528
|
-
path7.join("/Applications", app, "Contents/Resources/app/bin", binary),
|
|
3529
|
-
path7.join(os4.homedir(), "Applications", app, "Contents/Resources/app/bin", binary)
|
|
3530
|
-
];
|
|
3531
|
-
command = candidates.find((candidate) => fs6.existsSync(candidate)) || "";
|
|
3689
|
+
command = macBundleBinary(cursorLike ? "Cursor.app" : "Visual Studio Code.app", bundleBinary);
|
|
3532
3690
|
}
|
|
3533
3691
|
if (!command && process.platform === "win32" && (cursorLike || ide === "vscode" || ide === "codex")) {
|
|
3534
3692
|
command = ide === "codex" ? windowsCodexExecutable() : windowsCliShim(cursorLike ? "cursor" : "vscode");
|
|
@@ -4174,9 +4332,12 @@ function registerDeleteRoute(app, store, jobs) {
|
|
|
4174
4332
|
store,
|
|
4175
4333
|
{ ...grant, userId: mount.userId, workbenchId: mount.workbenchId },
|
|
4176
4334
|
mount.workspaceId
|
|
4177
|
-
).folders.map((folder) => ({
|
|
4335
|
+
).folders.map((folder) => ({
|
|
4336
|
+
folder_path: folder.path,
|
|
4337
|
+
workspaceId: mount.workspaceId
|
|
4338
|
+
}));
|
|
4178
4339
|
} catch {
|
|
4179
|
-
return [{ folder_path: mount.directory }];
|
|
4340
|
+
return [{ folder_path: mount.directory, workspaceId: mount.workspaceId }];
|
|
4180
4341
|
}
|
|
4181
4342
|
});
|
|
4182
4343
|
const removalKey = scopeKey(grant.workbenchId, grant.userId, id);
|
|
@@ -4957,7 +5118,9 @@ async function fetchWorkbenchIdentity(origin) {
|
|
|
4957
5118
|
}
|
|
4958
5119
|
|
|
4959
5120
|
// src/query-routes.ts
|
|
5121
|
+
import fs23 from "node:fs";
|
|
4960
5122
|
import os8 from "node:os";
|
|
5123
|
+
import path25 from "node:path";
|
|
4961
5124
|
|
|
4962
5125
|
// ../../packages/runtime/src/agent-sessions/service.ts
|
|
4963
5126
|
import path23 from "node:path";
|
|
@@ -5658,23 +5821,32 @@ async function readWorkspaceCommits(mount, offset = 0, refresh = false) {
|
|
|
5658
5821
|
// src/query-routes.ts
|
|
5659
5822
|
function registerQueryRoutes(app, store) {
|
|
5660
5823
|
const git4 = (fn) => gitExecution.run({ executable: store.state.git }, fn);
|
|
5824
|
+
const cachePath = (root, target) => {
|
|
5825
|
+
let resolved = root;
|
|
5826
|
+
try {
|
|
5827
|
+
resolved = fs23.realpathSync(root);
|
|
5828
|
+
} catch {
|
|
5829
|
+
}
|
|
5830
|
+
return target.startsWith(path25.join(resolved, ".cwm") + path25.sep);
|
|
5831
|
+
};
|
|
5661
5832
|
app.get("/v1/repositories", async (request) => {
|
|
5662
5833
|
const grant = requestGrant(request);
|
|
5663
5834
|
const prefix = scopeKey(grant.workbenchId, grant.userId, "");
|
|
5664
|
-
|
|
5835
|
+
const root = store.state.root;
|
|
5836
|
+
return Object.entries(store.state.repositories).filter(([key]) => key.startsWith(prefix)).map(([key, path30]) => ({ id: key.slice(prefix.length), path: path30 })).filter((entry) => !cachePath(root, entry.path));
|
|
5665
5837
|
});
|
|
5666
5838
|
app.post("/v1/repositories/inspect", async (request) => {
|
|
5667
5839
|
const body = request.body;
|
|
5668
|
-
const
|
|
5669
|
-
if (!
|
|
5840
|
+
const path30 = body.path;
|
|
5841
|
+
if (!path30 || !(await import("node:path")).default.isAbsolute(path30))
|
|
5670
5842
|
throw new Error("\u8BF7\u9009\u62E9\u7EDD\u5BF9\u8DEF\u5F84");
|
|
5671
5843
|
return git4(async () => ({
|
|
5672
|
-
root: await runGit(
|
|
5673
|
-
remoteUrl: (await runGit(
|
|
5844
|
+
root: await runGit(path30, ["rev-parse", "--show-toplevel"]),
|
|
5845
|
+
remoteUrl: (await runGit(path30, ["remote", "get-url", "origin"]).catch(() => "")).replace(
|
|
5674
5846
|
/(https?:\/\/)[^/@\s]+@/gi,
|
|
5675
5847
|
"$1"
|
|
5676
5848
|
),
|
|
5677
|
-
branches: (await runGit(
|
|
5849
|
+
branches: (await runGit(path30, [
|
|
5678
5850
|
"for-each-ref",
|
|
5679
5851
|
"--format=%(refname:short)",
|
|
5680
5852
|
"refs/heads",
|
|
@@ -5728,11 +5900,11 @@ function registerQueryRoutes(app, store) {
|
|
|
5728
5900
|
}
|
|
5729
5901
|
|
|
5730
5902
|
// src/review-route.ts
|
|
5731
|
-
import
|
|
5903
|
+
import path27 from "node:path";
|
|
5732
5904
|
|
|
5733
5905
|
// ../../packages/runtime/src/reviews/audit.ts
|
|
5734
|
-
import
|
|
5735
|
-
import
|
|
5906
|
+
import fs24 from "node:fs";
|
|
5907
|
+
import path26 from "node:path";
|
|
5736
5908
|
|
|
5737
5909
|
// ../../packages/runtime/src/reviews/branch-scope.ts
|
|
5738
5910
|
var lines = (text) => text ? text.split("\n") : [];
|
|
@@ -5933,14 +6105,14 @@ async function auditLocalVersion(initial, root, runId, inputHash, body, log) {
|
|
|
5933
6105
|
const valid = (value) => typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9._/-]*$/.test(value) && !value.includes("..") && !value.endsWith("/");
|
|
5934
6106
|
if (!valid(production) || !valid(staging) || production === staging)
|
|
5935
6107
|
throw new Error("\u8BF7\u586B\u5199\u4E0D\u540C\u7684\u6709\u6548\u751F\u4EA7\u3001UAT \u5206\u652F\u540D");
|
|
5936
|
-
|
|
6108
|
+
fs24.mkdirSync(root, { recursive: true });
|
|
5937
6109
|
const results = [], folders = [];
|
|
5938
6110
|
const groups = [...new Set(includedMounts.map((mount) => mount.repository_id))];
|
|
5939
6111
|
log("\u5BA1\u67E5\u4ED3\u5E93\u603B\u6570\uFF1A" + groups.length);
|
|
5940
6112
|
excludedRepositories.forEach((repo) => log("\u5DF2\u6392\u9664\uFF1A" + repo.alias + "\uFF08" + repo.reason + "\uFF09"));
|
|
5941
6113
|
for (const repositoryId of groups) {
|
|
5942
6114
|
const entries = includedMounts.filter((entry) => entry.repository_id === repositoryId), repo = entries[0];
|
|
5943
|
-
const directory =
|
|
6115
|
+
const directory = path26.join(root, repositoryId.replace(/[^a-zA-Z0-9_-]/g, "_"));
|
|
5944
6116
|
const result2 = {
|
|
5945
6117
|
repositoryId,
|
|
5946
6118
|
alias: repo.alias,
|
|
@@ -5956,10 +6128,10 @@ async function auditLocalVersion(initial, root, runId, inputHash, body, log) {
|
|
|
5956
6128
|
let remote = repo.remote_url?.trim() || "";
|
|
5957
6129
|
let localOrigin = false;
|
|
5958
6130
|
if (!remote) {
|
|
5959
|
-
if (!repo.canonical_path || !
|
|
6131
|
+
if (!repo.canonical_path || !fs24.existsSync(repo.canonical_path))
|
|
5960
6132
|
throw new Error("\u8BF7\u914D\u7F6E\u8FDC\u7A0B\u4ED3\u5E93\u5730\u5740\u6216\u6709\u6548\u672C\u5730\u6765\u6E90");
|
|
5961
6133
|
remote = await git3(repo.canonical_path, ["config", "--get", "remote.origin.url"]);
|
|
5962
|
-
localOrigin =
|
|
6134
|
+
localOrigin = path26.isAbsolute(remote) && fs24.existsSync(remote);
|
|
5963
6135
|
}
|
|
5964
6136
|
if (!localOrigin && !/^(https:\/\/|ssh:\/\/|[\w.-]+@[\w.-]+:)/.test(remote) || remote.includes("::") || /[\s\x00-\x1f]/.test(remote) || /https?:\/\/[^/]*@/.test(remote))
|
|
5965
6137
|
throw new Error("\u8FDC\u7AEF\u5730\u5740\u65E0\u6548");
|
|
@@ -6021,11 +6193,11 @@ async function auditLocalVersion(initial, root, runId, inputHash, body, log) {
|
|
|
6021
6193
|
);
|
|
6022
6194
|
const stale = false;
|
|
6023
6195
|
const result = stale ? "STALE" : results.some((repo) => ["ERROR", "CONFLICT"].includes(repo.status)) ? "BLOCKED" : uncoveredRequirements.length || results.some((repo) => repo.status === "WARN") ? "WARN" : "PASS";
|
|
6024
|
-
const workspaceFile =
|
|
6196
|
+
const workspaceFile = path26.join(
|
|
6025
6197
|
root,
|
|
6026
6198
|
"version-" + initial.version.name.replace(/[^A-Za-z0-9._-]/g, "-") + ".code-workspace"
|
|
6027
6199
|
);
|
|
6028
|
-
|
|
6200
|
+
fs24.writeFileSync(
|
|
6029
6201
|
workspaceFile,
|
|
6030
6202
|
JSON.stringify(
|
|
6031
6203
|
{ folders, settings: { "codeWorkspace.description": "\u9694\u79BB\u5BA1\u67E5\u5FEB\u7167\uFF1B\u8BF7\u52FF\u7528\u4E8E\u53D1\u5E03\u63A8\u9001" } },
|
|
@@ -6048,8 +6220,8 @@ async function auditLocalVersion(initial, root, runId, inputHash, body, log) {
|
|
|
6048
6220
|
stale
|
|
6049
6221
|
};
|
|
6050
6222
|
const report = readableReport(snapshot, result, createdAt, runId);
|
|
6051
|
-
|
|
6052
|
-
|
|
6223
|
+
fs24.writeFileSync(path26.join(root, "report.md"), report, { mode: 384 });
|
|
6224
|
+
fs24.writeFileSync(path26.join(root, "snapshot.json"), JSON.stringify(snapshot, null, 2), {
|
|
6053
6225
|
mode: 384
|
|
6054
6226
|
});
|
|
6055
6227
|
return { runId, result, report, workspaceFile, snapshot, createdAt };
|
|
@@ -6081,7 +6253,7 @@ function registerReviewRoute(app, store, jobs) {
|
|
|
6081
6253
|
},
|
|
6082
6254
|
(log) => auditLocalVersion(
|
|
6083
6255
|
snapshot,
|
|
6084
|
-
|
|
6256
|
+
path27.join(store.directory, "reviews", body.id),
|
|
6085
6257
|
body.id,
|
|
6086
6258
|
body.inputHash,
|
|
6087
6259
|
body,
|
|
@@ -6094,25 +6266,25 @@ function registerReviewRoute(app, store, jobs) {
|
|
|
6094
6266
|
|
|
6095
6267
|
// src/upgrade.ts
|
|
6096
6268
|
import { spawn as spawn7 } from "node:child_process";
|
|
6097
|
-
import
|
|
6098
|
-
import
|
|
6269
|
+
import fs25 from "node:fs";
|
|
6270
|
+
import path28 from "node:path";
|
|
6099
6271
|
function packageRoot(entry) {
|
|
6100
|
-
let directory =
|
|
6272
|
+
let directory = path28.dirname(path28.resolve(entry));
|
|
6101
6273
|
for (let depth = 0; depth < 4; depth++) {
|
|
6102
|
-
if (
|
|
6103
|
-
directory =
|
|
6274
|
+
if (fs25.existsSync(path28.join(directory, "package.json"))) return directory;
|
|
6275
|
+
directory = path28.dirname(directory);
|
|
6104
6276
|
}
|
|
6105
|
-
return
|
|
6277
|
+
return path28.dirname(path28.dirname(path28.resolve(entry)));
|
|
6106
6278
|
}
|
|
6107
6279
|
function isGlobalInstall(root, prefix) {
|
|
6108
6280
|
if (!prefix) return false;
|
|
6109
|
-
const relative =
|
|
6110
|
-
return Boolean(relative) && !relative.startsWith("..") && !
|
|
6281
|
+
const relative = path28.relative(path28.resolve(prefix), path28.resolve(root));
|
|
6282
|
+
return Boolean(relative) && !relative.startsWith("..") && !path28.isAbsolute(relative);
|
|
6111
6283
|
}
|
|
6112
6284
|
function looksGlobal(root) {
|
|
6113
|
-
const modules =
|
|
6114
|
-
if (
|
|
6115
|
-
return !
|
|
6285
|
+
const modules = path28.dirname(path28.resolve(root));
|
|
6286
|
+
if (path28.basename(modules) !== "node_modules") return false;
|
|
6287
|
+
return !fs25.existsSync(path28.join(path28.dirname(modules), "package.json"));
|
|
6116
6288
|
}
|
|
6117
6289
|
function isNewer(candidate, current) {
|
|
6118
6290
|
const parse = (value) => value.split("-")[0].split(".").map((part) => Number(part) || 0);
|
|
@@ -6133,10 +6305,10 @@ async function latestPublished() {
|
|
|
6133
6305
|
if (!data.version) throw new Error("npm \u8FD4\u56DE\u7684\u7248\u672C\u4FE1\u606F\u65E0\u6548");
|
|
6134
6306
|
return data.version;
|
|
6135
6307
|
}
|
|
6136
|
-
var statusFile = (store) =>
|
|
6308
|
+
var statusFile = (store) => path28.join(store.directory, "upgrade.json");
|
|
6137
6309
|
function readStatus(store) {
|
|
6138
6310
|
try {
|
|
6139
|
-
return JSON.parse(
|
|
6311
|
+
return JSON.parse(fs25.readFileSync(statusFile(store), "utf8"));
|
|
6140
6312
|
} catch {
|
|
6141
6313
|
return void 0;
|
|
6142
6314
|
}
|
|
@@ -6180,7 +6352,7 @@ function registerUpgradeRoutes(app, store, entry, options = {}) {
|
|
|
6180
6352
|
return reply.code(502).send({ error: cause.message });
|
|
6181
6353
|
}
|
|
6182
6354
|
if (!isNewer(latest, ASSISTANT_VERSION)) return { latest, started: false };
|
|
6183
|
-
|
|
6355
|
+
fs25.writeFileSync(
|
|
6184
6356
|
statusFile(store),
|
|
6185
6357
|
JSON.stringify({ target: latest, state: "running", startedAt: (/* @__PURE__ */ new Date()).toISOString() }),
|
|
6186
6358
|
{ mode: 384 }
|
|
@@ -6188,7 +6360,7 @@ function registerUpgradeRoutes(app, store, entry, options = {}) {
|
|
|
6188
6360
|
const child = spawn7(
|
|
6189
6361
|
process.execPath,
|
|
6190
6362
|
[
|
|
6191
|
-
|
|
6363
|
+
path28.join(root, "scripts", "upgrade.mjs"),
|
|
6192
6364
|
"--version",
|
|
6193
6365
|
latest,
|
|
6194
6366
|
"--state-dir",
|
|
@@ -6222,7 +6394,7 @@ async function buildAssistant(store, options = {}, entry, upgrade = {}) {
|
|
|
6222
6394
|
return;
|
|
6223
6395
|
}
|
|
6224
6396
|
const origin = request.headers.origin || "";
|
|
6225
|
-
const
|
|
6397
|
+
const path30 = request.url.split("?")[0];
|
|
6226
6398
|
let workbench = store.state.workbenches[origin];
|
|
6227
6399
|
if (!workbench && trustedOrigin(origin)) {
|
|
6228
6400
|
try {
|
|
@@ -6245,7 +6417,7 @@ async function buildAssistant(store, options = {}, entry, upgrade = {}) {
|
|
|
6245
6417
|
reply.header("Access-Control-Allow-Headers", "Authorization, Content-Type, X-CWM-Binding");
|
|
6246
6418
|
return reply.code(204).send();
|
|
6247
6419
|
}
|
|
6248
|
-
if (
|
|
6420
|
+
if (path30 === "/v1/health" || path30 === "/v1/pairings") return;
|
|
6249
6421
|
try {
|
|
6250
6422
|
const token = request.headers.authorization?.replace(/^Bearer /, "") || "";
|
|
6251
6423
|
const grant = verifyGrant(token, workbench.publicKey);
|
|
@@ -6397,6 +6569,7 @@ async function buildAssistant(store, options = {}, entry, upgrade = {}) {
|
|
|
6397
6569
|
registerLocalRoutes(app, store, options);
|
|
6398
6570
|
if (entry) registerUpgradeRoutes(app, store, entry, upgrade);
|
|
6399
6571
|
registerQueryRoutes(app, store);
|
|
6572
|
+
registerAiRoutes(app, store);
|
|
6400
6573
|
registerAdoptRoutes(app, store, jobs);
|
|
6401
6574
|
registerDeleteRoute(app, store, jobs);
|
|
6402
6575
|
registerReviewRoute(app, store, jobs);
|
|
@@ -6419,7 +6592,7 @@ async function cli(args, entry) {
|
|
|
6419
6592
|
return;
|
|
6420
6593
|
}
|
|
6421
6594
|
const dirIndex = args.indexOf("--state-dir");
|
|
6422
|
-
const directory = dirIndex >= 0 ?
|
|
6595
|
+
const directory = dirIndex >= 0 ? path29.resolve(args[dirIndex + 1]) : defaultStateDirectory();
|
|
6423
6596
|
const command = args[0] || "status";
|
|
6424
6597
|
const store = new StateStore(directory);
|
|
6425
6598
|
const control = async (action) => {
|
|
@@ -6445,7 +6618,7 @@ async function cli(args, entry) {
|
|
|
6445
6618
|
}
|
|
6446
6619
|
if (store.state.autostart) await startAutostart();
|
|
6447
6620
|
else {
|
|
6448
|
-
const log =
|
|
6621
|
+
const log = fs26.openSync(path29.join(directory, "assistant.log"), "a", 384);
|
|
6449
6622
|
const child = spawn8(process.execPath, [entry, "serve", "--state-dir", directory], {
|
|
6450
6623
|
detached: true,
|
|
6451
6624
|
windowsHide: true,
|
|
@@ -6455,7 +6628,7 @@ async function cli(args, entry) {
|
|
|
6455
6628
|
cwd: directory
|
|
6456
6629
|
});
|
|
6457
6630
|
child.unref();
|
|
6458
|
-
|
|
6631
|
+
fs26.closeSync(log);
|
|
6459
6632
|
}
|
|
6460
6633
|
const deadline = Date.now() + 1e4;
|
|
6461
6634
|
while (Date.now() < deadline) {
|
|
@@ -6470,18 +6643,18 @@ async function cli(args, entry) {
|
|
|
6470
6643
|
throw new Error("\u542F\u52A8\u5931\u8D25\uFF0C\u8BF7\u8FD0\u884C cwm-assistant logs \u68C0\u67E5\u7AEF\u53E3\u548C\u8FD0\u884C\u73AF\u5883");
|
|
6471
6644
|
}
|
|
6472
6645
|
if (command === "serve") {
|
|
6473
|
-
const lock =
|
|
6474
|
-
if (
|
|
6475
|
-
const pid = Number(
|
|
6646
|
+
const lock = path29.join(directory, "process.lock");
|
|
6647
|
+
if (fs26.existsSync(lock)) {
|
|
6648
|
+
const pid = Number(fs26.readFileSync(lock, "utf8"));
|
|
6476
6649
|
try {
|
|
6477
6650
|
process.kill(pid, 0);
|
|
6478
6651
|
throw new Error("Workspace \u52A9\u624B\u5DF2\u8FD0\u884C");
|
|
6479
6652
|
} catch (error) {
|
|
6480
6653
|
if (error.code !== "ESRCH") throw error;
|
|
6481
|
-
|
|
6654
|
+
fs26.unlinkSync(lock);
|
|
6482
6655
|
}
|
|
6483
6656
|
}
|
|
6484
|
-
|
|
6657
|
+
fs26.writeFileSync(lock, String(process.pid), { flag: "wx", mode: 384 });
|
|
6485
6658
|
store.reload();
|
|
6486
6659
|
store.recover();
|
|
6487
6660
|
const { app, jobs } = await buildAssistant(store, {}, entry);
|
|
@@ -6505,12 +6678,12 @@ async function cli(args, entry) {
|
|
|
6505
6678
|
return reply.code(400).send({ error: "\u63A7\u5236\u64CD\u4F5C\u65E0\u6548" });
|
|
6506
6679
|
});
|
|
6507
6680
|
app.addHook("onClose", async () => {
|
|
6508
|
-
|
|
6681
|
+
fs26.rmSync(lock, { force: true });
|
|
6509
6682
|
});
|
|
6510
6683
|
try {
|
|
6511
6684
|
await app.listen({ host: "127.0.0.1", port: ASSISTANT_PORT });
|
|
6512
6685
|
} catch (error) {
|
|
6513
|
-
|
|
6686
|
+
fs26.rmSync(lock, { force: true });
|
|
6514
6687
|
throw error;
|
|
6515
6688
|
}
|
|
6516
6689
|
for (const signal of ["SIGTERM", "SIGINT"])
|
|
@@ -6535,7 +6708,7 @@ async function cli(args, entry) {
|
|
|
6535
6708
|
await control("stop").catch(() => {
|
|
6536
6709
|
});
|
|
6537
6710
|
const deadline = Date.now() + 1e4;
|
|
6538
|
-
while (
|
|
6711
|
+
while (fs26.existsSync(path29.join(directory, "process.lock")) && Date.now() < deadline) {
|
|
6539
6712
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
6540
6713
|
}
|
|
6541
6714
|
}
|
|
@@ -6567,9 +6740,9 @@ async function cli(args, entry) {
|
|
|
6567
6740
|
return;
|
|
6568
6741
|
}
|
|
6569
6742
|
if (command === "logs") {
|
|
6570
|
-
const file =
|
|
6743
|
+
const file = path29.join(directory, "assistant.log");
|
|
6571
6744
|
console.log(
|
|
6572
|
-
|
|
6745
|
+
fs26.existsSync(file) ? fs26.readFileSync(file, "utf8").split("\n").slice(-100).join("\n") : "\u6682\u65E0\u65E5\u5FD7"
|
|
6573
6746
|
);
|
|
6574
6747
|
return;
|
|
6575
6748
|
}
|
|
@@ -6584,10 +6757,10 @@ async function cli(args, entry) {
|
|
|
6584
6757
|
await control("stop").catch(() => {
|
|
6585
6758
|
});
|
|
6586
6759
|
const deadline = Date.now() + 1e4;
|
|
6587
|
-
while (
|
|
6760
|
+
while (fs26.existsSync(path29.join(directory, "process.lock")) && Date.now() < deadline) {
|
|
6588
6761
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
6589
6762
|
}
|
|
6590
|
-
if (
|
|
6763
|
+
if (fs26.existsSync(path29.join(directory, "process.lock")))
|
|
6591
6764
|
throw new Error("\u52A9\u624B\u5C1A\u672A\u505C\u6B62\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\uFF1B\u672A\u4FEE\u6539\u672C\u5730\u72B6\u6001");
|
|
6592
6765
|
}
|
|
6593
6766
|
store.reload();
|
|
@@ -6622,11 +6795,11 @@ async function cli(args, entry) {
|
|
|
6622
6795
|
{
|
|
6623
6796
|
node: process.version,
|
|
6624
6797
|
nodePath: process.execPath,
|
|
6625
|
-
entryExists:
|
|
6798
|
+
entryExists: fs26.existsSync(entry),
|
|
6626
6799
|
git: store.state.git,
|
|
6627
6800
|
gitVersion,
|
|
6628
6801
|
platform: process.platform,
|
|
6629
|
-
rootExists: Boolean(store.state.root &&
|
|
6802
|
+
rootExists: Boolean(store.state.root && fs26.existsSync(store.state.root)),
|
|
6630
6803
|
configuredWorkbenches: Object.keys(store.state.workbenches),
|
|
6631
6804
|
autostart: store.state.autostart,
|
|
6632
6805
|
process: await control("status").catch(() => "\u672A\u8FD0\u884C")
|
|
@@ -6667,9 +6840,9 @@ async function cli(args, entry) {
|
|
|
6667
6840
|
}
|
|
6668
6841
|
}
|
|
6669
6842
|
if (rootArg) {
|
|
6670
|
-
if (!
|
|
6843
|
+
if (!path29.isAbsolute(rootArg) || !fs26.statSync(rootArg).isDirectory())
|
|
6671
6844
|
throw new Error("\u5DE5\u4F5C\u76EE\u5F55\u5FC5\u987B\u662F\u5DF2\u5B58\u5728\u7684\u7EDD\u5BF9\u8DEF\u5F84");
|
|
6672
|
-
store.state.root =
|
|
6845
|
+
store.state.root = fs26.realpathSync(rootArg);
|
|
6673
6846
|
}
|
|
6674
6847
|
const auto = wantAutostart || !assumeYes && await ask("\u767B\u5F55\u7535\u8111\u65F6\u81EA\u52A8\u542F\u52A8\uFF1F[Y/n] ") !== "n";
|
|
6675
6848
|
store.save();
|
|
@@ -6699,4 +6872,4 @@ async function cli(args, entry) {
|
|
|
6699
6872
|
export {
|
|
6700
6873
|
cli
|
|
6701
6874
|
};
|
|
6702
|
-
//# sourceMappingURL=cli-
|
|
6875
|
+
//# sourceMappingURL=cli-7UNIFMZL.js.map
|