hybrid 1.1.0 → 1.2.0

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 CHANGED
@@ -13,7 +13,7 @@ A flexible framework for building AI agents with plugin-based HTTP server extens
13
13
  ## Quick Start
14
14
 
15
15
  ```typescript
16
- import { Agent, XMTPPlugin, PonderPlugin } from "@hybrd/hybrid"
16
+ import { Agent, XMTPPlugin, PonderPlugin } from "hybrid"
17
17
 
18
18
  // Create an agent
19
19
  const agent = new Agent({
@@ -48,7 +48,7 @@ The framework uses a plugin-based architecture that allows you to extend the age
48
48
  ### Creating Custom Plugins
49
49
 
50
50
  ```typescript
51
- import type { Plugin } from "@hybrd/hybrid"
51
+ import type { Plugin } from "hybrid"
52
52
  import { Hono } from "hono"
53
53
 
54
54
  function MyCustomPlugin(): Plugin {
@@ -0,0 +1,401 @@
1
+ import * as ai from 'ai';
2
+ import { UIMessage, LanguageModel, generateText, TelemetrySettings, streamText, UIMessageStreamOnFinishCallback } from 'ai';
3
+ import { z } from 'zod';
4
+ import { HonoVariables, MessageListenerConfig, XmtpConversation, XmtpMessage, XmtpSender, XmtpSubjects, XmtpServiceClient } from '@hybrd/xmtp';
5
+ import { Hono } from 'hono';
6
+
7
+ /**
8
+ * Plugin interface for extending the agent's Hono app
9
+ *
10
+ * @description
11
+ * Plugins allow you to extend the agent's HTTP server with additional
12
+ * routes, middleware, and functionality. Each plugin receives the Hono
13
+ * app instance and can modify it as needed.
14
+ *
15
+ * @template T - Optional context type that can be passed to the plugin
16
+ */
17
+ interface Plugin<T = Record<string, never>> {
18
+ /**
19
+ * Unique identifier for the plugin
20
+ */
21
+ name: string;
22
+ /**
23
+ * Optional description of what the plugin does
24
+ */
25
+ description?: string;
26
+ /**
27
+ * Function that applies the plugin to the Hono app
28
+ *
29
+ * @param app - The Hono app instance to extend
30
+ * @param context - Optional context data passed to the plugin
31
+ */
32
+ apply: (app: Hono<{
33
+ Variables: HonoVariables;
34
+ }>, context?: T) => void | Promise<void>;
35
+ }
36
+ /**
37
+ * Plugin registry that manages all registered plugins
38
+ *
39
+ * @description
40
+ * The plugin registry allows you to register, configure, and apply
41
+ * plugins to Hono apps. It provides a centralized way to manage
42
+ * plugin dependencies and execution order.
43
+ */
44
+ declare class PluginRegistry<T = Record<string, never>> {
45
+ private plugins;
46
+ /**
47
+ * Registers a plugin with the registry
48
+ *
49
+ * @param plugin - The plugin to register
50
+ * @throws {Error} If a plugin with the same name is already registered
51
+ */
52
+ register(plugin: Plugin<T>): void;
53
+ /**
54
+ * Unregisters a plugin from the registry
55
+ *
56
+ * @param name - The name of the plugin to unregister
57
+ * @returns True if the plugin was unregistered, false if it wasn't found
58
+ */
59
+ unregister(name: string): boolean;
60
+ /**
61
+ * Gets a plugin by name
62
+ *
63
+ * @param name - The name of the plugin to retrieve
64
+ * @returns The plugin if found, undefined otherwise
65
+ */
66
+ get(name: string): Plugin<T> | undefined;
67
+ /**
68
+ * Gets all registered plugins
69
+ *
70
+ * @returns Array of all registered plugins
71
+ */
72
+ getAll(): Plugin<T>[];
73
+ /**
74
+ * Checks if a plugin is registered
75
+ *
76
+ * @param name - The name of the plugin to check
77
+ * @returns True if the plugin is registered, false otherwise
78
+ */
79
+ has(name: string): boolean;
80
+ /**
81
+ * Applies all registered plugins to a Hono app
82
+ *
83
+ * @param app - The Hono app instance to extend
84
+ * @param context - Optional context data passed to all plugins
85
+ */
86
+ applyAll(app: Hono<{
87
+ Variables: HonoVariables;
88
+ }>, context?: T): Promise<void>;
89
+ /**
90
+ * Clears all registered plugins
91
+ */
92
+ clear(): void;
93
+ /**
94
+ * Gets the number of registered plugins
95
+ */
96
+ get size(): number;
97
+ }
98
+
99
+ /**
100
+ * Context type for plugins that need agent information
101
+ */
102
+ interface PluginContext {
103
+ agent: Agent<DefaultRuntimeExtension>;
104
+ }
105
+ /**
106
+ * Options for starting the XMTP tools HTTP server
107
+ *
108
+ * @description
109
+ * Configuration object for the standalone XMTP tools server that provides
110
+ * basic HTTP endpoints for health checks and XMTP operations.
111
+ *
112
+ * @property agent - The agent instance to associate with the server
113
+ * @property port - The port number to listen on (defaults to 8454)
114
+ * @property filter - Optional message filter for XMTP messages
115
+ * @property plugins - Optional array of plugins to apply to the server
116
+ */
117
+ type ListenOptions = {
118
+ agent: Agent;
119
+ port: string;
120
+ filter?: MessageListenerConfig["filter"];
121
+ plugins?: Plugin<PluginContext>[];
122
+ };
123
+ /**
124
+ * Starts a standalone XMTP tools HTTP server
125
+ *
126
+ * @description
127
+ * This function creates and starts a minimal HTTP server specifically for
128
+ * XMTP tools operations. It includes:
129
+ * - Health check endpoint at `/health`
130
+ * - 404 handler for unmatched routes
131
+ * - Automatic port parsing from string input
132
+ * - Plugin-based route mounting
133
+ *
134
+ * The server runs independently and is useful for scenarios where you need
135
+ * XMTP tools functionality without the full Hono app integration.
136
+ *
137
+ * @param options - Configuration options for the server
138
+ * @param options.agent - The agent instance to associate with the server
139
+ * @param options.port - The port number to listen on (parsed as integer)
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * listen({
144
+ * agent: myAgent,
145
+ * port: "3000"
146
+ * })
147
+ *
148
+ * // Server starts on port 3000 with health endpoint at /health
149
+ * // and all registered plugins applied
150
+ * ```
151
+ */
152
+ declare function listen({ agent, port, filter, plugins }: ListenOptions): Promise<void>;
153
+
154
+ interface BaseRuntime extends Record<string, unknown> {
155
+ conversation: XmtpConversation;
156
+ message: XmtpMessage;
157
+ parentMessage?: XmtpMessage;
158
+ rootMessage: XmtpMessage;
159
+ sender: XmtpSender;
160
+ subjects: XmtpSubjects;
161
+ xmtpClient: XmtpServiceClient;
162
+ }
163
+ type AgentRuntime = BaseRuntime & {
164
+ chatId?: string;
165
+ messages: Array<UIMessage>;
166
+ };
167
+ interface XmtpCredentials {
168
+ inboxId: string;
169
+ xmtpServiceUrl: string;
170
+ xmtpServiceToken: string;
171
+ }
172
+
173
+ type DefaultRuntimeExtension$1 = Record<string, never>;
174
+ /**
175
+ * Configuration interface for creating custom tools that integrate with AI SDK.
176
+ */
177
+ interface ToolConfig<TInput extends z.ZodTypeAny = z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny, TRuntimeExtension = DefaultRuntimeExtension$1> {
178
+ /** Unique identifier for the tool */
179
+ id: string;
180
+ /** Human-readable description of what the tool does */
181
+ description: string;
182
+ /** Zod schema for validating tool input */
183
+ inputSchema: TInput;
184
+ /** Optional Zod schema for validating tool output */
185
+ outputSchema?: TOutput;
186
+ /** Function that executes the tool's logic */
187
+ execute: (args: {
188
+ input: z.infer<TInput>;
189
+ runtime: AgentRuntime & TRuntimeExtension;
190
+ messages: UIMessage[];
191
+ }) => Promise<z.infer<TOutput>>;
192
+ }
193
+ /**
194
+ * Internal tool interface used throughout the agent framework.
195
+ * Similar to ToolConfig but without the ID field, used after tool creation.
196
+ */
197
+ interface Tool<TInput extends z.ZodTypeAny = z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny, TRuntimeExtension = DefaultRuntimeExtension$1> {
198
+ /** Human-readable description of what the tool does */
199
+ description: string;
200
+ /** Zod schema for validating tool input */
201
+ inputSchema: TInput;
202
+ /** Optional Zod schema for validating tool output */
203
+ outputSchema?: TOutput;
204
+ /** Function that executes the tool's logic */
205
+ execute: (args: {
206
+ input: z.infer<TInput>;
207
+ runtime: AgentRuntime & TRuntimeExtension;
208
+ messages: UIMessage[];
209
+ }) => Promise<z.infer<TOutput>>;
210
+ }
211
+ /**
212
+ * Factory function to create tools with custom runtime extensions.
213
+ * Provides proper type inference for input/output schemas and runtime extensions.
214
+ */
215
+ declare function toolFactory<TRuntimeExtension = DefaultRuntimeExtension$1>(): <TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny>(config: ToolConfig<TInput, TOutput, TRuntimeExtension>) => Tool<TInput, TOutput, TRuntimeExtension>;
216
+ /**
217
+ * Default tool factory with no runtime extensions.
218
+ * Type-safe at creation time with proper schema inference.
219
+ */
220
+ declare const createTool: <TInput extends z.ZodTypeAny, TOutput extends z.ZodTypeAny = z.ZodTypeAny>(config: ToolConfig<TInput, TOutput, DefaultRuntimeExtension$1>) => Tool<TInput, TOutput, DefaultRuntimeExtension$1>;
221
+
222
+ type DefaultRuntimeExtension = Record<string, never>;
223
+ /**
224
+ * Tool that can be used in Agent configuration.
225
+ * Extends the base Tool type with runtime extension support.
226
+ */
227
+ type AgentTool<TRuntimeExtension = DefaultRuntimeExtension> = Tool<z.ZodTypeAny, z.ZodTypeAny, TRuntimeExtension>;
228
+ /**
229
+ * Function that generates tools dynamically based on messages and runtime context.
230
+ * Allows for context-aware tool selection and configuration.
231
+ */
232
+ type ToolGenerator<TRuntimeExtension = DefaultRuntimeExtension> = (props: {
233
+ messages: UIMessage[];
234
+ runtime: AgentRuntime & TRuntimeExtension;
235
+ }) => Record<string, AgentTool<TRuntimeExtension>> | Promise<Record<string, AgentTool<TRuntimeExtension>>>;
236
+ /**
237
+ * Configuration interface for creating an Agent instance.
238
+ * Supports both static and dynamic configuration through functions.
239
+ */
240
+ interface AgentConfig<TRuntimeExtension = DefaultRuntimeExtension> {
241
+ /** Unique identifier for the agent */
242
+ name: string;
243
+ /** Language model to use, can be static or dynamically resolved */
244
+ model: LanguageModel | ((props: {
245
+ runtime: AgentRuntime & TRuntimeExtension;
246
+ }) => LanguageModel | Promise<LanguageModel>);
247
+ /** Tools available to the agent, can be static or dynamically generated */
248
+ tools?: Record<string, AgentTool<TRuntimeExtension>> | ToolGenerator<TRuntimeExtension>;
249
+ /** Instructions for the agent, can be static or dynamically resolved */
250
+ instructions: string | ((props: {
251
+ messages: UIMessage[];
252
+ runtime: AgentRuntime & TRuntimeExtension;
253
+ }) => string | Promise<string>);
254
+ /** Function to create the runtime extension, type will be inferred */
255
+ createRuntime?: (runtime: AgentRuntime) => TRuntimeExtension | Promise<TRuntimeExtension>;
256
+ /** Optional metadata for the agent */
257
+ metadata?: Record<string, unknown>;
258
+ /** Maximum number of steps the agent can take */
259
+ maxSteps?: number;
260
+ /** Maximum tokens for generation */
261
+ maxTokens?: number;
262
+ /** Temperature for generation (0.0 to 2.0) */
263
+ temperature?: number;
264
+ }
265
+ /**
266
+ * Options for text generation with the agent.
267
+ * Extends AI SDK parameters while adding agent-specific options.
268
+ */
269
+ interface GenerateOptions<TRuntimeExtension = DefaultRuntimeExtension> extends Omit<Parameters<typeof generateText>[0], "model" | "tools" | "instructions" | "onFinish"> {
270
+ /** Maximum tokens for generation */
271
+ maxTokens?: number;
272
+ /** Runtime context for the agent */
273
+ runtime: AgentRuntime & TRuntimeExtension;
274
+ /** Optional telemetry configuration */
275
+ telemetry?: NonNullable<TelemetrySettings>;
276
+ }
277
+ /**
278
+ * Options for streaming text with the agent.
279
+ * Extends AI SDK parameters while adding agent-specific options.
280
+ */
281
+ interface StreamOptions<TRuntimeExtension = DefaultRuntimeExtension> extends Omit<Parameters<typeof streamText>[0], "model" | "tools" | "instructions" | "onFinish"> {
282
+ /** Maximum tokens for generation */
283
+ maxTokens?: number;
284
+ /** Runtime context for the agent */
285
+ runtime: AgentRuntime & TRuntimeExtension;
286
+ /** Optional telemetry configuration */
287
+ telemetry?: NonNullable<TelemetrySettings>;
288
+ /** Callback when streaming finishes */
289
+ onFinish?: UIMessageStreamOnFinishCallback<UIMessage>;
290
+ }
291
+ /**
292
+ * Core Agent implementation using AI SDK 5 directly.
293
+ * This class provides a flexible interface for creating AI agents with
294
+ * dynamic configuration, tool support, and streaming capabilities.
295
+ */
296
+ declare class Agent<TRuntimeExtension = DefaultRuntimeExtension> {
297
+ /** Agent's unique identifier */
298
+ readonly name: string;
299
+ /** Optional description of the agent */
300
+ readonly description?: string;
301
+ /** Optional metadata associated with the agent */
302
+ readonly metadata?: Record<string, unknown>;
303
+ /** Agent configuration */
304
+ private readonly config;
305
+ /** Default parameters for text generation */
306
+ private readonly generationDefaults;
307
+ /** Default parameters for text streaming */
308
+ private readonly streamDefaults;
309
+ /** Plugin registry for extending the agent's HTTP server */
310
+ readonly plugins: PluginRegistry<PluginContext>;
311
+ /**
312
+ * Creates a new Agent instance with the specified configuration.
313
+ * @param config - Configuration object for the agent
314
+ */
315
+ constructor(config: AgentConfig<TRuntimeExtension>);
316
+ /**
317
+ * Resolves dynamic configuration properties (model, tools, instructions) with runtime context.
318
+ * @param messages - Current conversation messages
319
+ * @param runtime - Runtime context for the agent
320
+ * @returns Resolved configuration with model, tools, and instructions
321
+ */
322
+ private resolveConfig;
323
+ /**
324
+ * Prepares messages by adding system instructions if provided.
325
+ * Merges instructions with existing system messages or creates new ones.
326
+ * @param messages - Current conversation messages
327
+ * @param instructions - System instructions to add
328
+ * @returns Messages with system instructions properly integrated
329
+ */
330
+ private prepareMessages;
331
+ /**
332
+ * Generates a text completion using the agent's configuration.
333
+ * @param messages - Conversation messages to generate from
334
+ * @param options - Generation options including runtime context
335
+ * @returns Generated text completion result
336
+ */
337
+ generate(messages: UIMessage[], options: GenerateOptions<TRuntimeExtension>): Promise<ai.GenerateTextResult<Record<string, ai.Tool>, unknown>>;
338
+ /**
339
+ * Streams a text completion using the agent's configuration.
340
+ * @param messages - Conversation messages to generate from
341
+ * @param options - Streaming options including runtime context
342
+ * @returns Streaming response that can be consumed
343
+ */
344
+ stream(messages: UIMessage[], options: StreamOptions<TRuntimeExtension>): Promise<Response>;
345
+ /**
346
+ * Gets the agent's configuration for debugging and inspection.
347
+ * @returns Object containing agent configuration details
348
+ */
349
+ getConfig(): {
350
+ name: string;
351
+ description: string | undefined;
352
+ metadata: Record<string, unknown> | undefined;
353
+ hasModel: boolean;
354
+ hasTools: boolean;
355
+ hasInstructions: boolean;
356
+ };
357
+ /**
358
+ * Gets the agent's instructions without running generation.
359
+ * Useful for external integrations that need instructions separately.
360
+ * @param options - Options containing runtime context and optional messages
361
+ * @returns Resolved instructions string
362
+ */
363
+ getInstructions(options: {
364
+ runtime: AgentRuntime & TRuntimeExtension;
365
+ messages?: UIMessage[];
366
+ }): Promise<string>;
367
+ /**
368
+ * Gets the agent's tools without running generation.
369
+ * Useful for external integrations that need tools separately.
370
+ * @param options - Options containing runtime context and optional messages
371
+ * @returns Resolved tools object
372
+ */
373
+ getTools(options: {
374
+ runtime: AgentRuntime & TRuntimeExtension;
375
+ messages?: UIMessage[];
376
+ }): Promise<Record<string, AgentTool<TRuntimeExtension>> | undefined>;
377
+ /**
378
+ * Creates the complete runtime context by merging base runtime with custom extension.
379
+ * @param baseRuntime - The base runtime context containing XMTP properties
380
+ * @returns Complete runtime context with custom extensions applied
381
+ */
382
+ /**
383
+ * Creates the complete runtime context by merging base runtime with custom extension.
384
+ * @param baseRuntime - The base runtime context containing XMTP properties
385
+ * @returns Complete runtime context with custom extensions applied
386
+ */
387
+ createRuntimeContext(baseRuntime: AgentRuntime): Promise<AgentRuntime & TRuntimeExtension>;
388
+ /**
389
+ * Registers a plugin with the agent
390
+ *
391
+ * @param plugin - The plugin to register
392
+ */
393
+ use(plugin: Plugin<PluginContext>): void;
394
+ /**
395
+ * Starts listening for messages and events using the agent instance.
396
+ * @param opts - Configuration options for the listener, excluding the agent property
397
+ */
398
+ listen(opts: Omit<ListenOptions, "agent">): Promise<void>;
399
+ }
400
+
401
+ export { Agent as A, type BaseRuntime as B, type DefaultRuntimeExtension as D, type ListenOptions as L, PluginRegistry as P, type Tool as T, type XmtpCredentials as X, type AgentConfig as a, type Plugin as b, createTool as c, type ToolConfig as d, type AgentRuntime as e, type PluginContext as f, listen as l, toolFactory as t };