@gemies/sdk 5.0.0 → 6.1.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 +34 -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 +252 -0
- package/src/realtime/connection.ts +317 -0
- package/src/realtime/contract.ts +169 -0
- package/src/realtime/index.ts +33 -0
- package/src/realtime/react.tsx +196 -0
- package/src/runtime/collections-model.ts +66 -0
- package/src/runtime/collections.ts +185 -0
- package/src/runtime/config.tsx +8 -26
- package/src/runtime/conversations.ts +55 -3
- package/src/runtime/index.ts +21 -0
- package/src/runtime/query-keys.ts +2 -0
- package/src/runtime/routes.ts +13 -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
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { io, type Socket } from 'socket.io-client';
|
|
2
|
-
import type { GemieAvatarState } from '../components/GemieAvatar';
|
|
3
|
-
import { useGemieActivityStore } from './activity-store';
|
|
4
|
-
|
|
5
|
-
/** A run that goes quiet this long is over: a crashed run never says `idle`. */
|
|
6
|
-
export const ACTIVITY_QUIET_MS = 90_000;
|
|
7
|
-
/**
|
|
8
|
-
* After the server refuses a handshake (socket.io does not retry those itself), wait this long,
|
|
9
|
-
* doubling up to {@link MAX_RETRY_MS}. A fixed 5 s retry with a token that could never pass (the
|
|
10
|
-
* session's base token after it expired) opened a websocket every few seconds for as long as the
|
|
11
|
-
* tab stayed open: 51 upgrades in 12 minutes for one person, on top of their API budget.
|
|
12
|
-
*/
|
|
13
|
-
export const RETRY_MS = 5_000;
|
|
14
|
-
export const MAX_RETRY_MS = 60_000;
|
|
15
|
-
|
|
16
|
-
/** A holder's way to the API's access token: fresh on every call (a linked client's). */
|
|
17
|
-
export type ActivityTokenSource = () => string | null | Promise<string | null>;
|
|
18
|
-
|
|
19
|
-
const STATES: ReadonlySet<string> = new Set<GemieAvatarState>([
|
|
20
|
-
'idle',
|
|
21
|
-
'thinking',
|
|
22
|
-
'working',
|
|
23
|
-
'writing',
|
|
24
|
-
]);
|
|
25
|
-
|
|
26
|
-
interface Connection {
|
|
27
|
-
socket: Socket;
|
|
28
|
-
refs: number;
|
|
29
|
-
quiet: Map<string, ReturnType<typeof setTimeout>>;
|
|
30
|
-
retry: ReturnType<typeof setTimeout> | null;
|
|
31
|
-
retryMs: number;
|
|
32
|
-
/** Each holder's token source; the socket asks the latest one still held. */
|
|
33
|
-
tokens: Map<symbol, ActivityTokenSource>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** One connection per API origin, however many buttons ask for it. */
|
|
37
|
-
const connections = new Map<string, Connection>();
|
|
38
|
-
|
|
39
|
-
function open(url: string): Connection {
|
|
40
|
-
const tokens = new Map<symbol, ActivityTokenSource>();
|
|
41
|
-
const socket = io(url, {
|
|
42
|
-
transports: ['websocket'],
|
|
43
|
-
// Asked on every (re)connect, of a holder still holding it, so the token that goes out is a
|
|
44
|
-
// current one: never a captured one, never one whose holder has gone.
|
|
45
|
-
auth: (cb) => {
|
|
46
|
-
const source = [...tokens.values()].at(-1);
|
|
47
|
-
Promise.resolve(source ? source() : null).then(
|
|
48
|
-
(token) => cb({ token }),
|
|
49
|
-
() => cb({}),
|
|
50
|
-
);
|
|
51
|
-
},
|
|
52
|
-
reconnection: true,
|
|
53
|
-
reconnectionAttempts: Infinity,
|
|
54
|
-
reconnectionDelay: 1000,
|
|
55
|
-
reconnectionDelayMax: 10_000,
|
|
56
|
-
});
|
|
57
|
-
const connection: Connection = {
|
|
58
|
-
socket,
|
|
59
|
-
refs: 0,
|
|
60
|
-
quiet: new Map(),
|
|
61
|
-
retry: null,
|
|
62
|
-
retryMs: RETRY_MS,
|
|
63
|
-
tokens,
|
|
64
|
-
};
|
|
65
|
-
const store = useGemieActivityStore.getState;
|
|
66
|
-
|
|
67
|
-
const reset = (): void => {
|
|
68
|
-
for (const timer of connection.quiet.values()) clearTimeout(timer);
|
|
69
|
-
connection.quiet.clear();
|
|
70
|
-
store().clearRemote();
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
socket.on('connect', () => {
|
|
74
|
-
connection.retryMs = RETRY_MS;
|
|
75
|
-
socket.emit('subscribe-alia-activity');
|
|
76
|
-
});
|
|
77
|
-
// What happened while disconnected is unknown; a stale "working" is worse than idle.
|
|
78
|
-
socket.on('disconnect', reset);
|
|
79
|
-
socket.on('connect_error', () => {
|
|
80
|
-
// A refused handshake (no token yet, an expired one) ends the socket's own retrying.
|
|
81
|
-
if (socket.active || connection.retry !== null) return;
|
|
82
|
-
const wait = connection.retryMs;
|
|
83
|
-
connection.retryMs = Math.min(wait * 2, MAX_RETRY_MS);
|
|
84
|
-
connection.retry = setTimeout(() => {
|
|
85
|
-
connection.retry = null;
|
|
86
|
-
socket.connect();
|
|
87
|
-
}, wait);
|
|
88
|
-
});
|
|
89
|
-
socket.on('alia:activity', (event: unknown) => {
|
|
90
|
-
if (typeof event !== 'object' || event === null) return;
|
|
91
|
-
const { state, sessionId } = event as { state?: unknown; sessionId?: unknown };
|
|
92
|
-
if (typeof sessionId !== 'string' || typeof state !== 'string' || !STATES.has(state)) return;
|
|
93
|
-
const pending = connection.quiet.get(sessionId);
|
|
94
|
-
if (pending !== undefined) clearTimeout(pending);
|
|
95
|
-
connection.quiet.delete(sessionId);
|
|
96
|
-
store().setRemote(sessionId, state as GemieAvatarState);
|
|
97
|
-
if (state !== 'idle') {
|
|
98
|
-
connection.quiet.set(
|
|
99
|
-
sessionId,
|
|
100
|
-
setTimeout(() => {
|
|
101
|
-
connection.quiet.delete(sessionId);
|
|
102
|
-
store().setRemote(sessionId, 'idle');
|
|
103
|
-
}, ACTIVITY_QUIET_MS),
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
return connection;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Listen for Alia's activity on the person's behalf. Every caller shares the one connection for
|
|
112
|
-
* `url`; the last to release closes it. Returns the release.
|
|
113
|
-
*/
|
|
114
|
-
export function acquireActivitySocket(url: string, getToken: ActivityTokenSource): () => void {
|
|
115
|
-
let held = connections.get(url);
|
|
116
|
-
if (!held) {
|
|
117
|
-
held = open(url);
|
|
118
|
-
connections.set(url, held);
|
|
119
|
-
}
|
|
120
|
-
const connection = held;
|
|
121
|
-
const holder = Symbol('activity-holder');
|
|
122
|
-
connection.tokens.set(holder, getToken);
|
|
123
|
-
connection.refs++;
|
|
124
|
-
let released = false;
|
|
125
|
-
return () => {
|
|
126
|
-
if (released) return;
|
|
127
|
-
released = true;
|
|
128
|
-
connection.tokens.delete(holder);
|
|
129
|
-
if (--connection.refs > 0) return;
|
|
130
|
-
connections.delete(url);
|
|
131
|
-
if (connection.retry !== null) clearTimeout(connection.retry);
|
|
132
|
-
connection.socket.removeAllListeners();
|
|
133
|
-
connection.socket.disconnect();
|
|
134
|
-
for (const timer of connection.quiet.values()) clearTimeout(timer);
|
|
135
|
-
useGemieActivityStore.getState().clearRemote();
|
|
136
|
-
};
|
|
137
|
-
}
|