From 003609239fe81c8a2920ed0c63b7f5142bb4f7e5 Mon Sep 17 00:00:00 2001 From: Tommaso Sciortino Date: Fri, 18 Jul 2025 15:38:04 -0700 Subject: Add /background commands (when background agent is configured) (#4407) Co-authored-by: Bryan Morgan --- packages/core/src/background/backgroundManager.ts | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 packages/core/src/background/backgroundManager.ts (limited to 'packages/core/src/background/backgroundManager.ts') diff --git a/packages/core/src/background/backgroundManager.ts b/packages/core/src/background/backgroundManager.ts new file mode 100644 index 00000000..a3ec526c --- /dev/null +++ b/packages/core/src/background/backgroundManager.ts @@ -0,0 +1,40 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { MCPServerConfig } from '../config/config.js'; +import { BackgroundAgent, loadBackgroundAgent } from './backgroundAgent.js'; + +export async function loadBackgroundAgentManager( + backgroundAgentConfigs: Record | undefined, + debugMode: boolean, +): Promise { + const agents = await Promise.all( + Object.entries(backgroundAgentConfigs ?? {}).map(([name, config]) => + loadBackgroundAgent(name, config, debugMode).catch((error) => { + console.error(`Error loading background agent '${name}': ${error}`); + return null; + }), + ), + ).then((agents) => agents.filter((agent) => agent !== null)); + return new BackgroundAgentManager(agents); +} + +export class BackgroundAgentManager { + // The active agent. May be empty if none are confgured. + activeAgent?: BackgroundAgent; + + constructor(readonly backgroundAgents: BackgroundAgent[]) { + if (backgroundAgents.length !== 0) { + this.activeAgent = backgroundAgents[0]; + } + } + + setActiveAgentByName(name: string) { + this.activeAgent = this.backgroundAgents.find( + (agent) => agent.serverName === name, + ); + } +} -- cgit v1.2.3