@compilr-dev/agents 0.0.1
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 +1277 -0
- package/dist/agent.d.ts +1272 -0
- package/dist/agent.js +1912 -0
- package/dist/anchors/builtin.d.ts +24 -0
- package/dist/anchors/builtin.js +61 -0
- package/dist/anchors/index.d.ts +6 -0
- package/dist/anchors/index.js +5 -0
- package/dist/anchors/manager.d.ts +115 -0
- package/dist/anchors/manager.js +412 -0
- package/dist/anchors/types.d.ts +168 -0
- package/dist/anchors/types.js +10 -0
- package/dist/context/index.d.ts +12 -0
- package/dist/context/index.js +10 -0
- package/dist/context/manager.d.ts +224 -0
- package/dist/context/manager.js +770 -0
- package/dist/context/types.d.ts +377 -0
- package/dist/context/types.js +7 -0
- package/dist/costs/index.d.ts +8 -0
- package/dist/costs/index.js +7 -0
- package/dist/costs/tracker.d.ts +121 -0
- package/dist/costs/tracker.js +295 -0
- package/dist/costs/types.d.ts +157 -0
- package/dist/costs/types.js +8 -0
- package/dist/errors.d.ts +178 -0
- package/dist/errors.js +249 -0
- package/dist/guardrails/builtin.d.ts +27 -0
- package/dist/guardrails/builtin.js +223 -0
- package/dist/guardrails/index.d.ts +6 -0
- package/dist/guardrails/index.js +5 -0
- package/dist/guardrails/manager.d.ts +117 -0
- package/dist/guardrails/manager.js +288 -0
- package/dist/guardrails/types.d.ts +159 -0
- package/dist/guardrails/types.js +7 -0
- package/dist/hooks/index.d.ts +31 -0
- package/dist/hooks/index.js +29 -0
- package/dist/hooks/manager.d.ts +147 -0
- package/dist/hooks/manager.js +600 -0
- package/dist/hooks/types.d.ts +368 -0
- package/dist/hooks/types.js +12 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +73 -0
- package/dist/mcp/client.d.ts +93 -0
- package/dist/mcp/client.js +287 -0
- package/dist/mcp/errors.d.ts +60 -0
- package/dist/mcp/errors.js +78 -0
- package/dist/mcp/index.d.ts +43 -0
- package/dist/mcp/index.js +45 -0
- package/dist/mcp/manager.d.ts +120 -0
- package/dist/mcp/manager.js +276 -0
- package/dist/mcp/tools.d.ts +54 -0
- package/dist/mcp/tools.js +99 -0
- package/dist/mcp/types.d.ts +150 -0
- package/dist/mcp/types.js +40 -0
- package/dist/memory/index.d.ts +8 -0
- package/dist/memory/index.js +7 -0
- package/dist/memory/loader.d.ts +114 -0
- package/dist/memory/loader.js +463 -0
- package/dist/memory/types.d.ts +182 -0
- package/dist/memory/types.js +8 -0
- package/dist/messages/index.d.ts +82 -0
- package/dist/messages/index.js +155 -0
- package/dist/permissions/index.d.ts +5 -0
- package/dist/permissions/index.js +4 -0
- package/dist/permissions/manager.d.ts +125 -0
- package/dist/permissions/manager.js +379 -0
- package/dist/permissions/types.d.ts +162 -0
- package/dist/permissions/types.js +7 -0
- package/dist/providers/claude.d.ts +90 -0
- package/dist/providers/claude.js +348 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.js +11 -0
- package/dist/providers/mock.d.ts +133 -0
- package/dist/providers/mock.js +204 -0
- package/dist/providers/types.d.ts +168 -0
- package/dist/providers/types.js +4 -0
- package/dist/rate-limit/index.d.ts +45 -0
- package/dist/rate-limit/index.js +47 -0
- package/dist/rate-limit/limiter.d.ts +104 -0
- package/dist/rate-limit/limiter.js +326 -0
- package/dist/rate-limit/provider-wrapper.d.ts +112 -0
- package/dist/rate-limit/provider-wrapper.js +201 -0
- package/dist/rate-limit/retry.d.ts +108 -0
- package/dist/rate-limit/retry.js +287 -0
- package/dist/rate-limit/types.d.ts +181 -0
- package/dist/rate-limit/types.js +22 -0
- package/dist/rehearsal/file-analyzer.d.ts +22 -0
- package/dist/rehearsal/file-analyzer.js +351 -0
- package/dist/rehearsal/git-analyzer.d.ts +22 -0
- package/dist/rehearsal/git-analyzer.js +472 -0
- package/dist/rehearsal/index.d.ts +35 -0
- package/dist/rehearsal/index.js +36 -0
- package/dist/rehearsal/manager.d.ts +100 -0
- package/dist/rehearsal/manager.js +290 -0
- package/dist/rehearsal/types.d.ts +235 -0
- package/dist/rehearsal/types.js +8 -0
- package/dist/skills/index.d.ts +160 -0
- package/dist/skills/index.js +282 -0
- package/dist/state/agent-state.d.ts +41 -0
- package/dist/state/agent-state.js +88 -0
- package/dist/state/checkpointer.d.ts +110 -0
- package/dist/state/checkpointer.js +362 -0
- package/dist/state/errors.d.ts +66 -0
- package/dist/state/errors.js +88 -0
- package/dist/state/index.d.ts +35 -0
- package/dist/state/index.js +37 -0
- package/dist/state/serializer.d.ts +55 -0
- package/dist/state/serializer.js +172 -0
- package/dist/state/types.d.ts +312 -0
- package/dist/state/types.js +14 -0
- package/dist/tools/builtin/bash-output.d.ts +61 -0
- package/dist/tools/builtin/bash-output.js +90 -0
- package/dist/tools/builtin/bash.d.ts +150 -0
- package/dist/tools/builtin/bash.js +354 -0
- package/dist/tools/builtin/edit.d.ts +50 -0
- package/dist/tools/builtin/edit.js +215 -0
- package/dist/tools/builtin/glob.d.ts +62 -0
- package/dist/tools/builtin/glob.js +244 -0
- package/dist/tools/builtin/grep.d.ts +74 -0
- package/dist/tools/builtin/grep.js +363 -0
- package/dist/tools/builtin/index.d.ts +44 -0
- package/dist/tools/builtin/index.js +69 -0
- package/dist/tools/builtin/kill-shell.d.ts +44 -0
- package/dist/tools/builtin/kill-shell.js +80 -0
- package/dist/tools/builtin/read-file.d.ts +57 -0
- package/dist/tools/builtin/read-file.js +184 -0
- package/dist/tools/builtin/shell-manager.d.ts +176 -0
- package/dist/tools/builtin/shell-manager.js +337 -0
- package/dist/tools/builtin/task.d.ts +202 -0
- package/dist/tools/builtin/task.js +350 -0
- package/dist/tools/builtin/todo.d.ts +207 -0
- package/dist/tools/builtin/todo.js +453 -0
- package/dist/tools/builtin/utils.d.ts +27 -0
- package/dist/tools/builtin/utils.js +70 -0
- package/dist/tools/builtin/web-fetch.d.ts +96 -0
- package/dist/tools/builtin/web-fetch.js +290 -0
- package/dist/tools/builtin/write-file.d.ts +54 -0
- package/dist/tools/builtin/write-file.js +147 -0
- package/dist/tools/define.d.ts +60 -0
- package/dist/tools/define.js +65 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +37 -0
- package/dist/tools/registry.d.ts +79 -0
- package/dist/tools/registry.js +151 -0
- package/dist/tools/types.d.ts +59 -0
- package/dist/tools/types.js +4 -0
- package/dist/tracing/hooks.d.ts +58 -0
- package/dist/tracing/hooks.js +377 -0
- package/dist/tracing/index.d.ts +51 -0
- package/dist/tracing/index.js +55 -0
- package/dist/tracing/logging.d.ts +78 -0
- package/dist/tracing/logging.js +310 -0
- package/dist/tracing/manager.d.ts +160 -0
- package/dist/tracing/manager.js +468 -0
- package/dist/tracing/otel.d.ts +102 -0
- package/dist/tracing/otel.js +246 -0
- package/dist/tracing/types.d.ts +346 -0
- package/dist/tracing/types.js +38 -0
- package/dist/utils/index.d.ts +23 -0
- package/dist/utils/index.js +44 -0
- package/package.json +79 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenTelemetry Integration
|
|
3
|
+
*
|
|
4
|
+
* Provides optional integration with OpenTelemetry for exporting traces
|
|
5
|
+
* to OTel-compatible backends (Jaeger, Zipkin, etc.)
|
|
6
|
+
*
|
|
7
|
+
* Requires optional peer dependency: @opentelemetry/api
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Error thrown when OpenTelemetry SDK is not installed
|
|
11
|
+
*/
|
|
12
|
+
export class OTelNotInstalledError extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super('OpenTelemetry SDK is not installed. ' +
|
|
15
|
+
'Install it with: npm install @opentelemetry/api @opentelemetry/sdk-trace-node');
|
|
16
|
+
this.name = 'OTelNotInstalledError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Check if a value is an OTelNotInstalledError
|
|
21
|
+
*/
|
|
22
|
+
export function isOTelNotInstalledError(error) {
|
|
23
|
+
return error instanceof Error && error.name === 'OTelNotInstalledError';
|
|
24
|
+
}
|
|
25
|
+
// Cache for OTel SDK
|
|
26
|
+
let otelCache = null;
|
|
27
|
+
/**
|
|
28
|
+
* Dynamically load OpenTelemetry SDK
|
|
29
|
+
*
|
|
30
|
+
* @throws OTelNotInstalledError if SDK is not installed
|
|
31
|
+
*/
|
|
32
|
+
async function getOTelSDK() {
|
|
33
|
+
if (otelCache)
|
|
34
|
+
return otelCache;
|
|
35
|
+
try {
|
|
36
|
+
// Use dynamic import with string to avoid TypeScript trying to resolve the module
|
|
37
|
+
const modulePath = '@opentelemetry/api';
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
39
|
+
const api = await import(modulePath);
|
|
40
|
+
otelCache = {
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
42
|
+
trace: api.trace,
|
|
43
|
+
};
|
|
44
|
+
return otelCache;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error instanceof Error &&
|
|
48
|
+
(error.message.includes('Cannot find module') ||
|
|
49
|
+
error.message.includes('ERR_MODULE_NOT_FOUND'))) {
|
|
50
|
+
throw new OTelNotInstalledError();
|
|
51
|
+
}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Create an OpenTelemetry exporter that wraps OTel spans
|
|
57
|
+
*
|
|
58
|
+
* This exporter converts our internal Span format to OpenTelemetry spans
|
|
59
|
+
* and uses the configured OTel tracer to export them.
|
|
60
|
+
*
|
|
61
|
+
* @param tracerName - Name for the OTel tracer
|
|
62
|
+
* @param tracerVersion - Version for the OTel tracer
|
|
63
|
+
* @returns OTelExporter instance
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* // First, set up OpenTelemetry in your application:
|
|
68
|
+
* // import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
|
69
|
+
* // const provider = new NodeTracerProvider();
|
|
70
|
+
* // provider.register();
|
|
71
|
+
*
|
|
72
|
+
* // Then use with TracingManager:
|
|
73
|
+
* const exporter = await createOTelExporter('my-agent', '1.0.0');
|
|
74
|
+
* const tracingManager = new TracingManager({
|
|
75
|
+
* serviceName: 'my-agent',
|
|
76
|
+
* otelExporter: exporter,
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export async function createOTelExporter(tracerName = 'agent-tracing', tracerVersion = '1.0.0') {
|
|
81
|
+
const sdk = await getOTelSDK();
|
|
82
|
+
const tracer = sdk.trace.getTracer(tracerName, tracerVersion);
|
|
83
|
+
return {
|
|
84
|
+
name: 'opentelemetry',
|
|
85
|
+
async export(spans) {
|
|
86
|
+
await Promise.resolve(); // Ensure async function has await
|
|
87
|
+
// Convert our spans to OTel spans
|
|
88
|
+
// Note: This creates new OTel spans which will be exported by the configured OTel exporter
|
|
89
|
+
for (const span of spans) {
|
|
90
|
+
const otelSpan = tracer.startSpan(span.name, {
|
|
91
|
+
startTime: span.startTime,
|
|
92
|
+
});
|
|
93
|
+
// Set attributes
|
|
94
|
+
for (const [key, value] of Object.entries(span.attributes)) {
|
|
95
|
+
otelSpan.setAttribute(key, value);
|
|
96
|
+
}
|
|
97
|
+
// Add events
|
|
98
|
+
for (const event of span.events) {
|
|
99
|
+
otelSpan.addEvent(event.name, event.attributes);
|
|
100
|
+
}
|
|
101
|
+
// Set status
|
|
102
|
+
const statusCode = span.status === 'ok' ? 1 : span.status === 'error' ? 2 : 0;
|
|
103
|
+
otelSpan.setStatus({
|
|
104
|
+
code: statusCode,
|
|
105
|
+
message: span.statusMessage,
|
|
106
|
+
});
|
|
107
|
+
// End span
|
|
108
|
+
otelSpan.end(span.endTime);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
async shutdown() {
|
|
112
|
+
// OTel handles shutdown through the provider
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Create a console exporter for debugging
|
|
118
|
+
*
|
|
119
|
+
* This exporter simply logs spans to the console in a readable format.
|
|
120
|
+
* Useful for development and debugging.
|
|
121
|
+
*
|
|
122
|
+
* @param options - Console exporter options
|
|
123
|
+
* @returns OTelExporter instance
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const exporter = createConsoleExporter({ prettyPrint: true });
|
|
128
|
+
* const tracingManager = new TracingManager({
|
|
129
|
+
* serviceName: 'my-agent',
|
|
130
|
+
* otelExporter: exporter,
|
|
131
|
+
* });
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export function createConsoleExporter(options = {}) {
|
|
135
|
+
// eslint-disable-next-line no-console
|
|
136
|
+
const { prettyPrint = true, output = console.log } = options;
|
|
137
|
+
return {
|
|
138
|
+
name: 'console',
|
|
139
|
+
async export(spans) {
|
|
140
|
+
await Promise.resolve(); // Ensure async function has await
|
|
141
|
+
for (const span of spans) {
|
|
142
|
+
const data = {
|
|
143
|
+
traceId: span.traceId,
|
|
144
|
+
spanId: span.spanId,
|
|
145
|
+
parentSpanId: span.parentSpanId,
|
|
146
|
+
name: span.name,
|
|
147
|
+
kind: span.kind,
|
|
148
|
+
status: span.status,
|
|
149
|
+
durationMs: span.durationMs,
|
|
150
|
+
attributes: span.attributes,
|
|
151
|
+
events: span.events,
|
|
152
|
+
};
|
|
153
|
+
if (prettyPrint) {
|
|
154
|
+
output(`[SPAN] ${span.name} (${String(span.durationMs ?? 0)}ms) - ${span.status}`);
|
|
155
|
+
output(JSON.stringify(data, null, 2));
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
output(JSON.stringify(data));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Create a batch exporter that buffers spans and exports in batches
|
|
166
|
+
*
|
|
167
|
+
* @param innerExporter - The actual exporter to use
|
|
168
|
+
* @param options - Batch options
|
|
169
|
+
* @returns OTelExporter instance
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const otelExporter = await createOTelExporter();
|
|
174
|
+
* const batchExporter = createBatchExporter(otelExporter, {
|
|
175
|
+
* maxBatchSize: 100,
|
|
176
|
+
* flushIntervalMs: 5000,
|
|
177
|
+
* });
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export function createBatchExporter(innerExporter, options = {}) {
|
|
181
|
+
const { maxBatchSize = 100, flushIntervalMs = 5000 } = options;
|
|
182
|
+
let buffer = [];
|
|
183
|
+
let flushTimer = null;
|
|
184
|
+
const flush = async () => {
|
|
185
|
+
if (buffer.length === 0)
|
|
186
|
+
return;
|
|
187
|
+
const spans = buffer;
|
|
188
|
+
buffer = [];
|
|
189
|
+
await innerExporter.export(spans);
|
|
190
|
+
};
|
|
191
|
+
// Start flush timer
|
|
192
|
+
flushTimer = setInterval(() => {
|
|
193
|
+
flush().catch(() => {
|
|
194
|
+
// Ignore flush errors
|
|
195
|
+
});
|
|
196
|
+
}, flushIntervalMs);
|
|
197
|
+
return {
|
|
198
|
+
name: `batch:${innerExporter.name}`,
|
|
199
|
+
async export(spans) {
|
|
200
|
+
buffer.push(...spans);
|
|
201
|
+
// Flush if buffer is full
|
|
202
|
+
if (buffer.length >= maxBatchSize) {
|
|
203
|
+
await flush();
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
async shutdown() {
|
|
207
|
+
// Clear timer
|
|
208
|
+
if (flushTimer) {
|
|
209
|
+
clearInterval(flushTimer);
|
|
210
|
+
flushTimer = null;
|
|
211
|
+
}
|
|
212
|
+
// Final flush
|
|
213
|
+
await flush();
|
|
214
|
+
// Shutdown inner exporter
|
|
215
|
+
await innerExporter.shutdown?.();
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Create a multi-exporter that sends to multiple backends
|
|
221
|
+
*
|
|
222
|
+
* @param exporters - List of exporters to use
|
|
223
|
+
* @returns OTelExporter instance
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* const multiExporter = createMultiExporter([
|
|
228
|
+
* createConsoleExporter(),
|
|
229
|
+
* await createOTelExporter(),
|
|
230
|
+
* ]);
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
export function createMultiExporter(exporters) {
|
|
234
|
+
return {
|
|
235
|
+
name: `multi:[${exporters.map((e) => e.name).join(',')}]`,
|
|
236
|
+
async export(spans) {
|
|
237
|
+
await Promise.all(exporters.map((e) => e.export(spans)));
|
|
238
|
+
},
|
|
239
|
+
async shutdown() {
|
|
240
|
+
const shutdowns = exporters
|
|
241
|
+
.map((e) => e.shutdown?.())
|
|
242
|
+
.filter((p) => p !== undefined);
|
|
243
|
+
await Promise.all(shutdowns);
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracing System Types
|
|
3
|
+
*
|
|
4
|
+
* Provides types for distributed tracing, structured logging, and observability.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Span status indicating success, error, or unset
|
|
8
|
+
*/
|
|
9
|
+
export type SpanStatus = 'unset' | 'ok' | 'error';
|
|
10
|
+
/**
|
|
11
|
+
* Span kind indicating the type of operation
|
|
12
|
+
*/
|
|
13
|
+
export type SpanKind = 'internal' | 'client' | 'server' | 'producer' | 'consumer';
|
|
14
|
+
/**
|
|
15
|
+
* Attribute value types (compatible with OpenTelemetry)
|
|
16
|
+
*/
|
|
17
|
+
export type AttributeValue = string | number | boolean | string[] | number[] | boolean[];
|
|
18
|
+
/**
|
|
19
|
+
* Span attributes map
|
|
20
|
+
*/
|
|
21
|
+
export type SpanAttributes = Record<string, AttributeValue>;
|
|
22
|
+
/**
|
|
23
|
+
* A trace span representing a unit of work
|
|
24
|
+
*/
|
|
25
|
+
export interface Span {
|
|
26
|
+
/** Unique span identifier */
|
|
27
|
+
spanId: string;
|
|
28
|
+
/** Trace ID this span belongs to */
|
|
29
|
+
traceId: string;
|
|
30
|
+
/** Parent span ID (undefined for root spans) */
|
|
31
|
+
parentSpanId?: string;
|
|
32
|
+
/** Human-readable span name */
|
|
33
|
+
name: string;
|
|
34
|
+
/** Span kind */
|
|
35
|
+
kind: SpanKind;
|
|
36
|
+
/** Start time in milliseconds since epoch */
|
|
37
|
+
startTime: number;
|
|
38
|
+
/** End time in milliseconds since epoch (undefined if still active) */
|
|
39
|
+
endTime?: number;
|
|
40
|
+
/** Duration in milliseconds (undefined if still active) */
|
|
41
|
+
durationMs?: number;
|
|
42
|
+
/** Span status */
|
|
43
|
+
status: SpanStatus;
|
|
44
|
+
/** Status message (typically for errors) */
|
|
45
|
+
statusMessage?: string;
|
|
46
|
+
/** Span attributes */
|
|
47
|
+
attributes: SpanAttributes;
|
|
48
|
+
/** Span events (timestamped annotations) */
|
|
49
|
+
events: SpanEvent[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Event within a span (timestamped annotation)
|
|
53
|
+
*/
|
|
54
|
+
export interface SpanEvent {
|
|
55
|
+
/** Event name */
|
|
56
|
+
name: string;
|
|
57
|
+
/** Timestamp in milliseconds since epoch */
|
|
58
|
+
timestamp: number;
|
|
59
|
+
/** Event attributes */
|
|
60
|
+
attributes?: SpanAttributes;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Context for creating a new span
|
|
64
|
+
*/
|
|
65
|
+
export interface SpanContext {
|
|
66
|
+
/** Trace ID */
|
|
67
|
+
traceId: string;
|
|
68
|
+
/** Parent span ID */
|
|
69
|
+
parentSpanId?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A complete trace containing multiple spans
|
|
73
|
+
*/
|
|
74
|
+
export interface Trace {
|
|
75
|
+
/** Unique trace identifier */
|
|
76
|
+
traceId: string;
|
|
77
|
+
/** Root span ID */
|
|
78
|
+
rootSpanId: string;
|
|
79
|
+
/** All spans in this trace */
|
|
80
|
+
spans: Span[];
|
|
81
|
+
/** Trace start time */
|
|
82
|
+
startTime: number;
|
|
83
|
+
/** Trace end time (undefined if still active) */
|
|
84
|
+
endTime?: number;
|
|
85
|
+
/** Total duration in milliseconds */
|
|
86
|
+
durationMs?: number;
|
|
87
|
+
/** Trace-level attributes */
|
|
88
|
+
attributes: SpanAttributes;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Standard attribute names following OpenTelemetry semantic conventions
|
|
92
|
+
*/
|
|
93
|
+
export declare const SemanticAttributes: {
|
|
94
|
+
readonly AGENT_SESSION_ID: "agent.session_id";
|
|
95
|
+
readonly AGENT_ITERATION: "agent.iteration";
|
|
96
|
+
readonly AGENT_MAX_ITERATIONS: "agent.max_iterations";
|
|
97
|
+
readonly LLM_PROVIDER: "llm.provider";
|
|
98
|
+
readonly LLM_MODEL: "llm.model";
|
|
99
|
+
readonly LLM_INPUT_TOKENS: "llm.input_tokens";
|
|
100
|
+
readonly LLM_OUTPUT_TOKENS: "llm.output_tokens";
|
|
101
|
+
readonly LLM_TOTAL_TOKENS: "llm.total_tokens";
|
|
102
|
+
readonly LLM_CACHE_READ_TOKENS: "llm.cache_read_tokens";
|
|
103
|
+
readonly LLM_CACHE_CREATION_TOKENS: "llm.cache_creation_tokens";
|
|
104
|
+
readonly TOOL_NAME: "tool.name";
|
|
105
|
+
readonly TOOL_INPUT: "tool.input";
|
|
106
|
+
readonly TOOL_SUCCESS: "tool.success";
|
|
107
|
+
readonly TOOL_ERROR: "tool.error";
|
|
108
|
+
readonly TOOL_RESULT: "tool.result";
|
|
109
|
+
readonly MESSAGE_ROLE: "message.role";
|
|
110
|
+
readonly MESSAGE_COUNT: "message.count";
|
|
111
|
+
readonly ERROR_TYPE: "error.type";
|
|
112
|
+
readonly ERROR_MESSAGE: "error.message";
|
|
113
|
+
readonly ERROR_STACK: "error.stack";
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Options for creating a TracingManager
|
|
117
|
+
*/
|
|
118
|
+
export interface TracingManagerOptions {
|
|
119
|
+
/** Service name for traces */
|
|
120
|
+
serviceName?: string;
|
|
121
|
+
/** Service version */
|
|
122
|
+
serviceVersion?: string;
|
|
123
|
+
/** Default attributes added to all spans */
|
|
124
|
+
defaultAttributes?: SpanAttributes;
|
|
125
|
+
/** Maximum spans per trace (prevents memory issues) */
|
|
126
|
+
maxSpansPerTrace?: number;
|
|
127
|
+
/** Whether to auto-generate trace IDs */
|
|
128
|
+
autoGenerateTraceId?: boolean;
|
|
129
|
+
/** Event handler for tracing events */
|
|
130
|
+
onEvent?: TracingEventHandler;
|
|
131
|
+
/** Optional OpenTelemetry exporter */
|
|
132
|
+
otelExporter?: OTelExporter;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Options for starting a span
|
|
136
|
+
*/
|
|
137
|
+
export interface StartSpanOptions {
|
|
138
|
+
/** Span name */
|
|
139
|
+
name: string;
|
|
140
|
+
/** Span kind (default: internal) */
|
|
141
|
+
kind?: SpanKind;
|
|
142
|
+
/** Parent span context (if not provided, uses current context) */
|
|
143
|
+
parentContext?: SpanContext;
|
|
144
|
+
/** Initial attributes */
|
|
145
|
+
attributes?: SpanAttributes;
|
|
146
|
+
/** Start time override (default: now) */
|
|
147
|
+
startTime?: number;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Options for ending a span
|
|
151
|
+
*/
|
|
152
|
+
export interface EndSpanOptions {
|
|
153
|
+
/** End time override (default: now) */
|
|
154
|
+
endTime?: number;
|
|
155
|
+
/** Final status */
|
|
156
|
+
status?: SpanStatus;
|
|
157
|
+
/** Status message */
|
|
158
|
+
statusMessage?: string;
|
|
159
|
+
/** Additional attributes to add */
|
|
160
|
+
attributes?: SpanAttributes;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Events emitted by TracingManager
|
|
164
|
+
*/
|
|
165
|
+
export type TracingEvent = {
|
|
166
|
+
type: 'trace:started';
|
|
167
|
+
traceId: string;
|
|
168
|
+
timestamp: number;
|
|
169
|
+
} | {
|
|
170
|
+
type: 'trace:ended';
|
|
171
|
+
traceId: string;
|
|
172
|
+
durationMs: number;
|
|
173
|
+
spanCount: number;
|
|
174
|
+
} | {
|
|
175
|
+
type: 'span:started';
|
|
176
|
+
spanId: string;
|
|
177
|
+
traceId: string;
|
|
178
|
+
name: string;
|
|
179
|
+
parentSpanId?: string;
|
|
180
|
+
} | {
|
|
181
|
+
type: 'span:ended';
|
|
182
|
+
spanId: string;
|
|
183
|
+
traceId: string;
|
|
184
|
+
durationMs: number;
|
|
185
|
+
status: SpanStatus;
|
|
186
|
+
} | {
|
|
187
|
+
type: 'span:event';
|
|
188
|
+
spanId: string;
|
|
189
|
+
traceId: string;
|
|
190
|
+
eventName: string;
|
|
191
|
+
} | {
|
|
192
|
+
type: 'span:error';
|
|
193
|
+
spanId: string;
|
|
194
|
+
traceId: string;
|
|
195
|
+
error: Error;
|
|
196
|
+
} | {
|
|
197
|
+
type: 'export:success';
|
|
198
|
+
traceId: string;
|
|
199
|
+
exporter: string;
|
|
200
|
+
} | {
|
|
201
|
+
type: 'export:error';
|
|
202
|
+
traceId: string;
|
|
203
|
+
exporter: string;
|
|
204
|
+
error: Error;
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Handler for tracing events
|
|
208
|
+
*/
|
|
209
|
+
export type TracingEventHandler = (event: TracingEvent) => void;
|
|
210
|
+
/**
|
|
211
|
+
* OpenTelemetry exporter interface (simplified)
|
|
212
|
+
* Allows integration with any OTel-compatible backend
|
|
213
|
+
*/
|
|
214
|
+
export interface OTelExporter {
|
|
215
|
+
/** Exporter name for logging */
|
|
216
|
+
name: string;
|
|
217
|
+
/** Export spans */
|
|
218
|
+
export(spans: Span[]): Promise<void>;
|
|
219
|
+
/** Shutdown the exporter */
|
|
220
|
+
shutdown?(): Promise<void>;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* OpenTelemetry SDK types (defined here to avoid importing optional dependency)
|
|
224
|
+
*/
|
|
225
|
+
export type OTelSDK = {
|
|
226
|
+
trace: {
|
|
227
|
+
getTracer(name: string, version?: string): OTelTracer;
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
export type OTelTracer = {
|
|
231
|
+
startSpan(name: string, options?: unknown): OTelSpan;
|
|
232
|
+
};
|
|
233
|
+
export type OTelSpan = {
|
|
234
|
+
setAttribute(key: string, value: AttributeValue): void;
|
|
235
|
+
setStatus(status: {
|
|
236
|
+
code: number;
|
|
237
|
+
message?: string;
|
|
238
|
+
}): void;
|
|
239
|
+
addEvent(name: string, attributes?: Record<string, unknown>): void;
|
|
240
|
+
end(endTime?: number): void;
|
|
241
|
+
spanContext(): {
|
|
242
|
+
traceId: string;
|
|
243
|
+
spanId: string;
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Log levels
|
|
248
|
+
*/
|
|
249
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
250
|
+
/**
|
|
251
|
+
* Structured log entry
|
|
252
|
+
*/
|
|
253
|
+
export interface LogEntry {
|
|
254
|
+
/** Log level */
|
|
255
|
+
level: LogLevel;
|
|
256
|
+
/** Log message */
|
|
257
|
+
message: string;
|
|
258
|
+
/** Timestamp in ISO format */
|
|
259
|
+
timestamp: string;
|
|
260
|
+
/** Trace ID for correlation */
|
|
261
|
+
traceId?: string;
|
|
262
|
+
/** Span ID for correlation */
|
|
263
|
+
spanId?: string;
|
|
264
|
+
/** Session ID */
|
|
265
|
+
sessionId?: string;
|
|
266
|
+
/** Additional structured data */
|
|
267
|
+
data?: Record<string, unknown>;
|
|
268
|
+
/** Error information */
|
|
269
|
+
error?: {
|
|
270
|
+
name: string;
|
|
271
|
+
message: string;
|
|
272
|
+
stack?: string;
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Logger interface for structured logging
|
|
277
|
+
*/
|
|
278
|
+
export interface StructuredLogger {
|
|
279
|
+
debug(message: string, data?: Record<string, unknown>): void;
|
|
280
|
+
info(message: string, data?: Record<string, unknown>): void;
|
|
281
|
+
warn(message: string, data?: Record<string, unknown>): void;
|
|
282
|
+
error(message: string, error?: Error, data?: Record<string, unknown>): void;
|
|
283
|
+
/** Create a child logger with additional context */
|
|
284
|
+
child(context: Record<string, unknown>): StructuredLogger;
|
|
285
|
+
/** Set correlation IDs */
|
|
286
|
+
setCorrelation(traceId?: string, spanId?: string, sessionId?: string): void;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Options for creating a structured logger
|
|
290
|
+
*/
|
|
291
|
+
export interface StructuredLoggerOptions {
|
|
292
|
+
/** Minimum log level */
|
|
293
|
+
level?: LogLevel;
|
|
294
|
+
/** Service name */
|
|
295
|
+
serviceName?: string;
|
|
296
|
+
/** Pretty print JSON (default: false for production) */
|
|
297
|
+
prettyPrint?: boolean;
|
|
298
|
+
/** Output function (default: console.log) */
|
|
299
|
+
output?: (entry: LogEntry) => void;
|
|
300
|
+
/** Initial context */
|
|
301
|
+
context?: Record<string, unknown>;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Configuration for built-in tracing hooks
|
|
305
|
+
*/
|
|
306
|
+
export interface TracingHooksConfig {
|
|
307
|
+
/** Trace LLM calls */
|
|
308
|
+
traceLLM?: boolean;
|
|
309
|
+
/** Trace tool executions */
|
|
310
|
+
traceTools?: boolean;
|
|
311
|
+
/** Trace iterations */
|
|
312
|
+
traceIterations?: boolean;
|
|
313
|
+
/** Include input/output in attributes (may be verbose) */
|
|
314
|
+
includeIO?: boolean;
|
|
315
|
+
/** Truncate long values at this length */
|
|
316
|
+
truncateAt?: number;
|
|
317
|
+
/** Custom attribute mapper */
|
|
318
|
+
attributeMapper?: (phase: 'llm' | 'tool' | 'iteration', data: Record<string, unknown>) => SpanAttributes;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Context passed to tracing hooks
|
|
322
|
+
*/
|
|
323
|
+
export interface TracingHookContext {
|
|
324
|
+
/** Tracing manager instance */
|
|
325
|
+
manager: TracingManagerInterface;
|
|
326
|
+
/** Current trace ID */
|
|
327
|
+
traceId: string;
|
|
328
|
+
/** Current span context */
|
|
329
|
+
spanContext?: SpanContext;
|
|
330
|
+
/** Session ID */
|
|
331
|
+
sessionId: string;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Interface for TracingManager (for type references without circular deps)
|
|
335
|
+
*/
|
|
336
|
+
export interface TracingManagerInterface {
|
|
337
|
+
startTrace(attributes?: SpanAttributes): string;
|
|
338
|
+
endTrace(traceId: string): Trace | undefined;
|
|
339
|
+
startSpan(options: StartSpanOptions): Span;
|
|
340
|
+
endSpan(spanId: string, options?: EndSpanOptions): Span | undefined;
|
|
341
|
+
addSpanEvent(spanId: string, name: string, attributes?: SpanAttributes): void;
|
|
342
|
+
setSpanAttributes(spanId: string, attributes: SpanAttributes): void;
|
|
343
|
+
setSpanStatus(spanId: string, status: SpanStatus, message?: string): void;
|
|
344
|
+
getCurrentSpan(): Span | undefined;
|
|
345
|
+
getTrace(traceId: string): Trace | undefined;
|
|
346
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracing System Types
|
|
3
|
+
*
|
|
4
|
+
* Provides types for distributed tracing, structured logging, and observability.
|
|
5
|
+
*/
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// Semantic Conventions
|
|
8
|
+
// ============================================================================
|
|
9
|
+
/**
|
|
10
|
+
* Standard attribute names following OpenTelemetry semantic conventions
|
|
11
|
+
*/
|
|
12
|
+
export const SemanticAttributes = {
|
|
13
|
+
// Agent attributes
|
|
14
|
+
AGENT_SESSION_ID: 'agent.session_id',
|
|
15
|
+
AGENT_ITERATION: 'agent.iteration',
|
|
16
|
+
AGENT_MAX_ITERATIONS: 'agent.max_iterations',
|
|
17
|
+
// LLM attributes
|
|
18
|
+
LLM_PROVIDER: 'llm.provider',
|
|
19
|
+
LLM_MODEL: 'llm.model',
|
|
20
|
+
LLM_INPUT_TOKENS: 'llm.input_tokens',
|
|
21
|
+
LLM_OUTPUT_TOKENS: 'llm.output_tokens',
|
|
22
|
+
LLM_TOTAL_TOKENS: 'llm.total_tokens',
|
|
23
|
+
LLM_CACHE_READ_TOKENS: 'llm.cache_read_tokens',
|
|
24
|
+
LLM_CACHE_CREATION_TOKENS: 'llm.cache_creation_tokens',
|
|
25
|
+
// Tool attributes
|
|
26
|
+
TOOL_NAME: 'tool.name',
|
|
27
|
+
TOOL_INPUT: 'tool.input',
|
|
28
|
+
TOOL_SUCCESS: 'tool.success',
|
|
29
|
+
TOOL_ERROR: 'tool.error',
|
|
30
|
+
TOOL_RESULT: 'tool.result',
|
|
31
|
+
// Message attributes
|
|
32
|
+
MESSAGE_ROLE: 'message.role',
|
|
33
|
+
MESSAGE_COUNT: 'message.count',
|
|
34
|
+
// Error attributes
|
|
35
|
+
ERROR_TYPE: 'error.type',
|
|
36
|
+
ERROR_MESSAGE: 'error.message',
|
|
37
|
+
ERROR_STACK: 'error.stack',
|
|
38
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Generate a unique ID for tool uses
|
|
6
|
+
*/
|
|
7
|
+
export declare function generateId(): string;
|
|
8
|
+
/**
|
|
9
|
+
* Sleep for a specified duration
|
|
10
|
+
*/
|
|
11
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Retry a function with exponential backoff
|
|
14
|
+
*/
|
|
15
|
+
export declare function retry<T>(fn: () => Promise<T>, options?: {
|
|
16
|
+
maxRetries?: number;
|
|
17
|
+
baseDelay?: number;
|
|
18
|
+
maxDelay?: number;
|
|
19
|
+
}): Promise<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Truncate a string to a maximum length
|
|
22
|
+
*/
|
|
23
|
+
export declare function truncate(str: string, maxLength: number, suffix?: string): string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Generate a unique ID for tool uses
|
|
6
|
+
*/
|
|
7
|
+
export function generateId() {
|
|
8
|
+
return `toolu_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 9)}`;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Sleep for a specified duration
|
|
12
|
+
*/
|
|
13
|
+
export function sleep(ms) {
|
|
14
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Retry a function with exponential backoff
|
|
18
|
+
*/
|
|
19
|
+
export async function retry(fn, options = {}) {
|
|
20
|
+
const { maxRetries = 3, baseDelay = 1000, maxDelay = 10000 } = options;
|
|
21
|
+
let lastError;
|
|
22
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
23
|
+
try {
|
|
24
|
+
return await fn();
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
28
|
+
if (attempt < maxRetries) {
|
|
29
|
+
const delay = Math.min(baseDelay * Math.pow(2, attempt), maxDelay);
|
|
30
|
+
await sleep(delay);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
throw lastError ?? new Error('Retry failed with no error captured');
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Truncate a string to a maximum length
|
|
38
|
+
*/
|
|
39
|
+
export function truncate(str, maxLength, suffix = '...') {
|
|
40
|
+
if (str.length <= maxLength) {
|
|
41
|
+
return str;
|
|
42
|
+
}
|
|
43
|
+
return str.slice(0, maxLength - suffix.length) + suffix;
|
|
44
|
+
}
|