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 hook issue of chat #1191

Merged
merged 2 commits into from
Jan 15, 2025
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
File renamed without changes.
5 changes: 3 additions & 2 deletions packages/chat/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestia/chat",
"version": "0.1.2",
"version": "0.2.1",
"main": "lib/index.js",
"module": "lib/index.mjs",
"typings": "lib/index.d.ts",
Expand All @@ -11,7 +11,8 @@
"build:lib": "rimraf lib && tsc --project tsconfig.lib.json && rollup -c",
"dev": "vite",
"lint": "eslint .",
"preview": "vite preview"
"preview": "vite preview",
"deploy": "node build/deploy.js"
},
"repository": {
"type": "git",
Expand Down
74 changes: 40 additions & 34 deletions packages/chat/src/movies/NestiaChatMovie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import {
INestiaAgentOperationSelection,
INestiaAgentPrompt,
INestiaAgentTokenUsage,
NestiaAgent,
} from "@nestia/agent";
import html2canvas from "html2canvas";
Expand All @@ -29,47 +30,50 @@ export const NestiaChatMovie = ({ agent }: NestiaChatMovie.IProps) => {
const inputRef = useRef<HTMLInputElement>(null);

const [text, setText] = useState("");
const [histories, setHistories] = useState(agent.getPromptHistories());
const [tokenUsage, setTokenUsage] = useState(agent.getTokenUsage());
const [histories, setHistories] = useState<INestiaAgentPrompt[]>(
agent.getPromptHistories().slice(),
);
const [tokenUsage, setTokenUsage] = useState<INestiaAgentTokenUsage>(
JSON.parse(JSON.stringify(agent.getTokenUsage())),
);
const [height, setHeight] = useState(122);

const [enabled, setEnabled] = useState(true);
const [selections, setSelections] = useState<
INestiaAgentOperationSelection[]
>([]);

const getHistories = () => histories;
const getSelections = () => selections;

useEffect(() => {
if (inputRef.current !== null) inputRef.current.select();
agent.on("text", async (evt) => setHistories([...getHistories(), evt]));
agent.on("describe", async (evt) => setHistories([...getHistories(), evt]));
agent.on("select", async (evt) => {
setHistories([
...getHistories(),
{
type: "select",
id: "something",
operations: [
{
...evt.operation,
reason: evt.reason,
toJSON: () => ({}) as any,
} satisfies INestiaAgentOperationSelection,
],
} satisfies INestiaAgentPrompt.ISelect,
]);
setSelections([
...getSelections(),
{
...evt.operation,
reason: evt.reason,
toJSON: () => ({}) as any,
} satisfies INestiaAgentOperationSelection,
]);
agent.on("text", (evt) => {
histories.push(evt);
setHistories(histories);
});
agent.on("describe", (evt) => {
histories.push(evt);
setHistories(histories);
});
agent.on("select", (evt) => {
histories.push({
type: "select",
id: "something",
operations: [
{
...evt.operation,
reason: evt.reason,
toJSON: () => ({}) as any,
} satisfies INestiaAgentOperationSelection,
],
});
setHistories(histories);

selections.push({
...evt.operation,
reason: evt.reason,
toJSON: () => ({}) as any,
} satisfies INestiaAgentOperationSelection);
setSelections(selections);
});
setTokenUsage(JSON.parse(JSON.stringify(agent.getTokenUsage())));
setTokenUsage(agent.getTokenUsage());
}, []);

const handleKeyUp = async (event: React.KeyboardEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -99,9 +103,11 @@ export const NestiaChatMovie = ({ agent }: NestiaChatMovie.IProps) => {
handleResize();
await agent.conversate(text);

histories.splice(0, histories.length);
histories.push(...agent.getPromptHistories());
setHistories(histories);
setTokenUsage(agent.getTokenUsage());
setEnabled(true);
setHistories(agent.getPromptHistories().slice());
setTokenUsage(JSON.parse(JSON.stringify(agent.getTokenUsage())));

const selections: INestiaAgentOperationSelection[] = agent
.getPromptHistories()
Expand Down
5 changes: 4 additions & 1 deletion packages/chat/src/shopping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const main = async (): Promise<void> => {

// HANDSHAKE WITH SHOPPING BACKEND
const connection: IHttpConnection = {
host: "http://localhost:37001",
host: "https://shopping-be.wrtn.ai/",
};
await ShoppingApi.functional.shoppings.customers.authenticate.create(
connection,
Expand Down Expand Up @@ -70,6 +70,9 @@ const main = async (): Promise<void> => {
connection,
},
],
config: {
locale: "en-US",
},
});
createRoot(window.document.getElementById("root")!).render(
<NestiaChatApplication agent={agent} />,
Expand Down
Loading