ask-runtime 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: def95e2bb1bbf6b60a1e635e9d3194b5e4cae9f73cd271eca74e590c092f2505
4
+ data.tar.gz: e2c50b4ffce5cdd05db02ef3cfc5fb3fd5feaf8f7adb1fa9cbfdd33d18fa6d21
5
+ SHA512:
6
+ metadata.gz: b40544054745189203090642cdeed9cc05d49c72aae3c58d03c726d414bee0ae5c6b044d13ea5973f6205bd0fa4e63737b70289b71abff0a80cf1ae1e7c4ee4f
7
+ data.tar.gz: 329b405f2049eb255dbbd0b05f61b815043d61a2dd35fa16b8e42746db12f66568089d2e80e4332237325d31a24bf843f536c9067fc510d2b2ac41442bc63a96
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - Unreleased
9
+
10
+ ### Added
11
+
12
+ - `Ask::Runtime::ToolCall` — immutable value object for a tool-call request with identity, tool name, input, session/turn correlation, and lifecycle state.
13
+ - `Ask::Runtime::ToolResult` — normalized success/failure/cancelled/timeout result from tool execution, wrapping `Ask::Result`.
14
+ - `Ask::Runtime::ExecutionContext` — immutable context passed to tool execution: session, turn, caller, workspace, capabilities, cancellation, event sink, artifact store, and metadata.
15
+ - `Ask::Runtime::ToolExecutor` — adapter contract (interface) for pluggable tool execution backends.
16
+ - `Ask::Runtime::Canceller` — cooperative cancellation token with `cancelled?` / `cancel` / `on_cancel` callbacks.
17
+ - `Ask::Runtime::EventSink` — simple event emitter contract for execution lifecycle notifications.
18
+
19
+ ### Documentation
20
+ - Added RuntimeAdapter integration section to README showing how to bridge EventSink events to ask-instrumentation.
21
+ - Added the reusable `Ask::Runtime::Testing::ExecutorContract` helper for adapter conformance tests.
22
+ - Added independent CI, setup, and release workflows for the runtime repository.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kaka Ruto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,168 @@
1
+ # Ask::Runtime
2
+
3
+ Tool-call execution kernel for the [ask-rb](https://github.com/ask-rb) ecosystem.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem "ask-runtime"
9
+ ```
10
+
11
+ ## Overview
12
+
13
+ Ask::Runtime provides the foundational value objects and adapter contracts for tool-call execution in ask-rb. It defines the stable public kernel that higher-level gems (ask-agent, ask-session) build on.
14
+
15
+ ### Core Types
16
+
17
+ - **`ToolCall`** — immutable value object for a tool-call request: identity, tool name, input, session/turn correlation, and lifecycle state (`:pending`, `:running`, `:completed`, `:cancelled`, `:timed_out`).
18
+ - **`ToolResult`** — normalized execution result wrapping `Ask::Result` with `success?`, `failure?`, `cancelled?`, and `timeout?` predicates.
19
+ - **`ExecutionContext`** — immutable context for tool execution: optional session, turn, caller, workspace, capabilities, cancellation token, event sink, artifact store, and metadata.
20
+ - **`ToolExecutor`** — adapter contract (duck-type interface) for pluggable tool execution backends.
21
+
22
+ ### Runtime Events
23
+
24
+ Immutable event value objects emitted through an `EventSink` during tool execution:
25
+
26
+ | Event | When | Key Fields |
27
+ |-------|------|------------|
28
+ | `ToolStarted` | Tool begins execution | `tool_call`, `execution_context`, `timestamp` |
29
+ | `ToolCompleted` | Tool finishes successfully | `tool_call`, `tool_result`, `execution_context`, `timestamp`, `duration` |
30
+ | `ToolFailed` | Tool errors | `tool_call`, `tool_result`, `execution_context`, `timestamp`, `duration` |
31
+ | `ToolCancelled` | Cooperative cancellation | `tool_call`, `tool_result`, `execution_context`, `timestamp`, `duration` |
32
+ | `ToolTimedOut` | Execution time exceeded | `tool_call`, `tool_result`, `execution_context`, `timestamp`, `duration` |
33
+
34
+ ### EventSink
35
+
36
+ Thread-safe pub/sub emitter for lifecycle notifications:
37
+
38
+ ```ruby
39
+ sink = Ask::Runtime::EventSink.new
40
+
41
+ # Listen for events
42
+ sink.on(:tool_started) { |payload| puts payload[:event].tool_name }
43
+ sink.on(:tool_completed) { |payload| puts payload[:event].duration }
44
+
45
+ # Emit events (typically done by executors)
46
+ sink.emit(:tool_started, event: Ask::Runtime::Events::ToolStarted.new(...))
47
+ ```
48
+
49
+ #### NullSink
50
+
51
+ When no observation is needed, use a `NullSink` to avoid allocations and output:
52
+
53
+ ```ruby
54
+ sink = Ask::Runtime::EventSink.null
55
+ sink.emit(:anything) # no-op
56
+ ```
57
+
58
+ ## Example
59
+
60
+ ```ruby
61
+ require "ask-runtime"
62
+
63
+ call = Ask::Runtime::ToolCall.new(
64
+ id: "tc_abc123",
65
+ tool_name: "search",
66
+ input: { query: "ruby concurrency" },
67
+ session_id: "s_001",
68
+ turn: 3
69
+ )
70
+
71
+ result = Ask::Runtime::ToolResult.success(data: "results here")
72
+ context = Ask::Runtime::ExecutionContext.new(session_id: "s_001", turn: 3)
73
+
74
+ executor = MyToolExecutor.new
75
+ result = executor.execute(call, context: context)
76
+ ```
77
+
78
+ ### Observing Tool Execution
79
+
80
+ ```ruby
81
+ sink = Ask::Runtime::EventSink.new
82
+
83
+ # Subscribe to terminal events
84
+ sink.on(:tool_completed) do |payload|
85
+ event = payload[:event]
86
+ puts "#{event.tool_name} completed in #{event.duration}s"
87
+ puts "Result: #{event.tool_result.output}"
88
+ end
89
+
90
+ sink.on(:tool_failed) do |payload|
91
+ event = payload[:event]
92
+ puts "#{event.tool_name} failed: #{event.error}"
93
+ end
94
+
95
+ # Wire into an executor
96
+ context = Ask::Runtime::ExecutionContext.new(
97
+ session_id: "s_001",
98
+ turn: 1,
99
+ event_sink: sink
100
+ )
101
+ ```
102
+
103
+ ### Integration with ask-instrumentation
104
+
105
+ If you use [ask-instrumentation](https://github.com/ask-rb/ask-instrumentation),
106
+ you can bridge runtime events to `ActiveSupport::Notifications`:
107
+
108
+ ```ruby
109
+ require "ask/instrumentation"
110
+ require "ask/instrumentation/runtime_adapter"
111
+
112
+ sink = Ask::Instrumentation.install_runtime_sink
113
+
114
+ # All tool lifecycle events are now forwarded as Ask::Instrumentation events:
115
+ # tool.started.ask, tool.completed.ask, tool.failed.ask,
116
+ # tool.cancelled.ask, tool.timed_out.ask
117
+
118
+ context = Ask::Runtime::ExecutionContext.new(
119
+ session_id: "s_001", turn: 1, event_sink: sink
120
+ )
121
+ ```
122
+
123
+ See the [ask-instrumentation README](https://github.com/ask-rb/ask-instrumentation#runtime-adapter)
124
+ for the full payload schema and event mapping.
125
+
126
+ ## Testing an executor
127
+
128
+ Adapter gems can reuse the runtime contract assertions instead of defining
129
+ their own compatibility checklist:
130
+
131
+ ```ruby
132
+ require "ask/runtime/testing"
133
+
134
+ class MyExecutorTest < Minitest::Test
135
+ include Ask::Runtime::Testing::ExecutorContract
136
+
137
+ def test_executor_contract
138
+ assert_conforms_to_runtime_contract(
139
+ MyExecutor.new,
140
+ success_call: build_success_call,
141
+ failure_call: build_failure_call,
142
+ cancelled_call: build_cancelled_call,
143
+ context_factory: ->(event_sink:, canceller: nil) {
144
+ Ask::Runtime::ExecutionContext.new(
145
+ event_sink: event_sink, canceller: canceller
146
+ )
147
+ }
148
+ )
149
+ end
150
+ end
151
+ ```
152
+
153
+ The helper checks the shared `ToolResult` shape, non-negative duration,
154
+ pre-execution cancellation, lifecycle event ordering, terminal state, and
155
+ call correlation. Backend-specific behavior should remain covered by the
156
+ adapter's own tests.
157
+
158
+ ## Contributing
159
+
160
+ 1. Fork it
161
+ 2. Create your feature branch (`git checkout -b my-feature`)
162
+ 3. Commit your changes (`git commit -am 'Add feature'`)
163
+ 4. Push to the branch (`git push origin my-feature`)
164
+ 5. Create a Pull Request
165
+
166
+ ## License
167
+
168
+ MIT License. See [LICENSE](LICENSE) for details.
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ask
4
+ module Runtime
5
+ # Cooperative cancellation token for tool execution.
6
+ #
7
+ # A {Canceller} starts in an uncancelled state and can be cancelled at any
8
+ # time. Tools receive the canceller via {ExecutionContext} and may check
9
+ # +cancelled?+ periodically during long-running work to abort early.
10
+ #
11
+ # canceller = Ask::Runtime::Canceller.new
12
+ # canceller.on_cancel { puts "cancelled!" }
13
+ # canceller.cancel
14
+ # canceller.cancelled? # => true
15
+ #
16
+ class Canceller
17
+ def initialize
18
+ @cancelled = false
19
+ @mutex = Mutex.new
20
+ @callbacks = []
21
+ end
22
+
23
+ # @return [Boolean] true if +cancel+ has been called
24
+ def cancelled?
25
+ @mutex.synchronize { @cancelled }
26
+ end
27
+
28
+ # Request cooperative cancellation. Fires registered callbacks exactly once.
29
+ #
30
+ # Callbacks are never invoked while the internal mutex is held: the
31
+ # callback list is snapshotted under the lock and each callback is
32
+ # invoked after the lock is released.
33
+ #
34
+ # @return [self]
35
+ def cancel
36
+ callbacks_to_run = @mutex.synchronize do
37
+ return self if @cancelled
38
+
39
+ @cancelled = true
40
+ snapshot = @callbacks.dup
41
+ @callbacks.clear
42
+ snapshot
43
+ end
44
+ callbacks_to_run.each(&:call)
45
+ self
46
+ end
47
+
48
+ # Register a callback to be invoked when cancellation is requested.
49
+ # If already cancelled, the callback fires immediately (outside the
50
+ # internal mutex) to preserve immediate already-cancelled semantics
51
+ # while never invoking user code under the lock.
52
+ #
53
+ # @yield [] block to run on cancellation
54
+ # @return [self]
55
+ def on_cancel(&block)
56
+ raise ArgumentError, "block required" unless block
57
+
58
+ immediate = @mutex.synchronize do
59
+ if @cancelled
60
+ block
61
+ else
62
+ @callbacks << block
63
+ nil
64
+ end
65
+ end
66
+ immediate&.call
67
+ self
68
+ end
69
+
70
+ def inspect
71
+ "#<Ask::Runtime::Canceller cancelled=#{cancelled?}>"
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ask
4
+ module Runtime
5
+ # Simple event sink contract for tool execution lifecycle notifications.
6
+ #
7
+ # Adapters and executors may emit events through the sink so that
8
+ # higher-level systems (logging, observability, UI) can observe execution
9
+ # without coupling to specific implementations.
10
+ #
11
+ # This is the base contract. Subclass and override +emit+ for concrete
12
+ # behavior, or use a lambda/proc as a minimal sink.
13
+ #
14
+ # sink = Ask::Runtime::EventSink.new
15
+ # sink.on(:tool_execution_start) { |event| puts event }
16
+ # sink.emit(:tool_execution_start, name: "search", id: "tc_1")
17
+ #
18
+ class EventSink
19
+ # A sink that discards all events. Use when no observation is needed
20
+ # but the executor requires a non-nil sink.
21
+ #
22
+ # sink = Ask::Runtime::EventSink.null
23
+ # sink.emit(:anything) # no-op, no allocation, no output
24
+ #
25
+ class NullSink
26
+ def on(_event_type, &_block)
27
+ self
28
+ end
29
+
30
+ def emit(_event_type, **_payload)
31
+ self
32
+ end
33
+
34
+ def listening?(_event_type)
35
+ false
36
+ end
37
+
38
+ def inspect
39
+ "#<Ask::Runtime::EventSink::NullSink>"
40
+ end
41
+ end
42
+
43
+ # Return a NullSink that discards all events.
44
+ #
45
+ # @return [NullSink]
46
+ def self.null
47
+ NullSink.new
48
+ end
49
+
50
+ def initialize
51
+ @listeners = Hash.new { |h, k| h[k] = [] }
52
+ @mutex = Mutex.new
53
+ end
54
+
55
+ # Register a listener for a specific event type.
56
+ #
57
+ # @param event_type [Symbol] the event name (e.g. +:tool_execution_start+)
58
+ # @yield [payload] block to invoke when the event fires
59
+ # @return [self]
60
+ def on(event_type, &block)
61
+ raise ArgumentError, "block required" unless block
62
+
63
+ @mutex.synchronize { @listeners[event_type] << block }
64
+ self
65
+ end
66
+
67
+ # Emit an event, notifying all registered listeners for that type.
68
+ #
69
+ # Listeners are never invoked while the internal mutex is held: the
70
+ # listener list is snapshotted under the lock and each listener is
71
+ # invoked after the lock is released.
72
+ #
73
+ # @param event_type [Symbol] the event name
74
+ # @param payload [Hash] arbitrary event data
75
+ # @return [self]
76
+ def emit(event_type, **payload)
77
+ listeners_to_run = @mutex.synchronize do
78
+ @listeners[event_type].dup
79
+ end
80
+ listeners_to_run.each { |cb| cb.call(payload) }
81
+ self
82
+ end
83
+
84
+ # @return [Boolean] true if any listeners are registered for +event_type+
85
+ def listening?(event_type)
86
+ @mutex.synchronize { @listeners[event_type].any? }
87
+ end
88
+
89
+ def inspect
90
+ "#<Ask::Runtime::EventSink listeners=#{@mutex.synchronize { @listeners.keys }}>"
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../tool_call"
4
+ require_relative "../tool_result"
5
+ require_relative "../execution_context"
6
+
7
+ module Ask
8
+ module Runtime
9
+ module Events
10
+ # Immutable event emitted when a tool call is cancelled via cooperative
11
+ # cancellation (Canceller).
12
+ #
13
+ # Carries the terminal ToolCall snapshot (state: :cancelled), the
14
+ # ToolResult with outcome: :cancelled, the ExecutionContext, a monotonic
15
+ # timestamp, and the wall-clock duration in seconds.
16
+ #
17
+ # event = ToolCancelled.new(
18
+ # tool_call: finished_call, tool_result: result,
19
+ # execution_context: ctx, timestamp: Time.now, duration: 0.8
20
+ # )
21
+ # event.cancelled? #=> true
22
+ # event.reason #=> "Aborted by sibling failure"
23
+ #
24
+ class ToolCancelled
25
+ attr_reader :tool_call, :tool_result, :execution_context, :timestamp, :duration
26
+
27
+ def initialize(tool_call:, tool_result:, execution_context:, timestamp:, duration:)
28
+ raise ArgumentError, "tool_call required" unless tool_call.is_a?(Ask::Runtime::ToolCall)
29
+ raise ArgumentError, "tool_result required" unless tool_result.is_a?(Ask::Runtime::ToolResult)
30
+ raise ArgumentError, "execution_context required" unless execution_context.is_a?(Ask::Runtime::ExecutionContext)
31
+ raise ArgumentError, "timestamp required" unless timestamp.is_a?(Time)
32
+
33
+ @tool_call = tool_call
34
+ @tool_result = tool_result
35
+ @execution_context = execution_context
36
+ @timestamp = timestamp
37
+ @duration = duration
38
+ freeze
39
+ end
40
+
41
+ def tool_call_id = @tool_call.id
42
+ def tool_name = @tool_call.tool_name
43
+ def cancelled? = @tool_result.cancelled?
44
+ def reason = @tool_result.error_message
45
+
46
+ def to_h
47
+ {
48
+ tool_call: @tool_call,
49
+ tool_result: @tool_result,
50
+ execution_context: @execution_context,
51
+ timestamp: @timestamp,
52
+ duration: @duration,
53
+ tool_call_id: tool_call_id,
54
+ tool_name: tool_name,
55
+ reason: reason
56
+ }
57
+ end
58
+
59
+ def inspect
60
+ "#<ToolCancelled tool=#{tool_name.inspect} id=#{tool_call_id.inspect} reason=#{reason.inspect}>"
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../tool_call"
4
+ require_relative "../tool_result"
5
+ require_relative "../execution_context"
6
+
7
+ module Ask
8
+ module Runtime
9
+ module Events
10
+ # Immutable event emitted when a tool call completes successfully.
11
+ #
12
+ # Carries the terminal ToolCall snapshot (state: :completed), the
13
+ # ToolResult with outcome: :success, the ExecutionContext, a monotonic
14
+ # timestamp, and the wall-clock duration in seconds.
15
+ #
16
+ # event = ToolCompleted.new(
17
+ # tool_call: finished_call, tool_result: result,
18
+ # execution_context: ctx, timestamp: Time.now, duration: 1.23
19
+ # )
20
+ # event.success? #=> true
21
+ # event.duration #=> 1.23
22
+ #
23
+ class ToolCompleted
24
+ attr_reader :tool_call, :tool_result, :execution_context, :timestamp, :duration
25
+
26
+ def initialize(tool_call:, tool_result:, execution_context:, timestamp:, duration:)
27
+ raise ArgumentError, "tool_call required" unless tool_call.is_a?(Ask::Runtime::ToolCall)
28
+ raise ArgumentError, "tool_result required" unless tool_result.is_a?(Ask::Runtime::ToolResult)
29
+ raise ArgumentError, "execution_context required" unless execution_context.is_a?(Ask::Runtime::ExecutionContext)
30
+ raise ArgumentError, "timestamp required" unless timestamp.is_a?(Time)
31
+
32
+ @tool_call = tool_call
33
+ @tool_result = tool_result
34
+ @execution_context = execution_context
35
+ @timestamp = timestamp
36
+ @duration = duration
37
+ freeze
38
+ end
39
+
40
+ def tool_call_id = @tool_call.id
41
+ def tool_name = @tool_call.tool_name
42
+ def success? = @tool_result.success?
43
+
44
+ def to_h
45
+ {
46
+ tool_call: @tool_call,
47
+ tool_result: @tool_result,
48
+ execution_context: @execution_context,
49
+ timestamp: @timestamp,
50
+ duration: @duration,
51
+ tool_call_id: tool_call_id,
52
+ tool_name: tool_name
53
+ }
54
+ end
55
+
56
+ def inspect
57
+ "#<ToolCompleted tool=#{tool_name.inspect} id=#{tool_call_id.inspect} duration=#{@duration}>"
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../tool_call"
4
+ require_relative "../tool_result"
5
+ require_relative "../execution_context"
6
+
7
+ module Ask
8
+ module Runtime
9
+ module Events
10
+ # Immutable event emitted when a tool call fails with an error.
11
+ #
12
+ # Carries the terminal ToolCall snapshot (state: :failed), the
13
+ # ToolResult with outcome: :failure, the ExecutionContext, a monotonic
14
+ # timestamp, and the wall-clock duration in seconds.
15
+ #
16
+ # event = ToolFailed.new(
17
+ # tool_call: finished_call, tool_result: result,
18
+ # execution_context: ctx, timestamp: Time.now, duration: 0.5
19
+ # )
20
+ # event.failed? #=> true
21
+ # event.error #=> "file not found"
22
+ #
23
+ class ToolFailed
24
+ attr_reader :tool_call, :tool_result, :execution_context, :timestamp, :duration
25
+
26
+ def initialize(tool_call:, tool_result:, execution_context:, timestamp:, duration:)
27
+ raise ArgumentError, "tool_call required" unless tool_call.is_a?(Ask::Runtime::ToolCall)
28
+ raise ArgumentError, "tool_result required" unless tool_result.is_a?(Ask::Runtime::ToolResult)
29
+ raise ArgumentError, "execution_context required" unless execution_context.is_a?(Ask::Runtime::ExecutionContext)
30
+ raise ArgumentError, "timestamp required" unless timestamp.is_a?(Time)
31
+
32
+ @tool_call = tool_call
33
+ @tool_result = tool_result
34
+ @execution_context = execution_context
35
+ @timestamp = timestamp
36
+ @duration = duration
37
+ freeze
38
+ end
39
+
40
+ def tool_call_id = @tool_call.id
41
+ def tool_name = @tool_call.tool_name
42
+ def failed? = @tool_result.failure?
43
+ def error = @tool_result.error_message
44
+
45
+ def to_h
46
+ {
47
+ tool_call: @tool_call,
48
+ tool_result: @tool_result,
49
+ execution_context: @execution_context,
50
+ timestamp: @timestamp,
51
+ duration: @duration,
52
+ tool_call_id: tool_call_id,
53
+ tool_name: tool_name,
54
+ error: error
55
+ }
56
+ end
57
+
58
+ def inspect
59
+ "#<ToolFailed tool=#{tool_name.inspect} id=#{tool_call_id.inspect} error=#{error.inspect}>"
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../tool_call"
4
+ require_relative "../execution_context"
5
+
6
+ module Ask
7
+ module Runtime
8
+ module Events
9
+ # Immutable event emitted when a tool call begins execution.
10
+ #
11
+ # Carries a snapshot of the ToolCall (in :pending or :running state)
12
+ # and the ExecutionContext for the execution. No ToolResult is present
13
+ # since the tool has not yet completed.
14
+ #
15
+ # event = ToolStarted.new(
16
+ # tool_call: call, execution_context: ctx, timestamp: Time.now
17
+ # )
18
+ # event.tool_call_id #=> "tc_abc123"
19
+ # event.tool_name #=> "search"
20
+ #
21
+ class ToolStarted
22
+ attr_reader :tool_call, :execution_context, :timestamp
23
+
24
+ def initialize(tool_call:, execution_context:, timestamp:)
25
+ raise ArgumentError, "tool_call required" unless tool_call.is_a?(Ask::Runtime::ToolCall)
26
+ raise ArgumentError, "execution_context required" unless execution_context.is_a?(Ask::Runtime::ExecutionContext)
27
+ raise ArgumentError, "timestamp required" unless timestamp.is_a?(Time)
28
+
29
+ @tool_call = tool_call
30
+ @execution_context = execution_context
31
+ @timestamp = timestamp
32
+ freeze
33
+ end
34
+
35
+ def tool_call_id = @tool_call.id
36
+ def tool_name = @tool_call.tool_name
37
+
38
+ def to_h
39
+ {
40
+ tool_call: @tool_call,
41
+ execution_context: @execution_context,
42
+ timestamp: @timestamp,
43
+ tool_call_id: tool_call_id,
44
+ tool_name: tool_name
45
+ }
46
+ end
47
+
48
+ def inspect
49
+ "#<ToolStarted tool=#{tool_name.inspect} id=#{tool_call_id.inspect}>"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end