@lowire/loop 0.0.20 → 0.0.21
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/fetchWithTimeout.d.ts +19 -0
- package/lib/fetchWithTimeout.js +38 -0
- package/lib/providers/anthropic.js +3 -1
- package/lib/providers/google.js +3 -1
- package/lib/providers/openai.js +3 -1
- package/lib/providers/openaiCompatible.js +3 -1
- package/lib/types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export declare function fetchWithTimeout(url: string, options: RequestInit & {
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
timeout?: number;
|
|
19
|
+
}): Promise<Response>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Microsoft Corporation.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.fetchWithTimeout = fetchWithTimeout;
|
|
19
|
+
async function fetchWithTimeout(url, options) {
|
|
20
|
+
if (!options.timeout)
|
|
21
|
+
return fetch(url, options);
|
|
22
|
+
const controller = new AbortController();
|
|
23
|
+
const timeoutId = setTimeout(() => controller.abort(), options.timeout);
|
|
24
|
+
const abort = () => controller.abort();
|
|
25
|
+
options.signal?.addEventListener('abort', abort);
|
|
26
|
+
try {
|
|
27
|
+
return await fetch(url, { ...options, signal: controller.signal });
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
if (options.signal?.aborted)
|
|
31
|
+
throw error;
|
|
32
|
+
throw new Error(`Fetch timeout after ${options.timeout}ms`);
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
clearTimeout(timeoutId);
|
|
36
|
+
options.signal?.removeEventListener('abort', abort);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.Anthropic = void 0;
|
|
19
|
+
const fetchWithTimeout_1 = require("../fetchWithTimeout");
|
|
19
20
|
class Anthropic {
|
|
20
21
|
name = 'anthropic';
|
|
21
22
|
async complete(conversation, options) {
|
|
@@ -49,11 +50,12 @@ async function create(createParams, options) {
|
|
|
49
50
|
};
|
|
50
51
|
const debugBody = { ...createParams, tools: `${createParams.tools?.length ?? 0} tools` };
|
|
51
52
|
options.debug?.('lowire:anthropic')('Request:', JSON.stringify(debugBody, null, 2));
|
|
52
|
-
const response = await
|
|
53
|
+
const response = await (0, fetchWithTimeout_1.fetchWithTimeout)(options.apiEndpoint ?? `https://api.anthropic.com/v1/messages`, {
|
|
53
54
|
method: 'POST',
|
|
54
55
|
headers,
|
|
55
56
|
body: JSON.stringify(createParams),
|
|
56
57
|
signal: options.signal,
|
|
58
|
+
timeout: options.apiTimeout
|
|
57
59
|
});
|
|
58
60
|
if (!response.ok) {
|
|
59
61
|
options.debug?.('lowire:anthropic')('Response:', response.status);
|
package/lib/providers/google.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.Google = void 0;
|
|
19
|
+
const fetchWithTimeout_1 = require("../fetchWithTimeout");
|
|
19
20
|
class Google {
|
|
20
21
|
name = 'google';
|
|
21
22
|
async complete(conversation, options) {
|
|
@@ -46,7 +47,7 @@ exports.Google = Google;
|
|
|
46
47
|
async function create(model, createParams, options) {
|
|
47
48
|
const debugBody = { ...createParams, tools: `${createParams.tools?.length ?? 0} tools` };
|
|
48
49
|
options.debug?.('lowire:google')('Request:', JSON.stringify(debugBody, null, 2));
|
|
49
|
-
const response = await
|
|
50
|
+
const response = await (0, fetchWithTimeout_1.fetchWithTimeout)(options.apiEndpoint ?? `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`, {
|
|
50
51
|
method: 'POST',
|
|
51
52
|
headers: {
|
|
52
53
|
'Content-Type': 'application/json',
|
|
@@ -54,6 +55,7 @@ async function create(model, createParams, options) {
|
|
|
54
55
|
},
|
|
55
56
|
body: JSON.stringify(createParams),
|
|
56
57
|
signal: options.signal,
|
|
58
|
+
timeout: options.apiTimeout
|
|
57
59
|
});
|
|
58
60
|
if (!response.ok) {
|
|
59
61
|
options.debug?.('lowire:google')('Response:', response.status);
|
package/lib/providers/openai.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.OpenAI = void 0;
|
|
19
|
+
const fetchWithTimeout_1 = require("../fetchWithTimeout");
|
|
19
20
|
class OpenAI {
|
|
20
21
|
name = 'openai';
|
|
21
22
|
async complete(conversation, options) {
|
|
@@ -69,11 +70,12 @@ async function create(createParams, options) {
|
|
|
69
70
|
};
|
|
70
71
|
const debugBody = { ...createParams, tools: `${createParams.tools?.length ?? 0} tools` };
|
|
71
72
|
options.debug?.('lowire:openai-responses')('Request:', JSON.stringify(debugBody, null, 2));
|
|
72
|
-
const response = await
|
|
73
|
+
const response = await (0, fetchWithTimeout_1.fetchWithTimeout)(options.apiEndpoint ?? `https://api.openai.com/v1/responses`, {
|
|
73
74
|
method: 'POST',
|
|
74
75
|
headers,
|
|
75
76
|
body: JSON.stringify(createParams),
|
|
76
77
|
signal: options.signal,
|
|
78
|
+
timeout: options.apiTimeout
|
|
77
79
|
});
|
|
78
80
|
if (!response.ok) {
|
|
79
81
|
options.debug?.('lowire:openai-responses')('Response:', response.status);
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.OpenAICompatible = void 0;
|
|
19
|
+
const fetchWithTimeout_1 = require("../fetchWithTimeout");
|
|
19
20
|
class OpenAICompatible {
|
|
20
21
|
name = 'openai-compatible';
|
|
21
22
|
async complete(conversation, options) {
|
|
@@ -67,11 +68,12 @@ async function create(createParams, options) {
|
|
|
67
68
|
};
|
|
68
69
|
const debugBody = { ...createParams, tools: `${createParams.tools?.length ?? 0} tools` };
|
|
69
70
|
options.debug?.('lowire:openai')('Request:', JSON.stringify(debugBody, null, 2));
|
|
70
|
-
const response = await
|
|
71
|
+
const response = await (0, fetchWithTimeout_1.fetchWithTimeout)(options.apiEndpoint ?? `https://api.openai.com/v1/chat/completions`, {
|
|
71
72
|
method: 'POST',
|
|
72
73
|
headers,
|
|
73
74
|
body: JSON.stringify(createParams),
|
|
74
75
|
signal: options.signal,
|
|
76
|
+
timeout: options.apiTimeout
|
|
75
77
|
});
|
|
76
78
|
if (!response.ok) {
|
|
77
79
|
options.debug?.('lowire:openai')('Response:', response.status);
|
package/lib/types.d.ts
CHANGED