cwm-assistant 0.3.12 → 0.4.1

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.3.12";
2622
+ var ASSISTANT_VERSION = "0.4.1";
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
 
@@ -5599,6 +5751,28 @@ async function latestComparison(cwd, branch, force) {
5599
5751
  comparisonSyncs.set(key, entry);
5600
5752
  return entry.result;
5601
5753
  }
5754
+ async function branchHasChanges(cwd, branch, compareBranch) {
5755
+ if (!fs22.existsSync(cwd)) return true;
5756
+ const baseline = normalizeCommitCompareBranch(compareBranch) || "production";
5757
+ const target = normalizeCommitCompareBranch(branch);
5758
+ if (!target) return true;
5759
+ try {
5760
+ const base = await latestComparison(cwd, baseline, false);
5761
+ if ("unavailable" in base) return true;
5762
+ const candidates = ["refs/remotes/origin/" + target, "refs/heads/" + target];
5763
+ for (const candidate of candidates) {
5764
+ try {
5765
+ const tip = await git2(cwd, ["rev-parse", "--verify", "--end-of-options", candidate]);
5766
+ const ahead = Number(await git2(cwd, ["rev-list", "--count", base.commit + ".." + tip]));
5767
+ return ahead > 0;
5768
+ } catch {
5769
+ }
5770
+ }
5771
+ return true;
5772
+ } catch {
5773
+ return true;
5774
+ }
5775
+ }
5602
5776
  async function readWorkspaceCommits(mount, offset = 0, refresh = false) {
5603
5777
  const compareBranch = normalizeCommitCompareBranch(mount.commit_compare_branch);
5604
5778
  const empty = {
@@ -5707,6 +5881,23 @@ function registerQueryRoutes(app, store) {
5707
5881
  const { mount, folders } = localWorkspace(store, requestGrant(request), id);
5708
5882
  return { ...mount, folders };
5709
5883
  });
5884
+ app.post("/v1/versions/branch-activity", async (request) => {
5885
+ const grant = requestGrant(request);
5886
+ const body = request.body;
5887
+ if (!Array.isArray(body?.branches)) throw new Error("\u5206\u652F\u5217\u8868\u65E0\u6548");
5888
+ const active = [];
5889
+ for (const item of body.branches) {
5890
+ const repositoryId = String(item?.repositoryId || "");
5891
+ const branch = String(item?.branch || "");
5892
+ if (!repositoryId || !branch) continue;
5893
+ const path30 = store.state.repositories[scopeKey(grant.workbenchId, grant.userId, repositoryId)];
5894
+ const changed = await git4(
5895
+ () => branchHasChanges(path30 || "", branch, String(item?.compareBranch || ""))
5896
+ );
5897
+ if (changed) active.push(repositoryId + "\0" + branch);
5898
+ }
5899
+ return { active };
5900
+ });
5710
5901
  app.get("/v1/workspaces/:id/repositories/:index/commits", async (request) => {
5711
5902
  const { id, index } = request.params;
5712
5903
  const query = request.query;
@@ -5956,6 +6147,7 @@ async function auditLocalVersion(initial, root, runId, inputHash, body, log) {
5956
6147
  fs24.mkdirSync(root, { recursive: true });
5957
6148
  const results = [], folders = [];
5958
6149
  const groups = [...new Set(includedMounts.map((mount) => mount.repository_id))];
6150
+ const emptyRepositories = /* @__PURE__ */ new Set();
5959
6151
  log("\u5BA1\u67E5\u4ED3\u5E93\u603B\u6570\uFF1A" + groups.length);
5960
6152
  excludedRepositories.forEach((repo) => log("\u5DF2\u6392\u9664\uFF1A" + repo.alias + "\uFF08" + repo.reason + "\uFF09"));
5961
6153
  for (const repositoryId of groups) {
@@ -5996,13 +6188,44 @@ async function auditLocalVersion(initial, root, runId, inputHash, body, log) {
5996
6188
  if (!ref) throw new Error(repo.alias + "\uFF1A\u8FDC\u7AEF\u5206\u652F\u4E0D\u5B58\u5728\uFF1A" + clean);
5997
6189
  return ref.sha;
5998
6190
  };
6191
+ const compareName = (repo.compare_branch || "").replace(/^origin\//, "") || production;
6192
+ const baseSha = await resolveBranch(compareName).catch(() => "");
6193
+ const activeEntries = [];
6194
+ for (const entry of entries) {
6195
+ if (!baseSha) {
6196
+ activeEntries.push(entry);
6197
+ continue;
6198
+ }
6199
+ const tip = await resolveBranch(entry.branch).catch(() => "");
6200
+ const ahead = tip ? Number(
6201
+ await git3(directory, ["rev-list", "--count", baseSha + ".." + tip]).catch(() => "1")
6202
+ ) : 1;
6203
+ if (ahead > 0) activeEntries.push(entry);
6204
+ else
6205
+ log(entry.alias + "\uFF1A" + entry.branch + " \u76F8\u5BF9 " + compareName + " \u65E0\u6539\u52A8\uFF0C\u4E0D\u7EB3\u5165\u5BA1\u67E5");
6206
+ }
6207
+ if (!activeEntries.length) {
6208
+ results.splice(results.indexOf(result2), 1);
6209
+ emptyRepositories.add(repositoryId);
6210
+ const folderIndex = folders.findIndex((folder) => folder.name === repo.alias);
6211
+ if (folderIndex >= 0) folders.splice(folderIndex, 1);
6212
+ excludedRepositories.push({
6213
+ repositoryId,
6214
+ alias: repo.alias,
6215
+ reason: "\u5206\u652F\u76F8\u5BF9 " + compareName + " \u65E0\u6539\u52A8"
6216
+ });
6217
+ log("\u5DF2\u6392\u9664\uFF1A" + repo.alias + "\uFF08\u5206\u652F\u76F8\u5BF9 " + compareName + " \u65E0\u6539\u52A8\uFF09");
6218
+ continue;
6219
+ }
6220
+ entries.length = 0;
6221
+ entries.push(...activeEntries);
5999
6222
  const p = await resolveBranch(production), s = await resolveBranch(staging);
6000
6223
  result2.refs = { production: p, staging: s };
6001
- for (const branch of [...new Set(entries.map((entry) => entry.branch))])
6224
+ for (const branch of [...new Set(activeEntries.map((entry) => entry.branch))])
6002
6225
  result2.branches.push({
6003
6226
  name: branch,
6004
6227
  sha: await resolveBranch(branch),
6005
- requirementIds: entries.filter((entry) => entry.branch === branch).map((entry) => entry.requirement_id)
6228
+ requirementIds: activeEntries.filter((entry) => entry.branch === branch).map((entry) => entry.requirement_id)
6006
6229
  });
6007
6230
  log("\u5F00\u59CB\u5B8C\u6574\u6027\u68C0\u67E5\uFF1A" + repo.alias);
6008
6231
  let mergeConflict = false;
@@ -6036,8 +6259,11 @@ async function auditLocalVersion(initial, root, runId, inputHash, body, log) {
6036
6259
  log(repo.alias + "\uFF1A\u68C0\u67E5\u5931\u8D25");
6037
6260
  }
6038
6261
  }
6262
+ const coveredMounts = initial.mounts.filter(
6263
+ (mount) => !emptyRepositories.has(mount.repository_id)
6264
+ );
6039
6265
  const uncoveredRequirements = initial.requirements.filter(
6040
- (requirement) => !initial.mounts.some((mount) => mount.requirement_id === requirement.id)
6266
+ (requirement) => !coveredMounts.some((mount) => mount.requirement_id === requirement.id)
6041
6267
  );
6042
6268
  const stale = false;
6043
6269
  const result = stale ? "STALE" : results.some((repo) => ["ERROR", "CONFLICT"].includes(repo.status)) ? "BLOCKED" : uncoveredRequirements.length || results.some((repo) => repo.status === "WARN") ? "WARN" : "PASS";
@@ -6053,6 +6279,7 @@ async function auditLocalVersion(initial, root, runId, inputHash, body, log) {
6053
6279
  2
6054
6280
  )
6055
6281
  );
6282
+ if (!results.length) throw new Error("\u6240\u6709\u5173\u8054\u5206\u652F\u76F8\u5BF9\u5BF9\u6BD4\u5206\u652F\u5747\u65E0\u6539\u52A8\uFF0C\u65E0\u53EF\u5BA1\u67E5\u7684\u4ED3\u5E93");
6056
6283
  const snapshot = {
6057
6284
  type: "release-integrity",
6058
6285
  formatVersion: 4,
@@ -6417,6 +6644,7 @@ async function buildAssistant(store, options = {}, entry, upgrade = {}) {
6417
6644
  registerLocalRoutes(app, store, options);
6418
6645
  if (entry) registerUpgradeRoutes(app, store, entry, upgrade);
6419
6646
  registerQueryRoutes(app, store);
6647
+ registerAiRoutes(app, store);
6420
6648
  registerAdoptRoutes(app, store, jobs);
6421
6649
  registerDeleteRoute(app, store, jobs);
6422
6650
  registerReviewRoute(app, store, jobs);
@@ -6719,4 +6947,4 @@ async function cli(args, entry) {
6719
6947
  export {
6720
6948
  cli
6721
6949
  };
6722
- //# sourceMappingURL=cli-33RDLVJY.js.map
6950
+ //# sourceMappingURL=cli-KVOOANBW.js.map