const require_runtime = require('../_virtual/_rolldown/runtime.cjs'); const require_CopilotKitProvider = require('../providers/CopilotKitProvider.cjs'); let react = require("react"); let _copilotkitnext_shared = require("@copilotkitnext/shared"); let _copilotkitnext_core = require("@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 ??= _copilotkitnext_shared.DEFAULT_AGENT_ID; const { copilotkit } = require_CopilotKitProvider.useCopilotKit(); const [, forceUpdate] = (0, react.useReducer)((x) => x + 1, 0); const updateFlags = (0, react.useMemo)(() => updates ?? ALL_UPDATES, [JSON.stringify(updates)]); const provisionalAgentCache = (0, react.useRef)(/* @__PURE__ */ new Map()); const agent = (0, react.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 === _copilotkitnext_core.CopilotKitCoreRuntimeConnectionStatus.Disconnected || status === _copilotkitnext_core.CopilotKitCoreRuntimeConnectionStatus.Connecting)) { const cached = provisionalAgentCache.current.get(agentId); if (cached) { cached.headers = { ...copilotkit.headers }; return cached; } const provisional = new _copilotkitnext_core.ProxiedCopilotRuntimeAgent({ runtimeUrl: copilotkit.runtimeUrl, agentId, transport: copilotkit.runtimeTransport }); provisional.headers = { ...copilotkit.headers }; provisionalAgentCache.current.set(agentId, provisional); return provisional; } if (isRuntimeConfigured && status === _copilotkitnext_core.CopilotKitCoreRuntimeConnectionStatus.Error) { const provisional = new _copilotkitnext_core.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) ]); (0, react.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 exports.UseAgentUpdate = UseAgentUpdate; exports.useAgent = useAgent; //# sourceMappingURL=use-agent.cjs.map