forked from xenodium/agent-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-shell-anthropic.el
More file actions
247 lines (202 loc) · 10.8 KB
/
agent-shell-anthropic.el
File metadata and controls
247 lines (202 loc) · 10.8 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
240
241
242
243
244
245
246
247
;;; agent-shell-anthropic.el --- Anthropic 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 Anthropic-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-acp-client "agent-shell")
(declare-function agent-shell-make-agent-config "agent-shell")
(autoload 'agent-shell-make-agent-config "agent-shell")
(declare-function agent-shell--dwim "agent-shell")
(cl-defun agent-shell-anthropic-make-authentication (&key api-key login oauth)
"Create anthropic authentication configuration.
API-KEY is the Anthropic API key string or a function returning one.
LOGIN when non-nil indicates to use login-based authentication.
OAUTH is an OAuth token string or a function returning one.
Only one of API-KEY, LOGIN, or OAUTH should be provided, never more than one."
(when (and api-key login)
(error "Cannot specify both :api-key and :login - choose one"))
(when (and oauth login)
(error "Cannot specify both :oauth and :login - choose one"))
(when (and api-key oauth)
(error "Cannot specify both :api-key and :oauth - choose one"))
(unless (or api-key login oauth)
(error "Must specify either :api-key, :login, or :oauth"))
(cond
(oauth `((:oauth . ,oauth)))
(api-key `((:api-key . ,api-key)))
(login `((:login . t)))))
(defcustom agent-shell-anthropic-authentication
(agent-shell-anthropic-make-authentication :login t)
"Configuration for Anthropic authentication.
For subscription/login (default):
(setq agent-shell-anthropic-authentication
(agent-shell-anthropic-make-authentication :login t))
For api key:
(setq agent-shell-anthropic-authentication
(agent-shell-anthropic-make-authentication :api-key \"your-key\"))
or
(setq agent-shell-anthropic-authentication
(agent-shell-anthropic-make-authentication :api-key (lambda () ... )))
For OAuth token:
(setq agent-shell-anthropic-authentication
(agent-shell-anthropic-make-authentication :oauth \"your-token\"))
or
(setq agent-shell-anthropic-authentication
(agent-shell-anthropic-make-authentication :oauth (lambda () ... )))"
:type 'alist
:group 'agent-shell)
(defcustom agent-shell-anthropic-default-model-id
nil
"Default Anthropic model ID.
Must be one of the model ID's displayed under \"Available models\"
when starting a new shell.
Can be set to either a string or a function that returns a string."
:type '(choice (const nil) string function)
:group 'agent-shell)
(defcustom agent-shell-anthropic-default-session-mode-id
nil
"Default Anthropic session mode ID.
Must be one of the mode ID's displayed under \"Available modes\"
when starting a new shell."
:type '(choice (const nil) string)
:group 'agent-shell)
(defcustom agent-shell-anthropic-claude-acp-command
'("claude-agent-acp")
"Command and parameters for the Anthropic Claude client.
The first element is the command name, and the rest are command parameters."
:type '(repeat string)
:group 'agent-shell)
(defcustom agent-shell-anthropic-claude-environment
nil
"Environment variables for the Anthropic Claude client.
This should be a list of environment variables to be used when
starting the Claude client process.
Example usage to set a custom Anthropic API base URL:
(setq agent-shell-anthropic-claude-environment
(`agent-shell-make-environment-variables'
\"ANTHROPIC_BASE_URL\" \"https://api.moonshot.cn/anthropic/\"
\"ANTHROPIC_MODEL\" \"moonshot-v1-auto\"))"
:type '(repeat string)
:group 'agent-shell)
(defun agent-shell-anthropic-make-claude-code-config ()
"Create a Claude Agent configuration.
Returns an agent configuration alist using `agent-shell-make-agent-config'."
(agent-shell-make-agent-config
:identifier 'claude-code
:mode-line-name "Claude"
:buffer-name "Claude"
:shell-prompt "Claude> "
:shell-prompt-regexp "Claude> "
:icon-name "claudecode.png"
:welcome-function #'agent-shell-anthropic--claude-code-welcome-message
:client-maker (lambda (buffer)
(agent-shell-anthropic-make-claude-client :buffer buffer))
:default-model-id (lambda () (if (functionp agent-shell-anthropic-default-model-id)
(funcall agent-shell-anthropic-default-model-id)
agent-shell-anthropic-default-model-id))
:default-session-mode-id (lambda () agent-shell-anthropic-default-session-mode-id)
:install-instructions "See https://github.com/agentclientprotocol/claude-agent-acp for installation."))
(defun agent-shell-anthropic-start-claude-code ()
"Start an interactive Claude Agent shell."
(interactive)
(agent-shell--dwim :config (agent-shell-anthropic-make-claude-code-config)
:new-shell t))
(cl-defun agent-shell-anthropic-make-claude-client (&key buffer)
"Create a Claude Agent ACP client with BUFFER as context.
See `agent-shell-anthropic-authentication' for authentication
and optionally `agent-shell-anthropic-claude-environment' for
additional environment variables."
(unless buffer
(error "Missing required argument: :buffer"))
(when (and (boundp 'agent-shell-anthropic-key) agent-shell-anthropic-key)
(user-error "Please migrate to use agent-shell-anthropic-authentication and eval (setq agent-shell-anthropic-key nil)"))
(when (and (boundp 'agent-shell-anthropic-claude-command) agent-shell-anthropic-claude-command)
(user-error "Please migrate to use agent-shell-anthropic-claude-acp-command and eval (setq agent-shell-anthropic-claude-command nil)"))
(let ((env-vars-overrides (cond
((map-elt agent-shell-anthropic-authentication :api-key)
(list (format "ANTHROPIC_API_KEY=%s"
(agent-shell-anthropic-key))))
((map-elt agent-shell-anthropic-authentication :login)
(list "ANTHROPIC_API_KEY="))
((map-elt agent-shell-anthropic-authentication :oauth)
(list (format "CLAUDE_CODE_OAUTH_TOKEN=%s"
(agent-shell-anthropic-oauth-token))))
(t
(error "Invalid authentication configuration")))))
(agent-shell--make-acp-client :command (car agent-shell-anthropic-claude-acp-command)
:command-params (cdr agent-shell-anthropic-claude-acp-command)
:environment-variables (append env-vars-overrides
agent-shell-anthropic-claude-environment)
:context-buffer buffer)))
(defun agent-shell-anthropic-key ()
"Get the Anthropic API key."
(cond ((stringp (map-elt agent-shell-anthropic-authentication :api-key))
(map-elt agent-shell-anthropic-authentication :api-key))
((functionp (map-elt agent-shell-anthropic-authentication :api-key))
(condition-case _err
(funcall (map-elt agent-shell-anthropic-authentication :api-key))
(error
"Api key not found. Check out `agent-shell-anthropic-authentication'")))
(t
nil)))
(defun agent-shell-anthropic-oauth-token ()
"Get the Anthropic OAuth token."
(cond ((stringp (map-elt agent-shell-anthropic-authentication :oauth))
(map-elt agent-shell-anthropic-authentication :oauth))
((functionp (map-elt agent-shell-anthropic-authentication :oauth))
(condition-case _err
(funcall (map-elt agent-shell-anthropic-authentication :oauth))
(error
"OAuth token not found. Check out `agent-shell-anthropic-authentication'")))
(t
nil)))
(defun agent-shell-anthropic--claude-code-welcome-message (config)
"Return Claude Agent ASCII art as per own repo using `shell-maker' CONFIG."
(let ((art (agent-shell--indent-string 4 (agent-shell-anthropic--claude-code-ascii-art)))
(message (string-trim-left (shell-maker-welcome-message config) "\n")))
(concat "\n\n"
art
"\n\n"
message)))
(defun agent-shell-anthropic--claude-code-ascii-art ()
"Claude Agent ASCII art.
Generated by https://github.com/shinshin86/oh-my-logo."
(let* ((is-dark (eq (frame-parameter nil 'background-mode) 'dark))
(text (string-trim "
██████╗ ██╗ █████╗ ██╗ ██╗ ██████╗ ███████╗
██╔════╝ ██║ ██╔══██╗ ██║ ██║ ██╔══██╗ ██╔════╝
██║ ██║ ███████║ ██║ ██║ ██║ ██║ █████╗
██║ ██║ ██╔══██║ ██║ ██║ ██║ ██║ ██╔══╝
╚██████╗ ███████╗ ██║ ██║ ╚██████╔╝ ██████╔╝ ███████╗
╚═════╝ ╚══════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝
██████╗ ██████╗ ██████╗ ███████╗
██╔════╝ ██╔═══██╗ ██╔══██╗ ██╔════╝
██║ ██║ ██║ ██║ ██║ █████╗
██║ ██║ ██║ ██║ ██║ ██╔══╝
╚██████╗ ╚██████╔╝ ██████╔╝ ███████╗
╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝
" "\n")))
(propertize text 'font-lock-face (if is-dark
'(:foreground "#d26043" :inherit fixed-pitch)
'(:foreground "#b8431f" :inherit fixed-pitch)))))
(provide 'agent-shell-anthropic)
;;; agent-shell-anthropic.el ends here