@magpie-community/opencode-agy-tui-auth 0.0.0-stage → 0.1.3
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 +24 -0
- package/README.en.md +31 -0
- package/README.md +30 -2
- package/agy-version.mjs +16 -0
- package/default-agent.mjs +131 -0
- package/index.mjs +781 -0
- package/login.mjs +116 -0
- package/opencode.mjs +4 -0
- package/opencode2.mjs +634 -0
- package/package.json +44 -4
- package/tool-gate.mjs +18 -0
package/login.mjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { spawn } from "node:child_process"
|
|
2
|
+
import os from "node:os"
|
|
3
|
+
|
|
4
|
+
// The Google sign-in, run by agy itself (from the agy-cli plugin's
|
|
5
|
+
// login.mjs): `agy acp --login` prints Google's sign-in link, then reads
|
|
6
|
+
// the code the page shows from stdin, trades it and keeps the token in
|
|
7
|
+
// the account's home. The plugin never sees the token. magpie opens the
|
|
8
|
+
// link and takes the code in its own field.
|
|
9
|
+
|
|
10
|
+
export const LOGIN_WAIT_MS = 15 * 60 * 1000
|
|
11
|
+
export const LOGIN_START_MS = 30_000
|
|
12
|
+
export const LOGIN_FINISH_MS = 60_000
|
|
13
|
+
|
|
14
|
+
const SIGN_IN_LINK = /https:\/\/accounts\.google\.com\/[^\s"'<>]+/
|
|
15
|
+
|
|
16
|
+
let pending = null
|
|
17
|
+
|
|
18
|
+
function lastLine(text) {
|
|
19
|
+
return String(text).split(/\r?\n/).map((l) => l.trim()).filter(Boolean).at(-1) ?? ""
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function stop(child) {
|
|
23
|
+
try { child.stdin?.end() } catch {}
|
|
24
|
+
try { child.kill() } catch {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// startLogin starts agy's sign-in with env (the account's) and resolves
|
|
28
|
+
// with its link and finish, or with why it couldn't start. A second
|
|
29
|
+
// sign-in ends the first's agy.
|
|
30
|
+
export function startLogin(agy, env, { waitMs = LOGIN_WAIT_MS, startMs = LOGIN_START_MS } = {}) {
|
|
31
|
+
if (pending) {
|
|
32
|
+
stop(pending)
|
|
33
|
+
pending = null
|
|
34
|
+
}
|
|
35
|
+
return new Promise((resolve) => {
|
|
36
|
+
let child
|
|
37
|
+
try {
|
|
38
|
+
child = spawn(agy, ["acp", "--login"], { env, cwd: os.tmpdir(), stdio: ["pipe", "pipe", "pipe"], windowsHide: true })
|
|
39
|
+
} catch (err) {
|
|
40
|
+
resolve({ error: `couldn't run agy: ${err.message}` })
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
pending = child
|
|
44
|
+
let out = ""
|
|
45
|
+
let settled = false
|
|
46
|
+
let exited = null
|
|
47
|
+
const waiters = []
|
|
48
|
+
const settle = (value) => {
|
|
49
|
+
if (settled) return
|
|
50
|
+
settled = true
|
|
51
|
+
clearTimeout(startTimer)
|
|
52
|
+
resolve(value)
|
|
53
|
+
}
|
|
54
|
+
const startTimer = setTimeout(() => {
|
|
55
|
+
stop(child)
|
|
56
|
+
settle({ error: `agy didn't print a sign-in link within ${Math.round(startMs / 1000)}s${lastLine(out) ? ` (${lastLine(out)})` : ""}` })
|
|
57
|
+
}, startMs)
|
|
58
|
+
const waitTimer = setTimeout(() => stop(child), waitMs)
|
|
59
|
+
waitTimer.unref?.()
|
|
60
|
+
const read = (data) => {
|
|
61
|
+
out += data
|
|
62
|
+
const link = SIGN_IN_LINK.exec(out)?.[0]
|
|
63
|
+
if (link) settle({ url: link, finish })
|
|
64
|
+
}
|
|
65
|
+
child.stdout?.setEncoding("utf8")
|
|
66
|
+
child.stderr?.setEncoding("utf8")
|
|
67
|
+
child.stdout?.on("data", read)
|
|
68
|
+
child.stderr?.on("data", read)
|
|
69
|
+
child.stdin?.on("error", () => {})
|
|
70
|
+
child.on("error", (err) => {
|
|
71
|
+
exited = { code: null, err }
|
|
72
|
+
for (const w of waiters.splice(0)) w()
|
|
73
|
+
settle({ error: `couldn't run agy: ${err.message}` })
|
|
74
|
+
})
|
|
75
|
+
child.on("close", (code) => {
|
|
76
|
+
clearTimeout(waitTimer)
|
|
77
|
+
if (pending === child) pending = null
|
|
78
|
+
exited = exited ?? { code }
|
|
79
|
+
for (const w of waiters.splice(0)) w()
|
|
80
|
+
if (/Usage:/.test(out) && !SIGN_IN_LINK.test(out)) {
|
|
81
|
+
settle({ error: "this agy can't sign in from magpie (it has no `agy acp --login`): update it with `agy update`" })
|
|
82
|
+
}
|
|
83
|
+
settle({ error: `agy exited before printing a sign-in link${lastLine(out) ? `: ${lastLine(out)}` : ""}` })
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// finish hands agy the code and waits for it to trade it.
|
|
87
|
+
async function finish(code, { signal, finishMs = LOGIN_FINISH_MS } = {}) {
|
|
88
|
+
const value = String(code ?? "").trim()
|
|
89
|
+
if (!value || /[\r\n]/.test(value)) {
|
|
90
|
+
stop(child)
|
|
91
|
+
return { error: "paste the code the sign-in page shows" }
|
|
92
|
+
}
|
|
93
|
+
if (exited) return { error: "this sign-in is over (its link waits 15 minutes); start it again" }
|
|
94
|
+
const before = out.length
|
|
95
|
+
const done = new Promise((r) => waiters.push(r))
|
|
96
|
+
let timer
|
|
97
|
+
const late = new Promise((r) => { timer = setTimeout(() => r("late"), finishMs) })
|
|
98
|
+
const onAbort = () => stop(child)
|
|
99
|
+
signal?.addEventListener?.("abort", onAbort, { once: true })
|
|
100
|
+
try {
|
|
101
|
+
child.stdin.write(value + "\n")
|
|
102
|
+
if ((await Promise.race([done, late])) === "late") {
|
|
103
|
+
stop(child)
|
|
104
|
+
return { error: `agy didn't finish the sign-in within ${Math.round(finishMs / 1000)}s` }
|
|
105
|
+
}
|
|
106
|
+
} finally {
|
|
107
|
+
clearTimeout(timer)
|
|
108
|
+
signal?.removeEventListener?.("abort", onAbort)
|
|
109
|
+
}
|
|
110
|
+
if (exited.code === 0) return { ok: true }
|
|
111
|
+
const said = lastLine(out.slice(before)) || lastLine(out)
|
|
112
|
+
if (/invalid_grant/.test(said)) return { error: `Google turned the code away: mistyped, used already, or from another sign-in page. Start the sign-in again. (${said})` }
|
|
113
|
+
return { error: said || `agy exited with code ${exited.code}` }
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
}
|
package/opencode.mjs
ADDED