@molecule/api-ai-anthropic 1.2.0 → 1.3.1
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/provider.d.ts.map +1 -1
- package/dist/provider.js +34 -2
- package/dist/tool-ids.d.ts +25 -0
- package/dist/tool-ids.d.ts.map +1 -0
- package/dist/tool-ids.js +44 -0
- package/package.json +2 -2
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;AAEH,OAAO,KAAK,EACV,UAAU,EAQX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;
|
|
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;AA6sBjD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,eAAe,GAAG,UAAU,CAEnE"}
|
package/dist/provider.js
CHANGED
|
@@ -14,6 +14,7 @@ const logger = getLogger();
|
|
|
14
14
|
// runtime registry is populated even when provider.js is imported directly
|
|
15
15
|
// (not through the package barrel).
|
|
16
16
|
import './secrets.js';
|
|
17
|
+
import { toAnthropicToolId } from './tool-ids.js';
|
|
17
18
|
/**
|
|
18
19
|
* Builds a `TokenUsage` from the stream state's provider-reported counters —
|
|
19
20
|
* shared by the mid-stream `usage` snapshots and the terminal `done` event so
|
|
@@ -31,8 +32,19 @@ function snapshotUsage(state) {
|
|
|
31
32
|
: {}),
|
|
32
33
|
...(state.cacheReadInputTokens ? { cacheReadInputTokens: state.cacheReadInputTokens } : {}),
|
|
33
34
|
...(state.speed ? { speed: state.speed } : {}),
|
|
35
|
+
...webSearchUsage(state.webSearchRequests ?? state.webSearchBlocks),
|
|
34
36
|
};
|
|
35
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* The `webSearchRequests` part of a `TokenUsage`: the count when searches ran,
|
|
40
|
+
* nothing otherwise.
|
|
41
|
+
*
|
|
42
|
+
* @param count - Web searches billed for the request.
|
|
43
|
+
* @returns A spreadable partial.
|
|
44
|
+
*/
|
|
45
|
+
function webSearchUsage(count) {
|
|
46
|
+
return count ? { webSearchRequests: count } : {};
|
|
47
|
+
}
|
|
36
48
|
/**
|
|
37
49
|
* Narrows the provider-reported speed tier to the TokenUsage union.
|
|
38
50
|
*
|
|
@@ -346,9 +358,18 @@ class AnthropicAIProvider {
|
|
|
346
358
|
text: `[Video attachment (${block.mediaType}) — not supported by this provider]`,
|
|
347
359
|
};
|
|
348
360
|
case 'tool_use':
|
|
349
|
-
return {
|
|
361
|
+
return {
|
|
362
|
+
type: 'tool_use',
|
|
363
|
+
id: toAnthropicToolId(block.id),
|
|
364
|
+
name: block.name,
|
|
365
|
+
input: block.input,
|
|
366
|
+
};
|
|
350
367
|
case 'tool_result':
|
|
351
|
-
return {
|
|
368
|
+
return {
|
|
369
|
+
type: 'tool_result',
|
|
370
|
+
tool_use_id: toAnthropicToolId(block.tool_use_id),
|
|
371
|
+
content: block.content,
|
|
372
|
+
};
|
|
352
373
|
default:
|
|
353
374
|
return block;
|
|
354
375
|
}
|
|
@@ -410,6 +431,7 @@ class AnthropicAIProvider {
|
|
|
410
431
|
? { cacheReadInputTokens: usage.cache_read_input_tokens }
|
|
411
432
|
: {}),
|
|
412
433
|
...(servedSpeed ? { speed: servedSpeed } : {}),
|
|
434
|
+
...webSearchUsage(usage?.server_tool_use?.web_search_requests),
|
|
413
435
|
},
|
|
414
436
|
};
|
|
415
437
|
}
|
|
@@ -437,6 +459,8 @@ class AnthropicAIProvider {
|
|
|
437
459
|
cacheCreationInputTokens: 0,
|
|
438
460
|
cacheReadInputTokens: 0,
|
|
439
461
|
speed: null,
|
|
462
|
+
webSearchRequests: null,
|
|
463
|
+
webSearchBlocks: 0,
|
|
440
464
|
pendingTool: null,
|
|
441
465
|
pendingThinking: null,
|
|
442
466
|
};
|
|
@@ -564,6 +588,11 @@ class AnthropicAIProvider {
|
|
|
564
588
|
else if (block.type === 'thinking') {
|
|
565
589
|
state.pendingThinking = '';
|
|
566
590
|
}
|
|
591
|
+
else if (block.type === 'server_tool_use' && block.name === 'web_search') {
|
|
592
|
+
// Billed per search. Counted here so a stream cut before
|
|
593
|
+
// message_delta (which carries the reported count) still meters it.
|
|
594
|
+
state.webSearchBlocks++;
|
|
595
|
+
}
|
|
567
596
|
}
|
|
568
597
|
else if (eventType === 'content_block_stop') {
|
|
569
598
|
// Emit completed tool_use with accumulated input
|
|
@@ -590,6 +619,9 @@ class AnthropicAIProvider {
|
|
|
590
619
|
}
|
|
591
620
|
else if (eventType === 'message_delta') {
|
|
592
621
|
const usage = event.usage;
|
|
622
|
+
const reportedSearches = usage?.server_tool_use?.web_search_requests;
|
|
623
|
+
if (typeof reportedSearches === 'number')
|
|
624
|
+
state.webSearchRequests = reportedSearches;
|
|
593
625
|
if (usage?.output_tokens) {
|
|
594
626
|
state.outputTokens = usage.output_tokens;
|
|
595
627
|
// Metering snapshot (latest wins): if the stream is cut after this
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool-call ids in the shape the Anthropic Messages API accepts.
|
|
3
|
+
*
|
|
4
|
+
* The API rejects a whole request (HTTP 400, `tool_use.id: String should match
|
|
5
|
+
* pattern '^[a-zA-Z0-9_-]+$'`) when any `tool_use.id` or
|
|
6
|
+
* `tool_result.tool_use_id` in the history contains another character. Other
|
|
7
|
+
* providers accept such ids, so a conversation can carry them — for example a
|
|
8
|
+
* host application's own synthetic tool calls (`plan-approval:<hash>`) or a
|
|
9
|
+
* history started on a different model. One such id makes every later request
|
|
10
|
+
* of that conversation fail.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Maps a tool-call id to one the Anthropic API accepts. Valid ids pass through
|
|
16
|
+
* unchanged. Any other id has each disallowed character replaced by `_` and a
|
|
17
|
+
* digest of the ORIGINAL id appended, so the mapping is deterministic (a
|
|
18
|
+
* `tool_use` and its `tool_result` still match) and two different ids cannot
|
|
19
|
+
* collapse into one.
|
|
20
|
+
*
|
|
21
|
+
* @param id - The id as stored in the conversation.
|
|
22
|
+
* @returns An id matching `^[a-zA-Z0-9_-]+$`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function toAnthropicToolId(id: string): string;
|
|
25
|
+
//# sourceMappingURL=tool-ids.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-ids.d.ts","sourceRoot":"","sources":["../src/tool-ids.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAoBH;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAGpD"}
|
package/dist/tool-ids.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool-call ids in the shape the Anthropic Messages API accepts.
|
|
3
|
+
*
|
|
4
|
+
* The API rejects a whole request (HTTP 400, `tool_use.id: String should match
|
|
5
|
+
* pattern '^[a-zA-Z0-9_-]+$'`) when any `tool_use.id` or
|
|
6
|
+
* `tool_result.tool_use_id` in the history contains another character. Other
|
|
7
|
+
* providers accept such ids, so a conversation can carry them — for example a
|
|
8
|
+
* host application's own synthetic tool calls (`plan-approval:<hash>`) or a
|
|
9
|
+
* history started on a different model. One such id makes every later request
|
|
10
|
+
* of that conversation fail.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
/** The characters the Anthropic API allows in a tool-call id. */
|
|
15
|
+
const VALID_TOOL_ID = /^[a-zA-Z0-9_-]+$/;
|
|
16
|
+
/**
|
|
17
|
+
* A short, stable hex digest (FNV-1a, 32-bit) of a string.
|
|
18
|
+
*
|
|
19
|
+
* @param value - The string to digest.
|
|
20
|
+
* @returns Eight hex characters.
|
|
21
|
+
*/
|
|
22
|
+
function fnv1a(value) {
|
|
23
|
+
let hash = 0x811c9dc5;
|
|
24
|
+
for (let i = 0; i < value.length; i++) {
|
|
25
|
+
hash ^= value.charCodeAt(i);
|
|
26
|
+
hash = Math.imul(hash, 0x01000193);
|
|
27
|
+
}
|
|
28
|
+
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Maps a tool-call id to one the Anthropic API accepts. Valid ids pass through
|
|
32
|
+
* unchanged. Any other id has each disallowed character replaced by `_` and a
|
|
33
|
+
* digest of the ORIGINAL id appended, so the mapping is deterministic (a
|
|
34
|
+
* `tool_use` and its `tool_result` still match) and two different ids cannot
|
|
35
|
+
* collapse into one.
|
|
36
|
+
*
|
|
37
|
+
* @param id - The id as stored in the conversation.
|
|
38
|
+
* @returns An id matching `^[a-zA-Z0-9_-]+$`.
|
|
39
|
+
*/
|
|
40
|
+
export function toAnthropicToolId(id) {
|
|
41
|
+
if (VALID_TOOL_ID.test(id))
|
|
42
|
+
return id;
|
|
43
|
+
return `${id.replace(/[^a-zA-Z0-9_-]/g, '_')}_${fnv1a(id)}`;
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-anthropic",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
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.
|
|
36
|
+
"@molecule/api-ai": "1.3.0",
|
|
37
37
|
"@molecule/api-bond": "1.0.2",
|
|
38
38
|
"@types/node": "26.1.2",
|
|
39
39
|
"typescript": "6.0.3",
|