langwatch 0.2.0 → 0.3.0-prerelease.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.
Files changed (235) hide show
  1. package/.editorconfig +16 -0
  2. package/LICENSE +7 -0
  3. package/README.md +268 -1
  4. package/copy-types.sh +19 -8
  5. package/examples/langchain/.env.example +2 -0
  6. package/examples/langchain/README.md +42 -0
  7. package/examples/langchain/package-lock.json +2930 -0
  8. package/examples/langchain/package.json +27 -0
  9. package/examples/langchain/src/cli-markdown.d.ts +137 -0
  10. package/examples/langchain/src/index.ts +109 -0
  11. package/examples/langchain/tsconfig.json +25 -0
  12. package/examples/langgraph/.env.example +2 -0
  13. package/examples/langgraph/README.md +42 -0
  14. package/examples/langgraph/package-lock.json +3031 -0
  15. package/examples/langgraph/package.json +28 -0
  16. package/examples/langgraph/src/cli-markdown.d.ts +137 -0
  17. package/examples/langgraph/src/index.ts +196 -0
  18. package/examples/langgraph/tsconfig.json +25 -0
  19. package/examples/mastra/.env.example +2 -0
  20. package/examples/mastra/README.md +57 -0
  21. package/examples/mastra/package-lock.json +5296 -0
  22. package/examples/mastra/package.json +32 -0
  23. package/examples/mastra/src/cli-markdown.d.ts +137 -0
  24. package/examples/mastra/src/index.ts +120 -0
  25. package/examples/mastra/src/mastra/agents/weather-agent.ts +30 -0
  26. package/examples/mastra/src/mastra/index.ts +21 -0
  27. package/examples/mastra/src/mastra/tools/weather-tool.ts +102 -0
  28. package/examples/mastra/tsconfig.json +25 -0
  29. package/examples/vercel-ai/.env.example +2 -0
  30. package/examples/vercel-ai/README.md +38 -0
  31. package/examples/vercel-ai/package-lock.json +2571 -0
  32. package/examples/vercel-ai/package.json +27 -0
  33. package/examples/vercel-ai/src/cli-markdown.d.ts +137 -0
  34. package/examples/vercel-ai/src/index.ts +110 -0
  35. package/examples/vercel-ai/src/instrumentation.ts +9 -0
  36. package/examples/vercel-ai/tsconfig.json +25 -0
  37. package/package.json +78 -34
  38. package/src/__tests__/client-browser.test.ts +92 -0
  39. package/src/__tests__/client-node.test.ts +76 -0
  40. package/src/__tests__/client.test.ts +71 -0
  41. package/src/__tests__/integration/client-browser.test.ts +46 -0
  42. package/src/__tests__/integration/client-node.test.ts +46 -0
  43. package/src/client-browser.ts +70 -0
  44. package/src/client-node.ts +82 -0
  45. package/src/client-shared.ts +72 -0
  46. package/src/client.ts +119 -0
  47. package/src/evaluation/__tests__/record-evaluation.test.ts +112 -0
  48. package/src/evaluation/__tests__/run-evaluation.test.ts +171 -0
  49. package/src/evaluation/index.ts +2 -0
  50. package/src/evaluation/record-evaluation.ts +101 -0
  51. package/src/evaluation/run-evaluation.ts +133 -0
  52. package/src/evaluation/tracer.ts +3 -0
  53. package/src/evaluation/types.ts +23 -0
  54. package/src/index.ts +10 -593
  55. package/src/internal/api/__tests__/errors.test.ts +98 -0
  56. package/src/internal/api/client.ts +30 -0
  57. package/src/internal/api/errors.ts +32 -0
  58. package/src/internal/generated/types/.gitkeep +0 -0
  59. package/src/observability/__tests__/integration/base.test.ts +74 -0
  60. package/src/observability/__tests__/integration/browser-setup-ordering.test.ts +60 -0
  61. package/src/observability/__tests__/integration/complex-nested-spans.test.ts +29 -0
  62. package/src/observability/__tests__/integration/error-handling.test.ts +24 -0
  63. package/src/observability/__tests__/integration/langwatch-disabled-otel.test.ts +24 -0
  64. package/src/observability/__tests__/integration/langwatch-first-then-vercel.test.ts +24 -0
  65. package/src/observability/__tests__/integration/multiple-setup-attempts.test.ts +27 -0
  66. package/src/observability/__tests__/integration/otel-ordering.test.ts +27 -0
  67. package/src/observability/__tests__/integration/vercel-configurations.test.ts +20 -0
  68. package/src/observability/__tests__/integration/vercel-first-then-langwatch.test.ts +27 -0
  69. package/src/observability/__tests__/span.test.ts +214 -0
  70. package/src/observability/__tests__/trace.test.ts +180 -0
  71. package/src/observability/exporters/index.ts +1 -0
  72. package/src/observability/exporters/langwatch-exporter.ts +53 -0
  73. package/src/observability/index.ts +4 -0
  74. package/src/observability/instrumentation/langchain/__tests__/integration/langchain-chatbot.test.ts +112 -0
  75. package/src/observability/instrumentation/langchain/__tests__/langchain.test.ts +284 -0
  76. package/src/observability/instrumentation/langchain/index.ts +624 -0
  77. package/src/observability/processors/__tests__/filterable-batch-span-exporter.test.ts +98 -0
  78. package/src/observability/processors/filterable-batch-span-processor.ts +99 -0
  79. package/src/observability/processors/index.ts +1 -0
  80. package/src/observability/semconv/attributes.ts +185 -0
  81. package/src/observability/semconv/events.ts +42 -0
  82. package/src/observability/semconv/index.ts +16 -0
  83. package/src/observability/semconv/values.ts +159 -0
  84. package/src/observability/span.ts +728 -0
  85. package/src/observability/trace.ts +301 -0
  86. package/src/prompt/__tests__/prompt.test.ts +139 -0
  87. package/src/prompt/get-prompt-version.ts +49 -0
  88. package/src/prompt/get-prompt.ts +44 -0
  89. package/src/prompt/index.ts +3 -0
  90. package/src/prompt/prompt.ts +133 -0
  91. package/src/prompt/service.ts +221 -0
  92. package/src/prompt/tracer.ts +3 -0
  93. package/src/prompt/types.ts +0 -0
  94. package/ts-to-zod.config.js +11 -0
  95. package/tsconfig.json +3 -9
  96. package/tsup.config.ts +11 -1
  97. package/vitest.config.ts +1 -0
  98. package/dist/chunk-LKD2K67J.mjs +0 -717
  99. package/dist/chunk-LKD2K67J.mjs.map +0 -1
  100. package/dist/index.d.mts +0 -1030
  101. package/dist/index.d.ts +0 -1030
  102. package/dist/index.js +0 -27310
  103. package/dist/index.js.map +0 -1
  104. package/dist/index.mjs +0 -963
  105. package/dist/index.mjs.map +0 -1
  106. package/dist/utils-Cv-rUjJ1.d.mts +0 -313
  107. package/dist/utils-Cv-rUjJ1.d.ts +0 -313
  108. package/dist/utils.d.mts +0 -2
  109. package/dist/utils.d.ts +0 -2
  110. package/dist/utils.js +0 -709
  111. package/dist/utils.js.map +0 -1
  112. package/dist/utils.mjs +0 -11
  113. package/dist/utils.mjs.map +0 -1
  114. package/example/.env.example +0 -12
  115. package/example/.eslintrc.json +0 -26
  116. package/example/LICENSE +0 -13
  117. package/example/README.md +0 -12
  118. package/example/app/(chat)/chat/[id]/page.tsx +0 -60
  119. package/example/app/(chat)/layout.tsx +0 -14
  120. package/example/app/(chat)/page.tsx +0 -27
  121. package/example/app/actions.ts +0 -156
  122. package/example/app/globals.css +0 -76
  123. package/example/app/guardrails/page.tsx +0 -26
  124. package/example/app/langchain/page.tsx +0 -27
  125. package/example/app/langchain-rag/page.tsx +0 -28
  126. package/example/app/late-update/page.tsx +0 -27
  127. package/example/app/layout.tsx +0 -64
  128. package/example/app/login/actions.ts +0 -71
  129. package/example/app/login/page.tsx +0 -18
  130. package/example/app/manual/page.tsx +0 -27
  131. package/example/app/new/page.tsx +0 -5
  132. package/example/app/opengraph-image.png +0 -0
  133. package/example/app/share/[id]/page.tsx +0 -58
  134. package/example/app/signup/actions.ts +0 -111
  135. package/example/app/signup/page.tsx +0 -18
  136. package/example/app/twitter-image.png +0 -0
  137. package/example/auth.config.ts +0 -42
  138. package/example/auth.ts +0 -45
  139. package/example/components/button-scroll-to-bottom.tsx +0 -36
  140. package/example/components/chat-history.tsx +0 -49
  141. package/example/components/chat-list.tsx +0 -52
  142. package/example/components/chat-message-actions.tsx +0 -40
  143. package/example/components/chat-message.tsx +0 -80
  144. package/example/components/chat-panel.tsx +0 -139
  145. package/example/components/chat-share-dialog.tsx +0 -95
  146. package/example/components/chat.tsx +0 -84
  147. package/example/components/clear-history.tsx +0 -75
  148. package/example/components/empty-screen.tsx +0 -38
  149. package/example/components/external-link.tsx +0 -29
  150. package/example/components/footer.tsx +0 -19
  151. package/example/components/header.tsx +0 -114
  152. package/example/components/login-button.tsx +0 -42
  153. package/example/components/login-form.tsx +0 -97
  154. package/example/components/markdown.tsx +0 -9
  155. package/example/components/prompt-form.tsx +0 -115
  156. package/example/components/providers.tsx +0 -17
  157. package/example/components/sidebar-actions.tsx +0 -125
  158. package/example/components/sidebar-desktop.tsx +0 -19
  159. package/example/components/sidebar-footer.tsx +0 -16
  160. package/example/components/sidebar-item.tsx +0 -124
  161. package/example/components/sidebar-items.tsx +0 -42
  162. package/example/components/sidebar-list.tsx +0 -38
  163. package/example/components/sidebar-mobile.tsx +0 -31
  164. package/example/components/sidebar-toggle.tsx +0 -24
  165. package/example/components/sidebar.tsx +0 -21
  166. package/example/components/signup-form.tsx +0 -95
  167. package/example/components/stocks/events-skeleton.tsx +0 -31
  168. package/example/components/stocks/events.tsx +0 -30
  169. package/example/components/stocks/index.tsx +0 -36
  170. package/example/components/stocks/message.tsx +0 -134
  171. package/example/components/stocks/spinner.tsx +0 -16
  172. package/example/components/stocks/stock-purchase.tsx +0 -146
  173. package/example/components/stocks/stock-skeleton.tsx +0 -22
  174. package/example/components/stocks/stock.tsx +0 -210
  175. package/example/components/stocks/stocks-skeleton.tsx +0 -9
  176. package/example/components/stocks/stocks.tsx +0 -67
  177. package/example/components/tailwind-indicator.tsx +0 -14
  178. package/example/components/theme-toggle.tsx +0 -31
  179. package/example/components/ui/alert-dialog.tsx +0 -141
  180. package/example/components/ui/badge.tsx +0 -36
  181. package/example/components/ui/button.tsx +0 -57
  182. package/example/components/ui/codeblock.tsx +0 -148
  183. package/example/components/ui/dialog.tsx +0 -122
  184. package/example/components/ui/dropdown-menu.tsx +0 -205
  185. package/example/components/ui/icons.tsx +0 -507
  186. package/example/components/ui/input.tsx +0 -25
  187. package/example/components/ui/label.tsx +0 -26
  188. package/example/components/ui/select.tsx +0 -164
  189. package/example/components/ui/separator.tsx +0 -31
  190. package/example/components/ui/sheet.tsx +0 -140
  191. package/example/components/ui/sonner.tsx +0 -31
  192. package/example/components/ui/switch.tsx +0 -29
  193. package/example/components/ui/textarea.tsx +0 -24
  194. package/example/components/ui/tooltip.tsx +0 -30
  195. package/example/components/user-menu.tsx +0 -53
  196. package/example/components.json +0 -17
  197. package/example/instrumentation.ts +0 -11
  198. package/example/lib/chat/guardrails.tsx +0 -181
  199. package/example/lib/chat/langchain-rag.tsx +0 -191
  200. package/example/lib/chat/langchain.tsx +0 -112
  201. package/example/lib/chat/late-update.tsx +0 -208
  202. package/example/lib/chat/manual.tsx +0 -605
  203. package/example/lib/chat/vercel-ai.tsx +0 -576
  204. package/example/lib/hooks/use-copy-to-clipboard.tsx +0 -33
  205. package/example/lib/hooks/use-enter-submit.tsx +0 -23
  206. package/example/lib/hooks/use-local-storage.ts +0 -24
  207. package/example/lib/hooks/use-scroll-anchor.tsx +0 -86
  208. package/example/lib/hooks/use-sidebar.tsx +0 -60
  209. package/example/lib/hooks/use-streamable-text.ts +0 -25
  210. package/example/lib/types.ts +0 -41
  211. package/example/lib/utils.ts +0 -89
  212. package/example/middleware.ts +0 -8
  213. package/example/next-env.d.ts +0 -5
  214. package/example/next.config.js +0 -16
  215. package/example/package-lock.json +0 -10917
  216. package/example/package.json +0 -84
  217. package/example/pnpm-lock.yaml +0 -5712
  218. package/example/postcss.config.js +0 -6
  219. package/example/prettier.config.cjs +0 -34
  220. package/example/public/apple-touch-icon.png +0 -0
  221. package/example/public/favicon-16x16.png +0 -0
  222. package/example/public/favicon.ico +0 -0
  223. package/example/public/next.svg +0 -1
  224. package/example/public/thirteen.svg +0 -1
  225. package/example/public/vercel.svg +0 -1
  226. package/example/tailwind.config.ts +0 -81
  227. package/example/tsconfig.json +0 -35
  228. package/src/LangWatchExporter.ts +0 -96
  229. package/src/evaluations.ts +0 -219
  230. package/src/index.test.ts +0 -402
  231. package/src/langchain.ts +0 -557
  232. package/src/typeUtils.ts +0 -89
  233. package/src/types.ts +0 -82
  234. package/src/utils.ts +0 -205
  235. /package/src/{server/types → internal/generated/openapi}/.gitkeep +0 -0
package/src/langchain.ts DELETED
@@ -1,557 +0,0 @@
1
- import type { AgentAction, AgentFinish } from "@langchain/core/agents";
2
- import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
3
- import { type DocumentInterface } from "@langchain/core/documents";
4
- import type { Serialized } from "@langchain/core/load/serializable";
5
- import {
6
- AIMessage,
7
- AIMessageChunk,
8
- FunctionMessage,
9
- FunctionMessageChunk,
10
- HumanMessage,
11
- HumanMessageChunk,
12
- SystemMessage,
13
- SystemMessageChunk,
14
- ToolMessage,
15
- ToolMessageChunk,
16
- mapChatMessagesToStoredMessages,
17
- type BaseMessage,
18
- type StoredMessage,
19
- } from "@langchain/core/messages";
20
- import type { ChatGeneration, LLMResult } from "@langchain/core/outputs";
21
- import type { ChainValues } from "@langchain/core/utils/types";
22
- import { stringify } from "javascript-stringify";
23
- import {
24
- type LangWatchRAGSpan,
25
- type LangWatchSpan,
26
- type LangWatchTrace,
27
- } from ".";
28
- import {
29
- type RAGSpan,
30
- type BaseSpan,
31
- type ChatMessage,
32
- type ChatRichContent,
33
- type SpanInputOutput,
34
- } from "./types";
35
-
36
- export class LangWatchCallbackHandler extends BaseCallbackHandler {
37
- name = "LangWatchCallbackHandler";
38
- trace: LangWatchTrace;
39
- spans: Record<string, LangWatchSpan> = {};
40
-
41
- constructor({ trace }: { trace: LangWatchTrace }) {
42
- super();
43
- this.trace = trace;
44
- }
45
-
46
- async handleLLMStart(
47
- llm: Serialized,
48
- prompts: string[],
49
- runId: string,
50
- parentRunId?: string | undefined,
51
- extraParams?: Record<string, unknown> | undefined,
52
- _tags?: string[] | undefined,
53
- metadata?: Record<string, unknown> | undefined,
54
- name?: string
55
- ): Promise<void> {
56
- this.spans[runId] = this.buildLLMSpan({
57
- llm,
58
- runId,
59
- parentRunId,
60
- input: {
61
- type: "json",
62
- value: prompts,
63
- },
64
- extraParams,
65
- metadata,
66
- name,
67
- });
68
- }
69
-
70
- private buildLLMSpan({
71
- llm,
72
- runId,
73
- parentRunId,
74
- input,
75
- extraParams,
76
- metadata,
77
- name,
78
- }: {
79
- llm: Serialized;
80
- runId: string;
81
- parentRunId?: string | undefined;
82
- input: SpanInputOutput;
83
- extraParams?: Record<string, unknown> | undefined;
84
- metadata?: Record<string, unknown> | undefined;
85
- name?: string | undefined;
86
- }) {
87
- try {
88
- const parent = this.getParent(parentRunId);
89
-
90
- const vendor = metadata?.ls_provider ?? llm.id.at(-2)?.toString();
91
- const model =
92
- metadata?.ls_model_name ?? (llm as any).kwargs?.model ?? "unknown";
93
-
94
- const span = parent.startLLMSpan({
95
- spanId: runId,
96
- name: name ?? llm.id.at(-1)?.toString(),
97
- input,
98
- model: [vendor, model].filter((x) => x).join("/"),
99
- params: {
100
- temperature: (extraParams?.invocation_params as any)?.temperature,
101
- ...((extraParams?.invocation_params as any)?.functions
102
- ? { functions: (extraParams?.invocation_params as any)?.functions }
103
- : {}),
104
- },
105
- });
106
-
107
- return span;
108
- } catch (e) {
109
- this.trace.client.emit("error", e);
110
- throw e;
111
- }
112
- }
113
-
114
- async handleChatModelStart(
115
- llm: Serialized,
116
- messages: BaseMessage[][],
117
- runId: string,
118
- parentRunId?: string | undefined,
119
- extraParams?: Record<string, unknown> | undefined,
120
- tags?: string[] | undefined,
121
- metadata?: Record<string, unknown> | undefined,
122
- name?: string
123
- ): Promise<void> {
124
- this.spans[runId] = this.buildLLMSpan({
125
- name,
126
- llm,
127
- runId,
128
- parentRunId,
129
- input: {
130
- type: "chat_messages",
131
- value: messages.flatMap(convertFromLangChainMessages),
132
- },
133
- extraParams,
134
- metadata,
135
- });
136
- }
137
-
138
- async handleNewToken(_token: string, runId: string): Promise<void> {
139
- const span = this.spans[runId];
140
- if (runId && span && !span.timestamps.firstTokenAt) {
141
- span.update({
142
- timestamps: { ...span.timestamps, firstTokenAt: Date.now() },
143
- });
144
- }
145
- }
146
-
147
- async handleLLMEnd(
148
- response: LLMResult,
149
- runId: string,
150
- _parentRunId?: string | undefined
151
- ): Promise<void> {
152
- try {
153
- const span = this.spans[runId];
154
- if (!span) {
155
- return;
156
- }
157
-
158
- const outputs: SpanInputOutput[] = [];
159
- for (const generation of response.generations) {
160
- // TODO: again, why the twice loop? Can OpenAI generate multiple chat outputs?
161
- for (const generation_ of generation) {
162
- if ("message" in generation_) {
163
- outputs.push({
164
- type: "chat_messages",
165
- value: convertFromLangChainMessages([
166
- (generation_ as ChatGeneration).message,
167
- ]),
168
- });
169
- } else if ("text" in generation_) {
170
- outputs.push({
171
- type: "text",
172
- value: generation_.text,
173
- });
174
- } else {
175
- outputs.push({
176
- type: "text",
177
- value: JSON.stringify(generation_),
178
- });
179
- }
180
- }
181
- }
182
-
183
- const output: SpanInputOutput | undefined =
184
- outputs.length === 1
185
- ? outputs[0]
186
- : { type: "list", value: outputs as any };
187
-
188
- // Commenting it out because LangChain.js prompt and completion tokens is broken, this one doesn't work as it should with python,
189
- // and response_metadata.prompt and response_metadata.completion is there but it's always 0. Better let our server count.
190
- // const metrics = response.llmOutput?.token_usage
191
- // ? {
192
- // promptTokens: response.llmOutput.token_usage.prompt_tokens,
193
- // completionTokens: response.llmOutput.token_usage.completion_tokens,
194
- // }
195
- // : undefined;
196
-
197
- span.end({
198
- output,
199
- // ...(metrics ? { metrics } : {}),
200
- });
201
- } catch (e) {
202
- this.trace.client.emit("error", e);
203
- throw e;
204
- }
205
- }
206
-
207
- async handleLLMError(
208
- err: Error,
209
- runId: string,
210
- _parentRunId?: string | undefined
211
- ): Promise<void> {
212
- this.errorSpan({ runId, error: err });
213
- }
214
-
215
- async handleChainStart(
216
- chain: Serialized,
217
- inputs: ChainValues,
218
- runId: string,
219
- parentRunId?: string | undefined,
220
- _tags?: string[] | undefined,
221
- _metadata?: Record<string, unknown> | undefined,
222
- _runType?: string,
223
- name?: string
224
- ): Promise<void> {
225
- this.spans[runId] = this.buildSpan({
226
- type: "chain",
227
- serialized: chain,
228
- runId,
229
- parentRunId,
230
- input: inputs,
231
- name,
232
- });
233
- }
234
-
235
- async handleChainEnd(
236
- output: ChainValues,
237
- runId: string,
238
- _parentRunId?: string | undefined
239
- ): Promise<void> {
240
- this.endSpan({
241
- runId,
242
- output,
243
- });
244
- }
245
-
246
- async handleChainError(
247
- err: Error,
248
- runId: string,
249
- _parentRunId?: string | undefined,
250
- _tags?: string[] | undefined,
251
- _kwargs?: { inputs?: Record<string, unknown> | undefined } | undefined
252
- ): Promise<void> {
253
- this.errorSpan({ runId, error: err });
254
- }
255
-
256
- async handleToolStart(
257
- tool: Serialized,
258
- input: string,
259
- runId: string,
260
- parentRunId?: string | undefined,
261
- _tags?: string[] | undefined,
262
- _metadata?: Record<string, unknown> | undefined,
263
- name?: string
264
- ): Promise<void> {
265
- this.spans[runId] = this.buildSpan({
266
- type: "tool",
267
- serialized: tool,
268
- runId,
269
- parentRunId,
270
- input,
271
- name,
272
- });
273
- }
274
-
275
- async handleToolEnd(
276
- output: string,
277
- runId: string,
278
- _parentRunId?: string | undefined
279
- ): Promise<void> {
280
- this.endSpan({ runId, output });
281
- }
282
-
283
- async handleToolError(
284
- err: Error,
285
- runId: string,
286
- _parentRunId?: string | undefined,
287
- _tags?: string[] | undefined
288
- ): Promise<void> {
289
- this.errorSpan({ runId, error: err });
290
- }
291
-
292
- async handleRetrieverStart(
293
- retriever: Serialized,
294
- query: string,
295
- runId: string,
296
- parentRunId?: string | undefined,
297
- _tags?: string[] | undefined,
298
- _metadata?: Record<string, unknown> | undefined,
299
- name?: string | undefined
300
- ) {
301
- try {
302
- const parent = this.getParent(parentRunId);
303
-
304
- this.spans[runId] = parent.startRAGSpan({
305
- spanId: runId,
306
- name: name ?? retriever.name ?? retriever.id.at(-1)?.toString(),
307
- input: this.autoconvertTypedValues(query),
308
- });
309
- } catch (e) {
310
- this.trace.client.emit("error", e);
311
- throw e;
312
- }
313
- }
314
-
315
- async handleRetrieverEnd(
316
- documents: DocumentInterface<Record<string, any>>[],
317
- runId: string,
318
- _parentRunId?: string | undefined,
319
- _tags?: string[] | undefined
320
- ) {
321
- try {
322
- const contexts: RAGSpan["contexts"] = documents.map((doc) => ({
323
- content: doc.pageContent,
324
- ...(doc.metadata.source ? { documentId: doc.metadata.source } : {}),
325
- }));
326
-
327
- const span = this.spans[runId] as LangWatchRAGSpan;
328
- if (!span) {
329
- return;
330
- }
331
-
332
- span.end({
333
- contexts,
334
- output: this.autoconvertTypedValues(documents),
335
- });
336
- } catch (e) {
337
- this.trace.client.emit("error", e);
338
- throw e;
339
- }
340
- }
341
-
342
- async handleRetrieverError(
343
- err: Error,
344
- runId: string,
345
- _parentRunId?: string | undefined,
346
- _tags?: string[] | undefined
347
- ) {
348
- this.errorSpan({ runId, error: err });
349
- }
350
-
351
- async handleAgentAction(
352
- _action: AgentAction,
353
- runId: string,
354
- _parentRunId?: string | undefined,
355
- _tags?: string[] | undefined
356
- ): Promise<void> {
357
- const span = this.spans[runId];
358
- if (!span) {
359
- return;
360
- }
361
-
362
- span.update({
363
- type: "agent",
364
- });
365
- }
366
-
367
- async handleAgentEnd(
368
- action: AgentFinish,
369
- runId: string,
370
- _parentRunId?: string | undefined,
371
- _tags?: string[] | undefined
372
- ): Promise<void> {
373
- this.endSpan({
374
- runId,
375
- output: action.returnValues,
376
- });
377
- }
378
-
379
- private buildSpan({
380
- type,
381
- serialized,
382
- runId,
383
- parentRunId,
384
- input,
385
- name,
386
- }: {
387
- type: BaseSpan["type"];
388
- serialized: Serialized;
389
- runId: string;
390
- parentRunId?: string | undefined;
391
- input: unknown;
392
- name?: string | undefined;
393
- }) {
394
- try {
395
- const parent = this.getParent(parentRunId);
396
-
397
- const span = parent.startSpan({
398
- spanId: runId,
399
- type,
400
- name: name ?? serialized.name ?? serialized.id.at(-1)?.toString(),
401
- input: this.autoconvertTypedValues(input),
402
- });
403
-
404
- return span;
405
- } catch (e) {
406
- this.trace.client.emit("error", e);
407
- throw e;
408
- }
409
- }
410
-
411
- private endSpan({ runId, output }: { runId: string; output: unknown }): void {
412
- try {
413
- const span = this.spans[runId];
414
- if (!span) {
415
- return;
416
- }
417
-
418
- span.end({
419
- output: this.autoconvertTypedValues(output),
420
- });
421
- } catch (e) {
422
- this.trace.client.emit("error", e);
423
- throw e;
424
- }
425
- }
426
-
427
- private errorSpan({ runId, error }: { runId: string; error: Error }): void {
428
- const span = this.spans[runId];
429
- if (!span) {
430
- return;
431
- }
432
-
433
- span.end({
434
- error,
435
- });
436
- }
437
-
438
- private autoconvertTypedValues(value: any): SpanInputOutput | undefined {
439
- if (
440
- !value ||
441
- (typeof value === "object" && Object.keys(value).length === 0)
442
- ) {
443
- return undefined;
444
- }
445
- if (typeof value === "string") {
446
- return { type: "text", value };
447
- }
448
- try {
449
- JSON.stringify(value);
450
- return { type: "json", value };
451
- } catch (e) {
452
- return { type: "text", value: stringify(value) ?? value.toString() };
453
- }
454
- }
455
-
456
- private getParent(
457
- parentRunId?: string | undefined
458
- ): LangWatchSpan | LangWatchTrace {
459
- return (
460
- (parentRunId
461
- ? this.spans[parentRunId]
462
- : this.spans[Object.keys(this.spans).at(-1) ?? ""]) ?? this.trace
463
- );
464
- }
465
- }
466
-
467
- export const convertFromLangChainMessages = (
468
- messages: BaseMessage[]
469
- ): ChatMessage[] => {
470
- const chatMessages: ChatMessage[] = [];
471
- for (const message of messages) {
472
- chatMessages.push(convertFromLangChainMessage(message));
473
- }
474
- return chatMessages;
475
- };
476
-
477
- const convertFromLangChainMessage = (
478
- message: BaseMessage & { id?: string[] }
479
- ): ChatMessage => {
480
- let role: ChatMessage["role"] = "user";
481
-
482
- const message_: (BaseMessage | StoredMessage) & {
483
- id?: string[];
484
- type?: string;
485
- } = message.lc_serializable
486
- ? mapChatMessagesToStoredMessages([message])[0]!
487
- : message;
488
-
489
- // Dang this is so hard, langchain.js has 3 ways of representing the same thing...
490
- if (
491
- message_ instanceof HumanMessage ||
492
- message_ instanceof HumanMessageChunk ||
493
- message_.id?.at(-1) === "HumanMessage" ||
494
- message_.id?.at(-1) === "HumanMessageChunk" ||
495
- message_.type === "human"
496
- ) {
497
- role = "user";
498
- } else if (
499
- message instanceof AIMessage ||
500
- message instanceof AIMessageChunk ||
501
- message.id?.at(-1) === "AIMessage" ||
502
- message.id?.at(-1) === "AIMessageChunk" ||
503
- message_.type === "ai"
504
- ) {
505
- role = "assistant";
506
- } else if (
507
- message instanceof SystemMessage ||
508
- message instanceof SystemMessageChunk ||
509
- message.id?.at(-1) === "SystemMessage" ||
510
- message.id?.at(-1) === "SystemMessageChunk" ||
511
- message_.type === "system"
512
- ) {
513
- role = "system";
514
- } else if (
515
- message instanceof FunctionMessage ||
516
- message instanceof FunctionMessageChunk ||
517
- message.id?.at(-1) === "FunctionMessage" ||
518
- message.id?.at(-1) === "FunctionMessageChunk" ||
519
- message_.type === "function"
520
- ) {
521
- role = "function";
522
- } else if (
523
- message instanceof ToolMessage ||
524
- message instanceof ToolMessageChunk ||
525
- message.id?.at(-1) === "ToolMessage" ||
526
- message.id?.at(-1) === "ToolMessageChunk" ||
527
- message_.type === "tool"
528
- ) {
529
- role = "tool";
530
- }
531
-
532
- const content =
533
- typeof message.content === "string"
534
- ? message.content
535
- : message.content.map(
536
- (content): ChatRichContent =>
537
- content.type === "text"
538
- ? { type: "text", text: content.text }
539
- : content.type == "image_url"
540
- ? { type: "image_url", image_url: content.image_url }
541
- : { type: "text", text: JSON.stringify(content) }
542
- );
543
-
544
- const functionCall = message.additional_kwargs as
545
- | ChatMessage["function_call"]
546
- | undefined;
547
-
548
- return {
549
- role,
550
- content,
551
- ...(functionCall &&
552
- typeof functionCall === "object" &&
553
- Object.keys(functionCall).length > 0
554
- ? { function_call: functionCall }
555
- : {}),
556
- };
557
- };
package/src/typeUtils.ts DELETED
@@ -1,89 +0,0 @@
1
- import {
2
- reservedSpanParamsSchema,
3
- reservedTraceMetadataSchema,
4
- } from "./server/types/tracer.generated";
5
-
6
- export type Strict<T> = T & { [K in Exclude<keyof any, keyof T>]: never };
7
-
8
- type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}`
9
- ? `${T}${Capitalize<SnakeToCamelCase<U>>}`
10
- : S;
11
-
12
- export type SnakeToCamelCaseNested<T> = T extends object
13
- ? T extends (infer U)[]
14
- ? U extends object
15
- ? {
16
- [K in keyof U as SnakeToCamelCase<
17
- K & string
18
- >]: SnakeToCamelCaseNested<U[K]>;
19
- }[]
20
- : T
21
- : {
22
- [K in keyof T as SnakeToCamelCase<K & string>]: SnakeToCamelCaseNested<
23
- T[K]
24
- >;
25
- }
26
- : T;
27
-
28
- type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}`
29
- ? `${T extends Capitalize<T> ? "_" : ""}${Lowercase<T>}${CamelToSnakeCase<U>}`
30
- : S;
31
-
32
- export type CamelToSnakeCaseNested<T> = T extends object
33
- ? T extends (infer U)[]
34
- ? U extends object
35
- ? {
36
- [K in keyof U as CamelToSnakeCase<
37
- K & string
38
- >]: CamelToSnakeCaseNested<U[K]>;
39
- }[]
40
- : T
41
- : {
42
- [K in keyof T as CamelToSnakeCase<K & string>]: CamelToSnakeCaseNested<
43
- T[K]
44
- >;
45
- }
46
- : T;
47
-
48
- function camelToSnakeCase(str: string): string {
49
- return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
50
- }
51
-
52
- export function camelToSnakeCaseNested<T>(
53
- obj: T,
54
- parentKey?: string
55
- ): CamelToSnakeCaseNested<T> {
56
- if (Array.isArray(obj)) {
57
- return obj.map((item) =>
58
- camelToSnakeCaseNested(item, parentKey)
59
- ) as CamelToSnakeCaseNested<T>;
60
- } else if (typeof obj === "object" && obj !== null) {
61
- const newObj: any = {};
62
- for (const key in obj) {
63
- if (obj.hasOwnProperty(key)) {
64
- const newKey = camelToSnakeCase(key);
65
- // Keep arbitrary keys the same
66
- if (
67
- (parentKey === "metadata" &&
68
- !Object.keys(reservedTraceMetadataSchema.shape).includes(newKey)) ||
69
- (parentKey === "params" &&
70
- !Object.keys(reservedSpanParamsSchema.shape).includes(newKey)) ||
71
- (parentKey === "input" &&
72
- ["json", "raw", "list"].includes(newObj.type) &&
73
- newKey === "value") ||
74
- (parentKey === "output" &&
75
- ["json", "raw", "list"].includes(newObj.type) &&
76
- newKey === "value") ||
77
- (parentKey === "contexts" && newKey === "content")
78
- ) {
79
- newObj[key] = (obj as any)[key];
80
- } else {
81
- newObj[newKey] = camelToSnakeCaseNested((obj as any)[key], newKey);
82
- }
83
- }
84
- }
85
- return newObj as CamelToSnakeCaseNested<T>;
86
- } else {
87
- return obj as CamelToSnakeCaseNested<T>;
88
- }
89
- }