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.
@@ -0,0 +1,251 @@
1
+ /**
2
+ * Next.js Server Utilities
3
+ *
4
+ * Server-side utilities for Next.js App Router and API Routes.
5
+ *
6
+ * @module
7
+ */
8
+ /**
9
+ * Create a streaming Response for Next.js App Router.
10
+ *
11
+ * @param stream - Async iterable of chunks
12
+ * @param options - Response options
13
+ * @returns Web Response with streaming body
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // app/api/chat/route.ts
18
+ * import { createStreamingResponse } from 'ai.matey.react.nextjs/server';
19
+ * import { Bridge } from 'ai.matey.core';
20
+ *
21
+ * export async function POST(request: Request) {
22
+ * const { messages } = await request.json();
23
+ * const bridge = new Bridge({ backend, frontend });
24
+ *
25
+ * const stream = bridge.chatStream({ messages });
26
+ * return createStreamingResponse(stream);
27
+ * }
28
+ * ```
29
+ */
30
+ export function createStreamingResponse(stream, options = {}) {
31
+ const { headers = {}, status = 200 } = options;
32
+ let body;
33
+ if (stream instanceof ReadableStream) {
34
+ body = stream;
35
+ }
36
+ else {
37
+ // Convert async iterable to ReadableStream
38
+ const encoder = new TextEncoder();
39
+ body = new ReadableStream({
40
+ async start(controller) {
41
+ try {
42
+ for await (const chunk of stream) {
43
+ controller.enqueue(encoder.encode(chunk));
44
+ }
45
+ controller.close();
46
+ }
47
+ catch (error) {
48
+ controller.error(error);
49
+ }
50
+ },
51
+ });
52
+ }
53
+ return new Response(body, {
54
+ status,
55
+ headers: {
56
+ 'Content-Type': 'text/event-stream',
57
+ 'Cache-Control': 'no-cache, no-transform',
58
+ Connection: 'keep-alive',
59
+ ...headers,
60
+ },
61
+ });
62
+ }
63
+ /**
64
+ * Create a Server-Sent Events (SSE) response.
65
+ *
66
+ * @param stream - Async iterable of data objects
67
+ * @param options - Response options
68
+ * @returns Web Response with SSE body
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * // app/api/chat/route.ts
73
+ * import { createSSEResponse } from 'ai.matey.react.nextjs/server';
74
+ *
75
+ * export async function POST(request: Request) {
76
+ * const stream = generateChatStream();
77
+ * return createSSEResponse(stream);
78
+ * }
79
+ * ```
80
+ */
81
+ export function createSSEResponse(stream, options = {}) {
82
+ const { headers = {}, status = 200 } = options;
83
+ const encoder = new TextEncoder();
84
+ const body = new ReadableStream({
85
+ async start(controller) {
86
+ try {
87
+ for await (const data of stream) {
88
+ const sseMessage = `data: ${JSON.stringify(data)}\n\n`;
89
+ controller.enqueue(encoder.encode(sseMessage));
90
+ }
91
+ // Send done signal
92
+ controller.enqueue(encoder.encode('data: [DONE]\n\n'));
93
+ controller.close();
94
+ }
95
+ catch (error) {
96
+ controller.error(error);
97
+ }
98
+ },
99
+ });
100
+ return new Response(body, {
101
+ status,
102
+ headers: {
103
+ 'Content-Type': 'text/event-stream',
104
+ 'Cache-Control': 'no-cache, no-transform',
105
+ Connection: 'keep-alive',
106
+ ...headers,
107
+ },
108
+ });
109
+ }
110
+ /**
111
+ * Create a chat API route handler for Next.js.
112
+ *
113
+ * @param options - Handler options
114
+ * @returns Route handler function
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * // app/api/chat/route.ts
119
+ * import { createChatHandler } from 'ai.matey.react.nextjs/server';
120
+ * import { Bridge } from 'ai.matey.core';
121
+ * import { OpenAIBackend } from 'ai.matey.backend/openai';
122
+ *
123
+ * const bridge = new Bridge({
124
+ * backend: new OpenAIBackend({ apiKey: process.env.OPENAI_API_KEY }),
125
+ * });
126
+ *
127
+ * export const POST = createChatHandler({ bridge });
128
+ * ```
129
+ */
130
+ export function createChatHandler(options) {
131
+ const { bridge: bridgeOrFactory, validate, transformRequest, transformChunk, onError } = options;
132
+ return async function handler(request) {
133
+ try {
134
+ // Parse request body
135
+ const body = (await request.json());
136
+ // Validate if provided
137
+ if (validate && !validate(body)) {
138
+ return new Response(JSON.stringify({ error: 'Invalid request body' }), {
139
+ status: 400,
140
+ headers: { 'Content-Type': 'application/json' },
141
+ });
142
+ }
143
+ // Get bridge instance
144
+ const bridge = typeof bridgeOrFactory === 'function' ? await bridgeOrFactory() : bridgeOrFactory;
145
+ // Transform request if provided
146
+ const processedBody = transformRequest ? transformRequest(body) : body;
147
+ // Check if streaming is requested
148
+ const shouldStream = processedBody.stream !== false;
149
+ if (shouldStream) {
150
+ // Streaming response
151
+ const stream = bridge.chatStream({
152
+ messages: processedBody.messages.map((m) => ({
153
+ role: m.role,
154
+ content: m.content,
155
+ })),
156
+ model: processedBody.model,
157
+ temperature: processedBody.temperature,
158
+ maxTokens: processedBody.maxTokens,
159
+ });
160
+ // Transform chunks if needed
161
+ const transformedStream = transformChunk
162
+ ? transformStreamChunks(stream, transformChunk)
163
+ : stream;
164
+ return createSSEResponse(transformedStream);
165
+ }
166
+ else {
167
+ // Non-streaming response
168
+ const response = await bridge.chat({
169
+ messages: processedBody.messages.map((m) => ({
170
+ role: m.role,
171
+ content: m.content,
172
+ })),
173
+ model: processedBody.model,
174
+ temperature: processedBody.temperature,
175
+ maxTokens: processedBody.maxTokens,
176
+ });
177
+ return new Response(JSON.stringify(response), {
178
+ status: 200,
179
+ headers: { 'Content-Type': 'application/json' },
180
+ });
181
+ }
182
+ }
183
+ catch (error) {
184
+ if (onError) {
185
+ return onError(error instanceof Error ? error : new Error(String(error)));
186
+ }
187
+ console.error('Chat handler error:', error);
188
+ return new Response(JSON.stringify({
189
+ error: error instanceof Error ? error.message : 'Internal server error',
190
+ }), {
191
+ status: 500,
192
+ headers: { 'Content-Type': 'application/json' },
193
+ });
194
+ }
195
+ };
196
+ }
197
+ /**
198
+ * Transform stream chunks.
199
+ */
200
+ async function* transformStreamChunks(stream, transform) {
201
+ for await (const chunk of stream) {
202
+ if (typeof chunk === 'string') {
203
+ yield transform(chunk);
204
+ }
205
+ else if (chunk &&
206
+ typeof chunk === 'object' &&
207
+ 'content' in chunk &&
208
+ typeof chunk.content === 'string') {
209
+ yield {
210
+ ...chunk,
211
+ content: transform(chunk.content),
212
+ };
213
+ }
214
+ else {
215
+ yield chunk;
216
+ }
217
+ }
218
+ }
219
+ /**
220
+ * Server action for chat completion.
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * // app/actions.ts
225
+ * 'use server';
226
+ *
227
+ * import { createChatAction } from 'ai.matey.react.nextjs/server';
228
+ * import { bridge } from './bridge';
229
+ *
230
+ * export const chatAction = createChatAction({ bridge });
231
+ *
232
+ * // In component:
233
+ * const result = await chatAction({ messages: [...] });
234
+ * ```
235
+ */
236
+ export function createChatAction(options) {
237
+ return async function chatAction(body) {
238
+ const bridge = typeof options.bridge === 'function' ? await options.bridge() : options.bridge;
239
+ const response = await bridge.chat({
240
+ messages: body.messages.map((m) => ({
241
+ role: m.role,
242
+ content: m.content,
243
+ })),
244
+ model: body.model,
245
+ temperature: body.temperature,
246
+ maxTokens: body.maxTokens,
247
+ });
248
+ return response;
249
+ };
250
+ }
251
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAcH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,uBAAuB,CACrC,MAA0D,EAC1D,UAAoC,EAAE;IAEtC,MAAM,EAAE,OAAO,GAAG,EAAE,EAAE,MAAM,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IAE/C,IAAI,IAAgC,CAAC;IAErC,IAAI,MAAM,YAAY,cAAc,EAAE,CAAC;QACrC,IAAI,GAAG,MAAM,CAAC;IAChB,CAAC;SAAM,CAAC;QACN,2CAA2C;QAC3C,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;QAClC,IAAI,GAAG,IAAI,cAAc,CAAC;YACxB,KAAK,CAAC,KAAK,CAAC,UAAU;gBACpB,IAAI,CAAC;oBACH,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;wBACjC,UAAU,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AO,CAAC,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC;QAE9F,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC;YACjC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;gBAClC,IAAI,EAAE,CAAC,CAAC,IAAI;gBACZ,OAAO,EAAE,CAAC,CAAC,OAAO;aACnB,CAAC,CAAC;YACH,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,SAAS,EAAE,IAAI,CAAC,SAAS;SAC1B,CAAC,CAAC;QAEH,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Next.js Client Utilities
3
+ *
4
+ * Client-side hooks and utilities for Next.js.
5
+ *
6
+ * @module
7
+ */
8
+ import type { UseChatOptions, UseChatReturn, UseCompletionOptions, UseCompletionReturn } from 'ai.matey.react.core';
9
+ /**
10
+ * Extended options for Next.js useChat.
11
+ */
12
+ export interface UseNextChatOptions extends UseChatOptions {
13
+ /** Use server action instead of API route */
14
+ serverAction?: (body: unknown) => Promise<unknown>;
15
+ /** Experimental features */
16
+ experimental?: {
17
+ /** Enable partial hydration */
18
+ partialHydration?: boolean;
19
+ /** Enable React Suspense */
20
+ suspense?: boolean;
21
+ };
22
+ }
23
+ /**
24
+ * useChat hook optimized for Next.js.
25
+ *
26
+ * Wraps the core useChat hook with Next.js-specific defaults.
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * 'use client';
31
+ *
32
+ * import { useChat } from 'ai.matey.react.nextjs';
33
+ *
34
+ * export function ChatComponent() {
35
+ * const { messages, input, handleInputChange, handleSubmit } = useChat();
36
+ *
37
+ * return (
38
+ * <form onSubmit={handleSubmit}>
39
+ * {messages.map((m) => <div key={m.id}>{m.content}</div>)}
40
+ * <input value={input} onChange={handleInputChange} />
41
+ * </form>
42
+ * );
43
+ * }
44
+ * ```
45
+ */
46
+ export declare function useChat(options?: UseNextChatOptions): UseChatReturn;
47
+ /**
48
+ * Extended options for Next.js useCompletion.
49
+ */
50
+ export interface UseNextCompletionOptions extends UseCompletionOptions {
51
+ /** Use server action instead of API route */
52
+ serverAction?: (body: unknown) => Promise<unknown>;
53
+ }
54
+ /**
55
+ * useCompletion hook optimized for Next.js.
56
+ *
57
+ * @example
58
+ * ```tsx
59
+ * 'use client';
60
+ *
61
+ * import { useCompletion } from 'ai.matey.react.nextjs';
62
+ *
63
+ * export function CompletionComponent() {
64
+ * const { completion, input, handleInputChange, handleSubmit } = useCompletion();
65
+ *
66
+ * return (
67
+ * <form onSubmit={handleSubmit}>
68
+ * <input value={input} onChange={handleInputChange} />
69
+ * <p>{completion}</p>
70
+ * </form>
71
+ * );
72
+ * }
73
+ * ```
74
+ */
75
+ export declare function useCompletion(options?: UseNextCompletionOptions): UseCompletionReturn;
76
+ /**
77
+ * Metadata for AI-generated content.
78
+ */
79
+ export interface AIMetadata {
80
+ /** Provider used */
81
+ provider?: string;
82
+ /** Model used */
83
+ model?: string;
84
+ /** Token usage */
85
+ usage?: {
86
+ promptTokens: number;
87
+ completionTokens: number;
88
+ totalTokens: number;
89
+ };
90
+ /** Generation time in ms */
91
+ generationTime?: number;
92
+ }
93
+ /**
94
+ * Options for generating metadata.
95
+ */
96
+ export interface GenerateMetadataOptions {
97
+ /** Title template */
98
+ titleTemplate?: string;
99
+ /** Description template */
100
+ descriptionTemplate?: string;
101
+ /** Include AI attribution */
102
+ includeAttribution?: boolean;
103
+ }
104
+ /**
105
+ * Generate Next.js metadata from AI response.
106
+ *
107
+ * Useful for SEO when AI generates page content.
108
+ *
109
+ * @param content - AI-generated content
110
+ * @param aiMetadata - AI generation metadata
111
+ * @param options - Metadata options
112
+ * @returns Next.js Metadata object
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * // app/blog/[slug]/page.tsx
117
+ * import { generateMetadata } from 'ai.matey.react.nextjs';
118
+ *
119
+ * export async function generateMetadata({ params }) {
120
+ * const article = await getAIArticle(params.slug);
121
+ * return generateAIMetadata(article.content, article.aiMetadata);
122
+ * }
123
+ * ```
124
+ */
125
+ export declare function generateAIMetadata(content: string, aiMetadata?: AIMetadata, options?: GenerateMetadataOptions): Record<string, unknown>;
126
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EACV,cAAc,EACd,aAAa,EACb,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,qBAAqB,CAAC;AAE7B;;GAEG;AACH,MAAM,WAAW,kBAAmB,SAAQ,cAAc;IACxD,6CAA6C;IAC7C,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IACnD,4BAA4B;IAC5B,YAAY,CAAC,EAAE;QACb,+BAA+B;QAC/B,gBAAgB,CAAC,EAAE,OAAO,CAAC;QAC3B,4BAA4B;QAC5B,QAAQ,CAAC,EAAE,OAAO,CAAC;KACpB,CAAC;CACH;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,OAAO,CAAC,OAAO,GAAE,kBAAuB,GAAG,aAAa,CAYvE;AAED;;GAEG;AACH,MAAM,WAAW,wBAAyB,SAAQ,oBAAoB;IACpE,6CAA6C;IAC7C,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CACpD;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,wBAA6B,GAAG,mBAAmB,CAWzF;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,oBAAoB;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iBAAiB;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,kBAAkB;IAClB,KAAK,CAAC,EAAE;QACN,YAAY,EAAE,MAAM,CAAC;QACrB,gBAAgB,EAAE,MAAM,CAAC;QACzB,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;IACF,4BAA4B;IAC5B,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,qBAAqB;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,2BAA2B;IAC3B,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,6BAA6B;IAC7B,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,MAAM,EACf,UAAU,CAAC,EAAE,UAAU,EACvB,OAAO,GAAE,uBAA4B,GACpC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAiCzB"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * AI Matey React Next.js
3
+ *
4
+ * Next.js integration for AI Matey.
5
+ *
6
+ * For server-side utilities, import from 'ai.matey.react.nextjs/server'.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ export { useChat, useCompletion, generateAIMetadata } from './client.js';
11
+ export type { Message, ToolCall, ToolInvocation, Tool, ChatRequestOptions, CompletionRequestOptions, } from 'ai.matey.react.core';
12
+ export type { UseNextChatOptions, UseNextCompletionOptions, AIMetadata, GenerateMetadataOptions, } from './client.js';
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGzE,YAAY,EACV,OAAO,EACP,QAAQ,EACR,cAAc,EACd,IAAI,EACJ,kBAAkB,EAClB,wBAAwB,GACzB,MAAM,qBAAqB,CAAC;AAG7B,YAAY,EACV,kBAAkB,EAClB,wBAAwB,EACxB,UAAU,EACV,uBAAuB,GACxB,MAAM,aAAa,CAAC"}
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Next.js Server Utilities
3
+ *
4
+ * Server-side utilities for Next.js App Router and API Routes.
5
+ *
6
+ * @module
7
+ */
8
+ import type { Bridge } from 'ai.matey.core';
9
+ /**
10
+ * Streaming response options.
11
+ */
12
+ export interface StreamingResponseOptions {
13
+ /** Response headers */
14
+ headers?: Record<string, string>;
15
+ /** Response status code */
16
+ status?: number;
17
+ }
18
+ /**
19
+ * Create a streaming Response for Next.js App Router.
20
+ *
21
+ * @param stream - Async iterable of chunks
22
+ * @param options - Response options
23
+ * @returns Web Response with streaming body
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * // app/api/chat/route.ts
28
+ * import { createStreamingResponse } from 'ai.matey.react.nextjs/server';
29
+ * import { Bridge } from 'ai.matey.core';
30
+ *
31
+ * export async function POST(request: Request) {
32
+ * const { messages } = await request.json();
33
+ * const bridge = new Bridge({ backend, frontend });
34
+ *
35
+ * const stream = bridge.chatStream({ messages });
36
+ * return createStreamingResponse(stream);
37
+ * }
38
+ * ```
39
+ */
40
+ export declare function createStreamingResponse(stream: AsyncIterable<string> | ReadableStream<Uint8Array>, options?: StreamingResponseOptions): Response;
41
+ /**
42
+ * Create a Server-Sent Events (SSE) response.
43
+ *
44
+ * @param stream - Async iterable of data objects
45
+ * @param options - Response options
46
+ * @returns Web Response with SSE body
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * // app/api/chat/route.ts
51
+ * import { createSSEResponse } from 'ai.matey.react.nextjs/server';
52
+ *
53
+ * export async function POST(request: Request) {
54
+ * const stream = generateChatStream();
55
+ * return createSSEResponse(stream);
56
+ * }
57
+ * ```
58
+ */
59
+ export declare function createSSEResponse<T>(stream: AsyncIterable<T>, options?: StreamingResponseOptions): Response;
60
+ /**
61
+ * Options for creating a chat handler.
62
+ */
63
+ export interface CreateChatHandlerOptions {
64
+ /** Bridge instance or factory */
65
+ bridge: Bridge | (() => Bridge | Promise<Bridge>);
66
+ /** Request validation */
67
+ validate?: (body: unknown) => boolean;
68
+ /** Transform request before processing */
69
+ transformRequest?: (body: ChatRequestBody) => ChatRequestBody;
70
+ /** Transform response chunks */
71
+ transformChunk?: (chunk: string) => string;
72
+ /** Error handler */
73
+ onError?: (error: Error) => Response;
74
+ }
75
+ /**
76
+ * Chat request body structure.
77
+ */
78
+ export interface ChatRequestBody {
79
+ /** Chat messages */
80
+ messages: Array<{
81
+ role: 'user' | 'assistant' | 'system';
82
+ content: string;
83
+ }>;
84
+ /** Model to use */
85
+ model?: string;
86
+ /** Temperature */
87
+ temperature?: number;
88
+ /** Max tokens */
89
+ maxTokens?: number;
90
+ /** Stream response */
91
+ stream?: boolean;
92
+ /** Additional options */
93
+ [key: string]: unknown;
94
+ }
95
+ /**
96
+ * Create a chat API route handler for Next.js.
97
+ *
98
+ * @param options - Handler options
99
+ * @returns Route handler function
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * // app/api/chat/route.ts
104
+ * import { createChatHandler } from 'ai.matey.react.nextjs/server';
105
+ * import { Bridge } from 'ai.matey.core';
106
+ * import { OpenAIBackend } from 'ai.matey.backend/openai';
107
+ *
108
+ * const bridge = new Bridge({
109
+ * backend: new OpenAIBackend({ apiKey: process.env.OPENAI_API_KEY }),
110
+ * });
111
+ *
112
+ * export const POST = createChatHandler({ bridge });
113
+ * ```
114
+ */
115
+ export declare function createChatHandler(options: CreateChatHandlerOptions): (request: Request) => Promise<Response>;
116
+ /**
117
+ * Server action for chat completion.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * // app/actions.ts
122
+ * 'use server';
123
+ *
124
+ * import { createChatAction } from 'ai.matey.react.nextjs/server';
125
+ * import { bridge } from './bridge';
126
+ *
127
+ * export const chatAction = createChatAction({ bridge });
128
+ *
129
+ * // In component:
130
+ * const result = await chatAction({ messages: [...] });
131
+ * ```
132
+ */
133
+ export declare function createChatAction(options: {
134
+ bridge: Bridge | (() => Bridge | Promise<Bridge>);
135
+ }): (body: ChatRequestBody) => Promise<unknown>;
136
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,uBAAuB;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,2BAA2B;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,EAC1D,OAAO,GAAE,wBAA6B,GACrC,QAAQ,CAiCV;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,EACxB,OAAO,GAAE,wBAA6B,GACrC,QAAQ,CA8BV;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,iCAAiC;IACjC,MAAM,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAClD,yBAAyB;IACzB,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC;IACtC,0CAA0C;IAC1C,gBAAgB,CAAC,EAAE,CAAC,IAAI,EAAE,eAAe,KAAK,eAAe,CAAC;IAC9D,gCAAgC;IAChC,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IAC3C,oBAAoB;IACpB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,QAAQ,CAAC;CACtC;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,oBAAoB;IACpB,QAAQ,EAAE,KAAK,CAAC;QACd,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,QAAQ,CAAC;QACtC,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC,CAAC;IACH,mBAAmB;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,kBAAkB;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iBAAiB;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,sBAAsB;IACtB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,yBAAyB;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,IAGnC,SAAS,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC,CA2EnE;AA4BD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IAAE,MAAM,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAA;CAAE,IAC5D,MAAM,eAAe,sBAevD"}
package/package.json ADDED
@@ -0,0 +1,93 @@
1
+ {
2
+ "name": "ai.matey.react.nextjs",
3
+ "version": "0.2.0",
4
+ "description": "Next.js integration for AI Matey - App Router, Server Actions, API Routes",
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
+ "./server": {
21
+ "import": {
22
+ "types": "./dist/types/server.d.ts",
23
+ "default": "./dist/esm/server.js"
24
+ },
25
+ "require": {
26
+ "types": "./dist/types/server.d.ts",
27
+ "default": "./dist/cjs/server.js"
28
+ }
29
+ }
30
+ },
31
+ "files": [
32
+ "dist",
33
+ "readme.md",
34
+ "CHANGELOG.md",
35
+ "LICENSE"
36
+ ],
37
+ "scripts": {
38
+ "build": "npm run build:esm && npm run build:cjs && npm run build:types",
39
+ "build:esm": "tsc -p tsconfig.esm.json",
40
+ "build:cjs": "tsc -p tsconfig.cjs.json",
41
+ "build:types": "tsc -p tsconfig.types.json",
42
+ "clean": "rm -rf dist",
43
+ "typecheck": "tsc --noEmit",
44
+ "test": "vitest run"
45
+ },
46
+ "dependencies": {
47
+ "ai.matey.types": "*",
48
+ "ai.matey.core": "*",
49
+ "ai.matey.react.core": "*"
50
+ },
51
+ "peerDependencies": {
52
+ "next": ">=13.0.0",
53
+ "react": ">=18.0.0"
54
+ },
55
+ "peerDependenciesMeta": {
56
+ "next": {
57
+ "optional": false
58
+ },
59
+ "react": {
60
+ "optional": false
61
+ }
62
+ },
63
+ "devDependencies": {
64
+ "@types/react": "^18.2.0",
65
+ "next": "^14.0.0",
66
+ "react": "^18.2.0",
67
+ "typescript": "^5.9.3",
68
+ "vitest": "^3.2.4"
69
+ },
70
+ "keywords": [
71
+ "ai",
72
+ "llm",
73
+ "react",
74
+ "nextjs",
75
+ "app-router",
76
+ "server-actions",
77
+ "ai-matey"
78
+ ],
79
+ "author": "AI Matey",
80
+ "license": "MIT",
81
+ "homepage": "https://github.com/johnhenry/ai.matey#readme",
82
+ "bugs": {
83
+ "url": "https://github.com/johnhenry/ai.matey/issues"
84
+ },
85
+ "repository": {
86
+ "type": "git",
87
+ "url": "git+https://github.com/johnhenry/ai.matey.git",
88
+ "directory": "packages/react-nextjs"
89
+ },
90
+ "engines": {
91
+ "node": ">=18.0.0"
92
+ }
93
+ }