pi-durable-bot 0.1.0 → 0.2.1
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 +22 -13
- 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 +41 -6
- package/dist/agent/models.js.map +1 -1
- package/dist/bot/bot.js +67 -38
- package/dist/bot/bot.js.map +1 -1
- package/dist/bot/definition.js +65 -23
- package/dist/bot/definition.js.map +1 -1
- package/dist/bot/machine.js +14 -6
- package/dist/bot/machine.js.map +1 -1
- package/dist/bot/prompt.js +16 -15
- package/dist/bot/prompt.js.map +1 -1
- package/dist/channels/slack/index.js +1 -3
- package/dist/channels/slack/index.js.map +1 -1
- package/dist/cli/ai.js +1 -1
- package/dist/cli/ai.js.map +1 -1
- package/dist/cli/chat.js +2 -2
- package/dist/cli/chat.js.map +1 -1
- package/dist/cli/commands.js +76 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/init.js +6 -6
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/pots.js +6 -72
- package/dist/cli/pots.js.map +1 -1
- package/dist/cli/service.js +1 -2
- package/dist/cli/service.js.map +1 -1
- package/dist/cli/trace.js +193 -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/home.js +16 -0
- package/dist/core/home.js.map +1 -1
- package/dist/core/judge.js +0 -1
- package/dist/core/judge.js.map +1 -1
- package/dist/gateway/channels.js +71 -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 +193 -135
- 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 +93 -107
- package/dist/gateway/log.js.map +1 -1
- package/dist/gateway/outbox.js +58 -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 +117 -0
- package/dist/gateway/timers.js.map +1 -0
- package/dist/gateway/tracing.js +190 -0
- package/dist/gateway/tracing.js.map +1 -0
- package/dist/gateway/waker.js +186 -137
- package/dist/gateway/waker.js.map +1 -1
- package/dist/main.js +2 -17
- package/dist/main.js.map +1 -1
- package/docs/channels/slack.md +1 -1
- package/package.json +8 -2
- package/dist/bot/timers.js +0 -113
- package/dist/bot/timers.js.map +0 -1
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Cause, Context, Deferred, Effect, FiberHandle, Layer, Option, Queue, Schema } from "effect";
|
|
2
|
+
import { Happening } from "../core/channel.js";
|
|
3
|
+
import { Channels, platform } from "./channels.js";
|
|
4
|
+
import { GatewayConfig } from "./config.js";
|
|
5
|
+
import { BadHappening, ChannelError, failureText } from "./errors.js";
|
|
6
|
+
import { MessageLog } from "./log.js";
|
|
7
|
+
import { Waker } from "./waker.js";
|
|
8
|
+
const decodeHappenings = Schema.decodeUnknownEffect(Schema.Array(Happening));
|
|
9
|
+
/** Adapters may say null for a missing value (a person without a name); the schema says absent. The raw data stays as it is. */
|
|
10
|
+
function withoutNulls(value) {
|
|
11
|
+
if (Array.isArray(value))
|
|
12
|
+
return value.map(withoutNulls);
|
|
13
|
+
if (!value || typeof value !== "object")
|
|
14
|
+
return value;
|
|
15
|
+
return Object.fromEntries(Object.entries(value)
|
|
16
|
+
.filter(([, item]) => item !== null)
|
|
17
|
+
.map(([key, item]) => [key, key === "raw" ? item : withoutNulls(item)]));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Every event from every channel comes in here. It is stored before the adapter acknowledges it, then applied to the
|
|
21
|
+
* log one at a time, in arrival order, so an edit never overtakes its message. A known event ID is a repeat and is
|
|
22
|
+
* dropped. Events that a crash left unapplied are applied on the next start.
|
|
23
|
+
*/
|
|
24
|
+
export class Inbox extends Context.Service()("pots/gateway/Inbox") {
|
|
25
|
+
static layer = Layer.effect(Inbox, Effect.gen(function* () {
|
|
26
|
+
const log = yield* MessageLog;
|
|
27
|
+
const channels = yield* Channels;
|
|
28
|
+
const waker = yield* Waker;
|
|
29
|
+
const { print } = yield* GatewayConfig;
|
|
30
|
+
const queue = yield* Queue.unbounded();
|
|
31
|
+
const worker = yield* FiberHandle.make();
|
|
32
|
+
let opened = false;
|
|
33
|
+
const apply = (channel, eventId, raw, received) => Effect.gen(function* () {
|
|
34
|
+
const adapter = channels.running(channel);
|
|
35
|
+
if (!adapter)
|
|
36
|
+
return yield* Effect.fail(ChannelError.from(channel, `channel ${channel} is not running`));
|
|
37
|
+
const translated = yield* platform(channel, () => adapter.translate(raw));
|
|
38
|
+
const happenings = yield* decodeHappenings(withoutNulls(translated)).pipe(Effect.mapError((error) => new BadHappening({ message: `${channel} gave a happening that does not fit: ${error.message}`, channel })));
|
|
39
|
+
const events = yield* log.apply(channel, eventId, happenings);
|
|
40
|
+
yield* Effect.annotateCurrentSpan({ happenings: happenings.length, events: events.map((event) => event.id).join(",") });
|
|
41
|
+
for (const event of events)
|
|
42
|
+
yield* waker.add(event);
|
|
43
|
+
}).pipe(Effect.catchCause((cause) => {
|
|
44
|
+
if (Cause.hasInterruptsOnly(cause))
|
|
45
|
+
return Effect.void;
|
|
46
|
+
const error = failureText(Cause.squash(cause));
|
|
47
|
+
return Effect.sync(() => print("event failed", { channel, eventId, error })).pipe(Effect.andThen(log.failInbox(channel, eventId, error)), Effect.ignore);
|
|
48
|
+
}),
|
|
49
|
+
// Its own trace with the receive, not the trace of whatever started the worker.
|
|
50
|
+
Effect.withSpan("inbox.apply", { attributes: { channel, event: eventId }, ...(received ? { parent: received } : { root: true }) }));
|
|
51
|
+
// One item at a time. An item that has started is finished before the worker stops.
|
|
52
|
+
const work = Effect.forever(Effect.flatMap(Queue.take(queue), (item) => item._tag === "mark" ? Effect.asVoid(Deferred.succeed(item.done, undefined)) : Effect.uninterruptible(apply(item.channel, item.eventId, item.raw, item.span))));
|
|
53
|
+
const drain = Effect.gen(function* () {
|
|
54
|
+
const done = yield* Deferred.make();
|
|
55
|
+
yield* Queue.offer(queue, { _tag: "mark", done });
|
|
56
|
+
if (opened)
|
|
57
|
+
yield* Deferred.await(done);
|
|
58
|
+
});
|
|
59
|
+
return Inbox.of({
|
|
60
|
+
receive: (channel, eventId, raw) => log.addInbox(channel, eventId, raw).pipe(Effect.tap((added) => added
|
|
61
|
+
? Effect.flatMap(Effect.option(Effect.currentSpan), (span) => Queue.offer(queue, { _tag: "event", channel, eventId, raw, ...(Option.isSome(span) ? { span: span.value } : {}) }))
|
|
62
|
+
: Effect.void), Effect.tap((added) => Effect.annotateCurrentSpan({ repeat: !added })), Effect.asVoid, Effect.withSpan("inbox.receive", { attributes: { channel, event: eventId } })),
|
|
63
|
+
replay: Effect.gen(function* () {
|
|
64
|
+
for (const row of yield* log.unfinishedInbox)
|
|
65
|
+
yield* Queue.offer(queue, { _tag: "event", channel: row.channel, eventId: row.event_id, raw: JSON.parse(row.raw) });
|
|
66
|
+
}),
|
|
67
|
+
open: Effect.suspend(() => {
|
|
68
|
+
opened = true;
|
|
69
|
+
return FiberHandle.run(worker, work, { onlyIfMissing: true });
|
|
70
|
+
}).pipe(Effect.asVoid),
|
|
71
|
+
drain,
|
|
72
|
+
close: Effect.gen(function* () {
|
|
73
|
+
yield* drain;
|
|
74
|
+
yield* FiberHandle.clear(worker);
|
|
75
|
+
opened = false;
|
|
76
|
+
}),
|
|
77
|
+
});
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=inbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inbox.js","sourceRoot":"","sources":["../../src/gateway/inbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,K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C,CAAC;YAChE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;YACtB,KAAK;YACL,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;gBACzB,KAAK,CAAC,CAAC,KAAK,CAAC;gBACb,KAAK,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;gBACjC,MAAM,GAAG,KAAK,CAAC;YACjB,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC,CAAC,CACH,CAAC;CACH"}
|
package/dist/gateway/log.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Clock, Context, Effect, Layer } from "effect";
|
|
2
|
+
import { LogError } from "./errors.js";
|
|
1
3
|
/**
|
|
2
4
|
* The gateway's tables: the inbox, the message log and the outbox. They share the bot's SQLite file with
|
|
3
5
|
* pi-durable (tables `pi_`) and the bot (tables `bot_`). The agent reads them with read-only SQL, so the
|
|
@@ -81,6 +83,12 @@ CREATE TABLE IF NOT EXISTS gw_outbox (
|
|
|
81
83
|
sent_at INTEGER
|
|
82
84
|
);
|
|
83
85
|
|
|
86
|
+
CREATE TABLE IF NOT EXISTS gw_pending_wakes (
|
|
87
|
+
id TEXT PRIMARY KEY,
|
|
88
|
+
wake TEXT NOT NULL,
|
|
89
|
+
decided_at INTEGER NOT NULL
|
|
90
|
+
);
|
|
91
|
+
|
|
84
92
|
CREATE TABLE IF NOT EXISTS gw_state (
|
|
85
93
|
key TEXT PRIMARY KEY,
|
|
86
94
|
value TEXT NOT NULL
|
|
@@ -99,106 +107,79 @@ export const LOG_SCHEMA = `Times are Unix ms. Columns that hold an address (plac
|
|
|
99
107
|
- gw_people(address, channel, name, real_name)
|
|
100
108
|
- gw_outbox(send_id, to_address, text, state, message, created_at, sent_at): what you sent with send.
|
|
101
109
|
- gw_inbox(channel, event_id, received_at, raw, state, error): the raw events as received.
|
|
110
|
+
- gw_spans(span_id, trace_id, parent_id, name, start_at, end_at, status, error, attributes, links): what the gateway did, the last 7 days. One trace per wake: name wake (attributes reason, place, events, submission), its sends (name send, attributes send, to, result). pots trace shows them.
|
|
102
111
|
Text search: LIKE, for example text LIKE '%invoice%'.`;
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
async addInbox(channel, eventId, raw) {
|
|
115
|
-
return this.sql.transaction(async (tx) => {
|
|
116
|
-
if (await tx.get("SELECT 1 AS x FROM gw_inbox WHERE channel = ? AND event_id = ?", channel, eventId))
|
|
117
|
-
return false;
|
|
118
|
-
await tx.run("INSERT INTO gw_inbox (channel, event_id, received_at, raw) VALUES (?, ?, ?, ?)", channel, eventId, Date.now(), JSON.stringify(raw));
|
|
119
|
-
return true;
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
async unfinishedInbox() {
|
|
123
|
-
return this.sql.all("SELECT channel, event_id, raw FROM gw_inbox WHERE state != 'done' ORDER BY received_at, rowid");
|
|
124
|
-
}
|
|
125
|
-
async failInbox(channel, eventId, error) {
|
|
126
|
-
await this.sql.run("UPDATE gw_inbox SET state = 'error', error = ? WHERE channel = ? AND event_id = ?", error, channel, eventId);
|
|
112
|
+
/**
|
|
113
|
+
* The gateway's tables, as effects. The statements run on the connection the bot shares with pi-durable; times come
|
|
114
|
+
* from the Clock, so tests control them.
|
|
115
|
+
*/
|
|
116
|
+
export class MessageLog extends Context.Service()("pots/gateway/MessageLog") {
|
|
117
|
+
/** Create the tables when they are missing. */
|
|
118
|
+
static layer(sql) {
|
|
119
|
+
return Layer.effect(MessageLog, Effect.gen(function* () {
|
|
120
|
+
yield* db(() => sql.exec(SCHEMA));
|
|
121
|
+
return MessageLog.of(makeLog(sql));
|
|
122
|
+
}));
|
|
127
123
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
124
|
+
}
|
|
125
|
+
/** A statement on the shared connection. */
|
|
126
|
+
export const db = (run) => Effect.tryPromise({ try: run, catch: LogError.from });
|
|
127
|
+
/** A statement that needs the current time. */
|
|
128
|
+
const dbAt = (run) => Effect.flatMap(Clock.currentTimeMillis, (now) => db(() => run(now)));
|
|
129
|
+
function makeLog(sql) {
|
|
130
|
+
return {
|
|
131
|
+
addInbox: (channel, eventId, raw) => dbAt(async (now) => (await sql.get("INSERT INTO gw_inbox (channel, event_id, received_at, raw) VALUES (?, ?, ?, ?) ON CONFLICT DO NOTHING RETURNING 1 AS added", channel, eventId, now, JSON.stringify(raw))) !== undefined),
|
|
132
|
+
unfinishedInbox: db(() => sql.all("SELECT channel, event_id, raw FROM gw_inbox WHERE state != 'done' ORDER BY received_at, rowid")),
|
|
133
|
+
failInbox: (channel, eventId, error) => db(() => sql.run("UPDATE gw_inbox SET state = 'error', error = ? WHERE channel = ? AND event_id = ?", error, channel, eventId)),
|
|
134
|
+
apply: (channel, eventId, happenings) => dbAt(async (now) => {
|
|
135
|
+
const ids = await sql.transaction(async (tx) => {
|
|
136
|
+
const ids = [];
|
|
137
|
+
for (const happening of happenings) {
|
|
138
|
+
const id = await applyOne(tx, channel, eventId, happening, now);
|
|
139
|
+
if (id !== undefined)
|
|
140
|
+
ids.push(id);
|
|
141
|
+
}
|
|
142
|
+
await tx.run("UPDATE gw_inbox SET state = 'done', error = NULL WHERE channel = ? AND event_id = ?", channel, eventId);
|
|
143
|
+
return ids;
|
|
144
|
+
});
|
|
145
|
+
const undecided = [];
|
|
146
|
+
for (const id of ids) {
|
|
147
|
+
const row = await sql.get(`${UNDECIDED_SELECT} WHERE e.id = ? AND e.wake IS NULL`, id);
|
|
148
|
+
if (row)
|
|
149
|
+
undecided.push(row);
|
|
139
150
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
/** Messages in a place before an event, oldest first. */
|
|
170
|
-
async messagesBefore(place, at, limit) {
|
|
171
|
-
const rows = await this.sql.all(`SELECT m.address, m.place, m.thread, m.person, p.name AS person_name, m.from_me, m.text, m.at, m.edited_at, m.deleted_at
|
|
172
|
-
FROM gw_messages m LEFT JOIN gw_people p ON p.address = m.person
|
|
173
|
-
WHERE m.place = ? AND m.at < ? ORDER BY m.at DESC LIMIT ?`, place, at, limit);
|
|
174
|
-
return rows.reverse();
|
|
175
|
-
}
|
|
176
|
-
async message(address) {
|
|
177
|
-
return this.sql.get(`SELECT m.address, m.place, m.thread, m.person, p.name AS person_name, m.from_me, m.text, m.at, m.edited_at, m.deleted_at
|
|
178
|
-
FROM gw_messages m LEFT JOIN gw_people p ON p.address = m.person WHERE m.address = ?`, address);
|
|
179
|
-
}
|
|
180
|
-
async place(address) {
|
|
181
|
-
const row = await this.sql.get("SELECT address, name, kind FROM gw_places WHERE address = ?", address);
|
|
182
|
-
return row && { address: row.address, kind: row.kind, ...(row.name ? { name: row.name } : {}) };
|
|
183
|
-
}
|
|
184
|
-
// ---- Outbox ----
|
|
185
|
-
async outbox(sendId) {
|
|
186
|
-
return this.sql.get("SELECT send_id, to_address, state, message, created_at FROM gw_outbox WHERE send_id = ?", sendId);
|
|
187
|
-
}
|
|
188
|
-
async outboxStart(sendId, to, text) {
|
|
189
|
-
await this.sql.run("INSERT INTO gw_outbox (send_id, to_address, text, state, created_at) VALUES (?, ?, ?, 'sending', ?)", sendId, to, text, Date.now());
|
|
190
|
-
}
|
|
191
|
-
async outboxDone(sendId, message) {
|
|
192
|
-
await this.sql.run("UPDATE gw_outbox SET state = 'sent', message = ?, sent_at = ? WHERE send_id = ?", message, Date.now(), sendId);
|
|
193
|
-
}
|
|
194
|
-
// ---- State ----
|
|
195
|
-
async getState(key) {
|
|
196
|
-
return (await this.sql.get("SELECT value FROM gw_state WHERE key = ?", key))?.value;
|
|
197
|
-
}
|
|
198
|
-
async setState(key, value) {
|
|
199
|
-
await this.sql.run("INSERT INTO gw_state (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value", key, value);
|
|
200
|
-
}
|
|
151
|
+
return undecided;
|
|
152
|
+
}),
|
|
153
|
+
undecided: db(() => sql.all(`${UNDECIDED_SELECT} WHERE e.wake IS NULL ORDER BY e.id`)),
|
|
154
|
+
setWake: (ids, wake) => db(() => decide(sql, ids, wake)),
|
|
155
|
+
recordWake: (wake, ids, decision) => dbAt((now) => sql.transaction(async (tx) => {
|
|
156
|
+
await tx.run("INSERT OR IGNORE INTO gw_pending_wakes (id, wake, decided_at) VALUES (?, ?, ?)", wake.id, JSON.stringify(wake), now);
|
|
157
|
+
await decide(tx, ids, decision);
|
|
158
|
+
})),
|
|
159
|
+
wakeDelivered: (id) => db(() => sql.run("DELETE FROM gw_pending_wakes WHERE id = ?", id)),
|
|
160
|
+
pendingWakes: db(async () => (await sql.all("SELECT wake FROM gw_pending_wakes ORDER BY decided_at, rowid")).map((row) => JSON.parse(row.wake))),
|
|
161
|
+
newestMessage: (place) => db(async () => (await sql.get("SELECT address FROM gw_messages WHERE place = ? ORDER BY at DESC LIMIT 1", place))?.address),
|
|
162
|
+
threads: (place) => db(async () => {
|
|
163
|
+
const rows = await sql.all("SELECT thread FROM gw_messages WHERE place = ? AND thread IS NOT NULL GROUP BY thread ORDER BY max(at) DESC", place);
|
|
164
|
+
return rows.map((row) => row.thread);
|
|
165
|
+
}),
|
|
166
|
+
messagesBefore: (place, at, limit) => db(async () => {
|
|
167
|
+
const rows = await sql.all(`SELECT m.address, m.place, m.thread, m.person, p.name AS person_name, m.from_me, m.text, m.at, m.edited_at, m.deleted_at
|
|
168
|
+
FROM gw_messages m LEFT JOIN gw_people p ON p.address = m.person
|
|
169
|
+
WHERE m.place = ? AND m.at < ? ORDER BY m.at DESC LIMIT ?`, place, at, limit);
|
|
170
|
+
return rows.reverse();
|
|
171
|
+
}),
|
|
172
|
+
message: (address) => db(() => sql.get(`SELECT m.address, m.place, m.thread, m.person, p.name AS person_name, m.from_me, m.text, m.at, m.edited_at, m.deleted_at
|
|
173
|
+
FROM gw_messages m LEFT JOIN gw_people p ON p.address = m.person WHERE m.address = ?`, address)),
|
|
174
|
+
outbox: (sendId) => db(() => sql.get("SELECT send_id, to_address, state, message, created_at FROM gw_outbox WHERE send_id = ?", sendId)),
|
|
175
|
+
outboxStart: (sendId, to, text) => dbAt((now) => sql.run("INSERT INTO gw_outbox (send_id, to_address, text, state, created_at) VALUES (?, ?, ?, 'sending', ?)", sendId, to, text, now)),
|
|
176
|
+
outboxDone: (sendId, message) => dbAt((now) => sql.run("UPDATE gw_outbox SET state = 'sent', message = ?, sent_at = ? WHERE send_id = ?", message, now, sendId)),
|
|
177
|
+
getState: (key) => db(async () => (await sql.get("SELECT value FROM gw_state WHERE key = ?", key))?.value),
|
|
178
|
+
setState: (key, value) => db(() => sql.run("INSERT INTO gw_state (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value", key, value)),
|
|
179
|
+
};
|
|
201
180
|
}
|
|
181
|
+
/** Set the wake decision of these events, in one statement. */
|
|
182
|
+
const decide = (sql, ids, wake) => sql.run("UPDATE gw_events SET wake = ? WHERE id IN (SELECT value FROM json_each(?))", wake, JSON.stringify(ids));
|
|
202
183
|
const UNDECIDED_SELECT = `SELECT e.id, e.channel, e.kind, e.at, e.place, pl.name AS place_name, pl.kind AS place_kind, e.thread, e.message,
|
|
203
184
|
e.person, pe.name AS person_name, e.from_me, e.mentions_me, e.text
|
|
204
185
|
FROM gw_events e LEFT JOIN gw_places pl ON pl.address = e.place LEFT JOIN gw_people pe ON pe.address = e.person`;
|
|
@@ -206,8 +187,7 @@ const UNDECIDED_SELECT = `SELECT e.id, e.channel, e.kind, e.at, e.place, pl.name
|
|
|
206
187
|
* Apply one happening. Returns the new event row ID, or undefined when it adds nothing (a message already logged,
|
|
207
188
|
* for example the same message from a live event and from backfill).
|
|
208
189
|
*/
|
|
209
|
-
async function applyOne(tx, channel, eventId, h) {
|
|
210
|
-
const now = Date.now();
|
|
190
|
+
async function applyOne(tx, channel, eventId, h, now) {
|
|
211
191
|
if (h.place)
|
|
212
192
|
await putPlace(tx, channel, h.place, now);
|
|
213
193
|
if (h.person)
|
|
@@ -221,8 +201,7 @@ async function applyOne(tx, channel, eventId, h) {
|
|
|
221
201
|
await tx.run("UPDATE gw_messages SET thread = coalesce(thread, ?), send_id = coalesce(send_id, ?), person = coalesce(person, ?) WHERE address = ?", thread, h.sendId ?? null, h.person?.address ?? null, h.message);
|
|
222
202
|
return undefined;
|
|
223
203
|
}
|
|
224
|
-
await tx
|
|
225
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, h.message, channel, h.place?.address ?? null, thread, h.person?.address ?? null, h.fromMe ? 1 : 0, h.mentionsMe ? 1 : 0, h.text ?? "", h.mentions?.length ? JSON.stringify(h.mentions) : null, h.files?.length ? JSON.stringify(h.files) : null, h.at, h.sendId ?? null, raw);
|
|
204
|
+
await insertMessage(tx, channel, h.message, h, { thread, raw, editedAt: null, sendId: h.sendId ?? null });
|
|
226
205
|
// A reply makes its root a thread root.
|
|
227
206
|
if (thread && h.threadRoot && h.threadRoot !== h.message) {
|
|
228
207
|
await tx.run("UPDATE gw_messages SET thread = ? WHERE address = ? AND thread IS NULL", thread, h.threadRoot);
|
|
@@ -233,26 +212,33 @@ async function applyOne(tx, channel, eventId, h) {
|
|
|
233
212
|
thread ??= known?.thread ?? null;
|
|
234
213
|
if (h.kind === "edit") {
|
|
235
214
|
if (known) {
|
|
236
|
-
await tx.run("UPDATE gw_messages SET text = ?, mentions_me = ?, mentions = ?, files = coalesce(?, files), edited_at = ?, raw = ? WHERE address = ?", h.text ?? "", h.mentionsMe ? 1 : 0,
|
|
215
|
+
await tx.run("UPDATE gw_messages SET text = ?, mentions_me = ?, mentions = ?, files = coalesce(?, files), edited_at = ?, raw = ? WHERE address = ?", h.text ?? "", h.mentionsMe ? 1 : 0, jsonList(h.mentions), jsonList(h.files), h.at, raw, h.message);
|
|
237
216
|
}
|
|
238
217
|
else {
|
|
239
|
-
|
|
240
|
-
|
|
218
|
+
// An edit of a message the log does not have: the message as it is now.
|
|
219
|
+
await insertMessage(tx, channel, h.message, h, { thread, raw, editedAt: h.at, sendId: null });
|
|
241
220
|
}
|
|
242
221
|
}
|
|
243
222
|
else if (h.kind === "delete" && known) {
|
|
244
223
|
await tx.run("UPDATE gw_messages SET deleted_at = ? WHERE address = ?", h.at, h.message);
|
|
245
224
|
}
|
|
246
225
|
else if (h.kind === "update" && known && raw !== null) {
|
|
247
|
-
await tx.run("UPDATE gw_messages SET raw = ?, files = coalesce(?, files) WHERE address = ?", raw,
|
|
226
|
+
await tx.run("UPDATE gw_messages SET raw = ?, files = coalesce(?, files) WHERE address = ?", raw, jsonList(h.files), h.message);
|
|
248
227
|
}
|
|
249
228
|
}
|
|
250
229
|
// Own events never notify the bot. Deletes are only logged. Everything else waits for the waker's decision.
|
|
251
230
|
const wake = h.fromMe ? "self" : h.kind === "delete" ? "log" : null;
|
|
252
|
-
await tx.
|
|
253
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, channel, eventId, h.kind, h.at, h.place?.address ?? null, thread, h.message ?? null, h.person?.address ?? null, h.fromMe ? 1 : 0, h.mentionsMe ? 1 : 0, h.text ?? null, raw, wake);
|
|
254
|
-
return
|
|
231
|
+
const event = await tx.get(`INSERT INTO gw_events (channel, inbox_event, kind, at, place, thread, message, person, from_me, mentions_me, text, raw, wake)
|
|
232
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id`, channel, eventId, h.kind, h.at, h.place?.address ?? null, thread, h.message ?? null, h.person?.address ?? null, h.fromMe ? 1 : 0, h.mentionsMe ? 1 : 0, h.text ?? null, raw, wake);
|
|
233
|
+
return event.id;
|
|
234
|
+
}
|
|
235
|
+
/** A message the log does not have yet. */
|
|
236
|
+
function insertMessage(tx, channel, address, h, row) {
|
|
237
|
+
return tx.run(`INSERT INTO gw_messages (address, channel, place, thread, person, from_me, mentions_me, text, mentions, files, at, edited_at, send_id, raw)
|
|
238
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, address, channel, h.place?.address ?? null, row.thread, h.person?.address ?? null, h.fromMe ? 1 : 0, h.mentionsMe ? 1 : 0, h.text ?? "", jsonList(h.mentions), jsonList(h.files), h.at, row.editedAt, row.sendId, row.raw);
|
|
255
239
|
}
|
|
240
|
+
/** A list as JSON, or NULL when it is empty. */
|
|
241
|
+
const jsonList = (items) => (items?.length ? JSON.stringify(items) : null);
|
|
256
242
|
async function putPlace(tx, channel, place, now) {
|
|
257
243
|
await tx.run(`INSERT INTO gw_places (address, channel, name, kind, updated_at) VALUES (?, ?, ?, ?, ?)
|
|
258
244
|
ON CONFLICT (address) DO UPDATE SET name = coalesce(excluded.name, name), kind = excluded.kind, updated_at = excluded.updated_at`, place.address, channel, place.name ?? null, place.kind, now);
|
package/dist/gateway/log.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.js","sourceRoot":"","sources":["../../src/gateway/log.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"log.js","sourceRoot":"","sources":["../../src/gateway/log.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM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CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAE3G,KAAK,UAAU,QAAQ,CAAC,EAAe,EAAE,OAAe,EAAE,KAAgB,EAAE,GAAW;IACrF,MAAM,EAAE,CAAC,GAAG,CACV;sIACkI,EAClI,KAAK,CAAC,OAAO,EACb,OAAO,EACP,KAAK,CAAC,IAAI,IAAI,IAAI,EAClB,KAAK,CAAC,IAAI,EACV,GAAG,CACJ,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,SAAS,CAAC,EAAe,EAAE,OAAe,EAAE,MAAkB,EAAE,GAAW;IACxF,MAAM,EAAE,CAAC,GAAG,CACV;qKACiK,EACjK,MAAM,CAAC,OAAO,EACd,OAAO,EACP,MAAM,CAAC,IAAI,IAAI,IAAI,EACnB,MAAM,CAAC,QAAQ,IAAI,IAAI,EACvB,GAAG,CACJ,CAAC;AACJ,CAAC"}
|
package/dist/gateway/outbox.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import { Context, Effect, Layer } from "effect";
|
|
2
|
+
import { Channels, platform } from "./channels.js";
|
|
3
|
+
import { GatewayConfig } from "./config.js";
|
|
4
|
+
import { BadSend } from "./errors.js";
|
|
5
|
+
import { Inbox } from "./inbox.js";
|
|
6
|
+
import { MessageLog } from "./log.js";
|
|
1
7
|
/**
|
|
2
8
|
* Sends once per send ID, across crashes:
|
|
3
9
|
* - the outbox says sent → return that message;
|
|
@@ -6,54 +12,61 @@
|
|
|
6
12
|
* - otherwise record "sending", send, record "sent".
|
|
7
13
|
* A sent message goes into the inbox as an event, so the log gets it the same way as every other message.
|
|
8
14
|
*/
|
|
9
|
-
export class Outbox {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (!sendId.trim())
|
|
26
|
-
throw new Error("send ID is empty");
|
|
27
|
-
const record = await this.#log.outbox(sendId);
|
|
28
|
-
if (record?.state === "sent")
|
|
29
|
-
return { message: record.message, ...placeOf(this.#adapterFor, record.message), repeated: true };
|
|
30
|
-
const adapter = this.#adapterFor(record?.to_address ?? to);
|
|
31
|
-
if (record) {
|
|
32
|
-
const found = await adapter.findSent(record.to_address, sendId, record.created_at);
|
|
33
|
-
if (found) {
|
|
34
|
-
await this.#receive(adapter.id, `sent:${sendId}`, found.raw);
|
|
35
|
-
await this.#log.outboxDone(sendId, found.message);
|
|
36
|
-
this.#print("send recovered", { sendId });
|
|
37
|
-
return { message: found.message, place: found.place, recovered: true };
|
|
15
|
+
export class Outbox extends Context.Service()("pots/gateway/Outbox") {
|
|
16
|
+
static layer = Layer.effect(Outbox, Effect.gen(function* () {
|
|
17
|
+
const log = yield* MessageLog;
|
|
18
|
+
const channels = yield* Channels;
|
|
19
|
+
const inbox = yield* Inbox;
|
|
20
|
+
const { print } = yield* GatewayConfig;
|
|
21
|
+
const hooks = {};
|
|
22
|
+
const send = (sendId, to, text) => Effect.gen(function* () {
|
|
23
|
+
if (!text.trim())
|
|
24
|
+
return yield* Effect.fail(new BadSend({ message: "text is empty" }));
|
|
25
|
+
if (!sendId.trim())
|
|
26
|
+
return yield* Effect.fail(new BadSend({ message: "send ID is empty" }));
|
|
27
|
+
const record = yield* log.outbox(sendId);
|
|
28
|
+
if (record?.state === "sent") {
|
|
29
|
+
yield* Effect.annotateCurrentSpan({ result: "repeated", message: record.message });
|
|
30
|
+
return { message: record.message, ...placeOf(channels.list(), record.message), repeated: true };
|
|
38
31
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
32
|
+
const adapter = yield* channels.adapterFor(record?.to_address ?? to);
|
|
33
|
+
if (record) {
|
|
34
|
+
const found = yield* platform(adapter.id, () => adapter.findSent(record.to_address, sendId, record.created_at)).pipe(Effect.withSpan("channel.findSent", { attributes: { channel: adapter.id } }));
|
|
35
|
+
if (found) {
|
|
36
|
+
yield* inbox.receive(adapter.id, `sent:${sendId}`, found.raw);
|
|
37
|
+
yield* log.outboxDone(sendId, found.message);
|
|
38
|
+
print("send recovered", { sendId });
|
|
39
|
+
yield* Effect.annotateCurrentSpan({ result: "recovered", message: found.message });
|
|
40
|
+
return { message: found.message, place: found.place, recovered: true };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
yield* log.outboxStart(sendId, to, text);
|
|
45
|
+
}
|
|
46
|
+
const sent = yield* platform(adapter.id, () => adapter.send(record?.to_address ?? to, text, sendId)).pipe(Effect.withSpan("channel.send", { attributes: { channel: adapter.id } }));
|
|
47
|
+
hooks.afterPlatformSend?.(sendId);
|
|
48
|
+
yield* inbox.receive(adapter.id, `sent:${sendId}`, sent.raw);
|
|
49
|
+
yield* log.outboxDone(sendId, sent.message);
|
|
50
|
+
yield* Effect.annotateCurrentSpan({ result: "sent", message: sent.message });
|
|
51
|
+
return { message: sent.message, place: sent.place };
|
|
52
|
+
});
|
|
53
|
+
return Outbox.of({
|
|
54
|
+
hooks,
|
|
55
|
+
send: (sendId, to, text, wakes = [], parent) => {
|
|
56
|
+
const [first, ...rest] = parent ? [parent, ...wakes] : wakes;
|
|
57
|
+
return send(sendId, to, text).pipe(Effect.withSpan("send", {
|
|
58
|
+
attributes: { send: sendId, to },
|
|
59
|
+
...(first ? { parent: first } : {}),
|
|
60
|
+
links: rest.map((span) => ({ span, attributes: {} })),
|
|
61
|
+
}));
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}));
|
|
53
65
|
}
|
|
54
|
-
function placeOf(
|
|
66
|
+
function placeOf(adapters, message) {
|
|
67
|
+
const adapter = adapters.find((candidate) => candidate.owns(message));
|
|
55
68
|
try {
|
|
56
|
-
const place =
|
|
69
|
+
const place = adapter?.placeOf(message);
|
|
57
70
|
return place ? { place } : {};
|
|
58
71
|
}
|
|
59
72
|
catch {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"outbox.js","sourceRoot":"","sources":["../../src/gateway/outbox.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"outbox.js","sourceRoot":"","sources":["../../src/gateway/outbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAe,MAAM,QAAQ,CAAC;AAE7D,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,OAAO,EAA8D,MAAM,aAAa,CAAC;AAClG,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAmBtC;;;;;;;GAOG;AACH,MAAM,OAAO,MAAO,SAAQ,OAAO,CAAC,OAAO,EAUxC,CAAC,qBAAqB,CAAC;IACxB,MAAM,CAAU,KAAK,GAAG,KAAK,CAAC,MAAM,CAClC,MAAM,EACN,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;QAClB,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,UAAU,CAAC;QAC9B,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,QAAQ,CAAC;QACjC,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC;QAC3B,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,CAAC,aAAa,CAAC;QACvC,MAAM,KAAK,GAAgB,EAAE,CAAC;QAE9B,MAAM,IAAI,GAAG,CAAC,MAAc,EAAE,EAAW,EAAE,IAAY,EAAE,EAAE,CACzD,MAAM,CAAC,GAAG,C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