st-comp 0.0.261 → 0.0.263

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.
@@ -0,0 +1,3 @@
1
+ "use strict";const y=async({appId:f,apiKey:i,value:p,callback:e})=>{var a;try{const t=await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${f}/completion`,{method:"POST",body:JSON.stringify({input:{prompt:p},parameters:{incremental_output:"true"},debug:{}}),headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json","X-DashScope-SSE":"enable"}});if(!t.ok){e("error",`HTTP ${t.status}: ${t.statusText}`);return}const u=t.body.getReader(),r=new TextDecoder;let s="",o=null;for(;;){const{done:h,value:S}=await u.read();if(h){s.trim()&&x(s,e),e("finish","");break}s+=r.decode(S,{stream:!0});let m;for(;(m=s.indexOf(`
2
+ `))!==-1;){const c=s.substring(0,m).trim();if(s=s.substring(m+1),c.startsWith("event:")){o=c.substring(6).trim();continue}if(c.startsWith("data:")){const d=c.substring(5).trim();if(o==="error"||d&&d.startsWith("{"))try{const n=JSON.parse(d);if(n.code||n.message||o==="error"){const l=n.message||n.code||"未知错误";e("error",`百炼服务端错误: ${l}`);return}const g=(a=n==null?void 0:n.output)==null?void 0:a.text;g&&e&&e("message",g)}catch{console.debug("JSON 解析失败:",d)}o=null}}}}catch(t){console.error("流式请求失败:",t),e("error",t.message)}},x=(f,i)=>{var a;const p=f.split(`
3
+ `);let e=null;for(const t of p){if(t.startsWith("event:")){e=t.substring(6).trim();continue}if(t.startsWith("data:")){const u=t.substring(5).trim();if(u&&u!=="[DONE]")try{const r=JSON.parse(u);if(e==="error"||r.code||r.message){const o=r.message||r.code||"未知错误";i("error",`百炼服务端错误: ${o}`);return}const s=(a=r==null?void 0:r.output)==null?void 0:a.text;s&&i&&i("message",s)}catch{}e=null}}};exports.sendToBaiLianAppStreaming=y;
@@ -0,0 +1,90 @@
1
+ const x = async ({ appId: f, apiKey: i, value: p, callback: e }) => {
2
+ var a;
3
+ try {
4
+ const t = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${f}/completion`, {
5
+ method: "POST",
6
+ body: JSON.stringify({
7
+ input: { prompt: p },
8
+ parameters: { incremental_output: "true" },
9
+ debug: {}
10
+ }),
11
+ headers: {
12
+ Authorization: `Bearer ${i}`,
13
+ "Content-Type": "application/json",
14
+ "X-DashScope-SSE": "enable"
15
+ }
16
+ });
17
+ if (!t.ok) {
18
+ e("error", `HTTP ${t.status}: ${t.statusText}`);
19
+ return;
20
+ }
21
+ const u = t.body.getReader(), r = new TextDecoder();
22
+ let s = "", o = null;
23
+ for (; ; ) {
24
+ const { done: h, value: l } = await u.read();
25
+ if (h) {
26
+ s.trim() && y(s, e), e("finish", "");
27
+ break;
28
+ }
29
+ s += r.decode(l, { stream: !0 });
30
+ let m;
31
+ for (; (m = s.indexOf(`
32
+ `)) !== -1; ) {
33
+ const c = s.substring(0, m).trim();
34
+ if (s = s.substring(m + 1), c.startsWith("event:")) {
35
+ o = c.substring(6).trim();
36
+ continue;
37
+ }
38
+ if (c.startsWith("data:")) {
39
+ const d = c.substring(5).trim();
40
+ if (o === "error" || d && d.startsWith("{"))
41
+ try {
42
+ const n = JSON.parse(d);
43
+ if (n.code || n.message || o === "error") {
44
+ const S = n.message || n.code || "未知错误";
45
+ e("error", `百炼服务端错误: ${S}`);
46
+ return;
47
+ }
48
+ const g = (a = n == null ? void 0 : n.output) == null ? void 0 : a.text;
49
+ g && e && e("message", g);
50
+ } catch {
51
+ console.debug("JSON 解析失败:", d);
52
+ }
53
+ o = null;
54
+ }
55
+ }
56
+ }
57
+ } catch (t) {
58
+ console.error("流式请求失败:", t), e("error", t.message);
59
+ }
60
+ }, y = (f, i) => {
61
+ var a;
62
+ const p = f.split(`
63
+ `);
64
+ let e = null;
65
+ for (const t of p) {
66
+ if (t.startsWith("event:")) {
67
+ e = t.substring(6).trim();
68
+ continue;
69
+ }
70
+ if (t.startsWith("data:")) {
71
+ const u = t.substring(5).trim();
72
+ if (u && u !== "[DONE]")
73
+ try {
74
+ const r = JSON.parse(u);
75
+ if (e === "error" || r.code || r.message) {
76
+ const o = r.message || r.code || "未知错误";
77
+ i("error", `百炼服务端错误: ${o}`);
78
+ return;
79
+ }
80
+ const s = (a = r == null ? void 0 : r.output) == null ? void 0 : a.text;
81
+ s && i && i("message", s);
82
+ } catch {
83
+ }
84
+ e = null;
85
+ }
86
+ }
87
+ };
88
+ export {
89
+ x as s
90
+ };