|
|
@@ -10,6 +10,7 @@ import {
|
|
|
editAgentWebsite as _editAgentWebsite,
|
|
|
createNewAgent as _createNewAgent,
|
|
|
editAgent as _editAgent,
|
|
|
+ getNewAgentInfo,
|
|
|
} from "@/service/agent";
|
|
|
import {
|
|
|
TAgentDetail,
|
|
|
@@ -21,12 +22,14 @@ import {
|
|
|
} from "@/types/agent";
|
|
|
import { isSuccess } from "@/utils";
|
|
|
import Taro from "@tarojs/taro";
|
|
|
+import { TAvatarItem } from "@/service/storage";
|
|
|
|
|
|
export interface AgentStoreState {
|
|
|
agents: TAgent[];
|
|
|
- agent: TAgentDetail | null;
|
|
|
- defaultAgent: TAgentDetail | TAgent | null;
|
|
|
- agentTemp: Omit<TAgentRequiredData, 'agentId'>; // 未创建智能体临时存储
|
|
|
+ agent: Partial<TAgentDetail> | null;
|
|
|
+ agentEdit: Partial<TAgentDetail> | null;
|
|
|
+ defaultAgent: Partial<TAgentDetail> | TAgent | null;
|
|
|
+ currentEditAvatar: TAvatarItem | null;
|
|
|
// 无需登录查看 agent 信息
|
|
|
agentProfile: TAgentDetail | null;
|
|
|
agentContactCard: TAgentContactCard | null;// deprecated
|
|
|
@@ -35,14 +38,15 @@ export interface AgentStoreState {
|
|
|
actions: {
|
|
|
fetchAgents: () => Promise<TAgent[]>;
|
|
|
// 请求智能体数据登录状态
|
|
|
- fetchAgent: (agentId: string) => Promise<TAgentDetail | null>;
|
|
|
+ fetchAgent: (agentId: string) => Promise<Partial<TAgentDetail> | null>;
|
|
|
+ fetchDefaultNewAgentInfo: () => Promise<Partial<TAgentDetail> | null>;
|
|
|
// 请求智能体数据非登录状态
|
|
|
fetchAgentProfile: (
|
|
|
agentId: string,
|
|
|
shareKey?: string
|
|
|
) => Promise<TAgentDetail | null>;
|
|
|
clearProfileAgent: () => void;
|
|
|
- setDefaultAgent: (agentId: string) => Promise<TAgentDetail | null>;
|
|
|
+ setDefaultAgent: (agentId: string) => Promise<Partial<TAgentDetail> | null>;
|
|
|
editAgentCharacter: (
|
|
|
agentId: string,
|
|
|
data: TEditAgentCharacter
|
|
|
@@ -53,23 +57,24 @@ export interface AgentStoreState {
|
|
|
data: TComponentItem[]
|
|
|
) => Promise<boolean>;
|
|
|
deleteAgent: (agentId: string) => Promise<void>;
|
|
|
- updateAgentTemp: (updates: Partial<TAgentRequiredData>) => void;
|
|
|
- clearAgentTemp: () => void;
|
|
|
- saveAgent: () => Promise<TAgentDetail | null>;
|
|
|
+ updateEditAgent: (updates: Partial<TAgentDetail>) => void;
|
|
|
+ saveAgent: (agentFields?: Partial<TAgentDetail>) => Promise<Partial<TAgentDetail> | null>;
|
|
|
resetData: () => void;
|
|
|
- clearMyAgent: () => void;
|
|
|
+ clearEditAgent: () => void;
|
|
|
+ setCurrentEditAvatar: (avatar: TAvatarItem|null) => void;
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
agents: [],
|
|
|
agent: null,
|
|
|
+ agentEdit: null,
|
|
|
agentProfile: null,
|
|
|
agentContactCard: null,
|
|
|
defaultAgent: null,
|
|
|
agentCharacter: null,
|
|
|
- agentTemp: {},
|
|
|
ents: [],
|
|
|
+ currentEditAvatar: null,
|
|
|
actions: {
|
|
|
resetData: () => {
|
|
|
set({
|
|
|
@@ -99,28 +104,8 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
const result = isSuccess(response.status);
|
|
|
if (result && response.data) {
|
|
|
const agent = response.data;
|
|
|
- const agentTempData: Omit<TAgentRequiredData, 'agentId'> = {
|
|
|
- address: agent.address ?? undefined,
|
|
|
- avatarLogo: agent.avatarLogo ?? undefined,
|
|
|
- avatarUrl: agent.avatarUrl ?? undefined,
|
|
|
- email: agent.email ?? undefined,
|
|
|
- enabledChatBg: agent.enabledChatBg ?? undefined,
|
|
|
- enabledPersonalKb: agent.enabledPersonalKb ?? undefined,
|
|
|
- entId: agent.entId ? Number(agent.entId) : undefined,
|
|
|
- entName: agent.entName ?? undefined,
|
|
|
- greeting: agent.greeting ?? undefined,
|
|
|
- mobile: agent.mobile ?? undefined,
|
|
|
- name: agent.name ?? undefined,
|
|
|
- personality: agent.personality ?? undefined,
|
|
|
- position: agent.position ?? undefined,
|
|
|
- qrCodeUrl: agent.qrCodeUrl ?? undefined,
|
|
|
- questionGuides: agent.questionGuides ?? undefined,
|
|
|
- voiceId: agent.voiceId ?? undefined,
|
|
|
- voiceName: agent.voiceName ?? undefined,
|
|
|
- };
|
|
|
set({
|
|
|
agent: agent,
|
|
|
- agentTemp: agentTempData,
|
|
|
});
|
|
|
|
|
|
return response.data;
|
|
|
@@ -166,30 +151,33 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
}
|
|
|
return null;
|
|
|
},
|
|
|
- saveAgent: async () => {
|
|
|
- const { agent, agentTemp } = get();
|
|
|
+ saveAgent: async (agentFields = {} ) => {
|
|
|
+ const { agentEdit } = get();
|
|
|
|
|
|
// 如果当前agent有agentId,则执行编辑操作
|
|
|
- if (agent?.agentId) {
|
|
|
+ if (agentEdit?.agentId) {
|
|
|
// 处理null值转换为undefined以符合TAgentRequiredData类型
|
|
|
+ const mixedAgent = {
|
|
|
+ ...agentEdit,
|
|
|
+ ...agentFields,
|
|
|
+ };
|
|
|
const cleanAgent = {
|
|
|
- ...agent,
|
|
|
- address: agent.address ?? undefined,
|
|
|
- email: agent.email ?? undefined,
|
|
|
- entName: agent.entName ?? undefined,
|
|
|
- mobile: agent.mobile ?? undefined,
|
|
|
- name: agent.name ?? undefined,
|
|
|
- position: agent.position ?? undefined,
|
|
|
- qrCodeUrl: agent.qrCodeUrl ?? undefined,
|
|
|
- avatarUrl: agent.avatarUrl ?? undefined,
|
|
|
- avatarLogo: agent.avatarLogo ?? undefined,
|
|
|
- entId: agent.entId ? Number(agent.entId) : undefined,
|
|
|
+ ...mixedAgent,
|
|
|
+ address: mixedAgent.address ?? undefined,
|
|
|
+ email: mixedAgent.email ?? undefined,
|
|
|
+ entName: mixedAgent.entName ?? undefined,
|
|
|
+ mobile: mixedAgent.mobile ?? undefined,
|
|
|
+ name: mixedAgent.name ?? undefined,
|
|
|
+ position: mixedAgent.position ?? undefined,
|
|
|
+ qrCodeUrl: mixedAgent.qrCodeUrl ?? undefined,
|
|
|
+ avatarUrl: mixedAgent.avatarUrl ?? undefined,
|
|
|
+ avatarLogo: mixedAgent.avatarLogo ?? undefined,
|
|
|
+ entId: mixedAgent.entId ? Number(mixedAgent.entId) : undefined,
|
|
|
};
|
|
|
|
|
|
const updateData: TAgentRequiredData = {
|
|
|
...cleanAgent,
|
|
|
- ...agentTemp,
|
|
|
- agentId: agent.agentId,
|
|
|
+ agentId: agentEdit.agentId,
|
|
|
};
|
|
|
|
|
|
const response = await _editAgent(updateData);
|
|
|
@@ -198,8 +186,6 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
if (result && response.data) {
|
|
|
// 更新当前agent数据
|
|
|
set({ agent: response.data });
|
|
|
- // 清除临时数据
|
|
|
- get().actions.clearAgentTemp();
|
|
|
// 重新获取agents列表以更新缓存
|
|
|
await get().actions.fetchAgents();
|
|
|
return response.data;
|
|
|
@@ -207,11 +193,31 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
return null;
|
|
|
} else {
|
|
|
// 如果没有agentId,则创建新智能体
|
|
|
- if (Object.keys(agentTemp).length === 0) {
|
|
|
+ if (!agentEdit) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- const response = await _createNewAgent(agentTemp);
|
|
|
+ const createData: Omit<TAgentRequiredData, 'agentId'> = {
|
|
|
+ address: agentEdit.address ?? undefined,
|
|
|
+ avatarLogo: agentEdit.avatarLogo ?? undefined,
|
|
|
+ avatarUrl: agentEdit.avatarUrl ?? undefined,
|
|
|
+ email: agentEdit.email ?? undefined,
|
|
|
+ enabledChatBg: agentEdit.enabledChatBg ?? undefined,
|
|
|
+ enabledPersonalKb: agentEdit.enabledPersonalKb ?? undefined,
|
|
|
+ entId: agentEdit.entId ? Number(agentEdit.entId) : undefined,
|
|
|
+ entName: agentEdit.entName ?? undefined,
|
|
|
+ greeting: agentEdit.greeting ?? undefined,
|
|
|
+ mobile: agentEdit.mobile ?? undefined,
|
|
|
+ name: agentEdit.name ?? undefined,
|
|
|
+ personality: agentEdit.personality ?? undefined,
|
|
|
+ position: agentEdit.position ?? undefined,
|
|
|
+ qrCodeUrl: agentEdit.qrCodeUrl ?? undefined,
|
|
|
+ questionGuides: agentEdit.questionGuides ?? undefined,
|
|
|
+ voiceId: agentEdit.voiceId ?? undefined,
|
|
|
+ voiceName: agentEdit.voiceName ?? undefined,
|
|
|
+ };
|
|
|
+
|
|
|
+ const response = await _createNewAgent(createData);
|
|
|
const result = isSuccess(response.status);
|
|
|
|
|
|
if (result && response.data) {
|
|
|
@@ -222,25 +228,21 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
agent: newAgent,
|
|
|
defaultAgent: newAgent,
|
|
|
}));
|
|
|
- // 清除临时数据
|
|
|
- get().actions.clearAgentTemp();
|
|
|
return newAgent;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
},
|
|
|
- updateAgentTemp: (updates: Partial<TAgentRequiredData>) => {
|
|
|
+ // 更新 agent 字段
|
|
|
+ updateEditAgent: async (updates: Partial<TAgentDetail>) => {
|
|
|
set((state) => ({
|
|
|
- agentTemp: {
|
|
|
- ...state.agentTemp,
|
|
|
+ agentEdit: {
|
|
|
+ ...state.agentEdit,
|
|
|
...updates,
|
|
|
},
|
|
|
}));
|
|
|
},
|
|
|
- clearAgentTemp: () => {
|
|
|
- set({ agentTemp: {} });
|
|
|
- },
|
|
|
// 编辑声音,人设,开场白,问题引导,知识库
|
|
|
// deprecated
|
|
|
editAgentCharacter: async (agentId: string, data: TEditAgentCharacter) => {
|
|
|
@@ -302,11 +304,30 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
- clearMyAgent: () => {
|
|
|
+ setCurrentEditAvatar: (avatarItem) => {
|
|
|
+ set({
|
|
|
+ currentEditAvatar: avatarItem,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ clearAgent: () => {
|
|
|
set({
|
|
|
agent: null,
|
|
|
});
|
|
|
},
|
|
|
+ fetchDefaultNewAgentInfo: async () => {
|
|
|
+ const response = await getNewAgentInfo()
|
|
|
+ if (isSuccess(response.status)) {
|
|
|
+ // set({
|
|
|
+ // agentEdit: response.data,
|
|
|
+ // });
|
|
|
+ }
|
|
|
+ return response.data
|
|
|
+ },
|
|
|
+ clearEditAgent: async () => {
|
|
|
+ set({
|
|
|
+ agentEdit: null,
|
|
|
+ });
|
|
|
+ },
|
|
|
}
|
|
|
}));
|
|
|
|