Open History to see the last commit
1diff --git a/dist/unstable/ai/McpServer.js b/dist/unstable/ai/McpServer.js
2index 4819ff0..f5e0f60 100644
3--- a/dist/unstable/ai/McpServer.js
4+++ b/dist/unstable/ai/McpServer.js
5@@ -235,6 +235,26 @@ export const run = /*#__PURE__*/Effect.fnUntraced(function* (options) {
6 const server = yield* McpServer;
7 const isHttp = Option.isSome(yield* Effect.serviceOption(HttpRouter.HttpRouter));
8 const clientSessions = new Map();
9+ if (isHttp && options.path !== undefined) {
10+ const router = yield* HttpRouter.HttpRouter;
11+ yield* router.add("DELETE", options.path, Effect.gen(function* () {
12+ const request = yield* HttpServerRequest.HttpServerRequest;
13+ const sessionId = request.headers[mcpSessionIdHeader];
14+ if (sessionId === undefined) {
15+ return HttpServerResponse.empty({
16+ status: 400
17+ });
18+ }
19+ if (!clientSessions.delete(sessionId)) {
20+ return HttpServerResponse.empty({
21+ status: 404
22+ });
23+ }
24+ return HttpServerResponse.empty({
25+ status: 204
26+ });
27+ }));
28+ }
29 const handlers = yield* Layer.build(layerHandlers(options, {
30 clientSessions
31 }));
32diff --git a/dist/unstable/rpc/RpcClient.d.ts b/dist/unstable/rpc/RpcClient.d.ts
33index b0f61df..ead663e 100644
34--- a/dist/unstable/rpc/RpcClient.d.ts
35+++ b/dist/unstable/rpc/RpcClient.d.ts
36@@ -2,6 +2,7 @@ import * as Cause from "../../Cause.ts";
37 import * as Context from "../../Context.ts";
38 import type * as Duration from "../../Duration.ts";
39 import * as Effect from "../../Effect.ts";
40+import * as Exit from "../../Exit.ts";
41 import * as Layer from "../../Layer.ts";
42 import * as Queue from "../../Queue.ts";
43 import * as Schedule from "../../Schedule.ts";
44@@ -260,9 +261,40 @@ export declare const layerProtocolWorker: (options: {
45 readonly targetUtilization?: number | undefined;
46 readonly timeToLive: Duration.Input;
47 }) => Layer.Layer<Protocol, WorkerError, Worker.WorkerPlatform | Worker.Spawner>;
48+declare const RequestHooks_base: Context.ServiceClass<RequestHooks, "effect/rpc/RpcClient/RequestHooks", {
49+ readonly onRequestStart?: ((info: {
50+ readonly id: RequestId;
51+ readonly tag: string;
52+ readonly stream: boolean;
53+ }) => Effect.Effect<void>) | undefined;
54+ readonly onRequestChunk?: ((info: {
55+ readonly id: RequestId;
56+ readonly tag: string;
57+ readonly chunkCount: number;
58+ }) => Effect.Effect<void>) | undefined;
59+ readonly onRequestExit?: ((info: {
60+ readonly id: RequestId;
61+ readonly tag: string;
62+ readonly stream: boolean;
63+ readonly exit: Exit.Exit<unknown, unknown>;
64+ }) => Effect.Effect<void>) | undefined;
65+ readonly onRequestInterrupt?: ((info: {
66+ readonly id: RequestId;
67+ readonly tag?: string | undefined;
68+ }) => Effect.Effect<void>) | undefined;
69+}>;
70+/**
71+ * @since 4.0.0
72+ * @category RequestHooks
73+ */
74+export declare class RequestHooks extends RequestHooks_base {
75+}
76 declare const ConnectionHooks_base: Context.ServiceClass<ConnectionHooks, "effect/rpc/RpcClient/ConnectionHooks", {
77 readonly onConnect: Effect.Effect<void>;
78 readonly onDisconnect: Effect.Effect<void>;
79+ readonly onPing?: Effect.Effect<void> | undefined;
80+ readonly onPong?: Effect.Effect<void> | undefined;
81+ readonly onPingTimeout?: Effect.Effect<void> | undefined;
82 }>;
83 /**
84 * Represents optional client protocol hooks that run when a transport connects
85@@ -279,4 +311,4 @@ declare const ConnectionHooks_base: Context.ServiceClass<ConnectionHooks, "effec
86 export declare class ConnectionHooks extends ConnectionHooks_base {
87 }
88 export {};
89-//# sourceMappingURL=RpcClient.d.ts.map
90\ No newline at end of file
91+//# sourceMappingURL=RpcClient.d.ts.map
92diff --git a/dist/unstable/rpc/RpcClient.js b/dist/unstable/rpc/RpcClient.js
93index a3161eb..0cb81f3 100644
94--- a/dist/unstable/rpc/RpcClient.js
95+++ b/dist/unstable/rpc/RpcClient.js
96@@ -46,6 +46,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
97 const services = yield* Effect.context();
98 const scope = Context.get(services, Scope.Scope);
99 const entries = new Map();
100+ const hooks = yield* Effect.serviceOption(RequestHooks);
101 let isShutdown = false;
102 yield* Scope.addFinalizer(scope, Effect.withFiber(parent => {
103 isShutdown = true;
104@@ -83,6 +84,11 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
105 return Effect.interrupt;
106 }
107 const id = generateRequestId();
108+ const onStart = Option.isSome(hooks) && hooks.value.onRequestStart ? hooks.value.onRequestStart({
109+ id,
110+ tag: rpc._tag,
111+ stream: false
112+ }) : Effect.void;
113 const send = middleware(message => options.onFromClient({
114 message,
115 context,
116@@ -100,7 +106,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
117 headers: Headers.merge(parentFiber.getRef(CurrentHeaders), headers)
118 });
119 if (discard) {
120- return send;
121+ return onStart.pipe(Effect.andThen(send));
122 }
123 let fiber;
124 return Effect.onInterrupt(Effect.callback(resume => {
125@@ -118,7 +124,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
126 }
127 };
128 entries.set(id, entry);
129- fiber = send.pipe(span ? Effect.withParentSpan(span, {
130+ fiber = onStart.pipe(Effect.andThen(send), span ? Effect.withParentSpan(span, {
131 captureStackTrace: false
132 }) : identity, Effect.runForkWith(parentFiber.context));
133 fiber.addObserver(exit => {
134@@ -127,8 +133,9 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
135 }
136 });
137 }), interruptors => {
138+ const entry = entries.get(id);
139 entries.delete(id);
140- return Effect.andThen(Fiber.interrupt(fiber), sendInterrupt(id, Array.from(interruptors), context));
141+ return Effect.andThen(Fiber.interrupt(fiber), sendInterrupt(id, Array.from(interruptors), context, entry?.rpc._tag));
142 });
143 });
144 const onStreamRequest = Effect.fnUntraced(function* (rpc, middleware, payload, headers, streamBufferSize, context) {
145@@ -140,11 +147,17 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
146 });
147 const fiber = Fiber.getCurrent();
148 const id = generateRequestId();
149+ const onStart = Option.isSome(hooks) && hooks.value.onRequestStart ? hooks.value.onRequestStart({
150+ id,
151+ tag: rpc._tag,
152+ stream: true
153+ }) : Effect.void;
154 const scope = Context.getUnsafe(fiber.context, Scope.Scope);
155 yield* Scope.addFinalizerExit(scope, exit => {
156- if (!entries.has(id)) return Effect.void;
157+ const entry = entries.get(id);
158+ if (!entry) return Effect.void;
159 entries.delete(id);
160- return sendInterrupt(id, Exit.isFailure(exit) ? Array.from(Cause.interruptors(exit.cause)) : [], context);
161+ return sendInterrupt(id, Exit.isFailure(exit) ? Array.from(Cause.interruptors(exit.cause)) : [], context, entry.rpc._tag);
162 });
163 const queue = yield* Queue.bounded(streamBufferSize);
164 entries.set(id, {
165@@ -152,9 +165,10 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
166 rpc,
167 queue,
168 scope,
169- context
170+ context,
171+ chunkCount: 0
172 });
173- yield* middleware(message => options.onFromClient({
174+ yield* onStart.pipe(Effect.andThen(middleware(message => options.onFromClient({
175 message,
176 context,
177 discard: false
178@@ -169,7 +183,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
179 sampled: span.sampled
180 } : {}),
181 headers: Headers.merge(fiber.getRef(CurrentHeaders), headers)
182- }).pipe(span ? Effect.withParentSpan(span, {
183+ }))).pipe(span ? Effect.withParentSpan(span, {
184 captureStackTrace: false
185 }) : identity, Effect.catchCause(error => Queue.failCause(queue, error)), Effect.interruptible, Effect.forkIn(scope, {
186 startImmediately: true
187@@ -199,7 +213,10 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
188 });
189 };
190 };
191- const sendInterrupt = (requestId, interruptors, context) => Effect.callback(resume => {
192+ const sendInterrupt = (requestId, interruptors, context, tag) => (Option.isSome(hooks) && hooks.value.onRequestInterrupt ? hooks.value.onRequestInterrupt({
193+ id: requestId,
194+ tag
195+ }) : Effect.void).pipe(Effect.andThen(Effect.callback(resume => {
196 const parentFiber = Fiber.getCurrent();
197 const fiber = options.onFromClient({
198 message: {
199@@ -213,7 +230,7 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
200 fiber.addObserver(() => {
201 resume(Effect.void);
202 });
203- });
204+ })));
205 const write = message => {
206 switch (message._tag) {
207 case "Chunk":
208@@ -221,7 +238,13 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
209 const requestId = message.requestId;
210 const entry = entries.get(requestId);
211 if (!entry || entry._tag !== "Queue") return Effect.void;
212- return Queue.offerAll(entry.queue, message.values).pipe(supportsAck ? Effect.flatMap(() => options.onFromClient({
213+ const chunkCount = entry.chunkCount += message.values.length;
214+ const onChunk = Option.isSome(hooks) && hooks.value.onRequestChunk ? hooks.value.onRequestChunk({
215+ id: requestId,
216+ tag: entry.rpc._tag,
217+ chunkCount
218+ }) : Effect.void;
219+ return Queue.offerAll(entry.queue, message.values).pipe(Effect.andThen(onChunk), supportsAck ? Effect.flatMap(() => options.onFromClient({
220 message: {
221 _tag: "Ack",
222 requestId: message.requestId
223@@ -236,11 +259,18 @@ export const makeNoSerialization = /*#__PURE__*/Effect.fnUntraced(function* (gro
224 const entry = entries.get(requestId);
225 if (!entry) return Effect.void;
226 entries.delete(requestId);
227+ const onExit = Option.isSome(hooks) && hooks.value.onRequestExit ? hooks.value.onRequestExit({
228+ id: requestId,
229+ tag: entry.rpc._tag,
230+ stream: entry._tag === "Queue",
231+ exit: message.exit
232+ }) : Effect.void;
233 if (entry._tag === "Effect") {
234- entry.resume(message.exit);
235- return Effect.void;
236+ return onExit.pipe(Effect.andThen(Effect.sync(() => {
237+ entry.resume(message.exit);
238+ })));
239 }
240- return message.exit._tag === "Success" ? Queue.end(entry.queue) : Queue.failCause(entry.queue, message.exit.cause);
241+ return onExit.pipe(Effect.andThen(message.exit._tag === "Success" ? Queue.end(entry.queue) : Queue.failCause(entry.queue, message.exit.cause)));
242 }
243 case "Defect":
244 {
245@@ -568,7 +598,7 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
246 const requestClientMap = new Map();
247 const write = yield* socket.writer;
248 let parser = serialization.makeUnsafe();
249- const pinger = yield* makePinger(write(parser.encode(constPing)));
250+ const pinger = yield* makePinger(write(parser.encode(constPing)), Option.isSome(hooks) ? hooks.value : undefined);
251 let currentError;
252 const onOpen = Effect.suspend(() => {
253 currentError = undefined;
254@@ -588,8 +618,7 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
255 body: () => {
256 const response = responses[i++];
257 if (response._tag === "Pong") {
258- pinger.onPong();
259- return Effect.void;
260+ return pinger.onPong;
261 }
262 if ("requestId" in response) {
263 const clientId = requestClientMap.get(response.requestId);
264@@ -617,12 +646,12 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
265 }
266 }, {
267 onOpen
268- }).pipe(Effect.raceFirst(Effect.flatMap(pinger.timeout, () => Effect.fail(new Socket.SocketError({
269+ }).pipe(Effect.raceFirst(Effect.flatMap(pinger.timeout, () => (Option.isSome(hooks) && hooks.value.onPingTimeout ? hooks.value.onPingTimeout : Effect.void).pipe(Effect.andThen(Effect.fail(new Socket.SocketError({
270 reason: new Socket.SocketOpenError({
271 kind: "Timeout",
272 cause: new Error("ping timeout")
273 })
274- })))));
275+ })))))));
276 }).pipe(Effect.flatMap(() => Effect.fail(new Socket.SocketError({
277 reason: new Socket.SocketCloseError({
278 code: 1000
279@@ -664,20 +693,20 @@ export const makeProtocolSocket = options => Protocol.make(Effect.fnUntraced(fun
280 };
281 }));
282 const defaultRetryPolicy = /*#__PURE__*/Schedule.exponential(500, 1.5).pipe(/*#__PURE__*/Schedule.either(/*#__PURE__*/Schedule.spaced(5000)));
283-const makePinger = /*#__PURE__*/Effect.fnUntraced(function* (writePing) {
284+const makePinger = /*#__PURE__*/Effect.fnUntraced(function* (writePing, hooks) {
285 let recievedPong = true;
286 const latch = Latch.makeUnsafe();
287 const reset = () => {
288 recievedPong = true;
289 latch.closeUnsafe();
290 };
291- const onPong = () => {
292+ const onPong = Effect.sync(() => {
293 recievedPong = true;
294- };
295+ }).pipe(Effect.andThen(hooks?.onPong ?? Effect.void));
296 yield* Effect.suspend(() => {
297 if (!recievedPong) return latch.open;
298 recievedPong = false;
299- return writePing;
300+ return (hooks?.onPing ?? Effect.void).pipe(Effect.andThen(writePing));
301 }).pipe(Effect.delay("5 seconds"), Effect.ignore, Effect.forever, Effect.interruptible, Effect.forkScoped);
302 return {
303 timeout: latch.await,
304@@ -820,6 +849,11 @@ export const makeProtocolWorker = options => Protocol.make(Effect.fnUntraced(fun
305 * @since 4.0.0
306 */
307 export const layerProtocolWorker = /*#__PURE__*/flow(makeProtocolWorker, /*#__PURE__*/Layer.effect(Protocol));
308+/**
309+ * @since 4.0.0
310+ * @category RequestHooks
311+ */
312+export class RequestHooks extends /*#__PURE__*/Context.Service()("effect/rpc/RpcClient/RequestHooks") {}
313 /**
314 * Represents optional client protocol hooks that run when a transport connects
315 * and disconnects.
316@@ -835,4 +869,4 @@ export const layerProtocolWorker = /*#__PURE__*/flow(makeProtocolWorker, /*#__PU
317 export class ConnectionHooks extends /*#__PURE__*/Context.Service()("effect/rpc/RpcClient/ConnectionHooks") {}
318 // internal
319 const decodeDefect = /*#__PURE__*/Schema.decodeSync(/*#__PURE__*/Schema.Defect());
320-//# sourceMappingURL=RpcClient.js.map
321\ No newline at end of file
322+//# sourceMappingURL=RpcClient.js.map