@botbotgo/agent-harness 0.0.308 → 0.0.310
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/README.md +17 -1
- package/README.zh.md +17 -1
- package/dist/acp.d.ts +1 -116
- package/dist/acp.js +1 -310
- package/dist/api.d.ts +4 -2
- package/dist/api.js +4 -1
- package/dist/cli/chat-interactive.d.ts +24 -0
- package/dist/cli/chat-interactive.js +244 -0
- package/dist/cli/chat-rendering.d.ts +9 -0
- package/dist/cli/chat-rendering.js +102 -0
- package/dist/cli/chat-stream.d.ts +23 -0
- package/dist/cli/chat-stream.js +330 -0
- package/dist/cli/chat-ui.d.ts +20 -0
- package/dist/cli/chat-ui.js +198 -0
- package/dist/cli/chat-workspace.d.ts +15 -0
- package/dist/cli/chat-workspace.js +205 -0
- package/dist/cli/main.d.ts +52 -0
- package/dist/cli/main.js +323 -0
- package/dist/cli/managed-service-commands.d.ts +23 -0
- package/dist/cli/managed-service-commands.js +63 -0
- package/dist/cli/managed-service.d.ts +27 -0
- package/dist/cli/managed-service.js +61 -0
- package/dist/cli/options-init-chat.d.ts +16 -0
- package/dist/cli/options-init-chat.js +108 -0
- package/dist/cli/options-runtime.d.ts +27 -0
- package/dist/cli/options-runtime.js +158 -0
- package/dist/cli/options-serve.d.ts +24 -0
- package/dist/cli/options-serve.js +166 -0
- package/dist/cli/options.d.ts +5 -0
- package/dist/cli/options.js +47 -0
- package/dist/cli/process-guards.d.ts +14 -0
- package/dist/cli/process-guards.js +139 -0
- package/dist/cli/request-tree.d.ts +12 -0
- package/dist/cli/request-tree.js +296 -0
- package/dist/cli/runtime-commands.d.ts +15 -0
- package/dist/cli/runtime-commands.js +247 -0
- package/dist/cli/runtime-output.d.ts +5 -0
- package/dist/cli/runtime-output.js +124 -0
- package/dist/cli/server-commands.d.ts +36 -0
- package/dist/cli/server-commands.js +250 -0
- package/dist/cli/workspace.d.ts +6 -0
- package/dist/cli/workspace.js +71 -0
- package/dist/cli.d.ts +1 -67
- package/dist/cli.js +2 -2734
- package/dist/client/acp.d.ts +1 -50
- package/dist/client/acp.js +1 -219
- package/dist/client/in-process.d.ts +5 -5
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.js +1 -1
- package/dist/contracts/runtime-evaluation.d.ts +103 -0
- package/dist/contracts/runtime-evaluation.js +1 -0
- package/dist/contracts/runtime-memory.d.ts +162 -0
- package/dist/contracts/runtime-memory.js +1 -0
- package/dist/contracts/runtime-observability.d.ts +248 -0
- package/dist/contracts/runtime-observability.js +1 -0
- package/dist/contracts/runtime-requests.d.ts +342 -0
- package/dist/contracts/runtime-requests.js +1 -0
- package/dist/contracts/runtime-scheduling.d.ts +146 -0
- package/dist/contracts/runtime-scheduling.js +1 -0
- package/dist/contracts/runtime.d.ts +5 -1042
- package/dist/contracts/runtime.js +27 -1
- package/dist/flow/build-flow-graph.js +4 -875
- package/dist/flow/flow-graph-normalization.d.ts +56 -0
- package/dist/flow/flow-graph-normalization.js +214 -0
- package/dist/flow/flow-graph-runtime.d.ts +8 -0
- package/dist/flow/flow-graph-runtime.js +107 -0
- package/dist/flow/flow-graph-upstream.d.ts +18 -0
- package/dist/flow/flow-graph-upstream.js +498 -0
- package/dist/flow/types.d.ts +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/init-project.d.ts +1 -12
- package/dist/init-project.js +1 -651
- package/dist/{procedural → knowledge/procedural}/manager.d.ts +3 -3
- package/dist/{procedural → knowledge/procedural}/manager.js +6 -6
- package/dist/mcp.d.ts +2 -62
- package/dist/mcp.js +2 -253
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -1
- package/dist/persistence/file-store.js +1 -1
- package/dist/persistence/sqlite-runtime.d.ts +19 -0
- package/dist/persistence/sqlite-runtime.js +86 -0
- package/dist/persistence/sqlite-store.js +11 -99
- package/dist/{request-events.d.ts → projections/request-events.d.ts} +1 -1
- package/dist/{upstream-events.js → projections/upstream-events.js} +1 -1
- package/dist/protocol/a2a/http-discovery.d.ts +39 -0
- package/dist/protocol/a2a/http-discovery.js +178 -0
- package/dist/protocol/a2a/http-rpc.d.ts +28 -0
- package/dist/protocol/a2a/http-rpc.js +623 -0
- package/dist/protocol/a2a/http.d.ts +72 -1
- package/dist/protocol/a2a/http.js +14 -1124
- package/dist/protocol/a2a/task-state.d.ts +29 -0
- package/dist/protocol/a2a/task-state.js +317 -0
- package/dist/protocol/acp/client.js +1 -1
- package/dist/protocol/acp/harness-client.d.ts +50 -0
- package/dist/protocol/acp/harness-client.js +219 -0
- package/dist/protocol/acp/server.d.ts +116 -0
- package/dist/protocol/acp/server.js +310 -0
- package/dist/protocol/ag-ui/http.js +1 -1
- package/dist/protocol/mcp/server.d.ts +76 -0
- package/dist/protocol/mcp/server.js +428 -0
- package/dist/resource/backend/workspace-scoped-backend.d.ts +40 -0
- package/dist/resource/backend/workspace-scoped-backend.js +296 -0
- package/dist/resource/mcp/tool-support.d.ts +35 -0
- package/dist/resource/mcp/tool-support.js +296 -0
- package/dist/resource/mcp-tool-support.d.ts +2 -35
- package/dist/resource/mcp-tool-support.js +2 -296
- package/dist/resource/providers/resource-provider.d.ts +22 -0
- package/dist/resource/providers/resource-provider.js +215 -0
- package/dist/resource/resource-impl.d.ts +3 -33
- package/dist/resource/resource-impl.js +2 -808
- package/dist/resource/resource-types.d.ts +33 -0
- package/dist/resource/resource-types.js +1 -0
- package/dist/resource/tools/function-tool-resolver.d.ts +2 -0
- package/dist/resource/tools/function-tool-resolver.js +306 -0
- package/dist/runtime/adapter/middleware-assembly.js +1 -1
- package/dist/runtime/adapter/model/invocation-request.js +2 -2
- package/dist/runtime/adapter/model/message-assembly.js +1 -1
- package/dist/runtime/agent-runtime-adapter.d.ts +3 -63
- package/dist/runtime/agent-runtime-adapter.js +5 -233
- package/dist/runtime/agent-runtime-assembly.d.ts +67 -0
- package/dist/runtime/agent-runtime-assembly.js +211 -0
- package/dist/runtime/harness/background-runtime.d.ts +1 -1
- package/dist/runtime/harness/events/event-sink.js +1 -1
- package/dist/runtime/harness/events/runtime-event-operations.d.ts +1 -1
- package/dist/runtime/harness/events/streaming.js +1 -1
- package/dist/runtime/harness/public-shapes.d.ts +43 -0
- package/dist/runtime/harness/public-shapes.js +186 -0
- package/dist/runtime/harness/run/inspection.js +2 -2
- package/dist/runtime/harness/run/resources.js +1 -1
- package/dist/runtime/harness/run/surface-semantics.js +1 -1
- package/dist/runtime/harness/system/inventory.d.ts +1 -1
- package/dist/runtime/harness/system/inventory.js +2 -2
- package/dist/runtime/harness/system/policy-engine.js +1 -1
- package/dist/runtime/harness/system/runtime-memory-manager.js +1 -1
- package/dist/runtime/harness/system/skill-requirements.d.ts +1 -1
- package/dist/runtime/harness/system/skill-requirements.js +1 -1
- package/dist/runtime/harness.d.ts +3 -2
- package/dist/runtime/harness.js +11 -191
- package/dist/runtime/maintenance/checkpoint-maintenance.js +1 -1
- package/dist/runtime/maintenance/runtime-record-maintenance.js +1 -1
- package/dist/runtime/parsing/output-content.d.ts +11 -0
- package/dist/runtime/parsing/output-content.js +442 -0
- package/dist/runtime/parsing/output-parsing.d.ts +3 -29
- package/dist/runtime/parsing/output-parsing.js +3 -806
- package/dist/runtime/parsing/output-recovery.d.ts +14 -0
- package/dist/runtime/parsing/output-recovery.js +288 -0
- package/dist/runtime/parsing/output-tool-args.d.ts +4 -0
- package/dist/runtime/parsing/output-tool-args.js +120 -0
- package/dist/runtime/support/runtime-factories.js +1 -1
- package/dist/scaffold/init-project.d.ts +12 -0
- package/dist/scaffold/init-project.js +651 -0
- package/dist/{extensions.d.ts → tooling/extensions.d.ts} +1 -1
- package/dist/{extensions.js → tooling/extensions.js} +3 -3
- package/dist/{tool-modules.d.ts → tooling/module-loader.d.ts} +1 -1
- package/dist/{tool-modules.js → tooling/module-loader.js} +2 -2
- package/dist/workspace/agent-binding-compiler.js +2 -2
- package/dist/workspace/compile.js +2 -2
- package/dist/workspace/object-loader-paths.d.ts +11 -0
- package/dist/workspace/object-loader-paths.js +75 -0
- package/dist/workspace/object-loader-readers.d.ts +21 -0
- package/dist/workspace/object-loader-readers.js +187 -0
- package/dist/workspace/object-loader.d.ts +0 -1
- package/dist/workspace/object-loader.js +6 -260
- package/dist/workspace/resource-compilers.js +1 -1
- package/dist/workspace/support/discovery.js +1 -1
- package/package.json +1 -1
- package/dist/runtime/adapter/index.d.ts +0 -13
- package/dist/runtime/adapter/index.js +0 -13
- package/dist/runtime/harness/index.d.ts +0 -19
- package/dist/runtime/harness/index.js +0 -19
- package/dist/runtime/maintenance/index.d.ts +0 -4
- package/dist/runtime/maintenance/index.js +0 -4
- package/dist/runtime/parsing/index.d.ts +0 -2
- package/dist/runtime/parsing/index.js +0 -2
- package/dist/runtime/support/index.d.ts +0 -4
- package/dist/runtime/support/index.js +0 -4
- package/dist/workspace/support/index.d.ts +0 -2
- package/dist/workspace/support/index.js +0 -2
- /package/dist/{procedural → knowledge/procedural}/config.d.ts +0 -0
- /package/dist/{procedural → knowledge/procedural}/config.js +0 -0
- /package/dist/{procedural → knowledge/procedural}/index.d.ts +0 -0
- /package/dist/{procedural → knowledge/procedural}/index.js +0 -0
- /package/dist/{presentation.d.ts → projections/presentation.d.ts} +0 -0
- /package/dist/{presentation.js → projections/presentation.js} +0 -0
- /package/dist/{request-events.js → projections/request-events.js} +0 -0
- /package/dist/{upstream-events.d.ts → projections/upstream-events.d.ts} +0 -0
- /package/dist/runtime/{support → env}/runtime-env.d.ts +0 -0
- /package/dist/runtime/{support → env}/runtime-env.js +0 -0
- /package/dist/runtime/{support → layout}/runtime-layout.d.ts +0 -0
- /package/dist/runtime/{support → layout}/runtime-layout.js +0 -0
- /package/dist/runtime/{support → prompts}/runtime-prompts.d.ts +0 -0
- /package/dist/runtime/{support → prompts}/runtime-prompts.js +0 -0
- /package/dist/runtime/{support → skills}/skill-metadata.d.ts +0 -0
- /package/dist/runtime/{support → skills}/skill-metadata.js +0 -0
|
@@ -1,910 +1,7 @@
|
|
|
1
1
|
import { createServer } from "node:http";
|
|
2
|
-
import { AGENT_HARNESS_VERSION } from "../../package-version.js";
|
|
3
2
|
import { createPersistentId } from "../../utils/id.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const source = typeof value === "string" && value.trim().length > 0 ? value.trim() : fallback;
|
|
7
|
-
return source.startsWith("/") ? source : `/${source}`;
|
|
8
|
-
}
|
|
9
|
-
function writeJson(response, statusCode, payload) {
|
|
10
|
-
response.statusCode = statusCode;
|
|
11
|
-
response.setHeader("content-type", "application/json; charset=utf-8");
|
|
12
|
-
response.end(JSON.stringify(payload));
|
|
13
|
-
}
|
|
14
|
-
function writeA2aDiscoveryHeaders(response) {
|
|
15
|
-
response.setHeader("A2A-Version", "1.0");
|
|
16
|
-
response.setHeader("A2A-Supported-Versions", SUPPORTED_A2A_VERSIONS.join(", "));
|
|
17
|
-
}
|
|
18
|
-
function normalizeNonEmptyStringArray(value) {
|
|
19
|
-
if (!Array.isArray(value)) {
|
|
20
|
-
return undefined;
|
|
21
|
-
}
|
|
22
|
-
const items = value.map((item) => item.trim()).filter((item) => item.length > 0);
|
|
23
|
-
return items.length > 0 ? items : undefined;
|
|
24
|
-
}
|
|
25
|
-
function buildDiscoveryMetadata(options) {
|
|
26
|
-
const registryUrls = normalizeNonEmptyStringArray(options.registryUrls);
|
|
27
|
-
const signature = options.agentCardSignature?.signature?.trim();
|
|
28
|
-
const algorithm = options.agentCardSignature?.algorithm?.trim();
|
|
29
|
-
const keyId = options.agentCardSignature?.keyId?.trim();
|
|
30
|
-
if (!registryUrls && (!signature || !algorithm)) {
|
|
31
|
-
return undefined;
|
|
32
|
-
}
|
|
33
|
-
return {
|
|
34
|
-
...(registryUrls ? { registryUrls } : {}),
|
|
35
|
-
...(signature && algorithm
|
|
36
|
-
? {
|
|
37
|
-
signedAgentCard: {
|
|
38
|
-
algorithm,
|
|
39
|
-
signature,
|
|
40
|
-
...(keyId ? { keyId } : {}),
|
|
41
|
-
},
|
|
42
|
-
}
|
|
43
|
-
: {}),
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
function writeOptionalAgentCardIntegrityHeaders(response, options) {
|
|
47
|
-
const registryUrls = normalizeNonEmptyStringArray(options.registryUrls);
|
|
48
|
-
if (registryUrls) {
|
|
49
|
-
response.setHeader("X-A2A-Registry-Urls", registryUrls.join(", "));
|
|
50
|
-
}
|
|
51
|
-
const signature = options.agentCardSignature?.signature?.trim();
|
|
52
|
-
const algorithm = options.agentCardSignature?.algorithm?.trim();
|
|
53
|
-
const keyId = options.agentCardSignature?.keyId?.trim();
|
|
54
|
-
if (signature && algorithm) {
|
|
55
|
-
response.setHeader("X-A2A-Agent-Card-Signature", signature);
|
|
56
|
-
response.setHeader("X-A2A-Agent-Card-Alg", algorithm);
|
|
57
|
-
if (keyId) {
|
|
58
|
-
response.setHeader("X-A2A-Agent-Card-Key-Id", keyId);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
function writeSseHeaders(response) {
|
|
63
|
-
response.statusCode = 200;
|
|
64
|
-
response.setHeader("content-type", "text/event-stream; charset=utf-8");
|
|
65
|
-
response.setHeader("cache-control", "no-cache, no-transform");
|
|
66
|
-
response.setHeader("connection", "keep-alive");
|
|
67
|
-
}
|
|
68
|
-
function writeSseEvent(response, event, payload) {
|
|
69
|
-
return new Promise((resolve, reject) => {
|
|
70
|
-
response.write(`event: ${event}\ndata: ${JSON.stringify(payload)}\n\n`, (error) => {
|
|
71
|
-
if (error) {
|
|
72
|
-
reject(error);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
resolve();
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
function readRequestBody(request) {
|
|
80
|
-
return new Promise((resolve, reject) => {
|
|
81
|
-
const chunks = [];
|
|
82
|
-
request.on("data", (chunk) => {
|
|
83
|
-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
84
|
-
});
|
|
85
|
-
request.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
86
|
-
request.on("error", reject);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
function toError(id, code, message, data) {
|
|
90
|
-
return {
|
|
91
|
-
jsonrpc: "2.0",
|
|
92
|
-
id,
|
|
93
|
-
error: {
|
|
94
|
-
code,
|
|
95
|
-
message,
|
|
96
|
-
...(data === undefined ? {} : { data }),
|
|
97
|
-
},
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function toSuccess(id, result) {
|
|
101
|
-
return {
|
|
102
|
-
jsonrpc: "2.0",
|
|
103
|
-
id,
|
|
104
|
-
result,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
function readServiceParameter(requestUrl, request, name) {
|
|
108
|
-
const headerValue = request.headers[name.toLowerCase()];
|
|
109
|
-
if (typeof headerValue === "string") {
|
|
110
|
-
return headerValue;
|
|
111
|
-
}
|
|
112
|
-
if (Array.isArray(headerValue)) {
|
|
113
|
-
return headerValue.join(",");
|
|
114
|
-
}
|
|
115
|
-
return requestUrl.searchParams.get(name) ?? undefined;
|
|
116
|
-
}
|
|
117
|
-
function parseA2aServiceParameters(requestUrl, request) {
|
|
118
|
-
const requestedVersion = readServiceParameter(requestUrl, request, "A2A-Version")?.trim() ?? "";
|
|
119
|
-
const normalizedVersion = requestedVersion.length === 0 ? "0.3" : requestedVersion;
|
|
120
|
-
if (!SUPPORTED_A2A_VERSIONS.includes(normalizedVersion)) {
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
const requestedExtensions = readServiceParameter(requestUrl, request, "A2A-Extensions") ?? "";
|
|
124
|
-
return {
|
|
125
|
-
version: normalizedVersion,
|
|
126
|
-
extensions: requestedExtensions
|
|
127
|
-
.split(",")
|
|
128
|
-
.map((entry) => entry.trim())
|
|
129
|
-
.filter((entry) => entry.length > 0),
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
function acceptsSse(request) {
|
|
133
|
-
const accept = request.headers.accept;
|
|
134
|
-
if (accept === undefined) {
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
const values = Array.isArray(accept) ? accept : [accept];
|
|
138
|
-
return values.some((value) => value.includes("text/event-stream"));
|
|
139
|
-
}
|
|
140
|
-
function parseTextPart(part) {
|
|
141
|
-
if (!part || typeof part !== "object" || Array.isArray(part)) {
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
const typed = part;
|
|
145
|
-
if (typeof typed.text !== "string"
|
|
146
|
-
|| typed.text.trim().length === 0
|
|
147
|
-
|| (typed.type !== undefined && typed.type !== "text")) {
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
return typed.text;
|
|
151
|
-
}
|
|
152
|
-
function normalizeUserRole(role) {
|
|
153
|
-
return typeof role === "string" ? role.toLowerCase().replace(/^role_/, "") : "";
|
|
154
|
-
}
|
|
155
|
-
function parseMessageSendParams(params) {
|
|
156
|
-
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
157
|
-
throw new Error("A2A message/send requires params object.");
|
|
158
|
-
}
|
|
159
|
-
const typed = params;
|
|
160
|
-
const message = typed.message;
|
|
161
|
-
if (!message || typeof message !== "object" || Array.isArray(message)) {
|
|
162
|
-
throw new Error("A2A message/send requires `message`.");
|
|
163
|
-
}
|
|
164
|
-
const typedMessage = message;
|
|
165
|
-
const role = normalizeUserRole(typedMessage.role);
|
|
166
|
-
if (role !== "user") {
|
|
167
|
-
throw new Error("A2A message/send currently supports only `user` / `ROLE_USER` role messages.");
|
|
168
|
-
}
|
|
169
|
-
const parts = Array.isArray(typedMessage.parts) ? typedMessage.parts : [];
|
|
170
|
-
const textParts = parts.map(parseTextPart).filter((item) => Boolean(item));
|
|
171
|
-
if (textParts.length === 0) {
|
|
172
|
-
throw new Error("A2A message/send requires at least one text part.");
|
|
173
|
-
}
|
|
174
|
-
const sessionId = typeof typed.contextId === "string" && typed.contextId.trim().length > 0
|
|
175
|
-
? typed.contextId.trim()
|
|
176
|
-
: typeof typedMessage.contextId === "string" && typedMessage.contextId.trim().length > 0
|
|
177
|
-
? typedMessage.contextId.trim()
|
|
178
|
-
: undefined;
|
|
179
|
-
const agentId = typeof typed.agentId === "string" && typed.agentId.trim().length > 0
|
|
180
|
-
? typed.agentId.trim()
|
|
181
|
-
: undefined;
|
|
182
|
-
const metadata = typed.metadata;
|
|
183
|
-
const invocation = metadata && typeof metadata === "object" && !Array.isArray(metadata)
|
|
184
|
-
? metadata
|
|
185
|
-
: undefined;
|
|
186
|
-
const configuration = typed.configuration;
|
|
187
|
-
const nestedPushNotificationConfig = configuration && typeof configuration === "object" && !Array.isArray(configuration)
|
|
188
|
-
? configuration.pushNotificationConfig
|
|
189
|
-
?? configuration.taskPushNotificationConfig
|
|
190
|
-
: undefined;
|
|
191
|
-
const pushNotificationConfig = parsePendingPushNotificationConfig(typed.pushNotificationConfig ?? nestedPushNotificationConfig);
|
|
192
|
-
return {
|
|
193
|
-
input: textParts.join("\n\n"),
|
|
194
|
-
...(agentId ? { agentId } : {}),
|
|
195
|
-
...(sessionId ? { sessionId } : {}),
|
|
196
|
-
...(invocation ? { invocation } : {}),
|
|
197
|
-
...(pushNotificationConfig ? { pushNotificationConfig } : {}),
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
function parsePushNotificationAuthenticationInfo(value) {
|
|
201
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
202
|
-
return undefined;
|
|
203
|
-
}
|
|
204
|
-
const typed = value;
|
|
205
|
-
const schemes = Array.isArray(typed.schemes)
|
|
206
|
-
? typed.schemes
|
|
207
|
-
.filter((entry) => typeof entry === "string" && entry.trim().length > 0)
|
|
208
|
-
.map((entry) => entry.trim())
|
|
209
|
-
: [];
|
|
210
|
-
if (schemes.length === 0) {
|
|
211
|
-
return undefined;
|
|
212
|
-
}
|
|
213
|
-
return {
|
|
214
|
-
schemes,
|
|
215
|
-
...(typeof typed.credentials === "string" && typed.credentials.trim().length > 0
|
|
216
|
-
? { credentials: typed.credentials.trim() }
|
|
217
|
-
: {}),
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
function parsePendingPushNotificationConfig(value) {
|
|
221
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
222
|
-
return undefined;
|
|
223
|
-
}
|
|
224
|
-
const typed = value;
|
|
225
|
-
const rawUrl = typeof typed.url === "string" ? typed.url.trim() : "";
|
|
226
|
-
if (rawUrl.length === 0) {
|
|
227
|
-
throw new Error("A2A push notification config requires `url`.");
|
|
228
|
-
}
|
|
229
|
-
const parsedUrl = new URL(rawUrl);
|
|
230
|
-
if (parsedUrl.protocol !== "http:" && parsedUrl.protocol !== "https:") {
|
|
231
|
-
throw new Error("A2A push notification config requires an http or https URL.");
|
|
232
|
-
}
|
|
233
|
-
const authentication = parsePushNotificationAuthenticationInfo(typed.authentication);
|
|
234
|
-
return {
|
|
235
|
-
url: parsedUrl.toString(),
|
|
236
|
-
...(typeof typed.id === "string" && typed.id.trim().length > 0 ? { id: typed.id.trim() } : {}),
|
|
237
|
-
...(typeof typed.token === "string" && typed.token.trim().length > 0 ? { token: typed.token.trim() } : {}),
|
|
238
|
-
...(authentication ? { authentication } : {}),
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
function attachServiceParameters(invocation, serviceParameters) {
|
|
242
|
-
return {
|
|
243
|
-
...(invocation ?? {}),
|
|
244
|
-
protocol: {
|
|
245
|
-
...(invocation?.protocol && typeof invocation.protocol === "object" && !Array.isArray(invocation.protocol)
|
|
246
|
-
? invocation.protocol
|
|
247
|
-
: {}),
|
|
248
|
-
a2a: {
|
|
249
|
-
version: serviceParameters.version,
|
|
250
|
-
...(serviceParameters.extensions.length > 0 ? { extensions: serviceParameters.extensions } : {}),
|
|
251
|
-
},
|
|
252
|
-
},
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
function parseTaskLocatorParams(params) {
|
|
256
|
-
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
257
|
-
throw new Error("A2A task lookup requires params object.");
|
|
258
|
-
}
|
|
259
|
-
const typed = params;
|
|
260
|
-
const taskId = typeof typed.id === "string" && typed.id.trim().length > 0
|
|
261
|
-
? typed.id.trim()
|
|
262
|
-
: typeof typed.taskId === "string" && typed.taskId.trim().length > 0
|
|
263
|
-
? typed.taskId.trim()
|
|
264
|
-
: "";
|
|
265
|
-
if (!taskId) {
|
|
266
|
-
throw new Error("A2A task lookup requires `id`.");
|
|
267
|
-
}
|
|
268
|
-
return { taskId };
|
|
269
|
-
}
|
|
270
|
-
function parseTaskListParams(params) {
|
|
271
|
-
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
272
|
-
return { limit: 20 };
|
|
273
|
-
}
|
|
274
|
-
const typed = params;
|
|
275
|
-
const rawLimit = typeof typed.pageSize === "number" && Number.isFinite(typed.pageSize)
|
|
276
|
-
? typed.pageSize
|
|
277
|
-
: typeof typed.limit === "number" && Number.isFinite(typed.limit)
|
|
278
|
-
? typed.limit
|
|
279
|
-
: 20;
|
|
280
|
-
const limitValue = typeof rawLimit === "number" && Number.isFinite(rawLimit)
|
|
281
|
-
? Math.max(1, Math.min(100, Math.trunc(rawLimit)))
|
|
282
|
-
: 20;
|
|
283
|
-
return {
|
|
284
|
-
...(typeof typed.agentId === "string" && typed.agentId.trim().length > 0 ? { agentId: typed.agentId.trim() } : {}),
|
|
285
|
-
...(typeof typed.contextId === "string" && typed.contextId.trim().length > 0 ? { contextId: typed.contextId.trim() } : {}),
|
|
286
|
-
...(typeof typed.status === "string" && typed.status.trim().length > 0
|
|
287
|
-
? { state: typed.status.trim() }
|
|
288
|
-
: typeof typed.state === "string" && typed.state.trim().length > 0
|
|
289
|
-
? { state: typed.state.trim() }
|
|
290
|
-
: {}),
|
|
291
|
-
...(typeof typed.pageToken === "string" && typed.pageToken.trim().length > 0
|
|
292
|
-
? { cursor: typed.pageToken.trim() }
|
|
293
|
-
: typeof typed.cursor === "string" && typed.cursor.trim().length > 0
|
|
294
|
-
? { cursor: typed.cursor.trim() }
|
|
295
|
-
: {}),
|
|
296
|
-
limit: limitValue,
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
function parsePushNotificationLocatorParams(params) {
|
|
300
|
-
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
301
|
-
throw new Error("A2A push notification lookup requires params object.");
|
|
302
|
-
}
|
|
303
|
-
const typed = params;
|
|
304
|
-
const taskId = typeof typed.taskId === "string" && typed.taskId.trim().length > 0 ? typed.taskId.trim() : "";
|
|
305
|
-
const configId = typeof typed.id === "string" && typed.id.trim().length > 0 ? typed.id.trim() : "";
|
|
306
|
-
if (!taskId) {
|
|
307
|
-
throw new Error("A2A push notification lookup requires `taskId`.");
|
|
308
|
-
}
|
|
309
|
-
if (!configId) {
|
|
310
|
-
throw new Error("A2A push notification lookup requires `id`.");
|
|
311
|
-
}
|
|
312
|
-
return { taskId, configId };
|
|
313
|
-
}
|
|
314
|
-
function parsePushNotificationListParams(params) {
|
|
315
|
-
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
316
|
-
throw new Error("A2A push notification list requires params object.");
|
|
317
|
-
}
|
|
318
|
-
const typed = params;
|
|
319
|
-
const taskId = typeof typed.taskId === "string" && typed.taskId.trim().length > 0 ? typed.taskId.trim() : "";
|
|
320
|
-
if (!taskId) {
|
|
321
|
-
throw new Error("A2A push notification list requires `taskId`.");
|
|
322
|
-
}
|
|
323
|
-
const pageSize = typeof typed.pageSize === "number" && Number.isFinite(typed.pageSize)
|
|
324
|
-
? Math.max(1, Math.min(100, Math.trunc(typed.pageSize)))
|
|
325
|
-
: 20;
|
|
326
|
-
return {
|
|
327
|
-
taskId,
|
|
328
|
-
pageSize,
|
|
329
|
-
...(typeof typed.pageToken === "string" && typed.pageToken.trim().length > 0 ? { pageToken: typed.pageToken.trim() } : {}),
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
function parseCreatePushNotificationConfigParams(params) {
|
|
333
|
-
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
334
|
-
throw new Error("A2A push notification create requires params object.");
|
|
335
|
-
}
|
|
336
|
-
const typed = params;
|
|
337
|
-
const taskId = typeof typed.taskId === "string" && typed.taskId.trim().length > 0 ? typed.taskId.trim() : "";
|
|
338
|
-
if (!taskId) {
|
|
339
|
-
throw new Error("A2A push notification create requires `taskId`.");
|
|
340
|
-
}
|
|
341
|
-
const config = parsePendingPushNotificationConfig(typed.pushNotificationConfig ?? typed.config ?? typed);
|
|
342
|
-
if (!config) {
|
|
343
|
-
throw new Error("A2A push notification create requires a config object.");
|
|
344
|
-
}
|
|
345
|
-
return { taskId, config };
|
|
346
|
-
}
|
|
347
|
-
function mapRequestState(state) {
|
|
348
|
-
switch (state) {
|
|
349
|
-
case "queued":
|
|
350
|
-
return "TASK_STATE_SUBMITTED";
|
|
351
|
-
case "running":
|
|
352
|
-
return "TASK_STATE_WORKING";
|
|
353
|
-
case "waiting_for_approval":
|
|
354
|
-
return "TASK_STATE_INPUT_REQUIRED";
|
|
355
|
-
case "completed":
|
|
356
|
-
return "TASK_STATE_COMPLETED";
|
|
357
|
-
case "cancelled":
|
|
358
|
-
return "TASK_STATE_CANCELED";
|
|
359
|
-
case "failed":
|
|
360
|
-
default:
|
|
361
|
-
return "TASK_STATE_FAILED";
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
function mapTaskStatusToRequestState(state) {
|
|
365
|
-
switch (state?.toLowerCase().replace(/^task_state_/, "").replace(/_/g, "-")) {
|
|
366
|
-
case "submitted":
|
|
367
|
-
return "queued";
|
|
368
|
-
case "working":
|
|
369
|
-
return "running";
|
|
370
|
-
case "input-required":
|
|
371
|
-
return "waiting_for_approval";
|
|
372
|
-
case "completed":
|
|
373
|
-
return "completed";
|
|
374
|
-
case "failed":
|
|
375
|
-
return "failed";
|
|
376
|
-
case "canceled":
|
|
377
|
-
case "cancelled":
|
|
378
|
-
return "cancelled";
|
|
379
|
-
default:
|
|
380
|
-
return undefined;
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
function toTextParts(text) {
|
|
384
|
-
if (typeof text !== "string" || text.length === 0) {
|
|
385
|
-
return [];
|
|
386
|
-
}
|
|
387
|
-
return [{ text, mediaType: "text/plain" }];
|
|
388
|
-
}
|
|
389
|
-
function contentToText(content) {
|
|
390
|
-
if (typeof content === "string") {
|
|
391
|
-
return content;
|
|
392
|
-
}
|
|
393
|
-
if (!Array.isArray(content)) {
|
|
394
|
-
return undefined;
|
|
395
|
-
}
|
|
396
|
-
const text = content
|
|
397
|
-
.filter((part) => part.type === "text" && typeof part.text === "string")
|
|
398
|
-
.map((part) => part.text)
|
|
399
|
-
.join("\n\n")
|
|
400
|
-
.trim();
|
|
401
|
-
return text.length > 0 ? text : undefined;
|
|
402
|
-
}
|
|
403
|
-
function toSessionRecord(session) {
|
|
404
|
-
if (!session) {
|
|
405
|
-
return null;
|
|
406
|
-
}
|
|
407
|
-
return {
|
|
408
|
-
sessionId: session.sessionId,
|
|
409
|
-
entryAgentId: session.entryAgentId,
|
|
410
|
-
currentAgentId: session.currentAgentId,
|
|
411
|
-
currentState: session.currentState,
|
|
412
|
-
latestRequestId: session.latestRequestId,
|
|
413
|
-
createdAt: session.createdAt,
|
|
414
|
-
updatedAt: session.updatedAt,
|
|
415
|
-
messages: session.messages,
|
|
416
|
-
requests: session.requests.map((request) => ({
|
|
417
|
-
requestId: request.requestId,
|
|
418
|
-
sessionId: request.sessionId,
|
|
419
|
-
agentId: request.agentId,
|
|
420
|
-
executionMode: request.executionMode,
|
|
421
|
-
adapterKind: request.adapterKind,
|
|
422
|
-
createdAt: request.createdAt,
|
|
423
|
-
updatedAt: request.updatedAt,
|
|
424
|
-
state: request.state,
|
|
425
|
-
checkpointRef: request.checkpointRef,
|
|
426
|
-
resumable: request.resumable,
|
|
427
|
-
startedAt: request.startedAt,
|
|
428
|
-
endedAt: request.endedAt,
|
|
429
|
-
lastActivityAt: request.lastActivityAt,
|
|
430
|
-
currentAgentId: request.currentAgentId,
|
|
431
|
-
delegationChain: request.delegationChain,
|
|
432
|
-
runtimeSnapshot: request.runtimeSnapshot,
|
|
433
|
-
traceItems: request.traceItems,
|
|
434
|
-
runtimeTimeline: request.runtimeTimeline,
|
|
435
|
-
})),
|
|
436
|
-
pendingDecision: session.pendingDecision,
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
function toRequestRecord(request) {
|
|
440
|
-
if (!request) {
|
|
441
|
-
return null;
|
|
442
|
-
}
|
|
443
|
-
return {
|
|
444
|
-
requestId: request.requestId,
|
|
445
|
-
sessionId: request.sessionId,
|
|
446
|
-
agentId: request.agentId,
|
|
447
|
-
executionMode: request.executionMode,
|
|
448
|
-
adapterKind: request.adapterKind,
|
|
449
|
-
createdAt: request.createdAt,
|
|
450
|
-
updatedAt: request.updatedAt,
|
|
451
|
-
state: request.state,
|
|
452
|
-
checkpointRef: request.checkpointRef,
|
|
453
|
-
resumable: request.resumable,
|
|
454
|
-
startedAt: request.startedAt,
|
|
455
|
-
endedAt: request.endedAt,
|
|
456
|
-
lastActivityAt: request.lastActivityAt,
|
|
457
|
-
currentAgentId: request.currentAgentId,
|
|
458
|
-
delegationChain: request.delegationChain,
|
|
459
|
-
runtimeSnapshot: request.runtimeSnapshot,
|
|
460
|
-
traceItems: request.traceItems,
|
|
461
|
-
runtimeTimeline: request.runtimeTimeline,
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
function buildTaskFromSessionAndRequest(session, request, approvals, output, failureMessage) {
|
|
465
|
-
if (!session || !request) {
|
|
466
|
-
return null;
|
|
467
|
-
}
|
|
468
|
-
const latestUserMessage = [...session.messages].reverse().find((message) => message.role === "user");
|
|
469
|
-
const latestAgentMessage = [...session.messages].reverse().find((message) => message.role === "assistant");
|
|
470
|
-
const approval = approvals.find((item) => item.status === "pending");
|
|
471
|
-
const latestAgentText = contentToText(latestAgentMessage?.content);
|
|
472
|
-
const latestUserText = contentToText(latestUserMessage?.content);
|
|
473
|
-
const statusText = request.state === "waiting_for_approval"
|
|
474
|
-
? approval
|
|
475
|
-
? `Approval required for tool ${approval.toolName}.`
|
|
476
|
-
: "Approval required."
|
|
477
|
-
: output ?? latestAgentText ?? failureMessage;
|
|
478
|
-
return {
|
|
479
|
-
id: request.requestId,
|
|
480
|
-
contextId: session.sessionId,
|
|
481
|
-
kind: "task",
|
|
482
|
-
status: {
|
|
483
|
-
state: mapRequestState(request.state),
|
|
484
|
-
timestamp: request.updatedAt,
|
|
485
|
-
...(statusText ? {
|
|
486
|
-
message: {
|
|
487
|
-
role: "ROLE_AGENT",
|
|
488
|
-
messageId: `${request.requestId}:status`,
|
|
489
|
-
parts: toTextParts(statusText),
|
|
490
|
-
},
|
|
491
|
-
} : {}),
|
|
492
|
-
},
|
|
493
|
-
history: [
|
|
494
|
-
...(latestUserText
|
|
495
|
-
? [{
|
|
496
|
-
role: "ROLE_USER",
|
|
497
|
-
messageId: `${request.requestId}:user`,
|
|
498
|
-
parts: toTextParts(latestUserText),
|
|
499
|
-
}]
|
|
500
|
-
: []),
|
|
501
|
-
...(latestAgentText
|
|
502
|
-
? [{
|
|
503
|
-
role: "ROLE_AGENT",
|
|
504
|
-
messageId: `${request.requestId}:agent`,
|
|
505
|
-
parts: toTextParts(latestAgentText),
|
|
506
|
-
}]
|
|
507
|
-
: []),
|
|
508
|
-
],
|
|
509
|
-
artifacts: [],
|
|
510
|
-
metadata: {
|
|
511
|
-
agentId: request.agentId,
|
|
512
|
-
sessionId: session.sessionId,
|
|
513
|
-
requestId: request.requestId,
|
|
514
|
-
state: request.state,
|
|
515
|
-
approvalIds: approvals.map((item) => item.approvalId),
|
|
516
|
-
},
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
async function buildTaskFromRuntime(runtime, requestId) {
|
|
520
|
-
const request = await runtime.getRequest(requestId);
|
|
521
|
-
if (!request) {
|
|
522
|
-
return null;
|
|
523
|
-
}
|
|
524
|
-
const session = await runtime.getSessionRecord(request.sessionId);
|
|
525
|
-
const approvals = await runtime.listApprovals({ sessionId: request.sessionId, requestId: request.requestId });
|
|
526
|
-
return buildTaskFromSessionAndRequest(toSessionRecord(session), toRequestRecord(request), approvals);
|
|
527
|
-
}
|
|
528
|
-
async function hydrateTaskStateFromEvents(runtime, task) {
|
|
529
|
-
if (!task) {
|
|
530
|
-
return null;
|
|
531
|
-
}
|
|
532
|
-
const events = await runtime.listRequestEvents(task.contextId, task.id);
|
|
533
|
-
const lastStateChange = [...events].reverse().find((event) => event.eventType === "request.state.changed");
|
|
534
|
-
const observedState = typeof lastStateChange?.payload?.state === "string" ? lastStateChange.payload.state : undefined;
|
|
535
|
-
if (!observedState) {
|
|
536
|
-
return task;
|
|
537
|
-
}
|
|
538
|
-
return {
|
|
539
|
-
...task,
|
|
540
|
-
status: {
|
|
541
|
-
...task.status,
|
|
542
|
-
state: mapRequestState(observedState),
|
|
543
|
-
},
|
|
544
|
-
metadata: {
|
|
545
|
-
...task.metadata,
|
|
546
|
-
state: observedState,
|
|
547
|
-
},
|
|
548
|
-
};
|
|
549
|
-
}
|
|
550
|
-
async function listTasksFromRuntime(runtime, params) {
|
|
551
|
-
const runState = mapTaskStatusToRequestState(params.state);
|
|
552
|
-
const requests = await runtime.listRequests({
|
|
553
|
-
...(params.agentId ? { agentId: params.agentId } : {}),
|
|
554
|
-
...(params.contextId ? { sessionId: params.contextId } : {}),
|
|
555
|
-
...(runState ? { state: runState } : {}),
|
|
556
|
-
});
|
|
557
|
-
const startIndex = params.cursor ? Number.parseInt(Buffer.from(params.cursor, "base64url").toString("utf8"), 10) || 0 : 0;
|
|
558
|
-
const page = requests.slice(startIndex, startIndex + params.limit);
|
|
559
|
-
const tasks = (await Promise.all(page.map((request) => buildTaskFromRuntime(runtime, request.requestId)))).filter((task) => Boolean(task));
|
|
560
|
-
const nextIndex = startIndex + page.length;
|
|
561
|
-
const nextPageToken = nextIndex < requests.length ? Buffer.from(String(nextIndex), "utf8").toString("base64url") : "";
|
|
562
|
-
return {
|
|
563
|
-
tasks,
|
|
564
|
-
pageSize: params.limit,
|
|
565
|
-
totalSize: requests.length,
|
|
566
|
-
nextPageToken,
|
|
567
|
-
...(nextPageToken ? { nextCursor: nextPageToken } : {}),
|
|
568
|
-
};
|
|
569
|
-
}
|
|
570
|
-
function buildAgentCard(runtime, options) {
|
|
571
|
-
const inventory = runtime.describeWorkspaceInventory();
|
|
572
|
-
const skills = inventory.agents.map((agent) => ({
|
|
573
|
-
id: agent.id,
|
|
574
|
-
name: agent.id,
|
|
575
|
-
description: agent.description || `Agent ${agent.id}`,
|
|
576
|
-
tags: ["agent-harness", agent.parentAgentId ? "subagent" : "agent"],
|
|
577
|
-
examples: [`Send a task to ${agent.id}.`],
|
|
578
|
-
}));
|
|
579
|
-
return {
|
|
580
|
-
name: options.agentName,
|
|
581
|
-
description: options.agentDescription,
|
|
582
|
-
version: AGENT_HARNESS_VERSION,
|
|
583
|
-
// Older JSON-RPC clients may still read these top-level fields; A2A v1.0 discovery uses `supportedInterfaces`.
|
|
584
|
-
protocolVersion: "1.0",
|
|
585
|
-
url: options.rpcUrl,
|
|
586
|
-
preferredTransport: "JSONRPC",
|
|
587
|
-
supportedInterfaces: [
|
|
588
|
-
{
|
|
589
|
-
url: options.rpcUrl,
|
|
590
|
-
protocolBinding: "JSONRPC",
|
|
591
|
-
protocolVersion: "1.0",
|
|
592
|
-
},
|
|
593
|
-
{
|
|
594
|
-
url: options.rpcUrl,
|
|
595
|
-
protocolBinding: "JSONRPC",
|
|
596
|
-
protocolVersion: "0.3",
|
|
597
|
-
},
|
|
598
|
-
],
|
|
599
|
-
capabilities: {
|
|
600
|
-
streaming: true,
|
|
601
|
-
pushNotifications: true,
|
|
602
|
-
extendedAgentCard: true,
|
|
603
|
-
},
|
|
604
|
-
defaultInputModes: ["text/plain"],
|
|
605
|
-
defaultOutputModes: ["text/plain"],
|
|
606
|
-
skills,
|
|
607
|
-
...(buildDiscoveryMetadata({
|
|
608
|
-
registryUrls: options.registryUrls,
|
|
609
|
-
agentCardSignature: options.agentCardSignature,
|
|
610
|
-
})
|
|
611
|
-
? {
|
|
612
|
-
metadata: {
|
|
613
|
-
agentHarness: {
|
|
614
|
-
discovery: buildDiscoveryMetadata({
|
|
615
|
-
registryUrls: options.registryUrls,
|
|
616
|
-
agentCardSignature: options.agentCardSignature,
|
|
617
|
-
}),
|
|
618
|
-
},
|
|
619
|
-
},
|
|
620
|
-
}
|
|
621
|
-
: {}),
|
|
622
|
-
...(options.documentationUrl ? { documentationUrl: options.documentationUrl } : {}),
|
|
623
|
-
...(options.provider ? { provider: options.provider } : {}),
|
|
624
|
-
...(options.defaultAgentId ? { defaultAgentId: options.defaultAgentId } : {}),
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
function buildExtendedAgentCard(runtime, options) {
|
|
628
|
-
const inventory = runtime.describeWorkspaceInventory();
|
|
629
|
-
const card = buildAgentCard(runtime, options);
|
|
630
|
-
return {
|
|
631
|
-
...card,
|
|
632
|
-
metadata: {
|
|
633
|
-
agentHarness: {
|
|
634
|
-
runtime: {
|
|
635
|
-
product: "agent-harness",
|
|
636
|
-
protocolSurface: "a2a-http",
|
|
637
|
-
...(options.defaultAgentId ? { defaultAgentId: options.defaultAgentId } : {}),
|
|
638
|
-
},
|
|
639
|
-
protocolSurfaces: ["A2A", "ACP", "AG-UI", "runtime MCP"],
|
|
640
|
-
inventory: {
|
|
641
|
-
agentCount: inventory.agents.length,
|
|
642
|
-
subagentCount: inventory.agents.filter((agent) => Boolean(agent.parentAgentId)).length,
|
|
643
|
-
},
|
|
644
|
-
...(buildDiscoveryMetadata({
|
|
645
|
-
registryUrls: options.registryUrls,
|
|
646
|
-
agentCardSignature: options.agentCardSignature,
|
|
647
|
-
})
|
|
648
|
-
? {
|
|
649
|
-
discovery: buildDiscoveryMetadata({
|
|
650
|
-
registryUrls: options.registryUrls,
|
|
651
|
-
agentCardSignature: options.agentCardSignature,
|
|
652
|
-
}),
|
|
653
|
-
}
|
|
654
|
-
: {}),
|
|
655
|
-
agents: inventory.agents.map((agent) => ({
|
|
656
|
-
id: agent.id,
|
|
657
|
-
name: agent.id,
|
|
658
|
-
description: agent.description || `Agent ${agent.id}`,
|
|
659
|
-
kind: agent.parentAgentId ? "subagent" : "agent",
|
|
660
|
-
})),
|
|
661
|
-
},
|
|
662
|
-
},
|
|
663
|
-
};
|
|
664
|
-
}
|
|
665
|
-
function isV1Method(method) {
|
|
666
|
-
return /^[A-Z]/.test(method);
|
|
667
|
-
}
|
|
668
|
-
function toSendMessageResult(method, task) {
|
|
669
|
-
if (!task || !isV1Method(method)) {
|
|
670
|
-
return task;
|
|
671
|
-
}
|
|
672
|
-
return { task };
|
|
673
|
-
}
|
|
674
|
-
function toStatusUpdate(task) {
|
|
675
|
-
return {
|
|
676
|
-
taskId: task.id,
|
|
677
|
-
contextId: task.contextId,
|
|
678
|
-
kind: "status-update",
|
|
679
|
-
status: task.status,
|
|
680
|
-
final: isTerminalTaskState(task.status.state),
|
|
681
|
-
metadata: task.metadata,
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
function toStreamingResult(method, task, mode) {
|
|
685
|
-
if (!task) {
|
|
686
|
-
return null;
|
|
687
|
-
}
|
|
688
|
-
if (!isV1Method(method)) {
|
|
689
|
-
return task;
|
|
690
|
-
}
|
|
691
|
-
if (mode === "status-update") {
|
|
692
|
-
return {
|
|
693
|
-
statusUpdate: toStatusUpdate(task),
|
|
694
|
-
};
|
|
695
|
-
}
|
|
696
|
-
return { task };
|
|
697
|
-
}
|
|
698
|
-
function withStreamingStatusText(task, text) {
|
|
699
|
-
if (!task || typeof text !== "string" || text.length === 0) {
|
|
700
|
-
return task;
|
|
701
|
-
}
|
|
702
|
-
return {
|
|
703
|
-
...task,
|
|
704
|
-
status: {
|
|
705
|
-
...task.status,
|
|
706
|
-
message: {
|
|
707
|
-
role: "ROLE_AGENT",
|
|
708
|
-
messageId: `${task.id}:status`,
|
|
709
|
-
parts: toTextParts(text),
|
|
710
|
-
},
|
|
711
|
-
},
|
|
712
|
-
};
|
|
713
|
-
}
|
|
714
|
-
function withRunResult(task, result, streamedText) {
|
|
715
|
-
if (!task) {
|
|
716
|
-
return null;
|
|
717
|
-
}
|
|
718
|
-
const statusText = streamedText || result.output;
|
|
719
|
-
return {
|
|
720
|
-
...task,
|
|
721
|
-
status: {
|
|
722
|
-
...task.status,
|
|
723
|
-
state: mapRequestState(result.state),
|
|
724
|
-
...(statusText
|
|
725
|
-
? {
|
|
726
|
-
message: {
|
|
727
|
-
role: "ROLE_AGENT",
|
|
728
|
-
messageId: `${task.id}:status`,
|
|
729
|
-
parts: toTextParts(statusText),
|
|
730
|
-
},
|
|
731
|
-
}
|
|
732
|
-
: {}),
|
|
733
|
-
},
|
|
734
|
-
metadata: {
|
|
735
|
-
...task.metadata,
|
|
736
|
-
state: result.state,
|
|
737
|
-
},
|
|
738
|
-
};
|
|
739
|
-
}
|
|
740
|
-
function isTerminalTaskState(state) {
|
|
741
|
-
return state === "TASK_STATE_COMPLETED" || state === "TASK_STATE_FAILED" || state === "TASK_STATE_CANCELED";
|
|
742
|
-
}
|
|
743
|
-
function clonePushNotificationConfig(config) {
|
|
744
|
-
return {
|
|
745
|
-
id: config.id,
|
|
746
|
-
taskId: config.taskId,
|
|
747
|
-
url: config.url,
|
|
748
|
-
...(config.token ? { token: config.token } : {}),
|
|
749
|
-
...(config.authentication ? { authentication: config.authentication } : {}),
|
|
750
|
-
};
|
|
751
|
-
}
|
|
752
|
-
function fingerprintTaskForPush(task) {
|
|
753
|
-
return JSON.stringify({
|
|
754
|
-
state: task.status.state,
|
|
755
|
-
timestamp: task.status.timestamp,
|
|
756
|
-
message: task.status.message,
|
|
757
|
-
});
|
|
758
|
-
}
|
|
759
|
-
async function sendPushNotification(config, task) {
|
|
760
|
-
const headers = {
|
|
761
|
-
"content-type": "application/json",
|
|
762
|
-
};
|
|
763
|
-
if (config.token) {
|
|
764
|
-
headers["x-a2a-notification-token"] = config.token;
|
|
765
|
-
}
|
|
766
|
-
const scheme = config.authentication?.schemes[0]?.toLowerCase();
|
|
767
|
-
if (scheme === "bearer" && config.authentication?.credentials) {
|
|
768
|
-
headers.authorization = `Bearer ${config.authentication.credentials}`;
|
|
769
|
-
}
|
|
770
|
-
else if (scheme === "basic" && config.authentication?.credentials) {
|
|
771
|
-
headers.authorization = `Basic ${config.authentication.credentials}`;
|
|
772
|
-
}
|
|
773
|
-
await fetch(config.url, {
|
|
774
|
-
method: "POST",
|
|
775
|
-
headers,
|
|
776
|
-
body: JSON.stringify({ task }),
|
|
777
|
-
});
|
|
778
|
-
}
|
|
779
|
-
async function streamTaskUpdates(response, id, method, iterator) {
|
|
780
|
-
writeSseHeaders(response);
|
|
781
|
-
for await (const item of iterator) {
|
|
782
|
-
if (!item.task) {
|
|
783
|
-
continue;
|
|
784
|
-
}
|
|
785
|
-
await writeSseEvent(response, "task-update", toSuccess(id, toStreamingResult(method, item.task, item.mode)));
|
|
786
|
-
}
|
|
787
|
-
response.end();
|
|
788
|
-
}
|
|
789
|
-
async function* streamSendMessageTaskUpdates(runtime, options, method) {
|
|
790
|
-
let requestId;
|
|
791
|
-
let streamedText = "";
|
|
792
|
-
let emittedInitialTask = false;
|
|
793
|
-
for await (const item of runtime.streamEvents(options)) {
|
|
794
|
-
if (item.type === "event") {
|
|
795
|
-
requestId = requestId ?? item.event.requestId;
|
|
796
|
-
if (item.event.eventType === "output.delta" && typeof item.event.payload.content === "string") {
|
|
797
|
-
streamedText += item.event.payload.content;
|
|
798
|
-
}
|
|
799
|
-
if (item.event.eventType === "request.created"
|
|
800
|
-
|| item.event.eventType === "request.state.changed"
|
|
801
|
-
|| item.event.eventType === "output.delta"
|
|
802
|
-
|| item.event.eventType === "approval.requested") {
|
|
803
|
-
const task = withStreamingStatusText(await buildTaskFromRuntime(runtime, item.event.requestId), streamedText || undefined);
|
|
804
|
-
if (task) {
|
|
805
|
-
yield { task, mode: emittedInitialTask ? "status-update" : "task" };
|
|
806
|
-
emittedInitialTask = true;
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
continue;
|
|
810
|
-
}
|
|
811
|
-
if (item.type === "result") {
|
|
812
|
-
requestId = item.result.requestId;
|
|
813
|
-
const task = withRunResult(await buildTaskFromRuntime(runtime, item.result.requestId), item.result, streamedText || item.result.output);
|
|
814
|
-
yield { task, mode: emittedInitialTask ? "status-update" : "task" };
|
|
815
|
-
return;
|
|
816
|
-
}
|
|
817
|
-
if (item.type === "upstream-event" && requestId) {
|
|
818
|
-
const task = withStreamingStatusText(await buildTaskFromRuntime(runtime, requestId), streamedText || undefined);
|
|
819
|
-
if (task) {
|
|
820
|
-
yield { task, mode: emittedInitialTask ? "status-update" : "task" };
|
|
821
|
-
emittedInitialTask = true;
|
|
822
|
-
}
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
async function streamExistingTaskUpdates(runtime, response, id, method, taskId) {
|
|
827
|
-
writeSseHeaders(response);
|
|
828
|
-
let currentTask = await hydrateTaskStateFromEvents(runtime, await buildTaskFromRuntime(runtime, taskId));
|
|
829
|
-
if (!currentTask) {
|
|
830
|
-
await writeSseEvent(response, "error", toError(id, -32001, "Task not found."));
|
|
831
|
-
response.end();
|
|
832
|
-
return;
|
|
833
|
-
}
|
|
834
|
-
await writeSseEvent(response, "task-update", toSuccess(id, toStreamingResult(method, currentTask, "task")));
|
|
835
|
-
if (isTerminalTaskState(currentTask.status.state)) {
|
|
836
|
-
response.end();
|
|
837
|
-
return;
|
|
838
|
-
}
|
|
839
|
-
await new Promise((resolve) => {
|
|
840
|
-
let closed = false;
|
|
841
|
-
let idleTimer;
|
|
842
|
-
let lastFingerprint = JSON.stringify({
|
|
843
|
-
state: currentTask?.status.state,
|
|
844
|
-
message: currentTask?.status.message,
|
|
845
|
-
});
|
|
846
|
-
const armIdleTimer = () => {
|
|
847
|
-
if (idleTimer) {
|
|
848
|
-
clearTimeout(idleTimer);
|
|
849
|
-
}
|
|
850
|
-
idleTimer = setTimeout(() => {
|
|
851
|
-
response.end();
|
|
852
|
-
finish();
|
|
853
|
-
}, 1_000);
|
|
854
|
-
};
|
|
855
|
-
const finish = () => {
|
|
856
|
-
if (closed) {
|
|
857
|
-
return;
|
|
858
|
-
}
|
|
859
|
-
closed = true;
|
|
860
|
-
unsubscribe();
|
|
861
|
-
clearInterval(pollTimer);
|
|
862
|
-
if (idleTimer) {
|
|
863
|
-
clearTimeout(idleTimer);
|
|
864
|
-
}
|
|
865
|
-
resolve();
|
|
866
|
-
};
|
|
867
|
-
const emitLatestTask = async () => {
|
|
868
|
-
currentTask = await hydrateTaskStateFromEvents(runtime, await buildTaskFromRuntime(runtime, taskId));
|
|
869
|
-
if (!currentTask) {
|
|
870
|
-
await writeSseEvent(response, "error", toError(id, -32001, "Task not found."));
|
|
871
|
-
response.end();
|
|
872
|
-
finish();
|
|
873
|
-
return;
|
|
874
|
-
}
|
|
875
|
-
const fingerprint = JSON.stringify({
|
|
876
|
-
state: currentTask.status.state,
|
|
877
|
-
message: currentTask.status.message,
|
|
878
|
-
});
|
|
879
|
-
if (fingerprint !== lastFingerprint) {
|
|
880
|
-
lastFingerprint = fingerprint;
|
|
881
|
-
await writeSseEvent(response, "task-update", toSuccess(id, toStreamingResult(method, currentTask, isV1Method(method) ? "status-update" : "task")));
|
|
882
|
-
armIdleTimer();
|
|
883
|
-
}
|
|
884
|
-
if (isTerminalTaskState(currentTask.status.state)) {
|
|
885
|
-
response.end();
|
|
886
|
-
finish();
|
|
887
|
-
}
|
|
888
|
-
};
|
|
889
|
-
const unsubscribe = runtime.subscribe((event) => {
|
|
890
|
-
if (event.requestId !== taskId) {
|
|
891
|
-
return;
|
|
892
|
-
}
|
|
893
|
-
if (event.eventType !== "request.state.changed"
|
|
894
|
-
&& event.eventType !== "output.delta"
|
|
895
|
-
&& event.eventType !== "approval.requested"
|
|
896
|
-
&& event.eventType !== "approval.resolved") {
|
|
897
|
-
return;
|
|
898
|
-
}
|
|
899
|
-
void emitLatestTask();
|
|
900
|
-
});
|
|
901
|
-
const pollTimer = setInterval(() => {
|
|
902
|
-
void emitLatestTask();
|
|
903
|
-
}, 250);
|
|
904
|
-
armIdleTimer();
|
|
905
|
-
response.on("close", finish);
|
|
906
|
-
});
|
|
907
|
-
}
|
|
3
|
+
import { buildAgentCard, normalizePath, parseA2aServiceParameters, SUPPORTED_A2A_VERSIONS, writeA2aDiscoveryHeaders, writeJson, writeOptionalAgentCardIntegrityHeaders, } from "./http-discovery.js";
|
|
4
|
+
import { createPushNotificationRegistry, handleA2aJsonRpcRequest, readRequestBody, toError } from "./http-rpc.js";
|
|
908
5
|
export async function serveA2aOverHttp(runtime, options = {}) {
|
|
909
6
|
const hostname = options.hostname?.trim() || "127.0.0.1";
|
|
910
7
|
const port = typeof options.port === "number" && Number.isFinite(options.port) ? options.port : 0;
|
|
@@ -920,67 +17,7 @@ export async function serveA2aOverHttp(runtime, options = {}) {
|
|
|
920
17
|
...(options.registryUrls ? { registryUrls: options.registryUrls } : {}),
|
|
921
18
|
...(options.agentCardSignature ? { agentCardSignature: options.agentCardSignature } : {}),
|
|
922
19
|
});
|
|
923
|
-
const
|
|
924
|
-
const registerPushNotificationConfig = (taskId, config) => {
|
|
925
|
-
const entry = {
|
|
926
|
-
id: config.id ?? `push-${createPersistentId()}`,
|
|
927
|
-
taskId,
|
|
928
|
-
url: config.url,
|
|
929
|
-
...(config.token ? { token: config.token } : {}),
|
|
930
|
-
...(config.authentication ? { authentication: config.authentication } : {}),
|
|
931
|
-
};
|
|
932
|
-
let taskConfigs = pushConfigsByTask.get(taskId);
|
|
933
|
-
if (!taskConfigs) {
|
|
934
|
-
taskConfigs = new Map();
|
|
935
|
-
pushConfigsByTask.set(taskId, taskConfigs);
|
|
936
|
-
}
|
|
937
|
-
taskConfigs.set(entry.id, entry);
|
|
938
|
-
return entry;
|
|
939
|
-
};
|
|
940
|
-
const listPushNotificationConfigs = (taskId) => [...(pushConfigsByTask.get(taskId)?.values() ?? [])].sort((left, right) => left.id.localeCompare(right.id));
|
|
941
|
-
const getPushNotificationConfig = (taskId, configId) => pushConfigsByTask.get(taskId)?.get(configId);
|
|
942
|
-
const deletePushNotificationConfig = (taskId, configId) => {
|
|
943
|
-
const taskConfigs = pushConfigsByTask.get(taskId);
|
|
944
|
-
if (!taskConfigs) {
|
|
945
|
-
return false;
|
|
946
|
-
}
|
|
947
|
-
const deleted = taskConfigs.delete(configId);
|
|
948
|
-
if (taskConfigs.size === 0) {
|
|
949
|
-
pushConfigsByTask.delete(taskId);
|
|
950
|
-
}
|
|
951
|
-
return deleted;
|
|
952
|
-
};
|
|
953
|
-
const dispatchPushNotificationsForTask = async (taskId) => {
|
|
954
|
-
const task = await hydrateTaskStateFromEvents(runtime, await buildTaskFromRuntime(runtime, taskId));
|
|
955
|
-
if (!task) {
|
|
956
|
-
return;
|
|
957
|
-
}
|
|
958
|
-
const fingerprint = fingerprintTaskForPush(task);
|
|
959
|
-
const configs = listPushNotificationConfigs(taskId);
|
|
960
|
-
await Promise.all(configs.map(async (config) => {
|
|
961
|
-
if (config.lastFingerprint === fingerprint) {
|
|
962
|
-
return;
|
|
963
|
-
}
|
|
964
|
-
try {
|
|
965
|
-
await sendPushNotification(config, task);
|
|
966
|
-
config.lastFingerprint = fingerprint;
|
|
967
|
-
}
|
|
968
|
-
catch {
|
|
969
|
-
// Push delivery is best-effort and should not fail the transport request.
|
|
970
|
-
}
|
|
971
|
-
}));
|
|
972
|
-
};
|
|
973
|
-
const pushRelevantEventTypes = new Set([
|
|
974
|
-
"request.state.changed",
|
|
975
|
-
"approval.requested",
|
|
976
|
-
"approval.resolved",
|
|
977
|
-
]);
|
|
978
|
-
const unsubscribePushNotifications = runtime.subscribe((event) => {
|
|
979
|
-
if (!pushRelevantEventTypes.has(event.eventType) || !pushConfigsByTask.has(event.requestId)) {
|
|
980
|
-
return;
|
|
981
|
-
}
|
|
982
|
-
void dispatchPushNotificationsForTask(event.requestId);
|
|
983
|
-
});
|
|
20
|
+
const pushNotifications = createPushNotificationRegistry(runtime, createPersistentId);
|
|
984
21
|
const httpServer = createServer(async (request, response) => {
|
|
985
22
|
try {
|
|
986
23
|
const requestUrl = new URL(request.url ?? "/", `http://${hostname}`);
|
|
@@ -1039,163 +76,16 @@ export async function serveA2aOverHttp(runtime, options = {}) {
|
|
|
1039
76
|
return;
|
|
1040
77
|
}
|
|
1041
78
|
try {
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
const approvals = await runtime.listApprovals({ sessionId: result.sessionId, requestId: result.requestId });
|
|
1053
|
-
const task = buildTaskFromSessionAndRequest(toSessionRecord(session), toRequestRecord(requestRecord), approvals, result.output);
|
|
1054
|
-
if (task && parsed.pushNotificationConfig) {
|
|
1055
|
-
registerPushNotificationConfig(task.id, parsed.pushNotificationConfig);
|
|
1056
|
-
await dispatchPushNotificationsForTask(task.id);
|
|
1057
|
-
}
|
|
1058
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, toSendMessageResult(payload.method, task)));
|
|
1059
|
-
return;
|
|
1060
|
-
}
|
|
1061
|
-
if (payload.method === "GetAgentCard") {
|
|
1062
|
-
writeOptionalAgentCardIntegrityHeaders(response, {
|
|
1063
|
-
registryUrls: options.registryUrls,
|
|
1064
|
-
agentCardSignature: options.agentCardSignature,
|
|
1065
|
-
});
|
|
1066
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, buildAgentCard(runtime, buildCardOptions())));
|
|
1067
|
-
return;
|
|
1068
|
-
}
|
|
1069
|
-
if (payload.method === "message/stream" || payload.method === "SendStreamingMessage") {
|
|
1070
|
-
if (!acceptsSse(request)) {
|
|
1071
|
-
writeJson(response, 406, toError(payload.id ?? null, -32004, "A2A streaming requires `Accept: text/event-stream`."));
|
|
1072
|
-
return;
|
|
1073
|
-
}
|
|
1074
|
-
const parsed = parseMessageSendParams(payload.params);
|
|
1075
|
-
let registeredPushTaskId;
|
|
1076
|
-
const sourceIterator = streamSendMessageTaskUpdates(runtime, {
|
|
1077
|
-
agentId: parsed.agentId ?? options.defaultAgentId,
|
|
1078
|
-
input: parsed.input,
|
|
1079
|
-
...(parsed.sessionId ? { sessionId: parsed.sessionId } : {}),
|
|
1080
|
-
invocation: attachServiceParameters(parsed.invocation, serviceParameters),
|
|
1081
|
-
}, payload.method);
|
|
1082
|
-
const iterator = (async function* () {
|
|
1083
|
-
for await (const item of sourceIterator) {
|
|
1084
|
-
if (item.task && parsed.pushNotificationConfig && !registeredPushTaskId) {
|
|
1085
|
-
registeredPushTaskId = item.task.id;
|
|
1086
|
-
registerPushNotificationConfig(item.task.id, parsed.pushNotificationConfig);
|
|
1087
|
-
await dispatchPushNotificationsForTask(item.task.id);
|
|
1088
|
-
}
|
|
1089
|
-
yield item;
|
|
1090
|
-
}
|
|
1091
|
-
}());
|
|
1092
|
-
await streamTaskUpdates(response, payload.id ?? null, payload.method, iterator);
|
|
1093
|
-
return;
|
|
1094
|
-
}
|
|
1095
|
-
if (payload.method === "tasks/get" || payload.method === "GetTask") {
|
|
1096
|
-
const { taskId } = parseTaskLocatorParams(payload.params);
|
|
1097
|
-
const task = await buildTaskFromRuntime(runtime, taskId);
|
|
1098
|
-
if (!task) {
|
|
1099
|
-
writeJson(response, 200, toError(payload.id ?? null, -32001, "Task not found."));
|
|
1100
|
-
return;
|
|
1101
|
-
}
|
|
1102
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, task));
|
|
1103
|
-
return;
|
|
1104
|
-
}
|
|
1105
|
-
if (payload.method === "tasks/list" || payload.method === "ListTasks") {
|
|
1106
|
-
const taskList = await listTasksFromRuntime(runtime, parseTaskListParams(payload.params));
|
|
1107
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, taskList));
|
|
1108
|
-
return;
|
|
1109
|
-
}
|
|
1110
|
-
if (payload.method === "tasks/cancel" || payload.method === "CancelTask") {
|
|
1111
|
-
const { taskId } = parseTaskLocatorParams(payload.params);
|
|
1112
|
-
const result = await runtime.cancelRequest({ requestId: taskId, reason: "Cancelled via A2A bridge." });
|
|
1113
|
-
const task = await buildTaskFromRuntime(runtime, result.requestId);
|
|
1114
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, task));
|
|
1115
|
-
return;
|
|
1116
|
-
}
|
|
1117
|
-
if (payload.method === "tasks/subscribe" || payload.method === "SubscribeToTask") {
|
|
1118
|
-
const { taskId } = parseTaskLocatorParams(payload.params);
|
|
1119
|
-
const task = await buildTaskFromRuntime(runtime, taskId);
|
|
1120
|
-
if (!task) {
|
|
1121
|
-
writeJson(response, 200, toError(payload.id ?? null, -32001, "Task not found."));
|
|
1122
|
-
return;
|
|
1123
|
-
}
|
|
1124
|
-
if (payload.method === "SubscribeToTask") {
|
|
1125
|
-
if (isTerminalTaskState(task.status.state)) {
|
|
1126
|
-
writeJson(response, 200, toError(payload.id ?? null, -32004, "SubscribeToTask is not supported for tasks in terminal state."));
|
|
1127
|
-
return;
|
|
1128
|
-
}
|
|
1129
|
-
if (!acceptsSse(request)) {
|
|
1130
|
-
writeJson(response, 406, toError(payload.id ?? null, -32004, "A2A streaming subscriptions require `Accept: text/event-stream`."));
|
|
1131
|
-
return;
|
|
1132
|
-
}
|
|
1133
|
-
await streamExistingTaskUpdates(runtime, response, payload.id ?? null, payload.method, taskId);
|
|
1134
|
-
return;
|
|
1135
|
-
}
|
|
1136
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, {
|
|
1137
|
-
task,
|
|
1138
|
-
streamable: false,
|
|
1139
|
-
pushNotifications: true,
|
|
1140
|
-
nextPollAfterMs: task.status.state === "TASK_STATE_COMPLETED" || task.status.state === "TASK_STATE_FAILED" || task.status.state === "TASK_STATE_CANCELED"
|
|
1141
|
-
? 0
|
|
1142
|
-
: 1_000,
|
|
1143
|
-
}));
|
|
1144
|
-
return;
|
|
1145
|
-
}
|
|
1146
|
-
if (payload.method === "CreateTaskPushNotificationConfig") {
|
|
1147
|
-
const { taskId, config } = parseCreatePushNotificationConfigParams(payload.params);
|
|
1148
|
-
if (!await buildTaskFromRuntime(runtime, taskId)) {
|
|
1149
|
-
writeJson(response, 200, toError(payload.id ?? null, -32001, "Task not found."));
|
|
1150
|
-
return;
|
|
1151
|
-
}
|
|
1152
|
-
const created = registerPushNotificationConfig(taskId, config);
|
|
1153
|
-
await dispatchPushNotificationsForTask(taskId);
|
|
1154
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, clonePushNotificationConfig(created)));
|
|
1155
|
-
return;
|
|
1156
|
-
}
|
|
1157
|
-
if (payload.method === "GetTaskPushNotificationConfig") {
|
|
1158
|
-
const { taskId, configId } = parsePushNotificationLocatorParams(payload.params);
|
|
1159
|
-
const config = getPushNotificationConfig(taskId, configId);
|
|
1160
|
-
if (!config) {
|
|
1161
|
-
writeJson(response, 200, toError(payload.id ?? null, -32001, "Push notification config not found."));
|
|
1162
|
-
return;
|
|
1163
|
-
}
|
|
1164
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, clonePushNotificationConfig(config)));
|
|
1165
|
-
return;
|
|
1166
|
-
}
|
|
1167
|
-
if (payload.method === "ListTaskPushNotificationConfigs") {
|
|
1168
|
-
const { taskId, pageSize, pageToken } = parsePushNotificationListParams(payload.params);
|
|
1169
|
-
const configs = listPushNotificationConfigs(taskId);
|
|
1170
|
-
const startIndex = pageToken ? Number.parseInt(Buffer.from(pageToken, "base64url").toString("utf8"), 10) || 0 : 0;
|
|
1171
|
-
const page = configs.slice(startIndex, startIndex + pageSize).map(clonePushNotificationConfig);
|
|
1172
|
-
const nextIndex = startIndex + page.length;
|
|
1173
|
-
const nextPageToken = nextIndex < configs.length ? Buffer.from(String(nextIndex), "utf8").toString("base64url") : "";
|
|
1174
|
-
const result = {
|
|
1175
|
-
configs: page,
|
|
1176
|
-
nextPageToken,
|
|
1177
|
-
};
|
|
1178
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, result));
|
|
1179
|
-
return;
|
|
1180
|
-
}
|
|
1181
|
-
if (payload.method === "DeleteTaskPushNotificationConfig") {
|
|
1182
|
-
const { taskId, configId } = parsePushNotificationLocatorParams(payload.params);
|
|
1183
|
-
if (!deletePushNotificationConfig(taskId, configId)) {
|
|
1184
|
-
writeJson(response, 200, toError(payload.id ?? null, -32001, "Push notification config not found."));
|
|
1185
|
-
return;
|
|
1186
|
-
}
|
|
1187
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, { deleted: true, id: configId, taskId }));
|
|
1188
|
-
return;
|
|
1189
|
-
}
|
|
1190
|
-
if (payload.method === "GetExtendedAgentCard") {
|
|
1191
|
-
writeOptionalAgentCardIntegrityHeaders(response, {
|
|
1192
|
-
registryUrls: options.registryUrls,
|
|
1193
|
-
agentCardSignature: options.agentCardSignature,
|
|
1194
|
-
});
|
|
1195
|
-
writeJson(response, 200, toSuccess(payload.id ?? null, buildExtendedAgentCard(runtime, buildCardOptions())));
|
|
1196
|
-
return;
|
|
1197
|
-
}
|
|
1198
|
-
writeJson(response, 200, toError(payload.id ?? null, -32601, `Unknown A2A method: ${payload.method}`));
|
|
79
|
+
await handleA2aJsonRpcRequest({
|
|
80
|
+
runtime,
|
|
81
|
+
request,
|
|
82
|
+
response,
|
|
83
|
+
payload,
|
|
84
|
+
serviceParameters,
|
|
85
|
+
serverOptions: options,
|
|
86
|
+
buildCardOptions,
|
|
87
|
+
pushNotifications,
|
|
88
|
+
});
|
|
1199
89
|
}
|
|
1200
90
|
catch (error) {
|
|
1201
91
|
writeJson(response, 200, toError(payload.id ?? null, -32000, error instanceof Error ? error.message : String(error)));
|
|
@@ -1233,7 +123,7 @@ export async function serveA2aOverHttp(runtime, options = {}) {
|
|
|
1233
123
|
agentCardUrl: `http://${hostname}:${resolvedPort}${agentCardPath}`,
|
|
1234
124
|
completed,
|
|
1235
125
|
close: async () => {
|
|
1236
|
-
|
|
126
|
+
pushNotifications.unsubscribe();
|
|
1237
127
|
await new Promise((resolve, reject) => {
|
|
1238
128
|
httpServer.close((error) => {
|
|
1239
129
|
if (error) {
|