cwm-assistant 0.3.12 → 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.
|
@@ -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
|
|
|
@@ -6417,6 +6569,7 @@ async function buildAssistant(store, options = {}, entry, upgrade = {}) {
|
|
|
6417
6569
|
registerLocalRoutes(app, store, options);
|
|
6418
6570
|
if (entry) registerUpgradeRoutes(app, store, entry, upgrade);
|
|
6419
6571
|
registerQueryRoutes(app, store);
|
|
6572
|
+
registerAiRoutes(app, store);
|
|
6420
6573
|
registerAdoptRoutes(app, store, jobs);
|
|
6421
6574
|
registerDeleteRoute(app, store, jobs);
|
|
6422
6575
|
registerReviewRoute(app, store, jobs);
|
|
@@ -6719,4 +6872,4 @@ async function cli(args, entry) {
|
|
|
6719
6872
|
export {
|
|
6720
6873
|
cli
|
|
6721
6874
|
};
|
|
6722
|
-
//# sourceMappingURL=cli-
|
|
6875
|
+
//# sourceMappingURL=cli-7UNIFMZL.js.map
|