@restatedev/agents 0.0.0-stage → 0.1.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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +689 -2
  3. package/dist/calls.d.ts +29 -0
  4. package/dist/calls.d.ts.map +1 -0
  5. package/dist/calls.js +91 -0
  6. package/dist/calls.js.map +1 -0
  7. package/dist/claude/conversation.d.ts +33 -0
  8. package/dist/claude/conversation.d.ts.map +1 -0
  9. package/dist/claude/conversation.js +95 -0
  10. package/dist/claude/conversation.js.map +1 -0
  11. package/dist/claude/index.d.ts +78 -0
  12. package/dist/claude/index.d.ts.map +1 -0
  13. package/dist/claude/index.js +269 -0
  14. package/dist/claude/index.js.map +1 -0
  15. package/dist/client.d.ts +83 -0
  16. package/dist/client.d.ts.map +1 -0
  17. package/dist/client.js +88 -0
  18. package/dist/client.js.map +1 -0
  19. package/dist/common.d.ts +284 -0
  20. package/dist/common.d.ts.map +1 -0
  21. package/dist/common.js +475 -0
  22. package/dist/common.js.map +1 -0
  23. package/dist/deepseek/index.d.ts +69 -0
  24. package/dist/deepseek/index.d.ts.map +1 -0
  25. package/dist/deepseek/index.js +381 -0
  26. package/dist/deepseek/index.js.map +1 -0
  27. package/dist/deepseek/persistence.d.ts +61 -0
  28. package/dist/deepseek/persistence.d.ts.map +1 -0
  29. package/dist/deepseek/persistence.js +224 -0
  30. package/dist/deepseek/persistence.js.map +1 -0
  31. package/dist/index.d.ts +15 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +37 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/langgraph/checkpointer.d.ts +29 -0
  36. package/dist/langgraph/checkpointer.d.ts.map +1 -0
  37. package/dist/langgraph/checkpointer.js +275 -0
  38. package/dist/langgraph/checkpointer.js.map +1 -0
  39. package/dist/langgraph/index.d.ts +67 -0
  40. package/dist/langgraph/index.d.ts.map +1 -0
  41. package/dist/langgraph/index.js +265 -0
  42. package/dist/langgraph/index.js.map +1 -0
  43. package/dist/log.d.ts +29 -0
  44. package/dist/log.d.ts.map +1 -0
  45. package/dist/log.js +145 -0
  46. package/dist/log.js.map +1 -0
  47. package/dist/object.d.ts +181 -0
  48. package/dist/object.d.ts.map +1 -0
  49. package/dist/object.js +232 -0
  50. package/dist/object.js.map +1 -0
  51. package/dist/opencode/db.d.ts +40 -0
  52. package/dist/opencode/db.d.ts.map +1 -0
  53. package/dist/opencode/db.js +161 -0
  54. package/dist/opencode/db.js.map +1 -0
  55. package/dist/opencode/index.d.ts +101 -0
  56. package/dist/opencode/index.d.ts.map +1 -0
  57. package/dist/opencode/index.js +464 -0
  58. package/dist/opencode/index.js.map +1 -0
  59. package/dist/output.d.ts +17 -0
  60. package/dist/output.d.ts.map +1 -0
  61. package/dist/output.js +81 -0
  62. package/dist/output.js.map +1 -0
  63. package/dist/pi/index.d.ts +90 -0
  64. package/dist/pi/index.d.ts.map +1 -0
  65. package/dist/pi/index.js +313 -0
  66. package/dist/pi/index.js.map +1 -0
  67. package/dist/pi/storage.d.ts +5 -0
  68. package/dist/pi/storage.d.ts.map +1 -0
  69. package/dist/pi/storage.js +41 -0
  70. package/dist/pi/storage.js.map +1 -0
  71. package/dist/signals.d.ts +195 -0
  72. package/dist/signals.d.ts.map +1 -0
  73. package/dist/signals.js +258 -0
  74. package/dist/signals.js.map +1 -0
  75. package/dist/tools.d.ts +158 -0
  76. package/dist/tools.d.ts.map +1 -0
  77. package/dist/tools.js +211 -0
  78. package/dist/tools.js.map +1 -0
  79. package/dist/webhook.d.ts +73 -0
  80. package/dist/webhook.d.ts.map +1 -0
  81. package/dist/webhook.js +104 -0
  82. package/dist/webhook.js.map +1 -0
  83. package/package.json +224 -3
  84. package/src/calls.ts +138 -0
  85. package/src/claude/README.md +84 -0
  86. package/src/claude/conversation.ts +114 -0
  87. package/src/claude/index.ts +364 -0
  88. package/src/client.ts +179 -0
  89. package/src/common.ts +593 -0
  90. package/src/deepseek/README.md +110 -0
  91. package/src/deepseek/index.ts +487 -0
  92. package/src/deepseek/persistence.ts +293 -0
  93. package/src/index.ts +60 -0
  94. package/src/langgraph/README.md +87 -0
  95. package/src/langgraph/checkpointer.ts +364 -0
  96. package/src/langgraph/index.ts +349 -0
  97. package/src/log.ts +193 -0
  98. package/src/object.ts +343 -0
  99. package/src/opencode/README.md +125 -0
  100. package/src/opencode/db.ts +223 -0
  101. package/src/opencode/index.ts +595 -0
  102. package/src/output.ts +95 -0
  103. package/src/pi/README.md +105 -0
  104. package/src/pi/index.ts +402 -0
  105. package/src/pi/storage.ts +65 -0
  106. package/src/signals.ts +426 -0
  107. package/src/tools.ts +343 -0
  108. package/src/webhook.ts +163 -0
package/src/signals.ts ADDED
@@ -0,0 +1,426 @@
1
+ // Signals to agents: a virtual object per agent, keyed `<agent>:<key>` (such
2
+ // as `assistant:alice`), whose `wait` returns once `resolve` gives the wait
3
+ // its value; the messages steered to the message an agent answers; and the
4
+ // revision of the agent's conversation, which its `watch` waits on.
5
+ //
6
+ // An agent's own handler cannot wait on an awakeable (it has no journal to
7
+ // replay one from), so it calls `wait`, which can: a journaled handler of
8
+ // this object, which waits without holding a process.
9
+ import type { SessionContext } from '@restatedev/sessions';
10
+ import { sdk as restate } from '@restatedev/sessions/internal';
11
+ import type { Turn } from './common.ts';
12
+ import type { Status } from './object.ts';
13
+
14
+ /** An agent's context, as far as its signals need it: any handler's or view's of its object. */
15
+ type AgentContext = { readonly key: string; readonly request: { readonly target: { readonly service: string } } };
16
+
17
+ /** The agent of `ctx`: its object's name, and key. */
18
+ export const agentOf = (ctx: AgentContext): Agent => ({ service: ctx.request.target.service, key: ctx.key });
19
+
20
+ /** The key of the signals of agent `service`'s key `key`: `<agent>:<key>`. */
21
+ const keyOf = (service: string, key: string) => `${service}:${key}`;
22
+
23
+ /** The key of the signals of the agent of `ctx`. */
24
+ export const signalsOf = (ctx: AgentContext) => keyOf(ctx.request.target.service, ctx.key);
25
+
26
+ /** Where an object of @restatedev/agents (`sessionObject()`, every `agent()`) points to its companion. */
27
+ const COMPANION = Symbol('restate-agents.companion');
28
+
29
+ /**
30
+ * An agent's companion, for one of its keys: its `signals` object, which
31
+ * knows what the agent is doing (the message it answers, what its tools wait
32
+ * for), and how to reach it.
33
+ */
34
+ export interface Companion {
35
+ /** The `signals` object, as a client takes it. */
36
+ object: typeof signals;
37
+ /** The key of the agent's key there: `<agent>:<key>`. */
38
+ key: string;
39
+ /** The agent, and its key. */
40
+ agent: Agent;
41
+ }
42
+
43
+ /** `definition`, pointing to its companion: what `sessionObject()` returns. */
44
+ export const withCompanion = <D extends { name: string }>(definition: D): D =>
45
+ Object.defineProperty(definition, COMPANION, {
46
+ value: (key: string): Companion => ({
47
+ object: signals,
48
+ key: keyOf(definition.name, key),
49
+ agent: { service: definition.name, key },
50
+ }),
51
+ });
52
+
53
+ /**
54
+ * The companion of `agent`'s key `key`: for anyone holding an agent (an
55
+ * `sessionObject()` or `agent()` of @restatedev/agents) to reach what it is doing.
56
+ */
57
+ export function companionOf(agent: { name: string }, key: string): Companion {
58
+ const companion = (agent as { [COMPANION]?: (key: string) => Companion })[COMPANION];
59
+ if (!companion)
60
+ throw new Error(`${agent.name} is not an agent of @restatedev/agents (a sessionObject() or an agent())`);
61
+ return companion(key);
62
+ }
63
+
64
+ /** Whether `agent` is an object of @restatedev/agents, with a companion. */
65
+ export const hasCompanion = (agent: object) => COMPANION in agent;
66
+
67
+ /**
68
+ * Deliver `text` to `agent`'s key `key`, from any handler: to the message it
69
+ * answers, if it answers one, which takes it at its next step (as `steer`);
70
+ * else as a message of its own (as `chat`). A turn that ends, or is stopped,
71
+ * before it takes it answers it after. It is sent, not awaited: `opts` may
72
+ * give it an idempotency key, so that it is delivered once.
73
+ *
74
+ * deliver(ctx, triage, 'ENG-1', 'The issue was closed', { idempotencyKey: delivery });
75
+ */
76
+ export function deliver(
77
+ ctx: Pick<restate.Context, 'objectSendClient'> | Pick<SessionContext, 'send'>,
78
+ agent: { name: string },
79
+ key: string,
80
+ text: string,
81
+ opts: { idempotencyKey?: string } = {},
82
+ ) {
83
+ const { object, key: companion, agent: target } = companionOf(agent, key);
84
+ const delivery = { agent: target, text };
85
+ // A session object's handler, or one of the SDK's.
86
+ if ('send' in ctx) ctx.send(object, companion, opts).deliver(delivery);
87
+ else ctx.objectSendClient(object, companion).deliver(delivery, restate.rpc.sendOpts(opts));
88
+ }
89
+
90
+ /** A wait for a signal, as `pending` lists it. */
91
+ export interface Waiting {
92
+ /** The wait's id, which `signal` takes to resolve this wait and no other. */
93
+ id: string;
94
+ /** The signal it waits for, such as `approval`. */
95
+ name: string;
96
+ /** What it waits for, as the model said it (such as the refund to approve), if the tool takes an input. */
97
+ input?: unknown;
98
+ /** When it started, as an ISO date. */
99
+ since: string;
100
+ }
101
+
102
+ /** Which wait a signal resolves, and with what. */
103
+ export interface Signal {
104
+ /** The wait of this id (from `pending`). */
105
+ id?: string;
106
+ /** Else the oldest wait for the signal of this name. */
107
+ name?: string;
108
+ /** Its value: what the wait answers the model with. */
109
+ value?: unknown;
110
+ /** With a `name` and no one waiting for it, keep the signal for the next wait for it (by default it is dropped). */
111
+ keep?: boolean;
112
+ }
113
+
114
+ /** What `signal` did: the id of the wait it resolved, or null if none waited (and it was dropped, or kept). */
115
+ export interface Resolved {
116
+ resolved: string | null;
117
+ }
118
+
119
+ /** A message steered to the message being answered (its turn): its id, `<turn>:steer:<n>`, and its text. */
120
+ export interface Steer {
121
+ id: string;
122
+ text: string;
123
+ /** A delivery (`deliver`): a cancelled turn answers it as a message of its own, rather than drop it. */
124
+ keep?: boolean;
125
+ }
126
+
127
+ /**
128
+ * What `steer` did: the invocation that answers it, and whether it steers
129
+ * the message being answered (or, with none answered, was sent as a message
130
+ * of its own).
131
+ */
132
+ export interface Steered {
133
+ invocationId: string;
134
+ steered: boolean;
135
+ }
136
+
137
+ /** The revision of an agent's conversation: each change of what a client reads increases it. */
138
+ export interface Notifications {
139
+ revision: number;
140
+ }
141
+
142
+ /** What `watch` waits for: a revision newer than `revision`, for at most `timeoutSeconds` (default 50, at most 60). */
143
+ export interface Watch {
144
+ revision: number;
145
+ timeoutSeconds?: number;
146
+ }
147
+
148
+ /** An agent: its object's name, and key. */
149
+ interface Agent {
150
+ service: string;
151
+ key: string;
152
+ }
153
+
154
+ /** The turns closed last, newest last: a message steered to one of them is a message of its own. */
155
+ const CLOSED = 16;
156
+
157
+ /**
158
+ * The waits, in the order they started, each with its awakeable; the signals
159
+ * kept for the next wait; the messages steered to the turn being answered,
160
+ * and the turns closed last; and the conversation's revision, with the
161
+ * `watch`es waiting for a newer one.
162
+ */
163
+ interface State {
164
+ /** The message the agent answers, from its turn's start to its close. */
165
+ turn: Turn;
166
+ waiting: (Waiting & { awakeable: string })[];
167
+ kept: Record<string, unknown[]>;
168
+ steering: { turn: string; messages: Steer[] };
169
+ closed: string[];
170
+ revision: number;
171
+ watching: { revision: number; awakeable: string }[];
172
+ }
173
+
174
+ /** Increase the revision, and answer each `watch` waiting for it: the new revision. */
175
+ async function publish(ctx: restate.ObjectContext<State>): Promise<Notifications> {
176
+ const revision = ((await ctx.get('revision')) ?? 0) + 1;
177
+ ctx.set('revision', revision);
178
+ for (const { awakeable } of (await ctx.get('watching')) ?? []) ctx.resolveAwakeable(awakeable, { revision });
179
+ ctx.clear('watching');
180
+ return { revision };
181
+ }
182
+
183
+ /** Send `text` to `agent`'s `chat`, as a message of its own: its invocation's id. */
184
+ const send = (ctx: restate.ObjectContext<State>, { service, key }: Agent, text: string) =>
185
+ ctx.genericSend({ service, method: 'chat', key, parameter: text, inputSerde: restate.serde.json }).invocationId;
186
+
187
+ /**
188
+ * Steer `turn` (the message `agent` answers, if any) with `text`: queued for
189
+ * it to take at its next step, or, if it is over, sent as a message of its own.
190
+ */
191
+ async function steered(ctx: restate.ObjectContext<State>, agent: Agent, text: string, keep = false): Promise<Steered> {
192
+ const turn = (await ctx.get('turn'))?.invocationId ?? null;
193
+ if (turn === null || ((await ctx.get('closed')) ?? []).includes(turn)) {
194
+ return { invocationId: await send(ctx, agent, text), steered: false };
195
+ }
196
+ const steering = await ctx.get('steering');
197
+ const messages = steering?.turn === turn ? steering.messages : [];
198
+ const id = `${turn}:steer:${messages.length + 1}`;
199
+ ctx.set('steering', { turn, messages: [...messages, { id, text, ...(keep ? { keep } : {}) }] });
200
+ await publish(ctx);
201
+ return { invocationId: turn, steered: true };
202
+ }
203
+
204
+ /** A turn's close: see `close`. */
205
+ interface Close {
206
+ agent: Agent;
207
+ turn: string;
208
+ taken: string[];
209
+ cancelled: boolean;
210
+ }
211
+
212
+ /** Close `turn`, once: see `close`. */
213
+ async function closed(ctx: restate.ObjectContext<State>, { agent, turn, taken, cancelled }: Close) {
214
+ if ((await ctx.get('turn'))?.invocationId === turn) ctx.clear('turn');
215
+ const done = (await ctx.get('closed')) ?? [];
216
+ if (done.includes(turn)) return;
217
+ ctx.set('closed', [...done, turn].slice(-CLOSED));
218
+ const steering = await ctx.get('steering');
219
+ if (steering?.turn !== turn) return;
220
+ ctx.clear('steering');
221
+ for (const { id, text, keep } of steering.messages) {
222
+ if (!taken.includes(id) && (keep || !cancelled)) await send(ctx, agent, text);
223
+ }
224
+ }
225
+
226
+ /** A handler that the agents' own handlers call, which clients cannot (through Restate's ingress). */
227
+ const internal = <I, O>(fn: (ctx: restate.ObjectContext<State>, input: I) => Promise<O>) =>
228
+ restate.handlers.object.exclusive({ ingressPrivate: true }, fn);
229
+
230
+ /**
231
+ * The signals of every agent, keyed `<agent>:<key>` ({@link signalsOf}).
232
+ * `serve` serves it beside the agents; the handlers marked `@internal` are
233
+ * called by the agents' own handlers, and Restate's ingress refuses them.
234
+ */
235
+ export const signals = restate.object({
236
+ name: 'signals',
237
+ handlers: {
238
+ /** Wait for the signal `name`, as wait `id` (for `input`), and return its value. */
239
+ wait: restate.handlers.object.shared(
240
+ async (ctx: restate.ObjectSharedContext<State>, wait: { id: string; name: string; input?: unknown }) => {
241
+ const { id, promise } = ctx.awakeable<unknown>();
242
+ const since = new Date(await ctx.date.now()).toISOString();
243
+ ctx.objectSendClient(signals, ctx.key).listen({ ...wait, since, awakeable: id });
244
+ try {
245
+ return await promise;
246
+ } catch (error) {
247
+ // The agent's invocation was cancelled: no one waits any more.
248
+ if (error instanceof restate.CancelledError) ctx.objectSendClient(signals, ctx.key).forget(wait.id);
249
+ throw error;
250
+ }
251
+ },
252
+ ),
253
+
254
+ /** Resolve the wait `id`, or else the oldest wait for `name`, with `value`. */
255
+ resolve: async (ctx: restate.ObjectContext<State>, { id, name, value = null, keep = false }: Signal) => {
256
+ if (id === undefined && name === undefined) throw new restate.TerminalError('a signal needs an id or a name');
257
+ const waiting = (await ctx.get('waiting')) ?? [];
258
+ const at = waiting.findIndex((w) => (id === undefined ? w.name === name : w.id === id));
259
+ if (at < 0) {
260
+ if (keep && name !== undefined && id === undefined) {
261
+ const kept = (await ctx.get('kept')) ?? {};
262
+ ctx.set('kept', { ...kept, [name]: [...(kept[name] ?? []), value] });
263
+ }
264
+ return { resolved: null } satisfies Resolved;
265
+ }
266
+ const [wait] = waiting.splice(at, 1);
267
+ ctx.resolveAwakeable(wait!.awakeable, value);
268
+ ctx.set('waiting', waiting);
269
+ await publish(ctx);
270
+ return { resolved: wait!.id } satisfies Resolved;
271
+ },
272
+
273
+ /** The waits, oldest first. */
274
+ pending: restate.handlers.object.shared(async (ctx: restate.ObjectSharedContext<State>): Promise<Waiting[]> =>
275
+ ((await ctx.get('waiting')) ?? []).map(({ id, name, input, since }) => ({ id, name, input, since })),
276
+ ),
277
+
278
+ /** @internal A wait: resolved now with a signal kept for it, or else when one comes. */
279
+ listen: internal(async (ctx: restate.ObjectContext<State>, wait: Waiting & { awakeable: string }) => {
280
+ const kept = (await ctx.get('kept')) ?? {};
281
+ const [value, ...rest] = kept[wait.name] ?? [];
282
+ if (kept[wait.name]?.length) {
283
+ ctx.resolveAwakeable(wait.awakeable, value);
284
+ ctx.set('kept', { ...kept, [wait.name]: rest });
285
+ return;
286
+ }
287
+ ctx.set('waiting', [...((await ctx.get('waiting')) ?? []), wait]);
288
+ await publish(ctx);
289
+ }),
290
+
291
+ /**
292
+ * @internal Steer the message `agent` answers, if any, with `text`:
293
+ * queued for it to take at its next step, or, if none is answered, sent
294
+ * as a message of its own.
295
+ */
296
+ steer: internal(async (ctx: restate.ObjectContext<State>, { agent, text }: { agent: Agent; text: string }) =>
297
+ steered(ctx, agent, text),
298
+ ),
299
+
300
+ /**
301
+ * @internal The agent's `chat` started answering `turn`: the same
302
+ * invocation again (a retry) keeps when it started. A turn before it that
303
+ * never closed (its invocation was killed) closes now, as a cancelled one:
304
+ * the deliveries steered to it are answered.
305
+ */
306
+ start: internal(async (ctx: restate.ObjectContext<State>, { agent, turn }: { agent: Agent; turn: Turn }) => {
307
+ const before = (await ctx.get('turn'))?.invocationId;
308
+ if (before === turn.invocationId) return;
309
+ if (before !== undefined) await closed(ctx, { agent, turn: before, taken: [], cancelled: true });
310
+ ctx.set('turn', turn);
311
+ }),
312
+
313
+ /** What the agent does: the message it answers, if any, and the waits of its tools. */
314
+ status: restate.handlers.object.shared(async (ctx: restate.ObjectSharedContext<State>): Promise<Status> => ({
315
+ answering: (await ctx.get('turn')) ?? null,
316
+ waiting: ((await ctx.get('waiting')) ?? []).map(({ id, name, input, since }) => ({ id, name, input, since })),
317
+ })),
318
+
319
+ /**
320
+ * The revision, once there is one newer than `revision`, or at the
321
+ * timeout (50 s by default, at most 60 s), whichever comes first. It
322
+ * waits in Restate, holding no process; a caller that retries it with the
323
+ * same idempotency key joins the same wait.
324
+ */
325
+ watch: restate.handlers.object.shared(
326
+ async (
327
+ ctx: restate.ObjectSharedContext<State>,
328
+ { revision, timeoutSeconds = 50 }: Watch,
329
+ ): Promise<Notifications> => {
330
+ const { id, promise } = ctx.awakeable<Notifications>();
331
+ ctx.objectSendClient(signals, ctx.key).subscribe({ revision, awakeable: id });
332
+ const timeout = ctx.sleep({ seconds: Math.min(Math.max(timeoutSeconds, 0), 60) }).map(() => null);
333
+ const changed = await restate.RestatePromise.race([promise, timeout]);
334
+ if (changed) return changed;
335
+ ctx.objectSendClient(signals, ctx.key).unsubscribe(id);
336
+ return { revision: (await ctx.get('revision')) ?? 0 };
337
+ },
338
+ ),
339
+
340
+ /**
341
+ * @internal Deliver `text` to `agent` (a webhook's delivery, say): steered
342
+ * to the message it answers, if any, which takes it at its next step;
343
+ * else, or once that turn is over, as a message of its own. Unlike a
344
+ * steer, a cancelled turn does not drop it: it is answered after.
345
+ */
346
+ deliver: internal(async (ctx: restate.ObjectContext<State>, { agent, text }: { agent: Agent; text: string }) =>
347
+ steered(ctx, agent, text, true),
348
+ ),
349
+
350
+ /** @internal The messages steered to `turn`, in order. */
351
+ steering: restate.handlers.object.shared(
352
+ { ingressPrivate: true },
353
+ async (ctx: restate.ObjectSharedContext<State>, turn: string) => {
354
+ const steering = await ctx.get('steering');
355
+ return steering?.turn === turn ? steering.messages : [];
356
+ },
357
+ ),
358
+
359
+ /**
360
+ * @internal `turn` is over: each message steered to it that it did not
361
+ * take (not in `taken`) is sent as a message of its own, unless the turn
362
+ * was cancelled (but for a delivery); later ones are, too.
363
+ */
364
+ close: internal(closed),
365
+
366
+ /**
367
+ * @internal The agent forgot its conversation (`clear`): forget its
368
+ * waits, kept signals and steered messages, and publish the change.
369
+ */
370
+ reset: restate.handlers.object.exclusive({ ingressPrivate: true }, async (ctx: restate.ObjectContext<State>) => {
371
+ ctx.clear('waiting');
372
+ ctx.clear('kept');
373
+ ctx.clear('steering');
374
+ await publish(ctx);
375
+ }),
376
+
377
+ /** @internal Forget wait `id`, whose agent no longer waits. */
378
+ forget: internal(async (ctx: restate.ObjectContext<State>, id: string) => {
379
+ const waiting = (await ctx.get('waiting')) ?? [];
380
+ ctx.set(
381
+ 'waiting',
382
+ waiting.filter((w) => w.id !== id),
383
+ );
384
+ await publish(ctx);
385
+ }),
386
+
387
+ /** @internal The conversation changed: increase its revision, and answer the `watch`es. */
388
+ publish: restate.handlers.object.exclusive({ ingressPrivate: true }, publish),
389
+
390
+ /**
391
+ * @internal The turn `turn` made progress (a tool call ended): publish
392
+ * it, and answer the messages steered to the turn.
393
+ */
394
+ progress: internal(async (ctx: restate.ObjectContext<State>, turn: string) => {
395
+ await publish(ctx);
396
+ const steering = await ctx.get('steering');
397
+ return steering?.turn === turn ? steering.messages : [];
398
+ }),
399
+
400
+ /** The conversation's revision. */
401
+ notifications: restate.handlers.object.shared(
402
+ async (ctx: restate.ObjectSharedContext<State>): Promise<Notifications> => ({
403
+ revision: (await ctx.get('revision')) ?? 0,
404
+ }),
405
+ ),
406
+
407
+ /** @internal Answer `awakeable` once there is a revision newer than `revision`: at once, if there is. */
408
+ subscribe: internal(async (ctx: restate.ObjectContext<State>, watch: { revision: number; awakeable: string }) => {
409
+ const revision = (await ctx.get('revision')) ?? 0;
410
+ if (revision > watch.revision) {
411
+ ctx.resolveAwakeable(watch.awakeable, { revision });
412
+ return;
413
+ }
414
+ ctx.set('watching', [...((await ctx.get('watching')) ?? []), watch]);
415
+ }),
416
+
417
+ /** @internal Forget the `watch` of `awakeable`, which no longer waits. */
418
+ unsubscribe: internal(async (ctx: restate.ObjectContext<State>, awakeable: string) => {
419
+ const watching = (await ctx.get('watching')) ?? [];
420
+ ctx.set(
421
+ 'watching',
422
+ watching.filter((w) => w.awakeable !== awakeable),
423
+ );
424
+ }),
425
+ },
426
+ });