{"version":3,"file":"console.cjs","names":["styles","BaseTracer"],"sources":["../../src/tracers/console.ts"],"sourcesContent":["import type { CSPair } from \"ansi-styles\";\nimport styles from \"ansi-styles\";\nimport { BaseTracer, type AgentRun, type Run } from \"./base.js\";\n\nfunction wrap(style: CSPair, text: string) {\n  return `${style.open}${text}${style.close}`;\n}\n\nfunction tryJsonStringify(obj: unknown, fallback: string) {\n  try {\n    return JSON.stringify(obj, null, 2);\n  } catch {\n    return fallback;\n  }\n}\n\nfunction formatKVMapItem(value: unknown) {\n  if (typeof value === \"string\") {\n    return value.trim();\n  }\n\n  if (value === null || value === undefined) {\n    return value;\n  }\n\n  return tryJsonStringify(value, value.toString());\n}\n\nfunction elapsed(run: Run): string {\n  if (!run.end_time) return \"\";\n  const elapsed = run.end_time - run.start_time;\n  if (elapsed < 1000) {\n    return `${elapsed}ms`;\n  }\n  return `${(elapsed / 1000).toFixed(2)}s`;\n}\n\nconst { color } = styles;\n\n/**\n * A tracer that logs all events to the console. It extends from the\n * `BaseTracer` class and overrides its methods to provide custom logging\n * functionality.\n * @example\n * ```typescript\n *\n * const llm = new ChatAnthropic({\n *   temperature: 0,\n *   tags: [\"example\", \"callbacks\", \"constructor\"],\n *   callbacks: [new ConsoleCallbackHandler()],\n * });\n *\n * ```\n */\nexport class ConsoleCallbackHandler extends BaseTracer {\n  name = \"console_callback_handler\" as const;\n\n  /**\n   * Method used to persist the run. In this case, it simply returns a\n   * resolved promise as there's no persistence logic.\n   * @param _run The run to persist.\n   * @returns A resolved promise.\n   */\n  protected persistRun(_run: Run) {\n    return Promise.resolve();\n  }\n\n  // utility methods\n\n  /**\n   * Method used to get all the parent runs of a given run.\n   * @param run The run whose parents are to be retrieved.\n   * @returns An array of parent runs.\n   */\n  getParents(run: Run) {\n    const parents: Run[] = [];\n    let currentRun = run;\n    while (currentRun.parent_run_id) {\n      const parent = this.runMap.get(currentRun.parent_run_id);\n      if (parent) {\n        parents.push(parent);\n        currentRun = parent;\n      } else {\n        break;\n      }\n    }\n    return parents;\n  }\n\n  /**\n   * Method used to get a string representation of the run's lineage, which\n   * is used in logging.\n   * @param run The run whose lineage is to be retrieved.\n   * @returns A string representation of the run's lineage.\n   */\n  getBreadcrumbs(run: Run) {\n    const parents = this.getParents(run).reverse();\n    const string = [...parents, run]\n      .map((parent, i, arr) => {\n        const name = `${parent.execution_order}:${parent.run_type}:${parent.name}`;\n        return i === arr.length - 1 ? wrap(styles.bold, name) : name;\n      })\n      .join(\" > \");\n    return wrap(color.grey, string);\n  }\n\n  // logging methods\n\n  /**\n   * Method used to log the start of a chain run.\n   * @param run The chain run that has started.\n   * @returns void\n   */\n  onChainStart(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(\n        color.green,\n        \"[chain/start]\"\n      )} [${crumbs}] Entering Chain run with input: ${tryJsonStringify(\n        run.inputs,\n        \"[inputs]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log the end of a chain run.\n   * @param run The chain run that has ended.\n   * @returns void\n   */\n  onChainEnd(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(color.cyan, \"[chain/end]\")} [${crumbs}] [${elapsed(\n        run\n      )}] Exiting Chain run with output: ${tryJsonStringify(\n        run.outputs,\n        \"[outputs]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log any errors of a chain run.\n   * @param run The chain run that has errored.\n   * @returns void\n   */\n  onChainError(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(color.red, \"[chain/error]\")} [${crumbs}] [${elapsed(\n        run\n      )}] Chain run errored with error: ${tryJsonStringify(\n        run.error,\n        \"[error]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log the start of an LLM run.\n   * @param run The LLM run that has started.\n   * @returns void\n   */\n  onLLMStart(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    const inputs =\n      \"prompts\" in run.inputs\n        ? { prompts: (run.inputs.prompts as string[]).map((p) => p.trim()) }\n        : run.inputs;\n    console.log(\n      `${wrap(\n        color.green,\n        \"[llm/start]\"\n      )} [${crumbs}] Entering LLM run with input: ${tryJsonStringify(\n        inputs,\n        \"[inputs]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log the end of an LLM run.\n   * @param run The LLM run that has ended.\n   * @returns void\n   */\n  onLLMEnd(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(color.cyan, \"[llm/end]\")} [${crumbs}] [${elapsed(\n        run\n      )}] Exiting LLM run with output: ${tryJsonStringify(\n        run.outputs,\n        \"[response]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log any errors of an LLM run.\n   * @param run The LLM run that has errored.\n   * @returns void\n   */\n  onLLMError(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(color.red, \"[llm/error]\")} [${crumbs}] [${elapsed(\n        run\n      )}] LLM run errored with error: ${tryJsonStringify(run.error, \"[error]\")}`\n    );\n  }\n\n  /**\n   * Method used to log the start of a tool run.\n   * @param run The tool run that has started.\n   * @returns void\n   */\n  onToolStart(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(\n        color.green,\n        \"[tool/start]\"\n      )} [${crumbs}] Entering Tool run with input: \"${formatKVMapItem(\n        run.inputs.input\n      )}\"`\n    );\n  }\n\n  /**\n   * Method used to log the end of a tool run.\n   * @param run The tool run that has ended.\n   * @returns void\n   */\n  onToolEnd(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n\n    console.log(\n      `${wrap(color.cyan, \"[tool/end]\")} [${crumbs}] [${elapsed(\n        run\n      )}] Exiting Tool run with output: \"${formatKVMapItem(\n        run.outputs?.output\n      )}\"`\n    );\n  }\n\n  /**\n   * Method used to log any errors of a tool run.\n   * @param run The tool run that has errored.\n   * @returns void\n   */\n  onToolError(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(color.red, \"[tool/error]\")} [${crumbs}] [${elapsed(\n        run\n      )}] Tool run errored with error: ${tryJsonStringify(\n        run.error,\n        \"[error]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log the start of a retriever run.\n   * @param run The retriever run that has started.\n   * @returns void\n   */\n  onRetrieverStart(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(\n        color.green,\n        \"[retriever/start]\"\n      )} [${crumbs}] Entering Retriever run with input: ${tryJsonStringify(\n        run.inputs,\n        \"[inputs]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log the end of a retriever run.\n   * @param run The retriever run that has ended.\n   * @returns void\n   */\n  onRetrieverEnd(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(color.cyan, \"[retriever/end]\")} [${crumbs}] [${elapsed(\n        run\n      )}] Exiting Retriever run with output: ${tryJsonStringify(\n        run.outputs,\n        \"[outputs]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log any errors of a retriever run.\n   * @param run The retriever run that has errored.\n   * @returns void\n   */\n  onRetrieverError(run: Run) {\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(color.red, \"[retriever/error]\")} [${crumbs}] [${elapsed(\n        run\n      )}] Retriever run errored with error: ${tryJsonStringify(\n        run.error,\n        \"[error]\"\n      )}`\n    );\n  }\n\n  /**\n   * Method used to log the action selected by the agent.\n   * @param run The run in which the agent action occurred.\n   * @returns void\n   */\n  onAgentAction(run: Run) {\n    const agentRun = run as AgentRun;\n    const crumbs = this.getBreadcrumbs(run);\n    console.log(\n      `${wrap(\n        color.blue,\n        \"[agent/action]\"\n      )} [${crumbs}] Agent selected action: ${tryJsonStringify(\n        agentRun.actions[agentRun.actions.length - 1],\n        \"[action]\"\n      )}`\n    );\n  }\n}\n"],"mappings":";;;;;;;AAIA,SAAS,KAAK,OAAe,MAAc;AACzC,QAAO,GAAG,MAAM,OAAO,OAAO,MAAM;;AAGtC,SAAS,iBAAiB,KAAc,UA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