ai

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildSystemPrompt

func BuildSystemPrompt() string

BuildSystemPrompt creates the static system prompt for the AI assistant. This prompt is designed to be cachable - all static content is included here. Dynamic content (current YAML context) should be added as separate messages.

func FormatValidationIssues

func FormatValidationIssues(issues []ValidationIssue) string

FormatValidationIssues formats validation issues as a human-readable string.

func GetAvailableTaskNames

func GetAvailableTaskNames() []string

GetAvailableTaskNames returns all available task names for reference.

func SerializeValidationResult

func SerializeValidationResult(result *ValidationResult) (json.RawMessage, error)

SerializeValidationResult converts a validation result to JSON.

Types

type ChatMessage

type ChatMessage struct {
	Role    string `json:"role"` // "system", "user", "assistant"
	Content string `json:"content"`
}

ChatMessage represents a single message in the chat history.

type ChatRequest

type ChatRequest struct {
	Model       string        `json:"model"`
	Messages    []ChatMessage `json:"messages"`
	MaxTokens   int           `json:"max_tokens,omitempty"`
	Temperature float64       `json:"temperature,omitempty"`
	Stream      bool          `json:"stream,omitempty"`
}

ChatRequest represents a request to the chat completions endpoint.

type ChatResponse

type ChatResponse struct {
	ID      string `json:"id"`
	Model   string `json:"model"`
	Choices []struct {
		Message      ChatMessage `json:"message"`
		FinishReason string      `json:"finish_reason"`
	} `json:"choices"`
	Usage struct {
		PromptTokens     int `json:"prompt_tokens"`
		CompletionTokens int `json:"completion_tokens"`
		TotalTokens      int `json:"total_tokens"`
	} `json:"usage"`
}

ChatResponse represents a response from the chat completions endpoint.

type OpenRouterClient

type OpenRouterClient struct {
	// contains filtered or unexported fields
}

OpenRouterClient handles communication with the OpenRouter API.

func NewOpenRouterClient

func NewOpenRouterClient(apiKey string) *OpenRouterClient

NewOpenRouterClient creates a new OpenRouter client.

func (*OpenRouterClient) Chat

Chat sends a chat completion request and returns the response.

func (*OpenRouterClient) ChatStream

func (c *OpenRouterClient) ChatStream(
	ctx context.Context,
	req *ChatRequest,
	onChunk func(string),
) (*ChatResponse, error)

ChatStream sends a streaming chat completion request. The onChunk callback is called for each content chunk received. Returns the final response with usage statistics.

type Session

type Session struct {
	ID        string        `json:"id"`
	Status    SessionStatus `json:"status"`
	CreatedAt time.Time     `json:"createdAt"`
	UpdatedAt time.Time     `json:"updatedAt"`

	// Response data
	Response      string            `json:"response"`
	GeneratedYaml string            `json:"generatedYaml,omitempty"`
	Validation    *ValidationResult `json:"validation,omitempty"`

	// Token usage
	Usage struct {
		PromptTokens     int `json:"promptTokens"`
		CompletionTokens int `json:"completionTokens"`
		TotalTokens      int `json:"totalTokens"`
	} `json:"usage"`

	// Error information
	Error string `json:"error,omitempty"`

	// Fix attempt tracking
	FixAttempts         int  `json:"fixAttempts"`
	MaxFixAttempts      int  `json:"-"`
	WarningFixAttempted bool `json:"warningFixAttempted"`
	// contains filtered or unexported fields
}

Session represents an AI chat session with streaming support.

func (*Session) AppendResponse

func (s *Session) AppendResponse(chunk string)

AppendResponse appends to the response (for streaming).

func (*Session) CanFixWarnings

func (s *Session) CanFixWarnings() bool

CanFixWarnings checks if a warning fix attempt is allowed (only 1 allowed).

func (*Session) CanRetryFix

func (s *Session) CanRetryFix() bool

CanRetryFix checks if another fix attempt is allowed.

func (*Session) Complete

func (s *Session) Complete()

Complete marks the session as complete.

func (*Session) GetSnapshot

func (s *Session) GetSnapshot() *Session

GetSnapshot returns a copy of the session for safe reading.

func (*Session) IncrementFixAttempts

func (s *Session) IncrementFixAttempts() int

IncrementFixAttempts increments the fix attempt counter.

func (*Session) SetCancelFunc

func (s *Session) SetCancelFunc(cancel context.CancelFunc)

SetCancelFunc sets the cancel function for the session.

func (*Session) SetError

func (s *Session) SetError(err string)

SetError sets an error and marks the session as failed.

func (*Session) SetGeneratedYaml

func (s *Session) SetGeneratedYaml(yaml string)

SetGeneratedYaml sets the generated YAML.

func (*Session) SetResponse

func (s *Session) SetResponse(response string)

SetResponse sets the response content.

func (*Session) SetUsage

func (s *Session) SetUsage(prompt, completion, total int)

SetUsage sets the token usage.

func (*Session) SetValidation

func (s *Session) SetValidation(validation *ValidationResult)

SetValidation sets the validation result.

func (*Session) SetWarningFixAttempted

func (s *Session) SetWarningFixAttempted()

SetWarningFixAttempted marks that a warning fix was attempted.

func (*Session) Subscribe

func (s *Session) Subscribe() <-chan *Session

Subscribe returns a channel that receives session updates.

func (*Session) Unsubscribe

func (s *Session) Unsubscribe(ch <-chan *Session)

Unsubscribe removes a subscriber channel.

func (*Session) UpdateStatus

func (s *Session) UpdateStatus(status SessionStatus)

UpdateStatus updates the session status and notifies subscribers.

type SessionManager

type SessionManager struct {
	// contains filtered or unexported fields
}

SessionManager manages AI chat sessions.

func NewSessionManager

func NewSessionManager() *SessionManager

NewSessionManager creates a new session manager.

func (*SessionManager) CreateSession

func (sm *SessionManager) CreateSession(maxFixAttempts int) *Session

CreateSession creates a new session and returns it.

func (*SessionManager) DeleteSession

func (sm *SessionManager) DeleteSession(id string)

DeleteSession removes a session.

func (*SessionManager) GetSession

func (sm *SessionManager) GetSession(id string) *Session

GetSession retrieves a session by ID.

func (*SessionManager) Stop

func (sm *SessionManager) Stop()

Stop stops the session manager cleanup loop.

type SessionStatus

type SessionStatus string

SessionStatus represents the current state of an AI chat session.

const (
	SessionStatusPending    SessionStatus = "pending"
	SessionStatusStreaming  SessionStatus = "streaming"
	SessionStatusValidating SessionStatus = "validating"
	SessionStatusFixing     SessionStatus = "fixing"
	SessionStatusComplete   SessionStatus = "complete"
	SessionStatusError      SessionStatus = "error"
)

type StreamChunk

type StreamChunk struct {
	ID      string `json:"id"`
	Model   string `json:"model"`
	Choices []struct {
		Delta struct {
			Content string `json:"content"`
		} `json:"delta"`
		FinishReason string `json:"finish_reason"`
	} `json:"choices"`
}

StreamChunk represents a single chunk from a streaming response.

type ValidationIssue

type ValidationIssue struct {
	Type    string `json:"type"`    // "error" or "warning"
	Path    string `json:"path"`    // Path to the issue (e.g., "tasks[0].name")
	Message string `json:"message"` // Human-readable message
}

ValidationIssue represents a problem found in the generated YAML.

type ValidationResult

type ValidationResult struct {
	Valid  bool              `json:"valid"`
	Issues []ValidationIssue `json:"issues,omitempty"`
}

ValidationResult contains the results of validating generated YAML.

func ValidateGeneratedYaml

func ValidateGeneratedYaml(yamlContent string) *ValidationResult

ValidateGeneratedYaml validates the AI-generated YAML for correct task names and configs.

Jump to

Keyboard shortcuts

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