npm-ai-hooks 1.0.2 → 2.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 +1 -1
- package/Readme.md +379 -137
- package/dist/cjs/index.js +14 -0
- package/dist/cjs/providers/base/BaseProvider.js +121 -0
- package/dist/cjs/providers/base/ProviderConfig.js +118 -0
- package/dist/cjs/providers/base/ProviderConfigs.js +185 -0
- package/dist/cjs/providers/base/ProviderRegistry.js +47 -0
- package/dist/cjs/providers/base/SpecializedProviders.js +58 -0
- package/dist/cjs/providers/index.js +82 -0
- package/dist/cjs/providers/init.js +90 -0
- package/dist/{wrap.js → cjs/wrap.js} +43 -12
- package/dist/esm/errors.js +17 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/providers/base/BaseProvider.js +114 -0
- package/dist/esm/providers/base/ProviderConfig.js +114 -0
- package/dist/esm/providers/base/ProviderConfigs.js +182 -0
- package/dist/esm/providers/base/ProviderRegistry.js +43 -0
- package/dist/esm/providers/base/SpecializedProviders.js +52 -0
- package/dist/esm/providers/index.js +75 -0
- package/dist/esm/providers/init.js +81 -0
- package/dist/esm/types/claude.js +4 -0
- package/dist/esm/types/core/providers.js +1 -0
- package/dist/esm/types/deepseek.js +4 -0
- package/dist/esm/types/gemini.js +4 -0
- package/dist/esm/types/groq.js +1 -0
- package/dist/esm/types/index.js +20 -0
- package/dist/esm/types/mistral.js +4 -0
- package/dist/esm/types/openai.js +4 -0
- package/dist/esm/types/openrouter.js +1 -0
- package/dist/esm/types/perplexity.js +4 -0
- package/dist/esm/types/xai.js +4 -0
- package/dist/esm/wrap.js +113 -0
- package/dist/index.d.ts +1 -0
- package/dist/providers/base/BaseProvider.d.ts +33 -0
- package/dist/providers/base/ProviderConfig.d.ts +28 -0
- package/dist/providers/base/ProviderConfigs.d.ts +31 -0
- package/dist/providers/base/ProviderRegistry.d.ts +12 -0
- package/dist/providers/base/SpecializedProviders.d.ts +68 -0
- package/dist/providers/index.d.ts +4 -0
- package/dist/providers/init.d.ts +58 -0
- package/package.json +50 -12
- package/dist/index.js +0 -6
- package/dist/providers/claude.d.ts +0 -2
- package/dist/providers/claude.js +0 -60
- package/dist/providers/deepkseek.d.ts +0 -2
- package/dist/providers/deepkseek.js +0 -57
- package/dist/providers/gemini.d.ts +0 -2
- package/dist/providers/gemini.js +0 -58
- package/dist/providers/groq.d.ts +0 -2
- package/dist/providers/groq.js +0 -57
- package/dist/providers/index.js +0 -75
- package/dist/providers/mistral.d.ts +0 -2
- package/dist/providers/mistral.js +0 -57
- package/dist/providers/openai.d.ts +0 -2
- package/dist/providers/openai.js +0 -57
- package/dist/providers/openrouter.d.ts +0 -1
- package/dist/providers/openrouter.js +0 -55
- package/dist/providers/perplexity.d.ts +0 -2
- package/dist/providers/perplexity.js +0 -57
- package/dist/providers/xai.d.ts +0 -2
- package/dist/providers/xai.js +0 -57
- /package/dist/{errors.js → cjs/errors.js} +0 -0
- /package/dist/{types → cjs/types}/claude.js +0 -0
- /package/dist/{types → cjs/types}/core/providers.js +0 -0
- /package/dist/{types → cjs/types}/deepseek.js +0 -0
- /package/dist/{types → cjs/types}/gemini.js +0 -0
- /package/dist/{types → cjs/types}/groq.js +0 -0
- /package/dist/{types → cjs/types}/index.js +0 -0
- /package/dist/{types → cjs/types}/mistral.js +0 -0
- /package/dist/{types → cjs/types}/openai.js +0 -0
- /package/dist/{types → cjs/types}/openrouter.js +0 -0
- /package/dist/{types → cjs/types}/perplexity.js +0 -0
- /package/dist/{types → cjs/types}/xai.js +0 -0
package/LICENSE
CHANGED
package/Readme.md
CHANGED
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
# npm-ai-hooks
|
|
1
|
+
# npm-ai-hooks
|
|
2
2
|
|
|
3
|
-
**Universal AI Hook Layer for Node.js – one wrapper for all AI providers.**
|
|
4
|
-
Inject LLM-like behavior into any JavaScript or TypeScript function with a single line, without writing prompts, handling SDKs, or locking into any provider.
|
|
3
|
+
**Universal AI Hook Layer for Node.js and React – one wrapper for all AI providers.**
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
Inject LLM-like behavior into any JavaScript or TypeScript function with a single line, without writing prompts, handling SDKs, or locking into any provider. Works seamlessly in both Node.js (Express) and React (Vite) environments.
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
---
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
* **Universal API:** Works with OpenAI, Claude, Gemini, DeepSeek, Groq, OpenRouter, XAI, Perplexity, and Mistral — out of the box.
|
|
12
|
+
* **Cross-Platform:** Works in both Node.js (Express) and React (Vite) environments with dual build system.
|
|
13
|
+
* **Plug & Play:** Wrap any function and instantly give it AI-powered behavior.
|
|
14
|
+
* **Zero Prompting:** Built-in task templates (summarize, explain, translate, sentiment, rewrite, code-review, etc.)
|
|
15
|
+
* **Explicit Configuration:** No environment variables needed - initialize providers explicitly with API keys.
|
|
16
|
+
* **Auto Provider Selection:** Smart fallback system with configurable preferences.
|
|
17
|
+
* **Type Safe:** Full TypeScript support with IntelliSense and type checking.
|
|
18
|
+
* **Error Safe:** Handles invalid keys, unauthorized models, rate limits, and more gracefully.
|
|
19
|
+
* **Dynamic Management:** Add/remove providers at runtime.
|
|
20
|
+
* **Cost Awareness:** Estimate and log token usage and cost before and after calls.
|
|
21
|
+
* **Caching:** Prevents duplicate calls and charges by caching results intelligently.
|
|
22
|
+
* **Extensible:** Add your own providers and custom tasks easily.
|
|
23
|
+
* **Debug Friendly:** Full debug logging with `AI_HOOK_DEBUG=true`.
|
|
20
24
|
|
|
21
25
|
---
|
|
22
26
|
|
|
23
|
-
##
|
|
27
|
+
## Installation
|
|
24
28
|
|
|
25
29
|
```bash
|
|
26
30
|
npm install npm-ai-hooks
|
|
@@ -30,59 +34,183 @@ yarn add npm-ai-hooks
|
|
|
30
34
|
|
|
31
35
|
---
|
|
32
36
|
|
|
33
|
-
##
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
### 1. Initialize Providers
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { initAIHooks, wrap } from "npm-ai-hooks";
|
|
34
43
|
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
// Initialize with your API keys
|
|
45
|
+
initAIHooks({
|
|
46
|
+
providers: [
|
|
47
|
+
{ provider: 'openai', key: 'sk-your-openai-key-here' },
|
|
48
|
+
{ provider: 'claude', key: 'sk-ant-your-claude-key-here' },
|
|
49
|
+
{ provider: 'groq', key: 'gsk_your-groq-key-here' }
|
|
50
|
+
],
|
|
51
|
+
defaultProvider: 'openai' // optional
|
|
52
|
+
});
|
|
53
|
+
```
|
|
37
54
|
|
|
38
|
-
|
|
39
|
-
const summarize = ai.wrap(text => text, { task: "summarize" });
|
|
55
|
+
### 2. Wrap Any Function
|
|
40
56
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
57
|
+
```typescript
|
|
58
|
+
// Wrap any function with AI behavior
|
|
59
|
+
const summarize = wrap((text: string) => text, { task: "summarize" });
|
|
60
|
+
|
|
61
|
+
// Use it
|
|
62
|
+
const result = await summarize("Node.js is a JavaScript runtime built on Chrome's V8...");
|
|
63
|
+
console.log(result); // "Node.js is a JS runtime for building server-side apps."
|
|
45
64
|
```
|
|
46
65
|
|
|
47
66
|
---
|
|
48
67
|
|
|
49
|
-
##
|
|
68
|
+
## 🔧 Provider Initialization
|
|
69
|
+
|
|
70
|
+
### Basic Setup
|
|
50
71
|
|
|
51
|
-
|
|
72
|
+
```typescript
|
|
73
|
+
import { initAIHooks } from "npm-ai-hooks";
|
|
52
74
|
|
|
75
|
+
initAIHooks({
|
|
76
|
+
providers: [
|
|
77
|
+
{ provider: 'openai', key: 'sk-...' },
|
|
78
|
+
{ provider: 'claude', key: 'sk-ant-...' }
|
|
79
|
+
]
|
|
80
|
+
});
|
|
53
81
|
```
|
|
54
|
-
AI_HOOK_OPENAI_KEY=sk-...
|
|
55
|
-
AI_HOOK_CLAUDE_KEY=sk-...
|
|
56
|
-
AI_HOOK_GEMINI_KEY=AIza...
|
|
57
|
-
AI_HOOK_DEEPSEEK_KEY=ds-...
|
|
58
|
-
AI_HOOK_GROQ_KEY=gr-...
|
|
59
82
|
|
|
60
|
-
|
|
83
|
+
### Advanced Setup with Custom Models
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
initAIHooks({
|
|
87
|
+
providers: [
|
|
88
|
+
{
|
|
89
|
+
provider: 'openai',
|
|
90
|
+
key: 'sk-...',
|
|
91
|
+
defaultModel: 'gpt-4' // custom default model
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
provider: 'claude',
|
|
95
|
+
key: 'sk-ant-...',
|
|
96
|
+
defaultModel: 'claude-3-sonnet-20240229'
|
|
97
|
+
}
|
|
98
|
+
],
|
|
99
|
+
defaultProvider: 'openai' // preferred provider
|
|
100
|
+
});
|
|
61
101
|
```
|
|
62
102
|
|
|
63
|
-
|
|
103
|
+
### Dynamic Provider Management
|
|
64
104
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
105
|
+
```typescript
|
|
106
|
+
import { addProvider, removeProvider, getAvailableProviders } from "npm-ai-hooks";
|
|
107
|
+
|
|
108
|
+
// Add providers after initialization
|
|
109
|
+
addProvider({
|
|
110
|
+
provider: 'mistral',
|
|
111
|
+
key: '...',
|
|
112
|
+
defaultModel: 'mistral-large'
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Remove providers
|
|
116
|
+
removeProvider('mistral');
|
|
117
|
+
|
|
118
|
+
// Check available providers
|
|
119
|
+
console.log(getAvailableProviders()); // ['openai', 'claude', 'groq', 'mistral']
|
|
120
|
+
```
|
|
68
121
|
|
|
69
122
|
---
|
|
70
123
|
|
|
71
|
-
##
|
|
124
|
+
## React/Vite Support
|
|
125
|
+
|
|
126
|
+
The library works seamlessly in React applications with Vite. The dual build system automatically provides the correct module format.
|
|
127
|
+
|
|
128
|
+
### React Setup
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
// App.tsx
|
|
132
|
+
import { useState, useEffect } from 'react';
|
|
133
|
+
import { initAIHooks, wrap } from 'npm-ai-hooks';
|
|
134
|
+
|
|
135
|
+
function App() {
|
|
136
|
+
const [isInitialized, setIsInitialized] = useState(false);
|
|
137
|
+
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
// Initialize with Vite environment variables (VITE_ prefix required)
|
|
140
|
+
initAIHooks({
|
|
141
|
+
providers: [
|
|
142
|
+
{ provider: 'openai', key: import.meta.env.VITE_OPENAI_KEY },
|
|
143
|
+
{ provider: 'groq', key: import.meta.env.VITE_GROQ_KEY },
|
|
144
|
+
{ provider: 'claude', key: import.meta.env.VITE_CLAUDE_KEY }
|
|
145
|
+
],
|
|
146
|
+
defaultProvider: 'groq'
|
|
147
|
+
});
|
|
148
|
+
setIsInitialized(true);
|
|
149
|
+
}, []);
|
|
150
|
+
|
|
151
|
+
const handleSummarize = async () => {
|
|
152
|
+
const summarize = wrap((text: string) => text, { task: "summarize" });
|
|
153
|
+
const result = await summarize("Your text here...");
|
|
154
|
+
console.log(result.output);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
return (
|
|
158
|
+
<div>
|
|
159
|
+
<button onClick={handleSummarize} disabled={!isInitialized}>
|
|
160
|
+
Summarize Text
|
|
161
|
+
</button>
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
```
|
|
72
166
|
|
|
73
|
-
###
|
|
167
|
+
### Vite Configuration
|
|
74
168
|
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
|
|
169
|
+
```typescript
|
|
170
|
+
// vite.config.ts
|
|
171
|
+
import { defineConfig } from 'vite'
|
|
172
|
+
import react from '@vitejs/plugin-react'
|
|
173
|
+
|
|
174
|
+
export default defineConfig({
|
|
175
|
+
plugins: [react()],
|
|
176
|
+
server: {
|
|
177
|
+
fs: {
|
|
178
|
+
allow: ['..', '../..'] // Allow access to parent directories for local library
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
})
|
|
78
182
|
```
|
|
79
183
|
|
|
80
184
|
---
|
|
81
185
|
|
|
82
|
-
|
|
186
|
+
## Usage Examples
|
|
187
|
+
|
|
188
|
+
### 1. Basic Tasks
|
|
83
189
|
|
|
84
|
-
```
|
|
85
|
-
|
|
190
|
+
```typescript
|
|
191
|
+
import { wrap } from "npm-ai-hooks";
|
|
192
|
+
|
|
193
|
+
// Summarization
|
|
194
|
+
const summarize = wrap((text: string) => text, { task: "summarize" });
|
|
195
|
+
console.log(await summarize("Long article text..."));
|
|
196
|
+
|
|
197
|
+
// Translation
|
|
198
|
+
const translate = wrap((text: string) => text, {
|
|
199
|
+
task: "translate",
|
|
200
|
+
targetLanguage: "spanish"
|
|
201
|
+
});
|
|
202
|
+
console.log(await translate("Hello world"));
|
|
203
|
+
|
|
204
|
+
// Code Review
|
|
205
|
+
const codeReview = wrap((code: string) => code, { task: "codeReview" });
|
|
206
|
+
console.log(await codeReview("function add(a, b) { return a + b; }"));
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### 2. Provider-Specific Usage
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
// Use specific provider
|
|
213
|
+
const explain = wrap((text: string) => text, {
|
|
86
214
|
task: "explain",
|
|
87
215
|
provider: "claude",
|
|
88
216
|
model: "claude-3-opus"
|
|
@@ -91,58 +219,94 @@ const explain = ai.wrap(t => t, {
|
|
|
91
219
|
console.log(await explain("Explain quantum computing like I'm 10."));
|
|
92
220
|
```
|
|
93
221
|
|
|
94
|
-
---
|
|
95
|
-
|
|
96
222
|
### 3. AI Pipelines
|
|
97
223
|
|
|
98
|
-
```
|
|
99
|
-
const summarize =
|
|
100
|
-
const translate =
|
|
224
|
+
```typescript
|
|
225
|
+
const summarize = wrap((t: string) => t, { task: "summarize" });
|
|
226
|
+
const translate = wrap((t: string) => t, { task: "translate", targetLanguage: "fr" });
|
|
101
227
|
|
|
228
|
+
// Chain operations
|
|
102
229
|
const result = await translate(await summarize("Long technical article..."));
|
|
103
|
-
console.log(result
|
|
230
|
+
console.log(result); // Résumé en français
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### 4. Error Handling
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
try {
|
|
237
|
+
const result = await summarize("Some text");
|
|
238
|
+
} catch (error) {
|
|
239
|
+
console.error(error);
|
|
240
|
+
/*
|
|
241
|
+
{
|
|
242
|
+
code: "INVALID_API_KEY",
|
|
243
|
+
message: "Invalid OpenAI API key: ...",
|
|
244
|
+
provider: "openai",
|
|
245
|
+
suggestion: "Verify your API key"
|
|
246
|
+
}
|
|
247
|
+
*/
|
|
248
|
+
}
|
|
104
249
|
```
|
|
105
250
|
|
|
106
251
|
---
|
|
107
252
|
|
|
108
|
-
|
|
253
|
+
## 🎯 Built-in Tasks
|
|
109
254
|
|
|
110
|
-
| Task | Description |
|
|
111
|
-
| ------------ | ---------------------------------------- |
|
|
112
|
-
| `summarize` | Summarize text into concise form |
|
|
113
|
-
| `translate` | Translate text to a target language |
|
|
114
|
-
| `explain` | Explain complex text simply |
|
|
115
|
-
| `rewrite` | Rephrase text for tone/clarity |
|
|
116
|
-
| `sentiment` | Analyze emotional tone of text |
|
|
117
|
-
| `codeReview` | Review code and provide feedback |
|
|
118
|
-
| `docstring` | Generate function documentation comments |
|
|
255
|
+
| Task | Description | Example |
|
|
256
|
+
| ------------ | ---------------------------------------- | ------- |
|
|
257
|
+
| `summarize` | Summarize text into concise form | `wrap(fn, { task: "summarize" })` |
|
|
258
|
+
| `translate` | Translate text to a target language | `wrap(fn, { task: "translate", targetLanguage: "es" })` |
|
|
259
|
+
| `explain` | Explain complex text simply | `wrap(fn, { task: "explain" })` |
|
|
260
|
+
| `rewrite` | Rephrase text for tone/clarity | `wrap(fn, { task: "rewrite" })` |
|
|
261
|
+
| `sentiment` | Analyze emotional tone of text | `wrap(fn, { task: "sentiment" })` |
|
|
262
|
+
| `codeReview` | Review code and provide feedback | `wrap(fn, { task: "codeReview" })` |
|
|
119
263
|
|
|
120
264
|
---
|
|
121
265
|
|
|
122
|
-
##
|
|
266
|
+
## 🤖 Supported Providers
|
|
123
267
|
|
|
124
|
-
|
|
268
|
+
| Provider | Key Format Example | Default Model |
|
|
269
|
+
| ----------- | ------------------------- | ----------------------- |
|
|
270
|
+
| OpenRouter | `sk-or-...` | `openai/gpt-4o-mini` |
|
|
271
|
+
| Groq | `gsk_...` | `llama-3.1-70b-versatile` |
|
|
272
|
+
| OpenAI | `sk-...` | `gpt-4o` |
|
|
273
|
+
| Gemini | `AIza...` | `gemini-1.5-flash` |
|
|
274
|
+
| Claude | `sk-ant-...` | `claude-3-5-sonnet-20241022` |
|
|
275
|
+
| DeepSeek | `ds-...` | `deepseek-chat` |
|
|
276
|
+
| XAI | `xai-...` | `grok-2-1212` |
|
|
277
|
+
| Perplexity | `pplx-...` | `sonar` |
|
|
278
|
+
| Mistral | `mistral-...` | `mistral-large-latest` |
|
|
125
279
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## ⚙️ Provider Selection Logic
|
|
129
283
|
|
|
130
|
-
|
|
131
|
-
* TTL defaults to 24h (configurable).
|
|
132
|
-
* Clear manually with:
|
|
284
|
+
The system follows this priority order:
|
|
133
285
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
286
|
+
1. **User-specified provider** (if available)
|
|
287
|
+
2. **Default provider** (if set during initialization)
|
|
288
|
+
3. **OpenRouter** (if available)
|
|
289
|
+
4. **First provider** in the initialization list
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
// Example: OpenRouter will be selected if available
|
|
293
|
+
initAIHooks({
|
|
294
|
+
providers: [
|
|
295
|
+
{ provider: 'groq', key: '...' },
|
|
296
|
+
{ provider: 'openrouter', key: '...' }, // This will be preferred
|
|
297
|
+
{ provider: 'openai', key: '...' }
|
|
298
|
+
]
|
|
299
|
+
});
|
|
300
|
+
```
|
|
137
301
|
|
|
138
302
|
---
|
|
139
303
|
|
|
140
|
-
|
|
304
|
+
## 🔍 Advanced Configuration
|
|
141
305
|
|
|
142
|
-
|
|
306
|
+
### Cost Awareness
|
|
143
307
|
|
|
144
|
-
```
|
|
145
|
-
const summarize =
|
|
308
|
+
```typescript
|
|
309
|
+
const summarize = wrap((t: string) => t, { task: "summarize" });
|
|
146
310
|
const result = await summarize(longText);
|
|
147
311
|
|
|
148
312
|
console.log(result.meta);
|
|
@@ -160,40 +324,22 @@ console.log(result.meta);
|
|
|
160
324
|
*/
|
|
161
325
|
```
|
|
162
326
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
### Error Handling
|
|
166
|
-
|
|
167
|
-
All errors follow a unified structure:
|
|
327
|
+
### Caching
|
|
168
328
|
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
/*
|
|
175
|
-
{
|
|
176
|
-
code: "MODEL_NOT_ALLOWED",
|
|
177
|
-
message: "Your API key does not have access to gpt-4o",
|
|
178
|
-
provider: "openai",
|
|
179
|
-
suggestion: "Upgrade your plan or choose another model."
|
|
180
|
-
}
|
|
181
|
-
*/
|
|
182
|
-
}
|
|
329
|
+
```typescript
|
|
330
|
+
const summarize = wrap((t: string) => t, {
|
|
331
|
+
task: "summarize",
|
|
332
|
+
cache: true // Enable caching
|
|
333
|
+
});
|
|
183
334
|
```
|
|
184
335
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
### Debugging
|
|
188
|
-
|
|
189
|
-
Enable verbose logs:
|
|
336
|
+
### Debug Mode
|
|
190
337
|
|
|
191
|
-
```
|
|
338
|
+
```bash
|
|
192
339
|
AI_HOOK_DEBUG=true
|
|
193
340
|
```
|
|
194
341
|
|
|
195
|
-
Output
|
|
196
|
-
|
|
342
|
+
Output:
|
|
197
343
|
```
|
|
198
344
|
[ai-hooks] Using provider: OpenAI (gpt-4o)
|
|
199
345
|
[ai-hooks] Estimated cost: $0.0012
|
|
@@ -203,52 +349,127 @@ Output example:
|
|
|
203
349
|
|
|
204
350
|
---
|
|
205
351
|
|
|
206
|
-
##
|
|
352
|
+
## 🔄 Migration from v1.x
|
|
207
353
|
|
|
208
|
-
|
|
354
|
+
### Old Way (v1.x)
|
|
355
|
+
```typescript
|
|
356
|
+
// Set environment variables
|
|
357
|
+
process.env.AI_HOOK_OPENAI_KEY = 'sk-...';
|
|
209
358
|
|
|
210
|
-
|
|
211
|
-
ai
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
359
|
+
// Use providers
|
|
360
|
+
import { getProvider } from 'npm-ai-hooks';
|
|
361
|
+
const { fn } = getProvider();
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### New Way (v2.0)
|
|
365
|
+
```typescript
|
|
366
|
+
// Initialize providers explicitly
|
|
367
|
+
import { initAIHooks, getProvider } from 'npm-ai-hooks';
|
|
368
|
+
|
|
369
|
+
initAIHooks({
|
|
370
|
+
providers: [
|
|
371
|
+
{ provider: 'openai', key: 'sk-...' }
|
|
372
|
+
]
|
|
218
373
|
});
|
|
374
|
+
|
|
375
|
+
// Use providers (same API)
|
|
376
|
+
const { fn } = getProvider();
|
|
219
377
|
```
|
|
220
378
|
|
|
379
|
+
### Benefits of Migration
|
|
380
|
+
|
|
381
|
+
- ✅ **No Environment Dependencies** - Cleaner, more explicit configuration
|
|
382
|
+
- ✅ **Better Security** - No accidental exposure of environment variables
|
|
383
|
+
- ✅ **Type Safety** - Full TypeScript support for provider configuration
|
|
384
|
+
- ✅ **Dynamic Management** - Add/remove providers at runtime
|
|
385
|
+
- ✅ **Custom Models** - Specify default models per provider
|
|
386
|
+
- ✅ **Smaller Bundle** - 77% reduction in code size
|
|
387
|
+
|
|
221
388
|
---
|
|
222
389
|
|
|
223
|
-
##
|
|
390
|
+
## 🧪 Development Setup
|
|
224
391
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
392
|
+
For contributors and developers:
|
|
393
|
+
|
|
394
|
+
```bash
|
|
395
|
+
# Clone the repository
|
|
396
|
+
git clone https://github.com/iTeebot/npm-ai-hooks.git
|
|
397
|
+
cd npm-ai-hooks
|
|
398
|
+
|
|
399
|
+
# Setup development environment
|
|
400
|
+
npm run setup:dev
|
|
401
|
+
|
|
402
|
+
# Or on Windows (PowerShell - recommended)
|
|
403
|
+
npm run setup:dev:ps
|
|
404
|
+
|
|
405
|
+
# Or on Windows (Command Prompt)
|
|
406
|
+
npm run setup:dev:win
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
The setup script will:
|
|
410
|
+
- Use the correct Node.js version from `.nvmrc`
|
|
411
|
+
- Apply npm configuration from `.npmrc`
|
|
412
|
+
- Install dependencies
|
|
413
|
+
- Run tests to verify everything works
|
|
414
|
+
|
|
415
|
+
### Testing with Real API Keys
|
|
416
|
+
|
|
417
|
+
To test with real API keys (optional):
|
|
418
|
+
|
|
419
|
+
```bash
|
|
420
|
+
# 1. Copy the example environment file
|
|
421
|
+
cp .env.example .env
|
|
422
|
+
|
|
423
|
+
# 2. Add your API keys to .env
|
|
424
|
+
# Edit .env and add your actual API keys
|
|
425
|
+
|
|
426
|
+
# 3. Run tests with real API keys
|
|
427
|
+
npm run test:env
|
|
428
|
+
|
|
429
|
+
# 4. Or run all tests (includes both mock and real API tests)
|
|
430
|
+
npm test
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
### Testing Commands
|
|
434
|
+
|
|
435
|
+
```bash
|
|
436
|
+
# Run all tests (mock + real API if available)
|
|
437
|
+
npm test
|
|
438
|
+
|
|
439
|
+
# Run only mock tests (no API keys needed)
|
|
440
|
+
npm run test:mock
|
|
441
|
+
|
|
442
|
+
# Run only real API tests (requires API keys in .env)
|
|
443
|
+
npm run test:env
|
|
444
|
+
|
|
445
|
+
# Run specific test suites
|
|
446
|
+
npm run test:providers
|
|
447
|
+
npm run test:tasks
|
|
448
|
+
npm run test:errors
|
|
449
|
+
npm run test:integration
|
|
450
|
+
npm run test:performance
|
|
451
|
+
```
|
|
231
452
|
|
|
232
453
|
---
|
|
233
454
|
|
|
234
|
-
##
|
|
455
|
+
## 🏗️ Project Structure
|
|
235
456
|
|
|
236
457
|
```
|
|
237
458
|
npm-ai-hooks/
|
|
238
459
|
├─ src/
|
|
239
|
-
│ ├─ index.
|
|
240
|
-
│ ├─ wrap.
|
|
241
|
-
│ ├─
|
|
242
|
-
│ ├─ cost.js
|
|
243
|
-
│ ├─ errors.js
|
|
460
|
+
│ ├─ index.ts # Main exports
|
|
461
|
+
│ ├─ wrap.ts # Core wrapping functionality
|
|
462
|
+
│ ├─ errors.ts # Error handling
|
|
244
463
|
│ ├─ providers/
|
|
245
|
-
│ │ ├─
|
|
246
|
-
│ │ ├─
|
|
247
|
-
│ │ ├─
|
|
248
|
-
│ │ ├─
|
|
249
|
-
│ │
|
|
250
|
-
│ │ └─ index.
|
|
251
|
-
|
|
464
|
+
│ │ ├─ base/ # Base provider system
|
|
465
|
+
│ │ │ ├─ BaseProvider.ts # Abstract base class
|
|
466
|
+
│ │ │ ├─ ProviderConfig.ts # Provider configuration
|
|
467
|
+
│ │ │ ├─ ProviderRegistry.ts # Provider management
|
|
468
|
+
│ │ │ └─ ProviderConfigs.ts # Provider definitions
|
|
469
|
+
│ │ └─ index.ts # Provider exports
|
|
470
|
+
│ └─ types/ # TypeScript definitions
|
|
471
|
+
├─ examples/ # Usage examples
|
|
472
|
+
├─ tests/ # Test suite
|
|
252
473
|
├─ package.json
|
|
253
474
|
├─ README.md
|
|
254
475
|
└─ LICENSE
|
|
@@ -256,13 +477,34 @@ npm-ai-hooks/
|
|
|
256
477
|
|
|
257
478
|
---
|
|
258
479
|
|
|
480
|
+
## 🛣️ Roadmap
|
|
481
|
+
|
|
482
|
+
* [ ] Streaming output support
|
|
483
|
+
* [ ] Cost ceiling + auto-fallbacks
|
|
484
|
+
* [ ] Rate limiter
|
|
485
|
+
* [ ] Multi-turn conversation API
|
|
486
|
+
* [ ] Local model support (llama.cpp, Ollama)
|
|
487
|
+
* [ ] VSCode extension for code-gen
|
|
488
|
+
* [ ] Custom provider registration
|
|
489
|
+
* [ ] Advanced caching strategies
|
|
490
|
+
|
|
491
|
+
---
|
|
492
|
+
|
|
259
493
|
## 🤝 Contributing
|
|
260
494
|
|
|
261
|
-
Contributions, ideas, and feedback are welcome!
|
|
262
|
-
|
|
495
|
+
Contributions, ideas, and feedback are welcome! Please open an issue or submit a pull request.
|
|
496
|
+
|
|
497
|
+
---
|
|
498
|
+
|
|
499
|
+
## 📄 License
|
|
500
|
+
|
|
501
|
+
MIT © 2025 `npm-ai-hooks` Team
|
|
263
502
|
|
|
264
503
|
---
|
|
265
504
|
|
|
266
|
-
##
|
|
505
|
+
## 🔗 Links
|
|
267
506
|
|
|
268
|
-
|
|
507
|
+
- [GitHub Repository](https://github.com/iTeebot/npm-ai-hooks)
|
|
508
|
+
- [NPM Package](https://www.npmjs.com/package/npm-ai-hooks)
|
|
509
|
+
- [Documentation](https://github.com/iTeebot/npm-ai-hooks#readme)
|
|
510
|
+
- [Changelog](CHANGELOG.md)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.reset = exports.isInitialized = exports.getProvider = exports.getAvailableProviders = exports.removeProvider = exports.addProvider = exports.initAIHooks = exports.wrap = void 0;
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var wrap_1 = require("./wrap");
|
|
6
|
+
Object.defineProperty(exports, "wrap", { enumerable: true, get: function () { return wrap_1.wrap; } });
|
|
7
|
+
var providers_1 = require("./providers");
|
|
8
|
+
Object.defineProperty(exports, "initAIHooks", { enumerable: true, get: function () { return providers_1.initAIHooks; } });
|
|
9
|
+
Object.defineProperty(exports, "addProvider", { enumerable: true, get: function () { return providers_1.addProvider; } });
|
|
10
|
+
Object.defineProperty(exports, "removeProvider", { enumerable: true, get: function () { return providers_1.removeProvider; } });
|
|
11
|
+
Object.defineProperty(exports, "getAvailableProviders", { enumerable: true, get: function () { return providers_1.getAvailableProviders; } });
|
|
12
|
+
Object.defineProperty(exports, "getProvider", { enumerable: true, get: function () { return providers_1.getProvider; } });
|
|
13
|
+
Object.defineProperty(exports, "isInitialized", { enumerable: true, get: function () { return providers_1.isInitialized; } });
|
|
14
|
+
Object.defineProperty(exports, "reset", { enumerable: true, get: function () { return providers_1.reset; } });
|