Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert error to previous protocol #139

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions apps/nextjs/src/app/api/chat/errorHandling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { RateLimitExceededError } from "@oakai/core/src/utils/rateLimiting/userB
import { PrismaClientWithAccelerate } from "@oakai/db";
import invariant from "tiny-invariant";

import {
consumeStream,
extractStreamMessage,
} from "@/utils/testHelpers/consumeStream";
import { consumeStream } from "@/utils/testHelpers/consumeStream";

import { handleChatException } from "./errorHandling";

Expand Down Expand Up @@ -38,7 +35,7 @@ describe("handleChatException", () => {
expect(response.status).toBe(200);

invariant(response.body instanceof ReadableStream);
const message = extractStreamMessage(await consumeStream(response.body));
const message = JSON.parse(await consumeStream(response.body));

expect(message).toEqual({
type: "error",
Expand Down Expand Up @@ -88,7 +85,8 @@ describe("handleChatException", () => {
expect(response.status).toBe(200);

const consumed = await consumeStream(response.body as ReadableStream);
const message = extractStreamMessage(consumed);
expect(consumed).toMatch(/^\{/);
const message = JSON.parse(consumed);

expect(message).toEqual({
type: "error",
Expand All @@ -114,7 +112,7 @@ describe("handleChatException", () => {

expect(response.status).toBe(200);

const message = extractStreamMessage(
const message = JSON.parse(
await consumeStream(response.body as ReadableStream),
);
expect(message).toEqual({
Expand All @@ -123,4 +121,4 @@ describe("handleChatException", () => {
});
});
});
});
});
3 changes: 1 addition & 2 deletions apps/nextjs/src/app/api/chat/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
import { StreamingTextResponse } from "ai";

export function streamingJSON(message: ErrorDocument | ActionDocument) {
const jsonContent = JSON.stringify(message);
const errorMessage = `0:"${jsonContent.replace(/"/g, '\\"')}"`;
const errorMessage = JSON.stringify(message);

const errorEncoder = new TextEncoder();

Expand Down
9 changes: 0 additions & 9 deletions apps/nextjs/src/utils/testHelpers/consumeStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,3 @@ export async function consumeStream(stream: ReadableStream): Promise<string> {

return result;
}

export function extractStreamMessage(streamedText: string) {
const content = streamedText.match(/0:"(.*)"/);
if (!content?.[1]) {
throw new Error("No message found in streamed text");
}
const strippedContent = content[1].replace(/\\"/g, '"');
return JSON.parse(strippedContent);
}
Loading