langwatch 0.1.7 → 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.
- package/.editorconfig +16 -0
- package/LICENSE +7 -0
- package/README.md +268 -1
- package/copy-types.sh +19 -8
- package/examples/langchain/.env.example +2 -0
- package/examples/langchain/README.md +42 -0
- package/examples/langchain/package-lock.json +2930 -0
- package/examples/langchain/package.json +27 -0
- package/examples/langchain/src/cli-markdown.d.ts +137 -0
- package/examples/langchain/src/index.ts +109 -0
- package/examples/langchain/tsconfig.json +25 -0
- package/examples/langgraph/.env.example +2 -0
- package/examples/langgraph/README.md +42 -0
- package/examples/langgraph/package-lock.json +3031 -0
- package/examples/langgraph/package.json +28 -0
- package/examples/langgraph/src/cli-markdown.d.ts +137 -0
- package/examples/langgraph/src/index.ts +196 -0
- package/examples/langgraph/tsconfig.json +25 -0
- package/examples/mastra/.env.example +2 -0
- package/examples/mastra/README.md +57 -0
- package/examples/mastra/package-lock.json +5296 -0
- package/examples/mastra/package.json +32 -0
- package/examples/mastra/src/cli-markdown.d.ts +137 -0
- package/examples/mastra/src/index.ts +120 -0
- package/examples/mastra/src/mastra/agents/weather-agent.ts +30 -0
- package/examples/mastra/src/mastra/index.ts +21 -0
- package/examples/mastra/src/mastra/tools/weather-tool.ts +102 -0
- package/examples/mastra/tsconfig.json +25 -0
- package/examples/vercel-ai/.env.example +2 -0
- package/examples/vercel-ai/README.md +38 -0
- package/examples/vercel-ai/package-lock.json +2571 -0
- package/examples/vercel-ai/package.json +27 -0
- package/examples/vercel-ai/src/cli-markdown.d.ts +137 -0
- package/examples/vercel-ai/src/index.ts +110 -0
- package/examples/vercel-ai/src/instrumentation.ts +9 -0
- package/examples/vercel-ai/tsconfig.json +25 -0
- package/package.json +80 -33
- package/src/__tests__/client-browser.test.ts +92 -0
- package/src/__tests__/client-node.test.ts +76 -0
- package/src/__tests__/client.test.ts +71 -0
- package/src/__tests__/integration/client-browser.test.ts +46 -0
- package/src/__tests__/integration/client-node.test.ts +46 -0
- package/src/client-browser.ts +70 -0
- package/src/client-node.ts +82 -0
- package/src/client-shared.ts +72 -0
- package/src/client.ts +119 -0
- package/src/evaluation/__tests__/record-evaluation.test.ts +112 -0
- package/src/evaluation/__tests__/run-evaluation.test.ts +171 -0
- package/src/evaluation/index.ts +2 -0
- package/src/evaluation/record-evaluation.ts +101 -0
- package/src/evaluation/run-evaluation.ts +133 -0
- package/src/evaluation/tracer.ts +3 -0
- package/src/evaluation/types.ts +23 -0
- package/src/index.ts +10 -591
- package/src/internal/api/__tests__/errors.test.ts +98 -0
- package/src/internal/api/client.ts +30 -0
- package/src/internal/api/errors.ts +32 -0
- package/src/internal/generated/types/.gitkeep +0 -0
- package/src/observability/__tests__/integration/base.test.ts +74 -0
- package/src/observability/__tests__/integration/browser-setup-ordering.test.ts +60 -0
- package/src/observability/__tests__/integration/complex-nested-spans.test.ts +29 -0
- package/src/observability/__tests__/integration/error-handling.test.ts +24 -0
- package/src/observability/__tests__/integration/langwatch-disabled-otel.test.ts +24 -0
- package/src/observability/__tests__/integration/langwatch-first-then-vercel.test.ts +24 -0
- package/src/observability/__tests__/integration/multiple-setup-attempts.test.ts +27 -0
- package/src/observability/__tests__/integration/otel-ordering.test.ts +27 -0
- package/src/observability/__tests__/integration/vercel-configurations.test.ts +20 -0
- package/src/observability/__tests__/integration/vercel-first-then-langwatch.test.ts +27 -0
- package/src/observability/__tests__/span.test.ts +214 -0
- package/src/observability/__tests__/trace.test.ts +180 -0
- package/src/observability/exporters/index.ts +1 -0
- package/src/observability/exporters/langwatch-exporter.ts +53 -0
- package/src/observability/index.ts +4 -0
- package/src/observability/instrumentation/langchain/__tests__/integration/langchain-chatbot.test.ts +112 -0
- package/src/observability/instrumentation/langchain/__tests__/langchain.test.ts +284 -0
- package/src/observability/instrumentation/langchain/index.ts +624 -0
- package/src/observability/processors/__tests__/filterable-batch-span-exporter.test.ts +98 -0
- package/src/observability/processors/filterable-batch-span-processor.ts +99 -0
- package/src/observability/processors/index.ts +1 -0
- package/src/observability/semconv/attributes.ts +185 -0
- package/src/observability/semconv/events.ts +42 -0
- package/src/observability/semconv/index.ts +16 -0
- package/src/observability/semconv/values.ts +159 -0
- package/src/observability/span.ts +728 -0
- package/src/observability/trace.ts +301 -0
- package/src/prompt/__tests__/prompt.test.ts +139 -0
- package/src/prompt/get-prompt-version.ts +49 -0
- package/src/prompt/get-prompt.ts +44 -0
- package/src/prompt/index.ts +3 -0
- package/src/prompt/prompt.ts +133 -0
- package/src/prompt/service.ts +221 -0
- package/src/prompt/tracer.ts +3 -0
- package/src/prompt/types.ts +0 -0
- package/ts-to-zod.config.js +11 -0
- package/tsconfig.json +3 -9
- package/tsup.config.ts +11 -1
- package/vitest.config.ts +1 -0
- package/dist/chunk-FWBCQQYZ.mjs +0 -711
- package/dist/chunk-FWBCQQYZ.mjs.map +0 -1
- package/dist/index.d.mts +0 -1010
- package/dist/index.d.ts +0 -1010
- package/dist/index.js +0 -27294
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -959
- package/dist/index.mjs.map +0 -1
- package/dist/utils-B0pgWcps.d.mts +0 -303
- package/dist/utils-B0pgWcps.d.ts +0 -303
- package/dist/utils.d.mts +0 -2
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -703
- package/dist/utils.js.map +0 -1
- package/dist/utils.mjs +0 -11
- package/dist/utils.mjs.map +0 -1
- 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 -9990
- 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 -91
- package/src/evaluations.ts +0 -219
- package/src/index.test.ts +0 -402
- package/src/langchain.ts +0 -557
- package/src/typeUtils.ts +0 -89
- package/src/types.ts +0 -79
- package/src/utils.ts +0 -205
- /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
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import type { OpenAI } from "openai";
|
|
2
|
-
import { type SnakeToCamelCaseNested } from "./typeUtils";
|
|
3
|
-
import {
|
|
4
|
-
type BaseSpan as ServerBaseSpan,
|
|
5
|
-
type ChatMessage as ServerChatMessage,
|
|
6
|
-
type ChatRichContent as ServerChatRichContent,
|
|
7
|
-
type LLMSpan as ServerLLMSpan,
|
|
8
|
-
type RAGSpan as ServerRAGSpan,
|
|
9
|
-
type SpanInputOutput as ServerSpanInputOutput,
|
|
10
|
-
type TypedValueChatMessages,
|
|
11
|
-
type Trace as ServerTrace,
|
|
12
|
-
type RESTEvaluation as ServerRESTEvaluation,
|
|
13
|
-
type LLMModeTrace as ServerLLMModeTrace,
|
|
14
|
-
} from "./server/types/tracer";
|
|
15
|
-
|
|
16
|
-
export type Trace = ServerTrace;
|
|
17
|
-
|
|
18
|
-
export type LLMModeTrace = ServerLLMModeTrace;
|
|
19
|
-
|
|
20
|
-
export type Metadata = SnakeToCamelCaseNested<Trace["metadata"]>;
|
|
21
|
-
|
|
22
|
-
export type ChatMessage = ServerChatMessage;
|
|
23
|
-
|
|
24
|
-
export type ChatRichContent = ServerChatRichContent;
|
|
25
|
-
|
|
26
|
-
// Check to see if out ChatMessage type is compatible with OpenAIChatCompletion messages
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
28
|
-
({}) as OpenAI.Chat.ChatCompletionMessageParam satisfies ChatMessage;
|
|
29
|
-
// Check to see spans input/output is still compatible with OpenAIChatCompletion messages to avoid camelCase/snake_case issues
|
|
30
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
31
|
-
({}) as {
|
|
32
|
-
type: "chat_messages";
|
|
33
|
-
value: OpenAI.Chat.ChatCompletionMessageParam[];
|
|
34
|
-
} satisfies BaseSpan["input"];
|
|
35
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
|
-
({}) as {
|
|
37
|
-
type: "chat_messages";
|
|
38
|
-
value: OpenAI.Chat.ChatCompletionMessageParam[];
|
|
39
|
-
} satisfies BaseSpan["output"];
|
|
40
|
-
|
|
41
|
-
// Keep the input/output types signatures as snake case to match the official openai nodejs api
|
|
42
|
-
export type SpanInputOutput =
|
|
43
|
-
| SnakeToCamelCaseNested<
|
|
44
|
-
Exclude<ServerSpanInputOutput, TypedValueChatMessages>
|
|
45
|
-
>
|
|
46
|
-
| (TypedValueChatMessages & { type: ChatMessage });
|
|
47
|
-
|
|
48
|
-
export type ConvertServerSpan<T extends ServerBaseSpan> =
|
|
49
|
-
SnakeToCamelCaseNested<Omit<T, "input" | "output" | "error">> & {
|
|
50
|
-
input?: SpanInputOutput | null;
|
|
51
|
-
output?: SpanInputOutput | null;
|
|
52
|
-
error?: T["error"] | NonNullable<unknown>;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export type PendingSpan<T extends BaseSpan> = Omit<
|
|
56
|
-
T,
|
|
57
|
-
"traceId" | "timestamps"
|
|
58
|
-
> & {
|
|
59
|
-
timestamps: Omit<T["timestamps"], "finishedAt"> & {
|
|
60
|
-
finishedAt?: number | null;
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export type BaseSpan = ConvertServerSpan<ServerBaseSpan>;
|
|
65
|
-
|
|
66
|
-
export type PendingBaseSpan = PendingSpan<BaseSpan>;
|
|
67
|
-
|
|
68
|
-
// vendor is deprecated, and we try to force the available models here
|
|
69
|
-
export type LLMSpan = ConvertServerSpan<
|
|
70
|
-
Omit<ServerLLMSpan, "vendor" | "model">
|
|
71
|
-
> & { model: string };
|
|
72
|
-
export type PendingLLMSpan = PendingSpan<LLMSpan>;
|
|
73
|
-
|
|
74
|
-
export type RAGSpan = ConvertServerSpan<ServerRAGSpan>;
|
|
75
|
-
export type PendingRAGSpan = PendingSpan<RAGSpan>;
|
|
76
|
-
|
|
77
|
-
export type RESTEvaluation = SnakeToCamelCaseNested<
|
|
78
|
-
Omit<ServerRESTEvaluation, "error">
|
|
79
|
-
> & { error?: ServerRESTEvaluation["error"] };
|