@alexsun-top/opencode-chat-room 0.8.2 → 0.8.4
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/CHANGELOG.md +12 -0
- package/README.md +18 -62
- package/README.zh-CN.md +25 -69
- package/dist/cli.js +466 -188
- package/package.json +11 -5
- package/packages/chat-room-channeld/README.md +25 -3
- package/packages/chat-room-channeld/dist/mock-adapter.js +5 -5
- package/packages/chat-room-channeld/src/adapter.ts +5 -0
- package/packages/chat-room-channeld/src/cli.ts +72 -5
- package/packages/chat-room-channeld/src/host.ts +17 -2
- package/packages/chat-room-channeld/src/pg/database.ts +187 -0
- package/packages/chat-room-channeld/src/pg/migrate.ts +150 -0
- package/packages/chat-room-channeld/src/pg/schema.ts +60 -0
- package/packages/chat-room-channeld/src/pg/sql.mjs +175 -0
- package/packages/chat-room-channeld/src/pg/sqlite.ts +100 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +133 -0
- package/packages/chat-room-channeld/src/protocol.ts +1 -0
- package/packages/chat-room-channeld/src/state.ts +73 -97
- package/packages/chat-room-channeld/src/stats.ts +140 -109
- package/server/assets/base.css +3 -1
- package/server/assets/chat/admin-badge.js +32 -30
- package/server/assets/chat/admin.js +201 -161
- package/server/assets/chat/announcements.js +302 -208
- package/server/assets/chat/attachment-store.js +116 -76
- package/server/assets/chat/attachments.js +701 -441
- package/server/assets/chat/chat-background.js +179 -156
- package/server/assets/chat/clipboard.js +45 -25
- package/server/assets/chat/confirm-dialog.js +38 -35
- package/server/assets/chat/copy-action.js +40 -41
- package/server/assets/chat/create-room-dialog.js +50 -47
- package/server/assets/chat/diagram-viewer.js +293 -236
- package/server/assets/chat/doc-preview.js +516 -443
- package/server/assets/chat/editor.js +446 -336
- package/server/assets/chat/events.js +650 -510
- package/server/assets/chat/files.js +505 -431
- package/server/assets/chat/history-loader.js +81 -73
- package/server/assets/chat/icons.js +9 -11
- package/server/assets/chat/image-zoom.js +246 -179
- package/server/assets/chat/index.js +2 -3
- package/server/assets/chat/install-prompt.js +90 -69
- package/server/assets/chat/integration-account-schema.js +91 -87
- package/server/assets/chat/integration-editor.js +162 -155
- package/server/assets/chat/integration-values.js +116 -101
- package/server/assets/chat/integrations.js +750 -626
- package/server/assets/chat/join-requests.js +415 -358
- package/server/assets/chat/locales/de.js +193 -194
- package/server/assets/chat/locales/es.js +193 -194
- package/server/assets/chat/locales/fr.js +193 -194
- package/server/assets/chat/locales/index.js +1 -2
- package/server/assets/chat/locales/it.js +193 -194
- package/server/assets/chat/locales/ja.js +193 -193
- package/server/assets/chat/locales/ko.js +193 -193
- package/server/assets/chat/markdown-footnotes.js +134 -103
- package/server/assets/chat/markdown.js +557 -404
- package/server/assets/chat/members.js +259 -197
- package/server/assets/chat/mentions.js +181 -133
- package/server/assets/chat/message-cache.js +40 -33
- package/server/assets/chat/message-polling.js +299 -227
- package/server/assets/chat/message-window.js +78 -55
- package/server/assets/chat/messages.js +562 -480
- package/server/assets/chat/office-preview.js +46 -45
- package/server/assets/chat/palette.js +464 -417
- package/server/assets/chat/panels.js +94 -79
- package/server/assets/chat/pwa.js +147 -75
- package/server/assets/chat/questions.js +374 -302
- package/server/assets/chat/recall.js +338 -273
- package/server/assets/chat/receipts.js +151 -121
- package/server/assets/chat/reminders.js +345 -214
- package/server/assets/chat/replies.js +181 -130
- package/server/assets/chat/resource-format.js +59 -62
- package/server/assets/chat/room-list.js +58 -55
- package/server/assets/chat/room-watcher.js +76 -50
- package/server/assets/chat/rooms.js +631 -500
- package/server/assets/chat/search.js +138 -114
- package/server/assets/chat/settings.js +225 -201
- package/server/assets/chat/shiki.js +62 -63
- package/server/assets/chat/shortcuts.js +212 -189
- package/server/assets/chat/side-pane.js +161 -150
- package/server/assets/chat/state.js +860 -737
- package/server/assets/chat/system-events.js +46 -44
- package/server/assets/chat/system-messages.js +57 -56
- package/server/assets/chat/task-form.js +179 -183
- package/server/assets/chat/task-row.js +158 -155
- package/server/assets/chat/tasks.js +665 -587
- package/server/assets/chat/viewport.js +32 -29
- package/server/assets/chat/window-controls.js +32 -32
- package/server/assets/conversation.css +10 -4
- package/server/chat-server.ts +9 -1
- package/server/chat.html +3 -1
- package/server/routes/pages.ts +7 -5
- package/server/sw.js +346 -268
- package/src/web/integration-rules-editor.ts +2 -0
package/dist/cli.js
CHANGED
|
@@ -9030,10 +9030,11 @@ function chatVersionTag(here) {
|
|
|
9030
9030
|
}
|
|
9031
9031
|
async function pageRoutes(req, url, here, channeld) {
|
|
9032
9032
|
const p = url.pathname;
|
|
9033
|
-
|
|
9033
|
+
const read = req.method === "GET" || req.method === "HEAD";
|
|
9034
|
+
if (read && p === "/") {
|
|
9034
9035
|
return Response.redirect(new URL("/chat", url).toString(), 302);
|
|
9035
9036
|
}
|
|
9036
|
-
if (
|
|
9037
|
+
if (read && p === "/chat") {
|
|
9037
9038
|
const html = await fs6.readFile(path16.join(here, "chat.html"), "utf8").catch(() => null);
|
|
9038
9039
|
if (html === null) {
|
|
9039
9040
|
return json12({ error: "chat page not found" }, 404);
|
|
@@ -9046,7 +9047,7 @@ async function pageRoutes(req, url, here, channeld) {
|
|
|
9046
9047
|
}
|
|
9047
9048
|
});
|
|
9048
9049
|
}
|
|
9049
|
-
if (
|
|
9050
|
+
if (read && p === "/manifest.webmanifest") {
|
|
9050
9051
|
const body = await fs6.readFile(path16.join(here, "manifest.webmanifest"), "utf8").catch(() => null);
|
|
9051
9052
|
if (body === null)
|
|
9052
9053
|
return json12({ error: "manifest not found" }, 404);
|
|
@@ -9054,7 +9055,7 @@ async function pageRoutes(req, url, here, channeld) {
|
|
|
9054
9055
|
headers: { "Content-Type": "application/manifest+json; charset=utf-8", "Cache-Control": "no-store" }
|
|
9055
9056
|
});
|
|
9056
9057
|
}
|
|
9057
|
-
if (
|
|
9058
|
+
if (read && p === "/sw.js") {
|
|
9058
9059
|
const body = await fs6.readFile(path16.join(here, "sw.js"), "utf8").catch(() => null);
|
|
9059
9060
|
if (body === null)
|
|
9060
9061
|
return json12({ error: "service worker not found" }, 404);
|
|
@@ -9067,7 +9068,7 @@ async function pageRoutes(req, url, here, channeld) {
|
|
|
9067
9068
|
}
|
|
9068
9069
|
});
|
|
9069
9070
|
}
|
|
9070
|
-
if (
|
|
9071
|
+
if (read && p.startsWith("/assets/")) {
|
|
9071
9072
|
let relative;
|
|
9072
9073
|
try {
|
|
9073
9074
|
relative = decodeURIComponent(p.slice("/assets/".length));
|
|
@@ -9837,6 +9838,12 @@ function readCapabilities(result, accountId) {
|
|
|
9837
9838
|
if (schema || env)
|
|
9838
9839
|
capabilities.account = { ...schema ? { schema } : {}, ...env ? { env } : {} };
|
|
9839
9840
|
}
|
|
9841
|
+
const send = entry.send;
|
|
9842
|
+
if (send && typeof send === "object") {
|
|
9843
|
+
const value = send.idempotency;
|
|
9844
|
+
if (typeof value === "string" && value.trim())
|
|
9845
|
+
capabilities.send = { idempotency: value.trim() };
|
|
9846
|
+
}
|
|
9840
9847
|
return Object.keys(capabilities).length ? capabilities : undefined;
|
|
9841
9848
|
}
|
|
9842
9849
|
|
|
@@ -11074,6 +11081,290 @@ var init_rules = __esm(() => {
|
|
|
11074
11081
|
init_config();
|
|
11075
11082
|
});
|
|
11076
11083
|
|
|
11084
|
+
// packages/chat-room-channeld/src/pg/database.ts
|
|
11085
|
+
import { existsSync as existsSync7 } from "node:fs";
|
|
11086
|
+
import { MessageChannel as MessageChannel2, receiveMessageOnPort as receiveMessageOnPort2, Worker as Worker2 } from "node:worker_threads";
|
|
11087
|
+
import { fileURLToPath as fileURLToPath5 } from "node:url";
|
|
11088
|
+
function workerURL2() {
|
|
11089
|
+
const candidates = [
|
|
11090
|
+
new URL("./worker.mjs", import.meta.url),
|
|
11091
|
+
new URL("../packages/chat-room-channeld/src/pg/worker.mjs", import.meta.url),
|
|
11092
|
+
new URL("../packages/chat-room-channeld/dist/pg/worker.mjs", import.meta.url),
|
|
11093
|
+
new URL("../../src/pg/worker.mjs", import.meta.url)
|
|
11094
|
+
];
|
|
11095
|
+
for (const candidate of candidates)
|
|
11096
|
+
if (existsSync7(fileURLToPath5(candidate)))
|
|
11097
|
+
return candidate;
|
|
11098
|
+
return candidates[0];
|
|
11099
|
+
}
|
|
11100
|
+
function databaseError2(error) {
|
|
11101
|
+
const result = new Error(String(error?.message ?? "PostgreSQL query failed"));
|
|
11102
|
+
Object.assign(result, error);
|
|
11103
|
+
return result;
|
|
11104
|
+
}
|
|
11105
|
+
|
|
11106
|
+
class PgDatabase2 {
|
|
11107
|
+
#worker;
|
|
11108
|
+
#port;
|
|
11109
|
+
#timeout;
|
|
11110
|
+
#lockTimeout;
|
|
11111
|
+
#nextID = 1;
|
|
11112
|
+
#depth = 0;
|
|
11113
|
+
#savepoints = 0;
|
|
11114
|
+
#broken;
|
|
11115
|
+
#closed = false;
|
|
11116
|
+
constructor(connectionString, options = {}) {
|
|
11117
|
+
if (!connectionString)
|
|
11118
|
+
throw new Error("PostgreSQL connection string is empty");
|
|
11119
|
+
this.#timeout = options.timeout ?? 5000;
|
|
11120
|
+
this.#lockTimeout = Math.max(1, this.#timeout - 1000);
|
|
11121
|
+
const ready = new SharedArrayBuffer(4);
|
|
11122
|
+
const { port1, port2 } = new MessageChannel2;
|
|
11123
|
+
this.#port = port1;
|
|
11124
|
+
this.#worker = new Worker2(workerURL2());
|
|
11125
|
+
this.#worker.unref();
|
|
11126
|
+
this.#port.unref();
|
|
11127
|
+
try {
|
|
11128
|
+
this.#worker.postMessage({
|
|
11129
|
+
type: "init",
|
|
11130
|
+
connectionString,
|
|
11131
|
+
ready,
|
|
11132
|
+
statementTimeout: Math.max(1, this.#timeout - 500),
|
|
11133
|
+
port: port2
|
|
11134
|
+
}, [port2]);
|
|
11135
|
+
} catch (error) {
|
|
11136
|
+
this.#worker.terminate();
|
|
11137
|
+
this.#port.close();
|
|
11138
|
+
throw error;
|
|
11139
|
+
}
|
|
11140
|
+
const status = Atomics.wait(new Int32Array(ready), 0, 0, this.#timeout);
|
|
11141
|
+
if (status === "timed-out") {
|
|
11142
|
+
this.#worker.terminate();
|
|
11143
|
+
throw new Error(`PostgreSQL bridge did not become ready within ${this.#timeout}ms`);
|
|
11144
|
+
}
|
|
11145
|
+
const message = receiveMessageOnPort2(this.#port)?.message;
|
|
11146
|
+
if (message?.type === "fatal") {
|
|
11147
|
+
this.#worker.terminate();
|
|
11148
|
+
throw databaseError2(message.error);
|
|
11149
|
+
}
|
|
11150
|
+
}
|
|
11151
|
+
query(sql) {
|
|
11152
|
+
return {
|
|
11153
|
+
all: (...params) => this.#request("all", sql, params).rows,
|
|
11154
|
+
get: (...params) => this.#request("get", sql, params).row,
|
|
11155
|
+
run: (...params) => {
|
|
11156
|
+
const result = this.#request("run", sql, params);
|
|
11157
|
+
return { changes: result.changes ?? 0, lastInsertRowid: result.lastInsertRowid ?? 0 };
|
|
11158
|
+
}
|
|
11159
|
+
};
|
|
11160
|
+
}
|
|
11161
|
+
prepare(sql) {
|
|
11162
|
+
return this.query(sql);
|
|
11163
|
+
}
|
|
11164
|
+
exec(sql) {
|
|
11165
|
+
this.#request("exec", sql, []);
|
|
11166
|
+
}
|
|
11167
|
+
transaction(fn) {
|
|
11168
|
+
const run = (immediate) => this.#run(fn, immediate);
|
|
11169
|
+
return Object.assign(() => run(false), {
|
|
11170
|
+
deferred: () => run(false),
|
|
11171
|
+
immediate: () => run(true)
|
|
11172
|
+
});
|
|
11173
|
+
}
|
|
11174
|
+
close() {
|
|
11175
|
+
if (this.#closed)
|
|
11176
|
+
return;
|
|
11177
|
+
this.#closed = true;
|
|
11178
|
+
const signal = new SharedArrayBuffer(4);
|
|
11179
|
+
try {
|
|
11180
|
+
this.#port.postMessage({ type: "close", signal });
|
|
11181
|
+
Atomics.wait(new Int32Array(signal), 0, 0, 2000);
|
|
11182
|
+
} catch {}
|
|
11183
|
+
this.#port.close();
|
|
11184
|
+
this.#worker.terminate();
|
|
11185
|
+
}
|
|
11186
|
+
#run(fn, immediate) {
|
|
11187
|
+
const nested = this.#depth > 0;
|
|
11188
|
+
const savepoint = `chat_room_savepoint_${this.#savepoints++}`;
|
|
11189
|
+
if (nested) {
|
|
11190
|
+
this.exec(`SAVEPOINT ${savepoint}`);
|
|
11191
|
+
} else {
|
|
11192
|
+
this.exec(immediate ? "BEGIN" : "BEGIN ISOLATION LEVEL REPEATABLE READ");
|
|
11193
|
+
if (immediate) {
|
|
11194
|
+
try {
|
|
11195
|
+
this.exec(`SET LOCAL lock_timeout = '${this.#lockTimeout}ms'; SELECT pg_advisory_xact_lock(hashtext('channeld-state')); SET LOCAL lock_timeout = 0`);
|
|
11196
|
+
} catch (error) {
|
|
11197
|
+
try {
|
|
11198
|
+
this.exec("ROLLBACK");
|
|
11199
|
+
} catch {}
|
|
11200
|
+
throw error;
|
|
11201
|
+
}
|
|
11202
|
+
}
|
|
11203
|
+
}
|
|
11204
|
+
this.#depth++;
|
|
11205
|
+
try {
|
|
11206
|
+
const result = fn();
|
|
11207
|
+
this.exec(nested ? `RELEASE ${savepoint}` : "COMMIT");
|
|
11208
|
+
return result;
|
|
11209
|
+
} catch (error) {
|
|
11210
|
+
try {
|
|
11211
|
+
this.exec(nested ? `ROLLBACK TO ${savepoint}; RELEASE ${savepoint}` : "ROLLBACK");
|
|
11212
|
+
} catch {}
|
|
11213
|
+
throw error;
|
|
11214
|
+
} finally {
|
|
11215
|
+
this.#depth--;
|
|
11216
|
+
}
|
|
11217
|
+
}
|
|
11218
|
+
#request(mode, sql, params) {
|
|
11219
|
+
if (this.#broken)
|
|
11220
|
+
throw this.#broken;
|
|
11221
|
+
if (this.#closed)
|
|
11222
|
+
throw new Error("PostgreSQL database is closed");
|
|
11223
|
+
const id = this.#nextID++;
|
|
11224
|
+
const signal = new SharedArrayBuffer(4);
|
|
11225
|
+
this.#port.postMessage({ id, mode, sql, params, signal });
|
|
11226
|
+
const status = Atomics.wait(new Int32Array(signal), 0, 0, this.#timeout);
|
|
11227
|
+
if (status === "timed-out") {
|
|
11228
|
+
this.#broken = Object.assign(new Error(`PostgreSQL bridge timed out after ${this.#timeout}ms; the database connection must be reopened`), { code: "PG_BRIDGE_TIMEOUT" });
|
|
11229
|
+
throw this.#broken;
|
|
11230
|
+
}
|
|
11231
|
+
const message = receiveMessageOnPort2(this.#port)?.message;
|
|
11232
|
+
if (!message || message.id !== id) {
|
|
11233
|
+
this.#broken = Object.assign(new Error("PostgreSQL bridge reply out of sequence"), { code: "PG_BRIDGE_SEQUENCE" });
|
|
11234
|
+
throw this.#broken;
|
|
11235
|
+
}
|
|
11236
|
+
if (!message.ok)
|
|
11237
|
+
throw databaseError2(message.error);
|
|
11238
|
+
return message;
|
|
11239
|
+
}
|
|
11240
|
+
}
|
|
11241
|
+
var init_database = () => {};
|
|
11242
|
+
|
|
11243
|
+
// packages/chat-room-channeld/src/pg/schema.ts
|
|
11244
|
+
function initializeChannelSchema(db, options = {}) {
|
|
11245
|
+
db.exec(SCHEMA);
|
|
11246
|
+
const columns = db.query("PRAGMA table_info(channel_echoes)").all();
|
|
11247
|
+
if (!columns.some((column) => column.name === "created_at")) {
|
|
11248
|
+
db.exec("ALTER TABLE channel_echoes ADD COLUMN created_at INTEGER NOT NULL DEFAULT 0");
|
|
11249
|
+
}
|
|
11250
|
+
if (options.backfillEchoOrder) {
|
|
11251
|
+
db.exec("UPDATE channel_echoes SET created_at = rowid WHERE created_at = 0");
|
|
11252
|
+
}
|
|
11253
|
+
}
|
|
11254
|
+
var SCHEMA = `
|
|
11255
|
+
CREATE TABLE IF NOT EXISTS channel_events (
|
|
11256
|
+
event_id TEXT PRIMARY KEY,
|
|
11257
|
+
dedup_key TEXT NOT NULL UNIQUE,
|
|
11258
|
+
payload TEXT NOT NULL,
|
|
11259
|
+
ingress_seq INTEGER,
|
|
11260
|
+
committed_at INTEGER NOT NULL
|
|
11261
|
+
);
|
|
11262
|
+
CREATE TABLE IF NOT EXISTS channel_deliveries (
|
|
11263
|
+
delivery_id TEXT PRIMARY KEY,
|
|
11264
|
+
event_id TEXT NOT NULL,
|
|
11265
|
+
rule_id TEXT NOT NULL,
|
|
11266
|
+
rule_version INTEGER NOT NULL,
|
|
11267
|
+
target TEXT NOT NULL,
|
|
11268
|
+
idempotency_key TEXT NOT NULL UNIQUE,
|
|
11269
|
+
payload TEXT NOT NULL,
|
|
11270
|
+
state TEXT NOT NULL,
|
|
11271
|
+
attempt_count INTEGER NOT NULL DEFAULT 0,
|
|
11272
|
+
next_attempt_at INTEGER,
|
|
11273
|
+
last_error TEXT,
|
|
11274
|
+
provider_message TEXT,
|
|
11275
|
+
created_at INTEGER NOT NULL,
|
|
11276
|
+
updated_at INTEGER NOT NULL
|
|
11277
|
+
);
|
|
11278
|
+
CREATE TABLE IF NOT EXISTS channel_receipts (
|
|
11279
|
+
receipt_id TEXT PRIMARY KEY,
|
|
11280
|
+
delivery_id TEXT NOT NULL,
|
|
11281
|
+
attempt_id TEXT NOT NULL,
|
|
11282
|
+
state TEXT NOT NULL,
|
|
11283
|
+
provider_message TEXT,
|
|
11284
|
+
detail TEXT,
|
|
11285
|
+
observed_at INTEGER NOT NULL
|
|
11286
|
+
);
|
|
11287
|
+
CREATE TABLE IF NOT EXISTS channel_echoes (
|
|
11288
|
+
endpoint TEXT NOT NULL,
|
|
11289
|
+
native_message_id TEXT NOT NULL,
|
|
11290
|
+
logical_message_id TEXT NOT NULL,
|
|
11291
|
+
delivery_id TEXT,
|
|
11292
|
+
created_at INTEGER NOT NULL DEFAULT 0,
|
|
11293
|
+
PRIMARY KEY (endpoint, native_message_id)
|
|
11294
|
+
);
|
|
11295
|
+
`;
|
|
11296
|
+
|
|
11297
|
+
// packages/chat-room-channeld/src/pg/sqlite.ts
|
|
11298
|
+
import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
|
|
11299
|
+
function bindings2(params) {
|
|
11300
|
+
let values;
|
|
11301
|
+
for (let index = 0;index < params.length; index++) {
|
|
11302
|
+
const value = params[index];
|
|
11303
|
+
if (value === undefined || typeof value === "boolean") {
|
|
11304
|
+
values ??= params.slice();
|
|
11305
|
+
values[index] = value === undefined ? null : value ? 1 : 0;
|
|
11306
|
+
}
|
|
11307
|
+
}
|
|
11308
|
+
return values ?? params;
|
|
11309
|
+
}
|
|
11310
|
+
|
|
11311
|
+
class Statement2 {
|
|
11312
|
+
#statement;
|
|
11313
|
+
constructor(statement) {
|
|
11314
|
+
this.#statement = statement;
|
|
11315
|
+
}
|
|
11316
|
+
all(...params) {
|
|
11317
|
+
return this.#statement.all(...bindings2(params));
|
|
11318
|
+
}
|
|
11319
|
+
get(...params) {
|
|
11320
|
+
return this.#statement.get(...bindings2(params)) ?? null;
|
|
11321
|
+
}
|
|
11322
|
+
run(...params) {
|
|
11323
|
+
const result = this.#statement.run(...bindings2(params));
|
|
11324
|
+
return { changes: Number(result.changes), lastInsertRowid: Number(result.lastInsertRowid) };
|
|
11325
|
+
}
|
|
11326
|
+
}
|
|
11327
|
+
|
|
11328
|
+
class Database2 {
|
|
11329
|
+
#db;
|
|
11330
|
+
#savepoints = 0;
|
|
11331
|
+
constructor(file, options = {}) {
|
|
11332
|
+
this.#db = options.readOnly ? new DatabaseSync2(file, { readOnly: true }) : new DatabaseSync2(file);
|
|
11333
|
+
}
|
|
11334
|
+
query(sql) {
|
|
11335
|
+
return new Statement2(this.#db.prepare(sql));
|
|
11336
|
+
}
|
|
11337
|
+
prepare(sql) {
|
|
11338
|
+
return this.query(sql);
|
|
11339
|
+
}
|
|
11340
|
+
exec(sql) {
|
|
11341
|
+
this.#db.exec(sql);
|
|
11342
|
+
}
|
|
11343
|
+
transaction(fn) {
|
|
11344
|
+
const run = (begin) => this.#run(begin, fn);
|
|
11345
|
+
return Object.assign(() => run("BEGIN"), { deferred: () => run("BEGIN"), immediate: () => run("BEGIN IMMEDIATE") });
|
|
11346
|
+
}
|
|
11347
|
+
close() {
|
|
11348
|
+
this.#db.close();
|
|
11349
|
+
}
|
|
11350
|
+
#run(begin, fn) {
|
|
11351
|
+
const nested = this.#db.isTransaction;
|
|
11352
|
+
const savepoint = `chat_room_savepoint_${this.#savepoints++}`;
|
|
11353
|
+
this.#db.exec(nested ? `SAVEPOINT ${savepoint}` : begin);
|
|
11354
|
+
try {
|
|
11355
|
+
const result = fn();
|
|
11356
|
+
this.#db.exec(nested ? `RELEASE ${savepoint}` : "COMMIT");
|
|
11357
|
+
return result;
|
|
11358
|
+
} catch (error) {
|
|
11359
|
+
try {
|
|
11360
|
+
this.#db.exec(nested ? `ROLLBACK TO ${savepoint}; RELEASE ${savepoint}` : "ROLLBACK");
|
|
11361
|
+
} catch {}
|
|
11362
|
+
throw error;
|
|
11363
|
+
}
|
|
11364
|
+
}
|
|
11365
|
+
}
|
|
11366
|
+
var init_sqlite2 = () => {};
|
|
11367
|
+
|
|
11077
11368
|
// packages/chat-room-channeld/src/published.ts
|
|
11078
11369
|
function buildPublishedResult(delivery, receipt, now) {
|
|
11079
11370
|
const occurredAt = new Date(now).toISOString();
|
|
@@ -11098,13 +11389,12 @@ var PUBLISHED_RESULT_PREFIX = "result.published:";
|
|
|
11098
11389
|
var init_published = () => {};
|
|
11099
11390
|
|
|
11100
11391
|
// packages/chat-room-channeld/src/stats.ts
|
|
11101
|
-
import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
|
|
11102
11392
|
function sanitizeAccountError(value) {
|
|
11103
11393
|
const redacted = value.replace(/\bBearer\s+\S+/gi, "Bearer [redacted]").replace(/\b(secret|token|password|passwd|authorization|api[_-]?key)\b\s*[:=]\s*\S+/gi, "$1=[redacted]");
|
|
11104
11394
|
return redacted.replace(/[\r\n\t]+/g, " ").replace(/\s+/g, " ").trim().slice(0, MAX_LAST_ERROR);
|
|
11105
11395
|
}
|
|
11106
|
-
function count(db, sql) {
|
|
11107
|
-
const row = db.
|
|
11396
|
+
function count(db, sql, ...params) {
|
|
11397
|
+
const row = db.query(sql).get(...params);
|
|
11108
11398
|
return Number(row.count);
|
|
11109
11399
|
}
|
|
11110
11400
|
function accountKey(target) {
|
|
@@ -11117,52 +11407,33 @@ function accountKey(target) {
|
|
|
11117
11407
|
function readChannelStats(path, now = Date.now(), runtime = new Map) {
|
|
11118
11408
|
let db;
|
|
11119
11409
|
try {
|
|
11120
|
-
db = new
|
|
11410
|
+
db = new Database2(path, { readOnly: true });
|
|
11121
11411
|
db.exec("PRAGMA busy_timeout = 2000");
|
|
11122
|
-
const names = new Set(db.
|
|
11412
|
+
const names = new Set(db.query("SELECT name FROM sqlite_master WHERE type = 'table'").all().map((row) => String(row.name)));
|
|
11123
11413
|
if (REQUIRED_TABLES.some((table) => !names.has(table))) {
|
|
11124
11414
|
throw new Error(`channel state not found: ${path}`);
|
|
11125
11415
|
}
|
|
11126
|
-
|
|
11127
|
-
|
|
11128
|
-
|
|
11129
|
-
|
|
11130
|
-
|
|
11131
|
-
|
|
11132
|
-
|
|
11133
|
-
|
|
11134
|
-
|
|
11135
|
-
|
|
11136
|
-
|
|
11137
|
-
|
|
11138
|
-
|
|
11139
|
-
|
|
11140
|
-
|
|
11141
|
-
|
|
11142
|
-
|
|
11143
|
-
|
|
11144
|
-
|
|
11145
|
-
|
|
11146
|
-
|
|
11147
|
-
if (state === "unknown")
|
|
11148
|
-
account.unknown += 1;
|
|
11149
|
-
if (state === "retryable")
|
|
11150
|
-
account.retryable += 1;
|
|
11151
|
-
if (state === "rate_limited")
|
|
11152
|
-
account.rateLimited += 1;
|
|
11153
|
-
if (QUEUE_STATES.has(state)) {
|
|
11154
|
-
const createdAt = Number(row.created_at);
|
|
11155
|
-
account.queueDepth += 1;
|
|
11156
|
-
if (account.oldestBacklogAt === null || createdAt < account.oldestBacklogAt) {
|
|
11157
|
-
account.oldestBacklogAt = createdAt;
|
|
11158
|
-
}
|
|
11159
|
-
if (oldestBacklogAt === null || createdAt < oldestBacklogAt) {
|
|
11160
|
-
oldestBacklogAt = createdAt;
|
|
11161
|
-
}
|
|
11162
|
-
}
|
|
11163
|
-
}
|
|
11164
|
-
for (const [key, live] of runtime) {
|
|
11165
|
-
const account = accounts.get(key) ?? {
|
|
11416
|
+
return buildStats(db, now, runtime);
|
|
11417
|
+
} catch {
|
|
11418
|
+
throw new Error(`channel state not found: ${path}`);
|
|
11419
|
+
} finally {
|
|
11420
|
+
try {
|
|
11421
|
+
db?.close();
|
|
11422
|
+
} catch {}
|
|
11423
|
+
}
|
|
11424
|
+
}
|
|
11425
|
+
function buildStats(db, now, runtime) {
|
|
11426
|
+
const stateCounts = {};
|
|
11427
|
+
const accounts = new Map;
|
|
11428
|
+
let oldestBacklogAt = null;
|
|
11429
|
+
const rows = db.query("SELECT target, state, created_at FROM channel_deliveries").all();
|
|
11430
|
+
for (const row of rows) {
|
|
11431
|
+
const state = String(row.state);
|
|
11432
|
+
stateCounts[state] = (stateCounts[state] ?? 0) + 1;
|
|
11433
|
+
const key = accountKey(String(row.target));
|
|
11434
|
+
let account = accounts.get(key);
|
|
11435
|
+
if (!account) {
|
|
11436
|
+
account = {
|
|
11166
11437
|
account: key,
|
|
11167
11438
|
state: "unknown",
|
|
11168
11439
|
queueDepth: 0,
|
|
@@ -11171,77 +11442,93 @@ function readChannelStats(path, now = Date.now(), runtime = new Map) {
|
|
|
11171
11442
|
retryable: 0,
|
|
11172
11443
|
rateLimited: 0
|
|
11173
11444
|
};
|
|
11174
|
-
account.state = live.state;
|
|
11175
|
-
const lastError = live.lastError ? sanitizeAccountError(live.lastError) : "";
|
|
11176
|
-
if (lastError)
|
|
11177
|
-
account.lastError = lastError;
|
|
11178
11445
|
accounts.set(key, account);
|
|
11179
11446
|
}
|
|
11180
|
-
|
|
11181
|
-
|
|
11182
|
-
|
|
11183
|
-
|
|
11184
|
-
|
|
11185
|
-
|
|
11186
|
-
|
|
11187
|
-
|
|
11188
|
-
|
|
11189
|
-
|
|
11190
|
-
|
|
11191
|
-
|
|
11192
|
-
|
|
11193
|
-
|
|
11194
|
-
|
|
11195
|
-
|
|
11196
|
-
|
|
11197
|
-
|
|
11198
|
-
|
|
11199
|
-
|
|
11200
|
-
|
|
11201
|
-
|
|
11202
|
-
|
|
11203
|
-
|
|
11204
|
-
|
|
11205
|
-
|
|
11206
|
-
queueDepth: pending + retryable + rateLimited,
|
|
11207
|
-
oldestBacklogAt,
|
|
11208
|
-
oldestBacklogAgeMs: oldestBacklogAt === null ? null : now - oldestBacklogAt,
|
|
11209
|
-
accounts: [...accounts.values()].sort((left, right) => left.account < right.account ? -1 : left.account > right.account ? 1 : 0)
|
|
11447
|
+
if (state === "unknown")
|
|
11448
|
+
account.unknown += 1;
|
|
11449
|
+
if (state === "retryable")
|
|
11450
|
+
account.retryable += 1;
|
|
11451
|
+
if (state === "rate_limited")
|
|
11452
|
+
account.rateLimited += 1;
|
|
11453
|
+
if (QUEUE_STATES.has(state)) {
|
|
11454
|
+
const createdAt = Number(row.created_at);
|
|
11455
|
+
account.queueDepth += 1;
|
|
11456
|
+
if (account.oldestBacklogAt === null || createdAt < account.oldestBacklogAt) {
|
|
11457
|
+
account.oldestBacklogAt = createdAt;
|
|
11458
|
+
}
|
|
11459
|
+
if (oldestBacklogAt === null || createdAt < oldestBacklogAt) {
|
|
11460
|
+
oldestBacklogAt = createdAt;
|
|
11461
|
+
}
|
|
11462
|
+
}
|
|
11463
|
+
}
|
|
11464
|
+
for (const [key, live] of runtime) {
|
|
11465
|
+
const account = accounts.get(key) ?? {
|
|
11466
|
+
account: key,
|
|
11467
|
+
state: "unknown",
|
|
11468
|
+
queueDepth: 0,
|
|
11469
|
+
oldestBacklogAt: null,
|
|
11470
|
+
unknown: 0,
|
|
11471
|
+
retryable: 0,
|
|
11472
|
+
rateLimited: 0
|
|
11210
11473
|
};
|
|
11211
|
-
|
|
11212
|
-
|
|
11213
|
-
|
|
11214
|
-
|
|
11215
|
-
|
|
11216
|
-
|
|
11217
|
-
|
|
11218
|
-
|
|
11219
|
-
|
|
11474
|
+
account.state = live.state;
|
|
11475
|
+
const lastError = live.lastError ? sanitizeAccountError(live.lastError) : "";
|
|
11476
|
+
if (lastError)
|
|
11477
|
+
account.lastError = lastError;
|
|
11478
|
+
accounts.set(key, account);
|
|
11479
|
+
}
|
|
11480
|
+
const pending = stateCounts.pending ?? 0;
|
|
11481
|
+
const retryable = stateCounts.retryable ?? 0;
|
|
11482
|
+
const rateLimited = stateCounts.rate_limited ?? 0;
|
|
11483
|
+
const failed = (stateCounts.permanent ?? 0) + (stateCounts.unsupported ?? 0) + (stateCounts.exhausted ?? 0);
|
|
11484
|
+
const publishedPattern = `${PUBLISHED_RESULT_PREFIX}%`;
|
|
11485
|
+
return {
|
|
11486
|
+
generatedAt: now,
|
|
11487
|
+
events: count(db, "SELECT COUNT(*) AS count FROM channel_events"),
|
|
11488
|
+
publishedResults: count(db, "SELECT COUNT(*) AS count FROM channel_events WHERE dedup_key LIKE ?", publishedPattern),
|
|
11489
|
+
eventsWithoutDeliveries: count(db, `SELECT COUNT(*) AS count FROM channel_events e
|
|
11490
|
+
WHERE e.dedup_key NOT LIKE ?
|
|
11491
|
+
AND NOT EXISTS (SELECT 1 FROM channel_deliveries d WHERE d.event_id = e.event_id)`, publishedPattern),
|
|
11492
|
+
deliveriesWithoutEvent: count(db, `SELECT COUNT(*) AS count FROM channel_deliveries d
|
|
11493
|
+
WHERE NOT EXISTS (SELECT 1 FROM channel_events e WHERE e.event_id = d.event_id)`),
|
|
11494
|
+
receiptsWithoutDelivery: count(db, `SELECT COUNT(*) AS count FROM channel_receipts r
|
|
11495
|
+
WHERE NOT EXISTS (SELECT 1 FROM channel_deliveries d WHERE d.delivery_id = r.delivery_id)`),
|
|
11496
|
+
deliveries: {
|
|
11497
|
+
total: rows.length,
|
|
11498
|
+
pending,
|
|
11499
|
+
inFlight: stateCounts.in_flight ?? 0,
|
|
11500
|
+
retryable,
|
|
11501
|
+
rateLimited,
|
|
11502
|
+
paused: stateCounts.paused ?? 0,
|
|
11503
|
+
unknown: stateCounts.unknown ?? 0,
|
|
11504
|
+
sent: stateCounts.sent ?? 0,
|
|
11505
|
+
failed
|
|
11506
|
+
},
|
|
11507
|
+
queueDepth: pending + retryable + rateLimited,
|
|
11508
|
+
oldestBacklogAt,
|
|
11509
|
+
oldestBacklogAgeMs: oldestBacklogAt === null ? null : now - oldestBacklogAt,
|
|
11510
|
+
accounts: [...accounts.values()].sort((left, right) => left.account < right.account ? -1 : left.account > right.account ? 1 : 0)
|
|
11511
|
+
};
|
|
11220
11512
|
}
|
|
11221
11513
|
var REQUIRED_TABLES, QUEUE_STATES, MAX_LAST_ERROR = 200;
|
|
11222
11514
|
var init_stats = __esm(() => {
|
|
11515
|
+
init_database();
|
|
11516
|
+
init_sqlite2();
|
|
11517
|
+
init_published();
|
|
11223
11518
|
REQUIRED_TABLES = ["channel_events", "channel_deliveries", "channel_receipts"];
|
|
11224
11519
|
QUEUE_STATES = new Set(["pending", "retryable", "rate_limited"]);
|
|
11225
11520
|
});
|
|
11226
11521
|
|
|
11227
11522
|
// packages/chat-room-channeld/src/state.ts
|
|
11228
11523
|
import { randomUUID as randomUUID18 } from "node:crypto";
|
|
11229
|
-
import { DatabaseSync as DatabaseSync3 } from "node:sqlite";
|
|
11230
11524
|
function isTerminalDeliveryState(state) {
|
|
11231
11525
|
return TERMINAL_DELIVERY_STATES.includes(state);
|
|
11232
11526
|
}
|
|
11233
11527
|
function transaction(db, fn) {
|
|
11234
|
-
db.
|
|
11235
|
-
|
|
11236
|
-
|
|
11237
|
-
|
|
11238
|
-
return value;
|
|
11239
|
-
} catch (error) {
|
|
11240
|
-
try {
|
|
11241
|
-
db.exec("ROLLBACK");
|
|
11242
|
-
} catch {}
|
|
11243
|
-
throw error;
|
|
11244
|
-
}
|
|
11528
|
+
return db.transaction(fn).immediate();
|
|
11529
|
+
}
|
|
11530
|
+
function likePrefix(value) {
|
|
11531
|
+
return `${value.replace(/[\\%_]/g, (ch) => `\\${ch}`)}%`;
|
|
11245
11532
|
}
|
|
11246
11533
|
function toDelivery(row) {
|
|
11247
11534
|
return {
|
|
@@ -11273,8 +11560,11 @@ function toReceipt(row) {
|
|
|
11273
11560
|
};
|
|
11274
11561
|
}
|
|
11275
11562
|
function openState(path) {
|
|
11276
|
-
const
|
|
11277
|
-
|
|
11563
|
+
const url = process.env.CHAT_ROOM_DATABASE_URL?.trim();
|
|
11564
|
+
return createChannelState(url ? new PgDatabase2(url) : new Database2(path));
|
|
11565
|
+
}
|
|
11566
|
+
function createChannelState(db) {
|
|
11567
|
+
initializeChannelSchema(db, { backfillEchoOrder: db instanceof Database2 });
|
|
11278
11568
|
let closed = false;
|
|
11279
11569
|
function getDelivery(deliveryId) {
|
|
11280
11570
|
const row = db.prepare("SELECT * FROM channel_deliveries WHERE delivery_id = ?").get(deliveryId);
|
|
@@ -11322,11 +11612,11 @@ function openState(path) {
|
|
|
11322
11612
|
nextPendingDelivery(accountKey, now) {
|
|
11323
11613
|
const prefix = `${accountKey.replace(/\/+$/, "")}/`;
|
|
11324
11614
|
const row = db.prepare(`SELECT * FROM channel_deliveries
|
|
11325
|
-
WHERE
|
|
11615
|
+
WHERE target LIKE ? ESCAPE '\\'
|
|
11326
11616
|
AND state IN ('pending', 'retryable', 'rate_limited')
|
|
11327
11617
|
AND (next_attempt_at IS NULL OR next_attempt_at <= ?)
|
|
11328
11618
|
ORDER BY created_at ASC, delivery_id ASC
|
|
11329
|
-
LIMIT 1`).get(prefix,
|
|
11619
|
+
LIMIT 1`).get(likePrefix(prefix), now);
|
|
11330
11620
|
return row ? toDelivery(row) : undefined;
|
|
11331
11621
|
},
|
|
11332
11622
|
markDelivery(deliveryId, state, patch = {}) {
|
|
@@ -11343,14 +11633,15 @@ function openState(path) {
|
|
|
11343
11633
|
},
|
|
11344
11634
|
recordSent(input) {
|
|
11345
11635
|
const receiptId = `rcp-${randomUUID18()}`;
|
|
11636
|
+
const observedAt = input.observedAt ?? Date.now();
|
|
11346
11637
|
transaction(db, () => {
|
|
11347
11638
|
db.prepare(`INSERT INTO channel_receipts
|
|
11348
11639
|
(receipt_id, delivery_id, attempt_id, state, provider_message, detail, observed_at)
|
|
11349
|
-
VALUES (?, ?, ?, 'sent', ?, ?, ?)`).run(receiptId, input.deliveryId, input.attemptId, input.providerMessage, input.detail ?? null,
|
|
11640
|
+
VALUES (?, ?, ?, 'sent', ?, ?, ?)`).run(receiptId, input.deliveryId, input.attemptId, input.providerMessage, input.detail ?? null, observedAt);
|
|
11350
11641
|
if (input.echo) {
|
|
11351
11642
|
db.prepare(`INSERT OR IGNORE INTO channel_echoes
|
|
11352
|
-
(endpoint, native_message_id, logical_message_id, delivery_id)
|
|
11353
|
-
VALUES (?, ?, ?, ?)`).run(input.echo.endpoint, input.echo.nativeMessageId, input.echo.logicalMessageId, input.deliveryId);
|
|
11643
|
+
(endpoint, native_message_id, logical_message_id, delivery_id, created_at)
|
|
11644
|
+
VALUES (?, ?, ?, ?, ?)`).run(input.echo.endpoint, input.echo.nativeMessageId, input.echo.logicalMessageId, input.deliveryId, observedAt);
|
|
11354
11645
|
}
|
|
11355
11646
|
});
|
|
11356
11647
|
return receiptId;
|
|
@@ -11359,27 +11650,27 @@ function openState(path) {
|
|
|
11359
11650
|
const prefix = `${accountKey.replace(/\/+$/, "")}/`;
|
|
11360
11651
|
const result = db.prepare(`UPDATE channel_deliveries
|
|
11361
11652
|
SET state = 'paused', next_attempt_at = NULL, last_error = ?, updated_at = ?
|
|
11362
|
-
WHERE
|
|
11363
|
-
AND state IN ('pending', 'retryable', 'rate_limited')`).run(reason, Date.now(), prefix
|
|
11653
|
+
WHERE target LIKE ? ESCAPE '\\'
|
|
11654
|
+
AND state IN ('pending', 'retryable', 'rate_limited')`).run(reason, Date.now(), likePrefix(prefix));
|
|
11364
11655
|
return Number(result.changes);
|
|
11365
11656
|
},
|
|
11366
11657
|
abandonUnconfiguredDeliveries(configuredAccountKeys) {
|
|
11367
|
-
const configured =
|
|
11368
|
-
const accountKeySql = `CASE WHEN instr(target, '/') > 0
|
|
11369
|
-
AND instr(substr(target, instr(target, '/') + 1), '/') > 0
|
|
11370
|
-
THEN substr(target, 1, instr(target, '/') + instr(substr(target, instr(target, '/') + 1), '/') - 1)
|
|
11371
|
-
ELSE target END`;
|
|
11372
|
-
const filter = configured.length > 0 ? `AND (${accountKeySql}) NOT IN (${configured.map(() => "?").join(", ")})` : "";
|
|
11658
|
+
const configured = new Set(configuredAccountKeys);
|
|
11373
11659
|
const rows = db.prepare(`SELECT * FROM channel_deliveries
|
|
11374
11660
|
WHERE state IN ('pending', 'retryable', 'rate_limited', 'in_flight', 'paused')
|
|
11375
|
-
|
|
11376
|
-
ORDER BY created_at ASC, delivery_id ASC`).all(...configured);
|
|
11661
|
+
ORDER BY created_at ASC, delivery_id ASC`).all().filter((row) => !configured.has(accountKey(String(row.target))));
|
|
11377
11662
|
if (rows.length === 0)
|
|
11378
11663
|
return [];
|
|
11379
|
-
|
|
11380
|
-
|
|
11381
|
-
|
|
11382
|
-
|
|
11664
|
+
const update = db.prepare(`UPDATE channel_deliveries
|
|
11665
|
+
SET state = 'unknown', next_attempt_at = NULL, last_error = ?, updated_at = ?
|
|
11666
|
+
WHERE delivery_id = ?
|
|
11667
|
+
AND state IN ('pending', 'retryable', 'rate_limited', 'in_flight', 'paused')`);
|
|
11668
|
+
transaction(db, () => {
|
|
11669
|
+
const now = Date.now();
|
|
11670
|
+
for (const row of rows) {
|
|
11671
|
+
update.run("account is not configured; add it back to retry", now, String(row.delivery_id));
|
|
11672
|
+
}
|
|
11673
|
+
});
|
|
11383
11674
|
return rows.map((row) => ({ ...toDelivery(row), state: "unknown" }));
|
|
11384
11675
|
},
|
|
11385
11676
|
resumePausedDeliveries() {
|
|
@@ -11388,12 +11679,23 @@ function openState(path) {
|
|
|
11388
11679
|
WHERE state = 'paused'`).run(Date.now());
|
|
11389
11680
|
return Number(result.changes);
|
|
11390
11681
|
},
|
|
11391
|
-
recoverInFlightDeliveries() {
|
|
11392
|
-
const
|
|
11393
|
-
|
|
11682
|
+
recoverInFlightDeliveries(idempotentAccountKeys) {
|
|
11683
|
+
const idempotent = new Set(idempotentAccountKeys);
|
|
11684
|
+
const rows = db.prepare("SELECT delivery_id, target FROM channel_deliveries WHERE state = 'in_flight'").all();
|
|
11685
|
+
if (!rows.length)
|
|
11686
|
+
return 0;
|
|
11687
|
+
const now = Date.now();
|
|
11688
|
+
const update = db.prepare(`UPDATE channel_deliveries
|
|
11689
|
+
SET state = ?, next_attempt_at = NULL,
|
|
11394
11690
|
last_error = 'recovered stale in_flight after restart', updated_at = ?
|
|
11395
|
-
WHERE state = 'in_flight'`)
|
|
11396
|
-
|
|
11691
|
+
WHERE delivery_id = ? AND state = 'in_flight'`);
|
|
11692
|
+
transaction(db, () => {
|
|
11693
|
+
for (const row of rows) {
|
|
11694
|
+
const state = idempotent.has(accountKey(String(row.target))) ? "pending" : "unknown";
|
|
11695
|
+
update.run(state, now, String(row.delivery_id));
|
|
11696
|
+
}
|
|
11697
|
+
});
|
|
11698
|
+
return rows.length;
|
|
11397
11699
|
},
|
|
11398
11700
|
recordReceipt(input) {
|
|
11399
11701
|
const receiptId = `rcp-${randomUUID18()}`;
|
|
@@ -11414,8 +11716,8 @@ function openState(path) {
|
|
|
11414
11716
|
},
|
|
11415
11717
|
recordEcho(endpoint, nativeMessageId, logicalMessageId, deliveryId) {
|
|
11416
11718
|
db.prepare(`INSERT OR IGNORE INTO channel_echoes
|
|
11417
|
-
(endpoint, native_message_id, logical_message_id, delivery_id)
|
|
11418
|
-
VALUES (?, ?, ?, ?)`).run(endpoint, nativeMessageId, logicalMessageId, deliveryId ?? null);
|
|
11719
|
+
(endpoint, native_message_id, logical_message_id, delivery_id, created_at)
|
|
11720
|
+
VALUES (?, ?, ?, ?, ?)`).run(endpoint, nativeMessageId, logicalMessageId, deliveryId ?? null, Date.now());
|
|
11419
11721
|
},
|
|
11420
11722
|
findEcho(endpoint, nativeMessageId) {
|
|
11421
11723
|
const row = db.prepare("SELECT logical_message_id FROM channel_echoes WHERE endpoint = ? AND native_message_id = ?").get(endpoint, nativeMessageId);
|
|
@@ -11424,7 +11726,7 @@ function openState(path) {
|
|
|
11424
11726
|
findProviderMessage(endpoint, logicalMessageId) {
|
|
11425
11727
|
const row = db.prepare(`SELECT native_message_id FROM channel_echoes
|
|
11426
11728
|
WHERE endpoint = ? AND logical_message_id = ? AND delivery_id IS NOT NULL
|
|
11427
|
-
ORDER BY
|
|
11729
|
+
ORDER BY created_at DESC LIMIT 1`).get(endpoint, logicalMessageId);
|
|
11428
11730
|
return row ? String(row.native_message_id) : undefined;
|
|
11429
11731
|
},
|
|
11430
11732
|
getDelivery,
|
|
@@ -11450,50 +11752,13 @@ function openState(path) {
|
|
|
11450
11752
|
}
|
|
11451
11753
|
};
|
|
11452
11754
|
}
|
|
11453
|
-
var DELIVERY_STATES, TERMINAL_DELIVERY_STATES
|
|
11454
|
-
CREATE TABLE IF NOT EXISTS channel_events (
|
|
11455
|
-
event_id TEXT PRIMARY KEY,
|
|
11456
|
-
dedup_key TEXT NOT NULL UNIQUE,
|
|
11457
|
-
payload TEXT NOT NULL,
|
|
11458
|
-
ingress_seq INTEGER,
|
|
11459
|
-
committed_at INTEGER NOT NULL
|
|
11460
|
-
);
|
|
11461
|
-
CREATE TABLE IF NOT EXISTS channel_deliveries (
|
|
11462
|
-
delivery_id TEXT PRIMARY KEY,
|
|
11463
|
-
event_id TEXT NOT NULL,
|
|
11464
|
-
rule_id TEXT NOT NULL,
|
|
11465
|
-
rule_version INTEGER NOT NULL,
|
|
11466
|
-
target TEXT NOT NULL,
|
|
11467
|
-
idempotency_key TEXT NOT NULL UNIQUE,
|
|
11468
|
-
payload TEXT NOT NULL,
|
|
11469
|
-
state TEXT NOT NULL,
|
|
11470
|
-
attempt_count INTEGER NOT NULL DEFAULT 0,
|
|
11471
|
-
next_attempt_at INTEGER,
|
|
11472
|
-
last_error TEXT,
|
|
11473
|
-
provider_message TEXT,
|
|
11474
|
-
created_at INTEGER NOT NULL,
|
|
11475
|
-
updated_at INTEGER NOT NULL
|
|
11476
|
-
);
|
|
11477
|
-
CREATE TABLE IF NOT EXISTS channel_receipts (
|
|
11478
|
-
receipt_id TEXT PRIMARY KEY,
|
|
11479
|
-
delivery_id TEXT NOT NULL,
|
|
11480
|
-
attempt_id TEXT NOT NULL,
|
|
11481
|
-
state TEXT NOT NULL,
|
|
11482
|
-
provider_message TEXT,
|
|
11483
|
-
detail TEXT,
|
|
11484
|
-
observed_at INTEGER NOT NULL
|
|
11485
|
-
);
|
|
11486
|
-
CREATE TABLE IF NOT EXISTS channel_echoes (
|
|
11487
|
-
endpoint TEXT NOT NULL,
|
|
11488
|
-
native_message_id TEXT NOT NULL,
|
|
11489
|
-
logical_message_id TEXT NOT NULL,
|
|
11490
|
-
delivery_id TEXT,
|
|
11491
|
-
PRIMARY KEY (endpoint, native_message_id)
|
|
11492
|
-
);
|
|
11493
|
-
`;
|
|
11755
|
+
var DELIVERY_STATES, TERMINAL_DELIVERY_STATES;
|
|
11494
11756
|
var init_state = __esm(() => {
|
|
11757
|
+
init_database();
|
|
11758
|
+
init_sqlite2();
|
|
11495
11759
|
init_published();
|
|
11496
11760
|
init_stats();
|
|
11761
|
+
init_stats();
|
|
11497
11762
|
DELIVERY_STATES = [
|
|
11498
11763
|
"pending",
|
|
11499
11764
|
"in_flight",
|
|
@@ -11792,9 +12057,6 @@ class ChannelHost {
|
|
|
11792
12057
|
const resumed = state.resumePausedDeliveries();
|
|
11793
12058
|
if (resumed > 0)
|
|
11794
12059
|
log.info(`resumed ${resumed} paused deliveries`);
|
|
11795
|
-
const recovered = state.recoverInFlightDeliveries();
|
|
11796
|
-
if (recovered > 0)
|
|
11797
|
-
log.warn(`recovered ${recovered} stale in-flight deliveries`);
|
|
11798
12060
|
this.#running = true;
|
|
11799
12061
|
this.#starting = true;
|
|
11800
12062
|
const started = [];
|
|
@@ -11850,6 +12112,16 @@ class ChannelHost {
|
|
|
11850
12112
|
state.pauseAccountDeliveries(`${account.platform}/${account.id}`, "account disabled until restart");
|
|
11851
12113
|
}
|
|
11852
12114
|
}
|
|
12115
|
+
const idempotentKeys = this.#config.accounts.filter((account) => !this.#disabled.has(account.id)).filter((account) => this.#adapters.get(account.id)?.capabilities?.send?.idempotency === "persistent_delivery_key").map((account) => `${account.platform}/${account.id}`);
|
|
12116
|
+
const recovered = state.recoverInFlightDeliveries(idempotentKeys);
|
|
12117
|
+
if (recovered > 0) {
|
|
12118
|
+
log.warn(`recovered ${recovered} stale in-flight deliveries`);
|
|
12119
|
+
for (const account of this.#config.accounts) {
|
|
12120
|
+
if (!this.#disabled.has(account.id) && idempotentKeys.includes(`${account.platform}/${account.id}`)) {
|
|
12121
|
+
this.#schedule(account.id);
|
|
12122
|
+
}
|
|
12123
|
+
}
|
|
12124
|
+
}
|
|
11853
12125
|
const usable = started.filter((accountId) => !this.#disabled.has(accountId));
|
|
11854
12126
|
if (usable.length === 0) {
|
|
11855
12127
|
this.#running = false;
|
|
@@ -12779,9 +13051,9 @@ var init_channels = __esm(() => {
|
|
|
12779
13051
|
});
|
|
12780
13052
|
|
|
12781
13053
|
// server/startup.ts
|
|
12782
|
-
import { existsSync as
|
|
13054
|
+
import { existsSync as existsSync8 } from "node:fs";
|
|
12783
13055
|
import path21 from "node:path";
|
|
12784
|
-
import { fileURLToPath as
|
|
13056
|
+
import { fileURLToPath as fileURLToPath6 } from "node:url";
|
|
12785
13057
|
function parsePort(value) {
|
|
12786
13058
|
const port = Number(value);
|
|
12787
13059
|
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
@@ -12790,8 +13062,8 @@ function parsePort(value) {
|
|
|
12790
13062
|
return port;
|
|
12791
13063
|
}
|
|
12792
13064
|
async function loadServerBootstrap() {
|
|
12793
|
-
const moduleDir = path21.dirname(
|
|
12794
|
-
const here =
|
|
13065
|
+
const moduleDir = path21.dirname(fileURLToPath6(import.meta.url));
|
|
13066
|
+
const here = existsSync8(path21.join(moduleDir, "chat.html")) ? moduleDir : path21.resolve(moduleDir, "../server");
|
|
12795
13067
|
const settings = resolveServerSettings(loadRoomConfig());
|
|
12796
13068
|
maxRoomTasks();
|
|
12797
13069
|
const hostname = settings.host;
|
|
@@ -12835,7 +13107,7 @@ var init_startup = __esm(() => {
|
|
|
12835
13107
|
});
|
|
12836
13108
|
|
|
12837
13109
|
// server/chat-server.ts
|
|
12838
|
-
var bootstrap, channels, handle, config, chat_server_default;
|
|
13110
|
+
var bootstrap, channels, serve2, handle, config, chat_server_default;
|
|
12839
13111
|
var init_chat_server = __esm(async () => {
|
|
12840
13112
|
init_http_server();
|
|
12841
13113
|
init_room_reminders();
|
|
@@ -12843,12 +13115,18 @@ var init_chat_server = __esm(async () => {
|
|
|
12843
13115
|
init_wake();
|
|
12844
13116
|
await init_app();
|
|
12845
13117
|
bootstrap = await loadServerBootstrap();
|
|
12846
|
-
|
|
13118
|
+
serve2 = createApp({
|
|
12847
13119
|
here: bootstrap.here,
|
|
12848
13120
|
token: bootstrap.token,
|
|
12849
13121
|
serveWakeUrl: bootstrap.serveWakeUrl,
|
|
12850
13122
|
channeld: () => channels
|
|
12851
13123
|
});
|
|
13124
|
+
handle = process.env.CHAT_ROOM_TEST_ACCESS_LOG === "1" ? (request) => {
|
|
13125
|
+
const path = new URL(request.url).pathname;
|
|
13126
|
+
if (path.startsWith("/assets/"))
|
|
13127
|
+
console.log(`[asset] ${path}`);
|
|
13128
|
+
return serve2(request);
|
|
13129
|
+
} : serve2;
|
|
12852
13130
|
config = {
|
|
12853
13131
|
hostname: bootstrap.hostname,
|
|
12854
13132
|
port: bootstrap.port,
|
|
@@ -12871,9 +13149,9 @@ var init_chat_server = __esm(async () => {
|
|
|
12871
13149
|
|
|
12872
13150
|
// cli.ts
|
|
12873
13151
|
import { spawnSync } from "node:child_process";
|
|
12874
|
-
import { existsSync as
|
|
13152
|
+
import { existsSync as existsSync9 } from "node:fs";
|
|
12875
13153
|
import { readFile as readFile3 } from "node:fs/promises";
|
|
12876
|
-
import { fileURLToPath as
|
|
13154
|
+
import { fileURLToPath as fileURLToPath7 } from "node:url";
|
|
12877
13155
|
var usage = `Usage: opencode-chat-room [options]
|
|
12878
13156
|
|
|
12879
13157
|
Options:
|
|
@@ -12920,7 +13198,7 @@ function relaunchTuiWithBun() {
|
|
|
12920
13198
|
const supported = major > 1 || major === 1 && (minor > 3 || minor === 3 && patch >= 14);
|
|
12921
13199
|
if (!supported)
|
|
12922
13200
|
return;
|
|
12923
|
-
const child = spawnSync("bun", [
|
|
13201
|
+
const child = spawnSync("bun", [fileURLToPath7(import.meta.url), ...process.argv.slice(2)], {
|
|
12924
13202
|
stdio: "inherit",
|
|
12925
13203
|
env: process.env
|
|
12926
13204
|
});
|
|
@@ -12930,7 +13208,7 @@ if (args[0] === "cli") {
|
|
|
12930
13208
|
relaunchTuiWithBun();
|
|
12931
13209
|
const packaged = new URL("../src/tui/main.mjs", import.meta.url);
|
|
12932
13210
|
const source = new URL("./src/tui/main.mjs", import.meta.url);
|
|
12933
|
-
const entry =
|
|
13211
|
+
const entry = existsSync9(fileURLToPath7(packaged)) ? packaged : source;
|
|
12934
13212
|
const { main } = await import(entry.href);
|
|
12935
13213
|
process.exit(await main(args.slice(1)));
|
|
12936
13214
|
}
|