@umituz/web-ai-groq-provider 1.1.4 → 1.1.6

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-ai-groq-provider",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Groq AI text generation provider for React web applications",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -3,20 +3,20 @@
3
3
  * @description Main React hook for chat functionality
4
4
  */
5
5
 
6
- import { useState, useCallback, useEffect, useMemo } from "react";
6
+ import { useState, useCallback, useEffect } from "react";
7
7
  import type {
8
8
  ChatMessage,
9
9
  ChatConfig,
10
- IChatStorage,
11
10
  } from "../entities";
11
+ import type { IChatStorage } from "../entities";
12
12
  import type {
13
13
  UseChatOptions,
14
14
  UseChatReturn,
15
15
  } from "../interfaces";
16
16
  import { chatService } from "../services/chat.service";
17
17
 
18
- const isDevelopment =
19
- typeof process !== "undefined" && process.env?.NODE_ENV === "development";
18
+ const MESSAGE_ID_PREFIX = "msg-";
19
+ const MESSAGE_ID_USER_SUFFIX = "user";
20
20
 
21
21
  /**
22
22
  * Hook for chat functionality with AI integration
@@ -52,10 +52,6 @@ export function useChat(options: UseChatOptions): UseChatReturn {
52
52
  try {
53
53
  const loaded = await storage.getMessages(conversationId);
54
54
  setMessages(loaded);
55
-
56
- if (isDevelopment) {
57
- console.log("[useChat] Loaded messages:", loaded.length);
58
- }
59
55
  } catch (err) {
60
56
  console.error("[useChat] Failed to load messages:", err);
61
57
  }
@@ -71,10 +67,6 @@ export function useChat(options: UseChatOptions): UseChatReturn {
71
67
 
72
68
  try {
73
69
  await storage.saveMessage(conversationId, message);
74
-
75
- if (isDevelopment) {
76
- console.log("[useChat] Message saved to storage");
77
- }
78
70
  } catch (err) {
79
71
  console.error("[useChat] Failed to save message:", err);
80
72
  }
@@ -91,13 +83,9 @@ export function useChat(options: UseChatOptions): UseChatReturn {
91
83
  setError(null);
92
84
 
93
85
  try {
94
- if (isDevelopment) {
95
- console.log("[useChat] Sending message:", { text, type });
96
- }
97
-
98
86
  // Create user message first
99
87
  const userMessage: ChatMessage = {
100
- id: `msg-${Date.now()}-user`,
88
+ id: `${MESSAGE_ID_PREFIX}${Date.now()}-${MESSAGE_ID_USER_SUFFIX}`,
101
89
  sender: "user",
102
90
  content: text,
103
91
  timestamp: new Date().toISOString(),
@@ -125,19 +113,11 @@ export function useChat(options: UseChatOptions): UseChatReturn {
125
113
  // Save AI response to storage
126
114
  await saveToStorage(aiResponse);
127
115
  onAIResponse?.(aiResponse);
128
-
129
- if (isDevelopment) {
130
- console.log("[useChat] Message sent, AI response received");
131
- }
132
116
  } catch (err) {
133
117
  const errorMessage =
134
118
  err instanceof Error ? err.message : "Failed to send message";
135
119
  setError(errorMessage);
136
120
  onError?.(errorMessage);
137
-
138
- if (isDevelopment) {
139
- console.error("[useChat] Error sending message:", err);
140
- }
141
121
  } finally {
142
122
  setIsLoading(false);
143
123
  setIsTyping(false);
@@ -9,8 +9,9 @@ import type {
9
9
  SendMessageInput,
10
10
  SendMessageResult,
11
11
  ChatConfig,
12
+ IChatStorage,
12
13
  } from "../entities";
13
- import type { GroqMessage } from "../../groq/interfaces";
14
+ import type { GroqMessage } from "../../groq/entities";
14
15
 
15
16
  /**
16
17
  * Chat service interface
@@ -33,7 +34,7 @@ export interface UseChatOptions {
33
34
  /** Conversation identifier */
34
35
  readonly conversationId: string;
35
36
  /** Storage implementation */
36
- readonly storage?: import("../entities").IChatStorage;
37
+ readonly storage?: IChatStorage;
37
38
  /** Chat configuration */
38
39
  readonly config?: ChatConfig;
39
40
  /** Auto-save messages */
@@ -16,6 +16,7 @@ import type {
16
16
  } from "../entities";
17
17
  import { textGenerationService } from "../../groq/services";
18
18
  import { messageFormatter } from "../utils/message-formatter";
19
+ import { DEFAULT_MODELS } from "../../groq/constants";
19
20
 
20
21
  const DEFAULT_CONFIG: ChatConfig = {
21
22
  temperature: 0.8,
@@ -92,7 +93,7 @@ class ChatService implements IChatService {
92
93
  const response = await textGenerationService.generateChatCompletion(
93
94
  groqMessages,
94
95
  {
95
- model: "llama-3.3-70b-versatile",
96
+ model: DEFAULT_MODELS.TEXT,
96
97
  generationConfig: {
97
98
  temperature: this.config.temperature,
98
99
  maxTokens: this.config.maxTokens,
@@ -105,15 +106,13 @@ class ChatService implements IChatService {
105
106
  ...messageFormatter.toChatMessage(response, "assistant"),
106
107
  metadata: {
107
108
  companionId,
108
- model: "llama-3.3-70b-versatile",
109
+ model: DEFAULT_MODELS.TEXT,
109
110
  },
110
111
  };
111
112
 
112
113
  return aiMessage;
113
114
  } catch (error) {
114
- console.error("[ChatService] Error generating AI response:", error);
115
-
116
- // Fallback response
115
+ // Fallback response on error
117
116
  return messageFormatter.toChatMessage(
118
117
  "Şimdi biraz meşgulüm, ama seni duyuyorum! 💫",
119
118
  "assistant"
@@ -5,7 +5,10 @@
5
5
 
6
6
  import type { IMessageFormatter } from "../interfaces";
7
7
  import type { ChatMessage } from "../entities";
8
- import type { GroqMessage } from "../../groq/interfaces";
8
+ import type { GroqMessage } from "../../groq/entities";
9
+
10
+ const MESSAGE_ID_PREFIX = "msg-";
11
+ const MESSAGE_ID_USER_SUFFIX = "user";
9
12
 
10
13
  class MessageFormatter implements IMessageFormatter {
11
14
  toGroqMessages(messages: ChatMessage[], systemPrompt?: string): GroqMessage[] {
@@ -35,7 +38,7 @@ class MessageFormatter implements IMessageFormatter {
35
38
 
36
39
  toChatMessage(content: string, sender: ChatMessage["sender"]): ChatMessage {
37
40
  return {
38
- id: `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
41
+ id: `${MESSAGE_ID_PREFIX}${Date.now()}-${Math.random().toString(36).substring(2, 11)}`,
39
42
  sender,
40
43
  content: content.trim(),
41
44
  timestamp: new Date().toISOString(),
@@ -11,8 +11,6 @@ import { GroqError } from "../utils/groq-error.util";
11
11
  import { getUserFriendlyError } from "../utils/error.util";
12
12
  import { DEFAULT_MODELS } from "../constants";
13
13
 
14
- const isDevelopment = typeof process !== "undefined" && process.env?.NODE_ENV === "development";
15
-
16
14
  export interface UseGroqOptions {
17
15
  /** Initial model to use */
18
16
  readonly model?: string;
@@ -80,12 +78,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
80
78
  setError(null);
81
79
  setResult(null);
82
80
 
83
- if (isDevelopment) {
84
- console.log("[useGroq] generate called:", {
85
- promptLength: prompt.length,
86
- });
87
- }
88
-
89
81
  stableOptions.onStart?.();
90
82
 
91
83
  try {
@@ -100,12 +92,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
100
92
  setResult(response);
101
93
  stableOptions.onSuccess?.(response);
102
94
 
103
- if (isDevelopment) {
104
- console.log("[useGroq] generate success:", {
105
- responseLength: response.length,
106
- });
107
- }
108
-
109
95
  return response;
110
96
  } catch (err) {
111
97
  const errorMessage = getUserFriendlyError(
@@ -114,10 +100,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
114
100
  setError(errorMessage);
115
101
  stableOptions.onError?.(errorMessage);
116
102
 
117
- if (isDevelopment) {
118
- console.error("[useGroq] generate error:", { error: err });
119
- }
120
-
121
103
  throw err;
122
104
  } finally {
123
105
  setIsLoading(false);
@@ -135,12 +117,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
135
117
  setError(null);
136
118
  setResult(null);
137
119
 
138
- if (isDevelopment) {
139
- console.log("[useGroq] generateJSON called:", {
140
- promptLength: prompt.length,
141
- });
142
- }
143
-
144
120
  stableOptions.onStart?.();
145
121
 
146
122
  try {
@@ -157,12 +133,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
157
133
  setResult(jsonString);
158
134
  stableOptions.onSuccess?.(jsonString);
159
135
 
160
- if (isDevelopment) {
161
- console.log("[useGroq] generateJSON success:", {
162
- hasResponse: !!response,
163
- });
164
- }
165
-
166
136
  return response;
167
137
  } catch (err) {
168
138
  const errorMessage = getUserFriendlyError(
@@ -171,10 +141,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
171
141
  setError(errorMessage);
172
142
  stableOptions.onError?.(errorMessage);
173
143
 
174
- if (isDevelopment) {
175
- console.error("[useGroq] generateJSON error:", { error: err });
176
- }
177
-
178
144
  throw err;
179
145
  } finally {
180
146
  setIsLoading(false);
@@ -195,12 +161,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
195
161
 
196
162
  let fullContent = "";
197
163
 
198
- if (isDevelopment) {
199
- console.log("[useGroq] stream called:", {
200
- promptLength: prompt.length,
201
- });
202
- }
203
-
204
164
  stableOptions.onStart?.();
205
165
 
206
166
  try {
@@ -229,12 +189,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
229
189
  // Consume the async generator
230
190
  void _;
231
191
  }
232
-
233
- if (isDevelopment) {
234
- console.log("[useGroq] stream success:", {
235
- contentLength: fullContent.length,
236
- });
237
- }
238
192
  } catch (err) {
239
193
  const errorMessage = getUserFriendlyError(
240
194
  err instanceof Error ? err : new Error("Unknown error")
@@ -242,10 +196,6 @@ export function useGroq(options: UseGroqOptions = {}): UseGroqReturn {
242
196
  setError(errorMessage);
243
197
  stableOptions.onError?.(errorMessage);
244
198
 
245
- if (isDevelopment) {
246
- console.error("[useGroq] stream error:", { error: err });
247
- }
248
-
249
199
  throw err;
250
200
  } finally {
251
201
  setIsLoading(false);
@@ -9,8 +9,6 @@ import { GroqError } from "../utils/groq-error.util";
9
9
  import { GroqErrorType, mapHttpStatusToErrorType } from "../constants/error.constants";
10
10
  import { DEFAULT_BASE_URL, TIMEOUTS } from "../constants/groq.constants";
11
11
 
12
- const isDevelopment = typeof process !== "undefined" && process.env?.NODE_ENV === "development";
13
-
14
12
  class GroqHttpClientService implements IGroqHttpClient {
15
13
  private config: GroqConfig | null = null;
16
14
  private initialized = false;
@@ -18,17 +16,6 @@ class GroqHttpClientService implements IGroqHttpClient {
18
16
  initialize(config: GroqConfig): void {
19
17
  const apiKey = config.apiKey?.trim();
20
18
 
21
- if (isDevelopment) {
22
- console.log("[GroqClient] Initializing:", {
23
- hasApiKey: !!apiKey,
24
- keyLength: apiKey?.length,
25
- keyPrefix: apiKey ? `${apiKey.substring(0, 10)}...` : "",
26
- baseUrl: config.baseUrl || DEFAULT_BASE_URL,
27
- timeoutMs: config.timeoutMs || TIMEOUTS.DEFAULT,
28
- textModel: config.textModel,
29
- });
30
- }
31
-
32
19
  if (!apiKey || apiKey.length < 10) {
33
20
  throw new GroqError(
34
21
  GroqErrorType.INVALID_API_KEY,
@@ -43,13 +30,6 @@ class GroqHttpClientService implements IGroqHttpClient {
43
30
  textModel: config.textModel,
44
31
  };
45
32
  this.initialized = true;
46
-
47
- if (isDevelopment) {
48
- console.log("[GroqClient] Initialization complete:", {
49
- initialized: this.initialized,
50
- baseUrl: this.config.baseUrl,
51
- });
52
- }
53
33
  }
54
34
 
55
35
  isInitialized(): boolean {
@@ -136,16 +116,6 @@ class GroqHttpClientService implements IGroqHttpClient {
136
116
  const url = `${this.config.baseUrl}${endpoint}`;
137
117
  const timeout = this.config.timeoutMs || TIMEOUTS.DEFAULT;
138
118
 
139
- if (isDevelopment) {
140
- console.log("[GroqClient] API Request:", {
141
- url,
142
- endpoint,
143
- method: "POST",
144
- timeout: `${timeout}ms`,
145
- hasBody: !!body,
146
- });
147
- }
148
-
149
119
  try {
150
120
  const controller = new AbortController();
151
121
  const timeoutId = setTimeout(() => controller.abort(), timeout);
@@ -154,7 +124,6 @@ class GroqHttpClientService implements IGroqHttpClient {
154
124
  signal.addEventListener("abort", () => controller.abort());
155
125
  }
156
126
 
157
- const fetchStartTime = Date.now();
158
127
  const response = await fetch(url, {
159
128
  method: "POST",
160
129
  headers: {
@@ -165,17 +134,8 @@ class GroqHttpClientService implements IGroqHttpClient {
165
134
  signal: controller.signal,
166
135
  });
167
136
 
168
- const fetchDuration = Date.now() - fetchStartTime;
169
137
  clearTimeout(timeoutId);
170
138
 
171
- if (isDevelopment) {
172
- console.log("[GroqClient] API Response:", {
173
- status: response.status,
174
- ok: response.ok,
175
- fetchDuration: `${fetchDuration}ms`,
176
- });
177
- }
178
-
179
139
  if (!response.ok) {
180
140
  await this.handleErrorResponse(response);
181
141
  }
@@ -16,23 +16,13 @@ import { GroqError } from "../utils/groq-error.util";
16
16
  import { GroqErrorType } from "../constants/error.constants";
17
17
  import { DEFAULT_MODELS, API_ENDPOINTS, DEFAULT_GENERATION_CONFIG } from "../constants/groq.constants";
18
18
 
19
- const isDevelopment = typeof process !== "undefined" && process.env?.NODE_ENV === "development";
20
-
21
19
  class TextGenerationService implements IGroqChatService {
22
20
  async generateCompletion(
23
21
  prompt: string,
24
22
  options: TextGenerationOptions = {}
25
23
  ): Promise<string> {
26
- const startTime = Date.now();
27
24
  const model = options.model || DEFAULT_MODELS.TEXT;
28
25
 
29
- if (isDevelopment) {
30
- console.log("[Groq] generateCompletion called:", {
31
- model,
32
- promptLength: prompt.length,
33
- });
34
- }
35
-
36
26
  const messages: GroqMessage[] = [{ role: "user", content: prompt }];
37
27
  const request = this.buildRequest(model, messages, options.generationConfig);
38
28
 
@@ -46,13 +36,6 @@ class TextGenerationService implements IGroqChatService {
46
36
  );
47
37
  }
48
38
 
49
- if (isDevelopment) {
50
- console.log("[Groq] generateCompletion complete:", {
51
- duration: `${Date.now() - startTime}ms`,
52
- responseLength: content.length,
53
- });
54
- }
55
-
56
39
  return content;
57
40
  }
58
41
 
@@ -60,16 +43,8 @@ class TextGenerationService implements IGroqChatService {
60
43
  messages: GroqMessage[],
61
44
  options: TextGenerationOptions = {}
62
45
  ): Promise<string> {
63
- const startTime = Date.now();
64
46
  const model = options.model || DEFAULT_MODELS.TEXT;
65
47
 
66
- if (isDevelopment) {
67
- console.log("[Groq] generateChatCompletion called:", {
68
- model,
69
- messageCount: messages.length,
70
- });
71
- }
72
-
73
48
  const request = this.buildRequest(model, messages, options.generationConfig);
74
49
  const response = await groqHttpClient.postChatCompletion(request);
75
50
  const content = response.choices[0]?.message?.content;
@@ -81,13 +56,6 @@ class TextGenerationService implements IGroqChatService {
81
56
  );
82
57
  }
83
58
 
84
- if (isDevelopment) {
85
- console.log("[Groq] generateChatCompletion complete:", {
86
- duration: `${Date.now() - startTime}ms`,
87
- responseLength: content.length,
88
- });
89
- }
90
-
91
59
  return content;
92
60
  }
93
61
 
@@ -95,17 +63,8 @@ class TextGenerationService implements IGroqChatService {
95
63
  prompt: string,
96
64
  options: StructuredGenerationOptions<T> = {}
97
65
  ): Promise<T> {
98
- const startTime = Date.now();
99
66
  const model = options.model || DEFAULT_MODELS.TEXT;
100
67
 
101
- if (isDevelopment) {
102
- console.log("[Groq] generateStructured called:", {
103
- model,
104
- promptLength: prompt.length,
105
- hasSchema: !!options.schema,
106
- });
107
- }
108
-
109
68
  // Build JSON prompt
110
69
  const jsonPrompt = this.buildStructuredPrompt(prompt, options.schema);
111
70
  const messages: GroqMessage[] = [{ role: "user", content: jsonPrompt }];
@@ -131,12 +90,6 @@ class TextGenerationService implements IGroqChatService {
131
90
  const jsonStr = jsonMatch ? jsonMatch[1] || jsonMatch[0] : content;
132
91
  const parsed = JSON.parse(jsonStr) as T;
133
92
 
134
- if (isDevelopment) {
135
- console.log("[Groq] generateStructured complete:", {
136
- duration: `${Date.now() - startTime}ms`,
137
- });
138
- }
139
-
140
93
  return parsed;
141
94
  } catch (error) {
142
95
  throw new GroqError(
@@ -152,15 +105,10 @@ class TextGenerationService implements IGroqChatService {
152
105
  callbacks: StreamingCallbacks,
153
106
  options: TextGenerationOptions = {}
154
107
  ): AsyncGenerator<void, void, unknown> {
155
- const startTime = Date.now();
156
108
  const model = options.model || DEFAULT_MODELS.TEXT;
157
109
  const messages: GroqMessage[] = [{ role: "user", content: prompt }];
158
110
  const request = this.buildRequest(model, messages, options.generationConfig);
159
111
 
160
- if (isDevelopment) {
161
- console.log("[Groq] streamCompletion called:", { model });
162
- }
163
-
164
112
  try {
165
113
  const response = await groqHttpClient.postChatCompletion({
166
114
  ...request,
@@ -175,12 +123,6 @@ class TextGenerationService implements IGroqChatService {
175
123
  callbacks.onError?.(error as Error);
176
124
  throw error;
177
125
  }
178
-
179
- if (isDevelopment) {
180
- console.log("[Groq] streamCompletion complete:", {
181
- duration: `${Date.now() - startTime}ms`,
182
- });
183
- }
184
126
  }
185
127
 
186
128
  private buildRequest(
package/src/index.ts CHANGED
@@ -12,5 +12,5 @@
12
12
  */
13
13
 
14
14
  // Re-export domains for backward compatibility
15
- export * from "./domains/groq";
16
- export * from "./domains/chat";
15
+ export * from "./domains/groq/index";
16
+ export * from "./domains/chat/index";