Skip to content

Commit

Permalink
Merge branch 'dev' of private:sdcb/chats into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
greywen committed Dec 5, 2024
2 parents 60719e8 + f29ccac commit bc62ee1
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ jobs:
- name: deploy dev/stg
run: |
ssh -o StrictHostKeyChecking=no -p 22 [email protected] << '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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ internal static async Task<ActionResult<AdminMessageRoot>> 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
{
Expand Down
2 changes: 1 addition & 1 deletion src/BE/Controllers/Chats/Chats/UserChatsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<ActionResult<PagedResult<ChatsResponse>>> GetChats([FromQuery]
IQueryable<Chat> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,8 +50,7 @@ public async Task<IActionResult> 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()
{
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/BE/Controllers/Chats/Messages/MessagesController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,8 +20,7 @@ public async Task<ActionResult<MessageDto[]>> 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()
{
Expand Down Expand Up @@ -52,9 +52,9 @@ public async Task<ActionResult<MessageDto[]>> GetMessages(string chatId, [FromSe
public async Task<ActionResult<string?>> 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)
Expand Down
10 changes: 7 additions & 3 deletions src/BE/Controllers/Chats/Prompts/PromptsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public async Task<ActionResult<PromptDto[]>> 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,
Expand All @@ -36,7 +38,9 @@ public async Task<ActionResult<BriefPromptDto[]>> 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,
Expand Down Expand Up @@ -78,7 +82,7 @@ public async Task<ActionResult<PromptDto>> 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;

Expand Down
34 changes: 11 additions & 23 deletions src/BE/DB/ChatsDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ public ChatsDB(DbContextOptions<ChatsDB> options)

public virtual DbSet<MessageContentFile> MessageContentFiles { get; set; }

public virtual DbSet<MessageContentType> MessageContentTypes { get; set; }

public virtual DbSet<MessageContentUtf16> MessageContentUtf16s { get; set; }
public virtual DbSet<MessageContentText> MessageContentTexts { get; set; }

public virtual DbSet<MessageContentUtf8> MessageContentUtf8s { get; set; }
public virtual DbSet<MessageContentType> MessageContentTypes { get; set; }

public virtual DbSet<Model> Models { get; set; }

Expand Down Expand Up @@ -169,7 +167,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<FileImageInfo>(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();

Expand All @@ -187,7 +185,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<FileServiceType>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__FileServ__3214EC07B386AD0F");
entity.HasKey(e => e.Id).HasName("PK__FileServ__3214EC0777BE06D3");
});

modelBuilder.Entity<InvitationCode>(entity =>
Expand Down Expand Up @@ -228,7 +226,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<MessageContentBlob>(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();

Expand All @@ -237,7 +235,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<MessageContentFile>(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();

Expand All @@ -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<MessageContentType>(entity =>
modelBuilder.Entity<MessageContentText>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__MessageC__3214EC07D7BA864A");
});

modelBuilder.Entity<MessageContentUtf16>(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<MessageContentUtf8>(entity =>
modelBuilder.Entity<MessageContentType>(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<Model>(entity =>
Expand Down
10 changes: 5 additions & 5 deletions src/BE/DB/Extensions/MessageContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public async Task<ChatMessageContentPart> 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()
};
Expand All @@ -20,16 +20,16 @@ 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(),
};
}

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)
Expand All @@ -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 };
}
}
5 changes: 1 addition & 4 deletions src/BE/DB/MessageContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

namespace Chats.BE.DB;

[Table("MessageContentUTF16")]
public partial class MessageContentUtf16
[Table("MessageContentText")]
public partial class MessageContentText
{
[Key]
public long Id { get; set; }

public string Content { get; set; } = null!;

[ForeignKey("Id")]
[InverseProperty("MessageContentUtf16")]
[InverseProperty("MessageContentText")]
public virtual MessageContent IdNavigation { get; set; } = null!;
}
21 changes: 0 additions & 21 deletions src/BE/DB/MessageContentUtf8.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/FE/components/HomeContent/HomeContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 0 additions & 19 deletions src/scripts/20241112-db-migration.linq
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
<Query Kind="Program">
<Connection>
<ID>0397d1ca-d774-43d2-965d-8797d2cc52f1</ID>
<NamingServiceVersion>2</NamingServiceVersion>
<Persist>true</Persist>
<Driver Assembly="(internal)" PublicKeyToken="no-strong-name">LINQPad.Drivers.EFCore.DynamicDriver</Driver>
<AllowDateOnlyTimeOnly>true</AllowDateOnlyTimeOnly>
<Server>home.starworks.cc,37965</Server>
<SqlSecurity>true</SqlSecurity>
<UserName>sa</UserName>
<Password>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAVbvrs6Jk5ESNEegA7jfNnwAAAAACAAAAAAAQZgAAAAEAACAAAABBe4Cv13ASn/ipSipWlPY2mowD6zfbngQJ+Z3+KTcGVAAAAAAOgAAAAAIAACAAAABMtXPZtcXWRzq2o3sHXx1+BVuNY62ubQdNn8IrSrXSxiAAAADcCHwb5SZfaPyiOsqcpFEU4/Lnue6fZBOLd4rAOBXt0UAAAADS65U3H6M+54aUZec9Yj/zdoSeT/Jb3S4gVVhk51Sje3lBRb4jCfUa42pVHALQoRPrtMP5lJSaJ/hB6XoT2mV6</Password>
<Database>ChatsDEV</Database>
<DriverData>
<EncryptSqlTraffic>False</EncryptSqlTraffic>
<PreserveNumeric1>True</PreserveNumeric1>
<EFProvider>Microsoft.EntityFrameworkCore.SqlServer</EFProvider>
<NoMARS>True</NoMARS>
<TrustServerCertificate>False</TrustServerCertificate>
</DriverData>
</Connection>
<NuGetReference>Microsoft.Extensions.Caching.Memory</NuGetReference>
<Namespace>Microsoft.EntityFrameworkCore.Storage</Namespace>
<Namespace>Microsoft.Data.SqlClient</Namespace>
Expand Down
9 changes: 8 additions & 1 deletion src/scripts/20241129-file.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1350,4 +1350,11 @@ FROM [dbo].[MessageContent]
WHERE ContentTypeId = 2;

ALTER TABLE [dbo].[MessageContent]
DROP COLUMN [Content];
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;

0 comments on commit bc62ee1

Please sign in to comment.