forked from xenodium/agent-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-shell-google.el
More file actions
239 lines (201 loc) · 11.4 KB
/
agent-shell-google.el
File metadata and controls
239 lines (201 loc) · 11.4 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
;;; agent-shell-google.el --- Google 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 Google-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--interpolate-gradient "agent-shell")
(declare-function agent-shell--make-acp-client "agent-shell")
(declare-function agent-shell-make-agent-config "agent-shell")
(declare-function agent-shell-start "agent-shell")
(cl-defun agent-shell-google-make-authentication (&key api-key login vertex-ai)
"Create Google authentication configuration.
API-KEY is the Google API key string or function that returns it.
LOGIN when non-nil indicates to use login-based authentication.
VERTEX-AI when non-nil indicates to use Vertex AI authentication.
Only one of API-KEY, LOGIN, or VERTEX-AI should be provided."
(when (> (seq-count #'identity (list api-key login vertex-ai)) 1)
(error "Cannot specify multiple authentication methods - choose one"))
(unless (> (seq-count #'identity (list api-key login vertex-ai)) 0)
(error "Must specify one of :api-key, :login, or :vertex-ai"))
(cond
(api-key `((:api-key . ,api-key)))
(login `((:login . t)))
(vertex-ai `((:vertex-ai . t)))))
(defcustom agent-shell-google-authentication
(agent-shell-google-make-authentication :login t)
"Configuration for Google authentication.
For login-based authentication (default):
(setq agent-shell-google-authentication
(agent-shell-google-make-authentication :login t))
For API key (string):
(setq agent-shell-google-authentication
(agent-shell-google-make-authentication :api-key \"your-key\"))
For API key (function):
(setq agent-shell-google-authentication
(agent-shell-google-make-authentication :api-key (lambda () ...)))
For Vertex AI authentication:
(setq agent-shell-google-authentication
(agent-shell-google-make-authentication :vertex-ai t))"
:type 'alist
:group 'agent-shell)
(defcustom agent-shell-google-gemini-command
'("gemini" "--experimental-acp"
"--allowed-tools" "run_shell_command" "list_directory"
"read_many_files" "web_fetch" "google_web_search")
"Command and parameters for the Gemini client.
The first element is the command name, and the rest are command parameters."
:type '(repeat string)
:group 'agent-shell)
(defcustom agent-shell-google-gemini-environment
nil
"Environment variables for the Google Gemini client.
This should be a list of environment variables to be used when
starting the Gemini client process.
Example usage to set custom environment variables:
(setq agent-shell-google-gemini-environment
(`agent-shell-make-environment-variables'
\"MY_VAR\" \"some-value\"
\"MY_OTHER_VAR\" \"another-value\"))"
:type '(repeat string)
:group 'agent-shell)
(defun agent-shell-google-make-gemini-config ()
"Create a Gemini CLI agent configuration.
Returns an agent configuration alist using `agent-shell-make-agent-config'."
(when (and (boundp 'agent-shell-google-key) agent-shell-google-key)
(user-error "Please migrate to use agent-shell-google-authentication and eval (setq agent-shell-google-key nil)"))
(agent-shell-make-agent-config
:mode-line-name "Gemini CLI"
:buffer-name "Gemini CLI"
:shell-prompt "Gemini> "
:shell-prompt-regexp "Gemini> "
:icon-name "gemini.png"
:welcome-function #'agent-shell-google--gemini-welcome-message
:needs-authentication t
:authenticate-request-maker (lambda ()
(cond ((map-elt agent-shell-google-authentication :api-key)
(acp-make-authenticate-request :method-id "gemini-api-key"))
((map-elt agent-shell-google-authentication :vertex-ai)
(acp-make-authenticate-request :method-id "vertex-ai"))
(t
(acp-make-authenticate-request :method-id "oauth-personal"))))
:client-maker (lambda (buffer)
(agent-shell-google-make-gemini-client :buffer buffer))
:install-instructions "See https://github.com/google-gemini/gemini-cli for installation."))
(defun agent-shell-google-start-gemini ()
"Start an interactive Gemini CLI agent shell."
(interactive)
(agent-shell-start
:config (agent-shell-google-make-gemini-config)))
(cl-defun agent-shell-google-make-gemini-client (&key buffer)
"Create a Gemini client using configured authentication with BUFFER as context.
Uses `agent-shell-google-authentication' for authentication configuration."
(unless buffer
(error "Missing required argument: :buffer"))
(when (and (boundp 'agent-shell-google-key) agent-shell-google-key)
(user-error "Please migrate to use agent-shell-google-authentication and eval (setq agent-shell-google-key nil)"))
(cond
((map-elt agent-shell-google-authentication :api-key)
(agent-shell--make-acp-client :command (car agent-shell-google-gemini-command)
:command-params (cdr agent-shell-google-gemini-command)
:environment-variables (append (when-let ((api-key (agent-shell-google-key)))
(list (format "GEMINI_API_KEY=%s" api-key)))
agent-shell-google-gemini-environment)
:context-buffer buffer))
((map-elt agent-shell-google-authentication :login)
(agent-shell--make-acp-client :command (car agent-shell-google-gemini-command)
:command-params (cdr agent-shell-google-gemini-command)
:environment-variables agent-shell-google-gemini-environment
:context-buffer buffer))
((map-elt agent-shell-google-authentication :vertex-ai)
(agent-shell--make-acp-client :command (car agent-shell-google-gemini-command)
:command-params (cdr agent-shell-google-gemini-command)
:environment-variables agent-shell-google-gemini-environment
:context-buffer buffer))
(t
(error "Invalid authentication configuration"))))
(defun agent-shell-google--gemini-welcome-message (config)
"Return Gemini CLI ASCII art as per own repo using `shell-maker' CONFIG."
(let ((art (agent-shell--indent-string 4 (agent-shell-google--gemini-ascii-art)))
(message (string-trim-left (shell-maker-welcome-message config) "\n")))
(concat "\n\n\n"
art
"\n\n"
message)))
(defun agent-shell-google--gemini-ascii-art ()
"Generate Gemini CLI ASCII art, inspired by its codebase."
;; Based on:
;; https://github.com/google-gemini/gemini-cli/tree/main/packages/cli/src/ui/components/Header.tsx
;; https://github.com/google-gemini/gemini-cli/tree/main/packages/cli/src/ui/components/AsciiArt.ts
;; https://github.com/google-gemini/gemini-cli/tree/main/packages/cli/src/ui/themes/theme.ts
(let* ((text (string-trim "
███ █████████ ██████████ ██████ ██████ █████ ██████ █████ █████
░░░███ ███░░░░░███░░███░░░░░█░░██████ ██████ ░░███ ░░██████ ░░███ ░░███
░░░███ ███ ░░░ ░███ █ ░ ░███░█████░███ ░███ ░███░███ ░███ ░███
░░░███ ░███ ░██████ ░███░░███ ░███ ░███ ░███░░███░███ ░███
███░ ░███ █████ ░███░░█ ░███ ░░░ ░███ ░███ ░███ ░░██████ ░███
███░ ░░███ ░░███ ░███ ░ █ ░███ ░███ ░███ ░███ ░░█████ ░███
███░ ░░█████████ ██████████ █████ █████ █████ █████ ░░█████ █████
░░░ ░░░░░░░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░ ░░░░░" "\n"))
(is-dark (eq (frame-parameter nil 'background-mode) 'dark))
(gradient-colors (if is-dark
'("#4796E4" "#847ACE" "#C3677F")
'("#3B82F6" "#8B5CF6" "#DD4C4C")))
(lines (split-string text "\n"))
(result ""))
(dolist (line lines)
(let ((line-length (length line))
(propertized-line ""))
(dotimes (i line-length)
(let* ((char (substring line i (1+ i)))
(progress (/ (float i) line-length))
(color (agent-shell--interpolate-gradient gradient-colors progress)))
(setq propertized-line
(concat propertized-line
(propertize char 'font-lock-face `(:foreground ,color))))))
(setq result (concat result propertized-line "\n"))))
(string-trim-right result)))
(defun agent-shell-google--gemini-text ()
"Colorized Gemini text with Google-branded colors."
(let* ((is-dark (eq (frame-parameter nil 'background-mode) 'dark))
(colors (if is-dark
'("#4796E4" "#6B82D9" "#847ACE" "#9E6FA8" "#B16C93" "#C3677F")
'("#3B82F6" "#5F6CF6" "#8B5CF6" "#A757D0" "#C354A0" "#DD4C4C")))
(text "Gemini")
(result ""))
(dotimes (i (length text))
(setq result (concat result
(propertize (substring text i (1+ i))
'font-lock-face `(:foreground ,(nth (mod i (length colors)) colors))))))
result))
(defun agent-shell-google-key ()
"Get the Google API key."
(cond ((stringp (map-elt agent-shell-google-authentication :api-key))
(map-elt agent-shell-google-authentication :api-key))
((functionp (map-elt agent-shell-google-authentication :api-key))
(condition-case _err
(funcall (map-elt agent-shell-google-authentication :api-key))
(error
"Api key not found. Check out `agent-shell-google-authentication'")))
(t
nil)))
(provide 'agent-shell-google)
;;; agent-shell-google.el ends here