react-native-ai-hooks 0.4.0 → 0.6.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/.github/workflows/ci.yml +34 -0
- package/CONTRIBUTING.md +122 -0
- package/README.md +70 -27
- package/{IMPLEMENTATION_COMPLETE.md → docs/IMPLEMENTATION_COMPLETE.md} +2 -2
- package/docs/README.md +17 -0
- package/jest.config.cjs +7 -0
- package/jest.setup.ts +28 -0
- package/package.json +17 -3
- package/src/hooks/__tests__/useAIForm.test.ts +345 -0
- package/src/hooks/__tests__/useAIStream.test.ts +427 -0
- package/src/hooks/useAIChat.ts +8 -0
- package/src/hooks/useAICode.ts +8 -0
- package/src/hooks/useAIForm.ts +10 -2
- package/src/hooks/useAIStream.ts +9 -0
- package/src/hooks/useAISummarize.ts +8 -0
- package/src/hooks/useAITranslate.ts +9 -0
- package/src/hooks/useAIVoice.ts +8 -0
- package/src/hooks/useImageAnalysis.ts +8 -0
- package/src/utils/__tests__/fetchWithRetry.test.ts +168 -0
- package/src/utils/__tests__/providerFactory.test.ts +493 -0
- package/src/utils/fetchWithRetry.ts +13 -11
- package/src/utils/providerFactory.ts +35 -12
- /package/{src → docs}/ARCHITECTURE.md +0 -0
- /package/{ARCHITECTURE_GUIDE.md → docs/ARCHITECTURE_GUIDE.md} +0 -0
- /package/{TECHNICAL_SPECIFICATION.md → docs/TECHNICAL_SPECIFICATION.md} +0 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
- master
|
|
8
|
+
pull_request:
|
|
9
|
+
branches:
|
|
10
|
+
- main
|
|
11
|
+
- master
|
|
12
|
+
workflow_dispatch:
|
|
13
|
+
|
|
14
|
+
jobs:
|
|
15
|
+
test:
|
|
16
|
+
runs-on: ubuntu-latest
|
|
17
|
+
env:
|
|
18
|
+
NODE_ENV: test
|
|
19
|
+
|
|
20
|
+
steps:
|
|
21
|
+
- name: Checkout repository
|
|
22
|
+
uses: actions/checkout@v4
|
|
23
|
+
|
|
24
|
+
- name: Setup Node.js
|
|
25
|
+
uses: actions/setup-node@v4
|
|
26
|
+
with:
|
|
27
|
+
node-version: 22
|
|
28
|
+
cache: npm
|
|
29
|
+
|
|
30
|
+
- name: Install dependencies
|
|
31
|
+
run: npm ci --include=dev
|
|
32
|
+
|
|
33
|
+
- name: Run Jest tests
|
|
34
|
+
run: npm test -- --config jest.config.cjs --ci
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Contributing to react-native-ai-hooks
|
|
2
|
+
|
|
3
|
+
Thank you for your interest in contributing to react-native-ai-hooks.
|
|
4
|
+
|
|
5
|
+
We are building a practical, provider-agnostic AI hooks toolkit for React Native, and contributions from the community are a big part of making it better. Whether you are fixing a bug, improving docs, adding a new hook, or integrating a new provider, your help is welcome.
|
|
6
|
+
|
|
7
|
+
## Ways to Contribute
|
|
8
|
+
|
|
9
|
+
- Report bugs and edge cases
|
|
10
|
+
- Suggest new features or API improvements
|
|
11
|
+
- Improve examples and documentation
|
|
12
|
+
- Add support for new AI providers (for example Groq, Perplexity, or Mistral)
|
|
13
|
+
- Create new hooks for common AI workflows
|
|
14
|
+
- Add or improve tests
|
|
15
|
+
|
|
16
|
+
## Reporting Bugs
|
|
17
|
+
|
|
18
|
+
Please report bugs through GitHub Issues:
|
|
19
|
+
|
|
20
|
+
- https://github.com/nikapkh/react-native-ai-hooks/issues
|
|
21
|
+
|
|
22
|
+
Before opening an issue:
|
|
23
|
+
|
|
24
|
+
- Check existing issues to avoid duplicates
|
|
25
|
+
- Confirm the problem on the latest version if possible
|
|
26
|
+
|
|
27
|
+
When opening a bug report, include:
|
|
28
|
+
|
|
29
|
+
- A clear title and short summary
|
|
30
|
+
- Steps to reproduce
|
|
31
|
+
- Expected behavior
|
|
32
|
+
- Actual behavior
|
|
33
|
+
- Environment details (OS, React Native version, package version)
|
|
34
|
+
- Minimal code snippet or repo that reproduces the problem
|
|
35
|
+
- Relevant logs, stack traces, or screenshots
|
|
36
|
+
|
|
37
|
+
## Development Setup
|
|
38
|
+
|
|
39
|
+
1. Fork the repository on GitHub.
|
|
40
|
+
2. Clone your fork.
|
|
41
|
+
3. Install dependencies:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm install
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
4. Run tests:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm test
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Git Workflow
|
|
54
|
+
|
|
55
|
+
Use this workflow for all contributions:
|
|
56
|
+
|
|
57
|
+
1. Fork repository
|
|
58
|
+
2. Create branch
|
|
59
|
+
3. Commit changes
|
|
60
|
+
4. Push branch
|
|
61
|
+
5. Open pull request
|
|
62
|
+
|
|
63
|
+
Example commands:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
git checkout -b feat/add-mistral-provider
|
|
67
|
+
# make changes
|
|
68
|
+
git add .
|
|
69
|
+
git commit -m "feat: add mistral provider adapter"
|
|
70
|
+
git push origin feat/add-mistral-provider
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Pull Request Guidelines
|
|
74
|
+
|
|
75
|
+
- Keep PRs focused and small when possible
|
|
76
|
+
- Write clear commit messages
|
|
77
|
+
- Describe the problem and solution in the PR description
|
|
78
|
+
- Link related issues when relevant
|
|
79
|
+
- Update docs when behavior or API changes
|
|
80
|
+
|
|
81
|
+
All pull requests must pass existing tests before they can be merged.
|
|
82
|
+
|
|
83
|
+
## Testing
|
|
84
|
+
|
|
85
|
+
We use Jest for testing.
|
|
86
|
+
|
|
87
|
+
- Test command: `npm test`
|
|
88
|
+
- New functionality should include tests where practical
|
|
89
|
+
- Bug fixes should include a regression test when possible
|
|
90
|
+
|
|
91
|
+
Relevant test setup files:
|
|
92
|
+
|
|
93
|
+
- [jest.config.cjs](jest.config.cjs)
|
|
94
|
+
- [jest.setup.ts](jest.setup.ts)
|
|
95
|
+
|
|
96
|
+
## Adding a New Provider
|
|
97
|
+
|
|
98
|
+
If you want to add a provider such as Groq, Perplexity, or Mistral, a typical path is:
|
|
99
|
+
|
|
100
|
+
- Add provider types and interfaces in [src/types/index.ts](src/types/index.ts)
|
|
101
|
+
- Add request/response adapter logic in [src/utils/providerFactory.ts](src/utils/providerFactory.ts)
|
|
102
|
+
- Ensure resilience behavior remains compatible with [src/utils/fetchWithRetry.ts](src/utils/fetchWithRetry.ts)
|
|
103
|
+
- Add tests in [src/utils/__tests__](src/utils/__tests__)
|
|
104
|
+
- Update docs in [README.md](README.md) and [docs/README.md](docs/README.md)
|
|
105
|
+
|
|
106
|
+
## Adding a New Hook
|
|
107
|
+
|
|
108
|
+
If you want to add a new hook:
|
|
109
|
+
|
|
110
|
+
- Create the hook in [src/hooks](src/hooks)
|
|
111
|
+
- Export it from [src/index.ts](src/index.ts)
|
|
112
|
+
- Add or update relevant types in [src/types/index.ts](src/types/index.ts)
|
|
113
|
+
- Add tests and docs for the new behavior
|
|
114
|
+
|
|
115
|
+
## Communication and Conduct
|
|
116
|
+
|
|
117
|
+
Please be respectful and constructive in issues and pull requests. Thoughtful feedback and collaboration help everyone ship better software.
|
|
118
|
+
|
|
119
|
+
## Thank You
|
|
120
|
+
|
|
121
|
+
Thanks again for helping improve react-native-ai-hooks.
|
|
122
|
+
Your contributions make the library more reliable, more useful, and more accessible for the entire community.
|
package/README.md
CHANGED
|
@@ -5,50 +5,93 @@
|
|
|
5
5
|
|
|
6
6
|
# react-native-ai-hooks
|
|
7
7
|
|
|
8
|
-
AI
|
|
8
|
+
Build AI features in React Native without rebuilding the same plumbing every sprint.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
One hooks-first API for Claude, OpenAI, and Gemini.
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
npm install react-native-ai-hooks
|
|
14
|
-
```
|
|
12
|
+
## Why use this?
|
|
15
13
|
|
|
16
|
-
|
|
14
|
+
Most teams burn time on the same AI integration work:
|
|
15
|
+
|
|
16
|
+
- Provider-specific request/response wiring
|
|
17
|
+
- Retry, timeout, and error edge cases
|
|
18
|
+
- Streaming token parsing
|
|
19
|
+
- State handling for loading, cancellation, and failures
|
|
17
20
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
This library gives you that foundation out of the box so you can ship product features, not infra glue.
|
|
22
|
+
|
|
23
|
+
| What you want | What this gives you |
|
|
24
|
+
|---|---|
|
|
25
|
+
| Ship chat quickly | Drop-in hooks with minimal setup |
|
|
26
|
+
| Avoid provider lock-in | Unified interface across providers |
|
|
27
|
+
| Handle real-world failures | Built-in retries, backoff, timeout, abort |
|
|
28
|
+
| Keep code clean | Strong TypeScript types and predictable APIs |
|
|
22
29
|
|
|
23
30
|
## Quick Start
|
|
24
31
|
|
|
25
32
|
```tsx
|
|
33
|
+
// npm install react-native-ai-hooks
|
|
34
|
+
|
|
26
35
|
import { useAIChat } from 'react-native-ai-hooks';
|
|
27
36
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
export function Assistant() {
|
|
38
|
+
const { messages, sendMessage, isLoading, error } = useAIChat({
|
|
39
|
+
provider: 'anthropic',
|
|
40
|
+
apiKey: process.env.EXPO_PUBLIC_AI_KEY ?? '',
|
|
41
|
+
model: 'claude-sonnet-4-20250514',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Example action
|
|
45
|
+
async function onAsk() {
|
|
46
|
+
await sendMessage('Draft a warm onboarding message for new users.');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
32
51
|
```
|
|
33
|
-
## ⚠️ Security Note
|
|
34
52
|
|
|
35
|
-
|
|
53
|
+
## Hooks
|
|
54
|
+
|
|
55
|
+
- 💬 useAIChat: multi-turn conversation
|
|
56
|
+
- ⚡ useAIStream: token streaming
|
|
57
|
+
- 👁️ useImageAnalysis: image and vision workflows
|
|
58
|
+
- 📝 useAIForm: AI-assisted form validation
|
|
59
|
+
- 🎙️ useAIVoice: speech-to-text plus AI response
|
|
60
|
+
- 🌍 useAITranslate: real-time translation
|
|
61
|
+
- 📄 useAISummarize: concise text summaries
|
|
62
|
+
- 🧠 useAICode: generate and explain code
|
|
63
|
+
|
|
64
|
+
## Provider Support
|
|
65
|
+
|
|
66
|
+
| Provider | Chat | Stream | Vision | Voice |
|
|
67
|
+
|---|---|---|---|---|
|
|
68
|
+
| Anthropic Claude | ✅ | ✅ | ✅ | ✅ |
|
|
69
|
+
| OpenAI | ✅ | ✅ | ✅ | ✅ |
|
|
70
|
+
| Gemini | ✅ | ✅ | 🔜 | 🔜 |
|
|
71
|
+
|
|
72
|
+
## Security
|
|
73
|
+
|
|
74
|
+
Use a backend proxy in production. Do not ship permanent provider API keys in app binaries.
|
|
36
75
|
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
|
|
76
|
+
```tsx
|
|
77
|
+
const { sendMessage } = useAIChat({
|
|
78
|
+
baseUrl: 'https://your-backend.com/api/ai',
|
|
79
|
+
});
|
|
40
80
|
```
|
|
41
81
|
|
|
42
|
-
|
|
82
|
+
## Example App
|
|
83
|
+
|
|
84
|
+
See [example](./example) for a full app with provider switching, API key settings, and streaming chat.
|
|
85
|
+
|
|
86
|
+
## Deep Technical Docs
|
|
43
87
|
|
|
44
|
-
|
|
88
|
+
Detailed architecture and implementation references now live in [docs](./docs):
|
|
45
89
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
| Gemini | 🔜 |
|
|
90
|
+
- [Architecture Guide](./docs/ARCHITECTURE_GUIDE.md)
|
|
91
|
+
- [Technical Specification](./docs/TECHNICAL_SPECIFICATION.md)
|
|
92
|
+
- [Implementation Summary](./docs/IMPLEMENTATION_COMPLETE.md)
|
|
93
|
+
- [Internal Architecture Notes](./docs/ARCHITECTURE.md)
|
|
51
94
|
|
|
52
95
|
## License
|
|
53
96
|
|
|
54
|
-
MIT
|
|
97
|
+
MIT © [nikapkh](https://github.com/nikapkh)
|
|
@@ -88,8 +88,8 @@
|
|
|
88
88
|
### Documentation (2)
|
|
89
89
|
| File | Purpose | Status |
|
|
90
90
|
|------|---------|--------|
|
|
91
|
-
| `
|
|
92
|
-
| `ARCHITECTURE_GUIDE.md` | Developer & maintainer guide | ✅ Complete |
|
|
91
|
+
| `docs/ARCHITECTURE.md` | Internal architecture details | ✅ Complete |
|
|
92
|
+
| `docs/ARCHITECTURE_GUIDE.md` | Developer & maintainer guide | ✅ Complete |
|
|
93
93
|
|
|
94
94
|
---
|
|
95
95
|
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Documentation
|
|
2
|
+
|
|
3
|
+
Deep technical references for react-native-ai-hooks live in this folder.
|
|
4
|
+
|
|
5
|
+
## Read First
|
|
6
|
+
|
|
7
|
+
1. [Architecture Guide](./ARCHITECTURE_GUIDE.md)
|
|
8
|
+
2. [Technical Specification](./TECHNICAL_SPECIFICATION.md)
|
|
9
|
+
3. [Implementation Complete](./IMPLEMENTATION_COMPLETE.md)
|
|
10
|
+
4. [Internal Architecture Notes](./ARCHITECTURE.md)
|
|
11
|
+
|
|
12
|
+
## What This Folder Covers
|
|
13
|
+
|
|
14
|
+
- Provider abstraction and normalization
|
|
15
|
+
- Retry and timeout strategy
|
|
16
|
+
- Hook design patterns and type system
|
|
17
|
+
- Security and performance considerations
|
package/jest.config.cjs
ADDED
package/jest.setup.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const originalConsoleError = console.error;
|
|
2
|
+
let consoleErrorSpy: jest.SpyInstance;
|
|
3
|
+
|
|
4
|
+
beforeAll(() => {
|
|
5
|
+
global.fetch = jest.fn();
|
|
6
|
+
|
|
7
|
+
// Required by React 18+ testing helpers so state updates inside act are tracked correctly.
|
|
8
|
+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
|
9
|
+
|
|
10
|
+
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation((...args: unknown[]) => {
|
|
11
|
+
const firstArg = args[0];
|
|
12
|
+
const message = typeof firstArg === 'string' ? firstArg : '';
|
|
13
|
+
|
|
14
|
+
if (message.includes('react-test-renderer is deprecated')) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
originalConsoleError(...(args as Parameters<typeof console.error>));
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
jest.clearAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterAll(() => {
|
|
27
|
+
consoleErrorSpy?.mockRestore();
|
|
28
|
+
});
|
package/package.json
CHANGED
|
@@ -1,17 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-ai-hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "AI hooks for React Native — useAIChat, useAIStream, useImageAnalysis. Works with Claude, OpenAI & Gemini.",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
7
|
-
"
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "jest"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"react-native",
|
|
12
|
+
"ai",
|
|
13
|
+
"claude",
|
|
14
|
+
"openai",
|
|
15
|
+
"hooks",
|
|
16
|
+
"expo"
|
|
17
|
+
],
|
|
8
18
|
"author": "nikapkh",
|
|
9
19
|
"license": "MIT",
|
|
10
20
|
"devDependencies": {
|
|
21
|
+
"@types/jest": "^29.5.14",
|
|
22
|
+
"jest": "^29.7.0",
|
|
23
|
+
"react-test-renderer": "^19.2.0",
|
|
24
|
+
"ts-jest": "^29.4.9",
|
|
11
25
|
"typescript": "^5.0.0"
|
|
12
26
|
},
|
|
13
27
|
"peerDependencies": {
|
|
14
28
|
"react": ">=18.0.0",
|
|
15
29
|
"react-native": ">=0.70.0"
|
|
16
30
|
}
|
|
17
|
-
}
|
|
31
|
+
}
|