nanobridge 0.2.2 → 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/chat/commands.js +1 -0
- package/src/commands/ask.js +74 -36
- package/src/commands/chat.js +37 -10
- package/src/commands/serve.js +1 -1
- package/src/utils/output.js +48 -0
- package/test/chat.test.js +6 -4
- package/test/files.test.js +1 -1
package/package.json
CHANGED
package/src/chat/commands.js
CHANGED
|
@@ -48,6 +48,7 @@ export function formatChatHelp(capabilities = {}) {
|
|
|
48
48
|
' /schema <file> Load JSON schema file (/schema clear to remove)',
|
|
49
49
|
' /context Show context window usage and token count',
|
|
50
50
|
' /settings Show current configuration and capabilities',
|
|
51
|
+
' /continue, /c Continue previous generation if cut off by token limit',
|
|
51
52
|
' /clear Clear conversation history (keeps config & sampling)',
|
|
52
53
|
' /new Start a fresh session with default configuration',
|
|
53
54
|
' /history Show turn history',
|
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';
|
|
@@ -82,14 +82,19 @@ ${style('Type', 'dim')} ${style('/help', 'yellow')} ${style('to see available co
|
|
|
82
82
|
promptUser();
|
|
83
83
|
|
|
84
84
|
for await (const line of rl) {
|
|
85
|
-
|
|
85
|
+
let rawInput = line.trim();
|
|
86
86
|
|
|
87
87
|
if (!rawInput) {
|
|
88
88
|
promptUser();
|
|
89
89
|
continue;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
let parsed = parseCommand(rawInput);
|
|
93
|
+
|
|
94
|
+
if (parsed.isCommand && (parsed.command === 'continue' || parsed.command === 'c')) {
|
|
95
|
+
rawInput = parsed.value ? `continue from where you left off: ${parsed.value}` : 'continue from where you left off';
|
|
96
|
+
parsed = { isCommand: false };
|
|
97
|
+
}
|
|
93
98
|
|
|
94
99
|
if (parsed.isCommand) {
|
|
95
100
|
const cmd = parsed.command;
|
|
@@ -361,8 +366,18 @@ Run ${style('/help', 'bold')} to see available commands.
|
|
|
361
366
|
}
|
|
362
367
|
|
|
363
368
|
rl.pause();
|
|
369
|
+
const spinner = createSpinner('Thinking...');
|
|
364
370
|
try {
|
|
365
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
|
+
|
|
366
381
|
if (daemon) {
|
|
367
382
|
accumulated = await streamPromptFromDaemon(daemon, {
|
|
368
383
|
promptText: finalPrompt,
|
|
@@ -370,24 +385,36 @@ Run ${style('/help', 'bold')} to see available commands.
|
|
|
370
385
|
samplingMode: config.samplingMode,
|
|
371
386
|
sessionType: 'chat',
|
|
372
387
|
responseConstraint,
|
|
373
|
-
onChunk:
|
|
374
|
-
process.stdout.write(delta);
|
|
375
|
-
}
|
|
388
|
+
onChunk: onDelta
|
|
376
389
|
});
|
|
377
390
|
} else if (sessionManager) {
|
|
378
391
|
accumulated = await sessionManager.promptStreaming(finalPrompt, {
|
|
379
|
-
onChunk:
|
|
380
|
-
process.stdout.write(delta);
|
|
381
|
-
}
|
|
392
|
+
onChunk: onDelta
|
|
382
393
|
});
|
|
383
394
|
}
|
|
384
395
|
|
|
396
|
+
spinner.stop();
|
|
397
|
+
|
|
385
398
|
history.addTurn('user', finalPrompt);
|
|
386
399
|
history.addTurn('assistant', accumulated);
|
|
387
|
-
process.stdout.write('\n
|
|
400
|
+
process.stdout.write('\n');
|
|
401
|
+
|
|
402
|
+
const trimmedAcc = (accumulated || '').trim();
|
|
403
|
+
const likelyTruncated = trimmedAcc.length > 300 &&
|
|
404
|
+
!/[.?!:;]\s*$/.test(trimmedAcc) &&
|
|
405
|
+
!trimmedAcc.endsWith('```') &&
|
|
406
|
+
!trimmedAcc.endsWith('}') &&
|
|
407
|
+
!trimmedAcc.endsWith(']');
|
|
408
|
+
|
|
409
|
+
if (likelyTruncated) {
|
|
410
|
+
log(style('↳ [Response reached token limit. Type "continue" or /c to resume]', 'dim'));
|
|
411
|
+
}
|
|
412
|
+
process.stdout.write('\n');
|
|
388
413
|
} catch (err) {
|
|
414
|
+
spinner.stop();
|
|
389
415
|
log(`\n${style('Error:', 'red')} ${err.message}\n`);
|
|
390
416
|
} finally {
|
|
417
|
+
spinner.stop();
|
|
391
418
|
rl.resume();
|
|
392
419
|
}
|
|
393
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'), {
|
|
@@ -49,6 +49,8 @@ describe('Chat Slash Command Parser', () => {
|
|
|
49
49
|
assert.strictEqual(parseCommand('/new').command, 'new');
|
|
50
50
|
assert.strictEqual(parseCommand('/settings').command, 'settings');
|
|
51
51
|
assert.strictEqual(parseCommand('/help').command, 'help');
|
|
52
|
+
assert.strictEqual(parseCommand('/continue').command, 'continue');
|
|
53
|
+
assert.strictEqual(parseCommand('/c').command, 'c');
|
|
52
54
|
});
|
|
53
55
|
|
|
54
56
|
test('recognizes exit aliases', () => {
|
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');
|