pi-durable-bot 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -4
- package/dist/agent/agent.js +20 -11
- package/dist/agent/agent.js.map +1 -1
- package/dist/agent/judge.js +4 -4
- package/dist/agent/judge.js.map +1 -1
- package/dist/agent/models.js +32 -1
- package/dist/agent/models.js.map +1 -1
- package/dist/bot/bot.js +50 -24
- package/dist/bot/bot.js.map +1 -1
- package/dist/bot/definition.js +64 -22
- package/dist/bot/definition.js.map +1 -1
- package/dist/bot/prompt.js +2 -1
- package/dist/bot/prompt.js.map +1 -1
- package/dist/bot/timers.js +1 -112
- package/dist/bot/timers.js.map +1 -1
- package/dist/cli/init.js +4 -3
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/pots.js +2 -1
- package/dist/cli/pots.js.map +1 -1
- package/dist/cli/trace.js +172 -0
- package/dist/cli/trace.js.map +1 -0
- package/dist/core/channel.js +49 -1
- package/dist/core/channel.js.map +1 -1
- package/dist/core/judge.js +0 -1
- package/dist/core/judge.js.map +1 -1
- package/dist/gateway/channels.js +69 -0
- package/dist/gateway/channels.js.map +1 -0
- package/dist/gateway/config.js +4 -0
- package/dist/gateway/config.js.map +1 -0
- package/dist/gateway/errors.js +54 -0
- package/dist/gateway/errors.js.map +1 -0
- package/dist/gateway/gateway.js +197 -133
- package/dist/gateway/gateway.js.map +1 -1
- package/dist/gateway/inbox.js +80 -0
- package/dist/gateway/inbox.js.map +1 -0
- package/dist/gateway/log.js +80 -93
- package/dist/gateway/log.js.map +1 -1
- package/dist/gateway/outbox.js +59 -45
- package/dist/gateway/outbox.js.map +1 -1
- package/dist/gateway/telemetry.js +131 -0
- package/dist/gateway/telemetry.js.map +1 -0
- package/dist/gateway/timers.js +121 -0
- package/dist/gateway/timers.js.map +1 -0
- package/dist/gateway/tracing.js +187 -0
- package/dist/gateway/tracing.js.map +1 -0
- package/dist/gateway/waker.js +195 -137
- package/dist/gateway/waker.js.map +1 -1
- package/docs/channels/slack.md +1 -1
- package/package.json +8 -2
package/dist/core/channel.js
CHANGED
|
@@ -1,4 +1,52 @@
|
|
|
1
1
|
// The interface between the gateway core and the channel adapters.
|
|
2
2
|
// Only the five common words: channel, place, thread, message, person. No platform concepts.
|
|
3
|
-
|
|
3
|
+
import { Schema } from "effect";
|
|
4
|
+
/** A channel, a DM or a group chat. "dm" is a one-to-one direct message; a DM with more people is a "group". */
|
|
5
|
+
export const PlaceKind = Schema.Literals(["channel", "dm", "group"]);
|
|
6
|
+
export const PlaceInfo = Schema.Struct({
|
|
7
|
+
address: Schema.String,
|
|
8
|
+
name: Schema.optional(Schema.String),
|
|
9
|
+
kind: PlaceKind,
|
|
10
|
+
});
|
|
11
|
+
export const PersonInfo = Schema.Struct({
|
|
12
|
+
address: Schema.String,
|
|
13
|
+
name: Schema.optional(Schema.String),
|
|
14
|
+
realName: Schema.optional(Schema.String),
|
|
15
|
+
});
|
|
16
|
+
export const FileInfo = Schema.Struct({
|
|
17
|
+
name: Schema.optional(Schema.String),
|
|
18
|
+
type: Schema.optional(Schema.String),
|
|
19
|
+
url: Schema.optional(Schema.String),
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* One thing that happened in a channel, in common words. The adapter makes these from the raw events it stored, and
|
|
23
|
+
* the gateway checks them against this schema before it logs them.
|
|
24
|
+
* kind: "message", "edit", "delete", "reaction_added", "reaction_removed", "join", "leave", "update", or another plain word.
|
|
25
|
+
*/
|
|
26
|
+
export const Happening = Schema.Struct({
|
|
27
|
+
kind: Schema.NonEmptyString,
|
|
28
|
+
/** Unix ms. */
|
|
29
|
+
at: Schema.Finite.check(Schema.isBetween({ minimum: 0, maximum: 8.64e15 })),
|
|
30
|
+
place: Schema.optional(PlaceInfo),
|
|
31
|
+
/** The thread the message is in. */
|
|
32
|
+
thread: Schema.optional(Schema.String),
|
|
33
|
+
/** The message that starts the thread, when the adapter knows it. */
|
|
34
|
+
threadRoot: Schema.optional(Schema.String),
|
|
35
|
+
/** The message this is, or is about. */
|
|
36
|
+
message: Schema.optional(Schema.String),
|
|
37
|
+
/** Who did it. */
|
|
38
|
+
person: Schema.optional(PersonInfo),
|
|
39
|
+
/** Done by this channel account itself. */
|
|
40
|
+
fromMe: Schema.Boolean,
|
|
41
|
+
mentionsMe: Schema.optional(Schema.Boolean),
|
|
42
|
+
/** Message text for "message" and "edit"; the emoji for reactions. */
|
|
43
|
+
text: Schema.optional(Schema.String),
|
|
44
|
+
/** People mentioned in the text. */
|
|
45
|
+
mentions: Schema.optional(Schema.Array(Schema.String)),
|
|
46
|
+
files: Schema.optional(Schema.Array(FileInfo)),
|
|
47
|
+
/** Set on messages this bot sent through the outbox. */
|
|
48
|
+
sendId: Schema.optional(Schema.String),
|
|
49
|
+
/** The platform data this happening comes from. Kept in the log next to the common fields. */
|
|
50
|
+
raw: Schema.optional(Schema.Unknown),
|
|
51
|
+
});
|
|
4
52
|
//# sourceMappingURL=channel.js.map
|
package/dist/core/channel.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../../src/core/channel.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,6FAA6F"}
|
|
1
|
+
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../../src/core/channel.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,6FAA6F;AAC7F,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAQhC,gHAAgH;AAChH,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;AAGrE,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC;IACrC,OAAO,EAAE,MAAM,CAAC,MAAM;IACtB,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC;IACpC,IAA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EAAE,MAAM,CAAC,OAAO;IACtB,UAAU,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC;IAC3C,sEAAsE;IACtE,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC;IACpC,oCAAoC;IACpC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACtD,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC9C,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC;IACtC,8FAA8F;IAC9F,GAAG,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC;CACrC,CAAC,CAAC"}
|
package/dist/core/judge.js
CHANGED
package/dist/core/judge.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"judge.js","sourceRoot":"","sources":["../../src/core/judge.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"judge.js","sourceRoot":"","sources":["../../src/core/judge.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Context, Effect, FiberMap, Layer } from "effect";
|
|
2
|
+
import { GatewayConfig } from "./config.js";
|
|
3
|
+
import { ChannelError, message, NoChannelForAddress, UnknownChannel } from "./errors.js";
|
|
4
|
+
/** How long to wait before the next attempt to connect a channel: 5 s more each time, at most a minute. */
|
|
5
|
+
export const reconnectDelay = (attempt) => Math.min(60_000, 5_000 * attempt);
|
|
6
|
+
/**
|
|
7
|
+
* The channel adapters of one bot and their life: connect, backfill what was missed, keep trying a channel that does
|
|
8
|
+
* not connect, stop. Each adapter has one background task (backfill or reconnect), which ends when it is removed.
|
|
9
|
+
*/
|
|
10
|
+
export class Channels extends Context.Service()("pots/gateway/Channels") {
|
|
11
|
+
static layer = Layer.effect(Channels, Effect.gen(function* () {
|
|
12
|
+
const { print } = yield* GatewayConfig;
|
|
13
|
+
const adapters = new Map();
|
|
14
|
+
const tasks = yield* FiberMap.make();
|
|
15
|
+
const connectOne = (adapter, sink) => Effect.tryPromise({ try: () => adapter.start(sink), catch: (cause) => ChannelError.from(adapter.id, cause) }).pipe(Effect.tap(() => Effect.sync(() => print("channel connected", { channel: adapter.id, me: adapter.me?.address }))), Effect.as(true), Effect.catch((error) => Effect.sync(() => print("channel connect failed", { channel: adapter.id, error: error.message })).pipe(Effect.andThen(Effect.promise(() => adapter.stop().catch(() => { }))), Effect.as(false))), Effect.withSpan("channel.connect", { attributes: { channel: adapter.id } }));
|
|
16
|
+
const backfill = (adapter, sink) => Effect.tryPromise({ try: () => adapter.backfill(sink), catch: (cause) => ChannelError.from(adapter.id, cause) }).pipe(Effect.catch((error) => Effect.sync(() => print("backfill failed", { channel: adapter.id, error: error.message }))), Effect.withSpan("channel.backfill", { attributes: { channel: adapter.id } }));
|
|
17
|
+
const follow = (adapter, connected, sink) => Effect.gen(function* () {
|
|
18
|
+
for (let attempt = 1; !connected; attempt++) {
|
|
19
|
+
yield* Effect.sleep(reconnectDelay(attempt));
|
|
20
|
+
connected = yield* connectOne(adapter, sink(adapter));
|
|
21
|
+
}
|
|
22
|
+
yield* backfill(adapter, sink(adapter));
|
|
23
|
+
}).pipe(FiberMap.run(tasks, adapter.id), Effect.asVoid);
|
|
24
|
+
const connect = (list, sink) => Effect.suspend(() => {
|
|
25
|
+
for (const adapter of list)
|
|
26
|
+
adapters.set(adapter.id, adapter);
|
|
27
|
+
return Effect.forEach(list, (adapter) => connectOne(adapter, sink(adapter)), { concurrency: "unbounded" });
|
|
28
|
+
});
|
|
29
|
+
return Channels.of({
|
|
30
|
+
list: () => [...adapters.values()],
|
|
31
|
+
running: (id) => adapters.get(id),
|
|
32
|
+
adapterFor: (address) => Effect.suspend(() => {
|
|
33
|
+
for (const adapter of adapters.values())
|
|
34
|
+
if (adapter.owns(address))
|
|
35
|
+
return Effect.succeed(adapter);
|
|
36
|
+
return Effect.fail(NoChannelForAddress.of(address));
|
|
37
|
+
}),
|
|
38
|
+
get: (id) => Effect.suspend(() => {
|
|
39
|
+
const adapter = adapters.get(id);
|
|
40
|
+
if (adapter)
|
|
41
|
+
return Effect.succeed(adapter);
|
|
42
|
+
const channels = [...adapters.keys()].join(", ");
|
|
43
|
+
return Effect.fail(new UnknownChannel({ message: `unknown channel ${JSON.stringify(id)}; channels: ${channels}`, channel: id }));
|
|
44
|
+
}),
|
|
45
|
+
connect,
|
|
46
|
+
follow,
|
|
47
|
+
add: (adapter, sink) => Effect.gen(function* () {
|
|
48
|
+
if (adapters.has(adapter.id)) {
|
|
49
|
+
return yield* Effect.fail(ChannelError.from(adapter.id, `channel ${adapter.id} is already running`));
|
|
50
|
+
}
|
|
51
|
+
const [connected] = yield* connect([adapter], sink);
|
|
52
|
+
yield* follow(adapter, connected, sink);
|
|
53
|
+
}),
|
|
54
|
+
remove: (id) => Effect.gen(function* () {
|
|
55
|
+
const adapter = adapters.get(id);
|
|
56
|
+
if (!adapter)
|
|
57
|
+
return;
|
|
58
|
+
adapters.delete(id);
|
|
59
|
+
yield* FiberMap.remove(tasks, id);
|
|
60
|
+
yield* Effect.tryPromise({ try: () => adapter.stop(), catch: (cause) => ChannelError.from(id, cause) });
|
|
61
|
+
}),
|
|
62
|
+
stop: Effect.gen(function* () {
|
|
63
|
+
yield* FiberMap.clear(tasks);
|
|
64
|
+
yield* Effect.forEach([...adapters.values()], (adapter) => Effect.promise(() => adapter.stop().catch((error) => print("channel stop failed", { channel: adapter.id, error: message(error) }))), { concurrency: "unbounded", discard: true });
|
|
65
|
+
}),
|
|
66
|
+
});
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=channels.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channels.js","sourceRoot":"","sources":["../../src/g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,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,qBAAqB,EAAE,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,EAChJ,EAAE,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE,CAC5C,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC,CAAC,CACH,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/gateway/config.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAsBjC,MAAM,OAAO,aAAc,SAAQ,OAAO,CAAC,OAAO,EAAiC,CAAC,4BAA4B,CAAC;CAAG"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Data } from "effect";
|
|
2
|
+
// What can go wrong in the gateway core, as types. The messages are what the agent and the command line see.
|
|
3
|
+
/** A statement on the bot's SQLite file failed. */
|
|
4
|
+
export class LogError extends Data.TaggedError("LogError") {
|
|
5
|
+
static from(cause) {
|
|
6
|
+
return new LogError({ message: message(cause), cause });
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/** A channel adapter failed to do what it was asked: connect, translate, send, react, call. */
|
|
10
|
+
export class ChannelError extends Data.TaggedError("ChannelError") {
|
|
11
|
+
static from(channel, cause) {
|
|
12
|
+
return new ChannelError({ message: cause instanceof Error ? cause.message : String(cause), channel, cause });
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** No running channel made this address. */
|
|
16
|
+
export class NoChannelForAddress extends Data.TaggedError("NoChannelForAddress") {
|
|
17
|
+
static of(address) {
|
|
18
|
+
return new NoChannelForAddress({ message: `no channel for address ${JSON.stringify(address)}. Copy addresses from a wake message or the log.`, address });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** No running channel has this ID. */
|
|
22
|
+
export class UnknownChannel extends Data.TaggedError("UnknownChannel") {
|
|
23
|
+
}
|
|
24
|
+
/** A send that cannot go out as asked: empty text or send ID. */
|
|
25
|
+
export class BadSend extends Data.TaggedError("BadSend") {
|
|
26
|
+
}
|
|
27
|
+
/** An adapter turned a raw event into happenings that do not match the Happening schema. */
|
|
28
|
+
export class BadHappening extends Data.TaggedError("BadHappening") {
|
|
29
|
+
}
|
|
30
|
+
/** A timer at a time that is not a number. */
|
|
31
|
+
export class BadTime extends Data.TaggedError("BadTime") {
|
|
32
|
+
}
|
|
33
|
+
/** The bot is not ready to be woken yet. */
|
|
34
|
+
export class NoWakeTarget extends Data.TaggedError("NoWakeTarget") {
|
|
35
|
+
}
|
|
36
|
+
/** The judge did not answer, also after retries. */
|
|
37
|
+
export class JudgeError extends Data.TaggedError("JudgeError") {
|
|
38
|
+
}
|
|
39
|
+
/** Delivering a wake to the bot failed. */
|
|
40
|
+
export class DeliverError extends Data.TaggedError("DeliverError") {
|
|
41
|
+
}
|
|
42
|
+
/** The message of a thrown value. */
|
|
43
|
+
export function message(cause) {
|
|
44
|
+
return cause instanceof Error ? cause.message : String(cause);
|
|
45
|
+
}
|
|
46
|
+
/** A failure as the log shows it: the thrown value, as String() gives it ("Error: ..."), or the failure's message. */
|
|
47
|
+
export function failureText(failure) {
|
|
48
|
+
if (failure && typeof failure === "object" && "_tag" in failure && "message" in failure) {
|
|
49
|
+
const { cause, message: text } = failure;
|
|
50
|
+
return cause instanceof Error ? String(cause) : text;
|
|
51
|
+
}
|
|
52
|
+
return String(failure);
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/gateway/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAE9B,6GAA6G;AAE7G,mDAA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MAAM,OAAO,YAAa,SAAQ,IAAI,CAAC,WAAW,CAAC,cAAc,CAA+B;CAAG;AAEnG,oDAAoD;AACpD,MAAM,OAAO,UAAW,SAAQ,IAAI,CAAC,WAAW,CAAC,YAAY,CAAwD;CAAG;AAExH,2CAA2C;AAC3C,MAAM,OAAO,YAAa,SAAQ,IAAI,CAAC,WAAW,CAAC,cAAc,CAAwD;CAAG;AAE5H,qCAAqC;AACrC,MAAM,UAAU,OAAO,CAAC,KAAc;IACpC,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAChE,CAAC;AAED,sHAAsH;AACtH,MAAM,UAAU,WAAW,CAAC,OAAgB;IAC1C,IAAI,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,MAAM,IAAI,OAAO,IAAI,SAAS,IAAI,OAAO,EAAE,CAAC;QACxF,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,OAA+C,CAAC;QACjF,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACvD,CAAC;IACD,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC;AACzB,CAAC"}
|
package/dist/gateway/gateway.js
CHANGED
|
@@ -1,175 +1,239 @@
|
|
|
1
|
+
import { Clock, Context, Effect, FiberHandle, FiberSet, Layer, ManagedRuntime, Schedule } from "effect";
|
|
2
|
+
import { Channels } from "./channels.js";
|
|
3
|
+
import { GatewayConfig } from "./config.js";
|
|
4
|
+
import { ChannelError } from "./errors.js";
|
|
5
|
+
import { Inbox } from "./inbox.js";
|
|
6
|
+
import { MessageLog } from "./log.js";
|
|
1
7
|
import { Outbox } from "./outbox.js";
|
|
8
|
+
import { Timers } from "./timers.js";
|
|
9
|
+
import { Telemetry } from "./telemetry.js";
|
|
10
|
+
import { tracingLayer, WakeSpans } from "./tracing.js";
|
|
2
11
|
import { Waker } from "./waker.js";
|
|
3
12
|
const HEARTBEAT_MS = 60_000;
|
|
13
|
+
/** The gateway core of one bot as Effect services: log, channels, inbox, waker, outbox, timers. */
|
|
14
|
+
export function gatewayLayer(sql, options) {
|
|
15
|
+
const config = Layer.succeed(GatewayConfig, options);
|
|
16
|
+
const tracing = tracingLayer(sql, options.bot).pipe(Layer.provide(config));
|
|
17
|
+
const base = Layer.mergeAll(MessageLog.layer(sql), WakeSpans.layer(sql), Channels.layer, Telemetry.layer).pipe(Layer.provideMerge(config), Layer.provideMerge(tracing));
|
|
18
|
+
const waker = Waker.layer.pipe(Layer.provideMerge(base));
|
|
19
|
+
const inbox = Inbox.layer.pipe(Layer.provideMerge(waker));
|
|
20
|
+
const core = Layer.mergeAll(Outbox.layer, Timers.layer(sql)).pipe(Layer.provideMerge(inbox));
|
|
21
|
+
return Background.layer.pipe(Layer.provideMerge(core));
|
|
22
|
+
}
|
|
23
|
+
/** Tasks of the gateway that run in the background, and effects started by adapters' callbacks. Both stop with it. */
|
|
24
|
+
class Background extends Context.Service()("pots/gateway/Background") {
|
|
25
|
+
static layer = Layer.effect(Background, Effect.gen(function* () {
|
|
26
|
+
return Background.of({ heartbeat: yield* FiberHandle.make(), run: yield* FiberSet.makeRuntimePromise() });
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
4
29
|
/**
|
|
5
|
-
* The gateway core of one bot, the same for every platform: inbox, message log, waker and
|
|
6
|
-
*
|
|
30
|
+
* The gateway core of one bot, the same for every platform: inbox, message log, waker, outbox and timers. Channel
|
|
31
|
+
* adapters only translate; everything they receive goes through here. The core runs on Effect; this class is its
|
|
32
|
+
* face for the bot, in promises.
|
|
7
33
|
*/
|
|
8
34
|
export class Gateway {
|
|
9
|
-
|
|
10
|
-
#
|
|
35
|
+
#runtime;
|
|
36
|
+
#channels;
|
|
11
37
|
#outbox;
|
|
12
|
-
#
|
|
38
|
+
#waker;
|
|
39
|
+
#wakeSpans;
|
|
40
|
+
#background;
|
|
13
41
|
#print;
|
|
14
|
-
/** Inbox events are applied one at a time, in arrival order, so an edit never overtakes its message. */
|
|
15
|
-
#queue = Promise.resolve();
|
|
16
42
|
#lastAlive;
|
|
17
|
-
#heartbeat;
|
|
18
43
|
#started = false;
|
|
19
|
-
|
|
20
|
-
|
|
44
|
+
#stopping;
|
|
45
|
+
timers;
|
|
46
|
+
/** pi's telemetry on the gateway's tracer, for the agent: its spans join the gateway's traces. */
|
|
47
|
+
telemetry;
|
|
48
|
+
constructor(runtime, services, print) {
|
|
49
|
+
this.#runtime = runtime;
|
|
50
|
+
this.#channels = services.channels;
|
|
51
|
+
this.#outbox = services.outbox;
|
|
52
|
+
this.#waker = services.waker;
|
|
53
|
+
this.telemetry = services.telemetry;
|
|
54
|
+
this.#wakeSpans = services.wakeSpans;
|
|
55
|
+
this.#background = services.background;
|
|
21
56
|
this.#print = print;
|
|
22
|
-
this
|
|
23
|
-
|
|
57
|
+
this.timers = new TimersHandle((effect) => this.#run(effect));
|
|
58
|
+
}
|
|
59
|
+
/** Open the gateway's tables on the bot's connection. */
|
|
60
|
+
static async open(sql, options) {
|
|
61
|
+
const runtime = ManagedRuntime.make(gatewayLayer(sql, options));
|
|
62
|
+
try {
|
|
63
|
+
const services = await runtime.runPromise(Effect.gen(function* () {
|
|
64
|
+
return {
|
|
65
|
+
channels: yield* Channels,
|
|
66
|
+
outbox: yield* Outbox,
|
|
67
|
+
waker: yield* Waker,
|
|
68
|
+
wakeSpans: yield* WakeSpans,
|
|
69
|
+
telemetry: yield* Telemetry,
|
|
70
|
+
background: yield* Background,
|
|
71
|
+
};
|
|
72
|
+
}));
|
|
73
|
+
return new Gateway(runtime, services, options.print);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
await runtime.dispose();
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
24
79
|
}
|
|
25
80
|
get outbox() {
|
|
26
|
-
return this.#outbox;
|
|
81
|
+
return this.#outbox.hooks;
|
|
82
|
+
}
|
|
83
|
+
get waker() {
|
|
84
|
+
return this.#waker.hooks;
|
|
27
85
|
}
|
|
28
86
|
get channels() {
|
|
29
|
-
return
|
|
87
|
+
return this.#channels.list();
|
|
30
88
|
}
|
|
31
89
|
/**
|
|
32
90
|
* Start with the given adapters: finish what the last process left, connect, then backfill what was missed.
|
|
33
91
|
* Undecided events from before go to the waker first, so nothing decided in this run is offered twice.
|
|
34
92
|
*/
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
93
|
+
start(target, adapters) {
|
|
94
|
+
const self = this;
|
|
95
|
+
return this.#runtime.runPromise(Effect.gen(function* () {
|
|
96
|
+
const log = yield* MessageLog;
|
|
97
|
+
const waker = yield* Waker;
|
|
98
|
+
const inbox = yield* Inbox;
|
|
99
|
+
waker.setTarget(target);
|
|
100
|
+
const lastAlive = yield* log.getState("last_alive");
|
|
101
|
+
self.#lastAlive = lastAlive ? Number(lastAlive) : undefined;
|
|
102
|
+
yield* waker.redeliver;
|
|
103
|
+
for (const event of yield* log.undecided)
|
|
104
|
+
yield* waker.add(event);
|
|
105
|
+
yield* inbox.replay;
|
|
106
|
+
const connected = yield* self.#channels.connect(adapters, (adapter) => self.#sink(adapter));
|
|
107
|
+
yield* inbox.open;
|
|
108
|
+
self.#started = true;
|
|
109
|
+
yield* self.#beat;
|
|
110
|
+
yield* FiberHandle.run(self.#background.heartbeat, Effect.repeat(self.#beat, Schedule.spaced(HEARTBEAT_MS)));
|
|
111
|
+
yield* inbox.drain;
|
|
112
|
+
for (const [index, adapter] of adapters.entries())
|
|
113
|
+
yield* self.#channels.follow(adapter, connected[index], (each) => self.#sink(each));
|
|
114
|
+
}).pipe(Effect.withSpan("gateway.start")));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Wake the bot no more: stop the timers and the waker. A decision in progress is finished and delivered first.
|
|
118
|
+
* Undecided events stay in the log for the next start. Everything else, the bot's actions too, keeps working.
|
|
119
|
+
*/
|
|
120
|
+
pauseWakes() {
|
|
121
|
+
return this.#run(Effect.gen(function* () {
|
|
122
|
+
yield* (yield* Timers).stop;
|
|
123
|
+
yield* (yield* Waker).stop;
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
/** Stop everything. Calls after this fail with "the gateway is stopped". */
|
|
127
|
+
stop() {
|
|
128
|
+
this.#stopping ??= this.#stop();
|
|
129
|
+
return this.#stopping;
|
|
130
|
+
}
|
|
131
|
+
async #stop() {
|
|
132
|
+
const self = this;
|
|
133
|
+
try {
|
|
134
|
+
await this.#runtime.runPromise(Effect.gen(function* () {
|
|
135
|
+
yield* FiberHandle.clear(self.#background.heartbeat);
|
|
136
|
+
yield* (yield* Timers).stop;
|
|
137
|
+
yield* self.#channels.stop;
|
|
138
|
+
yield* (yield* Inbox).close;
|
|
139
|
+
yield* (yield* Waker).stop;
|
|
140
|
+
if (self.#started)
|
|
141
|
+
yield* self.#beat;
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
await this.#runtime.dispose();
|
|
146
|
+
}
|
|
67
147
|
}
|
|
68
148
|
/** Add a channel account while running. */
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
void this.#backfill(adapter);
|
|
75
|
-
else
|
|
76
|
-
void this.#keepConnecting(adapter);
|
|
77
|
-
}
|
|
78
|
-
async removeChannel(id) {
|
|
79
|
-
const adapter = this.#adapters.get(id);
|
|
80
|
-
if (!adapter)
|
|
81
|
-
return;
|
|
82
|
-
this.#adapters.delete(id);
|
|
83
|
-
await adapter.stop();
|
|
149
|
+
addChannel(adapter) {
|
|
150
|
+
return this.#run(this.#channels.add(adapter, (each) => this.#sink(each)));
|
|
151
|
+
}
|
|
152
|
+
removeChannel(id) {
|
|
153
|
+
return this.#run(this.#channels.remove(id));
|
|
84
154
|
}
|
|
85
155
|
adapterFor(address) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
throw new Error(`no channel for address ${JSON.stringify(address)}. Copy addresses from a wake message or the log.`);
|
|
156
|
+
if (this.#stopping)
|
|
157
|
+
throw stopped();
|
|
158
|
+
return this.#runtime.runSync(this.#channels.adapterFor(address));
|
|
90
159
|
}
|
|
91
160
|
channel(id) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return adapter;
|
|
161
|
+
if (this.#stopping)
|
|
162
|
+
throw stopped();
|
|
163
|
+
return this.#runtime.runSync(this.#channels.get(id));
|
|
96
164
|
}
|
|
97
165
|
// ---- Actions ----
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
166
|
+
/**
|
|
167
|
+
* wakes: the agent's submission IDs this send answers. trace: the agent's tool call, when the send comes from one.
|
|
168
|
+
* The send's span is in the tool call's trace, or else in the first wake's, and links to the wakes.
|
|
169
|
+
*/
|
|
170
|
+
send(sendId, to, text, wakes = [], trace) {
|
|
171
|
+
const parent = this.telemetry.spanOf(trace);
|
|
172
|
+
return this.#run(Effect.flatMap(this.#wakeSpans.find(wakes), (spans) => this.#outbox.send(sendId, to, text, spans, parent)));
|
|
173
|
+
}
|
|
174
|
+
react(message, emoji, remove, trace) {
|
|
175
|
+
const parent = this.telemetry.spanOf(trace);
|
|
176
|
+
return this.#run(Effect.gen({ self: this }, function* () {
|
|
177
|
+
const adapter = yield* this.#channels.adapterFor(message);
|
|
178
|
+
yield* Effect.tryPromise({ try: () => adapter.react(message, emoji, remove), catch: (cause) => ChannelError.from(adapter.id, cause) });
|
|
179
|
+
}).pipe(Effect.withSpan("react", { attributes: { message, emoji, remove }, ...(parent ? { parent } : {}) })));
|
|
180
|
+
}
|
|
181
|
+
call(channel, method, params, trace) {
|
|
182
|
+
const parent = this.telemetry.spanOf(trace);
|
|
183
|
+
return this.#run(Effect.gen({ self: this }, function* () {
|
|
184
|
+
const adapter = yield* this.#channels.get(channel);
|
|
185
|
+
return yield* Effect.tryPromise({ try: () => adapter.call(method, params), catch: (cause) => ChannelError.from(adapter.id, cause) });
|
|
186
|
+
}).pipe(Effect.withSpan("call", { attributes: { channel, method }, ...(parent ? { parent } : {}) })));
|
|
187
|
+
}
|
|
188
|
+
/** Run an effect for the bot. Failures reach it as Error objects, also when the gateway stops in the middle. */
|
|
189
|
+
async #run(effect) {
|
|
190
|
+
if (this.#stopping)
|
|
191
|
+
throw stopped();
|
|
192
|
+
try {
|
|
193
|
+
return await this.#runtime.runPromise(effect);
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
throw error instanceof Error ? error : this.#stopping ? stopped() : new Error(String(error));
|
|
197
|
+
}
|
|
106
198
|
}
|
|
107
199
|
// ---- Receive ----
|
|
108
200
|
#sink(adapter) {
|
|
109
201
|
return {
|
|
110
|
-
receive: (eventId, raw) => this.#receive(adapter.id, eventId, raw),
|
|
111
|
-
newestMessage: (place) => this.log.newestMessage(place),
|
|
112
|
-
threads: (place) => this.log.threads(place),
|
|
202
|
+
receive: (eventId, raw) => this.#background.run(Inbox.use((inbox) => inbox.receive(adapter.id, eventId, raw))),
|
|
203
|
+
newestMessage: (place) => this.#background.run(MessageLog.use((log) => log.newestMessage(place))),
|
|
204
|
+
threads: (place) => this.#background.run(MessageLog.use((log) => log.threads(place))),
|
|
113
205
|
lastAlive: () => this.#lastAlive,
|
|
114
206
|
log: (message, data) => this.#print(`${adapter.id}: ${message}`, data),
|
|
115
207
|
};
|
|
116
208
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
await adapter.start(this.#sink(adapter));
|
|
120
|
-
this.#print("channel connected", { channel: adapter.id, me: adapter.me?.address });
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
catch (error) {
|
|
124
|
-
this.#print("channel connect failed", { channel: adapter.id, error: String(error) });
|
|
125
|
-
await adapter.stop().catch(() => { });
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
209
|
+
get #beat() {
|
|
210
|
+
return MessageLog.use((log) => Effect.flatMap(Clock.currentTimeMillis, (now) => log.setState("last_alive", String(now)))).pipe(Effect.catch((error) => Effect.sync(() => this.#print("heartbeat failed", { error: error.message }))));
|
|
128
211
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
await this.#backfill(adapter);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
212
|
+
}
|
|
213
|
+
const stopped = () => new Error("the gateway is stopped");
|
|
214
|
+
/** The bot's timers, in promises. */
|
|
215
|
+
export class TimersHandle {
|
|
216
|
+
#run;
|
|
217
|
+
constructor(run) {
|
|
218
|
+
this.#run = run;
|
|
140
219
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
220
|
+
start() {
|
|
221
|
+
return this.#run(Timers.use((timers) => timers.start));
|
|
222
|
+
}
|
|
223
|
+
stop() {
|
|
224
|
+
return this.#run(Timers.use((timers) => timers.stop));
|
|
225
|
+
}
|
|
226
|
+
add(dueAt, note) {
|
|
227
|
+
return this.#run(Timers.use((timers) => timers.add(dueAt, note)));
|
|
228
|
+
}
|
|
229
|
+
cancel(id) {
|
|
230
|
+
return this.#run(Timers.use((timers) => timers.cancel(id)));
|
|
231
|
+
}
|
|
232
|
+
resetFallback() {
|
|
233
|
+
return this.#run(Timers.use((timers) => timers.resetFallback));
|
|
148
234
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (!(await this.log.addInbox(channel, eventId, raw)))
|
|
152
|
-
return;
|
|
153
|
-
this.#enqueue(channel, eventId, raw);
|
|
154
|
-
}
|
|
155
|
-
#enqueue(channel, eventId, raw) {
|
|
156
|
-
this.#queue = this.#queue.then(async () => {
|
|
157
|
-
try {
|
|
158
|
-
const adapter = this.#adapters.get(channel);
|
|
159
|
-
if (!adapter)
|
|
160
|
-
throw new Error(`channel ${channel} is not running`);
|
|
161
|
-
const happenings = await adapter.translate(raw);
|
|
162
|
-
for (const event of await this.log.apply(channel, eventId, happenings))
|
|
163
|
-
this.#waker.add(event);
|
|
164
|
-
}
|
|
165
|
-
catch (error) {
|
|
166
|
-
this.#print("event failed", { channel, eventId, error: String(error) });
|
|
167
|
-
await this.log.failInbox(channel, eventId, String(error)).catch(() => { });
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
async #beat() {
|
|
172
|
-
await this.log.setState("last_alive", String(Date.now())).catch((error) => this.#print("heartbeat failed", { error: String(error) }));
|
|
235
|
+
pending() {
|
|
236
|
+
return this.#run(Timers.use((timers) => timers.pending));
|
|
173
237
|
}
|
|
174
238
|
}
|
|
175
239
|
//# sourceMappingURL=gateway.js.map
|