|
@@ -66,12 +66,14 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
ents: [],
|
|
|
fetchAgents: async () => {
|
|
|
const response = await getAgents();
|
|
|
- if (response && response?.data?.length) {
|
|
|
- const defaultAgent = response.data.find( item => item.isDefault)
|
|
|
- set({ agents: response.data, defaultAgent });
|
|
|
- return response.data;
|
|
|
+ const agentsData = response?.data
|
|
|
+ // const agentsData = response?.data?.filter(item => !item.isEnt)
|
|
|
+ if (isSuccess(response.status) && agentsData.length) {
|
|
|
+ const defaultAgent = agentsData.find( item => item.isDefault)
|
|
|
+ set({ agents: agentsData, defaultAgent });
|
|
|
+ return agentsData;
|
|
|
}
|
|
|
-
|
|
|
+ set({ agents: [], defaultAgent: null });
|
|
|
return [];
|
|
|
},
|
|
|
fetchAgent: async (agentId: string) => {
|
|
@@ -101,6 +103,9 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
|
|
|
return response.data;
|
|
|
}
|
|
|
+ set({
|
|
|
+ agent: null,
|
|
|
+ })
|
|
|
return null;
|
|
|
},
|
|
|
// 请求无需登录的 getAgent 接口
|
|
@@ -142,10 +147,13 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
createAgent: async () => {
|
|
|
const response = await _createAgent();
|
|
|
const agentDetail = response.data
|
|
|
- if (agentDetail) {
|
|
|
+ if (agentDetail?.agentId) {
|
|
|
+ // 创建新智能体,自动设置为默认智能体
|
|
|
+ await get().setDefaultAgent(agentDetail.agentId)
|
|
|
+
|
|
|
const a: TAgent = {
|
|
|
agentId: agentDetail.agentId ?? "",
|
|
|
- isDefault: agentDetail.isDefault ?? false,
|
|
|
+ isDefault: true,
|
|
|
isEnt: agentDetail.isEnt ?? false,
|
|
|
isNewEnt: agentDetail.isNewEnt ?? false,
|
|
|
name: agentDetail.name ?? "",
|
|
@@ -200,14 +208,22 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
deleteAgent: async (agentId: string)=> {
|
|
|
const response = await _deleteAgent(agentId)
|
|
|
if(isSuccess(response.status)){
|
|
|
- const agents = get().agents.filter((item: TAgent) => item.agentId !== agentId)
|
|
|
- set({agents: [...agents]})
|
|
|
- get().fetchAgents()
|
|
|
- if(agents.length <= 0){
|
|
|
+ const de = get().agents.filter((item: TAgent) => item.agentId !== agentId).reverse()[0]
|
|
|
+ console.log(agentId, de, 'setDefault')
|
|
|
+ // 默认设置自创的智能体
|
|
|
+ if(de){
|
|
|
+ await get().setDefaultAgent(de.agentId)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重新拉取智能体列表
|
|
|
+ const restAgents = await get().fetchAgents()
|
|
|
+ if(restAgents.length <= 0){
|
|
|
+ set({agent: null, defaultAgent: null})
|
|
|
Taro.reLaunch({url: '/pages/index/index'})
|
|
|
return;
|
|
|
}
|
|
|
- const defaultAgent = agents.find((item)=> !!item.isDefault)
|
|
|
+
|
|
|
+ const defaultAgent = restAgents.find((item)=> !!item.isDefault)
|
|
|
if(defaultAgent){
|
|
|
await get().fetchAgent(defaultAgent.agentId)
|
|
|
}
|