Class: Ace::PromptPrep::Organisms::PromptEnhancer

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/prompt_prep/organisms/prompt_enhancer.rb

Overview

Enhances prompts using LLM via ace-llm

Constant Summary collapse

DEFAULT_SYSTEM_PROMPT_URI =

Default system prompt URI

"prompt://prompt-enhance-instructions.system"

Class Method Summary collapse

Class Method Details

.call(content:, model: nil, system_prompt_uri: nil, temperature: 0.3) ⇒ Hash

Enhance prompt content using LLM

Parameters:

  • content (String)

    Original prompt content

  • model (String, nil) (defaults to: nil)

    Model alias or provider:model format (ace-llm handles alias resolution)

  • system_prompt_uri (String, nil) (defaults to: nil)

    System prompt URI or path

  • temperature (Float) (defaults to: 0.3)

    Temperature for LLM generation (0.0-2.0)

Returns:

  • (Hash)

    Result with :content, :enhanced, :cached, :error keys



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ace/prompt_prep/organisms/prompt_enhancer.rb', line 20

def self.call(content:, model: nil, system_prompt_uri: nil, temperature: 0.3)
  # Use default model from config if not specified
  resolved_model = model || Ace::PromptPrep.default_model

  # Validate and clamp temperature to valid range
  validated_temperature = [[temperature.to_f, Ace::PromptPrep.temperature_min].max, Ace::PromptPrep.temperature_max].min

  # Resolve system prompt path
  system_prompt_path = system_prompt_uri || DEFAULT_SYSTEM_PROMPT_URI

  # Load system prompt FIRST with context enrichment via session manager
  # This must happen before cache check so we can include resolved content in cache key
  session = EnhancementSessionManager.prepare_session(system_prompt_path)
  system_prompt = session[:content]

  unless system_prompt
    warn "Warning: Failed to load system prompt, using original content"
    return {
      content: content,
      enhanced: false,
      cached: false,
      error: session[:error] || "Failed to load system prompt"
    }
  end

  # Check cache with full key (content + model + resolved system prompt + temperature)
  # Uses resolved system_prompt content (not URI) so cache invalidates when prompt changes
  cache_key = Molecules::EnhancementTracker.cache_key(content, resolved_model, system_prompt, validated_temperature)
  if Molecules::EnhancementTracker.cached?(cache_key)
    cached_content = Molecules::EnhancementTracker.get_cached(cache_key)
    return {
      content: cached_content,
      enhanced: true,
      cached: true,
      error: nil
    }
  end

  # Call LLM
  begin
    require "ace/llm"

    result = Ace::LLM::QueryInterface.query(
      resolved_model,
      content,
      system: system_prompt,
      temperature: validated_temperature
    )

    enhanced_content = result[:text]

    # Validate response
    if enhanced_content.nil? || enhanced_content.empty?
      warn "Warning: LLM returned empty response, using original content"
      return {
        content: content,
        enhanced: false,
        cached: false,
        error: "Empty LLM response"
      }
    end

    # Store in cache
    Molecules::EnhancementTracker.store_cache(cache_key, enhanced_content)

    {
      content: enhanced_content,
      enhanced: true,
      cached: false,
      error: nil
    }
  rescue LoadError => e
    warn "Warning: ace-llm gem not available: #{e.message}"
    {
      content: content,
      enhanced: false,
      cached: false,
      error: "ace-llm not available"
    }
  rescue => e
    warn "Warning: LLM enhancement failed: #{e.message}"
    {
      content: content,
      enhanced: false,
      cached: false,
      error: e.message
    }
  end
end

.load_system_prompt(uri_or_path) ⇒ String?

Load system prompt from URI or path

Parameters:

  • uri_or_path (String)

    System prompt URI or path

Returns:

  • (String, nil)

    System prompt content or nil if failed



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ace/prompt_prep/organisms/prompt_enhancer.rb', line 114

def self.load_system_prompt(uri_or_path)
  # Try to resolve via ace-nav if it's a protocol URI
  if uri_or_path.include?("://")
    begin
      require "ace/support/nav/organisms/navigation_engine"

      engine = Ace::Support::Nav::Organisms::NavigationEngine.new

      # First try to get content directly (most efficient)
      content = engine.resolve(uri_or_path, content: true)
      return content if content.is_a?(String) && !content.empty?

      # Fallback: resolve to path and read file
      # Note: resolve() without options returns a string path, not a hash
      result = engine.resolve(uri_or_path)
      if result.is_a?(String) && File.exist?(result)
        return File.read(result, encoding: "utf-8")
      end
    rescue LoadError => e
      warn "Warning: ace-nav not available: #{e.message}" if ENV["DEBUG"]
    rescue => e
      warn "Warning: ace-nav resolution failed: #{e.message}" if ENV["DEBUG"]
    end
  end

  # Try as direct file path
  if File.exist?(uri_or_path)
    return File.read(uri_or_path, encoding: "utf-8")
  end

  # Failed to load
  nil
rescue => e
  warn "Warning: Failed to load system prompt: #{e.message}"
  nil
end