@molecule/api-ai-anthropic 1.3.4 → 1.3.6

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/README.md CHANGED
@@ -3,17 +3,17 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
3
  Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
4
  Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
5
  To change this document, edit the module-level JSDoc in src/index.ts.
6
- Generated: 2026-10-01T18:20:43.990Z
6
+ Generated: 2026-10-16T23:28:07.527Z
7
7
  -->
8
8
 
9
9
  # @molecule/api-ai-anthropic
10
10
 
11
+ Anthropic ai-anthropic provider for molecule.dev.
12
+
11
13
  > **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
12
14
  > It is written to be read by coding agents as much as by people, and is generated from this
13
15
  > package's source — edit `src/index.ts` JSDoc, not this file.
14
16
 
15
- Anthropic ai-anthropic provider for molecule.dev.
16
-
17
17
  ## Quick Start
18
18
 
19
19
  ```typescript
@@ -95,20 +95,20 @@ function createProvider(config?: AnthropicConfig): AIProvider
95
95
 
96
96
  ### Constants
97
97
 
98
- #### `aiAnthropicSecretDefinitions`
98
+ #### `provider`
99
99
 
100
- Secret definitions required by the Anthropic AI bond.
100
+ The provider implementation.
101
101
 
102
102
  ```typescript
103
- const aiAnthropicSecretDefinitions: SecretDefinition[]
103
+ const provider: AIProvider
104
104
  ```
105
105
 
106
- #### `provider`
106
+ #### `aiAnthropicSecretDefinitions`
107
107
 
108
- The provider implementation.
108
+ Secret definitions required by the Anthropic AI bond.
109
109
 
110
110
  ```typescript
111
- const provider: AIProvider
111
+ const aiAnthropicSecretDefinitions: SecretDefinition[]
112
112
  ```
113
113
 
114
114
  ## Core Interface
@@ -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;AA+1BjD;;;;;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;AAIH,OAAO,KAAK,EACV,UAAU,EASX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAGrB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AA25BjD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,eAAe,GAAG,UAAU,CAEnE"}
package/dist/provider.js CHANGED
@@ -7,6 +7,7 @@
7
7
  *
8
8
  * @module
9
9
  */
10
+ import { randomUUID } from 'node:crypto';
10
11
  import { getLogger } from '@molecule/api-bond';
11
12
  import { t } from '@molecule/api-i18n';
12
13
  const logger = getLogger();
@@ -179,11 +180,21 @@ class AnthropicAIProvider {
179
180
  else if (params.temperature !== undefined) {
180
181
  body.temperature = params.temperature;
181
182
  }
183
+ // Per-call correlation (see ChatParams.requestId): a header rather than the
184
+ // body's `metadata.user_id`, which is the stable per-account
185
+ // abuse-attribution value. Anthropic's `request-id` header comes back with
186
+ // the response and is recorded as the trace id.
187
+ const requestId = params.requestId ?? randomUUID();
188
+ const call = {
189
+ meta: { requestId, endpoint: `${this.baseUrl}/v1/messages` },
190
+ endUserId: params.endUserId,
191
+ };
182
192
  const headers = {
183
193
  'Content-Type': 'application/json',
184
194
  // Omitted for a keyless custom endpoint (a self-hosted / local server).
185
195
  ...(this.apiKey ? { 'x-api-key': this.apiKey } : {}),
186
196
  'anthropic-version': '2023-06-01',
197
+ 'X-Mol-Request-Id': requestId,
187
198
  };
188
199
  // Build anthropic-beta header — features that require beta flags.
189
200
  // Prompt caching is GA (no longer needs a beta flag). Interleaved thinking
@@ -440,8 +451,13 @@ class AnthropicAIProvider {
440
451
  // HTTP-level path and the mid-stream SSE `error` path keep). The CALLER's
441
452
  // own abort is the one exception: it is the caller's act, and rethrowing
442
453
  // its reason is what every abort-aware consumer expects.
454
+ // Anthropic's own trace handle for this request, alongside the `id` of the
455
+ // message it produces (recorded by the parser).
456
+ const traceId = response.headers.get('request-id') ?? response.headers.get('x-request-id') ?? undefined;
457
+ if (traceId)
458
+ call.meta.traceId = traceId;
443
459
  try {
444
- yield* this.parseStreamingResponse(response);
460
+ yield* this.parseStreamingResponse(response, call);
445
461
  }
446
462
  catch (error) {
447
463
  if (params.signal?.aborted)
@@ -584,7 +600,7 @@ class AnthropicAIProvider {
584
600
  * @param response - The fetch Response with an SSE body stream.
585
601
  * @yields {ChatEvent} Chat events as they arrive from the SSE stream.
586
602
  */
587
- async *parseStreamingResponse(response) {
603
+ async *parseStreamingResponse(response, call) {
588
604
  const reader = response.body?.getReader();
589
605
  if (!reader) {
590
606
  yield {
@@ -616,7 +632,7 @@ class AnthropicAIProvider {
616
632
  const lines = buffer.split('\n');
617
633
  buffer = lines.pop() ?? '';
618
634
  let yielded = false;
619
- for (const event of this.processSSELines(lines, state)) {
635
+ for (const event of this.processSSELines(lines, state, call)) {
620
636
  yielded = true;
621
637
  yield event;
622
638
  // A mid-stream provider error (Anthropic sends `overloaded_error` as an
@@ -639,7 +655,7 @@ class AnthropicAIProvider {
639
655
  }
640
656
  // Flush any remaining data in the buffer after EOF
641
657
  if (buffer.trim()) {
642
- for (const event of this.processSSELines(buffer.split('\n'), state)) {
658
+ for (const event of this.processSSELines(buffer.split('\n'), state, call)) {
643
659
  yield event;
644
660
  if (event.type === 'error')
645
661
  return;
@@ -681,7 +697,7 @@ class AnthropicAIProvider {
681
697
  }
682
698
  reader.releaseLock();
683
699
  }
684
- yield { type: 'done', usage: snapshotUsage(state) };
700
+ yield { type: 'done', usage: snapshotUsage(state), provider: call.meta };
685
701
  }
686
702
  /**
687
703
  * Processes an array of SSE lines from the Anthropic streaming response, yielding
@@ -691,7 +707,7 @@ class AnthropicAIProvider {
691
707
  * @param state - Mutable stream state (token counters, pending blocks).
692
708
  * @yields {ChatEvent} Chat events extracted from the SSE lines.
693
709
  */
694
- *processSSELines(lines, state) {
710
+ *processSSELines(lines, state, call) {
695
711
  for (const line of lines) {
696
712
  if (!line.startsWith('data: '))
697
713
  continue;
@@ -701,6 +717,32 @@ class AnthropicAIProvider {
701
717
  try {
702
718
  const event = JSON.parse(json);
703
719
  const eventType = event.type;
720
+ // The reply's own identity, recorded for the caller: Anthropic names the
721
+ // message it is streaming in `message_start` (and repeats it on
722
+ // `message_delta`), with the model that answered. Recorded rather than
723
+ // asserted — a proxy in front of the API may rewrite either.
724
+ const message = event.message;
725
+ if (typeof message?.id === 'string')
726
+ call.meta.responseId = message.id;
727
+ if (typeof message?.model === 'string')
728
+ call.meta.model = message.model;
729
+ // Anthropic echoes no request identity today; a proxy that does is
730
+ // checked here so another request's reply is never streamed on as ours.
731
+ const echoedRequest = typeof event.request_id === 'string'
732
+ ? event.request_id
733
+ : typeof event.requestId === 'string'
734
+ ? event.requestId
735
+ : undefined;
736
+ if (echoedRequest !== undefined && echoedRequest !== call.meta.requestId) {
737
+ logger.error('Provider echoed a different identity — this reply is not ours', {
738
+ requestId: call.meta.requestId,
739
+ echoedRequestId: echoedRequest,
740
+ responseId: call.meta.responseId,
741
+ traceId: call.meta.traceId,
742
+ });
743
+ yield { type: 'error', message: t('ai.error.apiError'), errorKey: 'ai.error.apiError' };
744
+ return;
745
+ }
704
746
  if (eventType === 'content_block_delta') {
705
747
  const delta = event.delta;
706
748
  if (delta.type === 'text_delta') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-ai-anthropic",
3
- "version": "1.3.4",
3
+ "version": "1.3.6",
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",
@@ -33,11 +33,11 @@
33
33
  "directory": "packages/api/bonds/ai/anthropic"
34
34
  },
35
35
  "devDependencies": {
36
- "@molecule/api-ai": "1.3.0",
37
- "@molecule/api-bond": "1.0.2",
38
- "@types/node": "26.1.2",
36
+ "@molecule/api-ai": "1.3.5",
37
+ "@molecule/api-bond": "1.0.3",
38
+ "@types/node": "26.6.5",
39
39
  "typescript": "6.0.3",
40
- "vitest": "4.1.11"
40
+ "vitest": "4.1.12"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "@molecule/api-ai": "^1.0.1",