Class: Tavus::Resources::Guardrails

Inherits:
Object
  • Object
show all
Defined in:
lib/tavus/resources/guardrails.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Guardrails



6
7
8
# File 'lib/tavus/resources/guardrails.rb', line 6

def initialize(client)
  @client = client
end

Instance Method Details

#build_patch_operation(field, value, operation: "replace") ⇒ Hash

Helper method to build patch operations



64
65
66
67
68
# File 'lib/tavus/resources/guardrails.rb', line 64

def build_patch_operation(field, value, operation: "replace")
  op = { op: operation, path: field }
  op[:value] = value unless operation == "remove"
  op
end

#create(name: nil, data: []) ⇒ Hash

Create new guardrails



14
15
16
17
18
19
20
# File 'lib/tavus/resources/guardrails.rb', line 14

def create(name: nil, data: [])
  body = {}
  body[:name] = name if name
  body[:data] = data unless data.empty?

  @client.post("/v2/guardrails", body: body)
end

#delete(guardrails_id) ⇒ Hash

Delete guardrails



83
84
85
# File 'lib/tavus/resources/guardrails.rb', line 83

def delete(guardrails_id)
  @client.delete("/v2/guardrails/#{guardrails_id}")
end

#get(guardrails_id) ⇒ Hash

Get a single set of guardrails by ID



25
26
27
# File 'lib/tavus/resources/guardrails.rb', line 25

def get(guardrails_id)
  @client.get("/v2/guardrails/#{guardrails_id}")
end

#list(**options) ⇒ Hash

List all guardrails

Options Hash (**options):

  • :limit (Integer)

    Number of guardrails to return per page (default: 10)

  • :page (Integer)

    Page number to return (default: 1)



34
35
36
# File 'lib/tavus/resources/guardrails.rb', line 34

def list(**options)
  @client.get("/v2/guardrails", params: options)
end

#patch(guardrails_id, operations) ⇒ Hash

Update guardrails using JSON Patch operations

Examples:

operations = [
  { op: "replace", path: "/data/0/guardrails_prompt", value: "Your updated prompt" },
  { op: "add", path: "/data/0/callback_url", value: "https://your-server.com/webhook" }
]

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tavus/resources/guardrails.rb', line 47

def patch(guardrails_id, operations)
  raise ArgumentError, "Operations must be an array" unless operations.is_a?(Array)
  raise ArgumentError, "Operations cannot be empty" if operations.empty?

  operations.each do |op|
    raise ArgumentError, "Each operation must have 'op' and 'path'" unless op[:op] && op[:path]
    raise ArgumentError, "Operation 'op' must be one of: add, remove, replace, copy, move, test" unless %w[add remove replace copy move test].include?(op[:op])
  end

  @client.patch("/v2/guardrails/#{guardrails_id}", body: operations)
end

#update_field(guardrails_id, field, value) ⇒ Hash

Convenient method to update a field



75
76
77
78
# File 'lib/tavus/resources/guardrails.rb', line 75

def update_field(guardrails_id, field, value)
  operation = build_patch_operation(field, value, operation: "replace")
  patch(guardrails_id, [operation])
end