openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/modify-responsesclient-default-model

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Assistants/AssistantClient.cs

990lines · modecode

1using Microsoft.TypeSpec.Generator.Customizations;
2using System;
3using System.ClientModel;
4using System.ClientModel.Primitives;
5using System.Collections.Generic;
6using System.Diagnostics.CodeAnalysis;
7using System.Linq;
8using System.Runtime.CompilerServices;
9using System.Threading;
10using System.Threading.Tasks;
11
12namespace OpenAI.Assistants;
13
14/// <summary> The service client for OpenAI assistants operations. </summary>
15[CodeGenType("Assistants")]
16[CodeGenSuppress("AssistantClient", typeof(ClientPipeline), typeof(Uri))]
17[CodeGenSuppress("CreateAssistantAsync", typeof(AssistantCreationOptions), typeof(CancellationToken))]
18[CodeGenSuppress("CreateAssistant", typeof(AssistantCreationOptions), typeof(CancellationToken))]
19[CodeGenSuppress("GetAssistantAsync", typeof(string))]
20[CodeGenSuppress("GetAssistant", typeof(string))]
21[CodeGenSuppress("ModifyAssistantAsync", typeof(string), typeof(AssistantModificationOptions))]
22[CodeGenSuppress("ModifyAssistant", typeof(string), typeof(AssistantModificationOptions))]
23[CodeGenSuppress("DeleteAssistantAsync", typeof(string))]
24[CodeGenSuppress("DeleteAssistant", typeof(string))]
25public partial class AssistantClient
26{
27 private readonly InternalAssistantMessageClient _messageSubClient;
28 private readonly InternalAssistantRunClient _runSubClient;
29 private readonly InternalAssistantThreadClient _threadSubClient;
30
31 // CUSTOM: Added as a convenience.
32 /// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
33 /// <param name="apiKey"> The API key to authenticate with the service. </param>
34 /// <exception cref="ArgumentNullException"> <paramref name="apiKey"/> is null. </exception>
35 public AssistantClient(string apiKey) : this(new ApiKeyCredential(apiKey), new OpenAIClientOptions())
36 {
37 }
38
39 // CUSTOM:
40 // - Used a custom pipeline.
41 // - Demoted the endpoint parameter to be a property in the options class.
42 /// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
43 /// <param name="credential"> The <see cref="ApiKeyCredential"/> to authenticate with the service. </param>
44 /// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
45 public AssistantClient(ApiKeyCredential credential) : this(credential, new OpenAIClientOptions())
46 {
47 }
48
49 // CUSTOM:
50 // - Used a custom pipeline.
51 // - Demoted the endpoint parameter to be a property in the options class.
52 /// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
53 /// <param name="credential"> The <see cref="ApiKeyCredential"/> to authenticate with the service. </param>
54 /// <param name="options"> The options to configure the client. </param>
55 /// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
56 public AssistantClient(ApiKeyCredential credential, OpenAIClientOptions options) : this(OpenAIClient.CreateApiKeyAuthenticationPolicy(credential), options)
57 {
58 }
59
60 // CUSTOM: Added as a convenience.
61 /// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
62 /// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
63 /// <exception cref="ArgumentNullException"> <paramref name="authenticationPolicy"/> is null. </exception>
64 public AssistantClient(AuthenticationPolicy authenticationPolicy) : this(authenticationPolicy, new OpenAIClientOptions())
65 {
66 }
67
68 // CUSTOM: Added as a convenience.
69 /// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
70 /// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
71 /// <param name="options"> The options to configure the client. </param>
72 /// <exception cref="ArgumentNullException"> <paramref name="authenticationPolicy"/> is null. </exception>
73 public AssistantClient(AuthenticationPolicy authenticationPolicy, OpenAIClientOptions options)
74 {
75 Argument.AssertNotNull(authenticationPolicy, nameof(authenticationPolicy));
76 options ??= new OpenAIClientOptions();
77
78 Pipeline = OpenAIClient.CreatePipeline(authenticationPolicy, options);
79 _endpoint = OpenAIClient.GetEndpoint(options);
80 _messageSubClient = new(Pipeline, options);
81 _runSubClient = new(Pipeline, options);
82 _threadSubClient = new(Pipeline, options);
83 }
84
85 /// <summary>
86 /// Gets the endpoint URI for the service.
87 /// </summary>
88 [Experimental("OPENAI001")]
89 public Uri Endpoint => _endpoint;
90
91
92 // CUSTOM:
93 // - Used a custom pipeline.
94 // - Demoted the endpoint parameter to be a property in the options class.
95 // - Made protected.
96 /// <summary> Initializes a new instance of <see cref="AssistantClient"/>. </summary>
97 /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
98 /// <param name="options"> The options to configure the client. </param>
99 /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> is null. </exception>
100 protected internal AssistantClient(ClientPipeline pipeline, OpenAIClientOptions options)
101 {
102 Argument.AssertNotNull(pipeline, nameof(pipeline));
103 options ??= new OpenAIClientOptions();
104
105 Pipeline = pipeline;
106 _endpoint = OpenAIClient.GetEndpoint(options);
107 _messageSubClient = new(Pipeline, options);
108 _runSubClient = new(Pipeline, options);
109 _threadSubClient = new(Pipeline, options);
110 }
111
112 /// <summary> Creates a new assistant. </summary>
113 /// <param name="model"> The default model that the assistant should use. </param>
114 /// <param name="options"> The additional <see cref="AssistantCreationOptions"/> to use. </param>
115 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
116 /// <exception cref="ArgumentException"> <paramref name="model"/> is null or empty. </exception>
117 public virtual async Task<ClientResult<Assistant>> CreateAssistantAsync(string model, AssistantCreationOptions options = null, CancellationToken cancellationToken = default)
118 {
119 Argument.AssertNotNullOrEmpty(model, nameof(model));
120 options ??= new();
121 options.Model = model;
122
123 ClientResult protocolResult = await CreateAssistantAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
124 return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
125 }
126
127 /// <summary> Creates a new assistant. </summary>
128 /// <param name="model"> The default model that the assistant should use. </param>
129 /// <param name="options"> The additional <see cref="AssistantCreationOptions"/> to use. </param>
130 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
131 /// <exception cref="ArgumentException"> <paramref name="model"/> is null or empty. </exception>
132 public virtual ClientResult<Assistant> CreateAssistant(string model, AssistantCreationOptions options = null, CancellationToken cancellationToken = default)
133 {
134 Argument.AssertNotNullOrEmpty(model, nameof(model));
135 options ??= new();
136 options.Model = model;
137
138 ClientResult protocolResult = CreateAssistant(options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
139 return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
140 }
141
142 /// <summary>
143 /// Gets an instance representing an existing <see cref="Assistant"/> based on its ID.
144 /// </summary>
145 /// <param name="assistantId"> The ID of the Assistant to retrieve. </param>
146 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
147 /// <returns>An <see cref="Assistant"/> instance representing the state of the Assistant with the provided ID.</returns>
148 public virtual async Task<ClientResult<Assistant>> GetAssistantAsync(string assistantId, CancellationToken cancellationToken = default)
149 {
150 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
151
152 ClientResult protocolResult = await GetAssistantAsync(assistantId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
153 return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
154 }
155
156 /// <summary>
157 /// Gets an instance representing an existing <see cref="Assistant"/> based on its ID.
158 /// </summary>
159 /// <param name="assistantId"> The ID of the Assistant to retrieve. </param>
160 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
161 /// <returns>An <see cref="Assistant"/> instance representing the state of the Assistant with the provided ID.</returns>
162 public virtual ClientResult<Assistant> GetAssistant(string assistantId, CancellationToken cancellationToken = default)
163 {
164 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
165
166 ClientResult protocolResult = GetAssistant(assistantId, cancellationToken.ToRequestOptions());
167 return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
168 }
169
170 /// <summary>
171 /// Modifies an existing <see cref="Assistant"/>.
172 /// </summary>
173 /// <param name="assistantId"> The ID of the Assistant to retrieve. </param>
174 /// <param name="options"> The new options to apply to the existing Assistant. </param>
175 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
176 /// <returns> An updated <see cref="Assistant"/> instance representing the state of the Assistant with the provided ID. </returns>
177 public virtual async Task<ClientResult<Assistant>> ModifyAssistantAsync(string assistantId, AssistantModificationOptions options, CancellationToken cancellationToken = default)
178 {
179 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
180 Argument.AssertNotNull(options, nameof(options));
181
182 using BinaryContent content = options?.ToBinaryContent();
183 ClientResult protocolResult
184 = await ModifyAssistantAsync(assistantId, content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
185 return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
186 }
187
188 /// <summary>
189 /// Modifies an existing <see cref="Assistant"/>.
190 /// </summary>
191 /// <param name="assistantId"> The ID of the Assistant to retrieve. </param>
192 /// <param name="options"> The new options to apply to the existing Assistant. </param>
193 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
194 /// <returns> An updated <see cref="Assistant"/> instance representing the state of the Assistant with the provided ID. </returns>
195 public virtual ClientResult<Assistant> ModifyAssistant(string assistantId, AssistantModificationOptions options, CancellationToken cancellationToken = default)
196 {
197 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
198 Argument.AssertNotNull(options, nameof(options));
199
200 using BinaryContent content = options?.ToBinaryContent();
201 ClientResult protocolResult = ModifyAssistant(assistantId, content, null);
202 return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
203 }
204
205 /// <summary>
206 /// Deletes an existing <see cref="Assistant"/>.
207 /// </summary>
208 /// <param name="assistantId"> The ID of the assistant to delete. </param>
209 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
210 /// <returns> A <see cref="AssistantDeletionResult"/> instance. </returns>
211 public virtual async Task<ClientResult<AssistantDeletionResult>> DeleteAssistantAsync(string assistantId, CancellationToken cancellationToken = default)
212 {
213 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
214
215 ClientResult protocolResult = await DeleteAssistantAsync(assistantId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
216 return ClientResult.FromValue((AssistantDeletionResult)protocolResult, protocolResult.GetRawResponse());
217 }
218
219 /// <summary>
220 /// Deletes an existing <see cref="Assistant"/>.
221 /// </summary>
222 /// <param name="assistantId"> The ID of the assistant to delete. </param>
223 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
224 /// <returns> A <see cref="AssistantDeletionResult"/> instance. </returns>
225 public virtual ClientResult<AssistantDeletionResult> DeleteAssistant(string assistantId, CancellationToken cancellationToken = default)
226 {
227 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
228
229 ClientResult protocolResult = DeleteAssistant(assistantId, cancellationToken.ToRequestOptions());
230 return ClientResult.FromValue((AssistantDeletionResult)protocolResult, protocolResult.GetRawResponse());
231 }
232
233 /// <summary>
234 /// Creates a new <see cref="AssistantThread"/>.
235 /// </summary>
236 /// <param name="options"> Additional options to use when creating the thread. </param>
237 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
238 /// <returns> A new thread. </returns>
239 public virtual async Task<ClientResult<AssistantThread>> CreateThreadAsync(ThreadCreationOptions options = null, CancellationToken cancellationToken = default)
240 {
241 ClientResult protocolResult = await CreateThreadAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
242 return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
243 }
244
245 /// <summary>
246 /// Creates a new <see cref="AssistantThread"/>.
247 /// </summary>
248 /// <param name="options"> Additional options to use when creating the thread. </param>
249 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
250 /// <returns> A new thread. </returns>
251 public virtual ClientResult<AssistantThread> CreateThread(ThreadCreationOptions options = null, CancellationToken cancellationToken = default)
252 {
253 ClientResult protocolResult = CreateThread(options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
254 return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
255 }
256
257 /// <summary>
258 /// Gets an existing <see cref="AssistantThread"/>, retrieved via a known ID.
259 /// </summary>
260 /// <param name="threadId"> The ID of the thread to retrieve. </param>
261 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
262 /// <returns> The existing thread instance. </returns>
263 public virtual async Task<ClientResult<AssistantThread>> GetThreadAsync(string threadId, CancellationToken cancellationToken = default)
264 {
265 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
266
267 ClientResult protocolResult = await GetThreadAsync(threadId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
268 return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
269 }
270
271 /// <summary>
272 /// Gets an existing <see cref="AssistantThread"/>, retrieved via a known ID.
273 /// </summary>
274 /// <param name="threadId"> The ID of the thread to retrieve. </param>
275 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
276 /// <returns> The existing thread instance. </returns>
277 public virtual ClientResult<AssistantThread> GetThread(string threadId, CancellationToken cancellationToken = default)
278 {
279 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
280
281 ClientResult protocolResult = GetThread(threadId, cancellationToken.ToRequestOptions());
282 return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
283 }
284
285 /// <summary>
286 /// Modifies an existing <see cref="AssistantThread"/>.
287 /// </summary>
288 /// <param name="threadId"> The ID of the thread to modify. </param>
289 /// <param name="options"> The modifications to apply to the thread. </param>
290 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
291 /// <returns> The updated <see cref="AssistantThread"/> instance. </returns>
292 public virtual async Task<ClientResult<AssistantThread>> ModifyThreadAsync(string threadId, ThreadModificationOptions options, CancellationToken cancellationToken = default)
293 {
294 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
295 Argument.AssertNotNull(options, nameof(options));
296
297 ClientResult protocolResult = await ModifyThreadAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
298 return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
299 }
300
301 /// <summary>
302 /// Modifies an existing <see cref="AssistantThread"/>.
303 /// </summary>
304 /// <param name="threadId"> The ID of the thread to modify. </param>
305 /// <param name="options"> The modifications to apply to the thread. </param>
306 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
307 /// <returns> The updated <see cref="AssistantThread"/> instance. </returns>
308 public virtual ClientResult<AssistantThread> ModifyThread(string threadId, ThreadModificationOptions options, CancellationToken cancellationToken = default)
309 {
310 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
311 Argument.AssertNotNull(options, nameof(options));
312
313 ClientResult protocolResult = ModifyThread(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
314 return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());
315 }
316
317 /// <summary>
318 /// Deletes an existing <see cref="AssistantThread"/>.
319 /// </summary>
320 /// <param name="threadId"> The ID of the thread to delete. </param>
321 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
322 /// <returns> A <see cref="ThreadDeletionResult"/> instance. </returns>
323 public virtual async Task<ClientResult<ThreadDeletionResult>> DeleteThreadAsync(string threadId, CancellationToken cancellationToken = default)
324 {
325 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
326
327 ClientResult protocolResult = await DeleteThreadAsync(threadId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
328 return ClientResult.FromValue((ThreadDeletionResult)protocolResult, protocolResult.GetRawResponse());
329 }
330
331 /// <summary>
332 /// Deletes an existing <see cref="AssistantThread"/>.
333 /// </summary>
334 /// <param name="threadId"> The ID of the thread to delete. </param>
335 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
336 /// <returns> A <see cref="ThreadDeletionResult"/> instance. </returns>
337 public virtual ClientResult<ThreadDeletionResult> DeleteThread(string threadId, CancellationToken cancellationToken = default)
338 {
339 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
340
341 ClientResult protocolResult = DeleteThread(threadId, cancellationToken.ToRequestOptions());
342 return ClientResult.FromValue((ThreadDeletionResult)protocolResult, protocolResult.GetRawResponse());
343 }
344
345 /// <summary>
346 /// Creates a new <see cref="ThreadMessage"/> on an existing <see cref="AssistantThread"/>.
347 /// </summary>
348 /// <param name="threadId"> The ID of the thread to associate the new message with. </param>
349 /// <param name="role"> The role to associate with the new message. </param>
350 /// <param name="content"> The collection of <see cref="MessageContent"/> items for the message. </param>
351 /// <param name="options"> Additional options to apply to the new message. </param>
352 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
353 /// <returns> A new <see cref="ThreadMessage"/>. </returns>
354 public virtual async Task<ClientResult<ThreadMessage>> CreateMessageAsync(
355 string threadId,
356 MessageRole role,
357 IEnumerable<MessageContent> content,
358 MessageCreationOptions options = null,
359 CancellationToken cancellationToken = default)
360 {
361 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
362 options ??= new();
363 options.Role = role;
364 options.Content.Clear();
365 foreach (MessageContent contentItem in content)
366 {
367 options.Content.Add(contentItem);
368 }
369
370 ClientResult protocolResult = await CreateMessageAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions())
371 .ConfigureAwait(false);
372 return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
373 }
374
375 /// <summary>
376 /// Creates a new <see cref="ThreadMessage"/> on an existing <see cref="AssistantThread"/>.
377 /// </summary>
378 /// <param name="threadId"> The ID of the thread to associate the new message with. </param>
379 /// <param name="role"> The role to associate with the new message. </param>
380 /// <param name="content"> The collection of <see cref="MessageContent"/> items for the message. </param>
381 /// <param name="options"> Additional options to apply to the new message. </param>
382 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
383 /// <returns> A new <see cref="ThreadMessage"/>. </returns>
384 public virtual ClientResult<ThreadMessage> CreateMessage(
385 string threadId,
386 MessageRole role,
387 IEnumerable<MessageContent> content,
388 MessageCreationOptions options = null,
389 CancellationToken cancellationToken = default)
390 {
391 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
392 options ??= new();
393 options.Role = role;
394 options.Content.Clear();
395 foreach (MessageContent contentItem in content)
396 {
397 options.Content.Add(contentItem);
398 }
399
400 ClientResult protocolResult = CreateMessage(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
401 return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
402 }
403
404 /// <summary>
405 /// Gets a page collection of <see cref="ThreadMessage"/> instances from an existing <see cref="AssistantThread"/>.
406 /// </summary>
407 /// <param name="threadId"> The ID of the thread to list messages from. </param>
408 /// <param name="options"> Options describing the collection to return. </param>
409 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
410 /// <returns> A collection of <see cref="ThreadMessage"/>. </returns>
411 public virtual AsyncCollectionResult<ThreadMessage> GetMessagesAsync(
412 string threadId,
413 MessageCollectionOptions options = default,
414 CancellationToken cancellationToken = default)
415 => _messageSubClient.GetMessagesAsync(threadId, options, cancellationToken);
416
417 /// <summary>
418 /// Gets a page collection holding <see cref="ThreadMessage"/> instances from an existing <see cref="AssistantThread"/>.
419 /// </summary>
420 /// <param name="threadId"> The ID of the thread to list messages from. </param>
421 /// <param name="options"> Options describing the collection to return. </param>
422 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
423 /// <returns> A collection of <see cref="ThreadMessage"/>. </returns>
424 public virtual CollectionResult<ThreadMessage> GetMessages(
425 string threadId,
426 MessageCollectionOptions options = default,
427 CancellationToken cancellationToken = default)
428 => _messageSubClient.GetMessages(threadId, options, cancellationToken);
429
430 /// <summary>
431 /// Gets an existing <see cref="ThreadMessage"/> from a known <see cref="AssistantThread"/>.
432 /// </summary>
433 /// <param name="threadId"> The ID of the thread to retrieve the message from. </param>
434 /// <param name="messageId"> The ID of the message to retrieve. </param>
435 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
436 /// <returns> The existing <see cref="ThreadMessage"/> instance. </returns>
437 public virtual async Task<ClientResult<ThreadMessage>> GetMessageAsync(string threadId, string messageId, CancellationToken cancellationToken = default)
438 {
439 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
440 Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
441
442 ClientResult protocolResult = await GetMessageAsync(threadId, messageId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
443 return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
444 }
445
446 /// <summary>
447 /// Gets an existing <see cref="ThreadMessage"/> from a known <see cref="AssistantThread"/>.
448 /// </summary>
449 /// <param name="threadId"> The ID of the thread to retrieve the message from. </param>
450 /// <param name="messageId"> The ID of the message to retrieve. </param>
451 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
452 /// <returns> The existing <see cref="ThreadMessage"/> instance. </returns>
453 public virtual ClientResult<ThreadMessage> GetMessage(string threadId, string messageId, CancellationToken cancellationToken = default)
454 {
455 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
456 Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
457
458 ClientResult protocolResult = GetMessage(threadId, messageId, cancellationToken.ToRequestOptions());
459 return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
460 }
461
462 /// <summary>
463 /// Modifies an existing <see cref="ThreadMessage"/>.
464 /// </summary>
465 /// <param name="threadId"> The ID of the thread associated with the message to modify. </param>
466 /// <param name="messageId"> The ID of the message to modify. </param>
467 /// <param name="options"> The changes to apply to the message. </param>
468 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
469 /// <returns> The updated <see cref="ThreadMessage"/>. </returns>
470 public virtual async Task<ClientResult<ThreadMessage>> ModifyMessageAsync(string threadId, string messageId, MessageModificationOptions options, CancellationToken cancellationToken = default)
471 {
472 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
473 Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
474 Argument.AssertNotNull(options, nameof(options));
475
476 ClientResult protocolResult = await ModifyMessageAsync(threadId, messageId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions())
477 .ConfigureAwait(false);
478 return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
479 }
480
481 /// <summary>
482 /// Modifies an existing <see cref="ThreadMessage"/>.
483 /// </summary>
484 /// <param name="threadId"> The ID of the thread associated with the message to modify. </param>
485 /// <param name="messageId"> The ID of the message to modify. </param>
486 /// <param name="options"> The changes to apply to the message. </param>
487 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
488 /// <returns> The updated <see cref="ThreadMessage"/>. </returns>
489 public virtual ClientResult<ThreadMessage> ModifyMessage(string threadId, string messageId, MessageModificationOptions options, CancellationToken cancellationToken = default)
490 {
491 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
492 Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
493 Argument.AssertNotNull(options, nameof(options));
494
495 ClientResult protocolResult = ModifyMessage(threadId, messageId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
496 return ClientResult.FromValue((ThreadMessage)protocolResult, protocolResult.GetRawResponse());
497 }
498
499 /// <summary>
500 /// Deletes an existing <see cref="ThreadMessage"/>.
501 /// </summary>
502 /// <param name="threadId"> The ID of the thread associated with the message. </param>
503 /// <param name="messageId"> The ID of the message. </param>
504 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
505 /// <returns> A <see cref="MessageDeletionResult"/> instance. </returns>
506 public virtual async Task<ClientResult<MessageDeletionResult>> DeleteMessageAsync(string threadId, string messageId, CancellationToken cancellationToken = default)
507 {
508 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
509 Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
510
511 ClientResult protocolResult = await DeleteMessageAsync(threadId, messageId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
512 return ClientResult.FromValue((MessageDeletionResult)protocolResult, protocolResult.GetRawResponse());
513 }
514
515 /// <summary>
516 /// Deletes an existing <see cref="ThreadMessage"/>.
517 /// </summary>
518 /// <param name="threadId"> The ID of the thread associated with the message. </param>
519 /// <param name="messageId"> The ID of the message. </param>
520 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
521 /// <returns> A <see cref="MessageDeletionResult"/> instance. </returns>
522 public virtual ClientResult<MessageDeletionResult> DeleteMessage(string threadId, string messageId, CancellationToken cancellationToken = default)
523 {
524 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
525 Argument.AssertNotNullOrEmpty(messageId, nameof(messageId));
526
527 ClientResult protocolResult = DeleteMessage(threadId, messageId, cancellationToken.ToRequestOptions());
528 return ClientResult.FromValue((MessageDeletionResult)protocolResult, protocolResult.GetRawResponse());
529 }
530
531 /// <summary>
532 /// Begins a new <see cref="ThreadRun"/> that evaluates a <see cref="AssistantThread"/> using a specified
533 /// <see cref="Assistant"/>.
534 /// </summary>
535 /// <param name="threadId"> The ID of the thread that the run should evaluate. </param>
536 /// <param name="assistantId"> The ID of the assistant that should be used when evaluating the thread. </param>
537 /// <param name="options"> Additional options for the run. </param>
538 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
539 /// <returns> A new <see cref="ThreadRun"/> instance. </returns>
540 public virtual async Task<ClientResult<ThreadRun>> CreateRunAsync(string threadId, string assistantId, RunCreationOptions options = null, CancellationToken cancellationToken = default)
541 {
542 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
543 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
544 options ??= new();
545 options.AssistantId = assistantId;
546 options.Stream = null;
547
548 ClientResult protocolResult = await CreateRunAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions())
549 .ConfigureAwait(false);
550 return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
551 }
552
553 /// <summary>
554 /// Begins a new <see cref="ThreadRun"/> that evaluates a <see cref="AssistantThread"/> using a specified
555 /// <see cref="Assistant"/>.
556 /// </summary>
557 /// <param name="threadId"> The ID of the thread that the run should evaluate. </param>
558 /// <param name="assistantId"> The ID of the assistant that should be used when evaluating the thread. </param>
559 /// <param name="options"> Additional options for the run. </param>
560 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
561 /// <returns> A new <see cref="ThreadRun"/> instance. </returns>
562 public virtual ClientResult<ThreadRun> CreateRun(string threadId, string assistantId, RunCreationOptions options = null, CancellationToken cancellationToken = default)
563 {
564 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
565 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
566 options ??= new();
567 options.AssistantId = assistantId;
568 options.Stream = null;
569
570 ClientResult protocolResult = CreateRun(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
571 return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
572 }
573
574 /// <summary>
575 /// Begins a new streaming <see cref="ThreadRun"/> that evaluates a <see cref="AssistantThread"/> using a specified
576 /// <see cref="Assistant"/>.
577 /// </summary>
578 /// <param name="threadId"> The ID of the thread that the run should evaluate. </param>
579 /// <param name="assistantId"> The ID of the assistant that should be used when evaluating the thread. </param>
580 /// <param name="options"> Additional options for the run. </param>
581 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
582 public virtual AsyncCollectionResult<StreamingUpdate> CreateRunStreamingAsync(
583 string threadId,
584 string assistantId,
585 RunCreationOptions options = null,
586 CancellationToken cancellationToken = default)
587 {
588 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
589 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
590
591 options ??= new();
592 options.AssistantId = assistantId;
593 options.Stream = true;
594
595 return new AsyncSseUpdateCollection<StreamingUpdate>(
596 async () => await CreateRunAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
597 StreamingUpdate.FromSseItem,
598 cancellationToken);
599 }
600
601 /// <summary>
602 /// Begins a new streaming <see cref="ThreadRun"/> that evaluates a <see cref="AssistantThread"/> using a specified
603 /// <see cref="Assistant"/>.
604 /// </summary>
605 /// <param name="threadId"> The ID of the thread that the run should evaluate. </param>
606 /// <param name="assistantId"> The ID of the assistant that should be used when evaluating the thread. </param>
607 /// <param name="options"> Additional options for the run. </param>
608 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
609 public virtual CollectionResult<StreamingUpdate> CreateRunStreaming(
610 string threadId,
611 string assistantId,
612 RunCreationOptions options = null,
613 CancellationToken cancellationToken = default)
614 {
615 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
616 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
617
618 options ??= new();
619 options.AssistantId = assistantId;
620 options.Stream = true;
621
622 return new SseUpdateCollection<StreamingUpdate>(
623 () => CreateRun(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions(streaming: true)),
624 StreamingUpdate.FromSseItem,
625 cancellationToken);
626 }
627
628 /// <summary>
629 /// Creates a new thread and immediately begins a run against it using the specified <see cref="Assistant"/>.
630 /// </summary>
631 /// <param name="assistantId"> The ID of the assistant that the new run should use. </param>
632 /// <param name="threadOptions"> Options for the new thread that will be created. </param>
633 /// <param name="runOptions"> Additional options to apply to the run that will begin. </param>
634 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
635 /// <returns> A new <see cref="ThreadRun"/>. </returns>
636 public virtual async Task<ClientResult<ThreadRun>> CreateThreadAndRunAsync(
637 string assistantId,
638 ThreadCreationOptions threadOptions = null,
639 RunCreationOptions runOptions = null,
640 CancellationToken cancellationToken = default)
641 {
642 runOptions ??= new();
643 runOptions.Stream = null;
644 BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions);
645 ClientResult protocolResult = await CreateThreadAndRunAsync(protocolContent, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
646 return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
647 }
648
649 /// <summary>
650 /// Creates a new thread and immediately begins a run against it using the specified <see cref="Assistant"/>.
651 /// </summary>
652 /// <param name="assistantId"> The ID of the assistant that the new run should use. </param>
653 /// <param name="threadOptions"> Options for the new thread that will be created. </param>
654 /// <param name="runOptions"> Additional options to apply to the run that will begin. </param>
655 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
656 /// <returns> A new <see cref="ThreadRun"/>. </returns>
657 public virtual ClientResult<ThreadRun> CreateThreadAndRun(
658 string assistantId,
659 ThreadCreationOptions threadOptions = null,
660 RunCreationOptions runOptions = null,
661 CancellationToken cancellationToken = default)
662 {
663 runOptions ??= new();
664 runOptions.Stream = null;
665 BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions);
666 ClientResult protocolResult = CreateThreadAndRun(protocolContent, cancellationToken.ToRequestOptions());
667 return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
668 }
669
670 /// <summary>
671 /// Creates a new thread and immediately begins a streaming run against it using the specified <see cref="Assistant"/>.
672 /// </summary>
673 /// <param name="assistantId"> The ID of the assistant that the new run should use. </param>
674 /// <param name="threadOptions"> Options for the new thread that will be created. </param>
675 /// <param name="runOptions"> Additional options to apply to the run that will begin. </param>
676 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
677 public virtual AsyncCollectionResult<StreamingUpdate> CreateThreadAndRunStreamingAsync(
678 string assistantId,
679 ThreadCreationOptions threadOptions = null,
680 RunCreationOptions runOptions = null,
681 CancellationToken cancellationToken = default)
682 {
683 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
684
685 runOptions ??= new();
686 runOptions.Stream = true;
687 BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions);
688
689 return new AsyncSseUpdateCollection<StreamingUpdate>(
690 async () => await CreateThreadAndRunAsync(protocolContent, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
691 StreamingUpdate.FromSseItem,
692 cancellationToken);
693 }
694
695 /// <summary>
696 /// Creates a new thread and immediately begins a streaming run against it using the specified <see cref="Assistant"/>.
697 /// </summary>
698 /// <param name="assistantId"> The ID of the assistant that the new run should use. </param>
699 /// <param name="threadOptions"> Options for the new thread that will be created. </param>
700 /// <param name="runOptions"> Additional options to apply to the run that will begin. </param>
701 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
702 public virtual CollectionResult<StreamingUpdate> CreateThreadAndRunStreaming(
703 string assistantId,
704 ThreadCreationOptions threadOptions = null,
705 RunCreationOptions runOptions = null,
706 CancellationToken cancellationToken = default)
707 {
708 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
709
710 runOptions ??= new();
711 runOptions.Stream = true;
712 BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions);
713
714 return new SseUpdateCollection<StreamingUpdate>(
715 () => CreateThreadAndRun(protocolContent, cancellationToken.ToRequestOptions(streaming: true)),
716 StreamingUpdate.FromSseItem,
717 cancellationToken);
718 }
719
720 /// <summary>
721 /// Gets a page collection holding <see cref="ThreadRun"/> instances associated with an existing <see cref="AssistantThread"/>.
722 /// </summary>
723 /// <param name="threadId"> The ID of the thread that runs in the list should be associated with. </param>
724 /// <param name="options"> Options describing the collection to return. </param>
725 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
726 /// <returns> A collection of <see cref="ThreadRun"/>. </returns>
727 public virtual AsyncCollectionResult<ThreadRun> GetRunsAsync(
728 string threadId,
729 RunCollectionOptions options = default,
730 CancellationToken cancellationToken = default)
731 => _runSubClient.GetRunsAsync(threadId, options, cancellationToken);
732
733 /// <summary>
734 /// Gets a page collection holding <see cref="ThreadRun"/> instances associated with an existing <see cref="AssistantThread"/>.
735 /// </summary>
736 /// <param name="threadId"> The ID of the thread that runs in the list should be associated with. </param>
737 /// <param name="options"> Options describing the collection to return. </param>
738 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
739 /// <returns> A collection of <see cref="ThreadRun"/>. </returns>
740 public virtual CollectionResult<ThreadRun> GetRuns(
741 string threadId,
742 RunCollectionOptions options = default,
743 CancellationToken cancellationToken = default)
744 => _runSubClient.GetRuns(threadId, options, cancellationToken);
745
746 /// <summary>
747 /// Gets an existing <see cref="ThreadRun"/> from a known <see cref="AssistantThread"/>.
748 /// </summary>
749 /// <param name="threadId"> The ID of the thread to retrieve the run from. </param>
750 /// <param name="runId"> The ID of the run to retrieve. </param>
751 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
752 /// <returns> The existing <see cref="ThreadRun"/> instance. </returns>
753 public virtual async Task<ClientResult<ThreadRun>> GetRunAsync(string threadId, string runId, CancellationToken cancellationToken = default)
754 => await _runSubClient.GetRunAsync(threadId, runId, cancellationToken);
755
756 /// <summary>
757 /// Gets an existing <see cref="ThreadRun"/> from a known <see cref="AssistantThread"/>.
758 /// </summary>
759 /// <param name="threadId"> The ID of the thread to retrieve the run from. </param>
760 /// <param name="runId"> The ID of the run to retrieve. </param>
761 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
762 /// <returns> The existing <see cref="ThreadRun"/> instance. </returns>
763 public virtual ClientResult<ThreadRun> GetRun(string threadId, string runId, CancellationToken cancellationToken = default)
764 => _runSubClient.GetRun(threadId, runId, cancellationToken);
765
766 /// <summary>
767 /// Submits a collection of required tool call outputs to a run and resumes the run.
768 /// </summary>
769 /// <param name="threadId"> The thread ID of the thread being run. </param>
770 /// <param name="runId"> The ID of the run that reached a <c>requires_action</c> status. </param>
771 /// <param name="toolOutputs">
772 /// The tool outputs, corresponding to <see cref="InternalRequiredToolCall"/> instances from the run.
773 /// </param>
774 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
775 /// <returns> The <see cref="ThreadRun"/>, updated after the submission was processed. </returns>
776 public virtual async Task<ClientResult<ThreadRun>> SubmitToolOutputsToRunAsync(
777 string threadId,
778 string runId,
779 IEnumerable<ToolOutput> toolOutputs,
780 CancellationToken cancellationToken = default)
781 {
782 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
783 Argument.AssertNotNullOrEmpty(runId, nameof(runId));
784
785 var submitToolOutputsRunRequest = new InternalSubmitToolOutputsRunRequest(toolOutputs);
786 using BinaryContent content = BinaryContent.Create(submitToolOutputsRunRequest, ModelSerializationExtensions.WireOptions);
787 ClientResult protocolResult = await SubmitToolOutputsToRunAsync(threadId, runId, content, cancellationToken.ToRequestOptions())
788 .ConfigureAwait(false);
789 return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
790 }
791
792 /// <summary>
793 /// Submits a collection of required tool call outputs to a run and resumes the run.
794 /// </summary>
795 /// <param name="threadId"> The thread ID of the thread being run. </param>
796 /// <param name="runId"> The ID of the run that reached a <c>requires_action</c> status. </param>
797 /// <param name="toolOutputs">
798 /// The tool outputs, corresponding to <see cref="InternalRequiredToolCall"/> instances from the run.
799 /// </param>
800 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
801 /// <returns> The <see cref="ThreadRun"/>, updated after the submission was processed. </returns>
802 public virtual ClientResult<ThreadRun> SubmitToolOutputsToRun(
803 string threadId,
804 string runId,
805 IEnumerable<ToolOutput> toolOutputs,
806 CancellationToken cancellationToken = default)
807 {
808 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
809 Argument.AssertNotNullOrEmpty(runId, nameof(runId));
810
811 var submitToolOutputsRunRequest = new InternalSubmitToolOutputsRunRequest(toolOutputs);
812 using BinaryContent content = BinaryContent.Create(submitToolOutputsRunRequest, ModelSerializationExtensions.WireOptions);
813 ClientResult protocolResult = SubmitToolOutputsToRun(threadId, runId, content, cancellationToken.ToRequestOptions());
814 return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
815 }
816
817 /// <summary>
818 /// Submits a collection of required tool call outputs to a run and resumes the run with streaming enabled.
819 /// </summary>
820 /// <param name="threadId"> The thread ID of the thread being run. </param>
821 /// <param name="runId"> The ID of the run that reached a <c>requires_action</c> status. </param>
822 /// <param name="toolOutputs">
823 /// The tool outputs, corresponding to <see cref="InternalRequiredToolCall"/> instances from the run.
824 /// </param>
825 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
826 public virtual AsyncCollectionResult<StreamingUpdate> SubmitToolOutputsToRunStreamingAsync(
827 string threadId,
828 string runId,
829 IEnumerable<ToolOutput> toolOutputs,
830 CancellationToken cancellationToken = default)
831 {
832 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
833 Argument.AssertNotNullOrEmpty(runId, nameof(runId));
834
835 var submitToolOutputsRunRequest = new InternalSubmitToolOutputsRunRequest(toolOutputs.ToList(), stream: true, null);
836 using BinaryContent content = BinaryContent.Create(submitToolOutputsRunRequest, ModelSerializationExtensions.WireOptions);
837
838 return new AsyncSseUpdateCollection<StreamingUpdate>(
839 async () => await SubmitToolOutputsToRunAsync(threadId, runId, content, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
840 StreamingUpdate.FromSseItem,
841 cancellationToken);
842 }
843
844 /// <summary>
845 /// Submits a collection of required tool call outputs to a run and resumes the run with streaming enabled.
846 /// </summary>
847 /// <param name="threadId"> The thread ID of the thread being run. </param>
848 /// <param name="runId"> The ID of the run that reached a <c>requires_action</c> status. </param>
849 /// <param name="toolOutputs">
850 /// The tool outputs, corresponding to <see cref="InternalRequiredToolCall"/> instances from the run.
851 /// </param>
852 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
853 public virtual CollectionResult<StreamingUpdate> SubmitToolOutputsToRunStreaming(
854 string threadId,
855 string runId,
856 IEnumerable<ToolOutput> toolOutputs,
857 CancellationToken cancellationToken = default)
858 {
859 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
860 Argument.AssertNotNullOrEmpty(runId, nameof(runId));
861
862 var submitToolOutputsRunRequest = new InternalSubmitToolOutputsRunRequest(toolOutputs.ToList(), stream: true, null);
863 using BinaryContent content = BinaryContent.Create(submitToolOutputsRunRequest, ModelSerializationExtensions.WireOptions);
864
865 return new SseUpdateCollection<StreamingUpdate>(
866 () => SubmitToolOutputsToRun(threadId, runId, content, cancellationToken.ToRequestOptions(streaming: true)),
867 StreamingUpdate.FromSseItem,
868 cancellationToken);
869 }
870
871 /// <summary>
872 /// Cancels an in-progress <see cref="ThreadRun"/>.
873 /// </summary>
874 /// <param name="threadId"> The ID of the thread associated with the run. </param>
875 /// <param name="runId"> The ID of the run to cancel. </param>
876 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
877 /// <returns> An updated <see cref="ThreadRun"/> instance, reflecting the new status of the run. </returns>
878 public virtual async Task<ClientResult<ThreadRun>> CancelRunAsync(string threadId, string runId, CancellationToken cancellationToken = default)
879 {
880 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
881 Argument.AssertNotNullOrEmpty(runId, nameof(runId));
882
883 ClientResult protocolResult = await CancelRunAsync(threadId, runId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
884 return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
885 }
886
887 /// <summary>
888 /// Cancels an in-progress <see cref="ThreadRun"/>.
889 /// </summary>
890 /// <param name="threadId"> The ID of the thread associated with the run. </param>
891 /// <param name="runId"> The ID of the run to cancel. </param>
892 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
893 /// <returns> An updated <see cref="ThreadRun"/> instance, reflecting the new status of the run. </returns>
894 public virtual ClientResult<ThreadRun> CancelRun(string threadId, string runId, CancellationToken cancellationToken = default)
895 {
896 Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
897 Argument.AssertNotNullOrEmpty(runId, nameof(runId));
898
899 ClientResult protocolResult = CancelRun(threadId, runId, cancellationToken.ToRequestOptions());
900 return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
901 }
902
903 /// <summary>
904 /// Gets a page collection holding <see cref="RunStep"/> instances associated with a <see cref="ThreadRun"/>.
905 /// </summary>
906 /// <param name="threadId"> The ID of the thread associated with the run. </param>
907 /// <param name="runId"> The ID of the run to list run steps from. </param>
908 /// <param name="options"></param>
909 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
910 /// <returns> A collection of <see cref="RunStep"/>. </returns>
911 public virtual AsyncCollectionResult<RunStep> GetRunStepsAsync(
912 string threadId,
913 string runId,
914 RunStepCollectionOptions options = default,
915 CancellationToken cancellationToken = default)
916 => _runSubClient.GetRunStepsAsync(threadId, runId, options, [InternalIncludedRunStepProperty.FileSearchResultContent], cancellationToken);
917
918 /// <summary>
919 /// Gets a page collection holding <see cref="RunStep"/> instances associated with a <see cref="ThreadRun"/>.
920 /// </summary>
921 /// <param name="threadId"> The ID of the thread associated with the run. </param>
922 /// <param name="runId"> The ID of the run to list run steps from. </param>
923 /// <param name="options"></param>
924 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
925 /// <returns> A collection of <see cref="RunStep"/>. </returns>
926 public virtual CollectionResult<RunStep> GetRunSteps(
927 string threadId,
928 string runId,
929 RunStepCollectionOptions options = default,
930 CancellationToken cancellationToken = default)
931 => _runSubClient.GetRunSteps(threadId, runId, options, [InternalIncludedRunStepProperty.FileSearchResultContent], cancellationToken);
932
933 /// <summary>
934 /// Gets a single run step from a run.
935 /// </summary>
936 /// <param name="threadId"> The ID of the thread associated with the run. </param>
937 /// <param name="runId"> The ID of the run. </param>
938 /// <param name="stepId"> The ID of the run step. </param>
939 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
940 /// <returns> A <see cref="RunStep"/> instance corresponding to the specified step. </returns>
941 public virtual async Task<ClientResult<RunStep>> GetRunStepAsync(string threadId, string runId, string stepId, CancellationToken cancellationToken = default)
942 => await _runSubClient.GetRunStepAsync(threadId, runId, stepId, [InternalIncludedRunStepProperty.FileSearchResultContent], cancellationToken).ConfigureAwait(false);
943
944 /// <summary>
945 /// Gets a single run step from a run.
946 /// </summary>
947 /// <param name="threadId"> The ID of the thread associated with the run. </param>
948 /// <param name="runId"> The ID of the run. </param>
949 /// <param name="stepId"> The ID of the run step. </param>
950 /// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
951 /// <returns> A <see cref="RunStep"/> instance corresponding to the specified step. </returns>
952 public virtual ClientResult<RunStep> GetRunStep(string threadId, string runId, string stepId, CancellationToken cancellationToken = default)
953 => _runSubClient.GetRunStep(threadId, runId, stepId, [InternalIncludedRunStepProperty.FileSearchResultContent], cancellationToken);
954
955 private static BinaryContent CreateThreadAndRunProtocolContent(
956 string assistantId,
957 ThreadCreationOptions threadOptions,
958 RunCreationOptions runOptions)
959 {
960 Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId));
961 InternalCreateThreadAndRunRequest internalRequest = new(
962 assistantId: assistantId,
963 thread: threadOptions,
964 instructions: runOptions.InstructionsOverride,
965 tools: runOptions.ToolsOverride,
966 metadata: runOptions.Metadata,
967 temperature: runOptions.Temperature,
968 // TODO: reconcile exposure of the the two different tool_resources, if needed
969 topP: runOptions.NucleusSamplingFactor,
970 stream: runOptions.Stream,
971 maxPromptTokens: runOptions.MaxInputTokenCount,
972 maxCompletionTokens: runOptions.MaxOutputTokenCount,
973 truncationStrategy: runOptions.TruncationStrategy,
974 parallelToolCalls: runOptions.AllowParallelToolCalls,
975 model: runOptions.ModelOverride,
976 toolResources: threadOptions.ToolResources,
977 responseFormat: runOptions.ResponseFormat,
978 toolChoice: runOptions.ToolConstraint,
979 additionalBinaryDataProperties: null);
980 return BinaryContent.Create(internalRequest, ModelSerializationExtensions.WireOptions);
981 }
982
983 [MethodImpl(MethodImplOptions.AggressiveInlining)]
984 private static ClientResult<T> CreateResultFromProtocol<T>(ClientResult protocolResult, Func<PipelineResponse, T> responseDeserializer)
985 {
986 PipelineResponse pipelineResponse = protocolResult?.GetRawResponse();
987 T deserializedResultValue = responseDeserializer.Invoke(pipelineResponse);
988 return ClientResult.FromValue(deserializedResultValue, pipelineResponse);
989 }
990}
991