lemma-sdk 0.2.7 → 0.2.8
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { parseSSEJson, readSSE } from "../streams.js";
|
|
3
3
|
import { parseAssistantStreamEvent, upsertConversationMessage } from "../assistant-events.js";
|
|
4
4
|
function resolveOptionalPodId(client, podId) {
|
|
@@ -22,15 +22,15 @@ function normalizeError(error, fallback) {
|
|
|
22
22
|
return error;
|
|
23
23
|
return new Error(fallback);
|
|
24
24
|
}
|
|
25
|
-
function normalizeScope(client,
|
|
25
|
+
function normalizeScope(client, defaults, override) {
|
|
26
26
|
return {
|
|
27
|
-
podId: override?.podId ??
|
|
28
|
-
assistantId: override?.assistantId ??
|
|
29
|
-
organizationId: override?.organizationId ??
|
|
27
|
+
podId: override?.podId ?? defaults.podId ?? client.podId ?? null,
|
|
28
|
+
assistantId: override?.assistantId ?? defaults.assistantId ?? null,
|
|
29
|
+
organizationId: override?.organizationId ?? defaults.organizationId ?? null,
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
32
|
export function useAssistantSession(options) {
|
|
33
|
-
const { client, conversationId: externalConversationId = null, autoLoad = true, onEvent, onStatus, onMessage, onError, } = options;
|
|
33
|
+
const { client, podId: defaultPodId, assistantId: defaultAssistantId, organizationId: defaultOrganizationId, conversationId: externalConversationId = null, autoLoad = true, onEvent, onStatus, onMessage, onError, } = options;
|
|
34
34
|
const [conversationId, setConversationIdState] = useState(externalConversationId);
|
|
35
35
|
const [conversation, setConversation] = useState(null);
|
|
36
36
|
const [status, setStatus] = useState(undefined);
|
|
@@ -53,9 +53,14 @@ export function useAssistantSession(options) {
|
|
|
53
53
|
abortRef.current?.abort();
|
|
54
54
|
abortRef.current = null;
|
|
55
55
|
}, []);
|
|
56
|
+
const defaultScope = useMemo(() => ({
|
|
57
|
+
podId: defaultPodId ?? null,
|
|
58
|
+
assistantId: defaultAssistantId ?? null,
|
|
59
|
+
organizationId: defaultOrganizationId ?? null,
|
|
60
|
+
}), [defaultAssistantId, defaultOrganizationId, defaultPodId]);
|
|
56
61
|
const listConversations = useCallback(async (input = {}) => {
|
|
57
62
|
try {
|
|
58
|
-
const scope = normalizeScope(client,
|
|
63
|
+
const scope = normalizeScope(client, defaultScope, input.scope);
|
|
59
64
|
applyPodScope(client, scope.podId);
|
|
60
65
|
const response = await client.conversations.list({
|
|
61
66
|
pod_id: scope.podId ?? undefined,
|
|
@@ -80,14 +85,14 @@ export function useAssistantSession(options) {
|
|
|
80
85
|
next_page_token: null,
|
|
81
86
|
};
|
|
82
87
|
}
|
|
83
|
-
}, [client,
|
|
88
|
+
}, [client, defaultScope, onError]);
|
|
84
89
|
const createConversation = useCallback(async (input = {}) => {
|
|
85
|
-
applyPodScope(client, input.podId ??
|
|
90
|
+
applyPodScope(client, input.podId ?? defaultPodId ?? null);
|
|
86
91
|
const payload = {
|
|
87
92
|
title: input.title ?? undefined,
|
|
88
|
-
pod_id: input.podId ??
|
|
89
|
-
assistant_id: input.assistantId ??
|
|
90
|
-
organization_id: input.organizationId ??
|
|
93
|
+
pod_id: input.podId ?? defaultPodId ?? client.podId ?? undefined,
|
|
94
|
+
assistant_id: input.assistantId ?? defaultAssistantId ?? undefined,
|
|
95
|
+
organization_id: input.organizationId ?? defaultOrganizationId ?? undefined,
|
|
91
96
|
model: typeof input.model === "undefined"
|
|
92
97
|
? undefined
|
|
93
98
|
: input.model,
|
|
@@ -100,13 +105,13 @@ export function useAssistantSession(options) {
|
|
|
100
105
|
setMessages([]);
|
|
101
106
|
}
|
|
102
107
|
return created;
|
|
103
|
-
}, [client,
|
|
108
|
+
}, [client, defaultAssistantId, defaultOrganizationId, defaultPodId]);
|
|
104
109
|
const refreshConversation = useCallback(async (explicitConversationId) => {
|
|
105
110
|
const id = explicitConversationId ?? conversationId;
|
|
106
111
|
if (!id)
|
|
107
112
|
return null;
|
|
108
113
|
try {
|
|
109
|
-
const scope = normalizeScope(client,
|
|
114
|
+
const scope = normalizeScope(client, defaultScope);
|
|
110
115
|
applyPodScope(client, scope.podId);
|
|
111
116
|
const nextConversation = await client.conversations.get(id, {
|
|
112
117
|
pod_id: scope.podId ?? undefined,
|
|
@@ -127,14 +132,14 @@ export function useAssistantSession(options) {
|
|
|
127
132
|
onError?.(refreshError);
|
|
128
133
|
return null;
|
|
129
134
|
}
|
|
130
|
-
}, [client, conversationId, onError, onStatus
|
|
135
|
+
}, [client, conversationId, defaultScope, onError, onStatus]);
|
|
131
136
|
const loadMessages = useCallback(async (input = {}) => {
|
|
132
137
|
const id = input.conversationId ?? conversationId;
|
|
133
138
|
if (!id) {
|
|
134
139
|
return { items: [], limit: input.limit ?? 20, next_page_token: null };
|
|
135
140
|
}
|
|
136
141
|
try {
|
|
137
|
-
const scope = normalizeScope(client,
|
|
142
|
+
const scope = normalizeScope(client, defaultScope);
|
|
138
143
|
applyPodScope(client, scope.podId);
|
|
139
144
|
const response = await client.conversations.messages.list(id, {
|
|
140
145
|
pod_id: scope.podId ?? undefined,
|
|
@@ -159,7 +164,7 @@ export function useAssistantSession(options) {
|
|
|
159
164
|
next_page_token: null,
|
|
160
165
|
};
|
|
161
166
|
}
|
|
162
|
-
}, [client, conversationId,
|
|
167
|
+
}, [client, conversationId, defaultScope, onError]);
|
|
163
168
|
const consume = useCallback(async (stream, controller) => {
|
|
164
169
|
setIsStreaming(true);
|
|
165
170
|
setError(null);
|
|
@@ -217,7 +222,7 @@ export function useAssistantSession(options) {
|
|
|
217
222
|
cancel();
|
|
218
223
|
const controller = new AbortController();
|
|
219
224
|
abortRef.current = controller;
|
|
220
|
-
const scope = normalizeScope(client,
|
|
225
|
+
const scope = normalizeScope(client, defaultScope, input.createConversation);
|
|
221
226
|
applyPodScope(client, scope.podId);
|
|
222
227
|
const stream = await client.conversations.sendMessageStream(resolvedConversationId, { content }, {
|
|
223
228
|
pod_id: scope.podId ?? undefined,
|
|
@@ -225,28 +230,28 @@ export function useAssistantSession(options) {
|
|
|
225
230
|
});
|
|
226
231
|
await consume(stream, controller);
|
|
227
232
|
return resolvedConversation;
|
|
228
|
-
}, [cancel, client, consume,
|
|
233
|
+
}, [cancel, client, consume, defaultScope, ensureConversation]);
|
|
229
234
|
const resume = useCallback(async (explicitConversationId) => {
|
|
230
235
|
const id = requireConversationId(explicitConversationId ?? conversationId);
|
|
231
236
|
cancel();
|
|
232
237
|
const controller = new AbortController();
|
|
233
238
|
abortRef.current = controller;
|
|
234
|
-
const scope = normalizeScope(client,
|
|
239
|
+
const scope = normalizeScope(client, defaultScope);
|
|
235
240
|
applyPodScope(client, scope.podId);
|
|
236
241
|
const stream = await client.conversations.resumeStream(id, {
|
|
237
242
|
pod_id: scope.podId ?? undefined,
|
|
238
243
|
signal: controller.signal,
|
|
239
244
|
});
|
|
240
245
|
await consume(stream, controller);
|
|
241
|
-
}, [cancel, client, consume, conversationId,
|
|
246
|
+
}, [cancel, client, consume, conversationId, defaultScope]);
|
|
242
247
|
const stop = useCallback(async (explicitConversationId) => {
|
|
243
248
|
const id = requireConversationId(explicitConversationId ?? conversationId);
|
|
244
|
-
const scope = normalizeScope(client,
|
|
249
|
+
const scope = normalizeScope(client, defaultScope);
|
|
245
250
|
applyPodScope(client, scope.podId);
|
|
246
251
|
await client.conversations.stopRun(id, {
|
|
247
252
|
pod_id: scope.podId ?? undefined,
|
|
248
253
|
});
|
|
249
|
-
}, [client, conversationId,
|
|
254
|
+
}, [client, conversationId, defaultScope]);
|
|
250
255
|
const clearMessages = useCallback(() => {
|
|
251
256
|
setMessages([]);
|
|
252
257
|
}, []);
|