create_app_logic.go 12 KB

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