@letta-ai/letta-react 0.2.0 → 0.2.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/README.md +5 -48
- package/dist/index.d.ts +0 -1
- package/dist/index.js +8 -19
- package/dist/index.js.map +3 -3
- package/dist/useLettaQuery/useLettaQuery.d.ts +6 -6
- package/index.d.ts +0 -1
- package/index.js +8 -19
- package/index.js.map +3 -3
- package/package.json +1 -1
- package/src/index.ts +0 -1
- package/src/useLettaQuery/useLettaQuery.test.tsx +9 -6
- package/src/useLettaQuery/useLettaQuery.ts +16 -28
- package/useLettaQuery/useLettaQuery.d.ts +6 -6
- package/dist/useLetta/useLetta.d.ts +0 -2
- package/src/useLetta/useLetta.ts +0 -8
- package/useLetta/useLetta.d.ts +0 -2
package/README.md
CHANGED
@@ -26,16 +26,17 @@ function App() {
|
|
26
26
|
}
|
27
27
|
```
|
28
28
|
|
29
|
-
Then you can use `
|
29
|
+
Then you can use `useLettaQuery` hooks to interact with the Letta API.
|
30
30
|
|
31
31
|
```tsx
|
32
32
|
import { useLetta, useLettaQuery } from '@letta-ai/letta-react';
|
33
33
|
|
34
34
|
function YourComponent() {
|
35
|
-
const client = useLetta();
|
36
35
|
const { data, isLoading, error } = useLettaQuery(
|
37
|
-
|
38
|
-
{ limit: 1 }
|
36
|
+
// the first argument exposes a client instantiation
|
37
|
+
(client) => client.agents.list({ limit: 1 }),
|
38
|
+
// the rest of the argumenst are the same as react-query, so you should specify a queryKey
|
39
|
+
{ queryKey: ['agents', 'list'] }
|
39
40
|
);
|
40
41
|
|
41
42
|
if (isLoading) return <div>Loading...</div>;
|
@@ -45,50 +46,6 @@ function YourComponent() {
|
|
45
46
|
}
|
46
47
|
```
|
47
48
|
|
48
|
-
The `useLetta` hook returns the Letta client instance, which you can use to call any of the API methods. The `useLettaQuery` hook is a wrapper around the `useQuery` hook from tanstack/react-query, which allows you to easily fetch data from the Letta API and manage the loading and error states.
|
49
|
-
|
50
|
-
The format of the `useLettaQuery` hook is as follows:
|
51
|
-
|
52
|
-
```tsx
|
53
|
-
useLettaQuery(
|
54
|
-
operation // the client operation you want to call, this should be 1:1 match with our API docs, it __must__ be wrapped in a function
|
55
|
-
args, // the arguments to pass to the operation
|
56
|
-
options // optional react-query options
|
57
|
-
)
|
58
|
-
```
|
59
|
-
|
60
|
-
## Examples
|
61
|
-
|
62
|
-
### List Agents
|
63
|
-
|
64
|
-
1. First let's go to the [API docs](https://docs.letta.ai/api-reference/agents/list) and see the API for listing agents.
|
65
|
-
|
66
|
-
```ts
|
67
|
-
import { LettaClient } from '@letta-ai/letta-client';
|
68
|
-
const client = new LettaClient({ token: 'YOUR_TOKEN' });
|
69
|
-
await client.agents.list();
|
70
|
-
```
|
71
|
-
|
72
|
-
As we can see, we can call the `list` method on the `agents` object to get a list of agents. The `list` method takes an optional `args`.
|
73
|
-
|
74
|
-
2. Now let's translate the usage to use the `useLettaQuery` hook to call this API in a React component.
|
75
|
-
|
76
|
-
```tsx
|
77
|
-
import { useLetta, useLettaQuery } from '@letta-ai/letta-react';
|
78
|
-
|
79
|
-
function YourComponent() {
|
80
|
-
const client = useLetta();
|
81
|
-
const { data, isLoading, error } = useLettaQuery((args) =>
|
82
|
-
client.agents.list(args)
|
83
|
-
);
|
84
|
-
|
85
|
-
if (isLoading) return <div>Loading...</div>;
|
86
|
-
if (error) return <div>Error: {error.message}</div>;
|
87
|
-
|
88
|
-
return <div>Data: {JSON.stringify(data)}</div>;
|
89
|
-
}
|
90
|
-
```
|
91
|
-
|
92
49
|
# Future Work
|
93
50
|
|
94
51
|
This library only supports queries not mutations at the moment. Please use this library in conjunction with [@letta-ai/vercel-ai-sdk-provider](https://www.npmjs.com/package/@letta-ai/vercel-ai-sdk-provider) for a full experience.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
@@ -48254,30 +48254,19 @@ var require_letta_client = __commonJS({
|
|
48254
48254
|
});
|
48255
48255
|
|
48256
48256
|
// src/useLettaQuery/useLettaQuery.ts
|
48257
|
+
var import_letta_client = __toESM(require_letta_client());
|
48257
48258
|
import { useQuery } from "@tanstack/react-query";
|
48258
|
-
|
48259
|
-
|
48260
|
-
|
48261
|
-
function useLettaQuery(operation, request, options, queryOptions = {}) {
|
48259
|
+
import { useRef } from "react";
|
48260
|
+
function useLettaQuery(operation, queryOptions) {
|
48261
|
+
const client = useRef(new import_letta_client.LettaClient());
|
48262
48262
|
return useQuery({
|
48263
|
-
|
48264
|
-
queryFn: () => {
|
48265
|
-
return operation(
|
48266
|
-
}
|
48267
|
-
...queryOptions || {}
|
48263
|
+
...queryOptions,
|
48264
|
+
queryFn: async () => {
|
48265
|
+
return operation(client.current);
|
48266
|
+
}
|
48268
48267
|
});
|
48269
48268
|
}
|
48270
|
-
|
48271
|
-
// src/useLetta/useLetta.ts
|
48272
|
-
var import_letta_client = __toESM(require_letta_client());
|
48273
|
-
import { useState } from "react";
|
48274
|
-
function useLetta(options = {}) {
|
48275
|
-
const [client] = useState(new import_letta_client.LettaClient(options));
|
48276
|
-
return client;
|
48277
|
-
}
|
48278
48269
|
export {
|
48279
|
-
getQueryKey,
|
48280
|
-
useLetta,
|
48281
48270
|
useLettaQuery
|
48282
48271
|
};
|
48283
48272
|
/*! Bundled license information:
|