-
Notifications
You must be signed in to change notification settings - Fork 78
/
index.d.ts
936 lines (936 loc) · 25.8 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/**
* The main Claude API client class.
* @typedef Claude
* @class
* @classdesc Creates an instance of the Claude API client.
*/
export class Claude {
/**
* A UUID string
* @typedef UUID
* @example "222aa20a-bc79-48d2-8f6d-c819a1b5eaed"
*/
/**
* Create a new Claude API client instance.
* @param {Object} options - Options
* @param {string} options.sessionKey - Claude session key
* @param {string|function} [options.proxy] - Proxy URL or proxy function
* @param {function} [options.fetch] - Fetch function
* @example
* const claude = new Claude({
* sessionKey: 'sk-ant-sid01-*****',
* fetch: globalThis.fetch
* })
*
* await claude.init();
* claude.sendMessage('Hello world').then(console.log)
*/
constructor({ sessionKey, proxy, fetch }: {
sessionKey: string;
proxy?: string | Function;
fetch?: Function;
});
/**
* If the Claude client has initialized yet (call `init()` if you haven't and this is false)
* @property {boolean}
*/
ready: boolean;
/**
* A proxy function/string to connect via
* @property {({endpoint: string, options: Object}) => {endpoint: string, options: Object} | string}
*/
proxy: Function;
/**
* A fetch function, defaults to globalThis.fetch
* @property {Function}
*/
fetch: Function;
/**
* The session key string (from the cookie)
* @property {string}
*/
sessionKey: string;
/**
* Get available Claude models.
* @returns {string[]} Array of model names
*/
models(): string[];
/**
* Get total token count for a Claude model.
* @param {string} [model] - Claude model name
* @returns {number} Total token count
*/
totalTokens(model?: string): number;
/**
* Get the default Claude model.
* @returns {string} Default model name
*/
defaultModel(): string;
/**
* A partial or total completion for a message.
* @typedef MessageStreamChunk
* @property {String} completion The markdown text completion for this response
* @property {String | null} stop_reason The reason for the response stop (if any)
* @property {String} model The model used
* @property {String} stop The string at which Claude stopped responding at, e.g. "\n\nHuman:"
* @property {String} log_id A logging ID
* @property {Object} messageLimit If you're within the message limit
* @param {String} messageLimit.type The type of message limit ("within_limit")
*/
/**
* Send a message to a new or existing conversation.
* @param {string} message - Initial message
* @param {SendMessageParams} [params] - Additional parameters
* @param {string} [params.conversation] - Existing conversation ID
* @param {boolean} [params.temporary=true] - Delete after getting response
* @returns {Promise<MessageStreamChunk>} Result message
*/
sendMessage(message: string, { conversation, temporary, ...params }?: SendMessageParams): Promise<{
/**
* The markdown text completion for this response
*/
completion: string;
/**
* The reason for the response stop (if any)
*/
stop_reason: string | null;
/**
* The model used
*/
model: string;
/**
* The string at which Claude stopped responding at, e.g. "\n\nHuman:"
*/
stop: string;
/**
* A logging ID
*/
log_id: string;
/**
* If you're within the message limit
*/
messageLimit: any;
}>;
/**
* Make an API request.
* @param {string} endpoint - API endpoint
* @param {Object} options - Request options
* @returns {Promise<Response>} Fetch response
* @example
* await claude.request('/api/organizations').then(r => r.json())
*/
request(endpoint: string, options: any): Promise<Response>;
/**
* Initialize the client.
* @async
* @returns {Promise<void>} Void
*/
init(): Promise<void>;
organizationId: string;
recent_conversations: Conversation[];
/**
* An organization
* @typedef Organization
* @property {String} join_token A token
* @property {String} name The organization name
* @property {String} uuid The organization UUID
* @property {String} created_at The organization creation date
* @property {String} updated_at The organization update date
* @property {String[]} capabilities What the organization can do
* @property {Object} settings The organization's settings
* @property {Array} active_flags Organization's flags (none that I've found)
*/
/**
* Get the organizations list.
* @async
* @returns {Promise<Organization[]>} A list of organizations
* @example
* await claude.getOrganizations().then(organizations => {
* console.log('Users organization name is:', organizations[0].name)
* })
*/
getOrganizations(): Promise<{
/**
* A token
*/
join_token: string;
/**
* The organization name
*/
name: string;
/**
* The organization UUID
*/
uuid: string;
/**
* The organization creation date
*/
created_at: string;
/**
* The organization update date
*/
updated_at: string;
/**
* What the organization can do
*/
capabilities: string[];
/**
* The organization's settings
*/
settings: any;
/**
* Organization's flags (none that I've found)
*/
active_flags: any[];
}[]>;
/**
* Delete all conversations
* @async
* @returns {Promise<Response[]>} An array of responses for the DELETE requests
* @example
* await claude.clearConversations();
* console.assert(await claude.getConversations().length === 0);
*/
clearConversations(): Promise<Response[]>;
/**
* @callback doneCallback
* @param {MessageStreamChunk} a The completed response
*/
/**
* @callback progressCallback
* @param {MessageStreamChunk} a The response in progress
*/
/**
* Start a new conversation
* @param {String} message The message to send to start the conversation
* @param {SendMessageParams} [params={}] Message params passed to Conversation.sendMessage
* @returns {Promise<Conversation>}
* @async
* @example
* const conversation = await claude.startConversation("Hello! How are you?")
* console.log(await conversation.getInfo());
*/
startConversation(message: string, params?: SendMessageParams): Promise<Conversation>;
/**
* Get a conversation by its ID
* @param {UUID} id The uuid of the conversation (Conversation.uuid or Conversation.conversationId)
* @async
* @returns {Conversation | null} The conversation
* @example
* const conversation = await claude.getConversation("222aa20a-bc79-48d2-8f6d-c819a1b5eaed");
*/
getConversation(id: any): Conversation | null;
/**
* Get all conversations
* @async
* @returns {Promise<Conversation[]>} A list of conversations
* @example
* console.log(`You have ${await claude.getConversations().length} conversations:`);
*/
getConversations(): Promise<Conversation[]>;
/**
* The response from uploading a file (an attachment)
* @typedef Attachment
* @property {String} file_name The file name
* @property {String} file_type The file's mime type
* @property {Number} file_size The file size in bytes
* @property {String} extracted_content The contents of the file that were extracted
* @property {Number | null} [totalPages] The total pages of the document
*/
/**
* Extract the contents of a file
* @param {File} file A JS File (like) object to upload.
* @async
* @returns {Promise<Attachment>}
* @example
* const file = await claude.uploadFile(
* new File(["test"], "test.txt", { type: "text/plain" }
* );
* console.log(await claude.sendMessage("What's the contents of test.txt?", {
* attachments: [file]
* }))
*/
uploadFile(file: File): Promise<{
/**
* The file name
*/
file_name: string;
/**
* The file's mime type
*/
file_type: string;
/**
* The file size in bytes
*/
file_size: number;
/**
* The contents of the file that were extracted
*/
extracted_content: string;
/**
* The total pages of the document
*/
totalPages?: number | null;
}>;
}
/**
* @typedef SendMessageParams
* @property {Boolean} [retry=false] Whether to retry the most recent message in the conversation instead of sending a new one
* @property {String} [timezone="America/New_York"] The timezone
* @property {Attachment[]} [attachments=[]] Attachments
* @property {doneCallback} [done] Callback when done receiving the message response
* @property {progressCallback} [progress] Callback on message response progress
* @property {string} [model=claude.defaultModel()] The model to use
*/
/**
* A Claude conversation instance.
* @class
* @typedef Conversation
* @classdesc Represents an active Claude conversation.
*/
export class Conversation {
/**
* Create a Conversation instance.
* @param {Claude} claude - Claude client instance
* @param {Object} options - Options
* @param {String} options.conversationId - Conversation ID
* @param {String} [options.name] - Conversation name
* @param {String} [options.summary] - Conversation summary
* @param {String} [options.created_at] - Conversation created at
* @param {String} [options.updated_at] - Conversation updated at
* @param {String} [options.model] - Claude model
*/
constructor(claude: Claude, { model, conversationId, name, summary, created_at, updated_at }: {
conversationId: string;
name?: string;
summary?: string;
created_at?: string;
updated_at?: string;
model?: string;
});
/**
* The conversation ID
* @property {string}
*/
conversationId: string;
/**
* The conversation name
* @property {string}
*/
name: any;
/**
* The conversation summary (usually empty)
* @property {string}
*/
summary: any;
/**
* The conversation created at
* @property {string}
*/
created_at: any;
/**
* The conversation updated at
* @property {string}
*/
updated_at: any;
/**
* The Claude client
* @property {Claude}
*/
claude: any;
/**
* The request function (from parent claude instance)
* @property {(url: string, options: object) => Response}
*/
request: (endpoint: string, options: any) => Promise<Response>;
/**
* The current model
* @property {string}
*/
model: any;
/**
* If the Claude client has initialized yet (call `init()` if you haven't and this is false)
* @property {boolean}
*/
ready: any;
/**
* A proxy function/string to connect via
* @property {({endpoint: string, options: Object}) => {endpoint: string, options: Object} | string}
*/
proxy: any;
/**
* A fetch function, defaults to globalThis.fetch
* @property {Function}
*/
fetch: any;
/**
* Convert the conversation to a JSON object
* @returns {Conversation} The serializable object
*/
toJSON(): Conversation;
/**
* Retry the last message in the conversation
* @param {SendMessageParams} [params={}]
* @returns {Promise<MessageStreamChunk>}
*/
retry(params?: SendMessageParams): Promise<{
/**
* The markdown text completion for this response
*/
completion: string;
/**
* The reason for the response stop (if any)
*/
stop_reason: string;
/**
* The model used
*/
model: string;
/**
* The string at which Claude stopped responding at, e.g. "\n\nHuman:"
*/
stop: string;
/**
* A logging ID
*/
log_id: string;
/**
* If you're within the message limit
*/
messageLimit: any;
}>;
/**
* Send a message to this conversation
* @param {String} message
* @async
* @param {SendMessageParams} params The parameters to send along with the message
* @returns {Promise<MessageStreamChunk>}
*/
sendMessage(message: string, { retry, timezone, attachments, model, done, progress, rawResponse }?: SendMessageParams): Promise<{
/**
* The markdown text completion for this response
*/
completion: string;
/**
* The reason for the response stop (if any)
*/
stop_reason: string;
/**
* The model used
*/
model: string;
/**
* The string at which Claude stopped responding at, e.g. "\n\nHuman:"
*/
stop: string;
/**
* A logging ID
*/
log_id: string;
/**
* If you're within the message limit
*/
messageLimit: any;
}>;
/**
* Rename the current conversation
* @async
* @param {String} title The new title
* @returns {Promise<Response>} A Response object
*/
rename(title: string): Promise<Response>;
/**
* Delete the conversation
* @async
* @returns Promise<Response>
*/
delete(): Promise<Response>;
/**
* @typedef Message
* @property {UUID} uuid The message UUID
* @property {String} text The message text
* @property {String} created_at The message created at
* @property {String} updated_at The message updated at
* @property {String | null} edited_at When the message was last edited (no editing support via api/web client)
* @property {Any | null} chat_feedback Feedback
* @property {Attachment[]} attachments The attachments
*/
/**
* @typedef ConversationInfo
* @extends Conversation
* @property {Message[]} chat_messages The messages in this conversation
*/
/**
* Get information about this conversation
* @returns {Promise<ConversationInfo>}
*/
getInfo(): Promise<any>;
/**
* Get all the files from this conversation
* @async
* @returns {Promise<Attachment[]>}
*/
getFiles(): Promise<{
/**
* The file name
*/
file_name: string;
/**
* The file's mime type
*/
file_type: string;
/**
* The file size in bytes
*/
file_size: number;
/**
* The contents of the file that were extracted
*/
extracted_content: string;
/**
* The total pages of the document
*/
totalPages?: number;
}[]>;
/**
* Get all messages in the conversation
* @async
* @returns {Promise<Message[]>}
*/
getMessages(): Promise<{
/**
* The message UUID
*/
uuid: any;
/**
* The message text
*/
text: string;
/**
* The message created at
*/
created_at: string;
/**
* The message updated at
*/
updated_at: string;
/**
* When the message was last edited (no editing support via api/web client)
*/
edited_at: string | null;
/**
* Feedback
*/
chat_feedback: Any | null;
/**
* The attachments
*/
attachments: {
/**
* The file name
*/
file_name: string;
/**
* The file's mime type
*/
file_type: string;
/**
* The file size in bytes
*/
file_size: number;
/**
* The contents of the file that were extracted
*/
extracted_content: string;
/**
* The total pages of the document
*/
totalPages?: number;
}[];
}[]>;
#private;
}
/**
* @typedef JSONResponse
* @property {'human' | 'assistant'} sender The sender
* @property {string} text The text
* @property {UUID} uuid msg uuid
* @property {string} created_at The message created at
* @property {string} updated_at The message updated at
* @property {string} edited_at When the message was last edited (no editing support via api/web client)
* @property {Attachment[]} attachments The attachments
* @property {string} chat_feedback Feedback
*/
/**
* Message class
* @class
* @classdesc A class representing a message in a Conversation
* @property {Function} request The request function (inherited from claude instance)
* @property {JSONResponse} json The JSON representation
* @property {Claude} claude The claude instance
* @property {Conversation} conversation The conversation this message belongs to
* @property {UUID} uuid The message uuid
*/
export class Message {
/**
* Create a Message instance.
* @param {Object} params - Params
* @param {Conversation} params.conversation - Conversation instance
* @param {Claude} params.claude - Claude instance
* @param {Message} message - Message data
*/
constructor({ conversation, claude }: {
conversation: Conversation;
claude: Claude;
}, { uuid, text, sender, index, updated_at, edited_at, chat_feedback, attachments }: {
/**
* The message UUID
*/
uuid: any;
/**
* The message text
*/
text: string;
/**
* The message created at
*/
created_at: string;
/**
* The message updated at
*/
updated_at: string;
/**
* When the message was last edited (no editing support via api/web client)
*/
edited_at: string;
/**
* Feedback
*/
chat_feedback: any;
/**
* The attachments
*/
attachments: {
/**
* The file name
*/
file_name: string;
/**
* The file's mime type
*/
file_type: string;
/**
* The file size in bytes
*/
file_size: number;
/**
* The contents of the file that were extracted
*/
extracted_content: string;
/**
* The total pages of the document
*/
totalPages?: number;
}[];
});
request: (endpoint: string, options: any) => Promise<Response>;
json: {
uuid: any;
text: string;
sender: any;
index: any;
updated_at: string;
edited_at: string;
chat_feedback: any;
attachments: {
/**
* The file name
*/
file_name: string;
/**
* The file's mime type
*/
file_type: string;
/**
* The file size in bytes
*/
file_size: number;
/**
* The contents of the file that were extracted
*/
extracted_content: string;
/**
* The total pages of the document
*/
totalPages?: number;
}[];
};
/**
* Convert this message to a JSON representation
* Necessary to prevent circular JSON errors
* @returns {Message}
*/
toJSON(): {
/**
* The message UUID
*/
uuid: any;
/**
* The message text
*/
text: string;
/**
* The message created at
*/
created_at: string;
/**
* The message updated at
*/
updated_at: string;
/**
* When the message was last edited (no editing support via api/web client)
*/
edited_at: string;
/**
* Feedback
*/
chat_feedback: any;
/**
* The attachments
*/
attachments: {
/**
* The file name
*/
file_name: string;
/**
* The file's mime type
*/
file_type: string;
/**
* The file size in bytes
*/
file_size: number;
/**
* The contents of the file that were extracted
*/
extracted_content: string;
/**
* The total pages of the document
*/
totalPages?: number;
}[];
};
/**
* Returns the value of the "created_at" property as a Date object.
*
* @return {Date} The value of the "created_at" property as a Date object.
*/
get createdAt(): Date;
/**
* Returns the value of the "updated_at" property as a Date object.
*
* @return {Date} The value of the "updated_at" property as a Date object.
*/
get updatedAt(): Date;
/**
* Returns the value of the "edited_at" property as a Date object.
*
* @return {Date} The value of the "edited_at" property as a Date object.
*/
get editedAt(): Date;
/**
* Get if message is from the assistant.
* @type {boolean}
*/
get isBot(): boolean;
/**
* @typedef MessageFeedback
* @property {UUID} uuid - Message UUID
* @property {"flag/bug" | "flag/harmful" | "flag/other"} type - Feedback type
* @property {String | null} reason - Feedback reason (details box)
* @property {String} created_at - Feedback creation date
* @property {String} updated_at - Feedback update date
*/
/**
* Send feedback on the message.
* @param {string} type - Feedback type
* @param {string} [reason] - Feedback reason
* @returns {Promise<MessageFeedback>} Response
*/
sendFeedback(type: string, reason?: string): Promise<{
/**
* - Message UUID
*/
uuid: any;
/**
* - Feedback type
*/
type: "flag/bug" | "flag/harmful" | "flag/other";
/**
* - Feedback reason (details box)
*/
reason: string | null;
/**
* - Feedback creation date
*/
created_at: string;
/**
* - Feedback update date
*/
updated_at: string;
}>;
}
export default Claude;
export type SendMessageParams = {
/**
* Whether to retry the most recent message in the conversation instead of sending a new one
*/
retry?: boolean;
/**
* The timezone
*/
timezone?: string;
/**
* Attachments
*/
attachments?: {
/**
* The file name
*/
file_name: string;
/**
* The file's mime type
*/
file_type: string;
/**
* The file size in bytes
*/
file_size: number;
/**
* The contents of the file that were extracted
*/
extracted_content: string;
/**
* The total pages of the document
*/
totalPages?: number;
}[];
/**
* Callback when done receiving the message response
*/
done?: (a: {
/**
* The markdown text completion for this response
*/
completion: string;
/**
* The reason for the response stop (if any)
*/
stop_reason: string;
/**
* The model used
*/
model: string;
/**
* The string at which Claude stopped responding at, e.g. "\n\nHuman:"
*/
stop: string;
/**
* A logging ID
*/
log_id: string;
/**
* If you're within the message limit
*/
messageLimit: any;
}) => any;
/**
* Callback on message response progress
*/
progress?: (a: {
/**
* The markdown text completion for this response
*/
completion: string;
/**
* The reason for the response stop (if any)
*/
stop_reason: string;
/**
* The model used
*/
model: string;
/**
* The string at which Claude stopped responding at, e.g. "\n\nHuman:"
*/
stop: string;
/**
* A logging ID
*/
log_id: string;
/**
* If you're within the message limit
*/
messageLimit: any;
}) => any;
/**
* The model to use
*/
model?: string;
};
export type JSONResponse = {
/**
* The sender
*/
sender: 'human' | 'assistant';
/**
* The text
*/
text: string;
/**
* msg uuid
*/
uuid: any;
/**
* The message created at
*/
created_at: string;
/**
* The message updated at
*/
updated_at: string;
/**
* When the message was last edited (no editing support via api/web client)
*/
edited_at: string;
/**
* The attachments
*/
attachments: {
/**
* The file name
*/
file_name: string;
/**
* The file's mime type
*/
file_type: string;
/**
* The file size in bytes
*/
file_size: number;
/**
* The contents of the file that were extracted
*/
extracted_content: string;
/**
* The total pages of the document
*/
totalPages?: number;
}[];
/**
* Feedback
*/
chat_feedback: string;
};