Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
108 changes: 93 additions & 15 deletions src/a2a/__tests__/a2a-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,34 @@ 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)
}
},
}))
}

const mockCreateFrom = vi.fn().mockImplementation((_original: unknown, overrides: unknown) => ({
transports: [],
...(overrides as Record<string, unknown>),
}))

return {
ClientFactory: MockClientFactory,
ClientFactoryOptions: {
default: { transports: [] },
createFrom: mockCreateFrom,
},
}
})

const mockAgentCard: AgentCard = {
name: 'Remote Agent',
Expand Down Expand Up @@ -77,8 +91,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', () => {
Expand Down Expand Up @@ -129,6 +148,62 @@ describe('A2AAgent', () => {
})
})

describe('clientFactoryOptions', () => {
it('creates ClientFactory with no arguments when clientFactoryOptions is not provided', async () => {
const agent = new A2AAgent({ url: 'http://localhost:9000' })

await agent.invoke('Hello')

expect(clientFactoryConstructorArgs).toHaveLength(1)
expect(clientFactoryConstructorArgs[0]).toHaveLength(0)
})

it('creates ClientFactory with merged options when clientFactoryOptions is provided', async () => {
const { ClientFactoryOptions } = await import('@a2a-js/sdk/client')
const customOptions = {
clientConfig: { interceptors: [] },
}
const agent = new A2AAgent({
url: 'http://localhost:9000',
clientFactoryOptions: customOptions,
})

await agent.invoke('Hello')

expect(ClientFactoryOptions.createFrom).toHaveBeenCalledWith(ClientFactoryOptions.default, customOptions)
expect(clientFactoryConstructorArgs).toHaveLength(1)
expect(clientFactoryConstructorArgs[0]![0]).toEqual(
expect.objectContaining({ clientConfig: { interceptors: [] } })
)
})

it('passes custom cardResolver through to ClientFactory', async () => {
const { ClientFactoryOptions } = await import('@a2a-js/sdk/client')
const mockResolver = { resolve: vi.fn() }
const agent = new A2AAgent({
url: 'http://localhost:9000',
clientFactoryOptions: { cardResolver: mockResolver },
})

await agent.invoke('Hello')

expect(ClientFactoryOptions.createFrom).toHaveBeenCalledWith(ClientFactoryOptions.default, {
cardResolver: mockResolver,
})
})

it('passes agentCardPath to createFromUrl', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: The agentCardPath test is in the clientFactoryOptions describe block but doesn't actually test clientFactoryOptions behavior — it tests the existing agentCardPath config passthrough to createFromUrl. This makes the test grouping misleading.

Suggestion: Move this test to a more appropriate describe block (e.g., the identity properties or invoke block), or rename the describe to something broader like client configuration.

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')
})
})

describe('invoke', () => {
it('returns AgentResult with response text', async () => {
const agent = new A2AAgent({ url: 'http://localhost:9000' })
Expand Down Expand Up @@ -243,14 +318,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')
})
Expand Down Expand Up @@ -306,7 +379,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('')
Expand Down Expand Up @@ -339,7 +412,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 () => {
Expand Down
29 changes: 27 additions & 2 deletions src/a2a/a2a-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import type { AgentCard, Part } from '@a2a-js/sdk'
import type { Client as A2AClientSdk } from '@a2a-js/sdk/client'
import { ClientFactory } from '@a2a-js/sdk/client'
import { ClientFactory, ClientFactoryOptions } from '@a2a-js/sdk/client'
import type { InvokableAgent, InvokeArgs, InvokeOptions } from '../types/agent.js'
import { AgentResult } from '../types/agent.js'
import { Message, TextBlock, type ContentBlock, type ContentBlockData, type MessageData } from '../types/messages.js'
Expand All @@ -31,6 +31,28 @@ export interface A2AAgentConfig {
name?: string
/** Optional description. If not provided, populated from the agent card after connection. */
description?: string
/**
* Options passed to the underlying A2A SDK {@link ClientFactory}.
*
* Use this to configure authentication, custom transports, interceptors,
* or a custom agent card resolver. Partial options are merged with the SDK
* defaults via `ClientFactoryOptions.createFrom`.
*
* @example
* ```typescript
* import { DefaultAgentCardResolver, createAuthenticatingFetchWithRetry } from '@a2a-js/sdk/client'
*
* const agent = new A2AAgent({
* url: 'https://protected-agent.example.com',
* clientFactoryOptions: {
* cardResolver: new DefaultAgentCardResolver({
* fetchImpl: createAuthenticatingFetchWithRetry(fetch, myAuthHandler),
* }),
* },
* })
* ```
*/
clientFactoryOptions?: Partial<ClientFactoryOptions>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: ClientFactoryOptions is not re-exported from src/a2a/index.ts. Users who want to construct clientFactoryOptions values will need to import the type directly from @a2a-js/sdk/client, which is not discoverable and requires knowledge of the underlying dependency.

Suggestion: Re-export ClientFactoryOptions from src/a2a/index.ts so users can import everything they need from @strands-agents/sdk/a2a:

export { ClientFactoryOptions } from '@a2a-js/sdk/client'

This aligns with the "Prefer Flat Namespaces Over Nested Modules" decision record — users shouldn't need to import from transitive dependencies for common functionality.

}

/**
Expand Down Expand Up @@ -170,7 +192,10 @@ export class A2AAgent implements InvokableAgent {

logExperimentalWarning()

const factory = new ClientFactory()
const factoryOptions = this._config.clientFactoryOptions
const factory = factoryOptions
? new ClientFactory(ClientFactoryOptions.createFrom(ClientFactoryOptions.default, factoryOptions))
: 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) {
Expand Down
Loading