@cortexkit/pi-anthropic-auth 2.4.11 → 2.4.12

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/dist/stream.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { type ApiKeyAccount, CacheKeepManager, type MidConversationEffortTransition, type QuotaManagerOptions } from '@cortexkit/anthropic-auth-core';
2
- import { type Api, type AssistantMessageEventStream, type Context, type Model, type SimpleStreamOptions } from '@earendil-works/pi-ai';
1
+ import { type ApiKeyAccount, CacheKeepManager, type MidConversationEffortTransition, type NativeAccountView, type QuotaManagerOptions } from '@cortexkit/anthropic-auth-core';
2
+ import { type Api, type AssistantMessageEventStream, type Context, type Model, type SimpleStreamOptions, type Tool } from '@earendil-works/pi-ai';
3
3
  /** Set or clear the test replacement for the wait between quota usage polls. */
4
4
  export declare function __setPiQuotaCallPauseForTest(pause: QuotaManagerOptions['pauseBetweenQuotaCalls']): void;
5
5
  export declare function clearPiStickyRoutingSession(storagePath: string, sessionId: string): Promise<void>;
@@ -20,6 +20,22 @@ type AnthropicEvent = {
20
20
  export declare function buildExplicitBaseMessagesUrl(baseURL: string): URL;
21
21
  export declare function configureApiRouteHeaders(account: ApiKeyAccount, fastMode: boolean): Headers;
22
22
  export declare function parseSse(response: Response): AsyncGenerator<AnthropicEvent>;
23
+ declare function sendAnthropicRequest(options: {
24
+ model: Model<Api>;
25
+ context: Context;
26
+ streamOptions?: SimpleStreamOptions;
27
+ apiAccount?: NativeAccountView;
28
+ storagePath: string;
29
+ oauthAccountId?: string;
30
+ route?: string;
31
+ effortTransitions?: readonly MidConversationEffortTransition[];
32
+ onResolvedTools?: (tools: Tool[]) => void;
33
+ }, deps?: {
34
+ fetch?: typeof fetch;
35
+ cacheKeep?: CacheKeepManager;
36
+ }): Promise<Response>;
37
+ /** Exercise one native send with isolated transport and scheduling in tests. */
38
+ export declare const __sendPiAnthropicRequestForTest: typeof sendAnthropicRequest;
23
39
  export declare function primaryResponseAllowsApiFallback(preflight: Response | string): boolean;
24
40
  /**
25
41
  * Classify a successful stream by its first event without a tee: the bytes
package/dist/stream.js CHANGED
@@ -335,7 +335,9 @@ export async function* parseSse(response) {
335
335
  reader.releaseLock();
336
336
  }
337
337
  }
338
- async function sendAnthropicRequest(options) {
338
+ async function sendAnthropicRequest(options, deps = {}) {
339
+ const fetchImpl = deps.fetch ?? globalThis.fetch;
340
+ const keep = deps.cacheKeep ?? cacheKeepManager;
339
341
  const native = getPiNativeRuntime(options.storagePath);
340
342
  // Capture the serving service while authorization is open. Shutdown rejects
341
343
  // new serving work immediately but drains writes for already-served attempts.
@@ -385,8 +387,10 @@ async function sendAnthropicRequest(options) {
385
387
  signal: options.streamOptions?.signal,
386
388
  ...(!options.apiAccount && { redirect: 'error' }),
387
389
  };
388
- if (!options.apiAccount)
389
- await cacheKeepManager.track({
390
+ const trackAcceptedRequest = () => {
391
+ if (options.apiAccount || init.signal?.aborted)
392
+ return;
393
+ keep.track({
390
394
  sessionId: relayAffinity,
391
395
  url: input.toString(),
392
396
  headers,
@@ -397,6 +401,7 @@ async function sendAnthropicRequest(options) {
397
401
  oauthAccountIdentity: verifiedUuid,
398
402
  accountStoragePath: options.storagePath,
399
403
  });
404
+ };
400
405
  const reported = new Set();
401
406
  let retried401 = false;
402
407
  let lastTransport;
@@ -455,13 +460,13 @@ async function sendAnthropicRequest(options) {
455
460
  // OAuth bodies go gzipped as Claude Code sends them; `bodyText` stays
456
461
  // the signed identity text for dumps and CacheKeep.
457
462
  if (options.apiAccount || typeof init.body !== 'string')
458
- return scope.redactResponse(await fetch(input, init));
463
+ return scope.redactResponse(await fetchImpl(input, init));
459
464
  return scope.redactResponse(await sendWithRequestGzip({
460
465
  url: input,
461
466
  body: init.body,
462
467
  headers,
463
468
  latch: getRequestGzipLatch(getRequestGzipLatchPath(options.storagePath)),
464
- send: (wireBody, wireHeaders) => fetch(input, { ...init, body: wireBody, headers: wireHeaders }),
469
+ send: (wireBody, wireHeaders) => fetchImpl(input, { ...init, body: wireBody, headers: wireHeaders }),
465
470
  }));
466
471
  };
467
472
  let response;
@@ -537,8 +542,10 @@ async function sendAnthropicRequest(options) {
537
542
  const subject = apiSubject;
538
543
  native.usagePublications.publish(() => servingRuntime.publishApi(subject, { lastUsed: Date.now() }));
539
544
  }
540
- if (attempt && response.ok)
545
+ if (attempt && response.ok) {
546
+ trackAcceptedRequest();
541
547
  markServed(attempt);
548
+ }
542
549
  return response;
543
550
  };
544
551
  if (options.apiAccount)
@@ -601,10 +608,14 @@ async function sendAnthropicRequest(options) {
601
608
  // If the request was cancelled since, end it as cancelled here.
602
609
  if (relay401Attempt && response.status === 401)
603
610
  await endIfCancelled(response, init.signal);
604
- if (response.ok && lastTransport === 'relay' && relayAttempt)
611
+ if (response.ok && lastTransport === 'relay' && relayAttempt) {
612
+ trackAcceptedRequest();
605
613
  markServed(relayAttempt);
614
+ }
606
615
  return response;
607
616
  }
617
+ /** Exercise one native send with isolated transport and scheduling in tests. */
618
+ export const __sendPiAnthropicRequestForTest = sendAnthropicRequest;
608
619
  /**
609
620
  * If the request's signal is aborted, cancels the response body and throws the
610
621
  * signal's abort error; otherwise does nothing. Call it before a rejected
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/pi-anthropic-auth",
3
- "version": "2.4.11",
3
+ "version": "2.4.12",
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.4.11"
39
+ "@cortexkit/anthropic-auth-core": "2.4.12"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "@earendil-works/pi-ai": ">=0.86.1 <2",