Skip to content

Commit 86368fa

Browse files
refactor(server): follow the Effect service rules in plugin notifications
PluginNotifications reaches the plugin supervisor through its module namespace and builds each PluginHostCallError where the call fails, without a forwarding helper or a shared stopped error. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent baaab21 commit 86368fa

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

‎apps/server/src/plugins/PluginNotifications.ts‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ import * as Stream from "effect/Stream";
3838

3939
import { normalizeContributionStatusText } from "@t3tools/provider-core/server/ContributionStatusStore";
4040
import { subscribeBeforeSnapshot } from "../utils/subscribeBeforeSnapshot.ts";
41-
import {
42-
PluginHostCallError,
43-
type PluginHostMethod,
44-
PluginSupervisor,
45-
} from "./PluginSupervisor.ts";
41+
import * as PluginSupervisor from "./PluginSupervisor.ts";
4642
import { makeTokenBucket } from "./pluginTokenBucket.ts";
4743

4844
/**
@@ -76,9 +72,6 @@ const encodedBytes = (notification: PluginNotification) =>
7672
/** Elapsed time, which a wall clock change does not move. */
7773
const elapsedMillis = Effect.map(Clock.monotonicTimeNanos, (nanos) => Number(nanos / 1_000_000n));
7874

79-
const hostError = (message: string) => new PluginHostCallError({ message });
80-
const STOPPED = hostError("The plugin was stopped.");
81-
8275
interface Retained {
8376
readonly notification: PluginNotification;
8477
/** In `elapsedMillis`, not wall time. */
@@ -101,7 +94,7 @@ export class PluginNotifications extends Context.Service<
10194

10295
/** @public Service construction is part of the canonical Effect module API. */
10396
export const make = Effect.fn("PluginNotifications.make")(function* () {
104-
const supervisor = yield* PluginSupervisor;
97+
const supervisor = yield* PluginSupervisor.PluginSupervisor;
10598
const crypto = yield* Crypto.Crypto;
10699
const epoch = yield* crypto.randomUUIDv4.pipe(Effect.orDie);
107100
// Every frame is the whole set, so a slow subscriber only skips intermediate ones.
@@ -165,24 +158,28 @@ export const make = Effect.fn("PluginNotifications.make")(function* () {
165158
return generation;
166159
});
167160

168-
const show: PluginHostMethod = ({ registration, input, admitted, lifetime }) =>
161+
const show: PluginSupervisor.PluginHostMethod = ({ registration, input, admitted, lifetime }) =>
169162
Effect.gen(function* () {
170163
if (!registration.manifest.capabilities.includes(PLUGIN_NOTIFICATIONS_CAPABILITY))
171-
return yield* hostError(
172-
`The plugin did not declare the "${PLUGIN_NOTIFICATIONS_CAPABILITY}" capability.`,
173-
);
164+
return yield* new PluginSupervisor.PluginHostCallError({
165+
message: `The plugin did not declare the "${PLUGIN_NOTIFICATIONS_CAPABILITY}" capability.`,
166+
});
174167
const request = yield* decodeShowInput(input).pipe(
175-
Effect.mapError(() =>
176-
hostError(
177-
`A notification needs a title string; body is a string; threadId is a string of at most ${PLUGIN_NOTIFICATION_LIMITS.threadIdMaxLength} characters; tone is neutral, info, success, warning or error.`,
178-
),
168+
Effect.mapError(
169+
() =>
170+
new PluginSupervisor.PluginHostCallError({
171+
message: `A notification needs a title string; body is a string; threadId is a string of at most ${PLUGIN_NOTIFICATION_LIMITS.threadIdMaxLength} characters; tone is neutral, info, success, warning or error.`,
172+
}),
179173
),
180174
);
181175
const title = normalizeContributionStatusText(
182176
request.title,
183177
PLUGIN_NOTIFICATION_TITLE_MAX_LENGTH,
184178
);
185-
if (title.length === 0) return yield* hostError("The notification title is empty.");
179+
if (title.length === 0)
180+
return yield* new PluginSupervisor.PluginHostCallError({
181+
message: "The notification title is empty.",
182+
});
186183
const body =
187184
request.body === undefined
188185
? ""
@@ -201,18 +198,21 @@ export const make = Effect.fn("PluginNotifications.make")(function* () {
201198
// sequence (an ISO timestamp is fixed width), so the record sent is never larger.
202199
const widest = record(Number.MAX_SAFE_INTEGER, yield* Clock.currentTimeMillis);
203200
if (encodedBytes(widest) > PLUGIN_NOTIFICATION_MAX_ENCODED_BYTES)
204-
return yield* hostError(
205-
`The notification is too large: at most ${PLUGIN_NOTIFICATION_MAX_ENCODED_BYTES} bytes encoded.`,
206-
);
201+
return yield* new PluginSupervisor.PluginHostCallError({
202+
message: `The notification is too large: at most ${PLUGIN_NOTIFICATION_MAX_ENCODED_BYTES} bytes encoded.`,
203+
});
207204
const generation = yield* generationOf(lifetime);
208205
if (!(yield* generation.bucket.take))
209-
return yield* hostError(
210-
`Too many notifications: at most ${PLUGIN_NOTIFICATION_LIMITS.burst} at once, then one every ${PLUGIN_NOTIFICATION_LIMITS.refillMillis / 1000} seconds.`,
211-
);
206+
return yield* new PluginSupervisor.PluginHostCallError({
207+
message: `Too many notifications: at most ${PLUGIN_NOTIFICATION_LIMITS.burst} at once, then one every ${PLUGIN_NOTIFICATION_LIMITS.refillMillis / 1000} seconds.`,
208+
});
212209
return yield* lock.withPermit(
213210
Effect.gen(function* () {
214211
// Checked under the lock, so a withdrawal never misses a notification it races.
215-
if (generation.closed) return yield* STOPPED;
212+
if (generation.closed)
213+
return yield* new PluginSupervisor.PluginHostCallError({
214+
message: "The plugin was stopped.",
215+
});
216216
yield* admitted;
217217
const now = yield* elapsedMillis;
218218
sequence += 1;

0 commit comments

Comments
 (0)