( ctx context.Context, store database.Store, chat database.Chat, modelConfig database.ChatModelConfig, usage fantasy.Usage, activeAPIKeyID string, newTitle string, )
| 3745 | } |
| 3746 | |
| 3747 | func recordManualTitleUsage( |
| 3748 | ctx context.Context, |
| 3749 | store database.Store, |
| 3750 | chat database.Chat, |
| 3751 | modelConfig database.ChatModelConfig, |
| 3752 | usage fantasy.Usage, |
| 3753 | activeAPIKeyID string, |
| 3754 | newTitle string, |
| 3755 | ) (database.Chat, error) { |
| 3756 | hasUsage := usage != (fantasy.Usage{}) |
| 3757 | if !hasUsage && newTitle == "" { |
| 3758 | return chat, nil |
| 3759 | } |
| 3760 | |
| 3761 | var totalCostMicros *int64 |
| 3762 | if hasUsage { |
| 3763 | callConfig := codersdk.ChatModelCallConfig{} |
| 3764 | if len(modelConfig.Options) > 0 { |
| 3765 | if err := json.Unmarshal(modelConfig.Options, &callConfig); err != nil { |
| 3766 | return database.Chat{}, xerrors.Errorf("parse model call config: %w", err) |
| 3767 | } |
| 3768 | } |
| 3769 | totalCostMicros = chatcost.CalculateTotalCostMicros( |
| 3770 | fantasyUsageToChatMessageUsage(usage), |
| 3771 | callConfig.Cost, |
| 3772 | ) |
| 3773 | } |
| 3774 | |
| 3775 | // Use a valid empty JSON array for the content column. |
| 3776 | // MarshalParts returns a null NullRawMessage for empty |
| 3777 | // slices, which becomes an empty string that PostgreSQL |
| 3778 | // rejects as invalid JSON. |
| 3779 | content := "[]" |
| 3780 | |
| 3781 | updatedChat := chat |
| 3782 | err := store.InTx(func(tx database.Store) error { |
| 3783 | lockedChat, err := tx.GetChatByIDForUpdate(ctx, chat.ID) |
| 3784 | if err != nil { |
| 3785 | return xerrors.Errorf("lock chat for manual title usage: %w", err) |
| 3786 | } |
| 3787 | updatedChat = lockedChat |
| 3788 | if hasUsage { |
| 3789 | messages, err := tx.InsertChatMessages(ctx, database.InsertChatMessagesParams{ |
| 3790 | ChatID: chat.ID, |
| 3791 | CreatedBy: []uuid.UUID{chat.OwnerID}, |
| 3792 | APIKeyID: []string{activeAPIKeyID}, |
| 3793 | ModelConfigID: []uuid.UUID{modelConfig.ID}, |
| 3794 | Role: []database.ChatMessageRole{database.ChatMessageRoleAssistant}, |
| 3795 | Content: []string{content}, |
| 3796 | ContentVersion: []int16{chatprompt.CurrentContentVersion}, |
| 3797 | Visibility: []database.ChatMessageVisibility{database.ChatMessageVisibilityModel}, |
| 3798 | InputTokens: []int64{usage.InputTokens}, |
| 3799 | OutputTokens: []int64{usage.OutputTokens}, |
| 3800 | TotalTokens: []int64{usage.TotalTokens}, |
| 3801 | ReasoningTokens: []int64{usage.ReasoningTokens}, |
| 3802 | CacheCreationTokens: []int64{usage.CacheCreationTokens}, |
| 3803 | CacheReadTokens: []int64{usage.CacheReadTokens}, |
| 3804 | ContextLimit: []int64{modelConfig.ContextLimit}, |
no test coverage detected