Skip to content
Merged
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
11 changes: 11 additions & 0 deletions src/client/interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AgentCard } from '../types.js';
import { A2AStreamEventData } from './client.js';
import { Client } from './multitransport-client.js';
import { RequestOptions } from './multitransport-client.js';
Expand All @@ -21,6 +22,11 @@ export interface BeforeArgs<K extends keyof Client = keyof Client> {
*/
readonly input: ClientCallInput<K>;

/**
* Identifies the agent card cached on the client
*/
readonly agentCard: AgentCard;

/**
* If set by the interceptor, stops execution, invokes "after"
* for executed interceptors and returns the result. Transport is not called.
Expand All @@ -40,6 +46,11 @@ export interface AfterArgs<K extends keyof Client = keyof Client> {
*/
readonly result: ClientCallResult<K>;

/**
* Identifies the agent card cached on the client
*/
readonly agentCard: AgentCard;

/**
* If set by the interceptor, stops execution and returns result value,
* remaining interceptors are not executed.
Expand Down
15 changes: 14 additions & 1 deletion src/client/multitransport-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class Client {
params = this.applyClientConfig({ params, blocking: true });
const beforeArgs: BeforeArgs<'sendMessageStream'> = {
input: { method, value: params },
agentCard: this.agentCard,
options,
};
const beforeResult = await this.interceptBefore(beforeArgs);
Expand All @@ -124,6 +125,7 @@ export class Client {
const earlyReturn = beforeResult.earlyReturn.value;
const afterArgs: AfterArgs<'sendMessageStream'> = {
result: { method, value: earlyReturn },
agentCard: this.agentCard,
options: beforeArgs.options,
};
await this.interceptAfter(afterArgs, beforeResult.executed);
Expand All @@ -135,6 +137,7 @@ export class Client {
const result = await this.transport.sendMessage(beforeArgs.input.value, beforeArgs.options);
const afterArgs: AfterArgs<'sendMessageStream'> = {
result: { method, value: result },
agentCard: this.agentCard,
options: beforeArgs.options,
};
await this.interceptAfter(afterArgs);
Expand All @@ -147,6 +150,7 @@ export class Client {
)) {
const afterArgs: AfterArgs<'sendMessageStream'> = {
result: { method, value: event },
agentCard: this.agentCard,
options: beforeArgs.options,
};
await this.interceptAfter(afterArgs);
Expand Down Expand Up @@ -260,13 +264,18 @@ export class Client {
): AsyncGenerator<A2AStreamEventData, void, undefined> {
const method = 'resubscribeTask';

const beforeArgs: BeforeArgs<'resubscribeTask'> = { input: { method, value: params }, options };
const beforeArgs: BeforeArgs<'resubscribeTask'> = {
input: { method, value: params },
agentCard: this.agentCard,
options,
};
const beforeResult = await this.interceptBefore(beforeArgs);

if (beforeResult) {
const earlyReturn = beforeResult.earlyReturn.value;
const afterArgs: AfterArgs<'resubscribeTask'> = {
result: { method, value: earlyReturn },
agentCard: this.agentCard,
options: beforeArgs.options,
};
await this.interceptAfter(afterArgs, beforeResult.executed);
Expand All @@ -280,6 +289,7 @@ export class Client {
)) {
const afterArgs: AfterArgs<'resubscribeTask'> = {
result: { method, value: event },
agentCard: this.agentCard,
options: beforeArgs.options,
};
await this.interceptAfter(afterArgs);
Expand Down Expand Up @@ -319,6 +329,7 @@ export class Client {
): Promise<ClientCallResult<K>['value']> {
const beforeArgs: BeforeArgs<K> = {
input: input,
agentCard: this.agentCard,
options,
};
const beforeResult = await this.interceptBefore(beforeArgs);
Expand All @@ -329,6 +340,7 @@ export class Client {
method: input.method,
value: beforeResult.earlyReturn.value,
} as ClientCallResult<K>,
agentCard: this.agentCard,
options: beforeArgs.options,
};
await this.interceptAfter(afterArgs, beforeResult.executed);
Expand All @@ -339,6 +351,7 @@ export class Client {

const afterArgs: AfterArgs<K> = {
result: { method: input.method, value: result } as ClientCallResult<K>,
agentCard: this.agentCard,
options: beforeArgs.options,
};
await this.interceptAfter(afterArgs);
Expand Down
26 changes: 26 additions & 0 deletions test/client/multitransport-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,32 @@ describe('Client', () => {
expect(result).to.equal(task);
});

it('should contain agent card', async () => {
let caughtAgentCard;
const config: ClientConfig = {
interceptors: [
{
before: async (args) => {
caughtAgentCard = args.agentCard;
},
after: async () => {},
},
],
};
client = new Client(transport, agentCard, config);
const params: TaskQueryParams = { id: '123' };
const task: Task = {
id: '123',
kind: 'task',
contextId: 'ctx1',
status: { state: 'working' },
};
transport.getTask.resolves(task);

await client.getTask(params);
expect(caughtAgentCard).to.equal(agentCard);
});

it('should return early from before', async () => {
const task: Task = {
id: '123',
Expand Down