diff --git a/chat.ts b/chat.ts index de1213f..d084116 100644 --- a/chat.ts +++ b/chat.ts @@ -150,7 +150,7 @@ export class Chat { decoder, readableStream, opts.statusStream, - ).then((content) => { + ).then(([content]) => { const messages: Message[] = [ { role: "user", @@ -296,7 +296,7 @@ export class Chat { decoder, readableStream, opts.statusStream, - ).then((content) => { + ).then(([content]) => { this.messages.push( { role: "user", diff --git a/content.ts b/content.ts index c88cf05..9758b97 100644 --- a/content.ts +++ b/content.ts @@ -89,6 +89,18 @@ export type ContentListOptions = { cortexName?: string; }; +export type ContentMetadata = { + id: string; + title: string; + version: number; + commands: ContentCommand[]; + cortex: string; + createdAt: string; + userEmail: string; + status: ContentStatus; + publishedVersion: number | null; +}; + export class Content { get id() { return this._id; @@ -194,44 +206,37 @@ export class Content { title, prompt, stream, + noContentInHeaders: true, }); const reader = res.body!.getReader(); const decoder = new TextDecoder("utf-8"); - const id: string = res.headers.get("id") || ""; - const version: number = parseInt(res.headers.get("version") || "0"); - const userEmail = res.headers.get("userEmail") || undefined; - const createdAt: string = res.headers.get("createdAt") || ""; - const status: ContentStatus = res.headers.get("status") as ContentStatus; - const publishedVersion: number | undefined = numberOrUndefined( - res.headers.get("publishedVersion"), - ); - const commands: ContentCommand[] = JSON.parse( - res.headers.get("commands") || "[]", - ); - const readableStream = new Readable({ read() {}, }); - const contentPromise = processStream( + const contentPromise = processStream( reader, decoder, readableStream, opts.statusStream, - ).then((content) => { + ).then(([content, metadata]) => { + if (!metadata) { + throw new Error("Metadata not found in stream"); + } + return new Content( client, - id, - title, + metadata.id, + metadata.title, content, - commands, - version, - createdAt, - status, - cortex.name, - userEmail, - publishedVersion, + metadata.commands, + metadata.version, + metadata.createdAt, + metadata.status, + metadata.cortex, + metadata.userEmail, + metadata.publishedVersion || undefined, ); }); @@ -317,38 +322,33 @@ export class Content { const res = await this.apiClient.POST(`/content/${this._id}/refine`, { prompt, stream: true, + noContentInHeaders: true, }); const reader = res.body!.getReader(); const decoder = new TextDecoder("utf-8"); - const version: number = parseInt(res.headers.get("version") || "0"); - const createdAt = res.headers.get("createdAt") || ""; - const userEmail = res.headers.get("userEmail") || undefined; - const commands: ContentCommand[] = JSON.parse( - res.headers.get("commands") || "[]", - ); - const status = res.headers.get("status") as ContentStatus; - const publishedVersion: number | undefined = numberOrUndefined( - res.headers.get("publishedVersion"), - ); - this._version = version; - this._commands = commands; - this._createdAt = createdAt; - this._userEmail = userEmail; - this._status = status; - this._publishedVersion = publishedVersion; - const readableStream = new Readable({ read() {}, }); - const contentPromise = processStream( + const contentPromise = processStream( reader, decoder, readableStream, opts.statusStream, - ).then((content) => { + ).then(([content, metadata]) => { + if (!metadata) { + throw new Error("Metadata not found in stream"); + } + this._content = content; + this._version = metadata.version; + this._commands = metadata.commands; + this._createdAt = metadata.createdAt; + this._userEmail = metadata.userEmail; + this._status = metadata.status; + this._publishedVersion = metadata.publishedVersion || undefined; + this._title = metadata.title; return this; }); diff --git a/package.json b/package.json index 1798e45..504dec2 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "url": "https://github.com/cortexclick/cortex-sdk", "type": "git" }, - "version": "0.0.4", + "version": "0.0.5", "type": "module", "main": "index.js", "scripts": { diff --git a/utils/streaming.ts b/utils/streaming.ts index 1f1300f..fa57489 100644 --- a/utils/streaming.ts +++ b/utils/streaming.ts @@ -1,15 +1,17 @@ import { Readable } from "stream"; -export async function processStream( +export async function processStream>( reader: ReadableStreamDefaultReader, decoder: TextDecoder, contentStream: Readable, statusStream?: Readable, -): Promise { +): Promise<[string, Metadata | undefined]> { let buffer = ""; let fullContent = ""; let isStatusStreamOpen = true; + let metadata: Metadata | undefined = undefined; + const processNextChunk = async (): Promise => { const { done, value } = await reader.read(); if (done) { @@ -43,6 +45,8 @@ export async function processStream( // t:s = status message else if (json.messageType === "status" && statusStream) { statusStream.push(line + "\n"); + } else if (json.messageType === "metadata") { + metadata = json.data; } } catch (e) { console.error("Error parsing JSON:", e); @@ -57,5 +61,5 @@ export async function processStream( contentStream.emit("error", error); }); - return fullContent; + return [fullContent, metadata]; }