142 lines
4.0 KiB
JavaScript
142 lines
4.0 KiB
JavaScript
import { useCopilotChatConfiguration } from "../providers/CopilotChatConfigurationProvider.mjs";
|
|
import { useCopilotKit } from "../providers/CopilotKitProvider.mjs";
|
|
import { useCallback, useEffect, useMemo, useRef } from "react";
|
|
import { DEFAULT_AGENT_ID } from "@copilotkitnext/shared";
|
|
|
|
//#region src/hooks/use-configure-suggestions.tsx
|
|
function useConfigureSuggestions(config, deps) {
|
|
const { copilotkit } = useCopilotKit();
|
|
const chatConfig = useCopilotChatConfiguration();
|
|
const extraDeps = deps ?? [];
|
|
const resolvedConsumerAgentId = useMemo(() => chatConfig?.agentId ?? DEFAULT_AGENT_ID, [chatConfig?.agentId]);
|
|
const rawConsumerAgentId = useMemo(() => config ? config.consumerAgentId : void 0, [config]);
|
|
const normalizationCacheRef = useRef({
|
|
serialized: null,
|
|
config: null
|
|
});
|
|
const { normalizedConfig, serializedConfig } = useMemo(() => {
|
|
if (!config) {
|
|
normalizationCacheRef.current = {
|
|
serialized: null,
|
|
config: null
|
|
};
|
|
return {
|
|
normalizedConfig: null,
|
|
serializedConfig: null
|
|
};
|
|
}
|
|
if (config.available === "disabled") {
|
|
normalizationCacheRef.current = {
|
|
serialized: null,
|
|
config: null
|
|
};
|
|
return {
|
|
normalizedConfig: null,
|
|
serializedConfig: null
|
|
};
|
|
}
|
|
let built;
|
|
if (isDynamicConfig(config)) built = { ...config };
|
|
else {
|
|
const normalizedSuggestions = normalizeStaticSuggestions(config.suggestions);
|
|
built = {
|
|
...config,
|
|
suggestions: normalizedSuggestions
|
|
};
|
|
}
|
|
const serialized = JSON.stringify(built);
|
|
const cache = normalizationCacheRef.current;
|
|
if (cache.serialized === serialized && cache.config) return {
|
|
normalizedConfig: cache.config,
|
|
serializedConfig: serialized
|
|
};
|
|
normalizationCacheRef.current = {
|
|
serialized,
|
|
config: built
|
|
};
|
|
return {
|
|
normalizedConfig: built,
|
|
serializedConfig: serialized
|
|
};
|
|
}, [
|
|
config,
|
|
resolvedConsumerAgentId,
|
|
...extraDeps
|
|
]);
|
|
const latestConfigRef = useRef(null);
|
|
latestConfigRef.current = normalizedConfig;
|
|
const previousSerializedConfigRef = useRef(null);
|
|
const targetAgentId = useMemo(() => {
|
|
if (!normalizedConfig) return resolvedConsumerAgentId;
|
|
const consumer = normalizedConfig.consumerAgentId;
|
|
if (!consumer || consumer === "*") return resolvedConsumerAgentId;
|
|
return consumer;
|
|
}, [normalizedConfig, resolvedConsumerAgentId]);
|
|
const isGlobalConfig = rawConsumerAgentId === void 0 || rawConsumerAgentId === "*";
|
|
const requestReload = useCallback(() => {
|
|
if (!normalizedConfig) return;
|
|
if (isGlobalConfig) {
|
|
const agents = Object.values(copilotkit.agents ?? {});
|
|
for (const entry of agents) {
|
|
const agentId = entry.agentId;
|
|
if (!agentId) continue;
|
|
if (!entry.isRunning) copilotkit.reloadSuggestions(agentId);
|
|
}
|
|
return;
|
|
}
|
|
if (!targetAgentId) return;
|
|
copilotkit.reloadSuggestions(targetAgentId);
|
|
}, [
|
|
copilotkit,
|
|
isGlobalConfig,
|
|
normalizedConfig,
|
|
targetAgentId
|
|
]);
|
|
useEffect(() => {
|
|
if (!serializedConfig || !latestConfigRef.current) return;
|
|
const id = copilotkit.addSuggestionsConfig(latestConfigRef.current);
|
|
requestReload();
|
|
return () => {
|
|
copilotkit.removeSuggestionsConfig(id);
|
|
};
|
|
}, [
|
|
copilotkit,
|
|
serializedConfig,
|
|
requestReload
|
|
]);
|
|
useEffect(() => {
|
|
if (!normalizedConfig) {
|
|
previousSerializedConfigRef.current = null;
|
|
return;
|
|
}
|
|
if (serializedConfig && previousSerializedConfigRef.current === serializedConfig) return;
|
|
if (serializedConfig) previousSerializedConfigRef.current = serializedConfig;
|
|
requestReload();
|
|
}, [
|
|
normalizedConfig,
|
|
requestReload,
|
|
serializedConfig
|
|
]);
|
|
useEffect(() => {
|
|
if (!normalizedConfig || extraDeps.length === 0) return;
|
|
requestReload();
|
|
}, [
|
|
extraDeps.length,
|
|
normalizedConfig,
|
|
requestReload,
|
|
...extraDeps
|
|
]);
|
|
}
|
|
function isDynamicConfig(config) {
|
|
return "instructions" in config;
|
|
}
|
|
function normalizeStaticSuggestions(suggestions) {
|
|
return suggestions.map((suggestion) => ({
|
|
...suggestion,
|
|
isLoading: suggestion.isLoading ?? false
|
|
}));
|
|
}
|
|
|
|
//#endregion
|
|
export { useConfigureSuggestions };
|
|
//# sourceMappingURL=use-configure-suggestions.mjs.map
|