praisonai 1.3.6 → 1.5.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/dist/agent/audio.d.ts +190 -0
- package/dist/agent/audio.js +302 -0
- package/dist/agent/index.d.ts +2 -0
- package/dist/agent/index.js +5 -1
- package/dist/ai/agent-loop.d.ts +177 -0
- package/dist/ai/agent-loop.js +313 -0
- package/dist/ai/devtools.d.ts +74 -0
- package/dist/ai/devtools.js +216 -0
- package/dist/ai/embed.d.ts +71 -0
- package/dist/ai/embed.js +154 -0
- package/dist/ai/generate-image.d.ts +66 -0
- package/dist/ai/generate-image.js +138 -0
- package/dist/ai/generate-object.d.ts +132 -0
- package/dist/ai/generate-object.js +206 -0
- package/dist/ai/generate-text.d.ts +201 -0
- package/dist/ai/generate-text.js +253 -0
- package/dist/ai/index.d.ts +27 -0
- package/dist/ai/index.js +140 -0
- package/dist/ai/mcp.d.ts +181 -0
- package/dist/ai/mcp.js +234 -0
- package/dist/ai/middleware.d.ts +110 -0
- package/dist/ai/middleware.js +186 -0
- package/dist/ai/models.d.ts +67 -0
- package/dist/ai/models.js +249 -0
- package/dist/ai/multimodal.d.ts +125 -0
- package/dist/ai/multimodal.js +270 -0
- package/dist/ai/nextjs.d.ts +122 -0
- package/dist/ai/nextjs.js +218 -0
- package/dist/ai/server.d.ts +126 -0
- package/dist/ai/server.js +336 -0
- package/dist/ai/speech.d.ts +135 -0
- package/dist/ai/speech.js +296 -0
- package/dist/ai/telemetry.d.ts +162 -0
- package/dist/ai/telemetry.js +311 -0
- package/dist/ai/tool-approval.d.ts +225 -0
- package/dist/ai/tool-approval.js +407 -0
- package/dist/ai/tools.d.ts +93 -0
- package/dist/ai/tools.js +89 -0
- package/dist/ai/types.d.ts +47 -0
- package/dist/ai/types.js +5 -0
- package/dist/ai/ui-message.d.ts +218 -0
- package/dist/ai/ui-message.js +291 -0
- package/dist/cli/commands/approval.d.ts +10 -0
- package/dist/cli/commands/approval.js +274 -0
- package/dist/cli/commands/observability.d.ts +7 -0
- package/dist/cli/commands/observability.js +202 -12
- package/dist/cli/commands/providers.d.ts +5 -2
- package/dist/cli/commands/providers.js +204 -5
- package/dist/cli/commands/tools.d.ts +11 -1
- package/dist/cli/commands/tools.js +450 -64
- package/dist/cli/output/errors.d.ts +1 -0
- package/dist/cli/output/errors.js +1 -0
- package/dist/cli/spec/cli-spec.js +17 -0
- package/dist/index.d.ts +21 -4
- package/dist/index.js +275 -19
- package/dist/integrations/computer-use.d.ts +182 -0
- package/dist/integrations/computer-use.js +356 -0
- package/dist/integrations/postgres.d.ts +162 -0
- package/dist/integrations/postgres.js +396 -0
- package/dist/integrations/slack.d.ts +145 -0
- package/dist/integrations/slack.js +270 -0
- package/dist/knowledge/chonkie-adapter.d.ts +99 -0
- package/dist/knowledge/chonkie-adapter.js +268 -0
- package/dist/knowledge/index.d.ts +5 -0
- package/dist/knowledge/index.js +29 -1
- package/dist/knowledge/query-engine.d.ts +136 -0
- package/dist/knowledge/query-engine.js +214 -0
- package/dist/knowledge/rag-pipeline.d.ts +192 -0
- package/dist/knowledge/rag-pipeline.js +283 -0
- package/dist/knowledge/readers.d.ts +129 -0
- package/dist/knowledge/readers.js +393 -0
- package/dist/llm/providers/ai-sdk/types.d.ts +49 -3
- package/dist/llm/providers/ai-sdk/types.js +169 -15
- package/dist/mcp/index.d.ts +188 -0
- package/dist/mcp/index.js +373 -0
- package/dist/mcp/server.d.ts +211 -0
- package/dist/mcp/server.js +412 -0
- package/dist/memory/docs-manager.d.ts +165 -0
- package/dist/memory/docs-manager.js +294 -0
- package/dist/memory/hooks.d.ts +154 -0
- package/dist/memory/hooks.js +228 -0
- package/dist/memory/rules-manager.d.ts +182 -0
- package/dist/memory/rules-manager.js +244 -0
- package/dist/observability/adapters/console.d.ts +33 -0
- package/dist/observability/adapters/console.js +71 -0
- package/dist/observability/adapters/external/arize.d.ts +20 -0
- package/dist/observability/adapters/external/arize.js +22 -0
- package/dist/observability/adapters/external/axiom.d.ts +20 -0
- package/dist/observability/adapters/external/axiom.js +22 -0
- package/dist/observability/adapters/external/braintrust.d.ts +20 -0
- package/dist/observability/adapters/external/braintrust.js +22 -0
- package/dist/observability/adapters/external/helicone.d.ts +20 -0
- package/dist/observability/adapters/external/helicone.js +22 -0
- package/dist/observability/adapters/external/laminar.d.ts +20 -0
- package/dist/observability/adapters/external/laminar.js +22 -0
- package/dist/observability/adapters/external/langfuse.d.ts +28 -0
- package/dist/observability/adapters/external/langfuse.js +186 -0
- package/dist/observability/adapters/external/langsmith.d.ts +21 -0
- package/dist/observability/adapters/external/langsmith.js +83 -0
- package/dist/observability/adapters/external/langwatch.d.ts +20 -0
- package/dist/observability/adapters/external/langwatch.js +22 -0
- package/dist/observability/adapters/external/maxim.d.ts +20 -0
- package/dist/observability/adapters/external/maxim.js +22 -0
- package/dist/observability/adapters/external/patronus.d.ts +20 -0
- package/dist/observability/adapters/external/patronus.js +22 -0
- package/dist/observability/adapters/external/scorecard.d.ts +20 -0
- package/dist/observability/adapters/external/scorecard.js +22 -0
- package/dist/observability/adapters/external/signoz.d.ts +20 -0
- package/dist/observability/adapters/external/signoz.js +22 -0
- package/dist/observability/adapters/external/traceloop.d.ts +20 -0
- package/dist/observability/adapters/external/traceloop.js +22 -0
- package/dist/observability/adapters/external/weave.d.ts +20 -0
- package/dist/observability/adapters/external/weave.js +22 -0
- package/dist/observability/adapters/index.d.ts +19 -0
- package/dist/observability/adapters/index.js +289 -0
- package/dist/observability/adapters/memory.d.ts +41 -0
- package/dist/observability/adapters/memory.js +190 -0
- package/dist/observability/adapters/noop.d.ts +25 -0
- package/dist/observability/adapters/noop.js +64 -0
- package/dist/observability/index.d.ts +39 -78
- package/dist/observability/index.js +99 -150
- package/dist/observability/types.d.ts +165 -0
- package/dist/observability/types.js +165 -0
- package/dist/tools/builtins/airweave.d.ts +32 -0
- package/dist/tools/builtins/airweave.js +119 -0
- package/dist/tools/builtins/bedrock-agentcore.d.ts +62 -0
- package/dist/tools/builtins/bedrock-agentcore.js +233 -0
- package/dist/tools/builtins/code-execution.d.ts +36 -0
- package/dist/tools/builtins/code-execution.js +136 -0
- package/dist/tools/builtins/code-mode.d.ts +54 -0
- package/dist/tools/builtins/code-mode.js +167 -0
- package/dist/tools/builtins/custom.d.ts +42 -0
- package/dist/tools/builtins/custom.js +195 -0
- package/dist/tools/builtins/exa.d.ts +53 -0
- package/dist/tools/builtins/exa.js +112 -0
- package/dist/tools/builtins/firecrawl.d.ts +56 -0
- package/dist/tools/builtins/firecrawl.js +158 -0
- package/dist/tools/builtins/index.d.ts +45 -0
- package/dist/tools/builtins/index.js +180 -0
- package/dist/tools/builtins/parallel.d.ts +30 -0
- package/dist/tools/builtins/parallel.js +113 -0
- package/dist/tools/builtins/perplexity.d.ts +32 -0
- package/dist/tools/builtins/perplexity.js +113 -0
- package/dist/tools/builtins/superagent.d.ts +66 -0
- package/dist/tools/builtins/superagent.js +187 -0
- package/dist/tools/builtins/tavily.d.ts +99 -0
- package/dist/tools/builtins/tavily.js +285 -0
- package/dist/tools/builtins/valyu.d.ts +65 -0
- package/dist/tools/builtins/valyu.js +193 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.js +16 -2
- package/dist/tools/registry/index.d.ts +9 -0
- package/dist/tools/registry/index.js +28 -0
- package/dist/tools/registry/middleware.d.ts +43 -0
- package/dist/tools/registry/middleware.js +220 -0
- package/dist/tools/registry/registry.d.ts +92 -0
- package/dist/tools/registry/registry.js +280 -0
- package/dist/tools/registry/types.d.ts +217 -0
- package/dist/tools/registry/types.js +49 -0
- package/dist/tools/subagent.d.ts +131 -0
- package/dist/tools/subagent.js +185 -0
- package/dist/tools/tools.d.ts +74 -0
- package/dist/tools/tools.js +133 -0
- package/dist/workflows/index.d.ts +76 -1
- package/dist/workflows/index.js +125 -6
- package/dist/workflows/loop.d.ts +111 -0
- package/dist/workflows/loop.js +274 -0
- package/dist/workflows/repeat.d.ts +115 -0
- package/dist/workflows/repeat.js +144 -0
- package/package.json +4 -1
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Multimodal - AI SDK Wrapper
|
|
4
|
+
*
|
|
5
|
+
* Provides utilities for multimodal inputs (images, files, PDFs).
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.createTextPart = createTextPart;
|
|
42
|
+
exports.createImagePart = createImagePart;
|
|
43
|
+
exports.createFilePart = createFilePart;
|
|
44
|
+
exports.createPdfPart = createPdfPart;
|
|
45
|
+
exports.toMessageContent = toMessageContent;
|
|
46
|
+
exports.createMultimodalMessage = createMultimodalMessage;
|
|
47
|
+
exports.base64ToUint8Array = base64ToUint8Array;
|
|
48
|
+
exports.uint8ArrayToBase64 = uint8ArrayToBase64;
|
|
49
|
+
exports.isUrl = isUrl;
|
|
50
|
+
exports.isDataUrl = isDataUrl;
|
|
51
|
+
const fs = __importStar(require("fs"));
|
|
52
|
+
const path = __importStar(require("path"));
|
|
53
|
+
/**
|
|
54
|
+
* Create a text part.
|
|
55
|
+
*/
|
|
56
|
+
function createTextPart(text) {
|
|
57
|
+
return { type: 'text', text };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create an image part from various sources.
|
|
61
|
+
*
|
|
62
|
+
* @example From URL
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const imagePart = createImagePart('https://example.com/image.png');
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @example From base64
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const imagePart = createImagePart('data:image/png;base64,...');
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @example From file path
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const imagePart = await createImagePart('./image.png');
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
function createImagePart(source, mimeType) {
|
|
78
|
+
// If it's a file path, read it
|
|
79
|
+
if (typeof source === 'string' && !source.startsWith('http') && !source.startsWith('data:')) {
|
|
80
|
+
if (fs.existsSync(source)) {
|
|
81
|
+
const buffer = fs.readFileSync(source);
|
|
82
|
+
const detectedMime = mimeType || detectImageMimeType(source);
|
|
83
|
+
return {
|
|
84
|
+
type: 'image',
|
|
85
|
+
image: buffer,
|
|
86
|
+
mimeType: detectedMime,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
type: 'image',
|
|
92
|
+
image: source,
|
|
93
|
+
mimeType,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create a file part from various sources.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const filePart = createFilePart('./document.txt', 'text/plain');
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
function createFilePart(source, mimeType, name) {
|
|
105
|
+
// If it's a file path, read it
|
|
106
|
+
if (typeof source === 'string' && !source.startsWith('http') && !source.startsWith('data:')) {
|
|
107
|
+
if (fs.existsSync(source)) {
|
|
108
|
+
const buffer = fs.readFileSync(source);
|
|
109
|
+
return {
|
|
110
|
+
type: 'file',
|
|
111
|
+
data: buffer,
|
|
112
|
+
mimeType,
|
|
113
|
+
name: name || path.basename(source),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
type: 'file',
|
|
119
|
+
data: source,
|
|
120
|
+
mimeType,
|
|
121
|
+
name,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Create a PDF part from various sources.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* const pdfPart = createPdfPart('./document.pdf');
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @example With page range
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const pdfPart = createPdfPart('./document.pdf', { pages: { start: 1, end: 5 } });
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
function createPdfPart(source, options) {
|
|
138
|
+
// If it's a file path, read it
|
|
139
|
+
if (typeof source === 'string' && !source.startsWith('http') && !source.startsWith('data:')) {
|
|
140
|
+
if (fs.existsSync(source)) {
|
|
141
|
+
const buffer = fs.readFileSync(source);
|
|
142
|
+
return {
|
|
143
|
+
type: 'pdf',
|
|
144
|
+
data: buffer,
|
|
145
|
+
pages: options?.pages,
|
|
146
|
+
textOnly: options?.textOnly,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
type: 'pdf',
|
|
152
|
+
data: source,
|
|
153
|
+
pages: options?.pages,
|
|
154
|
+
textOnly: options?.textOnly,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Convert input parts to AI SDK message content format.
|
|
159
|
+
*/
|
|
160
|
+
function toMessageContent(parts) {
|
|
161
|
+
return parts.map(part => {
|
|
162
|
+
switch (part.type) {
|
|
163
|
+
case 'text':
|
|
164
|
+
return { type: 'text', text: part.text };
|
|
165
|
+
case 'image':
|
|
166
|
+
return {
|
|
167
|
+
type: 'image',
|
|
168
|
+
image: part.image,
|
|
169
|
+
mimeType: part.mimeType,
|
|
170
|
+
};
|
|
171
|
+
case 'file':
|
|
172
|
+
return {
|
|
173
|
+
type: 'file',
|
|
174
|
+
data: part.data,
|
|
175
|
+
mimeType: part.mimeType,
|
|
176
|
+
};
|
|
177
|
+
case 'pdf':
|
|
178
|
+
// PDFs are treated as files with application/pdf mime type
|
|
179
|
+
return {
|
|
180
|
+
type: 'file',
|
|
181
|
+
data: part.data,
|
|
182
|
+
mimeType: 'application/pdf',
|
|
183
|
+
};
|
|
184
|
+
default:
|
|
185
|
+
return part;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Create a multimodal message with text and images.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* const message = createMultimodalMessage(
|
|
195
|
+
* 'What is in this image?',
|
|
196
|
+
* ['https://example.com/image.png']
|
|
197
|
+
* );
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
function createMultimodalMessage(text, images, role = 'user') {
|
|
201
|
+
const content = [];
|
|
202
|
+
// Add images first
|
|
203
|
+
for (const image of images) {
|
|
204
|
+
content.push(createImagePart(image));
|
|
205
|
+
}
|
|
206
|
+
// Add text
|
|
207
|
+
content.push(createTextPart(text));
|
|
208
|
+
return {
|
|
209
|
+
role,
|
|
210
|
+
content: toMessageContent(content),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Detect image MIME type from file extension.
|
|
215
|
+
*/
|
|
216
|
+
function detectImageMimeType(filePath) {
|
|
217
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
218
|
+
const mimeTypes = {
|
|
219
|
+
'.png': 'image/png',
|
|
220
|
+
'.jpg': 'image/jpeg',
|
|
221
|
+
'.jpeg': 'image/jpeg',
|
|
222
|
+
'.gif': 'image/gif',
|
|
223
|
+
'.webp': 'image/webp',
|
|
224
|
+
'.svg': 'image/svg+xml',
|
|
225
|
+
'.bmp': 'image/bmp',
|
|
226
|
+
'.ico': 'image/x-icon',
|
|
227
|
+
};
|
|
228
|
+
return mimeTypes[ext] || 'image/png';
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Convert a base64 string to Uint8Array.
|
|
232
|
+
*/
|
|
233
|
+
function base64ToUint8Array(base64) {
|
|
234
|
+
// Handle data URLs
|
|
235
|
+
const base64Data = base64.includes(',') ? base64.split(',')[1] : base64;
|
|
236
|
+
const binaryString = atob(base64Data);
|
|
237
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
238
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
239
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
240
|
+
}
|
|
241
|
+
return bytes;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Convert Uint8Array to base64 string.
|
|
245
|
+
*/
|
|
246
|
+
function uint8ArrayToBase64(bytes) {
|
|
247
|
+
let binary = '';
|
|
248
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
249
|
+
binary += String.fromCharCode(bytes[i]);
|
|
250
|
+
}
|
|
251
|
+
return btoa(binary);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Check if a string is a valid URL.
|
|
255
|
+
*/
|
|
256
|
+
function isUrl(str) {
|
|
257
|
+
try {
|
|
258
|
+
new URL(str);
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Check if a string is a data URL.
|
|
267
|
+
*/
|
|
268
|
+
function isDataUrl(str) {
|
|
269
|
+
return str.startsWith('data:');
|
|
270
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Next.js Integration - AI SDK Wrapper
|
|
3
|
+
*
|
|
4
|
+
* Provides route handlers and React hooks for Next.js applications.
|
|
5
|
+
*/
|
|
6
|
+
export interface RouteHandlerConfig {
|
|
7
|
+
/** Agent or handler function */
|
|
8
|
+
handler: (input: any) => Promise<any>;
|
|
9
|
+
/** Enable streaming (default: true) */
|
|
10
|
+
streaming?: boolean;
|
|
11
|
+
/** Maximum duration in seconds */
|
|
12
|
+
maxDuration?: number;
|
|
13
|
+
/** CORS configuration */
|
|
14
|
+
cors?: boolean | {
|
|
15
|
+
origin?: string | string[];
|
|
16
|
+
methods?: string[];
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a Next.js App Router route handler.
|
|
21
|
+
*
|
|
22
|
+
* @example app/api/chat/route.ts
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { createRouteHandler } from 'praisonai/ai';
|
|
25
|
+
* import { streamText } from 'praisonai/ai';
|
|
26
|
+
*
|
|
27
|
+
* export const POST = createRouteHandler({
|
|
28
|
+
* handler: async (input) => {
|
|
29
|
+
* return await streamText({
|
|
30
|
+
* model: 'gpt-4o',
|
|
31
|
+
* messages: input.messages
|
|
32
|
+
* });
|
|
33
|
+
* }
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example With Agent
|
|
38
|
+
* ```typescript
|
|
39
|
+
* import { createRouteHandler } from 'praisonai/ai';
|
|
40
|
+
* import { Agent } from 'praisonai';
|
|
41
|
+
*
|
|
42
|
+
* const agent = new Agent({ instructions: 'You are helpful' });
|
|
43
|
+
*
|
|
44
|
+
* export const POST = createRouteHandler({
|
|
45
|
+
* handler: async (input) => {
|
|
46
|
+
* return await agent.chat(input.message);
|
|
47
|
+
* }
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function createRouteHandler(config: RouteHandlerConfig): (request: Request) => Promise<Response>;
|
|
52
|
+
/**
|
|
53
|
+
* Create a Next.js Pages Router API handler.
|
|
54
|
+
*
|
|
55
|
+
* @example pages/api/chat.ts
|
|
56
|
+
* ```typescript
|
|
57
|
+
* import { createPagesHandler } from 'praisonai/ai';
|
|
58
|
+
* import { streamText } from 'praisonai/ai';
|
|
59
|
+
*
|
|
60
|
+
* export default createPagesHandler({
|
|
61
|
+
* handler: async (input) => {
|
|
62
|
+
* return await streamText({
|
|
63
|
+
* model: 'gpt-4o',
|
|
64
|
+
* messages: input.messages
|
|
65
|
+
* });
|
|
66
|
+
* }
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare function createPagesHandler(config: RouteHandlerConfig): (req: any, res: any) => Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Configuration for useChat hook wrapper.
|
|
73
|
+
*/
|
|
74
|
+
export interface UseChatConfig {
|
|
75
|
+
/** API endpoint */
|
|
76
|
+
api?: string;
|
|
77
|
+
/** Initial messages */
|
|
78
|
+
initialMessages?: Array<{
|
|
79
|
+
role: string;
|
|
80
|
+
content: string;
|
|
81
|
+
}>;
|
|
82
|
+
/** On error callback */
|
|
83
|
+
onError?: (error: Error) => void;
|
|
84
|
+
/** On finish callback */
|
|
85
|
+
onFinish?: (message: any) => void;
|
|
86
|
+
/** Custom headers */
|
|
87
|
+
headers?: Record<string, string>;
|
|
88
|
+
/** Custom body */
|
|
89
|
+
body?: Record<string, any>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Note: For React hooks (useChat, useCompletion, useObject),
|
|
93
|
+
* use the AI SDK React package directly:
|
|
94
|
+
*
|
|
95
|
+
* ```typescript
|
|
96
|
+
* import { useChat } from 'ai/react';
|
|
97
|
+
*
|
|
98
|
+
* function ChatComponent() {
|
|
99
|
+
* const { messages, input, handleInputChange, handleSubmit } = useChat({
|
|
100
|
+
* api: '/api/chat'
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* return (
|
|
104
|
+
* <form onSubmit={handleSubmit}>
|
|
105
|
+
* {messages.map(m => <div key={m.id}>{m.content}</div>)}
|
|
106
|
+
* <input value={input} onChange={handleInputChange} />
|
|
107
|
+
* </form>
|
|
108
|
+
* );
|
|
109
|
+
* }
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* The praisonai package focuses on server-side functionality.
|
|
113
|
+
* For client-side React hooks, install and use 'ai/react' directly.
|
|
114
|
+
*/
|
|
115
|
+
/**
|
|
116
|
+
* Export runtime configuration for Next.js edge runtime.
|
|
117
|
+
*/
|
|
118
|
+
export declare const runtime = "edge";
|
|
119
|
+
/**
|
|
120
|
+
* Export max duration configuration.
|
|
121
|
+
*/
|
|
122
|
+
export declare const maxDuration = 30;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Next.js Integration - AI SDK Wrapper
|
|
4
|
+
*
|
|
5
|
+
* Provides route handlers and React hooks for Next.js applications.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.maxDuration = exports.runtime = void 0;
|
|
9
|
+
exports.createRouteHandler = createRouteHandler;
|
|
10
|
+
exports.createPagesHandler = createPagesHandler;
|
|
11
|
+
/**
|
|
12
|
+
* Create a Next.js App Router route handler.
|
|
13
|
+
*
|
|
14
|
+
* @example app/api/chat/route.ts
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { createRouteHandler } from 'praisonai/ai';
|
|
17
|
+
* import { streamText } from 'praisonai/ai';
|
|
18
|
+
*
|
|
19
|
+
* export const POST = createRouteHandler({
|
|
20
|
+
* handler: async (input) => {
|
|
21
|
+
* return await streamText({
|
|
22
|
+
* model: 'gpt-4o',
|
|
23
|
+
* messages: input.messages
|
|
24
|
+
* });
|
|
25
|
+
* }
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example With Agent
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { createRouteHandler } from 'praisonai/ai';
|
|
32
|
+
* import { Agent } from 'praisonai';
|
|
33
|
+
*
|
|
34
|
+
* const agent = new Agent({ instructions: 'You are helpful' });
|
|
35
|
+
*
|
|
36
|
+
* export const POST = createRouteHandler({
|
|
37
|
+
* handler: async (input) => {
|
|
38
|
+
* return await agent.chat(input.message);
|
|
39
|
+
* }
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
function createRouteHandler(config) {
|
|
44
|
+
return async (request) => {
|
|
45
|
+
try {
|
|
46
|
+
// Parse request body
|
|
47
|
+
const body = await request.json();
|
|
48
|
+
// Execute handler
|
|
49
|
+
const result = await config.handler(body);
|
|
50
|
+
// Handle streaming response
|
|
51
|
+
if (config.streaming !== false && result.textStream) {
|
|
52
|
+
const stream = new ReadableStream({
|
|
53
|
+
async start(controller) {
|
|
54
|
+
const encoder = new TextEncoder();
|
|
55
|
+
try {
|
|
56
|
+
for await (const chunk of result.textStream) {
|
|
57
|
+
const data = JSON.stringify({ text: chunk });
|
|
58
|
+
controller.enqueue(encoder.encode(`data: ${data}\n\n`));
|
|
59
|
+
}
|
|
60
|
+
controller.enqueue(encoder.encode('data: [DONE]\n\n'));
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
controller.error(error);
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
controller.close();
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
return new Response(stream, {
|
|
71
|
+
headers: {
|
|
72
|
+
'Content-Type': 'text/event-stream',
|
|
73
|
+
'Cache-Control': 'no-cache',
|
|
74
|
+
'Connection': 'keep-alive',
|
|
75
|
+
...getCorsHeaders(config.cors),
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
// Handle toDataStreamResponse if available (AI SDK)
|
|
80
|
+
if (result.toDataStreamResponse) {
|
|
81
|
+
return result.toDataStreamResponse();
|
|
82
|
+
}
|
|
83
|
+
// Handle regular response
|
|
84
|
+
const responseData = typeof result === 'string'
|
|
85
|
+
? { text: result }
|
|
86
|
+
: result.text
|
|
87
|
+
? { text: result.text, usage: result.usage }
|
|
88
|
+
: result;
|
|
89
|
+
return new Response(JSON.stringify(responseData), {
|
|
90
|
+
headers: {
|
|
91
|
+
'Content-Type': 'application/json',
|
|
92
|
+
...getCorsHeaders(config.cors),
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.error('Route handler error:', error);
|
|
98
|
+
return new Response(JSON.stringify({ error: error.message || 'Internal server error' }), {
|
|
99
|
+
status: 500,
|
|
100
|
+
headers: {
|
|
101
|
+
'Content-Type': 'application/json',
|
|
102
|
+
...getCorsHeaders(config.cors),
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Create a Next.js Pages Router API handler.
|
|
110
|
+
*
|
|
111
|
+
* @example pages/api/chat.ts
|
|
112
|
+
* ```typescript
|
|
113
|
+
* import { createPagesHandler } from 'praisonai/ai';
|
|
114
|
+
* import { streamText } from 'praisonai/ai';
|
|
115
|
+
*
|
|
116
|
+
* export default createPagesHandler({
|
|
117
|
+
* handler: async (input) => {
|
|
118
|
+
* return await streamText({
|
|
119
|
+
* model: 'gpt-4o',
|
|
120
|
+
* messages: input.messages
|
|
121
|
+
* });
|
|
122
|
+
* }
|
|
123
|
+
* });
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
function createPagesHandler(config) {
|
|
127
|
+
return async (req, res) => {
|
|
128
|
+
try {
|
|
129
|
+
// Set CORS headers
|
|
130
|
+
if (config.cors) {
|
|
131
|
+
const headers = getCorsHeaders(config.cors);
|
|
132
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
133
|
+
res.setHeader(key, value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
// Handle preflight
|
|
137
|
+
if (req.method === 'OPTIONS') {
|
|
138
|
+
res.status(204).end();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Execute handler
|
|
142
|
+
const result = await config.handler(req.body);
|
|
143
|
+
// Handle streaming response
|
|
144
|
+
if (config.streaming !== false && result.textStream) {
|
|
145
|
+
res.setHeader('Content-Type', 'text/event-stream');
|
|
146
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
147
|
+
res.setHeader('Connection', 'keep-alive');
|
|
148
|
+
for await (const chunk of result.textStream) {
|
|
149
|
+
res.write(`data: ${JSON.stringify({ text: chunk })}\n\n`);
|
|
150
|
+
}
|
|
151
|
+
res.write('data: [DONE]\n\n');
|
|
152
|
+
res.end();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
// Handle regular response
|
|
156
|
+
const responseData = typeof result === 'string'
|
|
157
|
+
? { text: result }
|
|
158
|
+
: result.text
|
|
159
|
+
? { text: result.text, usage: result.usage }
|
|
160
|
+
: result;
|
|
161
|
+
res.status(200).json(responseData);
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
console.error('Pages handler error:', error);
|
|
165
|
+
res.status(500).json({ error: error.message || 'Internal server error' });
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Get CORS headers based on configuration.
|
|
171
|
+
*/
|
|
172
|
+
function getCorsHeaders(cors) {
|
|
173
|
+
if (!cors)
|
|
174
|
+
return {};
|
|
175
|
+
const config = typeof cors === 'boolean'
|
|
176
|
+
? { origin: '*', methods: ['GET', 'POST', 'OPTIONS'] }
|
|
177
|
+
: cors;
|
|
178
|
+
const origin = Array.isArray(config.origin)
|
|
179
|
+
? config.origin.join(', ')
|
|
180
|
+
: (config.origin || '*');
|
|
181
|
+
return {
|
|
182
|
+
'Access-Control-Allow-Origin': origin,
|
|
183
|
+
'Access-Control-Allow-Methods': (config.methods || ['GET', 'POST', 'OPTIONS']).join(', '),
|
|
184
|
+
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Note: For React hooks (useChat, useCompletion, useObject),
|
|
189
|
+
* use the AI SDK React package directly:
|
|
190
|
+
*
|
|
191
|
+
* ```typescript
|
|
192
|
+
* import { useChat } from 'ai/react';
|
|
193
|
+
*
|
|
194
|
+
* function ChatComponent() {
|
|
195
|
+
* const { messages, input, handleInputChange, handleSubmit } = useChat({
|
|
196
|
+
* api: '/api/chat'
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* return (
|
|
200
|
+
* <form onSubmit={handleSubmit}>
|
|
201
|
+
* {messages.map(m => <div key={m.id}>{m.content}</div>)}
|
|
202
|
+
* <input value={input} onChange={handleInputChange} />
|
|
203
|
+
* </form>
|
|
204
|
+
* );
|
|
205
|
+
* }
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* The praisonai package focuses on server-side functionality.
|
|
209
|
+
* For client-side React hooks, install and use 'ai/react' directly.
|
|
210
|
+
*/
|
|
211
|
+
/**
|
|
212
|
+
* Export runtime configuration for Next.js edge runtime.
|
|
213
|
+
*/
|
|
214
|
+
exports.runtime = 'edge';
|
|
215
|
+
/**
|
|
216
|
+
* Export max duration configuration.
|
|
217
|
+
*/
|
|
218
|
+
exports.maxDuration = 30;
|