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

Move data fetching into FlightCard #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 14 additions & 9 deletions app/action.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OpenAI } from "openai";
import { createAI, getMutableAIState, render } from "ai/rsc";
import { z } from "zod";
import { Suspense } from "react";

interface FlightInfo {
readonly flightNumber: string;
Expand All @@ -9,7 +10,7 @@ interface FlightInfo {
}

interface FlightCardProps {
readonly flightInfo: FlightInfo;
readonly flightNumber: string;
}

type AIStateItem =
Expand All @@ -31,6 +32,8 @@ interface UIStateItem {
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function getFlightInfo(flightNumber: string): Promise<FlightInfo> {
await new Promise((resolve) => setTimeout(resolve, 500));

return {
flightNumber,
departure: "New York",
Expand All @@ -42,7 +45,9 @@ function Spinner() {
return <div>Loading...</div>;
}

function FlightCard({ flightInfo }: FlightCardProps) {
async function FlightCard({ flightNumber }: FlightCardProps) {
const flightInfo = await getFlightInfo(flightNumber);

return (
<div>
<h2>Flight Information</h2>
Expand Down Expand Up @@ -83,21 +88,21 @@ async function submitUserMessage(userInput: string): Promise<UIStateItem> {
flightNumber: z.string().describe("the number of the flight"),
})
.required(),
render: async function* ({ flightNumber }) {
yield <Spinner />;

const flightInfo = await getFlightInfo(flightNumber);

render: function ({ flightNumber }) {
aiState.done([
...aiState.get(),
{
role: "function",
name: "get_flight_info",
content: JSON.stringify(flightInfo),
content: "TODO",
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware that this is not trivial to solve (one approach here), so there's a chance that this may be an anti-pattern.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But giving up colocation of data fetching in server components, when using them in an AI app (as shown in https://sdk.vercel.ai/docs/concepts/ai-rsc#nested-ui-streaming), would also be unfortunate.

},
]);

return <FlightCard flightInfo={flightInfo} />;
return (
<Suspense fallback={<Spinner />}>
<FlightCard flightNumber={flightNumber} />
</Suspense>
);
},
},
},
Expand Down