Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' of github.com:crestalnetwork/ethglobal-bangkok
Browse files Browse the repository at this point in the history
  • Loading branch information
dangbh1002 committed Nov 16, 2024
2 parents 69f4427 + 21e7219 commit 4e880d8
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ main ]
paths:
- backend
- 'backend/**'

jobs:
docker:
Expand Down
7 changes: 5 additions & 2 deletions backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Start(ctx context.Context) error {
if err != nil {
return err
}
h := handler.New(s)
h := handler.New(s, log)
// fiber app
app := fiber.New(fiber.Config{
ReadTimeout: 60 * time.Second,
Expand Down Expand Up @@ -68,7 +68,10 @@ func Start(ctx context.Context) error {
app.Use(recover.New(recover.Config{EnableStackTrace: true}))

// register routes
app.Post("/function", h.VAPIFunction)
app.Post("/function/assistant", h.VAPIFunction)
app.Post("/function/trade", h.VAPIFunctionTrade)
app.Post("/function/confirm", h.VAPIFunctionConfirm)
app.Post("/function/sign", h.VAPIFunctionSign)

go func() {
<-ctx.Done()
Expand Down
14 changes: 10 additions & 4 deletions backend/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package handler

import "github.com/crestalnetwork/ethglobal-bangkok/backend/service"
import (
"log/slog"

"github.com/crestalnetwork/ethglobal-bangkok/backend/service"
)

type Handler struct {
s *service.Service
s *service.Service
log *slog.Logger
}

func New(s *service.Service) *Handler {
func New(s *service.Service, log *slog.Logger) *Handler {
return &Handler{
s: s,
s: s,
log: log,
}
}
52 changes: 49 additions & 3 deletions backend/handler/vapi_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,58 @@ import (
)

func (h *Handler) VAPIFunction(c *fiber.Ctx) error {
var genericMessage map[string]interface{}
err := c.BodyParser(&genericMessage)
var msg map[string]interface{}
err := c.BodyParser(&msg)
if err != nil {
return types.NewError(fiber.StatusBadRequest, "BadRequest", err.Error())
}
resp, err := h.s.VAPIFunction(c.Context(), genericMessage)
err = h.s.VAPIFunction(c.Context(), msg)
if err != nil {
return err
}

return c.SendStatus(fiber.StatusNoContent)
}

func (h *Handler) VAPIFunctionTrade(c *fiber.Ctx) error {
var msg = new(types.VapiServerMessageToolCall)
err := c.BodyParser(msg)
if err != nil {
h.log.Error("VAPIFunctionTrade", "error", err)
h.log.Error("VAPIFunctionTrade body", "body", string(c.Body()))
return types.NewError(fiber.StatusBadRequest, "BadRequest", err.Error())
}
resp, err := h.s.VAPIFunctionTrade(c.Context(), msg)
if err != nil {
return err
}

return c.Status(fiber.StatusOK).JSON(resp)
}
func (h *Handler) VAPIFunctionConfirm(c *fiber.Ctx) error {
var msg = new(types.VapiServerMessageToolCall)
err := c.BodyParser(msg)
if err != nil {
h.log.Error("VAPIFunctionConfirm", "error", err)
h.log.Error("VAPIFunctionConfirm body", "body", string(c.Body()))
return types.NewError(fiber.StatusBadRequest, "BadRequest", err.Error())
}
resp, err := h.s.VAPIFunctionConfirm(c.Context(), msg)
if err != nil {
return err
}

return c.Status(fiber.StatusOK).JSON(resp)
}
func (h *Handler) VAPIFunctionSign(c *fiber.Ctx) error {
var msg = new(types.VapiServerMessageToolCall)
err := c.BodyParser(msg)
if err != nil {
h.log.Error("VAPIFunctionSign", "error", err)
h.log.Error("VAPIFunctionSign body", "body", string(c.Body()))
return types.NewError(fiber.StatusBadRequest, "BadRequest", err.Error())
}
resp, err := h.s.VAPIFunctionSign(c.Context(), msg)
if err != nil {
return err
}
Expand Down
91 changes: 86 additions & 5 deletions backend/service/vapi_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,94 @@ package service

import (
"context"
"encoding/json"
"fmt"
"math/rand"

"github.com/gofiber/fiber/v2"

"github.com/crestalnetwork/ethglobal-bangkok/backend/types"
)

func (s *Service) VAPIFunction(ctx context.Context, genericMessage map[string]interface{}) (*types.FunctionResult, error) {
req, _ := json.Marshal(genericMessage)
s.log.Info("VAPIFunction called", "message", req)
return &types.FunctionResult{Result: "Trade accept!"}, nil
func (s *Service) VAPIFunctionTrade(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.FunctionResult, error) {
var trade = new(types.Trade)
for _, tool := range msg.Message.ToolCallList {
if tool.Function != nil {
if tool.Function.Name == "trade" {
curr, ok := tool.Function.Arguments["currency"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "currency is required")
}
trade.Currency = curr.(string)
amount, ok := tool.Function.Arguments["amount"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "amount is required")
}
trade.Amount = amount.(float64)
action, ok := tool.Function.Arguments["action"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "action is required")
}
trade.Action = action.(string)
break
}
}
}
s.log.Warn("VAPIFunction called", "trade", trade)
return &types.FunctionResult{Result: fmt.Sprintf("The prize of %s is $%f now, are you sure you want to go ahead with this transaction?",
trade.Currency, rand.Float64()*100)}, nil
}

func (s *Service) VAPIFunctionConfirm(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.FunctionResult, error) {
var confirm bool
for _, tool := range msg.Message.ToolCallList {
if tool.Function != nil {
if tool.Function.Name == "confirm" {
curr, ok := tool.Function.Arguments["confirm"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "currency is required")
}
confirm, ok = curr.(bool)
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "confirm is required")
}
break
}
}
}
s.log.Warn("VAPIFunction called", "confirm", confirm)
if confirm {
return &types.FunctionResult{Result: "This is the transaction waiting for your signature. Can you authorize the AI assistant to sign for you?"}, nil
}
return &types.FunctionResult{Result: "We will stop this deal and if there are any new tasks, you can wake me up again."}, nil
}

func (s *Service) VAPIFunctionSign(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.FunctionResult, error) {
var confirm bool
for _, tool := range msg.Message.ToolCallList {
if tool.Function != nil {
if tool.Function.Name == "sign" {
curr, ok := tool.Function.Arguments["sign"]
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "currency is required")
}
confirm, ok = curr.(bool)
if !ok {
return nil, types.NewError(fiber.StatusBadRequest, "BadRequest", "confirm is required")
}
break
}
}
}
s.log.Warn("VAPIFunction called", "sign", confirm)
if confirm {
return &types.FunctionResult{Result: "Thank you, I will sign for you to complete the transaction."}, nil
}
return &types.FunctionResult{Result: "We will stop this deal and if there are any new tasks, you can wake me up again."}, nil
}

func (s *Service) VAPIFunction(ctx context.Context, genericMessage map[string]interface{}) error {
// req, _ := json.Marshal(genericMessage)
// s.log.Info("VAPIFunctionTrade called", "message", req)
// return &types.FunctionResult{Result: "Your transaction is in progress, please wait."}, nil
return nil
}
11 changes: 11 additions & 0 deletions backend/types/trade.go
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
package types

var (
TradeActionBuy = "buy"
TradeActionSell = "sell"
)

type Trade struct {
Currency string `json:"currency"`
Amount float64 `json:"amount"`
Action string `json:"action"`
}
15 changes: 14 additions & 1 deletion backend/types/vapi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"encoding/json"

vapi "github.com/VapiAI/server-sdk-go"
)

Expand Down Expand Up @@ -41,7 +43,8 @@ func (p AssistantRequestPayload) GetCallType() VapiWebhookEnum {

type StatusUpdatePayload struct {
BaseVapiPayload
Status vapi.CallStatus `json:"status"`
Status vapi.CallStatus `json:"status"`
vapi.ToolCallMessage
Messages []ConversationMessage `json:"messages,omitempty"`
}

Expand All @@ -52,3 +55,13 @@ func (p StatusUpdatePayload) GetCallType() VapiWebhookEnum {
type FunctionResult struct {
Result string `json:"result"`
}

type VapiServerMessageToolCall struct {
Message VapiServerMessageToolCallMessage `json:"message"`
}

type VapiServerMessageToolCallMessage struct {
ToolCallList []*vapi.ToolCall `json:"toolCallList,omitempty" url:"toolCallList,omitempty"`
extraProperties map[string]interface{}
_rawJSON json.RawMessage
}

0 comments on commit 4e880d8

Please sign in to comment.