@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
package/src/runtime/agents.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { type RecordView, skip } from '@oxy.so/bloom-data';
|
|
1
2
|
import { useOxy } from '@oxy.so/services/ui/client';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { type GemieApi, useGemieApi } from './api';
|
|
4
|
+
import { answerSource, useAnswer } from './data/answer';
|
|
5
|
+
import { gemieRead } from './data/read';
|
|
5
6
|
import { API_ROUTES } from './routes';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -45,8 +46,6 @@ export const GEMIE_FEATURES = {
|
|
|
45
46
|
images: 'attachments.images',
|
|
46
47
|
} as const;
|
|
47
48
|
|
|
48
|
-
const FIVE_MINUTES = 1000 * 60 * 5;
|
|
49
|
-
|
|
50
49
|
/** A string, or nothing: the profile is read field by field rather than cast. */
|
|
51
50
|
const text = (value: unknown): string | undefined =>
|
|
52
51
|
typeof value === 'string' && value !== '' ? value : undefined;
|
|
@@ -102,36 +101,56 @@ export function readAgentFeatures(value: unknown): GemieAgentFeature[] {
|
|
|
102
101
|
});
|
|
103
102
|
}
|
|
104
103
|
|
|
104
|
+
/**
|
|
105
|
+
* The profile of an agent, by its id: copied to the account's durable store, so a chat opened on
|
|
106
|
+
* it draws its face and greeting from the first frame. `null` when the server's answer had no
|
|
107
|
+
* profile's shape.
|
|
108
|
+
*/
|
|
109
|
+
export const agentProfile = answerSource('gemies.agent-profile', {
|
|
110
|
+
...gemieRead,
|
|
111
|
+
persist: true,
|
|
112
|
+
key: (_api: GemieApi, agentId: string) => agentId,
|
|
113
|
+
stale: 5 * 60_000,
|
|
114
|
+
retry: 1,
|
|
115
|
+
load: async ({ signal }, api, agentId) =>
|
|
116
|
+
readAgentProfile((await api.get<unknown>(API_ROUTES.agents.profile(agentId), { signal })).data),
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* What of Alia's features an agent offers the person present. A gate's state moves with the
|
|
121
|
+
* person's plan in another app, so it is read each time a chat opens (never fresh).
|
|
122
|
+
*/
|
|
123
|
+
export const agentFeatures = answerSource('gemies.agent-features', {
|
|
124
|
+
...gemieRead,
|
|
125
|
+
key: (_api: GemieApi, agentId: string) => agentId,
|
|
126
|
+
retry: 1,
|
|
127
|
+
load: async ({ signal }, api, agentId) =>
|
|
128
|
+
readAgentFeatures(
|
|
129
|
+
(await api.get<unknown>(API_ROUTES.agents.features(agentId), { signal })).data,
|
|
130
|
+
),
|
|
131
|
+
});
|
|
132
|
+
|
|
105
133
|
/** The profile of the agent `agentId`, or nothing for Alia (no id) and while it loads. */
|
|
106
|
-
export function useAgentProfile(agentId: string | undefined) {
|
|
107
|
-
const {
|
|
134
|
+
export function useAgentProfile(agentId: string | undefined): RecordView<GemieAgentProfile | null> {
|
|
135
|
+
const { canUsePrivateApi } = useOxy();
|
|
108
136
|
const api = useGemieApi();
|
|
109
|
-
return
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
enabled: agentId !== undefined && isAuthenticated,
|
|
114
|
-
staleTime: FIVE_MINUTES,
|
|
115
|
-
retry: 1,
|
|
116
|
-
});
|
|
137
|
+
return useAnswer(
|
|
138
|
+
agentProfile,
|
|
139
|
+
...(agentId !== undefined && canUsePrivateApi ? [api, agentId] : [skip]),
|
|
140
|
+
);
|
|
117
141
|
}
|
|
118
142
|
|
|
119
143
|
/**
|
|
120
144
|
* What of Alia's features the agent `agentId` offers the person present. Alia (no id) gates
|
|
121
|
-
* nothing, so she has no answer to wait for; see `
|
|
145
|
+
* nothing, so she has no answer to wait for; see `featureOffer` for what the chat makes of it.
|
|
122
146
|
*/
|
|
123
|
-
export function useAgentFeatures(agentId: string | undefined) {
|
|
124
|
-
const {
|
|
147
|
+
export function useAgentFeatures(agentId: string | undefined): RecordView<GemieAgentFeature[]> {
|
|
148
|
+
const { canUsePrivateApi } = useOxy();
|
|
125
149
|
const api = useGemieApi();
|
|
126
|
-
return
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
enabled: agentId !== undefined && isAuthenticated,
|
|
131
|
-
// A gate's state moves with the person's plan in another app: read it each time the chat opens.
|
|
132
|
-
staleTime: 0,
|
|
133
|
-
retry: 1,
|
|
134
|
-
});
|
|
150
|
+
return useAnswer(
|
|
151
|
+
agentFeatures,
|
|
152
|
+
...(agentId !== undefined && canUsePrivateApi ? [api, agentId] : [skip]),
|
|
153
|
+
);
|
|
135
154
|
}
|
|
136
155
|
|
|
137
156
|
/** How an attachment feature is offered: attached, an upsell, or not at all. */
|
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* How a person files their conversations
|
|
3
|
-
* so every device shows the same (Gemies `docs/realtime.mdx`, "Collections").
|
|
4
|
-
*
|
|
5
|
-
* One query holds the whole snapshot (`GET /v1/conversations/collections`). Every change is drawn
|
|
6
|
-
* at once (optimistic), sent, and replaced by the snapshot the server answers; the other devices
|
|
7
|
-
* receive the same snapshot as the realtime `collections` event. Nothing is stored on the device.
|
|
2
|
+
* How a person files their conversations (folders, projects, pins, favourites) on their account,
|
|
3
|
+
* so every device shows the same (Gemies `docs/realtime.mdx`, "Collections"). The snapshot is held
|
|
4
|
+
* in the account's store (`data/collections.ts`) and kept current by the realtime connection.
|
|
8
5
|
*/
|
|
9
6
|
|
|
10
|
-
import {
|
|
7
|
+
import { skip, useStore, withStore } from '@oxy.so/bloom-data';
|
|
11
8
|
import { useOxy } from '@oxy.so/services/ui/client';
|
|
12
|
-
import {
|
|
13
|
-
import type
|
|
9
|
+
import { useMemo } from 'react';
|
|
10
|
+
import { type GemieApi, useGemieApi } from './api';
|
|
14
11
|
import {
|
|
15
12
|
type CollectionFields,
|
|
16
|
-
type ConversationCollection,
|
|
17
13
|
type ConversationCollectionKind,
|
|
18
14
|
type ConversationCollections,
|
|
19
15
|
EMPTY_COLLECTIONS,
|
|
20
|
-
type LocalCollections,
|
|
21
|
-
readCollectionsSnapshot,
|
|
22
16
|
} from './collections-model';
|
|
17
|
+
import {
|
|
18
|
+
conversationCollections,
|
|
19
|
+
createCollection,
|
|
20
|
+
deleteCollection,
|
|
21
|
+
setCollectionMember,
|
|
22
|
+
setConversationFavorite,
|
|
23
|
+
setConversationPinned,
|
|
24
|
+
updateCollection,
|
|
25
|
+
} from './data/collections';
|
|
26
|
+
import { useAnswer } from './data/answer';
|
|
23
27
|
|
|
24
28
|
export type {
|
|
25
29
|
CollectionFields,
|
|
@@ -29,38 +33,15 @@ export type {
|
|
|
29
33
|
LocalCollections,
|
|
30
34
|
} from './collections-model';
|
|
31
35
|
export { readCollectionsSnapshot } from './collections-model';
|
|
32
|
-
import { type GemieApi, useGemieApi } from './api';
|
|
33
|
-
import { gemieQueryKeys } from './query-keys';
|
|
34
|
-
import { API_ROUTES } from './routes';
|
|
35
36
|
|
|
36
37
|
/** The person's folders, projects, pins and favourites. Kept current by the realtime connection. */
|
|
37
38
|
export function useConversationCollections(): ConversationCollections {
|
|
38
39
|
const api = useGemieApi();
|
|
39
|
-
const { canUsePrivateApi
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
(await api.get<CollectionsSnapshotWire>(API_ROUTES.collections.list)).data,
|
|
45
|
-
),
|
|
46
|
-
enabled: canUsePrivateApi && isAccountCacheReady,
|
|
47
|
-
// No timer and no refocus: the `collections` event keeps it, and a reset reads it again.
|
|
48
|
-
staleTime: Number.POSITIVE_INFINITY,
|
|
49
|
-
refetchOnWindowFocus: false,
|
|
50
|
-
});
|
|
51
|
-
return query.data ?? EMPTY_COLLECTIONS;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Bring what this device filed before collections were the account's into the account (the
|
|
56
|
-
* server merges by name, so every device may bring its own). Resolves with the account's snapshot.
|
|
57
|
-
*/
|
|
58
|
-
export async function importCollections(
|
|
59
|
-
api: GemieApi,
|
|
60
|
-
local: LocalCollections,
|
|
61
|
-
): Promise<ConversationCollections> {
|
|
62
|
-
const { data } = await api.post<CollectionsSnapshotWire>(API_ROUTES.collections.import, local);
|
|
63
|
-
return readCollectionsSnapshot(data);
|
|
40
|
+
const { canUsePrivateApi } = useOxy();
|
|
41
|
+
return (
|
|
42
|
+
useAnswer(conversationCollections, ...(canUsePrivateApi ? [api] : [skip])).value ??
|
|
43
|
+
EMPTY_COLLECTIONS
|
|
44
|
+
);
|
|
64
45
|
}
|
|
65
46
|
|
|
66
47
|
export interface CollectionActions {
|
|
@@ -78,108 +59,26 @@ export interface CollectionActions {
|
|
|
78
59
|
|
|
79
60
|
/**
|
|
80
61
|
* Change how the person files their conversations. Each change is drawn at once and then replaced
|
|
81
|
-
* by what the server answers; a refused change puts the server's state back.
|
|
62
|
+
* by what the server answers; a refused change puts the server's state back, and rejects.
|
|
82
63
|
*/
|
|
83
64
|
export function useCollectionActions(): CollectionActions {
|
|
84
65
|
const api = useGemieApi();
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
const run = useCallback(
|
|
88
|
-
async (
|
|
89
|
-
draw: (current: ConversationCollections) => ConversationCollections,
|
|
90
|
-
send: () => Promise<{ data: CollectionsSnapshotWire }>,
|
|
91
|
-
) => {
|
|
92
|
-
await queryClient.cancelQueries({ queryKey: gemieQueryKeys.collections });
|
|
93
|
-
queryClient.setQueryData<ConversationCollections>(gemieQueryKeys.collections, (current) =>
|
|
94
|
-
draw(current ?? EMPTY_COLLECTIONS),
|
|
95
|
-
);
|
|
96
|
-
try {
|
|
97
|
-
const { data } = await send();
|
|
98
|
-
queryClient.setQueryData(gemieQueryKeys.collections, readCollectionsSnapshot(data));
|
|
99
|
-
} catch (error) {
|
|
100
|
-
void queryClient.invalidateQueries({ queryKey: gemieQueryKeys.collections });
|
|
101
|
-
throw error;
|
|
102
|
-
}
|
|
103
|
-
},
|
|
104
|
-
[queryClient],
|
|
105
|
-
);
|
|
106
|
-
|
|
66
|
+
const store = useStore();
|
|
107
67
|
return useMemo<CollectionActions>(() => {
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
...current,
|
|
113
|
-
collections: current.collections.map((c) => (c.id === id ? change(c) : c)),
|
|
114
|
-
});
|
|
115
|
-
const toggle = (list: string[], id: string, on: boolean) =>
|
|
116
|
-
on ? (list.includes(id) ? list : [...list, id]) : list.filter((x) => x !== id);
|
|
68
|
+
const call = <A extends unknown[]>(
|
|
69
|
+
action: (api: GemieApi, ...args: A) => Promise<unknown>,
|
|
70
|
+
...args: A
|
|
71
|
+
) => withStore(store, () => action(api, ...args)).then(() => undefined);
|
|
117
72
|
return {
|
|
118
|
-
createCollection: (kind, fields) =>
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
collections: [
|
|
123
|
-
...current.collections,
|
|
124
|
-
{
|
|
125
|
-
id: `pending-${Date.now()}`,
|
|
126
|
-
kind,
|
|
127
|
-
name: fields.name,
|
|
128
|
-
description: fields.description ?? null,
|
|
129
|
-
icon: fields.icon ?? null,
|
|
130
|
-
color: fields.color ?? null,
|
|
131
|
-
isFavorite: false,
|
|
132
|
-
isExpanded: true,
|
|
133
|
-
conversationIds: [],
|
|
134
|
-
createdAt: new Date().toISOString(),
|
|
135
|
-
updatedAt: new Date().toISOString(),
|
|
136
|
-
},
|
|
137
|
-
],
|
|
138
|
-
}),
|
|
139
|
-
() => api.post(R.create, { kind, ...fields }),
|
|
140
|
-
),
|
|
141
|
-
updateCollection: (id, fields) =>
|
|
142
|
-
run(
|
|
143
|
-
edit(id, (c) => ({ ...c, ...fields, name: fields.name ?? c.name })),
|
|
144
|
-
() => api.patch(R.one(id), fields),
|
|
145
|
-
),
|
|
146
|
-
deleteCollection: (id) =>
|
|
147
|
-
run(
|
|
148
|
-
(current) => ({
|
|
149
|
-
...current,
|
|
150
|
-
collections: current.collections.filter((c) => c.id !== id),
|
|
151
|
-
}),
|
|
152
|
-
() => api.delete(R.one(id)),
|
|
153
|
-
),
|
|
154
|
-
addToCollection: (id, conversationId) =>
|
|
155
|
-
run(
|
|
156
|
-
edit(id, (c) => ({
|
|
157
|
-
...c,
|
|
158
|
-
conversationIds: toggle(c.conversationIds, conversationId, true),
|
|
159
|
-
})),
|
|
160
|
-
() => api.put(R.member(id, conversationId)),
|
|
161
|
-
),
|
|
73
|
+
createCollection: (kind, fields) => call(createCollection, kind, fields),
|
|
74
|
+
updateCollection: (id, fields) => call(updateCollection, id, fields),
|
|
75
|
+
deleteCollection: (id) => call(deleteCollection, id),
|
|
76
|
+
addToCollection: (id, conversationId) => call(setCollectionMember, id, conversationId, true),
|
|
162
77
|
removeFromCollection: (id, conversationId) =>
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
...c,
|
|
166
|
-
conversationIds: toggle(c.conversationIds, conversationId, false),
|
|
167
|
-
})),
|
|
168
|
-
() => api.delete(R.member(id, conversationId)),
|
|
169
|
-
),
|
|
170
|
-
setPinned: (conversationId, pinned) =>
|
|
171
|
-
run(
|
|
172
|
-
(current) => ({ ...current, pinned: toggle(current.pinned, conversationId, pinned) }),
|
|
173
|
-
() => api.patch(API_ROUTES.conversations.marks(conversationId), { pinned }),
|
|
174
|
-
),
|
|
78
|
+
call(setCollectionMember, id, conversationId, false),
|
|
79
|
+
setPinned: (conversationId, pinned) => call(setConversationPinned, conversationId, pinned),
|
|
175
80
|
setFavorite: (conversationId, favorite) =>
|
|
176
|
-
|
|
177
|
-
(current) => ({
|
|
178
|
-
...current,
|
|
179
|
-
favorites: toggle(current.favorites, conversationId, favorite),
|
|
180
|
-
}),
|
|
181
|
-
() => api.patch(API_ROUTES.conversations.marks(conversationId), { favorite }),
|
|
182
|
-
),
|
|
81
|
+
call(setConversationFavorite, conversationId, favorite),
|
|
183
82
|
};
|
|
184
|
-
}, [api,
|
|
83
|
+
}, [api, store]);
|
|
185
84
|
}
|
|
@@ -1,9 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
action,
|
|
3
|
+
type ActionView,
|
|
4
|
+
type RecordView,
|
|
5
|
+
skip,
|
|
6
|
+
useStore,
|
|
7
|
+
withStore,
|
|
8
|
+
} from '@oxy.so/bloom-data';
|
|
1
9
|
import { useOxy } from '@oxy.so/services/ui/client';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
10
|
+
import { useCallback, useEffect } from 'react';
|
|
11
|
+
import { rereadOn } from '../realtime/reread';
|
|
12
|
+
import { type GemieApi, useGemieApi } from './api';
|
|
13
|
+
import { answerSource, useAnswer } from './data/answer';
|
|
14
|
+
import { ACCOUNT, gemieRead } from './data/read';
|
|
4
15
|
import { errorStatus } from './error-utils';
|
|
5
|
-
import { gemieQueryKeys } from './query-keys';
|
|
6
16
|
import { ACCOUNT_ROUTES } from './routes';
|
|
17
|
+
import { useBoundAction } from './use-bound-action';
|
|
7
18
|
|
|
8
19
|
/**
|
|
9
20
|
* A person's connected accounts (ADR 0035 as amended): the WhatsApp and Telegram (Business,
|
|
@@ -85,65 +96,105 @@ export interface ConnectedAccountsAnswer {
|
|
|
85
96
|
platforms: GemieAccountPlatform[];
|
|
86
97
|
}
|
|
87
98
|
|
|
99
|
+
/**
|
|
100
|
+
* The caller's accounts with their grants, and what can be connected: memory only (phone numbers
|
|
101
|
+
* and a live code never reach the disk). Read again when the realtime `connections` event says they
|
|
102
|
+
* changed, and after a reset.
|
|
103
|
+
*/
|
|
104
|
+
export const connectedAccounts = answerSource('gemies.connected-accounts', {
|
|
105
|
+
...gemieRead,
|
|
106
|
+
key: (_api: GemieApi) => ACCOUNT,
|
|
107
|
+
load: async ({ signal }, api) =>
|
|
108
|
+
(await api.get<ConnectedAccountsAnswer>(ACCOUNT_ROUTES.all, { signal })).data,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
rereadOn('connections', connectedAccounts);
|
|
112
|
+
rereadOn('reset', connectedAccounts);
|
|
113
|
+
|
|
114
|
+
/** The code of an account being connected, as its session reports it now. */
|
|
115
|
+
export const connectedAccountQr = answerSource('gemies.connected-account-qr', {
|
|
116
|
+
...gemieRead,
|
|
117
|
+
key: (_api: GemieApi, accountId: string) => accountId,
|
|
118
|
+
load: async ({ signal }, api, accountId) =>
|
|
119
|
+
(
|
|
120
|
+
await api.get<{ status: GemieAccountStatus; qr: string | null }>(
|
|
121
|
+
`${ACCOUNT_ROUTES.one(accountId)}/qr`,
|
|
122
|
+
{ signal },
|
|
123
|
+
)
|
|
124
|
+
).data,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/** An account's recent chats; one that has none to list (a 404) has none. */
|
|
128
|
+
export const connectedAccountChats = answerSource('gemies.connected-account-chats', {
|
|
129
|
+
...gemieRead,
|
|
130
|
+
key: (_api: GemieApi, accountId: string) => accountId,
|
|
131
|
+
load: async ({ signal }, api, accountId) => {
|
|
132
|
+
try {
|
|
133
|
+
const { data } = await api.get<{ chats: GemieAccountChat[] }>(
|
|
134
|
+
`${ACCOUNT_ROUTES.one(accountId)}/chats`,
|
|
135
|
+
{ signal },
|
|
136
|
+
);
|
|
137
|
+
return data.chats;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (errorStatus(error) === 404) return [];
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
88
145
|
/** The caller's accounts, each as its session is now with its grants, and what can be connected. */
|
|
89
|
-
export function useConnectedAccounts() {
|
|
146
|
+
export function useConnectedAccounts(): RecordView<ConnectedAccountsAnswer> {
|
|
147
|
+
const api = useGemieApi();
|
|
148
|
+
const { canUsePrivateApi } = useOxy();
|
|
149
|
+
return useAnswer(connectedAccounts, ...(canUsePrivateApi ? [api] : [skip]));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Reads the caller's accounts again, in the store of the tree. */
|
|
153
|
+
function useRefreshConnectedAccounts(): () => void {
|
|
90
154
|
const api = useGemieApi();
|
|
91
|
-
const
|
|
92
|
-
return
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return data;
|
|
97
|
-
},
|
|
98
|
-
enabled: isAuthenticated,
|
|
99
|
-
});
|
|
155
|
+
const store = useStore();
|
|
156
|
+
return useCallback(
|
|
157
|
+
() => void withStore(store, () => connectedAccounts.refresh(api)),
|
|
158
|
+
[api, store],
|
|
159
|
+
);
|
|
100
160
|
}
|
|
101
161
|
|
|
162
|
+
/** How often a waiting code is read again: WhatsApp rotates its code every few seconds. */
|
|
163
|
+
const QR_REFRESH_MS = 4000;
|
|
164
|
+
|
|
102
165
|
/**
|
|
103
166
|
* The code of an account being connected, refreshed while it waits (WhatsApp rotates its code
|
|
104
167
|
* every few seconds; Telegram's link is renewed when it expires, and is gone once Telegram knows
|
|
105
|
-
* who the person is). It stops once the session leaves `qr-pending`, and the list is
|
|
106
|
-
* so the account shows as it is.
|
|
168
|
+
* who the person is). It stops once the session leaves `qr-pending`, and the list is read again
|
|
169
|
+
* then, so the account shows as it is. Until the first read, the code the connection answered.
|
|
107
170
|
*/
|
|
108
|
-
export function useConnectedAccountQr(
|
|
171
|
+
export function useConnectedAccountQr(
|
|
172
|
+
link: GemieAccountLink | null,
|
|
173
|
+
): RecordView<{ status: GemieAccountStatus; qr: string | null }> {
|
|
109
174
|
const api = useGemieApi();
|
|
110
|
-
const
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
});
|
|
175
|
+
const view = useAnswer(connectedAccountQr, ...(link ? [api, link.accountId] : [skip]));
|
|
176
|
+
const refreshAccounts = useRefreshConnectedAccounts();
|
|
177
|
+
const status = view.value?.status;
|
|
178
|
+
const { refresh } = view;
|
|
179
|
+
const waiting = link !== null && (status === undefined || status === 'qr-pending');
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
if (!waiting) return;
|
|
182
|
+
const timer = setInterval(() => void refresh(), QR_REFRESH_MS);
|
|
183
|
+
return () => clearInterval(timer);
|
|
184
|
+
}, [waiting, refresh]);
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
if (status !== undefined && status !== 'qr-pending') refreshAccounts();
|
|
187
|
+
}, [status, refreshAccounts]);
|
|
188
|
+
return {
|
|
189
|
+
...view,
|
|
190
|
+
value: view.value ?? (link ? { status: 'qr-pending', qr: link.qr } : undefined),
|
|
191
|
+
};
|
|
128
192
|
}
|
|
129
193
|
|
|
130
194
|
/** An account's recent chats, read while the person chooses which ones it answers. */
|
|
131
|
-
export function useConnectedAccountChats(accountId: string) {
|
|
195
|
+
export function useConnectedAccountChats(accountId: string): RecordView<GemieAccountChat[]> {
|
|
132
196
|
const api = useGemieApi();
|
|
133
|
-
return
|
|
134
|
-
queryKey: gemieQueryKeys.accounts.chats(accountId),
|
|
135
|
-
queryFn: async (): Promise<GemieAccountChat[]> => {
|
|
136
|
-
try {
|
|
137
|
-
const { data } = await api.get<{ chats: GemieAccountChat[] }>(
|
|
138
|
-
`${ACCOUNT_ROUTES.one(accountId)}/chats`,
|
|
139
|
-
);
|
|
140
|
-
return data.chats;
|
|
141
|
-
} catch (error) {
|
|
142
|
-
if (errorStatus(error) === 404) return [];
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
});
|
|
197
|
+
return useAnswer(connectedAccountChats, api, accountId);
|
|
147
198
|
}
|
|
148
199
|
|
|
149
200
|
/** What can be changed of an account: who answers it, and whom that gemie answers. */
|
|
@@ -155,44 +206,80 @@ export interface ConnectedAccountPatch {
|
|
|
155
206
|
groupReplies?: GemieAccountGroupReplies;
|
|
156
207
|
}
|
|
157
208
|
|
|
158
|
-
/**
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
209
|
+
/** Draws a change to the held accounts, as the calling action's layer. */
|
|
210
|
+
function drawAccounts(change: (accounts: ConnectedAccount[]) => ConnectedAccount[]): void {
|
|
211
|
+
connectedAccounts.entity.update(ACCOUNT, (held) => ({
|
|
212
|
+
...held,
|
|
213
|
+
value: { ...held.value, accounts: change([...held.value.accounts]) },
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Every write ends with the list read again, so each screen shows the accounts as they now are. */
|
|
218
|
+
const rereadAccounts = (api: GemieApi) => void connectedAccounts.refresh(api);
|
|
219
|
+
|
|
220
|
+
/** Connects an account on a platform: the code (or link) that finishes it. */
|
|
221
|
+
export const connectAccount = action({
|
|
222
|
+
run: async (api: GemieApi, platform: GemieAccountPlatform) =>
|
|
223
|
+
(await api.post<GemieAccountLink>(ACCOUNT_ROUTES.all, { platform })).data,
|
|
224
|
+
settle: (_link, api) => rereadAccounts(api),
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
/** Changes who answers an account and whom: drawn at once, put back if refused. */
|
|
228
|
+
export const updateAccount = action({
|
|
229
|
+
optimistic: (_api: GemieApi, accountId: string, patch: ConnectedAccountPatch) =>
|
|
230
|
+
drawAccounts((accounts) =>
|
|
231
|
+
accounts.map((account) => (account._id === accountId ? { ...account, ...patch } : account)),
|
|
232
|
+
),
|
|
233
|
+
run: async (api, accountId, patch) => {
|
|
234
|
+
await api.patch(ACCOUNT_ROUTES.one(accountId), patch);
|
|
235
|
+
},
|
|
236
|
+
settle: (_result, api) => rereadAccounts(api),
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
/** Grants an account to a gemie (`granted: true`) or revokes it: drawn at once, put back if refused. */
|
|
240
|
+
export const grantAccount = action({
|
|
241
|
+
optimistic: (_api: GemieApi, accountId: string, gemieId: string, granted: boolean) =>
|
|
242
|
+
drawAccounts((accounts) =>
|
|
243
|
+
accounts.map((account) =>
|
|
244
|
+
account._id !== accountId
|
|
245
|
+
? account
|
|
246
|
+
: {
|
|
247
|
+
...account,
|
|
248
|
+
gemieIds: granted
|
|
249
|
+
? [...account.gemieIds.filter((id) => id !== gemieId), gemieId]
|
|
250
|
+
: account.gemieIds.filter((id) => id !== gemieId),
|
|
251
|
+
},
|
|
252
|
+
),
|
|
253
|
+
),
|
|
254
|
+
run: async (api, accountId, gemieId, granted) => {
|
|
255
|
+
const path = ACCOUNT_ROUTES.grant(accountId, gemieId);
|
|
256
|
+
await (granted ? api.put(path) : api.delete(path));
|
|
257
|
+
},
|
|
258
|
+
settle: (_result, api) => rereadAccounts(api),
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
/** Removes an account (its session ends and its grants go): gone at once, back if refused. */
|
|
262
|
+
export const removeAccount = action({
|
|
263
|
+
optimistic: (_api: GemieApi, accountId: string) =>
|
|
264
|
+
drawAccounts((accounts) => accounts.filter((account) => account._id !== accountId)),
|
|
265
|
+
run: async (api, accountId) => {
|
|
266
|
+
await api.delete(ACCOUNT_ROUTES.one(accountId));
|
|
267
|
+
},
|
|
268
|
+
settle: (_result, api) => rereadAccounts(api),
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
/** Connecting, granting, who answers, and removing, as a component calls them. */
|
|
272
|
+
export function useConnectedAccountMutations(): {
|
|
273
|
+
connect: ActionView<[platform: GemieAccountPlatform], GemieAccountLink>;
|
|
274
|
+
update: ActionView<[accountId: string, patch: ConnectedAccountPatch], void>;
|
|
275
|
+
grant: ActionView<[accountId: string, gemieId: string, granted: boolean], void>;
|
|
276
|
+
remove: ActionView<[accountId: string], void>;
|
|
277
|
+
} {
|
|
163
278
|
const api = useGemieApi();
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return data;
|
|
171
|
-
},
|
|
172
|
-
onSuccess: invalidate,
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
const update = useMutation({
|
|
176
|
-
mutationFn: async ({ accountId, ...patch }: ConnectedAccountPatch & { accountId: string }) => {
|
|
177
|
-
await api.patch(ACCOUNT_ROUTES.one(accountId), patch);
|
|
178
|
-
},
|
|
179
|
-
onSuccess: invalidate,
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
const grant = useMutation({
|
|
183
|
-
mutationFn: async (input: { accountId: string; gemieId: string; granted: boolean }) => {
|
|
184
|
-
const path = ACCOUNT_ROUTES.grant(input.accountId, input.gemieId);
|
|
185
|
-
await (input.granted ? api.put(path) : api.delete(path));
|
|
186
|
-
},
|
|
187
|
-
onSuccess: invalidate,
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
const remove = useMutation({
|
|
191
|
-
mutationFn: async (accountId: string) => {
|
|
192
|
-
await api.delete(ACCOUNT_ROUTES.one(accountId));
|
|
193
|
-
},
|
|
194
|
-
onSuccess: invalidate,
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
return { connect, update, grant, remove };
|
|
279
|
+
return {
|
|
280
|
+
connect: useBoundAction(connectAccount, api),
|
|
281
|
+
update: useBoundAction(updateAccount, api),
|
|
282
|
+
grant: useBoundAction(grantAccount, api),
|
|
283
|
+
remove: useBoundAction(removeAccount, api),
|
|
284
|
+
};
|
|
198
285
|
}
|