143 lines
4.3 KiB
JavaScript
143 lines
4.3 KiB
JavaScript
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
const require_CopilotChatConfigurationProvider = require('../providers/CopilotChatConfigurationProvider.cjs');
|
|
const require_CopilotKitProvider = require('../providers/CopilotKitProvider.cjs');
|
|
let react = require("react");
|
|
let _copilotkitnext_shared = require("@copilotkitnext/shared");
|
|
|
|
//#region src/hooks/use-configure-suggestions.tsx
|
|
function useConfigureSuggestions(config, deps) {
|
|
const { copilotkit } = require_CopilotKitProvider.useCopilotKit();
|
|
const chatConfig = require_CopilotChatConfigurationProvider.useCopilotChatConfiguration();
|
|
const extraDeps = deps ?? [];
|
|
const resolvedConsumerAgentId = (0, react.useMemo)(() => chatConfig?.agentId ?? _copilotkitnext_shared.DEFAULT_AGENT_ID, [chatConfig?.agentId]);
|
|
const rawConsumerAgentId = (0, react.useMemo)(() => config ? config.consumerAgentId : void 0, [config]);
|
|
const normalizationCacheRef = (0, react.useRef)({
|
|
serialized: null,
|
|
config: null
|
|
});
|
|
const { normalizedConfig, serializedConfig } = (0, react.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 = (0, react.useRef)(null);
|
|
latestConfigRef.current = normalizedConfig;
|
|
const previousSerializedConfigRef = (0, react.useRef)(null);
|
|
const targetAgentId = (0, react.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 = (0, react.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
|
|
]);
|
|
(0, react.useEffect)(() => {
|
|
if (!serializedConfig || !latestConfigRef.current) return;
|
|
const id = copilotkit.addSuggestionsConfig(latestConfigRef.current);
|
|
requestReload();
|
|
return () => {
|
|
copilotkit.removeSuggestionsConfig(id);
|
|
};
|
|
}, [
|
|
copilotkit,
|
|
serializedConfig,
|
|
requestReload
|
|
]);
|
|
(0, react.useEffect)(() => {
|
|
if (!normalizedConfig) {
|
|
previousSerializedConfigRef.current = null;
|
|
return;
|
|
}
|
|
if (serializedConfig && previousSerializedConfigRef.current === serializedConfig) return;
|
|
if (serializedConfig) previousSerializedConfigRef.current = serializedConfig;
|
|
requestReload();
|
|
}, [
|
|
normalizedConfig,
|
|
requestReload,
|
|
serializedConfig
|
|
]);
|
|
(0, react.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
|
|
exports.useConfigureSuggestions = useConfigureSuggestions;
|
|
//# sourceMappingURL=use-configure-suggestions.cjs.map
|