60 lines
2.2 KiB
JavaScript
60 lines
2.2 KiB
JavaScript
import { useCopilotChatConfiguration } from "../providers/CopilotChatConfigurationProvider.mjs";
|
|
import { useCopilotKit } from "../providers/CopilotKitProvider.mjs";
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { DEFAULT_AGENT_ID } from "@copilotkitnext/shared";
|
|
|
|
//#region src/hooks/use-suggestions.tsx
|
|
function useSuggestions({ agentId } = {}) {
|
|
const { copilotkit } = useCopilotKit();
|
|
const config = useCopilotChatConfiguration();
|
|
const resolvedAgentId = useMemo(() => agentId ?? config?.agentId ?? DEFAULT_AGENT_ID, [agentId, config?.agentId]);
|
|
const [suggestions, setSuggestions] = useState(() => {
|
|
return copilotkit.getSuggestions(resolvedAgentId).suggestions;
|
|
});
|
|
const [isLoading, setIsLoading] = useState(() => {
|
|
return copilotkit.getSuggestions(resolvedAgentId).isLoading;
|
|
});
|
|
useEffect(() => {
|
|
const result = copilotkit.getSuggestions(resolvedAgentId);
|
|
setSuggestions(result.suggestions);
|
|
setIsLoading(result.isLoading);
|
|
}, [copilotkit, resolvedAgentId]);
|
|
useEffect(() => {
|
|
const subscription = copilotkit.subscribe({
|
|
onSuggestionsChanged: ({ agentId: changedAgentId, suggestions }) => {
|
|
if (changedAgentId !== resolvedAgentId) return;
|
|
setSuggestions(suggestions);
|
|
},
|
|
onSuggestionsStartedLoading: ({ agentId: changedAgentId }) => {
|
|
if (changedAgentId !== resolvedAgentId) return;
|
|
setIsLoading(true);
|
|
},
|
|
onSuggestionsFinishedLoading: ({ agentId: changedAgentId }) => {
|
|
if (changedAgentId !== resolvedAgentId) return;
|
|
setIsLoading(false);
|
|
},
|
|
onSuggestionsConfigChanged: () => {
|
|
const result = copilotkit.getSuggestions(resolvedAgentId);
|
|
setSuggestions(result.suggestions);
|
|
setIsLoading(result.isLoading);
|
|
}
|
|
});
|
|
return () => {
|
|
subscription.unsubscribe();
|
|
};
|
|
}, [copilotkit, resolvedAgentId]);
|
|
return {
|
|
suggestions,
|
|
reloadSuggestions: useCallback(() => {
|
|
copilotkit.reloadSuggestions(resolvedAgentId);
|
|
}, [copilotkit, resolvedAgentId]),
|
|
clearSuggestions: useCallback(() => {
|
|
copilotkit.clearSuggestions(resolvedAgentId);
|
|
}, [copilotkit, resolvedAgentId]),
|
|
isLoading
|
|
};
|
|
}
|
|
|
|
//#endregion
|
|
export { useSuggestions };
|
|
//# sourceMappingURL=use-suggestions.mjs.map
|