@molecule/api-ai-anthropic 1.3.4 → 1.3.5
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 +7 -7
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +48 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ 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-
|
|
6
|
+
Generated: 2026-10-16T00:29:26.168Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-ai-anthropic
|
|
@@ -95,20 +95,20 @@ function createProvider(config?: AnthropicConfig): AIProvider
|
|
|
95
95
|
|
|
96
96
|
### Constants
|
|
97
97
|
|
|
98
|
-
#### `
|
|
98
|
+
#### `provider`
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
The provider implementation.
|
|
101
101
|
|
|
102
102
|
```typescript
|
|
103
|
-
const
|
|
103
|
+
const provider: AIProvider
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
-
#### `
|
|
106
|
+
#### `aiAnthropicSecretDefinitions`
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
Secret definitions required by the Anthropic AI bond.
|
|
109
109
|
|
|
110
110
|
```typescript
|
|
111
|
-
const
|
|
111
|
+
const aiAnthropicSecretDefinitions: SecretDefinition[]
|
|
112
112
|
```
|
|
113
113
|
|
|
114
114
|
## Core Interface
|
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;
|
|
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.
|
|
3
|
+
"version": "1.3.5",
|
|
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,7 +33,7 @@
|
|
|
33
33
|
"directory": "packages/api/bonds/ai/anthropic"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@molecule/api-ai": "1.3.
|
|
36
|
+
"@molecule/api-ai": "1.3.3",
|
|
37
37
|
"@molecule/api-bond": "1.0.2",
|
|
38
38
|
"@types/node": "26.1.2",
|
|
39
39
|
"typescript": "6.0.3",
|