Skip to content

Commit d7135d8

Browse files
committed
docs: trim boilerplate comments across src
1 parent 297939a commit d7135d8

120 files changed

Lines changed: 1530 additions & 5364 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.betterer.results

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
//
66
exports[`TypeScript Strict Mode`] = {
77
value: `{
8-
"src/server/events/execution_event_bus.ts:150995267": [
9-
[327, 15, 4, "tsc: Expected 2 arguments, but got 1.", "2087764327"],
10-
[350, 15, 4, "tsc: Expected 2 arguments, but got 1.", "2087764327"]
8+
"src/server/events/execution_event_bus.ts:1102247656": [
9+
[233, 15, 4, "tsc: Expected 2 arguments, but got 1.", "2087764327"],
10+
[254, 15, 4, "tsc: Expected 2 arguments, but got 1.", "2087764327"]
1111
]
1212
}`
1313
};

src/client/auth-handler.ts

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,45 @@ export interface HttpHeaders {
33
}
44

55
/**
6-
* Generic interface for handling authentication for HTTP requests.
7-
*
8-
* - For each HTTP request, this handler is called to provide additional headers to the request through
9-
* the headers() function.
10-
* - After the server returns a response, the shouldRetryWithHeaders() function is called. Usually this
11-
* function responds to a 401 or 403 response or JSON-RPC codes, but can respond to any other signal -
12-
* that is an implementation detail of the AuthenticationHandler.
13-
* - If the shouldRetryWithHeaders() function returns new headers, then the request should retried with the provided
14-
* revised headers. These provisional headers may, or may not, be optimistically stored for subsequent requests -
15-
* that is an implementation detail of the AuthenticationHandler.
16-
* - If the request is successful and the onSuccessfulRetry() is defined, then the onSuccessfulRetry() function is
17-
* called with the headers that were used to successfully complete the request. This callback provides an
18-
* opportunity to save the headers for subsequent requests if they were not already saved.
6+
* Pluggable authentication handler for HTTP requests.
197
*
8+
* - {@link headers} is called before each request to supply additional
9+
* request headers (typically `Authorization`).
10+
* - {@link shouldRetryWithHeaders} is called after every response and
11+
* decides whether the request should be retried with new headers,
12+
* typically in response to a 401 / 403 or a WWW-Authenticate.
13+
* - {@link onSuccessfulRetry}, if defined, is called when a retry
14+
* succeeds, giving the handler a chance to persist the new headers.
2015
*/
2116
export interface AuthenticationHandler {
22-
/**
23-
* Provides additional HTTP request headers.
24-
* @returns HTTP headers which may include Authorization if available.
25-
*/
17+
/** Returns request headers (may include `Authorization`). */
2618
headers: () => Promise<HttpHeaders>;
2719

2820
/**
29-
* For every HTTP response (even 200s) the shouldRetryWithHeaders() method is called.
30-
* This method is supposed to check if the request needs to be retried and if, yes,
31-
* return a set of headers. An A2A server might indicate auth failures in its response
32-
* by JSON-rpc codes, HTTP codes like 401, 403 or headers like WWW-Authenticate.
33-
*
34-
* @param req The RequestInit object used to invoke fetch()
35-
* @param res The fetch Response object
36-
* @returns If the HTTP request should be retried then returns the HTTP headers to use,
37-
* or returns undefined if no retry should be made.
21+
* Called for every response. Returns new headers if the request
22+
* should be retried, or `undefined` to skip the retry.
3823
*/
3924
shouldRetryWithHeaders: (req: RequestInit, res: Response) => Promise<HttpHeaders | undefined>;
4025

4126
/**
42-
* If the last HTTP request using the headers from shouldRetryWithHeaders() was successful, and
43-
* this function is implemented, then it will be called with the headers provided from
44-
* shouldRetryWithHeaders().
45-
*
46-
* This callback allows transient headers to be saved for subsequent requests only when they
47-
* are validated by the server.
27+
* Called when a retry using the headers from
28+
* {@link shouldRetryWithHeaders} succeeded. Lets the handler persist
29+
* those headers for subsequent requests.
4830
*/
4931
onSuccessfulRetry?: (headers: HttpHeaders) => Promise<void>;
5032
}
5133

5234
/**
53-
* Higher-order function that wraps fetch with authentication handling logic.
54-
* Returns a new fetch function that automatically handles authentication retries for 401/403 responses.
55-
*
56-
* @param fetchImpl The underlying fetch implementation to wrap
57-
* @param authHandler Authentication handler for managing auth headers and retries
58-
* @returns A new fetch function with authentication handling capabilities
59-
*
60-
* Usage examples:
61-
* - const authFetch = createAuthHandlingFetch(fetch, authHandler);
62-
* - const response = await authFetch(url, options);
63-
* - const response = await authFetch(url); // Direct function call
35+
* Wraps `fetch` with authentication handling. The returned function
36+
* injects headers from `authHandler.headers()`, retries when
37+
* `authHandler.shouldRetryWithHeaders` returns new headers, and notifies
38+
* via `onSuccessfulRetry` when the retry succeeds.
6439
*/
6540
export function createAuthenticatingFetchWithRetry(
6641
fetchImpl: typeof fetch,
6742
authHandler: AuthenticationHandler
6843
): typeof fetch {
69-
/**
70-
* Executes a fetch request with authentication handling.
71-
* If the auth handler provides new headers for the shouldRetryWithHeaders() function,
72-
* then the request is retried.
73-
* @param url The URL to fetch
74-
* @param init The fetch request options
75-
* @returns A Promise that resolves to the Response
76-
*/
7744
async function authFetch(url: RequestInfo | URL, init?: RequestInit): Promise<Response> {
78-
// Merge auth headers with provided headers
7945
const authHeaders = (await authHandler.headers()) || {};
8046
const mergedInit: RequestInit = {
8147
...(init || {}),
@@ -87,10 +53,8 @@ export function createAuthenticatingFetchWithRetry(
8753

8854
let response = await fetchImpl(url, mergedInit);
8955

90-
// Check if the auth handler wants to retry the request with new headers
9156
const updatedHeaders = await authHandler.shouldRetryWithHeaders(mergedInit, response);
9257
if (updatedHeaders) {
93-
// Retry request with revised headers
9458
const retryInit: RequestInit = {
9559
...(init || {}),
9660
headers: {
@@ -101,14 +65,14 @@ export function createAuthenticatingFetchWithRetry(
10165
response = await fetchImpl(url, retryInit);
10266

10367
if (response.ok && authHandler.onSuccessfulRetry) {
104-
await authHandler.onSuccessfulRetry(updatedHeaders); // Remember headers that worked
68+
await authHandler.onSuccessfulRetry(updatedHeaders);
10569
}
10670
}
10771

10872
return response;
10973
}
11074

111-
// Copy fetch properties to maintain compatibility
75+
// Preserve fetch's own properties so the wrapped function is a drop-in.
11276
Object.setPrototypeOf(authFetch, Object.getPrototypeOf(fetchImpl));
11377
Object.defineProperties(authFetch, Object.getOwnPropertyDescriptors(fetchImpl));
11478

src/client/card-resolver.ts

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,32 @@ export interface AgentCardResolverOptions {
66
path?: string;
77
fetchImpl?: typeof fetch;
88
/**
9-
* Enables the v0.3 protocol compatibility layer.
10-
*
11-
* When enabled, the resolver inspects each fetched agent-card
12-
* payload; if its shape matches v0.3 (top-level `url` without
13-
* `supportedInterfaces`, `preferredTransport`,
14-
* `additionalInterfaces`, `supportsAuthenticatedExtendedCard`, or a
15-
* `protocolVersion` in `[0.3, 1.0)`), it is translated to the v1.0
16-
* proto shape via `toCoreAgentCard`. Each synthesized
9+
* Enables the v0.3 protocol compatibility layer. When enabled, the
10+
* resolver detects v0.3-shaped card payloads and translates them to
11+
* the v1.0 proto shape via `toCoreAgentCard`. Each synthesized
1712
* `AgentInterface` is stamped with `protocolVersion: '0.3'` so that
18-
* a `JsonRpcTransportFactory` configured with
19-
* `legacyCompat: { enabled: true }` selects the compat transport
20-
* automatically.
13+
* a transport factory configured with `legacyCompat: { enabled: true }`
14+
* selects the compat transport automatically.
2115
*
22-
* The discovery request itself always announces the SDK's native
23-
* v1.0 in the `A2A-Version` header — detection of v0.3 servers is
24-
* based on the response shape (see {@link resolve}), not on the
25-
* request value. This avoids a downgrade dance when both client
26-
* and server speak v1.0 natively but both have legacyCompat
27-
* enabled.
16+
* Detection is based on the response shape, not the request, so the
17+
* discovery request always announces the SDK's native v1.0 in the
18+
* `A2A-Version` header.
2819
*
29-
* Default: omitted (treated as disabled). When disabled, the v0.3
30-
* compat module is never loaded.
20+
* Default: omitted (disabled).
3121
*/
3222
legacyCompat?: { enabled: boolean };
3323
}
3424

3525
export interface AgentCardResolver {
36-
/**
37-
* Fetches the agent card based on provided base URL and path,
38-
*/
3926
resolve(baseUrl: string, path?: string): Promise<AgentCard>;
4027
}
4128

4229
export class DefaultAgentCardResolver implements AgentCardResolver {
4330
constructor(public readonly options?: AgentCardResolverOptions) {}
4431

4532
/**
46-
* Fetches the agent card based on provided base URL and path.
47-
* Path is selected in the following order:
48-
* 1) path parameter
49-
* 2) path from options
50-
* 3) .well-known/agent-card.json
33+
* Fetches the agent card. Path is selected in this order:
34+
* `path` parameter → `options.path` → `/.well-known/agent-card.json`.
5135
*/
5236
async resolve(baseUrl: string, path?: string): Promise<AgentCard> {
5337
const agentCardUrl = new URL(path ?? this.options?.path ?? AGENT_CARD_PATH, baseUrl);
@@ -69,22 +53,14 @@ export class DefaultAgentCardResolver implements AgentCardResolver {
6953
}
7054

7155
/*
72-
* In the v0.3.0 specification, there was a structural drift between the JSON Schema data model
73-
* and the Protobuf-based data model for AgentCards.
74-
* The JSON Schema format uses a `"type"` discriminator (e.g., `{"type": "openIdConnect"}`),
75-
* while the Protobuf JSON representation uses the `oneof` field name as the discriminator
76-
* (e.g., `{"openIdConnectSecurityScheme": {...}}`).
77-
*
78-
* The A2A SDK internal logic expects the JSON Schema-based format. This fallback detection
79-
* allows us to parse cards served by endpoints returning the Protobuf JSON structure by
80-
* identifying the lack of the "type" field in security schemes or the presence of the
81-
* "schemes" wrapper in security entries, and normalizing it before use.
56+
* In v0.3 there was structural drift between the JSON Schema data
57+
* model and the Protobuf-based data model for AgentCards: JSON Schema
58+
* uses a `"type"` discriminator, while Protobuf JSON uses the `oneof`
59+
* field name. The SDK expects the JSON Schema format; this fallback
60+
* detects the Protobuf JSON shape and normalizes it before use.
8261
*
8362
* When `legacyCompat: { enabled: true }`, this method also detects
84-
* v0.3-shaped cards and translates them via the compat
85-
* module so the rest of the client stack sees a uniform v1.0
86-
* representation with `protocolVersion: '0.3'` stamped on every
87-
* synthesized interface.
63+
* v0.3-shaped cards and translates them via the compat module.
8864
*/
8965
private normalizeAgentCard(card: unknown): AgentCard {
9066
if (this.options?.legacyCompat?.enabled) {
@@ -132,7 +108,7 @@ export class DefaultAgentCardResolver implements AgentCardResolver {
132108
const schemes = Object.values(securitySchemes);
133109
if (schemes.length > 0) {
134110
const first = schemes[0];
135-
// Proto JSON maps use the oneof field name directly rather than a "type" property
111+
// Proto JSON uses the oneof field name directly rather than a "type" property.
136112
return first && typeof first === 'object' && !('type' in first);
137113
}
138114
}

src/client/factory.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,27 @@ import { TransportFactory } from './transports/transport.js';
99

1010
export interface ClientFactoryOptions {
1111
/**
12-
* Transport factories to use.
13-
* Effectively defines transports supported by this client factory.
12+
* Transport factories to use. Effectively defines transports supported
13+
* by this client factory.
1414
*/
1515
transports: TransportFactory[];
1616

17-
/**
18-
* Client config to be used for clients created by this factory.
19-
*/
17+
/** Client config used for clients created by this factory. */
2018
clientConfig?: ClientConfig;
2119

2220
/**
23-
* Transport preferences to override ones defined by the agent card.
24-
* If no matches are found among preferred transports, agent card values are used next.
21+
* Transport preferences overriding those defined by the agent card.
22+
* If no matches are found among preferred transports, agent card
23+
* values are used next.
2524
*/
2625
preferredTransports?: TransportProtocolName[];
2726

28-
/**
29-
* Used for createFromAgentCardUrl to download agent card.
30-
*/
27+
/** Used by `createFromUrl` to download the agent card. */
3128
cardResolver?: AgentCardResolver;
3229
}
3330

3431
export const ClientFactoryOptions = {
35-
/**
36-
* SDK default options for {@link ClientFactory}.
37-
*/
32+
/** SDK default options for {@link ClientFactory}. */
3833
default: {
3934
transports: [new JsonRpcTransportFactory(), new RestTransportFactory()],
4035
} as Readonly<ClientFactoryOptions>,
@@ -95,12 +90,10 @@ export class ClientFactory {
9590
}
9691

9792
/**
98-
* Creates a new client from the provided agent card.
99-
*
100-
* When the selected `AgentInterface` declares a non-empty `tenant` value
101-
* (per spec Section 4.4.6), the transport is automatically wrapped with a
102-
* {@link TenantTransportDecorator} so the default tenant is applied to every
103-
* request without requiring callers to set it manually.
93+
* Creates a new client from the provided agent card. When the selected
94+
* `AgentInterface` declares a non-empty `tenant`, the transport is
95+
* wrapped with a {@link TenantTransportDecorator} so the default tenant
96+
* is applied to every request.
10497
*/
10598
async createFromAgentCard(agentCard: AgentCard): Promise<Client> {
10699
const interfaces = agentCard.supportedInterfaces ?? [];
@@ -123,8 +116,6 @@ export class ClientFactory {
123116
if (factory && selectedInterface) {
124117
let transport = await factory.create(selectedInterface.url, agentCard);
125118

126-
// If the agent interface declares a default tenant, wrap the transport
127-
// so the tenant is automatically applied to all requests.
128119
if (selectedInterface.tenant) {
129120
transport = new TenantTransportDecorator(transport, selectedInterface.tenant);
130121
}
@@ -139,15 +130,18 @@ export class ClientFactory {
139130
}
140131

141132
/**
142-
* Downloads agent card using AgentCardResolver from options
143-
* and creates a new client from the downloaded card.
133+
* Downloads the agent card using the configured {@link AgentCardResolver}
134+
* and creates a new client from it.
144135
*
145136
* @example
146137
* ```ts
147-
* const factory = new ClientFactory(); // use default options and default {@link AgentCardResolver}.
148-
* const client1 = await factory.createFromUrl('https://example.com'); // /.well-known/agent-card.json is used by default
149-
* const client2 = await factory.createFromUrl('https://example.com', '/my-agent-card.json'); // specify custom path
150-
* const client3 = await factory.createFromUrl('https://example.com/my-agent-card.json', ''); // specify full URL and set path to empty
138+
* const factory = new ClientFactory();
139+
* // /.well-known/agent-card.json is used by default.
140+
* const client = await factory.createFromUrl('https://example.com');
141+
* // Custom path.
142+
* const client2 = await factory.createFromUrl('https://example.com', '/my-card.json');
143+
* // Full URL with empty path.
144+
* const client3 = await factory.createFromUrl('https://example.com/my-card.json', '');
151145
* ```
152146
*/
153147
async createFromUrl(baseUrl: string, path?: string): Promise<Client> {
@@ -200,8 +194,7 @@ function mergeArrays<T>(
200194
}
201195

202196
/**
203-
* A Map that normalizes string keys to uppercase for case-insensitive lookups.
204-
* This prevents errors from inconsistent casing in protocol names.
197+
* Map that uppercases string keys so protocol-name lookups are case-insensitive.
205198
*/
206199
class CaseInsensitiveMap<T> extends Map<string, T> {
207200
private normalizeKey(key: string): string {

src/client/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* Client entry point for the A2A Server V2 library.
3-
*/
1+
/** Client entry point for the A2A SDK. */
42

53
export * from './auth-handler.js';
64
export {

0 commit comments

Comments
 (0)