@xmtp/agent-sdk 0.0.8 → 0.0.10
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 +131 -47
- package/dist/core/Agent.d.ts +18 -7
- package/dist/core/Agent.js +103 -41
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/ConversationContext.d.ts +12 -0
- package/dist/core/ConversationContext.js +22 -0
- package/dist/core/ConversationContext.js.map +1 -0
- package/dist/core/MessageContext.d.ts +18 -0
- package/dist/core/{AgentContext.js → MessageContext.js} +8 -22
- package/dist/core/MessageContext.js.map +1 -0
- package/dist/core/index.d.ts +2 -1
- package/dist/core/index.js +2 -1
- package/dist/core/index.js.map +1 -1
- package/dist/demo.js +19 -4
- package/dist/demo.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/middleware/CommandRouter.d.ts +1 -1
- package/dist/utils/filter.d.ts +3 -3
- package/dist/utils/filter.js +2 -2
- package/dist/utils/filter.js.map +1 -1
- package/dist/utils/message.d.ts +3 -0
- package/dist/utils/message.js +1 -0
- package/dist/utils/message.js.map +1 -1
- package/package.json +1 -1
- package/dist/core/AgentContext.d.ts +0 -14
- package/dist/core/AgentContext.js.map +0 -1
package/README.md
CHANGED
|
@@ -38,8 +38,8 @@ const agent = await Agent.create(signer, {
|
|
|
38
38
|
dbPath: null, // in-memory store; provide a path to persist
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
-
// 3. Respond to
|
|
42
|
-
agent.on("
|
|
41
|
+
// 3. Respond to text messages
|
|
42
|
+
agent.on("text", async (ctx) => {
|
|
43
43
|
await ctx.conversation.send("Hello from my XMTP Agent! 👋");
|
|
44
44
|
});
|
|
45
45
|
|
|
@@ -53,17 +53,15 @@ await agent.start();
|
|
|
53
53
|
|
|
54
54
|
## Environment Variables
|
|
55
55
|
|
|
56
|
-
The XMTP Agent SDK
|
|
56
|
+
The XMTP Agent SDK allows you to use environment variables (`process.env`) for easier configuration without modifying code. Simply set the following variables and call `Agent.createFromEnv()`:
|
|
57
57
|
|
|
58
58
|
**Available Variables:**
|
|
59
59
|
|
|
60
|
-
| Variable
|
|
61
|
-
|
|
|
62
|
-
| `XMTP_WALLET_KEY`
|
|
63
|
-
| `XMTP_ENV`
|
|
64
|
-
| `XMTP_DB_ENCRYPTION_KEY`
|
|
65
|
-
| `XMTP_FORCE_DEBUG` | [Activate debugging logs](https://docs.xmtp.org/agents/debug-agents) | `XMTP_FORCE_DEBUG=true` |
|
|
66
|
-
| `XMTP_FORCE_REVOKE_INSTALLATIONS` | [Remove other installations](https://docs.xmtp.org/agents/core-messaging/agent-installations#revoke-agent-installations) | `XMTP_FORCE_REVOKE_INSTALLATIONS=true` |
|
|
60
|
+
| Variable | Purpose | Example |
|
|
61
|
+
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------- | --------------------------------------- |
|
|
62
|
+
| `XMTP_WALLET_KEY` | [Private key for wallet](https://docs.xmtp.org/inboxes/core-messaging/create-a-signer) | `XMTP_WALLET_KEY=0x1234...abcd` |
|
|
63
|
+
| `XMTP_ENV` | [Network environment](https://docs.xmtp.org/agents/core-messaging/create-a-client#xmtp-network-environments) | `XMTP_ENV=dev` or `XMTP_ENV=production` |
|
|
64
|
+
| `XMTP_DB_ENCRYPTION_KEY` | [Database encryption key](https://docs.xmtp.org/agents/core-messaging/create-a-client#keep-the-database-encryption-key-safe) | `XMTP_DB_ENCRYPTION_KEY=0xabcd...1234` |
|
|
67
65
|
|
|
68
66
|
Using the environment variables, you can setup your agent in just a few lines of code:
|
|
69
67
|
|
|
@@ -72,24 +70,65 @@ Using the environment variables, you can setup your agent in just a few lines of
|
|
|
72
70
|
process.loadEnvFile(".env");
|
|
73
71
|
|
|
74
72
|
// Create agent using environment variables
|
|
75
|
-
const agent = await Agent.
|
|
73
|
+
const agent = await Agent.createFromEnv();
|
|
76
74
|
```
|
|
77
75
|
|
|
76
|
+
Agents can also recognize the following environment variables:
|
|
77
|
+
|
|
78
|
+
| Variable | Purpose | Example |
|
|
79
|
+
| ------------------ | -------------------------------------------------------------------- | ----------------------- |
|
|
80
|
+
| `XMTP_FORCE_DEBUG` | [Activate debugging logs](https://docs.xmtp.org/agents/debug-agents) | `XMTP_FORCE_DEBUG=true` |
|
|
81
|
+
|
|
78
82
|
## Core Concepts
|
|
79
83
|
|
|
80
84
|
### 1. Event‑Driven Architecture
|
|
81
85
|
|
|
82
|
-
Subscribe only to what you need using Node’s `EventEmitter` interface.
|
|
86
|
+
Subscribe only to what you need using Node’s `EventEmitter` interface. Events you can listen for:
|
|
87
|
+
|
|
88
|
+
**Message Events**
|
|
89
|
+
|
|
90
|
+
- `text` – a new incoming text message
|
|
91
|
+
- `reaction` – a new incoming reaction message
|
|
92
|
+
- `reply` – a new incoming reply message
|
|
93
|
+
- `attachment` – a new incoming remote attachment message
|
|
94
|
+
- `unhandledMessage` – a message that doesn't match any specific type
|
|
95
|
+
|
|
96
|
+
**Conversation Events**
|
|
83
97
|
|
|
84
|
-
|
|
98
|
+
- `dm` – a new DM conversation
|
|
99
|
+
- `group` – a new group conversation
|
|
85
100
|
|
|
86
|
-
|
|
87
|
-
|
|
101
|
+
**Lifecycle Events**
|
|
102
|
+
|
|
103
|
+
- `start` / `stop` – agent lifecycle events
|
|
88
104
|
- `unhandledError` – unhandled errors
|
|
89
105
|
|
|
90
|
-
**Example
|
|
106
|
+
**Example**
|
|
91
107
|
|
|
92
108
|
```ts
|
|
109
|
+
// Handle different message types
|
|
110
|
+
agent.on("text", async (ctx) => {
|
|
111
|
+
console.log(`Text message: ${ctx.text}`);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
agent.on("reaction", async (ctx) => {
|
|
115
|
+
console.log(`Reaction: ${ctx.message.content}`);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
agent.on("reply", async (ctx) => {
|
|
119
|
+
console.log(`Reply to: ${ctx.message.content.reference}`);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// Handle new conversations
|
|
123
|
+
agent.on("dm", async (ctx) => {
|
|
124
|
+
await ctx.conversation.send("Welcome to our DM!");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
agent.on("group", async (ctx) => {
|
|
128
|
+
await ctx.conversation.send("Hello group!");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Handle uncaught errors
|
|
93
132
|
agent.on("unhandledError", (error) => {
|
|
94
133
|
console.error("Agent error", error);
|
|
95
134
|
});
|
|
@@ -99,42 +138,98 @@ agent.on("unhandledError", (error) => {
|
|
|
99
138
|
|
|
100
139
|
Extend your agent with custom business logic using middlewares. Compose cross-cutting behavior like routing, telemetry, rate limiting, analytics, and feature flags, or plug in your own.
|
|
101
140
|
|
|
102
|
-
|
|
141
|
+
#### Standard Middleware
|
|
142
|
+
|
|
143
|
+
Middlewares can be registered with `agent.use` either one at a time or as an array. They are executed in the order they were added.
|
|
144
|
+
|
|
145
|
+
Middleware functions receive a `ctx` (context) object and a `next` function. Normally, a middleware calls `next()` to hand off control to the next one in the chain. However, a middleware can also alter the flow in the following ways:
|
|
146
|
+
|
|
147
|
+
1. Use `next()` to continue the chain and pass control to the next middleware
|
|
148
|
+
2. Use `return` to stop the chain and prevent events from firing
|
|
149
|
+
3. Use `throw` to trigger the error-handling middleware chain
|
|
150
|
+
|
|
151
|
+
**Example**
|
|
103
152
|
|
|
104
153
|
```ts
|
|
105
|
-
import {
|
|
154
|
+
import { Agent, AgentMiddleware } from "@xmtp/agent-sdk";
|
|
155
|
+
|
|
156
|
+
const onlyText: AgentMiddleware = async (ctx, next) => {
|
|
157
|
+
if (typeof ctx.message.content === "string") {
|
|
158
|
+
// Continue to next middleware
|
|
159
|
+
await next();
|
|
160
|
+
}
|
|
161
|
+
// Break middleware chain
|
|
162
|
+
return;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const agent = await Agent.createFromEnv();
|
|
166
|
+
agent.use(onlyText);
|
|
167
|
+
```
|
|
106
168
|
|
|
107
|
-
|
|
169
|
+
#### Error-Handling Middleware
|
|
108
170
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
171
|
+
Error middleware can be registered with `agent.errors.use` either one at a time or as an array. They are executed in the order they were added.
|
|
172
|
+
|
|
173
|
+
Error middleware receives the `error`, `ctx`, and a `next` function. Just like regular middleware, the flow in error middleware depends on how to use `next`:
|
|
174
|
+
|
|
175
|
+
1. Use `next()` to mark the error as handled and continue with the main middleware chain
|
|
176
|
+
2. Use `next(error)` to forward the original (or transformed) error to the next error handler
|
|
177
|
+
3. Use `return` to end error handling and stop the middleware chain
|
|
178
|
+
4. Use `throw` to raise a new error to be caught by the error chain
|
|
179
|
+
|
|
180
|
+
**Example**
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { Agent, AgentErrorMiddleware } from "@xmtp/agent-sdk";
|
|
184
|
+
|
|
185
|
+
const errorHandler: AgentErrorMiddleware = async (error, ctx, next) => {
|
|
186
|
+
if (error instanceof Error) {
|
|
187
|
+
// Transform the error and pass it along
|
|
188
|
+
await next(`Validation failed: ${error.message}`);
|
|
189
|
+
} else {
|
|
190
|
+
// Let other error handlers deal with it
|
|
191
|
+
await next(error);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const agent = await Agent.createFromEnv();
|
|
196
|
+
agent.errors.use(errorHandler);
|
|
197
|
+
```
|
|
112
198
|
|
|
113
|
-
|
|
199
|
+
#### Default Error Handler
|
|
200
|
+
|
|
201
|
+
Any error not handled by custom error middleware is caught by the default error handler and published to the `unhandledError` topic, where it can be observed.
|
|
202
|
+
|
|
203
|
+
**Example**
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
agent.on("unhandledError", (error) => {
|
|
207
|
+
console.log("Caught error", error);
|
|
208
|
+
});
|
|
114
209
|
```
|
|
115
210
|
|
|
116
211
|
### 3. Built‑in Filters
|
|
117
212
|
|
|
118
213
|
Instead of manually checking every incoming message, you can compose simple, reusable filters that make intent clear.
|
|
119
214
|
|
|
120
|
-
**Example
|
|
215
|
+
**Example**
|
|
121
216
|
|
|
122
217
|
```ts
|
|
123
218
|
import { withFilter, filter } from "@xmtp/agent-sdk";
|
|
124
219
|
|
|
125
220
|
// Using filter in message handler
|
|
126
221
|
agent.on(
|
|
127
|
-
"
|
|
222
|
+
"text",
|
|
128
223
|
withFilter(filter.startsWith("@agent"), async (ctx) => {
|
|
129
224
|
await ctx.conversation.send("How can I help you?");
|
|
130
225
|
}),
|
|
131
226
|
);
|
|
132
227
|
|
|
133
228
|
// Combination of filters
|
|
134
|
-
const combined = filter.and(filter.notFromSelf, filter.
|
|
229
|
+
const combined = filter.and(filter.notFromSelf, filter.isText);
|
|
135
230
|
|
|
136
231
|
agent.on(
|
|
137
|
-
"
|
|
232
|
+
"text",
|
|
138
233
|
withFilter(combined, async (ctx) => {
|
|
139
234
|
await ctx.conversation.send("You sent a text message ✅");
|
|
140
235
|
}),
|
|
@@ -148,40 +243,38 @@ For convenience, the `filter` object can also be imported as `f`:
|
|
|
148
243
|
import { filter, f } from "@xmtp/agent-sdk";
|
|
149
244
|
|
|
150
245
|
// Both work the same way:
|
|
151
|
-
const longVersion = filter.and(filter.notFromSelf, filter.
|
|
152
|
-
const shortVersion = f.and(f.notFromSelf, f.
|
|
246
|
+
const longVersion = filter.and(filter.notFromSelf, filter.isText);
|
|
247
|
+
const shortVersion = f.and(f.notFromSelf, f.isText);
|
|
153
248
|
```
|
|
154
249
|
|
|
155
250
|
You can find all available prebuilt filters [here](https://github.com/xmtp/xmtp-js/blob/main/sdks/agent-sdk/src/utils/filter.ts).
|
|
156
251
|
|
|
157
252
|
### 4. Rich Context
|
|
158
253
|
|
|
159
|
-
Every
|
|
254
|
+
Every message event handler receives a `MessageContext` with:
|
|
160
255
|
|
|
161
|
-
- `message` – decoded message
|
|
256
|
+
- `message` – the decoded message object
|
|
162
257
|
- `conversation` – the active conversation object
|
|
163
258
|
- `client` – underlying XMTP client
|
|
164
|
-
- Helpers like `
|
|
259
|
+
- Helpers like `sendTextReply()`, `sendReaction()`, `getSenderAddress`, and more
|
|
165
260
|
|
|
166
|
-
**Example
|
|
261
|
+
**Example**
|
|
167
262
|
|
|
168
263
|
```ts
|
|
169
|
-
agent.on("
|
|
264
|
+
agent.on("text", async (ctx) => {
|
|
170
265
|
await ctx.sendTextReply("Reply using helper ✨");
|
|
171
266
|
});
|
|
172
267
|
```
|
|
173
268
|
|
|
174
269
|
## Adding Custom Content Types
|
|
175
270
|
|
|
176
|
-
Pass codecs when creating your agent to extend supported content:
|
|
271
|
+
Pass `codecs` when creating your agent to extend supported content:
|
|
177
272
|
|
|
178
273
|
```ts
|
|
179
|
-
import { ReplyCodec } from "@xmtp/content-type-reply";
|
|
180
|
-
|
|
181
274
|
const agent = await Agent.create(signer, {
|
|
182
275
|
env: "dev",
|
|
183
276
|
dbPath: null,
|
|
184
|
-
codecs: [new
|
|
277
|
+
codecs: [new MyContentType()],
|
|
185
278
|
});
|
|
186
279
|
```
|
|
187
280
|
|
|
@@ -190,15 +283,6 @@ const agent = await Agent.create(signer, {
|
|
|
190
283
|
- [Debug an agent](https://docs.xmtp.org/agents/debug-agents)
|
|
191
284
|
- [Further debugging info](https://docs.xmtp.org/inboxes/debug-your-app#debug-your-inbox-app)
|
|
192
285
|
|
|
193
|
-
## FAQ (Quick Hits)
|
|
194
|
-
|
|
195
|
-
| Question | Answer |
|
|
196
|
-
| -------------------------------------------------------------------------------------------- | ----------------------------------------------- |
|
|
197
|
-
| Does middleware run for every message? | Yes, in the order added. |
|
|
198
|
-
| How do I reject a message early? | Don’t call `next()` in middleware. |
|
|
199
|
-
| How do I filter messages? | Use `withFilter(...)` around an event listener. |
|
|
200
|
-
| Can I send custom [content types](https://docs.xmtp.org/agents/content-types/content-types)? | Yes, register codecs during agent creation. |
|
|
201
|
-
|
|
202
286
|
## Contributing / Feedback
|
|
203
287
|
|
|
204
288
|
We’d love your feedback: [open an issue](https://github.com/xmtp/xmtp-js/issues) or discussion. PRs welcome for docs, examples, and core improvements.
|
package/dist/core/Agent.d.ts
CHANGED
|
@@ -3,13 +3,21 @@ import type { ContentCodec } from "@xmtp/content-type-primitives";
|
|
|
3
3
|
import { ReactionCodec } from "@xmtp/content-type-reaction";
|
|
4
4
|
import { RemoteAttachmentCodec } from "@xmtp/content-type-remote-attachment";
|
|
5
5
|
import { ReplyCodec } from "@xmtp/content-type-reply";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import type { TextCodec } from "@xmtp/content-type-text";
|
|
7
|
+
import { Client, Dm, Group, type ClientOptions } from "@xmtp/node-sdk";
|
|
8
|
+
import { ConversationContext } from "./ConversationContext.js";
|
|
9
|
+
import { AgentContext } from "./MessageContext.js";
|
|
8
10
|
interface EventHandlerMap<ContentTypes> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
attachment: [ctx: AgentContext<ReturnType<RemoteAttachmentCodec["decode"]>>];
|
|
12
|
+
dm: [ctx: ConversationContext<ContentTypes, Dm<ContentTypes>>];
|
|
13
|
+
group: [ctx: ConversationContext<ContentTypes, Group<ContentTypes>>];
|
|
14
|
+
reaction: [ctx: AgentContext<ReturnType<ReactionCodec["decode"]>>];
|
|
15
|
+
reply: [ctx: AgentContext<ReturnType<ReplyCodec["decode"]>>];
|
|
11
16
|
start: [];
|
|
12
17
|
stop: [];
|
|
18
|
+
text: [ctx: AgentContext<ReturnType<TextCodec["decode"]>>];
|
|
19
|
+
unhandledError: [error: Error];
|
|
20
|
+
unhandledMessage: [ctx: AgentContext<ContentTypes>];
|
|
13
21
|
}
|
|
14
22
|
export interface AgentOptions<ContentTypes> {
|
|
15
23
|
client: Client<ContentTypes>;
|
|
@@ -24,13 +32,16 @@ export interface AgentErrorRegistrar<ContentTypes> {
|
|
|
24
32
|
export declare class Agent<ContentTypes> extends EventEmitter<EventHandlerMap<ContentTypes>> {
|
|
25
33
|
#private;
|
|
26
34
|
constructor({ client }: AgentOptions<ContentTypes>);
|
|
27
|
-
static create<ContentCodecs extends ContentCodec[] = []>(signer
|
|
35
|
+
static create<ContentCodecs extends ContentCodec[] = []>(signer: Parameters<typeof Client.create>[0], options?: Omit<ClientOptions, "codecs"> & {
|
|
36
|
+
codecs?: ContentCodecs;
|
|
37
|
+
}): Promise<Agent<import("@xmtp/node-sdk").ExtractCodecContentTypes<(RemoteAttachmentCodec | ReactionCodec | ReplyCodec | (never[] | ContentCodecs)[number])[]>>>;
|
|
38
|
+
static createFromEnv<ContentCodecs extends ContentCodec[] = []>(options?: Omit<ClientOptions, "codecs"> & {
|
|
28
39
|
codecs?: ContentCodecs;
|
|
29
|
-
}): Promise<Agent<import("@xmtp/node-sdk").ExtractCodecContentTypes<(
|
|
40
|
+
}): Promise<Agent<import("@xmtp/node-sdk").ExtractCodecContentTypes<(RemoteAttachmentCodec | ReactionCodec | ReplyCodec | (never[] | ContentCodecs)[number])[]>>>;
|
|
30
41
|
use(...middleware: Array<AgentMiddleware<ContentTypes> | AgentMiddleware<ContentTypes>[]>): this;
|
|
31
42
|
start(options?: StreamAllMessagesOptions<ContentTypes>): Promise<void>;
|
|
32
43
|
get client(): Client<ContentTypes>;
|
|
33
44
|
get errors(): AgentErrorRegistrar<ContentTypes>;
|
|
34
|
-
stop(): void
|
|
45
|
+
stop(): Promise<void>;
|
|
35
46
|
}
|
|
36
47
|
export {};
|
package/dist/core/Agent.js
CHANGED
|
@@ -2,15 +2,17 @@ import EventEmitter from "node:events";
|
|
|
2
2
|
import { ReactionCodec } from "@xmtp/content-type-reaction";
|
|
3
3
|
import { RemoteAttachmentCodec } from "@xmtp/content-type-remote-attachment";
|
|
4
4
|
import { ReplyCodec } from "@xmtp/content-type-reply";
|
|
5
|
-
import { ApiUrls, Client, } from "@xmtp/node-sdk";
|
|
5
|
+
import { ApiUrls, Client, Dm, Group, } from "@xmtp/node-sdk";
|
|
6
6
|
import { isHex } from "viem/utils";
|
|
7
7
|
import { getEncryptionKeyFromHex } from "../utils/crypto.js";
|
|
8
|
-
import { logDetails } from "../utils/debug.js";
|
|
9
8
|
import { filter } from "../utils/filter.js";
|
|
9
|
+
import { hasDefinedContent, isReaction, isRemoteAttachment, isReply, isText, } from "../utils/message.js";
|
|
10
10
|
import { createSigner, createUser } from "../utils/user.js";
|
|
11
|
-
import {
|
|
11
|
+
import { ConversationContext } from "./ConversationContext.js";
|
|
12
|
+
import { AgentContext } from "./MessageContext.js";
|
|
12
13
|
export class Agent extends EventEmitter {
|
|
13
14
|
#client;
|
|
15
|
+
#conversationsStream;
|
|
14
16
|
#middleware = [];
|
|
15
17
|
#errorMiddleware = [];
|
|
16
18
|
#isListening = false;
|
|
@@ -42,45 +44,41 @@ export class Agent extends EventEmitter {
|
|
|
42
44
|
static async create(signer,
|
|
43
45
|
// Note: we need to omit this so that "Client.create" can correctly infer the codecs.
|
|
44
46
|
options) {
|
|
45
|
-
|
|
46
|
-
if (isHex(process.env.XMTP_WALLET_KEY)) {
|
|
47
|
-
signer = createSigner(createUser(process.env.XMTP_WALLET_KEY));
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
throw new Error(`No signer detected. Provide a "signer" to "Agent.create()" or set the "XMTP_WALLET_KEY" environment variable to a private key in hexadecimal format. Read more: https://docs.xmtp.org/inboxes/core-messaging/create-a-signer`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
const initializedOptions = { ...options };
|
|
47
|
+
const initializedOptions = { ...(options ?? {}) };
|
|
54
48
|
initializedOptions.appVersion ??= "agent-sdk/alpha";
|
|
55
|
-
if (process.env.XMTP_DB_ENCRYPTION_KEY) {
|
|
56
|
-
initializedOptions.dbEncryptionKey = getEncryptionKeyFromHex(process.env.XMTP_DB_ENCRYPTION_KEY);
|
|
57
|
-
}
|
|
58
|
-
if (process.env.XMTP_ENV &&
|
|
59
|
-
Object.keys(ApiUrls).includes(process.env.XMTP_ENV)) {
|
|
60
|
-
initializedOptions.env = process.env.XMTP_ENV;
|
|
61
|
-
}
|
|
62
|
-
if (process.env.XMTP_FORCE_DEBUG) {
|
|
63
|
-
initializedOptions.debugEventsEnabled = true;
|
|
64
|
-
initializedOptions.loggingLevel = "warn" /* LogLevel.warn */;
|
|
65
|
-
initializedOptions.structuredLogging = true;
|
|
66
|
-
}
|
|
67
49
|
const upgradedCodecs = [
|
|
68
50
|
...(initializedOptions.codecs ?? []),
|
|
69
51
|
new ReactionCodec(),
|
|
70
52
|
new ReplyCodec(),
|
|
71
53
|
new RemoteAttachmentCodec(),
|
|
72
54
|
];
|
|
55
|
+
if (process.env.XMTP_FORCE_DEBUG) {
|
|
56
|
+
initializedOptions.debugEventsEnabled = true;
|
|
57
|
+
initializedOptions.loggingLevel = "warn" /* LogLevel.warn */;
|
|
58
|
+
initializedOptions.structuredLogging = true;
|
|
59
|
+
}
|
|
73
60
|
const client = await Client.create(signer, {
|
|
74
61
|
...initializedOptions,
|
|
75
62
|
codecs: upgradedCodecs,
|
|
76
63
|
});
|
|
77
|
-
|
|
78
|
-
|
|
64
|
+
return new Agent({ client });
|
|
65
|
+
}
|
|
66
|
+
static async createFromEnv(
|
|
67
|
+
// Note: we need to omit this so that "Client.create" can correctly infer the codecs.
|
|
68
|
+
options) {
|
|
69
|
+
if (!isHex(process.env.XMTP_WALLET_KEY)) {
|
|
70
|
+
throw new Error(`XMTP_WALLET_KEY env is not in hex (0x) format.`);
|
|
79
71
|
}
|
|
80
|
-
|
|
81
|
-
|
|
72
|
+
const signer = createSigner(createUser(process.env.XMTP_WALLET_KEY));
|
|
73
|
+
const initializedOptions = { ...(options ?? {}) };
|
|
74
|
+
if (process.env.XMTP_DB_ENCRYPTION_KEY) {
|
|
75
|
+
initializedOptions.dbEncryptionKey = getEncryptionKeyFromHex(process.env.XMTP_DB_ENCRYPTION_KEY);
|
|
82
76
|
}
|
|
83
|
-
|
|
77
|
+
if (process.env.XMTP_ENV &&
|
|
78
|
+
Object.keys(ApiUrls).includes(process.env.XMTP_ENV)) {
|
|
79
|
+
initializedOptions.env = process.env.XMTP_ENV;
|
|
80
|
+
}
|
|
81
|
+
return this.create(signer, initializedOptions);
|
|
84
82
|
}
|
|
85
83
|
use(...middleware) {
|
|
86
84
|
for (const mw of middleware) {
|
|
@@ -100,38 +98,98 @@ export class Agent extends EventEmitter {
|
|
|
100
98
|
try {
|
|
101
99
|
this.#isListening = true;
|
|
102
100
|
void this.emit("start");
|
|
103
|
-
|
|
104
|
-
|
|
101
|
+
this.#conversationsStream = await this.#client.conversations.stream({
|
|
102
|
+
onValue: async (conversation) => {
|
|
103
|
+
try {
|
|
104
|
+
if (conversation instanceof Group) {
|
|
105
|
+
this.emit("group", new ConversationContext({
|
|
106
|
+
conversation,
|
|
107
|
+
client: this.#client,
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
else if (conversation instanceof Dm) {
|
|
111
|
+
this.emit("dm", new ConversationContext({
|
|
112
|
+
conversation,
|
|
113
|
+
client: this.#client,
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
const recovered = await this.#runErrorChain(error, null);
|
|
119
|
+
if (!recovered)
|
|
120
|
+
await this.stop();
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
onError: async (error) => {
|
|
124
|
+
const recovered = await this.#runErrorChain(error, null);
|
|
125
|
+
if (!recovered)
|
|
126
|
+
await this.stop();
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
const messages = await this.#client.conversations.streamAllMessages(options);
|
|
130
|
+
for await (const message of messages) {
|
|
105
131
|
// The "stop()" method sets "isListening"
|
|
106
132
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
107
133
|
if (!this.#isListening)
|
|
108
134
|
break;
|
|
109
|
-
|
|
135
|
+
try {
|
|
136
|
+
switch (true) {
|
|
137
|
+
case isRemoteAttachment(message):
|
|
138
|
+
await this.#processMessage(message, "attachment");
|
|
139
|
+
break;
|
|
140
|
+
case isReaction(message):
|
|
141
|
+
await this.#processMessage(message, "reaction");
|
|
142
|
+
break;
|
|
143
|
+
case isReply(message):
|
|
144
|
+
await this.#processMessage(message, "reply");
|
|
145
|
+
break;
|
|
146
|
+
case isText(message):
|
|
147
|
+
await this.#processMessage(message, "text");
|
|
148
|
+
break;
|
|
149
|
+
default:
|
|
150
|
+
await this.#processMessage(message);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
const recovered = await this.#runErrorChain(error, null);
|
|
156
|
+
if (!recovered) {
|
|
157
|
+
await this.stop();
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
110
161
|
}
|
|
111
162
|
}
|
|
112
163
|
catch (error) {
|
|
113
164
|
this.#isListening = false;
|
|
114
165
|
const recovered = await this.#runErrorChain(error, null);
|
|
115
166
|
if (recovered) {
|
|
167
|
+
await this.stop();
|
|
116
168
|
queueMicrotask(() => this.start(options));
|
|
117
169
|
}
|
|
118
170
|
}
|
|
119
171
|
}
|
|
120
|
-
async #processMessage(message) {
|
|
172
|
+
async #processMessage(message, topic = "unhandledMessage") {
|
|
173
|
+
// Skip messages with undefined content (failed to decode)
|
|
174
|
+
if (!hasDefinedContent(message)) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
// Skip messages from agent itself
|
|
178
|
+
if (filter.fromSelf(message, this.#client)) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
121
181
|
let context = null;
|
|
122
182
|
const conversation = await this.#client.conversations.getConversationById(message.conversationId);
|
|
123
183
|
if (!conversation) {
|
|
124
184
|
throw new Error(`Failed to process message ID "${message.id}" for conversation ID "${message.conversationId}" because the conversation could not be found.`);
|
|
125
185
|
}
|
|
126
|
-
context = new AgentContext(message, conversation, this.#client);
|
|
127
|
-
await this.#runMiddlewareChain(context);
|
|
186
|
+
context = new AgentContext({ message, conversation, client: this.#client });
|
|
187
|
+
await this.#runMiddlewareChain(context, topic);
|
|
128
188
|
}
|
|
129
|
-
async #runMiddlewareChain(context) {
|
|
189
|
+
async #runMiddlewareChain(context, topic = "unhandledMessage") {
|
|
130
190
|
const finalEmit = async () => {
|
|
131
191
|
try {
|
|
132
|
-
|
|
133
|
-
this.emit("message", context);
|
|
134
|
-
}
|
|
192
|
+
this.emit(topic, context);
|
|
135
193
|
}
|
|
136
194
|
catch (error) {
|
|
137
195
|
await this.#runErrorChain(error, context);
|
|
@@ -170,6 +228,9 @@ export class Agent extends EventEmitter {
|
|
|
170
228
|
return flow;
|
|
171
229
|
}
|
|
172
230
|
catch (thrown) {
|
|
231
|
+
if (settled) {
|
|
232
|
+
return flow;
|
|
233
|
+
}
|
|
173
234
|
return { kind: "continue", error: thrown };
|
|
174
235
|
}
|
|
175
236
|
}
|
|
@@ -199,9 +260,10 @@ export class Agent extends EventEmitter {
|
|
|
199
260
|
get errors() {
|
|
200
261
|
return this.#errors;
|
|
201
262
|
}
|
|
202
|
-
stop() {
|
|
263
|
+
async stop() {
|
|
264
|
+
await this.#conversationsStream?.end();
|
|
203
265
|
this.#isListening = false;
|
|
204
|
-
|
|
266
|
+
this.emit("stop");
|
|
205
267
|
}
|
|
206
268
|
}
|
|
207
269
|
//# sourceMappingURL=Agent.js.map
|
package/dist/core/Agent.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Agent.js","sourceRoot":"","sources":["../../src/core/Agent.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"Agent.js","sourceRoot":"","sources":["../../src/core/Agent.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,sC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OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;QAC7C,CAAC;IACH,CAAC;IAED,KAAK,CAAC,cAAc,CAClB,KAAc,EACd,OAA0C;QAE1C,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,gBAAgB,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC;QAEpE,IAAI,YAAY,GAAY,KAAK,CAAC;QAElC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,gBAAgB,CACzC,KAAK,CAAC,CAAC,CAAC,EACR,OAAO,EACP,YAAY,CACb,CAAC;YAEF,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;gBACrB,KAAK,SAAS;oBACZ,mDAAmD;oBACnD,OAAO,IAAI,CAAC;gBACd,KAAK,SAAS;oBACZ,2DAA2D;oBAC3D,OAAO,KAAK,CAAC;gBACf,KAAK,UAAU;oBACb,sCAAsC;oBACtC,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC;YACjC,CAAC;QACH,CAAC;QAED,wCAAwC;QACxC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,IAAI;QACR,MAAM,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE,CAAC;QACvC,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;QAC1B,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACpB,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Client, Conversation } from "@xmtp/node-sdk";
|
|
2
|
+
export declare class ConversationContext<ContentTypes = unknown, ConversationType extends Conversation = Conversation> {
|
|
3
|
+
#private;
|
|
4
|
+
constructor({ conversation, client, }: {
|
|
5
|
+
conversation: ConversationType;
|
|
6
|
+
client: Client<ContentTypes>;
|
|
7
|
+
});
|
|
8
|
+
sendText(text: string): Promise<void>;
|
|
9
|
+
getOwnAddress(): string | undefined;
|
|
10
|
+
get conversation(): ConversationType;
|
|
11
|
+
get client(): Client<ContentTypes>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ContentTypeText } from "@xmtp/content-type-text";
|
|
2
|
+
export class ConversationContext {
|
|
3
|
+
#client;
|
|
4
|
+
#conversation;
|
|
5
|
+
constructor({ conversation, client, }) {
|
|
6
|
+
this.#conversation = conversation;
|
|
7
|
+
this.#client = client;
|
|
8
|
+
}
|
|
9
|
+
async sendText(text) {
|
|
10
|
+
await this.#conversation.send(text, ContentTypeText);
|
|
11
|
+
}
|
|
12
|
+
getOwnAddress() {
|
|
13
|
+
return this.#client.accountIdentifier?.identifier;
|
|
14
|
+
}
|
|
15
|
+
get conversation() {
|
|
16
|
+
return this.#conversation;
|
|
17
|
+
}
|
|
18
|
+
get client() {
|
|
19
|
+
return this.#client;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=ConversationContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConversationContext.js","sourceRoot":"","sources":["../../src/core/ConversationContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG1D,MAAM,OAAO,mBAAmB;IAI9B,OAAO,CAAuB;IAC9B,aAAa,CAAmB;IAEhC,YAAY,EACV,YAAY,EACZ,MAAM,GAIP;QACC,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,IAAY;QACzB,MAAM,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;IACvD,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,UAAU,CAAC;IACpD,CAAC;IAED,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Reaction } from "@xmtp/content-type-reaction";
|
|
2
|
+
import type { Client, Conversation, DecodedMessage } from "@xmtp/node-sdk";
|
|
3
|
+
import { ConversationContext } from "./ConversationContext.js";
|
|
4
|
+
export type MessageContext<ContentTypes = unknown> = DecodedMessage<ContentTypes> & {
|
|
5
|
+
content: ContentTypes;
|
|
6
|
+
};
|
|
7
|
+
export declare class AgentContext<ContentTypes = unknown> extends ConversationContext<ContentTypes> {
|
|
8
|
+
#private;
|
|
9
|
+
constructor({ message, conversation, client, }: {
|
|
10
|
+
message: MessageContext<ContentTypes>;
|
|
11
|
+
conversation: Conversation;
|
|
12
|
+
client: Client<ContentTypes>;
|
|
13
|
+
});
|
|
14
|
+
sendReaction(content: string, schema?: Reaction["schema"]): Promise<void>;
|
|
15
|
+
sendTextReply(text: string): Promise<void>;
|
|
16
|
+
getSenderAddress(): Promise<string>;
|
|
17
|
+
get message(): MessageContext<ContentTypes>;
|
|
18
|
+
}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { ContentTypeReaction, } from "@xmtp/content-type-reaction";
|
|
2
2
|
import { ContentTypeReply } from "@xmtp/content-type-reply";
|
|
3
3
|
import { ContentTypeText } from "@xmtp/content-type-text";
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import { ConversationContext } from "./ConversationContext.js";
|
|
5
|
+
export class AgentContext extends ConversationContext {
|
|
6
6
|
#message;
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
constructor({ message, conversation, client, }) {
|
|
8
|
+
super({ conversation, client });
|
|
9
9
|
this.#message = message;
|
|
10
|
-
this.#conversation = conversation;
|
|
11
|
-
this.#client = client;
|
|
12
10
|
}
|
|
13
11
|
async sendReaction(content, schema = "unicode") {
|
|
14
12
|
const reaction = {
|
|
@@ -18,10 +16,7 @@ export class AgentContext {
|
|
|
18
16
|
schema,
|
|
19
17
|
content,
|
|
20
18
|
};
|
|
21
|
-
await this
|
|
22
|
-
}
|
|
23
|
-
async sendText(text) {
|
|
24
|
-
await this.#conversation.send(text, ContentTypeText);
|
|
19
|
+
await this.conversation.send(reaction, ContentTypeReaction);
|
|
25
20
|
}
|
|
26
21
|
async sendTextReply(text) {
|
|
27
22
|
const reply = {
|
|
@@ -30,13 +25,10 @@ export class AgentContext {
|
|
|
30
25
|
contentType: ContentTypeText,
|
|
31
26
|
content: text,
|
|
32
27
|
};
|
|
33
|
-
await this
|
|
34
|
-
}
|
|
35
|
-
getOwnAddress() {
|
|
36
|
-
return this.#client.accountIdentifier?.identifier;
|
|
28
|
+
await this.conversation.send(reply, ContentTypeReply);
|
|
37
29
|
}
|
|
38
30
|
async getSenderAddress() {
|
|
39
|
-
const inboxState = await this
|
|
31
|
+
const inboxState = await this.client.preferences.inboxStateFromInboxIds([
|
|
40
32
|
this.#message.senderInboxId,
|
|
41
33
|
]);
|
|
42
34
|
return inboxState[0].identifiers[0].identifier;
|
|
@@ -44,11 +36,5 @@ export class AgentContext {
|
|
|
44
36
|
get message() {
|
|
45
37
|
return this.#message;
|
|
46
38
|
}
|
|
47
|
-
get conversation() {
|
|
48
|
-
return this.#conversation;
|
|
49
|
-
}
|
|
50
|
-
get client() {
|
|
51
|
-
return this.#client;
|
|
52
|
-
}
|
|
53
39
|
}
|
|
54
|
-
//# sourceMappingURL=
|
|
40
|
+
//# sourceMappingURL=MessageContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MessageContext.js","sourceRoot":"","sources":["../../src/core/MessageContext.ts"],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,CAAC,aAAa;SAC5B,CAAC,CAAC;QACH,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;IACjD,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF"}
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
package/dist/core/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC"}
|
package/dist/demo.js
CHANGED
|
@@ -9,7 +9,7 @@ try {
|
|
|
9
9
|
}
|
|
10
10
|
catch { }
|
|
11
11
|
const agent = process.env.XMTP_WALLET_KEY
|
|
12
|
-
? await Agent.
|
|
12
|
+
? await Agent.createFromEnv()
|
|
13
13
|
: await Agent.create(createSigner(createUser()), {
|
|
14
14
|
dbPath: null,
|
|
15
15
|
});
|
|
@@ -18,10 +18,25 @@ router.command("/version", async (ctx) => {
|
|
|
18
18
|
await ctx.conversation.send(`v${process.env.npm_package_version}`);
|
|
19
19
|
});
|
|
20
20
|
agent.use(router.middleware());
|
|
21
|
-
agent.on("
|
|
22
|
-
console.log("Got message:", ctx.
|
|
21
|
+
agent.on("dm", (ctx) => {
|
|
22
|
+
console.log("Got new direct message in:", ctx.conversation.id);
|
|
23
23
|
});
|
|
24
|
-
agent.on("
|
|
24
|
+
agent.on("group", (ctx) => {
|
|
25
|
+
console.log("Got new group message in:", ctx.conversation.id);
|
|
26
|
+
});
|
|
27
|
+
agent.on("attachment", (ctx) => {
|
|
28
|
+
console.log("Got attachment:", ctx.message.content);
|
|
29
|
+
});
|
|
30
|
+
agent.on("text", (ctx) => {
|
|
31
|
+
console.log("Got text:", ctx.message.content);
|
|
32
|
+
});
|
|
33
|
+
agent.on("reaction", (ctx) => {
|
|
34
|
+
console.log("Got reaction:", ctx.message.content);
|
|
35
|
+
});
|
|
36
|
+
agent.on("reply", (ctx) => {
|
|
37
|
+
console.log("Got reply:", ctx.message.content);
|
|
38
|
+
});
|
|
39
|
+
agent.on("text", withFilter(f.startsWith("@agent"), async (ctx) => {
|
|
25
40
|
await ctx.conversation.send("How can I help you?");
|
|
26
41
|
}));
|
|
27
42
|
const errorHandler = (error) => {
|
package/dist/demo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"demo.js","sourceRoot":"","sources":["../src/demo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE3E,IAAI,CAAC;IACH,WAAW,CAAC,MAAM,CAAC,CAAC;IACpB,OAAO,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;AAChD,CAAC;AAAC,MAAM,CAAC,CAAA,CAAC;AAEV,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe;IACvC,CAAC,CAAC,MAAM,KAAK,CAAC,
|
|
1
|
+
{"version":3,"file":"demo.js","sourceRoot":"","sources":["../src/demo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,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,CAAC,CAAC,CAAC;AAEH,KAAK,KAAK,CAAC,KAAK,EAAE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AgentMessageHandler, type AgentMiddleware } from "../core/Agent.js";
|
|
2
|
-
import type { AgentContext } from "../core/
|
|
2
|
+
import type { AgentContext } from "../core/MessageContext.js";
|
|
3
3
|
export declare class CommandRouter<ContentTypes> {
|
|
4
4
|
private commandMap;
|
|
5
5
|
private defaultHandler;
|
package/dist/utils/filter.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Client, DecodedMessage } from "@xmtp/node-sdk";
|
|
2
|
-
import type { AgentContext } from "../core/
|
|
2
|
+
import type { AgentContext } from "../core/MessageContext.js";
|
|
3
3
|
/**
|
|
4
4
|
* Function type for filtering messages based on content and client state.
|
|
5
5
|
*/
|
|
@@ -45,7 +45,7 @@ declare function not<ContentTypes>(filter: MessageFilter<ContentTypes>): Message
|
|
|
45
45
|
export declare const filter: {
|
|
46
46
|
notFromSelf: MessageFilter<unknown>;
|
|
47
47
|
fromSelf: MessageFilter<unknown>;
|
|
48
|
-
|
|
48
|
+
isText: MessageFilter<unknown>;
|
|
49
49
|
fromSender: typeof fromSender;
|
|
50
50
|
startsWith: typeof startsWith;
|
|
51
51
|
and: typeof and;
|
|
@@ -55,7 +55,7 @@ export declare const filter: {
|
|
|
55
55
|
export declare const f: {
|
|
56
56
|
notFromSelf: MessageFilter<unknown>;
|
|
57
57
|
fromSelf: MessageFilter<unknown>;
|
|
58
|
-
|
|
58
|
+
isText: MessageFilter<unknown>;
|
|
59
59
|
fromSender: typeof fromSender;
|
|
60
60
|
startsWith: typeof startsWith;
|
|
61
61
|
and: typeof and;
|
package/dist/utils/filter.js
CHANGED
|
@@ -25,7 +25,7 @@ function fromSelf() {
|
|
|
25
25
|
*
|
|
26
26
|
* @returns Filter function
|
|
27
27
|
*/
|
|
28
|
-
function
|
|
28
|
+
function isText() {
|
|
29
29
|
return (message) => {
|
|
30
30
|
return !!message.contentType?.sameAs(ContentTypeText);
|
|
31
31
|
};
|
|
@@ -106,7 +106,7 @@ export const filter = {
|
|
|
106
106
|
// basic filters
|
|
107
107
|
notFromSelf: notFromSelf(),
|
|
108
108
|
fromSelf: fromSelf(),
|
|
109
|
-
|
|
109
|
+
isText: isText(),
|
|
110
110
|
// factory functions
|
|
111
111
|
fromSender,
|
|
112
112
|
startsWith,
|
package/dist/utils/filter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter.js","sourceRoot":"","sources":["../../src/utils/filter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG1D,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAU9C;;;;GAIG;AACH,SAAS,WAAW;IAClB,OAAO,CAAC,OAAuB,EAAE,MAA4B,EAAE,EAAE;QAC/D,OAAO,OAAO,CAAC,aAAa,KAAK,MAAM,CAAC,OAAO,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,SAAS,QAAQ;IACf,OAAO,CAAC,OAAuB,EAAE,MAA4B,EAAE,EAAE;QAC/D,OAAO,OAAO,CAAC,aAAa,KAAK,MAAM,CAAC,OAAO,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,SAAS,
|
|
1
|
+
{"version":3,"file":"filter.js","sourceRoot":"","sources":["../../src/utils/filter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG1D,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAU9C;;;;GAIG;AACH,SAAS,WAAW;IAClB,OAAO,CAAC,OAAuB,EAAE,MAA4B,EAAE,EAAE;QAC/D,OAAO,OAAO,CAAC,aAAa,KAAK,MAAM,CAAC,OAAO,CAAC;IAClD,CAAC,CAAC;AACJ,CAAC;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CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,EAAE,CACT,GAAG,OAAsC;IAEzC,OAAO,CAAC,OAAuB,EAAE,MAA4B,EAAE,EAAE;QAC/D,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC7B,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YACvC,IAAI,MAAM;gBAAE,OAAO,IAAI,CAAC;QAC1B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,GAAG,CACV,MAAmC;IAEnC,OAAO,CAAC,OAAuB,EAAE,MAA4B,EAAE,EAAE;QAC/D,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAClC,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,gBAAgB;IAChB,WAAW,EAAE,WAAW,EAAE;IAC1B,QAAQ,EAAE,QAAQ,EAAE;IACpB,MAAM,EAAE,MAAM,EAAE;IAChB,oBAAoB;IACpB,UAAU;IACV,UAAU;IACV,cAAc;IACd,GAAG;IACH,EAAE;IACF,GAAG;CACJ,CAAC;AAEF,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;AAExB,MAAM,CAAC,MAAM,UAAU,GACrB,CACE,MAAmC,EACnC,QAAmD,EACnD,EAAE,CACJ,CAAC,GAA+B,EAAE,EAAE;IAClC,IAAI,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QACpC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAChB,CAAC;AACH,CAAC,CAAC"}
|
package/dist/utils/message.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ import { type RemoteAttachment } from "@xmtp/content-type-remote-attachment";
|
|
|
3
3
|
import { type Reply } from "@xmtp/content-type-reply";
|
|
4
4
|
import type { DecodedMessage } from "@xmtp/node-sdk";
|
|
5
5
|
type ContentMessage = Pick<DecodedMessage, "content" | "contentType" | "fallback">;
|
|
6
|
+
export declare const hasDefinedContent: <ContentTypes = unknown>(message: DecodedMessage<ContentTypes>) => message is DecodedMessage<ContentTypes> & {
|
|
7
|
+
content: ContentTypes;
|
|
8
|
+
};
|
|
6
9
|
export declare const isReaction: <M extends ContentMessage>(m: M) => m is M & {
|
|
7
10
|
content: Reaction;
|
|
8
11
|
};
|
package/dist/utils/message.js
CHANGED
|
@@ -2,6 +2,7 @@ import { ContentTypeReaction, } from "@xmtp/content-type-reaction";
|
|
|
2
2
|
import { ContentTypeRemoteAttachment, } from "@xmtp/content-type-remote-attachment";
|
|
3
3
|
import { ContentTypeReply } from "@xmtp/content-type-reply";
|
|
4
4
|
import { ContentTypeText } from "@xmtp/content-type-text";
|
|
5
|
+
export const hasDefinedContent = (message) => message.content !== undefined;
|
|
5
6
|
export const isReaction = (m) => !!m.contentType?.sameAs(ContentTypeReaction);
|
|
6
7
|
export const isReply = (m) => !!m.contentType?.sameAs(ContentTypeReply);
|
|
7
8
|
export const isTextReply = (m) => isReply(m) && typeof m.content.content === "string";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../src/utils/message.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,GAEpB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,2BAA2B,GAE5B,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAc,MAAM,0BAA0B,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAQ1D,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,CAAI,EAC4B,EAAE,CAClC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAE/C,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,CAAI,EACyB,EAA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,SAAS,CAAC;AAEnC,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,CAAI,EAC4B,EAAE,CAClC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAE/C,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,CAAI,EACyB,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAE5E,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,CAAI,EAC+C,EAAE,CACrD,OAAO,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,OAAO,CAAC,OAAO,KAAK,QAAQ,CAAC;AAEtD,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,CAAI,EAC0B,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC;AAE5E,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,CAAI,EACoC,EAAE,CAC1C,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,MAAM,CAAC,2BAA2B,CAAC,CAAC;AAEvD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,OAAuB,EAAE,EAAE;IACxD,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,UAAU,CAAC,OAAO,CAAC,CAAC;QACzB,KAAK,WAAW,CAAC,OAAO,CAAC;YACvB,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;QACjC,KAAK,MAAM,CAAC,OAAO,CAAC;YAClB,OAAO,OAAO,CAAC,OAAO,CAAC;IAC3B,CAAC;AACH,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { type Reaction } from "@xmtp/content-type-reaction";
|
|
2
|
-
import type { Client, Conversation, DecodedMessage } from "@xmtp/node-sdk";
|
|
3
|
-
export declare class AgentContext<ContentTypes = unknown> {
|
|
4
|
-
#private;
|
|
5
|
-
constructor(message: DecodedMessage<ContentTypes>, conversation: Conversation, client: Client<ContentTypes>);
|
|
6
|
-
sendReaction(content: string, schema?: Reaction["schema"]): Promise<void>;
|
|
7
|
-
sendText(text: string): Promise<void>;
|
|
8
|
-
sendTextReply(text: string): Promise<void>;
|
|
9
|
-
getOwnAddress(): string | undefined;
|
|
10
|
-
getSenderAddress(): Promise<string>;
|
|
11
|
-
get message(): DecodedMessage<ContentTypes>;
|
|
12
|
-
get conversation(): Conversation<unknown>;
|
|
13
|
-
get client(): Client<ContentTypes>;
|
|
14
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AgentContext.js","sourceRoot":"","sources":["../../src/core/AgentContext.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,GAEpB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,gBAAgB,EAAc,MAAM,0BAA0B,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG1D,MAAM,OAAO,YAAY;IACvB,OAAO,CAAuB;IAC9B,QAAQ,CAA+B;IACvC,aAAa,CAAe;IAE5B,YACE,OAAqC,EACrC,YAA0B,EAC1B,MAA4B;QAE5B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,OAAe,EAAE,SAA6B,SAAS;QACxE,MAAM,QAAQ,GAAa;YACzB,MAAM,EAAE,OAAO;YACf,SAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,EAAE;YAC3B,gBAAgB,EAAE,IAAI,CAAC,QAAQ,CAAC,aAAa;YAC7C,MAAM;YACN,OAAO;SACR,CAAC;QACF,MAAM,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,QAAQ,EAAE,mBAAmB,CAAC,C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