Skip to content

Commit c5c29e5

Browse files
committed
Add docstrings
1 parent 88124d2 commit c5c29e5

5 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/http-stream.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ import type { AnyMessage } from "./jsonrpc.js";
1414
import type { Stream } from "./stream.js";
1515

1616
export interface HttpStreamOptions {
17+
/** Fetch implementation to use. Defaults to `globalThis.fetch`. */
1718
readonly fetch?: typeof globalThis.fetch;
19+
/** Headers to include on every HTTP/SSE request. */
1820
readonly headers?: Record<string, string>;
1921
}
2022

2123
/**
22-
* Creates an ACP Stream that speaks the Streamable HTTP transport.
24+
* Creates an ACP Stream over Streamable HTTP.
2325
*
24-
* The transport uses HTTP POST for client-to-agent messages and SSE GET streams for agent-to-client messages.
25-
* Cookie management is intentionally not built in; pass a cookie-aware fetch implementation when needed.
26+
* Uses POST for client messages and SSE GET streams for server messages.
27+
* Pass a custom `fetch` for cookies, auth, proxies, or non-browser runtimes.
2628
*/
2729
export function createHttpStream(
2830
serverUrl: string,

src/server.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,19 @@ import type {
2626
import type { Agent, AgentSideConnection } from "./acp.js";
2727
import type { AnyMessage, AnyRequest, AnyResponse } from "./jsonrpc.js";
2828

29+
/** Options for creating an ACP server transport. */
2930
export interface AcpServerOptions {
31+
/** Creates the agent implementation for each accepted ACP connection. */
3032
createAgent: (conn: AgentSideConnection) => Agent;
3133
}
3234

35+
/**
36+
* ACP server transport for Streamable HTTP and WebSocket connections.
37+
*
38+
* Route HTTP requests to {@link handleRequest}. For WebSocket upgrades, let your
39+
* framework perform the upgrade and pass the accepted socket to
40+
* {@link handleWebSocket}.
41+
*/
3342
export class AcpServer {
3443
private readonly createAgent: (conn: AgentSideConnection) => Agent;
3544
private readonly registry = new ConnectionRegistry();
@@ -38,6 +47,7 @@ export class AcpServer {
3847
this.createAgent = options.createAgent;
3948
}
4049

50+
/** Handles one Streamable HTTP ACP request. */
4151
async handleRequest(req: Request): Promise<Response> {
4252
if (req.method === "POST") {
4353
return await this.handlePost(req);
@@ -54,13 +64,15 @@ export class AcpServer {
5464
return textResponse("Method Not Allowed", 405);
5565
}
5666

67+
/** Handles one accepted ACP WebSocket connection. */
5768
handleWebSocket(socket: WebSocketServerSocket): void {
5869
handleWebSocketConnection(socket, {
5970
registry: this.registry,
6071
createAgent: this.createAgent,
6172
});
6273
}
6374

75+
/** Closes all active ACP connections owned by this server. */
6476
async close(): Promise<void> {
6577
this.registry.closeAll();
6678
}

src/ws-server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
import type { AnyMessage, AnyRequest } from "./jsonrpc.js";
1919
import type { WebSocketLike } from "./ws-utils.js";
2020

21+
/** WebSocket shape accepted by `AcpServer.handleWebSocket`. */
2122
export type WebSocketServerSocket = WebSocketLike;
2223

2324
type ForwardResult =

src/ws-stream.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import type { AnyMessage } from "./jsonrpc.js";
55
import type { Stream } from "./stream.js";
66

77
export interface WebSocketStreamOptions {
8-
/** WebSocket subprotocols. */
8+
/** WebSocket subprotocols to request. */
99
readonly protocols?: string[];
1010
/**
11-
* Custom headers for runtimes/constructors that support them, such as Node.js
12-
* `ws`. Browsers ignore custom headers because the browser WebSocket API does
13-
* not expose a headers option.
11+
* Headers for WebSocket constructors that support them, such as Node `ws`.
12+
* Browser WebSocket constructors ignore custom headers.
1413
*/
1514
readonly headers?: Record<string, string>;
16-
/** Custom WebSocket constructor, for example `ws.WebSocket` in Node.js. */
15+
/** WebSocket constructor to use. Defaults to `globalThis.WebSocket`. */
1716
readonly WebSocket?: WebSocketConstructor;
1817
}
1918

19+
/** Constructor shape used by `createWebSocketStream`. */
2020
export interface WebSocketConstructor {
2121
new (
2222
url: string,
@@ -25,14 +25,15 @@ export interface WebSocketConstructor {
2525
): WebSocketLike;
2626
}
2727

28+
export type { WebSocketLike };
29+
2830
const SOCKET_OPEN = 1;
2931

3032
/**
31-
* Creates an ACP Stream that speaks JSON-RPC over WebSocket text frames.
33+
* Creates an ACP Stream over WebSocket.
3234
*
33-
* Browser WebSocket constructors do not support custom headers. The `headers`
34-
* option is passed as a best-effort third constructor argument for runtimes such
35-
* as Node.js `ws` that accept it.
35+
* Sends and receives ACP JSON-RPC messages as WebSocket text frames. In Node,
36+
* pass a WebSocket constructor such as `ws.WebSocket` via `options.WebSocket`.
3637
*/
3738
export function createWebSocketStream(
3839
serverUrl: string,

src/ws-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** Minimal browser/Node-compatible WebSocket shape used by ACP transports. */
12
export interface WebSocketLike {
23
readonly readyState?: number;
34
send(data: string): void;

0 commit comments

Comments
 (0)