@magpie-community/opencode-agy-tui-auth 0.1.3 → 0.1.5

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/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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-agy-tui-auth",
3
- "version": "0.1.3",
4
- "description": "Magpie/OpenCode model provider that runs Antigravity's models through agy's own interactive mode: each request is typed into the real agy TUI and its answer read from agy's transcript",
3
+ "version": "0.1.5",
4
+ "description": "Magpie/OpenCode model provider that runs Antigravity's models through agy's own interactive mode: each request is typed into the real agy TUI and its answer streamed from agy's own conversation store as the model writes it",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",
7
7
  "exports": {
@@ -9,10 +9,16 @@
9
9
  },
10
10
  "files": [
11
11
  "index.mjs",
12
+ "live.mjs",
12
13
  "login.mjs",
13
14
  "default-agent.mjs",
14
15
  "agy-version.mjs",
15
16
  "tool-gate.mjs",
17
+ "tools.mjs",
18
+ "tool-schema.mjs",
19
+ "re2-pattern.mjs",
20
+ "array-items.mjs",
21
+ "tool-budget.mjs",
16
22
  "opencode.mjs",
17
23
  "opencode2.mjs",
18
24
  "README.md",
@@ -38,7 +44,7 @@
38
44
  "test": "bun test"
39
45
  },
40
46
  "engines": {
41
- "magpie": ">=0.1.5027"
47
+ "magpie": ">=0.1.5217"
42
48
  },
43
49
  "magpie": {
44
50
  "maxConcurrency": 4