@temporalio/langsmith 1.23.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 +36 -17
- 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/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2026 temporal.io
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
9
9
|
copies of the Software, and to permit persons to whom the Software is
|
|
10
10
|
furnished to do so, subject to the following conditions:
|
|
11
11
|
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
14
|
|
|
15
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
# @temporalio/langsmith — LangSmith tracing for Temporal
|
|
2
2
|
|
|
3
|
+
Release stage: Generally Available.
|
|
4
|
+
|
|
3
5
|
This plugin makes [LangSmith](https://docs.smith.langchain.com/) observability
|
|
4
6
|
work inside Temporal Workflows and Activities **without changing your existing
|
|
5
7
|
instrumentation**. Code you already trace with LangSmith's native `traceable`
|
|
6
8
|
keeps working when you move it into a Workflow or Activity body — you only add
|
|
7
9
|
the plugin to your `Client` and `Worker`.
|
|
8
10
|
|
|
9
|
-
This plugin is built on Temporal's Plugin API, which is experimental; its APIs
|
|
10
|
-
may change in a future release.
|
|
11
|
-
|
|
12
11
|
It handles the parts that are otherwise hard:
|
|
13
12
|
|
|
14
13
|
- **Replay safety.** Workflows replay history; a naive tracer re-emits every run
|
|
@@ -40,7 +39,8 @@ export LANGSMITH_API_KEY="<your key>"
|
|
|
40
39
|
|
|
41
40
|
The plugin reads the same flags LangSmith itself uses (`LANGSMITH_TRACING` /
|
|
42
41
|
`LANGSMITH_TRACING_V2` and their `LANGCHAIN_` aliases); with none set to `true`,
|
|
43
|
-
|
|
42
|
+
the plugin starts no new traces. As in LangSmith, a trace that arrives in
|
|
43
|
+
Temporal headers from a traced caller continues either way.
|
|
44
44
|
|
|
45
45
|
## Hello world
|
|
46
46
|
|
|
@@ -150,15 +150,75 @@ user_pipeline
|
|
|
150
150
|
| ----------------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
151
151
|
| `client` | `new Client()` | The LangSmith client runs are emitted to. Lives only in the worker/client process — it is never serialized or sent across a Temporal boundary. |
|
|
152
152
|
| `addTemporalRuns` | `false` | Emit first-class runs for Temporal operations (`StartWorkflow:`, `RunActivity:`, `HandleSignal:`, …) in addition to your `traceable` runs. |
|
|
153
|
-
| `projectName` | LangSmith default |
|
|
153
|
+
| `projectName` | LangSmith default | Default LangSmith project for new root traces. An inherited destination takes precedence. |
|
|
154
|
+
| `address` | — | Agent/environment destination, mutually exclusive with `projectName`. Requires LangSmith's workspace-enabled beta feature. |
|
|
154
155
|
| `tags` | — | Tags attached to every run the plugin emits. |
|
|
155
|
-
| `metadata` | — | Metadata merged into every run the plugin emits.
|
|
156
|
+
| `metadata` | — | Metadata merged into every run the plugin emits. It is sent unchanged and travels in Temporal headers, so keep secrets out of it. |
|
|
156
157
|
|
|
157
158
|
The LangSmith API key is never accepted as a plugin option and never crosses a
|
|
158
159
|
Temporal boundary. Supply a pre-constructed `Client` (which reads
|
|
159
160
|
`LANGSMITH_API_KEY` from the process environment) or let the plugin build a
|
|
160
161
|
default client from the environment.
|
|
161
162
|
|
|
163
|
+
## Agent and environment destinations
|
|
164
|
+
|
|
165
|
+
With LangSmith 0.10.13 or later, send traces to an agent's environment:
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { AgentAddress } from 'langsmith';
|
|
169
|
+
import { LangSmithPlugin } from '@temporalio/langsmith';
|
|
170
|
+
|
|
171
|
+
const plugin = new LangSmithPlugin({
|
|
172
|
+
address: new AgentAddress('support', 'staging'),
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Register the plugin on the Client and Worker as in the quick start. `address`
|
|
177
|
+
and `projectName` are mutually exclusive. Agent addressing is a LangSmith beta
|
|
178
|
+
feature that must be enabled for the workspace.
|
|
179
|
+
|
|
180
|
+
When upgrading from LangSmith 0.10.8, replace `address.agent(...)` with
|
|
181
|
+
`new AgentAddress(...)`. Addresses are objects in application code; tracing
|
|
182
|
+
headers retain their LRN string format, including headers in existing histories.
|
|
183
|
+
This is a breaking change for Workflows that pass address strings as recorded
|
|
184
|
+
inputs to LangSmith calls. Let those executions finish on the previous plugin
|
|
185
|
+
and LangSmith versions, or keep them on their previous Worker Deployment Version
|
|
186
|
+
when deploying the upgrade.
|
|
187
|
+
|
|
188
|
+
Native `traceable(fn, { address: ... })` and runtime traceable configuration
|
|
189
|
+
also select destinations for new root traces in Workflows, Activities, and
|
|
190
|
+
Nexus handlers. Child runs
|
|
191
|
+
inherit their parent's destination, following LangSmith's native rules. Plugin
|
|
192
|
+
defaults apply when a new root names no destination.
|
|
193
|
+
Destinations follow the trace through Activities, child Workflows, and Nexus,
|
|
194
|
+
including with `addTemporalRuns: false`.
|
|
195
|
+
|
|
196
|
+
## Write replicas
|
|
197
|
+
|
|
198
|
+
LangSmith write replicas configured on `traceable` or through
|
|
199
|
+
`LANGSMITH_RUNS_ENDPOINTS` follow the trace across Temporal boundaries:
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
const pipeline = traceable(runWorkflow, {
|
|
203
|
+
replicas: [
|
|
204
|
+
{ projectName: 'primary', primary: true },
|
|
205
|
+
{ projectName: 'audit' },
|
|
206
|
+
{ address: new AgentAddress('support', 'production') },
|
|
207
|
+
],
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
A primary replica retains the original run IDs. Secondary replicas use
|
|
212
|
+
LangSmith's deterministic destination-specific IDs, with corresponding parent
|
|
213
|
+
links and completion updates. Replaying Workflow history publishes no extra
|
|
214
|
+
copies.
|
|
215
|
+
|
|
216
|
+
Only destination identifiers, routing flags, and sanitized replica updates
|
|
217
|
+
cross Temporal boundaries. Each client/worker configures its own credentials;
|
|
218
|
+
matching project destinations in its local `LANGSMITH_RUNS_ENDPOINTS` supply
|
|
219
|
+
their own connection settings. API keys, ingestion URLs, and client objects
|
|
220
|
+
stay outside persisted trace headers.
|
|
221
|
+
|
|
162
222
|
## Where `traceable` works
|
|
163
223
|
|
|
164
224
|
`traceable` from `langsmith/traceable` works **unchanged** in every position
|
|
@@ -176,6 +236,13 @@ the worker injects onto the workflow-sandbox global, so context propagates
|
|
|
176
236
|
across `await` boundaries and `Promise.all(...)` fan-out exactly as it does
|
|
177
237
|
outside the isolate — the same deterministic run tree replays identically.
|
|
178
238
|
|
|
239
|
+
Native `RunTree` instances work in Workflow code too:
|
|
240
|
+
|
|
241
|
+
- Run IDs are the same on replay.
|
|
242
|
+
- End times come from the Worker's clock. Start times do too, unless the run
|
|
243
|
+
was rebuilt by a replay before it ended; then they come from Workflow time.
|
|
244
|
+
- A run's end is never sent twice.
|
|
245
|
+
|
|
179
246
|
## Process-wide effects to be aware of
|
|
180
247
|
|
|
181
248
|
- **Workflow context provider.** Loading the plugin's workflow interceptor
|
|
@@ -184,6 +251,12 @@ outside the isolate — the same deterministic run tree replays identically.
|
|
|
184
251
|
backed by the SDK-provided `AsyncLocalStorage`. This is what lets unchanged
|
|
185
252
|
`traceable` calls find their parent inside a Workflow. It is scoped to the
|
|
186
253
|
workflow bundle and does not affect your client/worker process context.
|
|
254
|
+
- **Worker routing defaults.** The plugin wraps LangSmith's destination and
|
|
255
|
+
shared-client hooks. Defaults apply only within parentless Activity and
|
|
256
|
+
Nexus handlers when `addTemporalRuns: false`; each call carries its own
|
|
257
|
+
destination, client, and replicas in async context. No placeholder run is
|
|
258
|
+
installed. Explicit run configuration and native child inheritance take
|
|
259
|
+
precedence.
|
|
187
260
|
|
|
188
261
|
## Composing with other plugins
|
|
189
262
|
|
|
@@ -14,6 +14,8 @@ const run_trees_1 = require("langsmith/run_trees");
|
|
|
14
14
|
const traceable_1 = require("langsmith/traceable");
|
|
15
15
|
const propagation_1 = require("./propagation");
|
|
16
16
|
const run_tree_1 = require("./run-tree");
|
|
17
|
+
const routing_1 = require("./routing");
|
|
18
|
+
const worker_run_tree_1 = require("./worker-run-tree");
|
|
17
19
|
/**
|
|
18
20
|
* Reconstruct the propagated parent run from a LangSmith trace context, wired to
|
|
19
21
|
* the plugin-configured client so descendant runs emit to the right place. The
|
|
@@ -31,30 +33,35 @@ function reconstructParentRun(config, ctx) {
|
|
|
31
33
|
id: parsed.id,
|
|
32
34
|
trace_id: parsed.trace_id,
|
|
33
35
|
dotted_order: parsed.dotted_order,
|
|
36
|
+
start_time: parsed.start_time,
|
|
34
37
|
parent_run_id: parsed.parent_run_id,
|
|
35
|
-
project_name: config.projectName
|
|
38
|
+
...(0, routing_1.destination)(parsed, { project_name: config.projectName, address: config.address }),
|
|
39
|
+
replicas: (0, routing_1.bindReplicaCredentials)(parsed.replicas, config.replicas),
|
|
40
|
+
distributedParentId: parsed.distributedParentId,
|
|
36
41
|
client: config.client,
|
|
37
42
|
// Force-enable so nested body `traceable` runs emit; tracing gate is `isTracingEnabled()`.
|
|
38
43
|
tracingEnabled: true,
|
|
39
44
|
});
|
|
40
45
|
}
|
|
41
46
|
/**
|
|
42
|
-
* Run `
|
|
43
|
-
*
|
|
47
|
+
* Run a handler under `parent`. With `addTemporalRuns`, wrap it in the operation run from `build`, emitted unless
|
|
48
|
+
* LangSmith turned tracing off for it (an env it can't address). Shared by activity and Nexus handlers.
|
|
44
49
|
*/
|
|
45
|
-
async function
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
await op.end((0, run_tree_1.asOutputs)(result));
|
|
50
|
-
await op.patchRun();
|
|
51
|
-
return result;
|
|
50
|
+
async function traceHandler(config, parent, build, fn) {
|
|
51
|
+
// A propagated parent keeps tracing on, as LangSmith does; the env only gates traces that start here.
|
|
52
|
+
if (!parent && !(0, langsmith_1.isTracingEnabled)()) {
|
|
53
|
+
return fn();
|
|
52
54
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
throw err;
|
|
55
|
+
if (!config.addTemporalRuns) {
|
|
56
|
+
// Without a parent, native roots fall back to the plugin's routing defaults.
|
|
57
|
+
return parent ? (0, traceable_1.withRunTree)(parent, fn) : (0, worker_run_tree_1.withRunTreeDefaults)(config, fn);
|
|
57
58
|
}
|
|
59
|
+
const run = build();
|
|
60
|
+
if (!run.tracingEnabled) {
|
|
61
|
+
return (0, traceable_1.withRunTree)(run, fn);
|
|
62
|
+
}
|
|
63
|
+
await run.postRun();
|
|
64
|
+
return (0, run_tree_1.wrapWithRun)(run, () => (0, traceable_1.withRunTree)(run, fn));
|
|
58
65
|
}
|
|
59
66
|
/**
|
|
60
67
|
* Build the activity-inbound interceptor factory. The worker calls the factory
|
|
@@ -64,30 +71,24 @@ async function traceOperation(op, fn) {
|
|
|
64
71
|
function createActivityInboundInterceptor(config) {
|
|
65
72
|
return (ctx) => ({
|
|
66
73
|
async execute(input, next) {
|
|
67
|
-
if (!(0, langsmith_1.isTracingEnabled)()) {
|
|
68
|
-
return next(input);
|
|
69
|
-
}
|
|
70
74
|
const parent = reconstructParentRun(config, (0, propagation_1.readContextHeader)(input.headers));
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
return
|
|
75
|
+
const build = () => {
|
|
76
|
+
const { activityType, activityId, workflowExecution } = ctx.info;
|
|
77
|
+
return (0, run_tree_1.buildRunTree)(config, {
|
|
78
|
+
name: (0, run_tree_1.runActivityRunName)(activityType),
|
|
79
|
+
runType: run_tree_1.RUN_TYPE.TOOL,
|
|
80
|
+
parent,
|
|
81
|
+
metadata: {
|
|
82
|
+
temporalWorkflowID: workflowExecution?.workflowId ?? '',
|
|
83
|
+
temporalRunID: workflowExecution?.runId ?? '',
|
|
84
|
+
temporalActivityID: activityId,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
return traceHandler(config, parent, build, () => next(input));
|
|
85
89
|
},
|
|
86
90
|
});
|
|
87
91
|
}
|
|
88
|
-
function nexusContext(headers) {
|
|
89
|
-
return (0, propagation_1.decodeContextString)(headers[propagation_1.HEADER_KEY]);
|
|
90
|
-
}
|
|
91
92
|
/**
|
|
92
93
|
* Build the Nexus-handler interceptor. `startOperation` opens a
|
|
93
94
|
* `RunStartNexusOperationHandler:` run (so workflows the handler starts nest
|
|
@@ -95,19 +96,9 @@ function nexusContext(headers) {
|
|
|
95
96
|
*/
|
|
96
97
|
function createNexusInboundInterceptor(config) {
|
|
97
98
|
const handle = async (input, next, nameOf) => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const parent = reconstructParentRun(config, nexusContext(input.ctx.headers));
|
|
102
|
-
if (!config.addTemporalRuns) {
|
|
103
|
-
return parent ? (0, traceable_1.withRunTree)(parent, () => next(input)) : next(input);
|
|
104
|
-
}
|
|
105
|
-
const run = (0, run_tree_1.buildRunTree)(config, {
|
|
106
|
-
name: nameOf(input.ctx.service, input.ctx.operation),
|
|
107
|
-
runType: run_tree_1.RUN_TYPE.CHAIN,
|
|
108
|
-
parent,
|
|
109
|
-
});
|
|
110
|
-
return traceOperation(run, () => next(input));
|
|
99
|
+
const parent = reconstructParentRun(config, (0, propagation_1.decodeContextString)(input.ctx.headers[propagation_1.HEADER_KEY]));
|
|
100
|
+
const build = () => (0, run_tree_1.buildRunTree)(config, { name: nameOf(input.ctx.service, input.ctx.operation), runType: run_tree_1.RUN_TYPE.TOOL, parent });
|
|
101
|
+
return traceHandler(config, parent, build, () => next(input));
|
|
111
102
|
};
|
|
112
103
|
return {
|
|
113
104
|
startOperation(input, next) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"activity-interceptor.js","sourceRoot":"","sources":["../src/activity-interceptor.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;
|
|
1
|
+
{"version":3,"file":"activity-interceptor.js","sourceRoot":"","sources":["../src/activity-interceptor.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAiFH,4EAoBC;AAOD,sEAoBC;AA9HD,yCAA6C;AAC7C,mDAA8C;AAC9C,mDAAkD;AAIlD,+CAA+G;AAC/G,yCAQoB;AAEpB,uCAAgE;AAChE,uDAAwD;AAExD;;;;;GAKG;AACH,SAAS,oBAAoB,CAAC,MAAqB,EAAE,GAAsC;IACzF,MAAM,MAAM,GAAG,IAAA,6BAAkB,EAAC,GAAG,CAAC,CAAC;IACvC,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,IAAI,mBAAO,CAAC;QACjB,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,QAAQ;QAC7B,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,mBAAQ,CAAC,KAAK;QAC3C,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,YAAY,EAAE,MAAM,CAAC,YAAY;QACjC,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,aAAa,EAAE,MAAM,CAAC,aAAa;QACnC,GAAG,IAAA,qBAAW,EAAC,MAAM,E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,MAAM,EAAE,CAAC,CAAC;QACjH,OAAO,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAChE,CAAC,CAAC;IAEF,OAAO;QACL,cAAc,CAAC,KAAK,EAAE,IAAI;YACxB,OAAO,MAAM,CAAC,KAAK,EAAE,IAAI,EAAE,sCAA2B,CAAC,CAAC;QAC1D,CAAC;QACD,eAAe,CAAC,KAAK,EAAE,IAAI;YACzB,OAAO,MAAM,CAAC,KAAK,EAAE,IAAI,EAAE,uCAA4B,CAAC,CAAC;QAC3D,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Client-side LangSmith interceptor.
|
|
3
|
-
*
|
|
4
|
-
* ops (`signal`, `query`, `
|
|
5
|
-
*
|
|
2
|
+
* Client-side LangSmith interceptor. Each call runs inside a marker run that ends when the call returns. Execution ops
|
|
3
|
+
* (`start`, `signalWithStart`, `startUpdateWithStart`) propagate the **ambient** context (the remote run is a
|
|
4
|
+
* sibling); messaging ops (`signal`, `query`, `startUpdate`) propagate the **marker** context (the remote handler
|
|
5
|
+
* nests under it).
|
|
6
6
|
*
|
|
7
7
|
* @module
|
|
8
8
|
*/
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Client-side LangSmith interceptor.
|
|
4
|
-
*
|
|
5
|
-
* ops (`signal`, `query`, `
|
|
6
|
-
*
|
|
3
|
+
* Client-side LangSmith interceptor. Each call runs inside a marker run that ends when the call returns. Execution ops
|
|
4
|
+
* (`start`, `signalWithStart`, `startUpdateWithStart`) propagate the **ambient** context (the remote run is a
|
|
5
|
+
* sibling); messaging ops (`signal`, `query`, `startUpdate`) propagate the **marker** context (the remote handler
|
|
6
|
+
* nests under it).
|
|
7
7
|
*
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
@@ -14,68 +14,49 @@ const traceable_1 = require("langsmith/traceable");
|
|
|
14
14
|
const propagation_1 = require("./propagation");
|
|
15
15
|
const run_tree_1 = require("./run-tree");
|
|
16
16
|
function createClientInterceptor(config) {
|
|
17
|
-
|
|
17
|
+
/** Run `call` inside a marker run, passing it the context to propagate: the marker's, or the ambient one when `peer`. */
|
|
18
|
+
const traced = async (name, peer, call) => {
|
|
18
19
|
if (!(0, langsmith_1.isTracingEnabled)()) {
|
|
19
|
-
return
|
|
20
|
+
return call(undefined);
|
|
20
21
|
}
|
|
21
22
|
const ambient = (0, traceable_1.getCurrentRunTree)(true);
|
|
22
|
-
if (config.addTemporalRuns) {
|
|
23
|
-
|
|
23
|
+
if (!config.addTemporalRuns) {
|
|
24
|
+
return call((0, run_tree_1.runHeaders)(ambient));
|
|
24
25
|
}
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (!(0, langsmith_1.isTracingEnabled)()) {
|
|
30
|
-
return next(input);
|
|
31
|
-
}
|
|
32
|
-
const ambient = (0, traceable_1.getCurrentRunTree)(true);
|
|
33
|
-
let propagate = ambient;
|
|
34
|
-
if (config.addTemporalRuns) {
|
|
35
|
-
const marker = (0, run_tree_1.buildRunTree)(config, { name, runType: run_tree_1.RUN_TYPE.CHAIN, parent: ambient, inputs: { args } });
|
|
36
|
-
await (0, run_tree_1.emitMarkerRun)(marker);
|
|
37
|
-
propagate = marker;
|
|
26
|
+
const marker = (0, run_tree_1.buildRunTree)(config, { name, runType: run_tree_1.RUN_TYPE.CHAIN, parent: ambient });
|
|
27
|
+
const context = (0, run_tree_1.runHeaders)(peer ? ambient : marker);
|
|
28
|
+
if (!marker.tracingEnabled) {
|
|
29
|
+
return call(context);
|
|
38
30
|
}
|
|
39
|
-
|
|
40
|
-
return
|
|
31
|
+
await marker.postRun();
|
|
32
|
+
return (0, run_tree_1.wrapWithRun)(marker, () => call(context));
|
|
41
33
|
};
|
|
34
|
+
const withHeaders = (input, next, name, peer) => traced(name, peer, (context) => next({ ...input, headers: (0, propagation_1.withContextHeader)(input.headers, context) }));
|
|
42
35
|
return {
|
|
43
36
|
start(input, next) {
|
|
44
|
-
return
|
|
37
|
+
return withHeaders(input, next, (0, run_tree_1.startWorkflowRunName)(input.workflowType), true);
|
|
45
38
|
},
|
|
46
39
|
startWithDetails(input, next) {
|
|
47
|
-
return
|
|
40
|
+
return withHeaders(input, next, (0, run_tree_1.startWorkflowRunName)(input.workflowType), true);
|
|
48
41
|
},
|
|
49
42
|
signal(input, next) {
|
|
50
|
-
return
|
|
43
|
+
return withHeaders(input, next, (0, run_tree_1.signalWorkflowRunName)(input.signalName), false);
|
|
51
44
|
},
|
|
52
45
|
signalWithStart(input, next) {
|
|
53
|
-
return
|
|
46
|
+
return withHeaders(input, next, (0, run_tree_1.signalWithStartRunName)(input.workflowType), true);
|
|
54
47
|
},
|
|
55
48
|
query(input, next) {
|
|
56
|
-
return
|
|
49
|
+
return withHeaders(input, next, (0, run_tree_1.queryWorkflowRunName)(input.queryType), false);
|
|
57
50
|
},
|
|
58
51
|
startUpdate(input, next) {
|
|
59
|
-
return
|
|
52
|
+
return withHeaders(input, next, (0, run_tree_1.startWorkflowUpdateRunName)(input.updateName), false);
|
|
60
53
|
},
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (config.addTemporalRuns) {
|
|
68
|
-
const marker = (0, run_tree_1.buildRunTree)(config, {
|
|
69
|
-
name: (0, run_tree_1.startUpdateWithStartRunName)(input.updateName),
|
|
70
|
-
runType: run_tree_1.RUN_TYPE.CHAIN,
|
|
71
|
-
parent: ambient,
|
|
72
|
-
inputs: { args: input.updateArgs },
|
|
73
|
-
});
|
|
74
|
-
await (0, run_tree_1.emitMarkerRun)(marker);
|
|
75
|
-
propagate = marker;
|
|
76
|
-
}
|
|
77
|
-
const workflowStartHeaders = (0, propagation_1.withContextHeader)(input.workflowStartHeaders, (0, run_tree_1.runHeaders)(propagate));
|
|
78
|
-
return next({ ...input, workflowStartHeaders });
|
|
54
|
+
startUpdateWithStart(input, next) {
|
|
55
|
+
return traced((0, run_tree_1.startUpdateWithStartRunName)(input.workflowType), true, (context) => next({
|
|
56
|
+
...input,
|
|
57
|
+
workflowStartHeaders: (0, propagation_1.withContextHeader)(input.workflowStartHeaders, context),
|
|
58
|
+
updateHeaders: (0, propagation_1.withContextHeader)(input.updateHeaders, context),
|
|
59
|
+
}));
|
|
79
60
|
},
|
|
80
61
|
};
|
|
81
62
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-interceptor.js","sourceRoot":"","sources":["../src/client-interceptor.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAwBH,
|
|
1
|
+
{"version":3,"file":"client-interceptor.js","sourceRoot":"","sources":["../src/client-interceptor.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAwBH,0DAuDC;AA7ED,yCAA6C;AAC7C,mDAAwD;AAGxD,+CAA8E;AAC9E,yCAWoB;AAMpB,SAAgB,uBAAuB,CAAC,MAAqB;IAC3D,yHAAyH;IACzH,MAAM,MAAM,GAAG,KAAK,EAClB,IAAY,EACZ,IAAa,EACb,IAAgE,EACpD,EAAE;QACd,IAAI,CAAC,IAAA,4BAAgB,GAAE,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC;QACzB,CAAC;QACD,MAAM,OAAO,GAAG,IAAA,6BAAiB,EAAC,IAAI,CAAC,CAAC;QACxC,IAAI,CAAC,MAAM,CAAC,eAAe,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAC,IAAA,qBAAU,EAAC,OAAO,CAAC,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,MAAM,GAAG,IAAA,uBAAY,EAAC,MAAM,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,mBAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,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AK,EAAE,IAAI,EAAE,IAAA,iCAAsB,EAAC,KAAK,CAAC,YAAY,CAAC,EAAE,IAAI,CAAC,CAAC;QACpF,CAAC;QACD,KAAK,CAAC,KAAK,EAAE,IAAI;YACf,OAAO,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,IAAA,+BAAoB,EAAC,KAAK,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC,CAAC;QAChF,CAAC;QACD,WAAW,CAAC,KAAK,EAAE,IAAI;YACrB,OAAO,WAAW,CAAC,KAAK,EAAE,IAAI,EAAE,IAAA,qCAA0B,EAAC,KAAK,CAAC,UAAU,CAAC,EAAE,KAAK,CAAC,CAAC;QACvF,CAAC;QACD,oBAAoB,CAAC,KAAK,EAAE,IAAI;YAC9B,OAAO,MAAM,CAAC,IAAA,sCAA2B,EAAC,KAAK,CAAC,YAAY,CAAC,EAAE,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE,CAC/E,IAAI,CAAC;gBACH,GAAG,KAAK;gBACR,oBAAoB,EAAE,IAAA,+BAAiB,EAAC,KAAK,CAAC,oBAAoB,EAAE,OAAO,CAAC;gBAC5E,aAAa,EAAE,IAAA,+BAAiB,EAAC,KAAK,CAAC,aAAa,EAAE,OAAO,CAAC;aAC/D,CAAC,CACH,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/lib/plugin.d.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
*
|
|
19
19
|
* @module
|
|
20
20
|
*/
|
|
21
|
-
import { Client } from 'langsmith';
|
|
21
|
+
import { Client, type AgentAddress } from 'langsmith';
|
|
22
22
|
import { SimplePlugin } from '@temporalio/plugin';
|
|
23
23
|
import type { ClientOptions } from '@temporalio/client';
|
|
24
24
|
import type { BundleOptions, ReplayWorkerOptions, Worker, WorkerOptions } from '@temporalio/worker';
|
|
@@ -29,8 +29,6 @@ import type { BundleOptions, ReplayWorkerOptions, Worker, WorkerOptions } from '
|
|
|
29
29
|
* never cross a Temporal boundary — supply a pre-constructed {@link Client}
|
|
30
30
|
* (which reads `LANGSMITH_API_KEY` from the worker/client process env) or let
|
|
31
31
|
* the plugin construct a default client from the environment.
|
|
32
|
-
*
|
|
33
|
-
* @experimental Plugins is an experimental feature; APIs may change without notice.
|
|
34
32
|
*/
|
|
35
33
|
export interface LangSmithPluginOptions {
|
|
36
34
|
/**
|
|
@@ -39,8 +37,10 @@ export interface LangSmithPluginOptions {
|
|
|
39
37
|
* environment. Lives only in the worker/client process — never serialized.
|
|
40
38
|
*/
|
|
41
39
|
client?: Client;
|
|
42
|
-
/**
|
|
40
|
+
/** Default LangSmith project for new root traces. An inherited destination takes precedence. */
|
|
43
41
|
projectName?: string;
|
|
42
|
+
/** Agent/environment destination, mutually exclusive with projectName. LangSmith beta. */
|
|
43
|
+
address?: AgentAddress;
|
|
44
44
|
/**
|
|
45
45
|
* When `true`, emit first-class runs for Temporal operations themselves
|
|
46
46
|
* (`StartWorkflow:`, `RunActivity:`, `HandleSignal:`, …) in addition to the
|
|
@@ -65,13 +65,14 @@ export interface LangSmithPluginOptions {
|
|
|
65
65
|
* const plugin = new LangSmithPlugin({ addTemporalRuns: true });
|
|
66
66
|
* const worker = await Worker.create({ workflowsPath, taskQueue: 'tq', plugins: [plugin] });
|
|
67
67
|
* ```
|
|
68
|
-
*
|
|
69
|
-
* @experimental Plugins is an experimental feature; APIs may change without notice.
|
|
70
68
|
*/
|
|
71
69
|
export declare class LangSmithPlugin extends SimplePlugin {
|
|
70
|
+
/** The package version, reported in worker heartbeats. */
|
|
71
|
+
readonly version: string;
|
|
72
72
|
private readonly client;
|
|
73
73
|
private readonly emitter;
|
|
74
74
|
private readonly workflowConfig;
|
|
75
|
+
private readonly drains;
|
|
75
76
|
private readonly workflowInterceptorModule;
|
|
76
77
|
private readonly asyncHooksShimModule;
|
|
77
78
|
constructor(options?: LangSmithPluginOptions);
|