@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.
- package/README.md +7 -1
- package/dist/Action.d.mts +2 -2
- package/dist/Action.mjs +1 -0
- package/dist/ActionCli.mjs +0 -1
- package/dist/ActionHttp.d.mts +1 -1
- package/dist/ActionMcp.d.mts +0 -3
- package/dist/ActionMcp.mjs +0 -5
- 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 +0 -25
- 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.md +0 -28
- 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.md +3 -13
- package/docs/Webhook.md +0 -46
- package/docs/setup.md +0 -5
- 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 +2 -1
package/docs/Testing.md
CHANGED
|
@@ -14,16 +14,13 @@ import * as Action from "@gjermundgaraba/effect-actions/Action";
|
|
|
14
14
|
import { Effect } from "effect";
|
|
15
15
|
|
|
16
16
|
import { CurrentUser } from "../../src/identity/Identity.js";
|
|
17
|
-
import {
|
|
17
|
+
import { demoUsers } from "../../src/identity/users.js";
|
|
18
18
|
import { notes } from "../../src/notes/handlers.js";
|
|
19
19
|
import { NoteNotFound } from "../../src/notes/Notes.js";
|
|
20
20
|
import { NoteStore } from "../../src/notes/store.js";
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// provides it per request.
|
|
25
|
-
const as = (user: keyof typeof users) =>
|
|
26
|
-
Effect.provideService(CurrentUser, users[user]);
|
|
22
|
+
const as = (user: keyof typeof demoUsers) =>
|
|
23
|
+
Effect.provideService(CurrentUser, demoUsers[user]);
|
|
27
24
|
|
|
28
25
|
describe("notes, in process", () => {
|
|
29
26
|
it.effect("keeps each user's notes their own", () =>
|
|
@@ -94,7 +91,6 @@ import { Http } from "../src/Api.js";
|
|
|
94
91
|
import { Greet } from "../src/greeting/Greeting.js";
|
|
95
92
|
import { routes } from "../src/routes.js";
|
|
96
93
|
|
|
97
|
-
// The routes answer in memory: no port, and the same code the server runs.
|
|
98
94
|
it.effect("greets over HTTP and as an MCP tool", () =>
|
|
99
95
|
Effect.gen(function* () {
|
|
100
96
|
const http = yield* ActionHttp.client(Http);
|
|
@@ -152,14 +148,12 @@ it.effect(
|
|
|
152
148
|
"delivers each build a member may see as it finishes, as the subscription selects",
|
|
153
149
|
() =>
|
|
154
150
|
Effect.gen(function* () {
|
|
155
|
-
// One client holds both: the action that finishes builds and the event that reports them.
|
|
156
151
|
const client = yield* Action.client([builds, buildEvents]);
|
|
157
152
|
|
|
158
153
|
const failed = yield* client
|
|
159
154
|
.watch(BuildFinished, { status: "failed" })
|
|
160
155
|
.pipe(Stream.take(1), Stream.runCollect, as("grace"), Effect.forkChild);
|
|
161
156
|
|
|
162
|
-
// The watch starts from now: let it take its first position before builds finish.
|
|
163
157
|
yield* TestClock.adjust("0 millis");
|
|
164
158
|
yield* client
|
|
165
159
|
.finishBuild({ pipeline: "api", status: "failed" })
|
|
@@ -167,12 +161,10 @@ it.effect(
|
|
|
167
161
|
yield* client
|
|
168
162
|
.finishBuild({ pipeline: "web", status: "passed" })
|
|
169
163
|
.pipe(as("ada"));
|
|
170
|
-
// Each build finished notifies: the watch reads at once, with no wait for its next poll.
|
|
171
164
|
yield* client
|
|
172
165
|
.finishBuild({ pipeline: "web", status: "failed" })
|
|
173
166
|
.pipe(as("ada"));
|
|
174
167
|
|
|
175
|
-
// grace sees only the web pipeline, and asked only for failures.
|
|
176
168
|
expect(yield* Fiber.join(failed)).toMatchObject([
|
|
177
169
|
{
|
|
178
170
|
_tag: "Occurrence",
|
|
@@ -192,8 +184,6 @@ it.effect(
|
|
|
192
184
|
.finishBuild({ pipeline: "web", status: "passed" })
|
|
193
185
|
.pipe(as("ada"));
|
|
194
186
|
|
|
195
|
-
// A cursor of the store before a restart, its offset one this store has too: what it named
|
|
196
|
-
// is gone, so the watch is told.
|
|
197
187
|
const resumed = yield* client
|
|
198
188
|
.watch(BuildFinished, {}, { cursor: "an-earlier-epoch:0" })
|
|
199
189
|
.pipe(Stream.take(2), Stream.runCollect, as("grace"));
|
package/docs/Webhook.md
CHANGED
|
@@ -27,7 +27,6 @@ import { Login } from "./identity/Identity.js";
|
|
|
27
27
|
import { authenticate } from "./identity/verifier.js";
|
|
28
28
|
import { Rpc } from "./Rpc.js";
|
|
29
29
|
|
|
30
|
-
// Every domain's implementation, once: the events beside the actions, in one list.
|
|
31
30
|
const implementations = [
|
|
32
31
|
builds,
|
|
33
32
|
logs,
|
|
@@ -37,17 +36,6 @@ const implementations = [
|
|
|
37
36
|
deployEvents,
|
|
38
37
|
];
|
|
39
38
|
|
|
40
|
-
// The actions as MCP tools and the events as MCP events, at `/mcp`. A client polls with
|
|
41
|
-
// `events/poll`, or holds `events/stream` open and is pushed each occurrence. Both events are
|
|
42
|
-
// protected, so each request needs a member's token, and each subscriber sees what its token
|
|
43
|
-
// lets it. This long-lived process holds the deploy event's `subscription` hook between polls and
|
|
44
|
-
// its log in memory, and pushes each occurrence: a `requestScoped` endpoint would serve the events
|
|
45
|
-
// by poll and webhook alone, and a serverless host, where no process lasts, check sources alone,
|
|
46
|
-
// without `Webhook.layer`.
|
|
47
|
-
//
|
|
48
|
-
// A tool answers one result, so `tailLog` would collect a whole log before answering, all of it
|
|
49
|
-
// in a model's context: its implementation, `logs`, is left out, and the endpoint serves every
|
|
50
|
-
// other action and every event.
|
|
51
39
|
const mcp = ActionMcp.layerHttp(
|
|
52
40
|
[builds, buildEvents, statusEvents, deploys, deployEvents],
|
|
53
41
|
{
|
|
@@ -57,25 +45,13 @@ const mcp = ActionMcp.layerHttp(
|
|
|
57
45
|
}
|
|
58
46
|
);
|
|
59
47
|
|
|
60
|
-
// The same implementations as routes under `/api`, for a client that speaks HTTP rather than
|
|
61
|
-
// MCP: one implementation of each event serves every surface.
|
|
62
48
|
const api = ActionHttp.layer(Http, implementations);
|
|
63
49
|
|
|
64
|
-
// And as Effect RPC at `/rpc`, over HTTP. A watch is a stream rpc that never ends, so the
|
|
65
|
-
// serialization frames each element as it is sent: `layerNdjson` does, where `layerJson` would
|
|
66
|
-
// answer once every stream of the request ended, which a watch never does.
|
|
67
50
|
const rpc = ActionRpc.layer(Rpc, implementations).pipe(
|
|
68
51
|
Layer.provide(RpcServer.layerProtocolHttp({ path: "/rpc" })),
|
|
69
52
|
Layer.provide(RpcSerialization.layerNdjson)
|
|
70
53
|
);
|
|
71
54
|
|
|
72
|
-
// Webhook delivery for MCP and HTTP alike: a member names a callback URL and a secret, with
|
|
73
|
-
// MCP's `events/subscribe` or `POST /api/events/<name>/subscribe`, and each occurrence it may see
|
|
74
|
-
// is POSTed there, signed. One layer holds the subscriptions of both, so either surface refreshes
|
|
75
|
-
// one made over the other. `persist` keeps each in a `KeyValueStore`, so a restart goes on
|
|
76
|
-
// delivering from where it reached, as the subscriber `Login`'s `schema` keeps. It needs an
|
|
77
|
-
// `HttpClient` to POST, `Dns` to check where each callback resolves, and the store, which
|
|
78
|
-
// `server.ts` provides.
|
|
79
55
|
export const routes = Layer.mergeAll(mcp, api, rpc).pipe(
|
|
80
56
|
Layer.provide(authenticate),
|
|
81
57
|
Layer.provide(Webhook.layer({ persist: true }))
|
|
@@ -103,12 +79,8 @@ import { Http } from "../src/Api.js";
|
|
|
103
79
|
import { routes } from "../src/routes.js";
|
|
104
80
|
import { inMemory } from "./support/memory.js";
|
|
105
81
|
|
|
106
|
-
// The subscriber's secret: `whsec_` and 24 to 64 random bytes in base64. The server signs each
|
|
107
|
-
// POST with it, and the subscriber checks each POST it receives against it.
|
|
108
82
|
const secret = `whsec_${Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString("base64")}`;
|
|
109
83
|
|
|
110
|
-
// A subscriber's check, as Standard Webhooks defines it: an HMAC-SHA256 of the id, the
|
|
111
|
-
// timestamp and the body exactly as received, among the space-separated `v1,` signatures.
|
|
112
84
|
const signed = async (request: Request, body: string) => {
|
|
113
85
|
const key = await crypto.subtle.importKey(
|
|
114
86
|
"raw",
|
|
@@ -146,8 +118,6 @@ interface Received {
|
|
|
146
118
|
readonly signed: boolean;
|
|
147
119
|
}
|
|
148
120
|
|
|
149
|
-
// grace's server: it answers the verification POST by echoing its challenge, which proves it
|
|
150
|
-
// asked for these deliveries, and acknowledges each delivery with a 2xx.
|
|
151
121
|
const subscriber =
|
|
152
122
|
(received: Queue.Enqueue<Received>) => async (request: Request) => {
|
|
153
123
|
const text = await request.text();
|
|
@@ -201,20 +171,14 @@ describe("the service, by webhook", () => {
|
|
|
201
171
|
),
|
|
202
172
|
});
|
|
203
173
|
|
|
204
|
-
// The answer names the subscription, which each POST's `x-mcp-subscription-id`
|
|
205
|
-
// carries, and when to subscribe again to keep it: one not refreshed by then ends.
|
|
206
174
|
const subscription = yield* subscribe;
|
|
207
175
|
const verification = yield* Queue.take(received);
|
|
208
176
|
|
|
209
177
|
yield* ada.finishBuild({ pipeline: "api", status: "passed" });
|
|
210
|
-
// Each build finished notifies `build.finished`: the server asks the store at once, and
|
|
211
|
-
// POSTs what it answers.
|
|
212
178
|
yield* ada.finishBuild({ pipeline: "web", status: "passed" });
|
|
213
179
|
|
|
214
|
-
// grace sees only the web pipeline.
|
|
215
180
|
const delivery = yield* Queue.take(received);
|
|
216
181
|
|
|
217
|
-
// The same request again refreshes the subscription, and answers how it is going.
|
|
218
182
|
const refreshed = yield* subscribe;
|
|
219
183
|
|
|
220
184
|
expect(verification).toMatchObject({
|
|
@@ -262,12 +226,8 @@ export interface NotifierOptions {
|
|
|
262
226
|
readonly publicUrl: string;
|
|
263
227
|
}
|
|
264
228
|
|
|
265
|
-
// The route build-events POSTs each failed build to, where the watch below reads them: the
|
|
266
|
-
// watch's delivery appends its path to this process's public URL.
|
|
267
229
|
const receiver = Webhook.layerReceiver({ path: "/hooks/builds" });
|
|
268
230
|
|
|
269
|
-
// A second service, the notifier: it watches build.finished for failures by webhook, the
|
|
270
|
-
// server POSTing each to it, rather than holding a stream open to build-events.
|
|
271
231
|
const watcher = ({ buildEvents, token, publicUrl }: NotifierOptions) =>
|
|
272
232
|
Layer.effectDiscard(
|
|
273
233
|
Effect.gen(function* () {
|
|
@@ -287,8 +247,6 @@ const watcher = ({ buildEvents, token, publicUrl }: NotifierOptions) =>
|
|
|
287
247
|
{ status: "failed" },
|
|
288
248
|
{
|
|
289
249
|
mode: Webhook.delivery({ baseUrl: publicUrl }),
|
|
290
|
-
// Where it reached is kept, so a restart resumes there; failures the server no
|
|
291
|
-
// longer keeps arrive as a Gap instead.
|
|
292
250
|
resume: {
|
|
293
251
|
store: yield* KeyValueStore.KeyValueStore,
|
|
294
252
|
key: "failed-builds",
|
|
@@ -296,15 +254,11 @@ const watcher = ({ buildEvents, token, publicUrl }: NotifierOptions) =>
|
|
|
296
254
|
}
|
|
297
255
|
)
|
|
298
256
|
.pipe(
|
|
299
|
-
// A Gap says failures were missed, which no notice can name: it is logged, for an
|
|
300
|
-
// operator to look the builds up.
|
|
301
257
|
Stream.runForEach((received) =>
|
|
302
258
|
Predicate.isTagged(received, "Occurrence")
|
|
303
259
|
? notices.add(received.data)
|
|
304
260
|
: Effect.logWarning("Failed builds were missed", received)
|
|
305
261
|
),
|
|
306
|
-
// A failure no reconnect outlasts, such as a wrong PUBLIC_URL or a revoked token,
|
|
307
|
-
// ends the watch: it is logged here, for a deployment to alert on or restart by.
|
|
308
262
|
Effect.tapCause(Effect.logError)
|
|
309
263
|
)
|
|
310
264
|
);
|
package/docs/setup.md
CHANGED
|
@@ -53,8 +53,6 @@ One action, served as a JSON route and as an MCP tool: four declarations, each i
|
|
|
53
53
|
import * as Action from "@gjermundgaraba/effect-actions/Action";
|
|
54
54
|
import { Schema } from "effect";
|
|
55
55
|
|
|
56
|
-
// A contract: what the action takes and answers, whether it changes anything, and who may
|
|
57
|
-
// call it. It imports no server code, so any client can import it, a browser page included.
|
|
58
56
|
export const Greet = Action.make("greet", {
|
|
59
57
|
description: "Greet someone by name.",
|
|
60
58
|
input: { name: Schema.String },
|
|
@@ -70,7 +68,6 @@ import { Effect } from "effect";
|
|
|
70
68
|
|
|
71
69
|
import { Greet } from "./Greeting.js";
|
|
72
70
|
|
|
73
|
-
// The implementation: the contract bound to its handler. Every surface takes this one value.
|
|
74
71
|
export const greeting = Action.implement(Greet, ({ name }) =>
|
|
75
72
|
Effect.succeed(`Hello, ${name}!`)
|
|
76
73
|
);
|
|
@@ -81,7 +78,6 @@ import * as ActionHttp from "@gjermundgaraba/effect-actions/ActionHttp";
|
|
|
81
78
|
|
|
82
79
|
import { Greet } from "./greeting/Greeting.js";
|
|
83
80
|
|
|
84
|
-
// The HTTP binding: `POST /api/greet`. The server serves it and the client calls it.
|
|
85
81
|
export const Http = ActionHttp.make([Greet]);
|
|
86
82
|
```
|
|
87
83
|
|
|
@@ -93,7 +89,6 @@ import { Layer } from "effect";
|
|
|
93
89
|
import { Http } from "./Api.js";
|
|
94
90
|
import { greeting } from "./greeting/handlers.js";
|
|
95
91
|
|
|
96
|
-
// One implementation, two surfaces: a JSON route and an MCP tool at `/mcp`.
|
|
97
92
|
export const routes = Layer.mergeAll(
|
|
98
93
|
ActionHttp.layer(Http, greeting),
|
|
99
94
|
ActionMcp.layerHttp(greeting, { name: "greetings", version: "1.0.0" })
|
package/examples/README.md
CHANGED
|
@@ -44,4 +44,10 @@ Every example follows one layout, which is the one we recommend for an applicati
|
|
|
44
44
|
|
|
45
45
|
In a monorepo, the contract files and the binding are the package that the server, the CLI and the website all depend on; the rest is the server.
|
|
46
46
|
|
|
47
|
-
Every token, user and store in these examples is a demo. Each place that must change before a deployment
|
|
47
|
+
Every token, user and store in these examples is a demo. Each place that must change before a deployment says so in its name (`demoMembers`, `layerMemory`, `layerDemo`, `fakeFarm`, `scripted`), except these:
|
|
48
|
+
|
|
49
|
+
- build-events' `finishBuild` action, which a member calls where a CI runner would, and its build steps, which pass at once.
|
|
50
|
+
- notes-service's `verify`, which takes a token as a user's name where a real verifier checks a signed token, and its `https://auth.example.com` authorization server, where no server is.
|
|
51
|
+
- notes-service's `stdio.ts`, whose in-memory store holds notes of its own, not the running server's.
|
|
52
|
+
- quickstart's `client.ts`, whose `baseUrl` a deployed client reads from configuration.
|
|
53
|
+
- kitchen-sink's `seed.ts`, which files a first issue in `acme` as `ada`, since the server's stores start empty.
|
|
@@ -4,23 +4,17 @@ import { Chat, Tool } from "effect/ai";
|
|
|
4
4
|
import type { LanguageModel, Prompt } from "effect/ai";
|
|
5
5
|
|
|
6
6
|
import type { Member } from "./identity/Identity.js";
|
|
7
|
-
import {
|
|
7
|
+
import { demoMembers } from "./identity/members.js";
|
|
8
8
|
import { Permissions } from "./identity/permissions.js";
|
|
9
|
-
import {
|
|
9
|
+
import { demoTasksFile } from "./platform/tasks-file.js";
|
|
10
10
|
import { scripted } from "./scripted.js";
|
|
11
11
|
import { actingFor, layer, toolkit } from "./toolkit.js";
|
|
12
12
|
|
|
13
|
-
// Entrypoint: `node --import tsx examples/assistant/src/agent.ts`.
|
|
14
|
-
// A model working on the team's tasks with tools that run in this process: no server and no
|
|
15
|
-
// port. Every tool call passes through the same `authorize` and handlers as the commands.
|
|
16
|
-
|
|
17
|
-
// What a turn answers with: its text, the calls the model made and what they returned.
|
|
18
13
|
type Response = LanguageModel.GenerateTextResponse<
|
|
19
14
|
typeof toolkit.tools,
|
|
20
15
|
"opaque"
|
|
21
16
|
>;
|
|
22
17
|
|
|
23
|
-
// Printing only.
|
|
24
18
|
const report = (member: Member) => (response: Response) => {
|
|
25
19
|
const waiting = new Set(
|
|
26
20
|
response.content.flatMap((part) =>
|
|
@@ -51,33 +45,21 @@ const report = (member: Member) => (response: Response) => {
|
|
|
51
45
|
);
|
|
52
46
|
};
|
|
53
47
|
|
|
54
|
-
// One turn of a conversation: the model reads the prompt and may call tools. The member is
|
|
55
|
-
// provided around the whole turn, so the approval rule, `authorize` and the handlers of every
|
|
56
|
-
// call in it see that member.
|
|
57
48
|
const turn = (chat: Chat.Chat, member: Member, prompt: Prompt.RawInput) =>
|
|
58
49
|
chat
|
|
59
50
|
.generateText({ prompt, toolkit })
|
|
60
51
|
.pipe(actingFor(member), Effect.tap(report(member)));
|
|
61
52
|
|
|
62
53
|
const conversations = Effect.gen(function* () {
|
|
63
|
-
// A `Chat` keeps the conversation's history, and approval depends on it: an answer is matched
|
|
64
|
-
// to its request by `approvalId` in the prompt. The answer alone, sent through a bare
|
|
65
|
-
// `LanguageModel.generateText`, runs nothing. Without a `Chat`, send again the assistant
|
|
66
|
-
// message holding the call and its request, before the answer.
|
|
67
54
|
const withAda = yield* Chat.empty;
|
|
68
55
|
|
|
69
|
-
// Ada's own task is added at once. The task for Grace is not run: the turn ends with a
|
|
70
|
-
// request for approval in place of that call's result.
|
|
71
56
|
const asked = yield* turn(
|
|
72
57
|
withAda,
|
|
73
|
-
|
|
58
|
+
demoMembers.ada,
|
|
74
59
|
"Add the venue for me, invitations for Grace."
|
|
75
60
|
);
|
|
76
61
|
|
|
77
|
-
|
|
78
|
-
// in which an approved call runs before the model is asked again. It runs as whoever that
|
|
79
|
-
// turn runs as, so the host provides the member who was asked: Ada still.
|
|
80
|
-
yield* turn(withAda, members.ada, [
|
|
62
|
+
yield* turn(withAda, demoMembers.ada, [
|
|
81
63
|
{
|
|
82
64
|
role: "tool",
|
|
83
65
|
content: asked.content.flatMap((part) =>
|
|
@@ -94,10 +76,9 @@ const conversations = Effect.gen(function* () {
|
|
|
94
76
|
},
|
|
95
77
|
]);
|
|
96
78
|
|
|
97
|
-
// The same toolkit, another member. The guest may read, so the write comes back refused.
|
|
98
79
|
yield* turn(
|
|
99
80
|
yield* Chat.empty,
|
|
100
|
-
|
|
81
|
+
demoMembers.guest,
|
|
101
82
|
"What is there to do? Add lunch for me."
|
|
102
83
|
);
|
|
103
84
|
});
|
|
@@ -110,8 +91,7 @@ const adding = (id: string, title: string, assignee: string) =>
|
|
|
110
91
|
params: { title, assignee },
|
|
111
92
|
}) as const;
|
|
112
93
|
|
|
113
|
-
|
|
114
|
-
const model = scripted(
|
|
94
|
+
const demoModel = scripted(
|
|
115
95
|
[
|
|
116
96
|
adding("1", "Book the venue", "ada"),
|
|
117
97
|
adding("2", "Send invitations", "grace"),
|
|
@@ -126,11 +106,8 @@ const model = scripted(
|
|
|
126
106
|
conversations.pipe(
|
|
127
107
|
Effect.provide(
|
|
128
108
|
Layer.mergeAll(
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
model,
|
|
132
|
-
// Startup services only: where tasks and permissions are kept.
|
|
133
|
-
layer.pipe(Layer.provide([tasksFile, Permissions.layerMemory]))
|
|
109
|
+
demoModel,
|
|
110
|
+
layer.pipe(Layer.provide([demoTasksFile, Permissions.layerMemory]))
|
|
134
111
|
).pipe(Layer.provideMerge(NodeServices.layer))
|
|
135
112
|
),
|
|
136
113
|
NodeRuntime.runMain()
|
|
@@ -9,22 +9,15 @@ import { commands } from "./commands.js";
|
|
|
9
9
|
import { CurrentCaller } from "./identity/Identity.js";
|
|
10
10
|
import type { Operator } from "./identity/Identity.js";
|
|
11
11
|
import { Permissions } from "./identity/permissions.js";
|
|
12
|
-
import {
|
|
12
|
+
import { demoTasksFile } from "./platform/tasks-file.js";
|
|
13
13
|
|
|
14
|
-
// Entrypoint: `node --import tsx examples/assistant/src/cli.ts list-tasks`.
|
|
15
|
-
// The operator's own tool. No credential arrives at a terminal, so this host names the caller
|
|
16
|
-
// itself: whoever runs the command, as the trusted operator. Being local earns nothing more:
|
|
17
|
-
// `authorize` runs here as for a model's tool call, and admits the operator by its kind.
|
|
18
14
|
const operator: Operator = { kind: "operator", id: userInfo().username };
|
|
19
15
|
|
|
20
16
|
const assistant = commands.pipe(
|
|
21
|
-
Command.provide(Layer.mergeAll(
|
|
17
|
+
Command.provide(Layer.mergeAll(demoTasksFile, Permissions.layerMemory)),
|
|
22
18
|
Command.provideSync(CurrentCaller, operator)
|
|
23
19
|
);
|
|
24
20
|
|
|
25
|
-
// A command's defect, such as a tasks file that does not decode, `runMain` reports on stdout.
|
|
26
|
-
// `logToStderr`, applied last, moves that to stderr, so `assistant list-tasks --json | jq` reads
|
|
27
|
-
// results alone.
|
|
28
21
|
Command.run(assistant, { version: "1.0.0" }).pipe(
|
|
29
22
|
Effect.provide(NodeServices.layer),
|
|
30
23
|
ActionCli.logToStderr,
|
|
@@ -3,15 +3,9 @@ import * as ActionCli from "@gjermundgaraba/effect-actions/ActionCli";
|
|
|
3
3
|
import { identity } from "./identity/handlers.js";
|
|
4
4
|
import { tasks } from "./tasks/handlers.js";
|
|
5
5
|
|
|
6
|
-
// The same implementations as local commands, `assistant <action>`: a command runs the handler
|
|
7
|
-
// in this process, behind the same `authorize`. `add-task` and `who-am-i` are as derived; the
|
|
8
|
-
// others each change their syntax or output.
|
|
9
6
|
export const commands = ActionCli.make([tasks, identity], {
|
|
10
7
|
name: "assistant",
|
|
11
8
|
commands: {
|
|
12
|
-
// `assistant list-tasks -a ada`: a short flag beside `--assignee`. `render` prints a line
|
|
13
|
-
// per task for a person, and adds `--json` for the JSON a script reads. `description` is the
|
|
14
|
-
// command's help, saying what a line holds; the action's stays what a model reads.
|
|
15
9
|
listTasks: {
|
|
16
10
|
description:
|
|
17
11
|
"Print one line per task: [x] when done, then its id, title and assignee.",
|
|
@@ -24,9 +18,7 @@ export const commands = ActionCli.make([tasks, identity], {
|
|
|
24
18
|
)
|
|
25
19
|
.join("\n"),
|
|
26
20
|
},
|
|
27
|
-
// `assistant complete-task 2`: an argument instead of `--id 2`.
|
|
28
21
|
completeTask: { positional: ["id"] },
|
|
29
|
-
// `assistant annotate-task --id 2 < notes.md`: the notes are piped in, not a flag.
|
|
30
22
|
annotateTask: { stdin: "notes" },
|
|
31
23
|
},
|
|
32
24
|
});
|
|
@@ -1,26 +1,16 @@
|
|
|
1
1
|
import * as Action from "@gjermundgaraba/effect-actions/Action";
|
|
2
2
|
import { Context, Schema } from "effect";
|
|
3
3
|
|
|
4
|
-
/** A teammate: what a sign-in can name. The assistant acts for one in each turn. */
|
|
5
4
|
export interface Member {
|
|
6
5
|
readonly kind: "member";
|
|
7
6
|
readonly id: string;
|
|
8
7
|
}
|
|
9
8
|
|
|
10
|
-
/**
|
|
11
|
-
* Whoever runs the machine. Only a host builds one, in its own code: nothing that turns a
|
|
12
|
-
* credential into a caller is typed to return it, so no token can claim to be the operator.
|
|
13
|
-
*/
|
|
14
9
|
export interface Operator {
|
|
15
10
|
readonly kind: "operator";
|
|
16
11
|
readonly id: string;
|
|
17
12
|
}
|
|
18
13
|
|
|
19
|
-
/**
|
|
20
|
-
* Who is calling. A protected action declares it, `caller: CurrentCaller`. Nothing here
|
|
21
|
-
* authenticates anyone: the host provides it around each call, as the member a model acts for
|
|
22
|
-
* or as the operator at the terminal.
|
|
23
|
-
*/
|
|
24
14
|
export class CurrentCaller extends Context.Service<
|
|
25
15
|
CurrentCaller,
|
|
26
16
|
Member | Operator
|
|
@@ -5,16 +5,12 @@ import { CurrentCaller } from "./Identity.js";
|
|
|
5
5
|
import { Permissions } from "./permissions.js";
|
|
6
6
|
import type { Permission } from "./permissions.js";
|
|
7
7
|
|
|
8
|
-
// The one rule, for a model's tool call and for a command alike. The permission store is a
|
|
9
|
-
// startup service, so this Effect gets it; the caller differs per call, so the function it
|
|
10
|
-
// returns reads it.
|
|
11
8
|
export const authorize = Effect.gen(function* () {
|
|
12
9
|
const permissions = yield* Permissions;
|
|
13
10
|
|
|
14
11
|
return Effect.fn("authorize")(function* (action: Action.Any) {
|
|
15
12
|
const caller = yield* CurrentCaller;
|
|
16
13
|
|
|
17
|
-
// The operator is trusted, and has no entry in the store. Only a host can name one.
|
|
18
14
|
if (caller.kind === "operator") {
|
|
19
15
|
return;
|
|
20
16
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { Member } from "./Identity.js";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
// `Member`. Typed as members alone: no entry, and so no sign-in, can be the operator.
|
|
5
|
-
export const members = {
|
|
3
|
+
export const demoMembers = {
|
|
6
4
|
ada: { kind: "member", id: "ada" },
|
|
7
5
|
grace: { kind: "member", id: "grace" },
|
|
8
6
|
guest: { kind: "member", id: "guest" },
|
|
@@ -2,10 +2,6 @@ import { Context, Effect, Layer } from "effect";
|
|
|
2
2
|
|
|
3
3
|
export type Permission = "tasks:read" | "tasks:write";
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* What each member may do. Permissions are kept here, not in the identity, so changing one
|
|
7
|
-
* takes effect on the member's next call.
|
|
8
|
-
*/
|
|
9
5
|
export class Permissions extends Context.Service<
|
|
10
6
|
Permissions,
|
|
11
7
|
{
|
|
@@ -14,7 +10,6 @@ export class Permissions extends Context.Service<
|
|
|
14
10
|
) => Effect.Effect<readonly Permission[]>;
|
|
15
11
|
}
|
|
16
12
|
>()("assistant/Permissions") {
|
|
17
|
-
// DEMO ONLY: a fixed table. A real application reads its database or its directory.
|
|
18
13
|
static readonly layerMemory = Layer.sync(Permissions, () => {
|
|
19
14
|
const table = new Map<string, readonly Permission[]>([
|
|
20
15
|
["ada", ["tasks:read", "tasks:write"]],
|
|
@@ -8,11 +8,7 @@ import { Task } from "../tasks/Tasks.js";
|
|
|
8
8
|
|
|
9
9
|
const TasksFile = Schema.fromJsonString(Schema.Array(Task));
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
// process, by the store's lock, but nothing locks the file across processes: the agent and a
|
|
13
|
-
// command writing at once lose a task. A real application uses a database. The agent and the
|
|
14
|
-
// commands are separate processes: this file is what lets one see what the other wrote.
|
|
15
|
-
export const tasksFile = Layer.effect(
|
|
11
|
+
export const demoTasksFile = Layer.effect(
|
|
16
12
|
TaskStore,
|
|
17
13
|
Effect.gen(function* () {
|
|
18
14
|
const files = yield* FileSystem.FileSystem;
|
|
@@ -32,7 +28,6 @@ export const tasksFile = Layer.effect(
|
|
|
32
28
|
const save = (tasks: readonly Task[]) =>
|
|
33
29
|
Schema.encodeEffect(TasksFile)(tasks).pipe(
|
|
34
30
|
Effect.flatMap((json) => files.writeFileString(file, json)),
|
|
35
|
-
// A log, not a result: a command sends its handler's logs to stderr.
|
|
36
31
|
Effect.tap(() => Effect.log(`Saved ${tasks.length} tasks to ${file}.`)),
|
|
37
32
|
Effect.orDie
|
|
38
33
|
);
|
|
@@ -2,10 +2,6 @@ import { Effect, Layer, Ref, Stream } from "effect";
|
|
|
2
2
|
import { LanguageModel } from "effect/ai";
|
|
3
3
|
import type { Response } from "effect/ai";
|
|
4
4
|
|
|
5
|
-
// DEMO ONLY: a model that needs no API key. It answers its first request with the first reply
|
|
6
|
-
// it was given, its second with the second, whatever the prompt says, and with nothing once
|
|
7
|
-
// they run out. It stands exactly where a provider's model layer goes, such as
|
|
8
|
-
// `OpenAiLanguageModel.model(...)` of `@effect/ai-openai`.
|
|
9
5
|
export const scripted = (
|
|
10
6
|
...replies: ReadonlyArray<readonly Response.PartEncoded[]>
|
|
11
7
|
) =>
|
|
@@ -14,7 +14,6 @@ export const Task = Schema.Struct({
|
|
|
14
14
|
|
|
15
15
|
export type Task = typeof Task.Type;
|
|
16
16
|
|
|
17
|
-
// A declared error is part of the contract.
|
|
18
17
|
export class TaskNotFound extends Schema.TaggedError<
|
|
19
18
|
TaskNotFound,
|
|
20
19
|
{ readonly brand: unique symbol }
|
|
@@ -22,7 +21,6 @@ export class TaskNotFound extends Schema.TaggedError<
|
|
|
22
21
|
id: Schema.String,
|
|
23
22
|
}) {}
|
|
24
23
|
|
|
25
|
-
// A field's description is what a model reads beside the tool, and the flag's help text.
|
|
26
24
|
const Id = Schema.String.annotate({
|
|
27
25
|
description: "The id listed beside the task.",
|
|
28
26
|
});
|
|
@@ -6,9 +6,6 @@ import { CurrentCaller } from "../identity/Identity.js";
|
|
|
6
6
|
import { TaskStore } from "./store.js";
|
|
7
7
|
import { Tasks } from "./Tasks.js";
|
|
8
8
|
|
|
9
|
-
// The builder gets the store. `CurrentCaller` is read per call. No handler knows whether a model
|
|
10
|
-
// or a person at a terminal called it, and none checks a permission: `authorize` has run before
|
|
11
|
-
// it.
|
|
12
9
|
export const tasks = Action.implement(
|
|
13
10
|
Tasks,
|
|
14
11
|
Effect.gen(function* () {
|
|
@@ -5,14 +5,10 @@ import { TaskNotFound } from "./Tasks.js";
|
|
|
5
5
|
|
|
6
6
|
type TaskList = readonly Task[];
|
|
7
7
|
|
|
8
|
-
// Every method reads the whole list, and a change writes it back: a layer says only where the
|
|
9
|
-
// list is kept, in memory below or in a file (src/platform/tasks-file.ts).
|
|
10
8
|
const make = (
|
|
11
9
|
load: Effect.Effect<TaskList>,
|
|
12
10
|
save: (tasks: TaskList) => Effect.Effect<void>
|
|
13
11
|
) => {
|
|
14
|
-
// A model's tool calls in one turn run concurrently. Two changes reading the list before
|
|
15
|
-
// either writes it would lose one, so a change holds this lock from its read to its write.
|
|
16
12
|
const lock = Semaphore.makeUnsafe(1);
|
|
17
13
|
|
|
18
14
|
const change = Effect.fn("TaskStore.change")(function* (
|
|
@@ -61,7 +57,6 @@ const make = (
|
|
|
61
57
|
});
|
|
62
58
|
};
|
|
63
59
|
|
|
64
|
-
/** The team's tasks. Its methods require nothing: a layer captures where the tasks are kept. */
|
|
65
60
|
export class TaskStore extends Context.Service<
|
|
66
61
|
TaskStore,
|
|
67
62
|
{
|
|
@@ -78,10 +73,8 @@ export class TaskStore extends Context.Service<
|
|
|
78
73
|
) => Effect.Effect<Task, TaskNotFound>;
|
|
79
74
|
}
|
|
80
75
|
>()("assistant/TaskStore") {
|
|
81
|
-
/** The store over a list kept by `load` and `save`, each change serialized. */
|
|
82
76
|
static readonly make = make;
|
|
83
77
|
|
|
84
|
-
/** Memory, lost with the process: what a test provides. */
|
|
85
78
|
static readonly layerMemory = Layer.effect(
|
|
86
79
|
TaskStore,
|
|
87
80
|
Effect.map(Ref.make<TaskList>([]), (tasks) =>
|
|
@@ -7,12 +7,7 @@ import { CurrentCaller } from "./identity/Identity.js";
|
|
|
7
7
|
import type { Member } from "./identity/Identity.js";
|
|
8
8
|
import { tasks } from "./tasks/handlers.js";
|
|
9
9
|
|
|
10
|
-
// `actions.layer` holds the handlers, and requires the startup services their builders read;
|
|
11
|
-
// neither it nor `actions.toolkit` holds a caller.
|
|
12
10
|
const actions = ActionToolkit.make([tasks, identity], {
|
|
13
|
-
// One rule over every call a model makes, asked before the call runs. A read runs at once. A
|
|
14
|
-
// write waits for a person to approve it, except a task the caller adds to their own list:
|
|
15
|
-
// `call.name` narrows `call.input` to that action's. Without a caller, it asks.
|
|
16
11
|
needsApproval: (call) =>
|
|
17
12
|
!call.action.readOnly &&
|
|
18
13
|
Effect.map(
|
|
@@ -25,8 +20,6 @@ const actions = ActionToolkit.make([tasks, identity], {
|
|
|
25
20
|
),
|
|
26
21
|
});
|
|
27
22
|
|
|
28
|
-
// A title for this assistant's approval display. The model still calls the tool `addTask`.
|
|
29
|
-
// The later definition wins; annotating the generated tool keeps its handler and approval rule.
|
|
30
23
|
export const toolkit = Toolkit.merge(
|
|
31
24
|
actions.toolkit,
|
|
32
25
|
Toolkit.make(actions.toolkit.tools.addTask.annotate(Tool.Title, "Add a task"))
|
|
@@ -34,9 +27,5 @@ export const toolkit = Toolkit.merge(
|
|
|
34
27
|
|
|
35
28
|
export const { layer } = actions;
|
|
36
29
|
|
|
37
|
-
// One toolkit serves every member, so who a turn runs as is provided around that turn. This is
|
|
38
|
-
// how this example names a model's caller, and it takes a `Member`. The tools themselves accept
|
|
39
|
-
// any `CurrentCaller`, so keeping the operator away from a model is this function's doing, and
|
|
40
|
-
// that of every host using it.
|
|
41
30
|
export const actingFor = (member: Member) =>
|
|
42
31
|
Effect.provideService(CurrentCaller, member);
|
|
@@ -10,15 +10,10 @@ import type { Operator } from "../src/identity/Identity.js";
|
|
|
10
10
|
import { Permissions } from "../src/identity/permissions.js";
|
|
11
11
|
import { TaskStore } from "../src/tasks/store.js";
|
|
12
12
|
|
|
13
|
-
// What the commands add to the implementations: their syntax and what they print. The services
|
|
14
|
-
// are provided around a test, not on the command as cli.ts does, so the commands of one test
|
|
15
|
-
// share one store in memory.
|
|
16
13
|
const services = Layer.mergeAll(TaskStore.layerMemory, Permissions.layerMemory);
|
|
17
14
|
|
|
18
15
|
const operator: Operator = { kind: "operator", id: "root" };
|
|
19
16
|
|
|
20
|
-
// Effect's own runner, given arguments instead of the process's, and stdin the text given here.
|
|
21
|
-
// The console of `it.effect` keeps every line printed, so a run returns the lines it added.
|
|
22
17
|
const run = (args: readonly string[], stdin = "") =>
|
|
23
18
|
Effect.gen(function* () {
|
|
24
19
|
const before = (yield* TestConsole.logLines).length;
|