@raindrop-ai/pi-agent 0.0.3 → 0.0.5

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,369 @@
1
+ type OtlpAnyValue = {
2
+ stringValue?: string;
3
+ intValue?: string;
4
+ doubleValue?: number;
5
+ boolValue?: boolean;
6
+ arrayValue?: {
7
+ values: OtlpAnyValue[];
8
+ };
9
+ };
10
+ type OtlpKeyValue = {
11
+ key: string;
12
+ value: OtlpAnyValue;
13
+ };
14
+ declare const SpanStatusCode: {
15
+ readonly UNSET: 0;
16
+ readonly OK: 1;
17
+ readonly ERROR: 2;
18
+ };
19
+ type OtlpSpanStatus = {
20
+ code: (typeof SpanStatusCode)[keyof typeof SpanStatusCode] | number;
21
+ message?: string;
22
+ };
23
+ type OtlpSpan = {
24
+ traceId: string;
25
+ spanId: string;
26
+ parentSpanId?: string;
27
+ name: string;
28
+ startTimeUnixNano: string;
29
+ endTimeUnixNano: string;
30
+ attributes?: OtlpKeyValue[];
31
+ status?: OtlpSpanStatus;
32
+ };
33
+ type SpanIds = {
34
+ traceIdB64: string;
35
+ spanIdB64: string;
36
+ parentSpanIdB64?: string;
37
+ };
38
+ type Attachment = {
39
+ type: string;
40
+ role: string;
41
+ name?: string;
42
+ value: string;
43
+ };
44
+ type IdentifyInput = {
45
+ userId: string;
46
+ traits?: Record<string, unknown>;
47
+ };
48
+ type Patch = {
49
+ eventName?: string;
50
+ userId?: string;
51
+ convoId?: string;
52
+ input?: string;
53
+ output?: string;
54
+ model?: string;
55
+ properties?: Record<string, unknown>;
56
+ attachments?: Attachment[];
57
+ isPending?: boolean;
58
+ timestamp?: string;
59
+ };
60
+ type SignalInput = {
61
+ eventId: string;
62
+ name: string;
63
+ type?: "default" | "feedback" | "edit" | "standard" | "agent" | "agent_internal";
64
+ sentiment?: "POSITIVE" | "NEGATIVE";
65
+ timestamp?: string;
66
+ properties?: Record<string, unknown>;
67
+ attachmentId?: string;
68
+ comment?: string;
69
+ after?: string;
70
+ };
71
+ type EventShipperOptions = {
72
+ writeKey?: string;
73
+ endpoint?: string;
74
+ enabled?: boolean;
75
+ debug: boolean;
76
+ partialFlushMs?: number;
77
+ sdkName?: string;
78
+ libraryName?: string;
79
+ libraryVersion?: string;
80
+ defaultEventName?: string;
81
+ /**
82
+ * Explicit Workshop / local debugger URL. Wins over env vars + auto-detect.
83
+ * Pass `null` to opt out of all mirroring (including auto-detect).
84
+ */
85
+ localDebuggerUrl?: string | null;
86
+ /**
87
+ * Per-field character cap applied to event input/output BEFORE buffering
88
+ * or serialization, so oversized payloads cost the cap — not the payload —
89
+ * on the calling code path. Truncated fields end with
90
+ * `...[truncated by raindrop]` and never exceed the cap, marker included.
91
+ * Defaults to 1,000,000 (matching the Python SDK).
92
+ */
93
+ maxTextFieldChars?: number;
94
+ };
95
+ declare class EventShipper {
96
+ private baseUrl;
97
+ private writeKey?;
98
+ private enabled;
99
+ private debug;
100
+ private partialFlushMs;
101
+ private sdkName;
102
+ private prefix;
103
+ private defaultEventName;
104
+ private context;
105
+ private buffers;
106
+ private sticky;
107
+ private timers;
108
+ private inFlight;
109
+ private maxTextFieldCharsOpt;
110
+ /**
111
+ * Epoch ms deadline while `shutdown()` is draining; undefined otherwise.
112
+ * Checked before every POST issued during the final flush.
113
+ */
114
+ private shutdownDeadlineAt;
115
+ /**
116
+ * Set once `shutdown()` begins and never cleared. Sends issued after the
117
+ * drain window (stragglers, or flush work the deadline abandoned
118
+ * mid-drain) run as a single short attempt instead of regaining the full
119
+ * retry schedule.
120
+ */
121
+ private hasShutdown;
122
+ /** URL of the local debugger / Workshop daemon, when one is reachable. */
123
+ private localDebuggerUrl;
124
+ constructor(opts: EventShipperOptions);
125
+ isDebugEnabled(): boolean;
126
+ private authHeaders;
127
+ /**
128
+ * Build the retry/timeout options for one POST, honoring the shutdown
129
+ * deadline. Returns `null` when the shutdown drain window is exhausted —
130
+ * the caller must drop the payload (with a rate-limited warning) instead
131
+ * of issuing a request that could outlive process exit.
132
+ *
133
+ * Checked fresh on EVERY send, so a shutdown that begins while the flush
134
+ * path is mid-drain takes effect immediately: no further retries, and the
135
+ * per-attempt timeout is clamped to the remaining window. After
136
+ * `shutdown()` returns (deadline cleared, `hasShutdown` still set),
137
+ * sends — late callers, or flush work the deadline abandoned mid-drain —
138
+ * run as a single short attempt rather than regaining the full retry
139
+ * schedule.
140
+ */
141
+ private requestOpts;
142
+ patch(eventId: string, patch: Patch): Promise<void>;
143
+ finish(eventId: string, patch: {
144
+ output?: string;
145
+ model?: string;
146
+ properties?: Record<string, unknown>;
147
+ userId?: string;
148
+ }): Promise<void>;
149
+ flush(): Promise<void>;
150
+ shutdown(): Promise<void>;
151
+ trackSignal(signal: SignalInput): Promise<void>;
152
+ identify(users: IdentifyInput | IdentifyInput[]): Promise<void>;
153
+ private warnShutdownDrop;
154
+ private flushOne;
155
+ }
156
+ /**
157
+ * Hook fired per OTLP span right before the span is shipped (to the Raindrop
158
+ * API and to a local debugger). Lets callers inspect, rewrite, or drop the
159
+ * entire span — not just individual attributes — which is more flexible than
160
+ * an attribute-level hook (you can rename attributes, add new ones, drop the
161
+ * span outright, etc.).
162
+ *
163
+ * Return values:
164
+ * - `undefined` or the same span: ship the span unchanged.
165
+ * - a new `OtlpSpan`: ship the returned span in place of the original.
166
+ * - `null`: drop the span entirely from every ship path.
167
+ *
168
+ * The hook runs on the hot path — keep it synchronous and side-effect-free.
169
+ * If the hook throws, the span is dropped (fail-closed) so a buggy hook can
170
+ * never accidentally ship raw, un-redacted spans.
171
+ */
172
+ type TransformSpanHook = (span: OtlpSpan) => OtlpSpan | null | undefined;
173
+
174
+ type InternalSpan = {
175
+ ids: SpanIds;
176
+ name: string;
177
+ startTimeUnixNano: string;
178
+ endTimeUnixNano?: string;
179
+ attributes: Array<OtlpKeyValue | undefined>;
180
+ };
181
+ type TraceShipperOptions = {
182
+ writeKey?: string;
183
+ endpoint?: string;
184
+ enabled?: boolean;
185
+ debug: boolean;
186
+ debugSpans?: boolean;
187
+ flushIntervalMs?: number;
188
+ maxBatchSize?: number;
189
+ maxQueueSize?: number;
190
+ sdkName?: string;
191
+ serviceName?: string;
192
+ serviceVersion?: string;
193
+ /**
194
+ * Explicit Workshop / local debugger URL. Wins over env vars + auto-detect.
195
+ * Pass `null` to opt out of all mirroring (including auto-detect).
196
+ */
197
+ localDebuggerUrl?: string | null;
198
+ /**
199
+ * Per-span hook that fires for every OTLP span right before the span is
200
+ * shipped (both to the Raindrop API and to a local debugger). Lets callers
201
+ * inspect, rewrite, or drop entire spans — rename attributes, add new ones,
202
+ * scrub additional secret-shaped values inside `ai.prompt.messages` /
203
+ * `ai.toolCall.args`, etc.
204
+ *
205
+ * Return values:
206
+ * - `undefined` or the same span reference: ship the span unchanged.
207
+ * - a new `OtlpSpan`: ship the returned span in place of the original.
208
+ * - `null`: drop the span entirely from every ship path.
209
+ *
210
+ * The hook runs BEFORE the default redactor (which is the always-on floor
211
+ * for documented BYOK secrets). The default redactor still runs on the
212
+ * post-transform span unless `disableDefaultRedaction` is set, so even if
213
+ * a custom transform overlooks a secret-shaped attribute, the floor catches
214
+ * it.
215
+ *
216
+ * The hook runs on the hot path — keep it synchronous and side-effect-free.
217
+ * If the hook itself throws, the span is dropped (fail-closed) so a buggy
218
+ * hook can never accidentally ship raw, un-redacted spans.
219
+ */
220
+ transformSpan?: TransformSpanHook;
221
+ /**
222
+ * Disable the built-in default span transformer (which scrubs documented
223
+ * secret-shaped properties — `apiKey`, `secretAccessKey`, `privateKey`,
224
+ * etc. — inside `ai.request.providerOptions` and
225
+ * `ai.response.providerMetadata`).
226
+ *
227
+ * Default: `false` (i.e. default redaction is on). Setting this to `true`
228
+ * disables the floor entirely; provide a custom `transformSpan` if you
229
+ * still want some redaction in that case.
230
+ */
231
+ disableDefaultRedaction?: boolean;
232
+ /**
233
+ * Per-attribute character cap applied to every span attribute string value
234
+ * right before the span enters a ship path, so a multi-MB prompt/tool
235
+ * payload can never make the batch `JSON.stringify` (which runs on the
236
+ * event loop) cost seconds. Truncated values end with
237
+ * `...[truncated by raindrop]` and never exceed the cap, marker included.
238
+ * Defaults to 1,000,000 (matching the Python SDK).
239
+ */
240
+ maxTextFieldChars?: number;
241
+ };
242
+ declare class TraceShipper {
243
+ private baseUrl;
244
+ private writeKey?;
245
+ private enabled;
246
+ private debug;
247
+ private debugSpans;
248
+ private sdkName;
249
+ private prefix;
250
+ private serviceName;
251
+ private serviceVersion;
252
+ private flushIntervalMs;
253
+ private maxBatchSize;
254
+ private maxQueueSize;
255
+ private queue;
256
+ private timer;
257
+ private inFlight;
258
+ /** URL of the local debugger / Workshop daemon, when one is reachable. */
259
+ private localDebuggerUrl;
260
+ private transformSpanHook;
261
+ private disableDefaultRedaction;
262
+ private maxTextFieldCharsOpt;
263
+ /**
264
+ * Epoch ms deadline while `shutdown()` is draining; undefined otherwise.
265
+ * Checked before every batch POST issued during the final flush.
266
+ */
267
+ private shutdownDeadlineAt;
268
+ /**
269
+ * Set once `shutdown()` begins and never cleared. Sends issued after the
270
+ * drain window (stragglers, or flush work the deadline abandoned
271
+ * mid-drain) run as a single short attempt instead of regaining the full
272
+ * retry schedule.
273
+ */
274
+ private hasShutdown;
275
+ constructor(opts: TraceShipperOptions);
276
+ /**
277
+ * Cap every string attribute value on the span. O(#attributes) length
278
+ * checks; only oversized values pay a slice. Runs AFTER the redaction
279
+ * pipeline so the default secret-scrub still sees parseable JSON in
280
+ * `ai.request.providerOptions` / `ai.response.providerMetadata` (capping
281
+ * first could cut a JSON blob mid-way, fail the parse, and ship secrets
282
+ * in the surviving prefix).
283
+ *
284
+ * A stricter `OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT` env var is honored
285
+ * for span content, matching the Python SDK and the OTel SDK convention.
286
+ */
287
+ private capSpanAttributes;
288
+ /**
289
+ * Apply the user `transformSpan` hook (if any) followed by the default
290
+ * redactor (unless disabled). Returns either the (possibly new) span to
291
+ * ship, or `null` to drop the span entirely.
292
+ *
293
+ * Ordering: user hook runs first so callers can rewrite the span freely
294
+ * (rename attrs, add new ones, scrub things the default doesn't know
295
+ * about). The default redactor then runs on whatever the user produced,
296
+ * acting as the always-on floor for documented BYOK secrets. If the user
297
+ * sets `disableDefaultRedaction: true`, the floor is skipped.
298
+ *
299
+ * Fail-closed: if the user hook throws, the span is dropped — a buggy
300
+ * hook can never accidentally ship raw, un-redacted spans.
301
+ */
302
+ private redactSpan;
303
+ isDebugEnabled(): boolean;
304
+ private authHeaders;
305
+ startSpan(args: {
306
+ name: string;
307
+ parent?: {
308
+ traceIdB64: string;
309
+ spanIdB64: string;
310
+ };
311
+ eventId: string;
312
+ operationId?: string;
313
+ attributes?: Array<OtlpKeyValue | undefined>;
314
+ startTimeUnixNano?: string;
315
+ }): InternalSpan;
316
+ private mirrorToLocalDebugger;
317
+ endSpan(span: InternalSpan, extra?: {
318
+ attributes?: InternalSpan["attributes"];
319
+ error?: unknown;
320
+ status?: OtlpSpanStatus;
321
+ endTimeUnixNano?: string;
322
+ }): void;
323
+ createSpan(args: {
324
+ name: string;
325
+ parent?: {
326
+ traceIdB64: string;
327
+ spanIdB64: string;
328
+ };
329
+ eventId: string;
330
+ startTimeUnixNano: string;
331
+ endTimeUnixNano: string;
332
+ attributes?: Array<OtlpKeyValue | undefined>;
333
+ status?: OtlpSpanStatus;
334
+ }): void;
335
+ enqueue(span: OtlpSpan): void;
336
+ flush(): Promise<void>;
337
+ /** See EventShipper.requestOpts — same shutdown-budget semantics. */
338
+ private requestOpts;
339
+ shutdown(): Promise<void>;
340
+ }
341
+
342
+ type ParentSpanContext = {
343
+ traceIdB64: string;
344
+ spanIdB64: string;
345
+ eventId: string;
346
+ };
347
+ interface ContextSpan {
348
+ readonly traceIdB64: string;
349
+ readonly spanIdB64: string;
350
+ readonly eventId: string;
351
+ log?(data: Record<string, unknown>): void;
352
+ }
353
+ interface AsyncLocalStorageLike<T> {
354
+ getStore(): T | undefined;
355
+ run<R>(store: T, callback: () => R): R;
356
+ enterWith?(store: T): void;
357
+ }
358
+ declare abstract class ContextManager {
359
+ abstract getParentSpanIds(): ParentSpanContext | undefined;
360
+ abstract runInContext<R>(span: ContextSpan, callback: () => R): R;
361
+ abstract getCurrentSpan(): ContextSpan | undefined;
362
+ abstract isReady(): boolean;
363
+ }
364
+ declare global {
365
+ var RAINDROP_CONTEXT_MANAGER: (new () => ContextManager) | undefined;
366
+ var RAINDROP_ASYNC_LOCAL_STORAGE: (new <T>() => AsyncLocalStorageLike<T>) | undefined;
367
+ }
368
+
369
+ export { type Attachment as A, EventShipper as E, type OtlpSpan as O, TraceShipper as T };