File size: 2,012 Bytes
9332ccf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type React from 'react';
import { createContext, useContext, useMemo } from 'react';
import type { Question } from '@google/gemini-cli-core';
export interface AskUserState {
questions: Question[];
correlationId: string;
}
interface AskUserActionsContextValue {
/** Current ask_user request, or null if no dialog should be shown */
request: AskUserState | null;
/** Submit answers - publishes ASK_USER_RESPONSE to message bus */
submit: (answers: { [questionIndex: string]: string }) => Promise<void>;
/** Cancel the dialog - clears request state */
cancel: () => void;
}
export const AskUserActionsContext =
createContext<AskUserActionsContextValue | null>(null);
export const useAskUserActions = () => {
const context = useContext(AskUserActionsContext);
if (!context) {
throw new Error(
'useAskUserActions must be used within an AskUserActionsProvider',
);
}
return context;
};
interface AskUserActionsProviderProps {
children: React.ReactNode;
/** Current ask_user request state (managed by AppContainer) */
request: AskUserState | null;
/** Handler to submit answers */
onSubmit: (answers: { [questionIndex: string]: string }) => Promise<void>;
/** Handler to cancel the dialog */
onCancel: () => void;
}
/**
* Provides ask_user dialog state and actions to child components.
*
* State is managed by AppContainer (which subscribes to the message bus)
* and passed here as props. This follows the same pattern as ToolActionsProvider.
*/
export const AskUserActionsProvider: React.FC<AskUserActionsProviderProps> = ({
children,
request,
onSubmit,
onCancel,
}) => {
const value = useMemo(
() => ({
request,
submit: onSubmit,
cancel: onCancel,
}),
[request, onSubmit, onCancel],
);
return (
<AskUserActionsContext.Provider value={value}>
{children}
</AskUserActionsContext.Provider>
);
};
|