12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/ask/instrumentation/tool.rb', line 12
def instrument(name:, arguments:, tool_call_id:, metadata: {}, &block)
arg_size = arguments.to_s.length
payload = {
name: name,
arguments: arguments,
argument_size: arg_size,
tool_call_id: tool_call_id
}.merge(metadata)
Ask::Instrumentation.instrument("tool_call.ask", payload)
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
begin
result = block.call
duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(1)
result_size = result.to_s.length
result_payload = {
name: name,
duration_ms: duration_ms,
result_size: result_size,
success: true,
tool_call_id: tool_call_id
}.merge(metadata)
Ask::Instrumentation.instrument("tool_result.ask", result_payload)
write_trace_log(name: name, duration_ms: duration_ms, success: true,
argument_size: arg_size, result_size: result_size,
tool_call_id: tool_call_id)
result
rescue => e
duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(1)
error_payload = {
name: name,
duration_ms: duration_ms,
error: e.class.name,
error_message: e.message,
success: false,
tool_call_id: tool_call_id
}.merge(metadata)
Ask::Instrumentation.instrument("tool_result.ask", error_payload)
write_trace_log(name: name, duration_ms: duration_ms, success: false,
error: "#{e.class.name}: #{e.message}",
tool_call_id: tool_call_id)
raise
end
end
|