Merge pull request #1165 from iceljc/features/refine-model-settings

fix audio and excel
This commit is contained in:
iceljc 2025-09-16 20:11:42 -05:00 committed by GitHub
commit c391051f5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 27 deletions

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Routing;
using BotSharp.Core.Infrastructures;
using Microsoft.AspNetCore.StaticFiles;
@ -35,12 +36,18 @@ public class HandleAudioRequestFn : IFunctionCallback
{
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
var conv = _serviceProvider.GetRequiredService<IConversationService>();
var routingCtx = _serviceProvider.GetRequiredService<IRoutingContext>();
var wholeDialogs = routingCtx.GetDialogs();
if (wholeDialogs.IsNullOrEmpty())
{
wholeDialogs = conv.GetDialogHistory();
}
var wholeDialogs = conv.GetDialogHistory();
var dialogs = AssembleFiles(conv.ConversationId, wholeDialogs);
var response = await GetResponeFromDialogs(dialogs);
message.Content = response;
dialogs.ForEach(x => x.Files = null);
return true;
}
@ -52,18 +59,23 @@ public class HandleAudioRequestFn : IFunctionCallback
}
var messageId = dialogs.Select(x => x.MessageId).Distinct().ToList();
var audioMessageFiles = _fileStorage.GetMessageFiles(convId, messageId, options: new()
var audioFiles = _fileStorage.GetMessageFiles(convId, messageId, options: new()
{
Sources = [FileSource.User],
ContentTypes = _audioContentTypes
});
audioMessageFiles = audioMessageFiles.Where(x => x.ContentType.Contains("audio")).ToList();
audioFiles = audioFiles.Where(x => x.ContentType.Contains("audio")).ToList();
foreach (var dialog in dialogs)
{
var found = audioMessageFiles.Where(x => x.MessageId == dialog.MessageId).ToList();
if (found.IsNullOrEmpty()) continue;
var found = audioFiles.Where(x => x.MessageId == dialog.MessageId
&& x.FileSource.IsEqualTo(FileSource.User)).ToList();
if (found.IsNullOrEmpty() || !dialog.IsFromUser)
{
continue;
}
dialog.Files = found.Select(x => new BotSharpFile
{

View file

@ -1,11 +1,12 @@
using System.Linq.Dynamic.Core;
using BotSharp.Abstraction.Files.Enums;
using BotSharp.Abstraction.Files.Models;
using BotSharp.Abstraction.Files.Utilities;
using BotSharp.Abstraction.Routing;
using BotSharp.Plugin.ExcelHandler.Models;
using BotSharp.Plugin.ExcelHandler.Services;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Linq.Dynamic.Core;
namespace BotSharp.Plugin.ExcelHandler.Functions;
@ -49,14 +50,20 @@ public class HandleExcelRequestFn : IFunctionCallback
{
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
var conv = _serviceProvider.GetRequiredService<IConversationService>();
var states = _serviceProvider.GetRequiredService<IConversationStateService>();
var routingCtx = _serviceProvider.GetRequiredService<IRoutingContext>();
if (_excelMimeTypes.IsNullOrEmpty())
{
_excelMimeTypes = FileUtility.GetMimeFileTypes(new List<string> { "excel", "spreadsheet" }).ToHashSet<string>();
}
var dialogs = conv.GetDialogHistory();
var dialogs = routingCtx.GetDialogs();
if (dialogs.IsNullOrEmpty())
{
dialogs = conv.GetDialogHistory();
}
var isExcelExist = AssembleFiles(conv.ConversationId, dialogs);
if (!isExcelExist)
{
@ -65,30 +72,43 @@ public class HandleExcelRequestFn : IFunctionCallback
}
var resultList = GetResponeFromDialogs(dialogs);
var states = _serviceProvider.GetRequiredService<IConversationStateService>();
message.Content = GenerateSqlExecutionSummary(resultList);
states.SetState("excel_import_result",message.Content);
dialogs.ForEach(x => x.Files = null);
return true;
}
#region Private Methods
private bool AssembleFiles(string convId, List<RoleDialogModel> dialogs)
private bool AssembleFiles(string conversationId, List<RoleDialogModel> dialogs)
{
if (dialogs.IsNullOrEmpty()) return false;
var messageId = dialogs.Select(x => x.MessageId).Distinct().ToList();
var contentType = FileUtility.GetContentFileTypes(mimeTypes: _excelMimeTypes);
var excelMessageFiles = _fileStorage.GetMessageFiles(convId, messageId, FileSourceType.User, contentType);
if (excelMessageFiles.IsNullOrEmpty()) return false;
dialogs.ForEach(dialog =>
if (dialogs.IsNullOrEmpty())
{
var found = excelMessageFiles.Where(y => y.MessageId == dialog.MessageId).ToList();
if (found.IsNullOrEmpty()) return;
return false;
}
var messageIds = dialogs.Select(x => x.MessageId).Distinct().ToList();
var contentTypes = FileUtility.GetContentFileTypes(mimeTypes: _excelMimeTypes);
var excelFiles = _fileStorage.GetMessageFiles(conversationId, messageIds, options: new()
{
Sources = [FileSource.User],
ContentTypes = contentTypes
});
if (excelFiles.IsNullOrEmpty())
{
return false;
}
foreach (var dialog in dialogs)
{
var found = excelFiles.Where(x => x.MessageId == dialog.MessageId
&& x.FileSource.IsEqualTo(FileSource.User)).ToList();
if (found.IsNullOrEmpty() || !dialog.IsFromUser)
{
continue;
}
dialog.Files = found.Select(x => new BotSharpFile
{
@ -96,7 +116,8 @@ public class HandleExcelRequestFn : IFunctionCallback
FileUrl = x.FileUrl,
FileStorageUrl = x.FileStorageUrl
}).ToList();
});
}
return true;
}

View file

@ -82,11 +82,11 @@ public class ReadImageFn : IFunctionCallback
if (found.IsNullOrEmpty()) continue;
var targets = found;
if (dialog.Role == AgentRole.User)
if (dialog.IsFromUser)
{
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.User)).ToList();
}
else if (dialog.Role == AgentRole.Assistant || dialog.Role == AgentRole.Model)
else if (dialog.IsFromAssistant)
{
targets = found.Where(x => x.FileSource.IsEqualTo(FileSource.Bot)).ToList();
}