st-comp 0.0.253 → 0.0.255

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
@@ -27,7 +27,7 @@ export const sendToBaiLianAppNonStreaming = async ({ appId, apiKey, value }) =>
27
27
  throw error;
28
28
  }
29
29
  };
30
- // 流式返回
30
+ // 流式返回 - 支持错误处理
31
31
  export const sendToBaiLianAppStreaming = async ({ appId, apiKey, value, callback }) => {
32
32
  try {
33
33
  const response = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
@@ -43,13 +43,17 @@ export const sendToBaiLianAppStreaming = async ({ appId, apiKey, value, callback
43
43
  "X-DashScope-SSE": "enable",
44
44
  },
45
45
  });
46
-
47
- if (!response.ok) throw new Error(`HTTP ${response.status}`);
48
-
46
+
47
+ if (!response.ok) {
48
+ callback("error", `HTTP ${response.status}: ${response.statusText}`);
49
+ return;
50
+ }
51
+
49
52
  const reader = response.body.getReader();
50
53
  const decoder = new TextDecoder();
51
54
  let buffer = "";
52
-
55
+ let currentEvent = null; // 记录当前 event 类型
56
+
53
57
  while (true) {
54
58
  const { done, value } = await reader.read();
55
59
  if (done) {
@@ -60,28 +64,48 @@ export const sendToBaiLianAppStreaming = async ({ appId, apiKey, value, callback
60
64
  callback("finish", "");
61
65
  break;
62
66
  }
63
-
67
+
64
68
  buffer += decoder.decode(value, { stream: true });
65
-
69
+
66
70
  let newlineIndex;
67
71
  while ((newlineIndex = buffer.indexOf("\n")) !== -1) {
68
72
  const line = buffer.substring(0, newlineIndex).trim();
69
73
  buffer = buffer.substring(newlineIndex + 1);
70
-
74
+
75
+ // 处理 event: 行(错误类型)
76
+ if (line.startsWith("event:")) {
77
+ currentEvent = line.substring(6).trim();
78
+ continue;
79
+ }
80
+
81
+ // 处理 data: 行
71
82
  if (line.startsWith("data:")) {
72
- const data = line.substring(5).trim();
73
- if (data && data !== "[DONE]") {
83
+ const dataStr = line.substring(5).trim();
84
+
85
+ // 检查是否是错误响应
86
+ if (currentEvent === "error" || (dataStr && dataStr.startsWith("{"))) {
74
87
  try {
75
- const parsed = JSON.parse(data);
88
+ const parsed = JSON.parse(dataStr);
89
+
90
+ // 处理错误响应
91
+ if (parsed.code || parsed.message || currentEvent === "error") {
92
+ const errorMsg = parsed.message || parsed.code || "未知错误";
93
+ callback("error", `百炼服务端错误: ${errorMsg}`);
94
+ return; // 终止流式处理
95
+ }
96
+
97
+ // 正常响应:提取 text
76
98
  const text = parsed?.output?.text;
77
99
  if (text && callback) {
78
100
  callback("message", text);
79
101
  }
80
102
  } catch (e) {
81
- // 可能是部分数据,继续等待
82
- console.debug("等待完整数据...");
103
+ console.debug("JSON 解析失败:", dataStr);
83
104
  }
84
105
  }
106
+
107
+ // 重置 event 类型
108
+ currentEvent = null;
85
109
  }
86
110
  }
87
111
  }
@@ -92,18 +116,35 @@ export const sendToBaiLianAppStreaming = async ({ appId, apiKey, value, callback
92
116
  };
93
117
 
94
118
  // 辅助函数:处理缓冲区剩余数据
95
- function tryProcessBuffer(buffer, callback) {
119
+ const tryProcessBuffer = (buffer, callback) => {
96
120
  const lines = buffer.split("\n");
121
+ let currentEvent = null;
122
+
97
123
  for (const line of lines) {
124
+ if (line.startsWith("event:")) {
125
+ currentEvent = line.substring(6).trim();
126
+ continue;
127
+ }
128
+
98
129
  if (line.startsWith("data:")) {
99
- const data = line.substring(5).trim();
100
- if (data && data !== "[DONE]") {
130
+ const dataStr = line.substring(5).trim();
131
+ if (dataStr && dataStr !== "[DONE]") {
101
132
  try {
102
- const parsed = JSON.parse(data);
133
+ const parsed = JSON.parse(dataStr);
134
+
135
+ // 错误响应
136
+ if (currentEvent === "error" || parsed.code || parsed.message) {
137
+ const errorMsg = parsed.message || parsed.code || "未知错误";
138
+ callback("error", `百炼服务端错误: ${errorMsg}`);
139
+ return;
140
+ }
141
+
142
+ // 正常响应
103
143
  const text = parsed?.output?.text;
104
144
  if (text && callback) callback("message", text);
105
145
  } catch (e) {}
106
146
  }
147
+ currentEvent = null;
107
148
  }
108
149
  }
109
- }
150
+ };