@vibe-agent-toolkit/transports 0.1.2-rc.4
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 +281 -0
- package/dist/cli.d.ts +92 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +250 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +59 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/dist/websocket.d.ts +77 -0
- package/dist/websocket.d.ts.map +1 -0
- package/dist/websocket.js +155 -0
- package/dist/websocket.js.map +1 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# @vibe-agent-toolkit/transports
|
|
2
|
+
|
|
3
|
+
Transport adapters for VAT conversational agents.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Transports connect conversational functions to different interaction channels (CLI, WebSocket, HTTP, etc.) without coupling to specific runtime implementations. This package provides the core transport abstraction and two reference implementations.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @vibe-agent-toolkit/transports
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Core Concepts
|
|
16
|
+
|
|
17
|
+
### Conversational Function
|
|
18
|
+
|
|
19
|
+
A conversational function is any async function that:
|
|
20
|
+
- Takes input and session state
|
|
21
|
+
- Returns output and updated session state
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
type ConversationalFunction<TInput, TOutput, TState> = (
|
|
25
|
+
input: TInput,
|
|
26
|
+
session: Session<TState>
|
|
27
|
+
) => Promise<{
|
|
28
|
+
output: TOutput;
|
|
29
|
+
session: Session<TState>;
|
|
30
|
+
}>;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Session
|
|
34
|
+
|
|
35
|
+
A session contains conversation history and application-specific state:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
type Session<TState> = {
|
|
39
|
+
history: Message[];
|
|
40
|
+
state: TState;
|
|
41
|
+
};
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Transport
|
|
45
|
+
|
|
46
|
+
A transport provides lifecycle management for running conversational functions:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
interface Transport {
|
|
50
|
+
start(): Promise<void>;
|
|
51
|
+
stop(): Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## CLI Transport
|
|
56
|
+
|
|
57
|
+
Interactive command-line interface with built-in commands and conversation history.
|
|
58
|
+
|
|
59
|
+
### Features
|
|
60
|
+
|
|
61
|
+
- Local session management (single user)
|
|
62
|
+
- Built-in commands: `/quit`, `/state`, `/restart`, `/help`
|
|
63
|
+
- Optional colored output
|
|
64
|
+
- Configurable prompts
|
|
65
|
+
|
|
66
|
+
### Usage
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { CLITransport } from '@vibe-agent-toolkit/transports';
|
|
70
|
+
|
|
71
|
+
// Create a simple echo agent
|
|
72
|
+
const echoFn = async (input: string, session: Session<any>) => {
|
|
73
|
+
const newHistory = [
|
|
74
|
+
...session.history,
|
|
75
|
+
{ role: 'user', content: input },
|
|
76
|
+
{ role: 'assistant', content: `Echo: ${input}` },
|
|
77
|
+
];
|
|
78
|
+
return {
|
|
79
|
+
output: `Echo: ${input}`,
|
|
80
|
+
session: { ...session, history: newHistory },
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// Run with CLI transport
|
|
85
|
+
const transport = new CLITransport({
|
|
86
|
+
fn: echoFn,
|
|
87
|
+
showState: true,
|
|
88
|
+
colors: true,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
await transport.start();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Options
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
interface CLITransportOptions<TState> {
|
|
98
|
+
fn: ConversationalFunction<string, string, TState>;
|
|
99
|
+
initialSession?: Session<TState>;
|
|
100
|
+
colors?: boolean; // Default: true
|
|
101
|
+
showState?: boolean; // Default: false
|
|
102
|
+
prompt?: string; // Default: "You: "
|
|
103
|
+
assistantPrefix?: string; // Default: "Assistant: "
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Built-in Commands
|
|
108
|
+
|
|
109
|
+
- `/help` - Show available commands
|
|
110
|
+
- `/state` - Display current session state
|
|
111
|
+
- `/restart` - Clear history and reset state
|
|
112
|
+
- `/quit` - Exit the CLI
|
|
113
|
+
|
|
114
|
+
## WebSocket Transport
|
|
115
|
+
|
|
116
|
+
Real-time bidirectional communication with per-connection session isolation.
|
|
117
|
+
|
|
118
|
+
### Features
|
|
119
|
+
|
|
120
|
+
- Per-connection session management
|
|
121
|
+
- JSON message format
|
|
122
|
+
- Automatic session cleanup on disconnect
|
|
123
|
+
- Configurable host and port
|
|
124
|
+
|
|
125
|
+
### Usage
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
import { WebSocketTransport } from '@vibe-agent-toolkit/transports';
|
|
129
|
+
|
|
130
|
+
// Create a stateful counter agent
|
|
131
|
+
const counterFn = async (input: string, session: Session<{ count: number }>) => {
|
|
132
|
+
const count = (session.state?.count ?? 0) + 1;
|
|
133
|
+
const newHistory = [
|
|
134
|
+
...session.history,
|
|
135
|
+
{ role: 'user', content: input },
|
|
136
|
+
{ role: 'assistant', content: `Message #${count}` },
|
|
137
|
+
];
|
|
138
|
+
return {
|
|
139
|
+
output: `Message #${count}`,
|
|
140
|
+
session: { history: newHistory, state: { count } },
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// Run with WebSocket transport
|
|
145
|
+
const transport = new WebSocketTransport({
|
|
146
|
+
fn: counterFn,
|
|
147
|
+
port: 8080,
|
|
148
|
+
createInitialSession: () => ({ history: [], state: { count: 0 } }),
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
await transport.start();
|
|
152
|
+
// Server listening on ws://localhost:8080
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Options
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
interface WebSocketTransportOptions<TState> {
|
|
159
|
+
fn: ConversationalFunction<string, string, TState>;
|
|
160
|
+
port?: number; // Default: 8080
|
|
161
|
+
host?: string; // Default: 'localhost'
|
|
162
|
+
createInitialSession?: () => Session<TState>;
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Message Format
|
|
167
|
+
|
|
168
|
+
**Client → Server:**
|
|
169
|
+
```json
|
|
170
|
+
{
|
|
171
|
+
"type": "message",
|
|
172
|
+
"content": "Hello"
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
**Server → Client (success):**
|
|
177
|
+
```json
|
|
178
|
+
{
|
|
179
|
+
"type": "message",
|
|
180
|
+
"reply": "Response text",
|
|
181
|
+
"state": { "count": 1 }
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**Server → Client (error):**
|
|
186
|
+
```json
|
|
187
|
+
{
|
|
188
|
+
"type": "error",
|
|
189
|
+
"error": "Error message"
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Client Example
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { WebSocket } from 'ws';
|
|
197
|
+
|
|
198
|
+
const ws = new WebSocket('ws://localhost:8080');
|
|
199
|
+
|
|
200
|
+
ws.on('open', () => {
|
|
201
|
+
ws.send(JSON.stringify({
|
|
202
|
+
type: 'message',
|
|
203
|
+
content: 'Hello, agent!'
|
|
204
|
+
}));
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
ws.on('message', (data) => {
|
|
208
|
+
const response = JSON.parse(data.toString());
|
|
209
|
+
console.log('Reply:', response.reply);
|
|
210
|
+
console.log('State:', response.state);
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Using with Runtime Adapters
|
|
215
|
+
|
|
216
|
+
Transports work with any runtime adapter that implements the conversational function signature:
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
import { CLITransport } from '@vibe-agent-toolkit/transports';
|
|
220
|
+
import { createClaudeSkillAdapter } from '@vibe-agent-toolkit/runtime-claude-skills';
|
|
221
|
+
|
|
222
|
+
// Create adapter from Claude skill
|
|
223
|
+
const adapter = createClaudeSkillAdapter({
|
|
224
|
+
skillDir: './my-skill',
|
|
225
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// Run with CLI transport
|
|
229
|
+
const transport = new CLITransport({
|
|
230
|
+
fn: adapter.conversationalFn,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
await transport.start();
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Architecture
|
|
237
|
+
|
|
238
|
+
### No Session IDs in Core Types
|
|
239
|
+
|
|
240
|
+
The transport abstraction intentionally excludes session IDs from core types. Session management is transport-specific:
|
|
241
|
+
|
|
242
|
+
- **CLI Transport**: Single local session (no ID needed)
|
|
243
|
+
- **WebSocket Transport**: Per-connection sessions (WeakMap keyed by socket)
|
|
244
|
+
- **HTTP Transport** (future): Session IDs in cookies/headers
|
|
245
|
+
|
|
246
|
+
This design keeps the core types simple while allowing each transport to implement session management appropriately.
|
|
247
|
+
|
|
248
|
+
### Transport Independence
|
|
249
|
+
|
|
250
|
+
Transports are independent of runtime implementations. The same conversational function can run on any transport:
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
// Same function, different transports
|
|
254
|
+
const myFn = createMyAgent();
|
|
255
|
+
|
|
256
|
+
// CLI
|
|
257
|
+
const cliTransport = new CLITransport({ fn: myFn });
|
|
258
|
+
|
|
259
|
+
// WebSocket
|
|
260
|
+
const wsTransport = new WebSocketTransport({ fn: myFn });
|
|
261
|
+
|
|
262
|
+
// HTTP (future)
|
|
263
|
+
const httpTransport = new HTTPTransport({ fn: myFn });
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## TypeScript
|
|
267
|
+
|
|
268
|
+
This package is written in TypeScript and provides full type definitions.
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
import type {
|
|
272
|
+
Transport,
|
|
273
|
+
ConversationalFunction,
|
|
274
|
+
Session,
|
|
275
|
+
Message,
|
|
276
|
+
} from '@vibe-agent-toolkit/transports';
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## License
|
|
280
|
+
|
|
281
|
+
MIT
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI transport for conversational agents.
|
|
3
|
+
*
|
|
4
|
+
* Provides an interactive command-line interface with:
|
|
5
|
+
* - Conversation history
|
|
6
|
+
* - Local session state
|
|
7
|
+
* - Built-in commands (/quit, /state, /restart)
|
|
8
|
+
* - Optional colored output
|
|
9
|
+
*/
|
|
10
|
+
import type { Message, SessionStore } from '@vibe-agent-toolkit/agent-runtime';
|
|
11
|
+
import type { ConversationalFunction, Transport } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Options for CLI transport.
|
|
14
|
+
*/
|
|
15
|
+
export interface CLITransportOptions<TState = any> {
|
|
16
|
+
/** The conversational function to run */
|
|
17
|
+
fn: ConversationalFunction<string, string, TState>;
|
|
18
|
+
/** Session identifier (default: "cli-singleton") */
|
|
19
|
+
sessionId?: string;
|
|
20
|
+
/** Session store for persistence (optional, enables durable sessions) */
|
|
21
|
+
sessionStore?: SessionStore<TState>;
|
|
22
|
+
/** Initial conversation history (default: [], fallback if session not found) */
|
|
23
|
+
initialHistory?: Message[];
|
|
24
|
+
/** Initial session state (default: undefined, fallback if session not found) */
|
|
25
|
+
initialState?: TState;
|
|
26
|
+
/** Enable colored output (default: true) */
|
|
27
|
+
colors?: boolean;
|
|
28
|
+
/** Show state after each interaction (default: false) */
|
|
29
|
+
showState?: boolean;
|
|
30
|
+
/** Prompt prefix (default: "You: ") */
|
|
31
|
+
prompt?: string;
|
|
32
|
+
/** Assistant prefix (default: "Assistant: ") */
|
|
33
|
+
assistantPrefix?: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* CLI transport implementation.
|
|
37
|
+
*
|
|
38
|
+
* Manages a single local session and provides an interactive REPL.
|
|
39
|
+
* Supports pluggable session stores for persistence.
|
|
40
|
+
*/
|
|
41
|
+
export declare class CLITransport<TState = any> implements Transport {
|
|
42
|
+
private readonly fn;
|
|
43
|
+
private readonly sessionId;
|
|
44
|
+
private readonly sessionStore;
|
|
45
|
+
private sessionLoaded;
|
|
46
|
+
private conversationHistory;
|
|
47
|
+
private state;
|
|
48
|
+
private readonly colors;
|
|
49
|
+
private readonly showState;
|
|
50
|
+
private readonly prompt;
|
|
51
|
+
private readonly assistantPrefix;
|
|
52
|
+
private rl;
|
|
53
|
+
constructor(options: CLITransportOptions<TState>);
|
|
54
|
+
/**
|
|
55
|
+
* Start the CLI transport.
|
|
56
|
+
*/
|
|
57
|
+
start(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Stop the CLI transport.
|
|
60
|
+
*/
|
|
61
|
+
stop(): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Load session from store if available.
|
|
64
|
+
* Falls back to initial history/state if session not found.
|
|
65
|
+
*/
|
|
66
|
+
private loadSessionIfNeeded;
|
|
67
|
+
/**
|
|
68
|
+
* Save current session to store if available.
|
|
69
|
+
*/
|
|
70
|
+
private saveSessionIfNeeded;
|
|
71
|
+
/**
|
|
72
|
+
* Handle built-in commands.
|
|
73
|
+
*/
|
|
74
|
+
private handleCommand;
|
|
75
|
+
/**
|
|
76
|
+
* Print welcome message.
|
|
77
|
+
*/
|
|
78
|
+
private printWelcome;
|
|
79
|
+
/**
|
|
80
|
+
* Print help message.
|
|
81
|
+
*/
|
|
82
|
+
private printHelp;
|
|
83
|
+
/**
|
|
84
|
+
* Print current session state.
|
|
85
|
+
*/
|
|
86
|
+
private printState;
|
|
87
|
+
/**
|
|
88
|
+
* Colorize text if colors are enabled.
|
|
89
|
+
*/
|
|
90
|
+
private colorize;
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,OAAO,EAAkB,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAE/F,OAAO,KAAK,EAAE,sBAAsB,EAAE,SAAS,EAA2B,MAAM,YAAY,CAAC;AAE7F;;GAEG;AAEH,MAAM,WAAW,mBAAmB,CAAC,MAAM,GAAG,GAAG;IAC/C,yCAAyC;IACzC,EAAE,EAAE,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACnD,oDAAoD;IACpD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,yEAAyE;IACzE,YAAY,CAAC,EAAE,YAAY,CAAC,MAAM,CAAC,CAAC;IACpC,gFAAgF;IAChF,cAAc,CAAC,EAAE,OAAO,EAAE,CAAC;IAC3B,gFAAgF;IAChF,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,4CAA4C;IAC5C,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,yDAAyD;IACzD,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,uCAAuC;IACvC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gDAAgD;IAChD,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;GAKG;AAEH,qBAAa,YAAY,CAAC,MAAM,GAAG,GAAG,CAAE,YAAW,SAAS;IAC1D,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAiD;IACpE,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAmC;IAChE,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,mBAAmB,CAAY;IACvC,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAU;IACjC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAU;IACpC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IACzC,OAAO,CAAC,EAAE,CAAmC;gBAEjC,OAAO,EAAE,mBAAmB,CAAC,MAAM,CAAC;IAYhD;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAwE5B;;OAEG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAO3B;;;OAGG;YACW,mBAAmB;IAmBjC;;OAEG;YACW,mBAAmB;IAkCjC;;OAEG;YACW,aAAa;IAuC3B;;OAEG;IACH,OAAO,CAAC,YAAY;IAMpB;;OAEG;IACH,OAAO,CAAC,SAAS;IASjB;;OAEG;IACH,OAAO,CAAC,UAAU;IAQlB;;OAEG;IACH,OAAO,CAAC,QAAQ;CAgBjB"}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI transport for conversational agents.
|
|
3
|
+
*
|
|
4
|
+
* Provides an interactive command-line interface with:
|
|
5
|
+
* - Conversation history
|
|
6
|
+
* - Local session state
|
|
7
|
+
* - Built-in commands (/quit, /state, /restart)
|
|
8
|
+
* - Optional colored output
|
|
9
|
+
*/
|
|
10
|
+
import * as readline from 'node:readline';
|
|
11
|
+
/**
|
|
12
|
+
* CLI transport implementation.
|
|
13
|
+
*
|
|
14
|
+
* Manages a single local session and provides an interactive REPL.
|
|
15
|
+
* Supports pluggable session stores for persistence.
|
|
16
|
+
*/
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
export class CLITransport {
|
|
19
|
+
fn;
|
|
20
|
+
sessionId;
|
|
21
|
+
sessionStore;
|
|
22
|
+
sessionLoaded = false;
|
|
23
|
+
conversationHistory;
|
|
24
|
+
state;
|
|
25
|
+
colors;
|
|
26
|
+
showState;
|
|
27
|
+
prompt;
|
|
28
|
+
assistantPrefix;
|
|
29
|
+
rl = null;
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.fn = options.fn;
|
|
32
|
+
this.sessionId = options.sessionId ?? 'cli-singleton';
|
|
33
|
+
this.sessionStore = options.sessionStore;
|
|
34
|
+
this.conversationHistory = options.initialHistory ?? [];
|
|
35
|
+
this.state = options.initialState ?? undefined;
|
|
36
|
+
this.colors = options.colors ?? true;
|
|
37
|
+
this.showState = options.showState ?? false;
|
|
38
|
+
this.prompt = options.prompt ?? 'You: ';
|
|
39
|
+
this.assistantPrefix = options.assistantPrefix ?? 'Assistant: ';
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Start the CLI transport.
|
|
43
|
+
*/
|
|
44
|
+
async start() {
|
|
45
|
+
// Lazy load session from store if available
|
|
46
|
+
await this.loadSessionIfNeeded();
|
|
47
|
+
this.rl = readline.createInterface({
|
|
48
|
+
input: process.stdin,
|
|
49
|
+
output: process.stdout,
|
|
50
|
+
prompt: this.colorize(this.prompt, 'cyan'),
|
|
51
|
+
});
|
|
52
|
+
this.printWelcome();
|
|
53
|
+
this.rl.on('line', (line) => {
|
|
54
|
+
void (async () => {
|
|
55
|
+
const input = line.trim();
|
|
56
|
+
if (!input) {
|
|
57
|
+
this.rl?.prompt();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// Handle commands
|
|
61
|
+
if (input.startsWith('/')) {
|
|
62
|
+
await this.handleCommand(input);
|
|
63
|
+
this.rl?.prompt();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
// Process user input
|
|
67
|
+
try {
|
|
68
|
+
// Add user message to history
|
|
69
|
+
this.conversationHistory.push({ role: 'user', content: input });
|
|
70
|
+
// Pass session context (just ID and current state)
|
|
71
|
+
const context = {
|
|
72
|
+
sessionId: this.sessionId,
|
|
73
|
+
conversationHistory: this.conversationHistory,
|
|
74
|
+
state: this.state,
|
|
75
|
+
};
|
|
76
|
+
const output = await this.fn(input, context);
|
|
77
|
+
// Update session state (function may have mutated context.state)
|
|
78
|
+
this.state = context.state;
|
|
79
|
+
// Add assistant response to history
|
|
80
|
+
this.conversationHistory.push({ role: 'assistant', content: output });
|
|
81
|
+
console.log(this.colorize(this.assistantPrefix, 'green') + output);
|
|
82
|
+
if (this.showState) {
|
|
83
|
+
this.printState();
|
|
84
|
+
}
|
|
85
|
+
// Save session after successful interaction
|
|
86
|
+
await this.saveSessionIfNeeded();
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
console.error(this.colorize('Error: ', 'red') + (error instanceof Error ? error.message : String(error)));
|
|
90
|
+
}
|
|
91
|
+
this.rl?.prompt();
|
|
92
|
+
})();
|
|
93
|
+
});
|
|
94
|
+
this.rl.on('close', () => {
|
|
95
|
+
console.log(this.colorize('\nGoodbye!', 'yellow'));
|
|
96
|
+
process.exit(0);
|
|
97
|
+
});
|
|
98
|
+
this.rl.prompt();
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Stop the CLI transport.
|
|
102
|
+
*/
|
|
103
|
+
async stop() {
|
|
104
|
+
// Save final session state before stopping
|
|
105
|
+
await this.saveSessionIfNeeded();
|
|
106
|
+
this.rl?.close();
|
|
107
|
+
this.rl = null;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Load session from store if available.
|
|
111
|
+
* Falls back to initial history/state if session not found.
|
|
112
|
+
*/
|
|
113
|
+
async loadSessionIfNeeded() {
|
|
114
|
+
if (this.sessionLoaded || !this.sessionStore) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const session = await this.sessionStore.load(this.sessionId);
|
|
119
|
+
this.conversationHistory = session.history;
|
|
120
|
+
this.state = session.state;
|
|
121
|
+
console.log(this.colorize(`✓ Resumed session: ${this.sessionId}`, 'green'));
|
|
122
|
+
console.log(this.colorize(` ${session.history.length} messages in history`, 'gray'));
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// Session not found or error loading - use initial values
|
|
126
|
+
// This is expected for new sessions
|
|
127
|
+
}
|
|
128
|
+
this.sessionLoaded = true;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Save current session to store if available.
|
|
132
|
+
*/
|
|
133
|
+
async saveSessionIfNeeded() {
|
|
134
|
+
if (!this.sessionStore) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
// Check if session exists first
|
|
139
|
+
const exists = await this.sessionStore.exists(this.sessionId);
|
|
140
|
+
if (exists) {
|
|
141
|
+
// Load existing session to preserve metadata
|
|
142
|
+
const session = await this.sessionStore.load(this.sessionId);
|
|
143
|
+
session.history = this.conversationHistory;
|
|
144
|
+
session.state = this.state;
|
|
145
|
+
await this.sessionStore.save(session);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
// Create new session with our sessionId (not a generated UUID)
|
|
149
|
+
const session = {
|
|
150
|
+
id: this.sessionId,
|
|
151
|
+
history: this.conversationHistory,
|
|
152
|
+
state: this.state,
|
|
153
|
+
metadata: {
|
|
154
|
+
createdAt: new Date(),
|
|
155
|
+
lastAccessedAt: new Date(),
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
await this.sessionStore.save(session);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
// Log error but don't crash - session persistence is optional
|
|
163
|
+
console.error(this.colorize('Warning: Failed to save session: ', 'yellow') + (error instanceof Error ? error.message : String(error)));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Handle built-in commands.
|
|
168
|
+
*/
|
|
169
|
+
async handleCommand(cmd) {
|
|
170
|
+
const command = cmd.toLowerCase();
|
|
171
|
+
switch (command) {
|
|
172
|
+
case '/quit':
|
|
173
|
+
case '/exit':
|
|
174
|
+
await this.stop();
|
|
175
|
+
break;
|
|
176
|
+
case '/state':
|
|
177
|
+
this.printState();
|
|
178
|
+
break;
|
|
179
|
+
case '/clear':
|
|
180
|
+
case '/restart': // Alias for backward compatibility
|
|
181
|
+
// Delete session from store if available
|
|
182
|
+
if (this.sessionStore) {
|
|
183
|
+
try {
|
|
184
|
+
await this.sessionStore.delete(this.sessionId);
|
|
185
|
+
console.log(this.colorize('✓ Session deleted from store', 'green'));
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
console.error(this.colorize('Warning: Failed to delete session: ', 'yellow') + (error instanceof Error ? error.message : String(error)));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
this.conversationHistory = [];
|
|
192
|
+
this.state = undefined;
|
|
193
|
+
console.log(this.colorize('Session cleared.', 'yellow'));
|
|
194
|
+
break;
|
|
195
|
+
case '/help':
|
|
196
|
+
this.printHelp();
|
|
197
|
+
break;
|
|
198
|
+
default:
|
|
199
|
+
console.log(this.colorize(`Unknown command: ${cmd}`, 'red'));
|
|
200
|
+
console.log(this.colorize('Type /help for available commands.', 'yellow'));
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Print welcome message.
|
|
205
|
+
*/
|
|
206
|
+
printWelcome() {
|
|
207
|
+
console.log(this.colorize('=== CLI Transport ===', 'cyan'));
|
|
208
|
+
console.log(this.colorize('Commands: /help /state /clear /quit', 'gray'));
|
|
209
|
+
console.log();
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Print help message.
|
|
213
|
+
*/
|
|
214
|
+
printHelp() {
|
|
215
|
+
console.log(this.colorize('\nAvailable commands:', 'cyan'));
|
|
216
|
+
console.log(this.colorize(' /help ', 'yellow') + '- Show this help message');
|
|
217
|
+
console.log(this.colorize(' /state ', 'yellow') + '- Display current session state');
|
|
218
|
+
console.log(this.colorize(' /clear ', 'yellow') + '- Clear session (delete history and state)');
|
|
219
|
+
console.log(this.colorize(' /quit ', 'yellow') + '- Exit the CLI');
|
|
220
|
+
console.log();
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Print current session state.
|
|
224
|
+
*/
|
|
225
|
+
printState() {
|
|
226
|
+
console.log(this.colorize('\n--- Session State ---', 'cyan'));
|
|
227
|
+
console.log(this.colorize('Session ID: ', 'yellow') + this.sessionId);
|
|
228
|
+
console.log(this.colorize('History length: ', 'yellow') + this.conversationHistory.length);
|
|
229
|
+
console.log(this.colorize('State: ', 'yellow') + JSON.stringify(this.state, null, 2));
|
|
230
|
+
console.log();
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Colorize text if colors are enabled.
|
|
234
|
+
*/
|
|
235
|
+
colorize(text, color) {
|
|
236
|
+
if (!this.colors) {
|
|
237
|
+
return text;
|
|
238
|
+
}
|
|
239
|
+
const colors = {
|
|
240
|
+
cyan: '\x1b[36m',
|
|
241
|
+
green: '\x1b[32m',
|
|
242
|
+
yellow: '\x1b[33m',
|
|
243
|
+
red: '\x1b[31m',
|
|
244
|
+
gray: '\x1b[90m',
|
|
245
|
+
};
|
|
246
|
+
const reset = '\x1b[0m';
|
|
247
|
+
return colors[color] + text + reset;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAC;AA+B1C;;;;;GAKG;AACH,8DAA8D;AAC9D,MAAM,OAAO,YAAY;IACN,EAAE,CAAiD;IACnD,SAAS,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@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vibe-agent-toolkit/transports
|
|
3
|
+
*
|
|
4
|
+
* Transport adapters for VAT conversational agents.
|
|
5
|
+
*
|
|
6
|
+
* Provides implementations for different interaction channels:
|
|
7
|
+
* - CLI: Interactive command-line interface
|
|
8
|
+
* - WebSocket: Real-time bidirectional communication
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
export type { Session, TransportSessionContext, ConversationalFunction, Transport } from './types.js';
|
|
13
|
+
export { CLITransport, type CLITransportOptions } from './cli.js';
|
|
14
|
+
export { WebSocketTransport, type WebSocketTransportOptions, type WebSocketIncomingMessage, type WebSocketOutgoingMessage } from './websocket.js';
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,YAAY,EAAE,OAAO,EAAE,uBAAuB,EAAE,sBAAsB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACtG,OAAO,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,KAAK,yBAAyB,EAAE,KAAK,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vibe-agent-toolkit/transports
|
|
3
|
+
*
|
|
4
|
+
* Transport adapters for VAT conversational agents.
|
|
5
|
+
*
|
|
6
|
+
* Provides implementations for different interaction channels:
|
|
7
|
+
* - CLI: Interactive command-line interface
|
|
8
|
+
* - WebSocket: Real-time bidirectional communication
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
export { CLITransport } from './cli.js';
|
|
13
|
+
export { WebSocketTransport } from './websocket.js';
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAE,YAAY,EAA4B,MAAM,UAAU,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAgG,MAAM,gBAAgB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for VAT transport layer.
|
|
3
|
+
*
|
|
4
|
+
* Transports connect conversational functions to different interaction channels
|
|
5
|
+
* (CLI, WebSocket, HTTP, etc.) without coupling to specific runtime implementations.
|
|
6
|
+
*/
|
|
7
|
+
import type { Message } from '@vibe-agent-toolkit/agent-runtime';
|
|
8
|
+
/**
|
|
9
|
+
* Transport-level session context.
|
|
10
|
+
*
|
|
11
|
+
* Contains session identification only - not storage.
|
|
12
|
+
* Runtime adapters are responsible for loading/saving session data.
|
|
13
|
+
*
|
|
14
|
+
* @template TState - Custom state type (application-specific)
|
|
15
|
+
*/
|
|
16
|
+
export interface TransportSessionContext<TState = unknown> {
|
|
17
|
+
/** Session identifier (opaque to transport) */
|
|
18
|
+
sessionId: string;
|
|
19
|
+
/** Conversation history (loaded by runtime) */
|
|
20
|
+
conversationHistory: Message[];
|
|
21
|
+
/** Application-specific state (loaded by runtime) */
|
|
22
|
+
state: TState;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A conversational function that uses session context.
|
|
26
|
+
*
|
|
27
|
+
* The runtime adapter is responsible for loading/saving session data.
|
|
28
|
+
*
|
|
29
|
+
* @template TInput - Input type (e.g., string for text, object for structured)
|
|
30
|
+
* @template TOutput - Output type (e.g., string for text, object for structured)
|
|
31
|
+
* @template TState - Session state type
|
|
32
|
+
*/
|
|
33
|
+
export type ConversationalFunction<TInput = any, TOutput = any, TState = any> = (input: TInput, context: TransportSessionContext<TState>) => Promise<TOutput>;
|
|
34
|
+
/**
|
|
35
|
+
* DEPRECATED: Old Session type for backward compatibility.
|
|
36
|
+
*
|
|
37
|
+
* Use RuntimeSession from @vibe-agent-toolkit/agent-runtime instead.
|
|
38
|
+
*
|
|
39
|
+
* @deprecated Use TransportSessionContext instead
|
|
40
|
+
* @template TState - Custom state type (application-specific)
|
|
41
|
+
*/
|
|
42
|
+
export type Session<TState = unknown> = {
|
|
43
|
+
/** Conversation history */
|
|
44
|
+
history: Message[];
|
|
45
|
+
/** Application-specific state */
|
|
46
|
+
state: TState;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Transport interface for running conversational agents.
|
|
50
|
+
*
|
|
51
|
+
* Transports handle lifecycle and I/O for different interaction channels.
|
|
52
|
+
*/
|
|
53
|
+
export interface Transport {
|
|
54
|
+
/** Start the transport (e.g., begin listening for connections) */
|
|
55
|
+
start(): Promise<void>;
|
|
56
|
+
/** Stop the transport (e.g., close connections and clean up) */
|
|
57
|
+
stop(): Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mCAAmC,CAAC;AAEjE;;;;;;;GAOG;AACH,MAAM,WAAW,uBAAuB,CAAC,MAAM,GAAG,OAAO;IACvD,+CAA+C;IAC/C,SAAS,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,mBAAmB,EAAE,OAAO,EAAE,CAAC;IAC/B,qDAAqD;IACrD,KAAK,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;GAQG;AAEH,MAAM,MAAM,sBAAsB,CAAC,MAAM,GAAG,GAAG,EAAE,OAAO,GAAG,GAAG,EAAE,MAAM,GAAG,GAAG,IAAI,CAC9E,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,uBAAuB,CAAC,MAAM,CAAC,KACrC,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB;;;;;;;GAOG;AACH,MAAM,MAAM,OAAO,CAAC,MAAM,GAAG,OAAO,IAAI;IACtC,2BAA2B;IAC3B,OAAO,EAAE,OAAO,EAAE,CAAC;IACnB,iCAAiC;IACjC,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB,kEAAkE;IAClE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,gEAAgE;IAChE,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for VAT transport layer.
|
|
3
|
+
*
|
|
4
|
+
* Transports connect conversational functions to different interaction channels
|
|
5
|
+
* (CLI, WebSocket, HTTP, etc.) without coupling to specific runtime implementations.
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket transport for conversational agents.
|
|
3
|
+
*
|
|
4
|
+
* Provides a WebSocket server with:
|
|
5
|
+
* - Per-connection session isolation
|
|
6
|
+
* - JSON message format
|
|
7
|
+
* - Automatic session cleanup on disconnect
|
|
8
|
+
*/
|
|
9
|
+
import type { ConversationalFunction, Transport } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Options for WebSocket transport.
|
|
12
|
+
*/
|
|
13
|
+
export interface WebSocketTransportOptions<TState = any> {
|
|
14
|
+
/** The conversational function to run */
|
|
15
|
+
fn: ConversationalFunction<string, string, TState>;
|
|
16
|
+
/** Port to listen on (default: 8080) */
|
|
17
|
+
port?: number;
|
|
18
|
+
/** Host to bind to (default: 'localhost') */
|
|
19
|
+
host?: string;
|
|
20
|
+
/** Session ID generator (default: crypto.randomUUID) */
|
|
21
|
+
generateSessionId?: () => string;
|
|
22
|
+
/** Factory for initial session state per connection */
|
|
23
|
+
createInitialState?: () => TState;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* WebSocket message format (client → server).
|
|
27
|
+
*/
|
|
28
|
+
export interface WebSocketIncomingMessage {
|
|
29
|
+
type: 'message';
|
|
30
|
+
content: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* WebSocket response format (server → client).
|
|
34
|
+
*/
|
|
35
|
+
export interface WebSocketOutgoingMessage<TState = any> {
|
|
36
|
+
type: 'message' | 'error';
|
|
37
|
+
reply?: string;
|
|
38
|
+
state?: TState;
|
|
39
|
+
error?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* WebSocket transport implementation.
|
|
43
|
+
*
|
|
44
|
+
* Each connection maintains its own isolated session.
|
|
45
|
+
* Uses in-memory session management for MVP.
|
|
46
|
+
*/
|
|
47
|
+
export declare class WebSocketTransport<TState = any> implements Transport {
|
|
48
|
+
private readonly fn;
|
|
49
|
+
private readonly port;
|
|
50
|
+
private readonly host;
|
|
51
|
+
private readonly generateSessionId;
|
|
52
|
+
private readonly createInitialState;
|
|
53
|
+
private server;
|
|
54
|
+
private readonly sessions;
|
|
55
|
+
constructor(options: WebSocketTransportOptions<TState>);
|
|
56
|
+
/**
|
|
57
|
+
* Start the WebSocket server.
|
|
58
|
+
*/
|
|
59
|
+
start(): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Stop the WebSocket server.
|
|
62
|
+
*/
|
|
63
|
+
stop(): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Handle a new WebSocket connection.
|
|
66
|
+
*/
|
|
67
|
+
private handleConnection;
|
|
68
|
+
/**
|
|
69
|
+
* Handle an incoming message from a client.
|
|
70
|
+
*/
|
|
71
|
+
private handleMessage;
|
|
72
|
+
/**
|
|
73
|
+
* Send an error message to a client.
|
|
74
|
+
*/
|
|
75
|
+
private sendError;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=websocket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,KAAK,EAAE,sBAAsB,EAAE,SAAS,EAA2B,MAAM,YAAY,CAAC;AAE7F;;GAEG;AAEH,MAAM,WAAW,yBAAyB,CAAC,MAAM,GAAG,GAAG;IACrD,yCAAyC;IACzC,EAAE,EAAE,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACnD,wCAAwC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,6CAA6C;IAC7C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wDAAwD;IACxD,iBAAiB,CAAC,EAAE,MAAM,MAAM,CAAC;IACjC,uDAAuD;IACvD,kBAAkB,CAAC,EAAE,MAAM,MAAM,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AAEH,MAAM,WAAW,wBAAwB,CAAC,MAAM,GAAG,GAAG;IACpD,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAWD;;;;;GAKG;AAEH,qBAAa,kBAAkB,CAAC,MAAM,GAAG,GAAG,CAAE,YAAW,SAAS;IAChE,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAiD;IACpE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAe;IACjD,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAe;IAClD,OAAO,CAAC,MAAM,CAAgC;IAC9C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAsD;gBAEnE,OAAO,EAAE,yBAAyB,CAAC,MAAM,CAAC;IAQtD;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAiB5B;;OAEG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IA0B3B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IA0BxB;;OAEG;YACW,aAAa;IAmD3B;;OAEG;IACH,OAAO,CAAC,SAAS;CAOlB"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket transport for conversational agents.
|
|
3
|
+
*
|
|
4
|
+
* Provides a WebSocket server with:
|
|
5
|
+
* - Per-connection session isolation
|
|
6
|
+
* - JSON message format
|
|
7
|
+
* - Automatic session cleanup on disconnect
|
|
8
|
+
*/
|
|
9
|
+
import { WebSocketServer } from 'ws';
|
|
10
|
+
/**
|
|
11
|
+
* WebSocket transport implementation.
|
|
12
|
+
*
|
|
13
|
+
* Each connection maintains its own isolated session.
|
|
14
|
+
* Uses in-memory session management for MVP.
|
|
15
|
+
*/
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
export class WebSocketTransport {
|
|
18
|
+
fn;
|
|
19
|
+
port;
|
|
20
|
+
host;
|
|
21
|
+
generateSessionId;
|
|
22
|
+
createInitialState;
|
|
23
|
+
server = null;
|
|
24
|
+
sessions = new WeakMap();
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this.fn = options.fn;
|
|
27
|
+
this.port = options.port ?? 8080;
|
|
28
|
+
this.host = options.host ?? 'localhost';
|
|
29
|
+
this.generateSessionId = options.generateSessionId ?? (() => crypto.randomUUID());
|
|
30
|
+
this.createInitialState = options.createInitialState ?? (() => undefined);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Start the WebSocket server.
|
|
34
|
+
*/
|
|
35
|
+
async start() {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
this.server = new WebSocketServer({ host: this.host, port: this.port });
|
|
38
|
+
this.server.on('error', reject);
|
|
39
|
+
this.server.on('listening', () => {
|
|
40
|
+
console.log(`WebSocket server listening on ws://${this.host}:${this.port}`);
|
|
41
|
+
resolve();
|
|
42
|
+
});
|
|
43
|
+
this.server.on('connection', (ws) => {
|
|
44
|
+
this.handleConnection(ws);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Stop the WebSocket server.
|
|
50
|
+
*/
|
|
51
|
+
async stop() {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
if (!this.server) {
|
|
54
|
+
resolve();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
// Close all active connections
|
|
58
|
+
for (const client of this.server.clients) {
|
|
59
|
+
if (client.readyState === client.OPEN) {
|
|
60
|
+
client.close(1000, 'Server shutting down');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Close the server
|
|
64
|
+
this.server.close((err) => {
|
|
65
|
+
if (err) {
|
|
66
|
+
reject(err);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.server = null;
|
|
70
|
+
resolve();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Handle a new WebSocket connection.
|
|
77
|
+
*/
|
|
78
|
+
handleConnection(ws) {
|
|
79
|
+
// Create session for this connection
|
|
80
|
+
const sessionId = this.generateSessionId();
|
|
81
|
+
const session = {
|
|
82
|
+
sessionId,
|
|
83
|
+
conversationHistory: [],
|
|
84
|
+
state: this.createInitialState(),
|
|
85
|
+
};
|
|
86
|
+
this.sessions.set(ws, session);
|
|
87
|
+
console.log(`Client connected (session: ${sessionId})`);
|
|
88
|
+
ws.on('message', (data) => {
|
|
89
|
+
void this.handleMessage(ws, data);
|
|
90
|
+
});
|
|
91
|
+
ws.on('close', () => {
|
|
92
|
+
// Session cleanup happens automatically via WeakMap
|
|
93
|
+
console.log(`Client disconnected (session: ${sessionId})`);
|
|
94
|
+
});
|
|
95
|
+
ws.on('error', (error) => {
|
|
96
|
+
console.error('WebSocket error:', error);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Handle an incoming message from a client.
|
|
101
|
+
*/
|
|
102
|
+
async handleMessage(ws, data) {
|
|
103
|
+
try {
|
|
104
|
+
// Parse message
|
|
105
|
+
const message = JSON.parse(data.toString());
|
|
106
|
+
if (message.type !== 'message' || typeof message.content !== 'string') {
|
|
107
|
+
this.sendError(ws, 'Invalid message format. Expected: { type: "message", content: string }');
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
// Get session for this connection
|
|
111
|
+
const session = this.sessions.get(ws);
|
|
112
|
+
if (!session) {
|
|
113
|
+
// Session lost (shouldn't happen, but handle gracefully)
|
|
114
|
+
this.sendError(ws, 'Session not found');
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// Add user message to history
|
|
118
|
+
session.conversationHistory.push({ role: 'user', content: message.content });
|
|
119
|
+
// Pass session context
|
|
120
|
+
const context = {
|
|
121
|
+
sessionId: session.sessionId,
|
|
122
|
+
conversationHistory: session.conversationHistory,
|
|
123
|
+
state: session.state,
|
|
124
|
+
};
|
|
125
|
+
// Process message through conversational function
|
|
126
|
+
const output = await this.fn(message.content, context);
|
|
127
|
+
// Update session state (function may have mutated context.state)
|
|
128
|
+
session.state = context.state;
|
|
129
|
+
// Add assistant response to history
|
|
130
|
+
session.conversationHistory.push({ role: 'assistant', content: output });
|
|
131
|
+
// Send response
|
|
132
|
+
const response = {
|
|
133
|
+
type: 'message',
|
|
134
|
+
reply: output,
|
|
135
|
+
state: session.state,
|
|
136
|
+
};
|
|
137
|
+
ws.send(JSON.stringify(response));
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
console.error('Error processing message:', error);
|
|
141
|
+
this.sendError(ws, error instanceof Error ? error.message : 'Unknown error');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Send an error message to a client.
|
|
146
|
+
*/
|
|
147
|
+
sendError(ws, error) {
|
|
148
|
+
const response = {
|
|
149
|
+
type: 'error',
|
|
150
|
+
error,
|
|
151
|
+
};
|
|
152
|
+
ws.send(JSON.stringify(response));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=websocket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket.js","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,eAAe,EAAkB,MAAM,IAAI,CAAC;AAiDrD;;;;;GAKG;AACH,8DAA8D;AAC9D,MAAM,OAAO,kBAAkB;IACZ,EAAE,CAAiD;IACnD,IAAI,CAAS;IACb,IAAI,CAAS;IACb,iBAAiB,CAAe;IAChC,kBAAkB,CAAe;IAC1C,MAAM,GAA2B,IAAI,CAAC;IAC7B,QAAQ,GAAG,IAAI,OAAO,EAAuC,CAAC;IAE/E,YAAY,OAA0C;QACpD,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,WAAW,CAAC;QACxC,IAAI,CAAC,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;QACl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AS;gBACf,KAAK,EAAE,MAAM;gBACb,KAAK,EAAE,OAAO,CAAC,KAAK;aACrB,CAAC;YAEF,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QACpC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,KAAK,CAAC,CAAC;YAClD,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC;QAC/E,CAAC;IACH,CAAC;IAED;;OAEG;IACK,SAAS,CAAC,EAAa,EAAE,KAAa;QAC5C,MAAM,QAAQ,GAA6B;YACzC,IAAI,EAAE,OAAO;YACb,KAAK;SACN,CAAC;QACF,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;IACpC,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vibe-agent-toolkit/transports",
|
|
3
|
+
"version": "0.1.2-rc.4",
|
|
4
|
+
"description": "Transport adapters for VAT conversational agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"test": "vitest run",
|
|
18
|
+
"test:watch": "vitest",
|
|
19
|
+
"typecheck": "tsc --noEmit"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@vibe-agent-toolkit/agent-runtime": "0.1.2-rc.4",
|
|
23
|
+
"ws": "^8.18.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^22.10.5",
|
|
27
|
+
"@types/ws": "^8.5.13",
|
|
28
|
+
"typescript": "^5.7.3",
|
|
29
|
+
"vitest": "^2.1.9"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"ai",
|
|
33
|
+
"agents",
|
|
34
|
+
"transport",
|
|
35
|
+
"cli",
|
|
36
|
+
"websocket"
|
|
37
|
+
],
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/jdutton/vibe-agent-toolkit.git",
|
|
41
|
+
"directory": "packages/transports"
|
|
42
|
+
},
|
|
43
|
+
"author": "Jeff Dutton",
|
|
44
|
+
"license": "MIT"
|
|
45
|
+
}
|