dsh-mcviseron-plugin 0.6.4 → 0.6.6

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.
@@ -7,6 +7,224 @@ import z from "@deepseek-ai/schemastery";
7
7
  // src/components/auto-review-fix/index.ts
8
8
  import { randomUUID } from "node:crypto";
9
9
 
10
+ // src/components/auto-review-fix/review-context.ts
11
+ import { appendFileSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+ var REVIEW_MARKER = "REVIEW_POLICY";
15
+ var REVIEW_MARKER_2 = "final authorization reviewer";
16
+ var HISTORY_HEADER = "FILTERED_HISTORY\n\n";
17
+ var PROJECT_HEADER = "PROJECT_INSTRUCTIONS\n\n";
18
+ var ACTION_HEADER = "\n\nPENDING_ACTION\n\n";
19
+ var USER_TEXT_CHARS = 4e3;
20
+ var TOOL_ARGUMENT_CHARS = 2e3;
21
+ var PROJECT_MAX_CHARS = 4e4;
22
+ function record(value) {
23
+ return value !== null && typeof value === "object" && !Array.isArray(value);
24
+ }
25
+ function textOf(message) {
26
+ if (!record(message) || !Array.isArray(message.content)) return void 0;
27
+ const texts = [];
28
+ for (const block of message.content) {
29
+ if (!record(block) || block.type !== "text" || typeof block.text !== "string") return void 0;
30
+ texts.push(block.text);
31
+ }
32
+ return texts.join("");
33
+ }
34
+ function isAutoReviewRequest(options) {
35
+ if (!record(options)) return false;
36
+ const system = options.system;
37
+ if (typeof system !== "string" || !system.includes(REVIEW_MARKER) || !system.includes(REVIEW_MARKER_2)) return false;
38
+ if (!Array.isArray(options.messages) || options.messages.length === 0) return false;
39
+ const text = textOf(options.messages[0]);
40
+ return text !== void 0 && text.includes(HISTORY_HEADER) && text.includes(ACTION_HEADER);
41
+ }
42
+ function truncateText(text, limit) {
43
+ if (text.length <= limit) return text;
44
+ return text.slice(0, limit) + "\n\u2026[" + String(text.length - limit) + " characters omitted]";
45
+ }
46
+ function compactBlock(block) {
47
+ if (!record(block)) return { type: "text", text: "[non-text history block omitted from reviewer context]" };
48
+ if (block.type === "text" && typeof block.text === "string") {
49
+ return { ...block, text: truncateText(block.text, USER_TEXT_CHARS) };
50
+ }
51
+ return {
52
+ type: "text",
53
+ text: "[" + String(block.type ?? "attachment") + " omitted from reviewer context]"
54
+ };
55
+ }
56
+ function compactEntry(entry) {
57
+ if (!record(entry)) return { kind: "summary", role: "fact", content: [{ type: "text", text: "[invalid history entry omitted]" }] };
58
+ if (entry.kind === "user-message" && Array.isArray(entry.content)) {
59
+ return { ...entry, content: entry.content.map(compactBlock) };
60
+ }
61
+ if (entry.kind === "tool-call") {
62
+ const value = entry.arguments;
63
+ if (typeof value === "string") {
64
+ return { ...entry, arguments: truncateText(value, TOOL_ARGUMENT_CHARS) };
65
+ }
66
+ if (value !== void 0) {
67
+ let rendered;
68
+ try {
69
+ rendered = JSON.stringify(value) ?? "";
70
+ } catch {
71
+ rendered = "";
72
+ }
73
+ return { ...entry, arguments: truncateText(rendered, TOOL_ARGUMENT_CHARS) };
74
+ }
75
+ }
76
+ return entry;
77
+ }
78
+ function protectedEntry(entry) {
79
+ if (!record(entry)) return false;
80
+ return entry.role === "human-instruction" || entry.role === "direct-parent-instruction";
81
+ }
82
+ function summaryEntry(omitted, originalChars) {
83
+ return {
84
+ kind: "summary",
85
+ role: "fact",
86
+ content: [{
87
+ type: "text",
88
+ text: "[auto-review-fix] " + String(omitted) + " earlier history entries omitted from the reviewer context (original history " + String(originalChars) + " chars). Current action and human instructions are retained."
89
+ }]
90
+ };
91
+ }
92
+ function jsonLength(value) {
93
+ try {
94
+ return JSON.stringify(value)?.length ?? 0;
95
+ } catch {
96
+ return Number.MAX_SAFE_INTEGER;
97
+ }
98
+ }
99
+ function boundEntries(entries, maxChars, keepRecent) {
100
+ const normalized = entries.map(compactEntry);
101
+ const originalChars = jsonLength(normalized);
102
+ const recentStart = Math.max(0, normalized.length - Math.max(1, keepRecent));
103
+ const selected = normalized.filter((entry, index) => protectedEntry(entry) || index >= recentStart);
104
+ let omitted = normalized.length - selected.length;
105
+ while (selected.length > 1 && jsonLength(selected) > maxChars) {
106
+ const at = selected.findIndex((entry) => !protectedEntry(entry));
107
+ if (at < 0) selected.shift();
108
+ else selected.splice(at, 1);
109
+ omitted += 1;
110
+ }
111
+ if (omitted > 0) selected.unshift(summaryEntry(omitted, originalChars));
112
+ return selected;
113
+ }
114
+ function parseArray(text) {
115
+ try {
116
+ const value = JSON.parse(text);
117
+ return Array.isArray(value) ? value : void 0;
118
+ } catch {
119
+ return void 0;
120
+ }
121
+ }
122
+ function logTrim(originalChars, nextChars, originalEntries, nextEntries) {
123
+ try {
124
+ appendFileSync(
125
+ join(tmpdir(), "dsh-mcviseron-auto-review.log"),
126
+ (/* @__PURE__ */ new Date()).toISOString() + " review context trimmed: " + String(originalChars) + " -> " + String(nextChars) + " chars, history " + String(originalEntries) + " -> " + String(nextEntries) + "\n",
127
+ "utf8"
128
+ );
129
+ } catch {
130
+ }
131
+ }
132
+ function trimReviewRequest(options, settings) {
133
+ if (!settings.enabled || !isAutoReviewRequest(options)) return options;
134
+ const messages = options.messages;
135
+ const message = messages[0];
136
+ const text = textOf(message);
137
+ if (text === void 0) return options;
138
+ const projectAt = text.indexOf(PROJECT_HEADER);
139
+ const historyAt = text.indexOf(HISTORY_HEADER, projectAt + 1);
140
+ const actionAt = text.indexOf(ACTION_HEADER, historyAt + 1);
141
+ if (projectAt < 0 || historyAt < 0 || actionAt < 0) return options;
142
+ const projectJson = text.slice(projectAt + PROJECT_HEADER.length, historyAt - 2);
143
+ const historyJson = text.slice(historyAt + HISTORY_HEADER.length, actionAt);
144
+ const project = parseArray(projectJson);
145
+ const history = parseArray(historyJson);
146
+ if (project === void 0 || history === void 0) return options;
147
+ const nextProject = boundEntries(project, PROJECT_MAX_CHARS, 4);
148
+ const nextHistory = boundEntries(history, settings.maxHistoryChars, settings.keepRecentEntries);
149
+ const nextText = text.slice(0, projectAt) + PROJECT_HEADER + JSON.stringify(nextProject) + HISTORY_HEADER + JSON.stringify(nextHistory) + text.slice(actionAt);
150
+ if (nextText === text) return options;
151
+ logTrim(text.length, nextText.length, history.length, nextHistory.length);
152
+ const content = [{ type: "text", text: nextText }];
153
+ return {
154
+ ...options,
155
+ messages: [
156
+ { ...message, content },
157
+ ...messages.slice(1)
158
+ ]
159
+ };
160
+ }
161
+ var guardedLlms = /* @__PURE__ */ new WeakMap();
162
+ function whenService(ctx, name, run) {
163
+ try {
164
+ const inject = ctx.inject;
165
+ if (typeof inject === "function") {
166
+ inject.call(ctx, [name], run);
167
+ return;
168
+ }
169
+ const get = ctx.get;
170
+ if (typeof get === "function" && get.call(ctx, name) !== void 0) run(ctx);
171
+ } catch {
172
+ }
173
+ }
174
+ function serviceOf(scope, name) {
175
+ try {
176
+ const direct = scope[name];
177
+ if (direct !== void 0) return direct;
178
+ } catch {
179
+ }
180
+ try {
181
+ const get = scope.get;
182
+ return typeof get === "function" ? get.call(scope, name) : void 0;
183
+ } catch {
184
+ return void 0;
185
+ }
186
+ }
187
+ function effectOf(scope, setup, label) {
188
+ try {
189
+ const effect = scope.effect;
190
+ if (typeof effect === "function") effect.call(scope, setup, label);
191
+ } catch {
192
+ }
193
+ }
194
+ function installReviewContextGuard(ctx, settings) {
195
+ if (!settings.enabled) return;
196
+ whenService(ctx, "llm", (scope) => {
197
+ const llm = serviceOf(scope, "llm");
198
+ if (llm === null || typeof llm !== "object" || typeof llm.stream !== "function") return;
199
+ const existing = guardedLlms.get(llm);
200
+ if (existing !== void 0) {
201
+ existing.settings = settings;
202
+ return;
203
+ }
204
+ const original = llm.stream;
205
+ const state = { settings };
206
+ const wrapper = function(options) {
207
+ let next = options;
208
+ try {
209
+ next = trimReviewRequest(options, state.settings);
210
+ } catch {
211
+ next = options;
212
+ }
213
+ return original.call(this, next);
214
+ };
215
+ try {
216
+ llm.stream = wrapper;
217
+ guardedLlms.set(llm, state);
218
+ } catch {
219
+ return;
220
+ }
221
+ effectOf(scope, () => () => {
222
+ if (llm.stream === wrapper) llm.stream = original;
223
+ guardedLlms.delete(llm);
224
+ }, "mcviseron: restore llm.stream");
225
+ });
226
+ }
227
+
10
228
  // src/components/auto-review-fix/translate.ts
11
229
  var TRANSLATION_LABEL = "\u3010\u5BA1\u67E5\u7406\u7531 \xB7 \u4E2D\u6587\u7FFB\u8BD1\u3011";
12
230
  var SYSTEM_PROMPT = [
@@ -173,10 +391,15 @@ function normalizeConfig(config) {
173
391
  timeoutMs: numberSetting(source, "translateTimeoutMs", 6e3, 500, 6e4),
174
392
  maxChars: numberSetting(source, "translateMaxChars", 4e3, 100, 2e4),
175
393
  provider: stringSetting(source, "translateProvider", ""),
176
- model: stringSetting(source, "translateModel", "")
394
+ model: stringSetting(source, "translateModel", ""),
395
+ reviewContext: {
396
+ enabled: boolSetting(source, "reviewContextGuard", true),
397
+ maxHistoryChars: numberSetting(source, "reviewMaxHistoryChars", 12e4, 2e4, 6e5),
398
+ keepRecentEntries: numberSetting(source, "reviewKeepRecentEntries", 16, 4, 64)
399
+ }
177
400
  };
178
401
  }
179
- function whenService(ctx, name, run) {
402
+ function whenService2(ctx, name, run) {
180
403
  try {
181
404
  const inject = ctx.inject;
182
405
  if (typeof inject === "function") {
@@ -189,7 +412,7 @@ function whenService(ctx, name, run) {
189
412
  } catch {
190
413
  }
191
414
  }
192
- function serviceOf(scope, name) {
415
+ function serviceOf2(scope, name) {
193
416
  try {
194
417
  const direct = scope[name];
195
418
  if (direct !== void 0) return direct;
@@ -202,7 +425,7 @@ function serviceOf(scope, name) {
202
425
  return void 0;
203
426
  }
204
427
  }
205
- function effectOf(scope, setup, label) {
428
+ function effectOf2(scope, setup, label) {
206
429
  try {
207
430
  const effect = scope.effect;
208
431
  if (typeof effect === "function") effect.call(scope, setup, label);
@@ -220,8 +443,8 @@ function rootIsActive(root) {
220
443
  }
221
444
  }
222
445
  function installPresetGuard(ctx) {
223
- whenService(ctx, "permissionPresets", (scope) => {
224
- const presets = serviceOf(scope, "permissionPresets");
446
+ whenService2(ctx, "permissionPresets", (scope) => {
447
+ const presets = serviceOf2(scope, "permissionPresets");
225
448
  if (presets === null || typeof presets !== "object") return;
226
449
  if (typeof presets.set !== "function") return;
227
450
  if (guardedPresets.has(presets)) return;
@@ -242,18 +465,18 @@ async function decorateApprovalRequest(scope, settings, request, translate = tra
242
465
  if (!settings.enabled || request === null || typeof request !== "object") return request;
243
466
  const display = request.displayReason;
244
467
  if (display === null || typeof display !== "object" || Array.isArray(display)) return request;
245
- const record = display;
246
- const key = chineseKey(record);
468
+ const record2 = display;
469
+ const key = chineseKey(record2);
247
470
  if (key === void 0) return request;
248
- const english = englishText(record);
471
+ const english = englishText(record2);
249
472
  if (english === void 0 || !isAutoReviewText(english)) return request;
250
- const localized = record[key];
473
+ const localized = record2[key];
251
474
  if (typeof localized !== "string" || hasTranslation(localized)) return request;
252
475
  const excerpt = reviewerExcerpt(english);
253
476
  if (excerpt.length === 0 || excerpt.length > settings.maxChars) return request;
254
477
  if (isPredominantlyChinese(excerpt)) return request;
255
478
  if (translationRoute(settings, request) === void 0) return request;
256
- const llm = serviceOf(scope, "llm");
479
+ const llm = serviceOf2(scope, "llm");
257
480
  const translated = await translate(llm, settings, request, excerpt);
258
481
  if (translated === void 0) return request;
259
482
  const cleaned = cleanTranslation(translated);
@@ -264,7 +487,7 @@ async function decorateApprovalRequest(scope, settings, request, translate = tra
264
487
  return {
265
488
  ...request,
266
489
  ...reasonWithTranslation !== originalReason ? { reason: reasonWithTranslation } : {},
267
- displayReason: { ...record, [key]: withTranslation(localized, cleaned) }
490
+ displayReason: { ...record2, [key]: withTranslation(localized, cleaned) }
268
491
  };
269
492
  }
270
493
  function sessionIdOf(agent) {
@@ -281,8 +504,8 @@ function rejectionKey(agent, callId) {
281
504
  return sessionId === "" ? "" : sessionId + ":" + callId;
282
505
  }
283
506
  function approvalReasonText(request) {
284
- const record = request;
285
- const display = record.displayReason;
507
+ const record2 = request;
508
+ const display = record2.displayReason;
286
509
  if (display !== null && typeof display === "object" && !Array.isArray(display)) {
287
510
  const key = chineseKey(display);
288
511
  if (key !== void 0) {
@@ -292,12 +515,12 @@ function approvalReasonText(request) {
292
515
  const english = englishText(display);
293
516
  if (english !== void 0 && english !== "") return english;
294
517
  }
295
- return typeof record.reason === "string" ? record.reason : "";
518
+ return typeof record2.reason === "string" ? record2.reason : "";
296
519
  }
297
520
  function isAutoReviewApproval(request) {
298
- const record = request;
299
- if (typeof record.reason === "string" && isAutoReviewText(record.reason)) return true;
300
- const display = record.displayReason;
521
+ const record2 = request;
522
+ if (typeof record2.reason === "string" && isAutoReviewText(record2.reason)) return true;
523
+ const display = record2.displayReason;
301
524
  if (display !== null && typeof display === "object" && !Array.isArray(display)) {
302
525
  const english = englishText(display);
303
526
  if (english !== void 0 && isAutoReviewText(english)) return true;
@@ -368,8 +591,8 @@ function installRejectionRelay(ctx, relay) {
368
591
  });
369
592
  }
370
593
  function installApprovalTranslation(ctx, settings, relay) {
371
- whenService(ctx, "approval", (scope) => {
372
- const approval = serviceOf(scope, "approval");
594
+ whenService2(ctx, "approval", (scope) => {
595
+ const approval = serviceOf2(scope, "approval");
373
596
  if (approval === null || typeof approval !== "object") return;
374
597
  const original = approval.request;
375
598
  if (typeof original !== "function") return;
@@ -397,7 +620,7 @@ function installApprovalTranslation(ctx, settings, relay) {
397
620
  } catch {
398
621
  return;
399
622
  }
400
- effectOf(scope, () => () => {
623
+ effectOf2(scope, () => () => {
401
624
  if (approval.request === wrapper) approval.request = call;
402
625
  }, "mcviseron: restore approval.request");
403
626
  });
@@ -405,6 +628,7 @@ function installApprovalTranslation(ctx, settings, relay) {
405
628
  function installAutoReview(ctx, config) {
406
629
  const settings = normalizeConfig(config);
407
630
  if (settings.keepPresetGuard) installPresetGuard(ctx);
631
+ if (settings.reviewContext.enabled) installReviewContextGuard(ctx, settings.reviewContext);
408
632
  if (settings.enabled) {
409
633
  const relay = createRejectionRelay();
410
634
  installRejectionRelay(ctx, relay);
@@ -436,7 +660,10 @@ var DEFAULT_AUTO_REVIEW_CONFIG = {
436
660
  translateTimeoutMs: 6e3,
437
661
  translateMaxChars: 4e3,
438
662
  translateProvider: "",
439
- translateModel: ""
663
+ translateModel: "",
664
+ reviewContextGuard: true,
665
+ reviewMaxHistoryChars: 12e4,
666
+ reviewKeepRecentEntries: 16
440
667
  };
441
668
  var Config = z.object({
442
669
  keepPresetGuard: z.boolean().default(DEFAULT_AUTO_REVIEW_CONFIG.keepPresetGuard).volatile(),
@@ -444,7 +671,10 @@ var Config = z.object({
444
671
  translateTimeoutMs: z.number().min(500).max(6e4).default(DEFAULT_AUTO_REVIEW_CONFIG.translateTimeoutMs).volatile(),
445
672
  translateMaxChars: z.number().min(100).max(2e4).default(DEFAULT_AUTO_REVIEW_CONFIG.translateMaxChars).volatile(),
446
673
  translateProvider: z.string().default(DEFAULT_AUTO_REVIEW_CONFIG.translateProvider).volatile(),
447
- translateModel: z.string().default(DEFAULT_AUTO_REVIEW_CONFIG.translateModel).volatile()
674
+ translateModel: z.string().default(DEFAULT_AUTO_REVIEW_CONFIG.translateModel).volatile(),
675
+ reviewContextGuard: z.boolean().default(DEFAULT_AUTO_REVIEW_CONFIG.reviewContextGuard).volatile(),
676
+ reviewMaxHistoryChars: z.number().min(2e4).max(6e5).default(DEFAULT_AUTO_REVIEW_CONFIG.reviewMaxHistoryChars).volatile(),
677
+ reviewKeepRecentEntries: z.number().min(4).max(64).default(DEFAULT_AUTO_REVIEW_CONFIG.reviewKeepRecentEntries).volatile()
448
678
  }).default(DEFAULT_AUTO_REVIEW_CONFIG);
449
679
  function apply(ctx, config) {
450
680
  installAutoReview(ctx, toPlain(config));