Documentation
¶
Overview ¶
Package agent implements the core AI agent system using Google's Genkit framework.
Provides Agent type for orchestrating AI interactions with:
- Genkit AI model interactions with RAG-first design (retriever required, always enabled)
- Conversation history management (in-memory with optional database persistence)
- Tool registration via internal/tools (file, system, network)
- Security validation (path traversal, command injection, SSRF prevention)
Agent is thread-safe for concurrent access (messages protected by RWMutex). Related packages: internal/tools, internal/session.
NOTE: MCP Client support temporarily removed during Phase 2 refactoring. Phase 2 implements MCP Server (not client). MCP Client may be re-added in future if needed.
Package agent provides conversation history management functionality.
This file contains history-related methods:
- ClearHistory: Clear conversation history
- HistoryLength: Get history length
- trimHistoryIfNeeded: Limit history size (sliding window)
- vectorizeConversationTurn: Vectorize and store conversation turns
- Helper functions for vectorization
Package agent provides session management functionality for the AI agent.
This file contains session-related methods:
- loadCurrentSession: Load session from local state
- NewSession: Create new conversation session
- SwitchSession: Switch to existing session
- GetCurrentSession: Get current session info
Package agent provides type definitions and interfaces for the AI agent system.
This file contains:
- Core interfaces (Generator, SessionStore, KnowledgeStore)
- Response types
- Functional options for Agent configuration
Index ¶
- type Agent
- func (a *Agent) ClearHistory()
- func (a *Agent) Execute(ctx context.Context, input string) (*Response, error)
- func (a *Agent) GetCurrentSession(ctx context.Context) (*session.Session, error)
- func (a *Agent) HistoryLength() int
- func (a *Agent) NewSession(ctx context.Context, title string) (*session.Session, error)
- func (a *Agent) SwitchSession(ctx context.Context, sessionID uuid.UUID) error
- type Generator
- type KnowledgeStore
- type Option
- type Response
- type SessionStore
- type TestAgentFramework
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent encapsulates Genkit AI functionality.
Responsibilities: AI interactions, tool registration, conversation history (in-memory and optional persistence). NOT responsible for: User interaction (cmd package handles CLI).
Thread Safety: Thread-safe for concurrent access (messages protected by RWMutex). Tools (registered via tools.RegisterTools) are thread-safe and hold their own validators.
func New ¶
func New( ctx context.Context, cfg *config.Config, g *genkit.Genkit, retriever ai.Retriever, opts ...Option, ) (*Agent, error)
New creates a new Agent instance with RAG support and session persistence. Accepts pre-initialized Genkit instance and retriever (resolves circular dependency, enables DI and testing). Registers tools and loads system prompt from Dotprompt file.
Parameters:
- ctx: Context
- cfg: Configuration
- g: Genkit instance
- retriever: RAG retriever
- opts: Functional options for configuration
func (*Agent) ClearHistory ¶
func (a *Agent) ClearHistory()
ClearHistory clears the conversation history
func (*Agent) Execute ¶
Execute runs the agent with the given input and returns the complete response. This is a synchronous, blocking operation that uses Genkit's native ReAct engine. Following 建議.md architecture: single Generate call with WithMaxTurns.
Architecture decisions (v4 consensus):
- Trust Genkit framework to handle multi-turn tool calling
- Sacrifice interrupt/human-in-the-loop for simplicity and elegance
- Sacrifice real-time streaming feedback
- Use Genkit's built-in OpenTelemetry for observability (no custom metrics)
func (*Agent) GetCurrentSession ¶
GetCurrentSession retrieves the current session information.
Returns:
- *session.Session: Current session
- error: If no active session or retrieval fails
func (*Agent) HistoryLength ¶
HistoryLength retrieves the conversation history length
func (*Agent) NewSession ¶
NewSession creates a new conversation session and switches to it. Clears current conversation history and starts fresh.
Parameters:
- title: Session title (can be empty)
Returns:
- *session.Session: Created session
- error: If creation fails
func (*Agent) SwitchSession ¶
SwitchSession switches to an existing session. Loads the session's conversation history from database.
Parameters:
- sessionID: UUID of the session to switch to
Returns:
- error: If switching fails
type Generator ¶
type Generator interface {
Generate(ctx context.Context, opts ...ai.GenerateOption) (*ai.ModelResponse, error)
}
Generator defines an interface for generating model responses, allowing for mocking in tests.
type KnowledgeStore ¶
type KnowledgeStore interface {
// Count returns the number of documents matching the filter
Count(ctx context.Context, filter map[string]string) (int, error)
// Add adds a document to the knowledge store with automatic embedding generation
Add(ctx context.Context, doc knowledge.Document) error
// Search performs semantic search on knowledge documents
Search(ctx context.Context, query string, opts ...knowledge.SearchOption) ([]knowledge.Result, error)
}
KnowledgeStore defines the minimal interface for knowledge operations needed by Agent. Following Go best practices: interfaces are defined by the consumer (agent), not the provider (knowledge package). This allows Agent to depend on abstraction rather than concrete implementation, improving testability.
Design: Only includes methods actually used by Agent (Count, Add, Search), not the full knowledge.Store API. This follows the Interface Segregation Principle - clients should not depend on methods they don't use.
Note: Search is needed by tools.KnowledgeSearcher interface (used in tools.RegisterTools).
type Option ¶
type Option func(*Agent)
Option is a functional option for configuring the Agent.
func WithKnowledgeStore ¶
func WithKnowledgeStore(store KnowledgeStore) Option
WithKnowledgeStore sets the knowledge store for the agent.
func WithLogger ¶
WithLogger sets the logger for the agent.
func WithSessionStore ¶
func WithSessionStore(store SessionStore) Option
WithSessionStore sets the session store for the agent.
type Response ¶
type Response struct {
FinalText string // Model's final text output
History []*ai.Message // Complete conversation history including all tool calls
}
Response represents the complete result of an agent execution. Designed for synchronous, blocking execution following 建議.md architecture.
type SessionStore ¶
type SessionStore interface {
// CreateSession creates a new conversation session
CreateSession(ctx context.Context, title, modelName, systemPrompt string) (*session.Session, error)
// GetSession retrieves a session by ID
GetSession(ctx context.Context, sessionID uuid.UUID) (*session.Session, error)
// GetMessages retrieves messages for a session with pagination
GetMessages(ctx context.Context, sessionID uuid.UUID, limit, offset int32) ([]*session.Message, error)
// AddMessages adds multiple messages to a session in batch
AddMessages(ctx context.Context, sessionID uuid.UUID, messages []*session.Message) error
}
SessionStore defines the interface for session persistence operations. Following Go best practices: interfaces are defined by the consumer (agent), not the provider (session package). This allows Agent to depend on abstraction rather than concrete implementation, improving testability.
type TestAgentFramework ¶
type TestAgentFramework struct {
// Database
DBContainer *testutil.TestDBContainer
// Core components
Agent *Agent
KnowledgeStore *knowledge.Store
SystemIndexer *knowledge.SystemKnowledgeIndexer
SessionStore *session.Store
Genkit *genkit.Genkit
Embedder ai.Embedder
Retriever ai.Retriever
Config *config.Config
// Test session
SessionID uuid.UUID
// contains filtered or unexported fields
}
TestAgentFramework provides a complete test environment for Agent integration tests.
Includes all components needed for full-stack agent testing:
- PostgreSQL database with test data
- Genkit AI framework
- Knowledge store with embeddings
- Session management
- Configured Agent instance
Usage:
framework, cleanup := SetupTestAgent(t) defer cleanup() resp, err := framework.Agent.Execute(ctx, "test query")
func SetupTestAgent ¶
func SetupTestAgent(t *testing.T) (*TestAgentFramework, func())
SetupTestAgent creates a complete Agent test environment with testcontainers.
Requirements:
- GEMINI_API_KEY environment variable must be set
- Docker daemon must be running (for testcontainers)
Creates:
- PostgreSQL container with pgvector
- Genkit instance with Google AI plugin
- Embedder for vector operations
- Knowledge store for RAG
- Session store for conversation persistence
- Fully configured Agent instance
Returns:
- TestAgentFramework: Complete test environment
- cleanup function: Must be called to terminate containers
Example:
func TestAgentFeature(t *testing.T) {
framework, cleanup := SetupTestAgent(t)
defer cleanup()
resp, err := framework.Agent.Execute(ctx, "What is Go?")
require.NoError(t, err)
assert.NotEmpty(t, resp.FinalText)
}
func (*TestAgentFramework) IndexSystemKnowledge ¶
func (f *TestAgentFramework) IndexSystemKnowledge(t *testing.T)
IndexSystemKnowledge indexes system knowledge for testing.
Indexes all 6 system knowledge documents (coding standards, error handling, etc.) into the knowledge store for RAG testing.
Example:
framework, cleanup := SetupTestAgent(t) defer cleanup() framework.IndexSystemKnowledge(t) // Now system knowledge is available for RAG queries