-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
111 lines (91 loc) · 3.27 KB
/
context.go
File metadata and controls
111 lines (91 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package tools
import (
"context"
"github.com/gopher-lab/tools-go/core"
)
// contextKey is used for context value keys to avoid collisions.
type contextKey string
const (
// PairContextKey is the context key for the current trading pair.
PairContextKey contextKey = "currentPair"
)
// WithPair injects the current trading pair into the context.
// This allows tools to access the pair without relying on shared mutable state.
func WithPair(ctx context.Context, pair string) context.Context {
return context.WithValue(ctx, PairContextKey, pair)
}
// PairFromContext extracts the current trading pair from the context.
// Returns empty string if not set.
func PairFromContext(ctx context.Context) string {
if pair, ok := ctx.Value(PairContextKey).(string); ok {
return pair
}
return ""
}
// Context holds dependencies needed by tools.
// All dependencies are interface-based for flexibility.
type Context struct {
// Wallet provides trading operations.
Wallet core.Wallet
// MarketClient provides market data access.
MarketClient core.MarketDataProvider
// State provides agent state operations.
State core.State
// Config provides agent configuration.
Config core.Config
// Persistence provides optional database operations.
// Can be nil if persistence is not needed.
Persistence core.StatePersistence
// TimeframeSeries holds multi-timeframe market data (interval -> series).
// This is implementation-specific and can be any type.
TimeframeSeries map[string]interface{}
// OnStateUpdate is called after trade execution to trigger immediate WebSocket broadcast.
OnStateUpdate func()
// OnPositionOpened is called when a new position is opened via a tool.
// Registers the symbol with a position tracker for real-time price updates.
OnPositionOpened func(symbol string)
// GetCurrentPrice fetches the real-time price from a price feed.
// Returns (price, true) if available, (0, false) otherwise.
GetCurrentPrice func(symbol string) (float64, bool)
// OnPairRemoved is called when a pair is removed from active trading.
// Allows cleanup of pair-specific caches.
OnPairRemoved func(pair string)
// OnPositionClosed is called when a position is closed via a tool.
// Triggers immediate trade recording to DB without waiting for eval cycle.
OnPositionClosed func()
}
// NewContext creates a new tool context with dependencies.
func NewContext(
wallet core.Wallet,
marketClient core.MarketDataProvider,
state core.State,
config core.Config,
) *Context {
return &Context{
Wallet: wallet,
MarketClient: marketClient,
State: state,
Config: config,
TimeframeSeries: make(map[string]interface{}),
}
}
// WithPersistence sets the persistence layer and returns the context.
func (c *Context) WithPersistence(p core.StatePersistence) *Context {
c.Persistence = p
return c
}
// WithCallbacks sets all callbacks and returns the context.
func (c *Context) WithCallbacks(
onStateUpdate func(),
onPositionOpened func(symbol string),
onPositionClosed func(),
onPairRemoved func(pair string),
getCurrentPrice func(symbol string) (float64, bool),
) *Context {
c.OnStateUpdate = onStateUpdate
c.OnPositionOpened = onPositionOpened
c.OnPositionClosed = onPositionClosed
c.OnPairRemoved = onPairRemoved
c.GetCurrentPrice = getCurrentPrice
return c
}