@morsehq-dev/sdk 0.4.0-rc.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.
Files changed (161) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +294 -0
  3. package/dist/anthropic/index.cjs +39 -0
  4. package/dist/anthropic/index.cjs.map +1 -0
  5. package/dist/anthropic/index.d.cts +213 -0
  6. package/dist/anthropic/index.d.ts +213 -0
  7. package/dist/anthropic/index.js +6 -0
  8. package/dist/anthropic/index.js.map +1 -0
  9. package/dist/anthropic-agent-sdk/index.cjs +744 -0
  10. package/dist/anthropic-agent-sdk/index.cjs.map +1 -0
  11. package/dist/anthropic-agent-sdk/index.d.cts +371 -0
  12. package/dist/anthropic-agent-sdk/index.d.ts +371 -0
  13. package/dist/anthropic-agent-sdk/index.js +735 -0
  14. package/dist/anthropic-agent-sdk/index.js.map +1 -0
  15. package/dist/browser/anthropic/index.cjs +39 -0
  16. package/dist/browser/anthropic/index.cjs.map +1 -0
  17. package/dist/browser/anthropic/index.js +6 -0
  18. package/dist/browser/anthropic/index.js.map +1 -0
  19. package/dist/browser/anthropic-agent-sdk/index.cjs +744 -0
  20. package/dist/browser/anthropic-agent-sdk/index.cjs.map +1 -0
  21. package/dist/browser/anthropic-agent-sdk/index.js +735 -0
  22. package/dist/browser/anthropic-agent-sdk/index.js.map +1 -0
  23. package/dist/browser/chunk-3643DC7K.cjs +365 -0
  24. package/dist/browser/chunk-3643DC7K.cjs.map +1 -0
  25. package/dist/browser/chunk-4FALQMOZ.js +232 -0
  26. package/dist/browser/chunk-4FALQMOZ.js.map +1 -0
  27. package/dist/browser/chunk-5J2QBK75.js +884 -0
  28. package/dist/browser/chunk-5J2QBK75.js.map +1 -0
  29. package/dist/browser/chunk-7MSXWGAH.js +53 -0
  30. package/dist/browser/chunk-7MSXWGAH.js.map +1 -0
  31. package/dist/browser/chunk-A24L5N5N.js +596 -0
  32. package/dist/browser/chunk-A24L5N5N.js.map +1 -0
  33. package/dist/browser/chunk-B7DEUDP6.cjs +177 -0
  34. package/dist/browser/chunk-B7DEUDP6.cjs.map +1 -0
  35. package/dist/browser/chunk-F6CJACNH.cjs +604 -0
  36. package/dist/browser/chunk-F6CJACNH.cjs.map +1 -0
  37. package/dist/browser/chunk-JBOYFQSB.js +412 -0
  38. package/dist/browser/chunk-JBOYFQSB.js.map +1 -0
  39. package/dist/browser/chunk-KRBADE6R.cjs +183 -0
  40. package/dist/browser/chunk-KRBADE6R.cjs.map +1 -0
  41. package/dist/browser/chunk-MCJYZH6W.cjs +415 -0
  42. package/dist/browser/chunk-MCJYZH6W.cjs.map +1 -0
  43. package/dist/browser/chunk-NQG2IAQS.js +178 -0
  44. package/dist/browser/chunk-NQG2IAQS.js.map +1 -0
  45. package/dist/browser/chunk-O4HG3OSK.cjs +929 -0
  46. package/dist/browser/chunk-O4HG3OSK.cjs.map +1 -0
  47. package/dist/browser/chunk-QWRQJO57.js +363 -0
  48. package/dist/browser/chunk-QWRQJO57.js.map +1 -0
  49. package/dist/browser/chunk-SWQOPFE4.cjs +234 -0
  50. package/dist/browser/chunk-SWQOPFE4.cjs.map +1 -0
  51. package/dist/browser/chunk-TYDG747E.js +171 -0
  52. package/dist/browser/chunk-TYDG747E.js.map +1 -0
  53. package/dist/browser/chunk-TZRSDDFP.cjs +56 -0
  54. package/dist/browser/chunk-TZRSDDFP.cjs.map +1 -0
  55. package/dist/browser/index.cjs +587 -0
  56. package/dist/browser/index.cjs.map +1 -0
  57. package/dist/browser/index.js +522 -0
  58. package/dist/browser/index.js.map +1 -0
  59. package/dist/browser/integrations/pino.cjs +89 -0
  60. package/dist/browser/integrations/pino.cjs.map +1 -0
  61. package/dist/browser/integrations/pino.js +86 -0
  62. package/dist/browser/integrations/pino.js.map +1 -0
  63. package/dist/browser/langchain/index.cjs +535 -0
  64. package/dist/browser/langchain/index.cjs.map +1 -0
  65. package/dist/browser/langchain/index.js +528 -0
  66. package/dist/browser/langchain/index.js.map +1 -0
  67. package/dist/browser/langgraph/index.cjs +377 -0
  68. package/dist/browser/langgraph/index.cjs.map +1 -0
  69. package/dist/browser/langgraph/index.js +371 -0
  70. package/dist/browser/langgraph/index.js.map +1 -0
  71. package/dist/browser/openai/index.cjs +125 -0
  72. package/dist/browser/openai/index.cjs.map +1 -0
  73. package/dist/browser/openai/index.js +122 -0
  74. package/dist/browser/openai/index.js.map +1 -0
  75. package/dist/browser/openai-agents/index.cjs +689 -0
  76. package/dist/browser/openai-agents/index.cjs.map +1 -0
  77. package/dist/browser/openai-agents/index.js +678 -0
  78. package/dist/browser/openai-agents/index.js.map +1 -0
  79. package/dist/browser/vercel-ai/index.cjs +233 -0
  80. package/dist/browser/vercel-ai/index.cjs.map +1 -0
  81. package/dist/browser/vercel-ai/index.js +231 -0
  82. package/dist/browser/vercel-ai/index.js.map +1 -0
  83. package/dist/chunk-4R4SHGOK.js +363 -0
  84. package/dist/chunk-4R4SHGOK.js.map +1 -0
  85. package/dist/chunk-7EO7MQBA.cjs +183 -0
  86. package/dist/chunk-7EO7MQBA.cjs.map +1 -0
  87. package/dist/chunk-7YCENA54.cjs +604 -0
  88. package/dist/chunk-7YCENA54.cjs.map +1 -0
  89. package/dist/chunk-CKFOGDUF.js +596 -0
  90. package/dist/chunk-CKFOGDUF.js.map +1 -0
  91. package/dist/chunk-FJUNILZT.cjs +1324 -0
  92. package/dist/chunk-FJUNILZT.cjs.map +1 -0
  93. package/dist/chunk-HDAFUKQ3.js +171 -0
  94. package/dist/chunk-HDAFUKQ3.js.map +1 -0
  95. package/dist/chunk-KBWPNIH4.cjs +234 -0
  96. package/dist/chunk-KBWPNIH4.cjs.map +1 -0
  97. package/dist/chunk-KJEO52QS.cjs +365 -0
  98. package/dist/chunk-KJEO52QS.cjs.map +1 -0
  99. package/dist/chunk-KZBCOZIQ.cjs +177 -0
  100. package/dist/chunk-KZBCOZIQ.cjs.map +1 -0
  101. package/dist/chunk-ME5JALGT.js +53 -0
  102. package/dist/chunk-ME5JALGT.js.map +1 -0
  103. package/dist/chunk-PVHDEPRE.cjs +56 -0
  104. package/dist/chunk-PVHDEPRE.cjs.map +1 -0
  105. package/dist/chunk-RTL23YOQ.js +178 -0
  106. package/dist/chunk-RTL23YOQ.js.map +1 -0
  107. package/dist/chunk-TQWI4UYO.js +1277 -0
  108. package/dist/chunk-TQWI4UYO.js.map +1 -0
  109. package/dist/chunk-VXDBDPDR.cjs +415 -0
  110. package/dist/chunk-VXDBDPDR.cjs.map +1 -0
  111. package/dist/chunk-XTKMUJWI.js +232 -0
  112. package/dist/chunk-XTKMUJWI.js.map +1 -0
  113. package/dist/chunk-ZKUGOWER.js +412 -0
  114. package/dist/chunk-ZKUGOWER.js.map +1 -0
  115. package/dist/index.cjs +843 -0
  116. package/dist/index.cjs.map +1 -0
  117. package/dist/index.d.cts +464 -0
  118. package/dist/index.d.ts +464 -0
  119. package/dist/index.js +778 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/integrations/pino.cjs +89 -0
  122. package/dist/integrations/pino.cjs.map +1 -0
  123. package/dist/integrations/pino.d.cts +65 -0
  124. package/dist/integrations/pino.d.ts +65 -0
  125. package/dist/integrations/pino.js +86 -0
  126. package/dist/integrations/pino.js.map +1 -0
  127. package/dist/langchain/index.cjs +535 -0
  128. package/dist/langchain/index.cjs.map +1 -0
  129. package/dist/langchain/index.d.cts +265 -0
  130. package/dist/langchain/index.d.ts +265 -0
  131. package/dist/langchain/index.js +528 -0
  132. package/dist/langchain/index.js.map +1 -0
  133. package/dist/langgraph/index.cjs +377 -0
  134. package/dist/langgraph/index.cjs.map +1 -0
  135. package/dist/langgraph/index.d.cts +324 -0
  136. package/dist/langgraph/index.d.ts +324 -0
  137. package/dist/langgraph/index.js +371 -0
  138. package/dist/langgraph/index.js.map +1 -0
  139. package/dist/openai/index.cjs +125 -0
  140. package/dist/openai/index.cjs.map +1 -0
  141. package/dist/openai/index.d.cts +136 -0
  142. package/dist/openai/index.d.ts +136 -0
  143. package/dist/openai/index.js +122 -0
  144. package/dist/openai/index.js.map +1 -0
  145. package/dist/openai-agents/index.cjs +689 -0
  146. package/dist/openai-agents/index.cjs.map +1 -0
  147. package/dist/openai-agents/index.d.cts +502 -0
  148. package/dist/openai-agents/index.d.ts +502 -0
  149. package/dist/openai-agents/index.js +678 -0
  150. package/dist/openai-agents/index.js.map +1 -0
  151. package/dist/spans-DZtMuBvc.d.cts +73 -0
  152. package/dist/spans-DZtMuBvc.d.ts +73 -0
  153. package/dist/tracing-BYAqjT5Q.d.cts +114 -0
  154. package/dist/tracing-rz9cWQ8d.d.ts +114 -0
  155. package/dist/vercel-ai/index.cjs +233 -0
  156. package/dist/vercel-ai/index.cjs.map +1 -0
  157. package/dist/vercel-ai/index.d.cts +93 -0
  158. package/dist/vercel-ai/index.d.ts +93 -0
  159. package/dist/vercel-ai/index.js +231 -0
  160. package/dist/vercel-ai/index.js.map +1 -0
  161. package/package.json +182 -0
@@ -0,0 +1,502 @@
1
+ /**
2
+ * Structural types for `@openai/agents` (OpenAI Agents SDK for TypeScript).
3
+ *
4
+ * No hard import — `@openai/agents` is an optional peer. Each interface
5
+ * captures the minimum surface our `wrapRunner` and event listeners need.
6
+ */
7
+ /** Event payload emitted by `Runner` during `run()`. */
8
+ interface RunnerEventLike {
9
+ type: "agent_start" | "agent_end" | "llm_call_start" | "llm_call_end" | "tool_call_start" | "tool_call_end" | "handoff" | "guardrail_check" | string;
10
+ agent?: {
11
+ name?: string;
12
+ model?: string;
13
+ [k: string]: unknown;
14
+ };
15
+ llm?: {
16
+ model?: string;
17
+ provider?: string;
18
+ usage?: {
19
+ input_tokens?: number;
20
+ output_tokens?: number;
21
+ total_tokens?: number;
22
+ [k: string]: unknown;
23
+ };
24
+ /**
25
+ * OpenAI-chat-completions-shaped request messages, when the host
26
+ * `Runner` surfaces them on the `llm_call_start` event (MHQ-750). Used
27
+ * to populate `context_segments` via the shared
28
+ * `_agent-sdk-common/context-decomposer`. Optional — hosts that don't
29
+ * surface this (or an older `@openai/agents` version) simply produce
30
+ * no context_segments, same as today.
31
+ */
32
+ messages?: unknown;
33
+ /** Tool definitions bound to this LLM call, OpenAI `tools` shape. */
34
+ tools?: unknown;
35
+ [k: string]: unknown;
36
+ };
37
+ tool?: {
38
+ name?: string;
39
+ input?: unknown;
40
+ output?: unknown;
41
+ duration_ms?: number;
42
+ success?: boolean;
43
+ [k: string]: unknown;
44
+ };
45
+ handoff?: {
46
+ from_agent?: string;
47
+ to_agent?: string;
48
+ reason?: string;
49
+ [k: string]: unknown;
50
+ };
51
+ guardrail?: {
52
+ name?: string;
53
+ passed?: boolean;
54
+ reason?: string;
55
+ [k: string]: unknown;
56
+ };
57
+ error?: unknown;
58
+ [k: string]: unknown;
59
+ }
60
+ /** Structural shape of an OpenAI Agents `Agent`. */
61
+ interface AgentLike {
62
+ name?: string;
63
+ model?: string | {
64
+ id?: string;
65
+ [k: string]: unknown;
66
+ };
67
+ instructions?: string | (() => string);
68
+ tools?: Array<{
69
+ name?: string;
70
+ [k: string]: unknown;
71
+ }>;
72
+ [k: string]: unknown;
73
+ }
74
+ /** Structural shape of a `RunResult`. */
75
+ interface RunResultLike {
76
+ finalOutput?: unknown;
77
+ newItems?: Array<{
78
+ type?: string;
79
+ [k: string]: unknown;
80
+ }>;
81
+ usage?: {
82
+ input_tokens?: number;
83
+ output_tokens?: number;
84
+ total_tokens?: number;
85
+ [k: string]: unknown;
86
+ };
87
+ /**
88
+ * MHQ-787: one entry per model round trip, in call order. The real SDK's
89
+ * default (Responses API) tracing spans carry a `response_id` but no
90
+ * `model`/`usage` of their own (see `trace-processor.ts`'s file doc) —
91
+ * this is where that data actually lives, keyed back to a span via
92
+ * `responseId` matching the span's `spanData.response_id`.
93
+ */
94
+ rawResponses?: Array<{
95
+ responseId?: string;
96
+ usage?: {
97
+ inputTokens?: number;
98
+ outputTokens?: number;
99
+ totalTokens?: number;
100
+ [k: string]: unknown;
101
+ };
102
+ [k: string]: unknown;
103
+ }>;
104
+ [k: string]: unknown;
105
+ }
106
+ /** Structural shape of `Runner`. */
107
+ interface RunnerLike {
108
+ run(agent: AgentLike, input: unknown, options?: Record<string, unknown>): Promise<RunResultLike>;
109
+ on?(event: string, handler: (payload: RunnerEventLike) => void): void;
110
+ off?(event: string, handler: (payload: RunnerEventLike) => void): void;
111
+ }
112
+ /** Module-shape probe for `instrumentOpenAIAgents({ openaiAgentsModule })`. */
113
+ interface OpenAIAgentsModuleLike {
114
+ Runner?: {
115
+ prototype: {
116
+ run: unknown;
117
+ [k: string]: unknown;
118
+ };
119
+ };
120
+ run?: (...args: unknown[]) => unknown;
121
+ default?: unknown;
122
+ [k: string]: unknown;
123
+ }
124
+
125
+ /**
126
+ * `@morsehq-dev/sdk/openai-agents` — OpenAI Agents SDK runner wrapper.
127
+ *
128
+ * This module implements ONLY the outer-span (agent) instrumentation around
129
+ * `Runner.run()`. Sibling files (`llm-and-tool.ts`, `handoff.ts`,
130
+ * `guardrail.ts`) layer the inner llm / tool / handoff / guardrail spans;
131
+ * Wave 3-O composes them via `src/openai-agents/index.ts`.
132
+ *
133
+ * Two entry points (mirroring `src/anthropic-agent-sdk/client-wrapper.ts`):
134
+ *
135
+ * - `wrapRunner(runner)` — returns a Proxy that intercepts `run()` and
136
+ * opens an outer `agent` span around the returned Promise. The rest of
137
+ * the runner surface (`on`, `off`, anything else) is forwarded.
138
+ *
139
+ * - `instrumentOpenAIAgents(runnerOrOptions, options?)` — two-mode
140
+ * installer. With a runner, equivalent to `wrapRunner`. With
141
+ * `{ openaiAgentsModule }`, best-effort patches
142
+ * `Runner.prototype.run`. Always returns the wrapped runner or a
143
+ * boolean; never throws.
144
+ *
145
+ * All telemetry paths are wrapped in `absorbErrors*` — a tracing failure
146
+ * MUST NEVER break the customer's `run()`.
147
+ */
148
+
149
+ interface WrapRunnerOptions {
150
+ /** Override the agent name on the outer span when `agent.name` is absent. */
151
+ defaultAgentName?: string;
152
+ /**
153
+ * MHQ-787: called with the resolved `RunResult` and the agent's
154
+ * configured model after each `run()` succeeds, so callers can enrich
155
+ * already-closed LLM spans with usage/model data that only becomes
156
+ * available on the result (not on the SDK's own tracing spans). Never
157
+ * called on a failed run. Errors from this callback are swallowed —
158
+ * telemetry must never break the customer's `run()`.
159
+ */
160
+ onRunResult?: (result: RunResultLike, model: string | undefined) => void;
161
+ }
162
+ /**
163
+ * Wrap a `Runner` so each `run()` call opens an outer `agent` span (via
164
+ * `emitAgentSpan` from `_agent-sdk-common`). The Proxy forwards the rest
165
+ * of the runner surface (event subscription via `on`/`off`, etc.) so
166
+ * customers see no behavior change. Idempotent.
167
+ *
168
+ * THIS WRAPPER ONLY HANDLES THE OUTER AGENT SPAN. llm / tool / handoff /
169
+ * guardrail spans are layered by sibling files (W2-O.2/.3/.4) and composed
170
+ * via `src/openai-agents/index.ts` in Wave 3-O.
171
+ */
172
+ declare function wrapRunner<R extends RunnerLike>(runner: R, options?: WrapRunnerOptions): R;
173
+ interface InstrumentOpenAIAgentsOptions extends WrapRunnerOptions {
174
+ openaiAgentsModule?: unknown;
175
+ }
176
+ type InstrumentRunnerArg = RunnerLike | InstrumentOpenAIAgentsOptions | undefined;
177
+ /**
178
+ * Top-level installer. Two modes:
179
+ *
180
+ * - `instrumentOpenAIAgents(runner)` — equivalent to `wrapRunner(runner)`.
181
+ * - `instrumentOpenAIAgents({ openaiAgentsModule: mod })` — best-effort
182
+ * prototype patch on `Runner.prototype.run`. Prefer the wrap form when
183
+ * possible.
184
+ *
185
+ * Always returns the wrapped runner, `true`/`false` for the module path, or
186
+ * `undefined` on absorbed error. Never throws.
187
+ */
188
+ declare const instrumentOpenAIAgents: (runnerOrOptions?: InstrumentRunnerArg, maybeOptions?: WrapRunnerOptions) => Promise<unknown>;
189
+ /**
190
+ * Restore the prototype patch and clear the install sentinel. Pass the same
191
+ * module reference given to `instrumentOpenAIAgents`. No-op when called with
192
+ * no module or on a wrapped-runner (vs. module) install — just discard the
193
+ * wrapper.
194
+ */
195
+ declare const uninstallOpenAIAgents: (openaiAgentsModule?: unknown) => void;
196
+ /** True when a wrapped OpenAI Agents `Runner` is passed in. */
197
+ declare function isOpenAIAgentsWrapped(runner: unknown): boolean;
198
+
199
+ /**
200
+ * `openai-agents/llm-and-tool.ts` — Phase 13 W2-O.2.
201
+ *
202
+ * Subscribes to the OpenAI Agents `Runner`'s event channel and emits `llm`
203
+ * and `tool` spans for each `*_call_start` / `*_call_end` pair.
204
+ *
205
+ * The real `@openai/agents` Runner fires events on both typed channels
206
+ * (e.g. `runner.on("llm_call_start", ...)`) and a catch-all `"event"`
207
+ * channel. We register on the typed channels only — the FakeRunner used
208
+ * in tests mirrors that behaviour, and using typed-only avoids the
209
+ * double-emit risk if a host has wired both.
210
+ *
211
+ * Call matching is FIFO via two per-installation stacks (one for llm,
212
+ * one for tool). Within a single `Runner.run` the SDK does not interleave
213
+ * calls, so FIFO is sufficient — and it tolerates orphan `_end` events
214
+ * (no matching `_start`) by simply ignoring them.
215
+ *
216
+ * All span emission goes through the shared `_agent-sdk-common/span-emitter`
217
+ * helpers which are themselves wrapped in `absorbErrorsSync` — telemetry
218
+ * MUST NOT throw at the caller.
219
+ *
220
+ * MHQ-750: when the `llm_call_start` event carries `event.llm.messages`
221
+ * (OpenAI-chat-completions-shaped, since `@openai/agents` is built on the
222
+ * same request format), they're decomposed into `context_segments` via the
223
+ * shared `_agent-sdk-common/context-decomposer` — the same decomposer used
224
+ * by `../langgraph/` and `../langchain/`. `event.llm.messages` is optional
225
+ * on the structural `RunnerEventLike` type; hosts that don't surface it
226
+ * simply produce no `context_segments`, same as before this change.
227
+ */
228
+
229
+ interface InstallLlmAndToolInstrumentationOptions {
230
+ /** Max bytes for serialized tool input summary. Defaults to redaction helper's cap. */
231
+ toolInputMaxBytes?: number;
232
+ /** Max bytes for serialized tool output summary. Defaults to redaction helper's cap. */
233
+ toolOutputMaxBytes?: number;
234
+ }
235
+ /**
236
+ * Subscribe to the runner's event channel and emit `llm` and `tool` spans
237
+ * for each `_call_start`/`_call_end` pair.
238
+ *
239
+ * Returns an unsubscribe function. If `runner.on` is not a function (e.g.
240
+ * the host injected a mock), returns a no-op unsubscriber.
241
+ */
242
+ declare function installLlmAndToolInstrumentation(runner: RunnerLike, options?: InstallLlmAndToolInstrumentationOptions): () => void;
243
+
244
+ /**
245
+ * `openai-agents/handoff.ts` — OpenAI Agents SDK handoff-span instrumentation.
246
+ *
247
+ * Subscribes to the Runner's "handoff" event channel and emits a
248
+ * `subagent.spawn`-shape span per handoff:
249
+ *
250
+ * - wire `type = "agent"`
251
+ * - `metadata.spawn_kind = "subagent"`
252
+ * - `metadata.parent_agent_name = handoff.from_agent`
253
+ * - `metadata.subagent_name = handoff.to_agent`
254
+ * - `metadata.handoff_from = handoff.from_agent` (alias for UI clarity)
255
+ * - `metadata.task_description = handoff.reason` (via emitSubagentSpan)
256
+ * - `metadata.handoff_reason = handoff.reason` (when present)
257
+ *
258
+ * The handoff event in the OpenAI Agents SDK is instantaneous — the to_agent's
259
+ * subsequent work is captured by separate llm/tool spans (W2-O.2). The span
260
+ * opened here is therefore closed immediately (duration_ms = 0).
261
+ *
262
+ * Errors emitted from inside the listener never propagate back to the runner —
263
+ * `absorbErrorsSync` swallows them and `emitSubagentSpan` itself is wrapped.
264
+ */
265
+
266
+ /**
267
+ * Subscribe to a Runner's "handoff" events and emit a `subagent.spawn`-shape
268
+ * span for each. Returns an unsubscribe function. Safely no-ops when
269
+ * `runner.on` is missing (e.g., a wrapped Runner without an event channel).
270
+ */
271
+ declare function installHandoffInstrumentation(runner: RunnerLike): () => void;
272
+
273
+ /**
274
+ * `openai-agents/guardrail.ts` — W2-O.4
275
+ *
276
+ * Subscribes to the `Runner`'s `"guardrail_check"` event channel and emits
277
+ * one guardrail-shaped span per check. Guardrails are wire-`tool` spans
278
+ * tagged with `metadata.tool_kind="guardrail"` so the dashboard can render
279
+ * them distinctly from regular tool calls and hook spans.
280
+ *
281
+ * Spans close immediately on emission. When the guardrail reports
282
+ * `passed === false`, the span closes with `failed` status and the
283
+ * reason (when present) lands in both the synthetic error message and
284
+ * `metadata.guardrail.reason`.
285
+ *
286
+ * Every emission is wrapped in `absorbErrorsSync` per the foundational
287
+ * "telemetry must never break the host" guarantee.
288
+ */
289
+
290
+ /**
291
+ * Subscribe to the runner's `"guardrail_check"` events. Emit one
292
+ * tool-shape span per check:
293
+ *
294
+ * - wire `type = "tool"`
295
+ * - `metadata.tool_kind = "guardrail"`
296
+ * - `metadata.guardrail.passed: boolean`
297
+ * - `metadata.guardrail.name: string` (when present)
298
+ * - `metadata.guardrail.reason: string` (when present, typically on fail)
299
+ *
300
+ * On failure, the span closes with `failed` status. Returns an
301
+ * unsubscribe function. No-op when `runner.on` is missing.
302
+ */
303
+ declare function installGuardrailInstrumentation(runner: RunnerLike): () => void;
304
+
305
+ /**
306
+ * `openai-agents/trace-processor.ts` — translate `@openai/agents` SDK spans
307
+ * into Morse spans via the SDK's first-class TraceProcessor pipeline.
308
+ *
309
+ * The SDK exposes `addTraceProcessor(p)` / `setTraceProcessors([p])` where
310
+ * `p` implements:
311
+ *
312
+ * - `onTraceStart(trace)`
313
+ * - `onTraceEnd(trace)`
314
+ * - `onSpanStart(span)`
315
+ * - `onSpanEnd(span)`
316
+ * - `shutdown()`
317
+ * - `forceFlush()`
318
+ *
319
+ * Each `span` carries `span.spanId` + `span.spanData.{type,name,model,usage,...}`.
320
+ * Span types observed in the SDK: `agent`, `response`, `generation`,
321
+ * `function`, `handoff`, `guardrail`, `mcp_list_tools`, `custom`.
322
+ *
323
+ * **Why this exists.** The original W2-O surfaces in this directory
324
+ * (llm-and-tool.ts, handoff.ts, guardrail.ts) listened to a hypothetical
325
+ * `runner.on('llm_call_start', ...)` event channel that the real SDK
326
+ * doesn't expose. Phase 13 Option A dogfood (commit 9e53731) confirmed
327
+ * the old installers never fired. This TraceProcessor is the canonical
328
+ * replacement — see `openspec/changes/2026-05-26-phase-13-ts-agent-sdk-adapters/`.
329
+ *
330
+ * **Telemetry containment.** Every processor method is wrapped — a
331
+ * translation failure must never bubble back into the customer's
332
+ * Runner.run().
333
+ */
334
+ /**
335
+ * Structural shape of a span passed to the processor. We avoid importing
336
+ * the real SDK types so this file builds without `@openai/agents` installed.
337
+ */
338
+ interface OpenAISpanLike {
339
+ spanId?: string;
340
+ parentId?: string;
341
+ traceId?: string;
342
+ spanData?: {
343
+ type?: string;
344
+ name?: string;
345
+ model?: string;
346
+ input?: unknown;
347
+ output?: unknown;
348
+ usage?: {
349
+ input_tokens?: number;
350
+ output_tokens?: number;
351
+ total_tokens?: number;
352
+ prompt_tokens?: number;
353
+ completion_tokens?: number;
354
+ [k: string]: unknown;
355
+ };
356
+ from_agent?: string;
357
+ to_agent?: string;
358
+ passed?: boolean;
359
+ reason?: string;
360
+ [k: string]: unknown;
361
+ };
362
+ error?: unknown;
363
+ [k: string]: unknown;
364
+ }
365
+ interface OpenAITraceLike {
366
+ traceId?: string;
367
+ name?: string;
368
+ [k: string]: unknown;
369
+ }
370
+ interface MorseTraceProcessorOptions {
371
+ toolInputMaxBytes?: number;
372
+ toolOutputMaxBytes?: number;
373
+ }
374
+ /**
375
+ * Translate OpenAI Agents SDK spans into Morse spans.
376
+ *
377
+ * Pass to the SDK via `addTraceProcessor(new MorseTraceProcessor())`
378
+ * (or via `wrapRunnerWithFullInstrumentation` which installs it for you).
379
+ *
380
+ * The processor is idempotent: registering the same instance twice is
381
+ * harmless. Multiple instances can coexist (each will translate spans
382
+ * independently — useful if you need to fan out to several backends).
383
+ */
384
+ declare class MorseTraceProcessor {
385
+ private readonly options;
386
+ private readonly openSpans;
387
+ /**
388
+ * MHQ-787: `'response'`-type spans (the SDK's default Responses-API path)
389
+ * carry a `response_id` but no `model`/`usage` of their own — that data
390
+ * only becomes available on `Runner.run()`'s resolved result
391
+ * (`result.rawResponses`), which arrives AFTER `onSpanEnd` already fired.
392
+ * Keep already-closed response spans around, keyed by `response_id`, so
393
+ * `enrichFromRunResult()` can patch them in place once the real usage
394
+ * data is available. Spans stay mutable (their span-tree object isn't
395
+ * exported until the outer trace closes), so a late patch still lands.
396
+ */
397
+ private readonly closedResponseSpans;
398
+ constructor(options?: MorseTraceProcessorOptions);
399
+ /**
400
+ * MHQ-787: called by `wrapRunnerWithFullInstrumentation` after each
401
+ * `Runner.run()` resolves. Matches `rawResponses[].responseId` back to
402
+ * the `'response'`-type span it belongs to and fills in the model/usage/
403
+ * cost fields `onSpanEnd` couldn't populate at the time. `fallbackModel`
404
+ * is the model configured on the `Agent` (known at call time in
405
+ * `runner-wrapper.ts`) — used only when the span still has no model.
406
+ * Never throws; a translation failure must not surface to the caller.
407
+ */
408
+ enrichFromRunResult(rawResponses: Array<{
409
+ responseId?: string;
410
+ usage?: {
411
+ inputTokens?: number;
412
+ outputTokens?: number;
413
+ totalTokens?: number;
414
+ };
415
+ }>, fallbackModel?: string): void;
416
+ onTraceStart(_trace: OpenAITraceLike): void;
417
+ onTraceEnd(_trace: OpenAITraceLike): void;
418
+ onSpanStart(span: OpenAISpanLike): void;
419
+ onSpanEnd(span: OpenAISpanLike): void;
420
+ shutdown(): Promise<void>;
421
+ forceFlush(): Promise<void>;
422
+ private openSpanFor;
423
+ private populateSpanFromData;
424
+ }
425
+ interface AgentsModuleLike {
426
+ addTraceProcessor?(processor: unknown): void;
427
+ setTraceProcessors?(processors: unknown[]): void;
428
+ }
429
+ /**
430
+ * Best-effort installation of an MorseTraceProcessor with the real
431
+ * `@openai/agents` SDK. Returns true if installation succeeded.
432
+ *
433
+ * Loads the module via runtime-eval'd dynamic import so the SDK is a
434
+ * pure optional peer dep — bundlers won't try to resolve it at build
435
+ * time when the customer hasn't installed it.
436
+ *
437
+ * Idempotent at the registration level: the SDK's `addTraceProcessor`
438
+ * appends to a list, so calling this twice will install two processors.
439
+ * Production callers should call once at app bootstrap.
440
+ */
441
+ declare function installMorseTraceProcessor(options?: MorseTraceProcessorOptions): Promise<{
442
+ installed: boolean;
443
+ processor: MorseTraceProcessor | null;
444
+ }>;
445
+
446
+ /**
447
+ * `@morsehq-dev/sdk/openai-agents` — adapter for `@openai/agents`.
448
+ *
449
+ * Wave 3-O composes the W2-O surfaces (`runner-wrapper`, `llm-and-tool`,
450
+ * `handoff`, `guardrail`) into a single public entry point.
451
+ *
452
+ * Two usage modes:
453
+ *
454
+ * 1. **Granular** — import any of the four installers individually and
455
+ * compose them yourself (`wrapRunner`, `installLlmAndToolInstrumentation`,
456
+ * `installHandoffInstrumentation`, `installGuardrailInstrumentation`).
457
+ *
458
+ * 2. **Full convenience** — call `wrapRunnerWithFullInstrumentation(runner)`
459
+ * to get an outer-span-wrapped Runner with llm + tool + handoff +
460
+ * guardrail event listeners pre-installed, plus a single `dispose()`
461
+ * that unsubscribes all three event installers.
462
+ *
463
+ * See `openspec/changes/2026-05-26-phase-13-ts-agent-sdk-adapters/`.
464
+ */
465
+
466
+ interface FullOpenAIInstrumentationOptions {
467
+ /** Override the agent name on the outer span when `agent.name` is absent. */
468
+ defaultAgentName?: string;
469
+ /** Max bytes for serialized tool input summary. */
470
+ toolInputMaxBytes?: number;
471
+ /** Max bytes for serialized tool output summary. */
472
+ toolOutputMaxBytes?: number;
473
+ }
474
+ /**
475
+ * Wrap an OpenAI Agents `Runner` with the full instrumentation stack:
476
+ *
477
+ * 1. Outer `agent` span via `wrapRunner` (provides the trace context).
478
+ * 2. SDK-native span translation via `MorseTraceProcessor`
479
+ * registered through the SDK's `addTraceProcessor()` pipeline —
480
+ * catches `agent` / `response` / `generation` / `function` /
481
+ * `handoff` / `guardrail` / `mcp_list_tools` / `custom` spans.
482
+ * 3. Legacy event-listener installers (`installLlmAndToolInstrumentation`,
483
+ * `installHandoffInstrumentation`, `installGuardrailInstrumentation`)
484
+ * remain wired as no-ops when the real SDK doesn't expose a
485
+ * `runner.on()` channel — they only fire against the mock fixture
486
+ * in unit tests. Real-SDK usage flows through the TraceProcessor.
487
+ *
488
+ * Returns the wrapped runner and a single `dispose()` that unsubscribes
489
+ * the legacy event installers. The TraceProcessor stays registered for
490
+ * the lifetime of the process — the SDK's `addTraceProcessor` API has no
491
+ * symmetric `removeTraceProcessor` (you'd have to `setTraceProcessors([])`
492
+ * which would nuke any other processors too).
493
+ *
494
+ * Telemetry failures NEVER propagate to the host — every emission path
495
+ * is wrapped in `absorbErrorsSync` upstream.
496
+ */
497
+ declare function wrapRunnerWithFullInstrumentation<R extends RunnerLike>(runner: R, options?: FullOpenAIInstrumentationOptions): {
498
+ runner: R;
499
+ dispose: () => void;
500
+ };
501
+
502
+ export { type AgentLike, type AgentsModuleLike, type FullOpenAIInstrumentationOptions, MorseTraceProcessor, type MorseTraceProcessorOptions, type OpenAIAgentsModuleLike, type OpenAISpanLike, type OpenAITraceLike, type RunResultLike, type RunnerEventLike, type RunnerLike, installGuardrailInstrumentation, installHandoffInstrumentation, installLlmAndToolInstrumentation, installMorseTraceProcessor, instrumentOpenAIAgents, isOpenAIAgentsWrapped, uninstallOpenAIAgents, wrapRunner, wrapRunnerWithFullInstrumentation };