@gemies/sdk 5.0.0 → 6.0.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.
- package/CHANGELOG.md +24 -0
- package/README.md +19 -0
- package/package.json +2 -1
- package/src/components/GemieActivityLine.tsx +28 -0
- package/src/components/GemieChatContent.tsx +5 -2
- package/src/components/GemieParticipants.tsx +4 -0
- package/src/hooks/useGemieActivity.ts +28 -23
- package/src/hooks/useMessageReactions.ts +10 -15
- package/src/index.ts +6 -0
- package/src/lib/activity-store.ts +6 -16
- package/src/lib/labels-es.ts +10 -0
- package/src/lib/labels.tsx +11 -0
- package/src/lib/participants.ts +5 -0
- package/src/realtime/activity-text.ts +23 -0
- package/src/realtime/activity.ts +96 -0
- package/src/realtime/cache.ts +244 -0
- package/src/realtime/connection.ts +317 -0
- package/src/realtime/contract.ts +148 -0
- package/src/realtime/index.ts +33 -0
- package/src/realtime/react.tsx +196 -0
- package/src/runtime/config.tsx +8 -26
- package/src/runtime/conversations.ts +55 -3
- package/src/runtime/index.ts +8 -0
- package/src/runtime/routes.ts +2 -0
- package/src/runtime/types.ts +5 -0
- package/src/runtime/use-agent-activity.ts +18 -45
- package/src/runtime/use-conversation-stream.ts +7 -2
- package/src/runtime/use-conversation-thread.ts +61 -26
- package/src/runtime/use-room-events.ts +10 -9
- package/src/lib/activity-socket.ts +0 -137
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realtime: the one socket a signed-in client holds to Gemies (`docs/realtime.mdx`). Mounted by
|
|
3
|
+
* `GemieRuntimeProvider`; a host listens with the hooks below for what is its own (credits, its
|
|
4
|
+
* lists, notifications), while the runtime's conversations, reactions, rooms and "typing…" are
|
|
5
|
+
* kept live by the SDK itself.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
GemieRealtimeProvider,
|
|
10
|
+
useGemieRealtime,
|
|
11
|
+
useRealtimeEvent,
|
|
12
|
+
useRealtimeReset,
|
|
13
|
+
useRealtimeTopic,
|
|
14
|
+
} from './react';
|
|
15
|
+
export {
|
|
16
|
+
useConversationActivity,
|
|
17
|
+
useRealtimeActivityStore,
|
|
18
|
+
type ActorActivityEntry,
|
|
19
|
+
} from './activity';
|
|
20
|
+
export type { RealtimeConnection, RealtimeListener } from './connection';
|
|
21
|
+
export {
|
|
22
|
+
REALTIME_DURABLE_TYPES,
|
|
23
|
+
REALTIME_SIGNAL_TYPES,
|
|
24
|
+
type ActivityState,
|
|
25
|
+
type RealtimeActivity,
|
|
26
|
+
type RealtimeConversationRow,
|
|
27
|
+
type RealtimeData,
|
|
28
|
+
type RealtimeDurableType,
|
|
29
|
+
type RealtimeFrame,
|
|
30
|
+
type RealtimeSignalType,
|
|
31
|
+
type RealtimeThreadTotal,
|
|
32
|
+
type RealtimeType,
|
|
33
|
+
} from './contract';
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The realtime connection in React (`docs/realtime.mdx`).
|
|
3
|
+
*
|
|
4
|
+
* {@link GemieRealtimeProvider} is mounted by `GemieRuntimeProvider`: it holds the signed-in
|
|
5
|
+
* account's connection to the runtime's API and keeps the runtime's caches and the activity store
|
|
6
|
+
* in step with it. Everything else only listens: {@link useRealtimeEvent} for a type,
|
|
7
|
+
* {@link useRealtimeTopic} for an agent run, {@link useConversationActivity} for "typing…".
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { QueryClientContext } from '@tanstack/react-query';
|
|
11
|
+
import { useOxy } from '@oxy.so/services/ui/client';
|
|
12
|
+
import {
|
|
13
|
+
createContext,
|
|
14
|
+
type ReactNode,
|
|
15
|
+
useContext,
|
|
16
|
+
useEffect,
|
|
17
|
+
useMemo,
|
|
18
|
+
useRef,
|
|
19
|
+
useState,
|
|
20
|
+
} from 'react';
|
|
21
|
+
import { useRealtimeActivityStore } from './activity';
|
|
22
|
+
import { attachRealtimeCache } from './cache';
|
|
23
|
+
import { acquireRealtime, type RealtimeConnection, type RealtimeListener } from './connection';
|
|
24
|
+
import type { RealtimeActivity, RealtimeFrame, RealtimeType } from './contract';
|
|
25
|
+
|
|
26
|
+
interface RealtimeHold {
|
|
27
|
+
readonly apiUrl: string;
|
|
28
|
+
readonly connection: RealtimeConnection | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const RealtimeContext = createContext<RealtimeHold | null>(null);
|
|
32
|
+
|
|
33
|
+
/** Hold the signed-in account's connection to `apiUrl`; `null` while signed out. */
|
|
34
|
+
function useHeldConnection(apiUrl: string, enabled: boolean): RealtimeConnection | null {
|
|
35
|
+
const { oxyServices, user, canUsePrivateApi } = useOxy();
|
|
36
|
+
const account = enabled && canUsePrivateApi ? (user?.id ?? null) : null;
|
|
37
|
+
const [connection, setConnection] = useState<RealtimeConnection | null>(null);
|
|
38
|
+
// The connection is the account's: a new services object for the same account does not open
|
|
39
|
+
// another one (read when the connection is opened).
|
|
40
|
+
const servicesRef = useRef(oxyServices);
|
|
41
|
+
servicesRef.current = oxyServices;
|
|
42
|
+
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (!account) {
|
|
45
|
+
setConnection(null);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// The API's own token, refreshed by Oxy when it is due — never a captured one.
|
|
49
|
+
const linked = servicesRef.current.createLinkedClient({ baseURL: apiUrl });
|
|
50
|
+
const held = acquireRealtime(apiUrl, account, () => linked.getAccessToken(apiUrl));
|
|
51
|
+
setConnection(held.connection);
|
|
52
|
+
return () => {
|
|
53
|
+
held.release();
|
|
54
|
+
linked.dispose();
|
|
55
|
+
setConnection(null);
|
|
56
|
+
};
|
|
57
|
+
}, [apiUrl, account]);
|
|
58
|
+
|
|
59
|
+
return connection;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const activityHolders = new WeakMap<RealtimeConnection, { refs: number; detach: () => void }>();
|
|
63
|
+
|
|
64
|
+
/** Feed a connection's `activity` frames into the store, once per connection. */
|
|
65
|
+
function attachActivity(connection: RealtimeConnection): () => void {
|
|
66
|
+
const existing = activityHolders.get(connection);
|
|
67
|
+
if (existing) existing.refs++;
|
|
68
|
+
else {
|
|
69
|
+
const store = useRealtimeActivityStore.getState;
|
|
70
|
+
const offFrames = connection.on('activity', (frame) =>
|
|
71
|
+
store().apply(frame.data as RealtimeActivity),
|
|
72
|
+
);
|
|
73
|
+
// What happened while away is unknown: a stale "typing…" is worse than none.
|
|
74
|
+
const offStatus = connection.onStatus((connected) => {
|
|
75
|
+
if (!connected) store().clear();
|
|
76
|
+
});
|
|
77
|
+
activityHolders.set(connection, {
|
|
78
|
+
refs: 1,
|
|
79
|
+
detach: () => {
|
|
80
|
+
offFrames();
|
|
81
|
+
offStatus();
|
|
82
|
+
store().clear();
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
let released = false;
|
|
87
|
+
return () => {
|
|
88
|
+
if (released) return;
|
|
89
|
+
released = true;
|
|
90
|
+
const entry = activityHolders.get(connection);
|
|
91
|
+
if (!entry || --entry.refs > 0) return;
|
|
92
|
+
entry.detach();
|
|
93
|
+
activityHolders.delete(connection);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Mounted by `GemieRuntimeProvider`: the account's connection for everything below, wired into the
|
|
99
|
+
* runtime's caches and the activity store. A nested provider on the same API shares its parent's.
|
|
100
|
+
*/
|
|
101
|
+
export function GemieRealtimeProvider({
|
|
102
|
+
apiUrl,
|
|
103
|
+
children,
|
|
104
|
+
}: {
|
|
105
|
+
apiUrl: string;
|
|
106
|
+
children: ReactNode;
|
|
107
|
+
}) {
|
|
108
|
+
const parent = useContext(RealtimeContext);
|
|
109
|
+
const inherited = parent !== null && parent.apiUrl === apiUrl;
|
|
110
|
+
const connection = useHeldConnection(apiUrl, !inherited);
|
|
111
|
+
// A host without React Query has no runtime caches to keep; it still hears activity.
|
|
112
|
+
const queryClient = useContext(QueryClientContext);
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (inherited || !connection) return;
|
|
115
|
+
const detachCache = queryClient ? attachRealtimeCache(connection, queryClient) : undefined;
|
|
116
|
+
const detachActivity = attachActivity(connection);
|
|
117
|
+
return () => {
|
|
118
|
+
detachCache?.();
|
|
119
|
+
detachActivity();
|
|
120
|
+
};
|
|
121
|
+
}, [inherited, connection, queryClient]);
|
|
122
|
+
const value = useMemo<RealtimeHold>(
|
|
123
|
+
() => (inherited && parent ? parent : { apiUrl, connection }),
|
|
124
|
+
[inherited, parent, apiUrl, connection],
|
|
125
|
+
);
|
|
126
|
+
return <RealtimeContext.Provider value={value}>{children}</RealtimeContext.Provider>;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** The account's realtime connection, or `null` while signed out or outside a runtime provider. */
|
|
130
|
+
export function useGemieRealtime(): RealtimeConnection | null {
|
|
131
|
+
return useContext(RealtimeContext)?.connection ?? null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Call `handler` for every frame of `type` while mounted (the latest `handler`, without resubscribing). */
|
|
135
|
+
export function useRealtimeEvent<T extends RealtimeType | '*'>(
|
|
136
|
+
type: T,
|
|
137
|
+
handler: (frame: RealtimeFrame) => void,
|
|
138
|
+
): void {
|
|
139
|
+
const connection = useGemieRealtime();
|
|
140
|
+
const ref = useRef(handler);
|
|
141
|
+
ref.current = handler;
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
if (!connection) return;
|
|
144
|
+
const listener: RealtimeListener = (frame) => ref.current(frame);
|
|
145
|
+
return connection.on(type, listener);
|
|
146
|
+
}, [connection, type]);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Call `handler` when the client must reload what it shows (its cursor fell out of the log). */
|
|
150
|
+
export function useRealtimeReset(handler: () => void): void {
|
|
151
|
+
const connection = useGemieRealtime();
|
|
152
|
+
const ref = useRef(handler);
|
|
153
|
+
ref.current = handler;
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
if (!connection) return;
|
|
156
|
+
return connection.onReset(() => ref.current());
|
|
157
|
+
}, [connection]);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Follow a topic (`agent:<id>`, `agent-session:<id>`) while mounted, and hear its frames. The
|
|
162
|
+
* server checks the person may; a refused topic simply never speaks.
|
|
163
|
+
*/
|
|
164
|
+
export function useRealtimeTopic(
|
|
165
|
+
topic: string | null,
|
|
166
|
+
handler: (frame: RealtimeFrame) => void,
|
|
167
|
+
): void {
|
|
168
|
+
const connection = useGemieRealtime();
|
|
169
|
+
const ref = useRef(handler);
|
|
170
|
+
ref.current = handler;
|
|
171
|
+
useEffect(() => {
|
|
172
|
+
if (!connection || !topic) return;
|
|
173
|
+
const release = connection.follow(topic);
|
|
174
|
+
const off = connection.on('agent.activity', (frame) => {
|
|
175
|
+
if (frame.topic === topic) ref.current(frame);
|
|
176
|
+
});
|
|
177
|
+
return () => {
|
|
178
|
+
off();
|
|
179
|
+
release();
|
|
180
|
+
};
|
|
181
|
+
}, [connection, topic]);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Hear the account's `activity` where no runtime provider holds the connection — an "Ask Alia"
|
|
186
|
+
* button an app draws on its own (`useGemieActivity`). Inside a provider it does nothing: the
|
|
187
|
+
* provider already feeds the store.
|
|
188
|
+
*/
|
|
189
|
+
export function useRealtimeActivityFeed(apiUrl: string): void {
|
|
190
|
+
const inherited = useContext(RealtimeContext) !== null;
|
|
191
|
+
const connection = useHeldConnection(apiUrl, !inherited);
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
if (!connection) return;
|
|
194
|
+
return attachActivity(connection);
|
|
195
|
+
}, [connection]);
|
|
196
|
+
}
|
package/src/runtime/config.tsx
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type { MessageReactionUpdate } from './message-reactions';
|
|
2
1
|
import { createContext, type ReactNode, useContext, useMemo } from 'react';
|
|
3
2
|
import { GEMIES_API_URL } from '../lib/origins';
|
|
4
|
-
import type {
|
|
5
|
-
import
|
|
3
|
+
import type { AgentRun } from './types';
|
|
4
|
+
import { GemieRealtimeProvider } from '../realtime/react';
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* Where a signed-out session keeps its conversations, per account. The host
|
|
@@ -107,28 +106,6 @@ export interface GemieRuntimeHost {
|
|
|
107
106
|
onAgentTurn?: (turn: AgentRun & { conversationId: string | null }) => void;
|
|
108
107
|
/** The agent's row in the host's list, ahead of the server: your line, then her answer. */
|
|
109
108
|
onTurnPreview?: (agentId: string | null | undefined, text: string) => void;
|
|
110
|
-
/**
|
|
111
|
-
* Messages an agent writes into the open conversation by itself (a result, a check-in).
|
|
112
|
-
* Returns the unsubscribe.
|
|
113
|
-
*/
|
|
114
|
-
subscribeConversationMessages?: (
|
|
115
|
-
conversationId: string,
|
|
116
|
-
listener: (message: Message) => void,
|
|
117
|
-
) => () => void;
|
|
118
|
-
/** Authoritative reaction changes on existing messages, including another device. */
|
|
119
|
-
subscribeConversationReactions?: (
|
|
120
|
-
conversationId: string,
|
|
121
|
-
listener: (update: MessageReactionUpdate) => void,
|
|
122
|
-
) => () => void;
|
|
123
|
-
/**
|
|
124
|
-
* Live actor responses (group turns or reactions in an agent chat): the host's `group:event` socket
|
|
125
|
-
* event for this conversation. Returns the unsubscribe. Without it, what a group's members say
|
|
126
|
-
* after the request ended appears on reload.
|
|
127
|
-
*/
|
|
128
|
-
subscribeRoomEvents?: (
|
|
129
|
-
conversationId: string,
|
|
130
|
-
listener: (event: RoomEvent) => void,
|
|
131
|
-
) => () => void;
|
|
132
109
|
/**
|
|
133
110
|
* Show where the work of a turn is read — the host's right panel — now that the thread selected
|
|
134
111
|
* one (`useThoughtStore`; the panel is `ThoughtPanel`). Return `false` to decline an
|
|
@@ -213,7 +190,12 @@ export function GemieRuntimeProvider({
|
|
|
213
190
|
app: app ?? parent.app,
|
|
214
191
|
};
|
|
215
192
|
}, [apiUrl, offline, streamFetch, speech, host, app, parent]);
|
|
216
|
-
return
|
|
193
|
+
return (
|
|
194
|
+
<GemieRuntimeContext.Provider value={value}>
|
|
195
|
+
{/* The account's one realtime connection, for everything below (`docs/realtime.mdx`). */}
|
|
196
|
+
<GemieRealtimeProvider apiUrl={value.apiUrl}>{children}</GemieRealtimeProvider>
|
|
197
|
+
</GemieRuntimeContext.Provider>
|
|
198
|
+
);
|
|
217
199
|
}
|
|
218
200
|
|
|
219
201
|
/** The runtime's configuration: the nearest provider's, or the defaults. */
|
|
@@ -18,6 +18,8 @@ import { gemieQueryKeys, type ConversationsFilter } from './query-keys';
|
|
|
18
18
|
import { API_ROUTES } from './routes';
|
|
19
19
|
import { createRandomUuid } from './random-uuid';
|
|
20
20
|
import type { Conversation, ConversationsInfiniteData, ConversationsPage, Message } from './types';
|
|
21
|
+
import type { RealtimeConnection } from '../realtime/connection';
|
|
22
|
+
import { useGemieRealtime } from '../realtime/react';
|
|
21
23
|
|
|
22
24
|
/** Conversation as returned by the API (timestamps are ISO strings, not Dates). */
|
|
23
25
|
type ApiConversation = Omit<Conversation, 'createdAt' | 'updatedAt' | 'messages'> & {
|
|
@@ -106,6 +108,8 @@ export interface ConversationsClient {
|
|
|
106
108
|
newChatTitle: string;
|
|
107
109
|
/** The chat's app, which labels a conversation it creates (`GemieRuntimeConfig.app`). */
|
|
108
110
|
app: string | undefined;
|
|
111
|
+
/** Told the account's realtime cursor each read carried, so no event is lost behind it. */
|
|
112
|
+
realtime: RealtimeConnection | null;
|
|
109
113
|
}
|
|
110
114
|
|
|
111
115
|
/** The runtime's conversation client for the nearest provider. */
|
|
@@ -113,9 +117,16 @@ function useConversationsClient(): ConversationsClient {
|
|
|
113
117
|
const api = useGemieApi();
|
|
114
118
|
const { offline, app } = useGemieRuntime();
|
|
115
119
|
const { newChat } = useLabels();
|
|
120
|
+
const realtime = useGemieRealtime();
|
|
116
121
|
return useMemo(
|
|
117
|
-
() => ({
|
|
118
|
-
|
|
122
|
+
() => ({
|
|
123
|
+
api,
|
|
124
|
+
offline: new OfflineConversations(offline),
|
|
125
|
+
newChatTitle: newChat,
|
|
126
|
+
app,
|
|
127
|
+
realtime,
|
|
128
|
+
}),
|
|
129
|
+
[api, offline, newChat, app, realtime],
|
|
119
130
|
);
|
|
120
131
|
}
|
|
121
132
|
|
|
@@ -129,6 +140,7 @@ async function fetchConversationsPage(
|
|
|
129
140
|
conversations: ApiConversation[];
|
|
130
141
|
nextCursor: string | null;
|
|
131
142
|
hasMore: boolean;
|
|
143
|
+
realtimeSeq?: number;
|
|
132
144
|
}>(API_ROUTES.conversations.list, {
|
|
133
145
|
signal,
|
|
134
146
|
params: {
|
|
@@ -138,6 +150,7 @@ async function fetchConversationsPage(
|
|
|
138
150
|
...(app === undefined ? {} : { app }),
|
|
139
151
|
},
|
|
140
152
|
});
|
|
153
|
+
if (typeof data.realtimeSeq === 'number') client.realtime?.noteSnapshot(data.realtimeSeq);
|
|
141
154
|
return {
|
|
142
155
|
conversations: data.conversations.map((conv) => ({
|
|
143
156
|
...conv,
|
|
@@ -226,7 +239,10 @@ export function useConversations({ agentId, app }: ConversationsFilter = {}) {
|
|
|
226
239
|
|
|
227
240
|
async function fetchConversation(client: ConversationsClient, id: string): Promise<Conversation> {
|
|
228
241
|
try {
|
|
229
|
-
const { data } = await client.api.get<ApiConversation>(
|
|
242
|
+
const { data } = await client.api.get<ApiConversation & { realtimeSeq?: number }>(
|
|
243
|
+
API_ROUTES.conversations.get(id),
|
|
244
|
+
);
|
|
245
|
+
if (typeof data.realtimeSeq === 'number') client.realtime?.noteSnapshot(data.realtimeSeq);
|
|
230
246
|
return {
|
|
231
247
|
...data,
|
|
232
248
|
createdAt: new Date(data.createdAt),
|
|
@@ -273,6 +289,42 @@ export function useConversation(id: string | undefined) {
|
|
|
273
289
|
});
|
|
274
290
|
}
|
|
275
291
|
|
|
292
|
+
/** Read one conversation afresh, as `useConversation` would (a rewritten thread, reloaded). */
|
|
293
|
+
export function useReadConversation(): (id: string) => Promise<Conversation> {
|
|
294
|
+
const client = useConversationsClient();
|
|
295
|
+
return useCallback((id) => fetchConversation(client, id), [client]);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* The person has seen a conversation: its unread count goes to zero here at once and on every
|
|
300
|
+
* other surface through the server's `conversation` event. Sent only when there is something
|
|
301
|
+
* unread, so an open conversation costs nothing while nothing arrives.
|
|
302
|
+
*/
|
|
303
|
+
export function useMarkConversationRead(): (id: string) => void {
|
|
304
|
+
const queryClient = useQueryClient();
|
|
305
|
+
const client = useConversationsClient();
|
|
306
|
+
return useCallback(
|
|
307
|
+
(id) => {
|
|
308
|
+
const update = (c: Conversation) => (c.id === id ? { ...c, unreadCount: 0 } : c);
|
|
309
|
+
queryClient.setQueriesData<ConversationsInfiniteData>(
|
|
310
|
+
{ queryKey: gemieQueryKeys.conversations.all },
|
|
311
|
+
(old) =>
|
|
312
|
+
old?.pages
|
|
313
|
+
? {
|
|
314
|
+
...old,
|
|
315
|
+
pages: old.pages.map((page) => ({
|
|
316
|
+
...page,
|
|
317
|
+
conversations: page.conversations.map(update),
|
|
318
|
+
})),
|
|
319
|
+
}
|
|
320
|
+
: old,
|
|
321
|
+
);
|
|
322
|
+
void client.api.post(API_ROUTES.conversations.read(id), {}).catch(() => undefined);
|
|
323
|
+
},
|
|
324
|
+
[queryClient, client],
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
276
328
|
/** Prefetch a conversation's detail (call from the sidebar on press-in / hover). */
|
|
277
329
|
export function usePrefetchConversation(): (id: string) => void {
|
|
278
330
|
const queryClient = useQueryClient();
|
package/src/runtime/index.ts
CHANGED
|
@@ -25,6 +25,13 @@ export type {
|
|
|
25
25
|
} from './config';
|
|
26
26
|
export type { RoomEvent } from './room-events';
|
|
27
27
|
export type { MessageReactionUpdate } from './message-reactions';
|
|
28
|
+
export {
|
|
29
|
+
useGemieRealtime,
|
|
30
|
+
useRealtimeEvent,
|
|
31
|
+
useRealtimeReset,
|
|
32
|
+
useRealtimeTopic,
|
|
33
|
+
} from '../realtime/react';
|
|
34
|
+
export { useConversationActivity } from '../realtime/activity';
|
|
28
35
|
export { GemieApiError, useGemieApi } from './api';
|
|
29
36
|
export {
|
|
30
37
|
GEMIE_FEATURES,
|
|
@@ -87,6 +94,7 @@ export {
|
|
|
87
94
|
useConversations,
|
|
88
95
|
useCreateConversation,
|
|
89
96
|
useDeleteConversation,
|
|
97
|
+
useMarkConversationRead,
|
|
90
98
|
usePrefetchConversation,
|
|
91
99
|
useRenameConversation,
|
|
92
100
|
useSaveConversation,
|
package/src/runtime/routes.ts
CHANGED
|
@@ -25,6 +25,8 @@ export const API_ROUTES = {
|
|
|
25
25
|
* what is being deleted is the message collection and nothing else.
|
|
26
26
|
*/
|
|
27
27
|
clearMessages: (id: string) => `/v1/conversations/${id}/messages`,
|
|
28
|
+
/** The person has seen it: its unread count goes to zero on every surface. */
|
|
29
|
+
read: (id: string) => `/v1/conversations/${id}/read`,
|
|
28
30
|
vote: (id: string, messageId: string) => `/v1/conversations/${id}/messages/${messageId}/vote`,
|
|
29
31
|
react: (id: string, messageId: string) =>
|
|
30
32
|
`/v1/conversations/${id}/messages/${messageId}/reactions`,
|
package/src/runtime/types.ts
CHANGED
|
@@ -117,6 +117,11 @@ export interface Conversation {
|
|
|
117
117
|
threadId?: string | null;
|
|
118
118
|
/** The app the conversation started in, by its slug, or `null` for Alia's own surfaces. */
|
|
119
119
|
app?: string | null;
|
|
120
|
+
/**
|
|
121
|
+
* Messages the person has not seen: what arrived while nobody watched (a gemie writing on its
|
|
122
|
+
* own, a group member answering in the background). The sidebar's badge; zeroed by reading it.
|
|
123
|
+
*/
|
|
124
|
+
unreadCount?: number;
|
|
120
125
|
createdAt: Date;
|
|
121
126
|
updatedAt: Date;
|
|
122
127
|
messages: Message[];
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { useOxy } from '@oxy.so/services/ui/client';
|
|
2
1
|
import { useCallback, useEffect, useState } from 'react';
|
|
3
|
-
import {
|
|
2
|
+
import { useRealtimeTopic } from '../realtime/react';
|
|
3
|
+
import type { RealtimeFrame } from '../realtime/contract';
|
|
4
4
|
import { useGemieApi } from './api';
|
|
5
|
-
import { useGemieRuntime } from './config';
|
|
6
5
|
import { AGENT_ROUTES } from './routes';
|
|
7
6
|
import {
|
|
8
7
|
type AgentActivityEvent,
|
|
@@ -17,11 +16,12 @@ import {
|
|
|
17
16
|
export type UseAgentActivityResult = AgentActivityState;
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
|
-
* Follow one agent run live:
|
|
21
|
-
*
|
|
19
|
+
* Follow one agent run live: its session's topic on the account's realtime connection
|
|
20
|
+
* (`agent-session:<id>`, `docs/realtime.mdx`), seeded with what the run had already done (`GET` on
|
|
21
|
+
* its status, best-effort — the topic still carries live progress).
|
|
22
22
|
*
|
|
23
23
|
* @param sessionId - The agent session to follow; `null` follows nothing.
|
|
24
|
-
* @param agentId - The agent's own
|
|
24
|
+
* @param agentId - The agent's own topic too, for the runs that announce themselves there.
|
|
25
25
|
*/
|
|
26
26
|
export function useAgentActivity(
|
|
27
27
|
sessionId: string | null,
|
|
@@ -29,60 +29,33 @@ export function useAgentActivity(
|
|
|
29
29
|
): UseAgentActivityResult {
|
|
30
30
|
const [state, setState] = useState<AgentActivityState>(INITIAL_ACTIVITY);
|
|
31
31
|
const api = useGemieApi();
|
|
32
|
-
const { apiUrl } = useGemieRuntime();
|
|
33
|
-
const { oxyServices } = useOxy();
|
|
34
|
-
|
|
35
|
-
const handleEvent = useCallback((event: AgentActivityEvent) => {
|
|
36
|
-
setState((prev) => applyActivityEvent(prev, event));
|
|
37
|
-
}, []);
|
|
38
32
|
|
|
39
33
|
useEffect(() => {
|
|
40
34
|
if (!sessionId) return;
|
|
41
|
-
|
|
42
35
|
setState(INITIAL_ACTIVITY);
|
|
43
36
|
let cancelled = false;
|
|
44
|
-
|
|
45
37
|
api
|
|
46
38
|
.get<AgentSessionStatus>(AGENT_ROUTES.sessionStatus(sessionId))
|
|
47
39
|
.then(({ data }) => {
|
|
48
40
|
if (!cancelled) setState((prev) => hydrateActivity(prev, data, sessionId));
|
|
49
41
|
})
|
|
50
42
|
.catch(() => {
|
|
51
|
-
//
|
|
43
|
+
// The topic still provides live progress; hydration is best-effort.
|
|
52
44
|
});
|
|
53
|
-
|
|
54
|
-
// The API's own token, refreshed when due (the session's base token is not, for this origin).
|
|
55
|
-
const linked = oxyServices.createLinkedClient({ baseURL: apiUrl });
|
|
56
|
-
const socket = io(apiUrl, {
|
|
57
|
-
transports: ['websocket'],
|
|
58
|
-
// The function form asks for a token on every (re)connect, so it survives rotation.
|
|
59
|
-
auth: (cb) => {
|
|
60
|
-
linked.getAccessToken(apiUrl).then(
|
|
61
|
-
(token) => cb({ token }),
|
|
62
|
-
() => cb({}),
|
|
63
|
-
);
|
|
64
|
-
},
|
|
65
|
-
reconnection: true,
|
|
66
|
-
reconnectionAttempts: Infinity,
|
|
67
|
-
reconnectionDelay: 1000,
|
|
68
|
-
reconnectionDelayMax: 10000,
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
socket.on('connect', () => {
|
|
72
|
-
socket.emit('subscribe-agent-session', sessionId);
|
|
73
|
-
if (agentId) socket.emit('subscribe-agent', agentId);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
socket.on('agent-activity', (event: AgentActivityEvent) => {
|
|
77
|
-
if (event.sessionId === sessionId) handleEvent(event);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
45
|
return () => {
|
|
81
46
|
cancelled = true;
|
|
82
|
-
socket.disconnect();
|
|
83
|
-
linked.dispose();
|
|
84
47
|
};
|
|
85
|
-
}, [sessionId,
|
|
48
|
+
}, [sessionId, api]);
|
|
49
|
+
|
|
50
|
+
const onFrame = useCallback(
|
|
51
|
+
(frame: RealtimeFrame) => {
|
|
52
|
+
const event = frame.data as AgentActivityEvent;
|
|
53
|
+
if (event?.sessionId === sessionId) setState((prev) => applyActivityEvent(prev, event));
|
|
54
|
+
},
|
|
55
|
+
[sessionId],
|
|
56
|
+
);
|
|
57
|
+
useRealtimeTopic(sessionId ? `agent-session:${sessionId}` : null, onFrame);
|
|
58
|
+
useRealtimeTopic(sessionId && agentId ? `agent:${agentId}` : null, onFrame);
|
|
86
59
|
|
|
87
60
|
return state;
|
|
88
61
|
}
|
|
@@ -47,6 +47,7 @@ import type {
|
|
|
47
47
|
ToolInvocation,
|
|
48
48
|
} from './types';
|
|
49
49
|
import { UsageLimitError } from './usage-limit-error';
|
|
50
|
+
import { useGemieRealtime } from '../realtime/react';
|
|
50
51
|
|
|
51
52
|
export type { ToolInvocation };
|
|
52
53
|
export type { FailedTurn };
|
|
@@ -181,6 +182,10 @@ export function useConversationStream({
|
|
|
181
182
|
room,
|
|
182
183
|
}: UseConversationStreamOptions = {}) {
|
|
183
184
|
const { apiUrl, streamFetch, host, app } = useGemieRuntime();
|
|
185
|
+
// Whether a member still speaking after the request can be followed live (`room` frames).
|
|
186
|
+
const realtime = useGemieRealtime();
|
|
187
|
+
const realtimeRef = useRef(realtime);
|
|
188
|
+
realtimeRef.current = realtime;
|
|
184
189
|
/** Where an awaited host action's answer goes: the same account and app assertion as the turn. */
|
|
185
190
|
const api = useGemieApi();
|
|
186
191
|
/** Read at send time and when an action arrives: the host's declaration may change between turns. */
|
|
@@ -677,8 +682,8 @@ export function useConversationStream({
|
|
|
677
682
|
flushPendingUpdates();
|
|
678
683
|
if (event.errorCode === ROOM_CONTINUES) {
|
|
679
684
|
// The request is over but the member is still speaking: the room's socket carries it on.
|
|
680
|
-
// With no socket to carry it, it stops here, unfinished but not failed.
|
|
681
|
-
if (
|
|
685
|
+
// With no socket to carry it (signed out), it stops here, unfinished but not failed.
|
|
686
|
+
if (realtimeRef.current) releaseConsult(roomRef.current, event.consultId);
|
|
682
687
|
else {
|
|
683
688
|
setMessages((prev) =>
|
|
684
689
|
prev.map((m) => (m.id === id ? { ...m, isStreaming: false } : m)),
|