Skip to content

Commit

Permalink
Merge pull request #337 from Lumerin-protocol/dev
Browse files Browse the repository at this point in the history
v1.1.5-TEST
  • Loading branch information
abs2023 authored Dec 4, 2024
2 parents 999e1dd + 5dfb8c1 commit 18bf711
Show file tree
Hide file tree
Showing 36 changed files with 1,019 additions and 149 deletions.
3 changes: 2 additions & 1 deletion .github/actions/gen_tag_name/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ runs:
VMIN=0
VPAT=0
set +o pipefail
VLAST=$(git describe --tags --abbrev=0 --match='v[1-9]*' 2>/dev/null | cut -c2-)
# VLAST=$(git describe --tags --abbrev=0 --match='v[1-9]*' 2>/dev/null | cut -c2-)
VLAST=$(git fetch origin main --tags && git describe --tags --abbrev=0 --match='v[1-9]*' origin/main | cut -c2-)
[ $VLAST ] && declare $(echo $VLAST | awk -F '.' '{print "VMAJ="$1" VMIN="$2" VPAT="$3}')
if [ "$GITHUB_REF_NAME" = "main" ]
then
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ jobs:
--form "ref=${{ env.gitlab_branch }}" \
--form "variables[SOURCE_REPO]=${{ github.repository }}" \
--form "variables[SOURCE_BRANCH]=${{ github.ref_name }}" \
--form "variables[GITHUB_VFULL]=${{ env.VFULL }}" \
--form "variables[GITHUB_TAG]=${{ env.TAG_NAME }}")

# Parse JSON response using jq
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/proxy-router.main.env
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ PROXY_ADDRESS=0.0.0.0:3333
WEB_ADDRESS=0.0.0.0:8082
WEB_PUBLIC_URL=http://localhost:8082
MODELS_CONFIG_PATH=
RATING_CONFIG_PATH=
ETH_NODE_USE_SUBSCRIPTIONS=false
ETH_NODE_ADDRESS=
ETH_NODE_LEGACY_TX=false
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/proxy-router.test.env
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ PROXY_ADDRESS=0.0.0.0:3333
WEB_ADDRESS=0.0.0.0:8082
WEB_PUBLIC_URL=http://localhost:8082
MODELS_CONFIG_PATH=
RATING_CONFIG_PATH=
ETH_NODE_USE_SUBSCRIPTIONS=false
ETH_NODE_ADDRESS=
ETH_NODE_LEGACY_TX=false
Expand Down
1 change: 1 addition & 0 deletions proxy-router/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PROXY_ADDRESS=0.0.0.0:3333
WEB_ADDRESS=0.0.0.0:8082
WEB_PUBLIC_URL=http://localhost:8082
MODELS_CONFIG_PATH=
RATING_CONFIG_PATH=
ETH_NODE_USE_SUBSCRIPTIONS=false
ETH_NODE_ADDRESS=
ETH_NODE_LEGACY_TX=false
Expand Down
1 change: 1 addition & 0 deletions proxy-router/.env.example.win
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PROXY_ADDRESS=0.0.0.0:3333
WEB_ADDRESS=0.0.0.0:8082
WEB_PUBLIC_URL=http://localhost:8082
MODELS_CONFIG_PATH=
RATING_CONFIG_PATH=
ETH_NODE_USE_SUBSCRIPTIONS=false
ETH_NODE_ADDRESS=
ETH_NODE_LEGACY_TX=false
Expand Down
3 changes: 2 additions & 1 deletion proxy-router/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ bin

data*

models-config.json
models-config.json
rating-config.json
30 changes: 14 additions & 16 deletions proxy-router/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func start() error {
docs.SwaggerInfo.Host = hostWithoutProtocol
} else if cfg.Web.Address != "" {
docs.SwaggerInfo.Host = cfg.Web.Address
} else {
docs.SwaggerInfo.Host = "localhost:8082"
}
docs.SwaggerInfo.Version = config.BuildVersion

docs.SwaggerInfo.Version = config.BuildVersion

log, err := lib.NewLogger(cfg.Log.LevelApp, cfg.Log.Color, cfg.Log.IsProd, cfg.Log.JSON, mainLogFilePath)
if err != nil {
return err
Expand Down Expand Up @@ -227,11 +227,6 @@ func start() error {
}
appLog.Infof("connected to ethereum node: %s, chainID: %d", cfg.Blockchain.EthNodeAddress, chainID)

publicUrl, err := url.Parse(cfg.Web.PublicUrl)
if err != nil {
return err
}

storage := storages.NewStorage(badgerLog, cfg.Proxy.StoragePath)
sessionStorage := storages.NewSessionStorage(storage)

Expand All @@ -246,19 +241,13 @@ func start() error {

var logWatcher contracts.LogWatcher
if cfg.Blockchain.UseSubscriptions {
logWatcher = contracts.NewLogWatcherSubscription(ethClient, cfg.Blockchain.MaxReconnects, log)
logWatcher = contracts.NewLogWatcherSubscription(ethClient, cfg.Blockchain.MaxReconnects, rpcLog)
appLog.Infof("using websocket log subscription for blockchain events")
} else {
logWatcher = contracts.NewLogWatcherPolling(ethClient, cfg.Blockchain.PollingInterval, cfg.Blockchain.MaxReconnects, log)
logWatcher = contracts.NewLogWatcherPolling(ethClient, cfg.Blockchain.PollingInterval, cfg.Blockchain.MaxReconnects, rpcLog)
appLog.Infof("using polling for blockchain events")
}

modelConfigLoader := config.NewModelConfigLoader(cfg.Proxy.ModelsConfigPath, log)
err = modelConfigLoader.Init()
if err != nil {
log.Warnf("failed to load model config: %s, run with empty", err)
}

scorer, err := config.LoadRating(cfg.Proxy.RatingConfigPath, log)
if err != nil {
return err
Expand All @@ -271,10 +260,17 @@ func start() error {
sessionRouter := registries.NewSessionRouter(*cfg.Marketplace.DiamondContractAddress, ethClient, multicallBackend, log)
marketplace := registries.NewMarketplace(*cfg.Marketplace.DiamondContractAddress, ethClient, multicallBackend, log)
sessionRepo := sessionrepo.NewSessionRepositoryCached(sessionStorage, sessionRouter, marketplace)
proxyRouterApi := proxyapi.NewProxySender(chainID, publicUrl, wallet, contractLogStorage, sessionStorage, sessionRepo, log)
proxyRouterApi := proxyapi.NewProxySender(chainID, wallet, contractLogStorage, sessionStorage, sessionRepo, log)
explorer := blockchainapi.NewExplorerClient(cfg.Blockchain.ExplorerApiUrl, *cfg.Marketplace.MorTokenAddress, cfg.Blockchain.ExplorerRetryDelay, cfg.Blockchain.ExplorerMaxRetries)
blockchainApi := blockchainapi.NewBlockchainService(ethClient, multicallBackend, *cfg.Marketplace.DiamondContractAddress, *cfg.Marketplace.MorTokenAddress, explorer, wallet, proxyRouterApi, sessionRepo, scorer, proxyLog, cfg.Blockchain.EthLegacyTx)
proxyRouterApi.SetSessionService(blockchainApi)

modelConfigLoader := config.NewModelConfigLoader(cfg.Proxy.ModelsConfigPath, valid, blockchainApi, &aiengine.ConnectionChecker{}, log)
err = modelConfigLoader.Init()
if err != nil {
log.Warnf("failed to load model config, running with empty: %s", err)
}

aiEngine := aiengine.NewAiEngine(proxyRouterApi, chatStorage, modelConfigLoader, log)

eventListener := blockchainapi.NewEventsListener(sessionRepo, sessionRouter, wallet, logWatcher, log)
Expand All @@ -299,6 +295,8 @@ func start() error {
cancel()
}()

log.Infof("API docs available at %s/swagger/index.html", cfg.Web.PublicUrl)

proxy := proxyctl.NewProxyCtl(eventListener, wallet, chainID, log, connLog, cfg.Proxy.Address, schedulerLogFactory, sessionStorage, modelConfigLoader, valid, aiEngine, blockchainApi, sessionRepo, sessionExpiryHandler)
err = proxy.Run(ctx)

Expand Down
54 changes: 54 additions & 0 deletions proxy-router/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,37 @@ const docTemplate = `{
}
}
},
"/proxy/provider/ping": {
"post": {
"description": "sends a ping to the provider on the RPC level",
"produces": [
"application/json"
],
"tags": [
"chat"
],
"summary": "Ping Provider",
"parameters": [
{
"description": "Ping Request",
"name": "pingReq",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/proxyapi.PingReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/proxyapi.PingRes"
}
}
}
}
},
"/proxy/sessions/initiate": {
"post": {
"description": "sends a handshake to the provider",
Expand Down Expand Up @@ -1844,6 +1875,29 @@ const docTemplate = `{
}
}
},
"proxyapi.PingReq": {
"type": "object",
"required": [
"providerAddr",
"providerUrl"
],
"properties": {
"providerAddr": {
"type": "string"
},
"providerUrl": {
"type": "string"
}
}
},
"proxyapi.PingRes": {
"type": "object",
"properties": {
"ping": {
"type": "integer"
}
}
},
"proxyapi.ResultResponse": {
"type": "object",
"properties": {
Expand Down
54 changes: 54 additions & 0 deletions proxy-router/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,37 @@
}
}
},
"/proxy/provider/ping": {
"post": {
"description": "sends a ping to the provider on the RPC level",
"produces": [
"application/json"
],
"tags": [
"chat"
],
"summary": "Ping Provider",
"parameters": [
{
"description": "Ping Request",
"name": "pingReq",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/proxyapi.PingReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/proxyapi.PingRes"
}
}
}
}
},
"/proxy/sessions/initiate": {
"post": {
"description": "sends a handshake to the provider",
Expand Down Expand Up @@ -1836,6 +1867,29 @@
}
}
},
"proxyapi.PingReq": {
"type": "object",
"required": [
"providerAddr",
"providerUrl"
],
"properties": {
"providerAddr": {
"type": "string"
},
"providerUrl": {
"type": "string"
}
}
},
"proxyapi.PingRes": {
"type": "object",
"properties": {
"ping": {
"type": "integer"
}
}
},
"proxyapi.ResultResponse": {
"type": "object",
"properties": {
Expand Down
35 changes: 35 additions & 0 deletions proxy-router/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,21 @@ definitions:
- spend
- user
type: object
proxyapi.PingReq:
properties:
providerAddr:
type: string
providerUrl:
type: string
required:
- providerAddr
- providerUrl
type: object
proxyapi.PingRes:
properties:
ping:
type: integer
type: object
proxyapi.ResultResponse:
properties:
result:
Expand Down Expand Up @@ -1375,6 +1390,26 @@ paths:
summary: Healthcheck example
tags:
- system
/proxy/provider/ping:
post:
description: sends a ping to the provider on the RPC level
parameters:
- description: Ping Request
in: body
name: pingReq
required: true
schema:
$ref: '#/definitions/proxyapi.PingReq'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/proxyapi.PingRes'
summary: Ping Provider
tags:
- chat
/proxy/sessions/{id}/providerClaim:
post:
description: Claim provider balance from session
Expand Down
6 changes: 1 addition & 5 deletions proxy-router/internal/aiengine/ai_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,13 @@ func (a *AiEngine) GetAdapter(ctx context.Context, chatID, modelID, sessionID co
return engine, nil
}

func (a *AiEngine) GetModelsConfig() ([]string, []config.ModelConfig) {
return a.modelsConfigLoader.GetAll()
}

func (a *AiEngine) GetLocalModels() ([]LocalModel, error) {
models := []LocalModel{}

IDs, modelsFromConfig := a.modelsConfigLoader.GetAll()
for i, model := range modelsFromConfig {
models = append(models, LocalModel{
Id: IDs[i],
Id: IDs[i].Hex(),
Name: model.ModelName,
Model: model.ModelName,
ApiType: model.ApiType,
Expand Down
53 changes: 53 additions & 0 deletions proxy-router/internal/aiengine/conn_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package aiengine

import (
"context"
"errors"
"net"
"net/url"
"time"

"github.com/MorpheusAIs/Morpheus-Lumerin-Node/proxy-router/internal/lib"
)

const (
TimeoutConnectDefault = 3 * time.Second
)

var (
ErrCannotParseURL = errors.New("cannot parse URL")
ErrCannotConnect = errors.New("cannot connect")
ErrConnectTimeout = errors.New("connection timeout")
)

type ConnectionChecker struct{}

func (*ConnectionChecker) TryConnect(ctx context.Context, URL string) error {
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeoutCause(ctx, TimeoutConnectDefault, ErrConnectTimeout)
defer cancel()
}
u, err := url.Parse(URL)
if err != nil {
return lib.WrapError(ErrCannotParseURL, err)
}

var host string
if u.Port() == "" {
host = net.JoinHostPort(u.Hostname(), "443")
} else {
host = u.Host
}

dialer := net.Dialer{}

conn, err := dialer.DialContext(ctx, "tcp", host)
if err != nil {
return lib.WrapError(ErrCannotConnect, err)
}

defer conn.Close()

return nil
}
Loading

0 comments on commit 18bf711

Please sign in to comment.