mesedi 0.2.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +57 -5
  2. package/dist/_errorClassesGenerated.d.ts +18 -0
  3. package/dist/_errorClassesGenerated.d.ts.map +1 -0
  4. package/dist/_errorClassesGenerated.js +24 -0
  5. package/dist/_errorClassesGenerated.js.map +1 -0
  6. package/dist/anthropic_integration.d.ts.map +1 -1
  7. package/dist/anthropic_integration.js +51 -10
  8. package/dist/anthropic_integration.js.map +1 -1
  9. package/dist/client.d.ts +26 -6
  10. package/dist/client.d.ts.map +1 -1
  11. package/dist/client.js +35 -7
  12. package/dist/client.js.map +1 -1
  13. package/dist/cohere_integration.d.ts +42 -0
  14. package/dist/cohere_integration.d.ts.map +1 -0
  15. package/dist/cohere_integration.js +456 -0
  16. package/dist/cohere_integration.js.map +1 -0
  17. package/dist/compress.d.ts +69 -0
  18. package/dist/compress.d.ts.map +1 -0
  19. package/dist/compress.js +75 -0
  20. package/dist/compress.js.map +1 -0
  21. package/dist/context.d.ts +11 -2
  22. package/dist/context.d.ts.map +1 -1
  23. package/dist/context.js +13 -3
  24. package/dist/context.js.map +1 -1
  25. package/dist/errors.d.ts +137 -0
  26. package/dist/errors.d.ts.map +1 -0
  27. package/dist/errors.js +462 -0
  28. package/dist/errors.js.map +1 -0
  29. package/dist/events.d.ts +13 -0
  30. package/dist/events.d.ts.map +1 -1
  31. package/dist/events.js +26 -1
  32. package/dist/events.js.map +1 -1
  33. package/dist/gemini_integration.d.ts +33 -0
  34. package/dist/gemini_integration.d.ts.map +1 -0
  35. package/dist/gemini_integration.js +334 -0
  36. package/dist/gemini_integration.js.map +1 -0
  37. package/dist/halt_stream.d.ts.map +1 -1
  38. package/dist/halt_stream.js +11 -1
  39. package/dist/halt_stream.js.map +1 -1
  40. package/dist/index.d.ts +15 -2
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +9 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/integrations/langchain.d.ts +95 -0
  45. package/dist/integrations/langchain.d.ts.map +1 -0
  46. package/dist/integrations/langchain.js +549 -0
  47. package/dist/integrations/langchain.js.map +1 -0
  48. package/dist/integrations/langgraph.d.ts +99 -0
  49. package/dist/integrations/langgraph.d.ts.map +1 -0
  50. package/dist/integrations/langgraph.js +572 -0
  51. package/dist/integrations/langgraph.js.map +1 -0
  52. package/dist/integrations/mastra.d.ts +114 -0
  53. package/dist/integrations/mastra.d.ts.map +1 -0
  54. package/dist/integrations/mastra.js +354 -0
  55. package/dist/integrations/mastra.js.map +1 -0
  56. package/dist/integrations/openai_agents.d.ts +66 -0
  57. package/dist/integrations/openai_agents.d.ts.map +1 -0
  58. package/dist/integrations/openai_agents.js +243 -0
  59. package/dist/integrations/openai_agents.js.map +1 -0
  60. package/dist/integrations/promptfoo.d.ts +62 -0
  61. package/dist/integrations/promptfoo.d.ts.map +1 -0
  62. package/dist/integrations/promptfoo.js +76 -0
  63. package/dist/integrations/promptfoo.js.map +1 -0
  64. package/dist/integrations/ragas.d.ts +65 -0
  65. package/dist/integrations/ragas.d.ts.map +1 -0
  66. package/dist/integrations/ragas.js +78 -0
  67. package/dist/integrations/ragas.js.map +1 -0
  68. package/dist/integrations/vectara.d.ts +60 -0
  69. package/dist/integrations/vectara.d.ts.map +1 -0
  70. package/dist/integrations/vectara.js +59 -0
  71. package/dist/integrations/vectara.js.map +1 -0
  72. package/dist/observe.d.ts +355 -0
  73. package/dist/observe.d.ts.map +1 -1
  74. package/dist/observe.js +532 -0
  75. package/dist/observe.js.map +1 -1
  76. package/dist/ollama_integration.d.ts +126 -0
  77. package/dist/ollama_integration.d.ts.map +1 -0
  78. package/dist/ollama_integration.js +404 -0
  79. package/dist/ollama_integration.js.map +1 -0
  80. package/dist/openai_integration.d.ts +67 -0
  81. package/dist/openai_integration.d.ts.map +1 -0
  82. package/dist/openai_integration.js +574 -0
  83. package/dist/openai_integration.js.map +1 -0
  84. package/dist/tool.d.ts.map +1 -1
  85. package/dist/tool.js +189 -6
  86. package/dist/tool.js.map +1 -1
  87. package/dist/truncate.d.ts +84 -0
  88. package/dist/truncate.d.ts.map +1 -0
  89. package/dist/truncate.js +134 -0
  90. package/dist/truncate.js.map +1 -0
  91. package/dist/vertex_gemini_integration.d.ts +27 -0
  92. package/dist/vertex_gemini_integration.d.ts.map +1 -0
  93. package/dist/vertex_gemini_integration.js +229 -0
  94. package/dist/vertex_gemini_integration.js.map +1 -0
  95. package/dist/wrap.d.ts +36 -0
  96. package/dist/wrap.d.ts.map +1 -1
  97. package/dist/wrap.js +5 -2
  98. package/dist/wrap.js.map +1 -1
  99. package/package.json +36 -3
@@ -0,0 +1,99 @@
1
+ /**
2
+ * LangGraph integration for Mesedi (Mesedi #26).
3
+ *
4
+ * Usage (one-liner):
5
+ *
6
+ * import { wrap } from "mesedi";
7
+ * import { instrumentLangGraph } from "mesedi/integrations/langgraph";
8
+ * import { StateGraph } from "@langchain/langgraph";
9
+ *
10
+ * let graph = buildMyGraph().compile();
11
+ * graph = instrumentLangGraph(graph);
12
+ *
13
+ * export const runAgent = wrap(async (question: string) => {
14
+ * const result = await graph.invoke({ question });
15
+ * return result.answer;
16
+ * });
17
+ *
18
+ * What the integration emits:
19
+ *
20
+ * - `llm_call` events for every chat-model invocation inside the
21
+ * graph (via the inherited LangChain dispatcher).
22
+ * - `tool_call` events for every tool invocation.
23
+ * - `checkpoint` events at every node entry and exit, with the
24
+ * node name and a truncated state repr. Feeds the semantic_loop
25
+ * detector (#6) which hashes canonical state.
26
+ * - `agent_handoff` events when the graph invokes a compiled
27
+ * sub-graph. Feeds cascading_failure (#12) and
28
+ * coordination_deadlock (#13).
29
+ *
30
+ * Why this exists at the TS layer:
31
+ *
32
+ * The Python integration ships in `mesedi.integrations.langgraph`.
33
+ * The TypeScript LangChain / LangGraph stack uses the same callback
34
+ * handler surface (`BaseCallbackHandler` from
35
+ * `@langchain/core/callbacks/base`), so this port keeps wire-format
36
+ * parity with the Python emitter while reading idiomatically in
37
+ * TypeScript.
38
+ *
39
+ * Out of scope for v1:
40
+ *
41
+ * - Streaming hooks (`graph.streamEvents`). Covered in a follow-up.
42
+ * - LangGraph's persistence (checkpointer) — Mesedi emits
43
+ * telemetry parallel to it without touching the stored state.
44
+ * - LangGraph `interrupt()` auto-bridging — use the existing HITL
45
+ * helpers (`pauseForHuman`, `requestHumanIntervention`) for now.
46
+ */
47
+ /**
48
+ * Mesedi LangGraph callback handler. Duck-typed against
49
+ * `BaseCallbackHandler` from `@langchain/core/callbacks/base`.
50
+ *
51
+ * We do NOT extend the LangChain class directly because that would
52
+ * make `@langchain/core` a required runtime dependency. The handler
53
+ * implements the methods the LangGraph dispatcher calls and is
54
+ * picked up structurally.
55
+ */
56
+ export declare class MesediLangGraphCallbackHandler {
57
+ readonly name = "mesedi-langgraph";
58
+ readonly raiseError = false;
59
+ private nodeStarts;
60
+ private handoffStarts;
61
+ onChainStart(serialized: Record<string, unknown> | undefined, inputs: Record<string, unknown> | undefined, runId: string, _parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>): void;
62
+ onChainEnd(outputs: Record<string, unknown> | undefined, runId: string, _parentRunId?: string, _tags?: string[], _kwargs?: Record<string, unknown>): void;
63
+ onChainError(error: unknown, runId: string, _parentRunId?: string, _tags?: string[], _kwargs?: Record<string, unknown>): void;
64
+ onChatModelStart(serialized: Record<string, unknown> | undefined, messages: Array<Array<unknown>>, runId: string, _parentRunId?: string, extraParams?: Record<string, unknown>): void;
65
+ onLLMStart(serialized: Record<string, unknown> | undefined, prompts: string[], runId: string, _parentRunId?: string, extraParams?: Record<string, unknown>): void;
66
+ onLLMEnd(output: unknown, runId: string): void;
67
+ onLLMError(_error: unknown, runId: string): void;
68
+ onToolStart(serialized: Record<string, unknown> | undefined, inputStr: string | Record<string, unknown>, runId: string): void;
69
+ onToolEnd(output: unknown, runId: string): void;
70
+ onToolError(error: unknown, runId: string): void;
71
+ private llmStarts;
72
+ private toolStarts;
73
+ private mostRecentNode;
74
+ }
75
+ /**
76
+ * Attach Mesedi telemetry to a compiled LangGraph.
77
+ *
78
+ * Wraps the supplied compiled graph so every call to `invoke`,
79
+ * `stream`, or their async variants automatically attaches a
80
+ * `MesediLangGraphCallbackHandler` to the LangChain callback config.
81
+ * The wrap is non-destructive: it adds the handler to whatever
82
+ * callbacks the caller passed in via `config={ callbacks: [...] }`,
83
+ * so existing instrumentation keeps working.
84
+ *
85
+ * Returns the same `graph` object with patched methods, so callers
86
+ * can re-assign in place.
87
+ *
88
+ * Outside a `wrap()` execution context, the handler's emissions
89
+ * silently no-op, so it is safe to instrument the graph once at
90
+ * module load and let individual invocations decide whether they
91
+ * want to be observed.
92
+ */
93
+ export declare function instrumentLangGraph<G extends LangGraphLike>(graph: G): G;
94
+ interface LangGraphLike {
95
+ invoke?: (input: unknown, config?: Record<string, unknown>, ...rest: unknown[]) => Promise<unknown>;
96
+ stream?: (input: unknown, config?: Record<string, unknown>, ...rest: unknown[]) => Promise<unknown> | AsyncIterable<unknown>;
97
+ }
98
+ export {};
99
+ //# sourceMappingURL=langgraph.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"langgraph.d.ts","sourceRoot":"","sources":["../../src/integrations/langgraph.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,572 @@
1
+ /**
2
+ * LangGraph integration for Mesedi (Mesedi #26).
3
+ *
4
+ * Usage (one-liner):
5
+ *
6
+ * import { wrap } from "mesedi";
7
+ * import { instrumentLangGraph } from "mesedi/integrations/langgraph";
8
+ * import { StateGraph } from "@langchain/langgraph";
9
+ *
10
+ * let graph = buildMyGraph().compile();
11
+ * graph = instrumentLangGraph(graph);
12
+ *
13
+ * export const runAgent = wrap(async (question: string) => {
14
+ * const result = await graph.invoke({ question });
15
+ * return result.answer;
16
+ * });
17
+ *
18
+ * What the integration emits:
19
+ *
20
+ * - `llm_call` events for every chat-model invocation inside the
21
+ * graph (via the inherited LangChain dispatcher).
22
+ * - `tool_call` events for every tool invocation.
23
+ * - `checkpoint` events at every node entry and exit, with the
24
+ * node name and a truncated state repr. Feeds the semantic_loop
25
+ * detector (#6) which hashes canonical state.
26
+ * - `agent_handoff` events when the graph invokes a compiled
27
+ * sub-graph. Feeds cascading_failure (#12) and
28
+ * coordination_deadlock (#13).
29
+ *
30
+ * Why this exists at the TS layer:
31
+ *
32
+ * The Python integration ships in `mesedi.integrations.langgraph`.
33
+ * The TypeScript LangChain / LangGraph stack uses the same callback
34
+ * handler surface (`BaseCallbackHandler` from
35
+ * `@langchain/core/callbacks/base`), so this port keeps wire-format
36
+ * parity with the Python emitter while reading idiomatically in
37
+ * TypeScript.
38
+ *
39
+ * Out of scope for v1:
40
+ *
41
+ * - Streaming hooks (`graph.streamEvents`). Covered in a follow-up.
42
+ * - LangGraph's persistence (checkpointer) — Mesedi emits
43
+ * telemetry parallel to it without touching the stored state.
44
+ * - LangGraph `interrupt()` auto-bridging — use the existing HITL
45
+ * helpers (`pauseForHuman`, `requestHumanIntervention`) for now.
46
+ */
47
+ import { getClient } from "../client.js";
48
+ import { currentExecutionContext, newEventId } from "../context.js";
49
+ import { EventType, utcNowRfc3339 } from "../events.js";
50
+ // Truncation budget for checkpoint state repr. Wide enough for
51
+ // semantic_loop hashing to be useful; narrow enough to keep
52
+ // individual event payloads bounded on graphs with large state.
53
+ const MAX_NODE_STATE_REPR = 1000;
54
+ /**
55
+ * Mesedi LangGraph callback handler. Duck-typed against
56
+ * `BaseCallbackHandler` from `@langchain/core/callbacks/base`.
57
+ *
58
+ * We do NOT extend the LangChain class directly because that would
59
+ * make `@langchain/core` a required runtime dependency. The handler
60
+ * implements the methods the LangGraph dispatcher calls and is
61
+ * picked up structurally.
62
+ */
63
+ export class MesediLangGraphCallbackHandler {
64
+ // The LangChain dispatcher reads this field to decide whether the
65
+ // handler is OK to share across runs. We allow it.
66
+ name = "mesedi-langgraph";
67
+ raiseError = false;
68
+ // run_id → { nodeName, startedAtMs }. Used to pair node
69
+ // start/end so we can compute node duration and emit it on the
70
+ // exit checkpoint.
71
+ nodeStarts = new Map();
72
+ // run_id → { fromAgent, toAgent }. Used to pair sub-graph
73
+ // start/end. We only emit the handoff request at start; the
74
+ // sub-graph's own execution (if it ran as a nested wrap) carries
75
+ // its terminal status independently.
76
+ handoffStarts = new Map();
77
+ // ── Node-level chain events ─────────────────────────────────────
78
+ onChainStart(serialized, inputs, runId, _parentRunId, tags, metadata) {
79
+ const nodeName = extractLangGraphNodeName(serialized, tags, metadata);
80
+ if (nodeName) {
81
+ this.nodeStarts.set(runId, {
82
+ nodeName,
83
+ startedAtMs: Date.now(),
84
+ });
85
+ emitCheckpoint(nodeName, inputs ?? {}, "enter", 0);
86
+ }
87
+ const subgraphName = extractSubgraphName(serialized, tags, metadata);
88
+ if (subgraphName) {
89
+ const fromAgent = this.mostRecentNode() ?? "graph";
90
+ this.handoffStarts.set(runId, { fromAgent, toAgent: subgraphName });
91
+ emitHandoffRequest(fromAgent, subgraphName, summarizeInputs(inputs ?? {}));
92
+ }
93
+ }
94
+ onChainEnd(outputs, runId, _parentRunId, _tags, _kwargs) {
95
+ const entry = this.nodeStarts.get(runId);
96
+ if (entry) {
97
+ this.nodeStarts.delete(runId);
98
+ const durationMs = Math.max(0, Date.now() - entry.startedAtMs);
99
+ emitCheckpoint(entry.nodeName, outputs ?? {}, "exit", durationMs);
100
+ }
101
+ this.handoffStarts.delete(runId);
102
+ }
103
+ onChainError(error, runId, _parentRunId, _tags, _kwargs) {
104
+ const entry = this.nodeStarts.get(runId);
105
+ if (entry) {
106
+ this.nodeStarts.delete(runId);
107
+ const durationMs = Math.max(0, Date.now() - entry.startedAtMs);
108
+ const errorName = error && typeof error === "object" && "name" in error
109
+ ? String(error.name)
110
+ : typeof error === "string"
111
+ ? error
112
+ : "Error";
113
+ emitCheckpoint(entry.nodeName, { error_type: errorName }, "error", durationMs);
114
+ }
115
+ this.handoffStarts.delete(runId);
116
+ }
117
+ // ── LLM passthrough ─────────────────────────────────────────────
118
+ //
119
+ // LangChain dispatches LLM and tool callbacks to this handler too.
120
+ // The Python integration inherits the LLM/tool handling from the
121
+ // existing MesediCallbackHandler; the TypeScript SDK does not ship
122
+ // a separate LangChain handler yet, so we provide thin
123
+ // implementations here.
124
+ onChatModelStart(serialized, messages, runId, _parentRunId, extraParams) {
125
+ const model = extractModel(serialized, extraParams);
126
+ const last = messages[messages.length - 1] ?? [];
127
+ const { userMessage, systemPrompt } = extractRoleMessages(last);
128
+ this.llmStarts.set(runId, {
129
+ model,
130
+ userMessage,
131
+ systemPrompt,
132
+ startedAtMs: Date.now(),
133
+ });
134
+ }
135
+ onLLMStart(serialized, prompts, runId, _parentRunId, extraParams) {
136
+ const model = extractModel(serialized, extraParams);
137
+ this.llmStarts.set(runId, {
138
+ model,
139
+ userMessage: prompts[prompts.length - 1] ?? "",
140
+ systemPrompt: "",
141
+ startedAtMs: Date.now(),
142
+ });
143
+ }
144
+ onLLMEnd(output, runId) {
145
+ const ctx = this.llmStarts.get(runId);
146
+ if (!ctx)
147
+ return;
148
+ this.llmStarts.delete(runId);
149
+ const durationMs = Math.max(0, Date.now() - ctx.startedAtMs);
150
+ const responseText = extractResponseText(output);
151
+ const { inputTokens, outputTokens } = extractTokenUsage(output);
152
+ emitLLMCallEvent({
153
+ model: ctx.model,
154
+ userMessage: ctx.userMessage,
155
+ systemPrompt: ctx.systemPrompt,
156
+ responseText,
157
+ inputTokens,
158
+ outputTokens,
159
+ durationMs,
160
+ status: "ok",
161
+ });
162
+ }
163
+ onLLMError(_error, runId) {
164
+ const ctx = this.llmStarts.get(runId);
165
+ if (!ctx)
166
+ return;
167
+ this.llmStarts.delete(runId);
168
+ const durationMs = Math.max(0, Date.now() - ctx.startedAtMs);
169
+ emitLLMCallEvent({
170
+ model: ctx.model,
171
+ userMessage: ctx.userMessage,
172
+ systemPrompt: ctx.systemPrompt,
173
+ responseText: "",
174
+ inputTokens: 0,
175
+ outputTokens: 0,
176
+ durationMs,
177
+ status: "failed",
178
+ });
179
+ }
180
+ onToolStart(serialized, inputStr, runId) {
181
+ // Pull the tool name off the serialized payload if present.
182
+ // The conditional has to be a plain ternary (not the
183
+ // && && ?? chain a previous revision tried) because `&&`
184
+ // short-circuits to `false` when an intermediate predicate is
185
+ // false, and `??` does not coalesce `false`.
186
+ const toolName = serialized && typeof serialized.name === "string" && serialized.name
187
+ ? serialized.name
188
+ : "tool";
189
+ const input = typeof inputStr === "string" ? inputStr : JSON.stringify(inputStr);
190
+ this.toolStarts.set(runId, {
191
+ name: toolName,
192
+ inputStr: input,
193
+ startedAtMs: Date.now(),
194
+ });
195
+ }
196
+ onToolEnd(output, runId) {
197
+ const ctx = this.toolStarts.get(runId);
198
+ if (!ctx)
199
+ return;
200
+ this.toolStarts.delete(runId);
201
+ const durationMs = Math.max(0, Date.now() - ctx.startedAtMs);
202
+ emitToolCallEvent({
203
+ toolName: ctx.name,
204
+ inputStr: ctx.inputStr,
205
+ resultSummary: output == null
206
+ ? ""
207
+ : typeof output === "string"
208
+ ? output
209
+ : JSON.stringify(output),
210
+ durationMs,
211
+ status: "ok",
212
+ });
213
+ }
214
+ onToolError(error, runId) {
215
+ const ctx = this.toolStarts.get(runId);
216
+ if (!ctx)
217
+ return;
218
+ this.toolStarts.delete(runId);
219
+ const durationMs = Math.max(0, Date.now() - ctx.startedAtMs);
220
+ const errorName = error && typeof error === "object" && "name" in error
221
+ ? String(error.name)
222
+ : "Error";
223
+ const errorMessage = error && typeof error === "object" && "message" in error
224
+ ? String(error.message)
225
+ : "";
226
+ emitToolCallEvent({
227
+ toolName: ctx.name,
228
+ inputStr: ctx.inputStr,
229
+ resultSummary: "",
230
+ durationMs,
231
+ status: "failed",
232
+ exceptionType: errorName,
233
+ exceptionMessage: errorMessage,
234
+ });
235
+ }
236
+ // Internal pairing tables for LLM and tool events.
237
+ llmStarts = new Map();
238
+ toolStarts = new Map();
239
+ // Internal helper to look up the most recent in-flight node so
240
+ // sub-graph handoffs can name a plausible from_agent even when
241
+ // the dispatcher does not surface an explicit parent.
242
+ mostRecentNode() {
243
+ if (this.nodeStarts.size === 0)
244
+ return null;
245
+ // JS Map preserves insertion order.
246
+ let last = null;
247
+ for (const v of this.nodeStarts.values())
248
+ last = v.nodeName;
249
+ return last;
250
+ }
251
+ }
252
+ /**
253
+ * Attach Mesedi telemetry to a compiled LangGraph.
254
+ *
255
+ * Wraps the supplied compiled graph so every call to `invoke`,
256
+ * `stream`, or their async variants automatically attaches a
257
+ * `MesediLangGraphCallbackHandler` to the LangChain callback config.
258
+ * The wrap is non-destructive: it adds the handler to whatever
259
+ * callbacks the caller passed in via `config={ callbacks: [...] }`,
260
+ * so existing instrumentation keeps working.
261
+ *
262
+ * Returns the same `graph` object with patched methods, so callers
263
+ * can re-assign in place.
264
+ *
265
+ * Outside a `wrap()` execution context, the handler's emissions
266
+ * silently no-op, so it is safe to instrument the graph once at
267
+ * module load and let individual invocations decide whether they
268
+ * want to be observed.
269
+ */
270
+ export function instrumentLangGraph(graph) {
271
+ const handler = new MesediLangGraphCallbackHandler();
272
+ const ensureHandler = (config) => {
273
+ const cfg = { ...(config ?? {}) };
274
+ const cbs = Array.isArray(cfg.callbacks) ? [...cfg.callbacks] : [];
275
+ for (const cb of cbs) {
276
+ if (cb instanceof MesediLangGraphCallbackHandler)
277
+ return cfg;
278
+ }
279
+ cbs.push(handler);
280
+ cfg.callbacks = cbs;
281
+ return cfg;
282
+ };
283
+ const origInvoke = graph.invoke?.bind(graph);
284
+ if (origInvoke) {
285
+ graph.invoke = async function patchedInvoke(input, config, ...rest) {
286
+ return origInvoke(input, ensureHandler(config), ...rest);
287
+ };
288
+ }
289
+ const origStream = graph.stream?.bind(graph);
290
+ if (origStream) {
291
+ graph.stream = async function patchedStream(input, config, ...rest) {
292
+ return origStream(input, ensureHandler(config), ...rest);
293
+ };
294
+ }
295
+ return graph;
296
+ }
297
+ function extractLangGraphNodeName(_serialized, tags, metadata) {
298
+ if (metadata && typeof metadata === "object") {
299
+ const fromMeta = (typeof metadata.langgraph_node === "string" && metadata.langgraph_node) ||
300
+ (typeof metadata.node === "string" && metadata.node);
301
+ if (fromMeta)
302
+ return fromMeta;
303
+ }
304
+ if (Array.isArray(tags)) {
305
+ for (const t of tags) {
306
+ if (typeof t === "string" && t.startsWith("langgraph:node:")) {
307
+ return t.split(":", 3)[2] ?? null;
308
+ }
309
+ }
310
+ }
311
+ return null;
312
+ }
313
+ function extractSubgraphName(_serialized, tags, metadata) {
314
+ if (metadata && typeof metadata === "object") {
315
+ const fromMeta = (typeof metadata.langgraph_subgraph === "string" &&
316
+ metadata.langgraph_subgraph) ||
317
+ (typeof metadata.subgraph === "string" && metadata.subgraph);
318
+ if (fromMeta)
319
+ return fromMeta;
320
+ }
321
+ if (Array.isArray(tags)) {
322
+ for (const t of tags) {
323
+ if (typeof t === "string" && t.startsWith("langgraph:subgraph:")) {
324
+ return t.split(":", 3)[2] ?? null;
325
+ }
326
+ }
327
+ }
328
+ return null;
329
+ }
330
+ function summarizeInputs(inputs) {
331
+ let r;
332
+ try {
333
+ r = JSON.stringify(inputs);
334
+ }
335
+ catch {
336
+ r = String(inputs);
337
+ }
338
+ if (r.length > MAX_NODE_STATE_REPR) {
339
+ r = r.slice(0, MAX_NODE_STATE_REPR - 3) + "...";
340
+ }
341
+ return r;
342
+ }
343
+ function emitCheckpoint(nodeName, state, nodeEvent, durationMs) {
344
+ const ctx = currentExecutionContext();
345
+ if (!ctx)
346
+ return;
347
+ let stateRepr;
348
+ try {
349
+ stateRepr = JSON.stringify(state);
350
+ }
351
+ catch {
352
+ stateRepr = String(state);
353
+ }
354
+ if (stateRepr.length > MAX_NODE_STATE_REPR) {
355
+ stateRepr = stateRepr.slice(0, MAX_NODE_STATE_REPR - 3) + "...";
356
+ }
357
+ const payload = {
358
+ name: `langgraph.${nodeEvent}.${nodeName}`,
359
+ node: nodeName,
360
+ node_event: nodeEvent,
361
+ state_repr: stateRepr,
362
+ };
363
+ if (durationMs > 0)
364
+ payload["duration_ms"] = Math.trunc(durationMs);
365
+ const client = getClient();
366
+ const event = {
367
+ event_id: newEventId(),
368
+ execution_id: ctx.executionId,
369
+ event_type: EventType.CHECKPOINT,
370
+ sequence: ctx.nextSequence(),
371
+ timestamp: utcNowRfc3339(),
372
+ duration_ms: durationMs > 0 ? Math.trunc(durationMs) : undefined,
373
+ payload,
374
+ };
375
+ client.submitEvent(event);
376
+ }
377
+ function emitHandoffRequest(fromAgent, toAgent, taskSummary) {
378
+ const ctx = currentExecutionContext();
379
+ if (!ctx)
380
+ return;
381
+ const payload = {
382
+ from_agent: fromAgent,
383
+ to_agent: toAgent,
384
+ handoff_kind: "delegate",
385
+ task_summary: taskSummary,
386
+ };
387
+ const client = getClient();
388
+ const event = {
389
+ event_id: newEventId(),
390
+ execution_id: ctx.executionId,
391
+ event_type: EventType.AGENT_HANDOFF,
392
+ sequence: ctx.nextSequence(),
393
+ timestamp: utcNowRfc3339(),
394
+ payload,
395
+ };
396
+ client.submitEvent(event);
397
+ }
398
+ function emitLLMCallEvent(args) {
399
+ const ctx = currentExecutionContext();
400
+ if (!ctx)
401
+ return;
402
+ const payload = {
403
+ model: args.model,
404
+ user_prompt: args.userMessage,
405
+ system_prompt: args.systemPrompt,
406
+ response: args.responseText,
407
+ input_tokens: args.inputTokens,
408
+ output_tokens: args.outputTokens,
409
+ latency_ms: Math.trunc(args.durationMs),
410
+ status: args.status,
411
+ };
412
+ const client = getClient();
413
+ const event = {
414
+ event_id: newEventId(),
415
+ execution_id: ctx.executionId,
416
+ event_type: EventType.LLM_CALL,
417
+ sequence: ctx.nextSequence(),
418
+ timestamp: utcNowRfc3339(),
419
+ duration_ms: Math.trunc(args.durationMs),
420
+ payload,
421
+ };
422
+ client.submitEvent(event);
423
+ }
424
+ function emitToolCallEvent(args) {
425
+ const ctx = currentExecutionContext();
426
+ if (!ctx)
427
+ return;
428
+ const payload = {
429
+ tool_name: args.toolName,
430
+ arguments: args.inputStr,
431
+ return_value: args.resultSummary,
432
+ latency_ms: Math.trunc(args.durationMs),
433
+ status: args.status,
434
+ };
435
+ if (args.exceptionType)
436
+ payload["error_class"] = args.exceptionType;
437
+ if (args.exceptionMessage)
438
+ payload["error"] = args.exceptionMessage;
439
+ const client = getClient();
440
+ const event = {
441
+ event_id: newEventId(),
442
+ execution_id: ctx.executionId,
443
+ event_type: EventType.TOOL_CALL,
444
+ sequence: ctx.nextSequence(),
445
+ timestamp: utcNowRfc3339(),
446
+ duration_ms: Math.trunc(args.durationMs),
447
+ payload,
448
+ };
449
+ client.submitEvent(event);
450
+ }
451
+ function extractModel(serialized, kwargs) {
452
+ const inv = kwargs && typeof kwargs === "object"
453
+ ? (kwargs.invocation_params ?? {})
454
+ : {};
455
+ for (const key of ["model", "model_name", "deployment_name"]) {
456
+ const v = inv[key];
457
+ if (typeof v === "string" && v)
458
+ return v;
459
+ }
460
+ if (serialized && typeof serialized === "object") {
461
+ const sk = serialized.kwargs ?? {};
462
+ for (const key of ["model", "model_name", "deployment_name"]) {
463
+ const v = sk[key];
464
+ if (typeof v === "string" && v)
465
+ return v;
466
+ }
467
+ const id = serialized.id;
468
+ if (Array.isArray(id) && id.length > 0) {
469
+ const last = id[id.length - 1];
470
+ if (typeof last === "string")
471
+ return last;
472
+ }
473
+ if (typeof serialized.name === "string" && serialized.name) {
474
+ return serialized.name;
475
+ }
476
+ }
477
+ return "unknown";
478
+ }
479
+ function extractRoleMessages(conversation) {
480
+ let userMessage = "";
481
+ let systemPrompt = "";
482
+ for (const msg of conversation) {
483
+ if (!msg || typeof msg !== "object")
484
+ continue;
485
+ const m = msg;
486
+ const type = typeof m.type === "string"
487
+ ? m.type.toLowerCase()
488
+ : typeof m._getType === "function"
489
+ ? String(m._getType())
490
+ : String(m.constructor?.name ?? "").toLowerCase();
491
+ let content = m.content;
492
+ if (Array.isArray(content)) {
493
+ const parts = [];
494
+ for (const block of content) {
495
+ if (block && typeof block === "object") {
496
+ const b = block;
497
+ if (b.type === "text" && typeof b.text === "string") {
498
+ parts.push(b.text);
499
+ }
500
+ }
501
+ else if (typeof block === "string") {
502
+ parts.push(block);
503
+ }
504
+ }
505
+ content = parts.join(" ");
506
+ }
507
+ const text = typeof content === "string" ? content : String(content ?? "");
508
+ if (type.includes("system"))
509
+ systemPrompt = text;
510
+ else if (type.includes("human") || type.includes("user"))
511
+ userMessage = text;
512
+ }
513
+ return { userMessage, systemPrompt };
514
+ }
515
+ function extractResponseText(output) {
516
+ if (!output || typeof output !== "object")
517
+ return "";
518
+ const o = output;
519
+ // LangChain's LLMResult shape: { generations: [[{ text }]] }
520
+ const gens = o.generations;
521
+ if (Array.isArray(gens) && gens.length > 0) {
522
+ const inner = gens[0];
523
+ if (Array.isArray(inner) && inner.length > 0) {
524
+ const g = inner[0];
525
+ if (typeof g.text === "string")
526
+ return g.text;
527
+ if (g.message && typeof g.message === "object") {
528
+ const msg = g.message;
529
+ if (typeof msg.content === "string")
530
+ return msg.content;
531
+ }
532
+ }
533
+ }
534
+ if (typeof o.text === "string")
535
+ return o.text;
536
+ return "";
537
+ }
538
+ function extractTokenUsage(output) {
539
+ if (!output || typeof output !== "object") {
540
+ return { inputTokens: 0, outputTokens: 0 };
541
+ }
542
+ const o = output;
543
+ // LangChain shapes: llmOutput.tokenUsage / usage_metadata / usage.
544
+ const containers = [o.llmOutput, o.usage_metadata, o.usage].filter((c) => c !== null && typeof c === "object" && !Array.isArray(c));
545
+ for (const c of containers) {
546
+ const usage = c.tokenUsage && typeof c.tokenUsage === "object"
547
+ ? c.tokenUsage
548
+ : c;
549
+ const inputCandidates = [
550
+ usage.input_tokens,
551
+ usage.inputTokens,
552
+ usage.prompt_tokens,
553
+ usage.promptTokens,
554
+ ];
555
+ const outputCandidates = [
556
+ usage.output_tokens,
557
+ usage.outputTokens,
558
+ usage.completion_tokens,
559
+ usage.completionTokens,
560
+ ];
561
+ const inputTokens = inputCandidates.find((v) => typeof v === "number");
562
+ const outputTokens = outputCandidates.find((v) => typeof v === "number");
563
+ if (inputTokens != null || outputTokens != null) {
564
+ return {
565
+ inputTokens: Math.trunc(inputTokens ?? 0),
566
+ outputTokens: Math.trunc(outputTokens ?? 0),
567
+ };
568
+ }
569
+ }
570
+ return { inputTokens: 0, outputTokens: 0 };
571
+ }
572
+ //# sourceMappingURL=langgraph.js.map