ai.matey.react.core 0.2.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.
@@ -0,0 +1,37 @@
1
+ /**
2
+ * useCompletion Hook
3
+ *
4
+ * React hook for text completion interfaces with streaming support.
5
+ *
6
+ * @module
7
+ */
8
+ import type { UseCompletionOptions, UseCompletionReturn } from './types.js';
9
+ /**
10
+ * useCompletion - React hook for text completion.
11
+ *
12
+ * Provides state management and streaming for single-turn
13
+ * text completion tasks.
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * import { useCompletion } from 'ai.matey.react.core';
18
+ *
19
+ * function CompletionComponent() {
20
+ * const { completion, input, handleInputChange, handleSubmit, isLoading } = useCompletion({
21
+ * api: '/api/completion',
22
+ * });
23
+ *
24
+ * return (
25
+ * <div>
26
+ * <p>{completion}</p>
27
+ * <form onSubmit={handleSubmit}>
28
+ * <input value={input} onChange={handleInputChange} placeholder="Enter a prompt..." />
29
+ * <button type="submit" disabled={isLoading}>Complete</button>
30
+ * </form>
31
+ * </div>
32
+ * );
33
+ * }
34
+ * ```
35
+ */
36
+ export declare function useCompletion(options?: UseCompletionOptions): UseCompletionReturn;
37
+ //# sourceMappingURL=use-completion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-completion.d.ts","sourceRoot":"","sources":["../../src/use-completion.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EAEpB,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,oBAAyB,GAAG,mBAAmB,CAoMrF"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * useObject Hook
3
+ *
4
+ * React hook for streaming structured objects with streaming support.
5
+ *
6
+ * @module
7
+ */
8
+ import type { UseObjectOptions, UseObjectReturn } from './types.js';
9
+ /**
10
+ * useObject - React hook for streaming structured objects.
11
+ *
12
+ * Provides state management for streaming JSON objects with
13
+ * partial updates as data arrives.
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * import { useObject } from 'ai.matey.react.core';
18
+ *
19
+ * interface Recipe {
20
+ * name: string;
21
+ * ingredients: string[];
22
+ * instructions: string[];
23
+ * }
24
+ *
25
+ * function RecipeGenerator() {
26
+ * const { object, submit, isLoading } = useObject<Recipe>({
27
+ * api: '/api/generate-recipe',
28
+ * });
29
+ *
30
+ * return (
31
+ * <div>
32
+ * <button onClick={() => submit('Generate a pasta recipe')} disabled={isLoading}>
33
+ * Generate Recipe
34
+ * </button>
35
+ * {object && (
36
+ * <div>
37
+ * <h2>{object.name}</h2>
38
+ * <ul>{object.ingredients?.map((i, idx) => <li key={idx}>{i}</li>)}</ul>
39
+ * </div>
40
+ * )}
41
+ * </div>
42
+ * );
43
+ * }
44
+ * ```
45
+ */
46
+ export declare function useObject<T>(options?: UseObjectOptions<T>): UseObjectReturn<T>;
47
+ //# sourceMappingURL=use-object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-object.d.ts","sourceRoot":"","sources":["../../src/use-object.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,GAAE,gBAAgB,CAAC,CAAC,CAAM,GAAG,eAAe,CAAC,CAAC,CAAC,CAwKlF"}
package/package.json ADDED
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "ai.matey.react.core",
3
+ "version": "0.2.0",
4
+ "description": "Core React hooks for AI Matey - useChat, useCompletion, useObject",
5
+ "type": "module",
6
+ "main": "./dist/cjs/index.js",
7
+ "module": "./dist/esm/index.js",
8
+ "types": "./dist/types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/types/index.d.ts",
13
+ "default": "./dist/esm/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/types/index.d.ts",
17
+ "default": "./dist/cjs/index.js"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "readme.md",
24
+ "CHANGELOG.md",
25
+ "LICENSE"
26
+ ],
27
+ "scripts": {
28
+ "build": "npm run build:esm && npm run build:cjs && npm run build:types",
29
+ "build:esm": "tsc -p tsconfig.esm.json",
30
+ "build:cjs": "tsc -p tsconfig.cjs.json",
31
+ "build:types": "tsc -p tsconfig.types.json",
32
+ "clean": "rm -rf dist",
33
+ "typecheck": "tsc --noEmit",
34
+ "test": "vitest run"
35
+ },
36
+ "dependencies": {
37
+ "ai.matey.types": "*",
38
+ "ai.matey.core": "*",
39
+ "ai.matey.wrapper": "*"
40
+ },
41
+ "peerDependencies": {
42
+ "react": ">=18.0.0"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "react": {
46
+ "optional": false
47
+ }
48
+ },
49
+ "devDependencies": {
50
+ "@types/react": "^18.2.0",
51
+ "react": "^18.2.0",
52
+ "typescript": "^5.9.3",
53
+ "vitest": "^3.2.4"
54
+ },
55
+ "keywords": [
56
+ "ai",
57
+ "llm",
58
+ "react",
59
+ "hooks",
60
+ "useChat",
61
+ "useCompletion",
62
+ "ai-matey"
63
+ ],
64
+ "author": "AI Matey",
65
+ "license": "MIT",
66
+ "homepage": "https://github.com/johnhenry/ai.matey#readme",
67
+ "bugs": {
68
+ "url": "https://github.com/johnhenry/ai.matey/issues"
69
+ },
70
+ "repository": {
71
+ "type": "git",
72
+ "url": "git+https://github.com/johnhenry/ai.matey.git",
73
+ "directory": "packages/react-core"
74
+ },
75
+ "engines": {
76
+ "node": ">=18.0.0"
77
+ }
78
+ }
package/readme.md ADDED
@@ -0,0 +1,232 @@
1
+ # ai.matey.react.core
2
+
3
+ Core React hooks for AI chat interactions.
4
+
5
+ Part of the [ai.matey](https://github.com/johnhenry/ai.matey) monorepo.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install ai.matey.react.core
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ### HTTP Mode (Default)
16
+
17
+ ```tsx
18
+ import { useChat } from 'ai.matey.react.core';
19
+
20
+ function ChatComponent() {
21
+ const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
22
+ api: '/api/chat',
23
+ });
24
+
25
+ return (
26
+ <div>
27
+ {messages.map((m) => (
28
+ <div key={m.id}>
29
+ <strong>{m.role}:</strong> {m.content}
30
+ </div>
31
+ ))}
32
+ <form onSubmit={handleSubmit}>
33
+ <input value={input} onChange={handleInputChange} />
34
+ <button type="submit" disabled={isLoading}>Send</button>
35
+ </form>
36
+ </div>
37
+ );
38
+ }
39
+ ```
40
+
41
+ ### Direct Backend Mode
42
+
43
+ Use any backend adapter directly without HTTP - great for Electron apps, browser extensions, or when you want to skip the server:
44
+
45
+ ```tsx
46
+ import { useChat } from 'ai.matey.react.core';
47
+ import { OpenAIBackend } from 'ai.matey.backend/openai';
48
+
49
+ // Create backend (could also be Anthropic, Gemini, Ollama, etc.)
50
+ const backend = new OpenAIBackend({
51
+ apiKey: process.env.REACT_APP_OPENAI_API_KEY,
52
+ });
53
+
54
+ function ChatComponent() {
55
+ const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat({
56
+ direct: {
57
+ backend,
58
+ systemPrompt: 'You are a helpful assistant.',
59
+ },
60
+ });
61
+
62
+ return (
63
+ <div>
64
+ {messages.map((m) => (
65
+ <div key={m.id}>
66
+ <strong>{m.role}:</strong> {m.content}
67
+ </div>
68
+ ))}
69
+ <form onSubmit={handleSubmit}>
70
+ <input value={input} onChange={handleInputChange} />
71
+ <button type="submit" disabled={isLoading}>Send</button>
72
+ </form>
73
+ </div>
74
+ );
75
+ }
76
+ ```
77
+
78
+ ## Exports
79
+
80
+ ### Hooks
81
+
82
+ - `useChat` - Chat interface with streaming support
83
+ - `useCompletion` - Text completion interface
84
+ - `useObject` - Structured object generation
85
+
86
+ ### Types
87
+
88
+ - `Message` - Chat message type
89
+ - `ToolCall`, `ToolInvocation`, `Tool` - Tool calling types
90
+ - `UseChatOptions`, `UseChatReturn` - useChat types
91
+ - `UseCompletionOptions`, `UseCompletionReturn` - useCompletion types
92
+ - `UseObjectOptions`, `UseObjectReturn` - useObject types
93
+ - `DirectBackend`, `DirectModeOptions`, `DirectToolCallHandler` - Direct mode types
94
+
95
+ ## API Reference
96
+
97
+ ### useChat
98
+
99
+ React hook for building chat interfaces with streaming support.
100
+
101
+ Supports two modes:
102
+ 1. **HTTP Mode** (default): Uses `api` endpoint with fetch
103
+ 2. **Direct Mode**: Uses `direct.backend` for direct backend access
104
+
105
+ ```tsx
106
+ const {
107
+ messages, // Message[] - Chat history
108
+ input, // string - Current input value
109
+ setInput, // (value: string) => void - Set input
110
+ handleInputChange, // (e: ChangeEvent) => void - Input change handler
111
+ handleSubmit, // (e?: FormEvent) => void - Form submit handler
112
+ append, // (message: Message | string) => Promise<string | null>
113
+ reload, // () => Promise<string | null> - Reload last response
114
+ stop, // () => void - Stop streaming
115
+ setMessages, // (messages: Message[]) => void
116
+ isLoading, // boolean - Request in progress
117
+ error, // Error | undefined
118
+ data, // unknown[] | undefined - Additional data
119
+ } = useChat(options);
120
+ ```
121
+
122
+ #### HTTP Mode Options
123
+
124
+ ```tsx
125
+ useChat({
126
+ api: '/api/chat', // API endpoint (default: '/api/chat')
127
+ initialMessages: [], // Initial messages
128
+ initialInput: '', // Initial input value
129
+ id: 'chat-1', // Chat instance ID
130
+ headers: {}, // Request headers
131
+ body: {}, // Extra request body fields
132
+ onFinish: (message) => {}, // Called when response completes
133
+ onError: (error) => {}, // Called on error
134
+ onResponse: (response) => {}, // Called with fetch response
135
+ streamProtocol: 'data', // 'data' (SSE) or 'text'
136
+ });
137
+ ```
138
+
139
+ #### Direct Mode Options
140
+
141
+ ```tsx
142
+ useChat({
143
+ direct: {
144
+ // Required: Backend adapter (any ai.matey backend or Bridge)
145
+ backend: myBackendAdapter,
146
+
147
+ // Optional: System prompt
148
+ systemPrompt: 'You are a helpful assistant.',
149
+
150
+ // Optional: Default IR parameters
151
+ defaultParameters: {
152
+ temperature: 0.7,
153
+ maxTokens: 1000,
154
+ },
155
+
156
+ // Optional: Available tools
157
+ tools: [
158
+ {
159
+ name: 'get_weather',
160
+ description: 'Get weather for a location',
161
+ parameters: { /* JSON Schema */ },
162
+ },
163
+ ],
164
+
165
+ // Optional: Tool call handler
166
+ onToolCall: async (name, input, id) => {
167
+ return 'Tool result';
168
+ },
169
+
170
+ // Optional: Auto-execute tools (default: false)
171
+ autoExecuteTools: true,
172
+
173
+ // Optional: Max tool rounds (default: 10)
174
+ maxToolRounds: 5,
175
+ },
176
+ // Common options still work
177
+ initialMessages: [],
178
+ onFinish: (message) => {},
179
+ onError: (error) => {},
180
+ });
181
+ ```
182
+
183
+ ### useCompletion
184
+
185
+ React hook for text completion interfaces.
186
+
187
+ ```tsx
188
+ const {
189
+ completion, // string - Current completion
190
+ input, // string - Current input
191
+ setInput, // (value: string) => void
192
+ handleInputChange,
193
+ handleSubmit,
194
+ complete, // (prompt: string) => Promise<string | null>
195
+ stop,
196
+ isLoading,
197
+ error,
198
+ } = useCompletion({
199
+ api: '/api/completion',
200
+ // ... similar options to useChat
201
+ });
202
+ ```
203
+
204
+ ### useObject
205
+
206
+ React hook for generating structured objects.
207
+
208
+ ```tsx
209
+ const {
210
+ object, // T | undefined - Generated object
211
+ submit, // (input: string) => void
212
+ isLoading,
213
+ error,
214
+ } = useObject<MyType>({
215
+ api: '/api/object',
216
+ schema: myZodSchema, // Zod schema for validation
217
+ });
218
+ ```
219
+
220
+ ## Direct Mode vs HTTP Mode
221
+
222
+ | Feature | HTTP Mode | Direct Mode |
223
+ |---------|-----------|-------------|
224
+ | Setup | Requires API endpoint | Just a backend adapter |
225
+ | Server needed | Yes | No |
226
+ | Use case | Full-stack apps | Electron, browser extensions, testing |
227
+ | Streaming | Via SSE | Native async iterators |
228
+ | Tool calling | Via API | Built-in with `wrapper-ir` |
229
+
230
+ ## License
231
+
232
+ MIT - see [LICENSE](./LICENSE) for details.