@restatedev/agents 0.2.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 -8
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +79 -18
- 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 +238 -156
- 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 +161 -71
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +277 -226
- 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 +323 -107
- 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 +234 -180
- 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 +123 -40
- 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 +75 -7
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +154 -27
- 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 +110 -24
- package/src/claude/README.md +41 -20
- package/src/claude/index.ts +271 -183
- package/src/client.ts +14 -3
- package/src/common.ts +354 -247
- package/src/conversation.ts +1527 -0
- package/src/deepseek/README.md +78 -11
- package/src/deepseek/index.ts +385 -138
- 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 +337 -193
- package/src/langgraph/nodes.ts +243 -0
- package/src/messages.ts +244 -0
- package/src/object.ts +178 -48
- package/src/output.ts +52 -31
- package/src/pi/README.md +18 -3
- package/src/pi/index.ts +346 -79
- package/src/pi/storage.ts +20 -7
- package/src/signals.ts +235 -31
- 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 -145
- 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 -193
- 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,14 +1,15 @@
|
|
|
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
|
|
7
8
|
// replay one from), so it calls `wait`, which can: a journaled handler of
|
|
8
9
|
// this object, which waits without holding a process.
|
|
9
10
|
import type { SessionContext } from '@restatedev/sessions';
|
|
10
|
-
import { needing, sdk as restate } from '@restatedev/sessions/internal';
|
|
11
|
-
import type {
|
|
11
|
+
import { Link, needing, sdk as restate } from '@restatedev/sessions/internal';
|
|
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. */
|
|
@@ -20,8 +21,8 @@ const keyOf = (service: string, key: string) => `${service}:${key}`;
|
|
|
20
21
|
/** The key of the signals of the agent of `ctx`. */
|
|
21
22
|
export const signalsOf = (ctx: AgentContext) => keyOf(ctx.request.target.service, ctx.key);
|
|
22
23
|
|
|
23
|
-
/**
|
|
24
|
-
const COMPANION =
|
|
24
|
+
/** How an object of @restatedev/agents (`sessionObject()`, every `agent()`) reaches its companion, for one of its keys. */
|
|
25
|
+
const COMPANION = new Link<(key: string) => Companion>('restate-agents.companion');
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* An agent's companion, for one of its keys: its `signals` object, which
|
|
@@ -43,13 +44,11 @@ export interface Companion {
|
|
|
43
44
|
*/
|
|
44
45
|
export const withCompanion = <D extends { name: string }>(definition: D): D =>
|
|
45
46
|
needing(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}),
|
|
52
|
-
}),
|
|
47
|
+
COMPANION.on(definition, (key) => ({
|
|
48
|
+
object: signals,
|
|
49
|
+
key: keyOf(definition.name, key),
|
|
50
|
+
agent: { service: definition.name, key },
|
|
51
|
+
})),
|
|
53
52
|
() => [signals],
|
|
54
53
|
);
|
|
55
54
|
|
|
@@ -58,14 +57,14 @@ export const withCompanion = <D extends { name: string }>(definition: D): D =>
|
|
|
58
57
|
* `sessionObject()` or `agent()` of @restatedev/agents) to reach what it is doing.
|
|
59
58
|
*/
|
|
60
59
|
export function companionOf(agent: { name: string }, key: string): Companion {
|
|
61
|
-
const companion = (agent
|
|
60
|
+
const companion = COMPANION.of(agent);
|
|
62
61
|
if (!companion)
|
|
63
62
|
throw new Error(`${agent.name} is not an agent of @restatedev/agents (a sessionObject() or an agent())`);
|
|
64
63
|
return companion(key);
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
/** Whether `agent` is an object of @restatedev/agents, with a companion. */
|
|
68
|
-
export const hasCompanion = (agent: object) => COMPANION
|
|
67
|
+
export const hasCompanion = (agent: object) => COMPANION.of(agent) !== undefined;
|
|
69
68
|
|
|
70
69
|
/**
|
|
71
70
|
* Deliver `text` to `agent`'s key `key`, from any handler: a message of its
|
|
@@ -132,12 +131,92 @@ export interface Notifications {
|
|
|
132
131
|
revision: number;
|
|
133
132
|
}
|
|
134
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
|
+
|
|
135
177
|
/** What `watch` waits for: a revision newer than `revision`, for at most `timeoutSeconds` (default 50, at most 60). */
|
|
136
178
|
export interface Watch {
|
|
137
179
|
revision: number;
|
|
138
180
|
timeoutSeconds?: number;
|
|
139
181
|
}
|
|
140
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
|
+
|
|
141
220
|
/** An agent: its object's name, and key. */
|
|
142
221
|
interface Agent {
|
|
143
222
|
service: string;
|
|
@@ -151,9 +230,11 @@ interface Agent {
|
|
|
151
230
|
*/
|
|
152
231
|
interface State {
|
|
153
232
|
/** The message the agent answers, from its turn's start to its close. */
|
|
154
|
-
turn:
|
|
233
|
+
turn: Started;
|
|
155
234
|
waiting: (Waiting & { awakeable: string })[];
|
|
156
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[];
|
|
157
238
|
revision: number;
|
|
158
239
|
watching: { revision: number; awakeable: string }[];
|
|
159
240
|
}
|
|
@@ -167,6 +248,10 @@ async function publish(ctx: restate.ObjectContext<State>): Promise<Notifications
|
|
|
167
248
|
return { revision };
|
|
168
249
|
}
|
|
169
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
|
+
|
|
170
255
|
/** How long one `awaitTurn` waits for its turn to end, before a new one takes over. */
|
|
171
256
|
const WATCH = { hours: 1 };
|
|
172
257
|
|
|
@@ -177,10 +262,38 @@ const WATCH = { hours: 1 };
|
|
|
177
262
|
* It publishes the change, for the clients that follow the conversation: a
|
|
178
263
|
* turn's own close (its last change), or the close of one that never closed
|
|
179
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.
|
|
180
273
|
*/
|
|
181
|
-
async function closed(ctx: restate.ObjectContext<State>, turn: string) {
|
|
182
|
-
const
|
|
183
|
-
|
|
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
|
+
);
|
|
184
297
|
const waiting = (await ctx.get('waiting')) ?? [];
|
|
185
298
|
// A wait's id starts with the invocation id of the turn whose tool waits.
|
|
186
299
|
const ofTurn = (id: string) => id.startsWith(`${turn}:`);
|
|
@@ -188,7 +301,7 @@ async function closed(ctx: restate.ObjectContext<State>, turn: string) {
|
|
|
188
301
|
const left = waiting.filter((w) => !ofTurn(w.id));
|
|
189
302
|
for (const w of over) ctx.rejectAwakeable(w.awakeable, 'its turn is over');
|
|
190
303
|
if (over.length > 0) ctx.set('waiting', left);
|
|
191
|
-
if (answering || over.length > 0) await publish(ctx);
|
|
304
|
+
if (answering || over.length > 0 || dropped) await publish(ctx);
|
|
192
305
|
}
|
|
193
306
|
|
|
194
307
|
/** A handler that the agents' own handlers call, which clients cannot (through Restate's ingress). */
|
|
@@ -263,10 +376,10 @@ export const signals = restate.object({
|
|
|
263
376
|
* `awaitTurn` did not close it yet. `awaitTurn` watches the new turn, so
|
|
264
377
|
* that it closes even if no turn comes after it.
|
|
265
378
|
*/
|
|
266
|
-
start: internal(async (ctx: restate.ObjectContext<State>, turn:
|
|
379
|
+
start: internal(async (ctx: restate.ObjectContext<State>, turn: Started) => {
|
|
267
380
|
const before = (await ctx.get('turn'))?.invocationId;
|
|
268
381
|
if (before === turn.invocationId) return;
|
|
269
|
-
if (before !== undefined) await closed(ctx, before);
|
|
382
|
+
if (before !== undefined) await closed(ctx, before, true);
|
|
270
383
|
ctx.set('turn', turn);
|
|
271
384
|
ctx.objectSendClient(signals, ctx.key).awaitTurn(turn.invocationId);
|
|
272
385
|
}),
|
|
@@ -297,18 +410,26 @@ export const signals = restate.object({
|
|
|
297
410
|
.map((_answer, failure) => ({ failed: failure !== undefined })),
|
|
298
411
|
ctx.sleep(WATCH).map(() => null),
|
|
299
412
|
]);
|
|
300
|
-
if (ended?.failed) ctx.objectSendClient(signals, ctx.key).
|
|
413
|
+
if (ended?.failed) ctx.objectSendClient(signals, ctx.key).ended(turn);
|
|
301
414
|
else if (ended === null && (await ctx.get('turn'))?.invocationId === turn) {
|
|
302
415
|
ctx.objectSendClient(signals, ctx.key).awaitTurn(turn);
|
|
303
416
|
}
|
|
304
417
|
},
|
|
305
418
|
),
|
|
306
419
|
|
|
307
|
-
/**
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
+
}),
|
|
312
433
|
|
|
313
434
|
/**
|
|
314
435
|
* The revision, once there is one newer than `revision`, or at the
|
|
@@ -332,15 +453,24 @@ export const signals = restate.object({
|
|
|
332
453
|
),
|
|
333
454
|
|
|
334
455
|
/** @internal The message `turn` (an invocation id) is answered, or its turn ended otherwise: see {@link closed}. */
|
|
335
|
-
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)),
|
|
336
463
|
|
|
337
464
|
/**
|
|
338
465
|
* @internal The agent forgot its conversation (`clear`): forget its
|
|
339
|
-
* 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`.
|
|
340
469
|
*/
|
|
341
470
|
reset: restate.handlers.object.exclusive({ ingressPrivate: true }, async (ctx: restate.ObjectContext<State>) => {
|
|
342
471
|
ctx.clear('waiting');
|
|
343
472
|
ctx.clear('kept');
|
|
473
|
+
ctx.clear('inbox');
|
|
344
474
|
await publish(ctx);
|
|
345
475
|
}),
|
|
346
476
|
|
|
@@ -354,8 +484,82 @@ export const signals = restate.object({
|
|
|
354
484
|
await publish(ctx);
|
|
355
485
|
}),
|
|
356
486
|
|
|
357
|
-
/**
|
|
358
|
-
|
|
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
|
+
}),
|
|
359
563
|
|
|
360
564
|
/** The conversation's revision. */
|
|
361
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
|