Skip to content

Commit

Permalink
feat(ai): add more AI methods support
Browse files Browse the repository at this point in the history
  • Loading branch information
innomaxx committed Sep 19, 2024
1 parent 6e40097 commit 3ba6219
Show file tree
Hide file tree
Showing 28 changed files with 1,663 additions and 157 deletions.
154 changes: 154 additions & 0 deletions src/Crowdin.Api/AI/AiApiExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ public AiApiExecutor(ICrowdinApiClient apiClient, IJsonParser jsonParser)
}

#region Prompts

/// <summary>
/// Clone AI Prompt. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.prompts.clones.post">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.prompts.clones.post">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.clones.post">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<AiPromptResource> CloneAiPrompt(int? userId, int aiPromptId, CloneAiPromptRequest request)
{
string url = AddUserIdIfAvailable(userId, $"/ai/prompts/{aiPromptId}/clones");
CrowdinApiResult result = await _apiClient.SendPostRequest(url, request);
return _jsonParser.ParseResponseObject<AiPromptResource>(result.JsonObject);
}

/// <summary>
/// List AI Prompts. Documentation:
Expand Down Expand Up @@ -63,6 +77,68 @@ public async Task<AiPromptResource> AddAiPrompt(int? userId, AddAiPromptRequest
CrowdinApiResult result = await _apiClient.SendPostRequest(url, request);
return _jsonParser.ParseResponseObject<AiPromptResource>(result.JsonObject);
}

/// <summary>
/// Generate AI Prompt Completion. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.ai.prompts.completions.post">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.ai.prompts.completions.post">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.completions.post">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<AiPromptCompletion> GenerateAiPromptCompletion(
int? userId,
int aiPromptId,
GenerateAiPromptCompletionRequest request)
{
string url = AddUserIdIfAvailable(userId, $"/ai/prompts/{aiPromptId}/completions");
CrowdinApiResult result = await _apiClient.SendPostRequest(url, request);
return _jsonParser.ParseResponseObject<AiPromptCompletion>(result.JsonObject);
}

/// <summary>
/// Get AI Prompt Completion Status. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.prompts.completions.get">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.prompts.completions.get">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.completions.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<AiPromptCompletion> GetAiPromptCompletionStatus(
int? userId,
int aiPromptId,
string completionId)
{
string url = AddUserIdIfAvailable(userId, $"/ai/prompts/{aiPromptId}/completions/{completionId}");
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<AiPromptCompletion>(result.JsonObject);
}

/// <summary>
/// Cancel AI Prompt Completion. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.prompts.completions.delete">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.prompts.completions.delete">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.completions.delete">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task CancelAiPromptCompletion(int? userId, int aiPromptId, string completionId)
{
string url = AddUserIdIfAvailable(userId, $"/ai/prompts/{aiPromptId}/completions/{completionId}");
HttpStatusCode statusCode = await _apiClient.SendDeleteRequest(url);
Utils.ThrowIfStatusNot204(statusCode, $"AI prompt completion {completionId} cancellation failed");
}

/// <summary>
/// Download AI Prompt Completion. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.prompts.completions.download.download">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.prompts.completions.download.download">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.prompts.completions.download.download">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<DownloadLink> DownloadAiPromptCompletion(int? userId, int aiPromptId, string completionId)
{
string url = AddUserIdIfAvailable(userId, $"/ai/prompts/{aiPromptId}/completions/{completionId}/download");
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<DownloadLink>(result.JsonObject);
}

/// <summary>
/// Get AI Prompt. Documentation:
Expand Down Expand Up @@ -226,6 +302,84 @@ private static string FormUrl_AiProviderId(int? userId, int aiProviderId)

#endregion

#endregion

#region Reports

/// <summary>
/// Generate AI Report. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.reports.post">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.reports.post">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.reports.post">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<AiReportGenerationStatus> GenerateAiReport(int? userId, GenerateAiReport request)
{
string url = AddUserIdIfAvailable(userId, "/ai/reports");
CrowdinApiResult result = await _apiClient.SendPostRequest(url, request);
return _jsonParser.ParseResponseObject<AiReportGenerationStatus>(result.JsonObject);
}

/// <summary>
/// Check AI Report Generation Status. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.reports.get">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.reports.get">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.reports.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<AiReportGenerationStatus> CheckAiReportGenerationStatus(int? userId, string aiReportId)
{
string url = AddUserIdIfAvailable(userId, $"/ai/reports/{aiReportId}");
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<AiReportGenerationStatus>(result.JsonObject);
}

/// <summary>
/// Download AI Report. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.reports.download.download">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.reports.download.download">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.reports.download.download">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<DownloadLink> DownloadAiReport(int? userId, string aiReportId)
{
string url = AddUserIdIfAvailable(userId, $"/ai/reports/{aiReportId}/download");
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<DownloadLink>(result.JsonObject);
}

#endregion

#region Settings

/// <summary>
/// Get AI Settings. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.settings.get">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.settings.get">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.settings.get">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<AiSettings> GetAiSettings(int? userId)
{
string url = AddUserIdIfAvailable(userId, "/ai/settings");
CrowdinApiResult result = await _apiClient.SendGetRequest(url);
return _jsonParser.ParseResponseObject<AiSettings>(result.JsonObject);
}

/// <summary>
/// Edit AI Settings. Documentation:
/// <a href="https://developer.crowdin.com/api/v2/#tag/AI/operation/api.users.ai.settings.patch">Crowdin File Based API</a>
/// <a href="https://developer.crowdin.com/api/v2/string-based/#tag/AI/operation/api.users.ai.settings.patch">Crowdin String Based API</a>
/// <a href="https://developer.crowdin.com/enterprise/api/v2/#tag/AI/operation/api.ai.settings.patch">Crowdin Enterprise API</a>
/// </summary>
[PublicAPI]
public async Task<AiSettings> EditAiSettings(int? userId, IEnumerable<AiSettingsPatch> patches)
{
string url = AddUserIdIfAvailable(userId, "/ai/settings");
CrowdinApiResult result = await _apiClient.SendPatchRequest(url, patches);
return _jsonParser.ParseResponseObject<AiSettings>(result.JsonObject);
}

#endregion

/// <summary>
Expand Down
42 changes: 42 additions & 0 deletions src/Crowdin.Api/AI/AiPromptCompletion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

using System;
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace Crowdin.Api.AI
{
[PublicAPI]
public class AiPromptCompletion
{
[JsonProperty("identifier")]
public string Identifier { get; set; }

[JsonProperty("status")]
public OperationStatus Status { get; set; }

[JsonProperty("progress")]
public int Progress { get; set; }

[JsonProperty("attributes")]
public AttributesObject Attributes { get; set; }

[JsonProperty("createdAt")]
public DateTimeOffset CreatedAt { get; set; }

[JsonProperty("updatedAt")]
public DateTimeOffset UpdatedAt { get; set; }

[JsonProperty("startedAt")]
public DateTimeOffset StartedAt { get; set; }

[JsonProperty("finishedAt")]
public DateTimeOffset FinishedAt { get; set; }

[PublicAPI]
public class AttributesObject
{
[JsonProperty("aiPromptId")]
public int AiPromptId { get; set; }
}
}
}
95 changes: 75 additions & 20 deletions src/Crowdin.Api/AI/AiPromptConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

using System.Collections.Generic;
using JetBrains.Annotations;
using Newtonsoft.Json;

#nullable enable

namespace Crowdin.Api.AI
{
[PublicAPI]
Expand All @@ -18,41 +21,71 @@ public class BasicModeAiPromptConfiguration : AiPromptConfiguration
public override AiPromptMode Mode => AiPromptMode.Basic;

[JsonProperty("companyDescription")]
public string CompanyDescription { get; set; }
public string? CompanyDescription { get; set; }

[JsonProperty("projectDescription")]
public string ProjectDescription { get; set; }
public string? ProjectDescription { get; set; }

[JsonProperty("audienceDescription")]
public string AudienceDescription { get; set; }
public string? AudienceDescription { get; set; }

[JsonProperty("otherLanguageTranslations")]
public OtherLanguageTranslationsConfig OtherLanguageTranslations { get; set; }
public OtherLanguageTranslationsConfig? OtherLanguageTranslations { get; set; }

[JsonProperty("glossaryTerms")]
public bool GlossaryTerms { get; set; }
public bool? GlossaryTerms { get; set; }

[JsonProperty("tmSuggestions")]
public bool TmSuggestions { get; set; }
public bool? TmSuggestions { get; set; }

[JsonProperty("fileContent")]
public bool FileContent { get; set; }
public bool? FileContent { get; set; }

[JsonProperty("fileContext")]
public bool FileContext { get; set; }
public bool? FileContext { get; set; }

[JsonProperty("publicProjectDescription")]
public bool PublicProjectDescription { get; set; }

[PublicAPI]
public class OtherLanguageTranslationsConfig
{
[JsonProperty("isEnabled")]
public bool IsEnabled { get; set; }

[JsonProperty("languageIds")]
public string[] LanguageIds { get; set; }
}
public bool? PublicProjectDescription { get; set; }
}

[PublicAPI]
public class BasicModeAssistActionAiPromptConfiguration : AiPromptConfiguration
{
[JsonProperty("mode")]
public override AiPromptMode Mode => AiPromptMode.Basic;

[JsonProperty("companyDescription")]
public string? CompanyDescription { get; set; }

[JsonProperty("projectDescription")]
public string? ProjectDescription { get; set; }

[JsonProperty("audienceDescription")]
public string? AudienceDescription { get; set; }

[JsonProperty("otherLanguageTranslations")]
public OtherLanguageTranslationsConfig? OtherLanguageTranslations { get; set; }

[JsonProperty("glossaryTerms")]
public bool? GlossaryTerms { get; set; }

[JsonProperty("tmSuggestions")]
public bool? TmSuggestions { get; set; }

[JsonProperty("fileContext")]
public bool? FileContext { get; set; }

[JsonProperty("screenshots")]
public bool? Screenshots { get; set; }

[JsonProperty("publicProjectDescription")]
public bool? PublicProjectDescription { get; set; }

[JsonProperty("siblingsStrings")]
public bool? SiblingsStrings { get; set; }

[JsonProperty("filteredStrings")]
public bool? FilteredStrings { get; set; }
}

[PublicAPI]
Expand All @@ -61,7 +94,29 @@ public class AdvancedModeAiPromptConfiguration : AiPromptConfiguration
[JsonProperty("mode")]
public override AiPromptMode Mode => AiPromptMode.Advanced;

[JsonProperty("screenshots")]
public bool? Screenshots { get; set; }

[JsonProperty("prompt")]
public string Prompt { get; set; }
public string Prompt { get; set; } = null!;

[JsonProperty("otherLanguageTranslations")]
public OtherLanguageTranslationsConfig? OtherLanguageTranslations { get; set; }
}

[PublicAPI]
public class ExternalModeAiPromptConfiguration : AiPromptConfiguration
{
[JsonProperty("mode")]
public override AiPromptMode Mode => AiPromptMode.External;

[JsonProperty("identifier")]
public string Identifier { get; set; } = null!;

[JsonProperty("key")]
public string Key { get; set; } = null!;

[JsonProperty("options")]
public IDictionary<string, object>? Options { get; set; }
}
}
Loading

0 comments on commit 3ba6219

Please sign in to comment.