@magpie-community/opencode-opendesign-auth 0.1.17 → 0.1.18
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 +17 -10
- package/index.mjs +65 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -289,12 +289,16 @@ that lasts longer goes before it.
|
|
|
289
289
|
The operation id is the whole of the retry safety, and it outlives the click
|
|
290
290
|
that started it — and the host that ran it. magpie runs one plugin host per
|
|
291
291
|
`magpie quota action` while the window's host lives on, so the three values are
|
|
292
|
-
also kept in `opendesign-reset-pending.json` in
|
|
293
|
-
`~/.magpie/opendesign/` — not in the project
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
292
|
+
also kept in `opendesign-reset-pending.json` in a directory of this plugin's
|
|
293
|
+
own, `~/.magpie/opendesign/` under the user's home — not in the project, and not
|
|
294
|
+
in magpie's config directory (`$XDG_CONFIG_HOME/magpie`, by default
|
|
295
|
+
`~/.config/magpie`), which the Dock app and the command line can resolve
|
|
296
|
+
differently. The `directory` a host hands a plugin is the directory that host
|
|
297
|
+
was started in, so a record kept there would land in whatever repository the
|
|
298
|
+
reader happened to be in, and the command line's directory is usually not the
|
|
299
|
+
window's (the vendor's console keeps the same three in `sessionStorage`, for the
|
|
300
|
+
same reason). Reading the file does not create the directory: a machine that
|
|
301
|
+
never spent a card has neither the file nor the directory.
|
|
298
302
|
That file is read at the start of every click, not once when the plugin is
|
|
299
303
|
created: a long-lived host would otherwise miss, and then overwrite, what a
|
|
300
304
|
command line's host wrote. A lock file beside it makes one spend at a time true
|
|
@@ -408,11 +412,14 @@ open-design.ai/cloud/reset,说明重置卡是什么、怎么获得,不会花
|
|
|
408
412
|
|
|
409
413
|
重试安全全靠 `operationId`,而且它活得比一次点击、也比跑它的宿主久。
|
|
410
414
|
magpie 每跑一次 `magpie quota action` 都是一个独立的插件宿主进程,而窗口里的宿主
|
|
411
|
-
是常驻的,所以 `cardId`、`operationId`、`workspaceId`
|
|
412
|
-
|
|
413
|
-
|
|
415
|
+
是常驻的,所以 `cardId`、`operationId`、`workspaceId` 三个值还会写到本插件自己的
|
|
416
|
+
一个目录里:`~/.magpie/opendesign/opendesign-reset-pending.json`,位于用户主目录下,
|
|
417
|
+
**不写进项目目录**,也不是 magpie 的配置目录(那是 `$XDG_CONFIG_HOME/magpie`,默认
|
|
418
|
+
`~/.config/magpie`,Dock 应用和命令行可能解析出不同的值):宿主交给插件的
|
|
419
|
+
`directory` 就是它启动时的工作目录,写在那里会落进用户当时碰巧打开的
|
|
414
420
|
仓库里,而命令行的工作目录通常跟窗口的不一样(官方控制台存在 sessionStorage,理由
|
|
415
|
-
|
|
421
|
+
一样)。读这个文件不会创建这个目录:从没花过卡的机器上,目录和文件都不存在。这个
|
|
422
|
+
文件**每次点击开始时都会重读**,不是插件创建时读一次——常驻宿主否则就
|
|
416
423
|
会看不到命令行宿主写进去的记录,回写时还会把它整个覆盖掉。旁边另有一个锁文件,
|
|
417
424
|
它是整个文件一把锁、不是每个账号一把:这个文件装着所有账号的记录,改一个账号就会
|
|
418
425
|
重写整份,所以只要有一笔在跑,别的点击(包括另一个账号的)都会被直接拒绝,而不是
|
package/index.mjs
CHANGED
|
@@ -251,7 +251,12 @@ async function readJSON(res) {
|
|
|
251
251
|
// back — in an error body, in a message — is cut here. It is cut before the
|
|
252
252
|
// body is shortened, which would otherwise leave half a key that no longer
|
|
253
253
|
// matches the pattern and would go out as it is.
|
|
254
|
-
|
|
254
|
+
// The key's alphabet is not documented, so a charset that stops at the first
|
|
255
|
+
// unusual character leaves the rest of the credential in the message:
|
|
256
|
+
// `vela_ctrl_ab.cdSECRET` came out as `vela_ctrl_….cdSECRET`. Everything up to
|
|
257
|
+
// the next whitespace is cut instead. Cutting a trailing word is harmless;
|
|
258
|
+
// leaving half a key behind is not.
|
|
259
|
+
const KEY = /(vela_ctrl_|sk-amr-)\S+/g
|
|
255
260
|
const scrub = (s) => s.replace(KEY, (_, prefix) => `${prefix}…`)
|
|
256
261
|
|
|
257
262
|
// said() is the vendor's own words, never a page of HTML.
|
|
@@ -854,6 +859,48 @@ const KEY_SHAPED = /(vela_ctrl_|sk-amr-)/
|
|
|
854
859
|
// carrying anything else could never settle: it is treated as damaged
|
|
855
860
|
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
856
861
|
|
|
862
|
+
// Give the lock back after reclaimStale moved it aside and then found it still
|
|
863
|
+
// live. That is not a contradiction: the lock is judged stale, and a host that
|
|
864
|
+
// takes it between that judgement and the rename is moved aside with it. The
|
|
865
|
+
// copy is fresh, so it goes back where it belongs; it is deleted only once it
|
|
866
|
+
// is either back or known to be dead, and never while it is the only record of
|
|
867
|
+
// which host held that lock.
|
|
868
|
+
//
|
|
869
|
+
// A function of its own because that ordering cannot be produced by a test:
|
|
870
|
+
// lock() refuses a live lock at lockIsLive() before reclaimStale() is called,
|
|
871
|
+
// and reclaimStale()'s own first stat returns early on a live lock, so the
|
|
872
|
+
// give-back branch is reachable only by calling this directly.
|
|
873
|
+
function giveBack(lockFile, aside) {
|
|
874
|
+
let give = false
|
|
875
|
+
try {
|
|
876
|
+
// a rename keeps the mtime, so a fresh lock grabbed by mistake admits it
|
|
877
|
+
give = Date.now() - statSync(aside).mtimeMs <= LOCK_STALE
|
|
878
|
+
} catch {}
|
|
879
|
+
if (give) {
|
|
880
|
+
// put a live lock back if the slot is free. When it is not, the copy is
|
|
881
|
+
// left alone rather than deleted: deleting it would destroy the only
|
|
882
|
+
// record of which host held that lock.
|
|
883
|
+
try { writeFileSync(lockFile, readFileSync(aside, "utf8"), { flag: "wx", mode: 0o600 }) } catch { return }
|
|
884
|
+
}
|
|
885
|
+
try { unlinkSync(aside) } catch {}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// Is the lock held by a host that may still be spending? One answer, one
|
|
889
|
+
// meaning: a lock younger than the stale bar is live, and a live lock is never
|
|
890
|
+
// taken. Like giveBack(), a function of its own so a test can hand it a path
|
|
891
|
+
// whose stat fails for a reason other than "there is no lock".
|
|
892
|
+
function lockIsLive(lockFile) {
|
|
893
|
+
try {
|
|
894
|
+
return Date.now() - statSync(lockFile).mtimeMs <= LOCK_STALE
|
|
895
|
+
} catch (e) {
|
|
896
|
+
// only "there is no lock" means no lock is live. A permission or handle
|
|
897
|
+
// problem is not evidence that nobody is spending, and taking it as that
|
|
898
|
+
// would let this host reclaim a lock that is in use.
|
|
899
|
+
if (e?.code === "ENOENT") return false
|
|
900
|
+
throw e
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
857
904
|
function storeOf(directory) {
|
|
858
905
|
const dir = text(directory)
|
|
859
906
|
const file = dir ? join(dir, PENDING) : ""
|
|
@@ -879,7 +926,10 @@ function storeOf(directory) {
|
|
|
879
926
|
// answer was lost from being repeated, and the way out of a record that can
|
|
880
927
|
// never settle is a human deleting it, not a timer that forgets a card.
|
|
881
928
|
const read = () => {
|
|
882
|
-
|
|
929
|
+
// reading does not need the directory to exist. `usage` reads this file on
|
|
930
|
+
// every poll, and creating a directory just to find no record in it would
|
|
931
|
+
// leave one behind on a machine that never spent a card.
|
|
932
|
+
if (!dir) return []
|
|
883
933
|
let raw
|
|
884
934
|
try {
|
|
885
935
|
raw = readFileSync(file, "utf8")
|
|
@@ -945,20 +995,6 @@ function storeOf(directory) {
|
|
|
945
995
|
// moved the one it judged old. Two hosts that both decide a lock is stale
|
|
946
996
|
// cannot both unlink it: the rename is atomic and only one wins, so the other
|
|
947
997
|
// finds nothing to take and goes back to waiting.
|
|
948
|
-
// Is the lock held by a host that may still be spending? One answer, one
|
|
949
|
-
// meaning: a lock younger than the stale bar is live, and a live lock is
|
|
950
|
-
// never taken.
|
|
951
|
-
const lockIsLive = () => {
|
|
952
|
-
try {
|
|
953
|
-
return Date.now() - statSync(lockFile).mtimeMs <= LOCK_STALE
|
|
954
|
-
} catch (e) {
|
|
955
|
-
// only "there is no lock" means no lock is live. A permission or handle
|
|
956
|
-
// problem is not evidence that nobody is spending, and taking it as that
|
|
957
|
-
// would let this host reclaim a lock that is in use.
|
|
958
|
-
if (e?.code === "ENOENT") return false
|
|
959
|
-
throw e
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
998
|
|
|
963
999
|
// Take a lock whose holder is demonstrably gone. It reports nothing: the
|
|
964
1000
|
// caller simply tries to create its own again, and whether that worked is
|
|
@@ -975,18 +1011,7 @@ function storeOf(directory) {
|
|
|
975
1011
|
} catch {
|
|
976
1012
|
return
|
|
977
1013
|
}
|
|
978
|
-
|
|
979
|
-
try {
|
|
980
|
-
// a rename keeps the mtime, so a fresh lock grabbed by mistake admits it
|
|
981
|
-
give = Date.now() - statSync(aside).mtimeMs <= LOCK_STALE
|
|
982
|
-
} catch {}
|
|
983
|
-
if (give) {
|
|
984
|
-
// put a live lock back if the slot is free. When it is not, the copy is
|
|
985
|
-
// left alone rather than deleted: deleting it would destroy the only
|
|
986
|
-
// record of which host held that lock.
|
|
987
|
-
try { writeFileSync(lockFile, readFileSync(aside, "utf8"), { flag: "wx", mode: 0o600 }) } catch { return }
|
|
988
|
-
}
|
|
989
|
-
try { unlinkSync(aside) } catch {}
|
|
1014
|
+
giveBack(lockFile, aside)
|
|
990
1015
|
}
|
|
991
1016
|
|
|
992
1017
|
// The lock is what makes "one spend at a time" true across the window's host
|
|
@@ -1023,7 +1048,7 @@ function storeOf(directory) {
|
|
|
1023
1048
|
// two: the moment it is released the record is settled and empty, and a
|
|
1024
1049
|
// queued click would read that emptiness as "nothing pending" and pick a
|
|
1025
1050
|
// second card. Only a lock whose holder is gone is taken.
|
|
1026
|
-
if (lockIsLive()) {
|
|
1051
|
+
if (lockIsLive(lockFile)) {
|
|
1027
1052
|
throw new Error("Another reset is running, so nothing was spent. · 还有一笔重置正在进行,没有花掉任何卡。")
|
|
1028
1053
|
}
|
|
1029
1054
|
reclaimStale()
|
|
@@ -1175,7 +1200,16 @@ export const OpenDesignAuthPlugin = async (input) => {
|
|
|
1175
1200
|
// does the same job.
|
|
1176
1201
|
const key = ids.join("|")
|
|
1177
1202
|
if (running.has(key)) return running.get(key)
|
|
1178
|
-
|
|
1203
|
+
// Every message a click throws is shown on the card, and one names the file
|
|
1204
|
+
// the record lives in: a directory named after a key would put that key
|
|
1205
|
+
// there. usage cuts its own messages; this is the same cut, made once for
|
|
1206
|
+
// the whole click so the click that shares this promise gets it too. A
|
|
1207
|
+
// message with no key-shaped run is rethrown as it is, so nothing else
|
|
1208
|
+
// changes shape.
|
|
1209
|
+
const run = spend(controlKey, ids, { signal }).catch((e) => {
|
|
1210
|
+
const m = String(e?.message ?? e)
|
|
1211
|
+
throw KEY_SHAPED.test(m) ? new Error(scrub(m)) : e
|
|
1212
|
+
})
|
|
1179
1213
|
running.set(key, run)
|
|
1180
1214
|
try {
|
|
1181
1215
|
return await run
|
|
@@ -1440,4 +1474,4 @@ export const OpenDesignAuthPlugin = async (input) => {
|
|
|
1440
1474
|
|
|
1441
1475
|
// _internal is what the tests read through; magpie calls every exported
|
|
1442
1476
|
// function as a plugin, so only the plugin itself is exported bare.
|
|
1443
|
-
export const _internal = { accountIdsOf, authOf, signFor, plainThinkingSse, allowances, cardToSpend, cardsOf, clearedMessage, controlKeyOf, credits, modelsOf, money, readOperation, rateOf, runtimeKeyOf, said, scrub, spendCard, startDevice, thePlan, usedBy, usedPercent, waitForDevice, windowName, when, workspaceOf }
|
|
1477
|
+
export const _internal = { accountIdsOf, authOf, signFor, plainThinkingSse, allowances, cardToSpend, cardsOf, clearedMessage, controlKeyOf, credits, giveBack, lockIsLive, modelsOf, money, readOperation, rateOf, runtimeKeyOf, said, scrub, spendCard, startDevice, thePlan, usedBy, usedPercent, waitForDevice, windowName, when, workspaceOf }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-opendesign-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
4
4
|
"description": "Open Design Cloud (Design Plan) in OpenCode and magpie: the account's device sign-in, chat and native video models, the plan's 5-hour and 7-day allowance, and its reset cards",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./opencode.mjs",
|