|
@@ -1,27 +1,30 @@
|
|
|
package com.bosshand.virgo.api.workark.controller;
|
|
|
|
|
|
-import com.bosshand.virgo.api.workark.model.DifyCompletion;
|
|
|
-import com.bosshand.virgo.api.workark.model.DifyType;
|
|
|
-import com.bosshand.virgo.api.workark.model.DifyWorkFlow;
|
|
|
-import com.bosshand.virgo.api.workark.model.HtmlCode;
|
|
|
+import com.bosshand.virgo.api.workark.model.*;
|
|
|
+import com.bosshand.virgo.api.workark.service.DifyDatasetService;
|
|
|
import com.bosshand.virgo.api.workark.service.DifyService;
|
|
|
import com.bosshand.virgo.core.response.Response;
|
|
|
-import io.swagger.annotations.Api;
|
|
|
-import io.swagger.annotations.ApiOperation;
|
|
|
+import com.bosshand.virgo.exception.Constant;
|
|
|
+import io.swagger.annotations.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping({"ai"})
|
|
|
-@Api(tags = {"dify"})
|
|
|
+@Api(tags = {"dify平台接口管理"})
|
|
|
public class DifyController {
|
|
|
|
|
|
@Autowired
|
|
|
DifyService difyService;
|
|
|
|
|
|
- @ApiOperation("保存dify类型")
|
|
|
+ @ApiOperation("新增dify类型")
|
|
|
@RequestMapping(value = "/type", method = RequestMethod.POST)
|
|
|
public Response insertType(@RequestBody DifyType difyType) {
|
|
|
difyService.saveType(difyType);
|
|
@@ -41,7 +44,7 @@ public class DifyController {
|
|
|
return Response.ok(difyService.getListType());
|
|
|
}
|
|
|
|
|
|
- @ApiOperation("保存HtmlCode")
|
|
|
+ @ApiOperation("新增HtmlCode")
|
|
|
@RequestMapping(value = "/htmlCode", method = RequestMethod.POST)
|
|
|
public Response insertHtmlCode(@RequestBody HtmlCode htmlCode) {
|
|
|
return Response.ok(difyService.saveHtmlCode(htmlCode));
|
|
@@ -55,6 +58,9 @@ public class DifyController {
|
|
|
}
|
|
|
|
|
|
@ApiOperation("发布HtmlCode")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "id", value = "HtmlCode记录id")
|
|
|
+ })
|
|
|
@RequestMapping(value = "/htmlCode/publish/{id}", method = RequestMethod.GET)
|
|
|
public Response publishHtmlCode(@PathVariable long id) {
|
|
|
String simpleUUID = difyService.publishHtmlCode(id);
|
|
@@ -67,19 +73,28 @@ public class DifyController {
|
|
|
return Response.ok(difyService.getListHtmlCode(htmlCode));
|
|
|
}
|
|
|
|
|
|
- @ApiOperation("HtmlCode详情")
|
|
|
+ @ApiOperation("获取HtmlCode详情")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "id", value = "HtmlCode记录id")
|
|
|
+ })
|
|
|
@RequestMapping(value = "/htmlCode/{id}", method = RequestMethod.GET)
|
|
|
public Response getHtmlCode(@PathVariable long id) {
|
|
|
return Response.ok(difyService.getHtmlCode(id));
|
|
|
}
|
|
|
|
|
|
@ApiOperation("执行workflow")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "difyTypeId", value = "dify类型id")
|
|
|
+ })
|
|
|
@RequestMapping(value = "/workflow/run/{difyTypeId}", method = RequestMethod.POST)
|
|
|
public Response workFlowRun(@PathVariable long difyTypeId, @RequestBody Map<String, Object> inputs) {
|
|
|
return Response.ok(difyService.workFlowRun(difyTypeId, inputs));
|
|
|
}
|
|
|
|
|
|
@ApiOperation("获取workflow执行情况")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "simpleUUID", value = "workflow自动生成的UUID")
|
|
|
+ })
|
|
|
@RequestMapping(value = "/workflow/{simpleUUID}", method = RequestMethod.GET)
|
|
|
public Response getWorkFlow(@PathVariable String simpleUUID) {
|
|
|
return Response.ok(difyService.getWorkFlow(simpleUUID));
|
|
@@ -93,12 +108,18 @@ public class DifyController {
|
|
|
|
|
|
@ApiOperation("执行completion")
|
|
|
@RequestMapping(value = "/completion/run/{difyTypeId}", method = RequestMethod.POST)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "difyTypeId", value = "dify类型id")
|
|
|
+ })
|
|
|
public Response completionRun(@PathVariable long difyTypeId, @RequestBody Map<String, Object> inputs) {
|
|
|
return Response.ok(difyService.completionRun(difyTypeId, inputs));
|
|
|
}
|
|
|
|
|
|
@ApiOperation("获取completion执行情况")
|
|
|
@RequestMapping(value = "/completion/{simpleUUID}", method = RequestMethod.GET)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "simpleUUID", value = "completion自动生成的UUID")
|
|
|
+ })
|
|
|
public Response getCompletion(@PathVariable String simpleUUID) {
|
|
|
return Response.ok(difyService.getCompletion(simpleUUID));
|
|
|
}
|
|
@@ -111,23 +132,35 @@ public class DifyController {
|
|
|
|
|
|
@ApiOperation("执行对话型Chat")
|
|
|
@RequestMapping(value = "/chat/run/{difyTypeId}", method = RequestMethod.POST)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "difyTypeId", value = "dify类型id")
|
|
|
+ })
|
|
|
public Response chatRun(@PathVariable long difyTypeId, @RequestBody Map<String, Object> inputs) {
|
|
|
return Response.ok(difyService.chatRun(difyTypeId, inputs));
|
|
|
}
|
|
|
|
|
|
@ApiOperation("获取对话型Chat执行情况")
|
|
|
@RequestMapping(value = "/chat/{simpleUUID}", method = RequestMethod.GET)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "simpleUUID", value = "chat自动生成的UUID")
|
|
|
+ })
|
|
|
public Response getChat(@PathVariable String simpleUUID) {
|
|
|
return Response.ok(difyService.getChat(simpleUUID));
|
|
|
}
|
|
|
|
|
|
@ApiOperation("获取会话历史消息")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "conversationId", value = "chat会话id")
|
|
|
+ })
|
|
|
@RequestMapping(value = "/chat/message/{conversationId}", method = RequestMethod.GET)
|
|
|
public Response chatMessageList(@PathVariable String conversationId) {
|
|
|
return Response.ok(difyService.messageList(conversationId));
|
|
|
}
|
|
|
|
|
|
@ApiOperation("获取会话列表")
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "difyTypeId", value = "dify类型id")
|
|
|
+ })
|
|
|
@RequestMapping(value = "/chat/conversations/{difyTypeId}", method = RequestMethod.GET)
|
|
|
public Response chatConversations(@PathVariable long difyTypeId) {
|
|
|
return Response.ok(difyService.conversations(difyTypeId));
|
|
@@ -135,10 +168,94 @@ public class DifyController {
|
|
|
|
|
|
@ApiOperation("删除会话")
|
|
|
@RequestMapping(value = "/chat/conversation/{conversationId}", method = RequestMethod.DELETE)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "conversationId", value = "chat会话id")
|
|
|
+ })
|
|
|
public Response deleteConversation(@PathVariable String conversationId) {
|
|
|
difyService.deleteConversation(conversationId);
|
|
|
return Response.ok();
|
|
|
}
|
|
|
|
|
|
+ @Autowired
|
|
|
+ DifyDatasetService difyDatasetService;
|
|
|
+
|
|
|
+ @ApiOperation("获取知识库")
|
|
|
+ @RequestMapping(value = "/datasets/list", method = RequestMethod.POST)
|
|
|
+ public Response datasetList(@RequestBody DifyDataset difyDataset) {
|
|
|
+ return Response.ok(difyDatasetService.getList(difyDataset));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("创建知识库")
|
|
|
+ @RequestMapping(value = "/dataset", method = RequestMethod.POST)
|
|
|
+ public Response createDataset(@RequestBody DifyDataset difyDataset) {
|
|
|
+ difyDatasetService.createDataset(difyDataset);
|
|
|
+ return Response.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("删除知识库")
|
|
|
+ @RequestMapping(value = "/datasets/{datasetId}", method = RequestMethod.DELETE)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "datasetId", value = "知识库id")
|
|
|
+ })
|
|
|
+ public Response tearDown(@PathVariable String datasetId) {
|
|
|
+ difyDatasetService.tearDown(datasetId);
|
|
|
+ return Response.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("获取文档列表")
|
|
|
+ @RequestMapping(value = "/datasets/file/{datasetId}", method = RequestMethod.GET)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "datasetId", value = "知识库id")
|
|
|
+ })
|
|
|
+ public Response getFileList(@PathVariable String datasetId) {
|
|
|
+ return Response.ok(difyDatasetService.getDocument(datasetId));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation("删除文档")
|
|
|
+ @RequestMapping(value = "/datasets/file/{documentId}", method = RequestMethod.DELETE)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "documentId", value = "文档id")
|
|
|
+ })
|
|
|
+ public Response deleteFile(@PathVariable String documentId) {
|
|
|
+ difyDatasetService.deleteDocument(documentId);
|
|
|
+ return Response.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final List<String> ALLOWED_TYPES = Arrays.asList("txt", "markdown", "md", "mdx", "pdf", "html", "xlsx", "xls", "docx", "csv", "vtt", "properties", "htm");
|
|
|
+
|
|
|
+ @ApiOperation("新增文档")
|
|
|
+ @RequestMapping(value = "/dataset/file/{datasetId}", method = RequestMethod.POST)
|
|
|
+ @ApiImplicitParams({
|
|
|
+ @ApiImplicitParam(name = "datasetId", value = "知识库id")
|
|
|
+ })
|
|
|
+ public Response createDocumentByFile(@ApiParam(name = "file", required = true) MultipartFile file, @PathVariable String datasetId) {
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
|
|
|
+ // 验证文件类型
|
|
|
+ if (!ALLOWED_TYPES.contains(suffix)) {
|
|
|
+ return Response.fail(Constant.CODE_BAD_REQUEST, "文件格式不支持");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证文件大小(15MB限制)
|
|
|
+ if (file.getSize() > 15 * 1024 * 1024) {
|
|
|
+ return Response.fail(Constant.CODE_BAD_REQUEST, "文件大小不能超过15MB");
|
|
|
+ }
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = file.getInputStream();
|
|
|
+ difyDatasetService.createDocumentByFile(inputStream, file.getOriginalFilename(), datasetId);
|
|
|
+ return Response.ok();
|
|
|
+ } catch (IOException e) {
|
|
|
+ return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR);
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|