diff --git a/internal/i18n/en_lxask.go b/internal/i18n/en_lxask.go index 6614bd6..fd3d514 100644 --- a/internal/i18n/en_lxask.go +++ b/internal/i18n/en_lxask.go @@ -101,7 +101,8 @@ func init() { "Prüfsumme von %s stimmt nicht": "Checksum of %s does not match", "bereit · %d MB": "ready · %d MB", "ca. 1 GB, nicht installiert": "about 1 GB, not installed", - "für %s/%s gibt es keinen fertigen llama-server; installiere llama.cpp selbst (llama-server im PATH)": "there is no ready-made llama-server for %s/%s; install llama.cpp yourself (llama-server in PATH)", + "für %s/%s gibt es keinen fertigen llama-server; installiere llama.cpp selbst (llama-server im PATH)": "there is no ready-made llama-server for %s/%s; install llama.cpp yourself (llama-server in PATH)", + "kein fertiger llama-server für den Mac gefunden; im Terminal „brew install llama.cpp“ ausführen, dann erneut herunterladen": "no ready-made llama-server for the Mac found; run “brew install llama.cpp” in a terminal, then download again", "im lxask-Release fehlt die Modelldatei": "the lxask release has no model file", "llama-server (llama.cpp)": "llama-server (llama.cpp)", "llama-server antwortet nicht (Zeitüberschreitung beim Laden des Modells)": "llama-server does not answer (timeout while loading the model)", diff --git a/internal/offlineai/download.go b/internal/offlineai/download.go index 8871761..68396b8 100644 --- a/internal/offlineai/download.go +++ b/internal/offlineai/download.go @@ -17,6 +17,7 @@ import ( "path/filepath" "regexp" "runtime" + "slices" "strings" "time" @@ -25,8 +26,9 @@ import ( // Quellen (je die neueste Veröffentlichung). var ( - ServerRelease = "https://api.github.com/repos/ggml-org/llama.cpp/releases/latest" - ModelRelease = "https://api.github.com/repos/arxhr007/lxask/releases/latest" + ServerRelease = "https://api.github.com/repos/ggml-org/llama.cpp/releases/latest" + ServerReleases = "https://api.github.com/repos/ggml-org/llama.cpp/releases?per_page=10" + ModelRelease = "https://api.github.com/repos/arxhr007/lxask/releases/latest" ) // Progress: Stand des Herunterladens. @@ -71,42 +73,53 @@ func getJSON(ctx context.Context, url string, v any) error { // Ausschlüsse: Builds für Grafikkarten, wir nehmen den CPU-Build. var gpuBuild = regexp.MustCompile(`(?i)cuda|vulkan|hip|sycl|opencl|kompute|musa|rocm|cann|openvino|radeon|kleidiai|xcframework`) -// PickServer wählt den llama.cpp-Build für dieses System. +// PickServer wählt den llama.cpp-Build für dieses System. Der Name wird in +// Teile zerlegt (llama-b7000-bin-macos-arm64.zip → llama, b7000, bin, macos, +// arm64), damit auch umbenannte Pakete (ohne „bin“, .tgz, osx/darwin) passen. func PickServer(goos, goarch string, assets []asset) (asset, bool) { - osTok := map[string][]string{"windows": {"win"}, "darwin": {"macos", "mac"}, "linux": {"ubuntu", "linux"}}[goos] + osTok := map[string][]string{"windows": {"win", "windows"}, "darwin": {"macos", "mac", "osx", "darwin", "apple"}, "linux": {"ubuntu", "linux"}}[goos] archTok := map[string][]string{"amd64": {"x64", "x86_64", "amd64"}, "arm64": {"arm64", "aarch64"}}[goarch] if osTok == nil || archTok == nil { return asset{}, false } + allArch := []string{"x64", "x86_64", "amd64", "arm64", "aarch64", "x86", "i686", "armv7", "riscv64", "s390x"} best, bestScore := asset{}, -1 for _, a := range assets { n := strings.ToLower(a.Name) - if !strings.Contains(n, "-bin-") || gpuBuild.MatchString(n) || !(strings.HasSuffix(n, ".zip") || strings.HasSuffix(n, ".tar.gz")) { + isZip, isTar := strings.HasSuffix(n, ".zip"), strings.HasSuffix(n, ".tar.gz") || strings.HasSuffix(n, ".tgz") + if !strings.HasPrefix(n, "llama") || gpuBuild.MatchString(n) || !(isZip || isTar) { continue } - score := -1 - for i, o := range osTok { - if strings.Contains(n, "-bin-"+o+"-") { - score = 10 - i - break + parts := strings.FieldsFunc(strings.TrimSuffix(strings.TrimSuffix(strings.TrimSuffix(n, ".zip"), ".gz"), ".tar"), + func(r rune) bool { return r == '-' || r == '_' || r == '.' }) + has := func(toks []string) int { + for i, t := range toks { + if slices.Contains(parts, t) { + return i + } } + return -1 } - if score < 0 { + oi := has(osTok) + if oi < 0 { continue } - archOK := false - for _, ar := range archTok { - if strings.Contains(n, "-"+ar+".") || strings.Contains(n, "-"+ar+"-") { - archOK = true - } - } + // x86_64 zerfällt in x86 und 64: als Ganzes prüfen + archOK := has(archTok) >= 0 || (goarch == "amd64" && strings.Contains(n, "x86_64")) if !archOK { - continue + // ohne Architektur im Namen nur auf dem Mac (Universal-Build) + if goos != "darwin" || has(allArch) >= 0 || strings.Contains(n, "x86_64") { + continue + } } - if strings.Contains(n, "-cpu-") { + score := 10 - oi + if archOK { + score += 3 + } + if slices.Contains(parts, "cpu") { score += 5 } - if strings.HasSuffix(n, ".zip") == (goos == "windows") { + if isZip == (goos == "windows") { score++ } if score > bestScore { @@ -255,6 +268,20 @@ func (e *Engine) downloadServer(ctx context.Context, progress func(Progress)) er return fmt.Errorf(i18n.T("llama.cpp-Veröffentlichung nicht erreichbar: %w"), err) } a, ok := PickServer(runtime.GOOS, runtime.GOARCH, rel.Assets) + if !ok { + // Der neueste Build hat manchmal (noch) nicht alle Pakete: die letzten zehn durchsehen. + var rels []release + if err := getJSON(ctx, ServerReleases, &rels); err == nil { + for _, r := range rels { + if a, ok = PickServer(runtime.GOOS, runtime.GOARCH, r.Assets); ok { + break + } + } + } + } + if !ok && runtime.GOOS == "darwin" { + return errors.New(i18n.T("kein fertiger llama-server für den Mac gefunden; im Terminal „brew install llama.cpp“ ausführen, dann erneut herunterladen")) + } if !ok { return fmt.Errorf(i18n.T("für %s/%s gibt es keinen fertigen llama-server; installiere llama.cpp selbst (llama-server im PATH)"), runtime.GOOS, runtime.GOARCH) } diff --git a/internal/offlineai/offlineai.go b/internal/offlineai/offlineai.go index 2019149..18507b1 100644 --- a/internal/offlineai/offlineai.go +++ b/internal/offlineai/offlineai.go @@ -72,9 +72,18 @@ func (e *Engine) ServerPath() string { if p, err := exec.LookPath(exeName("llama-server")); err == nil { return p } + // Aus dem Finder gestartet fehlt Homebrew oft im PATH. + for _, d := range brewDirs { + if p := filepath.Join(d, exeName("llama-server")); fileExists(p) { + return p + } + } return "" } +// brewDirs: wo Homebrew (Mac, Linux) Programme ablegt. +var brewDirs = []string{"/opt/homebrew/bin", "/usr/local/bin", "/home/linuxbrew/.linuxbrew/bin"} + func fileExists(p string) bool { st, err := os.Stat(p) return err == nil && !st.IsDir() diff --git a/internal/offlineai/offlineai_test.go b/internal/offlineai/offlineai_test.go index 581b9f8..40021e4 100644 --- a/internal/offlineai/offlineai_test.go +++ b/internal/offlineai/offlineai_test.go @@ -39,6 +39,24 @@ func TestPickServer(t *testing.T) { if _, ok := PickServer("linux", "riscv64", as); ok { t.Error("riscv64 should have no build") } + // umbenannte Pakete + for k, n := range map[string]string{ + "darwin/arm64": "llama-b8000-macos-arm64.tar.gz", + "darwin/amd64": "llama-b8000-bin-osx-x86_64.tgz", + "linux/amd64": "llama-b8000-bin-linux-x86_64.tar.gz", + "windows/amd64": "llama-b8000-bin-win-cpu-x64.zip", + } { + os_, arch, _ := strings.Cut(k, "/") + if got, ok := PickServer(os_, arch, names(n, "llama-b8000-bin-ubuntu-vulkan-x64.tar.gz")); !ok || got.Name != n { + t.Errorf("%s: %q, want %q", k, got.Name, n) + } + } + if got, ok := PickServer("darwin", "arm64", names("llama-b8000-bin-macos.zip")); !ok || got.Name != "llama-b8000-bin-macos.zip" { + t.Errorf("Mac-Universal-Build: %q", got.Name) + } + if _, ok := PickServer("darwin", "arm64", names("llama-b8000-bin-macos-x64.zip")); ok { + t.Error("arm64 must not take an x64 Mac build") + } if _, ok := PickServer("linux", "arm64", names("llama-b1-bin-ubuntu-x64.zip")); ok { t.Error("arm64 must not take an x64 build") }