@core-ai/core-ai 0.21.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
@@ -34,12 +34,22 @@ type ImagePart = {
34
34
  type: 'url';
35
35
  url: string;
36
36
  };
37
+ /**
38
+ * Application-owned metadata for this part. Provider adapters ignore this
39
+ * field and never serialize it to provider APIs.
40
+ */
41
+ metadata?: Record<string, unknown>;
37
42
  };
38
43
  type FilePart = {
39
44
  type: 'file';
40
45
  data: string;
41
46
  mimeType: string;
42
47
  filename?: string;
48
+ /**
49
+ * Application-owned metadata for this part. Provider adapters ignore this
50
+ * field and never serialize it to provider APIs.
51
+ */
52
+ metadata?: Record<string, unknown>;
43
53
  };
44
54
  type AudioPart = {
45
55
  type: 'audio';
@@ -48,6 +58,11 @@ type AudioPart = {
48
58
  mediaType: string;
49
59
  data: string;
50
60
  };
61
+ /**
62
+ * Application-owned metadata for this part. Provider adapters ignore this
63
+ * field and never serialize it to provider APIs.
64
+ */
65
+ metadata?: Record<string, unknown>;
51
66
  };
52
67
  type ReasoningEffort = 'minimal' | 'low' | 'medium' | 'high' | 'max';
53
68
  type ReasoningConfig = {
@@ -452,14 +467,22 @@ declare class StreamAbortedError extends AbortedError {
452
467
  }
453
468
  type ProviderErrorOptions = {
454
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;
455
477
  cause?: unknown;
456
478
  };
457
479
  declare class ProviderError extends CoreAIError {
458
480
  readonly statusCode?: number;
481
+ readonly code?: string;
459
482
  /**
460
483
  * @param message Human-readable error message (may include provider text).
461
484
  * @param provider Provider id (e.g. `'openai'`, `'anthropic'`).
462
- * @param options Optional HTTP status and underlying cause.
485
+ * @param options Optional HTTP status, provider code, and underlying cause.
463
486
  */
464
487
  constructor(message: string, provider: string, options?: ProviderErrorOptions);
465
488
  }
@@ -622,9 +645,16 @@ declare function createObjectStream<TSchema extends z.ZodType>(source: AsyncIter
622
645
  signal?: AbortSignal;
623
646
  }): ObjectStream<TSchema>;
624
647
 
625
- declare function createChatStream(source: AsyncIterable<StreamEvent> | (() => Promise<AsyncIterable<StreamEvent>>), options?: {
648
+ type CreateChatStreamOptions = {
626
649
  signal?: AbortSignal;
627
- }): 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;
628
658
 
629
659
  declare function wrapChatModel(config: {
630
660
  model: ChatModel;
@@ -653,4 +683,4 @@ type GenerateImageParams = ImageGenerateOptions & {
653
683
  };
654
684
  declare function generateImage(params: GenerateImageParams): Promise<ImageGenerateResult>;
655
685
 
656
- 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.21.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)",