@umituz/web-ai-groq-provider 1.1.10 → 1.1.12
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/package.json +2 -1
- package/src/domains/chat/hooks/use-chat.hook.ts +10 -12
- package/src/domains/chat/services/chat.service.ts +1 -1
- package/src/domains/chat/utils/message-formatter.ts +0 -1
- package/src/domains/groq/hooks/use-groq.hook.ts +3 -8
- package/src/domains/groq/interfaces/groq.interface.ts +1 -1
- package/src/domains/groq/services/text-generation.service.ts +3 -3
- package/src/domains/groq/utils/error.util.ts +1 -2
- package/src/domains/groq/utils/retry.util.ts +3 -3
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-ai-groq-provider",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.12",
|
|
4
4
|
"description": "Groq AI text generation provider for React web applications",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "./src/index.ts",
|
|
6
7
|
"types": "./src/index.ts",
|
|
7
8
|
"sideEffects": false,
|
|
@@ -8,7 +8,6 @@ import type {
|
|
|
8
8
|
ChatMessage,
|
|
9
9
|
ChatConfig,
|
|
10
10
|
} from "../entities";
|
|
11
|
-
import type { IChatStorage } from "../entities";
|
|
12
11
|
import type {
|
|
13
12
|
UseChatOptions,
|
|
14
13
|
UseChatReturn,
|
|
@@ -16,7 +15,6 @@ import type {
|
|
|
16
15
|
import { chatService } from "../services/chat.service";
|
|
17
16
|
|
|
18
17
|
const MESSAGE_ID_PREFIX = "msg-";
|
|
19
|
-
const MESSAGE_ID_USER_SUFFIX = "user";
|
|
20
18
|
|
|
21
19
|
/**
|
|
22
20
|
* Hook for chat functionality with AI integration and performance optimizations
|
|
@@ -27,9 +25,9 @@ export function useChat(options: UseChatOptions): UseChatReturn {
|
|
|
27
25
|
storage,
|
|
28
26
|
config,
|
|
29
27
|
autoSave = true,
|
|
30
|
-
onMessageSent,
|
|
31
|
-
onAIResponse,
|
|
32
|
-
onError,
|
|
28
|
+
onMessageSent: _onMessageSent,
|
|
29
|
+
onAIResponse: _onAIResponse,
|
|
30
|
+
onError: _onError,
|
|
33
31
|
} = options;
|
|
34
32
|
|
|
35
33
|
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
|
@@ -64,7 +62,7 @@ export function useChat(options: UseChatOptions): UseChatReturn {
|
|
|
64
62
|
try {
|
|
65
63
|
const loaded = await storage.getMessages(conversationId);
|
|
66
64
|
setMessages(loaded);
|
|
67
|
-
} catch (
|
|
65
|
+
} catch (_err) {
|
|
68
66
|
// Silently fail - errors are handled through onError callback if needed
|
|
69
67
|
}
|
|
70
68
|
};
|
|
@@ -79,29 +77,29 @@ export function useChat(options: UseChatOptions): UseChatReturn {
|
|
|
79
77
|
|
|
80
78
|
try {
|
|
81
79
|
await storage.saveMessage(conversationId, message);
|
|
82
|
-
} catch (
|
|
80
|
+
} catch (_err) {
|
|
83
81
|
// Silently fail - errors are handled through onError callback if needed
|
|
84
82
|
}
|
|
85
83
|
},
|
|
86
84
|
[conversationId, storage, autoSave]
|
|
87
85
|
);
|
|
88
86
|
|
|
89
|
-
// Batch save messages to storage
|
|
90
|
-
const
|
|
87
|
+
// Batch save messages to storage (reserved for future use)
|
|
88
|
+
const _saveMessagesToStorage = useCallback(
|
|
91
89
|
async (messagesToSave: ChatMessage[]) => {
|
|
92
90
|
if (!autoSave || !storage || messagesToSave.length === 0) return;
|
|
93
91
|
|
|
94
92
|
try {
|
|
95
93
|
// Try batch save if supported
|
|
96
94
|
if ('saveMessages' in storage) {
|
|
97
|
-
await (storage as
|
|
95
|
+
await (storage as unknown as { saveMessages: (id: string, msgs: ChatMessage[]) => Promise<void> }).saveMessages(conversationId, messagesToSave);
|
|
98
96
|
} else {
|
|
99
97
|
// Fallback to individual saves in parallel
|
|
100
98
|
await Promise.all(
|
|
101
99
|
messagesToSave.map(msg => storage.saveMessage(conversationId, msg))
|
|
102
100
|
);
|
|
103
101
|
}
|
|
104
|
-
} catch (
|
|
102
|
+
} catch (_err) {
|
|
105
103
|
// Silently fail
|
|
106
104
|
}
|
|
107
105
|
},
|
|
@@ -128,7 +126,7 @@ export function useChat(options: UseChatOptions): UseChatReturn {
|
|
|
128
126
|
try {
|
|
129
127
|
// Create user message
|
|
130
128
|
const userMessage: ChatMessage = {
|
|
131
|
-
id: `${MESSAGE_ID_PREFIX}${Date.now()}
|
|
129
|
+
id: `${MESSAGE_ID_PREFIX}${Date.now()}-user`,
|
|
132
130
|
sender: "user",
|
|
133
131
|
content: text,
|
|
134
132
|
timestamp: new Date().toISOString(),
|
|
@@ -8,7 +8,6 @@ import type { ChatMessage } from "../entities";
|
|
|
8
8
|
import type { GroqMessage } from "../../groq/entities";
|
|
9
9
|
|
|
10
10
|
const MESSAGE_ID_PREFIX = "msg-";
|
|
11
|
-
const MESSAGE_ID_USER_SUFFIX = "user";
|
|
12
11
|
|
|
13
12
|
class MessageFormatter implements IMessageFormatter {
|
|
14
13
|
toGroqMessages(messages: ChatMessage[], systemPrompt?: string): GroqMessage[] {
|
|
@@ -5,11 +5,9 @@
|
|
|
5
5
|
|
|
6
6
|
import { useState, useCallback, useMemo, useRef, useEffect } from "react";
|
|
7
7
|
import type { GroqGenerationConfig } from "../interfaces";
|
|
8
|
-
import type {
|
|
8
|
+
import type { StreamingCallbacks } from "../interfaces";
|
|
9
9
|
import { textGenerationService } from "../services";
|
|
10
|
-
import { GroqError } from "../utils/groq-error.util";
|
|
11
10
|
import { getUserFriendlyError } from "../utils/error.util";
|
|
12
|
-
import { DEFAULT_MODELS } from "../constants";
|
|
13
11
|
import { groqHttpClient } from "../services/http-client.service";
|
|
14
12
|
|
|
15
13
|
export interface UseGroqOptions {
|
|
@@ -185,7 +183,7 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
|
|
|
185
183
|
},
|
|
186
184
|
};
|
|
187
185
|
|
|
188
|
-
|
|
186
|
+
await textGenerationService.streamCompletion(
|
|
189
187
|
prompt,
|
|
190
188
|
callbacks,
|
|
191
189
|
{
|
|
@@ -195,10 +193,7 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
|
|
|
195
193
|
...config,
|
|
196
194
|
},
|
|
197
195
|
}
|
|
198
|
-
)
|
|
199
|
-
// Consume the async generator
|
|
200
|
-
void _;
|
|
201
|
-
}
|
|
196
|
+
);
|
|
202
197
|
} catch (err) {
|
|
203
198
|
const errorMessage = getUserFriendlyError(
|
|
204
199
|
err instanceof Error ? err : new Error("Unknown error")
|
|
@@ -14,7 +14,7 @@ import type {
|
|
|
14
14
|
import { groqHttpClient } from "./http-client.service";
|
|
15
15
|
import { GroqError } from "../utils/groq-error.util";
|
|
16
16
|
import { GroqErrorType } from "../constants/error.constants";
|
|
17
|
-
import { DEFAULT_MODELS,
|
|
17
|
+
import { DEFAULT_MODELS, DEFAULT_GENERATION_CONFIG } from "../constants/groq.constants";
|
|
18
18
|
import { cacheManager } from "../utils/cache-manager.util";
|
|
19
19
|
|
|
20
20
|
class TextGenerationService implements IGroqChatService {
|
|
@@ -134,11 +134,11 @@ class TextGenerationService implements IGroqChatService {
|
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
async
|
|
137
|
+
async streamCompletion(
|
|
138
138
|
prompt: string,
|
|
139
139
|
callbacks: StreamingCallbacks,
|
|
140
140
|
options: TextGenerationOptions = {}
|
|
141
|
-
):
|
|
141
|
+
): Promise<void> {
|
|
142
142
|
const model = options.model || DEFAULT_MODELS.TEXT;
|
|
143
143
|
const messages: GroqMessage[] = [{ role: "user", content: prompt }];
|
|
144
144
|
const request = this.buildRequest(model, messages, options.generationConfig);
|
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
* @description Helper functions for error handling
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
import { GroqErrorType } from "../constants/error.constants";
|
|
6
|
+
// Error utilities - moved to error constants for better organization
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Get user-friendly error message
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { GroqError } from "./groq-error.util";
|
|
7
|
-
import {
|
|
7
|
+
import { isRetryableError } from "../constants/error.constants";
|
|
8
8
|
|
|
9
9
|
interface RetryOptions {
|
|
10
10
|
/** Maximum number of retry attempts */
|
|
@@ -53,7 +53,7 @@ class RetryManager {
|
|
|
53
53
|
): Promise<T> {
|
|
54
54
|
const maxAttempts = options.maxAttempts ?? this.defaultMaxAttempts;
|
|
55
55
|
let lastError: Error | null = null;
|
|
56
|
-
let
|
|
56
|
+
let _totalDelay = 0;
|
|
57
57
|
|
|
58
58
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
59
59
|
try {
|
|
@@ -69,7 +69,7 @@ class RetryManager {
|
|
|
69
69
|
|
|
70
70
|
// Calculate delay for next attempt
|
|
71
71
|
const delay = this.calculateDelay(attempt, options);
|
|
72
|
-
|
|
72
|
+
_totalDelay += delay;
|
|
73
73
|
|
|
74
74
|
// Wait before retrying
|
|
75
75
|
await this.delay(delay);
|