Module: LanguageOperator::CLI::Commands::System::SynthesisTemplate

Included in:
Base
Defined in:
lib/language_operator/cli/commands/system/synthesis_template.rb

Overview

Synthesis template export command

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
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
109
110
111
112
113
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
# File 'lib/language_operator/cli/commands/system/synthesis_template.rb', line 12

def self.included(base)
  base.class_eval do
    desc 'synthesis-template', 'Export synthesis templates for agent code generation'
    long_desc <<-DESC
      Export the synthesis templates used by the Language Operator to generate
      agent code from natural language instructions.

      These templates are used by the operator's synthesis engine to convert
      user instructions into executable Ruby DSL code.

      Examples:
        # Export agent synthesis template (default)
        langop system synthesis-template

        # Export persona distillation template
        langop system synthesis-template --type persona

        # Export as JSON with schema included
        langop system synthesis-template --format json --with-schema

        # Export as YAML
        langop system synthesis-template --format yaml

        # Validate template syntax
        langop system synthesis-template --validate

        # Save to file
        langop system synthesis-template > agent_synthesis.tmpl
    DESC
    option :format, type: :string, default: 'template', desc: 'Output format (template, json, yaml)'
    option :type, type: :string, default: 'agent', desc: 'Template type (agent, persona)'
    option :with_schema, type: :boolean, default: false, desc: 'Include DSL schema in output'
    option :validate, type: :boolean, default: false, desc: 'Validate template syntax'
    def synthesis_template
      handle_command_error('load template') do
        # Validate type
        template_type = options[:type].downcase
        unless %w[agent persona].include?(template_type)
          Formatters::ProgressFormatter.error("Invalid template type: #{template_type}")
          puts
          puts 'Supported types: agent, persona'
          exit 1
        end

        # Load template
        template_content = load_template(template_type)

        # Validate if requested
        if options[:validate]
          validation_result = validate_template_content(template_content, template_type)

          # Display warnings if any
          unless validation_result[:warnings].empty?
            Formatters::ProgressFormatter.warn('Template validation warnings:')
            validation_result[:warnings].each do |warning|
              puts "#{warning}"
            end
            puts
          end

          # Display errors and exit if validation failed
          if validation_result[:valid]
            Formatters::ProgressFormatter.success('Template validation passed')
            return
          else
            Formatters::ProgressFormatter.error('Template validation failed:')
            validation_result[:errors].each do |error|
              puts "#{error}"
            end
            exit 1
          end
        end

        # Generate output based on format
        format = options[:format].downcase
        case format
        when 'template'
          output_template_format(template_content)
        when 'json'
          output_json_format(template_content, template_type)
        when 'yaml'
          output_yaml_format(template_content, template_type)
        else
          Formatters::ProgressFormatter.error("Invalid format: #{format}")
          puts
          puts 'Supported formats: template, json, yaml'
          exit 1
        end
      end
    end

    private

    # Output raw template format
    def output_template_format(content)
      puts content
    end

    # Output JSON format with metadata
    def output_json_format(content, type)
      data = {
        version: Dsl::Schema.version,
        template_type: type,
        template: content
      }

      if options[:with_schema]
        data[:schema] = Dsl::Schema.to_json_schema
        data[:safe_agent_methods] = Dsl::Schema.safe_agent_methods
        data[:safe_tool_methods] = Dsl::Schema.safe_tool_methods
        data[:safe_helper_methods] = Dsl::Schema.safe_helper_methods
      end

      puts JSON.pretty_generate(data)
    end

    # Output YAML format with metadata
    def output_yaml_format(content, type)
      data = {
        'version' => Dsl::Schema.version,
        'template_type' => type,
        'template' => content
      }

      if options[:with_schema]
        data['schema'] = Dsl::Schema.to_json_schema.transform_keys(&:to_s)
        data['safe_agent_methods'] = Dsl::Schema.safe_agent_methods
        data['safe_tool_methods'] = Dsl::Schema.safe_tool_methods
        data['safe_helper_methods'] = Dsl::Schema.safe_helper_methods
      end

      puts YAML.dump(data)
    end
  end
end