@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.
- package/LICENSE +21 -0
- package/README.md +294 -0
- package/dist/anthropic/index.cjs +39 -0
- package/dist/anthropic/index.cjs.map +1 -0
- package/dist/anthropic/index.d.cts +213 -0
- package/dist/anthropic/index.d.ts +213 -0
- package/dist/anthropic/index.js +6 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/anthropic-agent-sdk/index.cjs +744 -0
- package/dist/anthropic-agent-sdk/index.cjs.map +1 -0
- package/dist/anthropic-agent-sdk/index.d.cts +371 -0
- package/dist/anthropic-agent-sdk/index.d.ts +371 -0
- package/dist/anthropic-agent-sdk/index.js +735 -0
- package/dist/anthropic-agent-sdk/index.js.map +1 -0
- package/dist/browser/anthropic/index.cjs +39 -0
- package/dist/browser/anthropic/index.cjs.map +1 -0
- package/dist/browser/anthropic/index.js +6 -0
- package/dist/browser/anthropic/index.js.map +1 -0
- package/dist/browser/anthropic-agent-sdk/index.cjs +744 -0
- package/dist/browser/anthropic-agent-sdk/index.cjs.map +1 -0
- package/dist/browser/anthropic-agent-sdk/index.js +735 -0
- package/dist/browser/anthropic-agent-sdk/index.js.map +1 -0
- package/dist/browser/chunk-3643DC7K.cjs +365 -0
- package/dist/browser/chunk-3643DC7K.cjs.map +1 -0
- package/dist/browser/chunk-4FALQMOZ.js +232 -0
- package/dist/browser/chunk-4FALQMOZ.js.map +1 -0
- package/dist/browser/chunk-5J2QBK75.js +884 -0
- package/dist/browser/chunk-5J2QBK75.js.map +1 -0
- package/dist/browser/chunk-7MSXWGAH.js +53 -0
- package/dist/browser/chunk-7MSXWGAH.js.map +1 -0
- package/dist/browser/chunk-A24L5N5N.js +596 -0
- package/dist/browser/chunk-A24L5N5N.js.map +1 -0
- package/dist/browser/chunk-B7DEUDP6.cjs +177 -0
- package/dist/browser/chunk-B7DEUDP6.cjs.map +1 -0
- package/dist/browser/chunk-F6CJACNH.cjs +604 -0
- package/dist/browser/chunk-F6CJACNH.cjs.map +1 -0
- package/dist/browser/chunk-JBOYFQSB.js +412 -0
- package/dist/browser/chunk-JBOYFQSB.js.map +1 -0
- package/dist/browser/chunk-KRBADE6R.cjs +183 -0
- package/dist/browser/chunk-KRBADE6R.cjs.map +1 -0
- package/dist/browser/chunk-MCJYZH6W.cjs +415 -0
- package/dist/browser/chunk-MCJYZH6W.cjs.map +1 -0
- package/dist/browser/chunk-NQG2IAQS.js +178 -0
- package/dist/browser/chunk-NQG2IAQS.js.map +1 -0
- package/dist/browser/chunk-O4HG3OSK.cjs +929 -0
- package/dist/browser/chunk-O4HG3OSK.cjs.map +1 -0
- package/dist/browser/chunk-QWRQJO57.js +363 -0
- package/dist/browser/chunk-QWRQJO57.js.map +1 -0
- package/dist/browser/chunk-SWQOPFE4.cjs +234 -0
- package/dist/browser/chunk-SWQOPFE4.cjs.map +1 -0
- package/dist/browser/chunk-TYDG747E.js +171 -0
- package/dist/browser/chunk-TYDG747E.js.map +1 -0
- package/dist/browser/chunk-TZRSDDFP.cjs +56 -0
- package/dist/browser/chunk-TZRSDDFP.cjs.map +1 -0
- package/dist/browser/index.cjs +587 -0
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.js +522 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/integrations/pino.cjs +89 -0
- package/dist/browser/integrations/pino.cjs.map +1 -0
- package/dist/browser/integrations/pino.js +86 -0
- package/dist/browser/integrations/pino.js.map +1 -0
- package/dist/browser/langchain/index.cjs +535 -0
- package/dist/browser/langchain/index.cjs.map +1 -0
- package/dist/browser/langchain/index.js +528 -0
- package/dist/browser/langchain/index.js.map +1 -0
- package/dist/browser/langgraph/index.cjs +377 -0
- package/dist/browser/langgraph/index.cjs.map +1 -0
- package/dist/browser/langgraph/index.js +371 -0
- package/dist/browser/langgraph/index.js.map +1 -0
- package/dist/browser/openai/index.cjs +125 -0
- package/dist/browser/openai/index.cjs.map +1 -0
- package/dist/browser/openai/index.js +122 -0
- package/dist/browser/openai/index.js.map +1 -0
- package/dist/browser/openai-agents/index.cjs +689 -0
- package/dist/browser/openai-agents/index.cjs.map +1 -0
- package/dist/browser/openai-agents/index.js +678 -0
- package/dist/browser/openai-agents/index.js.map +1 -0
- package/dist/browser/vercel-ai/index.cjs +233 -0
- package/dist/browser/vercel-ai/index.cjs.map +1 -0
- package/dist/browser/vercel-ai/index.js +231 -0
- package/dist/browser/vercel-ai/index.js.map +1 -0
- package/dist/chunk-4R4SHGOK.js +363 -0
- package/dist/chunk-4R4SHGOK.js.map +1 -0
- package/dist/chunk-7EO7MQBA.cjs +183 -0
- package/dist/chunk-7EO7MQBA.cjs.map +1 -0
- package/dist/chunk-7YCENA54.cjs +604 -0
- package/dist/chunk-7YCENA54.cjs.map +1 -0
- package/dist/chunk-CKFOGDUF.js +596 -0
- package/dist/chunk-CKFOGDUF.js.map +1 -0
- package/dist/chunk-FJUNILZT.cjs +1324 -0
- package/dist/chunk-FJUNILZT.cjs.map +1 -0
- package/dist/chunk-HDAFUKQ3.js +171 -0
- package/dist/chunk-HDAFUKQ3.js.map +1 -0
- package/dist/chunk-KBWPNIH4.cjs +234 -0
- package/dist/chunk-KBWPNIH4.cjs.map +1 -0
- package/dist/chunk-KJEO52QS.cjs +365 -0
- package/dist/chunk-KJEO52QS.cjs.map +1 -0
- package/dist/chunk-KZBCOZIQ.cjs +177 -0
- package/dist/chunk-KZBCOZIQ.cjs.map +1 -0
- package/dist/chunk-ME5JALGT.js +53 -0
- package/dist/chunk-ME5JALGT.js.map +1 -0
- package/dist/chunk-PVHDEPRE.cjs +56 -0
- package/dist/chunk-PVHDEPRE.cjs.map +1 -0
- package/dist/chunk-RTL23YOQ.js +178 -0
- package/dist/chunk-RTL23YOQ.js.map +1 -0
- package/dist/chunk-TQWI4UYO.js +1277 -0
- package/dist/chunk-TQWI4UYO.js.map +1 -0
- package/dist/chunk-VXDBDPDR.cjs +415 -0
- package/dist/chunk-VXDBDPDR.cjs.map +1 -0
- package/dist/chunk-XTKMUJWI.js +232 -0
- package/dist/chunk-XTKMUJWI.js.map +1 -0
- package/dist/chunk-ZKUGOWER.js +412 -0
- package/dist/chunk-ZKUGOWER.js.map +1 -0
- package/dist/index.cjs +843 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +464 -0
- package/dist/index.d.ts +464 -0
- package/dist/index.js +778 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/pino.cjs +89 -0
- package/dist/integrations/pino.cjs.map +1 -0
- package/dist/integrations/pino.d.cts +65 -0
- package/dist/integrations/pino.d.ts +65 -0
- package/dist/integrations/pino.js +86 -0
- package/dist/integrations/pino.js.map +1 -0
- package/dist/langchain/index.cjs +535 -0
- package/dist/langchain/index.cjs.map +1 -0
- package/dist/langchain/index.d.cts +265 -0
- package/dist/langchain/index.d.ts +265 -0
- package/dist/langchain/index.js +528 -0
- package/dist/langchain/index.js.map +1 -0
- package/dist/langgraph/index.cjs +377 -0
- package/dist/langgraph/index.cjs.map +1 -0
- package/dist/langgraph/index.d.cts +324 -0
- package/dist/langgraph/index.d.ts +324 -0
- package/dist/langgraph/index.js +371 -0
- package/dist/langgraph/index.js.map +1 -0
- package/dist/openai/index.cjs +125 -0
- package/dist/openai/index.cjs.map +1 -0
- package/dist/openai/index.d.cts +136 -0
- package/dist/openai/index.d.ts +136 -0
- package/dist/openai/index.js +122 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/openai-agents/index.cjs +689 -0
- package/dist/openai-agents/index.cjs.map +1 -0
- package/dist/openai-agents/index.d.cts +502 -0
- package/dist/openai-agents/index.d.ts +502 -0
- package/dist/openai-agents/index.js +678 -0
- package/dist/openai-agents/index.js.map +1 -0
- package/dist/spans-DZtMuBvc.d.cts +73 -0
- package/dist/spans-DZtMuBvc.d.ts +73 -0
- package/dist/tracing-BYAqjT5Q.d.cts +114 -0
- package/dist/tracing-rz9cWQ8d.d.ts +114 -0
- package/dist/vercel-ai/index.cjs +233 -0
- package/dist/vercel-ai/index.cjs.map +1 -0
- package/dist/vercel-ai/index.d.cts +93 -0
- package/dist/vercel-ai/index.d.ts +93 -0
- package/dist/vercel-ai/index.js +231 -0
- package/dist/vercel-ai/index.js.map +1 -0
- package/package.json +182 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { R as RunHandle, a as RunOptions, b as SpanOptions, S as SpanHandle } from './tracing-BYAqjT5Q.cjs';
|
|
2
|
+
export { C as ContextSegment, S as SpanData, a as SpanStatus, b as SpanType } from './spans-DZtMuBvc.cjs';
|
|
3
|
+
export { AnthropicClientLike, AnthropicContentBlockParam, AnthropicMessageCreateParams, AnthropicMessageParam, AnthropicMessageResponse, AnthropicTextBlockParam, AnthropicToolParam, AnthropicToolResultBlockParam, AnthropicToolUseBlockParam, AnthropicUsage, InstrumentAnthropicOptions, MAX_SEGMENT_CONTENT_BYTES, extractAnthropicSegments, instrumentAnthropic, isAnthropicWrapped, uninstallAnthropic, wrapAnthropic } from './anthropic/index.cjs';
|
|
4
|
+
|
|
5
|
+
/** Public SDK option. Mirrors `redaction` in `init()`. */
|
|
6
|
+
interface RedactionOptions {
|
|
7
|
+
/** Master switch. Default: `true`. */
|
|
8
|
+
enabled?: boolean;
|
|
9
|
+
/** Pattern keys to selectively disable (mandatory keys rejected). */
|
|
10
|
+
disabledPatterns?: string[];
|
|
11
|
+
/** Customer-supplied regex strings (ECMAScript syntax). */
|
|
12
|
+
extraPatterns?: string[];
|
|
13
|
+
}
|
|
14
|
+
/** Validated, frozen config fed into `redactString` / `redactValue`. */
|
|
15
|
+
interface RedactionConfig {
|
|
16
|
+
enabled: boolean;
|
|
17
|
+
/** Keys actually applied, in stable order. */
|
|
18
|
+
enabledPatterns: ReadonlySet<string>;
|
|
19
|
+
/** Custom compiled regexes — applied last, with `[REDACTED:custom]`. */
|
|
20
|
+
extraCompiled: readonly RegExp[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface BatchTransportOptions {
|
|
24
|
+
endpoint: string;
|
|
25
|
+
apiKey: string;
|
|
26
|
+
/** Max wait between flushes, in seconds. Default: 2.0. */
|
|
27
|
+
flushIntervalSeconds?: number;
|
|
28
|
+
/** Max events per batch. Default: 100. */
|
|
29
|
+
maxBatchSize?: number;
|
|
30
|
+
/** Max queued events before drop. Default: 10_000. */
|
|
31
|
+
maxQueueSize?: number;
|
|
32
|
+
/** Timeout for the underlying fetch, in ms. Default: 5_000. */
|
|
33
|
+
fetchTimeoutMs?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Pluggable fetch implementation. Defaults to `globalThis.fetch`.
|
|
36
|
+
* Useful for tests.
|
|
37
|
+
*/
|
|
38
|
+
fetchImpl?: typeof fetch;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Wire shape for a single log record. Mirrors the Python SDK payload at
|
|
42
|
+
* `apps/sdk/python/src/morse/client.py:191-211` and the backend
|
|
43
|
+
* `SimpleLogsIngestController.ingest` contract
|
|
44
|
+
* (`apps/api/apps/logs/controllers.py:66-134`).
|
|
45
|
+
*/
|
|
46
|
+
interface LogRecordPayload {
|
|
47
|
+
timestamp: string;
|
|
48
|
+
severity: string;
|
|
49
|
+
service_name: string;
|
|
50
|
+
body: string;
|
|
51
|
+
attributes: Record<string, unknown>;
|
|
52
|
+
trace_id: string | null;
|
|
53
|
+
span_id: string | null;
|
|
54
|
+
}
|
|
55
|
+
type TelemetryEvent = {
|
|
56
|
+
traces: unknown[];
|
|
57
|
+
} | {
|
|
58
|
+
logs: LogRecordPayload[];
|
|
59
|
+
} | Record<string, unknown>;
|
|
60
|
+
/**
|
|
61
|
+
* Quota / rate-limit state parsed from a 429 batch-POST response.
|
|
62
|
+
*
|
|
63
|
+
* Mirrors Python's `transport.py::BatchTransport._record_quota_state` /
|
|
64
|
+
* `quota_state()` (TQE-04 / MHQ-676) field-for-field, camelCased per this
|
|
65
|
+
* codebase's convention (`received_at` -> `receivedAt`, etc.). Two known
|
|
66
|
+
* body shapes:
|
|
67
|
+
*
|
|
68
|
+
* - `event_limit_exceeded` (traces controllers) — flat body with
|
|
69
|
+
* `current_count` / `limit_count` / `plan_id` / `period_start` /
|
|
70
|
+
* `upgrade_url` / `sample_rate_url`.
|
|
71
|
+
* - `RATE_LIMIT_INGEST` (ingest rate-limit middleware) — nested
|
|
72
|
+
* `{ error: { code, message, details: { limit, plan } } }`.
|
|
73
|
+
*
|
|
74
|
+
* Anything else is recorded as `kind: "unknown"` so the state surface
|
|
75
|
+
* still reflects that ingest is throttled even when the body doesn't
|
|
76
|
+
* parse into a known shape.
|
|
77
|
+
*/
|
|
78
|
+
interface QuotaState {
|
|
79
|
+
kind: "event_limit" | "rate_limit" | "unknown";
|
|
80
|
+
status: number;
|
|
81
|
+
/** Epoch seconds, matching Python's `time.time()`. */
|
|
82
|
+
receivedAt: number;
|
|
83
|
+
message?: string;
|
|
84
|
+
currentCount?: number;
|
|
85
|
+
limitCount?: number;
|
|
86
|
+
planId?: string;
|
|
87
|
+
periodStart?: string;
|
|
88
|
+
upgradeUrl?: string;
|
|
89
|
+
sampleRateUrl?: string;
|
|
90
|
+
quotaLimit?: number;
|
|
91
|
+
quotaRemaining?: number;
|
|
92
|
+
quotaReset?: number;
|
|
93
|
+
retryAfterSeconds?: number;
|
|
94
|
+
}
|
|
95
|
+
declare class BatchTransport {
|
|
96
|
+
private readonly endpoint;
|
|
97
|
+
private readonly apiKey;
|
|
98
|
+
private readonly flushIntervalSeconds;
|
|
99
|
+
private readonly maxBatchSize;
|
|
100
|
+
private readonly maxQueueSize;
|
|
101
|
+
private readonly fetchTimeoutMs;
|
|
102
|
+
private readonly fetchImpl;
|
|
103
|
+
private queue;
|
|
104
|
+
private timer;
|
|
105
|
+
private flushing;
|
|
106
|
+
private closed;
|
|
107
|
+
private inflight;
|
|
108
|
+
constructor(opts: BatchTransportOptions);
|
|
109
|
+
/** Enqueue an event. Non-blocking. Returns false if the queue is full. */
|
|
110
|
+
track(event: TelemetryEvent): boolean;
|
|
111
|
+
/** Block until all currently-queued events have been sent. */
|
|
112
|
+
flush(): Promise<void>;
|
|
113
|
+
/** Stop the background timer. After this call, `track()` is a no-op. */
|
|
114
|
+
shutdown(): Promise<void>;
|
|
115
|
+
private tick;
|
|
116
|
+
private sendBatch;
|
|
117
|
+
private post;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* W3C Trace Context propagation — inject and extract `traceparent` headers.
|
|
122
|
+
*
|
|
123
|
+
* W3C traceparent format: `{version}-{trace_id}-{span_id}-{flags}`
|
|
124
|
+
* - version : "00" (fixed)
|
|
125
|
+
* - trace_id : 32 lowercase hex chars (128-bit)
|
|
126
|
+
* - span_id : 16 lowercase hex chars (64-bit)
|
|
127
|
+
* - flags : "01" (sampled, always set for Morse)
|
|
128
|
+
*
|
|
129
|
+
* Example: `00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01`
|
|
130
|
+
*
|
|
131
|
+
* References:
|
|
132
|
+
* https://www.w3.org/TR/trace-context/
|
|
133
|
+
*/
|
|
134
|
+
declare const W3C_TRACEPARENT_HEADER = "traceparent";
|
|
135
|
+
/**
|
|
136
|
+
* Build a W3C traceparent value from explicit IDs.
|
|
137
|
+
*
|
|
138
|
+
* Useful when you have a trace_id / span_id from somewhere other than
|
|
139
|
+
* the active context (e.g., re-emitting an upstream trace's IDs).
|
|
140
|
+
*/
|
|
141
|
+
declare function formatTraceparent(traceId: string, spanId: string): string;
|
|
142
|
+
/**
|
|
143
|
+
* Inject the W3C `traceparent` header derived from the active trace
|
|
144
|
+
* context into the supplied headers object. Mutates and returns the
|
|
145
|
+
* object for convenience.
|
|
146
|
+
*
|
|
147
|
+
* No-op (returns headers unchanged) if no trace is active.
|
|
148
|
+
*
|
|
149
|
+
* Accepts both `Headers` (from fetch) and a plain dict.
|
|
150
|
+
*/
|
|
151
|
+
declare function injectTraceparent<T extends HeadersLike>(headers: T): T;
|
|
152
|
+
interface ExtractedContext {
|
|
153
|
+
traceId: string;
|
|
154
|
+
parentSpanId: string;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Parse a W3C traceparent header and return `{ traceId, parentSpanId }`,
|
|
158
|
+
* or `null` if the header is missing or malformed.
|
|
159
|
+
*
|
|
160
|
+
* Validation per W3C §3.2.3:
|
|
161
|
+
* - exactly 4 dash-separated fields
|
|
162
|
+
* - version === "00"
|
|
163
|
+
* - trace_id is 32 lowercase hex chars (and not all-zero)
|
|
164
|
+
* - span_id is 16 lowercase hex chars (and not all-zero)
|
|
165
|
+
*/
|
|
166
|
+
declare function extractTraceparent(headers: HeadersLike | string | null | undefined): ExtractedContext | null;
|
|
167
|
+
/**
|
|
168
|
+
* Convenience for outbound `fetch`: build a fresh `Headers` object with
|
|
169
|
+
* the traceparent injected, layered on top of the caller's input.
|
|
170
|
+
*/
|
|
171
|
+
declare function injectIntoRequestInit(init?: RequestInit): RequestInit;
|
|
172
|
+
type HeadersLike = Headers | Record<string, string | string[] | undefined> | {
|
|
173
|
+
get(name: string): string | null;
|
|
174
|
+
} | {
|
|
175
|
+
[key: string]: unknown;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Phase 12.5 Tier 1.2 — TypeScript SDK `log()` export.
|
|
180
|
+
*
|
|
181
|
+
* Closes the TS side of Feature #4 "Gap A": before this module, customers
|
|
182
|
+
* using the TS SDK had no way to populate the Logs tab of a trace's
|
|
183
|
+
* Correlation panel without standing up their own OTel collector. The
|
|
184
|
+
* Python SDK already had `morse.log(...)` (AGE-240); this is the
|
|
185
|
+
* cross-language peer.
|
|
186
|
+
*
|
|
187
|
+
* Semantics mirror Python's `morse.log()`
|
|
188
|
+
* (`apps/sdk/python/src/morse/__init__.py:360-411`):
|
|
189
|
+
*
|
|
190
|
+
* - Default severity: "info" (case-normalised to upper on the wire).
|
|
191
|
+
* - Default service_name: "morse-sdk-ts" (overridable).
|
|
192
|
+
* - Auto-reads the active trace_id + span_id from the SDK's existing
|
|
193
|
+
* `node:async_hooks.AsyncLocalStorage` context (re-uses
|
|
194
|
+
* `getActiveTraceState()` — the same primitive `run()`/`span()` use).
|
|
195
|
+
* - Batched: the record is enqueued on the existing `BatchTransport`,
|
|
196
|
+
* which flushes it to `/api/v1/logs/ingest` (derived from the traces
|
|
197
|
+
* batch endpoint) on the next tick. NO synchronous fetch per call.
|
|
198
|
+
* - Internal errors are absorbed via `absorbErrorsSync` so they never
|
|
199
|
+
* propagate into the customer's logging call. Mirrors the Python
|
|
200
|
+
* `@absorb_errors` decorator pattern.
|
|
201
|
+
*
|
|
202
|
+
* Wire shape: `{ logs: [{ timestamp, severity, service_name, body,
|
|
203
|
+
* attributes, trace_id, span_id }] }`. See `apps/api/apps/logs/
|
|
204
|
+
* controllers.py:66-134` for the API contract.
|
|
205
|
+
*/
|
|
206
|
+
/** Log severity — case-insensitive on input, upper-cased on the wire. */
|
|
207
|
+
type LogSeverity = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
|
|
208
|
+
/** Options accepted by `log()`. */
|
|
209
|
+
interface LogOptions {
|
|
210
|
+
/** Default: `"info"`. Upper-cased server-side. */
|
|
211
|
+
severity?: LogSeverity;
|
|
212
|
+
/** Free-form structured fields persisted on the record. */
|
|
213
|
+
attributes?: Record<string, unknown>;
|
|
214
|
+
/**
|
|
215
|
+
* Override the auto-detected trace_id. Use only when emitting on
|
|
216
|
+
* behalf of a known external trace (rare).
|
|
217
|
+
*/
|
|
218
|
+
traceId?: string;
|
|
219
|
+
/** Override the auto-detected span_id. Same caveat as `traceId`. */
|
|
220
|
+
spanId?: string;
|
|
221
|
+
/** Logical service tag. Default: `"morse-sdk-ts"`. */
|
|
222
|
+
serviceName?: string;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Emit a log record correlated to the current trace context.
|
|
226
|
+
*
|
|
227
|
+
* Auto-correlates to the surrounding `morse.run()` /
|
|
228
|
+
* `morse.span()` if one is active. Inside a trace, the record's
|
|
229
|
+
* `trace_id` + `span_id` are filled in from `AsyncLocalStorage`;
|
|
230
|
+
* outside a trace, both fields are `null` and the record is still
|
|
231
|
+
* accepted by the backend (it surfaces in the service-wide Logs view
|
|
232
|
+
* but not on any individual trace's Correlation panel).
|
|
233
|
+
*
|
|
234
|
+
* Non-blocking. Batched alongside trace events on the existing
|
|
235
|
+
* transport — flushes at the SDK's configured interval (default 2s).
|
|
236
|
+
* Internal errors are absorbed; this call never throws.
|
|
237
|
+
*
|
|
238
|
+
* Silent no-op when the SDK isn't initialised (no `MORSE_API_KEY`
|
|
239
|
+
* and no explicit `init()` call).
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* import { init, runTraced, log } from "@morsehq-dev/sdk";
|
|
244
|
+
*
|
|
245
|
+
* init({ apiKey: process.env.MORSE_API_KEY });
|
|
246
|
+
*
|
|
247
|
+
* await runTraced({ agentName: "lead-qualifier" }, async () => {
|
|
248
|
+
* log("starting search", { attributes: { query: "morse" } });
|
|
249
|
+
* const results = await search();
|
|
250
|
+
* log("search complete", {
|
|
251
|
+
* severity: "info",
|
|
252
|
+
* attributes: { result_count: results.length },
|
|
253
|
+
* });
|
|
254
|
+
* });
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
declare const log: (message: string, options?: LogOptions | undefined) => void | undefined;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* `@morsehq-dev/sdk` — TypeScript SDK for Morse observability.
|
|
261
|
+
*
|
|
262
|
+
* V2 module-level singleton API:
|
|
263
|
+
*
|
|
264
|
+
* ```ts
|
|
265
|
+
* import * as morse from "@morsehq-dev/sdk";
|
|
266
|
+
*
|
|
267
|
+
* // Three-step zero-setup integration:
|
|
268
|
+
* // 1. Generate an API key in the Morse dashboard.
|
|
269
|
+
* // 2. npm install @morsehq-dev/sdk
|
|
270
|
+
* // 3. Set MORSE_API_KEY in your environment.
|
|
271
|
+
*
|
|
272
|
+
* morse.track({ agentId: "lead-qualifier", runData: { success: true } });
|
|
273
|
+
*
|
|
274
|
+
* // Or call init() explicitly to override env-var resolution:
|
|
275
|
+
* morse.init({
|
|
276
|
+
* apiKey: "mhq_xxx", // pragma: allowlist secret — placeholder, not a key
|
|
277
|
+
* endpoint: "https://api.morsehq.dev/api/v1/traces/batch",
|
|
278
|
+
* flushIntervalSeconds: 2,
|
|
279
|
+
* });
|
|
280
|
+
* ```
|
|
281
|
+
*
|
|
282
|
+
* Non-invasive guarantee: every public function is wrapped with
|
|
283
|
+
* `absorbErrors`. Telemetry never throws back at the caller; internal
|
|
284
|
+
* errors are reported out-of-band to Morse's Sentry instance.
|
|
285
|
+
*
|
|
286
|
+
* The internal `BatchTransport` and `TraceState` types back the singleton
|
|
287
|
+
* but are NOT part of the public API. Customers should never reach into
|
|
288
|
+
* the `_internal` directory.
|
|
289
|
+
*/
|
|
290
|
+
|
|
291
|
+
declare const version: string;
|
|
292
|
+
/** Options accepted by `init()`. Mirrors Python's `init(...)` kwargs. */
|
|
293
|
+
interface InitOptions {
|
|
294
|
+
/** API key (starts with "mhq_"). Required if no `MORSE_API_KEY` env var. */
|
|
295
|
+
apiKey?: string;
|
|
296
|
+
/** Full ingestion URL. Defaults to the production Railway endpoint. */
|
|
297
|
+
endpoint?: string;
|
|
298
|
+
/** Seconds between batch flushes. Default: 2. */
|
|
299
|
+
flushIntervalSeconds?: number;
|
|
300
|
+
/**
|
|
301
|
+
* Client-side PII redaction. Default-on with the canonical eight-pattern
|
|
302
|
+
* catalogue. Set `enabled: false` to opt out (the server-side mandatory
|
|
303
|
+
* floor still applies). Mandatory patterns (`api_key_prefixed`, `jwt`,
|
|
304
|
+
* `credit_card`, `ssn_us`) cannot be added to `disabledPatterns` —
|
|
305
|
+
* `init()` throws on that.
|
|
306
|
+
*/
|
|
307
|
+
redaction?: RedactionOptions;
|
|
308
|
+
/**
|
|
309
|
+
* INTERNAL USE ONLY — resource-level source label for Morse's own
|
|
310
|
+
* dogfood emitters. Mirrors the Python SDK's `init(source=...)` kwarg
|
|
311
|
+
* (PR #90). Accepted values: `"copilot" | "investigator" | "refagent"
|
|
312
|
+
* | "ui-bot"`. Any other value (including misspellings) is silently
|
|
313
|
+
* dropped — falls back to the `MORSE_INTERNAL_SOURCE` env var.
|
|
314
|
+
* Customers should not set this.
|
|
315
|
+
*/
|
|
316
|
+
source?: string;
|
|
317
|
+
}
|
|
318
|
+
/** Single agent-run payload accepted by `track()`. */
|
|
319
|
+
interface TrackOptions {
|
|
320
|
+
/** User-defined agent name (e.g., "lead-qualifier"). */
|
|
321
|
+
agentId: string;
|
|
322
|
+
/** Run-data dict. Shape mirrors backend Run model. */
|
|
323
|
+
runData: Record<string, unknown>;
|
|
324
|
+
}
|
|
325
|
+
/** High-level outcome shape accepted by `record()` (V1 ergonomic API). */
|
|
326
|
+
interface RunRecord {
|
|
327
|
+
agent: string;
|
|
328
|
+
success?: boolean;
|
|
329
|
+
outcome?: string;
|
|
330
|
+
cost?: number;
|
|
331
|
+
model?: string;
|
|
332
|
+
inputTokens?: number;
|
|
333
|
+
outputTokens?: number;
|
|
334
|
+
totalTokens?: number;
|
|
335
|
+
durationMs?: number;
|
|
336
|
+
errorMessage?: string;
|
|
337
|
+
metadata?: Record<string, unknown>;
|
|
338
|
+
status?: "completed" | "failed" | "timed_out";
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Initialize the Morse SDK singleton. Call once at application
|
|
342
|
+
* startup. After this call, `track()`, `batchTrack()`, `run()`, and
|
|
343
|
+
* `flush()` are wired up to the configured endpoint.
|
|
344
|
+
*
|
|
345
|
+
* `endpoint` is optional — when omitted, the SDK resolves in this order:
|
|
346
|
+
* 1. `MORSE_ENDPOINT` env var (if set)
|
|
347
|
+
* 2. The default Railway production URL.
|
|
348
|
+
*
|
|
349
|
+
* `MORSE_DISABLED=1` overrides everything: `init()` is a no-op.
|
|
350
|
+
*
|
|
351
|
+
* Calling `init()` a second time replaces the singleton — the previous
|
|
352
|
+
* transport is shut down cleanly and a new one is constructed.
|
|
353
|
+
*
|
|
354
|
+
* Validation errors (e.g., disabling a mandatory redaction pattern, an
|
|
355
|
+
* uncompilable extra pattern) are deliberately NOT absorbed — they
|
|
356
|
+
* propagate as `Error`s so the developer sees the misconfiguration at
|
|
357
|
+
* application startup. All other errors raised from internal SDK paths
|
|
358
|
+
* are absorbed via the per-call wrappers downstream.
|
|
359
|
+
*/
|
|
360
|
+
declare function init(options?: InitOptions): void;
|
|
361
|
+
/**
|
|
362
|
+
* Submit a single agent run to the ingestion endpoint. Non-blocking;
|
|
363
|
+
* returns immediately. Silently no-ops if the SDK isn't initialised.
|
|
364
|
+
*/
|
|
365
|
+
declare const track: (options: TrackOptions) => void | undefined;
|
|
366
|
+
/** Submit multiple agent runs in bulk. Silently no-ops if uninitialised. */
|
|
367
|
+
declare const batchTrack: (runs: Record<string, unknown>[]) => void | undefined;
|
|
368
|
+
/**
|
|
369
|
+
* High-level convenience: record an agent outcome with named fields.
|
|
370
|
+
*
|
|
371
|
+
* Builds a synthetic single-span `agent`-type trace and submits it through
|
|
372
|
+
* the same `traces`-shaped emission path `run()`/`span()` use (MHQ-746) —
|
|
373
|
+
* mirrors Python's `client.py::record()`. The live dev-api backend has no
|
|
374
|
+
* `runs` ingest route; sending through `state.transport.track()` (the
|
|
375
|
+
* shape `track()`/`batchTrack()` still use, matching Python's equally
|
|
376
|
+
* unmigrated `track()`/`batch_track()`) 422s silently. See MHQ-746.
|
|
377
|
+
*/
|
|
378
|
+
declare const record: (rec: RunRecord) => void | undefined;
|
|
379
|
+
/**
|
|
380
|
+
* Open a traced agent run, executing `fn` inside the trace context.
|
|
381
|
+
* Mirrors Python's `morse.run(...)` context manager.
|
|
382
|
+
*
|
|
383
|
+
* Two overloads are supported:
|
|
384
|
+
* - `run(name, fn)` — quick form with default options.
|
|
385
|
+
* - `run({ agentName, incomingTraceparent }, fn)` — full options.
|
|
386
|
+
*
|
|
387
|
+
* Async callers receive a Promise; sync callers receive the value
|
|
388
|
+
* directly. The function's return value is forwarded to the caller.
|
|
389
|
+
*/
|
|
390
|
+
declare function run<T>(name: string, fn: (handle: RunHandle) => T): T | undefined;
|
|
391
|
+
declare function run<T>(options: RunOptions, fn: (handle: RunHandle) => T): T | undefined;
|
|
392
|
+
/** Async variant of `run` — exported for callers who want explicit typing. */
|
|
393
|
+
declare function runTraced<T>(options: RunOptions, fn: (handle: RunHandle) => Promise<T>): Promise<T | undefined>;
|
|
394
|
+
/**
|
|
395
|
+
* Attach cost-attribution dimensions to the active trace (MHQ-751). Call
|
|
396
|
+
* once per request (e.g. in middleware) to propagate `customerId`,
|
|
397
|
+
* `feature`, `env`, `team`, etc. to all child spans downstream. Values are
|
|
398
|
+
* coerced to strings. No-op outside an active trace — never throws.
|
|
399
|
+
* Mirrors Python's `morse_ai.set_context(**dims)`.
|
|
400
|
+
*/
|
|
401
|
+
declare function setContext(dims: Record<string, unknown>): void;
|
|
402
|
+
/** Start a child span under the active trace. Returns null if no trace. */
|
|
403
|
+
declare function startSpan(options: SpanOptions): SpanHandle | null;
|
|
404
|
+
/** Run `fn` inside a child span. No-op if no trace is active. */
|
|
405
|
+
declare function span<T>(options: SpanOptions, fn: (handle: SpanHandle | null) => T): T;
|
|
406
|
+
/** Async variant of `span`. */
|
|
407
|
+
declare function spanAsync<T>(options: SpanOptions, fn: (handle: SpanHandle | null) => Promise<T>): Promise<T>;
|
|
408
|
+
/** Block until all queued events have been sent. Silent on uninit / errors. */
|
|
409
|
+
declare const flush: () => Promise<void | undefined>;
|
|
410
|
+
/**
|
|
411
|
+
* Latest ingest quota / rate-limit state observed by the SDK, or `null`.
|
|
412
|
+
*
|
|
413
|
+
* TQE-04 (MHQ-676/MHQ-749): when the Morse backend answers a telemetry
|
|
414
|
+
* batch with HTTP 429 — the monthly event limit (`event_limit_exceeded`)
|
|
415
|
+
* or the per-second ingest rate limit (`RATE_LIMIT_INGEST`) — the
|
|
416
|
+
* transport parses the response and stores it. This function exposes
|
|
417
|
+
* that state so host applications can alert on quota exhaustion instead
|
|
418
|
+
* of discovering it from missing telemetry. Mirrors Python's
|
|
419
|
+
* `morse_ai.quota_state()`.
|
|
420
|
+
*
|
|
421
|
+
* Returns `null` when the SDK is uninitialized or no 429 has been
|
|
422
|
+
* observed. The object includes at least `kind` (`"event_limit"`,
|
|
423
|
+
* `"rate_limit"`, or `"unknown"`), `status`, and `receivedAt` (epoch
|
|
424
|
+
* seconds); event-limit states add `currentCount` / `limitCount` /
|
|
425
|
+
* `planId` / `periodStart` / `upgradeUrl`.
|
|
426
|
+
*/
|
|
427
|
+
declare const quotaState: () => QuotaState | null | undefined;
|
|
428
|
+
/** Flush and stop the background transport. Call on app exit. */
|
|
429
|
+
declare const shutdown: () => Promise<void | undefined>;
|
|
430
|
+
/**
|
|
431
|
+
* Reset the module-level singleton for tests. Customer code must NOT
|
|
432
|
+
* call this — the singleton is meant to live for the process lifetime.
|
|
433
|
+
*
|
|
434
|
+
* @internal
|
|
435
|
+
*/
|
|
436
|
+
declare function _resetForTesting(): void;
|
|
437
|
+
/**
|
|
438
|
+
* Inject an alternate transport factory for tests (e.g., a mock that
|
|
439
|
+
* captures sent payloads instead of opening a fetch).
|
|
440
|
+
*
|
|
441
|
+
* @internal
|
|
442
|
+
*/
|
|
443
|
+
declare function _setTransportFactoryForTesting(factory: (opts: {
|
|
444
|
+
apiKey: string;
|
|
445
|
+
endpoint: string;
|
|
446
|
+
flushIntervalSeconds: number;
|
|
447
|
+
}) => BatchTransport): void;
|
|
448
|
+
/**
|
|
449
|
+
* Read the most recently completed trace entry, for test assertions.
|
|
450
|
+
*
|
|
451
|
+
* @internal
|
|
452
|
+
*/
|
|
453
|
+
declare function _getLastTraceForTesting(): {
|
|
454
|
+
trace: unknown;
|
|
455
|
+
spans: unknown[];
|
|
456
|
+
} | null;
|
|
457
|
+
/**
|
|
458
|
+
* Read the active trace state, for test assertions.
|
|
459
|
+
*
|
|
460
|
+
* @internal
|
|
461
|
+
*/
|
|
462
|
+
declare function _getActiveTraceStateForTesting(): unknown;
|
|
463
|
+
|
|
464
|
+
export { type InitOptions, type LogOptions, type LogSeverity, type QuotaState, type RedactionConfig, type RedactionOptions, RunHandle, RunOptions, type RunRecord, SpanHandle, SpanOptions, type TrackOptions, W3C_TRACEPARENT_HEADER, _getActiveTraceStateForTesting, _getLastTraceForTesting, _resetForTesting, _setTransportFactoryForTesting, batchTrack, extractTraceparent, flush, formatTraceparent, init, injectIntoRequestInit, injectTraceparent, log, quotaState, record, run, runTraced, setContext, shutdown, span, spanAsync, startSpan, track, version };
|