Socket
Socket
Sign inDemoInstall

ai

Package Overview
Dependencies
Maintainers
11
Versions
246
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ai - npm Package Compare versions

Comparing version 2.1.3 to 2.1.6

57

./dist/index.js

@@ -76,24 +76,24 @@ "use strict";

function createEventStreamTransformer(customParser) {
const decoder = new TextDecoder();
let parser;
const textDecoder = new TextDecoder();
let eventSourceParser;
return new TransformStream({
start(controller) {
return __async(this, null, function* () {
function onParse(event) {
if (event.type === "event") {
const data = event.data;
if (data === "[DONE]") {
eventSourceParser = (0, import_eventsource_parser.createParser)(
(event) => {
if ("data" in event && event.type === "event" && event.data === "[DONE]") {
controller.terminate();
return;
}
const message = customParser(data);
if (message)
controller.enqueue(message);
if ("data" in event) {
const parsedMessage = customParser(event.data);
if (parsedMessage)
controller.enqueue(parsedMessage);
}
}
}
parser = (0, import_eventsource_parser.createParser)(onParse);
);
});
},
transform(chunk) {
parser.feed(decoder.decode(chunk));
eventSourceParser.feed(textDecoder.decode(chunk));
}

@@ -103,4 +103,4 @@ });

function createCallbacksTransformer(callbacks) {
const encoder = new TextEncoder();
let fullResponse = "";
const textEncoder = new TextEncoder();
let aggregatedResponse = "";
const { onStart, onToken, onCompletion } = callbacks || {};

@@ -116,7 +116,7 @@ return new TransformStream({

return __async(this, null, function* () {
controller.enqueue(encoder.encode(message));
controller.enqueue(textEncoder.encode(message));
if (onToken)
yield onToken(message);
if (onCompletion)
fullResponse += message;
aggregatedResponse += message;
});

@@ -126,3 +126,4 @@ },

return __async(this, null, function* () {
yield onCompletion == null ? void 0 : onCompletion(fullResponse);
if (onCompletion)
yield onCompletion(aggregatedResponse);
});

@@ -133,18 +134,23 @@ }

function trimStartOfStreamHelper() {
let start = true;
let isStreamStart = true;
return (text) => {
if (start)
if (isStreamStart) {
text = text.trimStart();
if (text)
start = false;
if (text)
isStreamStart = false;
}
return text;
};
}
function AIStream(res, customParser, callbacks) {
if (!res.ok) {
function AIStream(response, customParser, callbacks) {
if (!response.ok) {
throw new Error(
`Failed to convert the response to stream. Received status code: ${res.status}.`
`Failed to convert the response to stream. Received status code: ${response.status}.`
);
}
const stream = res.body || new ReadableStream({
const responseBodyStream = response.body || createEmptyReadableStream();
return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
}
function createEmptyReadableStream() {
return new ReadableStream({
start(controller) {

@@ -154,3 +160,2 @@ controller.close();

});
return stream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
}

@@ -157,0 +162,0 @@

import { ServerResponse } from 'node:http';
/**
* Helper callback methods for AIStream stream lifecycle events
* @interface
*/
interface AIStreamCallbacks {

@@ -8,13 +12,63 @@ onStart?: () => Promise<void>;

}
/**
* Custom parser for AIStream data.
* @interface
*/
interface AIStreamParser {
(data: string): string | void;
}
/**
* Creates a TransformStream that parses events from an EventSource stream using a custom parser.
* @param {AIStreamParser} customParser - Function to handle event data.
* @returns {TransformStream<Uint8Array, string>} TransformStream parsing events.
*/
declare function createEventStreamTransformer(customParser: AIStreamParser): TransformStream<Uint8Array, string>;
/**
* This stream forks input stream, allowing us to use the result as a
* bytestream of the messages and pass the messages to our callback interface.
* Creates a transform stream that encodes input messages and invokes optional callback functions.
* The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle.
* - `onStart`: Called once when the stream is initialized.
* - `onToken`: Called for each tokenized message.
* - `onCompletion`: Called once when the stream is flushed, with the aggregated messages.
*
* This function is useful when you want to process a stream of messages and perform specific actions during the stream's lifecycle.
*
* @param {AIStreamCallbacks} [callbacks] - An object containing the callback functions.
* @return {TransformStream<string, Uint8Array>} A transform stream that encodes input messages as Uint8Array and allows the execution of custom logic through callbacks.
*
* @example
* const callbacks = {
* onStart: async () => console.log('Stream started'),
* onToken: async (token) => console.log(`Token: ${token}`),
* onCompletion: async (completion) => console.log(`Completion: ${completion}`)
* };
* const transformer = createCallbacksTransformer(callbacks);
*/
declare function createCallbacksTransformer(callbacks: AIStreamCallbacks | undefined): TransformStream<string, Uint8Array>;
/**
* Returns a stateful function that, when invoked, trims leading whitespace
* from the input text. The trimming only occurs on the first invocation, ensuring that
* subsequent calls do not alter the input text. This is particularly useful in scenarios
* where a text stream is being processed and only the initial whitespace should be removed.
*
* @return {function(string): string} A function that takes a string as input and returns a string
* with leading whitespace removed if it is the first invocation; otherwise, it returns the input unchanged.
*
* @example
* const trimStart = trimStartOfStreamHelper();
* const output1 = trimStart(" text"); // "text"
* const output2 = trimStart(" text"); // " text"
*
*/
declare function trimStartOfStreamHelper(): (text: string) => string;
declare function AIStream(res: Response, customParser: AIStreamParser, callbacks?: AIStreamCallbacks): ReadableStream;
/**
* Returns a ReadableStream created from the response, parsed and handled with custom logic.
* The stream goes through two transformation stages, first parsing the events and then
* invoking the provided callbacks.
* @param {Response} response - The response.
* @param {AIStreamParser} customParser - The custom parser function.
* @param {AIStreamCallbacks} callbacks - The callbacks.
* @return {ReadableStream} The AIStream.
* @throws Will throw an error if the response is not OK.
*/
declare function AIStream(response: Response, customParser: AIStreamParser, callbacks?: AIStreamCallbacks): ReadableStream;

@@ -53,3 +107,3 @@ declare function OpenAIStream(res: Response, cb?: AIStreamCallbacks): ReadableStream;

*/
type Message = {
declare type Message = {
id: string;

@@ -60,3 +114,3 @@ createdAt?: Date;

};
type CreateMessage = {
declare type CreateMessage = {
id?: string;

@@ -67,3 +121,3 @@ createdAt?: Date;

};
type UseChatOptions = {
declare type UseChatOptions = {
/**

@@ -124,3 +178,3 @@ * The API endpoint that accepts a `{ messages: Message[] }` object and returns

};
type UseCompletionOptions = {
declare type UseCompletionOptions = {
/**

@@ -127,0 +181,0 @@ * The API endpoint that accepts a `{ prompt: string }` object and returns

@@ -76,24 +76,24 @@ "use strict";

function createEventStreamTransformer(customParser) {
const decoder = new TextDecoder();
let parser;
const textDecoder = new TextDecoder();
let eventSourceParser;
return new TransformStream({
start(controller) {
return __async(this, null, function* () {
function onParse(event) {
if (event.type === "event") {
const data = event.data;
if (data === "[DONE]") {
eventSourceParser = (0, import_eventsource_parser.createParser)(
(event) => {
if ("data" in event && event.type === "event" && event.data === "[DONE]") {
controller.terminate();
return;
}
const message = customParser(data);
if (message)
controller.enqueue(message);
if ("data" in event) {
const parsedMessage = customParser(event.data);
if (parsedMessage)
controller.enqueue(parsedMessage);
}
}
}
parser = (0, import_eventsource_parser.createParser)(onParse);
);
});
},
transform(chunk) {
parser.feed(decoder.decode(chunk));
eventSourceParser.feed(textDecoder.decode(chunk));
}

@@ -103,4 +103,4 @@ });

function createCallbacksTransformer(callbacks) {
const encoder = new TextEncoder();
let fullResponse = "";
const textEncoder = new TextEncoder();
let aggregatedResponse = "";
const { onStart, onToken, onCompletion } = callbacks || {};

@@ -116,7 +116,7 @@ return new TransformStream({

return __async(this, null, function* () {
controller.enqueue(encoder.encode(message));
controller.enqueue(textEncoder.encode(message));
if (onToken)
yield onToken(message);
if (onCompletion)
fullResponse += message;
aggregatedResponse += message;
});

@@ -126,3 +126,4 @@ },

return __async(this, null, function* () {
yield onCompletion == null ? void 0 : onCompletion(fullResponse);
if (onCompletion)
yield onCompletion(aggregatedResponse);
});

@@ -133,18 +134,23 @@ }

function trimStartOfStreamHelper() {
let start = true;
let isStreamStart = true;
return (text) => {
if (start)
if (isStreamStart) {
text = text.trimStart();
if (text)
start = false;
if (text)
isStreamStart = false;
}
return text;
};
}
function AIStream(res, customParser, callbacks) {
if (!res.ok) {
function AIStream(response, customParser, callbacks) {
if (!response.ok) {
throw new Error(
`Failed to convert the response to stream. Received status code: ${res.status}.`
`Failed to convert the response to stream. Received status code: ${response.status}.`
);
}
const stream = res.body || new ReadableStream({
const responseBodyStream = response.body || createEmptyReadableStream();
return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
}
function createEmptyReadableStream() {
return new ReadableStream({
start(controller) {

@@ -154,3 +160,2 @@ controller.close();

});
return stream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
}

@@ -157,0 +162,0 @@

{
"name": "ai",
"version": "2.1.3",
"version": "2.1.6",
"license": "Apache-2.0",

@@ -64,4 +64,4 @@ "sideEffects": false,

"typescript": "^4.5.3",
"eslint-config-vercel-ai": "0.0.0",
"@vercel/ai-tsconfig": "0.0.0"
"@vercel/ai-tsconfig": "0.0.0",
"eslint-config-vercel-ai": "0.0.0"
},

@@ -68,0 +68,0 @@ "peerDependencies": {

/**
* Shared types between the API and UI packages.
*/
type Message = {
declare type Message = {
id: string;

@@ -10,3 +10,3 @@ createdAt?: Date;

};
type CreateMessage = {
declare type CreateMessage = {
id?: string;

@@ -17,3 +17,3 @@ createdAt?: Date;

};
type UseChatOptions = {
declare type UseChatOptions = {
/**

@@ -74,3 +74,3 @@ * The API endpoint that accepts a `{ messages: Message[] }` object and returns

};
type UseCompletionOptions = {
declare type UseCompletionOptions = {
/**

@@ -126,3 +126,3 @@ * The API endpoint that accepts a `{ prompt: string }` object and returns

type UseChatHelpers = {
declare type UseChatHelpers = {
/** Current messages in the chat */

@@ -158,3 +158,3 @@ messages: Message[];

/** An input/textarea-ready onChange handler to control the value of the input */
handleInputChange: (e: any) => void;
handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
/** Form submission handler to automattically reset input and append a user message */

@@ -167,3 +167,3 @@ handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;

type UseCompletionHelpers = {
declare type UseCompletionHelpers = {
/** The current completion result */

@@ -196,3 +196,3 @@ completion: string;

*/
handleInputChange: (e: any) => void;
handleInputChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement>) => void;
/**

@@ -199,0 +199,0 @@ * Form submission handler to automattically reset input and append a user message

@@ -84,5 +84,9 @@ 'use client'

);
var decoder = new TextDecoder();
function decodeAIStreamChunk(chunk) {
return decoder.decode(chunk);
function createChunkDecoder() {
const decoder = new TextDecoder();
return function(chunk) {
if (!chunk)
return "";
return decoder.decode(chunk, { stream: true });
};
}

@@ -166,2 +170,3 @@

const reader = res.body.getReader();
const decode = createChunkDecoder();
while (true) {

@@ -172,3 +177,3 @@ const { done, value } = yield reader.read();

}
result += decodeAIStreamChunk(value);
result += decode(value);
mutate(

@@ -347,2 +352,3 @@ [

const reader = res.body.getReader();
const decoder = createChunkDecoder();
while (true) {

@@ -353,3 +359,3 @@ const { done, value } = yield reader.read();

}
result += decodeAIStreamChunk(value);
result += decoder(value);
mutate(result, false);

@@ -356,0 +362,0 @@ if (abortController2 === null) {

@@ -18,2 +18,4 @@ # Vercel AI SDK

View the full documentation and examples on [sdk.vercel.ai/docs](https://sdk.vercel.ai/docs)
## Example: An AI Chatbot with Next.js and OpenAI

@@ -78,3 +80,3 @@

View the full documentation and examples on [play.vercel.ai/docs](https://play.vercel.ai/docs)
View the full documentation and examples on [sdk.vercel.ai/docs](https://sdk.vercel.ai/docs)

@@ -81,0 +83,0 @@ ## Authors

@@ -6,3 +6,3 @@ import { Readable, Writable } from 'svelte/store';

*/
type Message = {
declare type Message = {
id: string;

@@ -13,3 +13,3 @@ createdAt?: Date;

};
type CreateMessage = {
declare type CreateMessage = {
id?: string;

@@ -20,3 +20,3 @@ createdAt?: Date;

};
type UseChatOptions = {
declare type UseChatOptions = {
/**

@@ -77,3 +77,3 @@ * The API endpoint that accepts a `{ messages: Message[] }` object and returns

};
type UseCompletionOptions = {
declare type UseCompletionOptions = {
/**

@@ -129,3 +129,3 @@ * The API endpoint that accepts a `{ prompt: string }` object and returns

type UseChatHelpers = {
declare type UseChatHelpers = {
/** Current messages in the chat */

@@ -165,3 +165,3 @@ messages: Readable<Message[]>;

type UseCompletionHelpers = {
declare type UseCompletionHelpers = {
/** The current completion result */

@@ -168,0 +168,0 @@ completion: Readable<string>;

@@ -398,3 +398,3 @@ "use strict";

// ../../node_modules/.pnpm/sswr@1.10.0_svelte@3.59.1/node_modules/sswr/dist/sswr.mjs
// ../../node_modules/.pnpm/sswr@1.10.0_svelte@3.54.0/node_modules/sswr/dist/sswr.mjs
var import_svelte = require("svelte");

@@ -510,5 +510,9 @@ function h() {

);
var decoder = new TextDecoder();
function decodeAIStreamChunk(chunk) {
return decoder.decode(chunk);
function createChunkDecoder() {
const decoder = new TextDecoder();
return function(chunk) {
if (!chunk)
return "";
return decoder.decode(chunk, { stream: true });
};
}

@@ -587,2 +591,3 @@

const reader = res.body.getReader();
const decoder = createChunkDecoder();
while (true) {

@@ -593,3 +598,3 @@ const { done, value } = yield reader.read();

}
result += decodeAIStreamChunk(value);
result += decoder(value);
mutate([

@@ -747,2 +752,3 @@ ...messagesSnapshot,

const reader = res.body.getReader();
const decoder = createChunkDecoder();
while (true) {

@@ -753,3 +759,3 @@ const { done, value } = yield reader.read();

}
result += decodeAIStreamChunk(value);
result += decoder(value);
mutate(result);

@@ -756,0 +762,0 @@ if (abortController === null) {

@@ -6,3 +6,3 @@ import { Ref } from 'vue';

*/
type Message = {
declare type Message = {
id: string;

@@ -13,3 +13,3 @@ createdAt?: Date;

};
type CreateMessage = {
declare type CreateMessage = {
id?: string;

@@ -20,3 +20,3 @@ createdAt?: Date;

};
type UseChatOptions = {
declare type UseChatOptions = {
/**

@@ -77,3 +77,3 @@ * The API endpoint that accepts a `{ messages: Message[] }` object and returns

};
type UseCompletionOptions = {
declare type UseCompletionOptions = {
/**

@@ -129,3 +129,3 @@ * The API endpoint that accepts a `{ prompt: string }` object and returns

type UseChatHelpers = {
declare type UseChatHelpers = {
/** Current messages in the chat */

@@ -165,3 +165,3 @@ messages: Ref<Message[]>;

type UseCompletionHelpers = {
declare type UseCompletionHelpers = {
/** The current completion result */

@@ -168,0 +168,0 @@ completion: Ref<string>;

@@ -82,5 +82,9 @@ "use strict";

);
var decoder = new TextDecoder();
function decodeAIStreamChunk(chunk) {
return decoder.decode(chunk);
function createChunkDecoder() {
const decoder = new TextDecoder();
return function(chunk) {
if (!chunk)
return "";
return decoder.decode(chunk, { stream: true });
};
}

@@ -160,2 +164,3 @@

const reader = res.body.getReader();
const decoder = createChunkDecoder();
while (true) {

@@ -166,3 +171,3 @@ const { done, value } = yield reader.read();

}
result += decodeAIStreamChunk(value);
result += decoder(value);
mutate([

@@ -321,2 +326,3 @@ ...messagesSnapshot,

const reader = res.body.getReader();
const decoder = createChunkDecoder();
while (true) {

@@ -327,3 +333,3 @@ const { done, value } = yield reader.read();

}
result += decodeAIStreamChunk(value);
result += decoder(value);
mutate(result);

@@ -330,0 +336,0 @@ if (abortController === null) {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc