rdesign/frontend/node_modules/@copilotkitnext/react/dist/hooks/use-agent.mjs

91 lines
3.5 KiB
JavaScript

import { useCopilotKit } from "../providers/CopilotKitProvider.mjs";
import { useEffect, useMemo, useReducer, useRef } from "react";
import { DEFAULT_AGENT_ID } from "@copilotkitnext/shared";
import { CopilotKitCoreRuntimeConnectionStatus, ProxiedCopilotRuntimeAgent } from "@copilotkitnext/core";
//#region src/hooks/use-agent.tsx
let UseAgentUpdate = /* @__PURE__ */ function(UseAgentUpdate) {
UseAgentUpdate["OnMessagesChanged"] = "OnMessagesChanged";
UseAgentUpdate["OnStateChanged"] = "OnStateChanged";
UseAgentUpdate["OnRunStatusChanged"] = "OnRunStatusChanged";
return UseAgentUpdate;
}({});
const ALL_UPDATES = [
UseAgentUpdate.OnMessagesChanged,
UseAgentUpdate.OnStateChanged,
UseAgentUpdate.OnRunStatusChanged
];
function useAgent({ agentId, updates } = {}) {
agentId ??= DEFAULT_AGENT_ID;
const { copilotkit } = useCopilotKit();
const [, forceUpdate] = useReducer((x) => x + 1, 0);
const updateFlags = useMemo(() => updates ?? ALL_UPDATES, [JSON.stringify(updates)]);
const provisionalAgentCache = useRef(/* @__PURE__ */ new Map());
const agent = useMemo(() => {
const existing = copilotkit.getAgent(agentId);
if (existing) {
provisionalAgentCache.current.delete(agentId);
return existing;
}
const isRuntimeConfigured = copilotkit.runtimeUrl !== void 0;
const status = copilotkit.runtimeConnectionStatus;
if (isRuntimeConfigured && (status === CopilotKitCoreRuntimeConnectionStatus.Disconnected || status === CopilotKitCoreRuntimeConnectionStatus.Connecting)) {
const cached = provisionalAgentCache.current.get(agentId);
if (cached) {
cached.headers = { ...copilotkit.headers };
return cached;
}
const provisional = new ProxiedCopilotRuntimeAgent({
runtimeUrl: copilotkit.runtimeUrl,
agentId,
transport: copilotkit.runtimeTransport
});
provisional.headers = { ...copilotkit.headers };
provisionalAgentCache.current.set(agentId, provisional);
return provisional;
}
if (isRuntimeConfigured && status === CopilotKitCoreRuntimeConnectionStatus.Error) {
const provisional = new ProxiedCopilotRuntimeAgent({
runtimeUrl: copilotkit.runtimeUrl,
agentId,
transport: copilotkit.runtimeTransport
});
provisional.headers = { ...copilotkit.headers };
return provisional;
}
const knownAgents = Object.keys(copilotkit.agents ?? {});
const runtimePart = isRuntimeConfigured ? `runtimeUrl=${copilotkit.runtimeUrl}` : "no runtimeUrl";
throw new Error(`useAgent: Agent '${agentId}' not found after runtime sync (${runtimePart}). ` + (knownAgents.length ? `Known agents: [${knownAgents.join(", ")}]` : "No agents registered.") + " Verify your runtime /info and/or agents__unsafe_dev_only.");
}, [
agentId,
copilotkit.agents,
copilotkit.runtimeConnectionStatus,
copilotkit.runtimeUrl,
copilotkit.runtimeTransport,
JSON.stringify(copilotkit.headers)
]);
useEffect(() => {
if (updateFlags.length === 0) return;
const handlers = {};
if (updateFlags.includes(UseAgentUpdate.OnMessagesChanged)) handlers.onMessagesChanged = () => {
forceUpdate();
};
if (updateFlags.includes(UseAgentUpdate.OnStateChanged)) handlers.onStateChanged = forceUpdate;
if (updateFlags.includes(UseAgentUpdate.OnRunStatusChanged)) {
handlers.onRunInitialized = forceUpdate;
handlers.onRunFinalized = forceUpdate;
handlers.onRunFailed = forceUpdate;
}
const subscription = agent.subscribe(handlers);
return () => subscription.unsubscribe();
}, [
agent,
forceUpdate,
JSON.stringify(updateFlags)
]);
return { agent };
}
//#endregion
export { UseAgentUpdate, useAgent };
//# sourceMappingURL=use-agent.mjs.map