mesedi 0.6.0 → 0.7.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/README.md +87 -7
- package/dist/anthropic_integration.js +1 -1
- package/dist/anthropic_integration.js.map +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/client.js +1 -1
- package/dist/client.js.map +1 -1
- package/dist/cohere_integration.js +2 -2
- package/dist/cohere_integration.js.map +1 -1
- package/dist/errors.d.ts +24 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +62 -1
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +1 -1
- package/dist/events.js +10 -10
- package/dist/events.js.map +1 -1
- package/dist/gemini_integration.d.ts +1 -1
- package/dist/gemini_integration.js +2 -2
- package/dist/gemini_integration.js.map +1 -1
- package/dist/halt_stream.js +1 -1
- package/dist/halt_stream.js.map +1 -1
- package/dist/integrations/_integration_helpers.d.ts +98 -0
- package/dist/integrations/_integration_helpers.d.ts.map +1 -0
- package/dist/integrations/_integration_helpers.js +149 -0
- package/dist/integrations/_integration_helpers.js.map +1 -0
- package/dist/integrations/langchain.d.ts +95 -0
- package/dist/integrations/langchain.d.ts.map +1 -0
- package/dist/integrations/langchain.js +775 -0
- package/dist/integrations/langchain.js.map +1 -0
- package/dist/integrations/langgraph.d.ts +4 -4
- package/dist/integrations/langgraph.js +4 -4
- package/dist/integrations/mastra.d.ts +10 -4
- package/dist/integrations/mastra.d.ts.map +1 -1
- package/dist/integrations/mastra.js +237 -23
- package/dist/integrations/mastra.js.map +1 -1
- package/dist/integrations/openai_agents.d.ts +3 -3
- package/dist/integrations/openai_agents.js +3 -3
- package/dist/integrations/promptfoo.d.ts +1 -1
- package/dist/integrations/promptfoo.js +1 -1
- package/dist/integrations/ragas.d.ts +1 -1
- package/dist/integrations/ragas.js +1 -1
- package/dist/integrations/vectara.d.ts +1 -1
- package/dist/integrations/vectara.js +1 -1
- package/dist/observe.d.ts +14 -14
- package/dist/observe.js +7 -7
- package/dist/ollama_integration.d.ts +2 -2
- package/dist/ollama_integration.js +6 -6
- package/dist/ollama_integration.js.map +1 -1
- package/dist/openai_integration.d.ts +1 -1
- package/dist/openai_integration.js +4 -4
- package/dist/openai_integration.js.map +1 -1
- package/dist/tool.d.ts +34 -0
- package/dist/tool.d.ts.map +1 -1
- package/dist/tool.js +46 -4
- package/dist/tool.js.map +1 -1
- package/dist/truncate.d.ts +1 -1
- package/dist/truncate.js +1 -1
- package/dist/wrap.d.ts +2 -2
- package/dist/wrap.js +1 -1
- package/package.json +28 -7
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain.js integration for Mesedi (.C).
|
|
3
|
+
*
|
|
4
|
+
* Ships as a LangChain callback handler that emits Mesedi telemetry.
|
|
5
|
+
* The customer wraps their entry function with `mesedi.wrap()` (which
|
|
6
|
+
* owns the execution boundary) and attaches an instance of this
|
|
7
|
+
* handler to LangChain's `callbacks:` slot (which owns intra-execution
|
|
8
|
+
* events).
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
*
|
|
12
|
+
* import { wrap } from "mesedi";
|
|
13
|
+
* import { MesediLangChainCallbackHandler } from "mesedi/integrations/langchain";
|
|
14
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
15
|
+
*
|
|
16
|
+
* export const runAgent = wrap(
|
|
17
|
+
* { agentName: "support-triage" },
|
|
18
|
+
* async (question: string) => {
|
|
19
|
+
* const llm = new ChatOpenAI({ model: "gpt-4o" });
|
|
20
|
+
* const result = await llm.invoke(
|
|
21
|
+
* [{ role: "user", content: question }],
|
|
22
|
+
* { callbacks: [new MesediLangChainCallbackHandler()] },
|
|
23
|
+
* );
|
|
24
|
+
* return String(result.content);
|
|
25
|
+
* },
|
|
26
|
+
* );
|
|
27
|
+
*
|
|
28
|
+
* Design.
|
|
29
|
+
*
|
|
30
|
+
* `mesedi.wrap()` manages the execution boundary (started / completed /
|
|
31
|
+
* crash signature). The callback handler emits the intra-execution
|
|
32
|
+
* events — `llm_call` and `tool_call` — that Mesedi's detectors
|
|
33
|
+
* consume. Splitting responsibility this way lets the customer adopt
|
|
34
|
+
* Mesedi without reshaping their LangChain code: `wrap` goes around
|
|
35
|
+
* the entry point, the handler gets attached at the existing
|
|
36
|
+
* `callbacks:` slot LangChain already accepts.
|
|
37
|
+
*
|
|
38
|
+
* Mirrors the Python `mesedi.integrations.langchain.MesediCallbackHandler`
|
|
39
|
+
* surface. Wire format is byte-identical so backend detectors see one
|
|
40
|
+
* unified event stream whether the customer writes Python or TypeScript.
|
|
41
|
+
*
|
|
42
|
+
* Fail-open posture matches the rest of the SDK: any exception inside
|
|
43
|
+
* a handler method is swallowed and logged via `console.warn`. Never
|
|
44
|
+
* blocks or breaks the customer's LangChain pipeline.
|
|
45
|
+
*
|
|
46
|
+
* Out of scope for v1:
|
|
47
|
+
* - Streaming responses (`handleLLMNewToken`). Receivers see only
|
|
48
|
+
* the final assembled response. Streaming attribution is a v2
|
|
49
|
+
* concern that needs an event-payload schema change.
|
|
50
|
+
* - Per-chain depth tracking. Every chain start fires but we ignore
|
|
51
|
+
* it because `wrap` already owns the execution boundary. A
|
|
52
|
+
* chain-as-execution mode (no `wrap` required) is a later
|
|
53
|
+
* iteration.
|
|
54
|
+
* - Multi-modal content blocks (images in messages). We extract the
|
|
55
|
+
* text parts and ignore the rest.
|
|
56
|
+
*/
|
|
57
|
+
import { getClient } from "../client.js";
|
|
58
|
+
import { currentExecutionContext, newEventId } from "../context.js";
|
|
59
|
+
import { classifyByProvider, extractHttpStatus, extractRetryAfter, } from "../errors.js";
|
|
60
|
+
import { EventType, utcNowRfc3339 } from "../events.js";
|
|
61
|
+
import { structuredReturnValue } from "../tool.js";
|
|
62
|
+
// ── Peer-dep imports ─────────────────────────────────────────────────
|
|
63
|
+
//
|
|
64
|
+
// `@langchain/core` is declared as an OPTIONAL peer dependency in
|
|
65
|
+
// package.json. Customers who never import this module never pay the
|
|
66
|
+
// install cost. `tsc` resolves `BaseCallbackHandler` against the
|
|
67
|
+
// devDependency version at build time; at runtime the customer's
|
|
68
|
+
// installed `@langchain/core` provides the actual class.
|
|
69
|
+
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
70
|
+
// Truncation budgets. Kept in sync with `vercel_ai.ts` / `mastra.ts` /
|
|
71
|
+
// the Python `emit_llm_call` so wire-format payloads from this
|
|
72
|
+
// adapter and from hand-written code are byte-indistinguishable.
|
|
73
|
+
const MAX_SYSTEM = 1000;
|
|
74
|
+
// user_message cap raised from 1000 to 8192 so backend
|
|
75
|
+
// detectors that score on the raw user_message content
|
|
76
|
+
// (token_waste requires a 2048+ char shared prefix, drift lexical
|
|
77
|
+
// uses the full text for similarity scoring) actually see the
|
|
78
|
+
// customer's prompt instead of a defensive truncation that defeated
|
|
79
|
+
// them. Wire payload size is bounded elsewhere via payload
|
|
80
|
+
// truncation on the outer envelope.
|
|
81
|
+
const MAX_USER_MSG = 8192;
|
|
82
|
+
const MAX_RESPONSE = 1000;
|
|
83
|
+
const MAX_TOOL_INPUT_REPR = 200;
|
|
84
|
+
const MAX_TOOL_OUTPUT_REPR = 500;
|
|
85
|
+
const MAX_EXC_MSG = 500;
|
|
86
|
+
/**
|
|
87
|
+
* LangChain callback handler that emits Mesedi events. Attach an
|
|
88
|
+
* instance to any LangChain runnable via the standard `callbacks:`
|
|
89
|
+
* config slot:
|
|
90
|
+
*
|
|
91
|
+
* runnable.invoke(input, { callbacks: [new MesediLangChainCallbackHandler()] });
|
|
92
|
+
*
|
|
93
|
+
* Emits one `llm_call` event per LLM invocation (matching the wire
|
|
94
|
+
* format from `emit_llm_call` and the Anthropic patch) and one
|
|
95
|
+
* `tool_call` event per tool invocation (matching the wire format
|
|
96
|
+
* from `@mesedi.tool`). Both event types feed the standard Mesedi
|
|
97
|
+
* detector chain — drift, identical / similar-call loops,
|
|
98
|
+
* tool-failures, cost-velocity, prompt-injection.
|
|
99
|
+
*
|
|
100
|
+
* Outside a `wrap()` execution context, all emissions silently no-op
|
|
101
|
+
* (matches the rest of Mesedi's observe layer).
|
|
102
|
+
*/
|
|
103
|
+
export class MesediLangChainCallbackHandler extends BaseCallbackHandler {
|
|
104
|
+
name = "mesedi-langchain";
|
|
105
|
+
/**
|
|
106
|
+
* Tell LangChain's dispatcher not to reraise handler exceptions.
|
|
107
|
+
* We already swallow inside each method; this is belt-and-suspenders
|
|
108
|
+
* for any future LangChain version that changes the default.
|
|
109
|
+
*/
|
|
110
|
+
raiseError = false;
|
|
111
|
+
llmStarts = new Map();
|
|
112
|
+
toolStarts = new Map();
|
|
113
|
+
// ── LLM events ──────────────────────────────────────────────────
|
|
114
|
+
handleLLMStart(llm, prompts, runId, _parentRunId, extraParams, _tags, _metadata, _runName) {
|
|
115
|
+
try {
|
|
116
|
+
const serialized = llm;
|
|
117
|
+
const model = extractModel(serialized, extraParams);
|
|
118
|
+
const provider = extractProvider(serialized);
|
|
119
|
+
const userMessage = prompts.length > 0 ? (prompts[prompts.length - 1] ?? "") : "";
|
|
120
|
+
this.llmStarts.set(runId, {
|
|
121
|
+
model,
|
|
122
|
+
provider,
|
|
123
|
+
userMessage,
|
|
124
|
+
systemPrompt: "",
|
|
125
|
+
startedAt: Date.now(),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
console.warn("mesedi langchain handler: handleLLMStart error", err);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
handleChatModelStart(llm, messages, runId, _parentRunId, extraParams, _tags, _metadata, _runName) {
|
|
133
|
+
try {
|
|
134
|
+
const serialized = llm;
|
|
135
|
+
const model = extractModel(serialized, extraParams);
|
|
136
|
+
const provider = extractProvider(serialized);
|
|
137
|
+
const lastConversation = messages.length > 0 ? (messages[messages.length - 1] ?? []) : [];
|
|
138
|
+
const { userMessage, systemPrompt } = extractRoleMessages(lastConversation);
|
|
139
|
+
this.llmStarts.set(runId, {
|
|
140
|
+
model,
|
|
141
|
+
provider,
|
|
142
|
+
userMessage,
|
|
143
|
+
systemPrompt,
|
|
144
|
+
startedAt: Date.now(),
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
console.warn("mesedi langchain handler: handleChatModelStart error", err);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
handleLLMEnd(output, runId, _parentRunId, _tags) {
|
|
152
|
+
try {
|
|
153
|
+
const ctx = this.llmStarts.get(runId);
|
|
154
|
+
if (!ctx)
|
|
155
|
+
return;
|
|
156
|
+
this.llmStarts.delete(runId);
|
|
157
|
+
const responseText = extractResponseText(output);
|
|
158
|
+
const [inputTokens, outputTokens] = extractTokenUsage(output);
|
|
159
|
+
const durationMs = Math.max(0, Date.now() - ctx.startedAt);
|
|
160
|
+
emitLlmCallEvent({
|
|
161
|
+
model: ctx.model,
|
|
162
|
+
provider: ctx.provider,
|
|
163
|
+
userMessage: ctx.userMessage,
|
|
164
|
+
systemPrompt: ctx.systemPrompt,
|
|
165
|
+
responseText,
|
|
166
|
+
inputTokens,
|
|
167
|
+
outputTokens,
|
|
168
|
+
durationMs,
|
|
169
|
+
status: "ok",
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
console.warn("mesedi langchain handler: handleLLMEnd error", err);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
handleLLMError(err, runId, _parentRunId, _tags) {
|
|
177
|
+
try {
|
|
178
|
+
const ctx = this.llmStarts.get(runId);
|
|
179
|
+
if (!ctx)
|
|
180
|
+
return;
|
|
181
|
+
this.llmStarts.delete(runId);
|
|
182
|
+
const durationMs = Math.max(0, Date.now() - ctx.startedAt);
|
|
183
|
+
const rawExc = unwrapProviderException(err);
|
|
184
|
+
const [excType, excMessage] = extractError(rawExc);
|
|
185
|
+
const errorClass = classifyByProvider(ctx.provider, rawExc);
|
|
186
|
+
const httpStatus = extractHttpStatus(rawExc);
|
|
187
|
+
const retryAfter = extractRetryAfter(rawExc);
|
|
188
|
+
emitLlmCallEvent({
|
|
189
|
+
model: ctx.model,
|
|
190
|
+
provider: ctx.provider,
|
|
191
|
+
userMessage: ctx.userMessage,
|
|
192
|
+
systemPrompt: ctx.systemPrompt,
|
|
193
|
+
responseText: "",
|
|
194
|
+
inputTokens: 0,
|
|
195
|
+
outputTokens: 0,
|
|
196
|
+
durationMs,
|
|
197
|
+
status: "failed",
|
|
198
|
+
errorClass,
|
|
199
|
+
exceptionType: excType,
|
|
200
|
+
exceptionMessage: excMessage,
|
|
201
|
+
httpStatus,
|
|
202
|
+
retryAfter,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
catch (e) {
|
|
206
|
+
console.warn("mesedi langchain handler: handleLLMError error", e);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// ── Tool events ─────────────────────────────────────────────────
|
|
210
|
+
handleToolStart(tool, input, runId, _parentRunId, _tags, _metadata, _name) {
|
|
211
|
+
try {
|
|
212
|
+
const name = extractToolName(tool);
|
|
213
|
+
const inputStr = typeof input === "string" ? input : reprValue(input);
|
|
214
|
+
// LangChain always hands us `input` as a string. Try to parse
|
|
215
|
+
// it as JSON so `arguments` on the emitted event carries a
|
|
216
|
+
// structured shape when the tool takes a JSON payload (the
|
|
217
|
+
// common LangChain tool pattern). Falls through to the raw
|
|
218
|
+
// string when parsing fails.
|
|
219
|
+
let rawInput = input;
|
|
220
|
+
if (typeof input === "string") {
|
|
221
|
+
rawInput = tryParseJson(input);
|
|
222
|
+
}
|
|
223
|
+
this.toolStarts.set(runId, {
|
|
224
|
+
name,
|
|
225
|
+
inputStr,
|
|
226
|
+
rawInput,
|
|
227
|
+
startedAt: Date.now(),
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
console.warn("mesedi langchain handler: handleToolStart error", err);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
handleToolEnd(output, runId, _parentRunId, _tags) {
|
|
235
|
+
try {
|
|
236
|
+
const ctx = this.toolStarts.get(runId);
|
|
237
|
+
if (!ctx)
|
|
238
|
+
return;
|
|
239
|
+
this.toolStarts.delete(runId);
|
|
240
|
+
const durationMs = Math.max(0, Date.now() - ctx.startedAt);
|
|
241
|
+
const resultSummary = output == null
|
|
242
|
+
? ""
|
|
243
|
+
: typeof output === "string"
|
|
244
|
+
? output
|
|
245
|
+
: reprValue(output);
|
|
246
|
+
// LangChain tools frequently return a string that happens to be
|
|
247
|
+
// JSON (e.g. from a StructuredTool). Try parsing so the
|
|
248
|
+
// tool_schema_drift detector sees the underlying shape rather
|
|
249
|
+
// than a truncated string it can't fingerprint.
|
|
250
|
+
let rawOutput = output;
|
|
251
|
+
if (typeof output === "string") {
|
|
252
|
+
rawOutput = tryParseJson(output);
|
|
253
|
+
}
|
|
254
|
+
emitToolCallEvent({
|
|
255
|
+
toolName: ctx.name,
|
|
256
|
+
inputStr: ctx.inputStr,
|
|
257
|
+
rawInput: ctx.rawInput,
|
|
258
|
+
resultSummary,
|
|
259
|
+
rawOutput,
|
|
260
|
+
durationMs,
|
|
261
|
+
status: "ok",
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
console.warn("mesedi langchain handler: handleToolEnd error", err);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
handleToolError(err, runId, _parentRunId, _tags) {
|
|
269
|
+
try {
|
|
270
|
+
const ctx = this.toolStarts.get(runId);
|
|
271
|
+
if (!ctx)
|
|
272
|
+
return;
|
|
273
|
+
this.toolStarts.delete(runId);
|
|
274
|
+
const durationMs = Math.max(0, Date.now() - ctx.startedAt);
|
|
275
|
+
const [excType, excMessage] = extractError(err);
|
|
276
|
+
emitToolCallEvent({
|
|
277
|
+
toolName: ctx.name,
|
|
278
|
+
inputStr: ctx.inputStr,
|
|
279
|
+
rawInput: ctx.rawInput,
|
|
280
|
+
resultSummary: "",
|
|
281
|
+
rawOutput: undefined,
|
|
282
|
+
durationMs,
|
|
283
|
+
status: "failed",
|
|
284
|
+
exceptionType: excType,
|
|
285
|
+
exceptionMessage: excMessage,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
catch (e) {
|
|
289
|
+
console.warn("mesedi langchain handler: handleToolError error", e);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
// ── Emitters ────────────────────────────────────────────────────────
|
|
294
|
+
function emitLlmCallEvent(args) {
|
|
295
|
+
const ctx = currentExecutionContext();
|
|
296
|
+
if (!ctx)
|
|
297
|
+
return;
|
|
298
|
+
// Halt-safe boundary at LLM call. Matches @mesedi.tool, the
|
|
299
|
+
// Anthropic patch, and vercel_ai's emitLlmCallEvent.
|
|
300
|
+
ctx.checkBudget();
|
|
301
|
+
if (ctx.budgetTracker) {
|
|
302
|
+
ctx.budgetTracker.incrementSteps();
|
|
303
|
+
if (args.inputTokens > 0 || args.outputTokens > 0) {
|
|
304
|
+
ctx.budgetTracker.addTokens(args.inputTokens, args.outputTokens);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const payload = {
|
|
308
|
+
model: args.model,
|
|
309
|
+
// Stable lowercase provider identifier. Always emitted (even on
|
|
310
|
+
// "unknown") so the backend's provider_incident detector can
|
|
311
|
+
// cluster cross-tenant signals on (provider, error_class)
|
|
312
|
+
// regardless of which framework the customer wraps.
|
|
313
|
+
provider: args.provider,
|
|
314
|
+
system_prompt: truncate(args.systemPrompt, MAX_SYSTEM),
|
|
315
|
+
user_message: truncate(args.userMessage, MAX_USER_MSG),
|
|
316
|
+
status: args.status,
|
|
317
|
+
};
|
|
318
|
+
if (args.status === "ok") {
|
|
319
|
+
payload["response_text"] = truncate(args.responseText, MAX_RESPONSE);
|
|
320
|
+
payload["input_tokens"] = args.inputTokens;
|
|
321
|
+
payload["output_tokens"] = args.outputTokens;
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
// Canonical closed-vocabulary error class. Backend groups
|
|
325
|
+
// multi-provider signals on this bucket; free-form strings would
|
|
326
|
+
// defeat cross-provider clustering.
|
|
327
|
+
if (args.errorClass)
|
|
328
|
+
payload["error_class"] = args.errorClass;
|
|
329
|
+
if (args.exceptionType)
|
|
330
|
+
payload["exception_type"] = args.exceptionType;
|
|
331
|
+
if (args.exceptionMessage) {
|
|
332
|
+
payload["exception_message"] = truncate(args.exceptionMessage, MAX_EXC_MSG);
|
|
333
|
+
}
|
|
334
|
+
if (typeof args.httpStatus === "number") {
|
|
335
|
+
payload["http_status"] = args.httpStatus;
|
|
336
|
+
}
|
|
337
|
+
if (typeof args.retryAfter === "number") {
|
|
338
|
+
payload["retry_after"] = args.retryAfter;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const event = {
|
|
342
|
+
event_id: newEventId(),
|
|
343
|
+
execution_id: ctx.executionId,
|
|
344
|
+
event_type: EventType.LLM_CALL,
|
|
345
|
+
sequence: ctx.nextSequence(),
|
|
346
|
+
timestamp: utcNowRfc3339(),
|
|
347
|
+
duration_ms: args.durationMs,
|
|
348
|
+
payload,
|
|
349
|
+
};
|
|
350
|
+
getClient().submitEvent(event);
|
|
351
|
+
}
|
|
352
|
+
function emitToolCallEvent(args) {
|
|
353
|
+
const ctx = currentExecutionContext();
|
|
354
|
+
if (!ctx)
|
|
355
|
+
return;
|
|
356
|
+
// Halt-safe boundary at tool call, matches @mesedi.tool.
|
|
357
|
+
ctx.checkBudget();
|
|
358
|
+
if (ctx.budgetTracker) {
|
|
359
|
+
ctx.budgetTracker.incrementSteps();
|
|
360
|
+
}
|
|
361
|
+
// Assemble `arguments` in the @mesedi.tool wire shape:
|
|
362
|
+
// { args: [...], kwargs: {...} }
|
|
363
|
+
//
|
|
364
|
+
// Prefer a structured (schema-preserving) representation ONLY when
|
|
365
|
+
// the raw input actually parsed into a non-string container
|
|
366
|
+
// (object / array / number / bool). tool_schema_drift fingerprints
|
|
367
|
+
// this shape. For plain strings we keep the truncated string
|
|
368
|
+
// branch — the wire budget MAX_TOOL_INPUT_REPR still applies.
|
|
369
|
+
const rawInputIsStructured = args.rawInput !== null &&
|
|
370
|
+
args.rawInput !== undefined &&
|
|
371
|
+
typeof args.rawInput !== "string";
|
|
372
|
+
const structuredArgs = rawInputIsStructured
|
|
373
|
+
? structuredReturnValue(args.rawInput)
|
|
374
|
+
: undefined;
|
|
375
|
+
const argsSlot = structuredArgs !== undefined
|
|
376
|
+
? structuredArgs
|
|
377
|
+
: truncate(args.inputStr, MAX_TOOL_INPUT_REPR);
|
|
378
|
+
const payload = {
|
|
379
|
+
tool_name: args.toolName,
|
|
380
|
+
arguments: {
|
|
381
|
+
args: [argsSlot],
|
|
382
|
+
kwargs: {},
|
|
383
|
+
},
|
|
384
|
+
status: args.status,
|
|
385
|
+
};
|
|
386
|
+
if (args.status === "ok") {
|
|
387
|
+
payload["result_summary"] = truncate(args.resultSummary, MAX_TOOL_OUTPUT_REPR);
|
|
388
|
+
// Structured JSON-native form for backend detectors. Matches
|
|
389
|
+
// @mesedi.tool's return_value contract: the walker
|
|
390
|
+
// preserves type via typed sentinels, so
|
|
391
|
+
// tool_schema_drift can fingerprint the shape. Absent when the
|
|
392
|
+
// output isn't safely serializable (functions, circular refs).
|
|
393
|
+
const structuredReturn = structuredReturnValue(args.rawOutput);
|
|
394
|
+
if (structuredReturn !== undefined) {
|
|
395
|
+
payload["return_value"] = structuredReturn;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
if (args.exceptionType)
|
|
400
|
+
payload["exception_type"] = args.exceptionType;
|
|
401
|
+
if (args.exceptionMessage) {
|
|
402
|
+
payload["exception_message"] = truncate(args.exceptionMessage, MAX_EXC_MSG);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
const event = {
|
|
406
|
+
event_id: newEventId(),
|
|
407
|
+
execution_id: ctx.executionId,
|
|
408
|
+
event_type: EventType.TOOL_CALL,
|
|
409
|
+
sequence: ctx.nextSequence(),
|
|
410
|
+
timestamp: utcNowRfc3339(),
|
|
411
|
+
duration_ms: args.durationMs,
|
|
412
|
+
payload,
|
|
413
|
+
};
|
|
414
|
+
getClient().submitEvent(event);
|
|
415
|
+
}
|
|
416
|
+
// ── Extractors ──────────────────────────────────────────────────────
|
|
417
|
+
//
|
|
418
|
+
// LangChain's serialized-object shape and message-class hierarchy have
|
|
419
|
+
// shifted across versions. Each extractor below tries the known shapes
|
|
420
|
+
// in order and falls through to a safe default so the adapter still
|
|
421
|
+
// emits a useful event on a version we haven't seen.
|
|
422
|
+
function extractModel(serialized, extraParams) {
|
|
423
|
+
const inv = extraParams?.["invocation_params"] ??
|
|
424
|
+
{};
|
|
425
|
+
for (const key of ["model", "model_name", "deployment_name"]) {
|
|
426
|
+
const v = inv[key];
|
|
427
|
+
if (typeof v === "string" && v)
|
|
428
|
+
return v;
|
|
429
|
+
}
|
|
430
|
+
if (serialized && typeof serialized === "object") {
|
|
431
|
+
const kw = serialized["kwargs"] ?? {};
|
|
432
|
+
for (const key of ["model", "model_name", "deployment_name"]) {
|
|
433
|
+
const v = kw[key];
|
|
434
|
+
if (typeof v === "string" && v)
|
|
435
|
+
return v;
|
|
436
|
+
}
|
|
437
|
+
const ident = serialized["id"];
|
|
438
|
+
if (Array.isArray(ident) && ident.length > 0) {
|
|
439
|
+
const last = ident[ident.length - 1];
|
|
440
|
+
if (typeof last === "string" && last)
|
|
441
|
+
return last;
|
|
442
|
+
}
|
|
443
|
+
const name = serialized["name"];
|
|
444
|
+
if (typeof name === "string" && name)
|
|
445
|
+
return name;
|
|
446
|
+
}
|
|
447
|
+
return "unknown";
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Return a stable lowercase provider identifier for a LangChain
|
|
451
|
+
* serialized LLM object, defaulting to "unknown" when it can't be
|
|
452
|
+
* derived. Matches the shipped `provider` values used by the direct
|
|
453
|
+
* instrumentation modules (anthropic_integration, openai_integration,
|
|
454
|
+
* etc.) so the provider_incident detector clusters cross-tenant
|
|
455
|
+
* signals on ONE bucket regardless of whether the customer wraps
|
|
456
|
+
* their LLM directly or via LangChain.
|
|
457
|
+
*
|
|
458
|
+
* LangChain's `serialized.id` is an array like
|
|
459
|
+
* ["langchain", "chat_models", "anthropic", "ChatAnthropic"]
|
|
460
|
+
* ["@langchain/anthropic", "chat_models", "ChatAnthropic"]
|
|
461
|
+
* ["langchain_anthropic", "chat_models", "ChatAnthropic"]
|
|
462
|
+
* across versions. We walk the array looking for the first known
|
|
463
|
+
* provider substring; if none matches, we probe the tail element's
|
|
464
|
+
* class name for a "Chat<Provider>" prefix.
|
|
465
|
+
*/
|
|
466
|
+
function extractProvider(serialized) {
|
|
467
|
+
if (!serialized || typeof serialized !== "object")
|
|
468
|
+
return "unknown";
|
|
469
|
+
const ident = serialized["id"];
|
|
470
|
+
if (Array.isArray(ident)) {
|
|
471
|
+
for (const segRaw of ident) {
|
|
472
|
+
if (typeof segRaw !== "string")
|
|
473
|
+
continue;
|
|
474
|
+
const seg = segRaw.toLowerCase();
|
|
475
|
+
for (const known of KNOWN_PROVIDERS) {
|
|
476
|
+
if (seg.includes(known))
|
|
477
|
+
return normalizeProvider(known);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
// Fallback: parse class name at the tail
|
|
481
|
+
// (e.g. "ChatAnthropic" -> "anthropic").
|
|
482
|
+
const tail = ident[ident.length - 1];
|
|
483
|
+
if (typeof tail === "string") {
|
|
484
|
+
const cls = tail.toLowerCase();
|
|
485
|
+
for (const known of KNOWN_PROVIDERS) {
|
|
486
|
+
if (cls.includes(known))
|
|
487
|
+
return normalizeProvider(known);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
// Last-resort probe on serialized.name.
|
|
492
|
+
const name = serialized["name"];
|
|
493
|
+
if (typeof name === "string") {
|
|
494
|
+
const cls = name.toLowerCase();
|
|
495
|
+
for (const known of KNOWN_PROVIDERS) {
|
|
496
|
+
if (cls.includes(known))
|
|
497
|
+
return normalizeProvider(known);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return "unknown";
|
|
501
|
+
}
|
|
502
|
+
/** Known provider substrings, ordered so the more specific match
|
|
503
|
+
* wins (e.g. "vertexai" before "gemini" because Vertex-hosted Gemini
|
|
504
|
+
* calls also contain "gemini" in some serialized paths, but the
|
|
505
|
+
* canonical bucket is "vertexai"). */
|
|
506
|
+
const KNOWN_PROVIDERS = [
|
|
507
|
+
"anthropic",
|
|
508
|
+
"openai",
|
|
509
|
+
"cohere",
|
|
510
|
+
"vertexai",
|
|
511
|
+
"vertex",
|
|
512
|
+
"gemini",
|
|
513
|
+
"googlegenai",
|
|
514
|
+
"ollama",
|
|
515
|
+
"mistral",
|
|
516
|
+
"groq",
|
|
517
|
+
"bedrock",
|
|
518
|
+
];
|
|
519
|
+
/** Collapse the KNOWN_PROVIDERS variants to the canonical bucket the
|
|
520
|
+
* backend expects. Kept aligned with the values shipped by the
|
|
521
|
+
* direct instrumentation modules. */
|
|
522
|
+
function normalizeProvider(known) {
|
|
523
|
+
if (known === "vertexai" || known === "vertex")
|
|
524
|
+
return "vertexai";
|
|
525
|
+
if (known === "googlegenai")
|
|
526
|
+
return "gemini";
|
|
527
|
+
if (known === "bedrock")
|
|
528
|
+
return "bedrock";
|
|
529
|
+
return known;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* LangChain often wraps the provider SDK exception in its own
|
|
533
|
+
* envelope (`LangChainError`, `RetryError`, etc.) with the real
|
|
534
|
+
* exception dangling off `.cause`. Walk one hop to reach the raw
|
|
535
|
+
* provider exception so the class-name-based classifier scores it
|
|
536
|
+
* correctly. Falls back to the original object when no cause chain
|
|
537
|
+
* exists.
|
|
538
|
+
*/
|
|
539
|
+
function unwrapProviderException(err) {
|
|
540
|
+
if (!err || typeof err !== "object")
|
|
541
|
+
return err;
|
|
542
|
+
const cause = err.cause;
|
|
543
|
+
if (cause instanceof Error)
|
|
544
|
+
return cause;
|
|
545
|
+
return err;
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Try to JSON.parse a string; return the parsed value on success,
|
|
549
|
+
* the original string on failure. Used to derive a structured shape
|
|
550
|
+
* for `arguments` / `return_value` from LangChain's stringly-typed
|
|
551
|
+
* tool I/O so tool_schema_drift can fingerprint the underlying
|
|
552
|
+
* shape.
|
|
553
|
+
*/
|
|
554
|
+
function tryParseJson(s) {
|
|
555
|
+
if (typeof s !== "string")
|
|
556
|
+
return s;
|
|
557
|
+
const trimmed = s.trim();
|
|
558
|
+
if (!trimmed)
|
|
559
|
+
return s;
|
|
560
|
+
const first = trimmed[0] ?? "";
|
|
561
|
+
// Cheap-guard: only try JSON.parse on strings that could plausibly
|
|
562
|
+
// be JSON. Avoids a try/catch cycle for every plain-text output.
|
|
563
|
+
if (first !== "{" &&
|
|
564
|
+
first !== "[" &&
|
|
565
|
+
first !== '"' &&
|
|
566
|
+
!(first >= "0" && first <= "9") &&
|
|
567
|
+
first !== "-" &&
|
|
568
|
+
trimmed !== "true" &&
|
|
569
|
+
trimmed !== "false" &&
|
|
570
|
+
trimmed !== "null") {
|
|
571
|
+
return s;
|
|
572
|
+
}
|
|
573
|
+
try {
|
|
574
|
+
return JSON.parse(trimmed);
|
|
575
|
+
}
|
|
576
|
+
catch {
|
|
577
|
+
return s;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
function extractRoleMessages(conversation) {
|
|
581
|
+
let userMessage = "";
|
|
582
|
+
let systemPrompt = "";
|
|
583
|
+
for (const msg of conversation) {
|
|
584
|
+
if (!msg || typeof msg !== "object")
|
|
585
|
+
continue;
|
|
586
|
+
const m = msg;
|
|
587
|
+
let msgType = "";
|
|
588
|
+
if (typeof m.type === "string" && m.type) {
|
|
589
|
+
msgType = m.type;
|
|
590
|
+
}
|
|
591
|
+
else if (typeof m._getType === "function") {
|
|
592
|
+
try {
|
|
593
|
+
msgType = m._getType();
|
|
594
|
+
}
|
|
595
|
+
catch {
|
|
596
|
+
// ignore, fall through to constructor name
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
if (!msgType && m.constructor?.name) {
|
|
600
|
+
msgType = m.constructor.name;
|
|
601
|
+
}
|
|
602
|
+
let content = m.content;
|
|
603
|
+
if (Array.isArray(content)) {
|
|
604
|
+
const parts = [];
|
|
605
|
+
for (const block of content) {
|
|
606
|
+
if (typeof block === "string") {
|
|
607
|
+
parts.push(block);
|
|
608
|
+
}
|
|
609
|
+
else if (block && typeof block === "object") {
|
|
610
|
+
const b = block;
|
|
611
|
+
if (b.type === "text" && typeof b.text === "string") {
|
|
612
|
+
parts.push(b.text);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
content = parts.join(" ");
|
|
617
|
+
}
|
|
618
|
+
const contentStr = typeof content === "string" ? content : content == null ? "" : String(content);
|
|
619
|
+
const t = msgType.toLowerCase();
|
|
620
|
+
if (t === "system" || t === "systemmessage") {
|
|
621
|
+
systemPrompt = contentStr;
|
|
622
|
+
}
|
|
623
|
+
else if (t === "human" ||
|
|
624
|
+
t === "humanmessage" ||
|
|
625
|
+
t === "user" ||
|
|
626
|
+
t === "usermessage") {
|
|
627
|
+
userMessage = contentStr;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return { userMessage, systemPrompt };
|
|
631
|
+
}
|
|
632
|
+
function extractResponseText(response) {
|
|
633
|
+
try {
|
|
634
|
+
if (!response || typeof response !== "object")
|
|
635
|
+
return "";
|
|
636
|
+
const r = response;
|
|
637
|
+
const generations = r.generations;
|
|
638
|
+
if (!Array.isArray(generations) || generations.length === 0)
|
|
639
|
+
return "";
|
|
640
|
+
const firstBatch = generations[0];
|
|
641
|
+
if (!Array.isArray(firstBatch) || firstBatch.length === 0)
|
|
642
|
+
return "";
|
|
643
|
+
const gen = firstBatch[0];
|
|
644
|
+
if (typeof gen?.text === "string" && gen.text)
|
|
645
|
+
return gen.text;
|
|
646
|
+
const message = gen?.message;
|
|
647
|
+
if (message && typeof message === "object") {
|
|
648
|
+
const content = message.content;
|
|
649
|
+
if (typeof content === "string")
|
|
650
|
+
return content;
|
|
651
|
+
if (Array.isArray(content)) {
|
|
652
|
+
const parts = [];
|
|
653
|
+
for (const block of content) {
|
|
654
|
+
if (typeof block === "string") {
|
|
655
|
+
parts.push(block);
|
|
656
|
+
}
|
|
657
|
+
else if (block && typeof block === "object") {
|
|
658
|
+
const b = block;
|
|
659
|
+
if (b.type === "text" && typeof b.text === "string") {
|
|
660
|
+
parts.push(b.text);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return parts.join(" ");
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
catch {
|
|
669
|
+
// fall through to empty
|
|
670
|
+
}
|
|
671
|
+
return "";
|
|
672
|
+
}
|
|
673
|
+
function extractTokenUsage(response) {
|
|
674
|
+
try {
|
|
675
|
+
if (!response || typeof response !== "object")
|
|
676
|
+
return [0, 0];
|
|
677
|
+
const r = response;
|
|
678
|
+
const llmOutput = r.llmOutput ?? r.llm_output ?? {};
|
|
679
|
+
const usageAny = llmOutput["tokenUsage"] ??
|
|
680
|
+
llmOutput["token_usage"] ??
|
|
681
|
+
llmOutput["usage"] ??
|
|
682
|
+
{};
|
|
683
|
+
const usage = typeof usageAny === "object" && usageAny !== null
|
|
684
|
+
? usageAny
|
|
685
|
+
: {};
|
|
686
|
+
const inputTokens = toInt(usage["promptTokens"]) ||
|
|
687
|
+
toInt(usage["prompt_tokens"]) ||
|
|
688
|
+
toInt(usage["inputTokens"]) ||
|
|
689
|
+
toInt(usage["input_tokens"]);
|
|
690
|
+
const outputTokens = toInt(usage["completionTokens"]) ||
|
|
691
|
+
toInt(usage["completion_tokens"]) ||
|
|
692
|
+
toInt(usage["outputTokens"]) ||
|
|
693
|
+
toInt(usage["output_tokens"]);
|
|
694
|
+
if (inputTokens > 0 || outputTokens > 0) {
|
|
695
|
+
return [inputTokens, outputTokens];
|
|
696
|
+
}
|
|
697
|
+
// Newer LangChain: `usage_metadata` on the generation itself.
|
|
698
|
+
const generations = r.generations;
|
|
699
|
+
if (Array.isArray(generations) && generations.length > 0) {
|
|
700
|
+
const firstBatch = generations[0];
|
|
701
|
+
if (Array.isArray(firstBatch) && firstBatch.length > 0) {
|
|
702
|
+
const gen = firstBatch[0];
|
|
703
|
+
const meta = gen?.message?.usage_metadata ?? gen?.message?.usageMetadata ?? {};
|
|
704
|
+
if (meta && typeof meta === "object") {
|
|
705
|
+
return [
|
|
706
|
+
toInt(meta["input_tokens"]) || toInt(meta["inputTokens"]),
|
|
707
|
+
toInt(meta["output_tokens"]) || toInt(meta["outputTokens"]),
|
|
708
|
+
];
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
catch {
|
|
714
|
+
// fall through
|
|
715
|
+
}
|
|
716
|
+
return [0, 0];
|
|
717
|
+
}
|
|
718
|
+
function extractToolName(serialized) {
|
|
719
|
+
if (!serialized || typeof serialized !== "object")
|
|
720
|
+
return "unknown_tool";
|
|
721
|
+
const name = serialized["name"];
|
|
722
|
+
if (typeof name === "string" && name)
|
|
723
|
+
return name;
|
|
724
|
+
const ident = serialized["id"];
|
|
725
|
+
if (Array.isArray(ident) && ident.length > 0) {
|
|
726
|
+
const last = ident[ident.length - 1];
|
|
727
|
+
if (typeof last === "string" && last)
|
|
728
|
+
return last;
|
|
729
|
+
}
|
|
730
|
+
return "unknown_tool";
|
|
731
|
+
}
|
|
732
|
+
function extractError(err) {
|
|
733
|
+
if (err instanceof Error) {
|
|
734
|
+
return [err.constructor.name || "Error", err.message];
|
|
735
|
+
}
|
|
736
|
+
if (err && typeof err === "object") {
|
|
737
|
+
const e = err;
|
|
738
|
+
return [
|
|
739
|
+
typeof e.name === "string" && e.name ? e.name : "Error",
|
|
740
|
+
typeof e.message === "string" ? e.message : "",
|
|
741
|
+
];
|
|
742
|
+
}
|
|
743
|
+
return ["Error", typeof err === "string" ? err : ""];
|
|
744
|
+
}
|
|
745
|
+
// ── Helpers ────────────────────────────────────────────────────────
|
|
746
|
+
function truncate(s, maxLen) {
|
|
747
|
+
if (!s)
|
|
748
|
+
return "";
|
|
749
|
+
if (s.length <= maxLen)
|
|
750
|
+
return s;
|
|
751
|
+
return s.slice(0, maxLen - 3) + "...";
|
|
752
|
+
}
|
|
753
|
+
function toInt(v) {
|
|
754
|
+
if (typeof v === "number" && Number.isFinite(v)) {
|
|
755
|
+
return Math.max(0, Math.floor(v));
|
|
756
|
+
}
|
|
757
|
+
if (typeof v === "string") {
|
|
758
|
+
const n = parseInt(v, 10);
|
|
759
|
+
return Number.isFinite(n) ? Math.max(0, n) : 0;
|
|
760
|
+
}
|
|
761
|
+
return 0;
|
|
762
|
+
}
|
|
763
|
+
function reprValue(v) {
|
|
764
|
+
if (v == null)
|
|
765
|
+
return "";
|
|
766
|
+
if (typeof v === "string")
|
|
767
|
+
return v;
|
|
768
|
+
try {
|
|
769
|
+
return JSON.stringify(v);
|
|
770
|
+
}
|
|
771
|
+
catch {
|
|
772
|
+
return String(v);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
//# sourceMappingURL=langchain.js.map
|