Documentation
¶
Overview ¶
*
- Copyright © 2025 Magdiel Campelo <github.com/MagdielCAS/magi-cli>
- This file is part of the magi-cli
*
Package llm centralizes AI service construction for magi commands. The service builder enforces shared defaults (timeouts, provider selection, base URL overrides, and hardened HTTP clients) that are consumed by cmd/commit.go, cmd/pr.go, and future commands that need to talk to LLMs.
*
- Copyright © 2025 Magdiel Campelo <github.com/MagdielCAS/magi-cli>
- This file is part of the magi-cli
*
Index ¶
- Constants
- Variables
- func FixCommitMessage(ctx context.Context, runtime *shared.RuntimeContext, ...) (string, error)
- func GenerateCommitMessage(ctx context.Context, runtime *shared.RuntimeContext, diff string) (string, error)
- type Agent
- type ChatCompletionRequest
- type ChatMessage
- type CompletionRequest
- type MCPAgent
- type MCPAgentConfig
- type MCPClient
- type MCPError
- type MCPRequest
- type MCPResponse
- type MCPTool
- type ModelVariant
- type Service
- type ServiceBuilder
- func (b *ServiceBuilder) Build() (*Service, error)
- func (b *ServiceBuilder) UseFallbackModel() *ServiceBuilder
- func (b *ServiceBuilder) UseHeavyModel() *ServiceBuilder
- func (b *ServiceBuilder) UseLightModel() *ServiceBuilder
- func (b *ServiceBuilder) WithAPIKey(key string) *ServiceBuilder
- func (b *ServiceBuilder) WithBaseURL(baseURL string) *ServiceBuilder
- func (b *ServiceBuilder) WithHTTPClient(client *http.Client) *ServiceBuilder
- func (b *ServiceBuilder) WithModel(model string) *ServiceBuilder
Constants ¶
const ( AIModelKey_HEAVY = "heavy" DefaultHeavyModel = "gpt-4" )
Variables ¶
var ( CommitSchema = &openai.ChatCompletionNewParamsResponseFormatUnion{ OfJSONSchema: &openaiShared.ResponseFormatJSONSchemaParam{ JSONSchema: openaiShared.ResponseFormatJSONSchemaJSONSchemaParam{ Name: "commit_message", Description: openai.String("A conventional commit message"), Schema: interface{}(map[string]interface{}{ "type": "object", "properties": map[string]interface{}{ "type": map[string]interface{}{"type": "string", "enum": []string{"feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert"}}, "scope": map[string]interface{}{"type": "string"}, "gitmoji": map[string]interface{}{"type": "string"}, "description": map[string]interface{}{"type": "string"}, }, "required": []string{"type", "scope", "gitmoji", "description"}, "additionalProperties": false, }), Strict: openai.Bool(true), }, }, } )
Functions ¶
func FixCommitMessage ¶
func FixCommitMessage(ctx context.Context, runtime *shared.RuntimeContext, diff, previousMessage string, validationErr error) (string, error)
FixCommitMessage reparses the diff with guidance about the validation failure and returns a corrected message.
func GenerateCommitMessage ¶
func GenerateCommitMessage(ctx context.Context, runtime *shared.RuntimeContext, diff string) (string, error)
GenerateCommitMessage requests an AI-generated conventional commit message for the supplied diff.
Types ¶
type Agent ¶ added in v0.6.0
type Agent struct {
Name string
Task string
Personality string
CompletionRequest CompletionRequest
Runtime *shared.RuntimeContext
}
Agent represents a generic AI agent
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Messages []ChatMessage
Temperature float64
MaxTokens float64
TopP float64
FrequencyPenalty float64
PresencePenalty float64
ResponseFormat *openai.ChatCompletionNewParamsResponseFormatUnion
}
ChatCompletionRequest represents a chat completion call.
type ChatMessage ¶
ChatMessage represents a message in a chat completion request.
type CompletionRequest ¶ added in v0.6.0
type CompletionRequest struct {
ChatCompletionRequest
ApiKey string
}
CompletionRequest wraps ChatCompletionRequest with additional fields
type MCPAgent ¶ added in v0.6.0
type MCPAgent struct {
Agent
MCPClient *MCPClient
Tools []string // Available MCP tools for this agent
}
MCPAgent represents an agent that can use MCP tools
func NewMCPAgent ¶ added in v0.6.0
func NewMCPAgent(config MCPAgentConfig, mcpClient *MCPClient, runtime *shared.RuntimeContext) *MCPAgent
NewMCPAgent creates a new MCP-enabled agent
func (*MCPAgent) AnalyzeWithMCP ¶ added in v0.6.0
AnalyzeWithMCP performs analysis using both LLM and MCP tools
type MCPAgentConfig ¶ added in v0.6.0
type MCPAgentConfig struct {
Name string
Task string
Personality string
Tools []string
MaxTokens int
UseTextFormat bool
}
MCPAgentConfig configuration for MCP-enabled agents
type MCPClient ¶ added in v0.6.0
type MCPClient struct {
ServerURL string
HTTPClient *http.Client
SessionID string
Tools map[string]MCPTool
}
MCPClient represents a Model Context Protocol client
func NewMCPClient ¶ added in v0.6.0
NewMCPClient creates a new MCP client
func (*MCPClient) CallTool ¶ added in v0.6.0
func (c *MCPClient) CallTool(toolName string, arguments map[string]interface{}) (interface{}, error)
CallTool executes a tool on the MCP server
type MCPRequest ¶ added in v0.6.0
type MCPRequest struct {
Method string `json:"method"`
Params interface{} `json:"params"`
}
MCPRequest represents a request to an MCP server
type MCPResponse ¶ added in v0.6.0
type MCPResponse struct {
Result interface{} `json:"result"`
Error *MCPError `json:"error,omitempty"`
}
MCPResponse represents a response from an MCP server
type MCPTool ¶ added in v0.6.0
type MCPTool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]interface{} `json:"inputSchema"`
}
MCPTool represents an available MCP tool
type ModelVariant ¶
type ModelVariant int
ModelVariant defines the logical model buckets (light/heavy/fallback) available to commands.
const ( ModelVariantHeavy ModelVariant = iota ModelVariantLight ModelVariantFallback )
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service executes LLM requests using the resolved configuration.
func (*Service) ChatCompletion ¶
ChatCompletion sends a chat completion request and returns the assistant response text.
type ServiceBuilder ¶
type ServiceBuilder struct {
// contains filtered or unexported fields
}
ServiceBuilder lets commands configure which model tier, API key, and endpoint should be used.
func NewServiceBuilder ¶
func NewServiceBuilder(runtime *shared.RuntimeContext) *ServiceBuilder
NewServiceBuilder creates a builder tied to the given runtime context.
func (*ServiceBuilder) Build ¶
func (b *ServiceBuilder) Build() (*Service, error)
Build resolves the requested configuration and returns a ready-to-use LLM service.
func (*ServiceBuilder) UseFallbackModel ¶
func (b *ServiceBuilder) UseFallbackModel() *ServiceBuilder
UseFallbackModel selects the configured fallback model tier.
func (*ServiceBuilder) UseHeavyModel ¶
func (b *ServiceBuilder) UseHeavyModel() *ServiceBuilder
UseHeavyModel selects the heavy model tier (default).
func (*ServiceBuilder) UseLightModel ¶
func (b *ServiceBuilder) UseLightModel() *ServiceBuilder
UseLightModel selects the light model tier configured in the runtime context.
func (*ServiceBuilder) WithAPIKey ¶
func (b *ServiceBuilder) WithAPIKey(key string) *ServiceBuilder
WithAPIKey overrides the API key used for the service.
func (*ServiceBuilder) WithBaseURL ¶
func (b *ServiceBuilder) WithBaseURL(baseURL string) *ServiceBuilder
WithBaseURL overrides the base URL used for the service.
func (*ServiceBuilder) WithHTTPClient ¶
func (b *ServiceBuilder) WithHTTPClient(client *http.Client) *ServiceBuilder
WithHTTPClient overrides the HTTP client (otherwise RuntimeContext HTTP client is reused).
func (*ServiceBuilder) WithModel ¶
func (b *ServiceBuilder) WithModel(model string) *ServiceBuilder
WithModel overrides the model identifier entirely.