nanobridge 0.2.3 → 0.2.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/package.json +1 -1
- package/src/commands/ask.js +74 -36
- package/src/commands/chat.js +19 -9
- package/src/commands/serve.js +1 -1
- package/src/utils/output.js +48 -0
- package/test/chat.test.js +4 -4
- package/test/files.test.js +1 -1
package/package.json
CHANGED
package/src/commands/ask.js
CHANGED
|
@@ -3,7 +3,7 @@ import fs from 'node:fs';
|
|
|
3
3
|
import { launchChrome } from '../chrome/launch.js';
|
|
4
4
|
import { ChromeBridge } from '../chrome/bridge.js';
|
|
5
5
|
import { ensureDaemon, streamPromptFromDaemon } from '../daemon/client.js';
|
|
6
|
-
import { style, log, logError, debug } from '../utils/output.js';
|
|
6
|
+
import { style, log, logError, debug, createSpinner } from '../utils/output.js';
|
|
7
7
|
import { ModelUnavailableError, NanoBridgeError } from '../utils/errors.js';
|
|
8
8
|
import { readFileContent, formatFileAsContext, extractAndAttachFiles } from '../utils/files.js';
|
|
9
9
|
|
|
@@ -113,26 +113,43 @@ export async function handleAsk(promptArg, options = {}) {
|
|
|
113
113
|
|
|
114
114
|
let fullOutput = '';
|
|
115
115
|
if (stream) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
116
|
+
const spinner = createSpinner('Thinking...');
|
|
117
|
+
let isFirstChunk = true;
|
|
118
|
+
try {
|
|
119
|
+
fullOutput = await streamPromptFromDaemon({
|
|
120
|
+
port: daemon.port,
|
|
121
|
+
promptText: finalPrompt,
|
|
122
|
+
systemPrompt: options.system,
|
|
123
|
+
stream: true,
|
|
124
|
+
onChunk: (delta) => {
|
|
125
|
+
if (isFirstChunk) {
|
|
126
|
+
spinner.stop();
|
|
127
|
+
isFirstChunk = false;
|
|
128
|
+
}
|
|
129
|
+
process.stdout.write(delta);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
spinner.stop();
|
|
133
|
+
if (!fullOutput.endsWith('\n')) {
|
|
134
|
+
process.stdout.write('\n');
|
|
123
135
|
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
process.stdout.write('\n');
|
|
136
|
+
} finally {
|
|
137
|
+
spinner.stop();
|
|
127
138
|
}
|
|
128
139
|
} else {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
140
|
+
const spinner = createSpinner('Thinking...');
|
|
141
|
+
try {
|
|
142
|
+
fullOutput = await streamPromptFromDaemon({
|
|
143
|
+
port: daemon.port,
|
|
144
|
+
promptText: finalPrompt,
|
|
145
|
+
systemPrompt: options.system,
|
|
146
|
+
stream: false
|
|
147
|
+
});
|
|
148
|
+
spinner.stop();
|
|
149
|
+
process.stdout.write(fullOutput + (fullOutput.endsWith('\n') ? '' : '\n'));
|
|
150
|
+
} finally {
|
|
151
|
+
spinner.stop();
|
|
152
|
+
}
|
|
136
153
|
}
|
|
137
154
|
return;
|
|
138
155
|
} catch (err) {
|
|
@@ -145,32 +162,53 @@ export async function handleAsk(promptArg, options = {}) {
|
|
|
145
162
|
let bridge = null;
|
|
146
163
|
|
|
147
164
|
try {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
165
|
+
const spinner = createSpinner('Starting Chrome and preparing model...');
|
|
166
|
+
try {
|
|
167
|
+
chromeInstance = await launchChrome(options);
|
|
168
|
+
bridge = new ChromeBridge(chromeInstance);
|
|
169
|
+
await bridge.connect();
|
|
170
|
+
} catch (launchErr) {
|
|
171
|
+
spinner.stop();
|
|
172
|
+
throw launchErr;
|
|
173
|
+
}
|
|
151
174
|
|
|
152
175
|
const stream = options.stream !== false;
|
|
153
176
|
let fullOutput = '';
|
|
154
177
|
|
|
155
178
|
if (stream) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
179
|
+
let isFirstChunk = true;
|
|
180
|
+
try {
|
|
181
|
+
fullOutput = await bridge.prompt({
|
|
182
|
+
promptText: finalPrompt,
|
|
183
|
+
systemPrompt: options.system,
|
|
184
|
+
stream: true,
|
|
185
|
+
onChunk: (delta) => {
|
|
186
|
+
if (isFirstChunk) {
|
|
187
|
+
spinner.stop();
|
|
188
|
+
isFirstChunk = false;
|
|
189
|
+
}
|
|
190
|
+
process.stdout.write(delta);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
spinner.stop();
|
|
194
|
+
if (!fullOutput.endsWith('\n')) {
|
|
195
|
+
process.stdout.write('\n');
|
|
162
196
|
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
process.stdout.write('\n');
|
|
197
|
+
} finally {
|
|
198
|
+
spinner.stop();
|
|
166
199
|
}
|
|
167
200
|
} else {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
201
|
+
try {
|
|
202
|
+
fullOutput = await bridge.prompt({
|
|
203
|
+
promptText: finalPrompt,
|
|
204
|
+
systemPrompt: options.system,
|
|
205
|
+
stream: false
|
|
206
|
+
});
|
|
207
|
+
spinner.stop();
|
|
208
|
+
process.stdout.write(fullOutput + (fullOutput.endsWith('\n') ? '' : '\n'));
|
|
209
|
+
} finally {
|
|
210
|
+
spinner.stop();
|
|
211
|
+
}
|
|
174
212
|
}
|
|
175
213
|
} finally {
|
|
176
214
|
if (bridge) await bridge.close();
|
package/src/commands/chat.js
CHANGED
|
@@ -3,7 +3,7 @@ import process from 'node:process';
|
|
|
3
3
|
import { ensureDaemon, streamPromptFromDaemon, resetDaemon } from '../daemon/client.js';
|
|
4
4
|
import { launchChrome } from '../chrome/launch.js';
|
|
5
5
|
import { ChromeBridge } from '../chrome/bridge.js';
|
|
6
|
-
import { log, style, debug } from '../utils/output.js';
|
|
6
|
+
import { log, style, debug, createSpinner } from '../utils/output.js';
|
|
7
7
|
import { NanoBridgeError } from '../utils/errors.js';
|
|
8
8
|
import { readFileContent, formatFileAsContext, extractAndAttachFiles } from '../utils/files.js';
|
|
9
9
|
import { ChatSessionManager } from '../chat/session.js';
|
|
@@ -92,7 +92,7 @@ ${style('Type', 'dim')} ${style('/help', 'yellow')} ${style('to see available co
|
|
|
92
92
|
let parsed = parseCommand(rawInput);
|
|
93
93
|
|
|
94
94
|
if (parsed.isCommand && (parsed.command === 'continue' || parsed.command === 'c')) {
|
|
95
|
-
rawInput = parsed.value ? `
|
|
95
|
+
rawInput = parsed.value ? `continue from where you left off: ${parsed.value}` : 'continue from where you left off';
|
|
96
96
|
parsed = { isCommand: false };
|
|
97
97
|
}
|
|
98
98
|
|
|
@@ -366,8 +366,18 @@ Run ${style('/help', 'bold')} to see available commands.
|
|
|
366
366
|
}
|
|
367
367
|
|
|
368
368
|
rl.pause();
|
|
369
|
+
const spinner = createSpinner('Thinking...');
|
|
369
370
|
try {
|
|
370
371
|
let accumulated = '';
|
|
372
|
+
let isFirstChunk = true;
|
|
373
|
+
const onDelta = (delta) => {
|
|
374
|
+
if (isFirstChunk) {
|
|
375
|
+
spinner.stop();
|
|
376
|
+
isFirstChunk = false;
|
|
377
|
+
}
|
|
378
|
+
process.stdout.write(delta);
|
|
379
|
+
};
|
|
380
|
+
|
|
371
381
|
if (daemon) {
|
|
372
382
|
accumulated = await streamPromptFromDaemon(daemon, {
|
|
373
383
|
promptText: finalPrompt,
|
|
@@ -375,18 +385,16 @@ Run ${style('/help', 'bold')} to see available commands.
|
|
|
375
385
|
samplingMode: config.samplingMode,
|
|
376
386
|
sessionType: 'chat',
|
|
377
387
|
responseConstraint,
|
|
378
|
-
onChunk:
|
|
379
|
-
process.stdout.write(delta);
|
|
380
|
-
}
|
|
388
|
+
onChunk: onDelta
|
|
381
389
|
});
|
|
382
390
|
} else if (sessionManager) {
|
|
383
391
|
accumulated = await sessionManager.promptStreaming(finalPrompt, {
|
|
384
|
-
onChunk:
|
|
385
|
-
process.stdout.write(delta);
|
|
386
|
-
}
|
|
392
|
+
onChunk: onDelta
|
|
387
393
|
});
|
|
388
394
|
}
|
|
389
395
|
|
|
396
|
+
spinner.stop();
|
|
397
|
+
|
|
390
398
|
history.addTurn('user', finalPrompt);
|
|
391
399
|
history.addTurn('assistant', accumulated);
|
|
392
400
|
process.stdout.write('\n');
|
|
@@ -399,12 +407,14 @@ Run ${style('/help', 'bold')} to see available commands.
|
|
|
399
407
|
!trimmedAcc.endsWith(']');
|
|
400
408
|
|
|
401
409
|
if (likelyTruncated) {
|
|
402
|
-
log(style('↳ [
|
|
410
|
+
log(style('↳ [Response reached token limit. Type "continue" or /c to resume]', 'dim'));
|
|
403
411
|
}
|
|
404
412
|
process.stdout.write('\n');
|
|
405
413
|
} catch (err) {
|
|
414
|
+
spinner.stop();
|
|
406
415
|
log(`\n${style('Error:', 'red')} ${err.message}\n`);
|
|
407
416
|
} finally {
|
|
417
|
+
spinner.stop();
|
|
408
418
|
rl.resume();
|
|
409
419
|
}
|
|
410
420
|
|
package/src/commands/serve.js
CHANGED
|
@@ -32,7 +32,7 @@ export async function handleServe(options = {}) {
|
|
|
32
32
|
-H "Content-Type: application/json" \\
|
|
33
33
|
-d '{
|
|
34
34
|
"model": "gemini-nano",
|
|
35
|
-
"messages": [{"role": "user", "content": "
|
|
35
|
+
"messages": [{"role": "user", "content": "Hello!"}],
|
|
36
36
|
"stream": true
|
|
37
37
|
}'`);
|
|
38
38
|
log('');
|
package/src/utils/output.js
CHANGED
|
@@ -57,3 +57,51 @@ export function renderProgressBar(percent, length = 24) {
|
|
|
57
57
|
const empty = '-'.repeat(emptyLength);
|
|
58
58
|
return `[${filled}${empty}] ${Math.round(clamped)}%`;
|
|
59
59
|
}
|
|
60
|
+
|
|
61
|
+
export function createSpinner(initialText = 'Thinking...') {
|
|
62
|
+
if (!process.stderr.isTTY || isJsonMode) {
|
|
63
|
+
return {
|
|
64
|
+
setText: () => {},
|
|
65
|
+
stop: () => {}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
|
70
|
+
let frameIdx = 0;
|
|
71
|
+
let text = initialText;
|
|
72
|
+
let stopped = false;
|
|
73
|
+
const startTime = Date.now();
|
|
74
|
+
|
|
75
|
+
const render = () => {
|
|
76
|
+
if (stopped) return;
|
|
77
|
+
const elapsed = Date.now() - startTime;
|
|
78
|
+
if (elapsed > 2200 && text === initialText) {
|
|
79
|
+
text = 'Warming up on-device model (compiling WebGPU shaders)...';
|
|
80
|
+
} else if (elapsed > 8000 && text === 'Warming up on-device model (compiling WebGPU shaders)...') {
|
|
81
|
+
text = 'Loading model weights, preparing first token...';
|
|
82
|
+
}
|
|
83
|
+
const frame = style(frames[frameIdx % frames.length], 'cyan');
|
|
84
|
+
const label = style(text, 'dim');
|
|
85
|
+
process.stderr.write(`\r${frame} ${label} `);
|
|
86
|
+
frameIdx++;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
render();
|
|
90
|
+
const timer = setInterval(render, 80);
|
|
91
|
+
if (timer.unref) timer.unref();
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
setText(newText) {
|
|
95
|
+
text = newText;
|
|
96
|
+
},
|
|
97
|
+
stop() {
|
|
98
|
+
if (stopped) return;
|
|
99
|
+
stopped = true;
|
|
100
|
+
clearInterval(timer);
|
|
101
|
+
if (process.stderr.isTTY) {
|
|
102
|
+
process.stderr.write('\r\x1b[K');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
package/test/chat.test.js
CHANGED
|
@@ -18,12 +18,12 @@ describe('Chat Slash Command Parser', () => {
|
|
|
18
18
|
raw: '/sampling creative'
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
assert.deepEqual(parseCommand('/system Answer
|
|
21
|
+
assert.deepEqual(parseCommand('/system Answer concisely and factually.'), {
|
|
22
22
|
isCommand: true,
|
|
23
23
|
command: 'system',
|
|
24
|
-
value: 'Answer
|
|
25
|
-
args: ['Answer', '
|
|
26
|
-
raw: '/system Answer
|
|
24
|
+
value: 'Answer concisely and factually.',
|
|
25
|
+
args: ['Answer', 'concisely', 'and', 'factually.'],
|
|
26
|
+
raw: '/system Answer concisely and factually.'
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
assert.deepEqual(parseCommand('/json on'), {
|
package/test/files.test.js
CHANGED
|
@@ -26,7 +26,7 @@ describe('File Reading & Context Utilities', () => {
|
|
|
26
26
|
assert.strictEqual(res1.attachedFiles[0].filename, 'package.json');
|
|
27
27
|
assert.ok(res1.finalPrompt.includes('--- File: package.json ---'));
|
|
28
28
|
|
|
29
|
-
const input2 = '@file package.json
|
|
29
|
+
const input2 = '@file package.json which packages are used?';
|
|
30
30
|
const res2 = extractAndAttachFiles(input2);
|
|
31
31
|
assert.strictEqual(res2.attachedFiles.length, 1);
|
|
32
32
|
assert.strictEqual(res2.attachedFiles[0].filename, 'package.json');
|