@agents24/chat-react 0.1.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.
@@ -0,0 +1,351 @@
1
+ import * as react from 'react';
2
+ import { RefObject, ReactNode, UIEvent } from 'react';
3
+
4
+ type ChatRunStatus = "queued" | "running" | "completed" | "cancelled" | "failed" | string;
5
+ type ChatContextWindow = {
6
+ stage?: string;
7
+ label?: string;
8
+ usedTokens?: number;
9
+ maxTokens?: number;
10
+ [key: string]: unknown;
11
+ };
12
+ type ChatCitation = {
13
+ title: string;
14
+ url: string;
15
+ description: string;
16
+ sourceRef?: string;
17
+ ref?: string;
18
+ firstRef?: string;
19
+ totalSegments?: number;
20
+ rangeRef?: string;
21
+ };
22
+ type ChatAttachment = {
23
+ url?: string;
24
+ filename?: string;
25
+ mediaType?: string;
26
+ contentType?: string;
27
+ [key: string]: unknown;
28
+ };
29
+ type ChatReasoningStep = {
30
+ label: string;
31
+ status: "active" | "complete" | "pending";
32
+ icon?: unknown;
33
+ description?: string;
34
+ citations?: ChatCitation[];
35
+ query?: string;
36
+ sources?: Array<Record<string, unknown>>;
37
+ };
38
+ type ChatToolStatus = "running" | "done" | "error";
39
+ type ChatToolActionKey = "search" | "navigate" | "resolve" | "read" | "context" | "links" | "work";
40
+ type ChatTextBlock = {
41
+ id: string;
42
+ kind: "text";
43
+ content: string;
44
+ };
45
+ type ChatToolBlock = {
46
+ id: string;
47
+ kind: "tool";
48
+ title: string;
49
+ actionLabel: string;
50
+ actionKey: ChatToolActionKey;
51
+ status: ChatToolStatus;
52
+ detail?: string;
53
+ };
54
+ type ChatToolGroupBlock = {
55
+ id: string;
56
+ kind: "tool_group";
57
+ title: string;
58
+ status: ChatToolStatus;
59
+ tools: ChatToolBlock[];
60
+ };
61
+ type ChatMessageBlock = ChatTextBlock | ChatToolBlock | ChatToolGroupBlock;
62
+ type ChatMessage = {
63
+ id: string;
64
+ role: "user" | "assistant";
65
+ runId?: string | null;
66
+ content: string;
67
+ createdAt: Date;
68
+ isFinal?: boolean;
69
+ blocks?: ChatMessageBlock[];
70
+ attachments?: ChatAttachment[];
71
+ citations?: ChatCitation[];
72
+ reasoningSteps?: ChatReasoningStep[];
73
+ thinkingDurationMs?: number;
74
+ liked?: boolean;
75
+ disliked?: boolean;
76
+ messageIndex?: number;
77
+ isVoice?: boolean;
78
+ };
79
+ type ChatRuntimeEvent = {
80
+ version?: string;
81
+ seq?: number;
82
+ ts?: string;
83
+ event: string;
84
+ run_id?: string;
85
+ stage?: string;
86
+ payload?: Record<string, unknown>;
87
+ diagnostics?: Array<Record<string, unknown>>;
88
+ };
89
+ type ChatActiveRun = {
90
+ run_id?: string | null;
91
+ status?: string | null;
92
+ started_at?: string | null;
93
+ created_at?: string | null;
94
+ };
95
+ type ChatThreadSummary = {
96
+ id: string;
97
+ title?: string | null;
98
+ status?: string | null;
99
+ surface?: string | null;
100
+ agent_id?: string | null;
101
+ last_run_id?: string | null;
102
+ last_run_status?: string | null;
103
+ active_run?: ChatActiveRun | null;
104
+ activeRun?: ChatActiveRun | null;
105
+ updated_at?: string | null;
106
+ last_activity_at?: string | null;
107
+ created_at?: string | null;
108
+ messages?: ChatMessage[];
109
+ isHydrated?: boolean;
110
+ hasOlderTurns?: boolean;
111
+ nextBeforeTurnIndex?: number | null;
112
+ lastRunId?: string | null;
113
+ lastRunStatus?: string | null;
114
+ lastEventSeq?: number | null;
115
+ isRunning?: boolean;
116
+ [key: string]: unknown;
117
+ };
118
+ type ChatThreadTurn = {
119
+ id?: string;
120
+ run_id?: string;
121
+ turn_index?: number;
122
+ status?: string | null;
123
+ user_input_text?: string | null;
124
+ assistant_output_text?: string | null;
125
+ final_output?: unknown;
126
+ response_blocks?: Array<Record<string, unknown>>;
127
+ run_events?: ChatRuntimeEvent[];
128
+ context_window?: unknown;
129
+ attachments?: Array<Record<string, unknown>>;
130
+ created_at?: string | null;
131
+ completed_at?: string | null;
132
+ };
133
+ type ChatThreadDetail = ChatThreadSummary & {
134
+ turns?: ChatThreadTurn[];
135
+ context_window?: unknown;
136
+ paging?: {
137
+ has_more?: boolean;
138
+ next_before_turn_index?: number | null;
139
+ };
140
+ };
141
+ type ChatTransport = {
142
+ listThreads: (input?: {
143
+ signal?: AbortSignal;
144
+ }) => Promise<{
145
+ items: ChatThreadSummary[];
146
+ total?: number;
147
+ }>;
148
+ getThread: (input: {
149
+ threadId: string;
150
+ limit?: number;
151
+ beforeTurnIndex?: number | null;
152
+ signal?: AbortSignal;
153
+ }) => Promise<ChatThreadDetail>;
154
+ streamMessage: (input: {
155
+ text: string;
156
+ threadId?: string | null;
157
+ files?: ChatAttachment[];
158
+ signal?: AbortSignal;
159
+ }, onEvent: (event: ChatRuntimeEvent) => void | Promise<void>) => Promise<ChatStreamResult>;
160
+ attachRun: (input: {
161
+ runId: string;
162
+ signal?: AbortSignal;
163
+ }, onEvent: (event: ChatRuntimeEvent) => void | Promise<void>) => Promise<ChatStreamResult>;
164
+ cancelRun: (input: {
165
+ runId: string;
166
+ assistantOutputText?: string;
167
+ }) => Promise<{
168
+ run_id?: string;
169
+ status?: string;
170
+ }>;
171
+ deleteThread?: (input: {
172
+ threadId: string;
173
+ }) => Promise<{
174
+ deleted?: boolean;
175
+ }>;
176
+ uploadAttachment?: (input: {
177
+ file: File;
178
+ signal?: AbortSignal;
179
+ }) => Promise<ChatAttachment>;
180
+ };
181
+ type ChatStreamResult = {
182
+ threadId?: string | null;
183
+ runId?: string | null;
184
+ };
185
+ type StoredChatThread = ChatThreadSummary & {
186
+ messages: ChatMessage[];
187
+ isHydrated?: boolean;
188
+ hasOlderTurns?: boolean;
189
+ nextBeforeTurnIndex?: number | null;
190
+ };
191
+ type ChatStorageAdapter = {
192
+ listThreads: () => StoredChatThread[];
193
+ setThreads: (threads: StoredChatThread[]) => void;
194
+ getThread: (threadId: string) => StoredChatThread | undefined;
195
+ upsertThread: (thread: StoredChatThread) => void;
196
+ deleteThread?: (threadId: string) => void;
197
+ getActiveThreadId?: () => string | null;
198
+ setActiveThreadId?: (threadId: string | null) => void;
199
+ };
200
+ type ChatController = {
201
+ messages: ChatMessage[];
202
+ streamingContent: string;
203
+ streamingMessageId: string | null;
204
+ contextStatus: ChatContextWindow | null;
205
+ currentReasoning: ChatReasoningStep[];
206
+ isLoading: boolean;
207
+ isLoadingHistory: boolean;
208
+ isLoadingOlder: boolean;
209
+ hasOlderTurns: boolean;
210
+ liked: Record<string, boolean>;
211
+ disliked: Record<string, boolean>;
212
+ copiedMessageId: string | null;
213
+ lastThinkingDurationMs: number | null;
214
+ activeRunId: string | null;
215
+ handleSubmit: (message: {
216
+ text: string;
217
+ files?: ChatAttachment[];
218
+ }) => Promise<void>;
219
+ handleStop: () => void;
220
+ handleCopy: (content: string, messageId: string) => void;
221
+ handleLike: (msg: ChatMessage) => Promise<void>;
222
+ handleDislike: (msg: ChatMessage) => Promise<void>;
223
+ handleRetry: (msg: ChatMessage) => Promise<void>;
224
+ handleSourceClick: (citations: ChatMessage["citations"]) => void;
225
+ upsertLiveVoiceMessage: (input: {
226
+ role: "user" | "assistant";
227
+ content: string;
228
+ isFinal?: boolean;
229
+ citations?: ChatCitation[];
230
+ reasoningSteps?: ChatReasoningStep[];
231
+ }) => void;
232
+ loadOlderTurns: () => Promise<void>;
233
+ refresh: () => Promise<void>;
234
+ textareaRef: RefObject<HTMLTextAreaElement | null>;
235
+ };
236
+
237
+ type UseAgents24ChatControllerOptions = {
238
+ transport: ChatTransport;
239
+ storage: ChatStorageAdapter;
240
+ activeThreadId?: string | null;
241
+ pageSize?: number;
242
+ storageKey?: unknown;
243
+ createId?: () => string;
244
+ onActiveThreadIdChange?: (threadId: string | null) => void;
245
+ onSourceClick?: (citations: ChatMessage["citations"]) => void;
246
+ onStreamErrorMessage?: (error: unknown) => string;
247
+ };
248
+ declare function useAgents24ChatController({ transport, storage, activeThreadId: controlledActiveThreadId, pageSize, storageKey, createId, onActiveThreadIdChange, onSourceClick, onStreamErrorMessage, }: UseAgents24ChatControllerOptions): ChatController;
249
+
250
+ declare const DEFAULT_THREAD_PAGE_SIZE = 5;
251
+ declare const isRunningThreadStatus: (status?: string | null) => boolean;
252
+ declare const createChatId: () => string;
253
+ declare const titleFromMessage: (text: string, files?: Array<{
254
+ filename?: string;
255
+ }>) => string;
256
+ declare const threadActivityDate: (thread: ChatThreadSummary) => string;
257
+ declare const assistantTextFromBlocks: (blocks?: Array<Record<string, unknown>>) => string;
258
+ declare const textFromFinalOutput: (value: unknown) => string;
259
+ declare const renderBlocksFromResponseBlocks: (blocks?: Array<Record<string, unknown>>, fallbackText?: string) => ChatMessageBlock[];
260
+ declare const mergeReasoningSteps: (steps?: ChatReasoningStep[], options?: {
261
+ finalize?: boolean;
262
+ }) => ChatReasoningStep[];
263
+ declare const reasoningStepsFromBlocks: (blocks?: Array<Record<string, unknown>>) => ChatReasoningStep[];
264
+ declare const threadDetailToMessages: (thread: ChatThreadDetail) => ChatMessage[];
265
+ declare const threadPaging: (thread: ChatThreadDetail) => {
266
+ hasOlderTurns: boolean;
267
+ nextBeforeTurnIndex: number | null;
268
+ };
269
+ declare const latestContextWindowFromThread: (thread: ChatThreadDetail) => ChatContextWindow | null;
270
+ declare const activeRunIdFromThread: (thread?: Partial<ChatThreadSummary> | null) => string | null;
271
+ declare const hasUnfinishedAssistantMessage: (messages?: ChatMessage[]) => boolean;
272
+ declare const hasStaleUnfinishedAssistantCache: (thread?: (Partial<ChatThreadSummary> & {
273
+ messages?: ChatMessage[];
274
+ }) | null) => boolean;
275
+ declare const activeRunIdFromThreadDetail: (thread?: ChatThreadDetail | null) => string | null;
276
+
277
+ declare const parseSseBlock: (block: string) => ChatRuntimeEvent | null;
278
+ declare const consumeSseResponse: (response: Response, onEvent: (event: ChatRuntimeEvent) => void | Promise<void>) => Promise<ChatStreamResult>;
279
+
280
+ type MaybePromise<T> = T | Promise<T>;
281
+ type FetchChatTransportRoutes = {
282
+ listThreads: () => string;
283
+ getThread: (input: {
284
+ threadId: string;
285
+ limit?: number;
286
+ beforeTurnIndex?: number | null;
287
+ }) => string;
288
+ streamMessage: () => string;
289
+ attachRun: (input: {
290
+ runId: string;
291
+ }) => string;
292
+ cancelRun: (input: {
293
+ runId: string;
294
+ }) => string;
295
+ deleteThread?: (input: {
296
+ threadId: string;
297
+ }) => string;
298
+ };
299
+ type FetchChatTransportOptions = {
300
+ routes: FetchChatTransportRoutes;
301
+ fetchImpl?: typeof fetch;
302
+ headers?: () => MaybePromise<HeadersInit>;
303
+ streamBody?: (input: {
304
+ text: string;
305
+ threadId?: string | null;
306
+ files?: ChatAttachment[];
307
+ }) => unknown;
308
+ };
309
+ declare const createFetchChatTransport: ({ routes, fetchImpl, headers, streamBody, }: FetchChatTransportOptions) => ChatTransport;
310
+
311
+ declare const isScrollable: (element: Pick<HTMLDivElement, "scrollHeight" | "clientHeight">) => boolean;
312
+ declare const isAtTimelineLatestEdge: (element: Pick<HTMLDivElement, "scrollHeight" | "clientHeight" | "scrollTop">, isTopOrigin: boolean) => boolean;
313
+ declare const shouldPrefetchOlder: (element: Pick<HTMLDivElement, "scrollHeight" | "clientHeight" | "scrollTop">) => boolean;
314
+ declare const getLatestScrollTop: (element: Pick<HTMLDivElement, "scrollHeight" | "clientHeight">, isTopOrigin: boolean) => number;
315
+ type UseLatestThreadViewportOptions = {
316
+ itemCount: number;
317
+ hasOlder: boolean;
318
+ isLoadingOlder: boolean;
319
+ onLoadOlder: () => Promise<void> | void;
320
+ activeStreamKey?: string | null;
321
+ shouldAutoFollow?: boolean;
322
+ topOriginMaxItems?: number;
323
+ };
324
+ type LatestThreadViewportState = {
325
+ scrollContainerRef: RefObject<HTMLDivElement | null>;
326
+ isAtLatest: boolean;
327
+ isTopOrigin: boolean;
328
+ shouldShowLatestButton: boolean;
329
+ handleScroll: (event: UIEvent<HTMLDivElement>) => void;
330
+ scrollToLatest: () => void;
331
+ preserveIntrinsicResize: () => void;
332
+ };
333
+ declare function useLatestThreadViewport({ itemCount, hasOlder, isLoadingOlder, onLoadOlder, activeStreamKey, shouldAutoFollow, topOriginMaxItems, }: UseLatestThreadViewportOptions): LatestThreadViewportState;
334
+ type LatestThreadViewportRenderContext = {
335
+ preserveIntrinsicResize: () => void;
336
+ isTopOrigin: boolean;
337
+ };
338
+ type LatestThreadViewportProps<T> = {
339
+ items: T[];
340
+ hasOlder: boolean;
341
+ isLoadingOlder: boolean;
342
+ onLoadOlder: () => Promise<void> | void;
343
+ activeStreamKey?: string | null;
344
+ className?: string;
345
+ latestSpacer?: ReactNode;
346
+ trailingSpacer?: ReactNode;
347
+ renderItem: (item: T, context: LatestThreadViewportRenderContext) => ReactNode;
348
+ };
349
+ declare function LatestThreadViewport<T>({ items, hasOlder, isLoadingOlder, onLoadOlder, activeStreamKey, className, latestSpacer, trailingSpacer, renderItem, }: LatestThreadViewportProps<T>): react.JSX.Element;
350
+
351
+ export { type ChatActiveRun, type ChatAttachment, type ChatCitation, type ChatContextWindow, type ChatController, type ChatMessage, type ChatMessageBlock, type ChatReasoningStep, type ChatRunStatus, type ChatRuntimeEvent, type ChatStorageAdapter, type ChatStreamResult, type ChatTextBlock, type ChatThreadDetail, type ChatThreadSummary, type ChatThreadTurn, type ChatToolActionKey, type ChatToolBlock, type ChatToolGroupBlock, type ChatToolStatus, type ChatTransport, DEFAULT_THREAD_PAGE_SIZE, type FetchChatTransportOptions, type FetchChatTransportRoutes, LatestThreadViewport, type LatestThreadViewportProps, type LatestThreadViewportRenderContext, type LatestThreadViewportState, type StoredChatThread, type UseAgents24ChatControllerOptions, type UseLatestThreadViewportOptions, activeRunIdFromThread, activeRunIdFromThreadDetail, assistantTextFromBlocks, consumeSseResponse, createChatId, createFetchChatTransport, getLatestScrollTop, hasStaleUnfinishedAssistantCache, hasUnfinishedAssistantMessage, isAtTimelineLatestEdge, isRunningThreadStatus, isScrollable, latestContextWindowFromThread, mergeReasoningSteps, parseSseBlock, reasoningStepsFromBlocks, renderBlocksFromResponseBlocks, shouldPrefetchOlder, textFromFinalOutput, threadActivityDate, threadDetailToMessages, threadPaging, titleFromMessage, useAgents24ChatController, useLatestThreadViewport };