@a2anet/a2a-utils 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +1412 -2
  2. package/dist/artifacts/data.d.ts +118 -0
  3. package/dist/artifacts/data.d.ts.map +1 -0
  4. package/dist/artifacts/data.js +583 -0
  5. package/dist/artifacts/data.js.map +1 -0
  6. package/dist/artifacts/index.d.ts +33 -0
  7. package/dist/artifacts/index.d.ts.map +1 -0
  8. package/dist/artifacts/index.js +131 -0
  9. package/dist/artifacts/index.js.map +1 -0
  10. package/dist/artifacts/text.d.ts +54 -0
  11. package/dist/artifacts/text.d.ts.map +1 -0
  12. package/dist/artifacts/text.js +151 -0
  13. package/dist/artifacts/text.js.map +1 -0
  14. package/dist/client/a2a-session.d.ts +94 -0
  15. package/dist/client/a2a-session.d.ts.map +1 -0
  16. package/dist/client/a2a-session.js +264 -0
  17. package/dist/client/a2a-session.js.map +1 -0
  18. package/dist/client/a2a-tools.d.ts +152 -0
  19. package/dist/client/a2a-tools.d.ts.map +1 -0
  20. package/dist/client/a2a-tools.js +470 -0
  21. package/dist/client/a2a-tools.js.map +1 -0
  22. package/dist/client/agent-manager.d.ts +94 -0
  23. package/dist/client/agent-manager.d.ts.map +1 -0
  24. package/dist/client/agent-manager.js +243 -0
  25. package/dist/client/agent-manager.js.map +1 -0
  26. package/dist/client/index.d.ts +4 -0
  27. package/dist/client/index.d.ts.map +1 -0
  28. package/dist/client/index.js +7 -0
  29. package/dist/client/index.js.map +1 -0
  30. package/dist/files/file-store.d.ts +24 -0
  31. package/dist/files/file-store.d.ts.map +1 -0
  32. package/dist/files/file-store.js +5 -0
  33. package/dist/files/file-store.js.map +1 -0
  34. package/dist/files/index.d.ts +3 -0
  35. package/dist/files/index.d.ts.map +1 -0
  36. package/dist/files/index.js +5 -0
  37. package/dist/files/index.js.map +1 -0
  38. package/dist/files/local-file-store.d.ts +26 -0
  39. package/dist/files/local-file-store.d.ts.map +1 -0
  40. package/dist/files/local-file-store.js +99 -0
  41. package/dist/files/local-file-store.js.map +1 -0
  42. package/dist/index.d.ts +11 -0
  43. package/dist/index.d.ts.map +1 -0
  44. package/dist/index.js +14 -0
  45. package/dist/index.js.map +1 -0
  46. package/dist/tasks/index.d.ts +2 -0
  47. package/dist/tasks/index.d.ts.map +1 -0
  48. package/dist/tasks/index.js +5 -0
  49. package/dist/tasks/index.js.map +1 -0
  50. package/dist/tasks/json-task-store.d.ts +32 -0
  51. package/dist/tasks/json-task-store.d.ts.map +1 -0
  52. package/dist/tasks/json-task-store.js +66 -0
  53. package/dist/tasks/json-task-store.js.map +1 -0
  54. package/dist/types.d.ts +65 -0
  55. package/dist/types.d.ts.map +1 -0
  56. package/dist/types.js +23 -0
  57. package/dist/types.js.map +1 -0
  58. package/package.json +17 -4
@@ -0,0 +1,264 @@
1
+ // SPDX-FileCopyrightText: 2025-present A2A Net <hello@a2anet.com>
2
+ //
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ import { A2AClient } from "@a2a-js/sdk/client";
5
+ import { InMemoryTaskStore } from "@a2a-js/sdk/server";
6
+ import { v4 as uuidv4 } from "uuid";
7
+ import { TERMINAL_OR_ACTIONABLE_STATES } from "../types.js";
8
+ function sleep(seconds) {
9
+ return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
10
+ }
11
+ /** Main interface for sending messages to A2A agents. */
12
+ export class A2ASession {
13
+ agentManager;
14
+ taskStore;
15
+ fileStore;
16
+ sendMessageTimeout;
17
+ getTaskTimeout;
18
+ getTaskPollInterval;
19
+ constructor(agentManager, opts) {
20
+ this.agentManager = agentManager;
21
+ this.taskStore = opts?.taskStore ?? new InMemoryTaskStore();
22
+ this.fileStore = opts?.fileStore ?? null;
23
+ this.sendMessageTimeout = opts?.sendMessageTimeout ?? 60.0;
24
+ this.getTaskTimeout = opts?.getTaskTimeout ?? 60.0;
25
+ this.getTaskPollInterval = opts?.getTaskPollInterval ?? 5.0;
26
+ }
27
+ /**
28
+ * Send a message to an A2A agent.
29
+ *
30
+ * @param agentId - Registered agent identifier.
31
+ * @param message - The message content to send.
32
+ * @param opts.contextId - Optional context ID to continue a conversation.
33
+ * Auto-generated when null.
34
+ * @param opts.taskId - Optional task ID to attach to the message.
35
+ * @param opts.timeout - HTTP timeout in seconds. Defaults to sendMessageTimeout
36
+ * from constructor.
37
+ *
38
+ * @returns Task for task responses, Message for message-only responses.
39
+ *
40
+ * @throws Error if agent is not found.
41
+ */
42
+ async sendMessage(agentId, message, opts) {
43
+ const [agentCard, headers] = await this.resolveAgent(agentId);
44
+ const contextId = opts?.contextId ?? uuidv4();
45
+ // Build A2A message
46
+ const a2aMessage = {
47
+ kind: "message",
48
+ messageId: uuidv4(),
49
+ parts: [{ kind: "text", text: message }],
50
+ role: "user",
51
+ contextId,
52
+ };
53
+ if (opts?.taskId != null) {
54
+ a2aMessage.taskId = opts.taskId;
55
+ }
56
+ const effectiveTimeout = opts?.timeout !== undefined && opts?.timeout !== null
57
+ ? opts.timeout
58
+ : this.sendMessageTimeout;
59
+ const start = performance.now();
60
+ const client = this.createClient(agentCard, headers);
61
+ const response = await this.getSessionClient(client).sendMessage({
62
+ message: a2aMessage,
63
+ configuration: { blocking: false },
64
+ }, { signal: AbortSignal.timeout(effectiveTimeout * 1000) });
65
+ if ("error" in response) {
66
+ throw new Error(`JSON-RPC error: ${response.error.message} (code: ${response.error.code})`);
67
+ }
68
+ const result = response.result;
69
+ // Handle Message result
70
+ if (result.kind === "message") {
71
+ return result;
72
+ }
73
+ // Handle Task result
74
+ if (result.kind !== "task") {
75
+ throw new Error("Expected Task or Message response, got unexpected kind");
76
+ }
77
+ let task = result;
78
+ await this.taskStore.save(task);
79
+ // If task is already in a terminal/actionable state, save files and return
80
+ if (TERMINAL_OR_ACTIONABLE_STATES.has(task.status.state)) {
81
+ await this.saveFiles(task);
82
+ return task;
83
+ }
84
+ // Task is in a non-terminal state (e.g. working) — monitor with remaining time
85
+ const elapsed = (performance.now() - start) / 1000;
86
+ const remaining = Math.max(0, effectiveTimeout - elapsed);
87
+ if (remaining > 0) {
88
+ const supportsStreaming = agentCard.capabilities !== undefined &&
89
+ agentCard.capabilities !== null &&
90
+ agentCard.capabilities.streaming === true;
91
+ if (supportsStreaming) {
92
+ task = await this.getTaskStreaming(agentCard, headers, task.id, remaining);
93
+ }
94
+ else {
95
+ task = await this.getTaskPolling(agentCard, headers, task.id, remaining, this.getTaskPollInterval);
96
+ }
97
+ await this.taskStore.save(task);
98
+ }
99
+ await this.saveFiles(task);
100
+ return task;
101
+ }
102
+ /**
103
+ * Get the current state of a task, monitoring until terminal/actionable state.
104
+ *
105
+ * If the remote agent supports streaming, uses SSE resubscription for real-time
106
+ * updates. Otherwise, polls at regular intervals.
107
+ *
108
+ * On monitoring timeout, returns the current task state (which may still be
109
+ * non-terminal, e.g. "working"). The only errors from getTask are failed
110
+ * HTTP requests (agent down, network error).
111
+ *
112
+ * @param agentId - Registered agent identifier.
113
+ * @param taskId - Task ID from a previous sendMessage call.
114
+ * @param opts.timeout - Total monitoring timeout in seconds. Defaults to
115
+ * getTaskTimeout from constructor.
116
+ * @param opts.pollInterval - Interval between polls in seconds (used when streaming
117
+ * is not supported). Defaults to getTaskPollInterval from constructor.
118
+ *
119
+ * @returns Task with the current task state. If monitoring times out, the
120
+ * returned task may still be in a non-terminal state.
121
+ *
122
+ * @throws Error if agent is not found.
123
+ */
124
+ async getTask(agentId, taskId, opts) {
125
+ const [agentCard, headers] = await this.resolveAgent(agentId);
126
+ const effectiveTimeout = opts?.timeout !== undefined && opts?.timeout !== null
127
+ ? opts.timeout
128
+ : this.getTaskTimeout;
129
+ const effectivePollInterval = opts?.pollInterval !== undefined && opts?.pollInterval !== null
130
+ ? opts.pollInterval
131
+ : this.getTaskPollInterval;
132
+ const supportsStreaming = agentCard.capabilities !== undefined &&
133
+ agentCard.capabilities !== null &&
134
+ agentCard.capabilities.streaming === true;
135
+ let task;
136
+ if (supportsStreaming) {
137
+ task = await this.getTaskStreaming(agentCard, headers, taskId, effectiveTimeout);
138
+ }
139
+ else {
140
+ task = await this.getTaskPolling(agentCard, headers, taskId, effectiveTimeout, effectivePollInterval);
141
+ }
142
+ await this.taskStore.save(task);
143
+ await this.saveFiles(task);
144
+ return task;
145
+ }
146
+ /**
147
+ * Save file artifacts to the file store if configured.
148
+ *
149
+ * Idempotent: skips artifacts whose files have already been saved.
150
+ */
151
+ async saveFiles(task) {
152
+ if (this.fileStore === null || !task.artifacts) {
153
+ return;
154
+ }
155
+ for (const artifact of task.artifacts) {
156
+ const hasFiles = artifact.parts.some((p) => p.kind === "file");
157
+ if (hasFiles) {
158
+ const existing = await this.fileStore.get(task.id, artifact.artifactId);
159
+ if (existing.length === 0) {
160
+ await this.fileStore.save(task.id, artifact);
161
+ }
162
+ }
163
+ }
164
+ }
165
+ /** Monitor a task via SSE resubscription, falling back to a final fetch. */
166
+ async getTaskStreaming(agentCard, headers, taskId, timeout) {
167
+ const client = this.createClient(agentCard, headers);
168
+ const params = { id: taskId };
169
+ try {
170
+ const stream = this.getSessionClient(client).resubscribeTask(params, {
171
+ signal: AbortSignal.timeout(timeout * 1000),
172
+ });
173
+ for await (const event of stream) {
174
+ if (event.kind === "status-update") {
175
+ const statusEvent = event;
176
+ if (TERMINAL_OR_ACTIONABLE_STATES.has(statusEvent.status.state)) {
177
+ break;
178
+ }
179
+ }
180
+ else if (event.kind === "task") {
181
+ const taskEvent = event;
182
+ if (TERMINAL_OR_ACTIONABLE_STATES.has(taskEvent.status.state)) {
183
+ break;
184
+ }
185
+ }
186
+ }
187
+ }
188
+ catch (e) {
189
+ if (e instanceof DOMException && e.name === "AbortError") {
190
+ console.info(`Task ${taskId} still in working state after ${timeout}s`);
191
+ }
192
+ else {
193
+ throw e;
194
+ }
195
+ }
196
+ // Final fetch to get the complete Task with all artifacts
197
+ return this.fetchTask(client, taskId, timeout);
198
+ }
199
+ /** Monitor a task by polling at regular intervals. */
200
+ async getTaskPolling(agentCard, headers, taskId, timeout, pollInterval) {
201
+ const client = this.createClient(agentCard, headers);
202
+ const start = performance.now();
203
+ while (true) {
204
+ const task = await this.fetchTask(client, taskId, timeout);
205
+ if (TERMINAL_OR_ACTIONABLE_STATES.has(task.status.state)) {
206
+ return task;
207
+ }
208
+ const elapsed = (performance.now() - start) / 1000;
209
+ if (elapsed >= timeout) {
210
+ console.info(`Task ${taskId} still in working state after ${timeout}s`);
211
+ return task;
212
+ }
213
+ await sleep(pollInterval);
214
+ }
215
+ }
216
+ /** Fetch a task via A2AClient.getTask(). */
217
+ async fetchTask(client, taskId, timeout) {
218
+ const response = await this.getSessionClient(client).getTask({ id: taskId }, timeout !== undefined ? { signal: AbortSignal.timeout(timeout * 1000) } : undefined);
219
+ if ("error" in response) {
220
+ throw new Error(`JSON-RPC error: ${response.error.message} (code: ${response.error.code})`);
221
+ }
222
+ return response.result;
223
+ }
224
+ /** Narrow the SDK client to the methods this package relies on. */
225
+ getSessionClient(client) {
226
+ return client;
227
+ }
228
+ /** Create an A2AClient with optional custom headers. */
229
+ createClient(agentCard, headers) {
230
+ if (Object.keys(headers).length > 0) {
231
+ const customHeaders = headers;
232
+ const wrappedFetch = (input, init) => {
233
+ const mergedInit = {
234
+ ...init,
235
+ headers: {
236
+ ...init?.headers,
237
+ ...customHeaders,
238
+ },
239
+ };
240
+ return fetch(input, mergedInit);
241
+ };
242
+ wrappedFetch.preconnect = fetch.preconnect;
243
+ return new A2AClient(agentCard, { fetchImpl: wrappedFetch });
244
+ }
245
+ return new A2AClient(agentCard);
246
+ }
247
+ /**
248
+ * Resolve agent card and headers.
249
+ *
250
+ * @returns Tuple of [AgentCard, headers_dict].
251
+ *
252
+ * @throws Error if agent cannot be resolved.
253
+ */
254
+ async resolveAgent(agentId) {
255
+ const agent = await this.agentManager.getAgent(agentId);
256
+ if (agent === null) {
257
+ const agents = await this.agentManager.getAgents();
258
+ const available = Object.keys(agents).sort().join(", ");
259
+ throw new Error(`Agent '${agentId}' not found. Available agents: ${available}`);
260
+ }
261
+ return [agent.agentCard, agent.customHeaders];
262
+ }
263
+ }
264
+ //# sourceMappingURL=a2a-session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"a2a-session.js","sourceRoot":"","sources":["../../src/client/a2a-session.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,EAAE;AACF,sCAAsC;AAetC,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAkB,MAAM,oBAAoB,CAAC;AACvE,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAEpC,OAAO,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAC;AAG5D,SAAS,KAAK,CAAC,OAAe;IAC1B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC;AACzE,CAAC;AAmBD,yDAAyD;AACzD,MAAM,OAAO,UAAU;IACV,YAAY,CAAe;IAC3B,SAAS,CAAY;IACrB,SAAS,CAAmB;IACpB,kBAAkB,CAAS;IAC3B,cAAc,CAAS;IACvB,mBAAmB,CAAS;IAE7C,YACI,YAA0B,EAC1B,IAMC;QAED,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,SAAS,GAAG,IAAI,EAAE,SAAS,IAAI,IAAI,iBAAiB,EAAE,CAAC;QAC5D,IAAI,CAAC,SAAS,GAAG,IAAI,EAAE,SAAS,IAAI,IAAI,CAAC;QACzC,IAAI,CAAC,kBAAkB,GAAG,IAAI,EAAE,kBAAkB,IAAI,IAAI,CAAC;QAC3D,IAAI,CAAC,cAAc,GAAG,IAAI,EAAE,cAAc,IAAI,IAAI,CAAC;QACnD,IAAI,CAAC,mBAAmB,GAAG,IAAI,EAAE,mBAAmB,IAAI,GAAG,CAAC;IAChE,CAAC;IAED;;;;;;;;;;;;;;OAcG;I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ifactSettings } from "../types.js";
2
+ import type { A2ASession } from "./a2a-session.js";
3
+ export declare const TEXT_MINIMIZED_TIP = "Text was minimized. Call view_text_artifact() to view specific line ranges.";
4
+ export declare const DATA_MINIMIZED_TIP = "Data was minimized. Call view_data_artifact() to navigate to specific data.";
5
+ /**
6
+ * LLM-friendly tools that can be used out-of-the-box with agent frameworks.
7
+ *
8
+ * Each method has LLM-friendly docstrings, returns JSON-serialisable objects, and returns actionable error messages.
9
+ */
10
+ export declare class A2ATools {
11
+ private readonly session;
12
+ private readonly artifactSettings;
13
+ constructor(session: A2ASession, opts?: {
14
+ artifactSettings?: ArtifactSettings | null;
15
+ });
16
+ /**
17
+ * List all available agents with their names and descriptions.
18
+ *
19
+ * Use this first to discover what agents are available before sending messages.
20
+ * Each agent has a unique ID (the key) that you'll need for other tools like
21
+ * send_message and get_agent.
22
+ *
23
+ * Returns an object mapping agent IDs to their name and description.
24
+ * If any agents failed to load, an "errors" field is included with details.
25
+ */
26
+ getAgents(): Promise<Record<string, unknown>>;
27
+ /**
28
+ * Get detailed information about a specific agent, including its skills.
29
+ *
30
+ * Use this after get_agents to learn more about what a specific agent can do.
31
+ * The response includes the agent's name, description, and a list of skills
32
+ * with their descriptions.
33
+ *
34
+ * @param agentId - The agent's unique identifier (from get_agents).
35
+ */
36
+ getAgent(agentId: string): Promise<Record<string, unknown>>;
37
+ /**
38
+ * Send a message to an agent and receive a structured response.
39
+ *
40
+ * This is the primary way to communicate with agents. The response includes
41
+ * the agent's reply and any generated artifacts.
42
+ *
43
+ * Artifact data in responses may be minimized for display. Fields prefixed
44
+ * with "_" indicate metadata about minimized content. Use view_text_artifact
45
+ * or view_data_artifact to access full artifact data.
46
+ *
47
+ * If the task is still in progress after the timeout, the response includes
48
+ * a task_id. Use get_task with that task_id to continue monitoring.
49
+ *
50
+ * @param agentId - ID of the agent to message (from get_agents).
51
+ * @param message - The message content to send.
52
+ * @param contextId - Continue an existing conversation by providing its context ID.
53
+ * Omit to start a new conversation.
54
+ * @param taskId - Attach to an existing task (for input_required flows).
55
+ * @param timeout - Override the default timeout in seconds.
56
+ */
57
+ sendMessage(agentId: string, message: string, contextId?: string | null, taskId?: string | null, timeout?: number | null): Promise<Record<string, unknown>>;
58
+ /**
59
+ * Check the progress of a task that is still in progress.
60
+ *
61
+ * Use this after send_message returns a task in a non-terminal state
62
+ * (e.g. "working") to monitor its progress.
63
+ *
64
+ * If the task is still running after the timeout, the current state is
65
+ * returned. Call get_task again to continue monitoring.
66
+ *
67
+ * @param agentId - ID of the agent that owns the task.
68
+ * @param taskId - Task ID from a previous send_message response.
69
+ * @param timeout - Override the monitoring timeout in seconds.
70
+ * @param pollInterval - Override the interval between status checks in seconds.
71
+ */
72
+ getTask(agentId: string, taskId: string, timeout?: number | null, pollInterval?: number | null): Promise<Record<string, unknown>>;
73
+ /**
74
+ * View text content from an artifact, optionally selecting a range.
75
+ *
76
+ * Use this for artifacts containing text (documents, logs, code, etc.).
77
+ * You can select by line range OR character range, but not both.
78
+ *
79
+ * @param agentId - ID of the agent that produced the artifact.
80
+ * @param taskId - Task ID containing the artifact.
81
+ * @param artifactId - The artifact's unique identifier (from the task's artifacts list).
82
+ * @param lineStart - Starting line number (1-based, inclusive).
83
+ * @param lineEnd - Ending line number (1-based, inclusive).
84
+ * @param characterStart - Starting character index (0-based, inclusive).
85
+ * @param characterEnd - Ending character index (0-based, exclusive).
86
+ */
87
+ viewTextArtifact(agentId: string, taskId: string, artifactId: string, lineStart?: number | null, lineEnd?: number | null, characterStart?: number | null, characterEnd?: number | null): Promise<Record<string, unknown>>;
88
+ /**
89
+ * View structured data from an artifact with optional filtering.
90
+ *
91
+ * Use this for artifacts containing JSON data (objects, arrays, tables).
92
+ * You can navigate to specific data with json_path, then filter with
93
+ * rows and columns for tabular data.
94
+ *
95
+ * @param agentId - ID of the agent that produced the artifact.
96
+ * @param taskId - Task ID containing the artifact.
97
+ * @param artifactId - The artifact's unique identifier (from the task's artifacts list).
98
+ * @param jsonPath - Dot-separated path to navigate into the data (e.g. "results.items").
99
+ * @param rows - Row selection for list data. Examples: "0" (single row), "0-10" (range),
100
+ * "0,2,5" (specific rows), "all" (every row).
101
+ * @param columns - Column selection for tabular data (list of objects). Examples:
102
+ * "name" (single column), "name,age" (multiple columns), "all" (every column).
103
+ */
104
+ viewDataArtifact(agentId: string, taskId: string, artifactId: string, jsonPath?: string | null, rows?: string | null, columns?: string | null): Promise<Record<string, unknown>>;
105
+ /**
106
+ * Convert an A2A Message to MessageForLLM.
107
+ *
108
+ * Combines all TextParts into a single TextPartForLLM.
109
+ * FileParts are ignored; file handling is done at the artifact level.
110
+ */
111
+ private buildMessageForLlm;
112
+ /** Convert a Task to TaskForLLM with artifact minimization and file path queries. */
113
+ private buildTaskForLlm;
114
+ /**
115
+ * Look up an artifact through the resolution chain.
116
+ *
117
+ * 1. Check the task store (local cache)
118
+ * 2. Fetch fresh via session.getTask (remote retrieval)
119
+ *
120
+ * @returns The Artifact.
121
+ *
122
+ * @throws Error if artifact cannot be found.
123
+ */
124
+ private getArtifact;
125
+ /**
126
+ * Extract text content from artifact parts.
127
+ *
128
+ * @throws Error if artifact does not contain text content.
129
+ */
130
+ private static extractText;
131
+ /**
132
+ * Extract data content from artifact parts.
133
+ *
134
+ * @throws Error if artifact does not contain data content.
135
+ */
136
+ private static extractData;
137
+ /**
138
+ * Parse a rows string into the type expected by DataArtifacts.view.
139
+ *
140
+ * Accepts: "0" (single int), "0-10" (range string), "0,2,5" (comma-separated
141
+ * list of ints), "all" (passthrough string), or null.
142
+ */
143
+ private static parseRows;
144
+ /**
145
+ * Parse a columns string into the type expected by DataArtifacts.view.
146
+ *
147
+ * Accepts: "name" (single column), "name,age" (comma-separated list),
148
+ * "all" (passthrough string), or null.
149
+ */
150
+ private static parseColumns;
151
+ }
152
+ //# sourceMappingURL=a2a-tools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"a2a-tools.d.ts","sourceRoot":"","sources":["../../src/client/a2a-tools.ts"],"names":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