Class: PromptObjects::Execution::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/execution/context.rb

Overview

Immutable identity and dependencies passed to one run or tool call.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, workspace_session_id:, thread_id:, run_id:, target_po:, source_type:, source_name: nil, caller_po: nil, current_capability: nil, cancellation_token: CancellationToken.new, trace_id: SecureRandom.uuid, span_id: SecureRandom.uuid, deadline: nil, resource_lock_manager: nil, event_publisher: nil, tui_mode: false) ⇒ Context

Returns a new instance of Context.



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
# File 'lib/prompt_objects/execution/context.rb', line 43

def initialize(env:, workspace_session_id:, thread_id:, run_id:, target_po:,
               source_type:, source_name: nil, caller_po: nil,
               current_capability: nil, cancellation_token: CancellationToken.new,
               trace_id: SecureRandom.uuid, span_id: SecureRandom.uuid,
               deadline: nil, resource_lock_manager: nil, event_publisher: nil,
               tui_mode: false)
  @env = env
  @bus = env.bus
  @human_queue = env.human_queue
  @workspace_session_id = workspace_session_id
  @thread_id = thread_id
  @run_id = run_id
  @caller_po = caller_po
  @target_po = target_po
  @source_type = source_type
  @source_name = source_name
  @current_capability = current_capability
  @calling_po = caller_po
  @cancellation_token = cancellation_token
  @trace_id = trace_id
  @span_id = span_id
  @deadline = deadline
  @resource_lock_manager = resource_lock_manager
  @event_publisher = event_publisher
  @tui_mode = tui_mode
  freeze
end

Instance Attribute Details

#busObject (readonly)

Returns the value of attribute bus.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def bus
  @bus
end

#caller_poObject (readonly)

Returns the value of attribute caller_po.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def caller_po
  @caller_po
end

#calling_poObject (readonly)

Returns the value of attribute calling_po.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def calling_po
  @calling_po
end

#cancellation_tokenObject (readonly)

Returns the value of attribute cancellation_token.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def cancellation_token
  @cancellation_token
end

#current_capabilityObject (readonly)

Returns the value of attribute current_capability.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def current_capability
  @current_capability
end

#deadlineObject (readonly)

Returns the value of attribute deadline.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def deadline
  @deadline
end

#envObject (readonly)

Returns the value of attribute env.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def env
  @env
end

#event_publisherObject (readonly)

Returns the value of attribute event_publisher.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def event_publisher
  @event_publisher
end

#human_queueObject (readonly)

Returns the value of attribute human_queue.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def human_queue
  @human_queue
end

#resource_lock_managerObject (readonly)

Returns the value of attribute resource_lock_manager.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def resource_lock_manager
  @resource_lock_manager
end

#run_idObject (readonly)

Returns the value of attribute run_id.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def run_id
  @run_id
end

#source_nameObject (readonly)

Returns the value of attribute source_name.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def source_name
  @source_name
end

#source_typeObject (readonly)

Returns the value of attribute source_type.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def source_type
  @source_type
end

#span_idObject (readonly)

Returns the value of attribute span_id.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def span_id
  @span_id
end

#target_poObject (readonly)

Returns the value of attribute target_po.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def target_po
  @target_po
end

#thread_idObject (readonly)

Returns the value of attribute thread_id.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def thread_id
  @thread_id
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def trace_id
  @trace_id
end

#tui_modeObject (readonly)

Returns the value of attribute tui_mode.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def tui_mode
  @tui_mode
end

#workspace_session_idObject (readonly)

Returns the value of attribute workspace_session_id.



37
38
39
# File 'lib/prompt_objects/execution/context.rb', line 37

def workspace_session_id
  @workspace_session_id
end

Instance Method Details

#check_cancelled!Object

Raises:



98
99
100
101
102
103
# File 'lib/prompt_objects/execution/context.rb', line 98

def check_cancelled!
  cancellation_token.check!
  return true unless deadline && Time.now >= deadline

  raise CancelledError, "deadline exceeded"
end

#env_data_scopeObject



94
95
96
# File 'lib/prompt_objects/execution/context.rb', line 94

def env_data_scope
  workspace_session_id
end

#for_child_run(run_id:, thread_id:, target_po:, source_name: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/prompt_objects/execution/context.rb', line 80

def for_child_run(run_id:, thread_id:, target_po:, source_name: nil)
  self.class.new(
    **shared_attributes,
    run_id: run_id,
    thread_id: thread_id,
    target_po: target_po,
    caller_po: self.target_po,
    source_type: "delegation",
    source_name: source_name || self.target_po,
    current_capability: nil,
    span_id: SecureRandom.uuid
  )
end

#for_tool_call(tool_call) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/prompt_objects/execution/context.rb', line 71

def for_tool_call(tool_call)
  self.class.new(
    **shared_attributes,
    caller_po: target_po,
    current_capability: tool_call.name,
    span_id: SecureRandom.uuid
  )
end

#log_message(to:, message:) ⇒ Object



105
106
107
# File 'lib/prompt_objects/execution/context.rb', line 105

def log_message(to:, message:)
  bus.publish(from: current_capability || source_name || source_type, to: to, message: message)
end

#log_response(from:, message:) ⇒ Object



109
110
111
# File 'lib/prompt_objects/execution/context.rb', line 109

def log_response(from:, message:)
  bus.publish(from: from, to: current_capability || target_po, message: message)
end