@core-ai/core-ai 0.6.0 → 0.6.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.
Files changed (2) hide show
  1. package/dist/index.js +83 -42
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -75,38 +75,54 @@ function assistantMessage(content) {
75
75
  }
76
76
 
77
77
  // src/assertions.ts
78
+ function isEmptyText(value) {
79
+ return value.length === 0;
80
+ }
78
81
  function assertNonEmptyMessages(messages) {
79
82
  if (messages.length === 0) {
80
83
  throw new LLMError("messages must not be empty");
81
84
  }
82
85
  }
83
86
  function assertNonEmptyEmbedInput(input) {
84
- if (typeof input === "string" && input.length === 0) {
87
+ const isEmptyString = typeof input === "string" && isEmptyText(input);
88
+ const isEmptyArray = Array.isArray(input) && input.length === 0;
89
+ if (isEmptyString || isEmptyArray) {
85
90
  throw new LLMError("input must not be empty");
86
91
  }
87
- if (Array.isArray(input) && input.length === 0) {
88
- throw new LLMError("input must not be empty");
92
+ }
93
+ function assertNonEmptyPrompt(prompt) {
94
+ if (isEmptyText(prompt)) {
95
+ throw new LLMError("prompt must not be empty");
89
96
  }
90
97
  }
91
98
 
99
+ // src/model-options.ts
100
+ function splitModelFromParams(params) {
101
+ const { model, ...options } = params;
102
+ return {
103
+ model,
104
+ options
105
+ };
106
+ }
107
+
92
108
  // src/generate.ts
93
109
  async function generate(params) {
94
110
  assertNonEmptyMessages(params.messages);
95
- const { model, ...options } = params;
111
+ const { model, options } = splitModelFromParams(params);
96
112
  return model.generate(options);
97
113
  }
98
114
 
99
115
  // src/generate-object.ts
100
116
  async function generateObject(params) {
101
117
  assertNonEmptyMessages(params.messages);
102
- const { model, ...options } = params;
118
+ const { model, options } = splitModelFromParams(params);
103
119
  return model.generateObject(options);
104
120
  }
105
121
 
106
122
  // src/stream-chat.ts
107
123
  async function stream(params) {
108
124
  assertNonEmptyMessages(params.messages);
109
- const { model, ...options } = params;
125
+ const { model, options } = splitModelFromParams(params);
110
126
  return model.stream(options);
111
127
  }
112
128
 
@@ -370,41 +386,68 @@ function createChatStream(source, options = {}) {
370
386
  reasoningBuffer = "";
371
387
  reasoningProviderMetadata = void 0;
372
388
  };
389
+ const startReasoning = () => {
390
+ flushText();
391
+ flushReasoning();
392
+ insideReasoning = true;
393
+ };
394
+ const appendReasoning = (text) => {
395
+ if (!insideReasoning) {
396
+ flushText();
397
+ insideReasoning = true;
398
+ }
399
+ reasoningBuffer += text;
400
+ };
401
+ const endReasoning = (providerMetadata) => {
402
+ reasoningProviderMetadata = providerMetadata;
403
+ flushReasoning();
404
+ insideReasoning = false;
405
+ };
406
+ const appendText = (text) => {
407
+ if (insideReasoning) {
408
+ flushReasoning();
409
+ insideReasoning = false;
410
+ }
411
+ textBuffer += text;
412
+ };
413
+ const appendToolCall = (toolCall) => {
414
+ flushText();
415
+ flushReasoning();
416
+ insideReasoning = false;
417
+ parts.push({
418
+ type: "tool-call",
419
+ toolCall
420
+ });
421
+ };
422
+ const setFinish = (event) => {
423
+ finishReason = event.finishReason;
424
+ usage = event.usage;
425
+ };
373
426
  return createStream({
374
427
  source: resolvedSource,
375
428
  signal,
376
429
  reduceEvent(event) {
377
- if (event.type === "reasoning-start") {
378
- flushText();
379
- flushReasoning();
380
- insideReasoning = true;
381
- } else if (event.type === "reasoning-delta") {
382
- if (!insideReasoning) {
383
- flushText();
384
- insideReasoning = true;
385
- }
386
- reasoningBuffer += event.text;
387
- } else if (event.type === "reasoning-end") {
388
- reasoningProviderMetadata = event.providerMetadata;
389
- flushReasoning();
390
- insideReasoning = false;
391
- } else if (event.type === "text-delta") {
392
- if (insideReasoning) {
393
- flushReasoning();
394
- insideReasoning = false;
395
- }
396
- textBuffer += event.text;
397
- } else if (event.type === "tool-call-end") {
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;
430
+ switch (event.type) {
431
+ case "reasoning-start":
432
+ startReasoning();
433
+ break;
434
+ case "reasoning-delta":
435
+ appendReasoning(event.text);
436
+ break;
437
+ case "reasoning-end":
438
+ endReasoning(event.providerMetadata);
439
+ break;
440
+ case "text-delta":
441
+ appendText(event.text);
442
+ break;
443
+ case "tool-call-end":
444
+ appendToolCall(event.toolCall);
445
+ break;
446
+ case "finish":
447
+ setFinish(event);
448
+ break;
449
+ default:
450
+ break;
408
451
  }
409
452
  },
410
453
  finalizeResult() {
@@ -437,16 +480,14 @@ function getProviderMetadata(providerMetadata, provider) {
437
480
  // src/embed.ts
438
481
  async function embed(params) {
439
482
  assertNonEmptyEmbedInput(params.input);
440
- const { model, ...options } = params;
483
+ const { model, options } = splitModelFromParams(params);
441
484
  return model.embed(options);
442
485
  }
443
486
 
444
487
  // src/generate-image.ts
445
488
  async function generateImage(params) {
446
- if (params.prompt.length === 0) {
447
- throw new LLMError("prompt must not be empty");
448
- }
449
- const { model, ...options } = params;
489
+ assertNonEmptyPrompt(params.prompt);
490
+ const { model, options } = splitModelFromParams(params);
450
491
  return model.generate(options);
451
492
  }
452
493
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@core-ai/core-ai",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Type-safe LLM abstraction layer over native provider SDKs",
5
5
  "license": "MIT",
6
6
  "author": "Omnifact (https://omnifact.ai)",