Skip to content

Commit

Permalink
Added translation support
Browse files Browse the repository at this point in the history
  • Loading branch information
FloomAI committed Oct 14, 2024
1 parent 30c6103 commit f69afae
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 46 deletions.
16 changes: 14 additions & 2 deletions Floom.Core/Functions/FunctionDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Floom.Functions;

public class FunctionDto
{
public string name { get; set; }

Check warning on line 5 in Floom.Core/Functions/FunctionDto.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Expand All @@ -17,14 +19,15 @@ public class FeaturedFunctionDto
public string id { get; set; }
public string name { get; set; }
public string slug { get; set; }
public string description { get; set; }
public TranslatedField title { get; set; } // Translated titles
public TranslatedField description { get; set; } // Translated descriptions
public string runtimeLanguage { get; set; }
public string runtimeFramework { get; set; }
public string author { get; set; }
public string version { get; set; }
public int rating { get; set; }
public List<int> downloads { get; set; }
public List<ParameterDto> parameters { get; set; } = new();
public List<FeaturedFunctionParameterDto> parameters { get; set; } = new();
}

public class ParameterDto
Expand All @@ -34,3 +37,12 @@ public class ParameterDto
public bool required { get; set; }
public object? defaultValue { get; set; }
}

public class FeaturedFunctionParameterDto
{
public string name { get; set; }
public TranslatedField? description { get; set; }
public bool required { get; set; }
public object? defaultValue { get; set; }
}

109 changes: 67 additions & 42 deletions Floom.Core/Functions/FunctionsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ public async Task<string> DeployFunctionAsync(string filePath, string userId)
// 1. Unzip the file
Directory.CreateDirectory(tempDirectory);
ZipFile.ExtractToDirectory(filePath, tempDirectory);
// list all files in the directory
var files = Directory.GetFiles(tempDirectory);
foreach (var file in files)
{
Console.WriteLine("File: " + file);
}

// 2. Upload prompt.py to S3
var promptFilePath = Path.Combine(tempDirectory, "prompt.py");
if (!File.Exists(promptFilePath))
Expand Down Expand Up @@ -183,9 +178,14 @@ public async Task<string> DeployFunctionAsync(string filePath, string userId)
existingFunction.runtimeFramework = manifest.runtime.framework;
existingFunction.promptUrl = promptFileUrl;
existingFunction.dataUrl = dataFileUrl;
existingFunction.description = manifest.description;

if(manifest.parameters == null)
// Handle translated fields
existingFunction.description = new TranslatedField
{
en = manifest.description
};

if (manifest.parameters == null)
{
existingFunction.parameters = new List<Parameter>();
}
Expand All @@ -194,7 +194,10 @@ public async Task<string> DeployFunctionAsync(string filePath, string userId)
List<Parameter> parameters = manifest.parameters.Select(dto => new Parameter
{
name = dto.name,
description = dto.description,
description = new TranslatedField
{
en = dto.description
},
required = dto.required,
defaultValue = dto.defaultValue
}).ToList();
Expand All @@ -210,14 +213,18 @@ public async Task<string> DeployFunctionAsync(string filePath, string userId)
var functionEntity = new FunctionEntity
{
name = normalizedFunctionName,
description = manifest.description,
runtimeLanguage = manifest.runtime.language,
runtimeFramework = manifest.runtime.framework,
promptUrl = promptFileUrl,
dataUrl = dataFileUrl,
userId = userId
userId = userId,
description = new TranslatedField
{
en = manifest.description
}
};
if(manifest.parameters == null)

if (manifest.parameters == null)
{
functionEntity.parameters = new List<Parameter>();
}
Expand All @@ -226,12 +233,16 @@ public async Task<string> DeployFunctionAsync(string filePath, string userId)
List<Parameter> parameters = manifest.parameters.Select(dto => new Parameter
{
name = dto.name,
description = dto.description,
description = new TranslatedField
{
en = dto.description
},
required = dto.required,
defaultValue = dto.defaultValue
}).ToList();
functionEntity.parameters = parameters;
}

await _repository.Insert(functionEntity);
return functionEntity.name;
}
Expand Down Expand Up @@ -351,7 +362,7 @@ private async Task<string> RunFunctionInternal(FunctionEntity functionEntity, st
public async Task<List<FunctionDto>> ListFunctionsAsync(string userId)
{
var functions = await _repository.ListByConditionAsync(f => f.userId == userId);

var result = new List<FunctionDto>();

foreach (var function in functions)
Expand All @@ -364,7 +375,7 @@ public async Task<List<FunctionDto>> ListFunctionsAsync(string userId)
result.Add(new FunctionDto
{
name = function.name ?? string.Empty, // Set to empty string if null
description = function.description ?? string.Empty, // Set to empty string if null
description = function.description?.en ?? string.Empty, // Fetch only the English description
runtimeLanguage = function.runtimeLanguage ?? string.Empty, // Set to empty string if null
runtimeFramework = function.runtimeFramework ?? string.Empty, // Set to empty string if null
author = string.IsNullOrEmpty(authorName) ? null : authorName, // Set to null if empty
Expand All @@ -375,28 +386,25 @@ public async Task<List<FunctionDto>> ListFunctionsAsync(string userId)
parameters = function.parameters?.Select(p => new ParameterDto
{
name = p.name ?? string.Empty, // Set to empty string if null
description = p.description ?? string.Empty, // Set to empty string if null
description = p.description?.en ?? string.Empty, // Fetch only the English description
required = p.required, // Assuming this is a boolean, keep as is
defaultValue = p.defaultValue is string
? p.defaultValue // If it's a string, keep it as string
: p.defaultValue is IEnumerable<object> array
? array.ToArray() // If it's an array, return the array
: null // If it's neither, return null
}).ToList() ?? new List<ParameterDto>() // Initialize as empty list if parameters is null
}).ToList() ?? new List<ParameterDto>() // Initialize as empty list if parameters is null
});
}

return result;
}


public async Task<List<FeaturedFunctionDto>> ListPublicFeaturedFunctionsAsync()
{
var publicFeaturedFunctions = await _repository.ListByConditionAsync(
f => f.roles != null && f.roles.Contains(Roles.Public) && f.roles.Contains(Roles.Featured)
);
f => f.roles != null && f.roles.Contains(Roles.Public) && f.roles.Contains(Roles.Featured));

// Assuming a method GetUserByIdAsync(string userId) to fetch the user entity
var result = new List<FeaturedFunctionDto>();

foreach (var function in publicFeaturedFunctions)
Expand All @@ -406,32 +414,49 @@ public async Task<List<FeaturedFunctionDto>> ListPublicFeaturedFunctionsAsync()
{
continue;
}

var authorName = !string.IsNullOrEmpty(user.nickname) ? user.nickname : user.username;
// functionId should be the function name + user.username
var functionId = function.name + "-" + user.username;
result.Add(new FeaturedFunctionDto()

result.Add(new FeaturedFunctionDto
{
id = functionId,
name = function.name ?? string.Empty, // Set to empty string if null
slug = function.slug ?? string.Empty, // Set to empty string if null
description = function.description ?? string.Empty, // Set to empty string if null
runtimeLanguage = function.runtimeLanguage ?? string.Empty, // Set to empty string if null
runtimeFramework = function.runtimeFramework ?? string.Empty, // Set to empty string if null
author = string.IsNullOrEmpty(authorName) ? null : authorName, // Set to null if empty
version = function.version ?? string.Empty, // Set to empty string if null
rating = function.rating ?? 0, // Set to 0 if null (assuming rating is a numeric type)
downloads = function.downloads ?? new List<int>(), // Set to empty list if null
parameters = function.parameters?.Select(p => new ParameterDto
name = function.name ?? string.Empty,
slug = function.slug ?? string.Empty,
title = new TranslatedField
{
name = p.name ?? string.Empty, // Set to empty string if null
description = p.description ?? string.Empty, // Set to empty string if null
required = p.required, // Assuming this is a boolean, keep as is
defaultValue = p.defaultValue is string
? p.defaultValue // If it's a string, keep it as string
: p.defaultValue is IEnumerable<object> array
? array.ToArray() // If it's an array, return the array
: null // If it's neither, return null
}).ToList() ?? new List<ParameterDto>() // Initialize as empty list if parameters is null
en = function.title?.en ?? string.Empty,
fr = function.title?.fr ?? string.Empty,
es = function.title?.es ?? string.Empty
},
description = new TranslatedField
{
en = function.description?.en ?? string.Empty,
fr = function.description?.fr ?? string.Empty,
es = function.description?.es ?? string.Empty
},
runtimeLanguage = function.runtimeLanguage ?? string.Empty,
runtimeFramework = function.runtimeFramework ?? string.Empty,
author = string.IsNullOrEmpty(authorName) ? null : authorName,
version = function.version ?? string.Empty,
rating = function.rating ?? 0,
downloads = function.downloads ?? new List<int>(),
parameters = function.parameters?.Select(p => new FeaturedFunctionParameterDto
{
name = p.name ?? string.Empty,
description = new TranslatedField
{
en = p.description?.en ?? string.Empty,
fr = p.description?.fr ?? string.Empty,
es = p.description?.es ?? string.Empty
},
required = p.required,
defaultValue = p.defaultValue is string
? p.defaultValue
: p.defaultValue is IEnumerable<object> array
? array.ToArray()
: null
}).ToList() ?? new List<FeaturedFunctionParameterDto>()
});
}

Expand Down
8 changes: 8 additions & 0 deletions Floom.Core/Functions/TranslatedField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Floom.Functions;

public class TranslatedField
{
public string? en { get; set; } // English
public string? fr { get; set; } // French
public string? es { get; set; } // Spanish
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Floom.Base;
using Floom.Functions;
using Floom.Repository;

[CollectionName("functions")]
Expand All @@ -10,7 +11,9 @@ public class FunctionEntity: DatabaseEntity
public string runtimeFramework { get; set; }
public string promptUrl { get; set; }
public string? dataUrl { get; set; }
public string? description { get; set; }
public TranslatedField? title { get; set; } // Translated titles
public TranslatedField? description { get; set; } // Translated descriptions
public TranslatedField? promptPlaceholder { get; set; } // Translated prompt placeholders
public string userId { get; set; }
public string[]? roles { get; set; }

Expand All @@ -23,7 +26,7 @@ public class FunctionEntity: DatabaseEntity
public class Parameter
{
public string name { get; set; }
public string? description { get; set; }
public TranslatedField? description { get; set; } // Translated descriptions for parameters
public bool required { get; set; }
public object? defaultValue { get; set; }
}
Expand Down

0 comments on commit f69afae

Please sign in to comment.