@core-ai/core-ai 0.22.0 → 0.23.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
@@ -467,14 +467,22 @@ declare class StreamAbortedError extends AbortedError {
467
467
  }
468
468
  type ProviderErrorOptions = {
469
469
  statusCode?: number;
470
+ /**
471
+ * Provider-specific error code or type as reported by the API, e.g.
472
+ * `insufficient_quota`, `invalid_request_error`, `RESOURCE_EXHAUSTED`.
473
+ * Stable machine-readable identifier for logging and classification
474
+ * where the message itself cannot be recorded.
475
+ */
476
+ code?: string;
470
477
  cause?: unknown;
471
478
  };
472
479
  declare class ProviderError extends CoreAIError {
473
480
  readonly statusCode?: number;
481
+ readonly code?: string;
474
482
  /**
475
483
  * @param message Human-readable error message (may include provider text).
476
484
  * @param provider Provider id (e.g. `'openai'`, `'anthropic'`).
477
- * @param options Optional HTTP status and underlying cause.
485
+ * @param options Optional HTTP status, provider code, and underlying cause.
478
486
  */
479
487
  constructor(message: string, provider: string, options?: ProviderErrorOptions);
480
488
  }
@@ -637,9 +645,16 @@ declare function createObjectStream<TSchema extends z.ZodType>(source: AsyncIter
637
645
  signal?: AbortSignal;
638
646
  }): ObjectStream<TSchema>;
639
647
 
640
- declare function createChatStream(source: AsyncIterable<StreamEvent> | (() => Promise<AsyncIterable<StreamEvent>>), options?: {
648
+ type CreateChatStreamOptions = {
641
649
  signal?: AbortSignal;
642
- }): ChatStream;
650
+ /**
651
+ * Maps errors raised while opening or iterating the source — including
652
+ * in-band SDK failures after HTTP 200 — onto a {@link CoreAIError}.
653
+ * Already-typed core-ai errors pass through unchanged.
654
+ */
655
+ mapError?: (error: unknown) => CoreAIError;
656
+ };
657
+ declare function createChatStream(source: AsyncIterable<StreamEvent> | (() => Promise<AsyncIterable<StreamEvent>>), options?: CreateChatStreamOptions): ChatStream;
643
658
 
644
659
  declare function wrapChatModel(config: {
645
660
  model: ChatModel;
@@ -668,4 +683,4 @@ type GenerateImageParams = ImageGenerateOptions & {
668
683
  };
669
684
  declare function generateImage(params: GenerateImageParams): Promise<ImageGenerateResult>;
670
685
 
671
- export { AbortedError, type AssistantContentPart, type AssistantMessage, type AssistantTextPart, type AudioPart, type BaseGenerateOptions, type ChatInputModality, type ChatInputTokenDetails, type ChatModel, type ChatModelMiddleware, type ChatOutputModality, type ChatOutputTokenDetails, type ChatStream, type ChatUsage, ContextLengthExceededError, type ContextLengthExceededErrorOptions, CoreAIError, type EmbedOptions, type EmbedProviderOptions, type EmbedResult, type EmbeddingModel, type EmbeddingModelMiddleware, type EmbeddingUsage, type FilePart, type FinishReason, type GenerateObjectOptions, type GenerateObjectResult, type GenerateOptions, type GenerateProviderOptions, type GenerateResult, type GeneratedImage, type ImageGenerateOptions, type ImageGenerateResult, type ImageModel, type ImageModelMiddleware, type ImagePart, type ImageProviderOptions, MULTIMODAL_INPUT_MODALITIES, type Message, type ModelCapabilities, type ModelCapabilitiesRegistry, ModelOverloadedError, type ModelOverloadedErrorOptions, type ObjectStream, type ObjectStreamEvent, ProviderError, type ProviderErrorOptions, RateLimitError, type RateLimitErrorOptions, type ReasoningConfig, type ReasoningEffort, type ReasoningPart, RetryableProviderError, ServiceUnavailableError, type ServiceUnavailableErrorOptions, StreamAbortedError, type StreamEvent, type StreamObjectOptions, StructuredOutputError, StructuredOutputNoObjectGeneratedError, StructuredOutputParseError, StructuredOutputValidationError, type SystemMessage, TEXT_ONLY_MODALITIES, type TextPart, type ToolCall, type ToolCallPart, type ToolChoice, type ToolDefinition, type ToolResultMessage, type ToolSet, UNKNOWN_MODEL, UnsupportedInputModalityError, type UnsupportedInputModalityErrorOptions, type UserContentPart, type UserMessage, type ValidateInputModalitiesOptions, ValidationError, asObject, asRecord, assistantMessage, clampReasoningEffort, createChatStream, createObjectStream, defineTool, embed, generate, generateImage, generateObject, getErrorMessage, getHttpStatusCode, getProviderMetadata, getRegisteredModelCapabilities, getRetryAfterSecondsFromError, getString, isAbortErrorByName, isRateLimitStatus, isTransientUnavailableStatus, parseRetryAfterSeconds, resultToMessage, safeParseJsonObject, stream, streamObject, stripModelDateSuffix, supportsInputModality, supportsOutputModality, validateInputModalities, wrapChatModel, wrapEmbeddingModel, wrapImageModel, zodSchemaToJsonSchema };
686
+ export { AbortedError, type AssistantContentPart, type AssistantMessage, type AssistantTextPart, type AudioPart, type BaseGenerateOptions, type ChatInputModality, type ChatInputTokenDetails, type ChatModel, type ChatModelMiddleware, type ChatOutputModality, type ChatOutputTokenDetails, type ChatStream, type ChatUsage, ContextLengthExceededError, type ContextLengthExceededErrorOptions, CoreAIError, type CreateChatStreamOptions, type EmbedOptions, type EmbedProviderOptions, type EmbedResult, type EmbeddingModel, type EmbeddingModelMiddleware, type EmbeddingUsage, type FilePart, type FinishReason, type GenerateObjectOptions, type GenerateObjectResult, type GenerateOptions, type GenerateProviderOptions, type GenerateResult, type GeneratedImage, type ImageGenerateOptions, type ImageGenerateResult, type ImageModel, type ImageModelMiddleware, type ImagePart, type ImageProviderOptions, MULTIMODAL_INPUT_MODALITIES, type Message, type ModelCapabilities, type ModelCapabilitiesRegistry, ModelOverloadedError, type ModelOverloadedErrorOptions, type ObjectStream, type ObjectStreamEvent, ProviderError, type ProviderErrorOptions, RateLimitError, type RateLimitErrorOptions, type ReasoningConfig, type ReasoningEffort, type ReasoningPart, RetryableProviderError, ServiceUnavailableError, type ServiceUnavailableErrorOptions, StreamAbortedError, type StreamEvent, type StreamObjectOptions, StructuredOutputError, StructuredOutputNoObjectGeneratedError, StructuredOutputParseError, StructuredOutputValidationError, type SystemMessage, TEXT_ONLY_MODALITIES, type TextPart, type ToolCall, type ToolCallPart, type ToolChoice, type ToolDefinition, type ToolResultMessage, type ToolSet, UNKNOWN_MODEL, UnsupportedInputModalityError, type UnsupportedInputModalityErrorOptions, type UserContentPart, type UserMessage, type ValidateInputModalitiesOptions, ValidationError, asObject, asRecord, assistantMessage, clampReasoningEffort, createChatStream, createObjectStream, defineTool, embed, generate, generateImage, generateObject, getErrorMessage, getHttpStatusCode, getProviderMetadata, getRegisteredModelCapabilities, getRetryAfterSecondsFromError, getString, isAbortErrorByName, isRateLimitStatus, isTransientUnavailableStatus, parseRetryAfterSeconds, resultToMessage, safeParseJsonObject, stream, streamObject, stripModelDateSuffix, supportsInputModality, supportsOutputModality, validateInputModalities, wrapChatModel, wrapEmbeddingModel, wrapImageModel, zodSchemaToJsonSchema };
package/dist/index.js CHANGED
@@ -49,15 +49,17 @@ var StreamAbortedError = class extends AbortedError {
49
49
  };
50
50
  var ProviderError = class extends CoreAIError {
51
51
  statusCode;
52
+ code;
52
53
  /**
53
54
  * @param message Human-readable error message (may include provider text).
54
55
  * @param provider Provider id (e.g. `'openai'`, `'anthropic'`).
55
- * @param options Optional HTTP status and underlying cause.
56
+ * @param options Optional HTTP status, provider code, and underlying cause.
56
57
  */
57
58
  constructor(message, provider, options = {}) {
58
59
  super(message, options.cause, provider);
59
60
  this.name = "ProviderError";
60
61
  this.statusCode = options.statusCode;
62
+ this.code = options.code;
61
63
  }
62
64
  };
63
65
  var RetryableProviderError = class extends ProviderError {
@@ -72,6 +74,7 @@ var ContextLengthExceededError = class extends ProviderError {
72
74
  constructor(message, provider, options = {}) {
73
75
  super(message, provider, {
74
76
  statusCode: options.statusCode,
77
+ code: options.code,
75
78
  cause: options.cause
76
79
  });
77
80
  this.name = "ContextLengthExceededError";
@@ -84,6 +87,7 @@ var RateLimitError = class extends RetryableProviderError {
84
87
  constructor(message, provider, options = {}) {
85
88
  super(message, provider, {
86
89
  statusCode: options.statusCode ?? 429,
90
+ code: options.code,
87
91
  cause: options.cause
88
92
  });
89
93
  this.name = "RateLimitError";
@@ -648,12 +652,30 @@ function createObjectStream(source, options = {}) {
648
652
  });
649
653
  }
650
654
 
655
+ // src/map-stream-errors.ts
656
+ async function* mapStreamErrors(source, mapError) {
657
+ try {
658
+ for await (const event of source) {
659
+ yield event;
660
+ }
661
+ } catch (error) {
662
+ if (error instanceof CoreAIError || isLanguageError(error)) {
663
+ throw error;
664
+ }
665
+ throw mapError(error);
666
+ }
667
+ }
668
+ function isLanguageError(error) {
669
+ return error instanceof TypeError || error instanceof RangeError || error instanceof SyntaxError || error instanceof ReferenceError;
670
+ }
671
+
651
672
  // src/stream.ts
652
673
  function createChatStream(source, options = {}) {
653
- const { signal } = options;
674
+ const { signal, mapError } = options;
654
675
  const resolvedSource = typeof source === "function" ? (async function* () {
655
676
  yield* await source();
656
677
  })() : source;
678
+ const sourceWithMappedErrors = mapError ? mapStreamErrors(resolvedSource, mapError) : resolvedSource;
657
679
  const parts = [];
658
680
  let textBuffer = "";
659
681
  let textMetadata;
@@ -784,7 +806,7 @@ function createChatStream(source, options = {}) {
784
806
  };
785
807
  };
786
808
  return createStream({
787
- source: resolvedSource,
809
+ source: sourceWithMappedErrors,
788
810
  signal,
789
811
  reduceEvent(event) {
790
812
  switch (event.type) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@core-ai/core-ai",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "description": "Type-safe LLM abstraction layer over native provider SDKs",
5
5
  "license": "MIT",
6
6
  "author": "Omnifact (https://omnifact.ai)",