langwatch 0.2.0 → 0.3.0-prerelease.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +268 -1
- package/dist/chunk-4BZATFKJ.mjs +181 -0
- package/dist/chunk-4BZATFKJ.mjs.map +1 -0
- package/dist/chunk-CSC3CMIT.mjs +118 -0
- package/dist/chunk-CSC3CMIT.mjs.map +1 -0
- package/dist/chunk-F63YKTXA.mjs +47 -0
- package/dist/chunk-F63YKTXA.mjs.map +1 -0
- package/dist/chunk-G3AUABT7.js +4 -0
- package/dist/chunk-G3AUABT7.js.map +1 -0
- package/dist/chunk-HPC6Z7J4.js +118 -0
- package/dist/chunk-HPC6Z7J4.js.map +1 -0
- package/dist/chunk-KGDAENGD.js +50 -0
- package/dist/chunk-KGDAENGD.js.map +1 -0
- package/dist/chunk-LD74LVRU.js +47 -0
- package/dist/chunk-LD74LVRU.js.map +1 -0
- package/dist/chunk-OM7VY3XT.mjs +4 -0
- package/dist/chunk-PCQVQ7SB.js +45 -0
- package/dist/chunk-PCQVQ7SB.js.map +1 -0
- package/dist/chunk-PMBEK6YE.mjs +424 -0
- package/dist/chunk-PMBEK6YE.mjs.map +1 -0
- package/dist/chunk-PR3JDWC3.mjs +50 -0
- package/dist/chunk-PR3JDWC3.mjs.map +1 -0
- package/dist/chunk-PTJ6AAI7.js +360 -0
- package/dist/chunk-PTJ6AAI7.js.map +1 -0
- package/dist/chunk-QEWDG5QE.mjs +45 -0
- package/dist/chunk-QEWDG5QE.mjs.map +1 -0
- package/dist/chunk-REUCVT7A.mjs +39 -0
- package/dist/chunk-REUCVT7A.mjs.map +1 -0
- package/dist/chunk-SVJ7SCGB.js +424 -0
- package/dist/chunk-SVJ7SCGB.js.map +1 -0
- package/dist/chunk-VJSOCNPA.js +181 -0
- package/dist/chunk-VJSOCNPA.js.map +1 -0
- package/dist/chunk-WM2GRSRW.js +39 -0
- package/dist/chunk-WM2GRSRW.js.map +1 -0
- package/dist/chunk-Z5J5UI5E.mjs +360 -0
- package/dist/chunk-Z5J5UI5E.mjs.map +1 -0
- package/dist/client-B2HqIKg6.d.ts +51 -0
- package/dist/client-XyCqclCi.d.mts +51 -0
- package/dist/client-browser.d.mts +8 -0
- package/dist/client-browser.d.ts +8 -0
- package/dist/client-browser.js +83 -0
- package/dist/client-browser.js.map +1 -0
- package/dist/client-browser.mjs +83 -0
- package/dist/client-browser.mjs.map +1 -0
- package/dist/client-node.d.mts +8 -0
- package/dist/client-node.d.ts +8 -0
- package/dist/client-node.js +90 -0
- package/dist/client-node.js.map +1 -0
- package/dist/client-node.mjs +90 -0
- package/dist/client-node.mjs.map +1 -0
- package/dist/evaluation/index.d.mts +897 -0
- package/dist/evaluation/index.d.ts +897 -0
- package/dist/evaluation/index.js +13 -0
- package/dist/evaluation/index.js.map +1 -0
- package/dist/evaluation/index.mjs +13 -0
- package/dist/evaluation/index.mjs.map +1 -0
- package/dist/filterable-batch-span-processor-zO5kcjBY.d.mts +64 -0
- package/dist/filterable-batch-span-processor-zO5kcjBY.d.ts +64 -0
- package/dist/index.d.mts +45 -1027
- package/dist/index.d.ts +45 -1027
- package/dist/index.js +11 -27291
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +23 -956
- package/dist/index.mjs.map +1 -1
- package/dist/observability/index.d.mts +260 -0
- package/dist/observability/index.d.ts +260 -0
- package/dist/observability/index.js +20 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/observability/index.mjs +20 -0
- package/dist/observability/index.mjs.map +1 -0
- package/dist/observability/instrumentation/langchain/index.d.mts +40 -0
- package/dist/observability/instrumentation/langchain/index.d.ts +40 -0
- package/dist/observability/instrumentation/langchain/index.js +666 -0
- package/dist/observability/instrumentation/langchain/index.js.map +1 -0
- package/dist/observability/instrumentation/langchain/index.mjs +666 -0
- package/dist/observability/instrumentation/langchain/index.mjs.map +1 -0
- package/dist/prompt/index.d.mts +10 -0
- package/dist/prompt/index.d.ts +10 -0
- package/dist/prompt/index.js +18 -0
- package/dist/prompt/index.js.map +1 -0
- package/dist/prompt/index.mjs +18 -0
- package/dist/prompt/index.mjs.map +1 -0
- package/dist/prompt-BXJWdbQp.d.mts +1967 -0
- package/dist/prompt-BXJWdbQp.d.ts +1967 -0
- package/dist/record-evaluation-CmxMXa-3.d.mts +25 -0
- package/dist/record-evaluation-CmxMXa-3.d.ts +25 -0
- package/dist/trace-D-bZOuqb.d.mts +622 -0
- package/dist/trace-G2312klE.d.ts +622 -0
- package/package.json +86 -37
- package/.eslintrc.cjs +0 -37
- package/copy-types.sh +0 -17
- package/dist/chunk-LKD2K67J.mjs +0 -717
- package/dist/chunk-LKD2K67J.mjs.map +0 -1
- package/dist/utils-Cv-rUjJ1.d.mts +0 -313
- package/dist/utils-Cv-rUjJ1.d.ts +0 -313
- package/dist/utils.d.mts +0 -2
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -709
- package/dist/utils.js.map +0 -1
- package/dist/utils.mjs +0 -11
- package/example/.env.example +0 -12
- package/example/.eslintrc.json +0 -26
- package/example/LICENSE +0 -13
- package/example/README.md +0 -12
- package/example/app/(chat)/chat/[id]/page.tsx +0 -60
- package/example/app/(chat)/layout.tsx +0 -14
- package/example/app/(chat)/page.tsx +0 -27
- package/example/app/actions.ts +0 -156
- package/example/app/globals.css +0 -76
- package/example/app/guardrails/page.tsx +0 -26
- package/example/app/langchain/page.tsx +0 -27
- package/example/app/langchain-rag/page.tsx +0 -28
- package/example/app/late-update/page.tsx +0 -27
- package/example/app/layout.tsx +0 -64
- package/example/app/login/actions.ts +0 -71
- package/example/app/login/page.tsx +0 -18
- package/example/app/manual/page.tsx +0 -27
- package/example/app/new/page.tsx +0 -5
- package/example/app/opengraph-image.png +0 -0
- package/example/app/share/[id]/page.tsx +0 -58
- package/example/app/signup/actions.ts +0 -111
- package/example/app/signup/page.tsx +0 -18
- package/example/app/twitter-image.png +0 -0
- package/example/auth.config.ts +0 -42
- package/example/auth.ts +0 -45
- package/example/components/button-scroll-to-bottom.tsx +0 -36
- package/example/components/chat-history.tsx +0 -49
- package/example/components/chat-list.tsx +0 -52
- package/example/components/chat-message-actions.tsx +0 -40
- package/example/components/chat-message.tsx +0 -80
- package/example/components/chat-panel.tsx +0 -139
- package/example/components/chat-share-dialog.tsx +0 -95
- package/example/components/chat.tsx +0 -84
- package/example/components/clear-history.tsx +0 -75
- package/example/components/empty-screen.tsx +0 -38
- package/example/components/external-link.tsx +0 -29
- package/example/components/footer.tsx +0 -19
- package/example/components/header.tsx +0 -114
- package/example/components/login-button.tsx +0 -42
- package/example/components/login-form.tsx +0 -97
- package/example/components/markdown.tsx +0 -9
- package/example/components/prompt-form.tsx +0 -115
- package/example/components/providers.tsx +0 -17
- package/example/components/sidebar-actions.tsx +0 -125
- package/example/components/sidebar-desktop.tsx +0 -19
- package/example/components/sidebar-footer.tsx +0 -16
- package/example/components/sidebar-item.tsx +0 -124
- package/example/components/sidebar-items.tsx +0 -42
- package/example/components/sidebar-list.tsx +0 -38
- package/example/components/sidebar-mobile.tsx +0 -31
- package/example/components/sidebar-toggle.tsx +0 -24
- package/example/components/sidebar.tsx +0 -21
- package/example/components/signup-form.tsx +0 -95
- package/example/components/stocks/events-skeleton.tsx +0 -31
- package/example/components/stocks/events.tsx +0 -30
- package/example/components/stocks/index.tsx +0 -36
- package/example/components/stocks/message.tsx +0 -134
- package/example/components/stocks/spinner.tsx +0 -16
- package/example/components/stocks/stock-purchase.tsx +0 -146
- package/example/components/stocks/stock-skeleton.tsx +0 -22
- package/example/components/stocks/stock.tsx +0 -210
- package/example/components/stocks/stocks-skeleton.tsx +0 -9
- package/example/components/stocks/stocks.tsx +0 -67
- package/example/components/tailwind-indicator.tsx +0 -14
- package/example/components/theme-toggle.tsx +0 -31
- package/example/components/ui/alert-dialog.tsx +0 -141
- package/example/components/ui/badge.tsx +0 -36
- package/example/components/ui/button.tsx +0 -57
- package/example/components/ui/codeblock.tsx +0 -148
- package/example/components/ui/dialog.tsx +0 -122
- package/example/components/ui/dropdown-menu.tsx +0 -205
- package/example/components/ui/icons.tsx +0 -507
- package/example/components/ui/input.tsx +0 -25
- package/example/components/ui/label.tsx +0 -26
- package/example/components/ui/select.tsx +0 -164
- package/example/components/ui/separator.tsx +0 -31
- package/example/components/ui/sheet.tsx +0 -140
- package/example/components/ui/sonner.tsx +0 -31
- package/example/components/ui/switch.tsx +0 -29
- package/example/components/ui/textarea.tsx +0 -24
- package/example/components/ui/tooltip.tsx +0 -30
- package/example/components/user-menu.tsx +0 -53
- package/example/components.json +0 -17
- package/example/instrumentation.ts +0 -11
- package/example/lib/chat/guardrails.tsx +0 -181
- package/example/lib/chat/langchain-rag.tsx +0 -191
- package/example/lib/chat/langchain.tsx +0 -112
- package/example/lib/chat/late-update.tsx +0 -208
- package/example/lib/chat/manual.tsx +0 -605
- package/example/lib/chat/vercel-ai.tsx +0 -576
- package/example/lib/hooks/use-copy-to-clipboard.tsx +0 -33
- package/example/lib/hooks/use-enter-submit.tsx +0 -23
- package/example/lib/hooks/use-local-storage.ts +0 -24
- package/example/lib/hooks/use-scroll-anchor.tsx +0 -86
- package/example/lib/hooks/use-sidebar.tsx +0 -60
- package/example/lib/hooks/use-streamable-text.ts +0 -25
- package/example/lib/types.ts +0 -41
- package/example/lib/utils.ts +0 -89
- package/example/middleware.ts +0 -8
- package/example/next-env.d.ts +0 -5
- package/example/next.config.js +0 -16
- package/example/package-lock.json +0 -10917
- package/example/package.json +0 -84
- package/example/pnpm-lock.yaml +0 -5712
- package/example/postcss.config.js +0 -6
- package/example/prettier.config.cjs +0 -34
- package/example/public/apple-touch-icon.png +0 -0
- package/example/public/favicon-16x16.png +0 -0
- package/example/public/favicon.ico +0 -0
- package/example/public/next.svg +0 -1
- package/example/public/thirteen.svg +0 -1
- package/example/public/vercel.svg +0 -1
- package/example/tailwind.config.ts +0 -81
- package/example/tsconfig.json +0 -35
- package/src/LangWatchExporter.ts +0 -96
- package/src/evaluations.ts +0 -219
- package/src/index.test.ts +0 -402
- package/src/index.ts +0 -596
- package/src/langchain.ts +0 -557
- package/src/server/types/.gitkeep +0 -0
- package/src/typeUtils.ts +0 -89
- package/src/types.ts +0 -82
- package/src/utils.ts +0 -205
- package/ts-to-zod.config.js +0 -24
- package/tsconfig.json +0 -32
- package/tsup.config.ts +0 -10
- package/vitest.config.ts +0 -8
- /package/dist/{utils.mjs.map → chunk-OM7VY3XT.mjs.map} +0 -0
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
import {
|
|
2
|
+
canAutomaticallyCaptureInput,
|
|
3
|
+
canAutomaticallyCaptureOutput
|
|
4
|
+
} from "../../../chunk-QEWDG5QE.mjs";
|
|
5
|
+
import {
|
|
6
|
+
getLangWatchTracer
|
|
7
|
+
} from "../../../chunk-PMBEK6YE.mjs";
|
|
8
|
+
import {
|
|
9
|
+
ATTR_LANGWATCH_LANGCHAIN_EVENT_NAME,
|
|
10
|
+
ATTR_LANGWATCH_LANGCHAIN_RUN_EXTRA_PARAMS,
|
|
11
|
+
ATTR_LANGWATCH_LANGCHAIN_RUN_ID,
|
|
12
|
+
ATTR_LANGWATCH_LANGCHAIN_RUN_METADATA,
|
|
13
|
+
ATTR_LANGWATCH_LANGCHAIN_RUN_PARENT_ID,
|
|
14
|
+
ATTR_LANGWATCH_LANGCHAIN_RUN_TAGS,
|
|
15
|
+
ATTR_LANGWATCH_LANGCHAIN_RUN_TYPE,
|
|
16
|
+
ATTR_LANGWATCH_LANGCHAIN_TAGS,
|
|
17
|
+
EVNT_LANGWATCH_LANGCHAIN_CALLBACK,
|
|
18
|
+
__spreadValues
|
|
19
|
+
} from "../../../chunk-4BZATFKJ.mjs";
|
|
20
|
+
|
|
21
|
+
// src/observability/instrumentation/langchain/index.ts
|
|
22
|
+
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
23
|
+
import {
|
|
24
|
+
AIMessage,
|
|
25
|
+
AIMessageChunk,
|
|
26
|
+
FunctionMessage,
|
|
27
|
+
FunctionMessageChunk,
|
|
28
|
+
HumanMessage,
|
|
29
|
+
HumanMessageChunk,
|
|
30
|
+
SystemMessage,
|
|
31
|
+
SystemMessageChunk,
|
|
32
|
+
ToolMessage,
|
|
33
|
+
ToolMessageChunk,
|
|
34
|
+
mapChatMessagesToStoredMessages
|
|
35
|
+
} from "@langchain/core/messages";
|
|
36
|
+
import { context, trace, SpanStatusCode } from "@opentelemetry/api";
|
|
37
|
+
|
|
38
|
+
// src/internal/generated/types/tracer.generated.ts
|
|
39
|
+
import { z } from "zod";
|
|
40
|
+
var chatRoleSchema = z.union([
|
|
41
|
+
z.literal("system"),
|
|
42
|
+
z.literal("user"),
|
|
43
|
+
z.literal("assistant"),
|
|
44
|
+
z.literal("function"),
|
|
45
|
+
z.literal("tool"),
|
|
46
|
+
z.literal("unknown")
|
|
47
|
+
]);
|
|
48
|
+
var functionCallSchema = z.object({
|
|
49
|
+
name: z.string().optional(),
|
|
50
|
+
arguments: z.string().optional()
|
|
51
|
+
});
|
|
52
|
+
var toolCallSchema = z.object({
|
|
53
|
+
id: z.string(),
|
|
54
|
+
type: z.string(),
|
|
55
|
+
function: functionCallSchema
|
|
56
|
+
});
|
|
57
|
+
var rAGChunkSchema = z.object({
|
|
58
|
+
document_id: z.string().optional().nullable(),
|
|
59
|
+
chunk_id: z.string().optional().nullable(),
|
|
60
|
+
content: z.union([z.string(), z.record(z.string(), z.any()), z.array(z.any())])
|
|
61
|
+
});
|
|
62
|
+
var chatRichContentSchema = z.union([
|
|
63
|
+
z.object({
|
|
64
|
+
type: z.literal("text"),
|
|
65
|
+
text: z.string().optional()
|
|
66
|
+
}),
|
|
67
|
+
z.object({
|
|
68
|
+
text: z.string()
|
|
69
|
+
}),
|
|
70
|
+
z.object({
|
|
71
|
+
type: z.literal("image_url"),
|
|
72
|
+
image_url: z.object({
|
|
73
|
+
url: z.string(),
|
|
74
|
+
detail: z.union([z.literal("auto"), z.literal("low"), z.literal("high")]).optional()
|
|
75
|
+
}).optional()
|
|
76
|
+
}),
|
|
77
|
+
z.object({
|
|
78
|
+
type: z.literal("tool_call"),
|
|
79
|
+
toolName: z.string().optional(),
|
|
80
|
+
toolCallId: z.string().optional(),
|
|
81
|
+
args: z.string().optional()
|
|
82
|
+
}),
|
|
83
|
+
z.object({
|
|
84
|
+
type: z.literal("tool_result"),
|
|
85
|
+
toolName: z.string().optional(),
|
|
86
|
+
toolCallId: z.string().optional(),
|
|
87
|
+
result: z.any().optional()
|
|
88
|
+
})
|
|
89
|
+
]);
|
|
90
|
+
var chatMessageSchema = z.object({
|
|
91
|
+
role: chatRoleSchema.optional(),
|
|
92
|
+
content: z.union([z.string(), z.array(chatRichContentSchema)]).optional().nullable(),
|
|
93
|
+
function_call: functionCallSchema.optional().nullable(),
|
|
94
|
+
tool_calls: z.array(toolCallSchema).optional().nullable(),
|
|
95
|
+
tool_call_id: z.string().optional().nullable(),
|
|
96
|
+
name: z.string().optional().nullable()
|
|
97
|
+
});
|
|
98
|
+
var typedValueTextSchema = z.object({
|
|
99
|
+
type: z.literal("text"),
|
|
100
|
+
value: z.string()
|
|
101
|
+
});
|
|
102
|
+
var typedValueRawSchema = z.object({
|
|
103
|
+
type: z.literal("raw"),
|
|
104
|
+
value: z.string()
|
|
105
|
+
});
|
|
106
|
+
var jSONSerializableSchema = z.union([
|
|
107
|
+
z.string(),
|
|
108
|
+
z.number(),
|
|
109
|
+
z.boolean(),
|
|
110
|
+
z.record(z.string(), z.any()),
|
|
111
|
+
z.array(z.any())
|
|
112
|
+
]).nullable();
|
|
113
|
+
var typedValueJsonSchema = z.object({
|
|
114
|
+
type: z.literal("json"),
|
|
115
|
+
value: jSONSerializableSchema
|
|
116
|
+
});
|
|
117
|
+
var moneySchema = z.object({
|
|
118
|
+
currency: z.string(),
|
|
119
|
+
amount: z.number()
|
|
120
|
+
});
|
|
121
|
+
var evaluationResultSchema = z.object({
|
|
122
|
+
status: z.union([
|
|
123
|
+
z.literal("processed"),
|
|
124
|
+
z.literal("skipped"),
|
|
125
|
+
z.literal("error")
|
|
126
|
+
]),
|
|
127
|
+
passed: z.boolean().optional().nullable(),
|
|
128
|
+
score: z.number().optional().nullable(),
|
|
129
|
+
label: z.string().optional().nullable(),
|
|
130
|
+
details: z.string().optional().nullable(),
|
|
131
|
+
cost: moneySchema.optional().nullable()
|
|
132
|
+
});
|
|
133
|
+
var typedValueGuardrailResultSchema = z.object({
|
|
134
|
+
type: z.literal("guardrail_result"),
|
|
135
|
+
value: evaluationResultSchema
|
|
136
|
+
});
|
|
137
|
+
var typedValueEvaluationResultSchema = z.object({
|
|
138
|
+
type: z.literal("evaluation_result"),
|
|
139
|
+
value: evaluationResultSchema
|
|
140
|
+
});
|
|
141
|
+
var typedValueChatMessagesSchema = z.object({
|
|
142
|
+
type: z.literal("chat_messages"),
|
|
143
|
+
value: z.array(chatMessageSchema)
|
|
144
|
+
});
|
|
145
|
+
var spanInputOutputSchema = z.lazy(
|
|
146
|
+
() => z.union([
|
|
147
|
+
typedValueTextSchema,
|
|
148
|
+
typedValueChatMessagesSchema,
|
|
149
|
+
typedValueGuardrailResultSchema,
|
|
150
|
+
typedValueEvaluationResultSchema,
|
|
151
|
+
typedValueJsonSchema,
|
|
152
|
+
typedValueRawSchema,
|
|
153
|
+
z.object({
|
|
154
|
+
type: z.literal("list"),
|
|
155
|
+
value: z.array(spanInputOutputSchema)
|
|
156
|
+
})
|
|
157
|
+
])
|
|
158
|
+
);
|
|
159
|
+
var errorCaptureSchema = z.object({
|
|
160
|
+
has_error: z.literal(true),
|
|
161
|
+
message: z.string(),
|
|
162
|
+
stacktrace: z.array(z.string())
|
|
163
|
+
});
|
|
164
|
+
var spanMetricsSchema = z.object({
|
|
165
|
+
prompt_tokens: z.number().optional().nullable(),
|
|
166
|
+
completion_tokens: z.number().optional().nullable(),
|
|
167
|
+
tokens_estimated: z.boolean().optional().nullable(),
|
|
168
|
+
cost: z.number().optional().nullable()
|
|
169
|
+
});
|
|
170
|
+
var spanTimestampsSchema = z.object({
|
|
171
|
+
ignore_timestamps_on_write: z.boolean().optional().nullable(),
|
|
172
|
+
started_at: z.number(),
|
|
173
|
+
first_token_at: z.number().optional().nullable(),
|
|
174
|
+
finished_at: z.number()
|
|
175
|
+
});
|
|
176
|
+
var spanTypesSchema = z.union([
|
|
177
|
+
z.literal("span"),
|
|
178
|
+
z.literal("llm"),
|
|
179
|
+
z.literal("chain"),
|
|
180
|
+
z.literal("tool"),
|
|
181
|
+
z.literal("agent"),
|
|
182
|
+
z.literal("rag"),
|
|
183
|
+
z.literal("guardrail"),
|
|
184
|
+
z.literal("evaluation"),
|
|
185
|
+
z.literal("workflow"),
|
|
186
|
+
z.literal("component"),
|
|
187
|
+
z.literal("module"),
|
|
188
|
+
z.literal("server"),
|
|
189
|
+
z.literal("client"),
|
|
190
|
+
z.literal("producer"),
|
|
191
|
+
z.literal("consumer"),
|
|
192
|
+
z.literal("task"),
|
|
193
|
+
z.literal("unknown")
|
|
194
|
+
]);
|
|
195
|
+
var spanInputOutputValidatorSchema = spanInputOutputSchema.and(
|
|
196
|
+
z.object({
|
|
197
|
+
value: z.any()
|
|
198
|
+
})
|
|
199
|
+
);
|
|
200
|
+
var traceInputSchema = z.object({
|
|
201
|
+
value: z.string(),
|
|
202
|
+
satisfaction_score: z.number().optional()
|
|
203
|
+
});
|
|
204
|
+
var traceOutputSchema = z.object({
|
|
205
|
+
value: z.string()
|
|
206
|
+
});
|
|
207
|
+
var primitiveTypeSchema = z.union([z.string(), z.number(), z.boolean(), z.undefined()]).nullable();
|
|
208
|
+
var reservedTraceMetadataSchema = z.object({
|
|
209
|
+
thread_id: z.string().optional().nullable(),
|
|
210
|
+
user_id: z.string().optional().nullable(),
|
|
211
|
+
customer_id: z.string().optional().nullable(),
|
|
212
|
+
labels: z.array(z.string()).optional().nullable(),
|
|
213
|
+
topic_id: z.string().optional().nullable(),
|
|
214
|
+
subtopic_id: z.string().optional().nullable(),
|
|
215
|
+
sdk_name: z.string().optional().nullable(),
|
|
216
|
+
sdk_version: z.string().optional().nullable(),
|
|
217
|
+
sdk_language: z.string().optional().nullable(),
|
|
218
|
+
telemetry_sdk_language: z.string().optional().nullable(),
|
|
219
|
+
telemetry_sdk_name: z.string().optional().nullable(),
|
|
220
|
+
telemetry_sdk_version: z.string().optional().nullable(),
|
|
221
|
+
prompt_ids: z.array(z.string()).optional().nullable(),
|
|
222
|
+
prompt_version_ids: z.array(z.string()).optional().nullable()
|
|
223
|
+
});
|
|
224
|
+
var evaluationStatusSchema = z.union([
|
|
225
|
+
z.literal("scheduled"),
|
|
226
|
+
z.literal("in_progress"),
|
|
227
|
+
z.literal("error"),
|
|
228
|
+
z.literal("skipped"),
|
|
229
|
+
z.literal("processed")
|
|
230
|
+
]);
|
|
231
|
+
var evaluationSchema = z.object({
|
|
232
|
+
evaluation_id: z.string(),
|
|
233
|
+
evaluator_id: z.string(),
|
|
234
|
+
span_id: z.string().optional().nullable(),
|
|
235
|
+
name: z.string(),
|
|
236
|
+
type: z.string().optional().nullable(),
|
|
237
|
+
is_guardrail: z.boolean().optional().nullable(),
|
|
238
|
+
status: evaluationStatusSchema,
|
|
239
|
+
passed: z.boolean().optional().nullable(),
|
|
240
|
+
score: z.number().optional().nullable(),
|
|
241
|
+
label: z.string().optional().nullable(),
|
|
242
|
+
details: z.string().optional().nullable(),
|
|
243
|
+
error: errorCaptureSchema.optional().nullable(),
|
|
244
|
+
retries: z.number().optional().nullable(),
|
|
245
|
+
timestamps: z.object({
|
|
246
|
+
ignore_timestamps_on_write: z.boolean().optional().nullable(),
|
|
247
|
+
inserted_at: z.number().optional().nullable(),
|
|
248
|
+
started_at: z.number().optional().nullable(),
|
|
249
|
+
finished_at: z.number().optional().nullable(),
|
|
250
|
+
updated_at: z.number().optional().nullable()
|
|
251
|
+
})
|
|
252
|
+
});
|
|
253
|
+
var rESTEvaluationSchema = evaluationSchema.omit({
|
|
254
|
+
evaluation_id: true,
|
|
255
|
+
evaluator_id: true,
|
|
256
|
+
status: true,
|
|
257
|
+
timestamps: true,
|
|
258
|
+
retries: true
|
|
259
|
+
}).and(
|
|
260
|
+
z.object({
|
|
261
|
+
evaluation_id: z.string().optional().nullable(),
|
|
262
|
+
evaluator_id: z.string().optional().nullable(),
|
|
263
|
+
status: z.union([
|
|
264
|
+
z.literal("processed"),
|
|
265
|
+
z.literal("skipped"),
|
|
266
|
+
z.literal("error")
|
|
267
|
+
]).optional().nullable(),
|
|
268
|
+
timestamps: z.object({
|
|
269
|
+
started_at: z.number().optional().nullable(),
|
|
270
|
+
finished_at: z.number().optional().nullable()
|
|
271
|
+
}).optional().nullable()
|
|
272
|
+
})
|
|
273
|
+
);
|
|
274
|
+
var contextsSchema = z.object({
|
|
275
|
+
traceId: z.string(),
|
|
276
|
+
contexts: z.array(rAGChunkSchema)
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// src/observability/instrumentation/langchain/index.ts
|
|
280
|
+
import { z as z2 } from "zod";
|
|
281
|
+
var LangWatchCallbackHandler = class extends BaseCallbackHandler {
|
|
282
|
+
constructor() {
|
|
283
|
+
super();
|
|
284
|
+
this.name = "LangWatchCallbackHandler";
|
|
285
|
+
this.tracer = getLangWatchTracer("langwatch.instrumentation.langchain");
|
|
286
|
+
this.spans = {};
|
|
287
|
+
}
|
|
288
|
+
getParentContext(parentRunId) {
|
|
289
|
+
if (parentRunId && this.spans[parentRunId]) {
|
|
290
|
+
return trace.setSpan(context.active(), this.spans[parentRunId]);
|
|
291
|
+
}
|
|
292
|
+
return context.active();
|
|
293
|
+
}
|
|
294
|
+
getSpan(runId) {
|
|
295
|
+
return this.spans[runId];
|
|
296
|
+
}
|
|
297
|
+
async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, _tags, metadata, name) {
|
|
298
|
+
var _a, _b, _c;
|
|
299
|
+
const parentContext = this.getParentContext(parentRunId);
|
|
300
|
+
const span = this.tracer.startSpan(
|
|
301
|
+
(_c = name != null ? name : (_b = (_a = llm.id) == null ? void 0 : _a[llm.id.length - 1]) == null ? void 0 : _b.toString()) != null ? _c : "llm",
|
|
302
|
+
{},
|
|
303
|
+
parentContext
|
|
304
|
+
);
|
|
305
|
+
span.setType("llm");
|
|
306
|
+
if (canAutomaticallyCaptureInput()) {
|
|
307
|
+
span.setInput(prompts);
|
|
308
|
+
}
|
|
309
|
+
if (_tags) {
|
|
310
|
+
span.setAttribute(ATTR_LANGWATCH_LANGCHAIN_RUN_TAGS, _tags);
|
|
311
|
+
}
|
|
312
|
+
if (extraParams) {
|
|
313
|
+
span.setAttributes(
|
|
314
|
+
Object.fromEntries(
|
|
315
|
+
Object.entries(extraParams).map(([key, value]) => [
|
|
316
|
+
[`${ATTR_LANGWATCH_LANGCHAIN_RUN_EXTRA_PARAMS}.${key}`],
|
|
317
|
+
wrapNonScalarValues(value)
|
|
318
|
+
])
|
|
319
|
+
)
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
if (metadata) {
|
|
323
|
+
if (metadata.ls_model_name) {
|
|
324
|
+
span.setRequestModel(metadata.ls_model_name);
|
|
325
|
+
metadata.ls_model_name = void 0;
|
|
326
|
+
}
|
|
327
|
+
span.setAttributes(
|
|
328
|
+
Object.fromEntries(
|
|
329
|
+
Object.entries(metadata).map(([key, value]) => [
|
|
330
|
+
[`${ATTR_LANGWATCH_LANGCHAIN_RUN_METADATA}.${key}`],
|
|
331
|
+
wrapNonScalarValues(value)
|
|
332
|
+
])
|
|
333
|
+
)
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
this.spans[runId] = span;
|
|
337
|
+
}
|
|
338
|
+
async handleChatModelStart(llm, messages, runId, parentRunId, extraParams, _tags, metadata, name) {
|
|
339
|
+
var _a, _b, _c;
|
|
340
|
+
const parentContext = this.getParentContext(parentRunId);
|
|
341
|
+
const span = this.tracer.startSpan(
|
|
342
|
+
(_c = name != null ? name : (_b = (_a = llm.id) == null ? void 0 : _a[llm.id.length - 1]) == null ? void 0 : _b.toString()) != null ? _c : "chat_model",
|
|
343
|
+
{},
|
|
344
|
+
parentContext
|
|
345
|
+
);
|
|
346
|
+
span.setType("llm");
|
|
347
|
+
if (canAutomaticallyCaptureInput()) {
|
|
348
|
+
span.setInput(messages.flatMap(convertFromLangChainMessages));
|
|
349
|
+
}
|
|
350
|
+
if (_tags) {
|
|
351
|
+
span.setAttribute(ATTR_LANGWATCH_LANGCHAIN_RUN_TAGS, _tags);
|
|
352
|
+
}
|
|
353
|
+
if (extraParams) {
|
|
354
|
+
span.setAttributes(
|
|
355
|
+
Object.fromEntries(
|
|
356
|
+
Object.entries(extraParams).map(([key, value]) => [
|
|
357
|
+
[`${ATTR_LANGWATCH_LANGCHAIN_RUN_EXTRA_PARAMS}.${key}`],
|
|
358
|
+
wrapNonScalarValues(value)
|
|
359
|
+
])
|
|
360
|
+
)
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
if (metadata) {
|
|
364
|
+
if (metadata.ls_model_name) {
|
|
365
|
+
span.setRequestModel(metadata.ls_model_name);
|
|
366
|
+
metadata.ls_model_name = void 0;
|
|
367
|
+
}
|
|
368
|
+
span.setAttributes(
|
|
369
|
+
Object.fromEntries(
|
|
370
|
+
Object.entries(metadata).map(([key, value]) => [
|
|
371
|
+
[`${ATTR_LANGWATCH_LANGCHAIN_RUN_METADATA}.${key}`],
|
|
372
|
+
wrapNonScalarValues(value)
|
|
373
|
+
])
|
|
374
|
+
)
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
this.spans[runId] = span;
|
|
378
|
+
}
|
|
379
|
+
async handleLLMEnd(response, runId, _parentRunId) {
|
|
380
|
+
const span = this.getSpan(runId);
|
|
381
|
+
if (!span) return;
|
|
382
|
+
const outputs = [];
|
|
383
|
+
for (const generation of response.generations) {
|
|
384
|
+
for (const generation_ of generation) {
|
|
385
|
+
if ("message" in generation_ && generation_.message) {
|
|
386
|
+
outputs.push(
|
|
387
|
+
convertFromLangChainMessages([
|
|
388
|
+
generation_.message
|
|
389
|
+
])
|
|
390
|
+
);
|
|
391
|
+
} else if ("text" in generation_) {
|
|
392
|
+
outputs.push(generation_.text);
|
|
393
|
+
} else {
|
|
394
|
+
outputs.push(JSON.stringify(generation_));
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const output = outputs.length === 1 ? outputs[0] : outputs;
|
|
399
|
+
if (canAutomaticallyCaptureOutput()) {
|
|
400
|
+
span.setOutput(output);
|
|
401
|
+
}
|
|
402
|
+
addLangChainEvent(span, "handleLLMEnd", runId, _parentRunId);
|
|
403
|
+
span.end();
|
|
404
|
+
this.spans[runId] = void 0;
|
|
405
|
+
}
|
|
406
|
+
async handleLLMError(err, runId, _parentRunId) {
|
|
407
|
+
const span = this.getSpan(runId);
|
|
408
|
+
if (!span) return;
|
|
409
|
+
addLangChainEvent(span, "handleLLMError", runId, _parentRunId);
|
|
410
|
+
span.recordException(err);
|
|
411
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
|
|
412
|
+
span.end();
|
|
413
|
+
this.spans[runId] = void 0;
|
|
414
|
+
}
|
|
415
|
+
async handleChainStart(chain, inputs, runId, parentRunId, _tags, _metadata, _runType, name) {
|
|
416
|
+
var _a, _b, _c;
|
|
417
|
+
const parentContext = this.getParentContext(parentRunId);
|
|
418
|
+
const span = this.tracer.startSpan(
|
|
419
|
+
(_c = name != null ? name : (_b = (_a = chain.id) == null ? void 0 : _a[chain.id.length - 1]) == null ? void 0 : _b.toString()) != null ? _c : "chain",
|
|
420
|
+
{},
|
|
421
|
+
parentContext
|
|
422
|
+
);
|
|
423
|
+
span.setType("chain");
|
|
424
|
+
if (canAutomaticallyCaptureInput()) {
|
|
425
|
+
span.setInput(inputs);
|
|
426
|
+
}
|
|
427
|
+
if (_tags) {
|
|
428
|
+
span.setAttribute(ATTR_LANGWATCH_LANGCHAIN_RUN_TAGS, _tags);
|
|
429
|
+
}
|
|
430
|
+
if (_metadata) {
|
|
431
|
+
span.setAttributes(
|
|
432
|
+
Object.fromEntries(
|
|
433
|
+
Object.entries(_metadata).map(([key, value]) => [
|
|
434
|
+
[`${ATTR_LANGWATCH_LANGCHAIN_RUN_METADATA}.${key}`],
|
|
435
|
+
wrapNonScalarValues(value)
|
|
436
|
+
])
|
|
437
|
+
)
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
if (_runType) {
|
|
441
|
+
span.setAttribute(ATTR_LANGWATCH_LANGCHAIN_RUN_TYPE, _runType);
|
|
442
|
+
}
|
|
443
|
+
this.spans[runId] = span;
|
|
444
|
+
}
|
|
445
|
+
async handleChainEnd(output, runId, _parentRunId) {
|
|
446
|
+
const span = this.getSpan(runId);
|
|
447
|
+
if (!span) return;
|
|
448
|
+
addLangChainEvent(span, "handleChainEnd", runId, _parentRunId);
|
|
449
|
+
span.setOutput(output);
|
|
450
|
+
span.end();
|
|
451
|
+
this.spans[runId] = void 0;
|
|
452
|
+
}
|
|
453
|
+
async handleChainError(err, runId, _parentRunId, _tags, _kwargs) {
|
|
454
|
+
const span = this.getSpan(runId);
|
|
455
|
+
if (!span) return;
|
|
456
|
+
addLangChainEvent(span, "handleChainError", runId, _parentRunId);
|
|
457
|
+
span.recordException(err);
|
|
458
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
|
|
459
|
+
span.end();
|
|
460
|
+
this.spans[runId] = void 0;
|
|
461
|
+
}
|
|
462
|
+
async handleToolStart(tool, input, runId, parentRunId, _tags, _metadata, name) {
|
|
463
|
+
var _a, _b, _c;
|
|
464
|
+
const parentContext = this.getParentContext(parentRunId);
|
|
465
|
+
const span = this.tracer.startSpan(
|
|
466
|
+
(_c = name != null ? name : (_b = (_a = tool.id) == null ? void 0 : _a[tool.id.length - 1]) == null ? void 0 : _b.toString()) != null ? _c : "tool",
|
|
467
|
+
{},
|
|
468
|
+
parentContext
|
|
469
|
+
);
|
|
470
|
+
span.setType("tool");
|
|
471
|
+
if (canAutomaticallyCaptureInput()) {
|
|
472
|
+
span.setInputString(input);
|
|
473
|
+
}
|
|
474
|
+
span.setAttributes({
|
|
475
|
+
[ATTR_LANGWATCH_LANGCHAIN_RUN_ID]: runId,
|
|
476
|
+
[ATTR_LANGWATCH_LANGCHAIN_RUN_PARENT_ID]: parentRunId
|
|
477
|
+
});
|
|
478
|
+
if (_tags) {
|
|
479
|
+
span.setAttribute(ATTR_LANGWATCH_LANGCHAIN_RUN_TAGS, _tags);
|
|
480
|
+
}
|
|
481
|
+
if (_metadata) {
|
|
482
|
+
span.setAttributes(
|
|
483
|
+
Object.fromEntries(
|
|
484
|
+
Object.entries(_metadata).map(([key, value]) => [
|
|
485
|
+
[`${ATTR_LANGWATCH_LANGCHAIN_RUN_METADATA}.${key}`],
|
|
486
|
+
wrapNonScalarValues(value)
|
|
487
|
+
])
|
|
488
|
+
)
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
this.spans[runId] = span;
|
|
492
|
+
}
|
|
493
|
+
async handleToolEnd(output, runId, _parentRunId) {
|
|
494
|
+
const span = this.getSpan(runId);
|
|
495
|
+
if (!span) return;
|
|
496
|
+
if (canAutomaticallyCaptureOutput()) {
|
|
497
|
+
span.setOutputString(output);
|
|
498
|
+
}
|
|
499
|
+
addLangChainEvent(span, "handleToolEnd", runId, _parentRunId);
|
|
500
|
+
span.end();
|
|
501
|
+
this.spans[runId] = void 0;
|
|
502
|
+
}
|
|
503
|
+
async handleToolError(err, runId, _parentRunId, _tags) {
|
|
504
|
+
const span = this.getSpan(runId);
|
|
505
|
+
if (!span) return;
|
|
506
|
+
addLangChainEvent(span, "handleToolError", runId, _parentRunId, _tags);
|
|
507
|
+
span.recordException(err);
|
|
508
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
|
|
509
|
+
span.end();
|
|
510
|
+
this.spans[runId] = void 0;
|
|
511
|
+
}
|
|
512
|
+
async handleRetrieverStart(retriever, query, runId, parentRunId, _tags, _metadata, name) {
|
|
513
|
+
var _a, _b, _c;
|
|
514
|
+
const parentContext = this.getParentContext(parentRunId);
|
|
515
|
+
const span = this.tracer.startSpan(
|
|
516
|
+
(_c = name != null ? name : (_b = (_a = retriever.id) == null ? void 0 : _a[retriever.id.length - 1]) == null ? void 0 : _b.toString()) != null ? _c : "retriever",
|
|
517
|
+
{},
|
|
518
|
+
parentContext
|
|
519
|
+
);
|
|
520
|
+
span.setType("rag");
|
|
521
|
+
if (canAutomaticallyCaptureInput()) {
|
|
522
|
+
span.setInputString(query);
|
|
523
|
+
}
|
|
524
|
+
if (_tags) {
|
|
525
|
+
span.setAttribute(ATTR_LANGWATCH_LANGCHAIN_RUN_TAGS, _tags);
|
|
526
|
+
}
|
|
527
|
+
if (_metadata) {
|
|
528
|
+
span.setAttributes(
|
|
529
|
+
Object.fromEntries(
|
|
530
|
+
Object.entries(_metadata).map(([key, value]) => [
|
|
531
|
+
[`${ATTR_LANGWATCH_LANGCHAIN_RUN_METADATA}.${key}`],
|
|
532
|
+
wrapNonScalarValues(value)
|
|
533
|
+
])
|
|
534
|
+
)
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
span.setAttributes({
|
|
538
|
+
[ATTR_LANGWATCH_LANGCHAIN_RUN_ID]: runId,
|
|
539
|
+
[ATTR_LANGWATCH_LANGCHAIN_RUN_PARENT_ID]: parentRunId
|
|
540
|
+
});
|
|
541
|
+
this.spans[runId] = span;
|
|
542
|
+
}
|
|
543
|
+
async handleRetrieverEnd(documents, runId, _parentRunId, _tags) {
|
|
544
|
+
const span = this.getSpan(runId);
|
|
545
|
+
if (!span) return;
|
|
546
|
+
if (canAutomaticallyCaptureOutput()) {
|
|
547
|
+
span.setOutput(documents);
|
|
548
|
+
}
|
|
549
|
+
addLangChainEvent(span, "handleRetrieverEnd", runId, _parentRunId, _tags);
|
|
550
|
+
span.setRAGContexts(
|
|
551
|
+
documents.map((document) => ({
|
|
552
|
+
document_id: document.metadata.id,
|
|
553
|
+
chunk_id: document.metadata.chunk_id,
|
|
554
|
+
content: canAutomaticallyCaptureInput() ? document.pageContent : ""
|
|
555
|
+
}))
|
|
556
|
+
);
|
|
557
|
+
span.end();
|
|
558
|
+
this.spans[runId] = void 0;
|
|
559
|
+
}
|
|
560
|
+
async handleRetrieverError(err, runId, _parentRunId, _tags) {
|
|
561
|
+
const span = this.getSpan(runId);
|
|
562
|
+
if (!span) return;
|
|
563
|
+
addLangChainEvent(span, "handleRetrieverError", runId, _parentRunId, _tags);
|
|
564
|
+
span.recordException(err);
|
|
565
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: err.message });
|
|
566
|
+
span.end();
|
|
567
|
+
this.spans[runId] = void 0;
|
|
568
|
+
}
|
|
569
|
+
async handleAgentAction(_action, runId, _parentRunId, _tags) {
|
|
570
|
+
const span = this.getSpan(runId);
|
|
571
|
+
if (!span) return;
|
|
572
|
+
addLangChainEvent(span, "handleAgentAction", runId, _parentRunId, _tags);
|
|
573
|
+
span.setType("agent");
|
|
574
|
+
}
|
|
575
|
+
async handleAgentEnd(action, runId, _parentRunId, _tags) {
|
|
576
|
+
const span = this.getSpan(runId);
|
|
577
|
+
if (!span) return;
|
|
578
|
+
addLangChainEvent(span, "handleAgentEnd", runId, _parentRunId, _tags);
|
|
579
|
+
if (canAutomaticallyCaptureOutput()) {
|
|
580
|
+
span.setOutput(action.returnValues);
|
|
581
|
+
}
|
|
582
|
+
span.end();
|
|
583
|
+
this.spans[runId] = void 0;
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
var convertFromLangChainMessages = (messages) => {
|
|
587
|
+
const chatMessages = [];
|
|
588
|
+
for (const message of messages) {
|
|
589
|
+
chatMessages.push(
|
|
590
|
+
convertFromLangChainMessage(message)
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
return chatMessages;
|
|
594
|
+
};
|
|
595
|
+
var convertFromLangChainMessage = (message) => {
|
|
596
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
597
|
+
let role = "user";
|
|
598
|
+
const message_ = message.lc_serializable ? mapChatMessagesToStoredMessages([message])[0] : message;
|
|
599
|
+
if (message_ instanceof HumanMessage || message_ instanceof HumanMessageChunk || ((_a = message_.id) == null ? void 0 : _a[message_.id.length - 1]) === "HumanMessage" || ((_b = message_.id) == null ? void 0 : _b[message_.id.length - 1]) === "HumanMessageChunk" || message_.type === "human") {
|
|
600
|
+
role = "user";
|
|
601
|
+
} else if (message instanceof AIMessage || message instanceof AIMessageChunk || ((_c = message.id) == null ? void 0 : _c[message.id.length - 1]) === "AIMessage" || ((_d = message.id) == null ? void 0 : _d[message.id.length - 1]) === "AIMessageChunk" || message_.type === "ai") {
|
|
602
|
+
role = "assistant";
|
|
603
|
+
} else if (message instanceof SystemMessage || message instanceof SystemMessageChunk || ((_e = message.id) == null ? void 0 : _e[message.id.length - 1]) === "SystemMessage" || ((_f = message.id) == null ? void 0 : _f[message.id.length - 1]) === "SystemMessageChunk" || message_.type === "system") {
|
|
604
|
+
role = "system";
|
|
605
|
+
} else if (message instanceof FunctionMessage || message instanceof FunctionMessageChunk || ((_g = message.id) == null ? void 0 : _g[message.id.length - 1]) === "FunctionMessage" || ((_h = message.id) == null ? void 0 : _h[message.id.length - 1]) === "FunctionMessageChunk" || message_.type === "function") {
|
|
606
|
+
role = "function";
|
|
607
|
+
} else if (message instanceof ToolMessage || message instanceof ToolMessageChunk || ((_i = message.id) == null ? void 0 : _i[message.id.length - 1]) === "ToolMessage" || ((_j = message.id) == null ? void 0 : _j[message.id.length - 1]) === "ToolMessageChunk" || message_.type === "tool") {
|
|
608
|
+
role = "tool";
|
|
609
|
+
}
|
|
610
|
+
const content = typeof message.content === "string" ? message.content : message.content.map(
|
|
611
|
+
(content2) => content2.type === "text" ? { type: "text", text: content2.text } : content2.type == "image_url" ? { type: "image_url", image_url: content2.image_url } : { type: "text", text: JSON.stringify(content2) }
|
|
612
|
+
);
|
|
613
|
+
const functionCall = message.additional_kwargs;
|
|
614
|
+
return __spreadValues({
|
|
615
|
+
role,
|
|
616
|
+
content
|
|
617
|
+
}, functionCall && typeof functionCall === "object" && Object.keys(functionCall).length > 0 ? { function_call: functionCall } : {});
|
|
618
|
+
};
|
|
619
|
+
function wrapNonScalarValues(value) {
|
|
620
|
+
if (value === void 0) {
|
|
621
|
+
return void 0;
|
|
622
|
+
}
|
|
623
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
624
|
+
return value;
|
|
625
|
+
}
|
|
626
|
+
const chatMessages = z2.array(chatMessageSchema).safeParse(value);
|
|
627
|
+
if (Array.isArray(value) && chatMessages.success) {
|
|
628
|
+
return JSON.stringify({
|
|
629
|
+
type: "chat_messages",
|
|
630
|
+
value: chatMessages.data
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
try {
|
|
634
|
+
JSON.stringify(value);
|
|
635
|
+
return JSON.stringify({
|
|
636
|
+
type: "json",
|
|
637
|
+
value
|
|
638
|
+
});
|
|
639
|
+
} catch (e) {
|
|
640
|
+
return JSON.stringify({
|
|
641
|
+
type: "raw",
|
|
642
|
+
value
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
function addLangChainEvent(span, eventName, runId, parentRunId, tags, metadata, attributes) {
|
|
647
|
+
const attrs = __spreadValues({
|
|
648
|
+
[ATTR_LANGWATCH_LANGCHAIN_RUN_ID]: runId,
|
|
649
|
+
[ATTR_LANGWATCH_LANGCHAIN_RUN_PARENT_ID]: parentRunId,
|
|
650
|
+
[ATTR_LANGWATCH_LANGCHAIN_EVENT_NAME]: eventName
|
|
651
|
+
}, attributes);
|
|
652
|
+
if (tags) {
|
|
653
|
+
attrs[ATTR_LANGWATCH_LANGCHAIN_TAGS] = tags;
|
|
654
|
+
}
|
|
655
|
+
if (metadata) {
|
|
656
|
+
Object.entries(metadata).forEach(([key, value]) => {
|
|
657
|
+
attrs[key] = wrapNonScalarValues(value);
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
span.addEvent(EVNT_LANGWATCH_LANGCHAIN_CALLBACK, attrs);
|
|
661
|
+
}
|
|
662
|
+
export {
|
|
663
|
+
LangWatchCallbackHandler,
|
|
664
|
+
convertFromLangChainMessages
|
|
665
|
+
};
|
|
666
|
+
//# sourceMappingURL=index.mjs.map
|