The open-source agent harness - the runtime layer that turns an LLM into a working agent.
'use client';
import {
useTrueForgeAgentSpec,
useTrueForgeFlushAgentSpec,
useTrueForgeUpdateAgentSpec,
} from '@truefoundry/trueforge-assistant-ui-runtime';
import { useCallback, useEffect, useRef, useState } from 'react';
import type { AgentConfigEditor } from '../atoms/draft/AgentConfigEditors.js';
import { useAgentConfigInstructions } from '../atoms/draft/AgentConfigInstructionsContext.js';
import type { AgentInstructionsDraft } from '../atoms/draft/AgentInstructionsDrawer.js';
import { useDraftCatalog } from '../atoms/draft/DraftCatalogProvider.js';
import { withInitialUserMessages } from '../atoms/draft/agentConfigMessages.js';
import { useResourcePermissions } from '../hooks/useResourcePermissions.js';
import { useOptionalServer, useServerCapabilities } from '../server/ServerContext.js';
import { shellIsCreateAgent, useShellMode } from '../server/ShellModeContext.js';
import type { AgentSpec, ConnectorState, McpToolSelection } from '../server/types.js';
import { useSlot } from '../theme/SlotsProvider.js';
export function AgentConfigDrawerContainer({ showClose = false }: { showClose?: boolean }) {
const { agentSpec } = useTrueForgeAgentSpec();
const updateAgentSpec = useTrueForgeUpdateAgentSpec();
const flushAgentSpec = useTrueForgeFlushAgentSpec();
const shell = useShellMode();
const agentId = shell.mode.status === 'active' ? shell.mode.agentId : undefined;
const { allows } = useResourcePermissions({
resourceType: 'agent',
resourceIds: agentId == null ? [] : [agentId],
});
const canManageAgent = allows(agentId, 'MANAGE');
const server = useOptionalServer();
const capabilities = useServerCapabilities();
const catalog = useDraftCatalog();
const AgentConfigPanel = useSlot('AgentConfigPanel');
const AgentConfigEditors = useSlot('AgentConfigEditors');
const [editor, setEditor] = useState<AgentConfigEditor | null>(null);
const isBuilder = shellIsCreateAgent(shell.mode);
const { draft: instructionDraft, flush: flushInstructions } = useAgentConfigInstructions();
const closeDrawer = useCallback(() => {
flushInstructions();
void flushAgentSpec();
shell.setAgentConfigOpen(false);
}, [flushAgentSpec, flushInstructions, shell]);
useEffect(() => {
if (isBuilder) catalog.ensureLoaded();
}, [catalog, isBuilder]);
useEffect(() => {
if (!canManageAgent) setEditor(null);
}, [canManageAgent]);
useEffect(() => {
if (!showClose || !shell.agentConfigOpen) return;
const onKeyDown = (event: KeyboardEvent) => {
if (
!event.defaultPrevented &&
event.key === 'Escape' &&
editor === null &&
document.querySelector('dialog[open]') === null
) {
closeDrawer();
}
};
window.addEventListener('keydown', onKeyDown);
return () => window.removeEventListener('keydown', onKeyDown);
}, [closeDrawer, editor, shell.agentConfigOpen, showClose]);
// Flush only on unmount. These callbacks are new every render; depending on them
// runs this cleanup between keystrokes and sends the in-flight spec sync immediately.
const flushOnUnmountRef = useRef({ flushAgentSpec, flushInstructions });
flushOnUnmountRef.current = { flushAgentSpec, flushInstructions };
useEffect(
() => () => {
const { flushAgentSpec: flushSpec, flushInstructions: flushDraft } = flushOnUnmountRef.current;
flushDraft();
try {
void flushSpec();
} catch {
// Runtime already gone.
}
},
[],
);
const commitSpec = useCallback(
({ next, instructions }: { next: AgentSpec; instructions: string }) => {
if (!canManageAgent) return;
if (next.skills && next.skills.length > 0 && capabilities?.sandbox.enabled === true) {
updateAgentSpec?.({
...next,
instructions: instructions || undefined,
config: {
...next.config,
sandbox: { ...next.config?.sandbox, enabled: true },
},
});
return;
}
updateAgentSpec?.({ ...next, instructions: instructions || undefined });
},
[canManageAgent, capabilities?.sandbox.enabled, updateAgentSpec],
);
const updateSpec = useCallback(
(next: AgentSpec) => {
commitSpec({ next, instructions: instructionDraft });
},
[commitSpec, instructionDraft],
);
const saveInstructions = useCallback(
(draft: AgentInstructionsDraft) => {
if (agentSpec === null) return;
commitSpec({
next: withInitialUserMessages({ spec: agentSpec, messages: draft.messages }),
instructions: draft.instructions,
});
},
[agentSpec, commitSpec],
);
const loadMcpTools = useCallback(
async (connectorId: string): Promise<McpToolSelection[]> => {
if (server?.getMcpTools === undefined) return [];
return server.getMcpTools({ connectorId });
},
[server],
);
const loadMcpConnector = useCallback(
async (connectorId: string): Promise<ConnectorState | undefined> => {
return server?.getMcpConnector?.({ connectorId });
},
[server],
);
if (!isBuilder || agentSpec === null || (showClose && !shell.agentConfigOpen)) {
return null;
}
const model = catalog.models.find(item => item.name === agentSpec.model.name);
return (
<>
<AgentConfigPanel
spec={agentSpec}
model={model}
models={catalog.models}
modelsLoading={catalog.loading}
modelsError={catalog.error}
skills={catalog.skills}
skillsAvailable={capabilities?.skill.enabled === true}
webSearchAvailable={capabilities?.webSearch?.enabled === true}
instructions={instructionDraft}
onOpenEditor={nextEditor => {
if (canManageAgent) setEditor(nextEditor);
}}
onChange={updateSpec}
disabled={!canManageAgent}
onClose={showClose ? closeDrawer : undefined}
/>
<AgentConfigEditors
editor={editor}
spec={agentSpec}
models={catalog.models}
connectors={catalog.connectors}
skills={catalog.skills}
loading={catalog.loading}
error={catalog.error}
skillsDisabled={capabilities?.skill.enabled !== true}
sandboxAvailable={capabilities?.sandbox.enabled === true}
webSearchAvailable={capabilities?.webSearch?.enabled === true}
instructions={instructionDraft}
onInstructionsSave={saveInstructions}
{...(server?.getMcpConnector === undefined ? {} : { loadMcpConnector })}
loadMcpTools={loadMcpTools}
onRefreshConnectors={catalog.refreshConnectors}
onChange={updateSpec}
onClose={() => setEditor(null)}
/>
</>
);
}