@restatedev/agents 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -53
- package/dist/calls.d.ts +29 -5
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +77 -10
- package/dist/calls.js.map +1 -1
- package/dist/claude/index.d.ts +36 -7
- package/dist/claude/index.d.ts.map +1 -1
- package/dist/claude/index.js +232 -151
- package/dist/claude/index.js.map +1 -1
- package/dist/client.d.ts +12 -3
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/common.d.ts +151 -70
- package/dist/common.d.ts.map +1 -1
- package/dist/common.js +246 -204
- package/dist/common.js.map +1 -1
- package/dist/conversation.d.ts +478 -0
- package/dist/conversation.d.ts.map +1 -0
- package/dist/conversation.js +1256 -0
- package/dist/conversation.js.map +1 -0
- package/dist/deepseek/index.d.ts +38 -5
- package/dist/deepseek/index.d.ts.map +1 -1
- package/dist/deepseek/index.js +322 -106
- package/dist/deepseek/index.js.map +1 -1
- package/dist/deepseek/persistence.d.ts +9 -3
- package/dist/deepseek/persistence.d.ts.map +1 -1
- package/dist/deepseek/persistence.js +12 -6
- package/dist/deepseek/persistence.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/langgraph/checkpointer.d.ts +16 -2
- package/dist/langgraph/checkpointer.d.ts.map +1 -1
- package/dist/langgraph/checkpointer.js +133 -26
- package/dist/langgraph/checkpointer.js.map +1 -1
- package/dist/langgraph/context.d.ts +10 -0
- package/dist/langgraph/context.d.ts.map +1 -0
- package/dist/langgraph/context.js +21 -0
- package/dist/langgraph/context.js.map +1 -0
- package/dist/langgraph/errors.d.ts +16 -0
- package/dist/langgraph/errors.d.ts.map +1 -0
- package/dist/langgraph/errors.js +30 -0
- package/dist/langgraph/errors.js.map +1 -0
- package/dist/langgraph/graph.d.ts +25 -0
- package/dist/langgraph/graph.d.ts.map +1 -0
- package/dist/langgraph/graph.js +346 -0
- package/dist/langgraph/graph.js.map +1 -0
- package/dist/langgraph/index.d.ts +65 -12
- package/dist/langgraph/index.d.ts.map +1 -1
- package/dist/langgraph/index.js +227 -171
- package/dist/langgraph/index.js.map +1 -1
- package/dist/langgraph/nodes.d.ts +70 -0
- package/dist/langgraph/nodes.d.ts.map +1 -0
- package/dist/langgraph/nodes.js +145 -0
- package/dist/langgraph/nodes.js.map +1 -0
- package/dist/messages.d.ts +18 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +182 -0
- package/dist/messages.js.map +1 -0
- package/dist/object.d.ts +65 -23
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +123 -35
- package/dist/object.js.map +1 -1
- package/dist/opencode/db.d.ts +51 -10
- package/dist/opencode/db.d.ts.map +1 -1
- package/dist/opencode/db.js +122 -34
- package/dist/opencode/db.js.map +1 -1
- package/dist/opencode/index.d.ts +105 -21
- package/dist/opencode/index.d.ts.map +1 -1
- package/dist/opencode/index.js +827 -279
- package/dist/opencode/index.js.map +1 -1
- package/dist/output.d.ts +15 -7
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +44 -26
- package/dist/output.js.map +1 -1
- package/dist/pi/index.d.ts +39 -11
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +281 -77
- package/dist/pi/index.js.map +1 -1
- package/dist/pi/storage.d.ts +29 -4
- package/dist/pi/storage.d.ts.map +1 -1
- package/dist/pi/storage.js +18 -6
- package/dist/pi/storage.js.map +1 -1
- package/dist/signals.d.ts +74 -4
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +144 -15
- package/dist/signals.js.map +1 -1
- package/dist/steering.d.ts +31 -0
- package/dist/steering.d.ts.map +1 -0
- package/dist/steering.js +45 -0
- package/dist/steering.js.map +1 -0
- package/dist/tools.d.ts +28 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +34 -5
- package/dist/tools.js.map +1 -1
- package/package.json +42 -13
- package/src/calls.ts +107 -15
- package/src/claude/README.md +41 -20
- package/src/claude/index.ts +266 -178
- package/src/client.ts +14 -3
- package/src/common.ts +317 -232
- package/src/conversation.ts +1547 -0
- package/src/deepseek/README.md +78 -11
- package/src/deepseek/index.ts +384 -137
- package/src/deepseek/persistence.ts +11 -8
- package/src/index.ts +13 -4
- package/src/langgraph/README.md +115 -25
- package/src/langgraph/checkpointer.ts +168 -28
- package/src/langgraph/context.ts +27 -0
- package/src/langgraph/errors.ts +26 -0
- package/src/langgraph/graph.ts +396 -0
- package/src/langgraph/index.ts +326 -181
- package/src/langgraph/nodes.ts +243 -0
- package/src/messages.ts +244 -0
- package/src/object.ts +169 -38
- package/src/opencode/README.md +172 -66
- package/src/opencode/db.ts +204 -40
- package/src/opencode/index.ts +1010 -325
- package/src/output.ts +52 -31
- package/src/pi/README.md +18 -3
- package/src/pi/index.ts +346 -78
- package/src/pi/storage.ts +20 -7
- package/src/signals.ts +225 -19
- package/src/steering.ts +67 -0
- package/src/tools.ts +61 -8
- package/dist/claude/conversation.d.ts +0 -33
- package/dist/claude/conversation.d.ts.map +0 -1
- package/dist/claude/conversation.js +0 -95
- package/dist/claude/conversation.js.map +0 -1
- package/dist/log.d.ts +0 -29
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -146
- package/dist/log.js.map +0 -1
- package/src/claude/conversation.ts +0 -114
- package/src/log.ts +0 -194
package/src/opencode/db.ts
CHANGED
|
@@ -1,28 +1,42 @@
|
|
|
1
1
|
// OpenCode on the object's database: `openOpenCode(db, fence, options)` boots
|
|
2
|
-
// OpenCode 2.
|
|
3
|
-
//
|
|
2
|
+
// OpenCode 2.2's embedded host (@opencode/sdk) with its database in a SQL
|
|
3
|
+
// session, `ctx.db`.
|
|
4
4
|
//
|
|
5
5
|
// The host is OpenCode's workerd profile, the one it boots in a Cloudflare
|
|
6
|
-
// Durable Object: no local filesystem, shell, terminal or version
|
|
7
|
-
// config given inline, plugins given in code. Only its database
|
|
8
|
-
// workerd profile reads a Durable Object's SQLite, whose `exec`
|
|
9
|
-
// synchronous; here OpenCode gets an Effect SqlClient instead, which is
|
|
10
|
-
// asynchronous, through `Database.configuredClient` (OpenCode's), the seam
|
|
11
|
-
// for an injected client. Each transaction is an SQL
|
|
12
|
-
// session runs statements one at a time, as OpenCode's
|
|
6
|
+
// Durable Object: no local filesystem, shell, terminal, npm or version
|
|
7
|
+
// control, config given inline, plugins given in code. Only its database
|
|
8
|
+
// differs. The workerd profile reads a Durable Object's SQLite, whose `exec`
|
|
9
|
+
// is synchronous; here OpenCode gets an Effect SqlClient instead, which is
|
|
10
|
+
// asynchronous, through `Database.configuredClient` (OpenCode's), the seam
|
|
11
|
+
// OpenCode keeps for an injected client. Each transaction is an SQL
|
|
12
|
+
// transaction, and the session runs statements one at a time, as OpenCode's
|
|
13
|
+
// own clients do.
|
|
13
14
|
//
|
|
14
15
|
// `fence.closed` refuses every statement from then on: an attempt that is
|
|
15
16
|
// giving up stores nothing more, as Restate fences an attempt that a newer
|
|
16
17
|
// one replaced.
|
|
17
|
-
|
|
18
|
+
//
|
|
19
|
+
// At boot, OpenCode sweeps the turns a previous process left claimed
|
|
20
|
+
// (`SessionRestart`): `restart` says what this boot does with them (see
|
|
21
|
+
// {@link Restart}), and `Host.swept` settles once the sweep is done.
|
|
22
|
+
//
|
|
23
|
+
// Step 0 of docs/opencode.md (the spike) found its seams; `./opencode/db` exports it.
|
|
24
|
+
import { Context, Deferred, Effect, Exit, Layer, Option, Scope } from 'effect';
|
|
18
25
|
import { Reactivity } from 'effect/reactivity';
|
|
19
26
|
import { SqlClient, Statement } from 'effect/sql';
|
|
20
27
|
import { SqlError, UnknownError } from 'effect/sql/SqlError';
|
|
21
28
|
import type { Connection } from 'effect/sql/SqlConnection';
|
|
29
|
+
import { Bus } from '@opencode/core/bus';
|
|
22
30
|
import { Database as OpenCodeDatabase } from '@opencode/core/database/database';
|
|
31
|
+
import { fromPromise } from '@opencode/core/plugin/promise';
|
|
32
|
+
import { SessionEvent } from '@opencode/core/session/event';
|
|
33
|
+
import { SessionExecution } from '@opencode/core/session/execution';
|
|
23
34
|
import { SessionRestart } from '@opencode/core/session/execution/restart';
|
|
35
|
+
import { SessionStore } from '@opencode/core/session/store';
|
|
36
|
+
import { ConfigPluginSource } from '@opencode/core/config/plugin/source';
|
|
37
|
+
import { Pty } from '@opencode/core/pty';
|
|
38
|
+
import { Snapshot } from '@opencode/core/snapshot';
|
|
24
39
|
import { ServerWorkerd } from '@opencode/server/workerd';
|
|
25
|
-
import { fromPromise } from '@opencode/core/plugin/promise';
|
|
26
40
|
import { OpenCode } from '@opencode/sdk/effect';
|
|
27
41
|
import type { Plugin } from '@opencode/plugin';
|
|
28
42
|
import type { DatabaseWriter, Database, SqlValue } from '@restatedev/sessions';
|
|
@@ -46,18 +60,24 @@ const InTransaction = Context.Service<SqlClient.TransactionConnection, SqlClient
|
|
|
46
60
|
'@restatedev/agents/OpenCodeTransaction',
|
|
47
61
|
);
|
|
48
62
|
|
|
63
|
+
/** A statement that writes. */
|
|
64
|
+
const WRITE = /^\s*(INSERT|UPDATE|DELETE|REPLACE)\b/i;
|
|
65
|
+
|
|
49
66
|
/**
|
|
50
67
|
* A connection whose statements go to `target`: the session, which runs one
|
|
51
68
|
* call at a time and holds them while a transaction runs, or that
|
|
52
|
-
* transaction's database. Each is refused once `fence` closes
|
|
69
|
+
* transaction's database. Each is refused once `fence` closes; `wrote` is
|
|
70
|
+
* told of each statement that writes.
|
|
53
71
|
*/
|
|
54
|
-
function connection(target: DatabaseWriter, fence: Fence): Connection {
|
|
72
|
+
function connection(target: DatabaseWriter, fence: Fence, wrote: () => void = () => {}): Connection {
|
|
55
73
|
// Effect binds what the compiler made of OpenCode's values: SQL values.
|
|
56
74
|
const exec = <T>(read: (sql: string, params: SqlValue[]) => Promise<T>, sql: string, params: readonly unknown[]) =>
|
|
57
75
|
Effect.tryPromise({
|
|
58
76
|
try: async () => {
|
|
59
77
|
if (fence.closed) throw new Error('the attempt is over');
|
|
60
|
-
|
|
78
|
+
const result = await read(sql, params as SqlValue[]);
|
|
79
|
+
if (WRITE.test(sql)) wrote();
|
|
80
|
+
return result;
|
|
61
81
|
},
|
|
62
82
|
catch: (cause) => error('execute', cause),
|
|
63
83
|
});
|
|
@@ -77,8 +97,16 @@ function connection(target: DatabaseWriter, fence: Fence): Connection {
|
|
|
77
97
|
};
|
|
78
98
|
}
|
|
79
99
|
|
|
80
|
-
/**
|
|
81
|
-
|
|
100
|
+
/**
|
|
101
|
+
* An Effect SqlClient over `db`, with its statements refused once `fence`
|
|
102
|
+
* closes. `committed` runs after each write is committed: a transaction that
|
|
103
|
+
* wrote, or a statement that writes outside one.
|
|
104
|
+
*/
|
|
105
|
+
export function sqlClient(
|
|
106
|
+
db: Database,
|
|
107
|
+
fence: Fence,
|
|
108
|
+
committed: () => void = () => {},
|
|
109
|
+
): Layer.Layer<SqlClient.SqlClient> {
|
|
82
110
|
// `effect` as one SQL transaction: it commits when `effect` succeeds
|
|
83
111
|
// and rolls back when it fails. Its statements go to the transaction's
|
|
84
112
|
// database.
|
|
@@ -91,6 +119,7 @@ export function sqlClient(db: Database, fence: Fence): Layer.Layer<SqlClient.Sql
|
|
|
91
119
|
const rollback = new Error('rollback');
|
|
92
120
|
let interrupted = false;
|
|
93
121
|
let resumed = false;
|
|
122
|
+
let wrote = false;
|
|
94
123
|
// Settles when the transaction has committed, or rejects: with
|
|
95
124
|
// `rollback` once `effect` failed, or with the error of BEGIN or COMMIT.
|
|
96
125
|
const ended: Promise<void> = db.transaction(
|
|
@@ -99,7 +128,7 @@ export function sqlClient(db: Database, fence: Fence): Layer.Layer<SqlClient.Sql
|
|
|
99
128
|
if (interrupted) return reject(rollback);
|
|
100
129
|
const inner = Effect.provideContext(
|
|
101
130
|
effect,
|
|
102
|
-
Context.add(fiber.context, InTransaction, [connection(tx, fence), 0]),
|
|
131
|
+
Context.add(fiber.context, InTransaction, [connection(tx, fence, () => (wrote = true)), 0]),
|
|
103
132
|
);
|
|
104
133
|
resumed = true;
|
|
105
134
|
resume(
|
|
@@ -123,6 +152,10 @@ export function sqlClient(db: Database, fence: Fence): Layer.Layer<SqlClient.Sql
|
|
|
123
152
|
);
|
|
124
153
|
}),
|
|
125
154
|
);
|
|
155
|
+
ended.then(
|
|
156
|
+
() => (wrote ? committed() : undefined),
|
|
157
|
+
() => undefined,
|
|
158
|
+
);
|
|
126
159
|
const settled = ended.catch((cause: unknown) => {
|
|
127
160
|
// BEGIN failed: `effect` never ran.
|
|
128
161
|
if (!resumed && cause !== rollback) resume(Effect.fail(error('transaction', cause)));
|
|
@@ -136,8 +169,10 @@ export function sqlClient(db: Database, fence: Fence): Layer.Layer<SqlClient.Sql
|
|
|
136
169
|
|
|
137
170
|
const make = Effect.gen(function* () {
|
|
138
171
|
const client = yield* SqlClient.make({
|
|
139
|
-
|
|
172
|
+
// Outside a transaction, a statement commits as it runs.
|
|
173
|
+
acquirer: Effect.succeed(connection(db, fence, committed)),
|
|
140
174
|
compiler: Statement.makeCompilerSqlite(),
|
|
175
|
+
driver: 'sqlite-restate',
|
|
141
176
|
spanAttributes: [['db.system.name', 'sqlite']],
|
|
142
177
|
transactionService: InTransaction,
|
|
143
178
|
});
|
|
@@ -146,6 +181,34 @@ export function sqlClient(db: Database, fence: Fence): Layer.Layer<SqlClient.Sql
|
|
|
146
181
|
return Layer.effect(SqlClient.SqlClient, make).pipe(Layer.provide(Reactivity.layer));
|
|
147
182
|
}
|
|
148
183
|
|
|
184
|
+
/**
|
|
185
|
+
* What a boot does with the turns a previous process left claimed (OpenCode's
|
|
186
|
+
* execution claims), before any prompt of its own:
|
|
187
|
+
*
|
|
188
|
+
* - `resume`: OpenCode's own restart, however often the turn was resumed
|
|
189
|
+
* before (Restate's retry policy decides): a synthetic "Continue" message,
|
|
190
|
+
* its running tool calls failed, and a new step. A crashed attempt's turn
|
|
191
|
+
* goes on.
|
|
192
|
+
* - `close`: each is closed without asking the model: an idle marker
|
|
193
|
+
* (`Execution.Failed`) and the claim released. A killed invocation's turn
|
|
194
|
+
* is over. How, `closer` says.
|
|
195
|
+
* - `none`: nothing; the claims stay. A reader beside a running turn.
|
|
196
|
+
*/
|
|
197
|
+
export type Restart = 'resume' | 'close' | 'none';
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* How `restart: 'close'` closes a claimed turn:
|
|
201
|
+
*
|
|
202
|
+
* - `own`: a `SessionRestart` of ours. Each streaming or running tool call
|
|
203
|
+
* fails with `cutOff(toolCallId, name)` as its result, then the turn with
|
|
204
|
+
* `ended`, the claim released in the same transaction.
|
|
205
|
+
* - `budget`: OpenCode's own restart with no resumes left
|
|
206
|
+
* (`maxAttempts: 0`): the idle marker with OpenCode's text, the claim
|
|
207
|
+
* released; the running tool calls stay running until the next drain fails
|
|
208
|
+
* them ("Tool execution interrupted").
|
|
209
|
+
*/
|
|
210
|
+
export type Closer = 'own' | 'budget';
|
|
211
|
+
|
|
149
212
|
export interface OpenOptions {
|
|
150
213
|
/** OpenCode plugins: models, tools, agents. */
|
|
151
214
|
plugins?: Plugin.Plugin[];
|
|
@@ -157,48 +220,148 @@ export interface OpenOptions {
|
|
|
157
220
|
* session's directory. Without it, the workerd profile has none.
|
|
158
221
|
*/
|
|
159
222
|
local?: boolean;
|
|
160
|
-
/**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
223
|
+
/** What this boot does with the turns left claimed (default `resume`). */
|
|
224
|
+
restart?: Restart;
|
|
225
|
+
/** How `close` closes them (default `budget`: the spike found ours buys little over it). */
|
|
226
|
+
closer?: Closer;
|
|
227
|
+
/** The result of a tool call that a closed turn left running (`own` only). */
|
|
228
|
+
cutOff?: (toolCallId: string, name: string) => string;
|
|
229
|
+
/** The error a closed turn ends with (`own` only). */
|
|
230
|
+
ended?: string;
|
|
231
|
+
/** Runs after each of OpenCode's writes is committed: for a copy of what it wrote. */
|
|
232
|
+
committed?: () => void;
|
|
165
233
|
}
|
|
166
234
|
|
|
167
235
|
/** A booted OpenCode: `call` runs one of `client`'s calls (which return Effects); `close` shuts it down. */
|
|
168
236
|
export interface Host {
|
|
169
237
|
client: OpenCode.Interface;
|
|
170
238
|
call<A, E, R>(effect: Effect.Effect<A, E, R>): Promise<A>;
|
|
239
|
+
/**
|
|
240
|
+
* Settles once the boot's sweep of claimed turns is done: closed, for
|
|
241
|
+
* `close`; their resumes started, for `resume`. The host forks the sweep, so
|
|
242
|
+
* a prompt submitted before it settles may join a claimed turn.
|
|
243
|
+
*/
|
|
244
|
+
swept: Promise<void>;
|
|
171
245
|
close(): Promise<void>;
|
|
172
246
|
}
|
|
173
247
|
|
|
248
|
+
/** The result `own` gives a running tool call of a closed turn, by default. */
|
|
249
|
+
const CUT_OFF = 'The turn ended before this call returned: it may or may not have happened.';
|
|
250
|
+
/** The error `own` closes a turn with, by default. */
|
|
251
|
+
const ENDED = 'Ended: the turn was killed before it finished.';
|
|
252
|
+
|
|
174
253
|
/**
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
254
|
+
* A `SessionRestart` that closes each claimed top-level session this process
|
|
255
|
+
* does not run: OpenCode's budget's events (`Execution.Failed`, the release
|
|
256
|
+
* as its commit), and before them a `Tool.Failed` of our result for each
|
|
257
|
+
* streaming or running tool part. No synthetic message, no drain, no model.
|
|
178
258
|
*/
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
259
|
+
const closing = (cutOff: (toolCallId: string, name: string) => string, ended: string) =>
|
|
260
|
+
Layer.effect(
|
|
261
|
+
SessionRestart.Service,
|
|
262
|
+
Effect.gen(function* () {
|
|
263
|
+
const store = yield* SessionStore.Service;
|
|
264
|
+
const execution = yield* SessionExecution.Service;
|
|
265
|
+
const bus = yield* Bus.Service;
|
|
266
|
+
const close = Effect.fnUntraced(function* (sessionID: Parameters<typeof store.release>[0]) {
|
|
267
|
+
for (const message of yield* Effect.orDie(store.context(sessionID))) {
|
|
268
|
+
if (message.type !== 'assistant') continue;
|
|
269
|
+
for (const part of message.content) {
|
|
270
|
+
if (part.type !== 'tool') continue;
|
|
271
|
+
if (part.state.status !== 'streaming' && part.state.status !== 'running') continue;
|
|
272
|
+
yield* bus.publish(SessionEvent.Tool.Failed, {
|
|
273
|
+
sessionID,
|
|
274
|
+
assistantMessageID: message.id,
|
|
275
|
+
id: part.id,
|
|
276
|
+
error: { type: 'aborted', message: cutOff(part.id, part.name) },
|
|
277
|
+
executed: part.executed === true,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
yield* bus.publish(
|
|
282
|
+
SessionEvent.Execution.Failed,
|
|
283
|
+
{ sessionID, error: { type: 'aborted', message: ended } },
|
|
284
|
+
{ commit: () => store.release(sessionID) },
|
|
285
|
+
);
|
|
286
|
+
});
|
|
287
|
+
return SessionRestart.Service.of({
|
|
288
|
+
resumeSuspendedSessions: Effect.gen(function* () {
|
|
289
|
+
const active = yield* execution.active;
|
|
290
|
+
// Child sessions' claims: v1 has no background subagents to recover.
|
|
291
|
+
yield* store.releaseChildClaims([]);
|
|
292
|
+
const claimed = (yield* store.listSuspended()).filter((id) => !active.has(id));
|
|
293
|
+
yield* Effect.forEach(claimed, close, { discard: true });
|
|
294
|
+
}),
|
|
295
|
+
});
|
|
296
|
+
}),
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
/** The sweep's service for `restart` and `closer`. */
|
|
300
|
+
const restarts = (
|
|
301
|
+
restart: Restart,
|
|
302
|
+
closer: Closer,
|
|
303
|
+
cutOff: (toolCallId: string, name: string) => string,
|
|
304
|
+
ended: string,
|
|
305
|
+
): Layer.Layer<SessionRestart.Service, never, unknown> => {
|
|
306
|
+
if (restart === 'none') return Layer.succeed(SessionRestart.Service, { resumeSuspendedSessions: Effect.void });
|
|
307
|
+
if (restart === 'resume') return SessionRestart.layer({ maxAttempts: Infinity });
|
|
308
|
+
return closer === 'budget' ? SessionRestart.layer({ maxAttempts: 0 }) : closing(cutOff, ended);
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
/** `service`, whose sweep completes `done` as it ends, however it ends. */
|
|
312
|
+
const observed = (
|
|
313
|
+
layer: Layer.Layer<SessionRestart.Service, never, unknown>,
|
|
314
|
+
done: Deferred.Deferred<void, unknown>,
|
|
315
|
+
): Layer.Layer<SessionRestart.Service, never, unknown> =>
|
|
316
|
+
Layer.effect(
|
|
317
|
+
SessionRestart.Service,
|
|
318
|
+
Effect.map(SessionRestart.Service, (inner: SessionRestart.Interface) =>
|
|
319
|
+
SessionRestart.Service.of({
|
|
320
|
+
resumeSuspendedSessions: inner.resumeSuspendedSessions.pipe(
|
|
321
|
+
Effect.onExit((exit) => Effect.asVoid(Deferred.done(done, Exit.asVoid(exit)))),
|
|
322
|
+
),
|
|
323
|
+
}),
|
|
324
|
+
),
|
|
325
|
+
).pipe(Layer.provide(layer));
|
|
326
|
+
|
|
327
|
+
/** What a replacement replaces: `LayerNode`'s own record of it (its source node). */
|
|
328
|
+
const sourceOf = (replacement: ReturnType<typeof ServerWorkerd.replacements>[number]): unknown => {
|
|
329
|
+
const key = Object.getOwnPropertySymbols(replacement).find((s) => s.description === 'LayerNode.Replacement');
|
|
330
|
+
return key && (replacement as unknown as Record<symbol, { source: unknown }>)[key]?.source;
|
|
331
|
+
};
|
|
183
332
|
|
|
184
333
|
/**
|
|
185
|
-
* Boot OpenCode with its database in `db
|
|
186
|
-
*
|
|
334
|
+
* Boot OpenCode with its database in `db`; the boot sweeps the turns left
|
|
335
|
+
* claimed as `restart` says. Close the host when done.
|
|
187
336
|
*/
|
|
188
337
|
export async function openOpenCode(
|
|
189
338
|
db: Database,
|
|
190
339
|
fence: Fence,
|
|
191
|
-
{
|
|
340
|
+
{
|
|
341
|
+
plugins = [],
|
|
342
|
+
config = {},
|
|
343
|
+
local = false,
|
|
344
|
+
restart = 'resume',
|
|
345
|
+
closer = 'budget',
|
|
346
|
+
committed,
|
|
347
|
+
cutOff = () => CUT_OFF,
|
|
348
|
+
ended = ENDED,
|
|
349
|
+
}: OpenOptions,
|
|
192
350
|
): Promise<Host> {
|
|
193
351
|
// The workerd profile, but for its database, which comes first. (The
|
|
194
352
|
// Promise API's OpenCode.create takes no replacements; the Effect API's does.)
|
|
195
353
|
const profile = { storage: undefined as never, models: { fetch: false } };
|
|
196
|
-
//
|
|
197
|
-
//
|
|
198
|
-
|
|
199
|
-
const
|
|
200
|
-
|
|
354
|
+
// Taken by the node each replaces, not by position (2.1 added Npm's). Local
|
|
355
|
+
// keeps the stubs of snapshots, the pty and plugin discovery only.
|
|
356
|
+
const kept = local ? new Set<unknown>([Snapshot.node, Pty.node, ConfigPluginSource.node]) : undefined;
|
|
357
|
+
const rest = ServerWorkerd.replacements(profile).filter((r: Parameters<typeof sourceOf>[0]) => {
|
|
358
|
+
const source = sourceOf(r);
|
|
359
|
+
if (source === undefined) throw new Error('OpenCode: a workerd replacement of unknown node');
|
|
360
|
+
return source !== OpenCodeDatabase.node && (kept === undefined || kept.has(source));
|
|
361
|
+
});
|
|
201
362
|
const scope = Scope.makeUnsafe();
|
|
363
|
+
const done = Deferred.makeUnsafe<void, unknown>();
|
|
364
|
+
const sweep = observed(restarts(restart, closer, cutOff, ended), done);
|
|
202
365
|
const client = await Effect.runPromise(
|
|
203
366
|
OpenCode.create(
|
|
204
367
|
{
|
|
@@ -207,9 +370,9 @@ export async function openOpenCode(
|
|
|
207
370
|
},
|
|
208
371
|
{
|
|
209
372
|
overrides: [
|
|
210
|
-
OpenCodeDatabase.node.replace(OpenCodeDatabase.configuredClient(sqlClient(db, fence))),
|
|
373
|
+
OpenCodeDatabase.node.replace(OpenCodeDatabase.configuredClient(sqlClient(db, fence, committed))),
|
|
211
374
|
...rest,
|
|
212
|
-
SessionRestart.node.replace(SessionRestart.node.mapLayer(() =>
|
|
375
|
+
SessionRestart.node.replace(SessionRestart.node.mapLayer(() => sweep as never)),
|
|
213
376
|
],
|
|
214
377
|
},
|
|
215
378
|
).pipe(Scope.provide(scope)),
|
|
@@ -218,6 +381,7 @@ export async function openOpenCode(
|
|
|
218
381
|
client,
|
|
219
382
|
// The client's calls need nothing more: OpenCode provided it all.
|
|
220
383
|
call: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.runPromise(effect as Effect.Effect<A, E>),
|
|
384
|
+
swept: Effect.runPromise(Deferred.await(done)),
|
|
221
385
|
close: () => Effect.runPromise(Scope.close(scope, Exit.void)),
|
|
222
386
|
};
|
|
223
387
|
}
|