Skip to content

Commit f4db3f0

Browse files
morgmartCarl
andcommitted
Re-render only rows with agent work on each activity step
Rows without work subscribe to a primitive per-message key instead of the whole activity snapshot. Co-authored-by: Carl <c217fe6b9d958f41c3a5e030dccc7f626775a923089cb6491305eade75ea1f1b@buzz.block.builderlab.xyz> Signed-off-by: morgmart <[email protected]>
1 parent d7607e0 commit f4db3f0

2 files changed

Lines changed: 48 additions & 27 deletions

File tree

‎src/bundled/agent-activity/ActivityAccessory.tsx‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,37 @@ import {
3939
import { useTypingReplacement } from "../../features/conversation/typing-presentation";
4040
import styles from "./ActivityAccessory.module.css";
4141

42+
const noSubscribe = () => () => {};
4243
/** Activity belongs to the exact triggering message, not the composer. */
4344
export function ActivityAccessory(props: ComposerAccessoryProps) {
4445
const { session, channelId, message, canOpen } = props;
45-
const snapshot = useSyncExternalStore(
46+
const messageId = message?.id;
47+
// Every row mounts this; only rows with work subscribe to each activity
48+
// step, profile and choice change. Others read once and wait for work.
49+
const hasWork = useSyncExternalStore(
4650
session.agentActivity.subscribe,
51+
() =>
52+
!!messageId &&
53+
messageActivity(session.agentActivity.snapshot(), channelId, messageId)
54+
.length > 0,
55+
);
56+
const snapshot = useSyncExternalStore(
57+
hasWork ? session.agentActivity.subscribe : noSubscribe,
4758
session.agentActivity.snapshot,
4859
);
49-
const messageId = message?.id;
5060
const entries = useMemo(
51-
() => (messageId ? messageActivity(snapshot, channelId, messageId) : []),
52-
[snapshot, channelId, messageId],
61+
() =>
62+
messageId && hasWork
63+
? messageActivity(snapshot, channelId, messageId)
64+
: [],
65+
[snapshot, channelId, messageId, hasWork],
5366
);
5467
const profiles = useSyncExternalStore(
55-
session.profiles.subscribe,
68+
hasWork ? session.profiles.subscribe : noSubscribe,
5669
session.profiles.snapshot,
5770
);
5871
const choices = useSyncExternalStore(
59-
session.agentChoices.subscribe,
72+
hasWork ? session.agentChoices.subscribe : noSubscribe,
6073
session.agentChoices.snapshot,
6174
);
6275
const resolveName = useChannelIdentityNames(session, channelId);

‎src/features/messages/MessageRow.tsx‎

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,38 +1309,46 @@ function useThreadAgents(
13091309
get,
13101310
get,
13111311
);
1312-
const activity = useSyncExternalStore(
1312+
// Subscribe to this thread's working agents, not every activity step.
1313+
const getWork = () => {
1314+
const activity = session?.agentActivity?.snapshot();
1315+
const typers = threadWorkingAgents(
1316+
activity,
1317+
keys ? keys.split(",") : [],
1318+
channelId,
1319+
rootId,
1320+
{ messageId, agents: represented },
1321+
);
1322+
const local = localActivity(
1323+
{
1324+
local: (activity?.local ?? []).filter(
1325+
(row) => row.messageId !== messageId || !represented.has(row.agent),
1326+
),
1327+
},
1328+
channelId,
1329+
rootId,
1330+
);
1331+
const waiting =
1332+
typers.length > 0 && typers.every((agent) => breathing(local.get(agent)));
1333+
return `${waiting ? "waiting" : "working"}:${typers.join(",")}`;
1334+
};
1335+
const work = useSyncExternalStore(
13131336
session?.agentActivity?.subscribe ?? noSubscribe,
1314-
session?.agentActivity?.snapshot ?? noLibrary,
1315-
session?.agentActivity?.snapshot ?? noLibrary,
1316-
);
1317-
const typers = threadWorkingAgents(
1318-
activity,
1319-
keys ? keys.split(",") : [],
1320-
channelId,
1321-
rootId,
1322-
{ messageId, agents: represented },
1337+
getWork,
1338+
getWork,
13231339
);
1340+
const [state, joined = ""] = work.split(/:(.*)/s);
1341+
const typers = joined ? joined.split(",") : [];
13241342
const hasWork = typers.length > 0;
13251343
const getLibrary = (hasWork && session?.agentChoices.snapshot) || noLibrary;
13261344
const library = useSyncExternalStore(
13271345
(hasWork && session?.agentChoices.subscribe) || noSubscribe,
13281346
getLibrary,
13291347
getLibrary,
13301348
);
1331-
const local = localActivity(
1332-
{
1333-
local: (activity?.local ?? []).filter(
1334-
(row) => row.messageId !== messageId || !represented.has(row.agent),
1335-
),
1336-
},
1337-
channelId,
1338-
rootId,
1339-
);
13401349
return {
13411350
agents:
13421351
library?.identities.filter(({ pubkey }) => typers.includes(pubkey)) ?? [],
1343-
waiting:
1344-
typers.length > 0 && typers.every((agent) => breathing(local.get(agent))),
1352+
waiting: state === "waiting",
13451353
};
13461354
}

0 commit comments

Comments
 (0)