-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathAssistantClient.cs
More file actions
996 lines (893 loc) · 56.2 KB
/
AssistantClient.cs
File metadata and controls
996 lines (893 loc) · 56.2 KB
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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
using Microsoft.TypeSpec.Generator.Customizations;
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace OpenAI.Assistants;
/// <summary> The service client for OpenAI assistants operations. </summary>
[CodeGenType("Assistants")]
[CodeGenSuppress("AssistantClient", typeof(ClientPipeline), typeof(Uri))]
[CodeGenSuppress("CreateAssistantAsync", typeof(AssistantCreationOptions), typeof(CancellationToken))]
[CodeGenSuppress("CreateAssistant", typeof(AssistantCreationOptions), typeof(CancellationToken))]
[CodeGenSuppress("GetAssistantAsync", typeof(string))]
[CodeGenSuppress("GetAssistant", typeof(string))]
[CodeGenSuppress("ModifyAssistantAsync", typeof(string), typeof(AssistantModificationOptions))]
[CodeGenSuppress("ModifyAssistant", typeof(string), typeof(AssistantModificationOptions))]
[CodeGenSuppress("DeleteAssistantAsync", typeof(string))]
[CodeGenSuppress("DeleteAssistant", typeof(string))]
public partial class AssistantClient
{
private readonly InternalAssistantMessageClient _messageSubClient;
private readonly InternalAssistantRunClient _runSubClient;
private readonly InternalAssistantThreadClient _threadSubClient;
// CUSTOM: Added as a convenience.
/// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
/// <param name="apiKey"> The API key to authenticate with the service. </param>
/// <exception cref="ArgumentNullException"> <paramref name="apiKey"/> is null. </exception>
public AssistantClient(string apiKey) : this(new ApiKeyCredential(apiKey), new OpenAIClientOptions())
{
}
// CUSTOM:
// - Used a custom pipeline.
// - Demoted the endpoint parameter to be a property in the options class.
/// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
/// <param name="credential"> The <see cref="ApiKeyCredential"/> to authenticate with the service. </param>
/// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
public AssistantClient(ApiKeyCredential credential) : this(credential, new OpenAIClientOptions())
{
}
// CUSTOM:
// - Used a custom pipeline.
// - Demoted the endpoint parameter to be a property in the options class.
/// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
/// <param name="credential"> The <see cref="ApiKeyCredential"/> to authenticate with the service. </param>
/// <param name="options"> The options to configure the client. </param>
/// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
public AssistantClient(ApiKeyCredential credential, OpenAIClientOptions options) : this(OpenAIClient.CreateApiKeyAuthenticationPolicy(credential), options)
{
}
// CUSTOM: Added as a convenience.
/// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
/// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
/// <exception cref="ArgumentNullException"> <paramref name="authenticationPolicy"/> is null. </exception>
public AssistantClient(AuthenticationPolicy authenticationPolicy) : this(authenticationPolicy, new OpenAIClientOptions())
{
}
// CUSTOM: Added as a convenience.
/// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
/// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
/// <param name="options"> The options to configure the client. </param>
/// <exception cref="ArgumentNullException"> <paramref name="authenticationPolicy"/> is null. </exception>
public AssistantClient(AuthenticationPolicy authenticationPolicy, OpenAIClientOptions options)
{
Argument.AssertNotNull(authenticationPolicy, nameof(authenticationPolicy));
options ??= new OpenAIClientOptions();
Pipeline = OpenAIClient.CreatePipeline(authenticationPolicy, options);
_endpoint = OpenAIClient.GetEndpoint(options);
_messageSubClient = new(Pipeline, options);
_runSubClient = new(Pipeline, options);
_threadSubClient = new(Pipeline, options);
}
[Experimental("SCME0002")]
public AssistantClient(AssistantClientSettings settings)
: this(AuthenticationPolicy.Create(settings), settings?.Options)
{
}
/// <summary>
/// Gets the endpoint URI for the service.
/// </summary>
[Experimental("OPENAI001")]
public Uri Endpoint => _endpoint;
// CUSTOM:
// - Used a custom pipeline.
// - Demoted the endpoint parameter to be a property in the options class.
// - Made protected.
/// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
/// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
/// <param name="options"> The options to configure the client. </param>
/// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> is null. </exception>
protected internal AssistantClient(ClientPipeline pipeline, OpenAIClientOptions options)
{
Argument.AssertNotNull(pipeline, nameof(pipeline));
options ??= new OpenAIClientOptions();
Pipeline = pipeline;
_endpoint = OpenAIClient.GetEndpoint(options);
_messageSubClient = new(Pipeline, options);
_runSubClient = new(Pipeline, options);
_threadSubClient = new(Pipeline, options);
}
/// <summary> Creates a new assistant. </summary>
/// <param name="model"> The default model that the assistant should use. </param>
/// <param name="options"> The additional <see cref="AssistantCreationOptions"/> to use. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentException"> <paramref name="model"/> is null or empty. </exception>
public virtual async Task<ClientResult<Assistant>> CreateAssistantAsync(string model, AssistantCreationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(model, nameof(model));
options ??= new();
options.Model = model;
ClientResult protocolResult = await CreateAssistantAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
}
/// <summary> Creates a new assistant. </summary>
/// <param name="model"> The default model that the assistant should use. </param>
/// <param name="options"> The additional <see cref="AssistantCreationOptions"/> to use. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <exception cref="ArgumentException"> <paramref name="model"/> is null or empty. </exception>
public virtual ClientResult<Assistant> CreateAssistant(string model, AssistantCreationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(model, nameof(model));
options ??= new();
options.Model = model;
ClientResult protocolResult = CreateAssistant(options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Gets an instance representing an existing <see cref="Assistant"/> based on its ID.
/// </summary>
/// <param name="assistantId"> The ID of the Assistant to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns>An <see cref="Assistant"/> instance representing the state of the Assistant with the provided ID.</returns>
public virtual async Task<ClientResult<Assistant>> GetAssistantAsync(string assistantId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
ClientResult protocolResult = await GetAssistantAsync(assistantId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Gets an instance representing an existing <see cref="Assistant"/> based on its ID.
/// </summary>
/// <param name="assistantId"> The ID of the Assistant to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns>An <see cref="Assistant"/> instance representing the state of the Assistant with the provided ID.</returns>
public virtual ClientResult<Assistant> GetAssistant(string assistantId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
ClientResult protocolResult = GetAssistant(assistantId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Modifies an existing <see cref="Assistant"/>.
/// </summary>
/// <param name="assistantId"> The ID of the Assistant to retrieve. </param>
/// <param name="options"> The new options to apply to the existing Assistant. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <returns> An updated <see cref="Assistant"/> instance representing the state of the Assistant with the provided ID. </returns>
public virtual async Task<ClientResult<Assistant>> ModifyAssistantAsync(string assistantId, AssistantModificationOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
Argument.AssertNotNull(options, nameof(options));
using BinaryContent content = options?.ToBinaryContent();
ClientResult protocolResult
= await ModifyAssistantAsync(assistantId, content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Modifies an existing <see cref="Assistant"/>.
/// </summary>
/// <param name="assistantId"> The ID of the Assistant to retrieve. </param>
/// <param name="options"> The new options to apply to the existing Assistant. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <returns> An updated <see cref="Assistant"/> instance representing the state of the Assistant with the provided ID. </returns>
public virtual ClientResult<Assistant> ModifyAssistant(string assistantId, AssistantModificationOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
Argument.AssertNotNull(options, nameof(options));
using BinaryContent content = options?.ToBinaryContent();
ClientResult protocolResult = ModifyAssistant(assistantId, content, null);
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Deletes an existing <see cref="Assistant"/>.
/// </summary>
/// <param name="assistantId"> The ID of the assistant to delete. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A <see cref="AssistantDeletionResult"/> instance. </returns>
public virtual async Task<ClientResult<AssistantDeletionResult>> DeleteAssistantAsync(string assistantId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
ClientResult protocolResult = await DeleteAssistantAsync(assistantId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((AssistantDeletionResult)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Deletes an existing <see cref="Assistant"/>.
/// </summary>
/// <param name="assistantId"> The ID of the assistant to delete. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A <see cref="AssistantDeletionResult"/> instance. </returns>
public virtual ClientResult<AssistantDeletionResult> DeleteAssistant(string assistantId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
ClientResult protocolResult = DeleteAssistant(assistantId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((AssistantDeletionResult)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Creates a new <see cref="AssistantThread"/>.
/// </summary>
/// <param name="options"> Additional options to use when creating the thread. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A new thread. </returns>
public virtual async Task<ClientResult<AssistantThread>> CreateThreadAsync(ThreadCreationOptions options = null, CancellationToken cancellationToken = default)
{
ClientResult protocolResult = await CreateThreadAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Creates a new <see cref="AssistantThread"/>.
/// </summary>
/// <param name="options"> Additional options to use when creating the thread. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A new thread. </returns>
public virtual ClientResult<AssistantThread> CreateThread(ThreadCreationOptions options = null, CancellationToken cancellationToken = default)
{
ClientResult protocolResult = CreateThread(options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Gets an existing <see cref="AssistantThread"/>, retrieved via a known ID.
/// </summary>
/// <param name="threadId"> The ID of the thread to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The existing thread instance. </returns>
public virtual async Task<ClientResult<AssistantThread>> GetThreadAsync(string threadId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
ClientResult protocolResult = await GetThreadAsync(threadId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Gets an existing <see cref="AssistantThread"/>, retrieved via a known ID.
/// </summary>
/// <param name="threadId"> The ID of the thread to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The existing thread instance. </returns>
public virtual ClientResult<AssistantThread> GetThread(string threadId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
ClientResult protocolResult = GetThread(threadId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Modifies an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to modify. </param>
/// <param name="options"> The modifications to apply to the thread. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The updated <see cref="AssistantThread"/> instance. </returns>
public virtual async Task<ClientResult<AssistantThread>> ModifyThreadAsync(string threadId, ThreadModificationOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNull(options, nameof(options));
ClientResult protocolResult = await ModifyThreadAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Modifies an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to modify. </param>
/// <param name="options"> The modifications to apply to the thread. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The updated <see cref="AssistantThread"/> instance. </returns>
public virtual ClientResult<AssistantThread> ModifyThread(string threadId, ThreadModificationOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNull(options, nameof(options));
ClientResult protocolResult = ModifyThread(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Deletes an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to delete. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A <see cref="ThreadDeletionResult"/> instance. </returns>
public virtual async Task<ClientResult<ThreadDeletionResult>> DeleteThreadAsync(string threadId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
ClientResult protocolResult = await DeleteThreadAsync(threadId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((ThreadDeletionResult)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Deletes an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to delete. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A <see cref="ThreadDeletionResult"/> instance. </returns>
public virtual ClientResult<ThreadDeletionResult> DeleteThread(string threadId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
ClientResult protocolResult = DeleteThread(threadId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((ThreadDeletionResult)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Creates a new <see cref="ThreadMessage"/> on an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to associate the new message with. </param>
/// <param name="role"> The role to associate with the new message. </param>
/// <param name="content"> The collection of <see cref="MessageContent"/> items for the message. </param>
/// <param name="options"> Additional options to apply to the new message. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A new <see cref="ThreadMessage"/>. </returns>
public virtual async Task<ClientResult<ThreadMessage>> CreateMessageAsync(
string threadId,
MessageRole role,
IEnumerable<MessageContent> content,
MessageCreationOptions options = null,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
options ??= new();
options.Role = role;
options.Content.Clear();
foreach (MessageContent contentItem in content)
{
options.Content.Add(contentItem);
}
ClientResult protocolResult = await CreateMessageAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions())
.ConfigureAwait(false);
return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Creates a new <see cref="ThreadMessage"/> on an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to associate the new message with. </param>
/// <param name="role"> The role to associate with the new message. </param>
/// <param name="content"> The collection of <see cref="MessageContent"/> items for the message. </param>
/// <param name="options"> Additional options to apply to the new message. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A new <see cref="ThreadMessage"/>. </returns>
public virtual ClientResult<ThreadMessage> CreateMessage(
string threadId,
MessageRole role,
IEnumerable<MessageContent> content,
MessageCreationOptions options = null,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
options ??= new();
options.Role = role;
options.Content.Clear();
foreach (MessageContent contentItem in content)
{
options.Content.Add(contentItem);
}
ClientResult protocolResult = CreateMessage(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Gets a page collection of <see cref="ThreadMessage"/> instances from an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to list messages from. </param>
/// <param name="options"> Options describing the collection to return. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A collection of <see cref="ThreadMessage"/>. </returns>
public virtual AsyncCollectionResult<ThreadMessage> GetMessagesAsync(
string threadId,
MessageCollectionOptions options = default,
CancellationToken cancellationToken = default)
=> _messageSubClient.GetMessagesAsync(threadId, options, cancellationToken);
/// <summary>
/// Gets a page collection holding <see cref="ThreadMessage"/> instances from an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to list messages from. </param>
/// <param name="options"> Options describing the collection to return. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A collection of <see cref="ThreadMessage"/>. </returns>
public virtual CollectionResult<ThreadMessage> GetMessages(
string threadId,
MessageCollectionOptions options = default,
CancellationToken cancellationToken = default)
=> _messageSubClient.GetMessages(threadId, options, cancellationToken);
/// <summary>
/// Gets an existing <see cref="ThreadMessage"/> from a known <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to retrieve the message from. </param>
/// <param name="messageId"> The ID of the message to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The existing <see cref="ThreadMessage"/> instance. </returns>
public virtual async Task<ClientResult<ThreadMessage>> GetMessageAsync(string threadId, string messageId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
ClientResult protocolResult = await GetMessageAsync(threadId, messageId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Gets an existing <see cref="ThreadMessage"/> from a known <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to retrieve the message from. </param>
/// <param name="messageId"> The ID of the message to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The existing <see cref="ThreadMessage"/> instance. </returns>
public virtual ClientResult<ThreadMessage> GetMessage(string threadId, string messageId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
ClientResult protocolResult = GetMessage(threadId, messageId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Modifies an existing <see cref="ThreadMessage"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the message to modify. </param>
/// <param name="messageId"> The ID of the message to modify. </param>
/// <param name="options"> The changes to apply to the message. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The updated <see cref="ThreadMessage"/>. </returns>
public virtual async Task<ClientResult<ThreadMessage>> ModifyMessageAsync(string threadId, string messageId, MessageModificationOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
Argument.AssertNotNull(options, nameof(options));
ClientResult protocolResult = await ModifyMessageAsync(threadId, messageId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions())
.ConfigureAwait(false);
return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Modifies an existing <see cref="ThreadMessage"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the message to modify. </param>
/// <param name="messageId"> The ID of the message to modify. </param>
/// <param name="options"> The changes to apply to the message. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The updated <see cref="ThreadMessage"/>. </returns>
public virtual ClientResult<ThreadMessage> ModifyMessage(string threadId, string messageId, MessageModificationOptions options, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
Argument.AssertNotNull(options, nameof(options));
ClientResult protocolResult = ModifyMessage(threadId, messageId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Deletes an existing <see cref="ThreadMessage"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the message. </param>
/// <param name="messageId"> The ID of the message. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A <see cref="MessageDeletionResult"/> instance. </returns>
public virtual async Task<ClientResult<MessageDeletionResult>> DeleteMessageAsync(string threadId, string messageId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
ClientResult protocolResult = await DeleteMessageAsync(threadId, messageId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((MessageDeletionResult)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Deletes an existing <see cref="ThreadMessage"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the message. </param>
/// <param name="messageId"> The ID of the message. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A <see cref="MessageDeletionResult"/> instance. </returns>
public virtual ClientResult<MessageDeletionResult> DeleteMessage(string threadId, string messageId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
ClientResult protocolResult = DeleteMessage(threadId, messageId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((MessageDeletionResult)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Begins a new <see cref="ThreadRun"/> that evaluates a <see cref="AssistantThread"/> using a specified
/// <see cref="Assistant"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread that the run should evaluate. </param>
/// <param name="assistantId"> The ID of the assistant that should be used when evaluating the thread. </param>
/// <param name="options"> Additional options for the run. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A new <see cref="ThreadRun"/> instance. </returns>
public virtual async Task<ClientResult<ThreadRun>> CreateRunAsync(string threadId, string assistantId, RunCreationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
options ??= new();
options.AssistantId = assistantId;
options.Stream = null;
ClientResult protocolResult = await CreateRunAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions())
.ConfigureAwait(false);
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Begins a new <see cref="ThreadRun"/> that evaluates a <see cref="AssistantThread"/> using a specified
/// <see cref="Assistant"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread that the run should evaluate. </param>
/// <param name="assistantId"> The ID of the assistant that should be used when evaluating the thread. </param>
/// <param name="options"> Additional options for the run. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A new <see cref="ThreadRun"/> instance. </returns>
public virtual ClientResult<ThreadRun> CreateRun(string threadId, string assistantId, RunCreationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
options ??= new();
options.AssistantId = assistantId;
options.Stream = null;
ClientResult protocolResult = CreateRun(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Begins a new streaming <see cref="ThreadRun"/> that evaluates a <see cref="AssistantThread"/> using a specified
/// <see cref="Assistant"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread that the run should evaluate. </param>
/// <param name="assistantId"> The ID of the assistant that should be used when evaluating the thread. </param>
/// <param name="options"> Additional options for the run. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
public virtual AsyncCollectionResult<StreamingUpdate> CreateRunStreamingAsync(
string threadId,
string assistantId,
RunCreationOptions options = null,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
options ??= new();
options.AssistantId = assistantId;
options.Stream = true;
return new AsyncSseUpdateCollection<StreamingUpdate>(
async () => await CreateRunAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
StreamingUpdate.FromSseItem,
cancellationToken);
}
/// <summary>
/// Begins a new streaming <see cref="ThreadRun"/> that evaluates a <see cref="AssistantThread"/> using a specified
/// <see cref="Assistant"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread that the run should evaluate. </param>
/// <param name="assistantId"> The ID of the assistant that should be used when evaluating the thread. </param>
/// <param name="options"> Additional options for the run. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
public virtual CollectionResult<StreamingUpdate> CreateRunStreaming(
string threadId,
string assistantId,
RunCreationOptions options = null,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
options ??= new();
options.AssistantId = assistantId;
options.Stream = true;
return new SseUpdateCollection<StreamingUpdate>(
() => CreateRun(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions(streaming: true)),
StreamingUpdate.FromSseItem,
cancellationToken);
}
/// <summary>
/// Creates a new thread and immediately begins a run against it using the specified <see cref="Assistant"/>.
/// </summary>
/// <param name="assistantId"> The ID of the assistant that the new run should use. </param>
/// <param name="threadOptions"> Options for the new thread that will be created. </param>
/// <param name="runOptions"> Additional options to apply to the run that will begin. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A new <see cref="ThreadRun"/>. </returns>
public virtual async Task<ClientResult<ThreadRun>> CreateThreadAndRunAsync(
string assistantId,
ThreadCreationOptions threadOptions = null,
RunCreationOptions runOptions = null,
CancellationToken cancellationToken = default)
{
runOptions ??= new();
runOptions.Stream = null;
BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions);
ClientResult protocolResult = await CreateThreadAndRunAsync(protocolContent, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Creates a new thread and immediately begins a run against it using the specified <see cref="Assistant"/>.
/// </summary>
/// <param name="assistantId"> The ID of the assistant that the new run should use. </param>
/// <param name="threadOptions"> Options for the new thread that will be created. </param>
/// <param name="runOptions"> Additional options to apply to the run that will begin. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A new <see cref="ThreadRun"/>. </returns>
public virtual ClientResult<ThreadRun> CreateThreadAndRun(
string assistantId,
ThreadCreationOptions threadOptions = null,
RunCreationOptions runOptions = null,
CancellationToken cancellationToken = default)
{
runOptions ??= new();
runOptions.Stream = null;
BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions);
ClientResult protocolResult = CreateThreadAndRun(protocolContent, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Creates a new thread and immediately begins a streaming run against it using the specified <see cref="Assistant"/>.
/// </summary>
/// <param name="assistantId"> The ID of the assistant that the new run should use. </param>
/// <param name="threadOptions"> Options for the new thread that will be created. </param>
/// <param name="runOptions"> Additional options to apply to the run that will begin. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
public virtual AsyncCollectionResult<StreamingUpdate> CreateThreadAndRunStreamingAsync(
string assistantId,
ThreadCreationOptions threadOptions = null,
RunCreationOptions runOptions = null,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
runOptions ??= new();
runOptions.Stream = true;
BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions);
return new AsyncSseUpdateCollection<StreamingUpdate>(
async () => await CreateThreadAndRunAsync(protocolContent, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
StreamingUpdate.FromSseItem,
cancellationToken);
}
/// <summary>
/// Creates a new thread and immediately begins a streaming run against it using the specified <see cref="Assistant"/>.
/// </summary>
/// <param name="assistantId"> The ID of the assistant that the new run should use. </param>
/// <param name="threadOptions"> Options for the new thread that will be created. </param>
/// <param name="runOptions"> Additional options to apply to the run that will begin. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
public virtual CollectionResult<StreamingUpdate> CreateThreadAndRunStreaming(
string assistantId,
ThreadCreationOptions threadOptions = null,
RunCreationOptions runOptions = null,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
runOptions ??= new();
runOptions.Stream = true;
BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions);
return new SseUpdateCollection<StreamingUpdate>(
() => CreateThreadAndRun(protocolContent, cancellationToken.ToRequestOptions(streaming: true)),
StreamingUpdate.FromSseItem,
cancellationToken);
}
/// <summary>
/// Gets a page collection holding <see cref="ThreadRun"/> instances associated with an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread that runs in the list should be associated with. </param>
/// <param name="options"> Options describing the collection to return. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A collection of <see cref="ThreadRun"/>. </returns>
public virtual AsyncCollectionResult<ThreadRun> GetRunsAsync(
string threadId,
RunCollectionOptions options = default,
CancellationToken cancellationToken = default)
=> _runSubClient.GetRunsAsync(threadId, options, cancellationToken);
/// <summary>
/// Gets a page collection holding <see cref="ThreadRun"/> instances associated with an existing <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread that runs in the list should be associated with. </param>
/// <param name="options"> Options describing the collection to return. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A collection of <see cref="ThreadRun"/>. </returns>
public virtual CollectionResult<ThreadRun> GetRuns(
string threadId,
RunCollectionOptions options = default,
CancellationToken cancellationToken = default)
=> _runSubClient.GetRuns(threadId, options, cancellationToken);
/// <summary>
/// Gets an existing <see cref="ThreadRun"/> from a known <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to retrieve the run from. </param>
/// <param name="runId"> The ID of the run to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The existing <see cref="ThreadRun"/> instance. </returns>
public virtual async Task<ClientResult<ThreadRun>> GetRunAsync(string threadId, string runId, CancellationToken cancellationToken = default)
=> await _runSubClient.GetRunAsync(threadId, runId, cancellationToken);
/// <summary>
/// Gets an existing <see cref="ThreadRun"/> from a known <see cref="AssistantThread"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread to retrieve the run from. </param>
/// <param name="runId"> The ID of the run to retrieve. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The existing <see cref="ThreadRun"/> instance. </returns>
public virtual ClientResult<ThreadRun> GetRun(string threadId, string runId, CancellationToken cancellationToken = default)
=> _runSubClient.GetRun(threadId, runId, cancellationToken);
/// <summary>
/// Submits a collection of required tool call outputs to a run and resumes the run.
/// </summary>
/// <param name="threadId"> The thread ID of the thread being run. </param>
/// <param name="runId"> The ID of the run that reached a <c>requires_action</c> status. </param>
/// <param name="toolOutputs">
/// The tool outputs, corresponding to <see cref="InternalRequiredToolCall"/> instances from the run.
/// </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The <see cref="ThreadRun"/>, updated after the submission was processed. </returns>
public virtual async Task<ClientResult<ThreadRun>> SubmitToolOutputsToRunAsync(
string threadId,
string runId,
IEnumerable<ToolOutput> toolOutputs,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
var submitToolOutputsRunRequest = new InternalSubmitToolOutputsRunRequest(toolOutputs);
using BinaryContent content = BinaryContent.Create(submitToolOutputsRunRequest, ModelSerializationExtensions.WireOptions);
ClientResult protocolResult = await SubmitToolOutputsToRunAsync(threadId, runId, content, cancellationToken.ToRequestOptions())
.ConfigureAwait(false);
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Submits a collection of required tool call outputs to a run and resumes the run.
/// </summary>
/// <param name="threadId"> The thread ID of the thread being run. </param>
/// <param name="runId"> The ID of the run that reached a <c>requires_action</c> status. </param>
/// <param name="toolOutputs">
/// The tool outputs, corresponding to <see cref="InternalRequiredToolCall"/> instances from the run.
/// </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> The <see cref="ThreadRun"/>, updated after the submission was processed. </returns>
public virtual ClientResult<ThreadRun> SubmitToolOutputsToRun(
string threadId,
string runId,
IEnumerable<ToolOutput> toolOutputs,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
var submitToolOutputsRunRequest = new InternalSubmitToolOutputsRunRequest(toolOutputs);
using BinaryContent content = BinaryContent.Create(submitToolOutputsRunRequest, ModelSerializationExtensions.WireOptions);
ClientResult protocolResult = SubmitToolOutputsToRun(threadId, runId, content, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Submits a collection of required tool call outputs to a run and resumes the run with streaming enabled.
/// </summary>
/// <param name="threadId"> The thread ID of the thread being run. </param>
/// <param name="runId"> The ID of the run that reached a <c>requires_action</c> status. </param>
/// <param name="toolOutputs">
/// The tool outputs, corresponding to <see cref="InternalRequiredToolCall"/> instances from the run.
/// </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
public virtual AsyncCollectionResult<StreamingUpdate> SubmitToolOutputsToRunStreamingAsync(
string threadId,
string runId,
IEnumerable<ToolOutput> toolOutputs,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
var submitToolOutputsRunRequest = new InternalSubmitToolOutputsRunRequest(toolOutputs.ToList(), stream: true, null);
using BinaryContent content = BinaryContent.Create(submitToolOutputsRunRequest, ModelSerializationExtensions.WireOptions);
return new AsyncSseUpdateCollection<StreamingUpdate>(
async () => await SubmitToolOutputsToRunAsync(threadId, runId, content, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
StreamingUpdate.FromSseItem,
cancellationToken);
}
/// <summary>
/// Submits a collection of required tool call outputs to a run and resumes the run with streaming enabled.
/// </summary>
/// <param name="threadId"> The thread ID of the thread being run. </param>
/// <param name="runId"> The ID of the run that reached a <c>requires_action</c> status. </param>
/// <param name="toolOutputs">
/// The tool outputs, corresponding to <see cref="InternalRequiredToolCall"/> instances from the run.
/// </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
public virtual CollectionResult<StreamingUpdate> SubmitToolOutputsToRunStreaming(
string threadId,
string runId,
IEnumerable<ToolOutput> toolOutputs,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
var submitToolOutputsRunRequest = new InternalSubmitToolOutputsRunRequest(toolOutputs.ToList(), stream: true, null);
using BinaryContent content = BinaryContent.Create(submitToolOutputsRunRequest, ModelSerializationExtensions.WireOptions);
return new SseUpdateCollection<StreamingUpdate>(
() => SubmitToolOutputsToRun(threadId, runId, content, cancellationToken.ToRequestOptions(streaming: true)),
StreamingUpdate.FromSseItem,
cancellationToken);
}
/// <summary>
/// Cancels an in-progress <see cref="ThreadRun"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the run. </param>
/// <param name="runId"> The ID of the run to cancel. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> An updated <see cref="ThreadRun"/> instance, reflecting the new status of the run. </returns>
public virtual async Task<ClientResult<ThreadRun>> CancelRunAsync(string threadId, string runId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
ClientResult protocolResult = await CancelRunAsync(threadId, runId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Cancels an in-progress <see cref="ThreadRun"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the run. </param>
/// <param name="runId"> The ID of the run to cancel. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> An updated <see cref="ThreadRun"/> instance, reflecting the new status of the run. </returns>
public virtual ClientResult<ThreadRun> CancelRun(string threadId, string runId, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
Argument.AssertNotNullOrEmpty(runId, nameof(runId));
ClientResult protocolResult = CancelRun(threadId, runId, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
}
/// <summary>
/// Gets a page collection holding <see cref="RunStep"/> instances associated with a <see cref="ThreadRun"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the run. </param>
/// <param name="runId"> The ID of the run to list run steps from. </param>
/// <param name="options"></param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A collection of <see cref="RunStep"/>. </returns>
public virtual AsyncCollectionResult<RunStep> GetRunStepsAsync(
string threadId,
string runId,
RunStepCollectionOptions options = default,
CancellationToken cancellationToken = default)
=> _runSubClient.GetRunStepsAsync(threadId, runId, options, [InternalIncludedRunStepProperty.FileSearchResultContent], cancellationToken);
/// <summary>
/// Gets a page collection holding <see cref="RunStep"/> instances associated with a <see cref="ThreadRun"/>.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the run. </param>
/// <param name="runId"> The ID of the run to list run steps from. </param>
/// <param name="options"></param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A collection of <see cref="RunStep"/>. </returns>
public virtual CollectionResult<RunStep> GetRunSteps(
string threadId,
string runId,
RunStepCollectionOptions options = default,
CancellationToken cancellationToken = default)
=> _runSubClient.GetRunSteps(threadId, runId, options, [InternalIncludedRunStepProperty.FileSearchResultContent], cancellationToken);
/// <summary>
/// Gets a single run step from a run.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the run. </param>
/// <param name="runId"> The ID of the run. </param>
/// <param name="stepId"> The ID of the run step. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A <see cref="RunStep"/> instance corresponding to the specified step. </returns>
public virtual async Task<ClientResult<RunStep>> GetRunStepAsync(string threadId, string runId, string stepId, CancellationToken cancellationToken = default)
=> await _runSubClient.GetRunStepAsync(threadId, runId, stepId, [InternalIncludedRunStepProperty.FileSearchResultContent], cancellationToken).ConfigureAwait(false);
/// <summary>
/// Gets a single run step from a run.
/// </summary>
/// <param name="threadId"> The ID of the thread associated with the run. </param>
/// <param name="runId"> The ID of the run. </param>
/// <param name="stepId"> The ID of the run step. </param>
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
/// <returns> A <see cref="RunStep"/> instance corresponding to the specified step. </returns>
public virtual ClientResult<RunStep> GetRunStep(string threadId, string runId, string stepId, CancellationToken cancellationToken = default)
=> _runSubClient.GetRunStep(threadId, runId, stepId, [InternalIncludedRunStepProperty.FileSearchResultContent], cancellationToken);
private static BinaryContent CreateThreadAndRunProtocolContent(
string assistantId,
ThreadCreationOptions threadOptions,
RunCreationOptions runOptions)
{
Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
InternalCreateThreadAndRunRequest internalRequest = new(
assistantId: assistantId,
thread: threadOptions,
instructions: runOptions.InstructionsOverride,
tools: runOptions.ToolsOverride,
metadata: runOptions.Metadata,
temperature: runOptions.Temperature,
// TODO: reconcile exposure of the two different tool_resources, if needed
topP: runOptions.NucleusSamplingFactor,
stream: runOptions.Stream,
maxPromptTokens: runOptions.MaxInputTokenCount,
maxCompletionTokens: runOptions.MaxOutputTokenCount,
truncationStrategy: runOptions.TruncationStrategy,
parallelToolCalls: runOptions.AllowParallelToolCalls,
model: runOptions.ModelOverride,
toolResources: threadOptions.ToolResources,
responseFormat: runOptions.ResponseFormat,
toolChoice: runOptions.ToolConstraint,
additionalBinaryDataProperties: null);
return BinaryContent.Create(internalRequest, ModelSerializationExtensions.WireOptions);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ClientResult<T> CreateResultFromProtocol<T>(ClientResult protocolResult, Func<PipelineResponse, T> responseDeserializer)
{
PipelineResponse pipelineResponse = protocolResult?.GetRawResponse();
T deserializedResultValue = responseDeserializer.Invoke(pipelineResponse);
return ClientResult.FromValue(deserializedResultValue, pipelineResponse);
}
}