feat(types,model): 实现阶段 1 基础设施 — 错误码、AppError、配置模型、核心接口

This commit is contained in:
hhs
2026-06-10 12:49:29 +08:00
parent 1cfebd37be
commit 53488f792d
4 changed files with 270 additions and 0 deletions

View File

@@ -1 +1,67 @@
package model
// WorkflowType 工作流类型
type WorkflowType string
const (
WorkflowTypeLoop WorkflowType = "loop"
WorkflowTypeParallel WorkflowType = "parallel"
WorkflowTypeSequential WorkflowType = "sequential"
)
// AiAgentConfigTable 一个 Agent 配置表的顶层结构,对应 YAML 中 tables 下的每一项
type AiAgentConfigTable struct {
AppName string `yaml:"app-name" json:"appName"`
Agent AgentSummary `yaml:"agent" json:"agent"`
Module AgentModule `yaml:"module" json:"module"`
}
// AgentSummary Agent 摘要信息
type AgentSummary struct {
AgentID string `yaml:"agent-id" json:"agentId"`
AgentName string `yaml:"agent-name" json:"agentName"`
AgentDesc string `yaml:"agent-desc" json:"agentDesc"`
}
// AgentModule Agent 模块配置,包含 API、模型、Agent 定义、工作流和 Runner
type AgentModule struct {
AiAPI AiAPIConfig `yaml:"ai-api" json:"aiApi"`
ChatModel ChatModelConfig `yaml:"chat-model" json:"chatModel"`
Agents []AgentConfig `yaml:"agents" json:"agents"`
AgentWorkflows []AgentWorkflowConfig `yaml:"agent-workflows" json:"agentWorkflows"`
Runner RunnerConfig `yaml:"runner" json:"runner"`
}
// AiAPIConfig LLM API 连接配置
type AiAPIConfig struct {
BaseURL string `yaml:"base-url" json:"baseUrl"`
APIKey string `yaml:"api-key" json:"apiKey"`
CompletionsPath string `yaml:"completions-path" json:"completionsPath"`
}
// ChatModelConfig 聊天模型配置
type ChatModelConfig struct {
Model string `yaml:"model" json:"model"`
}
// AgentConfig 单个 Agent 的定义
type AgentConfig struct {
Name string `yaml:"name" json:"name"`
Instruction string `yaml:"instruction" json:"instruction"`
Description string `yaml:"description" json:"description"`
OutputKey string `yaml:"output-key" json:"outputKey"`
}
// AgentWorkflowConfig 工作流配置,支持 loop/parallel/sequential 三种类型
type AgentWorkflowConfig struct {
Type WorkflowType `yaml:"type" json:"type"`
Name string `yaml:"name" json:"name"`
SubAgents []string `yaml:"sub-agents" json:"subAgents"`
Description string `yaml:"description" json:"description"`
MaxIterations int `yaml:"max-iterations" json:"maxIterations"`
}
// RunnerConfig Runner 配置,指定入口 Agent 名称
type RunnerConfig struct {
AgentName string `yaml:"agent-name" json:"agentName"`
}

View File

@@ -1 +1,178 @@
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
}
// ============================================================
// 核心接口
// ============================================================
// Tool 外部工具接口
type Tool interface {
Name() string
Description() string
Call(ctx context.Context, input string) (string, error)
}
// ChatModel 聊天模型接口,支持同步生成和流式输出
type ChatModel interface {
Generate(ctx context.Context, messages []ChatMessage) (ChatReply, error)
Stream(ctx context.Context, messages []ChatMessage) (<-chan ChatStreamEvent, <-chan error)
}
// Agent 智能体接口
type Agent interface {
Name() string
Run(ctx context.Context, content ChatContent) (string, error)
Stream(ctx context.Context, content ChatContent, out chan<- string) error
}
// Runner 运行器接口,管理会话并执行 Agent
type Runner interface {
CreateSession(userID string) (string, error)
Run(userID, sessionID string, content ChatContent) ([]string, error)
Stream(userID, sessionID string, content ChatContent) (<-chan string, <-chan error)
}
// ============================================================
// 注册与存储
// ============================================================
// RegisteredAgent 已注册的 Agent 信息
type RegisteredAgent struct {
AppName string
AgentID string
AgentName string
AgentDesc string
Runner Runner
}
// AgentRegistry Agent 注册表接口
type AgentRegistry interface {
Register(agent RegisteredAgent) error
Get(agentID string) (RegisteredAgent, bool)
List() []RegisteredAgent
}
// SessionStore 会话存储接口
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
}

View File

@@ -1 +1,12 @@
package types
const (
CodeSuccess = "0000"
InfoSuccess = "success"
CodeUnknownError = "0001"
InfoUnknownError = "unknown error"
CodeIllegalParameter = "0002"
InfoIllegalParameter = "illegal parameter"
CodeAgentNotFound = "0003"
InfoAgentNotFound = "agent not found"
)

View File

@@ -1 +1,17 @@
package types
type AppError struct {
Code string
Info string
}
func NewAppError(code, info string) *AppError {
return &AppError{Code: code, Info: info}
}
func (e *AppError) Error() string {
if e == nil {
return ""
}
return e.Code + ": " + e.Info
}