llmist 11.0.1 → 11.1.0

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/index.cjs CHANGED
@@ -9125,15 +9125,27 @@ function getIterationFromTree(tree, nodeId) {
9125
9125
  return 0;
9126
9126
  }
9127
9127
  function buildSubagentContext(tree, event) {
9128
- if (event.depth === 0) {
9128
+ const parentGadgetInvocationId = findParentGadgetInvocationId(tree, event.nodeId);
9129
+ if (!parentGadgetInvocationId) {
9129
9130
  return void 0;
9130
9131
  }
9131
- const parentGadgetInvocationId = findParentGadgetInvocationId(tree, event.nodeId);
9132
9132
  return {
9133
- parentGadgetInvocationId: parentGadgetInvocationId ?? "unknown",
9133
+ parentGadgetInvocationId,
9134
9134
  depth: event.depth
9135
9135
  };
9136
9136
  }
9137
+ function getSubagentContextForNode(tree, nodeId) {
9138
+ const node = tree.getNode(nodeId);
9139
+ if (!node) return void 0;
9140
+ const parentGadgetInvocationId = findParentGadgetInvocationId(tree, nodeId);
9141
+ if (!parentGadgetInvocationId) {
9142
+ return void 0;
9143
+ }
9144
+ return {
9145
+ parentGadgetInvocationId,
9146
+ depth: node.depth
9147
+ };
9148
+ }
9137
9149
  async function safeObserve(fn, logger, eventType) {
9138
9150
  try {
9139
9151
  await fn();
@@ -9603,13 +9615,16 @@ var init_agent = __esm({
9603
9615
  });
9604
9616
  }
9605
9617
  this.logger.debug("Starting iteration", { iteration: currentIteration });
9618
+ let currentLLMNodeId;
9619
+ let llmOptions;
9606
9620
  try {
9607
9621
  const compactionEvent = await this.checkAndPerformCompaction(currentIteration);
9608
9622
  if (compactionEvent) {
9609
9623
  yield compactionEvent;
9610
9624
  }
9611
9625
  const prepared = await this.prepareLLMCall(currentIteration);
9612
- const llmOptions = prepared.options;
9626
+ llmOptions = prepared.options;
9627
+ currentLLMNodeId = prepared.llmNodeId;
9613
9628
  if (prepared.skipWithSynthetic !== void 0) {
9614
9629
  this.conversation.addAssistantMessage(prepared.skipWithSynthetic);
9615
9630
  yield { type: "text", content: prepared.skipWithSynthetic };
@@ -9623,13 +9638,6 @@ var init_agent = __esm({
9623
9638
  messageCount: llmOptions.messages.length,
9624
9639
  messages: llmOptions.messages
9625
9640
  });
9626
- const llmNode = this.tree.addLLMCall({
9627
- iteration: currentIteration,
9628
- model: llmOptions.model,
9629
- parentId: this.parentNodeId,
9630
- request: llmOptions.messages
9631
- });
9632
- const currentLLMNodeId = llmNode.id;
9633
9641
  const stream2 = await this.createStreamWithRetry(llmOptions, currentIteration);
9634
9642
  const processor = new StreamProcessor({
9635
9643
  iteration: currentIteration,
@@ -9691,6 +9699,7 @@ var init_agent = __esm({
9691
9699
  });
9692
9700
  await this.safeObserve(async () => {
9693
9701
  if (this.hooks.observers?.onLLMCallComplete) {
9702
+ const subagentContext = getSubagentContextForNode(this.tree, currentLLMNodeId);
9694
9703
  const context = {
9695
9704
  iteration: currentIteration,
9696
9705
  options: llmOptions,
@@ -9698,7 +9707,8 @@ var init_agent = __esm({
9698
9707
  usage: result.usage,
9699
9708
  rawResponse: result.rawResponse,
9700
9709
  finalMessage: result.finalMessage,
9701
- logger: this.logger
9710
+ logger: this.logger,
9711
+ subagentContext
9702
9712
  };
9703
9713
  await this.hooks.observers.onLLMCallComplete(context);
9704
9714
  }
@@ -9727,17 +9737,20 @@ var init_agent = __esm({
9727
9737
  const errorHandled = await this.handleLLMError(error, currentIteration);
9728
9738
  await this.safeObserve(async () => {
9729
9739
  if (this.hooks.observers?.onLLMCallError) {
9740
+ const options = llmOptions ?? {
9741
+ model: this.model,
9742
+ messages: this.conversation.getMessages(),
9743
+ temperature: this.temperature,
9744
+ maxTokens: this.defaultMaxTokens
9745
+ };
9746
+ const subagentContext = currentLLMNodeId ? getSubagentContextForNode(this.tree, currentLLMNodeId) : void 0;
9730
9747
  const context = {
9731
9748
  iteration: currentIteration,
9732
- options: {
9733
- model: this.model,
9734
- messages: this.conversation.getMessages(),
9735
- temperature: this.temperature,
9736
- maxTokens: this.defaultMaxTokens
9737
- },
9749
+ options,
9738
9750
  error,
9739
9751
  recovered: errorHandled,
9740
- logger: this.logger
9752
+ logger: this.logger,
9753
+ subagentContext
9741
9754
  };
9742
9755
  await this.hooks.observers.onLLMCallError(context);
9743
9756
  }
@@ -9975,8 +9988,8 @@ var init_agent = __esm({
9975
9988
  return { type: "compaction", event: compactionEvent };
9976
9989
  }
9977
9990
  /**
9978
- * Prepare LLM call options and process beforeLLMCall controller.
9979
- * @returns options and optional skipWithSynthetic response if controller wants to skip
9991
+ * Prepare LLM call options, create tree node, and process beforeLLMCall controller.
9992
+ * @returns options, node ID, and optional skipWithSynthetic response if controller wants to skip
9980
9993
  */
9981
9994
  async prepareLLMCall(iteration) {
9982
9995
  let llmOptions = {
@@ -9986,12 +9999,20 @@ var init_agent = __esm({
9986
9999
  maxTokens: this.defaultMaxTokens,
9987
10000
  signal: this.signal
9988
10001
  };
10002
+ const llmNode = this.tree.addLLMCall({
10003
+ iteration,
10004
+ model: llmOptions.model,
10005
+ parentId: this.parentNodeId,
10006
+ request: llmOptions.messages
10007
+ });
9989
10008
  await this.safeObserve(async () => {
9990
10009
  if (this.hooks.observers?.onLLMCallStart) {
10010
+ const subagentContext = getSubagentContextForNode(this.tree, llmNode.id);
9991
10011
  const context = {
9992
10012
  iteration,
9993
10013
  options: llmOptions,
9994
- logger: this.logger
10014
+ logger: this.logger,
10015
+ subagentContext
9995
10016
  };
9996
10017
  await this.hooks.observers.onLLMCallStart(context);
9997
10018
  }
@@ -10007,23 +10028,25 @@ var init_agent = __esm({
10007
10028
  validateBeforeLLMCallAction(action);
10008
10029
  if (action.action === "skip") {
10009
10030
  this.logger.info("Controller skipped LLM call, using synthetic response");
10010
- return { options: llmOptions, skipWithSynthetic: action.syntheticResponse };
10031
+ return { options: llmOptions, llmNodeId: llmNode.id, skipWithSynthetic: action.syntheticResponse };
10011
10032
  } else if (action.action === "proceed" && action.modifiedOptions) {
10012
10033
  llmOptions = { ...llmOptions, ...action.modifiedOptions };
10013
10034
  }
10014
10035
  }
10015
10036
  await this.safeObserve(async () => {
10016
10037
  if (this.hooks.observers?.onLLMCallReady) {
10038
+ const subagentContext = getSubagentContextForNode(this.tree, llmNode.id);
10017
10039
  const context = {
10018
10040
  iteration,
10019
10041
  maxIterations: this.maxIterations,
10020
10042
  options: llmOptions,
10021
- logger: this.logger
10043
+ logger: this.logger,
10044
+ subagentContext
10022
10045
  };
10023
10046
  await this.hooks.observers.onLLMCallReady(context);
10024
10047
  }
10025
10048
  });
10026
- return { options: llmOptions };
10049
+ return { options: llmOptions, llmNodeId: llmNode.id };
10027
10050
  }
10028
10051
  /**
10029
10052
  * Calculate cost and complete LLM call in execution tree.