@dobby.ai/dobby 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -192,6 +192,12 @@ dobby config schema show <contributionId> [--json]
|
|
|
192
192
|
|
|
193
193
|
配置变更建议直接编辑 `gateway.json`,再通过 `dobby doctor` 或 `dobby start` 做校验。
|
|
194
194
|
|
|
195
|
+
连接器状态:
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
dobby connector status [connectorId] [--json]
|
|
199
|
+
```
|
|
200
|
+
|
|
195
201
|
扩展管理:
|
|
196
202
|
|
|
197
203
|
```bash
|
|
@@ -397,6 +403,8 @@ npm run start -- cron add daily-report \
|
|
|
397
403
|
- 所有 connector 都会经过宿主侧 health supervisor 包装
|
|
398
404
|
- 统一暴露 `starting / ready / degraded / reconnecting / failed / stopped` 状态
|
|
399
405
|
- 若 connector 长时间停留在 `starting`、`degraded`、`reconnecting` 或 `failed`,宿主会 stop 并重建实例
|
|
406
|
+
- 运行中的 gateway 会把 connector 状态快照写到 `<data.rootDir>/state/connectors-status.json`
|
|
407
|
+
- `dobby connector status` 会读取这份快照并展示当前 connector 健康状态
|
|
400
408
|
- guild channel 仍按显式 binding 匹配
|
|
401
409
|
- DM 可通过 `bindings.default` 回落到默认 route
|
|
402
410
|
- 线程消息使用父频道 ID 做 binding 查找
|
|
@@ -443,7 +451,7 @@ npm run plugins:setup:local
|
|
|
443
451
|
|
|
444
452
|
- `plugins/*` 是扩展源码,不是运行时加载入口
|
|
445
453
|
- 本地扩展安装到 extension store 后,才会被宿主识别
|
|
446
|
-
- `@dobby.ai/plugin-sdk`
|
|
454
|
+
- `@dobby.ai/plugin-sdk` 在插件里按非 optional 的 `peerDependencies` 暴露,开发期通过 `file:../plugin-sdk` 提供
|
|
447
455
|
|
|
448
456
|
## 检查与测试
|
|
449
457
|
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { connectorStatusSnapshotExists, connectorStatusSnapshotPath, isConnectorStatusSnapshotStale, readConnectorStatusSnapshot, } from "../../core/connector-status.js";
|
|
3
|
+
import { requireRawConfig, resolveConfigPath, resolveDataRootDir } from "../shared/config-io.js";
|
|
4
|
+
function formatTimestamp(timestampMs) {
|
|
5
|
+
return new Date(timestampMs).toISOString();
|
|
6
|
+
}
|
|
7
|
+
function pad(value, width) {
|
|
8
|
+
return value.padEnd(width);
|
|
9
|
+
}
|
|
10
|
+
function renderTable(items) {
|
|
11
|
+
const rows = items.map((item) => ({
|
|
12
|
+
connectorId: item.connectorId,
|
|
13
|
+
platform: item.platform,
|
|
14
|
+
availability: item.availability,
|
|
15
|
+
health: item.health.status,
|
|
16
|
+
restarts: String(item.health.restartCount ?? 0),
|
|
17
|
+
updated: formatTimestamp(item.health.updatedAtMs),
|
|
18
|
+
}));
|
|
19
|
+
const widths = {
|
|
20
|
+
connectorId: Math.max("CONNECTOR".length, ...rows.map((row) => row.connectorId.length)),
|
|
21
|
+
platform: Math.max("PLATFORM".length, ...rows.map((row) => row.platform.length)),
|
|
22
|
+
availability: Math.max("AVAILABILITY".length, ...rows.map((row) => row.availability.length)),
|
|
23
|
+
health: Math.max("HEALTH".length, ...rows.map((row) => row.health.length)),
|
|
24
|
+
restarts: Math.max("RESTARTS".length, ...rows.map((row) => row.restarts.length)),
|
|
25
|
+
updated: Math.max("UPDATED".length, ...rows.map((row) => row.updated.length)),
|
|
26
|
+
};
|
|
27
|
+
const lines = [
|
|
28
|
+
[
|
|
29
|
+
pad("CONNECTOR", widths.connectorId),
|
|
30
|
+
pad("PLATFORM", widths.platform),
|
|
31
|
+
pad("AVAILABILITY", widths.availability),
|
|
32
|
+
pad("HEALTH", widths.health),
|
|
33
|
+
pad("RESTARTS", widths.restarts),
|
|
34
|
+
pad("UPDATED", widths.updated),
|
|
35
|
+
].join(" "),
|
|
36
|
+
];
|
|
37
|
+
for (const row of rows) {
|
|
38
|
+
lines.push([
|
|
39
|
+
pad(row.connectorId, widths.connectorId),
|
|
40
|
+
pad(row.platform, widths.platform),
|
|
41
|
+
pad(row.availability, widths.availability),
|
|
42
|
+
pad(row.health, widths.health),
|
|
43
|
+
pad(row.restarts, widths.restarts),
|
|
44
|
+
pad(row.updated, widths.updated),
|
|
45
|
+
].join(" "));
|
|
46
|
+
}
|
|
47
|
+
return lines;
|
|
48
|
+
}
|
|
49
|
+
export async function runConnectorStatusCommand(options) {
|
|
50
|
+
const configPath = resolveConfigPath();
|
|
51
|
+
const rawConfig = await requireRawConfig(configPath);
|
|
52
|
+
const statusPath = connectorStatusSnapshotPath(join(resolveDataRootDir(configPath, rawConfig), "state"));
|
|
53
|
+
if (!(await connectorStatusSnapshotExists(statusPath))) {
|
|
54
|
+
throw new Error(`Connector status snapshot '${statusPath}' does not exist. Start 'dobby start' first.`);
|
|
55
|
+
}
|
|
56
|
+
const snapshot = await readConnectorStatusSnapshot(statusPath);
|
|
57
|
+
const items = options.connectorId
|
|
58
|
+
? snapshot.items.filter((item) => item.connectorId === options.connectorId)
|
|
59
|
+
: snapshot.items;
|
|
60
|
+
if (options.connectorId && items.length === 0) {
|
|
61
|
+
throw new Error(`Connector '${options.connectorId}' was not found in '${statusPath}'.`);
|
|
62
|
+
}
|
|
63
|
+
if (options.json) {
|
|
64
|
+
console.log(JSON.stringify({ ...snapshot, items }));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (isConnectorStatusSnapshotStale(snapshot)) {
|
|
68
|
+
console.log("Warning: connector status snapshot is stale; the gateway may not be running.");
|
|
69
|
+
}
|
|
70
|
+
if (items.length === 0) {
|
|
71
|
+
console.log("(empty)");
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
for (const line of renderTable(items)) {
|
|
75
|
+
console.log(line);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -2,6 +2,7 @@ import { dirname, join } from "node:path";
|
|
|
2
2
|
import { loadCronConfig } from "../../cron/config.js";
|
|
3
3
|
import { CronService } from "../../cron/service.js";
|
|
4
4
|
import { CronStore } from "../../cron/store.js";
|
|
5
|
+
import { connectorStatusSnapshotPath, DEFAULT_CONNECTOR_STATUS_PUBLISH_INTERVAL_MS, DEFAULT_CONNECTOR_STATUS_STALE_AFTER_MS, writeConnectorStatusSnapshot, } from "../../core/connector-status.js";
|
|
5
6
|
import { DedupStore } from "../../core/dedup-store.js";
|
|
6
7
|
import { Gateway } from "../../core/gateway.js";
|
|
7
8
|
import { BindingResolver, loadGatewayConfig, RouteResolver } from "../../core/routing.js";
|
|
@@ -90,6 +91,8 @@ function selectSandboxInstances(config) {
|
|
|
90
91
|
export async function runStartCommand() {
|
|
91
92
|
const configPath = resolveConfigPath();
|
|
92
93
|
const config = await loadGatewayConfig(configPath);
|
|
94
|
+
const gatewayStartedAtMs = Date.now();
|
|
95
|
+
const connectorStatusPath = connectorStatusSnapshotPath(config.data.stateDir);
|
|
93
96
|
await ensureDataDirs(config.data.rootDir);
|
|
94
97
|
const logger = createLogger();
|
|
95
98
|
const loader = new ExtensionLoader(logger, {
|
|
@@ -151,18 +154,67 @@ export async function runStartCommand() {
|
|
|
151
154
|
gateway,
|
|
152
155
|
logger,
|
|
153
156
|
});
|
|
157
|
+
const publishConnectorStatuses = async () => {
|
|
158
|
+
await writeConnectorStatusSnapshot(connectorStatusPath, {
|
|
159
|
+
schemaVersion: 1,
|
|
160
|
+
generatedAtMs: Date.now(),
|
|
161
|
+
staleAfterMs: DEFAULT_CONNECTOR_STATUS_STALE_AFTER_MS,
|
|
162
|
+
gateway: {
|
|
163
|
+
pid: process.pid,
|
|
164
|
+
startedAtMs: gatewayStartedAtMs,
|
|
165
|
+
},
|
|
166
|
+
items: gateway.listConnectorStatuses(),
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
let connectorStatusTimer = null;
|
|
170
|
+
const startConnectorStatusPublisher = async () => {
|
|
171
|
+
try {
|
|
172
|
+
await publishConnectorStatuses();
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
logger.warn({ err: error, connectorStatusPath }, "Failed to write initial connector status snapshot");
|
|
176
|
+
}
|
|
177
|
+
connectorStatusTimer = setInterval(() => {
|
|
178
|
+
void publishConnectorStatuses().catch((error) => {
|
|
179
|
+
logger.warn({ err: error, connectorStatusPath }, "Failed to refresh connector status snapshot");
|
|
180
|
+
});
|
|
181
|
+
}, DEFAULT_CONNECTOR_STATUS_PUBLISH_INTERVAL_MS);
|
|
182
|
+
};
|
|
183
|
+
const stopConnectorStatusPublisher = async () => {
|
|
184
|
+
if (connectorStatusTimer) {
|
|
185
|
+
clearInterval(connectorStatusTimer);
|
|
186
|
+
connectorStatusTimer = null;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
await publishConnectorStatuses();
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
logger.warn({ err: error, connectorStatusPath }, "Failed to write final connector status snapshot");
|
|
193
|
+
}
|
|
194
|
+
};
|
|
154
195
|
await gateway.start();
|
|
155
196
|
await cronService.start();
|
|
197
|
+
await startConnectorStatusPublisher();
|
|
156
198
|
logger.info({
|
|
157
199
|
configPath,
|
|
158
200
|
cronConfigPath: loadedCronConfig.configPath,
|
|
159
201
|
cronConfigSource: loadedCronConfig.source,
|
|
160
202
|
cronEnabled: loadedCronConfig.config.enabled,
|
|
161
203
|
}, "Gateway started");
|
|
204
|
+
let shuttingDown = false;
|
|
162
205
|
const shutdown = async (signal) => {
|
|
206
|
+
if (shuttingDown) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
shuttingDown = true;
|
|
163
210
|
logger.info({ signal }, "Shutting down gateway");
|
|
211
|
+
if (connectorStatusTimer) {
|
|
212
|
+
clearInterval(connectorStatusTimer);
|
|
213
|
+
connectorStatusTimer = null;
|
|
214
|
+
}
|
|
164
215
|
await cronService.stop();
|
|
165
216
|
await gateway.stop();
|
|
217
|
+
await stopConnectorStatusPublisher();
|
|
166
218
|
await hostExecutor.close();
|
|
167
219
|
await closeProviderInstances(providers, logger);
|
|
168
220
|
await closeSandboxInstances(sandboxes, logger);
|
package/dist/src/cli/program.js
CHANGED
|
@@ -2,6 +2,7 @@ import { existsSync, readFileSync } from "node:fs";
|
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
3
|
import { Command } from "commander";
|
|
4
4
|
import { runConfigListCommand, runConfigSchemaListCommand, runConfigSchemaShowCommand, runConfigShowCommand, } from "./commands/config.js";
|
|
5
|
+
import { runConnectorStatusCommand } from "./commands/connector.js";
|
|
5
6
|
import { runCronAddCommand, runCronListCommand, runCronPauseCommand, runCronRemoveCommand, runCronResumeCommand, runCronRunCommand, runCronStatusCommand, runCronUpdateCommand, } from "./commands/cron.js";
|
|
6
7
|
import { runDoctorCommand } from "./commands/doctor.js";
|
|
7
8
|
import { runExtensionInstallCommand, runExtensionListCommand, runExtensionUninstallCommand, } from "./commands/extension.js";
|
|
@@ -127,6 +128,18 @@ export function buildProgram() {
|
|
|
127
128
|
fix: Boolean(opts.fix),
|
|
128
129
|
});
|
|
129
130
|
});
|
|
131
|
+
const connectorCommand = program.command("connector").description("Inspect runtime connector status");
|
|
132
|
+
connectorCommand
|
|
133
|
+
.command("status")
|
|
134
|
+
.description("Show status for all connectors or one connector")
|
|
135
|
+
.argument("[connectorId]", "Connector instance ID")
|
|
136
|
+
.option("--json", "Output JSON", false)
|
|
137
|
+
.action(async (connectorId, opts) => {
|
|
138
|
+
await runConnectorStatusCommand({
|
|
139
|
+
...(typeof connectorId === "string" ? { connectorId } : {}),
|
|
140
|
+
json: Boolean(opts.json),
|
|
141
|
+
});
|
|
142
|
+
});
|
|
130
143
|
const cronCommand = program.command("cron").description("Manage scheduled cron jobs");
|
|
131
144
|
cronCommand
|
|
132
145
|
.command("add")
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { access, mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
export const CONNECTOR_STATUS_SNAPSHOT_FILENAME = "connectors-status.json";
|
|
4
|
+
export const DEFAULT_CONNECTOR_STATUS_PUBLISH_INTERVAL_MS = 5_000;
|
|
5
|
+
export const DEFAULT_CONNECTOR_STATUS_STALE_AFTER_MS = 15_000;
|
|
6
|
+
function isRecord(value) {
|
|
7
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
function createFallbackHealth(detail) {
|
|
10
|
+
const now = Date.now();
|
|
11
|
+
return {
|
|
12
|
+
status: "stopped",
|
|
13
|
+
detail,
|
|
14
|
+
statusSinceMs: now,
|
|
15
|
+
updatedAtMs: now,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function availabilityFromHealthStatus(status) {
|
|
19
|
+
switch (status) {
|
|
20
|
+
case "ready":
|
|
21
|
+
return "online";
|
|
22
|
+
case "degraded":
|
|
23
|
+
return "degraded";
|
|
24
|
+
case "reconnecting":
|
|
25
|
+
return "reconnecting";
|
|
26
|
+
case "starting":
|
|
27
|
+
case "failed":
|
|
28
|
+
case "stopped":
|
|
29
|
+
default:
|
|
30
|
+
return "offline";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function connectorStatusSnapshotPath(stateDir) {
|
|
34
|
+
return join(resolve(stateDir), CONNECTOR_STATUS_SNAPSHOT_FILENAME);
|
|
35
|
+
}
|
|
36
|
+
export function statusItemFromConnector(connector) {
|
|
37
|
+
const health = connector.getHealth?.() ?? createFallbackHealth("Connector health is not available");
|
|
38
|
+
const availability = availabilityFromHealthStatus(health.status);
|
|
39
|
+
return {
|
|
40
|
+
connectorId: connector.id,
|
|
41
|
+
platform: connector.platform,
|
|
42
|
+
connectorName: connector.name,
|
|
43
|
+
availability,
|
|
44
|
+
online: availability === "online",
|
|
45
|
+
health,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async function writeAtomic(filePath, content) {
|
|
49
|
+
const absolutePath = resolve(filePath);
|
|
50
|
+
await mkdir(dirname(absolutePath), { recursive: true });
|
|
51
|
+
const tempPath = `${absolutePath}.tmp-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
52
|
+
await writeFile(tempPath, content, "utf-8");
|
|
53
|
+
await rename(tempPath, absolutePath);
|
|
54
|
+
}
|
|
55
|
+
export async function writeConnectorStatusSnapshot(filePath, snapshot) {
|
|
56
|
+
await writeAtomic(filePath, `${JSON.stringify(snapshot, null, 2)}\n`);
|
|
57
|
+
}
|
|
58
|
+
export async function connectorStatusSnapshotExists(filePath) {
|
|
59
|
+
try {
|
|
60
|
+
await access(resolve(filePath));
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function parseHealth(value) {
|
|
68
|
+
if (!isRecord(value)) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
if (typeof value.status !== "string" || typeof value.statusSinceMs !== "number" || typeof value.updatedAtMs !== "number") {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
function parseStatusItem(value) {
|
|
77
|
+
if (!isRecord(value)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (typeof value.connectorId !== "string"
|
|
81
|
+
|| typeof value.platform !== "string"
|
|
82
|
+
|| typeof value.connectorName !== "string"
|
|
83
|
+
|| typeof value.availability !== "string"
|
|
84
|
+
|| typeof value.online !== "boolean") {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const health = parseHealth(value.health);
|
|
88
|
+
if (!health) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
connectorId: value.connectorId,
|
|
93
|
+
platform: value.platform,
|
|
94
|
+
connectorName: value.connectorName,
|
|
95
|
+
availability: value.availability,
|
|
96
|
+
online: value.online,
|
|
97
|
+
health,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export async function readConnectorStatusSnapshot(filePath) {
|
|
101
|
+
const raw = await readFile(resolve(filePath), "utf-8");
|
|
102
|
+
const parsed = JSON.parse(raw);
|
|
103
|
+
if (!isRecord(parsed) || parsed.schemaVersion !== 1 || typeof parsed.generatedAtMs !== "number" || typeof parsed.staleAfterMs !== "number") {
|
|
104
|
+
throw new Error(`Connector status snapshot '${resolve(filePath)}' has invalid metadata`);
|
|
105
|
+
}
|
|
106
|
+
if (!isRecord(parsed.gateway) || typeof parsed.gateway.pid !== "number" || typeof parsed.gateway.startedAtMs !== "number") {
|
|
107
|
+
throw new Error(`Connector status snapshot '${resolve(filePath)}' has invalid gateway metadata`);
|
|
108
|
+
}
|
|
109
|
+
if (!Array.isArray(parsed.items)) {
|
|
110
|
+
throw new Error(`Connector status snapshot '${resolve(filePath)}' must contain an items array`);
|
|
111
|
+
}
|
|
112
|
+
const items = parsed.items.map((item) => {
|
|
113
|
+
const normalized = parseStatusItem(item);
|
|
114
|
+
if (!normalized) {
|
|
115
|
+
throw new Error(`Connector status snapshot '${resolve(filePath)}' contains an invalid connector entry`);
|
|
116
|
+
}
|
|
117
|
+
return normalized;
|
|
118
|
+
});
|
|
119
|
+
return {
|
|
120
|
+
schemaVersion: 1,
|
|
121
|
+
generatedAtMs: parsed.generatedAtMs,
|
|
122
|
+
staleAfterMs: parsed.staleAfterMs,
|
|
123
|
+
gateway: {
|
|
124
|
+
pid: parsed.gateway.pid,
|
|
125
|
+
startedAtMs: parsed.gateway.startedAtMs,
|
|
126
|
+
},
|
|
127
|
+
items,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
export function isConnectorStatusSnapshotStale(snapshot, now = Date.now()) {
|
|
131
|
+
return now - snapshot.generatedAtMs > snapshot.staleAfterMs;
|
|
132
|
+
}
|
package/dist/src/core/gateway.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import { EventForwarder } from "../agent/event-forwarder.js";
|
|
3
|
+
import { statusItemFromConnector } from "./connector-status.js";
|
|
3
4
|
import { parseControlCommand } from "./control-command.js";
|
|
4
5
|
import { createTypingKeepAliveController } from "./typing-controller.js";
|
|
5
6
|
function isImageAttachment(attachment) {
|
|
@@ -67,6 +68,11 @@ export class Gateway {
|
|
|
67
68
|
await this.options.runtimeRegistry.closeAll();
|
|
68
69
|
this.started = false;
|
|
69
70
|
}
|
|
71
|
+
listConnectorStatuses() {
|
|
72
|
+
return this.options.connectors
|
|
73
|
+
.map((connector) => statusItemFromConnector(connector))
|
|
74
|
+
.sort((a, b) => a.connectorId.localeCompare(b.connectorId));
|
|
75
|
+
}
|
|
70
76
|
async handleScheduled(request) {
|
|
71
77
|
const connector = this.connectorsById.get(request.connectorId);
|
|
72
78
|
if (!connector) {
|