utils.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { v4 as uuid4 } from "uuid";
  2. // 模板字符串装饰器
  3. // @See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
  4. export function template(strings, ...keys) {
  5. return (...values) => {
  6. const dict = values[values.length - 1] || {};
  7. const result = [strings[0]];
  8. keys.forEach((key, i) => {
  9. const value = Number.isInteger(key) ? values[key] : dict[key];
  10. result.push(value, strings[i + 1]);
  11. });
  12. return result.join("");
  13. };
  14. }
  15. export function getChatResponse({
  16. text,
  17. assistantId,
  18. userId,
  19. sessionName,
  20. model = "gpt-4o",
  21. }) {
  22. if (!sessionName) {
  23. sessionName = uuid4();
  24. }
  25. const data = {
  26. assistant_id: assistantId,
  27. userId,
  28. message: text,
  29. session_name: sessionName,
  30. model,
  31. };
  32. const uid = uuid4();
  33. const ctrl = new AbortController();
  34. async function* generator() {
  35. const queue = [];
  36. let resolveQueuePromise: (value?: unknown) => void;
  37. let queuePromise = new Promise(
  38. (resolve) => (resolveQueuePromise = resolve)
  39. );
  40. let done = false;
  41. let error;
  42. let timer;
  43. // 创建 EventSource 实例
  44. const eventSource = new EventSource(
  45. `${process.env.NEXT_PUBLIC_GPT_BASE_URL}/question/${uid}`
  46. );
  47. // 处理消息事件
  48. eventSource.onmessage = (ev) => {
  49. const content = JSON.parse(ev.data)?.content;
  50. if (content === "[DONE]") {
  51. done = true;
  52. resolveQueuePromise?.();
  53. eventSource.close(); // 关闭连接
  54. return;
  55. }
  56. queue.push(content);
  57. resolveQueuePromise();
  58. queuePromise = new Promise((resolve) => (resolveQueuePromise = resolve));
  59. };
  60. // 处理错误事件
  61. eventSource.onerror = () => {
  62. done = true;
  63. resolveQueuePromise();
  64. eventSource.close(); // 关闭连接
  65. };
  66. ctrl.signal.addEventListener("abort", () => {
  67. done = true;
  68. resolveQueuePromise();
  69. eventSource.close();
  70. });
  71. fetch(
  72. `${process.env.NEXT_PUBLIC_GPT_BASE_URL}/ai_agent_park_chat_new`,
  73. {
  74. method: "POST",
  75. body: JSON.stringify({ ...data, uid }),
  76. headers: {
  77. "Content-Type": "application/json",
  78. },
  79. }
  80. );
  81. while (!done || queue.length > 0) {
  82. if (error) {
  83. throw error;
  84. }
  85. clearTimeout(timer);
  86. timer = setTimeout(() => {
  87. ctrl.abort();
  88. error = new Error("SSE timeout aborted.");
  89. resolveQueuePromise();
  90. }, 30000);
  91. if (queue.length > 0) {
  92. yield queue.shift();
  93. } else {
  94. await queuePromise;
  95. }
  96. }
  97. clearTimeout(timer);
  98. }
  99. return [generator(), ctrl];
  100. }