forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot_listener.ts
69 lines (57 loc) · 1.63 KB
/
root_listener.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { RunnableConfig } from "../runnables/config.js";
import { BaseTracer, Run } from "./base.js";
export class RootListenersTracer extends BaseTracer {
name = "RootListenersTracer";
/** The Run's ID. Type UUID */
rootId?: string;
config: RunnableConfig;
argOnStart?: (run: Run, config: RunnableConfig) => void | Promise<void>;
argOnEnd?: (run: Run, config: RunnableConfig) => void | Promise<void>;
argOnError?: (run: Run, config: RunnableConfig) => void | Promise<void>;
constructor({
config,
onStart,
onEnd,
onError,
}: {
config: RunnableConfig;
onStart?: (run: Run, config: RunnableConfig) => void | Promise<void>;
onEnd?: (run: Run, config: RunnableConfig) => void | Promise<void>;
onError?: (run: Run, config: RunnableConfig) => void | Promise<void>;
}) {
super({ _awaitHandler: true });
this.config = config;
this.argOnStart = onStart;
this.argOnEnd = onEnd;
this.argOnError = onError;
}
/**
* This is a legacy method only called once for an entire run tree
* therefore not useful here
* @param {Run} _ Not used
*/
persistRun(_: Run): Promise<void> {
return Promise.resolve();
}
async onRunCreate(run: Run) {
if (this.rootId) {
return;
}
this.rootId = run.id;
if (this.argOnStart) {
await this.argOnStart(run, this.config);
}
}
async onRunUpdate(run: Run) {
if (run.id !== this.rootId) {
return;
}
if (!run.error) {
if (this.argOnEnd) {
await this.argOnEnd(run, this.config);
}
} else if (this.argOnError) {
await this.argOnError(run, this.config);
}
}
}