@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,322 @@
|
|
|
1
|
+
import { k as GroqMessage, T as TextGenerationService } from '../../text-generation.service-BP4Q_SH0.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Chat Domain Entities
|
|
5
|
+
* @description Core chat message and conversation types.
|
|
6
|
+
* All fields are readonly: consumers must not mutate
|
|
7
|
+
* messages that are in-flight or persisted.
|
|
8
|
+
*/
|
|
9
|
+
type ChatMessageSender = "user" | "assistant" | "system";
|
|
10
|
+
type ChatMessageType = "text" | "image" | "audio" | "sticker";
|
|
11
|
+
/**
|
|
12
|
+
* Chat message structure.
|
|
13
|
+
* Use the `metadata` field for provider-specific or app-specific data
|
|
14
|
+
* (e.g., model name, token usage, reactions, or any other extension).
|
|
15
|
+
*/
|
|
16
|
+
interface ChatMessage {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly sender: ChatMessageSender;
|
|
19
|
+
readonly content: string;
|
|
20
|
+
readonly timestamp: string;
|
|
21
|
+
readonly type?: ChatMessageType;
|
|
22
|
+
readonly imageUrl?: string;
|
|
23
|
+
readonly reaction?: string;
|
|
24
|
+
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Conversation metadata, derived from the message history of a conversation.
|
|
28
|
+
*/
|
|
29
|
+
interface ChatConversation {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
readonly companionId: string;
|
|
32
|
+
readonly companionName: string;
|
|
33
|
+
readonly lastMessage?: ChatMessage;
|
|
34
|
+
readonly messageCount: number;
|
|
35
|
+
readonly updatedAt: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Input for a single chat turn.
|
|
39
|
+
* The companionId is the addressable identifier for the chat partner
|
|
40
|
+
* (e.g., a character, agent, or session).
|
|
41
|
+
*/
|
|
42
|
+
interface SendMessageInput {
|
|
43
|
+
readonly companionId: string;
|
|
44
|
+
readonly text: string;
|
|
45
|
+
readonly type?: ChatMessageType;
|
|
46
|
+
readonly imageUrl?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Result of a single chat turn: the user message that was sent
|
|
50
|
+
* and the assistant's reply (if generation succeeded).
|
|
51
|
+
*/
|
|
52
|
+
interface SendMessageResult {
|
|
53
|
+
readonly userMessage: ChatMessage;
|
|
54
|
+
readonly aiResponse?: ChatMessage;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Chat configuration.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const config: ChatConfig = {
|
|
62
|
+
* systemPrompt: "You are a helpful assistant.",
|
|
63
|
+
* temperature: 0.7,
|
|
64
|
+
* maxTokens: 800,
|
|
65
|
+
* };
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
interface ChatConfig {
|
|
69
|
+
/** System prompt prepended to the conversation. */
|
|
70
|
+
readonly systemPrompt?: string;
|
|
71
|
+
/** Sampling temperature (0.0 - 2.0). */
|
|
72
|
+
readonly temperature?: number;
|
|
73
|
+
/** Maximum number of tokens to generate per response. */
|
|
74
|
+
readonly maxTokens?: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Storage contract for persisting chat messages.
|
|
78
|
+
* The host application provides an implementation; the library never
|
|
79
|
+
* persists on its own.
|
|
80
|
+
*/
|
|
81
|
+
interface IChatStorage {
|
|
82
|
+
saveMessage(conversationId: string, message: ChatMessage): Promise<void>;
|
|
83
|
+
getMessages(conversationId: string): Promise<ChatMessage[]>;
|
|
84
|
+
updateMessage(messageId: string, updates: Partial<ChatMessage>): Promise<void>;
|
|
85
|
+
deleteMessage(messageId: string): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Token usage information returned by the model, normalized to the chat layer.
|
|
89
|
+
*/
|
|
90
|
+
interface ChatTokenUsage {
|
|
91
|
+
readonly promptTokens: number;
|
|
92
|
+
readonly completionTokens: number;
|
|
93
|
+
readonly totalTokens: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* System Prompt Strategy
|
|
98
|
+
* @description Resolves the system prompt to use for a chat session.
|
|
99
|
+
* Allows library consumers to plug in their own prompts
|
|
100
|
+
* without hardcoded language defaults.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const customStrategy: SystemPromptStrategy = {
|
|
105
|
+
* resolve: (config) =>
|
|
106
|
+
* config.language === "tr"
|
|
107
|
+
* ? "Sen yardimsever bir asistansin."
|
|
108
|
+
* : "You are a helpful assistant.",
|
|
109
|
+
* };
|
|
110
|
+
* chatService.setPromptStrategy(customStrategy);
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
interface SystemPromptStrategy {
|
|
115
|
+
/**
|
|
116
|
+
* Resolve the system prompt for the given config.
|
|
117
|
+
* Returning `undefined` omits the system message entirely.
|
|
118
|
+
*/
|
|
119
|
+
resolve(config: ChatConfig): string | undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Default strategy: use the user-supplied `systemPrompt` if present,
|
|
123
|
+
* otherwise return undefined (no system message sent).
|
|
124
|
+
*/
|
|
125
|
+
declare const DEFAULT_PROMPT_STRATEGY: SystemPromptStrategy;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Chat Service Interfaces
|
|
129
|
+
* @description Contracts for chat functionality.
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Chat service contract. The high-level service used by `useChat`
|
|
134
|
+
* and any other consumer (e.g., CLI, server, scripts).
|
|
135
|
+
*/
|
|
136
|
+
interface IChatService {
|
|
137
|
+
/** Send a single turn and get the user message + AI response back. */
|
|
138
|
+
sendMessage(input: SendMessageInput): Promise<SendMessageResult>;
|
|
139
|
+
/** Get a single conversation summary by id. */
|
|
140
|
+
getConversation(conversationId: string): Promise<ChatConversation | null>;
|
|
141
|
+
/** List conversations. The default implementation returns an empty list. */
|
|
142
|
+
getConversations(): Promise<ChatConversation[]>;
|
|
143
|
+
/** Generate the AI response for a single turn given context. */
|
|
144
|
+
generateAIResponse(companionId: string, userMessage: string, context?: ReadonlyArray<ChatMessage>): Promise<ChatMessage>;
|
|
145
|
+
/** Generate a streaming AI response, emitting chunks via the callback. */
|
|
146
|
+
generateStreamingResponse(userMessage: string, context: ReadonlyArray<ChatMessage>, onChunk: (chunk: string) => void): Promise<string>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Options for the `useChat` hook.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const opts: UseChatOptions = {
|
|
154
|
+
* conversationId: "session-1",
|
|
155
|
+
* stream: true,
|
|
156
|
+
* storage: myStorageImpl,
|
|
157
|
+
* config: { systemPrompt: "You are a helpful assistant.", temperature: 0.7 },
|
|
158
|
+
* };
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
interface UseChatOptions {
|
|
162
|
+
/** Conversation identifier; also used as the storage key. */
|
|
163
|
+
readonly conversationId: string;
|
|
164
|
+
/** Optional storage implementation. When omitted, the hook is in-memory only. */
|
|
165
|
+
readonly storage?: IChatStorage;
|
|
166
|
+
/** Chat configuration (system prompt, temperature, max tokens). */
|
|
167
|
+
readonly config?: ChatConfig;
|
|
168
|
+
/** When true (default), messages are persisted to storage automatically. */
|
|
169
|
+
readonly autoSave?: boolean;
|
|
170
|
+
/** When true, AI responses are streamed. Default: false. */
|
|
171
|
+
readonly stream?: boolean;
|
|
172
|
+
readonly onMessageSent?: (message: ChatMessage) => void;
|
|
173
|
+
readonly onAIResponse?: (message: ChatMessage) => void;
|
|
174
|
+
readonly onError?: (error: string) => void;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Return value of the `useChat` hook.
|
|
178
|
+
*/
|
|
179
|
+
interface UseChatReturn {
|
|
180
|
+
/** Messages in the current conversation. */
|
|
181
|
+
readonly messages: ReadonlyArray<ChatMessage>;
|
|
182
|
+
/** True while a request is in flight (any turn). */
|
|
183
|
+
readonly isLoading: boolean;
|
|
184
|
+
/** True while the assistant is producing a response (including streaming). */
|
|
185
|
+
readonly isTyping: boolean;
|
|
186
|
+
/** Last error message; null when there is no error. */
|
|
187
|
+
readonly error: string | null;
|
|
188
|
+
/** Send a message; resolves with the AI response (or null on cancel/error). */
|
|
189
|
+
sendMessage: (text: string, type?: ChatMessage["type"]) => Promise<ChatMessage | null>;
|
|
190
|
+
/** Regenerate the most recent assistant response. */
|
|
191
|
+
regenerate: () => Promise<ChatMessage | null>;
|
|
192
|
+
/** Cancel any in-flight request issued by this hook instance. */
|
|
193
|
+
cancel: () => void;
|
|
194
|
+
/** Clear all messages and the error state. */
|
|
195
|
+
clearMessages: () => void;
|
|
196
|
+
/** Clear the error state only. */
|
|
197
|
+
clearError: () => void;
|
|
198
|
+
/** Reset transient state (loading/typing/error). Keeps messages. */
|
|
199
|
+
reset: () => void;
|
|
200
|
+
/** Update the chat configuration. */
|
|
201
|
+
updateConfig: (config: Partial<ChatConfig>) => void;
|
|
202
|
+
}
|
|
203
|
+
interface IMessageFormatter {
|
|
204
|
+
toGroqMessages(messages: ReadonlyArray<ChatMessage>, systemPrompt?: string): GroqMessage[];
|
|
205
|
+
toChatMessage(content: string, sender: ChatMessage["sender"]): ChatMessage;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Chat Service
|
|
210
|
+
* @description Core chat logic. Composes:
|
|
211
|
+
* - text generation (from groq domain)
|
|
212
|
+
* - message formatting
|
|
213
|
+
* - system prompt resolution (via injected strategy)
|
|
214
|
+
*
|
|
215
|
+
* Does NOT hardcode any language or fallback content.
|
|
216
|
+
* Errors propagate to the caller; the consumer decides
|
|
217
|
+
* how to render a user-friendly message.
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
declare class ChatService implements IChatService {
|
|
221
|
+
private readonly textGeneration;
|
|
222
|
+
private config;
|
|
223
|
+
private promptStrategy;
|
|
224
|
+
private storage;
|
|
225
|
+
constructor(textGeneration: TextGenerationService);
|
|
226
|
+
initialize(config: ChatConfig): void;
|
|
227
|
+
updateConfig(config: Partial<ChatConfig>): void;
|
|
228
|
+
setPromptStrategy(strategy: SystemPromptStrategy): void;
|
|
229
|
+
setStorage(storage: IChatStorage): void;
|
|
230
|
+
getConversation(conversationId: string): Promise<ChatConversation | null>;
|
|
231
|
+
getConversations(): Promise<ChatConversation[]>;
|
|
232
|
+
sendMessage(input: SendMessageInput): Promise<SendMessageResult>;
|
|
233
|
+
generateAIResponse(companionId: string, userMessage: string, context?: ReadonlyArray<ChatMessage>): Promise<ChatMessage>;
|
|
234
|
+
/**
|
|
235
|
+
* Generate a streaming AI response. Emits content chunks via the
|
|
236
|
+
* `onChunk` callback. Resolves once the stream completes.
|
|
237
|
+
*
|
|
238
|
+
* @param userMessage - The latest user message.
|
|
239
|
+
* @param context - The message history to send as context.
|
|
240
|
+
* @param onChunk - Called for each content chunk.
|
|
241
|
+
*/
|
|
242
|
+
generateStreamingResponse(userMessage: string, context: ReadonlyArray<ChatMessage>, onChunk: (chunk: string) => void): Promise<string>;
|
|
243
|
+
private requireStorage;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Helper to extract a `ChatTokenUsage` from a ChatMessage's metadata,
|
|
247
|
+
* if the model used reported it.
|
|
248
|
+
*
|
|
249
|
+
* @param message - The chat message to inspect.
|
|
250
|
+
* @returns The token usage, or `null` when not present.
|
|
251
|
+
*/
|
|
252
|
+
declare function extractTokenUsage(message: ChatMessage): ChatTokenUsage | null;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Chat ID Generator
|
|
256
|
+
* @description Generates unique message IDs. Delegates randomness
|
|
257
|
+
* to calculations/random-base36-suffix so the random
|
|
258
|
+
* source is configurable (and the ID format is unit-testable).
|
|
259
|
+
*
|
|
260
|
+
* Format: `msg-{timestamp}-{sender}-{counter}-{random}`
|
|
261
|
+
*/
|
|
262
|
+
declare class ChatIdGenerator {
|
|
263
|
+
private counter;
|
|
264
|
+
private readonly randomLength;
|
|
265
|
+
constructor(randomLength?: number);
|
|
266
|
+
/**
|
|
267
|
+
* Generate a new unique message ID.
|
|
268
|
+
*/
|
|
269
|
+
next(sender: string): string;
|
|
270
|
+
}
|
|
271
|
+
declare const chatIdGenerator: ChatIdGenerator;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Message Formatter
|
|
275
|
+
* @description Converts between ChatMessage (domain) and GroqMessage (transport).
|
|
276
|
+
* Does NOT include any prompts or fallback content; those are
|
|
277
|
+
* injected by the consumer (see prompt-strategy.ts).
|
|
278
|
+
*/
|
|
279
|
+
|
|
280
|
+
declare class MessageFormatter implements IMessageFormatter {
|
|
281
|
+
private readonly idGenerator;
|
|
282
|
+
constructor(idGenerator?: ChatIdGenerator);
|
|
283
|
+
toGroqMessages(messages: ReadonlyArray<ChatMessage>, systemPrompt?: string): GroqMessage[];
|
|
284
|
+
toChatMessage(content: string, sender: ChatMessage["sender"]): ChatMessage;
|
|
285
|
+
}
|
|
286
|
+
declare const messageFormatter: MessageFormatter;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* useChat Hook
|
|
290
|
+
* @description React hook for chat sessions with AI integration.
|
|
291
|
+
*
|
|
292
|
+
* Features:
|
|
293
|
+
* - Per-conversation message list with optional storage
|
|
294
|
+
* - Streaming AI responses (opt-in via `stream: true`)
|
|
295
|
+
* - Per-component AbortController: cancel in-flight requests
|
|
296
|
+
* without affecting other consumers
|
|
297
|
+
* - Errors surface through `onError` (no silent swallowing)
|
|
298
|
+
* - Storage errors propagate the same way as generation errors
|
|
299
|
+
*
|
|
300
|
+
* Requires an injected ChatService via the second argument
|
|
301
|
+
* (typically obtained from `getProvider().chat`).
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```tsx
|
|
305
|
+
* import { useChat, getProvider } from "@umituz/web-ai-groq-provider";
|
|
306
|
+
*
|
|
307
|
+
* function ChatScreen() {
|
|
308
|
+
* const { messages, sendMessage, isLoading, isTyping, error } = useChat(
|
|
309
|
+
* { conversationId: "demo-1" },
|
|
310
|
+
* { chatService: getProvider()!.chat },
|
|
311
|
+
* );
|
|
312
|
+
* // ...
|
|
313
|
+
* }
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
|
|
317
|
+
interface UseChatDeps {
|
|
318
|
+
readonly chatService: ChatService;
|
|
319
|
+
}
|
|
320
|
+
declare function useChat(options: UseChatOptions, deps?: UseChatDeps): UseChatReturn;
|
|
321
|
+
|
|
322
|
+
export { type ChatConfig, type ChatConversation, ChatIdGenerator, type ChatMessage, type ChatMessageSender, type ChatMessageType, ChatService, type ChatTokenUsage, DEFAULT_PROMPT_STRATEGY, type IChatService, type IChatStorage, type IMessageFormatter, type SendMessageInput, type SendMessageResult, type SystemPromptStrategy, type UseChatDeps, type UseChatOptions, type UseChatReturn, chatIdGenerator, extractTokenUsage, messageFormatter, useChat };
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { k as GroqMessage, T as TextGenerationService } from '../../text-generation.service-BP4Q_SH0.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Chat Domain Entities
|
|
5
|
+
* @description Core chat message and conversation types.
|
|
6
|
+
* All fields are readonly: consumers must not mutate
|
|
7
|
+
* messages that are in-flight or persisted.
|
|
8
|
+
*/
|
|
9
|
+
type ChatMessageSender = "user" | "assistant" | "system";
|
|
10
|
+
type ChatMessageType = "text" | "image" | "audio" | "sticker";
|
|
11
|
+
/**
|
|
12
|
+
* Chat message structure.
|
|
13
|
+
* Use the `metadata` field for provider-specific or app-specific data
|
|
14
|
+
* (e.g., model name, token usage, reactions, or any other extension).
|
|
15
|
+
*/
|
|
16
|
+
interface ChatMessage {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly sender: ChatMessageSender;
|
|
19
|
+
readonly content: string;
|
|
20
|
+
readonly timestamp: string;
|
|
21
|
+
readonly type?: ChatMessageType;
|
|
22
|
+
readonly imageUrl?: string;
|
|
23
|
+
readonly reaction?: string;
|
|
24
|
+
readonly metadata?: Readonly<Record<string, unknown>>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Conversation metadata, derived from the message history of a conversation.
|
|
28
|
+
*/
|
|
29
|
+
interface ChatConversation {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
readonly companionId: string;
|
|
32
|
+
readonly companionName: string;
|
|
33
|
+
readonly lastMessage?: ChatMessage;
|
|
34
|
+
readonly messageCount: number;
|
|
35
|
+
readonly updatedAt: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Input for a single chat turn.
|
|
39
|
+
* The companionId is the addressable identifier for the chat partner
|
|
40
|
+
* (e.g., a character, agent, or session).
|
|
41
|
+
*/
|
|
42
|
+
interface SendMessageInput {
|
|
43
|
+
readonly companionId: string;
|
|
44
|
+
readonly text: string;
|
|
45
|
+
readonly type?: ChatMessageType;
|
|
46
|
+
readonly imageUrl?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Result of a single chat turn: the user message that was sent
|
|
50
|
+
* and the assistant's reply (if generation succeeded).
|
|
51
|
+
*/
|
|
52
|
+
interface SendMessageResult {
|
|
53
|
+
readonly userMessage: ChatMessage;
|
|
54
|
+
readonly aiResponse?: ChatMessage;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Chat configuration.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const config: ChatConfig = {
|
|
62
|
+
* systemPrompt: "You are a helpful assistant.",
|
|
63
|
+
* temperature: 0.7,
|
|
64
|
+
* maxTokens: 800,
|
|
65
|
+
* };
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
interface ChatConfig {
|
|
69
|
+
/** System prompt prepended to the conversation. */
|
|
70
|
+
readonly systemPrompt?: string;
|
|
71
|
+
/** Sampling temperature (0.0 - 2.0). */
|
|
72
|
+
readonly temperature?: number;
|
|
73
|
+
/** Maximum number of tokens to generate per response. */
|
|
74
|
+
readonly maxTokens?: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Storage contract for persisting chat messages.
|
|
78
|
+
* The host application provides an implementation; the library never
|
|
79
|
+
* persists on its own.
|
|
80
|
+
*/
|
|
81
|
+
interface IChatStorage {
|
|
82
|
+
saveMessage(conversationId: string, message: ChatMessage): Promise<void>;
|
|
83
|
+
getMessages(conversationId: string): Promise<ChatMessage[]>;
|
|
84
|
+
updateMessage(messageId: string, updates: Partial<ChatMessage>): Promise<void>;
|
|
85
|
+
deleteMessage(messageId: string): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Token usage information returned by the model, normalized to the chat layer.
|
|
89
|
+
*/
|
|
90
|
+
interface ChatTokenUsage {
|
|
91
|
+
readonly promptTokens: number;
|
|
92
|
+
readonly completionTokens: number;
|
|
93
|
+
readonly totalTokens: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* System Prompt Strategy
|
|
98
|
+
* @description Resolves the system prompt to use for a chat session.
|
|
99
|
+
* Allows library consumers to plug in their own prompts
|
|
100
|
+
* without hardcoded language defaults.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const customStrategy: SystemPromptStrategy = {
|
|
105
|
+
* resolve: (config) =>
|
|
106
|
+
* config.language === "tr"
|
|
107
|
+
* ? "Sen yardimsever bir asistansin."
|
|
108
|
+
* : "You are a helpful assistant.",
|
|
109
|
+
* };
|
|
110
|
+
* chatService.setPromptStrategy(customStrategy);
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
interface SystemPromptStrategy {
|
|
115
|
+
/**
|
|
116
|
+
* Resolve the system prompt for the given config.
|
|
117
|
+
* Returning `undefined` omits the system message entirely.
|
|
118
|
+
*/
|
|
119
|
+
resolve(config: ChatConfig): string | undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Default strategy: use the user-supplied `systemPrompt` if present,
|
|
123
|
+
* otherwise return undefined (no system message sent).
|
|
124
|
+
*/
|
|
125
|
+
declare const DEFAULT_PROMPT_STRATEGY: SystemPromptStrategy;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Chat Service Interfaces
|
|
129
|
+
* @description Contracts for chat functionality.
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Chat service contract. The high-level service used by `useChat`
|
|
134
|
+
* and any other consumer (e.g., CLI, server, scripts).
|
|
135
|
+
*/
|
|
136
|
+
interface IChatService {
|
|
137
|
+
/** Send a single turn and get the user message + AI response back. */
|
|
138
|
+
sendMessage(input: SendMessageInput): Promise<SendMessageResult>;
|
|
139
|
+
/** Get a single conversation summary by id. */
|
|
140
|
+
getConversation(conversationId: string): Promise<ChatConversation | null>;
|
|
141
|
+
/** List conversations. The default implementation returns an empty list. */
|
|
142
|
+
getConversations(): Promise<ChatConversation[]>;
|
|
143
|
+
/** Generate the AI response for a single turn given context. */
|
|
144
|
+
generateAIResponse(companionId: string, userMessage: string, context?: ReadonlyArray<ChatMessage>): Promise<ChatMessage>;
|
|
145
|
+
/** Generate a streaming AI response, emitting chunks via the callback. */
|
|
146
|
+
generateStreamingResponse(userMessage: string, context: ReadonlyArray<ChatMessage>, onChunk: (chunk: string) => void): Promise<string>;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Options for the `useChat` hook.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const opts: UseChatOptions = {
|
|
154
|
+
* conversationId: "session-1",
|
|
155
|
+
* stream: true,
|
|
156
|
+
* storage: myStorageImpl,
|
|
157
|
+
* config: { systemPrompt: "You are a helpful assistant.", temperature: 0.7 },
|
|
158
|
+
* };
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
interface UseChatOptions {
|
|
162
|
+
/** Conversation identifier; also used as the storage key. */
|
|
163
|
+
readonly conversationId: string;
|
|
164
|
+
/** Optional storage implementation. When omitted, the hook is in-memory only. */
|
|
165
|
+
readonly storage?: IChatStorage;
|
|
166
|
+
/** Chat configuration (system prompt, temperature, max tokens). */
|
|
167
|
+
readonly config?: ChatConfig;
|
|
168
|
+
/** When true (default), messages are persisted to storage automatically. */
|
|
169
|
+
readonly autoSave?: boolean;
|
|
170
|
+
/** When true, AI responses are streamed. Default: false. */
|
|
171
|
+
readonly stream?: boolean;
|
|
172
|
+
readonly onMessageSent?: (message: ChatMessage) => void;
|
|
173
|
+
readonly onAIResponse?: (message: ChatMessage) => void;
|
|
174
|
+
readonly onError?: (error: string) => void;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Return value of the `useChat` hook.
|
|
178
|
+
*/
|
|
179
|
+
interface UseChatReturn {
|
|
180
|
+
/** Messages in the current conversation. */
|
|
181
|
+
readonly messages: ReadonlyArray<ChatMessage>;
|
|
182
|
+
/** True while a request is in flight (any turn). */
|
|
183
|
+
readonly isLoading: boolean;
|
|
184
|
+
/** True while the assistant is producing a response (including streaming). */
|
|
185
|
+
readonly isTyping: boolean;
|
|
186
|
+
/** Last error message; null when there is no error. */
|
|
187
|
+
readonly error: string | null;
|
|
188
|
+
/** Send a message; resolves with the AI response (or null on cancel/error). */
|
|
189
|
+
sendMessage: (text: string, type?: ChatMessage["type"]) => Promise<ChatMessage | null>;
|
|
190
|
+
/** Regenerate the most recent assistant response. */
|
|
191
|
+
regenerate: () => Promise<ChatMessage | null>;
|
|
192
|
+
/** Cancel any in-flight request issued by this hook instance. */
|
|
193
|
+
cancel: () => void;
|
|
194
|
+
/** Clear all messages and the error state. */
|
|
195
|
+
clearMessages: () => void;
|
|
196
|
+
/** Clear the error state only. */
|
|
197
|
+
clearError: () => void;
|
|
198
|
+
/** Reset transient state (loading/typing/error). Keeps messages. */
|
|
199
|
+
reset: () => void;
|
|
200
|
+
/** Update the chat configuration. */
|
|
201
|
+
updateConfig: (config: Partial<ChatConfig>) => void;
|
|
202
|
+
}
|
|
203
|
+
interface IMessageFormatter {
|
|
204
|
+
toGroqMessages(messages: ReadonlyArray<ChatMessage>, systemPrompt?: string): GroqMessage[];
|
|
205
|
+
toChatMessage(content: string, sender: ChatMessage["sender"]): ChatMessage;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Chat Service
|
|
210
|
+
* @description Core chat logic. Composes:
|
|
211
|
+
* - text generation (from groq domain)
|
|
212
|
+
* - message formatting
|
|
213
|
+
* - system prompt resolution (via injected strategy)
|
|
214
|
+
*
|
|
215
|
+
* Does NOT hardcode any language or fallback content.
|
|
216
|
+
* Errors propagate to the caller; the consumer decides
|
|
217
|
+
* how to render a user-friendly message.
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
declare class ChatService implements IChatService {
|
|
221
|
+
private readonly textGeneration;
|
|
222
|
+
private config;
|
|
223
|
+
private promptStrategy;
|
|
224
|
+
private storage;
|
|
225
|
+
constructor(textGeneration: TextGenerationService);
|
|
226
|
+
initialize(config: ChatConfig): void;
|
|
227
|
+
updateConfig(config: Partial<ChatConfig>): void;
|
|
228
|
+
setPromptStrategy(strategy: SystemPromptStrategy): void;
|
|
229
|
+
setStorage(storage: IChatStorage): void;
|
|
230
|
+
getConversation(conversationId: string): Promise<ChatConversation | null>;
|
|
231
|
+
getConversations(): Promise<ChatConversation[]>;
|
|
232
|
+
sendMessage(input: SendMessageInput): Promise<SendMessageResult>;
|
|
233
|
+
generateAIResponse(companionId: string, userMessage: string, context?: ReadonlyArray<ChatMessage>): Promise<ChatMessage>;
|
|
234
|
+
/**
|
|
235
|
+
* Generate a streaming AI response. Emits content chunks via the
|
|
236
|
+
* `onChunk` callback. Resolves once the stream completes.
|
|
237
|
+
*
|
|
238
|
+
* @param userMessage - The latest user message.
|
|
239
|
+
* @param context - The message history to send as context.
|
|
240
|
+
* @param onChunk - Called for each content chunk.
|
|
241
|
+
*/
|
|
242
|
+
generateStreamingResponse(userMessage: string, context: ReadonlyArray<ChatMessage>, onChunk: (chunk: string) => void): Promise<string>;
|
|
243
|
+
private requireStorage;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Helper to extract a `ChatTokenUsage` from a ChatMessage's metadata,
|
|
247
|
+
* if the model used reported it.
|
|
248
|
+
*
|
|
249
|
+
* @param message - The chat message to inspect.
|
|
250
|
+
* @returns The token usage, or `null` when not present.
|
|
251
|
+
*/
|
|
252
|
+
declare function extractTokenUsage(message: ChatMessage): ChatTokenUsage | null;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Chat ID Generator
|
|
256
|
+
* @description Generates unique message IDs. Delegates randomness
|
|
257
|
+
* to calculations/random-base36-suffix so the random
|
|
258
|
+
* source is configurable (and the ID format is unit-testable).
|
|
259
|
+
*
|
|
260
|
+
* Format: `msg-{timestamp}-{sender}-{counter}-{random}`
|
|
261
|
+
*/
|
|
262
|
+
declare class ChatIdGenerator {
|
|
263
|
+
private counter;
|
|
264
|
+
private readonly randomLength;
|
|
265
|
+
constructor(randomLength?: number);
|
|
266
|
+
/**
|
|
267
|
+
* Generate a new unique message ID.
|
|
268
|
+
*/
|
|
269
|
+
next(sender: string): string;
|
|
270
|
+
}
|
|
271
|
+
declare const chatIdGenerator: ChatIdGenerator;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Message Formatter
|
|
275
|
+
* @description Converts between ChatMessage (domain) and GroqMessage (transport).
|
|
276
|
+
* Does NOT include any prompts or fallback content; those are
|
|
277
|
+
* injected by the consumer (see prompt-strategy.ts).
|
|
278
|
+
*/
|
|
279
|
+
|
|
280
|
+
declare class MessageFormatter implements IMessageFormatter {
|
|
281
|
+
private readonly idGenerator;
|
|
282
|
+
constructor(idGenerator?: ChatIdGenerator);
|
|
283
|
+
toGroqMessages(messages: ReadonlyArray<ChatMessage>, systemPrompt?: string): GroqMessage[];
|
|
284
|
+
toChatMessage(content: string, sender: ChatMessage["sender"]): ChatMessage;
|
|
285
|
+
}
|
|
286
|
+
declare const messageFormatter: MessageFormatter;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* useChat Hook
|
|
290
|
+
* @description React hook for chat sessions with AI integration.
|
|
291
|
+
*
|
|
292
|
+
* Features:
|
|
293
|
+
* - Per-conversation message list with optional storage
|
|
294
|
+
* - Streaming AI responses (opt-in via `stream: true`)
|
|
295
|
+
* - Per-component AbortController: cancel in-flight requests
|
|
296
|
+
* without affecting other consumers
|
|
297
|
+
* - Errors surface through `onError` (no silent swallowing)
|
|
298
|
+
* - Storage errors propagate the same way as generation errors
|
|
299
|
+
*
|
|
300
|
+
* Requires an injected ChatService via the second argument
|
|
301
|
+
* (typically obtained from `getProvider().chat`).
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ```tsx
|
|
305
|
+
* import { useChat, getProvider } from "@umituz/web-ai-groq-provider";
|
|
306
|
+
*
|
|
307
|
+
* function ChatScreen() {
|
|
308
|
+
* const { messages, sendMessage, isLoading, isTyping, error } = useChat(
|
|
309
|
+
* { conversationId: "demo-1" },
|
|
310
|
+
* { chatService: getProvider()!.chat },
|
|
311
|
+
* );
|
|
312
|
+
* // ...
|
|
313
|
+
* }
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
|
|
317
|
+
interface UseChatDeps {
|
|
318
|
+
readonly chatService: ChatService;
|
|
319
|
+
}
|
|
320
|
+
declare function useChat(options: UseChatOptions, deps?: UseChatDeps): UseChatReturn;
|
|
321
|
+
|
|
322
|
+
export { type ChatConfig, type ChatConversation, ChatIdGenerator, type ChatMessage, type ChatMessageSender, type ChatMessageType, ChatService, type ChatTokenUsage, DEFAULT_PROMPT_STRATEGY, type IChatService, type IChatStorage, type IMessageFormatter, type SendMessageInput, type SendMessageResult, type SystemPromptStrategy, type UseChatDeps, type UseChatOptions, type UseChatReturn, chatIdGenerator, extractTokenUsage, messageFormatter, useChat };
|