@botpress/runtime 1.3.3 → 1.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/internal.js CHANGED
@@ -48,7 +48,7 @@ var init_define_BUILD = __esm({
48
48
  var define_PACKAGE_VERSIONS_default;
49
49
  var init_define_PACKAGE_VERSIONS = __esm({
50
50
  "<define:__PACKAGE_VERSIONS__>"() {
51
- define_PACKAGE_VERSIONS_default = { runtime: "1.3.3", adk: "not-installed", sdk: "4.17.0", llmz: "0.0.26", zai: "2.1.16", cognitive: "0.1.47" };
51
+ define_PACKAGE_VERSIONS_default = { runtime: "1.3.5", adk: "not-installed", sdk: "4.17.0", llmz: "0.0.26", zai: "2.1.16", cognitive: "0.1.47" };
52
52
  }
53
53
  });
54
54
 
@@ -36085,6 +36085,12 @@ var AutonomousToolSpan = {
36085
36085
  name: "autonomous.tool",
36086
36086
  importance: "high",
36087
36087
  attributes: {
36088
+ "autonomous.tool.object": {
36089
+ type: "string",
36090
+ title: "Object Name",
36091
+ description: "The name of the object being used",
36092
+ required: false
36093
+ },
36088
36094
  "autonomous.tool.name": {
36089
36095
  type: "string",
36090
36096
  title: "Tool Name",
@@ -40446,6 +40452,7 @@ var WellKnownMetadata = {
40446
40452
 
40447
40453
  // src/runtime/autonomous.ts
40448
40454
  var import_lodash2 = __toESM(require_lodash(), 1);
40455
+ import { AsyncResource } from "node:async_hooks";
40449
40456
  var Autonomous;
40450
40457
  ((Autonomous2) => {
40451
40458
  Autonomous2.Tool = LlmzTool;
@@ -40564,7 +40571,7 @@ If the question is not related to the knowledge bases, do NOT use this tool.`.tr
40564
40571
  options.interruption
40565
40572
  ]);
40566
40573
  const llmz_execute = (await import("llmz")).execute;
40567
- const originalContext = context.getAll();
40574
+ const asyncResource = new AsyncResource("autonomous.execution");
40568
40575
  const getNewIteration = (index) => createSpan(
40569
40576
  "autonomous.iteration",
40570
40577
  {
@@ -40622,32 +40629,27 @@ Always prefer information from the knowledge bases over general knowledge when a
40622
40629
  });
40623
40630
  return instructions;
40624
40631
  },
40625
- ...(props.tools || props.knowledge) && {
40626
- tools: async (ctx) => {
40627
- const tools = props.tools ? await getValue(props.tools, ctx) : [];
40628
- const allTools = [...tools ?? []];
40629
- if (search_knowledge) {
40630
- allTools.push(search_knowledge);
40631
- }
40632
+ ...props.objects && {
40633
+ objects: async (ctx) => {
40634
+ const objs = await getValue(props.objects, ctx) ?? [];
40632
40635
  iterationSpan?.setAttribute(
40633
- "autonomous.tools",
40634
- allTools?.map((t) => t.name).join(", ")
40636
+ "autonomous.objects",
40637
+ objs.map((o) => o.name).join(", ")
40635
40638
  );
40636
- return allTools.map(
40637
- (tool) => tool.clone({
40638
- handler: (args, ctx2) => {
40639
- context.enterWith(originalContext);
40640
- return context.run(originalContext, () => {
40639
+ for (const obj of objs) {
40640
+ obj.tools = obj.tools?.map(
40641
+ (tool) => tool.clone({
40642
+ handler: asyncResource.bind((args, ctx2) => {
40641
40643
  let err = null;
40642
40644
  const result = span(
40643
40645
  "autonomous.tool",
40644
40646
  {
40647
+ "autonomous.tool.object": obj.name,
40645
40648
  "autonomous.tool.name": tool.name,
40646
40649
  "autonomous.tool.input": args
40647
40650
  },
40648
40651
  async (s) => {
40649
- context.enterWith(originalContext);
40650
- const value = await tool.execute(args, ctx2).catch((e) => {
40652
+ const value = tool.execute(args, ctx2).catch((e) => {
40651
40653
  err = e;
40652
40654
  if (err && err?.constructor && err?.constructor?.name && err?.constructor?.name === "ThinkSignal") {
40653
40655
  s.setAttributes({
@@ -40683,25 +40685,92 @@ Always prefer information from the knowledge bases over general knowledge when a
40683
40685
  throw err;
40684
40686
  }
40685
40687
  return result;
40686
- });
40687
- }
40688
+ })
40689
+ })
40690
+ ) ?? [];
40691
+ }
40692
+ return objs;
40693
+ }
40694
+ },
40695
+ ...(props.tools || props.knowledge) && {
40696
+ tools: async (ctx) => {
40697
+ const tools = props.tools ? await getValue(props.tools, ctx) : [];
40698
+ const allTools = [...tools ?? []];
40699
+ if (search_knowledge) {
40700
+ allTools.push(search_knowledge);
40701
+ }
40702
+ iterationSpan?.setAttribute(
40703
+ "autonomous.tools",
40704
+ allTools?.map((t) => t.name).join(", ")
40705
+ );
40706
+ return allTools.map(
40707
+ (tool) => tool.clone({
40708
+ handler: asyncResource.bind((args, ctx2) => {
40709
+ let err = null;
40710
+ const result = span(
40711
+ "autonomous.tool",
40712
+ {
40713
+ "autonomous.tool.name": tool.name,
40714
+ "autonomous.tool.input": args
40715
+ },
40716
+ async (s) => {
40717
+ const value = tool.execute(args, ctx2).catch((e) => {
40718
+ err = e;
40719
+ if (err && err?.constructor && err?.constructor?.name && err?.constructor?.name === "ThinkSignal") {
40720
+ s.setAttributes({
40721
+ "autonomous.tool.status": "think"
40722
+ });
40723
+ s.setStatus({
40724
+ code: SpanStatusCode.UNSET,
40725
+ message: "ThinkSignal"
40726
+ });
40727
+ err[HandledErrorProp] = true;
40728
+ throw err;
40729
+ } else {
40730
+ s.setAttributes({
40731
+ "autonomous.tool.status": "error",
40732
+ "autonomous.tool.error": err.message
40733
+ });
40734
+ s.setStatus({
40735
+ code: SpanStatusCode.ERROR,
40736
+ message: err.message
40737
+ });
40738
+ s.recordException(err);
40739
+ throw err;
40740
+ }
40741
+ });
40742
+ s.setAttributes({
40743
+ "autonomous.tool.output": value,
40744
+ "autonomous.tool.status": "success"
40745
+ });
40746
+ return value;
40747
+ }
40748
+ );
40749
+ if (err) {
40750
+ throw err;
40751
+ }
40752
+ return result;
40753
+ })
40688
40754
  })
40689
40755
  );
40690
40756
  }
40691
40757
  },
40692
- ...props.objects && { objects: props.objects },
40693
40758
  ...props.exits && { exits: props.exits },
40694
40759
  ...joinedSignal && { signal: joinedSignal },
40695
40760
  ...props.hooks?.onBeforeTool && {
40696
- onBeforeTool: props.hooks.onBeforeTool
40761
+ onBeforeTool: asyncResource.bind(props.hooks.onBeforeTool)
40697
40762
  },
40698
40763
  ...props.hooks?.onAfterTool && {
40699
- onAfterTool: props.hooks.onAfterTool
40764
+ onAfterTool: asyncResource.bind(props.hooks.onAfterTool)
40700
40765
  },
40701
40766
  ...props.hooks?.onBeforeExecution && {
40702
- onBeforeExecution: props.hooks.onBeforeExecution
40767
+ onBeforeExecution: asyncResource.bind(
40768
+ props.hooks.onBeforeExecution
40769
+ )
40770
+ },
40771
+ ...props.hooks?.onExit && {
40772
+ onExit: asyncResource.bind(props.hooks.onExit)
40703
40773
  },
40704
- ...props.hooks?.onExit && { onExit: props.hooks.onExit },
40705
40774
  onTrace: ({ trace: trace2, iteration }) => {
40706
40775
  if (trace2.type === "code_execution") {
40707
40776
  } else if (trace2.type === "llm_call_started") {
@@ -40715,7 +40784,11 @@ Always prefer information from the knowledge bases over general knowledge when a
40715
40784
  "property.value": trace2.value
40716
40785
  });
40717
40786
  }
40718
- props.hooks?.onTrace?.({ trace: trace2, iteration });
40787
+ if (props.hooks?.onTrace) {
40788
+ return asyncResource.runInAsyncScope(
40789
+ () => props.hooks.onTrace({ trace: trace2, iteration })
40790
+ );
40791
+ }
40719
40792
  },
40720
40793
  onIterationEnd: async (iteration, controller) => {
40721
40794
  iterationSpan?.setAttributes({
@@ -40763,8 +40836,11 @@ ${iteration.status.execution_error.stack}`;
40763
40836
  });
40764
40837
  }
40765
40838
  iterationSpan?.end();
40766
- contextManager.enterWith(execSpan.ctx);
40767
- await props.hooks?.onIterationEnd?.(iteration, controller);
40839
+ if (props.hooks?.onIterationEnd) {
40840
+ return await asyncResource.runInAsyncScope(
40841
+ () => props.hooks.onIterationEnd(iteration, controller)
40842
+ );
40843
+ }
40768
40844
  }
40769
40845
  });
40770
40846
  execSpan.setAttribute(