a2a-mesh-adapters 1.0.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.
- package/LICENSE +171 -0
- package/README.md +20 -0
- package/dist/index.cjs +386 -0
- package/dist/index.d.cts +184 -0
- package/dist/index.d.mts +184 -0
- package/dist/index.d.ts +184 -0
- package/dist/index.mjs +378 -0
- package/package.json +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
|
34
|
+
available under the License, as indicated by a copyright notice that is included
|
|
35
|
+
in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
47
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
48
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
|
49
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
|
50
|
+
to the Licensor or its representatives, including but not limited to
|
|
51
|
+
communication on electronic mailing lists, source code control systems, and
|
|
52
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
|
53
|
+
the purpose of discussing and improving the Work, but excluding communication
|
|
54
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
|
55
|
+
owner as "Not a Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License.
|
|
62
|
+
|
|
63
|
+
Subject to the terms and conditions of this License, each Contributor hereby
|
|
64
|
+
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
|
65
|
+
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
|
66
|
+
publicly display, publicly perform, sublicense, and distribute the Work and such
|
|
67
|
+
Derivative Works in Source or Object form.
|
|
68
|
+
|
|
69
|
+
3. Grant of Patent License.
|
|
70
|
+
|
|
71
|
+
Subject to the terms and conditions of this License, each Contributor hereby
|
|
72
|
+
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
|
73
|
+
irrevocable (except as stated in this section) patent license to make, have
|
|
74
|
+
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
|
75
|
+
such license applies only to those patent claims licensable by such Contributor
|
|
76
|
+
that are necessarily infringed by their Contribution(s) alone or by combination
|
|
77
|
+
of their Contribution(s) with the Work to which such Contribution(s) was
|
|
78
|
+
submitted. If You institute patent litigation against any entity alleging that
|
|
79
|
+
the Work or a Contribution incorporated within the Work constitutes direct or
|
|
80
|
+
contributory patent infringement, then any patent licenses granted to You under
|
|
81
|
+
this License for that Work shall terminate as of the date such litigation is
|
|
82
|
+
filed.
|
|
83
|
+
|
|
84
|
+
4. Redistribution.
|
|
85
|
+
|
|
86
|
+
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
|
87
|
+
in any medium, with or without modifications, and in Source or Object form,
|
|
88
|
+
provided that You meet the following conditions:
|
|
89
|
+
|
|
90
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
91
|
+
this License; and
|
|
92
|
+
|
|
93
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
94
|
+
You changed the files; and
|
|
95
|
+
|
|
96
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
97
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
98
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
99
|
+
of the Derivative Works; and
|
|
100
|
+
|
|
101
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
102
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
103
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
104
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
105
|
+
following places: within a NOTICE text file distributed as part of the
|
|
106
|
+
Derivative Works; within the Source form or documentation, if provided along
|
|
107
|
+
with the Derivative Works; or, within a display generated by the Derivative
|
|
108
|
+
Works, if and wherever such third-party notices normally appear. The contents of
|
|
109
|
+
the NOTICE file are for informational purposes only and do not modify the
|
|
110
|
+
License. You may add Your own attribution notices within Derivative Works that
|
|
111
|
+
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
|
112
|
+
provided that such additional attribution notices cannot be construed as
|
|
113
|
+
modifying the License.
|
|
114
|
+
|
|
115
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
116
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
117
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
118
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
119
|
+
with the conditions stated in this License.
|
|
120
|
+
|
|
121
|
+
5. Submission of Contributions.
|
|
122
|
+
|
|
123
|
+
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
|
124
|
+
for inclusion in the Work by You to the Licensor shall be under the terms and
|
|
125
|
+
conditions of this License, without any additional terms or conditions.
|
|
126
|
+
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
|
127
|
+
any separate license agreement you may have executed with Licensor regarding
|
|
128
|
+
such Contributions.
|
|
129
|
+
|
|
130
|
+
6. Trademarks.
|
|
131
|
+
|
|
132
|
+
This License does not grant permission to use the trade names, trademarks,
|
|
133
|
+
service marks, or product names of the Licensor, except as required for
|
|
134
|
+
reasonable and customary use in describing the origin of the Work and
|
|
135
|
+
reproducing the content of the NOTICE file.
|
|
136
|
+
|
|
137
|
+
7. Disclaimer of Warranty.
|
|
138
|
+
|
|
139
|
+
Unless required by applicable law or agreed to in writing, Licensor provides the
|
|
140
|
+
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
141
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
|
142
|
+
including, without limitation, any warranties or conditions of TITLE,
|
|
143
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
|
144
|
+
solely responsible for determining the appropriateness of using or
|
|
145
|
+
redistributing the Work and assume any risks associated with Your exercise of
|
|
146
|
+
permissions under this License.
|
|
147
|
+
|
|
148
|
+
8. Limitation of Liability.
|
|
149
|
+
|
|
150
|
+
In no event and under no legal theory, whether in tort (including negligence),
|
|
151
|
+
contract, or otherwise, unless required by applicable law (such as deliberate
|
|
152
|
+
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
|
153
|
+
liable to You for damages, including any direct, indirect, special, incidental,
|
|
154
|
+
or consequential damages of any character arising as a result of this License or
|
|
155
|
+
out of the use or inability to use the Work, including but not limited to
|
|
156
|
+
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
|
157
|
+
any and all other commercial damages or losses, even if such Contributor has
|
|
158
|
+
been advised of the possibility of such damages.
|
|
159
|
+
|
|
160
|
+
9. Accepting Warranty or Additional Liability.
|
|
161
|
+
|
|
162
|
+
While redistributing the Work or Derivative Works thereof, You may choose to
|
|
163
|
+
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
|
164
|
+
other liability obligations and/or rights consistent with this License. However,
|
|
165
|
+
in accepting such obligations, You may act only on Your own behalf and on Your
|
|
166
|
+
sole responsibility, not on behalf of any other Contributor, and only if You
|
|
167
|
+
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
|
168
|
+
incurred by, or claims asserted against, such Contributor by reason of your
|
|
169
|
+
accepting any such warranty or additional liability.
|
|
170
|
+
|
|
171
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# a2a-mesh-adapters
|
|
2
|
+
|
|
3
|
+
Framework adapters for running A2A agents on top of popular provider and orchestration SDKs.
|
|
4
|
+
|
|
5
|
+
## Supported adapters
|
|
6
|
+
|
|
7
|
+
- OpenAI
|
|
8
|
+
- Anthropic
|
|
9
|
+
- LangChain
|
|
10
|
+
- Google ADK
|
|
11
|
+
- CrewAI
|
|
12
|
+
- LlamaIndex
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install a2a-mesh-adapters
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Install only the peer SDKs you need for your chosen adapters.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const a2aMesh = require('a2a-mesh');
|
|
4
|
+
|
|
5
|
+
class BaseAdapter extends a2aMesh.A2AServer {
|
|
6
|
+
constructor(card, options = {}) {
|
|
7
|
+
super(a2aMesh.normalizeAgentCard(card), options);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class OpenAIAdapter extends BaseAdapter {
|
|
12
|
+
client;
|
|
13
|
+
model;
|
|
14
|
+
systemPrompt;
|
|
15
|
+
constructor(card, client, model = "gpt-4o", systemPrompt) {
|
|
16
|
+
super(a2aMesh.normalizeAgentCard(card));
|
|
17
|
+
this.client = client;
|
|
18
|
+
this.model = model;
|
|
19
|
+
this.systemPrompt = systemPrompt;
|
|
20
|
+
}
|
|
21
|
+
async handleTask(task, message) {
|
|
22
|
+
a2aMesh.logger.info(`OpenAI processing task ${task.id}`);
|
|
23
|
+
const openAiMessages = [];
|
|
24
|
+
if (this.systemPrompt) {
|
|
25
|
+
openAiMessages.push({ role: "system", content: this.systemPrompt });
|
|
26
|
+
}
|
|
27
|
+
if (task.history) {
|
|
28
|
+
for (const histMsg of task.history) {
|
|
29
|
+
if (histMsg.messageId === message.messageId) continue;
|
|
30
|
+
const text = this.extractText(histMsg.parts);
|
|
31
|
+
if (text) {
|
|
32
|
+
openAiMessages.push({
|
|
33
|
+
role: histMsg.role === "agent" ? "assistant" : "user",
|
|
34
|
+
content: text
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const currentText = this.extractText(message.parts);
|
|
40
|
+
if (!currentText) {
|
|
41
|
+
throw new Error("OpenAI Adapter requires text input");
|
|
42
|
+
}
|
|
43
|
+
openAiMessages.push({
|
|
44
|
+
role: message.role === "agent" ? "assistant" : "user",
|
|
45
|
+
content: currentText
|
|
46
|
+
});
|
|
47
|
+
const response = await this.client.chat.completions.create({
|
|
48
|
+
model: this.model,
|
|
49
|
+
messages: openAiMessages
|
|
50
|
+
});
|
|
51
|
+
const outputContent = response.choices[0]?.message?.content;
|
|
52
|
+
if (!outputContent) {
|
|
53
|
+
throw new Error("OpenAI returned empty response");
|
|
54
|
+
}
|
|
55
|
+
const partsOut = [{ type: "text", text: outputContent }];
|
|
56
|
+
const artifact = {
|
|
57
|
+
artifactId: `oai-art-${Date.now()}`,
|
|
58
|
+
name: "OpenAI Response",
|
|
59
|
+
description: "Result generated by OpenAI Chat API",
|
|
60
|
+
parts: partsOut,
|
|
61
|
+
index: 0,
|
|
62
|
+
lastChunk: true
|
|
63
|
+
};
|
|
64
|
+
return [artifact];
|
|
65
|
+
}
|
|
66
|
+
extractText(parts) {
|
|
67
|
+
return parts.filter((p) => p.type === "text").map((p) => p.text).join("\n");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
class LangChainAdapter extends BaseAdapter {
|
|
72
|
+
runnable;
|
|
73
|
+
constructor(card, runnable) {
|
|
74
|
+
super(a2aMesh.normalizeAgentCard(card));
|
|
75
|
+
this.runnable = runnable;
|
|
76
|
+
}
|
|
77
|
+
async handleTask(task, message) {
|
|
78
|
+
a2aMesh.logger.info(`LangChain processing task ${task.id}`);
|
|
79
|
+
const messages = [];
|
|
80
|
+
for (const histMsg of task.history) {
|
|
81
|
+
if (histMsg.messageId === message.messageId) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const text = this.extractText(histMsg.parts);
|
|
85
|
+
if (text) {
|
|
86
|
+
messages.push({
|
|
87
|
+
role: histMsg.role === "agent" ? "assistant" : "user",
|
|
88
|
+
content: text
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const currentText = this.extractText(message.parts);
|
|
93
|
+
if (!currentText) {
|
|
94
|
+
throw new Error("LangChain Adapter requires text input");
|
|
95
|
+
}
|
|
96
|
+
messages.push({ role: message.role === "agent" ? "assistant" : "user", content: currentText });
|
|
97
|
+
const result = await this.runnable.invoke({ messages });
|
|
98
|
+
const outputString = typeof result === "string" ? result : result?.content ? result.content : result?.messages && Array.isArray(result.messages) && result.messages.length > 0 ? result.messages[result.messages.length - 1]?.content ?? JSON.stringify(result) : JSON.stringify(result);
|
|
99
|
+
const partsOut = [{ type: "text", text: outputString }];
|
|
100
|
+
const artifact = {
|
|
101
|
+
artifactId: `lc-art-${Date.now()}`,
|
|
102
|
+
name: "LangChain Output",
|
|
103
|
+
description: "Result generated by LangChain agent",
|
|
104
|
+
parts: partsOut,
|
|
105
|
+
index: 0,
|
|
106
|
+
lastChunk: true
|
|
107
|
+
};
|
|
108
|
+
return [artifact];
|
|
109
|
+
}
|
|
110
|
+
extractText(parts) {
|
|
111
|
+
return parts.filter((p) => p.type === "text").map((p) => p.text).join("\n");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function extractText$3(parts) {
|
|
116
|
+
return parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
117
|
+
}
|
|
118
|
+
class AnthropicAdapter extends BaseAdapter {
|
|
119
|
+
constructor(card, client, model = "claude-opus-4-6", systemPrompt, maxTokens = 4096) {
|
|
120
|
+
super(a2aMesh.normalizeAgentCard(card));
|
|
121
|
+
this.client = client;
|
|
122
|
+
this.model = model;
|
|
123
|
+
this.systemPrompt = systemPrompt;
|
|
124
|
+
this.maxTokens = maxTokens;
|
|
125
|
+
}
|
|
126
|
+
async handleTask(task, message) {
|
|
127
|
+
a2aMesh.logger.info("Anthropic processing task", {
|
|
128
|
+
taskId: task.id,
|
|
129
|
+
...task.contextId ? { contextId: task.contextId } : {}
|
|
130
|
+
});
|
|
131
|
+
const messages = task.history.filter((entry) => entry.messageId !== message.messageId).map((entry) => ({
|
|
132
|
+
role: entry.role === "agent" ? "assistant" : "user",
|
|
133
|
+
content: extractText$3(entry.parts)
|
|
134
|
+
})).filter((entry) => entry.content.length > 0);
|
|
135
|
+
const currentText = extractText$3(message.parts);
|
|
136
|
+
if (!currentText) {
|
|
137
|
+
throw new Error("Anthropic Adapter requires text input");
|
|
138
|
+
}
|
|
139
|
+
messages.push({
|
|
140
|
+
role: message.role === "agent" ? "assistant" : "user",
|
|
141
|
+
content: currentText
|
|
142
|
+
});
|
|
143
|
+
const streamRequested = task.metadata?.stream === true;
|
|
144
|
+
if (streamRequested) {
|
|
145
|
+
const stream = await this.client.messages.create({
|
|
146
|
+
model: this.model,
|
|
147
|
+
max_tokens: this.maxTokens,
|
|
148
|
+
system: this.systemPrompt,
|
|
149
|
+
messages,
|
|
150
|
+
stream: true
|
|
151
|
+
});
|
|
152
|
+
let streamedText = "";
|
|
153
|
+
for await (const event of stream) {
|
|
154
|
+
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
155
|
+
streamedText += event.delta.text;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const artifact2 = {
|
|
159
|
+
artifactId: `anthropic-${Date.now()}`,
|
|
160
|
+
name: "Anthropic Stream Response",
|
|
161
|
+
description: "Result generated by Anthropic Claude stream",
|
|
162
|
+
parts: [{ type: "text", text: streamedText }],
|
|
163
|
+
index: 0,
|
|
164
|
+
lastChunk: true,
|
|
165
|
+
metadata: {
|
|
166
|
+
provider: "anthropic",
|
|
167
|
+
model: this.model,
|
|
168
|
+
streamed: true
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
return [artifact2];
|
|
172
|
+
}
|
|
173
|
+
const response = await this.client.messages.create({
|
|
174
|
+
model: this.model,
|
|
175
|
+
max_tokens: this.maxTokens,
|
|
176
|
+
system: this.systemPrompt,
|
|
177
|
+
messages
|
|
178
|
+
});
|
|
179
|
+
const outputText = response.content.filter((block) => block.type === "text").map((block) => block.text).join("\n");
|
|
180
|
+
const toolUses = response.content.filter((block) => block.type === "tool_use").map((block) => ({
|
|
181
|
+
id: block.id,
|
|
182
|
+
name: block.name,
|
|
183
|
+
input: block.input
|
|
184
|
+
}));
|
|
185
|
+
const artifact = {
|
|
186
|
+
artifactId: `anthropic-${Date.now()}`,
|
|
187
|
+
name: "Anthropic Response",
|
|
188
|
+
description: "Result generated by Anthropic Claude",
|
|
189
|
+
parts: [{ type: "text", text: outputText || JSON.stringify(toolUses, null, 2) }],
|
|
190
|
+
index: 0,
|
|
191
|
+
lastChunk: true,
|
|
192
|
+
metadata: {
|
|
193
|
+
provider: "anthropic",
|
|
194
|
+
model: this.model,
|
|
195
|
+
inputTokens: response.usage.input_tokens,
|
|
196
|
+
outputTokens: response.usage.output_tokens,
|
|
197
|
+
toolUses
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
return [artifact];
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function extractText$2(parts) {
|
|
205
|
+
return parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
206
|
+
}
|
|
207
|
+
class GoogleADKAdapter extends BaseAdapter {
|
|
208
|
+
constructor(card, adkEndpoint, apiKey) {
|
|
209
|
+
super(a2aMesh.normalizeAgentCard(card));
|
|
210
|
+
this.adkEndpoint = adkEndpoint;
|
|
211
|
+
this.apiKey = apiKey;
|
|
212
|
+
}
|
|
213
|
+
async handleTask(task, message) {
|
|
214
|
+
a2aMesh.logger.info("Google ADK processing task", {
|
|
215
|
+
taskId: task.id,
|
|
216
|
+
...task.contextId ? { contextId: task.contextId } : {}
|
|
217
|
+
});
|
|
218
|
+
const history = task.history.map((entry) => ({
|
|
219
|
+
role: entry.role === "agent" ? "model" : "user",
|
|
220
|
+
content: extractText$2(entry.parts)
|
|
221
|
+
}));
|
|
222
|
+
const response = await fetch(this.adkEndpoint, {
|
|
223
|
+
method: "POST",
|
|
224
|
+
headers: {
|
|
225
|
+
"Content-Type": "application/json",
|
|
226
|
+
...this.apiKey ? { "x-goog-api-key": this.apiKey } : {}
|
|
227
|
+
},
|
|
228
|
+
body: JSON.stringify({
|
|
229
|
+
taskId: task.id,
|
|
230
|
+
contextId: task.contextId,
|
|
231
|
+
message: extractText$2(message.parts),
|
|
232
|
+
history
|
|
233
|
+
})
|
|
234
|
+
});
|
|
235
|
+
if (!response.ok) {
|
|
236
|
+
throw new Error(`Google ADK request failed with status ${response.status}`);
|
|
237
|
+
}
|
|
238
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
239
|
+
if (contentType.includes("text/event-stream")) {
|
|
240
|
+
const text = await response.text();
|
|
241
|
+
const chunks = text.split("\n").filter((line) => line.startsWith("data: ")).map((line) => line.slice("data: ".length)).join("\n");
|
|
242
|
+
const artifact2 = {
|
|
243
|
+
artifactId: `google-adk-${Date.now()}`,
|
|
244
|
+
name: "Google ADK Stream Response",
|
|
245
|
+
parts: [{ type: "text", text: chunks }],
|
|
246
|
+
index: 0,
|
|
247
|
+
lastChunk: true,
|
|
248
|
+
metadata: {
|
|
249
|
+
provider: "google-adk",
|
|
250
|
+
streamed: true
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
return [artifact2];
|
|
254
|
+
}
|
|
255
|
+
const json = await response.json();
|
|
256
|
+
const artifact = {
|
|
257
|
+
artifactId: `google-adk-${Date.now()}`,
|
|
258
|
+
name: "Google ADK Response",
|
|
259
|
+
parts: [{ type: "text", text: json.output ?? json.result ?? "" }],
|
|
260
|
+
index: 0,
|
|
261
|
+
lastChunk: true,
|
|
262
|
+
metadata: {
|
|
263
|
+
provider: "google-adk",
|
|
264
|
+
...json.metadata ?? {}
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
return [artifact];
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function extractText$1(parts) {
|
|
272
|
+
return parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
273
|
+
}
|
|
274
|
+
class LlamaIndexAdapter extends BaseAdapter {
|
|
275
|
+
constructor(card, engine) {
|
|
276
|
+
super(a2aMesh.normalizeAgentCard(card));
|
|
277
|
+
this.engine = engine;
|
|
278
|
+
}
|
|
279
|
+
async handleTask(task, message) {
|
|
280
|
+
a2aMesh.logger.info("LlamaIndex processing task", {
|
|
281
|
+
taskId: task.id,
|
|
282
|
+
...task.contextId ? { contextId: task.contextId } : {}
|
|
283
|
+
});
|
|
284
|
+
const input = extractText$1(message.parts);
|
|
285
|
+
if (!input) {
|
|
286
|
+
throw new Error("LlamaIndex Adapter requires text input");
|
|
287
|
+
}
|
|
288
|
+
if (this.isChatEngine(this.engine)) {
|
|
289
|
+
const chatHistory = task.history.filter((entry) => entry.messageId !== message.messageId).map((entry) => ({
|
|
290
|
+
role: entry.role === "agent" ? "assistant" : "user",
|
|
291
|
+
content: extractText$1(entry.parts)
|
|
292
|
+
}));
|
|
293
|
+
const response2 = await this.engine.chat({
|
|
294
|
+
message: input,
|
|
295
|
+
chatHistory,
|
|
296
|
+
stream: task.metadata?.stream === true
|
|
297
|
+
});
|
|
298
|
+
return [this.toArtifact(response2, "LlamaIndex Chat Response")];
|
|
299
|
+
}
|
|
300
|
+
const response = await this.engine.query({ query: input });
|
|
301
|
+
return [this.toArtifact(response, "LlamaIndex Query Response")];
|
|
302
|
+
}
|
|
303
|
+
isChatEngine(engine) {
|
|
304
|
+
return "chat" in engine;
|
|
305
|
+
}
|
|
306
|
+
toArtifact(response, name) {
|
|
307
|
+
if (this.isAsyncIterable(response)) {
|
|
308
|
+
throw new Error("Streaming LlamaIndex responses are not supported in handleTask");
|
|
309
|
+
}
|
|
310
|
+
const sourceNodes = typeof response === "string" ? [] : response.sourceNodes ?? [];
|
|
311
|
+
const text = typeof response === "string" ? response : response.response ?? response.message ?? JSON.stringify(response, null, 2);
|
|
312
|
+
return {
|
|
313
|
+
artifactId: `llamaindex-${Date.now()}`,
|
|
314
|
+
name,
|
|
315
|
+
parts: [{ type: "text", text }],
|
|
316
|
+
index: 0,
|
|
317
|
+
lastChunk: true,
|
|
318
|
+
metadata: {
|
|
319
|
+
provider: "llamaindex",
|
|
320
|
+
sourceNodes: sourceNodes.map((node) => ({
|
|
321
|
+
score: node.score,
|
|
322
|
+
metadata: node.node?.metadata ?? {}
|
|
323
|
+
}))
|
|
324
|
+
},
|
|
325
|
+
extensions: sourceNodes.length > 0 ? ["urn:a2a:extensions:llamaindex/source-nodes"] : []
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
isAsyncIterable(value) {
|
|
329
|
+
return typeof value === "object" && value !== null && Symbol.asyncIterator in value;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function extractText(parts) {
|
|
334
|
+
return parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
335
|
+
}
|
|
336
|
+
class CrewAIAdapter extends BaseAdapter {
|
|
337
|
+
constructor(card, bridgeUrl) {
|
|
338
|
+
super(a2aMesh.normalizeAgentCard(card));
|
|
339
|
+
this.bridgeUrl = bridgeUrl;
|
|
340
|
+
}
|
|
341
|
+
async handleTask(task, message) {
|
|
342
|
+
a2aMesh.logger.info("CrewAI bridge processing task", {
|
|
343
|
+
taskId: task.id,
|
|
344
|
+
...task.contextId ? { contextId: task.contextId } : {}
|
|
345
|
+
});
|
|
346
|
+
const response = await fetch(this.bridgeUrl, {
|
|
347
|
+
method: "POST",
|
|
348
|
+
headers: {
|
|
349
|
+
"Content-Type": "application/json"
|
|
350
|
+
},
|
|
351
|
+
body: JSON.stringify({
|
|
352
|
+
taskId: task.id,
|
|
353
|
+
contextId: task.contextId,
|
|
354
|
+
message: extractText(message.parts),
|
|
355
|
+
history: task.history.map((entry) => ({
|
|
356
|
+
role: entry.role,
|
|
357
|
+
content: extractText(entry.parts)
|
|
358
|
+
}))
|
|
359
|
+
})
|
|
360
|
+
});
|
|
361
|
+
if (!response.ok) {
|
|
362
|
+
throw new Error(`CrewAI bridge failed with status ${response.status}`);
|
|
363
|
+
}
|
|
364
|
+
const json = await response.json();
|
|
365
|
+
const artifact = {
|
|
366
|
+
artifactId: `crewai-${Date.now()}`,
|
|
367
|
+
name: "CrewAI Response",
|
|
368
|
+
parts: [{ type: "text", text: json.output ?? "" }],
|
|
369
|
+
index: 0,
|
|
370
|
+
lastChunk: true,
|
|
371
|
+
metadata: {
|
|
372
|
+
provider: "crewai",
|
|
373
|
+
...json.metadata ?? {}
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
return [artifact];
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
exports.AnthropicAdapter = AnthropicAdapter;
|
|
381
|
+
exports.BaseAdapter = BaseAdapter;
|
|
382
|
+
exports.CrewAIAdapter = CrewAIAdapter;
|
|
383
|
+
exports.GoogleADKAdapter = GoogleADKAdapter;
|
|
384
|
+
exports.LangChainAdapter = LangChainAdapter;
|
|
385
|
+
exports.LlamaIndexAdapter = LlamaIndexAdapter;
|
|
386
|
+
exports.OpenAIAdapter = OpenAIAdapter;
|