diff --git a/internal/model/chat.go b/internal/model/chat.go new file mode 100644 index 0000000..04ebf2d --- /dev/null +++ b/internal/model/chat.go @@ -0,0 +1,50 @@ +package model + +// ChatRole 消息角色 +type ChatRole string + +const ( + ChatRoleSystem ChatRole = "system" + ChatRoleUser ChatRole = "user" + ChatRoleAssistant ChatRole = "assistant" + ChatRoleTool ChatRole = "tool" +) + +// ChatMessage 聊天消息 +type ChatMessage struct { + Role ChatRole + Content string + ToolCallID string + Name string + ToolCalls []ChatToolCall +} + +// ChatToolCall 工具调用请求 +type ChatToolCall struct { + ID string + Name string + Arguments string +} + +// ChatReply 聊天回复 +type ChatReply struct { + Content string + ToolCalls []ChatToolCall +} + +// ChatStreamEvent 流式事件 +type ChatStreamEvent struct { + Delta string + ToolCalls []ChatToolCall + Done bool +} + +// ChatContent 聊天输入内容 +type ChatContent struct { + Texts []TextPart +} + +// TextPart 文本片段 +type TextPart struct { + Message string +} diff --git a/internal/model/store.go b/internal/model/store.go new file mode 100644 index 0000000..ffa1a0b --- /dev/null +++ b/internal/model/store.go @@ -0,0 +1,61 @@ +package model + +import "sync" + +// InMemoryAgentRegistry 基于内存的 Agent 注册表 +type InMemoryAgentRegistry struct { + mu sync.RWMutex + agents map[string]RegisteredAgent +} + +func NewInMemoryAgentRegistry() *InMemoryAgentRegistry { + return &InMemoryAgentRegistry{agents: make(map[string]RegisteredAgent)} +} + +func (r *InMemoryAgentRegistry) Register(agent RegisteredAgent) error { + r.mu.Lock() + defer r.mu.Unlock() + r.agents[agent.AgentID] = agent + return nil +} + +func (r *InMemoryAgentRegistry) Get(agentID string) (RegisteredAgent, bool) { + r.mu.RLock() + defer r.mu.RUnlock() + agent, ok := r.agents[agentID] + return agent, ok +} + +func (r *InMemoryAgentRegistry) List() []RegisteredAgent { + r.mu.RLock() + defer r.mu.RUnlock() + agents := make([]RegisteredAgent, 0, len(r.agents)) + for _, agent := range r.agents { + agents = append(agents, agent) + } + return agents +} + +// InMemorySessionStore 基于内存的会话存储 +type InMemorySessionStore struct { + mu sync.RWMutex + sessions map[string]string +} + +func NewInMemorySessionStore() *InMemorySessionStore { + return &InMemorySessionStore{sessions: make(map[string]string)} +} + +func (s *InMemorySessionStore) Get(userID, agentID string) (string, bool) { + s.mu.RLock() + defer s.mu.RUnlock() + sessionID, ok := s.sessions[userID+":"+agentID] + return sessionID, ok +} + +func (s *InMemorySessionStore) Set(userID, agentID, sessionID string) error { + s.mu.Lock() + defer s.mu.Unlock() + s.sessions[userID+":"+agentID] = sessionID + return nil +} diff --git a/internal/model/types.go b/internal/model/types.go index 1898a6b..bfb3290 100644 --- a/internal/model/types.go +++ b/internal/model/types.go @@ -1,62 +1,6 @@ package model -import ( - "context" - "sync" -) - -// ============================================================ -// Chat 数据类型 -// ============================================================ - -// ChatRole 消息角色 -type ChatRole string - -const ( - ChatRoleSystem ChatRole = "system" - ChatRoleUser ChatRole = "user" - ChatRoleAssistant ChatRole = "assistant" - ChatRoleTool ChatRole = "tool" -) - -// ChatMessage 聊天消息 -type ChatMessage struct { - Role ChatRole - Content string - ToolCallID string - Name string - ToolCalls []ChatToolCall -} - -// ChatToolCall 工具调用请求 -type ChatToolCall struct { - ID string - Name string - Arguments string -} - -// ChatReply 聊天回复 -type ChatReply struct { - Content string - ToolCalls []ChatToolCall -} - -// ChatStreamEvent 流式事件 -type ChatStreamEvent struct { - Delta string - ToolCalls []ChatToolCall - Done bool -} - -// ChatContent 聊天输入内容 -type ChatContent struct { - Texts []TextPart -} - -// TextPart 文本片段 -type TextPart struct { - Message string -} +import "context" // ============================================================ // 核心接口 @@ -90,7 +34,7 @@ type Runner interface { } // ============================================================ -// 注册与存储 +// 注册与存储接口 // ============================================================ // RegisteredAgent 已注册的 Agent 信息 @@ -114,65 +58,3 @@ type SessionStore interface { Get(userID, agentID string) (string, bool) Set(userID, agentID, sessionID string) error } - -// ============================================================ -// 内存实现 -// ============================================================ - -// InMemoryAgentRegistry 基于内存的 Agent 注册表 -type InMemoryAgentRegistry struct { - mu sync.RWMutex - agents map[string]RegisteredAgent -} - -func NewInMemoryAgentRegistry() *InMemoryAgentRegistry { - return &InMemoryAgentRegistry{agents: make(map[string]RegisteredAgent)} -} - -func (r *InMemoryAgentRegistry) Register(agent RegisteredAgent) error { - r.mu.Lock() - defer r.mu.Unlock() - r.agents[agent.AgentID] = agent - return nil -} - -func (r *InMemoryAgentRegistry) Get(agentID string) (RegisteredAgent, bool) { - r.mu.RLock() - defer r.mu.RUnlock() - agent, ok := r.agents[agentID] - return agent, ok -} - -func (r *InMemoryAgentRegistry) List() []RegisteredAgent { - r.mu.RLock() - defer r.mu.RUnlock() - agents := make([]RegisteredAgent, 0, len(r.agents)) - for _, agent := range r.agents { - agents = append(agents, agent) - } - return agents -} - -// InMemorySessionStore 基于内存的会话存储 -type InMemorySessionStore struct { - mu sync.RWMutex - sessions map[string]string -} - -func NewInMemorySessionStore() *InMemorySessionStore { - return &InMemorySessionStore{sessions: make(map[string]string)} -} - -func (s *InMemorySessionStore) Get(userID, agentID string) (string, bool) { - s.mu.RLock() - defer s.mu.RUnlock() - sessionID, ok := s.sessions[userID+":"+agentID] - return sessionID, ok -} - -func (s *InMemorySessionStore) Set(userID, agentID, sessionID string) error { - s.mu.Lock() - defer s.mu.Unlock() - s.sessions[userID+":"+agentID] = sessionID - return nil -}