st-comp 0.0.251 → 0.0.253

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/aiTools.js CHANGED
@@ -30,40 +30,80 @@ export const sendToBaiLianAppNonStreaming = async ({ appId, apiKey, value }) =>
30
30
  // 流式返回
31
31
  export const sendToBaiLianAppStreaming = async ({ appId, apiKey, value, callback }) => {
32
32
  try {
33
- const res = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
33
+ const response = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
34
34
  method: "POST",
35
35
  body: JSON.stringify({
36
36
  input: { prompt: value },
37
- parameters: {
38
- incremental_output: "true", // 流式返回
39
- },
37
+ parameters: { incremental_output: "true" },
40
38
  debug: {},
41
39
  }),
42
40
  headers: {
43
41
  Authorization: `Bearer ${apiKey}`,
44
42
  "Content-Type": "application/json",
45
- "X-DashScope-SSE": "enable", // 流式输出
43
+ "X-DashScope-SSE": "enable",
46
44
  },
47
45
  });
48
- if (!res.ok || !res.body) throw new Error("请求失败");
49
-
50
- const reader = res.body.getReader();
46
+
47
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
48
+
49
+ const reader = response.body.getReader();
51
50
  const decoder = new TextDecoder();
52
-
51
+ let buffer = "";
52
+
53
53
  while (true) {
54
54
  const { done, value } = await reader.read();
55
- if (done && callback) {
55
+ if (done) {
56
+ // 处理最后可能遗留的数据
57
+ if (buffer.trim()) {
58
+ tryProcessBuffer(buffer, callback);
59
+ }
56
60
  callback("finish", "");
57
61
  break;
58
62
  }
59
- try {
60
- const data = decoder.decode(value, { stream: true });
61
- const resData = JSON.parse(data.split("\n")[3].substr(5));
62
- const resText = resData?.output?.text;
63
- if (resText && callback) callback("message", resText);
64
- } catch (error) {}
63
+
64
+ buffer += decoder.decode(value, { stream: true });
65
+
66
+ let newlineIndex;
67
+ while ((newlineIndex = buffer.indexOf("\n")) !== -1) {
68
+ const line = buffer.substring(0, newlineIndex).trim();
69
+ buffer = buffer.substring(newlineIndex + 1);
70
+
71
+ if (line.startsWith("data:")) {
72
+ const data = line.substring(5).trim();
73
+ if (data && data !== "[DONE]") {
74
+ try {
75
+ const parsed = JSON.parse(data);
76
+ const text = parsed?.output?.text;
77
+ if (text && callback) {
78
+ callback("message", text);
79
+ }
80
+ } catch (e) {
81
+ // 可能是部分数据,继续等待
82
+ console.debug("等待完整数据...");
83
+ }
84
+ }
85
+ }
86
+ }
65
87
  }
66
88
  } catch (error) {
67
- console.error(error);
89
+ console.error("流式请求失败:", error);
90
+ callback("error", error.message);
68
91
  }
69
92
  };
93
+
94
+ // 辅助函数:处理缓冲区剩余数据
95
+ function tryProcessBuffer(buffer, callback) {
96
+ const lines = buffer.split("\n");
97
+ for (const line of lines) {
98
+ if (line.startsWith("data:")) {
99
+ const data = line.substring(5).trim();
100
+ if (data && data !== "[DONE]") {
101
+ try {
102
+ const parsed = JSON.parse(data);
103
+ const text = parsed?.output?.text;
104
+ if (text && callback) callback("message", text);
105
+ } catch (e) {}
106
+ }
107
+ }
108
+ }
109
+ }