@agentick/express 0.0.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/src/index.ts ADDED
@@ -0,0 +1,157 @@
1
+ /**
2
+ * @agentick/express - Express adapter for Agentick Gateway
3
+ *
4
+ * Provides an Express middleware that delegates to Gateway.
5
+ * This is a thin adapter - all business logic lives in @agentick/gateway.
6
+ *
7
+ * @example Quick start
8
+ * ```typescript
9
+ * import express from "express";
10
+ * import { createAgentickMiddleware } from "@agentick/express";
11
+ * import { createApp } from "@agentick/core";
12
+ *
13
+ * const app = express();
14
+ * app.use(express.json());
15
+ *
16
+ * const agentickApp = createApp(<MyAgent />);
17
+ *
18
+ * app.use("/api", createAgentickMiddleware({
19
+ * apps: { assistant: agentickApp },
20
+ * defaultApp: "assistant",
21
+ * }));
22
+ *
23
+ * const server = app.listen(3000);
24
+ *
25
+ * // Cleanup on shutdown
26
+ * process.on("SIGTERM", () => server.close());
27
+ * ```
28
+ *
29
+ * @example With custom methods and auth
30
+ * ```typescript
31
+ * import { createAgentickMiddleware, method } from "@agentick/express";
32
+ * import { z } from "zod";
33
+ *
34
+ * app.use("/api", createAgentickMiddleware({
35
+ * apps: { assistant: agentickApp },
36
+ * defaultApp: "assistant",
37
+ * auth: {
38
+ * type: "custom",
39
+ * validate: async (token) => {
40
+ * const user = await verifyToken(token);
41
+ * return user ? { valid: true, user } : { valid: false };
42
+ * },
43
+ * },
44
+ * methods: {
45
+ * tasks: {
46
+ * list: method({
47
+ * schema: z.object({ sessionId: z.string() }),
48
+ * handler: async (params) => todoService.list(params.sessionId),
49
+ * }),
50
+ * },
51
+ * },
52
+ * }));
53
+ * ```
54
+ *
55
+ * @module @agentick/express
56
+ */
57
+
58
+ import { Router } from "express";
59
+ import type { Request, Response, NextFunction } from "express";
60
+ import { Gateway, type GatewayConfig } from "@agentick/gateway";
61
+
62
+ /**
63
+ * Options for the Express middleware.
64
+ */
65
+ export interface AgentickMiddlewareOptions {
66
+ /**
67
+ * Extract token from Express request.
68
+ * By default, extracts from Authorization header.
69
+ */
70
+ getToken?: (req: Request) => string | undefined;
71
+ }
72
+
73
+ /**
74
+ * Gateway config type for Express middleware.
75
+ * Excludes standalone-mode-only options.
76
+ */
77
+ export type AgentickExpressConfig = Omit<GatewayConfig, "port" | "host" | "transport" | "httpPort">;
78
+
79
+ /**
80
+ * Express Router with attached Gateway instance for lifecycle management.
81
+ */
82
+ export interface AgentickRouter extends Router {
83
+ /** The underlying Gateway instance for lifecycle management */
84
+ gateway: Gateway;
85
+ }
86
+
87
+ /**
88
+ * Create Express middleware that delegates to Gateway.
89
+ *
90
+ * @param gatewayConfig - Gateway configuration (apps, methods, auth, etc.)
91
+ * @param options - Optional Express-specific options
92
+ * @returns Express Router middleware with attached gateway
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * const middleware = createAgentickMiddleware({
97
+ * apps: { assistant: myApp },
98
+ * defaultApp: "assistant",
99
+ * });
100
+ *
101
+ * app.use("/api", middleware);
102
+ *
103
+ * // Access gateway for lifecycle management
104
+ * process.on("SIGTERM", () => middleware.gateway.close());
105
+ * ```
106
+ */
107
+ export function createAgentickMiddleware(
108
+ gatewayConfig: AgentickExpressConfig,
109
+ options: AgentickMiddlewareOptions = {},
110
+ ): AgentickRouter {
111
+ // Create gateway in embedded mode
112
+ const gateway = new Gateway({
113
+ ...gatewayConfig,
114
+ embedded: true,
115
+ });
116
+
117
+ const router = Router() as AgentickRouter;
118
+
119
+ // Attach gateway for lifecycle management
120
+ router.gateway = gateway;
121
+
122
+ // Delegate all requests to gateway
123
+ router.use((req: Request, res: Response, next: NextFunction) => {
124
+ // Optionally inject token from custom extractor
125
+ if (options.getToken) {
126
+ const token = options.getToken(req);
127
+ if (token) {
128
+ req.headers.authorization = `Bearer ${token}`;
129
+ }
130
+ }
131
+
132
+ // Delegate to gateway
133
+ gateway.handleRequest(req, res).catch(next);
134
+ });
135
+
136
+ return router;
137
+ }
138
+
139
+ /**
140
+ * Get the Gateway instance from middleware for advanced use.
141
+ * Useful for lifecycle management, events, etc.
142
+ */
143
+ export function createAgentickGateway(gatewayConfig: AgentickExpressConfig): Gateway {
144
+ return new Gateway({
145
+ ...gatewayConfig,
146
+ embedded: true,
147
+ });
148
+ }
149
+
150
+ // Re-export gateway types for convenience
151
+ export {
152
+ Gateway,
153
+ method,
154
+ type GatewayConfig,
155
+ type MethodDefinition,
156
+ type AuthConfig,
157
+ } from "@agentick/gateway";