repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell/modules/zai.nu

Raw
# Uses the unofficial Z.AI REST API to show current usage of the Coding Plan.
# Needs the ZAI_API_KEY environment variable
export def usage [] {
	let token = $env | get --optional ZAI_API_KEY
	if ($token | is-empty) {
		error make {
			msg: "Token for Z.AI missing in environment"
			help: "Make sure to put ZAI_API_KEY in your secrets.nuon file"
		}
	}
	let headers = {
		Authorization: $'Bearer ($token)'
		Content-Type: 'application/json'
	}
	let response = http get --headers $headers 'https://api.z.ai/api/monitor/usage/quota/limit'

	if ($response.code ) != 200 {
		error make {
			msg: $response.msg
			help: "The Z.AI environment variable ZAI_API_KEY seems to be wrong."
		}
	}

	$response.data.limits
	| update nextResetTime { $in // 1000 | into datetime --timezone u --format '%s' }
}