@core-ai/anthropic 0.14.0 → 0.15.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
@@ -1,16 +1,43 @@
1
- import Anthropic from '@anthropic-ai/sdk';
2
1
  import { ChatModel, ModelCapabilities } from '@core-ai/core-ai';
2
+ import Anthropic from '@anthropic-ai/sdk';
3
3
  import { z } from 'zod';
4
4
 
5
- type AnthropicProviderOptions$1 = {
5
+ type AnthropicChatClient = {
6
+ messages: {
7
+ create: Anthropic['messages']['create'];
8
+ };
9
+ };
10
+
11
+ type AnthropicChatProviderOptions = {
6
12
  apiKey?: string;
7
13
  baseURL?: string;
8
- client?: Anthropic;
14
+ client?: AnthropicChatClient;
9
15
  defaultMaxTokens?: number;
10
16
  };
11
- type AnthropicProvider = {
17
+ /**
18
+ * Factory options for sibling packages that compose Anthropic chat.
19
+ * Not part of {@link createAnthropic} / {@link AnthropicProviderOptions} —
20
+ * first-party Anthropic always uses strict tool schemas.
21
+ */
22
+ type AnthropicChatProviderFactoryOptions = {
23
+ providerId?: string;
24
+ useStrictToolSchemas?: boolean;
25
+ };
26
+ type AnthropicChatProvider = {
12
27
  chatModel(modelId: string): ChatModel;
13
28
  };
29
+ type AnthropicProviderOptions$1 = AnthropicChatProviderOptions;
30
+ type AnthropicProvider = AnthropicChatProvider;
31
+ /**
32
+ * Creates a chat-only Anthropic provider. Handles client construction
33
+ * internally so consumers do not need a direct dependency on the
34
+ * `@anthropic-ai/sdk` package, and attributes generation, streaming, and
35
+ * error handling to the given provider id.
36
+ *
37
+ * Sibling packages (e.g. `@core-ai/anthropic-vertex`) pass a custom client
38
+ * and factory options; end users should prefer {@link createAnthropic}.
39
+ */
40
+ declare function createAnthropicChatProvider(options?: AnthropicChatProviderOptions, factoryOptions?: AnthropicChatProviderFactoryOptions): AnthropicChatProvider;
14
41
  declare function createAnthropic(options?: AnthropicProviderOptions$1): AnthropicProvider;
15
42
 
16
43
  type AnthropicReasoningMetadata = {
@@ -63,4 +90,4 @@ declare const anthropicProviderOptionsSchema: z.ZodObject<{
63
90
  }, z.core.$strict>;
64
91
  type AnthropicProviderOptions = AnthropicGenerateProviderOptions;
65
92
 
66
- export { type AnthropicCacheControl, type AnthropicGenerateProviderOptions, type AnthropicModelCapabilities, type AnthropicProviderOptions as AnthropicModelProviderOptions, type AnthropicProvider, type AnthropicProviderOptions$1 as AnthropicProviderOptions, type AnthropicReasoningMetadata, anthropicCacheControlSchema, anthropicGenerateProviderOptionsSchema, anthropicProviderOptionsSchema, createAnthropic, getAnthropicModelCapabilities };
93
+ export { type AnthropicCacheControl, type AnthropicChatClient, type AnthropicChatProvider, type AnthropicChatProviderFactoryOptions, type AnthropicChatProviderOptions, type AnthropicGenerateProviderOptions, type AnthropicModelCapabilities, type AnthropicProviderOptions as AnthropicModelProviderOptions, type AnthropicProvider, type AnthropicProviderOptions$1 as AnthropicProviderOptions, type AnthropicReasoningMetadata, anthropicCacheControlSchema, anthropicGenerateProviderOptionsSchema, anthropicProviderOptionsSchema, createAnthropic, createAnthropicChatProvider, getAnthropicModelCapabilities };
package/dist/index.js CHANGED
@@ -1,15 +1,6 @@
1
1
  // src/provider.ts
2
2
  import Anthropic from "@anthropic-ai/sdk";
3
3
 
4
- // src/chat-model.ts
5
- import {
6
- StructuredOutputNoObjectGeneratedError,
7
- StructuredOutputParseError,
8
- StructuredOutputValidationError,
9
- createObjectStream,
10
- createChatStream
11
- } from "@core-ai/core-ai";
12
-
13
4
  // src/chat-adapter.ts
14
5
  import { APIError, APIUserAbortError } from "@anthropic-ai/sdk";
15
6
  import {
@@ -153,6 +144,7 @@ function parseAnthropicGenerateProviderOptions(providerOptions) {
153
144
  var anthropicProviderOptionsSchema = anthropicGenerateProviderOptionsSchema;
154
145
 
155
146
  // src/chat-adapter.ts
147
+ var DEFAULT_PROVIDER_ID = "anthropic";
156
148
  var UNSUPPORTED_ANTHROPIC_SCHEMA_KEYWORDS = /* @__PURE__ */ new Set([
157
149
  "minimum",
158
150
  "maximum",
@@ -304,14 +296,14 @@ function convertUserContentPart(part) {
304
296
  }
305
297
  };
306
298
  }
307
- function convertTools(tools) {
299
+ function convertTools(tools, useStrictToolSchemas = true) {
308
300
  return Object.values(tools).map((tool) => {
309
301
  const schema = toAnthropicJsonSchema(tool.parameters);
310
302
  return {
311
303
  name: tool.name,
312
304
  description: tool.description,
313
305
  input_schema: schema,
314
- strict: true
306
+ ...useStrictToolSchemas ? { strict: true } : {}
315
307
  };
316
308
  });
317
309
  }
@@ -389,7 +381,7 @@ function isObjectSchema(value) {
389
381
  function isJsonObject(value) {
390
382
  return value !== null && typeof value === "object" && !Array.isArray(value);
391
383
  }
392
- function createGenerateRequest(modelId, defaultMaxTokens, options) {
384
+ function createGenerateRequest(modelId, defaultMaxTokens, options, provider = DEFAULT_PROVIDER_ID, useStrictToolSchemas = true) {
393
385
  const anthropicOptions = parseAnthropicGenerateProviderOptions(
394
386
  options.providerOptions
395
387
  );
@@ -397,11 +389,13 @@ function createGenerateRequest(modelId, defaultMaxTokens, options) {
397
389
  modelId,
398
390
  defaultMaxTokens,
399
391
  options,
400
- anthropicOptions
392
+ anthropicOptions,
393
+ provider,
394
+ useStrictToolSchemas
401
395
  );
402
396
  return mapAnthropicProviderOptionsToRequest(baseRequest, anthropicOptions);
403
397
  }
404
- function createStreamRequest(modelId, defaultMaxTokens, options) {
398
+ function createStreamRequest(modelId, defaultMaxTokens, options, provider = DEFAULT_PROVIDER_ID, useStrictToolSchemas = true) {
405
399
  const anthropicOptions = parseAnthropicGenerateProviderOptions(
406
400
  options.providerOptions
407
401
  );
@@ -410,19 +404,22 @@ function createStreamRequest(modelId, defaultMaxTokens, options) {
410
404
  modelId,
411
405
  defaultMaxTokens,
412
406
  options,
413
- anthropicOptions
407
+ anthropicOptions,
408
+ provider,
409
+ useStrictToolSchemas
414
410
  ),
415
411
  stream: true
416
412
  };
417
413
  return mapAnthropicProviderOptionsToRequest(baseRequest, anthropicOptions);
418
414
  }
419
- function createRequestBase(modelId, defaultMaxTokens, options, anthropicOptions) {
415
+ function createRequestBase(modelId, defaultMaxTokens, options, anthropicOptions, provider, useStrictToolSchemas) {
420
416
  const maxTokens = options.maxTokens ?? defaultMaxTokens;
421
417
  validateAnthropicReasoningConfig(
422
418
  modelId,
423
419
  maxTokens,
424
420
  options,
425
- anthropicOptions
421
+ anthropicOptions,
422
+ provider
426
423
  );
427
424
  const converted = convertMessages(options.messages);
428
425
  const reasoningFields = mapReasoningToRequestFields(
@@ -435,7 +432,7 @@ function createRequestBase(modelId, defaultMaxTokens, options, anthropicOptions)
435
432
  messages: converted.messages,
436
433
  max_tokens: maxTokens,
437
434
  ...converted.system ? { system: converted.system } : {},
438
- ...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
435
+ ...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools, useStrictToolSchemas) } : {},
439
436
  ...options.toolChoice ? { tool_choice: convertToolChoice(options.toolChoice) } : {},
440
437
  ...reasoningFields,
441
438
  ...mapSamplingToRequestFields(options)
@@ -447,27 +444,27 @@ function mapSamplingToRequestFields(options) {
447
444
  ...options.topP !== void 0 ? { top_p: options.topP } : {}
448
445
  };
449
446
  }
450
- function validateAnthropicReasoningConfig(modelId, maxTokens, options, anthropicOptions) {
447
+ function validateAnthropicReasoningConfig(modelId, maxTokens, options, anthropicOptions, provider) {
451
448
  const alwaysRestrictsSampling = restrictsAnthropicSamplingParamsAlways(modelId);
452
449
  if (alwaysRestrictsSampling && options.temperature !== void 0 && options.temperature !== 1) {
453
450
  throw new ValidationError(
454
451
  `Anthropic model "${modelId}" only supports the default temperature of 1`,
455
452
  void 0,
456
- "anthropic"
453
+ provider
457
454
  );
458
455
  }
459
456
  if (alwaysRestrictsSampling && options.topP !== void 0 && options.topP !== 1) {
460
457
  throw new ValidationError(
461
458
  `Anthropic model "${modelId}" only supports the default topP of 1`,
462
459
  void 0,
463
- "anthropic"
460
+ provider
464
461
  );
465
462
  }
466
463
  if (alwaysRestrictsSampling && anthropicOptions?.topK !== void 0) {
467
464
  throw new ValidationError(
468
465
  `Anthropic model "${modelId}" does not support top_k`,
469
466
  void 0,
470
- "anthropic"
467
+ provider
471
468
  );
472
469
  }
473
470
  if (!options.reasoning) {
@@ -477,35 +474,35 @@ function validateAnthropicReasoningConfig(modelId, maxTokens, options, anthropic
477
474
  throw new ValidationError(
478
475
  `Anthropic model "${modelId}" requires maxTokens greater than 1024 when reasoning is enabled`,
479
476
  void 0,
480
- "anthropic"
477
+ provider
481
478
  );
482
479
  }
483
480
  if (options.temperature !== void 0 && (!alwaysRestrictsSampling || options.temperature !== 1)) {
484
481
  throw new ValidationError(
485
482
  `Anthropic model "${modelId}" does not support temperature when reasoning is enabled`,
486
483
  void 0,
487
- "anthropic"
484
+ provider
488
485
  );
489
486
  }
490
487
  if (options.topP !== void 0 && (options.topP < 0.95 || options.topP > 1)) {
491
488
  throw new ValidationError(
492
489
  `Anthropic model "${modelId}" requires topP between 0.95 and 1 when reasoning is enabled`,
493
490
  void 0,
494
- "anthropic"
491
+ provider
495
492
  );
496
493
  }
497
494
  if (options.toolChoice && options.toolChoice !== "auto" && options.toolChoice !== "none") {
498
495
  throw new ValidationError(
499
496
  `Anthropic model "${modelId}" only supports toolChoice "auto" or "none" when reasoning is enabled`,
500
497
  void 0,
501
- "anthropic"
498
+ provider
502
499
  );
503
500
  }
504
501
  if (anthropicOptions?.topK !== void 0) {
505
502
  throw new ValidationError(
506
503
  `Anthropic model "${modelId}" does not support top_k when reasoning is enabled`,
507
504
  void 0,
508
- "anthropic"
505
+ provider
509
506
  );
510
507
  }
511
508
  }
@@ -843,29 +840,33 @@ function extractThinkingText(value) {
843
840
  return typeof text === "string" ? [text] : [];
844
841
  }).join("");
845
842
  }
846
- function wrapError(error) {
843
+ function wrapError(error, provider = DEFAULT_PROVIDER_ID) {
847
844
  if (error instanceof APIUserAbortError || error instanceof Error && error.name === "AbortError") {
848
- return new AbortedError(error, "anthropic");
845
+ return new AbortedError(error, provider);
849
846
  }
850
847
  if (error instanceof APIError) {
851
- return new ProviderError(
852
- error.message,
853
- "anthropic",
854
- error.status,
855
- error
856
- );
848
+ return new ProviderError(error.message, provider, error.status, error);
857
849
  }
858
850
  return new ProviderError(
859
851
  error instanceof Error ? error.message : String(error),
860
- "anthropic",
852
+ provider,
861
853
  void 0,
862
854
  error
863
855
  );
864
856
  }
865
857
 
866
858
  // src/chat-model.ts
867
- function createAnthropicChatModel(client, modelId, defaultMaxTokens) {
868
- const provider = "anthropic";
859
+ import {
860
+ StructuredOutputNoObjectGeneratedError,
861
+ StructuredOutputParseError,
862
+ StructuredOutputValidationError,
863
+ createObjectStream,
864
+ createChatStream
865
+ } from "@core-ai/core-ai";
866
+ function createAnthropicChatModel(client, modelId, modelOptions = {}) {
867
+ const defaultMaxTokens = modelOptions.defaultMaxTokens ?? 4096;
868
+ const provider = modelOptions.providerId ?? DEFAULT_PROVIDER_ID;
869
+ const useStrictToolSchemas = modelOptions.useStrictToolSchemas ?? true;
869
870
  async function callAnthropicMessagesApi(request, betas, signal) {
870
871
  try {
871
872
  return await client.messages.create(request, {
@@ -877,20 +878,28 @@ function createAnthropicChatModel(client, modelId, defaultMaxTokens) {
877
878
  signal
878
879
  });
879
880
  } catch (error) {
880
- throw wrapError(error);
881
+ throw wrapError(error, provider);
881
882
  }
882
883
  }
883
884
  async function generateChat(options) {
884
885
  const request = createGenerateRequest(
885
886
  modelId,
886
887
  defaultMaxTokens,
887
- options
888
+ options,
889
+ provider,
890
+ useStrictToolSchemas
888
891
  );
889
892
  const response = await callAnthropicMessagesApi(request, getAnthropicRequestBetas(modelId, options), options.signal);
890
893
  return mapGenerateResponse(response);
891
894
  }
892
895
  async function streamChat(options) {
893
- const request = createStreamRequest(modelId, defaultMaxTokens, options);
896
+ const request = createStreamRequest(
897
+ modelId,
898
+ defaultMaxTokens,
899
+ options,
900
+ provider,
901
+ useStrictToolSchemas
902
+ );
894
903
  return createChatStream(
895
904
  async () => transformStream(
896
905
  await callAnthropicMessagesApi(
@@ -1057,20 +1066,30 @@ function formatZodIssues(issues) {
1057
1066
  }
1058
1067
 
1059
1068
  // src/provider.ts
1060
- function createAnthropic(options = {}) {
1069
+ function createAnthropicChatProvider(options = {}, factoryOptions = {}) {
1061
1070
  const client = options.client ?? new Anthropic({
1062
1071
  apiKey: options.apiKey,
1063
1072
  baseURL: options.baseURL
1064
1073
  });
1065
1074
  const defaultMaxTokens = options.defaultMaxTokens ?? 4096;
1075
+ const providerId = factoryOptions.providerId ?? DEFAULT_PROVIDER_ID;
1076
+ const useStrictToolSchemas = factoryOptions.useStrictToolSchemas ?? true;
1066
1077
  return {
1067
- chatModel: (modelId) => createAnthropicChatModel(client, modelId, defaultMaxTokens)
1078
+ chatModel: (modelId) => createAnthropicChatModel(client, modelId, {
1079
+ defaultMaxTokens,
1080
+ providerId,
1081
+ useStrictToolSchemas
1082
+ })
1068
1083
  };
1069
1084
  }
1085
+ function createAnthropic(options = {}) {
1086
+ return createAnthropicChatProvider(options);
1087
+ }
1070
1088
  export {
1071
1089
  anthropicCacheControlSchema,
1072
1090
  anthropicGenerateProviderOptionsSchema,
1073
1091
  anthropicProviderOptionsSchema,
1074
1092
  createAnthropic,
1093
+ createAnthropicChatProvider,
1075
1094
  getAnthropicModelCapabilities
1076
1095
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@core-ai/anthropic",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Anthropic provider package for @core-ai/core-ai",
5
5
  "license": "MIT",
6
6
  "author": "Omnifact (https://omnifact.ai)",
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@anthropic-ai/sdk": "^0.110.0",
48
- "@core-ai/core-ai": "^0.14.0"
48
+ "@core-ai/core-ai": "^0.15.0"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "zod": "^4.0.0"