@aiagenta2z/onekey-gateway 0.1.3 → 0.1.5

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.
@@ -0,0 +1,9 @@
1
+ export type MonitorOptions = {
2
+ url: string;
3
+ intervalMs: number;
4
+ timeoutMs: number;
5
+ webhookUrl?: string;
6
+ failAfter: number;
7
+ once?: boolean;
8
+ };
9
+ export declare function runMonitor(options: MonitorOptions): Promise<void>;
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.runMonitor = runMonitor;
7
+ const http_1 = __importDefault(require("http"));
8
+ const https_1 = __importDefault(require("https"));
9
+ function nowIso() {
10
+ return new Date().toISOString();
11
+ }
12
+ function sleep(ms) {
13
+ return new Promise((resolve) => setTimeout(resolve, ms));
14
+ }
15
+ function httpGet(url, timeoutMs) {
16
+ const lib = url.protocol === "https:" ? https_1.default : http_1.default;
17
+ const start = Date.now();
18
+ return new Promise((resolve) => {
19
+ const req = lib.request({
20
+ method: "GET",
21
+ hostname: url.hostname,
22
+ port: url.port ? Number(url.port) : undefined,
23
+ path: `${url.pathname}${url.search}`,
24
+ headers: {
25
+ "User-Agent": "onekey-monitor"
26
+ }
27
+ }, (res) => {
28
+ const chunks = [];
29
+ res.on("data", (c) => chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(String(c))));
30
+ res.on("end", () => {
31
+ const latencyMs = Date.now() - start;
32
+ const status = res.statusCode || 0;
33
+ const body = Buffer.concat(chunks).toString("utf8");
34
+ resolve({ ok: status >= 200 && status < 300, status, latencyMs, body });
35
+ });
36
+ });
37
+ req.on("error", () => {
38
+ const latencyMs = Date.now() - start;
39
+ resolve({ ok: false, latencyMs });
40
+ });
41
+ req.setTimeout(timeoutMs, () => {
42
+ req.destroy();
43
+ const latencyMs = Date.now() - start;
44
+ resolve({ ok: false, latencyMs });
45
+ });
46
+ req.end();
47
+ });
48
+ }
49
+ async function postJson(url, payload, timeoutMs) {
50
+ const data = Buffer.from(JSON.stringify(payload), "utf8");
51
+ const lib = url.protocol === "https:" ? https_1.default : http_1.default;
52
+ await new Promise((resolve) => {
53
+ const req = lib.request({
54
+ method: "POST",
55
+ hostname: url.hostname,
56
+ port: url.port ? Number(url.port) : undefined,
57
+ path: `${url.pathname}${url.search}`,
58
+ headers: {
59
+ "Content-Type": "application/json",
60
+ "Content-Length": String(data.length),
61
+ "User-Agent": "onekey-monitor"
62
+ }
63
+ }, () => resolve());
64
+ req.on("error", () => resolve());
65
+ req.setTimeout(timeoutMs, () => {
66
+ req.destroy();
67
+ resolve();
68
+ });
69
+ req.write(data);
70
+ req.end();
71
+ });
72
+ }
73
+ async function maybeNotify(webhookUrl, message, timeoutMs) {
74
+ if (!webhookUrl)
75
+ return;
76
+ try {
77
+ await postJson(new URL(webhookUrl), message, timeoutMs);
78
+ }
79
+ catch {
80
+ // best-effort
81
+ }
82
+ }
83
+ async function runMonitor(options) {
84
+ const url = new URL(options.url);
85
+ let consecutiveFailures = 0;
86
+ for (;;) {
87
+ const result = await httpGet(url, options.timeoutMs);
88
+ const ts = nowIso();
89
+ if (result.ok) {
90
+ consecutiveFailures = 0;
91
+ process.stdout.write(`[${ts}] ok url=${options.url} status=${result.status} latency_ms=${result.latencyMs}\n`);
92
+ }
93
+ else {
94
+ consecutiveFailures += 1;
95
+ process.stderr.write(`[${ts}] fail url=${options.url} status=${result.status ?? "NA"} latency_ms=${result.latencyMs} failures=${consecutiveFailures}\n`);
96
+ if (consecutiveFailures >= options.failAfter) {
97
+ await maybeNotify(options.webhookUrl, {
98
+ text: `OneKey monitor alert: ${options.url} failing`,
99
+ url: options.url,
100
+ failures: consecutiveFailures,
101
+ timestamp: ts
102
+ }, Math.min(options.timeoutMs, 10000));
103
+ }
104
+ }
105
+ if (options.once) {
106
+ if (!result.ok)
107
+ process.exitCode = 2;
108
+ return;
109
+ }
110
+ await sleep(options.intervalMs);
111
+ }
112
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiagenta2z/onekey-gateway",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "OneKey Gateway CLI and SDK for routing AI agent, MCP, Skills and LLM requests by DeepNLP x AI Agent A2Z",
5
5
  "license": "MIT",
6
6
  "author": "aiagenta2z",
@@ -21,10 +21,13 @@
21
21
  "scripts": {
22
22
  "build": "tsc -p tsconfig.json",
23
23
  "dev": "tsc -p tsconfig.json --watch",
24
- "clean": "rm -rf dist"
24
+ "clean": "rm -rf dist",
25
+ "monitor": "node dist/cli.js monitor --url http://127.0.0.1:18000/healthz --interval 10",
26
+ "ph:ai-agent-digest": "node dist/scripts/producthuntAiAgentDigest.js"
25
27
  },
26
28
  "devDependencies": {
27
29
  "@types/node": "^25.5.0",
30
+ "@types/nodemailer": "^6.4.17",
28
31
  "typescript": "^5.5.4"
29
32
  },
30
33
  "engines": {
@@ -39,6 +42,8 @@
39
42
  "router"
40
43
  ],
41
44
  "dependencies": {
42
- "@aiagenta2z/onekey-gateway": "^0.1.0"
45
+ "node-pty": "^1.1.0",
46
+ "nodemailer": "^6.10.1",
47
+ "onekey": "file:./packages/onekey-bin"
43
48
  }
44
49
  }