langsmith 0.7.0 → 0.7.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 (39) hide show
  1. package/dist/client.cjs +1 -1
  2. package/dist/client.js +1 -1
  3. package/dist/experimental/vercel/index.cjs +18 -637
  4. package/dist/experimental/vercel/index.d.ts +3 -257
  5. package/dist/experimental/vercel/index.js +2 -635
  6. package/dist/experimental/vercel/middleware.cjs +2 -78
  7. package/dist/experimental/vercel/middleware.js +1 -77
  8. package/dist/experimental/vercel/telemetry.cjs +462 -0
  9. package/dist/experimental/vercel/telemetry.d.ts +89 -0
  10. package/dist/experimental/vercel/telemetry.js +459 -0
  11. package/dist/experimental/vercel/utils.cjs +142 -35
  12. package/dist/experimental/vercel/utils.d.ts +28 -3
  13. package/dist/experimental/vercel/utils.js +140 -34
  14. package/dist/experimental/vercel/wrap.cjs +639 -0
  15. package/dist/experimental/vercel/wrap.d.ts +257 -0
  16. package/dist/experimental/vercel/wrap.js +635 -0
  17. package/dist/index.cjs +1 -1
  18. package/dist/index.d.ts +1 -1
  19. package/dist/index.js +1 -1
  20. package/dist/sandbox/client.cjs +21 -0
  21. package/dist/sandbox/client.d.ts +1 -0
  22. package/dist/sandbox/client.js +21 -0
  23. package/dist/sandbox/sandbox.cjs +12 -1
  24. package/dist/sandbox/sandbox.js +12 -1
  25. package/dist/sandbox/types.d.ts +12 -0
  26. package/dist/sandbox/ws_execute.cjs +11 -7
  27. package/dist/sandbox/ws_execute.d.ts +2 -1
  28. package/dist/sandbox/ws_execute.js +11 -7
  29. package/dist/utils/types.cjs +13 -0
  30. package/dist/utils/types.d.ts +2 -0
  31. package/dist/utils/types.js +8 -0
  32. package/dist/utils/vercel.cjs +68 -10
  33. package/dist/utils/vercel.d.ts +17 -2
  34. package/dist/utils/vercel.js +68 -10
  35. package/experimental/sandbox.cjs +1 -0
  36. package/experimental/sandbox.d.cts +1 -0
  37. package/experimental/sandbox.d.ts +1 -0
  38. package/experimental/sandbox.js +1 -0
  39. package/package.json +22 -7
package/dist/client.cjs CHANGED
@@ -675,7 +675,7 @@ class Client {
675
675
  enumerable: true,
676
676
  configurable: true,
677
677
  writable: true,
678
- value: false
678
+ value: (0, env_js_1.getLangSmithEnvironmentVariable)("DISABLE_MULTIPART_STREAMING") === "true"
679
679
  });
680
680
  Object.defineProperty(this, "_multipartDisabled", {
681
681
  enumerable: true,
package/dist/client.js CHANGED
@@ -637,7 +637,7 @@ export class Client {
637
637
  enumerable: true,
638
638
  configurable: true,
639
639
  writable: true,
640
- value: false
640
+ value: getLangSmithEnvironmentVariable("DISABLE_MULTIPART_STREAMING") === "true"
641
641
  });
642
642
  Object.defineProperty(this, "_multipartDisabled", {
643
643
  enumerable: true,
@@ -1,639 +1,20 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertMessageToTracedFormat = exports.wrapAISDK = exports.createLangSmithProviderOptions = void 0;
4
- const middleware_js_1 = require("./middleware.cjs");
5
- const utils_js_1 = require("./utils.cjs");
6
- Object.defineProperty(exports, "convertMessageToTracedFormat", { enumerable: true, get: function () { return utils_js_1.convertMessageToTracedFormat; } });
7
- const traceable_js_1 = require("../../traceable.cjs");
8
- const _wrapTools = (tools, lsConfig) => {
9
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
- const wrappedTools = {};
11
- if (tools) {
12
- for (const [key, tool] of Object.entries(tools)) {
13
- wrappedTools[key] = Object.assign(Object.create(Object.getPrototypeOf(tool)), tool);
14
- if (wrappedTools[key] != null &&
15
- typeof wrappedTools[key] === "object" &&
16
- "execute" in wrappedTools[key] &&
17
- typeof wrappedTools[key].execute === "function" &&
18
- !(0, traceable_js_1.isTraceableFunction)(wrappedTools[key].execute)) {
19
- wrappedTools[key].execute = (0, traceable_js_1.traceable)(wrappedTools[key].execute.bind(wrappedTools[key]), {
20
- ...lsConfig,
21
- name: key,
22
- run_type: "tool",
23
- });
24
- }
25
- }
26
- }
27
- return wrappedTools;
28
- };
29
- const _getModelDisplayName = (model) => {
30
- if (typeof model === "string") {
31
- return model;
32
- }
33
- if (model.config != null &&
34
- typeof model.config === "object" &&
35
- "provider" in model.config &&
36
- typeof model.config.provider === "string") {
37
- return model.config.provider;
38
- }
39
- if (model.modelId != null && typeof model.modelId === "string") {
40
- return model.modelId;
41
- }
42
- return "unknown";
43
- };
44
- const _getModelId = (model) => {
45
- if (typeof model === "string") {
46
- return model;
47
- }
48
- return typeof model.modelId === "string" ? model.modelId : undefined;
49
- };
50
- const _formatTracedInputs = async (params) => {
51
- const { prompt, messages, model, tools, output, ...rest } = params;
52
- let processedInputs = {};
53
- if (Array.isArray(prompt)) {
54
- processedInputs = {
55
- ...rest,
56
- messages: prompt.map((message) => (0, utils_js_1.convertMessageToTracedFormat)(message)),
57
- };
58
- }
59
- else if (Array.isArray(messages)) {
60
- processedInputs = {
61
- ...rest,
62
- messages: messages.map((message) => (0, utils_js_1.convertMessageToTracedFormat)(message)),
63
- };
64
- }
65
- else {
66
- processedInputs = { ...rest, prompt, messages };
67
- }
68
- try {
69
- if (output != null &&
70
- typeof output === "object" &&
71
- "responseFormat" in output) {
72
- const responseFormat = await output.responseFormat;
73
- processedInputs.output = responseFormat;
74
- }
75
- else {
76
- processedInputs.output = output;
77
- }
78
- }
79
- catch {
80
- // Could not extract response format from output for tracing
81
- processedInputs.output = output;
82
- }
83
- return processedInputs;
84
- };
85
- const _mergeConfig = (baseConfig, runtimeConfig) => {
86
- return {
87
- ...baseConfig,
88
- ...runtimeConfig,
89
- metadata: {
90
- ls_integration: "vercel-ai-sdk",
91
- ...baseConfig?.metadata,
92
- ...runtimeConfig?.metadata,
93
- },
94
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
95
15
  };
96
- const _extractChildRunConfig = (lsConfig) => {
97
- const { id, name, parent_run_id, start_time, end_time, attachments, dotted_order, processInputs, processOutputs, processChildLLMRunInputs, processChildLLMRunOutputs, ...inheritedConfig } = lsConfig ?? {};
98
- const childConfig = inheritedConfig;
99
- if (processChildLLMRunInputs) {
100
- childConfig.processInputs = processChildLLMRunInputs;
101
- }
102
- if (processChildLLMRunOutputs) {
103
- // TODO: Fix this typing on minor bump
104
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
- childConfig.processOutputs = processChildLLMRunOutputs;
106
- }
107
- return childConfig;
108
- };
109
- const _resolveConfigs = (baseLsConfig, runtimeLsConfig) => {
110
- const baseChildRunConfig = _extractChildRunConfig(baseLsConfig);
111
- const runtimeChildLLMRunConfig = _extractChildRunConfig(runtimeLsConfig);
112
- const resolvedLsConfig = _mergeConfig(baseLsConfig, runtimeLsConfig);
113
- const resolvedChildLLMRunConfig = _mergeConfig(baseChildRunConfig, runtimeChildLLMRunConfig);
114
- const { processInputs: _processInputs, processOutputs: _processOutputs, ...resolvedToolConfig } = resolvedChildLLMRunConfig;
115
- return {
116
- resolvedLsConfig,
117
- resolvedChildLLMRunConfig,
118
- resolvedToolConfig,
119
- };
120
- };
121
- const _getLsAgentType = () => {
122
- const parentRun = (0, traceable_js_1.getCurrentRunTree)(true);
123
- if (parentRun != null && parentRun.run_type === "tool") {
124
- return "subagent";
125
- }
126
- return "root";
127
- };
128
- const _getGenerateTextWrapperConfig = ({ model, runName, aiSdkMethodName, resolvedLsConfig, hasExplicitOutput, hasExplicitExperimentalOutput, traceResponseMetadata, }) => {
129
- return {
130
- name: runName ?? _getModelDisplayName(model),
131
- ...resolvedLsConfig,
132
- metadata: {
133
- ls_agent_type: _getLsAgentType(),
134
- ai_sdk_method: aiSdkMethodName ?? "ai.generateText",
135
- ...resolvedLsConfig?.metadata,
136
- },
137
- processInputs: async (inputs) => {
138
- const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
139
- return inputFormatter(inputs);
140
- },
141
- processOutputs: async (outputs) => {
142
- if (resolvedLsConfig?.processOutputs) {
143
- const processedOutputs = await resolvedLsConfig.processOutputs(
144
- // TODO: Fix this typing on minor bump
145
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
146
- outputs);
147
- return processedOutputs;
148
- }
149
- if (outputs.outputs == null || typeof outputs.outputs !== "object") {
150
- return outputs;
151
- }
152
- // If output or experimental_output (legacy) was explicitly provided, return it directly at top level (like generateObject)
153
- // Note: In AI SDK 6, experimental_output/output is always available as a getter, so we need to check if it was explicitly provided
154
- if (hasExplicitOutput) {
155
- try {
156
- // Try new 'output' property first, then fall back to 'experimental_output' for backwards compatibility
157
- if ("output" in outputs.outputs) {
158
- const output = outputs.outputs.output;
159
- if (output != null && typeof output === "object") {
160
- if (Array.isArray(output)) {
161
- return { outputs: output };
162
- }
163
- return output;
164
- }
165
- }
166
- }
167
- catch {
168
- // output not accessible, continue with normal processing
169
- }
170
- }
171
- else if (hasExplicitExperimentalOutput) {
172
- try {
173
- if ("experimental_output" in outputs.outputs) {
174
- const experimentalOutput = outputs.outputs.experimental_output;
175
- if (experimentalOutput != null) {
176
- return experimentalOutput;
177
- }
178
- }
179
- }
180
- catch {
181
- // experimental_output not accessible, continue with normal processing
182
- }
183
- }
184
- const { steps } = outputs.outputs;
185
- if (Array.isArray(steps)) {
186
- const lastStep = steps.at(-1);
187
- if (lastStep == null || typeof lastStep !== "object") {
188
- return outputs;
189
- }
190
- const { content } = lastStep;
191
- return (0, utils_js_1.convertMessageToTracedFormat)({
192
- content: content ?? outputs.outputs.text,
193
- role: "assistant",
194
- }, (resolvedLsConfig?.traceResponseMetadata ?? traceResponseMetadata)
195
- ? { steps }
196
- : undefined);
197
- }
198
- else {
199
- return outputs;
200
- }
201
- },
202
- };
203
- };
204
- const _getStreamTextWrapperConfig = ({ model, runName, aiSdkMethodName, resolvedLsConfig, hasExplicitOutput, hasExplicitExperimentalOutput, traceResponseMetadata, }) => {
205
- return {
206
- name: runName ?? _getModelDisplayName(model),
207
- ...resolvedLsConfig,
208
- metadata: {
209
- ls_agent_type: _getLsAgentType(),
210
- ai_sdk_method: aiSdkMethodName ?? "ai.streamText",
211
- ...resolvedLsConfig?.metadata,
212
- },
213
- processInputs: async (inputs) => {
214
- const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
215
- return inputFormatter(inputs);
216
- },
217
- processOutputs: async (outputs) => {
218
- try {
219
- if (resolvedLsConfig?.processOutputs) {
220
- const processedOutputs = await resolvedLsConfig.processOutputs(
221
- // TODO: Fix this typing on minor bump
222
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
223
- outputs);
224
- return processedOutputs;
225
- }
226
- if (outputs.outputs == null || typeof outputs.outputs !== "object") {
227
- return outputs;
228
- }
229
- // Important: Even accessing this property creates a promise.
230
- // This must be awaited.
231
- let content = await outputs.outputs.content;
232
- if (content == null) {
233
- // AI SDK 4 shim
234
- content = await outputs.outputs.text;
235
- }
236
- if (content == null || !["object", "string"].includes(typeof content)) {
237
- return outputs;
238
- }
239
- try {
240
- if (hasExplicitOutput || hasExplicitExperimentalOutput) {
241
- const textContent = await outputs.outputs.text;
242
- return JSON.parse(textContent);
243
- }
244
- }
245
- catch {
246
- // experimental_partialOutputStream not specified, continue with normal processing
247
- }
248
- let responseMetadata = undefined;
249
- if (resolvedLsConfig?.traceResponseMetadata ?? traceResponseMetadata) {
250
- try {
251
- const steps = await outputs.outputs.steps;
252
- responseMetadata = { steps };
253
- }
254
- catch {
255
- // Do nothing if step parsing fails
256
- }
257
- }
258
- return (0, utils_js_1.convertMessageToTracedFormat)({
259
- content,
260
- role: "assistant",
261
- }, responseMetadata);
262
- }
263
- catch {
264
- // Handle parsing failures without a log
265
- return outputs;
266
- }
267
- },
268
- };
269
- };
270
- /**
271
- * Wraps LangSmith config in a way that matches AI SDK provider types.
272
- *
273
- * ```ts
274
- * import { createLangSmithProviderOptions } from "langsmith/experimental/vercel";
275
- * import * as ai from "ai";
276
- *
277
- * const lsConfig = createLangSmithProviderOptions<typeof ai.generateText>({
278
- * // Will have appropriate typing
279
- * processInputs: (inputs) => {
280
- * const { messages } = inputs;
281
- * return {
282
- * messages: messages?.map((message) => ({
283
- * ...message,
284
- * content: "REDACTED",
285
- * })),
286
- * prompt: "REDACTED",
287
- * };
288
- * },
289
- * });
290
- * ```
291
- *
292
- * Note: AI SDK expects only JSON values in an object for
293
- * provider options, but LangSmith's config may contain non-JSON values.
294
- * These are not passed to the underlying AI SDK model, so it is safe to
295
- * cast the typing here.
296
- */
297
- const createLangSmithProviderOptions = (lsConfig) => {
298
- return (lsConfig ?? {});
299
- };
300
- exports.createLangSmithProviderOptions = createLangSmithProviderOptions;
301
- /**
302
- * Wraps Vercel AI SDK 6 or AI SDK 5 functions with LangSmith tracing capabilities.
303
- *
304
- * @param methods - Object containing AI SDK methods to wrap
305
- * @param methods.wrapLanguageModel - AI SDK's wrapLanguageModel function
306
- * @param methods.generateText - AI SDK's generateText function
307
- * @param methods.streamText - AI SDK's streamText function
308
- * @param methods.streamObject - AI SDK's streamObject function
309
- * @param methods.generateObject - AI SDK's generateObject function
310
- *
311
- * @returns Object containing wrapped versions of the AI SDK functions with LangSmith tracing
312
- * @returns returns.generateText - Wrapped generateText function that traces calls to LangSmith
313
- * @returns returns.generateObject - Wrapped generateObject function that traces calls to LangSmith
314
- * @returns returns.streamText - Wrapped streamText function that traces calls to LangSmith
315
- * @returns returns.streamObject - Wrapped streamObject function that traces calls to LangSmith
316
- */
317
- const wrapAISDK = (ai, baseLsConfig) => {
318
- /**
319
- * Wrapped version of AI SDK's generateText with LangSmith tracing.
320
- *
321
- * This function has the same signature and behavior as the original generateText,
322
- * but adds automatic tracing to LangSmith for observability.
323
- *
324
- * ```ts
325
- * import * as ai from "ai";
326
- * import { wrapAISDK } from "langsmith/experimental/vercel";
327
- *
328
- * const { generateText } = wrapAISDK(ai);
329
- * const { text } = await generateText(...);
330
- * ```
331
- *
332
- * @see {@link https://sdk.vercel.ai/docs/ai-sdk-core/generating-text} Original generateText documentation
333
- * @param params - Same parameters as the original generateText function
334
- * @returns Promise resolving to the same result as generateText, with tracing applied
335
- */
336
- const wrappedGenerateText = async (...args) => {
337
- const params = args[0];
338
- const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
339
- const { resolvedLsConfig, resolvedChildLLMRunConfig, resolvedToolConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
340
- const hasExplicitOutput = "output" in params;
341
- const hasExplicitExperimentalOutput = "experimental_output" in params;
342
- const traceableFunc = (0, traceable_js_1.traceable)(async (...args) => {
343
- const [params, ...rest] = args;
344
- const wrappedModel = ai.wrapLanguageModel({
345
- model: params.model,
346
- middleware: (0, middleware_js_1.LangSmithMiddleware)({
347
- name: _getModelDisplayName(params.model),
348
- modelId: _getModelId(params.model),
349
- // TODO: Fix this typing on minor bump
350
- lsConfig: resolvedChildLLMRunConfig,
351
- }),
352
- });
353
- return ai.generateText({
354
- ...params,
355
- providerOptions,
356
- tools: _wrapTools(params.tools, resolvedToolConfig),
357
- model: wrappedModel,
358
- }, ...rest);
359
- }, _getGenerateTextWrapperConfig({
360
- model: params.model,
361
- resolvedLsConfig,
362
- hasExplicitOutput,
363
- hasExplicitExperimentalOutput,
364
- }));
365
- return traceableFunc(...args);
366
- };
367
- let wrappedGenerateObject;
368
- if (typeof ai.generateObject === "function") {
369
- const generateObject = ai.generateObject;
370
- /**
371
- * Wrapped version of AI SDK's generateObject with LangSmith tracing.
372
- *
373
- * This function has the same signature and behavior as the original generateObject,
374
- * but adds automatic tracing to LangSmith for observability.
375
- *
376
- * ```ts
377
- * import * as ai from "ai";
378
- * import { wrapAISDK } from "langsmith/experimental/vercel";
379
- *
380
- * const { generateObject } = wrapAISDK(ai);
381
- * const { object } = await generateObject(...);
382
- * ```
383
- *
384
- * @see {@link https://sdk.vercel.ai/docs/ai-sdk-core/generating-structured-data} Original generateObject documentation
385
- * @param params - Same parameters as the original generateObject function
386
- * @returns Promise resolving to the same result as generateObject, with tracing applied
387
- */
388
- wrappedGenerateObject = async (...args) => {
389
- const params = args[0];
390
- const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
391
- const { resolvedLsConfig, resolvedChildLLMRunConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
392
- const traceableFunc = (0, traceable_js_1.traceable)(async (...args) => {
393
- const [params, ...rest] = args;
394
- const wrappedModel = ai.wrapLanguageModel({
395
- model: params.model,
396
- middleware: (0, middleware_js_1.LangSmithMiddleware)({
397
- name: _getModelDisplayName(params.model),
398
- modelId: _getModelId(params.model),
399
- // TODO: Fix this typing on minor bump
400
- lsConfig: resolvedChildLLMRunConfig,
401
- }),
402
- });
403
- return generateObject({
404
- ...params,
405
- providerOptions,
406
- model: wrappedModel,
407
- }, ...rest);
408
- }, {
409
- name: _getModelDisplayName(params.model),
410
- ...resolvedLsConfig,
411
- metadata: {
412
- ls_agent_type: _getLsAgentType(),
413
- ai_sdk_method: "ai.generateObject",
414
- ...resolvedLsConfig?.metadata,
415
- },
416
- processInputs: async (inputs) => {
417
- const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
418
- return inputFormatter(inputs);
419
- },
420
- processOutputs: async (outputs) => {
421
- if (resolvedLsConfig?.processOutputs) {
422
- const processedOutputs = await resolvedLsConfig.processOutputs(
423
- // TODO: Fix this typing on minor bump
424
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
425
- outputs);
426
- return processedOutputs;
427
- }
428
- if (outputs.outputs == null ||
429
- typeof outputs.outputs !== "object") {
430
- return outputs;
431
- }
432
- return outputs.outputs.object ?? outputs;
433
- },
434
- });
435
- return traceableFunc(...args);
436
- };
437
- }
438
- /**
439
- * Wrapped version of AI SDK's streamText with LangSmith tracing.
440
- *
441
- * Must be called with `await`, but otherwise behaves the same as the
442
- * original streamText and adds adds automatic tracing to LangSmith
443
- * for observability.
444
- *
445
- * ```ts
446
- * import * as ai from "ai";
447
- * import { wrapAISDK } from "langsmith/experimental/vercel";
448
- *
449
- * const { streamText } = wrapAISDK(ai);
450
- * const { textStream } = await streamText(...);
451
- * ```
452
- *
453
- * @see {@link https://sdk.vercel.ai/docs/ai-sdk-core/generating-text} Original streamText documentation
454
- * @param params - Same parameters as the original streamText function
455
- * @returns Promise resolving to the same result as streamText, with tracing applied
456
- */
457
- const wrappedStreamText = (...args) => {
458
- const params = args[0];
459
- const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
460
- const { resolvedLsConfig, resolvedChildLLMRunConfig, resolvedToolConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
461
- const hasExplicitOutput = "output" in params;
462
- const hasExplicitExperimentalOutput = "experimental_output" in params;
463
- const traceableFunc = (0, traceable_js_1.traceable)((...args) => {
464
- const [params, ...rest] = args;
465
- const wrappedModel = ai.wrapLanguageModel({
466
- model: params.model,
467
- middleware: (0, middleware_js_1.LangSmithMiddleware)({
468
- name: _getModelDisplayName(params.model),
469
- modelId: _getModelId(params.model),
470
- // TODO: Fix this typing on minor bump
471
- lsConfig: resolvedChildLLMRunConfig,
472
- }),
473
- });
474
- return ai.streamText({
475
- ...params,
476
- providerOptions,
477
- tools: _wrapTools(params.tools, resolvedToolConfig),
478
- model: wrappedModel,
479
- }, ...rest);
480
- }, _getStreamTextWrapperConfig({
481
- model: params.model,
482
- resolvedLsConfig,
483
- hasExplicitOutput,
484
- hasExplicitExperimentalOutput,
485
- }));
486
- return traceableFunc(...args);
487
- };
488
- let wrappedStreamObject;
489
- if (typeof ai.streamObject === "function") {
490
- const streamObject = ai.streamObject;
491
- /**
492
- * Wrapped version of AI SDK's streamObject with LangSmith tracing.
493
- *
494
- * Must be called with `await`, but otherwise behaves the same as the
495
- * original streamObject and adds adds automatic tracing to LangSmith
496
- * for observability.
497
- *
498
- * ```ts
499
- * import * as ai from "ai";
500
- * import { wrapAISDK } from "langsmith/experimental/vercel";
501
- *
502
- * const { streamObject } = wrapAISDK(ai);
503
- * const { partialObjectStream } = await streamObject(...);
504
- * ```
505
- *
506
- * @see {@link https://sdk.vercel.ai/docs/ai-sdk-core/generating-structured-data} Original streamObject documentation
507
- * @param params - Same parameters as the original streamObject function
508
- * @returns Promise resolving to the same result as streamObject, with tracing applied
509
- */
510
- wrappedStreamObject = (...args) => {
511
- const params = args[0];
512
- const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
513
- const { resolvedLsConfig, resolvedChildLLMRunConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
514
- const traceableFunc = (0, traceable_js_1.traceable)((...args) => {
515
- const [params, ...rest] = args;
516
- const wrappedModel = ai.wrapLanguageModel({
517
- model: params.model,
518
- middleware: (0, middleware_js_1.LangSmithMiddleware)({
519
- name: _getModelDisplayName(params.model),
520
- modelId: _getModelId(params.model),
521
- // TODO: Fix this typing on minor bump
522
- lsConfig: resolvedChildLLMRunConfig,
523
- }),
524
- });
525
- return streamObject({
526
- ...params,
527
- providerOptions,
528
- model: wrappedModel,
529
- }, ...rest);
530
- }, {
531
- name: _getModelDisplayName(params.model),
532
- ...resolvedLsConfig,
533
- metadata: {
534
- ls_agent_type: _getLsAgentType(),
535
- ai_sdk_method: "ai.streamObject",
536
- ...resolvedLsConfig?.metadata,
537
- },
538
- processInputs: async (inputs) => {
539
- const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
540
- return inputFormatter(inputs);
541
- },
542
- processOutputs: async (outputs) => {
543
- try {
544
- if (resolvedLsConfig?.processOutputs) {
545
- const processedOutputs = await resolvedLsConfig.processOutputs(
546
- // TODO: Fix this typing on minor bump
547
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
548
- outputs);
549
- return processedOutputs;
550
- }
551
- if (outputs.outputs == null ||
552
- typeof outputs.outputs !== "object") {
553
- return outputs;
554
- }
555
- const object = await outputs.outputs.object;
556
- if (object == null || typeof object !== "object") {
557
- return outputs;
558
- }
559
- return object;
560
- }
561
- catch {
562
- // Handle parsing failures without a log
563
- return outputs;
564
- }
565
- },
566
- });
567
- return traceableFunc(...args);
568
- };
569
- }
570
- let wrappedToolLoopAgentClass;
571
- if (ai.ToolLoopAgent != null) {
572
- try {
573
- const wrapToolLoopAgent = (ToolLoopAgent) => {
574
- return new Proxy(ToolLoopAgent, {
575
- construct(ToolLoopAgent, args) {
576
- const params = args[0] ?? {};
577
- const { langsmith: runtimeLsConfig } = params.providerOptions ?? {};
578
- const { resolvedLsConfig, resolvedChildLLMRunConfig, resolvedToolConfig, } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
579
- let wrappedModel = params.model;
580
- if (wrappedModel != null) {
581
- wrappedModel = ai.wrapLanguageModel({
582
- model: params.model,
583
- middleware: (0, middleware_js_1.LangSmithMiddleware)({
584
- name: _getModelDisplayName(params.model),
585
- modelId: _getModelId(params.model),
586
- // TODO: Fix this typing on minor bump
587
- lsConfig: resolvedChildLLMRunConfig,
588
- }),
589
- });
590
- }
591
- let wrappedTools = params.tools;
592
- if (wrappedTools != null) {
593
- wrappedTools = _wrapTools(params.tools, resolvedToolConfig);
594
- }
595
- const instance = new ToolLoopAgent(...[
596
- { ...params, model: wrappedModel, tools: wrappedTools },
597
- ...args.slice(1),
598
- ]);
599
- if (typeof instance.generate === "function") {
600
- instance.generate = (0, traceable_js_1.traceable)(instance.generate.bind(instance), _getGenerateTextWrapperConfig({
601
- model: wrappedModel,
602
- runName: "ToolLoopAgent",
603
- aiSdkMethodName: "ai.ToolLoopAgent.generate",
604
- resolvedLsConfig,
605
- hasExplicitOutput: "output" in params && params.output != null,
606
- traceResponseMetadata: true,
607
- }));
608
- }
609
- if (typeof instance.stream === "function") {
610
- instance.stream = (0, traceable_js_1.traceable)(instance.stream.bind(instance), _getStreamTextWrapperConfig({
611
- model: wrappedModel,
612
- runName: "ToolLoopAgent",
613
- aiSdkMethodName: "ai.ToolLoopAgent.stream",
614
- resolvedLsConfig,
615
- hasExplicitOutput: "output" in params && params.output != null,
616
- traceResponseMetadata: true,
617
- }));
618
- }
619
- return instance;
620
- },
621
- });
622
- };
623
- wrappedToolLoopAgentClass = wrapToolLoopAgent(ai.ToolLoopAgent);
624
- }
625
- catch (e) {
626
- console.error("Failed to wrap passed ToolLoopAgent:", e);
627
- wrappedToolLoopAgentClass = ai.ToolLoopAgent;
628
- }
629
- }
630
- return {
631
- ...ai,
632
- generateText: wrappedGenerateText,
633
- generateObject: wrappedGenerateObject,
634
- streamText: wrappedStreamText,
635
- streamObject: wrappedStreamObject,
636
- ToolLoopAgent: wrappedToolLoopAgentClass,
637
- };
638
- };
639
- exports.wrapAISDK = wrapAISDK;
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.LangSmithTelemetry = void 0;
18
+ __exportStar(require("./wrap.cjs"), exports);
19
+ var telemetry_js_1 = require("./telemetry.cjs");
20
+ Object.defineProperty(exports, "LangSmithTelemetry", { enumerable: true, get: function () { return telemetry_js_1.LangSmithTelemetry; } });