Skip to content

Commit a92c761

Browse files
authored
Merge pull request #578 from iceljc/features/refine-file-select
Features/refine file select
2 parents 38b50a3 + c9a1761 commit a92c761

File tree

41 files changed

+508
-689
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+508
-689
lines changed

src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(TargetFramework)</TargetFramework>
@@ -25,6 +25,7 @@
2525

2626
<ItemGroup>
2727
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
28+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
2829
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
2930
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
3031
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />

src/Infrastructure/BotSharp.Abstraction/Files/IBotSharpFileService.cs renamed to src/Infrastructure/BotSharp.Abstraction/Files/IFileBasicService.cs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BotSharp.Abstraction.Files;
44

5-
public interface IBotSharpFileService
5+
public interface IFileBasicService
66
{
77
#region Conversation
88
/// <summary>
@@ -11,13 +11,13 @@ public interface IBotSharpFileService
1111
/// </summary>
1212
/// <param name="conversationId"></param>
1313
/// <param name="source"></param>
14-
/// <param name="conversations"></param>
14+
/// <param name="dialogs"></param>
1515
/// <param name="contentTypes"></param>
1616
/// <param name="includeScreenShot"></param>
1717
/// <param name="offset"></param>
1818
/// <returns></returns>
1919
Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string source,
20-
IEnumerable<RoleDialogModel> conversations, IEnumerable<string> contentTypes,
20+
IEnumerable<RoleDialogModel> dialogs, IEnumerable<string>? contentTypes,
2121
bool includeScreenShot = false, int? offset = null);
2222

2323
/// <summary>
@@ -28,7 +28,7 @@ Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string s
2828
/// <param name="source"></param>
2929
/// <param name="imageOnly"></param>
3030
/// <returns></returns>
31-
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, string source, bool imageOnly = false);
31+
IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnumerable<string> messageIds, string source, IEnumerable<string>? contentTypes = null);
3232
string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName);
3333
IEnumerable<MessageFileModel> GetMessagesWithFile(string conversationId, IEnumerable<string> messageIds);
3434
bool SaveMessageFiles(string conversationId, string messageId, string source, List<BotSharpFile> files);
@@ -45,38 +45,20 @@ Task<IEnumerable<MessageFileModel>> GetChatFiles(string conversationId, string s
4545
bool DeleteConversationFiles(IEnumerable<string> conversationIds);
4646
#endregion
4747

48-
#region Image
49-
Task<RoleDialogModel> GenerateImage(string? provider, string? model, string text);
50-
Task<RoleDialogModel> VaryImage(string? provider, string? model, BotSharpFile image);
51-
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, BotSharpFile image);
52-
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, BotSharpFile image, BotSharpFile mask);
53-
#endregion
54-
55-
#region Pdf
56-
/// <summary>
57-
/// Take screenshots of pdf pages and get response from llm
58-
/// </summary>
59-
/// <param name="prompt"></param>
60-
/// <param name="files">Pdf files</param>
61-
/// <returns></returns>
62-
Task<string> ReadPdf(string? provider, string? model, string? modelId, string prompt, List<BotSharpFile> files);
63-
#endregion
64-
6548
#region User
6649
string GetUserAvatar();
6750
bool SaveUserAvatar(BotSharpFile file);
6851
#endregion
6952

7053
#region Common
71-
/// <summary>
72-
/// Get file bytes and content type from data, e.g., "data:image/png;base64,aaaaaaaaa"
73-
/// </summary>
74-
/// <param name="data"></param>
75-
/// <returns></returns>
76-
(string, byte[]) GetFileInfoFromData(string data);
7754
string GetDirectory(string conversationId);
78-
string GetFileContentType(string filePath);
7955
byte[] GetFileBytes(string fileStorageUrl);
80-
bool SavefileToPath(string filePath, Stream stream);
56+
bool SaveFileStreamToPath(string filePath, Stream stream);
57+
bool SaveFileBytesToPath(string filePath, byte[] bytes);
58+
string GetParentDir(string dir, int level = 1);
59+
bool ExistDirectory(string? dir);
60+
void CreateDirectory(string dir);
61+
void DeleteDirectory(string dir);
62+
string BuildDirectory(params string[] segments);
8163
#endregion
8264
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace BotSharp.Abstraction.Files;
2+
3+
public interface IFileInstructService
4+
{
5+
#region Image
6+
Task<RoleDialogModel> ReadImages(string? provider, string? model, string text, IEnumerable<BotSharpFile> images);
7+
Task<RoleDialogModel> GenerateImage(string? provider, string? model, string text);
8+
Task<RoleDialogModel> VaryImage(string? provider, string? model, BotSharpFile image);
9+
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, BotSharpFile image);
10+
Task<RoleDialogModel> EditImage(string? provider, string? model, string text, BotSharpFile image, BotSharpFile mask);
11+
#endregion
12+
13+
#region Pdf
14+
/// <summary>
15+
/// Take screenshots of pdf pages and get response from llm
16+
/// </summary>
17+
/// <param name="prompt"></param>
18+
/// <param name="files">Pdf files</param>
19+
/// <returns></returns>
20+
Task<string> ReadPdf(string? provider, string? model, string? modelId, string prompt, List<BotSharpFile> files);
21+
#endregion
22+
23+
#region Select file
24+
Task<IEnumerable<MessageFileModel>> SelectMessageFiles(string conversationId,
25+
string? agentId = null, string? template = null, string? description = null,
26+
bool includeBotFile = false, bool fromBreakpoint = false,
27+
int? offset = null, IEnumerable<string>? contentTypes = null);
28+
#endregion
29+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BotSharp.Abstraction.Files.Models;
2+
3+
public class FileSelectContext
4+
{
5+
[JsonPropertyName("selected_ids")]
6+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
7+
public IEnumerable<int>? Selecteds { get; set; }
8+
}

src/Infrastructure/BotSharp.Abstraction/Files/Models/MessageFileModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public MessageFileModel()
3939

4040
public override string ToString()
4141
{
42-
return $"File name: {FileName}, File type: {FileType}, Content type: {ContentType}";
42+
return $"File name: {FileName}, File type: {FileType}, Content type: {ContentType}, Source: {FileSource}";
4343
}
4444
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.AspNetCore.StaticFiles;
2+
3+
namespace BotSharp.Abstraction.Files.Utilities;
4+
5+
public static class FileUtility
6+
{
7+
/// <summary>
8+
/// Get file bytes and content type from data, e.g., "data:image/png;base64,aaaaaaaaa"
9+
/// </summary>
10+
/// <param name="data"></param>
11+
/// <returns></returns>
12+
public static (string, byte[]) GetFileInfoFromData(string data)
13+
{
14+
if (string.IsNullOrEmpty(data))
15+
{
16+
return (string.Empty, new byte[0]);
17+
}
18+
19+
var typeStartIdx = data.IndexOf(':');
20+
var typeEndIdx = data.IndexOf(';');
21+
var contentType = data.Substring(typeStartIdx + 1, typeEndIdx - typeStartIdx - 1);
22+
23+
var base64startIdx = data.IndexOf(',');
24+
var base64Str = data.Substring(base64startIdx + 1);
25+
26+
return (contentType, Convert.FromBase64String(base64Str));
27+
}
28+
29+
public static string GetFileContentType(string filePath)
30+
{
31+
string contentType;
32+
var provider = new FileExtensionContentTypeProvider();
33+
if (!provider.TryGetContentType(filePath, out contentType))
34+
{
35+
contentType = string.Empty;
36+
}
37+
38+
return contentType;
39+
}
40+
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
<NoWarn>1701;1702</NoWarn>
4646
</PropertyGroup>
4747

48+
<ItemGroup>
49+
<Compile Remove="Planning\**" />
50+
<Compile Remove="Translation\Models\**" />
51+
<EmbeddedResource Remove="Planning\**" />
52+
<EmbeddedResource Remove="Translation\Models\**" />
53+
<None Remove="Planning\**" />
54+
<None Remove="Translation\Models\**" />
55+
</ItemGroup>
56+
4857
<ItemGroup>
4958
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json" />
5059
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instruction.liquid" />
@@ -69,6 +78,7 @@
6978
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.two_stage.2nd.task.liquid" />
7079
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\response_with_function.liquid" />
7180
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\translation_prompt.liquid" />
81+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_file_prompt.liquid" />
7282
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\agent.json" />
7383
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\instruction.liquid" />
7484
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.executor.liquid" />
@@ -155,6 +165,9 @@
155165
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instruction.liquid">
156166
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
157167
</Content>
168+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_file_prompt.liquid">
169+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
170+
</Content>
158171
<Content Include="data\plugins\config.json">
159172
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
160173
</Content>
@@ -172,7 +185,6 @@
172185
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
173186
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.5.0" />
174187
<PackageReference Include="Fluid.Core" Version="2.11.1" />
175-
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
176188
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
177189
<PackageReference Include="Nanoid" Version="3.1.0" />
178190
</ItemGroup>
@@ -181,9 +193,4 @@
181193
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
182194
</ItemGroup>
183195

184-
<ItemGroup>
185-
<Folder Include="Planning\" />
186-
<Folder Include="Translation\Models\" />
187-
</ItemGroup>
188-
189196
</Project>

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.TruncateMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public partial class ConversationService : IConversationService
55
public async Task<bool> TruncateConversation(string conversationId, string messageId, string? newMessageId = null)
66
{
77
var db = _services.GetRequiredService<IBotSharpRepository>();
8-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
8+
var fileService = _services.GetRequiredService<IFileBasicService>();
99
var deleteMessageIds = db.TruncateConversation(conversationId, messageId, cleanLog: true);
1010

1111
fileService.DeleteMessageFiles(conversationId, deleteMessageIds, messageId, newMessageId);

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ConversationService(
3737
public async Task<bool> DeleteConversations(IEnumerable<string> ids)
3838
{
3939
var db = _services.GetRequiredService<IBotSharpRepository>();
40-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
40+
var fileService = _services.GetRequiredService<IFileBasicService>();
4141
var isDeleted = db.DeleteConversations(ids);
4242
fileService.DeleteConversationFiles(ids);
4343
return await Task.FromResult(isDeleted);

src/Infrastructure/BotSharp.Core/Files/FilePlugin.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
2020

2121
if (myFileStorageSettings.Default == FileStorageEnum.LocalFileStorage)
2222
{
23-
services.AddScoped<IBotSharpFileService, BotSharpFileService>();
23+
services.AddScoped<IFileBasicService, FileBasicService>();
2424
}
25+
services.AddScoped<IFileInstructService, FileInstructService>();
2526
}
2627
}

0 commit comments

Comments
 (0)