langsmith 0.1.26 → 0.1.27
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/env.cjs +17 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +13 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/langchain.cjs +105 -0
- package/dist/langchain.d.ts +29 -0
- package/dist/langchain.js +100 -0
- package/dist/run_trees.cjs +25 -16
- package/dist/run_trees.d.ts +1 -1
- package/dist/run_trees.js +25 -16
- package/dist/singletons/traceable.cjs +62 -0
- package/dist/singletons/traceable.d.ts +23 -0
- package/dist/singletons/traceable.js +57 -0
- package/dist/singletons/types.cjs +2 -0
- package/dist/singletons/types.d.ts +38 -0
- package/dist/singletons/types.js +1 -0
- package/dist/traceable.cjs +25 -67
- package/dist/traceable.d.ts +4 -47
- package/dist/traceable.js +19 -61
- package/langchain.cjs +1 -0
- package/langchain.d.cts +1 -0
- package/langchain.d.ts +1 -0
- package/langchain.js +1 -0
- package/package.json +32 -2
- package/singletons/traceable.cjs +1 -0
- package/singletons/traceable.d.cts +1 -0
- package/singletons/traceable.d.ts +1 -0
- package/singletons/traceable.js +1 -0
package/dist/env.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isTracingEnabled = void 0;
|
|
4
|
+
const env_js_1 = require("./utils/env.cjs");
|
|
5
|
+
const isTracingEnabled = (tracingEnabled) => {
|
|
6
|
+
if (tracingEnabled !== undefined) {
|
|
7
|
+
return tracingEnabled;
|
|
8
|
+
}
|
|
9
|
+
const envVars = [
|
|
10
|
+
"LANGSMITH_TRACING_V2",
|
|
11
|
+
"LANGCHAIN_TRACING_V2",
|
|
12
|
+
"LANGSMITH_TRACING",
|
|
13
|
+
"LANGCHAIN_TRACING",
|
|
14
|
+
];
|
|
15
|
+
return !!envVars.find((envVar) => (0, env_js_1.getEnvironmentVariable)(envVar) === "true");
|
|
16
|
+
};
|
|
17
|
+
exports.isTracingEnabled = isTracingEnabled;
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isTracingEnabled: (tracingEnabled?: boolean) => boolean;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { getEnvironmentVariable } from "./utils/env.js";
|
|
2
|
+
export const isTracingEnabled = (tracingEnabled) => {
|
|
3
|
+
if (tracingEnabled !== undefined) {
|
|
4
|
+
return tracingEnabled;
|
|
5
|
+
}
|
|
6
|
+
const envVars = [
|
|
7
|
+
"LANGSMITH_TRACING_V2",
|
|
8
|
+
"LANGCHAIN_TRACING_V2",
|
|
9
|
+
"LANGSMITH_TRACING",
|
|
10
|
+
"LANGCHAIN_TRACING",
|
|
11
|
+
];
|
|
12
|
+
return !!envVars.find((envVar) => getEnvironmentVariable(envVar) === "true");
|
|
13
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -6,4 +6,4 @@ Object.defineProperty(exports, "Client", { enumerable: true, get: function () {
|
|
|
6
6
|
var run_trees_js_1 = require("./run_trees.cjs");
|
|
7
7
|
Object.defineProperty(exports, "RunTree", { enumerable: true, get: function () { return run_trees_js_1.RunTree; } });
|
|
8
8
|
// Update using yarn bump-version
|
|
9
|
-
exports.__version__ = "0.1.
|
|
9
|
+
exports.__version__ = "0.1.27";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Client } from "./client.js";
|
|
2
2
|
export type { Dataset, Example, TracerSession, Run, Feedback, RetrieverOutput, } from "./schemas.js";
|
|
3
3
|
export { RunTree, type RunTreeConfig } from "./run_trees.js";
|
|
4
|
-
export declare const __version__ = "0.1.
|
|
4
|
+
export declare const __version__ = "0.1.27";
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RunnableTraceable = exports.getLangchainCallbacks = void 0;
|
|
4
|
+
const manager_1 = require("@langchain/core/callbacks/manager");
|
|
5
|
+
const tracer_langchain_1 = require("@langchain/core/tracers/tracer_langchain");
|
|
6
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
7
|
+
const traceable_js_1 = require("./traceable.cjs");
|
|
8
|
+
/**
|
|
9
|
+
* Converts the current run tree active within a traceable-wrapped function
|
|
10
|
+
* into a LangChain compatible callback manager. This is useful to handoff tracing
|
|
11
|
+
* from LangSmith to LangChain Runnables and LLMs.
|
|
12
|
+
*
|
|
13
|
+
* @param {RunTree | undefined} currentRunTree Current RunTree from within a traceable-wrapped function. If not provided, the current run tree will be inferred from AsyncLocalStorage.
|
|
14
|
+
* @returns {CallbackManager | undefined} Callback manager used by LangChain Runnable objects.
|
|
15
|
+
*/
|
|
16
|
+
async function getLangchainCallbacks(currentRunTree) {
|
|
17
|
+
const runTree = currentRunTree ?? (0, traceable_js_1.getCurrentRunTree)();
|
|
18
|
+
if (!runTree)
|
|
19
|
+
return undefined;
|
|
20
|
+
// TODO: CallbackManager.configure() is only async due to LangChainTracer
|
|
21
|
+
// factory being unnecessarily async.
|
|
22
|
+
let callbacks = await manager_1.CallbackManager.configure();
|
|
23
|
+
if (!callbacks && runTree.tracingEnabled) {
|
|
24
|
+
callbacks = new manager_1.CallbackManager();
|
|
25
|
+
}
|
|
26
|
+
let langChainTracer = callbacks?.handlers.find((handler) => handler?.name === "langchain_tracer");
|
|
27
|
+
if (!langChainTracer && runTree.tracingEnabled) {
|
|
28
|
+
langChainTracer = new tracer_langchain_1.LangChainTracer();
|
|
29
|
+
callbacks?.addHandler(langChainTracer);
|
|
30
|
+
}
|
|
31
|
+
const runMap = new Map();
|
|
32
|
+
// find upward root run
|
|
33
|
+
let rootRun = runTree;
|
|
34
|
+
const rootVisited = new Set();
|
|
35
|
+
while (rootRun.parent_run) {
|
|
36
|
+
if (rootVisited.has(rootRun.id))
|
|
37
|
+
break;
|
|
38
|
+
rootVisited.add(rootRun.id);
|
|
39
|
+
rootRun = rootRun.parent_run;
|
|
40
|
+
}
|
|
41
|
+
const queue = [rootRun];
|
|
42
|
+
const visited = new Set();
|
|
43
|
+
while (queue.length > 0) {
|
|
44
|
+
const current = queue.shift();
|
|
45
|
+
if (!current || visited.has(current.id))
|
|
46
|
+
continue;
|
|
47
|
+
visited.add(current.id);
|
|
48
|
+
runMap.set(current.id, current);
|
|
49
|
+
if (current.child_runs) {
|
|
50
|
+
queue.push(...current.child_runs);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (callbacks != null) {
|
|
54
|
+
Object.assign(callbacks, { _parentRunId: runTree.id });
|
|
55
|
+
}
|
|
56
|
+
if (langChainTracer != null) {
|
|
57
|
+
Object.assign(langChainTracer, {
|
|
58
|
+
runMap,
|
|
59
|
+
client: runTree.client,
|
|
60
|
+
projectName: runTree.project_name || langChainTracer.projectName,
|
|
61
|
+
exampleId: runTree.reference_example_id || langChainTracer.exampleId,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return callbacks;
|
|
65
|
+
}
|
|
66
|
+
exports.getLangchainCallbacks = getLangchainCallbacks;
|
|
67
|
+
/**
|
|
68
|
+
* RunnableTraceable is a Runnable that wraps a traceable function.
|
|
69
|
+
* This allows adding Langsmith traced functions into LangChain sequences.
|
|
70
|
+
*/
|
|
71
|
+
class RunnableTraceable extends runnables_1.Runnable {
|
|
72
|
+
constructor(fields) {
|
|
73
|
+
super(fields);
|
|
74
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: false
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: ["langchain_core", "runnables"]
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(this, "func", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: void 0
|
|
91
|
+
});
|
|
92
|
+
if (!(0, traceable_js_1.isTraceableFunction)(fields.func)) {
|
|
93
|
+
throw new Error("RunnableTraceable requires a function that is wrapped in traceable higher-order function");
|
|
94
|
+
}
|
|
95
|
+
this.func = fields.func;
|
|
96
|
+
}
|
|
97
|
+
async invoke(input, options) {
|
|
98
|
+
const [config] = this._getOptionsList(options ?? {}, 1);
|
|
99
|
+
return (await this.func(config, input));
|
|
100
|
+
}
|
|
101
|
+
static from(func) {
|
|
102
|
+
return new RunnableTraceable({ func });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.RunnableTraceable = RunnableTraceable;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
2
|
+
import { Runnable, RunnableConfig } from "@langchain/core/runnables";
|
|
3
|
+
import { RunTree } from "./run_trees.js";
|
|
4
|
+
import { TraceableFunction } from "./traceable.js";
|
|
5
|
+
/**
|
|
6
|
+
* Converts the current run tree active within a traceable-wrapped function
|
|
7
|
+
* into a LangChain compatible callback manager. This is useful to handoff tracing
|
|
8
|
+
* from LangSmith to LangChain Runnables and LLMs.
|
|
9
|
+
*
|
|
10
|
+
* @param {RunTree | undefined} currentRunTree Current RunTree from within a traceable-wrapped function. If not provided, the current run tree will be inferred from AsyncLocalStorage.
|
|
11
|
+
* @returns {CallbackManager | undefined} Callback manager used by LangChain Runnable objects.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getLangchainCallbacks(currentRunTree?: RunTree | undefined): Promise<CallbackManager | undefined>;
|
|
14
|
+
type AnyTraceableFunction = TraceableFunction<(...any: any[]) => any>;
|
|
15
|
+
/**
|
|
16
|
+
* RunnableTraceable is a Runnable that wraps a traceable function.
|
|
17
|
+
* This allows adding Langsmith traced functions into LangChain sequences.
|
|
18
|
+
*/
|
|
19
|
+
export declare class RunnableTraceable<RunInput, RunOutput> extends Runnable<RunInput, RunOutput> {
|
|
20
|
+
lc_serializable: boolean;
|
|
21
|
+
lc_namespace: string[];
|
|
22
|
+
protected func: AnyTraceableFunction;
|
|
23
|
+
constructor(fields: {
|
|
24
|
+
func: AnyTraceableFunction;
|
|
25
|
+
});
|
|
26
|
+
invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunOutput>;
|
|
27
|
+
static from(func: AnyTraceableFunction): RunnableTraceable<unknown, unknown>;
|
|
28
|
+
}
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
2
|
+
import { LangChainTracer } from "@langchain/core/tracers/tracer_langchain";
|
|
3
|
+
import { Runnable } from "@langchain/core/runnables";
|
|
4
|
+
import { getCurrentRunTree, isTraceableFunction, } from "./traceable.js";
|
|
5
|
+
/**
|
|
6
|
+
* Converts the current run tree active within a traceable-wrapped function
|
|
7
|
+
* into a LangChain compatible callback manager. This is useful to handoff tracing
|
|
8
|
+
* from LangSmith to LangChain Runnables and LLMs.
|
|
9
|
+
*
|
|
10
|
+
* @param {RunTree | undefined} currentRunTree Current RunTree from within a traceable-wrapped function. If not provided, the current run tree will be inferred from AsyncLocalStorage.
|
|
11
|
+
* @returns {CallbackManager | undefined} Callback manager used by LangChain Runnable objects.
|
|
12
|
+
*/
|
|
13
|
+
export async function getLangchainCallbacks(currentRunTree) {
|
|
14
|
+
const runTree = currentRunTree ?? getCurrentRunTree();
|
|
15
|
+
if (!runTree)
|
|
16
|
+
return undefined;
|
|
17
|
+
// TODO: CallbackManager.configure() is only async due to LangChainTracer
|
|
18
|
+
// factory being unnecessarily async.
|
|
19
|
+
let callbacks = await CallbackManager.configure();
|
|
20
|
+
if (!callbacks && runTree.tracingEnabled) {
|
|
21
|
+
callbacks = new CallbackManager();
|
|
22
|
+
}
|
|
23
|
+
let langChainTracer = callbacks?.handlers.find((handler) => handler?.name === "langchain_tracer");
|
|
24
|
+
if (!langChainTracer && runTree.tracingEnabled) {
|
|
25
|
+
langChainTracer = new LangChainTracer();
|
|
26
|
+
callbacks?.addHandler(langChainTracer);
|
|
27
|
+
}
|
|
28
|
+
const runMap = new Map();
|
|
29
|
+
// find upward root run
|
|
30
|
+
let rootRun = runTree;
|
|
31
|
+
const rootVisited = new Set();
|
|
32
|
+
while (rootRun.parent_run) {
|
|
33
|
+
if (rootVisited.has(rootRun.id))
|
|
34
|
+
break;
|
|
35
|
+
rootVisited.add(rootRun.id);
|
|
36
|
+
rootRun = rootRun.parent_run;
|
|
37
|
+
}
|
|
38
|
+
const queue = [rootRun];
|
|
39
|
+
const visited = new Set();
|
|
40
|
+
while (queue.length > 0) {
|
|
41
|
+
const current = queue.shift();
|
|
42
|
+
if (!current || visited.has(current.id))
|
|
43
|
+
continue;
|
|
44
|
+
visited.add(current.id);
|
|
45
|
+
runMap.set(current.id, current);
|
|
46
|
+
if (current.child_runs) {
|
|
47
|
+
queue.push(...current.child_runs);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (callbacks != null) {
|
|
51
|
+
Object.assign(callbacks, { _parentRunId: runTree.id });
|
|
52
|
+
}
|
|
53
|
+
if (langChainTracer != null) {
|
|
54
|
+
Object.assign(langChainTracer, {
|
|
55
|
+
runMap,
|
|
56
|
+
client: runTree.client,
|
|
57
|
+
projectName: runTree.project_name || langChainTracer.projectName,
|
|
58
|
+
exampleId: runTree.reference_example_id || langChainTracer.exampleId,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return callbacks;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* RunnableTraceable is a Runnable that wraps a traceable function.
|
|
65
|
+
* This allows adding Langsmith traced functions into LangChain sequences.
|
|
66
|
+
*/
|
|
67
|
+
export class RunnableTraceable extends Runnable {
|
|
68
|
+
constructor(fields) {
|
|
69
|
+
super(fields);
|
|
70
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
writable: true,
|
|
74
|
+
value: false
|
|
75
|
+
});
|
|
76
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
writable: true,
|
|
80
|
+
value: ["langchain_core", "runnables"]
|
|
81
|
+
});
|
|
82
|
+
Object.defineProperty(this, "func", {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
configurable: true,
|
|
85
|
+
writable: true,
|
|
86
|
+
value: void 0
|
|
87
|
+
});
|
|
88
|
+
if (!isTraceableFunction(fields.func)) {
|
|
89
|
+
throw new Error("RunnableTraceable requires a function that is wrapped in traceable higher-order function");
|
|
90
|
+
}
|
|
91
|
+
this.func = fields.func;
|
|
92
|
+
}
|
|
93
|
+
async invoke(input, options) {
|
|
94
|
+
const [config] = this._getOptionsList(options ?? {}, 1);
|
|
95
|
+
return (await this.func(config, input));
|
|
96
|
+
}
|
|
97
|
+
static from(func) {
|
|
98
|
+
return new RunnableTraceable({ func });
|
|
99
|
+
}
|
|
100
|
+
}
|
package/dist/run_trees.cjs
CHANGED
|
@@ -27,6 +27,7 @@ exports.isRunnableConfigLike = exports.isRunTree = exports.RunTree = exports.con
|
|
|
27
27
|
const uuid = __importStar(require("uuid"));
|
|
28
28
|
const env_js_1 = require("./utils/env.cjs");
|
|
29
29
|
const client_js_1 = require("./client.cjs");
|
|
30
|
+
const env_js_2 = require("./env.cjs");
|
|
30
31
|
const warnedMessages = {};
|
|
31
32
|
function warnOnce(message) {
|
|
32
33
|
if (!warnedMessages[message]) {
|
|
@@ -208,34 +209,42 @@ class RunTree {
|
|
|
208
209
|
}
|
|
209
210
|
}
|
|
210
211
|
}
|
|
211
|
-
static fromRunnableConfig(
|
|
212
|
+
static fromRunnableConfig(parentConfig, props) {
|
|
212
213
|
// We only handle the callback manager case for now
|
|
213
|
-
const callbackManager =
|
|
214
|
+
const callbackManager = parentConfig?.callbacks;
|
|
214
215
|
let parentRun;
|
|
215
216
|
let projectName;
|
|
217
|
+
let client;
|
|
218
|
+
let tracingEnabled = (0, env_js_2.isTracingEnabled)();
|
|
216
219
|
if (callbackManager) {
|
|
217
220
|
const parentRunId = callbackManager?.getParentRunId?.() ?? "";
|
|
218
221
|
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
|
|
219
222
|
parentRun = langChainTracer?.getRun?.(parentRunId);
|
|
220
223
|
projectName = langChainTracer?.projectName;
|
|
224
|
+
client = langChainTracer?.client;
|
|
225
|
+
tracingEnabled = tracingEnabled || !!langChainTracer;
|
|
221
226
|
}
|
|
222
|
-
const
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
parent_run: parentRun,
|
|
232
|
-
tags: dedupedTags,
|
|
227
|
+
const parentRunTree = new RunTree({
|
|
228
|
+
name: parentRun?.name ?? "<parent>",
|
|
229
|
+
id: parentRun?.id,
|
|
230
|
+
client,
|
|
231
|
+
tracingEnabled,
|
|
232
|
+
project_name: projectName,
|
|
233
|
+
tags: [
|
|
234
|
+
...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
|
|
235
|
+
],
|
|
233
236
|
extra: {
|
|
234
|
-
metadata:
|
|
237
|
+
metadata: {
|
|
238
|
+
...parentRun?.extra?.metadata,
|
|
239
|
+
...parentConfig?.metadata,
|
|
240
|
+
},
|
|
235
241
|
},
|
|
236
|
-
project_name: projectName,
|
|
237
242
|
});
|
|
238
|
-
return
|
|
243
|
+
return parentRunTree.createChild({
|
|
244
|
+
name: props?.name ?? "<lambda>",
|
|
245
|
+
tags: props.tags,
|
|
246
|
+
metadata: props.metadata,
|
|
247
|
+
});
|
|
239
248
|
}
|
|
240
249
|
static getDefaultConfig() {
|
|
241
250
|
return {
|
package/dist/run_trees.d.ts
CHANGED
|
@@ -66,7 +66,7 @@ export declare class RunTree implements BaseRun {
|
|
|
66
66
|
execution_order: number;
|
|
67
67
|
child_execution_order: number;
|
|
68
68
|
constructor(originalConfig: RunTreeConfig);
|
|
69
|
-
static fromRunnableConfig(
|
|
69
|
+
static fromRunnableConfig(parentConfig: RunnableConfigLike, props: {
|
|
70
70
|
name: string;
|
|
71
71
|
tags?: string[];
|
|
72
72
|
metadata?: KVMap;
|
package/dist/run_trees.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as uuid from "uuid";
|
|
2
2
|
import { getEnvironmentVariable, getRuntimeEnvironment, } from "./utils/env.js";
|
|
3
3
|
import { Client } from "./client.js";
|
|
4
|
+
import { isTracingEnabled } from "./env.js";
|
|
4
5
|
const warnedMessages = {};
|
|
5
6
|
function warnOnce(message) {
|
|
6
7
|
if (!warnedMessages[message]) {
|
|
@@ -181,34 +182,42 @@ export class RunTree {
|
|
|
181
182
|
}
|
|
182
183
|
}
|
|
183
184
|
}
|
|
184
|
-
static fromRunnableConfig(
|
|
185
|
+
static fromRunnableConfig(parentConfig, props) {
|
|
185
186
|
// We only handle the callback manager case for now
|
|
186
|
-
const callbackManager =
|
|
187
|
+
const callbackManager = parentConfig?.callbacks;
|
|
187
188
|
let parentRun;
|
|
188
189
|
let projectName;
|
|
190
|
+
let client;
|
|
191
|
+
let tracingEnabled = isTracingEnabled();
|
|
189
192
|
if (callbackManager) {
|
|
190
193
|
const parentRunId = callbackManager?.getParentRunId?.() ?? "";
|
|
191
194
|
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name == "langchain_tracer");
|
|
192
195
|
parentRun = langChainTracer?.getRun?.(parentRunId);
|
|
193
196
|
projectName = langChainTracer?.projectName;
|
|
197
|
+
client = langChainTracer?.client;
|
|
198
|
+
tracingEnabled = tracingEnabled || !!langChainTracer;
|
|
194
199
|
}
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
parent_run: parentRun,
|
|
205
|
-
tags: dedupedTags,
|
|
200
|
+
const parentRunTree = new RunTree({
|
|
201
|
+
name: parentRun?.name ?? "<parent>",
|
|
202
|
+
id: parentRun?.id,
|
|
203
|
+
client,
|
|
204
|
+
tracingEnabled,
|
|
205
|
+
project_name: projectName,
|
|
206
|
+
tags: [
|
|
207
|
+
...new Set((parentRun?.tags ?? []).concat(parentConfig?.tags ?? [])),
|
|
208
|
+
],
|
|
206
209
|
extra: {
|
|
207
|
-
metadata:
|
|
210
|
+
metadata: {
|
|
211
|
+
...parentRun?.extra?.metadata,
|
|
212
|
+
...parentConfig?.metadata,
|
|
213
|
+
},
|
|
208
214
|
},
|
|
209
|
-
project_name: projectName,
|
|
210
215
|
});
|
|
211
|
-
return
|
|
216
|
+
return parentRunTree.createChild({
|
|
217
|
+
name: props?.name ?? "<lambda>",
|
|
218
|
+
tags: props.tags,
|
|
219
|
+
metadata: props.metadata,
|
|
220
|
+
});
|
|
212
221
|
}
|
|
213
222
|
static getDefaultConfig() {
|
|
214
223
|
return {
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isTraceableFunction = exports.ROOT = exports.getCurrentRunTree = exports.AsyncLocalStorageProviderSingleton = void 0;
|
|
4
|
+
class MockAsyncLocalStorage {
|
|
5
|
+
getStore() {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
run(_, callback) {
|
|
9
|
+
return callback();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class AsyncLocalStorageProvider {
|
|
13
|
+
constructor() {
|
|
14
|
+
Object.defineProperty(this, "asyncLocalStorage", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: new MockAsyncLocalStorage()
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "hasBeenInitialized", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: false
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
getInstance() {
|
|
28
|
+
return this.asyncLocalStorage;
|
|
29
|
+
}
|
|
30
|
+
initializeGlobalInstance(instance) {
|
|
31
|
+
if (!this.hasBeenInitialized) {
|
|
32
|
+
this.hasBeenInitialized = true;
|
|
33
|
+
this.asyncLocalStorage = instance;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider();
|
|
38
|
+
/**
|
|
39
|
+
* Return the current run tree from within a traceable-wrapped function.
|
|
40
|
+
* Will throw an error if called outside of a traceable function.
|
|
41
|
+
*
|
|
42
|
+
* @returns The run tree for the given context.
|
|
43
|
+
*/
|
|
44
|
+
const getCurrentRunTree = () => {
|
|
45
|
+
const runTree = exports.AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
46
|
+
if (runTree === undefined) {
|
|
47
|
+
throw new Error([
|
|
48
|
+
"Could not get the current run tree.",
|
|
49
|
+
"",
|
|
50
|
+
"Please make sure you are calling this method within a traceable function.",
|
|
51
|
+
].join("\n"));
|
|
52
|
+
}
|
|
53
|
+
return runTree;
|
|
54
|
+
};
|
|
55
|
+
exports.getCurrentRunTree = getCurrentRunTree;
|
|
56
|
+
exports.ROOT = Symbol.for("langsmith:traceable:root");
|
|
57
|
+
function isTraceableFunction(x
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
+
) {
|
|
60
|
+
return typeof x === "function" && "langsmith:traceable" in x;
|
|
61
|
+
}
|
|
62
|
+
exports.isTraceableFunction = isTraceableFunction;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RunTree } from "../run_trees.js";
|
|
2
|
+
import { TraceableFunction } from "./types.js";
|
|
3
|
+
interface AsyncLocalStorageInterface {
|
|
4
|
+
getStore: () => RunTree | undefined;
|
|
5
|
+
run: (context: RunTree | undefined, fn: () => void) => void;
|
|
6
|
+
}
|
|
7
|
+
declare class AsyncLocalStorageProvider {
|
|
8
|
+
private asyncLocalStorage;
|
|
9
|
+
private hasBeenInitialized;
|
|
10
|
+
getInstance(): AsyncLocalStorageInterface;
|
|
11
|
+
initializeGlobalInstance(instance: AsyncLocalStorageInterface): void;
|
|
12
|
+
}
|
|
13
|
+
export declare const AsyncLocalStorageProviderSingleton: AsyncLocalStorageProvider;
|
|
14
|
+
/**
|
|
15
|
+
* Return the current run tree from within a traceable-wrapped function.
|
|
16
|
+
* Will throw an error if called outside of a traceable function.
|
|
17
|
+
*
|
|
18
|
+
* @returns The run tree for the given context.
|
|
19
|
+
*/
|
|
20
|
+
export declare const getCurrentRunTree: () => RunTree;
|
|
21
|
+
export declare const ROOT: unique symbol;
|
|
22
|
+
export declare function isTraceableFunction(x: unknown): x is TraceableFunction<any>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
class MockAsyncLocalStorage {
|
|
2
|
+
getStore() {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
run(_, callback) {
|
|
6
|
+
return callback();
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
class AsyncLocalStorageProvider {
|
|
10
|
+
constructor() {
|
|
11
|
+
Object.defineProperty(this, "asyncLocalStorage", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: new MockAsyncLocalStorage()
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "hasBeenInitialized", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: false
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
getInstance() {
|
|
25
|
+
return this.asyncLocalStorage;
|
|
26
|
+
}
|
|
27
|
+
initializeGlobalInstance(instance) {
|
|
28
|
+
if (!this.hasBeenInitialized) {
|
|
29
|
+
this.hasBeenInitialized = true;
|
|
30
|
+
this.asyncLocalStorage = instance;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export const AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider();
|
|
35
|
+
/**
|
|
36
|
+
* Return the current run tree from within a traceable-wrapped function.
|
|
37
|
+
* Will throw an error if called outside of a traceable function.
|
|
38
|
+
*
|
|
39
|
+
* @returns The run tree for the given context.
|
|
40
|
+
*/
|
|
41
|
+
export const getCurrentRunTree = () => {
|
|
42
|
+
const runTree = AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
43
|
+
if (runTree === undefined) {
|
|
44
|
+
throw new Error([
|
|
45
|
+
"Could not get the current run tree.",
|
|
46
|
+
"",
|
|
47
|
+
"Please make sure you are calling this method within a traceable function.",
|
|
48
|
+
].join("\n"));
|
|
49
|
+
}
|
|
50
|
+
return runTree;
|
|
51
|
+
};
|
|
52
|
+
export const ROOT = Symbol.for("langsmith:traceable:root");
|
|
53
|
+
export function isTraceableFunction(x
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
+
) {
|
|
56
|
+
return typeof x === "function" && "langsmith:traceable" in x;
|
|
57
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { RunTree, RunnableConfigLike } from "../run_trees.js";
|
|
2
|
+
import { ROOT } from "./traceable.js";
|
|
3
|
+
type SmartPromise<T> = T extends AsyncGenerator ? T : T extends Promise<unknown> ? T : Promise<T>;
|
|
4
|
+
type WrapArgReturnPair<Pair> = Pair extends [
|
|
5
|
+
infer Args extends any[],
|
|
6
|
+
infer Return
|
|
7
|
+
] ? Args extends [RunTree, ...infer RestArgs] ? {
|
|
8
|
+
(runTree: RunTree | typeof ROOT, ...args: RestArgs): SmartPromise<Return>;
|
|
9
|
+
(config: RunnableConfigLike, ...args: RestArgs): SmartPromise<Return>;
|
|
10
|
+
} : {
|
|
11
|
+
(...args: Args): SmartPromise<Return>;
|
|
12
|
+
(runTree: RunTree, ...rest: Args): SmartPromise<Return>;
|
|
13
|
+
(config: RunnableConfigLike, ...args: Args): SmartPromise<Return>;
|
|
14
|
+
} : never;
|
|
15
|
+
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
16
|
+
export type TraceableFunction<Func extends (...args: any[]) => any> = Func extends {
|
|
17
|
+
(...args: infer A1): infer R1;
|
|
18
|
+
(...args: infer A2): infer R2;
|
|
19
|
+
(...args: infer A3): infer R3;
|
|
20
|
+
(...args: infer A4): infer R4;
|
|
21
|
+
(...args: infer A5): infer R5;
|
|
22
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3] | [A4, R4] | [A5, R5]>> : Func extends {
|
|
23
|
+
(...args: infer A1): infer R1;
|
|
24
|
+
(...args: infer A2): infer R2;
|
|
25
|
+
(...args: infer A3): infer R3;
|
|
26
|
+
(...args: infer A4): infer R4;
|
|
27
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3] | [A4, R4]>> : Func extends {
|
|
28
|
+
(...args: infer A1): infer R1;
|
|
29
|
+
(...args: infer A2): infer R2;
|
|
30
|
+
(...args: infer A3): infer R3;
|
|
31
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3]>> : Func extends {
|
|
32
|
+
(...args: infer A1): infer R1;
|
|
33
|
+
(...args: infer A2): infer R2;
|
|
34
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2]>> : Func extends {
|
|
35
|
+
(...args: infer A1): infer R1;
|
|
36
|
+
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1]>> : never;
|
|
37
|
+
export type RunTreeLike = RunTree;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/traceable.cjs
CHANGED
|
@@ -1,26 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.ROOT = exports.isTraceableFunction = exports.getCurrentRunTree = exports.traceable = void 0;
|
|
4
4
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
5
5
|
const run_trees_js_1 = require("./run_trees.cjs");
|
|
6
|
-
const env_js_1 = require("./
|
|
6
|
+
const env_js_1 = require("./env.cjs");
|
|
7
|
+
const traceable_js_1 = require("./singletons/traceable.cjs");
|
|
8
|
+
traceable_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
|
|
7
9
|
function isPromiseMethod(x) {
|
|
8
10
|
if (x === "then" || x === "catch" || x === "finally") {
|
|
9
11
|
return true;
|
|
10
12
|
}
|
|
11
13
|
return false;
|
|
12
14
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
function isKVMap(x) {
|
|
16
|
+
if (typeof x !== "object" || x == null) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
const prototype = Object.getPrototypeOf(x);
|
|
20
|
+
return ((prototype === null ||
|
|
21
|
+
prototype === Object.prototype ||
|
|
22
|
+
Object.getPrototypeOf(prototype) === null) &&
|
|
23
|
+
!(Symbol.toStringTag in x) &&
|
|
24
|
+
!(Symbol.iterator in x));
|
|
25
|
+
}
|
|
15
26
|
const isAsyncIterable = (x) => x != null &&
|
|
16
27
|
typeof x === "object" &&
|
|
17
28
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
29
|
typeof x[Symbol.asyncIterator] === "function";
|
|
19
|
-
const GeneratorFunction = function* () { }.constructor;
|
|
20
30
|
const isIteratorLike = (x) => x != null &&
|
|
21
31
|
typeof x === "object" &&
|
|
22
32
|
"next" in x &&
|
|
23
33
|
typeof x.next === "function";
|
|
34
|
+
const GeneratorFunction = function* () { }.constructor;
|
|
24
35
|
const isGenerator = (x) =>
|
|
25
36
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
26
37
|
x != null && typeof x === "function" && x instanceof GeneratorFunction;
|
|
@@ -32,18 +43,6 @@ const isReadableStream = (x) => x != null &&
|
|
|
32
43
|
typeof x === "object" &&
|
|
33
44
|
"getReader" in x &&
|
|
34
45
|
typeof x.getReader === "function";
|
|
35
|
-
const tracingIsEnabled = (tracingEnabled) => {
|
|
36
|
-
if (tracingEnabled !== undefined) {
|
|
37
|
-
return tracingEnabled;
|
|
38
|
-
}
|
|
39
|
-
const envVars = [
|
|
40
|
-
"LANGSMITH_TRACING_V2",
|
|
41
|
-
"LANGCHAIN_TRACING_V2",
|
|
42
|
-
"LANGSMITH_TRACING",
|
|
43
|
-
"LANGCHAIN_TRACING",
|
|
44
|
-
];
|
|
45
|
-
return Boolean(envVars.find((envVar) => (0, env_js_1.getEnvironmentVariable)(envVar) === "true"));
|
|
46
|
-
};
|
|
47
46
|
const handleRunInputs = (rawInputs) => {
|
|
48
47
|
const firstInput = rawInputs[0];
|
|
49
48
|
if (firstInput == null) {
|
|
@@ -64,8 +63,7 @@ const handleRunOutputs = (rawOutputs) => {
|
|
|
64
63
|
return { outputs: rawOutputs };
|
|
65
64
|
};
|
|
66
65
|
const getTracingRunTree = (runTree, inputs, getInvocationParams) => {
|
|
67
|
-
|
|
68
|
-
if (!tracingEnabled_) {
|
|
66
|
+
if (!(0, env_js_1.isTracingEnabled)(runTree.tracingEnabled)) {
|
|
69
67
|
return undefined;
|
|
70
68
|
}
|
|
71
69
|
runTree.inputs = handleRunInputs(inputs);
|
|
@@ -277,6 +275,7 @@ function traceable(wrappedFunc, config) {
|
|
|
277
275
|
...runTreeConfig,
|
|
278
276
|
};
|
|
279
277
|
}
|
|
278
|
+
const asyncLocalStorage = traceable_js_1.AsyncLocalStorageProviderSingleton.getInstance();
|
|
280
279
|
// TODO: deal with possible nested promises and async iterables
|
|
281
280
|
const processedArgs = args;
|
|
282
281
|
for (let i = 0; i < processedArgs.length; i++) {
|
|
@@ -291,7 +290,7 @@ function traceable(wrappedFunc, config) {
|
|
|
291
290
|
restArgs,
|
|
292
291
|
];
|
|
293
292
|
}
|
|
294
|
-
// legacy CallbackManagerRunTree used in runOnDataset
|
|
293
|
+
// deprecated: legacy CallbackManagerRunTree used in runOnDataset
|
|
295
294
|
// override ALS and do not pass-through the run tree
|
|
296
295
|
if ((0, run_trees_js_1.isRunTree)(firstArg) &&
|
|
297
296
|
"callbackManager" in firstArg &&
|
|
@@ -300,8 +299,8 @@ function traceable(wrappedFunc, config) {
|
|
|
300
299
|
}
|
|
301
300
|
// when ALS is unreliable, users can manually
|
|
302
301
|
// pass in the run tree
|
|
303
|
-
if (firstArg ===
|
|
304
|
-
const currentRunTree = getTracingRunTree(firstArg ===
|
|
302
|
+
if (firstArg === traceable_js_1.ROOT || (0, run_trees_js_1.isRunTree)(firstArg)) {
|
|
303
|
+
const currentRunTree = getTracingRunTree(firstArg === traceable_js_1.ROOT
|
|
305
304
|
? new run_trees_js_1.RunTree(ensuredConfig)
|
|
306
305
|
: firstArg.createChild(ensuredConfig), restArgs, config?.getInvocationParams);
|
|
307
306
|
return [currentRunTree, [currentRunTree, ...restArgs]];
|
|
@@ -452,48 +451,7 @@ function traceable(wrappedFunc, config) {
|
|
|
452
451
|
return traceableFunc;
|
|
453
452
|
}
|
|
454
453
|
exports.traceable = traceable;
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
* @returns The run tree for the given context.
|
|
460
|
-
*/
|
|
461
|
-
function getCurrentRunTree() {
|
|
462
|
-
const runTree = asyncLocalStorage.getStore();
|
|
463
|
-
if (runTree === undefined) {
|
|
464
|
-
throw new Error([
|
|
465
|
-
"Could not get the current run tree.",
|
|
466
|
-
"",
|
|
467
|
-
"Please make sure you are calling this method within a traceable function.",
|
|
468
|
-
].join("\n"));
|
|
469
|
-
}
|
|
470
|
-
return runTree;
|
|
471
|
-
}
|
|
472
|
-
exports.getCurrentRunTree = getCurrentRunTree;
|
|
473
|
-
function isTraceableFunction(x
|
|
474
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
475
|
-
) {
|
|
476
|
-
return typeof x === "function" && "langsmith:traceable" in x;
|
|
477
|
-
}
|
|
478
|
-
exports.isTraceableFunction = isTraceableFunction;
|
|
479
|
-
function isKVMap(x) {
|
|
480
|
-
if (typeof x !== "object" || x == null) {
|
|
481
|
-
return false;
|
|
482
|
-
}
|
|
483
|
-
const prototype = Object.getPrototypeOf(x);
|
|
484
|
-
return ((prototype === null ||
|
|
485
|
-
prototype === Object.prototype ||
|
|
486
|
-
Object.getPrototypeOf(prototype) === null) &&
|
|
487
|
-
!(Symbol.toStringTag in x) &&
|
|
488
|
-
!(Symbol.iterator in x));
|
|
489
|
-
}
|
|
490
|
-
function wrapFunctionAndEnsureTraceable(target, options, name = "target") {
|
|
491
|
-
if (typeof target === "function") {
|
|
492
|
-
return traceable(target, {
|
|
493
|
-
...options,
|
|
494
|
-
name,
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
throw new Error("Target must be runnable function");
|
|
498
|
-
}
|
|
499
|
-
exports.wrapFunctionAndEnsureTraceable = wrapFunctionAndEnsureTraceable;
|
|
454
|
+
var traceable_js_2 = require("./singletons/traceable.cjs");
|
|
455
|
+
Object.defineProperty(exports, "getCurrentRunTree", { enumerable: true, get: function () { return traceable_js_2.getCurrentRunTree; } });
|
|
456
|
+
Object.defineProperty(exports, "isTraceableFunction", { enumerable: true, get: function () { return traceable_js_2.isTraceableFunction; } });
|
|
457
|
+
Object.defineProperty(exports, "ROOT", { enumerable: true, get: function () { return traceable_js_2.ROOT; } });
|
package/dist/traceable.d.ts
CHANGED
|
@@ -1,41 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RunTreeConfig } from "./run_trees.js";
|
|
2
2
|
import { InvocationParamsSchema } from "./schemas.js";
|
|
3
|
-
|
|
4
|
-
export type RunTreeLike = RunTree;
|
|
5
|
-
type SmartPromise<T> = T extends AsyncGenerator ? T : T extends Promise<unknown> ? T : Promise<T>;
|
|
6
|
-
type WrapArgReturnPair<Pair> = Pair extends [
|
|
7
|
-
infer Args extends any[],
|
|
8
|
-
infer Return
|
|
9
|
-
] ? Args extends [RunTreeLike, ...infer RestArgs] ? {
|
|
10
|
-
(runTree: RunTreeLike | typeof ROOT, ...args: RestArgs): SmartPromise<Return>;
|
|
11
|
-
(config: RunnableConfigLike, ...args: RestArgs): SmartPromise<Return>;
|
|
12
|
-
} : {
|
|
13
|
-
(...args: Args): SmartPromise<Return>;
|
|
14
|
-
(runTree: RunTreeLike, ...rest: Args): SmartPromise<Return>;
|
|
15
|
-
(config: RunnableConfigLike, ...args: Args): SmartPromise<Return>;
|
|
16
|
-
} : never;
|
|
17
|
-
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
18
|
-
export type TraceableFunction<Func extends (...args: any[]) => any> = Func extends {
|
|
19
|
-
(...args: infer A1): infer R1;
|
|
20
|
-
(...args: infer A2): infer R2;
|
|
21
|
-
(...args: infer A3): infer R3;
|
|
22
|
-
(...args: infer A4): infer R4;
|
|
23
|
-
(...args: infer A5): infer R5;
|
|
24
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3] | [A4, R4] | [A5, R5]>> : Func extends {
|
|
25
|
-
(...args: infer A1): infer R1;
|
|
26
|
-
(...args: infer A2): infer R2;
|
|
27
|
-
(...args: infer A3): infer R3;
|
|
28
|
-
(...args: infer A4): infer R4;
|
|
29
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3] | [A4, R4]>> : Func extends {
|
|
30
|
-
(...args: infer A1): infer R1;
|
|
31
|
-
(...args: infer A2): infer R2;
|
|
32
|
-
(...args: infer A3): infer R3;
|
|
33
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2] | [A3, R3]>> : Func extends {
|
|
34
|
-
(...args: infer A1): infer R1;
|
|
35
|
-
(...args: infer A2): infer R2;
|
|
36
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1] | [A2, R2]>> : Func extends {
|
|
37
|
-
(...args: infer A1): infer R1;
|
|
38
|
-
} ? UnionToIntersection<WrapArgReturnPair<[A1, R1]>> : never;
|
|
3
|
+
import { TraceableFunction } from "./singletons/types.js";
|
|
39
4
|
/**
|
|
40
5
|
* Higher-order function that takes function as input and returns a
|
|
41
6
|
* "TraceableFunction" - a wrapped version of the input that
|
|
@@ -63,13 +28,5 @@ export declare function traceable<Func extends (...args: any[]) => any>(wrappedF
|
|
|
63
28
|
*/
|
|
64
29
|
getInvocationParams?: (...args: Parameters<Func>) => InvocationParamsSchema | undefined;
|
|
65
30
|
}): TraceableFunction<Func>;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
* Will throw an error if called outside of a traceable function.
|
|
69
|
-
*
|
|
70
|
-
* @returns The run tree for the given context.
|
|
71
|
-
*/
|
|
72
|
-
export declare function getCurrentRunTree(): RunTree;
|
|
73
|
-
export declare function isTraceableFunction(x: unknown): x is TraceableFunction<any>;
|
|
74
|
-
export declare function wrapFunctionAndEnsureTraceable<Func extends (...args: any[]) => any>(target: Func, options: Partial<RunTreeConfig>, name?: string): TraceableFunction<Func>;
|
|
75
|
-
export {};
|
|
31
|
+
export { getCurrentRunTree, isTraceableFunction, ROOT, } from "./singletons/traceable.js";
|
|
32
|
+
export type { RunTreeLike, TraceableFunction } from "./singletons/types.js";
|
package/dist/traceable.js
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import { RunTree, isRunTree, isRunnableConfigLike, } from "./run_trees.js";
|
|
3
|
-
import {
|
|
3
|
+
import { isTracingEnabled } from "./env.js";
|
|
4
|
+
import { ROOT, AsyncLocalStorageProviderSingleton, } from "./singletons/traceable.js";
|
|
5
|
+
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
4
6
|
function isPromiseMethod(x) {
|
|
5
7
|
if (x === "then" || x === "catch" || x === "finally") {
|
|
6
8
|
return true;
|
|
7
9
|
}
|
|
8
10
|
return false;
|
|
9
11
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
function isKVMap(x) {
|
|
13
|
+
if (typeof x !== "object" || x == null) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
const prototype = Object.getPrototypeOf(x);
|
|
17
|
+
return ((prototype === null ||
|
|
18
|
+
prototype === Object.prototype ||
|
|
19
|
+
Object.getPrototypeOf(prototype) === null) &&
|
|
20
|
+
!(Symbol.toStringTag in x) &&
|
|
21
|
+
!(Symbol.iterator in x));
|
|
22
|
+
}
|
|
12
23
|
const isAsyncIterable = (x) => x != null &&
|
|
13
24
|
typeof x === "object" &&
|
|
14
25
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
26
|
typeof x[Symbol.asyncIterator] === "function";
|
|
16
|
-
const GeneratorFunction = function* () { }.constructor;
|
|
17
27
|
const isIteratorLike = (x) => x != null &&
|
|
18
28
|
typeof x === "object" &&
|
|
19
29
|
"next" in x &&
|
|
20
30
|
typeof x.next === "function";
|
|
31
|
+
const GeneratorFunction = function* () { }.constructor;
|
|
21
32
|
const isGenerator = (x) =>
|
|
22
33
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
23
34
|
x != null && typeof x === "function" && x instanceof GeneratorFunction;
|
|
@@ -29,18 +40,6 @@ const isReadableStream = (x) => x != null &&
|
|
|
29
40
|
typeof x === "object" &&
|
|
30
41
|
"getReader" in x &&
|
|
31
42
|
typeof x.getReader === "function";
|
|
32
|
-
const tracingIsEnabled = (tracingEnabled) => {
|
|
33
|
-
if (tracingEnabled !== undefined) {
|
|
34
|
-
return tracingEnabled;
|
|
35
|
-
}
|
|
36
|
-
const envVars = [
|
|
37
|
-
"LANGSMITH_TRACING_V2",
|
|
38
|
-
"LANGCHAIN_TRACING_V2",
|
|
39
|
-
"LANGSMITH_TRACING",
|
|
40
|
-
"LANGCHAIN_TRACING",
|
|
41
|
-
];
|
|
42
|
-
return Boolean(envVars.find((envVar) => getEnvironmentVariable(envVar) === "true"));
|
|
43
|
-
};
|
|
44
43
|
const handleRunInputs = (rawInputs) => {
|
|
45
44
|
const firstInput = rawInputs[0];
|
|
46
45
|
if (firstInput == null) {
|
|
@@ -61,8 +60,7 @@ const handleRunOutputs = (rawOutputs) => {
|
|
|
61
60
|
return { outputs: rawOutputs };
|
|
62
61
|
};
|
|
63
62
|
const getTracingRunTree = (runTree, inputs, getInvocationParams) => {
|
|
64
|
-
|
|
65
|
-
if (!tracingEnabled_) {
|
|
63
|
+
if (!isTracingEnabled(runTree.tracingEnabled)) {
|
|
66
64
|
return undefined;
|
|
67
65
|
}
|
|
68
66
|
runTree.inputs = handleRunInputs(inputs);
|
|
@@ -274,6 +272,7 @@ export function traceable(wrappedFunc, config) {
|
|
|
274
272
|
...runTreeConfig,
|
|
275
273
|
};
|
|
276
274
|
}
|
|
275
|
+
const asyncLocalStorage = AsyncLocalStorageProviderSingleton.getInstance();
|
|
277
276
|
// TODO: deal with possible nested promises and async iterables
|
|
278
277
|
const processedArgs = args;
|
|
279
278
|
for (let i = 0; i < processedArgs.length; i++) {
|
|
@@ -288,7 +287,7 @@ export function traceable(wrappedFunc, config) {
|
|
|
288
287
|
restArgs,
|
|
289
288
|
];
|
|
290
289
|
}
|
|
291
|
-
// legacy CallbackManagerRunTree used in runOnDataset
|
|
290
|
+
// deprecated: legacy CallbackManagerRunTree used in runOnDataset
|
|
292
291
|
// override ALS and do not pass-through the run tree
|
|
293
292
|
if (isRunTree(firstArg) &&
|
|
294
293
|
"callbackManager" in firstArg &&
|
|
@@ -448,45 +447,4 @@ export function traceable(wrappedFunc, config) {
|
|
|
448
447
|
});
|
|
449
448
|
return traceableFunc;
|
|
450
449
|
}
|
|
451
|
-
|
|
452
|
-
* Return the current run tree from within a traceable-wrapped function.
|
|
453
|
-
* Will throw an error if called outside of a traceable function.
|
|
454
|
-
*
|
|
455
|
-
* @returns The run tree for the given context.
|
|
456
|
-
*/
|
|
457
|
-
export function getCurrentRunTree() {
|
|
458
|
-
const runTree = asyncLocalStorage.getStore();
|
|
459
|
-
if (runTree === undefined) {
|
|
460
|
-
throw new Error([
|
|
461
|
-
"Could not get the current run tree.",
|
|
462
|
-
"",
|
|
463
|
-
"Please make sure you are calling this method within a traceable function.",
|
|
464
|
-
].join("\n"));
|
|
465
|
-
}
|
|
466
|
-
return runTree;
|
|
467
|
-
}
|
|
468
|
-
export function isTraceableFunction(x
|
|
469
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
470
|
-
) {
|
|
471
|
-
return typeof x === "function" && "langsmith:traceable" in x;
|
|
472
|
-
}
|
|
473
|
-
function isKVMap(x) {
|
|
474
|
-
if (typeof x !== "object" || x == null) {
|
|
475
|
-
return false;
|
|
476
|
-
}
|
|
477
|
-
const prototype = Object.getPrototypeOf(x);
|
|
478
|
-
return ((prototype === null ||
|
|
479
|
-
prototype === Object.prototype ||
|
|
480
|
-
Object.getPrototypeOf(prototype) === null) &&
|
|
481
|
-
!(Symbol.toStringTag in x) &&
|
|
482
|
-
!(Symbol.iterator in x));
|
|
483
|
-
}
|
|
484
|
-
export function wrapFunctionAndEnsureTraceable(target, options, name = "target") {
|
|
485
|
-
if (typeof target === "function") {
|
|
486
|
-
return traceable(target, {
|
|
487
|
-
...options,
|
|
488
|
-
name,
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
throw new Error("Target must be runnable function");
|
|
492
|
-
}
|
|
450
|
+
export { getCurrentRunTree, isTraceableFunction, ROOT, } from "./singletons/traceable.js";
|
package/langchain.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/langchain.cjs');
|
package/langchain.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/langchain.js'
|
package/langchain.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/langchain.js'
|
package/langchain.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/langchain.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "langsmith",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.27",
|
|
4
4
|
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
|
|
5
5
|
"packageManager": "yarn@1.22.19",
|
|
6
6
|
"files": [
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
"schemas.js",
|
|
26
26
|
"schemas.d.ts",
|
|
27
27
|
"schemas.d.cts",
|
|
28
|
+
"langchain.cjs",
|
|
29
|
+
"langchain.js",
|
|
30
|
+
"langchain.d.ts",
|
|
31
|
+
"langchain.d.cts",
|
|
28
32
|
"wrappers.cjs",
|
|
29
33
|
"wrappers.js",
|
|
30
34
|
"wrappers.d.ts",
|
|
@@ -33,6 +37,10 @@
|
|
|
33
37
|
"wrappers/openai.js",
|
|
34
38
|
"wrappers/openai.d.ts",
|
|
35
39
|
"wrappers/openai.d.cts",
|
|
40
|
+
"singletons/traceable.cjs",
|
|
41
|
+
"singletons/traceable.js",
|
|
42
|
+
"singletons/traceable.d.ts",
|
|
43
|
+
"singletons/traceable.d.cts",
|
|
36
44
|
"index.cjs",
|
|
37
45
|
"index.js",
|
|
38
46
|
"index.d.ts",
|
|
@@ -109,11 +117,15 @@
|
|
|
109
117
|
"typescript": "^5.4.5"
|
|
110
118
|
},
|
|
111
119
|
"peerDependencies": {
|
|
112
|
-
"openai": "*"
|
|
120
|
+
"openai": "*",
|
|
121
|
+
"@langchain/core": "*"
|
|
113
122
|
},
|
|
114
123
|
"peerDependenciesMeta": {
|
|
115
124
|
"openai": {
|
|
116
125
|
"optional": true
|
|
126
|
+
},
|
|
127
|
+
"@langchain/core": {
|
|
128
|
+
"optional": true
|
|
117
129
|
}
|
|
118
130
|
},
|
|
119
131
|
"lint-staged": {
|
|
@@ -177,6 +189,15 @@
|
|
|
177
189
|
"import": "./schemas.js",
|
|
178
190
|
"require": "./schemas.cjs"
|
|
179
191
|
},
|
|
192
|
+
"./langchain": {
|
|
193
|
+
"types": {
|
|
194
|
+
"import": "./langchain.d.ts",
|
|
195
|
+
"require": "./langchain.d.cts",
|
|
196
|
+
"default": "./langchain.d.ts"
|
|
197
|
+
},
|
|
198
|
+
"import": "./langchain.js",
|
|
199
|
+
"require": "./langchain.cjs"
|
|
200
|
+
},
|
|
180
201
|
"./wrappers": {
|
|
181
202
|
"types": {
|
|
182
203
|
"import": "./wrappers.d.ts",
|
|
@@ -195,6 +216,15 @@
|
|
|
195
216
|
"import": "./wrappers/openai.js",
|
|
196
217
|
"require": "./wrappers/openai.cjs"
|
|
197
218
|
},
|
|
219
|
+
"./singletons/traceable": {
|
|
220
|
+
"types": {
|
|
221
|
+
"import": "./singletons/traceable.d.ts",
|
|
222
|
+
"require": "./singletons/traceable.d.cts",
|
|
223
|
+
"default": "./singletons/traceable.d.ts"
|
|
224
|
+
},
|
|
225
|
+
"import": "./singletons/traceable.js",
|
|
226
|
+
"require": "./singletons/traceable.cjs"
|
|
227
|
+
},
|
|
198
228
|
"./package.json": "./package.json"
|
|
199
229
|
}
|
|
200
230
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/singletons/traceable.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/singletons/traceable.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/singletons/traceable.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/singletons/traceable.js'
|