π OpenHands: AI-Driven Development
import { useCallback } from "react";
import { useTranslation } from "react-i18next";
import { askAgent } from "#/hooks/mutation/conversation-mutation-utils";
import { useBtwStore } from "#/stores/btw-store";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
import { I18nKey } from "#/i18n/declaration";
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 { t } = useTranslation();
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) {
displayErrorToast(t(I18nKey.CHAT_INTERFACE$BTW_QUESTION_REQUIRED)); // bare /btw β nothing to ask
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, t],
);
};