Spaces:
Paused
Paused
Commit
•
050e1d0
1
Parent(s):
7a5d912
a quick win, maybe
Browse files- src/app/main.tsx +17 -5
src/app/main.tsx
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
"use client"
|
2 |
|
3 |
-
import { useState, useTransition } from "react"
|
4 |
import { format } from "date-fns"
|
5 |
|
6 |
import { Observe } from "./observe"
|
@@ -19,15 +19,27 @@ export default function Main() {
|
|
19 |
const [isLoadingAction, setLoadingAction] = useState(false)
|
20 |
|
21 |
const [action, setAction] = useState<string>("Nothing to say yet.")
|
|
|
22 |
|
23 |
const handleOnEvent = (event: string, needAnswer: boolean) => {
|
|
|
24 |
setLoadingAction(true)
|
25 |
startTransition(async () => {
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
}
|
30 |
-
setLoadingAction(false)
|
31 |
})
|
32 |
}
|
33 |
// receive a new observation from what the agent is looking at
|
|
|
1 |
"use client"
|
2 |
|
3 |
+
import { useRef, useState, useTransition } from "react"
|
4 |
import { format } from "date-fns"
|
5 |
|
6 |
import { Observe } from "./observe"
|
|
|
19 |
const [isLoadingAction, setLoadingAction] = useState(false)
|
20 |
|
21 |
const [action, setAction] = useState<string>("Nothing to say yet.")
|
22 |
+
const lastEvent = useRef("")
|
23 |
|
24 |
const handleOnEvent = (event: string, needAnswer: boolean) => {
|
25 |
+
lastEvent.current = event
|
26 |
setLoadingAction(true)
|
27 |
startTransition(async () => {
|
28 |
+
try {
|
29 |
+
const action = await think(event, needAnswer)
|
30 |
+
|
31 |
+
// here what could happen is that we received a message more recent than what the LLM is currently working on
|
32 |
+
// when that happen, the best is to just interrupt the LLM (well.. in our case, it means ignore what it says)
|
33 |
+
const canSetAction = action && lastEvent.current === event
|
34 |
+
|
35 |
+
if (canSetAction) {
|
36 |
+
setAction(action)
|
37 |
+
}
|
38 |
+
} catch (err) {
|
39 |
+
console.error(err)
|
40 |
+
} finally {
|
41 |
+
setLoadingAction(false)
|
42 |
}
|
|
|
43 |
})
|
44 |
}
|
45 |
// receive a new observation from what the agent is looking at
|