@temporalio/langsmith 1.24.0 → 2.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +6 -6
- package/README.md +79 -6
- package/lib/activity-interceptor.js +38 -47
- package/lib/activity-interceptor.js.map +1 -1
- package/lib/client-interceptor.d.ts +4 -4
- package/lib/client-interceptor.js +28 -47
- package/lib/client-interceptor.js.map +1 -1
- package/lib/plugin.d.ts +7 -6
- package/lib/plugin.js +85 -9
- package/lib/plugin.js.map +1 -1
- package/lib/propagation.js +1 -1
- package/lib/routing.d.ts +14 -0
- package/lib/routing.js +101 -0
- package/lib/routing.js.map +1 -0
- package/lib/run-tree.d.ts +48 -13
- package/lib/run-tree.js +106 -166
- package/lib/run-tree.js.map +1 -1
- package/lib/sinks.d.ts +19 -4
- package/lib/sinks.js +33 -50
- package/lib/sinks.js.map +1 -1
- package/lib/worker-run-tree.d.ts +6 -0
- package/lib/worker-run-tree.js +43 -0
- package/lib/worker-run-tree.js.map +1 -0
- package/lib/workflow-interceptors.js +104 -151
- package/lib/workflow-interceptors.js.map +1 -1
- package/lib/workflow-run-tree.d.ts +7 -0
- package/lib/workflow-run-tree.js +112 -0
- package/lib/workflow-run-tree.js.map +1 -0
- package/package.json +37 -18
- package/src/activity-interceptor.ts +47 -51
- package/src/client-interceptor.ts +37 -59
- package/src/plugin.ts +99 -14
- package/src/propagation.ts +1 -1
- package/src/routing.ts +110 -0
- package/src/run-tree.ts +139 -185
- package/src/sinks.ts +46 -52
- package/src/worker-run-tree.ts +47 -0
- package/src/workflow-interceptors.ts +131 -217
- package/src/workflow-run-tree.ts +127 -0
package/lib/plugin.js
CHANGED
|
@@ -21,15 +21,32 @@
|
|
|
21
21
|
*/
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
exports.LangSmithPlugin = void 0;
|
|
24
|
+
exports.envDestination = envDestination;
|
|
24
25
|
const langsmith_1 = require("langsmith");
|
|
26
|
+
const run_trees_1 = require("langsmith/run_trees");
|
|
25
27
|
const plugin_1 = require("@temporalio/plugin");
|
|
28
|
+
const worker_1 = require("@temporalio/worker");
|
|
26
29
|
const activity_interceptor_1 = require("./activity-interceptor");
|
|
27
30
|
const client_interceptor_1 = require("./client-interceptor");
|
|
28
31
|
const sinks_1 = require("./sinks");
|
|
32
|
+
const run_tree_1 = require("./run-tree");
|
|
33
|
+
const worker_run_tree_1 = require("./worker-run-tree");
|
|
34
|
+
const workflow_run_tree_1 = require("./workflow-run-tree");
|
|
35
|
+
const routing_1 = require("./routing");
|
|
29
36
|
// Bare identifier (not dotted) so DefinePlugin substitutes textually.
|
|
30
37
|
const CONFIG_GLOBAL = '__TEMPORAL_LANGSMITH_CONFIG__';
|
|
31
38
|
// Bare builtin name added to `ignoreModules`; see {@link bundleRewritesPlugin}.
|
|
32
39
|
const ASYNC_HOOKS_MODULE = 'async_hooks';
|
|
40
|
+
/** Reads this package's version, which worker heartbeats report. Empty if it can't be read. */
|
|
41
|
+
function packageVersion() {
|
|
42
|
+
try {
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
44
|
+
return require('../package.json').version ?? '';
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return '';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
33
50
|
/**
|
|
34
51
|
* Temporal plugin that adds LangSmith observability to a Client + Worker.
|
|
35
52
|
*
|
|
@@ -41,13 +58,14 @@ const ASYNC_HOOKS_MODULE = 'async_hooks';
|
|
|
41
58
|
* const plugin = new LangSmithPlugin({ addTemporalRuns: true });
|
|
42
59
|
* const worker = await Worker.create({ workflowsPath, taskQueue: 'tq', plugins: [plugin] });
|
|
43
60
|
* ```
|
|
44
|
-
*
|
|
45
|
-
* @experimental Plugins is an experimental feature; APIs may change without notice.
|
|
46
61
|
*/
|
|
47
62
|
class LangSmithPlugin extends plugin_1.SimplePlugin {
|
|
63
|
+
/** The package version, reported in worker heartbeats. */
|
|
64
|
+
version = packageVersion();
|
|
48
65
|
client;
|
|
49
66
|
emitter;
|
|
50
67
|
workflowConfig;
|
|
68
|
+
drains = [];
|
|
51
69
|
// Absolute paths so the workflow bundler resolves them under strict pnpm.
|
|
52
70
|
workflowInterceptorModule = require.resolve('./workflow-interceptors');
|
|
53
71
|
// Target of the `node:async_hooks` rewrite; re-exports the runtime-injected ALS.
|
|
@@ -55,18 +73,36 @@ class LangSmithPlugin extends plugin_1.SimplePlugin {
|
|
|
55
73
|
constructor(options = {}) {
|
|
56
74
|
// super() reads options.name immediately
|
|
57
75
|
super({ name: 'langchain.LangSmithPlugin' });
|
|
76
|
+
if (options.projectName !== undefined && options.address !== undefined) {
|
|
77
|
+
throw new TypeError('projectName and address are mutually exclusive');
|
|
78
|
+
}
|
|
58
79
|
this.client = options.client ?? new langsmith_1.Client();
|
|
80
|
+
const defaults = new run_trees_1.RunTree({
|
|
81
|
+
name: 'temporal-routing-defaults',
|
|
82
|
+
client: this.client,
|
|
83
|
+
project_name: options.projectName,
|
|
84
|
+
address: options.address,
|
|
85
|
+
});
|
|
86
|
+
// Let LangSmith validate and normalize its own address objects.
|
|
87
|
+
const address = options.address === undefined ? undefined : defaults.address;
|
|
88
|
+
const replicas = defaults.replicas;
|
|
59
89
|
const addTemporalRuns = options.addTemporalRuns ?? false;
|
|
60
90
|
this.emitter = {
|
|
61
91
|
client: this.client,
|
|
62
92
|
addTemporalRuns,
|
|
63
|
-
|
|
93
|
+
// LangSmith treats an empty project name as unset, so the plugin does too.
|
|
94
|
+
projectName: options.projectName || undefined,
|
|
95
|
+
address,
|
|
96
|
+
replicas,
|
|
64
97
|
tags: options.tags,
|
|
65
98
|
metadata: options.metadata,
|
|
66
99
|
};
|
|
67
100
|
this.workflowConfig = {
|
|
68
101
|
addTemporalRuns,
|
|
69
|
-
|
|
102
|
+
// Resolved here, where LangSmith can read env; inside the isolate it can only fall back to `"default"`.
|
|
103
|
+
destination: pluginDestination(this.client, address, this.emitter.projectName),
|
|
104
|
+
tracingEnabled: (0, langsmith_1.isTracingEnabled)(),
|
|
105
|
+
replicas: (0, routing_1.replicaRouting)(replicas),
|
|
70
106
|
tags: options.tags,
|
|
71
107
|
metadata: options.metadata,
|
|
72
108
|
};
|
|
@@ -85,9 +121,10 @@ class LangSmithPlugin extends plugin_1.SimplePlugin {
|
|
|
85
121
|
return this.withLangSmithWorker(super.configureReplayWorker(options));
|
|
86
122
|
}
|
|
87
123
|
withLangSmithWorker(options) {
|
|
124
|
+
(0, run_tree_1.requireRunTreeMembers)(workflow_run_tree_1.WORKFLOW_RUN_TREE_OVERRIDES, worker_run_tree_1.WORKER_RUN_TREE_OVERRIDES);
|
|
88
125
|
const interceptors = options.interceptors ?? {};
|
|
89
|
-
const
|
|
90
|
-
|
|
126
|
+
const activity = [...(interceptors.activity ?? [])];
|
|
127
|
+
activity.push((ctx) => ({ inbound: (0, activity_interceptor_1.createActivityInboundInterceptor)(this.emitter)(ctx) }));
|
|
91
128
|
const workflowModules = [...(interceptors.workflowModules ?? [])];
|
|
92
129
|
if (!workflowModules.includes(this.workflowInterceptorModule)) {
|
|
93
130
|
workflowModules.push(this.workflowInterceptorModule);
|
|
@@ -95,11 +132,18 @@ class LangSmithPlugin extends plugin_1.SimplePlugin {
|
|
|
95
132
|
const nexusInbound = (0, activity_interceptor_1.createNexusInboundInterceptor)(this.emitter);
|
|
96
133
|
const nexusInterceptors = [...(interceptors.nexus ?? [])];
|
|
97
134
|
nexusInterceptors.push((_ctx) => ({ inbound: nexusInbound }));
|
|
135
|
+
const logger = worker_1.Runtime.instance().logger;
|
|
136
|
+
if (this.workflowConfig.destination === undefined) {
|
|
137
|
+
logger.warn("LangSmith can't name a destination from the environment, so the Temporal plugin traces nothing that starts " +
|
|
138
|
+
'without a parent run.');
|
|
139
|
+
}
|
|
140
|
+
const { sinks: langsmithSinks, drain } = (0, sinks_1.createLangSmithSinks)(this.client, this.emitter.replicas, logger);
|
|
141
|
+
this.drains.push(drain);
|
|
98
142
|
// The user's own Worker sinks take precedence on any key collision.
|
|
99
|
-
const sinks = { ...
|
|
143
|
+
const sinks = { ...langsmithSinks, ...(options.sinks ?? {}) };
|
|
100
144
|
return {
|
|
101
145
|
...options,
|
|
102
|
-
interceptors: { ...interceptors,
|
|
146
|
+
interceptors: { ...interceptors, activity, workflowModules, nexus: nexusInterceptors },
|
|
103
147
|
sinks,
|
|
104
148
|
};
|
|
105
149
|
}
|
|
@@ -140,6 +184,8 @@ class LangSmithPlugin extends plugin_1.SimplePlugin {
|
|
|
140
184
|
}
|
|
141
185
|
async flush() {
|
|
142
186
|
try {
|
|
187
|
+
// Sink writes reach the client one at a time, so hand them all over before flushing it.
|
|
188
|
+
await Promise.all(this.drains.map((drain) => drain()));
|
|
143
189
|
await this.client.awaitPendingTraceBatches();
|
|
144
190
|
}
|
|
145
191
|
catch {
|
|
@@ -148,9 +194,36 @@ class LangSmithPlugin extends plugin_1.SimplePlugin {
|
|
|
148
194
|
}
|
|
149
195
|
}
|
|
150
196
|
exports.LangSmithPlugin = LangSmithPlugin;
|
|
197
|
+
/** The plugin's explicit `address` or `projectName`, else the destination the env names. */
|
|
198
|
+
function pluginDestination(client, address, projectName) {
|
|
199
|
+
if (address !== undefined) {
|
|
200
|
+
return { address };
|
|
201
|
+
}
|
|
202
|
+
if (projectName !== undefined) {
|
|
203
|
+
return { project_name: projectName };
|
|
204
|
+
}
|
|
205
|
+
return envDestination(client);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* The destination LangSmith's env defaults (`LANGSMITH_PROJECT`, `LANGSMITH_AGENT_ID` and
|
|
209
|
+
* `LANGSMITH_AGENT_ENVIRONMENT`) name, as LangSmith's own `RunTree` constructor resolves it for a run that names none;
|
|
210
|
+
* `undefined` when the env can't address a run. The probe run is never posted.
|
|
211
|
+
*
|
|
212
|
+
* @internal
|
|
213
|
+
*/
|
|
214
|
+
function envDestination(client) {
|
|
215
|
+
// `tracingEnabled: false` stops LangSmith warning here about an env it can't use; its `Client` already warned once.
|
|
216
|
+
const probe = new run_trees_1.RunTree({ name: 'temporal-langsmith-destination-probe', client, tracingEnabled: false });
|
|
217
|
+
if (probe.project_name) {
|
|
218
|
+
return { project_name: probe.project_name };
|
|
219
|
+
}
|
|
220
|
+
return probe.address ? { address: probe.address } : undefined;
|
|
221
|
+
}
|
|
151
222
|
/**
|
|
152
|
-
* A webpack plugin that installs the
|
|
223
|
+
* A webpack plugin that installs the four Workflow-bundle rewrites LangSmith needs:
|
|
153
224
|
*
|
|
225
|
+
* - resolve `langsmith` and `langsmith/*` to the CommonJS build this plugin
|
|
226
|
+
* patches, so ESM Workflow code doesn't load a second, unpatched copy.
|
|
154
227
|
* - redirect `node:async_hooks` to the shim module that re-exports the
|
|
155
228
|
* runtime-injected `AsyncLocalStorage`, so LangSmith's `new AsyncLocalStorage()`
|
|
156
229
|
* and `AsyncLocalStorage.snapshot()` resolve to the real SDK-provided ALS.
|
|
@@ -176,6 +249,9 @@ function bundleRewritesPlugin(asyncHooksShimModule, definitions) {
|
|
|
176
249
|
return {
|
|
177
250
|
apply(compiler) {
|
|
178
251
|
const { NormalModuleReplacementPlugin, DefinePlugin } = compiler.webpack;
|
|
252
|
+
new NormalModuleReplacementPlugin(/^langsmith(?:\/|$)/, (resource) => {
|
|
253
|
+
resource.request = require.resolve(resource.request);
|
|
254
|
+
}).apply(compiler);
|
|
179
255
|
new NormalModuleReplacementPlugin(/^node:async_hooks$/, (resource) => {
|
|
180
256
|
resource.request = asyncHooksShimModule;
|
|
181
257
|
}).apply(compiler);
|
package/lib/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;GAmBG;;;
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;GAmBG;;;AA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,IAAI,OAAO,UAAU,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;wBAC3C,UAAU,CAAC,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;oBAChD,CAAC;oBACD,OAAO;gBACT,CAAC;gBACD,IAAI,CAAC,QAAQ,CAAC,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;oBAC9G,OAAO;gBACT,CAAC;gBACD,QAAQ,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YAC5C,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YAEnB,IAAI,YAAY,CAAC,WAAW,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAChD,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/lib/propagation.js
CHANGED
|
@@ -33,7 +33,7 @@ const common_1 = require("@temporalio/common");
|
|
|
33
33
|
* Must byte-match the Python plugin's key so trace context propagates across SDKs.
|
|
34
34
|
*/
|
|
35
35
|
exports.HEADER_KEY = '_temporal-langsmith-context';
|
|
36
|
-
/** Case-insensitive key prefixes scrubbed from
|
|
36
|
+
/** Case-insensitive key prefixes scrubbed from propagated replica settings. */
|
|
37
37
|
const SENSITIVE_KEY_PREFIXES = [
|
|
38
38
|
'auth',
|
|
39
39
|
'api_key',
|
package/lib/routing.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Destination and credential-free replica routing shared across Temporal boundaries. */
|
|
2
|
+
import type { RunTreeConfig, WriteReplica } from 'langsmith/run_trees';
|
|
3
|
+
type Destination = Pick<RunTreeConfig, 'project_name' | 'address'>;
|
|
4
|
+
export type ReplicaRouting = Pick<WriteReplica, 'projectName' | 'address' | 'primary' | 'updates' | 'reroot'>;
|
|
5
|
+
/** Select one destination, clearing the other mode when inheriting an address. */
|
|
6
|
+
export declare function destination(preferred?: Destination, fallback?: Destination): Destination;
|
|
7
|
+
/** Follow LangSmith's header allowlist; clients, credentials and ingestion URLs stay local. */
|
|
8
|
+
export declare function replicaRouting(replicas: readonly WriteReplica[] | undefined): ReplicaRouting[] | undefined;
|
|
9
|
+
/** The Temporal wire format uses Python's spelling; accept native JS headers too. */
|
|
10
|
+
export declare function replicaHeaders(replicas: readonly WriteReplica[]): unknown[];
|
|
11
|
+
export declare function normalizeReplicaHeaders(baggage: string): string;
|
|
12
|
+
/** Restore a destination's credentials from the receiving process's own configuration. */
|
|
13
|
+
export declare function bindReplicaCredentials(replicas: readonly ReplicaRouting[] | undefined, localReplicas: readonly WriteReplica[] | undefined): WriteReplica[] | undefined;
|
|
14
|
+
export {};
|
package/lib/routing.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** Destination and credential-free replica routing shared across Temporal boundaries. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.destination = destination;
|
|
5
|
+
exports.replicaRouting = replicaRouting;
|
|
6
|
+
exports.replicaHeaders = replicaHeaders;
|
|
7
|
+
exports.normalizeReplicaHeaders = normalizeReplicaHeaders;
|
|
8
|
+
exports.bindReplicaCredentials = bindReplicaCredentials;
|
|
9
|
+
const langsmith_1 = require("langsmith");
|
|
10
|
+
const propagation_1 = require("./propagation");
|
|
11
|
+
const SAFE_UPDATE_FIELDS = new Set(['tags', 'metadata', 'extra', 'reroot']);
|
|
12
|
+
/** Select one destination, clearing the other mode when inheriting an address. */
|
|
13
|
+
function destination(preferred, fallback) {
|
|
14
|
+
const selected = preferred?.address || preferred?.project_name ? preferred : fallback;
|
|
15
|
+
return {
|
|
16
|
+
project_name: selected?.address ? undefined : selected?.project_name,
|
|
17
|
+
address: selected?.address,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function safeUpdates(value) {
|
|
21
|
+
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(value))
|
|
25
|
+
return value.map(safeUpdates);
|
|
26
|
+
if (value && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype) {
|
|
27
|
+
return Object.fromEntries(Object.entries((0, propagation_1.scrubSensitive)(value) ?? {})
|
|
28
|
+
.filter(([key]) => !['client', 'apiUrl', 'workspaceId'].includes(key))
|
|
29
|
+
.map(([key, item]) => [key, safeUpdates(item)])
|
|
30
|
+
.filter(([, item]) => item !== undefined));
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
function replicaFields(replica) {
|
|
35
|
+
return {
|
|
36
|
+
...(typeof replica.projectName === 'string' ? { projectName: replica.projectName } : {}),
|
|
37
|
+
...(typeof replica.primary === 'boolean' ? { primary: replica.primary } : {}),
|
|
38
|
+
...(typeof replica.reroot === 'boolean' ? { reroot: replica.reroot } : {}),
|
|
39
|
+
...(replica.updates ? {
|
|
40
|
+
updates: safeUpdates(Object.fromEntries(Object.entries(replica.updates).filter(([key]) => SAFE_UPDATE_FIELDS.has(key)))),
|
|
41
|
+
} : {}),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** Follow LangSmith's header allowlist; clients, credentials and ingestion URLs stay local. */
|
|
45
|
+
function replicaRouting(replicas) {
|
|
46
|
+
return replicas?.map((replica) => ({
|
|
47
|
+
...replicaFields(replica),
|
|
48
|
+
...(replica.address ? { address: { ...new langsmith_1.AgentAddress(replica.address.id, replica.address.environment) } } : {}),
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
/** The Temporal wire format uses Python's spelling; accept native JS headers too. */
|
|
52
|
+
function replicaHeaders(replicas) {
|
|
53
|
+
return replicaRouting(replicas).map(({ projectName, address, ...replica }) => ({
|
|
54
|
+
...replica,
|
|
55
|
+
...(projectName === undefined ? {} : { project_name: projectName }),
|
|
56
|
+
// LangSmith and Python use LRN strings on the wire, including older histories.
|
|
57
|
+
...(address ? { address: `lrn:agents/${address.id}/environments/${address.environment.toLowerCase()}` } : {}),
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
function normalizeReplicaHeaders(baggage) {
|
|
61
|
+
return baggage.split(',').map((item) => {
|
|
62
|
+
const prefix = 'langsmith-replicas=';
|
|
63
|
+
if (!item.startsWith(prefix))
|
|
64
|
+
return item;
|
|
65
|
+
const replicas = JSON.parse(decodeURIComponent(item.slice(prefix.length)));
|
|
66
|
+
if (!Array.isArray(replicas))
|
|
67
|
+
throw new TypeError('replica routing must be an array');
|
|
68
|
+
const normalized = replicas.map((replica) => {
|
|
69
|
+
if (Array.isArray(replica))
|
|
70
|
+
return { projectName: replica[0], updates: replica[1] };
|
|
71
|
+
if (!replica || typeof replica !== 'object')
|
|
72
|
+
throw new TypeError('replica routing must be an object');
|
|
73
|
+
const record = replica;
|
|
74
|
+
return { ...record, projectName: record.project_name ?? record.projectName };
|
|
75
|
+
});
|
|
76
|
+
// Leave wire addresses for RunTree.fromHeaders to parse and validate.
|
|
77
|
+
const sanitized = normalized.map((replica) => ({
|
|
78
|
+
...replicaFields(replica),
|
|
79
|
+
...(replica.address === undefined ? {} : { address: replica.address }),
|
|
80
|
+
}));
|
|
81
|
+
return `${prefix}${encodeURIComponent(JSON.stringify(sanitized))}`;
|
|
82
|
+
}).join(',');
|
|
83
|
+
}
|
|
84
|
+
/** Restore a destination's credentials from the receiving process's own configuration. */
|
|
85
|
+
function bindReplicaCredentials(replicas, localReplicas) {
|
|
86
|
+
if (replicas === undefined)
|
|
87
|
+
return localReplicas ? [...localReplicas] : undefined;
|
|
88
|
+
return replicas.map((replica) => {
|
|
89
|
+
const local = localReplicas?.find((candidate) => candidate.projectName === replica.projectName &&
|
|
90
|
+
candidate.address?.kind === replica.address?.kind && candidate.address?.id === replica.address?.id &&
|
|
91
|
+
candidate.address?.environment === replica.address?.environment);
|
|
92
|
+
return {
|
|
93
|
+
...replica,
|
|
94
|
+
apiKey: local?.apiKey,
|
|
95
|
+
apiUrl: local?.apiUrl,
|
|
96
|
+
workspaceId: local?.workspaceId,
|
|
97
|
+
client: local?.client,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=routing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routing.js","sourceRoot":"","sources":["../src/routing.ts"],"names":[],"mappings":";AAAA,yFAAyF;;AAazF,kCAMC;AAgCD,wCAKC;AAGD,wCAOC;AAED,0DAmBC;AAGD,wDAmBC;AA3GD,yCAAyC;AAEzC,+CAA+C;AAM/C,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;AAE5E,kFAAkF;AAClF,SAAgB,WAAW,CAAC,SAAuB,EAAE,QAAsB;IACzE,MAAM,QAAQ,GAAG,SAAS,EAAE,OAAO,IAAI,SAAS,EAAE,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC;IACtF,OAAO;QACL,YAAY,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,EAAE,YAAY;QACpE,OAAO,EAAE,QAAQ,EAAE,OAAO;KAC3B,CAAC;AACJ,CAAC;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;QAC3G,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IACxD,IAAI,KAAK,IAAI,OAAO,KAAK,K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G,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC;IACrE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACf,CAAC;AAED,0FAA0F;AAC1F,SAAgB,sBAAsB,CACpC,QAA+C,EAC/C,aAAkD;IAElD,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAClF,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;QAC9B,MAAM,KAAK,GAAG,aAAa,EAAE,IAAI,CAC/B,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,WAAW,KAAK,OAAO,CAAC,WAAW;YAC1D,SAAS,CAAC,OAAO,EAAE,IAAI,KAAK,OAAO,CAAC,OAAO,EAAE,IAAI,IAAI,SAAS,CAAC,OAAO,EAAE,EAAE,KAAK,OAAO,CAAC,OAAO,EAAE,EAAE;YAClG,SAAS,CAAC,OAAO,EAAE,WAAW,KAAK,OAAO,CAAC,OAAO,EAAE,WAAW,CAClE,CAAC;QACF,OAAO;YACL,GAAG,OAAO;YACV,MAAM,EAAE,KAAK,EAAE,MAAM;YACrB,MAAM,EAAE,KAAK,EAAE,MAAM;YACrB,WAAW,EAAE,KAAK,EAAE,WAAW;YAC/B,MAAM,EAAE,KAAK,EAAE,MAAM;SACtB,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/lib/run-tree.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* run-name builders shared by every interceptor.
|
|
4
4
|
*
|
|
5
5
|
* Inside the workflow isolate LangSmith's default `RunTree` is unsafe on two
|
|
6
|
-
* counts that
|
|
6
|
+
* counts that the workflow lifecycle overrides fix:
|
|
7
7
|
*
|
|
8
8
|
* 1. **Non-deterministic ids.** Its default random-tailed uuid7 would mint a
|
|
9
9
|
* fresh id on every replay, flooding the backend with duplicates.
|
|
@@ -16,8 +16,9 @@
|
|
|
16
16
|
* @module
|
|
17
17
|
* @internal
|
|
18
18
|
*/
|
|
19
|
-
import { type
|
|
20
|
-
import {
|
|
19
|
+
import { RunTree, type RunTreeConfig } from 'langsmith/run_trees';
|
|
20
|
+
import type { LangSmithTraceContext } from './propagation';
|
|
21
|
+
import type { EmitterConfig, SerializedRun } from './sinks';
|
|
21
22
|
/** LangSmith run-type constants used for Temporal-operation runs. */
|
|
22
23
|
export declare const RUN_TYPE: {
|
|
23
24
|
/** Workflow / child-workflow / scheduling / handler spans. */
|
|
@@ -61,8 +62,23 @@ export declare const signalWithStartRunName: (workflowType: string) => string;
|
|
|
61
62
|
export declare const queryWorkflowRunName: (queryName: string) => string;
|
|
62
63
|
/** `StartWorkflowUpdate:<name>` — client update-start marker. */
|
|
63
64
|
export declare const startWorkflowUpdateRunName: (updateName: string) => string;
|
|
64
|
-
/** `StartUpdateWithStartWorkflow:<
|
|
65
|
-
export declare const startUpdateWithStartRunName: (
|
|
65
|
+
/** `StartUpdateWithStartWorkflow:<type>` — client update-with-start marker. */
|
|
66
|
+
export declare const startUpdateWithStartRunName: (workflowType: string) => string;
|
|
67
|
+
type RunTreeMember = (this: any, ...args: any[]) => any;
|
|
68
|
+
/** RunTree members to replace, each mapped to a function that builds the replacement from LangSmith's original. */
|
|
69
|
+
export interface RunTreeOverrides {
|
|
70
|
+
statics: Record<string, (original: RunTreeMember) => RunTreeMember>;
|
|
71
|
+
methods: Record<string, (original: RunTreeMember) => RunTreeMember>;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Throw, naming each one, unless every member the overrides replace is a function. The Workflow bundle may carry a
|
|
75
|
+
* different LangSmith copy than the Worker, so both check.
|
|
76
|
+
*/
|
|
77
|
+
export declare function requireRunTreeMembers(...overrides: RunTreeOverrides[]): void;
|
|
78
|
+
/** Replace the overridden RunTree members, or none if any is missing. */
|
|
79
|
+
export declare function patchRunTree(overrides: RunTreeOverrides): void;
|
|
80
|
+
/** Flatten a {@link RunTree} into the plain wire shape consumed by the Sink. */
|
|
81
|
+
export declare function serializeRun(run: RunTree): SerializedRun;
|
|
66
82
|
/**
|
|
67
83
|
* Render an error into the LangSmith `error` string, honoring Temporal's
|
|
68
84
|
* benign-failure category.
|
|
@@ -72,19 +88,38 @@ export declare const startUpdateWithStartRunName: (updateName: string) => string
|
|
|
72
88
|
* failures produce `"<type>: <message>"`.
|
|
73
89
|
*/
|
|
74
90
|
export declare function describeError(err: unknown): string | undefined;
|
|
75
|
-
/**
|
|
76
|
-
export declare function
|
|
77
|
-
/** The
|
|
91
|
+
/** Outputs of a plugin run that ends without an error. Plugin runs never record Temporal arguments or results. */
|
|
92
|
+
export declare function statusOutputs(status: 'ok' | 'cancelled' | 'continued_as_new'): Record<string, unknown>;
|
|
93
|
+
/** The `RunTree.end` arguments that close a plugin run. */
|
|
94
|
+
export type RunEnd = [outputs: Record<string, unknown> | undefined, error: string | undefined];
|
|
95
|
+
/** The `RunTree.end` arguments for a plugin run whose operation threw `err`; cancellation isn't an error. */
|
|
96
|
+
export declare function thrownEnd(err: unknown): RunEnd;
|
|
97
|
+
/** Run `fn`, then end and patch the already-posted `run` with an ok status, or with `endThrown` of what `fn` threw. */
|
|
98
|
+
export declare function wrapWithRun<T>(run: RunTree, fn: () => Promise<T>, endThrown?: (err: unknown) => RunEnd): Promise<T>;
|
|
99
|
+
export declare function isUntraced(run: RunTree): boolean;
|
|
100
|
+
export declare function suppressRun(run: RunTree): void;
|
|
101
|
+
/**
|
|
102
|
+
* The trace context to propagate for `run`, or `undefined` when no LangSmith run exists to parent under: no run, a
|
|
103
|
+
* run with no destination, or a run LangSmith won't trace (`tracingEnabled: false`). LangSmith turns tracing off for
|
|
104
|
+
* a run built in an env it can't use, such as a client marker.
|
|
105
|
+
*/
|
|
78
106
|
export declare function runHeaders(run: RunTree | undefined): LangSmithTraceContext | undefined;
|
|
79
|
-
/**
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
107
|
+
/**
|
|
108
|
+
* A run's destination: a LangSmith project, or an agent address (LangSmith's `RunTreeConfig.address`, in the
|
|
109
|
+
* installed version's own form).
|
|
110
|
+
*/
|
|
111
|
+
export type Destination = Pick<RunTreeConfig, 'project_name' | 'address'>;
|
|
112
|
+
/**
|
|
113
|
+
* Reconstruct a propagated parent {@link RunTree} from a trace context; never throws. LangSmith's `RunTree.fromHeaders`
|
|
114
|
+
* lets the context's destination outrank `fallback`, so `fallback` applies only when the context names none.
|
|
115
|
+
*/
|
|
116
|
+
export declare function runTreeFromContext(ctx: LangSmithTraceContext | undefined, fallback?: Destination): RunTree | undefined;
|
|
83
117
|
interface RunTreeParams {
|
|
84
118
|
name: string;
|
|
85
119
|
runType: string;
|
|
86
120
|
parent: RunTree | undefined;
|
|
87
|
-
|
|
121
|
+
/** Merged over the plugin's metadata. */
|
|
122
|
+
metadata?: Record<string, unknown>;
|
|
88
123
|
}
|
|
89
124
|
/**
|
|
90
125
|
* Build an emitter-side {@link RunTree} (client / activity / Nexus); force-enables
|