@chirpier/chirpier-js 0.1.6 → 0.2.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.
package/README.md CHANGED
@@ -1,155 +1,227 @@
1
1
  # Chirpier SDK
2
2
 
3
- The Chirpier SDK for JavaScript is a simple, lightweight, and efficient SDK to emit event data to Chirpier direct from your JavaScript applications.
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
- Install Chirpier SDK using npm:
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
- ## Getting Started
13
+ ## Quick Start
23
14
 
24
- ### Initializing the SDK
15
+ <!-- docs:start quickstart -->
16
+ ### Singleton API
25
17
 
26
- To start using the SDK, you need to initialize it with your API key. The SDK works in both browser and Node.js environments.
18
+ ```ts
19
+ import { initialize, logEvent, stop } from "@chirpier/chirpier-js";
27
20
 
28
- Here's a quick example of how to use the Chirpier SDK:
21
+ initialize({ key: "chp_your_api_key" });
29
22
 
30
- #### In a Browser
31
-
32
- ```js
33
- import { initialize, monitor, Event } from '@chirpier/chirpier-js';
34
-
35
- // Initialize the SDK with your API key
36
- initialize({ key: 'your-api-key' });
23
+ await logEvent({
24
+ agent_id: "openclaw.main",
25
+ event: "tool.errors.count",
26
+ value: 1,
27
+ meta: { tool_name: "browser.open", workflow: "triage" },
28
+ });
37
29
 
38
- // Send a data stream tied to a group of streams
39
- monitor({
40
- group_id: '02e4f4d8-415e-4fc1-b01a-677ac5bc9207',
41
- stream_name: 'My measurement',
42
- value: 15.30,
43
- } as Event);
30
+ await stop();
44
31
  ```
45
32
 
46
- #### In a Server (e.g., Express.js)
47
-
48
- ```js
49
- const express = require('express');
50
- const { initialize, monitor, Event } = require('@chirpier/chirpier-js');
33
+ ### Instance API (Recommended)
51
34
 
52
- const app = express();
53
- const port = 3000;
35
+ ```ts
36
+ import { createClient } from "@chirpier/chirpier-js";
54
37
 
55
- // Initialize the SDK with your API key
56
- initialize({ key: 'your-api-key', region: 'us-west' });
38
+ const client = createClient({ key: "chp_your_api_key" });
57
39
 
58
- app.use(express.json());
59
-
60
- app.post('/monitor', (req, res) => {
61
- const { group_id, stream_name, value } = req.body;
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' });
40
+ await client.log({
41
+ agent_id: "openclaw.main",
42
+ event: "task.duration_ms",
43
+ value: 420,
44
+ meta: { task_name: "email_triage", result: "success" },
71
45
  });
72
46
 
73
- app.listen(port, () => {
74
- console.log(`Server is running on http://localhost:${port}`);
75
- });
47
+ await client.flush();
48
+ await client.shutdown();
76
49
  ```
50
+ <!-- docs:end quickstart -->
77
51
 
78
- ### Usage
52
+ ## API
79
53
 
80
- ```js
81
- // Initialize the SDK with your API key
82
- initialize({ key: 'your-api-key', region: 'us-west' });
54
+ ### `initialize(config)`
83
55
 
84
- // Monitor an event
85
- monitor({
86
- group_id: '02e4f4d8-415e-4fc1-b01a-677ac5bc9207',
87
- stream_name: 'My measurement',
88
- value: 15.3,
89
- });
90
- ```
56
+ Initializes the SDK singleton.
91
57
 
92
- ## API Reference
58
+ `config`:
59
+ - `key` (string, optional): API key. Must start with `chp_`.
60
+ - `apiEndpoint` (string, optional): Full ingestion endpoint override.
61
+ - `servicerEndpoint` (string, optional): Control-plane endpoint override. Defaults to `https://api.chirpier.co/v1.0`.
62
+ - `logLevel` (enum, optional): `None | Error | Info | Debug`.
63
+ - `retries` (number, optional): Retry attempts.
64
+ - `timeout` (number, optional): HTTP timeout in ms.
65
+ - `batchSize` (number, optional): Flush size threshold.
66
+ - `flushDelay` (number, optional): Flush interval in ms.
67
+ - `maxQueueSize` (number, optional): In-memory queue capacity.
93
68
 
94
- ### Initialize
69
+ API key resolution precedence (when `key` is omitted):
70
+ 1. `CHIRPIER_API_KEY` from process environment
71
+ 2. `CHIRPIER_API_KEY` from `.env` in current working directory
95
72
 
96
- Initialize the Chirpier client with your API key and region. Find your API key in the Chirpier Integration page.
73
+ Default ingest endpoint is `https://logs.chirpier.co/v1.0/logs`.
74
+ Default servicer endpoint is `https://api.chirpier.co/v1.0`.
75
+ The same bearer token is used for both ingest and servicer APIs.
97
76
 
98
- ```js
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`
77
+ ### `logEvent(log)`
104
78
 
105
- ### Event
79
+ Queues a log for batched delivery.
106
80
 
107
- All events emitted to Chirpier must have the following properties:
81
+ Example with `occurred_at`:
108
82
 
109
- ```js
110
- event = {
111
- group_id: '02e4f4d8-415e-4fc1-b01a-677ac5bc9207',
112
- stream_name: 'My measurement',
113
- value: 15.3,
114
- };
83
+ ```ts
84
+ await logEvent({
85
+ agent_id: "openclaw.main",
86
+ event: "tokens.used",
87
+ value: 1530,
88
+ occurred_at: "2026-03-05T14:30:00Z",
89
+ });
115
90
  ```
116
91
 
117
- - `group_id` (str): UUID of the monitoring group
118
- - `stream_name` (str): Name of the measurement stream
119
- - `value` (float): Numeric value to record
120
-
121
- ### Monitor
92
+ `log`:
93
+ - `agent_id` (string, optional): Free-form agent identifier text.
94
+ - `event` (string, required): Event name.
95
+ - `value` (number, required): Numeric value.
96
+ - `occurred_at` (string | Date, optional): Event occurrence timestamp.
97
+ - `meta` (JSON, optional): Additional JSON-encodable metadata.
98
+
99
+ Notes:
100
+ - `agent_id` whitespace-only values are treated as omitted.
101
+ - `event` must be non-empty after trimming.
102
+ - `occurred_at` must be within the last 30 days and no more than 1 day in the future.
103
+ - Use ISO8601 UTC timestamps, such as `2026-03-05T14:30:00Z`, or pass a `Date` instance.
104
+ - `meta` must be JSON-encodable.
105
+ - Unknown events are auto-created in Ingres as event definitions.
106
+
107
+ ### `flush()`
108
+
109
+ Flushes pending logs for the initialized singleton without shutting down.
110
+
111
+ ### `createClient(config)`
112
+
113
+ Creates a standalone `Client` instance (no global singleton state).
114
+
115
+ <!-- docs:start common-tasks -->
116
+ Client methods:
117
+ - `client.log(log)`: Queue a log.
118
+ - `client.flush()`: Flush queued logs.
119
+ - `client.shutdown()`: Flush and release timers/resources.
120
+ - `client.close()`: Alias of `client.shutdown()`.
121
+ - `client.listEvents()`: List event definitions using the servicer API.
122
+ - `client.getEvent(eventID)`: Read one event definition.
123
+ - `client.updateEvent(eventID, payload)`: Update event definition metadata.
124
+ - `client.listPolicies()`: List monitors/policies.
125
+ - `client.createPolicy(payload)`: Create a monitor/policy.
126
+ - `client.listAlerts(status?)`: List alerts, optionally filtered by status.
127
+ - `client.getAlertDeliveries(alertID, { kind, limit, offset })`: Read alert delivery attempts. Defaults to real alerts only; use `kind: "test"` or `kind: "all"` as needed.
128
+ - `client.acknowledgeAlert(alertID)`: Acknowledge an alert.
129
+ - `client.resolveAlert(alertID)`: Resolve an alert.
130
+ - `client.archiveAlert(alertID)`: Archive an alert.
131
+ - `client.testWebhook(webhookID)`: Send a connector test message.
132
+ - `client.getEventLogs(eventID, { period, limit, offset })`: Read minute/hour/day event rollups.
133
+ <!-- docs:end common-tasks -->
134
+
135
+ ### OpenClaw Example
136
+
137
+ ```ts
138
+ const client = createClient({ key: "chp_your_api_key" });
139
+
140
+ await client.log({
141
+ agent_id: "openclaw.main",
142
+ event: "tool.errors.count",
143
+ value: 1,
144
+ meta: { tool_name: "browser.open" },
145
+ });
122
146
 
123
- Send an event to Chirpier using the `monitor` function.
147
+ await client.log({
148
+ agent_id: "openclaw.main",
149
+ event: "task.duration_ms",
150
+ value: 780,
151
+ meta: { task_name: "daily_digest" },
152
+ });
124
153
 
125
- ```js
126
- monitor(event);
154
+ const events = await client.listEvents();
155
+ const toolErrors = events.find((eventDef) => eventDef.event === "tool.errors.count");
156
+
157
+ if (toolErrors) {
158
+ await client.createPolicy({
159
+ event_id: toolErrors.event_id,
160
+ title: "OpenClaw tool errors spike",
161
+ condition: "gt",
162
+ threshold: 5,
163
+ enabled: true,
164
+ channel: "default",
165
+ period: "hour",
166
+ aggregate: "sum",
167
+ severity: "warning",
168
+ });
169
+
170
+ await client.getEventLogs(toolErrors.event_id, { period: "hour", limit: 24 });
171
+ }
172
+
173
+ await client.shutdown();
127
174
  ```
128
175
 
129
- ## Test
176
+ ### `stop()`
130
177
 
131
- Run the test suite to ensure everything works as expected:
178
+ Flushes pending logs and stops the SDK singleton.
179
+
180
+ ## Testing
132
181
 
133
182
  ```bash
134
- npx jest
183
+ npm test
135
184
  ```
136
185
 
137
- ## Contributing
138
-
139
- We welcome contributions! To contribute:
186
+ ## Connector Setup Examples
140
187
 
141
- 1. Fork this repository.
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.
188
+ Create a Slack connector for OpenClaw alerts:
144
189
 
145
- ## License
146
-
147
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
148
-
149
- ## Support
190
+ ```ts
191
+ await axios.post(
192
+ "https://api.chirpier.co/v1.0/webhooks",
193
+ {
194
+ url: "https://hooks.slack.com/services/T000/B000/secret",
195
+ type: "slack",
196
+ enabled: true,
197
+ },
198
+ {
199
+ headers: { Authorization: `Bearer ${process.env.CHIRPIER_API_KEY}` },
200
+ }
201
+ );
202
+ ```
150
203
 
151
- If you encounter any problems or have any questions, please open an issue on the GitHub repository or contact us at <contact@chirpier.co>.
204
+ Create a Telegram connector for OpenClaw alerts:
205
+
206
+ ```ts
207
+ await axios.post(
208
+ "https://api.chirpier.co/v1.0/webhooks",
209
+ {
210
+ type: "telegram",
211
+ enabled: true,
212
+ credentials: {
213
+ bot_token: "123456:telegram-bot-token",
214
+ chat_id: "987654321",
215
+ },
216
+ },
217
+ {
218
+ headers: { Authorization: `Bearer ${process.env.CHIRPIER_API_KEY}` },
219
+ }
220
+ );
221
+ ```
152
222
 
153
- ---
223
+ Send a test notification:
154
224
 
155
- Start tracking your events seamlessly with Chirpier SDK!
225
+ ```ts
226
+ await client.testWebhook("whk_123");
227
+ ```