npm-ai-hooks 2.0.4 → 2.0.5
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/cjs/errors.js +3 -0
- package/dist/cjs/providers/base/BaseProvider.js +37 -5
- package/dist/cjs/providers/base/ProviderConfig.js +3 -2
- package/dist/cjs/providers/base/ProviderRegistry.js +2 -4
- package/dist/esm/errors.js +3 -0
- package/dist/esm/providers/base/BaseProvider.js +37 -2
- package/dist/esm/providers/base/ProviderConfig.js +3 -2
- package/dist/esm/providers/base/ProviderRegistry.js +2 -4
- package/dist/providers/base/BaseProvider.d.ts +13 -5
- package/dist/providers/base/SpecializedProviders.d.ts +4 -64
- package/package.json +4 -7
package/dist/cjs/errors.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.BaseProvider = void 0;
|
|
7
|
-
const axios_1 = __importDefault(require("axios"));
|
|
8
4
|
const errors_1 = require("../../errors");
|
|
9
5
|
class BaseProvider {
|
|
6
|
+
config;
|
|
10
7
|
constructor(config) {
|
|
11
8
|
this.config = config;
|
|
12
9
|
}
|
|
@@ -54,7 +51,42 @@ class BaseProvider {
|
|
|
54
51
|
};
|
|
55
52
|
}
|
|
56
53
|
async makeRequest(config) {
|
|
57
|
-
|
|
54
|
+
try {
|
|
55
|
+
const response = await fetch(config.url, {
|
|
56
|
+
method: config.method || "POST",
|
|
57
|
+
headers: config.headers,
|
|
58
|
+
body: config.data ? JSON.stringify(config.data) : undefined,
|
|
59
|
+
});
|
|
60
|
+
let data;
|
|
61
|
+
try {
|
|
62
|
+
data = await response.json();
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
data = await response.text();
|
|
66
|
+
}
|
|
67
|
+
if (!response.ok) {
|
|
68
|
+
// Emulate axios error shape for the error handler
|
|
69
|
+
throw Object.assign(new Error(`Request failed with status code ${response.status}`), {
|
|
70
|
+
response: {
|
|
71
|
+
status: response.status,
|
|
72
|
+
statusText: response.statusText,
|
|
73
|
+
data: data
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return { data };
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
// If it's already an HTTP error we just threw, rethrow it
|
|
81
|
+
if (error.response) {
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
// Otherwise, it's a network error (e.g. fetch failed entirely)
|
|
85
|
+
// Emulate axios network error shape
|
|
86
|
+
throw Object.assign(new Error(error.message || "Network Error"), {
|
|
87
|
+
request: {}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
58
90
|
}
|
|
59
91
|
parseResponse(response) {
|
|
60
92
|
const output = this.config.responseParser(response);
|
|
@@ -6,9 +6,10 @@ const BaseProvider_1 = require("./BaseProvider");
|
|
|
6
6
|
const ProviderConfigs_1 = require("./ProviderConfigs");
|
|
7
7
|
const SpecializedProviders_1 = require("./SpecializedProviders");
|
|
8
8
|
class ProviderManager {
|
|
9
|
+
providers = new Map();
|
|
10
|
+
defaultProvider;
|
|
11
|
+
providerFunctions = new Map();
|
|
9
12
|
constructor(options) {
|
|
10
|
-
this.providers = new Map();
|
|
11
|
-
this.providerFunctions = new Map();
|
|
12
13
|
this.initializeProviders(options);
|
|
13
14
|
}
|
|
14
15
|
initializeProviders(options) {
|
|
@@ -2,10 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.providerRegistry = exports.ProviderRegistry = void 0;
|
|
4
4
|
class ProviderRegistry {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.providerFunctions = new Map();
|
|
8
|
-
}
|
|
5
|
+
providers = new Map();
|
|
6
|
+
providerFunctions = new Map();
|
|
9
7
|
register(name, provider) {
|
|
10
8
|
this.providers.set(name, provider);
|
|
11
9
|
this.providerFunctions.set(name, this.createProviderFunction(provider));
|
package/dist/esm/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
1
|
import { AIHookError } from "../../errors";
|
|
3
2
|
export class BaseProvider {
|
|
3
|
+
config;
|
|
4
4
|
constructor(config) {
|
|
5
5
|
this.config = config;
|
|
6
6
|
}
|
|
@@ -48,7 +48,42 @@ export class BaseProvider {
|
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
async makeRequest(config) {
|
|
51
|
-
|
|
51
|
+
try {
|
|
52
|
+
const response = await fetch(config.url, {
|
|
53
|
+
method: config.method || "POST",
|
|
54
|
+
headers: config.headers,
|
|
55
|
+
body: config.data ? JSON.stringify(config.data) : undefined,
|
|
56
|
+
});
|
|
57
|
+
let data;
|
|
58
|
+
try {
|
|
59
|
+
data = await response.json();
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
data = await response.text();
|
|
63
|
+
}
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
// Emulate axios error shape for the error handler
|
|
66
|
+
throw Object.assign(new Error(`Request failed with status code ${response.status}`), {
|
|
67
|
+
response: {
|
|
68
|
+
status: response.status,
|
|
69
|
+
statusText: response.statusText,
|
|
70
|
+
data: data
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return { data };
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
// If it's already an HTTP error we just threw, rethrow it
|
|
78
|
+
if (error.response) {
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
// Otherwise, it's a network error (e.g. fetch failed entirely)
|
|
82
|
+
// Emulate axios network error shape
|
|
83
|
+
throw Object.assign(new Error(error.message || "Network Error"), {
|
|
84
|
+
request: {}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
52
87
|
}
|
|
53
88
|
parseResponse(response) {
|
|
54
89
|
const output = this.config.responseParser(response);
|
|
@@ -3,9 +3,10 @@ import { BaseProvider } from "./BaseProvider";
|
|
|
3
3
|
import { providerConfigs } from "./ProviderConfigs";
|
|
4
4
|
import { ClaudeProvider, GeminiProvider, OpenRouterProvider } from "./SpecializedProviders";
|
|
5
5
|
export class ProviderManager {
|
|
6
|
+
providers = new Map();
|
|
7
|
+
defaultProvider;
|
|
8
|
+
providerFunctions = new Map();
|
|
6
9
|
constructor(options) {
|
|
7
|
-
this.providers = new Map();
|
|
8
|
-
this.providerFunctions = new Map();
|
|
9
10
|
this.initializeProviders(options);
|
|
10
11
|
}
|
|
11
12
|
initializeProviders(options) {
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
export class ProviderRegistry {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
this.providerFunctions = new Map();
|
|
5
|
-
}
|
|
2
|
+
providers = new Map();
|
|
3
|
+
providerFunctions = new Map();
|
|
6
4
|
register(name, provider) {
|
|
7
5
|
this.providers.set(name, provider);
|
|
8
6
|
this.providerFunctions.set(name, this.createProviderFunction(provider));
|
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
import { AxiosRequestConfig, AxiosResponse } from "axios";
|
|
2
1
|
import { AIHookError } from "../../errors";
|
|
2
|
+
export interface FetchRequestConfig {
|
|
3
|
+
url: string;
|
|
4
|
+
method?: string;
|
|
5
|
+
data?: any;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
}
|
|
8
|
+
export interface FetchResponse {
|
|
9
|
+
data: any;
|
|
10
|
+
}
|
|
3
11
|
export interface ProviderConfig {
|
|
4
12
|
name: string;
|
|
5
13
|
baseUrl: string;
|
|
6
14
|
envKey: string;
|
|
7
15
|
headers: Record<string, string>;
|
|
8
16
|
requestBody: (prompt: string, model: string) => any;
|
|
9
|
-
responseParser: (response:
|
|
17
|
+
responseParser: (response: FetchResponse) => string;
|
|
10
18
|
errorMessages: {
|
|
11
19
|
missingKey: string;
|
|
12
20
|
emptyResponse: string;
|
|
@@ -23,10 +31,10 @@ export declare class BaseProvider {
|
|
|
23
31
|
call(prompt: string, model: string): Promise<string>;
|
|
24
32
|
protected getApiKey(): string | undefined;
|
|
25
33
|
protected validateApiKey(apiKey: string | undefined): void;
|
|
26
|
-
protected buildRequestConfig(prompt: string, model: string, apiKey: string):
|
|
34
|
+
protected buildRequestConfig(prompt: string, model: string, apiKey: string): FetchRequestConfig;
|
|
27
35
|
protected buildAuthHeaders(apiKey: string): Record<string, string>;
|
|
28
|
-
protected makeRequest(config:
|
|
29
|
-
protected parseResponse(response:
|
|
36
|
+
protected makeRequest(config: FetchRequestConfig): Promise<FetchResponse>;
|
|
37
|
+
protected parseResponse(response: FetchResponse): string;
|
|
30
38
|
protected handleError(error: any): AIHookError;
|
|
31
39
|
protected getCapitalizedProviderName(): string;
|
|
32
40
|
protected handleHttpError(error: any): AIHookError;
|
|
@@ -8,71 +8,11 @@ export declare class GeminiProvider extends BaseProvider {
|
|
|
8
8
|
constructor();
|
|
9
9
|
protected buildRequestConfig(prompt: string, model: string, apiKey: string): {
|
|
10
10
|
url: string;
|
|
11
|
-
headers: {
|
|
12
|
-
|
|
13
|
-
baseURL?: string;
|
|
14
|
-
allowAbsoluteUrls?: boolean;
|
|
15
|
-
transformRequest?: import("axios").AxiosRequestTransformer | import("axios").AxiosRequestTransformer[];
|
|
16
|
-
transformResponse?: import("axios").AxiosResponseTransformer | import("axios").AxiosResponseTransformer[];
|
|
17
|
-
params?: any;
|
|
18
|
-
paramsSerializer?: import("axios").ParamsSerializerOptions | import("axios").CustomParamsSerializer;
|
|
19
|
-
data?: any;
|
|
20
|
-
timeout?: number;
|
|
21
|
-
timeoutErrorMessage?: string;
|
|
22
|
-
withCredentials?: boolean;
|
|
23
|
-
adapter?: (import("axios").AxiosAdapter | ((string & {}) | "xhr" | "http" | "fetch")) | (import("axios").AxiosAdapter | ((string & {}) | "xhr" | "http" | "fetch"))[];
|
|
24
|
-
auth?: import("axios").AxiosBasicCredentials;
|
|
25
|
-
responseType?: import("axios").ResponseType;
|
|
26
|
-
responseEncoding?: (string & {}) | import("axios").responseEncoding;
|
|
27
|
-
xsrfCookieName?: string;
|
|
28
|
-
xsrfHeaderName?: string;
|
|
29
|
-
onUploadProgress?: (progressEvent: import("axios").AxiosProgressEvent) => void;
|
|
30
|
-
onDownloadProgress?: (progressEvent: import("axios").AxiosProgressEvent) => void;
|
|
31
|
-
maxContentLength?: number;
|
|
32
|
-
validateStatus?: ((status: number) => boolean) | null;
|
|
33
|
-
maxBodyLength?: number;
|
|
34
|
-
maxRedirects?: number;
|
|
35
|
-
maxRate?: number | [number, number];
|
|
36
|
-
beforeRedirect?: (options: Record<string, any>, responseDetails: {
|
|
37
|
-
headers: Record<string, string>;
|
|
38
|
-
statusCode: import("axios").HttpStatusCode;
|
|
39
|
-
}, requestDetails: {
|
|
40
|
-
headers: Record<string, string>;
|
|
41
|
-
url: string;
|
|
42
|
-
method: string;
|
|
43
|
-
}) => void;
|
|
44
|
-
socketPath?: string | null;
|
|
45
|
-
allowedSocketPaths?: string | string[] | null;
|
|
46
|
-
transport?: any;
|
|
47
|
-
httpAgent?: any;
|
|
48
|
-
httpsAgent?: any;
|
|
49
|
-
proxy?: import("axios").AxiosProxyConfig | false;
|
|
50
|
-
cancelToken?: import("axios").CancelToken | undefined;
|
|
51
|
-
decompress?: boolean;
|
|
52
|
-
transitional?: import("axios").TransitionalOptions;
|
|
53
|
-
signal?: import("axios").GenericAbortSignal;
|
|
54
|
-
insecureHTTPParser?: boolean;
|
|
55
|
-
env?: {
|
|
56
|
-
FormData?: new (...args: any[]) => object;
|
|
57
|
-
fetch?: (input: URL | Request | string, init?: RequestInit) => Promise<Response>;
|
|
58
|
-
Request?: new (input: URL | Request | string, init?: RequestInit) => Request;
|
|
59
|
-
Response?: new (body?: ArrayBuffer | ArrayBufferView | Blob | FormData | URLSearchParams | string | null, init?: ResponseInit) => Response;
|
|
60
|
-
};
|
|
61
|
-
formSerializer?: import("axios").FormSerializerOptions;
|
|
62
|
-
family?: import("axios").AddressFamily;
|
|
63
|
-
lookup?: ((hostname: string, options: object, cb: (err: Error | null, address: import("axios").LookupAddress | import("axios").LookupAddress[], family?: import("axios").AddressFamily) => void) => void) | ((hostname: string, options: object) => Promise<[address: import("axios").LookupAddressEntry | import("axios").LookupAddressEntry[], family?: import("axios").AddressFamily] | import("axios").LookupAddress>);
|
|
64
|
-
withXSRFToken?: boolean | ((config: import("axios").InternalAxiosRequestConfig) => boolean | undefined);
|
|
65
|
-
parseReviver?: (this: any, key: string, value: any, context?: {
|
|
66
|
-
source?: string;
|
|
67
|
-
}) => any;
|
|
68
|
-
fetchOptions?: Omit<RequestInit, "body" | "headers" | "method" | "signal"> | Record<string, any>;
|
|
69
|
-
httpVersion?: 1 | 2;
|
|
70
|
-
http2Options?: Record<string, any> & {
|
|
71
|
-
sessionTimeout?: number;
|
|
11
|
+
headers: {
|
|
12
|
+
[x: string]: string;
|
|
72
13
|
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
sensitiveHeaders?: string[];
|
|
14
|
+
method?: string;
|
|
15
|
+
data?: any;
|
|
76
16
|
};
|
|
77
17
|
protected buildAuthHeaders(): Record<string, string>;
|
|
78
18
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "npm-ai-hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"description": "Universal AI Hook Layer for Node.js and React – one wrapper for all AI providers. Inject LLM-like behavior into any JavaScript or TypeScript function with a single line, without writing prompts, handling SDKs, or locking into any provider.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -48,19 +48,16 @@
|
|
|
48
48
|
"url": "git+https://github.com/iTeebot/npm-ai-hooks.git"
|
|
49
49
|
},
|
|
50
50
|
"license": "MIT",
|
|
51
|
-
"dependencies": {
|
|
52
|
-
"axios": "^1.12.2"
|
|
53
|
-
},
|
|
54
51
|
"devDependencies": {
|
|
55
52
|
"@types/jest": "^30.0.0",
|
|
56
|
-
"@types/node": "^
|
|
57
|
-
"eslint": "^
|
|
53
|
+
"@types/node": "^25.9.3",
|
|
54
|
+
"eslint": "^10.5.0",
|
|
58
55
|
"jest": "^30.2.0",
|
|
59
56
|
"prettier": "^3.6.2",
|
|
60
57
|
"rimraf": "^6.0.1",
|
|
61
58
|
"ts-jest": "^29.4.4",
|
|
62
59
|
"ts-node": "^10.9.2",
|
|
63
|
-
"typescript": "^
|
|
60
|
+
"typescript": "^6.0.3"
|
|
64
61
|
},
|
|
65
62
|
"keywords": [
|
|
66
63
|
"ai",
|