Class: Ad::AgentArchitecture::Dsl::Actions::SaveDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/ad/agent_architecture/dsl/actions/save_database.rb

Overview

Save workflow graph to database

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow) ⇒ SaveDatabase

Returns a new instance of SaveDatabase.



11
12
13
# File 'lib/ad/agent_architecture/dsl/actions/save_database.rb', line 11

def initialize(workflow)
  @workflow_hash = workflow.data
end

Instance Attribute Details

#workflow_idObject (readonly)

Returns the value of attribute workflow_id.



9
10
11
# File 'lib/ad/agent_architecture/dsl/actions/save_database.rb', line 9

def workflow_id
  @workflow_id
end

Instance Method Details

#saveObject



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
# File 'lib/ad/agent_architecture/dsl/actions/save_database.rb', line 15

def save
  @workflow_id = nil
  # puts JSON.pretty_generate(@workflow_hash)
  DB.transaction do
    # puts "Saving Workflow: #{@workflow_hash[:name]}"
    # Save workflow
    workflow_record = Ad::AgentArchitecture::Database::Workflow.create(
      name: @workflow_hash[:name],
      description: @workflow_hash[:description]
    )

    @workflow_id = workflow_record.id

    # Saving attributes
    attribute_records = @workflow_hash[:attributes].map do |_name, attr|
      # puts "Saving Attribute: #{attr}"
      Ad::AgentArchitecture::Database::Attribute.create(
        name: attr[:name],
        type: attr[:type],
        is_array: attr[:is_array],
        description: attr[:description],
        workflow: workflow_record
      )
    end

    attribute_map = attribute_records.to_h { |ar| [ar.name.to_sym, ar] }

    # Save prompts
    @workflow_hash[:prompts].each_value do |prompt|
      # puts "Saving Prompt: #{prompt}"
      Ad::AgentArchitecture::Database::Prompt.create(
        name: prompt[:name],
        path: prompt[:path],
        content: prompt[:content],
        description: prompt[:description],
        workflow: workflow_record
      )
    end

    # Save settings
    @workflow_hash[:settings].each do |name, setting|
      puts "Saving Setting: #{setting}"
      Ad::AgentArchitecture::Database::Setting.create(
        name: name.to_s,
        value: setting[:value],
        description: setting[:description],
        workflow: workflow_record
      )
    end

    # Save sections and steps
    @workflow_hash[:sections].each do |section|
      # puts "Saving Section: #{section}"
      section_record = Ad::AgentArchitecture::Database::Section.create(
        name: section[:name],
        order: section[:order],
        description: section[:description],
        workflow: workflow_record
      )

      section[:steps].each do |step|
        # puts "Saving Step: #{step}"
        step_record = Ad::AgentArchitecture::Database::Step.create(
          name: step[:name],
          order: step[:order],
          prompt: step[:prompt],
          description: step[:description],
          section: section_record
        )

        step[:input_attributes].each do |attr_name|
          # puts "Saving Input Attribute for Step: #{attr_name}"
          Ad::AgentArchitecture::Database::InputAttribute.create(
            step: step_record,
            attribute: attribute_map[attr_name.to_sym]
          )
        end

        step[:output_attributes].each do |attr_name|
          # puts "Saving Output Attribute for Step: #{attr_name}"
          Ad::AgentArchitecture::Database::OutputAttribute.create(
            step: step_record,
            attribute: attribute_map[attr_name.to_sym]
          )
        end
      end
    end
  end
  @workflow_id
rescue StandardError => e
  puts "An error occurred: #{e.message}"
  raise
end