agentmetrics-openclaw 0.1.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.
@@ -0,0 +1,35 @@
1
+ ---
2
+ name: agentmetrics
3
+ description: "Send every OpenClaw session to AgentMetrics for full observability — latency, failures, cost, and model usage."
4
+ metadata:
5
+ openclaw:
6
+ emoji: "📊"
7
+ events:
8
+ - agent:bootstrap
9
+ - acp:session:complete
10
+ - command:stop
11
+ requires:
12
+ env:
13
+ - AGENTMETRICS_API_KEY
14
+ ---
15
+
16
+ # AgentMetrics
17
+
18
+ Instruments every OpenClaw session automatically.
19
+
20
+ Each session becomes one event in AgentMetrics, capturing:
21
+
22
+ - Session duration
23
+ - Success or failure status
24
+ - Model used
25
+ - Agent identity
26
+
27
+ No code changes needed. Works with any agent, any model, any platform OpenClaw supports.
28
+
29
+ ## Setup
30
+
31
+ 1. Install: `openclaw plugins install agentmetrics-openclaw`
32
+ 2. Set env var: `AGENTMETRICS_API_KEY=am_your_key_here`
33
+ 3. Run your agent as normal.
34
+
35
+ Sessions appear in your [AgentMetrics dashboard](https://app.agentmetrics.dev) within seconds.
@@ -0,0 +1 @@
1
+ export default function handler(event: Record<string, unknown>): Promise<void>;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = handler;
4
+ const crypto_1 = require("crypto");
5
+ const sessions = new Map();
6
+ const API_KEY = process.env.AGENTMETRICS_API_KEY;
7
+ const BASE_URL = (process.env.AGENTMETRICS_URL ?? "https://api.agentmetrics.dev").replace(/\/$/, "");
8
+ async function send(payload) {
9
+ if (!API_KEY)
10
+ return;
11
+ try {
12
+ await fetch(`${BASE_URL}/events`, {
13
+ method: "POST",
14
+ headers: {
15
+ "Content-Type": "application/json",
16
+ "Authorization": `Bearer ${API_KEY}`,
17
+ },
18
+ body: JSON.stringify(payload),
19
+ });
20
+ }
21
+ catch {
22
+ // Never crash the agent on observability failure
23
+ }
24
+ }
25
+ function resolveAgentId(event) {
26
+ // Prefer explicit name from config, fall back to session label, then generic
27
+ const ctx = event["context"];
28
+ const cfg = ctx?.["cfg"];
29
+ return (cfg?.["name"] ??
30
+ ctx?.["label"] ??
31
+ event["sessionKey"] ??
32
+ "openclaw-agent");
33
+ }
34
+ async function handler(event) {
35
+ if (!API_KEY)
36
+ return;
37
+ const type = event["type"];
38
+ const action = event["action"];
39
+ const key = event["sessionKey"];
40
+ // ── Session start ────────────────────────────────────────────────────────
41
+ if (type === "agent" && action === "bootstrap" && key) {
42
+ sessions.set(key, {
43
+ traceId: (0, crypto_1.randomUUID)(),
44
+ agentId: resolveAgentId(event),
45
+ startedAt: Date.now(),
46
+ });
47
+ return;
48
+ }
49
+ // ── Session end (ACP) ────────────────────────────────────────────────────
50
+ if (type === "acp" && action === "session:complete" && key) {
51
+ const meta = sessions.get(key);
52
+ sessions.delete(key);
53
+ const ctx = event["context"];
54
+ const status = ctx?.["status"] === "success" ? "success" : "failed";
55
+ const error = status === "failed" ? ctx?.["lastOutput"] : undefined;
56
+ await send({
57
+ trace_id: meta?.traceId ?? (0, crypto_1.randomUUID)(),
58
+ agent_id: meta?.agentId ?? resolveAgentId(event),
59
+ status,
60
+ duration_ms: meta ? Date.now() - meta.startedAt : undefined,
61
+ error: error?.slice(0, 500),
62
+ });
63
+ return;
64
+ }
65
+ // ── Session end (manual /stop) ───────────────────────────────────────────
66
+ if (type === "command" && action === "stop" && key) {
67
+ const meta = sessions.get(key);
68
+ sessions.delete(key);
69
+ if (!meta)
70
+ return;
71
+ await send({
72
+ trace_id: meta.traceId,
73
+ agent_id: meta.agentId,
74
+ status: "success",
75
+ duration_ms: Date.now() - meta.startedAt,
76
+ });
77
+ }
78
+ }
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "agentmetrics-openclaw",
3
+ "version": "0.1.0",
4
+ "description": "AgentMetrics observability hook for OpenClaw agents",
5
+ "license": "MIT",
6
+ "homepage": "https://agentmetrics.dev",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/andausman/agentmetrics"
10
+ },
11
+ "keywords": ["openclaw", "agentmetrics", "observability", "ai-agents", "monitoring"],
12
+ "openclaw": {
13
+ "hooks": ["hooks/agentmetrics"]
14
+ },
15
+ "files": [
16
+ "hooks/agentmetrics/HOOK.md",
17
+ "hooks/agentmetrics/handler.js",
18
+ "hooks/agentmetrics/handler.d.ts"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "devDependencies": {
25
+ "typescript": "^5.4.0"
26
+ },
27
+ "engines": {
28
+ "node": ">=18"
29
+ }
30
+ }