diff --git a/.github/workflows/build-container.yml b/.github/workflows/build-container.yml index 7db1d62e..9ffc07ef 100644 --- a/.github/workflows/build-container.yml +++ b/.github/workflows/build-container.yml @@ -397,7 +397,7 @@ jobs: - name: deploy dev/stg run: | - ssh -o StrictHostKeyChecking=no -p 22 zhoujie@home.starworks.cc << 'EOF' + ssh -o StrictHostKeyChecking=no -p 22 ${{ secrets.SSH_TARGET }} << 'EOF' docker pull ${{ vars.DOCKER_REGISTRY }}/${{ vars.DOCKER_NAMESPACE }}/chats:r${{ github.run_number }}-linux-x64 cd chats sed -i "s/^TAG=.*/TAG=r${{ github.run_number }}-linux-x64/" ~/chats/dev.env diff --git a/src/BE/Controllers/Admin/AdminMessage/AdminMessageController.cs b/src/BE/Controllers/Admin/AdminMessage/AdminMessageController.cs index f0ccfedb..52cc0627 100644 --- a/src/BE/Controllers/Admin/AdminMessage/AdminMessageController.cs +++ b/src/BE/Controllers/Admin/AdminMessage/AdminMessageController.cs @@ -75,8 +75,7 @@ internal static async Task> GetAdminMessageIntern AdminMessageItemTemp[] messagesTemp = await db.Messages .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentBlob) .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentFile).ThenInclude(x => x!.File).ThenInclude(x => x.FileService) - .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentUtf16) - .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentUtf8) + .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentText) .Where(x => x.ChatId == conversationId) .Select(x => new AdminMessageItemTemp { diff --git a/src/BE/Controllers/Chats/Chats/UserChatsController.cs b/src/BE/Controllers/Chats/Chats/UserChatsController.cs index be4a5b73..8c8f9727 100644 --- a/src/BE/Controllers/Chats/Chats/UserChatsController.cs +++ b/src/BE/Controllers/Chats/Chats/UserChatsController.cs @@ -55,7 +55,7 @@ public async Task>> GetChats([FromQuery] IQueryable query = db.Chats .Include(x => x.Model) .Where(x => x.UserId == currentUser.Id && !x.IsDeleted) - .OrderByDescending(x => x.CreatedAt); + .OrderByDescending(x => x.Id); if (!string.IsNullOrWhiteSpace(request.Query)) { query = query.Where(x => x.Title.Contains(request.Query)); diff --git a/src/BE/Controllers/Chats/Conversations/ConversationController.cs b/src/BE/Controllers/Chats/Conversations/ConversationController.cs index 71630602..8828e029 100644 --- a/src/BE/Controllers/Chats/Conversations/ConversationController.cs +++ b/src/BE/Controllers/Chats/Conversations/ConversationController.cs @@ -15,6 +15,7 @@ using Sdcb.DashScope; using System.ClientModel; using System.Text.Json; +using TencentCloud.Common; using OpenAIChatMessage = OpenAI.Chat.ChatMessage; namespace Chats.BE.Controllers.Chats.Conversations; @@ -49,8 +50,7 @@ public async Task StartConversationStreamed( .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentBlob) .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentFile).ThenInclude(x => x!.File).ThenInclude(x => x.FileService) .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentFile).ThenInclude(x => x!.File).ThenInclude(x => x.FileImageInfo) - .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentUtf16) - .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentUtf8) + .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentText) .Where(x => x.ChatId == chatId && x.Chat.UserId == currentUser.Id) .Select(x => new MessageLiteDto() { @@ -184,7 +184,7 @@ ..await GetMessageTree(existingMessages, messageId).ToAsyncEnumerable().SelectAw icc.FinishReason = cse.ErrorCode; return this.BadRequestMessage(cse.Message); } - catch (Exception e) when (e is DashScopeException || e is ClientResultException) + catch (Exception e) when (e is DashScopeException or ClientResultException or TencentCloudSDKException) { icc.FinishReason = DBFinishReason.UpstreamError; errorText = e.Message; diff --git a/src/BE/Controllers/Chats/Messages/MessagesController.cs b/src/BE/Controllers/Chats/Messages/MessagesController.cs index 6748ff0b..f14732b5 100644 --- a/src/BE/Controllers/Chats/Messages/MessagesController.cs +++ b/src/BE/Controllers/Chats/Messages/MessagesController.cs @@ -1,5 +1,6 @@ using Chats.BE.Controllers.Chats.Messages.Dtos; using Chats.BE.DB; +using Chats.BE.DB.Enums; using Chats.BE.Infrastructure; using Chats.BE.Services.Conversations; using Chats.BE.Services.FileServices; @@ -19,8 +20,7 @@ public async Task> GetMessages(string chatId, [FromSe MessageDto[] messages = await db.Messages .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentBlob) .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentFile).ThenInclude(x => x!.File).ThenInclude(x => x.FileService) - .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentUtf16) - .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentUtf8) + .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentText) .Where(m => m.ChatId == urlEncryption.DecryptChatId(chatId) && m.Chat.UserId == currentUser.Id && m.ChatRoleId != (byte)DBChatRole.System) .Select(x => new ChatMessageTemp() { @@ -52,9 +52,9 @@ public async Task> GetMessages(string chatId, [FromSe public async Task> GetChatSystemPrompt(string chatId, CancellationToken cancellationToken) { MessageContent? content = await db.Messages - .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentUtf16) + .Include(x => x.MessageContents).ThenInclude(x => x.MessageContentText) .Where(m => m.ChatId == urlEncryption.DecryptChatId(chatId) && m.ChatRoleId == (byte)DBChatRole.System) - .Select(x => x.MessageContents.First()) + .Select(x => x.MessageContents.First(x => x.ContentTypeId == (byte)DBMessageContentType.Text)) .FirstOrDefaultAsync(cancellationToken); if (content == null) diff --git a/src/BE/Controllers/Chats/Prompts/PromptsController.cs b/src/BE/Controllers/Chats/Prompts/PromptsController.cs index db134746..65471883 100644 --- a/src/BE/Controllers/Chats/Prompts/PromptsController.cs +++ b/src/BE/Controllers/Chats/Prompts/PromptsController.cs @@ -16,7 +16,9 @@ public async Task> GetPrompts(CancellationToken cancel { PromptDto[] prompts = await db.Prompts .Where(x => x.CreateUserId == currentUser.Id || currentUser.IsAdmin && x.IsSystem) - .OrderBy(x => x.UpdatedAt) + .OrderBy(x => x.IsSystem) + .ThenBy(x => x.IsDefault) + .ThenBy(x => x.UpdatedAt) .Select(x => new PromptDto() { Content = x.Content, @@ -36,7 +38,9 @@ public async Task> GetBriefPrompts(CancellationTo { BriefPromptDto[] prompts = await db.Prompts .Where(x => x.CreateUserId == currentUser.Id || currentUser.IsAdmin && x.IsSystem) - .OrderBy(x => x.UpdatedAt) + .OrderBy(x => x.IsSystem) + .ThenBy(x => x.IsDefault) + .ThenBy(x => x.UpdatedAt) .Select(x => new BriefPromptDto() { Id = x.Id, @@ -78,7 +82,7 @@ public async Task> GetDefaultPrompt(CancellationToken ca .FirstOrDefaultAsync(cancellationToken); Prompt? systemDefault = await db.Prompts .OrderByDescending(x => x.UpdatedAt) - .Where(x => x.IsDefault && x.IsSystem) + .Where(x => x.IsSystem) .FirstOrDefaultAsync(cancellationToken); Prompt? consolidated = userDefault ?? systemDefault; diff --git a/src/BE/DB/ChatsDB.cs b/src/BE/DB/ChatsDB.cs index a962cdeb..425bdd34 100644 --- a/src/BE/DB/ChatsDB.cs +++ b/src/BE/DB/ChatsDB.cs @@ -51,11 +51,9 @@ public ChatsDB(DbContextOptions options) public virtual DbSet MessageContentFiles { get; set; } - public virtual DbSet MessageContentTypes { get; set; } - - public virtual DbSet MessageContentUtf16s { get; set; } + public virtual DbSet MessageContentTexts { get; set; } - public virtual DbSet MessageContentUtf8s { get; set; } + public virtual DbSet MessageContentTypes { get; set; } public virtual DbSet Models { get; set; } @@ -169,7 +167,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity(entity => { - entity.HasKey(e => e.FileId).HasName("PK__FileImag__6F0F98BFECCD99B0"); + entity.HasKey(e => e.FileId).HasName("PK__FileImag__6F0F98BF53BA668D"); entity.Property(e => e.FileId).ValueGeneratedNever(); @@ -187,7 +185,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity(entity => { - entity.HasKey(e => e.Id).HasName("PK__FileServ__3214EC07B386AD0F"); + entity.HasKey(e => e.Id).HasName("PK__FileServ__3214EC0777BE06D3"); }); modelBuilder.Entity(entity => @@ -228,7 +226,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity(entity => { - entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC073F7CC783"); + entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC073F90F69C"); entity.Property(e => e.Id).ValueGeneratedNever(); @@ -237,7 +235,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity(entity => { - entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC072B2C47D4"); + entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC0725A12791"); entity.Property(e => e.Id).ValueGeneratedNever(); @@ -246,28 +244,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.HasOne(d => d.IdNavigation).WithOne(p => p.MessageContentFile).HasConstraintName("FK_MessageContentFile_MessageContent"); }); - modelBuilder.Entity(entity => + modelBuilder.Entity(entity => { - entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC07D7BA864A"); - }); - - modelBuilder.Entity(entity => - { - entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC070448BEFF"); + entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC0778761FFD"); entity.Property(e => e.Id).ValueGeneratedNever(); - entity.HasOne(d => d.IdNavigation).WithOne(p => p.MessageContentUtf16).HasConstraintName("FK_MessageContentUTF16_MessageContent"); + entity.HasOne(d => d.IdNavigation).WithOne(p => p.MessageContentText).HasConstraintName("FK_MessageContentUTF16_MessageContent"); }); - modelBuilder.Entity(entity => + modelBuilder.Entity(entity => { - entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC0755163DAC"); - - entity.Property(e => e.Id).ValueGeneratedNever(); - entity.Property(e => e.Content).UseCollation("Latin1_General_100_CI_AS_SC_UTF8"); - - entity.HasOne(d => d.IdNavigation).WithOne(p => p.MessageContentUtf8).HasConstraintName("FK_MessageContentUTF8_MessageContent"); + entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC07D7BA864A"); }); modelBuilder.Entity(entity => diff --git a/src/BE/DB/Extensions/MessageContent.cs b/src/BE/DB/Extensions/MessageContent.cs index 68585a9d..79caf12e 100644 --- a/src/BE/DB/Extensions/MessageContent.cs +++ b/src/BE/DB/Extensions/MessageContent.cs @@ -10,7 +10,7 @@ public async Task ToOpenAI(FileUrlProvider fup, Cancella { return (DBMessageContentType)ContentTypeId switch { - DBMessageContentType.Text => ChatMessageContentPart.CreateTextPart(MessageContentUtf16!.Content), + DBMessageContentType.Text => ChatMessageContentPart.CreateTextPart(MessageContentText!.Content), DBMessageContentType.FileId => await fup.CreateOpenAIPart(MessageContentFile!, cancellationToken), _ => throw new NotImplementedException() }; @@ -20,8 +20,8 @@ public override string ToString() { return (DBMessageContentType)ContentTypeId switch { - DBMessageContentType.Text => MessageContentUtf16!.Content, - DBMessageContentType.Error => MessageContentUtf8!.Content, + DBMessageContentType.Text => MessageContentText!.Content, + DBMessageContentType.Error => MessageContentText!.Content, //DBMessageContentType.FileId => MessageContentUtil.ReadFileId(Content).ToString(), // not supported _ => throw new NotSupportedException(), }; @@ -29,7 +29,7 @@ public override string ToString() public static MessageContent FromText(string text) { - return new MessageContent { MessageContentUtf16 = new() { Content = text }, ContentTypeId = (byte)DBMessageContentType.Text }; + return new MessageContent { MessageContentText = new() { Content = text }, ContentTypeId = (byte)DBMessageContentType.Text }; } public static MessageContent FromFileId(int fileId) @@ -39,6 +39,6 @@ public static MessageContent FromFileId(int fileId) public static MessageContent FromError(string error) { - return new MessageContent { MessageContentUtf8 = new() { Content = error }, ContentTypeId = (byte)DBMessageContentType.Error }; + return new MessageContent { MessageContentText = new() { Content = error }, ContentTypeId = (byte)DBMessageContentType.Error }; } } diff --git a/src/BE/DB/MessageContent.cs b/src/BE/DB/MessageContent.cs index b8530e6d..dbf26d72 100644 --- a/src/BE/DB/MessageContent.cs +++ b/src/BE/DB/MessageContent.cs @@ -32,8 +32,5 @@ public partial class MessageContent public virtual MessageContentFile? MessageContentFile { get; set; } [InverseProperty("IdNavigation")] - public virtual MessageContentUtf16? MessageContentUtf16 { get; set; } - - [InverseProperty("IdNavigation")] - public virtual MessageContentUtf8? MessageContentUtf8 { get; set; } + public virtual MessageContentText? MessageContentText { get; set; } } diff --git a/src/BE/DB/MessageContentUtf16.cs b/src/BE/DB/MessageContentText.cs similarity index 77% rename from src/BE/DB/MessageContentUtf16.cs rename to src/BE/DB/MessageContentText.cs index e53adff3..56a67419 100644 --- a/src/BE/DB/MessageContentUtf16.cs +++ b/src/BE/DB/MessageContentText.cs @@ -6,8 +6,8 @@ namespace Chats.BE.DB; -[Table("MessageContentUTF16")] -public partial class MessageContentUtf16 +[Table("MessageContentText")] +public partial class MessageContentText { [Key] public long Id { get; set; } @@ -15,6 +15,6 @@ public partial class MessageContentUtf16 public string Content { get; set; } = null!; [ForeignKey("Id")] - [InverseProperty("MessageContentUtf16")] + [InverseProperty("MessageContentText")] public virtual MessageContent IdNavigation { get; set; } = null!; } diff --git a/src/BE/DB/MessageContentUtf8.cs b/src/BE/DB/MessageContentUtf8.cs deleted file mode 100644 index 5fe51a69..00000000 --- a/src/BE/DB/MessageContentUtf8.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using Microsoft.EntityFrameworkCore; - -namespace Chats.BE.DB; - -[Table("MessageContentUTF8")] -public partial class MessageContentUtf8 -{ - [Key] - public long Id { get; set; } - - [Unicode(false)] - public string Content { get; set; } = null!; - - [ForeignKey("Id")] - [InverseProperty("MessageContentUtf8")] - public virtual MessageContent IdNavigation { get; set; } = null!; -} diff --git a/src/FE/components/HomeContent/HomeContent.tsx b/src/FE/components/HomeContent/HomeContent.tsx index 91737106..9f118b66 100644 --- a/src/FE/components/HomeContent/HomeContent.tsx +++ b/src/FE/components/HomeContent/HomeContent.tsx @@ -298,7 +298,7 @@ const HomeContent = () => { }); let chatList = rows; if (!modelList) { - chatList = rows.concat(chats); + chatList = chats.concat(rows); } dispatch({ field: 'chats', value: chatList }); if (modelList) { diff --git a/src/scripts/20241112-db-migration.linq b/src/scripts/20241112-db-migration.linq index 7a77c0bd..10bca853 100644 --- a/src/scripts/20241112-db-migration.linq +++ b/src/scripts/20241112-db-migration.linq @@ -1,23 +1,4 @@ - - 0397d1ca-d774-43d2-965d-8797d2cc52f1 - 2 - true - LINQPad.Drivers.EFCore.DynamicDriver - true - home.starworks.cc,37965 - true - sa - AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAVbvrs6Jk5ESNEegA7jfNnwAAAAACAAAAAAAQZgAAAAEAACAAAABBe4Cv13ASn/ipSipWlPY2mowD6zfbngQJ+Z3+KTcGVAAAAAAOgAAAAAIAACAAAABMtXPZtcXWRzq2o3sHXx1+BVuNY62ubQdNn8IrSrXSxiAAAADcCHwb5SZfaPyiOsqcpFEU4/Lnue6fZBOLd4rAOBXt0UAAAADS65U3H6M+54aUZec9Yj/zdoSeT/Jb3S4gVVhk51Sje3lBRb4jCfUa42pVHALQoRPrtMP5lJSaJ/hB6XoT2mV6 - ChatsDEV - - False - True - Microsoft.EntityFrameworkCore.SqlServer - True - False - - Microsoft.Extensions.Caching.Memory Microsoft.EntityFrameworkCore.Storage Microsoft.Data.SqlClient diff --git a/src/scripts/20241129-file.sql b/src/scripts/20241129-file.sql index c775b608..ffb944e2 100644 --- a/src/scripts/20241129-file.sql +++ b/src/scripts/20241129-file.sql @@ -1350,4 +1350,11 @@ FROM [dbo].[MessageContent] WHERE ContentTypeId = 2; ALTER TABLE [dbo].[MessageContent] -DROP COLUMN [Content]; \ No newline at end of file +DROP COLUMN [Content]; + + +-- 20241205 +EXECUTE sp_rename N'dbo.MessageContentUTF16', N'MessageContentText', 'OBJECT'; +INSERT INTO MessageContentText(Id, Content) +SELECT Id, Content FROM MessageContentUTF8; +DROP TABLE MessageContentUTF8; \ No newline at end of file