Skip to content

Commit 689ffae

Browse files
authored
feat(website): apply react.use for ShoppingChat and stop using async component (#436)
1 parent 06afb3c commit 689ffae

2 files changed

Lines changed: 54 additions & 35 deletions

File tree

packages/chat/src/examples/shopping/ShoppingChatApplication.tsx

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import {
66
HttpLlm,
77
OpenApi,
88
} from "@samchon/openapi";
9+
import { Suspense, use } from "react";
910
import typia from "typia";
1011

1112
import { AgenticaChatApplication } from "../../AgenticaChatApplication";
1213

13-
export function ShoppingChatApplicationSkeleton() {
14+
function ShoppingChatApplicationSkeleton() {
1415
return (
1516
<div>
1617
<h2>Loading Swagger document</h2>
@@ -21,38 +22,58 @@ export function ShoppingChatApplicationSkeleton() {
2122
);
2223
}
2324

24-
export async function ShoppingChatApplication(props: ShoppingChatApplication.IProps) {
25-
const application = HttpLlm.application({
26-
model: props.schemaModel,
27-
document: OpenApi.convert(
28-
await fetch(
29-
"https://raw.githubusercontent.com/samchon/shopping-backend/refs/heads/master/packages/api/customer.swagger.json",
30-
)
31-
.then(async r => r.json() as unknown)
32-
.then(v => typia.assert<Parameters<typeof OpenApi.convert>[0]>(v)),
33-
),
34-
});
25+
function AsyncAgenticaChatApplication({ agentPromise, title }: { agentPromise: Promise<Agentica<ILlmSchema.Model>>; title: string }) {
26+
const agent = use(agentPromise);
27+
return (
28+
<AgenticaChatApplication
29+
agent={agent}
30+
title={title}
31+
/>
32+
);
33+
}
3534

36-
const agent: Agentica<ILlmSchema.Model> = new Agentica({
37-
model: props.schemaModel,
38-
vendor: {
39-
api: props.api,
40-
model: props.vendorModel,
41-
},
42-
controllers: [
43-
{
44-
protocol: "http",
45-
name: "main",
46-
application,
47-
connection: props.connection,
35+
export function ShoppingChatApplication(props: ShoppingChatApplication.IProps) {
36+
const getAgent = async () => {
37+
const application = HttpLlm.application({
38+
model: props.schemaModel,
39+
document: OpenApi.convert(
40+
await fetch(
41+
"https://raw.githubusercontent.com/samchon/shopping-backend/refs/heads/master/packages/api/customer.swagger.json",
42+
)
43+
.then(async r => r.json() as unknown)
44+
.then(v => typia.assert<Parameters<typeof OpenApi.convert>[0]>(v)),
45+
),
46+
});
47+
const agent: Agentica<ILlmSchema.Model> = new Agentica({
48+
model: props.schemaModel,
49+
vendor: {
50+
api: props.api,
51+
model: props.vendorModel,
4852
},
49-
],
50-
config: {
51-
locale: props.locale,
52-
},
53-
});
53+
controllers: [
54+
{
55+
protocol: "http",
56+
name: "main",
57+
application,
58+
connection: props.connection,
59+
},
60+
],
61+
config: {
62+
locale: props.locale,
63+
},
64+
});
65+
return agent;
66+
};
67+
68+
const agentPromise = getAgent();
69+
5470
return (
55-
<AgenticaChatApplication agent={agent} title="Agentica Shopping Chatbot" />
71+
<Suspense fallback={<ShoppingChatApplicationSkeleton />}>
72+
<AsyncAgenticaChatApplication
73+
agentPromise={agentPromise}
74+
title="Agentica Shopping Chatbot"
75+
/>
76+
</Suspense>
5677
);
5778
}
5879
export namespace ShoppingChatApplication {

packages/chat/src/examples/shopping/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import {
1010
} from "@mui/material";
1111
import ShoppingApi from "@samchon/shopping-api";
1212
import OpenAI from "openai";
13-
import { Suspense, useState } from "react";
13+
import { useState } from "react";
1414
import { createRoot } from "react-dom/client";
1515

1616
import { VendorConfigurationMovie } from "../common/VendorConfigurationMovie";
1717

18-
import { ShoppingChatApplication, ShoppingChatApplicationSkeleton } from "./ShoppingChatApplication";
18+
import { ShoppingChatApplication } from "./ShoppingChatApplication";
1919

2020
function Application() {
2121
const [config, setConfig] = useState<VendorConfigurationMovie.IConfig>(
@@ -78,9 +78,7 @@ function Application() {
7878
>
7979
{next !== null
8080
? (
81-
<Suspense fallback={<ShoppingChatApplicationSkeleton />}>
82-
<ShoppingChatApplication {...next} />
83-
</Suspense>
81+
<ShoppingChatApplication {...next} />
8482
)
8583
: (
8684
<FormControl

0 commit comments

Comments
 (0)