diff --git a/internal/library/bili.go b/internal/library/bili.go index b30b35dcf..3cc9634eb 100644 --- a/internal/library/bili.go +++ b/internal/library/bili.go @@ -212,6 +212,22 @@ func biliOn(status string) bool { } // ReadBili says whether bili is installed and which agents have its plugin. +// biliVersion is the version in `bili --version`'s output. Its output is +// read with stderr, where bili writes warnings first (a config key it +// doesn't know: "2026-11-09T23:44:06.211 [warn] [v=0.1.213] ..."), so +// the version is the first word of the line that starts with one. +func biliVersion(out string) string { + for _, l := range strings.Split(out, "\n") { + w, _, _ := strings.Cut(strings.TrimSpace(l), " ") + if biliVersionRe.MatchString(w) { + return w + } + } + return "" +} + +var biliVersionRe = lazyre.New(`^v?\d+\.\d+\.\d+([-+][0-9A-Za-z.+-]*)?$`) + func ReadBili() *BiliView { return readBili(detected()) } @@ -225,7 +241,7 @@ func readBili(agents []*agent.Agent) *BiliView { } } else { if out, err := biliRun(v.Path, 30*time.Second, "--version"); err == nil { - v.Version, _, _ = strings.Cut(strings.TrimSpace(out), " ") + v.Version = biliVersion(out) } v.Saved = readBiliSaved() out, err := biliRun(v.Path, 30*time.Second, "plugin", "list") diff --git a/internal/library/bili_test.go b/internal/library/bili_test.go index 2c561260a..ece5b537e 100644 --- a/internal/library/bili_test.go +++ b/internal/library/bili_test.go @@ -541,3 +541,22 @@ func TestBiliZedThroughPrefix(t *testing.T) { t.Fatalf("served %d, ran %v", served, f.runs) } } + +// TestBiliVersionPastItsWarnings: `bili --version` is read with stderr, +// and bili 0.1.213 on mote wrote a config warning before the version; the +// card said "2026-11-09T23:44:06.211" for the version. +func TestBiliVersionPastItsWarnings(t *testing.T) { + for out, want := range map[string]string{ + "0.1.213 (133f3525c)": "0.1.213", + "0.1.213 (133f3525c)\n": "0.1.213", + "2026-11-09T23:44:06.211 [warn] [v=0.1.213] [acp-config] ignoring unknown top-level config key(s): models\n0.1.213 (133f3525c)\n": "0.1.213", + "0.1.214-beta.1": "0.1.214-beta.1", + "[warn] something\r\n0.2.0 (abc)\r\n": "0.2.0", + "command not found": "", + "": "", + } { + if got := biliVersion(out); got != want { + t.Errorf("biliVersion(%q) = %q, want %q", out, got, want) + } + } +}