@umituz/web-ai-groq-provider 1.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/src/config.ts ADDED
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Configuration Builders
3
+ * Builder pattern for Groq configuration
4
+ */
5
+
6
+ import type { GroqGenerationConfig } from "./types";
7
+
8
+ /**
9
+ * Provider configuration options
10
+ */
11
+ export interface ProviderConfig {
12
+ apiKey: string;
13
+ baseUrl?: string;
14
+ timeoutMs?: number;
15
+ defaultModel?: string;
16
+ }
17
+
18
+ /**
19
+ * Config Builder for Provider Configuration
20
+ */
21
+ export class ConfigBuilder {
22
+ private config: Partial<ProviderConfig> = {};
23
+
24
+ /**
25
+ * Create a new config builder
26
+ */
27
+ static create(): ConfigBuilder {
28
+ return new ConfigBuilder();
29
+ }
30
+
31
+ /**
32
+ * Set API key
33
+ */
34
+ withApiKey(apiKey: string): ConfigBuilder {
35
+ this.config.apiKey = apiKey;
36
+ return this;
37
+ }
38
+
39
+ /**
40
+ * Set base URL
41
+ */
42
+ withBaseUrl(baseUrl: string): ConfigBuilder {
43
+ this.config.baseUrl = baseUrl;
44
+ return this;
45
+ }
46
+
47
+ /**
48
+ * Set timeout
49
+ */
50
+ withTimeout(timeoutMs: number): ConfigBuilder {
51
+ this.config.timeoutMs = timeoutMs;
52
+ return this;
53
+ }
54
+
55
+ /**
56
+ * Set default model
57
+ */
58
+ withDefaultModel(model: string): ConfigBuilder {
59
+ this.config.defaultModel = model;
60
+ return this;
61
+ }
62
+
63
+ /**
64
+ * Build the configuration
65
+ */
66
+ build(): ProviderConfig {
67
+ if (!this.config.apiKey) {
68
+ throw new Error("API key is required");
69
+ }
70
+ return this.config as ProviderConfig;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Generation Config Builder
76
+ */
77
+ export class GenerationConfigBuilder {
78
+ private config: Partial<GroqGenerationConfig> = {};
79
+
80
+ /**
81
+ * Create a new generation config builder
82
+ */
83
+ static create(): GenerationConfigBuilder {
84
+ return new GenerationConfigBuilder();
85
+ }
86
+
87
+ /**
88
+ * Set temperature (0.0 - 2.0)
89
+ */
90
+ withTemperature(temperature: number): GenerationConfigBuilder {
91
+ this.config.temperature = Math.max(0, Math.min(2, temperature));
92
+ return this;
93
+ }
94
+
95
+ /**
96
+ * Set max tokens
97
+ */
98
+ withMaxTokens(maxTokens: number): GenerationConfigBuilder {
99
+ this.config.maxTokens = Math.max(1, maxTokens);
100
+ return this;
101
+ }
102
+
103
+ /**
104
+ * Set top P (0.0 - 1.0)
105
+ */
106
+ withTopP(topP: number): GenerationConfigBuilder {
107
+ this.config.topP = Math.max(0, Math.min(1, topP));
108
+ return this;
109
+ }
110
+
111
+ /**
112
+ * Set number of completions
113
+ */
114
+ withN(n: number): GenerationConfigBuilder {
115
+ this.config.n = Math.max(1, n);
116
+ return this;
117
+ }
118
+
119
+ /**
120
+ * Set stop sequences
121
+ */
122
+ withStop(stop: string[]): GenerationConfigBuilder {
123
+ this.config.stop = stop;
124
+ return this;
125
+ }
126
+
127
+ /**
128
+ * Set frequency penalty (-2.0 to 2.0)
129
+ */
130
+ withFrequencyPenalty(penalty: number): GenerationConfigBuilder {
131
+ this.config.frequencyPenalty = Math.max(-2, Math.min(2, penalty));
132
+ return this;
133
+ }
134
+
135
+ /**
136
+ * Set presence penalty (-2.0 to 2.0)
137
+ */
138
+ withPresencePenalty(penalty: number): GenerationConfigBuilder {
139
+ this.config.presencePenalty = Math.max(-2, Math.min(2, penalty));
140
+ return this;
141
+ }
142
+
143
+ /**
144
+ * Build the configuration
145
+ */
146
+ build(): GroqGenerationConfig {
147
+ return this.config as GroqGenerationConfig;
148
+ }
149
+ }
@@ -0,0 +1,196 @@
1
+ /**
2
+ * Groq Domain Entities
3
+ * @description Core type definitions for Groq AI provider
4
+ */
5
+
6
+ /**
7
+ * Configuration for Groq client initialization
8
+ */
9
+ export interface GroqConfig {
10
+ /** API key for authentication */
11
+ readonly apiKey: string;
12
+ /** Base URL for API requests (default: https://api.groq.com/openai/v1) */
13
+ readonly baseUrl?: string;
14
+ /** Default timeout in milliseconds */
15
+ readonly timeoutMs?: number;
16
+ /** Default model to use for text generation */
17
+ readonly textModel?: string;
18
+ }
19
+
20
+ /**
21
+ * Generation configuration for AI requests
22
+ */
23
+ export interface GroqGenerationConfig {
24
+ /** Controls randomness (0.0 - 2.0, default: 0.7) */
25
+ readonly temperature?: number;
26
+ /** Maximum number of tokens to generate */
27
+ readonly maxTokens?: number;
28
+ /** Nucleus sampling threshold (0.0 - 1.0) */
29
+ readonly topP?: number;
30
+ /** Number of completions to generate */
31
+ readonly n?: number;
32
+ /** Stop sequences */
33
+ readonly stop?: string[];
34
+ /** Frequency penalty (-2.0 to 2.0) */
35
+ readonly frequencyPenalty?: number;
36
+ /** Presence penalty (-2.0 to 2.0) */
37
+ readonly presencePenalty?: number;
38
+ }
39
+
40
+ /**
41
+ * Message role in chat conversation
42
+ */
43
+ export type GroqMessageRole = "system" | "user" | "assistant";
44
+
45
+ /**
46
+ * Chat message structure
47
+ */
48
+ export interface GroqMessage {
49
+ /** Role of the message sender */
50
+ readonly role: GroqMessageRole;
51
+ /** Content of the message */
52
+ readonly content: string;
53
+ }
54
+
55
+ /**
56
+ * Chat completion request
57
+ */
58
+ export interface GroqChatRequest {
59
+ /** Model to use for generation */
60
+ readonly model: string;
61
+ /** Array of messages in the conversation */
62
+ readonly messages: GroqMessage[];
63
+ /** Generation configuration */
64
+ readonly temperature?: number;
65
+ readonly max_tokens?: number;
66
+ readonly top_p?: number;
67
+ readonly n?: number;
68
+ readonly stop?: string[];
69
+ readonly frequency_penalty?: number;
70
+ readonly presence_penalty?: number;
71
+ /** Enable streaming response */
72
+ readonly stream?: boolean;
73
+ }
74
+
75
+ /**
76
+ * Individual completion choice
77
+ */
78
+ export interface GroqChoice {
79
+ /** Index of the choice */
80
+ readonly index: number;
81
+ /** Generated message */
82
+ readonly message: {
83
+ readonly role: "assistant";
84
+ readonly content: string;
85
+ };
86
+ /** Reason for finish (stop, length, etc.) */
87
+ readonly finish_reason: GroqFinishReason;
88
+ /** Logprobs (optional) */
89
+ readonly logprobs: null | object;
90
+ }
91
+
92
+ /**
93
+ * Finish reason types
94
+ */
95
+ export type GroqFinishReason = "stop" | "length" | "content_filter";
96
+
97
+ /**
98
+ * Token usage information
99
+ */
100
+ export interface GroqUsage {
101
+ /** Number of tokens in the prompt */
102
+ readonly prompt_tokens: number;
103
+ /** Number of tokens in the completion */
104
+ readonly completion_tokens: number;
105
+ /** Total number of tokens used */
106
+ readonly total_tokens: number;
107
+ /** Prompt time (Groq specific) */
108
+ readonly prompt_time?: number;
109
+ /** Completion time (Groq specific) */
110
+ readonly completion_time?: number;
111
+ /** Total time (Groq specific) */
112
+ readonly total_time?: number;
113
+ }
114
+
115
+ /**
116
+ * Chat completion response
117
+ */
118
+ export interface GroqChatResponse {
119
+ /** Unique identifier for the response */
120
+ readonly id: string;
121
+ /** Object type (chat.completion) */
122
+ readonly object: string;
123
+ /** Timestamp of creation */
124
+ readonly created: number;
125
+ /** Model used for generation */
126
+ readonly model: string;
127
+ /** Array of completion choices */
128
+ readonly choices: GroqChoice[];
129
+ /** Token usage information */
130
+ readonly usage: GroqUsage;
131
+ /** System fingerprint (Groq specific) */
132
+ readonly system_fingerprint?: string;
133
+ /** X Groq (Groq specific) */
134
+ readonly x_groq?: {
135
+ readonly id?: string;
136
+ };
137
+ }
138
+
139
+ /**
140
+ * Individual chunk choice for streaming
141
+ */
142
+ export interface GroqChunkChoice {
143
+ /** Index of the choice */
144
+ readonly index: number;
145
+ /** Delta message (partial content) */
146
+ readonly delta: {
147
+ readonly role?: "assistant";
148
+ readonly content?: string;
149
+ };
150
+ /** Reason for finish (null if not finished) */
151
+ readonly finish_reason: GroqFinishReason | null;
152
+ /** Logprobs (optional) */
153
+ readonly logprobs: null | object;
154
+ }
155
+
156
+ /**
157
+ * Streaming chunk response
158
+ */
159
+ export interface GroqChatChunk {
160
+ /** Unique identifier for the response */
161
+ readonly id: string;
162
+ /** Object type (chat.completion.chunk) */
163
+ readonly object: string;
164
+ /** Timestamp of creation */
165
+ readonly created: number;
166
+ /** Model used for generation */
167
+ readonly model: string;
168
+ /** Array of completion choices */
169
+ readonly choices: GroqChunkChoice[];
170
+ /** System fingerprint (Groq specific) */
171
+ readonly system_fingerprint?: string;
172
+ /** X Groq (Groq specific) */
173
+ readonly x_groq?: {
174
+ readonly id?: string;
175
+ };
176
+ }
177
+
178
+ /**
179
+ * API error response
180
+ */
181
+ export interface GroqErrorResponse {
182
+ /** Error type */
183
+ readonly error: {
184
+ /** Error message */
185
+ readonly message: string;
186
+ /** Error type */
187
+ readonly type: string;
188
+ /** Error code */
189
+ readonly code?: string;
190
+ };
191
+ }
192
+
193
+ /**
194
+ * Input types for mutations
195
+ */
196
+ export type GroqMessageCreateInput = Omit<GroqMessage, 'content'> & { content: string };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Domain Entities Index
3
+ * Subpath: @umituz/web-ai-groq-provider/domain
4
+ */
5
+
6
+ export type {
7
+ GroqConfig,
8
+ GroqGenerationConfig,
9
+ GroqMessageRole,
10
+ GroqMessage,
11
+ GroqChatRequest,
12
+ GroqChoice,
13
+ GroqFinishReason,
14
+ GroqUsage,
15
+ GroqChatResponse,
16
+ GroqChunkChoice,
17
+ GroqChatChunk,
18
+ GroqErrorResponse,
19
+ GroqMessageCreateInput,
20
+ } from "./groq.entities";
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Domain Layer Index
3
+ * Subpath: @umituz/web-ai-groq-provider/domain
4
+ *
5
+ * Exports all entities and interfaces
6
+ */
7
+
8
+ // Entities
9
+ export type {
10
+ GroqMessageRole,
11
+ GroqMessage,
12
+ GroqChatRequest,
13
+ GroqChoice,
14
+ GroqFinishReason,
15
+ GroqUsage,
16
+ GroqChatResponse,
17
+ GroqChunkChoice,
18
+ GroqChatChunk,
19
+ GroqErrorResponse,
20
+ GroqMessageCreateInput,
21
+ } from "./entities";
22
+
23
+ // Interfaces
24
+ export type {
25
+ GroqConfig,
26
+ GroqGenerationConfig,
27
+ TextGenerationOptions,
28
+ StreamingCallbacks,
29
+ StructuredGenerationOptions,
30
+ IGroqChatService,
31
+ IGroqHttpClient,
32
+ } from "./interfaces";
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Groq Service Interfaces
3
+ * @description Contracts for Groq AI services
4
+ */
5
+
6
+ import type {
7
+ GroqMessage,
8
+ GroqChatResponse,
9
+ GroqChatChunk,
10
+ GroqChatRequest,
11
+ GroqConfig,
12
+ GroqGenerationConfig,
13
+ } from "../entities";
14
+
15
+ /**
16
+ * Text generation options
17
+ */
18
+ export interface TextGenerationOptions {
19
+ /** Model to use */
20
+ readonly model?: string;
21
+ /** Generation configuration */
22
+ readonly generationConfig?: GroqGenerationConfig;
23
+ }
24
+
25
+ /**
26
+ * Streaming callbacks
27
+ */
28
+ export interface StreamingCallbacks {
29
+ /** Called for each chunk */
30
+ readonly onChunk?: (chunk: string) => void;
31
+ /** Called on completion */
32
+ readonly onComplete?: (fullText: string) => void;
33
+ /** Called on error */
34
+ readonly onError?: (error: Error) => void;
35
+ }
36
+
37
+ /**
38
+ * Structured text generation options
39
+ */
40
+ export interface StructuredGenerationOptions<T = unknown> {
41
+ /** Model to use */
42
+ readonly model?: string;
43
+ /** Generation configuration */
44
+ readonly generationConfig?: GroqGenerationConfig;
45
+ /** JSON schema for validation */
46
+ readonly schema?: Record<string, unknown>;
47
+ }
48
+
49
+ /**
50
+ * Chat completion service interface
51
+ */
52
+ export interface IGroqChatService {
53
+ /** Generate completion from prompt */
54
+ generateCompletion(prompt: string, options?: TextGenerationOptions): Promise<string>;
55
+ /** Generate completion from messages */
56
+ generateChatCompletion(messages: GroqMessage[], options?: TextGenerationOptions): Promise<string>;
57
+ /** Generate structured JSON output */
58
+ generateStructured<T>(prompt: string, options?: StructuredGenerationOptions<T>): Promise<T>;
59
+ /** Stream completion */
60
+ streamCompletion(
61
+ prompt: string,
62
+ callbacks: StreamingCallbacks,
63
+ options?: TextGenerationOptions
64
+ ): AsyncGenerator<void, void, unknown>;
65
+ }
66
+
67
+ /**
68
+ * HTTP client interface
69
+ */
70
+ export interface IGroqHttpClient {
71
+ /** Send chat completion request */
72
+ postChatCompletion(request: GroqChatRequest): Promise<GroqChatResponse>;
73
+ /** Send streaming request */
74
+ streamChatCompletion(request: GroqChatRequest): Promise<ReadableStream>;
75
+ /** Get API key */
76
+ getApiKey(): string;
77
+ /** Set API key */
78
+ setApiKey(apiKey: string): void;
79
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Domain Interfaces Index
3
+ * Subpath: @umituz/web-ai-groq-provider/domain
4
+ */
5
+
6
+ export type {
7
+ TextGenerationOptions,
8
+ StreamingCallbacks,
9
+ StructuredGenerationOptions,
10
+ } from "./groq.interface";
11
+
12
+ export type { IGroqChatService, IGroqHttpClient } from "./groq.interface";
13
+
14
+ // Re-export from entities for convenience
15
+ export type {
16
+ GroqConfig,
17
+ GroqGenerationConfig,
18
+ GroqMessage,
19
+ GroqChatRequest,
20
+ GroqChatResponse,
21
+ GroqChatChunk,
22
+ } from "../entities";
package/src/factory.ts ADDED
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Provider Factory
3
+ * Factory for creating configured Groq provider instances
4
+ */
5
+
6
+ import { groqHttpClient } from "./client";
7
+ import type { ProviderConfig } from "./config";
8
+
9
+ /**
10
+ * Initialize Groq provider with configuration
11
+ */
12
+ export function initializeProvider(config: ProviderConfig): void {
13
+ groqHttpClient.initialize({
14
+ apiKey: config.apiKey,
15
+ baseUrl: config.baseUrl,
16
+ timeoutMs: config.timeoutMs,
17
+ textModel: config.defaultModel,
18
+ });
19
+ }
20
+
21
+ /**
22
+ * Provider factory - creates configured provider instances
23
+ */
24
+ export const providerFactory = {
25
+ /**
26
+ * Create a new provider instance
27
+ */
28
+ create(config: ProviderConfig): void {
29
+ initializeProvider(config);
30
+ },
31
+
32
+ /**
33
+ * Create provider from environment variables
34
+ */
35
+ fromEnv(): void {
36
+ const apiKey =
37
+ (typeof process !== "undefined" && process.env?.GROQ_API_KEY) ||
38
+ (typeof window !== "undefined" &&
39
+ (window as unknown as Record<string, unknown>).__GROQ_API_KEY__
40
+ ? String(
41
+ (window as unknown as Record<string, unknown>).__GROQ_API_KEY__
42
+ )
43
+ : "");
44
+
45
+ if (!apiKey) {
46
+ throw new Error("GROQ_API_KEY environment variable is not set");
47
+ }
48
+
49
+ initializeProvider({
50
+ apiKey,
51
+ baseUrl:
52
+ (typeof process !== "undefined" && process.env?.GROQ_BASE_URL) ||
53
+ undefined,
54
+ timeoutMs:
55
+ typeof process !== "undefined" && process.env?.GROQ_TIMEOUT_MS
56
+ ? parseInt(process.env.GROQ_TIMEOUT_MS)
57
+ : undefined,
58
+ });
59
+ },
60
+
61
+ /**
62
+ * Reset provider (clear configuration)
63
+ */
64
+ reset(): void {
65
+ groqHttpClient.reset();
66
+ },
67
+
68
+ /**
69
+ * Check if provider is initialized
70
+ */
71
+ isInitialized(): boolean {
72
+ return groqHttpClient.isInitialized();
73
+ },
74
+ };
75
+
76
+ /**
77
+ * Convenience function to initialize provider
78
+ */
79
+ export function configureProvider(config: ProviderConfig): void {
80
+ providerFactory.create(config);
81
+ }
82
+
83
+ /**
84
+ * Convenience function to reset provider
85
+ */
86
+ export function resetProvider(): void {
87
+ providerFactory.reset();
88
+ }
89
+
90
+ // Re-export builders
91
+ export { ConfigBuilder, GenerationConfigBuilder } from "./config";
92
+ export type { ProviderConfig } from "./config";