utils.ts 2.7 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 = (e) => {
  62. error = e;
  63. resolveQueuePromise();
  64. eventSource.close(); // 关闭连接
  65. };
  66. ctrl.signal.addEventListener("abort", (e) => {
  67. if (e?.target?.reason) {
  68. error = new Error(e.target.reason);
  69. } else {
  70. done = true;
  71. }
  72. resolveQueuePromise();
  73. eventSource.close();
  74. });
  75. fetch(`${process.env.NEXT_PUBLIC_GPT_BASE_URL}/ai_agent_park_chat_new`, {
  76. method: "POST",
  77. body: JSON.stringify({ ...data, uid }),
  78. headers: {
  79. "Content-Type": "application/json",
  80. },
  81. });
  82. while (!done || queue.length > 0) {
  83. clearTimeout(timer);
  84. if (error) {
  85. throw error;
  86. }
  87. // every looping stream, we wait for 10s
  88. timer = setTimeout(() => {
  89. ctrl.abort("SSE timeout aborted.");
  90. }, 10000);
  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. }