@restatedev/agents 0.0.0 → 0.2.0
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.
- package/LICENSE +21 -0
- package/README.md +672 -2
- package/dist/calls.d.ts +29 -0
- package/dist/calls.d.ts.map +1 -0
- package/dist/calls.js +91 -0
- package/dist/calls.js.map +1 -0
- package/dist/claude/conversation.d.ts +33 -0
- package/dist/claude/conversation.d.ts.map +1 -0
- package/dist/claude/conversation.js +95 -0
- package/dist/claude/conversation.js.map +1 -0
- package/dist/claude/index.d.ts +77 -0
- package/dist/claude/index.d.ts.map +1 -0
- package/dist/claude/index.js +269 -0
- package/dist/claude/index.js.map +1 -0
- package/dist/client.d.ts +81 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +88 -0
- package/dist/client.js.map +1 -0
- package/dist/common.d.ts +261 -0
- package/dist/common.d.ts.map +1 -0
- package/dist/common.js +438 -0
- package/dist/common.js.map +1 -0
- package/dist/deepseek/index.d.ts +68 -0
- package/dist/deepseek/index.d.ts.map +1 -0
- package/dist/deepseek/index.js +363 -0
- package/dist/deepseek/index.js.map +1 -0
- package/dist/deepseek/persistence.d.ts +61 -0
- package/dist/deepseek/persistence.d.ts.map +1 -0
- package/dist/deepseek/persistence.js +224 -0
- package/dist/deepseek/persistence.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/langgraph/checkpointer.d.ts +29 -0
- package/dist/langgraph/checkpointer.d.ts.map +1 -0
- package/dist/langgraph/checkpointer.js +275 -0
- package/dist/langgraph/checkpointer.js.map +1 -0
- package/dist/langgraph/index.d.ts +66 -0
- package/dist/langgraph/index.d.ts.map +1 -0
- package/dist/langgraph/index.js +265 -0
- package/dist/langgraph/index.js.map +1 -0
- package/dist/log.d.ts +29 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +145 -0
- package/dist/log.js.map +1 -0
- package/dist/object.d.ts +169 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +221 -0
- package/dist/object.js.map +1 -0
- package/dist/opencode/db.d.ts +40 -0
- package/dist/opencode/db.d.ts.map +1 -0
- package/dist/opencode/db.js +161 -0
- package/dist/opencode/db.js.map +1 -0
- package/dist/opencode/index.d.ts +100 -0
- package/dist/opencode/index.d.ts.map +1 -0
- package/dist/opencode/index.js +436 -0
- package/dist/opencode/index.js.map +1 -0
- package/dist/output.d.ts +17 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +81 -0
- package/dist/output.js.map +1 -0
- package/dist/pi/index.d.ts +89 -0
- package/dist/pi/index.d.ts.map +1 -0
- package/dist/pi/index.js +293 -0
- package/dist/pi/index.js.map +1 -0
- package/dist/pi/storage.d.ts +5 -0
- package/dist/pi/storage.d.ts.map +1 -0
- package/dist/pi/storage.js +41 -0
- package/dist/pi/storage.js.map +1 -0
- package/dist/signals.d.ts +154 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +256 -0
- package/dist/signals.js.map +1 -0
- package/dist/tools.d.ts +158 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +211 -0
- package/dist/tools.js.map +1 -0
- package/dist/webhook.d.ts +71 -0
- package/dist/webhook.d.ts.map +1 -0
- package/dist/webhook.js +102 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +218 -5
- package/src/calls.ts +138 -0
- package/src/claude/README.md +84 -0
- package/src/claude/conversation.ts +114 -0
- package/src/claude/index.ts +364 -0
- package/src/client.ts +176 -0
- package/src/common.ts +550 -0
- package/src/deepseek/README.md +110 -0
- package/src/deepseek/index.ts +464 -0
- package/src/deepseek/persistence.ts +293 -0
- package/src/index.ts +24 -0
- package/src/langgraph/README.md +87 -0
- package/src/langgraph/checkpointer.ts +364 -0
- package/src/langgraph/index.ts +349 -0
- package/src/log.ts +193 -0
- package/src/object.ts +319 -0
- package/src/opencode/README.md +125 -0
- package/src/opencode/db.ts +223 -0
- package/src/opencode/index.ts +559 -0
- package/src/output.ts +95 -0
- package/src/pi/README.md +105 -0
- package/src/pi/index.ts +382 -0
- package/src/pi/storage.ts +65 -0
- package/src/signals.ts +386 -0
- package/src/tools.ts +343 -0
- package/src/webhook.ts +161 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
// A LangGraph checkpointer on an SQL database: `SqlCheckpointSaver`, over a
|
|
2
|
+
// database session `db` (in Restate, `ctx.db`), which has `query`, `execute`
|
|
3
|
+
// and `transaction(body)`.
|
|
4
|
+
//
|
|
5
|
+
// Three tables, with values as LangGraph's serializer writes them:
|
|
6
|
+
// - `restate_langgraph_checkpoints`: each checkpoint, without its channel values.
|
|
7
|
+
// - `restate_langgraph_channel_values`: each channel's value at each version, stored when a
|
|
8
|
+
// step changes it. A checkpoint's values are those at its channel versions,
|
|
9
|
+
// so a step stores only what it changed, not the whole state again (an
|
|
10
|
+
// agent's message list, say). Versions are unique, as in LangGraph's own
|
|
11
|
+
// savers (a count and a random part): a thread forked from an earlier
|
|
12
|
+
// checkpoint takes the same counts again, and its values must not replace
|
|
13
|
+
// those of the branch it forked from.
|
|
14
|
+
// - `restate_langgraph_writes`: the pending writes of each checkpoint's tasks.
|
|
15
|
+
//
|
|
16
|
+
// Each `put` and `putWrites` is one SQL transaction, so a step is stored whole,
|
|
17
|
+
// in order with everything else the handler does. Operations run one at a
|
|
18
|
+
// time, in call order: LangGraph writes the results of parallel tasks at once.
|
|
19
|
+
//
|
|
20
|
+
// By default a thread keeps every checkpoint (LangGraph's history: `list`,
|
|
21
|
+
// time travel, forks). With `history: 'latest'`, each `put` drops the
|
|
22
|
+
// thread's earlier checkpoints, their writes and the values only they had:
|
|
23
|
+
// a thread then takes the space of its state once, not once per step (a
|
|
24
|
+
// message list stored again at each step grows with the square of its
|
|
25
|
+
// length).
|
|
26
|
+
import { randomUUID } from 'node:crypto';
|
|
27
|
+
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
28
|
+
import {
|
|
29
|
+
BaseCheckpointSaver,
|
|
30
|
+
copyCheckpoint,
|
|
31
|
+
maxChannelVersion,
|
|
32
|
+
TASKS,
|
|
33
|
+
WRITES_IDX_MAP,
|
|
34
|
+
type ChannelVersions,
|
|
35
|
+
type Checkpoint,
|
|
36
|
+
type CheckpointListOptions,
|
|
37
|
+
type CheckpointMetadata,
|
|
38
|
+
type CheckpointPendingWrite,
|
|
39
|
+
type CheckpointTuple,
|
|
40
|
+
type PendingWrite,
|
|
41
|
+
type SerializerProtocol,
|
|
42
|
+
} from '@langchain/langgraph-checkpoint';
|
|
43
|
+
import type { DatabaseWriter, Database, SqlValue } from '@restatedev/sessions';
|
|
44
|
+
import { Schema } from '@restatedev/sessions/internal';
|
|
45
|
+
|
|
46
|
+
const SCHEMA = new Schema('restate-agents.langgraph', [
|
|
47
|
+
`CREATE TABLE restate_langgraph_checkpoints (
|
|
48
|
+
thread_id TEXT NOT NULL,
|
|
49
|
+
checkpoint_ns TEXT NOT NULL,
|
|
50
|
+
checkpoint_id TEXT NOT NULL,
|
|
51
|
+
parent_checkpoint_id TEXT,
|
|
52
|
+
type TEXT NOT NULL,
|
|
53
|
+
checkpoint BLOB NOT NULL,
|
|
54
|
+
metadata BLOB NOT NULL,
|
|
55
|
+
PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id)
|
|
56
|
+
)`,
|
|
57
|
+
`CREATE TABLE restate_langgraph_channel_values (
|
|
58
|
+
thread_id TEXT NOT NULL,
|
|
59
|
+
checkpoint_ns TEXT NOT NULL,
|
|
60
|
+
channel TEXT NOT NULL,
|
|
61
|
+
version TEXT NOT NULL,
|
|
62
|
+
type TEXT NOT NULL,
|
|
63
|
+
value BLOB,
|
|
64
|
+
PRIMARY KEY (thread_id, checkpoint_ns, channel, version)
|
|
65
|
+
)`,
|
|
66
|
+
`CREATE TABLE restate_langgraph_writes (
|
|
67
|
+
thread_id TEXT NOT NULL,
|
|
68
|
+
checkpoint_ns TEXT NOT NULL,
|
|
69
|
+
checkpoint_id TEXT NOT NULL,
|
|
70
|
+
task_id TEXT NOT NULL,
|
|
71
|
+
idx INTEGER NOT NULL,
|
|
72
|
+
channel TEXT NOT NULL,
|
|
73
|
+
type TEXT NOT NULL,
|
|
74
|
+
value BLOB NOT NULL,
|
|
75
|
+
PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id, task_id, idx)
|
|
76
|
+
)`,
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
const COLUMNS = 'thread_id, checkpoint_ns, checkpoint_id, parent_checkpoint_id, type, checkpoint, metadata';
|
|
80
|
+
|
|
81
|
+
/** A row of `checkpoints`, in the order of COLUMNS. */
|
|
82
|
+
type CheckpointRow = [
|
|
83
|
+
threadId: string,
|
|
84
|
+
ns: string,
|
|
85
|
+
checkpointId: string,
|
|
86
|
+
parentId: string | null,
|
|
87
|
+
type: string,
|
|
88
|
+
checkpoint: Uint8Array,
|
|
89
|
+
metadata: Uint8Array,
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
// The type of a channel a step emptied.
|
|
93
|
+
const EMPTY = 'empty';
|
|
94
|
+
|
|
95
|
+
/** Whether `actual` has every field of `expected`, recursively (a metadata filter). */
|
|
96
|
+
function matches(actual: unknown, expected: unknown): boolean {
|
|
97
|
+
if (expected === null || typeof expected !== 'object') return actual === expected;
|
|
98
|
+
if (actual === null || typeof actual !== 'object') return false;
|
|
99
|
+
if (Array.isArray(expected) !== Array.isArray(actual)) return false;
|
|
100
|
+
if (Array.isArray(expected) && expected.length !== (actual as unknown[]).length) return false;
|
|
101
|
+
return Object.entries(expected).every(([key, value]) => matches((actual as Record<string, unknown>)[key], value));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Drop what the thread `threadId` (namespace `ns`) keeps from before
|
|
106
|
+
* `checkpoint`: its other checkpoints, their pending writes, and the channel
|
|
107
|
+
* values at versions other than `checkpoint`'s.
|
|
108
|
+
*/
|
|
109
|
+
async function dropEarlier(tx: DatabaseWriter, threadId: string, ns: string, checkpoint: Checkpoint) {
|
|
110
|
+
const thread = [threadId, ns];
|
|
111
|
+
for (const table of ['restate_langgraph_checkpoints', 'restate_langgraph_writes']) {
|
|
112
|
+
await tx.execute(`DELETE FROM ${table} WHERE thread_id = ? AND checkpoint_ns = ? AND checkpoint_id <> ?`, [
|
|
113
|
+
...thread,
|
|
114
|
+
checkpoint.id,
|
|
115
|
+
]);
|
|
116
|
+
}
|
|
117
|
+
const versions = Object.entries(checkpoint.channel_versions ?? {});
|
|
118
|
+
const kept = versions.map(() => '(channel = ? AND version = ?)').join(' OR ') || '0';
|
|
119
|
+
await tx.execute(
|
|
120
|
+
`DELETE FROM restate_langgraph_channel_values WHERE thread_id = ? AND checkpoint_ns = ? AND NOT (${kept})`,
|
|
121
|
+
[...thread, ...versions.flatMap(([channel, version]) => [channel, String(version)])],
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** A LangGraph checkpointer whose threads are in the database session `db`: see the top of this file. */
|
|
126
|
+
export class SqlCheckpointSaver extends BaseCheckpointSaver {
|
|
127
|
+
#db: Database;
|
|
128
|
+
#latest: boolean;
|
|
129
|
+
#ready: Promise<void> | undefined;
|
|
130
|
+
#tail: Promise<unknown> = Promise.resolve();
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* `db`: a database session (`ctx.db`). `history`: the checkpoints a thread
|
|
134
|
+
* keeps, `all` (the default) or the `latest` only; `serde`: LangGraph's
|
|
135
|
+
* serializer, by default its own.
|
|
136
|
+
*/
|
|
137
|
+
constructor(
|
|
138
|
+
db: Database,
|
|
139
|
+
{ history = 'all', serde }: { history?: 'all' | 'latest'; serde?: SerializerProtocol } = {},
|
|
140
|
+
) {
|
|
141
|
+
super(serde);
|
|
142
|
+
this.#db = db;
|
|
143
|
+
this.#latest = history === 'latest';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The version after `current`: its count plus one, zero-padded so that
|
|
148
|
+
* versions compare as their counts do, and a random part, so that branches
|
|
149
|
+
* of a thread never share one. A string: LangGraph compares string versions
|
|
150
|
+
* as strings, though `compile` types a saver's versions as numbers.
|
|
151
|
+
*/
|
|
152
|
+
override getNextVersion(current: number | undefined): number {
|
|
153
|
+
const count = current === undefined ? 0 : Number.parseInt(String(current).split('.')[0]!, 10);
|
|
154
|
+
return `${String(count + 1).padStart(32, '0')}.${randomUUID()}` as unknown as number;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Runs `operation` after the ones before it, on a database with the tables. */
|
|
158
|
+
#serial<T>(operation: () => Promise<T>): Promise<T> {
|
|
159
|
+
this.#ready ??= SCHEMA.ensure(this.#db);
|
|
160
|
+
const result = this.#tail.then(() => this.#ready).then(operation);
|
|
161
|
+
this.#tail = result.catch(() => {});
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The values of `checkpoint`'s channels, at its versions. */
|
|
166
|
+
async #channelValues(threadId: string, ns: string, checkpoint: Checkpoint): Promise<Record<string, unknown>> {
|
|
167
|
+
const versions = Object.entries(checkpoint.channel_versions ?? {});
|
|
168
|
+
const values: Record<string, unknown> = {};
|
|
169
|
+
if (versions.length === 0) return values;
|
|
170
|
+
const rows = await this.#db.values(
|
|
171
|
+
`SELECT channel, type, value FROM restate_langgraph_channel_values WHERE thread_id = ? AND checkpoint_ns = ? AND (${versions
|
|
172
|
+
.map(() => '(channel = ? AND version = ?)')
|
|
173
|
+
.join(' OR ')})`,
|
|
174
|
+
[threadId, ns, ...versions.flatMap(([channel, version]) => [channel, String(version)])],
|
|
175
|
+
);
|
|
176
|
+
for (const [channel, type, value] of rows as [string, string, Uint8Array][]) {
|
|
177
|
+
if (type !== EMPTY) values[channel] = await this.serde.loadsTyped(type, value);
|
|
178
|
+
}
|
|
179
|
+
return values;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async #tuple([
|
|
183
|
+
threadId,
|
|
184
|
+
ns,
|
|
185
|
+
checkpointId,
|
|
186
|
+
parentId,
|
|
187
|
+
type,
|
|
188
|
+
stored,
|
|
189
|
+
metadata,
|
|
190
|
+
]: CheckpointRow): Promise<CheckpointTuple> {
|
|
191
|
+
const checkpoint: Checkpoint = await this.serde.loadsTyped(type, stored);
|
|
192
|
+
checkpoint.channel_values = await this.#channelValues(threadId, ns, checkpoint);
|
|
193
|
+
// A checkpoint of an earlier format (v < 4) keeps its pending sends as
|
|
194
|
+
// writes of its parent.
|
|
195
|
+
if (checkpoint.v < 4 && parentId !== null) await this.#migratePendingSends(checkpoint, threadId, ns, parentId);
|
|
196
|
+
const writes = await this.#db.values(
|
|
197
|
+
'SELECT task_id, channel, type, value FROM restate_langgraph_writes WHERE thread_id = ? AND checkpoint_ns = ? AND checkpoint_id = ? ORDER BY rowid',
|
|
198
|
+
[threadId, ns, checkpointId],
|
|
199
|
+
);
|
|
200
|
+
const config = (id: string) => ({ configurable: { thread_id: threadId, checkpoint_ns: ns, checkpoint_id: id } });
|
|
201
|
+
return {
|
|
202
|
+
config: config(checkpointId),
|
|
203
|
+
checkpoint,
|
|
204
|
+
metadata: await this.serde.loadsTyped(type, metadata),
|
|
205
|
+
parentConfig: parentId === null ? undefined : config(parentId),
|
|
206
|
+
pendingWrites: await Promise.all(
|
|
207
|
+
(writes as [string, string, string, Uint8Array][]).map(
|
|
208
|
+
async ([taskId, channel, writeType, value]): Promise<CheckpointPendingWrite> => [
|
|
209
|
+
taskId,
|
|
210
|
+
channel,
|
|
211
|
+
await this.serde.loadsTyped(writeType, value),
|
|
212
|
+
],
|
|
213
|
+
),
|
|
214
|
+
),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async #migratePendingSends(checkpoint: Checkpoint, threadId: string, ns: string, parentId: string) {
|
|
219
|
+
const sends = await this.#db.values(
|
|
220
|
+
'SELECT type, value FROM restate_langgraph_writes WHERE thread_id = ? AND checkpoint_ns = ? AND checkpoint_id = ? AND channel = ? ORDER BY rowid',
|
|
221
|
+
[threadId, ns, parentId, TASKS],
|
|
222
|
+
);
|
|
223
|
+
checkpoint.channel_values[TASKS] = await Promise.all(
|
|
224
|
+
(sends as [string, Uint8Array][]).map(([type, value]) => this.serde.loadsTyped(type, value)),
|
|
225
|
+
);
|
|
226
|
+
const versions = Object.values(checkpoint.channel_versions);
|
|
227
|
+
checkpoint.channel_versions[TASKS] =
|
|
228
|
+
versions.length > 0 ? maxChannelVersion(...versions) : this.getNextVersion(undefined);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined> {
|
|
232
|
+
return this.#serial(async () => {
|
|
233
|
+
const { thread_id, checkpoint_ns = '', checkpoint_id } = config.configurable ?? {};
|
|
234
|
+
const rows = checkpoint_id
|
|
235
|
+
? await this.#db.values(
|
|
236
|
+
`SELECT ${COLUMNS} FROM restate_langgraph_checkpoints WHERE thread_id = ? AND checkpoint_ns = ? AND checkpoint_id = ?`,
|
|
237
|
+
[thread_id, checkpoint_ns, checkpoint_id],
|
|
238
|
+
)
|
|
239
|
+
: await this.#db.values(
|
|
240
|
+
`SELECT ${COLUMNS} FROM restate_langgraph_checkpoints WHERE thread_id = ? AND checkpoint_ns = ? ORDER BY checkpoint_id DESC LIMIT 1`,
|
|
241
|
+
[thread_id, checkpoint_ns],
|
|
242
|
+
);
|
|
243
|
+
return rows.length === 0 ? undefined : this.#tuple(rows[0] as CheckpointRow);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async *list(
|
|
248
|
+
config: RunnableConfig,
|
|
249
|
+
{ limit, before, filter }: CheckpointListOptions = {},
|
|
250
|
+
): AsyncGenerator<CheckpointTuple> {
|
|
251
|
+
const tuples = await this.#serial(async () => {
|
|
252
|
+
const { thread_id, checkpoint_ns } = config.configurable ?? {};
|
|
253
|
+
const where: string[] = [];
|
|
254
|
+
const params: SqlValue[] = [];
|
|
255
|
+
const filters: [string, SqlValue | undefined][] = [
|
|
256
|
+
['thread_id = ?', thread_id],
|
|
257
|
+
['checkpoint_ns = ?', checkpoint_ns ?? undefined],
|
|
258
|
+
['checkpoint_id < ?', before?.configurable?.checkpoint_id],
|
|
259
|
+
];
|
|
260
|
+
for (const [condition, value] of filters) {
|
|
261
|
+
if (value === undefined) continue;
|
|
262
|
+
where.push(condition);
|
|
263
|
+
params.push(value);
|
|
264
|
+
}
|
|
265
|
+
const rows = await this.#db.values(
|
|
266
|
+
`SELECT ${COLUMNS} FROM restate_langgraph_checkpoints ${where.length ? `WHERE ${where.join(' AND ')}` : ''} ORDER BY checkpoint_id DESC`,
|
|
267
|
+
params,
|
|
268
|
+
);
|
|
269
|
+
const wanted = Object.fromEntries(Object.entries(filter ?? {}).filter(([, value]) => value !== undefined));
|
|
270
|
+
const found: CheckpointTuple[] = [];
|
|
271
|
+
for (const row of rows) {
|
|
272
|
+
if (limit !== undefined && found.length >= limit) break;
|
|
273
|
+
const tuple = await this.#tuple(row as CheckpointRow);
|
|
274
|
+
if (matches(tuple.metadata, wanted)) found.push(tuple);
|
|
275
|
+
}
|
|
276
|
+
return found;
|
|
277
|
+
});
|
|
278
|
+
yield* tuples;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
put(
|
|
282
|
+
config: RunnableConfig,
|
|
283
|
+
checkpoint: Checkpoint,
|
|
284
|
+
metadata: CheckpointMetadata,
|
|
285
|
+
newVersions: ChannelVersions,
|
|
286
|
+
): Promise<RunnableConfig> {
|
|
287
|
+
return this.#serial(async () => {
|
|
288
|
+
const { thread_id, checkpoint_ns = '', checkpoint_id: parentId } = config.configurable ?? {};
|
|
289
|
+
if (!thread_id) throw new Error('Missing "thread_id" field in passed "config.configurable".');
|
|
290
|
+
const { channel_values: values, ...rest } = copyCheckpoint(checkpoint);
|
|
291
|
+
const [type, stored] = await this.serde.dumpsTyped(rest);
|
|
292
|
+
const [metadataType, metadataValue] = await this.serde.dumpsTyped(metadata);
|
|
293
|
+
if (type !== metadataType) throw new Error('Failed to serialize checkpoint and metadata to the same type.');
|
|
294
|
+
// The channels this step changed, at their new versions.
|
|
295
|
+
const changed = await Promise.all(
|
|
296
|
+
Object.entries(newVersions ?? {}).map(async ([channel, version]): Promise<SqlValue[]> => {
|
|
297
|
+
const [valueType, value] = channel in values ? await this.serde.dumpsTyped(values[channel]) : [EMPTY, null];
|
|
298
|
+
return [channel, String(version), valueType, value];
|
|
299
|
+
}),
|
|
300
|
+
);
|
|
301
|
+
await this.#db.transaction(async (tx) => {
|
|
302
|
+
for (const row of changed) {
|
|
303
|
+
await tx.execute(
|
|
304
|
+
'INSERT OR REPLACE INTO restate_langgraph_channel_values (thread_id, checkpoint_ns, channel, version, type, value) VALUES (?, ?, ?, ?, ?, ?)',
|
|
305
|
+
[thread_id, checkpoint_ns, ...row],
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
await tx.execute(
|
|
309
|
+
`INSERT OR REPLACE INTO restate_langgraph_checkpoints (${COLUMNS}) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
310
|
+
[thread_id, checkpoint_ns, checkpoint.id, parentId ?? null, type, stored, metadataValue],
|
|
311
|
+
);
|
|
312
|
+
if (this.#latest) await dropEarlier(tx, thread_id, checkpoint_ns, checkpoint);
|
|
313
|
+
});
|
|
314
|
+
return { configurable: { thread_id, checkpoint_ns, checkpoint_id: checkpoint.id } };
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
putWrites(config: RunnableConfig, writes: PendingWrite[], taskId: string): Promise<void> {
|
|
319
|
+
return this.#serial(async () => {
|
|
320
|
+
const { thread_id, checkpoint_ns = '', checkpoint_id } = config.configurable ?? {};
|
|
321
|
+
if (!thread_id) throw new Error('Missing thread_id field in config.configurable.');
|
|
322
|
+
if (!checkpoint_id) throw new Error('Missing checkpoint_id field in config.configurable.');
|
|
323
|
+
// Special writes (errors, interrupts) replace; others keep the first.
|
|
324
|
+
const verb = writes.every(([channel]) => channel in WRITES_IDX_MAP) ? 'INSERT OR REPLACE' : 'INSERT OR IGNORE';
|
|
325
|
+
const rows = await Promise.all(
|
|
326
|
+
writes.map(async ([channel, value], idx): Promise<SqlValue[]> => {
|
|
327
|
+
const [type, stored] = await this.serde.dumpsTyped(value);
|
|
328
|
+
return [
|
|
329
|
+
thread_id,
|
|
330
|
+
checkpoint_ns,
|
|
331
|
+
checkpoint_id,
|
|
332
|
+
taskId,
|
|
333
|
+
WRITES_IDX_MAP[channel] ?? idx,
|
|
334
|
+
channel,
|
|
335
|
+
type,
|
|
336
|
+
stored,
|
|
337
|
+
];
|
|
338
|
+
}),
|
|
339
|
+
);
|
|
340
|
+
await this.#db.transaction(async (tx) => {
|
|
341
|
+
for (const row of rows) {
|
|
342
|
+
await tx.execute(
|
|
343
|
+
`${verb} INTO restate_langgraph_writes (thread_id, checkpoint_ns, checkpoint_id, task_id, idx, channel, type, value) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
344
|
+
row,
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
deleteThread(threadId: string): Promise<void> {
|
|
352
|
+
return this.#serial(() =>
|
|
353
|
+
this.#db.transaction(async (tx) => {
|
|
354
|
+
for (const table of [
|
|
355
|
+
'restate_langgraph_checkpoints',
|
|
356
|
+
'restate_langgraph_channel_values',
|
|
357
|
+
'restate_langgraph_writes',
|
|
358
|
+
]) {
|
|
359
|
+
await tx.execute(`DELETE FROM ${table} WHERE thread_id = ?`, [threadId]);
|
|
360
|
+
}
|
|
361
|
+
}),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
}
|