@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.
- package/README.md +154 -72
- package/dist/domains/chat/index.cjs +539 -0
- package/dist/domains/chat/index.cjs.map +1 -0
- package/dist/domains/chat/index.d.cts +322 -0
- package/dist/domains/chat/index.d.ts +322 -0
- package/dist/domains/chat/index.js +531 -0
- package/dist/domains/chat/index.js.map +1 -0
- package/dist/domains/groq/index.cjs +1499 -0
- package/dist/domains/groq/index.cjs.map +1 -0
- package/dist/domains/groq/index.d.cts +561 -0
- package/dist/domains/groq/index.d.ts +561 -0
- package/dist/domains/groq/index.js +1453 -0
- package/dist/domains/groq/index.js.map +1 -0
- package/dist/index.cjs +1993 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +53 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +1937 -0
- package/dist/index.js.map +1 -0
- package/dist/text-generation.service-BP4Q_SH0.d.cts +252 -0
- package/dist/text-generation.service-BP4Q_SH0.d.ts +252 -0
- package/package.json +25 -11
- package/src/domains/chat/entities/chat.entities.ts +0 -113
- package/src/domains/chat/entities/index.ts +0 -14
- package/src/domains/chat/hooks/index.ts +0 -5
- package/src/domains/chat/hooks/use-chat.hook.ts +0 -259
- package/src/domains/chat/index.ts +0 -32
- package/src/domains/chat/interfaces/chat.interface.ts +0 -80
- package/src/domains/chat/interfaces/index.ts +0 -10
- package/src/domains/chat/services/chat.service.ts +0 -195
- package/src/domains/chat/services/index.ts +0 -5
- package/src/domains/chat/utils/message-formatter.ts +0 -49
- package/src/domains/groq/constants/error.constants.ts +0 -46
- package/src/domains/groq/constants/groq.constants.ts +0 -68
- package/src/domains/groq/constants/index.ts +0 -20
- package/src/domains/groq/entities/groq.entities.ts +0 -196
- package/src/domains/groq/entities/index.ts +0 -20
- package/src/domains/groq/hooks/index.ts +0 -7
- package/src/domains/groq/hooks/use-groq.hook.ts +0 -232
- package/src/domains/groq/index.ts +0 -72
- package/src/domains/groq/interfaces/groq.interface.ts +0 -77
- package/src/domains/groq/interfaces/index.ts +0 -22
- package/src/domains/groq/services/http-client.service.ts +0 -265
- package/src/domains/groq/services/index.ts +0 -9
- package/src/domains/groq/services/text-generation.service.ts +0 -238
- package/src/domains/groq/utils/cache-manager.util.ts +0 -215
- package/src/domains/groq/utils/debounce.util.ts +0 -268
- package/src/domains/groq/utils/error.util.ts +0 -57
- package/src/domains/groq/utils/groq-error.util.ts +0 -18
- package/src/domains/groq/utils/index.ts +0 -37
- package/src/domains/groq/utils/message.util.ts +0 -57
- package/src/domains/groq/utils/request-deduplicator.util.ts +0 -123
- package/src/domains/groq/utils/request-queue.util.ts +0 -178
- package/src/domains/groq/utils/retry.util.ts +0 -207
- package/src/index.ts +0 -15
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groq Domain Entities
|
|
3
|
+
* @description Core type definitions for the Groq AI provider.
|
|
4
|
+
* Readonly everywhere: consumers must not mutate in-flight.
|
|
5
|
+
*/
|
|
6
|
+
interface GroqConfig {
|
|
7
|
+
readonly apiKey: string;
|
|
8
|
+
readonly baseUrl?: string;
|
|
9
|
+
readonly timeoutMs?: number;
|
|
10
|
+
readonly textModel?: string;
|
|
11
|
+
}
|
|
12
|
+
interface GroqGenerationConfig {
|
|
13
|
+
readonly temperature?: number;
|
|
14
|
+
readonly maxTokens?: number;
|
|
15
|
+
readonly topP?: number;
|
|
16
|
+
readonly n?: number;
|
|
17
|
+
readonly stop?: string[];
|
|
18
|
+
readonly frequencyPenalty?: number;
|
|
19
|
+
readonly presencePenalty?: number;
|
|
20
|
+
}
|
|
21
|
+
type GroqMessageRole = "system" | "user" | "assistant";
|
|
22
|
+
interface GroqMessage {
|
|
23
|
+
readonly role: GroqMessageRole;
|
|
24
|
+
readonly content: string;
|
|
25
|
+
}
|
|
26
|
+
interface GroqChatRequest {
|
|
27
|
+
readonly model: string;
|
|
28
|
+
readonly messages: GroqMessage[];
|
|
29
|
+
readonly temperature?: number;
|
|
30
|
+
readonly max_tokens?: number;
|
|
31
|
+
readonly top_p?: number;
|
|
32
|
+
readonly n?: number;
|
|
33
|
+
readonly stop?: string[];
|
|
34
|
+
readonly frequency_penalty?: number;
|
|
35
|
+
readonly presence_penalty?: number;
|
|
36
|
+
readonly stream?: boolean;
|
|
37
|
+
}
|
|
38
|
+
type GroqFinishReason = "stop" | "length" | "content_filter";
|
|
39
|
+
interface GroqChoice {
|
|
40
|
+
readonly index: number;
|
|
41
|
+
readonly message: {
|
|
42
|
+
readonly role: "assistant";
|
|
43
|
+
readonly content: string;
|
|
44
|
+
};
|
|
45
|
+
readonly finish_reason: GroqFinishReason;
|
|
46
|
+
readonly logprobs: null | object;
|
|
47
|
+
}
|
|
48
|
+
interface GroqUsage {
|
|
49
|
+
readonly prompt_tokens: number;
|
|
50
|
+
readonly completion_tokens: number;
|
|
51
|
+
readonly total_tokens: number;
|
|
52
|
+
readonly prompt_time?: number;
|
|
53
|
+
readonly completion_time?: number;
|
|
54
|
+
readonly total_time?: number;
|
|
55
|
+
}
|
|
56
|
+
interface GroqChatResponse {
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly object: string;
|
|
59
|
+
readonly created: number;
|
|
60
|
+
readonly model: string;
|
|
61
|
+
readonly choices: GroqChoice[];
|
|
62
|
+
readonly usage: GroqUsage;
|
|
63
|
+
readonly system_fingerprint?: string;
|
|
64
|
+
readonly x_groq?: {
|
|
65
|
+
readonly id?: string;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
interface GroqChunkChoice {
|
|
69
|
+
readonly index: number;
|
|
70
|
+
readonly delta: {
|
|
71
|
+
readonly role?: "assistant";
|
|
72
|
+
readonly content?: string;
|
|
73
|
+
};
|
|
74
|
+
readonly finish_reason: GroqFinishReason | null;
|
|
75
|
+
readonly logprobs: null | object;
|
|
76
|
+
}
|
|
77
|
+
interface GroqChatChunk {
|
|
78
|
+
readonly id: string;
|
|
79
|
+
readonly object: string;
|
|
80
|
+
readonly created: number;
|
|
81
|
+
readonly model: string;
|
|
82
|
+
readonly choices: GroqChunkChoice[];
|
|
83
|
+
readonly system_fingerprint?: string;
|
|
84
|
+
readonly x_groq?: {
|
|
85
|
+
readonly id?: string;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
interface GroqErrorResponse {
|
|
89
|
+
readonly error: {
|
|
90
|
+
readonly message: string;
|
|
91
|
+
readonly type: string;
|
|
92
|
+
readonly code?: string;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Groq Service Interfaces
|
|
98
|
+
* @description Contracts for Groq AI services
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Options for a single text or chat generation call.
|
|
103
|
+
*/
|
|
104
|
+
interface TextGenerationOptions {
|
|
105
|
+
/** Override the default model for this call. */
|
|
106
|
+
readonly model?: string;
|
|
107
|
+
/** Sampling/limits configuration. */
|
|
108
|
+
readonly generationConfig?: GroqGenerationConfig;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Callbacks for streaming responses.
|
|
112
|
+
* - `onChunk` is called for each content fragment.
|
|
113
|
+
* - `onComplete` is called once the full text is available.
|
|
114
|
+
* - `onError` is called when the stream fails (the same error is also thrown).
|
|
115
|
+
*/
|
|
116
|
+
interface StreamingCallbacks {
|
|
117
|
+
readonly onChunk?: (chunk: string) => void;
|
|
118
|
+
readonly onComplete?: (fullText: string) => void;
|
|
119
|
+
readonly onError?: (error: Error) => void;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Options for structured (JSON) generation.
|
|
123
|
+
* Pass a JSON Schema in `schema` to constrain the model's output.
|
|
124
|
+
*/
|
|
125
|
+
interface StructuredGenerationOptions {
|
|
126
|
+
readonly model?: string;
|
|
127
|
+
readonly generationConfig?: GroqGenerationConfig;
|
|
128
|
+
readonly schema?: Record<string, unknown>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Contract for the high-level chat service.
|
|
132
|
+
*/
|
|
133
|
+
interface IGroqChatService {
|
|
134
|
+
/** Generate a completion from a single prompt string. */
|
|
135
|
+
generateCompletion(prompt: string, options?: TextGenerationOptions): Promise<string>;
|
|
136
|
+
/** Generate a completion from a message history. */
|
|
137
|
+
generateChatCompletion(messages: ReadonlyArray<GroqMessage>, options?: TextGenerationOptions): Promise<string>;
|
|
138
|
+
/** Generate and parse a JSON output matching the optional schema. */
|
|
139
|
+
generateStructured<T>(prompt: string, options?: StructuredGenerationOptions): Promise<T>;
|
|
140
|
+
/** Stream a completion from a single prompt. Returns the full text. */
|
|
141
|
+
streamCompletion(prompt: string, callbacks: StreamingCallbacks, options?: TextGenerationOptions): Promise<string>;
|
|
142
|
+
/** Stream a chat completion from a message history. Returns full text. */
|
|
143
|
+
streamChatCompletion(messages: ReadonlyArray<GroqMessage>, callbacks: StreamingCallbacks, options?: TextGenerationOptions): Promise<string>;
|
|
144
|
+
/** Clear the in-memory response cache. */
|
|
145
|
+
clearCache(): void;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Contract for the low-level HTTP client.
|
|
149
|
+
* The strategy pipeline (retry / queue / dedupe) is wired around
|
|
150
|
+
* `postChatCompletion`; the streaming endpoint intentionally bypasses it
|
|
151
|
+
* because responses cannot be deduplicated.
|
|
152
|
+
*/
|
|
153
|
+
interface IGroqHttpClient {
|
|
154
|
+
postChatCompletion(request: GroqChatRequest): Promise<GroqChatResponse>;
|
|
155
|
+
streamChatCompletion(request: GroqChatRequest): Promise<ReadableStream<Uint8Array>>;
|
|
156
|
+
getApiKey(): string;
|
|
157
|
+
setApiKey(apiKey: string): void;
|
|
158
|
+
initialize(config: Readonly<{
|
|
159
|
+
apiKey: string;
|
|
160
|
+
baseUrl?: string;
|
|
161
|
+
timeoutMs?: number;
|
|
162
|
+
textModel?: string;
|
|
163
|
+
}>): void;
|
|
164
|
+
isInitialized(): boolean;
|
|
165
|
+
cancelPendingRequests(): void;
|
|
166
|
+
reset(): void;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Cache Manager
|
|
171
|
+
* @description LRU cache for API responses with TTL. Delegates:
|
|
172
|
+
* - LRU key selection → calculations/lru-key-selector
|
|
173
|
+
* - Cache key construction → builders/cache-key-builder
|
|
174
|
+
* - Hit rate calculation → calculations/cache-hit-rate
|
|
175
|
+
*/
|
|
176
|
+
interface CacheOptions {
|
|
177
|
+
/** Maximum number of entries. Default 100. */
|
|
178
|
+
readonly maxSize?: number;
|
|
179
|
+
/** Time-to-live in milliseconds. Default 5 minutes. */
|
|
180
|
+
readonly ttl?: number;
|
|
181
|
+
}
|
|
182
|
+
declare class CacheManager {
|
|
183
|
+
private readonly cache;
|
|
184
|
+
private readonly maxSize;
|
|
185
|
+
private readonly ttl;
|
|
186
|
+
constructor(options?: CacheOptions);
|
|
187
|
+
get<T>(key: string): T | null;
|
|
188
|
+
set<T>(key: string, value: T): void;
|
|
189
|
+
has(key: string): boolean;
|
|
190
|
+
delete(key: string): boolean;
|
|
191
|
+
clear(): void;
|
|
192
|
+
/** Build a stable cache key from a parameter object. */
|
|
193
|
+
generateKey(params: Readonly<Record<string, unknown>>): string;
|
|
194
|
+
/**
|
|
195
|
+
* Dynamic cache statistics. All numbers are computed at call time
|
|
196
|
+
* from the current state; nothing is hardcoded.
|
|
197
|
+
*/
|
|
198
|
+
getStats(): CacheStats;
|
|
199
|
+
private isExpired;
|
|
200
|
+
}
|
|
201
|
+
interface CacheStats {
|
|
202
|
+
readonly size: number;
|
|
203
|
+
readonly maxSize: number;
|
|
204
|
+
readonly hitRate: number;
|
|
205
|
+
readonly entries: ReadonlyArray<{
|
|
206
|
+
readonly key: string;
|
|
207
|
+
readonly accessCount: number;
|
|
208
|
+
readonly ageMs: number;
|
|
209
|
+
}>;
|
|
210
|
+
}
|
|
211
|
+
declare const cacheManager: CacheManager;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Text Generation Service
|
|
215
|
+
* @description High-level text generation orchestrator. Coordinates:
|
|
216
|
+
* - cache lookup/store
|
|
217
|
+
* - request building
|
|
218
|
+
* - HTTP transport
|
|
219
|
+
* - response parsing (text / structured / streaming)
|
|
220
|
+
*
|
|
221
|
+
* No longer owns prompt construction (delegated to builder)
|
|
222
|
+
* or JSON parsing (delegated to JsonResponseParser).
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
declare class TextGenerationService implements IGroqChatService {
|
|
226
|
+
private readonly httpClient;
|
|
227
|
+
private readonly cache;
|
|
228
|
+
private readonly jsonParser;
|
|
229
|
+
constructor(httpClient: IGroqHttpClient, cache?: CacheManager);
|
|
230
|
+
generateCompletion(prompt: string, options?: TextGenerationOptions): Promise<string>;
|
|
231
|
+
generateChatCompletion(messages: ReadonlyArray<GroqMessage>, options?: TextGenerationOptions): Promise<string>;
|
|
232
|
+
generateStructured<T>(prompt: string, options?: StructuredGenerationOptions): Promise<T>;
|
|
233
|
+
streamCompletion(prompt: string, callbacks: StreamingCallbacks, options?: TextGenerationOptions): Promise<string>;
|
|
234
|
+
/**
|
|
235
|
+
* Stream a chat completion given an existing message history.
|
|
236
|
+
* Resolves with the accumulated full text once the stream completes.
|
|
237
|
+
*
|
|
238
|
+
* @param messages - The full conversation history.
|
|
239
|
+
* @param callbacks - Streaming callbacks (`onChunk`, `onComplete`, `onError`).
|
|
240
|
+
* @param options - Generation options.
|
|
241
|
+
* @returns The full accumulated text.
|
|
242
|
+
*/
|
|
243
|
+
streamChatCompletion(messages: ReadonlyArray<GroqMessage>, callbacks: StreamingCallbacks, options?: TextGenerationOptions): Promise<string>;
|
|
244
|
+
/**
|
|
245
|
+
* Clear the response cache (used by long-running services to free memory).
|
|
246
|
+
*/
|
|
247
|
+
clearCache(): void;
|
|
248
|
+
private executeWithCache;
|
|
249
|
+
private extractContent;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export { CacheManager as C, type GroqConfig as G, type IGroqChatService as I, type StreamingCallbacks as S, TextGenerationService as T, type CacheOptions as a, type CacheStats as b, type GroqChatChunk as c, type GroqChatRequest as d, type GroqChatResponse as e, type GroqChoice as f, type GroqChunkChoice as g, type GroqErrorResponse as h, type GroqFinishReason as i, type GroqGenerationConfig as j, type GroqMessage as k, type GroqMessageRole as l, type GroqUsage as m, type IGroqHttpClient as n, type StructuredGenerationOptions as o, type TextGenerationOptions as p, cacheManager as q };
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Groq Domain Entities
|
|
3
|
+
* @description Core type definitions for the Groq AI provider.
|
|
4
|
+
* Readonly everywhere: consumers must not mutate in-flight.
|
|
5
|
+
*/
|
|
6
|
+
interface GroqConfig {
|
|
7
|
+
readonly apiKey: string;
|
|
8
|
+
readonly baseUrl?: string;
|
|
9
|
+
readonly timeoutMs?: number;
|
|
10
|
+
readonly textModel?: string;
|
|
11
|
+
}
|
|
12
|
+
interface GroqGenerationConfig {
|
|
13
|
+
readonly temperature?: number;
|
|
14
|
+
readonly maxTokens?: number;
|
|
15
|
+
readonly topP?: number;
|
|
16
|
+
readonly n?: number;
|
|
17
|
+
readonly stop?: string[];
|
|
18
|
+
readonly frequencyPenalty?: number;
|
|
19
|
+
readonly presencePenalty?: number;
|
|
20
|
+
}
|
|
21
|
+
type GroqMessageRole = "system" | "user" | "assistant";
|
|
22
|
+
interface GroqMessage {
|
|
23
|
+
readonly role: GroqMessageRole;
|
|
24
|
+
readonly content: string;
|
|
25
|
+
}
|
|
26
|
+
interface GroqChatRequest {
|
|
27
|
+
readonly model: string;
|
|
28
|
+
readonly messages: GroqMessage[];
|
|
29
|
+
readonly temperature?: number;
|
|
30
|
+
readonly max_tokens?: number;
|
|
31
|
+
readonly top_p?: number;
|
|
32
|
+
readonly n?: number;
|
|
33
|
+
readonly stop?: string[];
|
|
34
|
+
readonly frequency_penalty?: number;
|
|
35
|
+
readonly presence_penalty?: number;
|
|
36
|
+
readonly stream?: boolean;
|
|
37
|
+
}
|
|
38
|
+
type GroqFinishReason = "stop" | "length" | "content_filter";
|
|
39
|
+
interface GroqChoice {
|
|
40
|
+
readonly index: number;
|
|
41
|
+
readonly message: {
|
|
42
|
+
readonly role: "assistant";
|
|
43
|
+
readonly content: string;
|
|
44
|
+
};
|
|
45
|
+
readonly finish_reason: GroqFinishReason;
|
|
46
|
+
readonly logprobs: null | object;
|
|
47
|
+
}
|
|
48
|
+
interface GroqUsage {
|
|
49
|
+
readonly prompt_tokens: number;
|
|
50
|
+
readonly completion_tokens: number;
|
|
51
|
+
readonly total_tokens: number;
|
|
52
|
+
readonly prompt_time?: number;
|
|
53
|
+
readonly completion_time?: number;
|
|
54
|
+
readonly total_time?: number;
|
|
55
|
+
}
|
|
56
|
+
interface GroqChatResponse {
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly object: string;
|
|
59
|
+
readonly created: number;
|
|
60
|
+
readonly model: string;
|
|
61
|
+
readonly choices: GroqChoice[];
|
|
62
|
+
readonly usage: GroqUsage;
|
|
63
|
+
readonly system_fingerprint?: string;
|
|
64
|
+
readonly x_groq?: {
|
|
65
|
+
readonly id?: string;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
interface GroqChunkChoice {
|
|
69
|
+
readonly index: number;
|
|
70
|
+
readonly delta: {
|
|
71
|
+
readonly role?: "assistant";
|
|
72
|
+
readonly content?: string;
|
|
73
|
+
};
|
|
74
|
+
readonly finish_reason: GroqFinishReason | null;
|
|
75
|
+
readonly logprobs: null | object;
|
|
76
|
+
}
|
|
77
|
+
interface GroqChatChunk {
|
|
78
|
+
readonly id: string;
|
|
79
|
+
readonly object: string;
|
|
80
|
+
readonly created: number;
|
|
81
|
+
readonly model: string;
|
|
82
|
+
readonly choices: GroqChunkChoice[];
|
|
83
|
+
readonly system_fingerprint?: string;
|
|
84
|
+
readonly x_groq?: {
|
|
85
|
+
readonly id?: string;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
interface GroqErrorResponse {
|
|
89
|
+
readonly error: {
|
|
90
|
+
readonly message: string;
|
|
91
|
+
readonly type: string;
|
|
92
|
+
readonly code?: string;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Groq Service Interfaces
|
|
98
|
+
* @description Contracts for Groq AI services
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Options for a single text or chat generation call.
|
|
103
|
+
*/
|
|
104
|
+
interface TextGenerationOptions {
|
|
105
|
+
/** Override the default model for this call. */
|
|
106
|
+
readonly model?: string;
|
|
107
|
+
/** Sampling/limits configuration. */
|
|
108
|
+
readonly generationConfig?: GroqGenerationConfig;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Callbacks for streaming responses.
|
|
112
|
+
* - `onChunk` is called for each content fragment.
|
|
113
|
+
* - `onComplete` is called once the full text is available.
|
|
114
|
+
* - `onError` is called when the stream fails (the same error is also thrown).
|
|
115
|
+
*/
|
|
116
|
+
interface StreamingCallbacks {
|
|
117
|
+
readonly onChunk?: (chunk: string) => void;
|
|
118
|
+
readonly onComplete?: (fullText: string) => void;
|
|
119
|
+
readonly onError?: (error: Error) => void;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Options for structured (JSON) generation.
|
|
123
|
+
* Pass a JSON Schema in `schema` to constrain the model's output.
|
|
124
|
+
*/
|
|
125
|
+
interface StructuredGenerationOptions {
|
|
126
|
+
readonly model?: string;
|
|
127
|
+
readonly generationConfig?: GroqGenerationConfig;
|
|
128
|
+
readonly schema?: Record<string, unknown>;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Contract for the high-level chat service.
|
|
132
|
+
*/
|
|
133
|
+
interface IGroqChatService {
|
|
134
|
+
/** Generate a completion from a single prompt string. */
|
|
135
|
+
generateCompletion(prompt: string, options?: TextGenerationOptions): Promise<string>;
|
|
136
|
+
/** Generate a completion from a message history. */
|
|
137
|
+
generateChatCompletion(messages: ReadonlyArray<GroqMessage>, options?: TextGenerationOptions): Promise<string>;
|
|
138
|
+
/** Generate and parse a JSON output matching the optional schema. */
|
|
139
|
+
generateStructured<T>(prompt: string, options?: StructuredGenerationOptions): Promise<T>;
|
|
140
|
+
/** Stream a completion from a single prompt. Returns the full text. */
|
|
141
|
+
streamCompletion(prompt: string, callbacks: StreamingCallbacks, options?: TextGenerationOptions): Promise<string>;
|
|
142
|
+
/** Stream a chat completion from a message history. Returns full text. */
|
|
143
|
+
streamChatCompletion(messages: ReadonlyArray<GroqMessage>, callbacks: StreamingCallbacks, options?: TextGenerationOptions): Promise<string>;
|
|
144
|
+
/** Clear the in-memory response cache. */
|
|
145
|
+
clearCache(): void;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Contract for the low-level HTTP client.
|
|
149
|
+
* The strategy pipeline (retry / queue / dedupe) is wired around
|
|
150
|
+
* `postChatCompletion`; the streaming endpoint intentionally bypasses it
|
|
151
|
+
* because responses cannot be deduplicated.
|
|
152
|
+
*/
|
|
153
|
+
interface IGroqHttpClient {
|
|
154
|
+
postChatCompletion(request: GroqChatRequest): Promise<GroqChatResponse>;
|
|
155
|
+
streamChatCompletion(request: GroqChatRequest): Promise<ReadableStream<Uint8Array>>;
|
|
156
|
+
getApiKey(): string;
|
|
157
|
+
setApiKey(apiKey: string): void;
|
|
158
|
+
initialize(config: Readonly<{
|
|
159
|
+
apiKey: string;
|
|
160
|
+
baseUrl?: string;
|
|
161
|
+
timeoutMs?: number;
|
|
162
|
+
textModel?: string;
|
|
163
|
+
}>): void;
|
|
164
|
+
isInitialized(): boolean;
|
|
165
|
+
cancelPendingRequests(): void;
|
|
166
|
+
reset(): void;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Cache Manager
|
|
171
|
+
* @description LRU cache for API responses with TTL. Delegates:
|
|
172
|
+
* - LRU key selection → calculations/lru-key-selector
|
|
173
|
+
* - Cache key construction → builders/cache-key-builder
|
|
174
|
+
* - Hit rate calculation → calculations/cache-hit-rate
|
|
175
|
+
*/
|
|
176
|
+
interface CacheOptions {
|
|
177
|
+
/** Maximum number of entries. Default 100. */
|
|
178
|
+
readonly maxSize?: number;
|
|
179
|
+
/** Time-to-live in milliseconds. Default 5 minutes. */
|
|
180
|
+
readonly ttl?: number;
|
|
181
|
+
}
|
|
182
|
+
declare class CacheManager {
|
|
183
|
+
private readonly cache;
|
|
184
|
+
private readonly maxSize;
|
|
185
|
+
private readonly ttl;
|
|
186
|
+
constructor(options?: CacheOptions);
|
|
187
|
+
get<T>(key: string): T | null;
|
|
188
|
+
set<T>(key: string, value: T): void;
|
|
189
|
+
has(key: string): boolean;
|
|
190
|
+
delete(key: string): boolean;
|
|
191
|
+
clear(): void;
|
|
192
|
+
/** Build a stable cache key from a parameter object. */
|
|
193
|
+
generateKey(params: Readonly<Record<string, unknown>>): string;
|
|
194
|
+
/**
|
|
195
|
+
* Dynamic cache statistics. All numbers are computed at call time
|
|
196
|
+
* from the current state; nothing is hardcoded.
|
|
197
|
+
*/
|
|
198
|
+
getStats(): CacheStats;
|
|
199
|
+
private isExpired;
|
|
200
|
+
}
|
|
201
|
+
interface CacheStats {
|
|
202
|
+
readonly size: number;
|
|
203
|
+
readonly maxSize: number;
|
|
204
|
+
readonly hitRate: number;
|
|
205
|
+
readonly entries: ReadonlyArray<{
|
|
206
|
+
readonly key: string;
|
|
207
|
+
readonly accessCount: number;
|
|
208
|
+
readonly ageMs: number;
|
|
209
|
+
}>;
|
|
210
|
+
}
|
|
211
|
+
declare const cacheManager: CacheManager;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Text Generation Service
|
|
215
|
+
* @description High-level text generation orchestrator. Coordinates:
|
|
216
|
+
* - cache lookup/store
|
|
217
|
+
* - request building
|
|
218
|
+
* - HTTP transport
|
|
219
|
+
* - response parsing (text / structured / streaming)
|
|
220
|
+
*
|
|
221
|
+
* No longer owns prompt construction (delegated to builder)
|
|
222
|
+
* or JSON parsing (delegated to JsonResponseParser).
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
declare class TextGenerationService implements IGroqChatService {
|
|
226
|
+
private readonly httpClient;
|
|
227
|
+
private readonly cache;
|
|
228
|
+
private readonly jsonParser;
|
|
229
|
+
constructor(httpClient: IGroqHttpClient, cache?: CacheManager);
|
|
230
|
+
generateCompletion(prompt: string, options?: TextGenerationOptions): Promise<string>;
|
|
231
|
+
generateChatCompletion(messages: ReadonlyArray<GroqMessage>, options?: TextGenerationOptions): Promise<string>;
|
|
232
|
+
generateStructured<T>(prompt: string, options?: StructuredGenerationOptions): Promise<T>;
|
|
233
|
+
streamCompletion(prompt: string, callbacks: StreamingCallbacks, options?: TextGenerationOptions): Promise<string>;
|
|
234
|
+
/**
|
|
235
|
+
* Stream a chat completion given an existing message history.
|
|
236
|
+
* Resolves with the accumulated full text once the stream completes.
|
|
237
|
+
*
|
|
238
|
+
* @param messages - The full conversation history.
|
|
239
|
+
* @param callbacks - Streaming callbacks (`onChunk`, `onComplete`, `onError`).
|
|
240
|
+
* @param options - Generation options.
|
|
241
|
+
* @returns The full accumulated text.
|
|
242
|
+
*/
|
|
243
|
+
streamChatCompletion(messages: ReadonlyArray<GroqMessage>, callbacks: StreamingCallbacks, options?: TextGenerationOptions): Promise<string>;
|
|
244
|
+
/**
|
|
245
|
+
* Clear the response cache (used by long-running services to free memory).
|
|
246
|
+
*/
|
|
247
|
+
clearCache(): void;
|
|
248
|
+
private executeWithCache;
|
|
249
|
+
private extractContent;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export { CacheManager as C, type GroqConfig as G, type IGroqChatService as I, type StreamingCallbacks as S, TextGenerationService as T, type CacheOptions as a, type CacheStats as b, type GroqChatChunk as c, type GroqChatRequest as d, type GroqChatResponse as e, type GroqChoice as f, type GroqChunkChoice as g, type GroqErrorResponse as h, type GroqFinishReason as i, type GroqGenerationConfig as j, type GroqMessage as k, type GroqMessageRole as l, type GroqUsage as m, type IGroqHttpClient as n, type StructuredGenerationOptions as o, type TextGenerationOptions as p, cacheManager as q };
|
package/package.json
CHANGED
|
@@ -1,22 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-ai-groq-provider",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Groq AI text generation provider for React web applications",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Groq AI text generation and chat provider for React web applications",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./
|
|
7
|
-
"
|
|
8
|
-
"
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
|
-
".":
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./groq": {
|
|
16
|
+
"types": "./dist/domains/groq/index.d.ts",
|
|
17
|
+
"import": "./dist/domains/groq/index.js",
|
|
18
|
+
"require": "./dist/domains/groq/index.cjs"
|
|
19
|
+
},
|
|
20
|
+
"./chat": {
|
|
21
|
+
"types": "./dist/domains/chat/index.d.ts",
|
|
22
|
+
"import": "./dist/domains/chat/index.js",
|
|
23
|
+
"require": "./dist/domains/chat/index.cjs"
|
|
24
|
+
},
|
|
13
25
|
"./package.json": "./package.json"
|
|
14
26
|
},
|
|
27
|
+
"sideEffects": false,
|
|
15
28
|
"scripts": {
|
|
16
29
|
"typecheck": "tsc --noEmit",
|
|
17
30
|
"lint": "eslint .",
|
|
18
|
-
"build": "tsup
|
|
19
|
-
"dev": "tsup
|
|
31
|
+
"build": "tsup",
|
|
32
|
+
"dev": "tsup --watch",
|
|
33
|
+
"test:scenarios": "echo 'See TEST_SCENARIOS.md for manual test scenarios'",
|
|
20
34
|
"version:patch": "npm version patch -m 'chore: release v%s'",
|
|
21
35
|
"version:minor": "npm version minor -m 'chore: release v%s'",
|
|
22
36
|
"version:major": "npm version major -m 'chore: release v%s'"
|
|
@@ -54,7 +68,7 @@
|
|
|
54
68
|
"access": "public"
|
|
55
69
|
},
|
|
56
70
|
"files": [
|
|
57
|
-
"
|
|
71
|
+
"dist",
|
|
58
72
|
"README.md",
|
|
59
73
|
"LICENSE"
|
|
60
74
|
]
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Chat Domain Entities
|
|
3
|
-
* @description Core chat message and conversation types
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Message sender type
|
|
8
|
-
*/
|
|
9
|
-
export type ChatMessageSender = "user" | "assistant" | "system";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Message content types
|
|
13
|
-
*/
|
|
14
|
-
export type ChatMessageType = "text" | "image" | "audio" | "sticker";
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Chat message structure
|
|
18
|
-
*/
|
|
19
|
-
export interface ChatMessage {
|
|
20
|
-
/** Unique message identifier */
|
|
21
|
-
readonly id: string;
|
|
22
|
-
/** Sender of the message */
|
|
23
|
-
readonly sender: ChatMessageSender;
|
|
24
|
-
/** Message content */
|
|
25
|
-
readonly content: string;
|
|
26
|
-
/** Timestamp in ISO format */
|
|
27
|
-
readonly timestamp: string;
|
|
28
|
-
/** Message type */
|
|
29
|
-
readonly type?: ChatMessageType;
|
|
30
|
-
/** Image URL for image messages */
|
|
31
|
-
readonly imageUrl?: string;
|
|
32
|
-
/** Reaction emoji */
|
|
33
|
-
readonly reaction?: string;
|
|
34
|
-
/** Metadata */
|
|
35
|
-
readonly metadata?: Record<string, unknown>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Conversation/session metadata
|
|
40
|
-
*/
|
|
41
|
-
export interface ChatConversation {
|
|
42
|
-
/** Unique conversation identifier */
|
|
43
|
-
readonly id: string;
|
|
44
|
-
/** Companion/character identifier */
|
|
45
|
-
readonly companionId: string;
|
|
46
|
-
/** Companion name */
|
|
47
|
-
readonly companionName: string;
|
|
48
|
-
/** Last message */
|
|
49
|
-
readonly lastMessage?: ChatMessage;
|
|
50
|
-
/** Message count */
|
|
51
|
-
readonly messageCount: number;
|
|
52
|
-
/** Updated timestamp */
|
|
53
|
-
readonly updatedAt: string;
|
|
54
|
-
/** Is pinned */
|
|
55
|
-
readonly pinned?: boolean;
|
|
56
|
-
/** Is archived */
|
|
57
|
-
readonly archived?: boolean;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Send message input
|
|
62
|
-
*/
|
|
63
|
-
export interface SendMessageInput {
|
|
64
|
-
/** Companion identifier */
|
|
65
|
-
readonly companionId: string;
|
|
66
|
-
/** Message text */
|
|
67
|
-
readonly text: string;
|
|
68
|
-
/** Message type */
|
|
69
|
-
readonly type?: ChatMessageType;
|
|
70
|
-
/** Image URL */
|
|
71
|
-
readonly imageUrl?: string;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Send message result
|
|
76
|
-
*/
|
|
77
|
-
export interface SendMessageResult {
|
|
78
|
-
/** User message */
|
|
79
|
-
readonly userMessage: ChatMessage;
|
|
80
|
-
/** AI response (if generated) */
|
|
81
|
-
readonly aiResponse?: ChatMessage;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Chat configuration
|
|
86
|
-
*/
|
|
87
|
-
export interface ChatConfig {
|
|
88
|
-
/** System prompt for AI personality */
|
|
89
|
-
readonly systemPrompt?: string;
|
|
90
|
-
/** Temperature for randomness */
|
|
91
|
-
readonly temperature?: number;
|
|
92
|
-
/** Maximum tokens */
|
|
93
|
-
readonly maxTokens?: number;
|
|
94
|
-
/** Include timestamp in messages */
|
|
95
|
-
readonly includeTimestamp?: boolean;
|
|
96
|
-
/** Language code (e.g., 'tr', 'en') */
|
|
97
|
-
readonly language?: string;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Storage interface for persisting messages
|
|
102
|
-
* Implementations should be provided by the application
|
|
103
|
-
*/
|
|
104
|
-
export interface IChatStorage {
|
|
105
|
-
/** Save message to storage */
|
|
106
|
-
saveMessage(conversationId: string, message: ChatMessage): Promise<void>;
|
|
107
|
-
/** Get messages for conversation */
|
|
108
|
-
getMessages(conversationId: string): Promise<ChatMessage[]>;
|
|
109
|
-
/** Update message */
|
|
110
|
-
updateMessage(messageId: string, updates: Partial<ChatMessage>): Promise<void>;
|
|
111
|
-
/** Delete message */
|
|
112
|
-
deleteMessage(messageId: string): Promise<void>;
|
|
113
|
-
}
|