-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemit_sample_events.node.mjs
More file actions
78 lines (71 loc) · 2.2 KB
/
emit_sample_events.node.mjs
File metadata and controls
78 lines (71 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Minimal POST /v1/events sample (Node 18+ with global fetch, or Node 20+).
*
* node examples/integration/emit_sample_events.node.mjs \
* --base-url http://127.0.0.1:8765 \
* --release-id rel_yourregisteredid \
* --agent-id agent_support \
* [--api-token "$FLIGHTDECK_LOCAL_API_TOKEN"]
*
* Same envelope as emit_sample_events.py: { "events": [ RunEvent, ... ] }.
*/
function arg(name, fallback = null) {
const i = process.argv.indexOf(name);
if (i === -1 || i + 1 >= process.argv.length) return fallback;
return process.argv[i + 1];
}
const baseUrl = (arg("--base-url", "http://127.0.0.1:8765") ?? "http://127.0.0.1:8765").replace(/\/$/, "");
const releaseId = arg("--release-id");
const agentId = arg("--agent-id");
const environment = arg("--environment", "local") ?? "local";
const apiToken = arg("--api-token", process.env.FLIGHTDECK_LOCAL_API_TOKEN ?? null);
if (!releaseId || !agentId) {
console.error(
"Usage: node emit_sample_events.node.mjs --release-id REL --agent-id AGENT [--base-url URL] [--environment ENV] [--api-token TOKEN]",
);
process.exit(2);
}
const rid = Math.random().toString(16).slice(2, 12);
const ts = new Date().toISOString();
const event = {
api_version: "v1",
type: "run_end",
timestamp: ts,
workspace_id: "ws_local",
agent_id: agentId,
release_id: releaseId,
run_id: `emit-sample-node-${rid}`,
tenant_id: "tenant_example",
task_id: "task_example",
environment,
metrics: { latency_ms: 250, success: true, error_type: null },
usage: {
model: {
provider: "openai",
model: "gpt-4.1-mini",
input_tokens: 400,
output_tokens: 120,
cached_input_tokens: 0,
},
tools: [],
},
labels: { source: "examples/integration/emit_sample_events.node.mjs" },
};
const body = JSON.stringify({ events: [event] });
const url = `${baseUrl}/v1/events`;
const headers = { "Content-Type": "application/json" };
if (apiToken) {
headers.Authorization = `Bearer ${apiToken}`;
}
const res = await fetch(url, {
method: "POST",
headers,
body,
});
const text = await res.text();
if (!res.ok) {
console.error(`HTTP ${res.status}: ${text}`);
process.exit(1);
}
console.log(`POST ${url} -> ${res.status}`);
console.log(text);