Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dev/src/utils/agent_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class AgentFile {
const rootApps = Object.values(jsModule).filter(isApp) as App[];

if (rootApps.length > 1) {
console.warn(
logger.warn(
`Multiple apps found in ${filePath}. Using the ${rootApps[0].name} as a root app.`,
);
}
Expand All @@ -278,7 +278,7 @@ export class AgentFile {
) as BaseAgent[];

if (rootAgents.length > 1) {
console.warn(
logger.warn(
`Multiple agents found in ${filePath}. Using the ${rootAgents[0].name} as a root agent.`,
);
}
Expand Down
54 changes: 51 additions & 3 deletions dev/test/utils/agent_loader_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
replaceDirnamePlugin,
} from '../../src/utils/agent_loader.js';
import * as fileUtils from '../../src/utils/file_utils.js';
import {AdkLogger} from '../../src/utils/logger.js';

vi.mock('../../src/utils/file_utils.js', () => ({
getTempDir: vi.fn(),
Expand Down Expand Up @@ -115,6 +116,25 @@ export const agent1 = new FakeAgent('agent1');
export const agent2 = new FakeAgent('agent2');
`;

const appMultipleExportsContent = `
import {App, BaseAgent} from '@google/adk';

class FakeAgentForApp extends BaseAgent {
constructor(name) {
super({name});
}
}

export const firstApp = new App({
name: 'test_app_multi_1',
rootAgent: new FakeAgentForApp('agent_for_app_1'),
});
export const secondApp = new App({
name: 'test_app_multi_2',
rootAgent: new FakeAgentForApp('agent_for_app_2'),
});
`;

const appJsContent = `
const {App, BaseAgent} = require('@google/adk');

Expand Down Expand Up @@ -447,16 +467,44 @@ describe('AgentLoader', () => {
return Promise.resolve();
});

const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const warnSpy = vi
.spyOn(AdkLogger.prototype, 'warn')
.mockImplementation(() => {});
const agentFile = new AgentFile(agentPath);
const agent = await agentFile.load();

expect(agent.name).toEqual('agent1');
expect(consoleSpy).toHaveBeenCalledWith(
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('Multiple agents found'),
);
await agentFile.dispose();
consoleSpy.mockRestore();
warnSpy.mockRestore();
});

it('loads first app if multiple apps exported', async () => {
const appPath = path.join(tempAgentsDir, 'app_multiple.js');
await fs.writeFile(appPath, appMultipleExportsContent);

const compiledAppPath = compiledPath('app_multiple.cjs');
(esbuild.build as Mock).mockImplementation(async () => {
await fs.writeFile(compiledAppPath, appMultipleExportsContent);
return Promise.resolve();
});

const warnSpy = vi
.spyOn(AdkLogger.prototype, 'warn')
.mockImplementation(() => {});
const agentFile = new AgentFile(appPath);
const loaded = await agentFile.load();

expect(isApp(loaded)).toBe(true);
expect((loaded as App).name).toBe('test_app_multi_1');
expect((loaded as App).rootAgent.name).toBe('agent_for_app_1');
expect(warnSpy).toHaveBeenCalledWith(
`Multiple apps found in ${compiledAppPath}. Using the test_app_multi_1 as a root app.`,
);
await agentFile.dispose();
warnSpy.mockRestore();
});

it('caches loaded agent instance', async () => {
Expand Down
Loading