ai.matey.react.nextjs 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.
- package/LICENSE +21 -0
- package/dist/cjs/client.js +130 -0
- package/dist/cjs/client.js.map +1 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/server.js +257 -0
- package/dist/cjs/server.js.map +1 -0
- package/dist/esm/client.js +125 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/server.js +251 -0
- package/dist/esm/server.js.map +1 -0
- package/dist/types/client.d.ts +126 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/server.d.ts +136 -0
- package/dist/types/server.d.ts.map +1 -0
- package/package.json +93 -0
- package/readme.md +170 -0
package/readme.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# ai.matey.react.nextjs
|
|
2
|
+
|
|
3
|
+
Next.js App Router integration for AI chat.
|
|
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.nextjs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
### Client Component
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
'use client';
|
|
19
|
+
|
|
20
|
+
import { useChat } from 'ai.matey.react.nextjs';
|
|
21
|
+
|
|
22
|
+
export function ChatComponent() {
|
|
23
|
+
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat();
|
|
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
|
+
### API Route
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// app/api/chat/route.ts
|
|
45
|
+
import { Bridge } from 'ai.matey.core';
|
|
46
|
+
import { OpenAIFrontendAdapter } from 'ai.matey.frontend/openai';
|
|
47
|
+
import { OpenAIBackendAdapter } from 'ai.matey.backend/openai';
|
|
48
|
+
|
|
49
|
+
const bridge = new Bridge(
|
|
50
|
+
new OpenAIFrontendAdapter(),
|
|
51
|
+
new OpenAIBackendAdapter({ apiKey: process.env.OPENAI_API_KEY! })
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
export async function POST(req: Request) {
|
|
55
|
+
const { messages } = await req.json();
|
|
56
|
+
|
|
57
|
+
const stream = await bridge.chatStream({
|
|
58
|
+
model: 'gpt-4',
|
|
59
|
+
messages,
|
|
60
|
+
stream: true,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Return streaming response
|
|
64
|
+
return new Response(stream, {
|
|
65
|
+
headers: { 'Content-Type': 'text/event-stream' },
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Exports
|
|
71
|
+
|
|
72
|
+
### Hooks
|
|
73
|
+
|
|
74
|
+
- `useChat` - Chat hook optimized for Next.js (defaults to `/api/chat`)
|
|
75
|
+
- `useCompletion` - Completion hook (defaults to `/api/completion`)
|
|
76
|
+
|
|
77
|
+
### Utilities
|
|
78
|
+
|
|
79
|
+
- `generateAIMetadata` - Generate Next.js metadata from AI content
|
|
80
|
+
|
|
81
|
+
### Types (re-exported from react.core)
|
|
82
|
+
|
|
83
|
+
- `Message`, `ToolCall`, `ToolInvocation`, `Tool`
|
|
84
|
+
- `ChatRequestOptions`, `CompletionRequestOptions`
|
|
85
|
+
- `UseNextChatOptions`, `UseNextCompletionOptions`
|
|
86
|
+
- `AIMetadata`, `GenerateMetadataOptions`
|
|
87
|
+
|
|
88
|
+
## API Reference
|
|
89
|
+
|
|
90
|
+
### useChat
|
|
91
|
+
|
|
92
|
+
Next.js-optimized chat hook with sensible defaults.
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
const {
|
|
96
|
+
messages,
|
|
97
|
+
input,
|
|
98
|
+
handleInputChange,
|
|
99
|
+
handleSubmit,
|
|
100
|
+
append,
|
|
101
|
+
reload,
|
|
102
|
+
stop,
|
|
103
|
+
isLoading,
|
|
104
|
+
error,
|
|
105
|
+
} = useChat({
|
|
106
|
+
api: '/api/chat', // Default for Next.js
|
|
107
|
+
streamProtocol: 'data', // SSE protocol
|
|
108
|
+
serverAction: async (body) => {}, // Use Server Action instead of API route
|
|
109
|
+
experimental: {
|
|
110
|
+
partialHydration: false,
|
|
111
|
+
suspense: false,
|
|
112
|
+
},
|
|
113
|
+
// ...all options from react.core useChat
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### useCompletion
|
|
118
|
+
|
|
119
|
+
Text completion hook for Next.js.
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
const {
|
|
123
|
+
completion,
|
|
124
|
+
input,
|
|
125
|
+
handleInputChange,
|
|
126
|
+
handleSubmit,
|
|
127
|
+
complete,
|
|
128
|
+
isLoading,
|
|
129
|
+
error,
|
|
130
|
+
} = useCompletion({
|
|
131
|
+
api: '/api/completion', // Default for Next.js
|
|
132
|
+
streamProtocol: 'data',
|
|
133
|
+
// ...all options from react.core useCompletion
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### generateAIMetadata
|
|
138
|
+
|
|
139
|
+
Generate Next.js Metadata from AI-generated content (useful for SEO).
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
// app/blog/[slug]/page.tsx
|
|
143
|
+
import { generateAIMetadata } from 'ai.matey.react.nextjs';
|
|
144
|
+
|
|
145
|
+
export async function generateMetadata({ params }) {
|
|
146
|
+
const article = await getAIArticle(params.slug);
|
|
147
|
+
|
|
148
|
+
return generateAIMetadata(article.content, article.aiMetadata, {
|
|
149
|
+
titleTemplate: '%s | My Blog',
|
|
150
|
+
descriptionTemplate: '%s',
|
|
151
|
+
includeAttribution: true, // Adds ai-generated meta tag
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**Returns:**
|
|
157
|
+
```typescript
|
|
158
|
+
{
|
|
159
|
+
title: string;
|
|
160
|
+
description: string;
|
|
161
|
+
other?: {
|
|
162
|
+
'ai-generated': 'true';
|
|
163
|
+
'ai-attribution'?: string;
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
MIT - see [LICENSE](./LICENSE) for details.
|