npm-ai-hooks 2.0.0 → 2.0.2

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/Readme.md CHANGED
@@ -1,510 +1,612 @@
1
- # npm-ai-hooks
2
-
3
- **Universal AI Hook Layer for Node.js and React – one wrapper for all AI providers.**
4
-
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.
6
-
7
- ---
8
-
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`.
24
-
25
- ---
26
-
27
- ## Installation
28
-
29
- ```bash
30
- npm install npm-ai-hooks
31
- # or
32
- yarn add npm-ai-hooks
33
- ```
34
-
35
- ---
36
-
37
- ## Quick Start
38
-
39
- ### 1. Initialize Providers
40
-
41
- ```typescript
42
- import { initAIHooks, wrap } from "npm-ai-hooks";
43
-
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
- ```
54
-
55
- ### 2. Wrap Any Function
56
-
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."
64
- ```
65
-
66
- ---
67
-
68
- ## 🔧 Provider Initialization
69
-
70
- ### Basic Setup
71
-
72
- ```typescript
73
- import { initAIHooks } from "npm-ai-hooks";
74
-
75
- initAIHooks({
76
- providers: [
77
- { provider: 'openai', key: 'sk-...' },
78
- { provider: 'claude', key: 'sk-ant-...' }
79
- ]
80
- });
81
- ```
82
-
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
- });
101
- ```
102
-
103
- ### Dynamic Provider Management
104
-
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
- ```
121
-
122
- ---
123
-
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
- ```
166
-
167
- ### Vite Configuration
168
-
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
- })
182
- ```
183
-
184
- ---
185
-
186
- ## Usage Examples
187
-
188
- ### 1. Basic Tasks
189
-
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, {
214
- task: "explain",
215
- provider: "claude",
216
- model: "claude-3-opus"
217
- });
218
-
219
- console.log(await explain("Explain quantum computing like I'm 10."));
220
- ```
221
-
222
- ### 3. AI Pipelines
223
-
224
- ```typescript
225
- const summarize = wrap((t: string) => t, { task: "summarize" });
226
- const translate = wrap((t: string) => t, { task: "translate", targetLanguage: "fr" });
227
-
228
- // Chain operations
229
- const result = await translate(await summarize("Long technical article..."));
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
- }
249
- ```
250
-
251
- ---
252
-
253
- ## 🎯 Built-in Tasks
254
-
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" })` |
263
-
264
- ---
265
-
266
- ## 🤖 Supported Providers
267
-
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` |
279
-
280
- ---
281
-
282
- ## ⚙️ Provider Selection Logic
283
-
284
- The system follows this priority order:
285
-
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
- ```
301
-
302
- ---
303
-
304
- ## 🔍 Advanced Configuration
305
-
306
- ### Cost Awareness
307
-
308
- ```typescript
309
- const summarize = wrap((t: string) => t, { task: "summarize" });
310
- const result = await summarize(longText);
311
-
312
- console.log(result.meta);
313
- /*
314
- {
315
- provider: "openai",
316
- model: "gpt-4o",
317
- cached: false,
318
- estimatedCostUSD: 0.0013,
319
- totalCostUSD: 0.0012,
320
- inputTokens: 326,
321
- outputTokens: 127,
322
- latencyMs: 812
323
- }
324
- */
325
- ```
326
-
327
- ### Caching
328
-
329
- ```typescript
330
- const summarize = wrap((t: string) => t, {
331
- task: "summarize",
332
- cache: true // Enable caching
333
- });
334
- ```
335
-
336
- ### Debug Mode
337
-
338
- ```bash
339
- AI_HOOK_DEBUG=true
340
- ```
341
-
342
- Output:
343
- ```
344
- [ai-hooks] Using provider: OpenAI (gpt-4o)
345
- [ai-hooks] Estimated cost: $0.0012
346
- [ai-hooks] Cache: MISS
347
- [ai-hooks] Response received in 812ms
348
- ```
349
-
350
- ---
351
-
352
- ## 🔄 Migration from v1.x
353
-
354
- ### Old Way (v1.x)
355
- ```typescript
356
- // Set environment variables
357
- process.env.AI_HOOK_OPENAI_KEY = 'sk-...';
358
-
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
- ]
373
- });
374
-
375
- // Use providers (same API)
376
- const { fn } = getProvider();
377
- ```
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
-
388
- ---
389
-
390
- ## 🧪 Development Setup
391
-
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
- ```
452
-
453
- ---
454
-
455
- ## 🏗️ Project Structure
456
-
457
- ```
458
- npm-ai-hooks/
459
- ├─ src/
460
- │ ├─ index.ts # Main exports
461
- │ ├─ wrap.ts # Core wrapping functionality
462
- │ ├─ errors.ts # Error handling
463
- │ ├─ providers/
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
473
- ├─ package.json
474
- ├─ README.md
475
- └─ LICENSE
476
- ```
477
-
478
- ---
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
-
493
- ## 🤝 Contributing
494
-
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
502
-
503
- ---
504
-
505
- ## 🔗 Links
506
-
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)
1
+ # npm-ai-hooks
2
+
3
+ **Universal AI Hook Layer for Node.js and React – one wrapper for all AI providers.**
4
+
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.
6
+
7
+ 💻 **[Node.js Example](./examples/demo.ts)**
8
+
9
+ ---
10
+
11
+ ## Features
12
+
13
+ * **Universal API:** Works with OpenAI, Claude, Gemini, DeepSeek, Groq, OpenRouter, XAI, Perplexity, and Mistral out of the box.
14
+ * **Cross-Platform:** Works in both Node.js (Express) and React (Vite) environments with dual build system.
15
+ * **Multimodal Support:** 🆕 Images, files, and voice input support with vision-enabled models.
16
+ * **Plug & Play:** Wrap any function and instantly give it AI-powered behavior.
17
+ * **Zero Prompting:** Built-in task templates (summarize, explain, translate, sentiment, rewrite, code-review, etc.)
18
+ * **Explicit Configuration:** No environment variables needed - initialize providers explicitly with API keys.
19
+ * **Auto Provider Selection:** Smart fallback system with configurable preferences.
20
+ * **Type Safe:** Full TypeScript support with IntelliSense and type checking.
21
+ * **Error Safe:** Handles invalid keys, unauthorized models, rate limits, and more gracefully.
22
+ * **Dynamic Management:** Add/remove providers at runtime.
23
+ * **Cost Awareness:** Estimate and log token usage and cost before and after calls.
24
+ * **Caching:** Prevents duplicate calls and charges by caching results intelligently.
25
+ * **Extensible:** Add your own providers and custom tasks easily.
26
+ * **Debug Friendly:** Full debug logging with `DEBUG=true`.
27
+
28
+ ---
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ npm install npm-ai-hooks
34
+ # or
35
+ yarn add npm-ai-hooks
36
+ ```
37
+
38
+ ---
39
+
40
+ ## Quick Start
41
+
42
+ ### 1. Initialize Providers
43
+
44
+ ```typescript
45
+ import { initAIHooks, wrap } from "npm-ai-hooks";
46
+
47
+ // Initialize with your API keys
48
+ initAIHooks({
49
+ providers: [
50
+ { provider: 'openai', key: 'sk-your-openai-key-here' },
51
+ { provider: 'claude', key: 'sk-ant-your-claude-key-here' },
52
+ { provider: 'groq', key: 'gsk_your-groq-key-here' }
53
+ ],
54
+ defaultProvider: 'openai' // optional
55
+ });
56
+ ```
57
+
58
+ ### 2. Wrap Any Function
59
+
60
+ ```typescript
61
+ // Wrap any function with AI behavior
62
+ const summarize = wrap((text: string) => text, { task: "summarize" });
63
+
64
+ // Use it
65
+ const result = await summarize("Node.js is a JavaScript runtime built on Chrome's V8...");
66
+ console.log(result); // "Node.js is a JS runtime for building server-side apps."
67
+ ```
68
+
69
+ ---
70
+
71
+ ## 🔧 Provider Initialization
72
+
73
+ ### Basic Setup
74
+
75
+ ```typescript
76
+ import { initAIHooks } from "npm-ai-hooks";
77
+
78
+ initAIHooks({
79
+ providers: [
80
+ { provider: 'openai', key: 'sk-...' },
81
+ { provider: 'claude', key: 'sk-ant-...' }
82
+ ]
83
+ });
84
+ ```
85
+
86
+ ### Advanced Setup with Custom Models
87
+
88
+ ```typescript
89
+ initAIHooks({
90
+ providers: [
91
+ {
92
+ provider: 'openai',
93
+ key: 'sk-...',
94
+ defaultModel: 'gpt-4' // custom default model
95
+ },
96
+ {
97
+ provider: 'claude',
98
+ key: 'sk-ant-...',
99
+ defaultModel: 'claude-3-sonnet-20240229'
100
+ }
101
+ ],
102
+ defaultProvider: 'openai' // preferred provider
103
+ });
104
+ ```
105
+
106
+ ### Dynamic Provider Management
107
+
108
+ ```typescript
109
+ import { addProvider, removeProvider, getAvailableProviders } from "npm-ai-hooks";
110
+
111
+ // Add providers after initialization
112
+ addProvider({
113
+ provider: 'mistral',
114
+ key: '...',
115
+ defaultModel: 'mistral-large'
116
+ });
117
+
118
+ // Remove providers
119
+ removeProvider('mistral');
120
+
121
+ // Check available providers
122
+ console.log(getAvailableProviders()); // ['openai', 'claude', 'groq', 'mistral']
123
+ ```
124
+
125
+ ---
126
+
127
+ ## React/Vite Support
128
+
129
+ The library works seamlessly in React applications with Vite. The dual build system automatically provides the correct module format.
130
+
131
+ ### React Setup
132
+
133
+ ```typescript
134
+ // App.tsx
135
+ import { useState, useEffect } from 'react';
136
+ import { initAIHooks, wrap } from 'npm-ai-hooks';
137
+
138
+ function App() {
139
+ const [isInitialized, setIsInitialized] = useState(false);
140
+
141
+ useEffect(() => {
142
+ // Initialize with Vite environment variables (VITE_ prefix required)
143
+ initAIHooks({
144
+ providers: [
145
+ { provider: 'openai', key: import.meta.env.VITE_OPENAI_KEY },
146
+ { provider: 'groq', key: import.meta.env.VITE_GROQ_KEY },
147
+ { provider: 'claude', key: import.meta.env.VITE_CLAUDE_KEY }
148
+ ],
149
+ defaultProvider: 'groq'
150
+ });
151
+ setIsInitialized(true);
152
+ }, []);
153
+
154
+ const handleSummarize = async () => {
155
+ const summarize = wrap((text: string) => text, { task: "summarize" });
156
+ const result = await summarize("Your text here...");
157
+ console.log(result.output);
158
+ };
159
+
160
+ return (
161
+ <div>
162
+ <button onClick={handleSummarize} disabled={!isInitialized}>
163
+ Summarize Text
164
+ </button>
165
+ </div>
166
+ );
167
+ }
168
+ ```
169
+
170
+ ### Vite Configuration
171
+
172
+ ```typescript
173
+ // vite.config.ts
174
+ import { defineConfig } from 'vite'
175
+ import react from '@vitejs/plugin-react'
176
+
177
+ export default defineConfig({
178
+ plugins: [react()],
179
+ server: {
180
+ fs: {
181
+ allow: ['..', '../..'] // Allow access to parent directories for local library
182
+ }
183
+ }
184
+ })
185
+ ```
186
+
187
+ ---
188
+
189
+ ## Usage Examples
190
+
191
+ ### 1. Basic Tasks
192
+
193
+ ```typescript
194
+ import { wrap } from "npm-ai-hooks";
195
+
196
+ // Summarization
197
+ const summarize = wrap((text: string) => text, { task: "summarize" });
198
+ console.log(await summarize("Long article text..."));
199
+
200
+ // Translation
201
+ const translate = wrap((text: string) => text, {
202
+ task: "translate",
203
+ targetLanguage: "spanish"
204
+ });
205
+ console.log(await translate("Hello world"));
206
+
207
+ // Code Review
208
+ const codeReview = wrap((code: string) => code, { task: "codeReview" });
209
+ console.log(await codeReview("function add(a, b) { return a + b; }"));
210
+ ```
211
+
212
+ ### 2. Provider-Specific Usage
213
+
214
+ ```typescript
215
+ // Use specific provider
216
+ const explain = wrap((text: string) => text, {
217
+ task: "explain",
218
+ provider: "claude",
219
+ model: "claude-3-opus"
220
+ });
221
+
222
+ console.log(await explain("Explain quantum computing like I'm 10."));
223
+ ```
224
+
225
+ ### 3. AI Pipelines
226
+
227
+ ```typescript
228
+ const summarize = wrap((t: string) => t, { task: "summarize" });
229
+ const translate = wrap((t: string) => t, { task: "translate", targetLanguage: "fr" });
230
+
231
+ // Chain operations
232
+ const result = await translate(await summarize("Long technical article..."));
233
+ console.log(result); // Résumé en français
234
+ ```
235
+
236
+ ### 4. Multimodal Support (Images, Files, Voice) 🎨🎤
237
+
238
+ **NEW!** npm-ai-hooks now supports multimodal inputs including images, files, and voice.
239
+
240
+ ```typescript
241
+ import { wrap, MultimodalInput } from "npm-ai-hooks";
242
+ import * as fs from 'fs';
243
+
244
+ // Image Analysis
245
+ const analyzeImage = wrap((input: MultimodalInput) => input, {
246
+ provider: 'openai',
247
+ model: 'gpt-4o', // Vision-enabled model
248
+ customPrompt: 'Describe what you see in this image'
249
+ });
250
+
251
+ // Load and encode image
252
+ const imageBuffer = fs.readFileSync('./photo.jpg');
253
+ const base64Image = `data:image/jpeg;base64,${imageBuffer.toString('base64')}`;
254
+
255
+ const result = await analyzeImage({
256
+ text: 'What is in this image?',
257
+ image: base64Image
258
+ });
259
+ console.log(result.output); // Detailed image description
260
+
261
+ // OCR (Text Extraction)
262
+ const extractText = wrap((input: MultimodalInput) => input, {
263
+ provider: 'openai',
264
+ model: 'gpt-4o',
265
+ customPrompt: 'Extract all text from this image'
266
+ });
267
+
268
+ const ocrResult = await extractText({
269
+ text: 'Extract text from this document',
270
+ image: base64Image
271
+ });
272
+
273
+ // File Processing
274
+ const analyzeFile = wrap((input: MultimodalInput) => input, {
275
+ provider: 'claude',
276
+ model: 'claude-3-opus',
277
+ task: 'summarize'
278
+ });
279
+
280
+ const fileBuffer = fs.readFileSync('./document.pdf');
281
+ const fileResult = await analyzeFile({
282
+ text: 'Summarize this document',
283
+ file: {
284
+ name: 'document.pdf',
285
+ data: `data:application/pdf;base64,${fileBuffer.toString('base64')}`,
286
+ type: 'application/pdf'
287
+ }
288
+ });
289
+ ```
290
+
291
+ **Browser Voice Input (Web Speech API):**
292
+ ```typescript
293
+ // In browser environment
294
+ const SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
295
+ const recognition = new SpeechRecognition();
296
+
297
+ recognition.onresult = async (event) => {
298
+ const transcript = event.results[0][0].transcript;
299
+
300
+ // Process voice input with AI
301
+ const explain = wrap((text: string) => text, { task: 'explain' });
302
+ const result = await explain(transcript);
303
+ console.log(result.output);
304
+ };
305
+
306
+ recognition.start();
307
+ ```
308
+
309
+ **Multimodal Interface:**
310
+ ```typescript
311
+ interface MultimodalInput {
312
+ text?: string; // Text content
313
+ image?: string; // Base64 encoded image (with data URI)
314
+ file?: { // File attachment
315
+ name: string; // File name
316
+ data: string; // Base64 encoded (with data URI)
317
+ type: string; // MIME type
318
+ };
319
+ }
320
+ ```
321
+
322
+ **Use Cases:**
323
+ - 📸 Image analysis and description
324
+ - 📄 OCR and document processing
325
+ - 🔍 Code review from screenshots
326
+ - 🎤 Voice commands and transcription
327
+ - 📊 Chart and diagram analysis
328
+ - 🏷️ Product image tagging
329
+
330
+ **See full examples:**
331
+ - [Vision Examples](./examples/multimodal/vision-example.ts)
332
+ - [Voice Examples](./examples/multimodal/voice-example.ts)
333
+ - [React Demo with UI](https://labs.iteebot.com/npm-packages/npm-ai-hooks/demo)
334
+
335
+ ### 5. Error Handling
336
+
337
+ ```typescript
338
+ try {
339
+ const result = await summarize("Some text");
340
+ } catch (error) {
341
+ console.error(error);
342
+ /*
343
+ {
344
+ code: "INVALID_API_KEY",
345
+ message: "Invalid OpenAI API key: ...",
346
+ provider: "openai",
347
+ suggestion: "Verify your API key"
348
+ }
349
+ */
350
+ }
351
+ ```
352
+
353
+ ---
354
+
355
+ ## 🎯 Built-in Tasks
356
+
357
+ | Task | Description | Example |
358
+ | ------------ | ---------------------------------------- | ------- |
359
+ | `summarize` | Summarize text into concise form | `wrap(fn, { task: "summarize" })` |
360
+ | `translate` | Translate text to a target language | `wrap(fn, { task: "translate", targetLanguage: "es" })` |
361
+ | `explain` | Explain complex text simply | `wrap(fn, { task: "explain" })` |
362
+ | `rewrite` | Rephrase text for tone/clarity | `wrap(fn, { task: "rewrite" })` |
363
+ | `sentiment` | Analyze emotional tone of text | `wrap(fn, { task: "sentiment" })` |
364
+ | `codeReview` | Review code and provide feedback | `wrap(fn, { task: "codeReview" })` |
365
+
366
+ ---
367
+
368
+ ## 🤖 Supported Providers
369
+
370
+ | Provider | Key Format Example | Default Model |
371
+ | ----------- | ------------------------- | ----------------------- |
372
+ | OpenRouter | `sk-or-...` | `openai/gpt-4o-mini` |
373
+ | Groq | `gsk_...` | `llama-3.1-70b-versatile` |
374
+ | OpenAI | `sk-...` | `gpt-4o` |
375
+ | Gemini | `AIza...` | `gemini-1.5-flash` |
376
+ | Claude | `sk-ant-...` | `claude-3-5-sonnet-20241022` |
377
+ | DeepSeek | `ds-...` | `deepseek-chat` |
378
+ | XAI | `xai-...` | `grok-2-1212` |
379
+ | Perplexity | `pplx-...` | `sonar` |
380
+ | Mistral | `mistral-...` | `mistral-large-latest` |
381
+
382
+ ---
383
+
384
+ ## ⚙️ Provider Selection Logic
385
+
386
+ The system follows this priority order:
387
+
388
+ 1. **User-specified provider** (if available)
389
+ 2. **Default provider** (if set during initialization)
390
+ 3. **OpenRouter** (if available)
391
+ 4. **First provider** in the initialization list
392
+
393
+ ```typescript
394
+ // Example: OpenRouter will be selected if available
395
+ initAIHooks({
396
+ providers: [
397
+ { provider: 'groq', key: '...' },
398
+ { provider: 'openrouter', key: '...' }, // This will be preferred
399
+ { provider: 'openai', key: '...' }
400
+ ]
401
+ });
402
+ ```
403
+
404
+ ---
405
+
406
+ ## 🔍 Advanced Configuration
407
+
408
+ ### Cost Awareness
409
+
410
+ ```typescript
411
+ const summarize = wrap((t: string) => t, { task: "summarize" });
412
+ const result = await summarize(longText);
413
+
414
+ console.log(result.meta);
415
+ /*
416
+ {
417
+ provider: "openai",
418
+ model: "gpt-4o",
419
+ cached: false,
420
+ estimatedCostUSD: 0.0013,
421
+ totalCostUSD: 0.0012,
422
+ inputTokens: 326,
423
+ outputTokens: 127,
424
+ latencyMs: 812
425
+ }
426
+ */
427
+ ```
428
+
429
+ ### Caching
430
+
431
+ ```typescript
432
+ const summarize = wrap((t: string) => t, {
433
+ task: "summarize",
434
+ cache: true // Enable caching
435
+ });
436
+ ```
437
+
438
+ ### Debug Mode
439
+
440
+ ```bash
441
+ DEBUG=true
442
+ ```
443
+
444
+ Output:
445
+ ```
446
+ [ai-hooks] Using provider: OpenAI (gpt-4o)
447
+ [ai-hooks] Estimated cost: $0.0012
448
+ [ai-hooks] Cache: MISS
449
+ [ai-hooks] Response received in 812ms
450
+ ```
451
+
452
+ ---
453
+
454
+ ## 🔄 Migration from v1.x
455
+
456
+ ### Old Way (v1.x)
457
+ ```typescript
458
+ // Set environment variables
459
+ process.env.OPENAI_KEY = 'sk-...';
460
+
461
+ // Use providers
462
+ import { getProvider } from 'npm-ai-hooks';
463
+ const { fn } = getProvider();
464
+ ```
465
+
466
+ ### New Way (v2.0)
467
+ ```typescript
468
+ // Initialize providers explicitly
469
+ import { initAIHooks, getProvider } from 'npm-ai-hooks';
470
+
471
+ initAIHooks({
472
+ providers: [
473
+ { provider: 'openai', key: 'sk-...' }
474
+ ]
475
+ });
476
+
477
+ // Use providers (same API)
478
+ const { fn } = getProvider();
479
+ ```
480
+
481
+ ### Benefits of Migration
482
+
483
+ - **No Environment Dependencies** - Cleaner, more explicit configuration
484
+ - **Better Security** - No accidental exposure of environment variables
485
+ - **Type Safety** - Full TypeScript support for provider configuration
486
+ - **Dynamic Management** - Add/remove providers at runtime
487
+ - **Custom Models** - Specify default models per provider
488
+ - **Smaller Bundle** - 77% reduction in code size
489
+
490
+ ---
491
+
492
+ ## 🧪 Development Setup
493
+
494
+ For contributors and developers:
495
+
496
+ ```bash
497
+ # Clone the repository
498
+ git clone https://github.com/iTeebot/npm-ai-hooks.git
499
+ cd npm-ai-hooks
500
+
501
+ # Setup development environment
502
+ npm run setup:dev
503
+
504
+ # Or on Windows (PowerShell - recommended)
505
+ npm run setup:dev:ps
506
+
507
+ # Or on Windows (Command Prompt)
508
+ npm run setup:dev:win
509
+ ```
510
+
511
+ The setup script will:
512
+ - Use the correct Node.js version from `.nvmrc`
513
+ - Apply npm configuration from `.npmrc`
514
+ - Install dependencies
515
+ - Run tests to verify everything works
516
+
517
+ ### Testing with Real API Keys
518
+
519
+ To test with real API keys (optional):
520
+
521
+ ```bash
522
+ # 1. Copy the example environment file
523
+ cp .env.example .env
524
+
525
+ # 2. Add your API keys to .env
526
+ # Edit .env and add your actual API keys
527
+
528
+ # 3. Run tests with real API keys
529
+ npm run test:env
530
+
531
+ # 4. Or run all tests (includes both mock and real API tests)
532
+ npm test
533
+ ```
534
+
535
+ ### Testing Commands
536
+
537
+ ```bash
538
+ # Run all tests (mock + real API if available)
539
+ npm test
540
+
541
+ # Run only mock tests (no API keys needed)
542
+ npm run test:mock
543
+
544
+ # Run only real API tests (requires API keys in .env)
545
+ npm run test:env
546
+
547
+ # Run specific test suites
548
+ npm run test:providers
549
+ npm run test:tasks
550
+ npm run test:errors
551
+ npm run test:integration
552
+ npm run test:performance
553
+ ```
554
+
555
+ ---
556
+
557
+ ## 🏗️ Project Structure
558
+
559
+ ```
560
+ npm-ai-hooks/
561
+ ├─ src/
562
+ │ ├─ index.ts # Main exports
563
+ │ ├─ wrap.ts # Core wrapping functionality
564
+ │ ├─ errors.ts # Error handling
565
+ │ ├─ providers/
566
+ │ │ ├─ base/ # Base provider system
567
+ │ │ │ ├─ BaseProvider.ts # Abstract base class
568
+ │ │ │ ├─ ProviderConfig.ts # Provider configuration
569
+ │ │ │ ├─ ProviderRegistry.ts # Provider management
570
+ │ │ │ └─ ProviderConfigs.ts # Provider definitions
571
+ │ │ └─ index.ts # Provider exports
572
+ │ └─ types/ # TypeScript definitions
573
+ ├─ examples/ # Usage examples
574
+ ├─ tests/ # Test suite
575
+ ├─ package.json
576
+ ├─ README.md
577
+ └─ LICENSE
578
+ ```
579
+
580
+ ---
581
+
582
+ ## 🛣️ Roadmap
583
+
584
+ * [ ] Streaming output support
585
+ * [ ] Cost ceiling + auto-fallbacks
586
+ * [ ] Rate limiter
587
+ * [ ] Multi-turn conversation API
588
+ * [ ] Local model support (llama.cpp, Ollama)
589
+ * [ ] VSCode extension for code-gen
590
+ * [ ] Custom provider registration
591
+ * [ ] Advanced caching strategies
592
+
593
+ ---
594
+
595
+ ## 🤝 Contributing
596
+
597
+ Contributions, ideas, and feedback are welcome! Please open an issue or submit a pull request.
598
+
599
+ ---
600
+
601
+ ## 📄 License
602
+
603
+ MIT © 2025 `npm-ai-hooks` Team
604
+
605
+ ---
606
+
607
+ ## 🔗 Links
608
+
609
+ - [GitHub Repository](https://github.com/iTeebot/npm-ai-hooks)
610
+ - [NPM Package](https://www.npmjs.com/package/npm-ai-hooks)
611
+ - [Documentation](https://labs.iteebot.com/npm-packages/npm-ai-hooks)
612
+ - [Changelog](CHANGELOG.md)