@molecule/api-ai-anthropic 1.3.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.
@@ -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;AAErB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAosBjD;;;;;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;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 { type: 'tool_use', id: block.id, name: block.name, input: block.input };
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 { type: 'tool_result', tool_use_id: block.tool_use_id, content: block.content };
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"}
@@ -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.0",
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",