mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
GetMessageAnswer and AnthropicMessages each built a writer at the top and threaded it through the rest of the handler, writing as they went. That shape needs the connection held open for the duration, which is what SendStreamWriter does: the body runs inside it and every event reaches the client as it is produced. Each return still ends the answer exactly where it used to. The Anthropic handler serves both shapes from one body. Streaming, zip hands the writer in; otherwise the writer only accumulates and the reply goes out whole at the end, so the sink it was handed is never written to. 909 compile errors down to 21.
653 lines
20 KiB
Go
653 lines
20 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2024 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/hanzoai/ai/agent"
|
|
"github.com/hanzoai/ai/embedding"
|
|
|
|
"github.com/hanzoai/ai/model"
|
|
"github.com/hanzoai/ai/object"
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
// GetMessageAnswer
|
|
// @Title GetMessageAnswer
|
|
// @Tag Message API
|
|
// @Description get message answer
|
|
// @Param id query string true "The id of message"
|
|
// @Success 200 {stream} string "An event stream of message answers in JSON format"
|
|
// @router /get-message-answer [get]
|
|
func (c *ApiController) GetMessageAnswer() {
|
|
id := c.Input().Get("id")
|
|
|
|
c.SetHeader("Content-Type", "text/event-stream")
|
|
c.SetHeader("Cache-Control", "no-cache")
|
|
c.SetHeader("Connection", "keep-alive")
|
|
|
|
message, err := object.GetMessage(id)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
if message == nil {
|
|
c.ResponseErrorStream(message, fmt.Sprintf("The message: %s is not found", id))
|
|
return
|
|
}
|
|
|
|
if message.Author != "AI" {
|
|
c.ResponseErrorStream(message, fmt.Sprintf("The message is invalid, message author should be \"AI\", but got \"%s\"", message.Author))
|
|
return
|
|
}
|
|
if message.ReplyTo == "" {
|
|
c.ResponseErrorStream(message, "The message is invalid, message replyTo should not be empty")
|
|
return
|
|
}
|
|
// Take the message before generating anything for it. This replaces the in-memory
|
|
// `message.Text != ""` test that stood here: that read the answer and acted on it
|
|
// as two steps, so two concurrent requests for one message — a second tab, an SSE
|
|
// reconnect — both saw an unanswered message and both generated AND billed it.
|
|
// The claim tests and acts in one statement, so the database picks one winner.
|
|
claimed, err := object.ClaimMessageAnswer(message)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
if !claimed {
|
|
// A nil message deliberately: ResponseErrorStream PERSISTS the one it is
|
|
// given, and this request's copy was read before the winner wrote its answer.
|
|
// Writing it back would erase the answer we refused to duplicate.
|
|
c.ResponseErrorStream(nil, fmt.Sprintf("The message: %s is already answered or being answered", id))
|
|
return
|
|
}
|
|
// A generation that ends without an answer releases at once, so a failure is
|
|
// retryable now rather than when the lease runs out. A process that dies never
|
|
// gets here — that is what the lease is for.
|
|
defer object.ReleaseMessageAnswer(message)
|
|
|
|
if strings.HasPrefix(message.ErrorText, "error, status code: 400, message: The response was filtered due to the prompt triggering") {
|
|
c.ResponseErrorStream(message, message.ErrorText)
|
|
return
|
|
}
|
|
|
|
chatId := util.GetIdFromOwnerAndName(message.Owner, message.Chat)
|
|
chat, err := object.GetChat(chatId)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
// GetChat answers (nil, nil) for an id no row matches, so the miss arrives here
|
|
// as a nil chat and not as an error. Reading chat.Type off it panics the process.
|
|
// A message can outlive its chat — DeleteChat cascades the message delete on the
|
|
// chat's own owner, so a message stored under a different owner survives as an
|
|
// orphan — and the answer for that orphan is a request away.
|
|
if chat == nil {
|
|
c.ResponseErrorStream(message, fmt.Sprintf("The chat: %s is not found", chatId))
|
|
return
|
|
}
|
|
|
|
if chat.Type != "AI" {
|
|
c.ResponseErrorStream(message, "The chat type must be \"AI\"")
|
|
return
|
|
}
|
|
|
|
storeId := util.GetIdFromOwnerAndName(chat.Owner, chat.Store)
|
|
store, err := object.GetStore(storeId)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
if store == nil {
|
|
c.ResponseErrorStream(message, fmt.Sprintf("The default store is not found"))
|
|
return
|
|
}
|
|
|
|
question := store.Welcome
|
|
var questionMessage *object.Message
|
|
if message.ReplyTo != "Welcome" {
|
|
questionMessage, err = object.GetMessage(util.GetId("admin", message.ReplyTo))
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
if questionMessage == nil {
|
|
c.ResponseErrorStream(message, fmt.Sprintf("The message: %s is not found", id))
|
|
return
|
|
}
|
|
|
|
question = questionMessage.Text
|
|
|
|
question, err = refineQuestionTextViaParsingUrlContent(question, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
if question == "" {
|
|
c.ResponseErrorStream(message, fmt.Sprintf("The question should not be empty"))
|
|
return
|
|
}
|
|
|
|
_, ok := c.CheckSignedIn()
|
|
if !ok {
|
|
var count int
|
|
count, err = object.GetNearMessageCount(message.User, store.LimitMinutes)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
if count > store.Frequency {
|
|
c.ResponseErrorStream(message, "You have queried too many times, please wait for a while")
|
|
return
|
|
}
|
|
}
|
|
|
|
modelProviderName := store.ModelProvider
|
|
if chat.ModelProvider != "" {
|
|
modelProviderName = chat.ModelProvider
|
|
}
|
|
|
|
modelProvider, modelProviderObj, err := object.GetModelProviderFromContext("admin", modelProviderName, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
// Perform dry run to validate user has sufficient balance before expensive operations
|
|
err = validateTransactionBeforeAIGeneration(message, chat, store, question, modelProvider, modelProviderObj, c.GetAcceptLanguage(), c.ResponseErrorStream)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
embeddingProvider, embeddingProviderObj, err := object.GetEmbeddingProviderFromContext("admin", chat.User2, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
_, agentProviderObj, err := object.GetAgentProviderFromContext("admin", store.AgentProvider, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
agentClients, err := object.GetAgentClients(agentProviderObj)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
webSearchEnabled := false
|
|
if questionMessage != nil {
|
|
webSearchEnabled = questionMessage.WebSearchEnabled
|
|
}
|
|
agentClients = agent.MergeBuiltinAndWebSearchTools(agentClients, store.BuiltinTools, webSearchEnabled)
|
|
|
|
knowledgeCount := store.KnowledgeCount
|
|
if knowledgeCount <= 0 {
|
|
knowledgeCount = 10
|
|
}
|
|
|
|
knowledge, vectorScores, embeddingResult, err := object.GetNearestKnowledge(store.Name, store.VectorStores, store.SearchProvider, embeddingProvider, embeddingProviderObj, modelProvider, "admin", question, knowledgeCount, c.GetAcceptLanguage())
|
|
if err != nil && err.Error() != "no knowledge vectors found" {
|
|
err = fmt.Errorf("%s", fmt.Sprintf(c.T("message_answer:object.GetNearestKnowledge() error, %s"), err.Error()))
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
if embeddingResult == nil {
|
|
embeddingResult = &embedding.EmbeddingResult{}
|
|
}
|
|
|
|
// The answer is streamed: zip holds the connection open and hands the
|
|
// writer in, so each event reaches the client as it is produced. Every
|
|
// return below ends the stream exactly where it used to end the reply.
|
|
_ = c.SendStreamWriter(func(w *bufio.Writer) {
|
|
writer := &RefinedWriter{w, *NewCleaner(6), []byte{}, []byte{}, []byte{}, []byte{}, []byte{}}
|
|
|
|
if questionMessage != nil {
|
|
questionMessage.TokenCount = embeddingResult.TokenCount
|
|
questionMessage.Price = embeddingResult.Price
|
|
questionMessage.Currency = embeddingResult.Currency
|
|
|
|
_, err = object.UpdateMessage(questionMessage.GetId(), questionMessage, false)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
history, err := object.GetRecentRawMessages(chat.Name, message.CreatedTime, store.MemoryLimit)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Question: [%s]\n", question)
|
|
fmt.Printf("Knowledge: [\n")
|
|
for i, k := range knowledge {
|
|
fmt.Printf("Knowledge %d: [%s]\n", i, k.Text)
|
|
}
|
|
fmt.Printf("]\n")
|
|
// fmt.Printf("Refined Question: [%s]\n", realQuestion)
|
|
fmt.Printf("Answer: [")
|
|
|
|
prompt := store.Prompt
|
|
if modelProvider.Type != "Dummy" && !isReasonModel(modelProvider.SubType) {
|
|
if modelProvider.Type == "Alibaba Cloud" && webSearchEnabled {
|
|
prompt, err = getPromptWithCarrier(prompt, store.SuggestionCount, chat.NeedTitle)
|
|
} else {
|
|
question, err = getQuestionWithCarriers(question, store.SuggestionCount, chat.NeedTitle)
|
|
}
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
var modelResult *model.ModelResult
|
|
if agentClients != nil {
|
|
messages := &model.AgentMessages{
|
|
Messages: []*model.RawMessage{},
|
|
ToolCalls: nil,
|
|
}
|
|
agentInfo := &model.AgentInfo{
|
|
AgentClients: agentClients,
|
|
AgentMessages: messages,
|
|
}
|
|
modelResult, err = model.QueryTextWithTools(modelProviderObj, question, writer, history, prompt, knowledge, agentInfo, c.GetAcceptLanguage())
|
|
} else {
|
|
if isReasonModel(modelProvider.SubType) {
|
|
modelResult, err = QueryCarrierText(question, writer, history, prompt, knowledge, modelProviderObj, chat.NeedTitle, store.SuggestionCount, c.GetAcceptLanguage())
|
|
} else {
|
|
modelResult, err = modelProviderObj.QueryText(question, writer, history, prompt, knowledge, nil, c.GetAcceptLanguage())
|
|
}
|
|
}
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "write tcp") {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
if len(vectorScores) > 0 {
|
|
bytes, err := json.Marshal(vectorScores)
|
|
if err == nil {
|
|
_, _ = w.Write([]byte(fmt.Sprintf("event: vector\ndata: %s\n\n", string(bytes))))
|
|
}
|
|
}
|
|
|
|
if writer.writerCleaner.cleaned == false {
|
|
cleanedData := writer.writerCleaner.GetCleanedData()
|
|
writer.buf = append(writer.buf, []byte(cleanedData)...)
|
|
jsonData, err := ConvertMessageDataToJSON(cleanedData)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
_, err = writer.Writer.Write([]byte(fmt.Sprintf("event: message\ndata: %s\n\n", jsonData)))
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
_ = writer.Writer.Flush()
|
|
fmt.Print(cleanedData)
|
|
}
|
|
|
|
fmt.Printf("]\n")
|
|
|
|
event := fmt.Sprintf("event: end\ndata: %s\n\n", "end")
|
|
_, err = w.Write([]byte(event))
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
answer := writer.MessageString()
|
|
message.ReasonText = writer.ReasonString()
|
|
message.ToolCalls = model.GetToolCallsFromWriter(writer.ToolString())
|
|
searchString := writer.SearchString()
|
|
if searchString != "" {
|
|
var searchResults []model.SearchResult
|
|
err := json.Unmarshal([]byte(searchString), &searchResults)
|
|
if err == nil {
|
|
message.SearchResults = searchResults
|
|
}
|
|
}
|
|
|
|
message.TokenCount = modelResult.TotalTokenCount
|
|
message.Price = modelResult.TotalPrice
|
|
message.Currency = modelResult.Currency
|
|
|
|
// Land the casibase chat turn in the ONE usage warehouse + o11y span plane, so
|
|
// this surface is no longer warehouse/o11y-blind. Attribution is the chat's own
|
|
// org (the store owner), not a bearer principal — this path is session/store-
|
|
// scoped. Trace only: this completion's ONE debit is AddTransactionForMessage
|
|
// below.
|
|
c.recordCasibaseChatUsage(chat, modelProvider, modelResult)
|
|
|
|
textAnswer := answer
|
|
textSuggestions := []object.Suggestion{}
|
|
textTitle := ""
|
|
textAnswer, textSuggestions, textTitle, err = parseAnswerWithCarriers(answer, store.SuggestionCount, chat.NeedTitle)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
message.Text = textAnswer
|
|
if message.Text != "" {
|
|
message.ErrorText = ""
|
|
message.IsAlerted = false
|
|
}
|
|
|
|
message.Suggestions = textSuggestions
|
|
|
|
message.VectorScores = vectorScores
|
|
|
|
// Normalize price precision before persisting or creating transactions
|
|
message.Price = model.AddPrices(message.Price, 0)
|
|
|
|
// The generation has TERMINATED. Record that before the charge, because it is what
|
|
// stops this turn being generated and charged a second time — and neither the text
|
|
// nor the claim can say it: an empty completion leaves `text = ''`, which is the
|
|
// condition every claim tests, and the claim itself lapses at the end of its lease.
|
|
// Settling first also covers a persist below that fails after the debit landed.
|
|
if err = object.SettleMessageAnswer(message); err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
// Add transaction for message with price
|
|
err = object.AddTransactionForMessage(message)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
_, err = object.UpdateMessage(message.GetId(), message, false)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
|
|
chat.TokenCount += message.TokenCount
|
|
chat.Price += message.Price
|
|
if chat.Currency == "" {
|
|
chat.Currency = message.Currency
|
|
}
|
|
|
|
// Update chat's ModelProvider if not set
|
|
if chat.ModelProvider == "" {
|
|
chat.ModelProvider = modelProvider.Name
|
|
}
|
|
|
|
if chat.NeedTitle && textTitle != "" {
|
|
chat.DisplayName = textTitle
|
|
chat.NeedTitle = false
|
|
}
|
|
|
|
if questionMessage != nil {
|
|
if chat.Currency == questionMessage.Currency {
|
|
chat.TokenCount += questionMessage.TokenCount
|
|
chat.Price += questionMessage.Price
|
|
}
|
|
}
|
|
|
|
_, err = object.UpdateChat(chat.GetId(), chat)
|
|
if err != nil {
|
|
c.ResponseErrorStream(message, err.Error())
|
|
return
|
|
}
|
|
})
|
|
}
|
|
|
|
// GetAnswer
|
|
// @Title GetAnswer
|
|
// @Tag Message API
|
|
// @Description get answer
|
|
// @Param provider query string true "The provider"
|
|
// @Param question query string true "The question of message"
|
|
// @Param framework query string true "The framework"
|
|
// @Param video query string true "The video"
|
|
// @Success 200 {string} string "answer message"
|
|
// @router /get-answer [get]
|
|
func (c *ApiController) GetAnswer() {
|
|
userName, ok := c.RequireSignedIn()
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
provider := c.Input().Get("provider")
|
|
question := c.Input().Get("question")
|
|
framework := c.Input().Get("framework")
|
|
video := c.Input().Get("video")
|
|
|
|
if question == "" {
|
|
c.ResponseError(fmt.Sprintf("The question should not be empty"))
|
|
return
|
|
}
|
|
|
|
category := "Custom"
|
|
chatName := fmt.Sprintf("chat_%s", util.GetRandomName())
|
|
if framework != "" {
|
|
if video == "" {
|
|
category = "FrameworkTest"
|
|
chatName = framework
|
|
} else {
|
|
category = "FrameworkVideoRun"
|
|
chatName = fmt.Sprintf("%s - %s", video, framework)
|
|
}
|
|
}
|
|
|
|
// Resolve the provider ONCE: the gate below estimates against it and the
|
|
// completion runs on it, so the call this gate prices is the call it admits.
|
|
modelProvider, modelProviderObj, err := object.GetModelProviderFromContext(chatOwner, provider, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
// Refuse before spending upstream when the payer cannot cover the estimate — the
|
|
// same pre-flight gate every chat answer runs. `provider` is a client-named model,
|
|
// so an ungated caller here picks the priciest one and runs it on credit.
|
|
err = gateBalance(chatOwner, userName, question, nil, object.AnswerPrompt, modelProvider, modelProviderObj, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
chat, err := object.GetChat(util.GetId(chatOwner, chatName))
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
if chat == nil {
|
|
org := c.GetOrg()
|
|
currentTime := util.GetCurrentTime()
|
|
chat = &object.Chat{
|
|
Owner: chatOwner,
|
|
Name: chatName,
|
|
CreatedTime: currentTime,
|
|
UpdatedTime: currentTime,
|
|
Organization: org,
|
|
DisplayName: chatName,
|
|
Store: "",
|
|
ModelProvider: provider,
|
|
Category: category,
|
|
Type: "AI",
|
|
User: userName,
|
|
User1: "",
|
|
User2: "",
|
|
Users: []string{},
|
|
ClientIp: c.getClientIp(),
|
|
UserAgent: c.getUserAgent(),
|
|
MessageCount: 0,
|
|
IsHidden: strings.HasPrefix(chatName, "chat_provider_"),
|
|
}
|
|
|
|
chat.ClientIpDesc = util.GetDescFromIP(chat.ClientIp)
|
|
chat.UserAgentDesc = util.GetDescFromUserAgent(chat.UserAgent)
|
|
|
|
_, err = object.AddChat(chat)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
answer, modelResult, err := object.QueryAnswer(modelProviderObj, question, nil, nil, object.AnswerPrompt, c.GetAcceptLanguage())
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
questionMessage := &object.Message{
|
|
Owner: chatOwner,
|
|
Name: fmt.Sprintf("message_%s", util.GetRandomName()),
|
|
CreatedTime: util.GetCurrentTimeEx(chat.CreatedTime),
|
|
Organization: chat.Organization,
|
|
Store: chat.Store,
|
|
User: userName,
|
|
Chat: chat.Name,
|
|
ReplyTo: "",
|
|
Author: userName,
|
|
Text: question,
|
|
}
|
|
|
|
questionMessage.Currency = modelResult.Currency
|
|
|
|
_, err = object.AddMessage(questionMessage)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
answerMessage := &object.Message{
|
|
Owner: chatOwner,
|
|
Name: fmt.Sprintf("message_%s", util.GetRandomName()),
|
|
CreatedTime: util.GetCurrentTimeEx(chat.CreatedTime),
|
|
Organization: chat.Organization,
|
|
Store: chat.Store,
|
|
User: userName,
|
|
Chat: chat.Name,
|
|
ReplyTo: questionMessage.Name,
|
|
Author: "AI",
|
|
Text: answer,
|
|
ModelProvider: provider,
|
|
}
|
|
|
|
answerMessage.TokenCount = modelResult.TotalTokenCount
|
|
answerMessage.Price = model.AddPrices(modelResult.TotalPrice, 0)
|
|
answerMessage.Currency = modelResult.Currency
|
|
|
|
_, err = object.AddMessage(answerMessage)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
// The ONE debit for this ONE completion: the answer's own price, keyed on the
|
|
// answer message — the same charge every other message answer takes. The row is
|
|
// persisted first so a failed debit leaves something the retry cron can find
|
|
// (AddTransactionForMessage stamps ErrorText on it).
|
|
err = object.AddTransactionForMessage(answerMessage)
|
|
if err != nil {
|
|
c.ResponseError(err.Error())
|
|
return
|
|
}
|
|
|
|
chat.TokenCount += answerMessage.TokenCount
|
|
chat.Price += answerMessage.Price
|
|
if chat.Currency == "" {
|
|
chat.Currency = answerMessage.Currency
|
|
}
|
|
|
|
chat.UpdatedTime = util.GetCurrentTime()
|
|
chat.MessageCount += 2
|
|
|
|
_, err = object.UpdateChat(chat.GetId(), chat)
|
|
if err != nil {
|
|
c.ResponseOk(err.Error())
|
|
return
|
|
}
|
|
|
|
c.ResponseOk(answer)
|
|
}
|
|
|
|
// recordCasibaseChatUsage traces a casibase chat turn (store/session-scoped, not
|
|
// bearer-authed) into the ONE usage warehouse + o11y span plane. It TRACES ONLY.
|
|
// The turn's single commerce debit is AddTransactionForMessage, which charges this
|
|
// same completion's message.Price. recordUsage would reach the SAME
|
|
// object.UsageRecorder hook a second time, and that hook is not idempotent — the
|
|
// ledger mints its own entry id per debit and reads no key we send — so calling it
|
|
// here charged one completion twice.
|
|
//
|
|
// A USD turn reports its billed amount EXACTLY (BilledNanoExact = the dollars
|
|
// AddTransactionForMessage debits), so the warehouse's billed_nano reconciles with
|
|
// the invoice rather than re-deriving a rate-table guess keyed on a provider name.
|
|
// A non-USD legacy float (message.Currency can be CNY on some providers) is not a
|
|
// dollar amount, so it is left to the recompute and flagged Unpriced until the
|
|
// legacy-price rip folds currency.
|
|
func (c *ApiController) recordCasibaseChatUsage(chat *object.Chat, provider *object.Provider, r *model.ModelResult) {
|
|
if chat == nil || r == nil {
|
|
return
|
|
}
|
|
org := chat.Organization
|
|
if org == "" {
|
|
org = chat.Owner
|
|
}
|
|
providerName := ""
|
|
if provider != nil {
|
|
providerName = provider.Name
|
|
}
|
|
usd := r.Currency == "" || r.Currency == "USD"
|
|
rec := &usageRecord{
|
|
Owner: org,
|
|
Organization: org,
|
|
User: chat.User,
|
|
Model: providerName,
|
|
Provider: providerName,
|
|
PromptTokens: r.PromptTokenCount,
|
|
CompletionTokens: r.ResponseTokenCount,
|
|
TotalTokens: r.TotalTokenCount,
|
|
Cost: r.TotalPrice,
|
|
Currency: "USD",
|
|
Status: "success",
|
|
Unpriced: !usd,
|
|
ClientIP: c.Fiber().IP(),
|
|
RequestID: uuid.NewString(),
|
|
}
|
|
if usd {
|
|
billed := usdToNano(r.TotalPrice)
|
|
rec.BilledNanoExact = &billed
|
|
}
|
|
recordTrace(c.Context(), rec, time.Now().UTC())
|
|
}
|