Skip to content

Commit

Permalink
feat: simplify isPendingAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Mar 1, 2024
1 parent 2937ef3 commit 7eaa76e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default function ChatSection () {
const [input, handleInputChange] = useAtom(inputAtom)
const handleSubmit = useSetAtom(submitAtom)
const isLoading = useAtomValue(isLoadingAtom)
console.log('isLoading', isLoading)

return (
<div className="space-y-4 max-w-5xl w-full">
Expand Down
2 changes: 1 addition & 1 deletion examples/llamaindex-straming/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "llamaindex-straming",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "next dev --port 3001",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand Down
12 changes: 7 additions & 5 deletions src/chatAtoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export function chatAtoms (

const abortControllerAtom = atom<AbortController>(new AbortController())
const isLoadingAtom = atom(false)
const isPendingAtom = atom(false)

type Metadata = {
credentials?: RequestCredentials;
Expand Down Expand Up @@ -125,7 +124,6 @@ export function chatAtoms (
})

if (chatOptions.onResponse) {
set(isPendingAtom, false)
try {
await chatOptions.onResponse(
get,
Expand Down Expand Up @@ -250,7 +248,6 @@ export function chatAtoms (
) {
const onFunctionCall = chatOptions.experimental_onFunctionCall
try {
set(isPendingAtom, true)
set(isLoadingAtom, true)
const abortController = new AbortController()
set(abortControllerAtom, abortController)
Expand Down Expand Up @@ -327,7 +324,6 @@ export function chatAtoms (
}
}
} finally {
set(isPendingAtom, false)
set(isLoadingAtom, false)
}
}
Expand Down Expand Up @@ -385,7 +381,13 @@ export function chatAtoms (
}),
dataAtom: atom(get => get(dataAtom)),
isLoadingAtom: atom(get => get(isLoadingAtom)),
isPendingAtom: atom(get => get(isPendingAtom)),
isPendingAtom: atom(get => {
const messages = get(messagesAtom)
if (isPromiseLike(messages)) {
return messages.then((messages)=> messages.at(-1)?.role !== 'assistant')
}
return messages.at(-1)?.role !== 'assistant'
}),
inputAtom: atom(
get => get(inputBaseAtom),
(get, set, event: {
Expand Down

0 comments on commit 7eaa76e

Please sign in to comment.