@agentick/server 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/dist/sse.js ADDED
@@ -0,0 +1,137 @@
1
+ /**
2
+ * SSE (Server-Sent Events) utilities.
3
+ *
4
+ * Provides helpers for streaming events to clients via SSE.
5
+ * Works with any web framework that supports writable streams.
6
+ *
7
+ * @module @agentick/server/sse
8
+ */
9
+ /**
10
+ * Create an SSE writer for a response stream.
11
+ *
12
+ * Works with any writable stream that has write() and end() methods
13
+ * (Node.js response, Express response, etc.).
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // Express
18
+ * app.get('/events', (req, res) => {
19
+ * setSSEHeaders(res);
20
+ * const writer = createSSEWriter(res);
21
+ *
22
+ * // Write events
23
+ * writer.writeEvent({
24
+ * channel: 'session:events',
25
+ * type: 'content_delta',
26
+ * payload: { delta: 'Hello' },
27
+ * });
28
+ *
29
+ * // Close when done
30
+ * writer.close();
31
+ * });
32
+ * ```
33
+ */
34
+ export function createSSEWriter(stream, options = {}) {
35
+ const keepaliveInterval = options.keepaliveInterval ?? 15000;
36
+ const eventName = options.eventName ?? "message";
37
+ let closed = false;
38
+ let keepaliveTimer;
39
+ // Start keepalive
40
+ if (keepaliveInterval > 0) {
41
+ keepaliveTimer = setInterval(() => {
42
+ if (!closed) {
43
+ stream.write(": keepalive\n\n");
44
+ }
45
+ }, keepaliveInterval);
46
+ }
47
+ return {
48
+ writeEvent(event) {
49
+ if (closed)
50
+ return;
51
+ let data;
52
+ try {
53
+ data = JSON.stringify(event);
54
+ }
55
+ catch (err) {
56
+ // JSON.stringify can throw on circular references, BigInt, etc.
57
+ // Fall back to a serializable error event
58
+ console.error("SSE: Failed to serialize event", err, event);
59
+ data = JSON.stringify({
60
+ type: "error",
61
+ code: "SERIALIZATION_ERROR",
62
+ message: `Failed to serialize event: ${err.message}`,
63
+ });
64
+ }
65
+ stream.write(`event: ${eventName}\n`);
66
+ stream.write(`data: ${data}\n\n`);
67
+ },
68
+ writeComment(comment) {
69
+ if (closed)
70
+ return;
71
+ stream.write(`: ${comment}\n\n`);
72
+ },
73
+ close() {
74
+ if (closed)
75
+ return;
76
+ closed = true;
77
+ if (keepaliveTimer) {
78
+ clearInterval(keepaliveTimer);
79
+ }
80
+ stream.end();
81
+ },
82
+ get closed() {
83
+ return closed;
84
+ },
85
+ };
86
+ }
87
+ /**
88
+ * Stream an async iterable to SSE.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * app.get('/sessions/:id/stream', async (req, res) => {
93
+ * setSSEHeaders(res);
94
+ *
95
+ * const stream = sessionHandler.stream(req.params.id, {});
96
+ * await streamToSSE(res, stream, 'session:events');
97
+ * });
98
+ * ```
99
+ */
100
+ export async function streamToSSE(stream, events, channel, options = {}) {
101
+ const writer = createSSEWriter(stream, options);
102
+ try {
103
+ for await (const event of events) {
104
+ writer.writeEvent({
105
+ channel,
106
+ type: event.type ?? "event",
107
+ payload: event,
108
+ });
109
+ }
110
+ }
111
+ finally {
112
+ writer.close();
113
+ }
114
+ }
115
+ /**
116
+ * Set SSE headers on a response.
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * app.get('/events', (req, res) => {
121
+ * setSSEHeaders(res);
122
+ * // ... write events
123
+ * });
124
+ * ```
125
+ */
126
+ export function setSSEHeaders(res) {
127
+ res.setHeader("Content-Type", "text/event-stream");
128
+ res.setHeader("Cache-Control", "no-cache");
129
+ res.setHeader("Connection", "keep-alive");
130
+ res.setHeader("X-Accel-Buffering", "no"); // Disable nginx buffering
131
+ // Flush headers immediately so EventSource 'open' event fires
132
+ // Without this, Node.js/Express may buffer and delay the response
133
+ if (res.flushHeaders) {
134
+ res.flushHeaders();
135
+ }
136
+ }
137
+ //# sourceMappingURL=sse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;;;;;;;;;;;;;;;;;;;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CAAC;AACH,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Server Types for @agentick/server
3
+ *
4
+ * @module @agentick/server/types
5
+ */
6
+ export type { ChannelEvent, ChannelEventMetadata, ConnectionMetadata, ProtocolError, SessionResultPayload, ToolConfirmationRequest, ToolConfirmationResponse, SessionState, CreateSessionResponse, } from "@agentick/shared";
7
+ export { FrameworkChannels, ErrorCodes } from "@agentick/shared";
8
+ /**
9
+ * SSE writer interface.
10
+ */
11
+ export interface SSEWriter {
12
+ /** Write an SSE event payload */
13
+ writeEvent(event: unknown): void;
14
+ /** Write a comment (keepalive) */
15
+ writeComment(comment: string): void;
16
+ /** Close the stream */
17
+ close(): void;
18
+ /** Check if closed */
19
+ readonly closed: boolean;
20
+ }
21
+ /**
22
+ * SSE writer options.
23
+ */
24
+ export interface SSEWriterOptions {
25
+ /** Keepalive interval in ms (default: 15000) */
26
+ keepaliveInterval?: number;
27
+ /** Event name for SSE (default: 'message') */
28
+ eventName?: string;
29
+ }
30
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,YAAY,EACV,YAAY,EACZ,oBAAoB,EACpB,kBAAkB,EAClB,aAAa,EACb,oBAAoB,EACpB,uBAAuB,EACvB,wBAAwB,EACxB,YAAY,EACZ,qBAAqB,GACtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAMjE;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,iCAAiC;IACjC,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IACjC,kCAAkC;IAClC,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACpC,uBAAuB;IACvB,KAAK,IAAI,IAAI,CAAC;IACd,sBAAsB;IACtB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,gDAAgD;IAChD,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,8CAA8C;IAC9C,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB"}
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Server Types for @agentick/server
3
+ *
4
+ * @module @agentick/server/types
5
+ */
6
+ export { FrameworkChannels, ErrorCodes } from "@agentick/shared";
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAgBH,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@agentick/server",
3
+ "version": "0.0.1",
4
+ "description": "Server SDK for Agentick - channel routing, session handling, transport adapters",
5
+ "keywords": [
6
+ "agent",
7
+ "ai",
8
+ "channels",
9
+ "server",
10
+ "sse",
11
+ "websocket"
12
+ ],
13
+ "license": "ISC",
14
+ "author": "Ryan Lindgren",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "type": "module",
19
+ "main": "./dist/index.js",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ }
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "dependencies": {
30
+ "@agentick/kernel": "0.0.1",
31
+ "@agentick/shared": "0.0.1"
32
+ },
33
+ "devDependencies": {
34
+ "typescript": "^5.8.3"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc -p tsconfig.build.json",
38
+ "test": "echo \"Tests run from workspace root\"",
39
+ "typecheck": "tsc -p tsconfig.build.json --noEmit",
40
+ "lint": "oxlint src/",
41
+ "format:check": "oxfmt --check src/",
42
+ "clean": "rm -rf dist",
43
+ "dev": "tsc --watch"
44
+ },
45
+ "types": "./dist/index.d.ts"
46
+ }