@vincentbanana/bananacli 0.8.1 → 0.8.3
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/dist/server/ui-server.js +112 -18
- package/dist/tools/todo.js +5 -2
- package/dist/ui/app.js +570 -106
- package/dist/ui/index.html +5 -5
- package/dist/ui/style.css +87 -6
- package/package.json +1 -1
package/dist/server/ui-server.js
CHANGED
|
@@ -253,7 +253,11 @@ async function attachmentContextFor(items) {
|
|
|
253
253
|
/* 读不到就退化成路径说明 */
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
|
-
if (kind === '
|
|
256
|
+
if (kind === 'text') {
|
|
257
|
+
// 文本文件过大或读取失败时,至少把路径和元信息交给模型,不能静默丢掉。
|
|
258
|
+
blocks.push(`### 附件:${meta}\n路径:${item.path}\n(文件较大或读取失败,未自动注入正文;需要时用工具按路径读取。)`);
|
|
259
|
+
}
|
|
260
|
+
else if (kind === 'pdf' || kind === 'binary') {
|
|
257
261
|
blocks.push(`### 附件:${meta}\n路径:${item.path}\n(未解析正文;需要时用工具按路径读取或让用户提供预览。)`);
|
|
258
262
|
}
|
|
259
263
|
}
|
|
@@ -313,7 +317,7 @@ export async function startUiServer(options) {
|
|
|
313
317
|
const provider = () => createProvider(config.current, config);
|
|
314
318
|
const backups = (sessionId) => new BackupStore(join(bananaHome(), 'backups'), sessionId);
|
|
315
319
|
const info = async (session) => ({
|
|
316
|
-
version: '0.8.
|
|
320
|
+
version: '0.8.3',
|
|
317
321
|
provider: { name: config.current, model: config.providers[config.current]?.model ?? '', protocol: config.providers[config.current]?.protocol ?? 'openai' },
|
|
318
322
|
reasoning: {
|
|
319
323
|
level: session.reasoning ?? 'standard',
|
|
@@ -340,7 +344,11 @@ export async function startUiServer(options) {
|
|
|
340
344
|
session: {
|
|
341
345
|
id: session.id,
|
|
342
346
|
title: session.title,
|
|
343
|
-
|
|
347
|
+
// 图片 base64 和附件全文只用于请求模型,不必每次 /api/info 全量回传前端。
|
|
348
|
+
messages: session.messages.map(({ images, attachmentContext, ...message }) => ({
|
|
349
|
+
...message,
|
|
350
|
+
...(images?.length ? { imageCount: images.length } : {}),
|
|
351
|
+
})),
|
|
344
352
|
todos: session.todos ?? [],
|
|
345
353
|
usage: session.usage ?? { inputTokens: 0, outputTokens: 0 },
|
|
346
354
|
mode: session.mode ?? 'build',
|
|
@@ -554,14 +562,35 @@ export async function startUiServer(options) {
|
|
|
554
562
|
}
|
|
555
563
|
if (path === '/api/session' && req.method === 'POST') {
|
|
556
564
|
const body = await readJson(req);
|
|
565
|
+
// 一轮还没结束时不要换会话:否则旧 turn 的结果会写到新会话,界面标题和正文也会串台。
|
|
566
|
+
const busyAction = body.action === 'new' ||
|
|
567
|
+
body.action === 'switch' ||
|
|
568
|
+
body.action === 'delete' ||
|
|
569
|
+
body.action === 'clear';
|
|
570
|
+
if (turn && busyAction) {
|
|
571
|
+
return json(res, 409, { error: '正在生成,请先停止当前任务再切换会话' });
|
|
572
|
+
}
|
|
573
|
+
if (body.action === 'load') {
|
|
574
|
+
const found = body.ref ? await store.find(body.ref) : null;
|
|
575
|
+
if (!found)
|
|
576
|
+
return json(res, 404, { error: '没找到这个对话' });
|
|
577
|
+
return json(res, 200, { session: found });
|
|
578
|
+
}
|
|
557
579
|
if (body.action === 'new') {
|
|
558
580
|
session = store.create(body.title?.trim() || '新对话');
|
|
581
|
+
// 用户明确指定了标题,后续自动标题不要覆盖它。
|
|
582
|
+
if (body.title?.trim())
|
|
583
|
+
session.titleSource = 'user';
|
|
559
584
|
await store.save(session);
|
|
560
585
|
}
|
|
561
586
|
else if (body.action === 'rename') {
|
|
562
|
-
|
|
563
|
-
session.
|
|
564
|
-
|
|
587
|
+
// 支持直接重命名侧栏里的某个会话,不必先把用户切过去。
|
|
588
|
+
const target = body.ref && body.ref !== session.id ? await store.find(body.ref) : session;
|
|
589
|
+
if (!target)
|
|
590
|
+
return json(res, 404, { error: '没找到这个对话' });
|
|
591
|
+
target.title = body.title?.trim() || target.title;
|
|
592
|
+
target.titleSource = 'user';
|
|
593
|
+
await store.save(target);
|
|
565
594
|
}
|
|
566
595
|
else if (body.action === 'switch') {
|
|
567
596
|
const found = body.ref ? await store.find(body.ref) : null;
|
|
@@ -601,8 +630,6 @@ export async function startUiServer(options) {
|
|
|
601
630
|
await store.save(session);
|
|
602
631
|
}
|
|
603
632
|
}
|
|
604
|
-
if (turn)
|
|
605
|
-
turn.session = session;
|
|
606
633
|
activeId = session.id;
|
|
607
634
|
return json(res, 200, { session: session.id, sessions: await listSessions(), info: await info(session) });
|
|
608
635
|
}
|
|
@@ -687,23 +714,30 @@ export async function startUiServer(options) {
|
|
|
687
714
|
before = '';
|
|
688
715
|
}
|
|
689
716
|
}
|
|
717
|
+
let afterExists = false;
|
|
718
|
+
let tooLarge = false;
|
|
690
719
|
try {
|
|
691
720
|
const info = await stat(change.path);
|
|
692
|
-
|
|
721
|
+
afterExists = info.isFile();
|
|
722
|
+
if (afterExists && info.size <= MAX_FILE_BYTES)
|
|
693
723
|
after = await readFile(change.path, 'utf8');
|
|
724
|
+
else if (afterExists)
|
|
725
|
+
tooLarge = true;
|
|
694
726
|
}
|
|
695
727
|
catch {
|
|
696
|
-
|
|
728
|
+
afterExists = false;
|
|
697
729
|
}
|
|
698
730
|
const diff = lineDiff(before, after);
|
|
699
731
|
files.push({
|
|
700
732
|
path: change.relative,
|
|
701
733
|
absolute: change.path,
|
|
702
734
|
existed: change.existed,
|
|
703
|
-
|
|
735
|
+
// 大文件读不出内容不等于文件被删了。
|
|
736
|
+
missing: !afterExists && change.existed,
|
|
737
|
+
tooLarge,
|
|
704
738
|
additions: diff.additions,
|
|
705
739
|
deletions: diff.deletions,
|
|
706
|
-
truncated: diff.truncated,
|
|
740
|
+
truncated: diff.truncated || tooLarge,
|
|
707
741
|
lines: diff.lines,
|
|
708
742
|
});
|
|
709
743
|
}
|
|
@@ -753,7 +787,8 @@ export async function startUiServer(options) {
|
|
|
753
787
|
delete current.apiKey;
|
|
754
788
|
}
|
|
755
789
|
}
|
|
756
|
-
else if (body.action === 'system' && body.value) {
|
|
790
|
+
else if (body.action === 'system' && typeof body.value === 'string') {
|
|
791
|
+
// 空字符串表示恢复内置提示词,不能按 falsy 忽略。
|
|
757
792
|
config.systemPrompt = body.value;
|
|
758
793
|
}
|
|
759
794
|
else if (body.action === 'showReasoning') {
|
|
@@ -767,6 +802,13 @@ export async function startUiServer(options) {
|
|
|
767
802
|
config.workspace = next;
|
|
768
803
|
// 换工作区后终端也要跟着换,否则 cd 还停在旧目录
|
|
769
804
|
terminalCwd = null;
|
|
805
|
+
// 项目级插件依赖 workspace,切目录后立即重载。
|
|
806
|
+
try {
|
|
807
|
+
await syncPlugins();
|
|
808
|
+
}
|
|
809
|
+
catch {
|
|
810
|
+
/* 插件加载失败不影响工作区切换 */
|
|
811
|
+
}
|
|
770
812
|
}
|
|
771
813
|
const { saveConfig } = await import("../config/config.js");
|
|
772
814
|
await saveConfig(config);
|
|
@@ -782,11 +824,17 @@ export async function startUiServer(options) {
|
|
|
782
824
|
return json(res, 200, { info: await info(session) });
|
|
783
825
|
}
|
|
784
826
|
if (path === '/api/undo' && req.method === 'POST') {
|
|
827
|
+
if (turn)
|
|
828
|
+
return json(res, 409, { error: '正在生成,请先停止当前任务再撤销' });
|
|
785
829
|
const result = await backups(session.id).undo();
|
|
786
830
|
return json(res, 200, { restored: result.restored, deleted: result.deleted, had: Boolean(result.turn) });
|
|
787
831
|
}
|
|
788
832
|
if (path === '/api/attach' && req.method === 'POST') {
|
|
789
833
|
const name = decodeURIComponent(new URL(req.url ?? '/', 'http://x').searchParams.get('name') ?? 'file');
|
|
834
|
+
// base64 会有约 4/3 膨胀,先按上限拒绝明显超大的请求,避免整包读进内存。
|
|
835
|
+
if (Number(req.headers['content-length'] ?? 0) > MAX_ATTACHMENT_BYTES * 1.4) {
|
|
836
|
+
return json(res, 413, { error: '附件超过 20MB' });
|
|
837
|
+
}
|
|
790
838
|
const body = await readBody(req);
|
|
791
839
|
const buffer = Buffer.from(body, 'base64');
|
|
792
840
|
if (buffer.byteLength > MAX_ATTACHMENT_BYTES) {
|
|
@@ -856,8 +904,34 @@ export async function startUiServer(options) {
|
|
|
856
904
|
sessions: await listSessions(),
|
|
857
905
|
});
|
|
858
906
|
}
|
|
907
|
+
if (path === '/api/title' && req.method === 'POST') {
|
|
908
|
+
// 标题在 done 之后异步生成,避免把 3 秒的标题请求算进本轮用时、也不让界面多转一次圈。
|
|
909
|
+
if (turn)
|
|
910
|
+
return json(res, 409, { error: '正在生成,稍后再生成标题' });
|
|
911
|
+
const body = await readJson(req);
|
|
912
|
+
let target = session;
|
|
913
|
+
if (body.session && body.session !== session.id) {
|
|
914
|
+
const found = await store.load(body.session);
|
|
915
|
+
if (!found)
|
|
916
|
+
return json(res, 404, { error: '没找到这个对话' });
|
|
917
|
+
target = found;
|
|
918
|
+
}
|
|
919
|
+
if (target.titleSource === 'user') {
|
|
920
|
+
return json(res, 200, { title: target.title, session: target.id, sessions: await listSessions() });
|
|
921
|
+
}
|
|
922
|
+
const hasAssistant = target.messages.some((message) => message.role === 'assistant');
|
|
923
|
+
if (!hasAssistant) {
|
|
924
|
+
return json(res, 200, { title: target.title, session: target.id, sessions: await listSessions() });
|
|
925
|
+
}
|
|
926
|
+
target.title = await generateSessionTitle(target);
|
|
927
|
+
await store.save(target);
|
|
928
|
+
return json(res, 200, { title: target.title, session: target.id, sessions: await listSessions() });
|
|
929
|
+
}
|
|
859
930
|
if (path === '/api/chat' && req.method === 'POST') {
|
|
860
931
|
const body = await readJson(req);
|
|
932
|
+
if (typeof body.message !== 'string' || !body.message.trim()) {
|
|
933
|
+
return json(res, 400, { error: '消息不能为空' });
|
|
934
|
+
}
|
|
861
935
|
if (turn)
|
|
862
936
|
return json(res, 409, { error: '当前正在跑一轮对话' });
|
|
863
937
|
// 前端会带上会话 id:以它为准,避免新建对话后又被解析回旧对话
|
|
@@ -1248,9 +1322,13 @@ export async function startUiServer(options) {
|
|
|
1248
1322
|
};
|
|
1249
1323
|
}
|
|
1250
1324
|
}
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1325
|
+
// 先给一个首条消息截断的临时标题,避免 done 之后标题还是一团空白;
|
|
1326
|
+
// 模型标题稍后由 /api/title 异步替换。
|
|
1327
|
+
if (wasFirstTurn &&
|
|
1328
|
+
session.titleSource !== 'user' &&
|
|
1329
|
+
(!session.title || session.title === '新对话')) {
|
|
1330
|
+
const firstUser = session.messages.find((message) => message.role === 'user');
|
|
1331
|
+
session.title = deriveTitle(firstUser?.content ?? '');
|
|
1254
1332
|
}
|
|
1255
1333
|
const pricing = resolvePricing(session.model, config.providers[config.current]?.pricing);
|
|
1256
1334
|
const cost = estimateCost(result.usage, pricing);
|
|
@@ -1261,6 +1339,8 @@ export async function startUiServer(options) {
|
|
|
1261
1339
|
};
|
|
1262
1340
|
await store.save(session);
|
|
1263
1341
|
await recorder.commit().catch(() => { });
|
|
1342
|
+
// done 发出后这一轮就结束了;提前释放 turn,让客户端紧接着的标题请求不会被 409 挡掉。
|
|
1343
|
+
turn = null;
|
|
1264
1344
|
send('done', {
|
|
1265
1345
|
stopReason: result.stopReason,
|
|
1266
1346
|
error: result.error ?? '',
|
|
@@ -1271,6 +1351,8 @@ export async function startUiServer(options) {
|
|
|
1271
1351
|
todos: session.todos ?? [],
|
|
1272
1352
|
plan: session.plan,
|
|
1273
1353
|
title: session.title,
|
|
1354
|
+
firstTurn: wasFirstTurn,
|
|
1355
|
+
titleSource: session.titleSource ?? 'auto',
|
|
1274
1356
|
info: await info(session),
|
|
1275
1357
|
});
|
|
1276
1358
|
finish();
|
|
@@ -1360,6 +1442,11 @@ async function sendFile(res, name) {
|
|
|
1360
1442
|
'Content-Type': MIME[extname(name)] ?? 'application/octet-stream',
|
|
1361
1443
|
'Content-Length': file.byteLength,
|
|
1362
1444
|
'Cache-Control': 'no-store',
|
|
1445
|
+
'X-Content-Type-Options': 'nosniff',
|
|
1446
|
+
// 只允许本地脚本和 data/blob 预览;阻断模型输出里的内联事件处理器。
|
|
1447
|
+
'Content-Security-Policy': "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; " +
|
|
1448
|
+
"img-src 'self' data: blob:; frame-src data: blob:; font-src 'self' data:; " +
|
|
1449
|
+
"connect-src 'self' ws: wss:;",
|
|
1363
1450
|
});
|
|
1364
1451
|
res.end(file);
|
|
1365
1452
|
}
|
|
@@ -1369,8 +1456,15 @@ async function sendFile(res, name) {
|
|
|
1369
1456
|
}
|
|
1370
1457
|
async function readBody(req) {
|
|
1371
1458
|
const chunks = [];
|
|
1372
|
-
|
|
1373
|
-
|
|
1459
|
+
let size = 0;
|
|
1460
|
+
const max = 30 * 1024 * 1024;
|
|
1461
|
+
for await (const chunk of req) {
|
|
1462
|
+
const buffer = Buffer.from(chunk);
|
|
1463
|
+
size += buffer.byteLength;
|
|
1464
|
+
if (size > max)
|
|
1465
|
+
throw new Error('请求体过大');
|
|
1466
|
+
chunks.push(buffer);
|
|
1467
|
+
}
|
|
1374
1468
|
return Buffer.concat(chunks).toString('utf8');
|
|
1375
1469
|
}
|
|
1376
1470
|
async function readJson(req) {
|
package/dist/tools/todo.js
CHANGED
|
@@ -56,11 +56,14 @@ export const todoWriteTool = {
|
|
|
56
56
|
},
|
|
57
57
|
approval: 'never',
|
|
58
58
|
async run(input, ctx) {
|
|
59
|
+
// 空数组是合法的“清空清单”,不要再当成错误。
|
|
60
|
+
if (!Array.isArray(input.todos))
|
|
61
|
+
return fail('todos 必须是数组。');
|
|
59
62
|
const items = normalize(input.todos);
|
|
60
|
-
if (!items.length)
|
|
61
|
-
return fail('todos 不能为空;要清空请传空数组(会保留空清单)。');
|
|
62
63
|
ctx.todos.length = 0;
|
|
63
64
|
ctx.todos.push(...items);
|
|
65
|
+
if (!items.length)
|
|
66
|
+
return ok('任务清单已清空。', 'todo 已清空');
|
|
64
67
|
return ok(`任务清单已更新(${items.length} 项):\n${formatTodos(items)}`, `todo ${items.length} 项`);
|
|
65
68
|
},
|
|
66
69
|
};
|