@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
package/docs/Action.md
CHANGED
|
@@ -12,8 +12,6 @@ One `Action.make` per action, in the domain's contract file. Export the domain's
|
|
|
12
12
|
import * as Action from "@gjermundgaraba/effect-actions/Action";
|
|
13
13
|
import { Schema } from "effect";
|
|
14
14
|
|
|
15
|
-
// A contract: what the action takes and answers, whether it changes anything, and who may
|
|
16
|
-
// call it. It imports no server code, so any client can import it, a browser page included.
|
|
17
15
|
export const Greet = Action.make("greet", {
|
|
18
16
|
description: "Greet someone by name.",
|
|
19
17
|
input: { name: Schema.String },
|
|
@@ -48,10 +46,6 @@ import { CurrentUser } from "../identity/Identity.js";
|
|
|
48
46
|
import { Notes } from "./Notes.js";
|
|
49
47
|
import { NoteStore } from "./store.js";
|
|
50
48
|
|
|
51
|
-
// The builder runs once, at startup, however many surfaces serve these actions: it is where a
|
|
52
|
-
// handler gets the services that last the process, never the caller. `CurrentUser` is read per
|
|
53
|
-
// call, inside each handler. Each handler is a thin step from the contract to the store, and
|
|
54
|
-
// none checks a scope: `authorize` has run before it.
|
|
55
49
|
export const notes = Action.implement(
|
|
56
50
|
Notes,
|
|
57
51
|
Effect.gen(function* () {
|
|
@@ -105,10 +99,6 @@ import { Effect } from "effect";
|
|
|
105
99
|
|
|
106
100
|
import { CurrentUser, scopes } from "./Identity.js";
|
|
107
101
|
|
|
108
|
-
// The one rule, read from each contract: a read needs the read scope, anything else the write
|
|
109
|
-
// scope. Naming the missing scope lets an OAuth client ask for it and retry (`Forbidden`):
|
|
110
|
-
// `scopes` below goes with `protectedResource` in verifier.ts, copy both or neither.
|
|
111
|
-
// This named span measures authorization within the action's span, including a refusal.
|
|
112
102
|
export const authorize = Effect.fn("authorize")(function* (action: Action.Any) {
|
|
113
103
|
const scope = action.readOnly ? scopes.read : scopes.write;
|
|
114
104
|
const user = yield* CurrentUser;
|
package/docs/ActionCli.md
CHANGED
|
@@ -20,22 +20,15 @@ import { commands } from "./commands.js";
|
|
|
20
20
|
import { CurrentCaller } from "./identity/Identity.js";
|
|
21
21
|
import type { Operator } from "./identity/Identity.js";
|
|
22
22
|
import { Permissions } from "./identity/permissions.js";
|
|
23
|
-
import {
|
|
23
|
+
import { demoTasksFile } from "./platform/tasks-file.js";
|
|
24
24
|
|
|
25
|
-
// Entrypoint: `node --import tsx examples/assistant/src/cli.ts list-tasks`.
|
|
26
|
-
// The operator's own tool. No credential arrives at a terminal, so this host names the caller
|
|
27
|
-
// itself: whoever runs the command, as the trusted operator. Being local earns nothing more:
|
|
28
|
-
// `authorize` runs here as for a model's tool call, and admits the operator by its kind.
|
|
29
25
|
const operator: Operator = { kind: "operator", id: userInfo().username };
|
|
30
26
|
|
|
31
27
|
const assistant = commands.pipe(
|
|
32
|
-
Command.provide(Layer.mergeAll(
|
|
28
|
+
Command.provide(Layer.mergeAll(demoTasksFile, Permissions.layerMemory)),
|
|
33
29
|
Command.provideSync(CurrentCaller, operator)
|
|
34
30
|
);
|
|
35
31
|
|
|
36
|
-
// A command's defect, such as a tasks file that does not decode, `runMain` reports on stdout.
|
|
37
|
-
// `logToStderr`, applied last, moves that to stderr, so `assistant list-tasks --json | jq` reads
|
|
38
|
-
// results alone.
|
|
39
32
|
Command.run(assistant, { version: "1.0.0" }).pipe(
|
|
40
33
|
Effect.provide(NodeServices.layer),
|
|
41
34
|
ActionCli.logToStderr,
|
|
@@ -66,8 +59,6 @@ import { HttpClient, HttpClientRequest } from "effect/http";
|
|
|
66
59
|
|
|
67
60
|
import { Http } from "./Api.js";
|
|
68
61
|
|
|
69
|
-
// The binding's client, connected to where the server is and as which member, both read from
|
|
70
|
-
// config when a command runs.
|
|
71
62
|
const api = Effect.gen(function* () {
|
|
72
63
|
const baseUrl = yield* Config.String("BUILDS_URL").pipe(
|
|
73
64
|
Config.withDefault("http://127.0.0.1:3000")
|
|
@@ -83,13 +74,6 @@ const api = Effect.gen(function* () {
|
|
|
83
74
|
});
|
|
84
75
|
});
|
|
85
76
|
|
|
86
|
-
// A command per action, and `watch <event>` per event type: `builds watch build.finished
|
|
87
|
-
// --pipeline api` holds the event's stream open and prints each occurrence as a line of JSON,
|
|
88
|
-
// its `cursor` included, until it is stopped. `--cursor <cursor>` resumes after that line;
|
|
89
|
-
// omitted, the watch starts from now. The lines pipe into `jq` or a script as they come.
|
|
90
|
-
// `builds start-build --pipeline api` prints each step as a line of JSON on stderr as it
|
|
91
|
-
// starts, and the build on stdout, which a pipe reads alone. `builds tail-log --build-id
|
|
92
|
-
// build-1` prints each line of its log as a line of JSON as it arrives.
|
|
93
77
|
export const commands = ActionCli.remote(Http, { name: "builds", client: api });
|
|
94
78
|
```
|
|
95
79
|
|
|
@@ -126,15 +110,9 @@ import * as ActionCli from "@gjermundgaraba/effect-actions/ActionCli";
|
|
|
126
110
|
import { identity } from "./identity/handlers.js";
|
|
127
111
|
import { tasks } from "./tasks/handlers.js";
|
|
128
112
|
|
|
129
|
-
// The same implementations as local commands, `assistant <action>`: a command runs the handler
|
|
130
|
-
// in this process, behind the same `authorize`. `add-task` and `who-am-i` are as derived; the
|
|
131
|
-
// others each change their syntax or output.
|
|
132
113
|
export const commands = ActionCli.make([tasks, identity], {
|
|
133
114
|
name: "assistant",
|
|
134
115
|
commands: {
|
|
135
|
-
// `assistant list-tasks -a ada`: a short flag beside `--assignee`. `render` prints a line
|
|
136
|
-
// per task for a person, and adds `--json` for the JSON a script reads. `description` is the
|
|
137
|
-
// command's help, saying what a line holds; the action's stays what a model reads.
|
|
138
116
|
listTasks: {
|
|
139
117
|
description:
|
|
140
118
|
"Print one line per task: [x] when done, then its id, title and assignee.",
|
|
@@ -147,9 +125,7 @@ export const commands = ActionCli.make([tasks, identity], {
|
|
|
147
125
|
)
|
|
148
126
|
.join("\n"),
|
|
149
127
|
},
|
|
150
|
-
// `assistant complete-task 2`: an argument instead of `--id 2`.
|
|
151
128
|
completeTask: { positional: ["id"] },
|
|
152
|
-
// `assistant annotate-task --id 2 < notes.md`: the notes are piped in, not a flag.
|
|
153
129
|
annotateTask: { stdin: "notes" },
|
|
154
130
|
},
|
|
155
131
|
});
|
|
@@ -20,7 +20,7 @@ What you see when something is wrong, and the fix. Search for a fragment of the
|
|
|
20
20
|
|
|
21
21
|
## Client methods
|
|
22
22
|
|
|
23
|
-
- `HttpClientError` whose `reason._tag` is `DecodeError`, printed `Decode
|
|
23
|
+
- `HttpClientError` whose `reason._tag` is `DecodeError`, printed `Decode: undeclared status code (<status> …)`, for a status such as 429: no schema declares that status; declare the error on the binding when middleware answers with it, on the action when its handler does. For a 500, the cause is in the server's logs.
|
|
24
24
|
- `HttpClientError` whose `reason._tag` is `InvalidUrlError` outside a browser: relative routes have no page to resolve against; set `baseUrl`.
|
|
25
25
|
- Type error listing `HttpClient` as an unsatisfied requirement: provide one, such as `FetchHttpClient.layer`, or use `ActionHttp.fetchClient`.
|
|
26
26
|
- `Expected 1 arguments` at a client method: `{}` is not a valid input for the action, so pass its input; where the argument may be omitted, leave it out, as `undefined` is a type error.
|
package/docs/ActionHttp.md
CHANGED
|
@@ -15,10 +15,6 @@ import { Login, WhoAmI } from "./identity/Identity.js";
|
|
|
15
15
|
import { Notes } from "./notes/Notes.js";
|
|
16
16
|
import { GetStatus } from "./status/Status.js";
|
|
17
17
|
|
|
18
|
-
// The HTTP binding: every action at `POST /api/<name>`, the protected ones requiring `Login`.
|
|
19
|
-
// It is plain data and imports no server code, so the server, the CLI and a browser page all
|
|
20
|
-
// import this one value, and each gets its routes or its typed methods from it. An action
|
|
21
|
-
// added here is also a `notes` command: commands.ts derives one per action of this binding.
|
|
22
18
|
export const Http = ActionHttp.make([GetStatus, ...Notes, WhoAmI], {
|
|
23
19
|
prefix: "/api",
|
|
24
20
|
authentication: Login,
|
|
@@ -45,30 +41,18 @@ import { authenticate } from "./identity/verifier.js";
|
|
|
45
41
|
import { notes } from "./notes/handlers.js";
|
|
46
42
|
import { status } from "./status/handlers.js";
|
|
47
43
|
|
|
48
|
-
// Every domain's implementation, once. Each surface below takes the same list.
|
|
49
44
|
const implementations = [status, notes, identity];
|
|
50
45
|
|
|
51
|
-
// JSON routes at `/api/<action>`, the binding's; `getStatus` answers anyone.
|
|
52
46
|
const api = ActionHttp.layer(Http, implementations);
|
|
53
47
|
|
|
54
|
-
// The host's own routes sit beside the actions' on the same router.
|
|
55
48
|
const health = HttpRouter.add("GET", "/healthz", HttpServerResponse.text("ok"));
|
|
56
49
|
|
|
57
|
-
// Served to anyone, which is a choice: it tells a signed-out caller every action's name and
|
|
58
|
-
// shape. To keep them, leave the route out, or provide `Authentication.protect(Login).layer`
|
|
59
|
-
// to it.
|
|
60
50
|
const openApi = HttpRouter.add(
|
|
61
51
|
"GET",
|
|
62
52
|
"/api/openapi.json",
|
|
63
53
|
HttpServerResponse.jsonUnsafe(OpenApi.fromApi(Http.api))
|
|
64
54
|
);
|
|
65
55
|
|
|
66
|
-
// What the host serves: the JSON routes, the same actions as MCP tools at `/mcp`, and the host's
|
|
67
|
-
// own routes. The MCP endpoint serves the public `getStatus` beside the protected tools, so a
|
|
68
|
-
// signed-out 2026-07-28 client may list the tools and call `getStatus`; what else needs a token
|
|
69
|
-
// is on `ActionMcp.LayerHttpOptions.authentication`. It still requires `NoteStore`: the
|
|
70
|
-
// entrypoint chooses which one, and a test chooses its own. The entrypoint also chooses how the
|
|
71
|
-
// MCP endpoint runs: one server for every request, or one per request.
|
|
72
56
|
const serving = (requestScoped: boolean) =>
|
|
73
57
|
Layer.mergeAll(
|
|
74
58
|
api,
|
|
@@ -103,11 +87,8 @@ import { HttpRouter, HttpServerRequest } from "effect/http";
|
|
|
103
87
|
import { NoteStore } from "./notes/store.js";
|
|
104
88
|
import { routes } from "./routes.js";
|
|
105
89
|
|
|
106
|
-
// Entrypoint: `node --import tsx examples/notes-service/src/server.ts`.
|
|
107
|
-
// It chooses the store, reads the configuration and starts Node's HTTP server.
|
|
108
90
|
const server = Layer.unwrap(
|
|
109
91
|
Effect.gen(function* () {
|
|
110
|
-
// The loopback default serves this machine alone; HOST=0.0.0.0 lets others reach it.
|
|
111
92
|
const host = yield* Config.String("HOST").pipe(
|
|
112
93
|
Config.withDefault("127.0.0.1")
|
|
113
94
|
);
|
|
@@ -116,12 +97,9 @@ const server = Layer.unwrap(
|
|
|
116
97
|
|
|
117
98
|
return HttpRouter.serve(routes).pipe(
|
|
118
99
|
Layer.provide(NodeHttpServer.layer(createServer, { host, port })),
|
|
119
|
-
// Request bodies have no size limit unless the host sets one. This covers every route that
|
|
120
|
-
// reads its body whole.
|
|
121
100
|
Layer.provide(
|
|
122
101
|
Layer.succeed(HttpServerRequest.MaxBodySize, ByteSize.mebibytes(1))
|
|
123
102
|
),
|
|
124
|
-
// DEMO ONLY: notes live in this process's memory; a deployment provides a store that lasts.
|
|
125
103
|
Layer.provide(NoteStore.layerMemory)
|
|
126
104
|
);
|
|
127
105
|
})
|
|
@@ -161,9 +139,6 @@ import { FetchHttpClient } from "effect/http";
|
|
|
161
139
|
|
|
162
140
|
import { Http } from "./Api.js";
|
|
163
141
|
|
|
164
|
-
// Entrypoint: `node --import tsx examples/quickstart/src/client.ts`, with the server running.
|
|
165
|
-
// One typed method per action of the binding.
|
|
166
|
-
// DEMO ONLY: a deployed client reads its `baseUrl` from configuration (kitchen-sink/src/seed.ts).
|
|
167
142
|
Effect.gen(function* () {
|
|
168
143
|
const client = yield* ActionHttp.client(Http, {
|
|
169
144
|
baseUrl: "http://127.0.0.1:3000",
|
|
@@ -194,9 +169,6 @@ import {
|
|
|
194
169
|
import { DeployRequested, RequestDeploy } from "./deploys/Deploys.js";
|
|
195
170
|
import { Login } from "./identity/Identity.js";
|
|
196
171
|
|
|
197
|
-
// The HTTP binding: each action at `POST /api/<name>`, and each event at
|
|
198
|
-
// `POST /api/events/<name>`, which answers a page of JSON, or, asked for `text/event-stream`,
|
|
199
|
-
// holds a stream open. It imports no server code, so a client imports it with the contracts.
|
|
200
172
|
export const Http = ActionHttp.make(
|
|
201
173
|
[
|
|
202
174
|
FinishBuild,
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
What you see when something is wrong, and the fix. Search for a fragment of the message. The tasks are in [ActionMcp.md](ActionMcp.md).
|
|
4
4
|
|
|
5
5
|
- `Type 'X' is not assignable to type 'never'` where the server is launched, or `Request<"Requires", X>` in an MCP endpoint's type: a handler or an authorizer yields a request service no middleware around the endpoint provides. Provide it with router middleware. `X` the identity, on an endpoint serving public tools: a public handler reads it, which no public tool gets, signed in or not. Make that action protected.
|
|
6
|
-
- `Type 'CurrentUser' is not assignable to type 'never'` where the server is launched, with `CurrentUser` among an MCP endpoint's startup requirements, not its `Request<"Requires", ...>`: a native feature reads a request service,
|
|
6
|
+
- `Type 'CurrentUser' is not assignable to type 'never'` where the server is launched, with `CurrentUser` among an MCP endpoint's startup requirements, not its `Request<"Requires", ...>`: a native feature's `Layer.unwrap` reads a request service at startup, when there is no caller. Content that depends on the caller is an action.
|
|
7
|
+
- `Service not found: effect/ai/McpServer` as a layer builds, or `McpServer` among a layer's requirements: a native feature is merged beside the endpoint. Pass it as `features`.
|
|
7
8
|
- `Type 'CurrentUser' is not assignable to type 'never'` at a stdio program's `runMain`: it serves a protected tool, whose caller the host supplies. Provide the identity around `runStdio`.
|
|
8
9
|
- A client that opens with `initialize`, such as Codex, gets 400 with JSON-RPC error `-32020`, `MCP-Protocol-Version header is required`, or 401 signed out where the endpoint serves a protected tool, and a `DELETE` gets 405: the endpoint is `requestScoped`, which speaks MCP 2026-07-28 alone and keeps no session. Connect that client to an endpoint without it, on a host that keeps a process running.
|
|
9
10
|
- `subscriptions/listen` answered with JSON-RPC error `-32601`, Method not found: an endpoint without `features` has no 2026-07-28 subscriptions unless `allowSubscriptions` is `true`, and a `requestScoped` one has none. Pass `true` where a handler changes the server through `McpServer.McpServer`.
|
package/docs/ActionMcp.md
CHANGED
|
@@ -16,7 +16,6 @@ import { Layer } from "effect";
|
|
|
16
16
|
import { Http } from "./Api.js";
|
|
17
17
|
import { greeting } from "./greeting/handlers.js";
|
|
18
18
|
|
|
19
|
-
// One implementation, two surfaces: a JSON route and an MCP tool at `/mcp`.
|
|
20
19
|
export const routes = Layer.mergeAll(
|
|
21
20
|
ActionHttp.layer(Http, greeting),
|
|
22
21
|
ActionMcp.layerHttp(greeting, { name: "greetings", version: "1.0.0" })
|
|
@@ -42,27 +41,17 @@ import { Effect } from "effect";
|
|
|
42
41
|
|
|
43
42
|
import { identity } from "./identity/handlers.js";
|
|
44
43
|
import { CurrentUser } from "./identity/Identity.js";
|
|
45
|
-
import {
|
|
44
|
+
import { demoUsers } from "./identity/users.js";
|
|
46
45
|
import { notes } from "./notes/handlers.js";
|
|
47
46
|
import { NoteStore } from "./notes/store.js";
|
|
48
47
|
import { status } from "./status/handlers.js";
|
|
49
48
|
|
|
50
|
-
// Entrypoint: an MCP server a local agent starts as a subprocess, such as
|
|
51
|
-
// `claude mcp add notes -- "$PWD/node_modules/.bin/tsx" "$PWD/examples/notes-service/src/stdio.ts"`
|
|
52
|
-
// from the repository root. The host starts the process in a directory it chooses, so `tsx`
|
|
53
|
-
// and the file are named by full path.
|
|
54
|
-
// The same implementations as the HTTP server, and the same `authorize`. No token arrives
|
|
55
|
-
// over stdio, so this host names the caller itself. It is a process of its own with services
|
|
56
|
-
// of its own. DEMO ONLY: this in-memory store holds its notes, not the running server's. To
|
|
57
|
-
// share them, both provide a store they both reach, such as a database.
|
|
58
49
|
ActionMcp.runStdio([status, notes, identity], {
|
|
59
50
|
name: "notes-service",
|
|
60
51
|
version: "1.0.0",
|
|
61
52
|
}).pipe(
|
|
62
|
-
|
|
63
|
-
Effect.provideService(CurrentUser, users.ada),
|
|
53
|
+
Effect.provideService(CurrentUser, demoUsers.ada),
|
|
64
54
|
Effect.provide([NodeStdio.layer, NoteStore.layerMemory]),
|
|
65
|
-
// stdout carries the protocol alone, so logs and failures go to stderr.
|
|
66
55
|
ActionCli.logToStderr,
|
|
67
56
|
NodeRuntime.runMain()
|
|
68
57
|
);
|
|
@@ -114,7 +103,6 @@ import { Login } from "./identity/Identity.js";
|
|
|
114
103
|
import { authenticate } from "./identity/verifier.js";
|
|
115
104
|
import { Rpc } from "./Rpc.js";
|
|
116
105
|
|
|
117
|
-
// Every domain's implementation, once: the events beside the actions, in one list.
|
|
118
106
|
const implementations = [
|
|
119
107
|
builds,
|
|
120
108
|
logs,
|
|
@@ -124,17 +112,6 @@ const implementations = [
|
|
|
124
112
|
deployEvents,
|
|
125
113
|
];
|
|
126
114
|
|
|
127
|
-
// The actions as MCP tools and the events as MCP events, at `/mcp`. A client polls with
|
|
128
|
-
// `events/poll`, or holds `events/stream` open and is pushed each occurrence. Both events are
|
|
129
|
-
// protected, so each request needs a member's token, and each subscriber sees what its token
|
|
130
|
-
// lets it. This long-lived process holds the deploy event's `subscription` hook between polls and
|
|
131
|
-
// its log in memory, and pushes each occurrence: a `requestScoped` endpoint would serve the events
|
|
132
|
-
// by poll and webhook alone, and a serverless host, where no process lasts, check sources alone,
|
|
133
|
-
// without `Webhook.layer`.
|
|
134
|
-
//
|
|
135
|
-
// A tool answers one result, so `tailLog` would collect a whole log before answering, all of it
|
|
136
|
-
// in a model's context: its implementation, `logs`, is left out, and the endpoint serves every
|
|
137
|
-
// other action and every event.
|
|
138
115
|
const mcp = ActionMcp.layerHttp(
|
|
139
116
|
[builds, buildEvents, statusEvents, deploys, deployEvents],
|
|
140
117
|
{
|
|
@@ -144,25 +121,13 @@ const mcp = ActionMcp.layerHttp(
|
|
|
144
121
|
}
|
|
145
122
|
);
|
|
146
123
|
|
|
147
|
-
// The same implementations as routes under `/api`, for a client that speaks HTTP rather than
|
|
148
|
-
// MCP: one implementation of each event serves every surface.
|
|
149
124
|
const api = ActionHttp.layer(Http, implementations);
|
|
150
125
|
|
|
151
|
-
// And as Effect RPC at `/rpc`, over HTTP. A watch is a stream rpc that never ends, so the
|
|
152
|
-
// serialization frames each element as it is sent: `layerNdjson` does, where `layerJson` would
|
|
153
|
-
// answer once every stream of the request ended, which a watch never does.
|
|
154
126
|
const rpc = ActionRpc.layer(Rpc, implementations).pipe(
|
|
155
127
|
Layer.provide(RpcServer.layerProtocolHttp({ path: "/rpc" })),
|
|
156
128
|
Layer.provide(RpcSerialization.layerNdjson)
|
|
157
129
|
);
|
|
158
130
|
|
|
159
|
-
// Webhook delivery for MCP and HTTP alike: a member names a callback URL and a secret, with
|
|
160
|
-
// MCP's `events/subscribe` or `POST /api/events/<name>/subscribe`, and each occurrence it may see
|
|
161
|
-
// is POSTed there, signed. One layer holds the subscriptions of both, so either surface refreshes
|
|
162
|
-
// one made over the other. `persist` keeps each in a `KeyValueStore`, so a restart goes on
|
|
163
|
-
// delivering from where it reached, as the subscriber `Login`'s `schema` keeps. It needs an
|
|
164
|
-
// `HttpClient` to POST, `Dns` to check where each callback resolves, and the store, which
|
|
165
|
-
// `server.ts` provides.
|
|
166
131
|
export const routes = Layer.mergeAll(mcp, api, rpc).pipe(
|
|
167
132
|
Layer.provide(authenticate),
|
|
168
133
|
Layer.provide(Webhook.layer({ persist: true }))
|
package/docs/ActionRpc-events.md
CHANGED
|
@@ -21,8 +21,6 @@ import {
|
|
|
21
21
|
import { DeployRequested, RequestDeploy } from "./deploys/Deploys.js";
|
|
22
22
|
import { Login } from "./identity/Identity.js";
|
|
23
23
|
|
|
24
|
-
// The RPC binding: each action an rpc named after it, and each event a stream rpc named after
|
|
25
|
-
// it, which pushes each occurrence as the server reads it. Plain data, as the HTTP binding is.
|
|
26
24
|
export const Rpc = ActionRpc.make(
|
|
27
25
|
[
|
|
28
26
|
FinishBuild,
|
|
@@ -56,9 +54,6 @@ const idOf = Schema.decodeUnknownOption(
|
|
|
56
54
|
Schema.NumberFromString.pipe(Schema.check(Schema.isInt()))
|
|
57
55
|
);
|
|
58
56
|
|
|
59
|
-
// A cursor this source gives is `<epoch>:<id>`, the store's epoch and the id of the last message
|
|
60
|
-
// a page holds, so one from before a restart of the memory store is told apart. A page starting
|
|
61
|
-
// a watch gives the id alone, as `readMessages`' `after` is, which is the store's now.
|
|
62
57
|
const positionOf = (epoch: string, cursor: string | null) => {
|
|
63
58
|
const parts =
|
|
64
59
|
cursor?.includes(":") === true ? cursor.split(":") : [epoch, cursor];
|
|
@@ -68,9 +63,6 @@ const positionOf = (epoch: string, cursor: string | null) => {
|
|
|
68
63
|
: Option.none();
|
|
69
64
|
};
|
|
70
65
|
|
|
71
|
-
// The room's messages as an event, read from the store that keeps them: a check source.
|
|
72
|
-
// `postMessage` notifies after each post, so a page receives it at once, not at its next read
|
|
73
|
-
// every 5 seconds.
|
|
74
66
|
export const messageEvents = Event.implement(
|
|
75
67
|
MessagePosted,
|
|
76
68
|
Effect.gen(function* () {
|
|
@@ -87,9 +79,6 @@ export const messageEvents = Event.implement(
|
|
|
87
79
|
(id) => id >= 0 && id <= last
|
|
88
80
|
);
|
|
89
81
|
|
|
90
|
-
// No cursor starts after the room's last message. One this room never gave, such as
|
|
91
|
-
// one from before a restart, is answered `truncated` from there: the page learns its
|
|
92
|
-
// messages may be another run's, and reads the room again with `readMessages`.
|
|
93
82
|
if (Option.isNone(after)) {
|
|
94
83
|
return {
|
|
95
84
|
cursor: `${store.epoch}:${last}`,
|
|
@@ -102,17 +91,12 @@ export const messageEvents = Event.implement(
|
|
|
102
91
|
|
|
103
92
|
return {
|
|
104
93
|
cursor: `${store.epoch}:${messages.at(-1)?.id ?? after.value}`,
|
|
105
|
-
// A message's id counts within its room and the store's epoch, so the event's id
|
|
106
|
-
// names all three: a watch drops an id it has seen, and after a restart of the memory
|
|
107
|
-
// store `lobby:1` is another message.
|
|
108
94
|
events: messages.map((data) => ({
|
|
109
95
|
eventId: `${store.epoch}:${room}:${data.id}`,
|
|
110
96
|
data,
|
|
111
97
|
})),
|
|
112
98
|
};
|
|
113
99
|
}),
|
|
114
|
-
// A notify for one room wakes that room's pages alone: the key is the room the watch's
|
|
115
|
-
// header names, read as `check` reads it.
|
|
116
100
|
key: () => CurrentRoom,
|
|
117
101
|
};
|
|
118
102
|
}),
|
package/docs/ActionRpc.md
CHANGED
|
@@ -14,10 +14,6 @@ import * as ActionRpc from "@gjermundgaraba/effect-actions/ActionRpc";
|
|
|
14
14
|
import { ListRooms, MessagePosted, Messages } from "./chat/Chat.js";
|
|
15
15
|
import { Login } from "./identity/Identity.js";
|
|
16
16
|
|
|
17
|
-
// The RPC binding: one native rpc per action, named after it, and a stream rpc for
|
|
18
|
-
// `MessagePosted`, the protected ones requiring `Login`. It is plain data and imports no server
|
|
19
|
-
// code, so the server and a browser page both import this one value. `Rpc.group` is the native
|
|
20
|
-
// `RpcGroup`, for Effect's own `RpcClient.make`.
|
|
21
17
|
export const Rpc = ActionRpc.make([ListRooms, ...Messages, MessagePosted], {
|
|
22
18
|
authentication: Login,
|
|
23
19
|
});
|
|
@@ -39,19 +35,6 @@ import { ResolveRoom, resolveRoom } from "./chat/room.js";
|
|
|
39
35
|
import { authenticate } from "./identity/verifier.js";
|
|
40
36
|
import { Rpc } from "./Rpc.js";
|
|
41
37
|
|
|
42
|
-
// The binding's rpcs on Effect's own RpcServer, the protected ones behind `Login`'s verifier;
|
|
43
|
-
// `listRooms` needs no token.
|
|
44
|
-
//
|
|
45
|
-
// `ResolveRoom`, listed in `middleware`, provides `CurrentRoom`, which the handlers, the event's
|
|
46
|
-
// source and `authorize` read; on a watch it runs as the watch starts, and each time it connects
|
|
47
|
-
// again. Only something that runs per message can, since each message names its own room:
|
|
48
|
-
// `Layer.succeed(CurrentRoom, "lobby")` provided here would leave the layer still requiring it,
|
|
49
|
-
// in its type.
|
|
50
|
-
//
|
|
51
|
-
// It still requires four things, each chosen by whoever serves it. The protocol and its
|
|
52
|
-
// serialization: server.ts gives it a WebSocket, and a test Effect's HTTP protocol, which
|
|
53
|
-
// answers in memory. `MessageStore`, and `Event.Events`, which `postMessage` notifies and the
|
|
54
|
-
// watches wait on: one of each, provided around the whole server.
|
|
55
38
|
export const rpc = ActionRpc.layer(Rpc, [rooms, messages, messageEvents], {
|
|
56
39
|
middleware: [ResolveRoom],
|
|
57
40
|
}).pipe(Layer.provide([resolveRoom, authenticate]));
|
|
@@ -71,13 +54,6 @@ import { RpcSerialization, RpcServer } from "effect/rpc";
|
|
|
71
54
|
import { MessageStore } from "./chat/store.js";
|
|
72
55
|
import { rpc } from "./routes.js";
|
|
73
56
|
|
|
74
|
-
// Entrypoint: `node --import tsx examples/live-rpc/src/server.ts`.
|
|
75
|
-
// It chooses the protocol, the store and the port, and starts Node's HTTP server. It provides
|
|
76
|
-
// no caller and no room: those arrive with each message.
|
|
77
|
-
|
|
78
|
-
// The rpcs over a WebSocket at `/rpc`, the router's one route: Node's HTTP server answers the
|
|
79
|
-
// upgrade. JSON, because a page's client speaks it: browser.ts gives its socket the same
|
|
80
|
-
// serialization.
|
|
81
57
|
const socket = rpc.pipe(
|
|
82
58
|
Layer.provide(RpcServer.layerProtocolWebsocket({ path: "/rpc" })),
|
|
83
59
|
Layer.provide(RpcSerialization.layerJson)
|
|
@@ -85,7 +61,6 @@ const socket = rpc.pipe(
|
|
|
85
61
|
|
|
86
62
|
const server = Layer.unwrap(
|
|
87
63
|
Effect.gen(function* () {
|
|
88
|
-
// The loopback default serves this machine alone; HOST=0.0.0.0 lets others reach it.
|
|
89
64
|
const host = yield* Config.String("HOST").pipe(
|
|
90
65
|
Config.withDefault("127.0.0.1")
|
|
91
66
|
);
|
|
@@ -97,18 +72,10 @@ const server = Layer.unwrap(
|
|
|
97
72
|
NodeHttpServer.layer(createServer, {
|
|
98
73
|
host,
|
|
99
74
|
port,
|
|
100
|
-
// A message on a WebSocket is no request body, so `HttpServerRequest.MaxBodySize`
|
|
101
|
-
// does not bound it: Node's WebSocket server reads one of up to 100 MiB whole unless
|
|
102
|
-
// told otherwise. A chat message is short, and a larger one closes the socket (1009).
|
|
103
75
|
websocket: { maxPayload: 64 * 1024 },
|
|
104
76
|
})
|
|
105
77
|
),
|
|
106
|
-
// DEMO ONLY: messages live in this process's memory; a deployment provides a store that
|
|
107
|
-
// lasts.
|
|
108
78
|
Layer.provide(MessageStore.layerMemory),
|
|
109
|
-
// The pages watching a room are this process's sockets, so what wakes them lives in its
|
|
110
|
-
// memory too: one for the whole server. Several processes behind a balancer would each
|
|
111
|
-
// wake only their own pages, the others reading a post at their next read, 5 seconds on.
|
|
112
79
|
Layer.provide(Event.layerMemory)
|
|
113
80
|
);
|
|
114
81
|
})
|
|
@@ -138,37 +105,23 @@ import { Socket } from "effect/socket";
|
|
|
138
105
|
import { MessagePosted, roomHeader } from "./chat/Chat.js";
|
|
139
106
|
import { Rpc } from "./Rpc.js";
|
|
140
107
|
|
|
141
|
-
// Client module: what a web page imports. It holds the binding and nothing of the server, so a
|
|
142
|
-
// bundler gets the contracts and Effect's RPC client, no handler and nothing of Node.
|
|
143
|
-
|
|
144
|
-
// Effect's own client protocol: one WebSocket, speaking the server's JSON. This layer owns
|
|
145
|
-
// the socket, so a page provides it once, around its whole program, and every call shares
|
|
146
|
-
// that one socket. Which WebSocket is left to the host: a page provides
|
|
147
|
-
// `Socket.layerWebSocketConstructorGlobal`, the browser's own, and client.ts provides Node's.
|
|
148
108
|
export const connection = (url: string) =>
|
|
149
109
|
RpcClient.layerProtocolSocket().pipe(
|
|
150
110
|
Layer.provide(Socket.layerWebSocket(url)),
|
|
151
111
|
Layer.provide(RpcSerialization.layerJson)
|
|
152
112
|
);
|
|
153
113
|
|
|
154
|
-
// The chat as a page uses it, on the connection provided around it.
|
|
155
114
|
export const openChat = Effect.gen(function* () {
|
|
156
|
-
// One typed method per action of the binding, all on the one connection.
|
|
157
115
|
const client = yield* ActionRpc.client(Rpc);
|
|
158
116
|
|
|
159
117
|
return {
|
|
160
|
-
// The public rpc: no token.
|
|
161
118
|
rooms: Effect.map(client.listRooms(), ({ rooms }) => rooms),
|
|
162
119
|
|
|
163
120
|
room: (name: string, token: string) => {
|
|
164
|
-
// A browser WebSocket cannot set an `Authorization` header on its upgrade, so the token
|
|
165
|
-
// travels on each message, and the room beside it. A refreshed token needs no new socket.
|
|
166
121
|
const headers = { authorization: `Bearer ${token}`, [roomHeader]: name };
|
|
167
122
|
const here = RpcClient.withHeaders(headers);
|
|
168
123
|
|
|
169
124
|
return {
|
|
170
|
-
// What the room holds after the last id the page has. A token may expire while the
|
|
171
|
-
// page stays open, so each refusal becomes what the page shows.
|
|
172
125
|
read: (after: number) =>
|
|
173
126
|
client.readMessages({ after }).pipe(
|
|
174
127
|
here,
|
|
@@ -189,14 +142,6 @@ export const openChat = Effect.gen(function* () {
|
|
|
189
142
|
|
|
190
143
|
post: (text: string) => client.postMessage({ text }).pipe(here),
|
|
191
144
|
|
|
192
|
-
// Each message posted to the room after the last id the page holds: a page opening a
|
|
193
|
-
// room reads it, then watches from the last id it read, so a post between the two is
|
|
194
|
-
// not lost; 0 sends the room's history first. `watch` connects again after a drop and
|
|
195
|
-
// resumes from the last message it delivered, so the page never subscribes by hand; a
|
|
196
|
-
// `Gap` says some were missed, or are another run's, so the page reads the room again
|
|
197
|
-
// with `read(0)`. The headers go with the
|
|
198
|
-
// stream, as `here` puts them on a call, and so with every request it sends again. A
|
|
199
|
-
// refusal ends it, as when it connects again with a token that has expired.
|
|
200
145
|
messages: (after: number) =>
|
|
201
146
|
client
|
|
202
147
|
.watch(MessagePosted, {}, { cursor: String(after) })
|
|
@@ -207,8 +152,6 @@ export const openChat = Effect.gen(function* () {
|
|
|
207
152
|
)
|
|
208
153
|
),
|
|
209
154
|
|
|
210
|
-
// A declared error arrives as the contract's class. A message someone else already
|
|
211
|
-
// removed is gone, which is what the page asked for.
|
|
212
155
|
remove: (id: number) =>
|
|
213
156
|
client.deleteMessage({ id }).pipe(
|
|
214
157
|
here,
|
|
@@ -250,17 +193,11 @@ export class CurrentRoom extends Context.Service<CurrentRoom, string>()(
|
|
|
250
193
|
"live-rpc/CurrentRoom"
|
|
251
194
|
) {}
|
|
252
195
|
|
|
253
|
-
// Native Effect RPC middleware, declared as providing `CurrentRoom`. Listed in
|
|
254
|
-
// `ActionRpc.layer`'s `middleware`, it runs once per message.
|
|
255
196
|
export class ResolveRoom extends RpcMiddleware.Service<
|
|
256
197
|
ResolveRoom,
|
|
257
198
|
{ provides: CurrentRoom }
|
|
258
199
|
>()("live-rpc/ResolveRoom") {}
|
|
259
200
|
|
|
260
|
-
// The middleware's implementation. It gets the message's headers, which over a WebSocket are
|
|
261
|
-
// the upgrade request's merged with the message's own, and runs the rest of the call with the
|
|
262
|
-
// room they name, or the default room when they name none. It refuses nothing: whether the
|
|
263
|
-
// caller may be in that room is `authorize`'s rule.
|
|
264
201
|
export const resolveRoom = Layer.succeed(ResolveRoom, (call, { headers }) =>
|
|
265
202
|
Effect.provideService(call, CurrentRoom, headers[roomHeader] ?? defaultRoom)
|
|
266
203
|
);
|
package/docs/ActionToolkit.md
CHANGED
|
@@ -18,12 +18,7 @@ import { CurrentCaller } from "./identity/Identity.js";
|
|
|
18
18
|
import type { Member } from "./identity/Identity.js";
|
|
19
19
|
import { tasks } from "./tasks/handlers.js";
|
|
20
20
|
|
|
21
|
-
// `actions.layer` holds the handlers, and requires the startup services their builders read;
|
|
22
|
-
// neither it nor `actions.toolkit` holds a caller.
|
|
23
21
|
const actions = ActionToolkit.make([tasks, identity], {
|
|
24
|
-
// One rule over every call a model makes, asked before the call runs. A read runs at once. A
|
|
25
|
-
// write waits for a person to approve it, except a task the caller adds to their own list:
|
|
26
|
-
// `call.name` narrows `call.input` to that action's. Without a caller, it asks.
|
|
27
22
|
needsApproval: (call) =>
|
|
28
23
|
!call.action.readOnly &&
|
|
29
24
|
Effect.map(
|
|
@@ -36,8 +31,6 @@ const actions = ActionToolkit.make([tasks, identity], {
|
|
|
36
31
|
),
|
|
37
32
|
});
|
|
38
33
|
|
|
39
|
-
// A title for this assistant's approval display. The model still calls the tool `addTask`.
|
|
40
|
-
// The later definition wins; annotating the generated tool keeps its handler and approval rule.
|
|
41
34
|
export const toolkit = Toolkit.merge(
|
|
42
35
|
actions.toolkit,
|
|
43
36
|
Toolkit.make(actions.toolkit.tools.addTask.annotate(Tool.Title, "Add a task"))
|
|
@@ -45,10 +38,6 @@ export const toolkit = Toolkit.merge(
|
|
|
45
38
|
|
|
46
39
|
export const { layer } = actions;
|
|
47
40
|
|
|
48
|
-
// One toolkit serves every member, so who a turn runs as is provided around that turn. This is
|
|
49
|
-
// how this example names a model's caller, and it takes a `Member`. The tools themselves accept
|
|
50
|
-
// any `CurrentCaller`, so keeping the operator away from a model is this function's doing, and
|
|
51
|
-
// that of every host using it.
|
|
52
41
|
export const actingFor = (member: Member) =>
|
|
53
42
|
Effect.provideService(CurrentCaller, member);
|
|
54
43
|
```
|
|
@@ -21,10 +21,6 @@ export class Tenant extends Context.Service<Tenant, Option.Option<string>>()(
|
|
|
21
21
|
"tracker/Tenant"
|
|
22
22
|
) {}
|
|
23
23
|
|
|
24
|
-
// Native router middleware. It provides `Tenant` to whatever it is provided to, per request:
|
|
25
|
-
// to the verifier too, when it is provided after the authentication, in a `Layer.provide` of
|
|
26
|
-
// its own: in one array with the authentication it would leave `Tenant` owed, since an array's
|
|
27
|
-
// layers provide to the routes, not to one another.
|
|
28
24
|
export const resolveTenant = HttpRouter.middleware<{ provides: Tenant }>()(
|
|
29
25
|
(route) =>
|
|
30
26
|
Effect.flatMap(HttpServerRequest.HttpServerRequest, (request) =>
|
|
@@ -59,32 +55,22 @@ import { CurrentMember, Session } from "../identity/Identity.js";
|
|
|
59
55
|
import { GetAttachment } from "./Attachments.js";
|
|
60
56
|
import { AttachmentStore } from "./store.js";
|
|
61
57
|
|
|
62
|
-
// A route of the host's own: `GET /attachments/<id>` answers the image's bytes, for an `<img>`
|
|
63
|
-
// of the web app, which no action can (an action answers JSON), behind
|
|
64
|
-
// `Authentication.protect(Session)`: the page's cookie, which the web app's rpcs use. A token
|
|
65
|
-
// holder has no use for it: `getAttachment` gives an API client the same image.
|
|
66
58
|
export const download = Layer.unwrap(
|
|
67
59
|
Effect.map(AttachmentStore, (store) =>
|
|
68
60
|
HttpRouter.add(
|
|
69
61
|
"GET",
|
|
70
62
|
"/attachments/:id",
|
|
71
63
|
Effect.gen(function* () {
|
|
72
|
-
// The rule of the action it mirrors.
|
|
73
64
|
yield* authorize(GetAttachment);
|
|
74
65
|
|
|
75
66
|
const member = yield* CurrentMember;
|
|
76
67
|
const { id = "" } = yield* HttpRouter.params;
|
|
77
68
|
const { image } = yield* store.get(member.workspace, id);
|
|
78
69
|
|
|
79
|
-
// An attachment never changes, but it is one workspace's: `private`.
|
|
80
70
|
return HttpServerResponse.uint8Array(image.data, {
|
|
81
71
|
contentType: image.mimeType,
|
|
82
72
|
headers: {
|
|
83
73
|
"cache-control": "private, max-age=31536000, immutable",
|
|
84
|
-
// What a member uploaded is served from the application's own origin, and an image
|
|
85
|
-
// can be a document: an SVG opened by its URL, not in an `<img>`, runs its scripts
|
|
86
|
-
// as the member reading it. `sandbox` gives it no origin and no scripts, and
|
|
87
|
-
// `nosniff` keeps the browser to the type stated.
|
|
88
74
|
"content-security-policy": "sandbox",
|
|
89
75
|
"x-content-type-options": "nosniff",
|
|
90
76
|
},
|
|
@@ -113,13 +99,6 @@ import { allowedOrigins, publicUrl } from "./config.js";
|
|
|
113
99
|
|
|
114
100
|
const loopback = ["localhost", "127.0.0.1"];
|
|
115
101
|
|
|
116
|
-
// Which Host and Origin a request may carry: not the library's concern, but every request of
|
|
117
|
-
// both ports passes it first. A Host that is neither loopback nor the public URL's is refused,
|
|
118
|
-
// against DNS rebinding. The web app signs in with a cookie, which a browser attaches to
|
|
119
|
-
// any page's request, so a foreign Origin must be refused before authentication reads it.
|
|
120
|
-
// That is why this is global router middleware (`global: true`): it runs before routing, so
|
|
121
|
-
// before every route's authentication. As a layer's own `middleware` it would run inside the
|
|
122
|
-
// authentication, too late.
|
|
123
102
|
export const requestPolicy = HttpRouter.middleware(
|
|
124
103
|
Effect.gen(function* () {
|
|
125
104
|
const hosts = new Set([...loopback, new URL(yield* publicUrl).hostname]);
|
|
@@ -139,10 +118,6 @@ export const requestPolicy = HttpRouter.middleware(
|
|
|
139
118
|
|
|
140
119
|
const { origin } = request.headers;
|
|
141
120
|
|
|
142
|
-
// The request's own origin is that of a page served from this port directly. Behind a
|
|
143
|
-
// proxy ending TLS it is an `http:` one from Effect 4.0.6, whose `originalUrl` takes the
|
|
144
|
-
// scheme of the connection, whatever `X-Forwarded-Proto` says, so the public URL's
|
|
145
|
-
// origin is among `origins`.
|
|
146
121
|
if (
|
|
147
122
|
origin !== undefined &&
|
|
148
123
|
origin !== url.value.origin &&
|
|
@@ -157,12 +132,6 @@ export const requestPolicy = HttpRouter.middleware(
|
|
|
157
132
|
{ global: true }
|
|
158
133
|
);
|
|
159
134
|
|
|
160
|
-
// CORS for the one cross-origin caller, a browser MCP client. `HttpRouter.cors` is global to
|
|
161
|
-
// the router too: it answers the preflight before any route's authentication, and adds its
|
|
162
|
-
// headers to refusals as well. An MCP endpoint's `allowedOrigins` is no CORS: it only refuses
|
|
163
|
-
// origins. The headers are those an MCP client sends and must be able to read. A client of an
|
|
164
|
-
// MCP revision with sessions sends its session's id on each request, reads it from the answer
|
|
165
|
-
// that opened the session, and ends the session with a `DELETE`.
|
|
166
135
|
export const cors = Layer.unwrap(
|
|
167
136
|
Effect.map(allowedOrigins, (origins) =>
|
|
168
137
|
HttpRouter.cors({
|