@core-ai/core-ai 0.22.0 → 0.24.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 +28 -4
- package/dist/index.js +32 -3
- package/package.json +1 -1
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
|
}
|
|
@@ -495,6 +503,15 @@ declare class ContextLengthExceededError extends ProviderError {
|
|
|
495
503
|
readonly actualTokens?: number;
|
|
496
504
|
constructor(message: string, provider: string, options?: ContextLengthExceededErrorOptions);
|
|
497
505
|
}
|
|
506
|
+
type ProviderQuotaExceededErrorOptions = ProviderErrorOptions;
|
|
507
|
+
/**
|
|
508
|
+
* The provider account cannot accept requests because its billing quota or
|
|
509
|
+
* credit balance is exhausted. This is not retryable until the account
|
|
510
|
+
* configuration changes.
|
|
511
|
+
*/
|
|
512
|
+
declare class ProviderQuotaExceededError extends ProviderError {
|
|
513
|
+
constructor(message: string, provider: string, options?: ProviderQuotaExceededErrorOptions);
|
|
514
|
+
}
|
|
498
515
|
type RateLimitErrorOptions = ProviderErrorOptions & {
|
|
499
516
|
retryAfterSeconds?: number;
|
|
500
517
|
};
|
|
@@ -637,9 +654,16 @@ declare function createObjectStream<TSchema extends z.ZodType>(source: AsyncIter
|
|
|
637
654
|
signal?: AbortSignal;
|
|
638
655
|
}): ObjectStream<TSchema>;
|
|
639
656
|
|
|
640
|
-
|
|
657
|
+
type CreateChatStreamOptions = {
|
|
641
658
|
signal?: AbortSignal;
|
|
642
|
-
|
|
659
|
+
/**
|
|
660
|
+
* Maps errors raised while opening or iterating the source — including
|
|
661
|
+
* in-band SDK failures after HTTP 200 — onto a {@link CoreAIError}.
|
|
662
|
+
* Already-typed core-ai errors pass through unchanged.
|
|
663
|
+
*/
|
|
664
|
+
mapError?: (error: unknown) => CoreAIError;
|
|
665
|
+
};
|
|
666
|
+
declare function createChatStream(source: AsyncIterable<StreamEvent> | (() => Promise<AsyncIterable<StreamEvent>>), options?: CreateChatStreamOptions): ChatStream;
|
|
643
667
|
|
|
644
668
|
declare function wrapChatModel(config: {
|
|
645
669
|
model: ChatModel;
|
|
@@ -668,4 +692,4 @@ type GenerateImageParams = ImageGenerateOptions & {
|
|
|
668
692
|
};
|
|
669
693
|
declare function generateImage(params: GenerateImageParams): Promise<ImageGenerateResult>;
|
|
670
694
|
|
|
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 };
|
|
695
|
+
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, ProviderQuotaExceededError, type ProviderQuotaExceededErrorOptions, 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";
|
|
@@ -79,11 +82,18 @@ var ContextLengthExceededError = class extends ProviderError {
|
|
|
79
82
|
this.actualTokens = options.actualTokens;
|
|
80
83
|
}
|
|
81
84
|
};
|
|
85
|
+
var ProviderQuotaExceededError = class extends ProviderError {
|
|
86
|
+
constructor(message, provider, options = {}) {
|
|
87
|
+
super(message, provider, options);
|
|
88
|
+
this.name = "ProviderQuotaExceededError";
|
|
89
|
+
}
|
|
90
|
+
};
|
|
82
91
|
var RateLimitError = class extends RetryableProviderError {
|
|
83
92
|
retryAfterSeconds;
|
|
84
93
|
constructor(message, provider, options = {}) {
|
|
85
94
|
super(message, provider, {
|
|
86
95
|
statusCode: options.statusCode ?? 429,
|
|
96
|
+
code: options.code,
|
|
87
97
|
cause: options.cause
|
|
88
98
|
});
|
|
89
99
|
this.name = "RateLimitError";
|
|
@@ -648,12 +658,30 @@ function createObjectStream(source, options = {}) {
|
|
|
648
658
|
});
|
|
649
659
|
}
|
|
650
660
|
|
|
661
|
+
// src/map-stream-errors.ts
|
|
662
|
+
async function* mapStreamErrors(source, mapError) {
|
|
663
|
+
try {
|
|
664
|
+
for await (const event of source) {
|
|
665
|
+
yield event;
|
|
666
|
+
}
|
|
667
|
+
} catch (error) {
|
|
668
|
+
if (error instanceof CoreAIError || isLanguageError(error)) {
|
|
669
|
+
throw error;
|
|
670
|
+
}
|
|
671
|
+
throw mapError(error);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
function isLanguageError(error) {
|
|
675
|
+
return error instanceof TypeError || error instanceof RangeError || error instanceof SyntaxError || error instanceof ReferenceError;
|
|
676
|
+
}
|
|
677
|
+
|
|
651
678
|
// src/stream.ts
|
|
652
679
|
function createChatStream(source, options = {}) {
|
|
653
|
-
const { signal } = options;
|
|
680
|
+
const { signal, mapError } = options;
|
|
654
681
|
const resolvedSource = typeof source === "function" ? (async function* () {
|
|
655
682
|
yield* await source();
|
|
656
683
|
})() : source;
|
|
684
|
+
const sourceWithMappedErrors = mapError ? mapStreamErrors(resolvedSource, mapError) : resolvedSource;
|
|
657
685
|
const parts = [];
|
|
658
686
|
let textBuffer = "";
|
|
659
687
|
let textMetadata;
|
|
@@ -784,7 +812,7 @@ function createChatStream(source, options = {}) {
|
|
|
784
812
|
};
|
|
785
813
|
};
|
|
786
814
|
return createStream({
|
|
787
|
-
source:
|
|
815
|
+
source: sourceWithMappedErrors,
|
|
788
816
|
signal,
|
|
789
817
|
reduceEvent(event) {
|
|
790
818
|
switch (event.type) {
|
|
@@ -969,6 +997,7 @@ export {
|
|
|
969
997
|
MULTIMODAL_INPUT_MODALITIES,
|
|
970
998
|
ModelOverloadedError,
|
|
971
999
|
ProviderError,
|
|
1000
|
+
ProviderQuotaExceededError,
|
|
972
1001
|
RateLimitError,
|
|
973
1002
|
RetryableProviderError,
|
|
974
1003
|
ServiceUnavailableError,
|