@cortexkit/pi-anthropic-auth 2.5.2 → 2.5.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.
Files changed (2) hide show
  1. package/dist/stream.js +36 -4
  2. package/package.json +2 -2
package/dist/stream.js CHANGED
@@ -1,4 +1,4 @@
1
- import { applyClaudeCodeHeaders, applyCustomHeaders, CACHE_KEEP_EXTENDED_TTL_BETA, CacheKeepManager, CacheKeepSessionRegistry, createNativeVaultNoRouteResponse, createProviderErrorResponseScope, createQuotaRefusalResponse, createSseFramer, createStickyNoRouteResponse, decideStickyQuotaFailure, decodeSseFrame, dumpDirectRequest, effectiveRelayTransport, FAST_MODE_BETA, getCache1hPersistentMode, getClaudeCodeIdentityForVerifiedAccount, getDefaultCacheKeepRegistryDirectory, getFallbackReauthLabels, getMaxSameAccountWait, getQuotaCheckIntervalMs, getRequestGzipLatch, getRequestGzipLatchPath, getRoutingMode, getScopedQuotaWindowForModel, getThinkingPrefixMismatchBehavior, isCache1hPersistentlyEnabled, isCacheKeepHybridActive, isDumpPersistentlyEnabled, isFastModePersistentlyEnabled, isKillswitchEnabled, isNativeTransientAccountError, isOAuthAccount, isPermanentRefreshError, isValidApiBaseURL, killswitchPassesPolicy, logger, MessageThreadTracker, mergeAnthropicBetas, NativeAccountRefusal, nativeAccountCanServe, parseSseFrame, prepareMessageThreadRequest, QuotaManager, quotaBackoffActive, quotaExhaustionFromHeaders, quotaRefusalTiming, quotaSnapshotHasStandardWindows, quotaSnapshotModelScopeIsExhausted, quotaSnapshotPassesModelScope, quotaSnapshotPassesPolicy, runWithRequestSignal, STICKY_ROUTING_MAIN_ACCOUNT_ID, StickyQuotaRetryCounter, StickySessionRouter, sendViaRelay, sendWithRequestGzip, sessionCacheActivity, setDumpEnabled, shouldFallbackStatus, signRequestBody, stickyQuotaSnapshotIsFresh, stickyRetryAfterWithJitter, stickyRouteFamilyForModel, } from '@cortexkit/anthropic-auth-core';
1
+ import { applyClaudeCodeHeaders, applyCustomHeaders, CACHE_KEEP_EXTENDED_TTL_BETA, CacheKeepManager, CacheKeepSessionRegistry, createNativeVaultNoRouteResponse, createProviderErrorResponseScope, createQuotaRefusalResponse, createSseFramer, createStickyNoRouteResponse, decideStickyQuotaFailure, decodeSseFrame, dumpDirectRequest, effectiveRelayTransport, FAST_MODE_BETA, getCache1hPersistentMode, getCacheKeepRegistryDirectories, getClaudeCodeIdentityForVerifiedAccount, getFallbackReauthLabels, getMaxSameAccountWait, getQuotaCheckIntervalMs, getRequestGzipLatch, getRequestGzipLatchPath, getRoutingMode, getScopedQuotaWindowForModel, getThinkingPrefixMismatchBehavior, isCache1hPersistentlyEnabled, isCacheKeepHybridActive, isDumpPersistentlyEnabled, isFastModePersistentlyEnabled, isKillswitchEnabled, isNativeTransientAccountError, isOAuthAccount, isPermanentRefreshError, isValidApiBaseURL, killswitchPassesPolicy, logger, MessageThreadTracker, mergeAnthropicBetas, NativeAccountRefusal, nativeAccountCanServe, parseSseFrame, prepareMessageThreadRequest, QuotaManager, quotaBackoffActive, quotaExhaustionFromHeaders, quotaRefusalTiming, quotaSnapshotHasStandardWindows, quotaSnapshotModelScopeIsExhausted, quotaSnapshotPassesModelScope, quotaSnapshotPassesPolicy, runWithRequestSignal, STICKY_ROUTING_MAIN_ACCOUNT_ID, StickyQuotaRetryCounter, StickySessionRouter, sendViaRelay, sendWithRequestGzip, sessionCacheActivity, setDumpEnabled, shouldFallbackStatus, signRequestBody, stickyQuotaSnapshotIsFresh, stickyRetryAfterWithJitter, stickyRouteFamilyForModel, } from '@cortexkit/anthropic-auth-core';
2
2
  import { calculateCost, createAssistantMessageEventStream, } from '@earendil-works/pi-ai';
3
3
  import { buildAnthropicRequest, fromClaudeCodeToolName } from './convert.js';
4
4
  import { getPiNativeRuntime, piAttemptAccessToken, } from './native.js';
@@ -167,10 +167,10 @@ export async function clearPiStickyRoutingSession(storagePath, sessionId) {
167
167
  await (await getPiStickyRouter(storagePath)).clear(sessionId);
168
168
  }
169
169
  function getPiCacheKeepRegistry() {
170
- const directory = process.env.PI_ANTHROPIC_AUTH_CACHEKEEP_REGISTRY_DIR ||
171
- getDefaultCacheKeepRegistryDirectory('pi');
170
+ const paths = getCacheKeepRegistryDirectories('pi');
171
+ const { directory } = paths;
172
172
  if (!cacheKeepRegistry || cacheKeepRegistryDirectory !== directory) {
173
- cacheKeepRegistry = new CacheKeepSessionRegistry({ directory });
173
+ cacheKeepRegistry = new CacheKeepSessionRegistry(paths);
174
174
  cacheKeepRegistryDirectory = directory;
175
175
  }
176
176
  return cacheKeepRegistry;
@@ -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',
@@ -460,6 +465,7 @@ async function sendAnthropicRequest(options, deps = {}) {
460
465
  bodyText,
461
466
  storage,
462
467
  cacheMode: isCacheKeepHybridActive(storage) ? 'hybrid' : 'disabled',
468
+ ttlMs: cacheTtlMs,
463
469
  oauthAccountId: routeId,
464
470
  oauthAccountIdentity: verifiedUuid,
465
471
  accountStoragePath: options.storagePath,
@@ -1590,6 +1596,30 @@ export function streamCortexKitAnthropic(model, context, options, effortTransiti
1590
1596
  else if (event.type === 'message_delta') {
1591
1597
  output.stopReason = mapStopReason(String(event.delta?.stop_reason ?? ''));
1592
1598
  updateUsage(model, output, event.usage);
1599
+ if (output.stopReason === 'error' && !output.errorMessage) {
1600
+ const stopReason = safeStopLabel(event.delta?.stop_reason) ?? 'unknown';
1601
+ const details = event.delta?.stop_details;
1602
+ const category = stopReason === 'refusal' &&
1603
+ details !== null &&
1604
+ typeof details === 'object' &&
1605
+ 'type' in details &&
1606
+ details.type === 'refusal' &&
1607
+ 'category' in details
1608
+ ? safeStopLabel(details.category)
1609
+ : undefined;
1610
+ // Provider explanations can echo the prompt; expose only bounded labels.
1611
+ output.errorMessage =
1612
+ stopReason === 'refusal'
1613
+ ? `Claude refused this request (stop_reason: refusal${category ? `, category: ${category}` : ''}).`
1614
+ : `Claude ended this request with an unsupported stop reason (stop_reason: ${stopReason}).`;
1615
+ logger.warn('pi', 'Anthropic response ended with an error stop reason', {
1616
+ stopReason,
1617
+ ...(category ? { category } : {}),
1618
+ model: model.id,
1619
+ sessionId: options?.sessionId,
1620
+ });
1621
+ failThread?.();
1622
+ }
1593
1623
  }
1594
1624
  else if (event.type === 'error') {
1595
1625
  throw new Error(JSON.stringify(event));
@@ -1597,6 +1627,8 @@ export function streamCortexKitAnthropic(model, context, options, effortTransiti
1597
1627
  }
1598
1628
  if (options?.signal?.aborted)
1599
1629
  throw new Error('Request was aborted');
1630
+ if (output.errorMessage)
1631
+ throw new Error(output.errorMessage);
1600
1632
  for (const block of output.content)
1601
1633
  delete block.index;
1602
1634
  stream.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-anthropic-auth",
3
- "version": "2.5.2",
3
+ "version": "2.5.4",
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.2"
39
+ "@cortexkit/anthropic-auth-core": "2.5.4"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@earendil-works/pi-ai": ">=0.86.1 <2",