Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181package cli
import ( "encoding/json" "errors" "fmt" "io" "net" "os" "path/filepath" "strconv" "strings" "time"
"albedo/cli/internal/app" "albedo/cli/internal/config" "albedo/cli/internal/proxyserver" "albedo/cli/internal/webui" "github.com/spf13/cobra")
type proxyManifest struct { webui.Manifest Started time.Time `json:"started"` Quiet bool `json:"quiet,omitempty"` Providers []app.ProxyProvider `json:"providers"` Unavailable []string `json:"unavailable,omitempty"`}
func proxyManifestPath() string { return filepath.Join(config.HomeDir(), "proxy.json") }
func readProxyManifest() (proxyManifest, error) { data, err := os.ReadFile(proxyManifestPath()) if err != nil { return proxyManifest{}, err } var manifest proxyManifest err = json.Unmarshal(data, &manifest) return manifest, err}
func writeProxyManifest(manifest proxyManifest) error { data, err := json.Marshal(manifest) if err != nil { return err } file, err := os.CreateTemp(config.HomeDir(), ".proxy.json-*") if err != nil { return err } defer os.Remove(file.Name()) _, err = file.Write(data) if err = errors.Join(err, file.Close()); err != nil { return err } return os.Rename(file.Name(), proxyManifestPath())}
func backgroundProxy(cmd *cobra.Command, deps Dependencies, options webui.Options, quiet, rotate, stop, save bool) error { if err := os.MkdirAll(config.HomeDir(), 0o700); err != nil { return err } service, serviceErr := userLoginService("proxy") if serviceErr != nil && save { return serviceErr } if serviceErr == nil { installed, err := service.Installed() if err != nil { return err } if save || installed { return managedProxy(cmd, deps, service, options, quiet, rotate, stop, save, installed) } } manifest, readErr := readProxyManifest() alive := readErr == nil && webui.ManifestAlive(manifest.Manifest) out := cmd.OutOrStdout() if stop { if !alive { if manifest.TailnetHTTPS { releaseTailnet(cmd, manifest.Port) } _, err := fmt.Fprintln(out, "albedo proxy is not running.") return err } if err := stopDetached(manifest.PID); err != nil { return err } _, err := fmt.Fprintf(out, "stopped albedo proxy on %s/v1.\n", manifest.URL) return err } if alive { if cmd.Flags().Changed("port") && options.Port != manifest.Port || cmd.Flags().Changed("allow") && !options.SameAllow(manifest.Options) || cmd.Flags().Changed("public-url") && options.Public != manifest.Public || cmd.Flags().Changed("tailnet-https") && options.TailnetHTTPS != manifest.TailnetHTTPS || cmd.Flags().Changed("quiet") && quiet != manifest.Quiet { return errors.New("albedo proxy is already serving with different options; run albedo proxy --stop first") } } prepared, err := deps.Application.PrepareProxy(cmd.Context()) if err != nil { return err } if !prepared.Enabled { _, err := fmt.Fprintln(out, "the proxy extension is off. turn it on in /extensions, then run albedo proxy again.") return err } key, err := proxyserver.LoadKey(rotate) if err != nil { return err } if !alive { args := []string{"proxy", "--serve", "--port", strconv.Itoa(options.Port)} for _, network := range options.Allow { args = append(args, "--allow", network) } if quiet { args = append(args, "--quiet") } if options.TailnetHTTPS { args = append(args, "--tailnet-https") } else if options.Public != "" { args = append(args, "--public-url", options.Public) } if err := startDetached(cmd.Context(), args, filepath.Join(config.HomeDir(), "proxy.log"), func(pid int) bool { manifest, _ = readProxyManifest() return manifest.PID == pid && webui.ManifestAlive(manifest.Manifest) }); err != nil { return err } fmt.Fprintf(out, "albedo proxy is running in the background on %s/v1.\n", manifest.URL) } else { fmt.Fprintf(out, "albedo proxy was already running since %s on %s/v1.\n", proxyStarted(manifest.Started), manifest.URL) } fmt.Fprint(out, "stop it with: albedo proxy --stop\n\n") manifest.Providers, manifest.Unavailable = prepared.Providers, prepared.Unavailable printProxySummary(out, key, manifest) return nil}
func proxyStarted(started time.Time) string { local, now := started.Local(), time.Now() layout := "3:04 pm" if local.Year() != now.Year() { layout = "2 Jan 2006 at 3:04 pm" } else if local.YearDay() != now.YearDay() { layout = "2 Jan at 3:04 pm" } return strings.ToLower(local.Format(layout))}
func printProxySummary(out io.Writer, key string, manifest proxyManifest) { fmt.Fprintf(out, "base url: %s/v1\n", manifest.URL) for _, address := range append(manifest.Names, manifest.Addresses...) { fmt.Fprintf(out, " http://%s/v1\n", net.JoinHostPort(address, strconv.Itoa(manifest.Port))) } if manifest.Public != "" { fmt.Fprintf(out, " %s/v1\n", manifest.Public) } fmt.Fprintf(out, "api key: %s\n\nproviders:\n", key) for _, provider := range manifest.Providers { name := provider.Name if name == "openai" { name = "openai-compatible" } unit := "models" if len(provider.Models) == 1 { unit = "model" } fmt.Fprintf(out, " %s: %d %s — %s\n", name, len(provider.Models), unit, strings.Join(provider.Models[:min(3, len(provider.Models))], ", ")) } if len(manifest.Providers) == 0 { fmt.Fprintln(out, " none configured; run albedo login to add one") } for _, profile := range manifest.Unavailable { fmt.Fprintf(out, " %s: unavailable (check its provider settings)\n", profile) }}