forked from xenodium/agent-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-shell-goose.el
More file actions
141 lines (114 loc) · 5.37 KB
/
agent-shell-goose.el
File metadata and controls
141 lines (114 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
;;; agent-shell-goose.el --- Goose agent configurations -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Alvaro Ramirez
;; Author: Alvaro Ramirez https://xenodium.com
;; URL: https://github.com/xenodium/agent-shell
;; This package is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This package is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This file includes Goose-specific configurations.
;;
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'shell-maker)
(require 'acp)
(declare-function agent-shell--indent-string "agent-shell")
(declare-function agent-shell-make-agent-config "agent-shell")
(declare-function agent-shell-start "agent-shell")
(cl-defun agent-shell-make-goose-authentication (&key openai-api-key)
"Create Goose authentication configuration.
OPENAI-API-KEY is the OpenAI API key string or function that returns it."
(unless openai-api-key
(error "Must specify :openai-api-key"))
`((:openai-api-key . ,openai-api-key)))
(defcustom agent-shell-goose-authentication nil
"Configuration for Goose authentication.
For API key (string):
(setq agent-shell-goose-authentication
(agent-shell-make-goose-authentication :openai-api-key \"your-key\"))
For API key (function):
(setq agent-shell-goose-authentication
(agent-shell-make-goose-authentication :openai-api-key (lambda () ...)))"
:type 'alist
:group 'agent-shell)
(defcustom agent-shell-goose-command
'("goose" "acp")
"Command and parameters for the Goose client.
The first element is the command name, and the rest are command parameters."
:type '(repeat string)
:group 'agent-shell)
(defun agent-shell-goose-make-agent-config ()
"Create a Goose agent configuration.
Returns an agent configuration alist using `agent-shell-make-agent-config'."
(agent-shell-make-agent-config
:new-session t
:mode-line-name "Goose"
:buffer-name "Goose"
:shell-prompt "Goose> "
:shell-prompt-regexp "Goose> "
:welcome-function #'agent-shell-goose--welcome-message
:icon-name "goose.png"
:client-maker (lambda (buffer)
(agent-shell-goose-make-client :buffer buffer))
:install-instructions "See https://block.github.io/goose/docs/getting-started/installation."))
(defun agent-shell-goose-start-agent ()
"Start an interactive Goose agent shell."
(interactive)
(agent-shell-start
:config (agent-shell-goose-make-agent-config)))
(cl-defun agent-shell-goose-make-client (&key buffer)
"Create a Goose client using configured authentication with BUFFER as context.
Uses `agent-shell-goose-authentication' for authentication configuration."
(unless buffer
(error "Missing required argument: :buffer"))
(let ((api-key (agent-shell-goose-key)))
(unless api-key
(error "Goose OpenAI API key not configured"))
(acp-make-client :command (car agent-shell-goose-command)
:command-params (cdr agent-shell-goose-command)
:environment-variables (list (format "OPENAI_API_KEY=%s" api-key))
:context-buffer buffer)))
(defun agent-shell-goose-key ()
"Get the Goose OpenAI API key."
(cond ((stringp (map-elt agent-shell-goose-authentication :openai-api-key))
(map-elt agent-shell-goose-authentication :openai-api-key))
((functionp (map-elt agent-shell-goose-authentication :openai-api-key))
(condition-case _err
(funcall (map-elt agent-shell-goose-authentication :openai-api-key))
(error
(error "OpenAI API key not found. Check out `agent-shell-goose-authentication'"))))
(t
nil)))
(defun agent-shell-goose--welcome-message (config)
"Return Goose welcome message using `shell-maker' CONFIG."
(let ((art (agent-shell--indent-string 4 (agent-shell-goose--ascii-art)))
(message (string-trim-left (shell-maker-welcome-message config) "\n")))
(concat "\n\n"
art
"\n\n"
message)))
(defun agent-shell-goose--ascii-art ()
"Goose ASCII art."
(let* ((is-dark (eq (frame-parameter nil 'background-mode) 'dark))
(text (string-trim "
╭─────╮ ╭─────╮ ╭─────╮ ╭─────╮ ╭─────╮
│ ╭───╯ │ ╭─╮ │ │ ╭─╮ │ │ ╭───╯ │ ╭───╯
│ │ │ │ │ │ │ │ │ │ │ ╰───╮ │ ╰───╮
│ │ ╭─╮ │ │ │ │ │ │ │ │ ╰───╮ │ │ ╭───╯
│ ╰─╯ │ │ ╰─╯ │ │ ╰─╯ │ ╭───╯ │ │ ╰───╮
╰───╮ │ ╰─────╯ ╰─────╯ ╰─────╯ ╰─────╯
╰─╯" "\n")))
(propertize text 'font-lock-face (if is-dark
'(:foreground "#a0a0a0")
'(:foreground "#505050")))))
(provide 'agent-shell-goose)
;;; agent-shell-goose.el ends here