local std = require("evalframe.std")
local M = {}
local function extract_text(result)
if type(result) == "string" then
return result
end
if type(result) ~= "table" then
return tostring(result)
end
local r = result.result
if r == nil then
if alc and alc.json_encode then
return alc.json_encode(result)
end
return tostring(result)
end
if type(r) == "string" then
return r
end
if type(r) == "table" then
for _, key in ipairs({ "answer", "summary", "output", "text" }) do
if type(r[key]) == "string" then
return r[key]
end
end
if alc and alc.json_encode then
return alc.json_encode(r)
end
end
return tostring(r)
end
function M.llm(opts)
opts = opts or {}
if type(alc) ~= "table" or type(alc.llm) ~= "function" then
error("algocline.llm provider: requires algocline VM (alc global not found)", 2)
end
return function(input)
local start = std.time()
local text = alc.llm(input)
local elapsed = (std.time() - start) * 1000
return {
text = type(text) == "string" and text or tostring(text),
model = "alc_llm",
latency_ms = elapsed,
}
end
end
setmetatable(M, {
__call = function(_, opts)
opts = opts or {}
local strategy_name = opts.strategy
if not strategy_name or type(strategy_name) ~= "string" then
error("algocline provider: 'strategy' must be a string", 2)
end
if type(alc) ~= "table" or type(alc.llm) ~= "function" then
error("algocline provider: requires algocline VM (alc global not found)", 2)
end
local strategy_opts = opts.opts or {}
return function(input)
local strategy = require(strategy_name)
if type(strategy.run) ~= "function" then
error(string.format(
"algocline provider: package '%s' has no run() function", strategy_name
), 2)
end
local run_ctx = { task = input }
for k, v in pairs(strategy_opts) do
run_ctx[k] = v
end
local start = std.time()
local ok, result = pcall(strategy.run, run_ctx)
local elapsed = (std.time() - start) * 1000
if not ok then
return {
text = "",
model = "algocline:" .. strategy_name,
error = tostring(result),
latency_ms = elapsed,
}
end
local text = extract_text(result)
return {
text = text,
model = "algocline:" .. strategy_name,
latency_ms = elapsed,
raw = result,
}
end
end,
})
return M