langchain 0.0.65 → 0.0.67

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.
Files changed (72) hide show
  1. package/dist/agents/chat/outputParser.cjs +4 -3
  2. package/dist/agents/chat/outputParser.js +4 -3
  3. package/dist/agents/chat_convo/outputParser.cjs +6 -7
  4. package/dist/agents/chat_convo/outputParser.js +6 -7
  5. package/dist/callbacks/handlers/console.cjs +104 -22
  6. package/dist/callbacks/handlers/console.d.ts +25 -17
  7. package/dist/callbacks/handlers/console.js +101 -22
  8. package/dist/callbacks/handlers/tracers.cjs +49 -23
  9. package/dist/callbacks/handlers/tracers.d.ts +24 -8
  10. package/dist/callbacks/handlers/tracers.js +49 -23
  11. package/dist/callbacks/manager.cjs +14 -3
  12. package/dist/callbacks/manager.js +14 -3
  13. package/dist/chains/index.d.ts +1 -1
  14. package/dist/chains/index.js +1 -1
  15. package/dist/chains/sql_db/sql_db_chain.cjs +1 -1
  16. package/dist/chains/sql_db/sql_db_chain.js +1 -1
  17. package/dist/chains/summarization/load.cjs +18 -5
  18. package/dist/chains/summarization/load.d.ts +11 -6
  19. package/dist/chains/summarization/load.js +19 -6
  20. package/dist/chains/summarization/refine_prompts.cjs +20 -0
  21. package/dist/chains/summarization/refine_prompts.d.ts +2 -0
  22. package/dist/chains/summarization/refine_prompts.js +17 -0
  23. package/dist/chat_models/base.cjs +2 -2
  24. package/dist/chat_models/base.js +2 -2
  25. package/dist/chat_models/openai.cjs +1 -1
  26. package/dist/chat_models/openai.js +1 -1
  27. package/dist/embeddings/hf.cjs +52 -0
  28. package/dist/embeddings/hf.d.ts +15 -0
  29. package/dist/embeddings/hf.js +48 -0
  30. package/dist/llms/openai-chat.cjs +60 -2
  31. package/dist/llms/openai-chat.d.ts +12 -0
  32. package/dist/llms/openai-chat.js +58 -1
  33. package/dist/llms/openai.cjs +2 -1
  34. package/dist/llms/openai.d.ts +1 -1
  35. package/dist/llms/openai.js +1 -1
  36. package/dist/output_parsers/combining.cjs +20 -8
  37. package/dist/output_parsers/combining.js +20 -8
  38. package/dist/output_parsers/structured.cjs +11 -61
  39. package/dist/output_parsers/structured.js +12 -62
  40. package/dist/prompts/base.cjs +2 -1
  41. package/dist/prompts/base.d.ts +1 -1
  42. package/dist/prompts/base.js +3 -2
  43. package/dist/retrievers/hyde.cjs +97 -0
  44. package/dist/retrievers/hyde.d.ts +16 -0
  45. package/dist/retrievers/hyde.js +92 -0
  46. package/dist/tools/aiplugin.cjs +1 -1
  47. package/dist/tools/aiplugin.js +1 -1
  48. package/dist/tools/base.cjs +6 -0
  49. package/dist/tools/base.d.ts +6 -0
  50. package/dist/tools/base.js +6 -0
  51. package/dist/tools/dynamic.cjs +3 -0
  52. package/dist/tools/dynamic.d.ts +3 -0
  53. package/dist/tools/dynamic.js +3 -0
  54. package/dist/tools/requests.cjs +20 -4
  55. package/dist/tools/requests.d.ts +9 -2
  56. package/dist/tools/requests.js +20 -4
  57. package/dist/tools/webbrowser.cjs +14 -9
  58. package/dist/tools/webbrowser.d.ts +1 -0
  59. package/dist/tools/webbrowser.js +12 -8
  60. package/dist/util/axios-fetch-adapter.cjs +24 -1
  61. package/dist/util/axios-fetch-adapter.js +24 -1
  62. package/dist/vectorstores/base.d.ts +6 -5
  63. package/dist/vectorstores/hnswlib.cjs +13 -2
  64. package/dist/vectorstores/hnswlib.d.ts +2 -1
  65. package/dist/vectorstores/hnswlib.js +13 -2
  66. package/embeddings/hf.cjs +1 -0
  67. package/embeddings/hf.d.ts +1 -0
  68. package/embeddings/hf.js +1 -0
  69. package/package.json +19 -2
  70. package/retrievers/hyde.cjs +1 -0
  71. package/retrievers/hyde.d.ts +1 -0
  72. package/retrievers/hyde.js +1 -0
@@ -6,13 +6,14 @@ const prompt_js_1 = require("./prompt.cjs");
6
6
  exports.FINAL_ANSWER_ACTION = "Final Answer:";
7
7
  class ChatAgentOutputParser extends types_js_1.AgentActionOutputParser {
8
8
  async parse(text) {
9
- if (text.includes(exports.FINAL_ANSWER_ACTION)) {
9
+ if (text.includes(exports.FINAL_ANSWER_ACTION) || !text.includes(`"action":`)) {
10
10
  const parts = text.split(exports.FINAL_ANSWER_ACTION);
11
11
  const output = parts[parts.length - 1].trim();
12
12
  return { returnValues: { output }, log: text };
13
13
  }
14
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
- const [_, action, __] = text.split(/```(?:json)?/g);
14
+ const action = text.includes("```")
15
+ ? text.trim().split(/```(?:json)?/)[1]
16
+ : text.trim();
16
17
  try {
17
18
  const response = JSON.parse(action.trim());
18
19
  return {
@@ -3,13 +3,14 @@ import { FORMAT_INSTRUCTIONS } from "./prompt.js";
3
3
  export const FINAL_ANSWER_ACTION = "Final Answer:";
4
4
  export class ChatAgentOutputParser extends AgentActionOutputParser {
5
5
  async parse(text) {
6
- if (text.includes(FINAL_ANSWER_ACTION)) {
6
+ if (text.includes(FINAL_ANSWER_ACTION) || !text.includes(`"action":`)) {
7
7
  const parts = text.split(FINAL_ANSWER_ACTION);
8
8
  const output = parts[parts.length - 1].trim();
9
9
  return { returnValues: { output }, log: text };
10
10
  }
11
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
12
- const [_, action, __] = text.split(/```(?:json)?/g);
11
+ const action = text.includes("```")
12
+ ? text.trim().split(/```(?:json)?/)[1]
13
+ : text.trim();
13
14
  try {
14
15
  const response = JSON.parse(action.trim());
15
16
  return {
@@ -9,14 +9,13 @@ class ChatConversationalAgentOutputParser extends types_js_1.AgentActionOutputPa
9
9
  if (jsonOutput.includes("```json")) {
10
10
  jsonOutput = jsonOutput.split("```json")[1].trimStart();
11
11
  }
12
- if (jsonOutput.includes("```")) {
13
- jsonOutput = jsonOutput.split("```")[0].trimEnd();
12
+ else if (jsonOutput.includes("```")) {
13
+ const firstIndex = jsonOutput.indexOf("```");
14
+ jsonOutput = jsonOutput.slice(firstIndex + 3).trimStart();
14
15
  }
15
- if (jsonOutput.startsWith("```")) {
16
- jsonOutput = jsonOutput.slice(3).trimStart();
17
- }
18
- if (jsonOutput.endsWith("```")) {
19
- jsonOutput = jsonOutput.slice(0, -3).trimEnd();
16
+ const lastIndex = jsonOutput.lastIndexOf("```");
17
+ if (lastIndex !== -1) {
18
+ jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
20
19
  }
21
20
  const response = JSON.parse(jsonOutput);
22
21
  const { action, action_input } = response;
@@ -6,14 +6,13 @@ export class ChatConversationalAgentOutputParser extends AgentActionOutputParser
6
6
  if (jsonOutput.includes("```json")) {
7
7
  jsonOutput = jsonOutput.split("```json")[1].trimStart();
8
8
  }
9
- if (jsonOutput.includes("```")) {
10
- jsonOutput = jsonOutput.split("```")[0].trimEnd();
9
+ else if (jsonOutput.includes("```")) {
10
+ const firstIndex = jsonOutput.indexOf("```");
11
+ jsonOutput = jsonOutput.slice(firstIndex + 3).trimStart();
11
12
  }
12
- if (jsonOutput.startsWith("```")) {
13
- jsonOutput = jsonOutput.slice(3).trimStart();
14
- }
15
- if (jsonOutput.endsWith("```")) {
16
- jsonOutput = jsonOutput.slice(0, -3).trimEnd();
13
+ const lastIndex = jsonOutput.lastIndexOf("```");
14
+ if (lastIndex !== -1) {
15
+ jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
17
16
  }
18
17
  const response = JSON.parse(jsonOutput);
19
18
  const { action, action_input } = response;
@@ -1,44 +1,126 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.ConsoleCallbackHandler = void 0;
4
- const base_js_1 = require("../base.cjs");
5
- class ConsoleCallbackHandler extends base_js_1.BaseCallbackHandler {
7
+ const ansi_styles_1 = __importDefault(require("ansi-styles"));
8
+ const tracers_js_1 = require("./tracers.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 elapsed(run) {
21
+ const elapsed = run.end_time - run.start_time;
22
+ if (elapsed < 1000) {
23
+ return `${elapsed}ms`;
24
+ }
25
+ return `${(elapsed / 1000).toFixed(2)}s`;
26
+ }
27
+ const { color } = ansi_styles_1.default;
28
+ class ConsoleCallbackHandler extends tracers_js_1.BaseTracer {
29
+ // boilerplate to work with the base tracer class
6
30
  constructor() {
7
- super(...arguments);
31
+ super();
8
32
  Object.defineProperty(this, "name", {
9
33
  enumerable: true,
10
34
  configurable: true,
11
35
  writable: true,
12
36
  value: "console_callback_handler"
13
37
  });
38
+ Object.defineProperty(this, "i", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: 0
43
+ });
44
+ }
45
+ persistSession(session) {
46
+ // eslint-disable-next-line no-plusplus
47
+ return Promise.resolve({ ...session, id: this.i++ });
48
+ }
49
+ persistRun(_run) {
50
+ return Promise.resolve();
51
+ }
52
+ loadDefaultSession() {
53
+ return this.newSession();
54
+ }
55
+ loadSession(sessionName) {
56
+ return this.newSession(sessionName);
57
+ }
58
+ // utility methods
59
+ getParents(run) {
60
+ const parents = [];
61
+ let currentRun = run;
62
+ while (currentRun.parent_uuid) {
63
+ const parent = this.runMap.get(currentRun.parent_uuid);
64
+ if (parent) {
65
+ parents.push(parent);
66
+ currentRun = parent;
67
+ }
68
+ else {
69
+ break;
70
+ }
71
+ }
72
+ return parents;
73
+ }
74
+ getBreadcrumbs(run) {
75
+ const parents = this.getParents(run).reverse();
76
+ const string = [...parents, run]
77
+ .map((parent, i, arr) => {
78
+ const name = `${parent.execution_order}:${parent.type}:${parent.serialized?.name}`;
79
+ return i === arr.length - 1 ? wrap(ansi_styles_1.default.bold, name) : name;
80
+ })
81
+ .join(" > ");
82
+ return wrap(color.grey, string);
83
+ }
84
+ // logging methods
85
+ onChainStart(run) {
86
+ const crumbs = this.getBreadcrumbs(run);
87
+ console.log(`${wrap(color.green, "[chain/start]")} [${crumbs}] Entering Chain run with input: ${tryJsonStringify(run.inputs, "[inputs]")}`);
14
88
  }
15
- handleLLMStart(llm, prompts, runId) {
16
- console.log(`Starting LLM ${runId} with name ${llm.name} with prompts: ${prompts.join(", ")}\n`);
89
+ onChainEnd(run) {
90
+ const crumbs = this.getBreadcrumbs(run);
91
+ console.log(`${wrap(color.cyan, "[chain/end]")} [${crumbs}] [${elapsed(run)}] Exiting Chain run with output: ${tryJsonStringify(run.outputs, "[outputs]")}`);
17
92
  }
18
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
- handleLLMError(err, runId) {
20
- console.log(`LLM ${runId} errored: ${err}\n`);
93
+ onChainError(run) {
94
+ const crumbs = this.getBreadcrumbs(run);
95
+ console.log(`${wrap(color.red, "[chain/error]")} [${crumbs}] [${elapsed(run)}] Chain run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
21
96
  }
22
- handleLLMEnd(output, runId) {
23
- console.log(`LLM ${runId} finished: ${JSON.stringify(output)}\n`);
97
+ onLLMStart(run) {
98
+ const crumbs = this.getBreadcrumbs(run);
99
+ console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify({ prompts: run.prompts.map((p) => p.trim()) }, "[inputs]")}`);
24
100
  }
25
- handleChainStart(chain) {
26
- console.log(`Entering new ${chain.name} chain...`);
101
+ onLLMEnd(run) {
102
+ const crumbs = this.getBreadcrumbs(run);
103
+ console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.response, "[response]")}`);
27
104
  }
28
- handleChainEnd(_output) {
29
- console.log("Finished chain.");
105
+ onLLMError(run) {
106
+ const crumbs = this.getBreadcrumbs(run);
107
+ console.log(`${wrap(color.red, "[llm/error]")} [${crumbs}] [${elapsed(run)}] LLM run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
30
108
  }
31
- handleAgentAction(action) {
32
- console.log(action.log);
109
+ onToolStart(run) {
110
+ const crumbs = this.getBreadcrumbs(run);
111
+ console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.tool_input?.trim()}"`);
33
112
  }
34
- handleToolEnd(output) {
35
- console.log(output);
113
+ onToolEnd(run) {
114
+ const crumbs = this.getBreadcrumbs(run);
115
+ console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.output?.trim()}"`);
36
116
  }
37
- handleText(text) {
38
- console.log(text);
117
+ onToolError(run) {
118
+ const crumbs = this.getBreadcrumbs(run);
119
+ console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
39
120
  }
40
- handleAgentEnd(action) {
41
- console.log(action.log);
121
+ onAgentAction(run) {
122
+ const crumbs = this.getBreadcrumbs(run);
123
+ console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(run.actions[run.actions.length - 1], "[action]")}`);
42
124
  }
43
125
  }
44
126
  exports.ConsoleCallbackHandler = ConsoleCallbackHandler;
@@ -1,18 +1,26 @@
1
- import { AgentAction, AgentFinish, ChainValues, LLMResult } from "../../schema/index.js";
2
- import { BaseCallbackHandler } from "../base.js";
3
- export declare class ConsoleCallbackHandler extends BaseCallbackHandler {
4
- name: string;
5
- handleLLMStart(llm: {
6
- name: string;
7
- }, prompts: string[], runId: string): void;
8
- handleLLMError(err: any, runId: string): void;
9
- handleLLMEnd(output: LLMResult, runId: string): void;
10
- handleChainStart(chain: {
11
- name: string;
12
- }): void;
13
- handleChainEnd(_output: ChainValues): void;
14
- handleAgentAction(action: AgentAction): void;
15
- handleToolEnd(output: string): void;
16
- handleText(text: string): void;
17
- handleAgentEnd(action: AgentFinish): void;
1
+ import { AgentRun, BaseTracer, BaseTracerSession, ChainRun, LLMRun, Run, ToolRun } from "./tracers.js";
2
+ export declare class ConsoleCallbackHandler extends BaseTracer {
3
+ name: "console_callback_handler";
4
+ constructor();
5
+ i: number;
6
+ protected persistSession(session: BaseTracerSession): Promise<{
7
+ id: number;
8
+ start_time: number;
9
+ name?: string | undefined;
10
+ }>;
11
+ protected persistRun(_run: Run): Promise<void>;
12
+ loadDefaultSession(): Promise<import("./tracers.js").TracerSession>;
13
+ loadSession(sessionName: string): Promise<import("./tracers.js").TracerSession>;
14
+ getParents(run: Run): Run[];
15
+ getBreadcrumbs(run: Run): string;
16
+ onChainStart(run: ChainRun): void;
17
+ onChainEnd(run: ChainRun): void;
18
+ onChainError(run: ChainRun): void;
19
+ onLLMStart(run: LLMRun): void;
20
+ onLLMEnd(run: LLMRun): void;
21
+ onLLMError(run: LLMRun): void;
22
+ onToolStart(run: ToolRun): void;
23
+ onToolEnd(run: ToolRun): void;
24
+ onToolError(run: ToolRun): void;
25
+ onAgentAction(run: AgentRun): void;
18
26
  }
@@ -1,40 +1,119 @@
1
- import { BaseCallbackHandler } from "../base.js";
2
- export class ConsoleCallbackHandler extends BaseCallbackHandler {
1
+ import styles from "ansi-styles";
2
+ import { BaseTracer, } from "./tracers.js";
3
+ function wrap(style, text) {
4
+ return `${style.open}${text}${style.close}`;
5
+ }
6
+ function tryJsonStringify(obj, fallback) {
7
+ try {
8
+ return JSON.stringify(obj, null, 2);
9
+ }
10
+ catch (err) {
11
+ return fallback;
12
+ }
13
+ }
14
+ function elapsed(run) {
15
+ const elapsed = run.end_time - run.start_time;
16
+ if (elapsed < 1000) {
17
+ return `${elapsed}ms`;
18
+ }
19
+ return `${(elapsed / 1000).toFixed(2)}s`;
20
+ }
21
+ const { color } = styles;
22
+ export class ConsoleCallbackHandler extends BaseTracer {
23
+ // boilerplate to work with the base tracer class
3
24
  constructor() {
4
- super(...arguments);
25
+ super();
5
26
  Object.defineProperty(this, "name", {
6
27
  enumerable: true,
7
28
  configurable: true,
8
29
  writable: true,
9
30
  value: "console_callback_handler"
10
31
  });
32
+ Object.defineProperty(this, "i", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: 0
37
+ });
38
+ }
39
+ persistSession(session) {
40
+ // eslint-disable-next-line no-plusplus
41
+ return Promise.resolve({ ...session, id: this.i++ });
42
+ }
43
+ persistRun(_run) {
44
+ return Promise.resolve();
45
+ }
46
+ loadDefaultSession() {
47
+ return this.newSession();
48
+ }
49
+ loadSession(sessionName) {
50
+ return this.newSession(sessionName);
51
+ }
52
+ // utility methods
53
+ getParents(run) {
54
+ const parents = [];
55
+ let currentRun = run;
56
+ while (currentRun.parent_uuid) {
57
+ const parent = this.runMap.get(currentRun.parent_uuid);
58
+ if (parent) {
59
+ parents.push(parent);
60
+ currentRun = parent;
61
+ }
62
+ else {
63
+ break;
64
+ }
65
+ }
66
+ return parents;
67
+ }
68
+ getBreadcrumbs(run) {
69
+ const parents = this.getParents(run).reverse();
70
+ const string = [...parents, run]
71
+ .map((parent, i, arr) => {
72
+ const name = `${parent.execution_order}:${parent.type}:${parent.serialized?.name}`;
73
+ return i === arr.length - 1 ? wrap(styles.bold, name) : name;
74
+ })
75
+ .join(" > ");
76
+ return wrap(color.grey, string);
77
+ }
78
+ // logging methods
79
+ onChainStart(run) {
80
+ const crumbs = this.getBreadcrumbs(run);
81
+ console.log(`${wrap(color.green, "[chain/start]")} [${crumbs}] Entering Chain run with input: ${tryJsonStringify(run.inputs, "[inputs]")}`);
11
82
  }
12
- handleLLMStart(llm, prompts, runId) {
13
- console.log(`Starting LLM ${runId} with name ${llm.name} with prompts: ${prompts.join(", ")}\n`);
83
+ onChainEnd(run) {
84
+ const crumbs = this.getBreadcrumbs(run);
85
+ console.log(`${wrap(color.cyan, "[chain/end]")} [${crumbs}] [${elapsed(run)}] Exiting Chain run with output: ${tryJsonStringify(run.outputs, "[outputs]")}`);
14
86
  }
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- handleLLMError(err, runId) {
17
- console.log(`LLM ${runId} errored: ${err}\n`);
87
+ onChainError(run) {
88
+ const crumbs = this.getBreadcrumbs(run);
89
+ console.log(`${wrap(color.red, "[chain/error]")} [${crumbs}] [${elapsed(run)}] Chain run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
18
90
  }
19
- handleLLMEnd(output, runId) {
20
- console.log(`LLM ${runId} finished: ${JSON.stringify(output)}\n`);
91
+ onLLMStart(run) {
92
+ const crumbs = this.getBreadcrumbs(run);
93
+ console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify({ prompts: run.prompts.map((p) => p.trim()) }, "[inputs]")}`);
21
94
  }
22
- handleChainStart(chain) {
23
- console.log(`Entering new ${chain.name} chain...`);
95
+ onLLMEnd(run) {
96
+ const crumbs = this.getBreadcrumbs(run);
97
+ console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.response, "[response]")}`);
24
98
  }
25
- handleChainEnd(_output) {
26
- console.log("Finished chain.");
99
+ onLLMError(run) {
100
+ const crumbs = this.getBreadcrumbs(run);
101
+ console.log(`${wrap(color.red, "[llm/error]")} [${crumbs}] [${elapsed(run)}] LLM run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
27
102
  }
28
- handleAgentAction(action) {
29
- console.log(action.log);
103
+ onToolStart(run) {
104
+ const crumbs = this.getBreadcrumbs(run);
105
+ console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.tool_input?.trim()}"`);
30
106
  }
31
- handleToolEnd(output) {
32
- console.log(output);
107
+ onToolEnd(run) {
108
+ const crumbs = this.getBreadcrumbs(run);
109
+ console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.output?.trim()}"`);
33
110
  }
34
- handleText(text) {
35
- console.log(text);
111
+ onToolError(run) {
112
+ const crumbs = this.getBreadcrumbs(run);
113
+ console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
36
114
  }
37
- handleAgentEnd(action) {
38
- console.log(action.log);
115
+ onAgentAction(run) {
116
+ const crumbs = this.getBreadcrumbs(run);
117
+ console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(run.actions[run.actions.length - 1], "[action]")}`);
39
118
  }
40
119
  }
@@ -17,12 +17,9 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
17
17
  writable: true,
18
18
  value: new Map()
19
19
  });
20
- Object.defineProperty(this, "executionOrder", {
21
- enumerable: true,
22
- configurable: true,
23
- writable: true,
24
- value: 1
25
- });
20
+ }
21
+ copy() {
22
+ return this;
26
23
  }
27
24
  async newSession(sessionName) {
28
25
  const sessionCreate = {
@@ -48,7 +45,6 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
48
45
  }
49
46
  }
50
47
  _startTrace(run) {
51
- this.executionOrder += 1;
52
48
  if (run.parent_uuid) {
53
49
  const parentRun = this.runMap.get(run.parent_uuid);
54
50
  if (parentRun) {
@@ -68,14 +64,32 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
68
64
  async _endTrace(run) {
69
65
  if (!run.parent_uuid) {
70
66
  await this.persistRun(run);
71
- this.executionOrder = 1;
67
+ }
68
+ else {
69
+ const parentRun = this.runMap.get(run.parent_uuid);
70
+ if (parentRun === undefined) {
71
+ throw new Error(`Parent run ${run.parent_uuid} not found`);
72
+ }
73
+ parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
72
74
  }
73
75
  this.runMap.delete(run.uuid);
74
76
  }
77
+ _getExecutionOrder(parentRunId) {
78
+ // If a run has no parent then execution order is 1
79
+ if (parentRunId === undefined) {
80
+ return 1;
81
+ }
82
+ const parentRun = this.runMap.get(parentRunId);
83
+ if (parentRun === undefined) {
84
+ throw new Error(`Parent run ${parentRunId} not found`);
85
+ }
86
+ return parentRun.child_execution_order + 1;
87
+ }
75
88
  async handleLLMStart(llm, prompts, runId, parentRunId) {
76
89
  if (this.session === undefined) {
77
90
  this.session = await this.loadDefaultSession();
78
91
  }
92
+ const execution_order = this._getExecutionOrder(parentRunId);
79
93
  const run = {
80
94
  uuid: runId,
81
95
  parent_uuid: parentRunId,
@@ -84,10 +98,12 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
84
98
  serialized: llm,
85
99
  prompts,
86
100
  session_id: this.session.id,
87
- execution_order: this.executionOrder,
101
+ execution_order,
102
+ child_execution_order: execution_order,
88
103
  type: "llm",
89
104
  };
90
105
  this._startTrace(run);
106
+ await this.onLLMStart?.(run);
91
107
  }
92
108
  async handleLLMEnd(output, runId) {
93
109
  const run = this.runMap.get(runId);
@@ -97,6 +113,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
97
113
  const llmRun = run;
98
114
  llmRun.end_time = Date.now();
99
115
  llmRun.response = output;
116
+ await this.onLLMEnd?.(llmRun);
100
117
  await this._endTrace(llmRun);
101
118
  }
102
119
  async handleLLMError(error, runId) {
@@ -107,12 +124,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
107
124
  const llmRun = run;
108
125
  llmRun.end_time = Date.now();
109
126
  llmRun.error = error.message;
127
+ await this.onLLMError?.(llmRun);
110
128
  await this._endTrace(llmRun);
111
129
  }
112
130
  async handleChainStart(chain, inputs, runId, parentRunId) {
113
131
  if (this.session === undefined) {
114
132
  this.session = await this.loadDefaultSession();
115
133
  }
134
+ const execution_order = this._getExecutionOrder(parentRunId);
116
135
  const run = {
117
136
  uuid: runId,
118
137
  parent_uuid: parentRunId,
@@ -121,13 +140,15 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
121
140
  serialized: chain,
122
141
  inputs,
123
142
  session_id: this.session.id,
124
- execution_order: this.executionOrder,
143
+ execution_order,
144
+ child_execution_order: execution_order,
125
145
  type: "chain",
126
146
  child_llm_runs: [],
127
147
  child_chain_runs: [],
128
148
  child_tool_runs: [],
129
149
  };
130
150
  this._startTrace(run);
151
+ await this.onChainStart?.(run);
131
152
  }
132
153
  async handleChainEnd(outputs, runId) {
133
154
  const run = this.runMap.get(runId);
@@ -137,6 +158,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
137
158
  const chainRun = run;
138
159
  chainRun.end_time = Date.now();
139
160
  chainRun.outputs = outputs;
161
+ await this.onChainEnd?.(chainRun);
140
162
  await this._endTrace(chainRun);
141
163
  }
142
164
  async handleChainError(error, runId) {
@@ -147,12 +169,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
147
169
  const chainRun = run;
148
170
  chainRun.end_time = Date.now();
149
171
  chainRun.error = error.message;
172
+ await this.onChainError?.(chainRun);
150
173
  await this._endTrace(chainRun);
151
174
  }
152
175
  async handleToolStart(tool, input, runId, parentRunId) {
153
176
  if (this.session === undefined) {
154
177
  this.session = await this.loadDefaultSession();
155
178
  }
179
+ const execution_order = this._getExecutionOrder(parentRunId);
156
180
  const run = {
157
181
  uuid: runId,
158
182
  parent_uuid: parentRunId,
@@ -161,7 +185,8 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
161
185
  serialized: tool,
162
186
  tool_input: input,
163
187
  session_id: this.session.id,
164
- execution_order: this.executionOrder,
188
+ execution_order,
189
+ child_execution_order: execution_order,
165
190
  type: "tool",
166
191
  action: JSON.stringify(tool),
167
192
  child_llm_runs: [],
@@ -169,6 +194,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
169
194
  child_tool_runs: [],
170
195
  };
171
196
  this._startTrace(run);
197
+ await this.onToolStart?.(run);
172
198
  }
173
199
  async handleToolEnd(output, runId) {
174
200
  const run = this.runMap.get(runId);
@@ -178,6 +204,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
178
204
  const toolRun = run;
179
205
  toolRun.end_time = Date.now();
180
206
  toolRun.output = output;
207
+ await this.onToolEnd?.(toolRun);
181
208
  await this._endTrace(toolRun);
182
209
  }
183
210
  async handleToolError(error, runId) {
@@ -188,8 +215,19 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
188
215
  const toolRun = run;
189
216
  toolRun.end_time = Date.now();
190
217
  toolRun.error = error.message;
218
+ await this.onToolError?.(toolRun);
191
219
  await this._endTrace(toolRun);
192
220
  }
221
+ async handleAgentAction(action, runId) {
222
+ const run = this.runMap.get(runId);
223
+ if (!run || run?.type !== "chain") {
224
+ return;
225
+ }
226
+ const agentRun = run;
227
+ agentRun.actions = agentRun.actions || [];
228
+ agentRun.actions.push(action);
229
+ await this.onAgentAction?.(run);
230
+ }
193
231
  }
194
232
  exports.BaseTracer = BaseTracer;
195
233
  class LangChainTracer extends BaseTracer {
@@ -299,17 +337,5 @@ class LangChainTracer extends BaseTracer {
299
337
  this.session = tracerSession;
300
338
  return tracerSession;
301
339
  }
302
- copy() {
303
- // TODO: this is a hack to get tracing to work with the current backend
304
- // we need to not use execution order, then remove this check
305
- if (this.executionOrder === 1) {
306
- const copy = new LangChainTracer();
307
- copy.session = this.session;
308
- copy.runMap = new Map(this.runMap);
309
- copy.executionOrder = this.executionOrder;
310
- return copy;
311
- }
312
- return this;
313
- }
314
340
  }
315
341
  exports.LangChainTracer = LangChainTracer;