@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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @gemies/sdk
|
|
2
2
|
|
|
3
|
+
## 6.0.0
|
|
4
|
+
|
|
5
|
+
- **Breaking: realtime over one socket ([Realtime](../../docs/realtime.mdx), Gemies ADR 0040).**
|
|
6
|
+
`GemieRuntimeProvider` holds the signed-in account's one Socket.IO connection (new entry
|
|
7
|
+
`@gemies/sdk/realtime`): reconnect with backoff, catch-up from the account's event cursor after a
|
|
8
|
+
reconnect (`rt:sync`), topics re-subscribed, every event applied once and in order.
|
|
9
|
+
- The runtime keeps itself live from it: conversation lists (a new message moves its row to the
|
|
10
|
+
top with its preview and `unreadCount`), an open conversation's messages (merged by id, never
|
|
11
|
+
doubled; a rewritten thread is read again), reactions, group rooms and an agent run's panel.
|
|
12
|
+
No list or detail is refetched after a turn any more.
|
|
13
|
+
- `GemieRuntimeHost` loses `subscribeConversationMessages`, `subscribeConversationReactions`
|
|
14
|
+
and `subscribeRoomEvents`: the SDK owns the connection.
|
|
15
|
+
- "Typing…": `GemieParticipants` shows what a gemie is doing in place of its name;
|
|
16
|
+
`GemieActivityLine`, `useConversationActivityText` and `useConversationActivity` draw it
|
|
17
|
+
anywhere. `useGemieActivity` hears the account's activity on the same connection (the
|
|
18
|
+
separate activity socket is gone, and so is `acquireActivitySocket`).
|
|
19
|
+
- `useAgentActivity` follows the run's topic on the shared connection instead of opening a
|
|
20
|
+
socket per run.
|
|
21
|
+
- Unread: `Conversation.unreadCount`; `useMarkConversationRead`, sent by an open conversation
|
|
22
|
+
when something arrives while it is in front.
|
|
23
|
+
- Hooks for a host's own data: `useGemieRealtime`, `useRealtimeEvent`, `useRealtimeReset`,
|
|
24
|
+
`useRealtimeTopic`.
|
|
25
|
+
- Copy: `activity(name, state)` in `GemieChatLabels` (English and Spanish).
|
|
26
|
+
|
|
3
27
|
## 5.0.0
|
|
4
28
|
|
|
5
29
|
- **The connectors store grows to hundreds of servers.** Beside the providers' servers Gemies curates,
|
package/README.md
CHANGED
|
@@ -359,6 +359,25 @@ transcript in the thread), `useThreadView` (the present or a moment of an agent'
|
|
|
359
359
|
An agent's thread and runs (`useThreadHistory`, `useThreadSearch`, `useThreadWindow`,
|
|
360
360
|
`useAgentActivity`) read the agent routes through the same API and cache.
|
|
361
361
|
|
|
362
|
+
### Realtime: one socket, kept by the provider
|
|
363
|
+
|
|
364
|
+
`GemieRuntimeProvider` holds the signed-in account's ONE Socket.IO connection to `apiUrl`
|
|
365
|
+
(`@gemies/sdk/realtime`, [Realtime](../../docs/realtime.mdx)). It keeps the runtime live by itself:
|
|
366
|
+
the conversation lists (a new message moves its row to the top with its unread count), an open
|
|
367
|
+
conversation's messages (merged by id, so nothing is doubled), reactions, group rooms, an agent
|
|
368
|
+
run's panel and "Pepe is typing…" (`GemieParticipants`, `GemieActivityLine`,
|
|
369
|
+
`useConversationActivity`). After a reconnect it catches up from its cursor; nothing polls.
|
|
370
|
+
|
|
371
|
+
A host listens for what is its own on the same connection:
|
|
372
|
+
|
|
373
|
+
```tsx
|
|
374
|
+
import { useRealtimeEvent, useRealtimeReset } from '@gemies/sdk/runtime';
|
|
375
|
+
|
|
376
|
+
useRealtimeEvent('credits', (frame) => setBalance(frame.data.credits));
|
|
377
|
+
useRealtimeEvent('notification', () => refetchNotifications());
|
|
378
|
+
useRealtimeReset(() => refetchEverythingYouShow()); // too far behind to catch up
|
|
379
|
+
```
|
|
380
|
+
|
|
362
381
|
A turn goes to `POST {apiUrl}/v1/chat/completions`; conversations to `{apiUrl}/v1/conversations`.
|
|
363
382
|
`apiUrl` defaults to `EXPO_PUBLIC_GEMIES_API_URL`, then `https://api.gemies.ai`. A failure to
|
|
364
383
|
answer is a typed error that keeps the person's turn in the thread with a retry (`FailedTurn`),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gemies/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "The Gemies SDK: the conversation runtime, chat UI, voice calls and dictation for Oxy apps that talk to a gemie",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"react-native": "src/index.ts",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"./csp": "./src/csp.ts",
|
|
20
20
|
"./dictation": "./src/dictation.ts",
|
|
21
21
|
"./model": "./src/runtime/model.ts",
|
|
22
|
+
"./realtime": "./src/realtime/index.ts",
|
|
22
23
|
"./runtime": "./src/runtime/index.ts",
|
|
23
24
|
"./text": "./src/text.ts",
|
|
24
25
|
"./tools": "./src/tools.ts",
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { memo } from 'react';
|
|
2
|
+
import { Text } from 'react-native';
|
|
3
|
+
import { useConversationActivityText } from '../realtime/activity-text';
|
|
4
|
+
|
|
5
|
+
export { activityText, useConversationActivityText } from '../realtime/activity-text';
|
|
6
|
+
|
|
7
|
+
export interface GemieActivityLineProps {
|
|
8
|
+
conversationId: string | null | undefined;
|
|
9
|
+
/** Classes for the line (NativeWind); a small muted line by default. */
|
|
10
|
+
className?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* WhatsApp's line under a chat's name: "Pepe is typing…", "Alia is thinking…", live from the
|
|
15
|
+
* account's realtime connection. Draws nothing while nobody is doing anything.
|
|
16
|
+
*/
|
|
17
|
+
export const GemieActivityLine = memo(function GemieActivityLine({
|
|
18
|
+
conversationId,
|
|
19
|
+
className = 'text-xs text-primary',
|
|
20
|
+
}: GemieActivityLineProps) {
|
|
21
|
+
const text = useConversationActivityText(conversationId);
|
|
22
|
+
if (!text) return null;
|
|
23
|
+
return (
|
|
24
|
+
<Text className={className} numberOfLines={1} accessibilityLiveRegion="polite">
|
|
25
|
+
{text}
|
|
26
|
+
</Text>
|
|
27
|
+
);
|
|
28
|
+
});
|
|
@@ -420,8 +420,11 @@ function GemieConversationView({
|
|
|
420
420
|
[agentId, group],
|
|
421
421
|
);
|
|
422
422
|
const participants = useMemo(
|
|
423
|
-
() => (
|
|
424
|
-
|
|
423
|
+
() => ({
|
|
424
|
+
...(group === undefined ? consulted : groupParticipants(group)),
|
|
425
|
+
...(conversationId === undefined ? {} : { conversationId }),
|
|
426
|
+
}),
|
|
427
|
+
[group, consulted, conversationId],
|
|
425
428
|
);
|
|
426
429
|
const asked = usePendingQuestions(messages, isLoading);
|
|
427
430
|
const gemieId = gemieThread?.gemieId;
|
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
consultedAgents,
|
|
33
33
|
} from '../lib/participants';
|
|
34
34
|
import type { Message } from '../runtime/types';
|
|
35
|
+
import { useConversationActivityText } from '../realtime/activity-text';
|
|
35
36
|
|
|
36
37
|
export interface UseParticipantsOptions {
|
|
37
38
|
lead: GemieLead;
|
|
@@ -113,6 +114,8 @@ export const GemieParticipants = memo(function GemieParticipants({
|
|
|
113
114
|
onPress,
|
|
114
115
|
}: GemieParticipantsProps) {
|
|
115
116
|
const flights = useGemieFlights();
|
|
117
|
+
// WhatsApp's header: the names give way to what one of them is doing, while they do it.
|
|
118
|
+
const activity = useConversationActivityText(participants.conversationId);
|
|
116
119
|
const agents = useMemo(
|
|
117
120
|
() =>
|
|
118
121
|
[participants.lead, ...participants.joined].map((agent) => ({
|
|
@@ -125,6 +128,7 @@ export const GemieParticipants = memo(function GemieParticipants({
|
|
|
125
128
|
return (
|
|
126
129
|
<AgentProfile
|
|
127
130
|
agents={agents}
|
|
131
|
+
{...(activity === null ? {} : { label: activity })}
|
|
128
132
|
onPress={onPress ?? noop}
|
|
129
133
|
avatarRef={flights?.anchor(ANCHORS.profile)}
|
|
130
134
|
avatarHidden={flights?.isLanding(ANCHORS.profile)}
|
|
@@ -1,39 +1,44 @@
|
|
|
1
|
-
import { useEffect } from 'react';
|
|
2
|
-
import { useOxy } from '@oxy.so/services/ui/client';
|
|
3
1
|
import type { GemieAvatarState } from '../components/GemieAvatar';
|
|
4
2
|
import { selectGemieActivity, useGemieActivityStore } from '../lib/activity-store';
|
|
5
|
-
import { acquireActivitySocket } from '../lib/activity-socket';
|
|
6
3
|
import { GEMIES_API_URL } from '../lib/origins';
|
|
4
|
+
import type { ActorActivityEntry } from '../realtime/activity';
|
|
5
|
+
import { useRealtimeActivityStore } from '../realtime/activity';
|
|
6
|
+
import { useRealtimeActivityFeed } from '../realtime/react';
|
|
7
7
|
|
|
8
8
|
export interface UseGemieActivityOptions {
|
|
9
|
-
/** The API
|
|
9
|
+
/** The API to hear from when no `GemieRuntimeProvider` is mounted. Defaults to Gemies's. */
|
|
10
10
|
apiUrl?: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/** A tool in use is the avatar's `working`; the other states are its own. */
|
|
14
|
+
const AVATAR_STATE: Record<ActorActivityEntry['state'], GemieAvatarState> = {
|
|
15
|
+
thinking: 'thinking',
|
|
16
|
+
writing: 'writing',
|
|
17
|
+
tool: 'working',
|
|
18
|
+
};
|
|
19
|
+
const RANK: Record<GemieAvatarState, number> = { idle: 0, writing: 1, thinking: 2, working: 3 };
|
|
20
|
+
|
|
21
|
+
function busiestRemote(entries: Readonly<Record<string, ActorActivityEntry>>): GemieAvatarState {
|
|
22
|
+
let top: GemieAvatarState = 'idle';
|
|
23
|
+
for (const entry of Object.values(entries)) {
|
|
24
|
+
const state = AVATAR_STATE[entry.state];
|
|
25
|
+
if (RANK[state] > RANK[top]) top = state;
|
|
26
|
+
}
|
|
27
|
+
return top;
|
|
28
|
+
}
|
|
29
|
+
|
|
13
30
|
/**
|
|
14
31
|
* What Alia is doing right now, for any surface that wants to show it — an "Ask Alia" button's
|
|
15
32
|
* avatar, a status dot. It is the busiest of:
|
|
16
33
|
*
|
|
17
34
|
* - a turn running in this app (`useConversationThread`, so every chat drives every button), and
|
|
18
|
-
* -
|
|
19
|
-
*
|
|
35
|
+
* - what the signed-in person's gemies are doing elsewhere (background work, another device), heard
|
|
36
|
+
* as `activity` on the account's realtime connection: the runtime provider's, or one held here
|
|
37
|
+
* while signed in when the button is drawn outside any.
|
|
20
38
|
*/
|
|
21
39
|
export function useGemieActivity(options: UseGemieActivityOptions = {}): GemieAvatarState {
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (!isAuthenticated) return;
|
|
27
|
-
// The API's own token, refreshed by core when it is due, as every other request gets it. The
|
|
28
|
-
// session's base `accessToken` is not refreshed for this origin: once it expired, the server
|
|
29
|
-
// refused every handshake and the socket retried forever.
|
|
30
|
-
const linked = oxyServices.createLinkedClient({ baseURL: apiUrl });
|
|
31
|
-
const release = acquireActivitySocket(apiUrl, () => linked.getAccessToken(apiUrl));
|
|
32
|
-
return () => {
|
|
33
|
-
release();
|
|
34
|
-
linked.dispose();
|
|
35
|
-
};
|
|
36
|
-
}, [apiUrl, isAuthenticated, oxyServices]);
|
|
37
|
-
|
|
38
|
-
return useGemieActivityStore(selectGemieActivity);
|
|
40
|
+
useRealtimeActivityFeed(options.apiUrl ?? GEMIES_API_URL);
|
|
41
|
+
const local = useGemieActivityStore(selectGemieActivity);
|
|
42
|
+
const remote = useRealtimeActivityStore((s) => busiestRemote(s.entries));
|
|
43
|
+
return RANK[remote] > RANK[local] ? remote : local;
|
|
39
44
|
}
|
|
@@ -4,7 +4,6 @@ import { useQueryClient } from '@tanstack/react-query';
|
|
|
4
4
|
import { useCallback, useEffect, useMemo, useReducer, useRef } from 'react';
|
|
5
5
|
import { useLabels } from '../lib/labels';
|
|
6
6
|
import { useGemieApi } from '../runtime/api';
|
|
7
|
-
import { useGemieRuntime } from '../runtime/config';
|
|
8
7
|
import { GEMIES_API_ROUTES } from '../runtime/model';
|
|
9
8
|
import {
|
|
10
9
|
MessageReactionController,
|
|
@@ -13,6 +12,7 @@ import {
|
|
|
13
12
|
type MessageReactionSnapshot,
|
|
14
13
|
} from '../runtime/message-reactions';
|
|
15
14
|
import { updateReactionCaches } from '../runtime/reaction-cache';
|
|
15
|
+
import { useGemieRealtime } from '../realtime/react';
|
|
16
16
|
|
|
17
17
|
const NONE: readonly string[] = [];
|
|
18
18
|
|
|
@@ -28,7 +28,6 @@ export function useMessageReactions({
|
|
|
28
28
|
} = {}) {
|
|
29
29
|
const { t } = useLabels();
|
|
30
30
|
const api = useGemieApi();
|
|
31
|
-
const { host } = useGemieRuntime();
|
|
32
31
|
const { user } = useOxy();
|
|
33
32
|
const queryClient = useQueryClient();
|
|
34
33
|
const [version, changed] = useReducer((value: number) => value + 1, 0);
|
|
@@ -68,21 +67,17 @@ export function useMessageReactions({
|
|
|
68
67
|
return () => controller.dispose();
|
|
69
68
|
}, [controller]);
|
|
70
69
|
|
|
70
|
+
// Reactions changed anywhere — another device, a gemie — arrive as `message.reaction`.
|
|
71
71
|
const subscriptions = JSON.stringify([...new Set(conversationIds)].sort());
|
|
72
|
-
const
|
|
72
|
+
const realtime = useGemieRealtime();
|
|
73
73
|
useEffect(() => {
|
|
74
|
-
if (!enabled || !
|
|
75
|
-
const ids
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
);
|
|
82
|
-
return () => {
|
|
83
|
-
for (const unsubscribe of unsubscribers) unsubscribe();
|
|
84
|
-
};
|
|
85
|
-
}, [controller, enabled, subscribe, subscriptions]);
|
|
74
|
+
if (!enabled || !realtime) return;
|
|
75
|
+
const ids = new Set<string>(JSON.parse(subscriptions));
|
|
76
|
+
return realtime.on('message.reaction', (frame) => {
|
|
77
|
+
const update = readMessageReactionUpdate(frame.data);
|
|
78
|
+
if (update && ids.has(update.conversationId)) controller.receive(update);
|
|
79
|
+
});
|
|
80
|
+
}, [controller, enabled, realtime, subscriptions]);
|
|
86
81
|
|
|
87
82
|
// biome-ignore lint/correctness/useExhaustiveDependencies: each update refreshes memoized rows
|
|
88
83
|
const reactionsOf = useCallback(
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,12 @@ export type {
|
|
|
16
16
|
UseParticipantsOptions,
|
|
17
17
|
} from './components/GemieParticipants';
|
|
18
18
|
export { GemieParticipantsHeader } from './components/GemieParticipantsHeader';
|
|
19
|
+
export {
|
|
20
|
+
GemieActivityLine,
|
|
21
|
+
activityText,
|
|
22
|
+
useConversationActivityText,
|
|
23
|
+
} from './components/GemieActivityLine';
|
|
24
|
+
export type { GemieActivityLineProps } from './components/GemieActivityLine';
|
|
19
25
|
export type { GemieParticipantsHeaderProps } from './components/GemieParticipantsHeader';
|
|
20
26
|
export { ALIA_GEMIE_ID } from './lib/alia-gemie';
|
|
21
27
|
export { ALIA_LEAD, leadOf } from './lib/participants';
|
|
@@ -4,20 +4,14 @@ import type { GemieAvatarState } from '../components/GemieAvatar';
|
|
|
4
4
|
type Sources = Readonly<Record<string, GemieAvatarState>>;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* reacts to work that is not in any open chat.
|
|
12
|
-
*
|
|
13
|
-
* An idle source holds no entry, so neither map grows with use.
|
|
7
|
+
* The turns running in THIS app, one entry per `useConversationThread` instance, so every chat
|
|
8
|
+
* drives every "Ask Alia" button. What the person's gemies do elsewhere is the realtime activity
|
|
9
|
+
* store's (`realtime/activity.ts`); `useGemieActivity` shows the busier of the two. An idle source
|
|
10
|
+
* holds no entry, so the map does not grow with use.
|
|
14
11
|
*/
|
|
15
12
|
interface GemieActivityStore {
|
|
16
13
|
local: Sources;
|
|
17
|
-
remote: Sources;
|
|
18
14
|
setLocal(source: string, state: GemieAvatarState): void;
|
|
19
|
-
setRemote(session: string, state: GemieAvatarState): void;
|
|
20
|
-
clearRemote(): void;
|
|
21
15
|
}
|
|
22
16
|
|
|
23
17
|
function put(map: Sources, key: string, state: GemieAvatarState): Sources {
|
|
@@ -31,10 +25,7 @@ function put(map: Sources, key: string, state: GemieAvatarState): Sources {
|
|
|
31
25
|
|
|
32
26
|
export const useGemieActivityStore = create<GemieActivityStore>((set) => ({
|
|
33
27
|
local: {},
|
|
34
|
-
remote: {},
|
|
35
28
|
setLocal: (source, state) => set((s) => ({ local: put(s.local, source, state) })),
|
|
36
|
-
setRemote: (session, state) => set((s) => ({ remote: put(s.remote, session, state) })),
|
|
37
|
-
clearRemote: () => set((s) => (Object.keys(s.remote).length === 0 ? s : { remote: {} })),
|
|
38
29
|
}));
|
|
39
30
|
|
|
40
31
|
/** The most telling thing any source is doing: a tool call outranks thinking, thinking outranks writing. */
|
|
@@ -48,6 +39,5 @@ export function busiest(...maps: ReadonlyArray<Sources>): GemieAvatarState {
|
|
|
48
39
|
return top;
|
|
49
40
|
}
|
|
50
41
|
|
|
51
|
-
/** A primitive, so a subscriber re-renders when the state changes and not when a
|
|
52
|
-
export const selectGemieActivity = (s: GemieActivityStore): GemieAvatarState =>
|
|
53
|
-
busiest(s.local, s.remote);
|
|
42
|
+
/** A primitive, so a subscriber re-renders when the state changes and not when a source does. */
|
|
43
|
+
export const selectGemieActivity = (s: GemieActivityStore): GemieAvatarState => busiest(s.local);
|
package/src/lib/labels-es.ts
CHANGED
|
@@ -75,6 +75,16 @@ export const SPANISH_LABELS: GemieChatLabels = {
|
|
|
75
75
|
reasoning: 'Razonamiento',
|
|
76
76
|
locale: 'es',
|
|
77
77
|
newChat: 'Nueva conversación',
|
|
78
|
+
activity: (name, state) => {
|
|
79
|
+
const verb =
|
|
80
|
+
state === 'writing'
|
|
81
|
+
? 'está escribiendo'
|
|
82
|
+
: state === 'tool'
|
|
83
|
+
? 'está trabajando'
|
|
84
|
+
: 'está pensando';
|
|
85
|
+
const bare = state === 'writing' ? 'Escribiendo' : state === 'tool' ? 'Trabajando' : 'Pensando';
|
|
86
|
+
return name ? `${name} ${verb}…` : `${bare}…`;
|
|
87
|
+
},
|
|
78
88
|
recentConversations: 'Recientes',
|
|
79
89
|
sendFailed:
|
|
80
90
|
'No se pudo enviar el mensaje. Lo tienes de vuelta en el cuadro de texto, inténtalo otra vez.',
|
package/src/lib/labels.tsx
CHANGED
|
@@ -88,6 +88,11 @@ export interface GemieChatLabels {
|
|
|
88
88
|
// Conversations (the runtime's own words: toasts and the errors it raises)
|
|
89
89
|
/** A conversation's title until it has one. */
|
|
90
90
|
newChat: string;
|
|
91
|
+
/**
|
|
92
|
+
* What a gemie is doing, live, as the header and a sidebar row say it ("Pepe is typing…"):
|
|
93
|
+
* `name` is the gemie's, `null` for one whose name the event did not carry.
|
|
94
|
+
*/
|
|
95
|
+
activity: (name: string | null, state: 'thinking' | 'writing' | 'tool') => string;
|
|
91
96
|
/** Over the sheet's recent conversations with its lead. */
|
|
92
97
|
recentConversations: string;
|
|
93
98
|
/** A turn could not be sent and the composer got its text back. */
|
|
@@ -168,6 +173,12 @@ export const DEFAULT_LABELS: GemieChatLabels = {
|
|
|
168
173
|
t: englishCopy,
|
|
169
174
|
locale: 'en',
|
|
170
175
|
newChat: 'New chat',
|
|
176
|
+
activity: (name, state) => {
|
|
177
|
+
const verb =
|
|
178
|
+
state === 'writing' ? 'is typing' : state === 'tool' ? 'is working' : 'is thinking';
|
|
179
|
+
const bare = state === 'writing' ? 'Typing' : state === 'tool' ? 'Working' : 'Thinking';
|
|
180
|
+
return name ? `${name} ${verb}…` : `${bare}…`;
|
|
181
|
+
},
|
|
171
182
|
recentConversations: 'Recent',
|
|
172
183
|
sendFailed: "Your message couldn't be sent",
|
|
173
184
|
signInRequired: 'Sign in to keep chatting',
|
package/src/lib/participants.ts
CHANGED
|
@@ -30,6 +30,11 @@ export interface GemieChatParticipants {
|
|
|
30
30
|
lead: GemieLead;
|
|
31
31
|
/** The consulted agents that have joined the header, in the order they joined. */
|
|
32
32
|
joined: GemieParticipant[];
|
|
33
|
+
/**
|
|
34
|
+
* The conversation they are in, so the header can say live what one of them is doing
|
|
35
|
+
* ("Pepe is typing…") in place of their names. Absent before the first turn creates one.
|
|
36
|
+
*/
|
|
37
|
+
conversationId?: string;
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
/**
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useLabels } from '../lib/labels';
|
|
2
|
+
import { type ActorActivityEntry, useConversationActivity } from './activity';
|
|
3
|
+
|
|
4
|
+
/** The words for what the busiest actor is doing, or `null` when nobody is doing anything. */
|
|
5
|
+
export function useConversationActivityText(
|
|
6
|
+
conversationId: string | null | undefined,
|
|
7
|
+
): string | null {
|
|
8
|
+
const activity = useConversationActivity(conversationId);
|
|
9
|
+
const labels = useLabels();
|
|
10
|
+
return activityText(activity, labels.activity);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const RANK: Record<ActorActivityEntry['state'], number> = { thinking: 1, writing: 2, tool: 3 };
|
|
14
|
+
|
|
15
|
+
/** The busiest entry, said once: "Pepe is typing…" (one), "Pepe is typing…" for the first of several. */
|
|
16
|
+
export function activityText(
|
|
17
|
+
activity: readonly ActorActivityEntry[],
|
|
18
|
+
say: (name: string | null, state: ActorActivityEntry['state']) => string,
|
|
19
|
+
): string | null {
|
|
20
|
+
let top: ActorActivityEntry | undefined;
|
|
21
|
+
for (const entry of activity) if (!top || RANK[entry.state] > RANK[top.state]) top = entry;
|
|
22
|
+
return top ? say(top.name, top.state) : null;
|
|
23
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who is doing what, live: WhatsApp's "typing…" for gemies (`activity` frames, `docs/realtime.mdx`).
|
|
3
|
+
*
|
|
4
|
+
* One entry per actor per place (a conversation, or an agent session with none). An entry lives
|
|
5
|
+
* for the `ttlMs` its frame states and is renewed by the server's heartbeat; `idle` removes it at
|
|
6
|
+
* once, and so does a lost connection — a stale "typing…" is worse than none, and a reconnect hears
|
|
7
|
+
* the truth within one heartbeat.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { create } from 'zustand';
|
|
11
|
+
import { useShallow } from 'zustand/react/shallow';
|
|
12
|
+
import type { ActivityState, RealtimeActivity } from './contract';
|
|
13
|
+
|
|
14
|
+
export interface ActorActivityEntry {
|
|
15
|
+
readonly conversationId: string | null;
|
|
16
|
+
readonly sessionId: string | null;
|
|
17
|
+
readonly agentId: string | null;
|
|
18
|
+
readonly name: string | null;
|
|
19
|
+
readonly state: Exclude<ActivityState, 'idle'>;
|
|
20
|
+
readonly tool: string | null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface RealtimeActivityStore {
|
|
24
|
+
readonly entries: Readonly<Record<string, ActorActivityEntry>>;
|
|
25
|
+
apply(activity: RealtimeActivity): void;
|
|
26
|
+
clear(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const timers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
30
|
+
|
|
31
|
+
function keyOf(activity: RealtimeActivity): string {
|
|
32
|
+
const place = activity.conversationId ?? `session:${activity.sessionId ?? ''}`;
|
|
33
|
+
return `${place}|${activity.actor.agentId ?? 'alia'}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function without(
|
|
37
|
+
entries: Readonly<Record<string, ActorActivityEntry>>,
|
|
38
|
+
key: string,
|
|
39
|
+
): Readonly<Record<string, ActorActivityEntry>> {
|
|
40
|
+
if (!(key in entries)) return entries;
|
|
41
|
+
const { [key]: _gone, ...rest } = entries;
|
|
42
|
+
return rest;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const useRealtimeActivityStore = create<RealtimeActivityStore>((set) => ({
|
|
46
|
+
entries: {},
|
|
47
|
+
apply(activity) {
|
|
48
|
+
const key = keyOf(activity);
|
|
49
|
+
const pending = timers.get(key);
|
|
50
|
+
if (pending !== undefined) clearTimeout(pending);
|
|
51
|
+
timers.delete(key);
|
|
52
|
+
if (activity.state === 'idle') {
|
|
53
|
+
set((s) => ({ entries: without(s.entries, key) }));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const entry: ActorActivityEntry = {
|
|
57
|
+
conversationId: activity.conversationId,
|
|
58
|
+
sessionId: activity.sessionId ?? null,
|
|
59
|
+
agentId: activity.actor.agentId,
|
|
60
|
+
name: activity.actor.name ?? null,
|
|
61
|
+
state: activity.state,
|
|
62
|
+
tool: activity.tool ?? null,
|
|
63
|
+
};
|
|
64
|
+
set((s) => ({ entries: { ...s.entries, [key]: entry } }));
|
|
65
|
+
timers.set(
|
|
66
|
+
key,
|
|
67
|
+
setTimeout(
|
|
68
|
+
() => {
|
|
69
|
+
timers.delete(key);
|
|
70
|
+
set((s) => ({ entries: without(s.entries, key) }));
|
|
71
|
+
},
|
|
72
|
+
Math.max(1_000, activity.ttlMs),
|
|
73
|
+
),
|
|
74
|
+
);
|
|
75
|
+
},
|
|
76
|
+
clear() {
|
|
77
|
+
for (const timer of timers.values()) clearTimeout(timer);
|
|
78
|
+
timers.clear();
|
|
79
|
+
set((s) => (Object.keys(s.entries).length === 0 ? s : { entries: {} }));
|
|
80
|
+
},
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
/** Who is active in one conversation. Use with `useShallow`, so it re-renders on a change only. */
|
|
84
|
+
export function selectConversationActivity(conversationId: string | null | undefined) {
|
|
85
|
+
return (s: RealtimeActivityStore): ActorActivityEntry[] =>
|
|
86
|
+
conversationId
|
|
87
|
+
? Object.values(s.entries).filter((entry) => entry.conversationId === conversationId)
|
|
88
|
+
: [];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Who is doing what in one conversation right now: "Pepe is typing…", "Alia is thinking…". */
|
|
92
|
+
export function useConversationActivity(
|
|
93
|
+
conversationId: string | null | undefined,
|
|
94
|
+
): readonly ActorActivityEntry[] {
|
|
95
|
+
return useRealtimeActivityStore(useShallow(selectConversationActivity(conversationId)));
|
|
96
|
+
}
|