Download src/hooks/chat/use-btw-interceptor.ts from SaylorTwift/openhands: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/SaylorTwift/openhands/resolve/main/src/hooks/chat/use-btw-interceptor.ts
- Command line
-
hf download hf://SaylorTwift/openhands/src/hooks/chat/use-btw-interceptor.ts
-
curl -L -o use-btw-interceptor.ts https://huggingface.co/SaylorTwift/openhands/resolve/main/src/hooks/chat/use-btw-interceptor.ts
1.44 kB
| import { useCallback } from "react"; | |
| import { askAgent } from "#/hooks/mutation/conversation-mutation-utils"; | |
| import { useBtwStore } from "#/stores/btw-store"; | |
| import { BTW_COMMAND } from "#/utils/constants"; | |
| const BTW_PREFIX = `${BTW_COMMAND} `; | |
| /** | |
| * Intercepts "/btw <question>" submissions and routes them through the | |
| * ask_agent side-channel. Everything else falls through to `onSubmit`. | |
| * Passthrough when `conversationId` is null. | |
| */ | |
| export const useBtwInterceptor = ( | |
| conversationId: string | null | undefined, | |
| onSubmit: (message: string) => void, | |
| ) => { | |
| const addPending = useBtwStore((s) => s.addPending); | |
| const resolve = useBtwStore((s) => s.resolve); | |
| const fail = useBtwStore((s) => s.fail); | |
| return useCallback( | |
| (message: string) => { | |
| const trimmed = message.trim(); | |
| const isBtw = trimmed === BTW_COMMAND || trimmed.startsWith(BTW_PREFIX); | |
| if (!conversationId || !isBtw) { | |
| onSubmit(message); | |
| return; | |
| } | |
| const question = trimmed.slice(BTW_COMMAND.length).trim(); | |
| if (!question) return; | |
| const entryId = addPending(conversationId, question); | |
| askAgent(conversationId, question) | |
| .then(({ response }) => resolve(conversationId, entryId, response)) | |
| .catch((err) => | |
| fail(conversationId, entryId, err?.message ?? "Failed to ask agent"), | |
| ); | |
| }, | |
| [conversationId, onSubmit, addPending, resolve, fail], | |
| ); | |
| }; | |