@core-ai/core-ai 0.8.0 → 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 +55 -1
- package/dist/index.js +112 -4
- package/package.json +1 -1
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 = {
|
|
@@ -366,6 +405,21 @@ declare function createChatStream(source: AsyncIterable<StreamEvent> | (() => Pr
|
|
|
366
405
|
signal?: AbortSignal;
|
|
367
406
|
}): ChatStream;
|
|
368
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
|
+
|
|
369
423
|
declare function getProviderMetadata<T extends Record<string, unknown>>(providerMetadata: Record<string, Record<string, unknown>> | undefined, provider: string): T | undefined;
|
|
370
424
|
|
|
371
425
|
type EmbedParams = EmbedOptions & {
|
|
@@ -378,4 +432,4 @@ type GenerateImageParams = ImageGenerateOptions & {
|
|
|
378
432
|
};
|
|
379
433
|
declare function generateImage(params: GenerateImageParams): Promise<ImageGenerateResult>;
|
|
380
434
|
|
|
381
|
-
export { AbortedError, type AssistantContentPart, type AssistantMessage, type AssistantTextPart, type BaseGenerateOptions, type ChatInputTokenDetails, type ChatModel, type ChatOutputTokenDetails, type ChatStream, type ChatUsage, CoreAIError, 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, 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, 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
|
@@ -155,10 +155,7 @@ function callModelWithOptions(params, call) {
|
|
|
155
155
|
// src/generate.ts
|
|
156
156
|
async function generate(params) {
|
|
157
157
|
assertNonEmptyMessages(params.messages);
|
|
158
|
-
return callModelWithOptions(
|
|
159
|
-
params,
|
|
160
|
-
(model, options) => model.generate(options)
|
|
161
|
-
);
|
|
158
|
+
return callModelWithOptions(params, (model, options) => model.generate(options));
|
|
162
159
|
}
|
|
163
160
|
|
|
164
161
|
// src/generate-object.ts
|
|
@@ -537,6 +534,114 @@ function createChatStream(source, options = {}) {
|
|
|
537
534
|
});
|
|
538
535
|
}
|
|
539
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
|
+
|
|
540
645
|
// src/provider-metadata.ts
|
|
541
646
|
function getProviderMetadata(providerMetadata, provider) {
|
|
542
647
|
return providerMetadata?.[provider];
|
|
@@ -581,5 +686,8 @@ export {
|
|
|
581
686
|
stream,
|
|
582
687
|
streamObject,
|
|
583
688
|
stripModelDateSuffix,
|
|
689
|
+
wrapChatModel,
|
|
690
|
+
wrapEmbeddingModel,
|
|
691
|
+
wrapImageModel,
|
|
584
692
|
zodSchemaToJsonSchema
|
|
585
693
|
};
|