104 lines
4 KiB
Text
104 lines
4 KiB
Text
|
|
# provisioning-daemon HTTP client — default http://127.0.0.1:9014
|
||
|
|
# All daemon API calls go through this module.
|
||
|
|
# Port is read from PROVISIONING_DAEMON_BIND env or falls back to 9014.
|
||
|
|
|
||
|
|
const DAEMON_DEFAULT_PORT = 9014
|
||
|
|
|
||
|
|
# Resolve the daemon base URL from environment or default.
|
||
|
|
def daemon-url []: nothing -> string {
|
||
|
|
let port = (
|
||
|
|
$env.PROVISIONING_DAEMON_BIND?
|
||
|
|
| default ""
|
||
|
|
| if ($in | str contains ":") { $in | split row ":" | last } else { $DAEMON_DEFAULT_PORT | into string }
|
||
|
|
)
|
||
|
|
$"http://127.0.0.1:($port)"
|
||
|
|
}
|
||
|
|
|
||
|
|
# Return true when the daemon's /health endpoint responds with status "ok".
|
||
|
|
export def daemon-health []: nothing -> bool {
|
||
|
|
let r = (do { http head --allow-errors $"(daemon-url)/health" } | complete)
|
||
|
|
$r.exit_code == 0
|
||
|
|
}
|
||
|
|
|
||
|
|
# GET /health — returns full health record or null on failure.
|
||
|
|
export def daemon-health-full []: nothing -> record {
|
||
|
|
let r = (do { http get $"(daemon-url)/health" } | complete)
|
||
|
|
if $r.exit_code != 0 { return {} }
|
||
|
|
$r.stdout | from json
|
||
|
|
}
|
||
|
|
|
||
|
|
# GET /api/v1/tools — list all registered tools (no auth required).
|
||
|
|
export def daemon-list-tools []: nothing -> list<record> {
|
||
|
|
let r = (do { http get $"(daemon-url)/api/v1/tools" } | complete)
|
||
|
|
if $r.exit_code != 0 { error make {msg: $"daemon-list-tools failed: ($r.stderr)"} }
|
||
|
|
($r.stdout | from json).tools
|
||
|
|
}
|
||
|
|
|
||
|
|
# POST /api/v1/tools/{name} — invoke a tool with JSON params (auth required).
|
||
|
|
# jwt_token: Bearer token string (pass "" in solo/open mode)
|
||
|
|
export def daemon-invoke [
|
||
|
|
name: string, # tool name
|
||
|
|
params: record, # tool params
|
||
|
|
--jwt: string = "", # Bearer token (omit in solo mode)
|
||
|
|
--session: string = "" # optional session_id
|
||
|
|
]: nothing -> record {
|
||
|
|
let payload = {
|
||
|
|
params: $params
|
||
|
|
session_id: (if ($session | is-empty) { null } else { $session })
|
||
|
|
}
|
||
|
|
let headers = (
|
||
|
|
if ($jwt | is-empty) {
|
||
|
|
{}
|
||
|
|
} else {
|
||
|
|
{Authorization: $"Bearer ($jwt)"}
|
||
|
|
}
|
||
|
|
)
|
||
|
|
let r = (do {
|
||
|
|
http post --content-type application/json --headers $headers $"(daemon-url)/api/v1/tools/($name)" ($payload | to json)
|
||
|
|
} | complete)
|
||
|
|
if $r.exit_code != 0 { error make {msg: $"daemon-invoke ($name) failed: ($r.stderr)"} }
|
||
|
|
$r.stdout | from json
|
||
|
|
}
|
||
|
|
|
||
|
|
# GET /api/v1/state/stats — aggregate invocation stats (auth required).
|
||
|
|
export def daemon-stats [--jwt: string = ""]: nothing -> record {
|
||
|
|
daemon-get-auth /api/v1/state/stats $jwt
|
||
|
|
}
|
||
|
|
|
||
|
|
# GET /api/v1/state/recent — last 50 invocations (auth required).
|
||
|
|
export def daemon-recent [--jwt: string = ""]: nothing -> list<record> {
|
||
|
|
(daemon-get-auth /api/v1/state/recent $jwt).invocations
|
||
|
|
}
|
||
|
|
|
||
|
|
# GET /api/v1/state/sessions — active sessions (auth required).
|
||
|
|
export def daemon-sessions [--jwt: string = ""]: nothing -> list<record> {
|
||
|
|
(daemon-get-auth /api/v1/state/sessions $jwt).sessions
|
||
|
|
}
|
||
|
|
|
||
|
|
# GET /api/v1/ontology/templates — list loaded Tera template names (auth required).
|
||
|
|
export def daemon-ontology-templates [--jwt: string = ""]: nothing -> list<string> {
|
||
|
|
(daemon-get-auth /api/v1/ontology/templates $jwt).templates
|
||
|
|
}
|
||
|
|
|
||
|
|
# GET /api/v1/ontology/render/{template} — render a named Tera template (auth required).
|
||
|
|
export def daemon-ontology-render [template: string, --jwt: string = ""]: nothing -> string {
|
||
|
|
let headers = (
|
||
|
|
if ($jwt | is-empty) { {} } else { {Authorization: $"Bearer ($jwt)"} }
|
||
|
|
)
|
||
|
|
let r = (do {
|
||
|
|
http get --headers $headers $"(daemon-url)/api/v1/ontology/render/($template)"
|
||
|
|
} | complete)
|
||
|
|
if $r.exit_code != 0 { error make {msg: $"daemon-ontology-render ($template) failed: ($r.stderr)"} }
|
||
|
|
$r.stdout
|
||
|
|
}
|
||
|
|
|
||
|
|
# Internal: authenticated GET helper.
|
||
|
|
def daemon-get-auth [path: string, jwt: string]: nothing -> record {
|
||
|
|
let headers = (if ($jwt | is-empty) { {} } else { {Authorization: $"Bearer ($jwt)"} })
|
||
|
|
let r = (do {
|
||
|
|
http get --headers $headers $"(daemon-url)($path)"
|
||
|
|
} | complete)
|
||
|
|
if $r.exit_code != 0 { error make {msg: $"daemon GET ($path) failed: ($r.stderr)"} }
|
||
|
|
$r.stdout | from json
|
||
|
|
}
|