|
|
@@ -1,6 +1,5 @@
|
|
|
import { create } from "zustand";
|
|
|
import {
|
|
|
- createAgent as _createAgent,
|
|
|
getAgents,
|
|
|
getMyAgent as _getMyAgent,
|
|
|
getAgent as _getAgent,
|
|
|
@@ -9,6 +8,8 @@ import {
|
|
|
editAgentCard as _editAgentCard,
|
|
|
editAgentCharacter as _editAgentCharacter,
|
|
|
editAgentWebsite as _editAgentWebsite,
|
|
|
+ createNewAgent as _createNewAgent,
|
|
|
+ editAgent as _editAgent,
|
|
|
} from "@/service/agent";
|
|
|
import {
|
|
|
TAgentDetail,
|
|
|
@@ -16,7 +17,7 @@ import {
|
|
|
TAgentContactCard,
|
|
|
TEditAgentCharacter,
|
|
|
TComponentItem,
|
|
|
- TAgentTemp,
|
|
|
+ TAgentRequiredData,
|
|
|
} from "@/types/agent";
|
|
|
import { isSuccess } from "@/utils";
|
|
|
import Taro from "@tarojs/taro";
|
|
|
@@ -25,36 +26,39 @@ export interface AgentStoreState {
|
|
|
agents: TAgent[];
|
|
|
agent: TAgentDetail | null;
|
|
|
defaultAgent: TAgentDetail | TAgent | null;
|
|
|
- agentTemp: TAgentTemp; // 未创建智能体临时存储
|
|
|
+ agentTemp: Omit<TAgentRequiredData, 'agentId'>; // 未创建智能体临时存储
|
|
|
// 无需登录查看 agent 信息
|
|
|
agentProfile: TAgentDetail | null;
|
|
|
agentContactCard: TAgentContactCard | null;// deprecated
|
|
|
agentCharacter: TEditAgentCharacter | null;// deprecated
|
|
|
ents: { entName: string; entId: string | number }[];
|
|
|
- fetchAgents: () => Promise<TAgent[]>;
|
|
|
- // 请求智能体数据登录状态
|
|
|
- fetchAgent: (agentId: string) => Promise<TAgentDetail | null>;
|
|
|
- // 请求智能体数据非登录状态
|
|
|
- fetchAgentProfile: (
|
|
|
- agentId: string,
|
|
|
- shareKey?: string
|
|
|
- ) => Promise<TAgentDetail | null>;
|
|
|
- createAgent: () => Promise<TAgentDetail | null>;
|
|
|
- clearProfileAgent: () => void;
|
|
|
- setDefaultAgent: (agentId: string) => Promise<TAgentDetail | null>;
|
|
|
- editAgentCharacter: (
|
|
|
- agentId: string,
|
|
|
- data: TEditAgentCharacter
|
|
|
- ) => Promise<boolean>;
|
|
|
- editAgentCard: (agentId: string, data: TAgentContactCard) => Promise<boolean>;
|
|
|
- editAgentWebsite: (
|
|
|
- agentId: string,
|
|
|
- data: TComponentItem[]
|
|
|
- ) => Promise<boolean>;
|
|
|
- deleteAgent: (agentId: string) => Promise<void>;
|
|
|
- updateAgentTemp: (updates: Partial<TAgentTemp>) => void;
|
|
|
- clearAgentTemp: () => void;
|
|
|
- resetData: () => void;
|
|
|
+ actions: {
|
|
|
+ fetchAgents: () => Promise<TAgent[]>;
|
|
|
+ // 请求智能体数据登录状态
|
|
|
+ fetchAgent: (agentId: string) => Promise<TAgentDetail | null>;
|
|
|
+ // 请求智能体数据非登录状态
|
|
|
+ fetchAgentProfile: (
|
|
|
+ agentId: string,
|
|
|
+ shareKey?: string
|
|
|
+ ) => Promise<TAgentDetail | null>;
|
|
|
+ clearProfileAgent: () => void;
|
|
|
+ setDefaultAgent: (agentId: string) => Promise<TAgentDetail | null>;
|
|
|
+ editAgentCharacter: (
|
|
|
+ agentId: string,
|
|
|
+ data: TEditAgentCharacter
|
|
|
+ ) => Promise<boolean>;
|
|
|
+ editAgentCard: (agentId: string, data: TAgentContactCard) => Promise<boolean>;
|
|
|
+ editAgentWebsite: (
|
|
|
+ agentId: string,
|
|
|
+ data: TComponentItem[]
|
|
|
+ ) => Promise<boolean>;
|
|
|
+ deleteAgent: (agentId: string) => Promise<void>;
|
|
|
+ updateAgentTemp: (updates: Partial<TAgentRequiredData>) => void;
|
|
|
+ clearAgentTemp: () => void;
|
|
|
+ saveAgent: () => Promise<TAgentDetail | null>;
|
|
|
+ resetData: () => void;
|
|
|
+ clearMyAgent: () => void;
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
@@ -66,191 +70,247 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
|
|
|
agentCharacter: null,
|
|
|
agentTemp: {},
|
|
|
ents: [],
|
|
|
- resetData: () => {
|
|
|
- set({
|
|
|
- agents: [],
|
|
|
- agent: null,
|
|
|
- agentProfile: null,
|
|
|
- agentContactCard: null,
|
|
|
- defaultAgent: null,
|
|
|
- agentCharacter: null,
|
|
|
- ents: [],
|
|
|
- });
|
|
|
- },
|
|
|
- fetchAgents: async () => {
|
|
|
- const response = await getAgents();
|
|
|
- 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) => {
|
|
|
- const response = await _getMyAgent(agentId);
|
|
|
- const result = isSuccess(response.status);
|
|
|
- if (result && response.data) {
|
|
|
- const agent = response.data;
|
|
|
+ actions: {
|
|
|
+ resetData: () => {
|
|
|
set({
|
|
|
- agent: response.data,
|
|
|
- agentContactCard: {
|
|
|
- address: agent.address ?? "",
|
|
|
- email: agent.email ?? "",
|
|
|
- entName: agent.entName ?? "",
|
|
|
- mobile: agent.mobile ?? "",
|
|
|
- name: agent.name ?? "",
|
|
|
- position: agent.position ?? "",
|
|
|
- qrCodeUrl: agent.qrCodeUrl ?? "",
|
|
|
- },
|
|
|
- agentCharacter: {
|
|
|
- enabledPersonalKb: agent.enabledPersonalKb ?? false,
|
|
|
- greeting: agent.greeting ?? `你好,我是${agent.name}`,
|
|
|
- personality: agent.personality ?? "",
|
|
|
- questionGuides: agent.questionGuides ?? [],
|
|
|
- voiceId: agent.voiceId ?? "",
|
|
|
- },
|
|
|
+ agents: [],
|
|
|
+ agent: null,
|
|
|
+ agentProfile: null,
|
|
|
+ agentContactCard: null,
|
|
|
+ defaultAgent: null,
|
|
|
+ agentCharacter: null,
|
|
|
+ ents: [],
|
|
|
});
|
|
|
+ },
|
|
|
+ fetchAgents: async () => {
|
|
|
+ const response = await getAgents();
|
|
|
+ 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) => {
|
|
|
+ const response = await _getMyAgent(agentId);
|
|
|
+ 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;
|
|
|
- }
|
|
|
- set({
|
|
|
- agent: null,
|
|
|
- });
|
|
|
- return null;
|
|
|
- },
|
|
|
- // 请求无需登录的 getAgent 接口
|
|
|
- fetchAgentProfile: async (agentId: string, shareKey?: string) => {
|
|
|
- if (shareKey) {
|
|
|
- shareKey = decodeURIComponent(shareKey);
|
|
|
- }
|
|
|
- const response = await _getAgent(agentId, shareKey);
|
|
|
- const result = isSuccess(response.status);
|
|
|
- const agent = response.data;
|
|
|
- if (result && agent) {
|
|
|
- agent.components = (agent.components ?? []).filter(
|
|
|
- (item) => item.enabled
|
|
|
- );
|
|
|
+ return response.data;
|
|
|
+ }
|
|
|
set({
|
|
|
- agentProfile: agent,
|
|
|
+ agent: null,
|
|
|
});
|
|
|
- return agent;
|
|
|
- }
|
|
|
- return null;
|
|
|
- },
|
|
|
- clearProfileAgent: () => {
|
|
|
- set({
|
|
|
- agentProfile: null,
|
|
|
- });
|
|
|
- },
|
|
|
- setDefaultAgent: async (agentId: string) => {
|
|
|
- const response = await _setDefaultAgent(agentId);
|
|
|
- const result = isSuccess(response.status);
|
|
|
- if (result) {
|
|
|
- const agent = await get().fetchAgent(agentId);
|
|
|
+ return null;
|
|
|
+ },
|
|
|
+ // 请求无需登录的 getAgent 接口
|
|
|
+ fetchAgentProfile: async (agentId: string, shareKey?: string) => {
|
|
|
+ if (shareKey) {
|
|
|
+ shareKey = decodeURIComponent(shareKey);
|
|
|
+ }
|
|
|
+ const response = await _getAgent(agentId, shareKey);
|
|
|
+ const result = isSuccess(response.status);
|
|
|
+ const agent = response.data;
|
|
|
+ if (result && agent) {
|
|
|
+ agent.components = (agent.components ?? []).filter(
|
|
|
+ (item) => item.enabled
|
|
|
+ );
|
|
|
+ set({
|
|
|
+ agentProfile: agent,
|
|
|
+ });
|
|
|
+ return agent;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ },
|
|
|
+ clearProfileAgent: () => {
|
|
|
set({
|
|
|
- defaultAgent: agent,
|
|
|
+ agentProfile: null,
|
|
|
});
|
|
|
- return agent;
|
|
|
- }
|
|
|
- return null;
|
|
|
- },
|
|
|
- // 创建并设置其为默认智能体
|
|
|
- createAgent: async () => {
|
|
|
- const response = await _createAgent();
|
|
|
- const agentDetail = response.data;
|
|
|
- if (agentDetail?.agentId) {
|
|
|
- // 创建新智能体,自动设置为默认智能体
|
|
|
- await get().setDefaultAgent(agentDetail.agentId);
|
|
|
+ },
|
|
|
+ setDefaultAgent: async (agentId: string) => {
|
|
|
+ const response = await _setDefaultAgent(agentId);
|
|
|
+ const result = isSuccess(response.status);
|
|
|
+ if (result) {
|
|
|
+ const agent = await get().actions.fetchAgent(agentId);
|
|
|
+ set({
|
|
|
+ defaultAgent: agent,
|
|
|
+ });
|
|
|
+ return agent;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ },
|
|
|
+ saveAgent: async () => {
|
|
|
+ const { agent, agentTemp } = get();
|
|
|
|
|
|
- const a: TAgent = {
|
|
|
- agentId: agentDetail.agentId ?? "",
|
|
|
- isDefault: true,
|
|
|
- isEnt: agentDetail.isEnt ?? false,
|
|
|
- isNewEnt: agentDetail.isNewEnt ?? false,
|
|
|
- name: agentDetail.name ?? "",
|
|
|
- enabledChatBg: false,
|
|
|
- };
|
|
|
+ // 如果当前agent有agentId,则执行编辑操作
|
|
|
+ if (agent?.agentId) {
|
|
|
+ // 处理null值转换为undefined以符合TAgentRequiredData类型
|
|
|
+ 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,
|
|
|
+ };
|
|
|
+
|
|
|
+ const updateData: TAgentRequiredData = {
|
|
|
+ ...cleanAgent,
|
|
|
+ ...agentTemp,
|
|
|
+ agentId: agent.agentId,
|
|
|
+ };
|
|
|
|
|
|
+ const response = await _editAgent(updateData);
|
|
|
+ const result = isSuccess(response.status);
|
|
|
+
|
|
|
+ if (result && response.data) {
|
|
|
+ // 更新当前agent数据
|
|
|
+ set({ agent: response.data });
|
|
|
+ // 清除临时数据
|
|
|
+ get().actions.clearAgentTemp();
|
|
|
+ // 重新获取agents列表以更新缓存
|
|
|
+ await get().actions.fetchAgents();
|
|
|
+ return response.data;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ // 如果没有agentId,则创建新智能体
|
|
|
+ if (Object.keys(agentTemp).length === 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = await _createNewAgent(agentTemp);
|
|
|
+ const result = isSuccess(response.status);
|
|
|
+
|
|
|
+ if (result && response.data) {
|
|
|
+ const newAgent = await get().actions.fetchAgent(response.data.agentId);
|
|
|
+ if (newAgent) {
|
|
|
+ set((state) => ({
|
|
|
+ agents: [...state.agents, newAgent as TAgent],
|
|
|
+ agent: newAgent,
|
|
|
+ defaultAgent: newAgent,
|
|
|
+ }));
|
|
|
+ // 清除临时数据
|
|
|
+ get().actions.clearAgentTemp();
|
|
|
+ return newAgent;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ updateAgentTemp: (updates: Partial<TAgentRequiredData>) => {
|
|
|
+ set((state) => ({
|
|
|
+ agentTemp: {
|
|
|
+ ...state.agentTemp,
|
|
|
+ ...updates,
|
|
|
+ },
|
|
|
+ }));
|
|
|
+ },
|
|
|
+ clearAgentTemp: () => {
|
|
|
+ set({ agentTemp: {} });
|
|
|
+ },
|
|
|
+ // 编辑声音,人设,开场白,问题引导,知识库
|
|
|
+ // deprecated
|
|
|
+ editAgentCharacter: async (agentId: string, data: TEditAgentCharacter) => {
|
|
|
+ const response = await _editAgentCharacter(agentId, data);
|
|
|
+ console.log(response.data);
|
|
|
+ const result = isSuccess(response.status);
|
|
|
+ //@ts-ignore
|
|
|
set((state) => {
|
|
|
return {
|
|
|
- agents: [...state.agents, a],
|
|
|
- defaultAgent: a,
|
|
|
+ agent: {
|
|
|
+ ...state.agent,
|
|
|
+ voiceId: data.voiceId,
|
|
|
+ },
|
|
|
};
|
|
|
});
|
|
|
- return agentDetail;
|
|
|
- }
|
|
|
- return null;
|
|
|
- },
|
|
|
- updateAgentTemp: (updates: Partial<TAgentTemp>) => {
|
|
|
- set((state) => ({
|
|
|
- agentTemp: {
|
|
|
- ...state.agentTemp,
|
|
|
- ...updates,
|
|
|
- },
|
|
|
- }));
|
|
|
- },
|
|
|
- clearAgentTemp: () => {
|
|
|
- set({ agentTemp: {} });
|
|
|
- },
|
|
|
- // 编辑声音,人设,开场白,问题引导,知识库
|
|
|
- editAgentCharacter: async (agentId: string, data: TEditAgentCharacter) => {
|
|
|
- const response = await _editAgentCharacter(agentId, data);
|
|
|
- console.log(response.data);
|
|
|
- const result = isSuccess(response.status);
|
|
|
- //@ts-ignore
|
|
|
- set((state) => {
|
|
|
- return {
|
|
|
- agent: {
|
|
|
- ...state.agent,
|
|
|
- voiceId: data.voiceId,
|
|
|
- },
|
|
|
- };
|
|
|
- });
|
|
|
- return result;
|
|
|
- },
|
|
|
- editAgentCard: async (agentId: string, data: TAgentContactCard) => {
|
|
|
- console.log(agentId, data);
|
|
|
- const response = await _editAgentCard(agentId, data);
|
|
|
- console.log(response);
|
|
|
- const result = isSuccess(response.status);
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+ // deprecated
|
|
|
+ editAgentCard: async (agentId: string, data: TAgentContactCard) => {
|
|
|
+ console.log(agentId, data);
|
|
|
+ const response = await _editAgentCard(agentId, data);
|
|
|
+ console.log(response);
|
|
|
+ const result = isSuccess(response.status);
|
|
|
|
|
|
- return result;
|
|
|
- },
|
|
|
- editAgentWebsite: async (agentId: string, data: TComponentItem[]) => {
|
|
|
- console.log(agentId, data);
|
|
|
- const response = await _editAgentWebsite(agentId, { components: data });
|
|
|
- console.log(response);
|
|
|
- const result = isSuccess(response.status);
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+ editAgentWebsite: async (agentId: string, data: TComponentItem[]) => {
|
|
|
+ console.log(agentId, data);
|
|
|
+ const response = await _editAgentWebsite(agentId, { components: data });
|
|
|
+ console.log(response);
|
|
|
+ const result = isSuccess(response.status);
|
|
|
|
|
|
- return result;
|
|
|
- },
|
|
|
- deleteAgent: async (agentId: string) => {
|
|
|
- const response = await _deleteAgent(agentId);
|
|
|
- if (isSuccess(response.status)) {
|
|
|
- const de = get()
|
|
|
- .agents.filter((item: TAgent) => item.agentId !== agentId)
|
|
|
- .reverse()[0];
|
|
|
- console.log(agentId, de, "setDefault");
|
|
|
- // 默认设置自创的智能体
|
|
|
- if (de) {
|
|
|
- await get().setDefaultAgent(de.agentId);
|
|
|
- }
|
|
|
+ return result;
|
|
|
+ },
|
|
|
|
|
|
- // 重新拉取智能体列表
|
|
|
- const restAgents = await get().fetchAgents();
|
|
|
- if (restAgents.length <= 0) {
|
|
|
- set({ agent: null, defaultAgent: null });
|
|
|
- Taro.reLaunch({ url: "/pages/index/index" });
|
|
|
- return;
|
|
|
- }
|
|
|
+ deleteAgent: async (agentId: string) => {
|
|
|
+ const response = await _deleteAgent(agentId);
|
|
|
+ if (isSuccess(response.status)) {
|
|
|
+ const de = get()
|
|
|
+ .agents.filter((item: TAgent) => item.agentId !== agentId)
|
|
|
+ .reverse()[0];
|
|
|
+ console.log(agentId, de, "setDefault");
|
|
|
+ // 默认设置自创的智能体
|
|
|
+ if (de) {
|
|
|
+ await get().actions.setDefaultAgent(de.agentId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重新拉取智能体列表
|
|
|
+ const restAgents = await get().actions.fetchAgents();
|
|
|
+ if (restAgents.length <= 0) {
|
|
|
+ set({ agent: null, defaultAgent: null });
|
|
|
+ Taro.reLaunch({ url: "/pages/index/index" });
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- const defaultAgent = restAgents.find((item) => !!item.isDefault);
|
|
|
- if (defaultAgent) {
|
|
|
- await get().fetchAgent(defaultAgent.agentId);
|
|
|
+ const defaultAgent = restAgents.find((item) => !!item.isDefault);
|
|
|
+ if (defaultAgent) {
|
|
|
+ await get().actions.fetchAgent(defaultAgent.agentId);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- },
|
|
|
+ },
|
|
|
+ clearMyAgent: () => {
|
|
|
+ set({
|
|
|
+ agent: null,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ }
|
|
|
}));
|
|
|
+
|
|
|
+export const useAgentStoreActions = () => {
|
|
|
+ const actions = useAgentStore((state) => state.actions);
|
|
|
+ return actions;
|
|
|
+};
|