-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstate.go
More file actions
68 lines (57 loc) · 1.3 KB
/
state.go
File metadata and controls
68 lines (57 loc) · 1.3 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
package agentkit
import "sync"
// RoleType 消息角色类型
type RoleType string
const (
RoleUser RoleType = "user"
RoleAssistant RoleType = "assistant"
RoleTool RoleType = "tool"
)
// Message 消息记录
type Message struct {
Role RoleType
Agent string // 产生消息的 Agent 名称
Content string
ReasoningContent string // 推理模型的思考内容(如 DeepSeek-R1、o1),非推理模型为空
}
// State Agent 状态管理(线程安全)
type State struct {
mu sync.RWMutex
messages []Message
streaming bool
}
func newState() *State {
return &State{}
}
// AddMessage 添加消息
func (s *State) AddMessage(msg Message) {
s.mu.Lock()
defer s.mu.Unlock()
s.messages = append(s.messages, msg)
}
// Messages 获取所有消息的副本
func (s *State) Messages() []Message {
s.mu.RLock()
defer s.mu.RUnlock()
out := make([]Message, len(s.messages))
copy(out, s.messages)
return out
}
// IsStreaming 是否正在流式输出
func (s *State) IsStreaming() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.streaming
}
func (s *State) setStreaming(v bool) {
s.mu.Lock()
defer s.mu.Unlock()
s.streaming = v
}
// Clear 清空状态
func (s *State) Clear() {
s.mu.Lock()
defer s.mu.Unlock()
s.messages = nil
s.streaming = false
}