mesedi 0.1.0 → 0.6.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.
- package/README.md +81 -31
- package/dist/_errorClassesGenerated.d.ts +18 -0
- package/dist/_errorClassesGenerated.d.ts.map +1 -0
- package/dist/_errorClassesGenerated.js +24 -0
- package/dist/_errorClassesGenerated.js.map +1 -0
- package/dist/anthropic_integration.d.ts +9 -9
- package/dist/anthropic_integration.d.ts.map +1 -1
- package/dist/anthropic_integration.js +67 -26
- package/dist/anthropic_integration.js.map +1 -1
- package/dist/client.d.ts +32 -12
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +46 -18
- package/dist/client.js.map +1 -1
- package/dist/cohere_integration.d.ts +42 -0
- package/dist/cohere_integration.d.ts.map +1 -0
- package/dist/cohere_integration.js +456 -0
- package/dist/cohere_integration.js.map +1 -0
- package/dist/compress.d.ts +69 -0
- package/dist/compress.d.ts.map +1 -0
- package/dist/compress.js +75 -0
- package/dist/compress.js.map +1 -0
- package/dist/context.d.ts +13 -4
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +15 -5
- package/dist/context.js.map +1 -1
- package/dist/errors.d.ts +137 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +462 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +19 -6
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +31 -6
- package/dist/events.js.map +1 -1
- package/dist/gemini_integration.d.ts +33 -0
- package/dist/gemini_integration.d.ts.map +1 -0
- package/dist/gemini_integration.js +334 -0
- package/dist/gemini_integration.js.map +1 -0
- package/dist/halt.d.ts +12 -12
- package/dist/halt.js +16 -16
- package/dist/halt.js.map +1 -1
- package/dist/halt_stream.d.ts +6 -6
- package/dist/halt_stream.d.ts.map +1 -1
- package/dist/halt_stream.js +22 -12
- package/dist/halt_stream.js.map +1 -1
- package/dist/index.d.ts +26 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -12
- package/dist/index.js.map +1 -1
- package/dist/integrations/langgraph.d.ts +99 -0
- package/dist/integrations/langgraph.d.ts.map +1 -0
- package/dist/integrations/langgraph.js +572 -0
- package/dist/integrations/langgraph.js.map +1 -0
- package/dist/integrations/mastra.d.ts +114 -0
- package/dist/integrations/mastra.d.ts.map +1 -0
- package/dist/integrations/mastra.js +354 -0
- package/dist/integrations/mastra.js.map +1 -0
- package/dist/integrations/openai_agents.d.ts +66 -0
- package/dist/integrations/openai_agents.d.ts.map +1 -0
- package/dist/integrations/openai_agents.js +243 -0
- package/dist/integrations/openai_agents.js.map +1 -0
- package/dist/integrations/promptfoo.d.ts +62 -0
- package/dist/integrations/promptfoo.d.ts.map +1 -0
- package/dist/integrations/promptfoo.js +76 -0
- package/dist/integrations/promptfoo.js.map +1 -0
- package/dist/integrations/ragas.d.ts +65 -0
- package/dist/integrations/ragas.d.ts.map +1 -0
- package/dist/integrations/ragas.js +78 -0
- package/dist/integrations/ragas.js.map +1 -0
- package/dist/integrations/vectara.d.ts +60 -0
- package/dist/integrations/vectara.d.ts.map +1 -0
- package/dist/integrations/vectara.js +59 -0
- package/dist/integrations/vectara.js.map +1 -0
- package/dist/integrations/vercel_ai.d.ts +6 -6
- package/dist/integrations/vercel_ai.js +10 -10
- package/dist/integrations/vercel_ai.js.map +1 -1
- package/dist/observe.d.ts +358 -3
- package/dist/observe.d.ts.map +1 -1
- package/dist/observe.js +536 -4
- package/dist/observe.js.map +1 -1
- package/dist/ollama_integration.d.ts +126 -0
- package/dist/ollama_integration.d.ts.map +1 -0
- package/dist/ollama_integration.js +404 -0
- package/dist/ollama_integration.js.map +1 -0
- package/dist/openai_integration.d.ts +67 -0
- package/dist/openai_integration.d.ts.map +1 -0
- package/dist/openai_integration.js +574 -0
- package/dist/openai_integration.js.map +1 -0
- package/dist/tool.d.ts +1 -1
- package/dist/tool.d.ts.map +1 -1
- package/dist/tool.js +193 -10
- package/dist/tool.js.map +1 -1
- package/dist/truncate.d.ts +84 -0
- package/dist/truncate.d.ts.map +1 -0
- package/dist/truncate.js +134 -0
- package/dist/truncate.js.map +1 -0
- package/dist/vertex_gemini_integration.d.ts +27 -0
- package/dist/vertex_gemini_integration.d.ts.map +1 -0
- package/dist/vertex_gemini_integration.js +229 -0
- package/dist/vertex_gemini_integration.js.map +1 -0
- package/dist/wrap.d.ts +39 -3
- package/dist/wrap.d.ts.map +1 -1
- package/dist/wrap.js +15 -12
- package/dist/wrap.js.map +1 -1
- package/package.json +28 -4
package/README.md
CHANGED
|
@@ -1,28 +1,18 @@
|
|
|
1
1
|
# Mesedi TypeScript SDK
|
|
2
2
|
|
|
3
|
-
**Status:**
|
|
3
|
+
**Status:** v0.4.0. Live on npm.
|
|
4
4
|
|
|
5
5
|
The TypeScript companion to `sdk-python/`. Feature parity for the v1
|
|
6
|
-
surface
|
|
7
|
-
fail-open posture
|
|
8
|
-
`AsyncLocalStorage
|
|
6
|
+
surface (`configure()`, `wrap()`, `tool()`, async event shipper,
|
|
7
|
+
fail-open posture), built on Node 18+ native `fetch`,
|
|
8
|
+
`AsyncLocalStorage`, and `node:zlib` for opt-in gzip compression of
|
|
9
|
+
request bodies above 1 KB. **Zero runtime dependencies.** Compresses
|
|
10
|
+
or not transparently; small calls ship uncompressed as before.
|
|
9
11
|
|
|
10
|
-
##
|
|
11
|
-
|
|
12
|
-
Prerequisites: Mesedi backend running on `localhost:8080`. See
|
|
13
|
-
`../backend/README.md` if not yet running.
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
cd ~/mesedi/sdk-typescript
|
|
17
|
-
npm install # installs only devDeps (TypeScript)
|
|
18
|
-
npm run build # compile src/ → dist/
|
|
19
|
-
node sandbox/real_agent.js
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
Or run the sandbox in one shot:
|
|
12
|
+
## Install
|
|
23
13
|
|
|
24
14
|
```bash
|
|
25
|
-
npm
|
|
15
|
+
npm install mesedi
|
|
26
16
|
```
|
|
27
17
|
|
|
28
18
|
## API
|
|
@@ -35,12 +25,12 @@ configure({
|
|
|
35
25
|
baseUrl: "http://localhost:8080",
|
|
36
26
|
});
|
|
37
27
|
|
|
38
|
-
// Define a tool
|
|
28
|
+
// Define a tool. Observed when called from inside a wrap()'d function.
|
|
39
29
|
const searchWeb = tool({ name: "search_web" }, async (q: string) => {
|
|
40
30
|
return ["result1", "result2"];
|
|
41
31
|
});
|
|
42
32
|
|
|
43
|
-
// Wrap an agent function
|
|
33
|
+
// Wrap an agent function. Records start/complete/crash automatically.
|
|
44
34
|
const runAgent = wrap(async (query: string) => {
|
|
45
35
|
const results = await searchWeb(query);
|
|
46
36
|
return `found ${results.length} results`;
|
|
@@ -57,7 +47,7 @@ await flush();
|
|
|
57
47
|
For each `wrap()`-decorated call:
|
|
58
48
|
|
|
59
49
|
- **On entry:** `POST /executions` (status=started, sdk_language=typescript,
|
|
60
|
-
sdk_version=0.0
|
|
50
|
+
sdk_version=0.4.0).
|
|
61
51
|
- **On normal return:** `PATCH /executions/{id}` (status=completed,
|
|
62
52
|
duration_ms, ended_at).
|
|
63
53
|
- **On thrown error:** `PATCH /executions/{id}` (status=crashed,
|
|
@@ -72,11 +62,59 @@ For each `tool()`-decorated call (from inside a `wrap()`):
|
|
|
72
62
|
|
|
73
63
|
All HTTP is async via a single in-process queue + a `setInterval`
|
|
74
64
|
drainer. Network failures during observation NEVER throw back into the
|
|
75
|
-
wrapped agent
|
|
65
|
+
wrapped agent. The SDK is fail-open: a Mesedi outage degrades to
|
|
76
66
|
invisibility, not to broken production code.
|
|
77
67
|
|
|
78
68
|
## Framework integrations
|
|
79
69
|
|
|
70
|
+
Adapter modules under `mesedi/integrations/*` translate each framework's
|
|
71
|
+
native callback or hook surface into Mesedi telemetry. They're optional;
|
|
72
|
+
importing `mesedi` itself never requires any framework to be installed.
|
|
73
|
+
|
|
74
|
+
Currently shipping: **LangGraph**, **OpenAI Agents SDK**, and **Vercel AI
|
|
75
|
+
SDK**. Each peer dependency is opt-in.
|
|
76
|
+
|
|
77
|
+
### LangGraph
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { configure, wrap } from "mesedi";
|
|
81
|
+
import { instrumentGraph } from "mesedi/integrations/langgraph";
|
|
82
|
+
|
|
83
|
+
configure({ apiKey: process.env.MESEDI_API_KEY! });
|
|
84
|
+
|
|
85
|
+
export const runMyGraph = wrap(async (question: string) => {
|
|
86
|
+
const graph = buildGraph();
|
|
87
|
+
instrumentGraph(graph);
|
|
88
|
+
const result = await graph.invoke({ input: question });
|
|
89
|
+
return result.output;
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`instrumentGraph` attaches Mesedi telemetry to each node in the graph
|
|
94
|
+
and emits `llm_call` and `tool_call` events labeled with the node name,
|
|
95
|
+
so the dashboard timeline shows the graph's flow alongside per-step
|
|
96
|
+
detail.
|
|
97
|
+
|
|
98
|
+
### OpenAI Agents SDK
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { configure, wrap } from "mesedi";
|
|
102
|
+
import { instrumentAgent } from "mesedi/integrations/openai_agents";
|
|
103
|
+
|
|
104
|
+
configure({ apiKey: process.env.MESEDI_API_KEY! });
|
|
105
|
+
|
|
106
|
+
export const runMyAgent = wrap(async (question: string) => {
|
|
107
|
+
const agent = buildAgent();
|
|
108
|
+
instrumentAgent(agent);
|
|
109
|
+
return agent.run(question);
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`instrumentAgent` subscribes to the OpenAI Agents SDK's lifecycle hooks
|
|
114
|
+
and emits `llm_call` + `tool_call` events with the same wire format as
|
|
115
|
+
the LangGraph and Vercel AI SDK adapters, so detectors see no
|
|
116
|
+
difference.
|
|
117
|
+
|
|
80
118
|
### Vercel AI SDK
|
|
81
119
|
|
|
82
120
|
If your agent uses Vercel's `ai` package (`generateText`, multi-step
|
|
@@ -113,25 +151,37 @@ Per invocation, the wrapper emits:
|
|
|
113
151
|
|
|
114
152
|
- One `llm_call` event per step (Vercel's multi-step ReAct surfaces
|
|
115
153
|
intermediate reasoning + final answer on `result.steps`). Model id,
|
|
116
|
-
user message, system prompt, token usage, response text
|
|
154
|
+
user message, system prompt, token usage, response text, all
|
|
117
155
|
captured in the standard Mesedi wire format.
|
|
118
156
|
- One `tool_call` event per tool invocation in each step. Pairs
|
|
119
157
|
`result.toolCalls[i]` to `result.toolResults` by `toolCallId`,
|
|
120
158
|
detects failure mode (missing result OR `result.error` field) and
|
|
121
159
|
records `status=failed` with `exception_type` / `exception_message`.
|
|
122
160
|
|
|
123
|
-
Detectors
|
|
124
|
-
cost-velocity, prompt-injection
|
|
161
|
+
Detectors (drift, identical/similar-call loops, tool-failures,
|
|
162
|
+
cost-velocity, prompt-injection) see the same wire format as a
|
|
125
163
|
hand-written `mesedi` instrumentation produces.
|
|
126
164
|
|
|
127
|
-
`ai` is declared as an **optional peer dependency
|
|
165
|
+
`ai` is declared as an **optional peer dependency**. Installing
|
|
128
166
|
`mesedi` doesn't require it. If your project already has `ai`
|
|
129
167
|
installed for `generateText`, the integration just works.
|
|
130
168
|
|
|
131
|
-
`streamText` and `generateObject`
|
|
169
|
+
Only `generateText` is wrapped today. `streamText` and `generateObject`
|
|
170
|
+
are not currently supported; use hand-instrumentation with
|
|
171
|
+
`emitLLMCall` / `tool()` for those code paths.
|
|
172
|
+
|
|
173
|
+
## Releases
|
|
174
|
+
|
|
175
|
+
This SDK is published to npm via OIDC Trusted Publishing from the
|
|
176
|
+
`release-sdk-typescript.yml` GitHub Actions workflow, with no long-lived
|
|
177
|
+
NPM_TOKEN secret. Every release carries an npm provenance attestation
|
|
178
|
+
linking it to a specific commit in `mesedi-ai/mesedi`.
|
|
132
179
|
|
|
133
|
-
|
|
180
|
+
To cut a new release, bump `version` in `package.json`, commit, then:
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
git tag -a sdk-typescript-v0.X.Y -m "Release sdk-typescript v0.X.Y"
|
|
184
|
+
git push origin sdk-typescript-v0.X.Y
|
|
185
|
+
```
|
|
134
186
|
|
|
135
|
-
|
|
136
|
-
trusted-publishing flow ships post-LOI alongside the SDK ecosystem
|
|
137
|
-
work in Phase 16.
|
|
187
|
+
The workflow installs, builds, and publishes with `--provenance`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AUTO-GENERATED by scripts/codegen.py from spec/error_classes.yaml. DO NOT EDIT BY HAND. Re-run codegen after editing the spec.
|
|
3
|
+
*/
|
|
4
|
+
/** Canonical ErrorClass constants (wire-format values). */
|
|
5
|
+
export declare const ERROR_CLASS_NAMES: {
|
|
6
|
+
readonly RATE_LIMITED: "rate_limited";
|
|
7
|
+
readonly QUOTA_EXHAUSTED: "quota_exhausted";
|
|
8
|
+
readonly INTERNAL_ERROR: "internal_error";
|
|
9
|
+
readonly SERVICE_UNAVAILABLE: "service_unavailable";
|
|
10
|
+
readonly TIMEOUT: "timeout";
|
|
11
|
+
readonly INVALID_API_KEY: "invalid_api_key";
|
|
12
|
+
readonly CLIENT_ERROR: "client_error";
|
|
13
|
+
readonly UNKNOWN: "unknown";
|
|
14
|
+
};
|
|
15
|
+
/** Subset of ErrorClass values that count as 'provider-side' for
|
|
16
|
+
* the provider_incident detector. */
|
|
17
|
+
export declare const PROVIDER_SIDE_ERROR_CLASS_VALUES: ReadonlySet<string>;
|
|
18
|
+
//# sourceMappingURL=_errorClassesGenerated.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_errorClassesGenerated.d.ts","sourceRoot":"","sources":["../src/_errorClassesGenerated.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,2DAA2D;AAC3D,eAAO,MAAM,iBAAiB;;;;;;;;;CASpB,CAAC;AAEX;qCACqC;AACrC,eAAO,MAAM,gCAAgC,EAAE,WAAW,CAAC,MAAM,CAM/D,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AUTO-GENERATED by scripts/codegen.py from spec/error_classes.yaml. DO NOT EDIT BY HAND. Re-run codegen after editing the spec.
|
|
3
|
+
*/
|
|
4
|
+
/** Canonical ErrorClass constants (wire-format values). */
|
|
5
|
+
export const ERROR_CLASS_NAMES = {
|
|
6
|
+
RATE_LIMITED: "rate_limited",
|
|
7
|
+
QUOTA_EXHAUSTED: "quota_exhausted",
|
|
8
|
+
INTERNAL_ERROR: "internal_error",
|
|
9
|
+
SERVICE_UNAVAILABLE: "service_unavailable",
|
|
10
|
+
TIMEOUT: "timeout",
|
|
11
|
+
INVALID_API_KEY: "invalid_api_key",
|
|
12
|
+
CLIENT_ERROR: "client_error",
|
|
13
|
+
UNKNOWN: "unknown",
|
|
14
|
+
};
|
|
15
|
+
/** Subset of ErrorClass values that count as 'provider-side' for
|
|
16
|
+
* the provider_incident detector. */
|
|
17
|
+
export const PROVIDER_SIDE_ERROR_CLASS_VALUES = new Set([
|
|
18
|
+
"rate_limited", // RATE_LIMITED
|
|
19
|
+
"quota_exhausted", // QUOTA_EXHAUSTED
|
|
20
|
+
"internal_error", // INTERNAL_ERROR
|
|
21
|
+
"service_unavailable", // SERVICE_UNAVAILABLE
|
|
22
|
+
"timeout", // TIMEOUT
|
|
23
|
+
]);
|
|
24
|
+
//# sourceMappingURL=_errorClassesGenerated.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_errorClassesGenerated.js","sourceRoot":"","sources":["../src/_errorClassesGenerated.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,2DAA2D;AAC3D,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,YAAY,EAAE,cAAc;IAC5B,eAAe,EAAE,iBAAiB;IAClC,cAAc,EAAE,gBAAgB;IAChC,mBAAmB,EAAE,qBAAqB;IAC1C,OAAO,EAAE,SAAS;IAClB,eAAe,EAAE,iBAAiB;IAClC,YAAY,EAAE,cAAc;IAC5B,OAAO,EAAE,SAAS;CACV,CAAC;AAEX;qCACqC;AACrC,MAAM,CAAC,MAAM,gCAAgC,GAAwB,IAAI,GAAG,CAAC;IAC3E,cAAc,EAAE,eAAe;IAC/B,iBAAiB,EAAE,kBAAkB;IACrC,gBAAgB,EAAE,iBAAiB;IACnC,qBAAqB,EAAE,sBAAsB;IAC7C,SAAS,EAAE,UAAU;CACtB,CAAC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Anthropic SDK monkey-patch
|
|
2
|
+
* Anthropic SDK monkey-patch, auto-emit llm_call events for every
|
|
3
3
|
* messages.create() call inside a wrap()'d execution.
|
|
4
4
|
*
|
|
5
5
|
* Activation is opt-in (call instrumentAnthropic() once at startup),
|
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
*
|
|
9
9
|
* What gets captured per call:
|
|
10
10
|
* - model (e.g. "claude-opus-4-6")
|
|
11
|
-
* - system_prompt
|
|
12
|
-
* - user_message
|
|
13
|
-
* - response_text
|
|
14
|
-
* - input_tokens / output_tokens
|
|
15
|
-
* - duration_ms
|
|
16
|
-
* - status
|
|
11
|
+
* - system_prompt, truncated to 1000 chars
|
|
12
|
+
* - user_message, the LAST user-role message, truncated to 1000
|
|
13
|
+
* - response_text, concatenated text-block content, truncated
|
|
14
|
+
* - input_tokens / output_tokens, from response.usage
|
|
15
|
+
* - duration_ms, wall-clock of the API call
|
|
16
|
+
* - status, "ok" / "failed"
|
|
17
17
|
* - exception_type + exception_message on failure
|
|
18
18
|
*
|
|
19
19
|
* Dependency injection: instrumentAnthropic accepts an optional
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
/**
|
|
26
26
|
* Minimal duck-typed interfaces for the parts of the Anthropic SDK
|
|
27
27
|
* shape we depend on. Defined here so the SDK has zero direct
|
|
28
|
-
* dependency on @anthropic-ai/sdk
|
|
28
|
+
* dependency on @anthropic-ai/sdk, at runtime we just call .create()
|
|
29
29
|
* on whatever class was passed.
|
|
30
30
|
*
|
|
31
31
|
* `create` typed as `(...args: any[]) => Promise<any>` rather than a
|
|
@@ -45,7 +45,7 @@ export interface MessagesClassLike {
|
|
|
45
45
|
* llm_call events. Returns true on success or no-op (already
|
|
46
46
|
* patched). Returns false if no class was supplied and the
|
|
47
47
|
* @anthropic-ai/sdk package isn't installed (best-effort dynamic
|
|
48
|
-
* import
|
|
48
|
+
* import, kept optional so the SDK stays dependency-free at install
|
|
49
49
|
* time).
|
|
50
50
|
*/
|
|
51
51
|
export declare function instrumentAnthropic(messagesClass?: MessagesClassLike): Promise<boolean>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic_integration.d.ts","sourceRoot":"","sources":["../src/anthropic_integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;
|
|
1
|
+
{"version":3,"file":"anthropic_integration.d.ts","sourceRoot":"","sources":["../src/anthropic_integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AA6BH;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,iBAAiB;IAEhC,SAAS,EAAE;QAAE,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;KAAE,CAAC;CACzD;AAkBD;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CACvC,aAAa,CAAC,EAAE,iBAAiB,GAChC,OAAO,CAAC,OAAO,CAAC,CAmKlB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Anthropic SDK monkey-patch
|
|
2
|
+
* Anthropic SDK monkey-patch, auto-emit llm_call events for every
|
|
3
3
|
* messages.create() call inside a wrap()'d execution.
|
|
4
4
|
*
|
|
5
5
|
* Activation is opt-in (call instrumentAnthropic() once at startup),
|
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
*
|
|
9
9
|
* What gets captured per call:
|
|
10
10
|
* - model (e.g. "claude-opus-4-6")
|
|
11
|
-
* - system_prompt
|
|
12
|
-
* - user_message
|
|
13
|
-
* - response_text
|
|
14
|
-
* - input_tokens / output_tokens
|
|
15
|
-
* - duration_ms
|
|
16
|
-
* - status
|
|
11
|
+
* - system_prompt, truncated to 1000 chars
|
|
12
|
+
* - user_message, the LAST user-role message, truncated to 1000
|
|
13
|
+
* - response_text, concatenated text-block content, truncated
|
|
14
|
+
* - input_tokens / output_tokens, from response.usage
|
|
15
|
+
* - duration_ms, wall-clock of the API call
|
|
16
|
+
* - status, "ok" / "failed"
|
|
17
17
|
* - exception_type + exception_message on failure
|
|
18
18
|
*
|
|
19
19
|
* Dependency injection: instrumentAnthropic accepts an optional
|
|
@@ -24,12 +24,20 @@
|
|
|
24
24
|
*/
|
|
25
25
|
import { getClient } from "./client.js";
|
|
26
26
|
import { currentExecutionContext, newEventId } from "./context.js";
|
|
27
|
+
import { classifyAnthropicException, extractHttpStatus, extractRetryAfter, } from "./errors.js";
|
|
27
28
|
import { EventType, utcNowRfc3339 } from "./events.js";
|
|
29
|
+
import { _maybeEmitThrottlingEvent } from "./observe.js";
|
|
28
30
|
const MAX_SYSTEM = 1000;
|
|
29
31
|
const MAX_USER_MSG = 1000;
|
|
30
32
|
const MAX_RESPONSE = 1000;
|
|
31
33
|
const MAX_EXC_MSG = 500;
|
|
32
|
-
/**
|
|
34
|
+
/** Stable lowercase provider identifier shipped on every llm_call
|
|
35
|
+
* event from this integration. Backend detectors cluster cross-
|
|
36
|
+
* tenant signals on (provider, error_class), so this string must
|
|
37
|
+
* NOT change between SDK versions without a coordinated backend
|
|
38
|
+
* change. */
|
|
39
|
+
const PROVIDER = "anthropic";
|
|
40
|
+
/** Already-patched classes, keyed by class identity to make
|
|
33
41
|
* instrumentAnthropic() idempotent and to allow distinct fake classes
|
|
34
42
|
* to be patched in tests without falsely tripping the check. */
|
|
35
43
|
const _patched = new WeakSet();
|
|
@@ -38,7 +46,7 @@ const _patched = new WeakSet();
|
|
|
38
46
|
* llm_call events. Returns true on success or no-op (already
|
|
39
47
|
* patched). Returns false if no class was supplied and the
|
|
40
48
|
* @anthropic-ai/sdk package isn't installed (best-effort dynamic
|
|
41
|
-
* import
|
|
49
|
+
* import, kept optional so the SDK stays dependency-free at install
|
|
42
50
|
* time).
|
|
43
51
|
*/
|
|
44
52
|
export async function instrumentAnthropic(messagesClass) {
|
|
@@ -50,7 +58,7 @@ export async function instrumentAnthropic(messagesClass) {
|
|
|
50
58
|
// installed, we auto-locate Messages; otherwise instrumentation
|
|
51
59
|
// is a no-op.
|
|
52
60
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
53
|
-
// @ts-ignore
|
|
61
|
+
// @ts-ignore, the package may not be installed; this is by design
|
|
54
62
|
const mod = (await import("@anthropic-ai/sdk"));
|
|
55
63
|
cls = mod?.Anthropic?.Messages;
|
|
56
64
|
}
|
|
@@ -60,7 +68,7 @@ export async function instrumentAnthropic(messagesClass) {
|
|
|
60
68
|
return false;
|
|
61
69
|
}
|
|
62
70
|
if (!cls) {
|
|
63
|
-
console.warn("mesedi: located @anthropic-ai/sdk but couldn't find Anthropic.Messages
|
|
71
|
+
console.warn("mesedi: located @anthropic-ai/sdk but couldn't find Anthropic.Messages, SDK version mismatch?");
|
|
64
72
|
return false;
|
|
65
73
|
}
|
|
66
74
|
}
|
|
@@ -70,14 +78,14 @@ export async function instrumentAnthropic(messagesClass) {
|
|
|
70
78
|
cls.prototype.create = async function patchedCreate(...args) {
|
|
71
79
|
const ctx = currentExecutionContext();
|
|
72
80
|
if (!ctx) {
|
|
73
|
-
// Outside wrap()
|
|
81
|
+
// Outside wrap(), pass through unobserved.
|
|
74
82
|
return originalCreate.apply(this, args);
|
|
75
83
|
}
|
|
76
84
|
// Halt-safe boundary: check the budget BEFORE the actual LLM
|
|
77
85
|
// call. If a budget exists and is exceeded, this throws
|
|
78
86
|
// MesediHalt which propagates up to wrap()'s catch block. We
|
|
79
87
|
// count this as a step now (after the check passes) so the
|
|
80
|
-
// counter advances even though the LLM call hasn't run yet
|
|
88
|
+
// counter advances even though the LLM call hasn't run yet ,
|
|
81
89
|
// matches the Python SDK's pattern (check, then count, then act).
|
|
82
90
|
ctx.checkBudget();
|
|
83
91
|
if (ctx.budgetTracker) {
|
|
@@ -101,7 +109,7 @@ export async function instrumentAnthropic(messagesClass) {
|
|
|
101
109
|
// halt-safe boundary checks know how many tokens this execution
|
|
102
110
|
// has consumed. Tokens from FAILED LLM calls don't get
|
|
103
111
|
// accounted (we don't reach this code path on the error
|
|
104
|
-
// branch)
|
|
112
|
+
// branch), that matches the Python SDK behavior.
|
|
105
113
|
if (ctx.budgetTracker) {
|
|
106
114
|
ctx.budgetTracker.addTokens(inputTokens, outputTokens);
|
|
107
115
|
}
|
|
@@ -113,6 +121,8 @@ export async function instrumentAnthropic(messagesClass) {
|
|
|
113
121
|
timestamp: utcNowRfc3339(),
|
|
114
122
|
duration_ms: durationMs,
|
|
115
123
|
payload: {
|
|
124
|
+
provider: PROVIDER,
|
|
125
|
+
surface: "chat",
|
|
116
126
|
model,
|
|
117
127
|
system_prompt: truncate(systemText, MAX_SYSTEM),
|
|
118
128
|
user_message: truncate(userMessage, MAX_USER_MSG),
|
|
@@ -127,6 +137,36 @@ export async function instrumentAnthropic(messagesClass) {
|
|
|
127
137
|
}
|
|
128
138
|
catch (err) {
|
|
129
139
|
const durationMs = Math.round(performance.now() - start);
|
|
140
|
+
// Build the failure payload with the canonical cross-
|
|
141
|
+
// provider vocabulary from ./errors. provider lets the
|
|
142
|
+
// backend group multi-provider signals; error_class maps
|
|
143
|
+
// the native exception to one of eight canonical buckets;
|
|
144
|
+
// http_status is included when the exception exposes it.
|
|
145
|
+
const failurePayload = {
|
|
146
|
+
provider: PROVIDER,
|
|
147
|
+
surface: "chat",
|
|
148
|
+
model,
|
|
149
|
+
system_prompt: truncate(systemText, MAX_SYSTEM),
|
|
150
|
+
user_message: truncate(userMessage, MAX_USER_MSG),
|
|
151
|
+
status: "failed",
|
|
152
|
+
error_class: classifyAnthropicException(err),
|
|
153
|
+
exception_type: err instanceof Error && err.constructor.name
|
|
154
|
+
? err.constructor.name
|
|
155
|
+
: typeof err,
|
|
156
|
+
exception_message: truncate(err instanceof Error ? err.message : String(err), MAX_EXC_MSG),
|
|
157
|
+
};
|
|
158
|
+
const httpStatus = extractHttpStatus(err);
|
|
159
|
+
if (httpStatus !== undefined) {
|
|
160
|
+
failurePayload.http_status = httpStatus;
|
|
161
|
+
}
|
|
162
|
+
// Provider-recommended back-off window. When present, the
|
|
163
|
+
// backend surfaces it on the provider_incident failure group
|
|
164
|
+
// so the customer's dashboard shows "back off N seconds"
|
|
165
|
+
// alongside the incident itself.
|
|
166
|
+
const retryAfter = extractRetryAfter(err);
|
|
167
|
+
if (retryAfter !== undefined) {
|
|
168
|
+
failurePayload.retry_after_seconds = retryAfter;
|
|
169
|
+
}
|
|
130
170
|
const event = {
|
|
131
171
|
event_id: eventId,
|
|
132
172
|
execution_id: ctx.executionId,
|
|
@@ -134,18 +174,19 @@ export async function instrumentAnthropic(messagesClass) {
|
|
|
134
174
|
sequence,
|
|
135
175
|
timestamp: utcNowRfc3339(),
|
|
136
176
|
duration_ms: durationMs,
|
|
137
|
-
payload:
|
|
138
|
-
model,
|
|
139
|
-
system_prompt: truncate(systemText, MAX_SYSTEM),
|
|
140
|
-
user_message: truncate(userMessage, MAX_USER_MSG),
|
|
141
|
-
status: "failed",
|
|
142
|
-
exception_type: err instanceof Error && err.constructor.name
|
|
143
|
-
? err.constructor.name
|
|
144
|
-
: typeof err,
|
|
145
|
-
exception_message: truncate(err instanceof Error ? err.message : String(err), MAX_EXC_MSG),
|
|
146
|
-
},
|
|
177
|
+
payload: failurePayload,
|
|
147
178
|
};
|
|
148
179
|
client.submitEvent(event);
|
|
180
|
+
// Wave 1.4: auto-emit infrastructure_event on throttling-class
|
|
181
|
+
// exceptions so the infrastructure_throttled detector isn't
|
|
182
|
+
// silently inactive for the default customer.
|
|
183
|
+
_maybeEmitThrottlingEvent({
|
|
184
|
+
provider: PROVIDER,
|
|
185
|
+
errorClass: failurePayload.error_class,
|
|
186
|
+
httpStatus,
|
|
187
|
+
retryAfterSeconds: retryAfter,
|
|
188
|
+
endpoint: "/v1/messages",
|
|
189
|
+
});
|
|
149
190
|
throw err;
|
|
150
191
|
}
|
|
151
192
|
};
|
|
@@ -191,7 +232,7 @@ function extractResponseFields(response) {
|
|
|
191
232
|
}
|
|
192
233
|
}
|
|
193
234
|
catch {
|
|
194
|
-
// best effort
|
|
235
|
+
// best effort, leave responseText empty
|
|
195
236
|
}
|
|
196
237
|
try {
|
|
197
238
|
if (response.usage) {
|
|
@@ -200,7 +241,7 @@ function extractResponseFields(response) {
|
|
|
200
241
|
}
|
|
201
242
|
}
|
|
202
243
|
catch {
|
|
203
|
-
// best effort
|
|
244
|
+
// best effort, leave token counts at 0
|
|
204
245
|
}
|
|
205
246
|
return { responseText, inputTokens, outputTokens };
|
|
206
247
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic_integration.js","sourceRoot":"","sources":["../src/anthropic_integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,EAAS,SAAS,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"anthropic_integration.js","sourceRoot":"","sources":["../src/anthropic_integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,EACL,0BAA0B,EAC1B,iBAAiB,EACjB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAS,SAAS,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AA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pC,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC;IACnB,CAAC;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,CAAS,EAAE,GAAW;IACtC,IAAI,CAAC,CAAC,MAAM,IAAI,GAAG;QAAE,OAAO,CAAC,CAAC;IAC9B,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;AACrC,CAAC"}
|
package/dist/client.d.ts
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* MesediClient
|
|
2
|
+
* MesediClient, HTTP client + async event shipper for Node.
|
|
3
3
|
*
|
|
4
|
-
* Zero runtime dependencies: uses Node 18+ native `fetch` for HTTP
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* Zero runtime dependencies: uses Node 18+ native `fetch` for HTTP,
|
|
5
|
+
* `AsyncLocalStorage` for async context (in wrap()/tool()), and
|
|
6
|
+
* `node:zlib` for opt-in gzip compression of request bodies above 1
|
|
7
|
+
* KB. Below the threshold requests ship uncompressed exactly as
|
|
8
|
+
* v0.2.0 did. No background threads (Node doesn't have them in the
|
|
9
|
+
* Python sense); instead a `setInterval` timer drains the queue
|
|
10
|
+
* periodically, and a `process.on("exit")` handler flushes any
|
|
11
|
+
* remaining items at shutdown.
|
|
12
|
+
*
|
|
13
|
+
* Why gzip (and why the Python SDK uses zstd): JavaScript zstd
|
|
14
|
+
* encoders are heavy (WASM, ~750 KB, async init) and would break
|
|
15
|
+
* this SDK's tiny-install posture. Node ships gzip synchronously in
|
|
16
|
+
* its standard library, so gzip lets us compress with no new
|
|
17
|
+
* dependency and zero install-size cost. The backend accepts both
|
|
18
|
+
* encodings through the same middleware path.
|
|
9
19
|
*
|
|
10
20
|
* Auth: bearer token (`mesedi_sk_...`). Wire-format version pinned
|
|
11
21
|
* at "1" today; future breaking changes bump SCHEMA_VERSION and the
|
|
12
22
|
* backend tightens enforcement.
|
|
13
23
|
*
|
|
14
24
|
* Fail-open: every observation HTTP call wraps its promise in a
|
|
15
|
-
* try/catch
|
|
25
|
+
* try/catch, backend errors are logged via console.warn (configurable
|
|
16
26
|
* via a logger injection in a future polish slice) but NEVER bubble
|
|
17
|
-
* back to the wrapped agent code.
|
|
27
|
+
* back to the wrapped agent code. Compression also fails open: if
|
|
28
|
+
* the encoder throws, the body ships uncompressed instead.
|
|
18
29
|
*/
|
|
19
30
|
import { Event, Execution } from "./events.js";
|
|
20
|
-
export declare const DEFAULT_BASE_URL = "
|
|
31
|
+
export declare const DEFAULT_BASE_URL = "https://api.mesedi.ai";
|
|
21
32
|
export declare const DEFAULT_TIMEOUT_MS = 10000;
|
|
22
33
|
export declare const SCHEMA_VERSION = "1";
|
|
23
34
|
/** Configuration accepted by `configure()`. */
|
|
@@ -29,7 +40,7 @@ export interface ConfigureOptions {
|
|
|
29
40
|
apiKey?: string;
|
|
30
41
|
/**
|
|
31
42
|
* Backend URL. Defaults to `MESEDI_BASE_URL` env var, then to
|
|
32
|
-
* "
|
|
43
|
+
* "https://api.mesedi.ai" (production). Override to localhost for local dev.
|
|
33
44
|
*/
|
|
34
45
|
baseUrl?: string;
|
|
35
46
|
/** Per-request timeout in milliseconds. Default 10s. */
|
|
@@ -40,6 +51,14 @@ export interface ConfigureOptions {
|
|
|
40
51
|
batchSize?: number;
|
|
41
52
|
/** Max queue depth before drop-on-full kicks in. Default 10_000. */
|
|
42
53
|
maxQueue?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Per-event payload cap in bytes (#243). Events whose serialized
|
|
56
|
+
* payload exceeds this size have their longest string fields
|
|
57
|
+
* smart-truncated to fit, with marker fields stamped so downstream
|
|
58
|
+
* readers know what happened. Default 32 KB; pass 0 (or negative)
|
|
59
|
+
* to disable truncation entirely.
|
|
60
|
+
*/
|
|
61
|
+
maxPayloadBytes?: number;
|
|
43
62
|
}
|
|
44
63
|
export declare class MesediClient {
|
|
45
64
|
readonly apiKey: string;
|
|
@@ -48,6 +67,7 @@ export declare class MesediClient {
|
|
|
48
67
|
private readonly flushIntervalMs;
|
|
49
68
|
private readonly batchSize;
|
|
50
69
|
private readonly maxQueue;
|
|
70
|
+
private readonly maxPayloadBytes;
|
|
51
71
|
private queue;
|
|
52
72
|
private pendingEvents;
|
|
53
73
|
private lastFlush;
|
|
@@ -72,7 +92,7 @@ export declare class MesediClient {
|
|
|
72
92
|
shutdown(timeoutMs?: number): Promise<void>;
|
|
73
93
|
private enqueue;
|
|
74
94
|
/**
|
|
75
|
-
* Single tick of the shipper loop
|
|
95
|
+
* Single tick of the shipper loop, called by setInterval. Drains
|
|
76
96
|
* the queue, dispatches non-event items, and flushes the pending-
|
|
77
97
|
* events batch if it's full OR the flush interval has elapsed.
|
|
78
98
|
*/
|
|
@@ -83,7 +103,7 @@ export declare class MesediClient {
|
|
|
83
103
|
private sendUpdateExecution;
|
|
84
104
|
/**
|
|
85
105
|
* One HTTP call with up to 3 retries on transient failure
|
|
86
|
-
* (network errors, 5xx). 4xx responses drop with a warning
|
|
106
|
+
* (network errors, 5xx). 4xx responses drop with a warning, the
|
|
87
107
|
* request is malformed and retrying won't help.
|
|
88
108
|
*/
|
|
89
109
|
private sendWithRetry;
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAIH,OAAO,EACL,KAAK,EACL,SAAS,EAIV,MAAM,aAAa,CAAC;AAErB,eAAO,MAAM,gBAAgB,0BAA0B,CAAC;AACxD,eAAO,MAAM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