paprflare-sdk 0.0.1
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/README.md +15 -0
- package/dist/index.d.mts +759 -0
- package/dist/index.d.ts +759 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +8 -0
- package/dist/index.mjs.map +1 -0
- package/dist/react/index.d.mts +165 -0
- package/dist/react/index.d.ts +165 -0
- package/dist/react/index.js +3 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/index.mjs +3 -0
- package/dist/react/index.mjs.map +1 -0
- package/package.json +77 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,759 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import EventEmitter from 'eventemitter3';
|
|
3
|
+
|
|
4
|
+
interface Message {
|
|
5
|
+
id: string;
|
|
6
|
+
role: 'user' | 'assistant' | 'system' | 'tool';
|
|
7
|
+
content: string;
|
|
8
|
+
toolCalls?: ToolCall[];
|
|
9
|
+
toolResults?: ToolResult[];
|
|
10
|
+
metadata?: Record<string, unknown>;
|
|
11
|
+
timestamp?: number;
|
|
12
|
+
}
|
|
13
|
+
interface ToolCall {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
arguments: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
interface ToolResult {
|
|
19
|
+
toolCallId: string;
|
|
20
|
+
result: unknown;
|
|
21
|
+
error?: string;
|
|
22
|
+
}
|
|
23
|
+
interface Tool {
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
parameters: z.ZodSchema;
|
|
27
|
+
execute?: (args: unknown) => Promise<unknown> | unknown;
|
|
28
|
+
}
|
|
29
|
+
type StreamEvent = {
|
|
30
|
+
type: 'text-delta';
|
|
31
|
+
delta: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: 'tool-call-start';
|
|
34
|
+
toolCall: ToolCall;
|
|
35
|
+
} | {
|
|
36
|
+
type: 'tool-call-delta';
|
|
37
|
+
toolCallId: string;
|
|
38
|
+
delta: string;
|
|
39
|
+
} | {
|
|
40
|
+
type: 'tool-call-end';
|
|
41
|
+
toolCall: ToolCall;
|
|
42
|
+
} | {
|
|
43
|
+
type: 'tool-result';
|
|
44
|
+
result: ToolResult;
|
|
45
|
+
} | {
|
|
46
|
+
type: 'metadata';
|
|
47
|
+
metadata: Record<string, unknown>;
|
|
48
|
+
} | {
|
|
49
|
+
type: 'usage';
|
|
50
|
+
usage: TokenUsage;
|
|
51
|
+
} | {
|
|
52
|
+
type: 'error';
|
|
53
|
+
error: string;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'done';
|
|
56
|
+
finalMessage: Message;
|
|
57
|
+
} | {
|
|
58
|
+
type: 'artifact-start';
|
|
59
|
+
artifact: ArtifactMetadata;
|
|
60
|
+
} | {
|
|
61
|
+
type: 'artifact-delta';
|
|
62
|
+
delta: string;
|
|
63
|
+
} | {
|
|
64
|
+
type: 'artifact-end';
|
|
65
|
+
artifact: Artifact;
|
|
66
|
+
};
|
|
67
|
+
interface TokenUsage {
|
|
68
|
+
promptTokens: number;
|
|
69
|
+
completionTokens: number;
|
|
70
|
+
totalTokens: number;
|
|
71
|
+
}
|
|
72
|
+
interface ArtifactMetadata {
|
|
73
|
+
id: string;
|
|
74
|
+
type: 'code' | 'document' | 'chart' | 'ui-component' | 'custom';
|
|
75
|
+
language?: string;
|
|
76
|
+
title?: string;
|
|
77
|
+
description?: string;
|
|
78
|
+
}
|
|
79
|
+
interface Artifact extends ArtifactMetadata {
|
|
80
|
+
content: string;
|
|
81
|
+
metadata?: Record<string, unknown>;
|
|
82
|
+
}
|
|
83
|
+
interface ChatConfig {
|
|
84
|
+
model?: string;
|
|
85
|
+
temperature?: number;
|
|
86
|
+
maxTokens?: number;
|
|
87
|
+
topP?: number;
|
|
88
|
+
frequencyPenalty?: number;
|
|
89
|
+
presencePenalty?: number;
|
|
90
|
+
stop?: string[];
|
|
91
|
+
tools?: Tool[];
|
|
92
|
+
toolChoice?: 'auto' | 'required' | 'none' | {
|
|
93
|
+
name: string;
|
|
94
|
+
};
|
|
95
|
+
stream?: boolean;
|
|
96
|
+
output?: {
|
|
97
|
+
schema: z.ZodSchema;
|
|
98
|
+
mode?: 'json' | 'function';
|
|
99
|
+
};
|
|
100
|
+
artifacts?: {
|
|
101
|
+
enabled: boolean;
|
|
102
|
+
autoDetect?: boolean;
|
|
103
|
+
};
|
|
104
|
+
cache?: {
|
|
105
|
+
enabled: boolean;
|
|
106
|
+
ttl?: number;
|
|
107
|
+
key?: string;
|
|
108
|
+
};
|
|
109
|
+
retry?: {
|
|
110
|
+
maxAttempts: number;
|
|
111
|
+
initialDelay: number;
|
|
112
|
+
maxDelay: number;
|
|
113
|
+
backoffMultiplier: number;
|
|
114
|
+
};
|
|
115
|
+
metadata?: Record<string, unknown>;
|
|
116
|
+
}
|
|
117
|
+
interface ProviderConfig {
|
|
118
|
+
apiKey: string;
|
|
119
|
+
baseURL?: string;
|
|
120
|
+
organization?: string;
|
|
121
|
+
timeout?: number;
|
|
122
|
+
maxRetries?: number;
|
|
123
|
+
headers?: Record<string, string>;
|
|
124
|
+
}
|
|
125
|
+
type PaprFlareMode = 'cloud' | 'self-managed';
|
|
126
|
+
interface CloudConfig {
|
|
127
|
+
apiKey: string;
|
|
128
|
+
baseURL?: string;
|
|
129
|
+
region?: 'in-mumbai' | 'in-delhi' | 'global';
|
|
130
|
+
}
|
|
131
|
+
interface SelfManagedConfig {
|
|
132
|
+
provider: ProviderCallback;
|
|
133
|
+
cache?: CacheConfig;
|
|
134
|
+
}
|
|
135
|
+
interface CacheConfig {
|
|
136
|
+
type: 'redis' | 'memory';
|
|
137
|
+
redis?: {
|
|
138
|
+
host: string;
|
|
139
|
+
port: number;
|
|
140
|
+
password?: string;
|
|
141
|
+
db?: number;
|
|
142
|
+
};
|
|
143
|
+
memory?: {
|
|
144
|
+
maxSize: number;
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
type ProviderCallback = (messages: Message[], config: ChatConfig) => AsyncGenerator<StreamEvent, void, unknown>;
|
|
148
|
+
interface ChatResponse {
|
|
149
|
+
message: Message;
|
|
150
|
+
usage?: TokenUsage;
|
|
151
|
+
metadata?: Record<string, unknown>;
|
|
152
|
+
}
|
|
153
|
+
interface StreamingChatResponse {
|
|
154
|
+
stream: AsyncGenerator<StreamEvent, void, unknown>;
|
|
155
|
+
toReadableStream: () => ReadableStream<Uint8Array>;
|
|
156
|
+
pipeToResponse: (response: Response) => Promise<void>;
|
|
157
|
+
mergeStreams: (...streams: AsyncGenerator<StreamEvent>[]) => AsyncGenerator<StreamEvent>;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Chat state interface
|
|
162
|
+
*/
|
|
163
|
+
interface ChatState {
|
|
164
|
+
messages: Message[];
|
|
165
|
+
currentMessage: string;
|
|
166
|
+
isStreaming: boolean;
|
|
167
|
+
error: string | null;
|
|
168
|
+
artifacts: Artifact[];
|
|
169
|
+
currentArtifact: Artifact | null;
|
|
170
|
+
toolCalls: ToolCall[];
|
|
171
|
+
usage: TokenUsage | null;
|
|
172
|
+
metadata: Record<string, unknown>;
|
|
173
|
+
addMessage: (message: Message) => void;
|
|
174
|
+
updateMessage: (id: string, updates: Partial<Message>) => void;
|
|
175
|
+
setCurrentMessage: (message: string) => void;
|
|
176
|
+
setStreaming: (isStreaming: boolean) => void;
|
|
177
|
+
setError: (error: string | null) => void;
|
|
178
|
+
addArtifact: (artifact: Artifact) => void;
|
|
179
|
+
setCurrentArtifact: (artifact: Artifact | null) => void;
|
|
180
|
+
addToolCall: (toolCall: ToolCall) => void;
|
|
181
|
+
addToolResult: (result: ToolResult) => void;
|
|
182
|
+
setUsage: (usage: TokenUsage) => void;
|
|
183
|
+
setMetadata: (metadata: Record<string, unknown>) => void;
|
|
184
|
+
reset: () => void;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Hook configuration
|
|
188
|
+
*/
|
|
189
|
+
interface UsePaprFlareConfig {
|
|
190
|
+
apiEndpoint: string;
|
|
191
|
+
apiKey?: string;
|
|
192
|
+
headers?: Record<string, string>;
|
|
193
|
+
defaultConfig?: Partial<ChatConfig>;
|
|
194
|
+
onError?: (error: Error) => void;
|
|
195
|
+
onComplete?: (message: Message) => void;
|
|
196
|
+
onArtifact?: (artifact: Artifact) => void;
|
|
197
|
+
onToolCall?: (toolCall: ToolCall) => void;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface CacheOptions {
|
|
201
|
+
type: 'redis' | 'memory';
|
|
202
|
+
ttl?: number;
|
|
203
|
+
redis?: {
|
|
204
|
+
host: string;
|
|
205
|
+
port: number;
|
|
206
|
+
password?: string;
|
|
207
|
+
db?: number;
|
|
208
|
+
keyPrefix?: string;
|
|
209
|
+
};
|
|
210
|
+
memory?: {
|
|
211
|
+
maxSize: number;
|
|
212
|
+
maxAge?: number;
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Abstract cache interface
|
|
217
|
+
*/
|
|
218
|
+
interface ICache {
|
|
219
|
+
get(key: string): Promise<string | null>;
|
|
220
|
+
set(key: string, value: string, ttl?: number): Promise<void>;
|
|
221
|
+
delete(key: string): Promise<void>;
|
|
222
|
+
clear(): Promise<void>;
|
|
223
|
+
has(key: string): Promise<boolean>;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Redis cache implementation for distributed caching
|
|
227
|
+
*/
|
|
228
|
+
declare class RedisCache implements ICache {
|
|
229
|
+
private client;
|
|
230
|
+
private keyPrefix;
|
|
231
|
+
private defaultTTL;
|
|
232
|
+
constructor(options: CacheOptions['redis'] & {
|
|
233
|
+
ttl?: number;
|
|
234
|
+
});
|
|
235
|
+
private getKey;
|
|
236
|
+
get(key: string): Promise<string | null>;
|
|
237
|
+
set(key: string, value: string, ttl?: number): Promise<void>;
|
|
238
|
+
delete(key: string): Promise<void>;
|
|
239
|
+
clear(): Promise<void>;
|
|
240
|
+
has(key: string): Promise<boolean>;
|
|
241
|
+
disconnect(): Promise<void>;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* In-memory LRU cache for single-instance or development use
|
|
245
|
+
*/
|
|
246
|
+
declare class MemoryCache implements ICache {
|
|
247
|
+
private cache;
|
|
248
|
+
constructor(options: CacheOptions['memory']);
|
|
249
|
+
get(key: string): Promise<string | null>;
|
|
250
|
+
set(key: string, value: string): Promise<void>;
|
|
251
|
+
delete(key: string): Promise<void>;
|
|
252
|
+
clear(): Promise<void>;
|
|
253
|
+
has(key: string): Promise<boolean>;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Cache manager with automatic cache key generation
|
|
257
|
+
*/
|
|
258
|
+
declare class CacheManager {
|
|
259
|
+
private cache;
|
|
260
|
+
private enabled;
|
|
261
|
+
constructor(options: CacheOptions);
|
|
262
|
+
/**
|
|
263
|
+
* Generate cache key from messages and config
|
|
264
|
+
*/
|
|
265
|
+
private generateCacheKey;
|
|
266
|
+
/**
|
|
267
|
+
* Get cached response
|
|
268
|
+
*/
|
|
269
|
+
getCachedResponse(messages: Message[], config: ChatConfig): Promise<StreamEvent[] | null>;
|
|
270
|
+
/**
|
|
271
|
+
* Cache response
|
|
272
|
+
*/
|
|
273
|
+
cacheResponse(messages: Message[], config: ChatConfig, events: StreamEvent[]): Promise<void>;
|
|
274
|
+
/**
|
|
275
|
+
* Invalidate cache
|
|
276
|
+
*/
|
|
277
|
+
invalidate(messages: Message[], config: ChatConfig): Promise<void>;
|
|
278
|
+
/**
|
|
279
|
+
* Clear all cache
|
|
280
|
+
*/
|
|
281
|
+
clearAll(): Promise<void>;
|
|
282
|
+
/**
|
|
283
|
+
* Disable caching
|
|
284
|
+
*/
|
|
285
|
+
disable(): void;
|
|
286
|
+
/**
|
|
287
|
+
* Enable caching
|
|
288
|
+
*/
|
|
289
|
+
enable(): void;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
interface PaprFlareClientConfig {
|
|
293
|
+
mode: PaprFlareMode;
|
|
294
|
+
cloud?: CloudConfig;
|
|
295
|
+
selfManaged?: SelfManagedConfig;
|
|
296
|
+
rateLimit?: {
|
|
297
|
+
maxConcurrent?: number;
|
|
298
|
+
interval?: number;
|
|
299
|
+
intervalCap?: number;
|
|
300
|
+
};
|
|
301
|
+
defaultRetry?: {
|
|
302
|
+
maxAttempts: number;
|
|
303
|
+
initialDelay: number;
|
|
304
|
+
maxDelay: number;
|
|
305
|
+
backoffMultiplier: number;
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Main PaprFlare client for AI interactions
|
|
310
|
+
* Optimized for millions of throughputs with built-in caching, rate limiting, and retry logic
|
|
311
|
+
*/
|
|
312
|
+
declare class PaprFlareClient {
|
|
313
|
+
private mode;
|
|
314
|
+
private cloudConfig?;
|
|
315
|
+
private providerCallback?;
|
|
316
|
+
private cacheManager?;
|
|
317
|
+
private queue;
|
|
318
|
+
private mutex;
|
|
319
|
+
private defaultRetryConfig;
|
|
320
|
+
constructor(config: PaprFlareClientConfig);
|
|
321
|
+
/**
|
|
322
|
+
* Main chat method - handles both streaming and non-streaming responses
|
|
323
|
+
*/
|
|
324
|
+
chat(messages: Message[], config?: ChatConfig): Promise<ChatResponse | StreamingChatResponse>;
|
|
325
|
+
/**
|
|
326
|
+
* Execute chat with retry logic
|
|
327
|
+
*/
|
|
328
|
+
private executeChat;
|
|
329
|
+
/**
|
|
330
|
+
* Execute chat in cloud mode
|
|
331
|
+
*/
|
|
332
|
+
private executeCloudChat;
|
|
333
|
+
/**
|
|
334
|
+
* Execute chat in self-managed mode
|
|
335
|
+
*/
|
|
336
|
+
private executeSelfManagedChat;
|
|
337
|
+
/**
|
|
338
|
+
* Execute structured output generation
|
|
339
|
+
*/
|
|
340
|
+
private executeStructuredOutput;
|
|
341
|
+
/**
|
|
342
|
+
* Create streaming response object
|
|
343
|
+
*/
|
|
344
|
+
private createStreamingResponse;
|
|
345
|
+
/**
|
|
346
|
+
* Parse cloud API stream
|
|
347
|
+
*/
|
|
348
|
+
private parseCloudStream;
|
|
349
|
+
/**
|
|
350
|
+
* Cache stream events
|
|
351
|
+
*/
|
|
352
|
+
private cacheStream;
|
|
353
|
+
/**
|
|
354
|
+
* Convert events to generator
|
|
355
|
+
*/
|
|
356
|
+
private eventsToGenerator;
|
|
357
|
+
/**
|
|
358
|
+
* Convert events to response
|
|
359
|
+
*/
|
|
360
|
+
private eventsToResponse;
|
|
361
|
+
/**
|
|
362
|
+
* Validate configuration
|
|
363
|
+
*/
|
|
364
|
+
private validateConfig;
|
|
365
|
+
/**
|
|
366
|
+
* Get queue statistics
|
|
367
|
+
*/
|
|
368
|
+
getQueueStats(): {
|
|
369
|
+
size: number;
|
|
370
|
+
pending: number;
|
|
371
|
+
concurrency: number;
|
|
372
|
+
};
|
|
373
|
+
/**
|
|
374
|
+
* Clear cache
|
|
375
|
+
*/
|
|
376
|
+
clearCache(): Promise<void>;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Create structured output helper
|
|
380
|
+
*/
|
|
381
|
+
declare function structuredOutput<T extends z.ZodSchema>(schema: T): {
|
|
382
|
+
schema: T;
|
|
383
|
+
mode: "json";
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
interface AnthropicConfig extends ProviderConfig {
|
|
387
|
+
model?: string;
|
|
388
|
+
anthropicVersion?: string;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Anthropic (Claude) provider for PaprFlare
|
|
392
|
+
*/
|
|
393
|
+
declare class AnthropicProvider {
|
|
394
|
+
private config;
|
|
395
|
+
private baseURL;
|
|
396
|
+
constructor(config: AnthropicConfig);
|
|
397
|
+
/**
|
|
398
|
+
* Create provider callback for PaprFlare client
|
|
399
|
+
*/
|
|
400
|
+
createCallback(): ProviderCallback;
|
|
401
|
+
/**
|
|
402
|
+
* Stream chat completion
|
|
403
|
+
*/
|
|
404
|
+
private streamChat;
|
|
405
|
+
/**
|
|
406
|
+
* Parse Anthropic SSE stream
|
|
407
|
+
*/
|
|
408
|
+
private parseStream;
|
|
409
|
+
/**
|
|
410
|
+
* Format messages for Anthropic API
|
|
411
|
+
*/
|
|
412
|
+
private formatMessages;
|
|
413
|
+
/**
|
|
414
|
+
* Format tools for Anthropic API
|
|
415
|
+
*/
|
|
416
|
+
private formatTools;
|
|
417
|
+
/**
|
|
418
|
+
* Convert Zod schema to JSON Schema (simplified)
|
|
419
|
+
*/
|
|
420
|
+
private zodToJsonSchema;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Create Anthropic provider instance
|
|
424
|
+
*/
|
|
425
|
+
declare function createAnthropicProvider(config: AnthropicConfig): ProviderCallback;
|
|
426
|
+
|
|
427
|
+
interface GoogleConfig extends ProviderConfig {
|
|
428
|
+
model?: string;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Google (Gemini) provider for PaprFlare
|
|
432
|
+
*/
|
|
433
|
+
declare class GoogleProvider {
|
|
434
|
+
private config;
|
|
435
|
+
private baseURL;
|
|
436
|
+
constructor(config: GoogleConfig);
|
|
437
|
+
/**
|
|
438
|
+
* Create provider callback for PaprFlare client
|
|
439
|
+
*/
|
|
440
|
+
createCallback(): ProviderCallback;
|
|
441
|
+
/**
|
|
442
|
+
* Stream chat completion
|
|
443
|
+
*/
|
|
444
|
+
private streamChat;
|
|
445
|
+
/**
|
|
446
|
+
* Parse Google AI stream
|
|
447
|
+
*/
|
|
448
|
+
private parseStream;
|
|
449
|
+
/**
|
|
450
|
+
* Format messages for Google AI API
|
|
451
|
+
*/
|
|
452
|
+
private formatMessages;
|
|
453
|
+
/**
|
|
454
|
+
* Format tools for Google AI API
|
|
455
|
+
*/
|
|
456
|
+
private formatTools;
|
|
457
|
+
/**
|
|
458
|
+
* Convert Zod schema to JSON Schema (simplified)
|
|
459
|
+
*/
|
|
460
|
+
private zodToJsonSchema;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Create Google provider instance
|
|
464
|
+
*/
|
|
465
|
+
declare function createGoogleProvider(config: GoogleConfig): ProviderCallback;
|
|
466
|
+
|
|
467
|
+
interface OpenAIConfig extends ProviderConfig {
|
|
468
|
+
model?: string;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* OpenAI provider for PaprFlare
|
|
472
|
+
*/
|
|
473
|
+
declare class OpenAIProvider {
|
|
474
|
+
private config;
|
|
475
|
+
private baseURL;
|
|
476
|
+
constructor(config: OpenAIConfig);
|
|
477
|
+
/**
|
|
478
|
+
* Create provider callback for PaprFlare client
|
|
479
|
+
*/
|
|
480
|
+
createCallback(): ProviderCallback;
|
|
481
|
+
/**
|
|
482
|
+
* Stream chat completion
|
|
483
|
+
*/
|
|
484
|
+
private streamChat;
|
|
485
|
+
/**
|
|
486
|
+
* Parse OpenAI SSE stream
|
|
487
|
+
*/
|
|
488
|
+
private parseStream;
|
|
489
|
+
/**
|
|
490
|
+
* Format messages for OpenAI API
|
|
491
|
+
*/
|
|
492
|
+
private formatMessages;
|
|
493
|
+
/**
|
|
494
|
+
* Format tools for OpenAI API
|
|
495
|
+
*/
|
|
496
|
+
private formatTools;
|
|
497
|
+
/**
|
|
498
|
+
* Convert Zod schema to JSON Schema (simplified)
|
|
499
|
+
*/
|
|
500
|
+
private zodToJsonSchema;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Create OpenAI provider instance
|
|
504
|
+
*/
|
|
505
|
+
declare function createOpenAIProvider(config: OpenAIConfig): ProviderCallback;
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Token counter with caching for high performance
|
|
509
|
+
*/
|
|
510
|
+
declare class TokenCounter {
|
|
511
|
+
private encoding;
|
|
512
|
+
private cache;
|
|
513
|
+
private maxCacheSize;
|
|
514
|
+
constructor(model?: string, maxCacheSize?: number);
|
|
515
|
+
/**
|
|
516
|
+
* Count tokens in a string
|
|
517
|
+
*/
|
|
518
|
+
countTokens(text: string): number;
|
|
519
|
+
/**
|
|
520
|
+
* Count tokens in messages
|
|
521
|
+
*/
|
|
522
|
+
countMessageTokens(messages: Message[]): number;
|
|
523
|
+
/**
|
|
524
|
+
* Estimate cost based on token count
|
|
525
|
+
* Prices are approximate and should be updated based on actual provider pricing
|
|
526
|
+
*/
|
|
527
|
+
estimateCost(promptTokens: number, completionTokens: number, model: string): number;
|
|
528
|
+
/**
|
|
529
|
+
* Clear the cache
|
|
530
|
+
*/
|
|
531
|
+
clearCache(): void;
|
|
532
|
+
/**
|
|
533
|
+
* Free the encoding
|
|
534
|
+
*/
|
|
535
|
+
dispose(): void;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Get the default token counter instance
|
|
539
|
+
*/
|
|
540
|
+
declare function getTokenCounter(model?: string): TokenCounter;
|
|
541
|
+
/**
|
|
542
|
+
* Quick token counting function
|
|
543
|
+
*/
|
|
544
|
+
declare function countTokens(text: string, model?: string): number;
|
|
545
|
+
/**
|
|
546
|
+
* Quick message token counting function
|
|
547
|
+
*/
|
|
548
|
+
declare function countMessageTokens(messages: Message[], model?: string): number;
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Generate a unique ID
|
|
552
|
+
*/
|
|
553
|
+
declare function generateId(prefix?: string): string;
|
|
554
|
+
/**
|
|
555
|
+
* Create a stable hash for cache keys
|
|
556
|
+
*/
|
|
557
|
+
declare function createCacheKey(data: unknown): string;
|
|
558
|
+
/**
|
|
559
|
+
* Format messages for provider APIs
|
|
560
|
+
*/
|
|
561
|
+
declare function formatMessages(messages: Message[]): Message[];
|
|
562
|
+
/**
|
|
563
|
+
* Retry with exponential backoff
|
|
564
|
+
*/
|
|
565
|
+
declare function retry<T>(fn: () => Promise<T>, options: {
|
|
566
|
+
maxAttempts: number;
|
|
567
|
+
initialDelay: number;
|
|
568
|
+
maxDelay: number;
|
|
569
|
+
backoffMultiplier: number;
|
|
570
|
+
onRetry?: (attempt: number, error: Error) => void;
|
|
571
|
+
}): Promise<T>;
|
|
572
|
+
/**
|
|
573
|
+
* Sleep for a given duration
|
|
574
|
+
*/
|
|
575
|
+
declare function sleep(ms: number): Promise<void>;
|
|
576
|
+
/**
|
|
577
|
+
* Chunk an array into smaller arrays
|
|
578
|
+
*/
|
|
579
|
+
declare function chunk<T>(array: T[], size: number): T[][];
|
|
580
|
+
/**
|
|
581
|
+
* Debounce a function
|
|
582
|
+
*/
|
|
583
|
+
declare function debounce<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void;
|
|
584
|
+
/**
|
|
585
|
+
* Throttle a function
|
|
586
|
+
*/
|
|
587
|
+
declare function throttle<T extends (...args: any[]) => any>(fn: T, limit: number): (...args: Parameters<T>) => void;
|
|
588
|
+
/**
|
|
589
|
+
* Safely parse JSON
|
|
590
|
+
*/
|
|
591
|
+
declare function safeJsonParse<T = unknown>(json: string): T | null;
|
|
592
|
+
/**
|
|
593
|
+
* Check if a value is a plain object
|
|
594
|
+
*/
|
|
595
|
+
declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
596
|
+
/**
|
|
597
|
+
* Deep merge objects
|
|
598
|
+
*/
|
|
599
|
+
declare function deepMerge<T extends Record<string, any>>(target: T, ...sources: Partial<T>[]): T;
|
|
600
|
+
/**
|
|
601
|
+
* Convert async generator to array
|
|
602
|
+
*/
|
|
603
|
+
declare function streamToArray<T>(stream: AsyncGenerator<T>): Promise<T[]>;
|
|
604
|
+
/**
|
|
605
|
+
* Measure execution time
|
|
606
|
+
*/
|
|
607
|
+
declare function measureTime<T>(fn: () => Promise<T>): Promise<{
|
|
608
|
+
result: T;
|
|
609
|
+
duration: number;
|
|
610
|
+
}>;
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* StreamWriter for generating and managing streaming events
|
|
614
|
+
* Handles live event emission for real-time AI responses
|
|
615
|
+
*/
|
|
616
|
+
declare class StreamWriter extends EventEmitter {
|
|
617
|
+
private debug;
|
|
618
|
+
private events;
|
|
619
|
+
private currentText;
|
|
620
|
+
private currentToolCall;
|
|
621
|
+
private currentArtifact;
|
|
622
|
+
private metadata;
|
|
623
|
+
private closed;
|
|
624
|
+
constructor(debug?: boolean);
|
|
625
|
+
/**
|
|
626
|
+
* Write text delta
|
|
627
|
+
*/
|
|
628
|
+
writeText(delta: string): void;
|
|
629
|
+
/**
|
|
630
|
+
* Start a tool call
|
|
631
|
+
*/
|
|
632
|
+
startToolCall(name: string, toolCallId?: string): void;
|
|
633
|
+
/**
|
|
634
|
+
* Write tool call arguments delta
|
|
635
|
+
*/
|
|
636
|
+
writeToolCallDelta(delta: string): void;
|
|
637
|
+
/**
|
|
638
|
+
* End tool call with complete arguments
|
|
639
|
+
*/
|
|
640
|
+
endToolCall(args: Record<string, unknown>): void;
|
|
641
|
+
/**
|
|
642
|
+
* Write tool result
|
|
643
|
+
*/
|
|
644
|
+
writeToolResult(toolCallId: string, result: unknown, error?: string): void;
|
|
645
|
+
/**
|
|
646
|
+
* Start an artifact
|
|
647
|
+
*/
|
|
648
|
+
startArtifact(type: Artifact['type'], options?: {
|
|
649
|
+
id?: string;
|
|
650
|
+
language?: string;
|
|
651
|
+
title?: string;
|
|
652
|
+
description?: string;
|
|
653
|
+
}): void;
|
|
654
|
+
/**
|
|
655
|
+
* Write artifact content delta
|
|
656
|
+
*/
|
|
657
|
+
writeArtifactDelta(delta: string): void;
|
|
658
|
+
/**
|
|
659
|
+
* End artifact
|
|
660
|
+
*/
|
|
661
|
+
endArtifact(metadata?: Record<string, unknown>): void;
|
|
662
|
+
/**
|
|
663
|
+
* Write metadata
|
|
664
|
+
*/
|
|
665
|
+
writeMetadata(metadata: Record<string, unknown>): void;
|
|
666
|
+
/**
|
|
667
|
+
* Write usage information
|
|
668
|
+
*/
|
|
669
|
+
writeUsage(usage: TokenUsage): void;
|
|
670
|
+
/**
|
|
671
|
+
* Write error
|
|
672
|
+
*/
|
|
673
|
+
writeError(error: string): void;
|
|
674
|
+
/**
|
|
675
|
+
* Complete the stream
|
|
676
|
+
*/
|
|
677
|
+
done(finalMessage?: Partial<Message>): void;
|
|
678
|
+
/**
|
|
679
|
+
* Get all collected events
|
|
680
|
+
*/
|
|
681
|
+
getEvents(): StreamEvent[];
|
|
682
|
+
/**
|
|
683
|
+
* Convert to async generator
|
|
684
|
+
*/
|
|
685
|
+
toGenerator(): AsyncGenerator<StreamEvent>;
|
|
686
|
+
/**
|
|
687
|
+
* Emit event (internal)
|
|
688
|
+
*/
|
|
689
|
+
private emitEvent;
|
|
690
|
+
/**
|
|
691
|
+
* Check if stream is closed
|
|
692
|
+
*/
|
|
693
|
+
isClosed(): boolean;
|
|
694
|
+
/**
|
|
695
|
+
* Reset the writer
|
|
696
|
+
*/
|
|
697
|
+
reset(): void;
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Create a stream writer
|
|
701
|
+
*/
|
|
702
|
+
declare function createStreamWriter(debug?: boolean): StreamWriter;
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* Parse SSE stream into structured events
|
|
706
|
+
*/
|
|
707
|
+
declare function parseSSEStream(stream: ReadableStream<Uint8Array>): AsyncGenerator<StreamEvent>;
|
|
708
|
+
/**
|
|
709
|
+
* Convert async generator to ReadableStream
|
|
710
|
+
*/
|
|
711
|
+
declare function toReadableStream(generator: AsyncGenerator<StreamEvent>): ReadableStream<Uint8Array>;
|
|
712
|
+
/**
|
|
713
|
+
* Pipe stream to HTTP response (h3 compatible)
|
|
714
|
+
*/
|
|
715
|
+
declare function pipeToResponse(generator: AsyncGenerator<StreamEvent>, response: any): Promise<void>;
|
|
716
|
+
/**
|
|
717
|
+
* Merge multiple streams into one
|
|
718
|
+
*/
|
|
719
|
+
declare function mergeStreams(...generators: AsyncGenerator<StreamEvent>[]): AsyncGenerator<StreamEvent>;
|
|
720
|
+
/**
|
|
721
|
+
* Transform stream events
|
|
722
|
+
*/
|
|
723
|
+
declare function transformStream<T>(generator: AsyncGenerator<StreamEvent>, transformer: (event: StreamEvent) => T | null): AsyncGenerator<T>;
|
|
724
|
+
/**
|
|
725
|
+
* Filter stream events
|
|
726
|
+
*/
|
|
727
|
+
declare function filterStream(generator: AsyncGenerator<StreamEvent>, predicate: (event: StreamEvent) => boolean): AsyncGenerator<StreamEvent>;
|
|
728
|
+
/**
|
|
729
|
+
* Collect stream into array
|
|
730
|
+
*/
|
|
731
|
+
declare function collectStream(generator: AsyncGenerator<StreamEvent>): Promise<StreamEvent[]>;
|
|
732
|
+
/**
|
|
733
|
+
* Take first N events from stream
|
|
734
|
+
*/
|
|
735
|
+
declare function takeStream(generator: AsyncGenerator<StreamEvent>, count: number): AsyncGenerator<StreamEvent>;
|
|
736
|
+
/**
|
|
737
|
+
* Batch stream events
|
|
738
|
+
*/
|
|
739
|
+
declare function batchStream(generator: AsyncGenerator<StreamEvent>, size: number, timeoutMs?: number): AsyncGenerator<StreamEvent[]>;
|
|
740
|
+
|
|
741
|
+
declare class PaprFlareError extends Error {
|
|
742
|
+
code: string;
|
|
743
|
+
statusCode?: number | undefined;
|
|
744
|
+
details?: unknown | undefined;
|
|
745
|
+
constructor(message: string, code: string, statusCode?: number | undefined, details?: unknown | undefined);
|
|
746
|
+
}
|
|
747
|
+
declare class RateLimitError extends PaprFlareError {
|
|
748
|
+
retryAfter?: number | undefined;
|
|
749
|
+
constructor(message: string, retryAfter?: number | undefined);
|
|
750
|
+
}
|
|
751
|
+
declare class ValidationError extends PaprFlareError {
|
|
752
|
+
constructor(message: string, details?: unknown);
|
|
753
|
+
}
|
|
754
|
+
declare class ProviderError extends PaprFlareError {
|
|
755
|
+
provider: string;
|
|
756
|
+
constructor(message: string, provider: string, statusCode?: number);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
export { type AnthropicConfig, AnthropicProvider, type Artifact, type ArtifactMetadata, type CacheConfig, CacheManager, type CacheOptions, type ChatConfig, type ChatResponse, type ChatState, type CloudConfig, type GoogleConfig, GoogleProvider, type ICache, MemoryCache, type Message, type OpenAIConfig, OpenAIProvider, PaprFlareClient, type PaprFlareClientConfig, PaprFlareError, type PaprFlareMode, type ProviderCallback, type ProviderConfig, ProviderError, RateLimitError, RedisCache, type SelfManagedConfig, type StreamEvent, StreamWriter, type StreamingChatResponse, TokenCounter, type TokenUsage, type Tool, type ToolCall, type ToolResult, type UsePaprFlareConfig, ValidationError, batchStream, chunk, collectStream, countMessageTokens, countTokens, createAnthropicProvider, createCacheKey, createGoogleProvider, createOpenAIProvider, createStreamWriter, debounce, deepMerge, filterStream, formatMessages, generateId, getTokenCounter, isPlainObject, measureTime, mergeStreams, parseSSEStream, pipeToResponse, retry, safeJsonParse, sleep, streamToArray, structuredOutput, takeStream, throttle, toReadableStream, transformStream };
|