Skip to content

Commit 4b0727d

Browse files
committed
messages
1 parent d318fa8 commit 4b0727d

23 files changed

+10751
-10346
lines changed

codegen/generator/src/Visitors/PaginationVisitor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public class PaginationVisitor : ScmLibraryVisitor
7878
"GetAssistantsAsync",
7979
("Assistant", "AssistantCollectionOptions", _paginationParamsToReplace)
8080
},
81+
{
82+
"GetMessages",
83+
("ThreadMessage", "MessageCollectionOptions", _paginationParamsToReplace)
84+
},
85+
{
86+
"GetMessagesAsync",
87+
("ThreadMessage", "MessageCollectionOptions", _paginationParamsToReplace)
88+
},
8189
};
8290

8391
protected override MethodProvider? VisitMethod(MethodProvider method)

specification/base/typespec/messages/models.tsp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ model ListMessagesResponse {
5555
// Tool customization: add a clear enum enforcement of constrained 'object' label
5656
object: "list";
5757

58-
data: MessageObject[];
58+
@pageItems data: MessageObject[];
5959
first_id: string;
60-
last_id: string;
60+
@continuationToken last_id: string;
6161
has_more: boolean;
6262
}
6363

specification/base/typespec/messages/operations.tsp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface Messages {
2929
@operationId("listMessages")
3030
@tag("Assistants")
3131
@summary("Returns a list of messages for a given thread.")
32+
@list
3233
listMessages(
3334
...AcceptJsonHeader,
3435
...AssistantsBetaHeader,

specification/client/models/assistants.models.tsp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,28 @@ model AssistantCollectionOptions {
2828
...CollectionLimitQueryParameter,
2929
...AssistantCollectionOrderQueryParameter,
3030
}
31+
32+
33+
34+
alias MessageCollectionOrderQueryParameter = {
35+
/**
36+
* Sort order by the `created_at` timestamp of the objects. `asc` for ascending order and`desc`
37+
* for descending order.
38+
*/
39+
@query order?: MessageCollectionOrder;
40+
};
41+
42+
union MessageCollectionOrder {
43+
string,
44+
Ascending: "asc",
45+
Descending: "desc",
46+
}
47+
48+
@access(Access.public)
49+
@usage(Usage.input)
50+
model MessageCollectionOptions {
51+
...CollectionAfterQueryParameter,
52+
...CollectionBeforeQueryParameter,
53+
...CollectionLimitQueryParameter,
54+
...MessageCollectionOrderQueryParameter,
55+
}

src/Custom/Assistants/AssistantClient.Protocol.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ public virtual ClientResult CreateMessage(string threadId, BinaryContent content
4444
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
4545
/// <returns> A collection of service responses, each holding a page of values. </returns>
4646
public virtual AsyncCollectionResult GetMessagesAsync(string threadId, int? limit, string order, string after, string before, RequestOptions options)
47-
{
48-
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
49-
50-
return new AsyncMessageCollectionResult(_messageSubClient, options, threadId, limit, order, after, before);
51-
}
47+
=> _messageSubClient.GetMessagesAsync(threadId, limit, order, after, before, options);
5248

5349
/// <summary>
5450
/// [Protocol Method] Returns a paginated collection of messages for a given thread.
@@ -78,11 +74,7 @@ public virtual AsyncCollectionResult GetMessagesAsync(string threadId, int? limi
7874
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
7975
/// <returns> A collection of service responses, each holding a page of values. </returns>
8076
public virtual CollectionResult GetMessages(string threadId, int? limit, string order, string after, string before, RequestOptions options)
81-
{
82-
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
83-
84-
return new MessageCollectionResult(_messageSubClient, options, threadId, limit, order, after, before);
85-
}
77+
=> _messageSubClient.GetMessages(threadId, limit, order, after, before, options);
8678

8779
/// <inheritdoc cref="InternalAssistantMessageClient.GetMessageAsync"/>
8880
public virtual Task<ClientResult> GetMessageAsync(string threadId, string messageId, RequestOptions options)

src/Custom/Assistants/AssistantClient.cs

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public virtual async Task<ClientResult<Assistant>> CreateAssistantAsync(string m
112112
options ??= new();
113113
options.Model = model;
114114

115-
ClientResult protocolResult = await CreateAssistantAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
115+
ClientResult protocolResult = await CreateAssistantAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
116116
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
117117
}
118118

@@ -128,7 +128,7 @@ public virtual ClientResult<Assistant> CreateAssistant(string model, AssistantCr
128128
options.Model = model;
129129

130130
ClientResult protocolResult = CreateAssistant(options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
131-
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
131+
return ClientResult.FromValue((Assistant)protocolResult, protocolResult.GetRawResponse());
132132
}
133133

134134
/// <summary>
@@ -231,7 +231,7 @@ public virtual ClientResult<AssistantDeletionResult> DeleteAssistant(string assi
231231
public virtual async Task<ClientResult<AssistantThread>> CreateThreadAsync(ThreadCreationOptions options = null, CancellationToken cancellationToken = default)
232232
{
233233
ClientResult protocolResult = await CreateThreadAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false);
234-
return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse());;
234+
return ClientResult.FromValue((AssistantThread)protocolResult, protocolResult.GetRawResponse()); ;
235235
}
236236

237237
/// <summary>
@@ -404,41 +404,7 @@ public virtual AsyncCollectionResult<ThreadMessage> GetMessagesAsync(
404404
string threadId,
405405
MessageCollectionOptions options = default,
406406
CancellationToken cancellationToken = default)
407-
{
408-
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
409-
410-
AsyncCollectionResult result = GetMessagesAsync(threadId, options?.PageSizeLimit, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions());
411-
412-
if (result is not AsyncCollectionResult<ThreadMessage> collection)
413-
{
414-
throw new InvalidOperationException("Failed to cast protocol return type to expected collection type 'AsyncCollectionResult<ThreadMessage>'.");
415-
}
416-
417-
return collection;
418-
}
419-
420-
/// <summary>
421-
/// Rehydrates a page collection of <see cref="ThreadMessage"/> instances from a page token.
422-
/// </summary>
423-
/// <param name="firstPageToken"> Page token corresponding to the first page of the collection to rehydrate. </param>
424-
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
425-
/// <returns> A collection of <see cref="ThreadMessage"/>. </returns>
426-
public virtual AsyncCollectionResult<ThreadMessage> GetMessagesAsync(
427-
ContinuationToken firstPageToken,
428-
CancellationToken cancellationToken = default)
429-
{
430-
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
431-
432-
MessageCollectionPageToken pageToken = MessageCollectionPageToken.FromToken(firstPageToken);
433-
AsyncCollectionResult result = GetMessagesAsync(pageToken?.ThreadId, pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken?.Before, cancellationToken.ToRequestOptions());
434-
435-
if (result is not AsyncCollectionResult<ThreadMessage> collection)
436-
{
437-
throw new InvalidOperationException("Failed to cast protocol return type to expected collection type 'AsyncCollectionResult<ThreadMessage>'.");
438-
}
439-
440-
return collection;
441-
}
407+
=> _messageSubClient.GetMessagesAsync(threadId, options, cancellationToken);
442408

443409
/// <summary>
444410
/// Gets a page collection holding <see cref="ThreadMessage"/> instances from an existing <see cref="AssistantThread"/>.
@@ -451,42 +417,7 @@ public virtual CollectionResult<ThreadMessage> GetMessages(
451417
string threadId,
452418
MessageCollectionOptions options = default,
453419
CancellationToken cancellationToken = default)
454-
{
455-
Argument.AssertNotNullOrEmpty(threadId, nameof(threadId));
456-
457-
CollectionResult result = GetMessages(threadId, options?.PageSizeLimit, options?.Order?.ToString(), options?.AfterId, options?.BeforeId, cancellationToken.ToRequestOptions());
458-
459-
if (result is not CollectionResult<ThreadMessage> collection)
460-
{
461-
throw new InvalidOperationException("Failed to cast protocol return type to expected collection type 'CollectionResult<ThreadMessage>'.");
462-
}
463-
464-
return collection;
465-
}
466-
467-
/// <summary>
468-
/// Rehydrates a page collection holding <see cref="ThreadMessage"/> instances from a page token.
469-
/// </summary>
470-
/// <param name="firstPageToken"> Page token corresponding to the first page of the collection to rehydrate. </param>
471-
/// <param name="cancellationToken">A token that can be used to cancel this method call.</param>
472-
/// <returns> A collection of <see cref="ThreadMessage"/>. </returns>
473-
public virtual CollectionResult<ThreadMessage> GetMessages(
474-
ContinuationToken firstPageToken,
475-
CancellationToken cancellationToken = default)
476-
{
477-
Argument.AssertNotNull(firstPageToken, nameof(firstPageToken));
478-
479-
MessageCollectionPageToken pageToken = MessageCollectionPageToken.FromToken(firstPageToken);
480-
CollectionResult result = GetMessages(pageToken?.ThreadId, pageToken?.Limit, pageToken?.Order, pageToken?.After, pageToken?.Before, cancellationToken.ToRequestOptions());
481-
482-
if (result is not CollectionResult<ThreadMessage> collection)
483-
{
484-
throw new InvalidOperationException("Failed to cast protocol return type to expected collection type 'CollectionResult<ThreadMessage>'.");
485-
}
486-
487-
return collection;
488-
489-
}
420+
=> _messageSubClient.GetMessages(threadId, options, cancellationToken);
490421

491422
/// <summary>
492423
/// Gets an existing <see cref="ThreadMessage"/> from a known <see cref="AssistantThread"/>.
@@ -629,7 +560,7 @@ public virtual ClientResult<ThreadRun> CreateRun(string threadId, string assista
629560
options.Stream = null;
630561

631562
ClientResult protocolResult = CreateRun(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions());
632-
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
563+
return ClientResult.FromValue((ThreadRun)protocolResult, protocolResult.GetRawResponse());
633564
}
634565

635566
/// <summary>

src/Custom/Assistants/GeneratorStubs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public readonly partial struct AssistantCollectionOrder { }
1616
[CodeGenType("AssistantToolsCode")]
1717
public partial class CodeInterpreterToolDefinition { }
1818

19+
[CodeGenType("MessageCollectionOptions")]
20+
public partial class MessageCollectionOptions { }
21+
22+
[CodeGenType("MessageCollectionOrder")]
23+
public readonly partial struct MessageCollectionOrder { }
24+
1925
[CodeGenType("MessageObjectStatus")]
2026
public readonly partial struct MessageStatus { }
2127

src/Custom/Assistants/Internal/InternalAssistantMessageClient.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace OpenAI.Assistants;
99
[CodeGenSuppress("InternalAssistantMessageClient", typeof(ClientPipeline), typeof(ApiKeyCredential), typeof(Uri))]
1010
[CodeGenSuppress("CreateMessageAsync", typeof(string), typeof(MessageCreationOptions), typeof(CancellationToken))]
1111
[CodeGenSuppress("CreateMessage", typeof(string), typeof(MessageCreationOptions), typeof(CancellationToken))]
12-
[CodeGenSuppress("GetMessagesAsync", typeof(string), typeof(int?), typeof(OpenAI.VectorStores.VectorStoreCollectionOrder?), typeof(string), typeof(string), typeof(CancellationToken))]
13-
[CodeGenSuppress("GetMessages", typeof(string), typeof(int?), typeof(OpenAI.VectorStores.VectorStoreCollectionOrder?), typeof(string), typeof(string), typeof(CancellationToken))]
1412
[CodeGenSuppress("GetMessageAsync", typeof(string), typeof(string), typeof(CancellationToken))]
1513
[CodeGenSuppress("GetMessage", typeof(string), typeof(string), typeof(CancellationToken))]
1614
[CodeGenSuppress("ModifyMessageAsync", typeof(string), typeof(string), typeof(MessageModificationOptions), typeof(CancellationToken))]

src/Custom/Assistants/Internal/Pagination/AsyncMessageCollectionResult.cs

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)