@core-ai/core-ai 0.7.1 → 0.10.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
@@ -102,12 +102,49 @@ interface EmbedProviderOptions {
102
102
  interface ImageProviderOptions {
103
103
  [key: string]: Record<string, unknown> | undefined;
104
104
  }
105
+ type ChatModelMiddleware = {
106
+ generate?: (args: {
107
+ execute: (options?: GenerateOptions) => Promise<GenerateResult>;
108
+ options: GenerateOptions;
109
+ model: ChatModel;
110
+ }) => Promise<GenerateResult>;
111
+ stream?: (args: {
112
+ execute: (options?: GenerateOptions) => Promise<ChatStream>;
113
+ options: GenerateOptions;
114
+ model: ChatModel;
115
+ }) => Promise<ChatStream>;
116
+ generateObject?: <TSchema extends z.ZodType>(args: {
117
+ execute: (options?: GenerateObjectOptions<TSchema>) => Promise<GenerateObjectResult<TSchema>>;
118
+ options: GenerateObjectOptions<TSchema>;
119
+ model: ChatModel;
120
+ }) => Promise<GenerateObjectResult<TSchema>>;
121
+ streamObject?: <TSchema extends z.ZodType>(args: {
122
+ execute: (options?: StreamObjectOptions<TSchema>) => Promise<ObjectStream<TSchema>>;
123
+ options: StreamObjectOptions<TSchema>;
124
+ model: ChatModel;
125
+ }) => Promise<ObjectStream<TSchema>>;
126
+ };
127
+ type EmbeddingModelMiddleware = {
128
+ embed?: (args: {
129
+ execute: (options?: EmbedOptions) => Promise<EmbedResult>;
130
+ options: EmbedOptions;
131
+ model: EmbeddingModel;
132
+ }) => Promise<EmbedResult>;
133
+ };
134
+ type ImageModelMiddleware = {
135
+ generate?: (args: {
136
+ execute: (options?: ImageGenerateOptions) => Promise<ImageGenerateResult>;
137
+ options: ImageGenerateOptions;
138
+ model: ImageModel;
139
+ }) => Promise<ImageGenerateResult>;
140
+ };
105
141
  type BaseGenerateOptions = {
106
142
  messages: Message[];
107
143
  temperature?: number;
108
144
  maxTokens?: number;
109
145
  topP?: number;
110
146
  reasoning?: ReasoningConfig;
147
+ metadata?: Record<string, unknown>;
111
148
  providerOptions?: GenerateProviderOptions;
112
149
  signal?: AbortSignal;
113
150
  };
@@ -247,6 +284,7 @@ type EmbeddingModel = {
247
284
  type EmbedOptions = {
248
285
  input: string | string[];
249
286
  dimensions?: number;
287
+ metadata?: Record<string, unknown>;
250
288
  providerOptions?: EmbedProviderOptions;
251
289
  };
252
290
  type EmbedResult = {
@@ -270,6 +308,7 @@ type ImageGenerateOptions = {
270
308
  prompt: string;
271
309
  n?: number;
272
310
  size?: string;
311
+ metadata?: Record<string, unknown>;
273
312
  providerOptions?: ImageProviderOptions;
274
313
  };
275
314
  type ImageGenerateResult = {
@@ -281,15 +320,21 @@ type GeneratedImage = {
281
320
  revisedPrompt?: string;
282
321
  };
283
322
 
284
- declare class LLMError extends Error {
323
+ declare class CoreAIError extends Error {
285
324
  readonly cause?: unknown;
286
- constructor(message: string, cause?: unknown);
325
+ readonly provider?: string;
326
+ constructor(message: string, cause?: unknown, provider?: string);
287
327
  }
288
- declare class StreamAbortedError extends LLMError {
289
- constructor(message?: string, cause?: unknown);
328
+ declare class ValidationError extends CoreAIError {
329
+ constructor(message: string, cause?: unknown, provider?: string);
290
330
  }
291
- declare class ProviderError extends LLMError {
292
- readonly provider: string;
331
+ declare class AbortedError extends CoreAIError {
332
+ constructor(cause?: unknown, provider?: string);
333
+ }
334
+ declare class StreamAbortedError extends AbortedError {
335
+ constructor(cause?: unknown, provider?: string);
336
+ }
337
+ declare class ProviderError extends CoreAIError {
293
338
  readonly statusCode?: number;
294
339
  constructor(message: string, provider: string, statusCode?: number, cause?: unknown);
295
340
  }
@@ -298,7 +343,8 @@ type StructuredOutputErrorOptions = {
298
343
  cause?: unknown;
299
344
  rawOutput?: string;
300
345
  };
301
- declare class StructuredOutputError extends ProviderError {
346
+ declare class StructuredOutputError extends CoreAIError {
347
+ readonly statusCode?: number;
302
348
  readonly rawOutput?: string;
303
349
  constructor(message: string, provider: string, options?: StructuredOutputErrorOptions);
304
350
  }
@@ -359,6 +405,21 @@ declare function createChatStream(source: AsyncIterable<StreamEvent> | (() => Pr
359
405
  signal?: AbortSignal;
360
406
  }): ChatStream;
361
407
 
408
+ declare function wrapChatModel(config: {
409
+ model: ChatModel;
410
+ middleware: ChatModelMiddleware | ChatModelMiddleware[];
411
+ }): ChatModel;
412
+
413
+ declare function wrapEmbeddingModel(config: {
414
+ model: EmbeddingModel;
415
+ middleware: EmbeddingModelMiddleware | EmbeddingModelMiddleware[];
416
+ }): EmbeddingModel;
417
+
418
+ declare function wrapImageModel(config: {
419
+ model: ImageModel;
420
+ middleware: ImageModelMiddleware | ImageModelMiddleware[];
421
+ }): ImageModel;
422
+
362
423
  declare function getProviderMetadata<T extends Record<string, unknown>>(providerMetadata: Record<string, Record<string, unknown>> | undefined, provider: string): T | undefined;
363
424
 
364
425
  type EmbedParams = EmbedOptions & {
@@ -371,4 +432,4 @@ type GenerateImageParams = ImageGenerateOptions & {
371
432
  };
372
433
  declare function generateImage(params: GenerateImageParams): Promise<ImageGenerateResult>;
373
434
 
374
- export { type AssistantContentPart, type AssistantMessage, type AssistantTextPart, type BaseGenerateOptions, type ChatInputTokenDetails, type ChatModel, type ChatOutputTokenDetails, type ChatStream, type ChatUsage, type EmbedOptions, type EmbedProviderOptions, type EmbedResult, type EmbeddingModel, 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 ImagePart, type ImageProviderOptions, LLMError, type Message, type ObjectStream, type ObjectStreamEvent, ProviderError, type ReasoningConfig, type ReasoningEffort, type ReasoningPart, StreamAbortedError, type StreamEvent, type StreamObjectOptions, StructuredOutputError, StructuredOutputNoObjectGeneratedError, StructuredOutputParseError, StructuredOutputValidationError, type SystemMessage, type TextPart, type ToolCall, type ToolCallPart, type ToolChoice, type ToolDefinition, type ToolResultMessage, type ToolSet, type UserContentPart, type UserMessage, asObject, assistantMessage, createChatStream, createObjectStream, defineTool, embed, generate, generateImage, generateObject, getProviderMetadata, resultToMessage, safeParseJsonObject, stream, streamObject, stripModelDateSuffix, zodSchemaToJsonSchema };
435
+ export { AbortedError, type AssistantContentPart, type AssistantMessage, type AssistantTextPart, type BaseGenerateOptions, type ChatInputTokenDetails, type ChatModel, type ChatModelMiddleware, type ChatOutputTokenDetails, type ChatStream, type ChatUsage, 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, type Message, type ObjectStream, type ObjectStreamEvent, ProviderError, type ReasoningConfig, type ReasoningEffort, type ReasoningPart, StreamAbortedError, type StreamEvent, type StreamObjectOptions, StructuredOutputError, StructuredOutputNoObjectGeneratedError, StructuredOutputParseError, StructuredOutputValidationError, type SystemMessage, type TextPart, type ToolCall, type ToolCallPart, type ToolChoice, type ToolDefinition, type ToolResultMessage, type ToolSet, type UserContentPart, type UserMessage, ValidationError, asObject, assistantMessage, createChatStream, createObjectStream, defineTool, embed, generate, generateImage, generateObject, getProviderMetadata, resultToMessage, safeParseJsonObject, stream, streamObject, stripModelDateSuffix, wrapChatModel, wrapEmbeddingModel, wrapImageModel, zodSchemaToJsonSchema };
package/dist/index.js CHANGED
@@ -1,33 +1,48 @@
1
1
  // src/errors.ts
2
- var LLMError = class extends Error {
2
+ var CoreAIError = class extends Error {
3
3
  cause;
4
- constructor(message, cause) {
4
+ provider;
5
+ constructor(message, cause, provider) {
5
6
  super(message);
6
- this.name = "LLMError";
7
+ this.name = "CoreAIError";
7
8
  this.cause = cause;
9
+ this.provider = provider;
10
+ }
11
+ };
12
+ var ValidationError = class extends CoreAIError {
13
+ constructor(message, cause, provider) {
14
+ super(message, cause, provider);
15
+ this.name = "ValidationError";
16
+ }
17
+ };
18
+ var AbortedError = class extends CoreAIError {
19
+ constructor(cause, provider) {
20
+ super("operation aborted", cause, provider);
21
+ this.name = "AbortedError";
8
22
  }
9
23
  };
10
- var StreamAbortedError = class extends LLMError {
11
- constructor(message = "stream aborted", cause) {
12
- super(message, cause);
24
+ var StreamAbortedError = class extends AbortedError {
25
+ constructor(cause, provider) {
26
+ super(cause, provider);
13
27
  this.name = "StreamAbortedError";
28
+ this.message = "stream aborted";
14
29
  }
15
30
  };
16
- var ProviderError = class extends LLMError {
17
- provider;
31
+ var ProviderError = class extends CoreAIError {
18
32
  statusCode;
19
33
  constructor(message, provider, statusCode, cause) {
20
- super(message, cause);
34
+ super(message, cause, provider);
21
35
  this.name = "ProviderError";
22
- this.provider = provider;
23
36
  this.statusCode = statusCode;
24
37
  }
25
38
  };
26
- var StructuredOutputError = class extends ProviderError {
39
+ var StructuredOutputError = class extends CoreAIError {
40
+ statusCode;
27
41
  rawOutput;
28
42
  constructor(message, provider, options = {}) {
29
- super(message, provider, options.statusCode, options.cause);
43
+ super(message, options.cause, provider);
30
44
  this.name = "StructuredOutputError";
45
+ this.statusCode = options.statusCode;
31
46
  this.rawOutput = options.rawOutput;
32
47
  }
33
48
  };
@@ -108,19 +123,19 @@ function isEmptyText(value) {
108
123
  }
109
124
  function assertNonEmptyMessages(messages) {
110
125
  if (messages.length === 0) {
111
- throw new LLMError("messages must not be empty");
126
+ throw new ValidationError("messages must not be empty");
112
127
  }
113
128
  }
114
129
  function assertNonEmptyEmbedInput(input) {
115
130
  const isEmptyString = typeof input === "string" && isEmptyText(input);
116
131
  const isEmptyArray = Array.isArray(input) && input.length === 0;
117
132
  if (isEmptyString || isEmptyArray) {
118
- throw new LLMError("input must not be empty");
133
+ throw new ValidationError("input must not be empty");
119
134
  }
120
135
  }
121
136
  function assertNonEmptyPrompt(prompt) {
122
137
  if (isEmptyText(prompt)) {
123
- throw new LLMError("prompt must not be empty");
138
+ throw new ValidationError("prompt must not be empty");
124
139
  }
125
140
  }
126
141
 
@@ -140,10 +155,7 @@ function callModelWithOptions(params, call) {
140
155
  // src/generate.ts
141
156
  async function generate(params) {
142
157
  assertNonEmptyMessages(params.messages);
143
- return callModelWithOptions(
144
- params,
145
- (model, options) => model.generate(options)
146
- );
158
+ return callModelWithOptions(params, (model, options) => model.generate(options));
147
159
  }
148
160
 
149
161
  // src/generate-object.ts
@@ -240,7 +252,7 @@ function createStream(options) {
240
252
  if (terminalState.status !== "running") {
241
253
  return;
242
254
  }
243
- settleRejected(new StreamAbortedError("stream aborted"));
255
+ settleRejected(new StreamAbortedError());
244
256
  void closeSourceIterator();
245
257
  }
246
258
  if (signal) {
@@ -361,7 +373,7 @@ function createObjectStream(source, options = {}) {
361
373
  },
362
374
  finalizeResult() {
363
375
  if (objectState.status !== "ready") {
364
- throw new LLMError(
376
+ throw new CoreAIError(
365
377
  "object stream completed without emitting a final object"
366
378
  );
367
379
  }
@@ -522,6 +534,114 @@ function createChatStream(source, options = {}) {
522
534
  });
523
535
  }
524
536
 
537
+ // src/wrap-model-utils.ts
538
+ function buildMiddlewareChain(config) {
539
+ const { model, operations, finalExecute } = config;
540
+ return operations.reduceRight(
541
+ (nextExecute, operation) => {
542
+ return (options) => operation({
543
+ execute: (overrideOptions) => nextExecute(overrideOptions ?? options),
544
+ options,
545
+ model
546
+ });
547
+ },
548
+ finalExecute
549
+ );
550
+ }
551
+ function normalizeMiddleware(middleware) {
552
+ return Array.isArray(middleware) ? middleware : [middleware];
553
+ }
554
+
555
+ // src/wrap-chat-model.ts
556
+ function wrapChatModel(config) {
557
+ const middlewares = normalizeMiddleware(config.middleware);
558
+ const { model } = config;
559
+ const generateOps = middlewares.flatMap(
560
+ (mw) => mw.generate ? [mw.generate] : []
561
+ );
562
+ const streamOps = middlewares.flatMap(
563
+ (mw) => mw.stream ? [mw.stream] : []
564
+ );
565
+ return {
566
+ provider: model.provider,
567
+ modelId: model.modelId,
568
+ generate(options) {
569
+ return buildMiddlewareChain({
570
+ model,
571
+ operations: generateOps,
572
+ finalExecute: (opts) => model.generate(opts)
573
+ })(options);
574
+ },
575
+ stream(options) {
576
+ return buildMiddlewareChain({
577
+ model,
578
+ operations: streamOps,
579
+ finalExecute: (opts) => model.stream(opts)
580
+ })(options);
581
+ },
582
+ generateObject(options) {
583
+ const operations = middlewares.flatMap(
584
+ (mw) => mw.generateObject ? [mw.generateObject] : []
585
+ );
586
+ return buildMiddlewareChain({
587
+ model,
588
+ operations,
589
+ finalExecute: (opts) => model.generateObject(opts)
590
+ })(options);
591
+ },
592
+ streamObject(options) {
593
+ const operations = middlewares.flatMap(
594
+ (mw) => mw.streamObject ? [mw.streamObject] : []
595
+ );
596
+ return buildMiddlewareChain({
597
+ model,
598
+ operations,
599
+ finalExecute: (opts) => model.streamObject(opts)
600
+ })(options);
601
+ }
602
+ };
603
+ }
604
+
605
+ // src/wrap-embedding-model.ts
606
+ function wrapEmbeddingModel(config) {
607
+ const middlewares = normalizeMiddleware(config.middleware);
608
+ const { model } = config;
609
+ const embedOps = middlewares.flatMap(
610
+ (mw) => mw.embed ? [mw.embed] : []
611
+ );
612
+ return {
613
+ provider: model.provider,
614
+ modelId: model.modelId,
615
+ embed(options) {
616
+ return buildMiddlewareChain({
617
+ model,
618
+ operations: embedOps,
619
+ finalExecute: (opts) => model.embed(opts)
620
+ })(options);
621
+ }
622
+ };
623
+ }
624
+
625
+ // src/wrap-image-model.ts
626
+ function wrapImageModel(config) {
627
+ const middlewares = normalizeMiddleware(config.middleware);
628
+ const { model } = config;
629
+ const generateOps = middlewares.flatMap(
630
+ (mw) => mw.generate ? [mw.generate] : []
631
+ );
632
+ return {
633
+ provider: model.provider,
634
+ modelId: model.modelId,
635
+ generate(options) {
636
+ return buildMiddlewareChain({
637
+ model,
638
+ operations: generateOps,
639
+ finalExecute: (opts) => model.generate(opts)
640
+ })(options);
641
+ }
642
+ };
643
+ }
644
+
525
645
  // src/provider-metadata.ts
526
646
  function getProviderMetadata(providerMetadata, provider) {
527
647
  return providerMetadata?.[provider];
@@ -542,13 +662,15 @@ async function generateImage(params) {
542
662
  );
543
663
  }
544
664
  export {
545
- LLMError,
665
+ AbortedError,
666
+ CoreAIError,
546
667
  ProviderError,
547
668
  StreamAbortedError,
548
669
  StructuredOutputError,
549
670
  StructuredOutputNoObjectGeneratedError,
550
671
  StructuredOutputParseError,
551
672
  StructuredOutputValidationError,
673
+ ValidationError,
552
674
  asObject,
553
675
  assistantMessage,
554
676
  createChatStream,
@@ -564,5 +686,8 @@ export {
564
686
  stream,
565
687
  streamObject,
566
688
  stripModelDateSuffix,
689
+ wrapChatModel,
690
+ wrapEmbeddingModel,
691
+ wrapImageModel,
567
692
  zodSchemaToJsonSchema
568
693
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@core-ai/core-ai",
3
- "version": "0.7.1",
3
+ "version": "0.10.0",
4
4
  "description": "Type-safe LLM abstraction layer over native provider SDKs",
5
5
  "license": "MIT",
6
6
  "author": "Omnifact (https://omnifact.ai)",