@osfactory/otel-hook 0.9.0 → 0.11.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 (51) hide show
  1. package/README.md +124 -4
  2. package/dist/{adapter-BJiz6lqM.d.ts → adapter-DiLknaTF.d.ts} +14 -2
  3. package/dist/cli.js +2272 -730
  4. package/dist/cli.js.map +1 -1
  5. package/dist/config.d.ts +13 -4
  6. package/dist/config.js +128 -23
  7. package/dist/config.js.map +1 -1
  8. package/dist/{destinations-C6Bm89W6.d.ts → destinations-BIl3gGzx.d.ts} +12 -2
  9. package/dist/diagnostics.d.ts +1 -1
  10. package/dist/diagnostics.js.map +1 -1
  11. package/dist/{events-B3MHoJTD.d.ts → events-CSD_YiJ2.d.ts} +2 -0
  12. package/dist/{export-destinations-d8tZV0G4.d.ts → export-destinations-QWKGGEjk.d.ts} +131 -6
  13. package/dist/{filesystem-store-r-nNA3BA.d.ts → filesystem-store-D3SCNmbX.d.ts} +1 -1
  14. package/dist/{hook-zA1b4cWd.d.ts → hook-BOecWGSz.d.ts} +10 -5
  15. package/dist/index.d.ts +16 -16
  16. package/dist/index.js +1808 -251
  17. package/dist/index.js.map +1 -1
  18. package/dist/install.d.ts +4 -4
  19. package/dist/install.js +31 -2
  20. package/dist/install.js.map +1 -1
  21. package/dist/integration.d.ts +37 -12
  22. package/dist/integration.js +3271 -1551
  23. package/dist/integration.js.map +1 -1
  24. package/dist/lifecycle.d.ts +55 -4
  25. package/dist/lifecycle.js +277 -4
  26. package/dist/lifecycle.js.map +1 -1
  27. package/dist/{memory-Bl9ANVff.d.ts → memory-Cs51jyRk.d.ts} +2 -2
  28. package/dist/{semconv-fPRb-dpU.d.ts → metric-model-DDnWxL9d.d.ts} +154 -2
  29. package/dist/model.d.ts +1 -1
  30. package/dist/model.js +7 -0
  31. package/dist/model.js.map +1 -1
  32. package/dist/{payload-Ci8_ERm6.d.ts → payload-ChtrEX6A.d.ts} +41 -2
  33. package/dist/{ports-n7KFOpKo.d.ts → ports-CeqLq31i.d.ts} +87 -2
  34. package/dist/providers.d.ts +146 -10
  35. package/dist/providers.js +397 -13
  36. package/dist/providers.js.map +1 -1
  37. package/dist/{registry-B8ZHc2bc.d.ts → registry-CcJNF3WY.d.ts} +2 -2
  38. package/dist/runtime.d.ts +8 -8
  39. package/dist/runtime.js +1404 -1219
  40. package/dist/runtime.js.map +1 -1
  41. package/dist/{schema-BxiVsU-d.d.ts → schema-DMnoU1V_.d.ts} +93 -1
  42. package/dist/state.d.ts +4 -4
  43. package/dist/state.js +630 -39
  44. package/dist/state.js.map +1 -1
  45. package/dist/telemetry.d.ts +6 -6
  46. package/dist/telemetry.js +872 -19
  47. package/dist/telemetry.js.map +1 -1
  48. package/dist/testing.d.ts +11 -9
  49. package/dist/testing.js +264 -34
  50. package/dist/testing.js.map +1 -1
  51. package/package.json +3 -1
@@ -1,19 +1,20 @@
1
- import { e as ProviderContext, P as ProviderAdapter, d as ProviderCapabilities, b as DeliveryIdentifierSupport, A as AdapterDescription } from './adapter-BJiz6lqM.js';
2
- export { D as DELIVERY_COMPONENT_PATTERN, a as DELIVERY_GAP_REASON_PATTERN, M as MAX_DELIVERY_COMPONENTS, c as MAX_DELIVERY_GAPS, f as ProviderDeliveryClaim, g as ProviderDeliveryGaps, h as ProviderDetection, i as ProviderDetectionInput, j as ProviderHookResponse, k as ProviderHookResponseInput, l as ProviderIdentityInput, m as ProviderParseInput, n as ProviderParseResult, S as SILENT_HOOK_RESPONSE, o as adapterSupports, p as asProviderId, q as deliveryComponentSchema, r as deliveryGapReasonSchema, s as deliveryIdentifierSupportSchema, t as describeAdapter, u as providerCapabilitiesSchema, v as providerDeliveryClaimSchema, w as providerDeliveryGapsSchema, x as providerDetectionSchema, y as providerHookResponseSchema, z as readDeliveryClaim, B as readDeliveryGap, C as unknownDetection } from './adapter-BJiz6lqM.js';
3
- import { b as CanonicalEvent, u as TargetHint, q as SessionStartEvent, p as SessionEndEvent, P as PromptSubmittedEvent, l as GenerationStartEvent, G as GenerationEndEvent, A as ToolStartEvent, y as ToolEndEvent, t as SubagentStartEvent, r as SubagentEndEvent, g as CompactionPerformedEvent, i as ErrorRaisedEvent } from './events-B3MHoJTD.js';
1
+ import { e as ProviderContext, P as ProviderAdapter, d as ProviderCapabilities, b as DeliveryIdentifierSupport, A as AdapterDescription } from './adapter-DiLknaTF.js';
2
+ export { D as DELIVERY_COMPONENT_PATTERN, a as DELIVERY_GAP_REASON_PATTERN, M as MAX_DELIVERY_COMPONENTS, c as MAX_DELIVERY_GAPS, f as ProviderDeliveryClaim, g as ProviderDeliveryGaps, h as ProviderDetection, i as ProviderDetectionInput, j as ProviderHookResponse, k as ProviderHookResponseInput, l as ProviderIdentityInput, m as ProviderParseInput, n as ProviderParseResult, S as SILENT_HOOK_RESPONSE, o as adapterSupports, p as asProviderId, q as deliveryComponentSchema, r as deliveryGapReasonSchema, s as deliveryIdentifierSupportSchema, t as describeAdapter, u as providerCapabilitiesSchema, v as providerDeliveryClaimSchema, w as providerDeliveryGapsSchema, x as providerDetectionSchema, y as providerHookResponseSchema, z as readDeliveryClaim, B as readDeliveryGap, C as unknownDetection } from './adapter-DiLknaTF.js';
3
+ import { b as CanonicalEvent, u as TargetHint, q as SessionStartEvent, p as SessionEndEvent, P as PromptSubmittedEvent, l as GenerationStartEvent, G as GenerationEndEvent, A as ToolStartEvent, y as ToolEndEvent, t as SubagentStartEvent, r as SubagentEndEvent, g as CompactionPerformedEvent, i as ErrorRaisedEvent } from './events-CSD_YiJ2.js';
4
4
  import { d as InvocationIdentity } from './identity-BrVBs901.js';
5
5
  import { d as EventId } from './primitives-CJ8-rn-f.js';
6
- import { P as ProviderRegistry } from './registry-B8ZHc2bc.js';
7
- export { B as BUILT_IN_PROVIDERS, D as DetectionCandidate, R as RegistryDetectionResult, c as createProviderRegistry } from './registry-B8ZHc2bc.js';
8
- export { A as ANTIGRAVITY_CAPABILITIES, a as ANTIGRAVITY_HOOK_EVENT_NAMES, b as ANTIGRAVITY_PROVIDER_ID, c as AntigravityAdapterOptions, d as AntigravityHookEventName, C as CLAUDE_CODE_PROVIDER_ID, e as CLAUDE_HOOK_EVENT_NAMES, f as CODEX_ADAPTER_VERSION, g as CODEX_HOOK_EVENT_NAMES, h as CODEX_PROVIDER_ID, i as CURSOR_DECISION_EVENTS, j as CURSOR_HOOK_EVENT_NAMES, k as CURSOR_PROVIDER_ID, l as CURSOR_UNMODELLED_HOOK_EVENT_NAMES, m as ClaudeHookEventName, n as CodexHookEventName, o as CursorHookEventName, D as DEFAULT_GEMINI_CAPABILITIES, G as GEMINI_ADAPTER_VERSION, p as GEMINI_HOOK_EVENT_NAMES, q as GEMINI_PROVIDER_ID, r as GeminiAdapterOptions, s as GeminiHookEventName, t as createAntigravityAdapter, u as createGeminiCliAdapter } from './payload-Ci8_ERm6.js';
6
+ import { P as ProviderRegistry } from './registry-CcJNF3WY.js';
7
+ export { B as BUILT_IN_PROVIDERS, D as DetectionCandidate, R as RegistryDetectionResult, c as createProviderRegistry } from './registry-CcJNF3WY.js';
8
+ import { v as ClaudeUsage } from './payload-ChtrEX6A.js';
9
+ export { A as ANTIGRAVITY_CAPABILITIES, a as ANTIGRAVITY_HOOK_EVENT_NAMES, b as ANTIGRAVITY_PROVIDER_ID, c as AntigravityAdapterOptions, d as AntigravityHookEventName, C as CLAUDE_CODE_PROVIDER_ID, e as CLAUDE_HOOK_EVENT_NAMES, f as CODEX_ADAPTER_VERSION, g as CODEX_HOOK_EVENT_NAMES, h as CODEX_PROVIDER_ID, i as CURSOR_DECISION_EVENTS, j as CURSOR_HOOK_EVENT_NAMES, k as CURSOR_PROVIDER_ID, l as CURSOR_UNMODELLED_HOOK_EVENT_NAMES, m as ClaudeHookEventName, n as CodexHookEventName, o as CursorHookEventName, D as DEFAULT_GEMINI_CAPABILITIES, G as GEMINI_ADAPTER_VERSION, p as GEMINI_HOOK_EVENT_NAMES, q as GEMINI_PROVIDER_ID, r as GeminiAdapterOptions, s as GeminiHookEventName, t as createAntigravityAdapter, u as createGeminiCliAdapter } from './payload-ChtrEX6A.js';
10
+ import { P as PayloadEnricher } from './ports-CeqLq31i.js';
9
11
  import 'zod';
10
12
  import './errors.js';
11
13
  import './taxonomy-DwhTtuWd.js';
12
14
  import './policy-CimmCfRp.js';
13
15
  import './content-M9xIKG7h.js';
14
16
  import './service-CbI9CObT.js';
15
- import './ports-n7KFOpKo.js';
16
- import './schema-BxiVsU-d.js';
17
+ import './schema-DMnoU1V_.js';
17
18
 
18
19
  /** Fields the factory supplies; an adapter never sets them itself. */
19
20
  type ManagedFields = "schemaVersion" | "eventId" | "invocationId" | "sessionId" | "sequence" | "provenance" | "workspace" | "extensions" | "subagentId" | "target" | "occurredAt";
@@ -91,7 +92,8 @@ declare const createClaudeCodeAdapter: (options?: ClaudeCodeAdapterOptions) => P
91
92
  * - `reportsCachedInput`/`reportsCacheCreation` are true because the payload
92
93
  * schema *accepts* Anthropic's `message.usage` shape when a wrapping harness
93
94
  * attaches it. No hook callback in the protocol carries a token counter, and
94
- * this adapter never reads the transcript to find them.
95
+ * this adapter never reads the transcript to find them; `transcript.ts` is the
96
+ * bounded helper a wrapper calls to attach them (issue #64).
95
97
  * - `cacheCreationAccounting` is `included-in-input` because `input_tokens` is
96
98
  * the fresh portion only, so the adapter folds all three input buckets into one
97
99
  * inclusive total (see `usage.ts`).
@@ -109,6 +111,140 @@ declare const createClaudeCodeAdapter: (options?: ClaudeCodeAdapterOptions) => P
109
111
  */
110
112
  declare const CLAUDE_CODE_CAPABILITIES: ProviderCapabilities;
111
113
 
114
+ /**
115
+ * Bounded readers for one Claude Code session transcript, and the enricher that
116
+ * uses them to attach a turn's model and token usage to a `Stop` payload.
117
+ *
118
+ * None of this is part of the Claude Code adapter, and the adapter never calls
119
+ * it: `ProviderContext` gives adapters no filesystem handle (ADR 0003). Two
120
+ * callers use it instead:
121
+ *
122
+ * - a wrapping harness, through {@link attachClaudeTranscriptTurn}, which reads
123
+ * the last assistant message only;
124
+ * - the hook runtime, through {@link createClaudeTranscriptUsageEnricher}, when
125
+ * the host turns on `providers.claudeCode.readTranscriptUsage` — the one
126
+ * sanctioned exception to "no transcript reads in the hook". It sums the whole
127
+ * turn, from a byte cursor the runtime keeps in the state store.
128
+ *
129
+ * Why the transcript at all: no Claude Code hook callback carries a token
130
+ * counter, and only `SessionStart` names a model. The per-turn model
131
+ * (`message.model`, which follows `/model`) and the Anthropic usage object
132
+ * (`message.usage`) exist only on assistant transcript records, one record per
133
+ * content block, each repeating its request's `message.id`.
134
+ *
135
+ * Every bound is hard, and nothing here throws:
136
+ * - only regular `.jsonl` files at an absolute path are opened, so a FIFO or a
137
+ * device cannot stall the hook, and no directory is ever listed;
138
+ * - a read is capped in bytes and raced against a time budget;
139
+ * - a malformed or truncated line is skipped, never fatal.
140
+ *
141
+ * Nothing but model ids and numeric counters leaves this module: a record's
142
+ * content (prompt, response, thinking, tool input) is parsed only to be dropped.
143
+ */
144
+ type ClaudeTranscriptUnavailableReason = "invalid-path" | "not-a-file" | "unreadable" | "timeout" | "no-assistant-message";
145
+ type ClaudeTranscriptTurn = {
146
+ readonly status: "found";
147
+ /** `message.model`; absent when the record named none. */
148
+ readonly model?: string;
149
+ /** `message.usage`, coerced onto the shape the `Stop` schema accepts. */
150
+ readonly usage?: ClaudeUsage;
151
+ } | {
152
+ readonly status: "unavailable";
153
+ readonly reason: ClaudeTranscriptUnavailableReason;
154
+ };
155
+ type ReadClaudeTranscriptTurnOptions = {
156
+ /** Bytes read from the end of the file. Default 1 MiB, clamped to 64 KiB..16 MiB. */
157
+ readonly maxTailBytes?: number;
158
+ /** Budget for the whole read. Default 250 ms, clamped to 10 ms..5 s. */
159
+ readonly timeoutMillis?: number;
160
+ };
161
+ declare const DEFAULT_CLAUDE_TRANSCRIPT_TAIL_BYTES = 1048576;
162
+ declare const DEFAULT_CLAUDE_TRANSCRIPT_TIMEOUT_MILLIS = 250;
163
+ declare const DEFAULT_CLAUDE_TRANSCRIPT_RANGE_BYTES = 8388608;
164
+ declare const DEFAULT_CLAUDE_TRANSCRIPT_RANGE_TIMEOUT_MILLIS = 500;
165
+ /**
166
+ * Read the model and usage of the last main-thread assistant message in a
167
+ * Claude Code transcript. Never throws, never rejects.
168
+ *
169
+ * The usage is that **one message's** — the final model request of the turn. A
170
+ * turn that called tools made several requests, and the earlier ones are not
171
+ * summed in; {@link readClaudeTranscriptRange} sums a whole turn.
172
+ */
173
+ declare const readClaudeTranscriptTurn: (transcriptPath: string, options?: ReadClaudeTranscriptTurnOptions) => Promise<ClaudeTranscriptTurn>;
174
+ type ClaudeTranscriptRange = {
175
+ readonly status: "found";
176
+ /** Model of the last main-thread assistant record in the range. */
177
+ readonly model?: string;
178
+ /** Summed usage of every distinct request; absent when none reported usage. */
179
+ readonly usage?: ClaudeUsage;
180
+ /** Distinct main-thread requests (by `message.id`) in the range. */
181
+ readonly requestCount: number;
182
+ /** Offset just past the last complete line read: the next range starts here. */
183
+ readonly endOffset: number;
184
+ } | {
185
+ readonly status: "unavailable";
186
+ readonly reason: "invalid-path" | "not-a-file" | "unreadable" | "timeout"
187
+ /** The file is shorter than the offset: it was replaced or truncated. */
188
+ | "truncated"
189
+ /** More than the range cap was written since the offset. */
190
+ | "over-cap";
191
+ };
192
+ type ReadClaudeTranscriptRangeOptions = {
193
+ /** Most bytes a range may span. Default 8 MiB, clamped to 64 KiB..64 MiB. */
194
+ readonly maxRangeBytes?: number;
195
+ /** Budget for the whole range read. Default 500 ms, clamped to 10 ms..5 s. */
196
+ readonly rangeTimeoutMillis?: number;
197
+ };
198
+ /**
199
+ * Sum every main-thread model request written between `fromOffset` and the end
200
+ * of the transcript. Never throws, never rejects.
201
+ *
202
+ * Records of one request share a `message.id` and each repeats its usage, so the
203
+ * last record per id is the one counted; a record without an id counts alone.
204
+ * A trailing partial line is left for the next range.
205
+ */
206
+ declare const readClaudeTranscriptRange: (transcriptPath: string, fromOffset: number, options?: ReadClaudeTranscriptRangeOptions) => Promise<ClaudeTranscriptRange>;
207
+ /**
208
+ * Attach the last assistant message's `{ model, usage }` to a Claude Code
209
+ * `Stop` / `StopFailure` payload, for a wrapper to hand to otel-hook.
210
+ *
211
+ * The usage is one request's, so it is attached with `usage_partial: true`,
212
+ * which the exported generation reports as `otelhook.usage.partial`. For whole
213
+ * turn totals, turn on `providers.claudeCode.readTranscriptUsage` instead.
214
+ *
215
+ * Returns the payload unchanged when it is not a stop, names no
216
+ * `transcript_path`, already carries both fields, or the transcript yields
217
+ * nothing — so a wrapper can call it unconditionally. A field the payload
218
+ * already carries is never overwritten. Never throws, never rejects.
219
+ */
220
+ declare const attachClaudeTranscriptTurn: (payload: unknown, options?: ReadClaudeTranscriptTurnOptions) => Promise<unknown>;
221
+ type ClaudeTranscriptUsageEnricherOptions = ReadClaudeTranscriptTurnOptions & ReadClaudeTranscriptRangeOptions;
222
+ declare const CLAUDE_TRANSCRIPT_USAGE_ENRICHER_ID = "claude-transcript-usage";
223
+ /**
224
+ * The hook-runtime side of `providers.claudeCode.readTranscriptUsage`: attach
225
+ * each turn's model and **summed** usage to its `Stop` / `StopFailure`.
226
+ *
227
+ * The runtime keeps a byte cursor per session (state value `read-cursor`):
228
+ *
229
+ * - `SessionStart` records where the transcript ends now (0 when it does not
230
+ * exist yet), unless a cursor for the same transcript is already stored — a
231
+ * compaction restart must not skip the turn it interrupted.
232
+ * - `Stop` sums every main-thread request from the cursor to the end, and moves
233
+ * the cursor there. That is the exact turn total: `usage_partial: false`.
234
+ * - With no usable cursor (enabled mid-session, an expired or lost store, a
235
+ * different transcript), a range over the cap, or a transcript that shrank, it
236
+ * falls back to the last request in the bounded tail, attaches it with
237
+ * `usage_partial: true`, and moves the cursor to the end so the next turn is
238
+ * exact again.
239
+ * - A timeout or an unreadable file attaches nothing and keeps the cursor, so
240
+ * the next turn's range includes this one: tokens are counted late, never
241
+ * twice and never lost.
242
+ *
243
+ * A payload that already carries `usage` (a wrapper attached it) is left as it
244
+ * is, and the cursor moves past it so those tokens are not counted again.
245
+ */
246
+ declare const createClaudeTranscriptUsageEnricher: (options?: ClaudeTranscriptUsageEnricherOptions) => PayloadEnricher;
247
+
112
248
  /**
113
249
  * Provider adapter for Cursor's agent hooks.
114
250
  *
@@ -271,4 +407,4 @@ declare const DELIVERY_IDENTIFIER_SUMMARY: Readonly<Record<DeliveryIdentifierSup
271
407
  */
272
408
  declare const describeProviderCatalog: (options?: DefaultRegistryOptions) => readonly ProviderCatalogEntry[];
273
409
 
274
- export { ANTIGRAVITY_PROMOTION_GATES, ANTIGRAVITY_PROVIDER_MATURITY, AdapterDescription, type AntigravityProviderMaturity, CLAUDE_CODE_CAPABILITIES, CODEX_CAPABILITIES, CURSOR_ADAPTER_VERSION, CURSOR_CAPABILITIES, type CanonicalEventDraft, type ClaudeCodeAdapterOptions, type CursorAdapterOptions, DELIVERY_IDENTIFIER_SUMMARY, type DefaultRegistryOptions, DeliveryIdentifierSupport, EXPERIMENTAL_PROVIDER_IDS, type EventFactory, type EventFactoryInput, PROVIDER_DESCRIPTORS, ProviderAdapter, ProviderCapabilities, type ProviderCatalogEntry, ProviderContext, type ProviderDescriptor, type ProviderMaturity, ProviderRegistry, createClaudeCodeAdapter, createCodexAdapter, createCursorAdapter, createDefaultProviderRegistry, createEventFactory, describeProviderCatalog, findProviderDescriptor, isExperimentalProvider };
410
+ export { ANTIGRAVITY_PROMOTION_GATES, ANTIGRAVITY_PROVIDER_MATURITY, AdapterDescription, type AntigravityProviderMaturity, CLAUDE_CODE_CAPABILITIES, CLAUDE_TRANSCRIPT_USAGE_ENRICHER_ID, CODEX_CAPABILITIES, CURSOR_ADAPTER_VERSION, CURSOR_CAPABILITIES, type CanonicalEventDraft, type ClaudeCodeAdapterOptions, type ClaudeTranscriptRange, type ClaudeTranscriptTurn, type ClaudeTranscriptUsageEnricherOptions, type CursorAdapterOptions, DEFAULT_CLAUDE_TRANSCRIPT_RANGE_BYTES, DEFAULT_CLAUDE_TRANSCRIPT_RANGE_TIMEOUT_MILLIS, DEFAULT_CLAUDE_TRANSCRIPT_TAIL_BYTES, DEFAULT_CLAUDE_TRANSCRIPT_TIMEOUT_MILLIS, DELIVERY_IDENTIFIER_SUMMARY, type DefaultRegistryOptions, DeliveryIdentifierSupport, EXPERIMENTAL_PROVIDER_IDS, type EventFactory, type EventFactoryInput, PROVIDER_DESCRIPTORS, ProviderAdapter, ProviderCapabilities, type ProviderCatalogEntry, ProviderContext, type ProviderDescriptor, type ProviderMaturity, ProviderRegistry, type ReadClaudeTranscriptRangeOptions, type ReadClaudeTranscriptTurnOptions, attachClaudeTranscriptTurn, createClaudeCodeAdapter, createClaudeTranscriptUsageEnricher, createCodexAdapter, createCursorAdapter, createDefaultProviderRegistry, createEventFactory, describeProviderCatalog, findProviderDescriptor, isExperimentalProvider, readClaudeTranscriptRange, readClaudeTranscriptTurn };