@vincentbanana/bananacli 0.8.0 → 0.8.2

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.
@@ -253,7 +253,11 @@ async function attachmentContextFor(items) {
253
253
  /* 读不到就退化成路径说明 */
254
254
  }
255
255
  }
256
- if (kind === 'pdf' || kind === 'binary') {
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.0',
320
+ version: '0.8.2',
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',
@@ -554,14 +558,35 @@ export async function startUiServer(options) {
554
558
  }
555
559
  if (path === '/api/session' && req.method === 'POST') {
556
560
  const body = await readJson(req);
561
+ // 一轮还没结束时不要换会话:否则旧 turn 的结果会写到新会话,界面标题和正文也会串台。
562
+ const busyAction = body.action === 'new' ||
563
+ body.action === 'switch' ||
564
+ body.action === 'delete' ||
565
+ body.action === 'clear';
566
+ if (turn && busyAction) {
567
+ return json(res, 409, { error: '正在生成,请先停止当前任务再切换会话' });
568
+ }
569
+ if (body.action === 'load') {
570
+ const found = body.ref ? await store.find(body.ref) : null;
571
+ if (!found)
572
+ return json(res, 404, { error: '没找到这个对话' });
573
+ return json(res, 200, { session: found });
574
+ }
557
575
  if (body.action === 'new') {
558
576
  session = store.create(body.title?.trim() || '新对话');
577
+ // 用户明确指定了标题,后续自动标题不要覆盖它。
578
+ if (body.title?.trim())
579
+ session.titleSource = 'user';
559
580
  await store.save(session);
560
581
  }
561
582
  else if (body.action === 'rename') {
562
- session.title = body.title?.trim() || session.title;
563
- session.titleSource = 'user';
564
- await store.save(session);
583
+ // 支持直接重命名侧栏里的某个会话,不必先把用户切过去。
584
+ const target = body.ref && body.ref !== session.id ? await store.find(body.ref) : session;
585
+ if (!target)
586
+ return json(res, 404, { error: '没找到这个对话' });
587
+ target.title = body.title?.trim() || target.title;
588
+ target.titleSource = 'user';
589
+ await store.save(target);
565
590
  }
566
591
  else if (body.action === 'switch') {
567
592
  const found = body.ref ? await store.find(body.ref) : null;
@@ -601,8 +626,6 @@ export async function startUiServer(options) {
601
626
  await store.save(session);
602
627
  }
603
628
  }
604
- if (turn)
605
- turn.session = session;
606
629
  activeId = session.id;
607
630
  return json(res, 200, { session: session.id, sessions: await listSessions(), info: await info(session) });
608
631
  }
@@ -687,23 +710,30 @@ export async function startUiServer(options) {
687
710
  before = '';
688
711
  }
689
712
  }
713
+ let afterExists = false;
714
+ let tooLarge = false;
690
715
  try {
691
716
  const info = await stat(change.path);
692
- if (info.isFile() && info.size <= MAX_FILE_BYTES)
717
+ afterExists = info.isFile();
718
+ if (afterExists && info.size <= MAX_FILE_BYTES)
693
719
  after = await readFile(change.path, 'utf8');
720
+ else if (afterExists)
721
+ tooLarge = true;
694
722
  }
695
723
  catch {
696
- after = '';
724
+ afterExists = false;
697
725
  }
698
726
  const diff = lineDiff(before, after);
699
727
  files.push({
700
728
  path: change.relative,
701
729
  absolute: change.path,
702
730
  existed: change.existed,
703
- missing: after === '' && change.existed,
731
+ // 大文件读不出内容不等于文件被删了。
732
+ missing: !afterExists && change.existed,
733
+ tooLarge,
704
734
  additions: diff.additions,
705
735
  deletions: diff.deletions,
706
- truncated: diff.truncated,
736
+ truncated: diff.truncated || tooLarge,
707
737
  lines: diff.lines,
708
738
  });
709
739
  }
@@ -753,7 +783,8 @@ export async function startUiServer(options) {
753
783
  delete current.apiKey;
754
784
  }
755
785
  }
756
- else if (body.action === 'system' && body.value) {
786
+ else if (body.action === 'system' && typeof body.value === 'string') {
787
+ // 空字符串表示恢复内置提示词,不能按 falsy 忽略。
757
788
  config.systemPrompt = body.value;
758
789
  }
759
790
  else if (body.action === 'showReasoning') {
@@ -767,6 +798,13 @@ export async function startUiServer(options) {
767
798
  config.workspace = next;
768
799
  // 换工作区后终端也要跟着换,否则 cd 还停在旧目录
769
800
  terminalCwd = null;
801
+ // 项目级插件依赖 workspace,切目录后立即重载。
802
+ try {
803
+ await syncPlugins();
804
+ }
805
+ catch {
806
+ /* 插件加载失败不影响工作区切换 */
807
+ }
770
808
  }
771
809
  const { saveConfig } = await import("../config/config.js");
772
810
  await saveConfig(config);
@@ -782,11 +820,17 @@ export async function startUiServer(options) {
782
820
  return json(res, 200, { info: await info(session) });
783
821
  }
784
822
  if (path === '/api/undo' && req.method === 'POST') {
823
+ if (turn)
824
+ return json(res, 409, { error: '正在生成,请先停止当前任务再撤销' });
785
825
  const result = await backups(session.id).undo();
786
826
  return json(res, 200, { restored: result.restored, deleted: result.deleted, had: Boolean(result.turn) });
787
827
  }
788
828
  if (path === '/api/attach' && req.method === 'POST') {
789
829
  const name = decodeURIComponent(new URL(req.url ?? '/', 'http://x').searchParams.get('name') ?? 'file');
830
+ // base64 会有约 4/3 膨胀,先按上限拒绝明显超大的请求,避免整包读进内存。
831
+ if (Number(req.headers['content-length'] ?? 0) > MAX_ATTACHMENT_BYTES * 1.4) {
832
+ return json(res, 413, { error: '附件超过 20MB' });
833
+ }
790
834
  const body = await readBody(req);
791
835
  const buffer = Buffer.from(body, 'base64');
792
836
  if (buffer.byteLength > MAX_ATTACHMENT_BYTES) {
@@ -856,6 +900,29 @@ export async function startUiServer(options) {
856
900
  sessions: await listSessions(),
857
901
  });
858
902
  }
903
+ if (path === '/api/title' && req.method === 'POST') {
904
+ // 标题在 done 之后异步生成,避免把 3 秒的标题请求算进本轮用时、也不让界面多转一次圈。
905
+ if (turn)
906
+ return json(res, 409, { error: '正在生成,稍后再生成标题' });
907
+ const body = await readJson(req);
908
+ let target = session;
909
+ if (body.session && body.session !== session.id) {
910
+ const found = await store.load(body.session);
911
+ if (!found)
912
+ return json(res, 404, { error: '没找到这个对话' });
913
+ target = found;
914
+ }
915
+ if (target.titleSource === 'user') {
916
+ return json(res, 200, { title: target.title, session: target.id, sessions: await listSessions() });
917
+ }
918
+ const hasAssistant = target.messages.some((message) => message.role === 'assistant');
919
+ if (!hasAssistant) {
920
+ return json(res, 200, { title: target.title, session: target.id, sessions: await listSessions() });
921
+ }
922
+ target.title = await generateSessionTitle(target);
923
+ await store.save(target);
924
+ return json(res, 200, { title: target.title, session: target.id, sessions: await listSessions() });
925
+ }
859
926
  if (path === '/api/chat' && req.method === 'POST') {
860
927
  const body = await readJson(req);
861
928
  if (turn)
@@ -1248,9 +1315,13 @@ export async function startUiServer(options) {
1248
1315
  };
1249
1316
  }
1250
1317
  }
1251
- if (wasFirstTurn && session.titleSource !== 'user' && result.stopReason === 'done') {
1252
- send('status', { message: '正在生成会话标题…' });
1253
- session.title = await generateSessionTitle(session);
1318
+ // 先给一个首条消息截断的临时标题,避免 done 之后标题还是一团空白;
1319
+ // 模型标题稍后由 /api/title 异步替换。
1320
+ if (wasFirstTurn &&
1321
+ session.titleSource !== 'user' &&
1322
+ (!session.title || session.title === '新对话')) {
1323
+ const firstUser = session.messages.find((message) => message.role === 'user');
1324
+ session.title = deriveTitle(firstUser?.content ?? '');
1254
1325
  }
1255
1326
  const pricing = resolvePricing(session.model, config.providers[config.current]?.pricing);
1256
1327
  const cost = estimateCost(result.usage, pricing);
@@ -1261,6 +1332,8 @@ export async function startUiServer(options) {
1261
1332
  };
1262
1333
  await store.save(session);
1263
1334
  await recorder.commit().catch(() => { });
1335
+ // done 发出后这一轮就结束了;提前释放 turn,让客户端紧接着的标题请求不会被 409 挡掉。
1336
+ turn = null;
1264
1337
  send('done', {
1265
1338
  stopReason: result.stopReason,
1266
1339
  error: result.error ?? '',
@@ -1271,6 +1344,8 @@ export async function startUiServer(options) {
1271
1344
  todos: session.todos ?? [],
1272
1345
  plan: session.plan,
1273
1346
  title: session.title,
1347
+ firstTurn: wasFirstTurn,
1348
+ titleSource: session.titleSource ?? 'auto',
1274
1349
  info: await info(session),
1275
1350
  });
1276
1351
  finish();
@@ -1360,6 +1435,11 @@ async function sendFile(res, name) {
1360
1435
  'Content-Type': MIME[extname(name)] ?? 'application/octet-stream',
1361
1436
  'Content-Length': file.byteLength,
1362
1437
  'Cache-Control': 'no-store',
1438
+ 'X-Content-Type-Options': 'nosniff',
1439
+ // 只允许本地脚本和 data/blob 预览;阻断模型输出里的内联事件处理器。
1440
+ 'Content-Security-Policy': "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; " +
1441
+ "img-src 'self' data: blob:; frame-src data: blob:; font-src 'self' data:; " +
1442
+ "connect-src 'self' ws: wss:;",
1363
1443
  });
1364
1444
  res.end(file);
1365
1445
  }
@@ -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
  };