@betterhyq/ask-oxygent 1.0.0
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/LICENSE +21 -0
- package/README.md +72 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +52 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# packageName
|
|
2
|
+
|
|
3
|
+
<!-- automd:badges color=yellow -->
|
|
4
|
+
|
|
5
|
+
[](https://npmjs.com/package/@betterhyq/ask-oxygent)
|
|
6
|
+
[](https://npm.chart.dev/@betterhyq/ask-oxygent)
|
|
7
|
+
|
|
8
|
+
<!-- /automd -->
|
|
9
|
+
|
|
10
|
+
This is my package description.
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
Install the package:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
# ✨ Auto-detect (supports npm, yarn, pnpm, deno and bun)
|
|
18
|
+
npx nypm install packageName
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Import:
|
|
22
|
+
|
|
23
|
+
<!-- automd:jsimport cdn name="pkg" -->
|
|
24
|
+
|
|
25
|
+
**ESM** (Node.js, Bun, Deno)
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import {} from "pkg";
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**CDN** (Deno and Browsers)
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import {} from "https://esm.sh/pkg";
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
<!-- /automd -->
|
|
38
|
+
|
|
39
|
+
## Development
|
|
40
|
+
|
|
41
|
+
<details>
|
|
42
|
+
|
|
43
|
+
<summary>local development</summary>
|
|
44
|
+
|
|
45
|
+
- Clone this repository
|
|
46
|
+
- Install latest LTS version of [Node.js](https://nodejs.org/en/)
|
|
47
|
+
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
|
48
|
+
- Install dependencies using `pnpm install`
|
|
49
|
+
- Run interactive tests using `pnpm dev`
|
|
50
|
+
|
|
51
|
+
</details>
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
<!-- automd:contributors license=MIT -->
|
|
56
|
+
|
|
57
|
+
Published under the [MIT](https://github.com/unjs/packageName/blob/main/LICENSE) license.
|
|
58
|
+
Made by [community](https://github.com/unjs/packageName/graphs/contributors) 💛
|
|
59
|
+
<br><br>
|
|
60
|
+
<a href="https://github.com/unjs/packageName/graphs/contributors">
|
|
61
|
+
<img src="https://contrib.rocks/image?repo=unjs/packageName" />
|
|
62
|
+
</a>
|
|
63
|
+
|
|
64
|
+
<!-- /automd -->
|
|
65
|
+
|
|
66
|
+
<!-- automd:with-automd -->
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
_🤖 auto updated with [automd](https://automd.unjs.io)_
|
|
71
|
+
|
|
72
|
+
<!-- /automd -->
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* 从 SSE 流中提取 answer 类型的 content
|
|
4
|
+
* 建立长连接,遇到 type=answer 时立即返回响应
|
|
5
|
+
* @param url - 请求的 URL
|
|
6
|
+
* @param postData - POST 请求的数据
|
|
7
|
+
* @returns 返回 answer 类型数据的 content 字段
|
|
8
|
+
* @throws {Error} 当请求失败、响应体为空或未找到 answer 类型响应时抛出错误
|
|
9
|
+
*/
|
|
10
|
+
declare const askOxygent: (url: string, postData: Record<string, unknown>) => Promise<string>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { askOxygent };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { fetchEventSource } from "@microsoft/fetch-event-source";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* 从 SSE 流中提取 answer 类型的 content
|
|
6
|
+
* 建立长连接,遇到 type=answer 时立即返回响应
|
|
7
|
+
* @param url - 请求的 URL
|
|
8
|
+
* @param postData - POST 请求的数据
|
|
9
|
+
* @returns 返回 answer 类型数据的 content 字段
|
|
10
|
+
* @throws {Error} 当请求失败、响应体为空或未找到 answer 类型响应时抛出错误
|
|
11
|
+
*/
|
|
12
|
+
const askOxygent = async (url, postData) => {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
let foundAnswer = false;
|
|
15
|
+
const controller = new AbortController();
|
|
16
|
+
fetchEventSource(url, {
|
|
17
|
+
method: "POST",
|
|
18
|
+
headers: { "Content-Type": "application/json" },
|
|
19
|
+
body: JSON.stringify(postData),
|
|
20
|
+
signal: controller.signal,
|
|
21
|
+
async onopen(response) {
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
const statusText = response.statusText || "未知错误";
|
|
24
|
+
reject(new Error(`请求失败:${response.status} ${statusText}。URL: ${url}`));
|
|
25
|
+
controller.abort();
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
onmessage(event) {
|
|
29
|
+
try {
|
|
30
|
+
const jsonData = JSON.parse(event.data);
|
|
31
|
+
if (jsonData.type === "answer" && jsonData.content) {
|
|
32
|
+
foundAnswer = true;
|
|
33
|
+
controller.abort();
|
|
34
|
+
resolve(jsonData.content);
|
|
35
|
+
}
|
|
36
|
+
} 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
|
+
}
|
|
45
|
+
}).catch((error) => {
|
|
46
|
+
if (!foundAnswer && error.name !== "AbortError") reject(error instanceof Error ? error : new Error(`请求失败:${String(error)}`));
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { askOxygent };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@betterhyq/ask-oxygent",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"repository": "unjs/packageName",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"types": "./dist/index.d.mts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "obuild",
|
|
18
|
+
"dev": "vitest dev",
|
|
19
|
+
"lint": "eslint . && prettier -c .",
|
|
20
|
+
"lint:fix": "automd && eslint . --fix && prettier -w .",
|
|
21
|
+
"prepack": "pnpm build",
|
|
22
|
+
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
|
|
23
|
+
"test": "pnpm lint && pnpm test:types && vitest run --coverage",
|
|
24
|
+
"test:types": "tsc --noEmit --skipLibCheck"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^24.3.0",
|
|
28
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
29
|
+
"automd": "^0.4.0",
|
|
30
|
+
"changelogen": "^0.6.2",
|
|
31
|
+
"eslint": "^9.33.0",
|
|
32
|
+
"eslint-config-unjs": "^0.5.0",
|
|
33
|
+
"obuild": "^0.2.1",
|
|
34
|
+
"prettier": "^3.6.2",
|
|
35
|
+
"typescript": "^5.9.2",
|
|
36
|
+
"vitest": "^3.2.4"
|
|
37
|
+
},
|
|
38
|
+
"packageManager": "pnpm@10.15.0",
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@microsoft/fetch-event-source": "2.0.1"
|
|
41
|
+
}
|
|
42
|
+
}
|