@anvia/core 1.0.2 → 1.0.4

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 +63 -0
  2. package/dist/agent/index.d.ts +3 -3
  3. package/dist/agent/index.js +6 -4
  4. package/dist/{agent-DYfzP-n5.d.ts → agent-fvQIBLBk.d.ts} +1 -1
  5. package/dist/{chunk-AR2K73CJ.js → chunk-34EDX4ZL.js} +4 -83
  6. package/dist/chunk-34EDX4ZL.js.map +1 -0
  7. package/dist/chunk-4EWTDOLR.js +412 -0
  8. package/dist/chunk-4EWTDOLR.js.map +1 -0
  9. package/dist/chunk-KSKST3KP.js +86 -0
  10. package/dist/chunk-KSKST3KP.js.map +1 -0
  11. package/dist/{chunk-YFJ4NHV6.js → chunk-LZD36ZAP.js} +86 -1
  12. package/dist/chunk-LZD36ZAP.js.map +1 -0
  13. package/dist/{chunk-PH6OTIPZ.js → chunk-NMVCGWQY.js} +4 -4
  14. package/dist/{chunk-EO2GECYH.js → chunk-OIB3URVG.js} +2 -2
  15. package/dist/{chunk-IWJK7JB5.js → chunk-VSTJNHOZ.js} +2 -2
  16. package/dist/documents/index.d.ts +2 -0
  17. package/dist/documents/index.js +6 -335
  18. package/dist/documents/index.js.map +1 -1
  19. package/dist/evals/index.d.ts +1 -1
  20. package/dist/index.d.ts +2 -2
  21. package/dist/index.js +7 -5
  22. package/dist/internal/agent.d.ts +2 -2
  23. package/dist/internal/agent.js +6 -4
  24. package/dist/internal/agent.js.map +1 -1
  25. package/dist/observability/index.d.ts +1 -1
  26. package/dist/observability/index.js +2 -1
  27. package/dist/pipeline/index.d.ts +76 -3
  28. package/dist/pipeline/index.js +268 -27
  29. package/dist/pipeline/index.js.map +1 -1
  30. package/dist/skills/index.js +4 -3
  31. package/dist/text-document-B-X7Vxq1.d.ts +32 -0
  32. package/dist/tool/index.js +3 -2
  33. package/dist/{types-BUEZMBhO.d.ts → types-ChH_bdtR.d.ts} +1 -0
  34. package/dist/vector-store/index.d.ts +25 -5
  35. package/dist/vector-store/index.js +6 -1
  36. package/package.json +1 -1
  37. package/dist/chunk-AR2K73CJ.js.map +0 -1
  38. package/dist/chunk-YFJ4NHV6.js.map +0 -1
  39. /package/dist/{chunk-PH6OTIPZ.js.map → chunk-NMVCGWQY.js.map} +0 -0
  40. /package/dist/{chunk-EO2GECYH.js.map → chunk-OIB3URVG.js.map} +0 -0
  41. /package/dist/{chunk-IWJK7JB5.js.map → chunk-VSTJNHOZ.js.map} +0 -0
@@ -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