@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.
@@ -0,0 +1,254 @@
1
+ import { atom, read, update } from 'claude-code';
2
+ import type { EngineInterface, ProcessSpawnChunk, ProcessSpawnResult, Register } from 'claude-code';
3
+
4
+ import type { AvronCard, AvronShown } from '../types';
5
+
6
+ // Avron for the Claude desktop app (the Code tab). The desktop app runs Claude Code's hooks, which tell
7
+ // the local Avron daemon when a turn starts and ends, but it never draws Claude Code's status line. This
8
+ // plugin is the missing screen: while a turn runs it starts a small helper (the same Node script the
9
+ // hooks use) that asks the daemon what to show and prints it, and the plugin draws that in ONE place:
10
+ // a plain line -> the status line under the input
11
+ // a card (details) -> one row above the input with a Details button; the side pane opens only when
12
+ // that button is pressed
13
+ // Nothing blinks or moves, nothing is drawn after the turn ends, and nothing is read from the project.
14
+ // Where there is no desktop surface (the terminal, Orca) this plugin does nothing at all.
15
+ //
16
+ // The helper's path comes from avron-plugin.json, which `avron install` writes next to this file.
17
+ const shown = atom({ plugin: 'avron', key: 'shown' } as const, null);
18
+ const PANE = 'avron-card';
19
+ const SURFACES: readonly string[] = ['desktop'];
20
+ /** The same rule the helper and the hook apply to a session id. */
21
+ const ID_RE = /^[A-Za-z0-9._:-]{1,128}$/;
22
+
23
+ type Config = { node: string; script: string };
24
+ type Helper = AsyncGenerator<ProcessSpawnChunk, ProcessSpawnResult>;
25
+
26
+ // Module variables start over at a reload, which is fine: a reload ends the helper (it is killed
27
+ // with the module) and the next turn starts a new one.
28
+ const run: { token: number; helper: Helper | null; config: Config | null; sessionId: string | null } = {
29
+ token: 0,
30
+ helper: null,
31
+ config: null,
32
+ sessionId: null,
33
+ };
34
+
35
+ async function loadConfig($: EngineInterface): Promise<Config | null> {
36
+ try {
37
+ const value: unknown = JSON.parse(await $.fs.read(`${$.plugin.root}/avron-plugin.json`));
38
+ const { node, script } = value as Record<string, unknown>;
39
+
40
+ return typeof node === 'string' && typeof script === 'string' && node !== '' && script !== ''
41
+ ? { node, script }
42
+ : null;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+
48
+ /** Claude Code names the transcript file after the session: .../<session id>.jsonl */
49
+ function sessionIdOf(transcriptPath: string): string | null {
50
+ const name = transcriptPath.split(/[\\/]/).pop() ?? '';
51
+ const id = name.replace(/\.jsonl$/, '');
52
+
53
+ return ID_RE.test(id) ? id : null;
54
+ }
55
+
56
+ function text(value: unknown, max: number): string {
57
+ return typeof value === 'string' ? value.slice(0, max) : '';
58
+ }
59
+
60
+ function cardOf(value: unknown): AvronCard | null {
61
+ if (typeof value !== 'object' || value === null) return null;
62
+ const c = value as Record<string, unknown>;
63
+
64
+ return {
65
+ label: text(c.label, 40),
66
+ sponsorName: text(c.sponsorName, 40),
67
+ headline: text(c.headline, 120),
68
+ body: text(c.body, 200),
69
+ snippet: text(c.snippet, 600),
70
+ promoCode: text(c.promoCode, 40),
71
+ };
72
+ }
73
+
74
+ /** One line the helper printed: {"shown": null} or {"shown": {text, sponsorName, url, card}}. */
75
+ function parse(line: string): { shown: AvronShown | null } | null {
76
+ try {
77
+ const value = (JSON.parse(line) as { shown?: unknown }).shown;
78
+ if (value === null) return { shown: null };
79
+ if (typeof value !== 'object' || value === undefined) return null;
80
+ const v = value as Record<string, unknown>;
81
+ const label = text(v.text, 300);
82
+ if (label === '') return { shown: null };
83
+ const url = typeof v.url === 'string' && v.url.startsWith('https://') ? v.url.slice(0, 2000) : null;
84
+
85
+ return { shown: { text: label, sponsorName: text(v.sponsorName, 40), url, card: cardOf(v.card) } };
86
+ } catch {
87
+ return null;
88
+ }
89
+ }
90
+
91
+ async function apply($: EngineInterface, token: number, line: string): Promise<void> {
92
+ const parsed = parse(line);
93
+ if (parsed === null || token !== run.token) return;
94
+ await update($, shown, () => parsed.shown);
95
+ // A plain line goes to the status line; a card goes to the row above the input (drawn below).
96
+ $.ui.status(parsed.shown !== null && parsed.shown.card === null ? parsed.shown.text : undefined);
97
+ }
98
+
99
+ async function clear($: EngineInterface): Promise<void> {
100
+ run.token += 1;
101
+ const helper = run.helper;
102
+ run.helper = null;
103
+ $.ui.status(undefined);
104
+ await update($, shown, () => null);
105
+
106
+ if (helper !== null) {
107
+ try {
108
+ // Leaving the stream kills the helper.
109
+ await helper.return({ code: null, signal: null });
110
+ } catch {
111
+ // Already gone.
112
+ }
113
+ }
114
+
115
+ try {
116
+ await $.ui.close({ id: PANE });
117
+ } catch {
118
+ // The pane was not open.
119
+ }
120
+ }
121
+
122
+ async function begin($: EngineInterface): Promise<void> {
123
+ await clear($);
124
+ const surfaces = await $.session.surfaces();
125
+ if (!surfaces.some((surface) => SURFACES.includes(surface))) return;
126
+ const config = await loadConfig($);
127
+ if (config === null) return;
128
+ const sessionId = sessionIdOf((await $.session.info()).transcriptPath);
129
+ if (sessionId === null) return;
130
+
131
+ const token = run.token;
132
+ const helper = $.process.spawn({ argv: [config.node, config.script, 'watch', sessionId] });
133
+ run.helper = helper;
134
+ run.config = config;
135
+ run.sessionId = sessionId;
136
+
137
+ void (async () => {
138
+ let buffer = '';
139
+
140
+ try {
141
+ for await (const chunk of helper) {
142
+ if (token !== run.token) break;
143
+ if (chunk.stream !== 'stdout') continue;
144
+ buffer += chunk.text;
145
+
146
+ for (let end = buffer.indexOf('\n'); end >= 0; end = buffer.indexOf('\n')) {
147
+ const line = buffer.slice(0, end);
148
+ buffer = buffer.slice(end + 1);
149
+ await apply($, token, line);
150
+ }
151
+ }
152
+ } catch {
153
+ // The helper ended or could not start: nothing is shown.
154
+ }
155
+ })();
156
+ }
157
+
158
+ /** The Stop button sends no Stop hook, so tell the daemon the turn is over (no turn id: the open one ends). */
159
+ async function reportEnd($: EngineInterface, reason: string): Promise<void> {
160
+ const { config, sessionId } = run;
161
+ if (config === null || sessionId === null) return;
162
+
163
+ try {
164
+ await $.process.run(
165
+ [config.node, config.script, 'idle', sessionId, reason === 'aborted' ? 'aborted' : 'error'],
166
+ {
167
+ timeoutMs: 5000,
168
+ },
169
+ );
170
+ } catch {
171
+ // The daemon will drop the turn by itself (display cap).
172
+ }
173
+ }
174
+
175
+ async function openDetails($: EngineInterface): Promise<void> {
176
+ try {
177
+ await $.ui.open({ id: PANE, title: 'Sponsored' });
178
+ } catch {
179
+ // Not placed: the line itself stays.
180
+ }
181
+ }
182
+
183
+ export const register: Register = (on) => {
184
+ on('turn.start', async ($, e, next) => {
185
+ // The main conversation only: a subagent's turn is not the person's turn.
186
+ if (e.agentId === undefined) {
187
+ await begin($);
188
+ }
189
+
190
+ return next(e);
191
+ });
192
+
193
+ on('turn.complete', async ($, e, next) => {
194
+ if (e.agentId === undefined) {
195
+ const wasShowing = run.helper !== null;
196
+ await clear($);
197
+
198
+ if (wasShowing && e.reason !== 'answer') {
199
+ await reportEnd($, e.reason);
200
+ }
201
+ }
202
+
203
+ return next(e);
204
+ });
205
+
206
+ on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
207
+ if (e.surface !== 'desktop' || e.props.hasSurvey) {
208
+ return next(e);
209
+ }
210
+
211
+ const value = await read($, shown);
212
+
213
+ if (value === null || value.card === null) {
214
+ return next(e);
215
+ }
216
+
217
+ const { Box, Text, Button } = $.ui.resolve(e);
218
+
219
+ return (
220
+ <Box>
221
+ <Text>{value.text} </Text>
222
+ <Button key="details" label="Details" onPress={() => openDetails($)} />
223
+ </Box>
224
+ );
225
+ });
226
+
227
+ on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
228
+ const { Box, Text, Link, Button } = $.ui.resolve(e);
229
+ const value = await read($, shown);
230
+ const card = value?.card ?? null;
231
+
232
+ if (value === null || card === null) {
233
+ return (
234
+ <Box flexDirection="column">
235
+ <Text dimColor>No sponsor right now.</Text>
236
+ </Box>
237
+ );
238
+ }
239
+
240
+ return (
241
+ <Box flexDirection="column">
242
+ <Text dimColor>{card.label}</Text>
243
+ <Text bold>
244
+ {card.sponsorName}: {card.headline}
245
+ </Text>
246
+ {card.body === '' ? null : <Text>{card.body}</Text>}
247
+ {card.snippet === '' ? null : <Text dimColor>{card.snippet}</Text>}
248
+ {card.promoCode === '' ? null : <Text>Code: {card.promoCode}</Text>}
249
+ {value.url === null ? null : <Link href={value.url}>Open the link</Link>}
250
+ <Button key="close" label="Close" onPress={() => $.ui.close({ id: PANE })} />
251
+ </Box>
252
+ );
253
+ });
254
+ };
@@ -0,0 +1,23 @@
1
+ /** The text of a sponsor card (what the Details pane draws). No images: this surface draws none. */
2
+ export type AvronCard = {
3
+ label: string;
4
+ sponsorName: string;
5
+ headline: string;
6
+ body: string;
7
+ snippet: string;
8
+ promoCode: string;
9
+ };
10
+
11
+ /** One sponsor line as the Avron helper prints it (packages/adapter-claude-code plugin-surface.ts). */
12
+ export type AvronShown = {
13
+ text: string;
14
+ sponsorName: string;
15
+ url: string | null;
16
+ card: AvronCard | null;
17
+ };
18
+
19
+ declare module 'claude-code' {
20
+ interface PluginState {
21
+ avron: { shown: AvronShown | null };
22
+ }
23
+ }
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "@wbcode/avron",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "One sponsor line in your status line while your AI coding agent is thinking. It clears when the agent stops.",
5
+ "license": "MIT",
6
+ "homepage": "https://avron.dev",
7
+ "bin": {
8
+ "avron": "avron.cjs"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "files": [
14
+ "avron.cjs",
15
+ "bin/",
16
+ "claude-plugin/",
17
+ "README.md",
18
+ "SECURITY.md",
19
+ "LICENSE"
20
+ ],
21
+ "engines": {
22
+ "node": ">=20"
23
+ },
24
+ "keywords": [
25
+ "claude-code",
26
+ "statusline",
27
+ "cli",
28
+ "sponsorship"
29
+ ]
30
+ }