local env_mod = require("evalframe.swarm.env")
local actions_mod = require("evalframe.swarm.actions")
local config_mod = require("evalframe.swarm.config")
local trace_mod = require("evalframe.swarm.trace")
local std = require("evalframe.std")
local M = {}
function M.build(runner, opts)
if type(runner) ~= "function" then
error("sw.provider: runner must be a function", 2)
end
if type(opts) ~= "table" then
error("sw.provider: opts must be a table", 2)
end
if not env_mod.is_env(opts.env) then
error("sw.provider: env is required (use sw.env)", 2)
end
if not actions_mod.is_action_space(opts.actions) then
error("sw.provider: actions is required (use sw.actions)", 2)
end
if not config_mod.is_swarm_config(opts.swarm) then
error("sw.provider: swarm is required (use sw.swarm)", 2)
end
return function(input)
local config = {
input = input,
env = opts.env,
actions = opts.actions,
swarm = opts.swarm,
}
local start = std.time()
local ok, raw = pcall(runner, config)
local elapsed = (std.time() - start) * 1000
if not ok then
return {
text = "",
latency_ms = elapsed,
error = tostring(raw),
}
end
local trace_input = raw
if raw.latency_ms == nil then
trace_input = {}
for k, v in pairs(raw) do trace_input[k] = v end
trace_input.latency_ms = elapsed
end
return trace_mod.build(trace_input)
end
end
return M