Class: LangsmithRunBuffer

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
lib/generators/langsmithrb_rails/buffer/templates/langsmith_run_buffer.rb

Overview

Model for buffering LangSmith traces

Instance Method Summary collapse

Instance Method Details

#mark_failed(error) ⇒ Boolean

Mark the trace as failed



57
58
59
60
61
62
63
# File 'lib/generators/langsmithrb_rails/buffer/templates/langsmith_run_buffer.rb', line 57

def mark_failed(error)
  increment!(:retry_count)
  update(
    status: "failed", 
    error: error
  )
end

#mark_sentBoolean

Mark the trace as sent



67
68
69
# File 'lib/generators/langsmithrb_rails/buffer/templates/langsmith_run_buffer.rb', line 67

def mark_sent
  update(status: "sent")
end

#send_to_langsmithBoolean

Send the trace to LangSmith



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
# File 'lib/generators/langsmithrb_rails/buffer/templates/langsmith_run_buffer.rb', line 18

def send_to_langsmith
  client = LangsmithrbRails::Client.new
  
  if run_id.present?
    # This is an update operation
    response = client.update_run(run_id, payload)
  else
    # This is a create operation
    response = client.create_run(payload)
    
    # Store the run ID if available
    if response&.dig(:status) == 200 && response&.dig(:body, "id").present?
      update(run_id: response.dig(:body, "id"))
    end
  end
  
  if response&.dig(:status) == 200
    update(status: "sent")
    true
  else
    increment!(:retry_count)
    update(
      status: "failed", 
      error: response&.dig(:error) || "Unknown error"
    )
    false
  end
rescue => e
  increment!(:retry_count)
  update(
    status: "failed", 
    error: e.message
  )
  false
end