@celljs/ta-channels 4.23.0 → 4.24.0

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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/web/index.js +197 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-channels",
3
- "version": "4.23.0",
3
+ "version": "4.24.0",
4
4
  "description": "A pi package for Ta: agents with roles of their own and channels where the user, Ta and the agents talk; Ta builds the team for what the user wants",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -27,9 +27,9 @@
27
27
  "watch": "tsc -b -w --preserveWatchOutput"
28
28
  },
29
29
  "devDependencies": {
30
- "@celljs/ta-protocol": "4.23.0",
31
- "@celljs/ta-relay": "4.23.0",
32
- "@celljs/ta-web": "4.23.0",
30
+ "@celljs/ta-protocol": "4.24.0",
31
+ "@celljs/ta-relay": "4.24.0",
32
+ "@celljs/ta-web": "4.24.0",
33
33
  "@types/react": "^19.3.0"
34
34
  },
35
35
  "publishConfig": {
package/web/index.js CHANGED
@@ -2407,7 +2407,6 @@ var CHANNEL_KEY = "ta-channels.channel";
2407
2407
  class ChannelsUiStore {
2408
2408
  current = {
2409
2409
  query: "",
2410
- fresh: [],
2411
2410
  adding: false,
2412
2411
  editing: false,
2413
2412
  creating: false,
@@ -2483,6 +2482,19 @@ function useChannels(service) {
2483
2482
  }, [service]);
2484
2483
  return state;
2485
2484
  }
2485
+ function useChannelUnseen(context, channelId) {
2486
+ const { seen, service } = context;
2487
+ const subscribe = React.useCallback((listener) => {
2488
+ const stopSeen = seen.subscribe(listener);
2489
+ const stopService = service.subscribe(listener);
2490
+ return () => {
2491
+ stopSeen();
2492
+ stopService();
2493
+ };
2494
+ }, [seen, service]);
2495
+ const read = () => seen.unseen(channelId, service.state.messages[channelId], (service.state.working[channelId] ?? []).length > 0);
2496
+ return React.useSyncExternalStore(subscribe, read, read);
2497
+ }
2486
2498
  function useUi(ui) {
2487
2499
  return React.useSyncExternalStore((listener) => ui.subscribe(listener), () => ui.state);
2488
2500
  }
@@ -3924,6 +3936,149 @@ function AgentInfoView({ context }) {
3924
3936
  }));
3925
3937
  }
3926
3938
 
3939
+ // web-src/channel-seen.ts
3940
+ var CHANNELS_SEEN_KEY = "ui.channelsSeen";
3941
+ var ChannelSeenUtil;
3942
+ ((ChannelSeenUtil) => {
3943
+ function parse(value) {
3944
+ if (!value || typeof value !== "object") {
3945
+ return;
3946
+ }
3947
+ const { since, at, unread } = value;
3948
+ if (typeof since !== "number" || !at || typeof at !== "object") {
3949
+ return;
3950
+ }
3951
+ const marks = unread && typeof unread === "object" ? times(unread) : {};
3952
+ return Object.keys(marks).length ? { since, at: times(at), unread: marks } : { since, at: times(at) };
3953
+ }
3954
+ ChannelSeenUtil.parse = parse;
3955
+ function merge(a, b) {
3956
+ const since = Math.max(a.since, b.since);
3957
+ const at = later(a.at, b.at);
3958
+ const unread = stillUnread(later(a.unread ?? {}, b.unread ?? {}), at, since);
3959
+ for (const [id, time] of Object.entries(at)) {
3960
+ if (time <= since) {
3961
+ delete at[id];
3962
+ }
3963
+ }
3964
+ if (since === a.since && same(at, a.at) && same(unread ?? {}, a.unread ?? {})) {
3965
+ return a;
3966
+ }
3967
+ return unread ? { since, at, unread } : { since, at };
3968
+ }
3969
+ ChannelSeenUtil.merge = merge;
3970
+ function lastAt(messages) {
3971
+ const last = messages?.filter((message) => message.author !== SYSTEM).at(-1);
3972
+ return last && last.author !== USER ? last.at : undefined;
3973
+ }
3974
+ ChannelSeenUtil.lastAt = lastAt;
3975
+ function unseen(seen, channelId, messages, working) {
3976
+ if (!seen || seen.since <= 0 || working) {
3977
+ return false;
3978
+ }
3979
+ const since = seen.at[channelId] ?? seen.since;
3980
+ const said = lastAt(messages);
3981
+ return said !== undefined && said > since || (seen.unread?.[channelId] ?? 0) > since;
3982
+ }
3983
+ ChannelSeenUtil.unseen = unseen;
3984
+ function look(seen, channelId, at) {
3985
+ const before = seen.at[channelId] ?? seen.since;
3986
+ const mark = seen.unread?.[channelId] ?? 0;
3987
+ if (before >= at && mark <= before) {
3988
+ return;
3989
+ }
3990
+ const times2 = { ...seen.at, [channelId]: Math.max(at, mark) };
3991
+ const unread = stillUnread(seen.unread ?? {}, times2, seen.since);
3992
+ return unread ? { since: seen.since, at: times2, unread } : { since: seen.since, at: times2 };
3993
+ }
3994
+ ChannelSeenUtil.look = look;
3995
+ function markUnread(seen, channelId, now) {
3996
+ const mark = Math.max(now, seen.at[channelId] ?? seen.since) + 1;
3997
+ return { ...seen, unread: { ...seen.unread, [channelId]: mark } };
3998
+ }
3999
+ ChannelSeenUtil.markUnread = markUnread;
4000
+ function times(record) {
4001
+ return Object.fromEntries(Object.entries(record).filter((entry) => typeof entry[1] === "number"));
4002
+ }
4003
+ function later(a, b) {
4004
+ const out = { ...a };
4005
+ for (const [id, time] of Object.entries(b)) {
4006
+ if (time > (out[id] ?? 0)) {
4007
+ out[id] = time;
4008
+ }
4009
+ }
4010
+ return out;
4011
+ }
4012
+ function stillUnread(unread, at, since) {
4013
+ const kept = Object.entries(unread).filter(([id, mark]) => mark > (at[id] ?? since));
4014
+ return kept.length ? Object.fromEntries(kept) : undefined;
4015
+ }
4016
+ function same(a, b) {
4017
+ const left = Object.entries(a);
4018
+ return left.length === Object.keys(b).length && left.every(([id, time]) => b[id] === time);
4019
+ }
4020
+ })(ChannelSeenUtil ||= {});
4021
+
4022
+ class ChannelSeenBook {
4023
+ storage;
4024
+ current;
4025
+ listeners = new Set;
4026
+ constructor(storage) {
4027
+ this.storage = storage;
4028
+ }
4029
+ get state() {
4030
+ return this.current;
4031
+ }
4032
+ subscribe(listener) {
4033
+ this.listeners.add(listener);
4034
+ return () => this.listeners.delete(listener);
4035
+ }
4036
+ start() {
4037
+ this.keep();
4038
+ return this.storage.onChange((keys) => keys.includes(CHANNELS_SEEN_KEY) && void this.keep());
4039
+ }
4040
+ unseen(channelId, messages, working) {
4041
+ return ChannelSeenUtil.unseen(this.current, channelId, messages, working);
4042
+ }
4043
+ look(channelId, at) {
4044
+ const next = this.current && ChannelSeenUtil.look(this.current, channelId, at);
4045
+ if (next) {
4046
+ this.put(next);
4047
+ this.keep();
4048
+ }
4049
+ }
4050
+ mark(channelId, unread, lastAt) {
4051
+ if (!this.current) {
4052
+ return;
4053
+ }
4054
+ if (!unread) {
4055
+ this.look(channelId, Math.max(Date.now(), lastAt ?? 0));
4056
+ return;
4057
+ }
4058
+ this.put(ChannelSeenUtil.markUnread(this.current, channelId, Date.now()));
4059
+ this.keep();
4060
+ }
4061
+ put(next) {
4062
+ this.current = next;
4063
+ for (const listener of this.listeners) {
4064
+ listener();
4065
+ }
4066
+ }
4067
+ async keep() {
4068
+ try {
4069
+ const kept = ChannelSeenUtil.parse(await this.storage.get(CHANNELS_SEEN_KEY));
4070
+ const local = this.current;
4071
+ const next = kept ? local ? ChannelSeenUtil.merge(local, kept) : kept : local ?? { since: Date.now(), at: {} };
4072
+ if (next !== local) {
4073
+ this.put(next);
4074
+ }
4075
+ if (!kept || ChannelSeenUtil.merge(kept, next) !== kept) {
4076
+ await this.storage.set(CHANNELS_SEEN_KEY, next);
4077
+ }
4078
+ } catch {}
4079
+ }
4080
+ }
4081
+
3927
4082
  // web-src/channels.ts
3928
4083
  class ChannelsClient {
3929
4084
  storage;
@@ -5361,6 +5516,19 @@ function ChannelRoom({ context, channelId: fixed }) {
5361
5516
  service.messages(channelId);
5362
5517
  }
5363
5518
  }, [service, channelId]);
5519
+ const page = pi.ui.useView(CHANNEL_PAGE);
5520
+ const inFront = !!channelId && (!!fixed || !!page?.shown);
5521
+ const lastAt = ChannelSeenUtil.lastAt(messages);
5522
+ const resting = working.length === 0;
5523
+ React.useEffect(() => {
5524
+ if (!channelId || !inFront || !resting) {
5525
+ return;
5526
+ }
5527
+ const look = () => document.visibilityState === "visible" && context.seen.look(channelId, Math.max(Date.now(), lastAt ?? 0));
5528
+ look();
5529
+ document.addEventListener("visibilitychange", look);
5530
+ return () => document.removeEventListener("visibilitychange", look);
5531
+ }, [context, channelId, inFront, resting, lastAt]);
5364
5532
  React.useEffect(() => {
5365
5533
  if (!channelId) {
5366
5534
  return;
@@ -5800,17 +5968,16 @@ async function exportChannel(context, channel) {
5800
5968
  download2(`${channel.name}.md`, ChannelsText.channelMarkdown(service.state.team, channel, messages, { user: w.you, ta: w.ta }));
5801
5969
  }
5802
5970
  function ChannelMenu({ context, channel, working }) {
5803
- const { pi, service, ui } = context;
5804
- const { fresh } = useUi(ui);
5971
+ const { pi, service } = context;
5972
+ const unread = useChannelUnseen(context, channel.id);
5805
5973
  const w = ChannelViews.words(pi);
5806
5974
  const icon = (name) => ChannelViews.maybeIcon(pi, name, null, 15);
5807
5975
  const act = (action2) => () => {
5808
5976
  Promise.resolve().then(action2).catch((error) => pi.ui.notify(ChannelViews.message(error), "error"));
5809
5977
  };
5810
- const unread = fresh.includes(channel.id);
5811
5978
  const manage = ChannelsUtil.may(service.state.team).manage(channel.id);
5812
5979
  const pin = act(() => service.updateChannel(channel.id, { pinned: !channel.pinned }));
5813
- const markRead = act(() => ui.set({ fresh: unread ? fresh.filter((id) => id !== channel.id) : [...fresh, channel.id] }));
5980
+ const markRead = act(() => context.seen.mark(channel.id, !unread, ChannelSeenUtil.lastAt(service.state.messages[channel.id])));
5814
5981
  const rename = act(() => renameChannel(context, channel));
5815
5982
  const editTopic = act(() => editChannelTopic(context, channel));
5816
5983
  const members = act(() => {
@@ -6330,10 +6497,12 @@ class ChannelPartners {
6330
6497
  start() {
6331
6498
  const stopService = this.context.service.subscribe(() => this.refresh());
6332
6499
  const stopUi = this.context.ui.subscribe(() => this.refresh());
6500
+ const stopSeen = this.context.seen.subscribe(() => this.refresh());
6333
6501
  this.refresh();
6334
6502
  return () => {
6335
6503
  stopService();
6336
6504
  stopUi();
6505
+ stopSeen();
6337
6506
  };
6338
6507
  }
6339
6508
  add() {
@@ -6369,8 +6538,8 @@ class ChannelPartners {
6369
6538
  };
6370
6539
  }
6371
6540
  refresh() {
6372
- const { pi, service, ui } = this.context;
6373
- const { team, messages, working, loaded } = service.state;
6541
+ const { pi, service, seen } = this.context;
6542
+ const { team, messages, working, answered, loaded } = service.state;
6374
6543
  if (!loaded) {
6375
6544
  return;
6376
6545
  }
@@ -6384,11 +6553,10 @@ class ChannelPartners {
6384
6553
  }
6385
6554
  }
6386
6555
  const agents = ChannelsUtil.standing(team);
6387
- const { fresh } = ui.state;
6388
6556
  const language = pi.ui.language;
6389
6557
  const signature = [
6390
6558
  language,
6391
- ...agents.map((agent) => [agent.id, agent.name, agent.role, agent.face, agent.sessionFile ?? "", agent.cwd ?? ""].join(":")),
6559
+ ...agents.map((agent) => [agent.id, agent.name, agent.role, agent.face, agent.sessionFile ?? "", agent.cwd ?? "", answered[agent.id] ?? ""].join(":")),
6392
6560
  ...channels.map((channel) => [
6393
6561
  channel.id,
6394
6562
  channel.name,
@@ -6396,7 +6564,7 @@ class ChannelPartners {
6396
6564
  channel.members.join("/"),
6397
6565
  messages[channel.id]?.at(-1)?.id ?? "",
6398
6566
  (working[channel.id] ?? []).join("/"),
6399
- fresh.includes(channel.id) ? 1 : 0
6567
+ seen.unseen(channel.id, messages[channel.id], (working[channel.id] ?? []).length > 0) ? 1 : 0
6400
6568
  ].join(":"))
6401
6569
  ].join("|");
6402
6570
  if (signature === this.signature) {
@@ -6419,7 +6587,7 @@ class ChannelPartners {
6419
6587
  face: this.agentFace(agent.id),
6420
6588
  icon: ChannelViews.icon(pi, "Bot"),
6421
6589
  about: agent.role,
6422
- chat: agent.sessionFile ? { sessionFile: agent.sessionFile } : undefined,
6590
+ chat: agent.sessionFile ? { sessionFile: agent.sessionFile, seenAt: this.context.service.state.answered[agent.id] } : undefined,
6423
6591
  location: agent.cwd,
6424
6592
  open: () => ChannelViews.openAgent(this.context, agent.id),
6425
6593
  info: () => AgentActions.profile(this.context, agent),
@@ -6428,7 +6596,7 @@ class ChannelPartners {
6428
6596
  };
6429
6597
  }
6430
6598
  channel(channel, w) {
6431
- const { pi, service, ui } = this.context;
6599
+ const { pi, service, seen } = this.context;
6432
6600
  const { team, messages, working } = service.state;
6433
6601
  const busy = working[channel.id] ?? [];
6434
6602
  const name = (id) => ChannelsText.authorName(team, id, { user: w.you, ta: w.ta });
@@ -6443,7 +6611,7 @@ class ChannelPartners {
6443
6611
  chat: {
6444
6612
  last: busy.length > 0 ? ChannelWords.format(w.answeringNow, { name: busy.map(name).join("、") }) : last ? `${name(last.author)}:${(last.images?.length && !last.text ? w.picture : last.text).replace(/\s+/g, " ")}` : undefined,
6445
6613
  at: SidebarUtil.channelAt(channel, messages[channel.id]),
6446
- unseen: ui.state.fresh.includes(channel.id),
6614
+ unseen: seen.unseen(channel.id, messages[channel.id], busy.length > 0),
6447
6615
  busy: busy.length > 0
6448
6616
  },
6449
6617
  open: () => this.context.showChannel(channel.id),
@@ -6459,20 +6627,20 @@ class ChannelPartners {
6459
6627
  return Object.values(this.context.service.state.working).some((ids) => ids.includes(id));
6460
6628
  }
6461
6629
  channelMenu(channel) {
6462
- const { pi, service, ui } = this.context;
6630
+ const { pi, service, seen } = this.context;
6463
6631
  const w = ChannelViews.words(pi);
6464
6632
  const icon = (name) => ChannelViews.icon(pi, name);
6465
6633
  const fail = (error) => pi.ui.notify(ChannelViews.message(error), "error");
6466
- const { fresh } = ui.state;
6467
- const unread = fresh.includes(channel.id);
6634
+ const messages = service.state.messages[channel.id];
6468
6635
  const working = service.state.working[channel.id] ?? [];
6636
+ const unread = seen.unseen(channel.id, messages, working.length > 0);
6469
6637
  return [
6470
6638
  [
6471
6639
  {
6472
6640
  key: "read",
6473
6641
  label: unread ? w.markRead : w.markUnread,
6474
6642
  icon: icon(unread ? "MessageSquareText" : "MessageSquareDot"),
6475
- run: () => ui.set({ fresh: unread ? fresh.filter((id) => id !== channel.id) : [...fresh, channel.id] })
6643
+ run: () => seen.mark(channel.id, !unread, ChannelSeenUtil.lastAt(messages))
6476
6644
  }
6477
6645
  ],
6478
6646
  [
@@ -6925,7 +7093,9 @@ var NO_HOME_ITEMS = [];
6925
7093
  function AgentsSidebar({ context, partners = false }) {
6926
7094
  const { pi, service, ui } = context;
6927
7095
  const state = useChannels(service);
6928
- const { query, draft, channelId, fresh: unread } = useUi(ui);
7096
+ const { query, draft, channelId } = useUi(ui);
7097
+ const channelsSeen = React.useSyncExternalStore((listener) => context.seen.subscribe(listener), () => context.seen.state);
7098
+ const unread = (id) => ChannelSeenUtil.unseen(channelsSeen, id, state.messages[id], (state.working[id] ?? []).length > 0);
6929
7099
  const w = ChannelViews.words(pi);
6930
7100
  const faces = useFaces(state.team);
6931
7101
  const may = ChannelsUtil.may(state.team);
@@ -7174,7 +7344,7 @@ function AgentsSidebar({ context, partners = false }) {
7174
7344
  count: channels.length + groupItems.length + chatAgents.length,
7175
7345
  folded,
7176
7346
  onFold: fold,
7177
- alert: channels.some((channel) => unread.includes(channel.id)) || groupItems.some((item) => item.unseen)
7347
+ alert: channels.some((channel) => unread(channel.id)) || groupItems.some((item) => item.unseen)
7178
7348
  }, SidebarUtil.byTime([
7179
7349
  ...channels.map((channel) => ({
7180
7350
  channel,
@@ -7218,7 +7388,7 @@ function AgentsSidebar({ context, partners = false }) {
7218
7388
  }),
7219
7389
  preview,
7220
7390
  live: busy.length > 0,
7221
- dot: busy.length > 0 ? "running" : unread.includes(channel.id) ? "attention" : undefined,
7391
+ dot: busy.length > 0 ? "running" : unread(channel.id) ? "attention" : undefined,
7222
7392
  time: SidebarUtil.time(last?.at ?? channel.createdAt, pi.ui.language),
7223
7393
  hint: channel.topic ? `#${channel.name} · ${channel.topic}` : `#${channel.name}`,
7224
7394
  selected: !!channelPage?.shown && channelId === channel.id,
@@ -7937,13 +8107,16 @@ function activate(pi) {
7937
8107
  const removeStyle = injectStyle();
7938
8108
  const service = new ChannelsClient(pi.storage, (request) => pi.callHost("request", request));
7939
8109
  const ui = new ChannelsUiStore;
8110
+ const seen = new ChannelSeenBook(pi.storage);
8111
+ const stopSeen = seen.start();
7940
8112
  const context = {
7941
8113
  pi,
7942
8114
  service,
7943
8115
  ui,
8116
+ seen,
7944
8117
  sessions: new Map,
7945
8118
  showChannel: (channelId) => {
7946
- ui.set({ channelId, adding: false, fresh: ui.state.fresh.filter((id) => id !== channelId) });
8119
+ ui.set({ channelId, adding: false });
7947
8120
  pi.ui.showView(CHANNEL_PAGE);
7948
8121
  },
7949
8122
  showMembers: (adding = false) => {
@@ -8018,6 +8191,7 @@ function activate(pi) {
8018
8191
  context
8019
8192
  }),
8020
8193
  location: "panel",
8194
+ navigation: false,
8021
8195
  order: 61
8022
8196
  });
8023
8197
  pi.registerView("members", {
@@ -8027,6 +8201,7 @@ function activate(pi) {
8027
8201
  context
8028
8202
  }),
8029
8203
  location: "panel",
8204
+ navigation: false,
8030
8205
  order: 60
8031
8206
  });
8032
8207
  registerCardOpener(context);
@@ -8034,14 +8209,9 @@ function activate(pi) {
8034
8209
  context,
8035
8210
  ...props
8036
8211
  }));
8037
- const stopFocus = service.onFocus((channelId) => {
8038
- if (!ui.state.fresh.includes(channelId)) {
8039
- ui.set({ fresh: [...ui.state.fresh, channelId] });
8040
- }
8041
- });
8042
8212
  service.load();
8043
8213
  return () => {
8044
- stopFocus();
8214
+ stopSeen();
8045
8215
  stopPartners?.();
8046
8216
  service.dispose();
8047
8217
  removeStyle();