@umituz/web-ai-groq-provider 1.1.13 → 2.0.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.
Files changed (55) hide show
  1. package/README.md +154 -72
  2. package/dist/domains/chat/index.cjs +539 -0
  3. package/dist/domains/chat/index.cjs.map +1 -0
  4. package/dist/domains/chat/index.d.cts +322 -0
  5. package/dist/domains/chat/index.d.ts +322 -0
  6. package/dist/domains/chat/index.js +531 -0
  7. package/dist/domains/chat/index.js.map +1 -0
  8. package/dist/domains/groq/index.cjs +1499 -0
  9. package/dist/domains/groq/index.cjs.map +1 -0
  10. package/dist/domains/groq/index.d.cts +561 -0
  11. package/dist/domains/groq/index.d.ts +561 -0
  12. package/dist/domains/groq/index.js +1453 -0
  13. package/dist/domains/groq/index.js.map +1 -0
  14. package/dist/index.cjs +1993 -0
  15. package/dist/index.cjs.map +1 -0
  16. package/dist/index.d.cts +53 -0
  17. package/dist/index.d.ts +53 -0
  18. package/dist/index.js +1937 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/text-generation.service-BP4Q_SH0.d.cts +252 -0
  21. package/dist/text-generation.service-BP4Q_SH0.d.ts +252 -0
  22. package/package.json +25 -11
  23. package/src/domains/chat/entities/chat.entities.ts +0 -113
  24. package/src/domains/chat/entities/index.ts +0 -14
  25. package/src/domains/chat/hooks/index.ts +0 -5
  26. package/src/domains/chat/hooks/use-chat.hook.ts +0 -259
  27. package/src/domains/chat/index.ts +0 -32
  28. package/src/domains/chat/interfaces/chat.interface.ts +0 -80
  29. package/src/domains/chat/interfaces/index.ts +0 -10
  30. package/src/domains/chat/services/chat.service.ts +0 -195
  31. package/src/domains/chat/services/index.ts +0 -5
  32. package/src/domains/chat/utils/message-formatter.ts +0 -49
  33. package/src/domains/groq/constants/error.constants.ts +0 -46
  34. package/src/domains/groq/constants/groq.constants.ts +0 -68
  35. package/src/domains/groq/constants/index.ts +0 -20
  36. package/src/domains/groq/entities/groq.entities.ts +0 -196
  37. package/src/domains/groq/entities/index.ts +0 -20
  38. package/src/domains/groq/hooks/index.ts +0 -7
  39. package/src/domains/groq/hooks/use-groq.hook.ts +0 -232
  40. package/src/domains/groq/index.ts +0 -72
  41. package/src/domains/groq/interfaces/groq.interface.ts +0 -77
  42. package/src/domains/groq/interfaces/index.ts +0 -22
  43. package/src/domains/groq/services/http-client.service.ts +0 -265
  44. package/src/domains/groq/services/index.ts +0 -9
  45. package/src/domains/groq/services/text-generation.service.ts +0 -238
  46. package/src/domains/groq/utils/cache-manager.util.ts +0 -215
  47. package/src/domains/groq/utils/debounce.util.ts +0 -268
  48. package/src/domains/groq/utils/error.util.ts +0 -57
  49. package/src/domains/groq/utils/groq-error.util.ts +0 -18
  50. package/src/domains/groq/utils/index.ts +0 -37
  51. package/src/domains/groq/utils/message.util.ts +0 -57
  52. package/src/domains/groq/utils/request-deduplicator.util.ts +0 -123
  53. package/src/domains/groq/utils/request-queue.util.ts +0 -178
  54. package/src/domains/groq/utils/retry.util.ts +0 -207
  55. package/src/index.ts +0 -15
@@ -1,215 +0,0 @@
1
- /**
2
- * Cache Manager Utility
3
- * @description LRU cache for storing API responses
4
- */
5
-
6
- interface CacheEntry<T> {
7
- value: T;
8
- timestamp: number;
9
- accessCount: number;
10
- lastAccess: number;
11
- }
12
-
13
- interface CacheOptions {
14
- /** Maximum number of entries in cache */
15
- maxSize?: number;
16
- /** Time-to-live for cache entries in milliseconds */
17
- ttl?: number;
18
- /** Custom key generator */
19
- keyGenerator?: (input: unknown) => string;
20
- }
21
-
22
- class CacheManager {
23
- private cache = new Map<string, CacheEntry<unknown>>();
24
- private readonly maxSize: number;
25
- private readonly ttl: number;
26
-
27
- constructor(options: CacheOptions = {}) {
28
- this.maxSize = options.maxSize || 100; // Default 100 entries
29
- this.ttl = options.ttl || 300000; // 5 minutes default TTL
30
- }
31
-
32
- /**
33
- * Get a value from cache
34
- */
35
- get<T>(key: string): T | null {
36
- const entry = this.cache.get(key);
37
-
38
- if (!entry) {
39
- return null;
40
- }
41
-
42
- // Check if entry has expired
43
- if (this.isExpired(entry)) {
44
- this.cache.delete(key);
45
- return null;
46
- }
47
-
48
- // Update access statistics
49
- entry.accessCount++;
50
- entry.lastAccess = Date.now();
51
-
52
- return entry.value as T;
53
- }
54
-
55
- /**
56
- * Set a value in cache
57
- */
58
- set<T>(key: string, value: T): void {
59
- // If cache is full, remove least recently used entry
60
- if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
61
- this.evictLRU();
62
- }
63
-
64
- this.cache.set(key, {
65
- value,
66
- timestamp: Date.now(),
67
- accessCount: 1,
68
- lastAccess: Date.now(),
69
- });
70
- }
71
-
72
- /**
73
- * Check if key exists and is not expired
74
- */
75
- has(key: string): boolean {
76
- const entry = this.cache.get(key);
77
- return entry !== undefined && !this.isExpired(entry);
78
- }
79
-
80
- /**
81
- * Delete a specific entry
82
- */
83
- delete(key: string): boolean {
84
- return this.cache.delete(key);
85
- }
86
-
87
- /**
88
- * Clear all cache entries
89
- */
90
- clear(): void {
91
- this.cache.clear();
92
- }
93
-
94
- /**
95
- * Get cache statistics
96
- */
97
- getStats(): {
98
- size: number;
99
- maxSize: number;
100
- hitRate: number;
101
- entries: Array<{ key: string; accessCount: number; age: number }>;
102
- } {
103
- const now = Date.now();
104
- const entries = Array.from(this.cache.entries()).map(([key, entry]) => ({
105
- key,
106
- accessCount: entry.accessCount,
107
- age: now - entry.timestamp,
108
- }));
109
-
110
- // Calculate average hit rate
111
- const totalAccessCount = entries.reduce(
112
- (sum, e) => sum + e.accessCount,
113
- 0
114
- );
115
- const hitRate = this.cache.size > 0 ? totalAccessCount / this.cache.size : 0;
116
-
117
- return {
118
- size: this.cache.size,
119
- maxSize: this.maxSize,
120
- hitRate,
121
- entries,
122
- };
123
- }
124
-
125
- /**
126
- * Clean up expired entries
127
- */
128
- cleanup(): number {
129
- let removed = 0;
130
- for (const [key, entry] of this.cache.entries()) {
131
- if (this.isExpired(entry)) {
132
- this.cache.delete(key);
133
- removed++;
134
- }
135
- }
136
- return removed;
137
- }
138
-
139
- /**
140
- * Check if an entry has expired
141
- */
142
- private isExpired(entry: CacheEntry<unknown>): boolean {
143
- return Date.now() - entry.timestamp > this.ttl;
144
- }
145
-
146
- /**
147
- * Evict least recently used entry
148
- */
149
- private evictLRU(): void {
150
- let lruKey: string | null = null;
151
- let lruTime = Infinity;
152
-
153
- for (const [key, entry] of this.cache.entries()) {
154
- if (entry.lastAccess < lruTime) {
155
- lruTime = entry.lastAccess;
156
- lruKey = key;
157
- }
158
- }
159
-
160
- if (lruKey) {
161
- this.cache.delete(lruKey);
162
- }
163
- }
164
-
165
- /**
166
- * Generate a cache key from parameters
167
- */
168
- generateKey(params: Record<string, unknown>): string {
169
- // Sort keys to ensure consistent key generation
170
- const sortedParams = Object.keys(params)
171
- .sort()
172
- .reduce((acc, key) => {
173
- acc[key] = params[key];
174
- return acc;
175
- }, {} as Record<string, unknown>);
176
-
177
- return JSON.stringify(sortedParams);
178
- }
179
-
180
- /**
181
- * Preload cache with multiple entries
182
- */
183
- preload<T>(entries: Array<{ key: string; value: T }>): void {
184
- for (const entry of entries) {
185
- this.set(entry.key, entry.value);
186
- }
187
- }
188
-
189
- /**
190
- * Get or set pattern - return cached value or compute and cache it
191
- */
192
- async getOrSet<T>(
193
- key: string,
194
- computeFn: () => Promise<T>
195
- ): Promise<T> {
196
- const cached = this.get<T>(key);
197
- if (cached !== null) {
198
- return cached;
199
- }
200
-
201
- const value = await computeFn();
202
- this.set(key, value);
203
- return value;
204
- }
205
-
206
- /**
207
- * Reset cache to initial state
208
- */
209
- reset(): void {
210
- this.cache.clear();
211
- }
212
- }
213
-
214
- export const cacheManager = new CacheManager();
215
- export { CacheManager };
@@ -1,268 +0,0 @@
1
- /**
2
- * Debounce and Throttle Utility
3
- * @description Rate limiting utilities for user input and API calls
4
- */
5
-
6
- interface DebounceOptions {
7
- /** Delay in milliseconds */
8
- delay?: number;
9
- /** Execute on leading edge */
10
- leading?: boolean;
11
- /** Execute on trailing edge */
12
- trailing?: boolean;
13
- }
14
-
15
- interface ThrottleOptions {
16
- /** Interval in milliseconds */
17
- interval?: number;
18
- /** Execute on leading edge */
19
- leading?: boolean;
20
- /** Execute on trailing edge */
21
- trailing?: boolean;
22
- }
23
-
24
- class DebounceManager {
25
- private timers = new Map<string, NodeJS.Timeout>();
26
- private lastArgs = new Map<string, unknown[]>();
27
- private lastThis = new Map<string, unknown>();
28
-
29
- /**
30
- * Debounce a function
31
- * Delays execution until after delay milliseconds have elapsed since last invocation
32
- */
33
- debounce<T extends (...args: unknown[]) => unknown>(
34
- fn: T,
35
- options: DebounceOptions = {}
36
- ): T {
37
- const delay = options.delay ?? 300;
38
- const leading = options.leading ?? false;
39
- const trailing = options.trailing ?? true;
40
-
41
- let timer: NodeJS.Timeout | null = null;
42
- let lastCallTime = 0;
43
- let lastResult: unknown;
44
-
45
- return ((...args: unknown[]) => {
46
- const now = Date.now();
47
- const shouldCallLeading = leading && !timer && now - lastCallTime > delay;
48
-
49
- // Clear existing timer
50
- if (timer) {
51
- clearTimeout(timer);
52
- timer = null;
53
- }
54
-
55
- // Call on leading edge
56
- if (shouldCallLeading) {
57
- lastCallTime = now;
58
- lastResult = fn(...args);
59
- return lastResult;
60
- }
61
-
62
- // Schedule trailing edge call
63
- if (trailing) {
64
- timer = setTimeout(() => {
65
- timer = null;
66
- lastCallTime = Date.now();
67
- lastResult = fn(...args);
68
- }, delay);
69
- }
70
-
71
- return lastResult;
72
- }) as T;
73
- }
74
-
75
- /**
76
- * Debounce with key (for multiple independent debounced functions)
77
- */
78
- debounceByKey<T extends (...args: unknown[]) => unknown>(
79
- key: string,
80
- fn: T,
81
- delay: number = 300
82
- ): T {
83
- return ((...args: unknown[]) => {
84
- // Store args and context for trailing call
85
- this.lastArgs.set(key, args);
86
-
87
- // Clear existing timer
88
- const existingTimer = this.timers.get(key);
89
- if (existingTimer) {
90
- clearTimeout(existingTimer);
91
- }
92
-
93
- // Schedule new call
94
- const timer = setTimeout(() => {
95
- this.timers.delete(key);
96
- const args = this.lastArgs.get(key);
97
- this.lastArgs.delete(key);
98
- if (args) {
99
- fn(...args);
100
- }
101
- }, delay);
102
-
103
- this.timers.set(key, timer);
104
- }) as T;
105
- }
106
-
107
- /**
108
- * Cancel a pending debounced call
109
- */
110
- cancelDebounce(key: string): void {
111
- const timer = this.timers.get(key);
112
- if (timer) {
113
- clearTimeout(timer);
114
- this.timers.delete(key);
115
- }
116
- this.lastArgs.delete(key);
117
- }
118
-
119
- /**
120
- * Flush a pending debounced call immediately
121
- */
122
- flushDebounce<T extends (...args: unknown[]) => unknown>(
123
- key: string,
124
- fn: T
125
- ): void {
126
- this.cancelDebounce(key);
127
- const args = this.lastArgs.get(key);
128
- if (args) {
129
- fn(...args);
130
- this.lastArgs.delete(key);
131
- }
132
- }
133
- }
134
-
135
- class ThrottleManager {
136
- private timers = new Map<string, NodeJS.Timeout>();
137
- private lastArgs = new Map<string, unknown[]>();
138
-
139
- /**
140
- * Throttle a function
141
- * Limits execution to once per interval
142
- */
143
- throttle<T extends (...args: unknown[]) => unknown>(
144
- fn: T,
145
- options: ThrottleOptions = {}
146
- ): T {
147
- const interval = options.interval ?? 1000;
148
- const leading = options.leading ?? true;
149
- const trailing = options.trailing ?? false;
150
-
151
- let timer: NodeJS.Timeout | null = null;
152
- let lastCallTime = 0;
153
- let lastResult: unknown;
154
- let pendingArgs: unknown[] | null = null;
155
-
156
- return ((...args: unknown[]) => {
157
- const now = Date.now();
158
- const timeSinceLastCall = now - lastCallTime;
159
- const shouldCall = timeSinceLastCall >= interval;
160
-
161
- // Store args for potential trailing call
162
- pendingArgs = args;
163
-
164
- // Call on leading edge or if enough time has passed
165
- if (shouldCall && (leading || !timer)) {
166
- lastCallTime = now;
167
- lastResult = fn(...args);
168
- return lastResult;
169
- }
170
-
171
- // Schedule trailing edge call if needed
172
- if (trailing && !timer) {
173
- timer = setTimeout(() => {
174
- timer = null;
175
- lastCallTime = Date.now();
176
- if (pendingArgs) {
177
- lastResult = fn(...pendingArgs);
178
- pendingArgs = null;
179
- }
180
- }, interval - timeSinceLastCall);
181
- }
182
-
183
- return lastResult;
184
- }) as T;
185
- }
186
-
187
- /**
188
- * Throttle by key (for multiple independent throttled functions)
189
- */
190
- throttleByKey<T extends (...args: unknown[]) => unknown>(
191
- key: string,
192
- fn: T,
193
- interval: number = 1000
194
- ): T {
195
- return ((...args: unknown[]) => {
196
- // Check if we're within throttle interval
197
- const existingTimer = this.timers.get(key);
198
- if (existingTimer) {
199
- // Store args for potential trailing call
200
- this.lastArgs.set(key, args);
201
- return;
202
- }
203
-
204
- // Execute immediately
205
- fn(...args);
206
-
207
- // Set up throttle window
208
- const timer = setTimeout(() => {
209
- this.timers.delete(key);
210
-
211
- // Check if there are pending args to execute
212
- const pendingArgs = this.lastArgs.get(key);
213
- if (pendingArgs) {
214
- this.lastArgs.delete(key);
215
- fn(...pendingArgs);
216
-
217
- // Set up another throttle window
218
- const nextTimer = setTimeout(() => {
219
- this.timers.delete(key);
220
- }, interval);
221
-
222
- this.timers.set(key, nextTimer);
223
- }
224
- }, interval);
225
-
226
- this.timers.set(key, timer);
227
- }) as T;
228
- }
229
-
230
- /**
231
- * Cancel a throttled function
232
- */
233
- cancelThrottle(key: string): void {
234
- const timer = this.timers.get(key);
235
- if (timer) {
236
- clearTimeout(timer);
237
- this.timers.delete(key);
238
- }
239
- this.lastArgs.delete(key);
240
- }
241
-
242
- /**
243
- * Check if function is currently throttled
244
- */
245
- isThrottled(key: string): boolean {
246
- return this.timers.has(key);
247
- }
248
- }
249
-
250
- export const debounceManager = new DebounceManager();
251
- export const throttleManager = new ThrottleManager();
252
-
253
- /**
254
- * Convenience functions
255
- */
256
- export function debounce<T extends (...args: unknown[]) => unknown>(
257
- fn: T,
258
- delay?: number
259
- ): T {
260
- return debounceManager.debounce(fn, { delay });
261
- }
262
-
263
- export function throttle<T extends (...args: unknown[]) => unknown>(
264
- fn: T,
265
- interval?: number
266
- ): T {
267
- return throttleManager.throttle(fn, { interval });
268
- }
@@ -1,57 +0,0 @@
1
- /**
2
- * Error Utilities
3
- * @description Helper functions for error handling
4
- */
5
-
6
- // Error utilities - moved to error constants for better organization
7
-
8
- /**
9
- * Get user-friendly error message
10
- */
11
- export function getUserFriendlyError(error: Error): string {
12
- const errorMessage = error.message.toLowerCase();
13
-
14
- if (errorMessage.includes("api key") || errorMessage.includes("unauthorized")) {
15
- return "Please check your Groq API key and try again.";
16
- }
17
-
18
- if (errorMessage.includes("rate limit")) {
19
- return "You've reached the rate limit. Please wait a moment and try again.";
20
- }
21
-
22
- if (errorMessage.includes("network") || errorMessage.includes("fetch")) {
23
- return "Network error. Please check your connection and try again.";
24
- }
25
-
26
- if (errorMessage.includes("abort")) {
27
- return "Request was cancelled.";
28
- }
29
-
30
- return error.message || "An unexpected error occurred. Please try again.";
31
- }
32
-
33
- // isRetryableError and isAuthError are exported from ../constants/error.constants.ts
34
- // Re-export them here for backward compatibility
35
- export { isRetryableError, isAuthError } from "../constants/error.constants";
36
-
37
- /**
38
- * Format error for logging
39
- */
40
- export function formatErrorForLogging(error: unknown): {
41
- message: string;
42
- type: string;
43
- stack?: string;
44
- } {
45
- if (error instanceof Error) {
46
- return {
47
- message: error.message,
48
- type: error.name,
49
- stack: error.stack,
50
- };
51
- }
52
-
53
- return {
54
- message: String(error),
55
- type: typeof error,
56
- };
57
- }
@@ -1,18 +0,0 @@
1
- /**
2
- * Groq Error Class
3
- * @description Custom error for Groq API operations
4
- */
5
-
6
- import { GroqErrorType } from "../constants/error.constants";
7
-
8
- export class GroqError extends Error {
9
- constructor(
10
- public readonly type: GroqErrorType,
11
- message: string,
12
- public readonly cause?: Error,
13
- public readonly metadata?: { readonly status?: number; readonly url?: string }
14
- ) {
15
- super(message);
16
- this.name = "GroqError";
17
- }
18
- }
@@ -1,37 +0,0 @@
1
- /**
2
- * Infrastructure Utils Index
3
- * Subpath: @umituz/web-ai-groq-provider/utils
4
- */
5
-
6
- // Message utilities
7
- export {
8
- createUserMessage,
9
- createAssistantMessage,
10
- createSystemMessage,
11
- createTextMessage,
12
- promptToMessages,
13
- extractTextFromMessages,
14
- formatMessagesForDisplay,
15
- } from "./message.util";
16
-
17
- // Error utilities
18
- export {
19
- getUserFriendlyError,
20
- isRetryableError,
21
- isAuthError,
22
- formatErrorForLogging,
23
- } from "./error.util";
24
-
25
- export { GroqError } from "./groq-error.util";
26
-
27
- // Performance utilities
28
- export { requestDeduplicator, RequestDeduplicator } from "./request-deduplicator.util";
29
- export { requestQueue, RequestQueue } from "./request-queue.util";
30
- export { cacheManager, CacheManager } from "./cache-manager.util";
31
- export { retryManager, RetryManager, withRetry } from "./retry.util";
32
- export {
33
- debounceManager,
34
- throttleManager,
35
- debounce,
36
- throttle,
37
- } from "./debounce.util";
@@ -1,57 +0,0 @@
1
- /**
2
- * Message Utilities
3
- * @description Helper functions for creating and working with messages
4
- */
5
-
6
- import type { GroqMessage, GroqMessageRole } from "../entities";
7
-
8
- /**
9
- * Create a user message
10
- */
11
- export function createUserMessage(content: string): GroqMessage {
12
- return { role: "user", content };
13
- }
14
-
15
- /**
16
- * Create an assistant message
17
- */
18
- export function createAssistantMessage(content: string): GroqMessage {
19
- return { role: "assistant", content };
20
- }
21
-
22
- /**
23
- * Create a system message
24
- */
25
- export function createSystemMessage(content: string): GroqMessage {
26
- return { role: "system", content };
27
- }
28
-
29
- /**
30
- * Create a text message with specified role
31
- */
32
- export function createTextMessage(role: GroqMessageRole, content: string): GroqMessage {
33
- return { role, content };
34
- }
35
-
36
- /**
37
- * Convert a simple prompt to message array
38
- */
39
- export function promptToMessages(prompt: string): GroqMessage[] {
40
- return [{ role: "user", content: prompt }];
41
- }
42
-
43
- /**
44
- * Extract text content from messages
45
- */
46
- export function extractTextFromMessages(messages: GroqMessage[]): string {
47
- return messages.map(m => m.content).join("\n\n");
48
- }
49
-
50
- /**
51
- * Format messages for display
52
- */
53
- export function formatMessagesForDisplay(messages: GroqMessage[]): string {
54
- return messages
55
- .map(m => `[${m.role.toUpperCase()}]: ${m.content}`)
56
- .join("\n\n---\n\n");
57
- }