Skip to content

Commit 6bdfcf3

Browse files
authored
Update dependencies and refactor OpenAI stream handling (#216)
- Updated various dependencies in `package.json` and `bun.lock`, including `@react-spring/web`, `ai`, and `openai`. - Refactored the OpenAI stream handling in `landing-page-quote.tsx` to utilize a new `ReadableStream` implementation for better performance and error handling. - Adjusted the linting command order in `package.json` for improved consistency.
1 parent 36db0e1 commit 6bdfcf3

File tree

3 files changed

+51
-117
lines changed

3 files changed

+51
-117
lines changed

app/landing-page-quote.tsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { documentToHtmlString } from "@contentful/rich-text-html-renderer";
22
import { RiOpenaiFill } from "@remixicon/react";
33
import { kv } from "@vercel/kv";
4-
import { OpenAIStream } from "ai";
54
import type { AllInOnePageQuery } from "gql/graphql";
65
import Link from "next/link";
76
import OpenAi from "openai";
@@ -84,12 +83,26 @@ Strikte Regeln
8483
],
8584
});
8685

87-
// Convert the response into a friendly text-stream
88-
// @ts-expect-error TODO: migrate to new OpenAIStream API
89-
const stream = OpenAIStream(response, {
90-
async onCompletion(completion) {
91-
await kv.set(cacheKey, completion);
92-
await kv.expire(cacheKey, 60 * 60);
86+
// Convert OpenAI stream to ReadableStream and handle completion callback
87+
let fullCompletion = "";
88+
const stream = new ReadableStream({
89+
async start(controller) {
90+
try {
91+
for await (const chunk of response) {
92+
const content = chunk.choices[0]?.delta?.content;
93+
if (content) {
94+
fullCompletion += content;
95+
const encoder = new TextEncoder();
96+
controller.enqueue(encoder.encode(content));
97+
}
98+
}
99+
// Cache the full completion when stream ends
100+
await kv.set(cacheKey, fullCompletion);
101+
await kv.expire(cacheKey, 60 * 60);
102+
controller.close();
103+
} catch (error) {
104+
controller.error(error);
105+
}
93106
},
94107
});
95108

@@ -105,7 +118,6 @@ Strikte Regeln
105118
);
106119
}
107120

108-
const extractTextRegex = /"([^"]*)"/;
109121
async function Reader({
110122
reader,
111123
}: {
@@ -119,15 +131,10 @@ async function Reader({
119131
}
120132

121133
const text = new TextDecoder().decode(value);
122-
const extractedText = text.match(extractTextRegex)?.[1]?.replace(
123-
/\\n/g,
124-
`
125-
`
126-
);
127134

128135
return (
129136
<>
130-
{extractedText}
137+
{text}
131138
<Suspense>
132139
<Reader reader={reader} />
133140
</Suspense>

0 commit comments

Comments
 (0)