llm

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2025 License: BSD-2-Clause Imports: 13 Imported by: 0

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

View Source
const (
	AIModelKey_HEAVY  = "heavy"
	DefaultHeavyModel = "gpt-4"
)

Variables

View Source
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

func (*Agent) Analyze added in v0.6.0

func (a *Agent) Analyze(input map[string]string) (string, error)

Analyze performs the agent's analysis based on input

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

type ChatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

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

func (a *MCPAgent) AnalyzeWithMCP(input map[string]string) (string, error)

AnalyzeWithMCP performs analysis using both LLM and MCP tools

func (*MCPAgent) SetAPIKey added in v0.6.0

func (a *MCPAgent) SetAPIKey(apiKey string)

SetAPIKey sets the API key for the underlying agent

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

func NewMCPClient(serverURL string) *MCPClient

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

func (*MCPClient) Close added in v0.6.0

func (c *MCPClient) Close() error

Close closes the MCP client connection

func (*MCPClient) Connect added in v0.6.0

func (c *MCPClient) Connect() error

Connect establishes connection to MCP server and initializes session

func (*MCPClient) GetResourceDetails added in v0.6.0

func (c *MCPClient) GetResourceDetails(token string) (string, error)

GetResourceDetails retrieves detailed information about a Pulumi Registry resource

type MCPError added in v0.6.0

type MCPError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

MCPError represents an MCP error

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

func (s *Service) ChatCompletion(ctx context.Context, req ChatCompletionRequest) (string, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL