@chirpier/chirpier-js 0.1.6 → 0.2.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/README.md +184 -108
- package/dist/__tests__/chirpier.test.js +435 -100
- package/dist/constants.d.ts +7 -6
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +5 -4
- package/dist/index.d.ts +129 -56
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +483 -203
- package/package.json +5 -5
- package/src/__tests__/chirpier.test.ts +296 -92
- package/src/constants.ts +7 -6
- package/src/index.ts +492 -206
package/README.md
CHANGED
|
@@ -1,155 +1,231 @@
|
|
|
1
1
|
# Chirpier SDK
|
|
2
2
|
|
|
3
|
-
The Chirpier SDK for JavaScript
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- Easy-to-use API for sending events to Chirpier
|
|
8
|
-
- Automatic batching of events for improved performance
|
|
9
|
-
- Automatic retry mechanism with exponential backoff
|
|
10
|
-
- Thread-safe operations
|
|
11
|
-
- Periodic flushing of the event queue
|
|
12
|
-
- Environment Agnostic: Works seamlessly in both browser and Node.js environments.
|
|
3
|
+
The Chirpier SDK for JavaScript sends OpenClaw-friendly flat events to Chirpier/Ingres with automatic batching and retries.
|
|
13
4
|
|
|
14
5
|
## Installation
|
|
15
6
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
``` bash
|
|
7
|
+
<!-- docs:start install -->
|
|
8
|
+
```bash
|
|
19
9
|
npm install @chirpier/chirpier-js
|
|
20
10
|
```
|
|
11
|
+
<!-- docs:end install -->
|
|
21
12
|
|
|
22
|
-
##
|
|
13
|
+
## Quick Start
|
|
23
14
|
|
|
24
|
-
|
|
15
|
+
<!-- docs:start quickstart -->
|
|
16
|
+
### Singleton API
|
|
25
17
|
|
|
26
|
-
|
|
18
|
+
```ts
|
|
19
|
+
import { initialize, logEvent, stop } from "@chirpier/chirpier-js";
|
|
27
20
|
|
|
28
|
-
|
|
21
|
+
initialize({ key: "chp_your_api_key" });
|
|
29
22
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
23
|
+
await logEvent({
|
|
24
|
+
log_id: "9f97d65f-fb30-4062-b4d0-8617c03fe4f6",
|
|
25
|
+
agent: "openclaw.main",
|
|
26
|
+
event: "tool.errors.count",
|
|
27
|
+
value: 1,
|
|
28
|
+
meta: { tool_name: "browser.open", workflow: "triage" },
|
|
29
|
+
});
|
|
37
30
|
|
|
38
|
-
|
|
39
|
-
monitor({
|
|
40
|
-
group_id: '02e4f4d8-415e-4fc1-b01a-677ac5bc9207',
|
|
41
|
-
stream_name: 'My measurement',
|
|
42
|
-
value: 15.30,
|
|
43
|
-
} as Event);
|
|
31
|
+
await stop();
|
|
44
32
|
```
|
|
45
33
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
```js
|
|
49
|
-
const express = require('express');
|
|
50
|
-
const { initialize, monitor, Event } = require('@chirpier/chirpier-js');
|
|
34
|
+
### Instance API (Recommended)
|
|
51
35
|
|
|
52
|
-
|
|
53
|
-
|
|
36
|
+
```ts
|
|
37
|
+
import { createClient } from "@chirpier/chirpier-js";
|
|
54
38
|
|
|
55
|
-
|
|
56
|
-
initialize({ key: 'your-api-key', region: 'us-west' });
|
|
39
|
+
const client = createClient({ key: "chp_your_api_key" });
|
|
57
40
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (!group_id || !stream_name || !value) {
|
|
64
|
-
return res.status(400).json({ error: 'Missing required fields' });
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Monitor an event
|
|
68
|
-
monitor({ group_id, stream_name, value } as Event);
|
|
69
|
-
|
|
70
|
-
res.status(200).json({ message: 'Event tracked successfully' });
|
|
41
|
+
await client.log({
|
|
42
|
+
agent: "openclaw.main",
|
|
43
|
+
event: "task.duration_ms",
|
|
44
|
+
value: 420,
|
|
45
|
+
meta: { task_name: "email_triage", result: "success" },
|
|
71
46
|
});
|
|
72
47
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
});
|
|
48
|
+
await client.flush();
|
|
49
|
+
await client.shutdown();
|
|
76
50
|
```
|
|
51
|
+
<!-- docs:end quickstart -->
|
|
77
52
|
|
|
78
|
-
|
|
53
|
+
## API
|
|
79
54
|
|
|
80
|
-
|
|
81
|
-
// Initialize the SDK with your API key
|
|
82
|
-
initialize({ key: 'your-api-key', region: 'us-west' });
|
|
55
|
+
### `initialize(config)`
|
|
83
56
|
|
|
84
|
-
|
|
85
|
-
monitor({
|
|
86
|
-
group_id: '02e4f4d8-415e-4fc1-b01a-677ac5bc9207',
|
|
87
|
-
stream_name: 'My measurement',
|
|
88
|
-
value: 15.3,
|
|
89
|
-
});
|
|
90
|
-
```
|
|
57
|
+
Initializes the SDK singleton.
|
|
91
58
|
|
|
92
|
-
|
|
59
|
+
`config`:
|
|
60
|
+
- `key` (string, optional): API key. Must start with `chp_`.
|
|
61
|
+
- `apiEndpoint` (string, optional): Full ingestion endpoint override.
|
|
62
|
+
- `servicerEndpoint` (string, optional): Control-plane endpoint override. Defaults to `https://api.chirpier.co/v1.0`.
|
|
63
|
+
- `logLevel` (enum, optional): `None | Error | Info | Debug`.
|
|
64
|
+
- `retries` (number, optional): Retry attempts.
|
|
65
|
+
- `timeout` (number, optional): HTTP timeout in ms.
|
|
66
|
+
- `batchSize` (number, optional): Flush size threshold.
|
|
67
|
+
- `flushDelay` (number, optional): Flush interval in ms.
|
|
68
|
+
- `maxQueueSize` (number, optional, deprecated): Ignored; queues grow in memory until flushed.
|
|
93
69
|
|
|
94
|
-
|
|
70
|
+
API key resolution precedence (when `key` is omitted):
|
|
71
|
+
1. `CHIRPIER_API_KEY` from process environment
|
|
72
|
+
2. `CHIRPIER_API_KEY` from `.env` in current working directory
|
|
95
73
|
|
|
96
|
-
|
|
74
|
+
Default ingest endpoint is `https://logs.chirpier.co/v1.0/logs`.
|
|
75
|
+
Default servicer endpoint is `https://api.chirpier.co/v1.0`.
|
|
76
|
+
The same bearer token is used for both ingest and servicer APIs.
|
|
77
|
+
Queued logs are not dropped locally because of queue capacity or retry exhaustion.
|
|
97
78
|
|
|
98
|
-
|
|
99
|
-
initialize({ key: 'your-api-key', region: 'region' });
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
- `your-api-key` (str): Your Chirpier integration key
|
|
103
|
-
- `region` (str): Your local region - options are `us-west`, `eu-west`, `asia-southeast`
|
|
79
|
+
### `logEvent(log)`
|
|
104
80
|
|
|
105
|
-
|
|
81
|
+
Queues a log for batched delivery.
|
|
106
82
|
|
|
107
|
-
|
|
83
|
+
Example with `occurred_at`:
|
|
108
84
|
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
value:
|
|
114
|
-
|
|
85
|
+
```ts
|
|
86
|
+
await logEvent({
|
|
87
|
+
agent: "openclaw.main",
|
|
88
|
+
event: "tokens.used",
|
|
89
|
+
value: 1530,
|
|
90
|
+
occurred_at: "2026-03-05T14:30:00Z",
|
|
91
|
+
});
|
|
115
92
|
```
|
|
116
93
|
|
|
117
|
-
|
|
118
|
-
- `
|
|
119
|
-
- `
|
|
120
|
-
|
|
121
|
-
|
|
94
|
+
`log`:
|
|
95
|
+
- `agent` (string, optional): Free-form agent identifier text.
|
|
96
|
+
- `log_id` (string, optional): UUID idempotency key for the log. Generated automatically when omitted.
|
|
97
|
+
- `event` (string, required): Event name.
|
|
98
|
+
- `value` (number, required): Numeric value.
|
|
99
|
+
- `occurred_at` (string | Date, optional): Event occurrence timestamp.
|
|
100
|
+
- `meta` (JSON, optional): Additional JSON-encodable metadata.
|
|
101
|
+
|
|
102
|
+
Notes:
|
|
103
|
+
- `agent` whitespace-only values are treated as omitted.
|
|
104
|
+
- `log_id` blank values are treated as omitted and replaced with a generated UUIDv4.
|
|
105
|
+
- `event` must be non-empty after trimming.
|
|
106
|
+
- `occurred_at` must be within the last 30 days and no more than 1 day in the future.
|
|
107
|
+
- Use ISO8601 UTC timestamps, such as `2026-03-05T14:30:00Z`, or pass a `Date` instance.
|
|
108
|
+
- `meta` must be JSON-encodable.
|
|
109
|
+
- Unknown events are auto-created in Ingres as event definitions.
|
|
110
|
+
|
|
111
|
+
### `flush()`
|
|
112
|
+
|
|
113
|
+
Flushes pending logs for the initialized singleton without shutting down.
|
|
114
|
+
|
|
115
|
+
### `createClient(config)`
|
|
116
|
+
|
|
117
|
+
Creates a standalone `Client` instance (no global singleton state).
|
|
118
|
+
|
|
119
|
+
<!-- docs:start common-tasks -->
|
|
120
|
+
Client methods:
|
|
121
|
+
- `client.log(log)`: Queue a log.
|
|
122
|
+
- `client.flush()`: Flush queued logs.
|
|
123
|
+
- `client.shutdown()`: Flush and release timers/resources.
|
|
124
|
+
- `client.close()`: Alias of `client.shutdown()`.
|
|
125
|
+
- `client.listEvents()`: List event definitions using the servicer API.
|
|
126
|
+
- `client.getEvent(eventID)`: Read one event definition.
|
|
127
|
+
- `client.updateEvent(eventID, payload)`: Update event definition metadata.
|
|
128
|
+
- `client.listPolicies()`: List monitors/policies.
|
|
129
|
+
- `client.createPolicy(payload)`: Create a monitor/policy.
|
|
130
|
+
- `client.listAlerts(status?)`: List alerts, optionally filtered by status.
|
|
131
|
+
- `client.getAlertDeliveries(alertID, { kind, limit, offset })`: Read alert delivery attempts. Defaults to real alerts only; use `kind: "test"` or `kind: "all"` as needed.
|
|
132
|
+
- `client.acknowledgeAlert(alertID)`: Acknowledge an alert.
|
|
133
|
+
- `client.resolveAlert(alertID)`: Resolve an alert.
|
|
134
|
+
- `client.archiveAlert(alertID)`: Archive an alert.
|
|
135
|
+
- `client.testWebhook(webhookID)`: Send a connector test message.
|
|
136
|
+
- `client.getEventLogs(eventID, { period, limit, offset })`: Read minute/hour/day event rollups.
|
|
137
|
+
<!-- docs:end common-tasks -->
|
|
138
|
+
|
|
139
|
+
### OpenClaw Example
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
const client = createClient({ key: "chp_your_api_key" });
|
|
143
|
+
|
|
144
|
+
await client.log({
|
|
145
|
+
agent: "openclaw.main",
|
|
146
|
+
event: "tool.errors.count",
|
|
147
|
+
value: 1,
|
|
148
|
+
meta: { tool_name: "browser.open" },
|
|
149
|
+
});
|
|
122
150
|
|
|
123
|
-
|
|
151
|
+
await client.log({
|
|
152
|
+
agent: "openclaw.main",
|
|
153
|
+
event: "task.duration_ms",
|
|
154
|
+
value: 780,
|
|
155
|
+
meta: { task_name: "daily_digest" },
|
|
156
|
+
});
|
|
124
157
|
|
|
125
|
-
|
|
126
|
-
|
|
158
|
+
const events = await client.listEvents();
|
|
159
|
+
const toolErrors = events.find((eventDef) => eventDef.event === "tool.errors.count");
|
|
160
|
+
|
|
161
|
+
if (toolErrors) {
|
|
162
|
+
await client.createPolicy({
|
|
163
|
+
event_id: toolErrors.event_id,
|
|
164
|
+
title: "OpenClaw tool errors spike",
|
|
165
|
+
condition: "gt",
|
|
166
|
+
threshold: 5,
|
|
167
|
+
enabled: true,
|
|
168
|
+
channel: "default",
|
|
169
|
+
period: "hour",
|
|
170
|
+
aggregate: "sum",
|
|
171
|
+
severity: "warning",
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
await client.getEventLogs(toolErrors.event_id, { period: "hour", limit: 24 });
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
await client.shutdown();
|
|
127
178
|
```
|
|
128
179
|
|
|
129
|
-
|
|
180
|
+
### `stop()`
|
|
130
181
|
|
|
131
|
-
|
|
182
|
+
Flushes pending logs and stops the SDK singleton.
|
|
183
|
+
|
|
184
|
+
## Testing
|
|
132
185
|
|
|
133
186
|
```bash
|
|
134
|
-
|
|
187
|
+
npm test
|
|
135
188
|
```
|
|
136
189
|
|
|
137
|
-
##
|
|
138
|
-
|
|
139
|
-
We welcome contributions! To contribute:
|
|
190
|
+
## Connector Setup Examples
|
|
140
191
|
|
|
141
|
-
|
|
142
|
-
2. Create a new branch for your feature or bug fix.
|
|
143
|
-
3. Submit a pull request with a clear explanation of your changes.
|
|
192
|
+
Create a Slack connector for OpenClaw alerts:
|
|
144
193
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
194
|
+
```ts
|
|
195
|
+
await axios.post(
|
|
196
|
+
"https://api.chirpier.co/v1.0/webhooks",
|
|
197
|
+
{
|
|
198
|
+
url: "https://hooks.slack.com/services/T000/B000/secret",
|
|
199
|
+
type: "slack",
|
|
200
|
+
enabled: true,
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
headers: { Authorization: `Bearer ${process.env.CHIRPIER_API_KEY}` },
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
```
|
|
150
207
|
|
|
151
|
-
|
|
208
|
+
Create a Telegram connector for OpenClaw alerts:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
await axios.post(
|
|
212
|
+
"https://api.chirpier.co/v1.0/webhooks",
|
|
213
|
+
{
|
|
214
|
+
type: "telegram",
|
|
215
|
+
enabled: true,
|
|
216
|
+
credentials: {
|
|
217
|
+
bot_token: "123456:telegram-bot-token",
|
|
218
|
+
chat_id: "987654321",
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
headers: { Authorization: `Bearer ${process.env.CHIRPIER_API_KEY}` },
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
```
|
|
152
226
|
|
|
153
|
-
|
|
227
|
+
Send a test notification:
|
|
154
228
|
|
|
155
|
-
|
|
229
|
+
```ts
|
|
230
|
+
await client.testWebhook("whk_123");
|
|
231
|
+
```
|