@curaious/uno-converse 0.1.7

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.
@@ -0,0 +1,473 @@
1
+ declare enum Role {
2
+ User = "user",
3
+ Developer = "developer",
4
+ System = "system",
5
+ Assistant = "assistant"
6
+ }
7
+ declare enum MessageType {
8
+ Message = "message",
9
+ FunctionCall = "function_call",
10
+ FunctionCallOutput = "function_call_output",
11
+ Reasoning = "reasoning",
12
+ ImageGenerationCall = "image_generation_call",
13
+ FunctionCallApprovalResponse = "function_call_approval_response"
14
+ }
15
+ declare enum ContentType {
16
+ InputText = "input_text",
17
+ OutputText = "output_text",
18
+ SummaryText = "summary_text",
19
+ InputImage = "input_image"
20
+ }
21
+ declare enum ChunkType {
22
+ ChunkTypeRunCreated = "run.created",
23
+ ChunkTypeRunInProgress = "run.in_progress",
24
+ ChunkTypeRunPaused = "run.paused",
25
+ ChunkTypeRunCompleted = "run.completed",
26
+ ChunkTypeResponseCreated = "response.created",
27
+ ChunkTypeResponseInProgress = "response.in_progress",
28
+ ChunkTypeResponseCompleted = "response.completed",
29
+ ChunkTypeOutputItemAdded = "response.output_item.added",
30
+ ChunkTypeOutputItemDone = "response.output_item.done",
31
+ ChunkTypeContentPartAdded = "response.content_part.added",
32
+ ChunkTypeContentPartDone = "response.content_part.done",
33
+ ChunkTypeOutputTextDelta = "response.output_text.delta",
34
+ ChunkTypeOutputTextDone = "response.output_text.done",
35
+ ChunkTypeFunctionCallArgumentsDelta = "response.function_call_arguments.delta",
36
+ ChunkTypeFunctionCallArgumentsDone = "response.function_call_arguments.done",
37
+ ChunkTypeReasoningSummaryPartAdded = "response.reasoning_summary_part.added",
38
+ ChunkTypeReasoningSummaryPartDone = "response.reasoning_summary_part.done",
39
+ ChunkTypeReasoningSummaryTextDelta = "response.reasoning_summary_text.delta",
40
+ ChunkTypeReasoningSummaryTextDone = "response.reasoning_summary_text.done",
41
+ ChunkTypeImageGenerationCallInProgress = "response.image_generation_call.in_progress",
42
+ ChunkTypeImageGenerationCallGenerating = "response.image_generation_call.generating",
43
+ ChunkTypeImageGenerationCallPartialImage = "response.image_generation_call.partial_image",
44
+ ChunkTypeFunctionCallOutput = "function_call_output"
45
+ }
46
+ /**
47
+ * Represents a conversation container
48
+ */
49
+ interface Conversation {
50
+ namespace_id: string;
51
+ conversation_id: string;
52
+ name: string;
53
+ created_at: string;
54
+ last_updated: string;
55
+ }
56
+ /**
57
+ * Represents a thread within a conversation
58
+ */
59
+ interface Thread {
60
+ conversation_id: string;
61
+ origin_message_id: string;
62
+ thread_id: string;
63
+ meta: Record<string, any>;
64
+ created_at: string;
65
+ last_updated: string;
66
+ }
67
+ /**
68
+ * Represents a message within a conversation thread
69
+ */
70
+ interface ConversationMessage {
71
+ conversation_id: string;
72
+ thread_id: string;
73
+ message_id: string;
74
+ messages: MessageUnion[];
75
+ meta: Record<string, any>;
76
+ isStreaming?: boolean;
77
+ }
78
+ type MessageUnion = EasyMessage | InputMessage | OutputMessage | FunctionCallMessage | FunctionCallApprovalResponseMessage | FunctionCallOutputMessage | ReasoningMessage | ImageGenerationCallMessage;
79
+ interface EasyMessage {
80
+ type: MessageType.Message;
81
+ id: string;
82
+ role?: Role;
83
+ content: EasyInputContentUnion;
84
+ }
85
+ interface InputMessage {
86
+ type: MessageType.Message;
87
+ id?: string;
88
+ role?: Role;
89
+ content?: InputContentUnion[];
90
+ }
91
+ interface OutputMessage {
92
+ id: string;
93
+ type?: MessageType.Message;
94
+ role?: Role;
95
+ content?: OutputContentUnion[];
96
+ }
97
+ interface FunctionCallMessage {
98
+ type: MessageType.FunctionCall;
99
+ id: string;
100
+ call_id?: string;
101
+ name: string;
102
+ arguments: string;
103
+ }
104
+ interface FunctionCallApprovalResponseMessage {
105
+ type: MessageType.FunctionCallApprovalResponse;
106
+ id: string;
107
+ approved_call_ids: string[];
108
+ rejected_call_ids: string[];
109
+ }
110
+ interface FunctionCallOutputMessage {
111
+ type: MessageType.FunctionCallOutput;
112
+ id: string;
113
+ call_id: string;
114
+ output: FunctionCallOutputContentUnion;
115
+ }
116
+ interface ReasoningMessage {
117
+ type: MessageType.Reasoning;
118
+ id: string;
119
+ summary?: SummaryTextContent[];
120
+ encrypted_content?: string;
121
+ }
122
+ interface ImageGenerationCallMessage {
123
+ type: MessageType.ImageGenerationCall;
124
+ id: string;
125
+ status: string;
126
+ background: string;
127
+ output_format: string;
128
+ quality: string;
129
+ size: string;
130
+ result: string;
131
+ }
132
+ type EasyInputContentUnion = string | InputContentUnion;
133
+ type InputContentUnion = InputTextContent | OutputTextContent | InputImageContent;
134
+ type FunctionCallOutputContentUnion = string | InputContentUnion;
135
+ type OutputContentUnion = OutputTextContent | FunctionCallMessage | SummaryTextContent;
136
+ interface InputTextContent {
137
+ type: ContentType.InputText;
138
+ text: string;
139
+ }
140
+ interface OutputTextContent {
141
+ type: ContentType.OutputText;
142
+ text: string;
143
+ }
144
+ interface SummaryTextContent {
145
+ type: ContentType.SummaryText;
146
+ text: string;
147
+ }
148
+ interface InputImageContent {
149
+ type: ContentType.InputImage;
150
+ image_url: string;
151
+ detail: string;
152
+ }
153
+ /**
154
+ * Configuration for the converse API endpoint
155
+ */
156
+ interface ConverseConfig {
157
+ namespace: string;
158
+ agentName: string;
159
+ baseUrl?: string;
160
+ context?: Record<string, unknown>;
161
+ headers?: Record<string, string>;
162
+ }
163
+ interface ResponseChunk {
164
+ type: ChunkType;
165
+ sequence_number: number;
166
+ run_state?: ChunkRunData;
167
+ response?: ChunkResponseData;
168
+ output_index?: number;
169
+ item?: ChunkOutputItemData;
170
+ item_id?: string;
171
+ content_index?: number;
172
+ part?: OutputContentUnion;
173
+ delta?: string;
174
+ text?: string;
175
+ arguments?: string;
176
+ summary_index?: number;
177
+ output: string;
178
+ partial_image_index?: number;
179
+ partial_image_b64?: string;
180
+ background?: string;
181
+ output_format?: string;
182
+ quality?: string;
183
+ size?: string;
184
+ status?: string;
185
+ }
186
+ interface ChunkRunData {
187
+ id: string;
188
+ object: "run";
189
+ status: "created" | "in_progress" | "paused" | "resumed" | "completed" | "aborted";
190
+ pending_tool_calls: FunctionCallMessage[];
191
+ usage: ChunkResponseUsage;
192
+ traceid: string;
193
+ }
194
+ interface ChunkResponseData {
195
+ id: string;
196
+ object: string;
197
+ created_at: number;
198
+ status: string;
199
+ background: boolean;
200
+ error: unknown;
201
+ incomplete_details: unknown;
202
+ output: OutputMessageUnion[];
203
+ usage: ChunkResponseUsage;
204
+ }
205
+ interface ChunkOutputItemData {
206
+ type: string;
207
+ id: string;
208
+ status: string;
209
+ content: OutputContentUnion[];
210
+ role: Role;
211
+ call_id?: string;
212
+ name?: string;
213
+ arguments?: string;
214
+ encrypted_content?: string;
215
+ summary?: OutputContentUnion[];
216
+ background?: string;
217
+ output_format?: string;
218
+ quality?: string;
219
+ result?: string;
220
+ size?: string;
221
+ }
222
+ type OutputMessageUnion = (OutputMessage & {
223
+ id: string;
224
+ }) | (FunctionCallMessage & {
225
+ id: string;
226
+ }) | (ReasoningMessage & {
227
+ id: string;
228
+ }) | (ImageGenerationCallMessage & {
229
+ id: string;
230
+ });
231
+ interface ChunkResponseUsage {
232
+ input_tokens: number;
233
+ input_tokens_details: {
234
+ cached_tokens: number;
235
+ };
236
+ output_tokens: number;
237
+ output_tokens_details: {
238
+ reasoning_tokens: number;
239
+ };
240
+ total_tokens: number;
241
+ }
242
+ interface Usage {
243
+ input_tokens: number;
244
+ output_tokens: number;
245
+ total_tokens: number;
246
+ input_tokens_details: {
247
+ cached_tokens: number;
248
+ };
249
+ output_tokens_details: {
250
+ reasoning_tokens: number;
251
+ };
252
+ }
253
+ declare function isEasyMessage(msg: MessageUnion): msg is EasyMessage;
254
+ declare function isInputMessage(msg: MessageUnion): msg is InputMessage;
255
+ declare function isFunctionCallMessage(msg: MessageUnion): msg is FunctionCallMessage;
256
+ declare function isFunctionCallOutputMessage(msg: MessageUnion): msg is FunctionCallOutputMessage;
257
+ declare function isReasoningMessage(msg: MessageUnion): msg is ReasoningMessage;
258
+ declare function isImageGenerationCallMessage(msg: MessageUnion): msg is ImageGenerationCallMessage;
259
+
260
+ /**
261
+ * Function type for providing custom headers.
262
+ * Called before each request to get headers (useful for dynamic auth tokens).
263
+ *
264
+ * @example
265
+ * ```ts
266
+ * const getHeaders: GetHeadersFn = () => ({
267
+ * 'Authorization': `Bearer ${getToken()}`,
268
+ * 'X-Custom-Header': 'value',
269
+ * });
270
+ * ```
271
+ */
272
+ type GetHeadersFn = () => Record<string, string> | Promise<Record<string, string>>;
273
+ /**
274
+ * Options for the useConversation hook
275
+ */
276
+ interface UseConversationOptions {
277
+ /** The namespace for conversations */
278
+ namespace: string;
279
+ /** Base URL of the Uno Agent Server (e.g., 'https://api.example.com/api/agent-server') */
280
+ baseUrl: string;
281
+ /** Project Name used to fetch the project ID */
282
+ projectName: string;
283
+ /** Optional function to get custom headers for requests (e.g., for authentication) */
284
+ getHeaders?: GetHeadersFn;
285
+ /** Auto-load conversations on mount (default: true) */
286
+ autoLoad?: boolean;
287
+ }
288
+ /**
289
+ * Return type for the useConversation hook
290
+ */
291
+ interface UseConversationReturn {
292
+ /** The fetched project ID */
293
+ projectId: string;
294
+ /** Whether the project ID is being fetched */
295
+ projectLoading: boolean;
296
+ /** List of all conversations */
297
+ conversations: Conversation[];
298
+ /** Whether conversations are being loaded */
299
+ conversationsLoading: boolean;
300
+ /** List of threads in the current conversation */
301
+ threads: Thread[];
302
+ /** Whether threads are being loaded */
303
+ threadsLoading: boolean;
304
+ /** Currently selected thread */
305
+ currentThread: Thread | null;
306
+ /** List of messages in the current thread */
307
+ messages: ConversationMessage[];
308
+ /** Message currently being streamed */
309
+ streamingMessage: ConversationMessage | null;
310
+ /** Whether messages are being loaded */
311
+ messagesLoading: boolean;
312
+ /** Whether a response is currently streaming */
313
+ isStreaming: boolean;
314
+ /** Whether waiting for a response */
315
+ isThinking: boolean;
316
+ /** ID of the currently selected conversation */
317
+ currentConversationId: string | null;
318
+ /** ID of the currently selected thread */
319
+ currentThreadId: string | null;
320
+ /** Load all conversations */
321
+ loadConversations: () => Promise<void>;
322
+ /** Select a conversation by ID */
323
+ selectConversation: (conversationId: string) => void;
324
+ /** Load threads for a conversation */
325
+ loadThreads: (conversationId: string) => Promise<void>;
326
+ /** Select a thread by ID */
327
+ selectThread: (threadId: string) => void;
328
+ /** Send a message and stream the response */
329
+ sendMessage: (userMessages: MessageUnion[], config: ConverseConfig) => Promise<void>;
330
+ /** Start a new chat (clears current state) */
331
+ startNewChat: () => void;
332
+ /** All messages including the currently streaming one */
333
+ allMessages: ConversationMessage[];
334
+ }
335
+ /**
336
+ * A comprehensive hook for managing conversations, threads, messages, and streaming
337
+ * with Uno Agent Server.
338
+ *
339
+ * @example
340
+ * ```tsx
341
+ * import { useConversation } from '@praveen001/uno-converse';
342
+ *
343
+ * function ChatComponent() {
344
+ * const {
345
+ * allMessages,
346
+ * isStreaming,
347
+ * sendMessage,
348
+ * startNewChat,
349
+ * } = useConversation({
350
+ * namespace: 'my-app',
351
+ * projectName: 'my-project',
352
+ * baseUrl: 'https://my-uno-server.com/api/agent-server',
353
+ * getHeaders: () => ({
354
+ * 'Authorization': `Bearer ${getToken()}`,
355
+ * }),
356
+ * });
357
+ *
358
+ * const handleSend = async (text: string) => {
359
+ * await sendMessage(
360
+ * [{ type: 'message', id: '1', content: text }],
361
+ * {
362
+ * namespace: 'my-app',
363
+ * agentName: 'my-agent',
364
+ * }
365
+ * );
366
+ * };
367
+ *
368
+ * return (
369
+ * <div>
370
+ * {allMessages.map(msg => (
371
+ * <MessageComponent key={msg.message_id} message={msg} />
372
+ * ))}
373
+ * {isStreaming && <LoadingIndicator />}
374
+ * </div>
375
+ * );
376
+ * }
377
+ * ```
378
+ */
379
+ declare function useConversation(options: UseConversationOptions): UseConversationReturn;
380
+
381
+ /**
382
+ * Callback invoked when the conversation state changes
383
+ */
384
+ type OnChangeCallback = (conversation: ConversationMessage) => void;
385
+ /**
386
+ * Processes streaming chunks from the LLM response.
387
+ * Builds up messages incrementally as chunks arrive.
388
+ *
389
+ * @example
390
+ * ```ts
391
+ * const processor = new ChunkProcessor(
392
+ * 'conv-123',
393
+ * 'thread-456',
394
+ * (conversation) => {
395
+ * // Update UI with new conversation state
396
+ * setConversation(conversation);
397
+ * }
398
+ * );
399
+ *
400
+ * // Process incoming chunks
401
+ * processor.processChunk(jsonData);
402
+ *
403
+ * // Get final conversation when done
404
+ * const finalConversation = processor.getConversation();
405
+ * ```
406
+ */
407
+ declare class ChunkProcessor {
408
+ private messages;
409
+ private currentOutputItem;
410
+ private _onChange;
411
+ private conversation;
412
+ constructor(conversationId: string, threadId: string, onChange: OnChangeCallback);
413
+ /**
414
+ * Get all processed messages
415
+ */
416
+ getMessages(): MessageUnion[];
417
+ /**
418
+ * Get the current conversation state
419
+ */
420
+ getConversation(): ConversationMessage;
421
+ private emitChange;
422
+ /**
423
+ * Process a raw JSON chunk from the SSE stream
424
+ */
425
+ processChunk(data: string): void;
426
+ private handleChunk;
427
+ private handleOutputItemAdded;
428
+ private handleContentPartAdded;
429
+ private handleOutputTextDelta;
430
+ private handleReasoningSummaryPartAdded;
431
+ private handleReasoningSummaryTextDelta;
432
+ private handleFunctionCallArgumentsDelta;
433
+ private handleFunctionCallOutput;
434
+ private handleImageGenerationCallPartialImage;
435
+ }
436
+
437
+ /**
438
+ * Options for the SSE stream callbacks
439
+ */
440
+ interface SSEStreamOptions {
441
+ /** Called for each data chunk received */
442
+ onChunk: (data: string) => void;
443
+ /** Called when an error occurs */
444
+ onError?: (error: Error) => void;
445
+ /** Called when the stream completes */
446
+ onComplete?: () => void;
447
+ }
448
+ /**
449
+ * Streams Server-Sent Events (SSE) from a URL.
450
+ * Parses SSE frames and calls onChunk for each data payload.
451
+ *
452
+ * @param url - The URL to stream from
453
+ * @param requestOptions - Fetch request options
454
+ * @param callbacks - SSE event callbacks
455
+ * @param abortSignal - Optional signal to abort the stream
456
+ *
457
+ * @example
458
+ * ```ts
459
+ * await streamSSE(
460
+ * 'https://api.example.com/stream',
461
+ * { method: 'POST', body: JSON.stringify({ message: 'Hello' }) },
462
+ * {
463
+ * onChunk: (data) => console.log('Received:', data),
464
+ * onComplete: () => console.log('Done'),
465
+ * onError: (err) => console.error('Error:', err),
466
+ * }
467
+ * );
468
+ * ```
469
+ */
470
+ declare function streamSSE(url: string, requestOptions: RequestInit, callbacks: SSEStreamOptions, abortSignal?: AbortSignal): Promise<void>;
471
+
472
+ export { ChunkProcessor, ChunkType, ContentType, MessageType, Role, isEasyMessage, isFunctionCallMessage, isFunctionCallOutputMessage, isImageGenerationCallMessage, isInputMessage, isReasoningMessage, streamSSE, useConversation };
473
+ export type { ChunkOutputItemData, ChunkResponseData, ChunkResponseUsage, ChunkRunData, Conversation, ConversationMessage, ConverseConfig, EasyInputContentUnion, EasyMessage, FunctionCallApprovalResponseMessage, FunctionCallMessage, FunctionCallOutputContentUnion, FunctionCallOutputMessage, GetHeadersFn, ImageGenerationCallMessage, InputContentUnion, InputImageContent, InputMessage, InputTextContent, MessageUnion, OnChangeCallback, OutputContentUnion, OutputMessage, OutputMessageUnion, OutputTextContent, ReasoningMessage, ResponseChunk, SSEStreamOptions, SummaryTextContent, Thread, Usage, UseConversationOptions, UseConversationReturn };