create_app_logic.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package fastgpt
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "strconv"
  7. "time"
  8. apps "wechat-api/mongo_model/apps"
  9. team_members "wechat-api/mongo_model/team_members"
  10. users "wechat-api/mongo_model/users"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type CreateAppLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewCreateAppLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAppLogic {
  21. return &CreateAppLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx}
  25. }
  26. func (l *CreateAppLogic) CreateApp(req *types.CreateAppsReq) (resp *types.BaseMsgResp, err error) {
  27. organizationId := l.ctx.Value("organizationId").(uint64)
  28. organizationIdStr := strconv.FormatUint(organizationId, 10)
  29. usersModel := users.NewUsersModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "users")
  30. user, err := usersModel.FindOneByUsername(context.TODO(), organizationIdStr)
  31. if err != nil {
  32. return nil, errorx.NewInvalidArgumentError("fastgpt get list failed " + err.Error())
  33. }
  34. teamMembersModel := team_members.NewTeamMembersModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "team_members")
  35. teamMember, err := teamMembersModel.FindOneByUserId(context.TODO(), user.ID)
  36. if err != nil {
  37. return nil, errorx.NewInvalidArgumentError("fastgpt get list failed " + err.Error())
  38. }
  39. intro := ""
  40. if req.Intro != nil {
  41. intro = *req.Intro
  42. }
  43. var apps_info *apps.Apps
  44. if req.Type == "simple" {
  45. // 创建默认智能体
  46. apps_info = &apps.Apps{
  47. ParentID: nil,
  48. TeamID: teamMember.TeamID,
  49. TmbID: teamMember.ID,
  50. Name: req.Name,
  51. Type: "simple",
  52. Version: "v2",
  53. Avatar: "/imgs/app/avatar/simple.svg",
  54. Intro: intro,
  55. TeamTags: []string{},
  56. Modules: []apps.AppModule{
  57. {
  58. NodeID: "userGuide",
  59. Name: "系统配置",
  60. Intro: "",
  61. FlowNodeType: "userGuide",
  62. Position: apps.Position{
  63. X: 531.242273606555,
  64. Y: -486.761172954975,
  65. },
  66. Version: "481",
  67. Inputs: []apps.AppInput{},
  68. Outputs: []apps.AppOutput{},
  69. },
  70. {
  71. NodeID: "workflowStartNodeId",
  72. Name: "流程开始",
  73. Intro: "",
  74. Avatar: "core/workflow/template/workflowStart",
  75. FlowNodeType: "workflowStart",
  76. Position: apps.Position{
  77. X: 558.40823764155,
  78. Y: 123.723874291941,
  79. },
  80. Version: "481",
  81. Inputs: []apps.AppInput{
  82. {
  83. Key: "userChatInput",
  84. RenderTypeList: []string{"reference", "textarea"},
  85. ValueType: "string",
  86. Label: "workflow:user_question",
  87. ToolDescription: "workflow:user_question",
  88. Required: true,
  89. },
  90. },
  91. Outputs: []apps.AppOutput{
  92. {
  93. ID: "userChatInput",
  94. Key: "userChatInput",
  95. Label: "common:core.module.input.label.user question",
  96. Type: "static",
  97. ValueType: "string",
  98. },
  99. {
  100. ID: "userFiles",
  101. Key: "userFiles",
  102. Label: "app:workflow.user_file_input",
  103. Description: "app:workflow.user_file_input_desc",
  104. Type: "static",
  105. ValueType: "arrayString",
  106. },
  107. },
  108. },
  109. {
  110. NodeID: "7BdojPlukIQw",
  111. Name: "AI 对话",
  112. Intro: "AI 大模型对话",
  113. Avatar: "core/workflow/template/aiChat",
  114. FlowNodeType: "chatNode",
  115. ShowStatus: true,
  116. Position: apps.Position{
  117. X: 1106.32383879608,
  118. Y: -350.603067468347,
  119. },
  120. Version: "4813",
  121. Inputs: []apps.AppInput{
  122. {
  123. Key: "model",
  124. RenderTypeList: []string{"settingLLMModel", "reference"},
  125. ValueType: "string",
  126. Value: "DeepSeek-V3",
  127. },
  128. {
  129. Key: "temperature",
  130. RenderTypeList: []string{"hidden"},
  131. ValueType: "number",
  132. Value: int32(0),
  133. Min: getInt32(0),
  134. Max: getInt32(10),
  135. Step: getInt32(1),
  136. },
  137. {
  138. Key: "maxToken",
  139. RenderTypeList: []string{"hidden"},
  140. ValueType: "number",
  141. Value: int32(2000),
  142. Min: getInt32(100),
  143. Max: getInt32(4000),
  144. Step: getInt32(50),
  145. },
  146. {
  147. Key: "isResponseAnswerText",
  148. RenderTypeList: []string{"hidden"},
  149. ValueType: "boolean",
  150. Value: true,
  151. },
  152. {
  153. Key: "aiChatQuoteRole",
  154. RenderTypeList: []string{"hidden"},
  155. ValueType: "string",
  156. Value: "system",
  157. },
  158. {
  159. Key: "quoteTemplate",
  160. RenderTypeList: []string{"hidden"},
  161. ValueType: "string",
  162. },
  163. {
  164. Key: "quotePrompt",
  165. RenderTypeList: []string{"hidden"},
  166. ValueType: "string",
  167. },
  168. {
  169. Key: "systemPrompt",
  170. RenderTypeList: []string{"textarea", "reference"},
  171. Max: getInt32(3000),
  172. ValueType: "string",
  173. Label: "core.ai.Prompt",
  174. Description: "core.app.tip.systemPromptTip",
  175. Placeholder: "core.app.tip.chatNodeSystemPromptTip",
  176. Value: "",
  177. },
  178. {
  179. Key: "history",
  180. RenderTypeList: []string{"numberInput", "reference"},
  181. ValueType: "chatHistory",
  182. Label: "core.module.input.label.chat history",
  183. Required: true,
  184. Min: getInt32(0),
  185. Max: getInt32(30),
  186. Value: int32(6),
  187. },
  188. {
  189. Key: "userChatInput",
  190. RenderTypeList: []string{"reference", "textarea"},
  191. ValueType: "string",
  192. Label: "common:core.module.input.label.user question",
  193. Required: true,
  194. ToolDescription: "common:core.module.input.label.user question",
  195. Value: []interface{}{"workflowStartNodeId", "userChatInput"},
  196. },
  197. {
  198. Key: "quoteQA",
  199. RenderTypeList: []string{"settingDatasetQuotePrompt"},
  200. Label: "",
  201. DebugLabel: "common:core.module.Dataset quote.label",
  202. Description: "",
  203. ValueType: "datasetQuote",
  204. },
  205. {
  206. Key: "fileUrlList",
  207. RenderTypeList: []string{"reference", "input"},
  208. Label: "app:file_quote_link",
  209. DebugLabel: "app:file_quote_link",
  210. ValueType: "arrayString",
  211. Value: [][]interface{}{{"workflowStartNodeId", "userFiles"}},
  212. },
  213. {
  214. Key: "aiChatVision",
  215. RenderTypeList: []string{"hidden"},
  216. ValueType: "boolean",
  217. Value: true,
  218. },
  219. },
  220. Outputs: []apps.AppOutput{
  221. {
  222. ID: "history",
  223. Key: "history",
  224. Required: true,
  225. Label: "common:core.module.output.label.New context",
  226. Description: "common:core.module.output.description.New context",
  227. ValueType: "chatHistory",
  228. ValueDesc: "{\n obj: System | Human | AI;\n value: string;\n}[]",
  229. Type: "static",
  230. },
  231. {
  232. ID: "answerText",
  233. Key: "answerText",
  234. Required: true,
  235. Label: "common:core.module.output.label.Ai response content",
  236. Description: "common:core.module.output.description.Ai response content",
  237. ValueType: "string",
  238. Type: "static",
  239. },
  240. },
  241. },
  242. },
  243. Edges: []apps.Edge{
  244. {
  245. Source: "workflowStartNodeId",
  246. Target: "7BdojPlukIQw",
  247. SourceHandle: "workflowStartNodeId-source-right",
  248. TargetHandle: "7BdojPlukIQw-target-left",
  249. },
  250. },
  251. PluginData: apps.PluginData{
  252. ID: mustParseObjectID("67da46b29667c5bf21203554"),
  253. NodeVersion: "67da46d29667c5bf2120361a",
  254. },
  255. InheritPermission: true,
  256. VersionNumber: int32(0),
  257. ChatConfig: apps.ChatConfig{
  258. WelcomeText: "",
  259. Variables: []interface{}{},
  260. QuestionGuide: false,
  261. TTSConfig: apps.TTSConfig{
  262. Type: "web",
  263. },
  264. WhisperConfig: apps.WhisperConfig{
  265. Open: false,
  266. AutoSend: false,
  267. AutoTTSResponse: false,
  268. },
  269. ScheduledTriggerConfig: nil,
  270. ChatInputGuide: apps.ChatInputGuide{
  271. Open: false,
  272. TextList: []string{},
  273. CustomUrl: "",
  274. },
  275. Instruction: "",
  276. ID: mustParseObjectID("67da46d29667c5bf2120361d"),
  277. },
  278. UpdateTime: time.Date(2025, 3, 19, 4, 24, 4, 394000000, time.UTC),
  279. ScheduledTriggerConfig: nil,
  280. ScheduledTriggerNextTime: nil,
  281. }
  282. } else {
  283. apps_info = &apps.Apps{
  284. ParentID: nil,
  285. TeamID: teamMember.TeamID,
  286. TmbID: teamMember.ID,
  287. Name: req.Name,
  288. Type: "advanced",
  289. Version: "v2",
  290. Avatar: "/imgs/app/avatar/workflow.svg",
  291. Intro: intro,
  292. TeamTags: []string{},
  293. Modules: []apps.AppModule{
  294. {
  295. NodeID: "userGuide",
  296. Name: "common:core.module.template.system_config",
  297. Intro: "common:core.module.template.system_config_info",
  298. Avatar: "core/workflow/template/systemConfig",
  299. FlowNodeType: "userGuide",
  300. Position: apps.Position{
  301. X: 262.273233881709,
  302. Y: -476.002411365981,
  303. },
  304. Version: "481",
  305. Inputs: []apps.AppInput{
  306. {
  307. Key: "welcomeText",
  308. RenderTypeList: []string{"hidden"},
  309. ValueType: "string",
  310. Label: "core.app.Welcome Text",
  311. Value: "",
  312. }, {
  313. Key: "variables",
  314. RenderTypeList: []string{"hidden"},
  315. ValueType: "any",
  316. Label: "core.app.Chat Variable",
  317. Value: []string{},
  318. }, {
  319. Key: "questionGuide",
  320. RenderTypeList: []string{"hidden"},
  321. ValueType: "boolean",
  322. Label: "core.app.Question Guide",
  323. Value: false,
  324. }, {
  325. Key: "tts",
  326. RenderTypeList: []string{"hidden"},
  327. ValueType: "any",
  328. Label: "",
  329. Value: apps.AppInputValue{
  330. Type: "web",
  331. },
  332. }, {
  333. Key: "whisper",
  334. RenderTypeList: []string{"hidden"},
  335. ValueType: "any",
  336. Label: "",
  337. Value: apps.AppInputValue{
  338. Open: false,
  339. AutoSend: false,
  340. AutoTTSResponse: false,
  341. },
  342. }, {
  343. Key: "scheduleTrigger",
  344. RenderTypeList: []string{"hidden"},
  345. ValueType: "any",
  346. Label: "",
  347. },
  348. },
  349. Outputs: []apps.AppOutput{},
  350. },
  351. {
  352. NodeID: "448745",
  353. Name: "common:core.module.template.work_start",
  354. Intro: "",
  355. Avatar: "core/workflow/template/workflowStart",
  356. FlowNodeType: "workflowStart",
  357. Position: apps.Position{
  358. X: 632.368838596004,
  359. Y: -347.744649294401,
  360. },
  361. Version: "481",
  362. Inputs: []apps.AppInput{
  363. {
  364. Key: "userChatInput",
  365. RenderTypeList: []string{"reference", "textarea"},
  366. ValueType: "string",
  367. Label: "common:core.module.input.label.user question",
  368. ToolDescription: "common:core.module.input.label.user question",
  369. Required: true,
  370. },
  371. },
  372. Outputs: []apps.AppOutput{
  373. {
  374. ID: "userChatInput",
  375. Key: "userChatInput",
  376. Label: "common:core.module.input.label.user question",
  377. Type: "static",
  378. ValueType: "string",
  379. },
  380. },
  381. },
  382. },
  383. Edges: []apps.Edge{},
  384. PluginData: apps.PluginData{
  385. ID: mustParseObjectID("67dce247bd93cb6e085a6bda"),
  386. NodeVersion: "481",
  387. },
  388. InheritPermission: true,
  389. VersionNumber: int32(0),
  390. UpdateTime: time.Date(2025, 3, 19, 4, 24, 4, 394000000, time.UTC),
  391. }
  392. }
  393. if apps_info != nil {
  394. appsModel := apps.NewAppsModel(l.svcCtx.Config.FastgptMongoConf.Url, l.svcCtx.Config.FastgptMongoConf.DBName, "apps")
  395. err = appsModel.Insert(context.TODO(), apps_info)
  396. if err != nil {
  397. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  398. }
  399. } else {
  400. return nil, errorx.NewInvalidArgumentError("fastgpt create failed ")
  401. }
  402. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  403. }