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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143package tui
import ( "context" "encoding/json" "errors" "os/exec" "strings" "time"
"albedo/cli/internal/daemon"
tea "charm.land/bubbletea/v2" "github.com/charmbracelet/x/ansi")
// ChatDirenvMsg answers whether the session's folder has a .envrc direnv// blocks, or, with Allow, how allowing it went.type ChatDirenvMsg struct { Err error SessionID string Workspace string Envrc string Allow bool Generation int64}
// direnvCmd asks direnv whether the folder has a .envrc it blocks, when the// chat opens and after a move. `direnv allow` is the person's own act on// their own machine, so it asks only when the daemon and the workspace are// both on this one.func (m ChatModel) direnvCmd() tea.Cmd { if host, _ := daemon.SplitLocation(m.Workspace); m.client == nil || host != "" || !m.client.LocalDaemon() { return nil } id, generation, workspace := m.SessionID, m.Generation, m.Workspace return func() tea.Msg { envrc, err := blockedEnvrc(workspace) return ChatDirenvMsg{SessionID: id, Generation: generation, Workspace: workspace, Envrc: envrc, Err: err} }}
// blockedEnvrc is the .envrc direnv would load for dir but blocks until it is// allowed; "" when there is none, direnv allows or denies it, or direnv is not// installed.func blockedEnvrc(dir string) (string, error) { if _, err := exec.LookPath("direnv"); err != nil { return "", nil } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() command := exec.CommandContext(ctx, "direnv", "status", "--json") command.Dir = dir out, err := command.Output() if err != nil { return "", err } var status struct { State struct { FoundRC *struct { Path string `json:"path"` Allowed int `json:"allowed"` // 0 allowed, 1 blocked, 2 denied } `json:"foundRC"` } `json:"state"` } if err := json.Unmarshal(out, &status); err != nil { return "", err } if found := status.State.FoundRC; found != nil && found.Allowed == 1 { return found.Path, nil } return "", nil}
// answerDirenv handles a key while the question is open over an empty// composer: y runs `direnv allow`, n (or esc) puts the question away until// the chat opens again, as direnv asks again at the next cd. Other typing is// held back, so the first letter of a prompt never answers by accident; keys// that type nothing pass on.func (m *ChatModel) answerDirenv(msg tea.KeyPressMsg) (tea.Cmd, bool) { switch { case strings.EqualFold(msg.Text, "y"): return m.allowEnvrc(), true case strings.EqualFold(msg.Text, "n"), msg.String() == "esc": m.direnvAsk = "" m.syncLayout() return nil, true case msg.Text != "": return nil, true } return nil, false}
func (m *ChatModel) allowEnvrc() tea.Cmd { envrc, id, generation, workspace := m.direnvAsk, m.SessionID, m.Generation, m.Workspace m.direnvAsk = "" m.syncLayout() return func() tea.Msg { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() out, err := exec.CommandContext(ctx, "direnv", "allow", envrc).CombinedOutput() if said := strings.TrimSpace(string(out)); err != nil && said != "" { err = errors.New(said) } return ChatDirenvMsg{SessionID: id, Generation: generation, Workspace: workspace, Envrc: envrc, Allow: true, Err: err} }}
func (m *ChatModel) direnvAnswered(msg ChatDirenvMsg) tea.Cmd { if msg.SessionID != m.SessionID || msg.Generation != m.Generation || msg.Workspace != m.Workspace { return nil } switch { case msg.Allow && msg.Err == nil: m.CopyStatus = "allowed " + msg.Envrc + " · jobs load it from the next run()" m.copyStatusRevision++ return m.clearCopyStatusCmd() case msg.Allow: m.Notices.Add("direnv allow "+msg.Envrc+" failed: "+msg.Err.Error(), true) case msg.Err == nil: m.direnvAsk = msg.Envrc } m.syncLayout() return nil}
// direnvRows is the question above the composer: one row, or two when the// keys do not fit beside it.func (m ChatModel) direnvRows(width int) []string { if m.direnvAsk == "" { return nil } question := "this folder's .envrc is blocked · direnv allow it for run() jobs?" keys := keyHints(hint{"y", "allow"}, hint{"n", "not now"}) if ansi.StringWidth(question)+3+ansi.StringWidth(keys) <= width { return []string{DefaultStyles.Warning.Render(question) + " " + keys} } return []string{ DefaultStyles.Warning.Render(ansi.Truncate(question, width, "…")), ansi.Truncate(keys, width, "…"), }}