@xmtp/agent-sdk 0.0.11 → 0.0.13
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 +24 -21
- package/dist/core/Agent.d.ts +22 -18
- package/dist/core/Agent.js +29 -15
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/AgentContext.d.ts +9 -0
- package/dist/core/AgentContext.js +2 -0
- package/dist/core/AgentContext.js.map +1 -0
- package/dist/core/ClientContext.d.ts +9 -0
- package/dist/core/ClientContext.js +13 -0
- package/dist/core/ClientContext.js.map +1 -0
- package/dist/core/ConversationContext.d.ts +5 -4
- package/dist/core/ConversationContext.js +10 -9
- package/dist/core/ConversationContext.js.map +1 -1
- package/dist/core/MessageContext.d.ts +18 -10
- package/dist/core/MessageContext.js +15 -2
- package/dist/core/MessageContext.js.map +1 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/index.js.map +1 -1
- package/dist/demo.js +13 -5
- package/dist/demo.js.map +1 -1
- package/dist/middleware/CommandRouter.d.ts +2 -2
- package/dist/middleware/CommandRouter.js.map +1 -1
- package/dist/utils/error.d.ts +5 -0
- package/dist/utils/error.js +11 -0
- package/dist/utils/error.js.map +1 -0
- package/dist/utils/filter.d.ts +19 -75
- package/dist/utils/filter.js +51 -158
- package/dist/utils/filter.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -91,7 +91,7 @@ Subscribe only to what you need using Node’s `EventEmitter` interface. Events
|
|
|
91
91
|
- `reaction` – a new incoming reaction message
|
|
92
92
|
- `reply` – a new incoming reply message
|
|
93
93
|
- `attachment` – a new incoming remote attachment message
|
|
94
|
-
- `
|
|
94
|
+
- `unknownMessage` – a message that doesn't match any specific type
|
|
95
95
|
|
|
96
96
|
**Conversation Events**
|
|
97
97
|
|
|
@@ -210,30 +210,30 @@ agent.on("unhandledError", (error) => {
|
|
|
210
210
|
|
|
211
211
|
### 3. Built‑in Filters
|
|
212
212
|
|
|
213
|
-
Instead of manually checking every incoming message, you can
|
|
213
|
+
Instead of manually checking every incoming message, you can use the provided filters.
|
|
214
214
|
|
|
215
215
|
**Example**
|
|
216
216
|
|
|
217
217
|
```ts
|
|
218
|
-
import {
|
|
218
|
+
import { filter } from "@xmtp/agent-sdk";
|
|
219
219
|
|
|
220
220
|
// Using filter in message handler
|
|
221
|
-
agent.on(
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
await ctx.conversation.send("
|
|
235
|
-
}
|
|
236
|
-
);
|
|
221
|
+
agent.on("text", async (ctx) => {
|
|
222
|
+
if (filter.isText(ctx.message)) {
|
|
223
|
+
await ctx.conversation.send("You sent a text message!");
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// Combine multiple conditions
|
|
228
|
+
agent.on("text", async (ctx) => {
|
|
229
|
+
if (
|
|
230
|
+
filter.hasDefinedContent(ctx.message) &&
|
|
231
|
+
!filter.fromSelf(ctx.message, ctx.client) &&
|
|
232
|
+
filter.isText(ctx.message)
|
|
233
|
+
) {
|
|
234
|
+
await ctx.conversation.send("Valid text message received ✅");
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
237
|
```
|
|
238
238
|
|
|
239
239
|
For convenience, the `filter` object can also be imported as `f`:
|
|
@@ -243,10 +243,13 @@ For convenience, the `filter` object can also be imported as `f`:
|
|
|
243
243
|
import { filter, f } from "@xmtp/agent-sdk";
|
|
244
244
|
|
|
245
245
|
// Both work the same way:
|
|
246
|
-
|
|
247
|
-
|
|
246
|
+
if (f.isText(ctx.message)) {
|
|
247
|
+
// Handle message...
|
|
248
|
+
}
|
|
248
249
|
```
|
|
249
250
|
|
|
251
|
+
**Available Filters:**
|
|
252
|
+
|
|
250
253
|
You can find all available prebuilt filters [here](https://github.com/xmtp/xmtp-js/blob/main/sdks/agent-sdk/src/utils/filter.ts).
|
|
251
254
|
|
|
252
255
|
### 4. Rich Context
|
package/dist/core/Agent.d.ts
CHANGED
|
@@ -5,39 +5,43 @@ import { RemoteAttachmentCodec } from "@xmtp/content-type-remote-attachment";
|
|
|
5
5
|
import { ReplyCodec } from "@xmtp/content-type-reply";
|
|
6
6
|
import type { TextCodec } from "@xmtp/content-type-text";
|
|
7
7
|
import { Client, Dm, Group, type ClientOptions } from "@xmtp/node-sdk";
|
|
8
|
+
import type { AgentErrorContext } from "./AgentContext.js";
|
|
8
9
|
import { ConversationContext } from "./ConversationContext.js";
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
attachment: [
|
|
10
|
+
import { MessageContext } from "./MessageContext.js";
|
|
11
|
+
type EventHandlerMap<ContentTypes> = {
|
|
12
|
+
attachment: [
|
|
13
|
+
ctx: MessageContext<ReturnType<RemoteAttachmentCodec["decode"]>>
|
|
14
|
+
];
|
|
15
|
+
conversation: [ctx: ConversationContext<ContentTypes>];
|
|
12
16
|
dm: [ctx: ConversationContext<ContentTypes, Dm<ContentTypes>>];
|
|
13
17
|
group: [ctx: ConversationContext<ContentTypes, Group<ContentTypes>>];
|
|
14
|
-
reaction: [ctx:
|
|
15
|
-
reply: [ctx:
|
|
18
|
+
reaction: [ctx: MessageContext<ReturnType<ReactionCodec["decode"]>>];
|
|
19
|
+
reply: [ctx: MessageContext<ReturnType<ReplyCodec["decode"]>>];
|
|
16
20
|
start: [];
|
|
17
21
|
stop: [];
|
|
18
|
-
text: [ctx:
|
|
22
|
+
text: [ctx: MessageContext<ReturnType<TextCodec["decode"]>>];
|
|
19
23
|
unhandledError: [error: Error];
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
export
|
|
24
|
+
unknownMessage: [ctx: MessageContext<ContentTypes>];
|
|
25
|
+
};
|
|
26
|
+
export type AgentOptions<ContentTypes> = {
|
|
23
27
|
client: Client<ContentTypes>;
|
|
24
|
-
}
|
|
25
|
-
export type AgentMessageHandler<ContentTypes = unknown> = (ctx:
|
|
26
|
-
export type AgentMiddleware<ContentTypes = unknown> = (ctx:
|
|
27
|
-
export type AgentErrorMiddleware<ContentTypes = unknown> = (error: unknown, ctx:
|
|
28
|
+
};
|
|
29
|
+
export type AgentMessageHandler<ContentTypes = unknown> = (ctx: MessageContext<ContentTypes>) => Promise<void> | void;
|
|
30
|
+
export type AgentMiddleware<ContentTypes = unknown> = (ctx: MessageContext<ContentTypes>, next: () => Promise<void> | void) => Promise<void>;
|
|
31
|
+
export type AgentErrorMiddleware<ContentTypes = unknown> = (error: unknown, ctx: AgentErrorContext<ContentTypes>, next: (err?: unknown) => Promise<void> | void) => Promise<void> | void;
|
|
28
32
|
export type StreamAllMessagesOptions<ContentTypes> = Parameters<Client<ContentTypes>["conversations"]["streamAllMessages"]>[0];
|
|
29
|
-
export
|
|
33
|
+
export type AgentErrorRegistrar<ContentTypes> = {
|
|
30
34
|
use(...errorMiddleware: Array<AgentErrorMiddleware<ContentTypes> | AgentErrorMiddleware<ContentTypes>[]>): AgentErrorRegistrar<ContentTypes>;
|
|
31
|
-
}
|
|
32
|
-
export declare class Agent<ContentTypes> extends EventEmitter<EventHandlerMap<ContentTypes>> {
|
|
35
|
+
};
|
|
36
|
+
export declare class Agent<ContentTypes = unknown> extends EventEmitter<EventHandlerMap<ContentTypes>> {
|
|
33
37
|
#private;
|
|
34
38
|
constructor({ client }: AgentOptions<ContentTypes>);
|
|
35
39
|
static create<ContentCodecs extends ContentCodec[] = []>(signer: Parameters<typeof Client.create>[0], options?: Omit<ClientOptions, "codecs"> & {
|
|
36
40
|
codecs?: ContentCodecs;
|
|
37
|
-
}): Promise<Agent<import("@xmtp/node-sdk").ExtractCodecContentTypes<(
|
|
41
|
+
}): Promise<Agent<import("@xmtp/node-sdk").ExtractCodecContentTypes<(ReplyCodec | ReactionCodec | RemoteAttachmentCodec | (never[] | ContentCodecs)[number])[]>>>;
|
|
38
42
|
static createFromEnv<ContentCodecs extends ContentCodec[] = []>(options?: Omit<ClientOptions, "codecs"> & {
|
|
39
43
|
codecs?: ContentCodecs;
|
|
40
|
-
}): Promise<Agent<import("@xmtp/node-sdk").ExtractCodecContentTypes<(
|
|
44
|
+
}): Promise<Agent<import("@xmtp/node-sdk").ExtractCodecContentTypes<(ReplyCodec | ReactionCodec | RemoteAttachmentCodec | (never[] | ContentCodecs)[number])[]>>>;
|
|
41
45
|
use(...middleware: Array<AgentMiddleware<ContentTypes> | AgentMiddleware<ContentTypes>[]>): this;
|
|
42
46
|
start(options?: StreamAllMessagesOptions<ContentTypes>): Promise<void>;
|
|
43
47
|
get client(): Client<ContentTypes>;
|
package/dist/core/Agent.js
CHANGED
|
@@ -5,10 +5,12 @@ import { ReplyCodec } from "@xmtp/content-type-reply";
|
|
|
5
5
|
import { ApiUrls, Client, Dm, Group, } from "@xmtp/node-sdk";
|
|
6
6
|
import { fromString } from "uint8arrays/from-string";
|
|
7
7
|
import { isHex } from "viem/utils";
|
|
8
|
+
import { AgentError } from "../utils/error.js";
|
|
8
9
|
import { filter } from "../utils/filter.js";
|
|
9
10
|
import { createSigner, createUser } from "../utils/user.js";
|
|
11
|
+
import { ClientContext } from "./ClientContext.js";
|
|
10
12
|
import { ConversationContext } from "./ConversationContext.js";
|
|
11
|
-
import {
|
|
13
|
+
import { MessageContext } from "./MessageContext.js";
|
|
12
14
|
export class Agent extends EventEmitter {
|
|
13
15
|
#client;
|
|
14
16
|
#conversationsStream;
|
|
@@ -31,9 +33,7 @@ export class Agent extends EventEmitter {
|
|
|
31
33
|
#defaultErrorHandler = (currentError) => {
|
|
32
34
|
const emittedError = currentError instanceof Error
|
|
33
35
|
? currentError
|
|
34
|
-
: new
|
|
35
|
-
cause: currentError,
|
|
36
|
-
});
|
|
36
|
+
: new AgentError(9999, `Unhandled error caught by default error middleware.`, currentError);
|
|
37
37
|
this.emit("unhandledError", emittedError);
|
|
38
38
|
};
|
|
39
39
|
constructor({ client }) {
|
|
@@ -66,7 +66,7 @@ export class Agent extends EventEmitter {
|
|
|
66
66
|
// Note: we need to omit this so that "Client.create" can correctly infer the codecs.
|
|
67
67
|
options) {
|
|
68
68
|
if (!isHex(process.env.XMTP_WALLET_KEY)) {
|
|
69
|
-
throw new
|
|
69
|
+
throw new AgentError(1000, `XMTP_WALLET_KEY env is not in hex (0x) format.`);
|
|
70
70
|
}
|
|
71
71
|
const signer = createSigner(createUser(process.env.XMTP_WALLET_KEY));
|
|
72
72
|
const initializedOptions = { ...(options ?? {}) };
|
|
@@ -100,6 +100,13 @@ export class Agent extends EventEmitter {
|
|
|
100
100
|
this.#conversationsStream = await this.#client.conversations.stream({
|
|
101
101
|
onValue: async (conversation) => {
|
|
102
102
|
try {
|
|
103
|
+
if (!conversation) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
this.emit("conversation", new ConversationContext({
|
|
107
|
+
conversation,
|
|
108
|
+
client: this.#client,
|
|
109
|
+
}));
|
|
103
110
|
if (conversation instanceof Group) {
|
|
104
111
|
this.emit("group", new ConversationContext({
|
|
105
112
|
conversation,
|
|
@@ -114,13 +121,13 @@ export class Agent extends EventEmitter {
|
|
|
114
121
|
}
|
|
115
122
|
}
|
|
116
123
|
catch (error) {
|
|
117
|
-
const recovered = await this.#runErrorChain(error,
|
|
124
|
+
const recovered = await this.#runErrorChain(new AgentError(1001, "Emitted value from conversation stream caused an error.", error), new ClientContext({ client: this.#client }));
|
|
118
125
|
if (!recovered)
|
|
119
126
|
await this.stop();
|
|
120
127
|
}
|
|
121
128
|
},
|
|
122
129
|
onError: async (error) => {
|
|
123
|
-
const recovered = await this.#runErrorChain(error,
|
|
130
|
+
const recovered = await this.#runErrorChain(new AgentError(1002, "Error occured during conversation streaming.", error), new ClientContext({ client: this.#client }));
|
|
124
131
|
if (!recovered)
|
|
125
132
|
await this.stop();
|
|
126
133
|
},
|
|
@@ -151,7 +158,9 @@ export class Agent extends EventEmitter {
|
|
|
151
158
|
}
|
|
152
159
|
}
|
|
153
160
|
catch (error) {
|
|
154
|
-
const recovered = await this.#runErrorChain(error,
|
|
161
|
+
const recovered = await this.#runErrorChain(error, {
|
|
162
|
+
client: this.#client,
|
|
163
|
+
});
|
|
155
164
|
if (!recovered) {
|
|
156
165
|
await this.stop();
|
|
157
166
|
break;
|
|
@@ -161,31 +170,36 @@ export class Agent extends EventEmitter {
|
|
|
161
170
|
}
|
|
162
171
|
catch (error) {
|
|
163
172
|
this.#isListening = false;
|
|
164
|
-
const recovered = await this.#runErrorChain(error,
|
|
173
|
+
const recovered = await this.#runErrorChain(error, {
|
|
174
|
+
client: this.#client,
|
|
175
|
+
});
|
|
165
176
|
if (recovered) {
|
|
166
177
|
await this.stop();
|
|
167
178
|
queueMicrotask(() => this.start(options));
|
|
168
179
|
}
|
|
169
180
|
}
|
|
170
181
|
}
|
|
171
|
-
async #processMessage(message, topic = "
|
|
182
|
+
async #processMessage(message, topic = "unknownMessage") {
|
|
172
183
|
// Skip messages with undefined content (failed to decode)
|
|
173
|
-
if (!filter.
|
|
184
|
+
if (!filter.hasContent(message)) {
|
|
174
185
|
return;
|
|
175
186
|
}
|
|
176
187
|
// Skip messages from agent itself
|
|
177
188
|
if (filter.fromSelf(message, this.#client)) {
|
|
178
189
|
return;
|
|
179
190
|
}
|
|
180
|
-
let context = null;
|
|
181
191
|
const conversation = await this.#client.conversations.getConversationById(message.conversationId);
|
|
182
192
|
if (!conversation) {
|
|
183
|
-
throw new
|
|
193
|
+
throw new AgentError(1003, `Failed to process message ID "${message.id}" for conversation ID "${message.conversationId}" because the conversation could not be found.`);
|
|
184
194
|
}
|
|
185
|
-
context = new
|
|
195
|
+
const context = new MessageContext({
|
|
196
|
+
message,
|
|
197
|
+
conversation,
|
|
198
|
+
client: this.#client,
|
|
199
|
+
});
|
|
186
200
|
await this.#runMiddlewareChain(context, topic);
|
|
187
201
|
}
|
|
188
|
-
async #runMiddlewareChain(context, topic = "
|
|
202
|
+
async #runMiddlewareChain(context, topic = "unknownMessage") {
|
|
189
203
|
const finalEmit = async () => {
|
|
190
204
|
try {
|
|
191
205
|
this.emit(topic, context);
|
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;AAEtD,OAAO,EACL,OAAO,EACP,MAAM,EACN,EAAE,EACF,KAAK,
|
|
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;AAEtD,OAAO,EACL,OAAO,EACP,MAAM,EACN,EAAE,EACF,KAAK,GAMN,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE3D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,C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entTypes = unknown> = {
|
|
3
|
+
client: Client<ContentTypes>;
|
|
4
|
+
conversation: Conversation;
|
|
5
|
+
message: DecodedMessage;
|
|
6
|
+
};
|
|
7
|
+
export type AgentErrorContext<ContentTypes = unknown> = Partial<AgentBaseContext<ContentTypes>> & {
|
|
8
|
+
client: Client<ContentTypes>;
|
|
9
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AgentContext.js","sourceRoot":"","sources":["../../src/core/AgentContext.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Client } from "@xmtp/node-sdk";
|
|
2
|
+
export declare class ClientContext<ContentTypes = unknown> {
|
|
3
|
+
#private;
|
|
4
|
+
constructor({ client }: {
|
|
5
|
+
client: Client<ContentTypes>;
|
|
6
|
+
});
|
|
7
|
+
getClientAddress(): string | undefined;
|
|
8
|
+
get client(): Client<ContentTypes>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class ClientContext {
|
|
2
|
+
#client;
|
|
3
|
+
constructor({ client }) {
|
|
4
|
+
this.#client = client;
|
|
5
|
+
}
|
|
6
|
+
getClientAddress() {
|
|
7
|
+
return this.#client.accountIdentifier?.identifier;
|
|
8
|
+
}
|
|
9
|
+
get client() {
|
|
10
|
+
return this.#client;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=ClientContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClientContext.js","sourceRoot":"","sources":["../../src/core/ClientContext.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,aAAa;IACxB,OAAO,CAAuB;IAE9B,YAAY,EAAE,MAAM,EAAoC;QACtD,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,gBAAgB;QACd,OAAO,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,UAAU,CAAC;IACpD,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF"}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import type
|
|
2
|
-
|
|
1
|
+
import { type Client, type Conversation, type Dm, type Group } from "@xmtp/node-sdk";
|
|
2
|
+
import { ClientContext } from "./ClientContext.js";
|
|
3
|
+
export declare class ConversationContext<ContentTypes = unknown, ConversationType extends Conversation = Conversation> extends ClientContext<ContentTypes> {
|
|
3
4
|
#private;
|
|
4
5
|
constructor({ conversation, client, }: {
|
|
5
6
|
conversation: ConversationType;
|
|
6
7
|
client: Client<ContentTypes>;
|
|
7
8
|
});
|
|
9
|
+
isDm(): this is ConversationContext<ContentTypes, Dm<ContentTypes>>;
|
|
10
|
+
isGroup(): this is ConversationContext<ContentTypes, Group<ContentTypes>>;
|
|
8
11
|
sendText(text: string): Promise<void>;
|
|
9
|
-
getOwnAddress(): string | undefined;
|
|
10
12
|
get conversation(): ConversationType;
|
|
11
|
-
get client(): Client<ContentTypes>;
|
|
12
13
|
}
|
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import { ContentTypeText } from "@xmtp/content-type-text";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { filter } from "../utils/filter.js";
|
|
3
|
+
import { ClientContext } from "./ClientContext.js";
|
|
4
|
+
export class ConversationContext extends ClientContext {
|
|
4
5
|
#conversation;
|
|
5
6
|
constructor({ conversation, client, }) {
|
|
7
|
+
super({ client });
|
|
6
8
|
this.#conversation = conversation;
|
|
7
|
-
|
|
9
|
+
}
|
|
10
|
+
isDm() {
|
|
11
|
+
return filter.isDM(this.#conversation);
|
|
12
|
+
}
|
|
13
|
+
isGroup() {
|
|
14
|
+
return filter.isGroup(this.#conversation);
|
|
8
15
|
}
|
|
9
16
|
async sendText(text) {
|
|
10
17
|
await this.#conversation.send(text, ContentTypeText);
|
|
11
18
|
}
|
|
12
|
-
getOwnAddress() {
|
|
13
|
-
return this.#client.accountIdentifier?.identifier;
|
|
14
|
-
}
|
|
15
19
|
get conversation() {
|
|
16
20
|
return this.#conversation;
|
|
17
21
|
}
|
|
18
|
-
get client() {
|
|
19
|
-
return this.#client;
|
|
20
|
-
}
|
|
21
22
|
}
|
|
22
23
|
//# sourceMappingURL=ConversationContext.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConversationContext.js","sourceRoot":"","sources":["../../src/core/ConversationContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"ConversationContext.js","sourceRoot":"","sources":["../../src/core/ConversationContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAO1D,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,MAAM,OAAO,mBAGX,SAAQ,aAA2B;IACnC,aAAa,CAAmB;IAEhC,YAAY,EACV,YAAY,EACZ,MAAM,GAIP;QACC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QAClB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;IACpC,CAAC;IAED,IAAI;QACF,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IACzC,CAAC;IAED,OAAO;QACL,OAAO,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,IAAY;QACzB,MAAM,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;IACvD,CAAC;IAED,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;CACF"}
|
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import { type Reaction } from "@xmtp/content-type-reaction";
|
|
2
|
-
import
|
|
1
|
+
import { type Reaction, type ReactionCodec } from "@xmtp/content-type-reaction";
|
|
2
|
+
import { type RemoteAttachmentCodec } from "@xmtp/content-type-remote-attachment";
|
|
3
|
+
import { type ReplyCodec } from "@xmtp/content-type-reply";
|
|
4
|
+
import { type TextCodec } from "@xmtp/content-type-text";
|
|
5
|
+
import type { DecodedMessage } from "@xmtp/node-sdk";
|
|
6
|
+
import type { AgentBaseContext } from "./AgentContext.js";
|
|
3
7
|
import { ConversationContext } from "./ConversationContext.js";
|
|
4
|
-
|
|
8
|
+
type DecodedMessageWithContent<ContentTypes = unknown> = DecodedMessage<ContentTypes> & {
|
|
5
9
|
content: ContentTypes;
|
|
6
10
|
};
|
|
7
|
-
export
|
|
11
|
+
export type MessageContextParams<ContentTypes = unknown> = Omit<AgentBaseContext<ContentTypes>, "message"> & {
|
|
12
|
+
message: DecodedMessageWithContent<ContentTypes>;
|
|
13
|
+
};
|
|
14
|
+
export declare class MessageContext<ContentTypes = unknown> extends ConversationContext<ContentTypes> {
|
|
8
15
|
#private;
|
|
9
|
-
constructor({ message, conversation, client, }:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
constructor({ message, conversation, client, }: MessageContextParams<ContentTypes>);
|
|
17
|
+
isText(): this is MessageContext<ReturnType<TextCodec["decode"]>>;
|
|
18
|
+
isReply(): this is MessageContext<ReturnType<ReplyCodec["decode"]>>;
|
|
19
|
+
isReaction(): this is MessageContext<ReturnType<ReactionCodec["decode"]>>;
|
|
20
|
+
isRemoteAttachment(): this is MessageContext<ReturnType<RemoteAttachmentCodec["decode"]>>;
|
|
14
21
|
sendReaction(content: string, schema?: Reaction["schema"]): Promise<void>;
|
|
15
22
|
sendTextReply(text: string): Promise<void>;
|
|
16
23
|
getSenderAddress(): Promise<string>;
|
|
17
|
-
get message():
|
|
24
|
+
get message(): DecodedMessageWithContent<ContentTypes>;
|
|
18
25
|
}
|
|
26
|
+
export {};
|
|
@@ -1,13 +1,26 @@
|
|
|
1
1
|
import { ContentTypeReaction, } from "@xmtp/content-type-reaction";
|
|
2
|
-
import { ContentTypeReply } from "@xmtp/content-type-reply";
|
|
2
|
+
import { ContentTypeReply, } from "@xmtp/content-type-reply";
|
|
3
3
|
import { ContentTypeText } from "@xmtp/content-type-text";
|
|
4
|
+
import { filter } from "../utils/filter.js";
|
|
4
5
|
import { ConversationContext } from "./ConversationContext.js";
|
|
5
|
-
export class
|
|
6
|
+
export class MessageContext extends ConversationContext {
|
|
6
7
|
#message;
|
|
7
8
|
constructor({ message, conversation, client, }) {
|
|
8
9
|
super({ conversation, client });
|
|
9
10
|
this.#message = message;
|
|
10
11
|
}
|
|
12
|
+
isText() {
|
|
13
|
+
return filter.isText(this.#message);
|
|
14
|
+
}
|
|
15
|
+
isReply() {
|
|
16
|
+
return filter.isReply(this.#message);
|
|
17
|
+
}
|
|
18
|
+
isReaction() {
|
|
19
|
+
return filter.isReaction(this.#message);
|
|
20
|
+
}
|
|
21
|
+
isRemoteAttachment() {
|
|
22
|
+
return filter.isRemoteAttachment(this.#message);
|
|
23
|
+
}
|
|
11
24
|
async sendReaction(content, schema = "unicode") {
|
|
12
25
|
const reaction = {
|
|
13
26
|
action: "added",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MessageContext.js","sourceRoot":"","sources":["../../src/core/MessageContext.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,
|
|
1
|
+
{"version":3,"file":"MessageContext.js","sourceRoot":"","sources":["../../src/core/MessageContext.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,GAGpB,MAAM,6BAA6B,CAAC;AAErC,OAAO,EACL,gBAAgB,GAGjB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,eAAe,EAAkB,MAAM,yBAAyB,CAAC;AAE1E,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAc/D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,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,sBAAsB,CAAC;YACtE,IAAI,CAAC,QAAQ,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,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC"}
|
package/dist/demo.js
CHANGED
|
@@ -2,7 +2,7 @@ import { loadEnvFile } from "node:process";
|
|
|
2
2
|
import { Agent } from "./core/index.js";
|
|
3
3
|
import { CommandRouter } from "./middleware/CommandRouter.js";
|
|
4
4
|
import { getTestUrl } from "./utils/debug.js";
|
|
5
|
-
import { createSigner, createUser
|
|
5
|
+
import { AgentError, createSigner, createUser } from "./utils/index.js";
|
|
6
6
|
try {
|
|
7
7
|
loadEnvFile(".env");
|
|
8
8
|
console.info(`Loaded keys from ".env" file.`);
|
|
@@ -36,11 +36,19 @@ agent.on("reaction", (ctx) => {
|
|
|
36
36
|
agent.on("reply", (ctx) => {
|
|
37
37
|
console.log("Got reply:", ctx.message.content);
|
|
38
38
|
});
|
|
39
|
-
agent.on("text",
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
agent.on("text", async (ctx) => {
|
|
40
|
+
if (ctx.message.content.startsWith("@agent")) {
|
|
41
|
+
await ctx.conversation.send("How can I help you?");
|
|
42
|
+
}
|
|
43
|
+
});
|
|
42
44
|
const errorHandler = (error) => {
|
|
43
|
-
|
|
45
|
+
if (error instanceof AgentError) {
|
|
46
|
+
console.log(`Caught error ID "${error.code}"`, error);
|
|
47
|
+
console.log("Original error", error.cause);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
console.log(`Caught error`, error);
|
|
51
|
+
}
|
|
44
52
|
};
|
|
45
53
|
agent.on("unhandledError", errorHandler);
|
|
46
54
|
agent.on("start", () => {
|
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,
|
|
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,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAExE,IAAI,CAAC;IACH,WAAW,CAAC,MAAM,CAAC,CAAC;IACpB,OAAO,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC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AC,CAAC;AACrD,CAAC,CAAC,CAAC;AAEH,KAAK,KAAK,CAAC,KAAK,EAAE,CAAC"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { type AgentMessageHandler, type AgentMiddleware } from "../core/Agent.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { MessageContext } from "../core/MessageContext.js";
|
|
3
3
|
export declare class CommandRouter<ContentTypes> {
|
|
4
4
|
private commandMap;
|
|
5
5
|
private defaultHandler;
|
|
6
6
|
command(command: string, handler: AgentMessageHandler): this;
|
|
7
7
|
default(handler: AgentMessageHandler): this;
|
|
8
|
-
handle(ctx:
|
|
8
|
+
handle(ctx: MessageContext): Promise<boolean>;
|
|
9
9
|
middleware: () => AgentMiddleware<ContentTypes>;
|
|
10
10
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommandRouter.js","sourceRoot":"","sources":["../../src/middleware/CommandRouter.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C,MAAM,OAAO,aAAa;IAChB,UAAU,GAAG,IAAI,GAAG,EAA+B,CAAC;IACpD,cAAc,GAA+B,IAAI,CAAC;IAE1D,OAAO,CAAC,OAAe,EAAE,OAA4B;QACnD,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;QACD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,OAAO,CAAC,CAAC;QACpD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,CAAC,OAA4B;QAClC,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC;QAC9B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"CommandRouter.js","sourceRoot":"","sources":["../../src/middleware/CommandR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Ac,EAAE,CAAC;YACxB,MAAM,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;YAC/B,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED,UAAU,GAAwC,GAAG,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC1E,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACvC,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,EAAE,CAAC;QACf,CAAC;IACH,CAAC,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/utils/error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,UAAW,SAAQ,KAAK;IACnC,KAAK,CAAS;IAEd,YAAY,IAAY,EAAE,OAAe,EAAE,KAAe;QACxD,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC1B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF"}
|
package/dist/utils/filter.d.ts
CHANGED
|
@@ -1,54 +1,16 @@
|
|
|
1
1
|
import { type Reaction } from "@xmtp/content-type-reaction";
|
|
2
2
|
import { type RemoteAttachment } from "@xmtp/content-type-remote-attachment";
|
|
3
3
|
import { type Reply } from "@xmtp/content-type-reply";
|
|
4
|
-
import type
|
|
5
|
-
import type { AgentContext } from "../core/MessageContext.js";
|
|
6
|
-
export type MessageFilter<ContentTypes> = (message: DecodedMessage, client: Client<ContentTypes>, conversation: Conversation) => boolean | Promise<boolean>;
|
|
7
|
-
/**
|
|
8
|
-
* Creates a filter for messages from specific senders
|
|
9
|
-
*
|
|
10
|
-
* @param senderInboxId - Single sender ID or array of sender IDs to match
|
|
11
|
-
* @returns Filter function
|
|
12
|
-
*/
|
|
13
|
-
declare function fromSender(senderInboxId: string | string[]): (message: DecodedMessage) => boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Creates a filter that matches text messages starting with a specific string.
|
|
16
|
-
*
|
|
17
|
-
* @param prefix - The string prefix to match against
|
|
18
|
-
* @returns Filter function
|
|
19
|
-
*/
|
|
20
|
-
declare function startsWith(prefix: string): (message: DecodedMessage) => boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Creates a filter that requires all provided filters to pass
|
|
23
|
-
*
|
|
24
|
-
* @param filters - Array of filters that must all return true
|
|
25
|
-
* @returns Filter function
|
|
26
|
-
*/
|
|
27
|
-
declare function and<ContentTypes>(...filters: MessageFilter<ContentTypes>[]): MessageFilter<ContentTypes>;
|
|
28
|
-
/**
|
|
29
|
-
* Creates a filter that requires at least one provided filter to pass.
|
|
30
|
-
*
|
|
31
|
-
* @param filters - Array of filters where at least one must return true
|
|
32
|
-
* @returns Filter function
|
|
33
|
-
*/
|
|
34
|
-
declare function or<ContentTypes>(...filters: MessageFilter<ContentTypes>[]): MessageFilter<ContentTypes>;
|
|
35
|
-
/**
|
|
36
|
-
* Creates a filter that inverts the result of another filter.
|
|
37
|
-
*
|
|
38
|
-
* @param filter - The filter to negate
|
|
39
|
-
* @returns Filter function
|
|
40
|
-
*/
|
|
41
|
-
declare function not<ContentTypes>(filter: MessageFilter<ContentTypes>): MessageFilter<ContentTypes>;
|
|
42
|
-
/**
|
|
43
|
-
* Pre-configured filter instances and factory functions for common filtering scenarios
|
|
44
|
-
*/
|
|
4
|
+
import { Dm, Group, type Client, type Conversation, type DecodedMessage } from "@xmtp/node-sdk";
|
|
45
5
|
export declare const filter: {
|
|
46
|
-
fromSelf: (message: DecodedMessage
|
|
47
|
-
|
|
48
|
-
content:
|
|
49
|
-
};
|
|
50
|
-
isDM:
|
|
51
|
-
isGroup:
|
|
6
|
+
fromSelf: <ContentTypes>(message: DecodedMessage<ContentTypes>, client: Client<ContentTypes>) => boolean;
|
|
7
|
+
hasContent: <ContentTypes>(message: DecodedMessage<ContentTypes>) => message is DecodedMessage<ContentTypes> & {
|
|
8
|
+
content: NonNullable<ContentTypes>;
|
|
9
|
+
};
|
|
10
|
+
isDM: (conversation: Conversation) => conversation is Dm;
|
|
11
|
+
isGroup: (conversation: Conversation) => conversation is Group;
|
|
12
|
+
isGroupAdmin: (conversation: Conversation, message: DecodedMessage) => boolean;
|
|
13
|
+
isGroupSuperAdmin: (conversation: Conversation, message: DecodedMessage) => boolean;
|
|
52
14
|
isReaction: (message: DecodedMessage) => message is DecodedMessage & {
|
|
53
15
|
content: Reaction;
|
|
54
16
|
};
|
|
@@ -61,24 +23,17 @@ export declare const filter: {
|
|
|
61
23
|
isText: (message: DecodedMessage) => message is DecodedMessage & {
|
|
62
24
|
content: string;
|
|
63
25
|
};
|
|
64
|
-
isTextReply: (message: DecodedMessage) =>
|
|
65
|
-
content: Reply & {
|
|
66
|
-
content: string;
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
fromSender: typeof fromSender;
|
|
70
|
-
startsWith: typeof startsWith;
|
|
71
|
-
and: typeof and;
|
|
72
|
-
or: typeof or;
|
|
73
|
-
not: typeof not;
|
|
26
|
+
isTextReply: (message: DecodedMessage) => boolean;
|
|
74
27
|
};
|
|
75
28
|
export declare const f: {
|
|
76
|
-
fromSelf: (message: DecodedMessage
|
|
77
|
-
|
|
78
|
-
content:
|
|
79
|
-
};
|
|
80
|
-
isDM:
|
|
81
|
-
isGroup:
|
|
29
|
+
fromSelf: <ContentTypes>(message: DecodedMessage<ContentTypes>, client: Client<ContentTypes>) => boolean;
|
|
30
|
+
hasContent: <ContentTypes>(message: DecodedMessage<ContentTypes>) => message is DecodedMessage<ContentTypes> & {
|
|
31
|
+
content: NonNullable<ContentTypes>;
|
|
32
|
+
};
|
|
33
|
+
isDM: (conversation: Conversation) => conversation is Dm;
|
|
34
|
+
isGroup: (conversation: Conversation) => conversation is Group;
|
|
35
|
+
isGroupAdmin: (conversation: Conversation, message: DecodedMessage) => boolean;
|
|
36
|
+
isGroupSuperAdmin: (conversation: Conversation, message: DecodedMessage) => boolean;
|
|
82
37
|
isReaction: (message: DecodedMessage) => message is DecodedMessage & {
|
|
83
38
|
content: Reaction;
|
|
84
39
|
};
|
|
@@ -91,16 +46,5 @@ export declare const f: {
|
|
|
91
46
|
isText: (message: DecodedMessage) => message is DecodedMessage & {
|
|
92
47
|
content: string;
|
|
93
48
|
};
|
|
94
|
-
isTextReply: (message: DecodedMessage) =>
|
|
95
|
-
content: Reply & {
|
|
96
|
-
content: string;
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
fromSender: typeof fromSender;
|
|
100
|
-
startsWith: typeof startsWith;
|
|
101
|
-
and: typeof and;
|
|
102
|
-
or: typeof or;
|
|
103
|
-
not: typeof not;
|
|
49
|
+
isTextReply: (message: DecodedMessage) => boolean;
|
|
104
50
|
};
|
|
105
|
-
export declare const withFilter: <ContentTypes>(filterFn: MessageFilter<ContentTypes>, listener: (ctx: AgentContext<ContentTypes>) => void | Promise<void>) => (ctx: AgentContext<ContentTypes>) => Promise<void>;
|
|
106
|
-
export {};
|
package/dist/utils/filter.js
CHANGED
|
@@ -2,165 +2,58 @@ 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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* @returns Filter function
|
|
9
|
-
*/
|
|
10
|
-
function fromSelf() {
|
|
11
|
-
return (message, client) => {
|
|
12
|
-
return message.senderInboxId === client.inboxId;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
function hasDefinedContent() {
|
|
16
|
-
return (message) => {
|
|
17
|
-
return !!message.content;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
function isDM() {
|
|
21
|
-
return async (_message, _client, conversation) => {
|
|
22
|
-
return (await conversation.metadata()).conversationType === "dm";
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
function isGroup() {
|
|
26
|
-
return async (_message, _client, conversation) => {
|
|
27
|
-
return (await conversation.metadata()).conversationType === "group";
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function isReaction() {
|
|
31
|
-
return (message) => {
|
|
32
|
-
return !!message.contentType?.sameAs(ContentTypeReaction);
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
function isReply() {
|
|
36
|
-
return (message) => {
|
|
37
|
-
return !!message.contentType?.sameAs(ContentTypeReply);
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
function isRemoteAttachment() {
|
|
41
|
-
return (message) => {
|
|
42
|
-
return !!message.contentType?.sameAs(ContentTypeRemoteAttachment);
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Creates a filter that includes only text messages.
|
|
47
|
-
*
|
|
48
|
-
* @returns Filter function
|
|
49
|
-
*/
|
|
50
|
-
function isText() {
|
|
51
|
-
return (message) => {
|
|
52
|
-
return !!message.contentType?.sameAs(ContentTypeText);
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
function isTextReply() {
|
|
56
|
-
return (message) => {
|
|
57
|
-
return isReply()(message) && typeof message.content.content === "string";
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Creates a filter for messages from specific senders
|
|
62
|
-
*
|
|
63
|
-
* @param senderInboxId - Single sender ID or array of sender IDs to match
|
|
64
|
-
* @returns Filter function
|
|
65
|
-
*/
|
|
66
|
-
function fromSender(senderInboxId) {
|
|
67
|
-
const senderIds = Array.isArray(senderInboxId)
|
|
68
|
-
? senderInboxId
|
|
69
|
-
: [senderInboxId];
|
|
70
|
-
return (message) => {
|
|
71
|
-
return senderIds.includes(message.senderInboxId);
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Creates a filter that matches text messages starting with a specific string.
|
|
76
|
-
*
|
|
77
|
-
* @param prefix - The string prefix to match against
|
|
78
|
-
* @returns Filter function
|
|
79
|
-
*/
|
|
80
|
-
function startsWith(prefix) {
|
|
81
|
-
return (message) => {
|
|
82
|
-
const getTextContent = (message) => {
|
|
83
|
-
switch (true) {
|
|
84
|
-
case filter.isReaction(message):
|
|
85
|
-
case filter.isTextReply(message):
|
|
86
|
-
return message.content.content;
|
|
87
|
-
case filter.isText(message):
|
|
88
|
-
return message.content;
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
const text = getTextContent(message);
|
|
92
|
-
return !!(text && text.startsWith(prefix));
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Creates a filter that requires all provided filters to pass
|
|
97
|
-
*
|
|
98
|
-
* @param filters - Array of filters that must all return true
|
|
99
|
-
* @returns Filter function
|
|
100
|
-
*/
|
|
101
|
-
function and(...filters) {
|
|
102
|
-
return async (message, client, conversation) => {
|
|
103
|
-
for (const filter of filters) {
|
|
104
|
-
const result = await filter(message, client, conversation);
|
|
105
|
-
if (!result)
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
return true;
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Creates a filter that requires at least one provided filter to pass.
|
|
113
|
-
*
|
|
114
|
-
* @param filters - Array of filters where at least one must return true
|
|
115
|
-
* @returns Filter function
|
|
116
|
-
*/
|
|
117
|
-
function or(...filters) {
|
|
118
|
-
return async (message, client, conversation) => {
|
|
119
|
-
for (const filter of filters) {
|
|
120
|
-
const result = await filter(message, client, conversation);
|
|
121
|
-
if (result)
|
|
122
|
-
return true;
|
|
123
|
-
}
|
|
124
|
-
return false;
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Creates a filter that inverts the result of another filter.
|
|
129
|
-
*
|
|
130
|
-
* @param filter - The filter to negate
|
|
131
|
-
* @returns Filter function
|
|
132
|
-
*/
|
|
133
|
-
function not(filter) {
|
|
134
|
-
return async (message, client, conversation) => {
|
|
135
|
-
return !(await filter(message, client, conversation));
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Pre-configured filter instances and factory functions for common filtering scenarios
|
|
140
|
-
*/
|
|
141
|
-
export const filter = {
|
|
142
|
-
// basic filters
|
|
143
|
-
fromSelf: fromSelf(),
|
|
144
|
-
hasDefinedContent: hasDefinedContent(),
|
|
145
|
-
isDM: isDM(),
|
|
146
|
-
isGroup: isGroup(),
|
|
147
|
-
isReaction: isReaction(),
|
|
148
|
-
isRemoteAttachment: isRemoteAttachment(),
|
|
149
|
-
isReply: isReply(),
|
|
150
|
-
isText: isText(),
|
|
151
|
-
isTextReply: isTextReply(),
|
|
152
|
-
// factory functions
|
|
153
|
-
fromSender,
|
|
154
|
-
startsWith,
|
|
155
|
-
// combinators
|
|
156
|
-
and,
|
|
157
|
-
or,
|
|
158
|
-
not,
|
|
5
|
+
import { Dm, Group, } from "@xmtp/node-sdk";
|
|
6
|
+
const fromSelf = (message, client) => {
|
|
7
|
+
return message.senderInboxId === client.inboxId;
|
|
159
8
|
};
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
9
|
+
const hasContent = (message) => {
|
|
10
|
+
return message.content !== undefined && message.content !== null;
|
|
11
|
+
};
|
|
12
|
+
const isDM = (conversation) => {
|
|
13
|
+
return conversation instanceof Dm;
|
|
14
|
+
};
|
|
15
|
+
const isGroup = (conversation) => {
|
|
16
|
+
return conversation instanceof Group;
|
|
17
|
+
};
|
|
18
|
+
const isGroupAdmin = (conversation, message) => {
|
|
19
|
+
if (isGroup(conversation)) {
|
|
20
|
+
return conversation.isAdmin(message.senderInboxId);
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
};
|
|
24
|
+
const isGroupSuperAdmin = (conversation, message) => {
|
|
25
|
+
if (isGroup(conversation)) {
|
|
26
|
+
return conversation.isSuperAdmin(message.senderInboxId);
|
|
164
27
|
}
|
|
28
|
+
return false;
|
|
29
|
+
};
|
|
30
|
+
const isReaction = (message) => {
|
|
31
|
+
return !!message.contentType?.sameAs(ContentTypeReaction);
|
|
165
32
|
};
|
|
33
|
+
const isReply = (message) => {
|
|
34
|
+
return !!message.contentType?.sameAs(ContentTypeReply);
|
|
35
|
+
};
|
|
36
|
+
const isRemoteAttachment = (message) => {
|
|
37
|
+
return !!message.contentType?.sameAs(ContentTypeRemoteAttachment);
|
|
38
|
+
};
|
|
39
|
+
const isText = (message) => {
|
|
40
|
+
return !!message.contentType?.sameAs(ContentTypeText);
|
|
41
|
+
};
|
|
42
|
+
const isTextReply = (message) => {
|
|
43
|
+
return isReply(message) && typeof message.content.content === "string";
|
|
44
|
+
};
|
|
45
|
+
export const filter = {
|
|
46
|
+
fromSelf,
|
|
47
|
+
hasContent,
|
|
48
|
+
isDM,
|
|
49
|
+
isGroup,
|
|
50
|
+
isGroupAdmin,
|
|
51
|
+
isGroupSuperAdmin,
|
|
52
|
+
isReaction,
|
|
53
|
+
isRemoteAttachment,
|
|
54
|
+
isReply,
|
|
55
|
+
isText,
|
|
56
|
+
isTextReply,
|
|
57
|
+
};
|
|
58
|
+
export const f = filter;
|
|
166
59
|
//# sourceMappingURL=filter.js.map
|
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,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;
|
|
1
|
+
{"version":3,"file":"filter.js","sourceR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D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF,MAAM,UAAU,GAAG,CACjB,OAAuB,EAC4B,EAAE;IACrD,OAAO,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAC5D,CAAC,CAAC;AAEF,MAAM,OAAO,GAAG,CACd,OAAuB,EACyB,EAAE;IAClD,OAAO,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,gBAAgB,CAAC,CAAC;AACzD,CAAC,CAAC;AAEF,MAAM,kBAAkB,GAAG,CACzB,OAAuB,EACoC,EAAE;IAC7D,OAAO,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,2BAA2B,CAAC,CAAC;AACpE,CAAC,CAAC;AAEF,MAAM,MAAM,GAAG,CACb,OAAuB,EAC0B,EAAE;IACnD,OAAO,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC;AACxD,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAC,OAAuB,EAAE,EAAE;IAC9C,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,KAAK,QAAQ,CAAC;AACzE,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,QAAQ;IACR,UAAU;IACV,IAAI;IACJ,OAAO;IACP,YAAY;IACZ,iBAAiB;IACjB,UAAU;IACV,kBAAkB;IAClB,OAAO;IACP,MAAM;IACN,WAAW;CACZ,CAAC;AAEF,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC"}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
package/dist/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC"}
|
package/package.json
CHANGED
|
@@ -8,19 +8,19 @@
|
|
|
8
8
|
"@xmtp/content-type-remote-attachment": "^2.0.2",
|
|
9
9
|
"@xmtp/content-type-reply": "^2.0.2",
|
|
10
10
|
"@xmtp/content-type-text": "^2.0.2",
|
|
11
|
-
"@xmtp/node-sdk": "^4.1.
|
|
11
|
+
"@xmtp/node-sdk": "^4.1.1",
|
|
12
12
|
"uint8arrays": "^5.1.0",
|
|
13
|
-
"viem": "^2.
|
|
13
|
+
"viem": "^2.37.6"
|
|
14
14
|
},
|
|
15
15
|
"description": "XMTP Agent SDK for interacting with XMTP networks",
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"@arethetypeswrong/cli": "^0.18.2",
|
|
18
|
-
"@types/node": "^22.
|
|
18
|
+
"@types/node": "^22.18.4",
|
|
19
19
|
"@vitest/coverage-v8": "^3.2.4",
|
|
20
20
|
"tsc-alias": "^1.8.16",
|
|
21
|
-
"tsx": "^4.20.
|
|
22
|
-
"typescript": "^5.
|
|
23
|
-
"vite": "^7.
|
|
21
|
+
"tsx": "^4.20.5",
|
|
22
|
+
"typescript": "^5.9.2",
|
|
23
|
+
"vite": "^7.1.5",
|
|
24
24
|
"vite-tsconfig-paths": "^5.1.4",
|
|
25
25
|
"vitest": "^3.2.4"
|
|
26
26
|
},
|
|
@@ -70,5 +70,5 @@
|
|
|
70
70
|
"typecheck": "tsc --noEmit"
|
|
71
71
|
},
|
|
72
72
|
"type": "module",
|
|
73
|
-
"version": "0.0.
|
|
73
|
+
"version": "0.0.13"
|
|
74
74
|
}
|