@core-ai/anthropic 0.13.1 → 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
  }
@@ -681,6 +678,12 @@ async function* transformStream(stream) {
681
678
  };
682
679
  continue;
683
680
  }
681
+ if (event.content_block.type === "text") {
682
+ yield {
683
+ type: "text-start"
684
+ };
685
+ continue;
686
+ }
684
687
  if (event.content_block.type === "tool_use") {
685
688
  const block = event.content_block;
686
689
  const initialArguments = block.input && typeof block.input === "object" ? Object.keys(block.input).length > 0 ? JSON.stringify(block.input) : "" : "";
@@ -738,6 +741,13 @@ async function* transformStream(stream) {
738
741
  continue;
739
742
  }
740
743
  if (event.type === "content_block_stop") {
744
+ if (contentBlockTypeByIndex.get(event.index) === "text") {
745
+ contentBlockTypeByIndex.delete(event.index);
746
+ yield {
747
+ type: "text-end"
748
+ };
749
+ continue;
750
+ }
741
751
  if (contentBlockTypeByIndex.get(event.index) === "thinking") {
742
752
  const signature = reasoningSignatureByIndex.get(event.index);
743
753
  reasoningSignatureByIndex.delete(event.index);
@@ -830,29 +840,33 @@ function extractThinkingText(value) {
830
840
  return typeof text === "string" ? [text] : [];
831
841
  }).join("");
832
842
  }
833
- function wrapError(error) {
843
+ function wrapError(error, provider = DEFAULT_PROVIDER_ID) {
834
844
  if (error instanceof APIUserAbortError || error instanceof Error && error.name === "AbortError") {
835
- return new AbortedError(error, "anthropic");
845
+ return new AbortedError(error, provider);
836
846
  }
837
847
  if (error instanceof APIError) {
838
- return new ProviderError(
839
- error.message,
840
- "anthropic",
841
- error.status,
842
- error
843
- );
848
+ return new ProviderError(error.message, provider, error.status, error);
844
849
  }
845
850
  return new ProviderError(
846
851
  error instanceof Error ? error.message : String(error),
847
- "anthropic",
852
+ provider,
848
853
  void 0,
849
854
  error
850
855
  );
851
856
  }
852
857
 
853
858
  // src/chat-model.ts
854
- function createAnthropicChatModel(client, modelId, defaultMaxTokens) {
855
- 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;
856
870
  async function callAnthropicMessagesApi(request, betas, signal) {
857
871
  try {
858
872
  return await client.messages.create(request, {
@@ -864,20 +878,28 @@ function createAnthropicChatModel(client, modelId, defaultMaxTokens) {
864
878
  signal
865
879
  });
866
880
  } catch (error) {
867
- throw wrapError(error);
881
+ throw wrapError(error, provider);
868
882
  }
869
883
  }
870
884
  async function generateChat(options) {
871
885
  const request = createGenerateRequest(
872
886
  modelId,
873
887
  defaultMaxTokens,
874
- options
888
+ options,
889
+ provider,
890
+ useStrictToolSchemas
875
891
  );
876
892
  const response = await callAnthropicMessagesApi(request, getAnthropicRequestBetas(modelId, options), options.signal);
877
893
  return mapGenerateResponse(response);
878
894
  }
879
895
  async function streamChat(options) {
880
- const request = createStreamRequest(modelId, defaultMaxTokens, options);
896
+ const request = createStreamRequest(
897
+ modelId,
898
+ defaultMaxTokens,
899
+ options,
900
+ provider,
901
+ useStrictToolSchemas
902
+ );
881
903
  return createChatStream(
882
904
  async () => transformStream(
883
905
  await callAnthropicMessagesApi(
@@ -1044,20 +1066,30 @@ function formatZodIssues(issues) {
1044
1066
  }
1045
1067
 
1046
1068
  // src/provider.ts
1047
- function createAnthropic(options = {}) {
1069
+ function createAnthropicChatProvider(options = {}, factoryOptions = {}) {
1048
1070
  const client = options.client ?? new Anthropic({
1049
1071
  apiKey: options.apiKey,
1050
1072
  baseURL: options.baseURL
1051
1073
  });
1052
1074
  const defaultMaxTokens = options.defaultMaxTokens ?? 4096;
1075
+ const providerId = factoryOptions.providerId ?? DEFAULT_PROVIDER_ID;
1076
+ const useStrictToolSchemas = factoryOptions.useStrictToolSchemas ?? true;
1053
1077
  return {
1054
- chatModel: (modelId) => createAnthropicChatModel(client, modelId, defaultMaxTokens)
1078
+ chatModel: (modelId) => createAnthropicChatModel(client, modelId, {
1079
+ defaultMaxTokens,
1080
+ providerId,
1081
+ useStrictToolSchemas
1082
+ })
1055
1083
  };
1056
1084
  }
1085
+ function createAnthropic(options = {}) {
1086
+ return createAnthropicChatProvider(options);
1087
+ }
1057
1088
  export {
1058
1089
  anthropicCacheControlSchema,
1059
1090
  anthropicGenerateProviderOptionsSchema,
1060
1091
  anthropicProviderOptionsSchema,
1061
1092
  createAnthropic,
1093
+ createAnthropicChatProvider,
1062
1094
  getAnthropicModelCapabilities
1063
1095
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@core-ai/anthropic",
3
- "version": "0.13.1",
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.13.1"
48
+ "@core-ai/core-ai": "^0.15.0"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "zod": "^4.0.0"