@agentgazer/server 0.1.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/LICENSE +191 -0
- package/README.md +86 -0
- package/dist/alerts/evaluator.d.ts +9 -0
- package/dist/alerts/evaluator.d.ts.map +1 -0
- package/dist/alerts/evaluator.js +323 -0
- package/dist/alerts/evaluator.js.map +1 -0
- package/dist/db.d.ts +121 -0
- package/dist/db.d.ts.map +1 -0
- package/dist/db.js +425 -0
- package/dist/db.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/auth.d.ts +3 -0
- package/dist/middleware/auth.d.ts.map +1 -0
- package/dist/middleware/auth.js +65 -0
- package/dist/middleware/auth.js.map +1 -0
- package/dist/middleware/rate-limit.d.ts +8 -0
- package/dist/middleware/rate-limit.d.ts.map +1 -0
- package/dist/middleware/rate-limit.js +72 -0
- package/dist/middleware/rate-limit.js.map +1 -0
- package/dist/routes/agents.d.ts +3 -0
- package/dist/routes/agents.d.ts.map +1 -0
- package/dist/routes/agents.js +149 -0
- package/dist/routes/agents.js.map +1 -0
- package/dist/routes/alerts.d.ts +3 -0
- package/dist/routes/alerts.d.ts.map +1 -0
- package/dist/routes/alerts.js +286 -0
- package/dist/routes/alerts.js.map +1 -0
- package/dist/routes/auth.d.ts +3 -0
- package/dist/routes/auth.d.ts.map +1 -0
- package/dist/routes/auth.js +20 -0
- package/dist/routes/auth.js.map +1 -0
- package/dist/routes/events.d.ts +3 -0
- package/dist/routes/events.d.ts.map +1 -0
- package/dist/routes/events.js +304 -0
- package/dist/routes/events.js.map +1 -0
- package/dist/routes/health.d.ts +3 -0
- package/dist/routes/health.d.ts.map +1 -0
- package/dist/routes/health.js +13 -0
- package/dist/routes/health.js.map +1 -0
- package/dist/routes/model-rules.d.ts +3 -0
- package/dist/routes/model-rules.d.ts.map +1 -0
- package/dist/routes/model-rules.js +71 -0
- package/dist/routes/model-rules.js.map +1 -0
- package/dist/routes/rate-limits.d.ts +3 -0
- package/dist/routes/rate-limits.d.ts.map +1 -0
- package/dist/routes/rate-limits.js +58 -0
- package/dist/routes/rate-limits.js.map +1 -0
- package/dist/routes/stats.d.ts +3 -0
- package/dist/routes/stats.d.ts.map +1 -0
- package/dist/routes/stats.js +233 -0
- package/dist/routes/stats.js.map +1 -0
- package/dist/server.d.ts +20 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +155 -0
- package/dist/server.js.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2025 AgentTrace Contributors
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# @agentgazer/server
|
|
2
|
+
|
|
3
|
+
Express + SQLite API server for AgentGazer. Handles event ingestion, agent management, statistics, alerting, and serves the web dashboard.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
### As part of the CLI (typical)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npx agentgazer
|
|
11
|
+
# Server starts on port 8080
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
### Programmatic
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { startServer } from "@agentgazer/server";
|
|
18
|
+
|
|
19
|
+
const { server, shutdown } = await startServer({
|
|
20
|
+
port: 8080,
|
|
21
|
+
token: "your-auth-token",
|
|
22
|
+
dbPath: "/path/to/data.db",
|
|
23
|
+
dashboardDir: "/path/to/dashboard/dist", // optional
|
|
24
|
+
retentionDays: 30, // optional
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Later
|
|
28
|
+
await shutdown();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## API endpoints
|
|
32
|
+
|
|
33
|
+
All endpoints except `/health` and `/api/auth/verify` require `Authorization: Bearer <token>`.
|
|
34
|
+
|
|
35
|
+
### Health & auth
|
|
36
|
+
|
|
37
|
+
- `GET /health` — Server health check
|
|
38
|
+
- `POST /api/auth/verify` — Verify auth token
|
|
39
|
+
|
|
40
|
+
### Events
|
|
41
|
+
|
|
42
|
+
- `POST /api/events` — Ingest a batch of events (rate-limited: 1000/min per key)
|
|
43
|
+
- `GET /api/events` — Query events with filters (`agent_id`, `from`, `to`, `provider`, `model`, `trace_id`, `search`)
|
|
44
|
+
- `GET /api/events/export` — Export events as CSV or JSON
|
|
45
|
+
|
|
46
|
+
### Agents
|
|
47
|
+
|
|
48
|
+
- `GET /api/agents` — List registered agents
|
|
49
|
+
- `POST /api/agents/register` — Register or update an agent
|
|
50
|
+
- `POST /api/agents/heartbeat` — Record a heartbeat
|
|
51
|
+
|
|
52
|
+
### Statistics
|
|
53
|
+
|
|
54
|
+
- `GET /api/stats/summary` — Aggregated stats (total calls, tokens, cost, errors)
|
|
55
|
+
- `GET /api/stats/timeseries` — Time-bucketed stats (`minute`, `hour`, `day`)
|
|
56
|
+
|
|
57
|
+
### Alerts
|
|
58
|
+
|
|
59
|
+
- `GET /api/alerts` — List alert rules
|
|
60
|
+
- `POST /api/alerts` — Create an alert rule
|
|
61
|
+
- `PUT /api/alerts/:id` — Update an alert rule
|
|
62
|
+
- `DELETE /api/alerts/:id` — Delete an alert rule
|
|
63
|
+
- `PATCH /api/alerts/:id/toggle` — Toggle enabled status
|
|
64
|
+
- `GET /api/alert-history` — View alert delivery history
|
|
65
|
+
|
|
66
|
+
See `openapi.yaml` in this package for the full OpenAPI specification.
|
|
67
|
+
|
|
68
|
+
## Alert rule types
|
|
69
|
+
|
|
70
|
+
| Type | Config | Description |
|
|
71
|
+
|------|--------|-------------|
|
|
72
|
+
| `agent_down` | `{ duration_minutes: number }` | No heartbeat within duration |
|
|
73
|
+
| `error_rate` | `{ window_minutes: number, threshold: number }` | Error % exceeds threshold |
|
|
74
|
+
| `budget` | `{ threshold: number }` | Daily USD spend exceeds threshold |
|
|
75
|
+
|
|
76
|
+
Alerts are delivered via webhook and/or email. Webhooks retry up to 3 times with exponential backoff. Email requires SMTP environment variables (`SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`, `SMTP_PASS`).
|
|
77
|
+
|
|
78
|
+
A 15-minute cooldown prevents duplicate alerts for the same rule.
|
|
79
|
+
|
|
80
|
+
## Data retention
|
|
81
|
+
|
|
82
|
+
Old events and alert history are automatically purged based on `retentionDays` (default: 30). Cleanup runs on startup and every 24 hours.
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
Apache-2.0
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type Database from "better-sqlite3";
|
|
2
|
+
export interface EvaluatorOptions {
|
|
3
|
+
db: Database.Database;
|
|
4
|
+
interval?: number;
|
|
5
|
+
}
|
|
6
|
+
export declare function startEvaluator(options: EvaluatorOptions): {
|
|
7
|
+
stop: () => void;
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=evaluator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evaluator.d.ts","sourceRoot":"","sources":["../../src/alerts/evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAU3C,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,QAAQ,CAAC,QAAQ,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAkbD,wBAAgB,cAAc,CAAC,OAAO,EAAE,gBAAgB,GAAG;IAAE,IAAI,EAAE,MAAM,IAAI,CAAA;CAAE,CAuB9E"}
|
|
@@ -0,0 +1,323 @@
|
|
|
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.startEvaluator = startEvaluator;
|
|
7
|
+
const nodemailer_1 = __importDefault(require("nodemailer"));
|
|
8
|
+
const shared_1 = require("@agentgazer/shared");
|
|
9
|
+
const log = (0, shared_1.createLogger)("evaluator");
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// Prepared-statement SQL
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
const SQL_ENABLED_RULES = `SELECT * FROM alert_rules WHERE enabled = 1`;
|
|
14
|
+
const SQL_AGENT_BY_ID = `SELECT * FROM agents WHERE agent_id = ?`;
|
|
15
|
+
const SQL_ERROR_RATE = `
|
|
16
|
+
SELECT
|
|
17
|
+
COUNT(*) AS total,
|
|
18
|
+
SUM(CASE WHEN status_code >= 400 THEN 1 ELSE 0 END) AS errors
|
|
19
|
+
FROM agent_events
|
|
20
|
+
WHERE agent_id = ?
|
|
21
|
+
AND event_type IN ('llm_call', 'completion')
|
|
22
|
+
AND timestamp >= ?
|
|
23
|
+
`;
|
|
24
|
+
const SQL_BUDGET_TODAY = `
|
|
25
|
+
SELECT COALESCE(SUM(cost_usd), 0) AS total_cost
|
|
26
|
+
FROM agent_events
|
|
27
|
+
WHERE agent_id = ?
|
|
28
|
+
AND event_type IN ('llm_call', 'completion')
|
|
29
|
+
AND timestamp >= ?
|
|
30
|
+
`;
|
|
31
|
+
const SQL_COOLDOWN_CHECK = `
|
|
32
|
+
SELECT id FROM alert_history
|
|
33
|
+
WHERE alert_rule_id = ? AND delivered_at > datetime('now', '-15 minutes')
|
|
34
|
+
LIMIT 1
|
|
35
|
+
`;
|
|
36
|
+
const SQL_INSERT_HISTORY = `
|
|
37
|
+
INSERT INTO alert_history (alert_rule_id, agent_id, rule_type, message, delivered_via)
|
|
38
|
+
VALUES (?, ?, ?, ?, ?)
|
|
39
|
+
`;
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// Webhook delivery
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
function postWebhook(url, payload) {
|
|
44
|
+
// Fire initial attempt, then retry in the background without blocking.
|
|
45
|
+
void postWebhookWithRetry(url, payload);
|
|
46
|
+
}
|
|
47
|
+
async function postWebhookWithRetry(url, payload) {
|
|
48
|
+
const MAX_RETRIES = 3;
|
|
49
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
50
|
+
try {
|
|
51
|
+
const res = await fetch(url, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: { "Content-Type": "application/json" },
|
|
54
|
+
body: JSON.stringify(payload),
|
|
55
|
+
signal: AbortSignal.timeout(10_000),
|
|
56
|
+
});
|
|
57
|
+
if (res.ok)
|
|
58
|
+
return;
|
|
59
|
+
log.error(`Webhook POST returned ${res.status}`, { url, attempt: attempt + 1, maxAttempts: MAX_RETRIES + 1 });
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
log.error(`Webhook POST failed`, { url, attempt: attempt + 1, maxAttempts: MAX_RETRIES + 1, err: String(err) });
|
|
63
|
+
}
|
|
64
|
+
if (attempt < MAX_RETRIES) {
|
|
65
|
+
const delayMs = Math.pow(4, attempt) * 1000; // 1s, 4s, 16s
|
|
66
|
+
await new Promise((r) => setTimeout(r, delayMs));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// Email delivery
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
function getSmtpTransport(smtpConfig) {
|
|
74
|
+
// Use per-rule SMTP config if provided
|
|
75
|
+
if (smtpConfig && smtpConfig.host) {
|
|
76
|
+
return nodemailer_1.default.createTransport({
|
|
77
|
+
host: smtpConfig.host,
|
|
78
|
+
port: smtpConfig.port || 587,
|
|
79
|
+
secure: smtpConfig.secure || false,
|
|
80
|
+
...(smtpConfig.user && smtpConfig.pass ? { auth: { user: smtpConfig.user, pass: smtpConfig.pass } } : {}),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
// Fall back to environment variables
|
|
84
|
+
const host = process.env.SMTP_HOST;
|
|
85
|
+
if (!host)
|
|
86
|
+
return null;
|
|
87
|
+
const port = parseInt(process.env.SMTP_PORT ?? "587", 10);
|
|
88
|
+
const secure = process.env.SMTP_SECURE === "true";
|
|
89
|
+
const user = process.env.SMTP_USER;
|
|
90
|
+
const pass = process.env.SMTP_PASS;
|
|
91
|
+
return nodemailer_1.default.createTransport({
|
|
92
|
+
host,
|
|
93
|
+
port,
|
|
94
|
+
secure,
|
|
95
|
+
...(user && pass ? { auth: { user, pass } } : {}),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async function sendEmail(payload, smtpConfig, legacyTo) {
|
|
99
|
+
const transport = getSmtpTransport(smtpConfig);
|
|
100
|
+
if (!transport) {
|
|
101
|
+
log.warn("Email alert skipped: SMTP not configured");
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const from = smtpConfig?.from ?? process.env.SMTP_FROM ?? "alerts@agentgazer.com";
|
|
105
|
+
const to = smtpConfig?.to ?? legacyTo;
|
|
106
|
+
if (!to) {
|
|
107
|
+
log.warn("Email alert skipped: no recipient configured");
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const ruleLabel = payload.rule_type.replace(/_/g, " ");
|
|
111
|
+
const subject = `[AgentGazer] ${ruleLabel} alert: ${payload.agent_id}`;
|
|
112
|
+
const text = [
|
|
113
|
+
`Alert: ${ruleLabel}`,
|
|
114
|
+
`Agent: ${payload.agent_id}`,
|
|
115
|
+
`Time: ${payload.timestamp}`,
|
|
116
|
+
``,
|
|
117
|
+
payload.message,
|
|
118
|
+
].join("\n");
|
|
119
|
+
try {
|
|
120
|
+
await transport.sendMail({ from, to, subject, text });
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
log.error(`Email delivery failed`, { to, err: String(err) });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
// Telegram delivery
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
async function sendTelegram(payload, telegramConfig) {
|
|
130
|
+
const { bot_token, chat_id, message_template } = telegramConfig;
|
|
131
|
+
if (!bot_token || !chat_id) {
|
|
132
|
+
log.warn("Telegram alert skipped: missing bot_token or chat_id");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
// Use template or default message
|
|
136
|
+
const template = message_template || "[From AgentGazer] Alert: {rule_type} - Agent: {agent_id} - {message}";
|
|
137
|
+
const text = template
|
|
138
|
+
.replace(/{agent_id}/g, payload.agent_id)
|
|
139
|
+
.replace(/{rule_type}/g, payload.rule_type.replace(/_/g, " "))
|
|
140
|
+
.replace(/{message}/g, payload.message)
|
|
141
|
+
.replace(/{timestamp}/g, payload.timestamp);
|
|
142
|
+
const url = `https://api.telegram.org/bot${bot_token}/sendMessage`;
|
|
143
|
+
try {
|
|
144
|
+
const res = await fetch(url, {
|
|
145
|
+
method: "POST",
|
|
146
|
+
headers: { "Content-Type": "application/json" },
|
|
147
|
+
body: JSON.stringify({
|
|
148
|
+
chat_id,
|
|
149
|
+
text,
|
|
150
|
+
parse_mode: "HTML",
|
|
151
|
+
}),
|
|
152
|
+
signal: AbortSignal.timeout(10_000),
|
|
153
|
+
});
|
|
154
|
+
if (!res.ok) {
|
|
155
|
+
const body = await res.text();
|
|
156
|
+
log.error(`Telegram API returned ${res.status}`, { chat_id, body });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
log.error(`Telegram delivery failed`, { chat_id, err: String(err) });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
// Evaluation helpers
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
function evaluateAgentDown(db, rule, config) {
|
|
167
|
+
const agent = db.prepare(SQL_AGENT_BY_ID).get(rule.agent_id);
|
|
168
|
+
if (!agent) {
|
|
169
|
+
// Agent has never been registered -- treat as inactive.
|
|
170
|
+
return `Agent "${rule.agent_id}" has never been registered`;
|
|
171
|
+
}
|
|
172
|
+
if (!agent.updated_at) {
|
|
173
|
+
return `Agent "${rule.agent_id}" has no recorded activity`;
|
|
174
|
+
}
|
|
175
|
+
const lastActivity = new Date(agent.updated_at.endsWith("Z") || agent.updated_at.includes("+")
|
|
176
|
+
? agent.updated_at
|
|
177
|
+
: agent.updated_at + "Z").getTime();
|
|
178
|
+
const threshold = config.duration_minutes * 60 * 1000;
|
|
179
|
+
const now = Date.now();
|
|
180
|
+
if (now - lastActivity > threshold) {
|
|
181
|
+
const minutesAgo = Math.round((now - lastActivity) / 60_000);
|
|
182
|
+
return `Agent "${rule.agent_id}" has been inactive for ${minutesAgo} minutes (threshold: ${config.duration_minutes}m)`;
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
function evaluateErrorRate(db, rule, config) {
|
|
187
|
+
const windowStart = new Date(Date.now() - config.window_minutes * 60 * 1000).toISOString();
|
|
188
|
+
const row = db.prepare(SQL_ERROR_RATE).get(rule.agent_id, windowStart);
|
|
189
|
+
if (row.total === 0) {
|
|
190
|
+
return null; // No events in window, nothing to evaluate.
|
|
191
|
+
}
|
|
192
|
+
const rate = (row.errors / row.total) * 100;
|
|
193
|
+
if (rate > config.threshold) {
|
|
194
|
+
return `Agent "${rule.agent_id}" error rate is ${rate.toFixed(1)}% (${row.errors}/${row.total}) over the last ${config.window_minutes}m (threshold: ${config.threshold}%)`;
|
|
195
|
+
}
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
function evaluateBudget(db, rule, config) {
|
|
199
|
+
// UTC midnight today
|
|
200
|
+
const todayUTC = new Date();
|
|
201
|
+
todayUTC.setUTCHours(0, 0, 0, 0);
|
|
202
|
+
const since = todayUTC.toISOString();
|
|
203
|
+
const row = db.prepare(SQL_BUDGET_TODAY).get(rule.agent_id, since);
|
|
204
|
+
if (row.total_cost > config.threshold) {
|
|
205
|
+
return `Agent "${rule.agent_id}" daily spend is $${row.total_cost.toFixed(4)} (threshold: $${config.threshold})`;
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
// Core tick
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
async function tick(db) {
|
|
213
|
+
let rules;
|
|
214
|
+
try {
|
|
215
|
+
rules = db.prepare(SQL_ENABLED_RULES).all();
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
log.error("Failed to query alert rules", { err: String(err) });
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
for (const rule of rules) {
|
|
222
|
+
try {
|
|
223
|
+
const config = JSON.parse(rule.config);
|
|
224
|
+
let message = null;
|
|
225
|
+
switch (rule.rule_type) {
|
|
226
|
+
case "agent_down":
|
|
227
|
+
message = evaluateAgentDown(db, rule, config);
|
|
228
|
+
break;
|
|
229
|
+
case "error_rate":
|
|
230
|
+
message = evaluateErrorRate(db, rule, config);
|
|
231
|
+
break;
|
|
232
|
+
case "budget":
|
|
233
|
+
message = evaluateBudget(db, rule, config);
|
|
234
|
+
break;
|
|
235
|
+
default:
|
|
236
|
+
log.warn(`Unknown rule_type "${rule.rule_type}"`, { ruleId: rule.id });
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (message === null) {
|
|
240
|
+
continue; // Condition not met; nothing to fire.
|
|
241
|
+
}
|
|
242
|
+
// -- Cooldown check: skip if we already delivered for this rule recently.
|
|
243
|
+
const recent = db.prepare(SQL_COOLDOWN_CHECK).get(rule.id);
|
|
244
|
+
if (recent) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
// -- Fire alert --------------------------------------------------------
|
|
248
|
+
log.info(`Alert fired: ${message}`, { ruleId: rule.id, ruleType: rule.rule_type, agentId: rule.agent_id });
|
|
249
|
+
const timestamp = new Date().toISOString();
|
|
250
|
+
const payload = {
|
|
251
|
+
agent_id: rule.agent_id,
|
|
252
|
+
rule_type: rule.rule_type,
|
|
253
|
+
message,
|
|
254
|
+
timestamp,
|
|
255
|
+
};
|
|
256
|
+
const notificationType = rule.notification_type || "webhook";
|
|
257
|
+
if (notificationType === "webhook" && rule.webhook_url) {
|
|
258
|
+
postWebhook(rule.webhook_url, payload);
|
|
259
|
+
db.prepare(SQL_INSERT_HISTORY).run(rule.id, rule.agent_id, rule.rule_type, message, "webhook");
|
|
260
|
+
}
|
|
261
|
+
else if (notificationType === "email") {
|
|
262
|
+
try {
|
|
263
|
+
let smtpConfig = null;
|
|
264
|
+
if (rule.smtp_config) {
|
|
265
|
+
try {
|
|
266
|
+
smtpConfig = JSON.parse(rule.smtp_config);
|
|
267
|
+
}
|
|
268
|
+
catch { /* ignore */ }
|
|
269
|
+
}
|
|
270
|
+
await sendEmail(payload, smtpConfig, rule.email);
|
|
271
|
+
db.prepare(SQL_INSERT_HISTORY).run(rule.id, rule.agent_id, rule.rule_type, message, "email");
|
|
272
|
+
}
|
|
273
|
+
catch (emailErr) {
|
|
274
|
+
log.error("Email delivery failed, history not recorded", { ruleId: rule.id, err: String(emailErr) });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else if (notificationType === "telegram") {
|
|
278
|
+
try {
|
|
279
|
+
let telegramConfig = null;
|
|
280
|
+
if (rule.telegram_config) {
|
|
281
|
+
try {
|
|
282
|
+
telegramConfig = JSON.parse(rule.telegram_config);
|
|
283
|
+
}
|
|
284
|
+
catch { /* ignore */ }
|
|
285
|
+
}
|
|
286
|
+
if (telegramConfig) {
|
|
287
|
+
await sendTelegram(payload, telegramConfig);
|
|
288
|
+
db.prepare(SQL_INSERT_HISTORY).run(rule.id, rule.agent_id, rule.rule_type, message, "telegram");
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
log.warn("Telegram alert skipped: no telegram_config", { ruleId: rule.id });
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
catch (tgErr) {
|
|
295
|
+
log.error("Telegram delivery failed, history not recorded", { ruleId: rule.id, err: String(tgErr) });
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
catch (err) {
|
|
300
|
+
log.error(`Error evaluating rule`, { ruleId: rule.id, ruleType: rule.rule_type, err: String(err) });
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
// ---------------------------------------------------------------------------
|
|
305
|
+
// Public API
|
|
306
|
+
// ---------------------------------------------------------------------------
|
|
307
|
+
function startEvaluator(options) {
|
|
308
|
+
const intervalMs = options.interval ?? 60_000;
|
|
309
|
+
const { db } = options;
|
|
310
|
+
// Run the first evaluation immediately (fire-and-forget).
|
|
311
|
+
tick(db).catch((err) => log.error("Evaluator tick failed", { err: String(err) }));
|
|
312
|
+
const timer = setInterval(() => {
|
|
313
|
+
tick(db).catch((err) => log.error("Evaluator tick failed", { err: String(err) }));
|
|
314
|
+
}, intervalMs);
|
|
315
|
+
// Allow Node.js to exit naturally even if the timer is still pending.
|
|
316
|
+
timer.unref();
|
|
317
|
+
return {
|
|
318
|
+
stop() {
|
|
319
|
+
clearInterval(timer);
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
//# sourceMappingURL=evaluator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evaluator.js","sourceRoot":"","sources":["../../src/alerts/evaluator.ts"],"names":[],"mappings":";;;;;AA+bA,wCAuBC;AArdD,4DAAoC;AACpC,+CAAkD;AAElD,MAAM,GAAG,GAAG,IAAA,qBAAY,EAAC,WAAW,CAAC,CAAC;AAuDtC,8EAA8E;AAC9E,yBAAyB;AACzB,8EAA8E;AAE9E,MAAM,iBAAiB,GAAG,6CAA6C,CAAC;AAExE,MAAM,eAAe,GAAG,yCAAyC,CAAC;AAElE,MAAM,cAAc,GAAG;;;;;;;;CAQtB,CAAC;AAEF,MAAM,gBAAgB,GAAG;;;;;;CAMxB,CAAC;AAEF,MAAM,kBAAkB,GAAG;;;;CAI1B,CAAC;AAEF,MAAM,kBAAkB,GAAG;;;CAG1B,CAAC;AAEF,8EAA8E;AAC9E,mBAAmB;AACnB,8EAA8E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