Skip to content

Commit 17117dc

Browse files
committed
fix(template): read agents list from clientConfig, drop /api/agents/info fetch
Reviewer caught the redundancy: the agents plugin already exposes `{ agents, defaultAgent }` via `clientConfig()` at startup, inlined into the boot HTML via `<script id="__appkit__">`. The template was doing an extra `GET /api/agents/info` round-trip on mount to fetch the same data that's already sitting in `window.__APPKIT_CONFIG__` and readable via `usePluginClientConfig`. Replace the useEffect + fetch + error-handling block with a synchronous `usePluginClientConfig<AgentsClientConfig>('agents')`. Net savings: -20 lines of effect/error/loading-state plumbing, one fewer HTTP request on page load, no flicker between empty-then- populated agent buttons because the data is available on first render. `selectedAgent` is now initialised directly from `defaultAgent ?? agents[0] ?? null` in `useState`, so there's no intermediate "loading agents…" state. Input placeholder updates to "No agents registered" when the registry is empty (which only happens if the agents plugin loaded without any agents — config bug, worth showing). The `/api/agents/info` route is left in place on the server — it's still useful for programmatic callers and debugging — just no longer consumed by the template. Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
1 parent c557334 commit 17117dc

1 file changed

Lines changed: 23 additions & 24 deletions

File tree

template/client/src/pages/agents/AgentChat.tsx

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
CardContent,
88
Input,
99
useAgentChat,
10+
usePluginClientConfig,
1011
} from '@databricks/appkit-ui/react';
1112

1213
interface Message {
@@ -16,25 +17,38 @@ interface Message {
1617
toolName?: string;
1718
}
1819

19-
interface AgentInfo {
20+
/**
21+
* Shape of the agents plugin's `clientConfig()` payload — exposed by
22+
* the agents plugin at server startup and inlined into the boot HTML
23+
* via `<script id="__appkit__">`. Read with `usePluginClientConfig` so
24+
* the page doesn't need a separate `GET /api/agents/info` round-trip.
25+
*/
26+
interface AgentsClientConfig {
2027
agents: string[];
2128
defaultAgent: string | null;
2229
}
2330

2431
/**
2532
* Minimal chat surface for the `agents` plugin.
2633
*
27-
* - Lists registered agents from `GET /api/agents/info` and lets the user
28-
* pick one (markdown `assistant` from `config/agents/assistant/agent.md`
29-
* and code-defined `helper` from `server/agents/helper.ts`).
34+
* - Reads the registered agent list from `usePluginClientConfig('agents')`
35+
* (boot-time, no extra fetch) and lets the user pick one (markdown
36+
* `assistant` from `config/agents/assistant/agent.md` and code-defined
37+
* `helper` from `server/agents/helper.ts`).
3038
* - Sends turns via `useAgentChat()` (POSTs `/api/agents/chat` and
3139
* consumes the Responses-API SSE stream the agents plugin emits).
3240
* - Renders streaming assistant text incrementally and surfaces tool
3341
* calls as separate inline rows.
3442
*/
3543
export function AgentChat() {
36-
const [agents, setAgents] = useState<string[]>([]);
37-
const [selectedAgent, setSelectedAgent] = useState<string | null>(null);
44+
// Agent registry comes from the agents plugin's `clientConfig()` payload
45+
// (boot-time, no fetch). `defaultAgent` is null only when the agents
46+
// plugin loaded with no registered agents.
47+
const { agents, defaultAgent } =
48+
usePluginClientConfig<AgentsClientConfig>('agents');
49+
const [selectedAgent, setSelectedAgent] = useState<string | null>(
50+
defaultAgent ?? agents[0] ?? null,
51+
);
3852
const [messages, setMessages] = useState<Message[]>([]);
3953
const [input, setInput] = useState('');
4054
const [pendingAssistantId, setPendingAssistantId] = useState<string | null>(
@@ -68,23 +82,6 @@ export function AgentChat() {
6882
onEvent: handleEvent,
6983
});
7084

71-
useEffect(() => {
72-
fetch('/api/agents/info')
73-
.then((res) => {
74-
if (!res.ok) throw new Error(`agents info failed: ${res.statusText}`);
75-
return res.json() as Promise<AgentInfo>;
76-
})
77-
.then((info) => {
78-
setAgents(info.agents);
79-
setSelectedAgent(info.defaultAgent ?? info.agents[0] ?? null);
80-
})
81-
.catch(() => {
82-
// Hook surfaces request-time errors via `error`; this catch only
83-
// matters for the initial agents-list fetch, which is fine to
84-
// silently leave empty.
85-
});
86-
}, []);
87-
8885
// Mirror the streaming `content` into the pending assistant message so
8986
// existing tool-call rows interleave correctly with deltas.
9087
useEffect(() => {
@@ -199,7 +196,9 @@ export function AgentChat() {
199196
value={input}
200197
onChange={(e) => setInput(e.target.value)}
201198
placeholder={
202-
selectedAgent ? `Message ${selectedAgent}…` : 'Loading agents…'
199+
selectedAgent
200+
? `Message ${selectedAgent}…`
201+
: 'No agents registered'
203202
}
204203
disabled={!selectedAgent || isStreaming}
205204
/>

0 commit comments

Comments
 (0)