agent-loft 0.2.55 → 0.2.56

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-loft",
3
- "version": "0.2.55",
3
+ "version": "0.2.56",
4
4
  "type": "module",
5
5
  "description": "多个 agent 的工作台:把本机 Claude Code / Codex 放到一处统一管理,用飞书和 Web 页面随时驱动——会话、审批、模型切换都共用",
6
6
  "keywords": [
@@ -347,9 +347,9 @@ export function removeSubscriptionsOf(mine: OwnerFilter): void {
347
347
  if (s.subscriptions.length !== before) persist();
348
348
  }
349
349
 
350
- /** 推给订阅的设备(给了 to 就只推给它挑出来的主人),返回成功的数量。单个设备失败不影响别的 */
351
- export async function pushToAll(payload: PushPayload, to?: OwnerFilter): Promise<number> {
352
- const subs = load().subscriptions.filter((x) => !to || to(x.userId));
350
+ /** 按主人和设备筛选订阅,返回成功的数量。单个设备失败不影响别的。 */
351
+ export async function pushToAll(payload: PushPayload, to?: OwnerFilter, device?: (endpoint: string, userId: string | undefined) => boolean): Promise<number> {
352
+ const subs = load().subscriptions.filter((x) => (!to || to(x.userId)) && (!device || device(x.endpoint, x.userId)));
353
353
  if (subs.length === 0) return 0;
354
354
  const results = await Promise.all(
355
355
  subs.map((sub) =>
@@ -182,6 +182,9 @@ type Client = {
182
182
  /** 页面在前台,且正看着哪条会话(前端在切会话、切前后台时上报) */
183
183
  visible: boolean;
184
184
  viewing: string | null;
185
+ /** 本设备应用是否在前台、它的推送订阅(与当前会话及空闲计时无关)。 */
186
+ foreground: boolean;
187
+ pushEndpoint: string | null;
185
188
  /** 最后一次收到这个页面的消息(含 ping) */
186
189
  seen: number;
187
190
  };
@@ -246,14 +249,24 @@ function broadcastSkills(): void {
246
249
  // ---------- 系统通知(Web Push) ----------
247
250
 
248
251
  /**
249
- * 有页面正在前台看着这条会话:人就在眼前,不用推。
252
+ * 桌面端或旧页面正在前台看着这条会话:人就在眼前,不用推。
253
+ * 能上报订阅的新页面按设备过滤,避免手机在前台时连带屏蔽其它设备。
250
254
  * 页面每 25 秒发一次 ping;40 秒没收到它的任何消息就不算(手机切走后网络断了,连接还没被心跳清掉,
251
255
  * 最后报的还是「在看」)
252
256
  */
253
257
  const SEEN_MS = 40_000;
254
258
  function watching(key: string): boolean {
255
259
  const now = Date.now();
256
- for (const client of clients) if (client.visible && client.viewing === key && now - client.seen < SEEN_MS) return true;
260
+ for (const client of clients) if (!client.pushEndpoint && client.visible && client.viewing === key && now - client.seen < SEEN_MS) return true;
261
+ return false;
262
+ }
263
+
264
+ /** 只暂停前台设备自己的推送,不影响同账号的其它设备或其它用户。 */
265
+ function foregroundDevice(endpoint: string, owner: string | undefined): boolean {
266
+ const now = Date.now();
267
+ for (const client of clients) {
268
+ if (client.foreground && client.pushEndpoint === endpoint && client.user.id === owner && now - client.seen < SEEN_MS && checkClient(client)) return true;
269
+ }
257
270
  return false;
258
271
  }
259
272
 
@@ -286,7 +299,7 @@ type Notice = { title: string; body: string; tag: string };
286
299
  * 一条要提醒的动静,走两条路:
287
300
  * - 广播给所有连着的页面(t: "notice"):桌面端据此弹系统通知,要不要弹由它自己判断
288
301
  * (它知道自己是不是正看着这条会话)。普通网页忽略。
289
- * - Web Push:只在没有页面正看着这条会话时推。
302
+ * - Web Push:只在没有页面正看着这条会话时推,并跳过应用正在前台的设备。
290
303
  */
291
304
  function notify(key: string, notice: Notice): void {
292
305
  if (!isWebConv(key)) return;
@@ -297,7 +310,7 @@ function notify(key: string, notice: Notice): void {
297
310
  void pushToAll({ ...notice, url: `/#${encodeURIComponent(key)}` }, (owner) => {
298
311
  const u = getUser(owner ?? legacy);
299
312
  return !!u && !u.disabled && canSeeKey(u, key);
300
- });
313
+ }, (endpoint, owner) => !foregroundDevice(endpoint, owner ?? legacy));
301
314
  }
302
315
 
303
316
  function pushEvent(env: EventEnvelope): void {
@@ -376,6 +389,8 @@ function handleClientMessage(client: Client, raw: string): void {
376
389
  case "presence":
377
390
  client.visible = msg.visible === true;
378
391
  client.viewing = typeof msg.key === "string" && msg.key ? msg.key : null;
392
+ client.foreground = msg.foreground === true;
393
+ client.pushEndpoint = typeof msg.pushEndpoint === "string" && msg.pushEndpoint.length <= 4096 ? msg.pushEndpoint : null;
379
394
  return;
380
395
 
381
396
  case "unsub":
@@ -509,7 +524,7 @@ export async function startWeb(): Promise<void> {
509
524
  if (url.pathname === "/ws/term") return upgradeTerminal(wss!, req, socket, head, user, token, url);
510
525
  if (url.pathname === "/ws/desktop") return upgradeDesktop(wss!, req, socket, head, user, token, url);
511
526
  wss!.handleUpgrade(req, socket, head, (ws) => {
512
- const client: Client = { ws, subs: new Set(), alive: true, token, user, visible: false, viewing: null, seen: Date.now() };
527
+ const client: Client = { ws, subs: new Set(), alive: true, token, user, visible: false, viewing: null, foreground: false, pushEndpoint: null, seen: Date.now() };
513
528
  clients.add(client);
514
529
  ws.on("pong", () => (client.alive = true));
515
530
  ws.on("message", (data) => {
@@ -168,10 +168,6 @@
168
168
  color: var(--fg-soft);
169
169
  }
170
170
 
171
- .top-title .conv-expert-icon:hover {
172
- background: var(--bg-hover);
173
- }
174
-
175
171
  .top-title .conv-expert-icon .agent-icon {
176
172
  display: inline-grid;
177
173
  place-items: center;
@@ -244,7 +244,8 @@ function chartOption(spec) {
244
244
  axisTick: { show: false },
245
245
  axisLabel: { color: c.muted },
246
246
  splitLine: { show: type === "value", lineStyle: { color: c.grid } },
247
- scale: type === "value" && spec.type === "scatter",
247
+ // 普通折线和散点按数据范围缩放;柱形、面积及堆叠图保留 0 基线。
248
+ scale: type === "value" && (spec.type === "scatter" || (spec.type === "line" && !spec.stack)),
248
249
  });
249
250
  const category = axis("category", spec.xName);
250
251
  category.data = spec.x;
package/web/lib/shell.js CHANGED
@@ -575,10 +575,12 @@ function onHostMessage(origin, m) {
575
575
  return;
576
576
  case "push":
577
577
  SH.push = m.push ?? null;
578
+ emit("aim:shell-active");
578
579
  applyRelayPush();
579
580
  return;
580
581
  case "push-off":
581
582
  SH.push = null;
583
+ emit("aim:shell-active");
582
584
  void dropRelayPush(m.endpoint, false).then(() => {
583
585
  toHost({ t: "push-off-done", request: m.request });
584
586
  }).catch((err) => console.warn("[多服务器] 通知订阅没有撤销:", err.message));
package/web/lib/ws.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { h } from "./dom.js";
2
2
  import { api } from "./api.js";
3
- import { desktop, S, ui } from "../state.js";
3
+ import { desktop, framed, S, ui } from "../state.js";
4
4
  import { showAuth } from "../views/auth.js";
5
5
  import { applyBootstrap, bootstrapQuery, renderSidebar, scheduleSidebar } from "../views/sidebar.js";
6
6
  import { scheduleQuota } from "../views/quota.js";
@@ -8,7 +8,8 @@ import { renderTopbar } from "../views/topbar.js";
8
8
  import { applyEvent, openConversation } from "../views/messages.js";
9
9
  import { upsertInteraction } from "../views/interaction.js";
10
10
  import { setBusy } from "../views/composer.js";
11
- import { shownHere } from "./shell.js";
11
+ import { SH, shownHere } from "./shell.js";
12
+ import { currentPushSub } from "./push.js";
12
13
  import { renderScheduled } from "../views/schedule.js";
13
14
 
14
15
  // ---------- WebSocket ----------
@@ -20,6 +21,7 @@ export const WS = {
20
21
  backoff: 500,
21
22
  connected: false,
22
23
  pingTimer: null,
24
+ pushEndpoint: null,
23
25
 
24
26
  stopped: false,
25
27
  epoch: null,
@@ -101,12 +103,27 @@ export const WS = {
101
103
  // 多服务器:外壳切到了别的服务器、或者这台被嵌着但没在显示,也算没在看
102
104
  const visible = document.visibilityState === "visible" && document.hasFocus() && Date.now() - lastInput < IDLE_MS && shownHere();
103
105
  // 在定时任务、远程控制页上:没在看哪条对话(远程控制页不清 S.current,回来时对话还在)
104
- this.raw({ t: "presence", visible, key: S.view ? "" : (S.current ?? "") });
106
+ this.raw({
107
+ t: "presence", visible, key: S.view ? "" : (S.current ?? ""),
108
+ // 前台免打扰按设备判断,不受正在看哪条会话、是否空闲的影响。
109
+ foreground: document.visibilityState === "visible",
110
+ pushEndpoint: framed ? (SH.push?.subscription.endpoint ?? null) : this.pushEndpoint,
111
+ });
112
+ },
113
+
114
+ async refreshPushPresence() {
115
+ if (!framed) this.pushEndpoint = (await currentPushSub())?.endpoint ?? null;
116
+ this.presence();
105
117
  },
106
118
  };
107
119
 
108
120
  // 切回窗口本身就算动了一下
109
- window.addEventListener("focus", () => (markActive(), WS.presence()));
121
+ window.addEventListener("focus", () => {
122
+ markActive();
123
+ WS.presence();
124
+ clearAppNotifications();
125
+ void WS.refreshPushPresence();
126
+ });
110
127
  document.addEventListener("aim:shell-active", () => {
111
128
  if (shownHere()) markActive();
112
129
  WS.presence();
@@ -146,17 +163,28 @@ function onInput() {
146
163
  for (const ev of ["pointerdown", "keydown", "wheel", "touchstart", "scroll"]) window.addEventListener(ev, onInput, { capture: true, passive: true });
147
164
  armIdle();
148
165
 
149
- /** 打开 / 切回页面:清掉桌面图标角标和计数(计数由 sw.js 在收到推送时累加) */
150
- export function clearAppBadge() {
151
- if (document.visibilityState !== "visible") return;
166
+ /** 打开 / 切回页面:清掉本应用已显示的通知、桌面图标角标和计数。 */
167
+ export function clearAppNotifications() {
168
+ if (framed || document.visibilityState !== "visible") return;
152
169
  navigator.clearAppBadge?.().catch(() => {});
153
170
  if ("caches" in window) caches.delete("aim-badge").catch(() => {});
171
+ navigator.serviceWorker?.getRegistration().then(async (reg) => {
172
+ const notifications = await reg?.getNotifications();
173
+ // 异步读取期间可能已经切走,后台到达的新通知留着提醒。
174
+ if (document.visibilityState === "visible") for (const n of notifications ?? []) n.close();
175
+ }).catch(() => {});
154
176
  }
155
177
 
178
+ window.addEventListener("pageshow", () => {
179
+ clearAppNotifications();
180
+ void WS.refreshPushPresence();
181
+ });
182
+
156
183
  document.addEventListener("visibilitychange", () => {
157
184
  if (document.visibilityState === "visible") markActive();
158
185
  WS.presence();
159
- clearAppBadge();
186
+ clearAppNotifications();
187
+ if (document.visibilityState === "visible") void WS.refreshPushPresence();
160
188
  // 手机切回前台:连接多半已经被系统掐了,立刻重连而不是等退避
161
189
  if (document.visibilityState === "visible" && WS.sock?.readyState !== 1) {
162
190
  WS.backoff = 300;
@@ -194,6 +222,7 @@ function onWsMessage(msg) {
194
222
  WS.epoch = msg.epoch;
195
223
  WS.hellos += 1;
196
224
  WS.presence();
225
+ void WS.refreshPushPresence();
197
226
  if (restarted) {
198
227
  // 服务重启过:seq 从头计数了,旧游标全部作废,当前会话整页重拉
199
228
  S.seq.clear();
@@ -3,6 +3,7 @@ import { api } from "../lib/api.js";
3
3
  import { relTime } from "../lib/time.js";
4
4
  import { currentPushSub, enablePush, pushSupport } from "../lib/push.js";
5
5
  import { refreshShellPush } from "../lib/shell.js";
6
+ import { WS } from "../lib/ws.js";
6
7
 
7
8
  export async function notifyTab(panel) {
8
9
  const support = pushSupport();
@@ -27,6 +28,7 @@ export async function notifyTab(panel) {
27
28
  }
28
29
  btn.disabled = false;
29
30
  // 多服务器的外壳:开关通知后,转给各台的订阅跟着变
31
+ void WS.refreshPushPresence();
30
32
  void refreshShellPush();
31
33
  await render();
32
34
  };
@@ -49,7 +51,7 @@ export async function notifyTab(panel) {
49
51
  fill(
50
52
  box,
51
53
  h("h4", {}, "系统通知"),
52
- h("div", { class: "muted" }, "Web 对话里 agent 回复完成、出错、需要授权或向你提问时,推送到开启了通知的设备。正在前台看着那条对话的时候不推。飞书对话由飞书自己提醒。"),
54
+ h("div", { class: "muted" }, "Web 对话里 agent 回复完成、出错、需要授权或向你提问时,推送到开启了通知的设备。本设备的应用在前台时不推送,打开或切回应用会清除本应用的历史通知和角标。飞书对话由飞书自己提醒。"),
53
55
  support.ok
54
56
  ? h("div", { class: "row" }, here ? offBtn : onBtn, data.subscriptions.length ? testBtn : null)
55
57
  : support.framed
@@ -1,7 +1,7 @@
1
1
  import { $app, fill, h, icon, toast } from "../lib/dom.js";
2
2
  import { api } from "../lib/api.js";
3
3
  import { desktop, desktopMenu, desktopQuota, desktopSettings, framed, remoteTabs, S, ui } from "../state.js";
4
- import { clearAppBadge, WS } from "../lib/ws.js";
4
+ import { clearAppNotifications, WS } from "../lib/ws.js";
5
5
  import { showAuth } from "./auth.js";
6
6
  import {
7
7
  applyBootstrap,
@@ -184,10 +184,10 @@ export async function boot() {
184
184
  }
185
185
  // 被嵌在别的服务器的页面里时,通知、角标都归外壳管
186
186
  if ("serviceWorker" in navigator && !framed) {
187
- navigator.serviceWorker.register("/sw.js").catch(() => {});
188
- clearAppBadge();
187
+ navigator.serviceWorker.register("/sw.js").then(() => clearAppNotifications()).catch(() => {});
189
188
  void releaseForeignPushSub()
190
189
  .catch(() => {})
190
+ .then(() => WS.refreshPushPresence())
191
191
  .then(() => refreshShellPush());
192
192
  // 点了系统通知、页面本来就开着:跳到通知对应的会话
193
193
  navigator.serviceWorker.addEventListener("message", (e) => {
@@ -1,4 +1,4 @@
1
- import { fill, h, icon, starsLoader, toast } from "../lib/dom.js";
1
+ import { fill, h, icon, isTouch, starsLoader, toast } from "../lib/dom.js";
2
2
  import { api } from "../lib/api.js";
3
3
  import { clockTime, liveDuration, serverNow, setServerTime, tidyFooter } from "../lib/time.js";
4
4
  import { copyText, patchMarkdown, renderMarkdown } from "../lib/markdown.js";
@@ -48,7 +48,7 @@ export function newDraft() {
48
48
  renderChips();
49
49
  renderSidebar();
50
50
  scheduleNav();
51
- ui.input?.focus();
51
+ if (!isTouch) ui.input?.focus();
52
52
  preloadCommands();
53
53
  }
54
54