@molecule/api-ai-anthropic 1.3.3 → 1.3.4

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 +1 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,UAAU,EAQX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAGrB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAqxBjD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,eAAe,GAAG,UAAU,CAEnE"}
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,UAAU,EAQX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAGrB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AA+1BjD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,eAAe,GAAG,UAAU,CAEnE"}
package/dist/provider.js CHANGED
@@ -211,12 +211,35 @@ class AnthropicAIProvider {
211
211
  // with the cancelled response still in `response`.
212
212
  let releasedBody = false;
213
213
  for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
214
- response = await fetch(`${this.baseUrl}/v1/messages`, {
215
- method: 'POST',
216
- headers,
217
- body: JSON.stringify(body),
218
- signal,
219
- });
214
+ try {
215
+ response = await fetch(`${this.baseUrl}/v1/messages`, {
216
+ method: 'POST',
217
+ headers,
218
+ body: JSON.stringify(body),
219
+ signal,
220
+ });
221
+ }
222
+ catch (error) {
223
+ // The CALLER's own abort is theirs to see — the same exception the
224
+ // mid-stream path deliberately keeps. Everything else reaching here is
225
+ // a transport failure BEFORE any response arrived: DNS, TLS, a
226
+ // connection reset, or this bond's own 5-minute default timeout firing
227
+ // while awaiting the headers (the identical provider-owned timeout the
228
+ // mid-stream path answers with a sanitized event). A raw `TypeError:
229
+ // fetch failed` / `TimeoutError` escaping this generator carries no
230
+ // `errorKey`, so the caller's error classification never fires for that
231
+ // whole phase — answer with the sanitized event every other failure of
232
+ // this method yields.
233
+ if (params.signal?.aborted)
234
+ throw error;
235
+ logger.error('Anthropic request failed before a response arrived', { error });
236
+ yield {
237
+ type: 'error',
238
+ message: 'AI service error. Please try again.',
239
+ errorKey: 'ai.error.apiError',
240
+ };
241
+ return;
242
+ }
220
243
  releasedBody = false;
221
244
  if (response.status === 429 || response.status === 529 || response.status === 503) {
222
245
  const willRetry = attempt < MAX_RETRIES;
@@ -271,6 +294,18 @@ class AnthropicAIProvider {
271
294
  delayMs,
272
295
  });
273
296
  await new Promise((resolve) => {
297
+ // A signal that aborted BEFORE this wait began never fires another
298
+ // `abort` event — the DOM dispatches it once — so a listener
299
+ // registered now would never run and the caller would sit out the
300
+ // whole backoff (up to the 60 s Retry-After cap) before the
301
+ // `signal.aborted` check below notices. The window is real: the
302
+ // body release two statements up (`await response.body?.cancel()`)
303
+ // is an await point between the 429 arriving and this sleep. The
304
+ // liquid-d1 retry sleep carries the same pre-check.
305
+ if (signal.aborted) {
306
+ resolve();
307
+ return;
308
+ }
274
309
  const timer = setTimeout(() => {
275
310
  // The window elapsed normally: drop the abort listener. `{ once:
276
311
  // true }` only removes it when abort FIRES, so a caller signal
@@ -288,6 +323,19 @@ class AnthropicAIProvider {
288
323
  // If aborted while waiting, resolve immediately
289
324
  signal.addEventListener('abort', onAbort, { once: true });
290
325
  });
326
+ // The CALLER's own abort ends the retry and stays the caller's to
327
+ // see, exactly as it is in this method's other two phases (the
328
+ // pre-response catch rethrows it, and so does the mid-stream path:
329
+ // "rethrowing its reason is what every abort-aware consumer
330
+ // expects"). Fall through to the error path below instead and a
331
+ // CANCELLED request is answered with the DISCARDED response's
332
+ // message — "AI rate limit exceeded. Please try again shortly." — so
333
+ // a user who pressed Stop saw a rate-limit error and the caller's
334
+ // abort/retry handling never fired. Only the caller's signal is
335
+ // rethrown: this bond's own 5-minute default timeout is a deadline,
336
+ // not a cancellation, and keeps answering with the error event.
337
+ if (params.signal?.aborted)
338
+ throw params.signal.reason;
291
339
  if (signal.aborted)
292
340
  break;
293
341
  continue;
@@ -300,8 +348,24 @@ class AnthropicAIProvider {
300
348
  // throws "Body is unusable" and would replace the sanitized error event
301
349
  // with a raw TypeError that reads as a crash, not an API answer (the
302
350
  // only way a cancelled body is still `response` here is the caller's
303
- // signal aborting during the backoff sleep).
304
- const errorBody = releasedBody ? '' : await response.text();
351
+ // signal aborting during the backoff sleep). A body that dies MID-READ
352
+ // (a connection reset while the error document streams) fails the same
353
+ // way, so the read is guarded too: the status is already known, and an
354
+ // unreadable detail can only downgrade the message — it must never
355
+ // escape this generator as a raw `TypeError: terminated`, which carries
356
+ // no `errorKey` and so defeats the caller's error classification.
357
+ let errorBody = '';
358
+ if (!releasedBody) {
359
+ try {
360
+ errorBody = await response.text();
361
+ }
362
+ catch (error) {
363
+ logger.warn('Anthropic API error body could not be read', {
364
+ status: response.status,
365
+ error,
366
+ });
367
+ }
368
+ }
305
369
  let detail = `HTTP ${response.status}`;
306
370
  try {
307
371
  const parsed = JSON.parse(errorBody);
@@ -600,6 +664,21 @@ class AnthropicAIProvider {
600
664
  }
601
665
  }
602
666
  finally {
667
+ // Cancel the body whenever this generator stops before the stream is
668
+ // exhausted — a mid-stream SSE `error` event (overloaded_error) or a
669
+ // consumer that stops iterating. A live-but-unread body holds its
670
+ // connection out of the pool until GC reclaims it — the same strand the
671
+ // retry loop's body release fixes for discarded 429s. Cancel is a no-op
672
+ // on an exhausted stream, and best-effort overall: a body already
673
+ // destroyed by a caller abort or a connection reset must not turn
674
+ // cleanup into a secondary failure.
675
+ try {
676
+ await reader.cancel();
677
+ }
678
+ catch (_error) {
679
+ // Best-effort release only — the body may already be destroyed, or the
680
+ // transport may not expose cancel; the lock release below still runs.
681
+ }
603
682
  reader.releaseLock();
604
683
  }
605
684
  yield { type: 'done', usage: snapshotUsage(state) };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-ai-anthropic",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "Anthropic Claude AI provider for molecule.dev",
5
5
  "homepage": "https://www.molecule.dev/packages/api-ai-anthropic",
6
6
  "type": "module",