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

Commit

Permalink
feat: backend, trade state
Browse files Browse the repository at this point in the history
  • Loading branch information
hyacinthus committed Nov 16, 2024
1 parent 9a3cf00 commit 14fb4eb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
12 changes: 10 additions & 2 deletions backend/handler/state.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package handler

import "github.com/gofiber/fiber/v2"
import (
"github.com/gofiber/fiber/v2"

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

func (h *Handler) GetChatState(c *fiber.Ctx) error {
resp, err := h.s.GetChatState(c.Context())
id := c.Params("id")
if id == "" {
return types.NewError(fiber.StatusBadRequest, "BadRequest", "missing id")
}
resp, err := h.s.GetChatState(c.Context(), id)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion backend/service/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (s *Service) GetChatState(ctx context.Context, id string) (*types.State, er
raw, ok := s.state.Load(id)
if !ok {
resp = &types.State{
ChatID: id,
CallID: id,
}
s.state.Store(id, resp)
return resp, nil
Expand Down
32 changes: 28 additions & 4 deletions backend/service/vapi_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,24 @@ func (s *Service) VAPIFunctionTrade(ctx context.Context, msg *types.VapiServerMe
}
}
}

callID := msg.Message.Call.Id
// check the call id
state, err := s.GetChatState(ctx, callID)
if err != nil {
return nil, err
}
if state.Step > 0 {
return vapiToolResponse(id, fmt.Sprintf("You are trading %s, please confirm or cancel the transaction.", state.Trade.Currency)), nil
}

s.log.Warn("VAPIFunction called", "trade", trade)
res := types.ToolResult{ToolCallID: id, Result: fmt.Sprintf("Tell the user:The prize of %s is $%f now, are you sure you want to go ahead with this transaction?",
trade.Currency, rand.Float64()*100)}
resp.Results = append(resp.Results, res)
return resp, nil
trade.Price = rand.Float64() * 100 // mock the price
// update the state
state.Trade = *trade
state.Step = 1
s.state.Store(callID, state)
return vapiToolResponse(id, fmt.Sprintf("Tell the user: The prize of %s is $%f now, are you sure you want to go ahead with this transaction?", trade.Currency, trade.Price)), nil
}

func (s *Service) VAPIFunctionConfirm(ctx context.Context, msg *types.VapiServerMessageToolCall) (*types.ToolResults, error) {
Expand Down Expand Up @@ -117,3 +130,14 @@ func (s *Service) VAPIFunction(ctx context.Context, genericMessage map[string]in
// return &types.FunctionResult{Result: "Your transaction is in progress, please wait."}, nil
return nil
}

func vapiToolResponse(toolID string, result string) *types.ToolResults {
return &types.ToolResults{
Results: []types.ToolResult{
{
ToolCallID: toolID,
Result: result,
},
},
}
}
2 changes: 1 addition & 1 deletion backend/types/state.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

type State struct {
ChatID string `json:"chat_id"`
CallID string `json:"call_id"`
Step int `json:"step"` // 0 start, 1 confirming, 2 signing, 3 trading, 4 done
Trade Trade `json:"trade"` // start from 1
IsConfirmed bool `json:"is_confirmed"` // start from 2
Expand Down
1 change: 1 addition & 0 deletions backend/types/vapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type VapiServerMessageToolCall struct {
}

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

0 comments on commit 14fb4eb

Please sign in to comment.