ework-web 0.5.0 → 0.5.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/package.json +1 -1
- package/src/coordination.ts +32 -0
- package/src/index.ts +20 -2
- package/src/opencode.ts +58 -1
package/package.json
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getDB } from "./db";
|
|
2
|
+
import { log } from "./logger";
|
|
3
|
+
|
|
4
|
+
export interface DaemonInfo {
|
|
5
|
+
id: number;
|
|
6
|
+
displayName: string;
|
|
7
|
+
endpoint: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const HEARTBEAT_STALE_MS = 120_000;
|
|
11
|
+
|
|
12
|
+
export async function getActiveDaemons(): Promise<DaemonInfo[]> {
|
|
13
|
+
const stale = new Date(Date.now() - HEARTBEAT_STALE_MS);
|
|
14
|
+
const staleStr = stale.toISOString().slice(0, 19).replace("T", " ");
|
|
15
|
+
try {
|
|
16
|
+
const rows = await getDB().all<{ id: number; display_name: string; internal_endpoint: string }>(
|
|
17
|
+
`SELECT id, display_name, internal_endpoint FROM {{d_daemons}} WHERE status = 'active' AND last_heartbeat > ?`,
|
|
18
|
+
[staleStr],
|
|
19
|
+
);
|
|
20
|
+
return rows
|
|
21
|
+
.filter((r): r is { id: number; display_name: string; internal_endpoint: string } =>
|
|
22
|
+
r.internal_endpoint !== null && r.internal_endpoint !== undefined && r.internal_endpoint !== "")
|
|
23
|
+
.map((r) => ({
|
|
24
|
+
id: r.id,
|
|
25
|
+
displayName: r.display_name ?? `daemon-${r.id}`,
|
|
26
|
+
endpoint: r.internal_endpoint,
|
|
27
|
+
}));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
log.info(`coordination: query failed (${e instanceof Error ? e.message : String(e)}) — assuming single-machine`);
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -6,10 +6,11 @@ import { homedir } from "os";
|
|
|
6
6
|
import { loadConfig, DB_OVERRIDABLE, parseOverride, resolveTtsBackend } from "./config";
|
|
7
7
|
import type { Config } from "./config";
|
|
8
8
|
import { setConfig, initDB } from "./db";
|
|
9
|
+
import { getActiveDaemons } from "./coordination";
|
|
9
10
|
import { testMysqlConnection, migrateSqliteToMysql, writeMysqlEnv, migrateMysqlToSqlite, writeSqliteEnv, migrateDaemonSqliteToMysql } from "./db-admin";
|
|
10
11
|
import type { MysqlTargetOpts } from "./db-admin";
|
|
11
12
|
import { checkAuth, makeAuthCookieHeader, clearAuthCookieHeader, loginHTML, sanitizeNext, ensureBootstrapAdmin, ensureBootstrapSystem, isReservedSystemLogin } from "./auth";
|
|
12
|
-
import { OpencodeError, createOpencodeClient } from "./opencode";
|
|
13
|
+
import { OpencodeError, createOpencodeClient, MultiDaemonOpencodeClient, RemoteOpencodeClient, isLocalhost, type OpencodeClientInterface } from "./opencode";
|
|
13
14
|
import { renderMarkdown } from "./render/markdown";
|
|
14
15
|
import { log, uptimeSeconds, version } from "./logger";
|
|
15
16
|
import { buildIssueThread, fetchIssuePage, fetchIssueSince, errorPage } from "./views/issueThread";
|
|
@@ -95,7 +96,24 @@ const STATIC_DIR = join(__dirname, "static");
|
|
|
95
96
|
|
|
96
97
|
await initDB();
|
|
97
98
|
const cfg: Config = await loadConfig();
|
|
98
|
-
|
|
99
|
+
let opencode: OpencodeClientInterface = createOpencodeClient(cfg, cfg.daemonWebhookUrl);
|
|
100
|
+
|
|
101
|
+
async function refreshOpencodeClient(): Promise<void> {
|
|
102
|
+
const daemons = await getActiveDaemons();
|
|
103
|
+
const remote = daemons.filter((d) => !isLocalhost(d.endpoint));
|
|
104
|
+
if (remote.length > 1) {
|
|
105
|
+
opencode = new MultiDaemonOpencodeClient(remote.map((d) => d.endpoint));
|
|
106
|
+
log.info(`opencode client: multi-daemon (${remote.length} remotes)`);
|
|
107
|
+
} else if (remote.length === 1) {
|
|
108
|
+
opencode = new RemoteOpencodeClient(remote[0]!.endpoint);
|
|
109
|
+
log.info(`opencode client: single remote (${remote[0]!.endpoint})`);
|
|
110
|
+
} else {
|
|
111
|
+
opencode = createOpencodeClient(cfg, cfg.daemonWebhookUrl);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
await refreshOpencodeClient();
|
|
116
|
+
setInterval(refreshOpencodeClient, 10_000);
|
|
99
117
|
|
|
100
118
|
async function autoWireDaemon(projectId: number, origin: string): Promise<void> {
|
|
101
119
|
if (!cfg.autowireActive) {
|
package/src/opencode.ts
CHANGED
|
@@ -293,11 +293,68 @@ export class RemoteOpencodeClient implements OpencodeClientInterface {
|
|
|
293
293
|
}
|
|
294
294
|
}
|
|
295
295
|
|
|
296
|
-
function isLocalhost(endpoint: string): boolean {
|
|
296
|
+
export function isLocalhost(endpoint: string): boolean {
|
|
297
297
|
const host = endpoint.replace(/^https?:\/\//, "").split(":")[0] ?? "";
|
|
298
298
|
return /^(127\.|localhost$|0\.0\.0\.0$|::1$|\[::1\]$)/.test(host);
|
|
299
299
|
}
|
|
300
300
|
|
|
301
|
+
export class MultiDaemonOpencodeClient implements OpencodeClientInterface {
|
|
302
|
+
private readonly clients: RemoteOpencodeClient[];
|
|
303
|
+
|
|
304
|
+
constructor(endpoints: string[]) {
|
|
305
|
+
this.clients = endpoints.map((ep) => new RemoteOpencodeClient(ep));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async listSessions(limit: number): Promise<SessionListItem[]> {
|
|
309
|
+
const results = await Promise.allSettled(
|
|
310
|
+
this.clients.map((c) => c.listSessions(limit)),
|
|
311
|
+
);
|
|
312
|
+
const all: SessionListItem[] = [];
|
|
313
|
+
const seen = new Set<string>();
|
|
314
|
+
for (const r of results) {
|
|
315
|
+
if (r.status === "fulfilled") {
|
|
316
|
+
for (const s of r.value) {
|
|
317
|
+
if (s.id && !seen.has(s.id)) {
|
|
318
|
+
seen.add(s.id);
|
|
319
|
+
all.push(s);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
all.sort((a, b) => b.updated - a.updated);
|
|
325
|
+
return all.slice(0, limit);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async exportSession(id: string): Promise<SessionExport> {
|
|
329
|
+
try {
|
|
330
|
+
return await Promise.any(this.clients.map((c) => c.exportSession(id)));
|
|
331
|
+
} catch {
|
|
332
|
+
throw new OpencodeError(`session ${id} not found on any daemon`, 404);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async exportSessionRaw(id: string): Promise<string> {
|
|
337
|
+
try {
|
|
338
|
+
return await Promise.any(this.clients.map((c) => c.exportSessionRaw(id)));
|
|
339
|
+
} catch {
|
|
340
|
+
throw new OpencodeError(`session ${id} not found on any daemon`, 404);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async listModels(): Promise<string[]> {
|
|
345
|
+
const results = await Promise.allSettled(
|
|
346
|
+
this.clients.map((c) => c.listModels()),
|
|
347
|
+
);
|
|
348
|
+
const models = new Set<string>();
|
|
349
|
+
for (const r of results) {
|
|
350
|
+
if (r.status === "fulfilled") {
|
|
351
|
+
for (const m of r.value) models.add(m);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return [...models];
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
301
358
|
export function createOpencodeClient(cfg: Config, daemonEndpoint?: string): OpencodeClientInterface {
|
|
302
359
|
if (daemonEndpoint && !isLocalhost(daemonEndpoint)) {
|
|
303
360
|
return new RemoteOpencodeClient(daemonEndpoint);
|