@gemies/sdk 3.3.0 → 3.3.1

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 CHANGED
@@ -1,5 +1,16 @@
1
1
  # @gemies/sdk
2
2
 
3
+ ## 3.3.1
4
+
5
+ - **The activity socket stops reconnecting every few seconds.** `useGemieActivity` (every Alia
6
+ button's avatar) and `useAgentActivity` authenticated their sockets with the session's base
7
+ `accessToken`, which is not refreshed for the Gemies API; once it expired the server refused
8
+ every handshake ("Token expired") and the shared socket opened a new websocket every 5 s for as
9
+ long as the tab stayed open (51 upgrades in 12 minutes for one person). Both now ask a linked
10
+ client for the API's own token on every handshake, refreshed by `@oxy.so/core` when due, as
11
+ every other request does; the shared socket asks a holder still holding it. A refused handshake
12
+ is retried after 5 s, doubling to at most a minute, and the next outage starts from 5 s again.
13
+
3
14
  ## 3.3.0
4
15
 
5
16
  - **New: `@gemies/sdk/connectors`**, the connectors a person connects — providers' own remote MCP
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gemies/sdk",
3
- "version": "3.3.0",
3
+ "version": "3.3.1",
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",
@@ -24,7 +24,15 @@ export function useGemieActivity(options: UseGemieActivityOptions = {}): GemieAv
24
24
 
25
25
  useEffect(() => {
26
26
  if (!isAuthenticated) return;
27
- return acquireActivitySocket(apiUrl, () => oxyServices.session.accessToken);
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
+ };
28
36
  }, [apiUrl, isAuthenticated, oxyServices]);
29
37
 
30
38
  return useGemieActivityStore(selectGemieActivity);
@@ -4,8 +4,17 @@ import { useGemieActivityStore } from './activity-store';
4
4
 
5
5
  /** A run that goes quiet this long is over: a crashed run never says `idle`. */
6
6
  export const ACTIVITY_QUIET_MS = 90_000;
7
- /** Between tries after the server refuses a handshake (socket.io does not retry those itself). */
8
- const RETRY_MS = 5_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>;
9
18
 
10
19
  const STATES: ReadonlySet<string> = new Set<GemieAvatarState>([
11
20
  'idle',
@@ -19,22 +28,40 @@ interface Connection {
19
28
  refs: number;
20
29
  quiet: Map<string, ReturnType<typeof setTimeout>>;
21
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>;
22
34
  }
23
35
 
24
36
  /** One connection per API origin, however many buttons ask for it. */
25
37
  const connections = new Map<string, Connection>();
26
38
 
27
- function open(url: string, getToken: () => string | null): Connection {
39
+ function open(url: string): Connection {
40
+ const tokens = new Map<symbol, ActivityTokenSource>();
28
41
  const socket = io(url, {
29
42
  transports: ['websocket'],
30
- // Read on every (re)connect, so a rotated access token is the one that goes out.
31
- auth: (cb) => cb({ token: getToken() }),
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
+ },
32
52
  reconnection: true,
33
53
  reconnectionAttempts: Infinity,
34
54
  reconnectionDelay: 1000,
35
55
  reconnectionDelayMax: 10_000,
36
56
  });
37
- const connection: Connection = { socket, refs: 0, quiet: new Map(), retry: null };
57
+ const connection: Connection = {
58
+ socket,
59
+ refs: 0,
60
+ quiet: new Map(),
61
+ retry: null,
62
+ retryMs: RETRY_MS,
63
+ tokens,
64
+ };
38
65
  const store = useGemieActivityStore.getState;
39
66
 
40
67
  const reset = (): void => {
@@ -43,16 +70,21 @@ function open(url: string, getToken: () => string | null): Connection {
43
70
  store().clearRemote();
44
71
  };
45
72
 
46
- socket.on('connect', () => socket.emit('subscribe-alia-activity'));
73
+ socket.on('connect', () => {
74
+ connection.retryMs = RETRY_MS;
75
+ socket.emit('subscribe-alia-activity');
76
+ });
47
77
  // What happened while disconnected is unknown; a stale "working" is worse than idle.
48
78
  socket.on('disconnect', reset);
49
79
  socket.on('connect_error', () => {
50
80
  // A refused handshake (no token yet, an expired one) ends the socket's own retrying.
51
81
  if (socket.active || connection.retry !== null) return;
82
+ const wait = connection.retryMs;
83
+ connection.retryMs = Math.min(wait * 2, MAX_RETRY_MS);
52
84
  connection.retry = setTimeout(() => {
53
85
  connection.retry = null;
54
86
  socket.connect();
55
- }, RETRY_MS);
87
+ }, wait);
56
88
  });
57
89
  socket.on('alia:activity', (event: unknown) => {
58
90
  if (typeof event !== 'object' || event === null) return;
@@ -79,18 +111,21 @@ function open(url: string, getToken: () => string | null): Connection {
79
111
  * Listen for Alia's activity on the person's behalf. Every caller shares the one connection for
80
112
  * `url`; the last to release closes it. Returns the release.
81
113
  */
82
- export function acquireActivitySocket(url: string, getToken: () => string | null): () => void {
114
+ export function acquireActivitySocket(url: string, getToken: ActivityTokenSource): () => void {
83
115
  let held = connections.get(url);
84
116
  if (!held) {
85
- held = open(url, getToken);
117
+ held = open(url);
86
118
  connections.set(url, held);
87
119
  }
88
120
  const connection = held;
121
+ const holder = Symbol('activity-holder');
122
+ connection.tokens.set(holder, getToken);
89
123
  connection.refs++;
90
124
  let released = false;
91
125
  return () => {
92
126
  if (released) return;
93
127
  released = true;
128
+ connection.tokens.delete(holder);
94
129
  if (--connection.refs > 0) return;
95
130
  connections.delete(url);
96
131
  if (connection.retry !== null) clearTimeout(connection.retry);
@@ -51,10 +51,17 @@ export function useAgentActivity(
51
51
  // Socket updates still provide live progress; hydration is best-effort.
52
52
  });
53
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 });
54
56
  const socket = io(apiUrl, {
55
57
  transports: ['websocket'],
56
- // The function form reads a fresh token on every (re)connect, so it survives rotation.
57
- auth: (cb) => cb({ token: oxyServices.session.accessToken }),
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
+ },
58
65
  reconnection: true,
59
66
  reconnectionAttempts: Infinity,
60
67
  reconnectionDelay: 1000,
@@ -73,6 +80,7 @@ export function useAgentActivity(
73
80
  return () => {
74
81
  cancelled = true;
75
82
  socket.disconnect();
83
+ linked.dispose();
76
84
  };
77
85
  }, [sessionId, agentId, handleEvent, api, apiUrl, oxyServices]);
78
86