@csaimonitor/sdk 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.
- package/CHANGELOG.md +34 -0
- package/LICENSE +22 -0
- package/README.md +315 -0
- package/dist/async-client.d.ts +143 -0
- package/dist/async-client.d.ts.map +1 -0
- package/dist/async-client.js +356 -0
- package/dist/async-client.js.map +1 -0
- package/dist/batcher.d.ts +50 -0
- package/dist/batcher.d.ts.map +1 -0
- package/dist/batcher.js +203 -0
- package/dist/batcher.js.map +1 -0
- package/dist/client.d.ts +178 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +248 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +72 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +115 -0
- package/dist/config.js.map +1 -0
- package/dist/decorators.d.ts +23 -0
- package/dist/decorators.d.ts.map +1 -0
- package/dist/decorators.js +203 -0
- package/dist/decorators.js.map +1 -0
- package/dist/exceptions.d.ts +57 -0
- package/dist/exceptions.d.ts.map +1 -0
- package/dist/exceptions.js +99 -0
- package/dist/exceptions.js.map +1 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +69 -0
- package/dist/index.js.map +1 -0
- package/dist/models.d.ts +59 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +84 -0
- package/dist/models.js.map +1 -0
- package/dist/utils.d.ts +53 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +176 -0
- package/dist/utils.js.map +1 -0
- package/package.json +52 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2026-01-01
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Initial release of @csaimonitor/sdk
|
|
13
|
+
- Synchronous `CSMonitor` client with decorator pattern support
|
|
14
|
+
- Asynchronous `AsyncCSMonitor` client with full async/await support
|
|
15
|
+
- Automatic event batching (default: 10 events per batch)
|
|
16
|
+
- Background flushing (default: every 5 seconds)
|
|
17
|
+
- Error handling with exponential backoff retry logic
|
|
18
|
+
- Data redaction for sensitive fields
|
|
19
|
+
- Context manager pattern for fine-grained control
|
|
20
|
+
- Manual event logging API
|
|
21
|
+
- Complete TypeScript type definitions
|
|
22
|
+
- Comprehensive documentation and examples
|
|
23
|
+
|
|
24
|
+
### Features
|
|
25
|
+
|
|
26
|
+
- Decorator pattern: `@monitor.track()` for automatic function tracking
|
|
27
|
+
- Context manager: `monitor.trackEvent()` for manual event control
|
|
28
|
+
- Manual tracking: `monitor.logEvent()` for direct event logging
|
|
29
|
+
- Auto-batching: Configurable batch size (1-100 events)
|
|
30
|
+
- Background flushing: Configurable flush interval
|
|
31
|
+
- Error handling: Never crashes user applications
|
|
32
|
+
- TypeScript: Full type support with strict mode
|
|
33
|
+
- Async support: Native async/await with AsyncCSMonitor
|
|
34
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Customer Support AI Monitor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
# @csaimonitor/sdk
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/%40csaimonitor%2Fsdk)
|
|
4
|
+
[](https://nodejs.org/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
Production-ready Node.js/TypeScript SDK for **Customer Support AI Monitor**. Track events, measure performance, analyze costs, and gain insights into your AI systems.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Zero Boilerplate**: One decorator and it works
|
|
12
|
+
- **Automatic Tracking**: Input, output, timing, and errors captured automatically
|
|
13
|
+
- **Batching**: 10x reduction in API calls with smart batching (default: 10 events per batch)
|
|
14
|
+
- **Background Flushing**: Automatic flush every 5 seconds
|
|
15
|
+
- **Async Support**: Full async/await compatibility with `AsyncCSMonitor`
|
|
16
|
+
- **Reliable**: Never crashes your application - all errors are caught and handled
|
|
17
|
+
- **Type Safe**: Complete TypeScript definitions for excellent IDE support
|
|
18
|
+
- **Flexible**: Decorator, context manager, or manual tracking patterns
|
|
19
|
+
|
|
20
|
+
## Quick Start (< 5 minutes)
|
|
21
|
+
|
|
22
|
+
### Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @csaimonitor/sdk
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Basic Usage
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { CSMonitor } from '@csaimonitor/sdk';
|
|
32
|
+
|
|
33
|
+
// Initialize monitor
|
|
34
|
+
const monitor = new CSMonitor({
|
|
35
|
+
apiKey: 'your_api_key_here_minimum_32_characters_long',
|
|
36
|
+
agentId: 'my_agent',
|
|
37
|
+
apiUrl: 'http://localhost:3002/api/v1'
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Track any function with a decorator
|
|
41
|
+
@monitor.track()
|
|
42
|
+
function chatWithUser(message: string): string {
|
|
43
|
+
return `AI response to: ${message}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Call your function normally
|
|
47
|
+
const result = chatWithUser('Hello!');
|
|
48
|
+
|
|
49
|
+
// Flush events before exit
|
|
50
|
+
monitor.flush();
|
|
51
|
+
monitor.close();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
That's it! Events are now flowing to your dashboard.
|
|
55
|
+
|
|
56
|
+
## Usage Patterns
|
|
57
|
+
|
|
58
|
+
### 1. Decorator Pattern (Recommended)
|
|
59
|
+
|
|
60
|
+
The simplest way to track functions:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
@monitor.track()
|
|
64
|
+
function myAgentFunction(inputData: string): string {
|
|
65
|
+
// Your AI logic here
|
|
66
|
+
return process(inputData);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
With custom event type and metadata:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
@monitor.track({
|
|
74
|
+
eventType: 'query',
|
|
75
|
+
metadata: { model: 'gpt-4', version: '1.0' }
|
|
76
|
+
})
|
|
77
|
+
function askQuestion(question: string): string {
|
|
78
|
+
return generateAnswer(question);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 2. Context Manager Pattern
|
|
83
|
+
|
|
84
|
+
For fine-grained control:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
const event = monitor.trackEvent('llm_call', { prompt: 'Hello' });
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const result = callLLM('Hello');
|
|
91
|
+
|
|
92
|
+
// Set output and metadata
|
|
93
|
+
event.setOutput({ response: result });
|
|
94
|
+
event.setCost(0.0042); // Track cost in USD
|
|
95
|
+
event.setMetadata({ model: 'gpt-4', tokens: 150 });
|
|
96
|
+
event.finish();
|
|
97
|
+
} catch (error) {
|
|
98
|
+
event.finish(error);
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 3. Manual Tracking
|
|
104
|
+
|
|
105
|
+
For legacy code or maximum control:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
monitor.logEvent({
|
|
109
|
+
eventType: 'decision',
|
|
110
|
+
inputData: { query: 'user input' },
|
|
111
|
+
outputData: { answer: 'AI response' },
|
|
112
|
+
metadata: { model: 'gpt-4' },
|
|
113
|
+
costUsd: 0.01,
|
|
114
|
+
latencyMs: 250,
|
|
115
|
+
status: 'success'
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 4. Async/Await Support
|
|
120
|
+
|
|
121
|
+
For async functions, use `AsyncCSMonitor`:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
import { AsyncCSMonitor } from '@csaimonitor/sdk';
|
|
125
|
+
|
|
126
|
+
const monitor = new AsyncCSMonitor({
|
|
127
|
+
apiKey: 'your_key',
|
|
128
|
+
agentId: 'async_agent',
|
|
129
|
+
apiUrl: 'http://localhost:3002/api/v1'
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
await monitor.start();
|
|
133
|
+
|
|
134
|
+
@monitor.track()
|
|
135
|
+
async function asyncFunction(data: string): Promise<string> {
|
|
136
|
+
await someAsyncOperation();
|
|
137
|
+
return `Processed: ${data}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const result = await asyncFunction('test');
|
|
141
|
+
await monitor.flush();
|
|
142
|
+
await monitor.stop();
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Configuration Options
|
|
146
|
+
|
|
147
|
+
| Option | Type | Required | Default | Description |
|
|
148
|
+
|--------|------|----------|---------|-------------|
|
|
149
|
+
| `apiKey` | `string` | Yes | - | API key for authentication (min 32 chars) |
|
|
150
|
+
| `agentId` | `string` | Yes | - | Identifier for the agent being monitored |
|
|
151
|
+
| `apiUrl` | `string` | No | Production URL | Base URL for the API |
|
|
152
|
+
| `batchSize` | `number` | No | `10` | Number of events to batch before sending (1-100) |
|
|
153
|
+
| `flushInterval` | `number` | No | `5.0` | Seconds between automatic flushes |
|
|
154
|
+
| `retryAttempts` | `number` | No | `3` | Maximum retry attempts for failed requests (0-10) |
|
|
155
|
+
| `timeout` | `number` | No | `30` | Request timeout in seconds |
|
|
156
|
+
| `debug` | `boolean` | No | `false` | Enable debug logging |
|
|
157
|
+
| `enabled` | `boolean` | No | `true` | Enable/disable tracking |
|
|
158
|
+
| `redactKeys` | `string[]` | No | `[]` | Array of keys to redact from event data |
|
|
159
|
+
| `onError` | `function` | No | - | Callback function for error handling |
|
|
160
|
+
|
|
161
|
+
### Example Configuration
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
const monitor = new CSMonitor({
|
|
165
|
+
apiKey: 'your_key_here',
|
|
166
|
+
agentId: 'my_agent',
|
|
167
|
+
apiUrl: 'http://localhost:3002/api/v1',
|
|
168
|
+
batchSize: 20,
|
|
169
|
+
flushInterval: 10.0,
|
|
170
|
+
retryAttempts: 5,
|
|
171
|
+
timeout: 60,
|
|
172
|
+
debug: true,
|
|
173
|
+
redactKeys: ['password', 'api_key', 'token'],
|
|
174
|
+
onError: (error) => {
|
|
175
|
+
console.error('CSMonitor error:', error);
|
|
176
|
+
// Log to your error tracking service
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Error Handling
|
|
182
|
+
|
|
183
|
+
The SDK is designed to never crash your application. All errors are caught and handled gracefully:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
const monitor = new CSMonitor({
|
|
187
|
+
apiKey: 'your_key',
|
|
188
|
+
agentId: 'my_agent',
|
|
189
|
+
onError: (error) => {
|
|
190
|
+
// Handle errors (log to Sentry, etc.)
|
|
191
|
+
console.error('Monitoring error:', error);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
// Even if the API is down, your app continues to work
|
|
196
|
+
@monitor.track()
|
|
197
|
+
function myFunction() {
|
|
198
|
+
return 'result';
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Data Redaction
|
|
203
|
+
|
|
204
|
+
Automatically redact sensitive data:
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
const monitor = new CSMonitor({
|
|
208
|
+
apiKey: 'your_key',
|
|
209
|
+
agentId: 'my_agent',
|
|
210
|
+
redactKeys: ['password', 'api_key', 'token', 'secret']
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
@monitor.track()
|
|
214
|
+
function login(username: string, password: string) {
|
|
215
|
+
// password will be redacted as [REDACTED] in events
|
|
216
|
+
return authenticate(username, password);
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Performance
|
|
221
|
+
|
|
222
|
+
- **Minimal Overhead**: < 1ms per tracked function call
|
|
223
|
+
- **Non-Blocking**: All API calls happen in the background
|
|
224
|
+
- **Efficient Batching**: 10x reduction in API calls
|
|
225
|
+
- **Automatic Flushing**: Events are sent automatically every 5 seconds
|
|
226
|
+
|
|
227
|
+
## API Reference
|
|
228
|
+
|
|
229
|
+
### CSMonitor
|
|
230
|
+
|
|
231
|
+
Main synchronous client for tracking events.
|
|
232
|
+
|
|
233
|
+
#### Methods
|
|
234
|
+
|
|
235
|
+
- `track(options?)`: Decorator for tracking functions
|
|
236
|
+
- `trackEvent(eventType, inputData?)`: Create a context manager for tracking
|
|
237
|
+
- `logEvent(options)`: Manually log an event
|
|
238
|
+
- `flush()`: Flush all pending events immediately (blocking)
|
|
239
|
+
- `close()`: Close the monitor and cleanup resources
|
|
240
|
+
|
|
241
|
+
### AsyncCSMonitor
|
|
242
|
+
|
|
243
|
+
Asynchronous client for tracking events with full async/await support.
|
|
244
|
+
|
|
245
|
+
#### Methods
|
|
246
|
+
|
|
247
|
+
- `start()`: Start the background flushing task
|
|
248
|
+
- `stop()`: Stop the background task and flush remaining events
|
|
249
|
+
- `track(options?)`: Decorator for tracking async functions
|
|
250
|
+
- `trackEvent(eventType, inputData?)`: Create an async context manager
|
|
251
|
+
- `logEvent(options)`: Manually log an event (async)
|
|
252
|
+
- `flush()`: Flush all pending events immediately (async)
|
|
253
|
+
|
|
254
|
+
## Examples
|
|
255
|
+
|
|
256
|
+
See the `examples/` directory for complete examples:
|
|
257
|
+
|
|
258
|
+
- `basic-usage.ts` - Simple decorator pattern
|
|
259
|
+
- `async-usage.ts` - Async/await examples
|
|
260
|
+
- `context-manager.ts` - Context manager pattern
|
|
261
|
+
- `manual-tracking.ts` - Direct event logging
|
|
262
|
+
- `error-handling.ts` - Error handling and redaction
|
|
263
|
+
|
|
264
|
+
## TypeScript Support
|
|
265
|
+
|
|
266
|
+
Full TypeScript support with complete type definitions:
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
import { CSMonitor, Event, EventMetadata, CSMonitorConfig } from '@csaimonitor/sdk';
|
|
270
|
+
|
|
271
|
+
const monitor: CSMonitor = new CSMonitor({
|
|
272
|
+
apiKey: 'your_key',
|
|
273
|
+
agentId: 'my_agent'
|
|
274
|
+
});
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Troubleshooting
|
|
278
|
+
|
|
279
|
+
### Events not appearing in dashboard
|
|
280
|
+
|
|
281
|
+
1. Check that `apiUrl` is correct
|
|
282
|
+
2. Verify `apiKey` is valid (min 32 characters)
|
|
283
|
+
3. Ensure `agentId` matches your agent
|
|
284
|
+
4. Call `monitor.flush()` before application exit
|
|
285
|
+
5. Enable `debug: true` to see detailed logs
|
|
286
|
+
|
|
287
|
+
### High memory usage
|
|
288
|
+
|
|
289
|
+
- Reduce `batchSize` if you have many events
|
|
290
|
+
- Reduce `flushInterval` to flush more frequently
|
|
291
|
+
- Call `monitor.flush()` periodically in long-running applications
|
|
292
|
+
|
|
293
|
+
### Network errors
|
|
294
|
+
|
|
295
|
+
- Increase `timeout` for slow networks
|
|
296
|
+
- Increase `retryAttempts` for unreliable connections
|
|
297
|
+
- Check `onError` callback for detailed error information
|
|
298
|
+
|
|
299
|
+
## Requirements
|
|
300
|
+
|
|
301
|
+
- Node.js 16.0.0 or higher
|
|
302
|
+
- TypeScript 5.0.0 or higher (for TypeScript projects)
|
|
303
|
+
|
|
304
|
+
## License
|
|
305
|
+
|
|
306
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
307
|
+
|
|
308
|
+
## Support
|
|
309
|
+
|
|
310
|
+
For issues, questions, or contributions, please visit our [GitHub repository](https://github.com/csaimonitor/sdk-nodejs).
|
|
311
|
+
|
|
312
|
+
## Changelog
|
|
313
|
+
|
|
314
|
+
See [CHANGELOG.md](CHANGELOG.md) for version history.
|
|
315
|
+
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asynchronous client for CSMonitor SDK.
|
|
3
|
+
*
|
|
4
|
+
* This module provides the AsyncCSMonitor class for async/await tracking.
|
|
5
|
+
*/
|
|
6
|
+
import { CSMonitorConfig, CSMonitorConfigOptions } from './config';
|
|
7
|
+
import { Event } from './models';
|
|
8
|
+
import { createTrackDecorator } from './decorators';
|
|
9
|
+
import { setupLogger } from './utils';
|
|
10
|
+
/**
|
|
11
|
+
* Async context manager for tracking events.
|
|
12
|
+
*/
|
|
13
|
+
export declare class AsyncTrackedEvent {
|
|
14
|
+
private readonly monitor;
|
|
15
|
+
readonly eventType: string;
|
|
16
|
+
private inputData?;
|
|
17
|
+
private outputData?;
|
|
18
|
+
private metadata;
|
|
19
|
+
private costUsd?;
|
|
20
|
+
private status;
|
|
21
|
+
private errorMessage?;
|
|
22
|
+
private startTime?;
|
|
23
|
+
private startTimestamp?;
|
|
24
|
+
constructor(monitor: AsyncCSMonitor, eventType: string, inputData?: Record<string, unknown>);
|
|
25
|
+
/**
|
|
26
|
+
* Set output data for the event.
|
|
27
|
+
*/
|
|
28
|
+
setOutput(outputData: unknown): void;
|
|
29
|
+
/**
|
|
30
|
+
* Set metadata fields.
|
|
31
|
+
*/
|
|
32
|
+
setMetadata(metadata: Record<string, unknown>): void;
|
|
33
|
+
/**
|
|
34
|
+
* Set cost in USD.
|
|
35
|
+
*/
|
|
36
|
+
setCost(costUsd: number): void;
|
|
37
|
+
/**
|
|
38
|
+
* Set event status.
|
|
39
|
+
*/
|
|
40
|
+
setStatus(status: string): void;
|
|
41
|
+
/**
|
|
42
|
+
* Set error message and status.
|
|
43
|
+
*/
|
|
44
|
+
setError(errorMessage: string): void;
|
|
45
|
+
/**
|
|
46
|
+
* Enter async context manager.
|
|
47
|
+
*/
|
|
48
|
+
start(): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Exit async context manager and log event.
|
|
51
|
+
*/
|
|
52
|
+
finish(error?: unknown): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Asynchronous client for tracking agent events.
|
|
56
|
+
*
|
|
57
|
+
* This class provides async/await support for event tracking.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const monitor = new AsyncCSMonitor({
|
|
62
|
+
* apiKey: 'your_key',
|
|
63
|
+
* agentId: 'my_agent',
|
|
64
|
+
* apiUrl: 'http://localhost:3002/api/v1'
|
|
65
|
+
* });
|
|
66
|
+
*
|
|
67
|
+
* @monitor.track()
|
|
68
|
+
* async function myAsyncFunction(x: number): Promise<number> {
|
|
69
|
+
* await someAsyncOperation();
|
|
70
|
+
* return x * 2;
|
|
71
|
+
* }
|
|
72
|
+
*
|
|
73
|
+
* const result = await myAsyncFunction(5);
|
|
74
|
+
* await monitor.flush();
|
|
75
|
+
* await monitor.stop();
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare class AsyncCSMonitor {
|
|
79
|
+
readonly config: CSMonitorConfig;
|
|
80
|
+
readonly logger: ReturnType<typeof setupLogger>;
|
|
81
|
+
private readonly eventQueue;
|
|
82
|
+
private flushTimer?;
|
|
83
|
+
private readonly onError?;
|
|
84
|
+
private started;
|
|
85
|
+
constructor(options: CSMonitorConfigOptions);
|
|
86
|
+
/**
|
|
87
|
+
* Start the background flushing task.
|
|
88
|
+
*/
|
|
89
|
+
start(): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Stop the background task and flush remaining events.
|
|
92
|
+
*/
|
|
93
|
+
stop(): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Decorator to automatically track async function execution.
|
|
96
|
+
* @param options - Tracking options
|
|
97
|
+
* @returns Decorator function
|
|
98
|
+
*/
|
|
99
|
+
track(options?: Parameters<ReturnType<typeof createTrackDecorator>>[0]): <T extends (...args: unknown[]) => unknown>(target: T, _propertyKey?: string | symbol, descriptor?: PropertyDescriptor) => T | PropertyDescriptor;
|
|
100
|
+
/**
|
|
101
|
+
* Create an async context manager for tracking an event.
|
|
102
|
+
* @param eventType - Type of event
|
|
103
|
+
* @param inputData - Initial input data
|
|
104
|
+
* @returns AsyncTrackedEvent context manager
|
|
105
|
+
*/
|
|
106
|
+
trackEvent(eventType: string, inputData?: Record<string, unknown>): AsyncTrackedEvent;
|
|
107
|
+
/**
|
|
108
|
+
* Manually log an event (async).
|
|
109
|
+
* @param options - Event data
|
|
110
|
+
*/
|
|
111
|
+
logEvent(options: {
|
|
112
|
+
eventType: string;
|
|
113
|
+
inputData?: Record<string, unknown>;
|
|
114
|
+
outputData?: Record<string, unknown>;
|
|
115
|
+
metadata?: Record<string, unknown>;
|
|
116
|
+
costUsd?: number;
|
|
117
|
+
latencyMs?: number;
|
|
118
|
+
status?: string;
|
|
119
|
+
errorMessage?: string;
|
|
120
|
+
timestamp?: string;
|
|
121
|
+
}): Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Add event to async queue (internal method).
|
|
124
|
+
*/
|
|
125
|
+
_addEventAsync(event: Event): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Flush all pending events immediately (async).
|
|
128
|
+
*/
|
|
129
|
+
flush(): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Send a batch of events to the API with retry logic (async).
|
|
132
|
+
*/
|
|
133
|
+
private sendBatch;
|
|
134
|
+
/**
|
|
135
|
+
* Sleep for a given number of seconds.
|
|
136
|
+
*/
|
|
137
|
+
private sleep;
|
|
138
|
+
/**
|
|
139
|
+
* String representation.
|
|
140
|
+
*/
|
|
141
|
+
toString(): string;
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=async-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-client.d.ts","sourceRoot":"","sources":["../src/async-client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AACnE,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAKL,WAAW,EACZ,MAAM,SAAS,CAAC;AAGjB;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,SAAgB,SAAS,EAAE,MAAM,CAAC;IAClC,OAAO,CAAC,SAAS,CAAC,CAA0B;IAC5C,OAAO,CAAC,UAAU,CAAC,CAA0B;IAC7C,OAAO,C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