chatccc 0.2.291 → 0.2.292

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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deepccc",
3
- "version": "0.2.16",
3
+ "version": "0.2.17",
4
4
  "description": "A lightweight coding agent with OpenAI-compatible and Anthropic Messages API support.",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -25,6 +25,12 @@ const SEARCH_TIMEOUT_MS = 15_000;
25
25
  const MAX_COMMAND_OUTPUT_BYTES = 256 * 1024;
26
26
  const DEFAULT_COMMAND_TIMEOUT_MS = 120_000;
27
27
  const MAX_COMMAND_TIMEOUT_MS = 900_000;
28
+ /** 让位注入的轮询间隔:run_command 执行期以此频率检查是否有待注入的用户消息 */
29
+ const YIELD_POLL_INTERVAL_MS = 250;
30
+ /** command_output 单次等待上限,避免代理用等待把 step 又堵死 */
31
+ const MAX_COMMAND_OUTPUT_WAIT_MS = 30_000;
32
+ /** 后台命令句柄上限;超出时优先回收已结束的句柄 */
33
+ const MAX_BACKGROUND_COMMANDS = 32;
28
34
  /** task 子代理工具:子任务结果回传主会话前的最大字符数(防止子代理长输出撑爆主上下文) */
29
35
  export const MAX_TASK_OUTPUT_CHARS = 32_000;
30
36
  const requireFromHere = createRequire(import.meta.url);
@@ -722,7 +728,41 @@ export async function searchCodeForTool(cwd, input, signal, runtimeOptions = {})
722
728
  : fallback.truncated,
723
729
  };
724
730
  }
725
- export async function runCommandForTool(cwd, input, abortSignal, coAuthor) {
731
+ const backgroundCommands = new Map();
732
+ let backgroundCommandSeq = 0;
733
+ function normalizeCommandOutputWaitMs(value) {
734
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0)
735
+ return 0;
736
+ return Math.min(Math.trunc(value), MAX_COMMAND_OUTPUT_WAIT_MS);
737
+ }
738
+ function snapshotBackgroundCommand(entry) {
739
+ return {
740
+ taskId: entry.taskId,
741
+ command: entry.command,
742
+ cwd: entry.cwd,
743
+ running: !entry.done,
744
+ exitCode: entry.exitCode,
745
+ signal: entry.signal,
746
+ stdout: entry.stdout.chunks.join(""),
747
+ stderr: entry.stderr.chunks.join(""),
748
+ timedOut: entry.timedOut,
749
+ truncated: entry.stdout.truncated || entry.stderr.truncated,
750
+ durationMs: Date.now() - entry.startedAt,
751
+ };
752
+ }
753
+ /** 句柄超限时优先回收已结束的条目(运行中的条目保留,避免破坏 agent 的跟踪)。 */
754
+ function pruneBackgroundCommands() {
755
+ if (backgroundCommands.size <= MAX_BACKGROUND_COMMANDS)
756
+ return;
757
+ for (const [id, entry] of backgroundCommands) {
758
+ if (!entry.done)
759
+ continue;
760
+ backgroundCommands.delete(id);
761
+ if (backgroundCommands.size <= MAX_BACKGROUND_COMMANDS)
762
+ return;
763
+ }
764
+ }
765
+ export async function runCommandForTool(cwd, input, abortSignal, coAuthor, shouldYield) {
726
766
  const command = withGitCoAuthor(input.command?.trim(), coAuthor);
727
767
  if (!command)
728
768
  throw new Error("command is required");
@@ -740,6 +780,8 @@ export async function runCommandForTool(cwd, input, abortSignal, coAuthor) {
740
780
  let timedOut = false;
741
781
  let timeout;
742
782
  let fallbackTimer;
783
+ let yieldPoll;
784
+ let backgroundEntry;
743
785
  const child = spawn(command, {
744
786
  cwd: commandCwd,
745
787
  shell: true,
@@ -751,6 +793,8 @@ export async function runCommandForTool(cwd, input, abortSignal, coAuthor) {
751
793
  clearTimeout(timeout);
752
794
  if (fallbackTimer)
753
795
  clearTimeout(fallbackTimer);
796
+ if (yieldPoll)
797
+ clearInterval(yieldPoll);
754
798
  abortSignal?.removeEventListener("abort", abort);
755
799
  };
756
800
  const finish = (exitCode, signal) => {
@@ -770,12 +814,32 @@ export async function runCommandForTool(cwd, input, abortSignal, coAuthor) {
770
814
  durationMs: Date.now() - startedAt,
771
815
  });
772
816
  };
817
+ /**
818
+ * 统一收敛点:已转入后台则更新句柄并唤醒等待方,否则按前台语义 resolve。
819
+ * 让位后 timeout 与 abort 的既有定时器/监听继续生效,因此后台任务仍受
820
+ * timeoutMs 约束,也仍能被 /stop 终止。
821
+ */
822
+ const settle = (exitCode, signal) => {
823
+ if (!backgroundEntry) {
824
+ finish(exitCode, signal);
825
+ return;
826
+ }
827
+ backgroundEntry.done = true;
828
+ backgroundEntry.exitCode = exitCode;
829
+ backgroundEntry.signal = signal;
830
+ backgroundEntry.timedOut = timedOut;
831
+ cleanup();
832
+ const waiters = backgroundEntry.waiters;
833
+ backgroundEntry.waiters = [];
834
+ for (const wake of waiters)
835
+ wake();
836
+ };
773
837
  const requestKill = (reason) => {
774
838
  void killProcessTree(child.pid);
775
839
  fallbackTimer = setTimeout(() => {
776
840
  child.stdout?.destroy();
777
841
  child.stderr?.destroy();
778
- finish(null, reason === "timeout" ? "SIGTERM" : reason);
842
+ settle(null, reason === "timeout" ? "SIGTERM" : reason);
779
843
  }, 5_000);
780
844
  fallbackTimer.unref?.();
781
845
  };
@@ -787,6 +851,70 @@ export async function runCommandForTool(cwd, input, abortSignal, coAuthor) {
787
851
  requestKill("abort");
788
852
  };
789
853
  abortSignal?.addEventListener("abort", abort, { once: true });
854
+ /**
855
+ * 让位:命令既不杀也不等,转为后台句柄后立即返回。这样当前 step 尽快结束,
856
+ * 下一个 prepareStep 边界即可调用 drainInput 把用户新消息注入当前 turn。
857
+ */
858
+ const detachToBackground = () => {
859
+ if (settled || backgroundEntry)
860
+ return;
861
+ // close/error 重新挂到 settle:settle 会按 backgroundEntry 分流到后台句柄
862
+ child.removeAllListeners("close");
863
+ child.removeAllListeners("error");
864
+ child.once("close", (code, signal) => settle(code, signal));
865
+ child.once("error", () => settle(null, "error"));
866
+ const taskId = `cmd-${++backgroundCommandSeq}-${Math.random().toString(36).slice(2, 6)}`;
867
+ backgroundEntry = {
868
+ taskId,
869
+ command,
870
+ cwd: commandCwd,
871
+ startedAt,
872
+ child,
873
+ stdout,
874
+ stderr,
875
+ done: false,
876
+ exitCode: null,
877
+ signal: null,
878
+ timedOut: false,
879
+ waiters: [],
880
+ };
881
+ backgroundCommands.set(taskId, backgroundEntry);
882
+ pruneBackgroundCommands();
883
+ if (yieldPoll)
884
+ clearInterval(yieldPoll);
885
+ yieldPoll = undefined;
886
+ // 刻意不调 cleanup:超时定时器与 abort 监听继续为后台任务服务
887
+ settled = true;
888
+ resolvePromise({
889
+ command,
890
+ cwd: commandCwd,
891
+ exitCode: null,
892
+ signal: null,
893
+ stdout: stdout.chunks.join(""),
894
+ stderr: stderr.chunks.join(""),
895
+ timedOut: false,
896
+ truncated: stdout.truncated || stderr.truncated,
897
+ durationMs: Date.now() - startedAt,
898
+ backgrounded: true,
899
+ taskId,
900
+ });
901
+ };
902
+ if (shouldYield) {
903
+ yieldPoll = setInterval(() => {
904
+ if (settled || backgroundEntry)
905
+ return;
906
+ let yieldNow = false;
907
+ try {
908
+ yieldNow = shouldYield() === true;
909
+ }
910
+ catch {
911
+ yieldNow = false;
912
+ }
913
+ if (yieldNow)
914
+ detachToBackground();
915
+ }, YIELD_POLL_INTERVAL_MS);
916
+ yieldPoll.unref?.();
917
+ }
790
918
  child.stdout?.on("data", (chunk) => {
791
919
  appendLimitedOutput(stdout, chunk);
792
920
  });
@@ -794,6 +922,10 @@ export async function runCommandForTool(cwd, input, abortSignal, coAuthor) {
794
922
  appendLimitedOutput(stderr, chunk);
795
923
  });
796
924
  child.once("error", (err) => {
925
+ if (backgroundEntry) {
926
+ settle(null, "error");
927
+ return;
928
+ }
797
929
  if (settled)
798
930
  return;
799
931
  settled = true;
@@ -801,10 +933,75 @@ export async function runCommandForTool(cwd, input, abortSignal, coAuthor) {
801
933
  reject(err);
802
934
  });
803
935
  child.once("close", (code, signal) => {
804
- finish(code, signal);
936
+ settle(code, signal);
805
937
  });
806
938
  });
807
939
  }
940
+ /** 读取后台命令的最新输出;waitMs > 0 时最多等待该时长(等待期间仍可让位)。 */
941
+ export async function commandOutputForTool(taskId, input, shouldYield) {
942
+ const entry = backgroundCommands.get(taskId);
943
+ if (!entry)
944
+ throw new Error(`未知的后台任务:${taskId}`);
945
+ const waitMs = normalizeCommandOutputWaitMs(input?.waitMs);
946
+ if (waitMs > 0 && !entry.done) {
947
+ await waitForBackgroundCommand(entry, waitMs, shouldYield);
948
+ }
949
+ return snapshotBackgroundCommand(entry);
950
+ }
951
+ /** 等待后台命令结束(或等待预算耗尽、或让位信号到来)。 */
952
+ function waitForBackgroundCommand(entry, waitMs, shouldYield) {
953
+ return new Promise((resolve) => {
954
+ let finished = false;
955
+ let poll;
956
+ const wake = () => {
957
+ if (finished)
958
+ return;
959
+ finished = true;
960
+ clearTimeout(timer);
961
+ if (poll)
962
+ clearInterval(poll);
963
+ const index = entry.waiters.indexOf(wake);
964
+ if (index >= 0)
965
+ entry.waiters.splice(index, 1);
966
+ resolve();
967
+ };
968
+ const timer = setTimeout(wake, waitMs);
969
+ timer.unref?.();
970
+ entry.waiters.push(wake);
971
+ if (shouldYield) {
972
+ poll = setInterval(() => {
973
+ let yieldNow = false;
974
+ try {
975
+ yieldNow = shouldYield() === true;
976
+ }
977
+ catch {
978
+ yieldNow = false;
979
+ }
980
+ if (yieldNow)
981
+ wake();
982
+ }, YIELD_POLL_INTERVAL_MS);
983
+ poll.unref?.();
984
+ }
985
+ });
986
+ }
987
+ /** 终止一个仍在运行的后台命令。 */
988
+ export async function commandKillForTool(taskId) {
989
+ const entry = backgroundCommands.get(taskId);
990
+ if (!entry)
991
+ throw new Error(`未知的后台任务:${taskId}`);
992
+ if (entry.done)
993
+ return { taskId, killed: false };
994
+ await killProcessTree(entry.child.pid);
995
+ return { taskId, killed: true };
996
+ }
997
+ /** 终止并清空全部后台命令句柄(进程退出与测试收尾),避免留下孤儿进程。 */
998
+ export function killAllBackgroundCommands() {
999
+ for (const entry of backgroundCommands.values()) {
1000
+ if (!entry.done)
1001
+ void killProcessTree(entry.child.pid);
1002
+ }
1003
+ backgroundCommands.clear();
1004
+ }
808
1005
  export async function editFileForTool(cwd, input) {
809
1006
  if (!Array.isArray(input.edits) || input.edits.length === 0) {
810
1007
  throw new Error("edits must contain at least one replacement");
@@ -1090,7 +1287,7 @@ export function createBuiltinFileTools(cwd, options = {}) {
1090
1287
  execute: (input) => presentFileForTool(cwd, input),
1091
1288
  }),
1092
1289
  run_command: tool({
1093
- description: "在本地工作区运行非交互式 shell 命令。用于测试、git 和包脚本。返回 stdout/stderr 和 exitCode;非零退出码不是工具错误。",
1290
+ description: "在本地工作区运行非交互式 shell 命令。用于测试、git 和包脚本。返回 stdout/stderr 和 exitCode;非零退出码不是工具错误。命令运行期间若用户发来新消息,会立即转入后台并返回 taskId(backgrounded:true),命令继续运行,用 command_output 取后续输出。",
1094
1291
  inputSchema: jsonSchema({
1095
1292
  type: "object",
1096
1293
  additionalProperties: false,
@@ -1108,9 +1305,34 @@ export function createBuiltinFileTools(cwd, options = {}) {
1108
1305
  reason: isDangerousCommand(input.command) ? "high-risk" : "rule",
1109
1306
  detail: `运行命令: ${input.command}`,
1110
1307
  });
1111
- return runCommandForTool(cwd, input, execOptions.abortSignal, options.gitCoAuthor);
1308
+ return runCommandForTool(cwd, input, execOptions.abortSignal, options.gitCoAuthor, options.shouldYieldToInjection);
1112
1309
  },
1113
1310
  }),
1311
+ command_output: tool({
1312
+ description: "读取被让位到后台的命令的最新输出。立即返回当前状态;waitMs 可最多等待 30 秒。任务结束后可反复读取完整 stdout/stderr。",
1313
+ inputSchema: jsonSchema({
1314
+ type: "object",
1315
+ additionalProperties: false,
1316
+ properties: {
1317
+ taskId: { type: "string", description: "run_command 返回的 taskId(backgrounded:true 时)。" },
1318
+ waitMs: { type: "number", description: `可选等待毫秒数;0 或省略立即返回,上限 ${MAX_COMMAND_OUTPUT_WAIT_MS}。` },
1319
+ },
1320
+ required: ["taskId"],
1321
+ }),
1322
+ execute: (input) => commandOutputForTool(input.taskId, input, options.shouldYieldToInjection),
1323
+ }),
1324
+ command_kill: tool({
1325
+ description: "终止一个仍在后台运行的命令(run_command 让位后转入后台的)。",
1326
+ inputSchema: jsonSchema({
1327
+ type: "object",
1328
+ additionalProperties: false,
1329
+ properties: {
1330
+ taskId: { type: "string", description: "run_command 返回的 taskId。" },
1331
+ },
1332
+ required: ["taskId"],
1333
+ }),
1334
+ execute: (input) => commandKillForTool(input.taskId),
1335
+ }),
1114
1336
  task: tool({
1115
1337
  description: "把独立子任务委派给子代理执行:子代理使用子模型、拥有独立上下文,不污染主对话上下文。适合边界清晰、可独立交付的调研/代码生成子任务(如扫描整个仓库、阅读长文档、生成独立模块)。子代理不能再次委派任务(禁止嵌套),结果会截断回传。",
1116
1338
  inputSchema: jsonSchema({
@@ -270,6 +270,8 @@ export class ChatSession {
270
270
  context;
271
271
  compactionTimeoutMs;
272
272
  maxSteps;
273
+ /** 透传给 run_command 的让位注入判定(见 ChatSessionOptions.shouldYieldToInjection)。 */
274
+ shouldYieldToInjection;
273
275
  effort;
274
276
  maxOutputTokens;
275
277
  streaming;
@@ -359,6 +361,7 @@ export class ChatSession {
359
361
  this.subModel = this.subModelId ? provider(this.subModelId) : this.model;
360
362
  this.cwd = options.cwd ?? process.cwd();
361
363
  this.maxSteps = normalizeMaxSteps(options.maxSteps);
364
+ this.shouldYieldToInjection = options.shouldYieldToInjection;
362
365
  this.compactionTimeoutMs = Math.max(1, options.compactionTimeoutMs ?? DEFAULT_COMPACTION_TIMEOUT_MS);
363
366
  this.customSystemPrompt = options.systemPrompt ?? "";
364
367
  this.permissionMode = options.permissionMode ?? "ask";
@@ -493,6 +496,7 @@ export class ChatSession {
493
496
  tools: createBuiltinFileTools(this.cwd, {
494
497
  permissionGate: this.permissionGate,
495
498
  runTask: this.runTask,
499
+ shouldYieldToInjection: this.shouldYieldToInjection,
496
500
  gitCoAuthor: {
497
501
  ...appConfig.git.coAuthor,
498
502
  enabled: this.gitCoAuthorEnabled,
@@ -19,6 +19,9 @@ function toChatSessionOptions(sessionId, cwd, options) {
19
19
  // 高危命令不询问,全部放行(与独立 deepccc CLI 的 ask 模式不同)
20
20
  permissionMode: "bypass",
21
21
  ...(options.gitCoAuthor !== undefined ? { gitCoAuthor: options.gitCoAuthor } : {}),
22
+ ...(options.hasPendingInjection
23
+ ? { shouldYieldToInjection: () => options.hasPendingInjection(sessionId) === true }
24
+ : {}),
22
25
  };
23
26
  }
24
27
  export function createCccAdapter(options = {}) {
@@ -29,7 +29,7 @@ function compressWechatDisplayText(text) {
29
29
  }
30
30
  import { readStreamState, writeStreamState, createEmptyStreamState, isFinalReplySentForTurn, markFinalReplySent, } from "./stream-state.js";
31
31
  import { addCardToTurn, finalizeTurnCards, markCardDone } from "./turn-cards.js";
32
- import { bindChatToSession, unbindChatFromSession, getChatsForSession, activePrompts, displayCards, unifiedDisplayLoopHandle, setUnifiedDisplayLoopHandle, rebuildSessionChatsFromRegistry, recordLastActiveChat, getLastActiveChat, pickDisplayChat, dequeueMessage, consumeQueuedMessage, cancelQueuedMessage, shiftInjection, unshiftInjection, drainRemainingInjections, clearInjections, setQueuePreservedChat, consumeQueuePreservedChat, markSessionFinalizing, clearSessionFinalizing, reserveAutoRecovery, consumeAutoRecoveryReservation, cancelAutoRecoveryReservation, hasAutoRecoveryReservation, } from "./session-chat-binding.js";
32
+ import { bindChatToSession, unbindChatFromSession, getChatsForSession, activePrompts, displayCards, unifiedDisplayLoopHandle, setUnifiedDisplayLoopHandle, rebuildSessionChatsFromRegistry, recordLastActiveChat, getLastActiveChat, pickDisplayChat, dequeueMessage, consumeQueuedMessage, cancelQueuedMessage, shiftInjection, unshiftInjection, drainRemainingInjections, clearInjections, setQueuePreservedChat, consumeQueuePreservedChat, markSessionFinalizing, clearSessionFinalizing, reserveAutoRecovery, consumeAutoRecoveryReservation, cancelAutoRecoveryReservation, hasAutoRecoveryReservation, hasPendingInjection, } from "./session-chat-binding.js";
33
33
  async function sendFinalReplyTextOnce(platform, chatId, sessionId, turnCount, text) {
34
34
  const sent = await platform.sendText(chatId, text).then((ok) => ok !== false).catch(() => false);
35
35
  if (sent)
@@ -550,6 +550,7 @@ export function getAdapterForTool(tool, sessionId) {
550
550
  ...(config.ccc.provider ? { provider: config.ccc.provider } : {}),
551
551
  ...(config.ccc.subModel ? { subModel: config.ccc.subModel } : {}),
552
552
  ...(config.ccc.gitCoAuthor !== null ? { gitCoAuthor: config.ccc.gitCoAuthor } : {}),
553
+ hasPendingInjection,
553
554
  });
554
555
  }
555
556
  else if (tool === "dsh") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chatccc",
3
- "version": "0.2.291",
3
+ "version": "0.2.292",
4
4
  "description": "Feishu bot bridge for Claude Code",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",