Files
ai/controllers/message_answer.go

700 lines
23 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"
"context"
"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() {
caller, ok := c.RequireSignedInUser()
if !ok {
return
}
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
}
// Whose message this is, before anything is generated for it. This path runs a
// model and takes the debit for it, so naming somebody else's unanswered
// message would spend their balance. Organization is what says whose it is —
// Owner is the namespace every message shares.
if !reaches(caller, message.Organization) {
c.ResponseErrorStream(nil, c.T("general:The message does not exist"))
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.
//
// WHICH exit releases it is the whole point. The generation does not happen in
// this function: it happens in the stream writer, which fasthttp runs after
// this function has returned. A release on this function's exit therefore drops
// the claim with the answer still being written — the row's text is empty until
// the very end, which is exactly the condition the release tests and the next
// claim tests — so a second request would generate the same turn and take a
// second debit for it. Everything that returns before a generation starts
// releases here; once the writer takes over, it owns the claim.
generating := false
defer func() {
if !generating {
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
}
_, signedIn := c.CheckSignedIn()
if !signedIn {
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.
//
// fasthttp drains that writer from ITS OWN goroutine once the handler has
// returned and fiber has released the request context, so nothing inside may
// reach through c — a header read there is a nil dereference, and a panic in
// that goroutine is not the router's to recover. Everything the stream needs is
// taken here, while the request is still ours, and answers travel out through w.
snap := c.takeSnapshot(nil)
generating = true
_ = c.SendStreamWriter(func(w *bufio.Writer) {
// The claim travels with the generation, and is dropped when it ends.
defer object.ReleaseMessageAnswer(message)
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 {
streamError(w, message, err.Error(), snap.lang)
return
}
}
history, err := object.GetRecentRawMessages(chat.Name, message.CreatedTime, store.MemoryLimit)
if err != nil {
streamError(w, message, err.Error(), snap.lang)
return
}
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 {
streamError(w, message, err.Error(), snap.lang)
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, snap.lang)
} else {
if isReasonModel(modelProvider.SubType) {
modelResult, err = QueryCarrierText(question, writer, history, prompt, knowledge, modelProviderObj, chat.NeedTitle, store.SuggestionCount, snap.lang)
} else {
modelResult, err = modelProviderObj.QueryText(question, writer, history, prompt, knowledge, nil, snap.lang)
}
}
if err != nil {
if strings.Contains(err.Error(), "write tcp") {
streamError(w, message, err.Error(), snap.lang)
return
}
streamError(w, message, err.Error(), snap.lang)
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 {
streamError(w, message, err.Error(), snap.lang)
return
}
_, err = writer.Writer.Write([]byte(fmt.Sprintf("event: message\ndata: %s\n\n", jsonData)))
if err != nil {
streamError(w, message, err.Error(), snap.lang)
return
}
_ = writer.Writer.Flush()
}
event := fmt.Sprintf("event: end\ndata: %s\n\n", "end")
_, err = w.Write([]byte(event))
if err != nil {
streamError(w, message, err.Error(), snap.lang)
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.
recordCasibaseChatUsage(snap.ctx, snap.ip, chat, modelProvider, modelResult)
textAnswer := answer
textSuggestions := []object.Suggestion{}
textTitle := ""
textAnswer, textSuggestions, textTitle, err = parseAnswerWithCarriers(answer, store.SuggestionCount, chat.NeedTitle)
if err != nil {
streamError(w, message, err.Error(), snap.lang)
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 {
streamError(w, message, err.Error(), snap.lang)
return
}
// Add transaction for message with price
err = object.AddTransactionForMessage(message)
if err != nil {
streamError(w, message, err.Error(), snap.lang)
return
}
_, err = object.UpdateMessage(message.GetId(), message, false)
if err != nil {
streamError(w, message, err.Error(), snap.lang)
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 {
streamError(w, message, err.Error(), snap.lang)
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
}
// A chat of this name in another organization is not this caller's to answer
// into: the turns below are filed under the chat's organization, so the answer
// and what it costs would land there. It cannot be treated as absent and made
// afresh either — the name is half the key, so a new one would collide.
if chat != nil && chat.Organization != c.GetOrg() {
c.ResponseError(fmt.Sprintf("chat:The chat: %s is not found", chatName))
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 {
// A failure is reported as one, the way the streamed path beside this
// reports the same write. Handed to ResponseOk it came back at status ok in
// the field that carries the answer, so the store's error text was read as
// what the assistant said.
c.ResponseError(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.
// recordCasibaseChatUsage lands a chat turn in the usage warehouse. It takes the
// client address and a context rather than reading them off a controller: it is
// called from inside a stream writer, which fasthttp drains from its own
// goroutine after the handler has returned and the request context has been
// released, so anything reached through the controller there is already gone.
func recordCasibaseChatUsage(ctx context.Context, clientIP string, 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
}
// A result that names no price is UNPRICED, not free. Currency alone cannot say
// so — empty reads as USD here — which is why the client states it outright.
usd := !r.Unpriced && (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: clientIP,
RequestID: uuid.NewString(),
}
if usd {
billed := usdToNano(r.TotalPrice)
rec.BilledNanoExact = &billed
}
recordTrace(ctx, rec, time.Now().UTC())
}