@kangyishuai/dsh-advisor 0.6.1 → 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
@@ -2,6 +2,8 @@
2
2
  // 一次顾问调用:prepareCall → stream → 聚合文本 / usage / 错误分类。
3
3
  // 这是整个插件唯一会花钱的地方,所有失败都必须变成**结构化结果**,绝不向上抛。
4
4
  import { BlockAssembler } from "@deepseek-ai/dsh-llm";
5
+ import { classifyAbort } from "./abort-reason.js";
6
+ import { createDeadline, raceWithDeadline } from "./deadline.js";
5
7
  import { advisorSystemPrompt, buildAdvisorInput, mintNonce } from "./prompt.js";
6
8
 
7
9
  /** 额外调用统一打这个 purpose:也是递归防护的判据(监听器按它跳过自己)。 */
@@ -91,13 +93,10 @@ export async function callAdvisor(input) {
91
93
  { role: "user", content: [{ type: "text", text: userText }] },
92
94
  ]);
93
95
 
94
- // 超时与用户中断合并:任一触发都中止(绝不留下孤儿请求继续烧 token)。
95
- const timeoutSignal = AbortSignal.timeout(settings.timeoutMs);
96
- const signals = [input.signal, timeoutSignal].filter(Boolean);
97
- const signal =
98
- signals.length > 1 && typeof AbortSignal.any === "function"
99
- ? AbortSignal.any(/** @type {AbortSignal[]} */ (signals))
100
- : /** @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;
101
100
 
102
101
  /**
103
102
  * @param {string} effortOverride 传空串表示「这次不带思考档」
@@ -162,10 +161,16 @@ export async function callAdvisor(input) {
162
161
  };
163
162
  }
164
163
  if (settled?.kind === "aborted" || finish?.kind === "aborted") {
164
+ const verdict = classifyAbort({
165
+ // 外部取消 → aborted;配置的超时触发 → timeout。
166
+ cancelled: deadline.cancelled(),
167
+ timedOut: deadline.timedOut(),
168
+ timeoutMs: settings.timeoutMs,
169
+ });
165
170
  return {
166
171
  ok: false,
167
- code: "aborted",
168
- message: "顾问调用被中止",
172
+ code: verdict.code,
173
+ message: verdict.message,
169
174
  ms: Date.now() - startedAt,
170
175
  model,
171
176
  effort: useEffort,
@@ -195,18 +200,45 @@ export async function callAdvisor(input) {
195
200
  };
196
201
  };
197
202
 
198
- try {
203
+ /**
204
+ * 一次尝试;模型不支持思考档时去掉档位再试一次(不无限重试)。
205
+ * @returns {Promise<CallResult>}
206
+ */
207
+ const attempt = async () => {
199
208
  try {
200
209
  return await run(effort);
201
210
  } catch (error) {
202
211
  const code = classifyError(error);
203
- // 模型不支持这个思考档:去掉思考档重试**一次**(不无限重试)。
204
212
  if (code === "UNSUPPORTED_REASONING_EFFORT" || code === "INVALID_MODEL_REASONING") {
205
213
  input.logger?.warn?.(`[advisor] 模型不支持思考档 ${effort || "(默认)"},已降级重试`);
206
214
  return await run("");
207
215
  }
208
216
  throw error;
209
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;
210
242
  } catch (error) {
211
243
  return {
212
244
  ok: false,
@@ -216,5 +248,7 @@ export async function callAdvisor(input) {
216
248
  model,
217
249
  effort,
218
250
  };
251
+ } finally {
252
+ deadline.dispose();
219
253
  }
220
254
  }