diff --git a/.changeset/tidy-onions-camp.md b/.changeset/tidy-onions-camp.md new file mode 100644 index 0000000..0e1ff68 --- /dev/null +++ b/.changeset/tidy-onions-camp.md @@ -0,0 +1,5 @@ +--- +'ai-connector': patch +--- + +Throw error when provided AI response isn't valid diff --git a/packages/core/streams/ai-stream.ts b/packages/core/streams/ai-stream.ts index 29d5ff0..6c7db3c 100644 --- a/packages/core/streams/ai-stream.ts +++ b/packages/core/streams/ai-stream.ts @@ -288,6 +288,29 @@ export function trimStartOfStreamHelper(): (text: string) => string { * @throws Will throw an error if the response is not OK. */ export function AIStream( + + res: Response, + customParser: AIStreamParser, + callbacks?: AIStreamCallbacks +): ReadableStream { + // If the response is not OK, we want to throw an error to indicate that + // the AI service is not available. + // When catching this error, we can check the status code and return a handled + // error response to the client. + if (!res.ok) { + throw new Error( + `Failed to convert the response to stream. Received status code: ${res.status}.` + ) + } + + const stream = + res.body || + new ReadableStream({ + start(controller) { + controller.close() + } + }) + response: Response, customParser?: AIStreamParser, callbacks?: AIStreamCallbacksAndOptions, @@ -315,6 +338,7 @@ export function AIStream( const responseBodyStream = response.body || createEmptyReadableStream(); + return responseBodyStream .pipeThrough(createEventStreamTransformer(customParser))