Class: Langsmith::Trace

Inherits:
Object
  • Object
show all
Defined in:
lib/langsmith/trace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, trace_id) ⇒ Trace

Initialize a new Trace instance

Parameters:

  • client (Langsmith::Client)

    The LangSmith client

  • trace_id (String)

    ID of the trace



11
12
13
14
15
16
# File 'lib/langsmith/trace.rb', line 11

def initialize(client, trace_id)
  @client = client
  @id = trace_id
  @runs = []
  refresh
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



5
6
7
# File 'lib/langsmith/trace.rb', line 5

def end_time
  @end_time
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/langsmith/trace.rb', line 5

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/langsmith/trace.rb', line 5

def name
  @name
end

#runsObject (readonly)

Returns the value of attribute runs.



5
6
7
# File 'lib/langsmith/trace.rb', line 5

def runs
  @runs
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



5
6
7
# File 'lib/langsmith/trace.rb', line 5

def start_time
  @start_time
end

Instance Method Details

#child_runs(parent_run_id) ⇒ Array<Langsmith::Run>

Get child runs of a specific run

Parameters:

  • parent_run_id (String)

    ID of the parent run

Returns:



45
46
47
# File 'lib/langsmith/trace.rb', line 45

def child_runs(parent_run_id)
  @runs.select { |run| run.parent_run_id == parent_run_id }
end

#completed?Boolean

Check if the trace is completed

Returns:

  • (Boolean)

    True if the trace is completed



52
53
54
# File 'lib/langsmith/trace.rb', line 52

def completed?
  !@end_time.nil?
end

#durationFloat

Get the duration of the trace in seconds

Returns:

  • (Float)

    Duration in seconds, or nil if the trace is not completed



59
60
61
62
# File 'lib/langsmith/trace.rb', line 59

def duration
  return nil unless @end_time && @start_time
  @end_time - @start_time
end

#refreshLangsmith::Trace

Refresh the trace data from the API

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/langsmith/trace.rb', line 21

def refresh
  response = @client.get("/traces/#{@id}")
  @name = response["name"]
  @start_time = response["start_time"] ? Time.parse(response["start_time"]) : nil
  @end_time = response["end_time"] ? Time.parse(response["end_time"]) : nil
  
  # Get all runs associated with this trace
  runs_response = @client.get("/runs", { trace_id: @id })
  @runs = runs_response.map { |run_data| Langsmith::Run.new(@client, run_data) }
  
  self
end

#root_runLangsmith::Run

Get the root run of this trace

Returns:



37
38
39
# File 'lib/langsmith/trace.rb', line 37

def root_run
  @runs.find { |run| run.parent_run_id.nil? }
end

#to_hierarchyHash

Get a hierarchical representation of the trace

Returns:

  • (Hash)

    Hierarchical representation of the trace



67
68
69
70
71
72
# File 'lib/langsmith/trace.rb', line 67

def to_hierarchy
  root = root_run
  return {} unless root

  build_hierarchy(root)
end