-
Notifications
You must be signed in to change notification settings - Fork 2k
/
passthrough.ts
70 lines (60 loc) · 1.79 KB
/
passthrough.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
70
import { Runnable, RunnableLike, RunnableMap } from "./base.js";
import type { RunnableConfig } from "./config.js";
/**
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
*/
export class RunnableAssign<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunInput extends Record<string, any> = any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunOutput extends Record<string, any> = any,
CallOptions extends RunnableConfig = RunnableConfig
> extends Runnable<RunInput, RunOutput> {
lc_namespace = ["langchain", "schema", "runnable"];
mapper: RunnableMap<RunInput>;
constructor(mapper: RunnableMap<RunInput>) {
super();
this.mapper = mapper;
}
async invoke(
input: RunInput,
options?: Partial<CallOptions>
): Promise<RunOutput> {
const mapperResult = await this.mapper.invoke(input, options);
return {
...input,
...mapperResult,
} as RunOutput;
}
}
/**
* A runnable that passes through the input.
*/
export class RunnablePassthrough<RunInput> extends Runnable<
RunInput,
RunInput
> {
static lc_name() {
return "RunnablePassthrough";
}
lc_namespace = ["langchain", "schema", "runnable"];
lc_serializable = true;
async invoke(
input: RunInput,
options?: Partial<RunnableConfig>
): Promise<RunInput> {
return this._callWithConfig(
(input: RunInput) => Promise.resolve(input),
input,
options
);
}
static assign(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
mapping: Record<string, RunnableLike<Record<string, unknown>, any>>
): RunnableAssign<Record<string, unknown>, Record<string, unknown>> {
return new RunnableAssign(
new RunnableMap<Record<string, unknown>>({ steps: mapping })
);
}
}