@coalex-ai/sdk 0.4.1 → 0.5.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/dist/auto-instrument.d.ts +14 -0
- package/dist/auto-instrument.d.ts.map +1 -0
- package/dist/auto-instrument.js +129 -0
- package/dist/auto-instrument.js.map +1 -0
- package/dist/config.d.ts +5 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +2 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +8 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +52 -0
- package/dist/context.js.map +1 -0
- package/dist/evaluate.d.ts +14 -0
- package/dist/evaluate.d.ts.map +1 -0
- package/dist/evaluate.js +45 -0
- package/dist/evaluate.js.map +1 -0
- package/dist/ext/base.d.ts +3 -0
- package/dist/ext/base.d.ts.map +1 -0
- package/dist/ext/base.js +5 -0
- package/dist/ext/base.js.map +1 -0
- package/dist/ext/embedding.d.ts +16 -0
- package/dist/ext/embedding.d.ts.map +1 -0
- package/dist/ext/embedding.js +62 -0
- package/dist/ext/embedding.js.map +1 -0
- package/dist/ext/guardrail.d.ts +6 -0
- package/dist/ext/guardrail.d.ts.map +1 -0
- package/dist/ext/guardrail.js +32 -0
- package/dist/ext/guardrail.js.map +1 -0
- package/dist/ext/index.d.ts +12 -0
- package/dist/ext/index.d.ts.map +1 -0
- package/dist/ext/index.js +7 -0
- package/dist/ext/index.js.map +1 -0
- package/dist/ext/reranker.d.ts +20 -0
- package/dist/ext/reranker.d.ts.map +1 -0
- package/dist/ext/reranker.js +72 -0
- package/dist/ext/reranker.js.map +1 -0
- package/dist/ext/retrieval.d.ts +22 -0
- package/dist/ext/retrieval.d.ts.map +1 -0
- package/dist/ext/retrieval.js +79 -0
- package/dist/ext/retrieval.js.map +1 -0
- package/dist/ext/tool.d.ts +6 -0
- package/dist/ext/tool.d.ts.map +1 -0
- package/dist/ext/tool.js +28 -0
- package/dist/ext/tool.js.map +1 -0
- package/dist/filtering-exporter.d.ts +18 -0
- package/dist/filtering-exporter.d.ts.map +1 -0
- package/dist/filtering-exporter.js +59 -0
- package/dist/filtering-exporter.js.map +1 -0
- package/dist/index.d.ts +23 -9
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -26
- package/dist/index.js.map +1 -0
- package/dist/resolve.d.ts +21 -0
- package/dist/resolve.d.ts.map +1 -0
- package/dist/resolve.js +42 -0
- package/dist/resolve.js.map +1 -0
- package/dist/safe-exporter.d.ts +17 -0
- package/dist/safe-exporter.d.ts.map +1 -0
- package/dist/safe-exporter.js +64 -0
- package/dist/safe-exporter.js.map +1 -0
- package/dist/span-processor.d.ts +9 -0
- package/dist/span-processor.d.ts.map +1 -0
- package/dist/span-processor.js +31 -0
- package/dist/span-processor.js.map +1 -0
- package/package.json +44 -32
- package/LICENSE +0 -21
- package/README.md +0 -263
- package/dist/approve.d.ts +0 -95
- package/dist/approve.js +0 -170
- package/dist/metrics.d.ts +0 -101
- package/dist/metrics.js +0 -192
- package/dist/otel/register.d.ts +0 -98
- package/dist/otel/register.js +0 -200
package/dist/metrics.d.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Coalex metrics functionality for performance tracking and monitoring.
|
|
3
|
-
*/
|
|
4
|
-
export interface MetricRequestData {
|
|
5
|
-
requestId: string;
|
|
6
|
-
metricId: string;
|
|
7
|
-
value: number;
|
|
8
|
-
metricType?: string;
|
|
9
|
-
metadata?: Record<string, any>;
|
|
10
|
-
agentId?: string;
|
|
11
|
-
reviewingAgentId?: string;
|
|
12
|
-
taskId?: string;
|
|
13
|
-
}
|
|
14
|
-
export interface MetricResponseData {
|
|
15
|
-
id: number;
|
|
16
|
-
agentId: string;
|
|
17
|
-
accountId: string;
|
|
18
|
-
requestId: string;
|
|
19
|
-
metricId: string;
|
|
20
|
-
metricType?: string;
|
|
21
|
-
value: number;
|
|
22
|
-
metadata: Record<string, any>;
|
|
23
|
-
reviewingAgentId?: string;
|
|
24
|
-
taskId?: string;
|
|
25
|
-
createdAt: Date;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Request object for metric submission.
|
|
29
|
-
*/
|
|
30
|
-
export declare class MetricRequest {
|
|
31
|
-
requestId: string;
|
|
32
|
-
metricId: string;
|
|
33
|
-
value: number;
|
|
34
|
-
metricType?: string;
|
|
35
|
-
metadata: Record<string, any>;
|
|
36
|
-
agentId?: string;
|
|
37
|
-
reviewingAgentId?: string;
|
|
38
|
-
taskId?: string;
|
|
39
|
-
constructor(data: MetricRequestData);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Response object for metric submissions.
|
|
43
|
-
*/
|
|
44
|
-
export declare class MetricResponse {
|
|
45
|
-
id: number;
|
|
46
|
-
agentId: string;
|
|
47
|
-
accountId: string;
|
|
48
|
-
requestId: string;
|
|
49
|
-
metricId: string;
|
|
50
|
-
metricType?: string;
|
|
51
|
-
value: number;
|
|
52
|
-
metadata: Record<string, any>;
|
|
53
|
-
reviewingAgentId?: string;
|
|
54
|
-
taskId?: string;
|
|
55
|
-
createdAt: Date;
|
|
56
|
-
constructor(data: MetricResponseData);
|
|
57
|
-
}
|
|
58
|
-
export interface SubmitMetricOptions {
|
|
59
|
-
requestId: string;
|
|
60
|
-
metricId: string;
|
|
61
|
-
value: number;
|
|
62
|
-
metricType?: string;
|
|
63
|
-
metadata?: Record<string, any>;
|
|
64
|
-
agentId?: string;
|
|
65
|
-
reviewingAgentId?: string;
|
|
66
|
-
taskId?: string;
|
|
67
|
-
endpoint?: string;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Submit a metric for performance tracking and monitoring.
|
|
71
|
-
*
|
|
72
|
-
* This function sends a metric to the coalex-ai-creator service to be stored
|
|
73
|
-
* and made available for analysis and reporting.
|
|
74
|
-
*
|
|
75
|
-
* @param options - Metric submission options
|
|
76
|
-
* @returns Promise that resolves to MetricResponse
|
|
77
|
-
* @throws Error if required parameters are missing or the request fails
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```typescript
|
|
81
|
-
* import { register, submitMetric } from '@coalex/sdk';
|
|
82
|
-
*
|
|
83
|
-
* // Register coalex first
|
|
84
|
-
* register({ agentId: 'your-agent-id' });
|
|
85
|
-
*
|
|
86
|
-
* // Submit a precision metric
|
|
87
|
-
* const response = await submitMetric({
|
|
88
|
-
* requestId: 'req_12345',
|
|
89
|
-
* metricId: 'model_precision',
|
|
90
|
-
* value: 0.92,
|
|
91
|
-
* metricType: 'precision',
|
|
92
|
-
* metadata: {
|
|
93
|
-
* modelVersion: 'v2.1.0',
|
|
94
|
-
* dataset: 'test_set_v1'
|
|
95
|
-
* }
|
|
96
|
-
* });
|
|
97
|
-
*
|
|
98
|
-
* console.log(`Metric stored with ID: ${response.id}`);
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
export declare function submitMetric(options: SubmitMetricOptions): Promise<MetricResponse>;
|
package/dist/metrics.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Coalex metrics functionality for performance tracking and monitoring.
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.MetricResponse = exports.MetricRequest = void 0;
|
|
7
|
-
exports.submitMetric = submitMetric;
|
|
8
|
-
const api_1 = require("@opentelemetry/api");
|
|
9
|
-
const DEFAULT_CREATOR_ENDPOINT = 'https://creator.coalex.ai';
|
|
10
|
-
/**
|
|
11
|
-
* Request object for metric submission.
|
|
12
|
-
*/
|
|
13
|
-
class MetricRequest {
|
|
14
|
-
constructor(data) {
|
|
15
|
-
this.requestId = data.requestId;
|
|
16
|
-
this.metricId = data.metricId;
|
|
17
|
-
this.value = data.value;
|
|
18
|
-
this.metricType = data.metricType;
|
|
19
|
-
this.metadata = data.metadata || {};
|
|
20
|
-
this.agentId = data.agentId;
|
|
21
|
-
this.reviewingAgentId = data.reviewingAgentId;
|
|
22
|
-
this.taskId = data.taskId;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.MetricRequest = MetricRequest;
|
|
26
|
-
/**
|
|
27
|
-
* Response object for metric submissions.
|
|
28
|
-
*/
|
|
29
|
-
class MetricResponse {
|
|
30
|
-
constructor(data) {
|
|
31
|
-
this.id = data.id;
|
|
32
|
-
this.agentId = data.agentId;
|
|
33
|
-
this.accountId = data.accountId;
|
|
34
|
-
this.requestId = data.requestId;
|
|
35
|
-
this.metricId = data.metricId;
|
|
36
|
-
this.metricType = data.metricType;
|
|
37
|
-
this.value = data.value;
|
|
38
|
-
this.metadata = data.metadata;
|
|
39
|
-
this.reviewingAgentId = data.reviewingAgentId;
|
|
40
|
-
this.taskId = data.taskId;
|
|
41
|
-
this.createdAt = data.createdAt;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.MetricResponse = MetricResponse;
|
|
45
|
-
/**
|
|
46
|
-
* Extract agent_id from the current tracer provider's resource.
|
|
47
|
-
*/
|
|
48
|
-
function getAgentIdFromTracer() {
|
|
49
|
-
try {
|
|
50
|
-
const tracerProvider = api_1.trace.getTracerProvider();
|
|
51
|
-
if (tracerProvider && tracerProvider.resource) {
|
|
52
|
-
const attributes = tracerProvider.resource.attributes;
|
|
53
|
-
return attributes['coalex.agent_id'];
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
console.warn('Failed to get agent_id from tracer:', error);
|
|
58
|
-
}
|
|
59
|
-
return undefined;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Submit a metric for performance tracking and monitoring.
|
|
63
|
-
*
|
|
64
|
-
* This function sends a metric to the coalex-ai-creator service to be stored
|
|
65
|
-
* and made available for analysis and reporting.
|
|
66
|
-
*
|
|
67
|
-
* @param options - Metric submission options
|
|
68
|
-
* @returns Promise that resolves to MetricResponse
|
|
69
|
-
* @throws Error if required parameters are missing or the request fails
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```typescript
|
|
73
|
-
* import { register, submitMetric } from '@coalex/sdk';
|
|
74
|
-
*
|
|
75
|
-
* // Register coalex first
|
|
76
|
-
* register({ agentId: 'your-agent-id' });
|
|
77
|
-
*
|
|
78
|
-
* // Submit a precision metric
|
|
79
|
-
* const response = await submitMetric({
|
|
80
|
-
* requestId: 'req_12345',
|
|
81
|
-
* metricId: 'model_precision',
|
|
82
|
-
* value: 0.92,
|
|
83
|
-
* metricType: 'precision',
|
|
84
|
-
* metadata: {
|
|
85
|
-
* modelVersion: 'v2.1.0',
|
|
86
|
-
* dataset: 'test_set_v1'
|
|
87
|
-
* }
|
|
88
|
-
* });
|
|
89
|
-
*
|
|
90
|
-
* console.log(`Metric stored with ID: ${response.id}`);
|
|
91
|
-
* ```
|
|
92
|
-
*/
|
|
93
|
-
async function submitMetric(options) {
|
|
94
|
-
const { requestId, metricId, value, metricType, metadata, reviewingAgentId, taskId, endpoint = DEFAULT_CREATOR_ENDPOINT } = options;
|
|
95
|
-
if (!requestId) {
|
|
96
|
-
throw new Error('requestId is required');
|
|
97
|
-
}
|
|
98
|
-
if (!metricId) {
|
|
99
|
-
throw new Error('metricId is required');
|
|
100
|
-
}
|
|
101
|
-
if (value === null || value === undefined) {
|
|
102
|
-
throw new Error('value is required');
|
|
103
|
-
}
|
|
104
|
-
// Ensure value is a number
|
|
105
|
-
const numericValue = Number(value);
|
|
106
|
-
if (isNaN(numericValue)) {
|
|
107
|
-
throw new Error('value must be a numeric type');
|
|
108
|
-
}
|
|
109
|
-
// Auto-detect agent_id from tracer if not provided
|
|
110
|
-
let agentId = options.agentId;
|
|
111
|
-
if (!agentId) {
|
|
112
|
-
agentId = getAgentIdFromTracer();
|
|
113
|
-
if (!agentId) {
|
|
114
|
-
throw new Error('agentId is required. Either pass it explicitly or ensure register() was called first.');
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
// Create metric request
|
|
118
|
-
const metricRequest = new MetricRequest({
|
|
119
|
-
requestId,
|
|
120
|
-
metricId,
|
|
121
|
-
value: numericValue,
|
|
122
|
-
metricType,
|
|
123
|
-
metadata,
|
|
124
|
-
agentId,
|
|
125
|
-
reviewingAgentId,
|
|
126
|
-
taskId
|
|
127
|
-
});
|
|
128
|
-
// Prepare request payload
|
|
129
|
-
const payload = {
|
|
130
|
-
request_id: metricRequest.requestId,
|
|
131
|
-
metric_id: metricRequest.metricId,
|
|
132
|
-
value: metricRequest.value,
|
|
133
|
-
agent_id: metricRequest.agentId
|
|
134
|
-
};
|
|
135
|
-
// Add optional fields if provided
|
|
136
|
-
if (metricRequest.metricType) {
|
|
137
|
-
payload.metric_type = metricRequest.metricType;
|
|
138
|
-
}
|
|
139
|
-
if (metricRequest.metadata && Object.keys(metricRequest.metadata).length > 0) {
|
|
140
|
-
payload.metadata = metricRequest.metadata;
|
|
141
|
-
}
|
|
142
|
-
if (metricRequest.reviewingAgentId) {
|
|
143
|
-
payload.reviewing_agent_id = metricRequest.reviewingAgentId;
|
|
144
|
-
}
|
|
145
|
-
if (metricRequest.taskId) {
|
|
146
|
-
payload.task_id = metricRequest.taskId;
|
|
147
|
-
}
|
|
148
|
-
// Prepare headers for authentication
|
|
149
|
-
const headers = {
|
|
150
|
-
'Authorization': `Bearer ${agentId}`,
|
|
151
|
-
'Content-Type': 'application/json'
|
|
152
|
-
};
|
|
153
|
-
// Build URL
|
|
154
|
-
const url = `${endpoint}/metrics`;
|
|
155
|
-
console.log(`Submitting metric ${metricId} for request ${requestId} (agent: ${agentId})`);
|
|
156
|
-
try {
|
|
157
|
-
// Make HTTP request
|
|
158
|
-
const response = await fetch(url, {
|
|
159
|
-
method: 'POST',
|
|
160
|
-
headers,
|
|
161
|
-
body: JSON.stringify(payload)
|
|
162
|
-
});
|
|
163
|
-
// Check response status
|
|
164
|
-
if (!response.ok) {
|
|
165
|
-
const errorText = await response.text();
|
|
166
|
-
throw new Error(`HTTP ${response.status}: ${errorText}`);
|
|
167
|
-
}
|
|
168
|
-
// Parse response
|
|
169
|
-
const responseData = await response.json();
|
|
170
|
-
// Parse created_at timestamp
|
|
171
|
-
const createdAt = new Date(responseData.created_at);
|
|
172
|
-
const metricResponse = new MetricResponse({
|
|
173
|
-
id: responseData.id,
|
|
174
|
-
agentId: responseData.agent_id,
|
|
175
|
-
accountId: responseData.account_id,
|
|
176
|
-
requestId: responseData.request_id,
|
|
177
|
-
metricId: responseData.metric_id,
|
|
178
|
-
metricType: responseData.metric_type,
|
|
179
|
-
value: responseData.value,
|
|
180
|
-
metadata: responseData.metadata || {},
|
|
181
|
-
reviewingAgentId: responseData.reviewing_agent_id,
|
|
182
|
-
taskId: responseData.task_id,
|
|
183
|
-
createdAt
|
|
184
|
-
});
|
|
185
|
-
console.log(`Metric ${metricId} submitted successfully. ID: ${metricResponse.id}`);
|
|
186
|
-
return metricResponse;
|
|
187
|
-
}
|
|
188
|
-
catch (error) {
|
|
189
|
-
console.error(`Failed to submit metric ${metricId} for request ${requestId}:`, error);
|
|
190
|
-
throw error;
|
|
191
|
-
}
|
|
192
|
-
}
|
package/dist/otel/register.d.ts
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Coalex OpenTelemetry registration and setup.
|
|
3
|
-
*/
|
|
4
|
-
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
|
5
|
-
export interface RegisterOptions {
|
|
6
|
-
agentId: string;
|
|
7
|
-
endpoint?: string;
|
|
8
|
-
serviceName?: string;
|
|
9
|
-
serviceVersion?: string;
|
|
10
|
-
additionalAttributes?: Record<string, string | number | boolean>;
|
|
11
|
-
}
|
|
12
|
-
export interface CoalexContextOptions {
|
|
13
|
-
requestId: string;
|
|
14
|
-
promptVersion: string;
|
|
15
|
-
spanName?: string;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Register Coalex OpenTelemetry tracing with Coalex OTLP endpoint.
|
|
19
|
-
*
|
|
20
|
-
* @param options - Configuration options for tracing
|
|
21
|
-
* @returns Configured TracerProvider instance
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* import { register } from '@coalex/sdk';
|
|
26
|
-
*
|
|
27
|
-
* const tracerProvider = register({
|
|
28
|
-
* agentId: 'YOUR_AGENT_ID'
|
|
29
|
-
* });
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
export declare function register(options: RegisterOptions): NodeTracerProvider;
|
|
33
|
-
/**
|
|
34
|
-
* Add request context to the current span.
|
|
35
|
-
*
|
|
36
|
-
* This function adds attributes to the current active span if one exists.
|
|
37
|
-
* For better attribute propagation, consider using coalexContext() instead.
|
|
38
|
-
*
|
|
39
|
-
* @param requestId - Unique request identifier
|
|
40
|
-
* @param promptVersion - Version of the prompt being used
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* import { addRequestContext } from '@coalex/sdk';
|
|
45
|
-
*
|
|
46
|
-
* addRequestContext('req_001', 'v1.0.0');
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
export declare function addRequestContext(requestId: string, promptVersion: string): void;
|
|
50
|
-
/**
|
|
51
|
-
* Execute a function within a Coalex context that ensures attributes are propagated to child spans.
|
|
52
|
-
*
|
|
53
|
-
* @param options - Context options including requestId and promptVersion
|
|
54
|
-
* @param fn - The function to execute within the context
|
|
55
|
-
* @returns Promise that resolves with the function result
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```typescript
|
|
59
|
-
* import { coalexContext } from '@coalex/sdk';
|
|
60
|
-
*
|
|
61
|
-
* await coalexContext(
|
|
62
|
-
* {
|
|
63
|
-
* requestId: 'req_001',
|
|
64
|
-
* promptVersion: 'v1.0.0'
|
|
65
|
-
* },
|
|
66
|
-
* async () => {
|
|
67
|
-
* // Any OpenTelemetry instrumented calls here will inherit the attributes
|
|
68
|
-
* const response = await someApiCall();
|
|
69
|
-
* return response;
|
|
70
|
-
* }
|
|
71
|
-
* );
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
export declare function coalexContext<T>(options: CoalexContextOptions, fn: () => Promise<T> | T): Promise<T>;
|
|
75
|
-
/**
|
|
76
|
-
* Synchronous version of coalexContext for non-async operations.
|
|
77
|
-
*
|
|
78
|
-
* @param options - Context options including requestId and promptVersion
|
|
79
|
-
* @param fn - The function to execute within the context
|
|
80
|
-
* @returns The function result
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* ```typescript
|
|
84
|
-
* import { coalexContextSync } from '@coalex/sdk';
|
|
85
|
-
*
|
|
86
|
-
* const result = coalexContextSync(
|
|
87
|
-
* {
|
|
88
|
-
* requestId: 'req_001',
|
|
89
|
-
* promptVersion: 'v1.0.0'
|
|
90
|
-
* },
|
|
91
|
-
* () => {
|
|
92
|
-
* // Synchronous operations here
|
|
93
|
-
* return someValue;
|
|
94
|
-
* }
|
|
95
|
-
* );
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
export declare function coalexContextSync<T>(options: CoalexContextOptions, fn: () => T): T;
|
package/dist/otel/register.js
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Coalex OpenTelemetry registration and setup.
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.register = register;
|
|
7
|
-
exports.addRequestContext = addRequestContext;
|
|
8
|
-
exports.coalexContext = coalexContext;
|
|
9
|
-
exports.coalexContextSync = coalexContextSync;
|
|
10
|
-
const api_1 = require("@opentelemetry/api");
|
|
11
|
-
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
12
|
-
const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
13
|
-
const exporter_trace_otlp_http_1 = require("@opentelemetry/exporter-trace-otlp-http");
|
|
14
|
-
const resources_1 = require("@opentelemetry/resources");
|
|
15
|
-
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
16
|
-
const DEFAULT_COALEX_ENDPOINT = 'https://traces.coalex.ai/v1/traces';
|
|
17
|
-
/**
|
|
18
|
-
* Register Coalex OpenTelemetry tracing with Coalex OTLP endpoint.
|
|
19
|
-
*
|
|
20
|
-
* @param options - Configuration options for tracing
|
|
21
|
-
* @returns Configured TracerProvider instance
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* import { register } from '@coalex/sdk';
|
|
26
|
-
*
|
|
27
|
-
* const tracerProvider = register({
|
|
28
|
-
* agentId: 'YOUR_AGENT_ID'
|
|
29
|
-
* });
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
function register(options) {
|
|
33
|
-
const { agentId, endpoint = DEFAULT_COALEX_ENDPOINT, serviceName = 'coalex-service', serviceVersion = '0.1.0', additionalAttributes = {} } = options;
|
|
34
|
-
if (!agentId) {
|
|
35
|
-
throw new Error('agentId is required');
|
|
36
|
-
}
|
|
37
|
-
// Create resource with service information
|
|
38
|
-
const resourceAttributes = {
|
|
39
|
-
[semantic_conventions_1.ATTR_SERVICE_NAME]: serviceName,
|
|
40
|
-
[semantic_conventions_1.ATTR_SERVICE_VERSION]: serviceVersion,
|
|
41
|
-
'coalex.agent_id': agentId,
|
|
42
|
-
...additionalAttributes
|
|
43
|
-
};
|
|
44
|
-
const resource = new resources_1.Resource(resourceAttributes);
|
|
45
|
-
// Create tracer provider
|
|
46
|
-
const tracerProvider = new sdk_trace_node_1.NodeTracerProvider({
|
|
47
|
-
resource
|
|
48
|
-
});
|
|
49
|
-
// Create OTLP exporter with agent_id in headers for auth
|
|
50
|
-
const exporter = new exporter_trace_otlp_http_1.OTLPTraceExporter({
|
|
51
|
-
url: endpoint,
|
|
52
|
-
headers: {
|
|
53
|
-
'Authorization': `Bearer ${agentId}`,
|
|
54
|
-
'X-Agent-ID': agentId
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
// Create batch span processor
|
|
58
|
-
const spanProcessor = new sdk_trace_base_1.BatchSpanProcessor(exporter);
|
|
59
|
-
tracerProvider.addSpanProcessor(spanProcessor);
|
|
60
|
-
// Set as global tracer provider
|
|
61
|
-
tracerProvider.register();
|
|
62
|
-
console.log(`Coalex tracing registered for agent ${agentId} -> ${endpoint}`);
|
|
63
|
-
return tracerProvider;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Add request context to the current span.
|
|
67
|
-
*
|
|
68
|
-
* This function adds attributes to the current active span if one exists.
|
|
69
|
-
* For better attribute propagation, consider using coalexContext() instead.
|
|
70
|
-
*
|
|
71
|
-
* @param requestId - Unique request identifier
|
|
72
|
-
* @param promptVersion - Version of the prompt being used
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* ```typescript
|
|
76
|
-
* import { addRequestContext } from '@coalex/sdk';
|
|
77
|
-
*
|
|
78
|
-
* addRequestContext('req_001', 'v1.0.0');
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
function addRequestContext(requestId, promptVersion) {
|
|
82
|
-
const span = api_1.trace.getActiveSpan();
|
|
83
|
-
if (span && span.isRecording()) {
|
|
84
|
-
span.setAttribute('session.id', requestId);
|
|
85
|
-
span.setAttribute('prompt.version', promptVersion);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
console.warn('addRequestContext called without an active span. ' +
|
|
89
|
-
'Consider using coalexContext() for better attribute propagation.');
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Execute a function within a Coalex context that ensures attributes are propagated to child spans.
|
|
94
|
-
*
|
|
95
|
-
* @param options - Context options including requestId and promptVersion
|
|
96
|
-
* @param fn - The function to execute within the context
|
|
97
|
-
* @returns Promise that resolves with the function result
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* import { coalexContext } from '@coalex/sdk';
|
|
102
|
-
*
|
|
103
|
-
* await coalexContext(
|
|
104
|
-
* {
|
|
105
|
-
* requestId: 'req_001',
|
|
106
|
-
* promptVersion: 'v1.0.0'
|
|
107
|
-
* },
|
|
108
|
-
* async () => {
|
|
109
|
-
* // Any OpenTelemetry instrumented calls here will inherit the attributes
|
|
110
|
-
* const response = await someApiCall();
|
|
111
|
-
* return response;
|
|
112
|
-
* }
|
|
113
|
-
* );
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
async function coalexContext(options, fn) {
|
|
117
|
-
const { requestId, promptVersion, spanName = 'coalex_operation' } = options;
|
|
118
|
-
const tracer = api_1.trace.getTracer('coalex');
|
|
119
|
-
// Get agent_id from the current tracer provider's resource
|
|
120
|
-
let agentId;
|
|
121
|
-
const tracerProvider = api_1.trace.getTracerProvider();
|
|
122
|
-
if (tracerProvider && tracerProvider.resource) {
|
|
123
|
-
const attributes = tracerProvider.resource.attributes;
|
|
124
|
-
agentId = attributes['coalex.agent_id'];
|
|
125
|
-
}
|
|
126
|
-
return tracer.startActiveSpan(spanName, async (span) => {
|
|
127
|
-
try {
|
|
128
|
-
// Set Coalex attributes
|
|
129
|
-
if (agentId) {
|
|
130
|
-
span.setAttribute('account_id', agentId);
|
|
131
|
-
}
|
|
132
|
-
span.setAttribute('session.id', requestId);
|
|
133
|
-
span.setAttribute('prompt.version', promptVersion);
|
|
134
|
-
// Execute the function
|
|
135
|
-
const result = await fn();
|
|
136
|
-
span.end();
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
catch (error) {
|
|
140
|
-
// Record the error and re-throw
|
|
141
|
-
span.recordException(error);
|
|
142
|
-
span.end();
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Synchronous version of coalexContext for non-async operations.
|
|
149
|
-
*
|
|
150
|
-
* @param options - Context options including requestId and promptVersion
|
|
151
|
-
* @param fn - The function to execute within the context
|
|
152
|
-
* @returns The function result
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```typescript
|
|
156
|
-
* import { coalexContextSync } from '@coalex/sdk';
|
|
157
|
-
*
|
|
158
|
-
* const result = coalexContextSync(
|
|
159
|
-
* {
|
|
160
|
-
* requestId: 'req_001',
|
|
161
|
-
* promptVersion: 'v1.0.0'
|
|
162
|
-
* },
|
|
163
|
-
* () => {
|
|
164
|
-
* // Synchronous operations here
|
|
165
|
-
* return someValue;
|
|
166
|
-
* }
|
|
167
|
-
* );
|
|
168
|
-
* ```
|
|
169
|
-
*/
|
|
170
|
-
function coalexContextSync(options, fn) {
|
|
171
|
-
const { requestId, promptVersion, spanName = 'coalex_operation' } = options;
|
|
172
|
-
const tracer = api_1.trace.getTracer('coalex');
|
|
173
|
-
// Get agent_id from the current tracer provider's resource
|
|
174
|
-
let agentId;
|
|
175
|
-
const tracerProvider = api_1.trace.getTracerProvider();
|
|
176
|
-
if (tracerProvider && tracerProvider.resource) {
|
|
177
|
-
const attributes = tracerProvider.resource.attributes;
|
|
178
|
-
agentId = attributes['coalex.agent_id'];
|
|
179
|
-
}
|
|
180
|
-
return tracer.startActiveSpan(spanName, (span) => {
|
|
181
|
-
try {
|
|
182
|
-
// Set Coalex attributes
|
|
183
|
-
if (agentId) {
|
|
184
|
-
span.setAttribute('account_id', agentId);
|
|
185
|
-
}
|
|
186
|
-
span.setAttribute('session.id', requestId);
|
|
187
|
-
span.setAttribute('prompt.version', promptVersion);
|
|
188
|
-
// Execute the function
|
|
189
|
-
const result = fn();
|
|
190
|
-
span.end();
|
|
191
|
-
return result;
|
|
192
|
-
}
|
|
193
|
-
catch (error) {
|
|
194
|
-
// Record the error and re-throw
|
|
195
|
-
span.recordException(error);
|
|
196
|
-
span.end();
|
|
197
|
-
throw error;
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
}
|