Module: Phronomy::Agent::AsyncEventApi Private

Included in:
Base
Defined in:
lib/phronomy/agent/async_event_api.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Symmetric Agent async event contract layered onto Agent::Base.

invoke_async and stream_async share lifecycle/tool events. stream_async additionally emits :token events. The returned Task remains a normal Task and is settled after the terminal event listener returns.

Instance Method Summary collapse

Instance Method Details

#invoke(input, messages: [], thread_id: nil, config: {}, invocation_context: nil, on_event: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
16
17
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
# File 'lib/phronomy/agent/async_event_api.rb', line 13

def invoke(
  input,
  messages: [],
  thread_id: nil,
  config: {},
  invocation_context: nil,
  on_event: nil
)
  if invocation_context
    thread_id, config = _apply_invocation_context(
      thread_id,
      config,
      invocation_context
    )
  end
  _check_scheduler_reentrancy(:invoke, :invoke_async)

  trace(
    "agent.invoke",
    input: input,
    **_build_caller_meta(config)
  ) do |_span|
    result = invoke_async(
      input,
      messages: messages,
      thread_id: thread_id,
      config: config,
      on_event: on_event
    ).wait_result
    [result, result[:usage]]
  end
end

#invoke_async(input, messages: [], thread_id: nil, config: {}, invocation_context: nil, on_tool_approval_required: nil, on_event: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/phronomy/agent/async_event_api.rb', line 46

def invoke_async(
  input,
  messages: [],
  thread_id: nil,
  config: {},
  invocation_context: nil,
  on_tool_approval_required: nil,
  on_event: nil
)
  if invocation_context
    thread_id, config = _apply_invocation_context(
      thread_id,
      config,
      invocation_context
    )
  end

  result_task = Phronomy::Task.deferred(
    name:
      "agent-#{(self.class.name || "anonymous").downcase}-async"
  )
  approval_snapshot = _approval_configuration_snapshot(
    on_tool_approval_required
  )
  _start_invocation(
    result_task,
    input,
    messages: messages,
    thread_id: thread_id,
    config: config,
    approval_snapshot: approval_snapshot,
    mode: :invoke,
    on_event: on_event
  )
  result_task
end

#stream(input, messages: [], thread_id: nil, config: {}, invocation_context: nil, on_tool_approval_required: nil, on_event: nil, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/phronomy/agent/async_event_api.rb', line 128

def stream(
  input,
  messages: [],
  thread_id: nil,
  config: {},
  invocation_context: nil,
  on_tool_approval_required: nil,
  on_event: nil,
  &block
)
  listener = resolve_event_listener(on_event, block)
  unless listener
    raise ArgumentError,
      "stream requires on_event: or a block"
  end

  if invocation_context
    thread_id, config = _apply_invocation_context(
      thread_id,
      config,
      invocation_context
    )
  end
  _check_scheduler_reentrancy(:stream, :stream_async)

  trace(
    "agent.stream",
    input: input,
    **_build_caller_meta(config)
  ) do |_span|
    result = stream_async(
      input,
      messages: messages,
      thread_id: thread_id,
      config: config,
      on_tool_approval_required:
        on_tool_approval_required,
      on_event: listener
    ).wait_result
    [result, result[:usage]]
  end
end

#stream_async(input, messages: [], thread_id: nil, config: {}, invocation_context: nil, on_tool_approval_required: nil, on_event: nil, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/phronomy/agent/async_event_api.rb', line 83

def stream_async(
  input,
  messages: [],
  thread_id: nil,
  config: {},
  invocation_context: nil,
  on_tool_approval_required: nil,
  on_event: nil,
  &block
)
  listener = resolve_event_listener(on_event, block)
  unless listener
    raise ArgumentError,
      "stream_async requires on_event: or a block"
  end

  if invocation_context
    thread_id, config = _apply_invocation_context(
      thread_id,
      config,
      invocation_context
    )
  end

  result_task = Phronomy::Task.deferred(
    name:
      "agent-#{(self.class.name || "anonymous").downcase}" \
      "-stream-async"
  )
  approval_snapshot = _approval_configuration_snapshot(
    on_tool_approval_required
  )
  _start_invocation(
    result_task,
    input,
    messages: messages,
    thread_id: thread_id,
    config: config,
    approval_snapshot: approval_snapshot,
    mode: :stream,
    on_event: listener
  )
  result_task
end