@gemies/sdk 6.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 +10 -0
- package/package.json +1 -1
- package/src/realtime/cache.ts +8 -0
- package/src/realtime/contract.ts +21 -0
- package/src/runtime/collections-model.ts +66 -0
- package/src/runtime/collections.ts +185 -0
- package/src/runtime/index.ts +13 -0
- package/src/runtime/query-keys.ts +2 -0
- package/src/runtime/routes.ts +11 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @gemies/sdk
|
|
2
2
|
|
|
3
|
+
## 6.1.0
|
|
4
|
+
|
|
5
|
+
- **New: a person's folders, projects, pins and favourites are their account's**, the same on every
|
|
6
|
+
device ([Realtime](../../docs/realtime.mdx#collections-folders-projects-pins-and-favourites)).
|
|
7
|
+
`useConversationCollections()` reads the snapshot (`{ collections, pinned, favorites }`), kept by
|
|
8
|
+
the realtime `collections` event; `useCollectionActions()` creates, edits, deletes and files, and
|
|
9
|
+
pins and favourites a conversation, drawing each change at once; `importCollections(api, local)`
|
|
10
|
+
brings what a device kept in its storage into the account, merging by name.
|
|
11
|
+
- `API_ROUTES.collections` and `API_ROUTES.conversations.marks`; `gemieQueryKeys.collections`.
|
|
12
|
+
|
|
3
13
|
## 6.0.0
|
|
4
14
|
|
|
5
15
|
- **Breaking: realtime over one socket ([Realtime](../../docs/realtime.mdx), Gemies ADR 0040).**
|
package/package.json
CHANGED
package/src/realtime/cache.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { updateReactionCaches } from '../runtime/reaction-cache';
|
|
|
15
15
|
import type { Conversation, ConversationsInfiniteData, Message } from '../runtime/types';
|
|
16
16
|
import type { RealtimeConnection } from './connection';
|
|
17
17
|
import type { RealtimeConversationRow, RealtimeData, RealtimeFrame } from './contract';
|
|
18
|
+
import { readCollectionsSnapshot } from '../runtime/collections-model';
|
|
18
19
|
|
|
19
20
|
function toConversation(row: RealtimeConversationRow): Conversation {
|
|
20
21
|
return {
|
|
@@ -182,6 +183,12 @@ export function applyRealtimeToCache(client: QueryClient, frame: RealtimeFrame):
|
|
|
182
183
|
updateReactionCaches(client, { ...update, reactions: [...update.reactions] });
|
|
183
184
|
}
|
|
184
185
|
return;
|
|
186
|
+
case 'collections':
|
|
187
|
+
client.setQueryData(
|
|
188
|
+
gemieQueryKeys.collections,
|
|
189
|
+
readCollectionsSnapshot(frame.data as RealtimeData['collections']),
|
|
190
|
+
);
|
|
191
|
+
return;
|
|
185
192
|
case 'connections':
|
|
186
193
|
// The person's connected accounts and connectors: their lists are read again, once.
|
|
187
194
|
void client.invalidateQueries({ queryKey: gemieQueryKeys.accounts.all });
|
|
@@ -195,6 +202,7 @@ export function applyRealtimeToCache(client: QueryClient, frame: RealtimeFrame):
|
|
|
195
202
|
export function resetRealtimeCache(client: QueryClient): void {
|
|
196
203
|
void client.invalidateQueries({ queryKey: gemieQueryKeys.conversations.all });
|
|
197
204
|
void client.invalidateQueries({ queryKey: ['conversation'] });
|
|
205
|
+
void client.invalidateQueries({ queryKey: gemieQueryKeys.collections });
|
|
198
206
|
void client.invalidateQueries({ queryKey: gemieQueryKeys.accounts.all });
|
|
199
207
|
void client.invalidateQueries({ queryKey: ['gemie-connectors', 'connected'] });
|
|
200
208
|
}
|
package/src/realtime/contract.ts
CHANGED
|
@@ -26,6 +26,7 @@ export const REALTIME_DURABLE_TYPES = [
|
|
|
26
26
|
'credits',
|
|
27
27
|
'notification',
|
|
28
28
|
'connections',
|
|
29
|
+
'collections',
|
|
29
30
|
] as const;
|
|
30
31
|
|
|
31
32
|
/** Signals: only true while they happen, never replayed. */
|
|
@@ -89,6 +90,25 @@ export interface RealtimeActivity {
|
|
|
89
90
|
readonly ttlMs: number;
|
|
90
91
|
}
|
|
91
92
|
|
|
93
|
+
/** A person's folders, projects, pins and favourites, as the API answers and publishes them. */
|
|
94
|
+
export interface CollectionsSnapshotWire {
|
|
95
|
+
readonly collections: ReadonlyArray<{
|
|
96
|
+
readonly id: string;
|
|
97
|
+
readonly kind: 'folder' | 'project';
|
|
98
|
+
readonly name: string;
|
|
99
|
+
readonly description: string | null;
|
|
100
|
+
readonly icon: string | null;
|
|
101
|
+
readonly color: string | null;
|
|
102
|
+
readonly isFavorite: boolean;
|
|
103
|
+
readonly isExpanded: boolean;
|
|
104
|
+
readonly conversationIds: string[];
|
|
105
|
+
readonly createdAt: string;
|
|
106
|
+
readonly updatedAt: string;
|
|
107
|
+
}>;
|
|
108
|
+
readonly pinned: string[];
|
|
109
|
+
readonly favorites: string[];
|
|
110
|
+
}
|
|
111
|
+
|
|
92
112
|
export interface RealtimeData {
|
|
93
113
|
conversation: {
|
|
94
114
|
readonly conversation: RealtimeConversationRow;
|
|
@@ -113,6 +133,7 @@ export interface RealtimeData {
|
|
|
113
133
|
credits: { readonly credits?: number };
|
|
114
134
|
notification: { readonly notification: Record<string, unknown> & { readonly id: string } };
|
|
115
135
|
connections: Record<string, never>;
|
|
136
|
+
collections: CollectionsSnapshotWire;
|
|
116
137
|
activity: RealtimeActivity;
|
|
117
138
|
room: {
|
|
118
139
|
readonly conversationId: string;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The data of a person's collections, without React (`runtime/collections.ts` has the hooks), so the
|
|
3
|
+
* realtime cache can read a `collections` frame without loading the UI client.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { CollectionsSnapshotWire } from '../realtime/contract';
|
|
7
|
+
|
|
8
|
+
export type ConversationCollectionKind = 'folder' | 'project';
|
|
9
|
+
|
|
10
|
+
export interface ConversationCollection {
|
|
11
|
+
id: string;
|
|
12
|
+
kind: ConversationCollectionKind;
|
|
13
|
+
name: string;
|
|
14
|
+
description: string | null;
|
|
15
|
+
icon: string | null;
|
|
16
|
+
color: string | null;
|
|
17
|
+
isFavorite: boolean;
|
|
18
|
+
isExpanded: boolean;
|
|
19
|
+
conversationIds: string[];
|
|
20
|
+
/** ISO 8601: the snapshot is persisted as JSON by hosts that keep their cache. */
|
|
21
|
+
createdAt: string;
|
|
22
|
+
updatedAt: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ConversationCollections {
|
|
26
|
+
collections: ConversationCollection[];
|
|
27
|
+
/** Pinned conversations, oldest pin first. */
|
|
28
|
+
pinned: string[];
|
|
29
|
+
favorites: string[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What a collection may be created or edited with. */
|
|
33
|
+
export interface CollectionFields {
|
|
34
|
+
name?: string;
|
|
35
|
+
description?: string | null;
|
|
36
|
+
icon?: string | null;
|
|
37
|
+
color?: string | null;
|
|
38
|
+
isFavorite?: boolean;
|
|
39
|
+
isExpanded?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** What a device filed before collections were the account's (`importCollections`). */
|
|
43
|
+
export interface LocalCollections {
|
|
44
|
+
pinned: string[];
|
|
45
|
+
favorites: string[];
|
|
46
|
+
folders: Array<CollectionFields & { name: string; conversationIds: string[] }>;
|
|
47
|
+
projects: Array<CollectionFields & { name: string; conversationIds: string[] }>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const EMPTY_COLLECTIONS: ConversationCollections = {
|
|
51
|
+
collections: [],
|
|
52
|
+
pinned: [],
|
|
53
|
+
favorites: [],
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** The wire snapshot as the runtime keeps it (a copy: the cache never shares a frame's arrays). */
|
|
57
|
+
export function readCollectionsSnapshot(wire: CollectionsSnapshotWire): ConversationCollections {
|
|
58
|
+
return {
|
|
59
|
+
collections: wire.collections.map((c) => ({
|
|
60
|
+
...c,
|
|
61
|
+
conversationIds: [...c.conversationIds],
|
|
62
|
+
})),
|
|
63
|
+
pinned: [...wire.pinned],
|
|
64
|
+
favorites: [...wire.favorites],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
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").
|
|
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.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
11
|
+
import { useOxy } from '@oxy.so/services/ui/client';
|
|
12
|
+
import { useCallback, useMemo } from 'react';
|
|
13
|
+
import type { CollectionsSnapshotWire } from '../realtime/contract';
|
|
14
|
+
import {
|
|
15
|
+
type CollectionFields,
|
|
16
|
+
type ConversationCollection,
|
|
17
|
+
type ConversationCollectionKind,
|
|
18
|
+
type ConversationCollections,
|
|
19
|
+
EMPTY_COLLECTIONS,
|
|
20
|
+
type LocalCollections,
|
|
21
|
+
readCollectionsSnapshot,
|
|
22
|
+
} from './collections-model';
|
|
23
|
+
|
|
24
|
+
export type {
|
|
25
|
+
CollectionFields,
|
|
26
|
+
ConversationCollection,
|
|
27
|
+
ConversationCollectionKind,
|
|
28
|
+
ConversationCollections,
|
|
29
|
+
LocalCollections,
|
|
30
|
+
} from './collections-model';
|
|
31
|
+
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
|
+
/** The person's folders, projects, pins and favourites. Kept current by the realtime connection. */
|
|
37
|
+
export function useConversationCollections(): ConversationCollections {
|
|
38
|
+
const api = useGemieApi();
|
|
39
|
+
const { canUsePrivateApi, isAccountCacheReady } = useOxy();
|
|
40
|
+
const query = useQuery({
|
|
41
|
+
queryKey: gemieQueryKeys.collections,
|
|
42
|
+
queryFn: async () =>
|
|
43
|
+
readCollectionsSnapshot(
|
|
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);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface CollectionActions {
|
|
67
|
+
createCollection(
|
|
68
|
+
kind: ConversationCollectionKind,
|
|
69
|
+
fields: CollectionFields & { name: string },
|
|
70
|
+
): Promise<void>;
|
|
71
|
+
updateCollection(id: string, fields: CollectionFields): Promise<void>;
|
|
72
|
+
deleteCollection(id: string): Promise<void>;
|
|
73
|
+
addToCollection(id: string, conversationId: string): Promise<void>;
|
|
74
|
+
removeFromCollection(id: string, conversationId: string): Promise<void>;
|
|
75
|
+
setPinned(conversationId: string, pinned: boolean): Promise<void>;
|
|
76
|
+
setFavorite(conversationId: string, favorite: boolean): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 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.
|
|
82
|
+
*/
|
|
83
|
+
export function useCollectionActions(): CollectionActions {
|
|
84
|
+
const api = useGemieApi();
|
|
85
|
+
const queryClient = useQueryClient();
|
|
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
|
+
|
|
107
|
+
return useMemo<CollectionActions>(() => {
|
|
108
|
+
const R = API_ROUTES.collections;
|
|
109
|
+
const edit =
|
|
110
|
+
(id: string, change: (c: ConversationCollection) => ConversationCollection) =>
|
|
111
|
+
(current: ConversationCollections) => ({
|
|
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);
|
|
117
|
+
return {
|
|
118
|
+
createCollection: (kind, fields) =>
|
|
119
|
+
run(
|
|
120
|
+
(current) => ({
|
|
121
|
+
...current,
|
|
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
|
+
),
|
|
162
|
+
removeFromCollection: (id, conversationId) =>
|
|
163
|
+
run(
|
|
164
|
+
edit(id, (c) => ({
|
|
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
|
+
),
|
|
175
|
+
setFavorite: (conversationId, favorite) =>
|
|
176
|
+
run(
|
|
177
|
+
(current) => ({
|
|
178
|
+
...current,
|
|
179
|
+
favorites: toggle(current.favorites, conversationId, favorite),
|
|
180
|
+
}),
|
|
181
|
+
() => api.patch(API_ROUTES.conversations.marks(conversationId), { favorite }),
|
|
182
|
+
),
|
|
183
|
+
};
|
|
184
|
+
}, [api, run]);
|
|
185
|
+
}
|
package/src/runtime/index.ts
CHANGED
|
@@ -100,6 +100,19 @@ export {
|
|
|
100
100
|
useSaveConversation,
|
|
101
101
|
} from './conversations';
|
|
102
102
|
export { useConversationThread, mergeEditedContent } from './use-conversation-thread';
|
|
103
|
+
export {
|
|
104
|
+
importCollections,
|
|
105
|
+
useCollectionActions,
|
|
106
|
+
useConversationCollections,
|
|
107
|
+
} from './collections';
|
|
108
|
+
export type {
|
|
109
|
+
CollectionActions,
|
|
110
|
+
CollectionFields,
|
|
111
|
+
ConversationCollection,
|
|
112
|
+
ConversationCollectionKind,
|
|
113
|
+
ConversationCollections,
|
|
114
|
+
LocalCollections,
|
|
115
|
+
} from './collections';
|
|
103
116
|
export type {
|
|
104
117
|
ConversationThread,
|
|
105
118
|
EditedContent,
|
|
@@ -11,6 +11,8 @@ export interface ConversationsFilter {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export const gemieQueryKeys = {
|
|
14
|
+
/** The person's folders, projects, pins and favourites (`runtime/collections.ts`). */
|
|
15
|
+
collections: ['conversation-collections'] as const,
|
|
14
16
|
conversations: {
|
|
15
17
|
all: ['conversations'] as const,
|
|
16
18
|
detail: (id: string) => ['conversation', id] as const,
|
package/src/runtime/routes.ts
CHANGED
|
@@ -13,6 +13,15 @@ export const API_ROUTES = {
|
|
|
13
13
|
chat: '/v1/chat/completions',
|
|
14
14
|
/** The answer to an awaited host action, read by the turn that is waiting for it. */
|
|
15
15
|
hostResults: (turnId: string) => `/v1/chat/turns/${encodeURIComponent(turnId)}/host-results`,
|
|
16
|
+
/** The person's folders and projects (`runtime/collections.ts`). */
|
|
17
|
+
collections: {
|
|
18
|
+
list: '/v1/conversations/collections',
|
|
19
|
+
create: '/v1/conversations/collections',
|
|
20
|
+
import: '/v1/conversations/collections/import',
|
|
21
|
+
one: (id: string) => `/v1/conversations/collections/${encodeURIComponent(id)}`,
|
|
22
|
+
member: (id: string, conversationId: string) =>
|
|
23
|
+
`/v1/conversations/collections/${encodeURIComponent(id)}/conversations/${encodeURIComponent(conversationId)}`,
|
|
24
|
+
},
|
|
16
25
|
conversations: {
|
|
17
26
|
list: '/v1/conversations',
|
|
18
27
|
save: '/v1/conversations',
|
|
@@ -27,6 +36,8 @@ export const API_ROUTES = {
|
|
|
27
36
|
clearMessages: (id: string) => `/v1/conversations/${id}/messages`,
|
|
28
37
|
/** The person has seen it: its unread count goes to zero on every surface. */
|
|
29
38
|
read: (id: string) => `/v1/conversations/${id}/read`,
|
|
39
|
+
/** Its own marks: `{ pinned?, favorite? }`. */
|
|
40
|
+
marks: (id: string) => `/v1/conversations/${id}`,
|
|
30
41
|
vote: (id: string, messageId: string) => `/v1/conversations/${id}/messages/${messageId}/vote`,
|
|
31
42
|
react: (id: string, messageId: string) =>
|
|
32
43
|
`/v1/conversations/${id}/messages/${messageId}/reactions`,
|