Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
89 changes: 86 additions & 3 deletions src/libs/tryAGI.OpenAI.CLI/GeneratedApi/CliRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,88 @@ public static TimeSpan ParseDuration(string value, string optionName)
}


public static async Task<T> PollUntilTerminalAsync<T>(
Func<CancellationToken, Task<T>> fetchAsync,
TimeSpan pollInterval,
TimeSpan waitTimeout,
JsonSerializerContext context,
CancellationToken cancellationToken)
{
if (pollInterval <= TimeSpan.Zero || waitTimeout <= TimeSpan.Zero)
{
throw new CliException("--poll-interval and --wait-timeout must be greater than zero.");
}

using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeout.CancelAfter(waitTimeout);
try
{
while (true)
{
var response = await fetchAsync(timeout.Token).ConfigureAwait(false);
var status = FindJobStatus(ToJsonElement(response, context), depth: 0);
if (string.IsNullOrWhiteSpace(status))
{
throw new CliException("The status response did not contain a status value.");
}

switch (status.Trim().ToUpperInvariant())
{
case "COMPLETED":
case "SUCCEEDED":
case "SUCCESS":
case "DONE":
case "FINISHED":
return response;
case "FAILED":
case "ERRORED":
case "ERROR":
case "CANCELLED":
case "CANCELED":
case "ABORTED":
throw new CliException($"The job ended with status '{status}'.");
}

await Task.Delay(pollInterval, timeout.Token).ConfigureAwait(false);
}
}
catch (OperationCanceledException) when (timeout.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
{
throw new CliException($"The job did not finish within {waitTimeout}.");
}
}

private static string? FindJobStatus(JsonElement element, int depth)
{
if (element.ValueKind != JsonValueKind.Object || depth > 3)
{
return null;
}

foreach (var property in element.EnumerateObject())
{
if (string.Equals(property.Name, "status", StringComparison.OrdinalIgnoreCase))
{
return property.Value.ValueKind == JsonValueKind.String
? property.Value.GetString()
: property.Value.ToString();
}
}

foreach (var property in element.EnumerateObject())
{
if (property.Name is "data" or "result" or "job" or "task")
{
var status = FindJobStatus(property.Value, depth + 1);
if (status is not null)
{
return status;
}
}
}

return null;
}

public static async global::System.Threading.Tasks.Task<string?> ReadInputAsync(
ParseResult parseResult,
Expand Down Expand Up @@ -355,9 +437,10 @@ public static TimeSpan ParseDuration(string value, string optionName)

public static T DeserializeJsonValue<T>(string json, JsonSerializerContext context)
{
_ = context;
return JsonSerializer.Deserialize<T>(json) ??
throw new CliException($"Unable to deserialize generated CLI value as {typeof(T).Name}.");
var value = JsonSerializer.Deserialize(json, typeof(T), context);
return value is T typed
? typed
: throw new CliException($"Unable to deserialize generated CLI value as {typeof(T).Name}.");
}

public static string SerializeKeyValuePairs(IEnumerable<string> pairs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ private static string FormatResponse(ParseResult parseResult, global::tryAGI.Ope
static partial void CustomizeResponseFormatHints(Dictionary<string, CliFormatHint> hints);


public static Command Create()
static partial void CustomizeCommand(ref Command command);

public static Command Create(string? commandName = null)
{
var command = new Command(@"admin-api-keys-create", @"Create admin API key
var command = new Command(commandName ?? @"admin-api-keys-create", @"Create admin API key
Create an organization admin API key");
command.Arguments.Add(NameOption);
command.Options.Add(ExpiresInSeconds);
Expand Down Expand Up @@ -104,6 +106,7 @@ await CliRuntime.WriteResponseAsync(
FormatResponse,
cancellationToken).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false));
CustomizeCommand(ref command);
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ private static string FormatResponse(ParseResult parseResult, global::tryAGI.Ope
static partial void CustomizeResponseFormatHints(Dictionary<string, CliFormatHint> hints);


public static Command Create()
static partial void CustomizeCommand(ref Command command);

public static Command Create(string? commandName = null)
{
var command = new Command(@"admin-api-keys-delete", @"Delete admin API key
var command = new Command(commandName ?? @"admin-api-keys-delete", @"Delete admin API key
Delete an organization admin API key");
command.Arguments.Add(KeyId);

Expand All @@ -59,6 +61,7 @@ await CliRuntime.WriteResponseAsync(
FormatResponse,
cancellationToken).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false));
CustomizeCommand(ref command);
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ private static string FormatResponse(ParseResult parseResult, global::tryAGI.Ope
static partial void CustomizeResponseFormatHints(Dictionary<string, CliFormatHint> hints);


public static Command Create()
static partial void CustomizeCommand(ref Command command);

public static Command Create(string? commandName = null)
{
var command = new Command(@"admin-api-keys-get", @"Retrieve admin API key
var command = new Command(commandName ?? @"admin-api-keys-get", @"Retrieve admin API key
Retrieve a single organization API key");
command.Arguments.Add(KeyId);

Expand All @@ -59,6 +61,7 @@ await CliRuntime.WriteResponseAsync(
FormatResponse,
cancellationToken).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false));
CustomizeCommand(ref command);
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ private static string FormatResponse(ParseResult parseResult, global::tryAGI.Ope
static partial void CustomizeResponseFormatHints(Dictionary<string, CliFormatHint> hints);


public static Command Create()
static partial void CustomizeCommand(ref Command command);

public static Command Create(string? commandName = null)
{
var command = new Command(@"admin-api-keys-list", @"List all organization and project API keys.
var command = new Command(commandName ?? @"admin-api-keys-list", @"List all organization and project API keys.
List organization API keys");
command.Options.Add(After);
command.Options.Add(Order);
Expand Down Expand Up @@ -85,6 +87,7 @@ await CliRuntime.WriteResponseAsync(
cancellationToken).ConfigureAwait(false);
}
}, cancellationToken).ConfigureAwait(false));
CustomizeCommand(ref command);
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace tryAGI.OpenAI.Cli.GeneratedApi.Commands;

internal static class AgentsApiGroupCommand
internal static partial class AgentsApiGroupCommand
{
static partial void CustomizeCommand(ref Command command);

public static Command Create()
{
var command = new Command(@"agents", @"Agents endpoint commands.");
Expand Down Expand Up @@ -43,6 +45,7 @@ public static Command Create()
command.Subcommands.Add(AgentsUpdateAgentCommandApiCommand.Create());
command.Subcommands.Add(AgentsUpdateAgentEnvironmentTemplateCommandApiCommand.Create());
command.Subcommands.Add(AgentsUpdateAgentSessionCommandApiCommand.Create());
CustomizeCommand(ref command);
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ private static string FormatResponse(ParseResult parseResult, global::tryAGI.Ope
static partial void CustomizeResponseFormatHints(Dictionary<string, CliFormatHint> hints);


public static Command Create()
static partial void CustomizeCommand(ref Command command);

public static Command Create(string? commandName = null)
{
var command = new Command(@"create-agent", @"Create an agent
var command = new Command(commandName ?? @"create-agent", @"Create an agent
Creates a reusable agent without storing credentials. See [agent configuration](https://developers.openai.com/api/docs/guides/agents-api/configuration).");
command.Options.Add(Metadata);
command.Options.Add(NameOption);
Expand Down Expand Up @@ -183,6 +185,7 @@ await CliRuntime.WriteResponseAsync(
cancellationToken).ConfigureAwait(false);
}
}, cancellationToken).ConfigureAwait(false));
CustomizeCommand(ref command);
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ private static string FormatResponse(ParseResult parseResult, global::tryAGI.Ope
static partial void CustomizeResponseFormatHints(Dictionary<string, CliFormatHint> hints);


public static Command Create()
static partial void CustomizeCommand(ref Command command);

public static Command Create(string? commandName = null)
{
var command = new Command(@"create-agent-environment-file", @"Create an agent environment file
var command = new Command(commandName ?? @"create-agent-environment-file", @"Create an agent environment file
Copies inline bytes or a Files API file into a connected execution environment. See [environment files](https://developers.openai.com/api/docs/guides/agents-api/environments/files).");
command.Arguments.Add(EnvironmentId);
command.Options.Add(Input);
Expand Down Expand Up @@ -96,6 +98,7 @@ await CliRuntime.WriteResponseAsync(
FormatResponse,
cancellationToken).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false));
CustomizeCommand(ref command);
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ private static string FormatResponse(ParseResult parseResult, global::tryAGI.Ope
static partial void CustomizeResponseFormatHints(Dictionary<string, CliFormatHint> hints);


public static Command Create()
static partial void CustomizeCommand(ref Command command);

public static Command Create(string? commandName = null)
{
var command = new Command(@"create-agent-environment-template", @"Create an agent environment template
var command = new Command(commandName ?? @"create-agent-environment-template", @"Create an agent environment template
Creates reusable environment configuration without returning confidential setup commands or environment values. See [reusing a hosted setup](https://developers.openai.com/api/docs/guides/agents-api/tools#reuse-a-hosted-plugin-setup).");
command.Options.Add(SetupCommands);
command.Options.Add(Env);
Expand Down Expand Up @@ -185,6 +187,7 @@ await CliRuntime.WriteResponseAsync(
FormatResponse,
cancellationToken).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false));
CustomizeCommand(ref command);
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ internal static partial class AgentsCreateAgentSessionAsStreamCommandApiCommand
Hidden = true,
};

public static Command Create()
static partial void CustomizeCommand(ref Command command);

public static Command Create(string? commandName = null)
{
var command = new Command(@"create-agent-session-as-stream", @"Create an agent session
var command = new Command(commandName ?? @"create-agent-session-as-stream", @"Create an agent session
Creates a managed agent session, optionally submits initial input, and returns the session or streams its events when stream is true. See [running sessions](https://developers.openai.com/api/docs/guides/agents-api/sessions).");
command.Options.Add(Metadata);
command.Options.Add(Environment);
Expand Down Expand Up @@ -117,6 +119,7 @@ await CliRuntime.WriteResponseLineAsync(
cancellationToken: cancellationToken).ConfigureAwait(false);
}
}, cancellationToken).ConfigureAwait(false));
CustomizeCommand(ref command);
return command;
}
}
Loading
Loading