@xmtp/agent-sdk 0.0.3 → 0.0.4
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 -15
- package/dist/core/Agent.js +1 -1
- package/dist/utils/filter.d.ts +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,27 +1,83 @@
|
|
|
1
|
-
# XMTP Agent SDK
|
|
1
|
+
# XMTP Agent SDK
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
To keep up with the latest SDK developments, see the [Issues tab](https://github.com/xmtp/xmtp-js/issues) in this repo.
|
|
3
|
+
Build event‑driven, middleware‑powered messaging agents on the XMTP network. 🚀
|
|
6
4
|
|
|
7
5
|
> [!CAUTION]
|
|
8
6
|
> This SDK is in beta status and ready for you to build with in production. Software in this status may change based on feedback.
|
|
9
7
|
|
|
10
|
-
##
|
|
8
|
+
## Documentation
|
|
9
|
+
|
|
10
|
+
Full agent building guide: **[Build an XMTP Agent](https://docs.xmtp.org/agents/get-started/build-an-agent)**
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
This SDK is based on familiar Node.js patterns: you register event listeners, compose middleware, and extend behavior just like you would in frameworks such as [Express](https://expressjs.com/). This makes it easy to bring existing JavaScript and TypeScript skills into building conversational agents.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Choose your package manager:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @xmtp/agent-sdk @xmtp/node-sdk
|
|
20
|
+
# or
|
|
21
|
+
pnpm add @xmtp/agent-sdk @xmtp/node-sdk
|
|
22
|
+
# or
|
|
23
|
+
yarn add @xmtp/agent-sdk @xmtp/node-sdk
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
15
27
|
|
|
16
28
|
```ts
|
|
29
|
+
import { Agent, createSigner, createUser } from "@xmtp/agent-sdk";
|
|
30
|
+
|
|
31
|
+
// 1. Create a local user + signer (you can plug in your own wallet signer)
|
|
32
|
+
const user = createUser();
|
|
33
|
+
const signer = createSigner(user);
|
|
34
|
+
|
|
35
|
+
// 2. Spin up the agent
|
|
36
|
+
const agent = await Agent.create(signer, {
|
|
37
|
+
env: "dev", // or 'production'
|
|
38
|
+
dbPath: null, // in-memory store; provide a path to persist
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// 3. Respond to any incoming message
|
|
17
42
|
agent.on("message", async (ctx) => {
|
|
18
|
-
await ctx.conversation.send("Hello!");
|
|
43
|
+
await ctx.conversation.send("Hello from my XMTP Agent! 👋");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// 4. Log when we're ready
|
|
47
|
+
agent.on("start", () => {
|
|
48
|
+
const address = agent.client.accountIdentifier?.identifier;
|
|
49
|
+
const env = agent.client.options?.env;
|
|
50
|
+
console.log(`Agent online: http://xmtp.chat/dm/${address}?env=${env}`);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await agent.start();
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Core Concepts
|
|
57
|
+
|
|
58
|
+
### 1. Event‑Driven Architecture
|
|
59
|
+
|
|
60
|
+
Subscribe only to what you need using Node’s `EventEmitter` interface.
|
|
61
|
+
|
|
62
|
+
Events you can listen for:
|
|
63
|
+
|
|
64
|
+
- `message` – a new incoming (non‑self) message
|
|
65
|
+
- `start` / `stop` – lifecycle events
|
|
66
|
+
- `error` – surfaced errors
|
|
67
|
+
|
|
68
|
+
**Example:**
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
agent.on("error", (error) => {
|
|
72
|
+
console.error("Agent error", error);
|
|
19
73
|
});
|
|
20
74
|
```
|
|
21
75
|
|
|
22
|
-
### Middleware
|
|
76
|
+
### 2. Middleware Support
|
|
23
77
|
|
|
24
|
-
|
|
78
|
+
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.
|
|
79
|
+
|
|
80
|
+
**Example:**
|
|
25
81
|
|
|
26
82
|
```ts
|
|
27
83
|
const router = new CommandRouter();
|
|
@@ -30,19 +86,79 @@ router.command("/start", async (ctx) => {
|
|
|
30
86
|
await ctx.conversation.send("👋 Welcome to your XMTP agent!");
|
|
31
87
|
});
|
|
32
88
|
|
|
33
|
-
const agent = new Agent({ client });
|
|
34
89
|
agent.use(router.middleware());
|
|
35
90
|
```
|
|
36
91
|
|
|
37
|
-
### Built
|
|
92
|
+
### 3. Built‑in Filters
|
|
93
|
+
|
|
94
|
+
Instead of manually checking every incoming message, you can compose simple, reusable filters that make intent clear.
|
|
38
95
|
|
|
39
|
-
|
|
96
|
+
**Example:**
|
|
40
97
|
|
|
41
98
|
```ts
|
|
99
|
+
import { withFilter, filter } from "@xmtp/agent-sdk";
|
|
100
|
+
|
|
101
|
+
const filters = filter.and(filter.notFromSelf, filter.textOnly);
|
|
102
|
+
|
|
42
103
|
agent.on(
|
|
43
104
|
"message",
|
|
44
|
-
withFilter(
|
|
45
|
-
await ctx.conversation.send("
|
|
105
|
+
withFilter(filters, async (ctx) => {
|
|
106
|
+
await ctx.conversation.send("You sent a text message ✅");
|
|
46
107
|
}),
|
|
47
108
|
);
|
|
48
109
|
```
|
|
110
|
+
|
|
111
|
+
### 4. Rich Context
|
|
112
|
+
|
|
113
|
+
Every `message` handler receives an `AgentContext` with:
|
|
114
|
+
|
|
115
|
+
- `message` – decoded message
|
|
116
|
+
- `conversation` – the active conversation object
|
|
117
|
+
- `client` – underlying XMTP client
|
|
118
|
+
- Helpers like `sendText()` / `sendTextReply()`
|
|
119
|
+
|
|
120
|
+
**Example:**
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
agent.on("message", async (ctx) => {
|
|
124
|
+
await ctx.sendTextReply("Reply using helper ✨");
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Adding Custom Content Types
|
|
129
|
+
|
|
130
|
+
Pass codecs when creating your agent to extend supported content:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
import { ReplyCodec } from "@xmtp/content-type-reply";
|
|
134
|
+
|
|
135
|
+
const agent = await Agent.create(signer, {
|
|
136
|
+
env: "dev",
|
|
137
|
+
dbPath: null,
|
|
138
|
+
codecs: [new ReplyCodec()],
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Development Resources
|
|
143
|
+
|
|
144
|
+
- [Debug an agent](https://docs.xmtp.org/agents/debug-agents)
|
|
145
|
+
- [Further debugging info](https://docs.xmtp.org/inboxes/debug-your-app#debug-your-inbox-app)
|
|
146
|
+
|
|
147
|
+
## FAQ (Quick Hits)
|
|
148
|
+
|
|
149
|
+
| Question | Answer |
|
|
150
|
+
| -------------------------------------------------------------------------------------------- | ----------------------------------------------- |
|
|
151
|
+
| Does middleware run for every message? | Yes, in the order added. |
|
|
152
|
+
| How do I reject a message early? | Don’t call `next()` in middleware. |
|
|
153
|
+
| How do I filter messages? | Use `withFilter(...)` around an event listener. |
|
|
154
|
+
| Can I send custom [content types](https://docs.xmtp.org/agents/content-types/content-types)? | Yes, register codecs during agent creation. |
|
|
155
|
+
|
|
156
|
+
## Contributing / Feedback
|
|
157
|
+
|
|
158
|
+
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.
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
Build something delightful. Then tell us what you wish was easier.
|
|
163
|
+
|
|
164
|
+
Happy hacking 💫
|
package/dist/core/Agent.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import EventEmitter from "node:events";
|
|
2
2
|
import { Client, } from "@xmtp/node-sdk";
|
|
3
|
-
import { filter } from "
|
|
3
|
+
import { filter } from "../utils/filter.js";
|
|
4
4
|
import { AgentContext } from "./AgentContext.js";
|
|
5
5
|
export class Agent extends EventEmitter {
|
|
6
6
|
#client;
|
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 "
|
|
2
|
+
import type { AgentContext } from "../core/AgentContext.js";
|
|
3
3
|
/**
|
|
4
4
|
* Function type for filtering messages based on content and client state.
|
|
5
5
|
*/
|
package/package.json
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"@arethetypeswrong/cli": "^0.18.2",
|
|
14
14
|
"@types/node": "^22.16.3",
|
|
15
15
|
"@vitest/coverage-v8": "^3.2.4",
|
|
16
|
+
"tsc-alias": "^1.8.16",
|
|
16
17
|
"tsx": "^4.20.3",
|
|
17
18
|
"typescript": "^5.8.3",
|
|
18
19
|
"vite": "^7.0.4",
|
|
@@ -53,7 +54,7 @@
|
|
|
53
54
|
"url": "git+https://git@github.com/xmtp/xmtp-js.git"
|
|
54
55
|
},
|
|
55
56
|
"scripts": {
|
|
56
|
-
"build": "yarn clean:dist && tsc -p tsconfig.build.json && attw --pack . --ignore-rules cjs-resolves-to-esm",
|
|
57
|
+
"build": "yarn clean:dist && tsc -p tsconfig.build.json && tsc-alias && attw --pack . --ignore-rules cjs-resolves-to-esm",
|
|
57
58
|
"clean": "rm -rf .turbo && rm -rf node_modules && yarn clean:dist",
|
|
58
59
|
"clean:dist": "rm -rf dist",
|
|
59
60
|
"dev": "tsx --watch src/main.ts",
|
|
@@ -63,5 +64,5 @@
|
|
|
63
64
|
"typecheck": "tsc --noEmit"
|
|
64
65
|
},
|
|
65
66
|
"type": "module",
|
|
66
|
-
"version": "0.0.
|
|
67
|
+
"version": "0.0.4"
|
|
67
68
|
}
|