feat(Agnet): support multi agents (#385)
This commit is contained in:
126
src/stores/agents.ts
Normal file
126
src/stores/agents.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
import { create } from 'zustand';
|
||||
import { hostApiFetch } from '@/lib/host-api';
|
||||
import type { ChannelType } from '@/types/channel';
|
||||
import type { AgentSummary, AgentsSnapshot } from '@/types/agent';
|
||||
|
||||
interface AgentsState {
|
||||
agents: AgentSummary[];
|
||||
defaultAgentId: string;
|
||||
configuredChannelTypes: string[];
|
||||
channelOwners: Record<string, string>;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
fetchAgents: () => Promise<void>;
|
||||
createAgent: (name: string) => Promise<void>;
|
||||
updateAgent: (agentId: string, name: string) => Promise<void>;
|
||||
deleteAgent: (agentId: string) => Promise<void>;
|
||||
assignChannel: (agentId: string, channelType: ChannelType) => Promise<void>;
|
||||
removeChannel: (agentId: string, channelType: ChannelType) => Promise<void>;
|
||||
clearError: () => void;
|
||||
}
|
||||
|
||||
function applySnapshot(snapshot: AgentsSnapshot | undefined) {
|
||||
return snapshot ? {
|
||||
agents: snapshot.agents,
|
||||
defaultAgentId: snapshot.defaultAgentId,
|
||||
configuredChannelTypes: snapshot.configuredChannelTypes,
|
||||
channelOwners: snapshot.channelOwners,
|
||||
} : {};
|
||||
}
|
||||
|
||||
export const useAgentsStore = create<AgentsState>((set) => ({
|
||||
agents: [],
|
||||
defaultAgentId: 'main',
|
||||
configuredChannelTypes: [],
|
||||
channelOwners: {},
|
||||
loading: false,
|
||||
error: null,
|
||||
|
||||
fetchAgents: async () => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const snapshot = await hostApiFetch<AgentsSnapshot & { success?: boolean }>('/api/agents');
|
||||
set({
|
||||
...applySnapshot(snapshot),
|
||||
loading: false,
|
||||
});
|
||||
} catch (error) {
|
||||
set({ loading: false, error: String(error) });
|
||||
}
|
||||
},
|
||||
|
||||
createAgent: async (name: string) => {
|
||||
set({ error: null });
|
||||
try {
|
||||
const snapshot = await hostApiFetch<AgentsSnapshot & { success?: boolean }>('/api/agents', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
set(applySnapshot(snapshot));
|
||||
} catch (error) {
|
||||
set({ error: String(error) });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
updateAgent: async (agentId: string, name: string) => {
|
||||
set({ error: null });
|
||||
try {
|
||||
const snapshot = await hostApiFetch<AgentsSnapshot & { success?: boolean }>(
|
||||
`/api/agents/${encodeURIComponent(agentId)}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({ name }),
|
||||
}
|
||||
);
|
||||
set(applySnapshot(snapshot));
|
||||
} catch (error) {
|
||||
set({ error: String(error) });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
deleteAgent: async (agentId: string) => {
|
||||
set({ error: null });
|
||||
try {
|
||||
const snapshot = await hostApiFetch<AgentsSnapshot & { success?: boolean }>(
|
||||
`/api/agents/${encodeURIComponent(agentId)}`,
|
||||
{ method: 'DELETE' }
|
||||
);
|
||||
set(applySnapshot(snapshot));
|
||||
} catch (error) {
|
||||
set({ error: String(error) });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
assignChannel: async (agentId: string, channelType: ChannelType) => {
|
||||
set({ error: null });
|
||||
try {
|
||||
const snapshot = await hostApiFetch<AgentsSnapshot & { success?: boolean }>(
|
||||
`/api/agents/${encodeURIComponent(agentId)}/channels/${encodeURIComponent(channelType)}`,
|
||||
{ method: 'PUT' }
|
||||
);
|
||||
set(applySnapshot(snapshot));
|
||||
} catch (error) {
|
||||
set({ error: String(error) });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
removeChannel: async (agentId: string, channelType: ChannelType) => {
|
||||
set({ error: null });
|
||||
try {
|
||||
const snapshot = await hostApiFetch<AgentsSnapshot & { success?: boolean }>(
|
||||
`/api/agents/${encodeURIComponent(agentId)}/channels/${encodeURIComponent(channelType)}`,
|
||||
{ method: 'DELETE' }
|
||||
);
|
||||
set(applySnapshot(snapshot));
|
||||
} catch (error) {
|
||||
set({ error: String(error) });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
clearError: () => set({ error: null }),
|
||||
}));
|
||||
Reference in New Issue
Block a user