@ai-setting/roy-agent-core 1.4.11 → 1.4.13

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,368 @@
1
+ import {
2
+ SQLiteSpanStorage,
3
+ init_span_storage
4
+ } from "./chunk-1qwabsm0.js";
5
+ import {
6
+ __esm,
7
+ __export,
8
+ __require
9
+ } from "./chunk-wbkh7wat.js";
10
+
11
+ // src/env/log-trace/opentelemetry/propagation.ts
12
+ function padLeft(str, length) {
13
+ return str.padStart(length, "0");
14
+ }
15
+ function isValidHex(str) {
16
+ return /^[0-9a-f]+$/i.test(str);
17
+ }
18
+ function serialize(context) {
19
+ const traceId = padLeft(context.traceId.toLowerCase(), TRACE_ID_LENGTH);
20
+ const spanId = padLeft(context.spanId.toLowerCase(), PARENT_SPAN_ID_LENGTH);
21
+ const parts = [
22
+ TRACE_CONTEXT_VERSION,
23
+ traceId,
24
+ spanId,
25
+ TRACE_FLAGS_SAMPLED
26
+ ];
27
+ return parts.join("-");
28
+ }
29
+ function parse(traceparent) {
30
+ if (!traceparent || typeof traceparent !== "string") {
31
+ return;
32
+ }
33
+ const trimmed = traceparent.trim();
34
+ const parts = trimmed.split("-");
35
+ if (parts.length !== 4) {
36
+ return;
37
+ }
38
+ const [version, traceId, senderSpanId, flags] = parts;
39
+ if (version !== TRACE_CONTEXT_VERSION) {
40
+ if (version > TRACE_CONTEXT_VERSION) {
41
+ return;
42
+ }
43
+ }
44
+ if (traceId.length !== TRACE_ID_LENGTH || !isValidHex(traceId)) {
45
+ return;
46
+ }
47
+ if (senderSpanId.length !== PARENT_SPAN_ID_LENGTH || !isValidHex(senderSpanId)) {
48
+ return;
49
+ }
50
+ if (flags.length !== FLAGS_LENGTH || !isValidHex(flags)) {
51
+ return;
52
+ }
53
+ return {
54
+ traceId: traceId.toLowerCase(),
55
+ spanId: senderSpanId.toLowerCase()
56
+ };
57
+ }
58
+ var TRACE_CONTEXT_VERSION = "00", TRACE_FLAGS_SAMPLED = "01", TRACEPARENT_HEADER = "TRACEPARENT", TRACE_ID_LENGTH = 32, PARENT_SPAN_ID_LENGTH = 16, FLAGS_LENGTH = 2, propagation;
59
+ var init_propagation = __esm(() => {
60
+ propagation = {
61
+ inject(carrier, context) {
62
+ carrier[TRACEPARENT_HEADER] = serialize(context);
63
+ },
64
+ extract(carrier) {
65
+ const traceparent = carrier[TRACEPARENT_HEADER];
66
+ if (!traceparent) {
67
+ return;
68
+ }
69
+ return parse(traceparent);
70
+ },
71
+ getTraceparentHeader() {
72
+ return TRACEPARENT_HEADER;
73
+ }
74
+ };
75
+ });
76
+
77
+ // src/env/log-trace/types.ts
78
+ var SpanKind, SpanStatus;
79
+ var init_types = __esm(() => {
80
+ ((SpanKind2) => {
81
+ SpanKind2["CLIENT"] = "client";
82
+ SpanKind2["SERVER"] = "server";
83
+ SpanKind2["INTERNAL"] = "internal";
84
+ })(SpanKind ||= {});
85
+ ((SpanStatus2) => {
86
+ SpanStatus2["OK"] = "ok";
87
+ SpanStatus2["ERROR"] = "error";
88
+ })(SpanStatus ||= {});
89
+ });
90
+
91
+ // src/env/log-trace/opentelemetry/tracer-provider.ts
92
+ var exports_tracer_provider = {};
93
+ __export(exports_tracer_provider, {
94
+ resetTracerProvider: () => resetTracerProvider,
95
+ getTracerProvider: () => getTracerProvider,
96
+ OTelTracerProvider: () => OTelTracerProvider
97
+ });
98
+
99
+ class OTelSpanImpl {
100
+ name;
101
+ kind;
102
+ spanContext;
103
+ attributes = {};
104
+ startTime;
105
+ endTime;
106
+ error;
107
+ storage;
108
+ parentSpanContext;
109
+ onEnd;
110
+ constructor(name, spanContext, storage, parentSpanContext, onEnd) {
111
+ this.name = name;
112
+ this.kind = "internal";
113
+ this.spanContext = spanContext;
114
+ this.parentSpanContext = parentSpanContext;
115
+ this.startTime = Date.now();
116
+ this.storage = storage;
117
+ this.onEnd = onEnd;
118
+ }
119
+ setAttribute(key, value) {
120
+ this.attributes[key] = value;
121
+ }
122
+ addEvent(name, attributes) {
123
+ const events = this.attributes._events || [];
124
+ events.push({ name, attributes });
125
+ this.attributes._events = events;
126
+ }
127
+ end(result, error) {
128
+ this.endTime = Date.now();
129
+ if (error) {
130
+ this.error = error.message;
131
+ }
132
+ this.storage.save({
133
+ traceId: this.spanContext.traceId,
134
+ spanId: this.spanContext.spanId,
135
+ parentSpanId: this.spanContext.parentSpanId,
136
+ name: this.name,
137
+ kind: this.kind,
138
+ status: error ? "error" /* ERROR */ : "ok" /* OK */,
139
+ startTime: this.startTime,
140
+ endTime: this.endTime,
141
+ attributes: this.attributes,
142
+ result,
143
+ error: this.error
144
+ });
145
+ this.onEnd?.();
146
+ }
147
+ }
148
+
149
+ class OTelTracerImpl {
150
+ name;
151
+ version;
152
+ storage;
153
+ currentContext;
154
+ activeSpans = new Map;
155
+ onSpanEndCallback;
156
+ provider;
157
+ constructor(name, version, storage, provider) {
158
+ this.name = name;
159
+ this.version = version;
160
+ this.storage = storage;
161
+ this.provider = provider;
162
+ }
163
+ setOnSpanEndCallback(callback) {
164
+ this.onSpanEndCallback = callback;
165
+ }
166
+ getActiveSpanCount() {
167
+ return this.activeSpans.size;
168
+ }
169
+ startSpan(name, options) {
170
+ const parentFromOptions = options?.parent;
171
+ const hasExplicitParent = options && "parent" in options;
172
+ const globalContext = this.provider.getGlobalContext();
173
+ let effectiveParentContext;
174
+ if (hasExplicitParent) {
175
+ effectiveParentContext = parentFromOptions;
176
+ } else if (this.currentContext) {
177
+ effectiveParentContext = this.currentContext;
178
+ } else if (globalContext) {
179
+ effectiveParentContext = globalContext;
180
+ }
181
+ const traceId = effectiveParentContext?.traceId || this.generateTraceId();
182
+ const spanId = this.generateSpanId();
183
+ let parentSpanId;
184
+ if (hasExplicitParent && parentFromOptions) {
185
+ parentSpanId = parentFromOptions.parentSpanId || parentFromOptions.spanId;
186
+ } else if (this.currentContext) {
187
+ parentSpanId = this.currentContext.spanId;
188
+ } else if (globalContext) {
189
+ parentSpanId = globalContext.spanId;
190
+ }
191
+ const spanContext = {
192
+ traceId,
193
+ spanId,
194
+ parentSpanId
195
+ };
196
+ this.currentContext = {
197
+ traceId,
198
+ spanId,
199
+ parentSpanId
200
+ };
201
+ const span = new OTelSpanImpl(name, spanContext, this.storage, effectiveParentContext, () => this.handleSpanEnd(spanContext.spanId, effectiveParentContext?.spanId));
202
+ if (options?.attributes) {
203
+ for (const [key, value] of Object.entries(options.attributes)) {
204
+ span.setAttribute(key, value);
205
+ }
206
+ }
207
+ this.activeSpans.set(spanId, span);
208
+ return span;
209
+ }
210
+ injectToEnv(env) {
211
+ if (this.currentContext) {
212
+ propagation.inject(env, this.currentContext);
213
+ }
214
+ }
215
+ getCurrentContext() {
216
+ return this.currentContext;
217
+ }
218
+ setCurrentContext(context) {
219
+ this.currentContext = context;
220
+ }
221
+ endSpan(span) {
222
+ const spanContext = span.spanContext;
223
+ this.activeSpans.delete(spanContext.spanId);
224
+ const currentTraceId = this.currentContext?.traceId;
225
+ if (spanContext.parentSpanId) {
226
+ const parentSpan = this.activeSpans.get(spanContext.parentSpanId);
227
+ if (parentSpan) {
228
+ const parentContext = {
229
+ traceId: spanContext.traceId,
230
+ spanId: parentSpan.spanContext.spanId,
231
+ parentSpanId: parentSpan.spanContext.parentSpanId
232
+ };
233
+ this.currentContext = parentContext;
234
+ return parentContext;
235
+ }
236
+ if (currentTraceId) {
237
+ const parentContext = {
238
+ traceId: currentTraceId,
239
+ spanId: spanContext.parentSpanId,
240
+ parentSpanId: undefined
241
+ };
242
+ this.currentContext = parentContext;
243
+ return parentContext;
244
+ }
245
+ }
246
+ this.currentContext = undefined;
247
+ return;
248
+ }
249
+ generateTraceId() {
250
+ const timestamp = Date.now().toString(16).padStart(12, "0");
251
+ const random = Array.from({ length: 5 }, () => Math.floor(Math.random() * 4294967295).toString(16).padStart(8, "0")).join("");
252
+ return (timestamp + random).slice(0, 32).padStart(32, "0");
253
+ }
254
+ generateSpanId() {
255
+ return Math.floor(Math.random() * 18446744073709552000).toString(16).padStart(16, "0");
256
+ }
257
+ handleSpanEnd(spanId, parentSpanId) {
258
+ this.activeSpans.delete(spanId);
259
+ const currentTraceId = this.currentContext?.traceId;
260
+ if (parentSpanId) {
261
+ const parentSpan = this.activeSpans.get(parentSpanId);
262
+ if (parentSpan) {
263
+ this.currentContext = {
264
+ traceId: parentSpan.spanContext.traceId,
265
+ spanId: parentSpan.spanContext.spanId,
266
+ parentSpanId: parentSpan.spanContext.parentSpanId
267
+ };
268
+ return;
269
+ }
270
+ if (currentTraceId) {
271
+ this.currentContext = {
272
+ traceId: currentTraceId,
273
+ spanId: parentSpanId,
274
+ parentSpanId: undefined
275
+ };
276
+ return;
277
+ }
278
+ }
279
+ this.currentContext = undefined;
280
+ }
281
+ }
282
+
283
+ class OTelTracerProvider {
284
+ tracers = new Map;
285
+ storage;
286
+ initialized = false;
287
+ globalContext;
288
+ constructor(storage, dbPath) {
289
+ this.storage = storage || new SQLiteSpanStorage(dbPath || getDefaultDbPath());
290
+ }
291
+ async initialize() {
292
+ if (this.initialized)
293
+ return;
294
+ await this.storage.initialize();
295
+ this.initialized = true;
296
+ }
297
+ isInitialized() {
298
+ return this.initialized;
299
+ }
300
+ getGlobalContext() {
301
+ return this.globalContext;
302
+ }
303
+ setGlobalContext(context) {
304
+ this.globalContext = context;
305
+ }
306
+ getTracer(name, version) {
307
+ const key = `${name}@${version || "0.0.0"}`;
308
+ let tracer = this.tracers.get(key);
309
+ if (!tracer) {
310
+ tracer = new OTelTracerImpl(name, version, this.storage, this);
311
+ this.tracers.set(key, tracer);
312
+ if (this.initialized) {
313
+ this.restoreFromEnv(tracer);
314
+ }
315
+ }
316
+ return tracer;
317
+ }
318
+ restoreFromEnv(tracer) {
319
+ const extracted = propagation.extract(process.env);
320
+ if (extracted) {
321
+ tracer.setCurrentContext({
322
+ traceId: extracted.traceId,
323
+ spanId: extracted.spanId,
324
+ parentSpanId: undefined
325
+ });
326
+ }
327
+ }
328
+ shutdown() {
329
+ for (const tracer of this.tracers.values()) {}
330
+ this.tracers.clear();
331
+ this.storage.close();
332
+ this.initialized = false;
333
+ this.globalContext = undefined;
334
+ delete process.env["TRACEPARENT"];
335
+ delete process.env["TRACE_ID"];
336
+ delete process.env["LOG_TRACE_REQUEST_ID"];
337
+ }
338
+ getStorage() {
339
+ return this.storage;
340
+ }
341
+ }
342
+ function getTracerProvider() {
343
+ if (!providerInstance) {
344
+ providerInstance = new OTelTracerProvider;
345
+ }
346
+ return providerInstance;
347
+ }
348
+ function resetTracerProvider() {
349
+ if (providerInstance) {
350
+ providerInstance.shutdown();
351
+ providerInstance = null;
352
+ }
353
+ }
354
+ function getDefaultDbPath() {
355
+ const os = __require("os");
356
+ const path = __require("path");
357
+ const home = os.homedir();
358
+ const dataHome = process.env.XDG_DATA_HOME || path.join(home, ".local", "share");
359
+ return path.join(dataHome, "roy-agent", "traces.db");
360
+ }
361
+ var providerInstance = null;
362
+ var init_tracer_provider = __esm(() => {
363
+ init_span_storage();
364
+ init_propagation();
365
+ init_types();
366
+ });
367
+
368
+ export { propagation, init_propagation, SpanKind, SpanStatus, init_types, OTelTracerProvider, getTracerProvider, resetTracerProvider, exports_tracer_provider, init_tracer_provider };