['session'];
+
+function Status({ session }: { readonly session: Session }) {
+ const snapshot = useAgent(session);
+ return (
+
+ {`${snapshot.status}:${snapshot.messages.map((m) => m.content).join(',')}`}
+
+ );
+}
+
+it('renders the session snapshot on the server without subscribing', () => {
+ const fake = createFakeAgentSession();
+ let subscriptions = 0;
+ const session: Session = {
+ ...fake.session,
+ getSnapshot: () => fake.session.getSnapshot(),
+ subscribe: (notify) => {
+ subscriptions += 1;
+ return fake.session.subscribe(notify);
+ },
+ };
+ const html = renderToString();
+ expect(html).toContain('idle:');
+ expect(subscriptions).toBe(0);
+});
+
+it('renders the snapshot a session already holds', () => {
+ const fake = createFakeAgentSession();
+ fake.push({ role: 'user', content: 'hello from the server' });
+ const html = renderToString();
+ expect(html).toContain('hello from the server');
+});
diff --git a/libs/react/src/use-agent.ts b/libs/react/src/use-agent.ts
index 733c6f630..e35fa04cf 100644
--- a/libs/react/src/use-agent.ts
+++ b/libs/react/src/use-agent.ts
@@ -12,5 +12,6 @@ export function useAgent(session: {
(notify: () => void) => session.subscribe(notify),
[session]
);
- return useSyncExternalStore(subscribe, getSnapshot);
+ // A session does no I/O until it is used, so its current snapshot is also the server snapshot.
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
}