-- SPDX-FileCopyrightText: © 2026 Vladimir Zorin -- SPDX-License-Identifier: LicenseRef-OWL-1.0-or-later -- Licensed under OWL v1.0+. See LICENSE. local router = require("agent_smith.command.router") local function normalize_result(result, fallback, route) if type(result) ~= "table" then return { ok = false, code = fallback.code, message = fallback.message, data = fallback.data, route = route, } end if result.ok == true then if type(result.code) ~= "string" then result.code = "ok" end if type(result.message) ~= "string" then result.message = "" end return result end if type(result.code) ~= "string" then result.code = fallback.code end if type(result.message) ~= "string" then result.message = fallback.message end if result.route == nil then result.route = route end return result end local M = {} function M.new(options) options = options or {} local builtin = require("agent_smith.command.builtin") local service = { name = "command", kind = "service", } function service.execute(line, ctx) local parsed = router.parse(line) if parsed == nil then return nil end if parsed.ok == false then return normalize_result(parsed, { code = "invalid_args", message = "invalid command", }, parsed) end local result, err = builtin.execute(parsed, ctx) if not result then return { ok = false, code = "command_failed", message = "command execution failed", data = { error = tostring(err), }, route = parsed, } end return normalize_result( result, { code = "command_failed", message = "command handler returned invalid result" }, parsed ) end return service end return M