routes-indexes.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. const { IndexesService } = require('./indexes.service');
  3. const router = require('express').Router();
  4. /**
  5. * 存活统计:用户存活情况统计接口
  6. * @see
  7. */
  8. /**
  9. * @api {get} /aiagent/indexes/loader AGENT:文件加载器
  10. * @apiSampleRequest /api/aiagent/indexes/loader
  11. * @apiVersion 0.5.0
  12. * @apiName agentIndexesLoader
  13. * @apiGroup apig
  14. * @apiParam {string} [story] 故事ID
  15. * @apiParam {string} [attach] 附件ID
  16. * @apiParam {string} [url] 可访问文件地址
  17. * @apiParam {string} [md5] 当前文件MD5校验码
  18. * @apiParam {string} [extend] 文件后缀名
  19. * @apiSuccess {data} data 结果
  20. * @apiSuccessExample Success-Response:
  21. * HTTP/11 200 OK
  22. * {
  23. * code:200,
  24. * data:[
  25. * docsIdList:["xxxxxxxxx1","xxxxxxxxx2","xxxxxxxxx3","xxxxxxxxx4"],
  26. * docs:[{pageContent:"",metadata:""}]
  27. * ]
  28. * }
  29. */
  30. router.post("/loader",async (request,response)=>{
  31. // 仅URL直接返回分割结果
  32. let url = request.body.url || request.params.url || request.query.url
  33. let md5 = request.body.md5 || request.params.md5 || request.query.md5
  34. let extend = request.body.extend || request.params.extend || request.query.extend
  35. if(!url){
  36. response.status(500)
  37. response.json({
  38. code:500,
  39. error:"请传入参数:url,md5"
  40. })
  41. return
  42. }
  43. // 含Story存储Document后,返回分割文档ID
  44. let story = request.body.story || request.params.story || request.query.story
  45. let attach = request.body.attach || request.params.attach || request.query.attach
  46. // 加载Indexes,开始分割文档
  47. let indexes = new IndexesService();
  48. let docs
  49. let splitDocs
  50. try{
  51. docs = await indexes.load(url,extend,md5);
  52. let splitCount = 0
  53. if(docs?.length>0){
  54. splitDocs = await indexes.split(docs)
  55. splitCount = splitDocs?.length
  56. }
  57. response.json({
  58. code:200,
  59. data:{
  60. doc:docs?.[0],
  61. split:splitDocs,
  62. docsIdList:[],
  63. splitCount:splitCount
  64. }
  65. })
  66. }catch(err){
  67. console.error(err)
  68. response.status(500)
  69. response.json({
  70. code:500,
  71. err:err
  72. })
  73. }
  74. })
  75. module.exports = router;