Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
"import": "./dist/transports/server.js",
"default": "./dist/transports/server.js"
},
"./experimental/http-backend": {
"types": "./dist/transports/http-backend.d.ts",
"import": "./dist/transports/http-backend.js",
"default": "./dist/transports/http-backend.js"
},
"./experimental/node": {
"types": "./dist/transports/node-adapter.d.ts",
"import": "./dist/transports/node-adapter.js",
Expand Down
22 changes: 21 additions & 1 deletion src/core/jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,15 @@ type PreparedRequest<Output> = {
*/
export type MaybePromise<T> = T | Promise<T>;

/**
* Allocates IDs for JSON-RPC requests sent by this connection.
*
* The default generator is a per-connection numeric sequence starting at `0`.
* HTTP server integrations can inject a custom generator so server-originated
* request IDs remain unique across distributed server instances.
*/
export type JsonRpcRequestIdGenerator = () => string | number;

/**
* Incoming request passed to JSON-RPC handlers.
*/
Expand Down Expand Up @@ -852,6 +861,13 @@ export type ConnectionOptions = {
* @internal
*/
allowBatches?: boolean;

/**
* Allocates IDs for outbound JSON-RPC requests.
*
* Defaults to the existing per-connection numeric sequence: 0, 1, 2, ...
*/
requestIdGenerator?: JsonRpcRequestIdGenerator;
};

/**
Expand All @@ -865,6 +881,8 @@ export class Connection {
new Map();
private incomingRequests: Map<JsonRpcId, AbortController> = new Map();
private nextRequestId = 0;
private requestIdGenerator: JsonRpcRequestIdGenerator = () =>
this.nextRequestId++;
private staticHandlers: JsonRpcHandler[] = [];
private dynamicHandlers: Set<JsonRpcHandler> = new Set();
private fallbackHandlers: JsonRpcHandler[] = [];
Expand Down Expand Up @@ -1115,7 +1133,7 @@ export class Connection {
mapResponse: ((response: Resp) => Output) | undefined,
options: SendRequestOptions = {},
): PreparedRequest<Output> {
const id = this.nextRequestId++;
const id = this.requestIdGenerator();
let cancel = () => {};
const response = new Promise<Output>((resolve, reject) => {
const pendingResponse: ConnectionPendingResponse = {
Expand Down Expand Up @@ -1189,6 +1207,8 @@ export class Connection {
this.staticHandlers = handlers;
this.fallbackHandlers = [...(options?.fallbackHandlers ?? [])];
this.allowBatches = options?.allowBatches ?? true;
this.requestIdGenerator =
options?.requestIdGenerator ?? (() => this.nextRequestId++);
this.closedPromise = new Promise((resolve) => {
this.abortController.signal.addEventListener("abort", () => resolve());
});
Expand Down
8 changes: 6 additions & 2 deletions src/test-support/test-http-server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AcpHttpBackend } from "../transports/http-backend.js";
import http from "node:http";
import { WebSocketServer } from "ws";

Expand All @@ -19,9 +20,12 @@ export interface TestHttpServer {

export async function startTestServer(
createAgent: () => AgentApp = () => createTestAgentApp(),
options: { port?: number } = {},
options: { port?: number; httpBackend?: AcpHttpBackend } = {},
): Promise<TestHttpServer> {
const acpServer = new AcpServer({ createAgent });
const acpServer = new AcpServer({
createAgent,
httpBackend: options.httpBackend,
});
const httpServer = http.createServer(createNodeHttpHandler(acpServer));
const webSocketServer = new WebSocketServer({ noServer: true });

Expand Down
Loading