@restatedev/agents 0.5.0 → 0.7.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 (101) hide show
  1. package/README.md +79 -24
  2. package/dist/calls.d.ts +20 -1
  3. package/dist/calls.d.ts.map +1 -1
  4. package/dist/calls.js +46 -7
  5. package/dist/calls.js.map +1 -1
  6. package/dist/claude/index.d.ts +11 -11
  7. package/dist/claude/index.d.ts.map +1 -1
  8. package/dist/claude/index.js +12 -6
  9. package/dist/claude/index.js.map +1 -1
  10. package/dist/client.d.ts +1 -1
  11. package/dist/client.d.ts.map +1 -1
  12. package/dist/common.d.ts +62 -23
  13. package/dist/common.d.ts.map +1 -1
  14. package/dist/common.js +67 -38
  15. package/dist/common.js.map +1 -1
  16. package/dist/{signals.d.ts → companion.d.ts} +106 -43
  17. package/dist/companion.d.ts.map +1 -0
  18. package/dist/companion.js +463 -0
  19. package/dist/companion.js.map +1 -0
  20. package/dist/conversation.d.ts +103 -5
  21. package/dist/conversation.d.ts.map +1 -1
  22. package/dist/conversation.js +295 -75
  23. package/dist/conversation.js.map +1 -1
  24. package/dist/deepseek/index.d.ts +11 -11
  25. package/dist/deepseek/index.d.ts.map +1 -1
  26. package/dist/deepseek/index.js +26 -26
  27. package/dist/deepseek/index.js.map +1 -1
  28. package/dist/index.d.ts +8 -4
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +7 -3
  31. package/dist/index.js.map +1 -1
  32. package/dist/langgraph/checkpointer.d.ts +3 -1
  33. package/dist/langgraph/checkpointer.d.ts.map +1 -1
  34. package/dist/langgraph/checkpointer.js +15 -6
  35. package/dist/langgraph/checkpointer.js.map +1 -1
  36. package/dist/langgraph/graph.d.ts.map +1 -1
  37. package/dist/langgraph/graph.js +32 -13
  38. package/dist/langgraph/graph.js.map +1 -1
  39. package/dist/langgraph/index.d.ts +11 -11
  40. package/dist/langgraph/index.d.ts.map +1 -1
  41. package/dist/langgraph/index.js +19 -13
  42. package/dist/langgraph/index.js.map +1 -1
  43. package/dist/langgraph/nodes.d.ts +2 -1
  44. package/dist/langgraph/nodes.d.ts.map +1 -1
  45. package/dist/langgraph/nodes.js +23 -20
  46. package/dist/langgraph/nodes.js.map +1 -1
  47. package/dist/object.d.ts +24 -22
  48. package/dist/object.d.ts.map +1 -1
  49. package/dist/object.js +14 -11
  50. package/dist/object.js.map +1 -1
  51. package/dist/opencode/index.d.ts +11 -11
  52. package/dist/opencode/index.d.ts.map +1 -1
  53. package/dist/opencode/index.js +25 -27
  54. package/dist/opencode/index.js.map +1 -1
  55. package/dist/pi/index.d.ts +13 -13
  56. package/dist/pi/index.d.ts.map +1 -1
  57. package/dist/pi/index.js +18 -20
  58. package/dist/pi/index.js.map +1 -1
  59. package/dist/pi/storage.d.ts +7 -0
  60. package/dist/pi/storage.d.ts.map +1 -1
  61. package/dist/pi/storage.js +12 -0
  62. package/dist/pi/storage.js.map +1 -1
  63. package/dist/steering.d.ts +2 -1
  64. package/dist/steering.d.ts.map +1 -1
  65. package/dist/steering.js +2 -2
  66. package/dist/steering.js.map +1 -1
  67. package/dist/tools.d.ts +21 -0
  68. package/dist/tools.d.ts.map +1 -1
  69. package/dist/tools.js +54 -14
  70. package/dist/tools.js.map +1 -1
  71. package/dist/webhook.js +1 -1
  72. package/dist/webhook.js.map +1 -1
  73. package/package.json +90 -84
  74. package/src/calls.ts +52 -11
  75. package/src/claude/README.md +4 -2
  76. package/src/claude/index.ts +12 -7
  77. package/src/client.ts +1 -1
  78. package/src/common.ts +99 -40
  79. package/src/companion.ts +674 -0
  80. package/src/conversation.ts +353 -101
  81. package/src/deepseek/README.md +7 -1
  82. package/src/deepseek/index.ts +25 -24
  83. package/src/index.ts +10 -4
  84. package/src/langgraph/README.md +8 -5
  85. package/src/langgraph/checkpointer.ts +26 -15
  86. package/src/langgraph/graph.ts +30 -16
  87. package/src/langgraph/index.ts +24 -17
  88. package/src/langgraph/nodes.ts +24 -20
  89. package/src/object.ts +43 -40
  90. package/src/opencode/README.md +5 -2
  91. package/src/opencode/index.ts +26 -27
  92. package/src/pi/README.md +6 -1
  93. package/src/pi/index.ts +25 -19
  94. package/src/pi/storage.ts +13 -0
  95. package/src/steering.ts +8 -7
  96. package/src/tools.ts +60 -18
  97. package/src/webhook.ts +1 -1
  98. package/dist/signals.d.ts.map +0 -1
  99. package/dist/signals.js +0 -383
  100. package/dist/signals.js.map +0 -1
  101. package/src/signals.ts +0 -590
package/src/common.ts CHANGED
@@ -5,8 +5,34 @@
5
5
  // budget.
6
6
  import * as restate from '@restatedev/sessions';
7
7
  import { cancelCalls, cancelled, definitionOf, Link, ScopeKey } from '@restatedev/sessions/internal';
8
- import { settleCalls } from './calls.ts';
9
- import { type Published, type Steer, signals, signalsOf } from './signals.ts';
8
+ import { flushHeld, settleCalls } from './calls.ts';
9
+ import { companionHere, type Published, type Steer } from './companion.ts';
10
+
11
+ /**
12
+ * The context of an agent's handler: a session context, from an object that
13
+ * has a companion.
14
+ *
15
+ * The agent's own surface takes it: the handlers and views of
16
+ * `sessionObject()` and of a framework's `agent()`, a `chat` handler's
17
+ * answer, and what only an agent does with a context ({@link chatTurn},
18
+ * {@link publish}). The layer a handler belongs to is then in its
19
+ * signature, where the person writing it reads it, rather than found out in
20
+ * the middle of a turn, after a model round-trip, looking like a tool
21
+ * failure.
22
+ *
23
+ * It is a signpost, not a fence: it adds nothing to a session context, so
24
+ * the two are the same type to the compiler, and `NoCompanion` stays the
25
+ * guard that stops a wait from an object without a companion.
26
+ */
27
+ export interface AgentContext extends restate.SessionContext {}
28
+
29
+ /**
30
+ * The context of an agent's view: a view context, from an object that has a
31
+ * companion. What {@link AgentContext} is to a handler, for the views that
32
+ * read what the agent is doing, or tell its companion to (`status`,
33
+ * `pending`, `signal`, `steer`, `watch`).
34
+ */
35
+ export interface AgentViewContext extends restate.ViewContext {}
10
36
 
11
37
  /**
12
38
  * A value, or a function of the invocation's context that gives it: the
@@ -273,16 +299,19 @@ export const settingsOf = (ctx: restate.SessionContext): Settings =>
273
299
  /** Whether the agent of `ctx` runs in the background. */
274
300
  const unfollowed = (ctx: restate.SessionContext) => agentOf(ctx)?.background === true;
275
301
 
276
- /** The `signals` object of the agent of `ctx`. */
277
- const signalsOfAgent = (ctx: restate.SessionContext) => ctx.rpc(signals, signalsOf(ctx));
302
+ /**
303
+ * The companion of the agent of `ctx`, for `needs`: its own, at its key. An
304
+ * object without one fails with a terminal error (`NoCompanion`).
305
+ */
306
+ const companionOfAgent = (ctx: restate.SessionContext, needs: string) => ctx.rpc(companionHere(ctx, needs), ctx.key);
278
307
 
279
308
  /**
280
309
  * The messages steered to a `chat` turn that it has not taken yet, as the
281
310
  * companion last published them. The companion answers them to every
282
- * `publish`, which a turn makes at each tool call's start and end: so the
283
- * turn knows of them without a call of its own, and finds them at its next
284
- * step (`takeSteers`). The newest revision wins, as tool calls that run at
285
- * once publish at once.
311
+ * `publish` (and `start`), which a turn makes as it starts and after each
312
+ * write of its steps that commits: so the turn knows of them without a call
313
+ * of its own, and finds them at its next step (`takeSteers`). The newest
314
+ * revision wins, as writes that run at once publish at once.
286
315
  */
287
316
  export class Inbox {
288
317
  revision = -1;
@@ -321,35 +350,54 @@ export const inboxOf = (ctx: restate.SessionContext) => INBOX.get(ctx);
321
350
  * increases, and the clients that follow it read it again; while a framework
322
351
  * answers ({@link answering}), the calls whose results it stored are
323
352
  * forgotten first. It is a call to Restate, so it also sends what the
324
- * handler wrote before, for them to read.
353
+ * handler wrote before, for them to read: the rows the conversation holds
354
+ * back are written first (`flushHeld`), as the clients read them once told.
355
+ *
356
+ * Only a `chat` turn ({@link chatTurn}: after each commit of its steps) and
357
+ * `settle` publish, on an object with a companion: one without fails with a
358
+ * terminal error (`NoCompanion`), rather than call a companion nobody serves.
325
359
  */
326
- export const publish = async (ctx: restate.SessionContext) => {
327
- const stored = ANSWERING.get(ctx);
328
- if (stored) await settled(ctx, stored);
329
- const published = await signalsOfAgent(ctx).publish();
360
+ export const publish = async (ctx: AgentContext) => {
361
+ const companion = companionOfAgent(ctx, 'a publish');
362
+ await flushHeld(ctx);
363
+ const published = await companion.publish();
330
364
  INBOX.get(ctx)?.saw(published);
331
365
  };
332
366
 
333
- /** A step of a turn of `ctx`: {@link publish} it, unless the agent runs in the background, which publishes the turn's end only. */
334
- export const stepped = async (ctx: restate.SessionContext) => {
335
- if (!unfollowed(ctx)) await publish(ctx);
336
- };
367
+ /**
368
+ * What the turn running on `ctx` makes after each commit of its steps (a
369
+ * reply with tool calls, the tools' results), if anything: set by
370
+ * {@link chatTurn}, for a followed turn, to publish. The conversation takes
371
+ * it as it is, without knowing what it does (`answerOnce` hands it to
372
+ * `Conversation.hold`); a turn run outside a `chat` turn (a handler of your
373
+ * own that calls a framework's `prompt()`) has none: it stores its steps,
374
+ * and publishes nothing.
375
+ */
376
+ const AFTER_STEPS = new ScopeKey<() => Promise<void>>('afterSteps');
377
+
378
+ /** What the turn running on `ctx` makes after each commit of its steps, if anything: see {@link AFTER_STEPS}. */
379
+ export const afterStepsOf = (ctx: restate.SessionContext): (() => Promise<void>) | undefined => AFTER_STEPS.get(ctx);
337
380
 
338
381
  /**
339
- * A tool call of the turn of `ctx` starts: {@link publish} it, which forgets
340
- * the calls whose results are stored (calls.ts) before the tool makes its
341
- * own. In the background, which publishes the turn's end only, it forgets
342
- * them alone.
382
+ * A tool call of the turn of `ctx` starts: forget the calls whose results are
383
+ * stored (calls.ts) before the tool makes its own. It publishes nothing: a
384
+ * followed `chat` turn publishes once the call is stored (with its reply),
385
+ * and once its result is ({@link afterStepsOf}).
343
386
  */
344
387
  export async function toolCallStarted(ctx: restate.SessionContext) {
345
- if (!unfollowed(ctx)) return publish(ctx);
346
- const stored = ANSWERING.get(ctx);
347
- if (stored) await settled(ctx, stored);
388
+ await settleAnswered(ctx);
348
389
  }
349
390
 
350
- /** A tool call of the turn of `ctx` (a `chat` handler's) ended: {@link publish} it, unless the agent runs in the background. */
351
- export async function toolCallEnded(ctx: restate.SessionContext) {
352
- await stepped(ctx);
391
+ /**
392
+ * Forget the call records (calls.ts) whose results the turn answering on
393
+ * `ctx` has stored ({@link answering}), if one does: after each commit of its
394
+ * steps (`answerOnce` does, before what the turn asked for then, such as a
395
+ * `chat` turn's publish, for every turn, in a `chat` turn or not), as a tool
396
+ * call starts, and as the turn ends.
397
+ */
398
+ export async function settleAnswered(ctx: restate.SessionContext) {
399
+ const stored = ANSWERING.get(ctx);
400
+ if (stored) await settled(ctx, stored);
353
401
  }
354
402
 
355
403
  /** Whether a tool call's result is stored: `stored(callId, failed)`, `failed` the error the call answered with. */
@@ -371,8 +419,9 @@ function settled(ctx: restate.SessionContext, stored: Stored): Promise<void> {
371
419
 
372
420
  /**
373
421
  * Run `work`, a framework's turn on the conversation of turns, forgetting
374
- * the calls whose results it stores as it goes: each publish while it runs
375
- * (a tool call's start and end), and its end, forget the Restate calls of
422
+ * the calls whose results it stores as it goes: each write of its steps
423
+ * that commits ({@link settleAnswered}), each tool call's start, and its
424
+ * end, forget the Restate calls of
376
425
  * the tool calls whose results `stored` says are stored (calls.ts).
377
426
  */
378
427
  export async function answering<T>(ctx: restate.SessionContext, stored: Stored, work: () => Promise<T>): Promise<T> {
@@ -403,14 +452,14 @@ export const aborted = (signal: AbortSignal) =>
403
452
  });
404
453
 
405
454
  /** A `chat` handler's answer to a message: a framework's prompt, say. */
406
- export type Chat<A = unknown> = (ctx: restate.SessionContext, message: string) => Promise<A>;
455
+ export type Chat<A = unknown> = (ctx: AgentContext, message: string) => Promise<A>;
407
456
 
408
457
  /**
409
458
  * Whether a `chat` handler's turns take steered messages: for every message,
410
459
  * or for each, as the invocation's context says (a framework's options that
411
460
  * a function gives, say).
412
461
  */
413
- export type Steers = boolean | ((ctx: restate.SessionContext) => boolean | Promise<boolean>);
462
+ export type Steers = boolean | ((ctx: AgentContext) => boolean | Promise<boolean>);
414
463
 
415
464
  /** What makes a `chat` handler (`chatHandler`): its answer, and whether it takes steered messages. */
416
465
  const CHAT = new Link<{ answer: Chat; steers: Steers }>('restate-agents.chat');
@@ -434,8 +483,9 @@ export const chatOf = (entry: object) => CHAT.of(entry);
434
483
  * attempt that ends first (`attemptCompletedSignal`) aborts it too, with an
435
484
  * {@link AttemptEnded}: the work stops, and the next attempt goes on with it.
436
485
  *
437
- * It publishes when it starts and ends ({@link publish}), for the clients
438
- * that follow the conversation.
486
+ * It publishes when it starts (its companion's `start` does) and ends
487
+ * ({@link publish}), for the clients that follow the conversation, unless
488
+ * the agent runs in the background, which publishes the end only.
439
489
  *
440
490
  * As the `chat` handler of an agent, it also answers each message sent to
441
491
  * the agent's `steer` view that no turn took, as a turn of its own (the
@@ -448,7 +498,7 @@ export const chatHandler = <A = string>(answer: Chat<A>, { steers = false }: { s
448
498
  input: messageSerde,
449
499
  accept: '*/*',
450
500
  explicitCancellation: true,
451
- handler: (ctx: restate.SessionContext, text: string): Promise<A> => chatTurn(ctx, text, answer, steers),
501
+ handler: (ctx: AgentContext, text: string): Promise<A> => chatTurn(ctx, text, answer, steers),
452
502
  },
453
503
  { answer, steers },
454
504
  );
@@ -460,7 +510,7 @@ export const chatHandler = <A = string>(answer: Chat<A>, { steers = false }: { s
460
510
  * in the background).
461
511
  */
462
512
  export async function chatTurn<A>(
463
- ctx: restate.SessionContext,
513
+ ctx: AgentContext,
464
514
  text: string,
465
515
  answer: Chat<A>,
466
516
  steers: Steers = false,
@@ -473,20 +523,28 @@ export async function chatTurn<A>(
473
523
  const steering = !unfollowed(ctx) && (typeof steers === 'function' ? await steers(ctx) : steers);
474
524
  // Before the first publish, which may answer messages steered to the turn already (a retried attempt's).
475
525
  if (steering) INBOX.set(ctx, new Inbox());
476
- await ctx.rpc(signals, signalsOf(ctx)).start({
526
+ // A followed turn's start is its first publish: the companion makes it, and returns what `publish` returns.
527
+ const followed = !unfollowed(ctx);
528
+ const companion = companionOfAgent(ctx, 'a chat turn');
529
+ const started = await companion.start({
477
530
  invocationId,
478
531
  since: new Date().toISOString(),
479
532
  ...(settles ? { settles } : {}),
480
533
  ...(steering ? { steers: steering } : {}),
534
+ ...(followed ? { publishes: true } : {}),
481
535
  });
482
- await stepped(ctx);
536
+ INBOX.get(ctx)?.saw(started);
537
+ // A followed turn publishes after each commit of its steps, for its
538
+ // followers to read them there: the conversation makes what the turn sets
539
+ // here (`afterStepsOf`), without knowing it publishes. A background turn
540
+ // publishes its end only.
541
+ if (followed) AFTER_STEPS.set(ctx, () => publish(ctx));
483
542
  // The log takes the turn's last change, then the companion closes the
484
543
  // turn, which publishes it: one revision. The close is a call the turn
485
544
  // waits for, so that it is done before the turn ends (see `awaitTurn`).
486
545
  const end = async () => {
487
- const stored = ANSWERING.get(ctx);
488
- if (stored) await settled(ctx, stored);
489
- await signalsOfAgent(ctx).close(invocationId);
546
+ await settleAnswered(ctx);
547
+ await companion.close(invocationId);
490
548
  };
491
549
  const turn = new AbortController();
492
550
  TURN.set(ctx, turn.signal);
@@ -517,6 +575,7 @@ export async function chatTurn<A>(
517
575
  } finally {
518
576
  // An attempt's signal aborts at every attempt's end, the turn's too.
519
577
  attempt.removeEventListener('abort', attemptEnded);
578
+ AFTER_STEPS.delete(ctx);
520
579
  }
521
580
  await end();
522
581
  return answered;