@cortexkit/pi-anthropic-auth 2.5.1 → 2.5.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.
Files changed (2) hide show
  1. package/dist/stream.js +31 -0
  2. package/package.json +2 -2
package/dist/stream.js CHANGED
@@ -282,6 +282,11 @@ function mapStopReason(reason) {
282
282
  return 'error';
283
283
  }
284
284
  }
285
+ function safeStopLabel(value) {
286
+ if (typeof value !== 'string')
287
+ return undefined;
288
+ return /^[a-z0-9_]{1,40}$/.exec(value)?.[0] === value ? value : undefined;
289
+ }
285
290
  function createOutput(model) {
286
291
  return {
287
292
  role: 'assistant',
@@ -1590,6 +1595,30 @@ export function streamCortexKitAnthropic(model, context, options, effortTransiti
1590
1595
  else if (event.type === 'message_delta') {
1591
1596
  output.stopReason = mapStopReason(String(event.delta?.stop_reason ?? ''));
1592
1597
  updateUsage(model, output, event.usage);
1598
+ if (output.stopReason === 'error' && !output.errorMessage) {
1599
+ const stopReason = safeStopLabel(event.delta?.stop_reason) ?? 'unknown';
1600
+ const details = event.delta?.stop_details;
1601
+ const category = stopReason === 'refusal' &&
1602
+ details !== null &&
1603
+ typeof details === 'object' &&
1604
+ 'type' in details &&
1605
+ details.type === 'refusal' &&
1606
+ 'category' in details
1607
+ ? safeStopLabel(details.category)
1608
+ : undefined;
1609
+ // Provider explanations can echo the prompt; expose only bounded labels.
1610
+ output.errorMessage =
1611
+ stopReason === 'refusal'
1612
+ ? `Claude refused this request (stop_reason: refusal${category ? `, category: ${category}` : ''}).`
1613
+ : `Claude ended this request with an unsupported stop reason (stop_reason: ${stopReason}).`;
1614
+ logger.warn('pi', 'Anthropic response ended with an error stop reason', {
1615
+ stopReason,
1616
+ ...(category ? { category } : {}),
1617
+ model: model.id,
1618
+ sessionId: options?.sessionId,
1619
+ });
1620
+ failThread?.();
1621
+ }
1593
1622
  }
1594
1623
  else if (event.type === 'error') {
1595
1624
  throw new Error(JSON.stringify(event));
@@ -1597,6 +1626,8 @@ export function streamCortexKitAnthropic(model, context, options, effortTransiti
1597
1626
  }
1598
1627
  if (options?.signal?.aborted)
1599
1628
  throw new Error('Request was aborted');
1629
+ if (output.errorMessage)
1630
+ throw new Error(output.errorMessage);
1600
1631
  for (const block of output.content)
1601
1632
  delete block.index;
1602
1633
  stream.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-anthropic-auth",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,7 +36,7 @@
36
36
  "prepublishOnly": "bun run build"
37
37
  },
38
38
  "dependencies": {
39
- "@cortexkit/anthropic-auth-core": "2.5.1"
39
+ "@cortexkit/anthropic-auth-core": "2.5.3"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@earendil-works/pi-ai": ">=0.86.1 <2",