@gjermundgaraba/effect-actions 0.22.0 → 0.23.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.
Files changed (271) hide show
  1. package/README.md +7 -1
  2. package/dist/Action.d.mts +2 -2
  3. package/dist/Action.mjs +1 -0
  4. package/dist/ActionCli.mjs +0 -1
  5. package/dist/ActionHttp.d.mts +1 -1
  6. package/dist/ActionMcp.d.mts +0 -3
  7. package/dist/ActionMcp.mjs +0 -5
  8. package/dist/ActionRpc.d.mts +8 -15
  9. package/dist/ActionRpc.mjs +29 -54
  10. package/dist/ActionToolkit.d.mts +0 -2
  11. package/dist/ActionToolkit.mjs +0 -2
  12. package/dist/Event.d.mts +0 -23
  13. package/dist/Testing.mjs +3 -2
  14. package/dist/Webhook.mjs +2 -2
  15. package/dist/authentication/refusal.mjs +0 -1
  16. package/dist/cli/commands.mjs +0 -5
  17. package/dist/contract/authorizer.mjs +0 -5
  18. package/dist/contract/call.mjs +0 -23
  19. package/dist/contract/callback.mjs +0 -1
  20. package/dist/contract/caller.mjs +0 -11
  21. package/dist/contract/canonical.mjs +0 -5
  22. package/dist/contract/codec.d.mts +0 -1
  23. package/dist/contract/codec.mjs +0 -1
  24. package/dist/contract/codes.mjs +1 -15
  25. package/dist/contract/declared.mjs +0 -15
  26. package/dist/contract/digest.mjs +0 -1
  27. package/dist/contract/errors.d.mts +0 -4
  28. package/dist/contract/errors.mjs +0 -4
  29. package/dist/contract/feed.d.mts +0 -1
  30. package/dist/contract/feed.mjs +0 -20
  31. package/dist/contract/hints.mjs +0 -6
  32. package/dist/contract/hub.mjs +0 -3
  33. package/dist/contract/idempotency.mjs +0 -4
  34. package/dist/contract/implementation.d.mts +0 -1
  35. package/dist/contract/implementation.mjs +0 -27
  36. package/dist/contract/input-check.mjs +0 -1
  37. package/dist/contract/lease.mjs +0 -1
  38. package/dist/contract/listed.mjs +0 -6
  39. package/dist/contract/log.mjs +0 -8
  40. package/dist/contract/names.mjs +0 -1
  41. package/dist/contract/occurrence.mjs +0 -8
  42. package/dist/contract/pages.mjs +5 -12
  43. package/dist/contract/progress.mjs +2 -25
  44. package/dist/contract/push.d.mts +0 -5
  45. package/dist/contract/push.mjs +1 -24
  46. package/dist/contract/receiving.mjs +0 -7
  47. package/dist/contract/reported.mjs +0 -5
  48. package/dist/contract/serving.mjs +0 -18
  49. package/dist/contract/status.mjs +0 -3
  50. package/dist/contract/stream.mjs +0 -2
  51. package/dist/contract/subscribe.d.mts +0 -2
  52. package/dist/contract/subscribe.mjs +0 -8
  53. package/dist/contract/transient.mjs +0 -12
  54. package/dist/contract/watch.mjs +5 -17
  55. package/dist/contract/webhooks.mjs +0 -11
  56. package/dist/http/client.mjs +0 -1
  57. package/dist/http/events.d.mts +0 -10
  58. package/dist/http/events.mjs +2 -31
  59. package/dist/http/placed.d.mts +0 -2
  60. package/dist/http/streamed.d.mts +0 -1
  61. package/dist/http/streamed.mjs +4 -32
  62. package/dist/http/watch.mjs +0 -6
  63. package/dist/mcp/bridge.mjs +0 -25
  64. package/dist/mcp/client.d.mts +0 -1
  65. package/dist/mcp/client.mjs +0 -1
  66. package/dist/mcp/events.mjs +0 -6
  67. package/dist/mcp/progress.mjs +0 -7
  68. package/dist/mcp/protocol.mjs +0 -6
  69. package/dist/mcp/reply.mjs +0 -11
  70. package/dist/mcp/request.mjs +0 -1
  71. package/dist/mcp/watch.mjs +0 -6
  72. package/dist/rpc/watch.mjs +0 -6
  73. package/dist/stdio/console.mjs +2 -2
  74. package/dist/webhook/address.mjs +4 -9
  75. package/dist/webhook/attempt.mjs +2 -15
  76. package/dist/webhook/receiver.mjs +2 -8
  77. package/dist/webhook/signing.mjs +0 -3
  78. package/dist/webhook/stored.mjs +8 -17
  79. package/dist/webhook/subscriptions.mjs +19 -23
  80. package/docs/Action-streams.md +0 -12
  81. package/docs/Action.md +0 -10
  82. package/docs/ActionCli.md +2 -26
  83. package/docs/ActionHttp.md +0 -28
  84. package/docs/ActionMcp.md +2 -37
  85. package/docs/ActionRpc-events.md +0 -16
  86. package/docs/ActionRpc.md +0 -63
  87. package/docs/ActionToolkit.md +0 -11
  88. package/docs/Authentication-advanced.md +0 -31
  89. package/docs/Authentication.md +4 -13
  90. package/docs/Event.md +0 -37
  91. package/docs/README.md +1 -1
  92. package/docs/Testing.md +3 -13
  93. package/docs/Webhook.md +0 -46
  94. package/docs/setup.md +0 -5
  95. package/examples/README.md +7 -1
  96. package/examples/assistant/src/agent.ts +8 -31
  97. package/examples/assistant/src/cli.ts +2 -9
  98. package/examples/assistant/src/commands.ts +0 -8
  99. package/examples/assistant/src/identity/Identity.ts +0 -10
  100. package/examples/assistant/src/identity/authorize.ts +0 -4
  101. package/examples/assistant/src/identity/members.ts +1 -3
  102. package/examples/assistant/src/identity/permissions.ts +0 -5
  103. package/examples/assistant/src/platform/tasks-file.ts +1 -6
  104. package/examples/assistant/src/scripted.ts +0 -4
  105. package/examples/assistant/src/tasks/Tasks.ts +0 -2
  106. package/examples/assistant/src/tasks/handlers.ts +0 -3
  107. package/examples/assistant/src/tasks/store.ts +0 -7
  108. package/examples/assistant/src/toolkit.ts +0 -11
  109. package/examples/assistant/test/commands.test.ts +0 -5
  110. package/examples/assistant/test/identity/identity.test.ts +6 -6
  111. package/examples/assistant/test/platform/tasks-file.test.ts +2 -6
  112. package/examples/assistant/test/tasks/tasks.test.ts +13 -14
  113. package/examples/assistant/test/toolkit.test.ts +7 -18
  114. package/examples/build-events/README.md +2 -0
  115. package/examples/build-events/src/Api.ts +0 -3
  116. package/examples/build-events/src/Rpc.ts +0 -2
  117. package/examples/build-events/src/builds/Builds.ts +0 -15
  118. package/examples/build-events/src/builds/events.ts +0 -10
  119. package/examples/build-events/src/builds/handlers.ts +0 -6
  120. package/examples/build-events/src/builds/log.ts +0 -6
  121. package/examples/build-events/src/builds/status.ts +0 -4
  122. package/examples/build-events/src/builds/store.ts +0 -21
  123. package/examples/build-events/src/cli.ts +0 -2
  124. package/examples/build-events/src/commands.ts +0 -9
  125. package/examples/build-events/src/deploys/approvers.ts +0 -3
  126. package/examples/build-events/src/deploys/events.ts +0 -8
  127. package/examples/build-events/src/deploys/handlers.ts +0 -2
  128. package/examples/build-events/src/identity/Identity.ts +0 -11
  129. package/examples/build-events/src/identity/members.ts +1 -2
  130. package/examples/build-events/src/identity/verifier.ts +8 -9
  131. package/examples/build-events/src/notifier/Notices.ts +0 -3
  132. package/examples/build-events/src/notifier/routes.ts +0 -10
  133. package/examples/build-events/src/notifier.ts +0 -8
  134. package/examples/build-events/src/routes.ts +0 -24
  135. package/examples/build-events/src/server.ts +0 -14
  136. package/examples/build-events/test/builds/events.test.ts +0 -6
  137. package/examples/build-events/test/builds/log.test.ts +0 -1
  138. package/examples/build-events/test/builds/progress.test.ts +0 -1
  139. package/examples/build-events/test/commands.test.ts +0 -5
  140. package/examples/build-events/test/deploys/events.test.ts +0 -1
  141. package/examples/build-events/test/http.test.ts +0 -4
  142. package/examples/build-events/test/notifier.test.ts +0 -9
  143. package/examples/build-events/test/routes.test.ts +0 -9
  144. package/examples/build-events/test/rpc.test.ts +0 -4
  145. package/examples/build-events/test/support/as.ts +3 -4
  146. package/examples/build-events/test/support/memory.ts +0 -5
  147. package/examples/build-events/test/webhooks.test.ts +0 -12
  148. package/examples/kitchen-sink/README.md +1 -1
  149. package/examples/kitchen-sink/src/Actions.ts +0 -10
  150. package/examples/kitchen-sink/src/Api.ts +0 -8
  151. package/examples/kitchen-sink/src/Rpc.ts +0 -4
  152. package/examples/kitchen-sink/src/admin/Admin.ts +0 -9
  153. package/examples/kitchen-sink/src/admin/authenticate.ts +2 -6
  154. package/examples/kitchen-sink/src/admin/authorize.ts +0 -5
  155. package/examples/kitchen-sink/src/admin/grants.ts +0 -5
  156. package/examples/kitchen-sink/src/admin/handlers.ts +0 -2
  157. package/examples/kitchen-sink/src/admin/keys.ts +1 -3
  158. package/examples/kitchen-sink/src/agent.ts +2 -13
  159. package/examples/kitchen-sink/src/assistant/Assistant.ts +7 -24
  160. package/examples/kitchen-sink/src/assistant/conversations.ts +3 -21
  161. package/examples/kitchen-sink/src/assistant/demo-model.ts +0 -5
  162. package/examples/kitchen-sink/src/assistant/handlers.ts +1 -30
  163. package/examples/kitchen-sink/src/attachments/Attachments.ts +0 -4
  164. package/examples/kitchen-sink/src/attachments/download.ts +0 -10
  165. package/examples/kitchen-sink/src/attachments/handlers.ts +0 -3
  166. package/examples/kitchen-sink/src/attachments/store.ts +0 -3
  167. package/examples/kitchen-sink/src/browser.ts +0 -9
  168. package/examples/kitchen-sink/src/cli.ts +0 -19
  169. package/examples/kitchen-sink/src/config.ts +3 -11
  170. package/examples/kitchen-sink/src/identity/Identity.ts +0 -18
  171. package/examples/kitchen-sink/src/identity/authenticate.ts +3 -14
  172. package/examples/kitchen-sink/src/identity/authorize.ts +0 -6
  173. package/examples/kitchen-sink/src/identity/directory.ts +8 -18
  174. package/examples/kitchen-sink/src/identity/handlers.ts +0 -3
  175. package/examples/kitchen-sink/src/identity/members.ts +1 -5
  176. package/examples/kitchen-sink/src/identity/quota.ts +0 -11
  177. package/examples/kitchen-sink/src/issues/Issues.ts +0 -24
  178. package/examples/kitchen-sink/src/issues/assistant.ts +0 -10
  179. package/examples/kitchen-sink/src/issues/events.ts +2 -16
  180. package/examples/kitchen-sink/src/issues/export.ts +0 -5
  181. package/examples/kitchen-sink/src/issues/guide.ts +0 -5
  182. package/examples/kitchen-sink/src/issues/handlers.ts +0 -5
  183. package/examples/kitchen-sink/src/issues/store.ts +19 -23
  184. package/examples/kitchen-sink/src/issues/write-limit.ts +0 -12
  185. package/examples/kitchen-sink/src/memory.ts +0 -9
  186. package/examples/kitchen-sink/src/ops.ts +0 -6
  187. package/examples/kitchen-sink/src/request-policy.ts +0 -17
  188. package/examples/kitchen-sink/src/routes.ts +0 -48
  189. package/examples/kitchen-sink/src/sdk.ts +0 -12
  190. package/examples/kitchen-sink/src/seed.ts +0 -10
  191. package/examples/kitchen-sink/src/server.ts +0 -22
  192. package/examples/kitchen-sink/src/stdio.ts +2 -12
  193. package/examples/kitchen-sink/src/workspaces/Workspaces.ts +0 -4
  194. package/examples/kitchen-sink/src/workspaces/handlers.ts +0 -4
  195. package/examples/kitchen-sink/src/workspaces/registry.ts +0 -5
  196. package/examples/kitchen-sink/src/workspaces/tenant.ts +0 -4
  197. package/examples/kitchen-sink/test/admin/admin.test.ts +0 -1
  198. package/examples/kitchen-sink/test/assistant/assistant.test.ts +0 -18
  199. package/examples/kitchen-sink/test/attachments/attachments.test.ts +0 -9
  200. package/examples/kitchen-sink/test/identity/identity.test.ts +0 -10
  201. package/examples/kitchen-sink/test/identity/session.test.ts +0 -10
  202. package/examples/kitchen-sink/test/issues/assistant.test.ts +3 -7
  203. package/examples/kitchen-sink/test/issues/events.test.ts +2 -9
  204. package/examples/kitchen-sink/test/issues/export.test.ts +0 -3
  205. package/examples/kitchen-sink/test/issues/issues.test.ts +6 -12
  206. package/examples/kitchen-sink/test/request-policy.test.ts +0 -6
  207. package/examples/kitchen-sink/test/routes.test.ts +0 -6
  208. package/examples/kitchen-sink/test/support/as.ts +3 -5
  209. package/examples/kitchen-sink/test/support/scripted.ts +0 -3
  210. package/examples/kitchen-sink/test/support/services.ts +0 -3
  211. package/examples/live-rpc/src/Rpc.ts +0 -4
  212. package/examples/live-rpc/src/browser.ts +0 -24
  213. package/examples/live-rpc/src/chat/Chat.ts +0 -22
  214. package/examples/live-rpc/src/chat/authorize.ts +0 -3
  215. package/examples/live-rpc/src/chat/events.ts +0 -14
  216. package/examples/live-rpc/src/chat/handlers.ts +0 -5
  217. package/examples/live-rpc/src/chat/room.ts +0 -6
  218. package/examples/live-rpc/src/chat/store.ts +0 -9
  219. package/examples/live-rpc/src/client.ts +0 -6
  220. package/examples/live-rpc/src/identity/Identity.ts +0 -6
  221. package/examples/live-rpc/src/identity/members.ts +1 -3
  222. package/examples/live-rpc/src/identity/verifier.ts +6 -12
  223. package/examples/live-rpc/src/page.ts +0 -10
  224. package/examples/live-rpc/src/routes.ts +0 -13
  225. package/examples/live-rpc/src/server.ts +0 -16
  226. package/examples/live-rpc/test/browser.test.ts +0 -9
  227. package/examples/live-rpc/test/chat/chat.test.ts +3 -11
  228. package/examples/live-rpc/test/routes.test.ts +0 -8
  229. package/examples/live-rpc/test/server.test.ts +0 -13
  230. package/examples/mcp-tools/src/instructions.ts +0 -4
  231. package/examples/mcp-tools/src/platform/fake-farm.ts +0 -6
  232. package/examples/mcp-tools/src/platform/print-farm.ts +0 -16
  233. package/examples/mcp-tools/src/printers/Printers.ts +0 -21
  234. package/examples/mcp-tools/src/printers/farm.ts +0 -5
  235. package/examples/mcp-tools/src/printers/handlers.ts +0 -6
  236. package/examples/mcp-tools/src/routes.ts +0 -11
  237. package/examples/mcp-tools/src/server.ts +0 -9
  238. package/examples/mcp-tools/src/stdio.ts +1 -13
  239. package/examples/mcp-tools/test/printers/printers.test.ts +0 -10
  240. package/examples/mcp-tools/test/routes.test.ts +0 -14
  241. package/examples/notes-service/src/Api.ts +0 -4
  242. package/examples/notes-service/src/browser.ts +0 -12
  243. package/examples/notes-service/src/cli.ts +0 -2
  244. package/examples/notes-service/src/commands.ts +0 -30
  245. package/examples/notes-service/src/identity/Identity.ts +0 -2
  246. package/examples/notes-service/src/identity/authorize.ts +0 -4
  247. package/examples/notes-service/src/identity/users.ts +1 -3
  248. package/examples/notes-service/src/identity/verifier.ts +4 -11
  249. package/examples/notes-service/src/notes/Notes.ts +0 -12
  250. package/examples/notes-service/src/notes/handlers.ts +0 -4
  251. package/examples/notes-service/src/notes/store.ts +62 -48
  252. package/examples/notes-service/src/routes.ts +0 -12
  253. package/examples/notes-service/src/server.ts +0 -6
  254. package/examples/notes-service/src/status/Status.ts +0 -1
  255. package/examples/notes-service/src/status/handlers.ts +0 -1
  256. package/examples/notes-service/src/stdio.ts +2 -12
  257. package/examples/notes-service/src/worker.ts +0 -7
  258. package/examples/notes-service/test/commands.test.ts +0 -20
  259. package/examples/notes-service/test/notes/as.ts +3 -4
  260. package/examples/notes-service/test/notes/in-process.test.ts +3 -6
  261. package/examples/notes-service/test/notes/notes.test.ts +0 -6
  262. package/examples/notes-service/test/routes.test.ts +0 -5
  263. package/examples/notes-service/test/worker.test.ts +0 -1
  264. package/examples/quickstart/src/Api.ts +0 -1
  265. package/examples/quickstart/src/client.ts +0 -3
  266. package/examples/quickstart/src/greeting/Greeting.ts +0 -2
  267. package/examples/quickstart/src/greeting/handlers.ts +0 -1
  268. package/examples/quickstart/src/routes.ts +0 -1
  269. package/examples/quickstart/src/server.ts +3 -6
  270. package/examples/quickstart/test/routes.test.ts +0 -1
  271. package/package.json +2 -1
@@ -6,8 +6,6 @@ import { HttpClient, HttpClientRequest } from "effect/http";
6
6
 
7
7
  import { Http } from "./Api.js";
8
8
 
9
- // What a command fails with when no token is configured: a tagged error of the CLI's own, printed
10
- // on stderr as `MissingToken: <message>` under an `ERROR` heading, and exit 1.
11
9
  class MissingToken extends Schema.TaggedError<
12
10
  MissingToken,
13
11
  { readonly brand: unique symbol }
@@ -15,15 +13,11 @@ class MissingToken extends Schema.TaggedError<
15
13
  message: Schema.String,
16
14
  }) {}
17
15
 
18
- // The binding's typed client, connected to where the server is and as who is calling, both read
19
- // from config. Every command here, the derived ones and your own, calls through this one.
20
16
  const api = Effect.gen(function* () {
21
17
  const baseUrl = yield* Config.String("NOTES_URL").pipe(
22
18
  Config.withDefault("http://127.0.0.1:3000")
23
19
  );
24
20
 
25
- // Required, since every command but `get-status` is refused without one, and mapped so that a
26
- // missing one prints what to do.
27
21
  const token = yield* Config.Redacted("NOTES_TOKEN").pipe(
28
22
  Effect.mapError(
29
23
  () =>
@@ -42,18 +36,10 @@ const api = Effect.gen(function* () {
42
36
  });
43
37
  });
44
38
 
45
- // The file's format: each note a `# ` title line, a blank line and its body, the notes apart by
46
- // a blank line. A body line that starts as a title would is written with a backslash before it,
47
- // as Markdown escapes one, and `import` takes it off, so every note reads back as it was.
48
39
  const escape = (body: string) => body.replaceAll(/^(?=\\*# )/gmu, "\\");
49
40
 
50
41
  const unescape = (body: string) => body.replaceAll(/^\\(?=\\*# )/gmu, "");
51
42
 
52
- // A command of your own, for what no single action does: this one calls two and writes a file.
53
- // It is a native Effect CLI command, so its syntax and its output are what you write: `notes
54
- // export notes.md` takes its argument from `Argument`, as a flag would come from `Flag`. It
55
- // calls the server through `api`, the client the derived commands call, and writes with the
56
- // `FileSystem` every Effect CLI command has.
57
43
  const exportNotes = Command.make(
58
44
  "export",
59
45
  {
@@ -80,24 +66,13 @@ const exportNotes = Command.make(
80
66
 
81
67
  yield* Console.log(`Wrote ${whole.length} notes to ${file}`);
82
68
  }).pipe(
83
- // Context of your own for a failure, on stderr before it. The line prints for every
84
- // failure of the command, a file that cannot be written included, so it says only what is
85
- // true of them all.
86
69
  Effect.tapError(() =>
87
70
  Console.error(`The export to ${file} did not finish.`)
88
71
  ),
89
- // So the export fails as a derived command does.
90
72
  ActionCli.toUserError(Http)
91
73
  )
92
74
  ).pipe(Command.withDescription("Write every note to a file, as Markdown."));
93
75
 
94
- // The other half of `export`: it reads such a file and creates its notes with one call of the
95
- // bulk action, so the server checks every note before it writes any, and a file of more than
96
- // the action's 100 is refused whole. The command reads the file and nothing more.
97
- // `Argument.FileText` reads it, and a missing file is a usage error naming it, where
98
- // `Argument.String` and `FileSystem` would print the platform's error. Text before the first
99
- // title is refused, not dropped: the file is not one `export` wrote. The derived `import-notes`
100
- // stays beside it, taking the notes as a JSON array in `--notes`.
101
76
  const importNotes = Command.make(
102
77
  "import",
103
78
  {
@@ -137,20 +112,15 @@ const importNotes = Command.make(
137
112
  )
138
113
  );
139
114
 
140
- // A command per action of the binding, a flag per input field, each calling the server over
141
- // HTTP. It imports the binding alone, never a handler. It is apart from cli.ts so a test runs
142
- // it: test/commands.test.ts.
143
115
  export const commands = ActionCli.remote(Http, {
144
116
  name: "notes",
145
117
  client: api,
146
118
  commands: {
147
- // `notes create-note --title Draft < draft.md`: the body is piped in, not a flag.
148
119
  createNote: { stdin: "body" },
149
120
  getNote: {
150
121
  positional: ["id"],
151
122
  render: (note) => `# ${note.title}\n\n${note.body}`,
152
123
  },
153
- // `notes update-note <id> --revision 1 < draft.md`: an argument, a flag and stdin together.
154
124
  updateNote: { positional: ["id"], stdin: "body" },
155
125
  },
156
126
  subcommands: [exportNotes, importNotes],
@@ -19,8 +19,6 @@ export class CurrentUser extends Context.Service<CurrentUser, User>()(
19
19
  "notes-service/CurrentUser"
20
20
  ) {}
21
21
 
22
- // How a remote caller proves it is CurrentUser: a bearer token, the default scheme. Clients
23
- // import this descriptor with the binding; its verifier, in verifier.ts, stays on the server.
24
22
  export const Login = Authentication.make("notes-service.Login", CurrentUser);
25
23
 
26
24
  export const WhoAmI = Action.make("whoAmI", {
@@ -3,10 +3,6 @@ import { Effect } from "effect";
3
3
 
4
4
  import { CurrentUser, scopes } from "./Identity.js";
5
5
 
6
- // The one rule, read from each contract: a read needs the read scope, anything else the write
7
- // scope. Naming the missing scope lets an OAuth client ask for it and retry (`Forbidden`):
8
- // `scopes` below goes with `protectedResource` in verifier.ts, copy both or neither.
9
- // This named span measures authorization within the action's span, including a refusal.
10
6
  export const authorize = Effect.fn("authorize")(function* (action: Action.Any) {
11
7
  const scope = action.readOnly ? scopes.read : scopes.write;
12
8
  const user = yield* CurrentUser;
@@ -1,9 +1,7 @@
1
1
  import { scopes } from "./Identity.js";
2
2
  import type { User } from "./Identity.js";
3
3
 
4
- // DEMO ONLY: the people who may sign in, each under a token that is their name. A real service
5
- // has no such table: its verifier reads the user from a signed token.
6
- export const users = {
4
+ export const demoUsers = {
7
5
  ada: { id: "ada", scopes: [scopes.read, scopes.write] },
8
6
  grace: { id: "grace", scopes: [scopes.read, scopes.write] },
9
7
  reader: { id: "reader", scopes: [scopes.read] },
@@ -3,27 +3,21 @@ import * as Authentication from "@gjermundgaraba/effect-actions/Authentication";
3
3
  import { Config, Effect, Redacted } from "effect";
4
4
 
5
5
  import { Login, scopes } from "./Identity.js";
6
- import { users } from "./users.js";
6
+ import { demoUsers } from "./users.js";
7
7
 
8
- const isToken = (token: string): token is keyof typeof users =>
9
- Object.hasOwn(users, token);
8
+ const isToken = (token: string): token is keyof typeof demoUsers =>
9
+ Object.hasOwn(demoUsers, token);
10
10
 
11
- // DEMO ONLY: a token is a user's name. A real verifier checks a signed token with your
12
- // authorization server's library, its audience included. An unknown token is the built-in
13
- // `Unauthenticated`: a 401 every client decodes.
14
11
  const verify = (token: Redacted.Redacted) => {
15
12
  const name = Redacted.value(token);
16
13
 
17
14
  return isToken(name)
18
- ? Effect.succeed(users[name])
15
+ ? Effect.succeed(demoUsers[name])
19
16
  : Effect.fail(
20
17
  new Action.Unauthenticated({ message: "Unknown demo token." })
21
18
  );
22
19
  };
23
20
 
24
- // Server only: `Login`'s provider. As an OAuth protected resource it also publishes discovery,
25
- // so an MCP client that gets a 401 finds where to sign in, asking for read first. The resource
26
- // is the server's public URL, known only at startup, so it is read from config.
27
21
  export const authenticate = Authentication.layer(Login, verify, {
28
22
  protectedResource: Effect.map(
29
23
  Config.String("PUBLIC_URL").pipe(
@@ -32,7 +26,6 @@ export const authenticate = Authentication.layer(Login, verify, {
32
26
  (url) =>
33
27
  ({
34
28
  resource: `${url}/mcp`,
35
- // DEMO ONLY: no server is there. Yours is your authorization server's issuer.
36
29
  authorizationServers: ["https://auth.example.com"],
37
30
  scopesSupported: [scopes.read, scopes.write],
38
31
  scopesRequired: [scopes.read],
@@ -12,8 +12,6 @@ export const Note = Schema.Struct({
12
12
 
13
13
  export type Note = typeof Note.Type;
14
14
 
15
- // A declared error is part of the contract: every client decodes it as this class, and its
16
- // status is what HTTP answers with.
17
15
  export class NoteNotFound extends Schema.TaggedError<
18
16
  NoteNotFound,
19
17
  { readonly brand: unique symbol }
@@ -33,15 +31,10 @@ export class RequestConflict extends Schema.TaggedError<
33
31
  { readonly brand: unique symbol }
34
32
  >()("RequestConflict", { requestId: Schema.String }, { httpApiStatus: 409 }) {}
35
33
 
36
- // A description on an input field is that field's help on every surface: beside its flag or
37
- // argument in a command's `--help`, and in the input schema a model reads from the MCP tool
38
- // listing and the OpenAPI document. A field without one has a flag with no help.
39
34
  const Id = Schema.String.annotate({
40
35
  description: "The note's id, as the list of your notes shows it.",
41
36
  });
42
37
 
43
- // A check's `message` is what a caller reads when the value fails it, in place of the
44
- // generic "Expected a value with a length of at least 1". A model and a CLI user read it too.
45
38
  const Title = Schema.String.check(
46
39
  Schema.isMinLength(1, { message: "A note needs a title." }),
47
40
  Schema.isPattern(/^[^\r\n\u2028\u2029]*$/u, {
@@ -89,7 +82,6 @@ export const CreateNote = Action.make("createNote", {
89
82
  error: RequestConflict,
90
83
  readOnly: false,
91
84
  caller: CurrentUser,
92
- // No `idempotentHint: true`: a call without a `requestId` creates a note each time.
93
85
  mcp: { destructiveHint: false },
94
86
  });
95
87
 
@@ -110,8 +102,6 @@ export const UpdateNote = Action.make("updateNote", {
110
102
  error: [NoteNotFound, NoteChanged],
111
103
  readOnly: false,
112
104
  caller: CurrentUser,
113
- // No `destructiveHint`: an update replaces the body, so it is destructive, MCP's default for
114
- // a write. `createNote` and `importNotes`, which only add, say `false`.
115
105
  });
116
106
 
117
107
  export const DeleteNote = Action.make("deleteNote", {
@@ -122,8 +112,6 @@ export const DeleteNote = Action.make("deleteNote", {
122
112
  caller: CurrentUser,
123
113
  });
124
114
 
125
- // Imports share note content with `createNote`, but exclude request keys because bulk creation
126
- // does not deduplicate.
127
115
  export const ImportNotes = Action.make("importNotes", {
128
116
  description:
129
117
  "Create several notes at once, such as those `notes export` wrote. Every note is checked " +
@@ -6,10 +6,6 @@ import { CurrentUser } from "../identity/Identity.js";
6
6
  import { Notes } from "./Notes.js";
7
7
  import { NoteStore } from "./store.js";
8
8
 
9
- // The builder runs once, at startup, however many surfaces serve these actions: it is where a
10
- // handler gets the services that last the process, never the caller. `CurrentUser` is read per
11
- // call, inside each handler. Each handler is a thin step from the contract to the store, and
12
- // none checks a scope: `authorize` has run before it.
13
9
  export const notes = Action.implement(
14
10
  Notes,
15
11
  Effect.gen(function* () {
@@ -3,10 +3,6 @@ import { Context, Effect, Layer } from "effect";
3
3
  import { NoteChanged, NoteNotFound, RequestConflict } from "./Notes.js";
4
4
  import type { Note } from "./Notes.js";
5
5
 
6
- /**
7
- * Where notes live, each owned by one user. Ownership is checked here, in the data access, so no
8
- * surface and no handler can skip it.
9
- */
10
6
  export class NoteStore extends Context.Service<
11
7
  NoteStore,
12
8
  {
@@ -21,7 +17,6 @@ export class NoteStore extends Context.Service<
21
17
  body: string,
22
18
  requestId?: string
23
19
  ) => Effect.Effect<Note, RequestConflict>;
24
- /** Every note or none: a database does this in one transaction. */
25
20
  readonly createAll: (
26
21
  owner: string,
27
22
  notes: ReadonlyArray<{ readonly title: string; readonly body: string }>
@@ -39,16 +34,9 @@ export class NoteStore extends Context.Service<
39
34
  readonly count: Effect.Effect<number>;
40
35
  }
41
36
  >()("notes-service/NoteStore") {
42
- // DEMO ONLY: notes and replay records live in this process's memory, without eviction, so
43
- // they neither survive a restart nor coordinate separate server instances. A deployed store
44
- // commits the mutation and its replay record in one transaction, enforces uniqueness of
45
- // caller, operation and key, and defines a retention window: an expired key no longer
46
- // protects a retry. A separate network call is not made atomic by a local record.
47
37
  static readonly layerMemory = Layer.sync(NoteStore, () => {
48
- // One map per owner: a note is only ever looked up among its owner's, whatever id is asked.
49
38
  const notes = new Map<string, Map<string, Note>>();
50
39
 
51
- // Keep the original result even after the note is updated or deleted.
52
40
  const creations = new Map<string, Map<string, Note>>();
53
41
 
54
42
  const ownedBy = (owner: string) =>
@@ -60,6 +48,57 @@ export class NoteStore extends Context.Service<
60
48
  return note;
61
49
  };
62
50
 
51
+ const createdAtomically = (
52
+ owner: string,
53
+ title: string,
54
+ body: string,
55
+ requestId: string | undefined
56
+ ): Note | RequestConflict => {
57
+ if (requestId !== undefined) {
58
+ const recorded = creations.get(owner)?.get(requestId);
59
+
60
+ if (recorded !== undefined) {
61
+ return recorded.title === title && recorded.body === body
62
+ ? recorded
63
+ : new RequestConflict({ requestId });
64
+ }
65
+ }
66
+
67
+ const note = save(owner, {
68
+ id: crypto.randomUUID(),
69
+ title,
70
+ body,
71
+ revision: 1,
72
+ });
73
+
74
+ if (requestId !== undefined) {
75
+ const requests = creations.get(owner) ?? new Map<string, Note>();
76
+ requests.set(requestId, note);
77
+ creations.set(owner, requests);
78
+ }
79
+
80
+ return note;
81
+ };
82
+
83
+ const updatedAtomically = (
84
+ owner: string,
85
+ id: string,
86
+ body: string,
87
+ revision: number
88
+ ): Note | NoteNotFound | NoteChanged => {
89
+ const current = ownedBy(owner).get(id);
90
+
91
+ if (current === undefined) {
92
+ return new NoteNotFound({ id });
93
+ }
94
+
95
+ if (current.revision !== revision) {
96
+ return new NoteChanged({ id, revision: current.revision });
97
+ }
98
+
99
+ return save(owner, { ...current, body, revision: revision + 1 });
100
+ };
101
+
63
102
  const get = Effect.fn("NoteStore.get")(function* (
64
103
  owner: string,
65
104
  id: string
@@ -78,34 +117,13 @@ export class NoteStore extends Context.Service<
78
117
  get,
79
118
  create: Effect.fn("NoteStore.create")(
80
119
  function* (owner, title, body, requestId) {
81
- if (requestId !== undefined) {
82
- const recorded = creations.get(owner)?.get(requestId);
83
-
84
- if (recorded !== undefined) {
85
- if (recorded.title !== title || recorded.body !== body) {
86
- return yield* new RequestConflict({ requestId });
87
- }
88
-
89
- return recorded;
90
- }
91
- }
120
+ const created = createdAtomically(owner, title, body, requestId);
92
121
 
93
- // No yield between lookup, mutation and recording: concurrent calls cannot interleave
94
- // or interruption leave a note without its result.
95
- const note = save(owner, {
96
- id: crypto.randomUUID(),
97
- title,
98
- body,
99
- revision: 1,
100
- });
101
-
102
- if (requestId !== undefined) {
103
- const requests = creations.get(owner) ?? new Map<string, Note>();
104
- requests.set(requestId, note);
105
- creations.set(owner, requests);
122
+ if (created instanceof RequestConflict) {
123
+ return yield* created;
106
124
  }
107
125
 
108
- return note;
126
+ return created;
109
127
  }
110
128
  ),
111
129
  createAll: (owner, all) =>
@@ -114,22 +132,18 @@ export class NoteStore extends Context.Service<
114
132
  save(owner, { id: crypto.randomUUID(), title, body, revision: 1 })
115
133
  )
116
134
  ),
117
- // No `yield*` between the read and the write: a fiber may be set aside at one, and two
118
- // updates that each read revision 1 before either wrote would both succeed. A database
119
- // does this with a conditional update or a transaction.
120
135
  update: Effect.fn("NoteStore.update")(
121
136
  function* (owner, id, body, revision) {
122
- const current = ownedBy(owner).get(id);
123
-
124
- if (current === undefined) {
125
- return yield* new NoteNotFound({ id });
126
- }
137
+ const updated = updatedAtomically(owner, id, body, revision);
127
138
 
128
- if (current.revision !== revision) {
129
- return yield* new NoteChanged({ id, revision: current.revision });
139
+ if (
140
+ updated instanceof NoteNotFound ||
141
+ updated instanceof NoteChanged
142
+ ) {
143
+ return yield* updated;
130
144
  }
131
145
 
132
- return save(owner, { ...current, body, revision: revision + 1 });
146
+ return updated;
133
147
  }
134
148
  ),
135
149
  remove: Effect.fn("NoteStore.remove")(function* (owner, id) {
@@ -11,30 +11,18 @@ import { authenticate } from "./identity/verifier.js";
11
11
  import { notes } from "./notes/handlers.js";
12
12
  import { status } from "./status/handlers.js";
13
13
 
14
- // Every domain's implementation, once. Each surface below takes the same list.
15
14
  const implementations = [status, notes, identity];
16
15
 
17
- // JSON routes at `/api/<action>`, the binding's; `getStatus` answers anyone.
18
16
  const api = ActionHttp.layer(Http, implementations);
19
17
 
20
- // The host's own routes sit beside the actions' on the same router.
21
18
  const health = HttpRouter.add("GET", "/healthz", HttpServerResponse.text("ok"));
22
19
 
23
- // Served to anyone, which is a choice: it tells a signed-out caller every action's name and
24
- // shape. To keep them, leave the route out, or provide `Authentication.protect(Login).layer`
25
- // to it.
26
20
  const openApi = HttpRouter.add(
27
21
  "GET",
28
22
  "/api/openapi.json",
29
23
  HttpServerResponse.jsonUnsafe(OpenApi.fromApi(Http.api))
30
24
  );
31
25
 
32
- // What the host serves: the JSON routes, the same actions as MCP tools at `/mcp`, and the host's
33
- // own routes. The MCP endpoint serves the public `getStatus` beside the protected tools, so a
34
- // signed-out 2026-07-28 client may list the tools and call `getStatus`; what else needs a token
35
- // is on `ActionMcp.LayerHttpOptions.authentication`. It still requires `NoteStore`: the
36
- // entrypoint chooses which one, and a test chooses its own. The entrypoint also chooses how the
37
- // MCP endpoint runs: one server for every request, or one per request.
38
26
  const serving = (requestScoped: boolean) =>
39
27
  Layer.mergeAll(
40
28
  api,
@@ -7,11 +7,8 @@ import { HttpRouter, HttpServerRequest } from "effect/http";
7
7
  import { NoteStore } from "./notes/store.js";
8
8
  import { routes } from "./routes.js";
9
9
 
10
- // Entrypoint: `node --import tsx examples/notes-service/src/server.ts`.
11
- // It chooses the store, reads the configuration and starts Node's HTTP server.
12
10
  const server = Layer.unwrap(
13
11
  Effect.gen(function* () {
14
- // The loopback default serves this machine alone; HOST=0.0.0.0 lets others reach it.
15
12
  const host = yield* Config.String("HOST").pipe(
16
13
  Config.withDefault("127.0.0.1")
17
14
  );
@@ -20,12 +17,9 @@ const server = Layer.unwrap(
20
17
 
21
18
  return HttpRouter.serve(routes).pipe(
22
19
  Layer.provide(NodeHttpServer.layer(createServer, { host, port })),
23
- // Request bodies have no size limit unless the host sets one. This covers every route that
24
- // reads its body whole.
25
20
  Layer.provide(
26
21
  Layer.succeed(HttpServerRequest.MaxBodySize, ByteSize.mebibytes(1))
27
22
  ),
28
- // DEMO ONLY: notes live in this process's memory; a deployment provides a store that lasts.
29
23
  Layer.provide(NoteStore.layerMemory)
30
24
  );
31
25
  })
@@ -1,7 +1,6 @@
1
1
  import * as Action from "@gjermundgaraba/effect-actions/Action";
2
2
  import { Schema } from "effect";
3
3
 
4
- // Public: `caller: Action.Anyone`. It answers before anyone has signed in, on every surface.
5
4
  export const GetStatus = Action.make("getStatus", {
6
5
  description: "Report whether the service is up and how many notes it holds.",
7
6
  success: { service: Schema.String, notes: Schema.Finite },
@@ -4,7 +4,6 @@ import { Effect } from "effect";
4
4
  import { NoteStore } from "../notes/store.js";
5
5
  import { GetStatus } from "./Status.js";
6
6
 
7
- // A public action takes no `authorize`: nothing would run it.
8
7
  export const status = Action.implement(
9
8
  GetStatus,
10
9
  Effect.gen(function* () {
@@ -5,27 +5,17 @@ import { Effect } from "effect";
5
5
 
6
6
  import { identity } from "./identity/handlers.js";
7
7
  import { CurrentUser } from "./identity/Identity.js";
8
- import { users } from "./identity/users.js";
8
+ import { demoUsers } from "./identity/users.js";
9
9
  import { notes } from "./notes/handlers.js";
10
10
  import { NoteStore } from "./notes/store.js";
11
11
  import { status } from "./status/handlers.js";
12
12
 
13
- // Entrypoint: an MCP server a local agent starts as a subprocess, such as
14
- // `claude mcp add notes -- "$PWD/node_modules/.bin/tsx" "$PWD/examples/notes-service/src/stdio.ts"`
15
- // from the repository root. The host starts the process in a directory it chooses, so `tsx`
16
- // and the file are named by full path.
17
- // The same implementations as the HTTP server, and the same `authorize`. No token arrives
18
- // over stdio, so this host names the caller itself. It is a process of its own with services
19
- // of its own. DEMO ONLY: this in-memory store holds its notes, not the running server's. To
20
- // share them, both provide a store they both reach, such as a database.
21
13
  ActionMcp.runStdio([status, notes, identity], {
22
14
  name: "notes-service",
23
15
  version: "1.0.0",
24
16
  }).pipe(
25
- // DEMO ONLY: a real host reads who started it, from its own login or configuration.
26
- Effect.provideService(CurrentUser, users.ada),
17
+ Effect.provideService(CurrentUser, demoUsers.ada),
27
18
  Effect.provide([NodeStdio.layer, NoteStore.layerMemory]),
28
- // stdout carries the protocol alone, so logs and failures go to stderr.
29
19
  ActionCli.logToStderr,
30
20
  NodeRuntime.runMain()
31
21
  );
@@ -4,15 +4,8 @@ import { HttpRouter, HttpServer } from "effect/http";
4
4
  import { NoteStore } from "./notes/store.js";
5
5
  import { requestScopedRoutes } from "./routes.js";
6
6
 
7
- // Entrypoint for a host that calls `fetch` per request and runs nothing between requests, such
8
- // as Cloudflare Workers, which takes this default export. The handler is built once, when the
9
- // module loads, as Effect's `HttpRouter.toWebHandler` asks on Workers: a build started inside
10
- // `fetch` would belong to that request. Building these routes does no I/O, which Workers refuses
11
- // outside a request. `toWebHandler` reads no `MaxBodySize`, so the host limits request bodies.
12
7
  const app = HttpRouter.toWebHandler(
13
8
  requestScopedRoutes.pipe(
14
- // DEMO ONLY: each instance of the worker holds notes of its own, lost when it stops. A
15
- // deployment provides a store every instance reaches.
16
9
  Layer.provide(NoteStore.layerMemory),
17
10
  Layer.provide(HttpServer.layerServices)
18
11
  )
@@ -22,15 +22,8 @@ import { ImportNotes, Note, NoteNotFound } from "../src/notes/Notes.js";
22
22
  import { NoteStore } from "../src/notes/store.js";
23
23
  import { routes } from "../src/routes.js";
24
24
 
25
- // What the commands add to the routes: their syntax and what they print. They call the real
26
- // routes in memory, as a client with a token does, where cli.ts connects them to a server. A
27
- // service a builder yields is provided here too (routes.test.ts).
28
25
  const served = Testing.layer(routes).pipe(Layer.provide(NoteStore.layerMemory));
29
26
 
30
- // Effect's own runner, given arguments instead of the process's, and what the commands read from
31
- // the environment as a Layer, so the test decides whether a token is configured, whatever the
32
- // shell holds. The console of `it.effect` keeps every line printed, so a run returns the lines
33
- // it added.
34
27
  const connected = (
35
28
  environment: Record<string, string>,
36
29
  args: readonly string[],
@@ -53,14 +46,12 @@ const connected = (
53
46
  return (yield* TestConsole.logLines).slice(before);
54
47
  });
55
48
 
56
- // A run as the caller `token` names.
57
49
  const run = (token: string, args: readonly string[], stdin = "") =>
58
50
  connected({ NOTES_TOKEN: token }, args, stdin);
59
51
 
60
52
  const fitsAFile = (text: string) =>
61
53
  new TextDecoder().decode(new TextEncoder().encode(text)) === text;
62
54
 
63
- // Lines a random string almost never holds, each one the format has to keep apart from a title.
64
55
  const trickyLines = Arbitrary.array(
65
56
  Arbitrary.schema(
66
57
  Schema.Literals([
@@ -103,7 +94,6 @@ describe("the commands", () => {
103
94
  "take an argument, a flag and stdin, and print what `render` returns",
104
95
  () =>
105
96
  Effect.gen(function* () {
106
- // A command without `render` prints its result as JSON.
107
97
  const [created] = yield* run(
108
98
  "ada",
109
99
  ["create-note", "--title", "Plan"],
@@ -134,7 +124,6 @@ describe("the commands", () => {
134
124
  "describe a flag in `--help` as the contract describes its field",
135
125
  () =>
136
126
  Effect.gen(function* () {
137
- // Help is one printed text. The description is the `revision` field's, from Notes.ts.
138
127
  expect(yield* run("ada", ["update-note", "--help"])).toStrictEqual([
139
128
  expect.stringContaining("The revision you read: 1 for a new note"),
140
129
  ]);
@@ -146,7 +135,6 @@ describe("the commands", () => {
146
135
  yield* run("ada", ["create-note", "--title", "Plan"], "First draft\n");
147
136
  yield* run("ada", ["create-note", "--title", "Budget"], "Too small\n");
148
137
 
149
- // The command writes with the real file system, so the test gives it a real directory.
150
138
  const files = yield* FileSystem.FileSystem;
151
139
  const file = `${yield* files.makeTempDirectoryScoped()}/notes.md`;
152
140
 
@@ -202,7 +190,6 @@ describe("the commands", () => {
202
190
 
203
191
  expect(notes.map(({ title }) => title)).toStrictEqual(["Plan", "Budget"]);
204
192
 
205
- // A missing file is a usage error: the command's help, and the file named on stderr.
206
193
  yield* Effect.flip(run("ada", ["import", `${directory}/nowhere.md`]));
207
194
 
208
195
  expect(yield* TestConsole.errorLines).toContainEqual(
@@ -213,9 +200,6 @@ describe("the commands", () => {
213
200
  }).pipe(Effect.provide(Layer.merge(served, NodeServices.layer)))
214
201
  );
215
202
 
216
- // Any notes the contract accepts, generated from its schema, with lines that start as a title
217
- // would added to their bodies. A file holds Unicode text, so a string JSON carries but no file
218
- // can, one with a lone surrogate, is left out.
219
203
  it.effect.prop(
220
204
  "import every note `export` wrote as it was, whatever its body holds",
221
205
  [importable],
@@ -301,12 +285,10 @@ describe("the commands", () => {
301
285
  "userMessage",
302
286
  '{"_tag":"Unauthenticated","message":"Unknown demo token."}'
303
287
  );
304
- // The command's own context is a line of its own on stderr, the JSON left as it is.
305
288
  expect(yield* TestConsole.errorLines).toContain(
306
289
  "The export to notes.md did not finish."
307
290
  );
308
291
 
309
- // Not an action's error: the file system's, named in the message.
310
292
  const unwritable = yield* Effect.flip(
311
293
  run("ada", ["export", "no-such-directory/notes.md"])
312
294
  );
@@ -342,12 +324,10 @@ describe("the commands", () => {
342
324
  "MissingToken: NOTES_TOKEN is not set. Set it to your token: export NOTES_TOKEN=<your token>"
343
325
  );
344
326
 
345
- // What a person sees: that message, once, on stderr, and no report of a `ConfigError`.
346
327
  expect(yield* TestConsole.errorLines).toStrictEqual([
347
328
  expect.stringContaining("NOTES_TOKEN is not set."),
348
329
  ]);
349
330
 
350
- // The token is read when a command runs, so help needs none.
351
331
  yield* connected({}, ["list-notes", "--help"]);
352
332
 
353
333
  expect(yield* TestConsole.logLines).toHaveLength(2);
@@ -1,8 +1,7 @@
1
1
  import { Effect } from "effect";
2
2
 
3
3
  import { CurrentUser } from "../../src/identity/Identity.js";
4
- import { users } from "../../src/identity/users.js";
4
+ import { demoUsers } from "../../src/identity/users.js";
5
5
 
6
- // The caller of one call, as a server provides it per request.
7
- export const as = (user: keyof typeof users) =>
8
- Effect.provideService(CurrentUser, users[user]);
6
+ export const as = (user: keyof typeof demoUsers) =>
7
+ Effect.provideService(CurrentUser, demoUsers[user]);
@@ -3,16 +3,13 @@ import * as Action from "@gjermundgaraba/effect-actions/Action";
3
3
  import { Effect } from "effect";
4
4
 
5
5
  import { CurrentUser } from "../../src/identity/Identity.js";
6
- import { users } from "../../src/identity/users.js";
6
+ import { demoUsers } from "../../src/identity/users.js";
7
7
  import { notes } from "../../src/notes/handlers.js";
8
8
  import { NoteNotFound } from "../../src/notes/Notes.js";
9
9
  import { NoteStore } from "../../src/notes/store.js";
10
10
 
11
- // What the implementation does, with no transport: `Action.client` runs the same decoding,
12
- // `authorize` and handler a server runs. The caller is provided around each call, as a server
13
- // provides it per request.
14
- const as = (user: keyof typeof users) =>
15
- Effect.provideService(CurrentUser, users[user]);
11
+ const as = (user: keyof typeof demoUsers) =>
12
+ Effect.provideService(CurrentUser, demoUsers[user]);
16
13
 
17
14
  describe("notes, in process", () => {
18
15
  it.effect("keeps each user's notes their own", () =>