UNPKG

@langchain/core

Version:
1 lines 13 kB
{"version":3,"file":"console.cjs","names":["BaseTracer"],"sources":["../../src/tracers/console.ts"],"sourcesContent":["import { BaseTracer, type AgentRun, type Run } from \"./base.js\";\n\ninterface CSPair {\n open: string;\n close: string;\n}\n\nconst styles: {\n bold: CSPair;\n color: Record<\"grey\" | \"green\" | \"cyan\" | \"red\" | \"blue\", CSPair>;\n} = {\n bold: { open: \"\\u001b[1m\", close: \"\\u001b[22m\" },\n color: {\n grey: { open: \"\\u001b[90m\", close: \"\\u001b[39m\" },\n green: { open: \"\\u001b[32m\", close: \"\\u001b[39m\" },\n cyan: { open: \"\\u001b[36m\", close: \"\\u001b[39m\" },\n red: { open: \"\\u001b[31m\", close: \"\\u001b[39m\" },\n blue: { open: \"\\u001b[34m\", close: \"\\u001b[39m\" },\n },\n};\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":";;;;;AAOA,MAAM,SAGF;CACF,MAAM;EAAE,MAAM;EAAa,OAAO;CAAa;CAC/C,OAAO;EACL,MAA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