diff --git a/src/a2a/__tests__/a2a-agent.test.ts b/src/a2a/__tests__/a2a-agent.test.ts index 4233d1170..deee08620 100644 --- a/src/a2a/__tests__/a2a-agent.test.ts +++ b/src/a2a/__tests__/a2a-agent.test.ts @@ -14,20 +14,25 @@ import type { InvokeArgs } from '../../types/agent.js' // Mock the A2A SDK client const mockSendMessageStream = vi.fn() const mockGetAgentCard = vi.fn() +const mockCreateFromUrl = vi.fn() -vi.mock('@a2a-js/sdk/client', () => ({ - ClientFactory: class MockClientFactory { - async createFromUrl(): Promise<{ - sendMessageStream: typeof mockSendMessageStream - getAgentCard: typeof mockGetAgentCard - }> { - return { - sendMessageStream: mockSendMessageStream, - getAgentCard: mockGetAgentCard, - } +// Track ClientFactory constructor calls +const clientFactoryConstructorArgs: unknown[][] = [] + +vi.mock('@a2a-js/sdk/client', () => { + class MockClientFactory { + constructor(...args: unknown[]) { + clientFactoryConstructorArgs.push(args) + } + async createFromUrl(...args: unknown[]) { + return mockCreateFromUrl(...args) } - }, -})) + } + + return { + ClientFactory: MockClientFactory, + } +}) const mockAgentCard: AgentCard = { name: 'Remote Agent', @@ -77,8 +82,13 @@ async function collectStream( describe('A2AAgent', () => { beforeEach(() => { vi.clearAllMocks() + clientFactoryConstructorArgs.length = 0 mockGetAgentCard.mockResolvedValue(mockAgentCard) mockSendMessageStream.mockReturnValue(mockStream(createMockTaskResponse())) + mockCreateFromUrl.mockResolvedValue({ + sendMessageStream: mockSendMessageStream, + getAgentCard: mockGetAgentCard, + }) }) describe('identity properties', () => { @@ -129,6 +139,65 @@ describe('A2AAgent', () => { }) }) + describe('clientFactory', () => { + it('creates a default ClientFactory when clientFactory is not provided', async () => { + const agent = new A2AAgent({ url: 'http://localhost:9000' }) + + await agent.invoke('Hello') + + // Default factory created with no arguments + expect(clientFactoryConstructorArgs).toHaveLength(1) + expect(clientFactoryConstructorArgs[0]).toHaveLength(0) + }) + + it('uses the provided ClientFactory instead of creating a new one', async () => { + const { ClientFactory } = await import('@a2a-js/sdk/client') + const customFactory = new ClientFactory() + + // Reset tracking after factory construction + clientFactoryConstructorArgs.length = 0 + + const agent = new A2AAgent({ + url: 'http://localhost:9000', + clientFactory: customFactory, + }) + + await agent.invoke('Hello') + + // No new factory created — the provided one is used directly + expect(clientFactoryConstructorArgs).toHaveLength(0) + expect(mockCreateFromUrl).toHaveBeenCalledWith('http://localhost:9000', undefined) + }) + + it('passes agentCardPath to createFromUrl', async () => { + const agent = new A2AAgent({ + url: 'http://localhost:9000', + agentCardPath: '/custom/card.json', + }) + + await agent.invoke('Hello') + + expect(mockCreateFromUrl).toHaveBeenCalledWith('http://localhost:9000', '/custom/card.json') + }) + + it('passes agentCardPath to createFromUrl with custom factory', async () => { + const { ClientFactory } = await import('@a2a-js/sdk/client') + const customFactory = new ClientFactory() + clientFactoryConstructorArgs.length = 0 + + const agent = new A2AAgent({ + url: 'http://localhost:9000', + agentCardPath: '/custom/card.json', + clientFactory: customFactory, + }) + + await agent.invoke('Hello') + + expect(clientFactoryConstructorArgs).toHaveLength(0) + expect(mockCreateFromUrl).toHaveBeenCalledWith('http://localhost:9000', '/custom/card.json') + }) + }) + describe('invoke', () => { it('returns AgentResult with response text', async () => { const agent = new A2AAgent({ url: 'http://localhost:9000' }) @@ -243,14 +312,12 @@ describe('A2AAgent', () => { const agent = new A2AAgent({ url: 'http://localhost:9000' }) const { events } = await collectStream(agent.stream('Hello')) - // 3 A2AStreamUpdateEvents + 1 A2AResultEvent expect(events).toHaveLength(4) expect(events[0]).toBeInstanceOf(A2AStreamUpdateEvent) expect(events[1]).toBeInstanceOf(A2AStreamUpdateEvent) expect(events[2]).toBeInstanceOf(A2AStreamUpdateEvent) expect(events[3]).toBeInstanceOf(A2AResultEvent) - // Final result built from last complete event (status-update with completed state) const resultEvent = events[3] as A2AResultEvent expect((resultEvent.result.lastMessage.content[0] as TextBlock).text).toBe('Final answer') }) @@ -306,7 +373,7 @@ describe('A2AAgent', () => { const agent = new A2AAgent({ url: 'http://localhost:9000' }) const { events, result } = await collectStream(agent.stream('Hello')) - expect(events).toHaveLength(1) // only A2AResultEvent + expect(events).toHaveLength(1) expect(events[0]).toBeInstanceOf(A2AResultEvent) expect((result as { lastMessage: Message }).lastMessage.content[0]).toBeInstanceOf(TextBlock) expect(((result as { lastMessage: Message }).lastMessage.content[0] as TextBlock).text).toBe('') @@ -339,7 +406,12 @@ describe('A2AAgent', () => { describe('response text extraction via invoke', () => { beforeEach(() => { vi.clearAllMocks() + clientFactoryConstructorArgs.length = 0 mockGetAgentCard.mockResolvedValue(mockAgentCard) + mockCreateFromUrl.mockResolvedValue({ + sendMessageStream: mockSendMessageStream, + getAgentCard: mockGetAgentCard, + }) }) it('joins multiple text parts from Task artifacts', async () => { diff --git a/src/a2a/a2a-agent.ts b/src/a2a/a2a-agent.ts index 2ee4adde8..4524c79c0 100644 --- a/src/a2a/a2a-agent.ts +++ b/src/a2a/a2a-agent.ts @@ -31,6 +31,30 @@ export interface A2AAgentConfig { name?: string /** Optional description. If not provided, populated from the agent card after connection. */ description?: string + /** + * Pre-configured {@link ClientFactory} for the underlying A2A SDK. + * + * Use this to configure authentication, custom transports, interceptors, + * or a custom agent card resolver. When not provided, a default + * `ClientFactory` is created with no authentication. + * + * @example + * ```typescript + * import { ClientFactory, ClientFactoryOptions, DefaultAgentCardResolver, createAuthenticatingFetchWithRetry } from '@a2a-js/sdk/client' + * + * const factory = new ClientFactory(ClientFactoryOptions.createFrom(ClientFactoryOptions.default, { + * cardResolver: new DefaultAgentCardResolver({ + * fetchImpl: createAuthenticatingFetchWithRetry(fetch, myAuthHandler), + * }), + * })) + * + * const agent = new A2AAgent({ + * url: 'https://protected-agent.example.com', + * clientFactory: factory, + * }) + * ``` + */ + clientFactory?: ClientFactory } /** @@ -170,7 +194,7 @@ export class A2AAgent implements InvokableAgent { logExperimentalWarning() - const factory = new ClientFactory() + const factory = this._config.clientFactory ?? new ClientFactory() const client = await factory.createFromUrl(this._config.url, this._config.agentCardPath) this._agentCard = await client.getAgentCard() if (this.name === undefined && this._agentCard?.name) {