-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy patha2aAgents.js
More file actions
817 lines (725 loc) · 27.1 KB
/
a2aAgents.js
File metadata and controls
817 lines (725 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
import { getAuthHeaders, loadAuthHeaders } from "./auth.js";
import { closeModal, openModal } from "./modals.js";
import { escapeHtml, validateInputName, validateUrl } from "./security.js";
import { applyVisibilityRestrictions } from "./teams.js";
import {
decodeHtml,
fetchWithTimeout,
handleFetchError,
isInactiveChecked,
makeCopyIdButton,
safeGetElement,
safeSetValue,
showErrorMessage,
} from "./utils.js";
// ===================================================================
// A2A AGENT TEST MODAL FUNCTIONALITY
// ===================================================================
let a2aTestFormHandler = null;
let a2aTestCloseHandler = null;
/**
* SECURE: View A2A Agents function with safe display
*/
export const viewA2AAgent = async function (agentId) {
try {
console.log(`Viewing agent ID: ${agentId}`);
const response = await fetchWithTimeout(
`${window.ROOT_PATH}/admin/a2a/${agentId}`
);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const agent = await response.json();
const agentDetailsDiv = safeGetElement("agent-details");
if (agentDetailsDiv) {
const container = document.createElement("div");
container.className = "space-y-2 dark:bg-gray-900 dark:text-gray-100";
// ID field with copy button
const agentIdP = document.createElement("p");
const agentIdStrong = document.createElement("strong");
agentIdStrong.textContent = "Agent ID: ";
agentIdP.appendChild(agentIdStrong);
const agentIdSpan = document.createElement("span");
agentIdSpan.className = "font-mono text-sm";
agentIdSpan.textContent = agent.id;
agentIdP.appendChild(agentIdSpan);
agentIdP.appendChild(makeCopyIdButton(agent.id));
container.appendChild(agentIdP);
const fields = [
{ label: "Name", value: agent.name },
{ label: "Slug", value: agent.slug },
{ label: "Endpoint URL", value: agent.endpointUrl },
{ label: "Agent Type", value: agent.agentType },
{ label: "Protocol Version", value: agent.protocolVersion },
{
label: "Description",
value: decodeHtml(agent.description) || "N/A",
},
{ label: "Visibility", value: agent.visibility || "private" },
];
// Tags
const tagsP = document.createElement("p");
const tagsStrong = document.createElement("strong");
tagsStrong.textContent = "Tags: ";
tagsP.appendChild(tagsStrong);
if (agent.tags && agent.tags.length > 0) {
agent.tags.forEach((tag) => {
const tagSpan = document.createElement("span");
tagSpan.className =
"inline-block bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded-full mr-1";
const raw =
typeof tag === "object" && tag !== null
? tag.id || tag.label || JSON.stringify(tag)
: tag;
tagSpan.textContent = raw;
tagsP.appendChild(tagSpan);
});
} else {
tagsP.appendChild(document.createTextNode("No tags"));
}
container.appendChild(tagsP);
// Render basic fields
fields.forEach((field) => {
const p = document.createElement("p");
const strong = document.createElement("strong");
strong.textContent = field.label + ": ";
p.appendChild(strong);
p.appendChild(document.createTextNode(field.value));
container.appendChild(p);
});
// Status
const statusP = document.createElement("p");
const statusStrong = document.createElement("strong");
statusStrong.textContent = "Status: ";
statusP.appendChild(statusStrong);
const statusSpan = document.createElement("span");
let statusText = "";
let statusClass = "";
let statusIcon = "";
if (!agent.enabled) {
statusText = "Inactive";
statusClass = "bg-red-100 text-red-800";
statusIcon = `
<svg class="ml-1 h-4 w-4 text-red-600 self-center" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M6.293 6.293a1 1 0 011.414 0L10 8.586l2.293-2.293a1 1 0 111.414 1.414L11.414 10l2.293 2.293a1 1 0 11-1.414 1.414L10 11.414l-2.293 2.293a1 1 0 11-1.414-1.414L8.586 10 6.293 7.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
</svg>`;
} else if (agent.enabled && agent.reachable) {
statusText = "Active";
statusClass = "bg-green-100 text-green-800";
statusIcon = `
<svg class="ml-1 h-4 w-4 text-green-600 self-center" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm-1-4.586l5.293-5.293-1.414-1.414L9 11.586 7.121 9.707 5.707 11.121 9 14.414z" clip-rule="evenodd"></path>
</svg>`;
} else if (agent.enabled && !agent.reachable) {
statusText = "Offline";
statusClass = "bg-yellow-100 text-yellow-800";
statusIcon = `
<svg class="ml-1 h-4 w-4 text-yellow-600 self-center" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm-1-10h2v4h-2V8zm0 6h2v2h-2v-2z" clip-rule="evenodd"></path>
</svg>`;
}
statusSpan.className = `px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${statusClass}`;
statusSpan.innerHTML = `${statusText} ${statusIcon}`;
statusP.appendChild(statusSpan);
container.appendChild(statusP);
// Capabilities + Config (JSON formatted)
const capConfigDiv = document.createElement("div");
capConfigDiv.className = "mt-4 p-2 bg-gray-50 dark:bg-gray-800 rounded";
const capTitle = document.createElement("strong");
capTitle.textContent = "Capabilities & Config:";
capConfigDiv.appendChild(capTitle);
const pre = document.createElement("pre");
pre.className = "text-xs mt-1 whitespace-pre-wrap break-words";
pre.textContent = JSON.stringify(
{ capabilities: agent.capabilities, config: agent.config },
null,
2
);
capConfigDiv.appendChild(pre);
container.appendChild(capConfigDiv);
// Metadata
const metadataDiv = document.createElement("div");
metadataDiv.className = "mt-6 border-t pt-4";
const metadataTitle = document.createElement("strong");
metadataTitle.textContent = "Metadata:";
metadataDiv.appendChild(metadataTitle);
const metadataGrid = document.createElement("div");
metadataGrid.className = "grid grid-cols-2 gap-4 mt-2 text-sm";
const metadataFields = [
{
label: "Created By",
value: agent.created_by || agent.createdBy || "Legacy Entity",
},
{
label: "Created At",
value:
agent.created_at || agent.createdAt
? new Date(agent.created_at || agent.createdAt).toLocaleString()
: "Pre-metadata",
},
{
label: "Created From IP",
value: agent.created_from_ip || agent.createdFromIp || "Unknown",
},
{
label: "Created Via",
value: agent.created_via || agent.createdVia || "Unknown",
},
{
label: "Last Modified By",
value: agent.modified_by || agent.modifiedBy || "N/A",
},
{
label: "Last Modified At",
value:
agent.updated_at || agent.updatedAt
? new Date(agent.updated_at || agent.updatedAt).toLocaleString()
: "N/A",
},
{
label: "Modified From IP",
value: agent.modified_from_ip || agent.modifiedFromIp || "N/A",
},
{
label: "Modified Via",
value: agent.modified_via || agent.modifiedVia || "N/A",
},
{ label: "Version", value: agent.version || "1" },
{
label: "Import Batch",
value: agent.importBatchId || "N/A",
},
];
metadataFields.forEach((field) => {
const fieldDiv = document.createElement("div");
const labelSpan = document.createElement("span");
labelSpan.className = "font-medium text-gray-600 dark:text-gray-400";
labelSpan.textContent = field.label + ":";
const valueSpan = document.createElement("span");
valueSpan.className = "ml-2";
valueSpan.textContent = field.value;
fieldDiv.appendChild(labelSpan);
fieldDiv.appendChild(valueSpan);
metadataGrid.appendChild(fieldDiv);
});
metadataDiv.appendChild(metadataGrid);
container.appendChild(metadataDiv);
agentDetailsDiv.innerHTML = "";
agentDetailsDiv.appendChild(container);
}
openModal("agent-modal");
const modal = safeGetElement("agent-modal");
if (modal && modal.classList.contains("hidden")) {
console.warn("Modal was still hidden — forcing visible.");
modal.classList.remove("hidden");
}
console.log("✓ Agent details loaded successfully");
} catch (error) {
console.error("Error fetching agent details:", error);
const errorMessage = handleFetchError(error, "load agent details");
showErrorMessage(errorMessage);
}
};
/**
* SECURE: Edit A2A Agent function
*/
export const editA2AAgent = async function (agentId) {
try {
console.log(`Editing A2A Agent ID: ${agentId}`);
const response = await fetchWithTimeout(
`${window.ROOT_PATH}/admin/a2a/${agentId}`
);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const agent = await response.json();
console.log("Agent Details: " + JSON.stringify(agent, null, 2));
// for (const [key, value] of Object.entries(agent)) {
// console.log(`${key}:`, value);
// }
const isInactiveCheckedBool = isInactiveChecked("a2a-agents");
const editForm = safeGetElement("edit-a2a-agent-form");
let hiddenField = safeGetElement("edit-a2a-agents-show-inactive");
if (!hiddenField) {
hiddenField = document.createElement("input");
hiddenField.type = "hidden";
hiddenField.name = "is_inactivate_checked";
hiddenField.id = "edit-a2a-agents-show-inactive";
if (editForm) {
editForm.appendChild(hiddenField);
}
}
hiddenField.value = isInactiveCheckedBool;
// Set form action and populate fields with validation
if (editForm) {
editForm.action = `${window.ROOT_PATH}/admin/a2a/${agentId}/edit`;
editForm.method = "POST"; // ensure method is POST
}
const nameValidation = validateInputName(agent.name, "a2a_agent");
const urlValidation = validateUrl(agent.endpointUrl);
const nameField = safeGetElement("a2a-agent-name-edit");
const urlField = safeGetElement("a2a-agent-endpoint-url-edit");
const descField = safeGetElement("a2a-agent-description-edit");
const agentType = safeGetElement("a2a-agent-type-edit");
agentType.value = agent.agentType;
console.log("Agent Type: ", agent.agentType);
if (nameField && nameValidation.valid) {
nameField.value = nameValidation.value;
}
if (urlField && urlValidation.valid) {
urlField.value = urlValidation.value;
}
if (descField) {
descField.value = agent.description || "";
}
// Set tags field
const tagsField = safeGetElement("a2a-agent-tags-edit");
if (tagsField) {
const rawTags = agent.tags
? agent.tags.map((tag) =>
typeof tag === "object" && tag !== null ? tag.label || tag.id : tag
)
: [];
tagsField.value = rawTags.join(", ");
}
const teamId = new URL(window.location.href).searchParams.get("team_id");
if (teamId) {
const hiddenInput = document.createElement("input");
hiddenInput.type = "hidden";
hiddenInput.name = "team_id";
hiddenInput.value = teamId;
editForm.appendChild(hiddenInput);
}
// ✅ Prefill visibility radios (consistent with server)
const visibility = agent.visibility ? agent.visibility.toLowerCase() : null;
const publicRadio = safeGetElement("edit-a2a-visibility-public");
const teamRadio = safeGetElement("edit-a2a-visibility-team");
const privateRadio = safeGetElement("edit-a2a-visibility-private");
// Clear all first
if (publicRadio) {
publicRadio.checked = false;
}
if (teamRadio) {
teamRadio.checked = false;
}
if (privateRadio) {
privateRadio.checked = false;
}
if (visibility) {
// When public visibility is disabled and we're in a team-scoped view,
// coerce legacy-public records to team.
const effectiveVisibility =
window.ALLOW_PUBLIC_VISIBILITY === false &&
visibility === "public" &&
teamId
? "team"
: visibility;
if (effectiveVisibility === "public" && publicRadio) {
publicRadio.checked = true;
} else if (effectiveVisibility === "team" && teamRadio) {
teamRadio.checked = true;
} else if (effectiveVisibility === "private" && privateRadio) {
privateRadio.checked = true;
}
}
const authTypeField = safeGetElement("auth-type-a2a-edit");
if (authTypeField) {
authTypeField.value = agent.authType || "";
}
toggleA2AAuthFields(agent.authType || "");
// Auth containers
const authBasicSection = safeGetElement("auth-basic-fields-a2a-edit");
const authBearerSection = safeGetElement("auth-bearer-fields-a2a-edit");
const authHeadersSection = safeGetElement("auth-headers-fields-a2a-edit");
const authOAuthSection = safeGetElement("auth-oauth-fields-a2a-edit");
const authQueryParamSection = safeGetElement(
"auth-query_param-fields-a2a-edit"
);
// Individual fields
const authUsernameField = safeGetElement(
"auth-basic-fields-a2a-edit"
)?.querySelector("input[name='auth_username']");
const authPasswordField = safeGetElement(
"auth-basic-fields-a2a-edit"
)?.querySelector("input[name='auth_password']");
const authTokenField = safeGetElement(
"auth-bearer-fields-a2a-edit"
)?.querySelector("input[name='auth_token']");
const authHeaderKeyField = safeGetElement(
"auth-headers-fields-a2a-edit"
)?.querySelector("input[name='auth_header_key']");
const authHeaderValueField = safeGetElement(
"auth-headers-fields-a2a-edit"
)?.querySelector("input[name='auth_header_value']");
// OAuth fields
const oauthGrantTypeField = safeGetElement("oauth-grant-type-a2a-edit");
const oauthClientIdField = safeGetElement("oauth-client-id-a2a-edit");
const oauthClientSecretField = safeGetElement(
"oauth-client-secret-a2a-edit"
);
const oauthTokenUrlField = safeGetElement("oauth-token-url-a2a-edit");
const oauthAuthUrlField = safeGetElement(
"oauth-authorization-url-a2a-edit"
);
const oauthRedirectUriField = safeGetElement("oauth-redirect-uri-a2a-edit");
const oauthIssuerField = safeGetElement("oauth-issuer-a2a-edit");
const oauthScopesField = safeGetElement("oauth-scopes-a2a-edit");
const oauthAuthCodeFields = safeGetElement(
"oauth-auth-code-fields-a2a-edit"
);
// Query param fields
const authQueryParamKeyField = safeGetElement(
"auth-query-param-key-a2a-edit"
);
const authQueryParamValueField = safeGetElement(
"auth-query-param-value-a2a-edit"
);
// Hide all auth sections first
if (authBasicSection) {
authBasicSection.style.display = "none";
}
if (authBearerSection) {
authBearerSection.style.display = "none";
}
if (authHeadersSection) {
authHeadersSection.style.display = "none";
}
if (authOAuthSection) {
authOAuthSection.style.display = "none";
}
if (authQueryParamSection) {
authQueryParamSection.style.display = "none";
}
switch (agent.authType) {
case "basic":
if (authBasicSection) {
authBasicSection.style.display = "block";
if (authUsernameField) {
authUsernameField.value = agent.authUsername || "";
}
if (authPasswordField) {
authPasswordField.value = "*****"; // mask password
}
}
break;
case "bearer":
if (authBearerSection) {
authBearerSection.style.display = "block";
if (authTokenField) {
authTokenField.value = agent.authValue || ""; // show full token
}
}
break;
case "authheaders":
if (authHeadersSection) {
authHeadersSection.style.display = "block";
if (authHeaderKeyField) {
authHeaderKeyField.value = agent.authHeaderKey || "";
}
if (authHeaderValueField) {
authHeaderValueField.value = "*****"; // mask header value
}
// Load existing auth_headers if present
if (agent.authHeaders && Array.isArray(agent.authHeaders)) {
loadAuthHeaders(
"auth-headers-container-a2a-edit",
agent.authHeaders,
{ maskValues: true }
);
}
}
break;
case "oauth":
if (authOAuthSection) {
authOAuthSection.style.display = "block";
}
// Populate OAuth fields if available
if (agent.oauthConfig) {
const config = agent.oauthConfig;
if (oauthIssuerField) {
oauthIssuerField.value = config.issuer || "";
}
if (oauthGrantTypeField) {
oauthGrantTypeField.value = config.grant_type || "";
// Show/hide authorization code fields based on grant type
if (oauthAuthCodeFields) {
oauthAuthCodeFields.style.display =
config.grant_type === "authorization_code" ? "block" : "none";
}
}
if (oauthClientIdField) {
oauthClientIdField.value = config.client_id || "";
}
if (oauthClientSecretField) {
oauthClientSecretField.value = config.client_secret ? MASKED_AUTH_VALUE : "";
}
if (oauthTokenUrlField) {
oauthTokenUrlField.value = config.token_url || "";
}
if (oauthAuthUrlField) {
oauthAuthUrlField.value = config.authorization_url || "";
}
if (oauthRedirectUriField) {
oauthRedirectUriField.value = config.redirect_uri || "";
}
if (oauthScopesField) {
oauthScopesField.value = Array.isArray(config.scopes)
? config.scopes.join(" ")
: "";
}
}
break;
case "query_param":
if (authQueryParamSection) {
authQueryParamSection.style.display = "block";
if (authQueryParamKeyField) {
authQueryParamKeyField.value = agent.authQueryParamKey || "";
}
if (authQueryParamValueField) {
authQueryParamValueField.value = "*****"; // mask value
}
}
break;
case "":
default:
// No auth – keep everything hidden
break;
}
// **Capabilities & Config (ensure valid dicts)**
safeSetValue(
"a2a-agent-capabilities-edit",
JSON.stringify(agent.capabilities || {})
);
safeSetValue("a2a-agent-config-edit", JSON.stringify(agent.config || {}));
// Set form action to the new POST endpoint
// Handle passthrough headers
const passthroughHeadersField = safeGetElement(
"edit-a2a-agent-passthrough-headers"
);
if (passthroughHeadersField) {
if (agent.passthroughHeaders && Array.isArray(agent.passthroughHeaders)) {
passthroughHeadersField.value = agent.passthroughHeaders.join(", ");
} else {
passthroughHeadersField.value = "";
}
}
openModal("a2a-edit-modal");
applyVisibilityRestrictions(["edit-a2a-visibility"]); // Disable public radio if restricted, preserve checked state
console.log("✓ A2A Agent edit modal loaded successfully");
} catch (err) {
console.error("Error loading A2A agent:", err);
const errorMessage = handleFetchError(err, "load A2A Agent for editing");
showErrorMessage(errorMessage);
}
};
export const toggleA2AAuthFields = function (authType) {
const sections = [
"auth-basic-fields-a2a-edit",
"auth-bearer-fields-a2a-edit",
"auth-headers-fields-a2a-edit",
"auth-oauth-fields-a2a-edit",
"auth-query_param-fields-a2a-edit",
];
sections.forEach((id) => {
const el = safeGetElement(id);
if (el) {
el.style.display = "none";
}
});
if (authType) {
const el = safeGetElement(`auth-${authType}-fields-a2a-edit`);
if (el) {
el.style.display = "block";
}
}
};
/**
* Open A2A test modal with agent details
* @param {string} agentId - ID of the agent to test
* @param {string} agentName - Name of the agent for display
* @param {string} endpointUrl - Endpoint URL of the agent
*/
export const testA2AAgent = async function (agentId, agentName, endpointUrl) {
try {
console.log("Opening A2A test modal for:", agentName);
// Clean up any existing event listeners
cleanupA2ATestModal();
// Open the modal
openModal("a2a-test-modal");
// Set modal title and description
const titleElement = safeGetElement("a2a-test-modal-title");
const descElement = safeGetElement("a2a-test-modal-description");
const agentIdInput = safeGetElement("a2a-test-agent-id");
const queryInput = safeGetElement("a2a-test-query");
const resultDiv = safeGetElement("a2a-test-result");
if (titleElement) {
titleElement.textContent = `Test A2A Agent: ${agentName}`;
}
if (descElement) {
descElement.textContent = `Endpoint: ${endpointUrl}`;
}
if (agentIdInput) {
agentIdInput.value = agentId;
}
if (queryInput) {
// Reset to default value
queryInput.value = "Hello from ContextForge Admin UI test!";
}
if (resultDiv) {
resultDiv.classList.add("hidden");
}
// Set up form submission handler
const form = safeGetElement("a2a-test-form");
if (form) {
a2aTestFormHandler = async (e) => {
await handleA2ATestSubmit(e);
};
form.addEventListener("submit", a2aTestFormHandler);
}
// Set up close button handler
const closeButton = safeGetElement("a2a-test-close");
if (closeButton) {
a2aTestCloseHandler = () => {
handleA2ATestClose();
};
closeButton.addEventListener("click", a2aTestCloseHandler);
}
} catch (error) {
console.error("Error setting up A2A test modal:", error);
showErrorMessage("Failed to open A2A test modal");
}
};
/**
* Handle A2A test form submission
* @param {Event} e - Form submit event
*/
export const handleA2ATestSubmit = async function (e) {
e.preventDefault();
const loading = safeGetElement("a2a-test-loading");
const responseDiv = safeGetElement("a2a-test-response-json");
const resultDiv = safeGetElement("a2a-test-result");
const testButton = safeGetElement("a2a-test-submit");
try {
// Show loading
if (loading) {
loading.classList.remove("hidden");
}
if (resultDiv) {
resultDiv.classList.add("hidden");
}
if (testButton) {
testButton.disabled = true;
testButton.textContent = "Testing...";
}
const agentId = safeGetElement("a2a-test-agent-id")?.value;
const query =
safeGetElement("a2a-test-query")?.value ||
"Hello from ContextForge Admin UI test!";
if (!agentId) {
throw new Error("Agent ID is missing");
}
// Reuse the standard admin auth helper:
// - sends Bearer auth when a JS-readable token exists
// - otherwise relies on same-origin cookie auth
// Never synthesize default credentials client-side.
const headers = await getAuthHeaders(true);
// Send test request with user query
const response = await fetchWithTimeout(
`${window.ROOT_PATH}/admin/a2a/${agentId}/test`,
{
method: "POST",
headers,
body: JSON.stringify({ query }),
},
window.MCPGATEWAY_UI_TOOL_TEST_TIMEOUT || 60000
);
// Parse the JSON body for all responses — the backend returns
// structured {success, error, error_type} even for non-2xx status
// codes, and the display logic below already handles both cases.
let result;
try {
result = await response.json();
} catch {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
// Display result
const isSuccess = result.success && !result.error;
const icon = isSuccess ? "✅" : "❌";
const title = isSuccess ? "Test Successful" : "Test Failed";
let bodyHtml = "";
if (result.result) {
bodyHtml = `<details open>
<summary class='cursor-pointer font-medium'>Response</summary>
<pre class="text-sm px-4 max-h-96 dark:bg-gray-800 dark:text-gray-100 overflow-auto whitespace-pre-wrap">${escapeHtml(JSON.stringify(result.result, null, 2))}</pre>
</details>`;
}
responseDiv.innerHTML = `
<div class="p-3 rounded ${isSuccess ? "bg-green-50 dark:bg-green-900/20" : "bg-red-50 dark:bg-red-900/20"}">
<h4 class="font-bold ${isSuccess ? "text-green-700 dark:text-green-400" : "text-red-700 dark:text-red-400"}">${icon} ${title}</h4>
${result.error ? `<p class="text-red-600 dark:text-red-400 mt-2">Error: ${escapeHtml(result.error)}</p>` : ""}
${bodyHtml}
</div>
`;
} catch (error) {
console.error("A2A test error:", error);
if (responseDiv) {
responseDiv.innerHTML = `<div class="text-red-600 dark:text-red-400 p-4 bg-red-50 dark:bg-red-900/20 rounded">❌ Error: ${escapeHtml(error.message)}</div>`;
}
} finally {
if (loading) {
loading.classList.add("hidden");
}
if (resultDiv) {
resultDiv.classList.remove("hidden");
}
if (testButton) {
testButton.disabled = false;
testButton.textContent = "Test Agent";
}
}
};
/**
* Handle A2A test modal close
*/
export const handleA2ATestClose = function () {
try {
// Reset form
const form = safeGetElement("a2a-test-form");
if (form) {
form.reset();
}
// Clear response
const responseDiv = safeGetElement("a2a-test-response-json");
const resultDiv = safeGetElement("a2a-test-result");
if (responseDiv) {
responseDiv.innerHTML = "";
}
if (resultDiv) {
resultDiv.classList.add("hidden");
}
// Close modal
closeModal("a2a-test-modal");
} catch (error) {
console.error("Error closing A2A test modal:", error);
}
};
/**
* Clean up A2A test modal event listeners
*/
export const cleanupA2ATestModal = function () {
try {
const form = safeGetElement("a2a-test-form");
const closeButton = safeGetElement("a2a-test-close");
if (form && a2aTestFormHandler) {
form.removeEventListener("submit", a2aTestFormHandler);
a2aTestFormHandler = null;
}
if (closeButton && a2aTestCloseHandler) {
closeButton.removeEventListener("click", a2aTestCloseHandler);
a2aTestCloseHandler = null;
}
console.log("✓ Cleaned up A2A test modal listeners");
} catch (error) {
console.error("Error cleaning up A2A test modal:", error);
}
};