@agentmark-ai/shared-utils 0.1.0

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,478 @@
1
+ declare const generateUnique8CharString: () => string;
2
+
3
+ declare function createSignature(secret: string | undefined, payload: string): Promise<string>;
4
+ declare function verifySignature(secret: string, header: string, payload: string): Promise<boolean>;
5
+
6
+ type Option = {
7
+ label: string;
8
+ value: string;
9
+ };
10
+ type ModelSettingsTypeSlider = {
11
+ minimum: number;
12
+ maximum: number;
13
+ default: number;
14
+ multipleOf: number;
15
+ type: "slider" | "number";
16
+ ui?: "slider";
17
+ };
18
+ type ModelSettingsTypeAspectRatio = {
19
+ type: "string";
20
+ ui?: "aspectRatio";
21
+ default: string;
22
+ };
23
+ type ModelSettingsTypeImageSize = {
24
+ type: "string";
25
+ ui?: "imageSize";
26
+ default: string;
27
+ };
28
+ type ModelSettingsTypeSelect = {
29
+ type: "string";
30
+ ui?: "select";
31
+ options: Option[];
32
+ default: string;
33
+ };
34
+ type AgentmarkModelSettingsConfig = {
35
+ label: string;
36
+ order: number;
37
+ deafult: any;
38
+ } & (ModelSettingsTypeSlider | ModelSettingsTypeSelect | ModelSettingsTypeImageSize | ModelSettingsTypeAspectRatio);
39
+ type AgentmarkModelSettingsSchema = {
40
+ [key: string]: AgentmarkModelSettingsConfig;
41
+ };
42
+ type AgentmarkModelConfig = {
43
+ label: string;
44
+ cost: {
45
+ inputCost: number;
46
+ outputCost: number;
47
+ unitScale: number;
48
+ };
49
+ settings: AgentmarkModelSettingsSchema;
50
+ };
51
+ type AgentmarkModelSchema = {
52
+ [key: string]: AgentmarkModelConfig;
53
+ };
54
+ type McpUrlServerConfig = {
55
+ url: string;
56
+ headers?: Record<string, string>;
57
+ };
58
+ type McpStdioServerConfig = {
59
+ command: string;
60
+ args?: string[];
61
+ cwd?: string;
62
+ env?: Record<string, string>;
63
+ };
64
+ type McpServerConfig = McpUrlServerConfig | McpStdioServerConfig;
65
+ type McpServers = Record<string, McpServerConfig>;
66
+ type AgentmarkConfig = {
67
+ $schema?: string;
68
+ mdxVersion?: "1.0" | "0.0";
69
+ agentmarkPath: string;
70
+ modelSchemas?: AgentmarkModelSchema;
71
+ version: string;
72
+ builtInModels?: string[];
73
+ mcpServers?: McpServers;
74
+ evals: string[];
75
+ };
76
+
77
+ declare function toFrontMatter(content: {
78
+ [key: string]: any;
79
+ }): string;
80
+
81
+ type TextPromptFrontmatterV1_0 = {
82
+ path: string;
83
+ version: "1.0";
84
+ text_config: {
85
+ model_name: string;
86
+ tools?: string[];
87
+ };
88
+ input_schema?: any;
89
+ };
90
+ type ObjectPromptFrontmatterV1_0 = {
91
+ path: string;
92
+ version: "1.0";
93
+ object_config: {
94
+ model_name: string;
95
+ schema: any;
96
+ tools?: Record<string, any>;
97
+ };
98
+ input_schema?: any;
99
+ };
100
+ type ImagePromptFrontmatterV1_0 = {
101
+ path: string;
102
+ version: "1.0";
103
+ image_config: {
104
+ model_name: string;
105
+ tools?: Record<string, any>;
106
+ };
107
+ input_schema?: any;
108
+ };
109
+ type PromptFrontmatterV0 = {
110
+ path: string;
111
+ metadata: {
112
+ model?: {
113
+ settings?: {
114
+ schema?: any;
115
+ tools?: Record<string, any>;
116
+ };
117
+ };
118
+ };
119
+ input_schema?: any;
120
+ version?: "0.0";
121
+ };
122
+ type PromptFrontmatter = PromptFrontmatterV0 | TextPromptFrontmatterV1_0 | ObjectPromptFrontmatterV1_0 | ImagePromptFrontmatterV1_0;
123
+ declare function findPromptFiles(dir: string): Promise<string[]>;
124
+ declare function generateTypeDefinitions(prompts: PromptFrontmatter[]): Promise<string>;
125
+ declare function fetchPromptsFrontmatter(options: {
126
+ local?: number;
127
+ rootDir?: string;
128
+ }): Promise<PromptFrontmatter[]>;
129
+
130
+ declare enum SpanType {
131
+ SPAN = "SPAN",
132
+ GENERATION = "GENERATION",
133
+ EVENT = "EVENT"
134
+ }
135
+ /**
136
+ * Standard message content part types
137
+ */
138
+ interface StandardTextContent {
139
+ type: 'text';
140
+ text: string;
141
+ }
142
+ interface StandardToolCallContent {
143
+ type: 'tool-call';
144
+ toolCallId: string;
145
+ toolName: string;
146
+ args: Record<string, any>;
147
+ }
148
+ interface StandardToolResultContent {
149
+ type: 'tool-result';
150
+ toolCallId: string;
151
+ toolName: string;
152
+ result: any;
153
+ }
154
+ type StandardMessageContent = StandardTextContent | StandardToolCallContent | StandardToolResultContent | string;
155
+ /**
156
+ * Standard message type used throughout the normalizer.
157
+ * Messages are normalized from V4/V5 formats to this standard format.
158
+ *
159
+ * Content can be:
160
+ * - A plain string
161
+ * - An array of content parts (text, tool-call, tool-result)
162
+ */
163
+ interface Message {
164
+ role: string;
165
+ content: StandardMessageContent | StandardMessageContent[];
166
+ }
167
+ interface ToolCall {
168
+ type: string;
169
+ toolCallId: string;
170
+ toolName: string;
171
+ args: Record<string, any>;
172
+ result?: string;
173
+ providerMetadata?: Record<string, any>;
174
+ }
175
+ interface OtelScope {
176
+ name?: string;
177
+ version?: string;
178
+ }
179
+ interface OtelResource {
180
+ attributes?: Record<string, any>;
181
+ }
182
+ interface OtelEvent {
183
+ timeUnixNano: string;
184
+ name: string;
185
+ attributes?: Record<string, any>;
186
+ }
187
+ interface OtelLink {
188
+ traceId: string;
189
+ spanId: string;
190
+ traceState?: string;
191
+ attributes?: Record<string, any>;
192
+ }
193
+ interface OtelSpan {
194
+ traceId: string;
195
+ spanId: string;
196
+ parentSpanId?: string;
197
+ traceState?: string;
198
+ name: string;
199
+ kind: number;
200
+ startTimeUnixNano: string;
201
+ endTimeUnixNano: string;
202
+ attributes?: Record<string, any>;
203
+ events?: OtelEvent[];
204
+ links?: OtelLink[];
205
+ status?: {
206
+ code: number;
207
+ message?: string;
208
+ };
209
+ }
210
+ interface NormalizedSpan {
211
+ traceId: string;
212
+ spanId: string;
213
+ parentSpanId?: string;
214
+ traceState?: string;
215
+ type: SpanType;
216
+ startTime: number;
217
+ endTime?: number;
218
+ duration: number;
219
+ name: string;
220
+ kind: string;
221
+ serviceName?: string;
222
+ statusCode: string;
223
+ statusMessage?: string;
224
+ model?: string;
225
+ inputTokens?: number;
226
+ outputTokens?: number;
227
+ totalTokens?: number;
228
+ reasoningTokens?: number;
229
+ cost?: number;
230
+ input?: Message[];
231
+ output?: string;
232
+ outputObject?: Record<string, any>;
233
+ toolCalls?: ToolCall[];
234
+ finishReason?: string;
235
+ settings?: {
236
+ temperature?: number;
237
+ maxTokens?: number;
238
+ topP?: number;
239
+ presencePenalty?: number;
240
+ frequencyPenalty?: number;
241
+ };
242
+ sessionId?: string;
243
+ sessionName?: string;
244
+ userId?: string;
245
+ traceName?: string;
246
+ datasetRunId?: string;
247
+ datasetRunName?: string;
248
+ datasetPath?: string;
249
+ datasetItemName?: string;
250
+ datasetExpectedOutput?: string;
251
+ promptName?: string;
252
+ props?: string;
253
+ commitSha?: string;
254
+ metadata?: Record<string, string>;
255
+ resourceAttributes: Record<string, any>;
256
+ spanAttributes: Record<string, any>;
257
+ events: Array<{
258
+ timestamp: number;
259
+ name: string;
260
+ attributes: Record<string, any>;
261
+ }>;
262
+ links: Array<{
263
+ traceId: string;
264
+ spanId: string;
265
+ traceState?: string;
266
+ attributes?: Record<string, any>;
267
+ }>;
268
+ }
269
+ interface AttributeExtractor {
270
+ extractModel(attributes: Record<string, any>): string | undefined;
271
+ extractInput(attributes: Record<string, any>): Message[] | undefined;
272
+ extractOutput(attributes: Record<string, any>): string | undefined;
273
+ extractTokens(attributes: Record<string, any>): {
274
+ input?: number;
275
+ output?: number;
276
+ total?: number;
277
+ reasoning?: number;
278
+ };
279
+ extractMetadata(attributes: Record<string, any>): Partial<NormalizedSpan>;
280
+ extractToolCalls(attributes: Record<string, any>): ToolCall[] | undefined;
281
+ extractOutputObject(attributes: Record<string, any>): Record<string, any> | undefined;
282
+ extractFinishReason(attributes: Record<string, any>): string | undefined;
283
+ extractSettings(attributes: Record<string, any>): NormalizedSpan['settings'];
284
+ }
285
+ interface ScopeTransformer {
286
+ classify(span: OtelSpan, attributes: Record<string, any>): SpanType;
287
+ transform(span: OtelSpan, attributes: Record<string, any>): Partial<NormalizedSpan>;
288
+ }
289
+
290
+ /**
291
+ * OTLP attribute value types
292
+ */
293
+ interface OtlpAttributeValue {
294
+ stringValue?: string;
295
+ intValue?: string | number;
296
+ doubleValue?: number;
297
+ boolValue?: boolean;
298
+ arrayValue?: {
299
+ values?: OtlpAttributeValue[];
300
+ };
301
+ bytesValue?: string | Uint8Array;
302
+ }
303
+ /**
304
+ * OTLP attribute structure
305
+ */
306
+ interface OtlpAttribute {
307
+ key: string;
308
+ value: OtlpAttributeValue;
309
+ }
310
+ /**
311
+ * Raw OTLP span structure
312
+ */
313
+ interface OtlpSpan {
314
+ traceId: string;
315
+ spanId: string;
316
+ parentSpanId?: string;
317
+ traceState?: string;
318
+ name: string;
319
+ kind: number;
320
+ startTimeUnixNano: string;
321
+ endTimeUnixNano: string;
322
+ attributes?: OtlpAttribute[];
323
+ events?: OtlpEvent[];
324
+ links?: OtlpLink[];
325
+ status?: {
326
+ code: number;
327
+ message?: string;
328
+ };
329
+ droppedAttributesCount?: number;
330
+ droppedEventsCount?: number;
331
+ droppedLinksCount?: number;
332
+ }
333
+ /**
334
+ * Raw OTLP event structure
335
+ */
336
+ interface OtlpEvent {
337
+ timeUnixNano: string;
338
+ name: string;
339
+ attributes?: OtlpAttribute[];
340
+ droppedAttributesCount?: number;
341
+ }
342
+ /**
343
+ * Raw OTLP link structure
344
+ */
345
+ interface OtlpLink {
346
+ traceId: string;
347
+ spanId: string;
348
+ traceState?: string;
349
+ attributes?: OtlpAttribute[];
350
+ droppedAttributesCount?: number;
351
+ }
352
+ /**
353
+ * Raw OTLP resource structure
354
+ */
355
+ interface OtlpResource {
356
+ attributes?: OtlpAttribute[];
357
+ droppedAttributesCount?: number;
358
+ }
359
+ /**
360
+ * Raw OTLP scope structure
361
+ */
362
+ interface OtlpScope {
363
+ name?: string;
364
+ version?: string;
365
+ }
366
+ /**
367
+ * Raw OTLP scope spans structure
368
+ */
369
+ interface OtlpScopeSpans {
370
+ scope?: OtlpScope;
371
+ spans: OtlpSpan[];
372
+ }
373
+ /**
374
+ * Raw OTLP resource spans structure
375
+ */
376
+ interface OtlpResourceSpans {
377
+ resource?: OtlpResource;
378
+ scopeSpans: OtlpScopeSpans[];
379
+ }
380
+ /**
381
+ * Convert OTLP attribute array to flat Record<string, any>
382
+ */
383
+ declare function convertOtlpAttributes(attributes?: OtlpAttribute[]): Record<string, any>;
384
+ /**
385
+ * Extract resource, scope, and span from OTLP structure
386
+ */
387
+ declare function extractResourceScopeSpan(resourceSpans: OtlpResourceSpans): Array<{
388
+ resource: OtelResource;
389
+ scope: OtelScope;
390
+ span: OtelSpan;
391
+ }>;
392
+
393
+ declare class TransformerRegistry {
394
+ private transformers;
395
+ private defaultTransformer;
396
+ register(scope: string, transformer: ScopeTransformer): void;
397
+ setDefault(transformer: ScopeTransformer): void;
398
+ getTransformer(scope: string): ScopeTransformer | null;
399
+ }
400
+ declare const registry: TransformerRegistry;
401
+
402
+ declare class TypeClassifier {
403
+ classify(span: OtelSpan, attributes: Record<string, any>): SpanType;
404
+ }
405
+ declare const typeClassifier: TypeClassifier;
406
+
407
+ interface TokenKeys {
408
+ inputKey?: string;
409
+ outputKey?: string;
410
+ totalKey?: string;
411
+ promptKey?: string;
412
+ completionKey?: string;
413
+ reasoningKey?: string;
414
+ }
415
+ interface TokenCounts {
416
+ inputTokens?: number;
417
+ outputTokens?: number;
418
+ totalTokens?: number;
419
+ reasoningTokens?: number;
420
+ }
421
+ declare function parseTokens(attributes: Record<string, any>, keys: TokenKeys): TokenCounts;
422
+
423
+ declare function parseMetadata(attributes: Record<string, any>, prefix?: string): Partial<NormalizedSpan>;
424
+ /**
425
+ * Extracts custom metadata keys from attributes that start with the given prefix.
426
+ * Excludes known metadata fields that are already extracted as normalized fields.
427
+ *
428
+ * @param attributes - The attributes to extract from
429
+ * @param prefix - The prefix to look for (default: 'agentmark.metadata.')
430
+ * @returns A record of custom metadata keys (with prefix stripped) to string values
431
+ */
432
+ declare function extractCustomMetadata(attributes: Record<string, any>, prefix?: string): Record<string, string>;
433
+
434
+ /**
435
+ * Parse attributes with 'agentmark.' prefix (not 'agentmark.metadata.')
436
+ * These are direct context attributes set by the AgentMark SDK.
437
+ *
438
+ * @param attributes - The attributes to extract from
439
+ * @param prefix - The prefix to look for (default: 'agentmark.')
440
+ * @returns Partial NormalizedSpan with extracted fields in camelCase
441
+ */
442
+ declare function parseAgentMarkAttributes(attributes: Record<string, any>, prefix?: string): Partial<NormalizedSpan>;
443
+
444
+ declare class AiSdkTransformer implements ScopeTransformer {
445
+ private strategies;
446
+ constructor();
447
+ classify(span: OtelSpan, _attributes: Record<string, any>): SpanType;
448
+ transform(_span: OtelSpan, attributes: Record<string, any>): Partial<NormalizedSpan>;
449
+ }
450
+
451
+ /**
452
+ * AI SDK-specific token extraction helpers
453
+ */
454
+ /**
455
+ * Extracts reasoning tokens from AI SDK's providerMetadata attribute.
456
+ * This is a fallback when ai.usage.reasoningTokens is not directly available.
457
+ */
458
+ declare function extractReasoningFromProviderMetadata(attributes: Record<string, any>): number | undefined;
459
+
460
+ type AiSdkVersion = 'v5' | 'v4' | 'unknown';
461
+ declare function detectVersion(attributes: Record<string, any>): AiSdkVersion;
462
+
463
+ declare class MastraTransformer implements ScopeTransformer {
464
+ classify(span: OtelSpan, _attributes: Record<string, any>): SpanType;
465
+ transform(span: OtelSpan, attributes: Record<string, any>): Partial<NormalizedSpan>;
466
+ private extractModelConfig;
467
+ private extractInput;
468
+ private extractStreamResult;
469
+ }
470
+
471
+ declare function normalizeSpan(resource: OtelResource, scope: OtelScope, span: OtelSpan): NormalizedSpan;
472
+ /**
473
+ * Normalize spans from raw OTLP resourceSpans structure
474
+ * This is a higher-level API that accepts raw OTLP format and handles conversion internally
475
+ */
476
+ declare function normalizeOtlpSpans(resourceSpans: OtlpResourceSpans[]): NormalizedSpan[];
477
+
478
+ export { type AgentmarkConfig, type AgentmarkModelConfig, type AgentmarkModelSchema, type AgentmarkModelSettingsConfig, type AgentmarkModelSettingsSchema, AiSdkTransformer, type AiSdkVersion, type AttributeExtractor, MastraTransformer, type McpServerConfig, type McpServers, type McpStdioServerConfig, type McpUrlServerConfig, type Message, type ModelSettingsTypeAspectRatio, type ModelSettingsTypeImageSize, type ModelSettingsTypeSelect, type ModelSettingsTypeSlider, type NormalizedSpan, type OtelEvent, type OtelLink, type OtelResource, type OtelScope, type OtelSpan, type OtlpAttribute, type OtlpAttributeValue, type OtlpEvent, type OtlpLink, type OtlpResource, type OtlpResourceSpans, type OtlpScope, type OtlpScopeSpans, type OtlpSpan, type ScopeTransformer, SpanType, type StandardMessageContent, type StandardTextContent, type StandardToolCallContent, type StandardToolResultContent, type ToolCall, TransformerRegistry, TypeClassifier, convertOtlpAttributes, createSignature, detectVersion, extractCustomMetadata, extractReasoningFromProviderMetadata, extractResourceScopeSpan, fetchPromptsFrontmatter, findPromptFiles, generateTypeDefinitions, generateUnique8CharString, normalizeOtlpSpans, normalizeSpan, parseAgentMarkAttributes, parseMetadata, parseTokens, registry, toFrontMatter, typeClassifier, verifySignature };