engine7 7.1.149 → 7.1.151

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.
@@ -8128,6 +8128,8 @@ function attachmentToMessage(attachment) {
8128
8128
  ${attachment.notifications.join("\n\n")}`
8129
8129
  )
8130
8130
  };
8131
+ case "coldstart_hint":
8132
+ return { role: "user", content: wrapInSystemReminder(attachment.text) };
8131
8133
  case "teammate_messages": {
8132
8134
  const lines = attachment.messages.map(
8133
8135
  (m) => m.summary ? `[From: ${m.from}] ${m.summary}
@@ -13249,13 +13251,17 @@ var init_session_map = __esm({
13249
13251
 
13250
13252
  // src/cc-driver/long-http.ts
13251
13253
  import { request as request3 } from "node:http";
13252
- function longPostJson(url, body, headers = {}, signal) {
13254
+ function longPostJson(url, body, headers = {}, signal, acknowledgment) {
13253
13255
  const data = Buffer.from(JSON.stringify(body));
13254
13256
  return new Promise((resolve12, reject) => {
13255
13257
  const req = request3(url, { method: "POST", headers: { ...headers, "Content-Length": String(data.byteLength) } }, (res) => {
13258
+ if (acknowledgment && res.statusCode === 200 && res.headers[CC_JOB_HEADER] === acknowledgment.jobId) acknowledgment.onAccepted();
13256
13259
  const chunks = [];
13257
13260
  res.on("data", (chunk) => chunks.push(chunk));
13258
- res.on("error", reject);
13261
+ res.on("error", (err) => {
13262
+ cleanup();
13263
+ reject(err);
13264
+ });
13259
13265
  res.on("end", () => {
13260
13266
  cleanup();
13261
13267
  const text2 = Buffer.concat(chunks).toString("utf8");
@@ -13287,9 +13293,11 @@ function longPostJson(url, body, headers = {}, signal) {
13287
13293
  req.end(data);
13288
13294
  });
13289
13295
  }
13296
+ var CC_JOB_HEADER;
13290
13297
  var init_long_http = __esm({
13291
13298
  "src/cc-driver/long-http.ts"() {
13292
13299
  "use strict";
13300
+ CC_JOB_HEADER = "x-engine7-cc-job-id";
13293
13301
  }
13294
13302
  });
13295
13303
 
@@ -13343,19 +13351,31 @@ async function findDesktopTargets(workDir, sessionId) {
13343
13351
  }
13344
13352
  return targets;
13345
13353
  }
13346
- async function askDesktop(target, prompt, source, signal) {
13354
+ async function askDesktop(target, prompt, source, signal, acknowledgment) {
13355
+ const endpoint = acknowledgment ? "submit" : "ask";
13347
13356
  const { status, json: result } = await longPostJson(
13348
- `http://127.0.0.1:${target.port}/ask`,
13349
- { prompt, source, sessionId: target.sessionId, instance: target.instance, workDir: target.workDir },
13357
+ `http://127.0.0.1:${target.port}/${endpoint}`,
13358
+ { prompt, source, jobId: acknowledgment?.jobId, sessionId: target.sessionId, instance: target.instance, workDir: target.workDir },
13350
13359
  { authorization: `Bearer ${target.token}` },
13351
- signal
13360
+ signal,
13361
+ acknowledgment
13352
13362
  );
13353
- if (status !== 200) throw failure(result?.error || `Desktop CC HTTP ${status}`, status);
13363
+ if (status !== 200 || result?.error) throw failure(result?.error || `Desktop CC HTTP ${status}`, status);
13354
13364
  if (result.sessionId !== target.sessionId || typeof result.text !== "string" || typeof result.isError !== "boolean") {
13355
13365
  throw failure("Desktop CC returned an invalid result");
13356
13366
  }
13357
13367
  return result;
13358
13368
  }
13369
+ async function stopDesktopJob(target, jobId) {
13370
+ const { status, json } = await longPostJson(
13371
+ `http://127.0.0.1:${target.port}/stop`,
13372
+ { jobId, sessionId: target.sessionId, instance: target.instance, workDir: target.workDir },
13373
+ { authorization: `Bearer ${target.token}` },
13374
+ AbortSignal.timeout(5e3)
13375
+ );
13376
+ if (status !== 200) throw failure(json?.error || `Desktop CC HTTP ${status}`, status);
13377
+ return json?.stopped === true;
13378
+ }
13359
13379
  var registryDir, canonical2, failure, alive;
13360
13380
  var init_desktop = __esm({
13361
13381
  "src/cc-driver/desktop.ts"() {
@@ -13803,6 +13823,7 @@ var init_driver = __esm({
13803
13823
  revision = 0;
13804
13824
  idleWaiters = [];
13805
13825
  abort = null;
13826
+ activeJob;
13806
13827
  /** 当前流式 chunk 的 steer 口(仅用户消息路径开;外部 ask/桌面路径为 null) */
13807
13828
  feeder = null;
13808
13829
  sessionId = null;
@@ -13847,13 +13868,17 @@ var init_driver = __esm({
13847
13868
  void this.drain();
13848
13869
  }
13849
13870
  /** External callers use the same FIFO; they never answer a pending permission card. */
13850
- ask(prompt, source, expectedSessionId, signal) {
13851
- if (this.closed) return Promise.reject(requestError("CC binding is stopping", 503));
13852
- if (!prompt.trim()) return Promise.reject(requestError("prompt is required", 400));
13853
- if (signal?.aborted) return Promise.reject(requestError("CC request cancelled", 499));
13854
- if (this.sessionId !== expectedSessionId) return Promise.reject(requestError("CC session changed; select the current binding again"));
13855
- if (this.queue.length >= MAX_QUEUE) return Promise.reject(requestError("CC queue is full", 429));
13856
- const job = { prompt: prompt.trim(), source, expectedSessionId, signal };
13871
+ async ask(prompt, source, expectedSessionId, signal) {
13872
+ return this.submit(prompt, source, expectedSessionId, signal);
13873
+ }
13874
+ submit(prompt, source, expectedSessionId, signal, id3, onSession) {
13875
+ if (this.closed) throw requestError("CC binding is stopping", 503);
13876
+ if (!prompt.trim()) throw requestError("prompt is required", 400);
13877
+ if (signal?.aborted) throw requestError("CC request cancelled", 499);
13878
+ if (this.sessionId !== expectedSessionId) throw requestError("CC session changed; select the current binding again");
13879
+ if (id3 && (this.activeJob?.id === id3 || this.queue.some((job2) => job2.id === id3))) throw requestError("CC job already exists");
13880
+ if (this.queue.length >= MAX_QUEUE) throw requestError("CC queue is full", 429);
13881
+ const job = { id: id3, prompt: prompt.trim(), source, expectedSessionId, expectedRevision: id3 ? this.revision : void 0, signal, onSession };
13857
13882
  const cancel = () => {
13858
13883
  const index = this.queue.indexOf(job);
13859
13884
  if (index >= 0) {
@@ -13868,7 +13893,16 @@ var init_driver = __esm({
13868
13893
  signal?.addEventListener("abort", cancel, { once: true });
13869
13894
  void this.drain();
13870
13895
  });
13871
- return pending.finally(() => signal?.removeEventListener("abort", cancel));
13896
+ return signal ? pending.finally(() => signal.removeEventListener("abort", cancel)) : pending;
13897
+ }
13898
+ stopJob(id3) {
13899
+ const index = this.queue.findIndex((job) => job.id === id3);
13900
+ if (index >= 0) {
13901
+ this.queue.splice(index, 1)[0].reject?.(requestError("CC request cancelled (stop)", 499));
13902
+ return true;
13903
+ }
13904
+ if (this.activeJob?.id === id3) return this.activeJob.stop?.() ?? false;
13905
+ return false;
13872
13906
  }
13873
13907
  /** Reload/stop must not leave the old hub executing queued work. */
13874
13908
  shutdown() {
@@ -13894,6 +13928,9 @@ var init_driver = __esm({
13894
13928
  stop() {
13895
13929
  this.abort?.abort();
13896
13930
  }
13931
+ get queued() {
13932
+ return this.queue.length;
13933
+ }
13897
13934
  get busy() {
13898
13935
  return this.draining;
13899
13936
  }
@@ -13907,15 +13944,17 @@ var init_driver = __esm({
13907
13944
  await this.predecessor;
13908
13945
  while (!this.closed && this.queue.length > 0) {
13909
13946
  const job = this.queue.shift();
13947
+ this.activeJob = job;
13910
13948
  try {
13911
13949
  if (job.signal?.aborted) throw requestError("CC request cancelled", 499);
13912
- if (job.expectedSessionId !== void 0 && job.expectedSessionId !== this.sessionId) {
13950
+ const changed = job.expectedRevision !== void 0 ? job.expectedRevision !== this.revision : job.expectedSessionId !== void 0 && job.expectedSessionId !== this.sessionId;
13951
+ if (changed) {
13913
13952
  throw requestError("CC session changed while this request was queued");
13914
13953
  }
13915
13954
  if (job.source) {
13916
13955
  const prompt = `[cc_ask from ${job.source.replace(/[\r\n]/g, " ").slice(0, 160)}]
13917
13956
  ${job.prompt}`;
13918
- const result = await this.runTurn(prompt, job.signal, true);
13957
+ const result = await this.runTurn(prompt, job.signal, true, job);
13919
13958
  job.resolve?.(result);
13920
13959
  } else {
13921
13960
  await this.runStreaming(job);
@@ -13924,6 +13963,8 @@ ${job.prompt}`;
13924
13963
  job.reject?.(err);
13925
13964
  console.error(`[cc-driver] turn \u5931\u8D25 key=${this.key}: ${err?.stack || err}`);
13926
13965
  await this.sendText(`[cc-driver] \u8FD9\u6B21\u4EFB\u52A1\u5931\u8D25\uFF1A${err?.message || String(err)}`);
13966
+ } finally {
13967
+ this.activeJob = void 0;
13927
13968
  }
13928
13969
  }
13929
13970
  } finally {
@@ -13931,12 +13972,24 @@ ${job.prompt}`;
13931
13972
  for (const resolve12 of this.idleWaiters.splice(0)) resolve12();
13932
13973
  }
13933
13974
  }
13934
- async runTurn(prompt, signal, external = false) {
13975
+ async runTurn(prompt, signal, external = false, job) {
13935
13976
  const ac = new AbortController();
13936
13977
  this.abort = ac;
13978
+ if (job?.id) job.stop = () => {
13979
+ ac.abort();
13980
+ return true;
13981
+ };
13937
13982
  const resumedFrom = this.sessionId;
13938
13983
  const revision = this.revision;
13939
13984
  let turnSessionId = resumedFrom;
13985
+ const observeSession = (id3) => {
13986
+ try {
13987
+ job?.onSession?.(id3);
13988
+ } catch (error) {
13989
+ console.error("[cc-driver] task session update failed:", error.message);
13990
+ }
13991
+ };
13992
+ observeSession(turnSessionId);
13940
13993
  const cancel = () => ac.abort();
13941
13994
  signal?.addEventListener("abort", cancel, { once: true });
13942
13995
  if (signal?.aborted) ac.abort();
@@ -13960,13 +14013,31 @@ ${job.prompt}`;
13960
14013
  const acc = this.makeStreamAccumulator();
13961
14014
  try {
13962
14015
  const desktops = resumedFrom ? await findDesktopTargets(this.workDir, resumedFrom) : [];
14016
+ ac.signal.throwIfAborted();
13963
14017
  if (desktops.length > 1) throw new Error("Multiple desktop writers claim this CC session; close the duplicate");
13964
14018
  if (desktops[0]) {
13965
- const result2 = await askDesktop(desktops[0], prompt, this.key, ac.signal);
14019
+ const desktop = desktops[0];
14020
+ let result2;
14021
+ if (job?.id) {
14022
+ const jobId = job.id;
14023
+ let acknowledge;
14024
+ const accepted = new Promise((resolve12) => {
14025
+ acknowledge = resolve12;
14026
+ });
14027
+ job.stop = async () => await accepted && stopDesktopJob(desktop, jobId);
14028
+ try {
14029
+ result2 = await askDesktop(desktop, prompt, this.key, ac.signal, { jobId, onAccepted: () => acknowledge(true) });
14030
+ } finally {
14031
+ acknowledge(false);
14032
+ }
14033
+ } else {
14034
+ result2 = await askDesktop(desktop, prompt, this.key, ac.signal);
14035
+ }
13966
14036
  finalText = result2.text;
13967
14037
  isError = result2.isError;
13968
14038
  gotResult = true;
13969
14039
  turnSessionId = result2.sessionId;
14040
+ observeSession(turnSessionId);
13970
14041
  if (this.revision === revision) this.sessionId = result2.sessionId;
13971
14042
  } else {
13972
14043
  if (resumedFrom && nativeSessionOwners(resumedFrom).length) {
@@ -13979,6 +14050,7 @@ ${job.prompt}`;
13979
14050
  const mb = total ? `\u7EA6 ${Math.round(total / 1024 / 1024)}M` : "\u7EA6 350M";
13980
14051
  void this.sendText(`[cc-driver] \u9996\u6B21\u4F7F\u7528\uFF1A\u6B63\u5728\u4E0B\u8F7D ${label}\uFF08${mb}\uFF0C\u53EA\u6B64\u4E00\u6B21\uFF09\uFF0C\u5B8C\u6210\u540E\u81EA\u52A8\u7EE7\u7EED\u2026`);
13981
14052
  });
14053
+ ac.signal.throwIfAborted();
13982
14054
  const stream = query({
13983
14055
  prompt,
13984
14056
  options: {
@@ -14021,6 +14093,7 @@ ${job.prompt}`;
14021
14093
  const r = msg2;
14022
14094
  if (r.session_id) {
14023
14095
  turnSessionId = r.session_id;
14096
+ observeSession(turnSessionId);
14024
14097
  if (this.revision === revision) this.sessionId = r.session_id;
14025
14098
  }
14026
14099
  isError = r.is_error === true;
@@ -14031,6 +14104,7 @@ ${job.prompt}`;
14031
14104
  const sid = msg2.session_id;
14032
14105
  if (sid) {
14033
14106
  turnSessionId = sid;
14107
+ observeSession(turnSessionId);
14034
14108
  if (this.revision === revision) this.sessionId = sid;
14035
14109
  }
14036
14110
  } else {
@@ -14040,6 +14114,7 @@ ${job.prompt}`;
14040
14114
  }
14041
14115
  if (turnSessionId && gotResult && this.revision === revision) this.map.switchTo(this.key, turnSessionId, this.workDir);
14042
14116
  } finally {
14117
+ if (job) job.stop = void 0;
14043
14118
  this.abort = null;
14044
14119
  signal?.removeEventListener("abort", cancel);
14045
14120
  }
@@ -14683,6 +14758,11 @@ function getProxyDispatcher(value) {
14683
14758
  const cached2 = dispatchers.get(raw);
14684
14759
  if (cached2) return cached2;
14685
14760
  const url = proxyAddress(raw);
14761
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
14762
+ throw new Error(
14763
+ `\u4E0D\u652F\u6301\u7684\u4EE3\u7406\u534F\u8BAE ${url.protocol}\uFF08\u4EC5 http/https \u53EF\u76F4\u63A5\u4F7F\u7528\uFF1Bsocks5 \u7B49\u8BF7\u5148\u8F6C\u6210 http \u4EE3\u7406\uFF0C\u5982 gost/privoxy\uFF09\uFF1A${raw}`
14764
+ );
14765
+ }
14686
14766
  const key4 = localProxyKey(url);
14687
14767
  const dispatcher = key4 ? new AdaptiveProxyDispatcher(url.href, key4) : new ProxyAgent({ uri: url.href });
14688
14768
  dispatchers.set(raw, dispatcher);
@@ -16792,7 +16872,8 @@ CREATE INDEX IF NOT EXISTS idx_proactive_decisions_created ON proactive_decision
16792
16872
  this.noteCascade(id3);
16793
16873
  this.db.prepare(`UPDATE actions SET basis_state='dirty',action_version=action_version+1,
16794
16874
  cancel_requested=CASE WHEN status='executing' THEN 1 ELSE cancel_requested END,
16795
- status=CASE WHEN status='queued' THEN 'cancelled' ELSE status END
16875
+ status=CASE WHEN status='queued' THEN 'cancelled' ELSE status END,
16876
+ cancel_reason=COALESCE(cancel_reason, CASE WHEN status='queued' THEN 'graph_basis_changed' END)
16796
16877
  WHERE intent_id=? AND status IN ('queued','executing')`).run(id3);
16797
16878
  this.db.prepare("UPDATE intent_runs SET state='stale',reason_code='graph_basis_changed',finished_at=? WHERE intent_id=? AND state='running'").run(Date.now(), id3);
16798
16879
  pending.push(...this.dependencies.children(scope, [{ kind: "node", id: id3 }]));
@@ -16877,11 +16958,12 @@ CREATE INDEX IF NOT EXISTS idx_proactive_decisions_created ON proactive_decision
16877
16958
  a.note ?? null
16878
16959
  );
16879
16960
  }
16880
- /** status='queued' AND scheduled_time<=now,按时间升序 */
16881
- dueActions(now2, limit2 = 20) {
16961
+ /** status='queued' AND scheduled_time<=now,按时间升序。excludeIds:sweep 翻页绕开已看过的(守卫跳过的) */
16962
+ dueActions(now2, limit2 = 20, excludeIds = []) {
16882
16963
  const rows = this.db.prepare(
16883
- `SELECT * FROM actions WHERE status='queued' AND scheduled_time <= ? ORDER BY scheduled_time ASC LIMIT ?`
16884
- ).all(now2, limit2);
16964
+ `SELECT * FROM actions WHERE status='queued' AND scheduled_time <= ?
16965
+ AND id NOT IN (SELECT value FROM json_each(?)) ORDER BY scheduled_time ASC LIMIT ?`
16966
+ ).all(now2, JSON.stringify(excludeIds), limit2);
16885
16967
  return rows.map(rowToAction);
16886
16968
  }
16887
16969
  /** queued 状态 action 的 intent_id 集合(去重) */
@@ -22004,6 +22086,10 @@ var FeishuAdapter = class _FeishuAdapter {
22004
22086
  }
22005
22087
  }
22006
22088
  }
22089
+ /** 公开上传入口(1130 gmail 连接卡片等业务卡用):委托 _uploadImage。 */
22090
+ async uploadImage(buffer, filename) {
22091
+ return this._uploadImage(buffer, filename);
22092
+ }
22007
22093
  async _uploadImage(buffer, filename) {
22008
22094
  const token = await this._getTenantToken();
22009
22095
  const resp = await fetch("https://open.feishu.cn/open-apis/im/v1/images", {
@@ -29649,7 +29735,9 @@ var SEARCH_ALIASES = /* @__PURE__ */ new Map([
29649
29735
  ["gmail", ["\u90AE\u7BB1", "\u90AE\u4EF6"]],
29650
29736
  ["device_data", ["\u8BBE\u5907\u8D44\u6599", "\u65E5\u5386", "\u63D0\u9192\u4E8B\u9879", "\u901A\u8BAF\u5F55", "\u8054\u7CFB\u4EBA", "\u5907\u5FD8\u5F55", "\u7167\u7247", "\u5065\u5EB7"]],
29651
29737
  ["cc_ask", ["\u5C0FC", "\u684C\u9762CC", "Claude Code"]],
29652
- ["cc_progress", ["\u5C0FC", "\u684C\u9762CC", "Claude Code"]]
29738
+ ["cc_progress", ["\u5C0FC", "\u684C\u9762CC", "Claude Code"]],
29739
+ ["cc_new", ["\u5C0FC", "\u684C\u9762CC", "Claude Code", "\u65B0\u5EFA CC \u4F1A\u8BDD"]],
29740
+ ["cc_stop", ["\u5C0FC", "\u684C\u9762CC", "Claude Code", "\u505C\u6B62 CC \u4EFB\u52A1"]]
29653
29741
  ]);
29654
29742
  function toolSearchAliases(name) {
29655
29743
  return SEARCH_ALIASES.get(name) ?? [];