@hybrd/types 0.7.6 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -11
- package/dist/index.cjs +124 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +418 -0
- package/dist/index.d.ts +418 -0
- package/dist/index.js +97 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -33
- package/src/agent.ts +192 -0
- package/src/behavior.test.ts +31 -0
- package/src/behavior.ts +251 -0
- package/src/index.ts +52 -0
- package/src/plugin.ts +58 -0
- package/src/resolver.ts +6 -0
- package/src/runtime.ts +31 -0
- package/src/tool.ts +57 -0
- package/src/xmtp.ts +36 -0
- package/LICENSE.md +0 -21
- package/dist/types.cjs +0 -18
- package/dist/types.cjs.map +0 -1
- package/dist/types.d.ts +0 -53
- package/dist/types.js +0 -1
- package/dist/types.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,21 +1,58 @@
|
|
|
1
1
|
# @hybrd/types
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This package is part of the Hybrid monorepo.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Hybrid makes it easy for developers to create intelligent agents that can understand natural language, process messages, and respond through XMTP's decentralized messaging protocol.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
See [hybrid.dev](https://hybrid.dev) for more information.
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## 📦 Quickstart
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
Getting started with Hybrid is simple:
|
|
12
|
+
|
|
13
|
+
### 1. Initialize your project
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm create hybrid my-agent
|
|
17
|
+
cd my-agent
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
This creates all the necessary files and configuration for your agent.
|
|
21
|
+
|
|
22
|
+
### 2. Get your OpenRouter API key
|
|
23
|
+
|
|
24
|
+
Visit [OpenRouter](https://openrouter.ai/keys), create an account and generate an API key
|
|
25
|
+
|
|
26
|
+
Add it to your `.env` file:
|
|
27
|
+
|
|
28
|
+
```env
|
|
29
|
+
OPENROUTER_API_KEY=your_openrouter_api_key_here
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 3. Generate XMTP keys
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
hybrid keys
|
|
13
36
|
```
|
|
14
37
|
|
|
15
|
-
|
|
38
|
+
or automatically add it to your `.env` file:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
hybrid keys --write
|
|
42
|
+
```
|
|
16
43
|
|
|
17
|
-
|
|
44
|
+
### 4. Register your wallet with XMTP
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
hybrid register
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
This generates secure wallet and encryption keys for your XMTP agent.
|
|
51
|
+
|
|
52
|
+
### 5. Start developing
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
hybrid dev
|
|
56
|
+
```
|
|
18
57
|
|
|
19
|
-
|
|
20
|
-
- Follow [@hybrid\_\_dev](https://twitter.com/hybrid__dev) on Twitter for project updates
|
|
21
|
-
- Contribute or fork on [GitHub](https://github.com/hybridhq/hybrid)
|
|
58
|
+
Your agent will start listening for XMTP messages and you're ready to build!
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
BehaviorRegistryImpl: () => BehaviorRegistryImpl
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/behavior.ts
|
|
28
|
+
var BehaviorRegistryImpl = class {
|
|
29
|
+
behaviors = [];
|
|
30
|
+
/**
|
|
31
|
+
* Register a behavior with the registry
|
|
32
|
+
*/
|
|
33
|
+
register(behavior) {
|
|
34
|
+
this.behaviors.push(behavior);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Register multiple behaviors at once
|
|
38
|
+
*/
|
|
39
|
+
registerAll(behaviors) {
|
|
40
|
+
this.behaviors.push(...behaviors);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get all registered behaviors
|
|
44
|
+
*/
|
|
45
|
+
getAll() {
|
|
46
|
+
return [...this.behaviors];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get behaviors that should run before the agent responds
|
|
50
|
+
*/
|
|
51
|
+
getBeforeBehaviors() {
|
|
52
|
+
return this.behaviors.filter((behavior) => behavior.before);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get behaviors that should run after the agent responds
|
|
56
|
+
*/
|
|
57
|
+
getAfterBehaviors() {
|
|
58
|
+
return this.behaviors.filter((behavior) => behavior.after);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Execute all before-response behaviors as a middleware chain
|
|
62
|
+
*/
|
|
63
|
+
async executeBefore(context) {
|
|
64
|
+
const behaviors = this.getBeforeBehaviors();
|
|
65
|
+
let currentIndex = 0;
|
|
66
|
+
const next = async () => {
|
|
67
|
+
if (currentIndex >= behaviors.length) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const behavior = behaviors[currentIndex];
|
|
71
|
+
if (!behavior) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
currentIndex++;
|
|
75
|
+
try {
|
|
76
|
+
await behavior.before?.(context);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
console.error(
|
|
79
|
+
`Error executing before behavior "${behavior.id}":`,
|
|
80
|
+
error
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
context.next = next;
|
|
85
|
+
await next();
|
|
86
|
+
context.stopped = currentIndex < behaviors.length;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Execute all after-response behaviors as a middleware chain
|
|
90
|
+
*/
|
|
91
|
+
async executeAfter(context) {
|
|
92
|
+
const behaviors = this.getAfterBehaviors();
|
|
93
|
+
let currentIndex = 0;
|
|
94
|
+
const next = async () => {
|
|
95
|
+
if (currentIndex >= behaviors.length) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const behavior = behaviors[currentIndex];
|
|
99
|
+
if (!behavior) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
currentIndex++;
|
|
103
|
+
try {
|
|
104
|
+
await behavior.after?.(context);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
console.error(`Error executing after behavior "${behavior.id}":`, error);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
context.next = next;
|
|
110
|
+
await next();
|
|
111
|
+
context.stopped = currentIndex < behaviors.length;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Clear all registered behaviors
|
|
115
|
+
*/
|
|
116
|
+
clear() {
|
|
117
|
+
this.behaviors = [];
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
121
|
+
0 && (module.exports = {
|
|
122
|
+
BehaviorRegistryImpl
|
|
123
|
+
});
|
|
124
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/behavior.ts"],"sourcesContent":["// Agent types\nexport type {\n\tAgentConfig,\n\tDefaultRuntimeExtension,\n\tGenerateOptions,\n\tListenOptions,\n\tStreamOptions,\n\tToolGenerator\n} from \"./agent\"\n\nexport type { Agent, AgentMessage } from \"./agent\"\n\n// Tool types\nexport type {\n\tAnyTool,\n\tTool,\n\tToolConfig\n} from \"./tool\"\n\n// Plugin types\nexport type {\n\tPlugin,\n\tPluginContext,\n\tPluginRegistry\n} from \"./plugin\"\n\n// Runtime types\nexport type { AgentRuntime } from \"./runtime\"\n\n// XMTP types\nexport type {\n\tHonoVariables,\n\tXmtpClient,\n\tXmtpConversation,\n\tXmtpMessage,\n\tXmtpSender,\n\tXmtpSubjects\n} from \"./xmtp\"\n\n// Resolver types\nexport type { Resolver } from \"./resolver\"\n\n// Behavior types\nexport { BehaviorRegistryImpl } from \"./behavior\"\nexport type {\n\tBehavior,\n\tBehaviorConfig,\n\tBehaviorContext,\n\tBehaviorInstance,\n\tBehaviorObject,\n\tBehaviorRegistry\n} from \"./behavior\"\n","import type { AgentRuntime } from \"./runtime\"\nimport type { XmtpClient, XmtpConversation, XmtpMessage } from \"./xmtp\"\n\n/**\n * Configuration options for a behavior\n */\nexport interface BehaviorConfig {\n\t/** Whether the behavior is enabled */\n\tenabled?: boolean\n\t/** Optional configuration data for the behavior */\n\tconfig?: Record<string, unknown>\n}\n\n/**\n * Context provided to behaviors when they execute\n */\nexport interface BehaviorContext<TRuntimeExtension = Record<string, never>> {\n\t/** The base runtime context */\n\truntime: AgentRuntime & TRuntimeExtension\n\t/** The XMTP client instance */\n\tclient: XmtpClient\n\t/** The conversation the message came from */\n\tconversation: XmtpConversation\n\t/** The message that triggered the behavior */\n\tmessage: XmtpMessage\n\t/** The agent's response (available in post-response behaviors) */\n\tresponse?: string\n\t/** Send options that behaviors can modify */\n\tsendOptions?: {\n\t\t/** Whether to thread the reply to the original message */\n\t\tthreaded?: boolean\n\t\t/** Content type override */\n\t\tcontentType?: string\n\t\t/** Whether this message should be filtered out and not processed */\n\t\tfiltered?: boolean\n\t\t/** Additional metadata */\n\t\tmetadata?: Record<string, unknown>\n\t}\n\t/**\n\t * Continue to the next behavior in the middleware chain\n\t * If not called, the behavior chain stops processing\n\t */\n\tnext?: () => Promise<void>\n\t/**\n\t * Whether the middleware chain was stopped early\n\t * This gets set to true when a behavior doesn't call next()\n\t */\n\tstopped?: boolean\n}\n\n/**\n * A behavior that can be executed before or after agent responses\n */\nexport interface BehaviorObject<TRuntimeExtension = Record<string, never>> {\n\t/** Unique identifier for the behavior */\n\tid: string\n\t/** Configuration for the behavior */\n\tconfig: BehaviorConfig\n\t/**\n\t * Execute the behavior before the agent responds\n\t * @param context - The context in which to execute the behavior\n\t */\n\tbefore?(context: BehaviorContext<TRuntimeExtension>): Promise<void> | void\n\t/**\n\t * Execute the behavior after the agent responds\n\t * @param context - The context in which to execute the behavior\n\t */\n\tafter?(context: BehaviorContext<TRuntimeExtension>): Promise<void> | void\n}\n\n/**\n * Factory function to create a behavior instance\n */\nexport type Behavior<TConfig = Record<string, unknown>> = (\n\tconfig: TConfig & BehaviorConfig\n) => BehaviorObject\n\n/**\n * Pre-configured behavior instance that can be used directly\n */\nexport type BehaviorInstance = Behavior\n\n/**\n * Behavior registry for managing and executing behaviors\n */\nexport interface BehaviorRegistry {\n\t/**\n\t * Register a behavior with the registry\n\t */\n\tregister(behavior: BehaviorObject): void\n\n\t/**\n\t * Register multiple behaviors at once\n\t */\n\tregisterAll(behaviors: BehaviorObject[]): void\n\n\t/**\n\t * Get all registered behaviors\n\t */\n\tgetAll(): BehaviorObject[]\n\n\t/**\n\t * Get behaviors that should run before the agent responds\n\t */\n\tgetBeforeBehaviors(): BehaviorObject[]\n\n\t/**\n\t * Get behaviors that should run after the agent responds\n\t */\n\tgetAfterBehaviors(): BehaviorObject[]\n\n\t/**\n\t * Execute all before-response behaviors as a middleware chain\n\t */\n\texecuteBefore(context: BehaviorContext): Promise<void>\n\n\t/**\n\t * Execute all after-response behaviors as a middleware chain\n\t */\n\texecuteAfter(context: BehaviorContext): Promise<void>\n\n\t/**\n\t * Clear all registered behaviors\n\t */\n\tclear(): void\n}\n\n/**\n * Concrete implementation of the BehaviorRegistry interface\n */\nexport class BehaviorRegistryImpl implements BehaviorRegistry {\n\tprivate behaviors: BehaviorObject[] = []\n\n\t/**\n\t * Register a behavior with the registry\n\t */\n\tregister(behavior: BehaviorObject): void {\n\t\tthis.behaviors.push(behavior)\n\t}\n\n\t/**\n\t * Register multiple behaviors at once\n\t */\n\tregisterAll(behaviors: BehaviorObject[]): void {\n\t\t// Register behavior objects directly\n\t\tthis.behaviors.push(...behaviors)\n\t}\n\n\t/**\n\t * Get all registered behaviors\n\t */\n\tgetAll(): BehaviorObject[] {\n\t\treturn [...this.behaviors]\n\t}\n\n\t/**\n\t * Get behaviors that should run before the agent responds\n\t */\n\tgetBeforeBehaviors(): BehaviorObject[] {\n\t\treturn this.behaviors.filter((behavior) => behavior.before)\n\t}\n\n\t/**\n\t * Get behaviors that should run after the agent responds\n\t */\n\tgetAfterBehaviors(): BehaviorObject[] {\n\t\treturn this.behaviors.filter((behavior) => behavior.after)\n\t}\n\n\t/**\n\t * Execute all before-response behaviors as a middleware chain\n\t */\n\tasync executeBefore(context: BehaviorContext): Promise<void> {\n\t\tconst behaviors = this.getBeforeBehaviors()\n\n\t\t// Create a middleware chain\n\t\tlet currentIndex = 0\n\t\tconst next = async (): Promise<void> => {\n\t\t\tif (currentIndex >= behaviors.length) {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tconst behavior = behaviors[currentIndex]\n\t\t\tif (!behavior) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcurrentIndex++\n\n\t\t\ttry {\n\t\t\t\tawait behavior.before?.(context)\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`Error executing before behavior \"${behavior.id}\":`,\n\t\t\t\t\terror\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\t// Set the next function in the context\n\t\tcontext.next = next\n\n\t\t// Start the chain\n\t\tawait next()\n\n\t\t// Check if the chain was stopped early\n\t\tcontext.stopped = currentIndex < behaviors.length\n\t}\n\n\t/**\n\t * Execute all after-response behaviors as a middleware chain\n\t */\n\tasync executeAfter(context: BehaviorContext): Promise<void> {\n\t\tconst behaviors = this.getAfterBehaviors()\n\n\t\t// Create a middleware chain\n\t\tlet currentIndex = 0\n\t\tconst next = async (): Promise<void> => {\n\t\t\tif (currentIndex >= behaviors.length) {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tconst behavior = behaviors[currentIndex]\n\t\t\tif (!behavior) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcurrentIndex++\n\n\t\t\ttry {\n\t\t\t\tawait behavior.after?.(context)\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(`Error executing after behavior \"${behavior.id}\":`, error)\n\t\t\t}\n\t\t}\n\n\t\t// Set the next function in the context\n\t\tcontext.next = next\n\n\t\t// Start the chain\n\t\tawait next()\n\n\t\t// Check if the chain was stopped early\n\t\tcontext.stopped = currentIndex < behaviors.length\n\t}\n\n\t/**\n\t * Clear all registered behaviors\n\t */\n\tclear(): void {\n\t\tthis.behaviors = []\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkIO,IAAM,uBAAN,MAAuD;AAAA,EACrD,YAA8B,CAAC;AAAA;AAAA;AAAA;AAAA,EAKvC,SAAS,UAAgC;AACxC,SAAK,UAAU,KAAK,QAAQ;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA,EAKA,YAAY,WAAmC;AAE9C,SAAK,UAAU,KAAK,GAAG,SAAS;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA,EAKA,SAA2B;AAC1B,WAAO,CAAC,GAAG,KAAK,SAAS;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA,EAKA,qBAAuC;AACtC,WAAO,KAAK,UAAU,OAAO,CAAC,aAAa,SAAS,MAAM;AAAA,EAC3D;AAAA;AAAA;AAAA;AAAA,EAKA,oBAAsC;AACrC,WAAO,KAAK,UAAU,OAAO,CAAC,aAAa,SAAS,KAAK;AAAA,EAC1D;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,cAAc,SAAyC;AAC5D,UAAM,YAAY,KAAK,mBAAmB;AAG1C,QAAI,eAAe;AACnB,UAAM,OAAO,YAA2B;AACvC,UAAI,gBAAgB,UAAU,QAAQ;AACrC;AAAA,MACD;AAEA,YAAM,WAAW,UAAU,YAAY;AACvC,UAAI,CAAC,UAAU;AACd;AAAA,MACD;AACA;AAEA,UAAI;AACH,cAAM,SAAS,SAAS,OAAO;AAAA,MAChC,SAAS,OAAO;AACf,gBAAQ;AAAA,UACP,oCAAoC,SAAS,EAAE;AAAA,UAC/C;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAGA,YAAQ,OAAO;AAGf,UAAM,KAAK;AAGX,YAAQ,UAAU,eAAe,UAAU;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,aAAa,SAAyC;AAC3D,UAAM,YAAY,KAAK,kBAAkB;AAGzC,QAAI,eAAe;AACnB,UAAM,OAAO,YAA2B;AACvC,UAAI,gBAAgB,UAAU,QAAQ;AACrC;AAAA,MACD;AAEA,YAAM,WAAW,UAAU,YAAY;AACvC,UAAI,CAAC,UAAU;AACd;AAAA,MACD;AACA;AAEA,UAAI;AACH,cAAM,SAAS,QAAQ,OAAO;AAAA,MAC/B,SAAS,OAAO;AACf,gBAAQ,MAAM,mCAAmC,SAAS,EAAE,MAAM,KAAK;AAAA,MACxE;AAAA,IACD;AAGA,YAAQ,OAAO;AAGf,UAAM,KAAK;AAGX,YAAQ,UAAU,eAAe,UAAU;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA,EAKA,QAAc;AACb,SAAK,YAAY,CAAC;AAAA,EACnB;AACD;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import { UIMessage, LanguageModel, generateText, TelemetrySettings, streamText, UIMessageStreamOnFinishCallback } from 'ai';
|
|
2
|
+
import { Conversation, DecodedMessage, Client } from '@xmtp/node-sdk';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Resolver interface for address resolution
|
|
8
|
+
*/
|
|
9
|
+
interface Resolver {
|
|
10
|
+
resolve: (address: string) => Promise<string | null>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @fileoverview XMTP Types
|
|
15
|
+
*
|
|
16
|
+
* Type definitions for both the XMTP core client and service client library.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
type HonoVariables = {
|
|
20
|
+
xmtpClient: XmtpClient;
|
|
21
|
+
resolver?: Resolver;
|
|
22
|
+
};
|
|
23
|
+
type XmtpClient = Client<unknown>;
|
|
24
|
+
type XmtpConversation = Conversation<unknown>;
|
|
25
|
+
type XmtpMessage = DecodedMessage<unknown>;
|
|
26
|
+
type XmtpSender = {
|
|
27
|
+
address: string;
|
|
28
|
+
inboxId: string;
|
|
29
|
+
name: string;
|
|
30
|
+
basename?: string;
|
|
31
|
+
};
|
|
32
|
+
type XmtpSubjects = Record<string, `0x${string}`>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Base runtime that is always included in agents and tools.
|
|
36
|
+
* Additional fields can be added via generic type parameters as intersections.
|
|
37
|
+
*
|
|
38
|
+
* Example usage:
|
|
39
|
+
* - Basic: AgentRuntime (just chatId and messages)
|
|
40
|
+
* - Extended: AgentRuntime & { userId: string } (adds userId field)
|
|
41
|
+
*/
|
|
42
|
+
interface AgentRuntime {
|
|
43
|
+
conversation: XmtpConversation;
|
|
44
|
+
message: XmtpMessage;
|
|
45
|
+
xmtpClient: XmtpClient;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Configuration options for a behavior
|
|
50
|
+
*/
|
|
51
|
+
interface BehaviorConfig {
|
|
52
|
+
/** Whether the behavior is enabled */
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
/** Optional configuration data for the behavior */
|
|
55
|
+
config?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Context provided to behaviors when they execute
|
|
59
|
+
*/
|
|
60
|
+
interface BehaviorContext<TRuntimeExtension = Record<string, never>> {
|
|
61
|
+
/** The base runtime context */
|
|
62
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
63
|
+
/** The XMTP client instance */
|
|
64
|
+
client: XmtpClient;
|
|
65
|
+
/** The conversation the message came from */
|
|
66
|
+
conversation: XmtpConversation;
|
|
67
|
+
/** The message that triggered the behavior */
|
|
68
|
+
message: XmtpMessage;
|
|
69
|
+
/** The agent's response (available in post-response behaviors) */
|
|
70
|
+
response?: string;
|
|
71
|
+
/** Send options that behaviors can modify */
|
|
72
|
+
sendOptions?: {
|
|
73
|
+
/** Whether to thread the reply to the original message */
|
|
74
|
+
threaded?: boolean;
|
|
75
|
+
/** Content type override */
|
|
76
|
+
contentType?: string;
|
|
77
|
+
/** Whether this message should be filtered out and not processed */
|
|
78
|
+
filtered?: boolean;
|
|
79
|
+
/** Additional metadata */
|
|
80
|
+
metadata?: Record<string, unknown>;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Continue to the next behavior in the middleware chain
|
|
84
|
+
* If not called, the behavior chain stops processing
|
|
85
|
+
*/
|
|
86
|
+
next?: () => Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Whether the middleware chain was stopped early
|
|
89
|
+
* This gets set to true when a behavior doesn't call next()
|
|
90
|
+
*/
|
|
91
|
+
stopped?: boolean;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A behavior that can be executed before or after agent responses
|
|
95
|
+
*/
|
|
96
|
+
interface BehaviorObject<TRuntimeExtension = Record<string, never>> {
|
|
97
|
+
/** Unique identifier for the behavior */
|
|
98
|
+
id: string;
|
|
99
|
+
/** Configuration for the behavior */
|
|
100
|
+
config: BehaviorConfig;
|
|
101
|
+
/**
|
|
102
|
+
* Execute the behavior before the agent responds
|
|
103
|
+
* @param context - The context in which to execute the behavior
|
|
104
|
+
*/
|
|
105
|
+
before?(context: BehaviorContext<TRuntimeExtension>): Promise<void> | void;
|
|
106
|
+
/**
|
|
107
|
+
* Execute the behavior after the agent responds
|
|
108
|
+
* @param context - The context in which to execute the behavior
|
|
109
|
+
*/
|
|
110
|
+
after?(context: BehaviorContext<TRuntimeExtension>): Promise<void> | void;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Factory function to create a behavior instance
|
|
114
|
+
*/
|
|
115
|
+
type Behavior<TConfig = Record<string, unknown>> = (config: TConfig & BehaviorConfig) => BehaviorObject;
|
|
116
|
+
/**
|
|
117
|
+
* Pre-configured behavior instance that can be used directly
|
|
118
|
+
*/
|
|
119
|
+
type BehaviorInstance = Behavior;
|
|
120
|
+
/**
|
|
121
|
+
* Behavior registry for managing and executing behaviors
|
|
122
|
+
*/
|
|
123
|
+
interface BehaviorRegistry {
|
|
124
|
+
/**
|
|
125
|
+
* Register a behavior with the registry
|
|
126
|
+
*/
|
|
127
|
+
register(behavior: BehaviorObject): void;
|
|
128
|
+
/**
|
|
129
|
+
* Register multiple behaviors at once
|
|
130
|
+
*/
|
|
131
|
+
registerAll(behaviors: BehaviorObject[]): void;
|
|
132
|
+
/**
|
|
133
|
+
* Get all registered behaviors
|
|
134
|
+
*/
|
|
135
|
+
getAll(): BehaviorObject[];
|
|
136
|
+
/**
|
|
137
|
+
* Get behaviors that should run before the agent responds
|
|
138
|
+
*/
|
|
139
|
+
getBeforeBehaviors(): BehaviorObject[];
|
|
140
|
+
/**
|
|
141
|
+
* Get behaviors that should run after the agent responds
|
|
142
|
+
*/
|
|
143
|
+
getAfterBehaviors(): BehaviorObject[];
|
|
144
|
+
/**
|
|
145
|
+
* Execute all before-response behaviors as a middleware chain
|
|
146
|
+
*/
|
|
147
|
+
executeBefore(context: BehaviorContext): Promise<void>;
|
|
148
|
+
/**
|
|
149
|
+
* Execute all after-response behaviors as a middleware chain
|
|
150
|
+
*/
|
|
151
|
+
executeAfter(context: BehaviorContext): Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Clear all registered behaviors
|
|
154
|
+
*/
|
|
155
|
+
clear(): void;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Concrete implementation of the BehaviorRegistry interface
|
|
159
|
+
*/
|
|
160
|
+
declare class BehaviorRegistryImpl implements BehaviorRegistry {
|
|
161
|
+
private behaviors;
|
|
162
|
+
/**
|
|
163
|
+
* Register a behavior with the registry
|
|
164
|
+
*/
|
|
165
|
+
register(behavior: BehaviorObject): void;
|
|
166
|
+
/**
|
|
167
|
+
* Register multiple behaviors at once
|
|
168
|
+
*/
|
|
169
|
+
registerAll(behaviors: BehaviorObject[]): void;
|
|
170
|
+
/**
|
|
171
|
+
* Get all registered behaviors
|
|
172
|
+
*/
|
|
173
|
+
getAll(): BehaviorObject[];
|
|
174
|
+
/**
|
|
175
|
+
* Get behaviors that should run before the agent responds
|
|
176
|
+
*/
|
|
177
|
+
getBeforeBehaviors(): BehaviorObject[];
|
|
178
|
+
/**
|
|
179
|
+
* Get behaviors that should run after the agent responds
|
|
180
|
+
*/
|
|
181
|
+
getAfterBehaviors(): BehaviorObject[];
|
|
182
|
+
/**
|
|
183
|
+
* Execute all before-response behaviors as a middleware chain
|
|
184
|
+
*/
|
|
185
|
+
executeBefore(context: BehaviorContext): Promise<void>;
|
|
186
|
+
/**
|
|
187
|
+
* Execute all after-response behaviors as a middleware chain
|
|
188
|
+
*/
|
|
189
|
+
executeAfter(context: BehaviorContext): Promise<void>;
|
|
190
|
+
/**
|
|
191
|
+
* Clear all registered behaviors
|
|
192
|
+
*/
|
|
193
|
+
clear(): void;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Plugin interface for extending the agent's Hono app
|
|
198
|
+
*
|
|
199
|
+
* @description
|
|
200
|
+
* Plugins allow you to extend the agent's HTTP server with additional
|
|
201
|
+
* routes, middleware, and functionality. Each plugin receives the Hono
|
|
202
|
+
* app instance and can modify it as needed.
|
|
203
|
+
*
|
|
204
|
+
* @template T - Optional context type that can be passed to the plugin
|
|
205
|
+
*/
|
|
206
|
+
interface Plugin<T = Record<string, never>> {
|
|
207
|
+
/**
|
|
208
|
+
* Unique identifier for the plugin
|
|
209
|
+
*/
|
|
210
|
+
name: string;
|
|
211
|
+
/**
|
|
212
|
+
* Optional description of what the plugin does
|
|
213
|
+
*/
|
|
214
|
+
description?: string;
|
|
215
|
+
/**
|
|
216
|
+
* Function that applies the plugin to the Hono app
|
|
217
|
+
*
|
|
218
|
+
* @param app - The Hono app instance to extend
|
|
219
|
+
* @param context - Optional context data passed to the plugin
|
|
220
|
+
*/
|
|
221
|
+
apply: (app: Hono<{
|
|
222
|
+
Variables: HonoVariables;
|
|
223
|
+
}>, context: T) => void | Promise<void>;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Plugin registry that manages all registered plugins
|
|
227
|
+
*
|
|
228
|
+
* @description
|
|
229
|
+
* The plugin registry allows you to register, configure, and apply
|
|
230
|
+
* plugins to Hono apps. It provides a centralized way to manage
|
|
231
|
+
* plugin dependencies and execution order.
|
|
232
|
+
*/
|
|
233
|
+
interface PluginRegistry<TContext = unknown> {
|
|
234
|
+
register: (plugin: Plugin<TContext>) => void;
|
|
235
|
+
applyAll: (app: Hono<{
|
|
236
|
+
Variables: HonoVariables;
|
|
237
|
+
}>, context: TContext) => Promise<void>;
|
|
238
|
+
}
|
|
239
|
+
interface PluginContext {
|
|
240
|
+
agent: Agent;
|
|
241
|
+
behaviors?: BehaviorRegistry;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Configuration interface for creating custom tools that integrate with AI SDK.
|
|
246
|
+
*/
|
|
247
|
+
interface ToolConfig<TInput extends z.ZodTypeAny = z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny, TRuntimeExtension = DefaultRuntimeExtension> {
|
|
248
|
+
/** Unique identifier for the tool */
|
|
249
|
+
id: string;
|
|
250
|
+
/** Human-readable description of what the tool does */
|
|
251
|
+
description: string;
|
|
252
|
+
/** Zod schema for validating tool input */
|
|
253
|
+
inputSchema: TInput;
|
|
254
|
+
/** Optional Zod schema for validating tool output */
|
|
255
|
+
outputSchema?: TOutput;
|
|
256
|
+
/** Function that executes the tool's logic */
|
|
257
|
+
execute: (args: {
|
|
258
|
+
input: z.infer<TInput>;
|
|
259
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
260
|
+
messages: UIMessage[];
|
|
261
|
+
}) => Promise<z.infer<TOutput>>;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Internal tool interface used throughout the agent framework.
|
|
265
|
+
* Similar to ToolConfig but without the ID field, used after tool creation.
|
|
266
|
+
*/
|
|
267
|
+
interface Tool<TInput extends z.ZodTypeAny = z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny, TRuntimeExtension = DefaultRuntimeExtension> {
|
|
268
|
+
/** Human-readable description of what the tool does */
|
|
269
|
+
description: string;
|
|
270
|
+
/** Zod schema for validating tool input */
|
|
271
|
+
inputSchema: TInput;
|
|
272
|
+
/** Optional Zod schema for validating tool output */
|
|
273
|
+
outputSchema?: TOutput;
|
|
274
|
+
/** Function that executes the tool's logic */
|
|
275
|
+
execute: (args: {
|
|
276
|
+
input: z.infer<TInput>;
|
|
277
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
278
|
+
messages: UIMessage[];
|
|
279
|
+
}) => Promise<z.infer<TOutput>>;
|
|
280
|
+
}
|
|
281
|
+
type AnyTool<TRuntimeExtension = DefaultRuntimeExtension> = Tool<any, any, TRuntimeExtension>;
|
|
282
|
+
|
|
283
|
+
type AgentMessage = UIMessage;
|
|
284
|
+
interface Agent<TRuntimeExtension = Record<string, never>, TPluginContext = unknown> {
|
|
285
|
+
/** Agent's unique identifier */
|
|
286
|
+
readonly name: string;
|
|
287
|
+
/** Plugin registry for extending the agent's HTTP server */
|
|
288
|
+
readonly plugins: PluginRegistry<TPluginContext>;
|
|
289
|
+
/**
|
|
290
|
+
* Generates a text completion using the agent's configuration.
|
|
291
|
+
* @param messages - Conversation messages to generate from
|
|
292
|
+
* @param options - Generation options including runtime context
|
|
293
|
+
* @returns Generated text completion result
|
|
294
|
+
*/
|
|
295
|
+
generate(messages: AgentMessage[], options: GenerateOptions<TRuntimeExtension>): Promise<Awaited<ReturnType<typeof generateText>>>;
|
|
296
|
+
/**
|
|
297
|
+
* Streams a text completion using the agent's configuration.
|
|
298
|
+
* @param messages - Conversation messages to generate from
|
|
299
|
+
* @param options - Streaming options including runtime context
|
|
300
|
+
* @returns Streaming response that can be consumed
|
|
301
|
+
*/
|
|
302
|
+
stream(messages: AgentMessage[], options: StreamOptions<TRuntimeExtension>): Promise<Response>;
|
|
303
|
+
/**
|
|
304
|
+
* Gets the agent's configuration for debugging and inspection.
|
|
305
|
+
* @returns Object containing agent configuration details
|
|
306
|
+
*/
|
|
307
|
+
getConfig(): {
|
|
308
|
+
name: string;
|
|
309
|
+
hasModel: boolean;
|
|
310
|
+
hasTools: boolean;
|
|
311
|
+
hasInstructions: boolean;
|
|
312
|
+
};
|
|
313
|
+
/**
|
|
314
|
+
* Gets the agent's instructions without running generation.
|
|
315
|
+
* Useful for external integrations that need instructions separately.
|
|
316
|
+
* @param options - Options containing runtime context and optional messages
|
|
317
|
+
* @returns Resolved instructions string
|
|
318
|
+
*/
|
|
319
|
+
getInstructions(options: {
|
|
320
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
321
|
+
messages?: AgentMessage[];
|
|
322
|
+
}): Promise<string | undefined>;
|
|
323
|
+
/**
|
|
324
|
+
* Gets the agent's tools without running generation.
|
|
325
|
+
* Useful for external integrations that need tools separately.
|
|
326
|
+
* @param options - Options containing runtime context and optional messages
|
|
327
|
+
* @returns Resolved tools object
|
|
328
|
+
*/
|
|
329
|
+
getTools(options: {
|
|
330
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
331
|
+
messages?: AgentMessage[];
|
|
332
|
+
}): Promise<Record<string, AnyTool<TRuntimeExtension>> | undefined>;
|
|
333
|
+
/**
|
|
334
|
+
* Creates the complete runtime context by merging base runtime with custom extension.
|
|
335
|
+
* @param baseRuntime - The base runtime context containing XMTP properties
|
|
336
|
+
* @returns Complete runtime context with custom extensions applied
|
|
337
|
+
*/
|
|
338
|
+
createRuntimeContext(baseRuntime: AgentRuntime): Promise<AgentRuntime & TRuntimeExtension>;
|
|
339
|
+
/**
|
|
340
|
+
* Registers a plugin with the agent
|
|
341
|
+
* @param plugin - The plugin to register
|
|
342
|
+
*/
|
|
343
|
+
use(plugin: Plugin<TPluginContext>): void;
|
|
344
|
+
/**
|
|
345
|
+
* Starts listening for messages and events using the agent instance.
|
|
346
|
+
* @param opts - Configuration options for the listener, excluding the agent property
|
|
347
|
+
*/
|
|
348
|
+
listen(opts: Omit<ListenOptions, "agent">): Promise<void>;
|
|
349
|
+
}
|
|
350
|
+
type DefaultRuntimeExtension = Record<string, never>;
|
|
351
|
+
type ToolGenerator<TRuntimeExtension = DefaultRuntimeExtension> = (props: {
|
|
352
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
353
|
+
messages: AgentMessage[];
|
|
354
|
+
}) => Record<string, AnyTool<TRuntimeExtension>> | Promise<Record<string, AnyTool<TRuntimeExtension>>>;
|
|
355
|
+
/**
|
|
356
|
+
* Configuration interface for creating an Agent instance.
|
|
357
|
+
* Supports both static and dynamic configuration through functions.
|
|
358
|
+
*/
|
|
359
|
+
interface AgentConfig<TRuntimeExtension = DefaultRuntimeExtension> {
|
|
360
|
+
/** Unique identifier for the agent */
|
|
361
|
+
name: string;
|
|
362
|
+
/** Language model to use, can be static or dynamically resolved */
|
|
363
|
+
model: LanguageModel | ((props: {
|
|
364
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
365
|
+
}) => LanguageModel | Promise<LanguageModel>);
|
|
366
|
+
/** Tools available to the agent, can be static or dynamically generated */
|
|
367
|
+
tools?: Record<string, AnyTool<TRuntimeExtension>> | ToolGenerator<TRuntimeExtension>;
|
|
368
|
+
/** Instructions for the agent, can be static or dynamically resolved */
|
|
369
|
+
instructions: string | ((props: {
|
|
370
|
+
messages: AgentMessage[];
|
|
371
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
372
|
+
}) => string | Promise<string>);
|
|
373
|
+
/** Function to create the runtime extension, type will be inferred */
|
|
374
|
+
createRuntime?: (runtime: AgentRuntime) => TRuntimeExtension | Promise<TRuntimeExtension>;
|
|
375
|
+
/** Maximum number of steps the agent can take */
|
|
376
|
+
maxSteps?: number;
|
|
377
|
+
/** Maximum tokens for generation */
|
|
378
|
+
maxTokens?: number;
|
|
379
|
+
/** Temperature for generation (0.0 to 2.0) */
|
|
380
|
+
temperature?: number;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Options for text generation with the agent.
|
|
384
|
+
* Extends AI SDK parameters while adding agent-specific options.
|
|
385
|
+
*/
|
|
386
|
+
interface GenerateOptions<TRuntimeExtension = DefaultRuntimeExtension> extends Omit<Parameters<typeof generateText>[0], "model" | "tools" | "instructions" | "onFinish"> {
|
|
387
|
+
/** Maximum tokens for generation */
|
|
388
|
+
maxTokens?: number;
|
|
389
|
+
/** Runtime context for the agent */
|
|
390
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
391
|
+
/** Optional telemetry configuration */
|
|
392
|
+
telemetry?: NonNullable<TelemetrySettings>;
|
|
393
|
+
}
|
|
394
|
+
interface StreamOptions<TRuntimeExtension = DefaultRuntimeExtension> extends Omit<Parameters<typeof streamText>[0], "model" | "tools" | "instructions" | "onFinish"> {
|
|
395
|
+
/** Maximum tokens for generation */
|
|
396
|
+
maxTokens?: number;
|
|
397
|
+
/** Runtime context for the agent */
|
|
398
|
+
runtime: AgentRuntime & TRuntimeExtension;
|
|
399
|
+
/** Optional telemetry configuration */
|
|
400
|
+
telemetry?: NonNullable<TelemetrySettings>;
|
|
401
|
+
/** Callback when streaming finishes */
|
|
402
|
+
onFinish?: UIMessageStreamOnFinishCallback<AgentMessage>;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Options for starting the agent listener
|
|
406
|
+
* @property agent - The agent instance to use
|
|
407
|
+
* @property port - The port number to listen on (defaults to 8454)
|
|
408
|
+
* @property plugins - Optional array of plugins to apply to the server
|
|
409
|
+
* @property behaviors - Optional array of behaviors to apply to message processing
|
|
410
|
+
*/
|
|
411
|
+
interface ListenOptions {
|
|
412
|
+
agent: Agent<unknown, unknown>;
|
|
413
|
+
port: string;
|
|
414
|
+
plugins?: Plugin<unknown>[];
|
|
415
|
+
behaviors?: BehaviorObject[];
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
export { type Agent, type AgentConfig, type AgentMessage, type AgentRuntime, type AnyTool, type Behavior, type BehaviorConfig, type BehaviorContext, type BehaviorInstance, type BehaviorObject, type BehaviorRegistry, BehaviorRegistryImpl, type DefaultRuntimeExtension, type GenerateOptions, type HonoVariables, type ListenOptions, type Plugin, type PluginContext, type PluginRegistry, type Resolver, type StreamOptions, type Tool, type ToolConfig, type ToolGenerator, type XmtpClient, type XmtpConversation, type XmtpMessage, type XmtpSender, type XmtpSubjects };
|