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: use new AI protocol format for friendly errors #122

Merged
merged 1 commit into from
Sep 12, 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: 9 additions & 5 deletions apps/nextjs/src/app/api/chat/errorHandling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { RateLimitExceededError } from "@oakai/core/src/utils/rateLimiting/userB
import { PrismaClientWithAccelerate } from "@oakai/db";
import invariant from "tiny-invariant";

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

import { handleChatException } from "./errorHandling";

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

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

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

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

expect(message).toEqual({
type: "error",
value: "Rate limit exceeded",
Expand All @@ -110,7 +114,7 @@ describe("handleChatException", () => {

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

const message = JSON.parse(
const message = extractStreamMessage(
await consumeStream(response.body as ReadableStream),
);
expect(message).toEqual({
Expand Down
4 changes: 3 additions & 1 deletion apps/nextjs/src/app/api/chat/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
import { StreamingTextResponse } from "ai";

export function streamingJSON(message: ErrorDocument | ActionDocument) {
const errorMessage = JSON.stringify(message);
const jsonContent = JSON.stringify(message);
const errorMessage = `0:"${jsonContent.replace(/"/g, '\\"')}"`;
mantagen marked this conversation as resolved.
Show resolved Hide resolved

const errorEncoder = new TextEncoder();

return new StreamingTextResponse(
Expand Down
9 changes: 9 additions & 0 deletions apps/nextjs/src/utils/testHelpers/consumeStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ export async function consumeStream(

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