autotel-genai 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.
Files changed (108) hide show
  1. package/README.md +149 -0
  2. package/dist/agent/index.cjs +24 -0
  3. package/dist/agent/index.d.cts +3 -0
  4. package/dist/agent/index.d.ts +3 -0
  5. package/dist/agent/index.js +3 -0
  6. package/dist/agent-CqXauTpC.js +951 -0
  7. package/dist/agent-CqXauTpC.js.map +1 -0
  8. package/dist/agent-eo4jY4Xg.cjs +1076 -0
  9. package/dist/agent-eo4jY4Xg.cjs.map +1 -0
  10. package/dist/ai-sdk-bridge.cjs +134 -0
  11. package/dist/ai-sdk-bridge.cjs.map +1 -0
  12. package/dist/ai-sdk-bridge.d.cts +59 -0
  13. package/dist/ai-sdk-bridge.d.cts.map +1 -0
  14. package/dist/ai-sdk-bridge.d.ts +59 -0
  15. package/dist/ai-sdk-bridge.d.ts.map +1 -0
  16. package/dist/ai-sdk-bridge.js +127 -0
  17. package/dist/ai-sdk-bridge.js.map +1 -0
  18. package/dist/attributes--Pdezjj6.js +119 -0
  19. package/dist/attributes--Pdezjj6.js.map +1 -0
  20. package/dist/attributes-DB35Boji.cjs +166 -0
  21. package/dist/attributes-DB35Boji.cjs.map +1 -0
  22. package/dist/attributes-QRj0DPJH.d.ts +106 -0
  23. package/dist/attributes-QRj0DPJH.d.ts.map +1 -0
  24. package/dist/attributes-kyiOdake.d.cts +106 -0
  25. package/dist/attributes-kyiOdake.d.cts.map +1 -0
  26. package/dist/cost-DXTkBUUW.d.cts +72 -0
  27. package/dist/cost-DXTkBUUW.d.cts.map +1 -0
  28. package/dist/cost-DXTkBUUW.d.ts +72 -0
  29. package/dist/cost-DXTkBUUW.d.ts.map +1 -0
  30. package/dist/cost.cjs +138 -0
  31. package/dist/cost.cjs.map +1 -0
  32. package/dist/cost.d.cts +2 -0
  33. package/dist/cost.d.ts +2 -0
  34. package/dist/cost.js +134 -0
  35. package/dist/cost.js.map +1 -0
  36. package/dist/events.cjs +88 -0
  37. package/dist/events.cjs.map +1 -0
  38. package/dist/events.d.cts +89 -0
  39. package/dist/events.d.cts.map +1 -0
  40. package/dist/events.d.ts +89 -0
  41. package/dist/events.d.ts.map +1 -0
  42. package/dist/events.js +84 -0
  43. package/dist/events.js.map +1 -0
  44. package/dist/index-D1gH80Xd.d.ts +403 -0
  45. package/dist/index-D1gH80Xd.d.ts.map +1 -0
  46. package/dist/index-DtjGz80V.d.cts +403 -0
  47. package/dist/index-DtjGz80V.d.cts.map +1 -0
  48. package/dist/index.cjs +72 -0
  49. package/dist/index.d.cts +9 -0
  50. package/dist/index.d.ts +9 -0
  51. package/dist/index.js +10 -0
  52. package/dist/metrics.cjs +166 -0
  53. package/dist/metrics.cjs.map +1 -0
  54. package/dist/metrics.d.cts +52 -0
  55. package/dist/metrics.d.cts.map +1 -0
  56. package/dist/metrics.d.ts +52 -0
  57. package/dist/metrics.d.ts.map +1 -0
  58. package/dist/metrics.js +161 -0
  59. package/dist/metrics.js.map +1 -0
  60. package/dist/semconv-CGiMo_LV.d.cts +190 -0
  61. package/dist/semconv-CGiMo_LV.d.cts.map +1 -0
  62. package/dist/semconv-CGiMo_LV.d.ts +190 -0
  63. package/dist/semconv-CGiMo_LV.d.ts.map +1 -0
  64. package/dist/semconv.cjs +196 -0
  65. package/dist/semconv.cjs.map +1 -0
  66. package/dist/semconv.d.cts +2 -0
  67. package/dist/semconv.d.ts +2 -0
  68. package/dist/semconv.js +186 -0
  69. package/dist/semconv.js.map +1 -0
  70. package/dist/trace.cjs +119 -0
  71. package/dist/trace.cjs.map +1 -0
  72. package/dist/trace.d.cts +42 -0
  73. package/dist/trace.d.cts.map +1 -0
  74. package/dist/trace.d.ts +42 -0
  75. package/dist/trace.d.ts.map +1 -0
  76. package/dist/trace.js +115 -0
  77. package/dist/trace.js.map +1 -0
  78. package/package.json +113 -0
  79. package/src/agent/agent.test.ts +874 -0
  80. package/src/agent/attributes.ts +162 -0
  81. package/src/agent/constants.ts +1 -0
  82. package/src/agent/context.ts +125 -0
  83. package/src/agent/delegation.ts +98 -0
  84. package/src/agent/hash.ts +44 -0
  85. package/src/agent/identity-registry.ts +188 -0
  86. package/src/agent/index.ts +45 -0
  87. package/src/agent/metadata.ts +254 -0
  88. package/src/agent/non-repudiation.ts +70 -0
  89. package/src/agent/privacy.ts +128 -0
  90. package/src/agent/runtime.ts +294 -0
  91. package/src/agent/scoped-tool.ts +221 -0
  92. package/src/agent/session.ts +66 -0
  93. package/src/agent/types.ts +311 -0
  94. package/src/ai-sdk-bridge.test.ts +96 -0
  95. package/src/ai-sdk-bridge.ts +209 -0
  96. package/src/attributes.test.ts +148 -0
  97. package/src/attributes.ts +265 -0
  98. package/src/cost.test.ts +92 -0
  99. package/src/cost.ts +196 -0
  100. package/src/events.test.ts +82 -0
  101. package/src/events.ts +210 -0
  102. package/src/index.ts +178 -0
  103. package/src/metrics.test.ts +60 -0
  104. package/src/metrics.ts +128 -0
  105. package/src/semconv.test.ts +88 -0
  106. package/src/semconv.ts +240 -0
  107. package/src/trace.test.ts +167 -0
  108. package/src/trace.ts +181 -0
package/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # autotel-genai
2
+
3
+ > Gold-standard OpenTelemetry **GenAI** instrumentation for LLM calls, tools, and agents.
4
+
5
+ `autotel-genai` is the AI layer for [autotel](https://github.com/jagreehal/autotel).
6
+ It emits the **canonical `gen_ai.*` semantic conventions** (OpenTelemetry semconv
7
+ **v1.42.0**) for everything from a single `chat` call to a multi-agent workflow —
8
+ token usage, cost, latency metrics, content/evaluation events, and an agent
9
+ identity / delegation / policy / audit governance layer.
10
+
11
+ Canonical-only by design: no legacy `gen.ai.*`, no `prompt_tokens` /
12
+ `completion_tokens`, no non-registry `total_tokens`. What you record is exactly
13
+ what backends (Grafana, Langfuse, Arize, Honeycomb, Jaeger, …) expect.
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ pnpm add autotel autotel-genai
19
+ ```
20
+
21
+ `autotel` is the peer core (trace/span/init). `@opentelemetry/sdk-metrics` is an
22
+ optional peer, needed only for `genAiMetricViews()`.
23
+
24
+ ## Quick start
25
+
26
+ ```ts
27
+ import { traceGenAI, recordGenAiResponse, recordGenAiUsage } from 'autotel-genai/trace';
28
+ import OpenAI from 'openai';
29
+
30
+ const openai = new OpenAI();
31
+
32
+ // Span name → `chat gpt-4o`; request attributes set up front.
33
+ export const chat = traceGenAI({
34
+ provider: 'openai',
35
+ model: 'gpt-4o',
36
+ operation: 'chat',
37
+ temperature: 0.2,
38
+ })((ctx) => async (prompt: string) => {
39
+ const res = await openai.chat.completions.create({
40
+ model: 'gpt-4o',
41
+ messages: [{ role: 'user', content: prompt }],
42
+ });
43
+
44
+ recordGenAiResponse(ctx, {
45
+ model: res.model,
46
+ id: res.id,
47
+ finishReasons: res.choices.map((c) => c.finish_reason),
48
+ });
49
+ // Sets gen_ai.usage.input_tokens / output_tokens + gen_ai.usage.cost.usd
50
+ recordGenAiUsage(ctx, 'gpt-4o', {
51
+ inputTokens: res.usage?.prompt_tokens,
52
+ outputTokens: res.usage?.completion_tokens,
53
+ });
54
+
55
+ return res.choices[0].message.content;
56
+ });
57
+ ```
58
+
59
+ ## What you get
60
+
61
+ | Area | Import | Highlights |
62
+ | --- | --- | --- |
63
+ | **Semconv** | `autotel-genai/semconv` | `GEN_AI.*` keys, `GEN_AI_OPERATION`, `GEN_AI_PROVIDER`, `genAiSpanName()` |
64
+ | **Cost** | `autotel-genai/cost` | `estimateLLMCost`, `recordLLMCost`, `MODEL_PRICING` (cache-read/write aware) |
65
+ | **Metrics** | `autotel-genai/metrics` | `genAiMetricViews()` re-buckets the canonical histograms |
66
+ | **Attributes** | `autotel-genai` | typed builders → canonical attribute maps |
67
+ | **Events** | `autotel-genai/events` | opt-in content + `inference.operation.details` / `evaluation.result` |
68
+ | **Trace** | `autotel-genai/trace` | `traceGenAI()`, `recordGenAiResponse/Usage` |
69
+ | **AI SDK** | `autotel-genai/ai-sdk` | Vercel `ai.*` → `gen_ai.*` mapping + cost |
70
+ | **Agents** | `autotel-genai/agent` | identity, delegation, policy, audit, privacy, non-repudiation |
71
+
72
+ ### Cost
73
+
74
+ ```ts
75
+ import { estimateLLMCost, recordLLMCost } from 'autotel-genai/cost';
76
+
77
+ estimateLLMCost('gpt-4o', { inputTokens: 1000, outputTokens: 500 }); // 0.0075
78
+ recordLLMCost(ctx, 'claude-sonnet-4', {
79
+ inputTokens: 4000,
80
+ cacheReadInputTokens: 3500, // priced at the cached rate
81
+ });
82
+ ```
83
+
84
+ ### Metrics
85
+
86
+ ```ts
87
+ import { NodeSDK } from '@opentelemetry/sdk-node';
88
+ import { genAiMetricViews } from 'autotel-genai/metrics';
89
+
90
+ const sdk = new NodeSDK({ serviceName: 'my-agent', views: [...genAiMetricViews()] });
91
+ ```
92
+
93
+ Re-buckets `gen_ai.client.operation.duration`, `…time_to_first_chunk`,
94
+ `…time_per_output_chunk`, `gen_ai.client.token.usage`, and the autotel
95
+ `gen_ai.client.cost.usd` extension for LLM-shaped distributions.
96
+
97
+ ### Agents & tools
98
+
99
+ ```ts
100
+ import { withScopedTool } from 'autotel-genai/agent';
101
+
102
+ await withScopedTool(
103
+ {
104
+ action: 'agent.refund.execute',
105
+ agent: { id: 'refund-specialist' },
106
+ tool: { name: 'stripe_refund_v3' },
107
+ requiredScopes: ['refund:write'],
108
+ delegation: { parentIdentity: 'usr_99824', scope: ['refund:write'] },
109
+ policy: { decision: 'permit', policyId: 'refund-scope-v2' },
110
+ ai: { model: 'gpt-4o', operation: 'execute_tool' },
111
+ },
112
+ { refundId: 're_123' },
113
+ async () => stripe.refunds.create(req),
114
+ );
115
+ ```
116
+
117
+ The agent layer records `agent.*`, `delegation.*`, `tool.*`, `policy.*`
118
+ governance attributes **and** canonical `gen_ai.*` when `ai` metadata is present.
119
+ It honours spec breaking change #242 (`gen_ai.agent.id` is dropped on internal
120
+ `invoke_agent` spans via `genAiAgentAttributes(…, { internal: true })`).
121
+
122
+ ### Vercel AI SDK
123
+
124
+ The current AI SDK (`@ai-sdk/otel`'s `OpenTelemetry` integration) already emits
125
+ `gen_ai.*` — nothing to map. For `LegacyOpenTelemetry`/older versions, or to add
126
+ cost:
127
+
128
+ ```ts
129
+ import { mapAiSdkAttributes, recordAiSdkCost } from 'autotel-genai/ai-sdk';
130
+
131
+ const canonical = mapAiSdkAttributes(span.attributes); // ai.* → gen_ai.*
132
+ recordAiSdkCost(ctx, span.attributes); // sets gen_ai.usage.cost.usd
133
+ ```
134
+
135
+ ## Semantic conventions
136
+
137
+ Aligned to the `semantic-conventions-genai` snapshot. Span names follow the
138
+ operation-specific upstream rules: inference and embeddings use
139
+ `{operation} {request.model}`, retrieval uses `retrieval {data_source.id}`,
140
+ `execute_tool` uses `execute_tool {tool.name}`, agent spans use
141
+ `... {agent.name}` when available, workflow spans use
142
+ `invoke_workflow {workflow.name}`, and memory spans are just the bare
143
+ operation. Usage is `input_tokens` / `output_tokens` with `cache_read` /
144
+ `cache_creation` / `reasoning.output_tokens`; providers use the
145
+ `gen_ai.provider.name` enum.
146
+
147
+ ## License
148
+
149
+ MIT © Jag Reehal
@@ -0,0 +1,24 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_agent = require('../agent-eo4jY4Xg.cjs');
3
+
4
+ exports.AGENT_AUDIT_SCHEMA_VERSION = require_agent.AGENT_AUDIT_SCHEMA_VERSION;
5
+ exports.canonicalizeForHash = require_agent.canonicalizeForHash;
6
+ exports.createAgentAuditMetadata = require_agent.createAgentAuditMetadata;
7
+ exports.createAgentIdentityRegistry = require_agent.createAgentIdentityRegistry;
8
+ exports.createSignedEventEnvelope = require_agent.createSignedEventEnvelope;
9
+ exports.defineAgentAction = require_agent.defineAgentAction;
10
+ exports.defineAgentToolCall = require_agent.defineAgentToolCall;
11
+ exports.delegateToAgent = require_agent.delegateToAgent;
12
+ exports.flattenAgentAttributes = require_agent.flattenAgentAttributes;
13
+ exports.hashPayload = require_agent.hashPayload;
14
+ exports.recordAgentHandoff = require_agent.recordAgentHandoff;
15
+ exports.recordDecisionBasis = require_agent.recordDecisionBasis;
16
+ exports.recordPolicyDecision = require_agent.recordPolicyDecision;
17
+ exports.resolvePrivacyProfile = require_agent.resolvePrivacyProfile;
18
+ exports.sanitizeAuditPayload = require_agent.sanitizeAuditPayload;
19
+ exports.setAgentAttributes = require_agent.setAgentAttributes;
20
+ exports.verifyEventEnvelopeHash = require_agent.verifyEventEnvelopeHash;
21
+ exports.withAgentAction = require_agent.withAgentAction;
22
+ exports.withAgentSession = require_agent.withAgentSession;
23
+ exports.withAgentToolCall = require_agent.withAgentToolCall;
24
+ exports.withScopedTool = require_agent.withScopedTool;
@@ -0,0 +1,3 @@
1
+ import { $ as ToolCallMetadata, A as AgentAuditEventEnvelope, B as AgentSessionActionMetadata, C as flattenAgentAttributes, D as AgentActionMetadata, E as AgentActionFactory, F as AgentIdentityRecord, G as AiLifecycleStage, H as AgentSessionStatus, I as AgentIdentityRegistry, J as PolicyDecision, K as DelegationContext, L as AgentIdentityStatus, M as AgentEventKind, N as AgentHandler, O as AgentActionOptions, P as AgentIdentity, Q as ScopedToolDefinition, R as AgentMetadataInput, S as recordAgentHandoff, T as createAgentAuditMetadata, U as AgentToolCallActionMetadata, V as AgentSessionMetadata, W as AgentToolCallOptions, X as PrivacyProfile, Y as PolicyMetadata, Z as PrivacyProfileName, _ as withAgentAction, a as PrivacyProfileInput, at as AGENT_AUDIT_SCHEMA_VERSION, b as RecordAgentHandoffMetadata, c as ProvisionAgentIdentityInput, d as createAgentIdentityRegistry, et as ToolStatus, f as withAgentSession, g as recordPolicyDecision, h as recordDecisionBasis, i as verifyEventEnvelopeHash, it as hashPayload, j as AgentDecisionMetadata, k as AgentAiMetadata, l as RevokeAgentIdentityInput, m as defineAgentToolCall, n as CreateSignedEventEnvelopeOptions, nt as HashPayloadOptions, o as resolvePrivacyProfile, p as defineAgentAction, q as GovernanceMetadata, r as createSignedEventEnvelope, rt as canonicalizeForHash, s as sanitizeAuditPayload, t as withScopedTool, tt as AgentContext, u as RotateAgentIdentityInput, v as withAgentToolCall, w as setAgentAttributes, x as delegateToAgent, y as DelegateToAgentInput, z as AgentOutcome } from "../index-DtjGz80V.cjs";
2
+ import { a as TokenUsage, i as ModelPricing } from "../cost-DXTkBUUW.cjs";
3
+ export { AGENT_AUDIT_SCHEMA_VERSION, type AgentActionFactory, type AgentActionMetadata, type AgentActionOptions, type AgentAiMetadata, type AgentAuditEventEnvelope, type AgentContext, type AgentDecisionMetadata, type AgentEventKind, type AgentHandler, type AgentIdentity, type AgentIdentityRecord, type AgentIdentityRegistry, type AgentIdentityStatus, type AgentMetadataInput, type AgentOutcome, type AgentSessionActionMetadata, type AgentSessionMetadata, type AgentSessionStatus, type AgentToolCallActionMetadata, type AgentToolCallOptions, type AiLifecycleStage, type CreateSignedEventEnvelopeOptions, type DelegateToAgentInput, type DelegationContext, type GovernanceMetadata, type HashPayloadOptions, type ModelPricing, type PolicyDecision, type PolicyMetadata, type PrivacyProfile, type PrivacyProfileInput, type PrivacyProfileName, type ProvisionAgentIdentityInput, type RecordAgentHandoffMetadata, type RevokeAgentIdentityInput, type RotateAgentIdentityInput, type ScopedToolDefinition, type TokenUsage, type ToolCallMetadata, type ToolStatus, canonicalizeForHash, createAgentAuditMetadata, createAgentIdentityRegistry, createSignedEventEnvelope, defineAgentAction, defineAgentToolCall, delegateToAgent, flattenAgentAttributes, hashPayload, recordAgentHandoff, recordDecisionBasis, recordPolicyDecision, resolvePrivacyProfile, sanitizeAuditPayload, setAgentAttributes, verifyEventEnvelopeHash, withAgentAction, withAgentSession, withAgentToolCall, withScopedTool };
@@ -0,0 +1,3 @@
1
+ import { $ as ToolCallMetadata, A as AgentAuditEventEnvelope, B as AgentSessionActionMetadata, C as flattenAgentAttributes, D as AgentActionMetadata, E as AgentActionFactory, F as AgentIdentityRecord, G as AiLifecycleStage, H as AgentSessionStatus, I as AgentIdentityRegistry, J as PolicyDecision, K as DelegationContext, L as AgentIdentityStatus, M as AgentEventKind, N as AgentHandler, O as AgentActionOptions, P as AgentIdentity, Q as ScopedToolDefinition, R as AgentMetadataInput, S as recordAgentHandoff, T as createAgentAuditMetadata, U as AgentToolCallActionMetadata, V as AgentSessionMetadata, W as AgentToolCallOptions, X as PrivacyProfile, Y as PolicyMetadata, Z as PrivacyProfileName, _ as withAgentAction, a as PrivacyProfileInput, at as AGENT_AUDIT_SCHEMA_VERSION, b as RecordAgentHandoffMetadata, c as ProvisionAgentIdentityInput, d as createAgentIdentityRegistry, et as ToolStatus, f as withAgentSession, g as recordPolicyDecision, h as recordDecisionBasis, i as verifyEventEnvelopeHash, it as hashPayload, j as AgentDecisionMetadata, k as AgentAiMetadata, l as RevokeAgentIdentityInput, m as defineAgentToolCall, n as CreateSignedEventEnvelopeOptions, nt as HashPayloadOptions, o as resolvePrivacyProfile, p as defineAgentAction, q as GovernanceMetadata, r as createSignedEventEnvelope, rt as canonicalizeForHash, s as sanitizeAuditPayload, t as withScopedTool, tt as AgentContext, u as RotateAgentIdentityInput, v as withAgentToolCall, w as setAgentAttributes, x as delegateToAgent, y as DelegateToAgentInput, z as AgentOutcome } from "../index-D1gH80Xd.js";
2
+ import { a as TokenUsage, i as ModelPricing } from "../cost-DXTkBUUW.js";
3
+ export { AGENT_AUDIT_SCHEMA_VERSION, type AgentActionFactory, type AgentActionMetadata, type AgentActionOptions, type AgentAiMetadata, type AgentAuditEventEnvelope, type AgentContext, type AgentDecisionMetadata, type AgentEventKind, type AgentHandler, type AgentIdentity, type AgentIdentityRecord, type AgentIdentityRegistry, type AgentIdentityStatus, type AgentMetadataInput, type AgentOutcome, type AgentSessionActionMetadata, type AgentSessionMetadata, type AgentSessionStatus, type AgentToolCallActionMetadata, type AgentToolCallOptions, type AiLifecycleStage, type CreateSignedEventEnvelopeOptions, type DelegateToAgentInput, type DelegationContext, type GovernanceMetadata, type HashPayloadOptions, type ModelPricing, type PolicyDecision, type PolicyMetadata, type PrivacyProfile, type PrivacyProfileInput, type PrivacyProfileName, type ProvisionAgentIdentityInput, type RecordAgentHandoffMetadata, type RevokeAgentIdentityInput, type RotateAgentIdentityInput, type ScopedToolDefinition, type TokenUsage, type ToolCallMetadata, type ToolStatus, canonicalizeForHash, createAgentAuditMetadata, createAgentIdentityRegistry, createSignedEventEnvelope, defineAgentAction, defineAgentToolCall, delegateToAgent, flattenAgentAttributes, hashPayload, recordAgentHandoff, recordDecisionBasis, recordPolicyDecision, resolvePrivacyProfile, sanitizeAuditPayload, setAgentAttributes, verifyEventEnvelopeHash, withAgentAction, withAgentSession, withAgentToolCall, withScopedTool };
@@ -0,0 +1,3 @@
1
+ import { _ as setAgentAttributes, a as sanitizeAuditPayload, b as hashPayload, c as delegateToAgent, d as defineAgentToolCall, f as recordDecisionBasis, g as flattenAgentAttributes, h as withAgentToolCall, i as resolvePrivacyProfile, l as recordAgentHandoff, m as withAgentAction, n as createSignedEventEnvelope, o as createAgentIdentityRegistry, p as recordPolicyDecision, r as verifyEventEnvelopeHash, s as withAgentSession, t as withScopedTool, u as defineAgentAction, v as createAgentAuditMetadata, x as AGENT_AUDIT_SCHEMA_VERSION, y as canonicalizeForHash } from "../agent-CqXauTpC.js";
2
+
3
+ export { AGENT_AUDIT_SCHEMA_VERSION, canonicalizeForHash, createAgentAuditMetadata, createAgentIdentityRegistry, createSignedEventEnvelope, defineAgentAction, defineAgentToolCall, delegateToAgent, flattenAgentAttributes, hashPayload, recordAgentHandoff, recordDecisionBasis, recordPolicyDecision, resolvePrivacyProfile, sanitizeAuditPayload, setAgentAttributes, verifyEventEnvelopeHash, withAgentAction, withAgentSession, withAgentToolCall, withScopedTool };