Skip to content

Commit d5b74bf

Browse files
committed
Merge branch 'master' of https://github.com/Qtoss-AI/BotSharp
2 parents feb48a1 + 6e611b7 commit d5b74bf

File tree

34 files changed

+275
-178
lines changed

34 files changed

+275
-178
lines changed

src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace BotSharp.Abstraction.Knowledges;
66
public interface IKnowledgeService
77
{
88
#region Vector
9+
Task<bool> ExistVectorCollection(string collectionName);
910
Task<bool> CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model);
1011
Task<bool> DeleteVectorCollection(string collectionName);
1112
Task<IEnumerable<string>> GetVectorCollections(string type);

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public interface IBotSharpRepository
116116
bool AddKnowledgeCollectionConfigs(List<VectorCollectionConfig> configs, bool reset = false);
117117
bool DeleteKnowledgeCollectionConfig(string collectionName);
118118
IEnumerable<VectorCollectionConfig> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter);
119-
120119
bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData);
121120
/// <summary>
122121
/// Delete file meta data in a knowledge collection, given the vector store provider. If "fileId" is null, delete all in the collection.

src/Infrastructure/BotSharp.Abstraction/Users/IUserIdentity.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public interface IUserIdentity
1010
string FullName { get; }
1111
string? UserLanguage { get; }
1212
string? Phone { get; }
13+
string? AffiliateId { get; }
1314
}

src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace BotSharp.Abstraction.VectorStorage;
55
public interface IVectorDb
66
{
77
string Provider { get; }
8-
8+
9+
Task<bool> DoesCollectionExist(string collectionName);
910
Task<IEnumerable<string>> GetCollections();
1011
Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionData(string collectionName, VectorFilter filter);
1112
Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids, bool withPayload = false, bool withVector = false);

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public PagedItems<KnowledgeDocMetaData> GetKnowledgeBaseFileMeta(string collecti
213213

214214
return new PagedItems<KnowledgeDocMetaData>
215215
{
216-
Items = records.Skip(filter.Offset).Take(filter.Size),
216+
Items = records.OrderByDescending(x => x.CreateDate).Skip(filter.Offset).Take(filter.Size),
217217
Count = records.Count
218218
};
219219
}

src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@ public string? UserLanguage
6969

7070
[JsonPropertyName("phone")]
7171
public string? Phone => _claims?.FirstOrDefault(x => x.Type == "phone")?.Value;
72+
73+
[JsonPropertyName("affiliateId")]
74+
public string? AffiliateId => _claims?.FirstOrDefault(x => x.Type == "affiliateId")?.Value;
7275
}

src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ private string GenerateJwtToken(User user)
254254
new Claim("type", user.Type ?? UserType.Client),
255255
new Claim("role", user.Role ?? UserRole.User),
256256
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
257-
new Claim("phone", user.Phone ?? string.Empty)
257+
new Claim("phone", user.Phone ?? string.Empty),
258+
new Claim("affiliateId", user.AffiliateId ?? string.Empty)
258259
};
259260

260261
var validators = _services.GetServices<IAuthenticationHook>();
@@ -280,14 +281,14 @@ private string GenerateJwtToken(User user)
280281
};
281282
var tokenHandler = new JwtSecurityTokenHandler();
282283
var token = tokenHandler.CreateToken(tokenDescriptor);
283-
SaveUserTokenExpiresCache(user.Id, expires).GetAwaiter().GetResult();
284+
SaveUserTokenExpiresCache(user.Id, expires, expireInMinutes).GetAwaiter().GetResult();
284285
return tokenHandler.WriteToken(token);
285286
}
286287

287-
private async Task SaveUserTokenExpiresCache(string userId, DateTime expires)
288+
private async Task SaveUserTokenExpiresCache(string userId, DateTime expires, int expireInMinutes)
288289
{
289290
var _cacheService = _services.GetRequiredService<ICacheService>();
290-
await _cacheService.SetAsync<DateTime>(GetUserTokenExpiresCacheKey(userId), expires, null);
291+
await _cacheService.SetAsync<DateTime>(GetUserTokenExpiresCacheKey(userId), expires, TimeSpan.FromMinutes(expireInMinutes));
291292
}
292293

293294
private string GetUserTokenExpiresCacheKey(string userId)

src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BotSharp.Abstraction.Files.Utilities;
22
using BotSharp.Abstraction.Graph.Models;
3-
using BotSharp.Abstraction.Knowledges.Models;
43
using BotSharp.Abstraction.VectorStorage.Models;
54
using BotSharp.OpenAPI.ViewModels.Knowledges;
65

@@ -20,6 +19,12 @@ public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvi
2019
}
2120

2221
#region Vector
22+
[HttpGet("knowledge/vector/{collection}/exist")]
23+
public async Task<bool> ExistVectorCollection([FromRoute] string collection)
24+
{
25+
return await _knowledgeService.ExistVectorCollection(collection);
26+
}
27+
2328
[HttpGet("knowledge/vector/collections")]
2429
public async Task<IEnumerable<string>> GetVectorCollections([FromQuery] string type)
2530
{

src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemoryVectorDb.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ public class MemoryVectorDb : IVectorDb
1010

1111
public string Provider => "MemoryVector";
1212

13+
14+
public async Task<bool> DoesCollectionExist(string collectionName)
15+
{
16+
return false;
17+
}
18+
1319
public async Task<bool> CreateCollection(string collectionName, int dimension)
1420
{
1521
_collections[collectionName] = dimension;

src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using BotSharp.Abstraction.Files.Utilities;
44
using BotSharp.Abstraction.Knowledges.Helpers;
55
using BotSharp.Abstraction.VectorStorage.Enums;
6-
using System.Collections;
76
using System.Net.Http;
87
using System.Net.Mime;
98

@@ -13,13 +12,21 @@ public partial class KnowledgeService
1312
{
1413
public async Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files)
1514
{
15+
var res = new UploadKnowledgeResponse
16+
{
17+
Success = [],
18+
Failed = files?.Select(x => x.FileName) ?? new List<string>()
19+
};
20+
1621
if (string.IsNullOrWhiteSpace(collectionName) || files.IsNullOrEmpty())
1722
{
18-
return new UploadKnowledgeResponse
19-
{
20-
Success = [],
21-
Failed = files?.Select(x => x.FileName) ?? new List<string>()
22-
};
23+
return res;
24+
}
25+
26+
var exist = await ExistVectorCollection(collectionName);
27+
if (!exist)
28+
{
29+
return res;
2330
}
2431

2532
var db = _services.GetRequiredService<IBotSharpRepository>();
@@ -103,6 +110,9 @@ public async Task<bool> ImportDocumentContentToKnowledge(string collectionName,
103110

104111
try
105112
{
113+
var exist = await ExistVectorCollection(collectionName);
114+
if (!exist) return false;
115+
106116
var db = _services.GetRequiredService<IBotSharpRepository>();
107117
var userId = await GetUserId();
108118
var vectorStoreProvider = _settings.VectorDb.Provider;

0 commit comments

Comments
 (0)