@agentuity/server 0.0.3 → 0.0.5

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.
Files changed (3) hide show
  1. package/AGENTS.md +124 -0
  2. package/README.md +218 -0
  3. package/package.json +2 -1
package/AGENTS.md ADDED
@@ -0,0 +1,124 @@
1
+ # Agent Guidelines for @agentuity/server
2
+
3
+ ## Package Overview
4
+
5
+ Server runtime for building Agentuity applications. Built on Hono framework and optimized for Bun runtime with OpenTelemetry observability.
6
+
7
+ ## Commands
8
+
9
+ - **Build**: `bun run build` (compiles for Bun target)
10
+ - **Typecheck**: `bun run typecheck` (runs TypeScript type checking)
11
+ - **Clean**: `bun run clean` (removes dist/)
12
+
13
+ ## Architecture
14
+
15
+ - **Runtime**: Bun server runtime
16
+ - **Framework**: Hono (lightweight web framework)
17
+ - **Build target**: Bun runtime
18
+ - **Dependencies**: `@agentuity/core`, Hono, OpenTelemetry
19
+ - **Observability**: Built-in OpenTelemetry for logs, traces, and metrics
20
+
21
+ ## Structure
22
+
23
+ ```
24
+ src/
25
+ ├── index.ts # Main exports
26
+ ├── app.ts # createApp() function
27
+ ├── agent.ts # Agent types and defineAgent()
28
+ ├── router.ts # createRouter() with extended methods
29
+ ├── logger.ts # Logging utilities
30
+ ├── _server.ts # Internal server creation
31
+ ├── _context.ts # Internal context management
32
+ └── _util.ts # Internal utilities
33
+ ```
34
+
35
+ ## Code Style
36
+
37
+ - **Hono patterns** - Follow Hono's context-based API design
38
+ - **Type safety** - Extensive use of TypeScript generics
39
+ - **Middleware pattern** - Support Hono middleware
40
+ - **Async handlers** - All handlers can be async
41
+ - **OpenTelemetry** - Use tracer/logger from context
42
+
43
+ ## Important Conventions
44
+
45
+ - **Agent context** - Every agent handler receives `AgentContext` as first parameter
46
+ - **Schema validation** - Support StandardSchemaV1 (works with Zod, Valibot, etc.)
47
+ - **Streaming support** - Agents can return ReadableStream for streaming responses
48
+ - **WebSocket support** - Use `router.websocket()` for WebSocket routes
49
+ - **SSE support** - Use `router.sse()` for Server-Sent Events
50
+ - **Session tracking** - Each request gets unique sessionId
51
+ - **Storage abstractions** - Provide kv, objectstore, stream, vector interfaces
52
+
53
+ ## Agent Definition Pattern
54
+
55
+ ```typescript
56
+ import { defineAgent } from '@agentuity/server';
57
+ import { z } from 'zod';
58
+
59
+ export default defineAgent({
60
+ metadata: {
61
+ id: 'unique-id',
62
+ identifier: 'folder-name',
63
+ name: 'Human Name',
64
+ description: 'What it does',
65
+ filename: __filename,
66
+ version: 'hash-or-version'
67
+ },
68
+ inputSchema: z.object({ /* ... */ }),
69
+ outputSchema: z.object({ /* ... */ }),
70
+ handler: async (ctx, input) => {
71
+ // ctx.logger, ctx.tracer, ctx.kv, etc.
72
+ return output;
73
+ }
74
+ });
75
+ ```
76
+
77
+ ## Router Extensions
78
+
79
+ The `createRouter()` function returns an extended Hono instance with:
80
+
81
+ - **Standard HTTP methods**: `get`, `post`, `put`, `delete`, `patch`
82
+ - **Streaming**: `stream(path, handler)` - Returns ReadableStream
83
+ - **WebSocket**: `websocket(path, handler)` - WebSocket connections
84
+ - **SSE**: `sse(path, handler)` - Server-Sent Events
85
+
86
+ ## AgentContext API
87
+
88
+ Every agent handler receives:
89
+
90
+ ```typescript
91
+ interface AgentContext {
92
+ logger: Logger; // Structured logger
93
+ tracer: Tracer; // OpenTelemetry tracer
94
+ sessionId: string; // Unique session ID
95
+ kv: KeyValueStorage; // Key-value storage
96
+ objectstore: ObjectStorage; // Object storage
97
+ stream: StreamStorage; // Stream storage
98
+ vector: VectorStorage; // Vector storage
99
+ agent: AgentRegistry; // Access other agents
100
+ waitUntil: (promise) => void; // Background tasks
101
+ }
102
+ ```
103
+
104
+ ## Observability
105
+
106
+ - **Logging**: Use `ctx.logger.info/warn/error()` not console.log
107
+ - **Tracing**: Create spans with `ctx.tracer.startSpan()`
108
+ - **Metrics**: Access via `c.var.meter` in Hono context
109
+ - **Environment**: Metrics/traces sent to OTLP endpoints
110
+
111
+ ## Testing
112
+
113
+ - Use Bun's test runner: `bun test`
114
+ - Mock storage interfaces (kv, objectstore, etc.)
115
+ - Test agent handlers with mock context
116
+ - Use Hono's testing utilities for routes
117
+
118
+ ## Publishing Checklist
119
+
120
+ 1. Run `bun run build` to compile for Bun runtime
121
+ 2. Verify OpenTelemetry dependencies are correct versions
122
+ 3. Test with real Hono server
123
+ 4. Must publish **after** @agentuity/core
124
+ 5. Ensure React is only in devDependencies (for type checking web components)
package/README.md ADDED
@@ -0,0 +1,218 @@
1
+ # @agentuity/server
2
+
3
+ Server runtime for building Agentuity applications with Bun and Hono.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @agentuity/server
9
+ ```
10
+
11
+ ## Overview
12
+
13
+ `@agentuity/server` provides the server-side runtime for Agentuity applications. Built on [Hono](https://hono.dev/) and optimized for [Bun](https://bun.sh/), it enables you to create type-safe agents with automatic routing, validation, and observability.
14
+
15
+ ## Quick Start
16
+
17
+ ### Creating an Application
18
+
19
+ ```typescript
20
+ import { createApp } from '@agentuity/server';
21
+
22
+ const { app, server, logger } = createApp();
23
+
24
+ // Start the server
25
+ server.listen(3000);
26
+ logger.info('Server running on http://localhost:3000');
27
+ ```
28
+
29
+ ### Defining an Agent
30
+
31
+ ```typescript
32
+ import { type AgentContext, createAgent } from '@agentuity/server';
33
+ import { z } from 'zod';
34
+
35
+ const agent = createAgent({
36
+ schema: {
37
+ input: z.object({
38
+ message: z.string()
39
+ }),
40
+ output: z.object({
41
+ response: z.string()
42
+ }),
43
+ },
44
+ handler: async (ctx: AgentContext, input) => {
45
+ ctx.logger.info('Processing message:', input.message);
46
+ return { response: `You said: ${input.message}` };
47
+ },
48
+ });
49
+
50
+ export default agent;
51
+ ```
52
+
53
+ ### Creating Custom Routes
54
+
55
+ ```typescript
56
+ import { createRouter } from '@agentuity/server';
57
+
58
+ const router = createRouter();
59
+
60
+ router.get('/hello', (c) => {
61
+ return c.json({ message: 'Hello, world!' });
62
+ });
63
+
64
+ router.post('/data', async (c) => {
65
+ const body = await c.req.json();
66
+ return c.json({ received: body });
67
+ });
68
+
69
+ export default router;
70
+ ```
71
+
72
+ ### Streaming Responses
73
+
74
+ ```typescript
75
+ router.stream('/events', async (c) => {
76
+ return new ReadableStream({
77
+ start(controller) {
78
+ controller.enqueue('Event 1\n');
79
+ controller.enqueue('Event 2\n');
80
+ controller.close();
81
+ }
82
+ });
83
+ });
84
+ ```
85
+
86
+ ### WebSocket Support
87
+
88
+ ```typescript
89
+ router.websocket('/chat', (c) => (ws) => {
90
+ ws.onOpen(() => {
91
+ console.log('Client connected');
92
+ });
93
+
94
+ ws.onMessage((event) => {
95
+ const data = JSON.parse(event.data);
96
+ ws.send(JSON.stringify({ echo: data }));
97
+ });
98
+
99
+ ws.onClose(() => {
100
+ console.log('Client disconnected');
101
+ });
102
+ });
103
+ ```
104
+
105
+ ### Server-Sent Events (SSE)
106
+
107
+ ```typescript
108
+ router.sse('/updates', (c) => async (stream) => {
109
+ for (let i = 0; i < 10; i++) {
110
+ await stream.writeSSE({
111
+ data: JSON.stringify({ count: i }),
112
+ event: 'update'
113
+ });
114
+ await stream.sleep(1000);
115
+ }
116
+ });
117
+ ```
118
+
119
+ ## API Reference
120
+
121
+ ### createApp(config?)
122
+
123
+ Creates a new Agentuity application instance.
124
+
125
+ **Returns:**
126
+ - `app` - Hono application instance
127
+ - `server` - Server instance with `listen()` method
128
+ - `logger` - Structured logger
129
+
130
+ ### createAgent(config)
131
+
132
+ Creates a type-safe agent with input/output validation.
133
+
134
+ **Config:**
135
+ - `schema.input?` - Schema for input validation (Zod, Valibot, etc.)
136
+ - `schema.output?` - Schema for output validation
137
+ - `handler` - Agent handler function `(ctx: AgentContext, input) => output`
138
+
139
+ ### createRouter()
140
+
141
+ Creates a new router for defining custom API routes.
142
+
143
+ **Methods:**
144
+ - `get/post/put/delete/patch` - HTTP method handlers
145
+ - `stream(path, handler)` - Streaming response handler
146
+ - `websocket(path, handler)` - WebSocket handler
147
+ - `sse(path, handler)` - Server-Sent Events handler
148
+
149
+ ### AgentContext
150
+
151
+ Context object available in agent handlers:
152
+
153
+ ```typescript
154
+ interface AgentContext {
155
+ logger: Logger; // Structured logger
156
+ tracer: Tracer; // OpenTelemetry tracer
157
+ sessionId: string; // Unique session ID
158
+ kv: KeyValueStorage; // Key-value storage
159
+ objectstore: ObjectStorage; // Object storage
160
+ stream: StreamStorage; // Stream storage
161
+ vector: VectorStorage; // Vector storage
162
+ agent: AgentRegistry; // Access to other agents
163
+ waitUntil: (promise) => void; // Defer cleanup tasks
164
+ }
165
+ ```
166
+
167
+ ## Storage Services
168
+
169
+ Agentuity provides built-in storage abstractions:
170
+
171
+ - **KeyValueStorage** - Simple key-value storage
172
+ - **ObjectStorage** - Object/blob storage
173
+ - **StreamStorage** - Streaming data storage
174
+ - **VectorStorage** - Vector embeddings storage
175
+
176
+ Access these via the agent context:
177
+
178
+ ```typescript
179
+ const agent = createAgent({
180
+ schema: {
181
+ output: z.object({ value: z.string().optional() }),
182
+ },
183
+ handler: async (ctx, input) => {
184
+ await ctx.kv.set('key', 'value');
185
+ const value = await ctx.kv.get('key');
186
+ return { value };
187
+ },
188
+ });
189
+ ```
190
+
191
+ ## Observability
192
+
193
+ Built-in OpenTelemetry support for logging, tracing, and metrics:
194
+
195
+ ```typescript
196
+ const agent = createAgent({
197
+ schema: {
198
+ output: z.object({ success: z.boolean() }),
199
+ },
200
+ handler: async (ctx, input) => {
201
+ ctx.logger.info('Processing request');
202
+
203
+ const span = ctx.tracer.startSpan('custom-operation');
204
+ // ... do work ...
205
+ span.end();
206
+
207
+ return { success: true };
208
+ },
209
+ });
210
+ ```
211
+
212
+ ## TypeScript
213
+
214
+ Fully typed with TypeScript. Input and output types are automatically inferred from your schemas.
215
+
216
+ ## License
217
+
218
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentuity/server",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./dist/index.d.ts",
@@ -11,6 +11,7 @@
11
11
  }
12
12
  },
13
13
  "files": [
14
+ "AGENTS.md",
14
15
  "README.md",
15
16
  "src",
16
17
  "dist"