@kangyishuai/dsh-advisor 0.6.2 → 0.6.3

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/call.js CHANGED
@@ -3,6 +3,7 @@
3
3
  // 这是整个插件唯一会花钱的地方,所有失败都必须变成**结构化结果**,绝不向上抛。
4
4
  import { BlockAssembler } from "@deepseek-ai/dsh-llm";
5
5
  import { classifyAbort } from "./abort-reason.js";
6
+ import { createDeadline, raceWithDeadline } from "./deadline.js";
6
7
  import { advisorSystemPrompt, buildAdvisorInput, mintNonce } from "./prompt.js";
7
8
 
8
9
  /** 额外调用统一打这个 purpose:也是递归防护的判据(监听器按它跳过自己)。 */
@@ -92,19 +93,10 @@ export async function callAdvisor(input) {
92
93
  { role: "user", content: [{ type: "text", text: userText }] },
93
94
  ]);
94
95
 
95
- // 超时与用户中断合并:任一触发都中止(绝不留下孤儿请求继续烧 token)。
96
- const timeoutSignal = AbortSignal.timeout(settings.timeoutMs);
97
- // 超时与用户取消都会让流以 aborted 收尾,事后无法从 finish 分辨;
98
- // 这里先记住「信号里的一员确实因超时触发」,收尾时用它区分 code。
99
- let timedOut = false;
100
- timeoutSignal.addEventListener("abort", () => {
101
- timedOut = true;
102
- });
103
- const signals = [input.signal, timeoutSignal].filter(Boolean);
104
- const signal =
105
- signals.length > 1 && typeof AbortSignal.any === "function"
106
- ? AbortSignal.any(/** @type {AbortSignal[]} */ (signals))
107
- : /** @type {AbortSignal|undefined} */ (signals[0]);
96
+ // 硬超时:到点由**插件侧**主动 abort 并提前返回,不等宿主流自己收尾(审计 AUD-ADV-03)。
97
+ // 外部取消(用户 / 回合取消)也合并进同一个 signal,宿主只需要看一个。
98
+ const deadline = createDeadline({ timeoutMs: settings.timeoutMs, external: input.signal });
99
+ const signal = deadline.signal;
108
100
 
109
101
  /**
110
102
  * @param {string} effortOverride 传空串表示「这次不带思考档」
@@ -170,9 +162,9 @@ export async function callAdvisor(input) {
170
162
  }
171
163
  if (settled?.kind === "aborted" || finish?.kind === "aborted") {
172
164
  const verdict = classifyAbort({
173
- // 外部 signal 先于超时触发 → 用户/回合取消;超时 signal 触发 → 超时。
174
- cancelled: timedOut !== true && (input.signal?.aborted ?? false),
175
- timedOut,
165
+ // 外部取消 → aborted;配置的超时触发 → timeout。
166
+ cancelled: deadline.cancelled(),
167
+ timedOut: deadline.timedOut(),
176
168
  timeoutMs: settings.timeoutMs,
177
169
  });
178
170
  return {
@@ -208,18 +200,45 @@ export async function callAdvisor(input) {
208
200
  };
209
201
  };
210
202
 
211
- try {
203
+ /**
204
+ * 一次尝试;模型不支持思考档时去掉档位再试一次(不无限重试)。
205
+ * @returns {Promise<CallResult>}
206
+ */
207
+ const attempt = async () => {
212
208
  try {
213
209
  return await run(effort);
214
210
  } catch (error) {
215
211
  const code = classifyError(error);
216
- // 模型不支持这个思考档:去掉思考档重试**一次**(不无限重试)。
217
212
  if (code === "UNSUPPORTED_REASONING_EFFORT" || code === "INVALID_MODEL_REASONING") {
218
213
  input.logger?.warn?.(`[advisor] 模型不支持思考档 ${effort || "(默认)"},已降级重试`);
219
214
  return await run("");
220
215
  }
221
216
  throw error;
222
217
  }
218
+ };
219
+
220
+ try {
221
+ // 整个尝试(含一次降级重试)共用一个 deadline:超时是「这次征询最多等多久」,
222
+ // 不是「每一轮最多等多久」。
223
+ const outcome = await raceWithDeadline(attempt(), deadline);
224
+ if (outcome.kind === "timeout" || outcome.kind === "aborted") {
225
+ const verdict = classifyAbort({
226
+ cancelled: outcome.kind === "aborted",
227
+ timedOut: outcome.kind === "timeout",
228
+ timeoutMs: settings.timeoutMs,
229
+ });
230
+ // 注意:这里不等流自己收尾,也不把迟到的 usage 记进账本 —— 超时的调用按未计费处理。
231
+ return {
232
+ ok: false,
233
+ code: verdict.code,
234
+ message: verdict.message,
235
+ ms: Date.now() - startedAt,
236
+ model,
237
+ effort,
238
+ };
239
+ }
240
+ if (outcome.kind === "error") throw outcome.error;
241
+ return outcome.value;
223
242
  } catch (error) {
224
243
  return {
225
244
  ok: false,
@@ -229,5 +248,7 @@ export async function callAdvisor(input) {
229
248
  model,
230
249
  effort,
231
250
  };
251
+ } finally {
252
+ deadline.dispose();
232
253
  }
233
254
  }