Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions server/public/agents.html
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ <h2 id="detail-name"></h2>
let allAgents = [];
let filters = { type: 'all', status: 'all', compliance: 'all', search: '' };
let isLoading = true;
let loadError = null;

function escapeHtml(str) {
if (!str) return '';
Expand All @@ -546,6 +547,22 @@ <h2 id="detail-name"></h2>
}
}

// The registry list is public. A stale or unusable session cookie makes the
// server answer 401 instead of treating the caller as anonymous, so retry
// without credentials rather than rendering an empty registry.
async function fetchRegistryAgents(query) {
const url = `/api/registry/agents?${query}`;
let response = await fetch(url);
if (response.status === 401) {
response = await fetch(url, { credentials: 'omit' });
}
const data = await response.json();
if (!response.ok || !Array.isArray(data.agents)) {
throw new Error(data.error || `Request failed (${response.status})`);
}
return data.agents;
}

function agentToolCount(agent) {
return Number(agent?.capabilities?.tools_count ?? agent?.health?.tools_count) || 0;
}
Expand All @@ -568,9 +585,7 @@ <h2 id="detail-name"></h2>

async function loadAllAgents() {
try {
const response = await fetch('/api/registry/agents?health=true&properties=true&capabilities=true&compliance=true');
const data = await response.json();
const agents = data.agents || data;
const agents = await fetchRegistryAgents('health=true&properties=true&capabilities=true&compliance=true');

allAgents = agents.map(agent => {
const hasPropertyError = agent.propertiesError && agent.propertiesError.length > 0;
Expand All @@ -592,6 +607,7 @@ <h2 id="detail-name"></h2>
renderAgents();
} catch (error) {
console.error('Failed to load agents:', error);
loadError = error;
isLoading = false;
renderAgents();
}
Expand Down Expand Up @@ -633,6 +649,11 @@ <h2 id="detail-name"></h2>
return;
}

if (loadError) {
container.innerHTML = `<div class="error-text">Failed to load agents: ${escapeHtml(loadError.message)}. Try reloading the page.</div>`;
return;
}

const filtered = allAgents.filter(agent => {
if (filters.type !== 'all' && agent.type !== filters.type) return false;
if (filters.status !== 'all') {
Expand Down Expand Up @@ -884,9 +905,7 @@ <h2 id="detail-name"></h2>
const [type, ...nameParts] = agentId.split('/');
const name = nameParts.join('/');

const capsResp = await fetch('/api/registry/agents?health=true&capabilities=true&properties=true&compliance=true');
const capsData = await capsResp.json();
const allAgents = capsData.agents || [];
const allAgents = await fetchRegistryAgents('health=true&capabilities=true&properties=true&compliance=true');

const agentWithCaps =
allAgents.find(a => {
Expand Down Expand Up @@ -1136,9 +1155,7 @@ <h3>Agent information</h3>
async function fetchTools(agent) {
const section = document.getElementById('tools-section');
try {
const response = await fetch('/api/registry/agents?health=true&capabilities=true');
const data = await response.json();
const allAgents = data.agents || data;
const allAgents = await fetchRegistryAgents('health=true&capabilities=true');
const agentWithCaps = allAgents.find(a => a.url === agent.url);

const toolCount = agentToolCount(agentWithCaps);
Expand Down
29 changes: 28 additions & 1 deletion server/tests/unit/public-agent-registry-ui.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';

const agentsHtml = readFileSync(new URL('../../public/agents.html', import.meta.url), 'utf8');

Expand Down Expand Up @@ -34,4 +34,31 @@ describe('public agent registry UI', () => {
expect(agentsHtml).not.toContain('href="${escapeHtml(p.verification_url)}"');
expect(agentsHtml).toContain('escapeHtml(String(params.min_width || 0))');
});

describe('registry list fetch', () => {
const source = agentsHtml.match(/async function fetchRegistryAgents\(query\) \{[\s\S]*?\n \}\n/)?.[0];
const load = (fetchImpl: typeof fetch) =>
new Function('fetch', `${source}; return fetchRegistryAgents;`)(fetchImpl) as (query: string) => Promise<unknown[]>;
const json = (status: number, body: unknown) =>
new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } });

it('retries anonymously when a stale session cookie is rejected', async () => {
expect(source).toBeDefined();
const fetchMock = vi.fn()
.mockResolvedValueOnce(json(401, { error: 'Invalid session', login_url: '/auth/login' }))
.mockResolvedValueOnce(json(200, { agents: [{ url: 'https://agent.example' }], count: 1 }));

await expect(load(fetchMock)('health=true')).resolves.toEqual([{ url: 'https://agent.example' }]);
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/registry/agents?health=true');
expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/registry/agents?health=true', { credentials: 'omit' });
});

it('surfaces failures instead of rendering an empty registry', async () => {
const fetchMock = vi.fn().mockResolvedValue(json(503, { error: 'Authorization service temporarily unavailable' }));

await expect(load(fetchMock)('health=true')).rejects.toThrow('Authorization service temporarily unavailable');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(agentsHtml).toContain('Failed to load agents: ${escapeHtml(loadError.message)}');
});
});
});
Loading