console.cjs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.ConsoleCallbackHandler = void 0;
  7. const ansi_styles_1 = __importDefault(require("ansi-styles"));
  8. const base_js_1 = require("./base.cjs");
  9. function wrap(style, text) {
  10. return `${style.open}${text}${style.close}`;
  11. }
  12. function tryJsonStringify(obj, fallback) {
  13. try {
  14. return JSON.stringify(obj, null, 2);
  15. }
  16. catch (err) {
  17. return fallback;
  18. }
  19. }
  20. function formatKVMapItem(value) {
  21. if (typeof value === "string") {
  22. return value.trim();
  23. }
  24. if (value === null || value === undefined) {
  25. return value;
  26. }
  27. return tryJsonStringify(value, value.toString());
  28. }
  29. function elapsed(run) {
  30. if (!run.end_time)
  31. return "";
  32. const elapsed = run.end_time - run.start_time;
  33. if (elapsed < 1000) {
  34. return `${elapsed}ms`;
  35. }
  36. return `${(elapsed / 1000).toFixed(2)}s`;
  37. }
  38. const { color } = ansi_styles_1.default;
  39. /**
  40. * A tracer that logs all events to the console. It extends from the
  41. * `BaseTracer` class and overrides its methods to provide custom logging
  42. * functionality.
  43. * @example
  44. * ```typescript
  45. *
  46. * const llm = new ChatAnthropic({
  47. * temperature: 0,
  48. * tags: ["example", "callbacks", "constructor"],
  49. * callbacks: [new ConsoleCallbackHandler()],
  50. * });
  51. *
  52. * ```
  53. */
  54. class ConsoleCallbackHandler extends base_js_1.BaseTracer {
  55. constructor() {
  56. super(...arguments);
  57. Object.defineProperty(this, "name", {
  58. enumerable: true,
  59. configurable: true,
  60. writable: true,
  61. value: "console_callback_handler"
  62. });
  63. }
  64. /**
  65. * Method used to persist the run. In this case, it simply returns a
  66. * resolved promise as there's no persistence logic.
  67. * @param _run The run to persist.
  68. * @returns A resolved promise.
  69. */
  70. persistRun(_run) {
  71. return Promise.resolve();
  72. }
  73. // utility methods
  74. /**
  75. * Method used to get all the parent runs of a given run.
  76. * @param run The run whose parents are to be retrieved.
  77. * @returns An array of parent runs.
  78. */
  79. getParents(run) {
  80. const parents = [];
  81. let currentRun = run;
  82. while (currentRun.parent_run_id) {
  83. const parent = this.runMap.get(currentRun.parent_run_id);
  84. if (parent) {
  85. parents.push(parent);
  86. currentRun = parent;
  87. }
  88. else {
  89. break;
  90. }
  91. }
  92. return parents;
  93. }
  94. /**
  95. * Method used to get a string representation of the run's lineage, which
  96. * is used in logging.
  97. * @param run The run whose lineage is to be retrieved.
  98. * @returns A string representation of the run's lineage.
  99. */
  100. getBreadcrumbs(run) {
  101. const parents = this.getParents(run).reverse();
  102. const string = [...parents, run]
  103. .map((parent, i, arr) => {
  104. const name = `${parent.execution_order}:${parent.run_type}:${parent.name}`;
  105. return i === arr.length - 1 ? wrap(ansi_styles_1.default.bold, name) : name;
  106. })
  107. .join(" > ");
  108. return wrap(color.grey, string);
  109. }
  110. // logging methods
  111. /**
  112. * Method used to log the start of a chain run.
  113. * @param run The chain run that has started.
  114. * @returns void
  115. */
  116. onChainStart(run) {
  117. const crumbs = this.getBreadcrumbs(run);
  118. console.log(`${wrap(color.green, "[chain/start]")} [${crumbs}] Entering Chain run with input: ${tryJsonStringify(run.inputs, "[inputs]")}`);
  119. }
  120. /**
  121. * Method used to log the end of a chain run.
  122. * @param run The chain run that has ended.
  123. * @returns void
  124. */
  125. onChainEnd(run) {
  126. const crumbs = this.getBreadcrumbs(run);
  127. console.log(`${wrap(color.cyan, "[chain/end]")} [${crumbs}] [${elapsed(run)}] Exiting Chain run with output: ${tryJsonStringify(run.outputs, "[outputs]")}`);
  128. }
  129. /**
  130. * Method used to log any errors of a chain run.
  131. * @param run The chain run that has errored.
  132. * @returns void
  133. */
  134. onChainError(run) {
  135. const crumbs = this.getBreadcrumbs(run);
  136. console.log(`${wrap(color.red, "[chain/error]")} [${crumbs}] [${elapsed(run)}] Chain run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
  137. }
  138. /**
  139. * Method used to log the start of an LLM run.
  140. * @param run The LLM run that has started.
  141. * @returns void
  142. */
  143. onLLMStart(run) {
  144. const crumbs = this.getBreadcrumbs(run);
  145. const inputs = "prompts" in run.inputs
  146. ? { prompts: run.inputs.prompts.map((p) => p.trim()) }
  147. : run.inputs;
  148. console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify(inputs, "[inputs]")}`);
  149. }
  150. /**
  151. * Method used to log the end of an LLM run.
  152. * @param run The LLM run that has ended.
  153. * @returns void
  154. */
  155. onLLMEnd(run) {
  156. const crumbs = this.getBreadcrumbs(run);
  157. console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.outputs, "[response]")}`);
  158. }
  159. /**
  160. * Method used to log any errors of an LLM run.
  161. * @param run The LLM run that has errored.
  162. * @returns void
  163. */
  164. onLLMError(run) {
  165. const crumbs = this.getBreadcrumbs(run);
  166. console.log(`${wrap(color.red, "[llm/error]")} [${crumbs}] [${elapsed(run)}] LLM run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
  167. }
  168. /**
  169. * Method used to log the start of a tool run.
  170. * @param run The tool run that has started.
  171. * @returns void
  172. */
  173. onToolStart(run) {
  174. const crumbs = this.getBreadcrumbs(run);
  175. console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${formatKVMapItem(run.inputs.input)}"`);
  176. }
  177. /**
  178. * Method used to log the end of a tool run.
  179. * @param run The tool run that has ended.
  180. * @returns void
  181. */
  182. onToolEnd(run) {
  183. const crumbs = this.getBreadcrumbs(run);
  184. console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${formatKVMapItem(run.outputs?.output)}"`);
  185. }
  186. /**
  187. * Method used to log any errors of a tool run.
  188. * @param run The tool run that has errored.
  189. * @returns void
  190. */
  191. onToolError(run) {
  192. const crumbs = this.getBreadcrumbs(run);
  193. console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
  194. }
  195. /**
  196. * Method used to log the start of a retriever run.
  197. * @param run The retriever run that has started.
  198. * @returns void
  199. */
  200. onRetrieverStart(run) {
  201. const crumbs = this.getBreadcrumbs(run);
  202. console.log(`${wrap(color.green, "[retriever/start]")} [${crumbs}] Entering Retriever run with input: ${tryJsonStringify(run.inputs, "[inputs]")}`);
  203. }
  204. /**
  205. * Method used to log the end of a retriever run.
  206. * @param run The retriever run that has ended.
  207. * @returns void
  208. */
  209. onRetrieverEnd(run) {
  210. const crumbs = this.getBreadcrumbs(run);
  211. console.log(`${wrap(color.cyan, "[retriever/end]")} [${crumbs}] [${elapsed(run)}] Exiting Retriever run with output: ${tryJsonStringify(run.outputs, "[outputs]")}`);
  212. }
  213. /**
  214. * Method used to log any errors of a retriever run.
  215. * @param run The retriever run that has errored.
  216. * @returns void
  217. */
  218. onRetrieverError(run) {
  219. const crumbs = this.getBreadcrumbs(run);
  220. console.log(`${wrap(color.red, "[retriever/error]")} [${crumbs}] [${elapsed(run)}] Retriever run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
  221. }
  222. /**
  223. * Method used to log the action selected by the agent.
  224. * @param run The run in which the agent action occurred.
  225. * @returns void
  226. */
  227. onAgentAction(run) {
  228. const agentRun = run;
  229. const crumbs = this.getBreadcrumbs(run);
  230. console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(agentRun.actions[agentRun.actions.length - 1], "[action]")}`);
  231. }
  232. }
  233. exports.ConsoleCallbackHandler = ConsoleCallbackHandler;