magpie-aws-quick-bridge 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -183
- package/bin/cli.mjs +211 -0
- package/gateway/anthropic.mjs +192 -0
- package/gateway/ccswitch.mjs +38 -0
- package/gateway/responses.mjs +254 -0
- package/gateway/server.mjs +195 -0
- package/gateway/service.mjs +68 -0
- package/gateway/sse.mjs +113 -0
- package/gateway/store.mjs +118 -0
- package/index.mjs +1 -73
- package/package.json +13 -4
- package/plugin/messages.mjs +5 -4
- package/plugin/settings.mjs +78 -0
- package/plugin/toolbridge.mjs +1 -0
package/README.md
CHANGED
|
@@ -8,226 +8,100 @@
|
|
|
8
8
|
<a href="https://www.npmjs.com/package/magpie-aws-quick-bridge">npm</a>
|
|
9
9
|
</p>
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
这是我做的一个小工具:把 **Amazon Quick(QuickWork)** 账号接到 Claude Code、Codex、OpenCode 这些编程 agent 上用。
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
**同一个包,[Magpie](https://usemagpie.ai) 和 [CC Switch](https://github.com/farion1231/cc-switch) 都支持:**
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
## 功能
|
|
18
|
-
|
|
19
|
-
- 浏览器登录 Amazon Quick:个人账号(social)与企业账号(IAM Identity Center / SSO),PKCE
|
|
20
|
-
- Magpie 在另一台电脑(服务器、Docker)上时,把浏览器最后停下的地址粘贴回 Magpie 即可完成登录
|
|
21
|
-
- 多账号、故障切换、并发上限由 Magpie 负责;token 到期前 10 分钟由 Magpie 调用插件自动续期
|
|
22
|
-
- agent 用哪种 API 都可以:Magpie 网关先转成 chat completions 再交给插件(Chat Completions、Responses、Anthropic Messages 已在网关上验证)
|
|
23
|
-
- 流式与非流式回复,返回上游 `turn_usage` 的 token 用量和缓存命中;模型的思考过程以 `reasoning_content` 返回
|
|
24
|
-
- `magpie quota` 显示 Amazon Quick 的套餐、月度额度和会话额度
|
|
25
|
-
- 模型列表按账号读取:账号里被停用的模式不列出,开通了 advanced 的账号多一个 `aws-quick/advanced`
|
|
26
|
-
- 多轮对话复用 Amazon Quick 服务端的线程,后续请求只发送新增的消息
|
|
27
|
-
- Amazon Quick 单帧上限是 64 KB:超出时先把工具定义压成一行签名,再把过长的对话上传为附件让模型读取,agent 的长提示词和大段工具结果不会因此失败
|
|
28
|
-
- 图片输入(`data:` URL)上传为附件
|
|
29
|
-
- 客户端工具调用:`tools` 通过 `<client_tool_call>` 文本协议桥接,返回标准 `tool_calls`;带工具的流式回复也是边生成边输出,只有工具调用标记本身会被拦下
|
|
30
|
-
- `system` / `developer` 消息走 Amazon Quick 的 `custom_prompt`,并附加覆盖声明,让模型忽略服务端追加的提示词
|
|
31
|
-
- 默认使用隐身线程,不进入 Amazon Quick 自己的聊天列表和记忆
|
|
32
|
-
- 没有运行时依赖,也没有安装脚本
|
|
33
|
-
|
|
34
|
-
## 模型
|
|
35
|
-
|
|
36
|
-
| Magpie 里的模型 | Amazon Quick 的 agent mode |
|
|
15
|
+
| 你在用 | 怎么接 |
|
|
37
16
|
|---|---|
|
|
38
|
-
| `aws-quick/smart`
|
|
39
|
-
|
|
|
40
|
-
| `aws-quick/balanced` | balanced |
|
|
41
|
-
| `aws-quick/auto` | smart,并打开 Amazon Quick 的自动路由 |
|
|
42
|
-
| `aws-quick/advanced` | advanced(仅账号开通时出现) |
|
|
43
|
-
|
|
44
|
-
这些是 Amazon Quick 的 agent mode,不是具体的模型名。我的账号上 fast、balanced、smart 实际分别由 Claude Haiku、Sonnet、Opus 回答(见每轮用量里的 `resolved_model_id`),其他账号可能不同。上下文长度按上游每轮用量里报告的 `context_window` 填为 1M;最大输出(fast 32k,其余 64k)是估计值,Amazon Quick 没有公布。推理档位 low / medium / high 对应上游的 `thinking_effort`。
|
|
45
|
-
|
|
46
|
-
## 安装
|
|
47
|
-
|
|
48
|
-
```sh
|
|
49
|
-
# 从 npm
|
|
50
|
-
magpie plugin add magpie-aws-quick-bridge
|
|
51
|
-
|
|
52
|
-
# 从 GitHub
|
|
53
|
-
magpie plugin add github:DaBinBinah/magpie-AWS-Quick-bridge
|
|
17
|
+
| **Magpie** | 装成 Magpie 插件,在 Magpie 里登录,模型是 `aws-quick/smart` 这样的名字 |
|
|
18
|
+
| **CC Switch** | 在本机起一个 API 服务,一条命令导入 CC Switch,切过去就能用 |
|
|
54
19
|
|
|
55
|
-
|
|
56
|
-
magpie plugin add /path/to/magpie-AWS-Quick-bridge
|
|
57
|
-
```
|
|
20
|
+
两种用法互不影响,可以同时用。
|
|
58
21
|
|
|
59
|
-
|
|
22
|
+
> 非官方项目,和 Amazon / AWS 没有关系。请只用你自己的、或者得到授权的账号,并遵守 Amazon Quick 的服务条款。
|
|
60
23
|
|
|
61
|
-
##
|
|
24
|
+
## 用在 Magpie 里
|
|
62
25
|
|
|
63
26
|
```sh
|
|
64
|
-
magpie plugin
|
|
27
|
+
magpie plugin add magpie-aws-quick-bridge # 安装
|
|
28
|
+
magpie plugin login aws-quick # 登录:浏览器里登 Amazon Quick,看到 Login complete 就好了
|
|
65
29
|
```
|
|
66
30
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
1. 选择账号类型:个人账号,或企业账号(再填组织所在的 AWS 区域,留空为 `us-east-1`)。
|
|
70
|
-
2. Magpie 打开浏览器,在 Amazon Quick 的页面完成登录;浏览器会回到本机的回调端口(默认 `18080`,被占用时个人账号自动换用 `18081`–`18083` 或任意空闲端口)。
|
|
71
|
-
3. 接着自动跳到 AWS 的授权页,同意后看到 “Login complete” 即完成。
|
|
72
|
-
|
|
73
|
-
需要多个账号时重复登录即可;同一邮箱再次登录会替换原来的那个账号。
|
|
74
|
-
|
|
75
|
-
### Magpie 在另一台电脑上
|
|
76
|
-
|
|
77
|
-
浏览器访问不到服务器的 `localhost` 时:
|
|
78
|
-
|
|
79
|
-
1. 在自己的浏览器里打开登录说明中给出的 `https://quick.aws.com/sn?qwSurface=…` 地址并登录。
|
|
80
|
-
2. 浏览器会停在一个打不开的 `http://localhost:18080/callback?nonce=…` 页面。把地址栏里的**完整地址**粘贴回 Magpie(终端里直接粘贴后回车;应用里填进登录窗口的 Callback URL)。
|
|
81
|
-
3. Magpie 给出下一步要打开的 AWS 授权地址(终端里是一行 `Amazon Quick sign-in, next page to open: …`)。打开它并同意。
|
|
82
|
-
4. 浏览器停在 `http://127.0.0.1:18080/?code=…`,再把这个完整地址粘贴回 Magpie,登录完成。
|
|
31
|
+
在 Magpie 应用里也一样:**插件 → 添加插件** 填 `magpie-aws-quick-bridge`,然后在 **Amazon Quick** 那一行点登录。个人账号和企业账号(IAM Identity Center / SSO)都可以,登多个账号就重复登录几次。
|
|
83
32
|
|
|
84
|
-
|
|
33
|
+
装好以后所有 agent 都能用 `aws-quick/smart`、`aws-quick/fast`、`aws-quick/balanced`、`aws-quick/auto`,`magpie quota` 能看到剩余额度。
|
|
85
34
|
|
|
86
|
-
|
|
35
|
+
## 用在 CC Switch 里
|
|
87
36
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
## 选项
|
|
37
|
+
CC Switch 没有插件,它只管给 agent 换「API 地址 + Key」。所以我在这个包里带了一个本机 API 服务,CC Switch 指向它就行。需要 Node.js 20 以上。
|
|
91
38
|
|
|
92
39
|
```sh
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
| 选项 | 默认 | 说明 |
|
|
97
|
-
|---|---|---|
|
|
98
|
-
| `thinking_effort` | 空 | 请求没有指定推理档位时使用:`off`、`low`、`medium`、`high` |
|
|
99
|
-
| `default_model` | `smart` | 收到未知模型名时使用的 agent mode |
|
|
100
|
-
| `timezone` / `locale` | `UTC` / `en-US` | 发给上游的时区与语言 |
|
|
101
|
-
| `request_timeout` | `5m` | 单次上游请求超时(`30s`、`5m`、`1h`,或秒数) |
|
|
102
|
-
| `keep_sessions` | `false` | 对话进入 Amazon Quick 的聊天列表和记忆,而不是使用隐身线程(也可在供应商编辑页的插件选项里开关) |
|
|
103
|
-
| `web_search` | `false` | 允许 Amazon Quick 使用它自己的联网搜索;请求里的 `web_search_enabled` 优先 |
|
|
104
|
-
| `stateful` | `true` | 多轮对话复用服务端线程、只发新增消息;`false` 时每次请求重发全部历史 |
|
|
105
|
-
| `attachments` | `true` | 放不进一帧的内容和图片上传为附件;`false` 时改为截断较早的内容 |
|
|
106
|
-
| `max_frame_bytes` | `60000` | 一帧里留给提示词的字节数上限,超过就压缩或转附件;`0` 关闭这项处理 |
|
|
107
|
-
| `app_version` | `1.2.4660` | 上游握手使用的 appVersion |
|
|
108
|
-
| `surface` | `QUICK_WORK_DESKTOP_WINDOWS` | 上游握手使用的 surfaceType |
|
|
109
|
-
| `outpost_id` / `working_session_id` | 空 | 上游握手可选字段 |
|
|
110
|
-
| `callback_port` | `18080` | 本机 OAuth 回调端口;`0` 为任意空闲端口 |
|
|
111
|
-
| `login_timeout` | `30m` | 等待浏览器完成登录的时间 |
|
|
112
|
-
| `build_stage` | `prod` | `prod`、`beta`、`integ`、`dev` |
|
|
113
|
-
| `proxy` | 空 | 聊天 WebSocket 使用的代理地址(见下面的限制) |
|
|
114
|
-
| `oidc_base` / `discovery_url` / `data_plane_url` | 空 | 覆盖上游地址,仅用于测试(模拟上游) |
|
|
115
|
-
|
|
116
|
-
改动选项会重启插件宿主,从下一个请求起生效。
|
|
117
|
-
|
|
118
|
-
## 工作方式
|
|
119
|
-
|
|
120
|
-
```mermaid
|
|
121
|
-
sequenceDiagram
|
|
122
|
-
participant A as agent
|
|
123
|
-
participant M as Magpie 网关
|
|
124
|
-
participant P as 本插件
|
|
125
|
-
participant Q as Amazon Quick
|
|
126
|
-
|
|
127
|
-
A->>M: 任意 API(Chat / Responses / Messages / Gemini)
|
|
128
|
-
M->>P: fetch:POST /chat/completions(已选好账号)
|
|
129
|
-
P->>Q: (需要时)上传附件
|
|
130
|
-
P->>Q: WebSocket:session_connect / user_message
|
|
131
|
-
Q-->>P: thinking_delta / text_delta / turn_usage / message_complete
|
|
132
|
-
P-->>M: JSON 或 SSE(文本、tool_calls、usage)
|
|
133
|
-
M-->>A: 按 agent 的 API 格式流式返回
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
- 每个请求打开一条到 Amazon Quick 的 WebSocket,把要发送的消息转成一条 `user_message`:单条用户消息原样发送,多轮对话和工具结果按 `User:` / `Assistant tool call (...)` / `Tool result (...)` 渲染。
|
|
137
|
-
- 线程复用:Magpie 通过 `chat.headers` 钩子把会话 ID 带给插件;同一账号、同一会话、历史前缀一致的请求会接到上一轮的服务端线程上,只发新增消息,系统提示词没变也不重发。历史被改写(压缩、回退)或线程缓存过期(内存里保留 6 小时)时,开新线程重发全部历史。
|
|
138
|
-
- 64 KB 帧上限:按“完整工具定义 → 一行签名 → 缩短系统提示词(最多占半帧)→ 过长内容转附件 → 截断较早的内容”的顺序处理,直到一帧放得下。附件经 Amazon Quick 的上传接口和 DLP 检查后挂到线程上,由模型用它自己的文件工具读取。
|
|
139
|
-
- 工具只在客户端执行:插件把模型输出的 `<client_tool_call>{"name":…,"arguments":…}</client_tool_call>` 转成 `tool_calls`,下一轮再把工具结果作为文本带回去。`tool_choice: "none"` 关闭桥接,指定函数时只接受该函数。
|
|
140
|
-
- 出错时尽量用 Magpie 能处理的状态回答:上游拒绝登录回 401(账号被标记为需要重新登录),限流回 429,超时回 504,其他上游错误回 502,Magpie 据此切换到下一个账号或模型。回复开始之后的错误只能写进流里。
|
|
141
|
-
|
|
142
|
-
代码结构:
|
|
143
|
-
|
|
144
|
-
```
|
|
145
|
-
index.mjs 插件入口:config / provider.models / chat.headers / auth(登录、refresh、usage、loader)钩子
|
|
146
|
-
opencode.mjs Magpie 与 OpenCode 1.x 加载的入口
|
|
147
|
-
opencode2.mjs OpenCode 2.x 的入口(Magpie 社区仓库提供的适配器)
|
|
148
|
-
plugin/oauth.mjs 发现、IDC client 注册、PKCE、换 token、refresh、user-info
|
|
149
|
-
plugin/login.mjs 本机回调端口上的浏览器登录
|
|
150
|
-
plugin/upstream.mjs 一轮对话的 WebSocket 收发
|
|
151
|
-
plugin/wire.mjs AIDP / Gateway 帧、AWS event-stream、CBOR
|
|
152
|
-
plugin/qbs.mjs 账号接口:额度、设置、附件上传
|
|
153
|
-
plugin/threads.mjs 请求与服务端线程的对应关系
|
|
154
|
-
plugin/messages.mjs OpenAI 消息 → user_message 文本与 custom_prompt
|
|
155
|
-
plugin/toolbridge.mjs 客户端工具的文本协议与流式解析
|
|
156
|
-
plugin/chat.mjs chat completions 的 JSON / SSE 回复
|
|
157
|
-
test/ 单元测试、模拟上游、端到端测试
|
|
40
|
+
npx magpie-aws-quick-bridge login # 1. 登录(企业账号加 --enterprise --region <区域>)
|
|
41
|
+
npx magpie-aws-quick-bridge serve # 2. 启动本机 API,这个窗口要一直开着
|
|
42
|
+
npx magpie-aws-quick-bridge ccswitch --open # 3. 另开一个终端,导入 CC Switch,每个应用点一次确认
|
|
158
43
|
```
|
|
159
44
|
|
|
160
|
-
|
|
45
|
+
然后在 CC Switch 里切到 **Amazon Quick**。第 3 步会给 Claude Code、Codex、OpenCode 各加一个;只要其中一个就加 `--app claude`(或 `codex`、`opencode`)。
|
|
161
46
|
|
|
162
|
-
|
|
47
|
+
想手动填也行,任何能填「地址 + Key」的工具都能这么接:
|
|
163
48
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
49
|
+
| 应用 | API 地址 | 接口格式 | 模型 |
|
|
50
|
+
|---|---|---|---|
|
|
51
|
+
| Claude Code | `http://127.0.0.1:18765` | Anthropic Messages | `smart` |
|
|
52
|
+
| Codex | `http://127.0.0.1:18765/v1` | OpenAI Responses | `smart` |
|
|
53
|
+
| OpenCode 等 | `http://127.0.0.1:18765/v1` | OpenAI Chat Completions | `smart` |
|
|
167
54
|
|
|
168
|
-
|
|
55
|
+
Key 用 `npx magpie-aws-quick-bridge status` 看。
|
|
169
56
|
|
|
170
|
-
|
|
171
|
-
sb=$(mktemp -d)
|
|
172
|
-
m() { env HOME=$sb XDG_CONFIG_HOME=$sb/.config XDG_CACHE_HOME=$sb/.cache MAGPIE_ADDR=127.0.0.1:3499 magpie "$@"; }
|
|
173
|
-
m plugin add . </dev/null
|
|
174
|
-
m plugin --json # 登录方式、账号、模型、加载错误
|
|
175
|
-
m plugin login aws-quick # 真实登录
|
|
176
|
-
m provider test aws-quick # 发一个很小的请求
|
|
177
|
-
m plugin logout aws-quick
|
|
178
|
-
```
|
|
57
|
+
几件要知道的事:
|
|
179
58
|
|
|
180
|
-
|
|
59
|
+
- **`serve` 得一直开着**,关了 agent 就连不上。macOS 上可以先 `npm install -g magpie-aws-quick-bridge`,再 `magpie-aws-quick-bridge service install` 让它在后台常驻。
|
|
60
|
+
- CC Switch 里**不用开「本地路由」**,每个应用拿到的已经是它自己的接口格式。
|
|
61
|
+
- token 快到期会自己续;真过期了请求会报 401,重新 `login` 一次就行,不用重启 `serve`。
|
|
62
|
+
- 服务只听 `127.0.0.1`,每个请求都要带 Key。别把它放到公网上。
|
|
63
|
+
- Claude Code 启动时会提示不认识 `smart` 这个模型名,不影响使用。
|
|
64
|
+
- Gemini CLI 用的是另一种接口,暂时不支持。
|
|
181
65
|
|
|
182
|
-
|
|
183
|
-
bun test/mock-upstream.mjs 39100 # 会打印下面要用的选项
|
|
184
|
-
m plugin options magpie-AWS-Quick-bridge '{"oidc_base":"http://127.0.0.1:39100","discovery_url":"http://127.0.0.1:39100/sn","data_plane_url":"http://127.0.0.1:39100"}'
|
|
185
|
-
m plugin login aws-quick
|
|
186
|
-
m provider test aws-quick
|
|
187
|
-
```
|
|
66
|
+
其他命令:`status`(地址、Key、账号、额度)、`serve --port <端口>`、`key --new`(换 Key)、`service uninstall`、`logout`。
|
|
188
67
|
|
|
189
|
-
##
|
|
68
|
+
## 模型
|
|
190
69
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
70
|
+
| 模型 | 说明 |
|
|
71
|
+
|---|---|
|
|
72
|
+
| `smart` | 最强的一档,默认用它 |
|
|
73
|
+
| `balanced` | 中间档 |
|
|
74
|
+
| `fast` | 最快的一档 |
|
|
75
|
+
| `auto` | 让 Amazon Quick 自己选 |
|
|
76
|
+
| `advanced` | 账号开通了才有 |
|
|
195
77
|
|
|
196
|
-
|
|
197
|
-
{ "package": "magpie-aws-quick-bridge", "name": "Amazon Quick", "providers": ["aws-quick"],
|
|
198
|
-
"icon": "https://raw.githubusercontent.com/DaBinBinah/magpie-AWS-Quick-bridge/main/assets/icon-128.png",
|
|
199
|
-
"summary": { "en": "Amazon Quick (QuickWork) with browser sign-in for personal and enterprise accounts: streaming chat and client tool calls. Unofficial.",
|
|
200
|
-
"zh": "浏览器登录 Amazon Quick(QuickWork)个人或企业账号,支持流式对话与客户端工具调用。非官方。" } }
|
|
201
|
-
```
|
|
78
|
+
在 Magpie 里前面加 `aws-quick/`。在我的账号上 fast、balanced、smart 背后分别是 Claude Haiku、Sonnet、Opus,别的账号可能不一样。
|
|
202
79
|
|
|
203
|
-
|
|
80
|
+
## 能做什么
|
|
204
81
|
|
|
205
|
-
|
|
82
|
+
- 流式回复、思考过程、token 用量
|
|
83
|
+
- 工具调用:读写文件、跑命令这些 agent 的工具都能用
|
|
84
|
+
- 多轮对话接着上一轮的线程走,只发新增的内容
|
|
85
|
+
- 很长的提示词和工具结果、图片会自动转成附件,不会因为 Amazon Quick 的 64 KB 单帧上限而失败
|
|
86
|
+
- 额度查询,模型列表按账号实际开通的来
|
|
206
87
|
|
|
207
|
-
##
|
|
88
|
+
## 我验证到了哪一步
|
|
208
89
|
|
|
209
|
-
-
|
|
210
|
-
-
|
|
211
|
-
-
|
|
212
|
-
- Amazon Quick
|
|
213
|
-
- 内容转成附件后,模型要先用它自己的文件工具读取,这一轮会慢一些。
|
|
214
|
-
- 线程缓存在插件进程的内存里,Magpie 重启后下一次请求会重发全部历史。
|
|
215
|
-
- 工具桥接是文本协议,模型偶尔可能不遵守;回复以 `{`、`[` 或反引号开头时会先整段缓冲,以判断它是不是一个没带标记的工具调用。
|
|
216
|
-
- 代理:按 Magpie 的文档,插件用 `fetch` 发出的请求(登录、续期)走 Magpie 给供应商或账号设置的代理。聊天用的是 WebSocket,它是否也走这个代理没有验证过;需要时用 `proxy` 选项明确指定(如 `http://127.0.0.1:7890`)。
|
|
217
|
-
- OpenCode 2.x 的入口(`opencode2.mjs`)只通过了 Magpie 社区仓库的检查脚本,没有在 OpenCode 2.x 里实际运行过。
|
|
218
|
-
- 上游接口可能随官方客户端更新而变化;登录或聊天失败时先重新登录,必要时调整 `app_version`。
|
|
90
|
+
- **真实账号(我自己的个人账号)**:Magpie 插件的登录、对话、工具调用、线程复用、附件、额度;CC Switch 模式下 Claude Code、Codex、OpenCode 经本机 API 读文件、写文件。
|
|
91
|
+
- **只用模拟服务测过**:企业账号、`advanced` 模型、图片附件。
|
|
92
|
+
- 工具调用是靠提示词约定的文本协议做的,模型偶尔会不按约定来,重试一下通常就好。
|
|
93
|
+
- Amazon Quick 每轮会带上它自己的一大段系统提示词,所以 token 数看起来很大;额度按它的 credits 算。
|
|
219
94
|
|
|
220
|
-
|
|
95
|
+
更多细节(全部选项、远程登录、工作方式、测试、已知限制)在 [docs/DEVELOPMENT.md](https://github.com/DaBinBinah/magpie-AWS-Quick-bridge/blob/main/docs/DEVELOPMENT.md)。
|
|
221
96
|
|
|
222
|
-
|
|
97
|
+
## 找我
|
|
223
98
|
|
|
224
|
-
- X
|
|
225
|
-
- GitHub:[DaBinBinah](https://github.com/DaBinBinah)
|
|
99
|
+
用着有问题或者有想法,直接提 [issue](https://github.com/DaBinBinah/magpie-AWS-Quick-bridge/issues),或者在 X 上找我:[@yedabinbinah](https://x.com/yedabinbinah)。
|
|
226
100
|
|
|
227
101
|
## License
|
|
228
102
|
|
|
229
103
|
[GNU General Public License v3.0](LICENSE)。
|
|
230
104
|
|
|
231
|
-
|
|
105
|
+
这个项目(`index.mjs`、`plugin/`、`gateway/`、`bin/`、`test/`)© 2026 DaBinBinah。登录流程、协议编解码和工具桥接移植自 [aws-quickwork-2api](https://github.com/jasonxu114514/aws-quickwork-2api)(© Jason Xu,GPL-3.0);按 GPL-3.0 的要求保留这条来源说明,并以同一许可证发布。
|
|
232
106
|
|
|
233
107
|
`opencode2.mjs` 是 [magpie-community/plugins](https://github.com/magpie-community/plugins) 提供的 OpenCode 2.x 适配器,原样收录(社区收录的检查要求逐字节一致),MIT 许可证,见 [LICENSES/opencode2-MIT.txt](LICENSES/opencode2-MIT.txt)。
|
package/bin/cli.mjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// magpie-aws-quick-bridge on its own: Amazon Quick behind a local API
|
|
3
|
+
// address, for CC Switch and any agent that takes an address and a key.
|
|
4
|
+
// magpie users do not need this: magpie loads the plugin itself.
|
|
5
|
+
|
|
6
|
+
import { spawn } from "node:child_process"
|
|
7
|
+
import { readFileSync, realpathSync } from "node:fs"
|
|
8
|
+
import { createInterface } from "node:readline"
|
|
9
|
+
import { fileURLToPath } from "node:url"
|
|
10
|
+
import { APPS, endpoint, importLink } from "../gateway/ccswitch.mjs"
|
|
11
|
+
import { createGateway, listen } from "../gateway/server.mjs"
|
|
12
|
+
import * as service from "../gateway/service.mjs"
|
|
13
|
+
import * as store from "../gateway/store.mjs"
|
|
14
|
+
import { browserSignIn } from "../plugin/login.mjs"
|
|
15
|
+
import * as oauth from "../plugin/oauth.mjs"
|
|
16
|
+
import * as qbs from "../plugin/qbs.mjs"
|
|
17
|
+
import { settings } from "../plugin/settings.mjs"
|
|
18
|
+
|
|
19
|
+
const NAME = "magpie-aws-quick-bridge"
|
|
20
|
+
const version = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version
|
|
21
|
+
|
|
22
|
+
const HELP = `${NAME} ${version}: Amazon Quick as a local API for CC Switch, Claude Code, Codex and OpenCode
|
|
23
|
+
|
|
24
|
+
${NAME} login sign in to Amazon Quick in the browser
|
|
25
|
+
--enterprise an organization's account (IAM Identity Center / SSO)
|
|
26
|
+
--region <region> its AWS region (default us-east-1)
|
|
27
|
+
--no-open print the sign-in address instead of opening it
|
|
28
|
+
${NAME} serve run the API on http://127.0.0.1:${store.DEFAULT_PORT}
|
|
29
|
+
--port <n> --host <h>
|
|
30
|
+
${NAME} ccswitch print the links that add Amazon Quick to CC Switch
|
|
31
|
+
--open open them in CC Switch now
|
|
32
|
+
--app <app> only for ${Object.keys(APPS).join(", ")}
|
|
33
|
+
${NAME} status the address, the key, the account and its quota
|
|
34
|
+
${NAME} key --new replace the API key
|
|
35
|
+
${NAME} service install macOS: keep the API running in the background
|
|
36
|
+
${NAME} service uninstall
|
|
37
|
+
${NAME} logout forget the account
|
|
38
|
+
|
|
39
|
+
Settings and the account are kept in ${store.home()}
|
|
40
|
+
`
|
|
41
|
+
|
|
42
|
+
function parse(argv) {
|
|
43
|
+
const flags = {}
|
|
44
|
+
const rest = []
|
|
45
|
+
for (let i = 0; i < argv.length; i++) {
|
|
46
|
+
const a = argv[i]
|
|
47
|
+
if (!a.startsWith("--")) rest.push(a)
|
|
48
|
+
else if (a.includes("=")) flags[a.slice(2, a.indexOf("="))] = a.slice(a.indexOf("=") + 1)
|
|
49
|
+
else if (i + 1 < argv.length && !argv[i + 1].startsWith("--") && !["open", "no-open", "enterprise", "new", "help", "version"].includes(a.slice(2))) flags[a.slice(2)] = argv[++i]
|
|
50
|
+
else flags[a.slice(2)] = true
|
|
51
|
+
}
|
|
52
|
+
return { flags, rest }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const open = (target) => {
|
|
56
|
+
const [cmd, args] = process.platform === "darwin" ? ["open", [target]] : process.platform === "win32" ? ["cmd", ["/c", "start", "", target]] : ["xdg-open", [target]]
|
|
57
|
+
try {
|
|
58
|
+
spawn(cmd, args, { stdio: "ignore", detached: true })
|
|
59
|
+
.on("error", () => {})
|
|
60
|
+
.unref()
|
|
61
|
+
} catch {}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const base = (cfg) => `http://${cfg.host === "0.0.0.0" || cfg.host === "::" ? "127.0.0.1" : cfg.host}:${cfg.port}`
|
|
65
|
+
|
|
66
|
+
function setup() {
|
|
67
|
+
const cfg = store.config()
|
|
68
|
+
oauth.setEndpoints({ oidcBase: cfg.options.oidc_base, discoveryUrl: cfg.options.discovery_url, dataPlaneUrl: cfg.options.data_plane_url })
|
|
69
|
+
return cfg
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function login(flags) {
|
|
73
|
+
const cfg = setup()
|
|
74
|
+
const options = settings(cfg.options)
|
|
75
|
+
const provider = typeof flags.provider === "string" ? flags.provider : flags.enterprise ? "enterprise" : "social"
|
|
76
|
+
const started = await browserSignIn({ provider, region: typeof flags.region === "string" ? flags.region : "" }, options)
|
|
77
|
+
if (started.type === "failed") throw new Error(started.error)
|
|
78
|
+
// the sign-in's own port and timers never keep the process alive
|
|
79
|
+
const alive = setInterval(() => {}, 60_000)
|
|
80
|
+
console.log(`Sign in to Amazon Quick in the browser:\n\n ${started.url}\n`)
|
|
81
|
+
if (!flags["no-open"]) open(started.url)
|
|
82
|
+
|
|
83
|
+
// A browser on another computer ends on a localhost page that won't load
|
|
84
|
+
// there: its address, pasted here, is opened on this computer instead.
|
|
85
|
+
let rl
|
|
86
|
+
if (process.stdin.isTTY) {
|
|
87
|
+
console.log("Browser on another computer? Paste the address of each localhost page that won't load here, one a line.\n")
|
|
88
|
+
rl = createInterface({ input: process.stdin })
|
|
89
|
+
rl.on("line", async (line) => {
|
|
90
|
+
let url
|
|
91
|
+
try {
|
|
92
|
+
url = new URL(line.trim())
|
|
93
|
+
} catch {
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
if (!["localhost", "127.0.0.1"].includes(url.hostname)) return
|
|
97
|
+
url.hostname = "127.0.0.1"
|
|
98
|
+
try {
|
|
99
|
+
const res = await fetch(url, { redirect: "manual" })
|
|
100
|
+
const next = res.headers.get("location")
|
|
101
|
+
if (next) console.log(`\nNow open this page, and paste the address it ends on:\n\n ${next}\n`)
|
|
102
|
+
} catch (e) {
|
|
103
|
+
console.log(`That address could not be opened: ${e.message}`)
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
const result = await started.callback()
|
|
108
|
+
rl?.close()
|
|
109
|
+
clearInterval(alive)
|
|
110
|
+
if (result?.type !== "success") throw new Error(result?.error ?? "the sign-in failed")
|
|
111
|
+
const { type: _type, ...fields } = result
|
|
112
|
+
store.saveAccount({ ...fields, type: "oauth" })
|
|
113
|
+
console.log(`Signed in as ${fields.accountId || "your account"}.\nNext: ${NAME} serve`)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function serve(flags) {
|
|
117
|
+
const cfg = setup()
|
|
118
|
+
const port = flags.port ? Number(flags.port) : cfg.port
|
|
119
|
+
const host = typeof flags.host === "string" ? flags.host : cfg.host
|
|
120
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Error("--port takes a number from 0 to 65535")
|
|
121
|
+
const account = new store.Account()
|
|
122
|
+
const log = (line) => console.log(`${new Date().toISOString()} ${line}`)
|
|
123
|
+
let running
|
|
124
|
+
try {
|
|
125
|
+
running = await listen(createGateway({ apiKey: cfg.apiKey, account, options: cfg.options, log }), { port, host })
|
|
126
|
+
} catch (e) {
|
|
127
|
+
throw new Error(e?.code === "EADDRINUSE" ? `port ${port} is in use: the API may be running already (${NAME} status), or pick another with --port` : e.message)
|
|
128
|
+
}
|
|
129
|
+
const at = base({ host, port: running.port })
|
|
130
|
+
console.log(`Amazon Quick API on ${at}\n key: ${cfg.apiKey}\n account: ${account.peek()?.accountId ?? `not signed in (${NAME} login)`}\n Claude Code ${endpoint("claude", at)} (Anthropic Messages)\n Codex ${endpoint("codex", at)} (OpenAI Responses)\n OpenCode, … ${endpoint("opencode", at)} (OpenAI Chat Completions)\nAdd it to CC Switch: ${NAME} ccswitch --open\n`)
|
|
131
|
+
const stop = () => {
|
|
132
|
+
running.close()
|
|
133
|
+
process.exit(0)
|
|
134
|
+
}
|
|
135
|
+
process.on("SIGINT", stop)
|
|
136
|
+
process.on("SIGTERM", stop)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function status() {
|
|
140
|
+
const cfg = setup()
|
|
141
|
+
const at = base(cfg)
|
|
142
|
+
let up = false
|
|
143
|
+
try {
|
|
144
|
+
up = (await (await fetch(`${at}/health`, { signal: AbortSignal.timeout(2000) })).json())?.name === NAME
|
|
145
|
+
} catch {}
|
|
146
|
+
console.log(`API: ${at} (${up ? "running" : `not running; start it with ${NAME} serve`})`)
|
|
147
|
+
console.log(`Key: ${cfg.apiKey}`)
|
|
148
|
+
if (process.platform === "darwin") console.log(`Service: ${service.installed() ? "installed (starts at login)" : "not installed"}`)
|
|
149
|
+
const account = new store.Account()
|
|
150
|
+
let auth = account.peek()
|
|
151
|
+
if (!auth) return console.log(`Account: not signed in (${NAME} login)`)
|
|
152
|
+
try {
|
|
153
|
+
auth = await account.get()
|
|
154
|
+
console.log(`Account: ${auth.accountId}`)
|
|
155
|
+
} catch (e) {
|
|
156
|
+
return console.log(`Account: ${auth.accountId}: ${e.message}`)
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const usage = await qbs.usage(auth)
|
|
160
|
+
console.log(`Plan: ${usage.plan}`)
|
|
161
|
+
for (const w of usage.windows) console.log(` ${w.name}: ${w.display ?? `${w.used}% used`}`)
|
|
162
|
+
} catch (e) {
|
|
163
|
+
console.log(`Quota: not available (${e.message})`)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function ccswitch(flags) {
|
|
168
|
+
const cfg = setup()
|
|
169
|
+
const apps = typeof flags.app === "string" ? flags.app.split(",") : Object.keys(APPS)
|
|
170
|
+
const at = typeof flags.url === "string" ? flags.url : base(cfg)
|
|
171
|
+
for (const app of apps) {
|
|
172
|
+
const link = importLink(app.trim(), { base: at, apiKey: cfg.apiKey, model: typeof flags.model === "string" ? flags.model : "smart" })
|
|
173
|
+
console.log(`${APPS[app.trim()].label}:\n ${link}\n`)
|
|
174
|
+
if (flags.open) open(link)
|
|
175
|
+
}
|
|
176
|
+
console.log(flags.open ? "CC Switch asks you to confirm each one." : `Open a link in the browser, or run this again with --open, and confirm in CC Switch.`)
|
|
177
|
+
console.log(`CC Switch only points the agent at ${at}: keep \`${NAME} serve\` running.`)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function main() {
|
|
181
|
+
const [command = "help", ...argv] = process.argv.slice(2)
|
|
182
|
+
const { flags, rest } = parse(argv)
|
|
183
|
+
if (command === "--version" || command === "-v" || command === "version") return console.log(version)
|
|
184
|
+
if (command === "login") return login(flags)
|
|
185
|
+
if (command === "serve") return serve(flags)
|
|
186
|
+
if (command === "status") return status()
|
|
187
|
+
if (command === "ccswitch" || command === "cc-switch") return ccswitch(flags)
|
|
188
|
+
if (command === "key") return console.log(flags.new ? `${store.newKey()}\nRestart serve, and give the agents the new key.` : store.config().apiKey)
|
|
189
|
+
if (command === "logout") {
|
|
190
|
+
store.removeAccount()
|
|
191
|
+
return console.log("The account is forgotten on this computer.")
|
|
192
|
+
}
|
|
193
|
+
if (command === "service") {
|
|
194
|
+
if (rest[0] === "install") {
|
|
195
|
+
service.install({ runtime: process.execPath, script: realpathSync(fileURLToPath(import.meta.url)) })
|
|
196
|
+
return console.log(`The API now runs in the background and starts at login.\nLog: ${service.logPath()}`)
|
|
197
|
+
}
|
|
198
|
+
if (rest[0] === "uninstall") {
|
|
199
|
+
service.uninstall()
|
|
200
|
+
return console.log("The background service is removed.")
|
|
201
|
+
}
|
|
202
|
+
throw new Error("service takes install or uninstall")
|
|
203
|
+
}
|
|
204
|
+
console.log(HELP)
|
|
205
|
+
if (!["help", "--help", "-h"].includes(command)) process.exitCode = 1
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
main().catch((e) => {
|
|
209
|
+
console.error(`${NAME}: ${e?.message ?? e}`)
|
|
210
|
+
process.exit(1)
|
|
211
|
+
})
|