sidekick-chat 0.2.7 → 0.2.9

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Sidekick 终端客户端
2
2
 
3
3
  在终端里和秘书说话。它只是 `chat` World 的又一个客户端,和手机 App、网页版共用同一批会话、未读和回复,
4
- 不新增任何后端接口——所以手机上收到的、终端里发的,是同一份对话。
4
+ 所以手机上收到的、终端里发的,是同一份对话。
5
5
 
6
6
  ```
7
7
  $ npx sidekick-chat
@@ -17,10 +17,12 @@ Sidekick · 秘书 https://hkv4.cznorth.cn/chat
17
17
  记成明天 9:30,提前 10 分钟提醒。
18
18
  ```
19
19
 
20
- 0.2.6 起,当前会话各 bot 的最后 LLM 调用时间和对应活动常驻显示在输入框上方,并实时更新;`/who` 查看全部 bot,`/conv` 也列出成员活动。纯文本交互在启动、切换会话及活动变化时输出。时间为调用开始时间,忙闲、消息和心跳不刷新它;没有记录时显示「尚无 LLM 活动记录」。
20
+ 0.2.9 起,当前会话各 bot 的最后 LLM 调用时间、对应活动、上下文用量和忙碌状态显示在输入框下方,并实时更新;输入框上方只显示对话内容,底部不再常驻操作提示。`/who` 查看全部 bot,`/conv` 也列出成员活动。纯文本交互在启动、切换会话及活动变化时输出。时间为调用开始时间,忙闲、消息和心跳不刷新它;没有记录时显示「尚无 LLM 活动记录」。
21
21
 
22
22
  ## 安装
23
23
 
24
+ 0.2.8 起,输入框上方显示当前会话各助理的「上下文 ≈ 已用 / 预算 token · 百分比」,每 5 秒检查更新,交接和预算设置变化后自动刷新。`/context` 查看当前会话用量,`/who` 查看全部助理。数值是当前主 session 的估算,预算使用服务器配置(默认 200000 token),与 `/usage` 的订阅额度不同;旧服务器没有数据时显示「上下文用量未知」。
25
+
24
26
  ```bash
25
27
  npx sidekick-chat # 不装直接用
26
28
  npm i -g sidekick-chat # 或者装到全局,命令就是 sidekick
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sidekick-chat",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "Sidekick 终端客户端:在终端里和秘书对话",
5
5
  "type": "module",
6
6
  "license": "PolyForm-Noncommercial-1.0.0",
@@ -27,8 +27,7 @@
27
27
  ],
28
28
  "repository": {
29
29
  "type": "git",
30
- "url": "git+https://github.com/Cznorth/sidekick.git",
31
- "directory": "cli"
30
+ "url": "git+https://github.com/Cznorth/sidekick-chat.git"
32
31
  },
33
32
  "dependencies": {
34
33
  "@earendil-works/pi-tui": "1.0.0"
@@ -0,0 +1,18 @@
1
+ const count = (n) => Math.round(n).toLocaleString('en-US');
2
+
3
+ export function formatContext(bot) {
4
+ const c = bot?.context;
5
+ if (!c || !Number.isFinite(c.estTokens) || c.estTokens < 0) return '上下文用量未知';
6
+ const used = count(c.estTokens);
7
+ if (!Number.isFinite(c.maxTokens) || c.maxTokens <= 0) return `上下文 ≈ ${used} token · 预算未知`;
8
+ return `上下文 ≈ ${used} / ${count(c.maxTokens)} token · ${Math.round(c.estTokens / c.maxTokens * 100)}%`;
9
+ }
10
+
11
+ export function applyContext(bots, frame) {
12
+ const bot = bots.find(b => b.id === frame?.bot);
13
+ if (!bot || !Number.isFinite(frame.at) || frame.at <= 0 || frame.at < (bot.context?.at || 0)
14
+ || !Number.isFinite(frame.maxTokens) || frame.maxTokens <= 0
15
+ || (frame.estTokens !== null && (!Number.isFinite(frame.estTokens) || frame.estTokens < 0))) return false;
16
+ bot.context = { at: frame.at, estTokens: frame.estTokens, maxTokens: frame.maxTokens };
17
+ return true;
18
+ }
package/src/main.mjs CHANGED
@@ -2,9 +2,10 @@
2
2
  * 终端客户端的主流程:`sidekick` 进交互对话,`sidekick login` 存凭据,`sidekick ask "…"` 一次性问一句。
3
3
  *
4
4
  * 它只是 chat World 的又一个客户端,和手机 App、网页版共用同一份会话和未读,
5
- * 不新增任何后端接口(见 packages/world-chat/src/world.ts 的 `startHub`)。
5
+ * 上下文用量随团队状态和 SSE 推流提供(见 packages/world-chat/src/world.ts 的 `startHub`)。
6
6
  */
7
7
  import { createInterface } from 'node:readline';
8
+ import { applyContext, formatContext } from './context.mjs';
8
9
  import { readFileSync } from 'node:fs';
9
10
  import { fileURLToPath } from 'node:url';
10
11
  import { AuthError, SidekickApi, loginWithPassword } from './api.mjs';
@@ -334,7 +335,7 @@ async function startChat(flags) {
334
335
 
335
336
  function drawActivity() {
336
337
  const members = convOf(convId)?.members || [];
337
- const text = state.bots.filter(b => members.includes(b.id)).map(b => `${b.name}: ${formatActivity(b)}`).join('\n');
338
+ const text = state.bots.filter(b => members.includes(b.id)).map(b => `${b.name}: ${formatActivity(b)}\n${b.name}: ${formatContext(b)}`).join('\n');
338
339
  if (ui) ui.activity(text);
339
340
  else if (text) out(text);
340
341
  }
@@ -422,6 +423,9 @@ async function startChat(flags) {
422
423
  else if (event === 'activity' && applyActivity(state.bots, payload)) {
423
424
  if (convOf(convId)?.members.includes(payload.bot)) drawActivity();
424
425
  }
426
+ else if (event === 'context' && applyContext(state.bots, payload)) {
427
+ if (convOf(convId)?.members.includes(payload.bot)) drawActivity();
428
+ }
425
429
  else if (event === 'busy') typing(Boolean(payload.busy));
426
430
  },
427
431
  })
@@ -469,7 +473,8 @@ async function startChat(flags) {
469
473
  '/conv 列出会话',
470
474
  '/to <序号|名字> 切换会话',
471
475
  '/history [n] 重印最近 n 条(默认 20)',
472
- '/who 名册和连接状态',
476
+ '/who 名册、连接状态和上下文用量',
477
+ '/context 当前会话各助理的上下文用量和预算',
473
478
  '/usage 查询额度,查看剩余比例和重置时间',
474
479
  '/paste 添加剪贴板图片(也可 Ctrl+V)',
475
480
  '/image <路径> 添加图片文件,最多 4 张、每张 6 MB',
@@ -509,10 +514,13 @@ async function startChat(flags) {
509
514
  case 'who':
510
515
  out(
511
516
  state.bots
512
- .map((b) => `${b.primary ? '*' : ' '}${b.name}(${b.id}) ${b.status}${b.busy ? ' 正在忙' : ''} — ${b.role || '无专长'}\n ${formatActivity(b)}`)
517
+ .map((b) => `${b.primary ? '*' : ' '}${b.name}(${b.id}) ${b.status}${b.busy ? ' 正在忙' : ''} — ${b.role || '无专长'}\n ${formatActivity(b)}\n ${formatContext(b)}`)
513
518
  .join('\n'),
514
519
  );
515
520
  return;
521
+ case 'context':
522
+ out(state.bots.filter(b => convOf(convId)?.members.includes(b.id)).map(b => `${b.name}: ${formatContext(b)}`).join('\n') || '当前会话没有助理');
523
+ return;
516
524
  case 'usage':
517
525
  case 'quota':
518
526
  out(formatQuota(await quotas.refresh(), { detail: true }));
package/src/tui.mjs CHANGED
@@ -32,12 +32,11 @@ export function createChatUI({ color, onSubmit, onQuit, onInput, onPasteImage, h
32
32
  });
33
33
  tui.addChild(transcript);
34
34
  tui.addChild(live);
35
+ tui.addChild(editor);
35
36
  tui.addChild(activity);
36
37
  tui.addChild(status);
37
- tui.addChild(editor);
38
38
  const attachments = new Text('', 1, 0);
39
39
  tui.addChild(attachments);
40
- tui.addChild(new Text(paint(color, 'gray', 'Enter 发送 · Ctrl+V 图片 · /paste · /image 路径 · /help · Ctrl+C 退出'), 1, 0));
41
40
  tui.setFocus(editor);
42
41
  tui.addInputListener((data) => {
43
42
  // 任何一个按键都算「人在终端前面」:调用方据此决定要不要把新消息标成已读。