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/VarietyAiHelper.cjs +5 -4
- package/es/VarietyAiHelper.js +281 -233
- package/es/aiTools.js +59 -18
- package/es/style.css +1 -1
- package/lib/aiTools.js +59 -18
- package/lib/bundle.js +1 -1
- package/lib/bundle.umd.cjs +54 -53
- package/lib/{index-0dab5f01.js → index-73a5aa87.js} +1993 -1945
- package/lib/{python-1a46034d.js → python-8821365d.js} +1 -1
- package/lib/style.css +1 -1
- package/package.json +1 -1
- package/packages/VarietyAiHelper/index.vue +58 -9
- package/public/aiTools.js +59 -18
package/lib/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)
|
|
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
|
|
73
|
-
|
|
83
|
+
const dataStr = line.substring(5).trim();
|
|
84
|
+
|
|
85
|
+
// 检查是否是错误响应
|
|
86
|
+
if (currentEvent === "error" || (dataStr && dataStr.startsWith("{"))) {
|
|
74
87
|
try {
|
|
75
|
-
const parsed = JSON.parse(
|
|
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
|
-
|
|
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
|
|
100
|
-
if (
|
|
130
|
+
const dataStr = line.substring(5).trim();
|
|
131
|
+
if (dataStr && dataStr !== "[DONE]") {
|
|
101
132
|
try {
|
|
102
|
-
const parsed = JSON.parse(
|
|
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
|
+
};
|
package/lib/bundle.js
CHANGED