st-comp 0.0.250 → 0.0.252

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/lib/aiTools.js CHANGED
@@ -1,11 +1,4 @@
1
- /**
2
- * 发送非流式请求到百炼 AI 应用
3
- * @param {Object} options
4
- * @param {string} options.appId - 应用ID
5
- * @param {string} options.apiKey - API Key
6
- * @param {string} options.value - 用户输入
7
- * @returns {Promise<string>} 返回 AI 输出的完整文本(JSON 字符串)
8
- */
1
+ // 非流式返回
9
2
  export const sendToBaiLianAppNonStreaming = async ({ appId, apiKey, value }) => {
10
3
  try {
11
4
  const response = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
@@ -34,3 +27,83 @@ export const sendToBaiLianAppNonStreaming = async ({ appId, apiKey, value }) =>
34
27
  throw error;
35
28
  }
36
29
  };
30
+ // 流式返回
31
+ export const sendToBaiLianAppStreaming = async ({ appId, apiKey, value, callback }) => {
32
+ try {
33
+ const response = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
34
+ method: "POST",
35
+ body: JSON.stringify({
36
+ input: { prompt: value },
37
+ parameters: { incremental_output: "true" },
38
+ debug: {},
39
+ }),
40
+ headers: {
41
+ Authorization: `Bearer ${apiKey}`,
42
+ "Content-Type": "application/json",
43
+ "X-DashScope-SSE": "enable",
44
+ },
45
+ });
46
+
47
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
48
+
49
+ const reader = response.body.getReader();
50
+ const decoder = new TextDecoder();
51
+ let buffer = "";
52
+
53
+ while (true) {
54
+ const { done, value } = await reader.read();
55
+ if (done) {
56
+ // 处理最后可能遗留的数据
57
+ if (buffer.trim()) {
58
+ tryProcessBuffer(buffer, callback);
59
+ }
60
+ callback("finish", "");
61
+ break;
62
+ }
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
+ }
87
+ }
88
+ } catch (error) {
89
+ console.error("流式请求失败:", error);
90
+ callback("error", error.message);
91
+ }
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
+ }
package/lib/bundle.js CHANGED
@@ -1,4 +1,4 @@
1
- import { i as m } from "./index-b51777a3.js";
1
+ import { i as m } from "./index-d57eff69.js";
2
2
  import "vue";
3
3
  import "echarts";
4
4
  export {