Repository navigation
Expand file tree
/
Copy pathtool_context.go
More file actions
115 lines (101 loc) · 2.74 KB
/
Copy pathtool_context.go
File metadata and controls
115 lines (101 loc) · 2.74 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
112
113
114
115
package agent
import "sync"
const SharedContextKey = "shared"
type ContextInput map[string]map[string]any
type ToolContextStore struct {
mu sync.RWMutex
values map[string]map[string]any
listeners []func(string, map[string]any)
}
func NewToolContextStore(initial ContextInput) *ToolContextStore {
s := &ToolContextStore{values: map[string]map[string]any{}}
for k, v := range initial {
s.values[k] = copyAnyMap(v)
}
return s
}
func (s *ToolContextStore) Snapshot() map[string]map[string]any {
s.mu.RLock()
defer s.mu.RUnlock()
out := map[string]map[string]any{}
for k, v := range s.values {
out[k] = copyAnyMap(v)
}
return out
}
func (s *ToolContextStore) Get(name string) map[string]any {
s.mu.RLock()
defer s.mu.RUnlock()
return copyAnyMap(s.values[name])
}
func (s *ToolContextStore) Set(name string, value map[string]any) {
s.mu.Lock()
s.values[name] = copyAnyMap(value)
listeners := append([]func(string, map[string]any){}, s.listeners...)
s.mu.Unlock()
for _, l := range listeners {
l(name, copyAnyMap(value))
}
}
func (s *ToolContextStore) Merge(name string, value map[string]any) {
cur := s.Get(name)
for k, v := range value {
cur[k] = v
}
s.Set(name, cur)
}
func (s *ToolContextStore) Subscribe(listener func(string, map[string]any)) {
s.mu.Lock()
defer s.mu.Unlock()
s.listeners = append(s.listeners, listener)
}
func BuildToolExecuteContext(call ParsedToolCall, turn TurnContext, store *ToolContextStore, emit func(any)) ToolExecuteContext {
var local, shared map[string]any
if store != nil {
local = store.Get(call.Name)
shared = store.Get(SharedContextKey)
}
return ToolExecuteContext{ToolCall: call, Turn: turn, Context: local, Shared: shared, Store: store, Emit: emit}
}
func (tc ToolExecuteContext) LocalContext() map[string]any {
if tc.Store == nil {
return copyAnyMap(tc.Context)
}
return tc.Store.Get(tc.ToolCall.Name)
}
func (tc ToolExecuteContext) SharedContext() map[string]any {
if tc.Store == nil {
return copyAnyMap(tc.Shared)
}
return tc.Store.Get(SharedContextKey)
}
func (tc ToolExecuteContext) SetContext(value map[string]any) {
if tc.Store != nil {
tc.Store.Set(tc.ToolCall.Name, value)
}
}
func (tc ToolExecuteContext) MergeContext(value map[string]any) {
if tc.Store != nil {
tc.Store.Merge(tc.ToolCall.Name, value)
}
}
func (tc ToolExecuteContext) SetSharedContext(value map[string]any) {
if tc.Store != nil {
tc.Store.Set(SharedContextKey, value)
}
}
func (tc ToolExecuteContext) MergeSharedContext(value map[string]any) {
if tc.Store != nil {
tc.Store.Merge(SharedContextKey, value)
}
}
func copyAnyMap(in map[string]any) map[string]any {
if in == nil {
return map[string]any{}
}
out := make(map[string]any, len(in))
for k, v := range in {
out[k] = v
}
return out
}