ai.router.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const Router = require('@koa/router');
  2. const ai = require('../controller/ai.controller');
  3. const router = new Router({ prefix: '/ai' });
  4. const request = require('../utils/request');
  5. const util = require('../utils/util');
  6. const qs = require('qs');
  7. const bodyParser = require('koa-bodyparser');
  8. const { chatStream } = require('../controller/ai.controller');
  9. /**
  10. * 接口代理和大模型接入API接口
  11. */
  12. // 流式chat
  13. router.post('/stream', bodyParser(), chatStream);
  14. // 接口转发(get)
  15. router.get('/proxy', async (ctx) => handleProxy(ctx, 'get'));
  16. // 接口转发(post)
  17. router.post('/proxy', async (ctx) => handleProxy(ctx, 'post'));
  18. // 接口转发(put)
  19. router.put('/proxy', async (ctx) => handleProxy(ctx, 'put'));
  20. // 接口转发(patch)
  21. router.patch('/proxy', async (ctx) => handleProxy(ctx, 'patch'));
  22. // 接口转发(delete)
  23. router.delete('/proxy', async (ctx) => handleProxy(ctx, 'delete'));
  24. const handleProxy = async (ctx, method) => {
  25. try {
  26. const {
  27. query,
  28. body,
  29. headers: { host, origin, proxyapi, ...headers },
  30. } = ctx.request;
  31. if (!proxyapi) {
  32. return util.fail(ctx, '接口请求地址不存在', 404);
  33. }
  34. let params = method === 'get' || method === 'delete' ? query : body;
  35. if (headers['content-type'] === 'application/x-www-form-urlencoded') {
  36. params = qs.stringify(params);
  37. } else {
  38. params = method === 'get' || method === 'delete' ? query : JSON.stringify(params || {});
  39. }
  40. let response = null;
  41. if (method === 'get' || method === 'delete') {
  42. response = await request[method](proxyapi, {
  43. params,
  44. headers,
  45. });
  46. } else {
  47. response = await request[method](proxyapi, params, {
  48. headers,
  49. });
  50. }
  51. const { status, statusText, data } = response;
  52. for (let key in response.headers) {
  53. ctx.set(key, response.headers[key]);
  54. }
  55. if (status === 200) {
  56. ctx.body = data;
  57. } else {
  58. if (status === 401 || status === 405) {
  59. return util.fail(ctx, '接口请求方法错误', status);
  60. }
  61. if (status === 404) {
  62. return util.fail(ctx, '接口请求地址不存在', status);
  63. }
  64. util.fail(ctx, data || statusText, 500);
  65. }
  66. } catch (error) {
  67. // 根据错误类型设置状态码和错误信息
  68. if (error.code === 'ECONNREFUSED' || error.code === 'ENOTFOUND') {
  69. // 服务地址不存在或无法连接
  70. ctx.status = 502; // Bad Gateway
  71. ctx.body = {
  72. message: '服务不可用或地址不存在',
  73. error: error.message,
  74. };
  75. } else if (error.response) {
  76. // 如果错误是来自后端接口的响应
  77. const { status, data } = error.response;
  78. ctx.status = status; // 设置状态码
  79. ctx.body = data; // 设置响应体
  80. } else if (error.request) {
  81. // 请求已发出,但没有收到响应
  82. ctx.status = 504; // Gateway Timeout
  83. ctx.body = {
  84. message: '请求超时,未收到响应',
  85. error: error.message,
  86. };
  87. } else {
  88. // 其他未知错误
  89. ctx.status = 500; // Internal Server Error
  90. ctx.body = {
  91. message: '服务器内部错误',
  92. error: error.message,
  93. };
  94. }
  95. }
  96. };
  97. module.exports = router;