@gemies/sdk 9.1.0 → 10.0.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 +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 +65 -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 +118 -0
- package/src/components/thread/ChatBubble.tsx +66 -25
- package/src/components/thread/GemieThread.tsx +36 -8
- 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/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/copy-en.ts +7 -0
- package/src/lib/copy-es.ts +7 -0
- 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/approvals.ts +99 -0
- package/src/runtime/collections.ts +35 -136
- package/src/runtime/config.tsx +10 -0
- 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 +74 -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 +47 -31
- package/src/runtime/use-conversation-thread.ts +44 -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
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A person's thread with a gemie, in the signed-in account's store: which stretch is active (the
|
|
3
|
+
* thread itself), everything said before it a page at a time (its history), a stretch of the past
|
|
4
|
+
* opened from a search hit (a window), and the search itself.
|
|
5
|
+
*
|
|
6
|
+
* History and windows are memory only; the thread itself is copied to the account's durable store,
|
|
7
|
+
* so a reopened thread knows its active stretch from the first frame.
|
|
8
|
+
*/
|
|
9
|
+
import { entity, list, source } from '@oxy.so/bloom-data';
|
|
10
|
+
import type { GemieApi } from '../api';
|
|
11
|
+
import { errorStatus } from '../error-utils';
|
|
12
|
+
import { AGENT_ROUTES, type GemieThreadAddress, threadKey } from '../routes';
|
|
13
|
+
import type { ThreadPage, ThreadSearchHit, WireThreadMessage } from '../thread/thread-history';
|
|
14
|
+
import { answerSource } from './answer';
|
|
15
|
+
import { gemieRead } from './read';
|
|
16
|
+
|
|
17
|
+
/** A gemie's thread with the signed-in person, as `GET /agents/:id/thread` answers it. */
|
|
18
|
+
export interface GemieThreadInfo {
|
|
19
|
+
/** Who the thread is with. `name`, `handle` and `color` come from Oxy and may be `null` (Alia's do, until her account exists). */
|
|
20
|
+
readonly agent: {
|
|
21
|
+
readonly _id: string;
|
|
22
|
+
readonly name: string | null;
|
|
23
|
+
readonly handle: string | null;
|
|
24
|
+
readonly character: unknown;
|
|
25
|
+
readonly tagline: string;
|
|
26
|
+
readonly description: string;
|
|
27
|
+
};
|
|
28
|
+
/** The active stretch: `GemieChatContent`'s `conversationId`, where the next turn goes. */
|
|
29
|
+
readonly conversationId: string;
|
|
30
|
+
/**
|
|
31
|
+
* The thread row, for a gemie whose chats are loose (Alia): her thread is apart from them, and its
|
|
32
|
+
* next stretch is `useCreateConversation().run({ threadId })`. `null` for any other gemie, whose
|
|
33
|
+
* thread is every conversation the two hold.
|
|
34
|
+
*/
|
|
35
|
+
readonly threadId: string | null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** How long a thread read is good; starting a stretch reads it again. */
|
|
39
|
+
const THREAD_STALE_MS = 5 * 60_000;
|
|
40
|
+
/** How many messages a history page carries. The server clamps its own maximum. */
|
|
41
|
+
const PAGE_SIZE = 50;
|
|
42
|
+
/** How many messages a window carries, half of them before the hit. */
|
|
43
|
+
const WINDOW_SIZE = 40;
|
|
44
|
+
/** How many hits a search asks for. */
|
|
45
|
+
const HIT_LIMIT = 30;
|
|
46
|
+
|
|
47
|
+
/** A 404 is the answer (the gemie does not exist or is not the person's): asking again does not change it. */
|
|
48
|
+
export function retryThreadRead(failureCount: number, error: unknown): boolean {
|
|
49
|
+
return errorStatus(error) !== 404 && failureCount <= 2;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The thread: its first open CREATES it and its first stretch, so it is read only when the person
|
|
54
|
+
* opens it, never to prefetch one nobody started. Keyed by `threadKey(address)`.
|
|
55
|
+
*/
|
|
56
|
+
export const gemieThread = answerSource('gemies.thread', {
|
|
57
|
+
...gemieRead,
|
|
58
|
+
persist: true,
|
|
59
|
+
key: (_api: GemieApi, thread: GemieThreadAddress) => threadKey(thread),
|
|
60
|
+
stale: THREAD_STALE_MS,
|
|
61
|
+
retry: retryThreadRead,
|
|
62
|
+
load: async ({ signal }, api, thread) =>
|
|
63
|
+
(await api.get<GemieThreadInfo>(AGENT_ROUTES.thread(thread), { signal })).data,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A message of a thread, by its cursor (unique to one message by construction). `message` finds
|
|
68
|
+
* one by its conversation and its id (the cursor of a message the server wrote, which has none).
|
|
69
|
+
*/
|
|
70
|
+
export const ThreadMessages = entity<WireThreadMessage>('gemies.thread-message', {
|
|
71
|
+
id: (message) => message.cursor,
|
|
72
|
+
index: {
|
|
73
|
+
message: (message) => threadMessageKey(message.conversationId, message.id ?? message.cursor),
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
/** The `message` index value of a thread message. */
|
|
78
|
+
export function threadMessageKey(conversationId: string, messageId: string): string {
|
|
79
|
+
return `${conversationId}\u0000${messageId}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A page as a list holds it: newest first. The server sends each page oldest-first and each page is
|
|
84
|
+
* the window before the last, so reversing each page keeps the whole list newest-first, and the
|
|
85
|
+
* list read backwards is reading order.
|
|
86
|
+
*/
|
|
87
|
+
function newestFirst(page: ThreadPage) {
|
|
88
|
+
return { items: [...page.messages].reverse(), next: page.nextCursor };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Everything said in a thread, going back from its present, newest first. */
|
|
92
|
+
export const ThreadHistory = list('gemies.thread-history', { of: ThreadMessages });
|
|
93
|
+
|
|
94
|
+
export const threadHistory = source(ThreadHistory, {
|
|
95
|
+
...gemieRead,
|
|
96
|
+
key: (_api: GemieApi, thread: GemieThreadAddress) => threadKey(thread),
|
|
97
|
+
retry: retryThreadRead,
|
|
98
|
+
load: async ({ cursor, signal }, api, thread) =>
|
|
99
|
+
newestFirst(
|
|
100
|
+
(
|
|
101
|
+
await api.get<ThreadPage>(AGENT_ROUTES.threadMessages(thread), {
|
|
102
|
+
signal,
|
|
103
|
+
params: { limit: PAGE_SIZE, before: cursor },
|
|
104
|
+
})
|
|
105
|
+
).data,
|
|
106
|
+
),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
/** A stretch of a thread's past around one message, newest first, opened from a search hit. */
|
|
110
|
+
export const ThreadWindow = list('gemies.thread-window', { of: ThreadMessages });
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* `at=` rather than `before=` for the first page: `before` is exclusive, so the one message the
|
|
114
|
+
* window exists to reveal would be the one missing from it. Further back it pages like the history.
|
|
115
|
+
*/
|
|
116
|
+
export const threadWindow = source(ThreadWindow, {
|
|
117
|
+
...gemieRead,
|
|
118
|
+
key: (_api: GemieApi, thread: GemieThreadAddress, at: string) =>
|
|
119
|
+
`${threadKey(thread)}\u0000${at}`,
|
|
120
|
+
retry: retryThreadRead,
|
|
121
|
+
load: async ({ cursor, signal }, api, thread, at) =>
|
|
122
|
+
newestFirst(
|
|
123
|
+
(
|
|
124
|
+
await api.get<ThreadPage>(AGENT_ROUTES.threadMessages(thread), {
|
|
125
|
+
signal,
|
|
126
|
+
params:
|
|
127
|
+
cursor === undefined
|
|
128
|
+
? { limit: WINDOW_SIZE, at }
|
|
129
|
+
: { limit: WINDOW_SIZE, before: cursor },
|
|
130
|
+
})
|
|
131
|
+
).data,
|
|
132
|
+
),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
/** What was said cannot change, so a query already asked is answered from the store. */
|
|
136
|
+
export const threadSearch = answerSource('gemies.thread-search', {
|
|
137
|
+
...gemieRead,
|
|
138
|
+
key: (_api: GemieApi, thread: GemieThreadAddress, query: string) =>
|
|
139
|
+
`${threadKey(thread)}\u0000${query}`,
|
|
140
|
+
stale: 5 * 60_000,
|
|
141
|
+
retry: retryThreadRead,
|
|
142
|
+
load: async ({ signal }, api, thread, query) =>
|
|
143
|
+
(
|
|
144
|
+
await api.get<{ hits: ThreadSearchHit[] }>(AGENT_ROUTES.threadSearch(thread), {
|
|
145
|
+
signal,
|
|
146
|
+
params: { q: query, limit: HIT_LIMIT },
|
|
147
|
+
})
|
|
148
|
+
).data.hits,
|
|
149
|
+
});
|
package/src/runtime/index.ts
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
* Layers, each depending only on the ones above it in this list:
|
|
5
5
|
*
|
|
6
6
|
* 1. transport — `api` (REST over the signed-in Oxy client), the turn's SSE stream
|
|
7
|
-
* 2. model and state — `types`,
|
|
7
|
+
* 2. model and state — `types`, the zustand stores, and the account's data (`data/*`): the
|
|
8
|
+
* conversations, their lists, the collections and the threads in the signed-in account's
|
|
9
|
+
* `@oxy.so/bloom-data` store, with the writes that change them
|
|
8
10
|
* 3. hooks — `useConversations`…, `useConversationThread`
|
|
9
11
|
*
|
|
10
12
|
* Nothing here draws anything. It is also a leaf entry, `@gemies/sdk/runtime`, for a host that
|
|
@@ -12,6 +14,8 @@
|
|
|
12
14
|
*/
|
|
13
15
|
|
|
14
16
|
export { GemieRuntimeProvider, useGemieRuntime } from './config';
|
|
17
|
+
export { ApprovalExpiredError } from './approvals';
|
|
18
|
+
export type { ApprovalDecision, ApprovalEntry, ApprovalRequest } from './approvals';
|
|
15
19
|
export type {
|
|
16
20
|
GemieArtifact,
|
|
17
21
|
GemieRuntimeConfig,
|
|
@@ -34,6 +38,8 @@ export {
|
|
|
34
38
|
export { useConversationActivity } from '../realtime/activity';
|
|
35
39
|
export { GemieApiError, useGemieApi } from './api';
|
|
36
40
|
export {
|
|
41
|
+
agentFeatures,
|
|
42
|
+
agentProfile,
|
|
37
43
|
GEMIE_FEATURES,
|
|
38
44
|
featureOffer,
|
|
39
45
|
readAgentFeatures,
|
|
@@ -75,7 +81,10 @@ export type { MemberTurnAgent, MemberTurnEvent } from './stream-events';
|
|
|
75
81
|
export {
|
|
76
82
|
DEFAULT_SHARE_TTL_HOURS,
|
|
77
83
|
readSharedConversation,
|
|
84
|
+
revokeConversationShare,
|
|
85
|
+
shareConversation,
|
|
78
86
|
SharedConversationGoneError,
|
|
87
|
+
sharedConversation,
|
|
79
88
|
sharedMessages,
|
|
80
89
|
useShareConversation,
|
|
81
90
|
useSharedConversation,
|
|
@@ -90,25 +99,82 @@ export type {
|
|
|
90
99
|
export { uploadToOxyFiles, useDocumentUpload } from './oxy-files';
|
|
91
100
|
export type { OxyFilesClient } from './oxy-files';
|
|
92
101
|
export type { GemieApi, GemieApiRequest, GemieApiResult } from './api';
|
|
93
|
-
export { gemieQueryKeys, type ConversationsFilter } from './query-keys';
|
|
94
102
|
export {
|
|
95
|
-
ConversationNotFoundError,
|
|
96
103
|
useClearConversation,
|
|
97
104
|
useConversation,
|
|
98
105
|
useConversations,
|
|
106
|
+
useConversationsClient,
|
|
99
107
|
useCreateConversation,
|
|
100
108
|
useDeleteConversation,
|
|
101
109
|
useMarkConversationRead,
|
|
102
110
|
usePrefetchConversation,
|
|
111
|
+
useRefreshConversation,
|
|
103
112
|
useRenameConversation,
|
|
104
113
|
useSaveConversation,
|
|
105
114
|
} from './conversations';
|
|
106
|
-
export {
|
|
115
|
+
export type { ConversationsView } from './conversations';
|
|
116
|
+
// The account's data, for a host that reads or writes it beside the hooks (a sidebar).
|
|
117
|
+
export {
|
|
118
|
+
clearConversation,
|
|
119
|
+
ConversationList,
|
|
120
|
+
ConversationMessages,
|
|
121
|
+
ConversationNotFoundError,
|
|
122
|
+
Conversations,
|
|
123
|
+
conversation,
|
|
124
|
+
conversationList,
|
|
125
|
+
conversationsKey,
|
|
126
|
+
createConversation,
|
|
127
|
+
deleteConversation,
|
|
128
|
+
fetchConversation,
|
|
129
|
+
filterOfKey,
|
|
130
|
+
forgetConversation,
|
|
131
|
+
heldConversation,
|
|
132
|
+
MAX_PERSISTED_BYTES,
|
|
133
|
+
MAX_PERSISTED_CONVERSATIONS,
|
|
134
|
+
MAX_PERSISTED_MESSAGES,
|
|
135
|
+
markConversationRead,
|
|
136
|
+
messagesRecord,
|
|
137
|
+
matchesFilter,
|
|
138
|
+
OfflineConversations,
|
|
139
|
+
placeConversation,
|
|
140
|
+
refreshConversationLists,
|
|
141
|
+
renameConversation,
|
|
142
|
+
saveConversation,
|
|
143
|
+
trimConversationMessages,
|
|
144
|
+
} from './data/conversations';
|
|
145
|
+
export type {
|
|
146
|
+
ConversationMessagesRecord,
|
|
147
|
+
ConversationsClient,
|
|
148
|
+
ConversationsFilter,
|
|
149
|
+
CreateConversationInput,
|
|
150
|
+
SaveConversationInput,
|
|
151
|
+
} from './data/conversations';
|
|
107
152
|
export {
|
|
153
|
+
conversationCollections,
|
|
154
|
+
createCollection,
|
|
155
|
+
deleteCollection,
|
|
108
156
|
importCollections,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
157
|
+
setCollectionMember,
|
|
158
|
+
setConversationFavorite,
|
|
159
|
+
setConversationPinned,
|
|
160
|
+
storeCollections,
|
|
161
|
+
updateCollection,
|
|
162
|
+
} from './data/collections';
|
|
163
|
+
export {
|
|
164
|
+
gemieThread,
|
|
165
|
+
retryThreadRead,
|
|
166
|
+
ThreadHistory,
|
|
167
|
+
threadHistory as threadHistorySource,
|
|
168
|
+
ThreadMessages,
|
|
169
|
+
threadSearch,
|
|
170
|
+
ThreadWindow,
|
|
171
|
+
threadWindow,
|
|
172
|
+
} from './data/threads';
|
|
173
|
+
export { answerSource, useAnswer } from './data/answer';
|
|
174
|
+
export type { Answer, AnswerSource, AnswerSourceOptions } from './data/answer';
|
|
175
|
+
export { ACCOUNT, gemieRead } from './data/read';
|
|
176
|
+
export { useConversationThread, mergeEditedContent } from './use-conversation-thread';
|
|
177
|
+
export { useCollectionActions, useConversationCollections } from './collections';
|
|
112
178
|
export type {
|
|
113
179
|
CollectionActions,
|
|
114
180
|
CollectionFields,
|
|
@@ -147,7 +213,7 @@ export type {
|
|
|
147
213
|
} from './thought-store';
|
|
148
214
|
export { useThreadHistory } from './use-thread-history';
|
|
149
215
|
export { useThreadSearch, useThreadWindow } from './use-thread-search';
|
|
150
|
-
export { useGemieThread } from './use-gemie-thread';
|
|
216
|
+
export { useGemieThread, useRefreshGemieThread } from './use-gemie-thread';
|
|
151
217
|
export type { GemieThreadInfo } from './use-gemie-thread';
|
|
152
218
|
export { AGENT_ROUTES, threadKey } from './routes';
|
|
153
219
|
export type { GemieThreadAddress } from './routes';
|
package/src/runtime/model.ts
CHANGED
package/src/runtime/routes.ts
CHANGED
|
@@ -67,8 +67,8 @@ function threadPath(thread: GemieThreadAddress): string {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
|
-
* The thread's
|
|
71
|
-
*
|
|
70
|
+
* The thread's key in the account's store (`gemieThread`, its history and its search): the handle,
|
|
71
|
+
* or `id:<gemieId>`, which no handle can be.
|
|
72
72
|
*/
|
|
73
73
|
export function threadKey(thread: GemieThreadAddress): string {
|
|
74
74
|
return 'handle' in thread ? thread.handle : `id:${thread.gemieId}`;
|
package/src/runtime/sharing.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { action, type RecordView, skip, useAction } from '@oxy.so/bloom-data';
|
|
2
2
|
import { useMemo } from 'react';
|
|
3
3
|
import { gemiesSharedConversationUrl } from '../lib/origins';
|
|
4
|
-
import { useGemieApi } from './api';
|
|
4
|
+
import { type GemieApi, useGemieApi } from './api';
|
|
5
5
|
import { useGemieRuntime } from './config';
|
|
6
|
-
import {
|
|
6
|
+
import { answerSource, useAnswer } from './data/answer';
|
|
7
|
+
import { gemieRead } from './data/read';
|
|
7
8
|
import { API_ROUTES } from './routes';
|
|
8
9
|
import type { Message, ToolInvocation } from './types';
|
|
9
10
|
|
|
@@ -20,48 +21,55 @@ export interface GemieConversationShare {
|
|
|
20
21
|
/** How long a share link lives unless the caller says otherwise, in hours (the API takes 1–168). */
|
|
21
22
|
export const DEFAULT_SHARE_TTL_HOURS = 24;
|
|
22
23
|
|
|
24
|
+
/** Mints a conversation's read-only link (1–168 hours). Nothing is held: the link is the answer. */
|
|
25
|
+
export const shareConversation = action({
|
|
26
|
+
run: async (
|
|
27
|
+
api: GemieApi,
|
|
28
|
+
conversationId: string,
|
|
29
|
+
ttlHours: number = DEFAULT_SHARE_TTL_HOURS,
|
|
30
|
+
): Promise<GemieConversationShare> => {
|
|
31
|
+
const hours = Math.min(168, Math.max(1, Math.round(ttlHours)));
|
|
32
|
+
const { data } = await api.post<{ token?: unknown; expiresAt?: unknown }>(
|
|
33
|
+
API_ROUTES.conversations.share(conversationId),
|
|
34
|
+
{ ttlHours: hours },
|
|
35
|
+
);
|
|
36
|
+
if (typeof data?.token !== 'string' || typeof data.expiresAt !== 'string') {
|
|
37
|
+
throw new Error('The share link came back without a token.');
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
conversationId,
|
|
41
|
+
token: data.token,
|
|
42
|
+
url: gemiesSharedConversationUrl(data.token),
|
|
43
|
+
expiresAt: data.expiresAt,
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/** Ends a conversation's link: the token stops answering. */
|
|
49
|
+
export const revokeConversationShare = action({
|
|
50
|
+
run: async (api: GemieApi, conversationId: string): Promise<void> => {
|
|
51
|
+
await api.delete(API_ROUTES.conversations.share(conversationId));
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
23
55
|
/** Mint and revoke a conversation's share link, as the signed-in person. */
|
|
24
56
|
export function useShareConversation() {
|
|
25
57
|
const api = useGemieApi();
|
|
26
|
-
const share =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}: {
|
|
31
|
-
conversationId: string;
|
|
32
|
-
ttlHours?: number;
|
|
33
|
-
}): Promise<GemieConversationShare> => {
|
|
34
|
-
const hours = Math.min(168, Math.max(1, Math.round(ttlHours)));
|
|
35
|
-
const { data } = await api.post<{ token?: unknown; expiresAt?: unknown }>(
|
|
36
|
-
API_ROUTES.conversations.share(conversationId),
|
|
37
|
-
{ ttlHours: hours },
|
|
38
|
-
);
|
|
39
|
-
if (typeof data?.token !== 'string' || typeof data.expiresAt !== 'string') {
|
|
40
|
-
throw new Error('The share link came back without a token.');
|
|
41
|
-
}
|
|
42
|
-
return {
|
|
43
|
-
conversationId,
|
|
44
|
-
token: data.token,
|
|
45
|
-
url: gemiesSharedConversationUrl(data.token),
|
|
46
|
-
expiresAt: data.expiresAt,
|
|
47
|
-
};
|
|
48
|
-
},
|
|
49
|
-
});
|
|
50
|
-
const revoke = useMutation({
|
|
51
|
-
mutationFn: async (conversationId: string): Promise<void> => {
|
|
52
|
-
await api.delete(API_ROUTES.conversations.share(conversationId));
|
|
53
|
-
},
|
|
54
|
-
});
|
|
58
|
+
const share = useAction(shareConversation);
|
|
59
|
+
const revoke = useAction(revokeConversationShare);
|
|
60
|
+
const { run: runShare, isPending: isSharing } = share;
|
|
61
|
+
const { run: runRevoke } = revoke;
|
|
55
62
|
return useMemo(
|
|
56
63
|
() => ({
|
|
57
64
|
/** Mint the link of `conversationId` (1–168 hours, default 24). */
|
|
65
|
+
// The action has no key, so every call runs and resolves with its link.
|
|
58
66
|
share: (conversationId: string, ttlHours?: number) =>
|
|
59
|
-
|
|
67
|
+
runShare(api, conversationId, ttlHours) as Promise<GemieConversationShare>,
|
|
60
68
|
/** End the link: the token stops answering. */
|
|
61
|
-
revoke: (conversationId: string) =>
|
|
62
|
-
isSharing
|
|
69
|
+
revoke: (conversationId: string) => runRevoke(api, conversationId),
|
|
70
|
+
isSharing,
|
|
63
71
|
}),
|
|
64
|
-
[
|
|
72
|
+
[api, runShare, runRevoke, isSharing],
|
|
65
73
|
);
|
|
66
74
|
}
|
|
67
75
|
|
|
@@ -191,27 +199,32 @@ export class SharedConversationGoneError extends Error {
|
|
|
191
199
|
}
|
|
192
200
|
|
|
193
201
|
/**
|
|
194
|
-
* A conversation somebody shared,
|
|
202
|
+
* A conversation somebody shared, by its token. The route needs no session, so it is fetched
|
|
195
203
|
* plainly rather than through the signed-in Oxy client: a person who is not signed in can read it.
|
|
204
|
+
* A token that answers nothing is not asked again.
|
|
196
205
|
*/
|
|
197
|
-
export
|
|
206
|
+
export const sharedConversation = answerSource('gemies.shared-conversation', {
|
|
207
|
+
...gemieRead,
|
|
208
|
+
key: (_apiUrl: string, token: string) => token,
|
|
209
|
+
retry: (failureCount, error) =>
|
|
210
|
+
!(error instanceof SharedConversationGoneError) && failureCount <= 2,
|
|
211
|
+
load: async ({ signal }, apiUrl, token) => {
|
|
212
|
+
const response = await fetch(new URL(API_ROUTES.shared(token), apiUrl).toString(), {
|
|
213
|
+
headers: { Accept: 'application/json' },
|
|
214
|
+
signal,
|
|
215
|
+
});
|
|
216
|
+
if (response.status === 404 || response.status === 410) throw new SharedConversationGoneError();
|
|
217
|
+
if (!response.ok) throw new Error(`Shared conversation failed with status ${response.status}`);
|
|
218
|
+
const shared = readSharedConversation(await response.json());
|
|
219
|
+
if (shared === null) throw new Error('The shared conversation had an unexpected shape.');
|
|
220
|
+
return shared;
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
/** A conversation somebody shared, read by its token. */
|
|
225
|
+
export function useSharedConversation(
|
|
226
|
+
token: string | undefined,
|
|
227
|
+
): RecordView<GemieSharedConversationData> {
|
|
198
228
|
const { apiUrl } = useGemieRuntime();
|
|
199
|
-
return
|
|
200
|
-
queryKey: gemieQueryKeys.shared(token ?? ''),
|
|
201
|
-
queryFn: async (): Promise<GemieSharedConversationData> => {
|
|
202
|
-
const response = await fetch(new URL(API_ROUTES.shared(token ?? ''), apiUrl).toString(), {
|
|
203
|
-
headers: { Accept: 'application/json' },
|
|
204
|
-
});
|
|
205
|
-
if (response.status === 404 || response.status === 410) {
|
|
206
|
-
throw new SharedConversationGoneError();
|
|
207
|
-
}
|
|
208
|
-
if (!response.ok)
|
|
209
|
-
throw new Error(`Shared conversation failed with status ${response.status}`);
|
|
210
|
-
const shared = readSharedConversation(await response.json());
|
|
211
|
-
if (shared === null) throw new Error('The shared conversation had an unexpected shape.');
|
|
212
|
-
return shared;
|
|
213
|
-
},
|
|
214
|
-
enabled: token !== undefined && token !== '',
|
|
215
|
-
retry: (count, error) => !(error instanceof SharedConversationGoneError) && count < 2,
|
|
216
|
-
});
|
|
229
|
+
return useAnswer(sharedConversation, ...(token ? [apiUrl, token] : [skip]));
|
|
217
230
|
}
|
package/src/runtime/types.ts
CHANGED
|
@@ -129,9 +129,14 @@ export interface Conversation {
|
|
|
129
129
|
kind?: 'person' | 'gemies';
|
|
130
130
|
/** The two gemies of a conversation between gemies, as their messages carry them. */
|
|
131
131
|
members?: NonNullable<Message['agentInfo']>[];
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
/** ISO 8601, as the API sends it: a conversation is a JSON record (`Conversations`). */
|
|
133
|
+
createdAt: string;
|
|
134
|
+
updatedAt: string;
|
|
135
|
+
/**
|
|
136
|
+
* The messages, once the conversation itself was read (`useConversation`). A row read from a
|
|
137
|
+
* list (`useConversations`) or a realtime event carries none, and leaves any held ones in place.
|
|
138
|
+
*/
|
|
139
|
+
messages?: Message[];
|
|
135
140
|
}
|
|
136
141
|
|
|
137
142
|
/** The run a delegated agent started for a turn: its session is `turnId`, in the agent's own room. */
|
|
@@ -140,19 +145,6 @@ export interface AgentRun {
|
|
|
140
145
|
agentId: string;
|
|
141
146
|
}
|
|
142
147
|
|
|
143
|
-
/** One page of the paginated conversation list. */
|
|
144
|
-
export interface ConversationsPage {
|
|
145
|
-
conversations: Conversation[];
|
|
146
|
-
nextCursor: string | null;
|
|
147
|
-
hasMore: boolean;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** React Query infinite-query cache shape for the conversation list. */
|
|
151
|
-
export interface ConversationsInfiniteData {
|
|
152
|
-
pages: ConversationsPage[];
|
|
153
|
-
pageParams: unknown[];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
148
|
/** What a chat turn is sent with. */
|
|
157
149
|
export interface SendOptions {
|
|
158
150
|
/** `null` explicitly withholds MCP tools; omission preserves legacy callers. */
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Action, type ActionView, useAction } from '@oxy.so/bloom-data';
|
|
2
|
+
import { useCallback, useRef } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An action whose first argument is the provider's client (the API, or the conversation client),
|
|
6
|
+
* as a component calls it: `run` takes the rest. `onFailure` hears every refusal (a toast), which
|
|
7
|
+
* `run` still rejects with.
|
|
8
|
+
*/
|
|
9
|
+
export function useBoundAction<C, A extends unknown[], R>(
|
|
10
|
+
action: Action<[C, ...A], R>,
|
|
11
|
+
client: C,
|
|
12
|
+
onFailure?: (error: unknown) => void,
|
|
13
|
+
): ActionView<A, R> {
|
|
14
|
+
const view = useAction(action);
|
|
15
|
+
const failure = useRef(onFailure);
|
|
16
|
+
failure.current = onFailure;
|
|
17
|
+
const bound = view.run;
|
|
18
|
+
const run = useCallback(
|
|
19
|
+
(...args: A) =>
|
|
20
|
+
bound(client, ...args).catch((error: unknown) => {
|
|
21
|
+
failure.current?.(error);
|
|
22
|
+
throw error;
|
|
23
|
+
}),
|
|
24
|
+
[bound, client],
|
|
25
|
+
);
|
|
26
|
+
return { ...view, run };
|
|
27
|
+
}
|