@core-ai/core-ai 0.2.0 → 0.3.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/README.md +2 -0
- package/dist/index.d.ts +68 -2
- package/dist/index.js +122 -1
- package/package.json +3 -3
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -62,6 +62,8 @@ type ChatModel = {
|
|
|
62
62
|
readonly modelId: string;
|
|
63
63
|
generate(options: GenerateOptions): Promise<GenerateResult>;
|
|
64
64
|
stream(options: GenerateOptions): Promise<StreamResult>;
|
|
65
|
+
generateObject<TSchema extends z.ZodType>(options: GenerateObjectOptions<TSchema>): Promise<GenerateObjectResult<TSchema>>;
|
|
66
|
+
streamObject<TSchema extends z.ZodType>(options: StreamObjectOptions<TSchema>): Promise<StreamObjectResult<TSchema>>;
|
|
65
67
|
};
|
|
66
68
|
type ModelConfig = {
|
|
67
69
|
temperature?: number;
|
|
@@ -85,6 +87,21 @@ type GenerateResult = {
|
|
|
85
87
|
finishReason: FinishReason;
|
|
86
88
|
usage: ChatUsage;
|
|
87
89
|
};
|
|
90
|
+
type GenerateObjectOptions<TSchema extends z.ZodType> = {
|
|
91
|
+
messages: Message[];
|
|
92
|
+
schema: TSchema;
|
|
93
|
+
schemaName?: string;
|
|
94
|
+
schemaDescription?: string;
|
|
95
|
+
config?: ModelConfig;
|
|
96
|
+
providerOptions?: Record<string, unknown>;
|
|
97
|
+
signal?: AbortSignal;
|
|
98
|
+
};
|
|
99
|
+
type StreamObjectOptions<TSchema extends z.ZodType> = GenerateObjectOptions<TSchema>;
|
|
100
|
+
type GenerateObjectResult<TSchema extends z.ZodType> = {
|
|
101
|
+
object: z.infer<TSchema>;
|
|
102
|
+
finishReason: FinishReason;
|
|
103
|
+
usage: ChatUsage;
|
|
104
|
+
};
|
|
88
105
|
type FinishReason = 'stop' | 'length' | 'tool-calls' | 'content-filter' | 'unknown';
|
|
89
106
|
/**
|
|
90
107
|
* Token usage reported by the model after a chat completion.
|
|
@@ -134,6 +151,20 @@ type StreamEvent = {
|
|
|
134
151
|
type StreamResult = AsyncIterable<StreamEvent> & {
|
|
135
152
|
toResponse(): Promise<GenerateResult>;
|
|
136
153
|
};
|
|
154
|
+
type ObjectStreamEvent<TSchema extends z.ZodType> = {
|
|
155
|
+
type: 'object-delta';
|
|
156
|
+
text: string;
|
|
157
|
+
} | {
|
|
158
|
+
type: 'object';
|
|
159
|
+
object: z.infer<TSchema>;
|
|
160
|
+
} | {
|
|
161
|
+
type: 'finish';
|
|
162
|
+
finishReason: FinishReason;
|
|
163
|
+
usage: ChatUsage;
|
|
164
|
+
};
|
|
165
|
+
type StreamObjectResult<TSchema extends z.ZodType> = AsyncIterable<ObjectStreamEvent<TSchema>> & {
|
|
166
|
+
toResponse(): Promise<GenerateObjectResult<TSchema>>;
|
|
167
|
+
};
|
|
137
168
|
type EmbeddingModel = {
|
|
138
169
|
readonly provider: string;
|
|
139
170
|
readonly modelId: string;
|
|
@@ -146,9 +177,14 @@ type EmbedOptions = {
|
|
|
146
177
|
};
|
|
147
178
|
type EmbedResult = {
|
|
148
179
|
embeddings: number[][];
|
|
149
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Optional embedding usage metadata. Some providers/models do not expose
|
|
182
|
+
* token usage for embedding calls.
|
|
183
|
+
*/
|
|
184
|
+
usage?: EmbeddingUsage;
|
|
150
185
|
};
|
|
151
186
|
type EmbeddingUsage = {
|
|
187
|
+
/** Number of tokens consumed by embedding input. */
|
|
152
188
|
inputTokens: number;
|
|
153
189
|
};
|
|
154
190
|
type ImageModel = {
|
|
@@ -180,6 +216,25 @@ declare class ProviderError extends LLMError {
|
|
|
180
216
|
readonly statusCode?: number;
|
|
181
217
|
constructor(message: string, provider: string, statusCode?: number, cause?: unknown);
|
|
182
218
|
}
|
|
219
|
+
type StructuredOutputErrorOptions = {
|
|
220
|
+
statusCode?: number;
|
|
221
|
+
cause?: unknown;
|
|
222
|
+
rawOutput?: string;
|
|
223
|
+
};
|
|
224
|
+
declare class StructuredOutputError extends ProviderError {
|
|
225
|
+
readonly rawOutput?: string;
|
|
226
|
+
constructor(message: string, provider: string, options?: StructuredOutputErrorOptions);
|
|
227
|
+
}
|
|
228
|
+
declare class StructuredOutputNoObjectGeneratedError extends StructuredOutputError {
|
|
229
|
+
constructor(message: string, provider: string, options?: StructuredOutputErrorOptions);
|
|
230
|
+
}
|
|
231
|
+
declare class StructuredOutputParseError extends StructuredOutputError {
|
|
232
|
+
constructor(message: string, provider: string, options?: StructuredOutputErrorOptions);
|
|
233
|
+
}
|
|
234
|
+
declare class StructuredOutputValidationError extends StructuredOutputError {
|
|
235
|
+
readonly issues: string[];
|
|
236
|
+
constructor(message: string, provider: string, issues: string[], options?: StructuredOutputErrorOptions);
|
|
237
|
+
}
|
|
183
238
|
|
|
184
239
|
declare function defineTool(options: ToolDefinition): ToolDefinition;
|
|
185
240
|
|
|
@@ -188,11 +243,22 @@ type GenerateParams = GenerateOptions & {
|
|
|
188
243
|
};
|
|
189
244
|
declare function generate(params: GenerateParams): Promise<GenerateResult>;
|
|
190
245
|
|
|
246
|
+
type GenerateObjectParams<TSchema extends z.ZodType> = GenerateObjectOptions<TSchema> & {
|
|
247
|
+
model: ChatModel;
|
|
248
|
+
};
|
|
249
|
+
declare function generateObject<TSchema extends z.ZodType>(params: GenerateObjectParams<TSchema>): Promise<GenerateObjectResult<TSchema>>;
|
|
250
|
+
|
|
191
251
|
type StreamParams = GenerateOptions & {
|
|
192
252
|
model: ChatModel;
|
|
193
253
|
};
|
|
194
254
|
declare function stream(params: StreamParams): Promise<StreamResult>;
|
|
195
255
|
|
|
256
|
+
type StreamObjectParams<TSchema extends z.ZodType> = StreamObjectOptions<TSchema> & {
|
|
257
|
+
model: ChatModel;
|
|
258
|
+
};
|
|
259
|
+
declare function streamObject<TSchema extends z.ZodType>(params: StreamObjectParams<TSchema>): Promise<StreamObjectResult<TSchema>>;
|
|
260
|
+
declare function createObjectStreamResult<TSchema extends z.ZodType>(source: AsyncIterable<ObjectStreamEvent<TSchema>>): StreamObjectResult<TSchema>;
|
|
261
|
+
|
|
196
262
|
declare function createStreamResult(source: AsyncIterable<StreamEvent>): StreamResult;
|
|
197
263
|
|
|
198
264
|
type EmbedParams = EmbedOptions & {
|
|
@@ -205,4 +271,4 @@ type GenerateImageParams = ImageGenerateOptions & {
|
|
|
205
271
|
};
|
|
206
272
|
declare function generateImage(params: GenerateImageParams): Promise<ImageGenerateResult>;
|
|
207
273
|
|
|
208
|
-
export { type AssistantMessage, type ChatModel, type ChatUsage, type EmbedOptions, type EmbedResult, type EmbeddingModel, type EmbeddingUsage, type FilePart, type FinishReason, type GenerateOptions, type GenerateResult, type GeneratedImage, type ImageGenerateOptions, type ImageGenerateResult, type ImageModel, type ImagePart, LLMError, type Message, type ModelConfig, ProviderError, type StreamEvent, type StreamResult, type SystemMessage, type TextPart, type ToolCall, type ToolChoice, type ToolDefinition, type ToolResultMessage, type ToolSet, type UserContentPart, type UserMessage, createStreamResult, defineTool, embed, generate, generateImage, stream };
|
|
274
|
+
export { type AssistantMessage, type ChatModel, type ChatUsage, type EmbedOptions, type EmbedResult, type EmbeddingModel, type EmbeddingUsage, type FilePart, type FinishReason, type GenerateObjectOptions, type GenerateObjectResult, type GenerateOptions, type GenerateResult, type GeneratedImage, type ImageGenerateOptions, type ImageGenerateResult, type ImageModel, type ImagePart, LLMError, type Message, type ModelConfig, type ObjectStreamEvent, ProviderError, type StreamEvent, type StreamObjectOptions, type StreamObjectResult, type StreamResult, StructuredOutputError, StructuredOutputNoObjectGeneratedError, StructuredOutputParseError, StructuredOutputValidationError, type SystemMessage, type TextPart, type ToolCall, type ToolChoice, type ToolDefinition, type ToolResultMessage, type ToolSet, type UserContentPart, type UserMessage, createObjectStreamResult, createStreamResult, defineTool, embed, generate, generateImage, generateObject, stream, streamObject };
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,34 @@ var ProviderError = class extends LLMError {
|
|
|
17
17
|
this.statusCode = statusCode;
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
+
var StructuredOutputError = class extends ProviderError {
|
|
21
|
+
rawOutput;
|
|
22
|
+
constructor(message, provider, options = {}) {
|
|
23
|
+
super(message, provider, options.statusCode, options.cause);
|
|
24
|
+
this.name = "StructuredOutputError";
|
|
25
|
+
this.rawOutput = options.rawOutput;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var StructuredOutputNoObjectGeneratedError = class extends StructuredOutputError {
|
|
29
|
+
constructor(message, provider, options = {}) {
|
|
30
|
+
super(message, provider, options);
|
|
31
|
+
this.name = "StructuredOutputNoObjectGeneratedError";
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var StructuredOutputParseError = class extends StructuredOutputError {
|
|
35
|
+
constructor(message, provider, options = {}) {
|
|
36
|
+
super(message, provider, options);
|
|
37
|
+
this.name = "StructuredOutputParseError";
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var StructuredOutputValidationError = class extends StructuredOutputError {
|
|
41
|
+
issues;
|
|
42
|
+
constructor(message, provider, issues, options = {}) {
|
|
43
|
+
super(message, provider, options);
|
|
44
|
+
this.name = "StructuredOutputValidationError";
|
|
45
|
+
this.issues = issues;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
20
48
|
|
|
21
49
|
// src/tool.ts
|
|
22
50
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
@@ -33,6 +61,15 @@ async function generate(params) {
|
|
|
33
61
|
return model.generate(options);
|
|
34
62
|
}
|
|
35
63
|
|
|
64
|
+
// src/generate-object.ts
|
|
65
|
+
async function generateObject(params) {
|
|
66
|
+
if (params.messages.length === 0) {
|
|
67
|
+
throw new LLMError("messages must not be empty");
|
|
68
|
+
}
|
|
69
|
+
const { model, ...options } = params;
|
|
70
|
+
return model.generateObject(options);
|
|
71
|
+
}
|
|
72
|
+
|
|
36
73
|
// src/stream-chat.ts
|
|
37
74
|
async function stream(params) {
|
|
38
75
|
if (params.messages.length === 0) {
|
|
@@ -42,6 +79,83 @@ async function stream(params) {
|
|
|
42
79
|
return model.stream(options);
|
|
43
80
|
}
|
|
44
81
|
|
|
82
|
+
// src/stream-object.ts
|
|
83
|
+
async function streamObject(params) {
|
|
84
|
+
if (params.messages.length === 0) {
|
|
85
|
+
throw new LLMError("messages must not be empty");
|
|
86
|
+
}
|
|
87
|
+
const { model, ...options } = params;
|
|
88
|
+
return model.streamObject(options);
|
|
89
|
+
}
|
|
90
|
+
function createObjectStreamResult(source) {
|
|
91
|
+
let resolveResponse;
|
|
92
|
+
let rejectResponse;
|
|
93
|
+
const responsePromise = new Promise(
|
|
94
|
+
(resolve, reject) => {
|
|
95
|
+
resolveResponse = resolve;
|
|
96
|
+
rejectResponse = reject;
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
let iteratorCreated = false;
|
|
100
|
+
async function* iterate() {
|
|
101
|
+
let objectResult;
|
|
102
|
+
let finishReason = "unknown";
|
|
103
|
+
let usage = {
|
|
104
|
+
inputTokens: 0,
|
|
105
|
+
outputTokens: 0,
|
|
106
|
+
reasoningTokens: 0,
|
|
107
|
+
totalTokens: 0
|
|
108
|
+
};
|
|
109
|
+
try {
|
|
110
|
+
for await (const event of source) {
|
|
111
|
+
if (event.type === "object") {
|
|
112
|
+
objectResult = event.object;
|
|
113
|
+
} else if (event.type === "finish") {
|
|
114
|
+
finishReason = event.finishReason;
|
|
115
|
+
usage = event.usage;
|
|
116
|
+
}
|
|
117
|
+
yield event;
|
|
118
|
+
}
|
|
119
|
+
if (objectResult === void 0) {
|
|
120
|
+
throw new LLMError(
|
|
121
|
+
"object stream completed without emitting a final object"
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
resolveResponse?.({
|
|
125
|
+
object: objectResult,
|
|
126
|
+
finishReason,
|
|
127
|
+
usage
|
|
128
|
+
});
|
|
129
|
+
} catch (error) {
|
|
130
|
+
rejectResponse?.(error);
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const generator = iterate();
|
|
135
|
+
return {
|
|
136
|
+
[Symbol.asyncIterator]() {
|
|
137
|
+
if (iteratorCreated) {
|
|
138
|
+
throw new Error("Stream can only be iterated once");
|
|
139
|
+
}
|
|
140
|
+
iteratorCreated = true;
|
|
141
|
+
return generator;
|
|
142
|
+
},
|
|
143
|
+
toResponse() {
|
|
144
|
+
if (!iteratorCreated) {
|
|
145
|
+
iteratorCreated = true;
|
|
146
|
+
(async () => {
|
|
147
|
+
try {
|
|
148
|
+
for await (const _event of generator) {
|
|
149
|
+
}
|
|
150
|
+
} catch {
|
|
151
|
+
}
|
|
152
|
+
})();
|
|
153
|
+
}
|
|
154
|
+
return responsePromise;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
45
159
|
// src/stream.ts
|
|
46
160
|
function createStreamResult(source) {
|
|
47
161
|
let resolveResponse;
|
|
@@ -123,10 +237,17 @@ async function generateImage(params) {
|
|
|
123
237
|
export {
|
|
124
238
|
LLMError,
|
|
125
239
|
ProviderError,
|
|
240
|
+
StructuredOutputError,
|
|
241
|
+
StructuredOutputNoObjectGeneratedError,
|
|
242
|
+
StructuredOutputParseError,
|
|
243
|
+
StructuredOutputValidationError,
|
|
244
|
+
createObjectStreamResult,
|
|
126
245
|
createStreamResult,
|
|
127
246
|
defineTool,
|
|
128
247
|
embed,
|
|
129
248
|
generate,
|
|
130
249
|
generateImage,
|
|
131
|
-
|
|
250
|
+
generateObject,
|
|
251
|
+
stream,
|
|
252
|
+
streamObject
|
|
132
253
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@core-ai/core-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Type-safe LLM abstraction layer over native provider SDKs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Omnifact (https://omnifact.ai)",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"test:watch": "vitest"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"zod": "^3.25.
|
|
48
|
-
"zod-to-json-schema": "^3.
|
|
47
|
+
"zod": "^3.25.0 || ^4.0.0",
|
|
48
|
+
"zod-to-json-schema": "^3.25.1"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@core-ai/eslint-config": "^0.0.0",
|