({
runtimeUrl,
runtimeTransport = "auto",
headers = {},
credentials,
properties = {},
agents__unsafe_dev_only = {},
tools = [],
suggestionsConfig = [],
debug,
}: CopilotKitCoreConfig)
| 386 | private previousAgentIds: Set<string> = new Set(); |
| 387 | |
| 388 | constructor({ |
| 389 | runtimeUrl, |
| 390 | runtimeTransport = "auto", |
| 391 | headers = {}, |
| 392 | credentials, |
| 393 | properties = {}, |
| 394 | agents__unsafe_dev_only = {}, |
| 395 | tools = [], |
| 396 | suggestionsConfig = [], |
| 397 | debug, |
| 398 | }: CopilotKitCoreConfig) { |
| 399 | this._headers = normalizeHeaders(headers); |
| 400 | this._credentials = credentials; |
| 401 | this._properties = properties; |
| 402 | this._debug = debug; |
| 403 | |
| 404 | // Initialize delegate classes |
| 405 | this.agentRegistry = new AgentRegistry(this); |
| 406 | this.contextStore = new ContextStore(this); |
| 407 | this.suggestionEngine = new SuggestionEngine(this); |
| 408 | this.runHandler = new RunHandler(this); |
| 409 | this.stateManager = new StateManager(this); |
| 410 | this.threadStoreRegistry = new ThreadStoreRegistry(this); |
| 411 | |
| 412 | // Initialize each subsystem |
| 413 | this.agentRegistry.initialize(agents__unsafe_dev_only); |
| 414 | this.runHandler.initialize(tools); |
| 415 | this.suggestionEngine.initialize(suggestionsConfig); |
| 416 | this.stateManager.initialize(); |
| 417 | |
| 418 | this.agentRegistry.setRuntimeTransport(runtimeTransport); |
| 419 | this.agentRegistry.setRuntimeUrl(runtimeUrl); |
| 420 | |
| 421 | // Seed the previous-agents snapshot from the constructor-supplied agents. |
| 422 | // `agentRegistry.initialize` does not emit `onAgentsChanged`, so the |
| 423 | // subscriber below would otherwise see its first non-empty notification |
| 424 | // as an "addition" relative to an empty baseline — and a later removal |
| 425 | // of those same agents would NOT trigger the auto-unregister branch |
| 426 | // because the guard would think the agentId was never previously |
| 427 | // present. Seeding the set here keeps the guard honest. |
| 428 | this.previousAgentIds = new Set(Object.keys(agents__unsafe_dev_only)); |
| 429 | |
| 430 | // Subscribe to agent changes to track state for new agents |
| 431 | this.subscribe({ |
| 432 | onAgentsChanged: ({ agents }) => { |
| 433 | Object.values(agents).forEach((agent) => { |
| 434 | if (agent.agentId) { |
| 435 | this.stateManager.subscribeToAgent(agent); |
| 436 | } |
| 437 | }); |
| 438 | |
| 439 | // Unregister thread stores for agents that have been removed. |
| 440 | // |
| 441 | // Critically, only unregister an agentId that was present in the |
| 442 | // PREVIOUS agents snapshot AND is missing from the new one. Without |
| 443 | // the "previously had" guard, the FIRST `onAgentsChanged({ agents: {} })` |
| 444 | // delivered to a freshly-published core would tear out a thread store |
| 445 | // that a consumer (e.g. useThreads) just registered — `core.agents` |
nothing calls this directly
no test coverage detected