@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,40 +1,42 @@
1
- // pi-durable storage on an SQL database: the `SqliteDatabase` facade of
2
- // @earendil-works/chord/sqlite, over a database session `db` (in Restate,
3
- // `ctx.db`), which has `query`, `get`, `execute` and
4
- // `transaction(body)`.
5
- //
6
- // The facade's operations must run one at a time, a transaction excluding
7
- // every other: the session does that, and a transaction's handle is invalid
8
- // once its body settles. A statement outside a transaction is its own
9
- // transaction.
10
- //
11
- // With `commits: 'effects'` the adapter batches: between two effects (a model
12
- // call, a tool) the commits of pi share one transaction of the session, held
13
- // open and written once, when an effect starts, something else is about to
14
- // see what was stored, or another call to the session waits for it. See
15
- // {@link SqlKvDatabase}.
16
- //
17
- // pi-durable's tables and indexes are named `restate_pi_<name>` (its
18
- // storage's `prefix`), apart from the tables of yours in the same database.
19
- import { AsyncLocalStorage } from 'node:async_hooks';
20
- import { deferSql, undeferSql } from '@restatedev/sessions/internal';
21
1
  import { SqliteStorage, } from '@earendil-works/pi-durable/storage/sqlite';
22
- /** The prefix of pi-durable's tables and indexes. */
23
2
  const PREFIX = 'restate_pi_';
24
- /** SQL through `db`: the session, or one transaction's. */
3
+ /** The table of the invocation that last wrote pi's storage after `abortLeft` checked it: one row. */
4
+ const WRITER = 'restate_withpi_writer';
5
+ /** The invocation each attempt's database records as pi's writer, by the database object, and whether it has. */
6
+ const writers = new WeakMap();
7
+ /**
8
+ * From now on, pi's next commit on `db` that writes also records `invocation` as the writer, once. `abortLeft` calls
9
+ * it once it has aborted the runs it found: every run live after that is the invocation's own.
10
+ */
11
+ export function recordWriter(db, invocation) {
12
+ if (!writers.has(db))
13
+ writers.set(db, { invocation, recorded: false });
14
+ }
15
+ /** The invocation that last wrote pi's storage on `db` after checking it, if one did. */
16
+ export async function lastWriter(db) {
17
+ const table = await db.get("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?", [WRITER]);
18
+ if (table === undefined)
19
+ return undefined;
20
+ return (await db.get(`SELECT invocation FROM ${WRITER}`))?.invocation;
21
+ }
25
22
  class Executor {
26
23
  db;
24
+ /** Whether a statement that may write ran through this executor. */
25
+ wrote = false;
27
26
  constructor(db) {
28
27
  this.db = db;
29
28
  }
30
29
  async exec(sql) {
30
+ this.wrote = true;
31
31
  await this.db.execute(sql);
32
32
  }
33
33
  async run(sql, ...params) {
34
+ this.wrote = true;
34
35
  await this.db.execute(sql, params);
35
36
  }
36
- /** `run`, resolving with the rows it changed: SQLite's `changes()`, read next on the same connection. */
37
+ /** `run`, resolving with SQLite's `changes()`, read next on the same connection. */
37
38
  async runCounted(sql, ...params) {
39
+ this.wrote = true;
38
40
  await this.db.execute(sql, params);
39
41
  return (await this.db.get('SELECT changes() AS n')).n;
40
42
  }
@@ -45,364 +47,44 @@ class Executor {
45
47
  return this.db.query(sql, params);
46
48
  }
47
49
  }
48
- /** The savepoint of a pi commit in a batch, and its end. */
49
- const SAVEPOINT = 'SAVEPOINT restate_pi';
50
- const RELEASE = 'RELEASE restate_pi';
51
- /** `tx`, whose `send` is the real one, and tells `sent` when it is called. */
52
- function noting(tx, sent) {
53
- const send = new Proxy(tx.send, {
54
- apply(target, thisArg, args) {
55
- sent();
56
- return Reflect.apply(target, thisArg, args);
57
- },
58
- });
59
- return { ...tx, send };
60
- }
61
- /** The refusal of a call to the adapter from the callback of one of its transactions, which would wait for itself. */
62
- const REFUSED = 'inside a transaction, use the transaction handle its callback gets';
63
- /**
64
- * The `SqliteDatabase` facade over the session `db`.
65
- *
66
- * With the default `commits: 'each'`, each `transaction(callback)` of pi is
67
- * one transaction of the session: one write, one round trip to Restate.
68
- *
69
- * With `'effects'`, while it is {@link armed} and no effect is in flight
70
- * ({@link effectStarted}), the first write opens one transaction of the
71
- * session that stays open, and each pi transaction is a savepoint in it
72
- * (a failed one undoes only itself). {@link flush} ends it: the real commit,
73
- * and then the `afterCommit` of every pi commit in it. It ends also when
74
- * another call to the session queues behind it (a `ctx.db` call of the
75
- * handler): that call then waits for one commit and no more. While an
76
- * effect is in flight, every pi commit is a transaction of its own, as with
77
- * `'each'`. Reads see the open batch. All calls run one at a time on a chain
78
- * of their own.
79
- */
50
+ /** chord's `SqliteDatabase` facade over the session `db`: each pi transaction is a session transaction. */
80
51
  export class SqlKvDatabase extends Executor {
81
52
  #session;
82
- #batching;
83
- #failed;
84
- /** Runs at the end of each commit, in its transaction, so that what it writes commits with it. */
85
- onCommit;
86
- /** Runs once each commit is done, outside its transaction: to tell others what it stored, say. */
87
- afterCommit;
88
- /** Settles when the last call made so far has: the next runs after it. */
89
- #chain = Promise.resolve();
90
- #batch;
91
- #armed = false;
92
- #effects = 0;
93
- /** The `afterCommit` of each commit in the batch, run after its real commit. */
94
- #after = [];
95
- /** The error of the first batch that failed, which every later call fails with. */
96
- #failure;
97
- /** Whether the attempt has been told of it. */
98
- #told = false;
99
- /** Settles when the `afterCommit` callbacks started so far have run. */
100
- #afterRun = Promise.resolve();
101
- /** Set while `afterCommit` callbacks run: a flush from one of them has them to wait for. */
102
- #inAfter = new AsyncLocalStorage();
103
- /** Set inside the callback of a transaction: a call to the adapter from there would wait for itself. */
104
- #inside = new AsyncLocalStorage();
105
- constructor(db, { commits = 'each', failed } = {}) {
53
+ constructor(db) {
106
54
  super(db);
107
55
  this.#session = db;
108
- this.#batching = commits === 'effects';
109
- this.#failed = failed;
110
- }
111
- /**
112
- * Whether commits are batched while no effect is in flight: set while pi
113
- * runs a prompt, and off otherwise. Ending the batch is the caller's
114
- * ({@link flush}). Without `'effects'` it does nothing.
115
- */
116
- set armed(armed) {
117
- this.#armed = armed && this.#batching;
118
- }
119
- /**
120
- * An effect starts: a model call or a tool, which must see what pi stored
121
- * before, and which may store commits of its own. Until the returned
122
- * function is called (once at most; later calls do nothing) every commit
123
- * is written at once. It starts no flush: {@link flush} does. An effect
124
- * whose end is never called leaves the batching off, which is how it was.
125
- */
126
- effectStarted() {
127
- if (!this.#batching)
128
- return () => { };
129
- this.#effects++;
130
- let ended = false;
131
- return () => {
132
- if (ended)
133
- return;
134
- ended = true;
135
- this.#effects--;
136
- };
137
56
  }
138
57
  /**
139
- * Write what the batch holds: its real commit, then what `afterCommit`
140
- * of each of its commits does, in order. Nothing if none is open.
141
- * Rejects if the commit failed (see {@link SqlKvOptions.failed}), and from
142
- * the callback of a transaction, which would wait for itself.
58
+ * Commits when the callback resolves; rolls back, and rejects with its error, when it rejects. A transaction that
59
+ * wrote also records the writer, if `recordWriter` asked for it and it is not recorded yet.
143
60
  */
144
- async flush() {
145
- if (!this.#batching)
146
- return;
147
- if (this.#inside.getStore())
148
- throw new Error(REFUSED);
149
- await this.#run(() => this.#closeBatch());
150
- await this.#runAfter();
151
- }
152
- /** Commits when the callback resolves; rolls back, and rejects with its error, when it rejects. */
153
61
  async transaction(callback) {
154
- if (!this.#batching) {
155
- const result = await this.#session.transaction(async (tx) => {
156
- const done = await callback(new Executor(tx));
157
- if (this.onCommit)
158
- await this.onCommit(tx);
159
- return done;
160
- });
161
- if (this.afterCommit)
162
- await this.afterCommit();
163
- return result;
164
- }
165
- this.#refuseInside();
166
- const after = this.afterCommit;
167
- const { result, batched } = await this.#run(async () => {
168
- if (!this.#writesBatched())
169
- return { result: await this.#commitAlone(callback), batched: false };
170
- return { result: await this.#savepoint(callback, after), batched: true };
171
- });
172
- // Outside the chain: it may publish, which flushes.
173
- if (!batched && after)
174
- await after();
175
- return result;
176
- }
177
- exec(sql) {
178
- return this.#batching ? this.#write((e) => e.exec(sql)) : super.exec(sql);
179
- }
180
- run(sql, ...params) {
181
- return this.#batching ? this.#write((e) => e.run(sql, ...params)) : super.run(sql, ...params);
182
- }
183
- /**
184
- * Outside a transaction, the statement and its count are one: another
185
- * statement between them would be what `changes()` counted.
186
- */
187
- runCounted(sql, ...params) {
188
- if (!this.#batching)
189
- return this.#session.transaction((tx) => new Executor(tx).runCounted(sql, ...params));
190
- return this.#write((e) => e.runCounted(sql, ...params), true);
191
- }
192
- get(sql, ...params) {
193
- return this.#batching ? this.#read((e) => e.get(sql, ...params)) : super.get(sql, ...params);
194
- }
195
- all(sql, ...params) {
196
- return this.#batching ? this.#read((e) => e.all(sql, ...params)) : super.all(sql, ...params);
197
- }
198
- /**
199
- * Writes what the batch holds (a prompt's end has written it already: a batch left now is one of a run that
200
- * was abandoned as the attempt ended or failed), without failing: the error the attempt ends with is the
201
- * attempt's, not this write's, which is told to the attempt and refused to every later call. The session is
202
- * the caller's to end, after the calls made before.
203
- */
204
- async close() {
205
- await this.flush().catch(() => { });
206
- }
207
- /** Whether the next write joins the batch: armed, and no effect in flight. */
208
- #writesBatched() {
209
- return this.#armed && this.#effects === 0;
210
- }
211
- #refuseInside() {
212
- if (this.#inside.getStore())
213
- throw new Error(REFUSED);
214
- }
215
- /** `call`, once the calls before it have ended, unless a batch failed. */
216
- #run(call) {
217
- const result = this.#chain.then(() => {
218
- if (this.#failure) {
219
- this.#tell();
220
- throw this.#failure.error;
221
- }
222
- return call();
223
- });
224
- this.#chain = result.catch(() => { });
225
- return result;
226
- }
227
- /** A read: in the batch if one is open (it sees its writes), else on the session, which opens nothing. */
228
- #read(read) {
229
- this.#refuseInside();
230
- return this.#run(() => read(new Executor(this.#batch ? this.#batch.batch.tx : this.#session)));
231
- }
232
- /** A write outside a transaction: in the batch if batched, else a transaction of its own. */
233
- #write(write, counted = false) {
234
- this.#refuseInside();
235
- return this.#run(async () => {
236
- if (this.#writesBatched()) {
237
- const batch = await this.#opened();
238
- const result = await write(new Executor(batch.tx));
239
- this.#batch.acknowledged = true;
240
- return result;
62
+ const writer = writers.get(this.#session);
63
+ const record = writer !== undefined && !writer.recorded;
64
+ let wrote = false;
65
+ const result = await this.#session.transaction(async (tx) => {
66
+ const executor = new Executor(tx);
67
+ const value = await callback(executor);
68
+ wrote = executor.wrote;
69
+ if (record && wrote) {
70
+ await tx.execute(`CREATE TABLE IF NOT EXISTS ${WRITER} (singleton INTEGER PRIMARY KEY CHECK (singleton = 1), invocation TEXT NOT NULL)`);
71
+ await tx.execute(`INSERT INTO ${WRITER} (singleton, invocation) VALUES (1, ?) ON CONFLICT (singleton) DO UPDATE SET invocation = excluded.invocation`, [writer.invocation]);
241
72
  }
242
- await this.#closeBatch();
243
- // `runCounted` is a transaction of its own, as ever; the others are one statement.
244
- return counted ? this.#session.transaction((tx) => write(new Executor(tx))) : write(new Executor(this.#session));
73
+ return value;
245
74
  });
246
- }
247
- /** One pi transaction that is a transaction of the session: not batched, with its `onCommit` in it. */
248
- async #commitAlone(callback) {
249
- await this.#closeBatch();
250
- return this.#session.transaction((tx) => this.#inside.run(true, async () => {
251
- const done = await callback(new Executor(tx));
252
- if (this.onCommit)
253
- await this.onCommit(tx);
254
- return done;
255
- }));
256
- }
257
- /**
258
- * One pi transaction in the batch, as a savepoint: released when it
259
- * succeeds, rolled back (and released) when it fails, so that a failed
260
- * one undoes only itself. Its `afterCommit` waits for the batch's commit.
261
- */
262
- async #savepoint(callback, after) {
263
- const { tx, run } = await this.#opened();
264
- const held = this.#batch;
265
- // The savepoint goes with the first statement of the commit, and the release that ended the one before it
266
- // (queued below), in the same crossing, in order. A session that cannot defer runs it at once.
267
- const deferred = deferSql(tx, SAVEPOINT);
268
- if (!deferred)
269
- await tx.execute(SAVEPOINT);
270
- // Whether this commit sent something. A send is recorded on the held transaction, and its real commit
271
- // makes it whatever a savepoint did: rolling the savepoint back does not take it back. So a commit that
272
- // sent and then failed cannot be undone alone; the whole batch is (below).
273
- let sent = false;
274
- let result;
275
- try {
276
- // In the async context of the batch's body, so that a call to `ctx.db` from the callback is refused
277
- // at once, as it is in a transaction of the session, and not left to wait for the batch.
278
- result = await run(() => this.#inside.run(true, async () => {
279
- const done = await callback(new Executor(tx));
280
- if (this.onCommit)
281
- await this.onCommit(noting(tx, () => (sent = true)));
282
- return done;
283
- }));
284
- // A savepoint still queued had no statement run in it: with nothing to undo, it and its release are
285
- // no-ops, and neither is run. Otherwise the release rides with the next statement of the batch, or goes
286
- // with the batch's end (see `#closeBatch`).
287
- if (!deferred)
288
- await tx.execute(RELEASE);
289
- else if (!undeferSql(tx, SAVEPOINT) && !deferSql(tx, RELEASE))
290
- await tx.execute(RELEASE);
291
- }
292
- catch (error) {
293
- if (sent) {
294
- await this.#abandon(held, error);
295
- throw error;
296
- }
297
- try {
298
- await tx.execute('ROLLBACK TO restate_pi');
299
- await tx.execute(RELEASE);
300
- }
301
- catch {
302
- // The session failed too: the error of the commit is what the caller gets, and the next call fails.
303
- }
304
- throw error;
305
- }
306
- held.acknowledged = true;
307
- if (after)
308
- this.#after.push(after);
75
+ if (record && wrote)
76
+ writer.recorded = true;
309
77
  return result;
310
78
  }
311
- /**
312
- * Drop the batch with `error`: its body throws, the session rolls the whole transaction back, and its sends
313
- * are never made. If the commits in it were told to pi as done, they are lost, as with a failed write.
314
- */
315
- async #abandon(held, error) {
316
- this.#batch = undefined;
317
- held.batch.abort(error);
318
- await held.done.catch(() => { });
319
- if (held.acknowledged)
320
- this.#fail(error);
321
- }
322
- /** The open batch, opened if there is none. */
323
- async #opened() {
324
- if (this.#batch)
325
- return this.#batch.batch;
326
- const opened = Promise.withResolvers();
327
- const released = Promise.withResolvers();
328
- const done = this.#session.transaction(async (tx) => {
329
- opened.resolve({ tx, run: AsyncLocalStorage.snapshot(), release: released.resolve, abort: released.reject });
330
- await released.promise;
331
- });
332
- // A failure before the body ran: the open itself.
333
- done.catch((error) => opened.reject(error));
334
- // A batch that could not be opened lost nothing: the call that asked for it fails, as one does without
335
- // batching (a cancellation owed to the handler is delivered once, say), and the next one tries again.
336
- const batch = await opened.promise;
337
- const held = { batch, done, acknowledged: false };
338
- // Failing while it waits to be ended, with something told as done, is every later call's error.
339
- done.catch((error) => (held.acknowledged ? this.#fail(error, false) : undefined));
340
- this.#batch = held;
341
- // Another call to the session queued behind it: end it, after the call of pi under way.
342
- // A failure is the attempt's already (`#fail`): nobody waits for this flush.
343
- void batch.tx.queued.then(() => (this.#batch === held ? this.flush().catch(() => { }) : undefined));
344
- return batch;
345
- }
346
- /** End the batch, if one is open: its commit, which the calls after it wait for. */
347
- async #closeBatch() {
348
- const held = this.#batch;
349
- if (!held)
350
- return;
351
- this.#batch = undefined;
352
- // The release of the last commit's savepoint, if nothing ran after it, is not run: the commit ends every
353
- // savepoint of its transaction.
354
- undeferSql(held.batch.tx, RELEASE);
355
- held.batch.release();
356
- try {
357
- await held.done;
358
- }
359
- catch (error) {
360
- // What was told to pi as done is lost; if nothing was, this fails as the commit it was would have.
361
- if (!held.acknowledged)
362
- throw error;
363
- this.#after = [];
364
- throw this.#fail(error);
365
- }
366
- }
367
- /**
368
- * Run what the commits of the last batch asked to run after it, in order,
369
- * after what an earlier flush is running: a flush that returns has had
370
- * all of those done, so that effects that start together start after the
371
- * publish that they all wait for, in the order they asked. A callback that
372
- * flushes (a publish does) does not wait for itself.
373
- */
374
- #runAfter() {
375
- if (this.#inAfter.getStore())
376
- return Promise.resolve();
377
- const callbacks = this.#after.splice(0);
378
- const run = this.#afterRun.then(() => this.#inAfter.run(true, async () => {
379
- for (const after of callbacks)
380
- await after();
381
- }));
382
- this.#afterRun = run.catch(() => { });
383
- return run;
384
- }
385
- /**
386
- * A batch failed: the first error is every later call's, and the attempt is told once, unless `tell` is
387
- * false: a failure that nobody has run into yet (the session closing as the attempt ends is one) is told
388
- * when the next call meets it, not before, so that the error the attempt ends with is its own.
389
- */
390
- #fail(error, tell = true) {
391
- this.#failure ??= { error };
392
- this.#after = [];
393
- if (tell)
394
- this.#tell();
395
- return this.#failure.error;
396
- }
397
- #tell() {
398
- if (this.#told || !this.#failure)
399
- return;
400
- this.#told = true;
401
- this.#failed?.(this.#failure.error);
79
+ /** Runs the statement and its count together: a statement between them would change what `changes()` counts. */
80
+ runCounted(sql, ...params) {
81
+ return this.transaction((tx) => tx.runCounted(sql, ...params));
402
82
  }
83
+ /** The session is the caller's to end, after the calls made before. */
84
+ async close() { }
403
85
  }
404
- /** Open pi-durable storage on the session `db`, through `database`, its facade, if given (to set its `onCommit`). */
405
- export function openSqlKvStorage(db, database = new SqlKvDatabase(db)) {
406
- return SqliteStorage.open(database, { prefix: PREFIX });
86
+ /** Opens pi-durable storage on the session `db`. */
87
+ export function openSqlKvStorage(db) {
88
+ return SqliteStorage.open(new SqlKvDatabase(db), { prefix: PREFIX });
407
89
  }
408
90
  //# sourceMappingURL=storage.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/pi/storage.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,0EAA0E;AAC1E,qDAAqD;AACrD,uBAAuB;AACvB,EAAE;AACF,0EAA0E;AAC1E,4EAA4E;AAC5E,sEAAsE;AACtE,eAAe;AACf,EAAE;AACF,8EAA8E;AAC9E,6EAA6E;AAC7E,2EAA2E;AAC3E,wEAAwE;AACxE,yBAAyB;AACzB,EAAE;AACF,qEAAqE;AACrE,4EAA4E;AAC5E,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EACL,aAAa,GAId,MAAM,2CAA2C,CAAC;AAEnD,qDAAqD;AACrD,MAAM,MAAM,GAAG,aAAa,CAAC;AAE7B,2DAA2D;AAC3D,MAAM,QAAQ;IACH,EAAE,CAAiB;IAC5B,YAAY,EAAkB;QAC5B,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACf,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,GAAW;QACpB,MAAM,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IACD,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,GAAG,MAAqB;QAC7C,MAAM,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IACrC,CAAC;IACD,yGAAyG;IACzG,KAAK,CAAC,UAAU,CAAC,GAAW,EAAE,GAAG,MAAqB;QACpD,MAAM,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACnC,OAAO,CAAC,MAAM,IAAI,CAAC,EAAE,CA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AC,QAAQ,GAAG,IAAI,CAAC;QAC5C,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,gHAAgH;IACvG,UAAU,CAAC,GAAW,EAAE,GAAG,MAAqB;QACvD,OAAO,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,CAAE,EAAe,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC;IAC/E,CAAC;IAED,uEAAuE;IACvE,KAAK,CAAC,KAAK,KAAI,CAAC;CACjB;AAED,oDAAoD;AACpD,MAAM,UAAU,gBAAgB,CAAC,EAAY;IAC3C,OAAO,aAAa,CAAC,IAAI,CAAC,IAAI,aAAa,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;AACvE,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { RegistryReader, ToolRegistration } from '@earendil-works/pi-durable';
2
+ import type * as restate from '@restatedev/sessions';
3
+ /**
4
+ * Wraps `registry` so that each tool keeps the attempt of `ctx` alive while it
5
+ * runs and gets the stop signal in its context:
6
+ *
7
+ * await Harness.open(storage, { models: keptModels(ctx, models), registry: keptTools(ctx, registry) }, context);
8
+ *
9
+ * The registry may keep changing: each snapshot is wrapped as it is read.
10
+ */
11
+ export declare function keptTools<Tool extends ToolRegistration>(ctx: restate.SessionContext, registry: RegistryReader<Tool>): RegistryReader<Tool>;
12
+ //# sourceMappingURL=tools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/pi/tools.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAa,cAAc,EAAoB,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAChH,OAAO,KAAK,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAErD;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,IAAI,SAAS,gBAAgB,EACrD,GAAG,EAAE,OAAO,CAAC,cAAc,EAC3B,QAAQ,EAAE,cAAc,CAAC,IAAI,CAAC,GAC7B,cAAc,CAAC,IAAI,CAAC,CAoDtB"}
@@ -0,0 +1,70 @@
1
+ // Wraps pi's tool registry (`Harness.open({ registry })`) so that each tool call
2
+ // runs in a `ctx.stoppable` scope and gets the stop signal in
3
+ // `context.abortSignal`. The tool's `execute` runs inside the scope, so a
4
+ // `ctx.suspendable` in it finds the scope through the async context. The
5
+ // Harness reads the registry through this wrapper, so tools of any extension,
6
+ // installed at any time, are covered without patching pi-durable. A wrapper an
7
+ // extension declares (`wrapTool`) wraps the kept tool, so its code before and
8
+ // after the tool runs outside the scope. On a cancellation pi records a
9
+ // stopped call as failed, and the next model request, aborted as it starts,
10
+ // ends the run. A tool that ignores the signal runs to its end.
11
+ import { withAbortSignal } from '@earendil-works/chord/context';
12
+ /**
13
+ * Wraps `registry` so that each tool keeps the attempt of `ctx` alive while it
14
+ * runs and gets the stop signal in its context:
15
+ *
16
+ * await Harness.open(storage, { models: keptModels(ctx, models), registry: keptTools(ctx, registry) }, context);
17
+ *
18
+ * The registry may keep changing: each snapshot is wrapped as it is read.
19
+ */
20
+ export function keptTools(ctx, registry) {
21
+ // Wrap each object once: the Harness reads snapshots often and may compare their parts by identity.
22
+ const snapshots = new WeakMap();
23
+ const extensions = new WeakMap();
24
+ const tools = new WeakMap();
25
+ const keptTool = (tool) => {
26
+ let kept = tools.get(tool);
27
+ if (kept === undefined) {
28
+ kept = {
29
+ ...tool,
30
+ execute: (args, api, context) => ctx.stoppable((signal) => tool.execute(args, api, withAbortSignal(signal, context))),
31
+ };
32
+ tools.set(tool, kept);
33
+ }
34
+ return kept;
35
+ };
36
+ const keptExtension = (extension) => {
37
+ let kept = extensions.get(extension);
38
+ if (kept === undefined) {
39
+ kept = extension.tools === undefined ? extension : { ...extension, tools: extension.tools.map(keptTool) };
40
+ extensions.set(extension, kept);
41
+ }
42
+ return kept;
43
+ };
44
+ const keptSnapshot = (snapshot) => {
45
+ let kept = snapshots.get(snapshot);
46
+ if (kept === undefined) {
47
+ const installed = snapshot.installed().map(keptExtension);
48
+ kept = {
49
+ installed: () => installed,
50
+ extension: (name) => {
51
+ const extension = snapshot.extension(name);
52
+ return extension === undefined ? undefined : keptExtension(extension);
53
+ },
54
+ tools: () => snapshot
55
+ .tools()
56
+ .map(({ extension, tool }) => ({ extension: keptExtension(extension), tool: keptTool(tool) })),
57
+ sections: () => snapshot.sections().map(({ extension, section }) => ({ extension: keptExtension(extension), section })),
58
+ tasks: () => snapshot.tasks(),
59
+ task: (name) => snapshot.task(name),
60
+ };
61
+ snapshots.set(snapshot, kept);
62
+ }
63
+ return kept;
64
+ };
65
+ return {
66
+ snapshot: () => keptSnapshot(registry.snapshot()),
67
+ subscribe: (listener) => registry.subscribe(listener),
68
+ };
69
+ }
70
+ //# sourceMappingURL=tools.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/pi/tools.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,8DAA8D;AAC9D,0EAA0E;AAC1E,yEAAyE;AACzE,8EAA8E;AAC9E,+EAA+E;AAC/E,8EAA8E;AAC9E,wEAAwE;AACxE,4EAA4E;AAC5E,gEAAgE;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAIhE;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CACvB,GAA2B,EAC3B,QAA8B;IAE9B,oGAAoG;IACpG,MAAM,SAAS,GAAG,IAAI,OAAO,EAAkD,CAAC;IAChF,MAAM,UAAU,GAAG,IAAI,OAAO,EAAoC,CAAC;IACnE,MAAM,KAAK,GAAG,IAAI,OAAO,EAAc,CAAC;IACxC,MAAM,QAAQ,GAAG,CAAC,IAAU,EAAQ,EAAE;QACpC,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC3B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,IAAI,GAAG;gBACL,GAAG,IAAI;gBACP,OAA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