claude_stream_viewer 1.0.14 → 1.0.16

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.
@@ -1,208 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- // Reads Claude API stream-json events line-by-line from stdin and pretty-prints
4
- // them with colorized output. Designed to wrap an upstream `claude --stream-json`
5
- // pipe, e.g. `claude … | npx claude_stream_viewer`.
6
- // Usage:
7
- // claude --stream-json | npx claude_stream_viewer
8
- //
9
- // claude --output-format stream-json --verbose --include-partial-messages --permission-mode auto -p "explain quantum computing like I'm 5" | npx claude_stream_viewer
10
-
11
-
12
- import Readline from 'node:readline';
13
- import Fs from 'node:fs';
14
- import * as Commander from 'commander';
15
- import Chalk from 'chalk';
16
- import Path from 'node:path';
17
-
18
- const __dirname = new URL('.', import.meta.url).pathname;
19
-
20
- type CliOptions = {
21
- color: boolean;
22
- };
23
-
24
- // Partial typing on purpose: stream-json's shape varies across SDK versions and
25
- // event subtypes, and we only consume the few fields we render. Anything we
26
- // don't recognize falls through to the UNKNOWN EVENT branch so the user can
27
- // extend this type incrementally as new shapes show up.
28
- type ClaudeEvent = {
29
- type?: string;
30
- event?: {
31
- type?: string;
32
- delta?: {
33
- type?: string;
34
- text?: string;
35
- };
36
- };
37
- // Legacy `message_delta` format places the text delta one level higher,
38
- // directly on the root event rather than inside `event.delta`.
39
- delta?: {
40
- text?: string;
41
- };
42
- message?: {
43
- content?: Array<{ type?: string; text?: string }>;
44
- };
45
- };
46
-
47
- const colors = {
48
- text: Chalk.white,
49
- tool: Chalk.cyan,
50
- system: Chalk.gray,
51
- error: Chalk.red,
52
- json: Chalk.dim,
53
- header: Chalk.yellow.bold,
54
- };
55
-
56
- ///////////////////////////////////////////////////////////////////////////////
57
- ///////////////////////////////////////////////////////////////////////////////
58
- // class MainHelper
59
- ///////////////////////////////////////////////////////////////////////////////
60
- ///////////////////////////////////////////////////////////////////////////////
61
-
62
- class MainHelper {
63
- static printText(text: string) {
64
- process.stdout.write(colors.text(text));
65
- }
66
-
67
- static printNewline() {
68
- process.stdout.write('\n');
69
- }
70
-
71
- static printHeader(label: string) {
72
- MainHelper.printNewline();
73
- console.log(colors.header(`\n=== ${label} ===`));
74
- }
75
-
76
- static printJSON(obj: unknown) {
77
- console.log(colors.json(JSON.stringify(obj, null, 2)));
78
- }
79
-
80
- static handleEvent(data: ClaudeEvent) {
81
- try {
82
- // Newer SDKs wrap each event in a `stream_event` envelope.
83
- if (data.type === 'stream_event' && data.event) {
84
- const evt = data.event;
85
-
86
- if (evt.delta?.type === 'text_delta' && evt.delta.text) {
87
- MainHelper.printText(evt.delta.text);
88
- return;
89
- }
90
-
91
- if (evt.type === 'tool_use') {
92
- MainHelper.printHeader('TOOL USE');
93
- MainHelper.printJSON(evt);
94
- return;
95
- }
96
-
97
- // `content_block_delta` is the parent envelope of `text_delta` —
98
- // already rendered above, so skip it here to avoid duplicating
99
- // the text stream as a JSON dump.
100
- if (evt.type && evt.type !== 'content_block_delta') {
101
- MainHelper.printHeader(`EVENT: ${evt.type}`);
102
- MainHelper.printJSON(evt);
103
- return;
104
- }
105
- }
106
-
107
- // Legacy format: pre-`stream_event` SDKs emit raw `message_delta`
108
- // events with the text delta hanging off the root.
109
- if (data.type === 'message_delta') {
110
- const text = data.delta?.text;
111
- if (text) {
112
- MainHelper.printText(text);
113
- return;
114
- }
115
- }
116
-
117
- // Final assistant message bundling all content blocks for the turn.
118
- if (data.message?.content) {
119
- MainHelper.printHeader('FINAL MESSAGE');
120
- for (const block of data.message.content) {
121
- if (block.text) {
122
- console.log(colors.text(block.text));
123
- }
124
- }
125
- return;
126
- }
127
-
128
- // Unrecognized shape — dump it raw so the user can extend ClaudeEvent.
129
- MainHelper.printHeader('UNKNOWN EVENT');
130
- MainHelper.printJSON(data);
131
- } catch (err) {
132
- // Never let a single malformed event take down the stream — log and move on.
133
- console.error(colors.error('Error processing event:'), err);
134
- MainHelper.printJSON(data);
135
- }
136
- }
137
- }
138
-
139
- ///////////////////////////////////////////////////////////////////////////////
140
- ///////////////////////////////////////////////////////////////////////////////
141
- // function main
142
- ///////////////////////////////////////////////////////////////////////////////
143
- ///////////////////////////////////////////////////////////////////////////////
144
-
145
- async function main(): Promise<void> {
146
-
147
- const packageJsonPath = Path.join(__dirname, '..', 'package.json');
148
- const packageJson: object = JSON.parse(await Fs.promises.readFile(packageJsonPath, 'utf-8'))
149
- const packageVersion = (packageJson as { version?: string }).version ?? 'unknown';
150
-
151
- ///////////////////////////////////////////////////////////////////////////////
152
- ///////////////////////////////////////////////////////////////////////////////
153
- //
154
- ///////////////////////////////////////////////////////////////////////////////
155
- ///////////////////////////////////////////////////////////////////////////////
156
-
157
- const program = new Commander.Command();
158
- program
159
- .name('claude_stream_viewer')
160
- .description('Pretty-prints Claude API stream-json events from stdin with colorized output.')
161
- .version(packageVersion)
162
- .option('--no-color', 'disable colored output')
163
- .parse(process.argv);
164
-
165
- const options = program.opts<CliOptions>();
166
- ///////////////////////////////////////////////////////////////////////////////
167
- ///////////////////////////////////////////////////////////////////////////////
168
- //
169
- ///////////////////////////////////////////////////////////////////////////////
170
- ///////////////////////////////////////////////////////////////////////////////
171
-
172
- if (options.color === false) {
173
- Chalk.level = 0;
174
- }
175
-
176
- const readline = Readline.createInterface({
177
- input: process.stdin,
178
- crlfDelay: Infinity,
179
- });
180
-
181
- readline.on('line', (line) => {
182
- // stream-json pipes occasionally pad with blank lines between events.
183
- if (line.trim() === '') return;
184
-
185
- try {
186
- const parsed = JSON.parse(line);
187
- MainHelper.handleEvent(parsed);
188
- } catch (err) {
189
- console.error(colors.error('Invalid JSON:'), line);
190
- }
191
- });
192
-
193
- await new Promise<void>((resolve) => {
194
- readline.on('close', () => {
195
- MainHelper.printNewline();
196
- console.log(colors.system('\n[stream ended]'));
197
- resolve();
198
- });
199
- });
200
- }
201
-
202
- ///////////////////////////////////////////////////////////////////////////////
203
- ///////////////////////////////////////////////////////////////////////////////
204
- //
205
- ///////////////////////////////////////////////////////////////////////////////
206
- ///////////////////////////////////////////////////////////////////////////////
207
-
208
- await main();