Skip to content

feat: Add core types and methods for plugins support - #826

Merged
bartek-gralewicz merged 1 commit into
mainfrom
bgralewicz/plugins_core_types
Nov 17, 2026
Merged

bartek-gralewicz merged 1 commit into
mainfrom
bgralewicz/plugins_core_types

Conversation

@bartek-gralewicz

Copy link
Copy Markdown
Collaborator

Description

Core types and methods for the plugins support. Types and methods shapes are aligned with a2a-python that already introduced this support. Relevant PR: a2aproject/a2a-python#1352

@bartek-gralewicz
bartek-gralewicz requested a review from a team as a code owner November 16, 2026 14:17
@github-actions

Copy link
Copy Markdown

🧪 Code Coverage

⬇️ Download Full Report

Base PR Delta
src/plugins/index.ts (new) — 0% —
src/plugins/manager.ts (new) — 98.9% —
src/plugins/types.ts (new) — 0% —
Total 84.53% 84.78% 🟢 +0.25%

Generated by coverage-comment.yml

@bartek-gralewicz
bartek-gralewicz merged commit 121db9d into main Nov 17, 2026
20 checks passed
@bartek-gralewicz
bartek-gralewicz deleted the bgralewicz/plugins_core_types branch November 17, 2026 08:54
bartek-gralewicz added a commit that referenced this pull request Nov 17, 2026
# Description

Wires the server hooks from `@a2a-js/sdk/plugins` (#826) into
`DefaultRequestHandler`. Plugins are registered through a new `plugins`
option.

## Example

```ts
const timing: A2APlugin = {
  name: 'timing',
  beforeHandlerRun: (req) => void req.context.state.set('start', Date.now()),
  afterHandlerRun: (res) => {
    if (res.isFinal) console.log(res.method, Date.now() - (res.context.state.get('start') as number), 'ms');
  },
};

new DefaultRequestHandler(agentCard, taskStore, executor, undefined, undefined, undefined, undefined, undefined, {
  plugins: [timing],
});```
bartek-gralewicz added a commit that referenced this pull request Nov 19, 2026
# Description

Runs the client hooks from `@a2a-js/sdk/plugins` (#826) around every
call `Client` makes to the server. Plugins are set with
`ClientConfig.plugins`. `ClientFactoryOptions.createFrom` concatenates
them, the same way it does interceptors.

```ts
const tracing: A2APlugin = {
  name: 'tracing',
  beforeClientRequest: (req) => {
    req.options.serviceParameters!['tracer'] = newTracer(); // Example name, not a part of the SDK
  },
};

const factory = new ClientFactory(
  ClientFactoryOptions.createFrom(ClientFactoryOptions.default, {
    clientConfig: { plugins: [tracing] },
  })
);
```

# Notes

Since `Server` plugin side shared many similarities with the `Client`
side, the common logic was extracted to the reused `src/plugins/run.ts`.
bartek-gralewicz added a commit that referenced this pull request Nov 20, 2026
# Description

Runs the execution hooks from `@a2a-js/sdk/plugins` (#826) around every
`AgentExecutor.execute` call made by `DefaultRequestHandler`. They use
the same `plugins` option as the server hooks (#835).

## Behavior

- `beforeExecution` runs before the agent. If it throws, the agent does
not run, plugins that already ran get `afterExecution` with the error,
and the handler's usual failure path publishes a FAILED task.
- The agent runs inside `aroundExecution`. The agent runs detached from
the request, so this is where a plugin can scope it, for example to make
the agent's outbound calls children of an OTel span.
- `afterExecution` runs when the agent returns or throws, with the error
if there is one. The error is rethrown, so the handler's existing
failure handling is unchanged.
- `onExecutionEvent` runs for each event published on the task's bus
while the agent runs. The agent gets the original bus. Events the
handler publishes after the agent throws (the synthetic FAILED task and
status) are not reported.
- `cancelTask` runs without hooks.
- With no execution hooks registered, the handler uses the executor as
given.
bartek-gralewicz added a commit that referenced this pull request Nov 20, 2026
# Description

Calls `onTaskEvent` from `@a2a-js/sdk/plugins` (#826) after every
successful task write made by `DefaultRequestHandler`. The hook uses the
same `plugins` option as the server hooks (#835).

```ts
const transitions: A2APlugin = {
  name: 'transitions',
  onTaskEvent: ({ taskId, fromState, toState }) => {
    if (fromState !== toState) console.log(taskId, fromState, '->', toState);
  },
};
```

## Behavior

- `ResultManager` saves the task, then calls `onTaskEvent` with the
task, the event that caused the write, and the state stored before it:
- a `task` event reports the stored state, or `undefined` when it
creates the task;
  - a `statusUpdate` reports the stored state;
- an `artifactUpdate` reports the same state as `fromState` and
`toState`.
- Nothing is reported when no write happens (message events, updates to
terminal tasks, events for unknown tasks) or when the store write
throws.
- The handler passes its plugins to every `ResultManager` it creates:
`sendMessage`, `sendMessageStream`, and both cancel paths. That includes
the direct CANCELED write when no agent is running.
- A follow-up message to an existing task appends it to the history and
saves it directly. That write is reported with no `event` and an
unchanged state.
- `onTaskEvent` runs after the per-task write lock is released, so a
hook may write to the same task, for example to cancel it, without
deadlocking. The states in each report are captured inside the lock, so
they match that write. Reports from one request arrive in write order.
Reports from concurrent requests on the same task may interleave.
- Errors thrown by `onTaskEvent` are logged and ignored.

`ResultManager` is public, so `plugins` is an optional third constructor
argument and existing callers are unaffected. The fields match
a2a-python's `TaskEventInfo`.
bartek-gralewicz added a commit that referenced this pull request Nov 20, 2026
…nder (#841)

# Description

Runs the push hooks from `@a2a-js/sdk/plugins` (#826) around each
webhook delivery in `DefaultPushNotificationSender`. Plugins are set
with the new `DefaultPushNotificationSenderOptions.plugins`.
`DefaultRequestHandler` passes its own `plugins` option to the default
sender it creates.

```ts
const pushTracing: A2APlugin = {
  name: 'push-tracing',
  beforePushSend: (req) => {
    req.headers['traceparent'] = currentTraceparent(); // your own helper
  },
  onPushSent: ({ request, statusCode, durationMs, error }) =>
    console.log(request.url, statusCode, `${durationMs.toFixed(1)}ms`, error ?? 'ok'),
};

const sender = new DefaultPushNotificationSender(store, { plugins: [pushTracing] });
```

## Behavior

- `beforePushSend` gets a `PushRequest` with the task id, URL, headers,
event, a `skip` flag and a `state` map. It can add or change headers,
which the POST then sends, and set `skip`.
- The URL can't be changed by a plugin, so a delivery can't be
redirected. This matches a2a-python.
- If `beforePushSend` throws, nothing is sent and the delivery fails.
`send` logs it the same way as a failed POST.
- If `skip` is set, nothing is sent.
- `onPushSent` runs exactly once per delivery:
  - on success, with the status code;
  - on a non-2xx response, with the status code and the error;
  - on a network error, with the error and no status code;
  - on skip, with `skipped: true`;
  - when `beforePushSend` throws, with that error.
- `durationMs` covers `beforePushSend` and the POST. The webhook timeout
covers only the POST.
- `createLegacyAwarePushNotificationSender` passes its options through,
so it accepts `plugins` too. A sender you pass to
`DefaultRequestHandler` yourself needs its own `plugins`, as in
a2a-python.
- With no push hooks registered, delivery is unchanged, except that the
body is now serialized before the timeout starts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants