@lelouchhe/webagent 0.1.0 → 0.1.2

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/lib/server.js ADDED
@@ -0,0 +1,144 @@
1
+ import { createServer } from "node:http";
2
+ import { join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { WebSocketServer } from "ws";
5
+ import { loadConfig } from "./config.js";
6
+ import { AgentBridge } from "./bridge.js";
7
+ import { Store } from "./store.js";
8
+ import { SessionManager } from "./session-manager.js";
9
+ import { TitleService } from "./title-service.js";
10
+ import { createRequestHandler } from "./routes.js";
11
+ import { setupWsHandler, broadcast } from "./ws-handler.js";
12
+ const config = loadConfig();
13
+ const __dirname = fileURLToPath(new URL(".", import.meta.url));
14
+ const PUBLIC_DIR = join(__dirname, "..", config.public_dir);
15
+ // --- Core dependencies ---
16
+ const store = new Store(config.data_dir);
17
+ console.log(`[store] using ${config.data_dir}/`);
18
+ const sessions = new SessionManager(store, config.default_cwd, config.data_dir);
19
+ const titleService = new TitleService(store, sessions, config.default_cwd);
20
+ let bridge = null;
21
+ // --- HTTP + WebSocket servers ---
22
+ const server = createServer(createRequestHandler(store, PUBLIC_DIR, config.data_dir, config.limits));
23
+ const wss = new WebSocketServer({ server });
24
+ setupWsHandler({
25
+ wss,
26
+ store,
27
+ sessions,
28
+ titleService,
29
+ getBridge: () => bridge,
30
+ limits: config.limits,
31
+ });
32
+ // --- Bridge initialization ---
33
+ async function initBridge() {
34
+ const b = new AgentBridge(config.agent_cmd);
35
+ b.on("event", (event) => {
36
+ if ("sessionId" in event && event.sessionId && sessions.restoringSessions.has(event.sessionId))
37
+ return;
38
+ switch (event.type) {
39
+ case "connected":
40
+ event.cancelTimeout = config.limits.cancel_timeout;
41
+ break;
42
+ case "session_created":
43
+ if (event.configOptions?.length)
44
+ sessions.cachedConfigOptions = event.configOptions;
45
+ for (const opt of event.configOptions ?? []) {
46
+ store.updateSessionConfig(event.sessionId, opt.id, opt.currentValue);
47
+ }
48
+ break;
49
+ case "config_option_update":
50
+ if (event.configOptions?.length)
51
+ sessions.cachedConfigOptions = event.configOptions;
52
+ for (const opt of event.configOptions ?? []) {
53
+ store.updateSessionConfig(event.sessionId, opt.id, opt.currentValue);
54
+ }
55
+ break;
56
+ case "message_chunk":
57
+ sessions.flushThinkingBuffer(event.sessionId);
58
+ sessions.appendAssistant(event.sessionId, event.text);
59
+ break;
60
+ case "thought_chunk":
61
+ sessions.flushAssistantBuffer(event.sessionId);
62
+ sessions.appendThinking(event.sessionId, event.text);
63
+ break;
64
+ case "tool_call":
65
+ sessions.flushBuffers(event.sessionId);
66
+ store.saveEvent(event.sessionId, event.type, { id: event.id, title: event.title, kind: event.kind, rawInput: event.rawInput });
67
+ break;
68
+ case "tool_call_update":
69
+ store.saveEvent(event.sessionId, event.type, { id: event.id, status: event.status, content: event.content });
70
+ break;
71
+ case "plan":
72
+ sessions.flushBuffers(event.sessionId);
73
+ store.saveEvent(event.sessionId, event.type, { entries: event.entries });
74
+ break;
75
+ case "permission_request": {
76
+ sessions.flushBuffers(event.sessionId);
77
+ store.saveEvent(event.sessionId, event.type, {
78
+ requestId: event.requestId, title: event.title, options: event.options,
79
+ });
80
+ // Auto-approve permissions in autopilot mode (allow_once only to avoid persisting across mode switches)
81
+ const mode = store.getSession(event.sessionId)?.mode ?? "";
82
+ if (mode.includes("#autopilot")) {
83
+ const opt = event.options.find((o) => o.kind === "allow_once");
84
+ if (opt) {
85
+ b.resolvePermission(event.requestId, opt.optionId);
86
+ const optionName = opt.label ?? opt.optionId;
87
+ store.saveEvent(event.sessionId, "permission_response", {
88
+ requestId: event.requestId, optionName, denied: false,
89
+ });
90
+ // Skip broadcasting the permission_request — send resolved directly
91
+ broadcast(wss, {
92
+ type: "permission_resolved",
93
+ sessionId: event.sessionId,
94
+ requestId: event.requestId,
95
+ optionName,
96
+ denied: false,
97
+ });
98
+ return;
99
+ }
100
+ }
101
+ break;
102
+ }
103
+ case "prompt_done":
104
+ sessions.activePrompts.delete(event.sessionId);
105
+ sessions.flushBuffers(event.sessionId);
106
+ store.saveEvent(event.sessionId, event.type, { stopReason: event.stopReason });
107
+ break;
108
+ case "error":
109
+ if (event.sessionId) {
110
+ sessions.activePrompts.delete(event.sessionId);
111
+ }
112
+ break;
113
+ }
114
+ broadcast(wss, event);
115
+ });
116
+ await b.start();
117
+ bridge = b;
118
+ return b;
119
+ }
120
+ // --- Graceful shutdown ---
121
+ async function shutdown() {
122
+ console.log("\n[server] shutting down...");
123
+ sessions.killAllBashProcs();
124
+ wss.close();
125
+ await bridge?.shutdown();
126
+ store.close();
127
+ server.close();
128
+ process.exit(0);
129
+ }
130
+ process.on("SIGINT", shutdown);
131
+ process.on("SIGTERM", shutdown);
132
+ // --- Start ---
133
+ server.listen(config.port, "0.0.0.0", async () => {
134
+ console.log(`[server] listening on http://localhost:${config.port}`);
135
+ console.log(`[bridge] starting: ${config.agent_cmd}...`);
136
+ try {
137
+ await initBridge();
138
+ console.log(`[bridge] ready`);
139
+ sessions.hydrate();
140
+ }
141
+ catch (err) {
142
+ console.error(`[bridge] failed to start:`, err);
143
+ }
144
+ });
@@ -0,0 +1,198 @@
1
+ import { rm } from "node:fs/promises";
2
+ import { stat } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ /** Known config option IDs that we persist per-session. */
5
+ const PERSISTED_CONFIG_IDS = ["model", "mode", "reasoning_effort"];
6
+ /**
7
+ * Centralizes all session-related state that was previously scattered
8
+ * across module-level variables in server.ts.
9
+ */
10
+ export class SessionManager {
11
+ liveSessions = new Set();
12
+ restoringSessions = new Set();
13
+ sessionHasTitle = new Set();
14
+ assistantBuffers = new Map();
15
+ thinkingBuffers = new Map();
16
+ activePrompts = new Set();
17
+ runningBashProcs = new Map();
18
+ cachedConfigOptions = [];
19
+ store;
20
+ defaultCwd;
21
+ dataDir;
22
+ constructor(store, defaultCwd, dataDir) {
23
+ this.store = store;
24
+ this.defaultCwd = defaultCwd;
25
+ this.dataDir = dataDir;
26
+ }
27
+ /** Populate sessionHasTitle from existing DB sessions on startup. */
28
+ hydrate() {
29
+ for (const s of this.store.listSessions()) {
30
+ if (s.title)
31
+ this.sessionHasTitle.add(s.id);
32
+ }
33
+ }
34
+ /** Create a new session in both bridge and store, inheriting the source session's config. */
35
+ async createSession(bridge, cwd, inheritFromSessionId) {
36
+ const sessionCwd = cwd ?? this.defaultCwd;
37
+ try {
38
+ const info = await stat(sessionCwd);
39
+ if (!info.isDirectory())
40
+ throw new Error("not a directory");
41
+ }
42
+ catch {
43
+ throw new Error(`Directory does not exist: ${sessionCwd}`);
44
+ }
45
+ const sourceSession = inheritFromSessionId
46
+ ? this.store.getSession(inheritFromSessionId)
47
+ : null;
48
+ const sessionId = await bridge.newSession(sessionCwd);
49
+ this.liveSessions.add(sessionId);
50
+ this.store.createSession(sessionId, sessionCwd);
51
+ // Inherit config options from source session
52
+ if (sourceSession) {
53
+ const inherited = [
54
+ { configId: "model", value: sourceSession.model },
55
+ { configId: "reasoning_effort", value: sourceSession.reasoning_effort },
56
+ ];
57
+ for (const { configId, value } of inherited) {
58
+ if (!value)
59
+ continue;
60
+ try {
61
+ await bridge.setConfigOption(sessionId, configId, value);
62
+ this.store.updateSessionConfig(sessionId, configId, value);
63
+ }
64
+ catch {
65
+ // Option may no longer be available; ignore
66
+ }
67
+ }
68
+ }
69
+ const session = this.store.getSession(sessionId);
70
+ return {
71
+ sessionId,
72
+ configOptions: session ? this.buildConfigOptions(session) : [],
73
+ };
74
+ }
75
+ /** Resume a session — returns event to send to the requesting client. */
76
+ async resumeSession(bridge, sessionId) {
77
+ const session = this.store.getSession(sessionId);
78
+ if (!session)
79
+ throw new Error("Session not found");
80
+ if (this.liveSessions.has(sessionId)) {
81
+ // Session already live — build configOptions with stored overrides
82
+ const configOptions = this.buildConfigOptions(session);
83
+ return {
84
+ type: "session_created",
85
+ sessionId,
86
+ cwd: session.cwd,
87
+ title: session.title,
88
+ configOptions,
89
+ busyKind: this.getBusyKind(sessionId) ?? undefined,
90
+ };
91
+ }
92
+ // Restore via ACP
93
+ this.restoringSessions.add(sessionId);
94
+ try {
95
+ const restored = await bridge.loadSession(sessionId, session.cwd);
96
+ this.liveSessions.add(sessionId);
97
+ if (session.title)
98
+ this.sessionHasTitle.add(sessionId);
99
+ const configOptions = this.applyStoredConfig(restored.configOptions, session);
100
+ console.log(`[session] restored: ${sessionId.slice(0, 8)}…`);
101
+ return {
102
+ type: "session_created",
103
+ sessionId,
104
+ cwd: session.cwd,
105
+ title: session.title,
106
+ configOptions,
107
+ busyKind: this.getBusyKind(sessionId) ?? undefined,
108
+ };
109
+ }
110
+ catch (err) {
111
+ console.error(`[session] restore failed:`, err);
112
+ throw err;
113
+ }
114
+ finally {
115
+ this.restoringSessions.delete(sessionId);
116
+ }
117
+ }
118
+ /** Build configOptions from cache, overriding currentValue with stored session values. */
119
+ buildConfigOptions(session) {
120
+ return this.applyStoredConfig(this.cachedConfigOptions, session);
121
+ }
122
+ /** Override currentValue in configOptions with stored session values. */
123
+ applyStoredConfig(configOptions, session) {
124
+ if (!configOptions.length)
125
+ return this.cachedConfigOptions;
126
+ const stored = {
127
+ model: session.model,
128
+ mode: session.mode,
129
+ reasoning_effort: session.reasoning_effort,
130
+ };
131
+ return configOptions.map((opt) => {
132
+ const override = stored[opt.id];
133
+ if (override)
134
+ return { ...opt, currentValue: override };
135
+ return opt;
136
+ });
137
+ }
138
+ /** Delete a session from store and clean up all state (including images). */
139
+ deleteSession(sessionId) {
140
+ this.store.deleteSession(sessionId);
141
+ this.liveSessions.delete(sessionId);
142
+ this.sessionHasTitle.delete(sessionId);
143
+ this.assistantBuffers.delete(sessionId);
144
+ this.thinkingBuffers.delete(sessionId);
145
+ this.activePrompts.delete(sessionId);
146
+ this.runningBashProcs.delete(sessionId);
147
+ // Remove uploaded images for this session
148
+ rm(join(this.dataDir, "images", sessionId), { recursive: true, force: true }).catch(() => { });
149
+ }
150
+ /** Flush assistant/thinking buffers to store. */
151
+ flushBuffers(sessionId) {
152
+ this.flushAssistantBuffer(sessionId);
153
+ this.flushThinkingBuffer(sessionId);
154
+ }
155
+ /** Flush only the assistant message buffer to store. */
156
+ flushAssistantBuffer(sessionId) {
157
+ const assistant = this.assistantBuffers.get(sessionId);
158
+ if (assistant) {
159
+ this.store.saveEvent(sessionId, "assistant_message", { text: assistant });
160
+ this.assistantBuffers.delete(sessionId);
161
+ }
162
+ }
163
+ /** Flush only the thinking buffer to store. */
164
+ flushThinkingBuffer(sessionId) {
165
+ const thinking = this.thinkingBuffers.get(sessionId);
166
+ if (thinking) {
167
+ this.store.saveEvent(sessionId, "thinking", { text: thinking });
168
+ this.thinkingBuffers.delete(sessionId);
169
+ }
170
+ }
171
+ /** Append to assistant message buffer. */
172
+ appendAssistant(sessionId, text) {
173
+ const buf = (this.assistantBuffers.get(sessionId) ?? "") + text;
174
+ this.assistantBuffers.set(sessionId, buf);
175
+ }
176
+ /** Append to thinking buffer. */
177
+ appendThinking(sessionId, text) {
178
+ const buf = (this.thinkingBuffers.get(sessionId) ?? "") + text;
179
+ this.thinkingBuffers.set(sessionId, buf);
180
+ }
181
+ /** Get CWD for a session (falls back to default). */
182
+ getSessionCwd(sessionId) {
183
+ return this.store.getSession(sessionId)?.cwd ?? this.defaultCwd;
184
+ }
185
+ getBusyKind(sessionId) {
186
+ if (this.runningBashProcs.has(sessionId))
187
+ return "bash";
188
+ if (this.activePrompts.has(sessionId))
189
+ return "agent";
190
+ return null;
191
+ }
192
+ /** Kill all running bash processes (for shutdown). */
193
+ killAllBashProcs() {
194
+ for (const [, proc] of this.runningBashProcs)
195
+ proc.kill("SIGKILL");
196
+ this.runningBashProcs.clear();
197
+ }
198
+ }
package/lib/store.js ADDED
@@ -0,0 +1,101 @@
1
+ import Database from "better-sqlite3";
2
+ import { mkdirSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ export class Store {
5
+ db;
6
+ constructor(dataDir) {
7
+ mkdirSync(dataDir, { recursive: true });
8
+ this.db = new Database(join(dataDir, "webagent.db"));
9
+ this.db.pragma("journal_mode = WAL");
10
+ this.migrate();
11
+ }
12
+ migrate() {
13
+ this.db.exec(`
14
+ CREATE TABLE IF NOT EXISTS sessions (
15
+ id TEXT PRIMARY KEY,
16
+ cwd TEXT NOT NULL,
17
+ title TEXT,
18
+ created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now')),
19
+ last_active_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))
20
+ );
21
+ CREATE TABLE IF NOT EXISTS events (
22
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
23
+ session_id TEXT NOT NULL REFERENCES sessions(id),
24
+ seq INTEGER NOT NULL,
25
+ type TEXT NOT NULL,
26
+ data TEXT NOT NULL DEFAULT '{}',
27
+ created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))
28
+ );
29
+ CREATE INDEX IF NOT EXISTS idx_events_session ON events(session_id, seq);
30
+ `);
31
+ // Migrate existing tables: add columns if missing
32
+ const cols = this.db.prepare("PRAGMA table_info(sessions)").all();
33
+ const colNames = new Set(cols.map(c => c.name));
34
+ if (!colNames.has("title")) {
35
+ this.db.exec("ALTER TABLE sessions ADD COLUMN title TEXT");
36
+ }
37
+ if (!colNames.has("last_active_at")) {
38
+ this.db.exec("ALTER TABLE sessions ADD COLUMN last_active_at TEXT");
39
+ // Backfill from created_at
40
+ this.db.exec("UPDATE sessions SET last_active_at = created_at WHERE last_active_at IS NULL");
41
+ }
42
+ if (!colNames.has("model")) {
43
+ this.db.exec("ALTER TABLE sessions ADD COLUMN model TEXT");
44
+ }
45
+ if (!colNames.has("mode")) {
46
+ this.db.exec("ALTER TABLE sessions ADD COLUMN mode TEXT");
47
+ }
48
+ if (!colNames.has("reasoning_effort")) {
49
+ this.db.exec("ALTER TABLE sessions ADD COLUMN reasoning_effort TEXT");
50
+ }
51
+ }
52
+ createSession(id, cwd) {
53
+ this.db.prepare("INSERT INTO sessions (id, cwd) VALUES (?, ?)").run(id, cwd);
54
+ return this.db.prepare("SELECT * FROM sessions WHERE id = ?").get(id);
55
+ }
56
+ listSessions() {
57
+ return this.db.prepare("SELECT * FROM sessions ORDER BY COALESCE(last_active_at, created_at) DESC").all();
58
+ }
59
+ getSession(id) {
60
+ return this.db.prepare("SELECT * FROM sessions WHERE id = ?").get(id);
61
+ }
62
+ deleteSession(id) {
63
+ this.db.prepare("DELETE FROM events WHERE session_id = ?").run(id);
64
+ this.db.prepare("DELETE FROM sessions WHERE id = ?").run(id);
65
+ }
66
+ updateSessionTitle(id, title) {
67
+ this.db.prepare("UPDATE sessions SET title = ? WHERE id = ?").run(title, id);
68
+ }
69
+ updateSessionLastActive(id) {
70
+ this.db.prepare("UPDATE sessions SET last_active_at = strftime('%Y-%m-%d %H:%M:%f', 'now') WHERE id = ?").run(id);
71
+ }
72
+ /** Update a config option value (model, mode, reasoning_effort) for a session. */
73
+ updateSessionConfig(id, configId, value) {
74
+ const column = { model: "model", mode: "mode", reasoning_effort: "reasoning_effort" }[configId];
75
+ if (!column)
76
+ return;
77
+ this.db.prepare(`UPDATE sessions SET ${column} = ? WHERE id = ?`).run(value, id);
78
+ }
79
+ saveEvent(sessionId, type, data = {}) {
80
+ const seq = this.db.prepare("SELECT COALESCE(MAX(seq), 0) + 1 AS next FROM events WHERE session_id = ?").get(sessionId).next;
81
+ this.db.prepare("INSERT INTO events (session_id, seq, type, data) VALUES (?, ?, ?, ?)").run(sessionId, seq, type, JSON.stringify(data));
82
+ return this.db.prepare("SELECT * FROM events WHERE session_id = ? AND seq = ?")
83
+ .get(sessionId, seq);
84
+ }
85
+ getEvents(sessionId, opts) {
86
+ let query = "SELECT * FROM events WHERE session_id = ?";
87
+ const params = [sessionId];
88
+ if (opts?.afterSeq != null) {
89
+ query += " AND seq > ?";
90
+ params.push(opts.afterSeq);
91
+ }
92
+ if (opts?.excludeThinking) {
93
+ query += " AND type != 'thinking'";
94
+ }
95
+ query += " ORDER BY seq";
96
+ return this.db.prepare(query).all(...params);
97
+ }
98
+ close() {
99
+ this.db.close();
100
+ }
101
+ }
@@ -0,0 +1,71 @@
1
+ const TITLE_MODEL = "claude-haiku-4.5";
2
+ export class TitleService {
3
+ titleSessionId = null;
4
+ activeSourceSessions = new Set();
5
+ cancelledSourceSessions = new Set();
6
+ defaultCwd;
7
+ store;
8
+ sessions;
9
+ constructor(store, sessions, defaultCwd) {
10
+ this.store = store;
11
+ this.sessions = sessions;
12
+ this.defaultCwd = defaultCwd;
13
+ }
14
+ /** Generate a title for the session (non-blocking, fire-and-forget). */
15
+ generate(bridge, userMessage, sessionId, onTitle) {
16
+ if (this.sessions.sessionHasTitle.has(sessionId) || this.activeSourceSessions.has(sessionId))
17
+ return;
18
+ this._generate(bridge, userMessage, sessionId).then((title) => {
19
+ if (title && onTitle)
20
+ onTitle(title);
21
+ }).catch((err) => {
22
+ console.error(`[title] generation failed:`, err);
23
+ });
24
+ }
25
+ async _generate(bridge, userMessage, sessionId) {
26
+ this.activeSourceSessions.add(sessionId);
27
+ const tsId = await this.ensureTitleSession(bridge);
28
+ if (!tsId) {
29
+ this.activeSourceSessions.delete(sessionId);
30
+ this.cancelledSourceSessions.delete(sessionId);
31
+ return;
32
+ }
33
+ try {
34
+ const prompt = `Generate a short title (max 30 chars, no quotes) for a chat that starts with this message. Reply with ONLY the title, nothing else:\n\n${userMessage.slice(0, 500)}`;
35
+ const title = await bridge.promptForText(tsId, prompt);
36
+ if (!title || this.cancelledSourceSessions.has(sessionId))
37
+ return;
38
+ const cleaned = title.replace(/^["']|["']$/g, "").trim().slice(0, 30);
39
+ if (!cleaned)
40
+ return;
41
+ this.store.updateSessionTitle(sessionId, cleaned);
42
+ this.sessions.sessionHasTitle.add(sessionId);
43
+ return cleaned;
44
+ }
45
+ finally {
46
+ this.activeSourceSessions.delete(sessionId);
47
+ this.cancelledSourceSessions.delete(sessionId);
48
+ }
49
+ }
50
+ async cancel(sessionId, bridge) {
51
+ this.cancelledSourceSessions.add(sessionId);
52
+ if (!this.titleSessionId || !this.activeSourceSessions.has(sessionId))
53
+ return;
54
+ await bridge.cancel(this.titleSessionId);
55
+ }
56
+ /** Ensure the dedicated title session exists. Returns session ID or null. */
57
+ async ensureTitleSession(bridge) {
58
+ if (this.titleSessionId)
59
+ return this.titleSessionId;
60
+ try {
61
+ const id = await bridge.newSession(this.defaultCwd, { silent: true });
62
+ this.sessions.liveSessions.add(id);
63
+ await bridge.setConfigOption(id, "model", TITLE_MODEL).catch(() => []);
64
+ this.titleSessionId = id;
65
+ return id;
66
+ }
67
+ catch {
68
+ return null;
69
+ }
70
+ }
71
+ }
package/lib/types.js ADDED
@@ -0,0 +1,47 @@
1
+ import { z } from "zod/v4";
2
+ // --- Inbound WS messages (client → server) ---
3
+ const ImageSchema = z.object({
4
+ data: z.string(),
5
+ mimeType: z.string(),
6
+ path: z.string().optional(),
7
+ });
8
+ export const WsMessageSchema = z.discriminatedUnion("type", [
9
+ z.object({
10
+ type: z.literal("new_session"),
11
+ cwd: z.string().optional(),
12
+ inheritFromSessionId: z.string().optional(),
13
+ }),
14
+ z.object({ type: z.literal("resume_session"), sessionId: z.string() }),
15
+ z.object({ type: z.literal("delete_session"), sessionId: z.string() }),
16
+ z.object({
17
+ type: z.literal("prompt"),
18
+ sessionId: z.string(),
19
+ text: z.string(),
20
+ images: z.array(ImageSchema).optional(),
21
+ }),
22
+ z.object({
23
+ type: z.literal("permission_response"),
24
+ sessionId: z.string().optional(),
25
+ requestId: z.string(),
26
+ optionId: z.string().optional(),
27
+ optionName: z.string().optional(),
28
+ denied: z.boolean().optional(),
29
+ }),
30
+ z.object({ type: z.literal("cancel"), sessionId: z.string() }),
31
+ z.object({ type: z.literal("set_config_option"), sessionId: z.string(), configId: z.string(), value: z.string() }),
32
+ z.object({ type: z.literal("bash_exec"), sessionId: z.string(), command: z.string() }),
33
+ z.object({ type: z.literal("bash_cancel"), sessionId: z.string() }),
34
+ ]);
35
+ // --- Utility ---
36
+ export function errorMessage(err) {
37
+ if (err instanceof Error)
38
+ return err.message;
39
+ if (typeof err === "string")
40
+ return err;
41
+ try {
42
+ return JSON.stringify(err);
43
+ }
44
+ catch {
45
+ return String(err);
46
+ }
47
+ }