-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to add/remove user tools from threads
- Loading branch information
1 parent
485fcbe
commit b7f661e
Showing
7 changed files
with
125 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { toast } from "sonner"; | ||
import useSWR from "swr"; | ||
|
||
import { UpdateThread } from "~/lib/model/threads"; | ||
import { AgentService } from "~/lib/service/api/agentService"; | ||
import { ThreadsService } from "~/lib/service/api/threadsService"; | ||
|
||
import { useAsync } from "~/hooks/useAsync"; | ||
|
||
function useThread(threadId?: Nullish<string>) { | ||
return useSWR(ThreadsService.getThreadById.key(threadId), ({ threadId }) => | ||
ThreadsService.getThreadById(threadId) | ||
); | ||
} | ||
|
||
export function useOptimisticThread(threadId?: Nullish<string>) { | ||
const { data: thread, mutate } = useThread(threadId); | ||
|
||
const handleUpdateThread = useAsync(ThreadsService.updateThreadById); | ||
|
||
const updateThread = async (updates: Partial<UpdateThread>) => { | ||
if (!thread) return; | ||
|
||
const updatedThread = { ...thread, ...updates }; | ||
|
||
// optimistic update | ||
mutate((thread) => (thread ? updatedThread : thread), false); | ||
|
||
const { error, data } = await handleUpdateThread.executeAsync( | ||
thread.id, | ||
updatedThread | ||
); | ||
|
||
if (data) return; | ||
|
||
if (error instanceof Error) toast.error(error.message); | ||
|
||
// revert optimistic update | ||
mutate(thread); | ||
}; | ||
|
||
return { thread, updateThread }; | ||
} | ||
|
||
export function useThreadKnowledge(threadId?: Nullish<string>) { | ||
return useSWR(ThreadsService.getKnowledge.key(threadId), ({ threadId }) => | ||
ThreadsService.getKnowledge(threadId) | ||
); | ||
} | ||
|
||
export function useThreadAgents(threadId?: Nullish<string>) { | ||
const { data: thread } = useThread(threadId); | ||
|
||
return useSWR( | ||
AgentService.getAgentById.key(thread?.agentID), | ||
({ agentId }) => AgentService.getAgentById(agentId) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters