Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V24.04 #78

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/chat_ai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package app

import (
"context"
"github.com/webitel/flow_manager/chat_ai"
"github.com/webitel/flow_manager/model"
"time"
)

const SysConnectionName = "chat_ai_connection"

var aiConnections = chat_ai.NewHub()

func (fm *FlowManager) ChatAnswerAi(ctx context.Context, domainId int64, r model.ChatAiAnswer) (*chat_ai.MessageResponse, error) {
if r.Connection == "" {
var appErr *model.AppError
r.Connection, appErr = fm.GetSystemSettingsString(ctx, domainId, SysConnectionName)
if appErr != nil {
return nil, appErr
}
}

if r.Connection == "" {
return nil, model.NewRequestError("app.chat_ai.answer", "connection is required")
}

cli, err := aiConnections.GetClient(r.Connection)
if err != nil {
return nil, err
}

request := &chat_ai.MessageRequest{
UserMetadata: r.Variables,
Categories: r.Categories,
Messages: make([]*chat_ai.Message, 0, len(r.Messages)),
ModelName: r.Model,
}

for _, v := range r.Messages {
request.Messages = append(request.Messages, &chat_ai.Message{
Message: v.Text,
Sender: aiChatSender(v.User),
})
}

rctx := ctx
if r.Timeout > 0 {
var cancel context.CancelFunc
rctx, cancel = context.WithTimeout(ctx, time.Duration(r.Timeout)*time.Second)
defer cancel()
}

result, err := cli.Api().Answer(rctx, request)
if err != nil {
return nil, err
}

return result, nil
}

func aiChatSender(userName string) string {
if userName != "" {
return "human"
}

return "ai"
}
48 changes: 48 additions & 0 deletions app/systemc_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app

import (
"context"
"encoding/json"
"fmt"
"github.com/webitel/engine/utils"
"github.com/webitel/flow_manager/model"
"golang.org/x/sync/singleflight"
)

var (
systemCache = utils.NewLruWithParams(100, "system_settings", 60, "")
systemGroup = singleflight.Group{}
)

func (fm *FlowManager) GetSystemSettingsString(ctx context.Context, domainId int64, name string) (string, *model.AppError) {
key := fmt.Sprintf("%d-%s", domainId, name)
c, ok := systemCache.Get(key)
if ok {
return c.(string), nil
}

v, err, share := systemGroup.Do(fmt.Sprintf("%d-%s", domainId, name), func() (interface{}, error) {
res, err := fm.Store.SystemcSettings().Get(ctx, domainId, name)
if err != nil {
return "", err
}
var val string
json.Unmarshal(res, &val)
return val, nil
})

if err != nil {
switch err.(type) {
case *model.AppError:
return "", err.(*model.AppError)
default:
return "", model.NewInternalError("app.sys_settings.get", err.Error())
}
}

if !share {
systemCache.Add(key, v.(string))
}

return v.(string), nil
}
5 changes: 3 additions & 2 deletions call/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ func (r *Router) handle(conn model.Connection) {
isTransfer := call.IsTransfer()

// TODO WTEL-4370
blXfer := strings.HasSuffix(call.GetVariable("variable_transfer_history"), fmt.Sprintf(":bl_xfer:%s/default/XML", call.Destination()))
ccXfer := strings.HasSuffix(call.GetVariable("variable_transfer_history"),
fmt.Sprintf(":bl_xfer:%s/default/XML", call.Destination())) && call.GetVariable("variable_cc_app_id") != ""

if transferSchemaId != nil && isTransfer {
routing, err = r.fm.SearchTransferredRouting(call.DomainId(), *transferSchemaId)
} else if isTransfer && queueId == nil && (blXfer || !call.IsOriginateRequest()) {
} else if isTransfer && queueId == nil && (ccXfer || !call.IsOriginateRequest()) {
wlog.Info(fmt.Sprintf("call %s [%d %s] is transfer from: [%s] to destination %s", call.Id(), call.DomainId(), call.Direction(),
call.From().String(), call.Destination()))
if routing, err = r.fm.SearchOutboundToDestinationRouting(call.DomainId(), call.Destination()); err == nil {
Expand Down
Loading