Class: Tavus::Resources::Objectives

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Objectives

Returns a new instance of Objectives.



6
7
8
# File 'lib/tavus/resources/objectives.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

Parameters:

  • field (String)

    The field path

  • value (Object)

    The value to set

  • operation (String) (defaults to: "replace")

    The operation type (default: “replace”)

Returns:

  • (Hash)

    A JSON Patch operation



63
64
65
66
67
# File 'lib/tavus/resources/objectives.rb', line 63

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

#create(data:) ⇒ Hash

Create new objectives

Parameters:

  • data (Array<Hash>)

    Array of objectives to create

Returns:

  • (Hash)

    The created objective details

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/tavus/resources/objectives.rb', line 13

def create(data:)
  raise ArgumentError, "data must be an array" unless data.is_a?(Array)
  raise ArgumentError, "data cannot be empty" if data.empty?

  @client.post("/v2/objectives", body: { data: data })
end

#delete(objectives_id) ⇒ Hash

Delete an objective

Parameters:

  • objectives_id (String)

    The unique identifier of the objective

Returns:

  • (Hash)

    Success response



82
83
84
# File 'lib/tavus/resources/objectives.rb', line 82

def delete(objectives_id)
  @client.delete("/v2/objectives/#{objectives_id}")
end

#get(objectives_id) ⇒ Hash

Get a single objective by ID

Parameters:

  • objectives_id (String)

    The unique identifier of the objective

Returns:

  • (Hash)

    The objective details



23
24
25
# File 'lib/tavus/resources/objectives.rb', line 23

def get(objectives_id)
  @client.get("/v2/objectives/#{objectives_id}")
end

#list(**options) ⇒ Hash

List all objectives

Parameters:

  • options (Hash)

    Query parameters

Options Hash (**options):

  • :limit (Integer)

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

  • :page (Integer)

    Page number to return (default: 1)

Returns:

  • (Hash)

    List of objectives and total count



32
33
34
# File 'lib/tavus/resources/objectives.rb', line 32

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

#patch(objectives_id, operations) ⇒ Hash

Update an objective using JSON Patch operations

Examples:

operations = [
  { op: "replace", path: "/data/0/objective_name", value: "updated_objective_name" },
  { op: "replace", path: "/data/0/objective_prompt", value: "Updated prompt" },
  { op: "add", path: "/data/0/output_variables", value: ["new_variable"] }
]

Parameters:

  • objectives_id (String)

    The unique identifier of the objective

  • operations (Array<Hash>)

    Array of JSON Patch operations

Returns:

  • (Hash)

    Success response

Raises:

  • (ArgumentError)


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

def patch(objectives_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/objectives/#{objectives_id}", body: operations)
end

#update_field(objectives_id, field, value) ⇒ Hash

Convenient method to update a field

Parameters:

  • objectives_id (String)

    The unique identifier of the objective

  • field (String)

    The field path to update

  • value (Object)

    The new value

Returns:

  • (Hash)

    Success response



74
75
76
77
# File 'lib/tavus/resources/objectives.rb', line 74

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