|
|
|
@ -5,9 +5,11 @@ package ai |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"errors" |
|
|
|
|
|
|
|
"github.com/youruser/base/internal/svc" |
|
|
|
"github.com/youruser/base/internal/types" |
|
|
|
"github.com/youruser/base/model" |
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx" |
|
|
|
) |
|
|
|
@ -28,7 +30,52 @@ func NewAiConversationGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) |
|
|
|
} |
|
|
|
|
|
|
|
func (l *AiConversationGetLogic) AiConversationGet(req *types.AIConversationGetRequest) (resp *types.AIConversationDetailResponse, err error) { |
|
|
|
// todo: add your logic here and delete this line
|
|
|
|
userId, _ := l.ctx.Value("userId").(int64) |
|
|
|
if userId == 0 { |
|
|
|
return nil, errors.New("unauthorized") |
|
|
|
} |
|
|
|
|
|
|
|
conv, err := model.AIConversationFindOne(l.ctx, l.svcCtx.DB, req.Id) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
if conv.UserId != userId { |
|
|
|
return nil, errors.New("forbidden") |
|
|
|
} |
|
|
|
|
|
|
|
// Get messages
|
|
|
|
messages, err := model.AIChatMessageFindByConversation(l.ctx, l.svcCtx.DB, conv.Id) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
msgList := make([]types.AIMessageInfo, len(messages)) |
|
|
|
for i, m := range messages { |
|
|
|
msgList[i] = types.AIMessageInfo{ |
|
|
|
Id: m.Id, |
|
|
|
ConversationId: m.ConversationId, |
|
|
|
Role: m.Role, |
|
|
|
Content: m.Content, |
|
|
|
TokenCount: m.TokenCount, |
|
|
|
Cost: m.Cost, |
|
|
|
ModelId: m.ModelId, |
|
|
|
LatencyMs: m.LatencyMs, |
|
|
|
CreatedAt: m.CreatedAt.Format("2006-01-02 15:04:05"), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return |
|
|
|
return &types.AIConversationDetailResponse{ |
|
|
|
Conversation: types.AIConversationInfo{ |
|
|
|
Id: conv.Id, |
|
|
|
Title: conv.Title, |
|
|
|
ModelId: conv.ModelId, |
|
|
|
ProviderId: conv.ProviderId, |
|
|
|
TotalTokens: conv.TotalTokens, |
|
|
|
TotalCost: conv.TotalCost, |
|
|
|
IsArchived: conv.IsArchived, |
|
|
|
CreatedAt: conv.CreatedAt.Format("2006-01-02 15:04:05"), |
|
|
|
UpdatedAt: conv.UpdatedAt.Format("2006-01-02 15:04:05"), |
|
|
|
}, |
|
|
|
Messages: msgList, |
|
|
|
}, nil |
|
|
|
} |
|
|
|
|