@restatedev/agents 0.3.0 → 0.4.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/README.md +89 -49
- package/dist/calls.d.ts +29 -5
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +77 -10
- package/dist/calls.js.map +1 -1
- package/dist/claude/index.d.ts +36 -7
- package/dist/claude/index.d.ts.map +1 -1
- package/dist/claude/index.js +232 -151
- package/dist/claude/index.js.map +1 -1
- package/dist/client.d.ts +12 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/common.d.ts +151 -70
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +246 -204
- package/dist/common.js.map +1 -1
- package/dist/conversation.d.ts +466 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +1247 -0
- package/dist/conversation.js.map +1 -0
- package/dist/deepseek/index.d.ts +38 -5
- package/dist/deepseek/index.d.ts.map +1 -1
- package/dist/deepseek/index.js +322 -106
- package/dist/deepseek/index.js.map +1 -1
- package/dist/deepseek/persistence.d.ts +9 -3
- package/dist/deepseek/persistence.d.ts.map +1 -1
- package/dist/deepseek/persistence.js +12 -6
- package/dist/deepseek/persistence.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/langgraph/checkpointer.d.ts +16 -2
- package/dist/langgraph/checkpointer.d.ts.map +1 -1
- package/dist/langgraph/checkpointer.js +133 -26
- package/dist/langgraph/checkpointer.js.map +1 -1
- package/dist/langgraph/context.d.ts +10 -0
- package/dist/langgraph/context.d.ts.map +1 -0
- package/dist/langgraph/context.js +21 -0
- package/dist/langgraph/context.js.map +1 -0
- package/dist/langgraph/errors.d.ts +16 -0
- package/dist/langgraph/errors.d.ts.map +1 -0
- package/dist/langgraph/errors.js +30 -0
- package/dist/langgraph/errors.js.map +1 -0
- package/dist/langgraph/graph.d.ts +25 -0
- package/dist/langgraph/graph.d.ts.map +1 -0
- package/dist/langgraph/graph.js +346 -0
- package/dist/langgraph/graph.js.map +1 -0
- package/dist/langgraph/index.d.ts +65 -12
- package/dist/langgraph/index.d.ts.map +1 -1
- package/dist/langgraph/index.js +227 -171
- package/dist/langgraph/index.js.map +1 -1
- package/dist/langgraph/nodes.d.ts +70 -0
- package/dist/langgraph/nodes.d.ts.map +1 -0
- package/dist/langgraph/nodes.js +145 -0
- package/dist/langgraph/nodes.js.map +1 -0
- package/dist/messages.d.ts +18 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +182 -0
- package/dist/messages.js.map +1 -0
- package/dist/object.d.ts +65 -23
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +121 -35
- package/dist/object.js.map +1 -1
- package/dist/output.d.ts +15 -7
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +44 -26
- package/dist/output.js.map +1 -1
- package/dist/pi/index.d.ts +39 -11
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +281 -77
- package/dist/pi/index.js.map +1 -1
- package/dist/pi/storage.d.ts +29 -4
- package/dist/pi/storage.d.ts.map +1 -1
- package/dist/pi/storage.js +18 -6
- package/dist/pi/storage.js.map +1 -1
- package/dist/signals.d.ts +74 -4
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +144 -15
- package/dist/signals.js.map +1 -1
- package/dist/steering.d.ts +31 -0
- package/dist/steering.d.ts.map +1 -0
- package/dist/steering.js +45 -0
- package/dist/steering.js.map +1 -0
- package/dist/tools.d.ts +28 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +34 -5
- package/dist/tools.js.map +1 -1
- package/package.json +33 -37
- package/src/calls.ts +107 -15
- package/src/claude/README.md +41 -20
- package/src/claude/index.ts +266 -178
- package/src/client.ts +14 -3
- package/src/common.ts +317 -232
- package/src/conversation.ts +1527 -0
- package/src/deepseek/README.md +78 -11
- package/src/deepseek/index.ts +384 -137
- package/src/deepseek/persistence.ts +11 -8
- package/src/index.ts +13 -4
- package/src/langgraph/README.md +115 -25
- package/src/langgraph/checkpointer.ts +168 -28
- package/src/langgraph/context.ts +27 -0
- package/src/langgraph/errors.ts +26 -0
- package/src/langgraph/graph.ts +396 -0
- package/src/langgraph/index.ts +326 -181
- package/src/langgraph/nodes.ts +243 -0
- package/src/messages.ts +244 -0
- package/src/object.ts +167 -38
- package/src/output.ts +52 -31
- package/src/pi/README.md +18 -3
- package/src/pi/index.ts +346 -78
- package/src/pi/storage.ts +20 -7
- package/src/signals.ts +225 -19
- package/src/steering.ts +67 -0
- package/src/tools.ts +61 -8
- package/dist/claude/conversation.d.ts +0 -33
- package/dist/claude/conversation.d.ts.map +0 -1
- package/dist/claude/conversation.js +0 -95
- package/dist/claude/conversation.js.map +0 -1
- package/dist/log.d.ts +0 -29
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -146
- package/dist/log.js.map +0 -1
- package/dist/opencode/db.d.ts +0 -40
- package/dist/opencode/db.d.ts.map +0 -1
- package/dist/opencode/db.js +0 -161
- package/dist/opencode/db.js.map +0 -1
- package/dist/opencode/index.d.ts +0 -100
- package/dist/opencode/index.d.ts.map +0 -1
- package/dist/opencode/index.js +0 -436
- package/dist/opencode/index.js.map +0 -1
- package/src/claude/conversation.ts +0 -114
- package/src/log.ts +0 -194
- package/src/opencode/README.md +0 -125
- package/src/opencode/db.ts +0 -223
- package/src/opencode/index.ts +0 -559
package/src/signals.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Signals to agents: a virtual object per agent, keyed `<agent>:<key>` (such
|
|
2
2
|
// as `assistant:alice`), whose `wait` returns once `resolve` gives the wait
|
|
3
|
-
// its value; the message the agent answers;
|
|
3
|
+
// its value; the message the agent answers; the messages steered to it that
|
|
4
|
+
// it has not taken yet (`steer`); and the revision of the agent's
|
|
4
5
|
// conversation, which its `watch` waits on.
|
|
5
6
|
//
|
|
6
7
|
// An agent's own handler cannot wait on an awakeable (it has no journal to
|
|
@@ -8,7 +9,7 @@
|
|
|
8
9
|
// this object, which waits without holding a process.
|
|
9
10
|
import type { SessionContext } from '@restatedev/sessions';
|
|
10
11
|
import { Link, needing, sdk as restate } from '@restatedev/sessions/internal';
|
|
11
|
-
import type {
|
|
12
|
+
import type { Answering } from './common.ts';
|
|
12
13
|
import type { Status } from './object.ts';
|
|
13
14
|
|
|
14
15
|
/** An agent's context, as far as its signals need it: any handler's or view's of its object. */
|
|
@@ -130,12 +131,92 @@ export interface Notifications {
|
|
|
130
131
|
revision: number;
|
|
131
132
|
}
|
|
132
133
|
|
|
134
|
+
/** A message steered to the turn being answered (`steer`): its id (the `steer` request's) and its text. */
|
|
135
|
+
export interface Steer {
|
|
136
|
+
id: string;
|
|
137
|
+
text: string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* What `publish` answers the turn that published: the new revision, and the
|
|
142
|
+
* messages steered to the turn being answered that it has not taken yet, if
|
|
143
|
+
* any, oldest first.
|
|
144
|
+
*/
|
|
145
|
+
export interface Published extends Notifications {
|
|
146
|
+
steering?: Steer[];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A message steered to the turn being answered that it has not taken yet, as `status` shows it. */
|
|
150
|
+
export interface Steering {
|
|
151
|
+
id: string;
|
|
152
|
+
/** When it was sent, as an ISO date. */
|
|
153
|
+
since: string;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* What the `steer` view answers: the message's id (the request's), and the
|
|
158
|
+
* invocation of the turn being answered, which takes the message at its next
|
|
159
|
+
* step if it reaches one; null if no turn takes it. Either way the message is
|
|
160
|
+
* answered once: in that turn, or else as a turn of its own, in the order it
|
|
161
|
+
* was sent.
|
|
162
|
+
*/
|
|
163
|
+
export interface Steered {
|
|
164
|
+
id: string;
|
|
165
|
+
turn: string | null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* How many messages a turn takes by `steer`, at most: past them, each is
|
|
170
|
+
* answered as a turn of its own. The companion counts each message it holds
|
|
171
|
+
* for the turn, taken or not, until the turn ends: so a turn that someone
|
|
172
|
+
* steers on and on stays within bounds, and so does what the companion holds
|
|
173
|
+
* for it.
|
|
174
|
+
*/
|
|
175
|
+
export const STEERS = 16;
|
|
176
|
+
|
|
133
177
|
/** What `watch` waits for: a revision newer than `revision`, for at most `timeoutSeconds` (default 50, at most 60). */
|
|
134
178
|
export interface Watch {
|
|
135
179
|
revision: number;
|
|
136
180
|
timeoutSeconds?: number;
|
|
137
181
|
}
|
|
138
182
|
|
|
183
|
+
/**
|
|
184
|
+
* A turn as its agent starts it, and whether the agent closes it itself once
|
|
185
|
+
* its invocation is gone: whether it has the private `settle` handler, which
|
|
186
|
+
* the companion sends such a turn. An agent of another SDK (the companion's
|
|
187
|
+
* handlers are shared) that does not say so is sent nothing: its next
|
|
188
|
+
* message closes such a turn.
|
|
189
|
+
*/
|
|
190
|
+
export interface Started extends Answering {
|
|
191
|
+
settles?: boolean;
|
|
192
|
+
/**
|
|
193
|
+
* Whether the turn takes steered messages at its steps (`steer`). A turn
|
|
194
|
+
* that does not say so (of another SDK, or of an agent whose framework
|
|
195
|
+
* cannot take them) gets none: each is answered as a turn of its own.
|
|
196
|
+
*/
|
|
197
|
+
steers?: boolean;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* A message steered to the turn `turn` (an invocation id), as the companion
|
|
202
|
+
* holds it until the turn ends: with its text until the turn takes it
|
|
203
|
+
* (`taken`), and then only as one of the turn's {@link STEERS}.
|
|
204
|
+
*/
|
|
205
|
+
interface Inboxed {
|
|
206
|
+
id: string;
|
|
207
|
+
since: string;
|
|
208
|
+
turn: string;
|
|
209
|
+
text?: string;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The messages of `inbox` steered to the turn being answered (`answering`) that it has not taken yet, oldest first. */
|
|
213
|
+
const waitingIn = (inbox: readonly Inboxed[], answering: Started | null | undefined) =>
|
|
214
|
+
answering ? inbox.filter((m) => m.turn === answering.invocationId && m.text !== undefined) : [];
|
|
215
|
+
|
|
216
|
+
/** `turn` as `status` shows it: without what only the companion reads. */
|
|
217
|
+
const shown = (turn: Started | null | undefined): Answering | null =>
|
|
218
|
+
turn ? { invocationId: turn.invocationId, since: turn.since } : null;
|
|
219
|
+
|
|
139
220
|
/** An agent: its object's name, and key. */
|
|
140
221
|
interface Agent {
|
|
141
222
|
service: string;
|
|
@@ -149,9 +230,11 @@ interface Agent {
|
|
|
149
230
|
*/
|
|
150
231
|
interface State {
|
|
151
232
|
/** The message the agent answers, from its turn's start to its close. */
|
|
152
|
-
turn:
|
|
233
|
+
turn: Started;
|
|
153
234
|
waiting: (Waiting & { awakeable: string })[];
|
|
154
235
|
kept: Record<string, unknown[]>;
|
|
236
|
+
/** The messages steered to the turns not closed yet (the one being answered), taken or not, oldest first. */
|
|
237
|
+
inbox: Inboxed[];
|
|
155
238
|
revision: number;
|
|
156
239
|
watching: { revision: number; awakeable: string }[];
|
|
157
240
|
}
|
|
@@ -165,6 +248,10 @@ async function publish(ctx: restate.ObjectContext<State>): Promise<Notifications
|
|
|
165
248
|
return { revision };
|
|
166
249
|
}
|
|
167
250
|
|
|
251
|
+
/** Keep `inbox`, or forget it once it is empty. */
|
|
252
|
+
const setInbox = (ctx: restate.ObjectContext<State>, inbox: Inboxed[]) =>
|
|
253
|
+
inbox.length > 0 ? ctx.set('inbox', inbox) : ctx.clear('inbox');
|
|
254
|
+
|
|
168
255
|
/** How long one `awaitTurn` waits for its turn to end, before a new one takes over. */
|
|
169
256
|
const WATCH = { hours: 1 };
|
|
170
257
|
|
|
@@ -175,10 +262,38 @@ const WATCH = { hours: 1 };
|
|
|
175
262
|
* It publishes the change, for the clients that follow the conversation: a
|
|
176
263
|
* turn's own close (its last change), or the close of one that never closed
|
|
177
264
|
* itself. A close again changes nothing, and publishes nothing.
|
|
265
|
+
*
|
|
266
|
+
* If the turn's invocation is `gone` and the turn did not close itself, its
|
|
267
|
+
* agent is sent `settle`, which closes the turn in its conversation too, if
|
|
268
|
+
* it said, as it started the turn, that it `settles`.
|
|
269
|
+
*
|
|
270
|
+
* The messages steered to the turn are forgotten here: each that it did not
|
|
271
|
+
* take was queued as a turn of its own as it was sent (`steer`), and is
|
|
272
|
+
* answered as one.
|
|
178
273
|
*/
|
|
179
|
-
async function closed(ctx: restate.ObjectContext<State>, turn: string) {
|
|
180
|
-
const
|
|
181
|
-
|
|
274
|
+
async function closed(ctx: restate.ObjectContext<State>, turn: string, gone = false) {
|
|
275
|
+
const started = await ctx.get('turn');
|
|
276
|
+
const answering = started?.invocationId === turn;
|
|
277
|
+
if (answering) {
|
|
278
|
+
ctx.clear('turn');
|
|
279
|
+
if (gone && started?.settles) {
|
|
280
|
+
const at = ctx.key.indexOf(':');
|
|
281
|
+
ctx.genericSend({
|
|
282
|
+
service: ctx.key.slice(0, at),
|
|
283
|
+
method: 'settle',
|
|
284
|
+
key: ctx.key.slice(at + 1),
|
|
285
|
+
parameter: turn,
|
|
286
|
+
inputSerde: restate.serde.json,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
const inbox = (await ctx.get('inbox')) ?? [];
|
|
291
|
+
const dropped = inbox.some((m) => m.turn === turn);
|
|
292
|
+
if (dropped)
|
|
293
|
+
setInbox(
|
|
294
|
+
ctx,
|
|
295
|
+
inbox.filter((m) => m.turn !== turn),
|
|
296
|
+
);
|
|
182
297
|
const waiting = (await ctx.get('waiting')) ?? [];
|
|
183
298
|
// A wait's id starts with the invocation id of the turn whose tool waits.
|
|
184
299
|
const ofTurn = (id: string) => id.startsWith(`${turn}:`);
|
|
@@ -186,7 +301,7 @@ async function closed(ctx: restate.ObjectContext<State>, turn: string) {
|
|
|
186
301
|
const left = waiting.filter((w) => !ofTurn(w.id));
|
|
187
302
|
for (const w of over) ctx.rejectAwakeable(w.awakeable, 'its turn is over');
|
|
188
303
|
if (over.length > 0) ctx.set('waiting', left);
|
|
189
|
-
if (answering || over.length > 0) await publish(ctx);
|
|
304
|
+
if (answering || over.length > 0 || dropped) await publish(ctx);
|
|
190
305
|
}
|
|
191
306
|
|
|
192
307
|
/** A handler that the agents' own handlers call, which clients cannot (through Restate's ingress). */
|
|
@@ -261,10 +376,10 @@ export const signals = restate.object({
|
|
|
261
376
|
* `awaitTurn` did not close it yet. `awaitTurn` watches the new turn, so
|
|
262
377
|
* that it closes even if no turn comes after it.
|
|
263
378
|
*/
|
|
264
|
-
start: internal(async (ctx: restate.ObjectContext<State>, turn:
|
|
379
|
+
start: internal(async (ctx: restate.ObjectContext<State>, turn: Started) => {
|
|
265
380
|
const before = (await ctx.get('turn'))?.invocationId;
|
|
266
381
|
if (before === turn.invocationId) return;
|
|
267
|
-
if (before !== undefined) await closed(ctx, before);
|
|
382
|
+
if (before !== undefined) await closed(ctx, before, true);
|
|
268
383
|
ctx.set('turn', turn);
|
|
269
384
|
ctx.objectSendClient(signals, ctx.key).awaitTurn(turn.invocationId);
|
|
270
385
|
}),
|
|
@@ -295,18 +410,26 @@ export const signals = restate.object({
|
|
|
295
410
|
.map((_answer, failure) => ({ failed: failure !== undefined })),
|
|
296
411
|
ctx.sleep(WATCH).map(() => null),
|
|
297
412
|
]);
|
|
298
|
-
if (ended?.failed) ctx.objectSendClient(signals, ctx.key).
|
|
413
|
+
if (ended?.failed) ctx.objectSendClient(signals, ctx.key).ended(turn);
|
|
299
414
|
else if (ended === null && (await ctx.get('turn'))?.invocationId === turn) {
|
|
300
415
|
ctx.objectSendClient(signals, ctx.key).awaitTurn(turn);
|
|
301
416
|
}
|
|
302
417
|
},
|
|
303
418
|
),
|
|
304
419
|
|
|
305
|
-
/**
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
420
|
+
/**
|
|
421
|
+
* What the agent does: the message it answers, if any, the waits of its
|
|
422
|
+
* tools, and the messages steered to it that it has not taken yet.
|
|
423
|
+
*/
|
|
424
|
+
status: restate.handlers.object.shared(async (ctx: restate.ObjectSharedContext<State>): Promise<Status> => {
|
|
425
|
+
const answering = await ctx.get('turn');
|
|
426
|
+
const status: Status = {
|
|
427
|
+
answering: shown(answering),
|
|
428
|
+
waiting: ((await ctx.get('waiting')) ?? []).map(({ id, name, input, since }) => ({ id, name, input, since })),
|
|
429
|
+
};
|
|
430
|
+
const steering = waitingIn((await ctx.get('inbox')) ?? [], answering).map(({ id, since }) => ({ id, since }));
|
|
431
|
+
return steering.length > 0 ? { ...status, steering } : status;
|
|
432
|
+
}),
|
|
310
433
|
|
|
311
434
|
/**
|
|
312
435
|
* The revision, once there is one newer than `revision`, or at the
|
|
@@ -330,15 +453,24 @@ export const signals = restate.object({
|
|
|
330
453
|
),
|
|
331
454
|
|
|
332
455
|
/** @internal The message `turn` (an invocation id) is answered, or its turn ended otherwise: see {@link closed}. */
|
|
333
|
-
close: internal(closed),
|
|
456
|
+
close: internal(async (ctx: restate.ObjectContext<State>, turn: string) => closed(ctx, turn)),
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* @internal The invocation of `turn` failed (killed, out of retries): if
|
|
460
|
+
* the turn did not close itself, its agent closes it too (see {@link closed}).
|
|
461
|
+
*/
|
|
462
|
+
ended: internal(async (ctx: restate.ObjectContext<State>, turn: string) => closed(ctx, turn, true)),
|
|
334
463
|
|
|
335
464
|
/**
|
|
336
465
|
* @internal The agent forgot its conversation (`clear`): forget its
|
|
337
|
-
* waits and
|
|
466
|
+
* waits, kept signals and steered messages, and publish the change. Each
|
|
467
|
+
* steered message was queued as a turn of its own as it was sent, before
|
|
468
|
+
* the `clear`.
|
|
338
469
|
*/
|
|
339
470
|
reset: restate.handlers.object.exclusive({ ingressPrivate: true }, async (ctx: restate.ObjectContext<State>) => {
|
|
340
471
|
ctx.clear('waiting');
|
|
341
472
|
ctx.clear('kept');
|
|
473
|
+
ctx.clear('inbox');
|
|
342
474
|
await publish(ctx);
|
|
343
475
|
}),
|
|
344
476
|
|
|
@@ -352,8 +484,82 @@ export const signals = restate.object({
|
|
|
352
484
|
await publish(ctx);
|
|
353
485
|
}),
|
|
354
486
|
|
|
355
|
-
/**
|
|
356
|
-
|
|
487
|
+
/**
|
|
488
|
+
* @internal The conversation changed: increase its revision, and answer
|
|
489
|
+
* the `watch`es. It answers the new revision, and the messages steered
|
|
490
|
+
* to the turn being answered that it has not taken yet, if any: a turn
|
|
491
|
+
* publishes at each tool call's start and end, so it learns of them at
|
|
492
|
+
* no cost of its own.
|
|
493
|
+
*/
|
|
494
|
+
publish: restate.handlers.object.exclusive(
|
|
495
|
+
{ ingressPrivate: true },
|
|
496
|
+
async (ctx: restate.ObjectContext<State>): Promise<Published> => {
|
|
497
|
+
const published: Published = await publish(ctx);
|
|
498
|
+
const inbox = (await ctx.get('inbox')) ?? [];
|
|
499
|
+
// Only then is the turn read: a turn that nobody steers costs one read more, of the empty inbox.
|
|
500
|
+
if (inbox.some((m) => m.text !== undefined)) {
|
|
501
|
+
const steering = waitingIn(inbox, await ctx.get('turn')).map(({ id, text }) => ({ id, text: text! }));
|
|
502
|
+
if (steering.length > 0) published.steering = steering;
|
|
503
|
+
}
|
|
504
|
+
return published;
|
|
505
|
+
},
|
|
506
|
+
),
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* @internal Take a message steered to the agent (its `steer` view): for
|
|
510
|
+
* the turn being answered, or as a turn of its own.
|
|
511
|
+
*
|
|
512
|
+
* The message is always sent to the agent's private `steered` handler at
|
|
513
|
+
* once, which keeps its place in the key's queue, in order with the
|
|
514
|
+
* messages sent after it: once the turn being answered ends, `steered`
|
|
515
|
+
* answers the message as a turn of its own, unless that turn took it. A
|
|
516
|
+
* turn that takes steered messages (`Started.steers`) also finds it
|
|
517
|
+
* here, at its next step, through `publish`, until it takes it (`taken`)
|
|
518
|
+
* or ends.
|
|
519
|
+
*/
|
|
520
|
+
steer: internal(async (ctx: restate.ObjectContext<State>, { id, text }: Steer): Promise<Steered> => {
|
|
521
|
+
const at = ctx.key.indexOf(':');
|
|
522
|
+
ctx.genericSend({
|
|
523
|
+
service: ctx.key.slice(0, at),
|
|
524
|
+
method: 'steered',
|
|
525
|
+
key: ctx.key.slice(at + 1),
|
|
526
|
+
parameter: { id, text },
|
|
527
|
+
inputSerde: restate.serde.json,
|
|
528
|
+
idempotencyKey: `steer:${id}`,
|
|
529
|
+
});
|
|
530
|
+
const answering = await ctx.get('turn');
|
|
531
|
+
if (!answering?.steers) return { id, turn: null };
|
|
532
|
+
const turn = answering.invocationId;
|
|
533
|
+
const inbox = (await ctx.get('inbox')) ?? [];
|
|
534
|
+
if (inbox.some((m) => m.id === id)) return { id, turn };
|
|
535
|
+
// Taken or not: the turn has room for so many in all.
|
|
536
|
+
if (inbox.filter((m) => m.turn === turn).length >= STEERS) return { id, turn: null };
|
|
537
|
+
const since = new Date(await ctx.date.now()).toISOString();
|
|
538
|
+
ctx.set('inbox', [...inbox, { id, since, turn, text }]);
|
|
539
|
+
await publish(ctx);
|
|
540
|
+
return { id, turn };
|
|
541
|
+
}),
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* @internal Mark the steered messages `ids` taken, which their turn has
|
|
545
|
+
* stored, and publish the change, if any. The turn sends it with the
|
|
546
|
+
* transaction that stores them, so it comes once they are stored, and
|
|
547
|
+
* once only. Each is kept without its text until the turn ends, as one
|
|
548
|
+
* of its {@link STEERS}; one already forgotten (its turn closed first)
|
|
549
|
+
* is left so.
|
|
550
|
+
*/
|
|
551
|
+
taken: internal(async (ctx: restate.ObjectContext<State>, ids: string[]) => {
|
|
552
|
+
const inbox = (await ctx.get('inbox')) ?? [];
|
|
553
|
+
let changed = false;
|
|
554
|
+
const marked = inbox.map((m) => {
|
|
555
|
+
if (m.text === undefined || !ids.includes(m.id)) return m;
|
|
556
|
+
changed = true;
|
|
557
|
+
return { id: m.id, since: m.since, turn: m.turn };
|
|
558
|
+
});
|
|
559
|
+
if (!changed) return;
|
|
560
|
+
setInbox(ctx, marked);
|
|
561
|
+
await publish(ctx);
|
|
562
|
+
}),
|
|
357
563
|
|
|
358
564
|
/** The conversation's revision. */
|
|
359
565
|
notifications: restate.handlers.object.shared(
|
package/src/steering.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Steering: a message sent while a turn is answered joins it, at the turn's
|
|
2
|
+
// next step.
|
|
3
|
+
//
|
|
4
|
+
// A client sends the message to the agent's `steer` view. The companion
|
|
5
|
+
// queues it at once as a turn of its own (the agent's private `steered`), in
|
|
6
|
+
// order with the messages sent after it, and, if the turn being answered
|
|
7
|
+
// takes steered messages, holds it for that turn too (`steer` of
|
|
8
|
+
// signals.ts).
|
|
9
|
+
//
|
|
10
|
+
// The turn learns of it from its next `publish`, which it makes at each tool
|
|
11
|
+
// call's start and end anyway (`Inbox` of common.ts). At its next step, once
|
|
12
|
+
// the model's reply has a result for each of its tool calls and before the
|
|
13
|
+
// next model request, the framework's integration calls `takeSteers`: the
|
|
14
|
+
// turn stores each message once, as a message of the user that is a step of
|
|
15
|
+
// the turn, and the integration puts it in the request about to go out. The
|
|
16
|
+
// transaction that stores them tells the companion they are taken (`taken`),
|
|
17
|
+
// once.
|
|
18
|
+
//
|
|
19
|
+
// Once the turn ends, the queued `steered` of a message it took does
|
|
20
|
+
// nothing; one it did not take (it came after the turn's last step, say) is
|
|
21
|
+
// answered as a turn of its own. So each message is answered once.
|
|
22
|
+
import type * as restate from '@restatedev/sessions';
|
|
23
|
+
import type { Transaction } from '@restatedev/sessions';
|
|
24
|
+
import { inboxOf, stopSignalOf } from './common.ts';
|
|
25
|
+
import type { Conversation } from './conversation.ts';
|
|
26
|
+
import { signals, signalsOf } from './signals.ts';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Take the messages steered to the turn being answered, at a step: store
|
|
30
|
+
* each once, and return their items.
|
|
31
|
+
*
|
|
32
|
+
* Call it right before a model request that follows the model's reply and
|
|
33
|
+
* its tools' results (or before the turn's first request): the integration
|
|
34
|
+
* then puts the returned items in the request, after what it already holds;
|
|
35
|
+
* one that builds each request from `conversation.history()` finds them
|
|
36
|
+
* there. They are messages of the user, as the framework's `Items` makes them
|
|
37
|
+
* (`steered`, or else `prompt`). It returns nothing, at no cost, unless the
|
|
38
|
+
* turn runs in a `chat` handler whose framework takes steered messages, a
|
|
39
|
+
* message it has not taken is waiting, the turn is not stopping, and each of
|
|
40
|
+
* its tool calls has a result.
|
|
41
|
+
*
|
|
42
|
+
* The messages are steps of the turn from then on: a retried attempt finds
|
|
43
|
+
* them stored, in `history()`, and never takes them again. They share the
|
|
44
|
+
* turn's fate: stopped, refused or failed with it, they are not answered
|
|
45
|
+
* again.
|
|
46
|
+
*/
|
|
47
|
+
export async function takeSteers<I>(ctx: restate.SessionContext, conversation: Conversation<I>): Promise<I[]> {
|
|
48
|
+
const inbox = inboxOf(ctx);
|
|
49
|
+
if (!inbox || inbox.messages.length === 0) return [];
|
|
50
|
+
const stop = stopSignalOf(ctx);
|
|
51
|
+
// A turn stopped while it waited to store them takes none: each is then answered as a turn of its own.
|
|
52
|
+
const going = () => stop?.aborted !== true;
|
|
53
|
+
const turn = conversation.running();
|
|
54
|
+
if (!going() || !turn || turn.openCalls().length > 0) return [];
|
|
55
|
+
return turn.steer(inbox.messages, (tx, ids) => tookSteers(ctx, tx, ids), going);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Tell the companion that the turn of `ctx` took the steered messages `ids`,
|
|
60
|
+
* with `tx`, the transaction that stores them: so they are marked taken once
|
|
61
|
+
* stored, and once. A framework that places steered messages itself (pi,
|
|
62
|
+
* DeepSeek) calls it as it copies them into the turn.
|
|
63
|
+
*/
|
|
64
|
+
export function tookSteers(ctx: restate.SessionContext, tx: Transaction, ids: string[]) {
|
|
65
|
+
if (ids.length === 0) return;
|
|
66
|
+
tx.send(signals, signalsOf(ctx), { idempotencyKey: `taken:${ids[0]}` }).taken(ids);
|
|
67
|
+
}
|
package/src/tools.ts
CHANGED
|
@@ -113,9 +113,15 @@ export const tool = <S extends z.ZodObject>(options: {
|
|
|
113
113
|
* the tool call runs (see calls.ts): it gets the id of the call, which an
|
|
114
114
|
* earlier attempt may have made, and so its idempotency key.
|
|
115
115
|
*/
|
|
116
|
-
async function once<T>(
|
|
116
|
+
async function once<T>(
|
|
117
|
+
name: string,
|
|
118
|
+
input: unknown,
|
|
119
|
+
context: ToolCall,
|
|
120
|
+
body: (call: ToolContext) => Promise<T>,
|
|
121
|
+
options?: { rejoin?: boolean; approval?: boolean },
|
|
122
|
+
) {
|
|
117
123
|
const { ctx } = context;
|
|
118
|
-
const call = await callOf(ctx, name, input, context.callId);
|
|
124
|
+
const call = await callOf(ctx, name, input, context.callId, options);
|
|
119
125
|
try {
|
|
120
126
|
const result = await body({ ctx, callId: call.id, idempotencyKey: `${ctx.request.id}:${call.id}` });
|
|
121
127
|
await call.answered();
|
|
@@ -334,10 +340,57 @@ export const signal = (name: string, options: { description: string; input?: z.Z
|
|
|
334
340
|
name: options.name ?? `wait_for_${name}`,
|
|
335
341
|
description: options.description,
|
|
336
342
|
input: z.object(options.input?.shape ?? {}),
|
|
337
|
-
run: async (input, context) =>
|
|
338
|
-
once(`signal/${name}`, input, context, async ({ ctx, idempotencyKey: id }) =>
|
|
339
|
-
ctx
|
|
340
|
-
.rpc(signals, signalsOf(ctx), { idempotencyKey: id })
|
|
341
|
-
.wait({ id, name, ...(options.input ? { input } : {}) }),
|
|
342
|
-
),
|
|
343
|
+
run: async (input, context) => wait(context, name, input, { shown: options.input !== undefined }),
|
|
343
344
|
});
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Wait for signal `name` as tool call `toolCall`, once however often it runs;
|
|
348
|
+
* `pending` shows `input` if `shown`. `rejoin` and `approval` are
|
|
349
|
+
* {@link callOf}'s.
|
|
350
|
+
*/
|
|
351
|
+
const wait = (
|
|
352
|
+
toolCall: ToolCall,
|
|
353
|
+
name: string,
|
|
354
|
+
input: unknown,
|
|
355
|
+
{ shown, rejoin, approval }: { shown: boolean; rejoin?: boolean; approval?: boolean },
|
|
356
|
+
): Promise<unknown> =>
|
|
357
|
+
once(
|
|
358
|
+
`signal/${name}`,
|
|
359
|
+
input,
|
|
360
|
+
toolCall,
|
|
361
|
+
async ({ ctx, idempotencyKey: id }) =>
|
|
362
|
+
ctx.rpc(signals, signalsOf(ctx), { idempotencyKey: id }).wait({ id, name, ...(shown ? { input } : {}) }),
|
|
363
|
+
{ rejoin, approval },
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Wait for the signal `name` to the agent, for the model's tool call
|
|
368
|
+
* `callId`, and answer with its value: what a {@link signal} tool does, for
|
|
369
|
+
* a framework whose own tool calls wait for something outside (an approval,
|
|
370
|
+
* say). The wait's id is `<invocation id>:<call id>`; `pending` lists it
|
|
371
|
+
* (with `input`, if given), and `signal` resolves it. It is opened once
|
|
372
|
+
* however often the tool call runs, and a crash goes on waiting on it.
|
|
373
|
+
*
|
|
374
|
+
* The wait is the tool call's alone: the framework stored its tool call
|
|
375
|
+
* before it waits, so a retried attempt waits again under the same id. A new
|
|
376
|
+
* call with the same input (the model asking again, after the first's
|
|
377
|
+
* answer) waits anew, as its answer is not the earlier call's.
|
|
378
|
+
*
|
|
379
|
+
* With `approval`, the tool call runs only after the wait: the wait's
|
|
380
|
+
* answer is not the call's, so a turn stopped before the call's result is
|
|
381
|
+
* stored reports the call as not done, rather than done.
|
|
382
|
+
*
|
|
383
|
+
* With `rejoin`, for a framework that makes a call cut off by a crash again
|
|
384
|
+
* under a new id (the DeepSeek Harness), the call joins the wait an earlier
|
|
385
|
+
* attempt opened with the same input, as Restate's own tools do: the call
|
|
386
|
+
* made again, waiting on (or getting the answer of) the same wait, until
|
|
387
|
+
* the earlier call's result is stored. Within one attempt, a call with the
|
|
388
|
+
* same input waits anew.
|
|
389
|
+
*/
|
|
390
|
+
export const waitFor = (
|
|
391
|
+
ctx: restate.SessionContext,
|
|
392
|
+
callId: string,
|
|
393
|
+
name: string,
|
|
394
|
+
input?: unknown,
|
|
395
|
+
{ approval = false, rejoin = false }: { approval?: boolean; rejoin?: boolean } = {},
|
|
396
|
+
): Promise<unknown> => wait({ ctx, callId }, name, input ?? null, { shown: input !== undefined, rejoin, approval });
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type Anthropic from '@anthropic-ai/sdk';
|
|
2
|
-
import type { Database } from '@restatedev/sessions';
|
|
3
|
-
type MessageParam = Anthropic.Beta.Messages.BetaMessageParam;
|
|
4
|
-
/** The conversation in the object's database: the tool runner's messages, and each prompt's answer. */
|
|
5
|
-
export declare class Conversation {
|
|
6
|
-
#private;
|
|
7
|
-
constructor(db: Database);
|
|
8
|
-
/** The conversation in `db` (`ctx.db`). */
|
|
9
|
-
static open(db: Database): Promise<Conversation>;
|
|
10
|
-
/** The answer to prompt `id`, or undefined. */
|
|
11
|
-
answer(id: string): Promise<string | undefined>;
|
|
12
|
-
/** The messages, in order. */
|
|
13
|
-
messages(): Promise<MessageParam[]>;
|
|
14
|
-
/** The messages stored after `seq`, in order, each with its own `seq`. */
|
|
15
|
-
since(seq: number): Promise<[seq: number, message: MessageParam][]>;
|
|
16
|
-
/** The prompt being answered: the last message's, unless it is answered. */
|
|
17
|
-
pending(): Promise<string | undefined>;
|
|
18
|
-
/**
|
|
19
|
-
* Close prompt `id`, which was cancelled: a result for each tool call of
|
|
20
|
-
* the last reply that has none, then `text` as the reply and the answer.
|
|
21
|
-
*/
|
|
22
|
-
cancel(id: string, text: string): Promise<void>;
|
|
23
|
-
/**
|
|
24
|
-
* Close prompt `id` with `text` as its last reply and its answer, after
|
|
25
|
-
* `reply` if given (a reply that cannot go on: cut off, or declined): first
|
|
26
|
-
* a result of `text` for each tool call of the last reply that has none.
|
|
27
|
-
*/
|
|
28
|
-
close(id: string, text: string, reply?: MessageParam): Promise<void>;
|
|
29
|
-
/** Store `messages` of prompt `id`, and its `answer` if given, in one transaction. */
|
|
30
|
-
append(id: string, messages: MessageParam[], answer?: string): Promise<void>;
|
|
31
|
-
}
|
|
32
|
-
export {};
|
|
33
|
-
//# sourceMappingURL=conversation.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"conversation.d.ts","sourceRoot":"","sources":["../../src/claude/conversation.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,SAAS,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAGrD,KAAK,YAAY,GAAG,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC;AAO7D,uGAAuG;AACvG,qBAAa,YAAY;;IAKvB,YAAY,EAAE,EAAE,QAAQ,EAEvB;IAED,2CAA2C;IAC3C,OAAa,IAAI,CAAC,EAAE,EAAE,QAAQ,GAAG,OAAO,CAAC,YAAY,CAAC,CAGrD;IAED,+CAA+C;IACzC,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAGpD;IAED,8BAA8B;IACxB,QAAQ,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC,CAGxC;IAED,0EAA0E;IACpE,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,CAAC,EAAE,CAAC,CAKxE;IAED,4EAA4E;IACtE,OAAO,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAK3C;IAED;;;OAGG;IACG,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGpD;IAED;;;;OAIG;IACG,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAUzE;IAED,sFAAsF;IACtF,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG3E;CAcF"}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
// A conversation with Claude in a Restate virtual object's database: its
|
|
2
|
-
// messages, in order, each with the id of the prompt it belongs to, and the
|
|
3
|
-
// answer to each prompt.
|
|
4
|
-
//
|
|
5
|
-
// It is the only state of the tool runner (@anthropic-ai/sdk's agent loop),
|
|
6
|
-
// which keeps the conversation in its params: an attempt starts a runner with
|
|
7
|
-
// the stored messages, and stores each message the runner adds.
|
|
8
|
-
import { Schema } from '@restatedev/sessions/internal';
|
|
9
|
-
const SCHEMA = new Schema('restate-agents.claude', [
|
|
10
|
-
'CREATE TABLE restate_claude_messages (seq INTEGER PRIMARY KEY, prompt TEXT NOT NULL, message TEXT NOT NULL)',
|
|
11
|
-
'CREATE TABLE restate_claude_answers (prompt TEXT PRIMARY KEY, answer TEXT NOT NULL)',
|
|
12
|
-
]);
|
|
13
|
-
/** The conversation in the object's database: the tool runner's messages, and each prompt's answer. */
|
|
14
|
-
export class Conversation {
|
|
15
|
-
#db;
|
|
16
|
-
/** Cancelled: a run cut off then stores nothing more. */
|
|
17
|
-
#closed = false;
|
|
18
|
-
constructor(db) {
|
|
19
|
-
this.#db = db;
|
|
20
|
-
}
|
|
21
|
-
/** The conversation in `db` (`ctx.db`). */
|
|
22
|
-
static async open(db) {
|
|
23
|
-
await SCHEMA.ensure(db);
|
|
24
|
-
return new Conversation(db);
|
|
25
|
-
}
|
|
26
|
-
/** The answer to prompt `id`, or undefined. */
|
|
27
|
-
async answer(id) {
|
|
28
|
-
const [row] = await this.#db.values('SELECT answer FROM restate_claude_answers WHERE prompt = ?', [id]);
|
|
29
|
-
return row?.[0];
|
|
30
|
-
}
|
|
31
|
-
/** The messages, in order. */
|
|
32
|
-
async messages() {
|
|
33
|
-
const rows = await this.#db.values('SELECT message FROM restate_claude_messages ORDER BY seq');
|
|
34
|
-
return rows.map(([message]) => JSON.parse(String(message)));
|
|
35
|
-
}
|
|
36
|
-
/** The messages stored after `seq`, in order, each with its own `seq`. */
|
|
37
|
-
async since(seq) {
|
|
38
|
-
const rows = await this.#db.values('SELECT seq, message FROM restate_claude_messages WHERE seq > ? ORDER BY seq', [
|
|
39
|
-
seq,
|
|
40
|
-
]);
|
|
41
|
-
return rows.map(([at, message]) => [Number(at), JSON.parse(String(message))]);
|
|
42
|
-
}
|
|
43
|
-
/** The prompt being answered: the last message's, unless it is answered. */
|
|
44
|
-
async pending() {
|
|
45
|
-
const [row] = await this.#db.values('SELECT prompt FROM restate_claude_messages WHERE prompt NOT IN (SELECT prompt FROM restate_claude_answers) ORDER BY seq DESC LIMIT 1');
|
|
46
|
-
return row?.[0];
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Close prompt `id`, which was cancelled: a result for each tool call of
|
|
50
|
-
* the last reply that has none, then `text` as the reply and the answer.
|
|
51
|
-
*/
|
|
52
|
-
async cancel(id, text) {
|
|
53
|
-
this.#closed = true;
|
|
54
|
-
await this.close(id, text);
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Close prompt `id` with `text` as its last reply and its answer, after
|
|
58
|
-
* `reply` if given (a reply that cannot go on: cut off, or declined): first
|
|
59
|
-
* a result of `text` for each tool call of the last reply that has none.
|
|
60
|
-
*/
|
|
61
|
-
async close(id, text, reply) {
|
|
62
|
-
const calls = toolUses(reply ?? (await this.messages()).at(-1));
|
|
63
|
-
const results = calls.map((call) => ({
|
|
64
|
-
type: 'tool_result',
|
|
65
|
-
tool_use_id: call.id,
|
|
66
|
-
content: text,
|
|
67
|
-
is_error: true,
|
|
68
|
-
}));
|
|
69
|
-
const closing = results.length > 0 ? [{ role: 'user', content: results }] : [];
|
|
70
|
-
await this.#store(id, [...(reply ? [reply] : []), ...closing, { role: 'assistant', content: text }], text);
|
|
71
|
-
}
|
|
72
|
-
/** Store `messages` of prompt `id`, and its `answer` if given, in one transaction. */
|
|
73
|
-
append(id, messages, answer) {
|
|
74
|
-
if (this.#closed)
|
|
75
|
-
return Promise.reject(new Error('the conversation was cancelled'));
|
|
76
|
-
return this.#store(id, messages, answer);
|
|
77
|
-
}
|
|
78
|
-
#store(id, messages, answer) {
|
|
79
|
-
return this.#db.transaction(async (tx) => {
|
|
80
|
-
for (const message of messages) {
|
|
81
|
-
await tx.execute('INSERT INTO restate_claude_messages (prompt, message) VALUES (?, ?)', [
|
|
82
|
-
id,
|
|
83
|
-
JSON.stringify(message),
|
|
84
|
-
]);
|
|
85
|
-
}
|
|
86
|
-
if (answer !== undefined)
|
|
87
|
-
await tx.execute('INSERT INTO restate_claude_answers (prompt, answer) VALUES (?, ?)', [id, answer]);
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/** The tool calls of `message`, if it is the assistant's. */
|
|
92
|
-
const toolUses = (message) => message?.role === 'assistant' && Array.isArray(message.content)
|
|
93
|
-
? message.content.filter((b) => b.type === 'tool_use')
|
|
94
|
-
: [];
|
|
95
|
-
//# sourceMappingURL=conversation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"conversation.js","sourceRoot":"","sources":["../../src/claude/conversation.ts"],"names":[],"mappings":"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"}
|
package/dist/log.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { DatabaseReader, DatabaseWriter, SessionContext } from '@restatedev/sessions';
|
|
2
|
-
import type { ChatMessage, Messages, MessagesRequest } from './common.ts';
|
|
3
|
-
/** Where the log starts: past the ids and cursors of the conversations `clear` forgot. */
|
|
4
|
-
export interface Start {
|
|
5
|
-
/** The cursor it starts at. */
|
|
6
|
-
seq: number;
|
|
7
|
-
/** Its first message's id. */
|
|
8
|
-
id: number;
|
|
9
|
-
}
|
|
10
|
-
/** A message as the transcript gives it: its role and parts, without an id. */
|
|
11
|
-
export type Said = Omit<ChatMessage, 'id'>;
|
|
12
|
-
/**
|
|
13
|
-
* Start the log at `start`, in the transaction `tx` of `clear` that emptied
|
|
14
|
-
* the database: the next conversation's ids and cursors go on from there.
|
|
15
|
-
*/
|
|
16
|
-
export declare function restart(tx: DatabaseWriter, start: Start): Promise<void>;
|
|
17
|
-
/**
|
|
18
|
-
* Store the transcript's `messages`, in order, in the log: those that
|
|
19
|
-
* changed since the last time, in one transaction.
|
|
20
|
-
*/
|
|
21
|
-
export declare function sync(ctx: SessionContext, messages: Said[]): Promise<void>;
|
|
22
|
-
/**
|
|
23
|
-
* A page of the log. With `cursor`: the messages new or changed since it,
|
|
24
|
-
* by when they changed, and the cursor to read on from. Without: the latest
|
|
25
|
-
* `limit` messages (or those before `before`, an id), oldest first, and the
|
|
26
|
-
* cursor to follow the conversation from.
|
|
27
|
-
*/
|
|
28
|
-
export declare function page(db: DatabaseReader, { limit, before, cursor }: MessagesRequest): Promise<Messages>;
|
|
29
|
-
//# sourceMappingURL=log.d.ts.map
|
package/dist/log.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3F,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAI1E,0FAA0F;AAC1F,MAAM,WAAW,KAAK;IACpB,+BAA+B;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,8BAA8B;IAC9B,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,+EAA+E;AAC/E,MAAM,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;AAqB3C;;;GAGG;AACH,wBAAsB,OAAO,CAAC,EAAE,EAAE,cAAc,EAAE,KAAK,EAAE,KAAK,iBAE7D;AAsCD;;;GAGG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,cAAc,EAAE,QAAQ,EAAE,IAAI,EAAE,iBAyC/D;AAED;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,EAAE,EAAE,cAAc,EAAE,EAAE,KAAU,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC,CA8BjH"}
|