Skip to content

The Open Standard for Generative UI

Update SSE format in stream pkg (#83)

abhithesys839ac10f7b95
diff --git a/js/packages/react-core/package.json b/js/packages/react-core/package.json
index 0e8e6a1a7..1a8eb8741 100644
--- a/js/packages/react-core/package.json
+++ b/js/packages/react-core/package.json
@@ -1,6 +1,6 @@
{
"name": "@crayonai/react-core",
- "version": "0.5.3",
+ "version": "0.6.0",
"description": "Generative UI SDK",
"license": "MIT",
"main": "dist/index.js",
@@ -25,7 +25,9 @@
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7",
"tiny-invariant": "^1.3.3",
- "zustand": "^5.0.2"
+ "zustand": "^5.0.2",
+ "eventsource-parser": "^3.0.0",
+ "@crayonai/stream": "workspace:*"
},
"devDependencies": {
"@types/react": ">=18.0.0",
diff --git a/js/packages/react-core/src/index.ts b/js/packages/react-core/src/index.ts
index 13d136c49..b47ce14a6 100644
--- a/js/packages/react-core/src/index.ts
+++ b/js/packages/react-core/src/index.ts
@@ -6,8 +6,8 @@ export { useThreadListActions } from "./hooks/useThreadListActions";
export { useThreadListState } from "./hooks/useThreadListState";
export { useThreadManagerSelector } from "./hooks/useThreadManagerSelector";
export { useThreadState } from "./hooks/useThreadState";
+export { processStreamedMessage } from "./stream/processStreamedMessage";
export { useThreadListManager } from "./useThreadListManager";
export { useThreadManager } from "./useThreadManager";
-export { processStreamedMessage } from "./utils/processStreamedMessage";
export * from "./types";
diff --git a/js/packages/react-core/src/stream/processStreamedMessage.ts b/js/packages/react-core/src/stream/processStreamedMessage.ts
new file mode 100644
index 000000000..ba7e749ff
--- /dev/null
+++ b/js/packages/react-core/src/stream/processStreamedMessage.ts
@@ -0,0 +1,136 @@
+import {
+ ContextUpdate,
+ MessageUpdate,
+ ResponseTemplate,
+ ResponseTemplatePropsChunk,
+ SSEType,
+ TextChunk,
+} from "@crayonai/stream";
+import { createParser } from "eventsource-parser";
+import invariant from "tiny-invariant";
+import { AssistantMessage } from "../types";
+
+export const processStreamedMessage = async ({
+ response,
+ createMessage,
+ updateMessage,
+ deleteMessage,
+}: {
+ response: Response;
+ createMessage: (message: AssistantMessage) => void;
+ updateMessage: (message: AssistantMessage) => void;
+ deleteMessage: (messageId: string) => void;
+}) => {
+ const stream = response.body?.getReader();
+ if (!stream) {
+ throw new Error("No stream");
+ }
+
+ let isMessagePushed = false;
+ let messageId: string = crypto.randomUUID();
+ let previousMessageId = messageId;
+
+ const onMessageUpdate = (message: AssistantMessage) => {
+ if (messageId !== message.id) {
+ previousMessageId = messageId;
+ messageId = message.id;
+ deleteMessage(previousMessageId);
+ createMessage(message);
+ return;
+ }
+
+ if (!isMessagePushed) {
+ createMessage(message);
+ } else {
+ updateMessage(message);
+ }
+ isMessagePushed = true;
+ };
+
+ let messageContent: NonNullable<AssistantMessage["message"]> = [];
+ let messageContext: NonNullable<AssistantMessage["context"]> = [];
+
+ const parser = createParser({
+ onEvent: (event) => {
+ const lastMessageContent = messageContent[messageContent.length - 1];
+ const isLastMessageContentString = lastMessageContent?.type === "text";
+
+ switch (event.event) {
+ case SSEType.TextDelta: {
+ const textChunk = TextChunk.fromSSEData(event.data);
+ if (isLastMessageContentString) {
+ messageContent.pop();
+ messageContent = messageContent.concat({
+ type: "text",
+ text: lastMessageContent.text + textChunk.chunk,
+ });
+ } else {
+ messageContent = messageContent.concat({
+ type: "text",
+ text: textChunk.chunk,
+ });
+ }
+ break;
+ }
+ case SSEType.ResponseTemplate: {
+ const responseTemplate = ResponseTemplate.fromSSEData(event.data);
+ messageContent = messageContent.concat({
+ type: "template",
+ name: responseTemplate.name,
+ templateProps: responseTemplate.templateProps,
+ });
+ break;
+ }
+ case SSEType.ResponseTemplatePropsChunk: {
+ const responseTemplateProps = ResponseTemplatePropsChunk.fromSSEData(event.data);
+ invariant(lastMessageContent?.type === "template", "response template expected");
+ messageContent.pop();
+ messageContent = messageContent.concat({
+ type: "template",
+ name: lastMessageContent.name,
+ templateProps: {
+ ...(lastMessageContent.templateProps || {}),
+ content:
+ (lastMessageContent.templateProps?.content || "") + responseTemplateProps.chunk,
+ },
+ });
+ break;
+ }
+ case SSEType.ContextAppend: {
+ const ctxUpdate = ContextUpdate.fromSSEData(event.data);
+ messageContext = messageContext.concat(ctxUpdate.contextItem);
+ break;
+ }
+ case SSEType.MessageUpdate: {
+ const messageUpdate = MessageUpdate.fromSSEData(event.data);
+ messageId = messageUpdate.id;
+ break;
+ }
+ default: {
+ console.warn(`Unknown event type: ${event.event}`);
+ break;
+ }
+ }
+
+ onMessageUpdate({
+ id: messageId,
+ role: "assistant",
+ message: messageContent,
+ context: messageContext,
+ });
+ },
+ });
+
+ const decoder = new TextDecoder();
+
+ while (true) {
+ const { done, value } = await stream.read();
+
+ parser.feed(decoder.decode(value, { stream: !done }));
+
+ if (done) {
+ parser.reset({ consume: true });
+ break;
+ }
+ }
+};
diff --git a/js/packages/react-core/src/types/message.ts b/js/packages/react-core/src/types/message.ts
index 03a3f494c..c9143797d 100644
--- a/js/packages/react-core/src/types/message.ts
+++ b/js/packages/react-core/src/types/message.ts
@@ -1,12 +1,4 @@
-type JSONValue =
- | string
- | number
- | boolean
- | {
- [value: string]: JSONValue;
- }
- | JSONValue[]
- | null;
+import { JSONValue } from "@crayonai/stream";
type common = {
id: string;
diff --git a/js/packages/react-core/src/utils/processStreamedMessage.ts b/js/packages/react-core/src/utils/processStreamedMessage.ts
deleted file mode 100644
index 419def1d0..000000000
--- a/js/packages/react-core/src/utils/processStreamedMessage.ts
+++ /dev/null
@@ -1,119 +0,0 @@
-import { AssistantMessage } from "../types";
-
-export const processStreamedMessage = async ({
- response,
- createMessage,
- updateMessage,
- deleteMessage,
-}: {
- response: Response;
- createMessage: (message: AssistantMessage) => void;
- updateMessage: (message: AssistantMessage) => void;
- deleteMessage: (messageId: string) => void;
-}) => {
- const stream = response.body?.getReader();
- if (!stream) {
- throw new Error("No stream");
- }
-
- let isMessagePushed = false;
- let messageId: string = crypto.randomUUID();
- let previousMessageId = messageId;
-
- const onMessageUpdate = (message: AssistantMessage) => {
- if (messageId !== message.id) {
- previousMessageId = messageId;
- messageId = message.id;
- deleteMessage(previousMessageId);
- createMessage(message);
- return;
- }
-
- if (!isMessagePushed) {
- createMessage(message);
- } else {
- updateMessage(message);
- }
- isMessagePushed = true;
- };
-
- const chunks: string[] = [];
- const decoder = new TextDecoder();
- while (true) {
- const { done, value } = await stream.read();
-
- if (!value?.length && !done) {
- continue;
- }
-
- if (done) {
- break;
- }
-
- chunks.push(decoder.decode(value));
-
- const concatenatedChunks = chunks.join("");
- const completedMessageParts = concatenatedChunks.substring(
- 0,
- concatenatedChunks.lastIndexOf("\n"),
- );
-
- const messageContent: AssistantMessage["message"] = [];
- const messageContext: AssistantMessage["context"] = [];
-
- for (let chunk of completedMessageParts.split("\n")) {
- chunk = chunk.replaceAll('\\"', '"').replaceAll("\\n", "\n");
-
- if (!chunk) {
- continue;
- }
-
- const firstSeparatorIndex = chunk.indexOf(":");
- if (firstSeparatorIndex === -1) {
- throw new Error("Invalid format");
- }
-
- const chunkType = chunk.substring(0, firstSeparatorIndex);
- const content = chunk.substring(firstSeparatorIndex + 1);
- const lastMessageContent = messageContent[messageContent.length - 1];
- const isLastMessageContentString = lastMessageContent?.type === "text";
- const messageContentLength = messageContent.length;
-
- switch (chunkType) {
- case "0": {
- if (isLastMessageContentString) {
- messageContent[messageContentLength - 1] = {
- type: "text",
- text: lastMessageContent.text + content,
- };
- } else {
- messageContent.push({
- type: "text",
- text: content,
- });
- }
- break;
- }
- case "1": {
- messageContent.push(JSON.parse(content));
- break;
- }
- case "8": {
- messageContext.push(JSON.parse(content));
- break;
- }
- case "9": {
- messageId = content;
- break;
- }
- }
-
- onMessageUpdate({
- id: messageId,
- role: "assistant",
- message: messageContent,
- context: messageContext,
- });
- }
- }
-};
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index c553da837..87b6a7b3a 100644
--- a/js/packages/react-ui/package.json
+++ b/js/packages/react-ui/package.json
@@ -2,7 +2,7 @@
"type": "module",
"name": "@crayonai/react-ui",
"license": "MIT",
- "version": "0.5.16",
+ "version": "0.6.0",
"description": "Component library for Generative UI SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@@ -48,6 +48,7 @@
},
"peerDependencies": {
"@crayonai/react-core": "workspace:*",
+ "@crayonai/stream": "workspace:*",
"react": ">=18.0.0",
"react-dom": ">=18.0.0",
"tailwind-merge": "^2.5.4",
diff --git a/js/packages/react-ui/src/components/CrayonChat/CrayonChat.tsx b/js/packages/react-ui/src/components/CrayonChat/CrayonChat.tsx
index e048bcd77..aca4583a1 100644
--- a/js/packages/react-ui/src/components/CrayonChat/CrayonChat.tsx
+++ b/js/packages/react-ui/src/components/CrayonChat/CrayonChat.tsx
@@ -2,6 +2,7 @@ import {
ChatProvider,
CreateMessage,
Message,
+ processStreamedMessage,
ResponseTemplate,
Thread,
ThreadListManager,
@@ -10,7 +11,6 @@ import {
useThreadListManager,
useThreadManager,
} from "@crayonai/react-core";
-import { processStreamedMessage } from "@crayonai/react-core/dist/utils/processStreamedMessage";
import { useEffect, useRef } from "react";
import invariant from "tiny-invariant";
import {
@@ -107,7 +107,7 @@ export const CrayonChat = ({
response,
createMessage: threadManager.appendMessages,
updateMessage: threadManager.updateMessage,
- deleteMessage: (messageId) => {
+ deleteMessage: (messageId: string) => {
const newMessages = threadManager.messages.filter((message) => message.id !== messageId);
threadManager.setMessages(newMessages);
},
diff --git a/js/packages/stream/package.json b/js/packages/stream/package.json
index 78747e599..195e6667f 100644
--- a/js/packages/stream/package.json
+++ b/js/packages/stream/package.json
@@ -1,6 +1,6 @@
{
"name": "@crayonai/stream",
- "version": "0.5.1",
+ "version": "0.6.0",
"description": "Generative UI SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@@ -32,11 +32,12 @@
"author": "",
"license": "ISC",
"peerDependencies": {
- "@crayonai/react-core": "workspace:*",
"zod": "^3.24.1",
"zod-to-json-schema": "^3.24.3"
},
"dependencies": {
- "best-effort-json-parser": "^1.1.2"
+ "best-effort-json-parser": "^1.1.2",
+ "eventsource-encoder": "^1.0.1",
+ "tiny-invariant": "^1.3.3"
}
}
diff --git a/js/packages/stream/src/openai.ts b/js/packages/stream/src/openai.ts
index be86fdbca..b4627225b 100644
--- a/js/packages/stream/src/openai.ts
+++ b/js/packages/stream/src/openai.ts
@@ -1,5 +1,5 @@
-import type { Message } from "@crayonai/react-core";
import { CrayonDataStreamTransformer, TransformerOpts } from "./transformer";
+
// These types are defined here so as to not introduce a dependency
// on openai library directly.
interface ChatCompletionChunk {
@@ -38,23 +38,21 @@ export const fromOpenAICompletion = (
return readableStream.pipeThrough(new CrayonDataStreamTransformer(opts));
};
-export const toOpenAIMessages = (messages: Message[]) => {
+// This is a utility function to convert a Crayon message to an OpenAI message.
+// Use this only when you are passing the Crayon messages directly to your backend
+// and not converting them.
+// Note: This type is specified as any so as to not introduce a dependency on the
+// Crayon frontendmessage type.
+export const toOpenAIMessages = (messages: any[]) => {
const openAIMessages: OpenAIMessage[] = [];
for (const message of messages) {
if (!message.message) {
continue;
}
- if (typeof message.message === "string") {
- openAIMessages.push({
- role: message.role,
- content: message.message,
- });
- } else if (Array.isArray(message.message)) {
- openAIMessages.push({
- role: message.role,
- content: JSON.stringify(message.message),
- });
- }
+ openAIMessages.push({
+ role: message.role,
+ content: JSON.stringify(message.message),
+ });
}
return openAIMessages;
};
diff --git a/js/packages/stream/src/transformer.ts b/js/packages/stream/src/transformer.ts
index 093247c52..594877366 100644
--- a/js/packages/stream/src/transformer.ts
+++ b/js/packages/stream/src/transformer.ts
@@ -1,5 +1,6 @@
import { parse } from "best-effort-json-parser";
-import { ResponseTemplateChunk, TextChunk } from "./types";
+import invariant from "tiny-invariant";
+import { ResponseTemplate, TextChunk } from "./types";
// Define the types for the data flowing through the stream
type InputType = string; // Change this to match your input type
type OutputType = string; // Change this to match your output type
@@ -14,38 +15,41 @@ export class CrayonDataStreamTransformer implements TransformStream<InputType, O
constructor(opts?: TransformerOpts) {
let streamedContent = "";
+ let previouslyStreamedTextContent: string = "";
+ let hasPendingTemplateToStream: boolean = false;
+
const transform = new TransformStream({
transform: async (
content: InputType,
controller: TransformStreamDefaultController<OutputType>,
) => {
try {
- const previousParsed = parse(streamedContent);
streamedContent += content;
+
const parsed = parse(streamedContent);
+ const newContent = parsed?.response?.pop?.();
- if (previousParsed.response && parsed.response) {
- if (previousParsed.response.length === parsed.response.length) {
- const newContent = parsed.response.pop();
- const lastContent = previousParsed.response.pop();
- if (newContent.type === "text" && newContent.text) {
- const textPart = newContent.text.substring(lastContent.text?.length);
- if (textPart.length > 0) {
- controller.enqueue(new TextChunk(textPart).toSSEString());
- }
- }
- } else {
- const lastTemplate = previousParsed.response.pop();
- if (typeof lastTemplate === "object") {
- controller.enqueue(new ResponseTemplateChunk(lastTemplate).toSSEString());
- }
+ if (newContent?.type === "text") {
+ const prevContent = parsed.response.pop();
+ const newText = newContent.text || "";
+
+ if (hasPendingTemplateToStream && prevContent && prevContent?.type !== "text") {
+ const { name, templateProps } = prevContent;
+ invariant(name, "name is required in ResponseTemplate");
+ controller.enqueue(new ResponseTemplate(name, templateProps).toSSEString());
+ hasPendingTemplateToStream = false;
+ }
- const newContent = parsed.response.pop();
+ const textContent = newText.substring(previouslyStreamedTextContent.length);
- if (newContent.type === "text" && newContent.text) {
- controller.enqueue(new TextChunk(newContent).toSSEString());
- }
+ if (textContent.length > 0) {
+ controller.enqueue(new TextChunk(textContent).toSSEString());
}
+
+ previouslyStreamedTextContent = newText;
+ } else {
+ hasPendingTemplateToStream = true;
+ previouslyStreamedTextContent = "";
}
} catch (error) {
controller.error(error);
@@ -54,14 +58,14 @@ export class CrayonDataStreamTransformer implements TransformStream<InputType, O
flush: async (controller: TransformStreamDefaultController<OutputType>) => {
const parsed = parse(streamedContent);
- if (
- parsed.response &&
- parsed.response.length &&
- parsed.response[parsed.response.length - 1]?.type !== "text"
- ) {
- const lastTemplate = parsed.response.pop();
- controller.enqueue(new ResponseTemplateChunk(lastTemplate).toSSEString());
+ const lastContent = parsed?.response?.pop?.();
+
+ if (hasPendingTemplateToStream && lastContent && lastContent.type !== "text") {
+ const { name, templateProps } = lastContent;
+ invariant(name, "name is required in ResponseTemplate");
+ controller.enqueue(new ResponseTemplate(name, templateProps).toSSEString());
}
+
await opts?.onFinish(controller);
},
});
diff --git a/js/packages/stream/src/types.ts b/js/packages/stream/src/types.ts
index d8c7e7469..a843ca8e3 100644
--- a/js/packages/stream/src/types.ts
+++ b/js/packages/stream/src/types.ts
@@ -1,24 +1,105 @@
+import { encode } from "eventsource-encoder";
+import invariant from "tiny-invariant";
+
+export type JSONValue =
+ | string
+ | number
+ | boolean
+ | {
+ [value: string]: JSONValue;
+ }
+ | JSONValue[]
+ | null;
+
+export enum SSEType {
+ TextDelta = "text",
+ ResponseTemplate = "tpl",
+ ResponseTemplatePropsChunk = "tpl_props_chunk",
+ ContextAppend = "context_append",
+ MessageUpdate = "message_update",
+}
+
interface Chunk {
toSSEString(): string;
}
export class TextChunk implements Chunk {
- constructor(private readonly data: string) {}
+ constructor(readonly chunk: string) {}
+
+ toSSEString(): string {
+ return encode({
+ event: SSEType.TextDelta,
+ data: this.chunk,
+ });
+ }
+
+ static fromSSEData(data: string): TextChunk {
+ return new TextChunk(data);
+ }
+}
+export class ResponseTemplate implements Chunk {
+ constructor(
+ readonly name: string,
+ readonly templateProps?: object,
+ ) {}
+
+ toSSEString(): string {
+ return encode({
+ event: SSEType.ResponseTemplate,
+ data: JSON.stringify({ name: this.name, templateProps: this.templateProps }),
+ });
+ }
+
+ static fromSSEData(data: string): ResponseTemplate {
+ const { name, templateProps } = JSON.parse(data);
+ invariant(name, "name is required in ResponseTemplate");
+ return new ResponseTemplate(name, templateProps);
+ }
+}
+
+export class ResponseTemplatePropsChunk implements Chunk {
+ constructor(readonly chunk: string) {}
toSSEString(): string {
- return `0:${this.data}\n`;
+ return encode({
+ event: SSEType.ResponseTemplatePropsChunk,
+ data: this.chunk,
+ });
+ }
+
+ static fromSSEData(data: string): ResponseTemplatePropsChunk {
+ return new ResponseTemplatePropsChunk(data);
}
}
-export interface ResponseTemplate {
- name: string;
- templateProps: object;
+export class ContextUpdate implements Chunk {
+ constructor(readonly contextItem: JSONValue) {}
+
+ toSSEString(): string {
+ return encode({
+ event: SSEType.ContextAppend,
+ data: JSON.stringify(this.contextItem),
+ });
+ }
+
+ static fromSSEData(data: string): ContextUpdate {
+ return new ContextUpdate(JSON.parse(data));
+ }
}
-export class ResponseTemplateChunk implements Chunk {
- constructor(private readonly template: ResponseTemplate) {}
+export class MessageUpdate implements Chunk {
+ constructor(readonly id: string) {}
toSSEString(): string {
- return `1:${JSON.stringify(this.template)}\n`;
+ return encode({
+ event: SSEType.MessageUpdate,
+ data: JSON.stringify({ id: this.id }),
+ });
+ }
+
+ static fromSSEData(data: string): MessageUpdate {
+ const { id } = JSON.parse(data);
+ invariant(id, "id is required in MessageUpdate");
+ return new MessageUpdate(id);
}
}
diff --git a/js/pnpm-lock.yaml b/js/pnpm-lock.yaml
index dd39a7c1f..4a6cdb606 100644
--- a/js/pnpm-lock.yaml
+++ b/js/pnpm-lock.yaml
@@ -44,9 +44,15 @@ importers:
packages/react-core:
dependencies:
+ '@crayonai/stream':
+ specifier: workspace:*
+ version: link:../stream
ai:
specifier: ^4.0.3
+ eventsource-parser:
+ specifier: ^3.0.0
+ version: 3.0.0
react:
specifier: '>=18.0.0'
version: 18.3.1
@@ -102,6 +108,9 @@ importers:
'@crayonai/react-core':
specifier: workspace:*
version: link:../react-core
+ '@crayonai/stream':
+ specifier: workspace:*
+ version: link:../stream
'@floating-ui/react-dom':
specifier: ^2.1.2
@@ -277,12 +286,15 @@ importers:
packages/stream:
dependencies:
- '@crayonai/react-core':
- specifier: workspace:*
- version: link:../react-core
best-effort-json-parser:
specifier: ^1.1.2
version: 1.1.2
+ eventsource-encoder:
+ specifier: ^1.0.1
+ version: 1.0.1
+ tiny-invariant:
+ specifier: ^1.3.3
+ version: 1.3.3
zod:
specifier: ^3.24.1
version: 3.24.1
@@ -2522,6 +2534,10 @@ packages:
resolution: {integrity: sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==}
engines: {node: '>=0.8.x'}
+ resolution: {integrity: sha512-57iFiUzq6/LZSrY1vuE8pdko9ip7/gpX6zfpT16r70fyrqAWwtMHhKKPbxoJJN5E9XfG4hJBi44U3/MSj8TD5Q==}
+ engines: {node: '>=18.0.0'}
+
resolution: {integrity: sha512-T1C0XCUimhxVQzW4zFipdx0SficT651NnkR0ZSH3yQwh+mFMdLfgjABVi4YtMTtaL4s168593DaoaRLMqryavA==}
engines: {node: '>=18.0.0'}
@@ -5778,6 +5794,8 @@ snapshots:
+