@gjermundgaraba/effect-actions 0.22.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -3
- package/dist/Action.d.mts +2 -2
- package/dist/Action.mjs +1 -0
- package/dist/ActionCli.d.mts +1 -1
- package/dist/ActionCli.mjs +1 -2
- package/dist/ActionHttp.d.mts +2 -2
- package/dist/ActionMcp.d.mts +20 -20
- package/dist/ActionMcp.mjs +9 -11
- package/dist/ActionRpc.d.mts +8 -15
- package/dist/ActionRpc.mjs +29 -54
- package/dist/ActionToolkit.d.mts +0 -2
- package/dist/ActionToolkit.mjs +0 -2
- package/dist/Event.d.mts +0 -23
- package/dist/Testing.mjs +3 -2
- package/dist/Webhook.mjs +2 -2
- package/dist/authentication/refusal.mjs +0 -1
- package/dist/cli/commands.mjs +0 -5
- package/dist/contract/authorizer.mjs +0 -5
- package/dist/contract/call.mjs +0 -23
- package/dist/contract/callback.mjs +0 -1
- package/dist/contract/caller.mjs +0 -11
- package/dist/contract/canonical.mjs +0 -5
- package/dist/contract/codec.d.mts +0 -1
- package/dist/contract/codec.mjs +0 -1
- package/dist/contract/codes.mjs +1 -15
- package/dist/contract/declared.mjs +0 -15
- package/dist/contract/digest.mjs +0 -1
- package/dist/contract/errors.d.mts +0 -4
- package/dist/contract/errors.mjs +0 -4
- package/dist/contract/feed.d.mts +0 -1
- package/dist/contract/feed.mjs +0 -20
- package/dist/contract/hints.mjs +0 -6
- package/dist/contract/hub.mjs +0 -3
- package/dist/contract/idempotency.mjs +0 -4
- package/dist/contract/implementation.d.mts +0 -1
- package/dist/contract/implementation.mjs +0 -27
- package/dist/contract/input-check.mjs +0 -1
- package/dist/contract/lease.mjs +0 -1
- package/dist/contract/listed.mjs +0 -6
- package/dist/contract/log.mjs +0 -8
- package/dist/contract/names.mjs +0 -1
- package/dist/contract/occurrence.mjs +0 -8
- package/dist/contract/pages.mjs +5 -12
- package/dist/contract/progress.mjs +2 -25
- package/dist/contract/push.d.mts +0 -5
- package/dist/contract/push.mjs +1 -24
- package/dist/contract/receiving.mjs +0 -7
- package/dist/contract/reported.mjs +0 -5
- package/dist/contract/serving.mjs +0 -18
- package/dist/contract/status.mjs +0 -3
- package/dist/contract/stream.mjs +0 -2
- package/dist/contract/subscribe.d.mts +0 -2
- package/dist/contract/subscribe.mjs +0 -8
- package/dist/contract/transient.mjs +0 -12
- package/dist/contract/watch.mjs +5 -17
- package/dist/contract/webhooks.mjs +0 -11
- package/dist/http/client.mjs +0 -1
- package/dist/http/events.d.mts +0 -10
- package/dist/http/events.mjs +2 -31
- package/dist/http/placed.d.mts +0 -2
- package/dist/http/streamed.d.mts +0 -1
- package/dist/http/streamed.mjs +4 -32
- package/dist/http/watch.mjs +0 -6
- package/dist/mcp/bridge.mjs +1 -26
- package/dist/mcp/client.d.mts +0 -1
- package/dist/mcp/client.mjs +0 -1
- package/dist/mcp/events.mjs +0 -6
- package/dist/mcp/progress.mjs +0 -7
- package/dist/mcp/protocol.mjs +0 -6
- package/dist/mcp/reply.mjs +0 -11
- package/dist/mcp/request.mjs +0 -1
- package/dist/mcp/watch.mjs +0 -6
- package/dist/rpc/watch.mjs +0 -6
- package/dist/stdio/console.mjs +2 -2
- package/dist/webhook/address.mjs +4 -9
- package/dist/webhook/attempt.mjs +2 -15
- package/dist/webhook/receiver.mjs +2 -8
- package/dist/webhook/signing.mjs +0 -3
- package/dist/webhook/stored.mjs +8 -17
- package/dist/webhook/subscriptions.mjs +19 -23
- package/docs/Action-streams.md +0 -12
- package/docs/Action.md +0 -10
- package/docs/ActionCli.md +2 -26
- package/docs/ActionHttp-failures.md +1 -1
- package/docs/ActionHttp.md +0 -28
- package/docs/ActionMcp-failures.md +2 -1
- package/docs/ActionMcp.md +2 -37
- package/docs/ActionRpc-events.md +0 -16
- package/docs/ActionRpc.md +0 -63
- package/docs/ActionToolkit.md +0 -11
- package/docs/Authentication-advanced.md +0 -31
- package/docs/Authentication.md +4 -13
- package/docs/Event.md +0 -37
- package/docs/README.md +1 -1
- package/docs/Testing-failures.md +1 -1
- package/docs/Testing.md +3 -13
- package/docs/Webhook.md +0 -46
- package/docs/setup-failures.md +1 -1
- package/docs/setup.md +5 -10
- package/examples/README.md +7 -1
- package/examples/assistant/src/agent.ts +8 -31
- package/examples/assistant/src/cli.ts +2 -9
- package/examples/assistant/src/commands.ts +0 -8
- package/examples/assistant/src/identity/Identity.ts +0 -10
- package/examples/assistant/src/identity/authorize.ts +0 -4
- package/examples/assistant/src/identity/members.ts +1 -3
- package/examples/assistant/src/identity/permissions.ts +0 -5
- package/examples/assistant/src/platform/tasks-file.ts +1 -6
- package/examples/assistant/src/scripted.ts +0 -4
- package/examples/assistant/src/tasks/Tasks.ts +0 -2
- package/examples/assistant/src/tasks/handlers.ts +0 -3
- package/examples/assistant/src/tasks/store.ts +0 -7
- package/examples/assistant/src/toolkit.ts +0 -11
- package/examples/assistant/test/commands.test.ts +0 -5
- package/examples/assistant/test/identity/identity.test.ts +6 -6
- package/examples/assistant/test/platform/tasks-file.test.ts +2 -6
- package/examples/assistant/test/tasks/tasks.test.ts +13 -14
- package/examples/assistant/test/toolkit.test.ts +7 -18
- package/examples/build-events/README.md +2 -0
- package/examples/build-events/src/Api.ts +0 -3
- package/examples/build-events/src/Rpc.ts +0 -2
- package/examples/build-events/src/builds/Builds.ts +0 -15
- package/examples/build-events/src/builds/events.ts +0 -10
- package/examples/build-events/src/builds/handlers.ts +0 -6
- package/examples/build-events/src/builds/log.ts +0 -6
- package/examples/build-events/src/builds/status.ts +0 -4
- package/examples/build-events/src/builds/store.ts +0 -21
- package/examples/build-events/src/cli.ts +0 -2
- package/examples/build-events/src/commands.ts +0 -9
- package/examples/build-events/src/deploys/approvers.ts +0 -3
- package/examples/build-events/src/deploys/events.ts +0 -8
- package/examples/build-events/src/deploys/handlers.ts +0 -2
- package/examples/build-events/src/identity/Identity.ts +0 -11
- package/examples/build-events/src/identity/members.ts +1 -2
- package/examples/build-events/src/identity/verifier.ts +8 -9
- package/examples/build-events/src/notifier/Notices.ts +0 -3
- package/examples/build-events/src/notifier/routes.ts +0 -10
- package/examples/build-events/src/notifier.ts +0 -8
- package/examples/build-events/src/routes.ts +0 -24
- package/examples/build-events/src/server.ts +0 -14
- package/examples/build-events/test/builds/events.test.ts +0 -6
- package/examples/build-events/test/builds/log.test.ts +0 -1
- package/examples/build-events/test/builds/progress.test.ts +0 -1
- package/examples/build-events/test/commands.test.ts +0 -5
- package/examples/build-events/test/deploys/events.test.ts +0 -1
- package/examples/build-events/test/http.test.ts +0 -4
- package/examples/build-events/test/notifier.test.ts +0 -9
- package/examples/build-events/test/routes.test.ts +0 -9
- package/examples/build-events/test/rpc.test.ts +0 -4
- package/examples/build-events/test/support/as.ts +3 -4
- package/examples/build-events/test/support/memory.ts +0 -5
- package/examples/build-events/test/webhooks.test.ts +0 -12
- package/examples/kitchen-sink/README.md +1 -1
- package/examples/kitchen-sink/src/Actions.ts +0 -10
- package/examples/kitchen-sink/src/Api.ts +0 -8
- package/examples/kitchen-sink/src/Rpc.ts +0 -4
- package/examples/kitchen-sink/src/admin/Admin.ts +0 -9
- package/examples/kitchen-sink/src/admin/authenticate.ts +2 -6
- package/examples/kitchen-sink/src/admin/authorize.ts +0 -5
- package/examples/kitchen-sink/src/admin/grants.ts +0 -5
- package/examples/kitchen-sink/src/admin/handlers.ts +0 -2
- package/examples/kitchen-sink/src/admin/keys.ts +1 -3
- package/examples/kitchen-sink/src/agent.ts +2 -13
- package/examples/kitchen-sink/src/assistant/Assistant.ts +7 -24
- package/examples/kitchen-sink/src/assistant/conversations.ts +3 -21
- package/examples/kitchen-sink/src/assistant/demo-model.ts +0 -5
- package/examples/kitchen-sink/src/assistant/handlers.ts +1 -30
- package/examples/kitchen-sink/src/attachments/Attachments.ts +0 -4
- package/examples/kitchen-sink/src/attachments/download.ts +0 -10
- package/examples/kitchen-sink/src/attachments/handlers.ts +0 -3
- package/examples/kitchen-sink/src/attachments/store.ts +0 -3
- package/examples/kitchen-sink/src/browser.ts +0 -9
- package/examples/kitchen-sink/src/cli.ts +0 -19
- package/examples/kitchen-sink/src/config.ts +3 -11
- package/examples/kitchen-sink/src/identity/Identity.ts +0 -18
- package/examples/kitchen-sink/src/identity/authenticate.ts +3 -14
- package/examples/kitchen-sink/src/identity/authorize.ts +0 -6
- package/examples/kitchen-sink/src/identity/directory.ts +8 -18
- package/examples/kitchen-sink/src/identity/handlers.ts +0 -3
- package/examples/kitchen-sink/src/identity/members.ts +1 -5
- package/examples/kitchen-sink/src/identity/quota.ts +0 -11
- package/examples/kitchen-sink/src/issues/Issues.ts +0 -24
- package/examples/kitchen-sink/src/issues/assistant.ts +0 -10
- package/examples/kitchen-sink/src/issues/events.ts +2 -16
- package/examples/kitchen-sink/src/issues/export.ts +0 -5
- package/examples/kitchen-sink/src/issues/guide.ts +0 -5
- package/examples/kitchen-sink/src/issues/handlers.ts +0 -5
- package/examples/kitchen-sink/src/issues/store.ts +19 -23
- package/examples/kitchen-sink/src/issues/write-limit.ts +0 -12
- package/examples/kitchen-sink/src/memory.ts +0 -9
- package/examples/kitchen-sink/src/ops.ts +0 -6
- package/examples/kitchen-sink/src/request-policy.ts +0 -17
- package/examples/kitchen-sink/src/routes.ts +0 -48
- package/examples/kitchen-sink/src/sdk.ts +0 -12
- package/examples/kitchen-sink/src/seed.ts +0 -10
- package/examples/kitchen-sink/src/server.ts +0 -22
- package/examples/kitchen-sink/src/stdio.ts +2 -12
- package/examples/kitchen-sink/src/workspaces/Workspaces.ts +0 -4
- package/examples/kitchen-sink/src/workspaces/handlers.ts +0 -4
- package/examples/kitchen-sink/src/workspaces/registry.ts +0 -5
- package/examples/kitchen-sink/src/workspaces/tenant.ts +0 -4
- package/examples/kitchen-sink/test/admin/admin.test.ts +0 -1
- package/examples/kitchen-sink/test/assistant/assistant.test.ts +0 -18
- package/examples/kitchen-sink/test/attachments/attachments.test.ts +0 -9
- package/examples/kitchen-sink/test/identity/identity.test.ts +0 -10
- package/examples/kitchen-sink/test/identity/session.test.ts +0 -10
- package/examples/kitchen-sink/test/issues/assistant.test.ts +3 -7
- package/examples/kitchen-sink/test/issues/events.test.ts +2 -9
- package/examples/kitchen-sink/test/issues/export.test.ts +0 -3
- package/examples/kitchen-sink/test/issues/issues.test.ts +6 -12
- package/examples/kitchen-sink/test/request-policy.test.ts +0 -6
- package/examples/kitchen-sink/test/routes.test.ts +0 -6
- package/examples/kitchen-sink/test/support/as.ts +3 -5
- package/examples/kitchen-sink/test/support/scripted.ts +0 -3
- package/examples/kitchen-sink/test/support/services.ts +0 -3
- package/examples/live-rpc/src/Rpc.ts +0 -4
- package/examples/live-rpc/src/browser.ts +0 -24
- package/examples/live-rpc/src/chat/Chat.ts +0 -22
- package/examples/live-rpc/src/chat/authorize.ts +0 -3
- package/examples/live-rpc/src/chat/events.ts +0 -14
- package/examples/live-rpc/src/chat/handlers.ts +0 -5
- package/examples/live-rpc/src/chat/room.ts +0 -6
- package/examples/live-rpc/src/chat/store.ts +0 -9
- package/examples/live-rpc/src/client.ts +0 -6
- package/examples/live-rpc/src/identity/Identity.ts +0 -6
- package/examples/live-rpc/src/identity/members.ts +1 -3
- package/examples/live-rpc/src/identity/verifier.ts +6 -12
- package/examples/live-rpc/src/page.ts +0 -10
- package/examples/live-rpc/src/routes.ts +0 -13
- package/examples/live-rpc/src/server.ts +0 -16
- package/examples/live-rpc/test/browser.test.ts +0 -9
- package/examples/live-rpc/test/chat/chat.test.ts +3 -11
- package/examples/live-rpc/test/routes.test.ts +0 -8
- package/examples/live-rpc/test/server.test.ts +0 -13
- package/examples/mcp-tools/src/instructions.ts +0 -4
- package/examples/mcp-tools/src/platform/fake-farm.ts +0 -6
- package/examples/mcp-tools/src/platform/print-farm.ts +0 -16
- package/examples/mcp-tools/src/printers/Printers.ts +0 -21
- package/examples/mcp-tools/src/printers/farm.ts +0 -5
- package/examples/mcp-tools/src/printers/handlers.ts +0 -6
- package/examples/mcp-tools/src/routes.ts +0 -11
- package/examples/mcp-tools/src/server.ts +0 -9
- package/examples/mcp-tools/src/stdio.ts +1 -13
- package/examples/mcp-tools/test/printers/printers.test.ts +0 -10
- package/examples/mcp-tools/test/routes.test.ts +0 -14
- package/examples/notes-service/src/Api.ts +0 -4
- package/examples/notes-service/src/browser.ts +0 -12
- package/examples/notes-service/src/cli.ts +0 -2
- package/examples/notes-service/src/commands.ts +0 -30
- package/examples/notes-service/src/identity/Identity.ts +0 -2
- package/examples/notes-service/src/identity/authorize.ts +0 -4
- package/examples/notes-service/src/identity/users.ts +1 -3
- package/examples/notes-service/src/identity/verifier.ts +4 -11
- package/examples/notes-service/src/notes/Notes.ts +0 -12
- package/examples/notes-service/src/notes/handlers.ts +0 -4
- package/examples/notes-service/src/notes/store.ts +62 -48
- package/examples/notes-service/src/routes.ts +0 -12
- package/examples/notes-service/src/server.ts +0 -6
- package/examples/notes-service/src/status/Status.ts +0 -1
- package/examples/notes-service/src/status/handlers.ts +0 -1
- package/examples/notes-service/src/stdio.ts +2 -12
- package/examples/notes-service/src/worker.ts +0 -7
- package/examples/notes-service/test/commands.test.ts +0 -20
- package/examples/notes-service/test/notes/as.ts +3 -4
- package/examples/notes-service/test/notes/in-process.test.ts +3 -6
- package/examples/notes-service/test/notes/notes.test.ts +0 -6
- package/examples/notes-service/test/routes.test.ts +0 -5
- package/examples/notes-service/test/worker.test.ts +0 -1
- package/examples/quickstart/src/Api.ts +0 -1
- package/examples/quickstart/src/client.ts +0 -3
- package/examples/quickstart/src/greeting/Greeting.ts +0 -2
- package/examples/quickstart/src/greeting/handlers.ts +0 -1
- package/examples/quickstart/src/routes.ts +0 -1
- package/examples/quickstart/src/server.ts +3 -6
- package/examples/quickstart/test/routes.test.ts +0 -1
- package/package.json +6 -6
|
@@ -10,9 +10,6 @@ const idOf = Schema.decodeUnknownOption(
|
|
|
10
10
|
Schema.NumberFromString.pipe(Schema.check(Schema.isInt()))
|
|
11
11
|
);
|
|
12
12
|
|
|
13
|
-
// A cursor this source gives is `<epoch>:<id>`, the store's epoch and the id of the last message
|
|
14
|
-
// a page holds, so one from before a restart of the memory store is told apart. A page starting
|
|
15
|
-
// a watch gives the id alone, as `readMessages`' `after` is, which is the store's now.
|
|
16
13
|
const positionOf = (epoch: string, cursor: string | null) => {
|
|
17
14
|
const parts =
|
|
18
15
|
cursor?.includes(":") === true ? cursor.split(":") : [epoch, cursor];
|
|
@@ -22,9 +19,6 @@ const positionOf = (epoch: string, cursor: string | null) => {
|
|
|
22
19
|
: Option.none();
|
|
23
20
|
};
|
|
24
21
|
|
|
25
|
-
// The room's messages as an event, read from the store that keeps them: a check source.
|
|
26
|
-
// `postMessage` notifies after each post, so a page receives it at once, not at its next read
|
|
27
|
-
// every 5 seconds.
|
|
28
22
|
export const messageEvents = Event.implement(
|
|
29
23
|
MessagePosted,
|
|
30
24
|
Effect.gen(function* () {
|
|
@@ -41,9 +35,6 @@ export const messageEvents = Event.implement(
|
|
|
41
35
|
(id) => id >= 0 && id <= last
|
|
42
36
|
);
|
|
43
37
|
|
|
44
|
-
// No cursor starts after the room's last message. One this room never gave, such as
|
|
45
|
-
// one from before a restart, is answered `truncated` from there: the page learns its
|
|
46
|
-
// messages may be another run's, and reads the room again with `readMessages`.
|
|
47
38
|
if (Option.isNone(after)) {
|
|
48
39
|
return {
|
|
49
40
|
cursor: `${store.epoch}:${last}`,
|
|
@@ -56,17 +47,12 @@ export const messageEvents = Event.implement(
|
|
|
56
47
|
|
|
57
48
|
return {
|
|
58
49
|
cursor: `${store.epoch}:${messages.at(-1)?.id ?? after.value}`,
|
|
59
|
-
// A message's id counts within its room and the store's epoch, so the event's id
|
|
60
|
-
// names all three: a watch drops an id it has seen, and after a restart of the memory
|
|
61
|
-
// store `lobby:1` is another message.
|
|
62
50
|
events: messages.map((data) => ({
|
|
63
51
|
eventId: `${store.epoch}:${room}:${data.id}`,
|
|
64
52
|
data,
|
|
65
53
|
})),
|
|
66
54
|
};
|
|
67
55
|
}),
|
|
68
|
-
// A notify for one room wakes that room's pages alone: the key is the room the watch's
|
|
69
|
-
// header names, read as `check` reads it.
|
|
70
56
|
key: () => CurrentRoom,
|
|
71
57
|
};
|
|
72
58
|
}),
|
|
@@ -8,7 +8,6 @@ import { ListRooms, MessagePosted, Messages } from "./Chat.js";
|
|
|
8
8
|
import { CurrentRoom } from "./room.js";
|
|
9
9
|
import { MessageStore } from "./store.js";
|
|
10
10
|
|
|
11
|
-
// A public action takes no `authorize`: nothing would run it.
|
|
12
11
|
export const rooms = Action.implement(
|
|
13
12
|
ListRooms,
|
|
14
13
|
Effect.gen(function* () {
|
|
@@ -18,8 +17,6 @@ export const rooms = Action.implement(
|
|
|
18
17
|
})
|
|
19
18
|
);
|
|
20
19
|
|
|
21
|
-
// The builder runs once, at startup: it is where a handler gets its startup services. The
|
|
22
|
-
// caller and the room are read per call, inside each handler, because every message has its own.
|
|
23
20
|
export const messages = Action.implement(
|
|
24
21
|
Messages,
|
|
25
22
|
Effect.gen(function* () {
|
|
@@ -33,8 +30,6 @@ export const messages = Action.implement(
|
|
|
33
30
|
|
|
34
31
|
return { messages: yield* store.read(room, after) };
|
|
35
32
|
}),
|
|
36
|
-
// The message is in the store, which `MessagePosted`'s check reads: notifying wakes the
|
|
37
|
-
// pages watching this room to read it now.
|
|
38
33
|
postMessage: ({ text }) =>
|
|
39
34
|
Effect.gen(function* () {
|
|
40
35
|
const room = yield* CurrentRoom;
|
|
@@ -11,17 +11,11 @@ export class CurrentRoom extends Context.Service<CurrentRoom, string>()(
|
|
|
11
11
|
"live-rpc/CurrentRoom"
|
|
12
12
|
) {}
|
|
13
13
|
|
|
14
|
-
// Native Effect RPC middleware, declared as providing `CurrentRoom`. Listed in
|
|
15
|
-
// `ActionRpc.layer`'s `middleware`, it runs once per message.
|
|
16
14
|
export class ResolveRoom extends RpcMiddleware.Service<
|
|
17
15
|
ResolveRoom,
|
|
18
16
|
{ provides: CurrentRoom }
|
|
19
17
|
>()("live-rpc/ResolveRoom") {}
|
|
20
18
|
|
|
21
|
-
// The middleware's implementation. It gets the message's headers, which over a WebSocket are
|
|
22
|
-
// the upgrade request's merged with the message's own, and runs the rest of the call with the
|
|
23
|
-
// room they name, or the default room when they name none. It refuses nothing: whether the
|
|
24
|
-
// caller may be in that room is `authorize`'s rule.
|
|
25
19
|
export const resolveRoom = Layer.succeed(ResolveRoom, (call, { headers }) =>
|
|
26
20
|
Effect.provideService(call, CurrentRoom, headers[roomHeader] ?? defaultRoom)
|
|
27
21
|
);
|
|
@@ -3,14 +3,9 @@ import { Context, Effect, Layer } from "effect";
|
|
|
3
3
|
import { MessageNotFound, NotAuthor } from "./Chat.js";
|
|
4
4
|
import type { Message } from "./Chat.js";
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* Where messages live, by room. That only its author deletes a message is checked here, in the
|
|
8
|
-
* data access, so no surface and no handler can skip it.
|
|
9
|
-
*/
|
|
10
6
|
export class MessageStore extends Context.Service<
|
|
11
7
|
MessageStore,
|
|
12
8
|
{
|
|
13
|
-
/** The store's epoch: a message's id is unique within it, and a restarted memory store starts another. */
|
|
14
9
|
readonly epoch: string;
|
|
15
10
|
readonly rooms: Effect.Effect<
|
|
16
11
|
ReadonlyArray<{ name: string; messages: number }>
|
|
@@ -19,7 +14,6 @@ export class MessageStore extends Context.Service<
|
|
|
19
14
|
room: string,
|
|
20
15
|
after: number
|
|
21
16
|
) => Effect.Effect<readonly Message[]>;
|
|
22
|
-
/** The id of the room's last message posted, kept or deleted: 0 for an empty room. */
|
|
23
17
|
readonly last: (room: string) => Effect.Effect<number>;
|
|
24
18
|
readonly post: (
|
|
25
19
|
room: string,
|
|
@@ -33,9 +27,6 @@ export class MessageStore extends Context.Service<
|
|
|
33
27
|
) => Effect.Effect<void, MessageNotFound | NotAuthor>;
|
|
34
28
|
}
|
|
35
29
|
>()("live-rpc/MessageStore") {
|
|
36
|
-
// DEMO ONLY: memory, lost when the process stops, its ids then counting from 1 again, so each
|
|
37
|
-
// start is another epoch. A real store is another Layer of this service, chosen by the
|
|
38
|
-
// entrypoint, whose ids never repeat.
|
|
39
30
|
static readonly layerMemory = Layer.sync(MessageStore, () => {
|
|
40
31
|
const epoch = crypto.randomUUID();
|
|
41
32
|
|
|
@@ -3,12 +3,9 @@ import { Config, Console, Effect, Layer, Stream } from "effect";
|
|
|
3
3
|
|
|
4
4
|
import { connection, openChat } from "./browser.js";
|
|
5
5
|
|
|
6
|
-
// Entrypoint: `node --import tsx examples/live-rpc/src/client.ts`, with the server running.
|
|
7
|
-
// The page's client, run from Node: the same `openChat`, given Node's WebSocket.
|
|
8
6
|
const visit = Effect.gen(function* () {
|
|
9
7
|
const chat = yield* openChat;
|
|
10
8
|
|
|
11
|
-
// Two members on the one socket.
|
|
12
9
|
const ada = chat.room("lobby", "ada");
|
|
13
10
|
const grace = chat.room("lobby", "grace");
|
|
14
11
|
|
|
@@ -18,8 +15,6 @@ const visit = Effect.gen(function* () {
|
|
|
18
15
|
|
|
19
16
|
yield* Console.log("ada posts:", posted);
|
|
20
17
|
yield* Console.log("grace reads:", yield* grace.read(posted.id - 1));
|
|
21
|
-
// A watch from the last id grace held before the post: it sends the post, then would send
|
|
22
|
-
// each one after it, until taken.
|
|
23
18
|
yield* Console.log(
|
|
24
19
|
"grace watches:",
|
|
25
20
|
yield* grace.messages(posted.id - 1).pipe(Stream.take(1), Stream.runCollect)
|
|
@@ -41,7 +36,6 @@ const main = Effect.gen(function* () {
|
|
|
41
36
|
Config.withDefault("ws://127.0.0.1:3000/rpc")
|
|
42
37
|
);
|
|
43
38
|
|
|
44
|
-
// One socket for the whole visit, so both members' calls share it.
|
|
45
39
|
yield* visit.pipe(
|
|
46
40
|
Effect.scoped,
|
|
47
41
|
Effect.provide(
|
|
@@ -6,14 +6,8 @@ export interface Member {
|
|
|
6
6
|
readonly rooms: readonly string[];
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
/**
|
|
10
|
-
* Who sent a message. A protected action declares it, `caller: CurrentMember`; authentication
|
|
11
|
-
* provides it for each message, and a test calling in process provides it itself.
|
|
12
|
-
*/
|
|
13
9
|
export class CurrentMember extends Context.Service<CurrentMember, Member>()(
|
|
14
10
|
"live-rpc/CurrentMember"
|
|
15
11
|
) {}
|
|
16
12
|
|
|
17
|
-
// How a remote caller proves it is CurrentMember: a bearer token, the default scheme. Clients
|
|
18
|
-
// import this descriptor with the binding; its verifier, in verifier.ts, stays on the server.
|
|
19
13
|
export const Login = Authentication.make("live-rpc.Login", CurrentMember);
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { Member } from "./Identity.js";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
// has no such table: its verifier reads the member from a signed token.
|
|
5
|
-
export const members = {
|
|
3
|
+
export const demoMembers = {
|
|
6
4
|
ada: { id: "ada", rooms: ["lobby", "ops"] },
|
|
7
5
|
grace: { id: "grace", rooms: ["lobby"] },
|
|
8
6
|
} as const satisfies Readonly<Record<string, Member>>;
|
|
@@ -3,25 +3,19 @@ import * as Authentication from "@gjermundgaraba/effect-actions/Authentication";
|
|
|
3
3
|
import { Effect, Redacted } from "effect";
|
|
4
4
|
|
|
5
5
|
import { Login } from "./Identity.js";
|
|
6
|
-
import {
|
|
6
|
+
import { demoMembers } from "./members.js";
|
|
7
7
|
|
|
8
|
-
const isToken = (token: string): token is keyof typeof
|
|
9
|
-
Object.hasOwn(
|
|
8
|
+
const isToken = (token: string): token is keyof typeof demoMembers =>
|
|
9
|
+
Object.hasOwn(demoMembers, token);
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
// authorization server's library, its audience included. An unknown token is the built-in
|
|
13
|
-
// `Unauthenticated`, which every client decodes.
|
|
14
|
-
const verify = (token: Redacted.Redacted) => {
|
|
11
|
+
const demoVerify = (token: Redacted.Redacted) => {
|
|
15
12
|
const name = Redacted.value(token);
|
|
16
13
|
|
|
17
14
|
return isToken(name)
|
|
18
|
-
? Effect.succeed(
|
|
15
|
+
? Effect.succeed(demoMembers[name])
|
|
19
16
|
: Effect.fail(
|
|
20
17
|
new Action.Unauthenticated({ message: "Unknown demo token." })
|
|
21
18
|
);
|
|
22
19
|
};
|
|
23
20
|
|
|
24
|
-
|
|
25
|
-
// once for the connection, so a socket has no identity of its own: each call is whoever its
|
|
26
|
-
// token says. It publishes no OAuth discovery, so it needs no router and no configuration.
|
|
27
|
-
export const authenticate = Authentication.layer(Login, verify);
|
|
21
|
+
export const authenticate = Authentication.layer(Login, demoVerify);
|
|
@@ -6,10 +6,6 @@ import { Socket } from "effect/socket";
|
|
|
6
6
|
import { connection, openChat } from "./browser.js";
|
|
7
7
|
import type { MessagePosted } from "./chat/Chat.js";
|
|
8
8
|
|
|
9
|
-
// The chat for a page that is not one Effect program: the same client, each call a promise.
|
|
10
|
-
// A `ManagedRuntime` keeps the protocol's layer, and so the socket, between calls. It builds
|
|
11
|
-
// the layer on the first call, which is when the socket opens, and `close` releases it. A
|
|
12
|
-
// failure `openChat` does not handle, such as `InvalidInput`, rejects the promise.
|
|
13
9
|
class Chat extends Context.Service<Chat, Effect.Success<typeof openChat>>()(
|
|
14
10
|
"live-rpc/Chat"
|
|
15
11
|
) {}
|
|
@@ -33,10 +29,6 @@ export const chatOver = <E>(protocol: Layer.Layer<RpcClient.Protocol, E>) => {
|
|
|
33
29
|
await call((chat) => chat.room(name, token).post(text)),
|
|
34
30
|
remove: async (id: number) =>
|
|
35
31
|
await call((chat) => chat.room(name, token).remove(id)),
|
|
36
|
-
// Calls `receive` with each message posted after `after`, the id of the last message the
|
|
37
|
-
// page holds, and each gap, as `messages` streams them, until the page calls the function
|
|
38
|
-
// it returns. `ended` is told why it stopped: a refusal, such as a token that expired by
|
|
39
|
-
// the time the watch connected again, or anything else, the page's own call included.
|
|
40
32
|
watch: (
|
|
41
33
|
after: number,
|
|
42
34
|
receive: (received: Event.Received<typeof MessagePosted>) => void,
|
|
@@ -72,8 +64,6 @@ export const chatOver = <E>(protocol: Layer.Layer<RpcClient.Protocol, E>) => {
|
|
|
72
64
|
};
|
|
73
65
|
};
|
|
74
66
|
|
|
75
|
-
// What a page calls: `const chat = connect("wss://chat.example/rpc")`, on the browser's own
|
|
76
|
-
// WebSocket, then `await chat.room("lobby", token).post("Hello")`.
|
|
77
67
|
export const connect = (url: string) =>
|
|
78
68
|
chatOver(
|
|
79
69
|
connection(url).pipe(Layer.provide(Socket.layerWebSocketConstructorGlobal))
|
|
@@ -7,19 +7,6 @@ import { ResolveRoom, resolveRoom } from "./chat/room.js";
|
|
|
7
7
|
import { authenticate } from "./identity/verifier.js";
|
|
8
8
|
import { Rpc } from "./Rpc.js";
|
|
9
9
|
|
|
10
|
-
// The binding's rpcs on Effect's own RpcServer, the protected ones behind `Login`'s verifier;
|
|
11
|
-
// `listRooms` needs no token.
|
|
12
|
-
//
|
|
13
|
-
// `ResolveRoom`, listed in `middleware`, provides `CurrentRoom`, which the handlers, the event's
|
|
14
|
-
// source and `authorize` read; on a watch it runs as the watch starts, and each time it connects
|
|
15
|
-
// again. Only something that runs per message can, since each message names its own room:
|
|
16
|
-
// `Layer.succeed(CurrentRoom, "lobby")` provided here would leave the layer still requiring it,
|
|
17
|
-
// in its type.
|
|
18
|
-
//
|
|
19
|
-
// It still requires four things, each chosen by whoever serves it. The protocol and its
|
|
20
|
-
// serialization: server.ts gives it a WebSocket, and a test Effect's HTTP protocol, which
|
|
21
|
-
// answers in memory. `MessageStore`, and `Event.Events`, which `postMessage` notifies and the
|
|
22
|
-
// watches wait on: one of each, provided around the whole server.
|
|
23
10
|
export const rpc = ActionRpc.layer(Rpc, [rooms, messages, messageEvents], {
|
|
24
11
|
middleware: [ResolveRoom],
|
|
25
12
|
}).pipe(Layer.provide([resolveRoom, authenticate]));
|
|
@@ -9,13 +9,6 @@ import { RpcSerialization, RpcServer } from "effect/rpc";
|
|
|
9
9
|
import { MessageStore } from "./chat/store.js";
|
|
10
10
|
import { rpc } from "./routes.js";
|
|
11
11
|
|
|
12
|
-
// Entrypoint: `node --import tsx examples/live-rpc/src/server.ts`.
|
|
13
|
-
// It chooses the protocol, the store and the port, and starts Node's HTTP server. It provides
|
|
14
|
-
// no caller and no room: those arrive with each message.
|
|
15
|
-
|
|
16
|
-
// The rpcs over a WebSocket at `/rpc`, the router's one route: Node's HTTP server answers the
|
|
17
|
-
// upgrade. JSON, because a page's client speaks it: browser.ts gives its socket the same
|
|
18
|
-
// serialization.
|
|
19
12
|
const socket = rpc.pipe(
|
|
20
13
|
Layer.provide(RpcServer.layerProtocolWebsocket({ path: "/rpc" })),
|
|
21
14
|
Layer.provide(RpcSerialization.layerJson)
|
|
@@ -23,7 +16,6 @@ const socket = rpc.pipe(
|
|
|
23
16
|
|
|
24
17
|
const server = Layer.unwrap(
|
|
25
18
|
Effect.gen(function* () {
|
|
26
|
-
// The loopback default serves this machine alone; HOST=0.0.0.0 lets others reach it.
|
|
27
19
|
const host = yield* Config.String("HOST").pipe(
|
|
28
20
|
Config.withDefault("127.0.0.1")
|
|
29
21
|
);
|
|
@@ -35,18 +27,10 @@ const server = Layer.unwrap(
|
|
|
35
27
|
NodeHttpServer.layer(createServer, {
|
|
36
28
|
host,
|
|
37
29
|
port,
|
|
38
|
-
// A message on a WebSocket is no request body, so `HttpServerRequest.MaxBodySize`
|
|
39
|
-
// does not bound it: Node's WebSocket server reads one of up to 100 MiB whole unless
|
|
40
|
-
// told otherwise. A chat message is short, and a larger one closes the socket (1009).
|
|
41
30
|
websocket: { maxPayload: 64 * 1024 },
|
|
42
31
|
})
|
|
43
32
|
),
|
|
44
|
-
// DEMO ONLY: messages live in this process's memory; a deployment provides a store that
|
|
45
|
-
// lasts.
|
|
46
33
|
Layer.provide(MessageStore.layerMemory),
|
|
47
|
-
// The pages watching a room are this process's sockets, so what wakes them lives in its
|
|
48
|
-
// memory too: one for the whole server. Several processes behind a balancer would each
|
|
49
|
-
// wake only their own pages, the others reading a post at their next read, 5 seconds on.
|
|
50
34
|
Layer.provide(Event.layerMemory)
|
|
51
35
|
);
|
|
52
36
|
})
|
|
@@ -11,9 +11,6 @@ import { MessageStore } from "../src/chat/store.js";
|
|
|
11
11
|
import { chatOver } from "../src/page.js";
|
|
12
12
|
import { rpc } from "../src/routes.js";
|
|
13
13
|
|
|
14
|
-
// The page's client against the real rpcs, in memory, served as in routes.test.ts, NDJSON
|
|
15
|
-
// included. `openChat` needs a client protocol and does not care which: a page gives it
|
|
16
|
-
// `connection(url)`, a WebSocket, and this test Effect's HTTP protocol.
|
|
17
14
|
const served = RpcClient.layerProtocolHttp({ url: "/rpc" }).pipe(
|
|
18
15
|
Layer.provide(RpcSerialization.layerNdjson),
|
|
19
16
|
Layer.provide(
|
|
@@ -108,8 +105,6 @@ describe("the page's client", () => {
|
|
|
108
105
|
|
|
109
106
|
const { messages } = yield* lobby.read(0);
|
|
110
107
|
|
|
111
|
-
// Posted after the page read and before it watches: the watch from the last id read
|
|
112
|
-
// still sends it.
|
|
113
108
|
yield* chat.room("lobby", "ada").post("Done.");
|
|
114
109
|
|
|
115
110
|
const watched = yield* lobby
|
|
@@ -128,7 +123,6 @@ describe("the page's client", () => {
|
|
|
128
123
|
Effect.gen(function* () {
|
|
129
124
|
const chat = yield* openChat;
|
|
130
125
|
|
|
131
|
-
// As after a restart emptied the demo's memory store: the page holds id 7.
|
|
132
126
|
const watching = yield* chat
|
|
133
127
|
.room("lobby", "grace")
|
|
134
128
|
.messages(7)
|
|
@@ -144,8 +138,6 @@ describe("the page's client", () => {
|
|
|
144
138
|
}).pipe(Effect.provide(served))
|
|
145
139
|
);
|
|
146
140
|
|
|
147
|
-
// `connect(url)` is this with a WebSocket. Each test's runtime builds its own layer, and so
|
|
148
|
-
// its own store.
|
|
149
141
|
it("answers a page that is not an Effect program with promises, until it closes", async () => {
|
|
150
142
|
const chat = chatOver(served);
|
|
151
143
|
|
|
@@ -171,7 +163,6 @@ describe("the page's client", () => {
|
|
|
171
163
|
{ name: "lobby", messages: 1 },
|
|
172
164
|
]);
|
|
173
165
|
|
|
174
|
-
// A failure `openChat` does not handle rejects: here, a text the contract refuses.
|
|
175
166
|
await expect(chat.room("lobby", "ada").post("")).rejects.toBeInstanceOf(
|
|
176
167
|
Action.InvalidInput
|
|
177
168
|
);
|
|
@@ -10,15 +10,11 @@ import { messages } from "../../src/chat/handlers.js";
|
|
|
10
10
|
import { CurrentRoom } from "../../src/chat/room.js";
|
|
11
11
|
import { MessageStore } from "../../src/chat/store.js";
|
|
12
12
|
import { CurrentMember } from "../../src/identity/Identity.js";
|
|
13
|
-
import {
|
|
13
|
+
import { demoMembers } from "../../src/identity/members.js";
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
// `authorize` and handler a server runs. No authentication and no middleware run here, so
|
|
17
|
-
// each call is given what they would provide: its caller and its room. Neither is provided
|
|
18
|
-
// around the client or beside the store: a request-time service belongs to one call.
|
|
19
|
-
const as = (member: keyof typeof members, room: string) =>
|
|
15
|
+
const as = (member: keyof typeof demoMembers, room: string) =>
|
|
20
16
|
flow(
|
|
21
|
-
Effect.provideService(CurrentMember,
|
|
17
|
+
Effect.provideService(CurrentMember, demoMembers[member]),
|
|
22
18
|
Effect.provideService(CurrentRoom, room)
|
|
23
19
|
);
|
|
24
20
|
|
|
@@ -122,14 +118,12 @@ describe("messages, in process", () => {
|
|
|
122
118
|
Effect.forkChild
|
|
123
119
|
);
|
|
124
120
|
|
|
125
|
-
// The watch starts from now: let it take its first position before anyone posts.
|
|
126
121
|
yield* TestClock.adjust("0 millis");
|
|
127
122
|
yield* client
|
|
128
123
|
.postMessage({ text: "Disk is full." })
|
|
129
124
|
.pipe(as("ada", "ops"));
|
|
130
125
|
yield* client.postMessage({ text: "Ready." }).pipe(as("ada", "lobby"));
|
|
131
126
|
|
|
132
|
-
// `postMessage` notifies: the watch reads at once, with no wait for its next read.
|
|
133
127
|
expect(yield* Fiber.join(watching)).toMatchObject([
|
|
134
128
|
{
|
|
135
129
|
_tag: "Occurrence",
|
|
@@ -166,7 +160,6 @@ describe("messages, in process", () => {
|
|
|
166
160
|
|
|
167
161
|
const received = [...(yield* firstReceived), ...(yield* firstReceived)];
|
|
168
162
|
|
|
169
|
-
// Both are the room's message 1, and a watch that saw the first still reads the second.
|
|
170
163
|
expect(received).toMatchObject([
|
|
171
164
|
{ data: { id: 1 } },
|
|
172
165
|
{ data: { id: 1 } },
|
|
@@ -204,7 +197,6 @@ describe("messages, in process", () => {
|
|
|
204
197
|
|
|
205
198
|
yield* client.postMessage({ text: "Ready." }).pipe(as("ada", "lobby"));
|
|
206
199
|
|
|
207
|
-
// An event's id, such as `<epoch>:lobby:1`, is no cursor: the cursor is `<epoch>:<id>`.
|
|
208
200
|
const received = yield* client
|
|
209
201
|
.watch(MessagePosted, {}, { cursor: "lobby:1" })
|
|
210
202
|
.pipe(Stream.take(1), Stream.runCollect, as("grace", "lobby"));
|
|
@@ -12,10 +12,6 @@ import { MessageStore } from "../src/chat/store.js";
|
|
|
12
12
|
import { rpc } from "../src/routes.js";
|
|
13
13
|
import { Rpc } from "../src/Rpc.js";
|
|
14
14
|
|
|
15
|
-
// What the wire adds: the real rpcs, behind their real authentication and middleware,
|
|
16
|
-
// answering in memory. The test gives them Effect's HTTP protocol, not the server's WebSocket,
|
|
17
|
-
// because `Testing.layer` answers HTTP requests; no port is opened. NDJSON, not JSON: over
|
|
18
|
-
// HTTP, JSON sends a response once all its streams end, so a watch would never answer.
|
|
19
15
|
const server = rpc.pipe(
|
|
20
16
|
Layer.provide(RpcServer.layerProtocolHttp({ path: "/rpc" })),
|
|
21
17
|
Layer.provide(RpcSerialization.layerNdjson)
|
|
@@ -27,7 +23,6 @@ const served = RpcClient.layerProtocolHttp({ url: "/rpc" }).pipe(
|
|
|
27
23
|
Layer.provide([MessageStore.layerMemory, Event.layerMemory])
|
|
28
24
|
);
|
|
29
25
|
|
|
30
|
-
// Each call carries its caller's token, so one client serves every caller of a test.
|
|
31
26
|
const as = (token: string) =>
|
|
32
27
|
RpcClient.withHeaders({ authorization: `Bearer ${token}` });
|
|
33
28
|
|
|
@@ -134,7 +129,6 @@ describe("the chat, over RPC", () => {
|
|
|
134
129
|
Effect.forkChild
|
|
135
130
|
);
|
|
136
131
|
|
|
137
|
-
// Let the watch take its position through the in-memory socket before anyone posts.
|
|
138
132
|
yield* Effect.repeat(TestClock.adjust("0 millis"), { times: 10 });
|
|
139
133
|
yield* client
|
|
140
134
|
.postMessage({ text: "Disk is full." })
|
|
@@ -174,8 +168,6 @@ describe("the chat, over RPC", () => {
|
|
|
174
168
|
"serves a native RpcGroup: Effect's own client calls the same rpcs",
|
|
175
169
|
() =>
|
|
176
170
|
Effect.gen(function* () {
|
|
177
|
-
// `Rpc.group` is what Effect's RPC tooling takes. Its methods take the payload as sent,
|
|
178
|
-
// `{}` included; `ActionRpc.client` is the one to write an application with.
|
|
179
171
|
const native = yield* RpcClient.make(Rpc.group);
|
|
180
172
|
|
|
181
173
|
expect(yield* native.listRooms({})).toStrictEqual({ rooms: [] });
|
|
@@ -11,23 +11,17 @@ import { MessageStore } from "../src/chat/store.js";
|
|
|
11
11
|
import { connect } from "../src/page.js";
|
|
12
12
|
import { rpc } from "../src/routes.js";
|
|
13
13
|
|
|
14
|
-
// What the socket adds, which no in-memory test reaches: the upgrade, the page's `connect(url)`
|
|
15
|
-
// and the bound on a message. The rpcs are served as server.ts serves them, over a WebSocket
|
|
16
|
-
// at `/rpc` on Node's HTTP server, on port 0: the system picks a free one, so tests run side
|
|
17
|
-
// by side. The server closes with the test's scope.
|
|
18
14
|
const socket = rpc.pipe(
|
|
19
15
|
Layer.provide(RpcServer.layerProtocolWebsocket({ path: "/rpc" })),
|
|
20
16
|
Layer.provide(RpcSerialization.layerJson)
|
|
21
17
|
);
|
|
22
18
|
|
|
23
|
-
// server.ts allows 64 KiB. A bound this small is passed by a message the contract accepts.
|
|
24
19
|
const maxPayload = 256;
|
|
25
20
|
|
|
26
21
|
const listening = HttpRouter.serve(socket, {
|
|
27
22
|
disableLogger: true,
|
|
28
23
|
disableListenLog: true,
|
|
29
24
|
}).pipe(
|
|
30
|
-
// `provideMerge` keeps `HttpServer` for the test, which reads the port from it.
|
|
31
25
|
Layer.provideMerge(
|
|
32
26
|
NodeHttpServer.layer(createServer, {
|
|
33
27
|
host: "127.0.0.1",
|
|
@@ -38,8 +32,6 @@ const listening = HttpRouter.serve(socket, {
|
|
|
38
32
|
Layer.provide([MessageStore.layerMemory, Event.layerMemory])
|
|
39
33
|
);
|
|
40
34
|
|
|
41
|
-
// The page's client, on Node's global `WebSocket`. Each test closes it with `Effect.scoped`,
|
|
42
|
-
// before the server stops: a server that is stopping waits for its open sockets.
|
|
43
35
|
const chat = Effect.gen(function* () {
|
|
44
36
|
const server = yield* HttpServer.HttpServer;
|
|
45
37
|
const url = HttpServer.formatAddress(server.address).replace("http", "ws");
|
|
@@ -88,8 +80,6 @@ describe("the chat, over a WebSocket", () => {
|
|
|
88
80
|
const received = yield* Deferred.make<string>();
|
|
89
81
|
const stopped = yield* Deferred.make<string>();
|
|
90
82
|
|
|
91
|
-
// From the room's start, id 0, so the post reaches it however soon after the watch it is
|
|
92
|
-
// sent.
|
|
93
83
|
const stop = page.room("lobby", "grace").watch(
|
|
94
84
|
0,
|
|
95
85
|
(message) => {
|
|
@@ -123,9 +113,6 @@ describe("the chat, over a WebSocket", () => {
|
|
|
123
113
|
|
|
124
114
|
yield* Effect.promise(async () => await ada.post("Short."));
|
|
125
115
|
|
|
126
|
-
// 400 characters are a text the contract accepts and a message over 256 bytes. The server
|
|
127
|
-
// reads none of it: it closes the socket with 1009, "message too big", and the call fails
|
|
128
|
-
// with Effect's `RpcClientError`, its reason the socket's close, which no action declares.
|
|
129
116
|
yield* Effect.promise(async () => {
|
|
130
117
|
await expect(ada.post("x".repeat(400))).rejects.toMatchObject({
|
|
131
118
|
reason: { code: 1009 },
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// What the server tells a client of itself, on either transport: `server.ts` and `stdio.ts`
|
|
2
|
-
// both give it. `instructions` are for the model, as the tools' descriptions are: what the
|
|
3
|
-
// tools are for together, and what to be careful with. A client shows them to the model once,
|
|
4
|
-
// when it connects.
|
|
5
1
|
export const serverInfo = {
|
|
6
2
|
name: "print-farm",
|
|
7
3
|
version: "1.0.0",
|
|
@@ -5,12 +5,6 @@ import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
|
|
|
5
5
|
|
|
6
6
|
import type { Printer } from "../printers/Printers.js";
|
|
7
7
|
|
|
8
|
-
// DEMO ONLY: the farm's HTTP API answered in memory, so the example and its tests run without
|
|
9
|
-
// a farm. It is an `HttpClient`, so `printFarm` sends it the requests it would send over
|
|
10
|
-
// the network. A real application has no such file: its entrypoints provide a real client,
|
|
11
|
-
// `FetchHttpClient.layer` from `effect/http`.
|
|
12
|
-
|
|
13
|
-
// A one-pixel PNG stands in for a camera's picture.
|
|
14
8
|
export const picture = Uint8Array.from(
|
|
15
9
|
Buffer.from(
|
|
16
10
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
|
|
@@ -14,13 +14,8 @@ const fail = (message: string) => Effect.fail(new PrintFarmError({ message }));
|
|
|
14
14
|
|
|
15
15
|
const unreadable = "The print farm sent an answer this server could not read.";
|
|
16
16
|
|
|
17
|
-
// An id is written into the path as it is: the contract admits only letters, digits,
|
|
18
|
-
// `_` and `-`, so it is always one segment. Encoding would not make other text safe:
|
|
19
|
-
// `encodeURIComponent` leaves `..` alone, and a URL parser then walks up the path.
|
|
20
17
|
const at = (id: string, rest = "") => `/api/printers/${id}${rest}`;
|
|
21
18
|
|
|
22
|
-
// The farm answers a request it refuses with a status that is not 2xx and `{ "error": "..." }`.
|
|
23
|
-
// Its reason is passed on to the model, which can act on "The printer is busy with a job".
|
|
24
19
|
const refuseUnlessOk = HttpClientResponse.matchStatus({
|
|
25
20
|
"2xx": (response) => Effect.succeed(response),
|
|
26
21
|
orElse: (response) =>
|
|
@@ -35,9 +30,6 @@ const refuseUnlessOk = HttpClientResponse.matchStatus({
|
|
|
35
30
|
),
|
|
36
31
|
});
|
|
37
32
|
|
|
38
|
-
// Every other way a call to the farm fails, as the same declared error: no connection, an
|
|
39
|
-
// answer of another shape than expected, or none in time. Nothing of the HTTP client's own
|
|
40
|
-
// errors leaves this file, so no tool can fail with something its contract does not declare.
|
|
41
33
|
const asPrintFarmError = <A>(
|
|
42
34
|
effect: Effect.Effect<
|
|
43
35
|
A,
|
|
@@ -71,10 +63,6 @@ const asImage = (response: HttpClientResponse.HttpClientResponse) =>
|
|
|
71
63
|
return { data: new Uint8Array(yield* response.arrayBuffer), mimeType };
|
|
72
64
|
});
|
|
73
65
|
|
|
74
|
-
// Built once, when the process starts. It requires an `HttpClient` and reads its settings
|
|
75
|
-
// from `Config`: the entrypoint decides which client, and the environment says which farm.
|
|
76
|
-
// Not `fetch`: as a service, a test replaces the network alone and opens no port
|
|
77
|
-
// (test/printers/printers.test.ts).
|
|
78
66
|
export const printFarm = Layer.effect(
|
|
79
67
|
PrintFarm,
|
|
80
68
|
Effect.gen(function* () {
|
|
@@ -89,10 +77,6 @@ export const printFarm = Layer.effect(
|
|
|
89
77
|
HttpClient.transformResponse(Effect.flatMap(refuseUnlessOk))
|
|
90
78
|
);
|
|
91
79
|
|
|
92
|
-
// Signs in once, here, and every call below reuses the token, kept `Redacted` so no log
|
|
93
|
-
// prints it. A farm that refuses the sign-in stops the process at startup, with its
|
|
94
|
-
// reason, before any tool is listed. A real client also signs in again when a call
|
|
95
|
-
// answers 401, as tokens expire.
|
|
96
80
|
const session = yield* http
|
|
97
81
|
.post("/api/login", {
|
|
98
82
|
body: HttpBody.jsonUnsafe({ user, password: Redacted.value(password) }),
|
|
@@ -17,9 +17,6 @@ export const Printer = Schema.Struct({
|
|
|
17
17
|
|
|
18
18
|
export type Printer = typeof Printer.Type;
|
|
19
19
|
|
|
20
|
-
// The one error every tool declares: the farm could not do what was asked. A model reads its
|
|
21
|
-
// `message` and decides what to do next, so the message says what went wrong in plain words.
|
|
22
|
-
// A handler failing with an error its action does not declare does not compile.
|
|
23
20
|
export class PrintFarmError extends Schema.TaggedError<
|
|
24
21
|
PrintFarmError,
|
|
25
22
|
{ readonly brand: unique symbol }
|
|
@@ -27,27 +24,14 @@ export class PrintFarmError extends Schema.TaggedError<
|
|
|
27
24
|
message: Schema.String,
|
|
28
25
|
}) {}
|
|
29
26
|
|
|
30
|
-
// A field's description is part of the tool's input schema, which is all a model knows of it.
|
|
31
|
-
// The pattern is there for safety: an id becomes a segment of the farm's URL, and it comes
|
|
32
|
-
// from a model. An id that does not match is refused as invalid input, on every surface,
|
|
33
|
-
// before a handler runs, so `..` or `a/b` never reaches the farm. The `u` flag is what puts the
|
|
34
|
-
// pattern in the tool's schema, where the model reads it.
|
|
35
27
|
const PrinterId = Schema.String.check(
|
|
36
28
|
Schema.isPattern(/^[A-Za-z0-9_-]+$/u)
|
|
37
29
|
).annotate({
|
|
38
30
|
description: "A printer's id, as list_printers gives it, such as mk4-1.",
|
|
39
31
|
});
|
|
40
32
|
|
|
41
|
-
// What every tool here shares. `caller: Action.Anyone` makes an action public: this server
|
|
42
|
-
// authenticates nobody, so it is reachable by whoever can reach the process.
|
|
43
33
|
const tool = { caller: Action.Anyone, error: PrintFarmError } as const;
|
|
44
34
|
|
|
45
|
-
// The names are snake_case because this server is tools only, and that is the convention MCP
|
|
46
|
-
// tool names usually follow. An application whose actions are also typed client methods or CLI
|
|
47
|
-
// commands names them in camelCase, as notes-service does.
|
|
48
|
-
//
|
|
49
|
-
// A description is written for the model that picks the tool: what it does, when to call it,
|
|
50
|
-
// and what it answers.
|
|
51
35
|
export const ListPrinters = Action.make("list_printers", {
|
|
52
36
|
...tool,
|
|
53
37
|
description:
|
|
@@ -59,7 +43,6 @@ export const ListPrinters = Action.make("list_printers", {
|
|
|
59
43
|
mcp: { title: "List printers" },
|
|
60
44
|
});
|
|
61
45
|
|
|
62
|
-
// `image` is an `Action.Image`, so a model sees the picture rather than reading its base64.
|
|
63
46
|
export const GetCameraSnapshot = Action.make("get_camera_snapshot", {
|
|
64
47
|
...tool,
|
|
65
48
|
description:
|
|
@@ -72,8 +55,6 @@ export const GetCameraSnapshot = Action.make("get_camera_snapshot", {
|
|
|
72
55
|
mcp: { title: "Camera snapshot" },
|
|
73
56
|
});
|
|
74
57
|
|
|
75
|
-
// Destructive, MCP's default for a write: a print sets a machine to work and spends material,
|
|
76
|
-
// which is more than adding a record. A second call is refused, so it is not idempotent.
|
|
77
58
|
export const StartPrint = Action.make("start_print", {
|
|
78
59
|
...tool,
|
|
79
60
|
description:
|
|
@@ -90,7 +71,6 @@ export const StartPrint = Action.make("start_print", {
|
|
|
90
71
|
mcp: { title: "Start a print" },
|
|
91
72
|
});
|
|
92
73
|
|
|
93
|
-
// Idempotent: the same call again leaves the printer as the first left it.
|
|
94
74
|
export const SetPaused = Action.make("set_paused", {
|
|
95
75
|
...tool,
|
|
96
76
|
description:
|
|
@@ -104,7 +84,6 @@ export const SetPaused = Action.make("set_paused", {
|
|
|
104
84
|
},
|
|
105
85
|
success: Printer,
|
|
106
86
|
readOnly: false,
|
|
107
|
-
// Destructive, MCP's default for a write: a pause interrupts a running print.
|
|
108
87
|
mcp: { title: "Pause or resume a print", idempotentHint: true },
|
|
109
88
|
});
|
|
110
89
|
|
|
@@ -4,11 +4,6 @@ import type { Effect } from "effect";
|
|
|
4
4
|
|
|
5
5
|
import type { Printer, PrintFarmError } from "./Printers.js";
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* The print farm, the one upstream of this server. Its methods require nothing and fail only
|
|
9
|
-
* with the error the tools declare: its layer, `printFarm` in src/platform/print-farm.ts, has
|
|
10
|
-
* captured the HTTP client, the farm's URL and the session.
|
|
11
|
-
*/
|
|
12
7
|
export class PrintFarm extends Context.Service<
|
|
13
8
|
PrintFarm,
|
|
14
9
|
{
|