@magpie-community/opencode-agy-tui-auth 0.0.0-stage → 0.1.4
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 +843 -0
- package/live.mjs +143 -0
- package/login.mjs +116 -0
- package/opencode.mjs +4 -0
- package/opencode2.mjs +634 -0
- package/package.json +45 -4
- package/tool-gate.mjs +18 -0
package/live.mjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// The answer as agy writes it, while it writes it. agy keeps each
|
|
2
|
+
// conversation in its home's antigravity-cli/conversations/<id>.db (SQLite,
|
|
3
|
+
// in WAL mode, <id> the conversation's brain folder), and commits its
|
|
4
|
+
// model's reply there as each piece of it arrives: a row of `steps` with
|
|
5
|
+
// step_type 15 (the PLANNER_RESPONSE the transcript gets at the end),
|
|
6
|
+
// status 8 while it comes and 3 once whole, its text, as the model wrote
|
|
7
|
+
// it, in field 20.1 of step_payload. The transcript is written only when
|
|
8
|
+
// the reply is whole, and the screen shows it drawn (markup taken out,
|
|
9
|
+
// lines wrapped), so this is the one place the reply can be read as it
|
|
10
|
+
// comes. Read-only, never waiting on a lock; anything unreadable is null,
|
|
11
|
+
// and the turn's transcript gives the answer at its end as before.
|
|
12
|
+
// (The layout was read from agy 1.5.20's store; agy-cli's step-usage.mjs
|
|
13
|
+
// reads the same table.)
|
|
14
|
+
import { createRequire } from "node:module"
|
|
15
|
+
import fs from "node:fs"
|
|
16
|
+
import path from "node:path"
|
|
17
|
+
|
|
18
|
+
const MODEL_STEP = 15
|
|
19
|
+
const require = createRequire(import.meta.url)
|
|
20
|
+
|
|
21
|
+
let opener
|
|
22
|
+
|
|
23
|
+
// bun:sqlite under Bun (magpie's plugin host), node:sqlite on Node 22.5+;
|
|
24
|
+
// read-only, and a busy store is not waited for.
|
|
25
|
+
function loadOpener() {
|
|
26
|
+
if (opener !== undefined) return opener
|
|
27
|
+
opener = null
|
|
28
|
+
try {
|
|
29
|
+
const { Database, constants } = require("bun:sqlite")
|
|
30
|
+
if (typeof Database === "function") {
|
|
31
|
+
opener = (file) => {
|
|
32
|
+
const db = new Database(file, constants.SQLITE_OPEN_READONLY)
|
|
33
|
+
return {
|
|
34
|
+
all(sql) {
|
|
35
|
+
const stmt = db.prepare(sql)
|
|
36
|
+
try { return stmt.all() } finally { stmt.finalize() }
|
|
37
|
+
},
|
|
38
|
+
close: () => db.close(),
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return opener
|
|
42
|
+
}
|
|
43
|
+
} catch {}
|
|
44
|
+
try {
|
|
45
|
+
const { DatabaseSync } = require("node:sqlite")
|
|
46
|
+
if (typeof DatabaseSync === "function") {
|
|
47
|
+
opener = (file) => {
|
|
48
|
+
const db = new DatabaseSync(file, { readOnly: true, timeout: 0 })
|
|
49
|
+
return { all: (sql) => db.prepare(sql).all(), close: () => db.close() }
|
|
50
|
+
}
|
|
51
|
+
return opener
|
|
52
|
+
}
|
|
53
|
+
} catch {}
|
|
54
|
+
return opener
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function conversationDb(home, id) {
|
|
58
|
+
if (typeof id !== "string" || !/^[A-Za-z0-9._-]+$/.test(id)) return null
|
|
59
|
+
return path.join(home, ".gemini", "antigravity-cli", "conversations", `${id}.db`)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function varint(buf, at) {
|
|
63
|
+
let value = 0
|
|
64
|
+
let scale = 1
|
|
65
|
+
for (let i = 0; i < 10; i++) {
|
|
66
|
+
const byte = buf[at + i]
|
|
67
|
+
if (byte === undefined) return null
|
|
68
|
+
value += (byte & 0x7f) * scale
|
|
69
|
+
if ((byte & 0x80) === 0) return { value, next: at + i + 1 }
|
|
70
|
+
scale *= 128
|
|
71
|
+
}
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// fields calls fn(field, wire, value) for each of buf's fields, the bytes
|
|
76
|
+
// of a length-delimited one; false when buf isn't a message.
|
|
77
|
+
function fields(buf, fn) {
|
|
78
|
+
let at = 0
|
|
79
|
+
while (at < buf.length) {
|
|
80
|
+
const tag = varint(buf, at)
|
|
81
|
+
if (!tag || tag.value < 8) return false
|
|
82
|
+
at = tag.next
|
|
83
|
+
const field = Math.floor(tag.value / 8)
|
|
84
|
+
const wire = tag.value % 8
|
|
85
|
+
if (wire === 0) {
|
|
86
|
+
const v = varint(buf, at)
|
|
87
|
+
if (!v) return false
|
|
88
|
+
at = v.next
|
|
89
|
+
fn(field, wire, v.value)
|
|
90
|
+
} else if (wire === 2) {
|
|
91
|
+
const len = varint(buf, at)
|
|
92
|
+
if (!len || len.next + len.value > buf.length) return false
|
|
93
|
+
fn(field, wire, buf.subarray(len.next, len.next + len.value))
|
|
94
|
+
at = len.next + len.value
|
|
95
|
+
} else if (wire === 1 || wire === 5) {
|
|
96
|
+
at += wire === 1 ? 8 : 4
|
|
97
|
+
if (at > buf.length) return false
|
|
98
|
+
} else {
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return true
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const bytesOf = (v) => v instanceof Uint8Array ? v : ArrayBuffer.isView(v) ? new Uint8Array(v.buffer, v.byteOffset, v.byteLength) : v instanceof ArrayBuffer ? new Uint8Array(v) : null
|
|
106
|
+
|
|
107
|
+
// replyOf is a PLANNER_RESPONSE step's text (field 20.1 of its payload),
|
|
108
|
+
// "" when it has none yet, null when the payload isn't one.
|
|
109
|
+
export function replyOf(payload) {
|
|
110
|
+
const buf = bytesOf(payload)
|
|
111
|
+
if (!buf) return null
|
|
112
|
+
let response = null
|
|
113
|
+
if (!fields(buf, (f, w, v) => { if (f === 20 && w === 2) response = v })) return null
|
|
114
|
+
if (!response) return ""
|
|
115
|
+
let text = ""
|
|
116
|
+
if (!fields(response, (f, w, v) => { if (f === 1 && w === 2) text = new TextDecoder().decode(v) })) return null
|
|
117
|
+
return text
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// liveText is what agy's model has said so far in the conversation id of
|
|
121
|
+
// the account at home: each reply's text, in order, a blank line between
|
|
122
|
+
// two; null when the store can't be read.
|
|
123
|
+
export function liveText(home, id) {
|
|
124
|
+
const file = conversationDb(home, id)
|
|
125
|
+
if (!file || !fs.existsSync(file)) return null
|
|
126
|
+
const open = loadOpener()
|
|
127
|
+
if (!open) return null
|
|
128
|
+
let db
|
|
129
|
+
try {
|
|
130
|
+
db = open(file)
|
|
131
|
+
const rows = db.all(`SELECT idx, step_payload FROM steps WHERE step_type = ${MODEL_STEP} ORDER BY idx`)
|
|
132
|
+
const texts = []
|
|
133
|
+
for (const row of rows) {
|
|
134
|
+
const text = replyOf(row.step_payload)
|
|
135
|
+
if (text) texts.push(text)
|
|
136
|
+
}
|
|
137
|
+
return texts.join("\n\n")
|
|
138
|
+
} catch {
|
|
139
|
+
return null
|
|
140
|
+
} finally {
|
|
141
|
+
try { db?.close() } catch {}
|
|
142
|
+
}
|
|
143
|
+
}
|
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