@wbcode/avron 0.0.0-stage → 0.1.1
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 +144 -2
- package/SECURITY.md +81 -0
- package/avron.cjs +2985 -0
- package/bin/avron-antigravity-hook.cjs +332 -0
- package/bin/avron-antigravity-statusline.cjs +593 -0
- package/bin/avron-claude-plugin.cjs +418 -0
- package/bin/avron-codex-hook.cjs +555 -0
- package/bin/avron-copilot-hook.cjs +294 -0
- package/bin/avron-copilot-statusline.cjs +510 -0
- package/bin/avron-cursor-hook.cjs +319 -0
- package/bin/avron-daemon.cjs +1846 -0
- package/bin/avron-hook.cjs +541 -0
- package/bin/avron-opencode-core.mjs +467 -0
- package/bin/avron-opencode.tsx +77 -0
- package/bin/avron-statusline.cjs +583 -0
- package/claude-plugin/.claude-plugin/marketplace.json +14 -0
- package/claude-plugin/avron/.claude-plugin/plugin.json +10 -0
- package/claude-plugin/avron/LICENSE +21 -0
- package/claude-plugin/avron/hooks/hooks.json +1 -0
- package/claude-plugin/avron/hooks/register.tsx +254 -0
- package/claude-plugin/avron/types/index.d.ts +23 -0
- package/package.json +28 -4
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
// packages/adapter-opencode/src/core.ts
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { dirname as dirname3, join as join4 } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
// packages/protocol/src/art.ts
|
|
7
|
+
var BLANK = String.fromCodePoint(10240);
|
|
8
|
+
|
|
9
|
+
// packages/protocol/src/constants.ts
|
|
10
|
+
var PROTOCOL_VERSION = "2.0.0";
|
|
11
|
+
var ADAPTER_TIMEOUT_MS = 15;
|
|
12
|
+
var MANIFEST_SYNC_INTERVAL_MS = 30 * 6e4;
|
|
13
|
+
var BATCH_FLUSH_INTERVAL_MS = 60 * 6e4;
|
|
14
|
+
var DAEMON_IDLE_EXIT_MS = 3 * 60 * 6e4;
|
|
15
|
+
var OFFLINE_QUEUE_MAX_BYTES = 5 * 1024 * 1024;
|
|
16
|
+
var MAX_LINE_BYTES = 8 * 1024;
|
|
17
|
+
var SPONSOR_LABEL = "Sponsored";
|
|
18
|
+
var SPONSOR_LABELS = {
|
|
19
|
+
ko: "\uAD11\uACE0",
|
|
20
|
+
ja: "\u5E83\u544A",
|
|
21
|
+
"zh-CN": "\u5E7F\u544A",
|
|
22
|
+
"zh-TW": "\u5EE3\u544A",
|
|
23
|
+
es: "Patrocinado",
|
|
24
|
+
pt: "Patrocinado",
|
|
25
|
+
de: "Anzeige",
|
|
26
|
+
fr: "Sponsoris\xE9"
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// packages/protocol/src/validate.ts
|
|
30
|
+
function isObject(v) {
|
|
31
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
32
|
+
}
|
|
33
|
+
function parsePayloadResponse(line) {
|
|
34
|
+
if (line.length === 0 || Buffer.byteLength(line, "utf8") > MAX_LINE_BYTES) return null;
|
|
35
|
+
let raw;
|
|
36
|
+
try {
|
|
37
|
+
raw = JSON.parse(line);
|
|
38
|
+
} catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
if (!isObject(raw) || raw.eventType !== "PAYLOAD_RESPONSE") return null;
|
|
42
|
+
if (!isObject(raw.payload) || typeof raw.payload.active !== "boolean") return null;
|
|
43
|
+
if (typeof raw.sessionId !== "string") return null;
|
|
44
|
+
return raw;
|
|
45
|
+
}
|
|
46
|
+
function serialize(msg) {
|
|
47
|
+
return `${JSON.stringify(msg)}
|
|
48
|
+
`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// packages/protocol/src/endpoint.ts
|
|
52
|
+
import { createHash } from "node:crypto";
|
|
53
|
+
import { homedir, tmpdir } from "node:os";
|
|
54
|
+
import { join, resolve } from "node:path";
|
|
55
|
+
function hostPaneOf(paneKey) {
|
|
56
|
+
if (!paneKey) return void 0;
|
|
57
|
+
return createHash("sha256").update(paneKey).digest("hex").slice(0, 16);
|
|
58
|
+
}
|
|
59
|
+
function avronHome(env = process.env) {
|
|
60
|
+
return env.AVRON_HOME && env.AVRON_HOME.length > 0 ? env.AVRON_HOME : join(homedir(), ".avron");
|
|
61
|
+
}
|
|
62
|
+
function avronPaths(env = process.env) {
|
|
63
|
+
const home = avronHome(env);
|
|
64
|
+
return {
|
|
65
|
+
home,
|
|
66
|
+
runDir: join(home, "run"),
|
|
67
|
+
logDir: join(home, "logs"),
|
|
68
|
+
dataDir: join(home, "data"),
|
|
69
|
+
cacheDir: join(home, "cache"),
|
|
70
|
+
keyDir: join(home, "keys"),
|
|
71
|
+
daemonLog: join(home, "logs", "daemon.log"),
|
|
72
|
+
impressionsLog: join(home, "logs", "impressions.ndjson"),
|
|
73
|
+
ledgerSnapshot: join(home, "data", "ledger.json"),
|
|
74
|
+
manifestCache: join(home, "cache", "manifest.json"),
|
|
75
|
+
eventQueue: join(home, "data", "queue.ndjson")
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
var UDS_MAX = 100;
|
|
79
|
+
function ipcEndpoint(env = process.env, platform = process.platform) {
|
|
80
|
+
const home = avronHome(env);
|
|
81
|
+
const tag = createHash("sha256").update(home).digest("hex").slice(0, 12);
|
|
82
|
+
if (platform === "win32") return `\\\\.\\pipe\\avron-uasp-${tag}.sock`;
|
|
83
|
+
const preferred = join(home, "run", "avron-uasp.sock");
|
|
84
|
+
if (Buffer.byteLength(preferred) <= UDS_MAX) return preferred;
|
|
85
|
+
return join(tmpdir(), `avron-uasp-${tag}.sock`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// packages/protocol/src/client.ts
|
|
89
|
+
import { connect } from "node:net";
|
|
90
|
+
|
|
91
|
+
// packages/protocol/src/quiet.ts
|
|
92
|
+
function quietHere(env = process.env) {
|
|
93
|
+
if (env.AVRON_ALLOW_CI === "1") return false;
|
|
94
|
+
const ci = (env.CI ?? "").trim().toLowerCase();
|
|
95
|
+
return ci !== "" && ci !== "0" && ci !== "false";
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// packages/protocol/src/client.ts
|
|
99
|
+
function request(msg, opts = {}) {
|
|
100
|
+
if (quietHere()) return Promise.resolve(null);
|
|
101
|
+
const endpoint = opts.endpoint ?? ipcEndpoint();
|
|
102
|
+
const timeoutMs = opts.timeoutMs ?? ADAPTER_TIMEOUT_MS;
|
|
103
|
+
return new Promise((resolve3) => {
|
|
104
|
+
let done = false;
|
|
105
|
+
let buf = "";
|
|
106
|
+
const finish = (value) => {
|
|
107
|
+
if (done) return;
|
|
108
|
+
done = true;
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
socket.destroy();
|
|
111
|
+
resolve3(value);
|
|
112
|
+
};
|
|
113
|
+
const timer = setTimeout(() => finish(null), timeoutMs);
|
|
114
|
+
const socket = connect(endpoint);
|
|
115
|
+
socket.setEncoding("utf8");
|
|
116
|
+
socket.on("connect", () => {
|
|
117
|
+
socket.write(serialize(msg));
|
|
118
|
+
});
|
|
119
|
+
socket.on("data", (chunk) => {
|
|
120
|
+
buf += chunk;
|
|
121
|
+
const nl = buf.indexOf("\n");
|
|
122
|
+
if (nl >= 0) finish(parsePayloadResponse(buf.slice(0, nl)));
|
|
123
|
+
else if (buf.length > MAX_LINE_BYTES) finish(null);
|
|
124
|
+
});
|
|
125
|
+
socket.on("error", () => finish(null));
|
|
126
|
+
socket.on("close", () => finish(null));
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function send(msg, opts = {}) {
|
|
130
|
+
if (quietHere()) return Promise.resolve(false);
|
|
131
|
+
const endpoint = opts.endpoint ?? ipcEndpoint();
|
|
132
|
+
const timeoutMs = opts.timeoutMs ?? ADAPTER_TIMEOUT_MS;
|
|
133
|
+
return new Promise((resolve3) => {
|
|
134
|
+
let done = false;
|
|
135
|
+
const finish = (ok) => {
|
|
136
|
+
if (done) return;
|
|
137
|
+
done = true;
|
|
138
|
+
clearTimeout(timer);
|
|
139
|
+
if (!ok) socket.destroy();
|
|
140
|
+
resolve3(ok);
|
|
141
|
+
};
|
|
142
|
+
const timer = setTimeout(() => finish(false), timeoutMs);
|
|
143
|
+
const socket = connect(endpoint);
|
|
144
|
+
socket.on("connect", () => {
|
|
145
|
+
socket.end(serialize(msg), () => finish(true));
|
|
146
|
+
});
|
|
147
|
+
socket.on("error", () => finish(false));
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// packages/protocol/src/spawn.ts
|
|
152
|
+
import { spawn } from "node:child_process";
|
|
153
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, statSync, utimesSync } from "node:fs";
|
|
154
|
+
import { basename, dirname, join as join2 } from "node:path";
|
|
155
|
+
var FIRST_MESSAGE_ENV = "AVRON_FIRST_MESSAGE";
|
|
156
|
+
var runtimeFile = (env = process.env) => join2(avronHome(env), "runtime.json");
|
|
157
|
+
function chooseNode(opts = {}) {
|
|
158
|
+
const execPath = opts.execPath ?? process.execPath;
|
|
159
|
+
const electron = "electron" in opts ? opts.electron : process.versions.electron;
|
|
160
|
+
if (!electron && /^node(\.exe)?$/i.test(basename(execPath))) return execPath;
|
|
161
|
+
try {
|
|
162
|
+
const node = JSON.parse(readFileSync(opts.runtime ?? runtimeFile(), "utf8")).node;
|
|
163
|
+
if (typeof node === "string" && node.length > 0 && existsSync(node)) return node;
|
|
164
|
+
} catch {
|
|
165
|
+
}
|
|
166
|
+
return "node";
|
|
167
|
+
}
|
|
168
|
+
function spawnDaemon(entry, env = process.env) {
|
|
169
|
+
try {
|
|
170
|
+
const child = spawn(chooseNode({ runtime: runtimeFile(env) }), [entry], {
|
|
171
|
+
detached: true,
|
|
172
|
+
stdio: "ignore",
|
|
173
|
+
windowsHide: true,
|
|
174
|
+
env
|
|
175
|
+
});
|
|
176
|
+
child.on("error", () => {
|
|
177
|
+
});
|
|
178
|
+
child.unref();
|
|
179
|
+
return true;
|
|
180
|
+
} catch {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function ensureDaemon(entry, env = process.env, firstMessage) {
|
|
185
|
+
if (quietHere(env)) return;
|
|
186
|
+
try {
|
|
187
|
+
const marker = join2(avronPaths(env).runDir, "spawn.marker");
|
|
188
|
+
try {
|
|
189
|
+
if (Date.now() - statSync(marker).mtimeMs < 1e4) return;
|
|
190
|
+
} catch {
|
|
191
|
+
}
|
|
192
|
+
mkdirSync(dirname(marker), { recursive: true });
|
|
193
|
+
closeSync(openSync(marker, "a"));
|
|
194
|
+
utimesSync(marker, /* @__PURE__ */ new Date(), /* @__PURE__ */ new Date());
|
|
195
|
+
spawnDaemon(entry, firstMessage ? { ...env, [FIRST_MESSAGE_ENV]: firstMessage } : env);
|
|
196
|
+
} catch {
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// packages/adapter-kit/src/render.ts
|
|
201
|
+
var ESC = "\x1B";
|
|
202
|
+
var RESET = `${ESC}[0m`;
|
|
203
|
+
function sanitizeText(input, maxChars = 120) {
|
|
204
|
+
if (typeof input !== "string") return "";
|
|
205
|
+
let out = "";
|
|
206
|
+
for (const ch of input) {
|
|
207
|
+
const cp = ch.codePointAt(0);
|
|
208
|
+
if (cp < 32 || cp >= 127 && cp <= 159) {
|
|
209
|
+
out += " ";
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (cp >= 8203 && cp <= 8207 || cp >= 8234 && cp <= 8238 || cp >= 8294 && cp <= 8297)
|
|
213
|
+
continue;
|
|
214
|
+
if (cp === 65279 || cp === 8232 || cp === 8233) continue;
|
|
215
|
+
out += ch;
|
|
216
|
+
}
|
|
217
|
+
out = out.replace(/\s+/g, " ").trim();
|
|
218
|
+
return [...out].slice(0, maxChars).join("");
|
|
219
|
+
}
|
|
220
|
+
function isWide(cp) {
|
|
221
|
+
return cp >= 4352 && cp <= 4447 || // Hangul Jamo
|
|
222
|
+
cp >= 11904 && cp <= 42191 || // CJK radicals .. Yi
|
|
223
|
+
cp >= 44032 && cp <= 55203 || // Hangul syllables
|
|
224
|
+
cp >= 63744 && cp <= 64255 || // CJK compatibility ideographs
|
|
225
|
+
cp >= 65072 && cp <= 65103 || // CJK compatibility forms
|
|
226
|
+
cp >= 65280 && cp <= 65376 || // Fullwidth forms
|
|
227
|
+
cp >= 65504 && cp <= 65510 || cp >= 127744 && cp <= 129791 || // pictographs
|
|
228
|
+
cp >= 131072 && cp <= 262141;
|
|
229
|
+
}
|
|
230
|
+
function isZeroWidth(cp) {
|
|
231
|
+
return cp >= 768 && cp <= 879 || cp >= 65024 && cp <= 65039 || cp === 8205;
|
|
232
|
+
}
|
|
233
|
+
function displayWidth(text) {
|
|
234
|
+
let w = 0;
|
|
235
|
+
for (const ch of stripAnsi(text)) {
|
|
236
|
+
const cp = ch.codePointAt(0);
|
|
237
|
+
if (isZeroWidth(cp)) continue;
|
|
238
|
+
w += isWide(cp) ? 2 : 1;
|
|
239
|
+
}
|
|
240
|
+
return w;
|
|
241
|
+
}
|
|
242
|
+
var ANSI_RE = /\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)|\u001b\[[0-9;?]*[ -/]*[@-~]/g;
|
|
243
|
+
function stripAnsi(text) {
|
|
244
|
+
return text.replace(ANSI_RE, "");
|
|
245
|
+
}
|
|
246
|
+
function truncate(text, columns) {
|
|
247
|
+
if (displayWidth(text) <= columns) return text;
|
|
248
|
+
if (columns <= 1) return "";
|
|
249
|
+
let out = "";
|
|
250
|
+
let w = 0;
|
|
251
|
+
for (const ch of text) {
|
|
252
|
+
const cw = displayWidth(ch);
|
|
253
|
+
if (w + cw > columns - 1) break;
|
|
254
|
+
out += ch;
|
|
255
|
+
w += cw;
|
|
256
|
+
}
|
|
257
|
+
return `${out.trimEnd()}\u2026`;
|
|
258
|
+
}
|
|
259
|
+
var SEP = "\u2502";
|
|
260
|
+
var KNOWN_LABELS = /* @__PURE__ */ new Set([SPONSOR_LABEL, ...Object.values(SPONSOR_LABELS)]);
|
|
261
|
+
function layout(c, width) {
|
|
262
|
+
const label = KNOWN_LABELS.has(c.label ?? "") ? c.label : SPONSOR_LABEL;
|
|
263
|
+
const badge = sanitizeText(c.sponsorName, 24).toUpperCase();
|
|
264
|
+
const copyFull = sanitizeText(c.headline, 80);
|
|
265
|
+
const ctaRaw = sanitizeText(c.ctaText, 28);
|
|
266
|
+
const cta = ctaRaw ? `${ctaRaw.replace(/\s*↗$/, "")} \u2197` : "";
|
|
267
|
+
if (!badge) return null;
|
|
268
|
+
const fits = (p) => displayWidth(plain(p)) <= width;
|
|
269
|
+
const wide = { label, badge, copy: copyFull, cta };
|
|
270
|
+
if (copyFull && fits(wide)) return wide;
|
|
271
|
+
if (copyFull) {
|
|
272
|
+
const fixed = displayWidth(plain({ label, badge, copy: "", cta })) + 3;
|
|
273
|
+
const room = width - fixed;
|
|
274
|
+
if (room >= 16) {
|
|
275
|
+
const standard = { label, badge, copy: truncate(copyFull, room), cta };
|
|
276
|
+
if (fits(standard)) return standard;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const compact = { label, badge, copy: "", cta };
|
|
280
|
+
if (cta && fits(compact)) return compact;
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
function plainLine(content, maxLength) {
|
|
284
|
+
const p = layout(content, maxLength);
|
|
285
|
+
return p ? plain(p) : "";
|
|
286
|
+
}
|
|
287
|
+
function plain(p) {
|
|
288
|
+
const left = `${p.label} ${p.badge} `;
|
|
289
|
+
if (p.copy && p.cta) return `${left}${SEP} ${p.copy} \xB7 ${p.cta}`;
|
|
290
|
+
if (p.copy) return `${left}${SEP} ${p.copy}`;
|
|
291
|
+
if (p.cta) return `${left}\xB7 ${p.cta}`;
|
|
292
|
+
return left;
|
|
293
|
+
}
|
|
294
|
+
var style = {
|
|
295
|
+
truecolor: {
|
|
296
|
+
badge: `${ESC}[48;2;39;39;42m${ESC}[38;2;244;244;245m`,
|
|
297
|
+
dim: `${ESC}[38;2;113;113;122m`,
|
|
298
|
+
copy: `${ESC}[38;2;161;161;170m`,
|
|
299
|
+
cta: `${ESC}[38;2;52;211;153m`
|
|
300
|
+
},
|
|
301
|
+
basic: { badge: `${ESC}[7m`, dim: `${ESC}[2m`, copy: `${ESC}[2m`, cta: `${ESC}[32m` }
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// packages/adapter-kit/src/turn.ts
|
|
305
|
+
function envelope(agent, sessionId, turnId) {
|
|
306
|
+
return {
|
|
307
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
308
|
+
agentName: agent.name,
|
|
309
|
+
agentTier: agent.tier,
|
|
310
|
+
sessionId,
|
|
311
|
+
turnId,
|
|
312
|
+
timestamp: Date.now()
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
function busyMessage(agent, sessionId, turnId, slots, where = {}) {
|
|
316
|
+
return {
|
|
317
|
+
...envelope(agent, sessionId, turnId),
|
|
318
|
+
eventType: "AGENT_BUSY",
|
|
319
|
+
payload: {
|
|
320
|
+
declaredSlots: slots,
|
|
321
|
+
...where.hostPane ? { hostPane: where.hostPane } : {},
|
|
322
|
+
...where.place ? { place: where.place } : {}
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
function idleMessage(agent, sessionId, turnId, reason = "completed") {
|
|
327
|
+
return {
|
|
328
|
+
...envelope(agent, sessionId, turnId),
|
|
329
|
+
eventType: "AGENT_IDLE",
|
|
330
|
+
payload: { terminationReason: reason }
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function requestMessage(agent, sessionId, turnId, slot, maxLength, drawn) {
|
|
334
|
+
return {
|
|
335
|
+
...envelope(agent, sessionId, turnId),
|
|
336
|
+
eventType: "PAYLOAD_REQUEST",
|
|
337
|
+
payload: { slot, ...maxLength !== void 0 ? { maxLength } : {}, ...drawn ? { drawn } : {} }
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// packages/adapter-kit/src/reversible.ts
|
|
342
|
+
import { dirname as dirname2, isAbsolute, join as join3, relative, resolve as resolve2 } from "node:path";
|
|
343
|
+
var PROTECTED_DIRS = [
|
|
344
|
+
".claude",
|
|
345
|
+
".cursor",
|
|
346
|
+
".codex",
|
|
347
|
+
".copilot",
|
|
348
|
+
".kiro",
|
|
349
|
+
".codeium",
|
|
350
|
+
".gemini",
|
|
351
|
+
".orca",
|
|
352
|
+
join3(".config", "opencode"),
|
|
353
|
+
join3("AppData", "Roaming", "orca"),
|
|
354
|
+
join3("Library", "Application Support", "orca")
|
|
355
|
+
];
|
|
356
|
+
|
|
357
|
+
// packages/adapter-opencode/src/turns.ts
|
|
358
|
+
var AGENT = { name: "opencode", tier: "cli" };
|
|
359
|
+
var Turns = class {
|
|
360
|
+
constructor(hostPane, newId, now = Date.now) {
|
|
361
|
+
this.hostPane = hostPane;
|
|
362
|
+
this.newId = newId;
|
|
363
|
+
this.now = now;
|
|
364
|
+
}
|
|
365
|
+
open = /* @__PURE__ */ new Map();
|
|
366
|
+
/** `session.status` with its status type. */
|
|
367
|
+
status(sessionId, type) {
|
|
368
|
+
if (type === "busy") {
|
|
369
|
+
if (this.open.has(sessionId)) return null;
|
|
370
|
+
const turnId = this.newId();
|
|
371
|
+
this.open.set(sessionId, { turnId, startedAt: this.now() });
|
|
372
|
+
return busyMessage(
|
|
373
|
+
AGENT,
|
|
374
|
+
sessionId,
|
|
375
|
+
turnId,
|
|
376
|
+
["statusline"],
|
|
377
|
+
this.hostPane ? { hostPane: this.hostPane } : {}
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
if (type === "idle") return this.end(sessionId);
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
/** `session.error`: remembered for the idle that follows. */
|
|
384
|
+
error(sessionId, name) {
|
|
385
|
+
const turn = this.open.get(sessionId);
|
|
386
|
+
if (turn) turn.reason = /abort/i.test(name) ? "aborted" : "error";
|
|
387
|
+
}
|
|
388
|
+
/** `session.idle` (or the "idle" status): the turn is over. */
|
|
389
|
+
end(sessionId) {
|
|
390
|
+
const turn = this.open.get(sessionId);
|
|
391
|
+
if (!turn) return null;
|
|
392
|
+
this.open.delete(sessionId);
|
|
393
|
+
return idleMessage(AGENT, sessionId, turn.turnId, turn.reason ?? "completed");
|
|
394
|
+
}
|
|
395
|
+
/** The turn to show: the one started last, if any is open. */
|
|
396
|
+
current() {
|
|
397
|
+
let best = null;
|
|
398
|
+
for (const [sessionId, t] of this.open)
|
|
399
|
+
if (!best || t.startedAt >= best.at) best = { sessionId, turnId: t.turnId, at: t.startedAt };
|
|
400
|
+
return best ? { sessionId: best.sessionId, turnId: best.turnId } : null;
|
|
401
|
+
}
|
|
402
|
+
/** Every open turn ends (OpenCode is closing). */
|
|
403
|
+
endAll() {
|
|
404
|
+
const out = [];
|
|
405
|
+
for (const [sessionId, t] of this.open) out.push(idleMessage(AGENT, sessionId, t.turnId, "aborted"));
|
|
406
|
+
this.open.clear();
|
|
407
|
+
return out;
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
// packages/adapter-opencode/src/core.ts
|
|
412
|
+
var str = (v) => typeof v === "string" && v.length <= 200 ? v : void 0;
|
|
413
|
+
var field = (o, k) => o !== null && typeof o === "object" ? o[k] : void 0;
|
|
414
|
+
function createAvron(env = process.env, binDir = dirname3(fileURLToPath(import.meta.url))) {
|
|
415
|
+
const turns = new Turns(hostPaneOf(env.ORCA_PANE_KEY), () => `t-${randomUUID()}`);
|
|
416
|
+
const deliver = (msg) => {
|
|
417
|
+
if (!msg) return;
|
|
418
|
+
send(msg).then((ok) => {
|
|
419
|
+
if (!ok && msg.eventType === "AGENT_BUSY")
|
|
420
|
+
ensureDaemon(join4(binDir, "avron-daemon.cjs"), env, JSON.stringify(msg));
|
|
421
|
+
}).catch(() => {
|
|
422
|
+
});
|
|
423
|
+
};
|
|
424
|
+
return {
|
|
425
|
+
event(type, properties) {
|
|
426
|
+
try {
|
|
427
|
+
const sessionId = str(field(properties, "sessionID"));
|
|
428
|
+
if (!sessionId) return;
|
|
429
|
+
if (type === "session.status")
|
|
430
|
+
deliver(turns.status(sessionId, str(field(field(properties, "status"), "type")) ?? ""));
|
|
431
|
+
else if (type === "session.error")
|
|
432
|
+
turns.error(sessionId, str(field(field(properties, "error"), "name")) ?? "");
|
|
433
|
+
else if (type === "session.idle") deliver(turns.end(sessionId));
|
|
434
|
+
} catch {
|
|
435
|
+
}
|
|
436
|
+
},
|
|
437
|
+
async line(width) {
|
|
438
|
+
try {
|
|
439
|
+
const open = turns.current();
|
|
440
|
+
if (!open) return "";
|
|
441
|
+
const max = Math.max(20, Math.min(Math.floor(width) || 80, 200));
|
|
442
|
+
const res = await request(requestMessage(AGENT, open.sessionId, open.turnId, "statusline", max));
|
|
443
|
+
const p = res?.payload;
|
|
444
|
+
if (!p?.active || !p.sponsorName || !p.headline) return "";
|
|
445
|
+
const content = {
|
|
446
|
+
label: p.label,
|
|
447
|
+
sponsorName: p.sponsorName,
|
|
448
|
+
headline: p.headline,
|
|
449
|
+
ctaText: p.ctaText,
|
|
450
|
+
actionUrl: p.actionUrl
|
|
451
|
+
};
|
|
452
|
+
return plainLine(content, max);
|
|
453
|
+
} catch {
|
|
454
|
+
return "";
|
|
455
|
+
}
|
|
456
|
+
},
|
|
457
|
+
close() {
|
|
458
|
+
try {
|
|
459
|
+
for (const msg of turns.endAll()) deliver(msg);
|
|
460
|
+
} catch {
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
export {
|
|
466
|
+
createAvron
|
|
467
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
// Avron's OpenCode TUI plugin. OpenCode loads this file as it is (it compiles the JSX itself) from
|
|
3
|
+
// Avron's bin folder, listed in ~/.config/opencode/tui.json by `npx @wbcode/avron install`. Everything
|
|
4
|
+
// else is in avron-opencode-core.mjs next to it. While an answer is being worked on, one sponsor
|
|
5
|
+
// line shows at the bottom of OpenCode's screen; it goes away when the answer ends or is stopped.
|
|
6
|
+
// Nothing of the prompt, the answer, the files or the folder is read.
|
|
7
|
+
import { Show, createSignal } from 'solid-js';
|
|
8
|
+
import { createAvron } from './avron-opencode-core.mjs';
|
|
9
|
+
|
|
10
|
+
const POLL_MS = 1000;
|
|
11
|
+
|
|
12
|
+
/** The few parts of OpenCode's plugin API used here (@opencode-ai/plugin/tui, TuiPluginApi). */
|
|
13
|
+
interface Api {
|
|
14
|
+
event: { on(type: string, handler: (event: { properties?: unknown }) => void): unknown };
|
|
15
|
+
slots: { register(plugin: { slots: Record<string, () => unknown> }): unknown };
|
|
16
|
+
lifecycle?: { onDispose(fn: () => void): unknown };
|
|
17
|
+
renderer?: { width?: number };
|
|
18
|
+
theme?: { current?: { textMuted?: unknown } };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface Avron {
|
|
22
|
+
event(type: string, properties: unknown): void;
|
|
23
|
+
line(width: number): Promise<string>;
|
|
24
|
+
close(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
id: 'avron',
|
|
29
|
+
tui: async (api: Api) => {
|
|
30
|
+
let avron: Avron;
|
|
31
|
+
try {
|
|
32
|
+
avron = createAvron();
|
|
33
|
+
} catch {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const [line, setLine] = createSignal('');
|
|
37
|
+
for (const type of ['session.status', 'session.error', 'session.idle']) {
|
|
38
|
+
try {
|
|
39
|
+
api.event.on(type, (e) => {
|
|
40
|
+
avron.event(type, e?.properties);
|
|
41
|
+
if (type !== 'session.status') setLine('');
|
|
42
|
+
});
|
|
43
|
+
} catch {
|
|
44
|
+
// An event OpenCode no longer has: the others still work.
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
let asking = false;
|
|
48
|
+
const timer = setInterval(async () => {
|
|
49
|
+
if (asking) return;
|
|
50
|
+
asking = true;
|
|
51
|
+
try {
|
|
52
|
+
setLine(await avron.line((Number(api.renderer?.width) || 80) - 2));
|
|
53
|
+
} catch {
|
|
54
|
+
setLine('');
|
|
55
|
+
} finally {
|
|
56
|
+
asking = false;
|
|
57
|
+
}
|
|
58
|
+
}, POLL_MS);
|
|
59
|
+
try {
|
|
60
|
+
api.lifecycle?.onDispose(() => {
|
|
61
|
+
clearInterval(timer);
|
|
62
|
+
avron.close();
|
|
63
|
+
});
|
|
64
|
+
} catch {
|
|
65
|
+
// Older OpenCode: the timer ends with the process.
|
|
66
|
+
}
|
|
67
|
+
api.slots.register({
|
|
68
|
+
slots: {
|
|
69
|
+
app_bottom: () => (
|
|
70
|
+
<Show when={line()}>
|
|
71
|
+
<text fg={api.theme?.current?.textMuted}> {line()}</text>
|
|
72
|
+
</Show>
|
|
73
|
+
),
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
};
|