@molecule/api-ai-anthropic 1.3.0 → 1.3.2
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 +21 -1
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -0
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +12 -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 +1 -1
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-
|
|
6
|
+
Generated: 2026-10-01T18:20:43.990Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-ai-anthropic
|
|
@@ -14,6 +14,26 @@ Generated: 2026-09-23T14:02:08.567Z
|
|
|
14
14
|
|
|
15
15
|
Anthropic ai-anthropic provider for molecule.dev.
|
|
16
16
|
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
// npm install @molecule/api-ai-anthropic --workspace=api
|
|
21
|
+
import { setProvider, requireProvider } from '@molecule/api-ai'
|
|
22
|
+
import { provider } from '@molecule/api-ai-anthropic'
|
|
23
|
+
|
|
24
|
+
// Named registration — several AI providers can be bonded side by side
|
|
25
|
+
// (getProviderByName('anthropic') targets this one); the FIRST one
|
|
26
|
+
// registered also answers requireProvider() (see @molecule/api-ai).
|
|
27
|
+
setProvider('anthropic', provider) // reads ANTHROPIC_API_KEY from the environment
|
|
28
|
+
|
|
29
|
+
let reply = ''
|
|
30
|
+
for await (const event of requireProvider().chat({
|
|
31
|
+
messages: [{ role: 'user', content: 'Hello!' }],
|
|
32
|
+
})) {
|
|
33
|
+
if (event.type === 'text') reply += event.content // or forward the chunk to the client (SSE)
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
17
37
|
## Type
|
|
18
38
|
|
|
19
39
|
`provider`
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Anthropic ai-anthropic provider for molecule.dev.
|
|
3
3
|
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* // npm install @molecule/api-ai-anthropic --workspace=api
|
|
7
|
+
* import { setProvider, requireProvider } from '@molecule/api-ai'
|
|
8
|
+
* import { provider } from '@molecule/api-ai-anthropic'
|
|
9
|
+
*
|
|
10
|
+
* // Named registration — several AI providers can be bonded side by side
|
|
11
|
+
* // (getProviderByName('anthropic') targets this one); the FIRST one
|
|
12
|
+
* // registered also answers requireProvider() (see @molecule/api-ai).
|
|
13
|
+
* setProvider('anthropic', provider) // reads ANTHROPIC_API_KEY from the environment
|
|
14
|
+
*
|
|
15
|
+
* let reply = ''
|
|
16
|
+
* for await (const event of requireProvider().chat({
|
|
17
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
18
|
+
* })) {
|
|
19
|
+
* if (event.type === 'text') reply += event.content // or forward the chunk to the client (SSE)
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
4
23
|
* @remarks
|
|
5
24
|
* Bond this as an AI provider (see `@molecule/api-ai` for the `chat()` streaming loop and the
|
|
6
25
|
* key-server-side / never-blindly-trust-model-output rules). Config: `ANTHROPIC_API_KEY`
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAE1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAMlD;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,UAUrB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Anthropic ai-anthropic provider for molecule.dev.
|
|
3
3
|
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* // npm install @molecule/api-ai-anthropic --workspace=api
|
|
7
|
+
* import { setProvider, requireProvider } from '@molecule/api-ai'
|
|
8
|
+
* import { provider } from '@molecule/api-ai-anthropic'
|
|
9
|
+
*
|
|
10
|
+
* // Named registration — several AI providers can be bonded side by side
|
|
11
|
+
* // (getProviderByName('anthropic') targets this one); the FIRST one
|
|
12
|
+
* // registered also answers requireProvider() (see @molecule/api-ai).
|
|
13
|
+
* setProvider('anthropic', provider) // reads ANTHROPIC_API_KEY from the environment
|
|
14
|
+
*
|
|
15
|
+
* let reply = ''
|
|
16
|
+
* for await (const event of requireProvider().chat({
|
|
17
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
18
|
+
* })) {
|
|
19
|
+
* if (event.type === 'text') reply += event.content // or forward the chunk to the client (SSE)
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
4
23
|
* @remarks
|
|
5
24
|
* Bond this as an AI provider (see `@molecule/api-ai` for the `chat()` streaming loop and the
|
|
6
25
|
* key-server-side / never-blindly-trust-model-output rules). Config: `ANTHROPIC_API_KEY`
|
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
|
|
@@ -357,9 +358,18 @@ class AnthropicAIProvider {
|
|
|
357
358
|
text: `[Video attachment (${block.mediaType}) — not supported by this provider]`,
|
|
358
359
|
};
|
|
359
360
|
case 'tool_use':
|
|
360
|
-
return {
|
|
361
|
+
return {
|
|
362
|
+
type: 'tool_use',
|
|
363
|
+
id: toAnthropicToolId(block.id),
|
|
364
|
+
name: block.name,
|
|
365
|
+
input: block.input,
|
|
366
|
+
};
|
|
361
367
|
case 'tool_result':
|
|
362
|
-
return {
|
|
368
|
+
return {
|
|
369
|
+
type: 'tool_result',
|
|
370
|
+
tool_use_id: toAnthropicToolId(block.tool_use_id),
|
|
371
|
+
content: block.content,
|
|
372
|
+
};
|
|
363
373
|
default:
|
|
364
374
|
return block;
|
|
365
375
|
}
|
|
@@ -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