@core-ai/core-ai 0.6.0 → 0.7.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 +7 -1
- package/dist/index.js +93 -44
- package/package.json +2 -3
package/dist/index.d.ts
CHANGED
|
@@ -315,6 +315,12 @@ declare class StructuredOutputValidationError extends StructuredOutputError {
|
|
|
315
315
|
|
|
316
316
|
declare function defineTool(options: ToolDefinition): ToolDefinition;
|
|
317
317
|
|
|
318
|
+
/**
|
|
319
|
+
* Convert a Zod schema to a JSON Schema object using Zod 4's native
|
|
320
|
+
* `z.toJSONSchema()`.
|
|
321
|
+
*/
|
|
322
|
+
declare function zodSchemaToJsonSchema(schema: z.ZodType): Record<string, unknown>;
|
|
323
|
+
|
|
318
324
|
type ResultToMessageOptions = {
|
|
319
325
|
includeReasoning?: boolean;
|
|
320
326
|
};
|
|
@@ -360,4 +366,4 @@ type GenerateImageParams = ImageGenerateOptions & {
|
|
|
360
366
|
};
|
|
361
367
|
declare function generateImage(params: GenerateImageParams): Promise<ImageGenerateResult>;
|
|
362
368
|
|
|
363
|
-
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 };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -53,11 +53,18 @@ var StructuredOutputValidationError = class extends StructuredOutputError {
|
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
// src/tool.ts
|
|
56
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
57
56
|
function defineTool(options) {
|
|
58
57
|
return options;
|
|
59
58
|
}
|
|
60
59
|
|
|
60
|
+
// src/json-schema.ts
|
|
61
|
+
import { z } from "zod";
|
|
62
|
+
function zodSchemaToJsonSchema(schema) {
|
|
63
|
+
return z.toJSONSchema(schema, {
|
|
64
|
+
io: "input"
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
61
68
|
// src/result-to-message.ts
|
|
62
69
|
function resultToMessage(result, options) {
|
|
63
70
|
const includeReasoning = options?.includeReasoning ?? true;
|
|
@@ -75,38 +82,54 @@ function assistantMessage(content) {
|
|
|
75
82
|
}
|
|
76
83
|
|
|
77
84
|
// src/assertions.ts
|
|
85
|
+
function isEmptyText(value) {
|
|
86
|
+
return value.length === 0;
|
|
87
|
+
}
|
|
78
88
|
function assertNonEmptyMessages(messages) {
|
|
79
89
|
if (messages.length === 0) {
|
|
80
90
|
throw new LLMError("messages must not be empty");
|
|
81
91
|
}
|
|
82
92
|
}
|
|
83
93
|
function assertNonEmptyEmbedInput(input) {
|
|
84
|
-
|
|
94
|
+
const isEmptyString = typeof input === "string" && isEmptyText(input);
|
|
95
|
+
const isEmptyArray = Array.isArray(input) && input.length === 0;
|
|
96
|
+
if (isEmptyString || isEmptyArray) {
|
|
85
97
|
throw new LLMError("input must not be empty");
|
|
86
98
|
}
|
|
87
|
-
|
|
88
|
-
|
|
99
|
+
}
|
|
100
|
+
function assertNonEmptyPrompt(prompt) {
|
|
101
|
+
if (isEmptyText(prompt)) {
|
|
102
|
+
throw new LLMError("prompt must not be empty");
|
|
89
103
|
}
|
|
90
104
|
}
|
|
91
105
|
|
|
106
|
+
// src/model-options.ts
|
|
107
|
+
function splitModelFromParams(params) {
|
|
108
|
+
const { model, ...options } = params;
|
|
109
|
+
return {
|
|
110
|
+
model,
|
|
111
|
+
options
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
92
115
|
// src/generate.ts
|
|
93
116
|
async function generate(params) {
|
|
94
117
|
assertNonEmptyMessages(params.messages);
|
|
95
|
-
const { model,
|
|
118
|
+
const { model, options } = splitModelFromParams(params);
|
|
96
119
|
return model.generate(options);
|
|
97
120
|
}
|
|
98
121
|
|
|
99
122
|
// src/generate-object.ts
|
|
100
123
|
async function generateObject(params) {
|
|
101
124
|
assertNonEmptyMessages(params.messages);
|
|
102
|
-
const { model,
|
|
125
|
+
const { model, options } = splitModelFromParams(params);
|
|
103
126
|
return model.generateObject(options);
|
|
104
127
|
}
|
|
105
128
|
|
|
106
129
|
// src/stream-chat.ts
|
|
107
130
|
async function stream(params) {
|
|
108
131
|
assertNonEmptyMessages(params.messages);
|
|
109
|
-
const { model,
|
|
132
|
+
const { model, options } = splitModelFromParams(params);
|
|
110
133
|
return model.stream(options);
|
|
111
134
|
}
|
|
112
135
|
|
|
@@ -370,41 +393,68 @@ function createChatStream(source, options = {}) {
|
|
|
370
393
|
reasoningBuffer = "";
|
|
371
394
|
reasoningProviderMetadata = void 0;
|
|
372
395
|
};
|
|
396
|
+
const startReasoning = () => {
|
|
397
|
+
flushText();
|
|
398
|
+
flushReasoning();
|
|
399
|
+
insideReasoning = true;
|
|
400
|
+
};
|
|
401
|
+
const appendReasoning = (text) => {
|
|
402
|
+
if (!insideReasoning) {
|
|
403
|
+
flushText();
|
|
404
|
+
insideReasoning = true;
|
|
405
|
+
}
|
|
406
|
+
reasoningBuffer += text;
|
|
407
|
+
};
|
|
408
|
+
const endReasoning = (providerMetadata) => {
|
|
409
|
+
reasoningProviderMetadata = providerMetadata;
|
|
410
|
+
flushReasoning();
|
|
411
|
+
insideReasoning = false;
|
|
412
|
+
};
|
|
413
|
+
const appendText = (text) => {
|
|
414
|
+
if (insideReasoning) {
|
|
415
|
+
flushReasoning();
|
|
416
|
+
insideReasoning = false;
|
|
417
|
+
}
|
|
418
|
+
textBuffer += text;
|
|
419
|
+
};
|
|
420
|
+
const appendToolCall = (toolCall) => {
|
|
421
|
+
flushText();
|
|
422
|
+
flushReasoning();
|
|
423
|
+
insideReasoning = false;
|
|
424
|
+
parts.push({
|
|
425
|
+
type: "tool-call",
|
|
426
|
+
toolCall
|
|
427
|
+
});
|
|
428
|
+
};
|
|
429
|
+
const setFinish = (event) => {
|
|
430
|
+
finishReason = event.finishReason;
|
|
431
|
+
usage = event.usage;
|
|
432
|
+
};
|
|
373
433
|
return createStream({
|
|
374
434
|
source: resolvedSource,
|
|
375
435
|
signal,
|
|
376
436
|
reduceEvent(event) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
flushText();
|
|
399
|
-
flushReasoning();
|
|
400
|
-
insideReasoning = false;
|
|
401
|
-
parts.push({
|
|
402
|
-
type: "tool-call",
|
|
403
|
-
toolCall: event.toolCall
|
|
404
|
-
});
|
|
405
|
-
} else if (event.type === "finish") {
|
|
406
|
-
finishReason = event.finishReason;
|
|
407
|
-
usage = event.usage;
|
|
437
|
+
switch (event.type) {
|
|
438
|
+
case "reasoning-start":
|
|
439
|
+
startReasoning();
|
|
440
|
+
break;
|
|
441
|
+
case "reasoning-delta":
|
|
442
|
+
appendReasoning(event.text);
|
|
443
|
+
break;
|
|
444
|
+
case "reasoning-end":
|
|
445
|
+
endReasoning(event.providerMetadata);
|
|
446
|
+
break;
|
|
447
|
+
case "text-delta":
|
|
448
|
+
appendText(event.text);
|
|
449
|
+
break;
|
|
450
|
+
case "tool-call-end":
|
|
451
|
+
appendToolCall(event.toolCall);
|
|
452
|
+
break;
|
|
453
|
+
case "finish":
|
|
454
|
+
setFinish(event);
|
|
455
|
+
break;
|
|
456
|
+
default:
|
|
457
|
+
break;
|
|
408
458
|
}
|
|
409
459
|
},
|
|
410
460
|
finalizeResult() {
|
|
@@ -437,16 +487,14 @@ function getProviderMetadata(providerMetadata, provider) {
|
|
|
437
487
|
// src/embed.ts
|
|
438
488
|
async function embed(params) {
|
|
439
489
|
assertNonEmptyEmbedInput(params.input);
|
|
440
|
-
const { model,
|
|
490
|
+
const { model, options } = splitModelFromParams(params);
|
|
441
491
|
return model.embed(options);
|
|
442
492
|
}
|
|
443
493
|
|
|
444
494
|
// src/generate-image.ts
|
|
445
495
|
async function generateImage(params) {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
}
|
|
449
|
-
const { model, ...options } = params;
|
|
496
|
+
assertNonEmptyPrompt(params.prompt);
|
|
497
|
+
const { model, options } = splitModelFromParams(params);
|
|
450
498
|
return model.generate(options);
|
|
451
499
|
}
|
|
452
500
|
export {
|
|
@@ -468,5 +516,6 @@ export {
|
|
|
468
516
|
getProviderMetadata,
|
|
469
517
|
resultToMessage,
|
|
470
518
|
stream,
|
|
471
|
-
streamObject
|
|
519
|
+
streamObject,
|
|
520
|
+
zodSchemaToJsonSchema
|
|
472
521
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@core-ai/core-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Type-safe LLM abstraction layer over native provider SDKs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Omnifact (https://omnifact.ai)",
|
|
@@ -43,8 +43,7 @@
|
|
|
43
43
|
"test:watch": "vitest"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"zod": "^
|
|
47
|
-
"zod-to-json-schema": "^3.25.1"
|
|
46
|
+
"zod": "^4.0.0"
|
|
48
47
|
},
|
|
49
48
|
"devDependencies": {
|
|
50
49
|
"@core-ai/eslint-config": "*",
|