@usecrow/client 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,424 @@
1
+ /**
2
+ * Core types for @usecrow/client
3
+ */
4
+ interface CrowClientConfig {
5
+ /** Your Crow product ID from the dashboard */
6
+ productId: string;
7
+ /** API URL (defaults to https://api.usecrow.org) */
8
+ apiUrl?: string;
9
+ /** Default model to use */
10
+ model?: string;
11
+ }
12
+ interface IdentifyOptions {
13
+ /** JWT token from your backend for user verification */
14
+ token: string;
15
+ /** Optional user display name */
16
+ name?: string;
17
+ /** Optional user email */
18
+ email?: string;
19
+ /** Any additional metadata */
20
+ [key: string]: unknown;
21
+ }
22
+ interface IdentityState {
23
+ token: string | null;
24
+ metadata: Record<string, unknown>;
25
+ isVerified: boolean;
26
+ }
27
+ interface Message {
28
+ id: string;
29
+ content: string;
30
+ role: 'user' | 'assistant';
31
+ timestamp: Date;
32
+ /** Citations from knowledge base */
33
+ citations?: Citation[];
34
+ /** Claude's reasoning/thinking trace */
35
+ thinking?: string;
36
+ /** Whether thinking is complete */
37
+ thinkingComplete?: boolean;
38
+ }
39
+ interface Citation {
40
+ document_id: string;
41
+ filename: string;
42
+ similarity?: number;
43
+ image_url?: string;
44
+ page?: number;
45
+ }
46
+ type StreamEvent = {
47
+ type: 'content';
48
+ text: string;
49
+ accumulated: string;
50
+ } | {
51
+ type: 'thinking';
52
+ content: string;
53
+ } | {
54
+ type: 'thinking_complete';
55
+ } | {
56
+ type: 'citations';
57
+ citations: Citation[];
58
+ } | {
59
+ type: 'tool_call_start';
60
+ toolName: string;
61
+ arguments: Record<string, unknown>;
62
+ } | {
63
+ type: 'tool_call_complete';
64
+ toolName: string;
65
+ success: boolean;
66
+ } | {
67
+ type: 'client_tool_call';
68
+ toolName: string;
69
+ arguments: Record<string, unknown>;
70
+ } | {
71
+ type: 'workflow_started';
72
+ name: string;
73
+ todos: WorkflowTodo[];
74
+ } | {
75
+ type: 'workflow_todo_updated';
76
+ todoId: string;
77
+ status: 'pending' | 'completed';
78
+ } | {
79
+ type: 'workflow_ended';
80
+ } | {
81
+ type: 'workflow_complete_prompt';
82
+ } | {
83
+ type: 'verification_status';
84
+ isVerified: boolean;
85
+ } | {
86
+ type: 'conversation_id';
87
+ conversationId: string;
88
+ } | {
89
+ type: 'error';
90
+ message: string;
91
+ } | {
92
+ type: 'done';
93
+ };
94
+ type ToolHandler = (args: Record<string, unknown>) => Promise<ToolResult> | ToolResult;
95
+ interface ToolResult {
96
+ status: 'success' | 'error';
97
+ data?: unknown;
98
+ error?: string;
99
+ }
100
+ type ToolHandlers = Record<string, ToolHandler>;
101
+ interface Conversation {
102
+ id: string;
103
+ name: string | null;
104
+ created_at: string;
105
+ updated_at: string;
106
+ }
107
+ interface WorkflowTodo {
108
+ id: string;
109
+ text: string;
110
+ status: 'pending' | 'completed';
111
+ }
112
+ interface ActiveWorkflow {
113
+ name: string;
114
+ todos: WorkflowTodo[];
115
+ isComplete?: boolean;
116
+ }
117
+ interface ContextData {
118
+ /** Current page URL or path */
119
+ page?: string;
120
+ /** Custom context data */
121
+ [key: string]: unknown;
122
+ }
123
+ interface CrowEventCallbacks {
124
+ onMessage?: (message: Message) => void;
125
+ onMessageUpdate?: (messageId: string, updates: Partial<Message>) => void;
126
+ onToolCall?: (event: StreamEvent & {
127
+ type: 'tool_call_start' | 'tool_call_complete' | 'client_tool_call';
128
+ }) => void;
129
+ onWorkflow?: (event: StreamEvent & {
130
+ type: 'workflow_started' | 'workflow_todo_updated' | 'workflow_ended' | 'workflow_complete_prompt';
131
+ }) => void;
132
+ onVerificationStatus?: (isVerified: boolean) => void;
133
+ onError?: (error: Error) => void;
134
+ }
135
+
136
+ /**
137
+ * CrowClient - Main headless client for Crow AI agents
138
+ */
139
+
140
+ declare class CrowClient {
141
+ private config;
142
+ private identity;
143
+ private tools;
144
+ private conversations;
145
+ private context;
146
+ private abortController;
147
+ private callbacks;
148
+ private _messages;
149
+ private messageListeners;
150
+ private _isLoading;
151
+ private loadingListeners;
152
+ constructor(config: CrowClientConfig);
153
+ /**
154
+ * Get current product ID
155
+ */
156
+ get productId(): string;
157
+ /**
158
+ * Get current API URL
159
+ */
160
+ get apiUrl(): string;
161
+ /**
162
+ * Get/set current model
163
+ */
164
+ get model(): string;
165
+ set model(value: string);
166
+ /**
167
+ * Register event callbacks
168
+ */
169
+ on(callbacks: CrowEventCallbacks): void;
170
+ /**
171
+ * Identify the current user with a JWT token
172
+ */
173
+ identify(options: IdentifyOptions): void;
174
+ /**
175
+ * Reset user identity (call on logout)
176
+ */
177
+ resetUser(): void;
178
+ /**
179
+ * Check if user is identified
180
+ */
181
+ isIdentified(): boolean;
182
+ /**
183
+ * Check if user is verified by server
184
+ */
185
+ isVerified(): boolean;
186
+ /**
187
+ * Register client-side tool handlers
188
+ */
189
+ registerTools(tools: ToolHandlers): void;
190
+ /**
191
+ * Unregister a tool handler
192
+ */
193
+ unregisterTool(name: string): void;
194
+ /**
195
+ * Get list of registered tool names
196
+ */
197
+ getRegisteredTools(): string[];
198
+ /**
199
+ * Set context data to be sent with messages
200
+ */
201
+ setContext(data: ContextData): void;
202
+ /**
203
+ * Clear context data
204
+ */
205
+ clearContext(): void;
206
+ /**
207
+ * Get current messages
208
+ */
209
+ get messages(): Message[];
210
+ /**
211
+ * Check if currently loading/streaming
212
+ */
213
+ get isLoading(): boolean;
214
+ /**
215
+ * Subscribe to message changes
216
+ */
217
+ onMessages(callback: (messages: Message[]) => void): () => void;
218
+ /**
219
+ * Subscribe to loading state changes
220
+ */
221
+ onLoading(callback: (isLoading: boolean) => void): () => void;
222
+ /**
223
+ * Clear all messages and start new conversation
224
+ */
225
+ clearMessages(): void;
226
+ /**
227
+ * Load messages from history
228
+ */
229
+ loadMessages(messages: Message[]): void;
230
+ private notifyMessages;
231
+ private setLoading;
232
+ private addMessage;
233
+ private updateMessage;
234
+ private generateMessageId;
235
+ /**
236
+ * Get current conversation ID
237
+ */
238
+ get conversationId(): string | null;
239
+ /**
240
+ * Set current conversation ID
241
+ */
242
+ set conversationId(id: string | null);
243
+ /**
244
+ * Get list of conversations for verified user
245
+ */
246
+ getConversations(): Promise<Conversation[]>;
247
+ /**
248
+ * Load conversation history
249
+ */
250
+ loadHistory(conversationId: string): Promise<Message[]>;
251
+ /**
252
+ * Switch to a different conversation
253
+ */
254
+ switchConversation(conversationId: string): Promise<void>;
255
+ /**
256
+ * Send a message and receive streaming response
257
+ * Returns an async generator of stream events
258
+ */
259
+ sendMessage(content: string): AsyncGenerator<StreamEvent>;
260
+ /**
261
+ * Send a message and wait for complete response (non-streaming)
262
+ */
263
+ send(content: string): Promise<Message | null>;
264
+ /**
265
+ * Stop current generation
266
+ */
267
+ stop(): void;
268
+ /**
269
+ * Destroy the client and clean up resources
270
+ */
271
+ destroy(): void;
272
+ }
273
+
274
+ /**
275
+ * Identity management for user authentication
276
+ */
277
+
278
+ declare class IdentityManager {
279
+ private state;
280
+ private listeners;
281
+ /**
282
+ * Identify the current user with a JWT token
283
+ */
284
+ identify(options: IdentifyOptions): void;
285
+ /**
286
+ * Update verification status (called when server confirms)
287
+ */
288
+ setVerified(isVerified: boolean): void;
289
+ /**
290
+ * Reset user identity (call on logout)
291
+ */
292
+ reset(): void;
293
+ /**
294
+ * Get current identity token
295
+ */
296
+ getToken(): string | null;
297
+ /**
298
+ * Get current identity state
299
+ */
300
+ getState(): IdentityState;
301
+ /**
302
+ * Check if user is identified
303
+ */
304
+ isIdentified(): boolean;
305
+ /**
306
+ * Check if user is verified
307
+ */
308
+ isVerified(): boolean;
309
+ /**
310
+ * Subscribe to identity changes
311
+ */
312
+ subscribe(callback: (state: IdentityState) => void): () => void;
313
+ private notify;
314
+ }
315
+
316
+ /**
317
+ * Client-side tool management
318
+ */
319
+
320
+ declare class ToolManager {
321
+ private handlers;
322
+ /**
323
+ * Register client-side tool handlers
324
+ */
325
+ register(tools: ToolHandlers): void;
326
+ /**
327
+ * Unregister a tool handler
328
+ */
329
+ unregister(name: string): void;
330
+ /**
331
+ * Check if a tool is registered
332
+ */
333
+ has(name: string): boolean;
334
+ /**
335
+ * Get all registered tool names
336
+ */
337
+ getRegisteredTools(): string[];
338
+ /**
339
+ * Execute a client-side tool
340
+ */
341
+ execute(name: string, args: Record<string, unknown>): Promise<ToolResult>;
342
+ }
343
+
344
+ /**
345
+ * Conversation management
346
+ */
347
+
348
+ declare class ConversationManager {
349
+ private productId;
350
+ private apiUrl;
351
+ private currentId;
352
+ constructor(productId: string, apiUrl: string);
353
+ /**
354
+ * Get localStorage key for this product
355
+ */
356
+ private getStorageKey;
357
+ /**
358
+ * Load conversation ID from localStorage
359
+ */
360
+ private loadFromStorage;
361
+ /**
362
+ * Save conversation ID to localStorage
363
+ */
364
+ private saveToStorage;
365
+ /**
366
+ * Clear conversation ID from localStorage
367
+ */
368
+ private clearStorage;
369
+ /**
370
+ * Get current conversation ID
371
+ */
372
+ getCurrentId(): string | null;
373
+ /**
374
+ * Set current conversation ID
375
+ */
376
+ setCurrentId(id: string | null): void;
377
+ /**
378
+ * Check if there's a restored conversation
379
+ */
380
+ hasRestoredConversation(): boolean;
381
+ /**
382
+ * Clear current conversation (start new)
383
+ */
384
+ clear(): void;
385
+ /**
386
+ * Fetch list of conversations for verified user
387
+ */
388
+ getConversations(identityToken: string): Promise<Conversation[]>;
389
+ /**
390
+ * Load conversation history for verified user
391
+ */
392
+ loadHistory(conversationId: string, identityToken: string): Promise<Message[]>;
393
+ /**
394
+ * Load conversation history for anonymous user
395
+ */
396
+ loadAnonymousHistory(conversationId: string): Promise<Message[]>;
397
+ /**
398
+ * Parse history messages from API format
399
+ */
400
+ private parseHistoryMessages;
401
+ /**
402
+ * Parse structured content (with thinking/text blocks) and extract just text
403
+ */
404
+ private parseContent;
405
+ }
406
+
407
+ /**
408
+ * SSE streaming utilities for parsing server-sent events
409
+ */
410
+
411
+ /**
412
+ * Parse a single SSE data line into a StreamEvent
413
+ */
414
+ declare function parseSSEData(data: string): StreamEvent | null;
415
+ /**
416
+ * Parse SSE chunk into lines and extract data
417
+ */
418
+ declare function parseSSEChunk(chunk: string): Generator<string>;
419
+ /**
420
+ * Create an async generator that streams events from a Response
421
+ */
422
+ declare function streamResponse(response: Response, signal?: AbortSignal): AsyncGenerator<StreamEvent>;
423
+
424
+ export { type ActiveWorkflow, type Citation, type ContextData, type Conversation, ConversationManager, CrowClient, type CrowClientConfig, type CrowEventCallbacks, type IdentifyOptions, IdentityManager, type IdentityState, type Message, type StreamEvent, type ToolHandler, type ToolHandlers, ToolManager, type ToolResult, type WorkflowTodo, parseSSEChunk, parseSSEData, streamResponse };