6c92f85d10
Bring the Nexus/Praxis/Hexis integration in line with MAVEN_ECOSYSTEM_ARCHITECTURE.md: - Praxis over HTTP: drop the in-process praxis.db open (praxisstore/ praxistools) and call praxisd's /api/v1/tools/* API via a new praxisClient. Honors the "no component reads another's DB" invariant (AC#12). PraxisConfig.DBPath -> URL. - Hexis confirmation gate: mutating capabilities (ReadOnly=false) now park a bound pendingHexis confirmation and require a spoken "да" before executing; read-only run immediately (AC#7, no auto attention->action). - Capability safety: >1 verb match is ambiguous -> ask instead of firing the first; ambiguous Nexus resolution asks for clarification (AC#2). - Correlation IDs on Hexis execute, recorded in the cross-service trace. - Bug: importance arrives as JSON float64 over HTTP, not int. - Tests: confirm-gate, decline, read-only, and ambiguity paths. Build: vendor/ bakes in the hexis client (replace-directed at a sibling repo outside the Docker context); Dockerfile builds from vendor and no longer `go mod download`s the unreachable replace paths. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
128 lines
2.8 KiB
Go
128 lines
2.8 KiB
Go
package humanize
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
var siPrefixTable = map[float64]string{
|
|
-30: "q", // quecto
|
|
-27: "r", // ronto
|
|
-24: "y", // yocto
|
|
-21: "z", // zepto
|
|
-18: "a", // atto
|
|
-15: "f", // femto
|
|
-12: "p", // pico
|
|
-9: "n", // nano
|
|
-6: "µ", // micro
|
|
-3: "m", // milli
|
|
0: "",
|
|
3: "k", // kilo
|
|
6: "M", // mega
|
|
9: "G", // giga
|
|
12: "T", // tera
|
|
15: "P", // peta
|
|
18: "E", // exa
|
|
21: "Z", // zetta
|
|
24: "Y", // yotta
|
|
27: "R", // ronna
|
|
30: "Q", // quetta
|
|
}
|
|
|
|
var revSIPrefixTable = revfmap(siPrefixTable)
|
|
|
|
// revfmap reverses the map and precomputes the power multiplier
|
|
func revfmap(in map[float64]string) map[string]float64 {
|
|
rv := map[string]float64{}
|
|
for k, v := range in {
|
|
rv[v] = math.Pow(10, k)
|
|
}
|
|
return rv
|
|
}
|
|
|
|
var riParseRegex *regexp.Regexp
|
|
|
|
func init() {
|
|
ri := `^([\-0-9.]+)\s?([`
|
|
for _, v := range siPrefixTable {
|
|
ri += v
|
|
}
|
|
ri += `]?)(.*)`
|
|
|
|
riParseRegex = regexp.MustCompile(ri)
|
|
}
|
|
|
|
// ComputeSI finds the most appropriate SI prefix for the given number
|
|
// and returns the prefix along with the value adjusted to be within
|
|
// that prefix.
|
|
//
|
|
// See also: SI, ParseSI.
|
|
//
|
|
// e.g. ComputeSI(2.2345e-12) -> (2.2345, "p")
|
|
func ComputeSI(input float64) (float64, string) {
|
|
if input == 0 {
|
|
return 0, ""
|
|
}
|
|
mag := math.Abs(input)
|
|
exponent := math.Floor(logn(mag, 10))
|
|
exponent = math.Floor(exponent/3) * 3
|
|
|
|
value := mag / math.Pow(10, exponent)
|
|
|
|
// Handle special case where value is exactly 1000.0
|
|
// Should return 1 M instead of 1000 k
|
|
if value == 1000.0 {
|
|
exponent += 3
|
|
value = mag / math.Pow(10, exponent)
|
|
}
|
|
|
|
value = math.Copysign(value, input)
|
|
|
|
prefix := siPrefixTable[exponent]
|
|
return value, prefix
|
|
}
|
|
|
|
// SI returns a string with default formatting.
|
|
//
|
|
// SI uses Ftoa to format float value, removing trailing zeros.
|
|
//
|
|
// See also: ComputeSI, ParseSI.
|
|
//
|
|
// e.g. SI(1000000, "B") -> 1 MB
|
|
// e.g. SI(2.2345e-12, "F") -> 2.2345 pF
|
|
func SI(input float64, unit string) string {
|
|
value, prefix := ComputeSI(input)
|
|
return Ftoa(value) + " " + prefix + unit
|
|
}
|
|
|
|
// SIWithDigits works like SI but limits the resulting string to the
|
|
// given number of decimal places.
|
|
//
|
|
// e.g. SIWithDigits(1000000, 0, "B") -> 1 MB
|
|
// e.g. SIWithDigits(2.2345e-12, 2, "F") -> 2.23 pF
|
|
func SIWithDigits(input float64, decimals int, unit string) string {
|
|
value, prefix := ComputeSI(input)
|
|
return FtoaWithDigits(value, decimals) + " " + prefix + unit
|
|
}
|
|
|
|
var errInvalid = errors.New("invalid input")
|
|
|
|
// ParseSI parses an SI string back into the number and unit.
|
|
//
|
|
// See also: SI, ComputeSI.
|
|
//
|
|
// e.g. ParseSI("2.2345 pF") -> (2.2345e-12, "F", nil)
|
|
func ParseSI(input string) (float64, string, error) {
|
|
found := riParseRegex.FindStringSubmatch(input)
|
|
if len(found) != 4 {
|
|
return 0, "", errInvalid
|
|
}
|
|
mag := revSIPrefixTable[found[2]]
|
|
unit := found[3]
|
|
|
|
base, err := strconv.ParseFloat(found[1], 64)
|
|
return base * mag, unit, err
|
|
}
|