-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathbase.ts
More file actions
152 lines (134 loc) · 3.71 KB
/
base.ts
File metadata and controls
152 lines (134 loc) · 3.71 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { RunnableConfig } from "@langchain/core/runnables";
import { DefaultSerializer, SerializerProtocol } from "../serde/base.js";
import { uuid6 } from "./id.js";
export interface CheckpointMetadata {
source: "input" | "loop" | "update";
/**
* The source of the checkpoint.
* - "input": The checkpoint was created from an input to invoke/stream/batch.
* - "loop": The checkpoint was created from inside the pregel loop.
* - "update": The checkpoint was created from a manual state update. */
step: number;
/**
* The step number of the checkpoint.
* -1 for the first "input" checkpoint.
* 0 for the first "loop" checkpoint.
* ... for the nth checkpoint afterwards. */
writes: Record<string, unknown> | null;
/**
* The writes that were made between the previous checkpoint and this one.
* Mapping from node name to writes emitted by that node.
*/
}
export interface Checkpoint<
N extends string = string,
C extends string = string
> {
/**
* Version number
*/
v: number;
/**
* Checkpoint ID {uuid6}
*/
id: string;
/**
* Timestamp {new Date().toISOString()}
*/
ts: string;
/**
* @default {}
*/
channel_values: Record<C, unknown>;
/**
* @default {}
*/
channel_versions: Record<C, number>;
/**
* @default {}
*/
versions_seen: Record<N, Record<C, number>>;
}
export interface ReadonlyCheckpoint extends Readonly<Checkpoint> {
readonly channel_values: Readonly<Record<string, unknown>>;
readonly channel_versions: Readonly<Record<string, number>>;
readonly versions_seen: Readonly<
Record<string, Readonly<Record<string, number>>>
>;
}
export function getChannelVersion(
checkpoint: ReadonlyCheckpoint,
channel: string
): number {
return checkpoint.channel_versions[channel] ?? 0;
}
export function getVersionSeen(
checkpoint: ReadonlyCheckpoint,
node: string,
channel: string
): number {
return checkpoint.versions_seen[node]?.[channel] ?? 0;
}
export function deepCopy<T>(obj: T): T {
if (typeof obj !== "object" || obj === null) {
return obj;
}
const newObj = Array.isArray(obj) ? [] : {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
(newObj as Record<PropertyKey, unknown>)[key] = deepCopy(
(obj as Record<string, unknown>)[key]
);
}
}
return newObj as T;
}
export function emptyCheckpoint(): Checkpoint {
return {
v: 1,
id: uuid6(-2),
ts: new Date().toISOString(),
channel_values: {},
channel_versions: {},
versions_seen: {},
};
}
export function copyCheckpoint(checkpoint: ReadonlyCheckpoint): Checkpoint {
return {
v: checkpoint.v,
id: checkpoint.id,
ts: checkpoint.ts,
channel_values: { ...checkpoint.channel_values },
channel_versions: { ...checkpoint.channel_versions },
versions_seen: deepCopy(checkpoint.versions_seen),
};
}
export interface CheckpointTuple {
config: RunnableConfig;
checkpoint: Checkpoint;
metadata?: CheckpointMetadata;
parentConfig?: RunnableConfig;
}
export abstract class BaseCheckpointSaver {
serde: SerializerProtocol<unknown> = DefaultSerializer;
constructor(serde?: SerializerProtocol<unknown>) {
this.serde = serde || this.serde;
}
async get(config: RunnableConfig): Promise<Checkpoint | undefined> {
const value = await this.getTuple(config);
return value ? value.checkpoint : undefined;
}
abstract getTuple(
config: RunnableConfig
): Promise<CheckpointTuple | undefined>;
abstract list(
config: RunnableConfig,
limit?: number,
before?: RunnableConfig
): AsyncGenerator<CheckpointTuple>;
abstract put(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata
): Promise<RunnableConfig>;
}