@agentuity/runtime 0.0.104 → 0.0.105
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 +61 -35
- package/dist/app.d.ts +5 -2
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js.map +1 -1
- package/dist/handlers/cron.d.ts +47 -0
- package/dist/handlers/cron.d.ts.map +1 -0
- package/dist/handlers/cron.js +49 -0
- package/dist/handlers/cron.js.map +1 -0
- package/dist/handlers/index.d.ts +5 -0
- package/dist/handlers/index.d.ts.map +1 -0
- package/dist/handlers/index.js +5 -0
- package/dist/handlers/index.js.map +1 -0
- package/dist/handlers/sse.d.ts +74 -0
- package/dist/handlers/sse.d.ts.map +1 -0
- package/dist/handlers/sse.js +70 -0
- package/dist/handlers/sse.js.map +1 -0
- package/dist/handlers/stream.d.ts +52 -0
- package/dist/handlers/stream.d.ts.map +1 -0
- package/dist/handlers/stream.js +75 -0
- package/dist/handlers/stream.js.map +1 -0
- package/dist/handlers/websocket.d.ts +49 -0
- package/dist/handlers/websocket.d.ts.map +1 -0
- package/dist/handlers/websocket.js +130 -0
- package/dist/handlers/websocket.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/otel/logger.d.ts +1 -4
- package/dist/otel/logger.d.ts.map +1 -1
- package/dist/otel/logger.js +11 -2
- package/dist/otel/logger.js.map +1 -1
- package/dist/router.d.ts +46 -236
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +82 -349
- package/dist/router.js.map +1 -1
- package/dist/workbench.d.ts +6 -2
- package/dist/workbench.d.ts.map +1 -1
- package/dist/workbench.js +29 -26
- package/dist/workbench.js.map +1 -1
- package/package.json +5 -7
- package/src/app.ts +7 -2
- package/src/handlers/cron.ts +70 -0
- package/src/handlers/index.ts +4 -0
- package/src/handlers/sse.ts +118 -0
- package/src/handlers/stream.ts +86 -0
- package/src/handlers/websocket.ts +153 -0
- package/src/index.ts +15 -3
- package/src/otel/logger.ts +13 -2
- package/src/router.ts +110 -597
- package/src/workbench.ts +30 -27
- package/dist/io/email.d.ts +0 -77
- package/dist/io/email.d.ts.map +0 -1
- package/dist/io/email.js +0 -162
- package/dist/io/email.js.map +0 -1
- package/src/io/email.ts +0 -191
package/README.md
CHANGED
|
@@ -74,48 +74,68 @@ export default router;
|
|
|
74
74
|
### Streaming Responses
|
|
75
75
|
|
|
76
76
|
```typescript
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
77
|
+
import { createRouter, stream } from '@agentuity/runtime';
|
|
78
|
+
|
|
79
|
+
const router = createRouter();
|
|
80
|
+
|
|
81
|
+
router.post(
|
|
82
|
+
'/events',
|
|
83
|
+
stream((c) => {
|
|
84
|
+
return new ReadableStream({
|
|
85
|
+
start(controller) {
|
|
86
|
+
controller.enqueue('Event 1\n');
|
|
87
|
+
controller.enqueue('Event 2\n');
|
|
88
|
+
controller.close();
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
})
|
|
92
|
+
);
|
|
86
93
|
```
|
|
87
94
|
|
|
88
95
|
### WebSocket Support
|
|
89
96
|
|
|
90
97
|
```typescript
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
ws.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
});
|
|
98
|
+
import { createRouter, websocket } from '@agentuity/runtime';
|
|
99
|
+
|
|
100
|
+
const router = createRouter();
|
|
101
|
+
|
|
102
|
+
router.get(
|
|
103
|
+
'/chat',
|
|
104
|
+
websocket((c, ws) => {
|
|
105
|
+
ws.onOpen(() => {
|
|
106
|
+
console.log('Client connected');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
ws.onMessage((event) => {
|
|
110
|
+
const data = JSON.parse(event.data);
|
|
111
|
+
ws.send(JSON.stringify({ echo: data }));
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
ws.onClose(() => {
|
|
115
|
+
console.log('Client disconnected');
|
|
116
|
+
});
|
|
117
|
+
})
|
|
118
|
+
);
|
|
105
119
|
```
|
|
106
120
|
|
|
107
121
|
### Server-Sent Events (SSE)
|
|
108
122
|
|
|
109
123
|
```typescript
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
124
|
+
import { createRouter, sse } from '@agentuity/runtime';
|
|
125
|
+
|
|
126
|
+
const router = createRouter();
|
|
127
|
+
|
|
128
|
+
router.get(
|
|
129
|
+
'/updates',
|
|
130
|
+
sse((c, stream) => {
|
|
131
|
+
for (let i = 0; i < 10; i++) {
|
|
132
|
+
stream.writeSSE({
|
|
133
|
+
data: JSON.stringify({ count: i }),
|
|
134
|
+
event: 'update',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
);
|
|
119
139
|
```
|
|
120
140
|
|
|
121
141
|
## API Reference
|
|
@@ -147,9 +167,15 @@ Creates a new router for defining custom API routes.
|
|
|
147
167
|
**Methods:**
|
|
148
168
|
|
|
149
169
|
- `get/post/put/delete/patch` - HTTP method handlers
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
170
|
+
|
|
171
|
+
**Middleware Functions:**
|
|
172
|
+
|
|
173
|
+
Use these middleware functions with standard HTTP methods:
|
|
174
|
+
|
|
175
|
+
- `websocket((c, ws) => { ... })` - WebSocket connections (use with `router.get()`)
|
|
176
|
+
- `sse((c, stream) => { ... })` - Server-Sent Events (use with `router.get()`)
|
|
177
|
+
- `stream((c) => ReadableStream)` - Streaming responses (use with `router.post()`)
|
|
178
|
+
- `cron(schedule, (c) => { ... })` - Scheduled tasks (use with `router.post()`)
|
|
153
179
|
|
|
154
180
|
### AgentContext
|
|
155
181
|
|
package/dist/app.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import type { compress } from 'hono/compress';
|
|
|
4
4
|
import type { Logger } from './logger';
|
|
5
5
|
import type { Meter, Tracer } from '@opentelemetry/api';
|
|
6
6
|
import type { KeyValueStorage, SessionEventProvider, EvalRunEventProvider, StreamStorage, VectorStorage, SessionStartEvent } from '@agentuity/core';
|
|
7
|
-
import type { Email } from './io/email';
|
|
8
7
|
import type { ThreadProvider, SessionProvider, Session, Thread } from './session';
|
|
9
8
|
import type WaitUntilHandler from './_waituntil';
|
|
10
9
|
import type { Context } from 'hono';
|
|
@@ -120,12 +119,16 @@ export interface AppConfig<TAppState = Record<string, never>> {
|
|
|
120
119
|
* Receives the app state returned from setup
|
|
121
120
|
*/
|
|
122
121
|
shutdown?: (state: TAppState) => Promise<void> | void;
|
|
122
|
+
/**
|
|
123
|
+
* Optional request timeout in seconds. If not provided, will default
|
|
124
|
+
* to zero which will cause the request to wait indefinitely.
|
|
125
|
+
*/
|
|
126
|
+
requestTimeout?: number;
|
|
123
127
|
}
|
|
124
128
|
export interface Variables<TAppState = Record<string, never>> {
|
|
125
129
|
logger: Logger;
|
|
126
130
|
meter: Meter;
|
|
127
131
|
tracer: Tracer;
|
|
128
|
-
email?: Email;
|
|
129
132
|
sessionId: string;
|
|
130
133
|
thread: Thread;
|
|
131
134
|
session: Session;
|
package/dist/app.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,IAAI,OAAO,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,MAAM,EAAE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C,CAAC,CAAC,CAAC,MAAM,CAAC;QACzC,CAAC;KACD,CAAC;IAEF,sCAAsC;IACtC,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,MAAM,CAAC;IACxC,MAAM,MAAM,GAAW;QACtB,GAAG,EAAE,oBAAoB,IAAI,EAAE;KAC/B,CAAC;IAEF,kEAAkE;IAClE,2DAA2D;IAC3D,MAAM,YAAY,GAAG,SAAS,EAAE,CAAC;IACjC,IAAI,CAAC,YAAY,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CACd,qLAAqL,CACrL,CAAC;IACH,CAAC;IACD,MAAM,MAAM,GAAG,YAAoC,CAAC;IAEpD,OAAO;QACN,KAAK;QACL,QAAQ;QACR,MAAM;QACN,MAAM;QACN,MAAM;QACN,MAAM;QACN,gBAAgB,EAAE,sBAAsB;QACxC,mBAAmB,EAAE,yBAAyB;KAC9C,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW;IAC1B,OAAQ,UAAkB,CAAC,uBAAuB,IAAK,EAAgB,CAAC;AACzE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY;IAC3B,OAAQ,UAAkB,CAAC,wBAAwB,CAAC;AACrD,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW;IAChC,MAAM,QAAQ,GAAI,UAAkB,CAAC,sBAAsB,CAAC;IAC5D,IAAI,QAAQ,EAAE,CAAC;QACd,MAAM,KAAK,GAAG,WAAW,EAAE,CAAC;QAC5B,MAAM,QAAQ,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Context, Handler } from 'hono';
|
|
2
|
+
import type { Env } from '../app';
|
|
3
|
+
/**
|
|
4
|
+
* Handler function for cron jobs.
|
|
5
|
+
* Receives the Hono context and can return any response.
|
|
6
|
+
*/
|
|
7
|
+
export type CronHandler<E extends Env = Env> = (c: Context<E>) => unknown | Promise<unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a cron middleware for scheduled task endpoints.
|
|
10
|
+
*
|
|
11
|
+
* **Important:** Cron endpoints must use POST method. The middleware will throw
|
|
12
|
+
* an error if called with any other HTTP method.
|
|
13
|
+
*
|
|
14
|
+
* Use with router.post() to create a cron endpoint:
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { createRouter, cron } from '@agentuity/runtime';
|
|
19
|
+
*
|
|
20
|
+
* const router = createRouter();
|
|
21
|
+
*
|
|
22
|
+
* // Daily cleanup at midnight
|
|
23
|
+
* router.post('/daily-cleanup', cron('0 0 * * *', (c) => {
|
|
24
|
+
* c.var.logger.info('Running daily cleanup');
|
|
25
|
+
* return { status: 'cleanup complete' };
|
|
26
|
+
* }));
|
|
27
|
+
*
|
|
28
|
+
* // Hourly health check
|
|
29
|
+
* router.post('/health-check', cron('0 * * * *', (c) => {
|
|
30
|
+
* c.var.logger.info('Running hourly health check');
|
|
31
|
+
* return c.text('OK');
|
|
32
|
+
* }));
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @param schedule - Cron expression (e.g., '0 0 * * *' for daily at midnight)
|
|
36
|
+
* @param handler - Handler function to run on schedule
|
|
37
|
+
* @returns Hono handler for cron endpoint
|
|
38
|
+
*/
|
|
39
|
+
export declare function cron<E extends Env = Env>(schedule: string, handler: CronHandler<E>): Handler<E>;
|
|
40
|
+
/**
|
|
41
|
+
* Metadata interface for cron jobs (can be used for registration/discovery).
|
|
42
|
+
*/
|
|
43
|
+
export interface CronMetadata {
|
|
44
|
+
schedule: string;
|
|
45
|
+
path: string;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=cron.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cron.d.ts","sourceRoot":"","sources":["../../src/handlers/cron.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE7C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAElC;;;GAGG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAE7F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAoB/F;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACb"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { returnResponse } from '../_util';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a cron middleware for scheduled task endpoints.
|
|
4
|
+
*
|
|
5
|
+
* **Important:** Cron endpoints must use POST method. The middleware will throw
|
|
6
|
+
* an error if called with any other HTTP method.
|
|
7
|
+
*
|
|
8
|
+
* Use with router.post() to create a cron endpoint:
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { createRouter, cron } from '@agentuity/runtime';
|
|
13
|
+
*
|
|
14
|
+
* const router = createRouter();
|
|
15
|
+
*
|
|
16
|
+
* // Daily cleanup at midnight
|
|
17
|
+
* router.post('/daily-cleanup', cron('0 0 * * *', (c) => {
|
|
18
|
+
* c.var.logger.info('Running daily cleanup');
|
|
19
|
+
* return { status: 'cleanup complete' };
|
|
20
|
+
* }));
|
|
21
|
+
*
|
|
22
|
+
* // Hourly health check
|
|
23
|
+
* router.post('/health-check', cron('0 * * * *', (c) => {
|
|
24
|
+
* c.var.logger.info('Running hourly health check');
|
|
25
|
+
* return c.text('OK');
|
|
26
|
+
* }));
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param schedule - Cron expression (e.g., '0 0 * * *' for daily at midnight)
|
|
30
|
+
* @param handler - Handler function to run on schedule
|
|
31
|
+
* @returns Hono handler for cron endpoint
|
|
32
|
+
*/
|
|
33
|
+
export function cron(schedule, handler) {
|
|
34
|
+
return async (c) => {
|
|
35
|
+
if (c.req.method !== 'POST') {
|
|
36
|
+
throw new Error(`Cron endpoint must use POST method, but received ${c.req.method}. ` +
|
|
37
|
+
`Use router.post() instead of router.${c.req.method.toLowerCase()}().`);
|
|
38
|
+
}
|
|
39
|
+
let result = handler(c);
|
|
40
|
+
if (result instanceof Promise) {
|
|
41
|
+
result = await result;
|
|
42
|
+
}
|
|
43
|
+
if (result instanceof Response) {
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
return returnResponse(c, result);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=cron.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cron.js","sourceRoot":"","sources":["../../src/handlers/cron.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAS1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,IAAI,CAAsB,QAAgB,EAAE,OAAuB;IAClF,OAAO,KAAK,EAAE,CAAa,EAAE,EAAE;QAC9B,IAAI,CAAC,CAAC,GAAG,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;YAC7B,MAAM,IAAI,KAAK,CACd,oDAAoD,CAAC,CAAC,GAAG,CAAC,MAAM,IAAI;gBACnE,uCAAuC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,EAAE,KAAK,CACvE,CAAC;QACH,CAAC;QAED,IAAI,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QACxB,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;YAC/B,MAAM,GAAG,MAAM,MAAM,CAAC;QACvB,CAAC;QAED,IAAI,MAAM,YAAY,QAAQ,EAAE,CAAC;YAChC,OAAO,MAAM,CAAC;QACf,CAAC;QAED,OAAO,cAAc,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IAClC,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { websocket, type WebSocketConnection, type WebSocketHandler } from './websocket';
|
|
2
|
+
export { sse, type SSEMessage, type SSEStream, type SSEHandler } from './sse';
|
|
3
|
+
export { stream, type StreamHandler } from './stream';
|
|
4
|
+
export { cron, type CronHandler, type CronMetadata } from './cron';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/handlers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,mBAAmB,EAAE,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACzF,OAAO,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,KAAK,SAAS,EAAE,KAAK,UAAU,EAAE,MAAM,OAAO,CAAC;AAC9E,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,EAAE,IAAI,EAAE,KAAK,WAAW,EAAE,KAAK,YAAY,EAAE,MAAM,QAAQ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/handlers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAmD,MAAM,aAAa,CAAC;AACzF,OAAO,EAAE,GAAG,EAAoD,MAAM,OAAO,CAAC;AAC9E,OAAO,EAAE,MAAM,EAAsB,MAAM,UAAU,CAAC;AACtD,OAAO,EAAE,IAAI,EAAuC,MAAM,QAAQ,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Context, Handler } from 'hono';
|
|
2
|
+
import type { Env } from '../app';
|
|
3
|
+
/**
|
|
4
|
+
* SSE message format for Server-Sent Events.
|
|
5
|
+
*/
|
|
6
|
+
export interface SSEMessage {
|
|
7
|
+
data: string;
|
|
8
|
+
event?: string;
|
|
9
|
+
id?: string;
|
|
10
|
+
retry?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* SSE stream interface for writing Server-Sent Events.
|
|
14
|
+
*/
|
|
15
|
+
export interface SSEStream {
|
|
16
|
+
/**
|
|
17
|
+
* Write a simple value as SSE data.
|
|
18
|
+
* Strings, numbers, and booleans are converted to string data.
|
|
19
|
+
* Objects are passed through as SSE message format.
|
|
20
|
+
*/
|
|
21
|
+
write: (data: string | number | boolean | SSEMessage) => Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Write a properly formatted SSE message.
|
|
24
|
+
*/
|
|
25
|
+
writeSSE: (message: SSEMessage) => Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Register a callback for when the client aborts the connection.
|
|
28
|
+
*/
|
|
29
|
+
onAbort: (callback: () => void) => void;
|
|
30
|
+
/**
|
|
31
|
+
* Close the SSE stream.
|
|
32
|
+
*/
|
|
33
|
+
close: () => void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Handler function for SSE connections.
|
|
37
|
+
* Receives the Hono context and SSE stream with a flattened signature.
|
|
38
|
+
*/
|
|
39
|
+
export type SSEHandler<E extends Env = Env> = (c: Context<E>, stream: SSEStream) => void | Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Creates an SSE (Server-Sent Events) middleware for streaming updates to clients.
|
|
42
|
+
*
|
|
43
|
+
* Use with router.get() to create an SSE endpoint:
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import { createRouter, sse } from '@agentuity/runtime';
|
|
48
|
+
*
|
|
49
|
+
* const router = createRouter();
|
|
50
|
+
*
|
|
51
|
+
* router.get('/events', sse((c, stream) => {
|
|
52
|
+
* let count = 0;
|
|
53
|
+
* const interval = setInterval(() => {
|
|
54
|
+
* stream.writeSSE({
|
|
55
|
+
* data: `Event ${++count}`,
|
|
56
|
+
* event: 'update'
|
|
57
|
+
* });
|
|
58
|
+
* if (count >= 10) {
|
|
59
|
+
* clearInterval(interval);
|
|
60
|
+
* stream.close();
|
|
61
|
+
* }
|
|
62
|
+
* }, 1000);
|
|
63
|
+
*
|
|
64
|
+
* stream.onAbort(() => {
|
|
65
|
+
* clearInterval(interval);
|
|
66
|
+
* });
|
|
67
|
+
* }));
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @param handler - Handler function receiving context and SSE stream
|
|
71
|
+
* @returns Hono handler for SSE streaming
|
|
72
|
+
*/
|
|
73
|
+
export declare function sse<E extends Env = Env>(handler: SSEHandler<E>): Handler<E>;
|
|
74
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/handlers/sse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAG7C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAElC;;GAEG;AACH,MAAM,WAAW,UAAU;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACzB;;;;OAIG;IACH,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACvE;;OAEG;IACH,QAAQ,EAAE,CAAC,OAAO,EAAE,UAAU,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACjD;;OAEG;IACH,OAAO,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;IACxC;;OAEG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI,CAC7C,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,EACb,MAAM,EAAE,SAAS,KACb,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAoC3E"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { streamSSE as honoStreamSSE } from 'hono/streaming';
|
|
2
|
+
import { getAgentAsyncLocalStorage } from '../_context';
|
|
3
|
+
/**
|
|
4
|
+
* Creates an SSE (Server-Sent Events) middleware for streaming updates to clients.
|
|
5
|
+
*
|
|
6
|
+
* Use with router.get() to create an SSE endpoint:
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { createRouter, sse } from '@agentuity/runtime';
|
|
11
|
+
*
|
|
12
|
+
* const router = createRouter();
|
|
13
|
+
*
|
|
14
|
+
* router.get('/events', sse((c, stream) => {
|
|
15
|
+
* let count = 0;
|
|
16
|
+
* const interval = setInterval(() => {
|
|
17
|
+
* stream.writeSSE({
|
|
18
|
+
* data: `Event ${++count}`,
|
|
19
|
+
* event: 'update'
|
|
20
|
+
* });
|
|
21
|
+
* if (count >= 10) {
|
|
22
|
+
* clearInterval(interval);
|
|
23
|
+
* stream.close();
|
|
24
|
+
* }
|
|
25
|
+
* }, 1000);
|
|
26
|
+
*
|
|
27
|
+
* stream.onAbort(() => {
|
|
28
|
+
* clearInterval(interval);
|
|
29
|
+
* });
|
|
30
|
+
* }));
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param handler - Handler function receiving context and SSE stream
|
|
34
|
+
* @returns Hono handler for SSE streaming
|
|
35
|
+
*/
|
|
36
|
+
export function sse(handler) {
|
|
37
|
+
return (c) => {
|
|
38
|
+
const asyncLocalStorage = getAgentAsyncLocalStorage();
|
|
39
|
+
const capturedContext = asyncLocalStorage.getStore();
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
return honoStreamSSE(c, async (stream) => {
|
|
42
|
+
const wrappedStream = {
|
|
43
|
+
write: async (data) => {
|
|
44
|
+
if (typeof data === 'string' ||
|
|
45
|
+
typeof data === 'number' ||
|
|
46
|
+
typeof data === 'boolean') {
|
|
47
|
+
return stream.writeSSE({ data: String(data) });
|
|
48
|
+
}
|
|
49
|
+
else if (typeof data === 'object' && data !== null) {
|
|
50
|
+
return stream.writeSSE(data);
|
|
51
|
+
}
|
|
52
|
+
return stream.writeSSE({ data: String(data) });
|
|
53
|
+
},
|
|
54
|
+
writeSSE: stream.writeSSE.bind(stream),
|
|
55
|
+
onAbort: stream.onAbort.bind(stream),
|
|
56
|
+
close: stream.close?.bind(stream) ?? (() => { }),
|
|
57
|
+
};
|
|
58
|
+
const runInContext = async () => {
|
|
59
|
+
await handler(c, wrappedStream);
|
|
60
|
+
};
|
|
61
|
+
if (capturedContext) {
|
|
62
|
+
await asyncLocalStorage.run(capturedContext, runInContext);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
await runInContext();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/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AE,GAAE,CAAC,CAAC;aAC/C,CAAC;YAEF,MAAM,YAAY,GAAG,KAAK,IAAI,EAAE;gBAC/B,MAAM,OAAO,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;YACjC,CAAC,CAAC;YAEF,IAAI,eAAe,EAAE,CAAC;gBACrB,MAAM,iBAAiB,CAAC,GAAG,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;YAC5D,CAAC;iBAAM,CAAC;gBACP,MAAM,YAAY,EAAE,CAAC;YACtB,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Context, Handler } from 'hono';
|
|
2
|
+
import type { Env } from '../app';
|
|
3
|
+
/**
|
|
4
|
+
* Handler function for streaming responses.
|
|
5
|
+
* Returns a ReadableStream that will be piped to the response.
|
|
6
|
+
*/
|
|
7
|
+
export type StreamHandler<E extends Env = Env> = (c: Context<E>) => ReadableStream<Uint8Array | string> | Promise<ReadableStream<Uint8Array | string>>;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a streaming middleware for returning ReadableStream responses.
|
|
10
|
+
*
|
|
11
|
+
* Use with router.post() (or any HTTP method) to create a streaming endpoint:
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { createRouter, stream } from '@agentuity/runtime';
|
|
16
|
+
*
|
|
17
|
+
* const router = createRouter();
|
|
18
|
+
*
|
|
19
|
+
* router.post('/events', stream((c) => {
|
|
20
|
+
* return new ReadableStream({
|
|
21
|
+
* start(controller) {
|
|
22
|
+
* controller.enqueue('event 1\n');
|
|
23
|
+
* controller.enqueue('event 2\n');
|
|
24
|
+
* controller.close();
|
|
25
|
+
* }
|
|
26
|
+
* });
|
|
27
|
+
* }));
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* // Async stream with data from request body
|
|
33
|
+
* router.post('/process', stream(async (c) => {
|
|
34
|
+
* const body = await c.req.json();
|
|
35
|
+
*
|
|
36
|
+
* return new ReadableStream({
|
|
37
|
+
* async start(controller) {
|
|
38
|
+
* for (const item of body.items) {
|
|
39
|
+
* controller.enqueue(`Processing: ${item}\n`);
|
|
40
|
+
* await new Promise(r => setTimeout(r, 100));
|
|
41
|
+
* }
|
|
42
|
+
* controller.close();
|
|
43
|
+
* }
|
|
44
|
+
* });
|
|
45
|
+
* }));
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @param handler - Handler function returning a ReadableStream
|
|
49
|
+
* @returns Hono handler for streaming response
|
|
50
|
+
*/
|
|
51
|
+
export declare function stream<E extends Env = Env>(handler: StreamHandler<E>): Handler<E>;
|
|
52
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/handlers/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAG7C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAElC;;;GAGG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI,CAChD,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,KACT,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC,UAAU,GAAG,MAAM,CAAC,CAAC,CAAC;AAExF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CA6BjF"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { stream as honoStream } from 'hono/streaming';
|
|
2
|
+
import { getAgentAsyncLocalStorage } from '../_context';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a streaming middleware for returning ReadableStream responses.
|
|
5
|
+
*
|
|
6
|
+
* Use with router.post() (or any HTTP method) to create a streaming endpoint:
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { createRouter, stream } from '@agentuity/runtime';
|
|
11
|
+
*
|
|
12
|
+
* const router = createRouter();
|
|
13
|
+
*
|
|
14
|
+
* router.post('/events', stream((c) => {
|
|
15
|
+
* return new ReadableStream({
|
|
16
|
+
* start(controller) {
|
|
17
|
+
* controller.enqueue('event 1\n');
|
|
18
|
+
* controller.enqueue('event 2\n');
|
|
19
|
+
* controller.close();
|
|
20
|
+
* }
|
|
21
|
+
* });
|
|
22
|
+
* }));
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // Async stream with data from request body
|
|
28
|
+
* router.post('/process', stream(async (c) => {
|
|
29
|
+
* const body = await c.req.json();
|
|
30
|
+
*
|
|
31
|
+
* return new ReadableStream({
|
|
32
|
+
* async start(controller) {
|
|
33
|
+
* for (const item of body.items) {
|
|
34
|
+
* controller.enqueue(`Processing: ${item}\n`);
|
|
35
|
+
* await new Promise(r => setTimeout(r, 100));
|
|
36
|
+
* }
|
|
37
|
+
* controller.close();
|
|
38
|
+
* }
|
|
39
|
+
* });
|
|
40
|
+
* }));
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @param handler - Handler function returning a ReadableStream
|
|
44
|
+
* @returns Hono handler for streaming response
|
|
45
|
+
*/
|
|
46
|
+
export function stream(handler) {
|
|
47
|
+
return (c) => {
|
|
48
|
+
const asyncLocalStorage = getAgentAsyncLocalStorage();
|
|
49
|
+
const capturedContext = asyncLocalStorage.getStore();
|
|
50
|
+
c.header('Content-Type', 'application/octet-stream');
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
+
return honoStream(c, async (s) => {
|
|
53
|
+
const runInContext = async () => {
|
|
54
|
+
try {
|
|
55
|
+
let streamResult = handler(c);
|
|
56
|
+
if (streamResult instanceof Promise) {
|
|
57
|
+
streamResult = await streamResult;
|
|
58
|
+
}
|
|
59
|
+
await s.pipe(streamResult);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
c.var.logger?.error('Stream error:', err);
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
if (capturedContext) {
|
|
67
|
+
await asyncLocalStorage.run(capturedContext, runInContext);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
await runInContext();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/handlers/stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAWxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAM,UAAU,MAAM,CAAsB,OAAyB;IACpE,OAAO,CAAC,CAAa,EAAE,EAAE;QACxB,MAAM,iBAAiB,GAAG,yBAAyB,EAAE,CAAC;QACtD,MAAM,eAAe,GAAG,iBAAiB,CAAC,QAAQ,EAAE,CAAC;QAErD,CAAC,CAAC,MAAM,CAAC,cAAc,EAAE,0BAA0B,CAAC,CAAC;QAErD,8DAA8D;QAC9D,OAAO,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,CAAM,EAAE,EAAE;YACrC,MAAM,YAAY,GAAG,KAAK,IAAI,EAAE;gBAC/B,IAAI,CAAC;oBACJ,IAAI,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;oBAC9B,IAAI,YAAY,YAAY,OAAO,EAAE,CAAC;wBACrC,YAAY,GAAG,MAAM,YAAY,CAAC;oBACnC,CAAC;oBACD,MAAM,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;gBAC5B,CAAC;gBAAC,OAAO,GAAG,EAAE,CAAC;oBACd,CAAC,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;oBAC1C,MAAM,GAAG,CAAC;gBACX,CAAC;YACF,CAAC,CAAC;YAEF,IAAI,eAAe,EAAE,CAAC;gBACrB,MAAM,iBAAiB,CAAC,GAAG,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC;YAC5D,CAAC;iBAAM,CAAC;gBACP,MAAM,YAAY,EAAE,CAAC;YACtB,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Context, MiddlewareHandler } from 'hono';
|
|
2
|
+
import type { Env } from '../app';
|
|
3
|
+
/**
|
|
4
|
+
* WebSocket connection interface for handling WebSocket events.
|
|
5
|
+
*/
|
|
6
|
+
export interface WebSocketConnection {
|
|
7
|
+
onOpen: (handler: (event: Event) => void | Promise<void>) => void;
|
|
8
|
+
onMessage: (handler: (event: MessageEvent) => void | Promise<void>) => void;
|
|
9
|
+
onClose: (handler: (event: CloseEvent) => void | Promise<void>) => void;
|
|
10
|
+
send: (data: string | ArrayBuffer | Uint8Array) => void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Handler function for WebSocket connections.
|
|
14
|
+
* Receives the Hono context and WebSocket connection with a flattened signature.
|
|
15
|
+
*/
|
|
16
|
+
export type WebSocketHandler<E extends Env = Env> = (c: Context<E>, ws: WebSocketConnection) => void | Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a WebSocket middleware for handling WebSocket connections.
|
|
19
|
+
*
|
|
20
|
+
* Use with router.get() to create a WebSocket endpoint:
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { createRouter, websocket } from '@agentuity/runtime';
|
|
25
|
+
*
|
|
26
|
+
* const router = createRouter();
|
|
27
|
+
*
|
|
28
|
+
* router.get('/ws', websocket((c, ws) => {
|
|
29
|
+
* ws.onOpen(() => {
|
|
30
|
+
* c.var.logger.info('WebSocket opened');
|
|
31
|
+
* ws.send('Welcome!');
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* ws.onMessage((event) => {
|
|
35
|
+
* c.var.logger.info('Received:', event.data);
|
|
36
|
+
* ws.send('Echo: ' + event.data);
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* ws.onClose(() => {
|
|
40
|
+
* c.var.logger.info('WebSocket closed');
|
|
41
|
+
* });
|
|
42
|
+
* }));
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @param handler - Handler function receiving context and WebSocket connection
|
|
46
|
+
* @returns Hono middleware handler for WebSocket upgrade
|
|
47
|
+
*/
|
|
48
|
+
export declare function websocket<E extends Env = Env>(handler: WebSocketHandler<E>): MiddlewareHandler<E>;
|
|
49
|
+
//# sourceMappingURL=websocket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../src/handlers/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAGvD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAElC;;GAEG;AACH,MAAM,WAAW,mBAAmB;IACnC,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC;IAClE,SAAS,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC;IAC5E,OAAO,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC;IACxE,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,UAAU,KAAK,IAAI,CAAC;CACxD;AAED;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,IAAI,CACnD,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,EACb,EAAE,EAAE,mBAAmB,KACnB,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,GAAG,GAAG,GAAG,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAiGjG"}
|