@core-ai/anthropic 0.7.0 → 0.8.0

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/index.d.ts CHANGED
@@ -18,11 +18,26 @@ type AnthropicReasoningMetadata = {
18
18
  redactedData?: string;
19
19
  };
20
20
 
21
+ declare const anthropicCacheControlSchema: z.ZodObject<{
22
+ type: z.ZodLiteral<"ephemeral">;
23
+ ttl: z.ZodOptional<z.ZodEnum<{
24
+ "5m": "5m";
25
+ "1h": "1h";
26
+ }>>;
27
+ }, z.core.$strict>;
28
+ type AnthropicCacheControl = z.infer<typeof anthropicCacheControlSchema>;
21
29
  declare const anthropicGenerateProviderOptionsSchema: z.ZodObject<{
22
30
  topK: z.ZodOptional<z.ZodNumber>;
23
31
  stopSequences: z.ZodOptional<z.ZodArray<z.ZodString>>;
24
32
  betas: z.ZodOptional<z.ZodArray<z.ZodString>>;
25
33
  outputConfig: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
34
+ cacheControl: z.ZodOptional<z.ZodObject<{
35
+ type: z.ZodLiteral<"ephemeral">;
36
+ ttl: z.ZodOptional<z.ZodEnum<{
37
+ "5m": "5m";
38
+ "1h": "1h";
39
+ }>>;
40
+ }, z.core.$strict>>;
26
41
  }, z.core.$strict>;
27
42
  type AnthropicGenerateProviderOptions = z.infer<typeof anthropicGenerateProviderOptionsSchema>;
28
43
  declare module '@core-ai/core-ai' {
@@ -35,7 +50,14 @@ declare const anthropicProviderOptionsSchema: z.ZodObject<{
35
50
  stopSequences: z.ZodOptional<z.ZodArray<z.ZodString>>;
36
51
  betas: z.ZodOptional<z.ZodArray<z.ZodString>>;
37
52
  outputConfig: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
53
+ cacheControl: z.ZodOptional<z.ZodObject<{
54
+ type: z.ZodLiteral<"ephemeral">;
55
+ ttl: z.ZodOptional<z.ZodEnum<{
56
+ "5m": "5m";
57
+ "1h": "1h";
58
+ }>>;
59
+ }, z.core.$strict>>;
38
60
  }, z.core.$strict>;
39
61
  type AnthropicProviderOptions = AnthropicGenerateProviderOptions;
40
62
 
41
- export { type AnthropicGenerateProviderOptions, type AnthropicProviderOptions as AnthropicModelProviderOptions, type AnthropicProvider, type AnthropicProviderOptions$1 as AnthropicProviderOptions, type AnthropicReasoningMetadata, anthropicGenerateProviderOptionsSchema, anthropicProviderOptionsSchema, createAnthropic };
63
+ export { type AnthropicCacheControl, type AnthropicGenerateProviderOptions, type AnthropicProviderOptions as AnthropicModelProviderOptions, type AnthropicProvider, type AnthropicProviderOptions$1 as AnthropicProviderOptions, type AnthropicReasoningMetadata, anthropicCacheControlSchema, anthropicGenerateProviderOptionsSchema, anthropicProviderOptionsSchema, createAnthropic };
package/dist/index.js CHANGED
@@ -11,10 +11,21 @@ import {
11
11
  } from "@core-ai/core-ai";
12
12
 
13
13
  // src/chat-adapter.ts
14
- import { APIError } from "@anthropic-ai/sdk";
15
- import { ProviderError, getProviderMetadata, zodSchemaToJsonSchema } from "@core-ai/core-ai";
14
+ import { APIError, APIUserAbortError } from "@anthropic-ai/sdk";
15
+ import {
16
+ AbortedError,
17
+ asObject,
18
+ getProviderMetadata,
19
+ ProviderError,
20
+ ValidationError,
21
+ safeParseJsonObject,
22
+ zodSchemaToJsonSchema
23
+ } from "@core-ai/core-ai";
16
24
 
17
25
  // src/model-capabilities.ts
26
+ import {
27
+ stripModelDateSuffix
28
+ } from "@core-ai/core-ai";
18
29
  var DEFAULT_CAPABILITIES = {
19
30
  reasoning: {
20
31
  thinkingMode: "adaptive",
@@ -77,45 +88,48 @@ var MODEL_CAPABILITIES = {
77
88
  }
78
89
  }
79
90
  };
91
+ var ANTHROPIC_ADAPTIVE_EFFORT_MAP = {
92
+ minimal: "low",
93
+ low: "low",
94
+ medium: "medium",
95
+ high: "high"
96
+ };
97
+ var ANTHROPIC_MANUAL_BUDGET_MAP = {
98
+ minimal: 1024,
99
+ low: 2048,
100
+ medium: 8192,
101
+ high: 32768,
102
+ max: 65536
103
+ };
80
104
  function getAnthropicModelCapabilities(modelId) {
81
105
  const normalizedModelId = normalizeModelId(modelId);
82
106
  return MODEL_CAPABILITIES[normalizedModelId] ?? DEFAULT_CAPABILITIES;
83
107
  }
84
108
  function normalizeModelId(modelId) {
85
- return modelId.replace(/-\d{8}$/, "");
109
+ return stripModelDateSuffix(modelId);
86
110
  }
87
111
  function toAnthropicAdaptiveEffort(effort, supportsMaxEffort) {
88
- if (effort === "minimal") {
89
- return "low";
90
- }
91
112
  if (effort === "max") {
92
113
  return supportsMaxEffort ? "max" : "high";
93
114
  }
94
- return effort;
115
+ return ANTHROPIC_ADAPTIVE_EFFORT_MAP[effort];
95
116
  }
96
117
  function toAnthropicManualBudget(effort) {
97
- if (effort === "minimal") {
98
- return 1024;
99
- }
100
- if (effort === "low") {
101
- return 2048;
102
- }
103
- if (effort === "medium") {
104
- return 8192;
105
- }
106
- if (effort === "high") {
107
- return 32768;
108
- }
109
- return 65536;
118
+ return ANTHROPIC_MANUAL_BUDGET_MAP[effort];
110
119
  }
111
120
 
112
121
  // src/provider-options.ts
113
122
  import { z } from "zod";
123
+ var anthropicCacheControlSchema = z.object({
124
+ type: z.literal("ephemeral"),
125
+ ttl: z.enum(["5m", "1h"]).optional()
126
+ }).strict();
114
127
  var anthropicGenerateProviderOptionsSchema = z.object({
115
128
  topK: z.number().int().optional(),
116
129
  stopSequences: z.array(z.string()).optional(),
117
130
  betas: z.array(z.string()).optional(),
118
- outputConfig: z.record(z.string(), z.unknown()).optional()
131
+ outputConfig: z.record(z.string(), z.unknown()).optional(),
132
+ cacheControl: anthropicCacheControlSchema.optional()
119
133
  }).strict();
120
134
  function parseAnthropicGenerateProviderOptions(providerOptions) {
121
135
  const rawOptions = providerOptions?.anthropic;
@@ -417,26 +431,30 @@ function validateAnthropicReasoningConfig(modelId, options, anthropicOptions) {
417
431
  return;
418
432
  }
419
433
  if (options.temperature !== void 0) {
420
- throw new ProviderError(
434
+ throw new ValidationError(
421
435
  `Anthropic model "${modelId}" does not support temperature when reasoning is enabled`,
436
+ void 0,
422
437
  "anthropic"
423
438
  );
424
439
  }
425
440
  if (options.topP !== void 0 && (options.topP < 0.95 || options.topP > 1)) {
426
- throw new ProviderError(
441
+ throw new ValidationError(
427
442
  `Anthropic model "${modelId}" requires topP between 0.95 and 1 when reasoning is enabled`,
443
+ void 0,
428
444
  "anthropic"
429
445
  );
430
446
  }
431
447
  if (options.toolChoice && options.toolChoice !== "auto" && options.toolChoice !== "none") {
432
- throw new ProviderError(
448
+ throw new ValidationError(
433
449
  `Anthropic model "${modelId}" only supports toolChoice "auto" or "none" when reasoning is enabled`,
450
+ void 0,
434
451
  "anthropic"
435
452
  );
436
453
  }
437
454
  if (anthropicOptions?.topK !== void 0) {
438
- throw new ProviderError(
455
+ throw new ValidationError(
439
456
  `Anthropic model "${modelId}" does not support top_k when reasoning is enabled`,
457
+ void 0,
440
458
  "anthropic"
441
459
  );
442
460
  }
@@ -483,6 +501,7 @@ function mapAnthropicProviderOptionsToRequest(baseRequest, providerOptions) {
483
501
  ];
484
502
  const mergedRequest = {
485
503
  ...baseRequest,
504
+ ...providerOptions.cacheControl ? { cache_control: providerOptions.cacheControl } : {},
486
505
  ...providerOptions.topK !== void 0 ? { top_k: providerOptions.topK } : {},
487
506
  ...providerOptions.stopSequences ? { stop_sequences: providerOptions.stopSequences } : {},
488
507
  ...Object.keys(mergedOutputConfig).length > 0 ? { output_config: mergedOutputConfig } : {},
@@ -721,20 +740,6 @@ function mapStopReason(reason) {
721
740
  }
722
741
  return "unknown";
723
742
  }
724
- function safeParseJsonObject(json) {
725
- try {
726
- const parsed = JSON.parse(json);
727
- return asObject(parsed);
728
- } catch {
729
- return {};
730
- }
731
- }
732
- function asObject(value) {
733
- if (value && typeof value === "object" && !Array.isArray(value)) {
734
- return value;
735
- }
736
- return {};
737
- }
738
743
  function asStringArray(value) {
739
744
  if (!Array.isArray(value)) {
740
745
  return [];
@@ -760,6 +765,9 @@ function extractThinkingText(value) {
760
765
  }).join("");
761
766
  }
762
767
  function wrapError(error) {
768
+ if (error instanceof APIUserAbortError || error instanceof Error && error.name === "AbortError") {
769
+ return new AbortedError(error, "anthropic");
770
+ }
763
771
  if (error instanceof APIError) {
764
772
  return new ProviderError(
765
773
  error.message,
@@ -971,6 +979,7 @@ function createAnthropic(options = {}) {
971
979
  };
972
980
  }
973
981
  export {
982
+ anthropicCacheControlSchema,
974
983
  anthropicGenerateProviderOptionsSchema,
975
984
  anthropicProviderOptionsSchema,
976
985
  createAnthropic
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@core-ai/anthropic",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Anthropic provider package for @core-ai/core-ai",
5
5
  "license": "MIT",
6
6
  "author": "Omnifact (https://omnifact.ai)",
@@ -42,7 +42,7 @@
42
42
  "test:watch": "vitest"
43
43
  },
44
44
  "dependencies": {
45
- "@core-ai/core-ai": "^0.7.0",
45
+ "@core-ai/core-ai": "^0.8.0",
46
46
  "@anthropic-ai/sdk": "^0.78.0"
47
47
  },
48
48
  "peerDependencies": {