@core-ai/core-ai 0.7.0 → 0.7.1
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 +6 -1
- package/dist/index.js +99 -52
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -321,6 +321,11 @@ declare function defineTool(options: ToolDefinition): ToolDefinition;
|
|
|
321
321
|
*/
|
|
322
322
|
declare function zodSchemaToJsonSchema(schema: z.ZodType): Record<string, unknown>;
|
|
323
323
|
|
|
324
|
+
declare function stripModelDateSuffix(modelId: string): string;
|
|
325
|
+
|
|
326
|
+
declare function asObject(value: unknown): Record<string, unknown>;
|
|
327
|
+
declare function safeParseJsonObject(json: string): Record<string, unknown>;
|
|
328
|
+
|
|
324
329
|
type ResultToMessageOptions = {
|
|
325
330
|
includeReasoning?: boolean;
|
|
326
331
|
};
|
|
@@ -366,4 +371,4 @@ type GenerateImageParams = ImageGenerateOptions & {
|
|
|
366
371
|
};
|
|
367
372
|
declare function generateImage(params: GenerateImageParams): Promise<ImageGenerateResult>;
|
|
368
373
|
|
|
369
|
-
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, assistantMessage, createChatStream, createObjectStream, defineTool, embed, generate, generateImage, generateObject, getProviderMetadata, resultToMessage, stream, streamObject, zodSchemaToJsonSchema };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -65,6 +65,27 @@ function zodSchemaToJsonSchema(schema) {
|
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
// src/model-id.ts
|
|
69
|
+
var MODEL_DATE_SUFFIX_PATTERN = /-\d{8}$/;
|
|
70
|
+
function stripModelDateSuffix(modelId) {
|
|
71
|
+
return modelId.replace(MODEL_DATE_SUFFIX_PATTERN, "");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/provider-utils.ts
|
|
75
|
+
function asObject(value) {
|
|
76
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
function safeParseJsonObject(json) {
|
|
82
|
+
try {
|
|
83
|
+
return asObject(JSON.parse(json));
|
|
84
|
+
} catch {
|
|
85
|
+
return {};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
68
89
|
// src/result-to-message.ts
|
|
69
90
|
function resultToMessage(result, options) {
|
|
70
91
|
const includeReasoning = options?.includeReasoning ?? true;
|
|
@@ -111,26 +132,33 @@ function splitModelFromParams(params) {
|
|
|
111
132
|
options
|
|
112
133
|
};
|
|
113
134
|
}
|
|
135
|
+
function callModelWithOptions(params, call) {
|
|
136
|
+
const { model, options } = splitModelFromParams(params);
|
|
137
|
+
return call(model, options);
|
|
138
|
+
}
|
|
114
139
|
|
|
115
140
|
// src/generate.ts
|
|
116
141
|
async function generate(params) {
|
|
117
142
|
assertNonEmptyMessages(params.messages);
|
|
118
|
-
|
|
119
|
-
|
|
143
|
+
return callModelWithOptions(
|
|
144
|
+
params,
|
|
145
|
+
(model, options) => model.generate(options)
|
|
146
|
+
);
|
|
120
147
|
}
|
|
121
148
|
|
|
122
149
|
// src/generate-object.ts
|
|
123
150
|
async function generateObject(params) {
|
|
124
151
|
assertNonEmptyMessages(params.messages);
|
|
125
|
-
|
|
126
|
-
|
|
152
|
+
return callModelWithOptions(
|
|
153
|
+
params,
|
|
154
|
+
(model, options) => model.generateObject(options)
|
|
155
|
+
);
|
|
127
156
|
}
|
|
128
157
|
|
|
129
158
|
// src/stream-chat.ts
|
|
130
159
|
async function stream(params) {
|
|
131
160
|
assertNonEmptyMessages(params.messages);
|
|
132
|
-
|
|
133
|
-
return model.stream(options);
|
|
161
|
+
return callModelWithOptions(params, (model, options) => model.stream(options));
|
|
134
162
|
}
|
|
135
163
|
|
|
136
164
|
// src/base-stream.ts
|
|
@@ -170,31 +198,31 @@ function createStream(options) {
|
|
|
170
198
|
function cleanupSignalListener() {
|
|
171
199
|
signal?.removeEventListener("abort", abortStream);
|
|
172
200
|
}
|
|
173
|
-
function
|
|
201
|
+
function settleTerminalState(nextState) {
|
|
174
202
|
if (terminalState.status !== "running") {
|
|
175
203
|
return;
|
|
176
204
|
}
|
|
177
|
-
terminalState =
|
|
178
|
-
status: "completed",
|
|
179
|
-
result: finalResult
|
|
180
|
-
};
|
|
205
|
+
terminalState = nextState;
|
|
181
206
|
cleanupSignalListener();
|
|
182
|
-
|
|
207
|
+
if (nextState.status === "completed") {
|
|
208
|
+
resolveResult?.(nextState.result);
|
|
209
|
+
} else {
|
|
210
|
+
rejectResult?.(nextState.error);
|
|
211
|
+
}
|
|
183
212
|
resolveEvents?.([...bufferedEvents]);
|
|
184
213
|
notifyWaiters();
|
|
185
214
|
}
|
|
215
|
+
function settleCompleted(finalResult) {
|
|
216
|
+
settleTerminalState({
|
|
217
|
+
status: "completed",
|
|
218
|
+
result: finalResult
|
|
219
|
+
});
|
|
220
|
+
}
|
|
186
221
|
function settleRejected(error) {
|
|
187
|
-
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
terminalState = {
|
|
222
|
+
settleTerminalState({
|
|
191
223
|
status: "rejected",
|
|
192
224
|
error
|
|
193
|
-
};
|
|
194
|
-
cleanupSignalListener();
|
|
195
|
-
rejectResult?.(error);
|
|
196
|
-
resolveEvents?.([...bufferedEvents]);
|
|
197
|
-
notifyWaiters();
|
|
225
|
+
});
|
|
198
226
|
}
|
|
199
227
|
function closeSourceIterator() {
|
|
200
228
|
if (closeSourceIteratorPromise) {
|
|
@@ -248,6 +276,12 @@ function createStream(options) {
|
|
|
248
276
|
}
|
|
249
277
|
}
|
|
250
278
|
void pump();
|
|
279
|
+
function getDoneResult() {
|
|
280
|
+
return {
|
|
281
|
+
done: true,
|
|
282
|
+
value: void 0
|
|
283
|
+
};
|
|
284
|
+
}
|
|
251
285
|
return {
|
|
252
286
|
[Symbol.asyncIterator]() {
|
|
253
287
|
let index = 0;
|
|
@@ -255,17 +289,11 @@ function createStream(options) {
|
|
|
255
289
|
return {
|
|
256
290
|
async next() {
|
|
257
291
|
if (closed) {
|
|
258
|
-
return
|
|
259
|
-
done: true,
|
|
260
|
-
value: void 0
|
|
261
|
-
};
|
|
292
|
+
return getDoneResult();
|
|
262
293
|
}
|
|
263
294
|
while (!closed && index >= bufferedEvents.length) {
|
|
264
295
|
if (terminalState.status === "completed") {
|
|
265
|
-
return
|
|
266
|
-
done: true,
|
|
267
|
-
value: void 0
|
|
268
|
-
};
|
|
296
|
+
return getDoneResult();
|
|
269
297
|
}
|
|
270
298
|
if (terminalState.status === "rejected") {
|
|
271
299
|
throw terminalState.error;
|
|
@@ -273,10 +301,7 @@ function createStream(options) {
|
|
|
273
301
|
await resolveWhenUpdated();
|
|
274
302
|
}
|
|
275
303
|
if (closed) {
|
|
276
|
-
return
|
|
277
|
-
done: true,
|
|
278
|
-
value: void 0
|
|
279
|
-
};
|
|
304
|
+
return getDoneResult();
|
|
280
305
|
}
|
|
281
306
|
const value = bufferedEvents[index];
|
|
282
307
|
index += 1;
|
|
@@ -288,10 +313,7 @@ function createStream(options) {
|
|
|
288
313
|
async return() {
|
|
289
314
|
closed = true;
|
|
290
315
|
notifyWaiters();
|
|
291
|
-
return
|
|
292
|
-
done: true,
|
|
293
|
-
value: void 0
|
|
294
|
-
};
|
|
316
|
+
return getDoneResult();
|
|
295
317
|
}
|
|
296
318
|
};
|
|
297
319
|
},
|
|
@@ -303,8 +325,10 @@ function createStream(options) {
|
|
|
303
325
|
// src/stream-object.ts
|
|
304
326
|
async function streamObject(params) {
|
|
305
327
|
assertNonEmptyMessages(params.messages);
|
|
306
|
-
|
|
307
|
-
|
|
328
|
+
return callModelWithOptions(
|
|
329
|
+
params,
|
|
330
|
+
(model, options) => model.streamObject(options)
|
|
331
|
+
);
|
|
308
332
|
}
|
|
309
333
|
function createObjectStream(source, options = {}) {
|
|
310
334
|
const { signal } = options;
|
|
@@ -430,6 +454,31 @@ function createChatStream(source, options = {}) {
|
|
|
430
454
|
finishReason = event.finishReason;
|
|
431
455
|
usage = event.usage;
|
|
432
456
|
};
|
|
457
|
+
const collectFinalizedData = () => {
|
|
458
|
+
const contentSegments = [];
|
|
459
|
+
const reasoningSegments = [];
|
|
460
|
+
const toolCalls = [];
|
|
461
|
+
for (const part of parts) {
|
|
462
|
+
if (part.type === "text") {
|
|
463
|
+
contentSegments.push(part.text);
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
if (part.type === "reasoning") {
|
|
467
|
+
reasoningSegments.push(part.text);
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (part.type === "tool-call") {
|
|
471
|
+
toolCalls.push(part.toolCall);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const content = contentSegments.join("");
|
|
475
|
+
const reasoning = reasoningSegments.join("");
|
|
476
|
+
return {
|
|
477
|
+
content: content.length > 0 ? content : null,
|
|
478
|
+
reasoning: reasoning.length > 0 ? reasoning : null,
|
|
479
|
+
toolCalls
|
|
480
|
+
};
|
|
481
|
+
};
|
|
433
482
|
return createStream({
|
|
434
483
|
source: resolvedSource,
|
|
435
484
|
signal,
|
|
@@ -460,17 +509,11 @@ function createChatStream(source, options = {}) {
|
|
|
460
509
|
finalizeResult() {
|
|
461
510
|
flushText();
|
|
462
511
|
flushReasoning();
|
|
463
|
-
const content
|
|
464
|
-
const reasoning = parts.flatMap(
|
|
465
|
-
(part) => part.type === "reasoning" ? [part.text] : []
|
|
466
|
-
).join("");
|
|
467
|
-
const toolCalls = parts.flatMap(
|
|
468
|
-
(part) => part.type === "tool-call" ? [part.toolCall] : []
|
|
469
|
-
);
|
|
512
|
+
const { content, reasoning, toolCalls } = collectFinalizedData();
|
|
470
513
|
return {
|
|
471
514
|
parts,
|
|
472
|
-
content
|
|
473
|
-
reasoning
|
|
515
|
+
content,
|
|
516
|
+
reasoning,
|
|
474
517
|
toolCalls,
|
|
475
518
|
finishReason,
|
|
476
519
|
usage
|
|
@@ -487,15 +530,16 @@ function getProviderMetadata(providerMetadata, provider) {
|
|
|
487
530
|
// src/embed.ts
|
|
488
531
|
async function embed(params) {
|
|
489
532
|
assertNonEmptyEmbedInput(params.input);
|
|
490
|
-
|
|
491
|
-
return model.embed(options);
|
|
533
|
+
return callModelWithOptions(params, (model, options) => model.embed(options));
|
|
492
534
|
}
|
|
493
535
|
|
|
494
536
|
// src/generate-image.ts
|
|
495
537
|
async function generateImage(params) {
|
|
496
538
|
assertNonEmptyPrompt(params.prompt);
|
|
497
|
-
|
|
498
|
-
|
|
539
|
+
return callModelWithOptions(
|
|
540
|
+
params,
|
|
541
|
+
(model, options) => model.generate(options)
|
|
542
|
+
);
|
|
499
543
|
}
|
|
500
544
|
export {
|
|
501
545
|
LLMError,
|
|
@@ -505,6 +549,7 @@ export {
|
|
|
505
549
|
StructuredOutputNoObjectGeneratedError,
|
|
506
550
|
StructuredOutputParseError,
|
|
507
551
|
StructuredOutputValidationError,
|
|
552
|
+
asObject,
|
|
508
553
|
assistantMessage,
|
|
509
554
|
createChatStream,
|
|
510
555
|
createObjectStream,
|
|
@@ -515,7 +560,9 @@ export {
|
|
|
515
560
|
generateObject,
|
|
516
561
|
getProviderMetadata,
|
|
517
562
|
resultToMessage,
|
|
563
|
+
safeParseJsonObject,
|
|
518
564
|
stream,
|
|
519
565
|
streamObject,
|
|
566
|
+
stripModelDateSuffix,
|
|
520
567
|
zodSchemaToJsonSchema
|
|
521
568
|
};
|