langwatch 0.3.0-prerelease.1 → 0.3.0-prerelease.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-4BZATFKJ.mjs +181 -0
- package/dist/chunk-4BZATFKJ.mjs.map +1 -0
- package/dist/chunk-CSC3CMIT.mjs +118 -0
- package/dist/chunk-CSC3CMIT.mjs.map +1 -0
- package/dist/chunk-F63YKTXA.mjs +47 -0
- package/dist/chunk-F63YKTXA.mjs.map +1 -0
- package/dist/chunk-G3AUABT7.js +4 -0
- package/dist/chunk-G3AUABT7.js.map +1 -0
- package/dist/chunk-HPC6Z7J4.js +118 -0
- package/dist/chunk-HPC6Z7J4.js.map +1 -0
- package/dist/chunk-KGDAENGD.js +50 -0
- package/dist/chunk-KGDAENGD.js.map +1 -0
- package/dist/chunk-LD74LVRU.js +47 -0
- package/dist/chunk-LD74LVRU.js.map +1 -0
- package/dist/chunk-OM7VY3XT.mjs +4 -0
- package/dist/chunk-OM7VY3XT.mjs.map +1 -0
- package/dist/chunk-PCQVQ7SB.js +45 -0
- package/dist/chunk-PCQVQ7SB.js.map +1 -0
- package/dist/chunk-PMBEK6YE.mjs +424 -0
- package/dist/chunk-PMBEK6YE.mjs.map +1 -0
- package/dist/chunk-PR3JDWC3.mjs +50 -0
- package/dist/chunk-PR3JDWC3.mjs.map +1 -0
- package/dist/chunk-PTJ6AAI7.js +360 -0
- package/dist/chunk-PTJ6AAI7.js.map +1 -0
- package/dist/chunk-QEWDG5QE.mjs +45 -0
- package/dist/chunk-QEWDG5QE.mjs.map +1 -0
- package/dist/chunk-REUCVT7A.mjs +39 -0
- package/dist/chunk-REUCVT7A.mjs.map +1 -0
- package/dist/chunk-SVJ7SCGB.js +424 -0
- package/dist/chunk-SVJ7SCGB.js.map +1 -0
- package/dist/chunk-VJSOCNPA.js +181 -0
- package/dist/chunk-VJSOCNPA.js.map +1 -0
- package/dist/chunk-WM2GRSRW.js +39 -0
- package/dist/chunk-WM2GRSRW.js.map +1 -0
- package/dist/chunk-Z5J5UI5E.mjs +360 -0
- package/dist/chunk-Z5J5UI5E.mjs.map +1 -0
- package/dist/client-B2HqIKg6.d.ts +51 -0
- package/dist/client-XyCqclCi.d.mts +51 -0
- package/dist/client-browser.d.mts +8 -0
- package/dist/client-browser.d.ts +8 -0
- package/dist/client-browser.js +83 -0
- package/dist/client-browser.js.map +1 -0
- package/dist/client-browser.mjs +83 -0
- package/dist/client-browser.mjs.map +1 -0
- package/dist/client-node.d.mts +8 -0
- package/dist/client-node.d.ts +8 -0
- package/dist/client-node.js +90 -0
- package/dist/client-node.js.map +1 -0
- package/dist/client-node.mjs +90 -0
- package/dist/client-node.mjs.map +1 -0
- package/dist/evaluation/index.d.mts +897 -0
- package/dist/evaluation/index.d.ts +897 -0
- package/dist/evaluation/index.js +13 -0
- package/dist/evaluation/index.js.map +1 -0
- package/dist/evaluation/index.mjs +13 -0
- package/dist/evaluation/index.mjs.map +1 -0
- package/dist/filterable-batch-span-processor-zO5kcjBY.d.mts +64 -0
- package/dist/filterable-batch-span-processor-zO5kcjBY.d.ts +64 -0
- package/dist/index.d.mts +48 -0
- package/{src/observability/exporters/langwatch-exporter.ts → dist/index.d.ts} +13 -18
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +30 -0
- package/dist/index.mjs.map +1 -0
- package/dist/observability/index.d.mts +260 -0
- package/dist/observability/index.d.ts +260 -0
- package/dist/observability/index.js +20 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/observability/index.mjs +20 -0
- package/dist/observability/index.mjs.map +1 -0
- package/dist/observability/instrumentation/langchain/index.d.mts +40 -0
- package/dist/observability/instrumentation/langchain/index.d.ts +40 -0
- package/dist/observability/instrumentation/langchain/index.js +666 -0
- package/dist/observability/instrumentation/langchain/index.js.map +1 -0
- package/dist/observability/instrumentation/langchain/index.mjs +666 -0
- package/dist/observability/instrumentation/langchain/index.mjs.map +1 -0
- package/dist/prompt/index.d.mts +10 -0
- package/dist/prompt/index.d.ts +10 -0
- package/dist/prompt/index.js +18 -0
- package/dist/prompt/index.js.map +1 -0
- package/dist/prompt/index.mjs +18 -0
- package/dist/prompt/index.mjs.map +1 -0
- package/dist/prompt-BXJWdbQp.d.mts +1967 -0
- package/dist/prompt-BXJWdbQp.d.ts +1967 -0
- package/dist/record-evaluation-CmxMXa-3.d.mts +25 -0
- package/dist/record-evaluation-CmxMXa-3.d.ts +25 -0
- package/dist/trace-D-bZOuqb.d.mts +622 -0
- package/dist/trace-G2312klE.d.ts +622 -0
- package/package.json +9 -4
- package/.editorconfig +0 -16
- package/.eslintrc.cjs +0 -37
- package/copy-types.sh +0 -28
- package/examples/langchain/.env.example +0 -2
- package/examples/langchain/README.md +0 -42
- package/examples/langchain/package-lock.json +0 -2930
- package/examples/langchain/package.json +0 -27
- package/examples/langchain/src/cli-markdown.d.ts +0 -137
- package/examples/langchain/src/index.ts +0 -109
- package/examples/langchain/tsconfig.json +0 -25
- package/examples/langgraph/.env.example +0 -2
- package/examples/langgraph/README.md +0 -42
- package/examples/langgraph/package-lock.json +0 -3031
- package/examples/langgraph/package.json +0 -28
- package/examples/langgraph/src/cli-markdown.d.ts +0 -137
- package/examples/langgraph/src/index.ts +0 -196
- package/examples/langgraph/tsconfig.json +0 -25
- package/examples/mastra/.env.example +0 -2
- package/examples/mastra/README.md +0 -57
- package/examples/mastra/package-lock.json +0 -5296
- package/examples/mastra/package.json +0 -32
- package/examples/mastra/src/cli-markdown.d.ts +0 -137
- package/examples/mastra/src/index.ts +0 -120
- package/examples/mastra/src/mastra/agents/weather-agent.ts +0 -30
- package/examples/mastra/src/mastra/index.ts +0 -21
- package/examples/mastra/src/mastra/tools/weather-tool.ts +0 -102
- package/examples/mastra/tsconfig.json +0 -25
- package/examples/vercel-ai/.env.example +0 -2
- package/examples/vercel-ai/README.md +0 -38
- package/examples/vercel-ai/package-lock.json +0 -2571
- package/examples/vercel-ai/package.json +0 -27
- package/examples/vercel-ai/src/cli-markdown.d.ts +0 -137
- package/examples/vercel-ai/src/index.ts +0 -110
- package/examples/vercel-ai/src/instrumentation.ts +0 -9
- package/examples/vercel-ai/tsconfig.json +0 -25
- package/src/__tests__/client-browser.test.ts +0 -92
- package/src/__tests__/client-node.test.ts +0 -76
- package/src/__tests__/client.test.ts +0 -71
- package/src/__tests__/integration/client-browser.test.ts +0 -46
- package/src/__tests__/integration/client-node.test.ts +0 -46
- package/src/client-browser.ts +0 -70
- package/src/client-node.ts +0 -82
- package/src/client-shared.ts +0 -72
- package/src/client.ts +0 -119
- package/src/evaluation/__tests__/record-evaluation.test.ts +0 -112
- package/src/evaluation/__tests__/run-evaluation.test.ts +0 -171
- package/src/evaluation/index.ts +0 -2
- package/src/evaluation/record-evaluation.ts +0 -101
- package/src/evaluation/run-evaluation.ts +0 -133
- package/src/evaluation/tracer.ts +0 -3
- package/src/evaluation/types.ts +0 -23
- package/src/index.ts +0 -13
- package/src/internal/api/__tests__/errors.test.ts +0 -98
- package/src/internal/api/client.ts +0 -30
- package/src/internal/api/errors.ts +0 -32
- package/src/internal/generated/openapi/.gitkeep +0 -0
- package/src/internal/generated/types/.gitkeep +0 -0
- package/src/observability/__tests__/integration/base.test.ts +0 -74
- package/src/observability/__tests__/integration/browser-setup-ordering.test.ts +0 -60
- package/src/observability/__tests__/integration/complex-nested-spans.test.ts +0 -29
- package/src/observability/__tests__/integration/error-handling.test.ts +0 -24
- package/src/observability/__tests__/integration/langwatch-disabled-otel.test.ts +0 -24
- package/src/observability/__tests__/integration/langwatch-first-then-vercel.test.ts +0 -24
- package/src/observability/__tests__/integration/multiple-setup-attempts.test.ts +0 -27
- package/src/observability/__tests__/integration/otel-ordering.test.ts +0 -27
- package/src/observability/__tests__/integration/vercel-configurations.test.ts +0 -20
- package/src/observability/__tests__/integration/vercel-first-then-langwatch.test.ts +0 -27
- package/src/observability/__tests__/span.test.ts +0 -214
- package/src/observability/__tests__/trace.test.ts +0 -180
- package/src/observability/exporters/index.ts +0 -1
- package/src/observability/index.ts +0 -4
- package/src/observability/instrumentation/langchain/__tests__/integration/langchain-chatbot.test.ts +0 -112
- package/src/observability/instrumentation/langchain/__tests__/langchain.test.ts +0 -284
- package/src/observability/instrumentation/langchain/index.ts +0 -624
- package/src/observability/processors/__tests__/filterable-batch-span-exporter.test.ts +0 -98
- package/src/observability/processors/filterable-batch-span-processor.ts +0 -99
- package/src/observability/processors/index.ts +0 -1
- package/src/observability/semconv/attributes.ts +0 -185
- package/src/observability/semconv/events.ts +0 -42
- package/src/observability/semconv/index.ts +0 -16
- package/src/observability/semconv/values.ts +0 -159
- package/src/observability/span.ts +0 -728
- package/src/observability/trace.ts +0 -301
- package/src/prompt/__tests__/prompt.test.ts +0 -139
- package/src/prompt/get-prompt-version.ts +0 -49
- package/src/prompt/get-prompt.ts +0 -44
- package/src/prompt/index.ts +0 -3
- package/src/prompt/prompt.ts +0 -133
- package/src/prompt/service.ts +0 -221
- package/src/prompt/tracer.ts +0 -3
- package/src/prompt/types.ts +0 -0
- package/ts-to-zod.config.js +0 -35
- package/tsconfig.json +0 -26
- package/tsup.config.ts +0 -20
- package/vitest.config.ts +0 -9
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
// --- Mock setup (must be at the top for Vitest hoisting) ---
|
|
2
|
-
const { mockSpan, mockTracer } = vi.hoisted(() => {
|
|
3
|
-
const calls: any = { setType: [], setInput: [], setOutput: [], setAttributes: [], setRequestModel: [], setOutputString: [], setInputString: [], addEvent: [], recordException: [], setStatus: [], end: 0 };
|
|
4
|
-
const span = {
|
|
5
|
-
setType: vi.fn(function (this: any, ...args) { calls.setType.push(args); return this; }),
|
|
6
|
-
setInput: vi.fn(function (this: any, ...args) { calls.setInput.push(args); return this; }),
|
|
7
|
-
setOutput: vi.fn(function (this: any, ...args) { calls.setOutput.push(args); return this; }),
|
|
8
|
-
setAttributes: vi.fn(function (this: any, ...args) { calls.setAttributes.push(args); return this; }),
|
|
9
|
-
setRequestModel: vi.fn(function (this: any, ...args) { calls.setRequestModel.push(args); return this; }),
|
|
10
|
-
setOutputString: vi.fn(function (this: any, ...args) { calls.setOutputString.push(args); return this; }),
|
|
11
|
-
setInputString: vi.fn(function (this: any, ...args) { calls.setInputString.push(args); return this; }),
|
|
12
|
-
addEvent: vi.fn(function (this: any, ...args) { calls.addEvent.push(args); return this; }),
|
|
13
|
-
recordException: vi.fn((...args) => { calls.recordException.push(args); }),
|
|
14
|
-
setStatus: vi.fn(function (this: any, ...args) { calls.setStatus.push(args); return this; }),
|
|
15
|
-
setSelectedPrompt: vi.fn(function (this: any, ...args) { calls.setSelectedPrompt.push(args); return this; }),
|
|
16
|
-
end: vi.fn(() => { calls.end++; }),
|
|
17
|
-
setRAGContexts: vi.fn(function (this: any, ...args) { return this; }),
|
|
18
|
-
setRAGContext: vi.fn(function (this: any, ...args) { return this; }),
|
|
19
|
-
setResponseModel: vi.fn(function (this: any, ...args) { return this; }),
|
|
20
|
-
setMetrics: vi.fn(function (this: any, ...args) { return this; }),
|
|
21
|
-
setOutputEvaluation: vi.fn(function (this: any, ...args) { return this; }),
|
|
22
|
-
recordEvaluation: vi.fn(function (this: any, ...args) { return this; }),
|
|
23
|
-
addGenAISystemMessageEvent: vi.fn(function (this: any, ...args) { return this; }),
|
|
24
|
-
addGenAIUserMessageEvent: vi.fn(function (this: any, ...args) { return this; }),
|
|
25
|
-
addGenAIAssistantMessageEvent: vi.fn(function (this: any, ...args) { return this; }),
|
|
26
|
-
addGenAIToolMessageEvent: vi.fn(function (this: any, ...args) { return this; }),
|
|
27
|
-
addGenAIChoiceEvent: vi.fn(function (this: any, ...args) { return this; }),
|
|
28
|
-
spanContext: vi.fn(() => ({ traceId: 'trace', spanId: 'span', traceFlags: 1 })),
|
|
29
|
-
setAttribute: vi.fn(function (this: any, ...args) { return this; }),
|
|
30
|
-
addLink: vi.fn(function (this: any, ...args) { return this; }),
|
|
31
|
-
addLinks: vi.fn(function (this: any, ...args) { return this; }),
|
|
32
|
-
updateName: vi.fn(function (this: any, ...args) { return this; }),
|
|
33
|
-
isRecording: vi.fn(),
|
|
34
|
-
calls,
|
|
35
|
-
};
|
|
36
|
-
const tracer = {
|
|
37
|
-
startSpan: vi.fn(() => span),
|
|
38
|
-
startActiveSpan: vi.fn(() => span),
|
|
39
|
-
withActiveSpan: vi.fn(async (...args: any[]) => {
|
|
40
|
-
// Find the function argument (should be the last argument)
|
|
41
|
-
const fnIndex = args.findIndex((arg) => typeof arg === "function");
|
|
42
|
-
if (fnIndex === -1) {
|
|
43
|
-
throw new Error("withActiveSpan requires a function as the last argument");
|
|
44
|
-
}
|
|
45
|
-
const userFn = args[fnIndex] as (span: any) => any;
|
|
46
|
-
return await userFn(span);
|
|
47
|
-
}) as any,
|
|
48
|
-
};
|
|
49
|
-
return { mockSpan: span, mockTracer: tracer };
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
vi.mock('../../../trace', () => ({
|
|
53
|
-
getLangWatchTracer: vi.fn(() => mockTracer),
|
|
54
|
-
}));
|
|
55
|
-
|
|
56
|
-
vi.mock('@opentelemetry/api', async () => {
|
|
57
|
-
const actual = await vi.importActual<any>('@opentelemetry/api');
|
|
58
|
-
return {
|
|
59
|
-
...actual,
|
|
60
|
-
context: {
|
|
61
|
-
...actual.context,
|
|
62
|
-
active: vi.fn(() => ({})),
|
|
63
|
-
},
|
|
64
|
-
trace: {
|
|
65
|
-
...actual.trace,
|
|
66
|
-
setSpan: vi.fn((_ctx, span) => span),
|
|
67
|
-
},
|
|
68
|
-
SpanStatusCode: { ERROR: 'ERROR' },
|
|
69
|
-
};
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
vi.mock('../../../client', () => ({
|
|
73
|
-
canAutomaticallyCaptureInput: () => true,
|
|
74
|
-
canAutomaticallyCaptureOutput: () => true,
|
|
75
|
-
}));
|
|
76
|
-
|
|
77
|
-
// --- Imports (must be after mocks for Vitest hoisting) ---
|
|
78
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
79
|
-
import { LangWatchCallbackHandler, convertFromLangChainMessages } from '..';
|
|
80
|
-
|
|
81
|
-
// --- Tests ---
|
|
82
|
-
describe('LangWatchCallbackHandler', () => {
|
|
83
|
-
let handler: LangWatchCallbackHandler;
|
|
84
|
-
|
|
85
|
-
beforeEach(() => {
|
|
86
|
-
handler = new LangWatchCallbackHandler();
|
|
87
|
-
handler.tracer = mockTracer;
|
|
88
|
-
vi.clearAllMocks();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
afterEach(() => {
|
|
92
|
-
vi.clearAllMocks();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('can be constructed', () => {
|
|
96
|
-
expect(handler).toBeInstanceOf(LangWatchCallbackHandler);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('handleLLMStart creates a span and sets input/attributes', async () => {
|
|
100
|
-
const serializedMock = { lc: 1, type: 'constructor', id: ['llm', 'test'], kwargs: {} } as import('@langchain/core/load/serializable').SerializedConstructor;
|
|
101
|
-
await handler.handleLLMStart(
|
|
102
|
-
serializedMock,
|
|
103
|
-
['prompt1', 'prompt2'],
|
|
104
|
-
'run1',
|
|
105
|
-
undefined,
|
|
106
|
-
{ temperature: 0.7 },
|
|
107
|
-
['tag1'],
|
|
108
|
-
{ ls_model_name: 'gpt-4', foo: 'bar' },
|
|
109
|
-
'llmName',
|
|
110
|
-
);
|
|
111
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('llmName', {}, expect.anything());
|
|
112
|
-
expect(mockSpan.setType).toHaveBeenCalledWith('llm');
|
|
113
|
-
expect(mockSpan.setInput).toHaveBeenCalledWith(['prompt1', 'prompt2']);
|
|
114
|
-
expect(mockSpan.setRequestModel).toHaveBeenCalledWith('gpt-4');
|
|
115
|
-
expect(mockSpan.setAttributes).toHaveBeenCalled();
|
|
116
|
-
expect(handler.spans['run1']).toBe(mockSpan);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('handleLLMEnd sets output and ends span', async () => {
|
|
120
|
-
handler.spans['run2'] = mockSpan;
|
|
121
|
-
await handler.handleLLMEnd(
|
|
122
|
-
{ generations: [[{ text: 'output1' }], [{ text: 'output2' }]] },
|
|
123
|
-
'run2',
|
|
124
|
-
);
|
|
125
|
-
expect(mockSpan.setOutput).toHaveBeenCalled();
|
|
126
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
127
|
-
expect(handler.spans['run2']).toBeUndefined();
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('handleLLMError records exception, sets error status, and ends span', async () => {
|
|
131
|
-
handler.spans['run3'] = mockSpan;
|
|
132
|
-
const error = new Error('fail');
|
|
133
|
-
await handler.handleLLMError(error, 'run3');
|
|
134
|
-
expect(mockSpan.recordException).toHaveBeenCalledWith(error);
|
|
135
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 'ERROR', message: 'fail' });
|
|
136
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
137
|
-
expect(handler.spans['run3']).toBeUndefined();
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('handleChainStart creates a span and sets input', async () => {
|
|
141
|
-
const serializedMock = { lc: 1, type: 'constructor', id: ['chain', 'test'], kwargs: {} } as import('@langchain/core/load/serializable').SerializedConstructor;
|
|
142
|
-
const inputs = { foo: 'bar' };
|
|
143
|
-
await handler.handleChainStart(
|
|
144
|
-
serializedMock,
|
|
145
|
-
inputs,
|
|
146
|
-
'chainRun1',
|
|
147
|
-
undefined,
|
|
148
|
-
undefined,
|
|
149
|
-
undefined,
|
|
150
|
-
undefined,
|
|
151
|
-
'chainName',
|
|
152
|
-
);
|
|
153
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('chainName', {}, expect.anything());
|
|
154
|
-
expect(mockSpan.setType).toHaveBeenCalledWith('chain');
|
|
155
|
-
expect(mockSpan.setInput).toHaveBeenCalledWith(inputs);
|
|
156
|
-
expect(handler.spans['chainRun1']).toBe(mockSpan);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('handleChainEnd sets output and ends span', async () => {
|
|
160
|
-
handler.spans['chainRun2'] = mockSpan;
|
|
161
|
-
const output = { result: 'done' };
|
|
162
|
-
await handler.handleChainEnd(output, 'chainRun2');
|
|
163
|
-
expect(mockSpan.setOutput).toHaveBeenCalledWith(output);
|
|
164
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
165
|
-
expect(handler.spans['chainRun2']).toBeUndefined();
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it('handleChainError records exception, sets error status, and ends span', async () => {
|
|
169
|
-
handler.spans['chainRun3'] = mockSpan;
|
|
170
|
-
const error = new Error('chain fail');
|
|
171
|
-
await handler.handleChainError(error, 'chainRun3');
|
|
172
|
-
expect(mockSpan.recordException).toHaveBeenCalledWith(error);
|
|
173
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 'ERROR', message: 'chain fail' });
|
|
174
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
175
|
-
expect(handler.spans['chainRun3']).toBeUndefined();
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it('handleToolStart creates a span and sets input string', async () => {
|
|
179
|
-
const serializedMock = { lc: 1, type: 'constructor', id: ['tool', 'test'], kwargs: {} } as import('@langchain/core/load/serializable').SerializedConstructor;
|
|
180
|
-
await handler.handleToolStart(
|
|
181
|
-
serializedMock,
|
|
182
|
-
'tool input',
|
|
183
|
-
'toolRun1',
|
|
184
|
-
undefined,
|
|
185
|
-
['tag1'],
|
|
186
|
-
{ meta: 'data' },
|
|
187
|
-
'toolName',
|
|
188
|
-
);
|
|
189
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('toolName', {}, expect.anything());
|
|
190
|
-
expect(mockSpan.setType).toHaveBeenCalledWith('tool');
|
|
191
|
-
expect(mockSpan.setInputString).toHaveBeenCalledWith('tool input');
|
|
192
|
-
expect(mockSpan.setAttributes).toHaveBeenCalled();
|
|
193
|
-
expect(handler.spans['toolRun1']).toBe(mockSpan);
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
it('handleToolEnd sets output string and ends span', async () => {
|
|
197
|
-
handler.spans['toolRun2'] = mockSpan;
|
|
198
|
-
await handler.handleToolEnd('tool output', 'toolRun2');
|
|
199
|
-
expect(mockSpan.setOutputString).toHaveBeenCalledWith('tool output');
|
|
200
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
201
|
-
expect(handler.spans['toolRun2']).toBeUndefined();
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('handleToolError records exception, sets error status, and ends span', async () => {
|
|
205
|
-
handler.spans['toolRun3'] = mockSpan;
|
|
206
|
-
const error = new Error('tool fail');
|
|
207
|
-
await handler.handleToolError(error, 'toolRun3');
|
|
208
|
-
expect(mockSpan.recordException).toHaveBeenCalledWith(error);
|
|
209
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 'ERROR', message: 'tool fail' });
|
|
210
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
211
|
-
expect(handler.spans['toolRun3']).toBeUndefined();
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it('handleRetrieverStart creates a span and sets input string', async () => {
|
|
215
|
-
const serializedMock = { lc: 1, type: 'constructor', id: ['retriever', 'test'], kwargs: {} } as import('@langchain/core/load/serializable').SerializedConstructor;
|
|
216
|
-
await handler.handleRetrieverStart(
|
|
217
|
-
serializedMock,
|
|
218
|
-
'retriever query',
|
|
219
|
-
'retrieverRun1',
|
|
220
|
-
undefined,
|
|
221
|
-
['tag1'],
|
|
222
|
-
{ meta: 'data' },
|
|
223
|
-
'retrieverName',
|
|
224
|
-
);
|
|
225
|
-
expect(mockTracer.startSpan).toHaveBeenCalledWith('retrieverName', {}, expect.anything());
|
|
226
|
-
expect(mockSpan.setType).toHaveBeenCalledWith('rag');
|
|
227
|
-
expect(mockSpan.setInputString).toHaveBeenCalledWith('retriever query');
|
|
228
|
-
expect(mockSpan.setAttributes).toHaveBeenCalled();
|
|
229
|
-
expect(handler.spans['retrieverRun1']).toBe(mockSpan);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it('handleRetrieverEnd sets output, RAG contexts, and ends span', async () => {
|
|
233
|
-
handler.spans['retrieverRun2'] = mockSpan;
|
|
234
|
-
const docs = [
|
|
235
|
-
{ metadata: { id: 'doc1', chunk_id: 'chunk1' }, pageContent: 'content1' },
|
|
236
|
-
{ metadata: { id: 'doc2', chunk_id: 'chunk2' }, pageContent: 'content2' },
|
|
237
|
-
];
|
|
238
|
-
await handler.handleRetrieverEnd(docs as any, 'retrieverRun2');
|
|
239
|
-
expect(mockSpan.setOutput).toHaveBeenCalledWith(docs);
|
|
240
|
-
expect(mockSpan.setRAGContexts).toHaveBeenCalledWith([
|
|
241
|
-
{ document_id: 'doc1', chunk_id: 'chunk1', content: 'content1' },
|
|
242
|
-
{ document_id: 'doc2', chunk_id: 'chunk2', content: 'content2' },
|
|
243
|
-
]);
|
|
244
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
245
|
-
expect(handler.spans['retrieverRun2']).toBeUndefined();
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
it('handleRetrieverError records exception, sets error status, and ends span', async () => {
|
|
249
|
-
handler.spans['retrieverRun3'] = mockSpan;
|
|
250
|
-
const error = new Error('retriever fail');
|
|
251
|
-
await handler.handleRetrieverError(error, 'retrieverRun3');
|
|
252
|
-
expect(mockSpan.recordException).toHaveBeenCalledWith(error);
|
|
253
|
-
expect(mockSpan.setStatus).toHaveBeenCalledWith({ code: 'ERROR', message: 'retriever fail' });
|
|
254
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
255
|
-
expect(handler.spans['retrieverRun3']).toBeUndefined();
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
it('handleAgentAction adds event and sets type', async () => {
|
|
259
|
-
handler.spans['agentRun1'] = mockSpan;
|
|
260
|
-
await handler.handleAgentAction({} as any, 'agentRun1');
|
|
261
|
-
expect(mockSpan.setType).toHaveBeenCalledWith('agent');
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
it('handleAgentEnd sets output, ends span, and cleans up', async () => {
|
|
265
|
-
handler.spans['agentRun2'] = mockSpan;
|
|
266
|
-
const action = { returnValues: { foo: 'bar' } };
|
|
267
|
-
await handler.handleAgentEnd(action as any, 'agentRun2');
|
|
268
|
-
expect(mockSpan.setOutput).toHaveBeenCalledWith(action.returnValues);
|
|
269
|
-
expect(mockSpan.end).toHaveBeenCalled();
|
|
270
|
-
expect(handler.spans['agentRun2']).toBeUndefined();
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('convertFromLangChainMessages converts messages to expected format', () => {
|
|
274
|
-
const messages = [
|
|
275
|
-
{ content: 'hi', type: 'human', lc_serializable: false },
|
|
276
|
-
{ content: 'hello', type: 'ai', lc_serializable: false },
|
|
277
|
-
];
|
|
278
|
-
const result = convertFromLangChainMessages(messages as any);
|
|
279
|
-
expect(result[0].role).toBe('user');
|
|
280
|
-
expect(result[1].role).toBe('assistant');
|
|
281
|
-
expect(result[0].content).toBe('hi');
|
|
282
|
-
expect(result[1].content).toBe('hello');
|
|
283
|
-
});
|
|
284
|
-
});
|