@gemies/sdk 9.2.0 → 10.0.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 +103 -0
- package/README.md +9 -6
- package/package.json +4 -11
- package/src/accounts.ts +7 -1
- package/src/ask-eager.ts +0 -2
- package/src/ask-text.ts +0 -1
- package/src/ask.ts +0 -1
- package/src/components/AmbientField.tsx +13 -2
- package/src/components/ComposerDropZone.tsx +20 -4
- package/src/components/ComposerSuggestions.tsx +9 -2
- package/src/components/GemieActivityLine.tsx +11 -5
- package/src/components/GemieBetweenGemies.tsx +23 -10
- package/src/components/GemieChatContent.tsx +46 -23
- package/src/components/GemieChatScreen.tsx +7 -4
- package/src/components/GemieChatSheet.tsx +6 -2
- package/src/components/GemieComposer.tsx +17 -7
- package/src/components/GemieGroupWelcome.tsx +16 -2
- package/src/components/GemieRecentConversations.tsx +13 -10
- package/src/components/GemieSharedConversation.tsx +38 -6
- package/src/components/GemieWelcomeMessage.tsx +23 -7
- package/src/components/IdentityMark.tsx +6 -12
- package/src/components/PlanPreviewCard.tsx +18 -7
- package/src/components/QuestionCard.tsx +6 -2
- package/src/components/ReplyMarkdown.tsx +32 -6
- package/src/components/RichBlocks.tsx +20 -15
- package/src/components/accounts/GemieAccountGrants.tsx +27 -10
- package/src/components/accounts/GemieConnectedAccounts.tsx +87 -51
- package/src/components/canvas/CanvasComponent.tsx +6 -2
- package/src/components/canvas/DashboardRenderer.tsx +25 -8
- package/src/components/canvas/DeckRenderer.tsx +23 -6
- package/src/components/canvas/FormRenderer.tsx +8 -3
- package/src/components/connectors/ConnectorLogo.tsx +15 -9
- package/src/components/connectors/GemieConnectedConnectors.tsx +51 -25
- package/src/components/connectors/GemieConnectorsStore.tsx +99 -41
- package/src/components/connectors/GrantSwitchRow.tsx +18 -5
- package/src/components/thought/ThoughtPanel.tsx +26 -12
- package/src/components/thought/ToolStep.tsx +14 -6
- package/src/components/thread/AgentResultCard.tsx +10 -4
- package/src/components/thread/AgentTaskCard.tsx +8 -3
- package/src/components/thread/ApprovalCard.tsx +6 -2
- package/src/components/thread/ChatBubble.tsx +66 -25
- package/src/components/thread/GemieThread.tsx +32 -10
- package/src/components/thread/ThreadRow.tsx +20 -15
- package/src/components/thread/ThreadSearch.tsx +27 -10
- package/src/components/thread/ThreadSkeleton.tsx +12 -6
- package/src/components/thread/ToolResultCard.tsx +10 -4
- package/src/components/thread/TurnAttachments.tsx +17 -8
- package/src/components/voice/VoiceControls.tsx +9 -6
- package/src/connectors.ts +15 -2
- package/src/hooks/useAgentRunFailureToast.ts +34 -0
- package/src/hooks/useChatVoice.ts +2 -10
- package/src/hooks/useMessageReactions.ts +5 -5
- package/src/hooks/useThoughtScope.ts +9 -8
- package/src/index.ts +2 -3
- package/src/lib/tool-call-status.ts +1 -1
- package/src/lib/utils.ts +1 -8
- package/src/realtime/react.tsx +9 -9
- package/src/realtime/reread.ts +28 -0
- package/src/realtime/store.ts +123 -0
- package/src/runtime/agents.ts +46 -27
- package/src/runtime/collections.ts +35 -136
- package/src/runtime/connected-accounts.ts +175 -88
- package/src/runtime/connectors.ts +244 -127
- package/src/runtime/conversations.ts +181 -607
- package/src/runtime/data/answer.ts +76 -0
- package/src/runtime/data/collections.ts +168 -0
- package/src/runtime/data/conversations.ts +626 -0
- package/src/runtime/data/reactions.ts +29 -0
- package/src/runtime/data/read.ts +37 -0
- package/src/runtime/data/threads.ts +149 -0
- package/src/runtime/index.ts +72 -8
- package/src/runtime/model.ts +0 -2
- package/src/runtime/routes.ts +2 -2
- package/src/runtime/sharing.ts +69 -56
- package/src/runtime/types.ts +8 -16
- package/src/runtime/use-bound-action.ts +27 -0
- package/src/runtime/use-conversation-stream.ts +8 -31
- package/src/runtime/use-conversation-thread.ts +40 -51
- package/src/runtime/use-gemie-thread.ts +32 -43
- package/src/runtime/use-start-conversation.ts +14 -15
- package/src/runtime/use-thread-history.ts +26 -36
- package/src/runtime/use-thread-search.ts +29 -76
- package/src/text.ts +0 -1
- package/src/voice.ts +0 -1
- package/src/components/Image.tsx +0 -12
- package/src/realtime/cache.ts +0 -257
- package/src/runtime/conversation-dates.ts +0 -31
- package/src/runtime/query-keys.ts +0 -66
- package/src/runtime/reaction-cache.ts +0 -45
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
import { useOxy } from '@oxy.so/services/ui/client';
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
action,
|
|
3
|
+
type ActionView,
|
|
4
|
+
entity,
|
|
5
|
+
list,
|
|
6
|
+
type RecordView,
|
|
7
|
+
skip,
|
|
8
|
+
source,
|
|
9
|
+
useEntities,
|
|
10
|
+
useList,
|
|
11
|
+
useListState,
|
|
12
|
+
useStore,
|
|
13
|
+
withStore,
|
|
14
|
+
} from '@oxy.so/bloom-data';
|
|
15
|
+
import { useOxy } from '@oxy.so/services/ui/client';
|
|
9
16
|
import * as WebBrowser from 'expo-web-browser';
|
|
10
|
-
import {
|
|
17
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
11
18
|
import { Platform } from 'react-native';
|
|
12
|
-
import {
|
|
19
|
+
import { rereadOn } from '../realtime/reread';
|
|
20
|
+
import { type GemieApi, useGemieApi } from './api';
|
|
13
21
|
import { useGemieRuntime } from './config';
|
|
22
|
+
import { answerSource, useAnswer } from './data/answer';
|
|
23
|
+
import { ACCOUNT, gemieRead } from './data/read';
|
|
24
|
+
import { useBoundAction } from './use-bound-action';
|
|
14
25
|
|
|
15
26
|
/**
|
|
16
27
|
* Connectors (`@gemies/sdk/connectors`): the providers' own remote MCP servers a person connects —
|
|
@@ -37,12 +48,6 @@ export const CONNECTOR_ROUTES = {
|
|
|
37
48
|
reconnect: (id: string) => `/mcp/${encodeURIComponent(id)}/reconnect`,
|
|
38
49
|
} as const;
|
|
39
50
|
|
|
40
|
-
export const connectorQueryKeys = {
|
|
41
|
-
catalogue: ['gemie-connectors', 'catalogue'] as const,
|
|
42
|
-
store: (query: ConnectorStoreQuery) => ['gemie-connectors', 'catalogue', query] as const,
|
|
43
|
-
connected: ['gemie-connectors', 'connected'] as const,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
51
|
export const CONNECTOR_CATEGORIES = [
|
|
47
52
|
'productivity',
|
|
48
53
|
'development',
|
|
@@ -148,44 +153,122 @@ export function connectorStoreParams(
|
|
|
148
153
|
};
|
|
149
154
|
}
|
|
150
155
|
|
|
156
|
+
/** The store's connectors, by id: one record serves every search that lists it. */
|
|
157
|
+
export const ConnectorEntries = entity<ConnectorEntry>('gemies.connector', {
|
|
158
|
+
id: (entry) => entry.id,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
/** What a store list keeps beside its entries: the count and each category's, under its search. */
|
|
162
|
+
export interface ConnectorStoreExtra {
|
|
163
|
+
total: number;
|
|
164
|
+
categories: ConnectorStorePage['categories'];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The store's lists, one per search, filters and order (`connectorStoreKey`); memory only. */
|
|
168
|
+
export const ConnectorStoreList = list<ConnectorStoreExtra>('gemies.connector-store', {
|
|
169
|
+
of: ConnectorEntries,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
/** The key of a store query: equal searches share one list. */
|
|
173
|
+
export function connectorStoreKey(query: ConnectorStoreQuery): string {
|
|
174
|
+
return JSON.stringify(connectorStoreParams(query, 0));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The store, a page at a time (by offset), searched, filtered and sorted by the API. */
|
|
178
|
+
export const connectorStore = source(ConnectorStoreList, {
|
|
179
|
+
...gemieRead,
|
|
180
|
+
key: (_api: GemieApi, query: ConnectorStoreQuery) => connectorStoreKey(query),
|
|
181
|
+
stale: 5 * 60_000,
|
|
182
|
+
load: async ({ cursor, signal }, api, query) => {
|
|
183
|
+
const { data } = await api.get<ConnectorStorePage>(CONNECTOR_ROUTES.catalogue, {
|
|
184
|
+
signal,
|
|
185
|
+
params: connectorStoreParams(query, cursor === undefined ? 0 : Number(cursor)),
|
|
186
|
+
});
|
|
187
|
+
return {
|
|
188
|
+
items: data.servers,
|
|
189
|
+
next: data.nextOffset === null ? null : String(data.nextOffset),
|
|
190
|
+
extra: { total: data.total, categories: data.categories },
|
|
191
|
+
};
|
|
192
|
+
},
|
|
193
|
+
// The first page's count and categories describe the whole search.
|
|
194
|
+
fold: (current, extra, mode) =>
|
|
195
|
+
(mode === 'next' ? current : extra) ?? { total: 0, categories: [] },
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
/** The store as a screen reads it. */
|
|
199
|
+
export interface ConnectorStoreView {
|
|
200
|
+
readonly entries: readonly ConnectorEntry[];
|
|
201
|
+
readonly total: number;
|
|
202
|
+
readonly categories: ConnectorStorePage['categories'];
|
|
203
|
+
/** Nothing to show yet: no answer for this search, and none for an earlier one. */
|
|
204
|
+
readonly isLoading: boolean;
|
|
205
|
+
readonly isError: boolean;
|
|
206
|
+
readonly hasMore: boolean;
|
|
207
|
+
readonly isLoadingMore: boolean;
|
|
208
|
+
readonly more: () => Promise<void>;
|
|
209
|
+
readonly refresh: () => Promise<void>;
|
|
210
|
+
}
|
|
211
|
+
|
|
151
212
|
/**
|
|
152
213
|
* The store, a page at a time: hundreds of connectors are searched, filtered and sorted by the API,
|
|
153
|
-
* and a new search keeps the previous results on screen until
|
|
214
|
+
* and a new search keeps the previous results on screen until its own arrive.
|
|
154
215
|
*/
|
|
155
|
-
export function useConnectorStore(query: ConnectorStoreQuery) {
|
|
216
|
+
export function useConnectorStore(query: ConnectorStoreQuery): ConnectorStoreView {
|
|
156
217
|
const api = useGemieApi();
|
|
157
|
-
const {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
218
|
+
const { canUsePrivateApi } = useOxy();
|
|
219
|
+
const view = useList(connectorStore, ...(canUsePrivateApi ? [api, query] : [skip]));
|
|
220
|
+
// The last search that answered, shown while a new one has no answer yet.
|
|
221
|
+
const [shown, setShown] = useState<string | null>(null);
|
|
222
|
+
useEffect(() => {
|
|
223
|
+
if (view.list) setShown(view.key);
|
|
224
|
+
}, [view.list, view.key]);
|
|
225
|
+
const previous = useListState(ConnectorStoreList, view.list ? null : shown);
|
|
226
|
+
const held = view.list ?? previous;
|
|
227
|
+
const entries = useEntities(ConnectorEntries, held?.ids ?? []);
|
|
228
|
+
return {
|
|
229
|
+
entries: useMemo(
|
|
230
|
+
() => entries.filter((entry): entry is ConnectorEntry => entry !== undefined),
|
|
231
|
+
[entries],
|
|
232
|
+
),
|
|
233
|
+
total: held?.extra?.total ?? 0,
|
|
234
|
+
categories: held?.extra?.categories ?? [],
|
|
235
|
+
isLoading: held === undefined && view.isLoading,
|
|
236
|
+
isError: view.status === 'error',
|
|
237
|
+
hasMore: view.hasMore,
|
|
238
|
+
isLoadingMore: view.isFetching && view.list !== undefined,
|
|
239
|
+
more: view.more,
|
|
240
|
+
refresh: view.refresh,
|
|
241
|
+
};
|
|
172
242
|
}
|
|
173
243
|
|
|
244
|
+
/**
|
|
245
|
+
* What the person connected, each account on its own: memory only, read again when the realtime
|
|
246
|
+
* `connections` event says it changed, and after a reset.
|
|
247
|
+
*/
|
|
248
|
+
export const connections = answerSource('gemies.connections', {
|
|
249
|
+
...gemieRead,
|
|
250
|
+
key: (_api: GemieApi) => ACCOUNT,
|
|
251
|
+
stale: 30_000,
|
|
252
|
+
load: async ({ signal }, api) =>
|
|
253
|
+
(await api.get<{ servers: Connection[] }>(CONNECTOR_ROUTES.connected, { signal })).data.servers,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
rereadOn('connections', connections);
|
|
257
|
+
rereadOn('reset', connections);
|
|
258
|
+
|
|
174
259
|
/**
|
|
175
260
|
* What the person connected, each account on its own. `enabled: false` reads nothing: the chat is
|
|
176
261
|
* embedded in apps whose origin the connector routes do not answer, and asks only for a turn that
|
|
177
262
|
* used a connector.
|
|
178
263
|
*/
|
|
179
|
-
export function useConnections({
|
|
264
|
+
export function useConnections({
|
|
265
|
+
enabled = true,
|
|
266
|
+
}: {
|
|
267
|
+
enabled?: boolean;
|
|
268
|
+
} = {}): RecordView<Connection[]> {
|
|
180
269
|
const api = useGemieApi();
|
|
181
|
-
const {
|
|
182
|
-
return
|
|
183
|
-
queryKey: connectorQueryKeys.connected,
|
|
184
|
-
queryFn: async () =>
|
|
185
|
-
(await api.get<{ servers: Connection[] }>(CONNECTOR_ROUTES.connected)).data.servers,
|
|
186
|
-
enabled: isAuthenticated && enabled,
|
|
187
|
-
staleTime: 30_000,
|
|
188
|
-
});
|
|
270
|
+
const { canUsePrivateApi } = useOxy();
|
|
271
|
+
return useAnswer(connections, ...(canUsePrivateApi && enabled ? [api] : [skip]));
|
|
189
272
|
}
|
|
190
273
|
|
|
191
274
|
/** A logo path the API answered, as a URL on its origin. */
|
|
@@ -232,87 +315,123 @@ export interface ConnectorActionsOptions {
|
|
|
232
315
|
returnTo: string;
|
|
233
316
|
}
|
|
234
317
|
|
|
318
|
+
/** Draws a change to the held connections, as the calling action's layer. */
|
|
319
|
+
function drawConnections(change: (held: Connection[]) => Connection[]): void {
|
|
320
|
+
connections.entity.update(ACCOUNT, (held) => ({ ...held, value: change([...held.value]) }));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Every write ends with the connections read again, so each screen shows them as they are. */
|
|
324
|
+
const rereadConnections = (api: GemieApi) => void connections.refresh(api);
|
|
325
|
+
|
|
326
|
+
/** Finishes a provider's sign-in with what it returned (`state`, `code`): the connection it made. */
|
|
327
|
+
export const completeConnectorSignIn = action({
|
|
328
|
+
run: async (api: GemieApi, result: { state: string; code: string }) =>
|
|
329
|
+
(await api.post<{ server: Connection }>(CONNECTOR_ROUTES.complete, result)).data.server,
|
|
330
|
+
settle: (_connection, api) => rereadConnections(api),
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
/** What a connection through a provider's sign-in needs: the API, and where the sign-in returns. */
|
|
334
|
+
export interface ConnectorClient {
|
|
335
|
+
api: GemieApi;
|
|
336
|
+
returnTo: string;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Sign in at the provider, and finish here when the native session hands the result back. */
|
|
340
|
+
async function signIn(
|
|
341
|
+
{ api, returnTo }: ConnectorClient,
|
|
342
|
+
authorizationUrl: string,
|
|
343
|
+
): Promise<Connection | null> {
|
|
344
|
+
const returned = await openSignIn(authorizationUrl, returnTo);
|
|
345
|
+
if (!returned) return null;
|
|
346
|
+
const result = signInResult(new URL(returned).searchParams);
|
|
347
|
+
if (!result) return null;
|
|
348
|
+
if ('error' in result) throw new Error(result.error);
|
|
349
|
+
return (await api.post<{ server: Connection }>(CONNECTOR_ROUTES.complete, result)).data.server;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** A new account: through the provider's sign-in, or at once for a server that needs none. */
|
|
353
|
+
export const connectConnector = action({
|
|
354
|
+
run: async (client: ConnectorClient, registryId: string) => {
|
|
355
|
+
const { data } = await client.api.post<{ authorizationUrl?: string; server?: Connection }>(
|
|
356
|
+
CONNECTOR_ROUTES.connect,
|
|
357
|
+
{ registryId, returnTo: client.returnTo },
|
|
358
|
+
);
|
|
359
|
+
if (data.authorizationUrl) return signIn(client, data.authorizationUrl);
|
|
360
|
+
return data.server ?? null;
|
|
361
|
+
},
|
|
362
|
+
settle: (_connection, client) => rereadConnections(client.api),
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
export interface CustomConnectorInput {
|
|
366
|
+
name: string;
|
|
367
|
+
url: string;
|
|
368
|
+
headers?: Record<string, string>;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** The person's own MCP server, by URL; through its sign-in when it has one. */
|
|
372
|
+
export const addCustomConnector = action({
|
|
373
|
+
run: async (client: ConnectorClient, input: CustomConnectorInput) => {
|
|
374
|
+
const { data } = await client.api.post<{ server: Connection; authorizationUrl?: string }>(
|
|
375
|
+
CONNECTOR_ROUTES.custom,
|
|
376
|
+
{ ...input, returnTo: client.returnTo },
|
|
377
|
+
);
|
|
378
|
+
return data.authorizationUrl ? signIn(client, data.authorizationUrl) : data.server;
|
|
379
|
+
},
|
|
380
|
+
settle: (_connection, client) => rereadConnections(client.api),
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
/** Connects an account again, through its provider's sign-in when it needs one. */
|
|
384
|
+
export const reconnectConnector = action({
|
|
385
|
+
run: async (client: ConnectorClient, id: string) => {
|
|
386
|
+
const { data } = await client.api.post<{ server: Connection; authorizationUrl?: string }>(
|
|
387
|
+
CONNECTOR_ROUTES.reconnect(id),
|
|
388
|
+
{ returnTo: client.returnTo },
|
|
389
|
+
);
|
|
390
|
+
return data.authorizationUrl ? signIn(client, data.authorizationUrl) : data.server;
|
|
391
|
+
},
|
|
392
|
+
settle: (_connection, client) => rereadConnections(client.api),
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
/** Names which account of the provider a connection is: drawn at once, put back if refused. */
|
|
396
|
+
export const renameConnector = action({
|
|
397
|
+
optimistic: (_api: GemieApi, id: string, accountLabel: string | null) =>
|
|
398
|
+
drawConnections((held) => held.map((c) => (c._id === id ? { ...c, accountLabel } : c))),
|
|
399
|
+
run: async (api, id, accountLabel) => {
|
|
400
|
+
await api.patch(CONNECTOR_ROUTES.connection(id), { accountLabel });
|
|
401
|
+
},
|
|
402
|
+
settle: (_result, api) => rereadConnections(api),
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
/** Disconnects an account: gone at once, back if refused. */
|
|
406
|
+
export const disconnectConnector = action({
|
|
407
|
+
optimistic: (_api: GemieApi, id: string) =>
|
|
408
|
+
drawConnections((held) => held.filter((c) => c._id !== id)),
|
|
409
|
+
run: async (api, id) => {
|
|
410
|
+
await api.delete(CONNECTOR_ROUTES.connection(id));
|
|
411
|
+
},
|
|
412
|
+
settle: (_result, api) => rereadConnections(api),
|
|
413
|
+
});
|
|
414
|
+
|
|
235
415
|
/**
|
|
236
|
-
* Connecting, adding a custom server, reconnecting, renaming and disconnecting
|
|
237
|
-
*
|
|
416
|
+
* Connecting, adding a custom server, reconnecting, renaming and disconnecting, as a component calls
|
|
417
|
+
* them. Each ends with the connections read again.
|
|
238
418
|
*/
|
|
239
|
-
export function useConnectorActions({ returnTo }: ConnectorActionsOptions) {
|
|
419
|
+
export function useConnectorActions({ returnTo }: ConnectorActionsOptions): {
|
|
420
|
+
connect: ActionView<[registryId: string], Connection | null>;
|
|
421
|
+
addCustom: ActionView<[input: CustomConnectorInput], Connection | null>;
|
|
422
|
+
reconnect: ActionView<[id: string], Connection | null>;
|
|
423
|
+
rename: ActionView<[id: string, accountLabel: string | null], void>;
|
|
424
|
+
disconnect: ActionView<[id: string], void>;
|
|
425
|
+
} {
|
|
240
426
|
const api = useGemieApi();
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
mutationFn: async (input: { state: string; code: string }) =>
|
|
249
|
-
(await api.post<{ server: Connection }>(CONNECTOR_ROUTES.complete, input)).data.server,
|
|
250
|
-
onSuccess: refresh,
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
/** Sign in at the provider, and finish here when the native session hands the result back. */
|
|
254
|
-
const signIn = async (authorizationUrl: string): Promise<Connection | null> => {
|
|
255
|
-
const returned = await openSignIn(authorizationUrl, returnTo);
|
|
256
|
-
if (!returned) return null;
|
|
257
|
-
const result = signInResult(new URL(returned).searchParams);
|
|
258
|
-
if (!result) return null;
|
|
259
|
-
if ('error' in result) throw new Error(result.error);
|
|
260
|
-
return complete.mutateAsync(result);
|
|
427
|
+
const client = useMemo<ConnectorClient>(() => ({ api, returnTo }), [api, returnTo]);
|
|
428
|
+
return {
|
|
429
|
+
connect: useBoundAction(connectConnector, client),
|
|
430
|
+
addCustom: useBoundAction(addCustomConnector, client),
|
|
431
|
+
reconnect: useBoundAction(reconnectConnector, client),
|
|
432
|
+
rename: useBoundAction(renameConnector, api),
|
|
433
|
+
disconnect: useBoundAction(disconnectConnector, api),
|
|
261
434
|
};
|
|
262
|
-
|
|
263
|
-
/** A new account: through the provider's sign-in, or at once for a server that needs none. */
|
|
264
|
-
const connect = useMutation({
|
|
265
|
-
mutationFn: async (registryId: string) => {
|
|
266
|
-
const { data } = await api.post<{ authorizationUrl?: string; server?: Connection }>(
|
|
267
|
-
CONNECTOR_ROUTES.connect,
|
|
268
|
-
{ registryId, returnTo },
|
|
269
|
-
);
|
|
270
|
-
if (data.authorizationUrl) return signIn(data.authorizationUrl);
|
|
271
|
-
await refresh();
|
|
272
|
-
return data.server ?? null;
|
|
273
|
-
},
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
const addCustom = useMutation({
|
|
277
|
-
mutationFn: async (input: { name: string; url: string; headers?: Record<string, string> }) => {
|
|
278
|
-
const { data } = await api.post<{ server: Connection; authorizationUrl?: string }>(
|
|
279
|
-
CONNECTOR_ROUTES.custom,
|
|
280
|
-
{ ...input, returnTo },
|
|
281
|
-
);
|
|
282
|
-
await refresh();
|
|
283
|
-
return data.authorizationUrl ? signIn(data.authorizationUrl) : data.server;
|
|
284
|
-
},
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
const reconnect = useMutation({
|
|
288
|
-
mutationFn: async (id: string) => {
|
|
289
|
-
const { data } = await api.post<{ server: Connection; authorizationUrl?: string }>(
|
|
290
|
-
CONNECTOR_ROUTES.reconnect(id),
|
|
291
|
-
{ returnTo },
|
|
292
|
-
);
|
|
293
|
-
if (!data.authorizationUrl) {
|
|
294
|
-
await refresh();
|
|
295
|
-
return data.server;
|
|
296
|
-
}
|
|
297
|
-
return signIn(data.authorizationUrl);
|
|
298
|
-
},
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
const rename = useMutation({
|
|
302
|
-
mutationFn: async (input: { id: string; accountLabel: string | null }) => {
|
|
303
|
-
await api.patch(CONNECTOR_ROUTES.connection(input.id), { accountLabel: input.accountLabel });
|
|
304
|
-
},
|
|
305
|
-
onSuccess: refresh,
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
const disconnect = useMutation({
|
|
309
|
-
mutationFn: async (id: string) => {
|
|
310
|
-
await api.delete(CONNECTOR_ROUTES.connection(id));
|
|
311
|
-
},
|
|
312
|
-
onSuccess: refresh,
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
return { connect, addCustom, reconnect, rename, disconnect, complete };
|
|
316
435
|
}
|
|
317
436
|
|
|
318
437
|
/** The connector slug a tool name carries: `mcp_notion_2__search` → `notion_2`. */
|
|
@@ -341,8 +460,8 @@ export function connectionOfTool(
|
|
|
341
460
|
* connector's tool.
|
|
342
461
|
*/
|
|
343
462
|
export function useConnectionOfTool(toolName: string | undefined): Connection | undefined {
|
|
344
|
-
const {
|
|
345
|
-
return connectionOfTool(
|
|
463
|
+
const { value } = useConnections({ enabled: toolSlug(toolName) !== undefined });
|
|
464
|
+
return connectionOfTool(value, toolName);
|
|
346
465
|
}
|
|
347
466
|
|
|
348
467
|
/** The copy key that says why a sign-in did not connect (`mcp_oauth_error`, or `failed`). */
|
|
@@ -377,7 +496,7 @@ export function useWebSignInReturn(
|
|
|
377
496
|
onDone: (outcome: { ok: true; connection: Connection } | { ok: false; error: string }) => void,
|
|
378
497
|
) {
|
|
379
498
|
const api = useGemieApi();
|
|
380
|
-
const
|
|
499
|
+
const store = useStore();
|
|
381
500
|
const handled = useRef(false);
|
|
382
501
|
useEffect(() => {
|
|
383
502
|
if (Platform.OS !== 'web' || typeof window === 'undefined' || handled.current) return;
|
|
@@ -396,12 +515,10 @@ export function useWebSignInReturn(
|
|
|
396
515
|
onDone({ ok: false, error: result.error });
|
|
397
516
|
return;
|
|
398
517
|
}
|
|
399
|
-
api
|
|
400
|
-
.
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
onDone({ ok: true, connection: data.server });
|
|
404
|
-
})
|
|
518
|
+
withStore(store, () => completeConnectorSignIn(api, result))
|
|
519
|
+
.then((connection) =>
|
|
520
|
+
onDone(connection ? { ok: true, connection } : { ok: false, error: 'failed' }),
|
|
521
|
+
)
|
|
405
522
|
.catch(() => onDone({ ok: false, error: 'failed' }));
|
|
406
523
|
// Once per page load: the query is cleared as soon as it is read.
|
|
407
524
|
}, []);
|