@betterhyq/ask-oxygent 1.0.0 → 1.0.1
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/dist/index.d.mts +2 -3
- package/dist/index.mjs +29 -36
- package/package.json +3 -6
package/dist/index.d.mts
CHANGED
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* @param url - 请求的 URL
|
|
6
6
|
* @param postData - POST 请求的数据
|
|
7
7
|
* @returns 返回 answer 类型数据的 content 字段
|
|
8
|
-
* @throws {Error} 当请求失败、响应体为空或未找到 answer 类型响应时抛出错误
|
|
9
8
|
*/
|
|
10
|
-
declare const
|
|
9
|
+
declare const fetchSSEAnswer: (url: string, postData: Record<string, unknown>) => Promise<string>;
|
|
11
10
|
//#endregion
|
|
12
|
-
export {
|
|
11
|
+
export { fetchSSEAnswer };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { fetchEventSource } from "@microsoft/fetch-event-source";
|
|
2
|
-
|
|
3
1
|
//#region src/index.ts
|
|
4
2
|
/**
|
|
5
3
|
* 从 SSE 流中提取 answer 类型的 content
|
|
@@ -7,46 +5,41 @@ import { fetchEventSource } from "@microsoft/fetch-event-source";
|
|
|
7
5
|
* @param url - 请求的 URL
|
|
8
6
|
* @param postData - POST 请求的数据
|
|
9
7
|
* @returns 返回 answer 类型数据的 content 字段
|
|
10
|
-
* @throws {Error} 当请求失败、响应体为空或未找到 answer 类型响应时抛出错误
|
|
11
8
|
*/
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
9
|
+
const fetchSSEAnswer = async (url, postData) => {
|
|
10
|
+
const response = await fetch(url, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: { "Content-Type": "application/json" },
|
|
13
|
+
body: JSON.stringify(postData)
|
|
14
|
+
});
|
|
15
|
+
if (!response.ok) throw new Error(`请求失败:${response.status}`);
|
|
16
|
+
if (!response.body) throw new Error("响应体为空");
|
|
17
|
+
const reader = response.body.getReader();
|
|
18
|
+
const decoder = new TextDecoder();
|
|
19
|
+
let buffer = "";
|
|
20
|
+
while (true) {
|
|
21
|
+
const { done, value } = await reader.read();
|
|
22
|
+
if (done) break;
|
|
23
|
+
buffer += decoder.decode(value, { stream: true });
|
|
24
|
+
const lines = buffer.split("\n");
|
|
25
|
+
buffer = lines.pop() || "";
|
|
26
|
+
for (const line of lines) {
|
|
27
|
+
const trimmedLine = line.trim();
|
|
28
|
+
if (!trimmedLine || trimmedLine.startsWith(":")) continue;
|
|
29
|
+
if (trimmedLine.startsWith("data: ")) {
|
|
30
|
+
const data = trimmedLine.slice(6);
|
|
29
31
|
try {
|
|
30
|
-
const jsonData = JSON.parse(
|
|
32
|
+
const jsonData = JSON.parse(data);
|
|
31
33
|
if (jsonData.type === "answer" && jsonData.content) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
resolve(jsonData.content);
|
|
34
|
+
reader.cancel();
|
|
35
|
+
return jsonData.content;
|
|
35
36
|
}
|
|
36
37
|
} catch {}
|
|
37
|
-
},
|
|
38
|
-
onerror(error) {
|
|
39
|
-
if (!foundAnswer) reject(new Error(`SSE 连接错误:${error.message || "未知错误"}`));
|
|
40
|
-
throw error;
|
|
41
|
-
},
|
|
42
|
-
onclose() {
|
|
43
|
-
if (!foundAnswer) reject(new Error("SSE 流已结束,但未找到 type=answer 的响应"));
|
|
44
38
|
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
throw new Error("未找到 answer 类型的响应");
|
|
49
42
|
};
|
|
50
43
|
|
|
51
44
|
//#endregion
|
|
52
|
-
export {
|
|
45
|
+
export { fetchSSEAnswer };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@betterhyq/ask-oxygent",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": "unjs/packageName",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,8 +35,5 @@
|
|
|
35
35
|
"typescript": "^5.9.2",
|
|
36
36
|
"vitest": "^3.2.4"
|
|
37
37
|
},
|
|
38
|
-
"packageManager": "pnpm@10.15.0"
|
|
39
|
-
|
|
40
|
-
"@microsoft/fetch-event-source": "2.0.1"
|
|
41
|
-
}
|
|
42
|
-
}
|
|
38
|
+
"packageManager": "pnpm@10.15.0"
|
|
39
|
+
}
|