π OpenHands: AI-Driven Development
import { useConversationWorkspace } from "#/hooks/query/use-conversation-workspace";
import { useEffect, useRef, useState } from "react";
import toast from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { CustomChatInput } from "#/components/features/chat/custom-chat-input";
import { useActiveBackend } from "#/contexts/active-backend-context";
import { useCreateConversation } from "#/hooks/mutation/use-create-conversation";
import { useLocalWorkspaces } from "#/hooks/query/use-local-workspaces";
import { useLlmProfiles } from "#/hooks/query/use-llm-profiles";
import { useModelInterceptor } from "#/hooks/chat/use-model-interceptor";
import { useLlmConfigured } from "#/hooks/use-llm-configured";
import { HOME_PROMPT_DRAFT_KEY } from "#/hooks/chat/use-draft-persistence";
import { useChatAttachmentUpload } from "#/hooks/chat/use-chat-attachment-upload";
import { useConversationStore } from "#/stores/conversation-store";
import { useModelStore } from "#/stores/model-store";
import type { WorkspaceMode } from "#/api/conversation-metadata-store";
import { setPendingTaskAttachments } from "#/stores/pending-task-attachments-store";
import { enqueueHomeTaskPendingMessage } from "#/utils/enqueue-home-task-pending-message";
import { sendMessageWithAttachments } from "#/utils/send-message-with-attachments";
import { useNavigation } from "#/context/navigation-context";
import { useIsCreatingConversation } from "#/hooks/use-is-creating-conversation";
import { Branch, GitRepository } from "#/types/git";
import { Provider } from "#/types/settings";
import { LocalWorkspace } from "#/types/workspace";
import { I18nKey } from "#/i18n/declaration";
import { cn } from "#/utils/utils";
import {
displayErrorToast,
TOAST_OPTIONS,
} from "#/utils/custom-toast-handlers";
import { getApiErrorMessage } from "#/utils/api-error-message";
import { getWorkspacesUnsupportedMessage } from "#/utils/workspaces-compatibility";
import {
readStoredLocalWorkspaceMode,
writeStoredLocalWorkspaceMode,
} from "#/utils/workspace-mode";
import type { PluginSpec } from "#/api/conversation-service/agent-server-conversation-service.types";
import { markAutomationSetupHandoff } from "#/api/automation-setup-handoff-store";
import { PluginPickerModal } from "#/components/features/plugins/plugin-picker-modal";
import { PluginPickerTrigger } from "#/components/features/plugins/plugin-picker-trigger";
import { RecommendedAutomationsLauncher } from "#/components/features/automations/recommended-automations-launcher";
import { useHomeRecommendedAutomationsDismissed } from "./use-home-recommended-automations-dismissed";
import { writeHomeRecommendedAutomationsDismissed } from "./home-recommended-automations-storage";
import { PinnedAutomationsDashboard } from "./featured-automations/pinned-automations-dashboard";
import { RunningAutomationsList } from "./featured-automations/running-automations-list";
import { HomeHeaderTitle } from "./home-header/home-header-title";
import {
HomeLauncherModeToggle,
type HomeLauncherMode,
} from "./home-launcher-mode-toggle";
import { OpenLauncherButton } from "./open-launcher-button";
import { OpenWorkspaceDialog } from "./open-workspace-dialog";
import { OpenRepositoryDialog } from "./open-repository-dialog";
import { HomeGitControlBarPreview } from "./home-git-control-bar-preview";
export function HomeChatLauncher() {
const { t } = useTranslation("openhands");
const { backend } = useActiveBackend();
const { navigate } = useNavigation();
const isLocal = backend.kind === "local";
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [pendingWorkspace, setPendingWorkspace] =
useState<LocalWorkspace | null>(null);
const [pendingRepository, setPendingRepository] =
useState<GitRepository | null>(null);
const [pendingBranch, setPendingBranch] = useState<Branch | null>(null);
const [pendingProvider, setPendingProvider] = useState<Provider | null>(null);
const [workspaceMode, setWorkspaceModeState] = useState<WorkspaceMode>(() =>
readStoredLocalWorkspaceMode(),
);
const isRecommendedAutomationsDismissed =
useHomeRecommendedAutomationsDismissed();
const [selectedPlugins, setSelectedPlugins] = useState<PluginSpec[]>([]);
const [isPluginPickerOpen, setIsPluginPickerOpen] = useState(false);
const [launcherMode, setLauncherMode] = useState<HomeLauncherMode>("code");
const displayedLauncherMode = launcherMode;
const isAutomateMode = launcherMode === "automate";
const handleLauncherModeChange = setLauncherMode;
const isMountedRef = useRef(true);
useEffect(() => {
isMountedRef.current = true;
return () => {
isMountedRef.current = false;
};
}, []);
// The launcher reports a failed create itself (see handleSubmit), so the
// global mutation error toast is turned off to keep it to one toast.
const { mutateAsync: createConversation, isPending } = useCreateConversation({
disableToast: true,
});
const isCreatingElsewhere = useIsCreatingConversation();
const isCreating = isPending || isCreatingElsewhere;
const { isConfigured: isLlmConfigured, isLoading: isLlmConfigLoading } =
useLlmConfigured();
// Block sending entirely when there's no usable LLM; the banner above the
// launcher (rendered by the home route) explains it and offers setup.
const llmBlocked = !isLlmConfigLoading && !isLlmConfigured;
const {
images,
files,
imagesMarkedUploadAsFile,
clearAllFiles,
restoreMessageToInputIfEmpty,
} = useConversationStore();
const { handleUpload } = useChatAttachmentUpload();
const homeLlmProfile = useModelStore((s) => s.homeLlmProfile);
const { data: llmProfiles } = useLlmProfiles();
const { error: workspacesError } = useLocalWorkspaces({ enabled: isLocal });
const { isolated, unsupportedMessage: runtimeWorkspaceMessage } =
useConversationWorkspace();
const workspacesUnsupportedMessage =
runtimeWorkspaceMessage ??
(isLocal ? getWorkspacesUnsupportedMessage(workspacesError, t) : null);
const setWorkspaceMode = (mode: WorkspaceMode) => {
setWorkspaceModeState(mode);
if (isLocal) writeStoredLocalWorkspaceMode(mode);
};
const hasSelection = isLocal
? !!pendingWorkspace
: !!pendingRepository && !!pendingBranch;
const handleSubmit = (message: string) => {
const trimmed = message.trim();
const hasAttachments = images.length > 0 || files.length > 0;
if ((!trimmed && !hasAttachments) || isCreating) return;
// Safety net: the input is disabled when there's no usable LLM, but never
// create a conversation that can't run (it would fail with a cryptic
// API-key error on the first turn).
if (llmBlocked) return;
const attachmentSnapshot = {
images: [...images],
files: [...files],
};
// Workspace/repo are optional β match the "Start from scratch" flow which
// creates a conversation with no working dir and no repo. Build the
// payload from whatever is selected.
// When attachments are present the first user message is sent afterward
// via sendMessageWithAttachments / flushPendingTaskAttachments. Passing
// query here would create a duplicate text-only initial_message.
let variables: Parameters<typeof createConversation>[0] = {
query: hasAttachments ? undefined : trimmed || undefined,
automationSetup: isAutomateMode,
entryPoint: "home_chat_launcher",
};
// An isolated backend owns its workspace, so a host selection left over
// from a non-isolated session must not be forwarded: the server rejects it
// (`HOME$ISOLATED_WORKSPACE_NOTICE`) and the user sees an error toast for a
// selection they may not have noticed. Creation proceeds isolated instead;
// the launcher still offers an explicit "clear" affordance for the UI.
if (isLocal && pendingWorkspace && !isolated) {
variables = {
...variables,
workingDir: pendingWorkspace.path,
workspaceMode,
};
} else if (!isLocal && pendingRepository && pendingBranch) {
variables = {
...variables,
repository: {
name: pendingRepository.full_name,
gitProvider: pendingRepository.git_provider,
branch: pendingBranch.name,
},
};
}
// Explicitly-attached plugins are additive on top of any ambient set and
// are resolved from git at run time. Omitted entirely when none selected so
// nothing attaches unless the user picked it.
if (!isAutomateMode && selectedPlugins.length > 0) {
variables = { ...variables, plugins: selectedPlugins };
}
// A cloud member's home LLM pick (see useChatInputLlmProfileState), unless
// that profile has since gone.
if (
homeLlmProfile &&
llmProfiles?.profiles.some((p) => p.name === homeLlmProfile)
) {
variables = { ...variables, llmProfile: homeLlmProfile };
}
const openAutomationSetupMode = (conversationId: string) => {
if (!isAutomateMode) return;
markAutomationSetupHandoff(conversationId);
};
// Loading toast gives the user a clear signal that the request is in
// flight; dismissed precisely once the mutation resolves.
const toastId = toast.loading(
t(I18nKey.HOME$CREATING_CONVERSATION),
TOAST_OPTIONS,
);
void (async () => {
let isCreated = false;
try {
const data = await createConversation(variables);
isCreated = true;
toast.dismiss(toastId);
try {
sessionStorage.removeItem(HOME_PROMPT_DRAFT_KEY);
} catch {
// sessionStorage not available
}
const targetConversationId = data.conversation_id;
const isTaskConversation = targetConversationId.startsWith("task-");
if (hasAttachments) {
// Cloud sandboxes provision asynchronously; uploads and the first
// message must target the runtime URL, not the bundled local server.
const shouldDeferAttachments = !isLocal || isTaskConversation;
if (shouldDeferAttachments) {
const taskId =
data.task_id ??
(isTaskConversation
? targetConversationId.slice("task-".length)
: null);
if (!taskId) {
displayErrorToast(null);
return;
}
setPendingTaskAttachments(taskId, {
content: trimmed,
images: attachmentSnapshot.images,
files: attachmentSnapshot.files,
imagesMarkedUploadAsFile: [...imagesMarkedUploadAsFile],
});
clearAllFiles();
await enqueueHomeTaskPendingMessage({
conversationId: targetConversationId,
text: trimmed,
images: attachmentSnapshot.images,
imagesMarkedUploadAsFile,
});
openAutomationSetupMode(targetConversationId);
navigate(`/conversations/${targetConversationId}`);
return;
} else {
try {
await sendMessageWithAttachments({
conversationId: targetConversationId,
content: trimmed,
images: attachmentSnapshot.images,
files: attachmentSnapshot.files,
imagesMarkedUploadAsFile,
t,
});
clearAllFiles();
} catch (error) {
displayErrorToast(error instanceof Error ? error.message : null);
return;
}
}
}
if (isTaskConversation && trimmed) {
await enqueueHomeTaskPendingMessage({
conversationId: targetConversationId,
text: trimmed,
images: [],
imagesMarkedUploadAsFile: [],
});
}
openAutomationSetupMode(targetConversationId);
navigate(`/conversations/${targetConversationId}`);
} catch (error) {
toast.dismiss(toastId);
// Prefer the server's own message; without one, keep the launcher's
// existing wording (the error's message).
const fallback = error instanceof Error ? error.message : "";
displayErrorToast(getApiErrorMessage(error, fallback) || null);
// The composer cleared itself on submit; hand the prompt back so the
// user can retry without retyping it. Only when the create itself
// failed and this composer is still on screen: the request is consumed
// by whichever composer is mounted, so otherwise the prompt would
// replay into a conversation.
if (!isCreated && isMountedRef.current) {
restoreMessageToInputIfEmpty(message);
}
}
})();
};
// Without this wrapper a `/model NAME` typed here would become the first
// user message of the new conversation. The interceptor activates the
// profile globally (null conversationId path) so the next conversation
// launches with it.
const handleSubmitWithModelGuard = useModelInterceptor(null, handleSubmit);
return (
<div
data-testid="home-chat-launcher"
className="flex w-full flex-col items-center pt-[max(4rem,28vh)] pb-10"
>
<div className="flex w-full max-w-200 flex-col gap-4 md:px-4">
<div className="flex w-full justify-center">
<HomeHeaderTitle />
</div>
<div className="flex w-full justify-center">
<HomeLauncherModeToggle
mode={displayedLauncherMode}
onChange={handleLauncherModeChange}
/>
</div>
<div className="w-full">
<CustomChatInput
onSubmit={handleSubmitWithModelGuard}
onFilesPaste={handleUpload}
disabled={isCreating || llmBlocked}
placeholder={
isAutomateMode
? t(I18nKey.HOME$AUTOMATE_PROMPT_PLACEHOLDER)
: t(I18nKey.SUGGESTIONS$WHAT_TO_BUILD)
}
/>
</div>
{isolated && (
<p role="status" className="text-xs text-[var(--oh-text-secondary)]">
{pendingWorkspace
? runtimeWorkspaceMessage
: t(I18nKey.HOME$ISOLATED_WORKSPACE_NEW)}
{pendingWorkspace && (
<button
type="button"
className="ml-2 underline"
onClick={() => setPendingWorkspace(null)}
>
{t(I18nKey.HOME$CLEAR_HOST_WORKSPACE)}
</button>
)}
</p>
)}
<div
data-testid="home-composer-actions"
aria-disabled={isAutomateMode}
className={cn(
"flex items-center justify-start gap-2",
isAutomateMode && "pointer-events-none opacity-40",
)}
>
{hasSelection ? (
<HomeGitControlBarPreview
workspace={pendingWorkspace}
repository={pendingRepository}
branch={pendingBranch}
provider={pendingProvider}
workspaceMode={workspaceMode}
backendKind={backend.kind}
onRepoClick={() => setIsDialogOpen(true)}
onWorkspaceModeChange={setWorkspaceMode}
/>
) : (
<OpenLauncherButton
kind={isLocal ? "local" : "cloud"}
onClick={() => setIsDialogOpen(true)}
disabled={
isCreating ||
isAutomateMode ||
Boolean(workspacesUnsupportedMessage)
}
disabledTooltip={workspacesUnsupportedMessage}
/>
)}
<PluginPickerTrigger
count={selectedPlugins.length}
onClick={() => setIsPluginPickerOpen(true)}
disabled={isCreating || isAutomateMode}
/>
</div>
<div className="mt-8 flex w-full flex-col gap-8">
{/* @spec HL-002 β Only Home's rail is dismissible; dismissal unmounts it, inset included */}
{!isRecommendedAutomationsDismissed && (
<RecommendedAutomationsLauncher
variant="rail"
dismissible
onDismiss={() => writeHomeRecommendedAutomationsDismissed(true)}
/>
)}
<PinnedAutomationsDashboard />
<RunningAutomationsList />
</div>
</div>
{isLocal ? (
<OpenWorkspaceDialog
isOpen={isDialogOpen}
onClose={() => setIsDialogOpen(false)}
onConfirm={(workspace) => {
setPendingWorkspace(workspace);
setPendingRepository(null);
setPendingBranch(null);
setPendingProvider(null);
}}
/>
) : (
<OpenRepositoryDialog
isOpen={isDialogOpen}
onClose={() => setIsDialogOpen(false)}
onConfirm={({ repository, branch, provider }) => {
setPendingRepository(repository);
setPendingBranch(branch);
setPendingProvider(provider ?? repository.git_provider);
setPendingWorkspace(null);
setWorkspaceModeState("local_repo");
}}
/>
)}
{isPluginPickerOpen && (
<PluginPickerModal
selected={selectedPlugins}
onChange={setSelectedPlugins}
onClose={() => setIsPluginPickerOpen(false)}
/>
)}
</div>
);
}