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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/tidewave/tools/project_eval.rb', line 32
def call(code:, arguments: [], timeout: 30_000, json: false)
original_stdout = $stdout
original_stderr = $stderr
stdout_capture = StringIO.new
stderr_capture = StringIO.new
$stdout = stdout_capture
$stderr = stderr_capture
begin
timeout_seconds = timeout / 1000.0
success, result = begin
Timeout.timeout(timeout_seconds) do
[ true, eval(code, eval_binding(arguments)) ]
end
rescue Timeout::Error
[ false, "Timeout::Error: Evaluation timed out after #{timeout} milliseconds." ]
rescue => e
[ false, e.full_message ]
end
stdout = stdout_capture.string
stderr = stderr_capture.string
if json
JSON.generate({
result: result,
success: success,
stdout: stdout,
stderr: stderr
})
elsif stdout.empty? && stderr.empty?
result.to_s
else
" STDOUT:\n\n \#{stdout}\n\n STDERR:\n\n \#{stderr}\n\n Result:\n\n \#{result}\n OUTPUT\n end\n ensure\n $stdout = original_stdout\n $stderr = original_stderr\n end\nend\n"
|