Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
import { useState, useEffect, useCallback } from 'react';
import { workspaceApi } from '@/api';
import type { MCPClient, MCPClientStatus, Skill } from '@/api';
import type { UploadOptions } from '@/api/workspace';
export function useWorkspace(agentId: string | null, sessionId: string | null) {
const [mcps, setMcps] = useState<MCPClientStatus[]>([]);
const [skills, setSkills] = useState<Skill[]>([]);
const [loading, setLoading] = useState(false);
const [skillsLoading, setSkillsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const refetch = useCallback(async () => {
if (!agentId || !sessionId) {
setMcps([]);
return;
}
setLoading(true);
setError(null);
try {
setMcps(await workspaceApi.mcp.list(agentId, sessionId));
} catch (e) {
setError(e as Error);
} finally {
setLoading(false);
}
}, [agentId, sessionId]);
const refetchSkills = useCallback(async () => {
if (!agentId || !sessionId) {
setSkills([]);
return;
}
setSkillsLoading(true);
try {
setSkills(await workspaceApi.skill.list(agentId, sessionId));
} catch (e) {
setError(e as Error);
} finally {
setSkillsLoading(false);
}
}, [agentId, sessionId]);
useEffect(() => {
refetch();
}, [refetch]);
useEffect(() => {
refetchSkills();
}, [refetchSkills]);
const addMcps = useCallback(
async (clients: MCPClient[]) => {
if (!agentId || !sessionId) throw new Error('No agent/session selected');
const existingNames = new Set(mcps.map((m) => m.name));
for (const mcp of clients) {
if (existingNames.has(mcp.name)) {
throw new Error(`MCP server "${mcp.name}" already exists in this workspace.`);
}
}
const batchNames = new Set<string>();
for (const mcp of clients) {
if (batchNames.has(mcp.name)) {
throw new Error(`Duplicate MCP server name "${mcp.name}" in configuration.`);
}
batchNames.add(mcp.name);
}
for (const mcp of clients) {
await workspaceApi.mcp.add(agentId, sessionId, mcp);
}
await refetch();
},
[agentId, sessionId, mcps, refetch],
);
const addMcpsFromLibrary = useCallback(
async (mcpIds: string[]) => {
if (!agentId || !sessionId) throw new Error('No agent/session selected');
const result = await workspaceApi.mcp.addFromLibrary(agentId, sessionId, mcpIds);
await refetch();
// Reported per MCP, so a partial success is still a success for
// what landed; surface only what did not.
const failures = Object.entries(result.failed);
if (failures.length > 0) {
throw new Error(failures.map(([name, why]) => `${name}: ${why}`).join('\n'));
}
},
[agentId, sessionId, refetch],
);
const removeMcp = useCallback(
async (mcpName: string) => {
if (!agentId || !sessionId) throw new Error('No agent/session selected');
await workspaceApi.mcp.remove(mcpName, agentId, sessionId);
await refetch();
},
[agentId, sessionId, refetch],
);
const uploadSkill = useCallback(
async (files: File[], options: UploadOptions = {}) => {
if (!agentId || !sessionId) throw new Error('No agent/session selected');
await workspaceApi.skill.upload(agentId, sessionId, files, options);
await refetchSkills();
},
[agentId, sessionId, refetchSkills],
);
const addSkillsFromLibrary = useCallback(
async (skillIds: string[]) => {
if (!agentId || !sessionId) throw new Error('No agent/session selected');
const result = await workspaceApi.skill.addFromLibrary(agentId, sessionId, skillIds);
await refetchSkills();
// Reported per skill, so a partial success is still a success
// for what landed; surface only what did not.
const failures = Object.entries(result.failed);
if (failures.length > 0) {
throw new Error(failures.map(([name, why]) => `${name}: ${why}`).join('\n'));
}
},
[agentId, sessionId, refetchSkills],
);
const removeSkill = useCallback(
async (skillName: string) => {
if (!agentId || !sessionId) throw new Error('No agent/session selected');
await workspaceApi.skill.remove(skillName, agentId, sessionId);
await refetchSkills();
},
[agentId, sessionId, refetchSkills],
);
return {
mcps,
loading,
error,
refetch,
addMcps,
addMcpsFromLibrary,
removeMcp,
skills,
skillsLoading,
uploadSkill,
addSkillsFromLibrary,
removeSkill,
};
}