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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();

Check warning on line 51 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 51 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 51 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 51 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 51 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 51 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.
void CreateUser(User user) => throw new NotImplementedException();
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
void UpdateUserVerified(string userId) => throw new NotImplementedException();
Expand All @@ -72,6 +72,8 @@
=> throw new NotImplementedException();
Agent? GetAgent(string agentId, bool basicsOnly = false)
=> throw new NotImplementedException();
Task<Agent?> GetAgentAsync(string agentId, bool basicsOnly = false)
=> throw new NotImplementedException();
List<Agent> GetAgents(AgentFilter filter)
=> throw new NotImplementedException();
List<UserAgent> GetUserAgents(string userId)
Expand Down Expand Up @@ -114,7 +116,7 @@
#region Agent Code
List<AgentCodeScript> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null)
=> throw new NotImplementedException();
AgentCodeScript? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
Task<AgentCodeScript?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
=> throw new NotImplementedException();
bool UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> scripts, AgentCodeScriptDbUpdateOptions? options = null)
=> throw new NotImplementedException();
Expand All @@ -139,7 +141,7 @@
=> throw new NotImplementedException();
void UpdateConversationStatus(string conversationId, string status)
=> throw new NotImplementedException();
Conversation GetConversation(string conversationId, bool isLoadStates = false)
Task<Conversation> GetConversation(string conversationId, bool isLoadStates = false)
=> throw new NotImplementedException();
ValueTask<PagedItems<Conversation>> GetConversations(ConversationFilter filter)
=> throw new NotImplementedException();
Expand Down Expand Up @@ -231,9 +233,9 @@
=> throw new NotImplementedException();
bool DeleteKnowledgeCollectionConfig(string collectionName)
=> throw new NotImplementedException();
IEnumerable<VectorCollectionConfig> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter)
Task<IEnumerable<VectorCollectionConfig>> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter)
=> throw new NotImplementedException();
VectorCollectionConfig GetKnowledgeCollectionConfig(string collectionName, string vectorStroageProvider)
Task<VectorCollectionConfig> GetKnowledgeCollectionConfig(string collectionName, string vectorStroageProvider)
=> throw new NotImplementedException();
bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData)
=> throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public async Task<List<AgentCodeScript>> GetAgentCodeScripts(string agentId, Age
public async Task<AgentCodeScript?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var script = db.GetAgentCodeScript(agentId, scriptName, scriptType);
return await Task.FromResult(script);
var script = await db.GetAgentCodeScript(agentId, scriptName, scriptType);
return script;
}

public async Task<bool> UpdateAgentCodeScripts(string agentId, List<AgentCodeScript> codeScripts, AgentCodeScriptUpdateOptions? options = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<Agent> GetAgent(string id)
return null;
}

var profile = _db.GetAgent(id);
var profile = await _db.GetAgentAsync(id);
if (profile == null)
{
_logger.LogError($"Can't find agent {id}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
return;
}

var record = _db.GetAgent(agent.Id);
var record = await _db.GetAgentAsync(agent.Id);
if (record == null) return;

record.Name = agent.Name ?? string.Empty;
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task<string> PatchAgentTemplate(Agent agent)
return patchResult;
}

var record = _db.GetAgent(agent.Id);
var record = await _db.GetAgentAsync(agent.Id);
if (record == null)
{
patchResult = $"Cannot find agent {agent.Id}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public async Task<Conversation> UpdateConversationTitle(string id, string title)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
db.UpdateConversationTitle(id, title);
var conversation = db.GetConversation(id);
var conversation = await db.GetConversation(id);
return conversation;
}

public async Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
db.UpdateConversationTitleAlias(id, titleAlias);
var conversation = db.GetConversation(id);
var conversation = await db.GetConversation(id);
return conversation;
}

Expand All @@ -72,7 +72,7 @@ public async Task<bool> UpdateConversationMessage(string conversationId, UpdateM
public async Task<Conversation> GetConversation(string id, bool isLoadStates = false)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var conversation = db.GetConversation(id);
var conversation = await db.GetConversation(id);
return conversation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public List<AgentCodeScript> GetAgentCodeScripts(string agentId, AgentCodeScript
return results;
}

public AgentCodeScript? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
public async Task<AgentCodeScript?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
{
if (string.IsNullOrWhiteSpace(agentId)
|| string.IsNullOrWhiteSpace(scriptName)
Expand All @@ -76,7 +76,7 @@ public List<AgentCodeScript> GetAgentCodeScripts(string agentId, AgentCodeScript
AgentId = agentId,
Name = scriptName,
ScriptType = scriptType,
Content = File.ReadAllText(foundFile),
Content = await File.ReadAllTextAsync(foundFile),
CreatedTime = File.GetCreationTimeUtc(foundFile),
UpdatedTime = File.GetLastWriteTimeUtc(foundFile)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public void UpdateConversationStatus(string conversationId, string status)
}
}

public Conversation GetConversation(string conversationId, bool isLoadStates = false)
public async Task<Conversation> GetConversation(string conversationId, bool isLoadStates = false)
{
var convDir = FindConversationDirectory(conversationId);
if (string.IsNullOrEmpty(convDir))
Expand All @@ -410,7 +410,7 @@ public Conversation GetConversation(string conversationId, bool isLoadStates = f
}

var convFile = Path.Combine(convDir, CONVERSATION_FILE);
var content = File.ReadAllText(convFile);
var content = await File.ReadAllTextAsync(convFile);
var record = JsonSerializer.Deserialize<Conversation>(content, _options);

var dialogFile = Path.Combine(convDir, DIALOG_FILE);
Expand All @@ -424,7 +424,7 @@ public Conversation GetConversation(string conversationId, bool isLoadStates = f
var latestStateFile = Path.Combine(convDir, CONV_LATEST_STATE_FILE);
if (record != null && File.Exists(latestStateFile))
{
var stateJson = File.ReadAllText(latestStateFile);
var stateJson = await File.ReadAllTextAsync(latestStateFile);
var states = JsonSerializer.Deserialize<Dictionary<string, JsonDocument>>(stateJson, _options) ?? [];
record.States = states.ToDictionary(x => x.Key, x =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public bool DeleteKnowledgeCollectionConfig(string collectionName)
return true;
}

public IEnumerable<VectorCollectionConfig> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter)
public async Task<IEnumerable<VectorCollectionConfig>> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter)
{
if (filter == null)
{
Expand All @@ -89,7 +89,7 @@ public IEnumerable<VectorCollectionConfig> GetKnowledgeCollectionConfigs(VectorC
}

// Get data
var content = File.ReadAllText(configFile);
var content = await File.ReadAllTextAsync(configFile);
var configs = JsonSerializer.Deserialize<List<VectorCollectionConfig>>(content, _options) ?? new();

// Apply filters
Expand All @@ -112,9 +112,9 @@ public IEnumerable<VectorCollectionConfig> GetKnowledgeCollectionConfigs(VectorC
}

[SharpCache(10)]
public VectorCollectionConfig GetKnowledgeCollectionConfig(string collectionName, string vectorStroageProvider)
public async Task<VectorCollectionConfig> GetKnowledgeCollectionConfig(string collectionName, string vectorStroageProvider)
{
var configs = GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
var configs = await GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
{
CollectionNames = [collectionName],
VectorStroageProviders = [vectorStroageProvider]
Expand Down
23 changes: 23 additions & 0 deletions src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using BotSharp.OpenAPI.BackgroundServices;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Authentication;
using Microsoft.OpenApi.Models;

namespace BotSharp.OpenAPI;

Expand Down Expand Up @@ -160,6 +161,28 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv
services.AddSwaggerGen(
c =>
{
#if NET8_0
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
#endif
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public TranslationController(IServiceProvider services,
public async Task<TranslationResponseModel> Translate([FromBody] TranslationRequestModel model)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgent(BuiltInAgentId.AIAssistant);
var agent = await db.GetAgentAsync(BuiltInAgentId.AIAssistant);
var translator = _services.GetRequiredService<ITranslationService>();
var states = _services.GetRequiredService<IConversationStateService>();
states.SetState("max_tokens", "8192");
Expand All @@ -38,7 +38,7 @@ public async Task<TranslationResponseModel> Translate([FromBody] TranslationRequ
public async Task SendMessageSse([FromBody] TranslationLongTextRequestModel model)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgent(BuiltInAgentId.AIAssistant);
var agent = await db.GetAgentAsync(BuiltInAgentId.AIAssistant);
var translator = _services.GetRequiredService<ITranslationService>();

Response.StatusCode = 200;
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public WelcomeHook(
public override async Task OnUserAgentConnectedInitially(Conversation conversation)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgent(conversation.AgentId);
var agent = await db.GetAgentAsync(conversation.AgentId);

// Check if the Welcome template exists.
var welcomeTemplate = agent?.Templates?.FirstOrDefault(x => x.Name == ".welcome");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace BotSharp.Plugin.KnowledgeBase.Helpers;

public static class KnowledgeSettingHelper
{
public static ITextEmbedding GetTextEmbeddingSetting(IServiceProvider services, string collectionName)
public static async Task<ITextEmbedding> GetTextEmbeddingSetting(IServiceProvider services, string collectionName)
{
var settings = services.GetRequiredService<KnowledgeBaseSettings>();
var db = services.GetRequiredService<IBotSharpRepository>();

// Get collection config from db
var config = db.GetKnowledgeCollectionConfig(collectionName, settings.VectorDb.Provider);
var config = await db.GetKnowledgeCollectionConfig(collectionName, settings.VectorDb.Provider);

var textEmbeddingConfig = config?.TextEmbedding;
var provider = textEmbeddingConfig?.Provider ?? settings.Default.TextEmbedding.Provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private async Task<IEnumerable<string>> SaveToVectorDb(string collectionName, IE

var dataIds = new List<string>();
var vectorDb = GetVectorDb();
var textEmbedding = GetTextEmbedding(collectionName);
var textEmbedding = await GetTextEmbedding(collectionName);

for (int i = 0; i < contents.Count(); i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task<bool> ExistVectorCollection(string collectionName)
var exist = await vectorDb.DoesCollectionExist(collectionName);
if (exist) return true;

var configs = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
var configs = await db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
{
CollectionNames = [collectionName],
VectorStroageProviders = [_settings.VectorDb.Provider]
Expand Down Expand Up @@ -72,11 +72,11 @@ public async Task<IEnumerable<VectorCollectionConfig>> GetVectorCollections(stri
try
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var configs = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
var configs = await db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
{
CollectionTypes = !string.IsNullOrEmpty(type) ? [type] : null,
VectorStroageProviders = [_settings.VectorDb.Provider]
}).ToList();
});

var vectorDb = GetVectorDb();
if (vectorDb == null)
Expand All @@ -100,10 +100,10 @@ public async Task<IEnumerable<VectorCollectionConfig>> GetVectorCollections(stri
if (string.IsNullOrWhiteSpace(collectionName)) return null;

var db = _services.GetRequiredService<IBotSharpRepository>();
var configs = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
var configs = await db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
{
CollectionNames = [collectionName]
}).ToList();
});

var vectorDb = GetVectorDb();
var details = await vectorDb.GetCollectionDetails(collectionName);
Expand Down Expand Up @@ -163,7 +163,7 @@ public async Task<bool> CreateVectorCollectionData(string collectionName, Vector
return false;
}

var textEmbedding = GetTextEmbedding(collectionName);
var textEmbedding = await GetTextEmbedding(collectionName);
var vector = await textEmbedding.GetVectorAsync(create.Text);

var db = GetVectorDb();
Expand Down Expand Up @@ -202,7 +202,7 @@ public async Task<bool> UpdateVectorCollectionData(string collectionName, Vector
return false;
}

var textEmbedding = GetTextEmbedding(collectionName);
var textEmbedding = await GetTextEmbedding(collectionName);
var vector = await textEmbedding.GetVectorAsync(update.Text);
var payload = update.Payload ?? new();

Expand Down Expand Up @@ -243,7 +243,7 @@ public async Task<bool> UpsertVectorCollectionData(string collectionName, Vector
}
}

var textEmbedding = GetTextEmbedding(collectionName);
var textEmbedding = await GetTextEmbedding(collectionName);
var vector = await textEmbedding.GetVectorAsync(update.Text);
var payload = update.Payload ?? new();

Expand Down Expand Up @@ -344,7 +344,7 @@ public async Task<IEnumerable<VectorSearchResult>> SearchVectorKnowledge(string
{
try
{
var textEmbedding = GetTextEmbedding(collectionName);
var textEmbedding = await GetTextEmbedding(collectionName);
var vector = await textEmbedding.GetVectorAsync(query);

// Vector search
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ private IGraphDb GetGraphDb()
return db;
}

private ITextEmbedding GetTextEmbedding(string collectionName)
private async Task<ITextEmbedding> GetTextEmbedding(string collectionName)
{
return KnowledgeSettingHelper.GetTextEmbeddingSetting(_services, collectionName);
return await KnowledgeSettingHelper.GetTextEmbeddingSetting(_services, collectionName);
}

private async Task<string> GetUserId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Routing.Models;
using MongoDB.Driver.Linq;

namespace BotSharp.Plugin.MongoStorage.Repository;

Expand Down Expand Up @@ -440,6 +441,17 @@ private void UpdateAgentAllFields(Agent agent)
return TransformAgentDocument(agent);
}

public async Task<Agent?> GetAgentAsync(string agentId, bool basicsOnly = false)
{
var agent = await _dc.Agents.AsQueryable().FirstOrDefaultAsync(x => x.Id == agentId);
if (agent == null)
{
return null;
}

return TransformAgentDocument(agent);
}

public List<Agent> GetAgents(AgentFilter filter)
{
if (filter == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public List<AgentCodeScript> GetAgentCodeScripts(string agentId, AgentCodeScript
return found.Select(x => AgentCodeScriptDocument.ToDomainModel(x)).ToList();
}

public AgentCodeScript? GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
public async Task<AgentCodeScript?> GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src)
{
if (string.IsNullOrWhiteSpace(agentId)
|| string.IsNullOrWhiteSpace(scriptName)
Expand All @@ -52,7 +52,7 @@ public List<AgentCodeScript> GetAgentCodeScripts(string agentId, AgentCodeScript
builder.Eq(x => x.ScriptType, scriptType)
};

var found = _dc.AgentCodeScripts.Find(builder.And(filters)).FirstOrDefault();
var found = await _dc.AgentCodeScripts.Find(builder.And(filters)).FirstOrDefaultAsync();
return found != null ? AgentCodeScriptDocument.ToDomainModel(found) : null;
}

Expand Down
Loading
Loading