@observyze/sdk 0.1.0 → 0.1.2

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,405 @@
1
+ /**
2
+ * SDK Configuration and Types
3
+ */
4
+ /**
5
+ * Type of span operation
6
+ */
7
+ declare enum SpanType {
8
+ LLM = "llm",
9
+ TOOL = "tool",
10
+ AGENT = "agent",
11
+ CHAIN = "chain",
12
+ RETRIEVAL = "retrieval"
13
+ }
14
+ /**
15
+ * Status of trace execution
16
+ */
17
+ declare enum TraceStatus {
18
+ SUCCESS = "success",
19
+ ERROR = "error",
20
+ TIMEOUT = "timeout",
21
+ RUNNING = "running"
22
+ }
23
+ /**
24
+ * Error information for a span
25
+ */
26
+ interface SpanError {
27
+ message: string;
28
+ stack?: string;
29
+ code?: string;
30
+ }
31
+ /**
32
+ * Token usage information
33
+ */
34
+ interface TokenUsage {
35
+ input: number;
36
+ output: number;
37
+ total: number;
38
+ computed_pixels?: number;
39
+ audio_seconds?: number;
40
+ }
41
+ /**
42
+ * Individual operation within a trace
43
+ */
44
+ interface Span$1 {
45
+ span_id: string;
46
+ parent_span_id?: string;
47
+ name: string;
48
+ type: SpanType;
49
+ start_time: Date;
50
+ end_time: Date;
51
+ duration_ms: number;
52
+ input: any;
53
+ output: any;
54
+ error?: SpanError;
55
+ metadata: {
56
+ model?: string;
57
+ provider?: string;
58
+ temperature?: number;
59
+ max_tokens?: number;
60
+ [key: string]: any;
61
+ };
62
+ tokens?: TokenUsage;
63
+ }
64
+ /**
65
+ * Complete record of an AI workflow execution
66
+ */
67
+ interface Trace$1 {
68
+ trace_id: string;
69
+ organization_id: string;
70
+ project_id?: string;
71
+ name: string;
72
+ status: TraceStatus;
73
+ start_time: Date;
74
+ end_time: Date;
75
+ duration_ms: number;
76
+ metadata: Record<string, any>;
77
+ spans: Span$1[];
78
+ tags: string[];
79
+ user_id?: string;
80
+ session_id?: string;
81
+ cost?: number;
82
+ cost_breakdown?: {
83
+ tokens: number;
84
+ pixels: number;
85
+ audio: number;
86
+ };
87
+ created_at: Date;
88
+ updated_at: Date;
89
+ }
90
+ /**
91
+ * Configuration for Observyze SDK client
92
+ */
93
+ interface ClientConfig {
94
+ /**
95
+ * API key for authentication with Observyze Ingestion Service
96
+ */
97
+ apiKey: string;
98
+ /**
99
+ * Endpoint URL for the Ingestion Service
100
+ * @default 'https://api.observyze.com'
101
+ */
102
+ endpoint?: string;
103
+ /**
104
+ * Maximum number of traces to buffer before flushing
105
+ * @default 100
106
+ */
107
+ batchSize?: number;
108
+ /**
109
+ * Time in milliseconds to wait before auto-flushing buffered traces
110
+ * @default 5000
111
+ */
112
+ flushInterval?: number;
113
+ /**
114
+ * Enable automatic instrumentation of popular LLM libraries
115
+ * @default true
116
+ */
117
+ enableAutoInstrumentation?: boolean;
118
+ /**
119
+ * Organization ID (optional, can be extracted from API key)
120
+ */
121
+ organizationId?: string;
122
+ /**
123
+ * Project ID for trace attribution
124
+ */
125
+ projectId?: string;
126
+ /**
127
+ * Enable debug logging
128
+ * @default false
129
+ */
130
+ debug?: boolean;
131
+ /**
132
+ * Dry run mode - don't send traces to server (useful for testing)
133
+ * @default false
134
+ */
135
+ dryRun?: boolean;
136
+ /**
137
+ * Automatically scrub PII from trace output before network transmission
138
+ * @default true
139
+ */
140
+ enablePiiRedaction?: boolean;
141
+ }
142
+ /**
143
+ * Internal configuration with defaults applied
144
+ */
145
+ interface ResolvedClientConfig extends Required<ClientConfig> {
146
+ }
147
+
148
+ /**
149
+ * Trace and Span classes for capturing AI workflow execution
150
+ */
151
+
152
+ /**
153
+ * Represents an individual operation within a trace
154
+ */
155
+ declare class Span {
156
+ private data;
157
+ private startTime;
158
+ constructor(name: string, type: SpanType, parentSpanId?: string);
159
+ /**
160
+ * Set the input data for this span
161
+ */
162
+ setInput(input: any): this;
163
+ /**
164
+ * Set the output data for this span
165
+ */
166
+ setOutput(output: any): this;
167
+ /**
168
+ * Record an error that occurred during span execution
169
+ */
170
+ setError(error: Error): this;
171
+ /**
172
+ * Set metadata for this span
173
+ */
174
+ setMetadata(key: string, value: any): this;
175
+ /**
176
+ * Set multiple metadata fields at once
177
+ */
178
+ setMetadataAll(metadata: Record<string, any>): this;
179
+ /**
180
+ * Set token usage information
181
+ */
182
+ setTokens(tokens: TokenUsage): this;
183
+ /**
184
+ * End the span and calculate duration
185
+ */
186
+ end(): void;
187
+ /**
188
+ * Get the span ID
189
+ */
190
+ get id(): string;
191
+ /**
192
+ * Get the span data for serialization
193
+ */
194
+ toJSON(): Span$1;
195
+ }
196
+ /**
197
+ * Represents a complete AI workflow execution
198
+ */
199
+ declare class Trace {
200
+ private data;
201
+ private startTime;
202
+ private spans;
203
+ private ended;
204
+ constructor(name: string, organizationId: string, projectId?: string);
205
+ /**
206
+ * Start a new span within this trace
207
+ */
208
+ startSpan(name: string, type: SpanType, parentSpanId?: string): Span;
209
+ /**
210
+ * Add metadata to the trace
211
+ */
212
+ setMetadata(key: string, value: any): this;
213
+ /**
214
+ * Set multiple metadata fields at once
215
+ */
216
+ setMetadataAll(metadata: Record<string, any>): this;
217
+ /**
218
+ * Add tags to the trace
219
+ */
220
+ addTag(tag: string): this;
221
+ /**
222
+ * Add multiple tags at once
223
+ */
224
+ addTags(tags: string[]): this;
225
+ /**
226
+ * Set the user ID associated with this trace
227
+ */
228
+ setUserId(userId: string): this;
229
+ /**
230
+ * Set the session ID associated with this trace
231
+ */
232
+ setSessionId(sessionId: string): this;
233
+ /**
234
+ * End the trace with a final status
235
+ */
236
+ end(status?: TraceStatus): void;
237
+ /**
238
+ * Get the trace ID
239
+ */
240
+ get id(): string;
241
+ /**
242
+ * Check if the trace has ended
243
+ */
244
+ get isEnded(): boolean;
245
+ /**
246
+ * Get the trace data for serialization
247
+ */
248
+ toJSON(): Omit<Trace$1, '_id' | 'created_at' | 'updated_at'>;
249
+ }
250
+
251
+ /**
252
+ * Observyze SDK Client
253
+ * Main entry point for instrumenting AI applications
254
+ */
255
+
256
+ /**
257
+ * Main SDK client for Observyze
258
+ */
259
+ declare class ObservyzeClient {
260
+ private config;
261
+ private traceBuffer;
262
+ private flushTimer;
263
+ private isShuttingDown;
264
+ private readonly MAX_QUEUE_SIZE;
265
+ private readonly RETRY_DELAYS;
266
+ constructor(config: ClientConfig);
267
+ /**
268
+ * Start a new trace
269
+ */
270
+ startTrace(name: string, metadata?: Record<string, any>): Trace;
271
+ /**
272
+ * Buffer a completed trace for batch sending
273
+ */
274
+ private bufferTrace;
275
+ /**
276
+ * Start the auto-flush timer
277
+ */
278
+ private startFlushTimer;
279
+ /**
280
+ * Flush all buffered traces to the Ingestion Service
281
+ */
282
+ flush(): Promise<void>;
283
+ /**
284
+ * Send traces with exponential backoff retry
285
+ */
286
+ private sendWithRetry;
287
+ /**
288
+ * Shutdown the SDK and flush remaining traces
289
+ */
290
+ shutdown(): Promise<void>;
291
+ /**
292
+ * Get current buffer size
293
+ */
294
+ get bufferSize(): number;
295
+ /**
296
+ * Get SDK configuration
297
+ */
298
+ getConfig(): Readonly<ResolvedClientConfig>;
299
+ /**
300
+ * Wrap an LLM client (OpenAI, Anthropic) to enable auto-instrumentation
301
+ *
302
+ * @example
303
+ * ```typescript
304
+ * import OpenAI from 'openai'
305
+ * import { ObservyzeClient } from '@observyze/sdk'
306
+ *
307
+ * const nw = new ObservyzeClient({ apiKey: 'your-api-key' })
308
+ * const openai = new OpenAI({ apiKey: 'openai-key' })
309
+ *
310
+ * // Wrap the client to enable auto-instrumentation
311
+ * nw.wrap(openai)
312
+ *
313
+ * // All calls are now automatically traced
314
+ * const response = await openai.chat.completions.create({
315
+ * model: 'gpt-4',
316
+ * messages: [{ role: 'user', content: 'Hello!' }]
317
+ * })
318
+ * ```
319
+ */
320
+ wrap<T>(client: T): T;
321
+ /**
322
+ * Sync local agent .history file to Observyze cloud
323
+ * Parses JSON/NDJSON agent history and sends to ingestion endpoint.
324
+ */
325
+ syncLocalHistory(filePath: string): Promise<void>;
326
+ /**
327
+ * Industry-grade PII Redaction (Compliance & RBAC)
328
+ *
329
+ * Recursively scrubs PII from trace data before transmission to the cloud.
330
+ * Coverage: emails, JWTs, bearer tokens, AWS/API keys, SSNs, credit cards,
331
+ * phone numbers (US + E.164), IPv4/IPv6, passport numbers, ZIP codes, plus
332
+ * key-based redaction for sensitive JSON fields (password, token, api_key, etc.).
333
+ *
334
+ * Design:
335
+ * - Pure function, never mutates the original object
336
+ * - Depth-limited to 16 levels to prevent stack overflow on deep agent outputs
337
+ * - Key-aware: sensitive key names are fully redacted regardless of value format
338
+ */
339
+ private static readonly PII_PATTERNS;
340
+ private static readonly SENSITIVE_KEYS;
341
+ private static isSensitiveKey;
342
+ private sanitizePII;
343
+ }
344
+
345
+ /**
346
+ * Observyze OpenTelemetry Span Exporter
347
+ *
348
+ * Implements `@opentelemetry/sdk-trace-base`'s `SpanExporter` interface
349
+ * so users can use the standard OTel Node.js SDK and export spans to
350
+ * the Observyze platform.
351
+ *
352
+ * Usage:
353
+ * ```typescript
354
+ * import { ObservyzeClient } from '@observyze/sdk'
355
+ * import { ObservyzeSpanExporter } from '@observyze/sdk/opentelemetry'
356
+ * import { NodeSDK } from '@opentelemetry/sdk-node'
357
+ * import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
358
+ *
359
+ * const nw = new ObservyzeClient({ apiKey: 'nw_...' })
360
+ *
361
+ * const sdk = new NodeSDK({
362
+ * spanProcessor: new BatchSpanProcessor(
363
+ * new ObservyzeSpanExporter(nw, { serviceName: 'my-app' })
364
+ * )
365
+ * })
366
+ * sdk.start()
367
+ * ```
368
+ */
369
+
370
+ /**
371
+ * Exporter configuration
372
+ */
373
+ interface ObservyzeExporterConfig {
374
+ serviceName?: string;
375
+ projectId?: string;
376
+ defaultSpanType?: SpanType | 'llm' | 'tool' | 'agent' | 'chain' | 'retrieval';
377
+ headers?: Record<string, string>;
378
+ }
379
+ /**
380
+ * Observyze OpenTelemetry Span Exporter
381
+ */
382
+ declare class ObservyzeSpanExporter {
383
+ private client;
384
+ private config;
385
+ constructor(client: ObservyzeClient, config?: ObservyzeExporterConfig);
386
+ /**
387
+ * Export spans — called by OTel SDK when spans are ready.
388
+ * Converts OTel spans to Observyze traces and buffers them.
389
+ */
390
+ export(spans: any[], resultCallback: (result: {
391
+ code: number;
392
+ error?: Error;
393
+ }) => void): Promise<void>;
394
+ /**
395
+ * Called when the exporter is shut down.
396
+ * Flushes any remaining buffered traces via the SDK client.
397
+ */
398
+ shutdown(): Promise<void>;
399
+ /**
400
+ * Called by the OTel SDK to force-export buffered spans.
401
+ */
402
+ forceFlush(): Promise<void>;
403
+ }
404
+
405
+ export { type ClientConfig as C, ObservyzeClient as O, type ResolvedClientConfig as R, Span as S, type TokenUsage as T, type ObservyzeExporterConfig as a, ObservyzeSpanExporter as b, type Span$1 as c, type SpanError as d, SpanType as e, Trace as f, type Trace$1 as g, TraceStatus as h };