|
| 1 | +/** |
| 2 | + * ClientInfo represents information about the client, such as protocol version, capabilities, and implementation details. |
| 3 | + * This structure should mirror the Rust/Python SDK structure and will eventually be backed by WASM bindings. |
| 4 | + */ |
| 5 | +export interface ClientInfo { |
| 6 | + protocolVersion: string; |
| 7 | + capabilities: object; // Should be typed to ClientCapabilities |
| 8 | + clientInfo: Implementation; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Implementation details for the client, such as name and version. |
| 13 | + * This structure should mirror the Rust/Python SDK Implementation struct. |
| 14 | + */ |
| 15 | +export interface Implementation { |
| 16 | + name: string; |
| 17 | + version: string; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * SSETransport provides an interface for connecting to an SSE (Server-Sent Events) endpoint. |
| 22 | + * In the browser, this would use EventSource; in Node.js, a compatible polyfill or HTTP client. |
| 23 | + * In the future, this should be backed by Rust/WASM for protocol logic. |
| 24 | + */ |
| 25 | +export class SSETransport { |
| 26 | + private url: string; |
| 27 | + private eventSource: EventSource | null = null; |
| 28 | + |
| 29 | + /** |
| 30 | + * Create a new SSETransport for the given URL. |
| 31 | + * @param url The SSE endpoint URL. |
| 32 | + */ |
| 33 | + constructor(url: string) { |
| 34 | + this.url = url; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Start the SSE connection. In browser, uses EventSource. In Node, requires a polyfill. |
| 39 | + * @param onMessage Callback for each message event. |
| 40 | + * @param onError Callback for error events. |
| 41 | + */ |
| 42 | + start(onMessage: (data: any) => void, onError?: (err: any) => void) { |
| 43 | + if (typeof window !== 'undefined' && typeof window.EventSource !== 'undefined') { |
| 44 | + this.eventSource = new window.EventSource(this.url); |
| 45 | + this.eventSource.onmessage = (event) => { |
| 46 | + onMessage(event.data); |
| 47 | + }; |
| 48 | + if (onError) { |
| 49 | + this.eventSource.onerror = onError; |
| 50 | + } |
| 51 | + } else { |
| 52 | + // Node.js: User must provide a compatible EventSource polyfill |
| 53 | + throw new Error('SSETransport requires EventSource (browser) or a polyfill (Node.js)'); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Close the SSE connection. |
| 59 | + */ |
| 60 | + close() { |
| 61 | + if (this.eventSource) { |
| 62 | + this.eventSource.close(); |
| 63 | + this.eventSource = null; |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * IntoTransport is an interface for types that can be converted into a transport. |
| 70 | + * This is a placeholder for extensibility and should mirror the Rust trait. |
| 71 | + */ |
| 72 | +export interface IntoTransport { |
| 73 | + intoTransport(): SSETransport; |
| 74 | +} |
| 75 | + |
| 76 | +// TODO: When Rust/WASM bindings are available, replace these stubs with real implementations and types. |
0 commit comments