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.
Files changed (229) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +268 -1
  3. package/dist/chunk-4BZATFKJ.mjs +181 -0
  4. package/dist/chunk-4BZATFKJ.mjs.map +1 -0
  5. package/dist/chunk-CSC3CMIT.mjs +118 -0
  6. package/dist/chunk-CSC3CMIT.mjs.map +1 -0
  7. package/dist/chunk-F63YKTXA.mjs +47 -0
  8. package/dist/chunk-F63YKTXA.mjs.map +1 -0
  9. package/dist/chunk-G3AUABT7.js +4 -0
  10. package/dist/chunk-G3AUABT7.js.map +1 -0
  11. package/dist/chunk-HPC6Z7J4.js +118 -0
  12. package/dist/chunk-HPC6Z7J4.js.map +1 -0
  13. package/dist/chunk-KGDAENGD.js +50 -0
  14. package/dist/chunk-KGDAENGD.js.map +1 -0
  15. package/dist/chunk-LD74LVRU.js +47 -0
  16. package/dist/chunk-LD74LVRU.js.map +1 -0
  17. package/dist/chunk-OM7VY3XT.mjs +4 -0
  18. package/dist/chunk-PCQVQ7SB.js +45 -0
  19. package/dist/chunk-PCQVQ7SB.js.map +1 -0
  20. package/dist/chunk-PMBEK6YE.mjs +424 -0
  21. package/dist/chunk-PMBEK6YE.mjs.map +1 -0
  22. package/dist/chunk-PR3JDWC3.mjs +50 -0
  23. package/dist/chunk-PR3JDWC3.mjs.map +1 -0
  24. package/dist/chunk-PTJ6AAI7.js +360 -0
  25. package/dist/chunk-PTJ6AAI7.js.map +1 -0
  26. package/dist/chunk-QEWDG5QE.mjs +45 -0
  27. package/dist/chunk-QEWDG5QE.mjs.map +1 -0
  28. package/dist/chunk-REUCVT7A.mjs +39 -0
  29. package/dist/chunk-REUCVT7A.mjs.map +1 -0
  30. package/dist/chunk-SVJ7SCGB.js +424 -0
  31. package/dist/chunk-SVJ7SCGB.js.map +1 -0
  32. package/dist/chunk-VJSOCNPA.js +181 -0
  33. package/dist/chunk-VJSOCNPA.js.map +1 -0
  34. package/dist/chunk-WM2GRSRW.js +39 -0
  35. package/dist/chunk-WM2GRSRW.js.map +1 -0
  36. package/dist/chunk-Z5J5UI5E.mjs +360 -0
  37. package/dist/chunk-Z5J5UI5E.mjs.map +1 -0
  38. package/dist/client-B2HqIKg6.d.ts +51 -0
  39. package/dist/client-XyCqclCi.d.mts +51 -0
  40. package/dist/client-browser.d.mts +8 -0
  41. package/dist/client-browser.d.ts +8 -0
  42. package/dist/client-browser.js +83 -0
  43. package/dist/client-browser.js.map +1 -0
  44. package/dist/client-browser.mjs +83 -0
  45. package/dist/client-browser.mjs.map +1 -0
  46. package/dist/client-node.d.mts +8 -0
  47. package/dist/client-node.d.ts +8 -0
  48. package/dist/client-node.js +90 -0
  49. package/dist/client-node.js.map +1 -0
  50. package/dist/client-node.mjs +90 -0
  51. package/dist/client-node.mjs.map +1 -0
  52. package/dist/evaluation/index.d.mts +897 -0
  53. package/dist/evaluation/index.d.ts +897 -0
  54. package/dist/evaluation/index.js +13 -0
  55. package/dist/evaluation/index.js.map +1 -0
  56. package/dist/evaluation/index.mjs +13 -0
  57. package/dist/evaluation/index.mjs.map +1 -0
  58. package/dist/filterable-batch-span-processor-zO5kcjBY.d.mts +64 -0
  59. package/dist/filterable-batch-span-processor-zO5kcjBY.d.ts +64 -0
  60. package/dist/index.d.mts +45 -1027
  61. package/dist/index.d.ts +45 -1027
  62. package/dist/index.js +11 -27291
  63. package/dist/index.js.map +1 -1
  64. package/dist/index.mjs +23 -956
  65. package/dist/index.mjs.map +1 -1
  66. package/dist/observability/index.d.mts +260 -0
  67. package/dist/observability/index.d.ts +260 -0
  68. package/dist/observability/index.js +20 -0
  69. package/dist/observability/index.js.map +1 -0
  70. package/dist/observability/index.mjs +20 -0
  71. package/dist/observability/index.mjs.map +1 -0
  72. package/dist/observability/instrumentation/langchain/index.d.mts +40 -0
  73. package/dist/observability/instrumentation/langchain/index.d.ts +40 -0
  74. package/dist/observability/instrumentation/langchain/index.js +666 -0
  75. package/dist/observability/instrumentation/langchain/index.js.map +1 -0
  76. package/dist/observability/instrumentation/langchain/index.mjs +666 -0
  77. package/dist/observability/instrumentation/langchain/index.mjs.map +1 -0
  78. package/dist/prompt/index.d.mts +10 -0
  79. package/dist/prompt/index.d.ts +10 -0
  80. package/dist/prompt/index.js +18 -0
  81. package/dist/prompt/index.js.map +1 -0
  82. package/dist/prompt/index.mjs +18 -0
  83. package/dist/prompt/index.mjs.map +1 -0
  84. package/dist/prompt-BXJWdbQp.d.mts +1967 -0
  85. package/dist/prompt-BXJWdbQp.d.ts +1967 -0
  86. package/dist/record-evaluation-CmxMXa-3.d.mts +25 -0
  87. package/dist/record-evaluation-CmxMXa-3.d.ts +25 -0
  88. package/dist/trace-D-bZOuqb.d.mts +622 -0
  89. package/dist/trace-G2312klE.d.ts +622 -0
  90. package/package.json +86 -37
  91. package/.eslintrc.cjs +0 -37
  92. package/copy-types.sh +0 -17
  93. package/dist/chunk-LKD2K67J.mjs +0 -717
  94. package/dist/chunk-LKD2K67J.mjs.map +0 -1
  95. package/dist/utils-Cv-rUjJ1.d.mts +0 -313
  96. package/dist/utils-Cv-rUjJ1.d.ts +0 -313
  97. package/dist/utils.d.mts +0 -2
  98. package/dist/utils.d.ts +0 -2
  99. package/dist/utils.js +0 -709
  100. package/dist/utils.js.map +0 -1
  101. package/dist/utils.mjs +0 -11
  102. package/example/.env.example +0 -12
  103. package/example/.eslintrc.json +0 -26
  104. package/example/LICENSE +0 -13
  105. package/example/README.md +0 -12
  106. package/example/app/(chat)/chat/[id]/page.tsx +0 -60
  107. package/example/app/(chat)/layout.tsx +0 -14
  108. package/example/app/(chat)/page.tsx +0 -27
  109. package/example/app/actions.ts +0 -156
  110. package/example/app/globals.css +0 -76
  111. package/example/app/guardrails/page.tsx +0 -26
  112. package/example/app/langchain/page.tsx +0 -27
  113. package/example/app/langchain-rag/page.tsx +0 -28
  114. package/example/app/late-update/page.tsx +0 -27
  115. package/example/app/layout.tsx +0 -64
  116. package/example/app/login/actions.ts +0 -71
  117. package/example/app/login/page.tsx +0 -18
  118. package/example/app/manual/page.tsx +0 -27
  119. package/example/app/new/page.tsx +0 -5
  120. package/example/app/opengraph-image.png +0 -0
  121. package/example/app/share/[id]/page.tsx +0 -58
  122. package/example/app/signup/actions.ts +0 -111
  123. package/example/app/signup/page.tsx +0 -18
  124. package/example/app/twitter-image.png +0 -0
  125. package/example/auth.config.ts +0 -42
  126. package/example/auth.ts +0 -45
  127. package/example/components/button-scroll-to-bottom.tsx +0 -36
  128. package/example/components/chat-history.tsx +0 -49
  129. package/example/components/chat-list.tsx +0 -52
  130. package/example/components/chat-message-actions.tsx +0 -40
  131. package/example/components/chat-message.tsx +0 -80
  132. package/example/components/chat-panel.tsx +0 -139
  133. package/example/components/chat-share-dialog.tsx +0 -95
  134. package/example/components/chat.tsx +0 -84
  135. package/example/components/clear-history.tsx +0 -75
  136. package/example/components/empty-screen.tsx +0 -38
  137. package/example/components/external-link.tsx +0 -29
  138. package/example/components/footer.tsx +0 -19
  139. package/example/components/header.tsx +0 -114
  140. package/example/components/login-button.tsx +0 -42
  141. package/example/components/login-form.tsx +0 -97
  142. package/example/components/markdown.tsx +0 -9
  143. package/example/components/prompt-form.tsx +0 -115
  144. package/example/components/providers.tsx +0 -17
  145. package/example/components/sidebar-actions.tsx +0 -125
  146. package/example/components/sidebar-desktop.tsx +0 -19
  147. package/example/components/sidebar-footer.tsx +0 -16
  148. package/example/components/sidebar-item.tsx +0 -124
  149. package/example/components/sidebar-items.tsx +0 -42
  150. package/example/components/sidebar-list.tsx +0 -38
  151. package/example/components/sidebar-mobile.tsx +0 -31
  152. package/example/components/sidebar-toggle.tsx +0 -24
  153. package/example/components/sidebar.tsx +0 -21
  154. package/example/components/signup-form.tsx +0 -95
  155. package/example/components/stocks/events-skeleton.tsx +0 -31
  156. package/example/components/stocks/events.tsx +0 -30
  157. package/example/components/stocks/index.tsx +0 -36
  158. package/example/components/stocks/message.tsx +0 -134
  159. package/example/components/stocks/spinner.tsx +0 -16
  160. package/example/components/stocks/stock-purchase.tsx +0 -146
  161. package/example/components/stocks/stock-skeleton.tsx +0 -22
  162. package/example/components/stocks/stock.tsx +0 -210
  163. package/example/components/stocks/stocks-skeleton.tsx +0 -9
  164. package/example/components/stocks/stocks.tsx +0 -67
  165. package/example/components/tailwind-indicator.tsx +0 -14
  166. package/example/components/theme-toggle.tsx +0 -31
  167. package/example/components/ui/alert-dialog.tsx +0 -141
  168. package/example/components/ui/badge.tsx +0 -36
  169. package/example/components/ui/button.tsx +0 -57
  170. package/example/components/ui/codeblock.tsx +0 -148
  171. package/example/components/ui/dialog.tsx +0 -122
  172. package/example/components/ui/dropdown-menu.tsx +0 -205
  173. package/example/components/ui/icons.tsx +0 -507
  174. package/example/components/ui/input.tsx +0 -25
  175. package/example/components/ui/label.tsx +0 -26
  176. package/example/components/ui/select.tsx +0 -164
  177. package/example/components/ui/separator.tsx +0 -31
  178. package/example/components/ui/sheet.tsx +0 -140
  179. package/example/components/ui/sonner.tsx +0 -31
  180. package/example/components/ui/switch.tsx +0 -29
  181. package/example/components/ui/textarea.tsx +0 -24
  182. package/example/components/ui/tooltip.tsx +0 -30
  183. package/example/components/user-menu.tsx +0 -53
  184. package/example/components.json +0 -17
  185. package/example/instrumentation.ts +0 -11
  186. package/example/lib/chat/guardrails.tsx +0 -181
  187. package/example/lib/chat/langchain-rag.tsx +0 -191
  188. package/example/lib/chat/langchain.tsx +0 -112
  189. package/example/lib/chat/late-update.tsx +0 -208
  190. package/example/lib/chat/manual.tsx +0 -605
  191. package/example/lib/chat/vercel-ai.tsx +0 -576
  192. package/example/lib/hooks/use-copy-to-clipboard.tsx +0 -33
  193. package/example/lib/hooks/use-enter-submit.tsx +0 -23
  194. package/example/lib/hooks/use-local-storage.ts +0 -24
  195. package/example/lib/hooks/use-scroll-anchor.tsx +0 -86
  196. package/example/lib/hooks/use-sidebar.tsx +0 -60
  197. package/example/lib/hooks/use-streamable-text.ts +0 -25
  198. package/example/lib/types.ts +0 -41
  199. package/example/lib/utils.ts +0 -89
  200. package/example/middleware.ts +0 -8
  201. package/example/next-env.d.ts +0 -5
  202. package/example/next.config.js +0 -16
  203. package/example/package-lock.json +0 -10917
  204. package/example/package.json +0 -84
  205. package/example/pnpm-lock.yaml +0 -5712
  206. package/example/postcss.config.js +0 -6
  207. package/example/prettier.config.cjs +0 -34
  208. package/example/public/apple-touch-icon.png +0 -0
  209. package/example/public/favicon-16x16.png +0 -0
  210. package/example/public/favicon.ico +0 -0
  211. package/example/public/next.svg +0 -1
  212. package/example/public/thirteen.svg +0 -1
  213. package/example/public/vercel.svg +0 -1
  214. package/example/tailwind.config.ts +0 -81
  215. package/example/tsconfig.json +0 -35
  216. package/src/LangWatchExporter.ts +0 -96
  217. package/src/evaluations.ts +0 -219
  218. package/src/index.test.ts +0 -402
  219. package/src/index.ts +0 -596
  220. package/src/langchain.ts +0 -557
  221. package/src/server/types/.gitkeep +0 -0
  222. package/src/typeUtils.ts +0 -89
  223. package/src/types.ts +0 -82
  224. package/src/utils.ts +0 -205
  225. package/ts-to-zod.config.js +0 -24
  226. package/tsconfig.json +0 -32
  227. package/tsup.config.ts +0 -10
  228. package/vitest.config.ts +0 -8
  229. /package/dist/{utils.mjs.map → chunk-OM7VY3XT.mjs.map} +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
- };
File without changes
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
- }