@restatedev/agents 0.11.2 → 0.12.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 (218) hide show
  1. package/README.md +189 -808
  2. package/dist/pi/factory.d.ts +73 -0
  3. package/dist/pi/factory.d.ts.map +1 -0
  4. package/dist/pi/factory.js +145 -0
  5. package/dist/pi/factory.js.map +1 -0
  6. package/dist/pi/index.d.ts +5 -129
  7. package/dist/pi/index.d.ts.map +1 -1
  8. package/dist/pi/index.js +31 -571
  9. package/dist/pi/index.js.map +1 -1
  10. package/dist/pi/keepalive.d.ts +3 -16
  11. package/dist/pi/keepalive.d.ts.map +1 -1
  12. package/dist/pi/keepalive.js +38 -41
  13. package/dist/pi/keepalive.js.map +1 -1
  14. package/dist/pi/left.d.ts +16 -0
  15. package/dist/pi/left.d.ts.map +1 -0
  16. package/dist/pi/left.js +23 -0
  17. package/dist/pi/left.js.map +1 -0
  18. package/dist/pi/storage.d.ts +19 -76
  19. package/dist/pi/storage.d.ts.map +1 -1
  20. package/dist/pi/storage.js +50 -368
  21. package/dist/pi/storage.js.map +1 -1
  22. package/dist/pi/tools.d.ts +12 -0
  23. package/dist/pi/tools.d.ts.map +1 -0
  24. package/dist/pi/tools.js +70 -0
  25. package/dist/pi/tools.js.map +1 -0
  26. package/package.json +11 -254
  27. package/src/pi/factory.ts +231 -0
  28. package/src/pi/index.ts +31 -759
  29. package/src/pi/keepalive.ts +46 -56
  30. package/src/pi/left.ts +52 -0
  31. package/src/pi/storage.ts +75 -409
  32. package/src/pi/tools.ts +78 -0
  33. package/dist/agentlib/agent.d.ts +0 -32
  34. package/dist/agentlib/agent.d.ts.map +0 -1
  35. package/dist/agentlib/agent.js +0 -174
  36. package/dist/agentlib/agent.js.map +0 -1
  37. package/dist/agentlib/index.d.ts +0 -5
  38. package/dist/agentlib/index.d.ts.map +0 -1
  39. package/dist/agentlib/index.js +0 -15
  40. package/dist/agentlib/index.js.map +0 -1
  41. package/dist/agentlib/messages.d.ts +0 -39
  42. package/dist/agentlib/messages.d.ts.map +0 -1
  43. package/dist/agentlib/messages.js +0 -177
  44. package/dist/agentlib/messages.js.map +0 -1
  45. package/dist/agentlib/model.d.ts +0 -31
  46. package/dist/agentlib/model.d.ts.map +0 -1
  47. package/dist/agentlib/model.js +0 -140
  48. package/dist/agentlib/model.js.map +0 -1
  49. package/dist/agentlib/options.d.ts +0 -58
  50. package/dist/agentlib/options.d.ts.map +0 -1
  51. package/dist/agentlib/options.js +0 -2
  52. package/dist/agentlib/options.js.map +0 -1
  53. package/dist/agentlib/runtime.d.ts +0 -37
  54. package/dist/agentlib/runtime.d.ts.map +0 -1
  55. package/dist/agentlib/runtime.js +0 -216
  56. package/dist/agentlib/runtime.js.map +0 -1
  57. package/dist/agentlib/state.d.ts +0 -114
  58. package/dist/agentlib/state.d.ts.map +0 -1
  59. package/dist/agentlib/state.js +0 -52
  60. package/dist/agentlib/state.js.map +0 -1
  61. package/dist/agentlib/steering.d.ts +0 -23
  62. package/dist/agentlib/steering.d.ts.map +0 -1
  63. package/dist/agentlib/steering.js +0 -48
  64. package/dist/agentlib/steering.js.map +0 -1
  65. package/dist/agentlib/tools.d.ts +0 -33
  66. package/dist/agentlib/tools.d.ts.map +0 -1
  67. package/dist/agentlib/tools.js +0 -245
  68. package/dist/agentlib/tools.js.map +0 -1
  69. package/dist/agentlib/turn.d.ts +0 -82
  70. package/dist/agentlib/turn.d.ts.map +0 -1
  71. package/dist/agentlib/turn.js +0 -253
  72. package/dist/agentlib/turn.js.map +0 -1
  73. package/dist/calls.d.ts +0 -90
  74. package/dist/calls.d.ts.map +0 -1
  75. package/dist/calls.js +0 -241
  76. package/dist/calls.js.map +0 -1
  77. package/dist/claude/index.d.ts +0 -106
  78. package/dist/claude/index.d.ts.map +0 -1
  79. package/dist/claude/index.js +0 -386
  80. package/dist/claude/index.js.map +0 -1
  81. package/dist/client.d.ts +0 -90
  82. package/dist/client.d.ts.map +0 -1
  83. package/dist/client.js +0 -88
  84. package/dist/client.js.map +0 -1
  85. package/dist/common.d.ts +0 -410
  86. package/dist/common.d.ts.map +0 -1
  87. package/dist/common.js +0 -563
  88. package/dist/common.js.map +0 -1
  89. package/dist/companion.d.ts +0 -286
  90. package/dist/companion.d.ts.map +0 -1
  91. package/dist/companion.js +0 -463
  92. package/dist/companion.js.map +0 -1
  93. package/dist/conversation.d.ts +0 -576
  94. package/dist/conversation.d.ts.map +0 -1
  95. package/dist/conversation.js +0 -1476
  96. package/dist/conversation.js.map +0 -1
  97. package/dist/deepseek/barriers.d.ts +0 -11
  98. package/dist/deepseek/barriers.d.ts.map +0 -1
  99. package/dist/deepseek/barriers.js +0 -36
  100. package/dist/deepseek/barriers.js.map +0 -1
  101. package/dist/deepseek/index.d.ts +0 -97
  102. package/dist/deepseek/index.d.ts.map +0 -1
  103. package/dist/deepseek/index.js +0 -603
  104. package/dist/deepseek/index.js.map +0 -1
  105. package/dist/deepseek/persistence.d.ts +0 -83
  106. package/dist/deepseek/persistence.d.ts.map +0 -1
  107. package/dist/deepseek/persistence.js +0 -262
  108. package/dist/deepseek/persistence.js.map +0 -1
  109. package/dist/index.d.ts +0 -14
  110. package/dist/index.d.ts.map +0 -1
  111. package/dist/index.js +0 -17
  112. package/dist/index.js.map +0 -1
  113. package/dist/langgraph/checkpointer.d.ts +0 -52
  114. package/dist/langgraph/checkpointer.d.ts.map +0 -1
  115. package/dist/langgraph/checkpointer.js +0 -477
  116. package/dist/langgraph/checkpointer.js.map +0 -1
  117. package/dist/langgraph/context.d.ts +0 -10
  118. package/dist/langgraph/context.d.ts.map +0 -1
  119. package/dist/langgraph/context.js +0 -21
  120. package/dist/langgraph/context.js.map +0 -1
  121. package/dist/langgraph/errors.d.ts +0 -16
  122. package/dist/langgraph/errors.d.ts.map +0 -1
  123. package/dist/langgraph/errors.js +0 -30
  124. package/dist/langgraph/errors.js.map +0 -1
  125. package/dist/langgraph/graph.d.ts +0 -25
  126. package/dist/langgraph/graph.d.ts.map +0 -1
  127. package/dist/langgraph/graph.js +0 -378
  128. package/dist/langgraph/graph.js.map +0 -1
  129. package/dist/langgraph/index.d.ts +0 -119
  130. package/dist/langgraph/index.d.ts.map +0 -1
  131. package/dist/langgraph/index.js +0 -325
  132. package/dist/langgraph/index.js.map +0 -1
  133. package/dist/langgraph/keepalive.d.ts +0 -16
  134. package/dist/langgraph/keepalive.d.ts.map +0 -1
  135. package/dist/langgraph/keepalive.js +0 -51
  136. package/dist/langgraph/keepalive.js.map +0 -1
  137. package/dist/langgraph/nodes.d.ts +0 -71
  138. package/dist/langgraph/nodes.d.ts.map +0 -1
  139. package/dist/langgraph/nodes.js +0 -148
  140. package/dist/langgraph/nodes.js.map +0 -1
  141. package/dist/messages.d.ts +0 -18
  142. package/dist/messages.d.ts.map +0 -1
  143. package/dist/messages.js +0 -182
  144. package/dist/messages.js.map +0 -1
  145. package/dist/object.d.ts +0 -213
  146. package/dist/object.d.ts.map +0 -1
  147. package/dist/object.js +0 -309
  148. package/dist/object.js.map +0 -1
  149. package/dist/opencode/db.d.ts +0 -81
  150. package/dist/opencode/db.d.ts.map +0 -1
  151. package/dist/opencode/db.js +0 -249
  152. package/dist/opencode/db.js.map +0 -1
  153. package/dist/opencode/index.d.ts +0 -184
  154. package/dist/opencode/index.d.ts.map +0 -1
  155. package/dist/opencode/index.js +0 -1079
  156. package/dist/opencode/index.js.map +0 -1
  157. package/dist/output.d.ts +0 -25
  158. package/dist/output.d.ts.map +0 -1
  159. package/dist/output.js +0 -99
  160. package/dist/output.js.map +0 -1
  161. package/dist/pi/commits.d.ts +0 -6
  162. package/dist/pi/commits.d.ts.map +0 -1
  163. package/dist/pi/commits.js +0 -3
  164. package/dist/pi/commits.js.map +0 -1
  165. package/dist/steering.d.ts +0 -32
  166. package/dist/steering.d.ts.map +0 -1
  167. package/dist/steering.js +0 -45
  168. package/dist/steering.js.map +0 -1
  169. package/dist/tools.d.ts +0 -267
  170. package/dist/tools.d.ts.map +0 -1
  171. package/dist/tools.js +0 -305
  172. package/dist/tools.js.map +0 -1
  173. package/dist/webhook.d.ts +0 -71
  174. package/dist/webhook.d.ts.map +0 -1
  175. package/dist/webhook.js +0 -102
  176. package/dist/webhook.js.map +0 -1
  177. package/src/agentlib/README.md +0 -229
  178. package/src/agentlib/agent.ts +0 -216
  179. package/src/agentlib/index.ts +0 -16
  180. package/src/agentlib/messages.ts +0 -207
  181. package/src/agentlib/model.ts +0 -156
  182. package/src/agentlib/options.ts +0 -62
  183. package/src/agentlib/runtime.ts +0 -252
  184. package/src/agentlib/state.ts +0 -93
  185. package/src/agentlib/steering.ts +0 -53
  186. package/src/agentlib/tools.ts +0 -355
  187. package/src/agentlib/turn.ts +0 -302
  188. package/src/calls.ts +0 -333
  189. package/src/claude/README.md +0 -107
  190. package/src/claude/index.ts +0 -487
  191. package/src/client.ts +0 -187
  192. package/src/common.ts +0 -766
  193. package/src/companion.ts +0 -675
  194. package/src/conversation.ts +0 -1799
  195. package/src/deepseek/README.md +0 -202
  196. package/src/deepseek/barriers.ts +0 -63
  197. package/src/deepseek/index.ts +0 -740
  198. package/src/deepseek/persistence.ts +0 -337
  199. package/src/index.ts +0 -41
  200. package/src/langgraph/README.md +0 -199
  201. package/src/langgraph/checkpointer.ts +0 -614
  202. package/src/langgraph/context.ts +0 -27
  203. package/src/langgraph/errors.ts +0 -26
  204. package/src/langgraph/graph.ts +0 -422
  205. package/src/langgraph/index.ts +0 -500
  206. package/src/langgraph/keepalive.ts +0 -58
  207. package/src/langgraph/nodes.ts +0 -247
  208. package/src/messages.ts +0 -244
  209. package/src/object.ts +0 -454
  210. package/src/opencode/README.md +0 -249
  211. package/src/opencode/db.ts +0 -387
  212. package/src/opencode/index.ts +0 -1339
  213. package/src/output.ts +0 -116
  214. package/src/pi/README.md +0 -174
  215. package/src/pi/commits.ts +0 -7
  216. package/src/steering.ts +0 -68
  217. package/src/tools.ts +0 -515
  218. package/src/webhook.ts +0 -161
@@ -1,18 +1,30 @@
1
- // pi-durable makes a model's request in this process, through the model
2
- // registry (`runtime.models`): `streamSimple` for a turn's request, `fetchDeferred`
3
- // for one a provider answers later, `completeSimple` for a compaction's. A
4
- // request keeps the attempt alive (`ctx.working`) from its call to its end.
5
- // `Harness.open({ models })` takes the registry, so each of its requests is
6
- // covered by wrapping it, with nothing of pi-durable's own patched.
1
+ // Wraps pi's model registry (`Harness.open({ models })`) so that each request
2
+ // runs in a `ctx.stoppable` scope and gets the stop signal beside pi's own.
3
+ // pi-durable makes every model request through this registry (`streamSimple`,
4
+ // `fetchDeferred`, `completeSimple`), so the wrapper covers all of them without
5
+ // patching pi-durable. A request the signal aborts ends as aborted. pi-durable
6
+ // retries only a response that ended in an error, and commits an aborted one
7
+ // as the run's failure.
7
8
  import type { Models } from '@earendil-works/pi-ai';
8
9
  import type { AssistantMessageEventStream } from '@earendil-works/pi-ai';
9
10
  import type * as restate from '@restatedev/sessions';
10
- import { keepAlive } from '../common.ts';
11
+ import { openScope } from '@restatedev/sessions/internal';
11
12
 
12
- /** `Models`' methods that return an event stream: the request is out as it is called, and ends with the stream. */
13
+ /** Every `Models` method takes its options third. */
14
+ const OPTIONS = 2;
15
+
16
+ function withSignal(args: unknown[], signal: AbortSignal): unknown[] {
17
+ const options = args[OPTIONS] as { signal?: AbortSignal } | undefined;
18
+ const given = options?.signal;
19
+ const copy = [...args];
20
+ copy[OPTIONS] = { ...options, signal: given ? AbortSignal.any([given, signal]) : signal };
21
+ return copy;
22
+ }
23
+
24
+ /** The request is open from the call until the stream ends. */
13
25
  const STREAMS = new Set<PropertyKey>(['stream', 'streamSimple', 'streamDeferred']);
14
26
 
15
- /** `Models`' methods that return a promise of the request's result: the request is out until it settles. */
27
+ /** The request is open until the promise settles. */
16
28
  const REQUESTS = new Set<PropertyKey>([
17
29
  'complete',
18
30
  'completeSimple',
@@ -23,40 +35,27 @@ const REQUESTS = new Set<PropertyKey>([
23
35
  ]);
24
36
 
25
37
  /**
26
- * What a request of the model does with pi's batched commits (storage.ts): it
27
- * is an effect, so what pi committed before it is written, and its own
28
- * commits are not batched until it is over.
38
+ * Requests pi-durable makes while it stops a run: `cancelDeferred` cancels the
39
+ * provider's job after a cancellation. The stop signal has already aborted by
40
+ * then, so these run in `ctx.working`, without it, with the caller's options.
29
41
  */
30
- export interface Effects {
31
- /** The request starts: commits are written at once until the returned function is called (once at most). */
32
- started(): () => void;
33
- /** Write what is batched. */
34
- flush(): Promise<void>;
35
- }
42
+ const STOPPING = new Set<PropertyKey>(['cancelDeferred']);
36
43
 
37
44
  /**
38
- * `stream`, kept alive until its response is over: it ends or fails (its
39
- * `result()` settles: pi-ai ends a stream that is aborted with an error
40
- * event), its consumer leaves it early, or `signal` aborts. Then `ended`
41
- * is called too: the same events, and no more, so a stream that none of
42
- * them reaches never ends its effect, and the batching stays off.
45
+ * Keeps `stream` alive until `result()` settles (pi-ai ends an aborted stream
46
+ * with an error event), the consumer stops iterating, or `signal` aborts.
43
47
  */
44
48
  function keptStream(
45
- ctx: restate.SessionContext,
49
+ alive: { release(): void },
46
50
  stream: AssistantMessageEventStream,
47
- signal: AbortSignal | undefined,
48
- ended: () => void,
51
+ signal: AbortSignal,
49
52
  ): AssistantMessageEventStream {
50
- const alive = keepAlive(ctx);
51
- const release = () => {
52
- alive();
53
- ended();
54
- };
53
+ const release = () => alive.release();
55
54
  stream.result().then(release, release);
56
- signal?.addEventListener('abort', release, { once: true });
57
- if (signal?.aborted) release();
55
+ signal.addEventListener('abort', release, { once: true });
56
+ if (signal.aborted) release();
58
57
  const iterate = stream[Symbol.asyncIterator].bind(stream);
59
- // A consumer that stops iterating (a `break`, a throw) abandons the stream: the response is not waited for.
58
+ // A consumer that stops iterating (a `break`, a throw) abandons the stream.
60
59
  stream[Symbol.asyncIterator] = () => {
61
60
  const iterator = iterate();
62
61
  return {
@@ -76,12 +75,10 @@ function keptStream(
76
75
  }
77
76
 
78
77
  /**
79
- * `models`, each request of whose methods keeps the attempt of `ctx` alive as
80
- * long as it is out. With `effects`, each is also an effect: a stream's flush
81
- * is started as the stream is made and not waited for (the request has no
82
- * effect that must not repeat), a promise-returning method's is waited for.
78
+ * Wraps `models` so that each request keeps the attempt of `ctx` alive while
79
+ * it is open and is aborted by the stop signal (except `STOPPING`).
83
80
  */
84
- export function keptModels<M extends Models>(ctx: restate.SessionContext, models: M, effects?: Effects): M {
81
+ export function keptModels<M extends Models>(ctx: restate.SessionContext, models: M): M {
85
82
  return new Proxy(models, {
86
83
  get(target, property) {
87
84
  const member: unknown = Reflect.get(target, property, target);
@@ -89,31 +86,24 @@ export function keptModels<M extends Models>(ctx: restate.SessionContext, models
89
86
  const method = member.bind(target) as (...args: unknown[]) => unknown;
90
87
  if (STREAMS.has(property)) {
91
88
  return (...args: unknown[]) => {
92
- const options = args.at(-1) as { signal?: AbortSignal } | undefined;
93
- const ended = effects?.started() ?? (() => {});
94
- // A failed flush fails the attempt itself (storage.ts), and is never this request's error.
95
- void effects?.flush().catch(() => {});
89
+ const alive = openScope(ctx);
90
+ const kept = withSignal(args, alive.signal);
91
+ const signal = (kept[OPTIONS] as { signal: AbortSignal }).signal;
96
92
  let stream: AssistantMessageEventStream;
97
93
  try {
98
- stream = method(...args) as AssistantMessageEventStream;
94
+ stream = method(...kept) as AssistantMessageEventStream;
99
95
  } catch (error) {
100
- ended();
96
+ alive.release();
101
97
  throw error;
102
98
  }
103
- return keptStream(ctx, stream, options?.signal, ended);
99
+ return keptStream(alive, stream, signal);
104
100
  };
105
101
  }
106
102
  if (REQUESTS.has(property)) {
107
- if (!effects) return (...args: unknown[]) => ctx.working(() => method(...args) as Promise<unknown>);
108
- return async (...args: unknown[]) => {
109
- const ended = effects.started();
110
- try {
111
- await effects.flush();
112
- return await ctx.working(() => method(...args) as Promise<unknown>);
113
- } finally {
114
- ended();
115
- }
116
- };
103
+ const request = STOPPING.has(property)
104
+ ? (args: unknown[]) => ctx.working(() => method(...args) as Promise<unknown>)
105
+ : (args: unknown[]) => ctx.stoppable((signal) => method(...withSignal(args, signal)) as Promise<unknown>);
106
+ return (...args: unknown[]) => request(args);
117
107
  }
118
108
  return method;
119
109
  },
package/src/pi/left.ts ADDED
@@ -0,0 +1,52 @@
1
+ // Aborts runs that another invocation left in pi's storage, before this
2
+ // invocation's own work. A run is left when its invocation was cancelled while
3
+ // no work held the stop signal (the attempt had ended, or it was between
4
+ // requests), or crashed while stopping. Restate considers that turn over, but
5
+ // `Harness.open` would dispatch its tasks again and ask the model for a message
6
+ // whose invocation ended. `Conversation.abort()` commits abort marks before its
7
+ // scheduler dispatches, so the run's tasks run their abort handlers, not their
8
+ // progress, and its inputs settle unanswered. The run of an earlier attempt of this invocation is kept:
9
+ // the retry resumes it.
10
+ //
11
+ // Which runs are this invocation's own is not in the request ids, which the
12
+ // caller chooses. Restate runs one handler per key at a time, so every other
13
+ // invocation that wrote pi on the key has ended. Once this check is done, pi's
14
+ // next writing commit records this invocation as the writer (storage.ts), and
15
+ // every run live from then on is its own. So a later attempt that finds itself
16
+ // recorded keeps every live run, and has nothing to record; one that finds
17
+ // another invocation, or none, aborts them: none of them is its own, as it has
18
+ // not written since its check.
19
+ import type { Harness } from '@earendil-works/pi-durable';
20
+ import type * as restate from '@restatedev/sessions';
21
+ import { lastWriter, recordWriter } from './storage.ts';
22
+
23
+ /** The chord context the Harness takes. */
24
+ type Context = Parameters<Harness['inspect']>[0];
25
+
26
+ /**
27
+ * Aborts the runs of conversations that hold a live input with a request id,
28
+ * unless this invocation is the one that last wrote pi's storage after this
29
+ * check; otherwise records it as the writer of pi's next writing commit. Call it
30
+ * before this invocation's work (a prompt, a compaction, a wait). An input
31
+ * without a request id is not a prompt, and pi's internal writes are not runs:
32
+ * both are left alone. `db` is the database pi's storage was opened on
33
+ * (`openSqlKvStorage(db)`), by default `ctx.db`.
34
+ */
35
+ export async function abortLeft(
36
+ ctx: restate.SessionContext,
37
+ harness: Harness,
38
+ context: Context,
39
+ db: restate.Database = ctx.db,
40
+ ): Promise<void> {
41
+ if ((await lastWriter(db)) === ctx.request.id) return;
42
+ const { submissions } = await harness.inspect(context);
43
+ const live = new Set(
44
+ submissions.flatMap((s) => (s.type === 'input' && s.requestId !== undefined ? [s.conversationId] : [])),
45
+ );
46
+ const conversations = await Promise.all([...live].map((id) => harness.conversation(id, context)));
47
+ // `abort()` resumes pi's scheduler, which dispatches in a later microtask, and queues its marks' commit before it
48
+ // yields. So every abort is called before any is awaited: the scheduler's first dispatch comes after all the marks,
49
+ // where one conversation's abort awaited alone would let it run another's tasks.
50
+ await Promise.all(conversations.flatMap((conversation) => (conversation ? [conversation.abort(context)] : [])));
51
+ recordWriter(db, ctx.request.id);
52
+ }