@kevisual/query 0.0.7 → 0.0.9-alpha.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.
@@ -0,0 +1,19 @@
1
+ type AdapterOpts = {
2
+ url: string;
3
+ headers?: Record<string, string>;
4
+ body?: Record<string, any>;
5
+ timeout?: number;
6
+ };
7
+ /**
8
+ *
9
+ * @param opts
10
+ * @param overloadOpts 覆盖fetch的默认配置
11
+ * @returns
12
+ */
13
+ declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
14
+ /**
15
+ * adapter
16
+ */
17
+ declare const queryFetch: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
18
+
19
+ export { adapter, queryFetch };
@@ -0,0 +1,53 @@
1
+ /**
2
+ *
3
+ * @param opts
4
+ * @param overloadOpts 覆盖fetch的默认配置
5
+ * @returns
6
+ */
7
+ const adapter = async (opts, overloadOpts) => {
8
+ const controller = new AbortController();
9
+ const signal = controller.signal;
10
+ const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
11
+ const timer = setTimeout(() => {
12
+ controller.abort();
13
+ }, timeout);
14
+ return fetch(opts.url, {
15
+ method: 'POST',
16
+ headers: {
17
+ 'Content-Type': 'application/json',
18
+ ...opts.headers,
19
+ },
20
+ body: JSON.stringify(opts.body),
21
+ signal,
22
+ ...overloadOpts,
23
+ })
24
+ .then((response) => {
25
+ // 获取 Content-Type 头部信息
26
+ const contentType = response.headers.get('Content-Type');
27
+ // 判断返回的数据类型
28
+ if (contentType && contentType.includes('application/json')) {
29
+ return response.json(); // 解析为 JSON
30
+ }
31
+ else {
32
+ return response.text(); // 解析为文本
33
+ }
34
+ })
35
+ .catch((err) => {
36
+ if (err.name === 'AbortError') {
37
+ console.log('Request timed out and was aborted');
38
+ }
39
+ console.error(err);
40
+ return {
41
+ code: 500,
42
+ };
43
+ })
44
+ .finally(() => {
45
+ clearTimeout(timer);
46
+ });
47
+ };
48
+ /**
49
+ * adapter
50
+ */
51
+ const queryFetch = adapter;
52
+
53
+ export { adapter, queryFetch };
@@ -0,0 +1,26 @@
1
+ import OpenAI, { ClientOptions } from 'openai';
2
+ export { default as OpenAI } from 'openai';
3
+ import { RequestOptions } from 'openai/core.mjs';
4
+
5
+ type QueryOpts = {
6
+ /**
7
+ * OpenAI model name, example: deepseek-chat
8
+ */
9
+ model: string;
10
+ /**
11
+ * OpenAI client options
12
+ * QueryAi.init() will be called with these options
13
+ */
14
+ openAiOpts?: ClientOptions;
15
+ openai?: OpenAI;
16
+ };
17
+ declare class QueryAI {
18
+ private openai;
19
+ model?: string;
20
+ constructor(opts?: QueryOpts);
21
+ init(opts: ClientOptions): void;
22
+ query(prompt: string, opts?: RequestOptions): Promise<any>;
23
+ queryAsync(prompt: string, opts?: RequestOptions): Promise<any>;
24
+ }
25
+
26
+ export { QueryAI };