magpie-su8codes 0.0.0-stage → 0.0.1-beta.2
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/LICENSE +21 -0
- package/README.md +194 -2
- package/index.mjs +240 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SU8 Codes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,195 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<img src="https://www.su8.codes/favicon.svg" width="88" height="88" alt="SU8 Codes">
|
|
4
|
+
|
|
5
|
+
# SU8 Codes for magpie
|
|
6
|
+
|
|
7
|
+
**在 magpie 里用 SU8:粘贴一个 Key,所有 Agent 都能用上 SU8 的模型。**
|
|
8
|
+
|
|
9
|
+
[](https://github.com/SU8-Codes/magpie-su8/actions/workflows/test.yml)
|
|
10
|
+
[](LICENSE)
|
|
11
|
+
|
|
12
|
+
中文 · [English](#english)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
[magpie](https://usemagpie.ai) 能把 Claude Code、Codex、OpenCode 等编程 Agent 统一接到你自己的模型来源上。装上这个插件后,你的 SU8 账号就是 magpie 里的一个供应商:
|
|
19
|
+
|
|
20
|
+
- 🔑 **一个 Key 就能用**:粘贴 SU8 的 API Key 即可,不用手动填接口地址和协议。
|
|
21
|
+
- 🧠 **模型自动同步**:列出这个 Key 能用的模型,带上下文长度和思考档位。
|
|
22
|
+
- 💰 **额度一眼看清**:账号卡片显示套餐今天还剩多少、钱包余额多少。
|
|
23
|
+
- 🌏 **五条线路可选**:主站、CN2、香港、日本、美国,挑网络最快的一条。
|
|
24
|
+
- ⚡ **长对话更省**:同一段对话尽量走同一条通道,缓存命中更高。
|
|
25
|
+
|
|
26
|
+
## 三步开始
|
|
27
|
+
|
|
28
|
+
### 1. 安装插件
|
|
29
|
+
|
|
30
|
+
在 magpie 里打开 **插件 › 发现**,搜索 `magpie-su8codes`,点安装。
|
|
31
|
+
|
|
32
|
+
也可以用命令行:
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
magpie plugin add magpie-su8codes
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
想试测试版(`beta` 标签,不影响正式用户):`magpie plugin add magpie-su8codes@beta`。
|
|
39
|
+
|
|
40
|
+
或者直接从 GitHub 安装最新版:`magpie plugin add github:SU8-Codes/magpie-su8`
|
|
41
|
+
|
|
42
|
+
### 2. 准备一个 Key
|
|
43
|
+
|
|
44
|
+
登录 [SU8 控制台](https://www.su8.codes/console/keys),在 **密钥管理** 里新建或复制一个 API Key(以 `su8-` 开头)。
|
|
45
|
+
|
|
46
|
+
### 3. 登录
|
|
47
|
+
|
|
48
|
+
在 magpie 的 **供应商** 页面点 **添加供应商**,在 **来自插件** 里选 **SU8 Codes**:先选线路,再粘贴 Key。
|
|
49
|
+
|
|
50
|
+
也可以用命令行:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
magpie plugin login su8
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
登录后,SU8 的模型就会出现在 magpie 的模型列表里,名字形如:
|
|
57
|
+
|
|
58
|
+
```text
|
|
59
|
+
su8/claude-sonnet-5-5
|
|
60
|
+
su8/gpt-6.1-sol
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 选择线路
|
|
64
|
+
|
|
65
|
+
五条线路连的是同一个 SU8 账号,额度、余额、Key 都通用,只是网络入口不同。哪条在你那里最快就用哪条。
|
|
66
|
+
|
|
67
|
+
| 线路 | 地址 | 说明 |
|
|
68
|
+
| --- | --- | --- |
|
|
69
|
+
| 主站(默认) | `www.su8.codes` | 大多数用户直接用这条 |
|
|
70
|
+
| CN2 | `cn2.su8.codes` | 中国大陆网络优化 |
|
|
71
|
+
| 香港 | `hk.su8.codes` | 海外节点,选离你近的 |
|
|
72
|
+
| 日本 | `jp.su8.codes` | 海外节点,选离你近的 |
|
|
73
|
+
| 美国 | `us2.su8.codes` | 海外节点,选离你近的 |
|
|
74
|
+
|
|
75
|
+
**想换线路?** 用同一个 Key 重新登录一次,选新的线路就行。magpie 会替换原来的登录,不会多出一个账号。
|
|
76
|
+
|
|
77
|
+
## 用量和余额
|
|
78
|
+
|
|
79
|
+
账号卡片上会显示:
|
|
80
|
+
|
|
81
|
+
- **每个套餐**:今天还剩多少额度(含前一天结转的部分)。
|
|
82
|
+
- **钱包余额**:按量付费可用的余额。
|
|
83
|
+
|
|
84
|
+
这些数字只用来展示,请求能不能用以 SU8 为准。每次调用的明细和费用,请到 SU8 控制台的 [调用记录](https://www.su8.codes/console/usage) 查看。
|
|
85
|
+
|
|
86
|
+
## 常见问题
|
|
87
|
+
|
|
88
|
+
<details>
|
|
89
|
+
<summary><b>登录后提示 Key 被拒绝</b></summary>
|
|
90
|
+
|
|
91
|
+
Key 可能被停用、已过期或已删除。到 SU8 控制台的 [密钥管理](https://www.su8.codes/console/keys) 检查这个 Key,确认可用后重新登录:`magpie plugin login su8`。
|
|
92
|
+
</details>
|
|
93
|
+
|
|
94
|
+
<details>
|
|
95
|
+
<summary><b>模型列表是空的,或者少了某个模型</b></summary>
|
|
96
|
+
|
|
97
|
+
插件只列出这个 Key 能调用的模型。请在 SU8 控制台确认:Key 绑定了你要的模型,并且有可用的套餐或余额。
|
|
98
|
+
</details>
|
|
99
|
+
|
|
100
|
+
<details>
|
|
101
|
+
<summary><b>请求很慢或者连不上</b></summary>
|
|
102
|
+
|
|
103
|
+
换一条线路试试:用同一个 Key 重新登录,选另一条线路。如果你的 magpie 设置了代理,也确认代理能访问对应的线路地址。
|
|
104
|
+
</details>
|
|
105
|
+
|
|
106
|
+
<details>
|
|
107
|
+
<summary><b>供应商显示成 <code>su8-plugin</code></b></summary>
|
|
108
|
+
|
|
109
|
+
说明你之前在 magpie 里手动添加过一个 id 叫 `su8` 的供应商。删掉手动添加的那个(`magpie provider rm su8`),插件就会恢复成 `su8`。
|
|
110
|
+
</details>
|
|
111
|
+
|
|
112
|
+
<details>
|
|
113
|
+
<summary><b>magpie 的用量页为什么算不出花了多少钱</b></summary>
|
|
114
|
+
|
|
115
|
+
SU8 的模型列表不带价格,所以 magpie 没法自己估算费用。实际扣费以 SU8 控制台的 [调用记录](https://www.su8.codes/console/usage) 为准。
|
|
116
|
+
</details>
|
|
117
|
+
|
|
118
|
+
## 安全与隐私
|
|
119
|
+
|
|
120
|
+
- **Key 只发给 SU8**:只会发往你选择的那条 SU8 线路,发往其他任何地址的请求都会被插件直接拒绝。
|
|
121
|
+
- **不暴露给其他插件**:magpie 会把插件声明的 Key 交给本机所有插件看,所以本插件对外只声明一个占位符,真正的 Key 只在发往 SU8 的那一刻才填上。
|
|
122
|
+
- **不收集任何数据**:没有统计上报,没有第三方依赖,也不在你的电脑上写任何文件。
|
|
123
|
+
- **Key 存在哪**:和 magpie 里其他登录信息一样,由 magpie 保存在它的配置目录(`~/.config/magpie/plugin-auth.json`)。
|
|
124
|
+
|
|
125
|
+
## 需要帮助
|
|
126
|
+
|
|
127
|
+
- SU8 控制台的 [帮助与支持](https://www.su8.codes/console/support)
|
|
128
|
+
- 插件问题:在本仓库提 [Issue](https://github.com/SU8-Codes/magpie-su8/issues)
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## English
|
|
133
|
+
|
|
134
|
+
<div align="center">
|
|
135
|
+
|
|
136
|
+
**Use SU8 in magpie: paste one key, and every agent can use SU8's models.**
|
|
137
|
+
|
|
138
|
+
</div>
|
|
139
|
+
|
|
140
|
+
[magpie](https://usemagpie.ai) connects coding agents such as Claude Code, Codex and OpenCode to the model sources you choose. With this plugin your SU8 account becomes a magpie provider:
|
|
141
|
+
|
|
142
|
+
- 🔑 **One key is all it takes**: no base URLs or protocols to fill in.
|
|
143
|
+
- 🧠 **Models stay in sync**: the models your key can use, with context windows and reasoning levels.
|
|
144
|
+
- 💰 **Allowance at a glance**: each plan's allowance left today and your wallet balance, on the account card.
|
|
145
|
+
- 🌏 **Five lines**: Main, CN2, Hong Kong, Japan and United States. Pick the fastest for you.
|
|
146
|
+
- ⚡ **Long conversations cost less**: a conversation stays on one upstream, so its prompt cache is reused.
|
|
147
|
+
|
|
148
|
+
### Get started
|
|
149
|
+
|
|
150
|
+
```sh
|
|
151
|
+
magpie plugin add magpie-su8codes # or: Plugins › Discover, search "magpie-su8codes"
|
|
152
|
+
magpie plugin login su8 # pick a line, then paste an API key (su8-…)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
To install the latest version straight from GitHub: `magpie plugin add github:SU8-Codes/magpie-su8`
|
|
156
|
+
|
|
157
|
+
Get a key from the **API keys** page of the [SU8 console](https://www.su8.codes/console/keys). Models are then named like `su8/claude-sonnet-5-5`.
|
|
158
|
+
|
|
159
|
+
### Lines
|
|
160
|
+
|
|
161
|
+
All five lines reach the same SU8 account, with the same keys, plans and balance. Only the network route differs.
|
|
162
|
+
|
|
163
|
+
| Line | Address | Notes |
|
|
164
|
+
| --- | --- | --- |
|
|
165
|
+
| Main (default) | `www.su8.codes` | Right for most users |
|
|
166
|
+
| CN2 | `cn2.su8.codes` | Tuned for mainland China networks |
|
|
167
|
+
| Hong Kong | `hk.su8.codes` | Overseas: pick the one nearest you |
|
|
168
|
+
| Japan | `jp.su8.codes` | Overseas: pick the one nearest you |
|
|
169
|
+
| United States | `us2.su8.codes` | Overseas: pick the one nearest you |
|
|
170
|
+
|
|
171
|
+
To switch lines, sign in again with the same key and pick another line. magpie replaces the old sign-in instead of adding a second account.
|
|
172
|
+
|
|
173
|
+
### Good to know
|
|
174
|
+
|
|
175
|
+
- Allowance and balance are shown for your information; SU8 decides whether a request is served. See each call and its cost under **Usage** in the [SU8 console](https://www.su8.codes/console/usage).
|
|
176
|
+
- SU8's model list carries no prices, so magpie can't estimate cost itself.
|
|
177
|
+
- If the provider shows as `su8-plugin`, you added a custom provider with the id `su8` earlier. Remove it (`magpie provider rm su8`) and the plugin becomes `su8`.
|
|
178
|
+
|
|
179
|
+
### Security and privacy
|
|
180
|
+
|
|
181
|
+
- Your key is sent only to the SU8 line you picked; the plugin refuses to send it anywhere else.
|
|
182
|
+
- magpie shows a plugin's declared key to every installed plugin, so this plugin declares a placeholder and sets the real key only on requests to SU8.
|
|
183
|
+
- No telemetry, no runtime dependencies, nothing written to your disk. magpie keeps the key with its other sign-ins, in `~/.config/magpie/plugin-auth.json`.
|
|
184
|
+
|
|
185
|
+
### Help
|
|
186
|
+
|
|
187
|
+
[Help & support](https://www.su8.codes/console/support) in the SU8 console, or open an [issue](https://github.com/SU8-Codes/magpie-su8/issues) for the plugin.
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
<div align="center">
|
|
192
|
+
|
|
193
|
+
MIT License · The logo belongs to SU8 Codes
|
|
194
|
+
|
|
195
|
+
</div>
|
package/index.mjs
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// SU8 Codes as a magpie provider: sign in with an SU8 API key, use the models
|
|
2
|
+
// that key may call, and see its plan allowance and balance on the account card.
|
|
3
|
+
//
|
|
4
|
+
// Only Su8CodesPlugin is exported as a function: magpie runs every exported
|
|
5
|
+
// function as a plugin, so helpers live on _internal at the end.
|
|
6
|
+
|
|
7
|
+
const PROVIDER = "su8"
|
|
8
|
+
// the lines a key may be used on: the same service and account, reached through different networks
|
|
9
|
+
const LINES = {
|
|
10
|
+
main: { origin: "https://www.su8.codes", label: "Main", hint: "Default", labels: { zh: "主站" }, hints: { zh: "默认" } },
|
|
11
|
+
cn2: { origin: "https://cn2.su8.codes", label: "CN2", hint: "For mainland China", labels: { zh: "CN2" }, hints: { zh: "中国大陆网络" } },
|
|
12
|
+
hk: { origin: "https://hk.su8.codes", label: "Hong Kong", hint: "", labels: { zh: "香港" } },
|
|
13
|
+
jp: { origin: "https://jp.su8.codes", label: "Japan", hint: "", labels: { zh: "日本" } },
|
|
14
|
+
us2: { origin: "https://us2.su8.codes", label: "United States", hint: "", labels: { zh: "美国" } },
|
|
15
|
+
}
|
|
16
|
+
const DEFAULT_LINE = "main"
|
|
17
|
+
const ICON = "https://www.su8.codes/favicon.svg"
|
|
18
|
+
const TIMEOUT_MS = 15_000
|
|
19
|
+
const MESSAGES = "@ai-sdk/anthropic"
|
|
20
|
+
const RESPONSES = "@ai-sdk/openai"
|
|
21
|
+
const CHAT = "@ai-sdk/openai-compatible"
|
|
22
|
+
// magpie shows the loader's apiKey to every plugin's chat.headers hook, so the
|
|
23
|
+
// key itself is set in fetch, and only for SU8's origin.
|
|
24
|
+
const PLACEHOLDER = "su8-key-set-per-request"
|
|
25
|
+
const SESSION_HEADER = "x-session-id"
|
|
26
|
+
const LEVELS = ["none", "minimal", "low", "medium", "high", "xhigh", "max", "ultra"]
|
|
27
|
+
// with a client_version the list comes in Codex's shape, which carries context windows and levels
|
|
28
|
+
const MODELS_PATH = "/v1/models?client_version=0.0.0"
|
|
29
|
+
const USAGE_PATH = "/api/v1/ccs/usage"
|
|
30
|
+
|
|
31
|
+
class Refused extends Error {
|
|
32
|
+
signIn = "expired"
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function keyOf(auth) {
|
|
36
|
+
return auth?.type === "api" && typeof auth.key === "string" && auth.key.trim() ? auth.key.trim() : undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** the line an account signed in on; one signed in before lines existed is on the main line. */
|
|
40
|
+
function originOf(auth) {
|
|
41
|
+
const line = auth?.metadata?.line ?? DEFAULT_LINE
|
|
42
|
+
const known = LINES[line]
|
|
43
|
+
if (!known) throw new Error(`Unknown SU8 line "${line}". Sign in to SU8 again and pick a line from the list.`)
|
|
44
|
+
return known.origin
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** a model that only draws. */
|
|
48
|
+
function drawsOnly(id) {
|
|
49
|
+
return /(^|[-_])image([-_]|$)/i.test(String(id))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** the API a model is spoken to on; magpie translates the others to it. */
|
|
53
|
+
function npmOf(id) {
|
|
54
|
+
const s = String(id).toLowerCase()
|
|
55
|
+
// magpie draws only with a model on an OpenAI-compatible base
|
|
56
|
+
if (drawsOnly(s)) return CHAT
|
|
57
|
+
if (s.startsWith("claude-")) return MESSAGES
|
|
58
|
+
if (/^(gpt-|o\d|codex-)/.test(s)) return RESPONSES
|
|
59
|
+
return CHAT
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function ask(url, key) {
|
|
63
|
+
let res
|
|
64
|
+
try {
|
|
65
|
+
res = await fetch(url, {
|
|
66
|
+
headers: { authorization: `Bearer ${key}`, accept: "application/json" },
|
|
67
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
68
|
+
})
|
|
69
|
+
} catch (e) {
|
|
70
|
+
throw new Error(`Couldn't reach SU8 at ${new URL(url).origin} (${e?.message ?? e}). Check your connection or proxy.`)
|
|
71
|
+
}
|
|
72
|
+
const text = await res.text()
|
|
73
|
+
let body
|
|
74
|
+
try {
|
|
75
|
+
body = JSON.parse(text)
|
|
76
|
+
} catch {
|
|
77
|
+
body = undefined
|
|
78
|
+
}
|
|
79
|
+
if (res.status === 401) {
|
|
80
|
+
throw new Refused("SU8 refused this API key: it may be disabled, expired or deleted. Check it on the API keys page of the SU8 console, then sign in again.")
|
|
81
|
+
}
|
|
82
|
+
if (!res.ok) throw new Error(`SU8 answered HTTP ${res.status}: ${String(body?.error?.message ?? (text.trim().slice(0, 200) || res.statusText))}`)
|
|
83
|
+
if (body === undefined) throw new Error(`SU8 answered something that isn't JSON at ${new URL(url).pathname}.`)
|
|
84
|
+
return body
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function levelsOf(entry) {
|
|
88
|
+
const said = Array.isArray(entry?.supported_reasoning_levels) ? entry.supported_reasoning_levels : []
|
|
89
|
+
const efforts = new Set(said.map((x) => String(x?.effort ?? x).toLowerCase()))
|
|
90
|
+
return LEVELS.filter((l) => efforts.has(l))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function modelOf(entry, url) {
|
|
94
|
+
const id = String(entry.slug)
|
|
95
|
+
const levels = levelsOf(entry)
|
|
96
|
+
const image = Array.isArray(entry.input_modalities) && entry.input_modalities.includes("image")
|
|
97
|
+
const context = Number.isInteger(entry.context_window) && entry.context_window > 0 ? entry.context_window : 0
|
|
98
|
+
const draws = drawsOnly(id)
|
|
99
|
+
return {
|
|
100
|
+
id,
|
|
101
|
+
providerID: PROVIDER,
|
|
102
|
+
name: typeof entry.display_name === "string" && entry.display_name ? entry.display_name : id,
|
|
103
|
+
api: { id, url, npm: npmOf(id) },
|
|
104
|
+
status: "active",
|
|
105
|
+
headers: {},
|
|
106
|
+
options: {},
|
|
107
|
+
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
|
|
108
|
+
limit: { context, output: 0 },
|
|
109
|
+
capabilities: {
|
|
110
|
+
temperature: true,
|
|
111
|
+
reasoning: levels.length > 0,
|
|
112
|
+
attachment: image,
|
|
113
|
+
toolcall: !draws,
|
|
114
|
+
// image input only where SU8 says so; elsewhere magpie keeps what it knows of the model
|
|
115
|
+
input: image ? { text: true, image: true } : { text: true },
|
|
116
|
+
output: { text: !draws, image: draws },
|
|
117
|
+
interleaved: false,
|
|
118
|
+
},
|
|
119
|
+
release_date: "",
|
|
120
|
+
variants: Object.fromEntries(levels.map((l) => [l, {}])),
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** the models the key may call; a throw keeps magpie's last list and tells the user why. */
|
|
125
|
+
async function listModels(origin, key) {
|
|
126
|
+
const body = await ask(`${origin}${MODELS_PATH}`, key)
|
|
127
|
+
const entries = Array.isArray(body?.models) ? body.models : []
|
|
128
|
+
const url = `${origin}/v1`
|
|
129
|
+
const models = Object.fromEntries(
|
|
130
|
+
entries.filter((e) => typeof e?.slug === "string" && e.slug !== "").map((e) => [e.slug, modelOf(e, url)]),
|
|
131
|
+
)
|
|
132
|
+
if (Object.keys(models).length === 0) {
|
|
133
|
+
throw new Error("SU8 lists no models for this API key. Check on the SU8 console that the key has models it may call and a plan or balance it may use.")
|
|
134
|
+
}
|
|
135
|
+
return models
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function dollars(n) {
|
|
139
|
+
return `${n < 0 ? "-" : ""}$${Math.abs(n).toFixed(2)}`
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** a key's plans and wallet, as the account card shows them. */
|
|
143
|
+
function usageFrom(body) {
|
|
144
|
+
if (body?.isValid === false) {
|
|
145
|
+
const why = String(body.invalidMessage ?? "").trim()
|
|
146
|
+
return { error: `SU8 says this key can't be used now${why ? ` (${why})` : ""}. See the API keys page of the SU8 console.`, windows: [] }
|
|
147
|
+
}
|
|
148
|
+
const plans = (Array.isArray(body?.subscriptions) ? body.subscriptions : []).filter((s) => s && s.isValid !== false)
|
|
149
|
+
// shown only (aside): SU8 itself decides whether a request is served
|
|
150
|
+
const windows = plans.map((s) => {
|
|
151
|
+
const limit = Number(s.todayLimit) || 0
|
|
152
|
+
const left = Number(s.remaining) || 0
|
|
153
|
+
const window = { name: String(s.planName || s.packageCode || "Plan"), display: `${dollars(left)} left · ${dollars(limit)} a day`, aside: true }
|
|
154
|
+
if (limit > 0) window.used = Math.round(Math.max(0, 1 - left / limit) * 1000) / 10
|
|
155
|
+
return window
|
|
156
|
+
})
|
|
157
|
+
const out = { windows }
|
|
158
|
+
const balance = Number(body?.balance)
|
|
159
|
+
if (Number.isFinite(balance) && (balance !== 0 || plans.length === 0)) out.balance = dollars(balance)
|
|
160
|
+
if (plans.length === 1) out.plan = String(plans[0].planName || plans[0].packageCode || "Plan")
|
|
161
|
+
else if (plans.length > 1) out.plan = `${plans.length} plans`
|
|
162
|
+
else out.plan = "Pay as you go"
|
|
163
|
+
const until = plans.map((s) => s.expiresAt).filter(Boolean).sort()[0]
|
|
164
|
+
if (until) out.until = until
|
|
165
|
+
return out
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function usageOf(origin, key) {
|
|
169
|
+
if (!key) return { error: "Not signed in to SU8. Sign in with an API key from the SU8 console.", windows: [] }
|
|
170
|
+
try {
|
|
171
|
+
return usageFrom(await ask(`${origin}${USAGE_PATH}`, key))
|
|
172
|
+
} catch (e) {
|
|
173
|
+
if (e instanceof Refused) return { error: e.message, signIn: e.signIn, windows: [] }
|
|
174
|
+
return { error: e?.message ?? String(e), windows: [] }
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** one account's fetch: the key goes only to SU8, with the agent's session named as SU8 reads it. */
|
|
179
|
+
function fetchFor(origin, key) {
|
|
180
|
+
return async (input, init) => {
|
|
181
|
+
const target = new URL(input instanceof Request ? input.url : String(input))
|
|
182
|
+
if (target.origin !== origin) throw new Error(`SU8 plugin: refusing to send the API key to ${target.origin}`)
|
|
183
|
+
const headers = new Headers(input instanceof Request ? input.headers : undefined)
|
|
184
|
+
for (const [k, v] of new Headers(init?.headers ?? {})) headers.set(k, v)
|
|
185
|
+
headers.delete("x-api-key")
|
|
186
|
+
headers.delete("x-goog-api-key")
|
|
187
|
+
headers.set("authorization", `Bearer ${key}`)
|
|
188
|
+
const session = headers.get("x-magpie-session")
|
|
189
|
+
if (session && !headers.has(SESSION_HEADER)) headers.set(SESSION_HEADER, session)
|
|
190
|
+
return fetch(input, { ...init, headers })
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// asked before the key; magpie keeps the choice as the account's metadata.line
|
|
195
|
+
const LINE_PROMPT = {
|
|
196
|
+
type: "select",
|
|
197
|
+
key: "line",
|
|
198
|
+
message: "Line",
|
|
199
|
+
messages: { zh: "线路" },
|
|
200
|
+
options: Object.entries(LINES).map(([value, l]) => ({ value, label: l.label, labels: l.labels, hint: l.hint, ...(l.hints ? { hints: l.hints } : {}) })),
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export async function Su8CodesPlugin() {
|
|
204
|
+
return {
|
|
205
|
+
config: async (cfg) => {
|
|
206
|
+
cfg.provider ??= {}
|
|
207
|
+
cfg.provider[PROVIDER] ??= { name: "SU8 Codes", npm: CHAT, api: `${LINES[DEFAULT_LINE].origin}/v1`, models: {} }
|
|
208
|
+
},
|
|
209
|
+
auth: {
|
|
210
|
+
provider: PROVIDER,
|
|
211
|
+
icon: ICON,
|
|
212
|
+
methods: [{ type: "api", label: "SU8 API key", labels: { zh: "SU8 API Key" }, placeholder: "su8-…", prompts: [LINE_PROMPT] }],
|
|
213
|
+
loader: async (getAuth) => {
|
|
214
|
+
const auth = await getAuth()
|
|
215
|
+
const key = keyOf(auth)
|
|
216
|
+
if (!key) return {}
|
|
217
|
+
const origin = originOf(auth)
|
|
218
|
+
return { baseURL: `${origin}/v1`, apiKey: PLACEHOLDER, fetch: fetchFor(origin, key) }
|
|
219
|
+
},
|
|
220
|
+
usage: async (getAuth) => {
|
|
221
|
+
const auth = await getAuth()
|
|
222
|
+
try {
|
|
223
|
+
return await usageOf(originOf(auth), keyOf(auth))
|
|
224
|
+
} catch (e) {
|
|
225
|
+
return { error: e?.message ?? String(e), windows: [] }
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
provider: {
|
|
230
|
+
id: PROVIDER,
|
|
231
|
+
models: async (provider, { auth } = {}) => {
|
|
232
|
+
const key = keyOf(auth)
|
|
233
|
+
return key ? listModels(originOf(auth), key) : provider.models
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// for tests: an object, not a function, so magpie never runs it as a plugin
|
|
240
|
+
export const _internal = { LINES, DEFAULT_LINE, LINE_PROMPT, PLACEHOLDER, SESSION_HEADER, originOf, npmOf, drawsOnly, levelsOf, modelOf, listModels, usageFrom, usageOf, fetchFor }
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "magpie-su8codes",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1-beta.2",
|
|
4
|
+
"description": "SU8 Codes for magpie: sign in with an SU8 API key, pick one of five lines, use the models it can call, and see plan allowance and balance",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.mjs",
|
|
7
|
+
"exports": "./index.mjs",
|
|
8
|
+
"files": [
|
|
9
|
+
"index.mjs",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node --test test/*.test.mjs"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"opencode",
|
|
18
|
+
"opencode-plugin",
|
|
19
|
+
"magpie",
|
|
20
|
+
"magpie-plugin",
|
|
21
|
+
"su8",
|
|
22
|
+
"su8codes",
|
|
23
|
+
"provider"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/SU8-Codes/magpie-su8.git"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/SU8-Codes/magpie-su8#readme",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/SU8-Codes/magpie-su8/issues"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=20"
|
|
36
|
+
},
|
|
37
|
+
"magpie": {
|
|
38
|
+
"name": "SU8 Codes",
|
|
39
|
+
"maxConcurrency": 8,
|
|
40
|
+
"summary": {
|
|
41
|
+
"en": "SU8 Codes: sign in with an SU8 API key, pick one of five lines, use its models, and see plan allowance and balance.",
|
|
42
|
+
"zh": "SU8 Codes:用 SU8 的 API Key 登录,五条线路任选,使用这个 Key 能调用的模型,查看套餐额度与余额。"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|