@anvia/core 1.0.3 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +61 -23
  2. package/dist/agent/index.d.ts +4 -4
  3. package/dist/agent/index.js +6 -5
  4. package/dist/{agent-DYfzP-n5.d.ts → agent-B-3jCRmI.d.ts} +2 -2
  5. package/dist/{chunk-OIB3URVG.js → chunk-2QZJTG2E.js} +2 -2
  6. package/dist/{chunk-AR2K73CJ.js → chunk-34EDX4ZL.js} +4 -83
  7. package/dist/chunk-34EDX4ZL.js.map +1 -0
  8. package/dist/{chunk-5TIXGU5U.js → chunk-HNRNTFMQ.js} +4 -4
  9. package/dist/chunk-KSKST3KP.js +86 -0
  10. package/dist/chunk-KSKST3KP.js.map +1 -0
  11. package/dist/{chunk-LZD36ZAP.js → chunk-XC3LVC4K.js} +5 -4
  12. package/dist/chunk-XC3LVC4K.js.map +1 -0
  13. package/dist/{chunk-4EWTDOLR.js → chunk-XEVOC433.js} +1 -157
  14. package/dist/chunk-XEVOC433.js.map +1 -0
  15. package/dist/{chunk-VSTJNHOZ.js → chunk-XSV5C4R5.js} +2 -2
  16. package/dist/documents/index.d.ts +1 -14
  17. package/dist/documents/index.js +3 -5
  18. package/dist/evals/index.d.ts +2 -2
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +7 -6
  21. package/dist/internal/agent.d.ts +3 -3
  22. package/dist/internal/agent.js +6 -5
  23. package/dist/internal/agent.js.map +1 -1
  24. package/dist/memory/index.d.ts +2 -2
  25. package/dist/observability/index.d.ts +2 -2
  26. package/dist/observability/index.js +2 -1
  27. package/dist/pipeline/index.d.ts +77 -4
  28. package/dist/pipeline/index.js +268 -27
  29. package/dist/pipeline/index.js.map +1 -1
  30. package/dist/skills/index.js +4 -4
  31. package/dist/tool/index.js +3 -3
  32. package/dist/{types-BUEZMBhO.d.ts → types-DL7wX6hJ.d.ts} +2 -1
  33. package/dist/{types-CSYt7-It.d.ts → types-DmzRCbU5.d.ts} +9 -1
  34. package/dist/vector-store/index.js +2 -2
  35. package/package.json +1 -10
  36. package/dist/chunk-4EWTDOLR.js.map +0 -1
  37. package/dist/chunk-AR2K73CJ.js.map +0 -1
  38. package/dist/chunk-LZD36ZAP.js.map +0 -1
  39. /package/dist/{chunk-OIB3URVG.js.map → chunk-2QZJTG2E.js.map} +0 -0
  40. /package/dist/{chunk-5TIXGU5U.js.map → chunk-HNRNTFMQ.js.map} +0 -0
  41. /package/dist/{chunk-VSTJNHOZ.js.map → chunk-XSV5C4R5.js.map} +0 -0
@@ -1,5 +1,5 @@
1
- import { g as AgentInteractionOutcome, f as AgentInput, o as AgentRunSettings } from '../types-BUEZMBhO.js';
2
- import { A as Agent } from '../agent-DYfzP-n5.js';
1
+ import { g as AgentInteractionOutcome, X as AgentTraceOptions, J as AgentObserverTraceInfo, H as AgentObserverErrorPolicy, W as AgentTraceInfo, f as AgentInput, o as AgentRunSettings } from '../types-DL7wX6hJ.js';
2
+ import { A as Agent } from '../agent-B-3jCRmI.js';
3
3
  import { J as JsonObject, c as CompletionModel } from '../types-DgvozfPc.js';
4
4
  import { ExtractOptions } from '../extractor/index.js';
5
5
  import { z } from 'zod';
@@ -7,7 +7,7 @@ import '../agent/interactions/index.js';
7
7
  import '../retry-CjvSlKGW.js';
8
8
  import '../types-DhkodEft.js';
9
9
  import '../type-utils-CtHVDRn_.js';
10
- import '../types-CSYt7-It.js';
10
+ import '../types-DmzRCbU5.js';
11
11
  import '../tool-BpqpoRSE.js';
12
12
  import '../middleware-kcF8AusP.js';
13
13
  import '../zod-schema-C7F4clpm.js';
@@ -22,6 +22,15 @@ declare class PipelineAgentSuspensionError extends Error {
22
22
  readonly result: AgentInteractionOutcome;
23
23
  constructor(result: AgentInteractionOutcome);
24
24
  }
25
+ type PipelineObserverFailure = {
26
+ readonly observer: string;
27
+ readonly error: unknown;
28
+ };
29
+ declare class PipelineObserverDispatchError extends AggregateError {
30
+ readonly phase: string;
31
+ readonly failures: readonly PipelineObserverFailure[];
32
+ constructor(phase: string, failures: readonly PipelineObserverFailure[]);
33
+ }
25
34
 
26
35
  type PipelineMetadata = {
27
36
  id?: string | undefined;
@@ -35,6 +44,7 @@ type PipelineOptions<Input, Parsed = Input> = {
35
44
  name?: string | undefined;
36
45
  description?: string | undefined;
37
46
  metadata?: JsonObject | undefined;
47
+ observability?: PipelineObservabilityOptions | undefined;
38
48
  };
39
49
  type PipelineStageMetadata = {
40
50
  id: string;
@@ -93,10 +103,69 @@ type PipelineRunEvent = (PipelineRunEventBase & {
93
103
  type PipelineRunObserver = {
94
104
  onEvent(event: PipelineRunEvent): void | Promise<void>;
95
105
  };
106
+ type PipelineTraceOptions = Omit<AgentTraceOptions, "promptRef">;
107
+ type PipelineTraceInfo = AgentTraceInfo;
108
+ type PipelineObserverTraceInfo = AgentObserverTraceInfo;
109
+ type PipelineObserverErrorPolicy = AgentObserverErrorPolicy;
110
+ type PipelineRunStartArgs = {
111
+ readonly runId: string;
112
+ readonly pipelineId: string;
113
+ readonly pipelineName?: string | undefined;
114
+ readonly pipelineDescription?: string | undefined;
115
+ readonly pipelineMetadata?: JsonObject | undefined;
116
+ readonly runMetadata?: JsonObject | undefined;
117
+ readonly trace?: PipelineTraceOptions | undefined;
118
+ readonly input: unknown;
119
+ };
120
+ type PipelineRunEndArgs = {
121
+ readonly runId: string;
122
+ readonly pipelineId: string;
123
+ readonly output: unknown;
124
+ readonly durationMs: number;
125
+ };
126
+ type PipelineRunErrorArgs = {
127
+ readonly runId: string;
128
+ readonly pipelineId: string;
129
+ readonly status: "failed" | "cancelled";
130
+ readonly error: unknown;
131
+ readonly durationMs: number;
132
+ };
133
+ type PipelineStageStartArgs = Readonly<PipelineRunEventBase> & {
134
+ readonly input: unknown;
135
+ };
136
+ type PipelineStageEndArgs = Readonly<PipelineRunEventBase> & {
137
+ readonly output: unknown;
138
+ readonly durationMs: number;
139
+ };
140
+ type PipelineStageErrorArgs = Readonly<PipelineRunEventBase> & {
141
+ readonly error: unknown;
142
+ readonly durationMs: number;
143
+ };
144
+ interface PipelineStageObservation {
145
+ readonly trace?: PipelineObserverTraceInfo | undefined;
146
+ end(args: PipelineStageEndArgs): void | Promise<void>;
147
+ error?(args: PipelineStageErrorArgs): void | Promise<void>;
148
+ }
149
+ interface PipelineRunObservation {
150
+ readonly trace?: PipelineObserverTraceInfo | undefined;
151
+ startStage?(args: PipelineStageStartArgs): PipelineStageObservation | undefined | Promise<PipelineStageObservation | undefined>;
152
+ end(args: PipelineRunEndArgs): void | Promise<void>;
153
+ error?(args: PipelineRunErrorArgs): void | Promise<void>;
154
+ }
155
+ interface PipelineObserver {
156
+ startRun(args: PipelineRunStartArgs): PipelineRunObservation | undefined | Promise<PipelineRunObservation | undefined>;
157
+ }
158
+ type PipelineObserverMap = Readonly<Record<string, PipelineObserver>>;
159
+ type PipelineObservabilityOptions<Observers extends PipelineObserverMap = PipelineObserverMap> = {
160
+ readonly observers: Observers;
161
+ readonly primaryTrace?: Extract<keyof Observers, string> | undefined;
162
+ readonly errorPolicy?: PipelineObserverErrorPolicy | undefined;
163
+ };
96
164
  type PipelineRunOptions<Input> = {
97
165
  input: Input;
98
166
  runId?: string | undefined;
99
167
  metadata?: JsonObject | undefined;
168
+ trace?: PipelineTraceOptions | undefined;
100
169
  abortSignal?: AbortSignal | undefined;
101
170
  observer?: PipelineRunObserver | undefined;
102
171
  failOnObserverError?: boolean | undefined;
@@ -104,11 +173,13 @@ type PipelineRunOptions<Input> = {
104
173
  type PipelineRunResult<Output> = {
105
174
  runId: string;
106
175
  output: Awaited<Output>;
176
+ trace?: PipelineTraceInfo | undefined;
107
177
  };
108
178
  type PipelineBatchOptions<Input> = {
109
179
  inputs: Iterable<Input>;
110
180
  concurrency: number;
111
181
  metadata?: JsonObject | undefined;
182
+ trace?: PipelineTraceOptions | undefined;
112
183
  abortSignal?: AbortSignal | undefined;
113
184
  observer?: PipelineRunObserver | undefined;
114
185
  failOnObserverError?: boolean | undefined;
@@ -117,6 +188,7 @@ type PipelineBatchItem<Output> = {
117
188
  status: "completed";
118
189
  runId: string;
119
190
  output: Awaited<Output>;
191
+ trace?: PipelineTraceInfo | undefined;
120
192
  } | {
121
193
  status: "failed";
122
194
  runId: string;
@@ -152,6 +224,7 @@ declare class Pipeline<Input, Output = Input> {
152
224
  readonly name: string | undefined;
153
225
  readonly description: string | undefined;
154
226
  readonly metadata: JsonObject | undefined;
227
+ readonly observability: PipelineOptions<Input>["observability"];
155
228
  private readonly executor;
156
229
  private readonly state;
157
230
  constructor(options: PipelineOptions<Input, Output>);
@@ -167,4 +240,4 @@ declare class Pipeline<Input, Output = Input> {
167
240
  private execute;
168
241
  }
169
242
 
170
- export { type ParallelOutput, Pipeline, type PipelineAgentOptions, PipelineAgentSuspensionError, type PipelineBatchItem, type PipelineBatchOptions, type PipelineComposeOptions, type PipelineExtractOptions, type PipelineGraph, type PipelineGraphEdge, type PipelineGraphNode, type PipelineMetadata, type PipelineNodePath, type PipelineOptions, type PipelineParallelOptions, type PipelineRunEvent, type PipelineRunObserver, type PipelineRunOptions, type PipelineRunResult, type PipelineStageContext, type PipelineStageKind, type PipelineStageMetadata, type PipelineStepOptions };
243
+ export { type ParallelOutput, Pipeline, type PipelineAgentOptions, PipelineAgentSuspensionError, type PipelineBatchItem, type PipelineBatchOptions, type PipelineComposeOptions, type PipelineExtractOptions, type PipelineGraph, type PipelineGraphEdge, type PipelineGraphNode, type PipelineMetadata, type PipelineNodePath, type PipelineObservabilityOptions, type PipelineObserver, PipelineObserverDispatchError, type PipelineObserverErrorPolicy, type PipelineObserverFailure, type PipelineObserverMap, type PipelineObserverTraceInfo, type PipelineOptions, type PipelineParallelOptions, type PipelineRunEndArgs, type PipelineRunErrorArgs, type PipelineRunEvent, type PipelineRunObservation, type PipelineRunObserver, type PipelineRunOptions, type PipelineRunResult, type PipelineRunStartArgs, type PipelineStageContext, type PipelineStageEndArgs, type PipelineStageErrorArgs, type PipelineStageKind, type PipelineStageMetadata, type PipelineStageObservation, type PipelineStageStartArgs, type PipelineStepOptions, type PipelineTraceInfo, type PipelineTraceOptions };
@@ -1,3 +1,6 @@
1
+ import {
2
+ observerSnapshot
3
+ } from "../chunk-KSKST3KP.js";
1
4
  import {
2
5
  AgentRunBlockedError
3
6
  } from "../chunk-AHLKV6KP.js";
@@ -26,6 +29,19 @@ var PipelineAgentSuspensionError = class extends Error {
26
29
  }
27
30
  result;
28
31
  };
32
+ var PipelineObserverDispatchError = class extends AggregateError {
33
+ phase;
34
+ failures;
35
+ constructor(phase, failures) {
36
+ super(
37
+ failures.map((failure) => failure.error),
38
+ `Pipeline observer ${phase} failed for ${failures.map((failure) => failure.observer).join(", ")}.`
39
+ );
40
+ this.name = "PipelineObserverDispatchError";
41
+ this.phase = phase;
42
+ this.failures = Object.freeze([...failures]);
43
+ }
44
+ };
29
45
 
30
46
  // src/pipeline/pipeline.ts
31
47
  import { z } from "zod";
@@ -201,48 +217,214 @@ function pathEquals(left, right) {
201
217
  return left.length === right.length && left.every((segment, index) => segment === right[index]);
202
218
  }
203
219
 
220
+ // src/pipeline/observability.ts
221
+ function resolvePrimaryTrace(observers, primaryTrace) {
222
+ if (primaryTrace === void 0) return void 0;
223
+ const primary = observers.find((entry) => entry.name === primaryTrace);
224
+ return primary?.observer.trace === void 0 ? void 0 : Object.freeze({ observer: primary.name, ...primary.observer.trace });
225
+ }
226
+ function snapshotPipelineObservability(observability) {
227
+ if (observability === void 0) return void 0;
228
+ const observers = {};
229
+ for (const [name, observer] of Object.entries(observability.observers)) {
230
+ if (name.trim().length === 0) {
231
+ throw new TypeError("Pipeline observer names must not be empty.");
232
+ }
233
+ if (typeof observer !== "object" || observer === null || typeof observer.startRun !== "function") {
234
+ throw new TypeError(`Pipeline observer "${name}" must implement startRun().`);
235
+ }
236
+ observers[name] = observer;
237
+ }
238
+ if (observability.primaryTrace !== void 0 && !(observability.primaryTrace in observers)) {
239
+ throw new TypeError(
240
+ `Pipeline primaryTrace "${observability.primaryTrace}" must name a configured observer.`
241
+ );
242
+ }
243
+ const errorPolicy = observability.errorPolicy ?? "ignore";
244
+ if (errorPolicy !== "ignore" && errorPolicy !== "throw") {
245
+ throw new TypeError('Pipeline observability.errorPolicy must be "ignore" or "throw".');
246
+ }
247
+ const snapshot = {
248
+ observers: Object.freeze(observers),
249
+ errorPolicy,
250
+ ...observability.primaryTrace === void 0 ? {} : { primaryTrace: observability.primaryTrace }
251
+ };
252
+ return Object.freeze(snapshot);
253
+ }
254
+ async function startPipelineRunObservers(observers, args, options) {
255
+ const runObservers = [];
256
+ const failures = [];
257
+ for (const [name, observer] of Object.entries(observers)) {
258
+ try {
259
+ const runObserver = await observer.startRun(observerSnapshot(args));
260
+ if (runObserver !== void 0) {
261
+ runObservers.push({ name, observer: runObserver, terminal: false });
262
+ }
263
+ } catch (error) {
264
+ failures.push({ observer: name, error });
265
+ }
266
+ }
267
+ if (options.errorPolicy === "throw" && failures.length > 0) {
268
+ const startupError = new PipelineObserverDispatchError("startRun", failures);
269
+ const cleanupFailures = await terminateObservers(
270
+ runObservers,
271
+ (observer) => observer.error?.(
272
+ observerSnapshot({
273
+ runId: args.runId,
274
+ pipelineId: args.pipelineId,
275
+ status: "failed",
276
+ error: startupError,
277
+ durationMs: 0
278
+ })
279
+ )
280
+ );
281
+ throw new PipelineObserverDispatchError("startRun", [...failures, ...cleanupFailures]);
282
+ }
283
+ return new ActivePipelineRunObservers(runObservers, options);
284
+ }
285
+ var ActivePipelineRunObservers = class {
286
+ constructor(observers, options) {
287
+ this.observers = observers;
288
+ this.options = options;
289
+ this.trace = resolvePrimaryTrace(observers, options.primaryTrace);
290
+ }
291
+ observers;
292
+ options;
293
+ trace;
294
+ async startStage(args) {
295
+ const stageObservers = [];
296
+ const failures = [];
297
+ for (const entry of this.observers) {
298
+ if (entry.observer.startStage === void 0) continue;
299
+ try {
300
+ const observer = await entry.observer.startStage(observerSnapshot(args));
301
+ if (observer !== void 0) {
302
+ stageObservers.push({ name: entry.name, observer, terminal: false });
303
+ }
304
+ } catch (error) {
305
+ failures.push({ observer: entry.name, error });
306
+ }
307
+ }
308
+ if (this.options.errorPolicy === "throw" && failures.length > 0) {
309
+ const startupError = new PipelineObserverDispatchError("startStage", failures);
310
+ const cleanupFailures = await terminateObservers(
311
+ stageObservers,
312
+ (observer) => observer.error?.(observerSnapshot({ ...args, error: startupError, durationMs: 0 }))
313
+ );
314
+ throw new PipelineObserverDispatchError("startStage", [...failures, ...cleanupFailures]);
315
+ }
316
+ return new ActivePipelineStageObservers(stageObservers, this.options);
317
+ }
318
+ async end(args) {
319
+ await dispatchTerminalObservers(
320
+ this.observers,
321
+ "end",
322
+ (observer) => observer.end(observerSnapshot(args)),
323
+ this.options.errorPolicy
324
+ );
325
+ }
326
+ async error(args) {
327
+ await dispatchTerminalObservers(
328
+ this.observers,
329
+ "error",
330
+ (observer) => observer.error?.(observerSnapshot(args)),
331
+ "ignore"
332
+ );
333
+ }
334
+ };
335
+ var ActivePipelineStageObservers = class {
336
+ constructor(observers, options) {
337
+ this.observers = observers;
338
+ this.options = options;
339
+ this.trace = resolvePrimaryTrace(observers, options.primaryTrace);
340
+ }
341
+ observers;
342
+ options;
343
+ trace;
344
+ async end(args) {
345
+ await dispatchTerminalObservers(
346
+ this.observers,
347
+ "stage.end",
348
+ (observer) => observer.end(observerSnapshot(args)),
349
+ this.options.errorPolicy
350
+ );
351
+ }
352
+ async error(args) {
353
+ await dispatchTerminalObservers(
354
+ this.observers,
355
+ "stage.error",
356
+ (observer) => observer.error?.(observerSnapshot(args)),
357
+ "ignore"
358
+ );
359
+ }
360
+ };
361
+ async function dispatchTerminalObservers(observers, phase, dispatch, errorPolicy) {
362
+ const failures = await terminateObservers(observers, dispatch);
363
+ if (errorPolicy === "throw" && failures.length > 0) {
364
+ throw new PipelineObserverDispatchError(phase, failures);
365
+ }
366
+ }
367
+ async function terminateObservers(observers, dispatch) {
368
+ const failures = [];
369
+ for (const entry of observers) {
370
+ if (entry.terminal) continue;
371
+ entry.terminal = true;
372
+ try {
373
+ await dispatch(entry.observer);
374
+ } catch (error) {
375
+ failures.push({ observer: entry.name, error });
376
+ }
377
+ }
378
+ return failures;
379
+ }
380
+
204
381
  // src/pipeline/runtime.ts
205
- async function runNode(context, node, path, fn) {
382
+ async function runNode(context, node, path, input, fn) {
206
383
  throwIfAborted(context.abortSignal);
207
384
  const eventNode = scopedNode(node, path);
208
385
  const startedAt = Date.now();
209
- await emitEvent(context, {
210
- type: "stage_started",
386
+ const eventBase = {
211
387
  runId: context.runId,
212
388
  pipelineId: context.pipelineId,
213
389
  path: [...path],
214
390
  node: eventNode
215
- });
391
+ };
392
+ const observers = await context.observability.startStage({ ...eventBase, input });
216
393
  try {
217
- const output = await fn();
394
+ await emitEvent(context, { type: "stage_started", ...eventBase });
395
+ } catch (error) {
396
+ await observers.error({ ...eventBase, error, durationMs: Date.now() - startedAt });
397
+ throw error;
398
+ }
399
+ try {
400
+ const output = await fn(observers.trace);
218
401
  throwIfAborted(context.abortSignal);
402
+ const durationMs = Date.now() - startedAt;
219
403
  await emitEvent(context, {
220
404
  type: "stage_completed",
221
- runId: context.runId,
222
- pipelineId: context.pipelineId,
223
- path: [...path],
224
- node: eventNode,
225
- durationMs: Date.now() - startedAt
405
+ ...eventBase,
406
+ durationMs
226
407
  });
408
+ await observers.end({ ...eventBase, output, durationMs });
227
409
  return output;
228
410
  } catch (error) {
411
+ const durationMs = Date.now() - startedAt;
229
412
  const failureEvent = {
230
413
  type: "stage_failed",
231
- runId: context.runId,
232
- pipelineId: context.pipelineId,
233
- path: [...path],
234
- node: eventNode,
235
- durationMs: Date.now() - startedAt,
414
+ ...eventBase,
415
+ durationMs,
236
416
  error
237
417
  };
238
418
  try {
239
419
  await emitEvent(context, failureEvent);
240
420
  } catch (observerError) {
421
+ await observers.error({ ...eventBase, error, durationMs });
241
422
  throw new AggregateError(
242
423
  [error, observerError],
243
424
  `Pipeline stage "${node.id}" and its observer both failed.`
244
425
  );
245
426
  }
427
+ await observers.error({ ...eventBase, error, durationMs });
246
428
  throw error;
247
429
  }
248
430
  }
@@ -297,12 +479,14 @@ var Pipeline = class {
297
479
  name;
298
480
  description;
299
481
  metadata;
482
+ observability;
300
483
  executor;
301
484
  state;
302
485
  constructor(options) {
303
486
  if (isInternalPipelineOptions(options)) {
304
487
  this.executor = options[internalPipelineOptions].executor;
305
488
  this.state = options[internalPipelineOptions].state;
489
+ this.observability = options[internalPipelineOptions].observability;
306
490
  } else {
307
491
  const id = normalizePipelineId(options.id);
308
492
  if (!isZodSchema(options.inputSchema)) {
@@ -319,6 +503,7 @@ var Pipeline = class {
319
503
  description: options.description,
320
504
  metadata: options.metadata
321
505
  });
506
+ this.observability = snapshotPipelineObservability(options.observability);
322
507
  }
323
508
  const graph = this.state.graph;
324
509
  this.id = graph.id;
@@ -335,6 +520,7 @@ var Pipeline = class {
335
520
  context,
336
521
  next.node,
337
522
  path,
523
+ value,
338
524
  () => options.run(stageContext(value, context))
339
525
  );
340
526
  });
@@ -353,6 +539,7 @@ var Pipeline = class {
353
539
  context,
354
540
  boundary.node,
355
541
  path,
542
+ value,
356
543
  () => options.pipeline.execute(value, context, path)
357
544
  );
358
545
  });
@@ -379,7 +566,7 @@ var Pipeline = class {
379
566
  return this.derive(async (input, context, pathPrefix) => {
380
567
  const value = await this.execute(input, context, pathPrefix);
381
568
  const parallelPath = [...pathPrefix, parallel.node.id];
382
- return runNode(context, parallel.node, parallelPath, async () => {
569
+ return runNode(context, parallel.node, parallelPath, value, async () => {
383
570
  const controller = new AbortController();
384
571
  const combined = combineAbortSignals(context.abortSignal, controller.signal);
385
572
  const branchContext = childContext(context, combined.signal);
@@ -392,6 +579,7 @@ var Pipeline = class {
392
579
  branchContext,
393
580
  node,
394
581
  path,
582
+ value,
395
583
  () => branch.execute(value, branchContext, path)
396
584
  );
397
585
  return [key, output];
@@ -427,10 +615,15 @@ var Pipeline = class {
427
615
  return this.derive(async (input, context, pathPrefix) => {
428
616
  const value = await this.execute(input, context, pathPrefix);
429
617
  const path = [...pathPrefix, next.node.id];
430
- return runNode(context, next.node, path, async () => {
618
+ return runNode(context, next.node, path, value, async (stageTrace) => {
431
619
  const request = await options.request(stageContext(value, context));
432
620
  const response = await options.agent.generate({
433
621
  ...request,
622
+ trace: withPipelineParentTrace(
623
+ request.trace,
624
+ stageTrace,
625
+ options.agent.observability?.primaryTrace
626
+ ),
434
627
  abortSignal: context.abortSignal
435
628
  });
436
629
  if (response.type === "interaction") {
@@ -448,7 +641,7 @@ var Pipeline = class {
448
641
  return this.derive(async (input, context, pathPrefix) => {
449
642
  const value = await this.execute(input, context, pathPrefix);
450
643
  const path = [...pathPrefix, next.node.id];
451
- return runNode(context, next.node, path, async () => {
644
+ return runNode(context, next.node, path, value, async () => {
452
645
  const text = await options.text(stageContext(value, context));
453
646
  const result = await extract({
454
647
  model: options.model,
@@ -467,18 +660,54 @@ var Pipeline = class {
467
660
  }
468
661
  async run(options) {
469
662
  const runId = normalizeRunId(options.runId ?? globalThis.crypto.randomUUID());
663
+ const startedAt = Date.now();
664
+ const observability = await startPipelineRunObservers(
665
+ this.observability?.observers ?? {},
666
+ {
667
+ runId,
668
+ pipelineId: this.id,
669
+ pipelineName: this.name,
670
+ pipelineDescription: this.description,
671
+ pipelineMetadata: this.metadata,
672
+ runMetadata: options.metadata,
673
+ trace: options.trace,
674
+ input: options.input
675
+ },
676
+ {
677
+ primaryTrace: this.observability?.primaryTrace,
678
+ errorPolicy: this.observability?.errorPolicy ?? "ignore"
679
+ }
680
+ );
470
681
  const context = {
471
682
  runId,
472
683
  pipelineId: this.id,
473
684
  runMetadata: options.metadata,
474
685
  abortSignal: options.abortSignal,
475
686
  observer: options.observer,
476
- failOnObserverError: options.failOnObserverError === true
687
+ failOnObserverError: options.failOnObserverError === true,
688
+ observability
477
689
  };
478
- throwIfAborted(context.abortSignal);
479
- const output = await this.execute(options.input, context, []);
480
- throwIfAborted(context.abortSignal);
481
- return { runId, output };
690
+ try {
691
+ throwIfAborted(context.abortSignal);
692
+ const output = await this.execute(options.input, context, []);
693
+ throwIfAborted(context.abortSignal);
694
+ await observability.end({
695
+ runId,
696
+ pipelineId: this.id,
697
+ output,
698
+ durationMs: Date.now() - startedAt
699
+ });
700
+ return observability.trace === void 0 ? { runId, output } : { runId, output, trace: observability.trace };
701
+ } catch (error) {
702
+ await observability.error({
703
+ runId,
704
+ pipelineId: this.id,
705
+ status: context.abortSignal?.aborted === true ? "cancelled" : "failed",
706
+ error,
707
+ durationMs: Date.now() - startedAt
708
+ });
709
+ throw error;
710
+ }
482
711
  }
483
712
  async runBatch(options) {
484
713
  throwIfAborted(options.abortSignal);
@@ -489,6 +718,7 @@ var Pipeline = class {
489
718
  input,
490
719
  runId,
491
720
  metadata: options.metadata,
721
+ trace: options.trace,
492
722
  abortSignal: options.abortSignal,
493
723
  observer: options.observer,
494
724
  failOnObserverError: options.failOnObserverError
@@ -506,19 +736,29 @@ var Pipeline = class {
506
736
  return cloneGraph(withOutputNode(this.state));
507
737
  }
508
738
  derive(executor, state) {
509
- return createDerivedPipeline(executor, state);
739
+ return createDerivedPipeline(executor, state, this.observability);
510
740
  }
511
741
  async execute(input, context, pathPrefix) {
512
742
  throwIfAborted(context.abortSignal);
513
743
  return await this.executor(input, context, pathPrefix);
514
744
  }
515
745
  };
516
- function createDerivedPipeline(executor, state) {
746
+ function createDerivedPipeline(executor, state, observability) {
517
747
  const options = {
518
- [internalPipelineOptions]: { executor, state }
748
+ [internalPipelineOptions]: { executor, state, observability }
519
749
  };
520
750
  return new Pipeline(options);
521
751
  }
752
+ function withPipelineParentTrace(trace, parent, agentPrimaryTrace) {
753
+ if (parent?.traceId === void 0) return trace;
754
+ if (parent.observer !== agentPrimaryTrace) return trace;
755
+ if (trace?.traceId !== void 0 && trace.traceId !== parent.traceId) return trace;
756
+ return {
757
+ ...trace,
758
+ traceId: parent.traceId,
759
+ ...trace?.parentObservationId !== void 0 || parent.observationId === void 0 ? {} : { parentObservationId: parent.observationId }
760
+ };
761
+ }
522
762
  function isInternalPipelineOptions(options) {
523
763
  return internalPipelineOptions in options;
524
764
  }
@@ -567,6 +807,7 @@ function normalizeParallelBranches(branches) {
567
807
  }
568
808
  export {
569
809
  Pipeline,
570
- PipelineAgentSuspensionError
810
+ PipelineAgentSuspensionError,
811
+ PipelineObserverDispatchError
571
812
  };
572
813
  //# sourceMappingURL=index.js.map