dsh-mcviseron-plugin 0.6.1 → 0.6.2

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/lib/index.js CHANGED
@@ -338,6 +338,9 @@ __export(auto_review_fix_exports, {
338
338
  });
339
339
  import z2 from "@deepseek-ai/schemastery";
340
340
 
341
+ // src/components/auto-review-fix/index.ts
342
+ import { randomUUID } from "node:crypto";
343
+
341
344
  // src/components/auto-review-fix/translate.ts
342
345
  var TRANSLATION_LABEL = "\u3010\u5BA1\u67E5\u7406\u7531 \xB7 \u4E2D\u6587\u7FFB\u8BD1\u3011";
343
346
  var SYSTEM_PROMPT = [
@@ -368,7 +371,7 @@ function isAutoReviewText(text) {
368
371
  return /auto[\s\-_]*review/i.test(text);
369
372
  }
370
373
  function reviewerExcerpt(text) {
371
- const match = /^\s*auto[\s\-_]*review\s+denied\s+this\s+call\s*[::.]?\s*/i.exec(text);
374
+ const match = /^\s*auto[\s\-_]*review\s+(?:denied\s+this\s+call|拒绝了此调用)\s*[::.]?\s*/i.exec(text);
372
375
  if (match === null) return text.trim();
373
376
  const rest = text.slice(match[0].length).trim();
374
377
  return rest.length === 0 ? text.trim() : rest;
@@ -383,8 +386,11 @@ ${translation}`;
383
386
  function hasTranslation(value) {
384
387
  return value.includes(TRANSLATION_LABEL);
385
388
  }
386
- function containsCjk(text) {
387
- return /[\u3400-\u9fff\uf900-\ufaff]/.test(text);
389
+ function isPredominantlyChinese(text) {
390
+ const han = (text.match(/[\u3400-\u9fff\uf900-\ufaff]/g) ?? []).length;
391
+ if (han === 0) return false;
392
+ const englishWords = (text.match(/[A-Za-z][A-Za-z0-9_.-]*/g) ?? []).length;
393
+ return englishWords <= Math.max(2, Math.ceil(han / 2));
388
394
  }
389
395
  function cleanTranslation(raw) {
390
396
  let text = raw.trim();
@@ -579,7 +585,7 @@ async function decorateApprovalRequest(scope, settings, request, translate = tra
579
585
  if (typeof localized !== "string" || hasTranslation(localized)) return request;
580
586
  const excerpt = reviewerExcerpt(english);
581
587
  if (excerpt.length === 0 || excerpt.length > settings.maxChars) return request;
582
- if (containsCjk(excerpt)) return request;
588
+ if (isPredominantlyChinese(excerpt)) return request;
583
589
  if (translationRoute(settings, request) === void 0) return request;
584
590
  const llm = serviceOf(scope, "llm");
585
591
  const translated = await translate(llm, settings, request, excerpt);
@@ -595,7 +601,107 @@ async function decorateApprovalRequest(scope, settings, request, translate = tra
595
601
  displayReason: { ...record, [key]: withTranslation(localized, cleaned) }
596
602
  };
597
603
  }
598
- function installApprovalTranslation(ctx, settings) {
604
+ function sessionIdOf(agent) {
605
+ const session = agent?.session;
606
+ return typeof session?.id === "string" ? session.id : "";
607
+ }
608
+ function sessionVersion(agent) {
609
+ const version = agent?.session?.header?.version;
610
+ return typeof version === "number" ? version : void 0;
611
+ }
612
+ function rejectionKey(agent, callId) {
613
+ if (typeof callId !== "string" || callId === "") return "";
614
+ const sessionId = sessionIdOf(agent);
615
+ return sessionId === "" ? "" : sessionId + ":" + callId;
616
+ }
617
+ function approvalReasonText(request) {
618
+ const record = request;
619
+ const display = record.displayReason;
620
+ if (display !== null && typeof display === "object" && !Array.isArray(display)) {
621
+ const key = chineseKey(display);
622
+ if (key !== void 0) {
623
+ const value = display[key];
624
+ if (typeof value === "string" && value !== "") return value;
625
+ }
626
+ const english = englishText(display);
627
+ if (english !== void 0 && english !== "") return english;
628
+ }
629
+ return typeof record.reason === "string" ? record.reason : "";
630
+ }
631
+ function isAutoReviewApproval(request) {
632
+ const record = request;
633
+ if (typeof record.reason === "string" && isAutoReviewText(record.reason)) return true;
634
+ const display = record.displayReason;
635
+ if (display !== null && typeof display === "object" && !Array.isArray(display)) {
636
+ const english = englishText(display);
637
+ if (english !== void 0 && isAutoReviewText(english)) return true;
638
+ }
639
+ return false;
640
+ }
641
+ function rejectionSource(agent, summary) {
642
+ return sessionVersion(agent) === 3 ? { kind: "plugin", plugin: "auto-review-fix", form: "notice", summary } : { kind: "plugin:auto-review-fix", form: "notice", summary };
643
+ }
644
+ function buildRejectionNotice(agent, request) {
645
+ const reason = approvalReasonText(request);
646
+ if (reason === "") return void 0;
647
+ const toolName = typeof request.toolName === "string" && request.toolName !== "" ? request.toolName : "\u672A\u77E5\u5DE5\u5177";
648
+ const text = [
649
+ '\u3010\u5BA1\u6279\u88AB\u62D2\u3011\u5DE5\u5177 "' + toolName + '" \u7684\u4EBA\u5DE5\u5BA1\u6279\u88AB\u62D2\u7EDD\u3002',
650
+ "\u81EA\u52A8\u5BA1\u67E5\u7406\u7531\uFF1A",
651
+ reason,
652
+ "",
653
+ "\u8BF7\u6839\u636E\u8BE5\u7406\u7531\u8C03\u6574\u65B9\u6848\uFF0C\u4E0D\u8981\u91CD\u590D\u540C\u4E00\u64CD\u4F5C\u3002"
654
+ ].join("\n");
655
+ return {
656
+ id: randomUUID(),
657
+ role: "user",
658
+ content: [{ type: "text", text }],
659
+ source: rejectionSource(agent, "\u5BA1\u6279\u88AB\u62D2 \xB7 " + toolName)
660
+ };
661
+ }
662
+ function createRejectionRelay() {
663
+ const notes = /* @__PURE__ */ new Map();
664
+ const ttl = 12e4;
665
+ return {
666
+ remember(key, notice) {
667
+ const now = Date.now();
668
+ for (const [oldKey, entry] of notes) if (now - entry.at > ttl) notes.delete(oldKey);
669
+ if (notes.size >= 50) {
670
+ const oldest = notes.keys().next().value;
671
+ if (oldest !== void 0) notes.delete(oldest);
672
+ }
673
+ notes.set(key, { notice, at: now });
674
+ },
675
+ take(key) {
676
+ const entry = notes.get(key);
677
+ if (entry === void 0) return void 0;
678
+ notes.delete(key);
679
+ return entry.notice;
680
+ }
681
+ };
682
+ }
683
+ var relayedContexts = /* @__PURE__ */ new WeakSet();
684
+ function installRejectionRelay(ctx, relay) {
685
+ const target = ctx;
686
+ if (typeof target.on !== "function" || relayedContexts.has(ctx)) return;
687
+ relayedContexts.add(ctx);
688
+ target.on.call(ctx, "tools/post-execute", async (exec, result, next) => {
689
+ const downstream = await next() ?? { kind: "accept" };
690
+ if (result?.isError !== true) return downstream;
691
+ const key = rejectionKey(exec?.agent, exec?.callId);
692
+ if (key === "") return downstream;
693
+ const notice = relay.take(key);
694
+ if (notice === void 0) return downstream;
695
+ return {
696
+ ...downstream,
697
+ additionalContexts: [
698
+ ...Array.isArray(downstream.additionalContexts) ? downstream.additionalContexts : [],
699
+ notice
700
+ ]
701
+ };
702
+ });
703
+ }
704
+ function installApprovalTranslation(ctx, settings, relay) {
599
705
  whenService(ctx, "approval", (scope) => {
600
706
  const approval = serviceOf(scope, "approval");
601
707
  if (approval === null || typeof approval !== "object") return;
@@ -610,7 +716,14 @@ function installApprovalTranslation(ctx, settings) {
610
716
  } catch {
611
717
  decorated = request;
612
718
  }
613
- return call.call(this, decorated);
719
+ const outcome = await call.call(this, decorated);
720
+ if (outcome === "rejected" && isAutoReviewApproval(decorated)) {
721
+ const request2 = decorated;
722
+ const key = rejectionKey(request2.agent, request2.callId);
723
+ const notice = buildRejectionNotice(request2.agent, decorated);
724
+ if (key !== "" && notice !== void 0) relay.remember(key, notice);
725
+ }
726
+ return outcome;
614
727
  };
615
728
  try {
616
729
  approval.request = wrapper;
@@ -626,7 +739,11 @@ function installApprovalTranslation(ctx, settings) {
626
739
  function installAutoReview(ctx, config) {
627
740
  const settings = normalizeConfig(config);
628
741
  if (settings.keepPresetGuard) installPresetGuard(ctx);
629
- if (settings.enabled) installApprovalTranslation(ctx, settings);
742
+ if (settings.enabled) {
743
+ const relay = createRejectionRelay();
744
+ installRejectionRelay(ctx, relay);
745
+ installApprovalTranslation(ctx, settings, relay);
746
+ }
630
747
  }
631
748
 
632
749
  // src/auto-review-fix/index.ts
@@ -832,7 +949,7 @@ __export(task_guard_exports, {
832
949
  import z4 from "@deepseek-ai/schemastery";
833
950
 
834
951
  // src/components/task-guard/index.ts
835
- import { randomUUID } from "node:crypto";
952
+ import { randomUUID as randomUUID2 } from "node:crypto";
836
953
  var SUMMARY_MAX_CHARS = 120;
837
954
  var DEFAULT_EXCLUDES = ["todo_write", "ask_user_question"];
838
955
  var STATUSES = /* @__PURE__ */ new Set(["pending", "in_progress", "completed"]);
@@ -851,7 +968,7 @@ var CONFIG_KEYS = /* @__PURE__ */ new Set([
851
968
  "translateTodoContent",
852
969
  "todoTranslationTimeoutMs"
853
970
  ]);
854
- function containsCjk2(text) {
971
+ function containsCjk(text) {
855
972
  return /[\u3400-\u9fff\uf900-\ufaff]/.test(text);
856
973
  }
857
974
  function normalizeText(value) {
@@ -871,7 +988,10 @@ var INTENT_MARKERS = [
871
988
  /\b(?:now|next|then)\s*,?\s*i(?:'ll| will| am going to|'m going to)\b([^\n]{2,160})/gi,
872
989
  /\bi(?:'ll| will| am going to|'m going to)\s+(?:now\s+)?([^\n]{2,160})/gi,
873
990
  /\blet me\s+(?:now\s+)?([^\n]{2,160})/gi,
874
- /(?:接下来|下一步|现在|然后|下面)(?:我会|我将|我要|我来|就开始|开始|准备)([^\n。;;]{2,120})/g
991
+ /\b(?:now|next|then)\s+(?!i(?:'ll| will| am|'m)\b)([a-z][^\n]{2,160})/gi,
992
+ /\b(?:next step|next,)\s*[:\-]?\s*([^\n]{2,160})/gi,
993
+ /(?:接下来|下一步|现在|然后|下面)(?:我会|我将|我要|我来|就开始|开始|准备)([^\n。;;]{2,120})/g,
994
+ /(?:接下来|下一步|现在|然后|下面)(?:开始|进行|做|处理|继续|检查|运行|绘制|布局|布线|导出|放置|连接)([^\n。;;]{2,120})/g
875
995
  ];
876
996
  function intentPhrases(text) {
877
997
  const found = [];
@@ -888,6 +1008,24 @@ function intentPhrases(text) {
888
1008
  found.sort((left, right) => left.index - right.index);
889
1009
  return found.map((item) => item.phrase);
890
1010
  }
1011
+ var PHASE_PATTERNS = [
1012
+ { id: "schematic", pattern: /schematic|原理图/ },
1013
+ { id: "library", pattern: /library|footprint|器件库|元件库|封装/ },
1014
+ { id: "board", pattern: /board\s*(?:outline|shape)|板框|边框|外形/ },
1015
+ { id: "placement", pattern: /placement|layout|布局|摆放/ },
1016
+ { id: "routing", pattern: /rout(?:e|ing|ed)|track|走线|布线|连线/ },
1017
+ { id: "drc", pattern: /\bdrc\b|\berc\b|design[_ ]audit|校验|检查/ },
1018
+ { id: "power", pattern: /power|电源|加粗/ },
1019
+ { id: "copper", pattern: /copper|pour|铺铜|覆铜/ },
1020
+ { id: "gerber", pattern: /gerber|export|导出|出图/ },
1021
+ { id: "usb", pattern: /usb|type-?c/ },
1022
+ { id: "mcu", pattern: /stm32|\bmcu\b|主控|单片机/ }
1023
+ ];
1024
+ function phasesOf(value) {
1025
+ const phases = /* @__PURE__ */ new Set();
1026
+ for (const { id, pattern } of PHASE_PATTERNS) if (pattern.test(value)) phases.add(id);
1027
+ return phases;
1028
+ }
891
1029
  function detectStepIntent(text, todos) {
892
1030
  if (text.length === 0 || todos.length === 0) return void 0;
893
1031
  const phrases = intentPhrases(text).slice(-3);
@@ -896,11 +1034,15 @@ function detectStepIntent(text, todos) {
896
1034
  for (const phrase of phrases) {
897
1035
  const phraseNormalized = normalizeText(phrase);
898
1036
  const phraseTokens = new Set(tokensOf(phrase));
1037
+ const phrasePhases = phasesOf(phrase);
899
1038
  for (const todo of todos) {
900
1039
  if (todo.status !== "pending") continue;
901
1040
  const contentNormalized = normalizeText(todo.content);
1041
+ const todoPhases = phasesOf(todo.content);
902
1042
  let score = 0;
903
- if (contentNormalized.length >= 4 && (phraseNormalized.includes(contentNormalized) || contentNormalized.includes(phraseNormalized))) {
1043
+ if ([...phrasePhases].some((phase) => todoPhases.has(phase))) {
1044
+ score = 1;
1045
+ } else if (contentNormalized.length >= 4 && (phraseNormalized.includes(contentNormalized) || contentNormalized.includes(phraseNormalized))) {
904
1046
  score = 1;
905
1047
  } else {
906
1048
  const contentTokens = tokensOf(todo.content);
@@ -915,6 +1057,56 @@ function detectStepIntent(text, todos) {
915
1057
  }
916
1058
  return best;
917
1059
  }
1060
+ var COMPLETION_PATTERNS = [
1061
+ /\ball\b[^.!?\n]{0,100}\b(?:routed|connected|placed|done|complete|completed)\b/i,
1062
+ /\b(?:schematic|layout|placement|routing|board|design|work|check|drc|erc)\b[^.!?\n]{0,50}\b(?:complete|completed|done|finished|passed|passes|clean)\b/i,
1063
+ /\b(?:drc|erc|check|validation)\b[^.!?\n]{0,40}\b(?:passes|passed|clean|0\s+(?:errors?|violations?))\b/i,
1064
+ /(?:全部|所有)[^。!?\n]{0,50}(?:布通|连通|完成|通过)|(?:完成|已完成|搞定|通过|布通|连通|无(?:错误|违规)|0\s*(?:错误|违规))/
1065
+ ];
1066
+ function isCompletionSentence(sentence) {
1067
+ return COMPLETION_PATTERNS.some((pattern) => pattern.test(sentence));
1068
+ }
1069
+ function detectCompletion(text, todos) {
1070
+ if (text.length === 0 || todos.length === 0) return void 0;
1071
+ const active = todos.filter((todo) => todo.status === "in_progress");
1072
+ if (active.length > 1) return void 0;
1073
+ const pool = active.length === 1 ? active : todos.filter((todo) => todo.status === "pending");
1074
+ if (pool.length === 0) return void 0;
1075
+ const sentences = text.split(/(?<=[.!?。!?])\s+|\n+/).map((sentence) => sentence.trim()).filter((sentence) => sentence.length > 0);
1076
+ for (const sentence of sentences) {
1077
+ if (!isCompletionSentence(sentence)) continue;
1078
+ const sentencePhases = phasesOf(sentence);
1079
+ let best;
1080
+ let bestScore = 0;
1081
+ for (const todo of pool) {
1082
+ const todoPhases = phasesOf(todo.content);
1083
+ let score = 0;
1084
+ for (const phase of sentencePhases) if (todoPhases.has(phase)) score += 1;
1085
+ if (score > bestScore) {
1086
+ best = todo;
1087
+ bestScore = score;
1088
+ }
1089
+ }
1090
+ if (best === void 0 && active.length === 1 && sentencePhases.size === 0) {
1091
+ best = active[0];
1092
+ bestScore = 0.5;
1093
+ }
1094
+ if (best !== void 0) return { todo: best, phrase: sentence.slice(0, 120), score: bestScore };
1095
+ }
1096
+ return void 0;
1097
+ }
1098
+ function completionPayload(todos, matched) {
1099
+ const active = todos.filter((todo) => todo.status === "in_progress");
1100
+ if (active.length > 1) return void 0;
1101
+ const index = todos.findIndex((todo) => todo.content === matched.content);
1102
+ if (index < 0) return void 0;
1103
+ const next = todos.find((todo, at) => at > index && todo.status === "pending") ?? todos.find((todo, at) => at !== index && todo.status === "pending");
1104
+ const updated = todos.map((todo, at) => ({
1105
+ content: todo.content,
1106
+ status: at === index ? "completed" : next !== void 0 && todo.content === next.content ? "in_progress" : todo.status === "in_progress" && at !== index ? "completed" : todo.status
1107
+ }));
1108
+ return JSON.stringify({ todos: updated });
1109
+ }
918
1110
  function serviceOf2(scope, name) {
919
1111
  try {
920
1112
  const direct = scope[name];
@@ -1014,7 +1206,7 @@ async function displayTodoContents(contents, context) {
1014
1206
  const pending = [];
1015
1207
  const seen = /* @__PURE__ */ new Set();
1016
1208
  for (const content of contents) {
1017
- if (containsCjk2(content) || translationCache.has(content) || seen.has(content)) continue;
1209
+ if (containsCjk(content) || translationCache.has(content) || seen.has(content)) continue;
1018
1210
  seen.add(content);
1019
1211
  pending.push(content);
1020
1212
  }
@@ -1034,7 +1226,7 @@ async function displayTodoContents(contents, context) {
1034
1226
  }
1035
1227
  return contents.map((content) => {
1036
1228
  const translated = translationCache.get(content);
1037
- if (translated === void 0 || containsCjk2(content)) return content;
1229
+ if (translated === void 0 || containsCjk(content)) return content;
1038
1230
  return translated + "\uFF08" + content + "\uFF09";
1039
1231
  });
1040
1232
  }
@@ -1062,7 +1254,7 @@ function installTaskGuard(ctx, input) {
1062
1254
  return next();
1063
1255
  }
1064
1256
  if (config.blockAfterCalls > 0 && exec.parent === void 0 && isTracked(exec.name, config)) {
1065
- const state = sessions.get(sessionIdOf(exec.agent));
1257
+ const state = sessions.get(sessionIdOf2(exec.agent));
1066
1258
  if (state?.todos != null && hasUnfinished(state.todos) && state.callsSinceWrite >= config.blockAfterCalls) {
1067
1259
  return { kind: "deny", reason: denyStale(state, config) };
1068
1260
  }
@@ -1076,7 +1268,7 @@ function installTaskGuard(ctx, input) {
1076
1268
  if (result.isError) return downstream;
1077
1269
  const todos = readTodos(exec.arguments);
1078
1270
  if (todos === null) return downstream;
1079
- const sessionId2 = sessionIdOf(exec.agent);
1271
+ const sessionId2 = sessionIdOf2(exec.agent);
1080
1272
  if (sessionId2 !== "") {
1081
1273
  resetState(sessions, sessionId2, todos);
1082
1274
  intentBuffers.delete(sessionId2);
@@ -1093,13 +1285,26 @@ function installTaskGuard(ctx, input) {
1093
1285
  return downstream;
1094
1286
  }
1095
1287
  if (exec.parent !== void 0 || result.isError || !isTracked(exec.name, config)) return downstream;
1096
- const sessionId = sessionIdOf(exec.agent);
1288
+ const sessionId = sessionIdOf2(exec.agent);
1097
1289
  if (sessionId === "") return downstream;
1098
1290
  const state = sessions.get(sessionId);
1099
1291
  if (state?.todos == null || !hasUnfinished(state.todos)) return downstream;
1100
1292
  state.callsSinceWrite += 1;
1101
1293
  if (config.detectStepIntent && !state.intentNotifiedThisStep) {
1102
- const match = detectStepIntent(intentBuffers.get(sessionId) ?? "", state.todos);
1294
+ const buffer = intentBuffers.get(sessionId) ?? "";
1295
+ const completed = detectCompletion(buffer, state.todos);
1296
+ if (completed !== void 0) {
1297
+ state.intentNotifiedThisStep = true;
1298
+ const notice = await buildCompletionNotice(
1299
+ state,
1300
+ completed,
1301
+ config,
1302
+ sourceKindFor(exec.agent, config),
1303
+ noticeContext(exec.agent, config, ctx)
1304
+ );
1305
+ return attachContext(downstream, notice);
1306
+ }
1307
+ const match = detectStepIntent(buffer, state.todos);
1103
1308
  if (match !== void 0) {
1104
1309
  state.intentNotifiedThisStep = true;
1105
1310
  const notice = await buildIntentNotice(
@@ -1128,7 +1333,7 @@ function installTaskGuard(ctx, input) {
1128
1333
  return attachContext(downstream, reminder);
1129
1334
  });
1130
1335
  bus.on("agent/pre-step", async (payload, next) => {
1131
- const sessionId = sessionIdOf(payload.agent);
1336
+ const sessionId = sessionIdOf2(payload.agent);
1132
1337
  if (sessionId !== "") intentBuffers.delete(sessionId);
1133
1338
  const state = sessions.get(sessionId);
1134
1339
  if (state !== void 0) {
@@ -1153,7 +1358,7 @@ function installTaskGuard(ctx, input) {
1153
1358
  intentBuffers.set(sessionId, next);
1154
1359
  });
1155
1360
  bus.on("session/event", (session, event) => {
1156
- const sessionId = sessionIdOf({ session });
1361
+ const sessionId = sessionIdOf2({ session });
1157
1362
  if (sessionId === "") return;
1158
1363
  if (event.type === "turn/start" || event.type === "turn/end") {
1159
1364
  sessions.delete(sessionId);
@@ -1278,7 +1483,7 @@ function resetState(sessions, sessionId, todos) {
1278
1483
  intentNotifiedThisStep: false
1279
1484
  });
1280
1485
  }
1281
- function sessionIdOf(agent) {
1486
+ function sessionIdOf2(agent) {
1282
1487
  const id = agent?.session?.id;
1283
1488
  return typeof id === "string" && id !== "" ? id : "";
1284
1489
  }
@@ -1331,22 +1536,49 @@ async function buildReminder(state, config, sourceKind, context) {
1331
1536
  const quoted = remaining.slice(0, config.maxListItems).map((todo, index) => (todo.status === "in_progress" ? "[~] " : "[ ] ") + truncate(display[index], 60)).join("\n");
1332
1537
  const omitted = remaining.length - config.maxListItems;
1333
1538
  const items = omitted > 0 ? quoted + "\n\u2026 +" + omitted : quoted;
1539
+ const activeOne = active.length === 1 ? active[0] : void 0;
1540
+ const suggested = activeOne === void 0 ? void 0 : completionPayload(todos, activeOne);
1334
1541
  const text = config.locale === "en" ? [
1335
1542
  "[Todo sync] " + state.callsSinceWrite + " tool calls have run since the last todo_write.",
1336
1543
  "In progress: " + activeText,
1337
1544
  "Remaining:\n" + items,
1338
- "If the current phase is not finished yet, ignore this reminder. The moment it is finished, call todo_write: mark it completed and the next task in_progress (never leave pending work with no active task).",
1545
+ activeOne === void 0 ? "If the current phase is finished, call todo_write: mark completed work completed and the next task in_progress (never leave pending work with no active task)." : suggested === void 0 ? "If the current phase is finished, call todo_write: mark it completed and the next task in_progress (never leave pending work with no active task)." : "If the current phase is finished, send this todo_write now; if it is still running, keep that task in_progress and leave the list unchanged:",
1546
+ suggested ?? "",
1339
1547
  "(Tip: todo titles can be written in Chinese.)"
1340
- ].join("\n") : [
1548
+ ].filter((line) => line !== "").join("\n") : [
1341
1549
  "\u3010todo \u540C\u6B65\u3011\u8DDD\u4E0A\u6B21 todo_write \u5DF2\u8FC7 " + state.callsSinceWrite + " \u6B21\u5DE5\u5177\u8C03\u7528\u3002",
1342
1550
  "\u8FDB\u884C\u4E2D\uFF1A" + activeText,
1343
1551
  "\u5269\u4F59\uFF1A\n" + items,
1344
- "\u5982\u679C\u5F53\u524D\u9636\u6BB5\u8FD8\u6CA1\u5B8C\u6210\uFF0C\u8BF7\u5FFD\u7565\u672C\u63D0\u9192\uFF1B\u4E00\u65E6\u5B8C\u6210\uFF0C\u8BF7\u7ACB\u523B\u8C03\u7528 todo_write\uFF1A\u628A\u8BE5\u9636\u6BB5\u6807\u4E3A completed\uFF0C\u5E76\u628A\u4E0B\u4E00\u9636\u6BB5\u6807\u4E3A in_progress\uFF08\u6709\u5269\u4F59\u4EFB\u52A1\u65F6\u4E0D\u8981\u7559\u7A7A\uFF09\u3002",
1552
+ activeOne === void 0 ? "\u5982\u679C\u5F53\u524D\u9636\u6BB5\u5DF2\u7ECF\u5B8C\u6210\uFF0C\u8BF7\u8C03\u7528 todo_write\uFF1A\u628A\u5DF2\u5B8C\u6210\u9636\u6BB5\u6807\u4E3A completed\uFF0C\u5E76\u628A\u4E0B\u4E00\u9636\u6BB5\u6807\u4E3A in_progress\uFF08\u6709\u5269\u4F59\u4EFB\u52A1\u65F6\u4E0D\u8981\u7559\u7A7A\uFF09\u3002" : suggested === void 0 ? "\u5982\u679C\u5F53\u524D\u9636\u6BB5\u5DF2\u7ECF\u5B8C\u6210\uFF0C\u8BF7\u8C03\u7528 todo_write\uFF1A\u628A\u8BE5\u9636\u6BB5\u6807\u4E3A completed\uFF0C\u5E76\u628A\u4E0B\u4E00\u9636\u6BB5\u6807\u4E3A in_progress\uFF08\u6709\u5269\u4F59\u4EFB\u52A1\u65F6\u4E0D\u8981\u7559\u7A7A\uFF09\u3002" : "\u5982\u679C\u5F53\u524D\u9636\u6BB5\u5DF2\u7ECF\u5B8C\u6210\uFF0C\u8BF7\u76F4\u63A5\u53D1\u9001\u4EE5\u4E0B todo_write\uFF1B\u5982\u679C\u4ECD\u5728\u8FDB\u884C\uFF0C\u8BF7\u4FDD\u6301\u8BE5\u5F85\u529E\u4E3A in_progress\uFF0C\u65E0\u9700\u6539\u52A8\u6E05\u5355\uFF1A",
1553
+ suggested ?? "",
1345
1554
  "\uFF08\u63D0\u793A\uFF1Atodo \u5185\u5BB9\u53EF\u4EE5\u76F4\u63A5\u7528\u4E2D\u6587\u4E66\u5199\u3002\uFF09"
1346
- ].join("\n");
1555
+ ].filter((line) => line !== "").join("\n");
1347
1556
  const summary = config.locale === "en" ? "todo stale \xB7 " + state.callsSinceWrite + " calls" : "todo \u672A\u66F4\u65B0 \xB7 " + state.callsSinceWrite + " \u6B21\u8C03\u7528";
1348
1557
  return createNotice(text, summary, sourceKind);
1349
1558
  }
1559
+ async function buildCompletionNotice(state, match, config, sourceKind, context) {
1560
+ const todos = state.todos ?? [];
1561
+ const [display] = await displayTodoContents([match.todo.content], context);
1562
+ const suggested = completionPayload(todos, match.todo);
1563
+ const phrase = truncate(match.phrase, 80);
1564
+ const text = config.locale === "en" ? [
1565
+ '[Todo sync] The assistant text looks like a completed phase: "' + display + '".',
1566
+ "Source: " + phrase,
1567
+ suggested === void 0 ? "If that phase is finished, call todo_write: mark it completed and the next task in_progress." : "If that phase is finished, send this todo_write now:",
1568
+ suggested ?? "",
1569
+ "If it is still running, keep that task in_progress; do not mark it completed yet.",
1570
+ "(Tip: todo titles can be written in Chinese.)"
1571
+ ].filter((line) => line !== "").join("\n") : [
1572
+ "\u3010todo \u540C\u6B65\u3011\u68C0\u6D4B\u5230\u9636\u6BB5\u5B8C\u6210\uFF1A\u300C" + display + "\u300D",
1573
+ "\u4F9D\u636E\uFF1A" + phrase,
1574
+ suggested === void 0 ? "\u5982\u679C\u8BE5\u9636\u6BB5\u5DF2\u5B8C\u6210\uFF0C\u8BF7\u8C03\u7528 todo_write\uFF1A\u628A\u8BE5\u5F85\u529E\u6807\u4E3A completed\uFF0C\u5E76\u628A\u4E0B\u4E00\u9636\u6BB5\u6807\u4E3A in_progress\u3002" : "\u5982\u679C\u8BE5\u9636\u6BB5\u786E\u5B9E\u5DF2\u5B8C\u6210\uFF0C\u8BF7\u76F4\u63A5\u53D1\u9001\u4EE5\u4E0B todo_write\uFF1A",
1575
+ suggested ?? "",
1576
+ "\u5982\u679C\u4ECD\u5728\u8FDB\u884C\uFF0C\u8BF7\u4FDD\u6301\u8BE5\u5F85\u529E\u4E3A in_progress\uFF0C\u4E0D\u8981\u63D0\u524D\u6807 completed\u3002",
1577
+ "\uFF08\u63D0\u793A\uFF1Atodo \u5185\u5BB9\u53EF\u4EE5\u76F4\u63A5\u7528\u4E2D\u6587\u4E66\u5199\u3002\uFF09"
1578
+ ].filter((line) => line !== "").join("\n");
1579
+ const summary = config.locale === "en" ? "todo complete \xB7 " + truncate(display, 40) : "todo \u5B8C\u6210 \xB7 " + truncate(display, 40);
1580
+ return createNotice(text, summary, sourceKind);
1581
+ }
1350
1582
  async function buildIntentNotice(state, match, config, sourceKind, context) {
1351
1583
  const todos = state.todos ?? [];
1352
1584
  const [display] = await displayTodoContents([match.todo.content], context);
@@ -1373,7 +1605,7 @@ function createNotice(text, summary, sourceKind) {
1373
1605
  const boundedSummary = summary.length <= SUMMARY_MAX_CHARS ? summary : `${summary.slice(0, SUMMARY_MAX_CHARS - 1)}\u2026`;
1374
1606
  const source = sourceKind === "v3" ? { kind: "plugin", plugin: "task-guard", form: "notice", summary: boundedSummary } : { kind: "plugin:task-guard", form: "notice", summary: boundedSummary };
1375
1607
  return deepFreeze({
1376
- id: randomUUID(),
1608
+ id: randomUUID2(),
1377
1609
  role: "user",
1378
1610
  content: [{ type: "text", text }],
1379
1611
  source
package/lib/task-guard.js CHANGED
@@ -41,7 +41,10 @@ var INTENT_MARKERS = [
41
41
  /\b(?:now|next|then)\s*,?\s*i(?:'ll| will| am going to|'m going to)\b([^\n]{2,160})/gi,
42
42
  /\bi(?:'ll| will| am going to|'m going to)\s+(?:now\s+)?([^\n]{2,160})/gi,
43
43
  /\blet me\s+(?:now\s+)?([^\n]{2,160})/gi,
44
- /(?:接下来|下一步|现在|然后|下面)(?:我会|我将|我要|我来|就开始|开始|准备)([^\n。;;]{2,120})/g
44
+ /\b(?:now|next|then)\s+(?!i(?:'ll| will| am|'m)\b)([a-z][^\n]{2,160})/gi,
45
+ /\b(?:next step|next,)\s*[:\-]?\s*([^\n]{2,160})/gi,
46
+ /(?:接下来|下一步|现在|然后|下面)(?:我会|我将|我要|我来|就开始|开始|准备)([^\n。;;]{2,120})/g,
47
+ /(?:接下来|下一步|现在|然后|下面)(?:开始|进行|做|处理|继续|检查|运行|绘制|布局|布线|导出|放置|连接)([^\n。;;]{2,120})/g
45
48
  ];
46
49
  function intentPhrases(text) {
47
50
  const found = [];
@@ -58,6 +61,24 @@ function intentPhrases(text) {
58
61
  found.sort((left, right) => left.index - right.index);
59
62
  return found.map((item) => item.phrase);
60
63
  }
64
+ var PHASE_PATTERNS = [
65
+ { id: "schematic", pattern: /schematic|原理图/ },
66
+ { id: "library", pattern: /library|footprint|器件库|元件库|封装/ },
67
+ { id: "board", pattern: /board\s*(?:outline|shape)|板框|边框|外形/ },
68
+ { id: "placement", pattern: /placement|layout|布局|摆放/ },
69
+ { id: "routing", pattern: /rout(?:e|ing|ed)|track|走线|布线|连线/ },
70
+ { id: "drc", pattern: /\bdrc\b|\berc\b|design[_ ]audit|校验|检查/ },
71
+ { id: "power", pattern: /power|电源|加粗/ },
72
+ { id: "copper", pattern: /copper|pour|铺铜|覆铜/ },
73
+ { id: "gerber", pattern: /gerber|export|导出|出图/ },
74
+ { id: "usb", pattern: /usb|type-?c/ },
75
+ { id: "mcu", pattern: /stm32|\bmcu\b|主控|单片机/ }
76
+ ];
77
+ function phasesOf(value) {
78
+ const phases = /* @__PURE__ */ new Set();
79
+ for (const { id, pattern } of PHASE_PATTERNS) if (pattern.test(value)) phases.add(id);
80
+ return phases;
81
+ }
61
82
  function detectStepIntent(text, todos) {
62
83
  if (text.length === 0 || todos.length === 0) return void 0;
63
84
  const phrases = intentPhrases(text).slice(-3);
@@ -66,11 +87,15 @@ function detectStepIntent(text, todos) {
66
87
  for (const phrase of phrases) {
67
88
  const phraseNormalized = normalizeText(phrase);
68
89
  const phraseTokens = new Set(tokensOf(phrase));
90
+ const phrasePhases = phasesOf(phrase);
69
91
  for (const todo of todos) {
70
92
  if (todo.status !== "pending") continue;
71
93
  const contentNormalized = normalizeText(todo.content);
94
+ const todoPhases = phasesOf(todo.content);
72
95
  let score = 0;
73
- if (contentNormalized.length >= 4 && (phraseNormalized.includes(contentNormalized) || contentNormalized.includes(phraseNormalized))) {
96
+ if ([...phrasePhases].some((phase) => todoPhases.has(phase))) {
97
+ score = 1;
98
+ } else if (contentNormalized.length >= 4 && (phraseNormalized.includes(contentNormalized) || contentNormalized.includes(phraseNormalized))) {
74
99
  score = 1;
75
100
  } else {
76
101
  const contentTokens = tokensOf(todo.content);
@@ -85,6 +110,56 @@ function detectStepIntent(text, todos) {
85
110
  }
86
111
  return best;
87
112
  }
113
+ var COMPLETION_PATTERNS = [
114
+ /\ball\b[^.!?\n]{0,100}\b(?:routed|connected|placed|done|complete|completed)\b/i,
115
+ /\b(?:schematic|layout|placement|routing|board|design|work|check|drc|erc)\b[^.!?\n]{0,50}\b(?:complete|completed|done|finished|passed|passes|clean)\b/i,
116
+ /\b(?:drc|erc|check|validation)\b[^.!?\n]{0,40}\b(?:passes|passed|clean|0\s+(?:errors?|violations?))\b/i,
117
+ /(?:全部|所有)[^。!?\n]{0,50}(?:布通|连通|完成|通过)|(?:完成|已完成|搞定|通过|布通|连通|无(?:错误|违规)|0\s*(?:错误|违规))/
118
+ ];
119
+ function isCompletionSentence(sentence) {
120
+ return COMPLETION_PATTERNS.some((pattern) => pattern.test(sentence));
121
+ }
122
+ function detectCompletion(text, todos) {
123
+ if (text.length === 0 || todos.length === 0) return void 0;
124
+ const active = todos.filter((todo) => todo.status === "in_progress");
125
+ if (active.length > 1) return void 0;
126
+ const pool = active.length === 1 ? active : todos.filter((todo) => todo.status === "pending");
127
+ if (pool.length === 0) return void 0;
128
+ const sentences = text.split(/(?<=[.!?。!?])\s+|\n+/).map((sentence) => sentence.trim()).filter((sentence) => sentence.length > 0);
129
+ for (const sentence of sentences) {
130
+ if (!isCompletionSentence(sentence)) continue;
131
+ const sentencePhases = phasesOf(sentence);
132
+ let best;
133
+ let bestScore = 0;
134
+ for (const todo of pool) {
135
+ const todoPhases = phasesOf(todo.content);
136
+ let score = 0;
137
+ for (const phase of sentencePhases) if (todoPhases.has(phase)) score += 1;
138
+ if (score > bestScore) {
139
+ best = todo;
140
+ bestScore = score;
141
+ }
142
+ }
143
+ if (best === void 0 && active.length === 1 && sentencePhases.size === 0) {
144
+ best = active[0];
145
+ bestScore = 0.5;
146
+ }
147
+ if (best !== void 0) return { todo: best, phrase: sentence.slice(0, 120), score: bestScore };
148
+ }
149
+ return void 0;
150
+ }
151
+ function completionPayload(todos, matched) {
152
+ const active = todos.filter((todo) => todo.status === "in_progress");
153
+ if (active.length > 1) return void 0;
154
+ const index = todos.findIndex((todo) => todo.content === matched.content);
155
+ if (index < 0) return void 0;
156
+ const next = todos.find((todo, at) => at > index && todo.status === "pending") ?? todos.find((todo, at) => at !== index && todo.status === "pending");
157
+ const updated = todos.map((todo, at) => ({
158
+ content: todo.content,
159
+ status: at === index ? "completed" : next !== void 0 && todo.content === next.content ? "in_progress" : todo.status === "in_progress" && at !== index ? "completed" : todo.status
160
+ }));
161
+ return JSON.stringify({ todos: updated });
162
+ }
88
163
  function serviceOf(scope, name) {
89
164
  try {
90
165
  const direct = scope[name];
@@ -269,7 +344,20 @@ function installTaskGuard(ctx, input) {
269
344
  if (state?.todos == null || !hasUnfinished(state.todos)) return downstream;
270
345
  state.callsSinceWrite += 1;
271
346
  if (config.detectStepIntent && !state.intentNotifiedThisStep) {
272
- const match = detectStepIntent(intentBuffers.get(sessionId) ?? "", state.todos);
347
+ const buffer = intentBuffers.get(sessionId) ?? "";
348
+ const completed = detectCompletion(buffer, state.todos);
349
+ if (completed !== void 0) {
350
+ state.intentNotifiedThisStep = true;
351
+ const notice = await buildCompletionNotice(
352
+ state,
353
+ completed,
354
+ config,
355
+ sourceKindFor(exec.agent, config),
356
+ noticeContext(exec.agent, config, ctx)
357
+ );
358
+ return attachContext(downstream, notice);
359
+ }
360
+ const match = detectStepIntent(buffer, state.todos);
273
361
  if (match !== void 0) {
274
362
  state.intentNotifiedThisStep = true;
275
363
  const notice = await buildIntentNotice(
@@ -501,22 +589,49 @@ async function buildReminder(state, config, sourceKind, context) {
501
589
  const quoted = remaining.slice(0, config.maxListItems).map((todo, index) => (todo.status === "in_progress" ? "[~] " : "[ ] ") + truncate(display[index], 60)).join("\n");
502
590
  const omitted = remaining.length - config.maxListItems;
503
591
  const items = omitted > 0 ? quoted + "\n\u2026 +" + omitted : quoted;
592
+ const activeOne = active.length === 1 ? active[0] : void 0;
593
+ const suggested = activeOne === void 0 ? void 0 : completionPayload(todos, activeOne);
504
594
  const text = config.locale === "en" ? [
505
595
  "[Todo sync] " + state.callsSinceWrite + " tool calls have run since the last todo_write.",
506
596
  "In progress: " + activeText,
507
597
  "Remaining:\n" + items,
508
- "If the current phase is not finished yet, ignore this reminder. The moment it is finished, call todo_write: mark it completed and the next task in_progress (never leave pending work with no active task).",
598
+ activeOne === void 0 ? "If the current phase is finished, call todo_write: mark completed work completed and the next task in_progress (never leave pending work with no active task)." : suggested === void 0 ? "If the current phase is finished, call todo_write: mark it completed and the next task in_progress (never leave pending work with no active task)." : "If the current phase is finished, send this todo_write now; if it is still running, keep that task in_progress and leave the list unchanged:",
599
+ suggested ?? "",
509
600
  "(Tip: todo titles can be written in Chinese.)"
510
- ].join("\n") : [
601
+ ].filter((line) => line !== "").join("\n") : [
511
602
  "\u3010todo \u540C\u6B65\u3011\u8DDD\u4E0A\u6B21 todo_write \u5DF2\u8FC7 " + state.callsSinceWrite + " \u6B21\u5DE5\u5177\u8C03\u7528\u3002",
512
603
  "\u8FDB\u884C\u4E2D\uFF1A" + activeText,
513
604
  "\u5269\u4F59\uFF1A\n" + items,
514
- "\u5982\u679C\u5F53\u524D\u9636\u6BB5\u8FD8\u6CA1\u5B8C\u6210\uFF0C\u8BF7\u5FFD\u7565\u672C\u63D0\u9192\uFF1B\u4E00\u65E6\u5B8C\u6210\uFF0C\u8BF7\u7ACB\u523B\u8C03\u7528 todo_write\uFF1A\u628A\u8BE5\u9636\u6BB5\u6807\u4E3A completed\uFF0C\u5E76\u628A\u4E0B\u4E00\u9636\u6BB5\u6807\u4E3A in_progress\uFF08\u6709\u5269\u4F59\u4EFB\u52A1\u65F6\u4E0D\u8981\u7559\u7A7A\uFF09\u3002",
605
+ activeOne === void 0 ? "\u5982\u679C\u5F53\u524D\u9636\u6BB5\u5DF2\u7ECF\u5B8C\u6210\uFF0C\u8BF7\u8C03\u7528 todo_write\uFF1A\u628A\u5DF2\u5B8C\u6210\u9636\u6BB5\u6807\u4E3A completed\uFF0C\u5E76\u628A\u4E0B\u4E00\u9636\u6BB5\u6807\u4E3A in_progress\uFF08\u6709\u5269\u4F59\u4EFB\u52A1\u65F6\u4E0D\u8981\u7559\u7A7A\uFF09\u3002" : suggested === void 0 ? "\u5982\u679C\u5F53\u524D\u9636\u6BB5\u5DF2\u7ECF\u5B8C\u6210\uFF0C\u8BF7\u8C03\u7528 todo_write\uFF1A\u628A\u8BE5\u9636\u6BB5\u6807\u4E3A completed\uFF0C\u5E76\u628A\u4E0B\u4E00\u9636\u6BB5\u6807\u4E3A in_progress\uFF08\u6709\u5269\u4F59\u4EFB\u52A1\u65F6\u4E0D\u8981\u7559\u7A7A\uFF09\u3002" : "\u5982\u679C\u5F53\u524D\u9636\u6BB5\u5DF2\u7ECF\u5B8C\u6210\uFF0C\u8BF7\u76F4\u63A5\u53D1\u9001\u4EE5\u4E0B todo_write\uFF1B\u5982\u679C\u4ECD\u5728\u8FDB\u884C\uFF0C\u8BF7\u4FDD\u6301\u8BE5\u5F85\u529E\u4E3A in_progress\uFF0C\u65E0\u9700\u6539\u52A8\u6E05\u5355\uFF1A",
606
+ suggested ?? "",
515
607
  "\uFF08\u63D0\u793A\uFF1Atodo \u5185\u5BB9\u53EF\u4EE5\u76F4\u63A5\u7528\u4E2D\u6587\u4E66\u5199\u3002\uFF09"
516
- ].join("\n");
608
+ ].filter((line) => line !== "").join("\n");
517
609
  const summary = config.locale === "en" ? "todo stale \xB7 " + state.callsSinceWrite + " calls" : "todo \u672A\u66F4\u65B0 \xB7 " + state.callsSinceWrite + " \u6B21\u8C03\u7528";
518
610
  return createNotice(text, summary, sourceKind);
519
611
  }
612
+ async function buildCompletionNotice(state, match, config, sourceKind, context) {
613
+ const todos = state.todos ?? [];
614
+ const [display] = await displayTodoContents([match.todo.content], context);
615
+ const suggested = completionPayload(todos, match.todo);
616
+ const phrase = truncate(match.phrase, 80);
617
+ const text = config.locale === "en" ? [
618
+ '[Todo sync] The assistant text looks like a completed phase: "' + display + '".',
619
+ "Source: " + phrase,
620
+ suggested === void 0 ? "If that phase is finished, call todo_write: mark it completed and the next task in_progress." : "If that phase is finished, send this todo_write now:",
621
+ suggested ?? "",
622
+ "If it is still running, keep that task in_progress; do not mark it completed yet.",
623
+ "(Tip: todo titles can be written in Chinese.)"
624
+ ].filter((line) => line !== "").join("\n") : [
625
+ "\u3010todo \u540C\u6B65\u3011\u68C0\u6D4B\u5230\u9636\u6BB5\u5B8C\u6210\uFF1A\u300C" + display + "\u300D",
626
+ "\u4F9D\u636E\uFF1A" + phrase,
627
+ suggested === void 0 ? "\u5982\u679C\u8BE5\u9636\u6BB5\u5DF2\u5B8C\u6210\uFF0C\u8BF7\u8C03\u7528 todo_write\uFF1A\u628A\u8BE5\u5F85\u529E\u6807\u4E3A completed\uFF0C\u5E76\u628A\u4E0B\u4E00\u9636\u6BB5\u6807\u4E3A in_progress\u3002" : "\u5982\u679C\u8BE5\u9636\u6BB5\u786E\u5B9E\u5DF2\u5B8C\u6210\uFF0C\u8BF7\u76F4\u63A5\u53D1\u9001\u4EE5\u4E0B todo_write\uFF1A",
628
+ suggested ?? "",
629
+ "\u5982\u679C\u4ECD\u5728\u8FDB\u884C\uFF0C\u8BF7\u4FDD\u6301\u8BE5\u5F85\u529E\u4E3A in_progress\uFF0C\u4E0D\u8981\u63D0\u524D\u6807 completed\u3002",
630
+ "\uFF08\u63D0\u793A\uFF1Atodo \u5185\u5BB9\u53EF\u4EE5\u76F4\u63A5\u7528\u4E2D\u6587\u4E66\u5199\u3002\uFF09"
631
+ ].filter((line) => line !== "").join("\n");
632
+ const summary = config.locale === "en" ? "todo complete \xB7 " + truncate(display, 40) : "todo \u5B8C\u6210 \xB7 " + truncate(display, 40);
633
+ return createNotice(text, summary, sourceKind);
634
+ }
520
635
  async function buildIntentNotice(state, match, config, sourceKind, context) {
521
636
  const todos = state.todos ?? [];
522
637
  const [display] = await displayTodoContents([match.todo.content], context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mcviseron-plugin",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "DeepSeek Harness (DSH) master plugin — one package hosting MCviseron's DSH components as separate bundle entries: a time-based light/dark Appearance schedule, an Auto-review fix (shutdown preset guard + Chinese approval-reason translation), and a todo plan guard that keeps multi-stage work advancing.",
5
5
  "license": "Apache-2.0",
6
6
  "author": {