@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/dist/object.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Virtual objects set up for SQL and agents.
|
|
2
2
|
import * as restate from '@restatedev/sessions';
|
|
3
3
|
import { reset } from '@restatedev/sessions/internal';
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
4
|
+
import { BUDGET, chatOf, chatTurn, checkWindow, isAgent, ITEMS, messageSerde, publish, refusalOf, WINDOW, } from './common.js';
|
|
5
|
+
import { compact, nextStart, restart, SCHEMA, settle, steerStored } from './conversation.js';
|
|
6
6
|
import { signals, signalsOf, withCompanion, } from './signals.js';
|
|
7
7
|
/**
|
|
8
8
|
* The options of every object here, beside a session object's: retries for
|
|
@@ -45,7 +45,7 @@ const WAITS = { inactivityTimeout: { minutes: 5 } };
|
|
|
45
45
|
* waits, `status`, what the object is doing, `stop`, which stops the message
|
|
46
46
|
* it answers, and `notifications` and `watch`, the revision of its
|
|
47
47
|
* conversation, and a wait for a newer one (an agent's
|
|
48
|
-
* has `clear` and `sync` too: {@link
|
|
48
|
+
* has `clear` and `sync` too: {@link FOLLOWED}). Each asks the object's
|
|
49
49
|
* `signals` companion, which knows what the object does: they run while the
|
|
50
50
|
* object's handler works.
|
|
51
51
|
*/
|
|
@@ -70,19 +70,21 @@ const BUILT_IN = {
|
|
|
70
70
|
};
|
|
71
71
|
/**
|
|
72
72
|
* The views that agents (an object with a `messages` view) have beside
|
|
73
|
-
* those of every object: `clear`, which forgets the conversation;
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* once
|
|
73
|
+
* those of every object: `clear`, which forgets the conversation; `steer`,
|
|
74
|
+
* which sends a message while a turn is answered (for an agent whose `chat`
|
|
75
|
+
* a framework's `chat()` made: `chatHandler`'s, which answers it as a turn
|
|
76
|
+
* of its own if no turn takes it); and `sync`, the conversation since
|
|
77
|
+
* `cursor` (by default, all of it), what the agent is doing, and the
|
|
78
|
+
* revision, in one request; with `revision`, once there is a newer one, or
|
|
79
|
+
* at the timeout. A client follows the conversation by sending each
|
|
80
|
+
* answer's `revision` and `cursor` back, at once while it has `more`.
|
|
79
81
|
*/
|
|
80
|
-
const followed = (
|
|
82
|
+
const followed = (steerable) => ({
|
|
81
83
|
/**
|
|
82
84
|
* Forget the conversation: stop the message being answered (as `stop`),
|
|
83
85
|
* then empty the key's database, and what its `signals` hold (waits and
|
|
84
|
-
* kept signals). The
|
|
85
|
-
*
|
|
86
|
+
* kept signals). The conversation's ids and cursors go on (see
|
|
87
|
+
* conversation.ts), so a follower reads on, and drops the messages before `first`.
|
|
86
88
|
* A message sent before it is answered first, then forgotten too; one sent
|
|
87
89
|
* after starts anew.
|
|
88
90
|
*/
|
|
@@ -100,43 +102,113 @@ const followed = (messages) => ({
|
|
|
100
102
|
return stop;
|
|
101
103
|
},
|
|
102
104
|
},
|
|
105
|
+
/**
|
|
106
|
+
* Send a message to the agent while it answers another: the turn being
|
|
107
|
+
* answered takes it at its next step (once the model's tool calls have
|
|
108
|
+
* their results), if its framework takes steered messages and it reaches
|
|
109
|
+
* one; otherwise the message is answered as a turn of its own, after the
|
|
110
|
+
* messages before it, in the order it was sent. With no message being
|
|
111
|
+
* answered, it starts a turn. Either way it is answered once: send it with
|
|
112
|
+
* an idempotency key, so that a retried request is the same message. It
|
|
113
|
+
* takes the message as plain text or as a JSON string, as `chat` does.
|
|
114
|
+
*/
|
|
115
|
+
steer: {
|
|
116
|
+
input: messageSerde,
|
|
117
|
+
accept: '*/*',
|
|
118
|
+
handler: async (ctx, text) => {
|
|
119
|
+
if (!steerable) {
|
|
120
|
+
throw new restate.TerminalError(`${ctx.request.target.service} takes no steered messages: its \`chat\` handler is no framework's \`chat()\` (chatHandler)`, { errorCode: 400 });
|
|
121
|
+
}
|
|
122
|
+
return companion(ctx, 'steer').steer({ id: ctx.request.id, text });
|
|
123
|
+
},
|
|
124
|
+
},
|
|
103
125
|
sync: {
|
|
104
126
|
...WAITS,
|
|
105
|
-
handler: async (ctx, { cursor = 0, revision, timeoutSeconds = 50, limit = 100 } = {}) => {
|
|
127
|
+
handler: async (ctx, { cursor = 0, revision, timeoutSeconds = 50, limit = 100, view } = {}) => {
|
|
106
128
|
if (revision !== undefined)
|
|
107
129
|
await companion(ctx, 'watch').watch({ revision, timeoutSeconds });
|
|
108
130
|
// The revision first: a change after it shows in the next sync. Then
|
|
109
|
-
// what the object does, and its conversation,
|
|
131
|
+
// what the object does, and its conversation, in an invocation of its
|
|
132
|
+
// own, which starts after the revision: a view reads the database as
|
|
133
|
+
// committed when it started, and this one started before the revision
|
|
134
|
+
// (by the whole wait, when it waited). So the page has every change the
|
|
135
|
+
// revision counts.
|
|
110
136
|
const now = await companion(ctx).notifications();
|
|
111
137
|
const status = await companion(ctx).status();
|
|
112
|
-
|
|
138
|
+
const request = { cursor, limit, ...(view ? { view } : {}) };
|
|
139
|
+
const page = (await ctx.rpc({
|
|
140
|
+
service: ctx.request.target.service,
|
|
141
|
+
key: ctx.key,
|
|
142
|
+
handler: 'messages',
|
|
143
|
+
input: request,
|
|
144
|
+
}));
|
|
145
|
+
return { revision: now.revision, status, ...page };
|
|
113
146
|
},
|
|
114
147
|
},
|
|
115
148
|
});
|
|
149
|
+
/** The views of every agent: {@link followed}'s. */
|
|
150
|
+
const FOLLOWED = followed(false);
|
|
116
151
|
/**
|
|
117
152
|
* The agents' handlers that only their own views call (Restate's ingress
|
|
118
153
|
* refuses them), and their types leave out: `erase`, `clear`'s work, once
|
|
119
154
|
* the handlers before it are done. It empties the key's database and its
|
|
120
|
-
* signals', and the
|
|
155
|
+
* signals', and the conversation's ids and cursors go on where they got to
|
|
156
|
+
* (conversation.ts).
|
|
121
157
|
*/
|
|
122
|
-
const
|
|
158
|
+
const internal = (items) => ({
|
|
123
159
|
erase: {
|
|
124
160
|
ingressPrivate: true,
|
|
125
161
|
handler: async (ctx) => {
|
|
126
|
-
|
|
162
|
+
// Read first, which also makes the reset apply the conversation's schema again.
|
|
163
|
+
await SCHEMA.ensure(ctx.db);
|
|
164
|
+
const start = await nextStart(ctx.db);
|
|
165
|
+
// A framework's database of its own first: the conversation's rows say what is left.
|
|
166
|
+
await items?.erase?.(ctx);
|
|
127
167
|
await ctx.db.transaction(async (tx) => {
|
|
128
168
|
await reset(tx);
|
|
129
|
-
await
|
|
169
|
+
await restart(tx, start);
|
|
130
170
|
});
|
|
131
|
-
await cleared(ctx);
|
|
132
171
|
await ctx.rpc(signals, signalsOf(ctx), { idempotencyKey: `${ctx.request.id}:reset` }).reset();
|
|
133
172
|
},
|
|
134
173
|
},
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
174
|
+
/**
|
|
175
|
+
* Close the latest turn, unanswered, if invocation `invocationId` ran it
|
|
176
|
+
* and is gone: the companion sends this once it finds the turn's
|
|
177
|
+
* invocation ended without answering (killed, say), so that the
|
|
178
|
+
* conversation shows the turn closed before the next message. Whether it
|
|
179
|
+
* closed it.
|
|
180
|
+
*/
|
|
181
|
+
settle: {
|
|
182
|
+
ingressPrivate: true,
|
|
183
|
+
handler: async (ctx, invocationId) => {
|
|
184
|
+
if (!items || !(await settle(ctx, items, invocationId)))
|
|
185
|
+
return false;
|
|
186
|
+
// The companion published as it closed the turn, before this stored how it ended: publish that too.
|
|
187
|
+
await publish(ctx);
|
|
188
|
+
return true;
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
/** Summarize the earliest turns, past the agent's budget. Whether it did. */
|
|
192
|
+
compact: {
|
|
193
|
+
ingressPrivate: true,
|
|
194
|
+
handler: async (ctx) => (items ? compact(ctx, items) : false),
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
/**
|
|
198
|
+
* The private `steered` of an agent whose `chat` is `chat` (`chatHandler`'s):
|
|
199
|
+
* it answers a message sent to `steer` as a turn of its own, with that
|
|
200
|
+
* `chat`, unless the turn it was sent to took it. The companion sends it as
|
|
201
|
+
* the message is sent, so that it keeps its place in the key's queue: it runs
|
|
202
|
+
* once the messages before it are answered.
|
|
203
|
+
*/
|
|
204
|
+
const steeredOf = ({ answer, steers }) => ({
|
|
205
|
+
ingressPrivate: true,
|
|
206
|
+
explicitCancellation: true,
|
|
207
|
+
handler: async (ctx, { id, text }) => {
|
|
208
|
+
if (await steerStored(ctx.db, id))
|
|
209
|
+
return;
|
|
210
|
+
await chatTurn(ctx, text, answer, steers);
|
|
211
|
+
},
|
|
140
212
|
});
|
|
141
213
|
/**
|
|
142
214
|
* A Restate virtual object with a SQL database per key, `ctx.db`: a
|
|
@@ -162,19 +234,19 @@ const startAfter = ({ messages: [latest], cursor, first = 0 }) => ({
|
|
|
162
234
|
* `schema`, optional: tables of your own, beside the agent's (which are its
|
|
163
235
|
* own business), made in each key's database as a session object makes
|
|
164
236
|
* them. Any name is yours but those starting with `restate_`, the agent's
|
|
165
|
-
* (`restate_pi_tasks`, `
|
|
166
|
-
* OpenCode's tables are in a database of their own.
|
|
237
|
+
* (`restate_pi_tasks`, `restate_agent_turns`, `restate_agent_items`...).
|
|
167
238
|
*
|
|
168
239
|
* `options` and `metadata` add to (or replace) the ones set here, such as
|
|
169
240
|
* `options: { retryPolicy: ... }`.
|
|
170
241
|
*/
|
|
171
242
|
export const sessionObject = (definition) => {
|
|
172
243
|
const { name, description, schema, background, handlers = {}, views = {}, metadata, options } = definition;
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
244
|
+
const { window = WINDOW, budget = BUDGET, summarize } = definition;
|
|
245
|
+
checkWindow(window);
|
|
246
|
+
if (!(Number.isInteger(budget) && budget >= 0)) {
|
|
247
|
+
throw new Error(`${name}: budget must be a number of tokens (0 or more), not ${JSON.stringify(budget)}`);
|
|
248
|
+
}
|
|
249
|
+
const own = [...Object.keys(BUILT_IN), ...Object.keys(FOLLOWED), ...Object.keys(internal(undefined)), 'steered'];
|
|
178
250
|
if ('messages' in handlers) {
|
|
179
251
|
throw new Error(`${name}: \`messages\` is a view: put it in \`views\`, so that the object is an agent`);
|
|
180
252
|
}
|
|
@@ -189,16 +261,32 @@ export const sessionObject = (definition) => {
|
|
|
189
261
|
]));
|
|
190
262
|
const messages = views.messages;
|
|
191
263
|
const page = (typeof messages === 'function' ? messages : messages?.handler);
|
|
192
|
-
|
|
264
|
+
// A `chat` that `chatHandler` made: its answer answers the steered messages no turn took (`steered`).
|
|
265
|
+
const chatEntry = handlers.chat;
|
|
266
|
+
const chat = typeof chatEntry === 'object' || typeof chatEntry === 'function' ? chatOf(chatEntry) : undefined;
|
|
267
|
+
const agent = page === undefined ? {} : followed(chat !== undefined);
|
|
268
|
+
// A framework on the conversation of turns: its view carries its items.
|
|
269
|
+
const items = (page && ITEMS.of(page));
|
|
270
|
+
// Its `compact` summarizes: without the conversation of turns, it has none, and nothing would.
|
|
271
|
+
if (summarize && !items) {
|
|
272
|
+
throw new Error(`${name}: \`summarize\` needs the conversation of turns: a framework's \`messages\` view (\`claude.messages()\`, say)`);
|
|
273
|
+
}
|
|
274
|
+
const { erase, ...stored } = internal(items);
|
|
275
|
+
const steering = chat ? { steered: steeredOf(chat) } : {};
|
|
276
|
+
const privates = messages === undefined
|
|
277
|
+
? {}
|
|
278
|
+
: items
|
|
279
|
+
? { erase, ...stored, ...steering }
|
|
280
|
+
: { erase, ...steering };
|
|
193
281
|
return withCompanion(isAgent(restate.sessionObject({
|
|
194
282
|
name,
|
|
195
283
|
description,
|
|
196
284
|
schema,
|
|
197
|
-
handlers: { ...
|
|
285
|
+
handlers: { ...privates, ...yours },
|
|
198
286
|
views: { ...BUILT_IN, ...agent, ...views },
|
|
199
287
|
metadata,
|
|
200
288
|
options: { ...OPTIONS, ...options },
|
|
201
|
-
}), { background,
|
|
289
|
+
}), { background, items, window, budget, summarize }));
|
|
202
290
|
};
|
|
203
291
|
/** `entry`, but a model provider's refusal it fails with ends its invocation, as `chat`'s does. */
|
|
204
292
|
function answering(entry) {
|
package/dist/object.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,+BAA+B,CAAC;AACtD,OAAO,
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,K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
+
* An Effect SqlClient over `db`, with its statements refused once `fence`
|
|
12
|
+
* closes. `committed` runs after each write is committed: a transaction that
|
|
13
|
+
* wrote, or a statement that writes outside one.
|
|
14
|
+
*/
|
|
15
|
+
export declare function sqlClient(db: Database, fence: Fence, committed?: () => void): Layer.Layer<SqlClient.SqlClient>;
|
|
16
|
+
/**
|
|
17
|
+
* What a boot does with the turns a previous process left claimed (OpenCode's
|
|
18
|
+
* execution claims), before any prompt of its own:
|
|
19
|
+
*
|
|
20
|
+
* - `resume`: OpenCode's own restart, however often the turn was resumed
|
|
21
|
+
* before (Restate's retry policy decides): a synthetic "Continue" message,
|
|
22
|
+
* its running tool calls failed, and a new step. A crashed attempt's turn
|
|
23
|
+
* goes on.
|
|
24
|
+
* - `close`: each is closed without asking the model: an idle marker
|
|
25
|
+
* (`Execution.Failed`) and the claim released. A killed invocation's turn
|
|
26
|
+
* is over. How, `closer` says.
|
|
27
|
+
* - `none`: nothing; the claims stay. A reader beside a running turn.
|
|
28
|
+
*/
|
|
29
|
+
export type Restart = 'resume' | 'close' | 'none';
|
|
30
|
+
/**
|
|
31
|
+
* How `restart: 'close'` closes a claimed turn:
|
|
32
|
+
*
|
|
33
|
+
* - `own`: a `SessionRestart` of ours. Each streaming or running tool call
|
|
34
|
+
* fails with `cutOff(toolCallId, name)` as its result, then the turn with
|
|
35
|
+
* `ended`, the claim released in the same transaction.
|
|
36
|
+
* - `budget`: OpenCode's own restart with no resumes left
|
|
37
|
+
* (`maxAttempts: 0`): the idle marker with OpenCode's text, the claim
|
|
38
|
+
* released; the running tool calls stay running until the next drain fails
|
|
39
|
+
* them ("Tool execution interrupted").
|
|
40
|
+
*/
|
|
41
|
+
export type Closer = 'own' | 'budget';
|
|
12
42
|
export interface OpenOptions {
|
|
13
43
|
/** OpenCode plugins: models, tools, agents. */
|
|
14
44
|
plugins?: Plugin.Plugin[];
|
|
@@ -20,21 +50,32 @@ export interface OpenOptions {
|
|
|
20
50
|
* session's directory. Without it, the workerd profile has none.
|
|
21
51
|
*/
|
|
22
52
|
local?: boolean;
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
53
|
+
/** What this boot does with the turns left claimed (default `resume`). */
|
|
54
|
+
restart?: Restart;
|
|
55
|
+
/** How `close` closes them (default `budget`: the spike found ours buys little over it). */
|
|
56
|
+
closer?: Closer;
|
|
57
|
+
/** The result of a tool call that a closed turn left running (`own` only). */
|
|
58
|
+
cutOff?: (toolCallId: string, name: string) => string;
|
|
59
|
+
/** The error a closed turn ends with (`own` only). */
|
|
60
|
+
ended?: string;
|
|
61
|
+
/** Runs after each of OpenCode's writes is committed: for a copy of what it wrote. */
|
|
62
|
+
committed?: () => void;
|
|
28
63
|
}
|
|
29
64
|
/** A booted OpenCode: `call` runs one of `client`'s calls (which return Effects); `close` shuts it down. */
|
|
30
65
|
export interface Host {
|
|
31
66
|
client: OpenCode.Interface;
|
|
32
67
|
call<A, E, R>(effect: Effect.Effect<A, E, R>): Promise<A>;
|
|
68
|
+
/**
|
|
69
|
+
* Settles once the boot's sweep of claimed turns is done: closed, for
|
|
70
|
+
* `close`; their resumes started, for `resume`. The host forks the sweep, so
|
|
71
|
+
* a prompt submitted before it settles may join a claimed turn.
|
|
72
|
+
*/
|
|
73
|
+
swept: Promise<void>;
|
|
33
74
|
close(): Promise<void>;
|
|
34
75
|
}
|
|
35
76
|
/**
|
|
36
|
-
* Boot OpenCode with its database in `db
|
|
37
|
-
*
|
|
77
|
+
* Boot OpenCode with its database in `db`; the boot sweeps the turns left
|
|
78
|
+
* claimed as `restart` says. Close the host when done.
|
|
38
79
|
*/
|
|
39
|
-
export declare function openOpenCode(db: Database, fence: Fence, { plugins, config, local,
|
|
80
|
+
export declare function openOpenCode(db: Database, fence: Fence, { plugins, config, local, restart, closer, committed, cutOff, ended, }: OpenOptions): Promise<Host>;
|
|
40
81
|
//# sourceMappingURL=db.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/opencode/db.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/opencode/db.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAqB,MAAM,EAAQ,KAAK,EAAiB,MAAM,QAAQ,CAAC;AAE/E,OAAO,EAAE,SAAS,EAAa,MAAM,YAAY,CAAC;AAclD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EAAkB,QAAQ,EAAY,MAAM,sBAAsB,CAAC;AAE/E,2CAA2C;AAC3C,MAAM,WAAW,KAAK;IACpB,MAAM,EAAE,OAAO,CAAC;CACjB;AAqDD;;;;GAIG;AACH,wBAAgB,SAAS,CACvB,EAAE,EAAE,QAAQ,EACZ,KAAK,EAAE,KAAK,EACZ,SAAS,GAAE,MAAM,IAAe,GAC/B,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,SAAS,CAAC,CAyElC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,OAAO,GAAG,QAAQ,GAAG,OAAO,GAAG,MAAM,CAAC;AAElD;;;;;;;;;;GAUG;AACH,MAAM,MAAM,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;AAEtC,MAAM,WAAW,WAAW;IAC1B,+CAA+C;IAC/C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;IAC1B,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,0EAA0E;IAC1E,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,4FAA4F;IAC5F,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,8EAA8E;IAC9E,MAAM,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;IACtD,sDAAsD;IACtD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sFAAsF;IACtF,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;CACxB;AAED,4GAA4G;AAC5G,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,QAAQ,CAAC,SAAS,CAAC;IAC3B,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAC1D;;;;OAIG;IACH,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACrB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAuFD;;;GAGG;AACH,wBAAsB,YAAY,CAChC,EAAE,EAAE,QAAQ,EACZ,KAAK,EAAE,KAAK,EACZ,EACE,OAAY,EACZ,MAAW,EACX,KAAa,EACb,OAAkB,EAClB,MAAiB,EACjB,SAAS,EACT,MAAsB,EACtB,KAAa,GACd,EAAE,WAAW,GACb,OAAO,CAAC,IAAI,CAAC,CAqCf"}
|
package/dist/opencode/db.js
CHANGED
|
@@ -1,27 +1,41 @@
|
|
|
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';
|
|
28
|
+
import { Bus } from '@opencode/core/bus';
|
|
21
29
|
import { Database as OpenCodeDatabase } from '@opencode/core/database/database';
|
|
30
|
+
import { fromPromise } from '@opencode/core/plugin/promise';
|
|
31
|
+
import { SessionEvent } from '@opencode/core/session/event';
|
|
32
|
+
import { SessionExecution } from '@opencode/core/session/execution';
|
|
22
33
|
import { SessionRestart } from '@opencode/core/session/execution/restart';
|
|
34
|
+
import { SessionStore } from '@opencode/core/session/store';
|
|
35
|
+
import { ConfigPluginSource } from '@opencode/core/config/plugin/source';
|
|
36
|
+
import { Pty } from '@opencode/core/pty';
|
|
37
|
+
import { Snapshot } from '@opencode/core/snapshot';
|
|
23
38
|
import { ServerWorkerd } from '@opencode/server/workerd';
|
|
24
|
-
import { fromPromise } from '@opencode/core/plugin/promise';
|
|
25
39
|
import { OpenCode } from '@opencode/sdk/effect';
|
|
26
40
|
const error = (operation, cause) => new SqlError({
|
|
27
41
|
reason: new UnknownError({
|
|
@@ -32,18 +46,24 @@ const error = (operation, cause) => new SqlError({
|
|
|
32
46
|
});
|
|
33
47
|
/** The connection inside a transaction, for statements of that transaction. */
|
|
34
48
|
const InTransaction = Context.Service('@restatedev/agents/OpenCodeTransaction');
|
|
49
|
+
/** A statement that writes. */
|
|
50
|
+
const WRITE = /^\s*(INSERT|UPDATE|DELETE|REPLACE)\b/i;
|
|
35
51
|
/**
|
|
36
52
|
* A connection whose statements go to `target`: the session, which runs one
|
|
37
53
|
* call at a time and holds them while a transaction runs, or that
|
|
38
|
-
* transaction's database. Each is refused once `fence` closes
|
|
54
|
+
* transaction's database. Each is refused once `fence` closes; `wrote` is
|
|
55
|
+
* told of each statement that writes.
|
|
39
56
|
*/
|
|
40
|
-
function connection(target, fence) {
|
|
57
|
+
function connection(target, fence, wrote = () => { }) {
|
|
41
58
|
// Effect binds what the compiler made of OpenCode's values: SQL values.
|
|
42
59
|
const exec = (read, sql, params) => Effect.tryPromise({
|
|
43
60
|
try: async () => {
|
|
44
61
|
if (fence.closed)
|
|
45
62
|
throw new Error('the attempt is over');
|
|
46
|
-
|
|
63
|
+
const result = await read(sql, params);
|
|
64
|
+
if (WRITE.test(sql))
|
|
65
|
+
wrote();
|
|
66
|
+
return result;
|
|
47
67
|
},
|
|
48
68
|
catch: (cause) => error('execute', cause),
|
|
49
69
|
});
|
|
@@ -61,8 +81,12 @@ function connection(target, fence) {
|
|
|
61
81
|
},
|
|
62
82
|
};
|
|
63
83
|
}
|
|
64
|
-
/**
|
|
65
|
-
|
|
84
|
+
/**
|
|
85
|
+
* An Effect SqlClient over `db`, with its statements refused once `fence`
|
|
86
|
+
* closes. `committed` runs after each write is committed: a transaction that
|
|
87
|
+
* wrote, or a statement that writes outside one.
|
|
88
|
+
*/
|
|
89
|
+
export function sqlClient(db, fence, committed = () => { }) {
|
|
66
90
|
// `effect` as one SQL transaction: it commits when `effect` succeeds
|
|
67
91
|
// and rolls back when it fails. Its statements go to the transaction's
|
|
68
92
|
// database.
|
|
@@ -74,12 +98,13 @@ export function sqlClient(db, fence) {
|
|
|
74
98
|
const rollback = new Error('rollback');
|
|
75
99
|
let interrupted = false;
|
|
76
100
|
let resumed = false;
|
|
101
|
+
let wrote = false;
|
|
77
102
|
// Settles when the transaction has committed, or rejects: with
|
|
78
103
|
// `rollback` once `effect` failed, or with the error of BEGIN or COMMIT.
|
|
79
104
|
const ended = db.transaction((tx) => new Promise((resolve, reject) => {
|
|
80
105
|
if (interrupted)
|
|
81
106
|
return reject(rollback);
|
|
82
|
-
const inner = Effect.provideContext(effect, Context.add(fiber.context, InTransaction, [connection(tx, fence), 0]));
|
|
107
|
+
const inner = Effect.provideContext(effect, Context.add(fiber.context, InTransaction, [connection(tx, fence, () => (wrote = true)), 0]));
|
|
83
108
|
resumed = true;
|
|
84
109
|
resume(
|
|
85
110
|
// A finalizer, so that the body settles however `effect`
|
|
@@ -97,6 +122,7 @@ export function sqlClient(db, fence) {
|
|
|
97
122
|
// does the transaction.
|
|
98
123
|
Effect.flatMap((value) => Effect.as(Effect.tryPromise({ try: () => ended, catch: (e) => error('commit', e) }), value))));
|
|
99
124
|
}));
|
|
125
|
+
ended.then(() => (wrote ? committed() : undefined), () => undefined);
|
|
100
126
|
const settled = ended.catch((cause) => {
|
|
101
127
|
// BEGIN failed: `effect` never ran.
|
|
102
128
|
if (!resumed && cause !== rollback)
|
|
@@ -110,8 +136,10 @@ export function sqlClient(db, fence) {
|
|
|
110
136
|
});
|
|
111
137
|
const make = Effect.gen(function* () {
|
|
112
138
|
const client = yield* SqlClient.make({
|
|
113
|
-
|
|
139
|
+
// Outside a transaction, a statement commits as it runs.
|
|
140
|
+
acquirer: Effect.succeed(connection(db, fence, committed)),
|
|
114
141
|
compiler: Statement.makeCompilerSqlite(),
|
|
142
|
+
driver: 'sqlite-restate',
|
|
115
143
|
spanAttributes: [['db.system.name', 'sqlite']],
|
|
116
144
|
transactionService: InTransaction,
|
|
117
145
|
});
|
|
@@ -119,42 +147,102 @@ export function sqlClient(db, fence) {
|
|
|
119
147
|
});
|
|
120
148
|
return Layer.effect(SqlClient.SqlClient, make).pipe(Layer.provide(Reactivity.layer));
|
|
121
149
|
}
|
|
150
|
+
/** The result `own` gives a running tool call of a closed turn, by default. */
|
|
151
|
+
const CUT_OFF = 'The turn ended before this call returned: it may or may not have happened.';
|
|
152
|
+
/** The error `own` closes a turn with, by default. */
|
|
153
|
+
const ENDED = 'Ended: the turn was killed before it finished.';
|
|
122
154
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
155
|
+
* A `SessionRestart` that closes each claimed top-level session this process
|
|
156
|
+
* does not run: OpenCode's budget's events (`Execution.Failed`, the release
|
|
157
|
+
* as its commit), and before them a `Tool.Failed` of our result for each
|
|
158
|
+
* streaming or running tool part. No synthetic message, no drain, no model.
|
|
126
159
|
*/
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
160
|
+
const closing = (cutOff, ended) => Layer.effect(SessionRestart.Service, Effect.gen(function* () {
|
|
161
|
+
const store = yield* SessionStore.Service;
|
|
162
|
+
const execution = yield* SessionExecution.Service;
|
|
163
|
+
const bus = yield* Bus.Service;
|
|
164
|
+
const close = Effect.fnUntraced(function* (sessionID) {
|
|
165
|
+
for (const message of yield* Effect.orDie(store.context(sessionID))) {
|
|
166
|
+
if (message.type !== 'assistant')
|
|
167
|
+
continue;
|
|
168
|
+
for (const part of message.content) {
|
|
169
|
+
if (part.type !== 'tool')
|
|
170
|
+
continue;
|
|
171
|
+
if (part.state.status !== 'streaming' && part.state.status !== 'running')
|
|
172
|
+
continue;
|
|
173
|
+
yield* bus.publish(SessionEvent.Tool.Failed, {
|
|
174
|
+
sessionID,
|
|
175
|
+
assistantMessageID: message.id,
|
|
176
|
+
id: part.id,
|
|
177
|
+
error: { type: 'aborted', message: cutOff(part.id, part.name) },
|
|
178
|
+
executed: part.executed === true,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
yield* bus.publish(SessionEvent.Execution.Failed, { sessionID, error: { type: 'aborted', message: ended } }, { commit: () => store.release(sessionID) });
|
|
183
|
+
});
|
|
184
|
+
return SessionRestart.Service.of({
|
|
185
|
+
resumeSuspendedSessions: Effect.gen(function* () {
|
|
186
|
+
const active = yield* execution.active;
|
|
187
|
+
// Child sessions' claims: v1 has no background subagents to recover.
|
|
188
|
+
yield* store.releaseChildClaims([]);
|
|
189
|
+
const claimed = (yield* store.listSuspended()).filter((id) => !active.has(id));
|
|
190
|
+
yield* Effect.forEach(claimed, close, { discard: true });
|
|
191
|
+
}),
|
|
192
|
+
});
|
|
193
|
+
}));
|
|
194
|
+
/** The sweep's service for `restart` and `closer`. */
|
|
195
|
+
const restarts = (restart, closer, cutOff, ended) => {
|
|
196
|
+
if (restart === 'none')
|
|
197
|
+
return Layer.succeed(SessionRestart.Service, { resumeSuspendedSessions: Effect.void });
|
|
198
|
+
if (restart === 'resume')
|
|
199
|
+
return SessionRestart.layer({ maxAttempts: Infinity });
|
|
200
|
+
return closer === 'budget' ? SessionRestart.layer({ maxAttempts: 0 }) : closing(cutOff, ended);
|
|
201
|
+
};
|
|
202
|
+
/** `service`, whose sweep completes `done` as it ends, however it ends. */
|
|
203
|
+
const observed = (layer, done) => Layer.effect(SessionRestart.Service, Effect.map(SessionRestart.Service, (inner) => SessionRestart.Service.of({
|
|
204
|
+
resumeSuspendedSessions: inner.resumeSuspendedSessions.pipe(Effect.onExit((exit) => Effect.asVoid(Deferred.done(done, Exit.asVoid(exit))))),
|
|
205
|
+
}))).pipe(Layer.provide(layer));
|
|
206
|
+
/** What a replacement replaces: `LayerNode`'s own record of it (its source node). */
|
|
207
|
+
const sourceOf = (replacement) => {
|
|
208
|
+
const key = Object.getOwnPropertySymbols(replacement).find((s) => s.description === 'LayerNode.Replacement');
|
|
209
|
+
return key && replacement[key]?.source;
|
|
210
|
+
};
|
|
130
211
|
/**
|
|
131
|
-
* Boot OpenCode with its database in `db
|
|
132
|
-
*
|
|
212
|
+
* Boot OpenCode with its database in `db`; the boot sweeps the turns left
|
|
213
|
+
* claimed as `restart` says. Close the host when done.
|
|
133
214
|
*/
|
|
134
|
-
export async function openOpenCode(db, fence, { plugins = [], config = {}, local = false, resume =
|
|
215
|
+
export async function openOpenCode(db, fence, { plugins = [], config = {}, local = false, restart = 'resume', closer = 'budget', committed, cutOff = () => CUT_OFF, ended = ENDED, }) {
|
|
135
216
|
// The workerd profile, but for its database, which comes first. (The
|
|
136
217
|
// Promise API's OpenCode.create takes no replacements; the Effect API's does.)
|
|
137
218
|
const profile = { storage: undefined, models: { fetch: false } };
|
|
138
|
-
//
|
|
139
|
-
//
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
|
|
219
|
+
// Taken by the node each replaces, not by position (2.1 added Npm's). Local
|
|
220
|
+
// keeps the stubs of snapshots, the pty and plugin discovery only.
|
|
221
|
+
const kept = local ? new Set([Snapshot.node, Pty.node, ConfigPluginSource.node]) : undefined;
|
|
222
|
+
const rest = ServerWorkerd.replacements(profile).filter((r) => {
|
|
223
|
+
const source = sourceOf(r);
|
|
224
|
+
if (source === undefined)
|
|
225
|
+
throw new Error('OpenCode: a workerd replacement of unknown node');
|
|
226
|
+
return source !== OpenCodeDatabase.node && (kept === undefined || kept.has(source));
|
|
227
|
+
});
|
|
143
228
|
const scope = Scope.makeUnsafe();
|
|
229
|
+
const done = Deferred.makeUnsafe();
|
|
230
|
+
const sweep = observed(restarts(restart, closer, cutOff, ended), done);
|
|
144
231
|
const client = await Effect.runPromise(OpenCode.create({
|
|
145
232
|
...ServerWorkerd.serverOptions({ ...profile, config: { content: JSON.stringify(config) } }),
|
|
146
233
|
plugins: plugins.map(fromPromise),
|
|
147
234
|
}, {
|
|
148
235
|
overrides: [
|
|
149
|
-
OpenCodeDatabase.node.replace(OpenCodeDatabase.configuredClient(sqlClient(db, fence))),
|
|
236
|
+
OpenCodeDatabase.node.replace(OpenCodeDatabase.configuredClient(sqlClient(db, fence, committed))),
|
|
150
237
|
...rest,
|
|
151
|
-
SessionRestart.node.replace(SessionRestart.node.mapLayer(() =>
|
|
238
|
+
SessionRestart.node.replace(SessionRestart.node.mapLayer(() => sweep)),
|
|
152
239
|
],
|
|
153
240
|
}).pipe(Scope.provide(scope)));
|
|
154
241
|
return {
|
|
155
242
|
client,
|
|
156
243
|
// The client's calls need nothing more: OpenCode provided it all.
|
|
157
244
|
call: (effect) => Effect.runPromise(effect),
|
|
245
|
+
swept: Effect.runPromise(Deferred.await(done)),
|
|
158
246
|
close: () => Effect.runPromise(Scope.close(scope, Exit.void)),
|
|
159
247
|
};
|
|
160
248
|
}
|