@letta-ai/letta-react 0.0.7 → 0.0.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.
- package/dist/hooks/useAgentMessages/useAgentMessages.js +5 -6
- package/dist/hooks/useAgentPassages/useAgentPassages.d.ts +13 -0
- package/dist/hooks/useAgentPassages/useAgentPassages.js +45 -0
- package/dist/hooks/useAgentState/useAgentState.d.ts +5 -2
- package/dist/hooks/useAgentState/useAgentState.js +25 -8
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/examples/view-and-send-messages/src/App.tsx +21 -3
- package/hooks/useAgentMessages/useAgentMessages.js +5 -6
- package/hooks/useAgentPassages/useAgentPassages.d.ts +13 -0
- package/hooks/useAgentPassages/useAgentPassages.js +45 -0
- package/hooks/useAgentState/useAgentState.d.ts +5 -2
- package/hooks/useAgentState/useAgentState.js +25 -8
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/src/hooks/useAgentMessages/useAgentMessages.ts +5 -9
- package/src/hooks/useAgentPassages/useAgentPassages.ts +51 -0
- package/src/hooks/useAgentState/useAgentState.ts +29 -12
- package/src/index.ts +1 -0
@@ -60,7 +60,7 @@ export function useAgentMessages(options) {
|
|
60
60
|
const [localMessages, setLocalMessages] = useCachedState(`messages-${agentId}`, {
|
61
61
|
messages: [],
|
62
62
|
});
|
63
|
-
const
|
63
|
+
const agentIdMessagesLoadedRef = useRef(false);
|
64
64
|
const [isLoading, setIsLoading] = useState(true);
|
65
65
|
const [isFetching, setIsFetching] = useState(false);
|
66
66
|
const [loadingError, setLoadingError] = useState(null);
|
@@ -230,12 +230,11 @@ export function useAgentMessages(options) {
|
|
230
230
|
return getMessages(nextCursor);
|
231
231
|
}), [getMessages]);
|
232
232
|
useEffect(() => {
|
233
|
-
if (
|
234
|
-
|
233
|
+
if (!agentIdMessagesLoadedRef.current) {
|
234
|
+
setIsLoading(true);
|
235
|
+
void getMessages();
|
236
|
+
agentIdMessagesLoadedRef.current = true;
|
235
237
|
}
|
236
|
-
hasInitialLoaded.current = true;
|
237
|
-
setIsLoading(true);
|
238
|
-
getMessages();
|
239
238
|
}, []);
|
240
239
|
return {
|
241
240
|
messages: localMessages.messages,
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import type { Passage } from '@letta-ai/letta-client/api';
|
2
|
+
import type { LettaClient } from '@letta-ai/letta-client';
|
3
|
+
interface UseAgentStateOptions {
|
4
|
+
client?: LettaClient.Options;
|
5
|
+
agentId: string;
|
6
|
+
}
|
7
|
+
export declare function useAgentPassages(options: UseAgentStateOptions): {
|
8
|
+
isLoading: boolean;
|
9
|
+
isLoadingError: unknown;
|
10
|
+
passages: Passage[] | undefined;
|
11
|
+
refresh: () => Promise<void>;
|
12
|
+
};
|
13
|
+
export {};
|
@@ -0,0 +1,45 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { useLettaClient } from '../useLettaClient/useLettaClient';
|
11
|
+
import { useCachedState } from '../useCachedState/useCachedState';
|
12
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
13
|
+
export function useAgentPassages(options) {
|
14
|
+
const { client, agentId } = options;
|
15
|
+
const localClient = useLettaClient(client);
|
16
|
+
const loadedAgentId = useRef(null);
|
17
|
+
const [localState, setLocalState] = useCachedState(`agent-passages-${agentId}`, undefined);
|
18
|
+
const [isLoading, setIsLoading] = useState(true);
|
19
|
+
const [isLoadingError, setIsLoadingError] = useState(null);
|
20
|
+
const getAgentPassages = useCallback(() => __awaiter(this, void 0, void 0, function* () {
|
21
|
+
try {
|
22
|
+
const state = yield localClient.agents.passages.list(agentId);
|
23
|
+
setLocalState(state);
|
24
|
+
}
|
25
|
+
catch (error) {
|
26
|
+
setIsLoadingError(error);
|
27
|
+
}
|
28
|
+
finally {
|
29
|
+
setIsLoading(false);
|
30
|
+
}
|
31
|
+
}), [agentId, localClient, setLocalState]);
|
32
|
+
useEffect(() => {
|
33
|
+
if (agentId !== loadedAgentId.current) {
|
34
|
+
setIsLoading(true);
|
35
|
+
void getAgentPassages();
|
36
|
+
loadedAgentId.current = agentId;
|
37
|
+
}
|
38
|
+
}, [agentId, getAgentPassages]);
|
39
|
+
return {
|
40
|
+
isLoading,
|
41
|
+
isLoadingError,
|
42
|
+
passages: localState,
|
43
|
+
refresh: getAgentPassages,
|
44
|
+
};
|
45
|
+
}
|
@@ -1,12 +1,15 @@
|
|
1
1
|
import type { LettaClient } from '@letta-ai/letta-client';
|
2
|
-
import type { AgentState } from '@letta-ai/letta-client/api';
|
2
|
+
import type { AgentState, UpdateAgent } from '@letta-ai/letta-client/api';
|
3
3
|
interface UseAgentStateOptions {
|
4
4
|
client?: LettaClient.Options;
|
5
5
|
agentId: string;
|
6
6
|
}
|
7
7
|
export declare function useAgentState(options: UseAgentStateOptions): {
|
8
8
|
isLoading: boolean;
|
9
|
-
|
9
|
+
updateAgentState: (state: Partial<UpdateAgent>) => Promise<void>;
|
10
|
+
isUpdating: boolean;
|
11
|
+
updatingError: unknown;
|
12
|
+
loadingError: unknown;
|
10
13
|
agentState: AgentState | undefined;
|
11
14
|
refresh: () => Promise<void>;
|
12
15
|
};
|
@@ -15,8 +15,10 @@ export function useAgentState(options) {
|
|
15
15
|
const localClient = useLettaClient(client);
|
16
16
|
const [localState, setLocalState] = useCachedState(`agent-state-${agentId}`, undefined);
|
17
17
|
const [isLoading, setIsLoading] = useState(true);
|
18
|
+
const [isUpdating, setIsUpdating] = useState(false);
|
19
|
+
const [updatingError, setUpdatingError] = useState(null);
|
18
20
|
const [loadingError, setLoadingError] = useState(null);
|
19
|
-
const
|
21
|
+
const loadedAgentId = useRef(null);
|
20
22
|
const getAgentState = useCallback(() => __awaiter(this, void 0, void 0, function* () {
|
21
23
|
try {
|
22
24
|
const state = yield localClient.agents.retrieve(agentId);
|
@@ -29,17 +31,32 @@ export function useAgentState(options) {
|
|
29
31
|
setIsLoading(false);
|
30
32
|
}
|
31
33
|
}), [agentId, localClient, setLocalState]);
|
34
|
+
const updateAgentState = useCallback((state) => __awaiter(this, void 0, void 0, function* () {
|
35
|
+
setIsUpdating(true);
|
36
|
+
try {
|
37
|
+
const response = yield localClient.agents.modify(agentId, state);
|
38
|
+
setLocalState(response);
|
39
|
+
}
|
40
|
+
catch (error) {
|
41
|
+
setUpdatingError(error);
|
42
|
+
}
|
43
|
+
finally {
|
44
|
+
setIsUpdating(false);
|
45
|
+
}
|
46
|
+
}), [agentId, localClient, setLocalState]);
|
32
47
|
useEffect(() => {
|
33
|
-
if (
|
34
|
-
|
48
|
+
if (agentId !== loadedAgentId.current) {
|
49
|
+
setIsLoading(true);
|
50
|
+
void getAgentState();
|
51
|
+
loadedAgentId.current = agentId;
|
35
52
|
}
|
36
|
-
|
37
|
-
setIsLoading(true);
|
38
|
-
getAgentState();
|
39
|
-
}, []);
|
53
|
+
}, [agentId]);
|
40
54
|
return {
|
41
55
|
isLoading,
|
42
|
-
|
56
|
+
updateAgentState,
|
57
|
+
isUpdating,
|
58
|
+
updatingError,
|
59
|
+
loadingError,
|
43
60
|
agentState: localState,
|
44
61
|
refresh: getAgentState,
|
45
62
|
};
|
package/dist/index.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
export { LettaProvider } from './hooks/useGlobalLettaConfig/useGlobalLettaConfig';
|
2
2
|
export { useAgentMessages } from './hooks/useAgentMessages/useAgentMessages';
|
3
3
|
export { useAgentState } from './hooks/useAgentState/useAgentState';
|
4
|
+
export { useAgentPassages } from './hooks/useAgentPassages/useAgentPassages';
|
package/dist/index.js
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
export { LettaProvider } from './hooks/useGlobalLettaConfig/useGlobalLettaConfig';
|
2
2
|
export { useAgentMessages } from './hooks/useAgentMessages/useAgentMessages';
|
3
3
|
export { useAgentState } from './hooks/useAgentState/useAgentState';
|
4
|
+
export { useAgentPassages } from './hooks/useAgentPassages/useAgentPassages';
|
@@ -1,7 +1,11 @@
|
|
1
1
|
import React, { FormEvent, useCallback } from 'react';
|
2
2
|
import { useState } from 'react';
|
3
3
|
import './App.css';
|
4
|
-
import {
|
4
|
+
import {
|
5
|
+
useAgentMessages,
|
6
|
+
useAgentPassages,
|
7
|
+
useAgentState,
|
8
|
+
} from '@letta-ai/letta-react';
|
5
9
|
|
6
10
|
function App() {
|
7
11
|
const [messageToSend, setMessageToSend] = useState<string>('');
|
@@ -14,11 +18,15 @@ function App() {
|
|
14
18
|
isSending,
|
15
19
|
sendMessage,
|
16
20
|
} = useAgentMessages({
|
17
|
-
agentId: 'agent-
|
21
|
+
agentId: 'agent-0d07e901-64de-4bbd-8a7c-268ce88bc6cb',
|
22
|
+
});
|
23
|
+
|
24
|
+
const { passages, isLoading: isPassagesLoading } = useAgentPassages({
|
25
|
+
agentId: 'agent-0d07e901-64de-4bbd-8a7c-268ce88bc6cb',
|
18
26
|
});
|
19
27
|
|
20
28
|
const { agentState } = useAgentState({
|
21
|
-
agentId: 'agent-
|
29
|
+
agentId: 'agent-0d07e901-64de-4bbd-8a7c-268ce88bc6cb',
|
22
30
|
});
|
23
31
|
|
24
32
|
const handleSubmit = useCallback(
|
@@ -42,6 +50,16 @@ function App() {
|
|
42
50
|
return (
|
43
51
|
<main>
|
44
52
|
<header>Talking to: {agentState?.name}</header>
|
53
|
+
{isPassagesLoading ? (
|
54
|
+
<div>Loading passages!</div>
|
55
|
+
) : (
|
56
|
+
<ul className="passages">
|
57
|
+
{passages?.map((passage) => (
|
58
|
+
<li key={passage.id}>{passage.text}</li>
|
59
|
+
))}
|
60
|
+
</ul>
|
61
|
+
)}
|
62
|
+
|
45
63
|
{hasOlderMessages && (
|
46
64
|
<button onClick={fetchOlderMessages} disabled={isFetching}>
|
47
65
|
{isFetching ? 'Loading older messages' : 'Load older messages'}
|
@@ -60,7 +60,7 @@ export function useAgentMessages(options) {
|
|
60
60
|
const [localMessages, setLocalMessages] = useCachedState(`messages-${agentId}`, {
|
61
61
|
messages: [],
|
62
62
|
});
|
63
|
-
const
|
63
|
+
const agentIdMessagesLoadedRef = useRef(false);
|
64
64
|
const [isLoading, setIsLoading] = useState(true);
|
65
65
|
const [isFetching, setIsFetching] = useState(false);
|
66
66
|
const [loadingError, setLoadingError] = useState(null);
|
@@ -230,12 +230,11 @@ export function useAgentMessages(options) {
|
|
230
230
|
return getMessages(nextCursor);
|
231
231
|
}), [getMessages]);
|
232
232
|
useEffect(() => {
|
233
|
-
if (
|
234
|
-
|
233
|
+
if (!agentIdMessagesLoadedRef.current) {
|
234
|
+
setIsLoading(true);
|
235
|
+
void getMessages();
|
236
|
+
agentIdMessagesLoadedRef.current = true;
|
235
237
|
}
|
236
|
-
hasInitialLoaded.current = true;
|
237
|
-
setIsLoading(true);
|
238
|
-
getMessages();
|
239
238
|
}, []);
|
240
239
|
return {
|
241
240
|
messages: localMessages.messages,
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import type { Passage } from '@letta-ai/letta-client/api';
|
2
|
+
import type { LettaClient } from '@letta-ai/letta-client';
|
3
|
+
interface UseAgentStateOptions {
|
4
|
+
client?: LettaClient.Options;
|
5
|
+
agentId: string;
|
6
|
+
}
|
7
|
+
export declare function useAgentPassages(options: UseAgentStateOptions): {
|
8
|
+
isLoading: boolean;
|
9
|
+
isLoadingError: unknown;
|
10
|
+
passages: Passage[] | undefined;
|
11
|
+
refresh: () => Promise<void>;
|
12
|
+
};
|
13
|
+
export {};
|
@@ -0,0 +1,45 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { useLettaClient } from '../useLettaClient/useLettaClient';
|
11
|
+
import { useCachedState } from '../useCachedState/useCachedState';
|
12
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
13
|
+
export function useAgentPassages(options) {
|
14
|
+
const { client, agentId } = options;
|
15
|
+
const localClient = useLettaClient(client);
|
16
|
+
const loadedAgentId = useRef(null);
|
17
|
+
const [localState, setLocalState] = useCachedState(`agent-passages-${agentId}`, undefined);
|
18
|
+
const [isLoading, setIsLoading] = useState(true);
|
19
|
+
const [isLoadingError, setIsLoadingError] = useState(null);
|
20
|
+
const getAgentPassages = useCallback(() => __awaiter(this, void 0, void 0, function* () {
|
21
|
+
try {
|
22
|
+
const state = yield localClient.agents.passages.list(agentId);
|
23
|
+
setLocalState(state);
|
24
|
+
}
|
25
|
+
catch (error) {
|
26
|
+
setIsLoadingError(error);
|
27
|
+
}
|
28
|
+
finally {
|
29
|
+
setIsLoading(false);
|
30
|
+
}
|
31
|
+
}), [agentId, localClient, setLocalState]);
|
32
|
+
useEffect(() => {
|
33
|
+
if (agentId !== loadedAgentId.current) {
|
34
|
+
setIsLoading(true);
|
35
|
+
void getAgentPassages();
|
36
|
+
loadedAgentId.current = agentId;
|
37
|
+
}
|
38
|
+
}, [agentId, getAgentPassages]);
|
39
|
+
return {
|
40
|
+
isLoading,
|
41
|
+
isLoadingError,
|
42
|
+
passages: localState,
|
43
|
+
refresh: getAgentPassages,
|
44
|
+
};
|
45
|
+
}
|
@@ -1,12 +1,15 @@
|
|
1
1
|
import type { LettaClient } from '@letta-ai/letta-client';
|
2
|
-
import type { AgentState } from '@letta-ai/letta-client/api';
|
2
|
+
import type { AgentState, UpdateAgent } from '@letta-ai/letta-client/api';
|
3
3
|
interface UseAgentStateOptions {
|
4
4
|
client?: LettaClient.Options;
|
5
5
|
agentId: string;
|
6
6
|
}
|
7
7
|
export declare function useAgentState(options: UseAgentStateOptions): {
|
8
8
|
isLoading: boolean;
|
9
|
-
|
9
|
+
updateAgentState: (state: Partial<UpdateAgent>) => Promise<void>;
|
10
|
+
isUpdating: boolean;
|
11
|
+
updatingError: unknown;
|
12
|
+
loadingError: unknown;
|
10
13
|
agentState: AgentState | undefined;
|
11
14
|
refresh: () => Promise<void>;
|
12
15
|
};
|
@@ -15,8 +15,10 @@ export function useAgentState(options) {
|
|
15
15
|
const localClient = useLettaClient(client);
|
16
16
|
const [localState, setLocalState] = useCachedState(`agent-state-${agentId}`, undefined);
|
17
17
|
const [isLoading, setIsLoading] = useState(true);
|
18
|
+
const [isUpdating, setIsUpdating] = useState(false);
|
19
|
+
const [updatingError, setUpdatingError] = useState(null);
|
18
20
|
const [loadingError, setLoadingError] = useState(null);
|
19
|
-
const
|
21
|
+
const loadedAgentId = useRef(null);
|
20
22
|
const getAgentState = useCallback(() => __awaiter(this, void 0, void 0, function* () {
|
21
23
|
try {
|
22
24
|
const state = yield localClient.agents.retrieve(agentId);
|
@@ -29,17 +31,32 @@ export function useAgentState(options) {
|
|
29
31
|
setIsLoading(false);
|
30
32
|
}
|
31
33
|
}), [agentId, localClient, setLocalState]);
|
34
|
+
const updateAgentState = useCallback((state) => __awaiter(this, void 0, void 0, function* () {
|
35
|
+
setIsUpdating(true);
|
36
|
+
try {
|
37
|
+
const response = yield localClient.agents.modify(agentId, state);
|
38
|
+
setLocalState(response);
|
39
|
+
}
|
40
|
+
catch (error) {
|
41
|
+
setUpdatingError(error);
|
42
|
+
}
|
43
|
+
finally {
|
44
|
+
setIsUpdating(false);
|
45
|
+
}
|
46
|
+
}), [agentId, localClient, setLocalState]);
|
32
47
|
useEffect(() => {
|
33
|
-
if (
|
34
|
-
|
48
|
+
if (agentId !== loadedAgentId.current) {
|
49
|
+
setIsLoading(true);
|
50
|
+
void getAgentState();
|
51
|
+
loadedAgentId.current = agentId;
|
35
52
|
}
|
36
|
-
|
37
|
-
setIsLoading(true);
|
38
|
-
getAgentState();
|
39
|
-
}, []);
|
53
|
+
}, [agentId]);
|
40
54
|
return {
|
41
55
|
isLoading,
|
42
|
-
|
56
|
+
updateAgentState,
|
57
|
+
isUpdating,
|
58
|
+
updatingError,
|
59
|
+
loadingError,
|
43
60
|
agentState: localState,
|
44
61
|
refresh: getAgentState,
|
45
62
|
};
|
package/index.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
export { LettaProvider } from './hooks/useGlobalLettaConfig/useGlobalLettaConfig';
|
2
2
|
export { useAgentMessages } from './hooks/useAgentMessages/useAgentMessages';
|
3
3
|
export { useAgentState } from './hooks/useAgentState/useAgentState';
|
4
|
+
export { useAgentPassages } from './hooks/useAgentPassages/useAgentPassages';
|
package/index.js
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
export { LettaProvider } from './hooks/useGlobalLettaConfig/useGlobalLettaConfig';
|
2
2
|
export { useAgentMessages } from './hooks/useAgentMessages/useAgentMessages';
|
3
3
|
export { useAgentState } from './hooks/useAgentState/useAgentState';
|
4
|
+
export { useAgentPassages } from './hooks/useAgentPassages/useAgentPassages';
|
package/package.json
CHANGED
@@ -85,7 +85,7 @@ export function useAgentMessages(options: UseAgentOptions) {
|
|
85
85
|
}
|
86
86
|
);
|
87
87
|
|
88
|
-
const
|
88
|
+
const agentIdMessagesLoadedRef = useRef(false);
|
89
89
|
|
90
90
|
const [isLoading, setIsLoading] = useState(true);
|
91
91
|
const [isFetching, setIsFetching] = useState(false);
|
@@ -371,15 +371,11 @@ export function useAgentMessages(options: UseAgentOptions) {
|
|
371
371
|
}, [getMessages]);
|
372
372
|
|
373
373
|
useEffect(() => {
|
374
|
-
if (
|
375
|
-
|
374
|
+
if (!agentIdMessagesLoadedRef.current) {
|
375
|
+
setIsLoading(true);
|
376
|
+
void getMessages();
|
377
|
+
agentIdMessagesLoadedRef.current = true;
|
376
378
|
}
|
377
|
-
|
378
|
-
hasInitialLoaded.current = true;
|
379
|
-
|
380
|
-
setIsLoading(true);
|
381
|
-
|
382
|
-
getMessages();
|
383
379
|
}, []);
|
384
380
|
|
385
381
|
return {
|
@@ -0,0 +1,51 @@
|
|
1
|
+
import { useLettaClient } from '../useLettaClient/useLettaClient';
|
2
|
+
import { useCachedState } from '../useCachedState/useCachedState';
|
3
|
+
import type { AgentState, Passage } from '@letta-ai/letta-client/api';
|
4
|
+
import type { LettaClient } from '@letta-ai/letta-client';
|
5
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
6
|
+
|
7
|
+
interface UseAgentStateOptions {
|
8
|
+
client?: LettaClient.Options;
|
9
|
+
agentId: string;
|
10
|
+
}
|
11
|
+
|
12
|
+
export function useAgentPassages(options: UseAgentStateOptions) {
|
13
|
+
const { client, agentId } = options;
|
14
|
+
const localClient = useLettaClient(client);
|
15
|
+
const loadedAgentId = useRef<string | null>(null);
|
16
|
+
|
17
|
+
const [localState, setLocalState] = useCachedState<Passage[] | undefined>(
|
18
|
+
`agent-passages-${agentId}`,
|
19
|
+
undefined
|
20
|
+
);
|
21
|
+
|
22
|
+
const [isLoading, setIsLoading] = useState(true);
|
23
|
+
const [isLoadingError, setIsLoadingError] = useState<unknown | null>(null);
|
24
|
+
|
25
|
+
const getAgentPassages = useCallback(async () => {
|
26
|
+
try {
|
27
|
+
const state = await localClient.agents.passages.list(agentId);
|
28
|
+
|
29
|
+
setLocalState(state);
|
30
|
+
} catch (error) {
|
31
|
+
setIsLoadingError(error);
|
32
|
+
} finally {
|
33
|
+
setIsLoading(false);
|
34
|
+
}
|
35
|
+
}, [agentId, localClient, setLocalState]);
|
36
|
+
|
37
|
+
useEffect(() => {
|
38
|
+
if (agentId !== loadedAgentId.current) {
|
39
|
+
setIsLoading(true);
|
40
|
+
void getAgentPassages();
|
41
|
+
loadedAgentId.current = agentId;
|
42
|
+
}
|
43
|
+
}, [agentId, getAgentPassages]);
|
44
|
+
|
45
|
+
return {
|
46
|
+
isLoading,
|
47
|
+
isLoadingError,
|
48
|
+
passages: localState,
|
49
|
+
refresh: getAgentPassages,
|
50
|
+
};
|
51
|
+
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { useLettaClient } from '../useLettaClient/useLettaClient';
|
2
2
|
import type { LettaClient } from '@letta-ai/letta-client';
|
3
3
|
import { useCachedState } from '../useCachedState/useCachedState';
|
4
|
-
import type { AgentState } from '@letta-ai/letta-client/api';
|
4
|
+
import type { AgentState, UpdateAgent } from '@letta-ai/letta-client/api';
|
5
5
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
6
6
|
|
7
7
|
interface UseAgentStateOptions {
|
@@ -19,8 +19,10 @@ export function useAgentState(options: UseAgentStateOptions) {
|
|
19
19
|
);
|
20
20
|
|
21
21
|
const [isLoading, setIsLoading] = useState(true);
|
22
|
+
const [isUpdating, setIsUpdating] = useState(false);
|
23
|
+
const [updatingError, setUpdatingError] = useState<unknown | null>(null);
|
22
24
|
const [loadingError, setLoadingError] = useState<unknown | null>(null);
|
23
|
-
const
|
25
|
+
const loadedAgentId = useRef<string | null>(null);
|
24
26
|
|
25
27
|
const getAgentState = useCallback(async () => {
|
26
28
|
try {
|
@@ -34,21 +36,36 @@ export function useAgentState(options: UseAgentStateOptions) {
|
|
34
36
|
}
|
35
37
|
}, [agentId, localClient, setLocalState]);
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
hasInitialLoaded.current = true;
|
39
|
+
const updateAgentState = useCallback(
|
40
|
+
async (state: Partial<UpdateAgent>) => {
|
41
|
+
setIsUpdating(true);
|
42
|
+
try {
|
43
|
+
const response = await localClient.agents.modify(agentId, state);
|
43
44
|
|
44
|
-
|
45
|
+
setLocalState(response);
|
46
|
+
} catch (error) {
|
47
|
+
setUpdatingError(error);
|
48
|
+
} finally {
|
49
|
+
setIsUpdating(false);
|
50
|
+
}
|
51
|
+
},
|
52
|
+
[agentId, localClient, setLocalState]
|
53
|
+
);
|
45
54
|
|
46
|
-
|
47
|
-
|
55
|
+
useEffect(() => {
|
56
|
+
if (agentId !== loadedAgentId.current) {
|
57
|
+
setIsLoading(true);
|
58
|
+
void getAgentState();
|
59
|
+
loadedAgentId.current = agentId;
|
60
|
+
}
|
61
|
+
}, [agentId]);
|
48
62
|
|
49
63
|
return {
|
50
64
|
isLoading,
|
51
|
-
|
65
|
+
updateAgentState,
|
66
|
+
isUpdating,
|
67
|
+
updatingError,
|
68
|
+
loadingError,
|
52
69
|
agentState: localState,
|
53
70
|
refresh: getAgentState,
|
54
71
|
};
|
package/src/index.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
1
|
export { LettaProvider } from './hooks/useGlobalLettaConfig/useGlobalLettaConfig';
|
2
2
|
export { useAgentMessages } from './hooks/useAgentMessages/useAgentMessages';
|
3
3
|
export { useAgentState } from './hooks/useAgentState/useAgentState';
|
4
|
+
export { useAgentPassages } from './hooks/useAgentPassages/useAgentPassages';
|